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

vcs.ts

286 lines8.6 KB

1import * as Schema from "effect/Schema";
2import { NonNegativeInt, TrimmedNonEmptyString } from "./baseSchemas.ts";
3​
4export const VcsDriverKind = Schema.Literals(["git", "jj", "unknown"]);
5export type VcsDriverKind = typeof VcsDriverKind.Type;
6​
7export const VcsFreshnessSource = Schema.Literals([
8 "live-local",
9 "cached-local",
10 "cached-remote",
11 "explicit-remote",
12]);
13export type VcsFreshnessSource = typeof VcsFreshnessSource.Type;
14​
15export const VcsFreshness = Schema.Struct({
16 source: VcsFreshnessSource,
17 observedAt: Schema.DateTimeUtc,
18 expiresAt: Schema.Option(Schema.DateTimeUtc),
19});
20export type VcsFreshness = typeof VcsFreshness.Type;
21​
22export const VcsDriverCapabilities = Schema.Struct({
23 kind: VcsDriverKind,
24 supportsWorktrees: Schema.Boolean,
25 supportsBookmarks: Schema.Boolean,
26 supportsAtomicSnapshot: Schema.Boolean,
27 supportsPushDefaultRemote: Schema.Boolean,
28 ignoreClassifier: Schema.Literals(["native", "git-compatible-fallback"]),
29});
30export type VcsDriverCapabilities = typeof VcsDriverCapabilities.Type;
31​
32export const VcsRepositoryIdentity = Schema.Struct({
33 kind: VcsDriverKind,
34 rootPath: TrimmedNonEmptyString,
35 metadataPath: Schema.NullOr(TrimmedNonEmptyString),
36 freshness: VcsFreshness,
37});
38export type VcsRepositoryIdentity = typeof VcsRepositoryIdentity.Type;
39​
40export const VcsListWorkspaceFilesResult = Schema.Struct({
41 paths: Schema.Array(TrimmedNonEmptyString),
42 truncated: Schema.Boolean,
43 freshness: VcsFreshness,
44});
45export type VcsListWorkspaceFilesResult = typeof VcsListWorkspaceFilesResult.Type;
46​
47export const VcsRemote = Schema.Struct({
48 name: TrimmedNonEmptyString,
49 url: TrimmedNonEmptyString,
50 pushUrl: Schema.Option(TrimmedNonEmptyString),
51 isPrimary: Schema.Boolean,
52});
53export type VcsRemote = typeof VcsRemote.Type;
54​
55export const VcsListRemotesResult = Schema.Struct({
56 remotes: Schema.Array(VcsRemote),
57 freshness: VcsFreshness,
58});
59export type VcsListRemotesResult = typeof VcsListRemotesResult.Type;
60​
61export interface VcsProcessErrorContext {
62 readonly operation: string;
63 readonly command: string;
64 readonly cwd: string;
65 readonly argumentCount?: number;
67​
68export interface VcsProcessSpawnFailure {
69 readonly cause: unknown;
71​
72export interface VcsProcessTimeoutFailure {
73 readonly timeoutMs: number;
75​
76export const VcsProcessExitFailureKind = Schema.Literals([
77 "authentication",
78 "not-found",
79 "rate-limited",
80 "command-failed",
81]);
82export type VcsProcessExitFailureKind = typeof VcsProcessExitFailureKind.Type;
83​
84export interface VcsProcessExitFailure {
85 readonly exitCode: number;
86 readonly stderr: string;
87 readonly stderrTruncated: boolean;
89​
90export class VcsProcessSpawnError extends Schema.TaggedError<VcsProcessSpawnError>()(
91 "VcsProcessSpawnError",
92 {
93 operation: Schema.String,
94 command: Schema.String,
95 cwd: Schema.String,
96 argumentCount: Schema.optional(NonNegativeInt),
97 cause: Schema.Defect(),
98 },
99) {
100 override get message(): string {
101 return `VCS process failed to spawn in ${this.operation}: ${this.command} (${this.cwd})`;
102 }
103​
104 static fromProcessSpawnError(context: VcsProcessErrorContext, error: VcsProcessSpawnFailure) {
105 return new VcsProcessSpawnError({
106 ...context,
107 cause: error.cause,
108 });
109 }
111​
112export class VcsProcessExitError extends Schema.TaggedError<VcsProcessExitError>()(
113 "VcsProcessExitError",
114 {
115 operation: Schema.String,
116 command: Schema.String,
117 cwd: Schema.String,
118 argumentCount: Schema.optional(NonNegativeInt),
119 exitCode: Schema.Number,
120 detail: Schema.String,
121 failureKind: Schema.optional(VcsProcessExitFailureKind),
122 retryable: Schema.optional(Schema.Boolean),
123 stderrLength: Schema.optional(NonNegativeInt),
124 stderrTruncated: Schema.optional(Schema.Boolean),
125 },
126) {
127 override get message(): string {
128 return `VCS process failed in ${this.operation}: ${this.command} (${this.cwd}) exited with ${this.exitCode} - ${this.detail}`;
129 }
130​
131 static fromProcessExit(
132 context: VcsProcessErrorContext,
133 error: VcsProcessExitFailure,
134 failureKind: VcsProcessExitFailureKind,
135 retryable?: boolean,
136 ) {
137 const detail =
138 failureKind === "authentication"
139 ? "Authentication failed."
140 : failureKind === "rate-limited"
141 ? "API rate limit exceeded."
142 : failureKind === "not-found"
143 ? context.command === "glab"
144 ? "Merge request not found."
145 : context.command === "gh" || context.command === "az"
146 ? "Pull request not found."
147 : "VCS resource not found."
148 : "Process exited with a non-zero status.";
149​
150 return new VcsProcessExitError({
151 ...context,
152 exitCode: error.exitCode,
153 detail,
154 failureKind,
155 ...(retryable === true ? { retryable: true } : {}),
156 stderrLength: error.stderr.length,
157 stderrTruncated: error.stderrTruncated,
158 });
159 }
161​
162export class VcsProcessTimeoutError extends Schema.TaggedError<VcsProcessTimeoutError>()(
163 "VcsProcessTimeoutError",
164 {
165 operation: Schema.String,
166 command: Schema.String,
167 cwd: Schema.String,
168 argumentCount: Schema.optional(NonNegativeInt),
169 timeoutMs: Schema.Number,
170 },
171) {
172 override get message(): string {
173 return `VCS process timed out in ${this.operation}: ${this.command} (${this.cwd}) after ${this.timeoutMs}ms`;
174 }
175​
176 static fromProcessTimeoutError(context: VcsProcessErrorContext, error: VcsProcessTimeoutFailure) {
177 return new VcsProcessTimeoutError({
178 ...context,
179 timeoutMs: error.timeoutMs,
180 });
181 }
183​
184const VcsProcessBoundaryErrorFields = {
185 operation: Schema.String,
186 command: Schema.String,
187 cwd: Schema.String,
188 argumentCount: Schema.optional(NonNegativeInt),
189};
190​
191export class VcsProcessStdinWriteError extends Schema.TaggedError<VcsProcessStdinWriteError>()(
192 "VcsProcessStdinWriteError",
193 {
194 ...VcsProcessBoundaryErrorFields,
195 stdinBytes: NonNegativeInt,
196 cause: Schema.Defect(),
197 },
198) {
199 override get message(): string {
200 return `VCS process failed to write ${this.stdinBytes} bytes to stdin in ${this.operation}: ${this.command} (${this.cwd})`;
201 }
203​
204export class VcsProcessOutputReadError extends Schema.TaggedError<VcsProcessOutputReadError>()(
205 "VcsProcessOutputReadError",
206 {
207 ...VcsProcessBoundaryErrorFields,
208 stream: Schema.Literals(["stdout", "stderr", "exitCode"]),
209 cause: Schema.Defect(),
210 },
211) {
212 override get message(): string {
213 return `VCS process failed to read ${this.stream} in ${this.operation}: ${this.command} (${this.cwd})`;
214 }
216​
217export class VcsProcessOutputLimitError extends Schema.TaggedError<VcsProcessOutputLimitError>()(
218 "VcsProcessOutputLimitError",
219 {
220 ...VcsProcessBoundaryErrorFields,
221 stream: Schema.Literals(["stdout", "stderr"]),
222 maxBytes: NonNegativeInt,
223 observedBytes: NonNegativeInt,
224 },
225) {
226 override get message(): string {
227 return `VCS process ${this.stream} produced ${this.observedBytes} bytes in ${this.operation}: ${this.command} (${this.cwd}), exceeding the ${this.maxBytes} byte limit`;
228 }
230​
231export class VcsProcessMissingExitCodeError extends Schema.TaggedError<VcsProcessMissingExitCodeError>()(
232 "VcsProcessMissingExitCodeError",
233 VcsProcessBoundaryErrorFields,
234) {
235 override get message(): string {
236 return `VCS process completed without an exit code in ${this.operation}: ${this.command} (${this.cwd})`;
237 }
239​
240export const VcsOutputDecodeError = Schema.Union([
241 VcsProcessStdinWriteError,
242 VcsProcessOutputReadError,
243 VcsProcessOutputLimitError,
244 VcsProcessMissingExitCodeError,
245]);
246export type VcsOutputDecodeError = typeof VcsOutputDecodeError.Type;
247​
248export class VcsRepositoryDetectionError extends Schema.TaggedError<VcsRepositoryDetectionError>()(
249 "VcsRepositoryDetectionError",
250 {
251 operation: Schema.String,
252 cwd: Schema.String,
253 detail: Schema.String,
254 cause: Schema.optional(Schema.Defect()),
255 },
256) {
257 override get message(): string {
258 return `VCS repository detection failed in ${this.operation}: ${this.cwd} - ${this.detail}`;
259 }
261​
262export class VcsUnsupportedOperationError extends Schema.TaggedError<VcsUnsupportedOperationError>()(
263 "VcsUnsupportedOperationError",
264 {
265 operation: Schema.String,
266 kind: VcsDriverKind,
267 detail: Schema.String,
268 },
269) {
270 override get message(): string {
271 return `VCS operation is unsupported for ${this.kind} in ${this.operation}: ${this.detail}`;
272 }
274​
275export const VcsError = Schema.Union([
276 VcsProcessSpawnError,
277 VcsProcessExitError,
278 VcsProcessTimeoutError,
279 VcsProcessStdinWriteError,
280 VcsProcessOutputReadError,
281 VcsProcessOutputLimitError,
282 VcsProcessMissingExitCodeError,
283 VcsRepositoryDetectionError,
284 VcsUnsupportedOperationError,
285]);
286export type VcsError = typeof VcsError.Type;