diff --git a/src/compress-tool.ts b/src/compress-tool.ts index 2cd8a24e3c..a036c12a64 100644 --- a/src/compress-tool.ts +++ b/src/compress-tool.ts @@ -344,6 +344,30 @@ export function withProjectAttribution(prompt: string): string { return prompt + "\n\n" + PROJECT_ATTRIBUTION_LINE; } +/** The MCP-dialect name bili's fixed "bili" shim server (src/mcp.ts, #2578) + * exposes an ACP tool under on MCP-bridged hosts: zcode/claude-code lanes + * carry `mcp__bili__compress` etc. in their outbound tool tables. */ +export function mcpBiliToolName(name: string): string { + return "mcp__bili__" + name; +} + +/** #4344: MCP-bridged plugin hosts expose the ACP tools under mcp__bili__* + * names while the kernel prompts teach the bare names — the nudge/standing + * prompt telling the model to call `compress` lands on a tool table that + * holds no such tool (the #4344 zcode shape: nudges armed at 21-29% usage, + * 0 compressions across 46 requests). When the wire declares the prefixed + * compress and NOT the bare one, this factual name mapping is appended so + * the model can act on the prompt (a bare-name call would hit an unknown + * tool — the MCP tool call routes through the host's MCP bridge, which bili + * cannot rewrite in plugin mode). Static string + per-session-stable tool + * table = cache-stable, the same premise the surface flag itself rides + * (#3403). */ +export const MCP_DIALECT_NOTE = "On this host the context-management tools are exposed under MCP-prefixed names: call `mcp__bili__compress` (not `compress`) — likewise `mcp__bili__decompress`, `mcp__bili__search_context`, `mcp__bili__acp_status`."; + +export function withMcpDialectNote(prompt: string, on: boolean): string { + return on ? `${prompt}\n\n${MCP_DIALECT_NOTE}` : prompt; +} + /** Note mode for one request: the harness is code-mode-shaped when the * client's own tools array carries a tool named like a known code-mode * omnibus (run_code / code_run — see CODE_MODE_OMNIBUS_TOOL_NAMES, #3884). */ diff --git a/src/host-profiles.ts b/src/host-profiles.ts index 9420e62573..9312b83593 100644 --- a/src/host-profiles.ts +++ b/src/host-profiles.ts @@ -69,6 +69,17 @@ export interface HostProfile { const PROFILES: Record = { dsh: { identity: "dsh", toolsHostScopedByDefault: true, wantsBillingBlock: true, stampsBodyPromptCacheKey: false, coworkTransportPossible: false, instructionsFingerprintKeyed: false, compactsClientSide: true }, + // #4344: zcode reaches the ACP tools ONLY through its MCP bridge + // (mcp__bili__* names), and bridge delivery is environmental — zcode's + // agent modes gate MCP tools, and the degraded shim path serves an EMPTY + // tool list when no healthy proxy is found (src/zcode/mcp-entry.ts + // serveDegraded). So reachability is OBSERVED per request, never assumed: + // a healthy bridge declares mcp__bili__compress (dialect-aware, #4344 + // fix) and the surface stays on; a broken/idle bridge declares nothing + // and the surface goes silent instead of advertising a tool the model + // cannot call (the #4344 shape: nudges armed at 21-29% usage, 0 + // compressions — #3177's "unknown tool" discipline, applied to zcode). + zcode: { identity: "zcode", toolsHostScopedByDefault: true, wantsBillingBlock: false, stampsBodyPromptCacheKey: false, coworkTransportPossible: false, instructionsFingerprintKeyed: false, compactsClientSide: false }, pi: { identity: "pi", toolsHostScopedByDefault: false, wantsBillingBlock: false, stampsBodyPromptCacheKey: false, coworkTransportPossible: false, instructionsFingerprintKeyed: false, compactsClientSide: false }, omp: { identity: "omp", toolsHostScopedByDefault: false, wantsBillingBlock: false, stampsBodyPromptCacheKey: true, coworkTransportPossible: true, instructionsFingerprintKeyed: false, compactsClientSide: false }, opencode: { identity: "opencode", toolsHostScopedByDefault: false, wantsBillingBlock: false, stampsBodyPromptCacheKey: false, coworkTransportPossible: false, instructionsFingerprintKeyed: false, compactsClientSide: false }, diff --git a/src/server.ts b/src/server.ts index df98297b21..eeb2f26250 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1554,6 +1554,11 @@ export type Prepared = { * response must pass through verbatim, and usage is sniffed instead of * captured by the compress loop. */ pluginMode?: boolean; + /** #4344: this request declared the compress tool ONLY in the MCP dialect + * (mcp__bili__compress, no bare `compress`) — the prepared standing + * prompt carries the MCP name-mapping note (withMcpDialectNote). Threads + * to the round-2 system rebuild for byte-parity. */ + mcpDialectNote?: boolean; responsesTextProtocol?: boolean; resetAfterSuccess?: boolean; responsesProjection?: ResponsesProjection; diff --git a/src/server/inject.ts b/src/server/inject.ts index 6c342f7102..78b52dc56e 100644 --- a/src/server/inject.ts +++ b/src/server/inject.ts @@ -4,7 +4,7 @@ import type { ProxyOptions } from "../config.js"; import { type AnthropicRequestBody, type GoogleFunctionDeclaration, type GoogleTool, type OpenAITool } from "acp-kernel/wire"; import { appendSystemText } from "../util.js"; import { absorbEnabled, absorbToolName } from "../absorb.js"; -import { BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_GOOGLE_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, packOwnsDescription, ptcNoteMode, withAcpNotes, withAcpNotesOpenAI, withMarkerIntegrityNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; +import { BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_GOOGLE_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, packOwnsDescription, ptcNoteMode, withAcpNotes, withAcpNotesOpenAI, withMarkerIntegrityNote, withMcpDialectNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; import { externalSummaryEnabled, withExternalSummaryTools } from "../external-summary-surface.js"; import { forceTextProtocol as knobForceTextProtocol, renderNone as knobRenderNone } from "../knobs.js"; @@ -16,13 +16,14 @@ export function injectSystem( surface?: PackSurface, visibilityMarkers = true, suppressCompressPrompt = false, + mcpDialectNote = 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 && !suppressCompressPrompt) parts.push(withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers))); + if (opts.compress.injectTool && !suppressCompressPrompt) parts.push(withProjectAttribution(withMcpDialectNote(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers), mcpDialectNote))); 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/lane-decisions.ts b/src/server/lane-decisions.ts index ed864979b1..92e2d3d92d 100644 --- a/src/server/lane-decisions.ts +++ b/src/server/lane-decisions.ts @@ -25,7 +25,7 @@ import { biliToolsDeclaredOnWire } from "../session-self-heal.js"; import { extractBillingAttributionBlock } from "../preflight.js"; import { ANONYMOUS, hostProfileFor, type HostProfile } from "../host-profiles.js"; -import { CODE_MODE_OMNIBUS_TOOL_NAMES } from "../compress-tool.js"; +import { CODE_MODE_OMNIBUS_TOOL_NAMES, mcpBiliToolName } from "../compress-tool.js"; type LaneProtocol = "anthropic" | "openai" | "google" | "responses"; @@ -154,6 +154,12 @@ interface LaneDecisions { /** Legacy alias kept for the four prepares' migration: TRUE when the * surface must be OFF (the old `dshCompressSurfaceOff` variable). */ readonly compressSurfaceOff: boolean; + /** #4344: this plugin-mode request declares the compress tool ONLY in the + * MCP dialect (`mcp__bili__compress`, no bare `compress`) — the standing + * prompt must teach the model the name it can actually call (see + * withMcpDialectNote). False in proxy mode (bili injects the bare tools + * itself, so the bare name is always callable). */ + readonly mcpDialect: boolean; /** #3482 site 1: should this lane's summary side-calls carry the billing * attribution block extracted from the client system (#2189)? The dsh * profile declares it (subscription-OAuth lanes 429 without it); any @@ -232,9 +238,13 @@ export function resolveLaneDecisions(input: LaneDecisionInput): LaneDecisions { // In PLUGIN mode the host owns the tool table: reach when this request // declares `compress` (observed — some dsh profiles DO surface it, #3401) // or the host exposes bili tools to the model by design (profile default, - // #3177). PTC presets collapse EVERY tool into a single omnibus (SDK - // bindings nested inside it), so top-level absence of `compress` is not - // evidence of unreachability there — ANY known omnibus name reads as + // #3177). #4344: the declaration counts in EITHER dialect — MCP-bridged + // hosts (zcode/claude-code) carry `mcp__bili__compress`, and a lane whose + // bridge is healthy must not read as unreachable just because the name is + // prefixed (session-self-heal's biliToolsDeclaredOnWire docs the same + // dual dialect). PTC presets collapse EVERY tool into a single omnibus + // (SDK bindings nested inside it), so top-level absence of `compress` is + // not evidence of unreachability there — ANY known omnibus name reads as // reachable. Known shapes live in CODE_MODE_OMNIBUS_TOOL_NAMES (run_code // = dsh-tools built-in, DSH 0.1.x; code_run = dsh-codemode transport, // DSH 0.2.x): #3600's miss was released 0.1.198 suppressing the surface @@ -244,9 +254,11 @@ export function resolveLaneDecisions(input: LaneDecisionInput): LaneDecisions { // threshold, fully silent). const compressReachable = !input.pluginMode || declaredTools.has("compress") + || declaredTools.has(mcpBiliToolName("compress")) || [...CODE_MODE_OMNIBUS_TOOL_NAMES].some((n) => declaredTools.has(n)) || !profile.toolsHostScopedByDefault; const compressSurfaceOn = input.shouldInject && compressReachable; + const mcpDialect = input.pluginMode && declaredTools.has(mcpBiliToolName("compress")) && !declaredTools.has("compress"); const billingBlockObserved = extractBillingAttributionBlock(systemOf(input.parsed)) !== undefined; const billingBlockWanted = profile.wantsBillingBlock || billingBlockObserved; return Object.freeze({ @@ -255,6 +267,7 @@ export function resolveLaneDecisions(input: LaneDecisionInput): LaneDecisions { toolsReachable: Object.freeze({ compress: compressReachable }), compressSurfaceOn, compressSurfaceOff: !compressSurfaceOn, + mcpDialect, billingBlockWanted, }); } diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index aafb3fb290..c9c773d6e5 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -418,7 +418,7 @@ export async function prepareAnthropic( rebuiltMessages = [...rebuiltMessages, ...sysNotes.map((text) => ({ role: "user" as const, content: text }))]; } - systemOut = injectSystem(parsed, opts, prompts, loopConfig, surface, visibilityMarkers, laneDecisions.compressSurfaceOff); + systemOut = injectSystem(parsed, opts, prompts, loopConfig, surface, visibilityMarkers, laneDecisions.compressSurfaceOff, laneDecisions.mcpDialect); // #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 @@ -539,7 +539,7 @@ export async function prepareAnthropic( + countSystemAndToolsTokens(extractSystem(systemOut), toolsOut) + imageReserveFor(session, "anthropic", rebuilt, opts, upstreamOrigin); if (upstreamOrigin) session.stats.lastLocalTextEstimateOrigin = upstreamOrigin; - return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, nudge, nudgeOutcome, prompts, surface, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; + return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, anthropicSystem: parsed.system, anthropicBillingBlock, anthropicCacheMarks, anthropicClientCacheControls: clientCacheControls, systemNotes: sysNotes, protocol: "anthropic", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; } // #2648: true when any system block pins cache_control.scope:"global". bili's diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index 0cd9ed03c9..1a993c6909 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -15,7 +15,7 @@ import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistory import { recordConflict } from "../conflict-watch.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; import { carriesBiliSnapshotCheckpoint } from "./dsh-authored-compaction.js"; -import { ABSORB_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_GOOGLE, RULE_TOOL_GOOGLE, absorbToolsFor, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; +import { ABSORB_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_GOOGLE, RULE_TOOL_GOOGLE, absorbToolsFor, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withMcpDialectNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; import { absorbToolName, applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, imageRescaleToolArmed, retrieveToolName } from "../store.js"; import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; @@ -272,7 +272,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 (laneDecisions.compressSurfaceOn) sysParts.push(withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers))); + if (laneDecisions.compressSurfaceOn) sysParts.push(withProjectAttribution(withMcpDialectNote(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers), laneDecisions.mcpDialect))); 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); @@ -371,7 +371,7 @@ export async function prepareGoogle( } snapshotMessages(session, originalMessages); markDirty(session); - return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, protocol: "google", stream, compressInjected: injectTools, pluginMode, nudge, nudgeOutcome, prompts, surface, google: { system: googleClientSystem, model }, systemNotes: sysNotes, renderTags: renderStrategy } as Prepared; + return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, protocol: "google", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, google: { system: googleClientSystem, model }, systemNotes: sysNotes, renderTags: renderStrategy } as Prepared; } /** `POST /v1beta/models/:countTokens` — the fold-prune twin of diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 9361302dc4..d5920fc889 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -16,7 +16,7 @@ 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"; import { carriesBiliSnapshotCheckpoint } from "./dsh-authored-compaction.js"; -import { ABSORB_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_OPENAI, RULE_TOOL_OPENAI, absorbToolsFor, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; +import { ABSORB_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_OPENAI, RULE_TOOL_OPENAI, absorbToolsFor, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withMcpDialectNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; 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"; @@ -311,7 +311,7 @@ export async function prepareOpenai( // would invalidate the cache every turn. const sysParts: string[] = []; if (openaiSystemText) sysParts.push(openaiSystemText); - if (laneDecisions.compressSurfaceOn) sysParts.push(withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers))); + if (laneDecisions.compressSurfaceOn) sysParts.push(withProjectAttribution(withMcpDialectNote(withMarkerIntegrityNote(withSummaryBudgetNote(buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers), laneDecisions.mcpDialect))); else if (!isTitleGen && !knobRenderNone()) { // #1881: the NEVER-echo prohibition follows the rendered tags, not the tool switch. const tagsOnly = buildAcpTagsOnlyPrompt("function", prompts, surface?.promptSections); @@ -476,7 +476,7 @@ export async function prepareOpenai( } snapshotMessages(session, originalMessages); markDirty(session); - return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, protocol: "openai", stream, compressInjected: injectTools, pluginMode, nudge, nudgeOutcome, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; + return { body: JSON.stringify(rebuilt), session, attachedRetrievals, attachedRetrievalNoteIds, processedMessages, originalMessages, protocol: "openai", stream, compressInjected: injectTools, pluginMode, mcpDialectNote: laneDecisions.mcpDialect, nudge, nudgeOutcome, prompts, surface, openaiSystemText, systemNotes: sysNotes, renderTags: renderStrategy, dropReasoning: (msgs: BiliMessage[]) => applyReasoningDrop(msgs, reasoningPlan, log, sessionId) } as Prepared; } /** Append the ephemeral nudge to a Gemini `contents` array. Gemini is diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index 67663b46c4..aad90cd379 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -19,7 +19,7 @@ import { applyCompactionArchive, coveredRealHistoryIds, foldCoverage, markDirty, import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; import { carriesBiliSnapshotCheckpoint } from "./dsh-authored-compaction.js"; import { recordConflict } from "../conflict-watch.js"; -import { ABSORB_TOOL_NAME, BILI_ACP_READONLY_TOOLS_RESPONSES, BILI_ACP_READONLY_TOOLS_RESPONSES_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, READ_ORIGINAL_IMAGE_TOOL_RESPONSES, RULE_TOOL_RESPONSES, absorbToolsFor, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressHybridSystemPrompt, buildCompressSystemPrompt, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; +import { ABSORB_TOOL_NAME, BILI_ACP_READONLY_TOOLS_RESPONSES, BILI_ACP_READONLY_TOOLS_RESPONSES_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, READ_ORIGINAL_IMAGE_TOOL_RESPONSES, RULE_TOOL_RESPONSES, absorbToolsFor, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressHybridSystemPrompt, buildCompressSystemPrompt, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withMcpDialectNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; import { absorbToolName, applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, imageRescaleToolArmed, retrieveToolName } from "../store.js"; import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; @@ -393,7 +393,7 @@ export async function prepareResponses( ? buildAcpTagsOnlyPrompt(responsesTextProtocol ? "hybrid" : "function", prompts, surface?.promptSections) : ""; if (compressSurfaceOn && !isCompactionTrigger && !knobNoCompressPrompt()) { - const prompt = withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts, surface?.promptSections) : buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)); + const prompt = withProjectAttribution(withMcpDialectNote(withMarkerIntegrityNote(withSummaryBudgetNote(responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts, surface?.promptSections) : buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers), laneDecisions.mcpDialect)); const devParts = [...projection.systemParts, ...forgedSummaries, prompt]; if (absorbActive) devParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); const devContent = devParts.join("\n\n---\n\n"); @@ -654,6 +654,7 @@ export async function prepareResponses( stream, compressInjected: injectTools && !isCompactionTrigger, pluginMode, + mcpDialectNote: laneDecisions.mcpDialect, responsesTextProtocol, nudge, nudgeOutcome, diff --git a/src/server/relay.ts b/src/server/relay.ts index 594cd98736..e86fc45673 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -17,7 +17,7 @@ import { applyCompatDropFields, dropCompatFieldsJson, resolveCompatDropFields } import { applyCompatRoles, applyCompatRolesJson, detectRoleRejection, detectSystemPlacementError, hasOffHeadSystem, resolveCompatRoles, type CompatRoles } from "../compat-roles.js"; import { compressLoopResponsesJson } from "../compress-loop-responses.js"; import { resolveCompress } from "../compress-settings.js"; -import { buildAbsorbSystemPrompt, buildCompressHybridSystemPrompt, buildCompressSystemPrompt, withMarkerIntegrityNote, withProjectAttribution, withSummaryBudgetNote } from "../compress-tool.js"; +import { buildAbsorbSystemPrompt, buildCompressHybridSystemPrompt, buildCompressSystemPrompt, withMarkerIntegrityNote, withMcpDialectNote, withProjectAttribution, withSummaryBudgetNote } from "../compress-tool.js"; import { findRoute, resolveDeclaredProtocol, type ProxyOptions } from "../config.js"; import { makeContinuationRefetch } from "../degenerate-retry.js"; import { dumpRejectedBody } from "../error-dump.js"; @@ -1856,7 +1856,12 @@ export async function forward( // only THEN pushes the absorb section (sysParts order). Nesting it // inside flipped the two sections on round-2 and broke the // system-element prefix at every fold (cache-seam-probes P1+P2/P3). - const systemPrompt = withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)) + absorbSection; + // #4344: the round-2 rebuild threads the MCP-dialect note inside the + // same nesting for byte-parity with the steady prepare paths. Round-2 + // is proxy-loop-only today (proxy mode never sets the flag — bili + // injects the bare tools there), so `prepared.mcpDialectNote === true` + // is the explicit seam, not a live branch yet. + const systemPrompt = withProjectAttribution(withMcpDialectNote(withMarkerIntegrityNote(withSummaryBudgetNote(textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers), prepared.mcpDialectNote === true)) + absorbSection; const adapter = pickAdapter(prepared.protocol, parsedReq, textProtocol, prepared.responsesProjection, prepared.anthropicSystem, prepared.openaiSystemText, absorbActive ? absorbToolName(loopConfig) : undefined, prepared.google, prepared.systemNotes, opts.streamErrorShape, prepared.anthropicCacheMarks, prepared.anthropicClientCacheControls, absorbActive); const refreshFolded = async (current: CoreMessage[]): Promise => { return withSessionLock(prepared.session, async () => { diff --git a/tests/issue4344-mcp-dialect.test.ts b/tests/issue4344-mcp-dialect.test.ts new file mode 100644 index 0000000000..bd4fe6ba2c --- /dev/null +++ b/tests/issue4344-mcp-dialect.test.ts @@ -0,0 +1,226 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test 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 { MCP_DIALECT_NOTE, withMcpDialectNote } from "../src/compress-tool.ts"; +import { resolveLaneDecisions, surfaceOffDetail } from "../src/server/lane-decisions.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"; + +// #4344 (zcode rarely compresses): three gaps compounded — +// 1. the lane-decisions reachability gate read ONLY the bare `compress` +// name, so an MCP-bridged host declaring `mcp__bili__compress` (zcode / +// claude-code lanes, fixed shim server name "bili" per src/mcp.ts #2578) +// was invisible to reachability; +// 2. zcode had NO host-profile entry and inherited ANONYMOUS's +// toolsHostScopedByDefault:false — in plugin mode the surface was +// structurally ON regardless of the wire, so the nudge advertised a +// tool the model could not call (reporter log: 5+ T1 nudges at 21-29% +// usage, 0 compressions in 46 requests); +// 3. the standing prompt teaches the bare names while the model's tool +// table holds the MCP-prefixed ones. +// The fix: dialect-aware reachability + a zcode profile (host-scoped, +// observed) + an MCP name-mapping note on the standing prompt when the wire +// declares the prefixed compress and NOT the bare one. This file pins all +// three through the unit surface (resolveLaneDecisions / withMcpDialectNote) +// and the real chain (openai wire, x-bili-plugin: zcode). +// +// NOT the fix (recorded for posterity, ework's 方案1 in the issue thread): +// injecting a bare `compress` into a plugin-mode tool table. Plugin-mode +// relay passes responses VERBATIM (src/server/relay.ts — the agent's native +// loop owns the tool surface), so a model call to the injected bare name +// would surface in zcode as an unknown tool; proxy-mode server-side +// interception (src/loop/core.ts isProxyToolFor) does not run for plugin +// sessions. 方案2's prompt-side mapping is what ships here. + +process.env.NODE_ENV = "test"; + +const root = mkdtempSync(join(tmpdir(), "bili-4344-")); +test.after(() => rmSync(root, { recursive: true, force: true })); +for (const key of ["HOME", "APPHOME", "HERMES_HOME", "XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = root; + +function openaiTools(names: string[]): unknown[] { + return names.map((name) => ({ type: "function", function: { name, description: "d", parameters: { type: "object", properties: {} } } })); +} + +test("unit: zcode reachability is observed per request, in BOTH dialects (#4344)", () => { + const decide = (tools: unknown[], pluginAgent = "zcode") => + resolveLaneDecisions({ protocol: "openai", pluginMode: true, pluginAgent, parsed: { model: "m", messages: [], tools }, shouldInject: true }); + + // Bridge absent (zcode agent mode gating MCP tools / degraded shim serving + // an EMPTY tool list): surface OFF, honest — advertising `compress` against + // a tool table that holds no such tool is the #3177 "unknown tool" shape. + const absent = decide([]); + assert.equal(absent.compressSurfaceOn, false, "no-tools zcode drops the surface"); + assert.equal(absent.mcpDialect, false); + assert.equal(surfaceOffDetail(absent, true), "surface=off(no-tools)"); + + // Hostile single regular tool: still off (precision retained). + const regular = decide(openaiTools(["get_weather"])); + assert.equal(regular.compressSurfaceOn, false, "single non-bili tool stays off"); + assert.equal(surfaceOffDetail(regular, true), "surface=off(top-level=[get_weather])"); + + // Healthy bridge (MCP dialect): reachable — dialect-aware, the #4344 fix. + const bridged = decide(openaiTools(["mcp__bili__compress", "mcp__bili__acp_status", "get_weather"])); + assert.equal(bridged.compressSurfaceOn, true, "mcp__bili__compress reads reachable"); + assert.equal(bridged.mcpDialect, true, "prefixed-only declaration flips the dialect note"); + + // Bare declaration (a host that surfaces the bare names): reachable, and + // the note stays off — the prompt already names a callable tool. + const bare = decide(openaiTools(["compress", "get_weather"])); + assert.equal(bare.compressSurfaceOn, true); + assert.equal(bare.mcpDialect, false); + + // BOTH dialects declared: bare wins the note (both names callable, the + // kernel prompt's bare wording is already actionable). + const both = decide(openaiTools(["compress", "mcp__bili__compress"])); + assert.equal(both.compressSurfaceOn, true); + assert.equal(both.mcpDialect, false); +}); + +test("unit: dialect observation is lane-independent and proxy mode never flags it (#4344)", () => { + const mcpTable = openaiTools(["mcp__bili__compress"]); + + // Any host-scoped lane declaring the prefixed compress reads reachable — + // same as #3401's bare-name observation on dsh. + const dsh = resolveLaneDecisions({ protocol: "openai", pluginMode: true, pluginAgent: "dsh", parsed: { model: "m", messages: [], tools: mcpTable }, shouldInject: true }); + assert.equal(dsh.compressSurfaceOn, true, "dsh mcp-dialect reads reachable"); + assert.equal(dsh.mcpDialect, true); + + // Non-host-scoped lanes (pi/omp/opencode) keep the surface regardless of + // the table, and the note stays off: their extensions register the bare + // names, so a prefixed name on the wire is not evidence of an MCP bridge. + const pi = resolveLaneDecisions({ protocol: "openai", pluginMode: true, pluginAgent: "pi", parsed: { model: "m", messages: [], tools: [] }, shouldInject: true }); + assert.equal(pi.compressSurfaceOn, true, "pi default keeps the surface"); + assert.equal(pi.mcpDialect, false); + + // Proxy mode: bili injects the BARE tools itself, so the dialect note is + // never armed there regardless of what the client's table happens to carry. + const proxy = resolveLaneDecisions({ protocol: "openai", pluginMode: false, pluginAgent: undefined, parsed: { model: "m", messages: [], tools: mcpTable }, shouldInject: true }); + assert.equal(proxy.compressSurfaceOn, true, "proxy mode is structurally on"); + assert.equal(proxy.mcpDialect, false, "proxy mode never arms the MCP note"); +}); + +test("unit: withMcpDialectNote appends the mapping only when armed (#4344)", () => { + const prompt = "HOW TO COMPRESS: call compress."; + assert.equal(withMcpDialectNote(prompt, false), prompt, "off is identity"); + const noted = withMcpDialectNote(prompt, true); + assert.notEqual(noted, prompt); + assert.ok(noted.includes(MCP_DIALECT_NOTE), "carries the mapping note"); + assert.ok(noted.startsWith(prompt), "note is appended after the prompt, never rewrites it"); + assert.ok(MCP_DIALECT_NOTE.includes("mcp__bili__compress"), "note names the callable tool"); +}); + +// --------------------------------------------------------------------------- +// Real-chain pins: through startServer on the openai wire. +// --------------------------------------------------------------------------- + +interface ChainResult { + forwarded: Record[]; +} + +async function chainOnce(lane: "zcode" | "pi", toolNames: string[]): Promise { + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + _setForTest({}); + const store = new SessionStore({ dir: join(root, "sessions"), enabled: false }); + _setStoreForTest(store); + const forwarded: Record[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + forwarded.push(JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "chat_fixture", choices: [{ message: { role: "assistant", content: "answer" }, finish_reason: "stop" }], usage: { prompt_tokens: 500, completion_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: { "model-test": { context: 64_000 } } } }, + modelContextLimit: 200_000, kernelConfig: defaultConfig(200_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", + log: false, debug: false, passthrough: false, autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const paddr = proxy.address(); + assert(paddr && typeof paddr === "object"); + const origin = `http://127.0.0.1:${paddr.port}`; + try { + const r = await fetch(`${origin}/bili/${upstreamUrl}/v1/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": `s4344-${lane}-${toolNames.join("-") || "notools"}`, "x-bili-plugin": lane }, + body: JSON.stringify({ + model: "model-test", + messages: [{ role: "user", content: "hello" }], + ...(toolNames.length > 0 ? { tools: openaiTools(toolNames) } : {}), + max_tokens: 1024, stream: false, + }), + }); + const bodyText = await r.text(); + assert.equal(r.status, 200, bodyText); + return { forwarded }; + } finally { + store.cancelAll(); + proxy.close(); proxy.closeIdleConnections?.(); proxy.closeAllConnections?.(); + upstream.close(); upstream.closeIdleConnections?.(); upstream.closeAllConnections?.(); + await Promise.all([once(proxy, "close").catch(() => {}), once(upstream, "close").catch(() => {})]); + } +} + +test("chain: zcode with a healthy bridge gets the MCP name-mapping on the standing prompt (#4344)", async () => { + const { forwarded } = await chainOnce("zcode", ["mcp__bili__compress", "mcp__bili__acp_status", "read_file"]); + assert.equal(forwarded.length, 1); + const msgs = forwarded[0]!["messages"] as { role?: string; content?: unknown }[]; + const first = msgs[0]; + assert.equal(first?.role, "system", "standing prompt is the head system anchor"); + const sys = String(first?.content ?? ""); + assert.ok(sys.includes("HOW TO COMPRESS"), "standing compress prompt present"); + assert.ok(sys.includes(MCP_DIALECT_NOTE), "prompt carries the MCP name mapping"); + // The wire's own tool table passes through untouched in plugin mode — no + // bare `compress` injection next to the bridged names (injecting one + // would strand an unknown tool in zcode: relay is verbatim, #4344). + const tools = forwarded[0]!["tools"] as { function?: { name?: string } }[]; + const names = tools.map((t) => t.function?.name); + assert.ok(names.includes("mcp__bili__compress"), "bridged tool forwarded untouched"); + assert.ok(!names.includes("compress"), "no bare compress injected in plugin mode"); +}); + +test("chain: zcode without a bridge goes silent — no prompt, no note, no injection (#4344)", async () => { + const { forwarded } = await chainOnce("zcode", []); + assert.equal(forwarded.length, 1); + const msgs = forwarded[0]!["messages"] as { role?: string; content?: unknown }[]; + const first = msgs[0]; + const sys = first?.role === "system" ? String(first?.content ?? "") : ""; + assert.ok(!sys.includes(MCP_DIALECT_NOTE), "no dialect note without the bridge"); + assert.ok(!sys.includes("acp_status"), "no standing compress prompt advertising unreachable tools"); + assert.equal(forwarded[0]!["tools"], undefined, "nothing injected into the tool table"); +}); + +test("chain: pi keeps the bare-name prompt with no MCP note (negative control)", async () => { + const { forwarded } = await chainOnce("pi", []); + assert.equal(forwarded.length, 1); + const msgs = forwarded[0]!["messages"] as { role?: string; content?: unknown }[]; + const first = msgs[0]; + assert.equal(first?.role, "system", "standing prompt present (pi is not host-scoped)"); + const sys = String(first?.content ?? ""); + assert.ok(sys.includes("acp_status"), "full standing prompt"); + assert.ok(!sys.includes(MCP_DIALECT_NOTE), "no MCP note on a bare-name lane"); +}); diff --git a/tests/lane-decisions-matrix.test.ts b/tests/lane-decisions-matrix.test.ts index b16d0c2401..b991c04326 100644 --- a/tests/lane-decisions-matrix.test.ts +++ b/tests/lane-decisions-matrix.test.ts @@ -47,7 +47,7 @@ for (const key of ["HOME", "APPHOME", "HERMES_HOME", "XDG_STATE_HOME", "XDG_DATA type Wire = "anthropic" | "openai" | "responses" | "google"; // "proxy" = no x-bili-plugin header at all (plain client; bili injects its own tools). -type Lane = "omp" | "dsh" | "pi" | "opencode" | "proxy"; +type Lane = "omp" | "dsh" | "pi" | "opencode" | "zcode" | "proxy"; interface RowOutcome { nudge: boolean; @@ -296,6 +296,22 @@ const ROWS: { name: string; lane: Lane; tools: string[]; injectTool?: boolean; t { name: "EMERGENCY cannot punch through an unreachable dsh tool table (reverse guard)", lane: "dsh", tools: [], teach: 45_000, expect: () => ({ nudge: false, standingPrompt: false }) }, { name: "EMERGENCY override fires on the PTC run_code shape too", lane: "dsh", tools: ["run_code"], teach: 45_000, expect: () => ({ nudge: true, standingPrompt: true }) }, { name: "EMERGENCY override fires on the PTC code_run shape too (#3884)", lane: "dsh", tools: ["code_run"], teach: 45_000, expect: () => ({ nudge: true, standingPrompt: true }) }, + // #4344 FIX ROWS (zcode). zcode reaches the ACP tools ONLY through its MCP + // bridge, and bridge delivery is environmental (agent modes gate MCP tools; + // the degraded shim serves an EMPTY tool list). The profile now mirrors + // that truth: host-scoped, observed per request — a lane whose bridge is + // absent goes silent instead of advertising a tool the model cannot call + // (the #4344 shape: 5+ T1 nudges at 21-29% usage, 0 compressions in 46 + // requests). The dialect row is the companion fix: a healthy bridge + // declares mcp__bili__compress, which counts as reachable (#4344). + { name: "zcode no-tools drops the surface (#4344: bridge absent → do not advertise)", lane: "zcode", tools: [], expect: () => ({ nudge: false, standingPrompt: false }) }, + { name: "zcode mcp__bili__compress keeps the surface (#4344 dialect-aware reachability)", lane: "zcode", tools: ["mcp__bili__compress", "mcp__bili__acp_status"], expect: () => ({ nudge: true, standingPrompt: true }) }, + { name: "zcode bare compress keeps the surface (#4344 observed declaration)", lane: "zcode", tools: ["compress", "get_weather"], expect: () => ({ nudge: true, standingPrompt: true }) }, + { name: "zcode single regular tool still drops the surface (precision retained)", lane: "zcode", tools: ["get_weather"], expect: () => ({ nudge: false, standingPrompt: false }) }, + // Dialect reachability is lane-independent: any host-scoped lane whose + // wire carries the MCP-prefixed compress reads reachable, same as #3401's + // bare-name observation on dsh. + { name: "dsh mcp__bili__compress keeps the surface (#4344 dialect is lane-independent)", lane: "dsh", tools: ["mcp__bili__compress"], expect: () => ({ nudge: true, standingPrompt: true }) }, ]; for (const wire of ["anthropic", "openai", "responses", "google"] as const) {