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 GIT_LIST_BRANCHES_MAX_LIMIT = 200;
15
16// Domain Types
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 current: Schema.Boolean,
88 isDefault: Schema.Boolean,
89 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
90});
91export type VcsRef = typeof VcsRef.Type;
92
93const VcsWorktree = Schema.Struct({
94 path: TrimmedNonEmptyStringSchema,
95 refName: TrimmedNonEmptyStringSchema,
96});
97const GitResolvedPullRequest = Schema.Struct({
98 number: PositiveInt,
99 title: TrimmedNonEmptyStringSchema,
100 url: Schema.String,
101 baseBranch: TrimmedNonEmptyStringSchema,
102 headBranch: TrimmedNonEmptyStringSchema,
103 state: GitPullRequestState,
104});
105export type GitResolvedPullRequest = typeof GitResolvedPullRequest.Type;
106
107// RPC Inputs
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(
133 Schema.Array(TrimmedNonEmptyStringSchema).check(Schema.isMinLength(1)),
134 ),
135 /** The thread the action runs beside; a pull request it creates is linked to it. */
136 threadId: Schema.optional(ThreadId),
137 projectId: Schema.optional(ProjectId),
138});
139export type GitRunStackedActionInput = typeof GitRunStackedActionInput.Type;
140
141export const VcsListRefsInput = Schema.Struct({
142 cwd: TrimmedNonEmptyStringSchema,
143 query: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(256))),
144 cursor: Schema.optional(NonNegativeInt),
145 includeMatchingRemoteRefs: Schema.optional(Schema.Boolean),
146 refKind: Schema.optional(Schema.Literals(["all", "local", "remote"])),
147 refresh: Schema.optional(Schema.Boolean),
148 limit: Schema.optional(
149 PositiveInt.check(Schema.isLessThanOrEqualTo(GIT_LIST_BRANCHES_MAX_LIMIT)),
150 ),
151});
152export type VcsListRefsInput = typeof VcsListRefsInput.Type;
153
154export const VcsCreateWorktreeInput = Schema.Struct({
155 cwd: TrimmedNonEmptyStringSchema,
156 refName: TrimmedNonEmptyStringSchema,
157 newRefName: Schema.optional(TrimmedNonEmptyStringSchema),
158 baseRefName: Schema.optional(TrimmedNonEmptyStringSchema),
159 path: Schema.NullOr(TrimmedNonEmptyStringSchema),
160});
161export type VcsCreateWorktreeInput = typeof VcsCreateWorktreeInput.Type;
162
163export const GitPullRequestRefInput = Schema.Struct({
164 cwd: TrimmedNonEmptyStringSchema,
165 reference: GitPullRequestReference,
166});
167export type GitPullRequestRefInput = typeof GitPullRequestRefInput.Type;
168
169export const GitPreparePullRequestThreadInput = Schema.Struct({
170 cwd: TrimmedNonEmptyStringSchema,
171 reference: GitPullRequestReference,
172 mode: GitPreparePullRequestThreadMode,
173 threadId: Schema.optional(ThreadId),
174});
175export type GitPreparePullRequestThreadInput = typeof GitPreparePullRequestThreadInput.Type;
176
177export const VcsRemoveWorktreeInput = Schema.Struct({
178 cwd: TrimmedNonEmptyStringSchema,
179 path: TrimmedNonEmptyStringSchema,
180 force: Schema.optional(Schema.Boolean),
181});
182export type VcsRemoveWorktreeInput = typeof VcsRemoveWorktreeInput.Type;
183
184export const VcsCreateRefInput = Schema.Struct({
185 cwd: TrimmedNonEmptyStringSchema,
186 refName: TrimmedNonEmptyStringSchema,
187 switchRef: Schema.optional(Schema.Boolean),
188});
189export type VcsCreateRefInput = typeof VcsCreateRefInput.Type;
190
191export const VcsCreateRefResult = Schema.Struct({
192 refName: TrimmedNonEmptyStringSchema,
193});
194export type VcsCreateRefResult = typeof VcsCreateRefResult.Type;
195
196export const VcsSwitchRefInput = Schema.Struct({
197 cwd: TrimmedNonEmptyStringSchema,
198 refName: TrimmedNonEmptyStringSchema,
199});
200export type VcsSwitchRefInput = typeof VcsSwitchRefInput.Type;
201
202export const VcsInitInput = Schema.Struct({
203 cwd: TrimmedNonEmptyStringSchema,
204 kind: Schema.optional(VcsDriverKind),
205});
206export type VcsInitInput = typeof VcsInitInput.Type;
207
208// RPC Results
209
210const VcsStatusChangeRequest = Schema.Struct({
211 number: PositiveInt,
212 title: TrimmedNonEmptyStringSchema,
213 url: Schema.String,
214 baseRef: TrimmedNonEmptyStringSchema,
215 headRef: TrimmedNonEmptyStringSchema,
216 state: VcsStatusChangeRequestState,
217 /** Optional for compatibility with older servers and providers. */
218 isDraft: Schema.optional(Schema.Boolean),
219 /**
220 * Last provider-side activity (ISO), including comments and metadata edits.
221 * This is not the time a change request closed or merged. Optional for old
222 * servers and providers whose lookups do not report it.
223 */
224 updatedAt: Schema.optional(Schema.NullOr(Schema.String)),
225});
226
227const VcsStatusLocalShape = {
228 isRepo: Schema.Boolean,
229 sourceControlProvider: Schema.optional(SourceControlProviderInfo),
230 hasPrimaryRemote: Schema.Boolean,
231 isDefaultRef: Schema.Boolean,
232 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
233 hasWorkingTreeChanges: Schema.Boolean,
234 workingTree: Schema.Struct({
235 files: Schema.Array(
236 Schema.Struct({
237 path: TrimmedNonEmptyStringSchema,
238 insertions: NonNegativeInt,
239 deletions: NonNegativeInt,
240 }),
241 ),
242 insertions: NonNegativeInt,
243 deletions: NonNegativeInt,
244 }),
245 /**
246 * Totals for the diff panel's Changes view: merge-base with the base branch to the
247 * working tree, untracked files included. Absent on older servers.
248 */
249 branchChanges: Schema.optional(
250 Schema.Struct({
251 baseRef: Schema.NullOr(TrimmedNonEmptyStringSchema),
252 insertions: NonNegativeInt,
253 deletions: NonNegativeInt,
254 }),
255 ),
256};
257
258const VcsStatusRemoteShape = {
259 hasUpstream: Schema.Boolean,
260 aheadCount: NonNegativeInt,
261 behindCount: NonNegativeInt,
262 aheadOfDefaultCount: Schema.optional(NonNegativeInt),
263 pr: Schema.NullOr(VcsStatusChangeRequest),
264};
265
266export const VcsStatusLocalResult = Schema.Struct(VcsStatusLocalShape);
267export type VcsStatusLocalResult = typeof VcsStatusLocalResult.Type;
268
269export const VcsStatusRemoteResult = Schema.Struct(VcsStatusRemoteShape);
270export type VcsStatusRemoteResult = typeof VcsStatusRemoteResult.Type;
271
272export const VcsStatusResult = Schema.Struct({
273 ...VcsStatusLocalShape,
274 ...VcsStatusRemoteShape,
275});
276export type VcsStatusResult = typeof VcsStatusResult.Type;
277
278export const VcsStatusStreamEvent = Schema.Union([
279 Schema.TaggedStruct("snapshot", {
280 local: VcsStatusLocalResult,
281 remote: Schema.NullOr(VcsStatusRemoteResult),
282 }),
283 Schema.TaggedStruct("localUpdated", {
284 local: VcsStatusLocalResult,
285 }),
286 Schema.TaggedStruct("remoteUpdated", {
287 remote: Schema.NullOr(VcsStatusRemoteResult),
288 }),
289]);
290export type VcsStatusStreamEvent = typeof VcsStatusStreamEvent.Type;
291
292export const VcsListRefsResult = Schema.Struct({
293 refs: Schema.Array(VcsRef),
294 isRepo: Schema.Boolean,
295 hasPrimaryRemote: Schema.Boolean,
296 nextCursor: NonNegativeInt.pipe(Schema.NullOr),
297 totalCount: NonNegativeInt,
298});
299export type VcsListRefsResult = typeof VcsListRefsResult.Type;
300
301export const VcsCreateWorktreeResult = Schema.Struct({
302 worktree: VcsWorktree,
303});
304export type VcsCreateWorktreeResult = typeof VcsCreateWorktreeResult.Type;
305
306export const GitResolvePullRequestResult = Schema.Struct({
307 pullRequest: GitResolvedPullRequest,
308});
309export type GitResolvePullRequestResult = typeof GitResolvePullRequestResult.Type;
310
311export const GitPreparePullRequestThreadResult = Schema.Struct({
312 pullRequest: GitResolvedPullRequest,
313 branch: TrimmedNonEmptyStringSchema,
314 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
315 /**
316 * False when the checkout could not be brought to the pull request head — a reused worktree
317 * holding local commits or uncommitted changes keeps its own state, so the code being handed
318 * over is older than the pull request.
319 */
320 isOnPullRequestHead: Schema.Boolean.pipe(Schema.withDecodingDefaultKey(Effect.succeed(true))),
321});
322export type GitPreparePullRequestThreadResult = typeof GitPreparePullRequestThreadResult.Type;
323
324export const VcsSwitchRefResult = Schema.Struct({
325 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
326});
327export type VcsSwitchRefResult = typeof VcsSwitchRefResult.Type;
328
329export const GitRunStackedActionResult = Schema.Struct({
330 action: GitStackedAction,
331 branch: Schema.Struct({
332 status: GitBranchStepStatus,
333 name: Schema.optional(TrimmedNonEmptyStringSchema),
334 }),
335 commit: Schema.Struct({
336 status: GitCommitStepStatus,
337 commitSha: Schema.optional(TrimmedNonEmptyStringSchema),
338 subject: Schema.optional(TrimmedNonEmptyStringSchema),
339 }),
340 push: Schema.Struct({
341 status: GitPushStepStatus,
342 branch: Schema.optional(TrimmedNonEmptyStringSchema),
343 upstreamBranch: Schema.optional(TrimmedNonEmptyStringSchema),
344 setUpstream: Schema.optional(Schema.Boolean),
345 }),
346 pr: Schema.Struct({
347 status: GitPrStepStatus,
348 url: Schema.optional(Schema.String),
349 number: Schema.optional(PositiveInt),
350 baseBranch: Schema.optional(TrimmedNonEmptyStringSchema),
351 headBranch: Schema.optional(TrimmedNonEmptyStringSchema),
352 title: Schema.optional(TrimmedNonEmptyStringSchema),
353 }),
354 toast: GitRunStackedActionToast,
355});
356export type GitRunStackedActionResult = typeof GitRunStackedActionResult.Type;
357
358export const VcsPullResult = Schema.Struct({
359 status: Schema.Literals(["pulled", "skipped_up_to_date"]),
360 refName: TrimmedNonEmptyStringSchema,
361 upstreamRef: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
362});
363export type VcsPullResult = typeof VcsPullResult.Type;
364
365// RPC / domain errors
366
367// Well-known git failures, recognized from stderr at the driver and carried as
368// a closed set of diagnostic tags. Git's stderr itself stays off the error: it
369// echoes argv and remote URLs, which can hold credentials. The tag names the
370// cause for logs and callers; it does not select a message.
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 "remote_unreachable",
379 "tag_would_be_clobbered",
380]);
381export type GitCommandFailureReason = typeof GitCommandFailureReason.Type;
382
383export class GitCommandError extends Schema.TaggedError<GitCommandError>()("GitCommandError", {
384 operation: Schema.String,
385 command: Schema.String,
386 cwd: Schema.String,
387 argumentCount: Schema.optional(Schema.Number),
388 exitCode: Schema.optional(Schema.Number),
389 stdoutLength: Schema.optional(Schema.Number),
390 stderrLength: Schema.optional(Schema.Number),
391 outputLength: Schema.optional(Schema.Number),
392 reason: Schema.optional(GitCommandFailureReason),
393 detail: Schema.String,
394 cause: Schema.optional(Schema.Defect()),
395}) {
396 override get message(): string {
397 const reason = this.reason === undefined ? "" : ` (${this.reason})`;
398 return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}${reason}`;
399 }
400}
401
402export class TextGenerationError extends Schema.TaggedError<TextGenerationError>()(
403 "TextGenerationError",
404 {
405 operation: Schema.String,
406 detail: Schema.String,
407 cause: Schema.optional(Schema.Defect()),
408 },
409) {
410 override get message(): string {
411 return `Text generation failed in ${this.operation}: ${this.detail}`;
412 }
413}
414
415export class GitManagerError extends Schema.TaggedError<GitManagerError>()("GitManagerError", {
416 operation: Schema.String,
417 cwd: Schema.String,
418 detail: Schema.String,
419 cause: Schema.optional(Schema.Defect()),
420}) {
421 override get message(): string {
422 return `Git manager failed in ${this.operation}: ${this.detail}`;
423 }
424}
425
426export class GitPullRequestMaterializationError extends Schema.TaggedError<GitPullRequestMaterializationError>()(
427 "GitPullRequestMaterializationError",
428 {
429 cwd: TrimmedNonEmptyStringSchema,
430 pullRequestNumber: PositiveInt,
431 headRepository: Schema.NullOr(TrimmedNonEmptyStringSchema),
432 headBranch: TrimmedNonEmptyStringSchema,
433 localBranch: TrimmedNonEmptyStringSchema,
434 cause: Schema.Defect(),
435 },
436) {
437 override get message(): string {
438 return `Failed to materialize pull request #${this.pullRequestNumber} branch ${this.headBranch} as ${this.localBranch}.`;
439 }
440}
441
442export const GitManagerServiceError = Schema.Union([
443 GitManagerError,
444 GitPullRequestMaterializationError,
445 GitCommandError,
446 SourceControlProviderError,
447 TextGenerationError,
448]);
449export type GitManagerServiceError = typeof GitManagerServiceError.Type;
450
451const GitActionProgressBase = Schema.Struct({
452 actionId: TrimmedNonEmptyStringSchema,
453 cwd: TrimmedNonEmptyStringSchema,
454 action: GitStackedAction,
455});
456
457const GitActionStartedEvent = Schema.Struct({
458 ...GitActionProgressBase.fields,
459 kind: Schema.Literal("action_started"),
460 phases: Schema.Array(GitActionProgressPhase),
461});
462const GitActionPhaseStartedEvent = Schema.Struct({
463 ...GitActionProgressBase.fields,
464 kind: Schema.Literal("phase_started"),
465 phase: GitActionProgressPhase,
466 label: TrimmedNonEmptyStringSchema,
467});
468const GitActionHookStartedEvent = Schema.Struct({
469 ...GitActionProgressBase.fields,
470 kind: Schema.Literal("hook_started"),
471 hookName: TrimmedNonEmptyStringSchema,
472});
473const GitActionHookOutputEvent = Schema.Struct({
474 ...GitActionProgressBase.fields,
475 kind: Schema.Literal("hook_output"),
476 hookName: Schema.NullOr(TrimmedNonEmptyStringSchema),
477 stream: GitActionProgressStream,
478 text: TrimmedNonEmptyStringSchema,
479});
480const GitActionHookFinishedEvent = Schema.Struct({
481 ...GitActionProgressBase.fields,
482 kind: Schema.Literal("hook_finished"),
483 hookName: TrimmedNonEmptyStringSchema,
484 exitCode: Schema.NullOr(Schema.Int),
485 durationMs: Schema.NullOr(NonNegativeInt),
486});
487const GitActionFinishedEvent = Schema.Struct({
488 ...GitActionProgressBase.fields,
489 kind: Schema.Literal("action_finished"),
490 result: GitRunStackedActionResult,
491});
492const GitActionFailedEvent = Schema.Struct({
493 ...GitActionProgressBase.fields,
494 kind: Schema.Literal("action_failed"),
495 phase: Schema.NullOr(GitActionProgressPhase),
496 message: TrimmedNonEmptyStringSchema,
497});
498
499export const GitActionProgressEvent = Schema.Union([
500 GitActionStartedEvent,
501 GitActionPhaseStartedEvent,
502 GitActionHookStartedEvent,
503 GitActionHookOutputEvent,
504 GitActionHookFinishedEvent,
505 GitActionFinishedEvent,
506 GitActionFailedEvent,
507]);
508export type GitActionProgressEvent = typeof GitActionProgressEvent.Type;