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 files: Schema.Array(
222 Schema.Struct({
223 path: GitFilePath,
224 insertions: NonNegativeInt,
225 deletions: NonNegativeInt,
226 }),
227 ),
228 insertions: NonNegativeInt,
229 deletions: NonNegativeInt,
230 }),
231 branchChanges: Schema.optional(
232 Schema.Struct({
233 baseRef: Schema.NullOr(TrimmedNonEmptyStringSchema),
234 insertions: NonNegativeInt,
235 deletions: NonNegativeInt,
236 }),
237 ),
238};
239
240const VcsStatusRemoteShape = {
241 hasUpstream: Schema.Boolean,
242 aheadCount: NonNegativeInt,
243 behindCount: NonNegativeInt,
244 aheadOfDefaultCount: Schema.optional(NonNegativeInt),
245 pr: Schema.NullOr(VcsStatusChangeRequest),
246};
247
248export const VcsStatusLocalResult = Schema.Struct(VcsStatusLocalShape);
249export type VcsStatusLocalResult = typeof VcsStatusLocalResult.Type;
250
251export const VcsStatusRemoteResult = Schema.Struct(VcsStatusRemoteShape);
252export type VcsStatusRemoteResult = typeof VcsStatusRemoteResult.Type;
253
254export const VcsStatusResult = Schema.Struct({
255 ...VcsStatusLocalShape,
256 ...VcsStatusRemoteShape,
257});
258export type VcsStatusResult = typeof VcsStatusResult.Type;
259
260export const VcsStatusStreamEvent = Schema.Union([
261 Schema.TaggedStruct("snapshot", {
262 local: VcsStatusLocalResult,
263 remote: Schema.NullOr(VcsStatusRemoteResult),
264 }),
265 Schema.TaggedStruct("localUpdated", {
266 local: VcsStatusLocalResult,
267 }),
268 Schema.TaggedStruct("remoteUpdated", {
269 remote: Schema.NullOr(VcsStatusRemoteResult),
270 }),
271]);
272export type VcsStatusStreamEvent = typeof VcsStatusStreamEvent.Type;
273
274export const VcsListRefsResult = Schema.Struct({
275 refs: Schema.Array(VcsRef),
276 isRepo: Schema.Boolean,
277 hasPrimaryRemote: Schema.Boolean,
278 nextCursor: NonNegativeInt.pipe(Schema.NullOr),
279 totalCount: NonNegativeInt,
280});
281export type VcsListRefsResult = typeof VcsListRefsResult.Type;
282
283export const VcsCreateWorktreeResult = Schema.Struct({
284 worktree: VcsWorktree,
285});
286export type VcsCreateWorktreeResult = typeof VcsCreateWorktreeResult.Type;
287
288export const GitResolvePullRequestResult = Schema.Struct({
289 pullRequest: GitResolvedPullRequest,
290});
291export type GitResolvePullRequestResult = typeof GitResolvePullRequestResult.Type;
292
293export const GitPreparePullRequestThreadResult = Schema.Struct({
294 pullRequest: GitResolvedPullRequest,
295 branch: TrimmedNonEmptyStringSchema,
296 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
297 /**
298 * False when the checkout could not be brought to the pull request head — a reused worktree
299 * holding local commits or uncommitted changes keeps its own state, so the code being handed
300 * over is older than the pull request.
301 */
302 isOnPullRequestHead: Schema.Boolean.pipe(Schema.withDecodingDefaultKey(Effect.succeed(true))),
303});
304export type GitPreparePullRequestThreadResult = typeof GitPreparePullRequestThreadResult.Type;
305
306export const VcsSwitchRefResult = Schema.Struct({
307 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
308});
309export type VcsSwitchRefResult = typeof VcsSwitchRefResult.Type;
310
311export const GitRunStackedActionResult = Schema.Struct({
312 action: GitStackedAction,
313 branch: Schema.Struct({
314 status: GitBranchStepStatus,
315 name: Schema.optional(TrimmedNonEmptyStringSchema),
316 }),
317 commit: Schema.Struct({
318 status: GitCommitStepStatus,
319 commitSha: Schema.optional(TrimmedNonEmptyStringSchema),
320 subject: Schema.optional(TrimmedNonEmptyStringSchema),
321 }),
322 push: Schema.Struct({
323 status: GitPushStepStatus,
324 branch: Schema.optional(TrimmedNonEmptyStringSchema),
325 upstreamBranch: Schema.optional(TrimmedNonEmptyStringSchema),
326 setUpstream: Schema.optional(Schema.Boolean),
327 }),
328 pr: Schema.Struct({
329 status: GitPrStepStatus,
330 url: Schema.optional(Schema.String),
331 number: Schema.optional(PositiveInt),
332 baseBranch: Schema.optional(TrimmedNonEmptyStringSchema),
333 headBranch: Schema.optional(TrimmedNonEmptyStringSchema),
334 title: Schema.optional(TrimmedNonEmptyStringSchema),
335 }),
336 toast: GitRunStackedActionToast,
337});
338export type GitRunStackedActionResult = typeof GitRunStackedActionResult.Type;
339
340export const VcsPullResult = Schema.Struct({
341 status: Schema.Literals(["pulled", "skipped_up_to_date"]),
342 refName: TrimmedNonEmptyStringSchema,
343 upstreamRef: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
344});
345export type VcsPullResult = typeof VcsPullResult.Type;
346
347// Git's stderr itself stays off the error: it echoes argv and remote URLs,
348// which can hold credentials. The tag names the cause for logs and callers; it
349// does not select a message.
350export const GitCommandFailureReason = Schema.Literals([
351 "authentication_failed",
352 "branch_already_exists",
353 "branch_checked_out_in_worktree",
354 "host_key_unverified",
355 "not_a_repository",
356 "path_already_exists",
357 "remote_unreachable",
358 "tag_would_be_clobbered",
359]);
360export type GitCommandFailureReason = typeof GitCommandFailureReason.Type;
361
362export class GitCommandError extends Schema.TaggedError<GitCommandError>()("GitCommandError", {
363 operation: Schema.String,
364 command: Schema.String,
365 cwd: Schema.String,
366 argumentCount: Schema.optional(Schema.Number),
367 exitCode: Schema.optional(Schema.Number),
368 stdoutLength: Schema.optional(Schema.Number),
369 stderrLength: Schema.optional(Schema.Number),
370 outputLength: Schema.optional(Schema.Number),
371 reason: Schema.optional(GitCommandFailureReason),
372 detail: Schema.String,
373 cause: Schema.optional(Schema.Defect()),
374}) {
375 override get message(): string {
376 const reason = this.reason === undefined ? "" : ` (${this.reason})`;
377 return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}${reason}`;
378 }
379}
380
381export class TextGenerationError extends Schema.TaggedError<TextGenerationError>()(
382 "TextGenerationError",
383 {
384 operation: Schema.String,
385 detail: Schema.String,
386 cause: Schema.optional(Schema.Defect()),
387 },
388) {
389 override get message(): string {
390 return `Text generation failed in ${this.operation}: ${this.detail}`;
391 }
392}
393
394export class GitManagerError extends Schema.TaggedError<GitManagerError>()("GitManagerError", {
395 operation: Schema.String,
396 cwd: Schema.String,
397 detail: Schema.String,
398 cause: Schema.optional(Schema.Defect()),
399}) {
400 override get message(): string {
401 return `Git manager failed in ${this.operation}: ${this.detail}`;
402 }
403}
404
405export class GitPullRequestMaterializationError extends Schema.TaggedError<GitPullRequestMaterializationError>()(
406 "GitPullRequestMaterializationError",
407 {
408 cwd: TrimmedNonEmptyStringSchema,
409 pullRequestNumber: PositiveInt,
410 headRepository: Schema.NullOr(TrimmedNonEmptyStringSchema),
411 headBranch: TrimmedNonEmptyStringSchema,
412 localBranch: TrimmedNonEmptyStringSchema,
413 cause: Schema.Defect(),
414 },
415) {
416 override get message(): string {
417 return `Failed to materialize pull request #${this.pullRequestNumber} branch ${this.headBranch} as ${this.localBranch}.`;
418 }
419}
420
421export const GitManagerServiceError = Schema.Union([
422 GitManagerError,
423 GitPullRequestMaterializationError,
424 GitCommandError,
425 SourceControlProviderError,
426 TextGenerationError,
427]);
428export type GitManagerServiceError = typeof GitManagerServiceError.Type;
429
430const GitActionProgressBase = Schema.Struct({
431 actionId: TrimmedNonEmptyStringSchema,
432 cwd: TrimmedNonEmptyStringSchema,
433 action: GitStackedAction,
434});
435
436const GitActionStartedEvent = Schema.Struct({
437 ...GitActionProgressBase.fields,
438 kind: Schema.Literal("action_started"),
439 phases: Schema.Array(GitActionProgressPhase),
440});
441const GitActionPhaseStartedEvent = Schema.Struct({
442 ...GitActionProgressBase.fields,
443 kind: Schema.Literal("phase_started"),
444 phase: GitActionProgressPhase,
445 label: TrimmedNonEmptyStringSchema,
446});
447const GitActionHookStartedEvent = Schema.Struct({
448 ...GitActionProgressBase.fields,
449 kind: Schema.Literal("hook_started"),
450 hookName: TrimmedNonEmptyStringSchema,
451});
452const GitActionHookOutputEvent = Schema.Struct({
453 ...GitActionProgressBase.fields,
454 kind: Schema.Literal("hook_output"),
455 hookName: Schema.NullOr(TrimmedNonEmptyStringSchema),
456 stream: GitActionProgressStream,
457 text: TrimmedNonEmptyStringSchema,
458});
459const GitActionHookFinishedEvent = Schema.Struct({
460 ...GitActionProgressBase.fields,
461 kind: Schema.Literal("hook_finished"),
462 hookName: TrimmedNonEmptyStringSchema,
463 exitCode: Schema.NullOr(Schema.Int),
464 durationMs: Schema.NullOr(NonNegativeInt),
465});
466const GitActionFinishedEvent = Schema.Struct({
467 ...GitActionProgressBase.fields,
468 kind: Schema.Literal("action_finished"),
469 result: GitRunStackedActionResult,
470});
471const GitActionFailedEvent = Schema.Struct({
472 ...GitActionProgressBase.fields,
473 kind: Schema.Literal("action_failed"),
474 phase: Schema.NullOr(GitActionProgressPhase),
475 message: TrimmedNonEmptyStringSchema,
476});
477
478export const GitActionProgressEvent = Schema.Union([
479 GitActionStartedEvent,
480 GitActionPhaseStartedEvent,
481 GitActionHookStartedEvent,
482 GitActionHookOutputEvent,
483 GitActionHookFinishedEvent,
484 GitActionFinishedEvent,
485 GitActionFailedEvent,
486]);
487export type GitActionProgressEvent = typeof GitActionProgressEvent.Type;