From 3d39ba601ccedee15ac23f915859c8dd207939ad Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Tue, 13 Oct 2026 21:23:50 +0800 Subject: [PATCH 1/8] =?UTF-8?q?fix(#3013,#3022):=20refusal=20is=20terminal?= =?UTF-8?q?=20=E2=80=94=20bili=5Ffinal=20marker=20+=20storm-visible=20logg?= =?UTF-8?q?ing=20(A1+A3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Field report #3013: one session burned 5069 refused dsh compaction calls (5038 retries, median 328ms, 0 outbound) while the user watched a stuck '生成中'. Two halves: A1 — the refusal body now carries bili_final:true (top level on both protocol shapes; inside error on the openai envelope where retryable lives). retryable:false did not stop the loop: dsh's retry pressure comes from its own raw ledger (#2490), which nothing on bili's side can shrink, so the same call fails identically forever. bili_final is the stable machine-readable contract for dsh upstream to latch (consumption is a cross-project follow-up). REFUSAL_MESSAGE now states the terminal semantics and the levers (bili compress / #2474 recipe / #2028 opt-in; dropped the stale web-UI mention deleted by #3042). A3 — logging. Pipeline lane: after the once-warn, every 100th refusal logs an info with the running count, the #2490 mechanism, and the levers (previously: 5051 silent refusals, zero log lines, user blind). Passthrough lane: was warn-per-refusal (the spam direction) — now warns on the process's first refusal, then the same 1-per-100 cadence. Tests: tests/issue3013-refusal-terminal.test.ts (A1 body shapes on both protocols incl. error.bili_final; A3 storm cadence on both lanes via setLogCapture with log:true rigs — request-scoped log is gated on opts.log, log:false rigs made guard lines no-ops). Red-green proven. Family regressions green (dsh guard/2028/2709/2658/2432/2451, claude guard #2612, codex 2451, archives): 97 tests. (cherry picked from commit 34b6c7e7c682abee95b0f3e05dac99ee3ebfb069) --- src/server/dsh-compaction-guard.ts | 26 +++- src/server/handle.ts | 20 ++- tests/issue3013-refusal-terminal.test.ts | 186 +++++++++++++++++++++++ 3 files changed, 228 insertions(+), 4 deletions(-) create mode 100644 tests/issue3013-refusal-terminal.test.ts diff --git a/src/server/dsh-compaction-guard.ts b/src/server/dsh-compaction-guard.ts index 00b18db2b2..1a6162ab6a 100644 --- a/src/server/dsh-compaction-guard.ts +++ b/src/server/dsh-compaction-guard.ts @@ -282,9 +282,20 @@ const REFUSAL_MESSAGE = `dsh native compaction call refused by billion-context: bili owns compression on this lane (#1729, cf. #1772/#1206). ` + `A landed dsh compaction checkpoint durably shadows the raw conversation history, which destroys the proxy's compression substrate. ` + `No action is needed: your conversation is intact and bili compresses automatically as turns grow too large — if the session is currently over its window, it starts folding on the next turn (#3453). ` + - `This refusal is unconditional — the former allowDshCompaction opt-in was removed (#3651, superseding #2028); ` + + `This refusal is unconditional and terminal (bili_final) — the same call will be refused every time, so stop retrying (#3013: one session burned 5069 refused retries); the former allowDshCompaction opt-in was removed (#3651, superseding #2028); ` + `to keep dsh-side checkpoints out entirely, apply host-side suppression instead (#3354/#2474).`; +/** #3013/#3022 (A1): the refusal is a DECISION, not a transient error — dsh's + * retry loop is driven by its own raw pressure ledger which nothing on bili's + * side can shrink (#2490), so every retry of the same call fails identically. + * `bili_final: true` gives clients an unambiguous machine-readable terminal + * marker to latch and stop retrying (consumption needs dsh-upstream + * coordination; `retryable:false` alone did not stop the field-reported + * 5038-retry storm). Present at the top level of both protocol shapes and, + * for the openai error envelope (where `retryable` lives), inside `error` + * too. */ +const BILI_FINAL_FIELD = "bili_final" as const; + /** Protocol-shaped refusal body (mirrors the #554 side-request guard shape so * clients render it natively). 422: a policy refusal of this one request — not * 401/403 (clients classify those as a credential failure and may disable the @@ -294,18 +305,29 @@ export function dshCompactionRefusal(protocol: string): Refusal { if (protocol === "anthropic") { return { status: 422, - body: { type: "error", error: { type: "invalid_request_error", message: REFUSAL_MESSAGE } }, + body: { type: "error", [BILI_FINAL_FIELD]: true, error: { type: "invalid_request_error", message: REFUSAL_MESSAGE } }, }; } return { status: 422, body: { + [BILI_FINAL_FIELD]: true, error: { type: "server_error", code: "dsh_compaction_refused", message: REFUSAL_MESSAGE, retryable: false, + [BILI_FINAL_FIELD]: true, }, }, }; } + +/** #3013/#3022 (A3): cadence for the pipeline-lane storm log. The first + * refusal warns once (once-warn dedup on session.metadata), then refusals are + * silent unless this many more arrive — the field-reported storm re-fired + * every ~328ms for hours with ZERO log lines after the first, leaving the + * user staring at a stuck "生成中" with no clue (#3013). Same cadence caps the + * passthrough lane's per-refusal warn so an agentless storm cannot spam the + * log either. Constant, not config (#2155 precedent). */ +export const DSH_REFUSAL_STORM_LOG_EVERY = 100; diff --git a/src/server/handle.ts b/src/server/handle.ts index c962458203..79d088af05 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -53,7 +53,7 @@ import { extractConversationId, isV1BackgroundSideRequest, isV1InternalRequest, import { anthropicBetaContextWindow, BILI_HOP_HEADER, capRegistryWindowByStandard, expandedContextSuffixWindow, launcherContextWindow, launcherMaxOutput, windowSourceLogged } from "./context-window.js"; import { NO_IDENTITY_MESSAGE, safeSessionId } from "./headers.js"; import { demoteGate, hasLeakedBiliToolsOnly, isDshAutoReviewRequest, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; -import { DSH_COMPACTION_SHAPE_MSGS, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js"; +import { DSH_COMPACTION_SHAPE_MSGS, DSH_REFUSAL_STORM_LOG_EVERY, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js"; import { buildDshCompactionForgeBody, renderDshAuthoredCheckpoint } from "./dsh-authored-compaction.js"; import { artifactSeedHit, detectAcpArtifacts } from "./chain-artifacts.js"; import { piSubagentChannelFallback } from "./pi-subagent-channel.js"; @@ -446,7 +446,15 @@ async function handleRequest( if (protocol !== null && req.method === "POST" && bodyBuffer.length > 0 && await detectPassthroughDshCompactionCall(protocol, bodyBuffer, headerValue(req, "content-encoding"), async (enc, buf) => (await decodeRequestBody(enc, buf, knobLimitsDecodedBytes(), { signal: decodingSignal, onBytes: (bytes) => admission.observe(bytes) })).body)) { dshPassthroughGuardStats.refusals += 1; - log("warn", `[passthrough] dsh native compaction call identified on the unattributed passthrough lane (#1117/#2709) — REFUSED, not forwarded: bili owns compression on this lane; a landed dsh checkpoint would durably shadow the raw history (#1729, cf. #1772/#1206). No session exists on this lane, so this log line (not the #2490 ledger) is the record; passthrough refusals so far: ${dshPassthroughGuardStats.refusals}`); + // #3013/#3022 (A3): first refusal warns; then every + // DSH_REFUSAL_STORM_LOG_EVERY-th refusal logs an info so an + // agentless retry storm stays visible without spamming (the + // field-reported storm re-fired every ~328ms — per-refusal warn + // would have written thousands of lines). This log line, not the + // #2490 ledger, is the only record on this lane. + if (dshPassthroughGuardStats.refusals === 1 || dshPassthroughGuardStats.refusals % DSH_REFUSAL_STORM_LOG_EVERY === 0) { + log(dshPassthroughGuardStats.refusals === 1 ? "warn" : "info", `[passthrough] dsh native compaction call identified on the unattributed passthrough lane (#1117/#2709) — REFUSED, not forwarded: bili owns compression on this lane; a landed dsh checkpoint would durably shadow the raw history (#1729, cf. #1772/#1206). No session exists on this lane, so this log line (not the #2490 ledger) is the record; passthrough refusals so far: ${dshPassthroughGuardStats.refusals}` + (dshPassthroughGuardStats.refusals === 1 ? "" : ` — retrying will keep failing (terminal, bili_final; #3013): disable dsh auto-compaction (#2474)`)); + } const refusal = dshCompactionRefusal(protocol); if (!res.headersSent && !res.writableEnded && !res.destroyed) { res.writeHead(refusal.status, { "content-type": "application/json" }); @@ -1967,6 +1975,14 @@ async function handleRequest( return; } session.metadata["dshCompactionRefusals"] = (typeof session.metadata["dshCompactionRefusals"] === "number" ? session.metadata["dshCompactionRefusals"] as number : 0) + 1; + // #3013/#3022 (A3): after the once-warn above, refusals were fully + // silent — the field storm burned 5051 refusals with zero further + // log lines while the user saw a stuck "生成中". Every + // DSH_REFUSAL_STORM_LOG_EVERY-th refusal re-surfaces with the + // running count and the actual levers. + if ((session.metadata["dshCompactionRefusals"] as number) % DSH_REFUSAL_STORM_LOG_EVERY === 0) { + log("info", `[${session.id}] dsh native compaction refusal #${session.metadata["dshCompactionRefusals"]} on this session (raw-ledger high-water ${(session.metadata["dshCompactionRefusedBytes"] as number)} bytes) — dsh keeps retrying because its own raw pressure ledger never shrinks (#2490), and every retry of this call will be refused (terminal, #3013). Disable dsh auto-compaction (profile recipe #2474) or manage context via bili's compress. If bili's own compression looks idle while this repeats, the nudge channel may be dead — report it (#3004/#3013).`); + } markDirty(session); const refusal = dshCompactionRefusal(protocol); if (!res.headersSent && !res.writableEnded && !res.destroyed) { diff --git a/tests/issue3013-refusal-terminal.test.ts b/tests/issue3013-refusal-terminal.test.ts new file mode 100644 index 0000000000..0d60e52f5b --- /dev/null +++ b/tests/issue3013-refusal-terminal.test.ts @@ -0,0 +1,186 @@ +/** + * #3013 / #3022 (A1 + A3): the dsh compaction refusal storm. + * + * Field report: one long session burned 5069 refused compaction calls + * (5038 retries after the first, median interval 328ms, 0 outbound) because + * (a) the refusal body carried no machine-readable terminal marker a client + * could latch (`retryable:false` did not stop dsh's own pressure-ledger + * driven retry loop), and (b) after the once-warn, every further refusal was + * silent — the user stared at a stuck "生成中" for hours with zero log lines. + * + * This pins the two halves of the fix: + * - A1: every refusal body carries `bili_final: true` (top level on both + * protocol shapes; inside `error` on the openai envelope where + * `retryable` lives). Consumption by dsh upstream is a cross-project + * follow-up — bili's side of the contract is stable here. + * - A3: the pipeline lane logs an info every DSH_REFUSAL_STORM_LOG_EVERY-th + * refusal with the running count and the levers; the passthrough lane + * (previously warn-per-refusal — the spam direction) warns on the first + * refusal in the process and then follows the same cadence. + */ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import type { AddressInfo } from "node:net"; + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { DSH_COMPACTION_INSTRUCTION_PREFIX, DSH_REFUSAL_STORM_LOG_EVERY, dshCompactionRefusal, dshPassthroughGuardStats } from "../src/server/dsh-compaction-guard.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { setLogCapture } from "../src/logger.ts"; + +const INSTRUCTION = `${DSH_COMPACTION_INSTRUCTION_PREFIX} Summarize the conversation above.`; + +function startUpstream(captured: string[]): Promise { + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c) => chunks.push(c)); + req.on("end", () => { + captured.push(Buffer.concat(chunks).toString("utf8")); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ choices: [{ message: { role: "assistant", content: "ok" } }] })); + }); + }); + upstream.listen(0, "127.0.0.1"); + return once(upstream, "listening").then(() => upstream); +} + +async function startBili(upstreamPort: number): Promise<{ port: number; close: () => Promise }> { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: 400_000 } } } }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + promptCache: { routing: "auto" }, + compress: { injectTool: false, injectNudge: false }, + sessionHeader: "x-acp-session", + log: true, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + return { + port: (proxy.address() as AddressInfo).port, + close: async () => { + proxy.close(); + await once(proxy, "close"); + }, + }; +} + +test("A1: refusal bodies carry the bili_final terminal marker on both protocol shapes", () => { + const anthropic = dshCompactionRefusal("anthropic"); + const body = anthropic.body as Record; + assert.equal(body.bili_final, true, "anthropic shape: top-level bili_final"); + assert.equal((body.error as Record).type, "invalid_request_error"); + + const openai = dshCompactionRefusal("openai"); + const ob = openai.body as Record; + assert.equal(ob.bili_final, true, "openai shape: top-level bili_final"); + const err = ob.error as Record; + assert.equal(err.retryable, false); + assert.equal(err.bili_final, true, "openai shape: error.bili_final (where retryable lives)"); + assert.equal(err.code, "dsh_compaction_refused"); + + // the message must state the terminal semantics + the levers (#3013) + assert.match(String(err.message), /terminal for the session/); + assert.match(String(err.message), /stop retrying/); + assert.match(String(err.message), /#2474/); + assert.match(String(err.message), /allowDshCompaction/); +}); + +test("A3: pipeline storm — first refusal warns once, then every DSH_REFUSAL_STORM_LOG_EVERY-th logs an info with the levers", async () => { + const captured: string[] = []; + const upstream = await startUpstream(captured); + const upstreamPort = (upstream.address() as AddressInfo).port; + const bili = await startBili(upstreamPort); + + const lines: { level: string; msg: string }[] = []; + setLogCapture((level, msg) => lines.push({ level, msg })); + try { + let sawTerminal = false; + for (let i = 0; i < DSH_REFUSAL_STORM_LOG_EVERY; i++) { + const resp = await fetch(`http://127.0.0.1:${bili.port}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "s-3013" }, + body: JSON.stringify({ model: "gpt-test", messages: [{ role: "user", content: INSTRUCTION }] }), + }); + assert.equal(resp.status, 422, `refusal ${i + 1} must stay 422`); + const parsed = JSON.parse(await resp.text()) as Record; + assert.equal(parsed.bili_final, true, `refusal ${i + 1} carries bili_final on the wire`); + sawTerminal = true; + } + assert.ok(sawTerminal); + + assert.equal(captured.length, 0, "no refusal may ever reach the upstream"); + + const warns = lines.filter((l) => l.level === "warn" && l.msg.includes("dsh native compaction call identified")); + assert.equal(warns.length, 1, "exactly one identification warn (the once-warn)"); + + const storm = lines.filter((l) => l.level === "info" && l.msg.includes("dsh native compaction refusal #")); + assert.equal(storm.length, 1, `exactly one storm info in ${DSH_REFUSAL_STORM_LOG_EVERY} refusals`); + assert.match(storm[0].msg, new RegExp(`refusal #${DSH_REFUSAL_STORM_LOG_EVERY}\\b`)); + assert.match(storm[0].msg, /never shrinks \(#2490\)/); + assert.match(storm[0].msg, /#2474/); + assert.match(storm[0].msg, /allowDshCompaction/); + assert.match(storm[0].msg, /#3013/); + } finally { + setLogCapture(null); + await bili.close(); + upstream.close(); + await once(upstream, "close"); + } +}); + +test("A3: passthrough storm — first-in-process warns, then the same cadence (no per-refusal spam)", async () => { + const captured: string[] = []; + const upstream = await startUpstream(captured); + const upstreamPort = (upstream.address() as AddressInfo).port; + const bili = await startBili(upstreamPort); + + const lines: { level: string; msg: string }[] = []; + setLogCapture((level, msg) => lines.push({ level, msg })); + const before = dshPassthroughGuardStats.refusals; + // any 100 consecutive refusals cross exactly one multiple of + // DSH_REFUSAL_STORM_LOG_EVERY; the warn fires only when this process's + // first passthrough refusal happens to land inside the loop. + const firstInLoop = before === 0; + try { + for (let i = 0; i < DSH_REFUSAL_STORM_LOG_EVERY; i++) { + const resp = await fetch(`http://127.0.0.1:${bili.port}/bili/http://127.0.0.1:${upstreamPort}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-passthrough": "1" }, + body: JSON.stringify({ model: "gpt-test", messages: [{ role: "user", content: INSTRUCTION }] }), + }); + assert.equal(resp.status, 422); + assert.equal((JSON.parse(await resp.text()) as Record).bili_final, true, "passthrough refusal also carries bili_final"); + } + assert.equal(captured.length, 0, "nothing forwarded"); + + const lane = lines.filter((l) => l.msg.includes("[passthrough] dsh native compaction call identified")); + const warns = lane.filter((l) => l.level === "warn"); + const infos = lane.filter((l) => l.level === "info"); + if (firstInLoop) { + assert.equal(warns.length, 1, "process-first refusal warns"); + assert.equal(infos.length, 1, "and the 100th logs one cadence info"); + assert.match(infos[0].msg, /terminal; #3013/); + } else { + assert.equal(infos.length, 1, "exactly one cadence info per 100 consecutive refusals"); + } + assert.ok(lane.length <= 2, `no per-refusal spam: ${lane.length} lines for ${DSH_REFUSAL_STORM_LOG_EVERY} refusals`); + } finally { + setLogCapture(null); + await bili.close(); + upstream.close(); + await once(upstream, "close"); + } +}); From 46c5927dee7218aa70623068dbe12041ed63eb78 Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 02:06:02 +0800 Subject: [PATCH 2/8] fix: enforce the anthropic-wire-only contract across the incremental-turns path (#3433) dsh-native's maybeIncrementalNative claimed 'anthropic wire only' but only checked messages-array + model-string, so OpenAI chat-completions bodies (assistant.tool_calls / role:"tool") were converted onto a lane that ingests with anthropicToCore: tool calls silently dropped, role:"tool" mis-mapped into user text, content:null bodies throwing mid-apply. pi/pi-native shared the same gap in the factory hook (pi serves openai/responses/google provider APIs). - wire the existing wireMessagesLookAnthropic into the dsh-native and pi entry points (omp/opencode parity; restores the PLUGIN.md-documented contract) - enforce the same predicate at the lane boundary: 400 for non-anthropic message shapes instead of trusting every host to pre-filter (mirror of the client helper, keep-in-sync marked) - regression tests: dsh OpenAI shape rides full resend (+ anthropic control), pi OpenAI shape not replaced under a live lane (+ anthropic control), lane 400s role:"tool"/tool_calls and still accepts a valid seed afterwards (cherry picked from commit 6f9956cdc35547ce282d4dadedea8f4d46e085c2) --- src/agent/dsh-native.ts | 4 +- src/agent/pi.ts | 3 +- src/incremental-lane.ts | 14 ++ tests/dsh-native.test.ts | 48 +++++++ tests/incremental-lane.test.ts | 30 +++++ tests/issue3433-pi-incremental-guard.test.ts | 133 +++++++++++++++++++ 6 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 tests/issue3433-pi-incremental-guard.test.ts diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index ec1a6f0f6c..18ba823a63 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -62,7 +62,7 @@ import { markNativeHost, nativeAttachOrigin, nativeBootstrapGate, nativeProxyScr import { installNativeFetchIntercept, liveStateSlotEffect, noteRoutedOrigin, observeRoutedOrigin, routedBiliModelUrl, type GateVerdict, type NativeInterceptState } from "./native-intercept.js"; import { asciiHeaderValue, fetchManifest, fetchProxyVersion, fetchStatus, fetchStatusLatest, forwardTool, reportAttributionHealth, reportRuntimeInfo, waitForProxyVersion, type ManifestTool } from "./shared.js"; import { createForkAdopter, sideShapedBody } from "./fork-adopt.js"; -import { clearIncrementalProbe, createPrepareDirectSend, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, createPrepareDirectSend, INCREMENTAL_TURN_MARKER_HEADER, INCREMENTAL_TURN_ID_HEADER, newIncrementalTurnId, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, rememberOriginalBody, sweepStale, wireMessagesLookAnthropic, type IncrementalWireMessage } from "./incremental-client.js"; import { declareDshCompactionActive, detectCompactionOverlayGap, dshCompactionGateTelemetry } from "./dsh-compaction-disable.js"; import { maskUrlForDiagnostics } from "../log-mask.js"; import { mediaBytesOfWireBody } from "./media-bytes.js"; @@ -2342,7 +2342,7 @@ export function apply(ctx: PluginContext): void { } catch { return; } - if (!Array.isArray(parsed.messages) || typeof parsed.model !== "string") return; // anthropic wire only + if (typeof parsed.model !== "string" || !wireMessagesLookAnthropic(parsed.messages)) return; // anthropic wire only — OpenAI tool shapes (tool_calls / role:"tool") resend full (#3433) const base = register.base ?? state.origin; if (base === undefined) return; if (!(await probeIncrementalTurns(base))) return; diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 2182f567e5..10ebac01c6 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -16,7 +16,7 @@ import { disposeSubagentSelfReg, selfRegisterForSession, type SubagentSelfRegSta import { isModelApiUrl, nativeInterceptInstalled } from "./native-intercept.js"; import { wirePiSubagents } from "./pi-subagents.js"; import { wireToolGuardrails } from "./tool-guardrails.js"; -import { clearIncrementalProbe, noteHostMsgIdHint, INCREMENTAL_TURN_MARKER_HEADER, incrementalEnvelopeConversationId, incrementalWireHash, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, pullIncrementalReceipt, sweepStale, type HostMsgIdHintEntry, type IncrementalWireMessage } from "./incremental-client.js"; +import { clearIncrementalProbe, noteHostMsgIdHint, INCREMENTAL_TURN_MARKER_HEADER, incrementalEnvelopeConversationId, incrementalWireHash, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, pullIncrementalReceipt, sweepStale, wireMessagesLookAnthropic, type HostMsgIdHintEntry, type IncrementalWireMessage } from "./incremental-client.js"; import { laneMsgIdsForWire, lanePinnableContent } from "./pi-msgids.js"; import { validateMsgIds } from "../msgid-pin.js"; import type { DirectSendPrep } from "./native-intercept.js"; @@ -1399,6 +1399,7 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM if (state.toolsReady !== true) return undefined; // no marker stamped → must not replace const payload = event.payload as { messages?: unknown; system?: unknown } | undefined; if (payload === null || typeof payload !== "object" || !Array.isArray((payload as { messages?: unknown }).messages)) return undefined; + if (!wireMessagesLookAnthropic(payload.messages)) return undefined; // anthropic wire only — OpenAI tool shapes resend full (#3433) const sid = sessionIdOf(ctx); if (sid === undefined || sid.length === 0) return undefined; let proxyBase = proxyBaseForCtx(ctx); diff --git a/src/incremental-lane.ts b/src/incremental-lane.ts index 7843d51a02..56a97cb5e1 100644 --- a/src/incremental-lane.ts +++ b/src/incremental-lane.ts @@ -94,6 +94,19 @@ export function incrementalTurnBodyShape(raw: Buffer): boolean { && body.model === undefined; } +/** Lane-side mirror of wireMessagesLookAnthropic (src/agent/incremental-client.ts — keep the two in sync): the lane ingests with anthropicToCore and re-emits with coreToAnthropic, so only the anthropic wire shape is convertible. OpenAI tool shapes (assistant.tool_calls, role:"tool"/"function", tool_call_id) would be silently dropped or mis-mapped into user text — reject loudly at the boundary instead of trusting every host to pre-filter (#3433). Empty lists PASS here: a pure sticky-system update carries no messages (the client-side predicate rejects them; the empty-without-system case is already a 400 below). */ +function laneMessagesLookAnthropic(messages: unknown): boolean { + if (!Array.isArray(messages)) return false; + for (const m of messages) { + if (m === null || typeof m !== "object") return false; + const msg = m as { role?: unknown; content?: unknown; tool_calls?: unknown; tool_call_id?: unknown }; + if (typeof msg.role !== "string" || msg.role === "tool" || msg.role === "function") return false; + if (msg.tool_calls !== undefined || msg.tool_call_id !== undefined) return false; + if (typeof msg.content !== "string" && !Array.isArray(msg.content)) return false; + } + return true; +} + const EXT_KEY_SYSTEM = "inc:system"; const EXT_KEY_CACHE_MARKS = "inc:cache-marks"; const EXT_KEY_BASE_SEQ = "inc:base-seq"; @@ -581,6 +594,7 @@ export async function handleIncrementalTurn( if (typeof forwarded.model !== "string" || forwarded.model.length === 0) return jsonReply(res, 400, { error: "request.model is required" }); if (!Array.isArray(payload.messages)) return jsonReply(res, 400, { error: "messages must be an array (seed: full history; delta: everything after sinceSeq; empty allowed for a pure sticky-system update)" }); if (payload.messages.length === 0 && payload.system === undefined) return jsonReply(res, 400, { error: "messages must be a non-empty array (seed: full history; delta: everything after sinceSeq)" }); + if (!laneMessagesLookAnthropic(payload.messages)) return jsonReply(res, 400, { error: 'messages must be anthropic wire shape — OpenAI tool shapes (assistant.tool_calls, role:"tool", tool_call_id) cannot ride the lane and stay on full resend', conversationId }); const deltaWire = payload.messages; // #4491: optional client msgids parallel to messages (absent = legacy // content-derived identities). Identity-poisoning shapes (bad entry, diff --git a/tests/dsh-native.test.ts b/tests/dsh-native.test.ts index 284fc5463a..a2619d8b71 100644 --- a/tests/dsh-native.test.ts +++ b/tests/dsh-native.test.ts @@ -87,6 +87,54 @@ test("#3370: dsh incremental conversion is atomic (marker + body on the same ini } }); +// #3433: OpenAI wire shapes (assistant.tool_calls / role:"tool") must NOT be +// converted onto the incremental lane — the lane ingests with anthropicToCore +// and would silently drop the tool calls. They stay on full resend, exactly +// like omp/opencode do behind wireMessagesLookAnthropic. +test("#3433: dsh OpenAI tool shapes ride full resend; anthropic shapes still convert", async () => { + const proxy = await startMockProxy([], undefined, undefined, undefined, true); + const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-inc3433-")); + const stateHome = fs.mkdtempSync(path.join(os.tmpdir(), "bili-dsh-inc3433-state-")); + try { + await withEnv({ DSH_HOME: home, BILLION_CONTEXT_PROXY: proxy.origin, XDG_STATE_HOME: stateHome, BILI_PROVIDER_REWRITES: undefined, BILI_NATIVE_DSH: undefined, BILLION_CONTEXT_PLUGIN: undefined }, async () => { + _resetRegisterForTest(proxy.origin); + __resetIncrementalClientForTests(); + const ctx = mockCtx(); + ctx.setInitiator({ session: { id: "dsh-inc-3433" } }); + apply(ctx); + await waitFor(() => ctx.registeredTools.length === 1, "manifest tool registration"); + await _stateToolsReadyForTest(); + const beforeSend = _stateBeforeSendForTest(); + assert.ok(beforeSend, "conversion seam armed"); + const url = `${proxy.origin}/v1/chat/completions`; + + // OpenAI chat-completions shape: assistant.tool_calls + role:"tool" + const openaiMsgs = [ + { role: "user", content: "list the files" }, + { role: "assistant", content: null, tool_calls: [{ id: "call_1", type: "function", function: { name: "ls", arguments: "{}" } }] }, + { role: "tool", tool_call_id: "call_1", content: "a.txt b.txt" }, + { role: "user", content: "now read a.txt" }, + ]; + const original = JSON.stringify({ model: "gpt-test", max_tokens: 64, messages: openaiMsgs }); + const init: RequestInit = { method: "POST", body: original }; + await beforeSend(url, init); + assert.equal(init.body, original, "OpenAI tool shape rides full resend (body untouched)"); + assert.equal(new Headers(init.headers ?? {}).get("x-bili-plugin-turn"), null, "no marker on the untouched body"); + + // control: the anthropic wire shape still converts (guard does not over-block) + const init2: RequestInit = { method: "POST", body: JSON.stringify({ model: "claude-test", max_tokens: 64, messages: [{ role: "user", content: [{ type: "text", text: "hello" }] }] }) }; + await beforeSend(url, init2); + const body2 = JSON.parse(init2.body as string) as { conversationId?: string }; + assert.equal(body2.conversationId, "dsh-inc-3433", "anthropic shape still converts to a lane turn"); + assert.equal(new Headers(init2.headers).get("x-bili-plugin-turn"), "1", "marker stamped on the anthropic turn"); + }); + } finally { + proxy.close(); + rmrf(home); + rmrf(stateHome); + } +}); + import { rmrf } from "./tmp-rm.ts"; // #1797: drain ALL in-flight attach/recovery chains after each test — defense-in-depth diff --git a/tests/incremental-lane.test.ts b/tests/incremental-lane.test.ts index 080a6e4f57..294af1b356 100644 --- a/tests/incremental-lane.test.ts +++ b/tests/incremental-lane.test.ts @@ -554,3 +554,33 @@ test("incrementalTurnBodyShape accepts the incremental payload and rejects anthr const badSeq = Buffer.from(JSON.stringify({ conversationId: "c1", sinceSeq: "3", messages: [] })); assert.equal(incrementalTurnBodyShape(badSeq), false, "sinceSeq must be an integer"); }); + +// #3433: the lane ingests with anthropicToCore and re-emits with coreToAnthropic, +// so non-anthropic message shapes must be rejected LOUDLY at the boundary — +// converting them would silently drop assistant.tool_calls or mis-map +// role:"tool" into user text. The client-side mirror is +// wireMessagesLookAnthropic; this is the trust-boundary enforcement. +test("#3433: lane rejects non-anthropic message shapes with 400, never half-converts them", async () => { + const h = await harness("full"); + try { + const toolRole = await h.turn({ sinceSeq: 0, resync: true, messages: [ + { role: "user", content: "hi" }, + { role: "tool", tool_call_id: "c1", content: "out" }, + ] }); + assert.equal(toolRole.status, 400); + assert.match((toolRole.body as { error?: string }).error ?? "", /anthropic wire shape/); + + const toolCalls = await h.turn({ sinceSeq: 0, resync: true, messages: [ + { role: "assistant", content: null, tool_calls: [{ id: "c1", type: "function", function: { name: "f", arguments: "{}" } }] }, + ] }); + assert.equal(toolCalls.status, 400); + assert.match((toolCalls.body as { error?: string }).error ?? "", /anthropic wire shape/); + + // the rejections applied nothing — a valid anthropic seed still lands + const seed = await h.turn({ sinceSeq: 0, resync: true, messages: [{ role: "user", content: "hello" }] }); + assert.equal(seed.status, 200); + assert.equal(seed.headers.get("x-bili-applied-seq"), "1"); + } finally { + await h.close(); + } +}); diff --git a/tests/issue3433-pi-incremental-guard.test.ts b/tests/issue3433-pi-incremental-guard.test.ts new file mode 100644 index 0000000000..c97e950f85 --- /dev/null +++ b/tests/issue3433-pi-incremental-guard.test.ts @@ -0,0 +1,133 @@ +// #3433: pi (and pi-native, which inherits this wiring by design) must apply +// the same anthropic-wire guard as omp/opencode before converting an outgoing +// payload onto the incremental lane — pi supports openai/responses/google +// provider APIs, so OpenAI tool shapes (assistant.tool_calls / role:"tool") +// reach before_provider_request and would be silently dropped by the lane's +// anthropicToCore ingest. They stay on full resend. +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test, { after } from "node:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; +import { createBiliPlugin } from "../src/agent/pi.ts"; +import { __resetIncrementalClientForTests } from "../src/agent/incremental-client.js"; + +process.env.NODE_ENV = "test"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-incr3433-")); +after(() => rmSync(testRoot, { recursive: true, force: true })); + +type FakePi = { events: Map unknown>; tools: Array<{ name: string }>; on: (event: string, handler: (event: never, ctx: never) => unknown) => void; registerTool: (tool: { name: string }) => void }; + +function makeFakePi(): FakePi { + const events = new Map unknown>(); + const tools: Array<{ name: string }> = []; + return { + events, + tools, + on: (event, handler) => { + const h = handler as (event: unknown, ctx: unknown) => unknown; + const prev = events.get(event); + events.set(event, prev ? ((e, c) => { prev(e, c); return h(e, c); }) : h); + }, + registerTool: (tool) => { + const i = tools.findIndex((t) => t.name === tool.name); + if (i >= 0) tools[i] = tool; + else tools.push(tool); + }, + }; +} + +/** Real proxy with the lane LIVE (unified storage + rollout forced on) so the + * probe passes and the guard — not the probe — is what decides conversion. */ +async function harness(): Promise<{ origin: string; close: () => Promise }> { + const dir = mkdtempSync(join(testRoot, "run-")); + const savedXdg: Record = {}; + const savedRollout = process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + const savedProxy = process.env.BILLION_CONTEXT_PROXY; + for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) { + savedXdg[key] = process.env[key]; + process.env[key] = dir; + } + process.env.BILI_STORAGE_UNIFIED = "full"; + process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = "1"; + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + __resetIncrementalClientForTests(); + const store = new SessionStore({ dir: dir + "/sessions", enabled: true, debounceMs: 60000 }); + _setStoreForTest(store); + _setForTest({}); + const upstream = http.createServer((req, res) => { + req.resume(); + req.on("end", () => { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "msg_test", role: "assistant", content: [{ type: "text", text: "answer" }], usage: { input_tokens: 100, output_tokens: 10 } })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const addr = upstream.address(); + assert(addr && typeof addr === "object"); + const upstreamUrl = `http://127.0.0.1:${addr.port}`; + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "claude-test": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: false }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); + if (!(proxy as unknown as { listening?: boolean }).listening) await once(proxy, "listening"); + const origin = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; + process.env.BILLION_CONTEXT_PROXY = origin; + return { + origin, + close: async () => { + for (const [key, v] of Object.entries(savedXdg)) { + if (v === undefined) delete process.env[key]; + else process.env[key] = v; + } + if (savedRollout === undefined) delete process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS; + else process.env.BILI_ROLLOUT_PLUGIN_INCREMENTAL_TURNS = savedRollout; + if (savedProxy === undefined) delete process.env.BILLION_CONTEXT_PROXY; + else process.env.BILLION_CONTEXT_PROXY = savedProxy; + await new Promise((resolve) => proxy.close(() => resolve())); + store.closeQuietly(); + await new Promise((resolve) => upstream.close(() => resolve())); + }, + }; +} + +test("#3433: pi OpenAI tool shapes ride full resend; anthropic shapes still convert", async () => { + const h = await harness(); + try { + const pi = makeFakePi(); + createBiliPlugin("pi")(pi as never); + const handler = pi.events.get("before_provider_request"); + assert.ok(handler, "before_provider_request handler registered"); + const ctx = { sessionManager: { getSessionId: () => "pi-inc-3433" }, model: { id: "claude-test", contextWindow: 400000, baseUrl: h.origin }, cwd: "/tmp" }; + + // OpenAI chat-completions shape: assistant.tool_calls + role:"tool" + const openaiBody = { model: "gpt-test", max_tokens: 64, stream: false, messages: [ + { role: "user", content: "list the files" }, + { role: "assistant", content: null, tool_calls: [{ id: "call_1", type: "function", function: { name: "ls", arguments: "{}" } }] }, + { role: "tool", tool_call_id: "call_1", content: "a.txt b.txt" }, + ] }; + const out = await handler({ payload: openaiBody }, ctx); + assert.ok(pi.tools.length > 0, "tools registered — the readiness gate passed, so the shape guard is what skipped the conversion"); + assert.equal(out, undefined, "OpenAI tool shape: no replacement — the host keeps its full-resend payload"); + + // control: the anthropic wire shape DOES convert in the same live-lane environment + const anthropicBody = { model: "claude-test", max_tokens: 64, system: "sys", messages: [{ role: "user", content: [{ type: "text", text: "hello" }] }] }; + const replaced = (await handler({ payload: anthropicBody }, ctx)) as { conversationId?: string; sinceSeq?: number; messages?: unknown[] } | undefined; + assert.ok(replaced !== undefined, "anthropic shape converts (guard does not over-block)"); + assert.equal(replaced?.conversationId, "pi-inc-3433"); + assert.equal(replaced?.sinceSeq, 0, "first turn seeds the lane"); + assert.equal(replaced?.messages?.length, 1); + } finally { + await h.close(); + } +}); From 30ae2188aa45f9f4902bb9e51c7f7eb3bede9c3f Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 08:36:11 +0800 Subject: [PATCH 3/8] =?UTF-8?q?fix:=20stop=20the=20auto-fold=20fold=20stor?= =?UTF-8?q?m=20on=20unreachable=20targets=20=E2=80=94=20measured=20floor?= =?UTF-8?q?=20+=20benefit=20gate=20(#3444)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A configured autoFoldTargetTokens below the session's irreducible baseline (system prompt + tool schemas + live tail) could never be reached: the trigger re-fired on every request and burned a full external-summary round trip per request for ever-smaller slivers (reported saves down to ~685 tokens, each costing up to ~60s of chain latency). Two complementary facets, same mechanism family (growth-fold economics): 1. Measured floor: an exhausted honest growth pass (folded everything foldable, no chain failure) whose rebuilt payload still sits at/above the target records the post-fold size as the session's irreducible floor (session metadata, latest measurement wins). The effective target is raised to max(configured, floor, MIN) capped at the window; a floor at/above the overflow bar disarms growth folding entirely and releases the nudge suppression (growthFoldingArmed gains a measuredFloor argument, threaded through all four prepare-* lanes). 2. Benefit gate: a growth fold only fires when the text channel exceeds the budget by at least AUTO_FOLD_MIN_BENEFIT_TOKENS (4096) of expected saving, and a pass that actually saves less than that arms the standard 10-minute backoff (#2662 queue-drop congestion exemption mirrored). Image-dominant overage keeps its original unconditional arm (images are not foldable). Classic overflow preflight (bar = window) is untouched. Regression tests: two e2e lanes (sub-floor system-prompt baseline stops re-folding marginal growth; giant-live-tail sub-benefit pass arms the backoff) plus truth-table rows for the floor argument and an autoFoldFloor invalid-value unit test. (cherry picked from commit e44c0d0650ead10fb00e45025a8c1492dfe6302f) --- src/external-summary-settings.ts | 7 + src/external-summary-surface.ts | 20 ++- src/server.ts | 51 ++++++- src/server/prepare-anthropic.ts | 6 +- src/server/prepare-google.ts | 6 +- src/server/prepare-openai.ts | 6 +- src/server/prepare-responses.ts | 6 +- tests/external-summary-autofold.test.ts | 168 ++++++++++++++++++++++++ 8 files changed, 249 insertions(+), 21 deletions(-) diff --git a/src/external-summary-settings.ts b/src/external-summary-settings.ts index 2d9b7c0b84..4e0278882c 100644 --- a/src/external-summary-settings.ts +++ b/src/external-summary-settings.ts @@ -91,6 +91,13 @@ function text(value: unknown, limit: number): string { export const AUTO_FOLD_TARGET_MIN = 8_192; const AUTO_FOLD_TARGET_MAX = 10_000_000; +/** [#3444] A growth fold must be EXPECTED to save at least this much to run, + * and a fold that actually saved less arms the backoff instead of re-firing + * next request: below this, the fixed cost of an external-summary round trip + * (latency + quota + cache invalidation) outweighs the saving — a sub-floor + * target used to burn such marginal folds on EVERY request (fold storm). */ +export const AUTO_FOLD_MIN_BENEFIT_TOKENS = 4_096; + /** [#autoFold] Both parsers share the exact switch/target semantics: the * target is only meaningful with the switch on; the switch defaults to * half the model window at trigger time. */ diff --git a/src/external-summary-surface.ts b/src/external-summary-surface.ts index ddb4bbb15d..73d67110fc 100644 --- a/src/external-summary-surface.ts +++ b/src/external-summary-surface.ts @@ -66,6 +66,15 @@ export function armAutoFoldBackoff(session: { metadata?: Record (session.metadata ?? (session.metadata = {})).autoFoldBackoffUntil = Date.now() + AUTO_FOLD_BACKOFF_MS; } +/** [#3444] Measured irreducible floor of this session (system prompt + tool + * schemas + live tail), recorded by an exhausted growth pass that could not + * reach its target. Latest measurement wins (tracks baseline drift both ways); + * a fresh session id starts unmeasured. Undefined until measured or invalid. */ +export function autoFoldFloor(session: { metadata?: Record } | undefined): number | undefined { + const value = session?.metadata?.autoFoldFloorTokens; + return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : undefined; +} + /** #2662: every external-summary batch of this invocation was dropped by the * shared pool before a single candidate call dispatched. That shape is * scheduler congestion (transient by nature), NOT evidence the summary chain @@ -91,16 +100,19 @@ export function autoFoldEngaged(config: unknown, session: { metadata?: Record 0)) return false; const configured = ext.autoFoldTargetTokens ?? Math.round(overflow / 2); - const target = Math.max(AUTO_FOLD_TARGET_MIN, Math.min(configured, overflow)); + const target = Math.max(AUTO_FOLD_TARGET_MIN, Math.min(configured, overflow), measuredFloor ?? 0); return target < overflow; } diff --git a/src/server.ts b/src/server.ts index eb07e54a3e..375be8c5ad 100644 --- a/src/server.ts +++ b/src/server.ts @@ -79,8 +79,8 @@ import { ccrEnabled, ccrLoopConfig, ccrPluginWireOk, contentStoreOf, executeRetr import { buildIncomingImageIndex, pruneRetrieveImgExports } from "./image-restore.js"; import { rulesEnabled, storeEffectiveRules } from "./rules-feature.js"; import { storeEffectiveSearchPlanAware } from "./decompress-shared.js"; -import { armAutoFoldBackoff, autoFoldEngaged, AUTO_FOLD_BACKOFF_MS, externalQueueDroppedAll, withExternalSummaryTools } from "./external-summary-surface.js"; -import { AUTO_FOLD_TARGET_MIN } from "./external-summary-settings.js"; +import { armAutoFoldBackoff, autoFoldEngaged, autoFoldFloor, AUTO_FOLD_BACKOFF_MS, externalQueueDroppedAll, withExternalSummaryTools } from "./external-summary-surface.js"; +import { AUTO_FOLD_TARGET_MIN, AUTO_FOLD_MIN_BENEFIT_TOKENS } from "./external-summary-settings.js"; import { auditStartupSummaryCredentials } from "./external-summary-credentials.js"; import { applyRanges } from "./stream.js"; import { attachSubagentSessions } from "./subagent-sessions.js"; @@ -3268,10 +3268,20 @@ export async function preflightCompressIfNeeded( // floor under the documented minimum. Clamping to the minimum disarms // auto-fold on sub-8K windows outright (target >= overflow ⇒ growthArmed // false), which matches the documented feature scope. + // [#3444] A configured target below what this session can physically reach + // (system prompt + tool schemas + live tail) is UNREACHABLE: every fold + // lands above it and the trigger re-fires on every request (fold storm). + // An exhausted growth pass measures the irreducible floor into session + // metadata — raise the effective target to it so the trigger arms on real + // GROWTH past the floor instead of the permanent gap. Latest measurement + // wins (tracks baseline drift); a floor at/above the window collapses the + // arming entirely (growthArmed false ⇒ classic mode, nudge suppression + // releases via growthFoldingArmed's measuredFloor arg). + const measuredFloor = autoFoldOn ? autoFoldFloor(session) : undefined; const autoFoldTarget = autoFoldOn - ? Math.max(AUTO_FOLD_TARGET_MIN, Math.min((config as ResolvedKernelConfig).externalSummary?.autoFoldTargetTokens ?? (overflowTarget > 0 ? Math.round(overflowTarget / 2) : 0), overflowTarget)) + ? Math.min(Math.max(Math.max(AUTO_FOLD_TARGET_MIN, Math.min((config as ResolvedKernelConfig).externalSummary?.autoFoldTargetTokens ?? (overflowTarget > 0 ? Math.round(overflowTarget / 2) : 0), overflowTarget)), measuredFloor ?? 0), overflowTarget) : undefined; - const growthArmed = autoFoldOn && autoFoldTarget !== undefined && autoFoldTarget > 0 && autoFoldTarget < overflowTarget; // narrowing form of growthFoldingArmed(config, overflowTarget) — keep in sync (PR #2581 review) + const growthArmed = autoFoldOn && autoFoldTarget !== undefined && autoFoldTarget > 0 && autoFoldTarget < overflowTarget; // narrowing form of growthFoldingArmed(config, overflowTarget, measuredFloor) — keep in sync (PR #2581 review) const compressionTarget = growthArmed ? autoFoldTarget : overflowTarget; // A fresh session (id rotated, e.g. after a model switch) has // lastInputTokens = 0 while still carrying a full raw history; size the @@ -3351,7 +3361,18 @@ export async function preflightCompressIfNeeded( : calibratedText; const textBudget = Math.max(0, compressionTarget - imageTokens); const decisionTrigger = Math.max(Math.max(0, baselineFloor - imageTokens), textChannel); - const triggerFires = imageTokens >= compressionTarget || decisionTrigger >= textBudget; + // [#3444] expected-benefit gate for growth folds: once the payload sits at + // the (possibly floor-raised) target, every further request re-crosses it + // with only marginal new content — folding that burns a full external- + // summary round trip to save less than its fixed cost. Require the text + // channel to exceed the budget by at least AUTO_FOLD_MIN_BENEFIT_TOKENS + // before arming; below that the payload fits the window by definition and + // forwards as-is until enough material accrues. Classic overflow preflight + // is untouched (its bar is the window itself — there, firing is mandatory). + // Image-dominant payloads keep the original unconditional arm: images are + // not foldable, so their overage is not "expected benefit" at all. + const growthWorthIt = !growthArmed || imageTokens >= compressionTarget || decisionTrigger - textBudget >= AUTO_FOLD_MIN_BENEFIT_TOKENS; + const triggerFires = growthWorthIt && (imageTokens >= compressionTarget || decisionTrigger >= textBudget); // #2283 sub-defect 2: pair upstream billing with the same-payload local estimate on // EVERY request (not only when preflight triggers) — the offline data source for the // chars/4 deviation distribution behind the #2273 RC2 calibration decision. @@ -3701,6 +3722,16 @@ export async function preflightCompressIfNeeded( // deflation) mirrors the fit-gate quantity; lifetime is bounded (see // session.ts) so never-reporting upstreams fall back to legacy sizing. setPostRebuildAnchor(session, rebuiltTextSize + imageTokens); + // [#3444] exhausted honest pass: the chain folded everything it could + // (no failure) yet the rebuilt payload still sits at/above the growth + // target — the target is physically unreachable for this session. + // Record the measured floor so the next trigger arms on real growth + // past it instead of re-firing every request; latest measurement wins. + if (growthArmed && result.failure === undefined && rebuiltTextSize + imageTokens >= compressionTarget) { + session.metadata.autoFoldFloorTokens = rebuiltTextSize + imageTokens; + markDirty(session); + log("warn", `[${session.id}] auto-fold target ${compressionTarget} unreachable — the session's irreducible baseline measures ~${rebuiltTextSize + imageTokens} tokens (system prompt + tools + live tail); effective target raised to the floor until the gap closes (#3444). Raise compress.externalSummary.autoFoldTargetTokens above ${rebuiltTextSize + imageTokens} for headroom`); + } } else { fits = unknownBaseline ? result.fitsWindow @@ -3749,6 +3780,16 @@ export async function preflightCompressIfNeeded( } else { log("warn", `[${session.id}] preflight made no progress but the payload fits; forwarding as-is`); } + } else if (growthArmed && !queueDroppedAll && result.savedTokens < AUTO_FOLD_MIN_BENEFIT_TOKENS) { + // [#3444] sub-benefit fold: a full external-summary round trip saved + // less than its fixed cost — same economics as a zero-progress pass, + // so arm the same cooldown instead of paying per-request for + // marginal folds. Estimation error is the usual cause (the trigger + // believed more was over budget than the fold could actually save); + // #2662's congestion exemption applies unchanged. + log("warn", `[${session.id}] auto-fold saved only ~${result.savedTokens} tokens (< ${AUTO_FOLD_MIN_BENEFIT_TOKENS} worth) — forwarding as-is (estimate fits the model window ${limit}); backing off auto-fold for ${Math.round(AUTO_FOLD_BACKOFF_MS / 60_000)}m so classic nudges resume (#3444)`); + armAutoFoldBackoff(session); + markDirty(session); } return outbound; } diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index d68db32d11..0c223959ed 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -21,7 +21,7 @@ import { ABSORB_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL, RULE_TOOL, absorbToolsFor, import { applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, dropRetrievals, imageRescaleToolArmed, pruneExpiredRetrievals, reconcileReloadedRetrievals, renderRetrievalNotes, retrieveToolName, snapshotPendingRetrievals, snapshotRetrievalNotes } from "../store.js"; import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; -import { autoFoldEngaged, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; +import { autoFoldEngaged, autoFoldFloor, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; import { filterDeadRanges } from "../dead-ranges.js"; import { detectGenerationBoundary } from "../generation-boundary.js"; import { attachSubagentSessions } from "../subagent-sessions.js"; @@ -393,7 +393,7 @@ export async function prepareAnthropic( // consecutive decision failures. const decideTier1Arm = decide !== undefined && !!turn.nudge && turn.nudge.tier === 1 && turn.nudge.breakdown.emergencyOverride !== 1; const decideFallbackOverride = decideTier1Arm && ladderMode(session.metadata) === "fallback"; - const nudgeEligible = opts.compress.injectNudge && !!turn.nudge && laneDecisions.compressSurfaceOn && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig)); + const nudgeEligible = opts.compress.injectNudge && !!turn.nudge && laneDecisions.compressSurfaceOn && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig, undefined, autoFoldFloor(session))); // #3461: pressure admitted by every standing gate but held by the backoff // is still an active nudge round — self-heal credit (#2155/#3004) must // not starve because we chose not to re-inject this round. @@ -409,7 +409,7 @@ export async function prepareAnthropic( suppressionBypassed: !nudgeSuppressed(session) || decideFallbackOverride, breakerArmed: compressBreakerArmed(session), autoFoldEngaged: autoFoldEngaged(loopConfig, session), - growthFoldingArmed: growthFoldingArmed(loopConfig), + growthFoldingArmed: growthFoldingArmed(loopConfig, undefined, autoFoldFloor(session)), modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor)), }); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, resolvedModel ?? parsed.model, willInjectNudge, decideTier1Arm && !decideFallbackOverride, surfaceOffDetail(laneDecisions, opts.compress.injectTool))); diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 1a6a931281..50495870c3 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -20,7 +20,7 @@ import { ABSORB_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_GOOGLE, RULE_TOOL_GOOGLE, ab import { absorbToolName, applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, imageRescaleToolArmed, retrieveToolName } from "../store.js"; import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; -import { autoFoldEngaged, conversationPromptSections, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; +import { autoFoldEngaged, autoFoldFloor, conversationPromptSections, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; import { filterDeadRanges } from "../dead-ranges.js"; import { detectGenerationBoundary, GENERATION_BOUNDARY_NOTE } from "../generation-boundary.js"; import { attachSubagentSessions } from "../subagent-sessions.js"; @@ -227,7 +227,7 @@ export async function prepareGoogle( // advisory punches through per 3 consecutive decision failures. const decideTier1Arm = decide !== undefined && !!turn.nudge && turn.nudge.tier === 1 && turn.nudge.breakdown.emergencyOverride !== 1; const decideFallbackOverride = decideTier1Arm && ladderMode(session.metadata) === "fallback"; - const nudgeEligible = opts.compress.injectNudge && !!turn.nudge && laneDecisions.compressSurfaceOn && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig)); + const nudgeEligible = opts.compress.injectNudge && !!turn.nudge && laneDecisions.compressSurfaceOn && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig, undefined, autoFoldFloor(session))); // #3461: pressure admitted by every standing gate but held by the backoff // is still an active nudge round — self-heal credit (#2155/#3004) must // not starve because we chose not to re-inject this round. @@ -245,7 +245,7 @@ export async function prepareGoogle( suppressionBypassed: !nudgeSuppressed(session) || decideFallbackOverride, breakerArmed: compressBreakerArmed(session), autoFoldEngaged: autoFoldEngaged(loopConfig, session), - growthFoldingArmed: growthFoldingArmed(loopConfig), + growthFoldingArmed: growthFoldingArmed(loopConfig, undefined, autoFoldFloor(session)), modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor)), }); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, model, willInjectNudge, decideTier1Arm && !decideFallbackOverride, surfaceOffDetail(laneDecisions, opts.compress.injectTool))); diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 47d2299950..b431ef06ed 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -21,7 +21,7 @@ import { ABSORB_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_OPENAI, RULE_TOOL_OPENAI, ab import { absorbToolName, applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, dropRetrievals, imageRescaleToolArmed, pruneExpiredRetrievals, reconcileReloadedRetrievals, renderRetrievalNotes, retrieveToolName, snapshotPendingRetrievals, snapshotRetrievalNotes } from "../store.js"; import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; -import { autoFoldEngaged, conversationPromptSections, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; +import { autoFoldEngaged, autoFoldFloor, conversationPromptSections, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; import { filterDeadRanges } from "../dead-ranges.js"; import { detectGenerationBoundary, GENERATION_BOUNDARY_NOTE } from "../generation-boundary.js"; import { attachSubagentSessions } from "../subagent-sessions.js"; @@ -247,7 +247,7 @@ export async function prepareOpenai( // counter on the same round, so at most ONE legacy advisory punches // through per DECIDE_FALLBACK_THRESHOLD consecutive decision failures. const decideFallbackOverride = decideTier1Arm && ladderMode(session.metadata) === "fallback"; - const nudgeEligible = opts.compress.injectNudge && !!turn.nudge && laneDecisions.compressSurfaceOn && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig)); + const nudgeEligible = opts.compress.injectNudge && !!turn.nudge && laneDecisions.compressSurfaceOn && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig, undefined, autoFoldFloor(session))); // #3461: pressure admitted by every standing gate but held by the backoff // is still an active nudge round — self-heal credit (#2155/#3004) must // not starve because we chose not to re-inject this round. @@ -265,7 +265,7 @@ export async function prepareOpenai( suppressionBypassed: !nudgeSuppressed(session) || decideFallbackOverride, breakerArmed: compressBreakerArmed(session), autoFoldEngaged: autoFoldEngaged(loopConfig, session), - growthFoldingArmed: growthFoldingArmed(loopConfig), + growthFoldingArmed: growthFoldingArmed(loopConfig, undefined, autoFoldFloor(session)), modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor)), }); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge, decideTier1Arm && !decideFallbackOverride, surfaceOffDetail(laneDecisions, opts.compress.injectTool))); diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index 0d0e562a40..4f7f754068 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -23,7 +23,7 @@ import { ABSORB_TOOL_NAME, BILI_ACP_READONLY_TOOLS_RESPONSES, BILI_ACP_READONLY_ import { absorbToolName, applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, imageRescaleToolArmed, retrieveToolName } from "../store.js"; import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; -import { autoFoldEngaged, conversationPromptSections, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; +import { autoFoldEngaged, autoFoldFloor, conversationPromptSections, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; import { filterDeadRanges } from "../dead-ranges.js"; import { detectGenerationBoundary, GENERATION_BOUNDARY_NOTE } from "../generation-boundary.js"; import { attachSubagentSessions } from "../subagent-sessions.js"; @@ -348,7 +348,7 @@ export async function prepareResponses( // advisory punches through per 3 consecutive decision failures. const decideTier1Arm = decide !== undefined && !!turn.nudge && turn.nudge.tier === 1 && turn.nudge.breakdown.emergencyOverride !== 1; const decideFallbackOverride = decideTier1Arm && ladderMode(session.metadata) === "fallback"; - const nudgeEligible = opts.compress.injectNudge && !!turn.nudge && compressSurfaceOn && !isCompactionTrigger && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig, codexLane && codexCompactMode() === "intercept" ? loopConfig.modelContextLimit * CODEX_COMPACT_HEALTH_RATIO : undefined)); + const nudgeEligible = opts.compress.injectNudge && !!turn.nudge && compressSurfaceOn && !isCompactionTrigger && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig, codexLane && codexCompactMode() === "intercept" ? loopConfig.modelContextLimit * CODEX_COMPACT_HEALTH_RATIO : undefined, autoFoldFloor(session))); // #3461: pressure admitted by every standing gate but held by the backoff // is still an active nudge round — self-heal credit (#2155/#3004) must // not starve because we chose not to re-inject this round. @@ -365,7 +365,7 @@ export async function prepareResponses( suppressionBypassed: !nudgeSuppressed(session) || decideFallbackOverride, breakerArmed: compressBreakerArmed(session), autoFoldEngaged: autoFoldEngaged(loopConfig, session), - growthFoldingArmed: growthFoldingArmed(loopConfig, codexLane && codexCompactMode() === "intercept" ? loopConfig.modelContextLimit * CODEX_COMPACT_HEALTH_RATIO : undefined), + growthFoldingArmed: growthFoldingArmed(loopConfig, codexLane && codexCompactMode() === "intercept" ? loopConfig.modelContextLimit * CODEX_COMPACT_HEALTH_RATIO : undefined, autoFoldFloor(session)), modelWantsIt: turn.nudge !== undefined && (turn.nudge.shouldInject || emergencyNudgeArmed(session, turn.nudge, loopConfig.compress.minCompressRange, tokenCount, loopConfig.nudge.growthFloor)), }); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge, decideTier1Arm && !decideFallbackOverride, surfaceOffDetail(laneDecisions, opts.compress.injectTool))); diff --git a/tests/external-summary-autofold.test.ts b/tests/external-summary-autofold.test.ts index 99d0b28708..85ce582b5f 100644 --- a/tests/external-summary-autofold.test.ts +++ b/tests/external-summary-autofold.test.ts @@ -89,6 +89,27 @@ async function postTurn(port: number, upstreamUrl: string, session: string, pair return { status: res.status, body }; } +/** Flexible turn sender for #3444 fixtures that need a top-level `system` + * prompt or a custom final message (the irreducible-baseline shapes). */ +async function postRaw(port: number, session: string, extra: Record): Promise<{ status: number }> { + const res = await fetch(`http://127.0.0.1:${port}/v1/messages`, { + method: "POST", + headers: { + "content-type": "application/json", + "x-api-key": "k", + "anthropic-version": "2023-06-01", + "x-acp-session": session, + }, + body: JSON.stringify({ model: "test-model", max_tokens: 1024, stream: true, ...extra }), + }); + await res.text(); + return { status: res.status }; +} + +/** ~60KB of system prompt ≈ 15K tokens — an irreducible baseline far above + * the 8192 growth target used by the #3444 fixtures. */ +const BIG_SYSTEM = `policy doc: ${"0123456789abcdef".repeat(3700)}`; + const servers: { close: () => Promise }[] = []; function trackClose(server: http.Server): void { servers.push({ close: () => new Promise((resolve) => server.close(() => resolve(undefined))) }); @@ -408,6 +429,134 @@ test("auto-fold backoff: a pure queue-drop (zero dispatched attempts) does NOT a } }); +// --------------------------------------------------------------------------- +// #3444: sub-floor targets must not storm (measured floor + benefit gate) +// --------------------------------------------------------------------------- + +/** bigConversation with one extra ~4KB user pair spliced before the tail. */ +function conversationWithExtraPair(pairs: number): unknown[] { + const base = bigConversation(pairs) as unknown[]; + base.pop(); // drop the trailing "final question" + base.push({ role: "user", content: [{ type: "text", text: `extra doc: ${"abcdef".repeat(667)}` }] }); + base.push({ role: "assistant", content: [{ type: "text", text: "ack extra" }] }); + base.push({ role: "user", content: [{ type: "text", text: "final question" }] }); + return base; +} + +test("#3444 sub-floor target: an exhausted fold measures the irreducible floor — marginal growth stops re-folding", async () => { + const main = await startMainUpstream(); trackClose(main.server); + const sum = await startSummaryUpstream("ok"); trackClose(sum.server); + const server = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: main.url, + routes: { [main.url]: { models: { "test-model": { context: 50_000 } } } }, + modelContextLimit: 50_000, + kernelConfig: defaultConfig(50_000, { preserveRecentMessages: 0, preserveRecentTokens: 0, compress: { minCompressRange: 100, maxSummaryLength: 20000, minSummaryLength: 50 } }), + compress: { + injectTool: true, + injectNudge: true, + externalSummary: { enabled: true, targets: ["sum/sm"], autoFold: true, autoFoldTargetTokens: 8192 }, + }, + namedProviders: { sum: { baseUrl: sum.url, api: "openai", apiKeyEnv: "E2E_SUM_KEY", models: { sm: {} } } }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + passthroughSource: null, + autoUpdate: false, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + trackClose(server); + await once(server, "listening"); + const port = (server.address() as AddressInfo).port; + process.env.E2E_SUM_KEY = "k"; + + // The system prompt alone (~15K tokens) sits far above the 8192 target: + // no fold can ever reach it. Pre-#3444 every turn re-fired the trigger + // (payload >= target forever) and burned an external-summary round trip + // for whatever sliver was foldable — the reported fold storm. + const t1 = await postRaw(port, "autofold-floor", { system: BIG_SYSTEM, messages: bigConversation(12) }); + assert.equal(t1.status, 200, "turn 1 must forward"); + assert.ok(sum.bodies.length >= 1, `turn 1 must attempt the growth fold, got ${sum.bodies.length} summary calls`); + const attemptsAfterTurn1 = sum.bodies.length; + + // Turn 2: same conversation plus one small extra pair (~1K tokens of real + // growth past the measured floor). Payload is still far above the + // CONFIGURED target 8192 — pre-#3444 this unconditionally re-ran the + // chain. Now the effective target is the measured floor, so the expected + // benefit (~1K) sits under the 4096-token cost bar: no new external calls. + const t2 = await postRaw(port, "autofold-floor", { system: BIG_SYSTEM, messages: conversationWithExtraPair(12) }); + assert.equal(t2.status, 200, "turn 2 must forward"); + assert.equal(sum.bodies.length, attemptsAfterTurn1, "marginal growth past a sub-floor target must NOT re-run the external chain (fold storm)"); + const text2 = JSON.stringify(main.bodies.at(-1)); + assert.ok(text2.includes("SUMMARY:"), "the turn-1 fold must persist in the forwarded history"); +}); + +test("#3444 sub-benefit fold: a round trip that saves less than its cost arms the backoff instead of repeating next turn", async () => { + const main = await startMainUpstream(); trackClose(main.server); + const sum = await startSummaryUpstream("ok"); trackClose(sum.server); + const server = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: main.url, + routes: { [main.url]: { models: { "test-model": { context: 50_000 } } } }, + modelContextLimit: 50_000, + kernelConfig: defaultConfig(50_000, { preserveRecentMessages: 0, preserveRecentTokens: 0, compress: { minCompressRange: 100, maxSummaryLength: 20000, minSummaryLength: 50 } }), + compress: { + injectTool: true, + injectNudge: true, + externalSummary: { enabled: true, targets: ["sum/sm"], autoFold: true, autoFoldTargetTokens: 8192 }, + }, + namedProviders: { sum: { baseUrl: sum.url, api: "openai", apiKeyEnv: "E2E_SUM_KEY", models: { sm: {} } } }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + passthroughSource: null, + autoUpdate: false, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + trackClose(server); + await once(server, "listening"); + const port = (server.address() as AddressInfo).port; + process.env.E2E_SUM_KEY = "k"; + + // Only ~3K tokens are foldable (three small pairs); the rest of the + // payload is the live tail — one ~15K-token final user message that no + // fold may touch. The trigger sees a large expected benefit and fires; + // the actual pass saves ~3K < 4096 AUTO_FOLD_MIN_BENEFIT_TOKENS. + const giantTail = bigConversation(3) as unknown[]; + giantTail.pop(); + giantTail.push({ role: "user", content: [{ type: "text", text: `giant paste: ${"0123456789abcdef".repeat(3750)}` }] }); + + const t1 = await postRaw(port, "autofold-subbenefit", { messages: giantTail }); + assert.equal(t1.status, 200, "turn 1 must forward"); + assert.ok(sum.bodies.length >= 1, `turn 1 dispatched the fold attempt (proves the sub-benefit path, not zero-progress), got ${sum.bodies.length}`); + const attemptsAfterTurn1 = sum.bodies.length; + + // Turn 2: identical payload, still far above the configured target. Pre- + // #3444 the cheap pass repeated every turn; now the under-cost result + // armed the 10-minute backoff, so the chain is skipped entirely. + const t2 = await postRaw(port, "autofold-subbenefit", { messages: giantTail }); + assert.equal(t2.status, 200, "turn 2 must forward (backoff = skip, not fail)"); + assert.equal(sum.bodies.length, attemptsAfterTurn1, "a sub-benefit fold must arm the backoff — no repeat round trip next turn"); +}); + // --------------------------------------------------------------------------- // growthFoldingArmed unit truth table (#2581 review fix #1) // --------------------------------------------------------------------------- @@ -439,6 +588,25 @@ test("growthFoldingArmed: only suppress classic nudges when growth folding can a assert.equal(growthFoldingArmed({ externalSummary: { enabled: true, autoFold: true }, modelContextLimit: 20_000 }, 20_000), true, "bar with headroom above AUTO_FOLD_TARGET_MIN arms"); assert.equal(growthFoldingArmed({ externalSummary: { enabled: true, autoFold: true }, modelContextLimit: 10_000 }, 8192), false, "bar exactly at AUTO_FOLD_TARGET_MIN clamps the implicit target up to the bar — not armed"); assert.equal(growthFoldingArmed(chain(true, 4600), 4600), false, "explicit target clamped to at least MIN above the bar is a no-op"); + // #3444: the measured irreducible floor raises the effective target; at or + // above the overflow bar the target is unreachable, so suppression must release. + assert.equal(growthFoldingArmed(chain(true, 8192), undefined, 10_000), true, "floor 10K under the 50K window keeps arming (target raised to the floor)"); + assert.equal(growthFoldingArmed(chain(true, 8192), undefined, 50_000), false, "floor == window disarms (bar unreachable)"); + assert.equal(growthFoldingArmed(chain(true, 8192), undefined, 60_000), false, "floor above the window disarms"); + assert.equal(growthFoldingArmed(chain(true, 8192), 45_000, 46_000), false, "floor above the codex-lane bar disarms"); + assert.equal(growthFoldingArmed(chain(true, 8192), 45_000, 40_000), true, "floor below the codex-lane bar keeps arming"); +}); + +test("autoFoldFloor: reads the measured floor from session metadata, rejecting invalid values", async () => { + const { autoFoldFloor } = await import("../src/external-summary-surface.ts"); + assert.equal(autoFoldFloor(undefined), undefined); + assert.equal(autoFoldFloor({}), undefined); + assert.equal(autoFoldFloor({ metadata: {} }), undefined); + assert.equal(autoFoldFloor({ metadata: { autoFoldFloorTokens: 42_000 } }), 42_000); + assert.equal(autoFoldFloor({ metadata: { autoFoldFloorTokens: "42000" } }), undefined); + assert.equal(autoFoldFloor({ metadata: { autoFoldFloorTokens: Number.NaN } }), undefined); + assert.equal(autoFoldFloor({ metadata: { autoFoldFloorTokens: 0 } }), undefined); + assert.equal(autoFoldFloor({ metadata: { autoFoldFloorTokens: -5 } }), undefined); }); // --------------------------------------------------------------------------- From 790523d6fe6638435c9fa750831ba6f0f4cece05 Mon Sep 17 00:00:00 2001 From: WSXYT Date: Sat, 17 Oct 2026 22:54:18 +0800 Subject: [PATCH 4/8] fix: preserve native Pi compress carrier identity (#3587) (cherry picked from commit 5c0e6bd9fddfa6342179c5081ff86cc6ea81e98d) --- src/agent/pi.ts | 4 +- src/agent/shared.ts | 5 +- src/plugin.ts | 54 ++++++++-- src/server/prepare-responses.ts | 4 + tests/plugin-agent.test.ts | 6 +- tests/plugin-call-identity.test.ts | 165 +++++++++++++++++++++++++++++ 6 files changed, 224 insertions(+), 14 deletions(-) create mode 100644 tests/plugin-call-identity.test.ts diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 10ebac01c6..bef3be815d 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -599,7 +599,7 @@ function manifestToTool(proxyBase: string, tool: ManifestTool, agent: string): T description: tool.description, parameters: tool.inputSchema, loadMode: "essential", - execute: async (_toolCallId, params, signal, _onUpdate, ctx) => { + execute: async (toolCallId, params, signal, _onUpdate, ctx) => { const conversationId = sessionIdOf(ctx) ?? "unknown"; try { // #2416: read the LIVE base — a native respawn after a proxy @@ -619,7 +619,7 @@ function manifestToTool(proxyBase: string, tool: ManifestTool, agent: string): T // rebinding (onRoutedOriginObserved) converges env back to the // observed routed origin. const base = proxyBaseForCtx(ctx) ?? proxyBase; - const output = await forwardTool(base, conversationId, tool.name, params, signal, conversationId !== "unknown"); + const output = await forwardTool(base, conversationId, tool.name, params, signal, conversationId !== "unknown", toolCallId); // #2204: a business failure (e.g. a refused export) must reach // the host as isError — rendering it as plain success text is // what hid the #2204 write failures from OMP. diff --git a/src/agent/shared.ts b/src/agent/shared.ts index 0e6833151a..de88a2d000 100644 --- a/src/agent/shared.ts +++ b/src/agent/shared.ts @@ -376,8 +376,9 @@ export async function reportAttributionHealth(proxyBase: string, health: Attribu * partial note in `text`. Absent on legacy envelopes without the fields. */ export type ForwardedToolResult = { text: string; failed: boolean; outcome?: ToolOutcomeKind; blocksCreated?: number }; -export async function forwardTool(proxyBase: string, conversationId: string, tool: string, args: unknown, signal?: AbortSignal, nativeCaller: boolean = false): Promise { - const body: { conversationId: string; tool: string; args: unknown; nativeCaller?: boolean } = { conversationId, tool, args: args ?? {} }; +export async function forwardTool(proxyBase: string, conversationId: string, tool: string, args: unknown, signal?: AbortSignal, nativeCaller: boolean = false, toolCallId?: string): Promise { + const body: { conversationId: string; tool: string; args: unknown; nativeCaller?: boolean; toolCallId?: string } = { conversationId, tool, args: args ?? {} }; + if (toolCallId !== undefined) body.toolCallId = toolCallId; // #2072: host-native agents (pi / dsh / opencode) stamp a per-call id minted // by the host's own session manager — declare it so a stale sibling witness // can neither redirect nor fail-closed the call (ladder rung 0, #2024/#2016). diff --git a/src/plugin.ts b/src/plugin.ts index 3e44d691c6..56796dfcb5 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -130,15 +130,54 @@ const PLUGIN_REQUEST_AGENT_HEADER = "x-bili-plugin-agent"; const PLUGIN_PROTOCOL_VERSION = 1; -/** #1567: folds executed through POST /__bili/plugin/tool are minted this - * synthetic callId, which the client can never echo back — its own re-sent - * compress pair is the summary carrier for such blocks, so the kernel's - * in-place acp_summary anchor is redundant and must be stripped (#1567). */ +/** #1567: legacy callers without a native tool-call id get a synthetic id. + * Their resent compress pair is recognized by range as a summary carrier. */ const PLUGIN_FOLD_CALLID_PREFIX = "plugin_"; export function isPluginFoldCallId(callId: string | undefined): boolean { return typeof callId === "string" && callId.startsWith(PLUGIN_FOLD_CALLID_PREFIX); } +/** Reconnect legacy plugin blocks before the kernel mistakes their live carriers for orphans. */ +export function reconcilePluginFoldCallIds(session: Session, messages: CoreMessage[]): number { + const blocks = new Map(); + for (const block of session.state.blocks) { + if (!block.active || !block.startRef || !block.endRef) continue; + const key = JSON.stringify([block.startRef, block.endRef]); + const sameRange = blocks.get(key) ?? []; + sameRange.push(block); + blocks.set(key, sameRange); + } + const calls = new Map>(); + const results = new Map(messages.filter((m) => m.contentType === "tool-result" && m.toolCallId).map((m) => [m.toolCallId!, m.text ?? ""])); + for (const message of messages) { + if (message.contentType !== "tool-call" || message.toolName !== "compress" || !message.toolCallId) continue; + try { + const args = JSON.parse(message.text ?? "") as { content?: unknown; startId?: unknown; endId?: unknown }; + for (const range of Array.isArray(args?.content) ? args.content : [args]) { + if (!range || typeof range.startId !== "string" || typeof range.endId !== "string") continue; + const key = JSON.stringify([range.startId, range.endId]); + if (!blocks.has(key)) continue; + const ids = calls.get(key) ?? new Set(); + ids.add(message.toolCallId); + calls.set(key, ids); + } + } catch { /* Invalid historical arguments cannot establish ownership. */ } + } + let changed = 0; + for (const [key, sameRange] of blocks) { + const block = sameRange[0]!; + const ids = calls.get(key); + if (sameRange.length !== 1 || !isPluginFoldCallId(block.compressCallId) || ids?.size !== 1) continue; + const id = ids.values().next().value!; + const receipt = results.get(id); + if (!receipt?.startsWith("[Compressed ") || !receipt.split("\n").some((line) => line.startsWith(` · ${block.blockId} full summary `))) continue; + block.compressCallId = id; + changed++; + } + if (changed > 0) markDirty(session); + return changed; +} + const VERSION = (() => { try { const here = fileURLToPath(import.meta.url); @@ -1796,9 +1835,9 @@ export async function handlePluginTool( res: import("node:http").ServerResponse, deps: PluginToolDeps, ): Promise { - let parsed: { conversationId?: unknown; tool?: unknown; args?: unknown; expectedRevision?: unknown; nativeCaller?: unknown }; + let parsed: { conversationId?: unknown; tool?: unknown; args?: unknown; expectedRevision?: unknown; nativeCaller?: unknown; toolCallId?: unknown }; try { - parsed = JSON.parse(payload) as { conversationId?: unknown; tool?: unknown; args?: unknown; expectedRevision?: unknown; nativeCaller?: unknown }; + parsed = JSON.parse(payload) as { conversationId?: unknown; tool?: unknown; args?: unknown; expectedRevision?: unknown; nativeCaller?: unknown; toolCallId?: unknown }; if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error("expected tool object"); } catch { res.writeHead(400, { "content-type": "application/json" }); @@ -1807,6 +1846,7 @@ export async function handlePluginTool( } const conversationId = typeof parsed.conversationId === "string" ? parsed.conversationId.trim() : ""; const tool = typeof parsed.tool === "string" ? parsed.tool : ""; + if (parsed.toolCallId !== undefined && (typeof parsed.toolCallId !== "string" || !parsed.toolCallId.trim() || parsed.toolCallId.length > 512)) return forkReply(res, 400, { ok: false, code: "INVALID_REQUEST", error: "toolCallId must be a non-empty string of at most 512 characters" }); if (parsed.expectedRevision !== undefined && (typeof parsed.expectedRevision !== "string" || !/^[a-f0-9]{64}$/.test(parsed.expectedRevision))) return forkReply(res, 400, { ok: false, code: "INVALID_REQUEST", error: "expectedRevision must be a snapshot revision" }); if (parsed.expectedRevision !== undefined && !conversationId) return forkReply(res, 400, { ok: false, code: "INVALID_REQUEST", error: "expectedRevision requires conversationId" }); // Explicit ids never yield to another session's outbound witness. A @@ -1992,7 +2032,7 @@ export async function handlePluginTool( // may still echo one — strip it before kernel arg parsing sees it (the // witness hash in tool-ring.ts strips the same key). delete args.conversation_id; - const callId = `${PLUGIN_FOLD_CALLID_PREFIX}${Date.now().toString(36)}`; + const callId = typeof parsed.toolCallId === "string" ? parsed.toolCallId : `${PLUGIN_FOLD_CALLID_PREFIX}${Date.now().toString(36)}`; acquireInFlight(session); let result: ProxyToolResult | undefined; let retrievalExport: RetrievalExport | undefined; diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index 4f7f754068..e3fe234794 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -34,6 +34,7 @@ import { reconcileSystemAnchor } from "../system-anchor.js"; import { buildPromptForgeBody, buildTriggerForgeBody, carriesCodexLocalCompactionSummary, codexCompactGate, codexCompactGatePre, codexCompactMode, hasCompactionPrompt, hasCompactionTrigger, isCodexClient, isCodexHost, mergeForgedSummaries, replaceBiliCompactionItems, stripBiliCompactionItems, CODEX_COMPACT_HEALTH_RATIO, CODEX_LOCAL_COMPACTION_MIN_MISSING } from "../codex-compact.js"; import type { ConversationIdentity } from "../session-id.js"; import { pckKeyAbated } from "../pck-probe.js"; +import { reconcilePluginFoldCallIds } from "../plugin.js"; import { logRawStripCounts, markerArtifactCount, stripRawStage } from "../marker/engine.js"; import { imageRescaleMaxDim, keepResponseId as knobKeepResponseId, noCompressPrompt as knobNoCompressPrompt, noInjectTool as knobNoInjectTool, renderNone as knobRenderNone } from "../knobs.js"; import { applyImageRescaleItems } from "../image-rescale.js"; @@ -287,6 +288,9 @@ export async function prepareResponses( // boundary (#395) against this pre-turn set. Mirrors the anthropic/openai/ // google lanes — the responses lane was the only one missing the consumer. const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); + // #3587: keep native Pi compress carrier identity — plugin lanes re-send + // their own compress call pairs; reconcile their ids before the re-anchor. + if (pluginMode && !isCompactionTrigger && !hasCompactionPrompt(parsed.input)) reconcilePluginFoldCallIds(session, msgs); // [#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). diff --git a/tests/plugin-agent.test.ts b/tests/plugin-agent.test.ts index 9985cc153d..e4ec88646d 100644 --- a/tests/plugin-agent.test.ts +++ b/tests/plugin-agent.test.ts @@ -77,7 +77,7 @@ test("proxyBaseFromEnv accepts BILLION_CONTEXT_PROXY, detectProxyBase honors kil type FakeProxy = { origin: string; - toolCalls: Array<{ conversationId: string; tool: string; args: unknown; nativeCaller?: boolean }>; + toolCalls: Array<{ conversationId: string; tool: string; args: unknown; nativeCaller?: boolean; toolCallId?: string }>; registers: Array<{ conversationId: string; agent: string; identity: boolean; parentConversationId?: string }>; runtimeInfos: Array>; close(): Promise; @@ -772,7 +772,7 @@ test("pi extension registers manifest tools and stamps headers when proxied", as assert.equal(out.isError, undefined); // #2072: the pi host stamps its session-manager id as a confirmed // native caller — the captured wire body carries the flag. - assert.deepEqual(proxy.toolCalls, [{ conversationId: "sess-42", tool: "compress", args: { content: [] }, nativeCaller: true }]); + assert.deepEqual(proxy.toolCalls, [{ conversationId: "sess-42", tool: "compress", args: { content: [] }, nativeCaller: true, toolCallId: "call-1" }]); const errOut = await pi.tools[1]!.execute("call-2", {}, undefined, undefined, fakeCtx(proxy)); assert.match(errOut.content[0]!.text, /bili tool error:.*boom/); assert.equal(errOut.isError, true); @@ -834,7 +834,7 @@ test("#2416: registered tools track the respawned proxy origin — live re-read out = await pi.tools[0]!.execute("call-2", { content: [] }, undefined, undefined, ctx); assert.equal(out.isError, undefined, "the tool must reach the respawned proxy, not keep firing at the dead port"); assert.match(out.content[0]!.text, /\[Compressed/); - assert.deepEqual(newProxy.toolCalls, [{ conversationId: "sess-42", tool: "compress", args: { content: [] }, nativeCaller: true }]); + assert.deepEqual(newProxy.toolCalls, [{ conversationId: "sess-42", tool: "compress", args: { content: [] }, nativeCaller: true, toolCallId: "call-2" }]); assert.equal(oldProxy.toolCalls.length, 1, "nothing further may hit the dead origin"); } finally { if (prevEnv === undefined) delete process.env.BILLION_CONTEXT_PROXY; diff --git a/tests/plugin-call-identity.test.ts b/tests/plugin-call-identity.test.ts new file mode 100644 index 0000000000..7ea6970bf7 --- /dev/null +++ b/tests/plugin-call-identity.test.ts @@ -0,0 +1,165 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { once } from "node:events"; +import test, { after } from "node:test"; +import type { ResponseInputItem } from "acp-kernel/wire"; +import type { ProxyOptions } from "../src/config.ts"; +import { rmrf } from "./tmp-rm.ts"; + +const home = fs.mkdtempSync(path.join(os.tmpdir(), "bili-call-identity-")); +for (const key of ["XDG_DATA_HOME", "XDG_STATE_HOME", "XDG_CONFIG_HOME", "XDG_CACHE_HOME"]) process.env[key] = path.join(home, key); +process.env.BILI_CONFIG_FILE = path.join(home, "config.json"); +process.env.BILI_SESSIONS_DIR = path.join(home, "sessions"); +after(() => rmrf(home)); +const { defaultConfig } = await import("acp-kernel"); +const { startServer } = await import("../src/server.ts"); +const { SessionStore, _setStoreForTest } = await import("../src/persist.ts"); +const { _setForTest: setRegistryForTest } = await import("../src/registry.ts"); +const { _resetPluginStateForTest } = await import("../src/plugin.ts"); +const { listSessions, _resetSessionsForTest } = await import("../src/session.ts"); + +async function harness() { + const inputs: ResponseInputItem[][] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const body = JSON.parse(Buffer.concat(chunks).toString("utf8")) as { input?: ResponseInputItem[] }; + if (Array.isArray(body.input)) inputs.push(body.input); + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(`event: response.completed\ndata: ${JSON.stringify({ type: "response.completed", response: { id: "resp_identity", output: [], usage: { input_tokens: 1000, output_tokens: 1 } } })}\n\n`); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const origin = `http://127.0.0.1:${(upstream.address() as { port: number }).port}`; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + _resetSessionsForTest(); + _resetPluginStateForTest(); + const config = defaultConfig(200_000); + const opts: ProxyOptions = { + port: 0, host: "127.0.0.1", upstream: origin, + routes: { [origin]: { models: { "gpt-identity": { context: 200_000 } } } }, + modelContextLimit: 200_000, kernelConfig: config, + compress: { injectTool: true, injectNudge: false }, promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, + autoUpdate: false, autoRestartOnUpdate: false, updateTag: "latest", streamErrorShape: "protocol", + passthroughSource: null, advisoryCheck: false, releaseNotesCheck: false, + compat: { roles: {} }, mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + await once(proxy, "listening"); + const base = `http://127.0.0.1:${(proxy.address() as { port: number }).port}`; + const cid = "identity-plugin"; + const history: ResponseInputItem[] = Array.from({ length: 20 }, (_, i) => ({ type: "message", role: i % 2 === 0 ? "user" : "assistant", content: `identity seed ${i} ${"x".repeat(2000)}` })); + const post = async () => { + const r = await fetch(`${base}/bili/${origin}/v1/responses`, { + method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "pi", "x-bili-plugin-conversation": cid }, + body: JSON.stringify({ model: "gpt-identity", stream: true, instructions: "Stable identity fixture.", input: history }), + }); + assert.equal(r.status, 200, await r.text()); + return inputs.at(-1)!; + }; + const tool = async (body: Record) => { + const r = await fetch(`${base}/__bili/plugin/tool`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ conversationId: cid, nativeCaller: true, tool: "compress", ...body }) }); + return { status: r.status, data: await r.json() as { ok?: boolean; result?: string } }; + }; + return { inputs, history, post, tool, session: () => listSessions()[0]!, close: async () => { + await new Promise((resolve, reject) => proxy.close(e => e ? reject(e) : resolve())); + await new Promise((resolve, reject) => upstream.close(e => e ? reject(e) : resolve())); + } }; +} + +function appendPair(history: ResponseInputItem[], id: string, args: unknown, result: string) { + history.push({ type: "function_call", name: "compress", call_id: id, arguments: JSON.stringify(args) }); + history.push({ type: "function_call_output", call_id: id, output: result }); +} + +const args = { content: [{ startId: "m00002", endId: "m00007", topic: "identity", summary: "The earlier fixture messages establish the completed setup. Preserve the retained tail and use the original block when full detail is needed." }] }; + +test("native plugin compress stores the host call identity, not an unrelated synthetic identity", async () => { + const h = await harness(); + try { + await h.post(); + const out = await h.tool({ toolCallId: "call_host_success", args }); + assert.equal(out.status, 200); + assert.ok(h.session().state.blocks.some(b => b.active), out.data.result); + assert.equal(h.session().state.blocks.at(-1)?.compressCallId, "call_host_success"); + } finally { await h.close(); } +}); + +test("two later failures must not evict an active successful plugin carrier or move the cached prefix", async () => { + const h = await harness(); + try { + await h.post(); + const out = await h.tool({ toolCallId: "call_host_success", args }); + assert.ok(h.session().state.blocks.some(b => b.active), out.data.result); + appendPair(h.history, "call_host_success", args, out.data.result!); + const before = await h.post(); + for (let i = 0; i < 2; i++) appendPair(h.history, `call_failed_${i}`, { content: [] }, "[Compression FAILED: external summary unavailable.]"); + const afterInput = await h.post(); + assert.ok(afterInput.some(item => item.type === "function_call" && item.call_id === "call_host_success"), "still-active success must not be an orphan"); + assert.deepEqual(afterInput.slice(0, before.length), before, "ordinary failed calls must append, not resurrect an early summary anchor or delete the old carrier"); + assert.equal(h.session().state.blocks.filter(b => b.active).length, 1); + } finally { await h.close(); } +}); + +test("legacy synthetic blocks bind only to a unique resent carrier before orphan pruning", async () => { + const h = await harness(); + try { + await h.post(); + const out = await h.tool({ args }); + const block = h.session().state.blocks.at(-1)!; + assert.match(block.compressCallId!, /^plugin_/); + appendPair(h.history, "call_legacy_success", args, out.data.result!); + const before = await h.post(); + assert.equal(h.session().state.blocks.at(-1)?.compressCallId, "call_legacy_success"); + for (let i = 0; i < 2; i++) appendPair(h.history, `call_failed_${i}`, { content: [] }, "[Compression FAILED: external summary unavailable.]"); + const afterInput = await h.post(); + assert.deepEqual(afterInput.slice(0, before.length), before); + } finally { await h.close(); } +}); + +test("ambiguous historical calls must not be guessed as a legacy block's owner", async () => { + const h = await harness(); + try { + await h.post(); + const out = await h.tool({ args }); + const oldId = h.session().state.blocks.at(-1)!.compressCallId; + appendPair(h.history, "call_candidate_1", args, out.data.result!); + appendPair(h.history, "call_candidate_2", args, "[Compression FAILED: retry.]"); + await h.post(); + assert.equal(h.session().state.blocks.at(-1)?.compressCallId, oldId); + } finally { await h.close(); } +}); + +test("matching ranges with a failed or foreign block receipt cannot establish legacy ownership", async () => { + const h = await harness(); + try { + await h.post(); + await h.tool({ args }); + const oldId = h.session().state.blocks.at(-1)!.compressCallId; + appendPair(h.history, "call_foreign_receipt", args, "[Compressed m00002..m00007]\n · b999 full summary 100ch"); + await h.post(); + assert.equal(h.session().state.blocks.at(-1)?.compressCallId, oldId); + } finally { await h.close(); } +}); + +test("missing host call identity keeps legacy compatibility; malformed identity rejects before mutation", async () => { + const h = await harness(); + try { + await h.post(); + for (const toolCallId of [null, 23, "", " ", "x".repeat(513)]) { + const out = await h.tool({ toolCallId, args }); + assert.equal(out.status, 400, `invalid id ${String(toolCallId).slice(0, 15)}`); + assert.equal(h.session().state.blocks.length, 0); + } + const out = await h.tool({ args }); + assert.equal(out.status, 200); + assert.match(h.session().state.blocks.at(-1)?.compressCallId ?? "", /^plugin_/); + } finally { await h.close(); } +}); From 3793823d390ebc094b3ba8e0a13fa3a70ecd4e85 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Tue, 20 Oct 2026 05:57:10 +0800 Subject: [PATCH 5/8] fix: recognize externalSummary in COMPRESS_SETTING_FIELDS so misplaced placement warns (#3876) (cherry picked from commit 51cfbbad549a325fb578be255b8b32edce6ee01c) --- src/config.ts | 2 +- tests/issue1815-unknown-top-level-keys.test.ts | 12 ++++++++++++ website/config-reference/compress.yaml | 10 ++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/config.ts b/src/config.ts index f73522c306..2cb12aa5c1 100644 --- a/src/config.ts +++ b/src/config.ts @@ -2207,7 +2207,7 @@ const COMPRESS_SETTING_FIELDS = new Set([ "visibilityMarkers", "rules", "injectTool", "injectNudge", "acknowledgePromptsRisk", "absorb", "ccr", "search", "prompts", "promptPack", "reasoningGuard", "outputSteering", "priceProfile", - "reconcile", "streamSummary", + "reconcile", "streamSummary", "externalSummary", // #3635: summary-length caps — previously accepted nowhere, silently dropped. "maxSummaryLength", "minSummaryLength", ]); diff --git a/tests/issue1815-unknown-top-level-keys.test.ts b/tests/issue1815-unknown-top-level-keys.test.ts index 8e3ea49171..3a78e3e9e9 100644 --- a/tests/issue1815-unknown-top-level-keys.test.ts +++ b/tests/issue1815-unknown-top-level-keys.test.ts @@ -75,6 +75,18 @@ test("#1815: root-level promptPack (issue repro) warns through the real config-l } }); +test("#3876: bare top-level externalSummary gets the compress-placement hint", () => { + const { captured, stop } = captureLogs(); + try { + warnUnknownTopLevelKeys({ externalSummary: { enabled: true, targets: ["p/m"] } }); + const warns = unknownKeyWarns(captured); + assert.equal(warns.length, 1, `expected 1 warning, got: ${captured.map((e) => e.msg).join(" | ")}`); + assert.ok(warns[0].msg.includes('"externalSummary" belongs under "compress" (did you mean "compress.externalSummary"?)'), warns[0].msg); + } finally { + stop(); + } +}); + test("#1815: multiple unknown keys list together; fixing some re-warns about the rest", () => { const { captured, stop } = captureLogs(); try { diff --git a/website/config-reference/compress.yaml b/website/config-reference/compress.yaml index 677fb4235d..532cd5a055 100644 --- a/website/config-reference/compress.yaml +++ b/website/config-reference/compress.yaml @@ -355,6 +355,16 @@ entries: desc_zh: "w/r/q 相对输入价的比率(写入/缓存读/输出);仅供报告,不影响触发条件或线上行为。" site_en: "priceProfile — w / r / q ratios normalized to the input price (write / cache-read / output); unset ⇒ models.dev $/Mtok registry rows, kernel fallback {1, 0.1, 4}. Report-only — never affects triggers or the wire." site_zh: "priceProfile —— w / r / q 相对输入价的比率(写入/缓存读/输出);未设置 ⇒ 取 models.dev $/Mtok 注册表行,内核兜底 {1, 0.1, 4}。仅供报告,不影响触发条件或线上行为。" + - key: compress.externalSummary + type: object + default: "opt-in (disabled)" + group: compress + id: co.es + render: obj-item + desc_en: "Sends compression summaries to separately configured models referenced from the providers table; whole-chain replace semantics at all three compress levels." + desc_zh: "把压缩摘要交给 providers 表引用的独立配置模型;三级 compress 均为整链替换语义。" + site_en: "externalSummary — optional chain of external summary models, each target a \"provider/model\" reference into the providers table; disabled unless enabled is true, targets tried in order with fall-through (enabled, targets, budget {totalTimeoutMs, targetTimeoutMs, maxSummaryBytes}, concurrency, autoFold, autoFoldTargetTokens). Whole-chain replace semantics at all three compress levels." + site_zh: "externalSummary —— 可选的外部摘要模型链,每个目标为指向 providers 表的 \"provider/model\" 引用;enabled 为 true 才启用,目标按顺序尝试、失败落到下一个(enabled, targets, budget {totalTimeoutMs, targetTimeoutMs, maxSummaryBytes}, concurrency, autoFold, autoFoldTargetTokens)。三级 compress 均为整链替换语义。" - key: compress.acknowledgePromptsRisk type: boolean default: "false" From ca1c1ed3d2ee1d08f7336590d199543e5f00fb88 Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 18:03:51 +0800 Subject: [PATCH 6/8] fix: link anonymous forks to their parent so skipped fold-inheritance heals across a restart (#3925 -> #3011 C9) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Proxy-mode anonymous forks ('new session from here' with no session id) were detected at the first request (prefix-affinity LCP >= 3, via='new', reason='forked') but only fed into maybeAdoptForkBlocks — which bails silently when forkAdoption is off (the default). No lineage metadata was recorded, so neither the startup sweep (reconcileDerivedLineage iterates derivedFromSessionId entries only) nor the read-time decompress/search_context parent walk could ever recover the lost block state. The child replayed the whole parent history (including the parent's compress tool calls), context re-inflated, and search_context reported 'no compressed blocks'. - record derivedFrom/derivedFromSessionId on every detected anonymous fork whose parent loads (memory or disk), before the outcome branches, and make it durable immediately (writeNow) like persistLineageLinkNow (#2986); a bili restart then lets the existing sweep inherit the parent's fully- present blocks, and read-time search/decompress fall back to the parent chain for free - on the disabled bail, persist a forkAdoptionSkipped marker {parentId, blocks, tok} and upgrade the log to warn so the degradation is visible - acp_status (no-scope views) prints one FORK ADOPTION SKIPPED line naming the parent, the block/tok counts and how to enable adoption; suppressed once the child has any active block; malformed markers are ignored - success path sets derivedBlocksAdopted = true (#3351 discipline) Behavior changes (old -> new): - anon fork children: no persistent lineage -> persistent parent link even when adoption is off (enables restart healing + read-time fallback) - bail log: info -> warn - acp_status: gains the conditional warning line (additive display) - success path: derivedBlocksAdopted now set (sweep skip already held via blocks > 0; marker keeps the gate consistent) The forkAdoption default itself is deliberately untouched here — flipping it is a product decision tracked in #3011. Refs #3925, #3011, #3025 (cherry picked from commit 703f16d03de65061447b2883e8e2975ea1945ade) --- src/acp-status.ts | 12 ++++++++ src/fork-adoption.ts | 61 +++++++++++++++++++++++++++++++++++-- tests/acp-status.test.ts | 22 +++++++++++++ tests/fork-adoption.test.ts | 25 +++++++++++++-- 4 files changed, 115 insertions(+), 5 deletions(-) diff --git a/src/acp-status.ts b/src/acp-status.ts index 453007e7f1..79173aa326 100644 --- a/src/acp-status.ts +++ b/src/acp-status.ts @@ -21,6 +21,7 @@ import { getUnrecognizedPathStats } from "./server/observability.js"; import { ccrEnabled, ccrLoopConfig, contentStoreOf } from "./store.js"; import { coveredRefSpan } from "./decompress-shared.js"; import { describeUncoveredRuns, shortTokenCount, uncoveredHistoryRuns } from "./uncovered.js"; +import { forkAdoptionSkippedOf } from "./fork-adoption.js"; import { preCompactionArchiveOf, statusInputBaseline, type Session } from "./session.js"; import { compressBreakerDetail, compressLastFailureCause } from "./stream.js"; import { describeAdvisory, getAdvisoryState } from "./advisory.js"; @@ -182,6 +183,17 @@ export function handleAcpStatus(args: Record, ctx: AcpStatusCtx }); if (scope) return toolOk(base); const extra: string[] = []; + // #3925 → #3011 C9: a fork that started with zero inherited blocks used to + // be invisible here — "COMPRESSED BLOCKS — none" reads as "nothing folded + // yet", not "inheritance was skipped". Name the gap and its recovery path: + // the lineage link recorded at fork detection means a bili restart lets the + // startup sweep inherit the parent's blocks. Suppressed once any active + // block exists (the gap is closed by then, however it was closed). + const forkSkipped = forkAdoptionSkippedOf(ctx.session); + if (forkSkipped && !ctx.session.state.blocks.some((b) => b.active)) { + extra.push(""); + extra.push(`FORK ADOPTION SKIPPED — this session forked from ${forkSkipped.parentId} with ${forkSkipped.blocks} parent block(s) (~${Math.round(forkSkipped.tokens / 1000)}K tokens) NOT inherited because forkAdoption is disabled; the parent's already-folded history replays raw and will re-fold from scratch. Set "forkAdoption": true (or BILI_FORK_ADOPTION=1) and restart bili — the recorded lineage link lets the startup sweep inherit those blocks.`); + } // #2366: everything in the report above is LOCAL ESTIMATES (defaultCountTokens); // on CJK-heavy routes upstream billing runs 2–4× higher, so an agent judging // pressure from those figures misreads a filling window as healthy and folds diff --git a/src/fork-adoption.ts b/src/fork-adoption.ts index 3d15fffdf1..e6cfa66b2b 100644 --- a/src/fork-adoption.ts +++ b/src/fork-adoption.ts @@ -296,11 +296,52 @@ export function applyForkAdoption(session: Session, plan: ForkAdoptionPlan, pare markDirty(session); } +/** #3925 → #3011 C9: what acp_status renders while a fork runs with zero + * inherited blocks — the "starting fresh despite an adoptable parent" gap. */ +export interface ForkAdoptionSkipped { + parentId: string; + blocks: number; + tokens: number; +} + +/** Typed read of the forkAdoptionSkipped metadata field (session.metadata is + * a free Record; validate the shape on the way out). */ +export function forkAdoptionSkippedOf(session: Session): ForkAdoptionSkipped | undefined { + const raw = session.metadata.forkAdoptionSkipped; + if (typeof raw !== "object" || raw === null) return undefined; + const r = raw as Record; + if (typeof r.parentId !== "string" || typeof r.blocks !== "number" || typeof r.tokens !== "number") return undefined; + return { parentId: r.parentId, blocks: r.blocks, tokens: r.tokens }; +} + +/** #2986/#3925: the lineage link must be durable BEFORE the next request — + * markDirty alone persists on the debounce cadence, and a non-graceful exit + * inside that window strands the child unlinked forever (the boot sweep and + * the read-time decompress/search fallback both key off this link). Fire- + * and-forget on purpose: this runs on the model-request hot path, so we must + * not stall the response (mirrors persistLineageLinkNow in server/handle.ts). */ +function persistForkLineageNow(session: Session, log: (level: string, msg: string) => void): void { + void getStore().writeNow(session).catch((err) => { + log("warn", `[fork-adoption] lineage durable write failed for ${session.id}: ${String(err)} (#2986)`); + }); +} + /** Server hook: on a fresh anonymous fork (via "new" + lineage "forked"), * find the parent (memory first, then disk), measure what is adoptable, and * seed the fresh session when enabled. Always logs the adoptable inventory — * the measurement ework asked for in #629 — so operators can size the win - * before turning adoption on. */ + * before turning adoption on. + * + * #3925 → #3011 C9: records the persistent derivedFromSessionId link at + * detection time whatever the adoption outcome — the same unconditional link + * #1333 writes for explicitly derived conversations. Without it a bail left + * NO trace: the boot sweep (reconcileDerivedLineage) only iterates sessions + * carrying derivedFromSessionId, and decompress/search_context's read-time + * parent fallback (derivedAncestorSessions) walks that same link — so an + * unlinked child is never healed across a restart and its search stays blind + * for life. The link copies nothing at link time; adoption still judges per + * block in raw-id set semantics, so a mis-guessed parent can at most be + * searched read-only, never adopted from. */ export function maybeAdoptForkBlocks(args: { session: Session; parentId: string; @@ -323,15 +364,29 @@ export function maybeAdoptForkBlocks(args: { return; } const plan = planForkAdoption(parent, new Set(incomingMsgs.map((m) => m.id))); + // Link once the parent is confirmed loadable (a dangling link to a + // vanished id would only churn the read-time walk for nothing). + const linked = session.metadata.derivedFromSessionId === undefined; + if (linked) { + session.metadata.derivedFrom = parentId; + session.metadata.derivedFromSessionId = parentId; + } if (plan.adoptedActive === 0) { - log("info", `[fork-adoption] fork of ${parentId}: no fully-present blocks to adopt (${plan.straddled} straddle the fork point); starting fresh (#629)`); + if (linked) persistForkLineageNow(session, log); + log("info", `[fork-adoption] fork of ${parentId}: no fully-present blocks to adopt (${plan.straddled} straddle the fork point); starting fresh; lineage linked so the startup sweep can inherit later folds (#629/#3925)`); return; } if (!enabled) { - log("info", `[fork-adoption] fork of ${parentId}: parent has ${plan.adoptedActive} adoptable block(s) covering ~${plan.adoptedTokens} tokens; forkAdoption disabled, starting fresh (set forkAdoption: true or BILI_FORK_ADOPTION=1)`); + session.metadata.forkAdoptionSkipped = { parentId, blocks: plan.adoptedActive, tokens: plan.adoptedTokens }; + persistForkLineageNow(session, log); + log("warn", `[fork-adoption] fork of ${parentId}: parent has ${plan.adoptedActive} adoptable block(s) covering ~${plan.adoptedTokens} tokens; forkAdoption disabled, starting fresh (set forkAdoption: true or BILI_FORK_ADOPTION=1). Lineage linked — a bili restart lets the startup sweep inherit those blocks, and acp_status shows the gap (#3925)`); return; } applyForkAdoption(session, plan, parent); + // #3351 discipline: close the derived-blocks gate ONLY on real block + // adoption — the boot-sweep retry door stays open otherwise. + session.metadata.derivedBlocksAdopted = true; + persistForkLineageNow(session, log); log("info", `[fork-adoption] session ${session.id} adopted ${plan.adoptedActive} block(s) (~${plan.adoptedTokens} tokens, refs up to ${plan.maxRef || "n/a"}) from parent ${parentId} across fork (#629)`); // #1341: carry the originals behind the refs the fork now advertises as // retrievable — covered messages of the adopted blocks ∪ refs cited by diff --git a/tests/acp-status.test.ts b/tests/acp-status.test.ts index e9460ed8c2..99b4fd6aca 100644 --- a/tests/acp-status.test.ts +++ b/tests/acp-status.test.ts @@ -225,3 +225,25 @@ test("acp_status marks host-native compaction landings per session (#4458)", () assert.ok(outDone.includes("rebased onto it at"), "rebased state surfaced"); assert.ok(outDone.includes("(codex)"), "codex lane resolved from clientHint"); }); + +test("#3925: acp_status names a skipped fork adoption and stays quiet once blocks exist", () => { + const ctx = makeCtx12(); + assert.ok(!handleAcpStatus({}, ctx).text.includes("FORK ADOPTION SKIPPED"), "no line without the marker (negative gate)"); + + ctx.session.metadata.forkAdoptionSkipped = { parentId: "pfa-parent-1", blocks: 7, tokens: 431_500 }; + const skipped = handleAcpStatus({}, ctx).text; + assert.ok(skipped.includes("FORK ADOPTION SKIPPED"), "line appears while zero active blocks"); + assert.ok(skipped.includes("pfa-parent-1"), "line names the parent session"); + assert.ok(skipped.includes("7 parent block(s)"), "line names the skipped inventory"); + assert.ok(skipped.includes("BILI_FORK_ADOPTION=1"), "line carries the recovery knob"); + + // Malformed marker must not render (shape validation on the way out). + ctx.session.metadata.forkAdoptionSkipped = { parentId: 42 }; + assert.ok(!handleAcpStatus({}, ctx).text.includes("FORK ADOPTION SKIPPED"), "malformed marker renders nothing"); + + // Once any active block exists the gap is closed — line goes away. + ctx.session.metadata.forkAdoptionSkipped = { parentId: "pfa-parent-1", blocks: 7, tokens: 431_500 }; + applyRanges(parseCompressInput(JSON.parse(COMPRESS_ARGS)), ctx); + assert.ok(ctx.session.state.blocks.some((b) => b.active), "fixture sanity: an active block now exists"); + assert.ok(!handleAcpStatus({}, ctx).text.includes("FORK ADOPTION SKIPPED"), "suppressed once blocks exist"); +}); diff --git a/tests/fork-adoption.test.ts b/tests/fork-adoption.test.ts index 97b897b67d..47630243fa 100644 --- a/tests/fork-adoption.test.ts +++ b/tests/fork-adoption.test.ts @@ -6,6 +6,7 @@ import { defaultConfig } from "acp-kernel"; import { startServer } from "../src/server.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { listSessions } from "../src/session.ts"; +import { forkAdoptionSkippedOf } from "../src/fork-adoption.ts"; import type { ProxyOptions } from "../src/config.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; @@ -183,7 +184,7 @@ function proxyOpts(relayUrl: string, forkAdoption: boolean): ProxyOptions { }; } -async function runForkScenario(forkAdoption: boolean): Promise<{ parentBlocks: number; parentBlockIds: string[]; childBlockIds: string[]; childBlocks: number; childContents: number; lineage: string | undefined }> { +async function runForkScenario(forkAdoption: boolean): Promise<{ parentId: string; parentBlocks: number; parentBlockIds: string[]; childBlockIds: string[]; childBlocks: number; childContents: number; lineage: string | undefined; derivedFromSessionId: string | undefined; derivedBlocksAdopted: boolean; forkAdoptionSkipped: ReturnType }> { const run = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; _setStoreForTest(new SessionStore({ enabled: false })); setRegistryForTest({}); @@ -243,6 +244,11 @@ async function runForkScenario(forkAdoption: boolean): Promise<{ parentBlocks: n childBlocks: child.state.blocks.filter((b) => b.active).length, childContents: child.state.blocks.filter((b) => child.blockContents.has(b.blockId)).length, lineage: (child.metadata.anonymousPrefixAffinity as { lineage?: { reason?: string } } | undefined)?.lineage?.reason, + // #3925 → #3011 C9: the persistent lineage link + adoption markers + // must be on the child regardless of the adoption outcome. + derivedFromSessionId: typeof child.metadata.derivedFromSessionId === "string" ? child.metadata.derivedFromSessionId : undefined, + derivedBlocksAdopted: child.metadata.derivedBlocksAdopted === true, + forkAdoptionSkipped: forkAdoptionSkippedOf(child), }; assert.equal(snapshot.lineage, "forked", "child lineage must record the fork"); @@ -268,7 +274,7 @@ async function runForkScenario(forkAdoption: boolean): Promise<{ parentBlocks: n assert.ok(followUp.length > 0, "fork follow-up must produce a reply"); assert.equal(parent.state.blocks.length, parentIdCount, "parent blocks must be untouched (copy-on-fork)"); - return { ...snapshot, parentBlockIds: [...snapshot.parentBlockIds], childBlockIds: [...snapshot.childBlockIds] }; + return { ...snapshot, parentId: parent.id, parentBlockIds: [...snapshot.parentBlockIds], childBlockIds: [...snapshot.childBlockIds] }; } finally { await close(proxy); await close(relay); @@ -281,6 +287,11 @@ test("anonymous fork adopts the parent's fully-present compression blocks (#629) assert.equal(result.childContents, result.childBlocks, "adopted blocks must carry their summaries"); const adopted = result.parentBlockIds.filter((id) => result.childBlockIds.includes(id)); assert.ok(adopted.length >= 1, `child must carry the parent's block id (parent=${JSON.stringify(result.parentBlockIds)} child=${JSON.stringify(result.childBlockIds)})`); + // #3925 → #3011 C9: the lineage link is recorded and the derived-blocks + // gate closes on real adoption; nothing is marked skipped. + assert.equal(result.derivedFromSessionId, result.parentId, "adopting child must link its parent persistently"); + assert.equal(result.derivedBlocksAdopted, true, "derived-blocks gate must close on real block adoption (#3351)"); + assert.equal(result.forkAdoptionSkipped, undefined, "no skipped marker when adoption succeeded"); }); test("forkAdoption disabled leaves the fork at zero compression state", async () => { @@ -288,4 +299,14 @@ test("forkAdoption disabled leaves the fork at zero compression state", async () assert.equal(result.lineage, "forked", "fork lineage is still recorded"); assert.equal(result.childBlocks, 0, "disabled adoption must not seed blocks"); assert.equal(result.childContents, 0, "disabled adoption must not seed summaries"); + // #3925 → #3011 C9: even when adoption bails, the persistent lineage link + // must be recorded — without it the startup sweep can never heal the child + // across a restart and decompress/search_context stay parent-blind for + // life. The skipped marker carries what was left on the table. + assert.equal(result.derivedFromSessionId, result.parentId, "bailed fork must still link its parent persistently"); + assert.equal(result.derivedBlocksAdopted, false, "bailed fork must NOT close the derived-blocks gate (boot-sweep door stays open)"); + assert.ok(result.forkAdoptionSkipped, "bailed fork must record the skipped-adoption marker for acp_status"); + assert.equal(result.forkAdoptionSkipped?.parentId, result.parentId, "skipped marker must name the parent"); + assert.ok((result.forkAdoptionSkipped?.blocks ?? 0) >= 1, "skipped marker must count the adoptable blocks that were left behind"); + assert.ok((result.forkAdoptionSkipped?.tokens ?? 0) > 0, "skipped marker must carry the token inventory"); }); From f13411c2634af484b7b4eb0422fe78c22d649fd3 Mon Sep 17 00:00:00 2001 From: awork Date: Tue, 20 Oct 2026 18:24:13 +0800 Subject: [PATCH 7/8] fix: openai adapter treats empty tool_calls array as absent (#3926) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Gateways that stamp every chunk with `tool_calls: []` (WorkBuddy / DeepSeek-family private deployments) routed all frames into the tool-frame branch: text events lost their raw passthrough, round-1 forwarding in core.ts found nothing to emit, and the settle-time prose rescue early-returned with pending empty — ALL round-1 text was silently dropped while finish_reason/usage looked healthy. Normalize an empty (or non-array) tool_calls field to absent at the single classification point so such frames take the normal text path. Non-empty arrays keep the tool-frame path unchanged. Regression pins (tests/issue3926-empty-tool-calls.test.ts): adapter-level exact-once round-1 text + no fabricated tool calls; gate negative row for non-empty arrays; loop-level client-boundary end-to-end pin. Verified the new tests fail on the unfixed code and pass on this one. Fixes #3926 (cherry picked from commit d2e53210489777e6860b40b6e94b4df7c71b2d1c) --- tests/issue3926-empty-tool-calls.test.ts | 154 +++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 tests/issue3926-empty-tool-calls.test.ts diff --git a/tests/issue3926-empty-tool-calls.test.ts b/tests/issue3926-empty-tool-calls.test.ts new file mode 100644 index 0000000000..1b23aed17d --- /dev/null +++ b/tests/issue3926-empty-tool-calls.test.ts @@ -0,0 +1,154 @@ +// #3926: gateways that stamp EVERY chunk with `tool_calls: []` (WorkBuddy / +// DeepSeek-family private deployments) routed every frame into the tool-frame +// branch of the openai adapter: text events lost their raw passthrough, core +// forwarded nothing in round 1, and the settle-time prose rescue (#1881/#1882) +// early-returned because an empty array absorbs no pending entries — ALL +// round-1 text was silently dropped while finish_reason/usage looked healthy. +// Pins: an empty array takes the normal text path (raw passthrough, exact-once, +// no fabricated tool calls); a NON-empty array keeps the tool-frame path +// (gate negative row). +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { createCore, createInitialState, defaultConfig } from "acp-kernel"; +import type { Config, CoreMessage } from "acp-kernel"; +import type { Session } from "../src/session.ts"; +import { createOpenaiAdapter, runCompressLoop } from "../src/loop/index.ts"; +import { buildCompressSystemPrompt } from "../src/compress-tool.ts"; +import type { CompressLoopAdapter, ParsedStreamEvent } from "../src/loop/index.ts"; +import type { WireProtocol } from "../src/util.ts"; + +async function collect(adapter: CompressLoopAdapter, body: string): Promise { + const events: ParsedStreamEvent[] = []; + for await (const ev of adapter.parseStream(new Response(body, { status: 200 }).body!, 1)) events.push(ev); + return events; +} + +// Everything that reaches the client: raw payloads on structured events plus +// whole-frame chunks on meta events. +function clientBytes(events: ParsedStreamEvent[]): string { + return events.map((e) => { + const b = e as { raw?: Buffer; chunk?: Buffer }; + return (b.raw ?? b.chunk)?.toString("utf8") ?? ""; + }).join(""); +} + +function sse(obj: unknown): string { + return `data: ${JSON.stringify(obj)}\n\n`; +} + +// WorkBuddy upstream shape (#3926 evidence): every delta carries an EMPTY +// tool_calls array plus empty sibling fields. +const wbDelta = (content: string, extra: Record = {}) => ({ + content, + reasoning_content: "", + function_call: null, + refusal: "", + tool_calls: [], + extra_fields: null, + ...extra, +}); + +test("#3926 openai: empty tool_calls chunks stream their round-1 text to the client", async () => { + const adapter = createOpenaiAdapter({ model: "gpt" }); + const events = await collect( + adapter, + sse({ choices: [{ index: 0, delta: wbDelta("", { role: "assistant" }), finish_reason: null }] }) + + sse({ choices: [{ index: 0, delta: wbDelta("9"), finish_reason: null }] }) + + sse({ choices: [{ index: 0, delta: wbDelta("+"), finish_reason: null }] }) + + sse({ choices: [{ index: 0, delta: wbDelta(""), finish_reason: "stop" }], usage: { prompt_tokens: 1, completion_tokens: 2 } }) + + "data: [DONE]\n\n", + ); + const out = clientBytes(events); + assert.ok(out.includes(`"role":"assistant"`), `the role framing chunk still reaches the client, got: ${out}`); + assert.equal((out.match(/"content":"9"/g) ?? []).length, 1, `round-1 text "9" reaches the client exactly once, got: ${out}`); + assert.equal((out.match(/"content":"\+"/g) ?? []).length, 1, `round-1 text "+" reaches the client exactly once, got: ${out}`); + assert.ok(!events.some((e) => e.kind === "tool_call"), `an empty array must not fabricate tool calls: ${JSON.stringify(events.map((e) => e.kind))}`); + const done = events.find((e) => e.kind === "done"); + assert.equal(done?.kind === "done" && done.finishReason, "stop", "finish_reason preserved as-is"); +}); + +// Gate negative row (#3617): the normalization must not swallow REAL tool +// calls — a non-empty array keeps the tool-frame path verbatim. +test("#3926 openai gate: a non-empty tool_calls array still takes the tool-frame path", async () => { + const adapter = createOpenaiAdapter({ model: "gpt" }); + const args = JSON.stringify({ city: "SF" }); + const events = await collect( + adapter, + sse({ choices: [{ index: 0, delta: { role: "assistant", tool_calls: [{ index: 0, id: "call_1", type: "function", function: { name: "get_weather", arguments: args } }] }, finish_reason: null }] }) + + sse({ choices: [{ index: 0, delta: {}, finish_reason: "tool_calls" }] }) + + "data: [DONE]\n\n", + ); + const out = clientBytes(events); + assert.equal((out.match(/get_weather/g) ?? []).length, 1, `the real call reaches the client verbatim, got: ${out}`); + // args is a STRING field of the frame: byte-exact means its JSON-escaped + // form inside the frame equals JSON.stringify(args) (#1039). + assert.ok(out.includes(JSON.stringify(args)), `arguments stay byte-exact (#1039): ${out}`); + const tc = events.find((e) => e.kind === "tool_call"); + assert.ok(tc?.kind === "tool_call" && tc.name === "get_weather", "structured passthrough event"); + const done = events.find((e) => e.kind === "done"); + assert.equal(done?.kind === "done" && done.finishReason, "tool_calls", "finish_reason unchanged"); +}); + +function makeLoopCtx(): { + core: ReturnType; + config: Config; + messages: CoreMessage[]; + session: Session; + log: (message: string) => void; + protocol?: WireProtocol; +} { + return { + core: createCore(), + config: defaultConfig(200000), + messages: [], + session: { + id: "issue3926-empty-tool-calls-loop", + 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 }, + pendingRetrievals: [], + metadata: {}, + state: createInitialState(), + createdAt: Date.now(), + lastSeen: Date.now(), + blockContents: new Map(), + inFlight: 0, + persisted: false, + }, + log: () => {}, + protocol: "openai", + }; +} + +// Loop-level pin: the user-visible symptom was at the client boundary — the +// adapter now emits raw-carrying text events, but only core.ts decides what +// round-1 forwards. This drives the full loop against the WorkBuddy-shaped +// stream and asserts the streamed text actually lands in the client output. +test("#3926 openai loop: empty tool_calls stream delivers round-1 text end-to-end", async () => { + const enc = new TextEncoder(); + const frame = (delta: Record, finishReason: string | null = null, usage?: Record) => + enc.encode(`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", created: 1, model: "gpt", choices: [{ index: 0, delta, finish_reason: finishReason }], ...(usage ? { usage } : {}) })}\n\n`); + const stream = new ReadableStream({ + start(controller) { + controller.enqueue(frame(wbDelta("", { role: "assistant" }))); + controller.enqueue(frame(wbDelta("9"))); + controller.enqueue(frame(wbDelta("+"))); + controller.enqueue(frame(wbDelta(""), "stop", { prompt_tokens: 1, completion_tokens: 2 })); + controller.enqueue(enc.encode(`data: [DONE]\n\n`)); + controller.close(); + }, + }); + const chunks: Buffer[] = []; + for await (const chunk of runCompressLoop( + stream, + makeLoopCtx(), + { model: "gpt", stream: true }, + { url: "http://mock", headers: {} }, + createOpenaiAdapter({ model: "gpt" }), + buildCompressSystemPrompt(), + )) chunks.push(chunk); + const output = Buffer.concat(chunks).toString("utf8"); + assert.equal((output.match(/"content":"9"/g) ?? []).length, 1, `streamed round-1 text "9" reaches the client exactly once, got: ${output}`); + assert.equal((output.match(/"content":"\+"/g) ?? []).length, 1, `streamed round-1 text "+" reaches the client exactly once, got: ${output}`); + assert.match(output, /"finish_reason":"stop"/); + assert.match(output, /\[DONE\]/); +}); From 4d34abfafc29f89a268291a7125e35b5537f8ef2 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sat, 24 Oct 2026 23:56:10 +0800 Subject: [PATCH 8/8] =?UTF-8?q?fix(gates):=20repair=20the=20merge-ref=20ca?= =?UTF-8?q?sualties=20=E2=80=94=20config-surface=20guard=20locates=20the?= =?UTF-8?q?=20Set=20definition=20(master-red=20too,=20#3675=20comment=20tr?= =?UTF-8?q?ipped=20indexOf),=20de-export=203=20internal-only=20symbols,=20?= =?UTF-8?q?regenerate=20derived=20config=20docs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CONFIGURATION.md | 1 + CONFIGURATION.zh-CN.md | 1 + package-lock.json | 2 +- scripts/config-surface-guard.mjs | 8 +++++++- src/fold-decomposition-lookup.ts | 2 +- src/fork-adoption.ts | 2 +- src/server.ts | 2 +- website/docs/index.html | 1 + website/main.js | 2 ++ 9 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 09a96fb9ea..db5db50de5 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -289,6 +289,7 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `compress.reasoningGuard` | object | off | — | Auto-repair of reasoning-lattice truncation on gpt-5.x/6.x (up to 3 continue-nudge rounds). | | `compress.outputSteering` | object { enabled?, verbosityLevel?, effortRouting? } | enabled false · verbosityLevel 2 | — | Appends a conciseness directive to the system-prompt tail; clamps mechanical continuation requests. | | `compress.priceProfile` | { w?, r?, q? } | unset (registry, kernel fallback {1, 0.1, 4}) | — | w/r/q ratios normalized to the input price (write/cache-read/output); report-only — never affects triggers or the wire. | +| `compress.externalSummary` | object | opt-in (disabled) | — | Sends compression summaries to separately configured models referenced from the providers table; whole-chain replace semantics at all three compress levels. | | `compress.acknowledgePromptsRisk` | boolean | false | — | Must be true before custom prompts take effect. | | `compress.absorb.enabled` | boolean | false | — | Enable the absorb distillation block. | | `compress.absorb.minToolTokens` | number | 1000 | — | Minimum estimated token size of a tool result eligible for absorption. | diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 56d9cf8280..83046c0e43 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -289,6 +289,7 @@ | `compress.reasoningGuard` | object | off | — | gpt-5.x/6.x 推理格截断自动修复(最多 3 轮继续提示)。 | | `compress.outputSteering` | object { enabled?, verbosityLevel?, effortRouting? } | enabled false · verbosityLevel 2 | — | 向系统提示词尾部附加简洁度指令;只收紧机械延续请求。 | | `compress.priceProfile` | { w?, r?, q? } | unset (registry, kernel fallback {1, 0.1, 4}) | — | w/r/q 相对输入价的比率(写入/缓存读/输出);仅供报告,不影响触发条件或线上行为。 | +| `compress.externalSummary` | object | opt-in (disabled) | — | 把压缩摘要交给 providers 表引用的独立配置模型;三级 compress 均为整链替换语义。 | | `compress.acknowledgePromptsRisk` | boolean | false | — | 必须先置 true,自定义 prompts 才会生效。 | | `compress.absorb.enabled` | boolean | false | — | 启用 absorb 蒸馏块。 | | `compress.absorb.minToolTokens` | number | 1000 | — | 可被吸收的工具结果的最小估算 token 大小。 | diff --git a/package-lock.json b/package-lock.json index b8ec13dff5..ad31ac56d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.162", + "version": "0.0.163", "dev": true, "license": "MIT", "devDependencies": { diff --git a/scripts/config-surface-guard.mjs b/scripts/config-surface-guard.mjs index 1865dfcafe..da36098e7d 100644 --- a/scripts/config-surface-guard.mjs +++ b/scripts/config-surface-guard.mjs @@ -134,7 +134,13 @@ function namedTypeBodies(src) { // Extract the quoted identifiers of `const = new Set([ ... ]);`. export function extractStringSet(src, varName) { - const i = src.indexOf(varName); + // Locate the DEFINITION (`const = new Set([`), not the first textual + // mention: doc comments legitimately reference the identifier above its + // definition (#3675's header did exactly that and fed the extractor the + // wrong bracket window — the join(", ") in unrelated code parsed as a + // leaf named ", "). + const def = src.indexOf(varName + " = new Set(["); + const i = def >= 0 ? def : src.indexOf(varName); if (i < 0) throw new Error(varName + " not found"); const open = src.indexOf("[", i); const close = src.indexOf("]", open); diff --git a/src/fold-decomposition-lookup.ts b/src/fold-decomposition-lookup.ts index 6ba7e7bc85..543e6b160b 100644 --- a/src/fold-decomposition-lookup.ts +++ b/src/fold-decomposition-lookup.ts @@ -23,7 +23,7 @@ export interface StoredRow { content: string; } -export interface DecompositionSource { +interface DecompositionSource { /** null = no index available (legacy/shadow/degraded); [] = index has none of them. */ rows(sessionId: string, msgIds: readonly string[]): StoredRow[] | null; } diff --git a/src/fork-adoption.ts b/src/fork-adoption.ts index e6cfa66b2b..8bc295b144 100644 --- a/src/fork-adoption.ts +++ b/src/fork-adoption.ts @@ -298,7 +298,7 @@ export function applyForkAdoption(session: Session, plan: ForkAdoptionPlan, pare /** #3925 → #3011 C9: what acp_status renders while a fork runs with zero * inherited blocks — the "starting fresh despite an adoptable parent" gap. */ -export interface ForkAdoptionSkipped { +interface ForkAdoptionSkipped { parentId: string; blocks: number; tokens: number; diff --git a/src/server.ts b/src/server.ts index df8150333c..2dd74daee8 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1926,7 +1926,7 @@ const SHUTDOWN_DRAIN_MAX_PASSES = 10; // genuinely dead parent still exits promptly. const WATCHER_DEAD_CONFIRM_TICKS = 3; -export interface WatcherTickDecision { +interface WatcherTickDecision { /** Remove this pid from the watcher set on this tick. */ removed: boolean; /** Removed because the heartbeat expired while the pid still reads alive (#3544). */ diff --git a/website/docs/index.html b/website/docs/index.html index 0534169cfd..f536028c6f 100644 --- a/website/docs/index.html +++ b/website/docs/index.html @@ -218,6 +218,7 @@

compress — every knob

  • reasoningGuard — auto-repair of reasoning-lattice truncation on gpt-5.x/6.x (≤3 continue-nudge rounds, marker "Continue thinking...") (enabled, maxContinue, maxTierN, markerText, base, offset, debugLog).
  • outputSteering — { enabled? false, verbosityLevel? 2 (0–4), effortRouting? }: appends a conciseness directive to the system-prompt tail; clamps mechanical continuation requests.
  • priceProfile — w / r / q ratios normalized to the input price (write / cache-read / output); unset ⇒ models.dev $/Mtok registry rows, kernel fallback {1, 0.1, 4}. Report-only — never affects triggers or the wire.
  • +
  • externalSummary — optional chain of external summary models, each target a "provider/model" reference into the providers table; disabled unless enabled is true, targets tried in order with fall-through (enabled, targets, budget {totalTimeoutMs, targetTimeoutMs, maxSummaryBytes}, concurrency, autoFold, autoFoldTargetTokens). Whole-chain replace semantics at all three compress levels.
  • acknowledgePromptsRisk — must be true before custom prompts take effect.
  • diff --git a/website/main.js b/website/main.js index 5fd333c529..de092ebcba 100644 --- a/website/main.js +++ b/website/main.js @@ -96,6 +96,7 @@ "co.rg": "reasoningGuard — auto-repair of reasoning-lattice truncation on gpt-5.x/6.x (≤3 continue-nudge rounds, marker \"Continue thinking...\") (enabled, maxContinue, maxTierN, markerText, base, offset, debugLog).", "co.os": "outputSteering — { enabled? false, verbosityLevel? 2 (0–4), effortRouting? }: appends a conciseness directive to the system-prompt tail; clamps mechanical continuation requests.", "co.pp": "priceProfile — w / r / q ratios normalized to the input price (write / cache-read / output); unset ⇒ models.dev $/Mtok registry rows, kernel fallback {1, 0.1, 4}. Report-only — never affects triggers or the wire.", + "co.es": "externalSummary — optional chain of external summary models, each target a \"provider/model\" reference into the providers table; disabled unless enabled is true, targets tried in order with fall-through (enabled, targets, budget {totalTimeoutMs, targetTimeoutMs, maxSummaryBytes}, concurrency, autoFold, autoFoldTargetTokens). Whole-chain replace semantics at all three compress levels.", "co.apr": "acknowledgePromptsRisk — must be true before custom prompts take effect.", "cr.models": "Record of model name → {context?, output?, compress?, benefit?}: declares each model's context window, output ceiling, and per-model compression overrides (benefit marks free-quota billing). Not a traffic filter.", "cr.proxy": "Per-route outbound proxy URL; empty string = explicitly direct.", @@ -340,6 +341,7 @@ "co.rg": "reasoningGuard —— gpt-5.x/6.x 推理格截断自动修复(≤3 轮继续提示,标记 \"Continue thinking...\")(enabled, maxContinue, maxTierN, markerText, base, offset, debugLog)。", "co.os": "outputSteering —— { enabled? false, verbosityLevel? 2 (0–4), effortRouting? }:向系统提示词尾部附加简洁度指令;只收紧机械延续请求。", "co.pp": "priceProfile —— w / r / q 相对输入价的比率(写入/缓存读/输出);未设置 ⇒ 取 models.dev $/Mtok 注册表行,内核兜底 {1, 0.1, 4}。仅供报告,不影响触发条件或线上行为。", + "co.es": "externalSummary —— 可选的外部摘要模型链,每个目标为指向 providers 表的 \"provider/model\" 引用;enabled 为 true 才启用,目标按顺序尝试、失败落到下一个(enabled, targets, budget {totalTimeoutMs, targetTimeoutMs, maxSummaryBytes}, concurrency, autoFold, autoFoldTargetTokens)。三级 compress 均为整链替换语义。", "co.apr": "acknowledgePromptsRisk —— 必须先置 true,自定义 prompts 才会生效。", "cr.models": "模型名 → {context?, output?, compress?, benefit?} 的记录:声明每个模型的上下文窗口、输出上限与逐模型压缩覆盖(benefit 标记免费额度计费)。不是流量过滤器。", "cr.proxy": "本路由出站代理地址;空字符串 = 显式直连。",