Skip to content
Merged
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
25 changes: 19 additions & 6 deletions src/compress-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,21 @@ export const BILI_SEARCH_CONTEXT_TOOL_GOOGLE = {

// #1179 CCR v2: host-side range-restore extension of decompress. Optional
// startId/endId (mNNNNN refs) restore only the block's messages inside that
// span instead of the whole block. Served unconditionally on every wire + the
// plugin manifest (one definition, no drift — same rule as conversation_id
// above); execution is gated on CCR being armed for the session
// (resolveDecompressRange fails explicitly when it is not).
// span instead of the whole block. Execution is gated on CCR being armed for
// the session (resolveDecompressRange fails explicitly when it is not), so
// the params are advertised ONLY where they can work (#1712): proxy-lane wire
// injection serves the *_NO_RANGE variant while the session is unarmed, and
// the plugin manifest serves them only when the base config enables CCR
// (plugin policy is the base block verbatim, #1345) — same conservative rule
// as acp_retrieve/absorb/rule in handlePluginManifest (#1192/#1271). Blank or
// whitespace values are treated as omitted at execution (whole-block restore).
const DECOMPRESS_RANGE_PARAM_START = {
type: "string",
description: "Optional mNNNNN message ref, inclusive lower bound of a sub-range of this block. With endId, restores only that span instead of the whole block (requires CCR: compress.ccr.enabled).",
description: "Optional mNNNNN message ref, inclusive lower bound of a sub-range of this block. With endId, restores only that span instead of the whole block (requires CCR: compress.ccr.enabled). Omit both to restore the whole block; empty values count as omitted.",
};
const DECOMPRESS_RANGE_PARAM_END = {
type: "string",
description: "Optional mNNNNN message ref, inclusive upper bound. Used together with startId.",
description: "Optional mNNNNN message ref, inclusive upper bound. Used together with startId; omit both to restore the whole block.",
};

function withRangeParams(schema: JsonSchemaObject): JsonSchemaObject {
Expand All @@ -166,6 +170,15 @@ export const BILI_ACP_TOOLS_ANTHROPIC = ACP_TOOLS_ANTHROPIC.map((t) => (t.name =
export const BILI_ACP_TOOLS_OPENAI = ACP_TOOLS_OPENAI.map((t) => (t.function.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_OPENAI : t.function.name === DECOMPRESS_TOOL_NAME ? BILI_DECOMPRESS_TOOL_OPENAI : t));
export const BILI_ACP_TOOLS_RESPONSES = ACP_TOOLS_RESPONSES.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_RESPONSES : t.name === DECOMPRESS_TOOL_NAME ? BILI_DECOMPRESS_TOOL_RESPONSES : t));
export const BILI_ACP_TOOLS_GOOGLE = ACP_TOOLS_GOOGLE.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_GOOGLE : t.name === DECOMPRESS_TOOL_NAME ? BILI_DECOMPRESS_TOOL_GOOGLE : t));

// #1712: no-range variants — identical except decompress lacks startId/endId.
// Served where range restore cannot be armed so the advertised schema never
// offers what execution will refuse (see the #1179 note above).
export const BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE = ACP_TOOLS_ANTHROPIC.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL : t));
export const BILI_ACP_TOOLS_OPENAI_NO_RANGE = ACP_TOOLS_OPENAI.map((t) => (t.function.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_OPENAI : t));
export const BILI_ACP_TOOLS_RESPONSES_NO_RANGE = ACP_TOOLS_RESPONSES.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_RESPONSES : t));
export const BILI_ACP_TOOLS_GOOGLE_NO_RANGE = ACP_TOOLS_GOOGLE.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_GOOGLE : t));
export const BILI_ACP_READONLY_TOOLS_RESPONSES_NO_RANGE = ACP_READONLY_TOOLS_RESPONSES.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_RESPONSES : t));
export const BILI_ACP_READONLY_TOOLS_RESPONSES = ACP_READONLY_TOOLS_RESPONSES.map((t) => (t.name === SEARCH_CONTEXT_TOOL_NAME ? BILI_SEARCH_CONTEXT_TOOL_RESPONSES : t.name === DECOMPRESS_TOOL_NAME ? BILI_DECOMPRESS_TOOL_RESPONSES : t));

