Skip to content

Commit 00c1dce

Browse files
juliusmarmingecodex
andcommitted
sanitize VCS refresh diagnostics
Co-authored-by: codex <[email protected]>
1 parent a5e7975 commit 00c1dce

2 files changed

Lines changed: 154 additions & 6 deletions

File tree

‎apps/server/src/vcs/VcsStatusBroadcaster.test.ts‎

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { assert, it, describe } from "@effect/vitest";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import * as Cause from "effect/Cause";
34
import * as Deferred from "effect/Deferred";
45
import * as Duration from "effect/Duration";
56
import * as Effect from "effect/Effect";
67
import * as Exit from "effect/Exit";
78
import * as FileSystem from "effect/FileSystem";
89
import * as Layer from "effect/Layer";
10+
import * as Logger from "effect/Logger";
911
import * as Option from "effect/Option";
1012
import * as Path from "effect/Path";
1113
import * as Scope from "effect/Scope";
@@ -432,6 +434,12 @@ describe("VcsStatusBroadcaster", () => {
432434
remoteInvalidationCalls: 0,
433435
remoteStatusRefreshUpstreamValues: [] as Array<boolean | undefined>,
434436
};
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+
});
435443
let firstRemoteAttemptDeferred: Deferred.Deferred<void> | null = null;
436444
const testLayer = VcsStatusBroadcaster.layer.pipe(
437445
Layer.provideMerge(NodeServices.layer),
@@ -450,8 +458,9 @@ describe("VcsStatusBroadcaster", () => {
450458
return Effect.fail(
451459
new GitManagerError({
452460
operation: "VcsStatusBroadcaster.test",
453-
cwd: "/repo",
454-
detail: "initial remote status failed",
461+
cwd: privateCwd,
462+
detail: "private initial remote status failure",
463+
cause: nestedCause,
455464
}),
456465
).pipe(
457466
Effect.ensuring(
@@ -482,7 +491,7 @@ describe("VcsStatusBroadcaster", () => {
482491
const remoteUpdatedDeferred = yield* Deferred.make<VcsStatusStreamEvent>();
483492
yield* Stream.runForEach(
484493
broadcaster.streamStatus(
485-
{ cwd: "/repo" },
494+
{ cwd: privateCwd },
486495
{ automaticRemoteRefreshInterval: Effect.succeed(Duration.zero) },
487496
),
488497
(event) =>
@@ -494,6 +503,24 @@ describe("VcsStatusBroadcaster", () => {
494503
yield* Deferred.await(firstRemoteAttemptDeferred);
495504
yield* Effect.yieldNow;
496505
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+
);
497524

498525
yield* TestClock.adjust(Duration.seconds(30));
499526
const remoteUpdated = yield* Deferred.await(remoteUpdatedDeferred);
@@ -507,7 +534,15 @@ describe("VcsStatusBroadcaster", () => {
507534
assert.deepStrictEqual(state.remoteStatusRefreshUpstreamValues, [false, false]);
508535

509536
yield* Scope.close(scope, Exit.void);
510-
}).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+
);
511546
});
512547

513548
it.effect("delays automatic refresh when a cached remote snapshot is available", () => {
@@ -575,6 +610,28 @@ describe("VcsStatusBroadcaster", () => {
575610
);
576611
});
577612

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+
assert.strictEqual(failure.cause, nestedCause);
633+
});
634+
578635
it.effect("stops the remote poller after the last stream subscriber disconnects", () => {
579636
const state = {
580637
currentLocalStatus: baseLocalStatus,

‎apps/server/src/vcs/VcsStatusBroadcaster.ts‎

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as Cause from "effect/Cause";
12
import * as Context from "effect/Context";
23
import * as Duration from "effect/Duration";
34
import * as Effect from "effect/Effect";
@@ -26,6 +27,91 @@ import * as GitWorkflowService from "../git/GitWorkflowService.ts";
2627
const DEFAULT_VCS_STATUS_REFRESH_INTERVAL = Duration.seconds(30);
2728
const VCS_STATUS_REFRESH_FAILURE_BASE_DELAY = Duration.seconds(30);
2829
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+
}
29115

30116
interface VcsStatusChange {
31117
readonly cwd: string;
@@ -318,14 +404,19 @@ export const make = Effect.gen(function* () {
318404
return activeInterval;
319405
}
320406

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+
321412
const consecutiveFailures = yield* Ref.updateAndGet(
322413
consecutiveFailuresRef,
323414
(count) => count + 1,
324415
);
325416
const nextDelay = remoteRefreshFailureDelay(consecutiveFailures, activeInterval);
326417
yield* Effect.logWarning("VCS remote status refresh failed", {
327-
cwd,
328-
detail: exit.cause.toString(),
418+
cwdLength: cwd.length,
419+
...remoteRefreshFailureDiagnostics(exit.cause),
329420
consecutiveFailures,
330421
nextDelayMs: Duration.toMillis(nextDelay),
331422
});

0 commit comments

Comments
 (0)