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
24 changes: 23 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaultConfig, type Config, type Prompts } from "acp-kernel";
import { defaultConfig, RETRIEVE_TOOL_NAME, type Config, type Prompts } from "acp-kernel";
import { readFileSync, existsSync, mkdirSync, writeFileSync } from "node:fs";
import { dirname } from "node:path";
import { configFile } from "./paths.js";
Expand Down Expand Up @@ -646,6 +646,27 @@ export function passthroughState(env: NodeJS.ProcessEnv): { enabled: boolean; so
return { enabled: filePassthrough, source: filePassthrough ? "file" : null };
}

/** [#1345] ccr.toolName is part of the plugin manifest's static surface: the
* manifest reads only the global compress block, so provider/model-level
* overrides apply exclusively in proxy mode. Warn at load — a half-applied
* override (declared one way, dispatched another) must surface here, never
* as an unknown-tool rejection mid-session. */
export function warnCcrToolNameOverrides(routes: ProviderRoutes, globalCcr: CompressSettings["ccr"]): void {
const base = globalCcr?.toolName ?? RETRIEVE_TOOL_NAME;
for (const [origin, route] of Object.entries(routes)) {
const pName = route.compress?.ccr?.toolName;
if (pName !== undefined && pName !== base) {
loggerLog("warn", `[acp-config] providers["${origin}"].compress.ccr.toolName="${pName}" differs from the base name "${base}" — plugin mode advertises the base name and ignores the override; proxy mode resolves deepest-wins (#1345)`);
}
for (const [model, entry] of Object.entries(route.models ?? {})) {
const mName = entry.compress?.ccr?.toolName;
if (mName !== undefined && mName !== base) {
loggerLog("warn", `[acp-config] providers["${origin}"].models["${model}"].compress.ccr.toolName="${mName}" differs from the base name "${base}" — plugin mode advertises the base name and ignores the override; proxy mode resolves deepest-wins (#1345)`);
}
}
}
}

