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