From 08e23d0d98c17ddbfa8c820a33bdad85e074cb6d Mon Sep 17 00:00:00 2001 From: qianfeng tang <105932053+0XCocoa@users.noreply.github.com> Date: Mon, 19 Oct 2026 21:59:37 +0800 Subject: [PATCH 1/2] fix: preserve main folds across expanded short Responses requests Fixes ranxianglei/billion-context#3844 --- src/fold-reconcile.ts | 5 ++- src/server/prepare-responses.ts | 3 +- tests/fold-drift.test.ts | 26 +++++++++++++++ tests/fold-reconcile.test.ts | 32 +++++++++++++++++++ tests/preflight-fail-fast.test.ts | 53 ++++++++++++++++++++++++++++++- 5 files changed, 116 insertions(+), 3 deletions(-) diff --git a/src/fold-reconcile.ts b/src/fold-reconcile.ts index bb6d2ed930..b84202091e 100644 --- a/src/fold-reconcile.ts +++ b/src/fold-reconcile.ts @@ -1463,6 +1463,9 @@ export interface ReconcileOptions { * cleans the resend — benign churn that must not feed the #2193 destroy * streak. #2815/#2789. */ markerStripped?: number; + /** Inbound wire items before codec expansion; auxiliary requests can + * project a few items into dozens of cores without carrying history. */ + inboundMessageCount?: number; } /** Entry point wired into the four wire sites, immediately BEFORE the @@ -1490,7 +1493,7 @@ export function reconcileFoldCoverage(session: Session, msgs: CoreMessage[], opt // evidence is taken from such a pass; skipping is NOT recovery either, so // the episode state is left exactly as found (the resets above stay // reserved for true episode boundaries: reconcile off / no folds at all). - if (msgs.length < SIDE_REQUEST_MAX_MSGS) { + if (msgs.length < SIDE_REQUEST_MAX_MSGS || (opts.inboundMessageCount !== undefined && opts.inboundMessageCount < SIDE_REQUEST_MAX_MSGS)) { return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 }; } if (!session.metadata) return { kind: "noop", missing: 0, claims: 0, byPos: 0, byTool: 0, byTwin: 0, bySim: 0, byOrder: 0, byNorm: 0, byTurn: 0, byImage: 0, byShape: 0, reaped: 0, unmatched: 0 }; diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index b5de5b5bdb..23e4710adc 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -57,6 +57,7 @@ export async function prepareResponses( decide?: DecideConfig, ): Promise { const sessionId = session.id; + const inboundMessageCount = Array.isArray(parsed.input) ? parsed.input.length : 1; const stream = parsed.stream === true; ++session.stats.requests; // #3222: #651's reasoning-drop plan, computed on the INBOUND history before @@ -273,7 +274,7 @@ export async function prepareResponses( // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client // re-serialization stay covered (src/fold-reconcile.ts). - reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts) }); + reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); if (!isCompactionTrigger) { noteSystemPromptFingerprint(session, responsesClientSystem, { sessionId, log }); // #2867: the mode-independent twin — lets the ledger classify a diff --git a/tests/fold-drift.test.ts b/tests/fold-drift.test.ts index 2383a95bb3..c561e80666 100644 --- a/tests/fold-drift.test.ts +++ b/tests/fold-drift.test.ts @@ -3,8 +3,34 @@ import assert from "node:assert/strict"; import { createCore, createInitialState, defaultConfig, type CoreMessage } from "acp-kernel"; import type { Session } from "../src/session.ts"; import { foldCoverage } from "../src/session.ts"; +import { responsesToCore } from "acp-kernel/wire"; import { reconcileFoldCoverage, METADATA_DRIFT_ESCALATED, type FoldAnchor } from "../src/fold-reconcile.ts"; +test("projected short requests preserve real kernel folds until the main history returns", () => { + const { session, stableResend, coveredIds } = driftFixture(); + const core = createCore(); + const config = defaultConfig(200_000); + const opts = { mode: "repair" as const }; + reconcileFoldCoverage(session, stableResend, opts); + const blockIds = session.state.blocks.map((b) => b.blockId); + const input = Array.from({ length: 6 }, (_, i) => ({ + type: "message" as const, role: "user" as const, + content: Array.from({ length: 13 }, (_, j) => ({ type: "input_text" as const, text: `short utility ${i} part ${j}` })), + })); + const { msgs } = responsesToCore({ input }); + assert.ok(msgs.length >= 10); + for (let i = 0; i < 4; i++) { + reconcileFoldCoverage(session, msgs, { ...opts, inboundMessageCount: input.length }); + session.state = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: 0 }).state; + assert.deepEqual(session.state.blocks.map((b) => b.blockId), blockIds, "sync may deactivate but must retain every fold"); + } + reconcileFoldCoverage(session, stableResend, opts); + session.state = core.processTurn({ messages: stableResend, state: session.state, config, tokenCount: 0 }).state; + assert.deepEqual(session.state.blocks.map((b) => b.blockId), blockIds); + assert.ok(session.state.blocks.every((b) => b.active)); + assert.deepEqual(session.state.blocks[0].effectiveMessageIds, coveredIds); +}); + function makeSession(): Session { return { id: `test-${Math.random().toString(36).slice(2)}`, diff --git a/tests/fold-reconcile.test.ts b/tests/fold-reconcile.test.ts index 8bc8ecbdd2..383bd04c87 100644 --- a/tests/fold-reconcile.test.ts +++ b/tests/fold-reconcile.test.ts @@ -21,6 +21,7 @@ import { import { simhashHex } from "../src/affinity-simhash.ts"; import { defaultCountTokens } from "acp-kernel"; import type { CoreMessage } from "acp-kernel"; +import { responsesToCore } from "acp-kernel/wire"; import type { Session } from "../src/session.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { conflictEventsOf } from "../src/conflict-watch.js"; @@ -652,6 +653,37 @@ describe("reconcileFoldCoverage coverage evidence + side-request guard (#2202)", assert.equal(conflict?.kind, "orphan-reap"); }); + test("short wire requests expanded into many cores never reap main folds", () => { + const ids = Array.from({ length: 12 }, (_, i) => `main${i}`); + const session = blockSession("main-fold", ids); + const originals = ids.map((id, i) => msg(id, "user", `main conversation evidence ${i}`)); + reconcileFoldCoverage(session, originals, makeOpts()); + const before = JSON.stringify({ state: session.state, metadata: session.metadata }); + // A short Responses input can project to dozens of cores. The wire + // shape, rather than projection granularity, decides whether its + // missing history can be treated as evidence of owner deletion. + const input = Array.from({ length: 6 }, (_, i) => ({ + type: "message" as const, role: "user" as const, + content: Array.from({ length: 13 }, (_, j) => ({ type: "input_text" as const, text: `utility ${i} part ${j}` })), + })); + const { msgs } = responsesToCore({ input }); + assert.ok(msgs.length >= 10, `fixture must defeat the core-count guard, got ${msgs.length}`); + const opts = { ...makeOpts(), inboundMessageCount: input.length }; + const passes = Array.from({ length: 4 }, () => { + const result = reconcileFoldCoverage(session, msgs, opts); + return { result, snapshot: JSON.stringify({ state: session.state, metadata: session.metadata }) }; + }); + assert.equal(session.state.blocks.length, 1, "four short wire passes must retain the main fold"); + for (const [i, { result, snapshot }] of passes.entries()) { + assert.equal(result.kind, "noop", `short wire pass ${i + 1} takes no deletion evidence`); + assert.equal(result.reaped, 0); + assert.equal(snapshot, before); + } + const next = reconcileFoldCoverage(session, originals, makeOpts()); + assert.equal(next.missing, 0); + assert.equal(session.state.blocks[0].blockId, "main-fold"); + }); + test("short passes do not skew the next pass's alignment", () => { const ids = Array.from({ length: 10 }, (_, i) => `k${i}`); const session = blockSession("blk4", ids); diff --git a/tests/preflight-fail-fast.test.ts b/tests/preflight-fail-fast.test.ts index d516943ca5..8422e8753c 100644 --- a/tests/preflight-fail-fast.test.ts +++ b/tests/preflight-fail-fast.test.ts @@ -8,10 +8,12 @@ process.env.NODE_ENV = "test"; // exponential backoff — these tests are about the post-retry behavior. process.env.BILI_REPLAY_RETRY_MAX = "1"; -import { defaultConfig, type Config } from "acp-kernel"; +import { createCore, defaultConfig, type Config } from "acp-kernel"; import { startServer } from "../src/server.ts"; import { type CompressSettings, type ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { METADATA_FOLD_COVERAGE } from "../src/fold-reconcile.ts"; +import { getSession } from "../src/session.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; // #301: when preflight (overflow) compression cannot proceed — the summary @@ -116,6 +118,55 @@ function startProxy(upstreamPort: number, models: Record { + const calls: Call[] = []; + const upstream = makeUpstreamOk(calls); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startProxy(upstreamPort, { "gpt-short-wire": { context: 400_000 } }); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + const post = async (input: unknown[]) => { + const response = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/responses`, { + method: "POST", headers: { "content-type": "application/json", "x-acp-session": "short-wire-pipeline" }, + body: JSON.stringify({ model: "gpt-short-wire", stream: true, max_output_tokens: 1024, input }), + }); + const body = await response.text(); + assert.equal(response.status, 200, body.slice(0, 300)); + }; + try { + const main = Array.from({ length: 24 }, (_, i) => ({ type: "message", role: i % 2 ? "assistant" : "user", content: `main turn ${i} ${"x".repeat(2000)}` })); + await post(main); + const session = getSession("short-wire-pipeline"); + const messages = session.lastMessages; + assert.ok(messages && messages.length >= 24); + session.state = createCore().applyCompression({ + ranges: [{ startRef: "m00001", endRef: "m00007", summary: "Compressed main history: seven early turns preserved the investigation, implementation decisions, and verified test evidence." }], + messages, state: session.state, config: defaultConfig(400_000), + }).state; + assert.equal(session.state.blocks.length, 1); + await post(main); + const blockIds = session.state.blocks.map((b) => b.blockId); + const coverage = structuredClone(session.metadata[METADATA_FOLD_COVERAGE]); + assert.ok(coverage); + const short = Array.from({ length: 6 }, (_, i) => ({ + type: "message", role: "user", + content: Array.from({ length: 13 }, (_, j) => ({ type: "input_text", text: `utility ${i} part ${j}` })), + })); + for (let i = 0; i < 4; i++) await post(short); + assert.deepEqual(session.metadata[METADATA_FOLD_COVERAGE], coverage, "auxiliary projection must not accumulate deletion evidence"); + assert.deepEqual(session.state.blocks.map((b) => b.blockId), blockIds); + await post(main); + assert.deepEqual(session.state.blocks.map((b) => b.blockId), blockIds); + assert.ok(session.state.blocks.every((b) => b.active), "returning main history reactivates every retained fold"); + } finally { + proxy.close(); proxy.closeIdleConnections?.(); + upstream.close(); upstream.closeIdleConnections?.(); + await Promise.allSettled([once(proxy, "close"), once(upstream, "close")]); + } +}); + test("e2e #301: overflow + summary upstream 429 → structured 503, over-window payload NOT forwarded", async () => { const calls: Call[] = []; // The preflight summarization call hits the same rate-limited upstream as From 0ef73b7e063bcd11bf2a4954ae89ffd3bc3e525a Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 01:21:23 +0800 Subject: [PATCH 2/2] fix: bound fold-reconcile short-pass evidence by raw cardinality on the openai/anthropic/google wires too (#3844) #3642's canonical per-part granularity defeats the projected core-count guard (SIDE_REQUEST_MAX_MSGS) on every codec, not just Responses: a few multi-part inbound items project into dozens of cores, so repeated short passes accumulate zombie-reap evidence against the main conversation's folds. Extends this PR's inboundMessageCount option to the three sibling wire sites (same two-line pattern as prepare-responses) so a pass whose RAW inbound cardinality is below the side-request threshold can no longer supply deletion evidence on any wire. Adds a both-directions regression per wire (#3617): uncounted expanded short passes reap the main fold (pre-fix behavior); counted passes take no evidence across four passes (byte-identical state), retain the block, and resending main history restores coverage with zero missing. --- src/server/prepare-anthropic.ts | 3 ++- src/server/prepare-google.ts | 3 ++- src/server/prepare-openai.ts | 3 ++- tests/fold-reconcile.test.ts | 37 ++++++++++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index 8838713290..00dfcd2f31 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -57,6 +57,7 @@ export async function prepareAnthropic( ): Promise { const sessionId = session.id; const stream = parsed.stream === true; + const inboundMessageCount = Array.isArray(parsed.messages) ? parsed.messages.length : 1; ++session.stats.requests; const injectTools = opts.compress.injectTool && !pluginMode; // #3417: lane decisions (profile + per-wire observation) own the @@ -283,7 +284,7 @@ export async function prepareAnthropic( // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client // re-serialization stay covered (src/fold-reconcile.ts). - reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts) }); + reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); noteSystemPromptFingerprint(session, clientSystem, { sessionId, log }); // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside // processTurn may deactivate fully-drifted blocks, erasing them. diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index a0cadfb191..4fa41deac4 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -77,6 +77,7 @@ export async function prepareGoogle( ): Promise { const sessionId = session.id; ++session.stats.requests; + const inboundMessageCount = Array.isArray(parsed.contents) ? parsed.contents.length : 1; let googleClientSystem = ""; let sysNotes: string[] = []; let googleOutboundSystem: string | undefined; @@ -168,7 +169,7 @@ export async function prepareGoogle( // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client // re-serialization stay covered (src/fold-reconcile.ts). - reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts) }); + reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); if (!isTitleGen) noteSystemPromptFingerprint(session, systemText, { sessionId, log }); // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside // processTurn may deactivate fully-drifted blocks, erasing them. diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index ee6d87503a..0ab9adb73f 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -53,6 +53,7 @@ export async function prepareOpenai( ): Promise { const sessionId = session.id; const stream = parsed.stream === true; + const inboundMessageCount = Array.isArray(parsed.messages) ? parsed.messages.length : 1; ++session.stats.requests; let openaiSystemText = ""; let sysNotes: string[] = []; @@ -174,7 +175,7 @@ export async function prepareOpenai( // [#1921] re-anchor fold coverage onto churned-but-same messages // before the #1195 snapshot, so covered ids surviving a client // re-serialization stay covered (src/fold-reconcile.ts). - reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts) }); + reconcileFoldCoverage(session, msgs, { mode: resolveFoldReconcileMode(process.env, opts.compress.reconcile), sessionId, log, markerStripped: markerArtifactCount(markerCounts), inboundMessageCount }); if (!isTitleGen) noteSystemPromptFingerprint(session, systemText, { sessionId, log }); // #1195: pre-turn snapshot of the fold's covered ids — syncBlocks inside // processTurn may deactivate fully-drifted blocks, erasing them. diff --git a/tests/fold-reconcile.test.ts b/tests/fold-reconcile.test.ts index 383bd04c87..ab8d3dcb7c 100644 --- a/tests/fold-reconcile.test.ts +++ b/tests/fold-reconcile.test.ts @@ -21,7 +21,7 @@ import { import { simhashHex } from "../src/affinity-simhash.ts"; import { defaultCountTokens } from "acp-kernel"; import type { CoreMessage } from "acp-kernel"; -import { responsesToCore } from "acp-kernel/wire"; +import { responsesToCore, openaiToCore, anthropicToCore, googleToCore } from "acp-kernel/wire"; import type { Session } from "../src/session.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { conflictEventsOf } from "../src/conflict-watch.js"; @@ -684,6 +684,41 @@ describe("reconcileFoldCoverage coverage evidence + side-request guard (#2202)", assert.equal(session.state.blocks[0].blockId, "main-fold"); }); + test("sibling-wire short multi-part passes never reap main folds (#3844 openai/anthropic/google)", () => { + const textParts = (n: number, tag: string) => + Array.from({ length: n }, (_, j) => ({ type: "text" as const, text: `${tag} part ${j} words` })); + const wires = [ + { name: "openai", raw: 3, msgs: openaiToCore({ messages: Array.from({ length: 3 }, (_, i) => ({ role: "user" as const, content: textParts(5, `oai-${i}`) })) }).msgs }, + { name: "anthropic", raw: 3, msgs: anthropicToCore({ messages: Array.from({ length: 3 }, (_, i) => ({ role: "user" as const, content: textParts(5, `ant-${i}`) })) }).msgs }, + { name: "google", raw: 3, msgs: googleToCore({ contents: Array.from({ length: 3 }, (_, i) => ({ role: "user", parts: Array.from({ length: 5 }, (_, j) => ({ text: `ggl-${i} part ${j} words` })) })) }).msgs }, + ]; + for (const w of wires) { + assert.ok(w.msgs.length >= 10, `${w.name}: fixture must defeat the core-count guard, got ${w.msgs.length}`); + const ctrlIds = Array.from({ length: 12 }, (_, i) => `c${i}`); + const ctrl = blockSession("main-fold", ctrlIds); + const ctrlOriginals = ctrlIds.map((id, i) => msg(id, "user", `control evidence ${i}`)); + reconcileFoldCoverage(ctrl, ctrlOriginals, makeOpts()); + for (let i = 0; i < 4; i++) reconcileFoldCoverage(ctrl, w.msgs, makeOpts()); + assert.equal(ctrl.state.blocks.length, 0, `${w.name}: uncounted expanded short passes feed deletion evidence and reap the main fold (pre-fix behavior)`); + const ids = Array.from({ length: 12 }, (_, i) => `main${i}`); + const session = blockSession("main-fold", ids); + const originals = ids.map((id, i) => msg(id, "user", `main conversation evidence ${i}`)); + reconcileFoldCoverage(session, originals, makeOpts()); + const before = JSON.stringify({ state: session.state, metadata: session.metadata }); + const opts = { ...makeOpts(), inboundMessageCount: w.raw }; + for (let i = 0; i < 4; i++) { + const result = reconcileFoldCoverage(session, w.msgs, opts); + assert.equal(result.kind, "noop", `${w.name}: short wire pass ${i + 1} takes no deletion evidence`); + assert.equal(result.reaped, 0); + assert.equal(JSON.stringify({ state: session.state, metadata: session.metadata }), before); + } + assert.equal(session.state.blocks.length, 1, `${w.name}: four short wire passes retain the main fold`); + const next = reconcileFoldCoverage(session, originals, makeOpts()); + assert.equal(next.missing, 0); + assert.equal(session.state.blocks[0].blockId, "main-fold"); + } + }); + test("short passes do not skew the next pass's alignment", () => { const ids = Array.from({ length: 10 }, (_, i) => `k${i}`); const session = blockSession("blk4", ids);