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

git.ts

509 lines17.6 KB

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 VcsBranchStatus = Schema.Struct({
115 refName: Schema.NullOr(Schema.String),
116 /** Absent from older servers, where a removed checkout looks like a detached HEAD. */
117 isRepo: Schema.optional(Schema.Boolean),
118});
119export type VcsBranchStatus = typeof VcsBranchStatus.Type;
120​
121export const VcsStatusSubscriptionInput = Schema.Struct({
122 ...VcsStatusInput.fields,
123 /** Passive observers receive cached remote status without retaining its refresh loop. */
124 includeRemote: Schema.optional(Schema.Boolean),
125});
126export type VcsStatusSubscriptionInput = typeof VcsStatusSubscriptionInput.Type;
127​
128export const VcsPullInput = Schema.Struct({
129 cwd: TrimmedNonEmptyStringSchema,
130});
131export type VcsPullInput = typeof VcsPullInput.Type;
132​
133export const GitRunStackedActionInput = Schema.Struct({
134 actionId: TrimmedNonEmptyStringSchema,
135 cwd: TrimmedNonEmptyStringSchema,
136 action: GitStackedAction,
137 commitMessage: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(10_000))),
138 featureBranch: Schema.optional(Schema.Boolean),
139 filePaths: Schema.optional(Schema.Array(GitFilePath).check(Schema.isMinLength(1))),
140 threadId: Schema.optional(ThreadId),
141 projectId: Schema.optional(ProjectId),
142});
143export type GitRunStackedActionInput = typeof GitRunStackedActionInput.Type;
144​
145export const VcsListRefsInput = Schema.Struct({
146 cwd: TrimmedNonEmptyStringSchema,
147 query: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(256))),
148 cursor: Schema.optional(NonNegativeInt),
149 includeMatchingRemoteRefs: Schema.optional(Schema.Boolean),
150 refKind: Schema.optional(Schema.Literals(["all", "local", "remote"])),
151 refresh: Schema.optional(Schema.Boolean),
152 limit: Schema.optional(
153 PositiveInt.check(Schema.isLessThanOrEqualTo(GIT_LIST_BRANCHES_MAX_LIMIT)),
154 ),
155});
156export type VcsListRefsInput = typeof VcsListRefsInput.Type;
157​
158export const VcsCreateWorktreeInput = Schema.Struct({
159 cwd: TrimmedNonEmptyStringSchema,
160 refName: TrimmedNonEmptyStringSchema,
161 newRefName: Schema.optional(TrimmedNonEmptyStringSchema),
162 baseRefName: Schema.optional(TrimmedNonEmptyStringSchema),
163 path: Schema.NullOr(TrimmedNonEmptyStringSchema),
164});
165export type VcsCreateWorktreeInput = typeof VcsCreateWorktreeInput.Type;
166​
167export const GitPullRequestRefInput = Schema.Struct({
168 cwd: TrimmedNonEmptyStringSchema,
169 reference: GitPullRequestReference,
170});
171export type GitPullRequestRefInput = typeof GitPullRequestRefInput.Type;
172​
173export const GitPreparePullRequestThreadInput = Schema.Struct({
174 cwd: TrimmedNonEmptyStringSchema,
175 reference: GitPullRequestReference,
176 mode: GitPreparePullRequestThreadMode,
177 threadId: Schema.optional(ThreadId),
178});
179export type GitPreparePullRequestThreadInput = typeof GitPreparePullRequestThreadInput.Type;
180​
181export const VcsRemoveWorktreeInput = Schema.Struct({
182 cwd: TrimmedNonEmptyStringSchema,
183 path: TrimmedNonEmptyStringSchema,
184 force: Schema.optional(Schema.Boolean),
185});
186export type VcsRemoveWorktreeInput = typeof VcsRemoveWorktreeInput.Type;
187​
188export const VcsCreateRefInput = Schema.Struct({
189 cwd: TrimmedNonEmptyStringSchema,
190 refName: TrimmedNonEmptyStringSchema,
191 switchRef: Schema.optional(Schema.Boolean),
192});
193export type VcsCreateRefInput = typeof VcsCreateRefInput.Type;
194​
195export const VcsCreateRefResult = Schema.Struct({
196 refName: TrimmedNonEmptyStringSchema,
197});
198export type VcsCreateRefResult = typeof VcsCreateRefResult.Type;
199​
200export const VcsSwitchRefInput = Schema.Struct({
201 cwd: TrimmedNonEmptyStringSchema,
202 refName: TrimmedNonEmptyStringSchema,
203});
204export type VcsSwitchRefInput = typeof VcsSwitchRefInput.Type;
205​
206export const VcsInitInput = Schema.Struct({
207 cwd: TrimmedNonEmptyStringSchema,
208 kind: Schema.optional(VcsDriverKind),
209});
210export type VcsInitInput = typeof VcsInitInput.Type;
211​
212const VcsStatusChangeRequest = Schema.Struct({
213 number: PositiveInt,
214 title: TrimmedNonEmptyStringSchema,
215 url: Schema.String,
216 baseRef: TrimmedNonEmptyStringSchema,
217 headRef: TrimmedNonEmptyStringSchema,
218 state: VcsStatusChangeRequestState,
219 isDraft: Schema.optional(Schema.Boolean),
220 updatedAt: Schema.optional(Schema.NullOr(Schema.String)),
221});
222​
223const VcsStatusLocalShape = {
224 isRepo: Schema.Boolean,
225 sourceControlProvider: Schema.optional(SourceControlProviderInfo),
226 hasPrimaryRemote: Schema.Boolean,
227 isDefaultRef: Schema.Boolean,
228 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
229 hasWorkingTreeChanges: Schema.Boolean,
230 workingTree: Schema.Struct({
231 // Absolute worktree root on the environment that every files[].path is
232 // relative to. Absent when the server could not resolve it.
233 rootPath: Schema.optional(GitFilePath),
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 branchChanges: Schema.optional(
245 Schema.Struct({
246 baseRef: Schema.NullOr(TrimmedNonEmptyStringSchema),
247 insertions: NonNegativeInt,
248 deletions: NonNegativeInt,
249 }),
250 ),
251};
252​
253const VcsStatusRemoteShape = {
254 hasUpstream: Schema.Boolean,
255 aheadCount: NonNegativeInt,
256 behindCount: NonNegativeInt,
257 aheadOfDefaultCount: Schema.optional(NonNegativeInt),
258 pr: Schema.NullOr(VcsStatusChangeRequest),
259};
260​
261export const VcsStatusLocalResult = Schema.Struct(VcsStatusLocalShape);
262export type VcsStatusLocalResult = typeof VcsStatusLocalResult.Type;
263​
264export const VcsStatusRemoteResult = Schema.Struct(VcsStatusRemoteShape);
265export type VcsStatusRemoteResult = typeof VcsStatusRemoteResult.Type;
266​
267export const VcsStatusResult = Schema.Struct({
268 ...VcsStatusLocalShape,
269 ...VcsStatusRemoteShape,
270});
271export type VcsStatusResult = typeof VcsStatusResult.Type;
272​
273export const VcsRefreshStatusResult = Schema.Struct({
274 ...VcsStatusResult.fields,
275 // Digest of the repository's refs after the refresh; absent when they could not be read.
276 refsDigest: Schema.optional(TrimmedNonEmptyString),
277});
278export type VcsRefreshStatusResult = typeof VcsRefreshStatusResult.Type;
279​
280export const VcsStatusStreamEvent = Schema.Union([
281 Schema.TaggedStruct("snapshot", {
282 local: VcsStatusLocalResult,
283 remote: Schema.NullOr(VcsStatusRemoteResult),
284 }),
285 Schema.TaggedStruct("localUpdated", {
286 local: VcsStatusLocalResult,
287 }),
288 Schema.TaggedStruct("remoteUpdated", {
289 remote: Schema.NullOr(VcsStatusRemoteResult),
290 }),
291]);
292export type VcsStatusStreamEvent = typeof VcsStatusStreamEvent.Type;
293​
294export const VcsListRefsResult = Schema.Struct({
295 isAtRepositoryRoot: Schema.optional(Schema.Boolean),
296 refs: Schema.Array(VcsRef),
297 isRepo: Schema.Boolean,
298 hasPrimaryRemote: Schema.Boolean,
299 nextCursor: NonNegativeInt.pipe(Schema.NullOr),
300 totalCount: NonNegativeInt,
301});
302export type VcsListRefsResult = typeof VcsListRefsResult.Type;
303​
304export const VcsCreateWorktreeResult = Schema.Struct({
305 worktree: VcsWorktree,
306});
307export type VcsCreateWorktreeResult = typeof VcsCreateWorktreeResult.Type;
308​
309export const GitResolvePullRequestResult = Schema.Struct({
310 pullRequest: GitResolvedPullRequest,
311});
312export type GitResolvePullRequestResult = typeof GitResolvePullRequestResult.Type;
313​
314export const GitPreparePullRequestThreadResult = Schema.Struct({
315 pullRequest: GitResolvedPullRequest,
316 branch: TrimmedNonEmptyStringSchema,
317 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
318 /** False when local commits or edits prevented a reused worktree from reaching the PR head. */
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// Git stderr can echo credentials from argv and remote URLs. Only a fixed
365// reason reaches the error and its diagnostic suffix; newer, unknown reasons
366// decode as absent through GitCommandError.
367export const GitCommandFailureReason = Schema.Literals([
368 "authentication_failed",
369 "branch_already_exists",
370 "branch_checked_out_in_worktree",
371 "config_locked",
372 "host_key_unverified",
373 "not_a_repository",
374 "path_already_exists",
375 "push_rejected_by_remote",
376 "push_rejected_non_fast_forward",
377 "ref_name_conflict",
378 "ref_not_found",
379 "remote_unreachable",
380 "tag_would_be_clobbered",
381]);
382export type GitCommandFailureReason = typeof GitCommandFailureReason.Type;
383​
384export class GitCommandError extends Schema.TaggedError<GitCommandError>()("GitCommandError", {
385 operation: Schema.String,
386 command: Schema.String,
387 cwd: Schema.String,
388 argumentCount: Schema.optional(Schema.Number),
389 exitCode: Schema.optional(Schema.Number),
390 stdoutLength: Schema.optional(Schema.Number),
391 stderrLength: Schema.optional(Schema.Number),
392 outputLength: Schema.optional(Schema.Number),
393 reason: ForwardCompatibleOptional(GitCommandFailureReason),
394 detail: Schema.String,
395 cause: Schema.optional(Schema.Defect()),
396}) {
397 override get message(): string {
398 const reason = this.reason === undefined ? "" : ` (${this.reason})`;
399 return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}${reason}`;
400 }
402​
403export class TextGenerationError extends Schema.TaggedError<TextGenerationError>()(
404 "TextGenerationError",
405 {
406 operation: Schema.String,
407 detail: Schema.String,
408 cause: Schema.optional(Schema.Defect()),
409 },
410) {
411 override get message(): string {
412 return `Text generation failed in ${this.operation}: ${this.detail}`;
413 }
415​
416export class GitManagerError extends Schema.TaggedError<GitManagerError>()("GitManagerError", {
417 operation: Schema.String,
418 cwd: Schema.String,
419 detail: Schema.String,
420 cause: Schema.optional(Schema.Defect()),
421}) {
422 override get message(): string {
423 return `Git manager failed in ${this.operation}: ${this.detail}`;
424 }
426​
427export class GitPullRequestMaterializationError extends Schema.TaggedError<GitPullRequestMaterializationError>()(
428 "GitPullRequestMaterializationError",
429 {
430 cwd: TrimmedNonEmptyStringSchema,
431 pullRequestNumber: PositiveInt,
432 headRepository: Schema.NullOr(TrimmedNonEmptyStringSchema),
433 headBranch: TrimmedNonEmptyStringSchema,
434 localBranch: TrimmedNonEmptyStringSchema,
435 cause: Schema.Defect(),
436 },
437) {
438 override get message(): string {
439 return `Failed to materialize pull request #${this.pullRequestNumber} branch ${this.headBranch} as ${this.localBranch}.`;
440 }
442​
443export const GitManagerServiceError = Schema.Union([
444 GitManagerError,
445 GitPullRequestMaterializationError,
446 GitCommandError,
447 SourceControlProviderError,
448 TextGenerationError,
449]);
450export type GitManagerServiceError = typeof GitManagerServiceError.Type;
451​
452const GitActionProgressBase = Schema.Struct({
453 actionId: TrimmedNonEmptyStringSchema,
454 cwd: TrimmedNonEmptyStringSchema,
455 action: GitStackedAction,
456});
457​
458const GitActionStartedEvent = Schema.Struct({
459 ...GitActionProgressBase.fields,
460 kind: Schema.Literal("action_started"),
461 phases: Schema.Array(GitActionProgressPhase),
462});
463const GitActionPhaseStartedEvent = Schema.Struct({
464 ...GitActionProgressBase.fields,
465 kind: Schema.Literal("phase_started"),
466 phase: GitActionProgressPhase,
467 label: TrimmedNonEmptyStringSchema,
468});
469const GitActionHookStartedEvent = Schema.Struct({
470 ...GitActionProgressBase.fields,
471 kind: Schema.Literal("hook_started"),
472 hookName: TrimmedNonEmptyStringSchema,
473});
474const GitActionHookOutputEvent = Schema.Struct({
475 ...GitActionProgressBase.fields,
476 kind: Schema.Literal("hook_output"),
477 hookName: Schema.NullOr(TrimmedNonEmptyStringSchema),
478 stream: GitActionProgressStream,
479 text: TrimmedNonEmptyStringSchema,
480});
481const GitActionHookFinishedEvent = Schema.Struct({
482 ...GitActionProgressBase.fields,
483 kind: Schema.Literal("hook_finished"),
484 hookName: TrimmedNonEmptyStringSchema,
485 exitCode: Schema.NullOr(Schema.Int),
486 durationMs: Schema.NullOr(NonNegativeInt),
487});
488const GitActionFinishedEvent = Schema.Struct({
489 ...GitActionProgressBase.fields,
490 kind: Schema.Literal("action_finished"),
491 result: GitRunStackedActionResult,
492});
493const GitActionFailedEvent = Schema.Struct({
494 ...GitActionProgressBase.fields,
495 kind: Schema.Literal("action_failed"),
496 phase: Schema.NullOr(GitActionProgressPhase),
497 message: TrimmedNonEmptyStringSchema,
498});
499​
500export const GitActionProgressEvent = Schema.Union([
501 GitActionStartedEvent,
502 GitActionPhaseStartedEvent,
503 GitActionHookStartedEvent,
504 GitActionHookOutputEvent,
505 GitActionHookFinishedEvent,
506 GitActionFinishedEvent,
507 GitActionFailedEvent,
508]);
509export type GitActionProgressEvent = typeof GitActionProgressEvent.Type;