The best way to control Claude Code, Codex and more from any computer or phone
[codex] enrich Git workflow errors (#3241)
Co-authored-by: codex <codex@users.noreply.github.com>
Julius Marminge committed
Jun 20, 2026 at 13:16 UTC
c637dfc454342c156dbe61ee8513f5e0ac0c4691
7 files changed
+359
-131
apps/server/src/git/GitManager.test.ts
+8
-6
@@ -1589,16 +1589,18 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
1589
yield* initRepo(repoDir);
1590
1591
const { manager } = yield* makeManager();
1592
- const errorMessage = yield* runStackedAction(manager, {
1592
+ const error = yield* runStackedAction(manager, {
1593
cwd: repoDir,
1594
action: "commit",
1595
featureBranch: true,
1596
- }).pipe(
1597
- Effect.flip,
1598
- Effect.map((error) => error.message),
1599
- );
1596
+ }).pipe(Effect.flip);
1597
1601
- expect(errorMessage).toContain("no changes to commit");
1598
+ expect(error).toMatchObject({
1599
+ _tag: "GitManagerError",
1600
+ operation: "runFeatureBranchStep",
1601
+ cwd: repoDir,
1602
+ });
1603
+ expect(error.message).toContain("no changes to commit");
1604
}),
1605
);
1606
apps/server/src/git/GitManager.ts
+86
-57
@@ -320,14 +320,6 @@ function toPullRequestInfo(summary: ChangeRequest): PullRequestInfo {
320
};
321
}
322
323
-function gitManagerError(operation: string, detail: string, cause?: unknown): GitManagerError {
324
- return new GitManagerError({
325
- operation,
326
- detail,
327
- ...(cause !== undefined ? { cause } : {}),
328
- });
329
-}
330
-
323
function limitContext(value: string, maxChars: number): string {
324
if (value.length <= maxChars) return value;
325
return `${value.slice(0, maxChars)}\n\n[truncated]`;
@@ -535,17 +527,27 @@ export const make = Effect.gen(function* () {
527
528
const sourceControlProvider = (cwd: string) => sourceControlProviders.resolve({ cwd });
529
const serverSettingsService = yield* ServerSettings.ServerSettingsService;
538
- const randomUUIDv4 = crypto.randomUUIDv4.pipe(
539
- Effect.mapError((cause) =>
540
- gitManagerError("randomUUIDv4", "Failed to generate Git operation identifier.", cause),
541
- ),
542
- );
530
+ const randomUUIDv4 = (cwd: string) =>
531
+ crypto.randomUUIDv4.pipe(
532
+ Effect.mapError(
533
+ (cause) =>
534
+ new GitManagerError({
535
+ operation: "randomUUIDv4",
536
+ cwd,
537
+ detail: "Failed to generate Git operation identifier.",
538
+ cause,
539
+ }),
540
+ ),
541
+ );
542
543
const createProgressEmitter = (
544
input: { cwd: string; action: GitStackedAction },
545
options?: GitRunStackedActionOptions,
546
) =>
548
- (options?.actionId === undefined ? randomUUIDv4 : Effect.succeed(options.actionId)).pipe(
547
+ (options?.actionId === undefined
548
+ ? randomUUIDv4(input.cwd)
549
+ : Effect.succeed(options.actionId)
550
+ ).pipe(
551
Effect.map((actionId) => {
552
const reporter = options?.progressReporter;
553
const emit = (event: GitActionProgressPayload) =>
@@ -1284,16 +1286,18 @@ export const make = Effect.gen(function* () {
1286
const details = yield* gitCore.statusDetails(cwd);
1287
const branch = details.branch ?? fallbackBranch;
1288
if (!branch) {
1287
- return yield* gitManagerError(
1288
- "runPrStep",
1289
- "Cannot create a pull request from detached HEAD.",
1290
- );
1289
+ return yield* new GitManagerError({
1290
+ operation: "runPrStep",
1291
+ cwd,
1292
+ detail: "Cannot create a pull request from detached HEAD.",
1293
+ });
1294
}
1295
if (!details.hasUpstream) {
1293
- return yield* gitManagerError(
1294
- "runPrStep",
1295
- "Current branch has not been pushed. Push before creating a PR.",
1296
- );
1296
+ return yield* new GitManagerError({
1297
+ operation: "runPrStep",
1298
+ cwd,
1299
+ detail: "Current branch has not been pushed. Push before creating a PR.",
1300
+ });
1301
}
1302
1303
const headContext = yield* resolveBranchHeadContext(cwd, {
@@ -1332,14 +1336,21 @@ export const make = Effect.gen(function* () {
1336
modelSelection,
1337
});
1338
1335
- const bodyFile = path.join(tempDir, `t3code-pr-body-${process.pid}-${yield* randomUUIDv4}.md`);
1336
- yield* fileSystem
1337
- .writeFileString(bodyFile, generated.body)
1338
- .pipe(
1339
- Effect.mapError((cause) =>
1340
- gitManagerError("runPrStep", "Failed to write pull request body temp file.", cause),
1341
- ),
1342
- );
1339
+ const bodyFile = path.join(
1340
+ tempDir,
1341
+ `t3code-pr-body-${process.pid}-${yield* randomUUIDv4(cwd)}.md`,
1342
+ );
1343
+ yield* fileSystem.writeFileString(bodyFile, generated.body).pipe(
1344
+ Effect.mapError(
1345
+ (cause) =>
1346
+ new GitManagerError({
1347
+ operation: "runPrStep",
1348
+ cwd,
1349
+ detail: "Failed to write pull request body temp file.",
1350
+ cause,
1351
+ }),
1352
+ ),
1353
+ );
1354
yield* emit({
1355
kind: "phase_started",
1356
phase: "pr",
@@ -1541,10 +1552,12 @@ export const make = Effect.gen(function* () {
1552
};
1553
}
1554
if (existingBranchBeforeFetchPath === rootWorktreePath) {
1544
- return yield* gitManagerError(
1545
- "preparePullRequestThread",
1546
- "This PR branch is already checked out in the main repo. Use Local, or switch the main repo off that branch before creating a worktree thread.",
1547
- );
1555
+ return yield* new GitManagerError({
1556
+ operation: "preparePullRequestThread",
1557
+ cwd: input.cwd,
1558
+ detail:
1559
+ "This PR branch is already checked out in the main repo. Use Local, or switch the main repo off that branch before creating a worktree thread.",
1560
+ });
1561
}
1562
1563
yield* materializePullRequestHeadBranch(
@@ -1569,10 +1582,12 @@ export const make = Effect.gen(function* () {
1582
};
1583
}
1584
if (existingBranchAfterFetchPath === rootWorktreePath) {
1572
- return yield* gitManagerError(
1573
- "preparePullRequestThread",
1574
- "This PR branch is already checked out in the main repo. Use Local, or switch the main repo off that branch before creating a worktree thread.",
1575
- );
1585
+ return yield* new GitManagerError({
1586
+ operation: "preparePullRequestThread",
1587
+ cwd: input.cwd,
1588
+ detail:
1589
+ "This PR branch is already checked out in the main repo. Use Local, or switch the main repo off that branch before creating a worktree thread.",
1590
+ });
1591
}
1592
1593
const worktree = yield* gitCore.createWorktree({
@@ -1607,10 +1622,11 @@ export const make = Effect.gen(function* () {
1622
modelSelection,
1623
});
1624
if (!suggestion) {
1610
- return yield* gitManagerError(
1611
- "runFeatureBranchStep",
1612
- "Cannot create a feature branch because there are no changes to commit.",
1613
- );
1625
+ return yield* new GitManagerError({
1626
+ operation: "runFeatureBranchStep",
1627
+ cwd,
1628
+ detail: "Cannot create a feature branch because there are no changes to commit.",
1629
+ });
1630
}
1631
1632
const preferredBranch = suggestion.branch ?? sanitizeFeatureBranchName(suggestion.subject);
@@ -1647,16 +1663,18 @@ export const make = Effect.gen(function* () {
1663
const wantsPr = input.action === "create_pr" || input.action === "commit_push_pr";
1664
1665
if (input.featureBranch && !wantsCommit) {
1650
- return yield* gitManagerError(
1651
- "runStackedAction",
1652
- "Feature-branch checkout is only supported for commit actions.",
1653
- );
1666
+ return yield* new GitManagerError({
1667
+ operation: "runStackedAction",
1668
+ cwd: input.cwd,
1669
+ detail: "Feature-branch checkout is only supported for commit actions.",
1670
+ });
1671
}
1672
if (input.action === "create_pr" && initialStatus.hasWorkingTreeChanges) {
1656
- return yield* gitManagerError(
1657
- "runStackedAction",
1658
- "Commit local changes before creating a PR.",
1659
- );
1673
+ return yield* new GitManagerError({
1674
+ operation: "runStackedAction",
1675
+ cwd: input.cwd,
1676
+ detail: "Commit local changes before creating a PR.",
1677
+ });
1678
}
1679
1680
const phases: GitActionProgressPhase[] = [
@@ -1672,13 +1690,18 @@ export const make = Effect.gen(function* () {
1690
});
1691
1692
if (!input.featureBranch && wantsPush && !initialStatus.branch) {
1675
- return yield* gitManagerError("runStackedAction", "Cannot push from detached HEAD.");
1693
+ return yield* new GitManagerError({
1694
+ operation: "runStackedAction",
1695
+ cwd: input.cwd,
1696
+ detail: "Cannot push from detached HEAD.",
1697
+ });
1698
}
1699
if (!input.featureBranch && wantsPr && !initialStatus.branch) {
1678
- return yield* gitManagerError(
1679
- "runStackedAction",
1680
- "Cannot create a pull request from detached HEAD.",
1681
- );
1700
+ return yield* new GitManagerError({
1701
+ operation: "runStackedAction",
1702
+ cwd: input.cwd,
1703
+ detail: "Cannot create a pull request from detached HEAD.",
1704
+ });
1705
}
1706
1707
let branchStep: { status: "created" | "skipped_not_requested"; name?: string };
@@ -1687,8 +1710,14 @@ export const make = Effect.gen(function* () {
1710
1711
const modelSelection = yield* serverSettingsService.getSettings.pipe(
1712
Effect.map((settings) => settings.textGenerationModelSelection),
1690
- Effect.mapError((cause) =>
1691
- gitManagerError("runStackedAction", "Failed to get server settings.", cause),
1713
+ Effect.mapError(
1714
+ (cause) =>
1715
+ new GitManagerError({
1716
+ operation: "runStackedAction",
1717
+ cwd: input.cwd,
1718
+ detail: "Failed to get server settings.",
1719
+ cause,
1720
+ }),
1721
),
1722
);
1723
apps/server/src/git/GitWorkflowService.test.ts
+58
-1
@@ -1,7 +1,9 @@
1
-import { assert, describe, it, vi } from "@effect/vitest";
1
+import { assert, describe, expect, it, vi } from "@effect/vitest";
2
import * as Effect from "effect/Effect";
3
import * as Layer from "effect/Layer";
4
5
+import { VcsRepositoryDetectionError } from "@t3tools/contracts";
6
+
7
import * as GitManager from "./GitManager.ts";
8
import * as GitWorkflowService from "./GitWorkflowService.ts";
9
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
@@ -132,4 +134,59 @@ describe("GitWorkflowService", () => {
134
),
135
),
136
);
137
+
138
+ it.effect("structures workflow detection failures without exposing upstream details", () => {
139
+ const cause = new VcsRepositoryDetectionError({
140
+ operation: "VcsDriverRegistry.detect",
141
+ cwd: "/repo",
142
+ detail: "upstream detail must stay in the cause chain",
143
+ });
144
+
145
+ return Effect.gen(function* () {
146
+ const workflow = yield* GitWorkflowService.GitWorkflowService;
147
+ const error = yield* workflow.status({ cwd: "/repo" }).pipe(Effect.flip);
148
+
149
+ expect(error).toMatchObject({
150
+ _tag: "GitManagerError",
151
+ operation: "GitWorkflowService.status",
152
+ cwd: "/repo",
153
+ detail: "Failed to detect a VCS repository for this Git workflow.",
154
+ });
155
+ expect(error.message).not.toContain(cause.detail);
156
+ }).pipe(
157
+ Effect.provide(
158
+ makeLayer({
159
+ detect: () => Effect.fail(cause),
160
+ }),
161
+ ),
162
+ );
163
+ });
164
+
165
+ it.effect("structures command detection failures without exposing upstream details", () => {
166
+ const cause = new VcsRepositoryDetectionError({
167
+ operation: "VcsDriverRegistry.detect",
168
+ cwd: "/repo",
169
+ detail: "upstream command detail must stay in the cause chain",
170
+ });
171
+
172
+ return Effect.gen(function* () {
173
+ const workflow = yield* GitWorkflowService.GitWorkflowService;
174
+ const error = yield* workflow.listRefs({ cwd: "/repo" }).pipe(Effect.flip);
175
+
176
+ expect(error).toMatchObject({
177
+ _tag: "GitCommandError",
178
+ operation: "GitWorkflowService.listRefs",
179
+ command: "vcs-route",
180
+ cwd: "/repo",
181
+ detail: "Failed to detect a VCS repository for this Git command.",
182
+ });
183
+ expect(error.message).not.toContain(cause.detail);
184
+ }).pipe(
185
+ Effect.provide(
186
+ makeLayer({
187
+ detect: () => Effect.fail(cause),
188
+ }),
189
+ ),
190
+ );
191
+ });
192
});
apps/server/src/git/GitWorkflowService.ts
+52
-62
@@ -94,20 +94,6 @@ export class GitWorkflowService extends Context.Service<
94
}
95
>()("t3/git/GitWorkflowService") {}
96
97
-const unsupportedGitWorkflow = (operation: string, cwd: string, detail: string) =>
98
- new GitManagerError({
99
- operation,
100
- detail: `${detail} (${cwd})`,
101
- });
102
-
103
-const unsupportedGitCommand = (operation: string, cwd: string, detail: string) =>
104
- new GitCommandError({
105
- operation,
106
- command: "vcs-route",
107
- cwd,
108
- detail,
109
- });
110
-
97
function nonRepositoryLocalStatus(): VcsStatusLocalResult {
98
return {
99
isRepo: false,
@@ -153,23 +139,23 @@ export const make = Effect.gen(function* () {
139
operation: string,
140
cwd: string,
141
) {
156
- const handle = yield* registry
157
- .resolve({ cwd })
158
- .pipe(
159
- Effect.mapError((error) =>
160
- unsupportedGitWorkflow(
142
+ const handle = yield* registry.resolve({ cwd }).pipe(
143
+ Effect.mapError(
144
+ (cause) =>
145
+ new GitManagerError({
146
operation,
147
cwd,
163
- error instanceof Error ? error.message : String(error),
164
- ),
165
- ),
166
- );
148
+ detail: "Failed to resolve the VCS driver for this Git workflow.",
149
+ cause,
150
+ }),
151
+ ),
152
+ );
153
if (handle.kind !== "git") {
168
- return yield* unsupportedGitWorkflow(
154
+ return yield* new GitManagerError({
155
operation,
156
cwd,
171
- `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}.`,
172
- );
157
+ detail: `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}. (${cwd})`,
158
+ });
159
}
160
});
161
@@ -177,48 +163,50 @@ export const make = Effect.gen(function* () {
163
operation: string,
164
cwd: string,
165
) {
180
- const handle = yield* registry
181
- .resolve({ cwd })
182
- .pipe(
183
- Effect.mapError((error) =>
184
- unsupportedGitCommand(
166
+ const handle = yield* registry.resolve({ cwd }).pipe(
167
+ Effect.mapError(
168
+ (cause) =>
169
+ new GitCommandError({
170
operation,
171
+ command: "vcs-route",
172
cwd,
187
- error instanceof Error ? error.message : String(error),
188
- ),
189
- ),
190
- );
173
+ detail: "Failed to resolve the VCS driver for this Git command.",
174
+ cause,
175
+ }),
176
+ ),
177
+ );
178
if (handle.kind !== "git") {
192
- return yield* unsupportedGitCommand(
179
+ return yield* new GitCommandError({
180
operation,
181
+ command: "vcs-route",
182
cwd,
195
- `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
196
- );
183
+ detail: `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
184
+ });
185
}
186
});
187
188
const detectGitRepositoryForStatus = Effect.fn("GitWorkflowService.detectGitRepositoryForStatus")(
189
function* (operation: string, cwd: string) {
202
- const handle = yield* registry
203
- .detect({ cwd })
204
- .pipe(
205
- Effect.mapError((error) =>
206
- unsupportedGitWorkflow(
190
+ const handle = yield* registry.detect({ cwd }).pipe(
191
+ Effect.mapError(
192
+ (cause) =>
193
+ new GitManagerError({
194
operation,
195
cwd,
209
- error instanceof Error ? error.message : String(error),
210
- ),
211
- ),
212
- );
196
+ detail: "Failed to detect a VCS repository for this Git workflow.",
197
+ cause,
198
+ }),
199
+ ),
200
+ );
201
if (!handle) {
202
return false;
203
}
204
if (handle.kind !== "git") {
217
- return yield* unsupportedGitWorkflow(
205
+ return yield* new GitManagerError({
206
operation,
207
cwd,
220
- `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}.`,
221
- );
208
+ detail: `The ${operation} workflow currently supports Git repositories only; detected ${handle.kind}. (${cwd})`,
209
+ });
210
}
211
return true;
212
},
@@ -227,26 +215,28 @@ export const make = Effect.gen(function* () {
215
const detectGitRepositoryForCommand = Effect.fn(
216
"GitWorkflowService.detectGitRepositoryForCommand",
217
)(function* (operation: string, cwd: string) {
230
- const handle = yield* registry
231
- .detect({ cwd })
232
- .pipe(
233
- Effect.mapError((error) =>
234
- unsupportedGitCommand(
218
+ const handle = yield* registry.detect({ cwd }).pipe(
219
+ Effect.mapError(
220
+ (cause) =>
221
+ new GitCommandError({
222
operation,
223
+ command: "vcs-route",
224
cwd,
237
- error instanceof Error ? error.message : String(error),
238
- ),
239
- ),
240
- );
225
+ detail: "Failed to detect a VCS repository for this Git command.",
226
+ cause,
227
+ }),
228
+ ),
229
+ );
230
if (!handle) {
231
return false;
232
}
233
if (handle.kind !== "git") {
245
- return yield* unsupportedGitCommand(
234
+ return yield* new GitCommandError({
235
operation,
236
+ command: "vcs-route",
237
cwd,
248
- `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
249
- );
238
+ detail: `The ${operation} command currently supports Git repositories only; detected ${handle.kind}.`,
239
+ });
240
}
241
return true;
242
});
apps/server/src/vcs/VcsStatusBroadcaster.test.ts
+61
-3
@@ -1,11 +1,13 @@
1
import { assert, it, describe } from "@effect/vitest";
2
import * as NodeServices from "@effect/platform-node/NodeServices";
3
+import * as Cause from "effect/Cause";
4
import * as Deferred from "effect/Deferred";
5
import * as Duration from "effect/Duration";
6
import * as Effect from "effect/Effect";
7
import * as Exit from "effect/Exit";
8
import * as FileSystem from "effect/FileSystem";
9
import * as Layer from "effect/Layer";
10
+import * as Logger from "effect/Logger";
11
import * as Option from "effect/Option";
12
import * as Path from "effect/Path";
13
import * as Scope from "effect/Scope";
@@ -190,6 +192,7 @@ describe("VcsStatusBroadcaster", () => {
192
? Effect.fail(
193
new GitManagerError({
194
operation: "VcsStatusBroadcaster.test",
195
+ cwd: "/repo",
196
detail: "remote status failed",
197
}),
198
)
@@ -431,6 +434,12 @@ describe("VcsStatusBroadcaster", () => {
434
remoteInvalidationCalls: 0,
435
remoteStatusRefreshUpstreamValues: [] as Array<boolean | undefined>,
436
};
437
+ const privateCwd = "/private/user/workspace/repo";
438
+ const nestedCause = new Error("private nested VCS failure");
439
+ const messages: Array<ReadonlyArray<unknown>> = [];
440
+ const logger = Logger.make<unknown, void>(({ message }) => {
441
+ messages.push(message as ReadonlyArray<unknown>);
442
+ });
443
let firstRemoteAttemptDeferred: Deferred.Deferred<void> | null = null;
444
const testLayer = VcsStatusBroadcaster.layer.pipe(
445
Layer.provideMerge(NodeServices.layer),
@@ -449,7 +458,9 @@ describe("VcsStatusBroadcaster", () => {
458
return Effect.fail(
459
new GitManagerError({
460
operation: "VcsStatusBroadcaster.test",
452
- detail: "initial remote status failed",
461
+ cwd: privateCwd,
462
+ detail: "private initial remote status failure",
463
+ cause: nestedCause,
464
}),
465
).pipe(
466
Effect.ensuring(
@@ -480,7 +491,7 @@ describe("VcsStatusBroadcaster", () => {
491
const remoteUpdatedDeferred = yield* Deferred.make<VcsStatusStreamEvent>();
492
yield* Stream.runForEach(
493
broadcaster.streamStatus(
483
- { cwd: "/repo" },
494
+ { cwd: privateCwd },
495
{ automaticRemoteRefreshInterval: Effect.succeed(Duration.zero) },
496
),
497
(event) =>
@@ -492,6 +503,24 @@ describe("VcsStatusBroadcaster", () => {
503
yield* Deferred.await(firstRemoteAttemptDeferred);
504
yield* Effect.yieldNow;
505
assert.equal(state.remoteStatusCalls, 1);
506
+ assert.deepStrictEqual(
507
+ messages.find((message) => message[0] === "VCS remote status refresh failed"),
508
+ [
509
+ "VCS remote status refresh failed",
510
+ {
511
+ cwdLength: privateCwd.length,
512
+ reasonCount: 1,
513
+ failureCount: 1,
514
+ failureTags: ["GitManagerError"],
515
+ failureOperations: ["VcsStatusBroadcaster.test"],
516
+ defectCount: 0,
517
+ defectTags: [],
518
+ interruptionCount: 0,
519
+ consecutiveFailures: 1,
520
+ nextDelayMs: 30_000,
521
+ },
522
+ ],
523
+ );
524
525
yield* TestClock.adjust(Duration.seconds(30));
526
const remoteUpdated = yield* Deferred.await(remoteUpdatedDeferred);
@@ -505,7 +534,15 @@ describe("VcsStatusBroadcaster", () => {
534
assert.deepStrictEqual(state.remoteStatusRefreshUpstreamValues, [false, false]);
535
536
yield* Scope.close(scope, Exit.void);
508
- }).pipe(Effect.provide(Layer.merge(testLayer, TestClock.layer())));
537
+ }).pipe(
538
+ Effect.provide(
539
+ Layer.mergeAll(
540
+ testLayer,
541
+ TestClock.layer(),
542
+ Logger.layer([logger], { mergeWithExisting: false }),
543
+ ),
544
+ ),
545
+ );
546
});
547
548
it.effect("delays automatic refresh when a cached remote snapshot is available", () => {
@@ -573,6 +610,27 @@ describe("VcsStatusBroadcaster", () => {
610
);
611
});
612
613
+ it("summarizes refresh causes without exposing nested failure details", () => {
614
+ const nestedCause = new Error("private nested failure detail");
615
+ const failure = new GitManagerError({
616
+ operation: "VcsStatusBroadcaster.remoteStatus",
617
+ cwd: "/private/user/workspace/repo",
618
+ detail: "private Git failure detail",
619
+ cause: nestedCause,
620
+ });
621
+ const cause = Cause.combine(Cause.fail(failure), Cause.die(new TypeError("private defect")));
622
+
623
+ assert.deepStrictEqual(VcsStatusBroadcaster.remoteRefreshFailureDiagnostics(cause), {
624
+ reasonCount: 2,
625
+ failureCount: 1,
626
+ failureTags: ["GitManagerError"],
627
+ failureOperations: ["VcsStatusBroadcaster.remoteStatus"],
628
+ defectCount: 1,
629
+ defectTags: ["TypeError"],
630
+ interruptionCount: 0,
631
+ });
632
+ });
633
+
634
it.effect("stops the remote poller after the last stream subscriber disconnects", () => {
635
const state = {
636
currentLocalStatus: baseLocalStatus,
apps/server/src/vcs/VcsStatusBroadcaster.ts
+93
-2
@@ -1,3 +1,4 @@
1
+import * as Cause from "effect/Cause";
2
import * as Context from "effect/Context";
3
import * as Duration from "effect/Duration";
4
import * as Effect from "effect/Effect";
@@ -26,6 +27,91 @@ import * as GitWorkflowService from "../git/GitWorkflowService.ts";
27
const DEFAULT_VCS_STATUS_REFRESH_INTERVAL = Duration.seconds(30);
28
const VCS_STATUS_REFRESH_FAILURE_BASE_DELAY = Duration.seconds(30);
29
const VCS_STATUS_REFRESH_FAILURE_MAX_DELAY = Duration.minutes(15);
30
+const MAX_FAILURE_DIAGNOSTIC_VALUES = 8;
31
+const MAX_FAILURE_DIAGNOSTIC_VALUE_LENGTH = 128;
32
+
33
+function boundedDiagnosticValue(value: string): string {
34
+ return value.slice(0, MAX_FAILURE_DIAGNOSTIC_VALUE_LENGTH);
35
+}
36
+
37
+function diagnosticValueTag(value: unknown): string {
38
+ try {
39
+ if (
40
+ typeof value === "object" &&
41
+ value !== null &&
42
+ "_tag" in value &&
43
+ typeof value._tag === "string"
44
+ ) {
45
+ return boundedDiagnosticValue(value._tag);
46
+ }
47
+ if (value instanceof Error) {
48
+ return boundedDiagnosticValue(value.name);
49
+ }
50
+ return typeof value;
51
+ } catch {
52
+ return "Uninspectable";
53
+ }
54
+}
55
+
56
+function diagnosticFailureOperation(value: unknown): string | undefined {
57
+ try {
58
+ if (
59
+ typeof value === "object" &&
60
+ value !== null &&
61
+ "operation" in value &&
62
+ typeof value.operation === "string"
63
+ ) {
64
+ return boundedDiagnosticValue(value.operation);
65
+ }
66
+ } catch {
67
+ return undefined;
68
+ }
69
+ return undefined;
70
+}
71
+
72
+function addUniqueDiagnosticValue(values: Array<string>, value: string | undefined): void {
73
+ if (
74
+ value !== undefined &&
75
+ values.length < MAX_FAILURE_DIAGNOSTIC_VALUES &&
76
+ !values.includes(value)
77
+ ) {
78
+ values.push(value);
79
+ }
80
+}
81
+
82
+export function remoteRefreshFailureDiagnostics(cause: Cause.Cause<unknown>) {
83
+ const failureTags: Array<string> = [];
84
+ const failureOperations: Array<string> = [];
85
+ const defectTags: Array<string> = [];
86
+ let failureCount = 0;
87
+ let defectCount = 0;
88
+ let interruptionCount = 0;
89
+
90
+ for (const reason of cause.reasons) {
91
+ if (Cause.isFailReason(reason)) {
92
+ failureCount += 1;
93
+ addUniqueDiagnosticValue(failureTags, diagnosticValueTag(reason.error));
94
+ addUniqueDiagnosticValue(failureOperations, diagnosticFailureOperation(reason.error));
95
+ continue;
96
+ }
97
+ if (Cause.isDieReason(reason)) {
98
+ defectCount += 1;
99
+ addUniqueDiagnosticValue(defectTags, diagnosticValueTag(reason.defect));
100
+ continue;
101
+ }
102
+ interruptionCount += 1;
103
+ }
104
+
105
+ return {
106
+ reasonCount: cause.reasons.length,
107
+ failureCount,
108
+ failureTags,
109
+ failureOperations,
110
+ defectCount,
111
+ defectTags,
112
+ interruptionCount,
113
+ };
114
+}
115
116
interface VcsStatusChange {
117
readonly cwd: string;
@@ -318,14 +404,19 @@ export const make = Effect.gen(function* () {
404
return activeInterval;
405
}
406
407
+ const interruptionReasons = exit.cause.reasons.filter(Cause.isInterruptReason);
408
+ if (interruptionReasons.length > 0) {
409
+ return yield* Effect.failCause(Cause.fromReasons<never>(interruptionReasons));
410
+ }
411
+
412
const consecutiveFailures = yield* Ref.updateAndGet(
413
consecutiveFailuresRef,
414
(count) => count + 1,
415
);
416
const nextDelay = remoteRefreshFailureDelay(consecutiveFailures, activeInterval);
417
yield* Effect.logWarning("VCS remote status refresh failed", {
327
- cwd,
328
- detail: exit.cause.toString(),
418
+ cwdLength: cwd.length,
419
+ ...remoteRefreshFailureDiagnostics(exit.cause),
420
consecutiveFailures,
421
nextDelayMs: Duration.toMillis(nextDelay),
422
});
packages/contracts/src/git.ts
+1
@@ -352,6 +352,7 @@ export class TextGenerationError extends Schema.TaggedErrorClass<TextGenerationE
352
353
export class GitManagerError extends Schema.TaggedErrorClass<GitManagerError>()("GitManagerError", {
354
operation: Schema.String,
355
+ cwd: Schema.String,
356
detail: Schema.String,
357
cause: Schema.optional(Schema.Defect()),
358
}) {