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

git.ts

516 lines17.3 KB

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