Skip to content

Commit 2b8e94e

Browse files
authored
fix(sessions): warn when a fire-and-forget disk-budget sweep fails (#124681)
The throttled budget kick swallowed every sweep failure into an empty catch. Retrying on the next kick is right, but a persistently failing sweep (corrupt store, permission loss) meant unbounded disk growth with no operator signal — silent failure on the default maintenance path. Warn with the error and store path before the retry.
1 parent 2404d41 commit 2b8e94e

2 files changed

Lines changed: 56 additions & 3 deletions

File tree

‎src/config/sessions/session-history-eviction.test.ts‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
import { randomBytes } from "node:crypto";
22
import fs from "node:fs";
33
import path from "node:path";
4-
import { afterEach, beforeEach, describe, expect, it } from "vitest";
4+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5+
6+
const evictionWarnSpy = vi.hoisted(() => vi.fn());
7+
vi.mock("../../logging/subsystem.js", async () => {
8+
const actual = await vi.importActual<typeof import("../../logging/subsystem.js")>(
9+
"../../logging/subsystem.js",
10+
);
11+
return {
12+
...actual,
13+
createSubsystemLogger: (subsystem: string) => {
14+
const logger = actual.createSubsystemLogger(subsystem);
15+
return subsystem === "sessions/history-eviction"
16+
? { ...logger, warn: evictionWarnSpy }
17+
: logger;
18+
},
19+
};
20+
});
521
import { executeSqliteQuerySync } from "../../infra/kysely-sync.js";
622
import { beginSessionWorkAdmission } from "../../sessions/session-lifecycle-admission.js";
723
import {
@@ -25,6 +41,7 @@ import { getSessionKysely } from "./session-accessor.sqlite-scope.js";
2541
import {
2642
enforceSqliteSessionHistoryDiskBudget,
2743
inspectSqliteSessionHistoryDiskBudget,
44+
kickSessionHistoryDiskBudgetMaintenance,
2845
} from "./session-history-eviction.js";
2946
import { resolveSqliteTargetFromSessionStorePath } from "./session-sqlite-target.js";
3047

@@ -344,6 +361,33 @@ describe("SQLite historical session disk budget", () => {
344361
expect(sessionExists("stale-live")).toBe(true);
345362
});
346363

364+
it("warns when a fire-and-forget budget sweep fails instead of swallowing it", async () => {
365+
evictionWarnSpy.mockClear();
366+
// The kick gate reads maxDiskBytes twice synchronously; the queued sweep
367+
// re-reads it asynchronously. Throwing on the later read rejects the
368+
// fire-and-forget promise, exercising the catch path deterministically.
369+
let maxDiskBytesReads = 0;
370+
const maintenanceConfig = {
371+
mode: "enforce",
372+
highWaterBytes: 1,
373+
get maxDiskBytes() {
374+
maxDiskBytesReads += 1;
375+
if (maxDiskBytesReads > 1) {
376+
throw new Error("sweep exploded");
377+
}
378+
return 1;
379+
},
380+
} as never;
381+
382+
kickSessionHistoryDiskBudgetMaintenance({ storePath, force: true, maintenanceConfig });
383+
await vi.waitFor(() => {
384+
expect(evictionWarnSpy).toHaveBeenCalledWith(
385+
expect.stringContaining("disk-budget sweep failed"),
386+
expect.objectContaining({ storePath }),
387+
);
388+
});
389+
});
390+
347391
it("warn mode reports physical overage without extracting or deleting history", async () => {
348392
await createHistoricalTranscript({
349393
content: "warn history",

‎src/config/sessions/session-history-eviction.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from "node:fs";
22
import path from "node:path";
33
import { executeSqliteQuerySync } from "../../infra/kysely-sync.js";
4+
import { createSubsystemLogger } from "../../logging/subsystem.js";
45
import {
56
collectActiveSessionWorkAdmissionIdentities,
67
runExclusiveSessionLifecycleMutation,
@@ -441,6 +442,8 @@ async function pruneAllSessionTranscriptArchivesToHighWater(params: {
441442
};
442443
}
443444

445+
const log = createSubsystemLogger("sessions/history-eviction");
446+
444447
const PHYSICAL_BUDGET_CHECK_INTERVAL_MS = 30 * 60 * 1000;
445448
// Single-slot per store: ordinary entry writes kick a throttled background
446449
// budget pass so an over-budget database self-heals without waiting for a
@@ -503,8 +506,14 @@ export function kickSessionHistoryDiskBudgetMaintenance(params: {
503506
mode: maintenance.mode,
504507
maintenance,
505508
})
506-
.catch(() => {
507-
// Best-effort: budget pressure is retried on the next throttled kick.
509+
.catch((error: unknown) => {
510+
// Best-effort: budget pressure is retried on the next throttled kick,
511+
// but a persistently failing sweep must stay operator-visible — silent
512+
// failure here means unbounded disk growth with no signal.
513+
log.warn("session history disk-budget sweep failed; retrying on next kick", {
514+
error,
515+
storePath: params.storePath,
516+
});
508517
})
509518
.finally(() => {
510519
state.running = false;

0 commit comments

Comments
 (0)