Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

git.ts

507 lines17.4 KB

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