From 8c8f6ae82bd96f800e40bc39e442e9b4570b50df Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 21 Sep 2026 22:15:02 +0800 Subject: [PATCH 1/5] feat: content-addressed message store + acp_retrieve (built-in CCR) (#1097) Oversized tool results (>= minTokens, within the per-session budget) are now ID-referenced at arrival in proxy mode: the wire keeps a byte-stable, deterministic placeholder carrying ref/tool/size/head-preview metadata, and the original goes into a per-session content store (hash-keyed sidecar files, index persisted alongside blockContents), retrievable via an injected acp_retrieve tool on all four wires. Lossless by default: a retrieve not made costs one cheap tool call; a detail distilled away is gone for good. - store.* config (enabled/minTokens/maxStoreBytes): three-level merge, default off - file-backed lazy load: index = metadata only in session JSON; payloads in storeDir()//, deduped by content hash, codec-aware (AES-256-GCM parity with session JSON when BILI_ENCRYPTION_KEY is set) - retrieve-rate + wire byte-savings surfaced in acp_status; per-retrieve log line - v1 gate is proxy-mode only (plugin mode needs acp_retrieve in its manifest) - reversible-absorb (the digest half) deferred to a follow-up --- src/absorb.ts | 4 +- src/acp-status.ts | 19 +++- src/compress-loop-responses.ts | 6 +- src/compress-settings.ts | 2 + src/compress-tool.ts | 20 ++++ src/config.ts | 39 +++++++ src/loop/core.ts | 5 + src/paths.ts | 9 ++ src/persist.ts | 23 ++++ src/server.ts | 38 +++++-- src/session.ts | 12 +- src/store.ts | 194 +++++++++++++++++++++++++++++++++ src/stream.ts | 6 +- tests/store.test.ts | 112 +++++++++++++++++++ 14 files changed, 477 insertions(+), 12 deletions(-) create mode 100644 src/store.ts create mode 100644 tests/store.test.ts diff --git a/src/absorb.ts b/src/absorb.ts index 7d1c267399..4df28d796d 100644 --- a/src/absorb.ts +++ b/src/absorb.ts @@ -11,8 +11,9 @@ import { type Config, type CoreMessage, } from "acp-kernel"; -import { PROXY_TOOL_NAMES, RULE_TOOL_NAME } from "./compress-tool.js"; +import { PROXY_TOOL_NAMES, RETRIEVE_TOOL_NAME, RULE_TOOL_NAME } from "./compress-tool.js"; import { effectiveRulesConfig } from "./rules-feature.js"; +import { effectiveStoreConfig } from "./store.js"; import { log as loggerLog } from "./logger.js"; import type { Session } from "./session.js"; @@ -48,6 +49,7 @@ export function isProxyToolFor(name: string, session: Session | undefined, confi if (PROXY_TOOL_NAMES.has(name)) return true; const absorb = effectiveAbsorbConfig(session, config); if (absorb?.enabled === true && name === (absorb.toolName ?? ABSORB_TOOL_NAME)) return true; + if (effectiveStoreConfig(session)?.enabled === true && name === RETRIEVE_TOOL_NAME) return true; return effectiveRulesConfig(session, config)?.enabled === true && name === RULE_TOOL_NAME; } diff --git a/src/acp-status.ts b/src/acp-status.ts index 4502400fe8..611bd460d1 100644 --- a/src/acp-status.ts +++ b/src/acp-status.ts @@ -8,6 +8,7 @@ import { type CoreMessage, } from "acp-kernel"; import { getBlindTunnelStats } from "./mitm.js"; +import { effectiveStoreConfig } from "./store.js"; import { preCompactionArchiveOf, type Session } from "./session.js"; import { VERSION } from "./version.js"; @@ -23,7 +24,13 @@ export interface AcpStatusCtx { // compress mutates state mid-turn without re-running prepare, so the snapshot // goes stale and lists already-compressed refs as compressible (#389). // processTurn is pure (nodes return new objects), so the returned state is -// intentionally NOT adopted — this is a read-only recompute. + // intentionally NOT adopted — this is a read-only recompute. +function fmtBytes(n: number): string { + if (n < 1024) return `${n}B`; + if (n < 1024 * 1024) return `${(n / 1024).toFixed(1)}KiB`; + return `${(n / (1024 * 1024)).toFixed(1)}MiB`; +} + export function handleAcpStatus(args: Record, ctx: AcpStatusCtx): string { const scope = typeof args.scope === "string" ? (args.scope as "compressed" | "uncompressed") : undefined; const view = typeof args.view === "string" ? (args.view as "ranges" | "messages") : undefined; @@ -69,6 +76,16 @@ export function handleAcpStatus(args: Record, ctx: AcpStatusCtx } catch { // Base-only report; never fall back to a stale snapshot. } + const storeIdx = ctx.session.metadata.storeIndex as Record | undefined; + const storeCount = storeIdx ? Object.keys(storeIdx).length : 0; + if (effectiveStoreConfig(ctx.session)?.enabled === true && (storeCount > 0 || (ctx.session.stats.retrieveCalls ?? 0) > 0)) { + const st = ctx.session.stats; + const calls = st.retrieveCalls ?? 0; + const hits = st.retrieveHits ?? 0; + const rate = calls > 0 ? Math.round((hits / calls) * 100) : 0; + extra.push(""); + extra.push(`STORE (CCR) — ${storeCount} item(s) · ${fmtBytes(st.storedBytes ?? 0)} stored · ${fmtBytes(st.storeBytesSaved ?? 0)} saved on wire · retrieved ${hits}/${calls}${calls > 0 ? ` (${rate}%)` : ""}`); + } const archive = preCompactionArchiveOf(ctx.session); const archivedIds = Object.keys(archive); if (archivedIds.length > 0) { diff --git a/src/compress-loop-responses.ts b/src/compress-loop-responses.ts index 9ad499396c..e35639c3e0 100644 --- a/src/compress-loop-responses.ts +++ b/src/compress-loop-responses.ts @@ -6,10 +6,11 @@ import { import { handleAcpStatus } from "./acp-status.js"; import { handleAcpCache } from "./cache-ledger.js"; import { lastCompressSuffix, withSessionLock, type Session } from "./session.js"; -import { parseCompressInput, PROXY_TOOL_NAMES, MUTATING_PROXY_TOOLS, COMPRESS_TOOL_NAME, ACP_TEXT_OPEN, ACP_TEXT_CLOSE } from "./compress-tool.js"; +import { parseCompressInput, PROXY_TOOL_NAMES, MUTATING_PROXY_TOOLS, COMPRESS_TOOL_NAME, ACP_TEXT_OPEN, ACP_TEXT_CLOSE, RETRIEVE_TOOL_NAME } from "./compress-tool.js"; import { log as loggerLog } from "./logger.js"; import { applyRanges } from "./stream.js"; import { executeSearchContextTarget, resolveDecompress } from "./decompress-shared.js"; +import { effectiveStoreConfig, executeRetrieve } from "./store.js"; import { buildVisibilityMarker } from "./compress-loop.js"; import { hoistTrappedToolItems, type ToolPairItem } from "./tool-pair-order.js"; import { MAX_LOOP_ROUNDS } from "./loop/index.js"; @@ -104,6 +105,9 @@ function executeProxyTool( if (toolName === "acp_cache") { return handleAcpCache(ctx.session, args); } + if (effectiveStoreConfig(ctx.session)?.enabled === true && toolName === RETRIEVE_TOOL_NAME) { + return executeRetrieve(args, ctx.session); + } return `[Unknown proxy tool: ${toolName}]`; } diff --git a/src/compress-settings.ts b/src/compress-settings.ts index 96be05f936..398037ea93 100644 --- a/src/compress-settings.ts +++ b/src/compress-settings.ts @@ -59,6 +59,7 @@ export function mergeCompress( // like `prompts`: a model-level minToolTokens must not discard a // provider-level excludeTools. const absorbLevels = [global?.absorb, provider?.absorb, model?.absorb].filter(Boolean) as NonNullable[]; + const storeLevels = [global?.store, provider?.store, model?.store].filter(Boolean) as NonNullable[]; const reasoningLevels = [global?.reasoning, provider?.reasoning, model?.reasoning].filter(Boolean) as NonNullable[]; const reasoningGuardLevels = [global?.reasoningGuard, provider?.reasoningGuard, model?.reasoningGuard].filter(Boolean) as NonNullable[]; return { @@ -76,6 +77,7 @@ export function mergeCompress( prompts: promptLevels.length > 0 ? Object.assign({}, ...promptLevels) : undefined, acknowledgePromptsRisk: pick("acknowledgePromptsRisk"), absorb: absorbLevels.length > 0 ? Object.assign({}, ...absorbLevels) : undefined, + store: storeLevels.length > 0 ? Object.assign({}, ...storeLevels) : undefined, rules: pick("rules"), stripImages: pick("stripImages"), diff --git a/src/compress-tool.ts b/src/compress-tool.ts index 535b8003ad..f275b7241d 100644 --- a/src/compress-tool.ts +++ b/src/compress-tool.ts @@ -149,6 +149,26 @@ export const RULE_TOOL_OPENAI = { type: "function" as const, function: { name: R export const RULE_TOOL_RESPONSES = { type: "function" as const, name: RULE_TOOL_NAME, description: RULE_TOOL_DESCRIPTION, parameters: RULE_PARAM_SCHEMA }; export const RULE_TOOL_GOOGLE = { name: RULE_TOOL_NAME, description: RULE_TOOL_DESCRIPTION, parameters: RULE_PARAM_SCHEMA }; +// #1097: acp_retrieve — resolve a stored ID-referenced message back to its full +// original. Host-registered opt-in (no kernel const); synthesized in all four +// wire shapes here so every injection point (wire helpers, plugin manifest) +// serves one definition. Takes a single `ref` (the mNNNNN id printed in the +// [stored] placeholder). Read-only with respect to context: the fetched content +// rides the ephemeral tool-result channel and consumes no message ref. +const RETRIEVE_TOOL_DESCRIPTION = "Retrieve the full original text of a stored message by its id. Large tool results are replaced on the wire with a placeholder shaped like \"📦 [stored #m00423 · bash · 4213 tok] \\\"npm run build\\\" → acp_retrieve(\\\"m00423\\\")\". Call this with that ref to read the complete original back into context. Leaving the placeholder costs nothing; retrieving costs one call — fetch only when the detail matters to the current step."; +const RETRIEVE_PARAM_SCHEMA = { + type: "object", + properties: { + ref: { type: "string", description: "The stored message id to retrieve (an mNNNNN ref from a [stored] placeholder)." }, + }, + required: ["ref"], +}; +export const RETRIEVE_TOOL_NAME = "acp_retrieve"; +export const RETRIEVE_TOOL = { name: RETRIEVE_TOOL_NAME, description: RETRIEVE_TOOL_DESCRIPTION, input_schema: RETRIEVE_PARAM_SCHEMA }; +export const RETRIEVE_TOOL_OPENAI = { type: "function" as const, function: { name: RETRIEVE_TOOL_NAME, description: RETRIEVE_TOOL_DESCRIPTION, parameters: RETRIEVE_PARAM_SCHEMA } }; +export const RETRIEVE_TOOL_RESPONSES = { type: "function" as const, name: RETRIEVE_TOOL_NAME, description: RETRIEVE_TOOL_DESCRIPTION, parameters: RETRIEVE_PARAM_SCHEMA }; +export const RETRIEVE_TOOL_GOOGLE = { name: RETRIEVE_TOOL_NAME, description: RETRIEVE_TOOL_DESCRIPTION, parameters: RETRIEVE_PARAM_SCHEMA }; + export function parseCompressInput(input: unknown, callId?: string) { const parsed = parseCompressArgs(input, { callId }); if (parsed.diagnostics.quoteSalvage === true) { diff --git a/src/config.ts b/src/config.ts index 00799abb07..3a510e8ea8 100644 --- a/src/config.ts +++ b/src/config.ts @@ -224,6 +224,25 @@ export type CompressSettings = { * the client's own tool names or the agent will call its own tool. */ toolName?: string; }; + /** [#1097] Content-addressed message store (host-only; no kernel Config + * equivalent). When `enabled`, oversized tool results (>= minTokens) are + * ID-referenced at arrival: the wire keeps a deterministic placeholder and + * the original goes into the per-session content store (persisted alongside + * blockContents), retrievable via the injected `acp_retrieve` tool. Lossless + * by default — a retrieve not made costs one cheap tool call, whereas a + * distilled-away detail is gone for good. Takes precedence over absorb / + * pre-crush for the same result. Off unless explicitly enabled at some level + * (default off for v1). Merged sub-field-wise across the three levels like + * `absorb`. */ + store?: { + /** Enable ID-reference + acp_retrieve for this scope. Absent/false = off. */ + enabled?: boolean; + /** Tool results smaller than this many tokens stay verbatim (default 500). */ + minTokens?: number; + /** Per-session cap on unique stored bytes (default 2 MiB). New content + * beyond the cap is left verbatim rather than substituted. */ + maxStoreBytes?: number; + }; /** Persistent rule reminders (kernel `Config.rules`, acp-kernel >= 0.0.70). * When `enabled`, an `acp_rule` tool is injected (or advertised in the * plugin manifest): passing a short `rule` records a principle-level @@ -1010,6 +1029,26 @@ export function parseCompressSettings(v: unknown): (CompressSettings & { injectT if (ok) out.absorb = cleaned; } } + if ("store" in obj && obj.store !== undefined) { + const st = obj.store; + if (!st || typeof st !== "object" || Array.isArray(st)) { + ok = false; + } else { + const sto = st as Record; + const cleaned: NonNullable = {}; + if ("enabled" in sto) { + if (typeof sto.enabled !== "boolean") { ok = false; } + else cleaned.enabled = sto.enabled; + } + for (const key of ["minTokens", "maxStoreBytes"] as const) { + if (!(key in sto)) continue; + const v = sto[key]; + if (typeof v !== "number" || !Number.isFinite(v)) { ok = false; continue; } + cleaned[key] = v; + } + if (ok) out.store = cleaned; + } + } if ("prompts" in obj && obj.prompts !== undefined) { const prompts = obj.prompts; if (!prompts || typeof prompts !== "object" || Array.isArray(prompts)) { diff --git a/src/loop/core.ts b/src/loop/core.ts index 1205cd8fbc..5c8b08f63e 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -12,9 +12,11 @@ import { parseCompressInput, ABSORB_TOOL_NAME, RULE_TOOL_NAME, + RETRIEVE_TOOL_NAME, } from "../compress-tool.js"; import { effectiveAbsorbConfig, executeAbsorb, isProxyToolFor } from "../absorb.js"; import { effectiveRulesConfig, executeRule } from "../rules-feature.js"; +import { effectiveStoreConfig, executeRetrieve } from "../store.js"; import { applyRanges } from "../stream.js"; import { executeSearchContextTarget, resolveDecompress } from "../decompress-shared.js"; import { buildVisibilityMarker } from "../compress-loop.js"; @@ -185,6 +187,9 @@ export function executeProxyTool( if (effectiveRulesConfig(ctx.session, ctx.config)?.enabled === true && toolName === RULE_TOOL_NAME) { return executeRule(args, ctx); } + if (effectiveStoreConfig(ctx.session)?.enabled === true && toolName === RETRIEVE_TOOL_NAME) { + return executeRetrieve(args, ctx.session); + } return `[Unknown proxy tool: ${toolName}]`; } diff --git a/src/paths.ts b/src/paths.ts index bd39faa006..75b2423a12 100644 --- a/src/paths.ts +++ b/src/paths.ts @@ -47,6 +47,15 @@ export function sessionsDir(): string { return path.join(dataDir(), "sessions"); } +/** Content-store dir (#1097): hash-keyed sidecar originals, kept OUTSIDE the + * per-session JSON so whole-file encryption and .json discovery never touch + * payloads. Overridable via BILI_STORE_DIR (test isolation / custom layout). */ +export function storeDir(): string { + const env = process.env.BILI_STORE_DIR; + if (env && env.length > 0) return path.resolve(env); + return path.join(dataDir(), "store"); +} + /** Root cache dir: transient/ephemeral data (update-check throttle, etc.). */ export function cacheDir(): string { return path.join(xdg("XDG_CACHE_HOME", ".cache"), "billion-context"); diff --git a/src/persist.ts b/src/persist.ts index e6e7a5670c..3e34c290d7 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -104,6 +104,11 @@ interface PersistedSession { lastInputTokensSource?: string; overflowArmTokens?: number; contextTokens?: number; + retrieveCalls?: number; + retrieveHits?: number; + retrieveMisses?: number; + storedBytes?: number; + storeBytesSaved?: number; }; /** Free-form escape hatch (v2+). */ metadata?: Record; @@ -247,6 +252,13 @@ export class SessionStore { }); } + /** #1097: the active at-rest codec (undefined when BILI_ENCRYPTION_KEY is + * unset), shared with the content-store sidecar files so they encrypt + * identically to the session JSON. */ + payloadCodec(): StateStoreCodec | undefined { + return this.codec; + } + /** Bulk-load every persisted session from disk into a map keyed by the * REAL session id (read from the file body, not the filename). Called once * at startup before the server accepts traffic. Corrupt individual files @@ -633,6 +645,11 @@ function buildSession(parsed: PersistedSession): Session { // In-memory only — a fresh process has no pending compress fold. compressCreditTokens: 0, contextTokens: Math.max(0, stats.contextTokens ?? parsed.contextTokens ?? 0), + retrieveCalls: stats.retrieveCalls ?? 0, + retrieveHits: stats.retrieveHits ?? 0, + retrieveMisses: stats.retrieveMisses ?? 0, + storedBytes: stats.storedBytes ?? 0, + storeBytesSaved: stats.storeBytesSaved ?? 0, }, metadata: parsed.metadata ?? {}, state: mergeState(parsed.state), @@ -793,6 +810,12 @@ export function getStore(): SessionStore { return _store; } +/** #1097: active at-rest codec for content-store sidecar files (plaintext when + * BILI_ENCRYPTION_KEY is unset) — mirrors the session-JSON encryption. */ +export function storePayloadCodec(): StateStoreCodec | undefined { + return getStore().payloadCodec(); +} + /** Test hook: inject a store with a temp dir. */ export function _setStoreForTest(store: SessionStore): void { _store = store; diff --git a/src/server.ts b/src/server.ts index 5e9153cd4a..e98b4568aa 100644 --- a/src/server.ts +++ b/src/server.ts @@ -60,8 +60,9 @@ import { type GoogleSystemInstruction, type GoogleTool, } 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, RULE_TOOL, RULE_TOOL_GOOGLE, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, buildAbsorbSystemPrompt, buildCompressSystemPrompt, buildCompressHybridSystemPrompt, withConversationIdNote, withMarkerIntegrityNote, withStagedCompressGuidance, withSummaryBudgetNote } from "./compress-tool.js"; +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, RULE_TOOL, RULE_TOOL_GOOGLE, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, RETRIEVE_TOOL, RETRIEVE_TOOL_GOOGLE, RETRIEVE_TOOL_OPENAI, RETRIEVE_TOOL_RESPONSES, buildAbsorbSystemPrompt, buildCompressSystemPrompt, buildCompressHybridSystemPrompt, withConversationIdNote, withMarkerIntegrityNote, withStagedCompressGuidance, withSummaryBudgetNote } from "./compress-tool.js"; import { applyAbsorbView, absorbEnabled, absorbToolName, storeEffectiveAbsorb } from "./absorb.js"; +import { applyStoreView, effectiveStoreConfig, storeEffectiveStore, type StoreConfig } from "./store.js"; import { rulesEnabled, storeEffectiveRules } from "./rules-feature.js"; import { rewriteJsonResponse, type RewriteCtx } from "./stream.js"; import { applyRanges } from "./stream.js"; @@ -1156,6 +1157,10 @@ async function handle( let reqSurface: PackSurface = {}; let reqSurfacePack = "default"; let wsSourceForLog: string | undefined; + // [#1097] host-only content-store policy for this request scope (three-level + // merge); resolved before the session is bound, then stamped onto it below so + // every view / injection / execution site reads one value. + let resolvedStoreCfg: StoreConfig | undefined; let reqModelId: string | undefined; if (parsed && typeof parsed === "object") { // Gemini's model lives in the request path, every other wire carries it @@ -1248,8 +1253,9 @@ async function handle( } else if (operatorWindowTuned && native !== undefined && reqConfig.modelContextLimit < native) { windowShrinkReason = "operator"; } - const compressCfg = resolveCompress(opts.routes, embeddedUrl, model, opts.compress); - reqPrompts = resolveCompressPrompts(compressCfg); + const compressCfg = resolveCompress(opts.routes, embeddedUrl, model, opts.compress); + resolvedStoreCfg = compressCfg.store; + reqPrompts = resolveCompressPrompts(compressCfg); const surfaceRes = resolveCompressSurfaceDetailed(compressCfg); reqSurface = surfaceRes.surface; reqSurfacePack = surfaceRes.packName; @@ -1635,6 +1641,12 @@ async function handle( // exactly one system at index 0, #377) accept it and the head system // message stays byte-stable for the prefix cache. const pluginMode = pluginAgent !== undefined; + // [#1097] Stamp the resolved store policy. acp_retrieve needs a tool + // channel, so the store is only armed in proxy mode with tool injection + // on — we must never emit a placeholder the model cannot retrieve (silent + // loss). Plugin mode is out of scope for v1: the agent would need + // acp_retrieve advertised in the plugin manifest to avoid that same trap. + storeEffectiveStore(session, opts.compress.injectTool && !pluginMode ? resolvedStoreCfg : undefined); // #546: restore a client-shrunk output budget BEFORE the side gate so a // tool-carrying main request re-enters the pipeline at full budget (see // restoreOutputBudget for the starvation mechanism). @@ -2347,6 +2359,9 @@ function prepareAnthropic( storeEffectiveAbsorb(session, loopConfig); storeEffectiveRules(session, config); turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); + // [#1097] ID-reference oversized tool results AFTER absorb (absorb wins); + // self-gates off when the store is disabled or in plugin mode. + turn.messages = applyStoreView(turn.messages, session); // Drop sub-viability fragments before any consumer sees them: a tiny // range in the list makes batched compress attempts fail atomically // (kernel validates the whole batch). Mirrors billion-context-pi. @@ -2380,7 +2395,7 @@ function prepareAnthropic( systemOut = injectSystem(parsed, opts, prompts, loopConfig, ensureCanonicalId(session), surface, visibilityMarkers); if (injectTools) { - toolsOut = injectTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL] : []), ...(rulesActive ? [RULE_TOOL] : [])], surface?.toolPrompts); + toolsOut = injectTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL] : []), ...(rulesActive ? [RULE_TOOL] : []), ...(effectiveStoreConfig(session)?.enabled === true ? [RETRIEVE_TOOL] : [])], surface?.toolPrompts); } // Nudge as a separate trailing user message (cache-friendly): the // system block stays byte-stable so the prefix cache survives. @@ -2518,6 +2533,9 @@ function prepareOpenai( storeEffectiveAbsorb(session, loopConfig); storeEffectiveRules(session, config); turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); + // [#1097] ID-reference oversized tool results AFTER absorb (absorb wins); + // self-gates off when the store is disabled or in plugin mode. + turn.messages = applyStoreView(turn.messages, session); // Drop sub-viability fragments before any consumer sees them: a tiny // range in the list makes batched compress attempts fail atomically // (kernel validates the whole batch). Mirrors billion-context-pi. @@ -2566,7 +2584,7 @@ function prepareOpenai( // avoids double-counting it. openaiOutboundSystem = sysParts.join("\n\n"); if (injectTools) { - toolsOut = injectOpenaiTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL_OPENAI] : []), ...(rulesActive ? [RULE_TOOL_OPENAI] : [])], surface?.toolPrompts); + toolsOut = injectOpenaiTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL_OPENAI] : []), ...(rulesActive ? [RULE_TOOL_OPENAI] : []), ...(effectiveStoreConfig(session)?.enabled === true ? [RETRIEVE_TOOL_OPENAI] : [])], surface?.toolPrompts); } // Nudge as a separate trailing user message (cache-friendly). Injected // in BOTH modes (#451): plugin agents supply the ACP tools but have no @@ -2720,6 +2738,9 @@ function prepareGoogle( session.stats.compressCreditTokens = 0; storeEffectiveAbsorb(session, loopConfig); turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); + // [#1097] ID-reference oversized tool results AFTER absorb (absorb wins); + // self-gates off when the store is disabled or in plugin mode. + turn.messages = applyStoreView(turn.messages, session); // Drop sub-viability fragments before any consumer sees them (the // kernel validates a compress batch atomically). if (turn.nudge) turn.nudge.compressibleRanges = viableRanges(turn.nudge.compressibleRanges); @@ -2752,7 +2773,7 @@ function prepareGoogle( const extraSystemParts = sysParts.slice(googleClientSystem ? 1 : 0); systemInstruction = extraSystemParts.length > 0 ? { parts: sysParts.map((text) => ({ text })) } : parsed.systemInstruction; if (injectTools) { - toolsOut = injectGoogleTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL_GOOGLE] : []), ...(rulesActive ? [RULE_TOOL_GOOGLE] : [])], surface?.toolPrompts); + toolsOut = injectGoogleTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL_GOOGLE] : []), ...(rulesActive ? [RULE_TOOL_GOOGLE] : []), ...(effectiveStoreConfig(session)?.enabled === true ? [RETRIEVE_TOOL_GOOGLE] : [])], surface?.toolPrompts); } if (sysNotes.length > 0) { rebuiltContents = appendGoogleNudge(rebuiltContents, sysNotes.join("\n\n---\n\n")); @@ -2947,6 +2968,9 @@ function prepareResponses( storeEffectiveAbsorb(session, loopConfig); storeEffectiveRules(session, config); turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); + // [#1097] ID-reference oversized tool results AFTER absorb (absorb wins); + // self-gates off when the store is disabled or in plugin mode. + turn.messages = applyStoreView(turn.messages, session); // Drop sub-viability fragments before any consumer sees them: a tiny // range in the list makes batched compress attempts fail atomically // (kernel validates the whole batch). Mirrors billion-context-pi. @@ -2981,7 +3005,7 @@ function prepareResponses( responsesDevContent = devContent; rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); if (!process.env.ACP_NO_INJECT_TOOL && injectTools) { - const respExtra = [...(absorbActive ? [ABSORB_TOOL_RESPONSES] : []), ...(rulesActive ? [RULE_TOOL_RESPONSES] : [])]; + const respExtra = [...(absorbActive ? [ABSORB_TOOL_RESPONSES] : []), ...(rulesActive ? [RULE_TOOL_RESPONSES] : []), ...(effectiveStoreConfig(session)?.enabled === true ? [RETRIEVE_TOOL_RESPONSES] : [])]; toolsOut = responsesTextProtocol ? injectResponsesTool(parsed.tools, BILI_ACP_READONLY_TOOLS_RESPONSES, surface?.toolPrompts) : injectResponsesTool(parsed.tools, respExtra.length > 0 ? [...BILI_ACP_TOOLS_RESPONSES, ...respExtra] : BILI_ACP_TOOLS_RESPONSES, surface?.toolPrompts); diff --git a/src/session.ts b/src/session.ts index 33dfd65045..b6a8a545da 100644 --- a/src/session.ts +++ b/src/session.ts @@ -128,6 +128,16 @@ export type Session = { * Cleared by resetSessionCompression (native-compaction boundary). * Persisted (survives restart like the rest of stats). */ localInputEstimate?: number; + /** #1097 content store: total acp_retrieve calls issued this session. */ + retrieveCalls: number; + /** #1097: acp_retrieve calls that resolved to stored content. */ + retrieveHits: number; + /** #1097: acp_retrieve calls that missed (unknown/hallucinated ref). */ + retrieveMisses: number; + /** #1097: cumulative bytes of unique originals held in the store. */ + storedBytes: number; + /** #1097: cumulative wire bytes saved by placeholder substitution. */ + storeBytesSaved: number; }; /** Free-form escape hatch for future fields not yet promoted to typed * members. Persisted as-is (must be JSON-serializable). Use sparingly — @@ -283,7 +293,7 @@ export function getSession(id: string, meta?: { protocol?: Session["meta"]["prot const session: Session = { id, meta: { protocol: meta?.protocol, upstreamOrigin: meta?.upstreamOrigin, label: meta?.label }, - stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, compressCreditTokens: 0, contextTokens: 0 }, + 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 }, metadata: {}, state: createInitialState(), createdAt: Date.now(), diff --git a/src/store.ts b/src/store.ts new file mode 100644 index 0000000000..eb72998544 --- /dev/null +++ b/src/store.ts @@ -0,0 +1,194 @@ +import { createHash } from "node:crypto"; +import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; +import * as path from "node:path"; +import { defaultCountTokens, type CoreMessage } from "acp-kernel"; +import { log as loggerLog } from "./logger.js"; +import { getStore, storePayloadCodec } from "./persist.js"; +import { storeDir } from "./paths.js"; +import { RETRIEVE_TOOL_NAME } from "./compress-tool.js"; +import type { CompressSettings } from "./config.js"; +import type { Session } from "./session.js"; + +export type StoreConfig = NonNullable; + +/** Per-message-ref record of ID-referenced content. `hash` is the sha1 of the + * original bytes — the content address, the dedup key, and the sidecar + * filename. The rest is deterministic metadata the wire placeholder renders so + * the model can judge relevance without fetching. */ +export type StoredEntry = { + hash: string; + bytes: number; + tokens: number; + kind: "tool-result" | "image"; + tool?: string; +}; + +const EFFECTIVE_STORE_KEY = "effectiveStore"; +const INDEX_KEY = "storeIndex"; + +// Tool results below this many tokens stay verbatim on the wire — the +// placeholder overhead is not worth it, and small results are cheap to keep. +const DEFAULT_MIN_TOKENS = 500; +// Per-session cap on stored bytes (logical, per-ref). Content beyond the cap is +// left verbatim (we never emit a placeholder whose original we did not store). +const DEFAULT_MAX_STORE_BYTES = 2 * 1024 * 1024; + +/** Stamp the last-resolved store policy onto the session so the view/execution + * sites — and the plugin-tool path, which resolves sessions without a request + * context — can read it back. Mirrors storeEffectiveAbsorb (#833). */ +export function storeEffectiveStore(session: Session, store: StoreConfig | undefined): void { + session.metadata[EFFECTIVE_STORE_KEY] = store ?? null; +} + +/** Read back the last-resolved store policy stamped by {@link storeEffectiveStore}. */ +export function effectiveStoreConfig(session: Session | undefined): StoreConfig | undefined { + const meta = session?.metadata[EFFECTIVE_STORE_KEY]; + if (meta && typeof meta === "object" && typeof (meta as StoreConfig).enabled === "boolean") { + return meta as StoreConfig; + } + return undefined; +} + +export function storeEnabled(session: Session | undefined): boolean { + return effectiveStoreConfig(session)?.enabled === true; +} + +/** The per-ref index lives in session.metadata (persisted via buildRecord's + * metadata spread — atomic, compaction-proof, zero new deps, same lifecycle as + * blockContents). It holds metadata + a content hash only, NEVER payload bytes. */ +function indexOf(session: Session): Record { + const raw = session.metadata[INDEX_KEY]; + return raw && typeof raw === "object" && !Array.isArray(raw) ? (raw as Record) : {}; +} + +function setIdx(session: Session, idx: Record): void { + session.metadata[INDEX_KEY] = idx; +} + +function idSlug(id: string): string { + return createHash("sha256").update(id, "utf8").digest("hex").slice(0, 20); +} + +function poolDirFor(session: Session): string { + return path.join(storeDir(), idSlug(session.id)); +} + +function poolFileFor(session: Session, hash: string): string { + return path.join(poolDirFor(session), `${hash}.txt`); +} + +/** Store `text` under `ref`, writing the original to a hash-keyed sidecar file + * and recording only metadata in the persisted index. Idempotent: re-storing + * the same ref returns the existing entry. Returns null when disabled, too + * small, or the byte budget is exhausted (the caller then leaves the original + * on the wire rather than emitting an unretrievable placeholder). */ +export function ensureStored(session: Session, ref: string, text: string, tool?: string): StoredEntry | null { + const cfg = effectiveStoreConfig(session); + if (!cfg?.enabled || !ref || !text) return null; + const existing = indexOf(session)[ref]; + if (existing) return existing; + const minTokens = cfg.minTokens ?? DEFAULT_MIN_TOKENS; + const maxStoreBytes = cfg.maxStoreBytes ?? DEFAULT_MAX_STORE_BYTES; + const tokens = defaultCountTokens(text); + if (tokens < minTokens) return null; + const bytes = Buffer.byteLength(text, "utf8"); + const idx = indexOf(session); + const held = Object.values(idx).reduce((a, e) => a + e.bytes, 0); + if (held + bytes > maxStoreBytes) return null; + const hash = createHash("sha1").update(text, "utf8").digest("hex"); + // Payload-file-first: persist the original BEFORE recording the index entry, + // so a crash mid-arrival leaves at worst an orphan file (swept later) — never + // a placeholder pointing at missing content. + try { + const dir = poolDirFor(session); + const file = poolFileFor(session, hash); + if (!existsSync(dir)) mkdirSync(dir, { recursive: true }); + if (!existsSync(file)) { + const codec = storePayloadCodec(); + writeFileSync(file, codec ? codec.encode(text) : text); + } + } catch (err) { + loggerLog("warn", `[store] failed to persist ${ref}: ${err instanceof Error ? err.message : String(err)}`); + return null; + } + idx[ref] = { hash, bytes, tokens, kind: "tool-result", ...(tool ? { tool } : {}) }; + setIdx(session, idx); + session.stats.storedBytes = Object.values(idx).reduce((a, e) => a + e.bytes, 0); + getStore().scheduleSave(session); + return idx[ref]; +} + +/** Render the deterministic wire placeholder for a stored ref. Carries enough + * signal (tool, size, a head preview) for the model to judge relevance without + * fetching, plus the exact acp_retrieve call to make. Byte-stable for a given + * ref+original, so the upstream prefix cache stays warm across turns. */ +export function renderPlaceholder(ref: string, entry: StoredEntry, original: string): string { + const label = entry.tool ?? "tool"; + const head = original.replace(/\s+/g, " ").trim().slice(0, 60); + const headPart = head ? ` ${JSON.stringify(head)}` : ""; + return `\u{1F4E6} [stored #${ref} \u00B7 ${label} \u00B7 ${entry.tokens} tok]${headPart} \u2192 ${RETRIEVE_TOOL_NAME}("${ref}")`; +} + +/** ID-reference view: replace oversized tool-result content with a deterministic + * placeholder, storing the original in the session content store first. Runs + * after the absorb view (so model-triggered absorption wins); only shrinks the + * content INSIDE a tool result, leaving role/toolCallId pairing intact. Pure + * with respect to the wire for a given input. `storeBytesSaved` is recomputed + * (not accumulated) here so repeated re-substitution across turns does not + * inflate it — it reflects the CURRENT wire savings. */ +export function applyStoreView(messages: CoreMessage[], session: Session): CoreMessage[] { + if (effectiveStoreConfig(session)?.enabled !== true) return messages; + const cfg = effectiveStoreConfig(session)!; + const minTokens = cfg.minTokens ?? DEFAULT_MIN_TOKENS; + const byRaw = session.state.messageRefs?.byRaw ?? {}; + let changed = false; + let saved = 0; + const out = messages.map((m) => { + if (m.role !== "tool" || m.contentType !== "tool-result") return m; + const text = m.text ?? ""; + if (defaultCountTokens(text) < minTokens) return m; + const ref = byRaw[m.id]; + if (!ref) return m; + const entry = ensureStored(session, ref, text, m.toolName); + if (!entry) return m; + const placeholder = renderPlaceholder(ref, entry, text); + saved += Math.max(0, Buffer.byteLength(text, "utf8") - Buffer.byteLength(placeholder, "utf8")); + changed = true; + return { ...m, text: placeholder }; + }); + if (changed) session.stats.storeBytesSaved = saved; + return changed ? out : messages; +} + +/** Execute an acp_retrieve call: resolve a stored ref back to its full original + * text, lazy-loading the SINGLE sidecar file named by the index entry (no + * whole-blob parse). The returned content rides the ephemeral tool-result + * channel (the same intra-request re-request path as decompress) — it never + * enters fold space and consumes no message ref. Not-found costs one tool call + * and self-corrects. */ +export function executeRetrieve(args: Record, session: Session): string { + session.stats.retrieveCalls = (session.stats.retrieveCalls ?? 0) + 1; + const rawRef = args.ref; + const ref = typeof rawRef === "string" ? rawRef.trim() : ""; + if (!ref) { + session.stats.retrieveMisses = (session.stats.retrieveMisses ?? 0) + 1; + return `[${RETRIEVE_TOOL_NAME} FAILED: ref (an mNNNNN id) is required]`; + } + const entry = indexOf(session)[ref]; + if (!entry) { + session.stats.retrieveMisses = (session.stats.retrieveMisses ?? 0) + 1; + return `[${RETRIEVE_TOOL_NAME} FAILED: ${ref} not found in store]`; + } + let text: string; + try { + const buf = readFileSync(poolFileFor(session, entry.hash)); + const codec = storePayloadCodec(); + text = codec ? codec.decode(buf) : buf.toString("utf8"); + } catch { + session.stats.retrieveMisses = (session.stats.retrieveMisses ?? 0) + 1; + return `[${RETRIEVE_TOOL_NAME} FAILED: ${ref} content unavailable]`; + } + session.stats.retrieveHits = (session.stats.retrieveHits ?? 0) + 1; + loggerLog("info", `[store] retrieve ${ref} (${entry.tokens} tok, ${entry.bytes} B)${entry.tool ? ` tool=${entry.tool}` : ""}`); + return `[retrieved #${ref} \u00B7 ${entry.tokens} tok]\n${text}`; +} diff --git a/src/stream.ts b/src/stream.ts index 194d5b20ad..287b7e8039 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -2,9 +2,10 @@ import { collectBlockContent, type CompressionCore, type Config, type CoreMessag import { handleAcpStatus } from "./acp-status.js"; import { handleAcpCache, recordCacheFoldsFromBlocks } from "./cache-ledger.js"; import { type Session, cacheBlockContent, markDirty } from "./session.js"; -import { COMPRESS_TOOL_NAME, parseCompressInput, ABSORB_TOOL_NAME, type ParsedRange } from "./compress-tool.js"; +import { COMPRESS_TOOL_NAME, parseCompressInput, ABSORB_TOOL_NAME, RETRIEVE_TOOL_NAME, type ParsedRange } from "./compress-tool.js"; import { effectiveAbsorbConfig, executeAbsorb, isProxyToolFor } from "./absorb.js"; import { executeSearchContextTarget, resolveDecompress } from "./decompress-shared.js"; +import { effectiveStoreConfig, executeRetrieve } from "./store.js"; import { containsMarkerLineText, containsRenderTagText, stripAcpTags } from "./loop/tag-echo-filter.js"; import { maxShrinkPerCompress } from "./fetch-util.js"; @@ -45,6 +46,9 @@ function executeAnthropicProxyTool(toolName: string, args: Record raw === "t-res")?.[0] ?? ""; + storeEffectiveStore(session, withStore ? { enabled: true, minTokens: 500 } : { enabled: false }); + return { session, ref }; +} + +function findSidecar(hash: string): string | null { + for (const d of readdirSync(STORE_TMP)) { + const p = path.join(STORE_TMP, d, `${hash}.txt`); + if (existsSync(p)) return p; + } + return null; +} + +test("effectiveStoreConfig reflects the stamped policy", () => { + assert.equal(effectiveStoreConfig(makeSession(false).session)?.enabled, false); + assert.equal(effectiveStoreConfig(makeSession(true).session)?.enabled, true); + assert.equal(effectiveStoreConfig(undefined), undefined); +}); + +test("ensureStored: stores large content once, idempotent, skips small/disabled", () => { + const { session, ref } = makeSession(true); + const e1 = ensureStored(session, ref, BIG_TEXT, "bash"); + assert.ok(e1 && e1.tokens > 500 && e1.bytes > 0); + assert.equal(e1!.hash.length, 40); + assert.equal(ensureStored(session, ref, BIG_TEXT, "bash"), e1); + assert.equal(ensureStored(session, "m00002", "tiny", "bash"), null); + assert.equal(ensureStored(makeSession(false).session, ref, BIG_TEXT, "bash"), null); +}); + +test("applyStoreView replaces the oversized tool result with a stable placeholder, pairing intact", () => { + const { session, ref } = makeSession(true); + const out = applyStoreView(toolResult(), session); + const res = out.find((m) => m.id === "t-res")!; + assert.match(res.text!, new RegExp(`stored #${ref}`)); + assert.match(res.text!, new RegExp(RETRIEVE_TOOL_NAME)); + assert.notEqual(res.text, BIG_TEXT); + assert.equal(res.role, "tool"); + assert.equal(res.contentType, "tool-result"); + assert.equal(res.toolCallId, "call_1"); + assert.equal(out.find((m) => m.id === "a-tc")!.text, JSON.stringify({ command: "npm run build" })); + const again = applyStoreView(toolResult(), session); + assert.equal(again.find((m) => m.id === "t-res")!.text, res.text); +}); + +test("applyStoreView is a no-op when the store is disabled", () => { + const { session } = makeSession(false); + const out = applyStoreView(toolResult(), session); + assert.equal(out.find((m) => m.id === "t-res")!.text, BIG_TEXT); +}); + +test("executeRetrieve round-trips the original; a bad ref self-corrects", () => { + const { session, ref } = makeSession(true); + ensureStored(session, ref, BIG_TEXT, "bash"); + const got = executeRetrieve({ ref }, session); + assert.ok(got.includes(BIG_TEXT.slice(0, 80))); + assert.equal(session.stats.retrieveCalls, 1); + assert.equal(session.stats.retrieveHits, 1); + const miss = executeRetrieve({ ref: "m99999" }, session); + assert.match(miss, /not found/); + assert.equal(session.stats.retrieveMisses, 1); + assert.equal(session.stats.retrieveCalls, 2); +}); + +test("sidecar is written to disk under storeDir and the index lands in session.metadata", () => { + const { session, ref } = makeSession(true); + const e = ensureStored(session, ref, BIG_TEXT, "bash")!; + const file = findSidecar(e.hash); + assert.ok(file, "sidecar file was written under storeDir"); + assert.equal(readFileSync(file!, "utf8"), BIG_TEXT); + const idx = session.metadata.storeIndex as Record; + assert.equal(idx[ref].hash, e.hash); + assert.ok(!JSON.stringify(idx).includes(BIG_TEXT.slice(0, 40)), "index holds metadata only, never payload bytes"); +}); From d257fb009da18bde1512d71e4c72e8c764cb3dad Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 21 Sep 2026 22:33:40 +0800 Subject: [PATCH 2/5] fix: disarm content store where acp_retrieve has no tool channel (#1097) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR #1099 review: on the responses text/marker protocol (route compressProtocol 'marker' or ACP_COMPRESS_PROTOCOL=text) only the readonly ACP tools are declared and the text-trigger parser knows no acp_retrieve trigger; ACP_NO_INJECT_TOOL disables all injection on that wire. applyStoreView still emitted placeholders there — unretrievable by construction, i.e. the silent loss #1097 explicitly guards against everywhere else. Gate the policy stamping site (single source for all four prepare fns + plugin-tool path + acp_status) on channel availability, mirroring how absorb/rules strip themselves in text mode. Adds tests/store-marker-gate.test.ts (marker wire keeps the original verbatim; native-tool wire emits placeholder + declaration — fails on unfixed code). Also documents compress.store.{enabled,minTokens,maxStoreBytes} + BILI_STORE_DIR in CONFIGURATION/README (en+zh) and normalizes a mis-indent introduced at the stamping site. --- CONFIGURATION.md | 11 +++ CONFIGURATION.zh-CN.md | 11 +++ README.md | 2 + README.zh-CN.md | 2 + src/server.ts | 14 +++- tests/store-marker-gate.test.ts | 130 ++++++++++++++++++++++++++++++++ 6 files changed, 166 insertions(+), 4 deletions(-) create mode 100644 tests/store-marker-gate.test.ts diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 3c1f5ac02a..a60ab55000 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -360,6 +360,17 @@ For each request, the proxy resolves the settings by longest-URL-prefix match (t - `toolName: string` — rename the injected tool (default `"absorb"`); the schema, system-prompt section and per-session adjudication all follow the name. Injection follows the wire's native-tool surface: proxy mode injects the tool + a static system-prompt section on the anthropic/openai/responses native-tools wires, plugin mode advertises it in the plugin manifest (the MCP shell picks it up for free). Responses **marker/text-protocol** routes are not supported (no native tool surface — the REQUIRED absorb instruction would be unsatisfiable), and title-generation requests (`max_tokens ≤ 200`) skip injection like the compress prompt does. Absorbed pairs stay hidden across restarts (persisted in the session state). +#### `store` + +- **Type:** `object` (`{ enabled?, minTokens?, maxStoreBytes? }`) +- **Default:** *(disabled — the feature is off unless you set `enabled: true`)* +- **Status:** ACTIVE (v1 — proxy mode, native-tools wires only) +- **Description:** Opt-in **content-addressed message store** / built-in CCR (issue #1097). Instead of force-distilling oversized tool results (like `absorb`) or carrying them on the wire forever, the original goes into a per-session content store at arrival and the wire keeps a deterministic, byte-stable placeholder (`📦 [stored #m00423 · shell · 4213 tok] "head preview" → acp_retrieve("m00423")`). The model retrieves the full original on demand via the injected `acp_retrieve` tool; a retrieve is ephemeral (it rides the intra-request tool-result channel, never enters fold space, consumes no message ref). Lossless by default: a retrieve not made costs one cheap tool call; a detail distilled away by absorb is gone for good. Sub-fields (merged deepest-wins like every other CompressSettings field): + - `enabled: boolean` — master switch; anything other than `true` keeps the feature fully off (no placeholders, no tool). + - `minTokens: number` — only tool results at or above this many tokens are ID-referenced (default `500`); smaller results stay verbatim. + - `maxStoreBytes: number` — per-session cap on stored bytes, summed logically per ref (default `2 MiB`); content beyond the cap stays verbatim rather than being emitted as an unretrievable placeholder. + The index is metadata-only and persists inside the session JSON alongside `blockContents`; payloads live in hash-keyed sidecar files (`/.txt`) under `storeDir()` (env `BILI_STORE_DIR`, default `/store`), deduped by content hash and lazy-read on retrieve. Sidecars use the same at-rest codec as the session file when `BILI_ENCRYPTION_KEY` is set. Only the *content* inside a `tool` result shrinks — pairing with the assistant `tool_calls` is untouched. v1 scope gates: **proxy mode only** (plugin agents would need `acp_retrieve` advertised in their plugin manifest before a placeholder could be emitted — otherwise silent loss), and **native-tools wires only**: responses marker/text-protocol routes and `ACP_NO_INJECT_TOOL` have no channel to execute the retrieve, so the store disarms itself there instead of losing content. Per-session stats (stored bytes, current wire bytes saved, retrieve rate) surface in `acp_status`; each retrieve logs a `[store] retrieve …` line. + #### `rules` - **Type:** `boolean` diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 9a0b7d89df..fd83f0e70b 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -360,6 +360,17 @@ - `toolName: string` — 重命名注入工具(默认 `"absorb"`);模式、系统提示段与按会话裁决都跟随名称。 注入跟随线上原生工具面:代理模式在 anthropic/openai/responses 原生工具线上注入工具 + 静态系统提示段,插件模式在插件清单中广告它(MCP shell 自动拾取)。Responses **marker/文本协议**路由不支持(无原生工具面 — 强制的 absorb 指令不可满足),标题生成请求(`max_tokens ≤ 200`)跳过注入如压缩提示一样。吸收配对在重启后保持隐藏(在会话状态持久化)。 +#### `store` + +- **类型:** `object`(`{ enabled?, minTokens?, maxStoreBytes? }`) +- **默认值:** *(禁用 — 除非显式设置 `enabled: true`,该特性完全关闭)* +- **状态:** ACTIVE(v1 — 仅代理模式、仅原生工具线) +- **说明:** 可选开启的**内容寻址消息存储**/内置 CCR(issue #1097)。超大工具结果不再被强制蒸馏(如 `absorb`)或永远挂在线上:原文在到达时写入按会话的内容存储,线上保留一个确定性、字节稳定的占位符(`📦 [stored #m00423 · shell · 4213 tok] "头部预览" → acp_retrieve("m00423")`)。模型通过注入的 `acp_retrieve` 工具按需取回完整原文;retrieve 是临时的(走请求内工具结果通道,不进入折叠空间,不占用消息 ref)。默认无损:未执行的 retrieve 只花一次廉价工具调用;而被 absorb 蒸馏掉的细节则永久丢失。子字段(按字段最深层级胜出,与其他 CompressSettings 字段一致): + - `enabled: boolean` — 主开关;任何值不为 `true` 时特性完全关闭(无占位符、无工具)。 + - `minTokens: number` — 仅达到此 token 数的工具结果被 ID 引用(默认 `500`);更小的结果保持原样。 + - `maxStoreBytes: number` — 按会话的存储字节上限,按 ref 逻辑累加(默认 `2 MiB`);超出上限的内容保持原样,而不是发出不可检索的占位符。 + 索引是纯元数据,随会话 JSON 与 `blockContents` 一同持久化;载荷存放在 `storeDir()`(环境变量 `BILI_STORE_DIR`,默认 `/store`)下的哈希键边车文件(`/.txt`),按内容哈希去重,retrieve 时懒加载。设置 `BILI_ENCRYPTION_KEY` 时,边车文件使用与会话文件相同的静态加密编解码器。只有 `tool` 结果*内部的内容*缩小——与 assistant `tool_calls` 的配对不受影响。v1 范围门控:**仅代理模式**(插件 agent 需要在其插件清单中声明 `acp_retrieve`,否则占位符会造成静默丢失);且**仅限原生工具线**:responses marker/文本协议路由与 `ACP_NO_INJECT_TOOL` 没有执行 retrieve 的通道,因此存储在这些场景下自动解除武装,而不是丢失内容。按会话统计(已存字节、当前线上节省字节、retrieve 率)在 `acp_status` 中展示;每次 retrieve 记录一条 `[store] retrieve …` 日志。 + #### `rules` - **类型:** `boolean` diff --git a/README.md b/README.md index d7b081b88b..bf863917e5 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,8 @@ An opt-in fifth tool, `absorb` (`compress.absorb.enabled: true` — see [CONFIGU An opt-in sixth tool, `acp_rule` (`compress.rules: true` — see [CONFIGURATION.md](CONFIGURATION.md)), records **persistent principle-level reminders**: a short rule recorded by the model (user-emphasized lessons, behaviors to remember, major pitfalls hit) is hard-protected from compression — the call and its result stay in context across every fold — and omitting the argument lists the recorded rules ([ranxianglei/billion-context-pi#433](https://github.com/ranxianglei/billion-context-pi/issues/433)). +An opt-in seventh tool, `acp_retrieve` (`compress.store.enabled: true` — see [CONFIGURATION.md](CONFIGURATION.md)), backs the **content-addressed message store** (built-in CCR, #1097): oversized tool results are **ID-referenced at arrival instead of force-distilled** — the wire keeps a byte-stable placeholder and the original goes into a per-session, hash-deduped sidecar store, retrievable on demand via one cheap tool call. Lossless by default: a retrieve not made costs nothing but the call; a detail distilled away by absorb is gone for good. Proxy mode and native-tools wires only in v1 (the marker/text protocol has no channel to execute the retrieve, so the store disarms itself there rather than silently losing content). + A sibling protection knob, `compress.protectedLatestTools` (see [CONFIGURATION.md](CONFIGURATION.md)), keeps the **latest** snapshot of a cumulative tool (a client's todo/task list, e.g. `["todo_list", "TodoWrite"]`) un-compressible while older instances fold normally — so the agent never loses its live task list to a fold (#639). Its full-history counterpart `compress.protectedTools` hard-excludes **every** instance of a tool — for independent-content results no later result supersedes (e.g. opencode/pi `skill` loads); protecting all instances of a chatty or cumulative-snapshot tool grows context without bound (#639), so keep it to low-frequency, high-value tools. ### Two compression modes — who executes `compress` diff --git a/README.zh-CN.md b/README.zh-CN.md index 3f84da5acb..a70c5517f7 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -72,6 +72,8 @@ QQ群: 可选的第六个工具 `acp_rule`(`compress.rules: true` —— 见 [CONFIGURATION.zh-CN.md](CONFIGURATION.zh-CN.md))记录**持久化的原则级提醒**:模型记录的简短规则(用户强调的教训、要求记住的行为、撞到的大坑)受硬性保护不被压缩——调用及结果在每次折叠中都保留在上下文中——省略参数则列出已记录规则([ranxianglei/billion-context-pi#433](https://github.com/ranxianglei/billion-context-pi/issues/433))。 +可选的第七个工具 `acp_retrieve`(`compress.store.enabled: true` —— 见 [CONFIGURATION.zh-CN.md](CONFIGURATION.zh-CN.md))支撑**内容寻址消息存储**(内置 CCR,#1097):超大工具结果**在到达时改为 ID 引用,而非强制蒸馏**——线上保留字节稳定的占位符,原文进入按会话、按内容哈希去重的边车存储,模型通过一次廉价工具调用按需取回。默认无损:未执行的 retrieve 只花一次调用;而被 absorb 蒸馏掉的细节则永久丢失。v1 仅代理模式、仅原生工具线(marker/文本协议没有执行 retrieve 的通道,存储在这些场景下自动解除武装,而不是静默丢失内容)。 + 同族的保护开关 `compress.protectedLatestTools`(见 [CONFIGURATION.zh-CN.md](CONFIGURATION.zh-CN.md))让累积型工具(客户端的 todo/任务清单,如 `["todo_list", "TodoWrite"]`)的**最新**快照永远不被压缩,旧实例照常折叠 —— agent 的活跃任务清单不会在折叠中丢失(#639)。其全历史对应项 `compress.protectedTools` 对工具的**全部实例**做硬排除 —— 适用于各次结果相互独立、后续结果不会取代旧结果的内容(如 opencode/pi 的 `skill` 加载);对高频或累积快照型工具保护全部实例会让上下文无界增长(#639),请只用于低频高价值工具。 **如何确认压缩真的生效了。** 代理执行 `compress` 后会以普通 assistant 文本发出确认标记(`📦 [ACP] Compressed …`)—— 但曾观察到模型在持续上下文压力下*自行书写该标记格式*而从未调用工具(#717):约 2 小时内 17 次假"压缩",真实用量一路爬到 89%。因此对话中看到的标记行本身不是持久化完成的证据 —— 请先用 `acp_status` 复核(块数 +1、可压缩区间起点前移)再采信。作为兜底,代理会剥离模型自发的标记形文本并记录 `[marker-echo]` 警告;注入的 nudge 与系统提示词也明确声明标记只由代理发出。 diff --git a/src/server.ts b/src/server.ts index e98b4568aa..6902d4a9be 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1253,9 +1253,9 @@ async function handle( } else if (operatorWindowTuned && native !== undefined && reqConfig.modelContextLimit < native) { windowShrinkReason = "operator"; } - const compressCfg = resolveCompress(opts.routes, embeddedUrl, model, opts.compress); - resolvedStoreCfg = compressCfg.store; - reqPrompts = resolveCompressPrompts(compressCfg); + const compressCfg = resolveCompress(opts.routes, embeddedUrl, model, opts.compress); + resolvedStoreCfg = compressCfg.store; + reqPrompts = resolveCompressPrompts(compressCfg); const surfaceRes = resolveCompressSurfaceDetailed(compressCfg); reqSurface = surfaceRes.surface; reqSurfacePack = surfaceRes.packName; @@ -1646,7 +1646,13 @@ async function handle( // on — we must never emit a placeholder the model cannot retrieve (silent // loss). Plugin mode is out of scope for v1: the agent would need // acp_retrieve advertised in the plugin manifest to avoid that same trap. - storeEffectiveStore(session, opts.compress.injectTool && !pluginMode ? resolvedStoreCfg : undefined); + // The responses text/marker protocol has no native tool channel either + // (absorb/rules strip themselves there for the same reason), and + // ACP_NO_INJECT_TOOL disables all injection on that wire — both would + // leave placeholders unretrievable. + const storeChannelOk = protocol !== "responses" || + (!process.env.ACP_NO_INJECT_TOOL && !FORCE_TEXT_PROTOCOL && resolveCompressProtocol(opts.routes, upstreamOrigin) !== "marker"); + storeEffectiveStore(session, opts.compress.injectTool && !pluginMode && storeChannelOk ? resolvedStoreCfg : undefined); // #546: restore a client-shrunk output budget BEFORE the side gate so a // tool-carrying main request re-enters the pipeline at full budget (see // restoreOutputBudget for the starvation mechanism). diff --git a/tests/store-marker-gate.test.ts b/tests/store-marker-gate.test.ts new file mode 100644 index 0000000000..37254bb976 --- /dev/null +++ b/tests/store-marker-gate.test.ts @@ -0,0 +1,130 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { mkdtempSync, rmSync } from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +// [#1097] The store must never emit a placeholder the model cannot retrieve. +// The responses text/marker protocol declares only the readonly ACP tools (no +// native tool channel for acp_retrieve), so an armed store there would silently +// lose oversized tool results. These tests pin the gate at the policy-stamping +// site: marker protocol → no placeholder, original stays verbatim; native tool +// protocol → placeholder + acp_retrieve declaration. +const PLACEHOLDER_MARK = "[stored #"; +const BIG_OUTPUT = "big-output-line-0123456789abcdef\n".repeat(512); + +interface Harness { + proxyPort: number; + upstreamUrl: string; + captured: string[]; + close(): Promise; +} + +async function startHarness(marker: boolean): Promise { + const captured: string[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + captured.push(Buffer.concat(chunks).toString("utf8")); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "resp_gate", status: "completed", output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: "ok" }] }] })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = upstream.address().port; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const storeDir = mkdtempSync(path.join(os.tmpdir(), "bili-store-gate-")); + process.env.BILI_STORE_DIR = storeDir; + const route: Record = { models: { "gpt-test": { context: 400_000 } } }; + if (marker) route.compressProtocol = "marker"; + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: route } as ProxyOptions["routes"], + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + promptCache: { routing: "auto" }, + compress: { injectTool: true, injectNudge: false, store: { enabled: true, minTokens: 500 } }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + return { + proxyPort: proxy.address().port, + upstreamUrl: `http://127.0.0.1:${upstreamPort}`, + captured, + close: async () => { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + rmSync(storeDir, { recursive: true, force: true }); + }, + }; +} + +async function sendBigToolResult(h: Harness, sessionId: string): Promise { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/${h.upstreamUrl}/v1/responses`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": sessionId }, + body: JSON.stringify({ + model: "gpt-test", + stream: false, + session_id: sessionId, + instructions: "s", + input: [ + { type: "message", role: "user", content: [{ type: "input_text", text: "run the big script" }] }, + { type: "function_call", name: "shell", arguments: "{\"cmd\":\"gen\"}", call_id: "call_big" }, + { type: "function_call_output", call_id: "call_big", output: BIG_OUTPUT }, + ], + }), + }); + const text = await resp.text(); + assert.equal(resp.status, 200, `request failed: ${text.slice(0, 300)}`); +} + +test("#1097: marker-protocol responses wire — store disarmed, oversized tool result stays verbatim (no unretrievable placeholder)", async () => { + const h = await startHarness(true); + try { + await sendBigToolResult(h, "store-gate-marker"); + assert.equal(h.captured.length, 1, `expected exactly one upstream request, got ${h.captured.length}`); + const body = h.captured[0]!; + assert.ok(!body.includes(PLACEHOLDER_MARK), "marker-protocol wire must not carry a store placeholder"); + assert.ok(!body.includes("acp_retrieve"), "acp_retrieve must not be declared or referenced on the marker-protocol wire"); + const line = "big-output-line-0123456789abcdef"; + assert.ok(body.includes(`${line}\\n${line}`), "original tool result must stay verbatim when the store is disarmed"); + assert.ok(body.length > BIG_OUTPUT.length, "full tool result payload missing from the outbound body"); + } finally { + await h.close(); + } +}); + +test("#1097: native-tool responses wire — store armed, placeholder emitted and acp_retrieve declared (control)", async () => { + const h = await startHarness(false); + try { + await sendBigToolResult(h, "store-gate-control"); + assert.equal(h.captured.length, 1, `expected exactly one upstream request, got ${h.captured.length}`); + const body = h.captured[0]!; + assert.ok(body.includes(PLACEHOLDER_MARK), `native-tool wire must carry the store placeholder: ${body.slice(0, 400)}`); + assert.ok(body.includes('"name":"acp_retrieve"'), "acp_retrieve tool declaration missing on the native-tool wire"); + } finally { + await h.close(); + } +}); From f12ec03efca69053a8bc94246ab632915cd44a60 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Mon, 21 Sep 2026 23:04:59 +0800 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20reversible-absorb=20=E2=80=94=20sum?= =?UTF-8?q?marizing=20a=20stored=20item=20keeps=20its=20original=20retriev?= =?UTF-8?q?able=20(#1097)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The v1 content-addressed store already keeps oversized tool results lossless by default. This makes the two fidelities explicit so the model can pick gist-or-full: the wire placeholder now states that summarizing is reversible (the original persists under the same ref), and a regression test proves absorb (a model-written digest) and acp_retrieve (the full original) coexist under one ref. Pure composition of the existing store + absorb — no new tool, no model-call path. --- src/store.ts | 9 ++++++--- tests/store.test.ts | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/store.ts b/src/store.ts index eb72998544..4a7f42238f 100644 --- a/src/store.ts +++ b/src/store.ts @@ -120,13 +120,16 @@ export function ensureStored(session: Session, ref: string, text: string, tool?: /** Render the deterministic wire placeholder for a stored ref. Carries enough * signal (tool, size, a head preview) for the model to judge relevance without - * fetching, plus the exact acp_retrieve call to make. Byte-stable for a given - * ref+original, so the upstream prefix cache stays warm across turns. */ + * fetching, plus the exact acp_retrieve call to make. It also states that + * summarizing is REVERSIBLE — the original persists under this ref — so the + * model can freely pick gist (absorb) or full (retrieve); both fidelities + * coexist under the same id. Byte-stable for a given ref+original, so the + * upstream prefix cache stays warm across turns. */ export function renderPlaceholder(ref: string, entry: StoredEntry, original: string): string { const label = entry.tool ?? "tool"; const head = original.replace(/\s+/g, " ").trim().slice(0, 60); const headPart = head ? ` ${JSON.stringify(head)}` : ""; - return `\u{1F4E6} [stored #${ref} \u00B7 ${label} \u00B7 ${entry.tokens} tok]${headPart} \u2192 ${RETRIEVE_TOOL_NAME}("${ref}")`; + return `\u{1F4E6} [stored #${ref} \u00B7 ${label} \u00B7 ${entry.tokens} tok]${headPart} \u2192 ${RETRIEVE_TOOL_NAME}("${ref}") restores full text; safe to summarize \u2014 the original stays retrievable by this ref`; } /** ID-reference view: replace oversized tool-result content with a deterministic diff --git a/tests/store.test.ts b/tests/store.test.ts index 67cefbbf10..6230ef314c 100644 --- a/tests/store.test.ts +++ b/tests/store.test.ts @@ -3,7 +3,7 @@ import assert from "node:assert/strict"; import { existsSync, mkdtempSync, readdirSync, readFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; -import { createCore, createInitialState, defaultConfig, type CoreMessage } from "acp-kernel"; +import { applyAbsorb, createCore, createInitialState, defaultConfig, type CoreMessage } from "acp-kernel"; import { applyStoreView, effectiveStoreConfig, @@ -110,3 +110,22 @@ test("sidecar is written to disk under storeDir and the index lands in session.m assert.equal(idx[ref].hash, e.hash); assert.ok(!JSON.stringify(idx).includes(BIG_TEXT.slice(0, 40)), "index holds metadata only, never payload bytes"); }); + +test("absorbing a stored item keeps its original retrievable", () => { + const { session, ref } = makeSession(true); + const substituted = applyStoreView(toolResult(), session); + assert.match(substituted.find((m) => m.id === "t-res")!.text!, new RegExp(`stored #${ref}`)); + const outcome = applyAbsorb({ + ref, + summary: "build succeeded in 3s", + absorbCallId: undefined, + messages: toolResult(), + state: session.state, + config: { ...defaultConfig(200000), absorb: { enabled: true } }, + }); + session.state = outcome.state; + assert.equal(outcome.ok, true, `absorb should succeed: ${outcome.resultText}`); + const got = executeRetrieve({ ref }, session); + assert.ok(got.includes(BIG_TEXT.slice(0, 80)), "retrieve returns the full original"); + assert.ok(!got.includes("build succeeded in 3s"), "retrieve returns the original, not the digest"); +}); From 41a9c678bb53d36113003808f51d02ea66cbff83 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 23 Sep 2026 19:11:48 +0800 Subject: [PATCH 4/5] feat: switch CCR to kernel-native content store + acp_retrieve (#1097) --- src/absorb.ts | 6 +- src/acp-status.ts | 13 +- src/compress-loop-responses.ts | 11 +- src/compress-settings.ts | 24 +++- src/compress-tool.ts | 28 ++-- src/config.ts | 78 +++++----- src/loop/core.ts | 12 +- src/paths.ts | 9 -- src/persist.ts | 91 ++++++++++-- src/plugin.ts | 5 +- src/preflight.ts | 5 +- src/server.ts | 87 +++++++----- src/session.ts | 23 ++- src/store.ts | 242 ++++++++++---------------------- src/stream-google.ts | 6 + src/stream.ts | 12 +- tests/store-marker-gate.test.ts | 44 +++--- tests/store.test.ts | 242 ++++++++++++++++++++------------ 18 files changed, 542 insertions(+), 396 deletions(-) diff --git a/src/absorb.ts b/src/absorb.ts index 4df28d796d..b1962c072f 100644 --- a/src/absorb.ts +++ b/src/absorb.ts @@ -11,9 +11,9 @@ import { type Config, type CoreMessage, } from "acp-kernel"; -import { PROXY_TOOL_NAMES, RETRIEVE_TOOL_NAME, RULE_TOOL_NAME } from "./compress-tool.js"; +import { PROXY_TOOL_NAMES, RULE_TOOL_NAME } from "./compress-tool.js"; import { effectiveRulesConfig } from "./rules-feature.js"; -import { effectiveStoreConfig } from "./store.js"; +import { ccrEnabled, retrieveToolName } from "./store.js"; import { log as loggerLog } from "./logger.js"; import type { Session } from "./session.js"; @@ -49,7 +49,7 @@ export function isProxyToolFor(name: string, session: Session | undefined, confi if (PROXY_TOOL_NAMES.has(name)) return true; const absorb = effectiveAbsorbConfig(session, config); if (absorb?.enabled === true && name === (absorb.toolName ?? ABSORB_TOOL_NAME)) return true; - if (effectiveStoreConfig(session)?.enabled === true && name === RETRIEVE_TOOL_NAME) return true; + if (ccrEnabled(session) && name === retrieveToolName(session)) return true; return effectiveRulesConfig(session, config)?.enabled === true && name === RULE_TOOL_NAME; } diff --git a/src/acp-status.ts b/src/acp-status.ts index 611bd460d1..7c4b11f20e 100644 --- a/src/acp-status.ts +++ b/src/acp-status.ts @@ -8,7 +8,7 @@ import { type CoreMessage, } from "acp-kernel"; import { getBlindTunnelStats } from "./mitm.js"; -import { effectiveStoreConfig } from "./store.js"; +import { ccrEnabled, contentStoreOf } from "./store.js"; import { preCompactionArchiveOf, type Session } from "./session.js"; import { VERSION } from "./version.js"; @@ -54,9 +54,10 @@ export function handleAcpStatus(args: Record, ctx: AcpStatusCtx const turn = ctx.core.processTurn({ messages: ctx.messages, state: ctx.session.state, - config: ctx.config, + config: ccrEnabled(ctx.session) ? ctx.config : { ...ctx.config, ccr: undefined }, tokenCount: ctx.session.stats.lastInputTokens, renderTags: "none", + contentStore: contentStoreOf(ctx.session), }); const nudge = turn.nudge; if (nudge) { @@ -76,9 +77,11 @@ export function handleAcpStatus(args: Record, ctx: AcpStatusCtx } catch { // Base-only report; never fall back to a stale snapshot. } - const storeIdx = ctx.session.metadata.storeIndex as Record | undefined; - const storeCount = storeIdx ? Object.keys(storeIdx).length : 0; - if (effectiveStoreConfig(ctx.session)?.enabled === true && (storeCount > 0 || (ctx.session.stats.retrieveCalls ?? 0) > 0)) { + // #1097: the processTurn above already resolved the envelope when armed + // (contentStoreOf is idempotent); when disarmed skip the disk read. + const ccrArmed = ccrEnabled(ctx.session); + const storeCount = ccrArmed ? Object.keys(contentStoreOf(ctx.session).byRef).length : 0; + if (ccrArmed && (storeCount > 0 || (ctx.session.stats.retrieveCalls ?? 0) > 0)) { const st = ctx.session.stats; const calls = st.retrieveCalls ?? 0; const hits = st.retrieveHits ?? 0; diff --git a/src/compress-loop-responses.ts b/src/compress-loop-responses.ts index e35639c3e0..e71a615585 100644 --- a/src/compress-loop-responses.ts +++ b/src/compress-loop-responses.ts @@ -6,11 +6,11 @@ import { import { handleAcpStatus } from "./acp-status.js"; import { handleAcpCache } from "./cache-ledger.js"; import { lastCompressSuffix, withSessionLock, type Session } from "./session.js"; -import { parseCompressInput, PROXY_TOOL_NAMES, MUTATING_PROXY_TOOLS, COMPRESS_TOOL_NAME, ACP_TEXT_OPEN, ACP_TEXT_CLOSE, RETRIEVE_TOOL_NAME } from "./compress-tool.js"; +import { parseCompressInput, PROXY_TOOL_NAMES, MUTATING_PROXY_TOOLS, COMPRESS_TOOL_NAME, ACP_TEXT_OPEN, ACP_TEXT_CLOSE } from "./compress-tool.js"; import { log as loggerLog } from "./logger.js"; import { applyRanges } from "./stream.js"; import { executeSearchContextTarget, resolveDecompress } from "./decompress-shared.js"; -import { effectiveStoreConfig, executeRetrieve } from "./store.js"; +import { ccrEnabled, drainPendingRetrievals, executeRetrieve, retrieveToolName } from "./store.js"; import { buildVisibilityMarker } from "./compress-loop.js"; import { hoistTrappedToolItems, type ToolPairItem } from "./tool-pair-order.js"; import { MAX_LOOP_ROUNDS } from "./loop/index.js"; @@ -105,7 +105,7 @@ function executeProxyTool( if (toolName === "acp_cache") { return handleAcpCache(ctx.session, args); } - if (effectiveStoreConfig(ctx.session)?.enabled === true && toolName === RETRIEVE_TOOL_NAME) { + if (ccrEnabled(ctx.session) && toolName === retrieveToolName(ctx.session)) { return executeRetrieve(args, ctx.session); } return `[Unknown proxy tool: ${toolName}]`; @@ -225,6 +225,11 @@ export async function compressLoopResponsesJson( ctx.log(`[acp-proxy: responses JSON ${call.name} → ${result.slice(0, 120).replace(/\n/g, " ")}]`); if (ctx.visibilityMarkers !== false) inputItems.push({ type: "message", role: "developer", content: buildVisibilityMarker(call.name, result) }); } + // #1097: retrieval injections ride the re-request after their ack — + // coreToResponses re-voices system as developer, so mirror that here. + for (const injection of drainPendingRetrievals(ctx.session)) { + inputItems.push({ type: "message", role: "developer", content: [{ type: "output_text", text: injection.text }] }); + } requestBody.input = hoistTrappedToolItems(inputItems as ToolPairItem[]); const result = await fetchWithRetry(requestOptions.url, { method: "POST", diff --git a/src/compress-settings.ts b/src/compress-settings.ts index 398037ea93..2a92da7040 100644 --- a/src/compress-settings.ts +++ b/src/compress-settings.ts @@ -1,4 +1,4 @@ -import { DEFAULT_ABSORB_CONFIG, defaultPrompts, resolvePrompts, createPackResolver, defaultPackSources, isValidPackName, type AbsorbConfig, type Config, type PackSurface, type Prompts } from "acp-kernel"; +import { DEFAULT_ABSORB_CONFIG, DEFAULT_CCR_CONFIG, defaultPrompts, resolvePrompts, createPackResolver, defaultPackSources, isValidPackName, type AbsorbConfig, type Config, type CcrConfig, type PackSurface, type Prompts } from "acp-kernel"; import * as path from "node:path"; import { findRoute, type CompressSettings, type ProviderRoutes } from "./config.js"; import { configDir } from "./paths.js"; @@ -59,7 +59,7 @@ export function mergeCompress( // like `prompts`: a model-level minToolTokens must not discard a // provider-level excludeTools. const absorbLevels = [global?.absorb, provider?.absorb, model?.absorb].filter(Boolean) as NonNullable[]; - const storeLevels = [global?.store, provider?.store, model?.store].filter(Boolean) as NonNullable[]; + const ccrLevels = [global?.ccr, provider?.ccr, model?.ccr].filter(Boolean) as NonNullable[]; const reasoningLevels = [global?.reasoning, provider?.reasoning, model?.reasoning].filter(Boolean) as NonNullable[]; const reasoningGuardLevels = [global?.reasoningGuard, provider?.reasoningGuard, model?.reasoningGuard].filter(Boolean) as NonNullable[]; return { @@ -77,7 +77,7 @@ export function mergeCompress( prompts: promptLevels.length > 0 ? Object.assign({}, ...promptLevels) : undefined, acknowledgePromptsRisk: pick("acknowledgePromptsRisk"), absorb: absorbLevels.length > 0 ? Object.assign({}, ...absorbLevels) : undefined, - store: storeLevels.length > 0 ? Object.assign({}, ...storeLevels) : undefined, + ccr: ccrLevels.length > 0 ? Object.assign({}, ...ccrLevels) : undefined, rules: pick("rules"), stripImages: pick("stripImages"), @@ -212,7 +212,11 @@ export function hasCompressSettings(s: CompressSettings): boolean { * fully). Absent `s.absorb` leaves `base.absorb` untouched — the feature * stays off unless some level enables it. * - `rules` → `rules = { enabled }` (kernel RuleFeatureConfig; limits stay - * at kernel defaults). Absent `s.rules` leaves `base.rules` untouched. */ + * at kernel defaults). Absent `s.rules` leaves `base.rules` untouched. + * - `ccr` → `ccr` (kernel CcrConfig, acp-kernel >= 0.0.84; the kernel runs + * the ccr-store node inside processTurn between prune and absorb). + * Unset fields inherit DEFAULT_CCR_CONFIG. Absent `s.ccr` leaves + * `base.ccr` untouched — the feature stays off unless some level enables it. */ export function applyCompressSettings(base: Config, limit: number, s: CompressSettings): Config { const nudge = { ...base.nudge }; const truncate = { ...base.truncate }; @@ -239,6 +243,17 @@ export function applyCompressSettings(base: Config, limit: number, s: CompressSe excludeTools: s.absorb.excludeTools ?? [...d.excludeTools], }; } + let ccr: CcrConfig | undefined; + if (s.ccr !== undefined) { + const d = DEFAULT_CCR_CONFIG; + ccr = { + enabled: s.ccr.enabled === true, + toolName: s.ccr.toolName ?? d.toolName, + minToolTokens: s.ccr.minToolTokens ?? d.minToolTokens, + excludeTools: s.ccr.excludeTools ?? [...d.excludeTools], + maxHeadChars: s.ccr.maxHeadChars ?? d.maxHeadChars, + }; + } return { ...base, modelContextLimit: limit, @@ -254,6 +269,7 @@ export function applyCompressSettings(base: Config, limit: number, s: CompressSe protectedLatestTools: s.protectedLatestTools ?? base.protectedLatestTools, protectedTools: s.protectedTools ?? base.protectedTools, ...(absorb !== undefined ? { absorb } : {}), + ...(ccr !== undefined ? { ccr } : {}), ...(s.rules !== undefined ? { rules: { enabled: s.rules === true } } : {}), }; } diff --git a/src/compress-tool.ts b/src/compress-tool.ts index f275b7241d..ea54740f93 100644 --- a/src/compress-tool.ts +++ b/src/compress-tool.ts @@ -13,6 +13,7 @@ import { parseCompressArgs, ABSORB_TOOL_OPENAI, + RETRIEVE_TOOL_NAME, RULE_TOOL_NAME, SEARCH_CONTEXT_TOOL, SEARCH_CONTEXT_TOOL_GOOGLE, @@ -150,24 +151,31 @@ export const RULE_TOOL_RESPONSES = { type: "function" as const, name: RULE_TOOL_ export const RULE_TOOL_GOOGLE = { name: RULE_TOOL_NAME, description: RULE_TOOL_DESCRIPTION, parameters: RULE_PARAM_SCHEMA }; // #1097: acp_retrieve — resolve a stored ID-referenced message back to its full -// original. Host-registered opt-in (no kernel const); synthesized in all four +// original. Kernel-owned tool (RETRIEVE_TOOL_NAME); synthesized in all four // wire shapes here so every injection point (wire helpers, plugin manifest) // serves one definition. Takes a single `ref` (the mNNNNN id printed in the -// [stored] placeholder). Read-only with respect to context: the fetched content +// [acp-stored] placeholder). Read-only with respect to context: the fetched content // rides the ephemeral tool-result channel and consumes no message ref. -const RETRIEVE_TOOL_DESCRIPTION = "Retrieve the full original text of a stored message by its id. Large tool results are replaced on the wire with a placeholder shaped like \"📦 [stored #m00423 · bash · 4213 tok] \\\"npm run build\\\" → acp_retrieve(\\\"m00423\\\")\". Call this with that ref to read the complete original back into context. Leaving the placeholder costs nothing; retrieving costs one call — fetch only when the detail matters to the current step."; +const RETRIEVE_TOOL_DESCRIPTION = "Retrieve the full original text of a stored message by its id. Large tool results are replaced on the wire with a placeholder shaped like \"📦 [acp-stored #m00423 · shell output · 4,213 tok] `npm run build`\n → acp_retrieve(\"m00423\") returns the full text\". Call this with that ref to read the complete original back into context. Leaving the placeholder costs nothing; retrieving costs one call — fetch only when the detail matters to the current step."; const RETRIEVE_PARAM_SCHEMA = { - type: "object", + type: "object" as const, properties: { - ref: { type: "string", description: "The stored message id to retrieve (an mNNNNN ref from a [stored] placeholder)." }, + ref: { type: "string", description: "The stored message id to retrieve (an mNNNNN ref from an [acp-stored] placeholder)." }, }, required: ["ref"], }; -export const RETRIEVE_TOOL_NAME = "acp_retrieve"; -export const RETRIEVE_TOOL = { name: RETRIEVE_TOOL_NAME, description: RETRIEVE_TOOL_DESCRIPTION, input_schema: RETRIEVE_PARAM_SCHEMA }; -export const RETRIEVE_TOOL_OPENAI = { type: "function" as const, function: { name: RETRIEVE_TOOL_NAME, description: RETRIEVE_TOOL_DESCRIPTION, parameters: RETRIEVE_PARAM_SCHEMA } }; -export const RETRIEVE_TOOL_RESPONSES = { type: "function" as const, name: RETRIEVE_TOOL_NAME, description: RETRIEVE_TOOL_DESCRIPTION, parameters: RETRIEVE_PARAM_SCHEMA }; -export const RETRIEVE_TOOL_GOOGLE = { name: RETRIEVE_TOOL_NAME, description: RETRIEVE_TOOL_DESCRIPTION, parameters: RETRIEVE_PARAM_SCHEMA }; +export { RETRIEVE_TOOL_NAME }; +/** Wire tool shapes for the retrieve tool; name follows the session's resolved + * `ccr.toolName` (default acp_retrieve) so registration, dispatch, and the + * kernel placeholder hint all agree. */ +export function retrieveToolsFor(name: string) { + return { + anthropic: { name, description: RETRIEVE_TOOL_DESCRIPTION, input_schema: RETRIEVE_PARAM_SCHEMA }, + openai: { type: "function" as const, function: { name, description: RETRIEVE_TOOL_DESCRIPTION, parameters: RETRIEVE_PARAM_SCHEMA } }, + responses: { type: "function" as const, name, description: RETRIEVE_TOOL_DESCRIPTION, parameters: RETRIEVE_PARAM_SCHEMA }, + google: { name, description: RETRIEVE_TOOL_DESCRIPTION, parameters: RETRIEVE_PARAM_SCHEMA }, + }; +} export function parseCompressInput(input: unknown, callId?: string) { const parsed = parseCompressArgs(input, { callId }); diff --git a/src/config.ts b/src/config.ts index 3a510e8ea8..02e8e0c8b0 100644 --- a/src/config.ts +++ b/src/config.ts @@ -224,24 +224,31 @@ export type CompressSettings = { * the client's own tool names or the agent will call its own tool. */ toolName?: string; }; - /** [#1097] Content-addressed message store (host-only; no kernel Config - * equivalent). When `enabled`, oversized tool results (>= minTokens) are - * ID-referenced at arrival: the wire keeps a deterministic placeholder and - * the original goes into the per-session content store (persisted alongside - * blockContents), retrievable via the injected `acp_retrieve` tool. Lossless - * by default — a retrieve not made costs one cheap tool call, whereas a - * distilled-away detail is gone for good. Takes precedence over absorb / - * pre-crush for the same result. Off unless explicitly enabled at some level - * (default off for v1). Merged sub-field-wise across the three levels like - * `absorb`. */ - store?: { - /** Enable ID-reference + acp_retrieve for this scope. Absent/false = off. */ + /** [#1097] Kernel CCR content store (kernel `Config.ccr`, acp-kernel + * 0.0.84). When `enabled`, oversized tool results (>= minToolTokens) are + * ID-referenced at arrival by the kernel's ccr-store node (prune → + * ccr-store → absorb): the wire keeps a deterministic `[acp-stored` … + * placeholder and the original goes into the session's kernel + * `MessageContentStore`, retrievable via the injected retrieve tool. + * Lossless by default — a retrieve not made costs one cheap tool call, + * whereas a distilled-away detail is gone for good. ID-reference wins + * over absorb (kernel ordering). Off unless explicitly enabled at some + * level. Merged sub-field-wise across the three levels like `absorb`. */ + ccr?: { + /** Enable CCR for this scope. Absent/false = off (kernel semantics). */ enabled?: boolean; - /** Tool results smaller than this many tokens stay verbatim (default 500). */ - minTokens?: number; - /** Per-session cap on unique stored bytes (default 2 MiB). New content - * beyond the cap is left verbatim rather than substituted. */ - maxStoreBytes?: number; + /** Tool results smaller than this many tokens stay verbatim + * (kernel default 4000). */ + minToolTokens?: number; + /** Tool-name patterns (glob suffix allowed) never CCR-stored + * (kernel default: none). */ + excludeTools?: string[]; + /** Rename the retrieve tool (default "acp_retrieve"). Must stay unique + * against the client's own tool names. */ + toolName?: string; + /** Max characters for the placeholder head/command preview (kernel + * default 96). */ + maxHeadChars?: number; }; /** Persistent rule reminders (kernel `Config.rules`, acp-kernel >= 0.0.70). * When `enabled`, an `acp_rule` tool is injected (or advertised in the @@ -1029,24 +1036,31 @@ export function parseCompressSettings(v: unknown): (CompressSettings & { injectT if (ok) out.absorb = cleaned; } } - if ("store" in obj && obj.store !== undefined) { - const st = obj.store; - if (!st || typeof st !== "object" || Array.isArray(st)) { + if ("ccr" in obj && obj.ccr !== undefined) { + const c = obj.ccr; + if (!c || typeof c !== "object" || Array.isArray(c)) { ok = false; } else { - const sto = st as Record; - const cleaned: NonNullable = {}; - if ("enabled" in sto) { - if (typeof sto.enabled !== "boolean") { ok = false; } - else cleaned.enabled = sto.enabled; - } - for (const key of ["minTokens", "maxStoreBytes"] as const) { - if (!(key in sto)) continue; - const v = sto[key]; - if (typeof v !== "number" || !Number.isFinite(v)) { ok = false; continue; } - cleaned[key] = v; + const co = c as Record; + const cleaned: NonNullable = {}; + for (const key of ["enabled", "minToolTokens", "excludeTools", "toolName", "maxHeadChars"] as const) { + if (!(key in co)) continue; + const v = co[key]; + if (key === "enabled") { + if (typeof v !== "boolean") { ok = false; continue; } + cleaned.enabled = v; + } else if (key === "minToolTokens" || key === "maxHeadChars") { + if (typeof v !== "number" || !Number.isFinite(v)) { ok = false; continue; } + cleaned[key] = v; + } else if (key === "excludeTools") { + if (!Array.isArray(v) || v.some((x) => typeof x !== "string")) { ok = false; continue; } + cleaned.excludeTools = [...v] as string[]; + } else { + if (typeof v !== "string" || v.trim().length === 0) { ok = false; continue; } + cleaned.toolName = v.trim(); + } } - if (ok) out.store = cleaned; + if (ok) out.ccr = cleaned; } } if ("prompts" in obj && obj.prompts !== undefined) { diff --git a/src/loop/core.ts b/src/loop/core.ts index 5c8b08f63e..8fa33dc9e7 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -12,11 +12,10 @@ import { parseCompressInput, ABSORB_TOOL_NAME, RULE_TOOL_NAME, - RETRIEVE_TOOL_NAME, } from "../compress-tool.js"; import { effectiveAbsorbConfig, executeAbsorb, isProxyToolFor } from "../absorb.js"; import { effectiveRulesConfig, executeRule } from "../rules-feature.js"; -import { effectiveStoreConfig, executeRetrieve } from "../store.js"; +import { ccrEnabled, drainPendingRetrievals, executeRetrieve, retrieveToolName } from "../store.js"; import { applyRanges } from "../stream.js"; import { executeSearchContextTarget, resolveDecompress } from "../decompress-shared.js"; import { buildVisibilityMarker } from "../compress-loop.js"; @@ -187,7 +186,7 @@ export function executeProxyTool( if (effectiveRulesConfig(ctx.session, ctx.config)?.enabled === true && toolName === RULE_TOOL_NAME) { return executeRule(args, ctx); } - if (effectiveStoreConfig(ctx.session)?.enabled === true && toolName === RETRIEVE_TOOL_NAME) { + if (ccrEnabled(ctx.session) && toolName === retrieveToolName(ctx.session)) { return executeRetrieve(args, ctx.session); } return `[Unknown proxy tool: ${toolName}]`; @@ -694,6 +693,13 @@ export async function* runCompressLoop( }); } } + // #1097: retrieval injections ride the same re-request channel + // as the ack pairs above — ack first, full text second. Their + // ids (acp_retrieved_*) are structurally excluded from ref + // assignment, so they never consume message numbers. + for (const injection of drainPendingRetrievals(ctx.session)) { + coreMessages.push(injection); + } const anyCompressFailed = proxyResults.some( (pr) => (pr.name === "compress" || pr.name === "decompress") && pr.result.includes("FAILED"), ); diff --git a/src/paths.ts b/src/paths.ts index 75b2423a12..bd39faa006 100644 --- a/src/paths.ts +++ b/src/paths.ts @@ -47,15 +47,6 @@ export function sessionsDir(): string { return path.join(dataDir(), "sessions"); } -/** Content-store dir (#1097): hash-keyed sidecar originals, kept OUTSIDE the - * per-session JSON so whole-file encryption and .json discovery never touch - * payloads. Overridable via BILI_STORE_DIR (test isolation / custom layout). */ -export function storeDir(): string { - const env = process.env.BILI_STORE_DIR; - if (env && env.length > 0) return path.resolve(env); - return path.join(dataDir(), "store"); -} - /** Root cache dir: transient/ephemeral data (update-check throttle, etc.). */ export function cacheDir(): string { return path.join(xdg("XDG_CACHE_HOME", ".cache"), "billion-context"); diff --git a/src/persist.ts b/src/persist.ts index 3e34c290d7..a136e164fb 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -1,5 +1,5 @@ import { createHash } from "node:crypto"; -import { existsSync, mkdirSync, renameSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"; import { readdir, readFile, rm } from "node:fs/promises"; import * as path from "node:path"; import { StateStore, flatFileNameFor, type PersistedEnvelope, type StateStoreCodec } from "acp-kernel/persist"; @@ -7,7 +7,7 @@ import { sessionsDir } from "./paths.js"; import { log as loggerLog } from "./logger.js"; import { createStorageCodec, parseEncryptionKey } from "./encrypt.js"; import { PersistEpermAlert } from "./persist-eperm.js"; -import { createInitialState, defaultCountTokens, prune, type CompressionState, type CoreMessage } from "acp-kernel"; +import { createInitialState, defaultCountTokens, prune, type CompressionState, type CoreMessage, type MessageContentStore } from "acp-kernel"; import type { Session, BlockContent, BlockView } from "./session.js"; import type { WireProtocol } from "./util.js"; @@ -197,6 +197,16 @@ function relPathFor(id: string, protocol?: string, upstreamOrigin?: string): str return path.join(proto, `${host}${createHash("sha256").update(id, "utf8").digest("hex").slice(0, 24)}.json`); } +/** #1097: the kernel CCR content-store envelope lives in ONE dedicated file + * per session, next to the session JSON (same namespace, same codec): the + * originals are payload bytes, not state — keeping them out of the session + * record preserves whole-file encryption, .json discovery, and the bounded + * in-memory session cache (the envelope is lazily loaded per session). */ +function contentStoreRelPathFor(id: string, protocol?: string, upstreamOrigin?: string): string { + const base = relPathFor(id, protocol, upstreamOrigin); + return base.slice(0, -".json".length) + ".content-store.json"; +} + /** Session persistence policy over the kernel StateStore mechanism. The * public API predates the extraction and is kept stable for session.ts / * server.ts / export.ts. */ @@ -252,11 +262,62 @@ export class SessionStore { }); } - /** #1097: the active at-rest codec (undefined when BILI_ENCRYPTION_KEY is - * unset), shared with the content-store sidecar files so they encrypt - * identically to the session JSON. */ - payloadCodec(): StateStoreCodec | undefined { - return this.codec; + /** #1097: load the session's kernel content-store envelope (namespaced + * path first, then the _unknown/ fallback — same probe order as + * loadEnvelope). Returns null when absent or unreadable; callers degrade + * to a fresh store (retrieve misses), never crash the request. */ + loadContentStore(session: Session): MessageContentStore | null { + if (!this.enabled) return null; + const rels = [ + contentStoreRelPathFor(session.id, session.meta.protocol, session.meta.upstreamOrigin), + contentStoreRelPathFor(session.id), + ]; + for (const rel of rels) { + let buf: Buffer; + try { + buf = readFileSync(path.join(this.dir, rel)); + } catch { + continue; + } + try { + const text = this.codec ? this.codec.decode(buf) : buf.toString("utf8"); + const parsed = JSON.parse(text) as MessageContentStore; + if (parsed && typeof parsed === "object" && parsed.version === 1 + && parsed.byHash && typeof parsed.byHash === "object" + && parsed.byRef && typeof parsed.byRef === "object") { + return parsed; + } + this.log("warn", `[persist] content-store for ${session.id} malformed, starting fresh`); + return null; + } catch (err) { + this.log("warn", `[persist] content-store for ${session.id} unreadable, starting fresh: ${err instanceof Error ? err.message : String(err)}`); + return null; + } + } + return null; + } + + /** #1097: persist the content-store envelope when dirty; an emptied store + * (rebase reset) deletes the file. Payload-first: runs BEFORE the session + * state write so a crash mid-save leaves at worst a retrieve miss, never + * a placeholder whose original is gone. */ + private saveContentStore(session: Session): void { + if (!this.enabled || !session.contentStoreDirty) return; + session.contentStoreDirty = false; + const rel = contentStoreRelPathFor(session.id, session.meta.protocol, session.meta.upstreamOrigin); + const abs = path.join(this.dir, rel); + if (!session.contentStore || Object.keys(session.contentStore.byRef).length === 0) { + rmSync(abs, { force: true }); + return; + } + try { + mkdirSync(path.dirname(abs), { recursive: true }); + const text = JSON.stringify(session.contentStore); + writeFileSync(abs, this.codec ? this.codec.encode(text) : text); + } catch (err) { + session.contentStoreDirty = true; + this.log("warn", `[persist] content-store write failed for ${session.id}: ${err instanceof Error ? err.message : String(err)}`); + } } /** Bulk-load every persisted session from disk into a map keyed by the @@ -427,6 +488,12 @@ export class SessionStore { for (const rel of candidates) { await rm(path.join(this.dir, rel), { force: true }).catch(() => {}); } + for (const rel of [ + contentStoreRelPathFor(id, session.meta.protocol, session.meta.upstreamOrigin), + contentStoreRelPathFor(id), + ]) { + await rm(path.join(this.dir, rel), { force: true }).catch(() => {}); + } } /** Shared envelope probe for the sync read paths: namespaced path @@ -540,6 +607,7 @@ export class SessionStore { * session state is persisted. Safe to call on the hot path. No-op if * disabled. */ scheduleSave(session: Session): void { + this.saveContentStore(session); this.store.scheduleSave(session.id, this.guardedBuild(session)); } @@ -547,6 +615,7 @@ export class SessionStore { * on write failure so callers can react (e.g. avoid evicting). Serialized * per-session by the kernel store's write chains. */ async writeNow(session: Session): Promise { + this.saveContentStore(session); await this.store.writeNow(session.id, this.guardedBuild(session)); } @@ -556,6 +625,7 @@ export class SessionStore { * Returns true on success, false on failure (caller must NOT evict on * failure for a never-persisted session or it is lost permanently). */ flushSync(session: Session): boolean { + this.saveContentStore(session); return this.store.flushSync(session.id, this.guardedBuild(session)); } @@ -666,6 +736,7 @@ function buildSession(parsed: PersistedSession): Session { lastMessagesFolded: parsed.messagesFolded === true, inFlight: 0, persisted: true, + pendingRetrievals: [], }; } @@ -810,12 +881,6 @@ export function getStore(): SessionStore { return _store; } -/** #1097: active at-rest codec for content-store sidecar files (plaintext when - * BILI_ENCRYPTION_KEY is unset) — mirrors the session-JSON encryption. */ -export function storePayloadCodec(): StateStoreCodec | undefined { - return getStore().payloadCodec(); -} - /** Test hook: inject a store with a temp dir. */ export function _setStoreForTest(store: SessionStore): void { _store = store; diff --git a/src/plugin.ts b/src/plugin.ts index 5127bfe866..ee2b6de95d 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -12,6 +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 } from "./store.js"; import { emitStreamError, emitUpstreamTruncation } from "./stream-error.js"; import { degenerateTurnWarning } from "./degenerate-turn.js"; import { warnCacheCollapse } from "./cache-warn.js"; @@ -653,12 +654,14 @@ export function handlePluginStatus(conversationId: string, res: import("node:htt // #833: base kernelConfig carries no file/provider/model compress // settings — render from the session's last resolved Config so the // panel matches actual injection behavior. + const pluginCfg = effectiveConfig(session, deps.config); nudge = deps.core.processTurn({ messages, state: session.state, - config: effectiveConfig(session, deps.config), + config: ccrEnabled(session) ? pluginCfg : { ...pluginCfg, ccr: undefined }, tokenCount: session.stats.lastInputTokens, renderTags: "none", + contentStore: contentStoreOf(session), }).nudge; } } catch { diff --git a/src/preflight.ts b/src/preflight.ts index df7b50f5bf..66314c1f14 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -10,6 +10,7 @@ import { import { buildCompressSystemPrompt, parseCompressInput } from "./compress-tool.js"; import { IMAGE_PLACEHOLDER, imagePlaceholders } from "./image-note.js"; import { applyAbsorbView } from "./absorb.js"; +import { adoptContentStore, ccrEnabled, contentStoreOf } from "./store.js"; import { applyRanges, normalizeRangeOrder, type RewriteCtx } from "./stream.js"; import { fetchWithTimeout, isTransientUpstreamError, replayMaxAttempts, replayBackoffMs, sleep, UpstreamHttpError } from "./fetch-util.js"; import { proxyDispatcher } from "./upstream-proxy.js"; @@ -847,11 +848,13 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa const turn = deps.core.processTurn({ messages, state: deps.session.state, - config: noEmergencyTruncate(activeConfig), + config: noEmergencyTruncate(ccrEnabled(deps.session) ? activeConfig : { ...activeConfig, ccr: undefined }), tokenCount: currentTokens, renderTags: "text-only", + contentStore: contentStoreOf(deps.session), }); deps.session.state = turn.state; + adoptContentStore(deps.session, turn.contentStore); // Absorbed pairs are hidden on the wire, so the fit check must see the // same reduced payload prepare* will actually forward. turn.messages = applyAbsorbView(turn.messages, turn.state, activeConfig, currentTokens); diff --git a/src/server.ts b/src/server.ts index 6902d4a9be..29cbfb45dc 100644 --- a/src/server.ts +++ b/src/server.ts @@ -60,9 +60,9 @@ import { type GoogleSystemInstruction, type GoogleTool, } 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, RULE_TOOL, RULE_TOOL_GOOGLE, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, RETRIEVE_TOOL, RETRIEVE_TOOL_GOOGLE, RETRIEVE_TOOL_OPENAI, RETRIEVE_TOOL_RESPONSES, buildAbsorbSystemPrompt, buildCompressSystemPrompt, buildCompressHybridSystemPrompt, withConversationIdNote, withMarkerIntegrityNote, withStagedCompressGuidance, withSummaryBudgetNote } from "./compress-tool.js"; +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, 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 { applyStoreView, effectiveStoreConfig, storeEffectiveStore, type StoreConfig } from "./store.js"; +import { adoptContentStore, ccrEnabled, contentStoreOf, executeRetrieve, retrieveToolName, storeEffectiveCcr, type CcrSettings } from "./store.js"; import { rulesEnabled, storeEffectiveRules } from "./rules-feature.js"; import { rewriteJsonResponse, type RewriteCtx } from "./stream.js"; import { applyRanges } from "./stream.js"; @@ -1157,10 +1157,10 @@ async function handle( let reqSurface: PackSurface = {}; let reqSurfacePack = "default"; let wsSourceForLog: string | undefined; - // [#1097] host-only content-store policy for this request scope (three-level + // [#1097] host-only CCR policy for this request scope (three-level // merge); resolved before the session is bound, then stamped onto it below so // every view / injection / execution site reads one value. - let resolvedStoreCfg: StoreConfig | undefined; + let resolvedCcrCfg: CcrSettings | undefined; let reqModelId: string | undefined; if (parsed && typeof parsed === "object") { // Gemini's model lives in the request path, every other wire carries it @@ -1254,7 +1254,7 @@ async function handle( windowShrinkReason = "operator"; } const compressCfg = resolveCompress(opts.routes, embeddedUrl, model, opts.compress); - resolvedStoreCfg = compressCfg.store; + resolvedCcrCfg = compressCfg.ccr; reqPrompts = resolveCompressPrompts(compressCfg); const surfaceRes = resolveCompressSurfaceDetailed(compressCfg); reqSurface = surfaceRes.surface; @@ -1641,8 +1641,11 @@ async function handle( // exactly one system at index 0, #377) accept it and the head system // message stays byte-stable for the prefix cache. const pluginMode = pluginAgent !== undefined; - // [#1097] Stamp the resolved store policy. acp_retrieve needs a tool - // channel, so the store is only armed in proxy mode with tool injection + // [#1097] Stamp the resolved CCR policy. acp_retrieve needs a tool + // channel, so CCR is only armed in proxy mode with tool injection + // armed (mirrors absorb); every processTurn site strips `ccr` from the + // loop config unless this stamp says armed — the kernel's ccr-store + // node must never substitute placeholders the wire cannot resolve. // on — we must never emit a placeholder the model cannot retrieve (silent // loss). Plugin mode is out of scope for v1: the agent would need // acp_retrieve advertised in the plugin manifest to avoid that same trap. @@ -1652,7 +1655,7 @@ async function handle( // leave placeholders unretrievable. const storeChannelOk = protocol !== "responses" || (!process.env.ACP_NO_INJECT_TOOL && !FORCE_TEXT_PROTOCOL && resolveCompressProtocol(opts.routes, upstreamOrigin) !== "marker"); - storeEffectiveStore(session, opts.compress.injectTool && !pluginMode && storeChannelOk ? resolvedStoreCfg : undefined); + storeEffectiveCcr(session, opts.compress.injectTool && !pluginMode && storeChannelOk && resolvedCcrCfg?.enabled === true ? resolvedCcrCfg : undefined); // #546: restore a client-shrunk output budget BEFORE the side gate so a // tool-carrying main request re-enters the pipeline at full budget (see // restoreOutputBudget for the starvation mechanism). @@ -2356,18 +2359,20 @@ function prepareAnthropic( // ever injected into messages), so unlike absorb it needs no loop- // config stripping — only tool availability matters. const rulesActive = rulesEnabled(config) && opts.compress.injectTool; - const loopConfig = absorbActive ? config : { ...config, absorb: undefined }; - const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" }); + // [#1097] the kernel ccr-store node ID-references oversized tool results + // BEFORE absorb (ID-reference wins over distill); armed policy is + // stamped per-request — strip `ccr` from the loop config when disarmed + // (plugin mode / no tool channel) so placeholders never hit the wire. + const loopConfig = { ...(absorbActive ? config : { ...config, absorb: undefined }), ...(ccrEnabled(session) ? {} : { ccr: undefined }) }; + const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); session.state = turn.state; + adoptContentStore(session, turn.contentStore); // The fold from last turn's compress has now materialized in state — // future usage reports are post-fold reality, drop the credit. session.stats.compressCreditTokens = 0; storeEffectiveAbsorb(session, loopConfig); storeEffectiveRules(session, config); turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); - // [#1097] ID-reference oversized tool results AFTER absorb (absorb wins); - // self-gates off when the store is disabled or in plugin mode. - turn.messages = applyStoreView(turn.messages, session); // Drop sub-viability fragments before any consumer sees them: a tiny // range in the list makes batched compress attempts fail atomically // (kernel validates the whole batch). Mirrors billion-context-pi. @@ -2401,7 +2406,7 @@ function prepareAnthropic( systemOut = injectSystem(parsed, opts, prompts, loopConfig, ensureCanonicalId(session), surface, visibilityMarkers); if (injectTools) { - toolsOut = injectTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL] : []), ...(rulesActive ? [RULE_TOOL] : []), ...(effectiveStoreConfig(session)?.enabled === true ? [RETRIEVE_TOOL] : [])], surface?.toolPrompts); + toolsOut = injectTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL] : []), ...(rulesActive ? [RULE_TOOL] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).anthropic] : [])], surface?.toolPrompts); } // Nudge as a separate trailing user message (cache-friendly): the // system block stays byte-stable so the prefix cache survives. @@ -2530,18 +2535,16 @@ function prepareOpenai( // prefix-cache stability, so strip absorb from the loop config there. const absorbActive = absorbEnabled(config) && shouldInject; const rulesActive = rulesEnabled(config) && shouldInject; - const loopConfig = absorbActive ? config : { ...config, absorb: undefined }; - const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" }); + const loopConfig = { ...(absorbActive ? config : { ...config, absorb: undefined }), ...(ccrEnabled(session) ? {} : { ccr: undefined }) }; + const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); session.state = turn.state; + adoptContentStore(session, turn.contentStore); // The fold from last turn's compress has now materialized in state — // future usage reports are post-fold reality, drop the credit. session.stats.compressCreditTokens = 0; storeEffectiveAbsorb(session, loopConfig); storeEffectiveRules(session, config); turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); - // [#1097] ID-reference oversized tool results AFTER absorb (absorb wins); - // self-gates off when the store is disabled or in plugin mode. - turn.messages = applyStoreView(turn.messages, session); // Drop sub-viability fragments before any consumer sees them: a tiny // range in the list makes batched compress attempts fail atomically // (kernel validates the whole batch). Mirrors billion-context-pi. @@ -2590,7 +2593,7 @@ function prepareOpenai( // avoids double-counting it. openaiOutboundSystem = sysParts.join("\n\n"); if (injectTools) { - toolsOut = injectOpenaiTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL_OPENAI] : []), ...(rulesActive ? [RULE_TOOL_OPENAI] : []), ...(effectiveStoreConfig(session)?.enabled === true ? [RETRIEVE_TOOL_OPENAI] : [])], surface?.toolPrompts); + toolsOut = injectOpenaiTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL_OPENAI] : []), ...(rulesActive ? [RULE_TOOL_OPENAI] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).openai] : [])], surface?.toolPrompts); } // Nudge as a separate trailing user message (cache-friendly). Injected // in BOTH modes (#451): plugin agents supply the ACP tools but have no @@ -2736,17 +2739,15 @@ function prepareGoogle( // ever injected into messages), so unlike absorb it needs no loop- // config stripping — only tool availability matters. const rulesActive = rulesEnabled(config) && shouldInject; - const loopConfig = absorbActive ? config : { ...config, absorb: undefined }; - const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: "text-only" }); + const loopConfig = { ...(absorbActive ? config : { ...config, absorb: undefined }), ...(ccrEnabled(session) ? {} : { ccr: undefined }) }; + const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags: "text-only", contentStore: contentStoreOf(session) }); session.state = turn.state; + adoptContentStore(session, turn.contentStore); // The fold from last turn's compress has materialized in state — future // usage reports are post-fold reality, drop the credit. session.stats.compressCreditTokens = 0; storeEffectiveAbsorb(session, loopConfig); turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); - // [#1097] ID-reference oversized tool results AFTER absorb (absorb wins); - // self-gates off when the store is disabled or in plugin mode. - turn.messages = applyStoreView(turn.messages, session); // Drop sub-viability fragments before any consumer sees them (the // kernel validates a compress batch atomically). if (turn.nudge) turn.nudge.compressibleRanges = viableRanges(turn.nudge.compressibleRanges); @@ -2779,7 +2780,7 @@ function prepareGoogle( const extraSystemParts = sysParts.slice(googleClientSystem ? 1 : 0); systemInstruction = extraSystemParts.length > 0 ? { parts: sysParts.map((text) => ({ text })) } : parsed.systemInstruction; if (injectTools) { - toolsOut = injectGoogleTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL_GOOGLE] : []), ...(rulesActive ? [RULE_TOOL_GOOGLE] : []), ...(effectiveStoreConfig(session)?.enabled === true ? [RETRIEVE_TOOL_GOOGLE] : [])], surface?.toolPrompts); + toolsOut = injectGoogleTool(parsed.tools, [...(absorbActive ? [ABSORB_TOOL_GOOGLE] : []), ...(rulesActive ? [RULE_TOOL_GOOGLE] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).google] : [])], surface?.toolPrompts); } if (sysNotes.length > 0) { rebuiltContents = appendGoogleNudge(rebuiltContents, sysNotes.join("\n\n---\n\n")); @@ -2823,7 +2824,9 @@ export function prepareGoogleCountTokens( const sessionId = session.id; try { const { msgs } = googleToCore(parsed); - const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: session.stats.lastInputTokens, renderTags: "text-only" }); + // Read-only preview: the store rides in so placeholder substitution is + // counted, but nothing is adopted (state is discarded here too). + const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrEnabled(session) ? config : { ...config, ccr: undefined }, tokenCount: session.stats.lastInputTokens, renderTags: "text-only", contentStore: contentStoreOf(session) }); const stripped = stripKernelSummaries(turn.messages, turn.state); const rebuilt: GoogleRequestBody = { ...parsed, contents: coreToGoogle(stripped as BiliMessage[]) }; log("info", `[${sessionId}] countTokens pruned: ${msgs.length} → ${stripped.length} msgs`); @@ -2965,18 +2968,16 @@ function prepareResponses( // so strip absorb from the loop config there (both modes). const absorbActive = absorbEnabled(config) && shouldInject && !isCompactionTrigger && !responsesTextProtocol; const rulesActive = rulesEnabled(config) && shouldInject && !isCompactionTrigger && !responsesTextProtocol; - const loopConfig = absorbActive ? config : { ...config, absorb: undefined }; - const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags }); + const loopConfig = { ...(absorbActive ? config : { ...config, absorb: undefined }), ...(ccrEnabled(session) ? {} : { ccr: undefined }) }; + const turn = core.processTurn({ messages: msgs, state: session.state, config: loopConfig, tokenCount, renderTags, contentStore: contentStoreOf(session) }); session.state = turn.state; + adoptContentStore(session, turn.contentStore); // The fold from last turn's compress has now materialized in state — // future usage reports are post-fold reality, drop the credit. session.stats.compressCreditTokens = 0; storeEffectiveAbsorb(session, loopConfig); storeEffectiveRules(session, config); turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); - // [#1097] ID-reference oversized tool results AFTER absorb (absorb wins); - // self-gates off when the store is disabled or in plugin mode. - turn.messages = applyStoreView(turn.messages, session); // Drop sub-viability fragments before any consumer sees them: a tiny // range in the list makes batched compress attempts fail atomically // (kernel validates the whole batch). Mirrors billion-context-pi. @@ -3011,7 +3012,7 @@ function prepareResponses( responsesDevContent = devContent; rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); if (!process.env.ACP_NO_INJECT_TOOL && injectTools) { - const respExtra = [...(absorbActive ? [ABSORB_TOOL_RESPONSES] : []), ...(rulesActive ? [RULE_TOOL_RESPONSES] : []), ...(effectiveStoreConfig(session)?.enabled === true ? [RETRIEVE_TOOL_RESPONSES] : [])]; + const respExtra = [...(absorbActive ? [ABSORB_TOOL_RESPONSES] : []), ...(rulesActive ? [RULE_TOOL_RESPONSES] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).responses] : [])]; toolsOut = responsesTextProtocol ? injectResponsesTool(parsed.tools, BILI_ACP_READONLY_TOOLS_RESPONSES, surface?.toolPrompts) : injectResponsesTool(parsed.tools, respExtra.length > 0 ? [...BILI_ACP_TOOLS_RESPONSES, ...respExtra] : BILI_ACP_TOOLS_RESPONSES, surface?.toolPrompts); @@ -3188,7 +3189,8 @@ export function prepareCountTokens( const sessionId = session.id; try { const { msgs, cacheControls } = anthropicToCore(parsed); - const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" }); + // Read-only preview: same policy as the google twin above. + const turn = core.processTurn({ messages: msgs, state: session.state, config: ccrEnabled(session) ? config : { ...config, ccr: undefined }, tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); const stripped = stripKernelSummaries(turn.messages as BiliMessage[], turn.state); const rebuiltMessages = coreToAnthropic(stripped, cacheControls); log("info", `[${sessionId}] count_tokens pruned: ${msgs.length} → ${stripped.length} msgs`); @@ -3257,6 +3259,8 @@ function prepareResponsesCompact( // half-applied fold (the upstream's own compaction boundary is handled by // markNativeCompactionBoundary + rebase instead). const prevState = session.state; + const prevStore = session.contentStore; + const prevStoreDirty = session.contentStoreDirty === true; // E2 endpoint form: /responses/compact. When the gate passes, run the same // fold pipeline as a normal turn and forge the compacted history as // {"output": [...]} — a deterministic handoff to the ACP state instead of a @@ -3267,12 +3271,15 @@ function prepareResponsesCompact( // The forged handoff is one-shot with no tool channel: strip absorb so // no [ACP absorb] instruction bakes into the forged history, and run // the absorb view so absorbed pairs stay hidden in it (wire parity). - const compactConfig = { ...config, absorb: undefined }; - const turn = core.processTurn({ messages: projection.msgs, state: session.state, config: compactConfig, tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" }); + const compactConfig = { ...config, absorb: undefined, ...(ccrEnabled(session) ? {} : { ccr: undefined }) }; + const turn = core.processTurn({ messages: projection.msgs, state: session.state, config: compactConfig, tokenCount: session.stats.lastInputTokens, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only", contentStore: contentStoreOf(session) }); session.state = turn.state; + adoptContentStore(session, turn.contentStore); transformOk = true; if (!codexCompactGate(session, config.modelContextLimit, transformOk)) { session.state = prevState; + session.contentStore = prevStore; + session.contentStoreDirty = prevStoreDirty; return base; } const viewed = applyAbsorbView(turn.messages, turn.state, compactConfig, session.stats.lastInputTokens); @@ -3280,6 +3287,8 @@ function prepareResponsesCompact( let output = patchResponsesInput(projection, processed); if (typeof output === "string") { session.state = prevState; + session.contentStore = prevStore; + session.contentStoreDirty = prevStoreDirty; return base; } output = hoistTrappedToolItems(output); @@ -3289,6 +3298,8 @@ function prepareResponsesCompact( return { ...base, codexForge: { kind: "endpoint", body: JSON.stringify({ output }), contentType: "application/json" } }; } catch (err) { session.state = prevState; + session.contentStore = prevStore; + session.contentStoreDirty = prevStoreDirty; log("warn", `[${session.id}] codex compact forge failed (${String(err)}); passing through to upstream`); return base; } @@ -4680,7 +4691,7 @@ async function forward( // tool is callable, keeping loop re-requests byte-consistent with // the first request (prefix-cache anchor). const absorbActive = absorbEnabled(config) && opts.compress.injectTool && !textProtocol; - const loopConfig = absorbActive ? config : { ...config, absorb: undefined }; + const loopConfig = { ...(absorbActive ? config : { ...config, absorb: undefined }), ...(ccrEnabled(prepared.session) ? {} : { ccr: undefined }) }; const absorbSection = absorbActive ? `\n\n---\n\n${buildAbsorbSystemPrompt(absorbToolName(loopConfig))}` : ""; @@ -4699,8 +4710,10 @@ async function forward( config: loopConfig, tokenCount: prepared.session.stats.lastInputTokens, renderTags: prepared.renderTags ?? "text-only", + contentStore: contentStoreOf(prepared.session), }); prepared.session.state = turn.state; + adoptContentStore(prepared.session, turn.contentStore); const viewed = applyAbsorbView(turn.messages, turn.state, loopConfig, prepared.session.stats.lastInputTokens); const records = current.filter((m) => typeof m.id === "string" && m.id.startsWith("acp_loop_")); return repairResponsesAssistantOrdering(stripKernelSummaries([...viewed, ...records] as BiliMessage[], turn.state), prepared.originalMessages); diff --git a/src/session.ts b/src/session.ts index b6a8a545da..2f969ff613 100644 --- a/src/session.ts +++ b/src/session.ts @@ -1,4 +1,4 @@ -import { createInitialState, type CompressionState, type Config, type CoreMessage } from "acp-kernel"; +import { createInitialState, type CompressionState, type Config, type CoreMessage, type MessageContentStore } from "acp-kernel"; import { createHash } from "node:crypto"; import { getStore } from "./persist.js"; import type { WireProtocol } from "./util.js"; @@ -173,6 +173,19 @@ export type Session = { * the state ranges). Cleared by snapshotMessages on the next live * request — the client re-sends full raw history, restoring the invariant. */ lastMessagesFolded?: boolean; + /** Kernel CCR envelope (#1097): originals of ID-referenced tool results, + * owned and mutated only by kernel processTurn (ccr-store node) via + * adoptContentStore. Lazily loaded from the session's content-store.json; + * NOT part of session.state (separate file, separate lifecycle — reset on + * full-state rebase). In-memory only here; buildRecord omits it. */ + contentStore?: MessageContentStore; + /** In-memory only (NOT persisted): content-store.json is rewritten only + * while this is set (adopt grew entries / reset cleared the store). */ + contentStoreDirty?: boolean; + /** In-memory only (NOT persisted): full-text retrieval injections queued by + * executeRetrieve, drained into the next re-request after the tool-result + * pair (request-only, same channel as nudges). */ + pendingRetrievals: CoreMessage[]; /** Number of in-flight requests using this session. A session with * inFlight > 0 must NOT be LRU-evicted: evicting it mid-stream flushes a * half-mutated snapshot and then a miss reloads a SECOND Session object, @@ -301,6 +314,7 @@ export function getSession(id: string, meta?: { protocol?: Session["meta"]["prot blockContents: new Map(), inFlight: 0, persisted: false, + pendingRetrievals: [], }; sessions.set(id, session); return session; @@ -454,6 +468,13 @@ export function cacheBlockContent(session: Session, blockId: string, content: Bl export function resetSessionCompression(session: Session): void { session.state = createInitialState(); session.blockContents.clear(); + // The kernel contract ties the store to the state ('host resets the store + // with the state'): a full rebase restarts refs at m00001, so old entries + // would misattribute under reused numbers. contentStoreDirty + markDirty + // deletes the on-disk envelope on the next save. + session.contentStore = undefined; + session.contentStoreDirty = true; + session.pendingRetrievals.length = 0; session.stats.lastInputTokens = 0; // #857: a zeroed baseline carries no provenance — drop any stale flag. delete session.stats.lastInputTokensSource; diff --git a/src/store.ts b/src/store.ts index 4a7f42238f..620cdf3980 100644 --- a/src/store.ts +++ b/src/store.ts @@ -1,197 +1,111 @@ -import { createHash } from "node:crypto"; -import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; -import * as path from "node:path"; -import { defaultCountTokens, type CoreMessage } from "acp-kernel"; +import { + applyRetrieve, + buildStoredPlaceholder, + contentStoreStats, + createContentStore, + noteRetrieval, + RETRIEVE_TOOL_NAME, + type CoreMessage, + type MessageContentStore, +} from "acp-kernel"; import { log as loggerLog } from "./logger.js"; -import { getStore, storePayloadCodec } from "./persist.js"; -import { storeDir } from "./paths.js"; -import { RETRIEVE_TOOL_NAME } from "./compress-tool.js"; +import { getStore } from "./persist.js"; import type { CompressSettings } from "./config.js"; import type { Session } from "./session.js"; -export type StoreConfig = NonNullable; +export type CcrSettings = NonNullable; -/** Per-message-ref record of ID-referenced content. `hash` is the sha1 of the - * original bytes — the content address, the dedup key, and the sidecar - * filename. The rest is deterministic metadata the wire placeholder renders so - * the model can judge relevance without fetching. */ -export type StoredEntry = { - hash: string; - bytes: number; - tokens: number; - kind: "tool-result" | "image"; - tool?: string; -}; +const EFFECTIVE_CCR_KEY = "effectiveCcr"; -const EFFECTIVE_STORE_KEY = "effectiveStore"; -const INDEX_KEY = "storeIndex"; - -// Tool results below this many tokens stay verbatim on the wire — the -// placeholder overhead is not worth it, and small results are cheap to keep. -const DEFAULT_MIN_TOKENS = 500; -// Per-session cap on stored bytes (logical, per-ref). Content beyond the cap is -// left verbatim (we never emit a placeholder whose original we did not store). -const DEFAULT_MAX_STORE_BYTES = 2 * 1024 * 1024; - -/** Stamp the last-resolved store policy onto the session so the view/execution - * sites — and the plugin-tool path, which resolves sessions without a request - * context — can read it back. Mirrors storeEffectiveAbsorb (#833). */ -export function storeEffectiveStore(session: Session, store: StoreConfig | undefined): void { - session.metadata[EFFECTIVE_STORE_KEY] = store ?? null; +/** Stamp the last-resolved CCR policy onto the session (per-request; the + * processTurn loop config must match or be stripped, mirroring absorb). */ +export function storeEffectiveCcr(session: Session, ccr: CcrSettings | undefined): void { + session.metadata[EFFECTIVE_CCR_KEY] = ccr ?? null; } -/** Read back the last-resolved store policy stamped by {@link storeEffectiveStore}. */ -export function effectiveStoreConfig(session: Session | undefined): StoreConfig | undefined { - const meta = session?.metadata[EFFECTIVE_STORE_KEY]; - if (meta && typeof meta === "object" && typeof (meta as StoreConfig).enabled === "boolean") { - return meta as StoreConfig; +/** Read back the CCR policy stamped by {@link storeEffectiveCcr}. */ +export function effectiveCcr(session: Session | undefined): CcrSettings | undefined { + const meta = session?.metadata[EFFECTIVE_CCR_KEY]; + if (meta && typeof meta === "object" && typeof (meta as CcrSettings).enabled === "boolean") { + return meta as CcrSettings; } return undefined; } -export function storeEnabled(session: Session | undefined): boolean { - return effectiveStoreConfig(session)?.enabled === true; +export function ccrEnabled(session: Session | undefined): boolean { + return effectiveCcr(session)?.enabled === true; } -/** The per-ref index lives in session.metadata (persisted via buildRecord's - * metadata spread — atomic, compaction-proof, zero new deps, same lifecycle as - * blockContents). It holds metadata + a content hash only, NEVER payload bytes. */ -function indexOf(session: Session): Record { - const raw = session.metadata[INDEX_KEY]; - return raw && typeof raw === "object" && !Array.isArray(raw) ? (raw as Record) : {}; +/** Model-facing retrieve tool name for this session: the config `ccr.toolName` + * override when set, else the kernel default. */ +export function retrieveToolName(session: Session | undefined): string { + return effectiveCcr(session)?.toolName ?? RETRIEVE_TOOL_NAME; } -function setIdx(session: Session, idx: Record): void { - session.metadata[INDEX_KEY] = idx; -} - -function idSlug(id: string): string { - return createHash("sha256").update(id, "utf8").digest("hex").slice(0, 20); -} - -function poolDirFor(session: Session): string { - return path.join(storeDir(), idSlug(session.id)); -} - -function poolFileFor(session: Session, hash: string): string { - return path.join(poolDirFor(session), `${hash}.txt`); -} - -/** Store `text` under `ref`, writing the original to a hash-keyed sidecar file - * and recording only metadata in the persisted index. Idempotent: re-storing - * the same ref returns the existing entry. Returns null when disabled, too - * small, or the byte budget is exhausted (the caller then leaves the original - * on the wire rather than emitting an unretrievable placeholder). */ -export function ensureStored(session: Session, ref: string, text: string, tool?: string): StoredEntry | null { - const cfg = effectiveStoreConfig(session); - if (!cfg?.enabled || !ref || !text) return null; - const existing = indexOf(session)[ref]; - if (existing) return existing; - const minTokens = cfg.minTokens ?? DEFAULT_MIN_TOKENS; - const maxStoreBytes = cfg.maxStoreBytes ?? DEFAULT_MAX_STORE_BYTES; - const tokens = defaultCountTokens(text); - if (tokens < minTokens) return null; - const bytes = Buffer.byteLength(text, "utf8"); - const idx = indexOf(session); - const held = Object.values(idx).reduce((a, e) => a + e.bytes, 0); - if (held + bytes > maxStoreBytes) return null; - const hash = createHash("sha1").update(text, "utf8").digest("hex"); - // Payload-file-first: persist the original BEFORE recording the index entry, - // so a crash mid-arrival leaves at worst an orphan file (swept later) — never - // a placeholder pointing at missing content. - try { - const dir = poolDirFor(session); - const file = poolFileFor(session, hash); - if (!existsSync(dir)) mkdirSync(dir, { recursive: true }); - if (!existsSync(file)) { - const codec = storePayloadCodec(); - writeFileSync(file, codec ? codec.encode(text) : text); - } - } catch (err) { - loggerLog("warn", `[store] failed to persist ${ref}: ${err instanceof Error ? err.message : String(err)}`); - return null; +/** Lazily materialize the session's kernel content-store envelope: loaded + * from the session's content-store.json on first touch, fresh when the file + * is absent (or corrupt — degraded to retrieve misses, never a crash). */ +export function contentStoreOf(session: Session): MessageContentStore { + if (!session.contentStore) { + session.contentStore = getStore().loadContentStore(session) ?? createContentStore(); } - idx[ref] = { hash, bytes, tokens, kind: "tool-result", ...(tool ? { tool } : {}) }; - setIdx(session, idx); - session.stats.storedBytes = Object.values(idx).reduce((a, e) => a + e.bytes, 0); - getStore().scheduleSave(session); - return idx[ref]; -} - -/** Render the deterministic wire placeholder for a stored ref. Carries enough - * signal (tool, size, a head preview) for the model to judge relevance without - * fetching, plus the exact acp_retrieve call to make. It also states that - * summarizing is REVERSIBLE — the original persists under this ref — so the - * model can freely pick gist (absorb) or full (retrieve); both fidelities - * coexist under the same id. Byte-stable for a given ref+original, so the - * upstream prefix cache stays warm across turns. */ -export function renderPlaceholder(ref: string, entry: StoredEntry, original: string): string { - const label = entry.tool ?? "tool"; - const head = original.replace(/\s+/g, " ").trim().slice(0, 60); - const headPart = head ? ` ${JSON.stringify(head)}` : ""; - return `\u{1F4E6} [stored #${ref} \u00B7 ${label} \u00B7 ${entry.tokens} tok]${headPart} \u2192 ${RETRIEVE_TOOL_NAME}("${ref}") restores full text; safe to summarize \u2014 the original stays retrievable by this ref`; + return session.contentStore; } -/** ID-reference view: replace oversized tool-result content with a deterministic - * placeholder, storing the original in the session content store first. Runs - * after the absorb view (so model-triggered absorption wins); only shrinks the - * content INSIDE a tool result, leaving role/toolCallId pairing intact. Pure - * with respect to the wire for a given input. `storeBytesSaved` is recomputed - * (not accumulated) here so repeated re-substitution across turns does not - * inflate it — it reflects the CURRENT wire savings. */ -export function applyStoreView(messages: CoreMessage[], session: Session): CoreMessage[] { - if (effectiveStoreConfig(session)?.enabled !== true) return messages; - const cfg = effectiveStoreConfig(session)!; - const minTokens = cfg.minTokens ?? DEFAULT_MIN_TOKENS; - const byRaw = session.state.messageRefs?.byRaw ?? {}; - let changed = false; +/** Adopt the store returned by kernel processTurn (append-only, first write + * wins — refs are never rewritten). Stats and the persist dirty flag move + * only when new entries appeared, so a no-growth turn costs nothing. */ +export function adoptContentStore(session: Session, store: MessageContentStore): void { + const prev = session.contentStore; + const added = prev + ? Object.entries(store.byRef).filter(([ref]) => !(ref in prev.byRef)) + : Object.entries(store.byRef); + session.contentStore = store; + if (added.length === 0) return; let saved = 0; - const out = messages.map((m) => { - if (m.role !== "tool" || m.contentType !== "tool-result") return m; - const text = m.text ?? ""; - if (defaultCountTokens(text) < minTokens) return m; - const ref = byRaw[m.id]; - if (!ref) return m; - const entry = ensureStored(session, ref, text, m.toolName); - if (!entry) return m; - const placeholder = renderPlaceholder(ref, entry, text); - saved += Math.max(0, Buffer.byteLength(text, "utf8") - Buffer.byteLength(placeholder, "utf8")); - changed = true; - return { ...m, text: placeholder }; - }); - if (changed) session.stats.storeBytesSaved = saved; - return changed ? out : messages; + for (const [ref, entry] of added) { + const placeholder = buildStoredPlaceholder({ + ref, + kind: entry.kind, + tokens: entry.tokens, + head: entry.head, + retrieveToolName: retrieveToolName(session), + }); + saved += Math.max(0, entry.chars - Buffer.byteLength(placeholder, "utf8")); + } + session.stats.storedBytes = contentStoreStats(store).totalChars; + session.stats.storeBytesSaved = (session.stats.storeBytesSaved ?? 0) + saved; + session.contentStoreDirty = true; } -/** Execute an acp_retrieve call: resolve a stored ref back to its full original - * text, lazy-loading the SINGLE sidecar file named by the index entry (no - * whole-blob parse). The returned content rides the ephemeral tool-result - * channel (the same intra-request re-request path as decompress) — it never - * enters fold space and consumes no message ref. Not-found costs one tool call - * and self-corrects. */ +/** Execute a retrieve-tool call against the kernel store: resolve the ref, + * count hit/miss, and queue the full-text injection for the re-request path + * (request-only, same channel as nudges — never persisted, structurally + * excluded from refs). Returns the deterministic ack that rides as the tool + * result on every wire. A hallucinated ref costs one tool call by design. */ export function executeRetrieve(args: Record, session: Session): string { session.stats.retrieveCalls = (session.stats.retrieveCalls ?? 0) + 1; const rawRef = args.ref; const ref = typeof rawRef === "string" ? rawRef.trim() : ""; if (!ref) { session.stats.retrieveMisses = (session.stats.retrieveMisses ?? 0) + 1; - return `[${RETRIEVE_TOOL_NAME} FAILED: ref (an mNNNNN id) is required]`; + return `[${retrieveToolName(session)} FAILED: ref (an mNNNNN id) is required]`; } - const entry = indexOf(session)[ref]; - if (!entry) { + const result = applyRetrieve({ store: contentStoreOf(session), ref }); + if (!result.ok) { session.stats.retrieveMisses = (session.stats.retrieveMisses ?? 0) + 1; - return `[${RETRIEVE_TOOL_NAME} FAILED: ${ref} not found in store]`; - } - let text: string; - try { - const buf = readFileSync(poolFileFor(session, entry.hash)); - const codec = storePayloadCodec(); - text = codec ? codec.decode(buf) : buf.toString("utf8"); - } catch { - session.stats.retrieveMisses = (session.stats.retrieveMisses ?? 0) + 1; - return `[${RETRIEVE_TOOL_NAME} FAILED: ${ref} content unavailable]`; + loggerLog("info", `[ccr] retrieve ${ref}: miss (${result.reason})`); + return result.ackText; } session.stats.retrieveHits = (session.stats.retrieveHits ?? 0) + 1; - loggerLog("info", `[store] retrieve ${ref} (${entry.tokens} tok, ${entry.bytes} B)${entry.tool ? ` tool=${entry.tool}` : ""}`); - return `[retrieved #${ref} \u00B7 ${entry.tokens} tok]\n${text}`; + session.state = noteRetrieval(session.state); + session.pendingRetrievals.push(result.injection); + loggerLog("info", `[ccr] retrieve ${ref} (${result.entry.tokens} tok, ${result.entry.chars} chars)`); + return result.ackText; +} + +/** Drain queued retrieval injections: callers append them to the re-request + * message list AFTER the tool-result pair (ack first, full text second). */ +export function drainPendingRetrievals(session: Session): CoreMessage[] { + return session.pendingRetrievals?.length ? session.pendingRetrievals.splice(0) : []; } diff --git a/src/stream-google.ts b/src/stream-google.ts index 789e6bdbbb..c6cd607b61 100644 --- a/src/stream-google.ts +++ b/src/stream-google.ts @@ -3,6 +3,7 @@ import type { GooglePart } from "acp-kernel/wire"; import type { Session } from "./session.js"; import { isProxyToolFor } from "./absorb.js"; import { executeProxyTool } from "./loop/core.js"; +import { drainPendingRetrievals } from "./store.js"; import type { RewriteCtx } from "./stream.js"; import { containsMarkerLineText, containsRenderTagText, stripAcpTags } from "./loop/tag-echo-filter.js"; @@ -35,6 +36,11 @@ export function rewriteGoogleJsonResponse(body: unknown, ctx: RewriteCtx): unkno } keptParts.push(part); } + // #1097: non-stream has no re-request to ride — retrieval full text rides + // inline after the ack, same as the anthropic rewrite twin. + for (const injection of drainPendingRetrievals(ctx.session)) { + if (typeof injection.text === "string") noteParts.push(injection.text); + } if (!converted) return body; // The tool result is a NEW text part: an existing text part may carry a // thoughtSignature the client echoes back, and appending to its text would diff --git a/src/stream.ts b/src/stream.ts index 287b7e8039..e006263768 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -2,10 +2,10 @@ import { collectBlockContent, type CompressionCore, type Config, type CoreMessag import { handleAcpStatus } from "./acp-status.js"; import { handleAcpCache, recordCacheFoldsFromBlocks } from "./cache-ledger.js"; import { type Session, cacheBlockContent, markDirty } from "./session.js"; -import { COMPRESS_TOOL_NAME, parseCompressInput, ABSORB_TOOL_NAME, RETRIEVE_TOOL_NAME, type ParsedRange } from "./compress-tool.js"; +import { COMPRESS_TOOL_NAME, parseCompressInput, ABSORB_TOOL_NAME, type ParsedRange } from "./compress-tool.js"; import { effectiveAbsorbConfig, executeAbsorb, isProxyToolFor } from "./absorb.js"; import { executeSearchContextTarget, resolveDecompress } from "./decompress-shared.js"; -import { effectiveStoreConfig, executeRetrieve } from "./store.js"; +import { ccrEnabled, drainPendingRetrievals, executeRetrieve, retrieveToolName } from "./store.js"; import { containsMarkerLineText, containsRenderTagText, stripAcpTags } from "./loop/tag-echo-filter.js"; import { maxShrinkPerCompress } from "./fetch-util.js"; @@ -46,8 +46,12 @@ function executeAnthropicProxyTool(toolName: string, args: Record 0 ? injections.reduce((acc, inj) => `${acc}\n\n${inj.text}`, ack) : ack; } return `[Unknown proxy tool: ${toolName}]`; } diff --git a/tests/store-marker-gate.test.ts b/tests/store-marker-gate.test.ts index 37254bb976..31d72ad4b7 100644 --- a/tests/store-marker-gate.test.ts +++ b/tests/store-marker-gate.test.ts @@ -1,6 +1,5 @@ import assert from "node:assert/strict"; import http from "node:http"; -import { mkdtempSync, rmSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { once } from "node:events"; @@ -8,18 +7,19 @@ import test from "node:test"; process.env.NODE_ENV = "test"; -import { defaultConfig } from "acp-kernel"; +import { buildStoredPlaceholder, defaultConfig, RETRIEVE_TOOL_NAME } from "acp-kernel"; import { startServer, type ProxyOptions } from "../src/server.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; -// [#1097] The store must never emit a placeholder the model cannot retrieve. -// The responses text/marker protocol declares only the readonly ACP tools (no -// native tool channel for acp_retrieve), so an armed store there would silently -// lose oversized tool results. These tests pin the gate at the policy-stamping -// site: marker protocol → no placeholder, original stays verbatim; native tool -// protocol → placeholder + acp_retrieve declaration. -const PLACEHOLDER_MARK = "[stored #"; +// [#1097] The kernel content store must never emit a placeholder the model +// cannot retrieve. The responses text/marker protocol declares only the +// readonly ACP tools (no native tool channel for the retrieve tool), so an +// armed store there would silently lose oversized tool results. These tests +// pin the gate at the policy-stamping site: marker protocol → no placeholder, +// original stays verbatim; native tool protocol → placeholder + retrieve tool +// declaration, in the kernel's [acp-stored format. +const PLACEHOLDER_MARK = "[acp-stored #"; const BIG_OUTPUT = "big-output-line-0123456789abcdef\n".repeat(512); interface Harness { @@ -43,10 +43,8 @@ async function startHarness(marker: boolean): Promise { upstream.listen(0, "127.0.0.1"); await once(upstream, "listening"); const upstreamPort = upstream.address().port; - _setStoreForTest(new SessionStore({ enabled: false })); + _setStoreForTest(new SessionStore({ dir: path.join(os.tmpdir(), `bili-store-gate-${marker ? "marker" : "native"}-${Math.random().toString(36).slice(2)}`), debounceMs: 0 })); setRegistryForTest({}); - const storeDir = mkdtempSync(path.join(os.tmpdir(), "bili-store-gate-")); - process.env.BILI_STORE_DIR = storeDir; const route: Record = { models: { "gpt-test": { context: 400_000 } } }; if (marker) route.compressProtocol = "marker"; const proxy = await startServer({ @@ -57,7 +55,7 @@ async function startHarness(marker: boolean): Promise { modelContextLimit: 400_000, kernelConfig: defaultConfig(400_000), promptCache: { routing: "auto" }, - compress: { injectTool: true, injectNudge: false, store: { enabled: true, minTokens: 500 } }, + compress: { injectTool: true, injectNudge: false, ccr: { enabled: true, minToolTokens: 500 } }, sessionHeader: "x-acp-session", log: false, debug: false, @@ -75,7 +73,6 @@ async function startHarness(marker: boolean): Promise { await once(proxy, "close"); upstream.close(); await once(upstream, "close"); - rmSync(storeDir, { recursive: true, force: true }); }, }; } @@ -100,7 +97,7 @@ async function sendBigToolResult(h: Harness, sessionId: string): Promise { assert.equal(resp.status, 200, `request failed: ${text.slice(0, 300)}`); } -test("#1097: marker-protocol responses wire — store disarmed, oversized tool result stays verbatim (no unretrievable placeholder)", async () => { +test("#1097: marker-protocol responses wire — ccr disarmed, oversized tool result stays verbatim (no unretrievable placeholder)", async () => { const h = await startHarness(true); try { await sendBigToolResult(h, "store-gate-marker"); @@ -109,22 +106,33 @@ test("#1097: marker-protocol responses wire — store disarmed, oversized tool r assert.ok(!body.includes(PLACEHOLDER_MARK), "marker-protocol wire must not carry a store placeholder"); assert.ok(!body.includes("acp_retrieve"), "acp_retrieve must not be declared or referenced on the marker-protocol wire"); const line = "big-output-line-0123456789abcdef"; - assert.ok(body.includes(`${line}\\n${line}`), "original tool result must stay verbatim when the store is disarmed"); + assert.ok(body.includes(`${line}\\n${line}`), "original tool result must stay verbatim when ccr is disarmed"); assert.ok(body.length > BIG_OUTPUT.length, "full tool result payload missing from the outbound body"); } finally { await h.close(); } }); -test("#1097: native-tool responses wire — store armed, placeholder emitted and acp_retrieve declared (control)", async () => { +test("#1097: native-tool responses wire — ccr armed, kernel placeholder emitted and acp_retrieve declared (control)", async () => { const h = await startHarness(false); try { await sendBigToolResult(h, "store-gate-control"); assert.equal(h.captured.length, 1, `expected exactly one upstream request, got ${h.captured.length}`); const body = h.captured[0]!; - assert.ok(body.includes(PLACEHOLDER_MARK), `native-tool wire must carry the store placeholder: ${body.slice(0, 400)}`); + assert.ok(body.includes(PLACEHOLDER_MARK), `native-tool wire must carry the kernel store placeholder: ${body.slice(0, 400)}`); assert.ok(body.includes('"name":"acp_retrieve"'), "acp_retrieve tool declaration missing on the native-tool wire"); + // the placeholder must teach retrieval in the kernel format + const ref = body.match(/\[acp-stored #(m\d+)/)?.[1]; + assert.ok(ref, "placeholder must cite an mNNNNN ref"); + assert.ok(body.includes(`${RETRIEVE_TOOL_NAME}(\\"${ref}\\")`), `placeholder must show the retrieve call for ${ref}`); } finally { await h.close(); } }); + +test("#1097: kernel placeholder format reference — buildStoredPlaceholder renders the shape the gate asserts on", () => { + const text = buildStoredPlaceholder({ ref: "m00423", kind: "shell output", tokens: 4213, head: "gen", command: "gen", retrieveToolName: RETRIEVE_TOOL_NAME }); + assert.ok(text.startsWith("\u{1F4E6} [acp-stored #m00423"), text); + assert.ok(text.includes("4,213 tok")); + assert.ok(text.includes(`${RETRIEVE_TOOL_NAME}("m00423")`)); +}); diff --git a/tests/store.test.ts b/tests/store.test.ts index 6230ef314c..3332eccddf 100644 --- a/tests/store.test.ts +++ b/tests/store.test.ts @@ -1,24 +1,29 @@ import test from "node:test"; import assert from "node:assert/strict"; -import { existsSync, mkdtempSync, readdirSync, readFileSync } from "node:fs"; +import { existsSync, mkdtempSync, readdirSync, rmSync, statSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; -import { applyAbsorb, createCore, createInitialState, defaultConfig, type CoreMessage } from "acp-kernel"; import { - applyStoreView, - effectiveStoreConfig, - ensureStored, - executeRetrieve, - storeEffectiveStore, -} from "../src/store.ts"; + buildStoredPlaceholder, + createCore, + createInitialState, + DEFAULT_CCR_CONFIG, + defaultConfig, + STORED_PLACEHOLDER_MARKER, + type CoreMessage, + type MessageContentStore, +} from "acp-kernel"; +import { parseCompressSettings } from "../src/config.ts"; +import { applyCompressSettings, mergeCompress } from "../src/compress-settings.ts"; +import { adoptContentStore, drainPendingRetrievals, executeRetrieve, retrieveToolName, storeEffectiveCcr, contentStoreOf } from "../src/store.ts"; import { RETRIEVE_TOOL_NAME } from "../src/compress-tool.ts"; -import { getSession, type Session } from "../src/session.ts"; +import { getSession } from "../src/session.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; -// Route every sidecar write to a throwaway dir and disable session-file -// persistence so these unit tests never touch the real data/state trees. -const STORE_TMP = mkdtempSync(path.join(tmpdir(), "bili-store-test-")); -process.env.BILI_STORE_DIR = STORE_TMP; +// Unit tests never touch the real data/state trees: persistence off by +// default; the envelope round-trip builds its own throwaway SessionStore. process.env.BILI_PERSIST = "0"; +const PERSIST_TMP = mkdtempSync(path.join(tmpdir(), "bili-ccr-persist-")); const BIG_TEXT = "line of build output ".repeat(700); @@ -30,102 +35,163 @@ function toolResult(): CoreMessage[] { ]; } -// Build a session whose messageRefs map the tool-result raw id to a kernel ref, -// mirroring the live pipeline (processTurn assigns the refs we later cite). -function makeSession(withStore: boolean): { session: Session; ref: string } { - const session = getSession(`t-store-${Math.random().toString(36).slice(2)}`); - const core = createCore(); - const turn = core.processTurn({ messages: toolResult(), state: createInitialState(), config: defaultConfig(200000), tokenCount: 0, renderTags: "text-only" }); - session.state.messageRefs = turn.state.messageRefs; - const ref = Object.entries(turn.state.messageRefs.byRef ?? {}).find(([, raw]) => raw === "t-res")?.[0] ?? ""; - storeEffectiveStore(session, withStore ? { enabled: true, minTokens: 500 } : { enabled: false }); - return { session, ref }; +function ccrConfig() { + return applyCompressSettings(defaultConfig(200000), 200_000, { + absorb: { enabled: true, minToolTokens: 50 }, + ccr: { enabled: true, minToolTokens: 50 }, + }); } -function findSidecar(hash: string): string | null { - for (const d of readdirSync(STORE_TMP)) { - const p = path.join(STORE_TMP, d, `${hash}.txt`); - if (existsSync(p)) return p; - } - return null; +function turnWith(cfg: ReturnType, store?: MessageContentStore) { + const core = createCore(); + return core.processTurn({ messages: toolResult(), state: createInitialState(), config: cfg, tokenCount: 0, renderTags: "text-only", ...(store ? { contentStore: store } : {}) }); } -test("effectiveStoreConfig reflects the stamped policy", () => { - assert.equal(effectiveStoreConfig(makeSession(false).session)?.enabled, false); - assert.equal(effectiveStoreConfig(makeSession(true).session)?.enabled, true); - assert.equal(effectiveStoreConfig(undefined), undefined); +test("parseCompressSettings: ccr key validates shape and types", () => { + const okFull = parseCompressSettings({ + ccr: { enabled: true, minToolTokens: 500, excludeTools: ["webfetch"], toolName: "fetch_original", maxHeadChars: 64 }, + }); + assert.ok(okFull); + assert.deepEqual(okFull.ccr, { enabled: true, minToolTokens: 500, excludeTools: ["webfetch"], toolName: "fetch_original", maxHeadChars: 64 }); + + // toolName is trimmed; empty after trim is invalid + assert.equal(parseCompressSettings({ ccr: { toolName: " lookup " } }).ccr?.toolName, "lookup"); + assert.equal(parseCompressSettings({ ccr: { toolName: " " } }), undefined); + // wrong types reject the whole settings block (fail loudly, #155) + assert.equal(parseCompressSettings({ ccr: { enabled: "yes" } }), undefined); + assert.equal(parseCompressSettings({ ccr: { minToolTokens: "500" } }), undefined); + assert.equal(parseCompressSettings({ ccr: { maxHeadChars: NaN } }), undefined); + assert.equal(parseCompressSettings({ ccr: { excludeTools: [42] } }), undefined); + assert.equal(parseCompressSettings({ ccr: { excludeTools: "webfetch" } }), undefined); + assert.equal(parseCompressSettings({ ccr: "on" }), undefined); +}); + +test("mergeCompress: ccr merges sub-field-wise across the three levels", () => { + const global = parseCompressSettings({ ccr: { enabled: true, excludeTools: ["webfetch"] } })!; + const model = parseCompressSettings({ ccr: { minToolTokens: 200 } })!; + const merged = mergeCompress(global, undefined, model); + assert.deepEqual(merged.ccr, { enabled: true, minToolTokens: 200, excludeTools: ["webfetch"] }); + + // a deeper level can override a scalar without clobbering siblings + const provider = parseCompressSettings({ ccr: { toolName: "lookup" } })!; + const merged2 = mergeCompress(global, provider, model); + assert.deepEqual(merged2.ccr, { enabled: true, minToolTokens: 200, excludeTools: ["webfetch"], toolName: "lookup" }); + + assert.equal(mergeCompress(undefined, undefined, undefined).ccr, undefined); }); -test("ensureStored: stores large content once, idempotent, skips small/disabled", () => { - const { session, ref } = makeSession(true); - const e1 = ensureStored(session, ref, BIG_TEXT, "bash"); - assert.ok(e1 && e1.tokens > 500 && e1.bytes > 0); - assert.equal(e1!.hash.length, 40); - assert.equal(ensureStored(session, ref, BIG_TEXT, "bash"), e1); - assert.equal(ensureStored(session, "m00002", "tiny", "bash"), null); - assert.equal(ensureStored(makeSession(false).session, ref, BIG_TEXT, "bash"), null); +test("applyCompressSettings: maps ccr onto kernel CcrConfig with DEFAULT_CCR_CONFIG defaults", () => { + const base = defaultConfig(200000); + const out = applyCompressSettings(base, 200_000, { ccr: { enabled: true, minToolTokens: 200 } }); + assert.deepEqual(out.ccr, { ...DEFAULT_CCR_CONFIG, minToolTokens: 200, enabled: true }); + // absent block leaves base.ccr untouched (kernel default = feature off) + const untouched = applyCompressSettings(base, 200_000, {}); + assert.equal(untouched.ccr, base.ccr); }); -test("applyStoreView replaces the oversized tool result with a stable placeholder, pairing intact", () => { - const { session, ref } = makeSession(true); - const out = applyStoreView(toolResult(), session); - const res = out.find((m) => m.id === "t-res")!; - assert.match(res.text!, new RegExp(`stored #${ref}`)); - assert.match(res.text!, new RegExp(RETRIEVE_TOOL_NAME)); +test("integration: kernel processTurn ID-references the oversized tool result (ccr+absorb enabled)", () => { + const cfg = ccrConfig(); + const turn = turnWith(cfg); + const res = turn.messages.find((m) => m.role === "tool" && m.toolCallId === "call_1")!; + assert.ok(res, "tool-result message survived the turn"); assert.notEqual(res.text, BIG_TEXT); - assert.equal(res.role, "tool"); - assert.equal(res.contentType, "tool-result"); - assert.equal(res.toolCallId, "call_1"); - assert.equal(out.find((m) => m.id === "a-tc")!.text, JSON.stringify({ command: "npm run build" })); - const again = applyStoreView(toolResult(), session); - assert.equal(again.find((m) => m.id === "t-res")!.text, res.text); + assert.ok(res.text!.includes(STORED_PLACEHOLDER_MARKER), `placeholder expected: ${res.text!.slice(0, 160)}`); + const ref = Object.keys(turn.contentStore.byRef)[0]!; + assert.ok(res.text!.includes(ref), "placeholder cites the stored ref"); + assert.ok(res.text!.includes(RETRIEVE_TOOL_NAME), "placeholder names the retrieve tool"); + assert.equal(turn.contentStore.byRef[ref]!.rawId, "t-res"); + assert.equal(Object.keys(turn.contentStore.byHash).length, 1); + // tool-call pairing survives substitution + assert.equal(turn.messages.find((m) => m.id === "a-tc")!.text, JSON.stringify({ command: "npm run build" })); }); -test("applyStoreView is a no-op when the store is disabled", () => { - const { session } = makeSession(false); - const out = applyStoreView(toolResult(), session); - assert.equal(out.find((m) => m.id === "t-res")!.text, BIG_TEXT); +test("integration: ccr off → byte-identical pass-through", () => { + const cfg = applyCompressSettings(defaultConfig(200000), 200_000, {}); + assert.notEqual(cfg.ccr?.enabled, true); + const turn = turnWith(cfg); + const res = turn.messages.find((m) => m.role === "tool" && m.toolCallId === "call_1")!; + assert.equal(res.text, BIG_TEXT); + assert.equal(Object.keys(turn.contentStore.byRef).length, 0); }); -test("executeRetrieve round-trips the original; a bad ref self-corrects", () => { - const { session, ref } = makeSession(true); - ensureStored(session, ref, BIG_TEXT, "bash"); - const got = executeRetrieve({ ref }, session); - assert.ok(got.includes(BIG_TEXT.slice(0, 80))); +test("executeRetrieve: hit queues injection + ack, miss self-corrects, tool name follows config", () => { + const session = getSession(`t-ccr-${Math.random().toString(36).slice(2)}`); + storeEffectiveCcr(session, { enabled: true, toolName: "lookup", minToolTokens: 50 }); + assert.equal(retrieveToolName(session), "lookup"); + adoptContentStore(session, turnWith(ccrConfig()).contentStore); + const ref = Object.keys(session.contentStore!.byRef)[0]!; + + const ack = executeRetrieve({ ref }, session); + assert.match(ack, new RegExp(`retrieved ${ref}: [\\d,]+ tok`)); assert.equal(session.stats.retrieveCalls, 1); assert.equal(session.stats.retrieveHits, 1); + const injections = drainPendingRetrievals(session); + assert.equal(injections.length, 1); + assert.equal(injections[0]!.id, `acp_retrieved_${ref}`); + assert.ok(injections[0]!.text.includes(BIG_TEXT.slice(0, 80)), "injection carries the full original"); + assert.equal(drainPendingRetrievals(session).length, 0); + const miss = executeRetrieve({ ref: "m99999" }, session); assert.match(miss, /not found/); assert.equal(session.stats.retrieveMisses, 1); assert.equal(session.stats.retrieveCalls, 2); + assert.equal(drainPendingRetrievals(session).length, 0, "a miss queues nothing"); + // malformed arg is a miss, not a crash + assert.match(executeRetrieve({}, session), /ref/); + assert.equal(session.stats.retrieveMisses, 2); }); -test("sidecar is written to disk under storeDir and the index lands in session.metadata", () => { - const { session, ref } = makeSession(true); - const e = ensureStored(session, ref, BIG_TEXT, "bash")!; - const file = findSidecar(e.hash); - assert.ok(file, "sidecar file was written under storeDir"); - assert.equal(readFileSync(file!, "utf8"), BIG_TEXT); - const idx = session.metadata.storeIndex as Record; - assert.equal(idx[ref].hash, e.hash); - assert.ok(!JSON.stringify(idx).includes(BIG_TEXT.slice(0, 40)), "index holds metadata only, never payload bytes"); +test("buildStoredPlaceholder renders the kernel wire format the gates assert on", () => { + const text = buildStoredPlaceholder({ ref: "m00423", kind: "shell output", tokens: 4213, head: "npm run build", command: "npm run build", retrieveToolName: RETRIEVE_TOOL_NAME }); + assert.ok(text.includes("[acp-stored #m00423")); + assert.ok(text.includes("shell output")); + assert.ok(text.includes("4,213 tok")); + assert.ok(text.includes(`${RETRIEVE_TOOL_NAME}("m00423")`), "placeholder tells the model how to retrieve"); + assert.ok(text.includes("npm run build")); }); -test("absorbing a stored item keeps its original retrievable", () => { - const { session, ref } = makeSession(true); - const substituted = applyStoreView(toolResult(), session); - assert.match(substituted.find((m) => m.id === "t-res")!.text!, new RegExp(`stored #${ref}`)); - const outcome = applyAbsorb({ - ref, - summary: "build succeeded in 3s", - absorbCallId: undefined, - messages: toolResult(), - state: session.state, - config: { ...defaultConfig(200000), absorb: { enabled: true } }, - }); - session.state = outcome.state; - assert.equal(outcome.ok, true, `absorb should succeed: ${outcome.resultText}`); - const got = executeRetrieve({ ref }, session); - assert.ok(got.includes(BIG_TEXT.slice(0, 80)), "retrieve returns the full original"); - assert.ok(!got.includes("build succeeded in 3s"), "retrieve returns the original, not the digest"); +test("envelope round-trip: dirty flag gates the write; reload restores the store", () => { + const store = new SessionStore({ dir: PERSIST_TMP, debounceMs: 0 }); + _setStoreForTest(store); + try { + const session = getSession(`ccr-rt-${Math.random().toString(36).slice(2)}`); + storeEffectiveCcr(session, { enabled: true, minToolTokens: 50 }); + adoptContentStore(session, turnWith(ccrConfig()).contentStore); + const ref = Object.keys(session.contentStore!.byRef)[0]!; + + // clean store → no envelope write + session.contentStoreDirty = false; + store.flushSync(session); + assert.equal(findEnvelope(PERSIST_TMP), null, "clean store must not write the envelope"); + + // dirty → written; round-trip restores it verbatim + session.contentStoreDirty = true; + assert.ok(store.flushSync(session)); + const file = findEnvelope(PERSIST_TMP); + assert.ok(file, "content-store.json written under the session dir"); + const fresh = getSession(session.id); + const loaded = contentStoreOf(fresh); + assert.equal(loaded.byRef[ref]!.rawId, "t-res"); + assert.equal(loaded.byHash[loaded.byRef[ref]!.hash], BIG_TEXT); + + // rebase reset: store cleared + dirty → file deleted + session.contentStore = undefined; + session.contentStoreDirty = true; + store.flushSync(session); + assert.equal(findEnvelope(PERSIST_TMP), null, "emptied store deletes the envelope"); + } finally { + _setStoreForTest(new SessionStore({ enabled: false })); + rmSync(PERSIST_TMP, { recursive: true, force: true }); + } }); + +function findEnvelope(root: string): string | null { + for (const d of readdirSync(root)) { + const p = path.join(root, d); + if (!statSync(p).isDirectory()) continue; + for (const f of readdirSync(p)) { + if (f.endsWith(".content-store.json")) return path.join(p, f); + } + } + return null; +} From 1e097d0260bd3ac636cfc44c92c40af5511ef640 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 23 Sep 2026 19:49:37 +0800 Subject: [PATCH 5/5] docs: align CCR docs with the kernel-native ccr.* surface (#1097) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The rework onto acp-kernel 0.0.84 changed the config surface from the v1 host-side compress.store.{minTokens,maxStoreBytes} + BILI_STORE_DIR sidecars to compress.ccr.{minToolTokens,excludeTools,toolName,maxHeadChars} with a single .content-store.json envelope next to the session JSON, but the doc sections still described the removed API — an operator following them would set keys that silently keep the feature off. Rewrite both CONFIGURATION sections and both README paragraphs to the implemented surface (kernel defaults 4000/96, [acp-stored] placeholder format, no eviction yet), and normalize one mis-indented comment line in server.ts. --- CONFIGURATION.md | 14 ++++++++------ CONFIGURATION.zh-CN.md | 14 ++++++++------ README.md | 2 +- README.zh-CN.md | 2 +- src/server.ts | 2 +- 5 files changed, 19 insertions(+), 15 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index a60ab55000..a2d9a0de44 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -360,16 +360,18 @@ For each request, the proxy resolves the settings by longest-URL-prefix match (t - `toolName: string` — rename the injected tool (default `"absorb"`); the schema, system-prompt section and per-session adjudication all follow the name. Injection follows the wire's native-tool surface: proxy mode injects the tool + a static system-prompt section on the anthropic/openai/responses native-tools wires, plugin mode advertises it in the plugin manifest (the MCP shell picks it up for free). Responses **marker/text-protocol** routes are not supported (no native tool surface — the REQUIRED absorb instruction would be unsatisfiable), and title-generation requests (`max_tokens ≤ 200`) skip injection like the compress prompt does. Absorbed pairs stay hidden across restarts (persisted in the session state). -#### `store` +#### `ccr` -- **Type:** `object` (`{ enabled?, minTokens?, maxStoreBytes? }`) +- **Type:** `object` (`{ enabled?, minToolTokens?, excludeTools?, toolName?, maxHeadChars? }`) - **Default:** *(disabled — the feature is off unless you set `enabled: true`)* - **Status:** ACTIVE (v1 — proxy mode, native-tools wires only) -- **Description:** Opt-in **content-addressed message store** / built-in CCR (issue #1097). Instead of force-distilling oversized tool results (like `absorb`) or carrying them on the wire forever, the original goes into a per-session content store at arrival and the wire keeps a deterministic, byte-stable placeholder (`📦 [stored #m00423 · shell · 4213 tok] "head preview" → acp_retrieve("m00423")`). The model retrieves the full original on demand via the injected `acp_retrieve` tool; a retrieve is ephemeral (it rides the intra-request tool-result channel, never enters fold space, consumes no message ref). Lossless by default: a retrieve not made costs one cheap tool call; a detail distilled away by absorb is gone for good. Sub-fields (merged deepest-wins like every other CompressSettings field): +- **Description:** Opt-in **content-addressed message store** / built-in CCR (issue #1097, via the `acp-kernel` CCR API, acp-kernel >= 0.0.84). Instead of force-distilling oversized tool results (like `absorb`) or carrying them on the wire forever, the kernel ID-references them at arrival (the ccr-store node runs between prune and absorb within `processTurn` — ID-referencing wins over distillation): the wire keeps a deterministic, byte-stable placeholder (`📦 [acp-stored #m00423 · shell output · 4,213 tok] \`npm run build\`\n → acp_retrieve("m00423") returns the full text`) and the original goes into the session's content store. The model retrieves the full original on demand via the injected `acp_retrieve` tool; a retrieve is ephemeral (it rides the intra-request tool-result channel, never enters fold space, consumes no message ref). Lossless by default: a retrieve not made costs one cheap tool call; a detail distilled away by absorb is gone for good. Sub-fields (merged deepest-wins like every other CompressSettings field): - `enabled: boolean` — master switch; anything other than `true` keeps the feature fully off (no placeholders, no tool). - - `minTokens: number` — only tool results at or above this many tokens are ID-referenced (default `500`); smaller results stay verbatim. - - `maxStoreBytes: number` — per-session cap on stored bytes, summed logically per ref (default `2 MiB`); content beyond the cap stays verbatim rather than being emitted as an unretrievable placeholder. - The index is metadata-only and persists inside the session JSON alongside `blockContents`; payloads live in hash-keyed sidecar files (`/.txt`) under `storeDir()` (env `BILI_STORE_DIR`, default `/store`), deduped by content hash and lazy-read on retrieve. Sidecars use the same at-rest codec as the session file when `BILI_ENCRYPTION_KEY` is set. Only the *content* inside a `tool` result shrinks — pairing with the assistant `tool_calls` is untouched. v1 scope gates: **proxy mode only** (plugin agents would need `acp_retrieve` advertised in their plugin manifest before a placeholder could be emitted — otherwise silent loss), and **native-tools wires only**: responses marker/text-protocol routes and `ACP_NO_INJECT_TOOL` have no channel to execute the retrieve, so the store disarms itself there instead of losing content. Per-session stats (stored bytes, current wire bytes saved, retrieve rate) surface in `acp_status`; each retrieve logs a `[store] retrieve …` line. + - `minToolTokens: number` — only tool results at or above this many tokens are ID-referenced (kernel default `4000`); smaller results stay verbatim. + - `excludeTools: string[]` — tool-name patterns never stored (glob suffixes allowed; kernel default: none). + - `toolName: string` — rename the retrieval tool (default `"acp_retrieve"`); declaration, dispatch and the placeholder hint all follow the name. Must stay unique against the client's own tool names. + - `maxHeadChars: number` — head/command preview length inside the placeholder (kernel default `96`). + The store persists as a single envelope file (`.content-store.json`) next to the session JSON, using the same at-rest codec as the session file when `BILI_ENCRYPTION_KEY` is set; entries are deduped by content hash and lazily loaded per session. Only the *content* inside a `tool` result shrinks — pairing with the assistant `tool_calls` is untouched. v1 scope gates: **proxy mode only** (plugin agents would need `acp_retrieve` advertised in their plugin manifest before a placeholder could be emitted — otherwise silent loss), and **native-tools wires only**: responses marker/text-protocol routes and `ACP_NO_INJECT_TOOL` have no channel to execute the retrieve, so the store disarms itself there instead of losing content. Known limitation: no eviction policy yet — the envelope grows with the unique originals held (tracked as a follow-up). Per-session stats (stored bytes, current wire bytes saved, retrieve rate) surface in `acp_status`; each retrieve logs a `[ccr] retrieve …` line. #### `rules` diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index fd83f0e70b..e1d526da20 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -360,16 +360,18 @@ - `toolName: string` — 重命名注入工具(默认 `"absorb"`);模式、系统提示段与按会话裁决都跟随名称。 注入跟随线上原生工具面:代理模式在 anthropic/openai/responses 原生工具线上注入工具 + 静态系统提示段,插件模式在插件清单中广告它(MCP shell 自动拾取)。Responses **marker/文本协议**路由不支持(无原生工具面 — 强制的 absorb 指令不可满足),标题生成请求(`max_tokens ≤ 200`)跳过注入如压缩提示一样。吸收配对在重启后保持隐藏(在会话状态持久化)。 -#### `store` +#### `ccr` -- **类型:** `object`(`{ enabled?, minTokens?, maxStoreBytes? }`) +- **类型:** `object`(`{ enabled?, minToolTokens?, excludeTools?, toolName?, maxHeadChars? }`) - **默认值:** *(禁用 — 除非显式设置 `enabled: true`,该特性完全关闭)* - **状态:** ACTIVE(v1 — 仅代理模式、仅原生工具线) -- **说明:** 可选开启的**内容寻址消息存储**/内置 CCR(issue #1097)。超大工具结果不再被强制蒸馏(如 `absorb`)或永远挂在线上:原文在到达时写入按会话的内容存储,线上保留一个确定性、字节稳定的占位符(`📦 [stored #m00423 · shell · 4213 tok] "头部预览" → acp_retrieve("m00423")`)。模型通过注入的 `acp_retrieve` 工具按需取回完整原文;retrieve 是临时的(走请求内工具结果通道,不进入折叠空间,不占用消息 ref)。默认无损:未执行的 retrieve 只花一次廉价工具调用;而被 absorb 蒸馏掉的细节则永久丢失。子字段(按字段最深层级胜出,与其他 CompressSettings 字段一致): +- **说明:** 可选开启的**内容寻址消息存储**/内置 CCR(issue #1097,经 `acp-kernel` CCR API,需 acp-kernel >= 0.0.84)。超大工具结果不再被强制蒸馏(如 `absorb`)或永远挂在线上:kernel 在到达时将其 ID 引用(ccr-store 节点位于 `processTurn` 内 prune 与 absorb 之间 —— ID 引用优先于蒸馏),线上保留一个确定性、字节稳定的占位符(`📦 [acp-stored #m00423 · shell output · 4,213 tok] \`npm run build\`\n → acp_retrieve("m00423") returns the full text`),原文进入该会话的内容存储。模型通过注入的 `acp_retrieve` 工具按需取回完整原文;retrieve 是临时的(走请求内工具结果通道,不进入折叠空间,不占用消息 ref)。默认无损:未执行的 retrieve 只花一次廉价工具调用;而被 absorb 蒸馏掉的细节则永久丢失。子字段(按字段最深层级胜出,与其他 CompressSettings 字段一致): - `enabled: boolean` — 主开关;任何值不为 `true` 时特性完全关闭(无占位符、无工具)。 - - `minTokens: number` — 仅达到此 token 数的工具结果被 ID 引用(默认 `500`);更小的结果保持原样。 - - `maxStoreBytes: number` — 按会话的存储字节上限,按 ref 逻辑累加(默认 `2 MiB`);超出上限的内容保持原样,而不是发出不可检索的占位符。 - 索引是纯元数据,随会话 JSON 与 `blockContents` 一同持久化;载荷存放在 `storeDir()`(环境变量 `BILI_STORE_DIR`,默认 `/store`)下的哈希键边车文件(`/.txt`),按内容哈希去重,retrieve 时懒加载。设置 `BILI_ENCRYPTION_KEY` 时,边车文件使用与会话文件相同的静态加密编解码器。只有 `tool` 结果*内部的内容*缩小——与 assistant `tool_calls` 的配对不受影响。v1 范围门控:**仅代理模式**(插件 agent 需要在其插件清单中声明 `acp_retrieve`,否则占位符会造成静默丢失);且**仅限原生工具线**:responses marker/文本协议路由与 `ACP_NO_INJECT_TOOL` 没有执行 retrieve 的通道,因此存储在这些场景下自动解除武装,而不是丢失内容。按会话统计(已存字节、当前线上节省字节、retrieve 率)在 `acp_status` 中展示;每次 retrieve 记录一条 `[store] retrieve …` 日志。 + - `minToolTokens: number` — 仅达到此 token 数的工具结果被 ID 引用(kernel 默认 `4000`);更小的结果保持原样。 + - `excludeTools: string[]` — 从不存储的工具名模式(允许 glob 后缀;kernel 默认为空)。 + - `toolName: string` — 重命名检索工具(默认 `"acp_retrieve"`);声明、分发与占位符提示都跟随名称。必须与客户端自身工具名保持唯一。 + - `maxHeadChars: number` — 占位符中头部/命令预览的长度(kernel 默认 `96`)。 + 存储以单个信封文件(`.content-store.json`)持久化在会话 JSON 旁边,设置 `BILI_ENCRYPTION_KEY` 时使用与会话文件相同的静态加密编解码器;条目按内容哈希去重,按会话懒加载。只有 `tool` 结果*内部的内容*缩小——与 assistant `tool_calls` 的配对不受影响。v1 范围门控:**仅代理模式**(插件 agent 需要在其插件清单中声明 `acp_retrieve`,否则占位符会造成静默丢失);且**仅限原生工具线**:responses marker/文本协议路由与 `ACP_NO_INJECT_TOOL` 没有执行 retrieve 的通道,因此存储在这些场景下自动解除武装,而不是丢失内容。已知限制:尚无驱逐策略——信封随持有的唯一原文数量增长(已记录为后续项)。按会话统计(已存字节、当前线上节省字节、retrieve 率)在 `acp_status` 中展示;每次 retrieve 记录一条 `[ccr] retrieve …` 日志。 #### `rules` diff --git a/README.md b/README.md index bf863917e5..326cb9842e 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ An opt-in fifth tool, `absorb` (`compress.absorb.enabled: true` — see [CONFIGU An opt-in sixth tool, `acp_rule` (`compress.rules: true` — see [CONFIGURATION.md](CONFIGURATION.md)), records **persistent principle-level reminders**: a short rule recorded by the model (user-emphasized lessons, behaviors to remember, major pitfalls hit) is hard-protected from compression — the call and its result stay in context across every fold — and omitting the argument lists the recorded rules ([ranxianglei/billion-context-pi#433](https://github.com/ranxianglei/billion-context-pi/issues/433)). -An opt-in seventh tool, `acp_retrieve` (`compress.store.enabled: true` — see [CONFIGURATION.md](CONFIGURATION.md)), backs the **content-addressed message store** (built-in CCR, #1097): oversized tool results are **ID-referenced at arrival instead of force-distilled** — the wire keeps a byte-stable placeholder and the original goes into a per-session, hash-deduped sidecar store, retrievable on demand via one cheap tool call. Lossless by default: a retrieve not made costs nothing but the call; a detail distilled away by absorb is gone for good. Proxy mode and native-tools wires only in v1 (the marker/text protocol has no channel to execute the retrieve, so the store disarms itself there rather than silently losing content). +An opt-in seventh tool, `acp_retrieve` (`compress.ccr.enabled: true` — see [CONFIGURATION.md](CONFIGURATION.md)), backs the **content-addressed message store** (built-in CCR, #1097): oversized tool results are **ID-referenced at arrival instead of force-distilled** — the wire keeps a byte-stable placeholder and the original goes into a per-session content-store envelope (hash-deduped), retrievable on demand via one cheap tool call. Lossless by default: a retrieve not made costs nothing but the call; a detail distilled away by absorb is gone for good. Proxy mode and native-tools wires only in v1 (the marker/text protocol has no channel to execute the retrieve, so the store disarms itself there rather than silently losing content). A sibling protection knob, `compress.protectedLatestTools` (see [CONFIGURATION.md](CONFIGURATION.md)), keeps the **latest** snapshot of a cumulative tool (a client's todo/task list, e.g. `["todo_list", "TodoWrite"]`) un-compressible while older instances fold normally — so the agent never loses its live task list to a fold (#639). Its full-history counterpart `compress.protectedTools` hard-excludes **every** instance of a tool — for independent-content results no later result supersedes (e.g. opencode/pi `skill` loads); protecting all instances of a chatty or cumulative-snapshot tool grows context without bound (#639), so keep it to low-frequency, high-value tools. diff --git a/README.zh-CN.md b/README.zh-CN.md index a70c5517f7..89866c04a6 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -72,7 +72,7 @@ QQ群: 可选的第六个工具 `acp_rule`(`compress.rules: true` —— 见 [CONFIGURATION.zh-CN.md](CONFIGURATION.zh-CN.md))记录**持久化的原则级提醒**:模型记录的简短规则(用户强调的教训、要求记住的行为、撞到的大坑)受硬性保护不被压缩——调用及结果在每次折叠中都保留在上下文中——省略参数则列出已记录规则([ranxianglei/billion-context-pi#433](https://github.com/ranxianglei/billion-context-pi/issues/433))。 -可选的第七个工具 `acp_retrieve`(`compress.store.enabled: true` —— 见 [CONFIGURATION.zh-CN.md](CONFIGURATION.zh-CN.md))支撑**内容寻址消息存储**(内置 CCR,#1097):超大工具结果**在到达时改为 ID 引用,而非强制蒸馏**——线上保留字节稳定的占位符,原文进入按会话、按内容哈希去重的边车存储,模型通过一次廉价工具调用按需取回。默认无损:未执行的 retrieve 只花一次调用;而被 absorb 蒸馏掉的细节则永久丢失。v1 仅代理模式、仅原生工具线(marker/文本协议没有执行 retrieve 的通道,存储在这些场景下自动解除武装,而不是静默丢失内容)。 +可选的第七个工具 `acp_retrieve`(`compress.ccr.enabled: true` —— 见 [CONFIGURATION.zh-CN.md](CONFIGURATION.zh-CN.md))支撑**内容寻址消息存储**(内置 CCR,#1097):超大工具结果**在到达时改为 ID 引用,而非强制蒸馏**——线上保留字节稳定的占位符,原文进入按会话的内容存储信封(按内容哈希去重),模型通过一次廉价工具调用按需取回。默认无损:未执行的 retrieve 只花一次调用;而被 absorb 蒸馏掉的细节则永久丢失。v1 仅代理模式、仅原生工具线(marker/文本协议没有执行 retrieve 的通道,存储在这些场景下自动解除武装,而不是静默丢失内容)。 同族的保护开关 `compress.protectedLatestTools`(见 [CONFIGURATION.zh-CN.md](CONFIGURATION.zh-CN.md))让累积型工具(客户端的 todo/任务清单,如 `["todo_list", "TodoWrite"]`)的**最新**快照永远不被压缩,旧实例照常折叠 —— agent 的活跃任务清单不会在折叠中丢失(#639)。其全历史对应项 `compress.protectedTools` 对工具的**全部实例**做硬排除 —— 适用于各次结果相互独立、后续结果不会取代旧结果的内容(如 opencode/pi 的 `skill` 加载);对高频或累积快照型工具保护全部实例会让上下文无界增长(#639),请只用于低频高价值工具。 diff --git a/src/server.ts b/src/server.ts index 29cbfb45dc..1e6558ccec 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1157,7 +1157,7 @@ async function handle( let reqSurface: PackSurface = {}; let reqSurfacePack = "default"; let wsSourceForLog: string | undefined; - // [#1097] host-only CCR policy for this request scope (three-level + // [#1097] host-only CCR policy for this request scope (three-level // merge); resolved before the session is bound, then stamped onto it below so // every view / injection / execution site reads one value. let resolvedCcrCfg: CcrSettings | undefined;