export function loadOptions(env: NodeJS.ProcessEnv = process.env): ProxyOptions {
// --- Source 1: JSON config file (~/.config/billion-context/billion-context.json) ---
// The canonical, user-editable config. Loaded first so env vars below can
Expand Down Expand Up @@ -731,6 +752,7 @@ export function loadOptions(env: NodeJS.ProcessEnv = process.env): ProxyOptions
throw new Error(`[acp-config] invalid upstream proxy for ${url}: ${String(error)}`);
}
}
warnCcrToolNameOverrides(routes, fileConfig.compress?.ccr);
return {
port,
host,
Expand Down
4 changes: 2 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { executeProxyTool } from "./loop/core.js";
import { normalizeSseLineEndings } from "./sse-util.js";
import { composeStreamFilters, containsMarkerLineText, containsRenderTagText, createMarkerLineFilter, createTagEchoFilter, mayStartMarkerLine, mayStartRenderTag, stripAcpTags, stripAnthropicText, stripOpenaiChatText, stripResponsesText, type TagEchoFilter } from "./loop/tag-echo-filter.js";
import { log as loggerLog } from "./logger.js";
import { ccrEnabled, contentStoreOf, retrieveToolName } from "./store.js";
import { advertisedRetrieveToolName, ccrEnabled, contentStoreOf, retrieveToolName } from "./store.js";
import { imageUsageSuffix } from "./image-compress.js";
import { emitStreamError, emitUpstreamTruncation } from "./stream-error.js";
import { degenerateTurnWarning } from "./degenerate-turn.js";
Expand Down Expand Up @@ -567,7 +567,7 @@ export function handlePluginManifest(res: import("node:http").ServerResponse, co
// openai lanes in plugin mode; the responses wire is deliberately NOT advertised —
// that proxy disarms CCR there, so advertising would break #1192.
const ccrOn = config.ccr?.enabled === true;
const ccrName = config.ccr?.toolName ?? RETRIEVE_TOOL_NAME;
const ccrName = advertisedRetrieveToolName(config.ccr);
const ccrTools = ccrOn ? retrieveToolsFor(ccrName) : undefined;
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({
Expand Down
10 changes: 8 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import {
} from "acp-kernel/wire";
import { ABSORB_TOOL, ABSORB_TOOL_GOOGLE, ABSORB_TOOL_OPENAI, ABSORB_TOOL_RESPONSES, COMPRESS_TOOL, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_RESPONSES, BILI_ACP_READONLY_TOOLS_RESPONSES, COMPRESS_TOOL_NAME, IMAGE_FULL_TOOL, IMAGE_FULL_TOOL_GOOGLE, IMAGE_FULL_TOOL_OPENAI, IMAGE_FULL_TOOL_RESPONSES, RULE_TOOL, RULE_TOOL_GOOGLE, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, retrieveToolsFor, buildAbsorbSystemPrompt, buildCompressSystemPrompt, buildCompressHybridSystemPrompt, withConversationIdNote, withMarkerIntegrityNote, withStagedCompressGuidance, withSummaryBudgetNote } from "./compress-tool.js";
import { applyAbsorbView, absorbEnabled, absorbToolName, storeEffectiveAbsorb } from "./absorb.js";
import { adoptContentStore, ccrEnabled, ccrPluginWireOk, contentStoreOf, drainPendingRetrievals, executeRetrieve, retrieveToolName, storeEffectiveCcr, type CcrSettings } from "./store.js";
import { adoptContentStore, ccrEnabled, ccrPluginWireOk, contentStoreOf, drainPendingRetrievals, executeRetrieve, normalizePluginCcrName, retrieveToolName, storeEffectiveCcr, type CcrSettings } from "./store.js";
import { applyImageCompressionPass, imageCompressionEnabled, imageFullTrailingNote, storeEffectiveImageCompression, type ImageCompressionSettings } from "./image-compress.js";
import { rulesEnabled, storeEffectiveRules } from "./rules-feature.js";
import { rewriteJsonResponse, type RewriteCtx } from "./stream.js";
Expand Down Expand Up @@ -1852,7 +1852,13 @@ async function handle(
// retrieval channel (silent loss). Route-scoped-only enablement stays
// proxy-mode-only (the proxy injects the tool itself, per-request).
const pluginCcrOk = !pluginMode || (ccrPluginWireOk(protocol) && opts.compress.ccr?.enabled === true);
storeEffectiveCcr(session, opts.compress.injectTool && pluginCcrOk && storeChannelOk && resolvedCcrCfg?.enabled === true ? resolvedCcrCfg : undefined);
let ccrCfg = opts.compress.injectTool && pluginCcrOk && storeChannelOk && resolvedCcrCfg?.enabled === true ? resolvedCcrCfg : undefined;
// [#1345] plugin mode: the manifest is the session's static tool
// surface and advertises the BASE-config name — normalize the stamp so
// manifest, gates, and placeholder hints all agree even when a
// provider/model override resolved a different toolName.
if (pluginMode && ccrCfg) ccrCfg = normalizePluginCcrName(ccrCfg, opts.compress.ccr, session, log);
storeEffectiveCcr(session, ccrCfg);
// [#1095] same channel/plugin-mode gating as CCR: image_full's restore
// round-trip needs a tool channel on this wire; without one the model
// could request originals it never gets back (silent-loss trap).
Expand Down
31 changes: 31 additions & 0 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,37 @@ export function effectiveCcr(session: Session | undefined): CcrSettings | undefi
return undefined;
}

/** [#1345] The name the plugin manifest advertises: the BASE-config
* ccr.toolName — handlePluginManifest reads only the global compress block,
* route/model merges never reach it. */
export function advertisedRetrieveToolName(ccr: CcrSettings | undefined): string {
return ccr?.toolName ?? RETRIEVE_TOOL_NAME;
}

/** [#1345] Plugin mode stamps the session under the ADVERTISED name: a
* provider/model toolName override would otherwise dispatch under a name
* the host never registered — the model's calls rejected as unknown tools
* while CCR reports enabled. Proxy mode injects the tool per request under
* the session name, so per-route renaming keeps working there. Warns once
* per session. */
export function normalizePluginCcrName(
stamped: CcrSettings,
optsCcr: CcrSettings | undefined,
session: Session,
log: (level: string, msg: string) => void,
): CcrSettings {
const advertised = advertisedRetrieveToolName(optsCcr);
if ((stamped.toolName ?? RETRIEVE_TOOL_NAME) === advertised) return stamped;
if (session.metadata.ccrNameOverrideWarned !== true) {
log(
"warn",
`[ccr] plugin mode: ignoring provider/model ccr.toolName "${stamped.toolName ?? RETRIEVE_TOOL_NAME}" — the manifest advertises "${advertised}" (base config); per-route renaming stays a proxy-mode feature (#1345)`,
);
session.metadata.ccrNameOverrideWarned = true;
}
return { ...stamped, toolName: advertised };
}

export function ccrEnabled(session: Session | undefined): boolean {
return effectiveCcr(session)?.enabled === true;
}
Expand Down
125 changes: 125 additions & 0 deletions tests/issue1345-plugin-ccr-name.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import test from "node:test";
import assert from "node:assert/strict";
import { setLogCapture } from "../src/logger.ts";
import { advertisedRetrieveToolName, normalizePluginCcrName, retrieveToolName, storeEffectiveCcr } from "../src/store.ts";
import { warnCcrToolNameOverrides } from "../src/config.ts";
import { handlePluginManifest } from "../src/plugin.ts";
import { applyCompressSettings } from "../src/compress-settings.ts";
import { defaultConfig } from "acp-kernel";
import { createInitialState } from "acp-kernel";
import type { Session } from "../src/session.ts";
import type { ProviderRoutes } from "../src/config.ts";

/**
* #1345: the plugin manifest advertises the BASE-config ccr.toolName while
* execution gated on the session-effective (deepest-wins) name. A
* provider/model-level toolName override left the plugin host registering one
* name while the runtime dispatched another — model calls rejected as unknown
* tools with CCR reporting enabled.
*
* Fix contract: plugin mode normalizes the session stamp to the advertised
* (base) name and warns (once per session at runtime, once per load at
* config load); proxy mode keeps the per-route rename untouched.
*/

function freshSession(id: string): Session {
return {
id,
meta: {},
stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 },
metadata: {},
state: createInitialState(),
createdAt: Date.now(),
lastSeen: Date.now(),
blockContents: new Map(),
inFlight: 0,
persisted: false,
pendingRetrievals: [],
};
}

test("normalizePluginCcrName: divergent override is normalized to the advertised name, warned once", () => {
const session = freshSession("n1");
const logs: string[] = [];
const log = (_l: string, m: string) => logs.push(m);
const resolved = { enabled: true, toolName: "retrieve_original", minToolTokens: 500 };

const normalized = normalizePluginCcrName(resolved, undefined, session, log);
assert.equal(normalized.toolName, "acp_retrieve", "falls back to the kernel default as advertised");
assert.equal(normalized.minToolTokens, 500, "sub-fields ride along untouched");
assert.equal(logs.length, 1, "warned once");
assert.match(logs[0]!, /plugin mode: ignoring provider\/model ccr\.toolName "retrieve_original"/);
assert.match(logs[0]!, /manifest advertises "acp_retrieve"/);

// Second request on the same session: still normalized, no repeat warn.
const again = normalizePluginCcrName(resolved, undefined, session, log);
assert.equal(again.toolName, "acp_retrieve");
assert.equal(logs.length, 1, "warn flag sticky per session");
});

test("normalizePluginCcrName: matching name (explicit or default) passes through untouched", () => {
const session = freshSession("n2");
const logs: string[] = [];
const log = (_l: string, m: string) => logs.push(m);

const same = { enabled: true, toolName: "get_context" };
const out = normalizePluginCcrName(same, { enabled: true, toolName: "get_context" }, session, log);
assert.equal(out, same, "identical reference — nothing to do");
assert.equal(logs.length, 0);

// No override anywhere: undefined resolves to the same default on both
// sides — no warn, no copy.
const noOverride = { enabled: true };
const out2 = normalizePluginCcrName(noOverride, { enabled: true }, session, log);
assert.equal(out2, noOverride);
assert.equal(logs.length, 0);
});

test("stamped session dispatches under the advertised name end-to-end", () => {
const session = freshSession("n3");
const logs: string[] = [];
const resolved = { enabled: true, toolName: "retrieve_original" };
storeEffectiveCcr(session, normalizePluginCcrName(resolved, { enabled: true, toolName: "acp_retrieve" }, session, (_l, m) => logs.push(m)));
// The manifest, the gate, and the placeholder hint all read the same name.
assert.equal(retrieveToolName(session), advertisedRetrieveToolName({ enabled: true, toolName: "acp_retrieve" }));
assert.equal(retrieveToolName(session), "acp_retrieve");
});

test("manifest advertises the base name even when a provider override exists", () => {
const routes: ProviderRoutes = {
"https://example.com": {
compress: { ccr: { enabled: true, toolName: "retrieve_original" } },
models: { "some-model": { compress: { ccr: { toolName: "get_context" } } } },
},
};
const baseConfig = applyCompressSettings(defaultConfig(100000), 100000, { ccr: { enabled: true } });

let manifest = "";
const res = {
writeHead: () => {},
end: (body: string) => {
manifest = body;
},
} as unknown as import("node:http").ServerResponse;
handlePluginManifest(res, baseConfig);
const parsed = JSON.parse(manifest) as { toolNames: string[] };
assert.ok(parsed.toolNames.includes("acp_retrieve"), "base-config name advertised");
assert.ok(!parsed.toolNames.includes("retrieve_original"), "override never reaches the manifest");

// Config load surfaces the half-applied override at load time.
const captured: string[] = [];
setLogCapture((_level, msg) => captured.push(msg));
try {
warnCcrToolNameOverrides(routes, { enabled: true });
assert.equal(captured.length, 2, "provider + model overrides both named");
assert.match(captured[0]!, /providers\["https:\/\/example\.com"\]\.compress\.ccr\.toolName="retrieve_original"/);
assert.match(captured[0]!, /base name "acp_retrieve"/);
assert.match(captured[1]!, /models\["some-model"\]\.compress\.ccr\.toolName="get_context"/);
// Matching names stay silent.
captured.length = 0;
warnCcrToolNameOverrides({ "https://ok.example": { compress: { ccr: { toolName: "acp_retrieve" } } } }, { enabled: true });
assert.equal(captured.length, 0);
} finally {
setLogCapture(null);
}
});
Loading