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

git.ts

510 lines17.2 KB

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