From 0858098f9eb041854c44c4eb3ba479733e667d50 Mon Sep 17 00:00:00 2001 From: mariopasquali Date: Mon, 16 Nov 2026 11:10:25 +0100 Subject: [PATCH 1/5] fix(storage): do not serve migrated block placeholders as content snapshots Full-mode import stores { one: null, full: { text: block.summary, count: 0 } } for blocks whose legacy record had no snapshot, so every block row has a view. Readers took that row as the snapshot: after migration, decompress returned the block summary as the block's content instead of collecting the live messages. Readers now go through blockContentSnapshot(), which treats the placeholder like a missing entry. --- src/decompress-shared.ts | 6 +- src/export.ts | 10 +-- src/external-summary-compress.ts | 3 +- src/session.ts | 16 ++++ src/web/sessions-data.ts | 6 +- tests/migrate-block-placeholder.test.ts | 72 +++++++++++++++ .../storage-block-content-placeholder.test.ts | 90 +++++++++++++++++++ 7 files changed, 191 insertions(+), 12 deletions(-) create mode 100644 tests/migrate-block-placeholder.test.ts create mode 100644 tests/storage-block-content-placeholder.test.ts diff --git a/src/decompress-shared.ts b/src/decompress-shared.ts index f5eabd5603..a1561634df 100644 --- a/src/decompress-shared.ts +++ b/src/decompress-shared.ts @@ -22,7 +22,7 @@ import { existsSync, mkdirSync, unlinkSync, writeFileSync } from "node:fs"; import { dirname, join, resolve as resolvePath } from "node:path"; import { tmpdir } from "node:os"; import { fileURLToPath } from "node:url"; -import { markDirty, preCompactionArchiveOf, peekSession, findSessionByCanonicalId, type Session } from "./session.js"; +import { blockContentSnapshot, markDirty, preCompactionArchiveOf, peekSession, findSessionByCanonicalId, type Session } from "./session.js"; import { getStore } from "./persist.js"; import { ccrEnabled, contentStoreOf } from "./store.js"; import { safePrefix } from "./text-safe.js"; @@ -218,7 +218,7 @@ export function resolveDecompress( } const full = args.full === true || args.full === "true"; - const cached = ctx.session.blockContents.get(blockId); + const cached = blockContentSnapshot(ctx.session.blockContents, block); let body: string; let count: number; if (cached) { @@ -591,7 +591,7 @@ function resolveDerivedDecompress( return toolFail(`[decompress FAILED: range restore (startId/endId) of derived-parent blocks is not supported — decompress "${blockId}" without range args (#1333)]`); } const full = args.full === true || args.full === "true"; - const cached = anc.blockContents.get(blockId); + const cached = blockContentSnapshot(anc.blockContents, block); const view = cached ? (full ? cached.full : (cached.one ?? cached.full)) : undefined; const body = view?.text || block.summary; const count = view?.count ?? 0; diff --git a/src/export.ts b/src/export.ts index 14a47aecf5..26c17f678d 100644 --- a/src/export.ts +++ b/src/export.ts @@ -1,7 +1,7 @@ import { existsSync, mkdirSync, writeFileSync } from "node:fs"; import path from "node:path"; import { indexToRef, matchSession as matchSessionKernel, parseStoredPlaceholder, refForRaw, renderHandoff as renderHandoffKernel, renderMessage, type CompressionBlock, type MessageContentStore, type CoreMessage } from "acp-kernel"; -import type { Session } from "./session.js"; +import { blockContentSnapshot, type Session } from "./session.js"; import { SessionStore } from "./persist.js"; import { SqliteBackend, backendSelectionNotice, listBackendNames, openBackend, resolveBackendName, resolveMysqlDsn, selectedBackendRaw, type StorageBackend } from "./storage/backend.js"; import { fileStorageBackend, fileStorageMysqlDsn } from "./knobs.js"; @@ -64,7 +64,7 @@ function blockLedgerLine(s: Session, b: CompressionBlock): string { } } } - const orig = s.blockContents.get(b.blockId)?.full.text.length; + const orig = blockContentSnapshot(s.blockContents, b)?.full.text.length; const status = !b.active ? "consumed" : b.retired ? "retired" : "active"; return `messages ${range} · original ${orig !== undefined ? `${orig} chars` : "not cached"} · summary ${b.summary.length} chars · ${status}`; } @@ -196,7 +196,7 @@ export function renderHandoff(s: Session, full: boolean): string { lines.push(""); lines.push(b.summary.trim()); lines.push(""); - const content = s.blockContents.get(b.blockId); + const content = blockContentSnapshot(s.blockContents, b); if (full && content) { lines.push(`### Original messages (${content.full.count})`); lines.push(""); @@ -242,7 +242,7 @@ function blockSummariesSection(s: Session, includeOriginals: boolean, priorDoc: lines.push(summary); } lines.push(""); - const content = includeOriginals ? s.blockContents.get(b.blockId) : undefined; + const content = includeOriginals ? blockContentSnapshot(s.blockContents, b) : undefined; if (content) { lines.push(`#### Original messages (${content.full.count})`); lines.push(""); @@ -392,7 +392,7 @@ export function renderSessionJsonlFromSession(s: Session, full: boolean, content lines.push(JSON.stringify({ type: "message", seq: i, rawId: m.id, role: m.role, message: out })); } for (const [i, b] of s.state.blocks.entries()) { - const orig = s.blockContents.get(b.blockId)?.full.text.length; + const orig = blockContentSnapshot(s.blockContents, b)?.full.text.length; lines.push( JSON.stringify({ type: "block", diff --git a/src/external-summary-compress.ts b/src/external-summary-compress.ts index 004032b072..652fcbac0d 100644 --- a/src/external-summary-compress.ts +++ b/src/external-summary-compress.ts @@ -1,4 +1,5 @@ import { collectBlockContent } from "acp-kernel"; +import { blockContentSnapshot } from "./session.js"; import { buildCompressSystemPrompt, parseCompressInput, type ParsedRange } from "./compress-tool.js"; import { messageTimestampSections } from "./external-summary-surface.js"; import { configuredSummaryPlan } from "./external-summary-runtime.js"; @@ -84,7 +85,7 @@ export async function applyConfiguredCompression(input: unknown, ctx: RewriteCtx const collected = collectBlockContent(state, block, source, { full: false }).text; if (collected.trim()) return collected; // An inline-restored block may no longer have originals in the wire view. - const cached = ctx.session.blockContents.get(block.blockId); + const cached = blockContentSnapshot(ctx.session.blockContents, block); return cached?.one?.text ?? cached?.full?.text ?? ""; }).join("\n\n"); if (!content.trim()) continue; diff --git a/src/session.ts b/src/session.ts index f518aa9ca0..380fcd30d4 100644 --- a/src/session.ts +++ b/src/session.ts @@ -29,6 +29,22 @@ export type BlockContent = { full: BlockView; }; +/** The compression-time snapshot of a block, or undefined when there is none. + * The unified index stores a placeholder for blocks whose legacy record had no + * snapshot ({ one: null, full: { text: block.summary, count: 0 } }, see + * completeBlockContents in src/storage/ingest.ts) so every block row has a view; + * replay serves it back. It is not a snapshot: readers must treat it like a + * missing entry (decompress collects from the live messages, the web view says + * "no snapshot") or a migrated session returns the summary as the block's content. */ +export function blockContentSnapshot( + contents: { get(blockId: string): BlockContent | undefined }, + block: { blockId: string; summary?: string }, +): BlockContent | undefined { + const content = contents.get(block.blockId); + if (content !== undefined && content.one === null && content.full.count === 0 && content.full.text === block.summary) return undefined; + return content; +} + /** #3050 R3 ②b: block-contents map that folds away to (almost) nothing once * a save has captured it, and re-materializes from the store on the first * access. Idle sessions stop carrying every folded block's originals; the diff --git a/src/web/sessions-data.ts b/src/web/sessions-data.ts index 19933d6dc0..b53d233994 100644 --- a/src/web/sessions-data.ts +++ b/src/web/sessions-data.ts @@ -1,5 +1,5 @@ import { defaultCountTokens, orderedRefPair, type CompressionBlock } from "acp-kernel"; -import { listSessions, displayContextBest, type ContextBest, type Session, type BlockContent } from "../session.js"; +import { blockContentSnapshot, listSessions, displayContextBest, type ContextBest, type Session, type BlockContent } from "../session.js"; import { conflictClientOf, conflictEventsOf } from "../conflict-watch.js"; import { isDisplayOnlyConflictDetail, isSiblingConflictDetail } from "../thirdparty-scan.js"; import { fileNameMatchesId, getStore, isValidRecord, _resetStoreForTest, relPathFor } from "../persist.js"; @@ -1729,7 +1729,7 @@ function snapshotRefIndexFor(session: Session, block: CompressionBlock): Map String(n).padStart(5, "0"); + +function fold() { + const core = createCore(); + const config = applyCompressSettings(defaultConfig(200_000), 200_000, { ccr: { enabled: true, minToolTokens: 50 } }) as Config; + const session = getSession(`mig-ph-${Math.random().toString(36).slice(2)}`); + storeEffectiveCcr(session, { enabled: true, minToolTokens: 50 }); + const raw: CoreMessage[] = []; + for (let i = 0; i < 20; i++) { + raw.push({ + id: `h_${i}`, + role: i % 2 === 0 ? "user" : "assistant", + contentType: "text", + text: `\x3cacp tokens="2K" type="text"\x3em${pad(i + 1)}\x3c/acp\x3e\nHistorical detail ${i}. ${"x".repeat(1000)}`, + }); + } + const turn = core.processTurn({ messages: raw, state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); + session.state = turn.state; + const ctx = { core, config, messages: turn.messages, session, log: () => {} }; + applyRanges(parseCompressInput({ content: [{ startId: "m00001", endId: "m00007", summary: "Early history: messages 1-7 covered initial setup.", topic: "Early history" }] }), ctx); + const block = [...session.state.blocks].find((b) => b.active); + assert.ok(block, "a block was created"); + return { ctx, session, block }; +} + +test("migration: decompress of a block whose snapshot is the index placeholder restores the originals, not the summary", () => { + const { ctx, session, block } = fold(); + const legacyLoad = resolveDecompress({ blockId: block.blockId }, ctx).text; + assert.match(legacyLoad, /Historical detail 0\./, "cached snapshot restores originals"); + + // Legacy JSON without a snapshot for this block (pruned) → collected from the live messages. + session.blockContents = new Map(); + const noSnapshot = resolveDecompress({ blockId: block.blockId }, ctx).text; + assert.match(noSnapshot, /Historical detail 0\./); + + // Same session after the unified import + index replay: the placeholder row comes back. + session.blockContents = completeBlockContents(new Map(), session.state.blocks); + assert.equal(session.blockContents.get(block.blockId)?.full.text, block.summary, "replay serves the placeholder"); + const migrated = resolveDecompress({ blockId: block.blockId }, ctx).text; + assert.equal(migrated, noSnapshot, "migrated decompress must equal the legacy no-snapshot decompress"); +}); + +test("migration: blockContentSnapshot ignores only the placeholder shape", () => { + const { session, block } = fold(); + const real = session.blockContents.get(block.blockId); + assert.ok(real); + assert.equal(blockContentSnapshot(session.blockContents, block), real); + const placeholder = completeBlockContents(new Map(), session.state.blocks); + assert.equal(blockContentSnapshot(placeholder, block), undefined); + const emptySummary = new Map([[block.blockId, { one: null, full: { text: "different", count: 0 } }]]); + assert.ok(blockContentSnapshot(emptySummary, block), "a real zero-count snapshot with other text is kept"); +}); diff --git a/tests/storage-block-content-placeholder.test.ts b/tests/storage-block-content-placeholder.test.ts new file mode 100644 index 0000000000..6121a87d9d --- /dev/null +++ b/tests/storage-block-content-placeholder.test.ts @@ -0,0 +1,90 @@ +// Repro on master: a legacy session with a folded block but no blockContents +// snapshot. After SQLite import the index replays the completeBlockContents +// placeholder ({ one: null, full: { text: summary, count: 0 } }) and readers +// treat it as the compression-time snapshot. +import test from "node:test"; +import assert from "node:assert/strict"; +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; + +process.env.BILI_PERSIST_ZSTD = "0"; +const hermetic = fs.mkdtempSync(path.join(os.tmpdir(), "bili-ph-repro-")); +process.env.BILI_CONFIG_FILE = path.join(hermetic, "no-config.json"); +process.env.BILI_STORAGE_UNIFIED = "legacy"; + +import { createCore, defaultConfig, type Config, type CoreMessage } from "acp-kernel"; +import { applyCompressSettings } from "../src/compress-settings.ts"; +import { storeEffectiveCcr } from "../src/store.ts"; +import { getSession, type Session } from "../src/session.ts"; +import { parseCompressInput } from "../src/compress-tool.ts"; +import { applyRanges } from "../src/stream.ts"; +import { resolveDecompress } from "../src/decompress-shared.ts"; +import { SessionStore } from "../src/persist.ts"; + +const pad = (n: number): string => String(n).padStart(5, "0"); + +function fold() { + const core = createCore(); + const config = applyCompressSettings(defaultConfig(200_000), 200_000, { ccr: { enabled: true, minToolTokens: 50 } }) as Config; + const session = getSession(`ph-repro-${Math.random().toString(36).slice(2)}`); + session.meta.protocol = "anthropic"; + session.meta.upstreamOrigin = "http://127.0.0.1:8199"; + storeEffectiveCcr(session, { enabled: true, minToolTokens: 50 }); + const raw: CoreMessage[] = []; + for (let i = 0; i < 20; i++) { + raw.push({ + id: `h_${i}`, + role: i % 2 === 0 ? "user" : "assistant", + contentType: "text", + text: `\x3cacp tokens="2K" type="text"\x3em${pad(i + 1)}\x3c/acp\x3e\nHistorical detail ${i}. ${"x".repeat(1000)}`, + }); + } + const turn = core.processTurn({ messages: raw, state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); + session.state = turn.state; + const ctx = { core, config, messages: turn.messages, session, log: () => {} }; + applyRanges(parseCompressInput({ content: [{ startId: "m00001", endId: "m00007", summary: "Early history: messages 1-7 covered initial setup.", topic: "Early history" }] }), ctx); + const block = [...session.state.blocks].find((b) => b.active); + assert.ok(block, "a block was created"); + return { ctx, session, block }; +} + +function reload(dir: string, mode: string, session: Session): Session { + process.env.BILI_STORAGE_UNIFIED = mode; + const store = new SessionStore({ dir, debounceMs: 1, enabled: true }); + try { + const loaded = store.loadSync(session.id, session.meta); + assert.ok(loaded, `${mode}: loadSync returned null`); + return loaded; + } finally { + store.cancelAll(); + store.closeQuietly(); + } +} + +test("legacy session without a block snapshot: SQLite load must decompress like the JSON load", async () => { + const dir = path.join(hermetic, "store"); + const { ctx, session, block } = fold(); + // Legacy record without a snapshot for the folded block (pruned / never cached). + session.blockContents = new Map(); + process.env.BILI_STORAGE_UNIFIED = "legacy"; + const writer = new SessionStore({ dir, debounceMs: 1, enabled: true }); + await writer.writeNow(session); + writer.cancelAll(); + writer.closeQuietly(); + + const legacy = reload(dir, "legacy", session); + const legacyText = resolveDecompress({ blockId: block.blockId }, { ...ctx, session: legacy }).text; + + reload(dir, "full", session); // organic import + const migrated = reload(dir, "full", session); // served from the index + const placeholder = migrated.blockContents.get(block.blockId); + console.log("legacy has snapshot:", legacy.blockContents.get(block.blockId) !== undefined); + console.log("migrated snapshot:", JSON.stringify(placeholder)?.slice(0, 200)); + const migratedText = resolveDecompress({ blockId: block.blockId }, { ...ctx, session: migrated }).text; + console.log("legacy decompress head:", JSON.stringify(legacyText.slice(0, 160))); + console.log("migrated decompress head:", JSON.stringify(migratedText.slice(0, 160))); + assert.match(legacyText, /Historical detail 0\./, "legacy JSON load restores originals from the live messages"); + assert.equal(migratedText, legacyText, "SQLite load must not serve the block summary as its content"); +}); + From 8ed56be997c292584844ab8b482c04f8fe77256f Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 16 Nov 2026 20:42:39 +0800 Subject: [PATCH 2/5] docs(architecture): add src/responses-system-sections.ts to the module map (#6922) --- reference/architecture.md | 1 + 1 file changed, 1 insertion(+) diff --git a/reference/architecture.md b/reference/architecture.md index 484cd70bf1..9226ec7713 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -303,6 +303,7 @@ billion-context/ │ ├── remote-notices.ts # Remote announcement feed watcher: fail-open polling, last-known-good snapshot (#5548) │ ├── request-membership.ts # Settle-side membership probe: is this usage report ours (#4805) │ ├── resolver-wedge.ts # Persistent local DNS resolver wedge detector + alarm (#5221) +│ ├── responses-system-sections.ts # Single source of truth for the Responses-wire developer-section join: steady prepare + round-2 rebuild share one separator (#6922) │ ├── responses-tool-output.ts # Responses tool-output image parts: projection + patch helpers │ ├── rollout.ts # Feature rollout: per-install uuid, basis-point buckets, nested cohorts (#3062) │ ├── runaway-guard.ts # Intrinsic stream terminator for runaway tag/codepoint enumeration (#2346) From d756a5ad7a00ab471b5cfea314db8e2128088f43 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 16 Nov 2026 21:01:54 +0800 Subject: [PATCH 3/5] test: pin the placeholder precondition and complete the guard truth table (review fix for #6943) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - storage-block-content-placeholder: drop debug console.log residue; assert the reloaded row is actually the completeBlockContents placeholder shape (and the legacy load has no snapshot), so the test fails loudly if the import ever stops emitting the placeholder instead of silently passing. - migrate-block-placeholder: add the two missing negative rows to the blockContentSnapshot truth table (summary-text + real count kept; real one-view + zero full count kept) — the guard must not fire on any real snapshot shape. --- tests/migrate-block-placeholder.test.ts | 4 ++++ tests/storage-block-content-placeholder.test.ts | 9 ++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/migrate-block-placeholder.test.ts b/tests/migrate-block-placeholder.test.ts index 18e4884f85..ed444ce73c 100644 --- a/tests/migrate-block-placeholder.test.ts +++ b/tests/migrate-block-placeholder.test.ts @@ -69,4 +69,8 @@ test("migration: blockContentSnapshot ignores only the placeholder shape", () => assert.equal(blockContentSnapshot(placeholder, block), undefined); const emptySummary = new Map([[block.blockId, { one: null, full: { text: "different", count: 0 } }]]); assert.ok(blockContentSnapshot(emptySummary, block), "a real zero-count snapshot with other text is kept"); + const nonEmptyCount = new Map([[block.blockId, { one: null, full: { text: block.summary, count: 5 } }]]); + assert.ok(blockContentSnapshot(nonEmptyCount, block), "a snapshot with the summary text but real items is kept"); + const oneView = new Map([[block.blockId, { one: { text: "x", count: 1 }, full: { text: block.summary, count: 0 } }]]); + assert.ok(blockContentSnapshot(oneView, block), "a snapshot with a real one-view is kept"); }); diff --git a/tests/storage-block-content-placeholder.test.ts b/tests/storage-block-content-placeholder.test.ts index 6121a87d9d..ccde370c0e 100644 --- a/tests/storage-block-content-placeholder.test.ts +++ b/tests/storage-block-content-placeholder.test.ts @@ -78,12 +78,11 @@ test("legacy session without a block snapshot: SQLite load must decompress like reload(dir, "full", session); // organic import const migrated = reload(dir, "full", session); // served from the index - const placeholder = migrated.blockContents.get(block.blockId); - console.log("legacy has snapshot:", legacy.blockContents.get(block.blockId) !== undefined); - console.log("migrated snapshot:", JSON.stringify(placeholder)?.slice(0, 200)); + // Pin the precondition: the index replay must serve the placeholder shape — + // without it this test would stop exercising the bug and still pass. + assert.equal(legacy.blockContents.get(block.blockId), undefined, "legacy load has no snapshot for the block"); + assert.deepEqual(migrated.blockContents.get(block.blockId), { one: null, full: { text: block.summary, count: 0 } }, "index replay serves the completeBlockContents placeholder"); const migratedText = resolveDecompress({ blockId: block.blockId }, { ...ctx, session: migrated }).text; - console.log("legacy decompress head:", JSON.stringify(legacyText.slice(0, 160))); - console.log("migrated decompress head:", JSON.stringify(migratedText.slice(0, 160))); assert.match(legacyText, /Historical detail 0\./, "legacy JSON load restores originals from the live messages"); assert.equal(migratedText, legacyText, "SQLite load must not serve the block summary as its content"); }); From 245ecb1a9e5161ba97fa0b09865078025c9a2c8f Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 16 Nov 2026 22:51:46 +0800 Subject: [PATCH 4/5] fix(test): recalibrate #2929 probe anchor to nudge positions {3,5,11,14} MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The probe anchor PROXY_TOOL_AT=6 was calibrated on 2026-10-31 for #4521 (schedule {4,6,8,9}). The nudge scheduling has shifted again — nudge now lands at {3,5,11,14}. Re-calibrate the anchor to the next nudge-carrying request pair (5 → tool round, 6 → fold round). The fold/proxy-tool-round mechanics under test are unchanged. Closes: master-red on test (windows-latest, 22, 3) and (ubuntu-latest, 22, 1). --- tests/issue2929-codex-fold-double-miss.test.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/issue2929-codex-fold-double-miss.test.ts b/tests/issue2929-codex-fold-double-miss.test.ts index 81b61ca727..83ee857801 100644 --- a/tests/issue2929-codex-fold-double-miss.test.ts +++ b/tests/issue2929-codex-fold-double-miss.test.ts @@ -38,11 +38,12 @@ const NUDGE_MARKER = "Compressible ranges ("; // and ~9KiB filler turns. Pre-#4521 (master) the injections landed on // requests {7,8,9}; since #4521 the armed-but-silent status line rides an // ephemeral carrier on the gated turns and its shown-marker feeds the nudge -// gate back, so the schedule shifts to {4,6,8,9} — the fold/proxy-tool-round -// mechanics under test are unchanged, only this probe's anchor re-calibrates -// to the next nudge-carrying request pair (6 → tool round, 7 → fold round). -const PROXY_TOOL_AT = 6; // upstream request index whose response = acp_status call (round 1) -const FOLD_AT = 7; // upstream request index whose response = compress call (round 2) +// gate back, so the schedule shifts — the fold/proxy-tool-round mechanics +// under test are unchanged, only this probe's anchor re-calibrates to the +// next nudge-carrying request pair (5 → tool round, 6 → fold round). +// Re-calibrated 2026-11-16: nudge now lands at {3,5,11,14} (was {4,6,8,9}). +const PROXY_TOOL_AT = 5; // upstream request index whose response = acp_status call (round 1) +const FOLD_AT = 6; // upstream request index whose response = compress call (round 2) // FOLD_AT+1 = post-fold refreshed re-request (round 3); FOLD_AT+2 = next steady client turn. const TOTAL_TURNS = 13; From 7247410976508f4dedf42afb89450c2041141c0b Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 17 Nov 2026 01:36:34 +0800 Subject: [PATCH 5/5] test(#2929): make fold probe schedule-independent + hermetic wire shape MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The probe's hardcoded request-index anchors (PROXY_TOOL_AT/FOLD_AT) broke every time the nudge gate's schedule shifted. The last recalibration (245ecb1a9) was measured on a direct local run (timestamps ON by default since #4313), but CI runs the suite preload pin BILI_MESSAGE_TIMESTAMPS="0" (timestamps OFF) — a different wire shape, a different schedule (no nudge at index 5) — so it passed locally and failed on every CI shard. Root-cause fix instead of another anchor whack-a-mole: - Arm the fold on the first request carrying the ephemeral nudge with >=11 tagged refs; fold on the re-request after the acp_status round. The probe now materializes the #2929 scenario wherever the schedule places it. - Pin compress.messageTimestamps:false so the probe's wire shape is hermetic regardless of how the suite is launched (matches the CI preload pin). Supersedes the 245ecb1a9 recalibration. Verified: passes with and without the preload pin, same deterministic schedule (nudge at inputs[7], fold at inputs[8]). --- .../issue2929-codex-fold-double-miss.test.ts | 67 +++++++++++-------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/tests/issue2929-codex-fold-double-miss.test.ts b/tests/issue2929-codex-fold-double-miss.test.ts index 83ee857801..7419629dc1 100644 --- a/tests/issue2929-codex-fold-double-miss.test.ts +++ b/tests/issue2929-codex-fold-double-miss.test.ts @@ -34,17 +34,17 @@ type Item = Record; const SUMMARY_MARKER = "[Compressed conversation section]"; const NUDGE_MARKER = "Compressible ranges ("; -// Indices verified against the kernel's nudge gate with nudgeGrowthTokens=2000 -// and ~9KiB filler turns. Pre-#4521 (master) the injections landed on -// requests {7,8,9}; since #4521 the armed-but-silent status line rides an -// ephemeral carrier on the gated turns and its shown-marker feeds the nudge -// gate back, so the schedule shifts — the fold/proxy-tool-round mechanics -// under test are unchanged, only this probe's anchor re-calibrates to the -// next nudge-carrying request pair (5 → tool round, 6 → fold round). -// Re-calibrated 2026-11-16: nudge now lands at {3,5,11,14} (was {4,6,8,9}). -const PROXY_TOOL_AT = 5; // upstream request index whose response = acp_status call (round 1) -const FOLD_AT = 6; // upstream request index whose response = compress call (round 2) -// FOLD_AT+1 = post-fold refreshed re-request (round 3); FOLD_AT+2 = next steady client turn. +// The fold turn is materialized SCHEDULE-INDEPENDENTLY: the fake upstream +// arms on the first request carrying the ephemeral nudge (with >=11 tagged +// refs to fold) and folds on the re-request that follows the acp_status +// round. A hardcoded request-index anchor broke every time the nudge gate's +// token schedule shifted — #4521's status-line feedback ({7,8,9} → {4,6,8,9}), +// #6928's status-line removal, and #4313's timestamp-rendering default flip +// (the suite preload pin BILI_MESSAGE_TIMESTAMPS="0" changes the wire shape +// and hence the schedule: OFF {4,13} vs ON {3,5,11,14} — direct local runs +// without the preload diverge from CI). The probe also pins +// compress.messageTimestamps:false so its wire shape is hermetic regardless +// of how the suite is launched. const TOTAL_TURNS = 13; const FILLER = (seed: number, kb: number): string => { @@ -121,6 +121,13 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl try { const captured: Array<{ url: string; body: string }> = []; + // Object (not bare lets) so the outer-scope reads aren't narrowed to + // the initial "idle" — the mutations happen inside the request callback. + const fold = { + state: "idle" as "idle" | "armed" | "folded", + armIdx: -1, // upstream request index that carried the nudge (acp_status round) + foldIdx: -1, // upstream request index that carried the compress call + }; upstream = http.createServer((req, res) => { const chunks: Buffer[] = []; req.on("data", (c: Buffer) => chunks.push(c)); @@ -128,15 +135,21 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl const idx = captured.length; const body = Buffer.concat(chunks).toString("utf8"); captured.push({ url: req.url ?? "", body }); - if (idx === PROXY_TOOL_AT) { - // Round 1 of the fold turn: model asks for proxy status — the - // loop executes it locally and re-requests (round 2). + const input = (JSON.parse(body) as { input?: Item[] }).input ?? []; + if (fold.state === "idle" && hasNudge(input) && refsOf(input).length >= 11) { + // Round 1 of the fold turn: the first nudge-carrying request + // with enough refs — model asks for proxy status, the loop + // executes it locally and re-requests (round 2). + fold.state = "armed"; + fold.armIdx = idx; functionCallSse(res, `resp_${idx}`, "acp_status", "{}", `call_st_${idx}`); return; } - if (idx === FOLD_AT) { - const prev = (JSON.parse(captured[idx - 1]!.body) as { input?: Item[] }).input ?? []; - const uniq = refsOf(prev); + if (fold.state === "armed") { + // Round 2: the re-request after the acp_status round — fold. + fold.state = "folded"; + fold.foldIdx = idx; + const uniq = refsOf(input); assert.ok(uniq.length >= 11, `fold round: expected >=11 tagged refs, saw ${uniq.length}`); const args = JSON.stringify({ content: [ { startId: uniq[3]!, endId: uniq[5]!, topic: "F1A", summary: `F1A summary: ${FILLER(1, 1)}` }, @@ -145,10 +158,9 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl functionCallSse(res, `resp_${idx}`, "compress", args, `call_cmp_${idx}`); return; } - const parsed = JSON.parse(body) as { input?: Item[] }; let label = "0"; - for (let i = (parsed.input ?? []).length - 1; i >= 0; i--) { - const it = parsed.input![i]!; + for (let i = input.length - 1; i >= 0; i--) { + const it = input[i]!; if (it.type === "message" && it.role === "user" && typeof it.content === "string") { const m = (it.content as string).match(/Turn (\d+):/); if (m) { label = m[1]!; break; } @@ -170,7 +182,7 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: 200_000 } } } }, modelContextLimit: 200_000, kernelConfig: defaultConfig(200_000), - compress: { injectTool: true, injectNudge: true, nudgeGrowthTokens: 2000 }, + compress: { injectTool: true, injectNudge: true, nudgeGrowthTokens: 2000, messageTimestamps: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: true, @@ -224,17 +236,18 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl } const inputs = captured.map((c) => (JSON.parse(c.body) as { input?: Item[] }).input ?? []); - assert.ok(inputs.length >= FOLD_AT + 3, `expected >= ${FOLD_AT + 3} upstream requests, got ${inputs.length}`); + assert.ok(fold.state === "folded", `scenario miss: the nudge never fired on a request with >=11 refs — probe cannot materialize the #2929 trigger; adjust nudgeGrowthTokens/filler`); + assert.ok(inputs.length >= fold.foldIdx + 3, `expected >= ${fold.foldIdx + 3} upstream requests, got ${inputs.length}`); // (0) Scenario materialization: the nudge must have been injected into - // the PRE-FOLD original request (production shape: nudge armed -> model + // the arming (pre-fold) request (production shape: nudge armed -> model // calls proxy tool -> compress). Without it this probe does not cover // the #2929 trigger. - assert.ok(hasNudge(inputs[PROXY_TOOL_AT]!), `scenario miss: no ephemeral nudge in the pre-fold request (inputs[${PROXY_TOOL_AT}]) — probe does not cover the production trigger; adjust nudgeGrowthTokens/filler`); - console.log(`probe: nudge confirmed in pre-fold request; total upstream requests=${inputs.length}`); + assert.ok(hasNudge(inputs[fold.armIdx]!), `scenario miss: no ephemeral nudge in the arming request (inputs[${fold.armIdx}]) — probe does not cover the production trigger`); + console.log(`probe: nudge at inputs[${fold.armIdx}], fold at inputs[${fold.foldIdx}]; total upstream requests=${inputs.length}`); // (1) The post-fold re-request carries the in-place summary(s). - const rPost = inputs[FOLD_AT + 1]!; + const rPost = inputs[fold.foldIdx + 1]!; const postSums = summaryIndices(rPost); assert.ok(postSums.length >= 1, "post-fold re-request must carry the in-place summary"); @@ -242,7 +255,7 @@ test("#2929: nudge + proxy-tool-round fold keeps re-request/next-turn byte-stabl // the post-fold re-request up to and including the summary anchor. In // production this is where the chain breaks (next turn hits only the // instructions baseline). - const next = inputs[FOLD_AT + 2]!; + const next = inputs[fold.foldIdx + 2]!; const nextSums = summaryIndices(next); assert.ok(nextSums.length >= 1, `next steady turn lost the summary entirely (summaries: ${nextSums.length}) — fold did not survive to the client turn`); assert.equal(nextSums[0], postSums[0], `summary anchor drifted: re-request item[${postSums[0]}] vs next turn item[${nextSums[0]}]`);