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

git.ts

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