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