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 /** False when local commits or edits prevented a reused worktree from reaching the PR head. */
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 stderr can echo credentials from argv and remote URLs. Only a fixed
351// reason reaches the error and its diagnostic suffix; newer, unknown reasons
352// decode as absent through GitCommandError.
353export const GitCommandFailureReason = Schema.Literals([
354 "authentication_failed",
355 "branch_already_exists",
356 "branch_checked_out_in_worktree",
357 "config_locked",
358 "host_key_unverified",
359 "not_a_repository",
360 "path_already_exists",
361 "push_rejected_by_remote",
362 "push_rejected_non_fast_forward",
363 "ref_not_found",
364 "remote_unreachable",
365 "tag_would_be_clobbered",
366]);
367export type GitCommandFailureReason = typeof GitCommandFailureReason.Type;
368
369export class GitCommandError extends Schema.TaggedError<GitCommandError>()("GitCommandError", {
370 operation: Schema.String,
371 command: Schema.String,
372 cwd: Schema.String,
373 argumentCount: Schema.optional(Schema.Number),
374 exitCode: Schema.optional(Schema.Number),
375 stdoutLength: Schema.optional(Schema.Number),
376 stderrLength: Schema.optional(Schema.Number),
377 outputLength: Schema.optional(Schema.Number),
378 reason: ForwardCompatibleOptional(GitCommandFailureReason),
379 detail: Schema.String,
380 cause: Schema.optional(Schema.Defect()),
381}) {
382 override get message(): string {
383 const reason = this.reason === undefined ? "" : ` (${this.reason})`;
384 return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}${reason}`;
385 }
386}
387
388export class TextGenerationError extends Schema.TaggedError<TextGenerationError>()(
389 "TextGenerationError",
390 {
391 operation: Schema.String,
392 detail: Schema.String,
393 cause: Schema.optional(Schema.Defect()),
394 },
395) {
396 override get message(): string {
397 return `Text generation failed in ${this.operation}: ${this.detail}`;
398 }
399}
400
401export class GitManagerError extends Schema.TaggedError<GitManagerError>()("GitManagerError", {
402 operation: Schema.String,
403 cwd: Schema.String,
404 detail: Schema.String,
405 cause: Schema.optional(Schema.Defect()),
406}) {
407 override get message(): string {
408 return `Git manager failed in ${this.operation}: ${this.detail}`;
409 }
410}
411
412export class GitPullRequestMaterializationError extends Schema.TaggedError<GitPullRequestMaterializationError>()(
413 "GitPullRequestMaterializationError",
414 {
415 cwd: TrimmedNonEmptyStringSchema,
416 pullRequestNumber: PositiveInt,
417 headRepository: Schema.NullOr(TrimmedNonEmptyStringSchema),
418 headBranch: TrimmedNonEmptyStringSchema,
419 localBranch: TrimmedNonEmptyStringSchema,
420 cause: Schema.Defect(),
421 },
422) {
423 override get message(): string {
424 return `Failed to materialize pull request #${this.pullRequestNumber} branch ${this.headBranch} as ${this.localBranch}.`;
425 }
426}
427
428export const GitManagerServiceError = Schema.Union([
429 GitManagerError,
430 GitPullRequestMaterializationError,
431 GitCommandError,
432 SourceControlProviderError,
433 TextGenerationError,
434]);
435export type GitManagerServiceError = typeof GitManagerServiceError.Type;
436
437const GitActionProgressBase = Schema.Struct({
438 actionId: TrimmedNonEmptyStringSchema,
439 cwd: TrimmedNonEmptyStringSchema,
440 action: GitStackedAction,
441});
442
443const GitActionStartedEvent = Schema.Struct({
444 ...GitActionProgressBase.fields,
445 kind: Schema.Literal("action_started"),
446 phases: Schema.Array(GitActionProgressPhase),
447});
448const GitActionPhaseStartedEvent = Schema.Struct({
449 ...GitActionProgressBase.fields,
450 kind: Schema.Literal("phase_started"),
451 phase: GitActionProgressPhase,
452 label: TrimmedNonEmptyStringSchema,
453});
454const GitActionHookStartedEvent = Schema.Struct({
455 ...GitActionProgressBase.fields,
456 kind: Schema.Literal("hook_started"),
457 hookName: TrimmedNonEmptyStringSchema,
458});
459const GitActionHookOutputEvent = Schema.Struct({
460 ...GitActionProgressBase.fields,
461 kind: Schema.Literal("hook_output"),
462 hookName: Schema.NullOr(TrimmedNonEmptyStringSchema),
463 stream: GitActionProgressStream,
464 text: TrimmedNonEmptyStringSchema,
465});
466const GitActionHookFinishedEvent = Schema.Struct({
467 ...GitActionProgressBase.fields,
468 kind: Schema.Literal("hook_finished"),
469 hookName: TrimmedNonEmptyStringSchema,
470 exitCode: Schema.NullOr(Schema.Int),
471 durationMs: Schema.NullOr(NonNegativeInt),
472});
473const GitActionFinishedEvent = Schema.Struct({
474 ...GitActionProgressBase.fields,
475 kind: Schema.Literal("action_finished"),
476 result: GitRunStackedActionResult,
477});
478const GitActionFailedEvent = Schema.Struct({
479 ...GitActionProgressBase.fields,
480 kind: Schema.Literal("action_failed"),
481 phase: Schema.NullOr(GitActionProgressPhase),
482 message: TrimmedNonEmptyStringSchema,
483});
484
485export const GitActionProgressEvent = Schema.Union([
486 GitActionStartedEvent,
487 GitActionPhaseStartedEvent,
488 GitActionHookStartedEvent,
489 GitActionHookOutputEvent,
490 GitActionHookFinishedEvent,
491 GitActionFinishedEvent,
492 GitActionFailedEvent,
493]);
494export type GitActionProgressEvent = typeof GitActionProgressEvent.Type;