From 00a1f2432d0bfb14c19acdee6c8ec6de7cdce4d3 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 15 Oct 2026 20:22:37 +0800 Subject: [PATCH] fix: suppress DSH model-side compress surface (host-scope tool unreachable) (#3177) --- src/server/inject.ts | 3 +- src/server/prepare-anthropic.ts | 6 ++-- src/server/prepare-google.ts | 6 ++-- src/server/prepare-openai.ts | 11 ++++-- src/server/prepare-responses.ts | 11 ++++-- tests/issue2432-dsh-local-compaction.test.ts | 5 ++- tests/plugin-nudge-injection.test.ts | 38 ++++++++++++++++---- 7 files changed, 63 insertions(+), 17 deletions(-) diff --git a/src/server/inject.ts b/src/server/inject.ts index 935e3dfb7..b9f0f9407 100644 --- a/src/server/inject.ts +++ b/src/server/inject.ts @@ -15,13 +15,14 @@ export function injectSystem( config: Config, surface?: PackSurface, visibilityMarkers = true, + suppressCompressPrompt = false, ): string | AnthropicRequestBody["system"] { // ONLY the static compress prompt goes into the system block — it is the // prefix-cache anchor and must stay byte-stable across turns. The nudge // (which changes every turn) is appended as a trailing user message by // the caller (prepareAnthropic), never merged into system. const parts: string[] = []; - if (opts.compress.injectTool) parts.push(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)); + if (opts.compress.injectTool && !suppressCompressPrompt) parts.push(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)); else if (!knobRenderNone()) { // #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch. const tagsOnly = buildAcpTagsOnlyPrompt("function", prompts, surface?.promptSections); diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index a41bc7d30..41350a74f 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -51,6 +51,8 @@ export async function prepareAnthropic( const stream = parsed.stream === true; ++session.stats.requests; const injectTools = opts.compress.injectTool && !pluginMode; + // #3177: DSH tools are host-scope (the preset agent can't reach them) — suppress the model-side compress surface. + const dshCompressSurfaceOff = pluginMode && session.metadata["pluginAgent"] === "dsh"; // #3222: #651's reasoning-drop plan, computed on the INBOUND history before // core.processTurn — see planReasoningDrop for why the post-turn view is too late. let reasoningPlan = new Set(); @@ -263,7 +265,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 willInjectNudge = opts.compress.injectNudge && !!turn.nudge && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig)) && (turn.nudge.shouldInject || emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange)); + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && !dshCompressSurfaceOff && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig)) && (turn.nudge.shouldInject || emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange)); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge, decideTier1Arm && !decideFallbackOverride)); processedMessages = applyReasoningDrop(stripKernelSummaries(turn.messages, turn.state), reasoningPlan, log, sessionId); // #1001: a silent client history rewrite takes the same archive+prune path @@ -316,7 +318,7 @@ export async function prepareAnthropic( rebuiltMessages = [...rebuiltMessages, ...sysNotes.map((text) => ({ role: "user" as const, content: text }))]; } - systemOut = injectSystem(parsed, opts, prompts, loopConfig, surface, visibilityMarkers); + systemOut = injectSystem(parsed, opts, prompts, loopConfig, surface, visibilityMarkers, dshCompressSurfaceOff); // #1637: stamp the steady body's system. NOTE: parsed.system stays the // #1085 frozen CLIENT head (injectSystem's in-place view) — the round-2 // adapter consumes that frozen head and stamps its own rebuild, so NO diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 462b8fe00..3af7650d5 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -93,6 +93,8 @@ export async function prepareGoogle( const isTitleGen = maxTokens <= 200; const shouldInject = opts.compress.injectTool && !isTitleGen; const injectTools = shouldInject && !pluginMode; + // #3177: DSH tools are host-scope (the preset agent can't reach them) — suppress the model-side compress surface. + const dshCompressSurfaceOff = pluginMode && session.metadata["pluginAgent"] === "dsh"; // #2483: this lane used to ignore renderNone entirely (hardcoded "text-only" // in processTurn, Prepared and countTokens) — the knob was dead config on /v1beta. const renderStrategy = knobRenderNone() ? "none" : "text-only"; @@ -199,7 +201,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 willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig)) && (turn.nudge.shouldInject || emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange)); + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && !dshCompressSurfaceOff && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig)) && (turn.nudge.shouldInject || emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange)); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, model, willInjectNudge, decideTier1Arm && !decideFallbackOverride)); processedMessages = stripKernelSummaries(turn.messages, turn.state); // #1001: a silent client history rewrite takes the same archive+prune path @@ -228,7 +230,7 @@ export async function prepareGoogle( // constants, so the system anchor stays identical across normal turns // and round-2 re-requests — skipping them here would fork the prefix // at every fold and collapse the upstream cache hit. - if (shouldInject) sysParts.push(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)); + if (shouldInject && !dshCompressSurfaceOff) sysParts.push(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)); else if (!isTitleGen && renderStrategy !== "none") { // #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch. const tagsOnly = buildAcpTagsOnlyPrompt("function", prompts, surface?.promptSections); diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 0f60d8d0a..71e6da9bf 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -81,6 +81,13 @@ export async function prepareOpenai( const isTitleGen = maxTokens <= 200; const shouldInject = opts.compress.injectTool && !isTitleGen; const injectTools = shouldInject && !pluginMode; + // #3177: DSH registers bili's ACP tools host-scope (invisible to the preset + // agent — 0/3939 request headers carry them), so model-driven compression is + // structurally unreachable there. Drop the model-side compress surface (system + // prompt + nudge) for DSH plugin sessions; otherwise the nudge tells the model + // to call a tool it can never see ("unknown tool \"compress\""). pi/omp/opencode + // keep it — their extensions expose the tools to the agent. + const dshCompressSurfaceOff = pluginMode && session.metadata["pluginAgent"] === "dsh"; const markerCounts = stripRawStage(parsed, "openai"); logRawStripCounts(markerCounts, log, sessionId); @@ -210,7 +217,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 willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig)) && (turn.nudge.shouldInject || emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange)); + const willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && !dshCompressSurfaceOff && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig)) && (turn.nudge.shouldInject || emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange)); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge, decideTier1Arm && !decideFallbackOverride)); processedMessages = applyReasoningDrop(stripKernelSummaries(turn.messages, turn.state), reasoningPlan, log, sessionId); // #1001: a silent client history rewrite takes the same archive+prune path @@ -255,7 +262,7 @@ export async function prepareOpenai( // would invalidate the cache every turn. const sysParts: string[] = []; if (openaiSystemText) sysParts.push(openaiSystemText); - if (shouldInject) sysParts.push(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)); + if (shouldInject && !dshCompressSurfaceOff) sysParts.push(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)); else if (!isTitleGen && !knobRenderNone()) { // #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch. const tagsOnly = buildAcpTagsOnlyPrompt("function", prompts, surface?.promptSections); diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index 37ad1a87b..bae7a9076 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -114,6 +114,11 @@ export async function prepareResponses( const shouldInject = opts.compress.injectTool; const injectTools = shouldInject && !pluginMode; + // #3177: DSH tools are host-scope (the preset agent can't reach them) — suppress the model-side compress surface. + const dshCompressSurfaceOff = pluginMode && session.metadata["pluginAgent"] === "dsh"; + // Effective gate for the standing prompt + nudge: DSH reads as injectTool-off so it takes the same + // stable tags-only developer slot as any non-injecting session (prefix cache stays append-stable). + const compressSurfaceOn = shouldInject && !dshCompressSurfaceOff; // Codex native remote-compact request: no compress prompt/tools (the model // produces the compaction itself), no acp tags, plain passthrough so the // response terminal state can gate the rebase marker. @@ -314,7 +319,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 willInjectNudge = opts.compress.injectNudge && !!turn.nudge && shouldInject && !isCompactionTrigger && (!nudgeSuppressed(session) || decideFallbackOverride) && !compressBreakerArmed(session) && !(autoFoldEngaged(loopConfig, session) && growthFoldingArmed(loopConfig, codexLane && codexCompactMode() === "intercept" ? loopConfig.modelContextLimit * CODEX_COMPACT_HEALTH_RATIO : undefined)) && (turn.nudge.shouldInject || emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange)); + const willInjectNudge = 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)) && (turn.nudge.shouldInject || emergencyNudge(turn.nudge, undefined, loopConfig.compress.minCompressRange)); log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit, parsed.model, willInjectNudge, decideTier1Arm && !decideFallbackOverride)); processedMessages = repairResponsesAssistantOrdering(applyReasoningDrop(stripKernelSummaries(turn.messages, turn.state), reasoningPlan, log, sessionId), originalMessages); reapOrphansLogged(session, msgs, log, sessionId); @@ -330,10 +335,10 @@ export async function prepareResponses( ? [] : (session.metadata.codexForgedSummaries as string[] | undefined) ?? []; // #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch. - const tagsOnlyPrompt = !shouldInject && !isCompactionTrigger && !knobNoCompressPrompt() && !knobRenderNone() + const tagsOnlyPrompt = !compressSurfaceOn && !isCompactionTrigger && !knobNoCompressPrompt() && !knobRenderNone() ? buildAcpTagsOnlyPrompt(responsesTextProtocol ? "hybrid" : "function", prompts, surface?.promptSections) : ""; - if (shouldInject && !isCompactionTrigger && !knobNoCompressPrompt()) { + if (compressSurfaceOn && !isCompactionTrigger && !knobNoCompressPrompt()) { const prompt = withMarkerIntegrityNote(withSummaryBudgetNote(responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts, surface?.promptSections) : buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers); const devParts = [...projection.systemParts, ...forgedSummaries, prompt]; if (absorbActive) devParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); diff --git a/tests/issue2432-dsh-local-compaction.test.ts b/tests/issue2432-dsh-local-compaction.test.ts index 5d8836fd7..527c9d0c7 100644 --- a/tests/issue2432-dsh-local-compaction.test.ts +++ b/tests/issue2432-dsh-local-compaction.test.ts @@ -428,8 +428,11 @@ test("#2432: per-turn nudge injection goes quiet while the compress breaker is a // third request sees the big report against the small baseline. const small = Array.from({ length: 8 }, (_, i) => ({ role: i % 2 === 0 ? "user" : "assistant", content: `warm-${i}-` + "z".repeat(1000) })); const grown = [...small, ...Array.from({ length: 48 }, (_, i) => ({ role: i % 2 === 0 ? "user" : "assistant", content: `grow-${i}-` + "z".repeat(1000) }))]; + // #3177: DSH's ACP tools are host-scope (the preset agent can't reach them), so bili no + // longer nudges DSH at all — the breaker has nothing to silence there. Exercise it on a + // tool-visible plugin lane (omp) so the #2432 breaker-quenches-nudge coverage stays live. const post = (conv: string, messages: { role: string; content: string }[]) => - fetch(chatUrl(rig), { method: "POST", headers: dshHeaders(conv), body: JSON.stringify({ model: MODEL, messages }) }); + fetch(chatUrl(rig), { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "omp", "x-bili-plugin-conversation": conv }, body: JSON.stringify({ model: MODEL, messages }) }); try { // Control: healthy session through the growth shape → nudge on turn 3. for (const [i, m] of [small, grown, grown].entries()) { diff --git a/tests/plugin-nudge-injection.test.ts b/tests/plugin-nudge-injection.test.ts index 7c9b7e672..82ecb4477 100644 --- a/tests/plugin-nudge-injection.test.ts +++ b/tests/plugin-nudge-injection.test.ts @@ -49,7 +49,7 @@ function turn2Messages(): Record[] { ]; } -async function forwardedTurn2Length(sessionId: string, extraHeaders: Record): Promise { +async function forwardedTurn2(sessionId: string, extraHeaders: Record): Promise<{ count: number; messages: Record[] }> { const received: unknown[][] = []; const upstream = http.createServer((req, res) => { const chunks: Buffer[] = []; @@ -114,7 +114,8 @@ async function forwardedTurn2Length(sessionId: string, extraHeaders: Record[]; + return { count: msgs.length, messages: msgs }; } finally { proxy.close(); proxy.closeIdleConnections?.(); @@ -126,11 +127,36 @@ async function forwardedTurn2Length(sessionId: string, extraHeaders: Record { - const len = await forwardedTurn2Length("nudge-ctrl", {}); - assert.equal(len, 31, "proxy mode appends the nudge as a trailing user message at 78% of 64k"); + const { count } = await forwardedTurn2("nudge-ctrl", {}); + assert.equal(count, 31, "proxy mode appends the nudge as a trailing user message at 78% of 64k"); }); test("#451: nudge ALSO injected in PLUGIN mode when armed (regression)", async () => { - const len = await forwardedTurn2Length("nudge-plugin", { "x-bili-plugin": "omp" }); - assert.equal(len, 31, "plugin mode must append the nudge too — before the fix the !pluginMode gate suppressed it and this would be 30"); + const { count } = await forwardedTurn2("nudge-plugin", { "x-bili-plugin": "omp" }); + assert.equal(count, 31, "plugin mode must append the nudge too — before the fix the !pluginMode gate suppressed it and this would be 30"); +}); + +// #3177: DSH registers bili's ACP tools host-scope, invisible to its preset agent +// (0 of 3939 request headers carried them), so model-driven compression is +// structurally unreachable there. The proxy must drop the whole model-side compress +// surface for DSH plugin sessions — otherwise the standing prompt + nudge tell the +// model to call a tool it can never reach ("unknown tool \"compress\""). pi/omp keep +// the surface (their extensions expose the tools visibly), so the differential below +// pins the fix without touching their behavior. +test("#3177: DSH plugin session drops the compress nudge (regression)", async () => { + const { count, messages } = await forwardedTurn2("dsh-nudge", { "x-bili-plugin": "dsh" }); + assert.equal(count, 30, "DSH drops the nudge — an omp plugin session is 31, pre-fix DSH was 31 too"); + const last = messages[messages.length - 1]; + assert.ok(String(last?.["content"]).includes("now summarize"), "the client's own final turn is last — no nudge user message appended after it"); +}); + +test("#3177: DSH plugin session also drops the standing compress system prompt (regression)", async () => { + const omp = await forwardedTurn2("dsh-sys-omp", { "x-bili-plugin": "omp" }); + const dsh = await forwardedTurn2("dsh-sys-dsh", { "x-bili-plugin": "dsh" }); + const ompSys = String((omp.messages[0] ?? {})["content"] ?? ""); + const dshSys = String((dsh.messages[0] ?? {})["content"] ?? ""); + assert.ok(ompSys.includes("ACP TAGS"), "control: omp keeps the standing compress prompt (carries the ACP tag guidance)"); + // The full standing prompt (philosophy + how-to + tool surface) is far larger than + // the tags-only fallback DSH falls back to — so DSH's leading system block shrinks. + assert.ok(ompSys.length - dshSys.length > 300, `omp system ${ompSys.length}B vs dsh ${dshSys.length}B — DSH must drop the standing compress prompt`); });