Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

git.ts

497 lines17.2 KB

1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import {
4 ForwardCompatibleOptional,
5 NonNegativeInt,
6 PositiveInt,
7 ProjectId,
8 ThreadId,
9 TrimmedNonEmptyString,
10} from "./baseSchemas.ts";
11import { SourceControlProviderError, SourceControlProviderInfo } from "./sourceControl.ts";
12import { VcsDriverKind } from "./vcs.ts";
13​
14const TrimmedNonEmptyStringSchema = TrimmedNonEmptyString;
15const GitFilePath = Schema.String.check(Schema.isNonEmpty());
16const GIT_LIST_BRANCHES_MAX_LIMIT = 200;
17​
18export const GitStackedAction = Schema.Literals([
19 "commit",
20 "push",
21 "create_pr",
22 "commit_push",
23 "commit_push_pr",
24]);
25export type GitStackedAction = typeof GitStackedAction.Type;
26export const GitActionProgressPhase = Schema.Literals(["branch", "commit", "push", "pr"]);
27export type GitActionProgressPhase = typeof GitActionProgressPhase.Type;
28export const GitActionProgressKind = Schema.Literals([
29 "action_started",
30 "phase_started",
31 "hook_started",
32 "hook_output",
33 "hook_finished",
34 "action_finished",
35 "action_failed",
36]);
37export type GitActionProgressKind = typeof GitActionProgressKind.Type;
38export const GitActionProgressStream = Schema.Literals(["stdout", "stderr"]);
39export type GitActionProgressStream = typeof GitActionProgressStream.Type;
40const GitCommitStepStatus = Schema.Literals([
41 "created",
42 "skipped_no_changes",
43 "skipped_not_requested",
44]);
45const GitPushStepStatus = Schema.Literals([
46 "pushed",
47 "skipped_not_requested",
48 "skipped_up_to_date",
49]);
50const GitBranchStepStatus = Schema.Literals(["created", "skipped_not_requested"]);
51const GitPrStepStatus = Schema.Literals(["created", "opened_existing", "skipped_not_requested"]);
52const VcsStatusChangeRequestState = Schema.Literals(["open", "closed", "merged"]);
53const GitPullRequestReference = TrimmedNonEmptyStringSchema;
54const GitPullRequestState = Schema.Literals(["open", "closed", "merged"]);
55const GitPreparePullRequestThreadMode = Schema.Literals(["local", "worktree"]);
56export const GitRunStackedActionToastRunAction = Schema.Struct({
57 kind: GitStackedAction,
58});
59export type GitRunStackedActionToastRunAction = typeof GitRunStackedActionToastRunAction.Type;
60const GitRunStackedActionToastCta = Schema.Union([
61 Schema.Struct({
62 kind: Schema.Literal("none"),
63 }),
64 Schema.Struct({
65 kind: Schema.Literal("open_pr"),
66 label: TrimmedNonEmptyStringSchema,
67 url: Schema.String,
68 }),
69 Schema.Struct({
70 kind: Schema.Literal("run_action"),
71 label: TrimmedNonEmptyStringSchema,
72 action: GitRunStackedActionToastRunAction,
73 }),
74]);
75export type GitRunStackedActionToastCta = typeof GitRunStackedActionToastCta.Type;
76const GitRunStackedActionToast = Schema.Struct({
77 title: TrimmedNonEmptyStringSchema,
78 description: Schema.optional(TrimmedNonEmptyStringSchema),
79 cta: GitRunStackedActionToastCta,
80});
81export type GitRunStackedActionToast = typeof GitRunStackedActionToast.Type;
82​
83export const VcsRef = Schema.Struct({
84 name: TrimmedNonEmptyStringSchema,
85 isRemote: Schema.optional(Schema.Boolean),
86 remoteName: Schema.optional(TrimmedNonEmptyStringSchema),
87 // Full Git ref, for example refs/remotes/origin/feature.
88 upstreamRef: Schema.optional(TrimmedNonEmptyStringSchema),
89 current: Schema.Boolean,
90 isDefault: Schema.Boolean,
91 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
92});
93export type VcsRef = typeof VcsRef.Type;
94​
95const VcsWorktree = Schema.Struct({
96 path: TrimmedNonEmptyStringSchema,
97 refName: TrimmedNonEmptyStringSchema,
98});
99const GitResolvedPullRequest = Schema.Struct({
100 number: PositiveInt,
101 title: TrimmedNonEmptyStringSchema,
102 url: Schema.String,
103 baseBranch: TrimmedNonEmptyStringSchema,
104 headBranch: TrimmedNonEmptyStringSchema,
105 state: GitPullRequestState,
106});
107export type GitResolvedPullRequest = typeof GitResolvedPullRequest.Type;
108​
109export const VcsStatusInput = Schema.Struct({
110 cwd: TrimmedNonEmptyStringSchema,
111});
112export type VcsStatusInput = typeof VcsStatusInput.Type;
113​
114export const VcsBranchStatus = Schema.Struct({ refName: Schema.NullOr(Schema.String) });
115export type VcsBranchStatus = typeof VcsBranchStatus.Type;
116​
117export const VcsStatusSubscriptionInput = Schema.Struct({
118 ...VcsStatusInput.fields,
119 /** Passive observers receive cached remote status without retaining its refresh loop. */
120 includeRemote: Schema.optional(Schema.Boolean),
121});
122export type VcsStatusSubscriptionInput = typeof VcsStatusSubscriptionInput.Type;
123​
124export const VcsPullInput = Schema.Struct({
125 cwd: TrimmedNonEmptyStringSchema,
126});
127export type VcsPullInput = typeof VcsPullInput.Type;
128​
129export const GitRunStackedActionInput = Schema.Struct({
130 actionId: TrimmedNonEmptyStringSchema,
131 cwd: TrimmedNonEmptyStringSchema,
132 action: GitStackedAction,
133 commitMessage: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(10_000))),
134 featureBranch: Schema.optional(Schema.Boolean),
135 filePaths: Schema.optional(Schema.Array(GitFilePath).check(Schema.isMinLength(1))),
136 threadId: Schema.optional(ThreadId),
137 projectId: Schema.optional(ProjectId),
138});
139export type GitRunStackedActionInput = typeof GitRunStackedActionInput.Type;
140​
141export const VcsListRefsInput = Schema.Struct({
142 cwd: TrimmedNonEmptyStringSchema,
143 query: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(256))),
144 cursor: Schema.optional(NonNegativeInt),
145 includeMatchingRemoteRefs: Schema.optional(Schema.Boolean),
146 refKind: Schema.optional(Schema.Literals(["all", "local", "remote"])),
147 refresh: Schema.optional(Schema.Boolean),
148 limit: Schema.optional(
149 PositiveInt.check(Schema.isLessThanOrEqualTo(GIT_LIST_BRANCHES_MAX_LIMIT)),
150 ),
151});
152export type VcsListRefsInput = typeof VcsListRefsInput.Type;
153​
154export const VcsCreateWorktreeInput = Schema.Struct({
155 cwd: TrimmedNonEmptyStringSchema,
156 refName: TrimmedNonEmptyStringSchema,
157 newRefName: Schema.optional(TrimmedNonEmptyStringSchema),
158 baseRefName: Schema.optional(TrimmedNonEmptyStringSchema),
159 path: Schema.NullOr(TrimmedNonEmptyStringSchema),
160});
161export type VcsCreateWorktreeInput = typeof VcsCreateWorktreeInput.Type;
162​
163export const GitPullRequestRefInput = Schema.Struct({
164 cwd: TrimmedNonEmptyStringSchema,
165 reference: GitPullRequestReference,
166});
167export type GitPullRequestRefInput = typeof GitPullRequestRefInput.Type;
168​
169export const GitPreparePullRequestThreadInput = Schema.Struct({
170 cwd: TrimmedNonEmptyStringSchema,
171 reference: GitPullRequestReference,
172 mode: GitPreparePullRequestThreadMode,
173 threadId: Schema.optional(ThreadId),
174});
175export type GitPreparePullRequestThreadInput = typeof GitPreparePullRequestThreadInput.Type;
176​
177export const VcsRemoveWorktreeInput = Schema.Struct({
178 cwd: TrimmedNonEmptyStringSchema,
179 path: TrimmedNonEmptyStringSchema,
180 force: Schema.optional(Schema.Boolean),
181});
182export type VcsRemoveWorktreeInput = typeof VcsRemoveWorktreeInput.Type;
183​
184export const VcsCreateRefInput = Schema.Struct({
185 cwd: TrimmedNonEmptyStringSchema,
186 refName: TrimmedNonEmptyStringSchema,
187 switchRef: Schema.optional(Schema.Boolean),
188});
189export type VcsCreateRefInput = typeof VcsCreateRefInput.Type;
190​
191export const VcsCreateRefResult = Schema.Struct({
192 refName: TrimmedNonEmptyStringSchema,
193});
194export type VcsCreateRefResult = typeof VcsCreateRefResult.Type;
195​
196export const VcsSwitchRefInput = Schema.Struct({
197 cwd: TrimmedNonEmptyStringSchema,
198 refName: TrimmedNonEmptyStringSchema,
199});
200export type VcsSwitchRefInput = typeof VcsSwitchRefInput.Type;
201​
202export const VcsInitInput = Schema.Struct({
203 cwd: TrimmedNonEmptyStringSchema,
204 kind: Schema.optional(VcsDriverKind),
205});
206export type VcsInitInput = typeof VcsInitInput.Type;
207​
208const VcsStatusChangeRequest = Schema.Struct({
209 number: PositiveInt,
210 title: TrimmedNonEmptyStringSchema,
211 url: Schema.String,
212 baseRef: TrimmedNonEmptyStringSchema,
213 headRef: TrimmedNonEmptyStringSchema,
214 state: VcsStatusChangeRequestState,
215 isDraft: Schema.optional(Schema.Boolean),
216 updatedAt: Schema.optional(Schema.NullOr(Schema.String)),
217});
218​
219const VcsStatusLocalShape = {
220 isRepo: Schema.Boolean,
221 sourceControlProvider: Schema.optional(SourceControlProviderInfo),
222 hasPrimaryRemote: Schema.Boolean,
223 isDefaultRef: Schema.Boolean,
224 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
225 hasWorkingTreeChanges: Schema.Boolean,
226 workingTree: Schema.Struct({
227 // Absolute worktree root on the environment that every files[].path is
228 // relative to. Absent when the server could not resolve it.
229 rootPath: Schema.optional(GitFilePath),
230 files: Schema.Array(
231 Schema.Struct({
232 path: GitFilePath,
233 insertions: NonNegativeInt,
234 deletions: NonNegativeInt,
235 }),
236 ),
237 insertions: NonNegativeInt,
238 deletions: NonNegativeInt,
239 }),
240 branchChanges: Schema.optional(
241 Schema.Struct({
242 baseRef: Schema.NullOr(TrimmedNonEmptyStringSchema),
243 insertions: NonNegativeInt,
244 deletions: NonNegativeInt,
245 }),
246 ),
247};
248​
249const VcsStatusRemoteShape = {
250 hasUpstream: Schema.Boolean,
251 aheadCount: NonNegativeInt,
252 behindCount: NonNegativeInt,
253 aheadOfDefaultCount: Schema.optional(NonNegativeInt),
254 pr: Schema.NullOr(VcsStatusChangeRequest),
255};
256​
257export const VcsStatusLocalResult = Schema.Struct(VcsStatusLocalShape);
258export type VcsStatusLocalResult = typeof VcsStatusLocalResult.Type;
259​
260export const VcsStatusRemoteResult = Schema.Struct(VcsStatusRemoteShape);
261export type VcsStatusRemoteResult = typeof VcsStatusRemoteResult.Type;
262​
263export const VcsStatusResult = Schema.Struct({
264 ...VcsStatusLocalShape,
265 ...VcsStatusRemoteShape,
266});
267export type VcsStatusResult = typeof VcsStatusResult.Type;
268​
269export const VcsStatusStreamEvent = Schema.Union([
270 Schema.TaggedStruct("snapshot", {
271 local: VcsStatusLocalResult,
272 remote: Schema.NullOr(VcsStatusRemoteResult),
273 }),
274 Schema.TaggedStruct("localUpdated", {
275 local: VcsStatusLocalResult,
276 }),
277 Schema.TaggedStruct("remoteUpdated", {
278 remote: Schema.NullOr(VcsStatusRemoteResult),
279 }),
280]);
281export type VcsStatusStreamEvent = typeof VcsStatusStreamEvent.Type;
282​
283export const VcsListRefsResult = Schema.Struct({
284 isAtRepositoryRoot: Schema.optional(Schema.Boolean),
285 refs: Schema.Array(VcsRef),
286 isRepo: Schema.Boolean,
287 hasPrimaryRemote: Schema.Boolean,
288 nextCursor: NonNegativeInt.pipe(Schema.NullOr),
289 totalCount: NonNegativeInt,
290});
291export type VcsListRefsResult = typeof VcsListRefsResult.Type;
292​
293export const VcsCreateWorktreeResult = Schema.Struct({
294 worktree: VcsWorktree,
295});
296export type VcsCreateWorktreeResult = typeof VcsCreateWorktreeResult.Type;
297​
298export const GitResolvePullRequestResult = Schema.Struct({
299 pullRequest: GitResolvedPullRequest,
300});
301export type GitResolvePullRequestResult = typeof GitResolvePullRequestResult.Type;
302​
303export const GitPreparePullRequestThreadResult = Schema.Struct({
304 pullRequest: GitResolvedPullRequest,
305 branch: TrimmedNonEmptyStringSchema,
306 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
307 /** False when local commits or edits prevented a reused worktree from reaching the PR head. */
308 isOnPullRequestHead: Schema.Boolean.pipe(Schema.withDecodingDefaultKey(Effect.succeed(true))),
309});
310export type GitPreparePullRequestThreadResult = typeof GitPreparePullRequestThreadResult.Type;
311​
312export const VcsSwitchRefResult = Schema.Struct({
313 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
314});
315export type VcsSwitchRefResult = typeof VcsSwitchRefResult.Type;
316​
317export const GitRunStackedActionResult = Schema.Struct({
318 action: GitStackedAction,
319 branch: Schema.Struct({
320 status: GitBranchStepStatus,
321 name: Schema.optional(TrimmedNonEmptyStringSchema),
322 }),
323 commit: Schema.Struct({
324 status: GitCommitStepStatus,
325 commitSha: Schema.optional(TrimmedNonEmptyStringSchema),
326 subject: Schema.optional(TrimmedNonEmptyStringSchema),
327 }),
328 push: Schema.Struct({
329 status: GitPushStepStatus,
330 branch: Schema.optional(TrimmedNonEmptyStringSchema),
331 upstreamBranch: Schema.optional(TrimmedNonEmptyStringSchema),
332 setUpstream: Schema.optional(Schema.Boolean),
333 }),
334 pr: Schema.Struct({
335 status: GitPrStepStatus,
336 url: Schema.optional(Schema.String),
337 number: Schema.optional(PositiveInt),
338 baseBranch: Schema.optional(TrimmedNonEmptyStringSchema),
339 headBranch: Schema.optional(TrimmedNonEmptyStringSchema),
340 title: Schema.optional(TrimmedNonEmptyStringSchema),
341 }),
342 toast: GitRunStackedActionToast,
343});
344export type GitRunStackedActionResult = typeof GitRunStackedActionResult.Type;
345​
346export const VcsPullResult = Schema.Struct({
347 status: Schema.Literals(["pulled", "skipped_up_to_date"]),
348 refName: TrimmedNonEmptyStringSchema,
349 upstreamRef: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
350});
351export type VcsPullResult = typeof VcsPullResult.Type;
352​
353// Git stderr can echo credentials from argv and remote URLs. Only a fixed
354// reason reaches the error and its diagnostic suffix; newer, unknown reasons
355// decode as absent through GitCommandError.
356export const GitCommandFailureReason = Schema.Literals([
357 "authentication_failed",
358 "branch_already_exists",
359 "branch_checked_out_in_worktree",
360 "config_locked",
361 "host_key_unverified",
362 "not_a_repository",
363 "path_already_exists",
364 "push_rejected_by_remote",
365 "push_rejected_non_fast_forward",
366 "ref_not_found",
367 "remote_unreachable",
368 "tag_would_be_clobbered",
369]);
370export type GitCommandFailureReason = typeof GitCommandFailureReason.Type;
371​
372export class GitCommandError extends Schema.TaggedError<GitCommandError>()("GitCommandError", {
373 operation: Schema.String,
374 command: Schema.String,
375 cwd: Schema.String,
376 argumentCount: Schema.optional(Schema.Number),
377 exitCode: Schema.optional(Schema.Number),
378 stdoutLength: Schema.optional(Schema.Number),
379 stderrLength: Schema.optional(Schema.Number),
380 outputLength: Schema.optional(Schema.Number),
381 reason: ForwardCompatibleOptional(GitCommandFailureReason),
382 detail: Schema.String,
383 cause: Schema.optional(Schema.Defect()),
384}) {
385 override get message(): string {
386 const reason = this.reason === undefined ? "" : ` (${this.reason})`;
387 return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}${reason}`;
388 }
390​
391export class TextGenerationError extends Schema.TaggedError<TextGenerationError>()(
392 "TextGenerationError",
393 {
394 operation: Schema.String,
395 detail: Schema.String,
396 cause: Schema.optional(Schema.Defect()),
397 },
398) {
399 override get message(): string {
400 return `Text generation failed in ${this.operation}: ${this.detail}`;
401 }
403​
404export class GitManagerError extends Schema.TaggedError<GitManagerError>()("GitManagerError", {
405 operation: Schema.String,
406 cwd: Schema.String,
407 detail: Schema.String,
408 cause: Schema.optional(Schema.Defect()),
409}) {
410 override get message(): string {
411 return `Git manager failed in ${this.operation}: ${this.detail}`;
412 }
414​
415export class GitPullRequestMaterializationError extends Schema.TaggedError<GitPullRequestMaterializationError>()(
416 "GitPullRequestMaterializationError",
417 {
418 cwd: TrimmedNonEmptyStringSchema,
419 pullRequestNumber: PositiveInt,
420 headRepository: Schema.NullOr(TrimmedNonEmptyStringSchema),
421 headBranch: TrimmedNonEmptyStringSchema,
422 localBranch: TrimmedNonEmptyStringSchema,
423 cause: Schema.Defect(),
424 },
425) {
426 override get message(): string {
427 return `Failed to materialize pull request #${this.pullRequestNumber} branch ${this.headBranch} as ${this.localBranch}.`;
428 }
430​
431export const GitManagerServiceError = Schema.Union([
432 GitManagerError,
433 GitPullRequestMaterializationError,
434 GitCommandError,
435 SourceControlProviderError,
436 TextGenerationError,
437]);
438export type GitManagerServiceError = typeof GitManagerServiceError.Type;
439​
440const GitActionProgressBase = Schema.Struct({
441 actionId: TrimmedNonEmptyStringSchema,
442 cwd: TrimmedNonEmptyStringSchema,
443 action: GitStackedAction,
444});
445​
446const GitActionStartedEvent = Schema.Struct({
447 ...GitActionProgressBase.fields,
448 kind: Schema.Literal("action_started"),
449 phases: Schema.Array(GitActionProgressPhase),
450});
451const GitActionPhaseStartedEvent = Schema.Struct({
452 ...GitActionProgressBase.fields,
453 kind: Schema.Literal("phase_started"),
454 phase: GitActionProgressPhase,
455 label: TrimmedNonEmptyStringSchema,
456});
457const GitActionHookStartedEvent = Schema.Struct({
458 ...GitActionProgressBase.fields,
459 kind: Schema.Literal("hook_started"),
460 hookName: TrimmedNonEmptyStringSchema,
461});
462const GitActionHookOutputEvent = Schema.Struct({
463 ...GitActionProgressBase.fields,
464 kind: Schema.Literal("hook_output"),
465 hookName: Schema.NullOr(TrimmedNonEmptyStringSchema),
466 stream: GitActionProgressStream,
467 text: TrimmedNonEmptyStringSchema,
468});
469const GitActionHookFinishedEvent = Schema.Struct({
470 ...GitActionProgressBase.fields,
471 kind: Schema.Literal("hook_finished"),
472 hookName: TrimmedNonEmptyStringSchema,
473 exitCode: Schema.NullOr(Schema.Int),
474 durationMs: Schema.NullOr(NonNegativeInt),
475});
476const GitActionFinishedEvent = Schema.Struct({
477 ...GitActionProgressBase.fields,
478 kind: Schema.Literal("action_finished"),
479 result: GitRunStackedActionResult,
480});
481const GitActionFailedEvent = Schema.Struct({
482 ...GitActionProgressBase.fields,
483 kind: Schema.Literal("action_failed"),
484 phase: Schema.NullOr(GitActionProgressPhase),
485 message: TrimmedNonEmptyStringSchema,
486});
487​
488export const GitActionProgressEvent = Schema.Union([
489 GitActionStartedEvent,
490 GitActionPhaseStartedEvent,
491 GitActionHookStartedEvent,
492 GitActionHookOutputEvent,
493 GitActionHookFinishedEvent,
494 GitActionFinishedEvent,
495 GitActionFailedEvent,
496]);
497export type GitActionProgressEvent = typeof GitActionProgressEvent.Type;