Open History to see the last commit
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;
66}
67
68export interface VcsProcessSpawnFailure {
69 readonly cause: unknown;
70}
71
72export interface VcsProcessTimeoutFailure {
73 readonly timeoutMs: number;
74}
75
76export const VcsProcessExitFailureKind = Schema.Literals([
77 "authentication",
78 "not-found",
79 "command-failed",
80]);
81export type VcsProcessExitFailureKind = typeof VcsProcessExitFailureKind.Type;
82
83export interface VcsProcessExitFailure {
84 readonly exitCode: number;
85 readonly stderr: string;
86 readonly stderrTruncated: boolean;
87}
88
89export class VcsProcessSpawnError extends Schema.TaggedErrorClass<VcsProcessSpawnError>()(
90 "VcsProcessSpawnError",
91 {
92 operation: Schema.String,
93 command: Schema.String,
94 cwd: Schema.String,
95 argumentCount: Schema.optional(NonNegativeInt),
96 cause: Schema.Defect(),
97 },
98) {
99 override get message(): string {
100 return `VCS process failed to spawn in ${this.operation}: ${this.command} (${this.cwd})`;
101 }
102
103 static fromProcessSpawnError(context: VcsProcessErrorContext, error: VcsProcessSpawnFailure) {
104 return new VcsProcessSpawnError({
105 ...context,
106 cause: error.cause,
107 });
108 }
109}
110
111export class VcsProcessExitError extends Schema.TaggedErrorClass<VcsProcessExitError>()(
112 "VcsProcessExitError",
113 {
114 operation: Schema.String,
115 command: Schema.String,
116 cwd: Schema.String,
117 argumentCount: Schema.optional(NonNegativeInt),
118 exitCode: Schema.Number,
119 detail: Schema.String,
120 failureKind: Schema.optional(VcsProcessExitFailureKind),
121 stderrLength: Schema.optional(NonNegativeInt),
122 stderrTruncated: Schema.optional(Schema.Boolean),
123 },
124) {
125 override get message(): string {
126 return `VCS process failed in ${this.operation}: ${this.command} (${this.cwd}) exited with ${this.exitCode} - ${this.detail}`;
127 }
128
129 static fromProcessExit(
130 context: VcsProcessErrorContext,
131 error: VcsProcessExitFailure,
132 failureKind: VcsProcessExitFailureKind,
133 ) {
134 const detail =
135 failureKind === "authentication"
136 ? "Authentication failed."
137 : failureKind === "not-found"
138 ? context.command === "glab"
139 ? "Merge request not found."
140 : context.command === "gh" || context.command === "az"
141 ? "Pull request not found."
142 : "VCS resource not found."
143 : "Process exited with a non-zero status.";
144
145 return new VcsProcessExitError({
146 ...context,
147 exitCode: error.exitCode,
148 detail,
149 failureKind,
150 stderrLength: error.stderr.length,
151 stderrTruncated: error.stderrTruncated,
152 });
153 }
154}
155
156export class VcsProcessTimeoutError extends Schema.TaggedErrorClass<VcsProcessTimeoutError>()(
157 "VcsProcessTimeoutError",
158 {
159 operation: Schema.String,
160 command: Schema.String,
161 cwd: Schema.String,
162 argumentCount: Schema.optional(NonNegativeInt),
163 timeoutMs: Schema.Number,
164 },
165) {
166 override get message(): string {
167 return `VCS process timed out in ${this.operation}: ${this.command} (${this.cwd}) after ${this.timeoutMs}ms`;
168 }
169
170 static fromProcessTimeoutError(context: VcsProcessErrorContext, error: VcsProcessTimeoutFailure) {
171 return new VcsProcessTimeoutError({
172 ...context,
173 timeoutMs: error.timeoutMs,
174 });
175 }
176}
177
178const VcsProcessBoundaryErrorFields = {
179 operation: Schema.String,
180 command: Schema.String,
181 cwd: Schema.String,
182 argumentCount: Schema.optional(NonNegativeInt),
183};
184
185export class VcsProcessStdinWriteError extends Schema.TaggedErrorClass<VcsProcessStdinWriteError>()(
186 "VcsProcessStdinWriteError",
187 {
188 ...VcsProcessBoundaryErrorFields,
189 stdinBytes: NonNegativeInt,
190 cause: Schema.Defect(),
191 },
192) {
193 override get message(): string {
194 return `VCS process failed to write ${this.stdinBytes} bytes to stdin in ${this.operation}: ${this.command} (${this.cwd})`;
195 }
196}
197
198export class VcsProcessOutputReadError extends Schema.TaggedErrorClass<VcsProcessOutputReadError>()(
199 "VcsProcessOutputReadError",
200 {
201 ...VcsProcessBoundaryErrorFields,
202 stream: Schema.Literals(["stdout", "stderr", "exitCode"]),
203 cause: Schema.Defect(),
204 },
205) {
206 override get message(): string {
207 return `VCS process failed to read ${this.stream} in ${this.operation}: ${this.command} (${this.cwd})`;
208 }
209}
210
211export class VcsProcessOutputLimitError extends Schema.TaggedErrorClass<VcsProcessOutputLimitError>()(
212 "VcsProcessOutputLimitError",
213 {
214 ...VcsProcessBoundaryErrorFields,
215 stream: Schema.Literals(["stdout", "stderr"]),
216 maxBytes: NonNegativeInt,
217 observedBytes: NonNegativeInt,
218 },
219) {
220 override get message(): string {
221 return `VCS process ${this.stream} produced ${this.observedBytes} bytes in ${this.operation}: ${this.command} (${this.cwd}), exceeding the ${this.maxBytes} byte limit`;
222 }
223}
224
225export class VcsProcessMissingExitCodeError extends Schema.TaggedErrorClass<VcsProcessMissingExitCodeError>()(
226 "VcsProcessMissingExitCodeError",
227 VcsProcessBoundaryErrorFields,
228) {
229 override get message(): string {
230 return `VCS process completed without an exit code in ${this.operation}: ${this.command} (${this.cwd})`;
231 }
232}
233
234export const VcsOutputDecodeError = Schema.Union([
235 VcsProcessStdinWriteError,
236 VcsProcessOutputReadError,
237 VcsProcessOutputLimitError,
238 VcsProcessMissingExitCodeError,
239]);
240export type VcsOutputDecodeError = typeof VcsOutputDecodeError.Type;
241
242export class VcsRepositoryDetectionError extends Schema.TaggedErrorClass<VcsRepositoryDetectionError>()(
243 "VcsRepositoryDetectionError",
244 {
245 operation: Schema.String,
246 cwd: Schema.String,
247 detail: Schema.String,
248 cause: Schema.optional(Schema.Defect()),
249 },
250) {
251 override get message(): string {
252 return `VCS repository detection failed in ${this.operation}: ${this.cwd} - ${this.detail}`;
253 }
254}
255
256export class VcsUnsupportedOperationError extends Schema.TaggedErrorClass<VcsUnsupportedOperationError>()(
257 "VcsUnsupportedOperationError",
258 {
259 operation: Schema.String,
260 kind: VcsDriverKind,
261 detail: Schema.String,
262 },
263) {
264 override get message(): string {
265 return `VCS operation is unsupported for ${this.kind} in ${this.operation}: ${this.detail}`;
266 }
267}
268
269export const VcsError = Schema.Union([
270 VcsProcessSpawnError,
271 VcsProcessExitError,
272 VcsProcessTimeoutError,
273 VcsProcessStdinWriteError,
274 VcsProcessOutputReadError,
275 VcsProcessOutputLimitError,
276 VcsProcessMissingExitCodeError,
277 VcsRepositoryDetectionError,
278 VcsUnsupportedOperationError,
279]);
280export type VcsError = typeof VcsError.Type;