From e22b18c4aca1c027647f0097c002cc0872830f16 Mon Sep 17 00:00:00 2001 From: awork Date: Fri, 16 Oct 2026 21:16:35 +0800 Subject: [PATCH 1/2] fix: gate DSH compress-surface suppression on actual tool reachability (#3401) #3177 keyed the model-side compress-surface suppression off the lane name (pluginAgent === "dsh"), assuming every DSH install registers bili's ACP tools host-scope and unreachable by its preset agent (its scanned profile had 0/3939 request headers carrying them). That assumption does not hold for every DSH profile: some expose the ACP tools to the agent, so the unconditional gate silently killed the whole model-side compression path (nudge + standing prompt) for those setups. Key the gate off observed reachability instead: biliToolsDeclaredOnWire reads the request's own tools array (ground truth of what the model sees; bili injects nothing in plugin mode) and suppresses only when the ACP tools are genuinely absent from it. When a DSH agent exposes them, the surface stays on. The host's tool table is per-session-stable, so the flag does not flip mid-session and the responses developer slot stays append-cache-stable. pi/omp/opencode/proxy are untouched (still gated only on pluginAgent). Closes #3401 --- src/server/prepare-anthropic.ts | 10 ++++++--- src/server/prepare-google.ts | 10 ++++++--- src/server/prepare-openai.ts | 20 +++++++++++------- src/server/prepare-responses.ts | 15 +++++++++----- tests/plugin-nudge-injection.test.ts | 31 +++++++++++++++++++++++++--- 5 files changed, 64 insertions(+), 22 deletions(-) diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index b862723ab5..9d80030dc0 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -9,7 +9,7 @@ import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTo import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; -import { nudgeSuppressed } from "../session-self-heal.js"; +import { biliToolsDeclaredOnWire, nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; @@ -49,8 +49,12 @@ 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"; + // #3177/#3401: suppress the model-side compress surface ONLY when the DSH agent genuinely + // cannot reach bili's ACP tools — judged from the request's own tools array (ground truth of + // what the model sees; bili injects nothing in plugin mode), NOT from the lane name alone. + // Some DSH profiles expose the tools to the agent; keying off "pluginAgent===dsh" over-suppressed + // those (see #3401). The host's tool table is per-session-stable, so this does not flip mid-session. + const dshCompressSurfaceOff = pluginMode && session.metadata["pluginAgent"] === "dsh" && !biliToolsDeclaredOnWire(parsed, "anthropic"); // #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(); diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index b26e8714ac..b189f369e4 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -8,7 +8,7 @@ import { diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBilli import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; -import { nudgeSuppressed } from "../session-self-heal.js"; +import { biliToolsDeclaredOnWire, nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; import { recordConflict } from "../conflict-watch.js"; @@ -91,8 +91,12 @@ 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"; + // #3177/#3401: suppress the model-side compress surface ONLY when the DSH agent genuinely + // cannot reach bili's ACP tools — judged from the request's own tools array (ground truth of + // what the model sees; bili injects nothing in plugin mode), NOT from the lane name alone. + // Some DSH profiles expose the tools to the agent; keying off "pluginAgent===dsh" over-suppressed + // those (see #3401). The host's tool table is per-session-stable, so this does not flip mid-session. + const dshCompressSurfaceOff = pluginMode && session.metadata["pluginAgent"] === "dsh" && !biliToolsDeclaredOnWire(parsed, "google"); // #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"; diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 94fadf9083..7ff8efff63 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -10,7 +10,7 @@ import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTo import { buildDecisionPrompt, buildDirectiveText, consumeFallback, ladderMode, resolveDecisionRange, stripDecisionReasoningFields, type DecideConfig } from "../nudge-decide.js"; import { excludeDeadRefRanges } from "../acp-status.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; -import { nudgeSuppressed } from "../session-self-heal.js"; +import { biliToolsDeclaredOnWire, nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; @@ -79,13 +79,17 @@ 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"; + // #3177/#3401: drop the model-side compress surface (system prompt + nudge) + // ONLY when the DSH agent genuinely cannot reach bili's ACP tools — judged from + // the request's own tools array (ground truth of what the model sees; bili injects + // nothing in plugin mode), NOT from the lane name. #3177 assumed every DSH install + // registers them host-scope (its scanned profile had 0/3939 headers carrying them), + // but some DSH profiles expose them to the agent, and keying off "pluginAgent===dsh" + // alone over-suppressed those and killed the whole model-side compression path. When + // the tools are on the wire the model CAN call them, so keep the surface. The host's + // tool table is per-session-stable, so this does not flip mid-session (no prefix-cache + // churn). pi/omp/opencode keep it unconditionally — their extensions always expose it. + const dshCompressSurfaceOff = pluginMode && session.metadata["pluginAgent"] === "dsh" && !biliToolsDeclaredOnWire(parsed, "openai"); const markerCounts = stripRawStage(parsed, "openai"); logRawStripCounts(markerCounts, log, sessionId); diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index 0cb8b37fa5..05e5753f59 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -11,7 +11,7 @@ import { excludeDeadRefRanges } from "../acp-status.js"; import { mergeAdjacentConfigurationUpdates, patchResponsesInputWithToolImages as patchResponsesInput, responsesToCoreWithToolImages as responsesToCore } from "../responses-tool-output.js"; import { dropWhitespaceResponsesMessages, normalizeResponsesMessageItems, sanitizeResponsesInputIds } from "../loop/adapter-responses.js"; import { METADATA_LAST_RESPONSES_MODE, noteCanonicalSystemFingerprint, reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "../fold-reconcile.js"; -import { nudgeSuppressed, pluginLaneDegraded } from "../session-self-heal.js"; +import { biliToolsDeclaredOnWire, nudgeSuppressed, pluginLaneDegraded } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; import { coveredRealHistoryIds, foldCoverage, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; @@ -112,10 +112,15 @@ 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). + // #3177/#3401: suppress the model-side compress surface ONLY when the DSH agent genuinely + // cannot reach bili's ACP tools — judged from the request's own tools array (ground truth of + // what the model sees; bili injects nothing in plugin mode), NOT from the lane name alone. + // Some DSH profiles expose the tools to the agent; keying off "pluginAgent===dsh" over-suppressed + // those (see #3401). The host's tool table is per-session-stable, so this does not flip mid-session. + const dshCompressSurfaceOff = pluginMode && session.metadata["pluginAgent"] === "dsh" && !biliToolsDeclaredOnWire(parsed, "responses"); + // Effective gate for the standing prompt + nudge. When suppressed, 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); when NOT suppressed the surface is on and stable across turns likewise. 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 diff --git a/tests/plugin-nudge-injection.test.ts b/tests/plugin-nudge-injection.test.ts index 82ecb44775..35c42efae6 100644 --- a/tests/plugin-nudge-injection.test.ts +++ b/tests/plugin-nudge-injection.test.ts @@ -10,6 +10,7 @@ import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { BILI_ACP_TOOLS_OPENAI } from "../src/compress-tool.js"; // #451 regression: 0.1.69 gated all three nudge injection sites by !pluginMode // on the premise that "in plugin mode the agent owns compression". The agent @@ -49,7 +50,7 @@ function turn2Messages(): Record[] { ]; } -async function forwardedTurn2(sessionId: string, extraHeaders: Record): Promise<{ count: number; messages: Record[] }> { +async function forwardedTurn2(sessionId: string, extraHeaders: Record, bodyExtra?: Record): Promise<{ count: number; messages: Record[] }> { const received: unknown[][] = []; const upstream = http.createServer((req, res) => { const chunks: Buffer[] = []; @@ -98,7 +99,7 @@ async function forwardedTurn2(sessionId: string, extraHeaders: Record 300, `omp system ${ompSys.length}B vs dsh ${dshSys.length}B — DSH must drop the standing compress prompt`); }); + +// #3401: reachability is judged from the request's own tools array, not the lane name. +// A DSH profile that exposes bili's ACP tools to its agent must KEEP the whole model-side +// compress surface — #3177 keyed the suppression off pluginAgent==="dsh" alone and +// over-suppressed exactly those setups. These are REAL agent turns: the tools array carries +// bili's ACP set alongside the host's own action tools (a host tool vetoes the #1897 +// "leaked bili tools only" side-request demotion, so the request rides the normal pipeline). +const dshAgentTools = [ + ...BILI_ACP_TOOLS_OPENAI, + { type: "function" as const, function: { name: "read_file", description: "host action tool", parameters: { type: "object", properties: {} } } }, +]; + +test("#3401: DSH keeps the compress nudge when its agent exposes the ACP tools on the wire (regression)", async () => { + const dshNoTools = await forwardedTurn2("dsh3401-notools", { "x-bili-plugin": "dsh" }); + const dshWithTools = await forwardedTurn2("dsh3401-tools", { "x-bili-plugin": "dsh" }, { tools: dshAgentTools }); + assert.equal(dshNoTools.count, 30, "control: DSH without tools on the wire still drops the nudge (#3177)"); + assert.equal(dshWithTools.count, 31, "DSH whose agent exposes the ACP tools keeps the nudge — pre-fix this was 30 too"); +}); + +test("#3401: DSH keeps the standing compress system prompt when its agent exposes the ACP tools on the wire (regression)", async () => { + const dshWithTools = await forwardedTurn2("dsh3401-sys", { "x-bili-plugin": "dsh" }, { tools: dshAgentTools }); + const sys = String((dshWithTools.messages[0] ?? {})["content"] ?? ""); + assert.ok(sys.includes("ACP TAGS"), "DSH with exposed ACP tools keeps the standing compress prompt (carries the ACP tag guidance)"); +}); From a7b40b90eeaadaa878f094dd540f310e060b61ab Mon Sep 17 00:00:00 2001 From: awork Date: Sat, 17 Oct 2026 00:18:25 +0800 Subject: [PATCH 2/2] =?UTF-8?q?test(#3401):=20give=20the=20standing-prompt?= =?UTF-8?q?=20regression=20test=20teeth=20=E2=80=94=20compare=20against=20?= =?UTF-8?q?the=20no-tools=20baseline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The first assertion ('ACP TAGS' present) also holds for the suppressed tags-only fallback, so it passed on pre-fix code (verified by reverting src/ and re-running). Add the length-delta check against a DSH session WITHOUT tools on the wire, mirroring the #3177 test's discipline: the with-tools system block must carry the FULL compress prompt, not just the tags-only fallback. --- tests/plugin-nudge-injection.test.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/plugin-nudge-injection.test.ts b/tests/plugin-nudge-injection.test.ts index 35c42efae6..48e95041e9 100644 --- a/tests/plugin-nudge-injection.test.ts +++ b/tests/plugin-nudge-injection.test.ts @@ -181,7 +181,12 @@ test("#3401: DSH keeps the compress nudge when its agent exposes the ACP tools o }); test("#3401: DSH keeps the standing compress system prompt when its agent exposes the ACP tools on the wire (regression)", async () => { - const dshWithTools = await forwardedTurn2("dsh3401-sys", { "x-bili-plugin": "dsh" }, { tools: dshAgentTools }); - const sys = String((dshWithTools.messages[0] ?? {})["content"] ?? ""); - assert.ok(sys.includes("ACP TAGS"), "DSH with exposed ACP tools keeps the standing compress prompt (carries the ACP tag guidance)"); + const dshNoTools = await forwardedTurn2("dsh3401-sys-off", { "x-bili-plugin": "dsh" }); + const dshWithTools = await forwardedTurn2("dsh3401-sys-on", { "x-bili-plugin": "dsh" }, { tools: dshAgentTools }); + const off = String((dshNoTools.messages[0] ?? {})["content"] ?? ""); + const on = String((dshWithTools.messages[0] ?? {})["content"] ?? ""); + assert.ok(on.includes("ACP TAGS"), "DSH with exposed ACP tools keeps the standing compress prompt (carries the ACP tag guidance)"); + // 'ACP TAGS' alone cannot discriminate — the suppressed tags-only fallback carries it too. + // The FULL compress prompt must be present: same length-delta discipline as the #3177 test above. + assert.ok(on.length - off.length > 300, `with-tools ${on.length}B vs no-tools ${off.length}B — exposed ACP tools must restore the full compress prompt, not just the tags-only fallback`); });