diff --git a/src/fork-adoption.ts b/src/fork-adoption.ts index 7d2e8be995..46268e2ba5 100644 --- a/src/fork-adoption.ts +++ b/src/fork-adoption.ts @@ -533,7 +533,7 @@ export function maybeAdoptResume(args: { session.state.timestampsHighWaterRefSeq = parent.state.timestampsHighWaterRefSeq; log( "info", - `[resume-inheritance] ${session.id} ${rel}: inherited ${seededRefs} ref(s) up to ${plan.maxRef || "n/a"}${plan.adoptedActive > 0 ? `, ${plan.adoptedActive} block(s) (~${plan.adoptedTokens} tokens)` : ""}; stale citations now resolve to their original messages (#1486)`, + `[resume-inheritance] ${session.id} ${rel}: inherited ${seededRefs} ref(s) up to ${plan.maxRef || "n/a"}${plan.adoptedActive > 0 ? `, ${plan.adoptedActive} block(s) (~${plan.adoptedTokens} tokens)` : ""}${plan.straddled > 0 ? `, ${plan.straddled} active block(s) straddled the fork point (adopted once fully present, #6122)` : ""}; stale citations now resolve to their original messages (#1486)`, ); const wanted = new Set([...Object.keys(plan.refs.byRef), ...citedPlaceholderRefs(incomingMsgs)]); const storeSlice = cloneStoreForRefs(contentStoreOf(parent), wanted); diff --git a/src/util.ts b/src/util.ts index 9f05351d4c..e6bc1c438f 100644 --- a/src/util.ts +++ b/src/util.ts @@ -407,6 +407,20 @@ const CONTEXT_OVERFLOW_PATTERNS: RegExp[] = [ // ("Range of input length should be [1, 1000000]"), with no token or length // subject phrase: unlisted here, the 400 is not an overflow and nothing arms. /range of input length should be \[/i, + // #6122: the native-intercept gate's OWN oversize refusal (src/agent/ + // native-intercept.ts) — bili's definitive "provably over window" signal. + // The field case: the gate refused an unattributed oversized body, the + // relay saw that refusal come back through the client as a 400, and — no + // parseable marker — logged the #4452 CANNOT-engage warning while the + // oversized history was re-sent on every turn (400 forever). The code + // covers all five oversize variants (bare / _fallback / _no_live_state / + // _unattributed / _unattributed_bytes). The compaction refusal + // (bili_gate_dsh_compaction_refused) is a different class (no proxy + // available, body fits fine) and must stay dark. No window number is + // learnable from the prose ("1,000,000 tokens" is a pricing FLOOR, not + // the model's window) — the no-window arm (min of judging window and + // payload estimate) is what engages. + /bili_gate_oversize_refusal/i, ]; function toTokenNumber(s: string): number | undefined { diff --git a/tests/context-overflow.test.ts b/tests/context-overflow.test.ts index 8f00d7c89b..27e308c268 100644 --- a/tests/context-overflow.test.ts +++ b/tests/context-overflow.test.ts @@ -330,6 +330,45 @@ test("empty body is NOT an overflow", () => { assert.equal(inspectContextOverflow(400, "").isOverflow, false); }); +test("#6122: bili's own oversize-gate refusal (bili_gate_oversize_refusal*) is an overflow, window undefined", () => { + // Field case: the native-intercept gate refused an unattributed oversized + // body; the relay saw that refusal come back through the client as a 400 + // and — with no parseable marker — logged the #4452 CANNOT-engage warning + // while the oversized history was re-sent every turn. bili's own refusal + // code IS the definitive over-window proof: it must read as an overflow so + // self-heal (same-request refold / emergency shrink) arms. + const body = JSON.stringify({ + error: { + type: "server_error", + code: "bili_gate_oversize_refusal_unattributed", + message: + "billion-context refused this request locally: it is not attributed to any bili-managed session, and its core text is priced over the model's context window (conservative text pricing — every declared byte counts as text, and the bar is never assumed below the larger of the client-declared window and 1,000,000 tokens) — sending it directly to the provider can only be rejected (every retry re-sends the same oversized history, #4068). ", + retryable: false, + }, + }); + const info = inspectContextOverflow(400, body); + assert.equal(info.isOverflow, true, "the gate's own refusal must read as an overflow"); + assert.equal( + info.window, + undefined, + "the 1,000,000 in the prose is a pricing FLOOR, not the model's window — the no-window arm (min of judging window and payload estimate) must fire", + ); + // Every oversize variant shares the code prefix. + for (const code of [ + "bili_gate_oversize_refusal", + "bili_gate_oversize_refusal_fallback", + "bili_gate_oversize_refusal_no_live_state", + "bili_gate_oversize_refusal_unattributed_bytes", + ]) { + const v = inspectContextOverflow(400, JSON.stringify({ error: { type: "server_error", code } })); + assert.equal(v.isOverflow, true, `${code} must be recognized`); + } + // The compaction refusal is a different class (no proxy available, body + // fits fine) — it must stay dark. + const compaction = inspectContextOverflow(400, JSON.stringify({ error: { type: "server_error", code: "bili_gate_dsh_compaction_refused" } })); + assert.equal(compaction.isOverflow, false, "compaction refusal is not an over-window proof"); +}); + // reserveOutputHeadroom: the effective window handed to the kernel after // reserving the model's per-request output budget. The kernel's nudge/truncate // bands are a fraction of this window, so reserving maxOutput keeps the context diff --git a/tests/e2e/e2e-update-state.test.ts b/tests/e2e/e2e-update-state.test.ts index 03f2b1db22..e31baf7659 100644 --- a/tests/e2e/e2e-update-state.test.ts +++ b/tests/e2e/e2e-update-state.test.ts @@ -299,6 +299,11 @@ test("self-update keeps live compression state: folds survive the OLD→NEW hop envBase.BILI_UPDATE_POLICY_URL = "http://127.0.0.1:1/no-policy.json"; envBase.BILI_PERSIST_DEBOUNCE_MS = "200"; envBase.BILI_SESSIONS_DIR = sessionsDir; + // Pure-sqlite default (5a80e2b8e, #6480) turns the JSON envelope + // writer OFF, but this lane asserts on the on-disk .json session file + // (state survival across the update). Pin the writer ON so the + // envelope this test was written for is still produced. + envBase.BILI_STORAGE_JSON_WRITER = "on"; const port = await freePort(); proxy = spawnProxy(installDir, port, envBase); diff --git a/tests/fork-ccr-adoption.test.ts b/tests/fork-ccr-adoption.test.ts index 5b5bedde45..586d899a02 100644 --- a/tests/fork-ccr-adoption.test.ts +++ b/tests/fork-ccr-adoption.test.ts @@ -38,6 +38,12 @@ import { maybeAdoptForkBlocks } from "../src/fork-adoption.ts"; import { rmrf } from "./tmp-rm.ts"; process.env.BILI_PERSIST = "0"; +// Pure-sqlite default (5a80e2b8e, #6480) turns the JSON envelope writer OFF, +// so the per-session CCR content-store sidecar (.content-store.json) this +// suite asserts on is never written. Pin the writer ON at module load (before +// any SessionStore is constructed) so the companion-envelope coverage this +// test was written for still runs regardless of the storage-mode default. +process.env.BILI_STORAGE_JSON_WRITER = "on"; const LIMIT = 200_000; const META = { protocol: "openai" as const, upstreamOrigin: "http://upstream.example/v1" }; diff --git a/tests/issue4452-markerless-reject.test.ts b/tests/issue4452-markerless-reject.test.ts index d3bd1c9749..04e42b311a 100644 --- a/tests/issue4452-markerless-reject.test.ts +++ b/tests/issue4452-markerless-reject.test.ts @@ -34,6 +34,19 @@ const OPENAI_OVERFLOW_BODY = JSON.stringify({ const CJK_OVERFLOW_BODY = JSON.stringify({ error: { message: "上下文长度超出限制", type: "invalid_request_error" } }); +// #6122 field case: bili's OWN oversize-gate refusal (native-intercept) came +// back through the client as a 400 and was logged as "no parseable overflow +// marker … CANNOT engage" — while it IS the definitive over-window proof. +const GATE_REJECTION_BODY = JSON.stringify({ + error: { + type: "server_error", + code: "bili_gate_oversize_refusal_unattributed", + message: + "billion-context refused this request locally: it is not attributed to any bili-managed session, and its core text is priced over the model's context window (conservative text pricing — every declared byte counts as text, and the bar is never assumed below the larger of the client-declared window and 1,000,000 tokens) — sending it directly to the provider can only be rejected (every retry re-sends the same oversized history, #4068). ", + retryable: false, + }, +}); + test("generic gateway body on 413 is the markerless SIZE verdict and is flagged", () => { // The first-order failure: the marker gate itself says "not overflow". assert.equal(inspectContextOverflow(413, GENERIC_BODY).isOverflow, false); @@ -77,6 +90,18 @@ test("5xx is excluded — those already self-heal via the local estimate (#604)" assert.equal(isMarkerlessOverflowReject(507, GENERIC_BODY), false); }); +test("#6122: bili's own oversize-gate refusal is NOT markerless — self-heal engages from it", () => { + // The gate body IS the marker: it is bili's own definitive over-window + // proof. Reading it as an opaque gateway rewrite (the #4452 warn) is what + // left the #6122 child session 400-looping — the CANNOT-engage warning + // fires on its own refusal. + assert.equal(inspectContextOverflow(400, GATE_REJECTION_BODY).isOverflow, true); + assert.equal(isMarkerlessOverflowReject(400, GATE_REJECTION_BODY), false); + // …and the prose's "1,000,000 tokens" is a pricing floor, not a learnable + // model window — the no-window arm must be what fires, not a 1M learning. + assert.equal(inspectContextOverflow(400, GATE_REJECTION_BODY).window, undefined); +}); + test("non-overflow 4xx and success codes are excluded (no false alarm on auth/malformed)", () => { assert.equal(isMarkerlessOverflowReject(200, GENERIC_BODY), false); assert.equal(isMarkerlessOverflowReject(401, GENERIC_BODY), false); diff --git a/tests/issue6122-fork-straddled-log.test.ts b/tests/issue6122-fork-straddled-log.test.ts new file mode 100644 index 0000000000..bcfe682273 --- /dev/null +++ b/tests/issue6122-fork-straddled-log.test.ts @@ -0,0 +1,110 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { maybeAdoptResume, incomingCoreMessages } from "../src/fork-adoption.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { type Session } from "../src/session.ts"; + +process.env.NODE_ENV = "test"; + +// #6122: a DSH fork that inherited 1 of 5 active blocks logged only the +// adopted count — the 4 straddling blocks were invisible in the log, so the +// field triage had to rebuild the ledger by hand to see what would (and when) +// still be adopted. The inherited line must name the straddled count, the +// way the lineage-reconcile line already does (#2807). + +function fixtures(straddle: boolean) { + const parsed = { + model: "m", + max_tokens: 1, + messages: [ + { role: "user", content: "hello world 6122" }, + { role: "assistant", content: "hi there 6122" }, + ], + }; + const msgs = incomingCoreMessages("anthropic", parsed)!; + const byRaw: Record = {}; + const byRef: Record = {}; + const tsStamps: Record = {}; + msgs.forEach((m, i) => { + const ref = `m${String(i + 1).padStart(5, "0")}`; + byRaw[m.id] = ref; + byRef[ref] = m.id; + tsStamps[m.id] = 1730000000 + i; + }); + // b1 is fully present in the incoming core; b2 names a raw id the child + // never sent (a message the child's replay omitted) — it straddles. + const blocks = [ + { blockId: "b1", active: true, effectiveMessageIds: [msgs[0].id], directBlockIds: [], compressedTokens: 42 }, + ]; + if (straddle) { + blocks.push({ + blockId: "b2", + active: true, + effectiveMessageIds: [msgs[1].id, "raw-id-the-child-never-sent"], + directBlockIds: [], + compressedTokens: 13, + }); + } + const meta = { protocol: "anthropic", upstreamOrigin: "test" }; + const parent = { + id: "parent-6122", + meta, + blockContents: new Map(), + state: { blocks, tokenSnapshot: {}, nextBlockId: blocks.length + 1, nextRunId: 1, messageRefs: { byRaw, byRef }, tsStamps }, + } as unknown as Session; + const child = { + id: "child-6122", + meta, + state: { + blocks: [] as unknown[], + tokenSnapshot: {} as Record, + nextBlockId: 1, + nextRunId: 1, + messageRefs: { byRaw: {} as Record, byRef: {} as Record }, + }, + blockContents: new Map(), + } as unknown as Session; + return { parsed, parent, child }; +} + +test("#6122: the inherited line names the straddled active blocks", () => { + _setStoreForTest(new SessionStore({ enabled: false })); + const { parsed, parent, child } = fixtures(true); + const lines: string[] = []; + const outcome = maybeAdoptResume({ + session: child, + parent, + sharedDepth: 0, + protocol: "anthropic", + parsed, + upstreamOrigin: "test", + blocksEnabled: true, + log: (_level, msg) => { lines.push(msg); }, + }); + const adoptLine = lines.find((l) => l.includes("inherited")); + assert.ok(adoptLine, "the inherited line was logged"); + assert.equal(outcome.adoptedActive, 1, "only the fully-present block is adopted"); + assert.ok( + adoptLine.includes("1 active block(s) straddled the fork point"), + `the straddled count must appear on the inherited line, got: ${adoptLine}`, + ); +}); + +test("#6122: nothing straddling → the line keeps its pre-#6122 wording", () => { + _setStoreForTest(new SessionStore({ enabled: false })); + const { parsed, parent, child } = fixtures(false); + const lines: string[] = []; + maybeAdoptResume({ + session: child, + parent, + sharedDepth: 0, + protocol: "anthropic", + parsed, + upstreamOrigin: "test", + blocksEnabled: true, + log: (_level, msg) => { lines.push(msg); }, + }); + const adoptLine = lines.find((l) => l.includes("inherited")); + assert.ok(adoptLine, "the inherited line was logged"); + assert.ok(!adoptLine.includes("straddled"), `no straddle, no clause — got: ${adoptLine}`); +});