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