From 46ce5ec25ec65b53ddd285cf571cf3014e014fc4 Mon Sep 17 00:00:00 2001 From: ci-check Date: Tue, 24 Nov 2026 23:02:00 +0800 Subject: [PATCH] fix(persist): #7670 warn on cold load when the [acp-drift] fold-coverage alert is off MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pendingFoldUsage latch is intentionally NOT restored on cold load (#2129 triage), so the [acp-drift] fold-coverage alert is off on the first post-load request — the exact round that could destroy the fold (client resends mutated/truncated history losing covered ids → block silently active:false, no log). buildSession now warns whenever a session with active fold blocks is loaded, so the operator knows the alert is off. Latch restoration behavior is unchanged (Option B stopgap from #7670; the proper fix — re-deriving the latch — is a separate design decision). Test: tests/persist-fold-latch-warn.test.ts pins both directions (active blocks ⇒ warn fires; no/inactive blocks ⇒ warn stays silent). --- src/persist.ts | 13 ++- tests/persist-fold-latch-warn.test.ts | 147 ++++++++++++++++++++++++++ 2 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 tests/persist-fold-latch-warn.test.ts diff --git a/src/persist.ts b/src/persist.ts index ecdc7d59d4..0751ac3ca7 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -4247,7 +4247,7 @@ function buildSession(parsed: PersistedSession): Session { // Read grouped shape (v2+); fall back to flat fields for v1 files. const meta = parsed.meta ?? {}; const stats = parsed.stats ?? {}; - return { + const session: Session = { id: parsed.id, meta: { protocol: meta.protocol ?? parsed.protocol, @@ -4355,6 +4355,17 @@ function buildSession(parsed: PersistedSession): Session { persisted: true, pendingRetrievals: [], }; + // #7670: the pendingFoldUsage latch is intentionally NOT restored (see the + // stats comment above), so the [acp-drift] fold-coverage alert is off on + // the first post-load request — the exact round that could destroy the fold + // (client resends mutated/truncated history losing covered ids → block + // silently active:false, no log). Warn whenever a session with active fold + // blocks is loaded so the operator knows the alert is off. + const activeFoldBlocks = session.state.blocks.filter((b) => b.active).length; + if (activeFoldBlocks > 0) { + loggerLog("warn", `[persist] ${session.id}: loaded ${activeFoldBlocks} active fold block(s) with pendingFoldUsage latch NOT restored — the [acp-drift] fold-coverage alert is off on the first post-load request; a fold destroyed in that round will not be logged (#7670)`); + } + return session; } export function isValidRecord(parsed: unknown): parsed is PersistedSession { diff --git a/tests/persist-fold-latch-warn.test.ts b/tests/persist-fold-latch-warn.test.ts new file mode 100644 index 0000000000..92fe477f2d --- /dev/null +++ b/tests/persist-fold-latch-warn.test.ts @@ -0,0 +1,147 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { tmpdir } from "node:os"; +import { mkdtempSync } from "node:fs"; +import { join } from "node:path"; +import { createInitialState } from "acp-kernel"; +import type { CompressionBlock } from "acp-kernel"; +import { SessionStore } from "../src/persist.ts"; +import { setLogCapture } from "../src/logger.ts"; +import type { Session } from "../src/session.ts"; +import { rmrf } from "./tmp-rm.ts"; + +// #7670: the pendingFoldUsage latch is intentionally NOT restored on cold load +// (see the stats comment in buildSession), so the [acp-drift] fold-coverage +// alert is off on the first post-load request — the exact round that could +// destroy the fold. buildSession now warns whenever a session with active fold +// blocks is loaded so the operator knows the alert is off. These tests pin both +// directions: active blocks ⇒ warn fires (positive), no active blocks ⇒ warn +// stays silent (negative). + +function makeSession(id: string): Session { + return { + id, + meta: {}, + stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 }, + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + pendingRetrievals: [], + }; +} + +function makeBlock(active: boolean): CompressionBlock { + return { + blockId: "blk-1", + runId: "run-1", + tier: 1, + summary: "test fold summary", + directMessageIds: ["m00001"], + effectiveMessageIds: ["m00001"], + directBlockIds: [], + compressedTokens: 100, + createdAt: Date.now(), + survivedCount: 0, + generation: "young", + active, + }; +} + +const LATCH_MARKER = "pendingFoldUsage latch NOT restored"; + +test("#7670: cold-loading a session with active fold blocks warns that the [acp-drift] alert is off", async () => { + const dir = mkdtempSync(join(tmpdir(), "bili-foldlatch-pos-")); + const stores: SessionStore[] = []; + try { + const store = new SessionStore({ dir, debounceMs: 5, enabled: true }); + stores.push(store); + const s = makeSession("foldlatch-positive"); + s.state.blocks.push(makeBlock(true)); + await store.writeNow(s); + await store.flushAll([]); + + const captured: Array<{ level: string; msg: string }> = []; + setLogCapture((level, msg) => captured.push({ level, msg })); + try { + const reloaded = new SessionStore({ dir, debounceMs: 5, enabled: true }); + stores.push(reloaded); + const back = reloaded.loadSync("foldlatch-positive"); + assert.ok(back, "session reloaded"); + assert.equal(back.stats.pendingFoldUsage, undefined, "latch is not restored (intentional, #2129)"); + const warn = captured.find((c) => c.level === "warn" && c.msg.includes(LATCH_MARKER)); + assert.ok(warn, `a warn log naming the latch must fire on cold load, got: ${JSON.stringify(captured)}`); + assert.ok(warn.msg.includes("foldlatch-positive"), "the warn names the session id"); + assert.ok(warn.msg.includes("1 active fold block"), "the warn counts the active blocks"); + await reloaded.flushAll([]); + } finally { + setLogCapture(null); + } + } finally { + for (const st of stores) st.closeQuietly(); + rmrf(dir); + } +}); + +test("#7670: cold-loading a session with NO active fold blocks stays silent (negative)", async () => { + const dir = mkdtempSync(join(tmpdir(), "bili-foldlatch-neg-")); + const stores: SessionStore[] = []; + try { + const store = new SessionStore({ dir, debounceMs: 5, enabled: true }); + stores.push(store); + const s = makeSession("foldlatch-negative"); + // No blocks at all — the common case for a fresh session. + await store.writeNow(s); + await store.flushAll([]); + + const captured: Array<{ level: string; msg: string }> = []; + setLogCapture((level, msg) => captured.push({ level, msg })); + try { + const reloaded = new SessionStore({ dir, debounceMs: 5, enabled: true }); + stores.push(reloaded); + const back = reloaded.loadSync("foldlatch-negative"); + assert.ok(back, "session reloaded"); + const warn = captured.find((c) => c.level === "warn" && c.msg.includes(LATCH_MARKER)); + assert.equal(warn, undefined, `no latch warn expected for a blockless session, got: ${JSON.stringify(warn)}`); + await reloaded.flushAll([]); + } finally { + setLogCapture(null); + } + } finally { + for (const st of stores) st.closeQuietly(); + rmrf(dir); + } +}); + +test("#7670: cold-loading a session whose fold blocks are all INACTIVE stays silent (negative)", async () => { + const dir = mkdtempSync(join(tmpdir(), "bili-foldlatch-inact-")); + const stores: SessionStore[] = []; + try { + const store = new SessionStore({ dir, debounceMs: 5, enabled: true }); + stores.push(store); + const s = makeSession("foldlatch-inactive"); + s.state.blocks.push(makeBlock(false)); + await store.writeNow(s); + await store.flushAll([]); + + const captured: Array<{ level: string; msg: string }> = []; + setLogCapture((level, msg) => captured.push({ level, msg })); + try { + const reloaded = new SessionStore({ dir, debounceMs: 5, enabled: true }); + stores.push(reloaded); + const back = reloaded.loadSync("foldlatch-inactive"); + assert.ok(back, "session reloaded"); + const warn = captured.find((c) => c.level === "warn" && c.msg.includes(LATCH_MARKER)); + assert.equal(warn, undefined, `no latch warn expected when every block is inactive, got: ${JSON.stringify(warn)}`); + await reloaded.flushAll([]); + } finally { + setLogCapture(null); + } + } finally { + for (const st of stores) st.closeQuietly(); + rmrf(dir); + } +});