Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/server/prepare-anthropic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<string>();
Expand Down
10 changes: 7 additions & 3 deletions src/server/prepare-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down
20 changes: 12 additions & 8 deletions src/server/prepare-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 10 additions & 5 deletions src/server/prepare-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down
36 changes: 33 additions & 3 deletions tests/plugin-nudge-injection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,7 +50,7 @@ function turn2Messages(): Record<string, unknown>[] {
];
}

async function forwardedTurn2(sessionId: string, extraHeaders: Record<string, string>): Promise<{ count: number; messages: Record<string, unknown>[] }> {
async function forwardedTurn2(sessionId: string, extraHeaders: Record<string, string>, bodyExtra?: Record<string, unknown>): Promise<{ count: number; messages: Record<string, unknown>[] }> {
const received: unknown[][] = [];
const upstream = http.createServer((req, res) => {
const chunks: Buffer[] = [];
Expand Down Expand Up @@ -98,7 +99,7 @@ async function forwardedTurn2(sessionId: string, extraHeaders: Record<string, st
const r1 = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify({ model: "deepseek-v4-flash", max_tokens: 64_000, messages: [{ role: "user", content: "hello" }] }),
body: JSON.stringify({ model: "deepseek-v4-flash", max_tokens: 64_000, messages: [{ role: "user", content: "hello" }], ...(bodyExtra ?? {}) }),
});
assert.equal(r1.status, 200);
await r1.text();
Expand All @@ -108,7 +109,7 @@ async function forwardedTurn2(sessionId: string, extraHeaders: Record<string, st
const r2 = await fetch(url, {
method: "POST",
headers,
body: JSON.stringify({ model: "deepseek-v4-flash", max_tokens: 64_000, messages: turn2Messages() }),
body: JSON.stringify({ model: "deepseek-v4-flash", max_tokens: 64_000, messages: turn2Messages(), ...(bodyExtra ?? {}) }),
});
assert.equal(r2.status, 200);
await r2.text();
Expand Down Expand Up @@ -160,3 +161,32 @@ test("#3177: DSH plugin session also drops the standing compress system prompt (
// 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`);
});

// #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 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`);
});
Loading