// The kernel ships no Responses-format absorb const (the four ACP tools have
Expand Down
29 changes: 21 additions & 8 deletions src/decompress-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ function toFilePointer(args: Record<string, unknown>, ctx: ProxyToolCtx, header:
* - Otherwise fall back to collectBlockContent against the unfolded view
* (ctx.compressMessages ?? ctx.messages); if that yields nothing, return
* the block summary. */
/** A decompress call asks for a range restore only when at least one range
* field carries a non-blank value (#1712). */
function hasRangeArgs(args: Record<string, unknown>): boolean {
const start = typeof args.startId === "string" ? args.startId.trim() : "";
const end = typeof args.endId === "string" ? args.endId.trim() : "";
return start !== "" || end !== "";
}

export function resolveDecompress(
args: Record<string, unknown>,
ctx: ProxyToolCtx,
Expand All @@ -123,7 +131,10 @@ export function resolveDecompress(
if (archived[blockId] !== undefined) {
return `[decompress FAILED: block ${blockId} is a pre-compaction archive — its content was in the history BEFORE the client's native compaction and is no longer reachable (replaced by the client's compaction summary). decompress is unavailable for archived blocks.]`;
}
if (typeof args.startId === "string" || typeof args.endId === "string") {
// #1712: blank/whitespace range fields mean "unspecified" — hosts and models
// emit "" for optional fields, and treating that as a range request produced
// an unsatisfiable retry loop (fill in a real range → refused in plugin mode).
if (hasRangeArgs(args)) {
return resolveDecompressRange(args, ctx, block);
}

Expand Down Expand Up @@ -253,14 +264,16 @@ export function coveredRefSpan(state: CompressionState, block: CompressionBlock)
function resolveDecompressRange(args: Record<string, unknown>, ctx: ProxyToolCtx, block: CompressionBlock): string {
const startRaw = typeof args.startId === "string" ? args.startId.trim() : "";
const endRaw = typeof args.endId === "string" ? args.endId.trim() : "";
if (!startRaw || !endRaw) return "[decompress FAILED: startId and endId must be given together]";
if (!startRaw || !endRaw) return "[decompress FAILED: startId and endId must be given together — pass both mNNNNN refs, or omit both to restore the whole block]";
if (!ccrEnabled(ctx.session)) {
// [#1207 review F3] Plugin mode structurally never arms CCR (the agent
// owns its folds; bili never executes them) — the generic "enable
// compress.ccr.enabled" advice is unsatisfiable there and would send
// the model chasing a config that cannot help.
// Range restore needs the CCR content store; a CCR-off session has none.
// In plugin mode the model cannot enable CCR itself (it lives in the
// proxy's base config — plugin policy is the base block verbatim,
// #1345), so pointing it at the setting sends it chasing a config it
// cannot change (#1207 review F3). Give it the working call instead:
// whole-block restore with just blockId (#1712).
if (typeof ctx.session.metadata.pluginAgent === "string") {
return "[decompress FAILED: range restore (startId/endId) is proxy-mode only — plugin mode owns its folds natively]";
return `[decompress FAILED: range restore (startId/endId) requires CCR, which this plugin-mode session does not have — omit startId/endId and restore the whole block: {"blockId":"${block.blockId}"}]`;
}
return "[decompress FAILED: range restore (startId/endId) requires CCR — enable compress.ccr.enabled]";
}
Expand Down Expand Up @@ -444,7 +457,7 @@ function resolveDerivedDecompress(
if (preCompactionArchiveOf(anc)[blockId] !== undefined) {
return `[decompress FAILED: block ${blockId} is a pre-compaction archive in derived session ${anc.id} — its content was replaced by the client's compaction summary and is no longer reachable.]`;
}
if (typeof args.startId === "string" || typeof args.endId === "string") {
if (hasRangeArgs(args)) {
return `[decompress FAILED: range restore (startId/endId) of derived-parent blocks is not supported — decompress "${blockId}" without range args (#1333)]`;
}
const full = args.full === true;
Expand Down
20 changes: 16 additions & 4 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import fs from "node:fs";
import path from "node:path";
import { acquireInFlight, diagnoseSuccessWithoutUsage, effectiveConfig, findSessionByCanonicalId, listSessions, markCompactionBoundary, markDirty, peekSession, releaseInFlight, withSessionLock, type Session } from "./session.js";
import { clientConversationHeader } from "./session-id.js";
import { ABSORB_TOOL_NAME, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_RESPONSES, PROXY_TOOL_NAMES, RETRIEVE_TOOL_NAME, RULE_TOOL, RULE_TOOL_NAME, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, SEARCH_CONTEXT_CONVERSATION_ID_PARAM, SEARCH_CONTEXT_TOOL_NAME, absorbToolsFor, retrieveToolsFor } from "./compress-tool.js";
import { ABSORB_TOOL_NAME, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, PROXY_TOOL_NAMES, RETRIEVE_TOOL_NAME, RULE_TOOL, RULE_TOOL_NAME, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, SEARCH_CONTEXT_CONVERSATION_ID_PARAM, SEARCH_CONTEXT_TOOL_NAME, absorbToolsFor, retrieveToolsFor } from "./compress-tool.js";
import { absorbEnabled, effectiveAbsorbConfig, isProxyToolFor } from "./absorb.js";
import { effectiveRulesEnabled, rulesEnabled } from "./rules-feature.js";
import { executeProxyTool } from "./loop/core.js";
Expand Down Expand Up @@ -627,6 +627,18 @@ export function handlePluginManifest(res: import("node:http").ServerResponse, co
const ccrOn = config.ccr?.enabled === true;
const ccrName = config.ccr?.toolName ?? RETRIEVE_TOOL_NAME;
const ccrTools = ccrOn ? retrieveToolsFor(ccrName) : undefined;
// #1712: decompress's startId/endId (range restore) executes only on CCR-armed
// sessions, so the manifest advertises them only when the base config enables
// CCR (#1345 plugin policy = base block verbatim) — same conservative #1192
// rule as acp_retrieve above. CCR-off manifests serve the no-range variants so
// a registered agent never sees range fields execution would refuse.
const acpAnthropic = ccrOn ? BILI_ACP_TOOLS_ANTHROPIC : BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE;
const acpOpenai = ccrOn ? BILI_ACP_TOOLS_OPENAI : BILI_ACP_TOOLS_OPENAI_NO_RANGE;
// Responses wire: plugin mode structurally disarms CCR there (#1271 —
// PLUGIN_CCR_WIRES excludes it), so range restore can never execute for a
// registered agent on that wire — always the no-range variant, mirroring
// how ccrTools above is never spread into the responses array.
const acpResponses = BILI_ACP_TOOLS_RESPONSES_NO_RANGE;
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({
ok: true,
Expand All @@ -635,9 +647,9 @@ export function handlePluginManifest(res: import("node:http").ServerResponse, co
version: VERSION,
toolNames: [...PROXY_TOOL_NAMES, ...(absorbTools ? [absorbName] : []), ...(rulesOn ? [RULE_TOOL_NAME] : []), ...(ccrOn ? [ccrName] : [])],
tools: {
anthropic: withSearchContextConversationDescription([...BILI_ACP_TOOLS_ANTHROPIC, ...(absorbTools ? [absorbTools.anthropic] : []), ...(rulesOn ? [RULE_TOOL] : []), ...(ccrTools ? [ccrTools.anthropic] : [])].map(withConversationIdParam)),
openai: withSearchContextConversationDescription([...BILI_ACP_TOOLS_OPENAI, ...(absorbTools ? [absorbTools.openai] : []), ...(rulesOn ? [RULE_TOOL_OPENAI] : []), ...(ccrTools ? [ccrTools.openai] : [])].map(withConversationIdParam)),
responses: withSearchContextConversationDescription([...BILI_ACP_TOOLS_RESPONSES, ...(absorbTools ? [absorbTools.responses] : []), ...(rulesOn ? [RULE_TOOL_RESPONSES] : [])].map(withConversationIdParam)),
anthropic: withSearchContextConversationDescription([...acpAnthropic, ...(absorbTools ? [absorbTools.anthropic] : []), ...(rulesOn ? [RULE_TOOL] : []), ...(ccrTools ? [ccrTools.anthropic] : [])].map(withConversationIdParam)),
openai: withSearchContextConversationDescription([...acpOpenai, ...(absorbTools ? [absorbTools.openai] : []), ...(rulesOn ? [RULE_TOOL_OPENAI] : []), ...(ccrTools ? [ccrTools.openai] : [])].map(withConversationIdParam)),
responses: withSearchContextConversationDescription([...acpResponses, ...(absorbTools ? [absorbTools.responses] : []), ...(rulesOn ? [RULE_TOOL_RESPONSES] : [])].map(withConversationIdParam)),
},
headers: { agent: PLUGIN_AGENT_HEADER, conversation: PLUGIN_CONVERSATION_HEADER, contextWindow: PLUGIN_CONTEXT_WINDOW_HEADER, maxOutput: PLUGIN_MAX_OUTPUT_HEADER, model: PLUGIN_MODEL_HEADER, instructionsMutable: PLUGIN_INSTRUCTIONS_MUTABLE_HEADER },
toolEndpoint: "/__bili/plugin/tool",
Expand Down
Loading
Loading