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

vcs.ts

283 lines8.5 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.TaggedErrorClass<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.TaggedErrorClass<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 stderrLength: Schema.optional(NonNegativeInt),
123 stderrTruncated: Schema.optional(Schema.Boolean),
124 },
125) {
126 override get message(): string {
127 return `VCS process failed in ${this.operation}: ${this.command} (${this.cwd}) exited with ${this.exitCode} - ${this.detail}`;
128 }
129​
130 static fromProcessExit(
131 context: VcsProcessErrorContext,
132 error: VcsProcessExitFailure,
133 failureKind: VcsProcessExitFailureKind,
134 ) {
135 const detail =
136 failureKind === "authentication"
137 ? "Authentication failed."
138 : failureKind === "rate-limited"
139 ? "API rate limit exceeded."
140 : failureKind === "not-found"
141 ? context.command === "glab"
142 ? "Merge request not found."
143 : context.command === "gh" || context.command === "az"
144 ? "Pull request not found."
145 : "VCS resource not found."
146 : "Process exited with a non-zero status.";
147​
148 return new VcsProcessExitError({
149 ...context,
150 exitCode: error.exitCode,
151 detail,
152 failureKind,
153 stderrLength: error.stderr.length,
154 stderrTruncated: error.stderrTruncated,
155 });
156 }
158​
159export class VcsProcessTimeoutError extends Schema.TaggedErrorClass<VcsProcessTimeoutError>()(
160 "VcsProcessTimeoutError",
161 {
162 operation: Schema.String,
163 command: Schema.String,
164 cwd: Schema.String,
165 argumentCount: Schema.optional(NonNegativeInt),
166 timeoutMs: Schema.Number,
167 },
168) {
169 override get message(): string {
170 return `VCS process timed out in ${this.operation}: ${this.command} (${this.cwd}) after ${this.timeoutMs}ms`;
171 }
172​
173 static fromProcessTimeoutError(context: VcsProcessErrorContext, error: VcsProcessTimeoutFailure) {
174 return new VcsProcessTimeoutError({
175 ...context,
176 timeoutMs: error.timeoutMs,
177 });
178 }
180​
181const VcsProcessBoundaryErrorFields = {
182 operation: Schema.String,
183 command: Schema.String,
184 cwd: Schema.String,
185 argumentCount: Schema.optional(NonNegativeInt),
186};
187​
188export class VcsProcessStdinWriteError extends Schema.TaggedErrorClass<VcsProcessStdinWriteError>()(
189 "VcsProcessStdinWriteError",
190 {
191 ...VcsProcessBoundaryErrorFields,
192 stdinBytes: NonNegativeInt,
193 cause: Schema.Defect(),
194 },
195) {
196 override get message(): string {
197 return `VCS process failed to write ${this.stdinBytes} bytes to stdin in ${this.operation}: ${this.command} (${this.cwd})`;
198 }
200​
201export class VcsProcessOutputReadError extends Schema.TaggedErrorClass<VcsProcessOutputReadError>()(
202 "VcsProcessOutputReadError",
203 {
204 ...VcsProcessBoundaryErrorFields,
205 stream: Schema.Literals(["stdout", "stderr", "exitCode"]),
206 cause: Schema.Defect(),
207 },
208) {
209 override get message(): string {
210 return `VCS process failed to read ${this.stream} in ${this.operation}: ${this.command} (${this.cwd})`;
211 }
213​
214export class VcsProcessOutputLimitError extends Schema.TaggedErrorClass<VcsProcessOutputLimitError>()(
215 "VcsProcessOutputLimitError",
216 {
217 ...VcsProcessBoundaryErrorFields,
218 stream: Schema.Literals(["stdout", "stderr"]),
219 maxBytes: NonNegativeInt,
220 observedBytes: NonNegativeInt,
221 },
222) {
223 override get message(): string {
224 return `VCS process ${this.stream} produced ${this.observedBytes} bytes in ${this.operation}: ${this.command} (${this.cwd}), exceeding the ${this.maxBytes} byte limit`;
225 }
227​
228export class VcsProcessMissingExitCodeError extends Schema.TaggedErrorClass<VcsProcessMissingExitCodeError>()(
229 "VcsProcessMissingExitCodeError",
230 VcsProcessBoundaryErrorFields,
231) {
232 override get message(): string {
233 return `VCS process completed without an exit code in ${this.operation}: ${this.command} (${this.cwd})`;
234 }
236​
237export const VcsOutputDecodeError = Schema.Union([
238 VcsProcessStdinWriteError,
239 VcsProcessOutputReadError,
240 VcsProcessOutputLimitError,
241 VcsProcessMissingExitCodeError,
242]);
243export type VcsOutputDecodeError = typeof VcsOutputDecodeError.Type;
244​
245export class VcsRepositoryDetectionError extends Schema.TaggedErrorClass<VcsRepositoryDetectionError>()(
246 "VcsRepositoryDetectionError",
247 {
248 operation: Schema.String,
249 cwd: Schema.String,
250 detail: Schema.String,
251 cause: Schema.optional(Schema.Defect()),
252 },
253) {
254 override get message(): string {
255 return `VCS repository detection failed in ${this.operation}: ${this.cwd} - ${this.detail}`;
256 }
258​
259export class VcsUnsupportedOperationError extends Schema.TaggedErrorClass<VcsUnsupportedOperationError>()(
260 "VcsUnsupportedOperationError",
261 {
262 operation: Schema.String,
263 kind: VcsDriverKind,
264 detail: Schema.String,
265 },
266) {
267 override get message(): string {
268 return `VCS operation is unsupported for ${this.kind} in ${this.operation}: ${this.detail}`;
269 }
271​
272export const VcsError = Schema.Union([
273 VcsProcessSpawnError,
274 VcsProcessExitError,
275 VcsProcessTimeoutError,
276 VcsProcessStdinWriteError,
277 VcsProcessOutputReadError,
278 VcsProcessOutputLimitError,
279 VcsProcessMissingExitCodeError,
280 VcsRepositoryDetectionError,
281 VcsUnsupportedOperationError,
282]);
283export type VcsError = typeof VcsError.Type;