Open History to see the last commit
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 VcsStatusSubscriptionInput = Schema.Struct({
115 ...VcsStatusInput.fields,
116 /** Passive observers receive cached remote status without retaining its refresh loop. */
117 includeRemote: Schema.optional(Schema.Boolean),
118});
119export type VcsStatusSubscriptionInput = typeof VcsStatusSubscriptionInput.Type;
120
121export const VcsPullInput = Schema.Struct({
122 cwd: TrimmedNonEmptyStringSchema,
123});
124export type VcsPullInput = typeof VcsPullInput.Type;
125
126export const GitRunStackedActionInput = Schema.Struct({
127 actionId: TrimmedNonEmptyStringSchema,
128 cwd: TrimmedNonEmptyStringSchema,
129 action: GitStackedAction,
130 commitMessage: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(10_000))),
131 featureBranch: Schema.optional(Schema.Boolean),
132 filePaths: Schema.optional(Schema.Array(GitFilePath).check(Schema.isMinLength(1))),
133 threadId: Schema.optional(ThreadId),
134 projectId: Schema.optional(ProjectId),
135});
136export type GitRunStackedActionInput = typeof GitRunStackedActionInput.Type;
137
138export const VcsListRefsInput = Schema.Struct({
139 cwd: TrimmedNonEmptyStringSchema,
140 query: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(256))),
141 cursor: Schema.optional(NonNegativeInt),
142 includeMatchingRemoteRefs: Schema.optional(Schema.Boolean),
143 refKind: Schema.optional(Schema.Literals(["all", "local", "remote"])),
144 refresh: Schema.optional(Schema.Boolean),
145 limit: Schema.optional(
146 PositiveInt.check(Schema.isLessThanOrEqualTo(GIT_LIST_BRANCHES_MAX_LIMIT)),
147 ),
148});
149export type VcsListRefsInput = typeof VcsListRefsInput.Type;
150
151export const VcsCreateWorktreeInput = Schema.Struct({
152 cwd: TrimmedNonEmptyStringSchema,
153 refName: TrimmedNonEmptyStringSchema,
154 newRefName: Schema.optional(TrimmedNonEmptyStringSchema),
155 baseRefName: Schema.optional(TrimmedNonEmptyStringSchema),
156 path: Schema.NullOr(TrimmedNonEmptyStringSchema),
157});
158export type VcsCreateWorktreeInput = typeof VcsCreateWorktreeInput.Type;
159
160export const GitPullRequestRefInput = Schema.Struct({
161 cwd: TrimmedNonEmptyStringSchema,
162 reference: GitPullRequestReference,
163});
164export type GitPullRequestRefInput = typeof GitPullRequestRefInput.Type;
165
166export const GitPreparePullRequestThreadInput = Schema.Struct({
167 cwd: TrimmedNonEmptyStringSchema,
168 reference: GitPullRequestReference,
169 mode: GitPreparePullRequestThreadMode,
170 threadId: Schema.optional(ThreadId),
171});
172export type GitPreparePullRequestThreadInput = typeof GitPreparePullRequestThreadInput.Type;
173
174export const VcsRemoveWorktreeInput = Schema.Struct({
175 cwd: TrimmedNonEmptyStringSchema,
176 path: TrimmedNonEmptyStringSchema,
177 force: Schema.optional(Schema.Boolean),
178});
179export type VcsRemoveWorktreeInput = typeof VcsRemoveWorktreeInput.Type;
180
181export const VcsCreateRefInput = Schema.Struct({
182 cwd: TrimmedNonEmptyStringSchema,
183 refName: TrimmedNonEmptyStringSchema,
184 switchRef: Schema.optional(Schema.Boolean),
185});
186export type VcsCreateRefInput = typeof VcsCreateRefInput.Type;
187
188export const VcsCreateRefResult = Schema.Struct({
189 refName: TrimmedNonEmptyStringSchema,
190});
191export type VcsCreateRefResult = typeof VcsCreateRefResult.Type;
192
193export const VcsSwitchRefInput = Schema.Struct({
194 cwd: TrimmedNonEmptyStringSchema,
195 refName: TrimmedNonEmptyStringSchema,
196});
197export type VcsSwitchRefInput = typeof VcsSwitchRefInput.Type;
198
199export const VcsInitInput = Schema.Struct({
200 cwd: TrimmedNonEmptyStringSchema,
201 kind: Schema.optional(VcsDriverKind),
202});
203export type VcsInitInput = typeof VcsInitInput.Type;
204
205const VcsStatusChangeRequest = Schema.Struct({
206 number: PositiveInt,
207 title: TrimmedNonEmptyStringSchema,
208 url: Schema.String,
209 baseRef: TrimmedNonEmptyStringSchema,
210 headRef: TrimmedNonEmptyStringSchema,
211 state: VcsStatusChangeRequestState,
212 isDraft: Schema.optional(Schema.Boolean),
213 updatedAt: Schema.optional(Schema.NullOr(Schema.String)),
214});
215
216const VcsStatusLocalShape = {
217 isRepo: Schema.Boolean,
218 sourceControlProvider: Schema.optional(SourceControlProviderInfo),
219 hasPrimaryRemote: Schema.Boolean,
220 isDefaultRef: Schema.Boolean,
221 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
222 hasWorkingTreeChanges: Schema.Boolean,
223 workingTree: Schema.Struct({
224 // Absolute worktree root on the environment that every files[].path is
225 // relative to. Absent when the server could not resolve it.
226 rootPath: Schema.optional(GitFilePath),
227 files: Schema.Array(
228 Schema.Struct({
229 path: GitFilePath,
230 insertions: NonNegativeInt,
231 deletions: NonNegativeInt,
232 }),
233 ),
234 insertions: NonNegativeInt,
235 deletions: NonNegativeInt,
236 }),
237 branchChanges: Schema.optional(
238 Schema.Struct({
239 baseRef: Schema.NullOr(TrimmedNonEmptyStringSchema),
240 insertions: NonNegativeInt,
241 deletions: NonNegativeInt,
242 }),
243 ),
244};
245
246const VcsStatusRemoteShape = {
247 hasUpstream: Schema.Boolean,
248 aheadCount: NonNegativeInt,
249 behindCount: NonNegativeInt,
250 aheadOfDefaultCount: Schema.optional(NonNegativeInt),
251 pr: Schema.NullOr(VcsStatusChangeRequest),
252};
253
254export const VcsStatusLocalResult = Schema.Struct(VcsStatusLocalShape);
255export type VcsStatusLocalResult = typeof VcsStatusLocalResult.Type;
256
257export const VcsStatusRemoteResult = Schema.Struct(VcsStatusRemoteShape);
258export type VcsStatusRemoteResult = typeof VcsStatusRemoteResult.Type;
259
260export const VcsStatusResult = Schema.Struct({
261 ...VcsStatusLocalShape,
262 ...VcsStatusRemoteShape,
263});
264export type VcsStatusResult = typeof VcsStatusResult.Type;
265
266export const VcsStatusStreamEvent = Schema.Union([
267 Schema.TaggedStruct("snapshot", {
268 local: VcsStatusLocalResult,
269 remote: Schema.NullOr(VcsStatusRemoteResult),
270 }),
271 Schema.TaggedStruct("localUpdated", {
272 local: VcsStatusLocalResult,
273 }),
274 Schema.TaggedStruct("remoteUpdated", {
275 remote: Schema.NullOr(VcsStatusRemoteResult),
276 }),
277]);
278export type VcsStatusStreamEvent = typeof VcsStatusStreamEvent.Type;
279
280export const VcsListRefsResult = Schema.Struct({
281 isAtRepositoryRoot: Schema.optional(Schema.Boolean),
282 refs: Schema.Array(VcsRef),
283 isRepo: Schema.Boolean,
284 hasPrimaryRemote: Schema.Boolean,
285 nextCursor: NonNegativeInt.pipe(Schema.NullOr),
286 totalCount: NonNegativeInt,
287});
288export type VcsListRefsResult = typeof VcsListRefsResult.Type;
289
290export const VcsCreateWorktreeResult = Schema.Struct({
291 worktree: VcsWorktree,
292});
293export type VcsCreateWorktreeResult = typeof VcsCreateWorktreeResult.Type;
294
295export const GitResolvePullRequestResult = Schema.Struct({
296 pullRequest: GitResolvedPullRequest,
297});
298export type GitResolvePullRequestResult = typeof GitResolvePullRequestResult.Type;
299
300export const GitPreparePullRequestThreadResult = Schema.Struct({
301 pullRequest: GitResolvedPullRequest,
302 branch: TrimmedNonEmptyStringSchema,
303 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
304 /**
305 * False when the checkout could not be brought to the pull request head — a reused worktree
306 * holding local commits or uncommitted changes keeps its own state, so the code being handed
307 * over is older than the pull request.
308 */
309 isOnPullRequestHead: Schema.Boolean.pipe(Schema.withDecodingDefaultKey(Effect.succeed(true))),
310});
311export type GitPreparePullRequestThreadResult = typeof GitPreparePullRequestThreadResult.Type;
312
313export const VcsSwitchRefResult = Schema.Struct({
314 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
315});
316export type VcsSwitchRefResult = typeof VcsSwitchRefResult.Type;
317
318export const GitRunStackedActionResult = Schema.Struct({
319 action: GitStackedAction,
320 branch: Schema.Struct({
321 status: GitBranchStepStatus,
322 name: Schema.optional(TrimmedNonEmptyStringSchema),
323 }),
324 commit: Schema.Struct({
325 status: GitCommitStepStatus,
326 commitSha: Schema.optional(TrimmedNonEmptyStringSchema),
327 subject: Schema.optional(TrimmedNonEmptyStringSchema),
328 }),
329 push: Schema.Struct({
330 status: GitPushStepStatus,
331 branch: Schema.optional(TrimmedNonEmptyStringSchema),
332 upstreamBranch: Schema.optional(TrimmedNonEmptyStringSchema),
333 setUpstream: Schema.optional(Schema.Boolean),
334 }),
335 pr: Schema.Struct({
336 status: GitPrStepStatus,
337 url: Schema.optional(Schema.String),
338 number: Schema.optional(PositiveInt),
339 baseBranch: Schema.optional(TrimmedNonEmptyStringSchema),
340 headBranch: Schema.optional(TrimmedNonEmptyStringSchema),
341 title: Schema.optional(TrimmedNonEmptyStringSchema),
342 }),
343 toast: GitRunStackedActionToast,
344});
345export type GitRunStackedActionResult = typeof GitRunStackedActionResult.Type;
346
347export const VcsPullResult = Schema.Struct({
348 status: Schema.Literals(["pulled", "skipped_up_to_date"]),
349 refName: TrimmedNonEmptyStringSchema,
350 upstreamRef: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
351});
352export type VcsPullResult = typeof VcsPullResult.Type;
353
354// Git stderr can echo credentials from argv and remote URLs. Only a fixed
355// reason reaches the error and its diagnostic suffix; newer, unknown reasons
356// decode as absent through GitCommandError.
357export const GitCommandFailureReason = Schema.Literals([
358 "authentication_failed",
359 "branch_already_exists",
360 "branch_checked_out_in_worktree",
361 "host_key_unverified",
362 "not_a_repository",
363 "path_already_exists",
364 "ref_not_found",
365 "remote_unreachable",
366 "tag_would_be_clobbered",
367]);
368export type GitCommandFailureReason = typeof GitCommandFailureReason.Type;
369
370export class GitCommandError extends Schema.TaggedError<GitCommandError>()("GitCommandError", {
371 operation: Schema.String,
372 command: Schema.String,
373 cwd: Schema.String,
374 argumentCount: Schema.optional(Schema.Number),
375 exitCode: Schema.optional(Schema.Number),
376 stdoutLength: Schema.optional(Schema.Number),
377 stderrLength: Schema.optional(Schema.Number),
378 outputLength: Schema.optional(Schema.Number),
379 reason: ForwardCompatibleOptional(GitCommandFailureReason),
380 detail: Schema.String,
381 cause: Schema.optional(Schema.Defect()),
382}) {
383 override get message(): string {
384 const reason = this.reason === undefined ? "" : ` (${this.reason})`;
385 return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}${reason}`;
386 }
387}
388
389export class TextGenerationError extends Schema.TaggedError<TextGenerationError>()(
390 "TextGenerationError",
391 {
392 operation: Schema.String,
393 detail: Schema.String,
394 cause: Schema.optional(Schema.Defect()),
395 },
396) {
397 override get message(): string {
398 return `Text generation failed in ${this.operation}: ${this.detail}`;
399 }
400}
401
402export class GitManagerError extends Schema.TaggedError<GitManagerError>()("GitManagerError", {
403 operation: Schema.String,
404 cwd: Schema.String,
405 detail: Schema.String,
406 cause: Schema.optional(Schema.Defect()),
407}) {
408 override get message(): string {
409 return `Git manager failed in ${this.operation}: ${this.detail}`;
410 }
411}
412
413export class GitPullRequestMaterializationError extends Schema.TaggedError<GitPullRequestMaterializationError>()(
414 "GitPullRequestMaterializationError",
415 {
416 cwd: TrimmedNonEmptyStringSchema,
417 pullRequestNumber: PositiveInt,
418 headRepository: Schema.NullOr(TrimmedNonEmptyStringSchema),
419 headBranch: TrimmedNonEmptyStringSchema,
420 localBranch: TrimmedNonEmptyStringSchema,
421 cause: Schema.Defect(),
422 },
423) {
424 override get message(): string {
425 return `Failed to materialize pull request #${this.pullRequestNumber} branch ${this.headBranch} as ${this.localBranch}.`;
426 }
427}
428
429export const GitManagerServiceError = Schema.Union([
430 GitManagerError,
431 GitPullRequestMaterializationError,
432 GitCommandError,
433 SourceControlProviderError,
434 TextGenerationError,
435]);
436export type GitManagerServiceError = typeof GitManagerServiceError.Type;
437
438const GitActionProgressBase = Schema.Struct({
439 actionId: TrimmedNonEmptyStringSchema,
440 cwd: TrimmedNonEmptyStringSchema,
441 action: GitStackedAction,
442});
443
444const GitActionStartedEvent = Schema.Struct({
445 ...GitActionProgressBase.fields,
446 kind: Schema.Literal("action_started"),
447 phases: Schema.Array(GitActionProgressPhase),
448});
449const GitActionPhaseStartedEvent = Schema.Struct({
450 ...GitActionProgressBase.fields,
451 kind: Schema.Literal("phase_started"),
452 phase: GitActionProgressPhase,
453 label: TrimmedNonEmptyStringSchema,
454});
455const GitActionHookStartedEvent = Schema.Struct({
456 ...GitActionProgressBase.fields,
457 kind: Schema.Literal("hook_started"),
458 hookName: TrimmedNonEmptyStringSchema,
459});
460const GitActionHookOutputEvent = Schema.Struct({
461 ...GitActionProgressBase.fields,
462 kind: Schema.Literal("hook_output"),
463 hookName: Schema.NullOr(TrimmedNonEmptyStringSchema),
464 stream: GitActionProgressStream,
465 text: TrimmedNonEmptyStringSchema,
466});
467const GitActionHookFinishedEvent = Schema.Struct({
468 ...GitActionProgressBase.fields,
469 kind: Schema.Literal("hook_finished"),
470 hookName: TrimmedNonEmptyStringSchema,
471 exitCode: Schema.NullOr(Schema.Int),
472 durationMs: Schema.NullOr(NonNegativeInt),
473});
474const GitActionFinishedEvent = Schema.Struct({
475 ...GitActionProgressBase.fields,
476 kind: Schema.Literal("action_finished"),
477 result: GitRunStackedActionResult,
478});
479const GitActionFailedEvent = Schema.Struct({
480 ...GitActionProgressBase.fields,
481 kind: Schema.Literal("action_failed"),
482 phase: Schema.NullOr(GitActionProgressPhase),
483 message: TrimmedNonEmptyStringSchema,
484});
485
486export const GitActionProgressEvent = Schema.Union([
487 GitActionStartedEvent,
488 GitActionPhaseStartedEvent,
489 GitActionHookStartedEvent,
490 GitActionHookOutputEvent,
491 GitActionHookFinishedEvent,
492 GitActionFinishedEvent,
493 GitActionFailedEvent,
494]);
495export type GitActionProgressEvent = typeof GitActionProgressEvent.Type;