diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fff45075c..9d1e122c52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag - **Fix: the Vertex managed block no longer wraps its own value.** `bili install claude` pins `ANTHROPIC_VERTEX_BASE_URL`, and the next derivation read that value back — the settings `env` block reaches the client process, which `readClaudeSettings` falls back to — wrapped it again, and added a `/bili/anthropic/` layer per launch. The extra hop targets the proxy's own port, which the tunnel guard denies (`403 detail:"self"`), so the lane died on the second start. Both derivations (`claudeNativeVertexBaseUrl` in `src/plugin-install.ts`, `claudeVertexUpstream` in `src/launcher.ts`) now collapse any nesting of bili's own wrapper first (`stripBiliWrappers`, `src/vertex.ts`) and are idempotent on their own output; a user's own endpoint behind a wrapper is preserved intact. The same pass closes an install/remove asymmetry: `bili plugin remove claude` now takes `ANTHROPIC_VERTEX_BASE_URL` back when it is ours (a foreign endpoint is left alone, as at install). - **Claude Code on Vertex AI (`CLAUDE_CODE_USE_VERTEX=1`) is routed through bili.** Under Vertex the plain `ANTHROPIC_BASE_URL` is inert — the client dials `/projects/…:rawPredict|streamRawPredict` instead — so the launcher, the managed native install, the upstream-discovery pass and the client config all gained that key: `bili claude` now sets `ANTHROPIC_VERTEX_BASE_URL` to `/bili/anthropic//v1`, where the protocol marker is required (`:rawPredict` matches no path suffix, so the suffix ladder cannot classify it) and the `/v1` is required because the client appends its own path starting at `/projects/…`; the endpoint host is derived from `ANTHROPIC_VERTEX_BASE_URL`/`CLOUD_ML_REGION` with the region validated against a strict pattern, host and `locations/` moving together; a dated model id (`claude-opus-5-5@…`) is normalized before the window lookup. The plain key stays set and untouched (inert under Vertex); with Vertex off nothing changes and nothing is emitted. Grounded in a captured Claude Code 2.1.300 wire (`tests/fixtures/vertex-anthropic/`, `tests/vertex-wire-fixture.test.ts`) plus a lane-scoped upstream fake (`tests/wire-contract-fakes.ts` rules `WC-019`/`WC-020`/`WC-021`, `tests/vertex-fake-upstream.test.ts`). - **Anthropic Threads lane: mirror + reforge (#2926, #2884).** `thread` bodies (`create`/`continue`) hold conversation state server-side: a continue wire body is delta-only no matter how big the thread grew, so bili's history-rewrite lever does not exist on this wire and the compress machinery now stands down for these turns entirely (byte-faithful passthrough, no preflight — the wire body says nothing about the real thread size). Instead every turn is mirrored into the unified store (`session_ext` keys `threads:mirror:*`: create replaces the mirror — it is the client's authoritative snapshot — continue appends its delta; oversized entries are elided, not stored as media). When the mirror estimate crosses the window on a continue and the rollout feature `anthropicThreadsReforge` is on (default 0%; force with `rollout.features.anthropicThreadsReforge: true` or `BILI_ROLLOUT_ANTHROPIC_THREADS_REFORGE=1`), bili swaps that continue for a fresh `create` carrying [summary of the folded prefix, verbatim tail ring (last 3 user turns), this turn's delta] — the only channel that can deliver a fold to a server-side thread. The client's system/tools are copied byte-faithfully from the captured create shape (host instructions never enter the summary); one summarizer side-call rides the client's auth and the same upstream; because the message-id chain IS the thread identity, the client's next continue lands in the new thread with zero redirection. Fail-open everywhere: legacy-only storage (no unified sidecar) captures nothing and never reforges; a failed summary side-call forwards the continue untouched. The lane also claims turns before the #2648 global-scope stand-down — the stand-down used to swallow whole Claude Code threads sessions and silently disable folding (#2884). +- **Absorb distillation restored and scoped to image-bearing results by default (#3402, reverts #3309).** The `compress.absorb` block returns. New `compress.absorb.mediaOnly` (default `true`) restricts absorption to tool results that carry media (images): image bytes are not counted in text tokens, so such results are exempt from the `minToolTokens` floor and get marked `[ACP absorb]` even when small, while plain-text large results are no longer marked by default — this removes the old "always over-compresses" failure mode that motivated the original removal. Set `mediaOnly: false` to restore the pre-#3309 full-text behavior. `absorb.enabled` still defaults to `false` (the feature stays opt-in); once enabled, an absorbed result's call+result pair leaves subsequent context and its reclaimed tokens are credited against the compression budget. The plugin-lane prompt/tool gate is pinned consistent (the absorb prompt appears ⟺ the tool is advertised, both driven by the same base config block — #3293). Kernel `acp-kernel` 0.0.128 → 0.0.129 (new optional `mediaOnly` field; wire-contract goldens unchanged). +- **Absorb distillation removed (#3309).** The opt-in `compress.absorb` block (instant distillation of large tool results into short abstracts, originals to the content store) and its `absorb` tool were deleted from host and kernel. *(Backfill: #3309 shipped without a changelog line; this entry was added when the change was reverted. Superseded by the entry above.)* - **Output-budget restore never exceeds the session's proven budget (#3406 item 4).** When no output ceiling is known for a model (no runtime-info, launcher, config, or registry row) and a starved main request must be restored, the #3071 absolute floor (32768) now applies ONLY when the session has no functional high-water of its own (born-starved / fork-inherited rattle, or a high-water below the 4096 trust line). A high-water at/above the trust line is the budget the provider already accepted at full value — restoring TO it instead of raising it to 32K fixes the hard-rejection on providers that cap `maxOutputTokens` below 32768 (the raised value 400'd outright, trading a degraded-but-working session for a failing one). The #3071 protections are unchanged: fresh sessions and death-rattle decay chains (…, 2061, 252) still get the floor, a known ceiling still floors the target, and non-starved values still pass through verbatim. - **Stale-guard ownership fast path (#3049, hardened in #3406 item 2):** in shadow/full modes the per-save rollback guard no longer re-reads and re-parses the entire session envelope (the ~320 ms/turn, full-file read on >40 MiB sessions — a large slice of #3049's read amplification). The builder carries an ownership token (the two monotonic counters it just ingested); when the unified row still matches those counters and the envelope file is byte-identical to the stat stamped right after OUR write landed (same size, mtime not newer beyond sub-ms clock skew), no other writer can have committed and the guard skips the read. Cross-instance commits are still caught through the shared `index.db` row; foreign file writes (legacy sibling, manual edit) through the size/mtime stamp mismatch — now regardless of WHEN they land, closing the #3406 item-2 race where a sibling writing inside the old builtAtMs+1.5 s grace window was adopted as "ours" and clobbered on the next save. The debounce is host-owned so EVERY envelope write — the per-turn debounced save or an explicit one — gets stamped when it lands; a save whose write never landed observably (restore-replica seeds, crash before stamping) leaves the token unstamped and always takes the full read. Legacy-only installs keep the exact per-save full read. Residual (documented on `ownershipFastPath`): a foreign write landing in the microsecond gap between our awaited write resolving and the stamp's statSync can be adopted as ours for one cycle — the window shrank from 1.5 s to ~µs. - **Image pre-compression (`compress.imageCompression`) and the `image_full` tool removed (#3266).** The opt-in downscale pass and its restore channel are gone from host and kernel: no more arrival-time shrinking, no `image_full` injection, no `[Downscaled screenshots]` notes, no image stats — `sharp` is no longer an optional dependency. Rationale: the restore loop was structurally unreachable on hosts that reject bili-injected tools (codex), and where reachable the model had no reliable way to learn the right ref, producing the 38-round guess-reject-resend loop in #3266 (cache hit 75%→28%). Every image now passes through byte-identical at original resolution. Sessions persisted by older versions load unchanged; their legacy `imageShrinks`/`imageFullRestored` state keys and `imageShrunk*` stats are ignored on load (never materialize). A future redesign is tracked on the issue. diff --git a/CLIENTS.md b/CLIENTS.md index e4b43eee04..9fb654fb62 100644 --- a/CLIENTS.md +++ b/CLIENTS.md @@ -803,7 +803,7 @@ file (2.x rejects bare file paths in the config `plugin` array); **1.x** hosts get the bare file path. What the plugin does (both generations): registers the bili tools natively -in-host — compress / decompress / search_context / acp_status — +in-host — compress / decompress / search_context / acp_status (+ absorb) — and stamps the proxy headers on every outgoing provider request, including context-window / max-output read from the host's own model catalog (`ctx.catalog.model.list()`, refreshed every 60s) and reported to the proxy diff --git a/CLIENTS.zh-CN.md b/CLIENTS.zh-CN.md index 4049c49518..729a4fe007 100644 --- a/CLIENTS.zh-CN.md +++ b/CLIENTS.zh-CN.md @@ -260,7 +260,7 @@ bili 只压缩路径匹配已知 wire 协议(`/chat/completions`、`/llm_raw_cha HTTPS 走证书 MITM,HTTP 走临时 `opencode.json` 副本(`/bili/` 改写;JSONC 注释照单接受,合并方式与 opencode 自身一致;相对本地插件路径在副本里重新锚定为绝对路径 —— opencode 按声明所在配置文件目录解析,#826)。宿主代次用 `--version` 探测(探测失败默认按 1.x):**2.x** 宿主注入内置 V2 插件(`dist/agent/opencode.js`),以临时包装目录形式给出(目录入口 `index.js` 再 re-export 插件文件 —— 2.x 拒绝配置 `plugin` 数组里的裸文件路径);**1.x** 宿主直接给裸文件路径。 -插件在两代宿主上做的事相同:在宿主内原生注册 bili 工具 —— compress / decompress / search_context / acp_status—— 并在每个 provider 请求上盖章代理头,含从宿主自身模型目录(`ctx.catalog.model.list()`,每 60s 刷新)读取的 context-window / max-output,并以 runtime-info 上报给代理(#955)—— 压缩走插件模式,**不做** wire 级工具注入;原生 auto-compaction 自动关闭(`compaction.auto: false`)。所有注册都是防御式的(可选链):任一 2.x build 上接缝缺失或未触发时,插件保持惰性,会话透明回退纯代理模式而不是报错 —— 在相邻的 `dev` build 上观察到过 API 面互不相同(#754 评审探针)。 +插件在两代宿主上做的事相同:在宿主内原生注册 bili 工具 —— compress / decompress / search_context / acp_status(另加 absorb)—— 并在每个 provider 请求上盖章代理头,含从宿主自身模型目录(`ctx.catalog.model.list()`,每 60s 刷新)读取的 context-window / max-output,并以 runtime-info 上报给代理(#955)—— 压缩走插件模式,**不做** wire 级工具注入;原生 auto-compaction 自动关闭(`compaction.auto: false`)。所有注册都是防御式的(可选链):任一 2.x build 上接缝缺失或未触发时,插件保持惰性,会话透明回退纯代理模式而不是报错 —— 在相邻的 `dev` build 上观察到过 API 面互不相同(#754 评审探针)。 1.x 细节(1.14.46 + 1.18.31 验证):V1 `.server()` 钩子在进程内把每个 provider 的 `options.baseURL` 改写为 `/bili/…` 并设 `compaction.auto: false`;`chat.headers` 每次请求盖章插件头;`tool` 用真实 zod 形状注册 bili 工具(zod 是运行时依赖 —— 解析不到时降级为只改写)。没有显式 `baseURL` 的 provider(SDK 默认值,如裸 `@ai-sdk/openai` → api.openai.com)由全局 `fetch` 补丁兜住(日志:`v1: fetch patch installed`)—— 幂等,`/bili/` 包装过的 URL 原样直通;含 OpenAI Responses 端点端到端验证。 diff --git a/CONFIGURATION.md b/CONFIGURATION.md index e00ef3ae8b..e1d1693674 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -249,10 +249,11 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `compress.neverPreserveRecentTools` | string[] ([] valid) | ["decompress", "search_context", "read", "bash"] (kernel) | — | Excluded from the recent protection zone (immediately compressible); an empty array excludes nothing (maximum protection). | | `compress.preserveRecentTools` | string[] | n/a (subtraction form) | — | Subtraction form: recent-zone tools minus this list get full protection; an empty array here is rejected as a typo. | | `compress.visibilityMarkers` | boolean | true | — | Append visibility markers after compress/decompress/search_context/acp_status results; off stops imitated narration. | -| `compress.rules` | boolean | false | — | Persistent model reminders delivered through an injected acp_rule tool, hard-protected from folds; pi/omp expose an /acp-rule command. | +| `compress.rules` | boolean | false | — | Persistent model reminders delivered through an injected acp_rule tool, hard-protected from folds; pi/omp expose an /acp-rule command. Proxy-mode only — inert in plugin mode, where the host owns the tool surface. | | `compress.injectTool` | boolean | true | ACP_COMPRESS_TOOL | Register the acp_compress tool for clients (global level only). | | `compress.injectNudge` | boolean | true | ACP_COMPRESS_NUDGE | Send growth nudges as the window fills (global level only). | | `compress.reasoning` | { drop?, threshold? } | drop true · threshold 2048 | — | Drop finished-round reasoning runs longer than 2048 chars (drop: true by default); strict-reasoning upstreams need drop:false. | +| `compress.absorb` | object | opt-in (disabled) | — | Opt-in instant distillation of tool results into short abstracts; originals go to the content store. By default only image-bearing results qualify. | | `compress.ccr` | object | enabled in proxy mode since v2 | — | Content cache & retrieve: large outputs stored losslessly beside the session, replaced by head excerpt + pointer fetched back via acp_retrieve; no cap or eviction. | | `compress.search` | object | off | — | Search-behavior settings for search_context; currently holds planAware re-ranking. | | `compress.search.planAware` | boolean | false | — | When on, search_context candidates are re-ranked against current plan state; off keeps byte-identical lexical results. | @@ -261,6 +262,12 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `compress.outputSteering` | object { enabled?, verbosityLevel?, effortRouting? } | enabled false · verbosityLevel 2 | — | Appends a conciseness directive to the system-prompt tail; clamps mechanical continuation requests. | | `compress.priceProfile` | { w?, r?, q? } | unset (registry, kernel fallback {1, 0.1, 4}) | — | w/r/q ratios normalized to the input price (write/cache-read/output); report-only — never affects triggers or the wire. | | `compress.acknowledgePromptsRisk` | boolean | false | — | Must be true before custom prompts take effect. | +| `compress.absorb.enabled` | boolean | false | — | Enable the absorb distillation block. | +| `compress.absorb.minToolTokens` | number | 1000 | — | Minimum estimated token size of a tool result eligible for absorption. | +| `compress.absorb.contextThresholdPct` | number \| % | unset | — | Only absorb when context usage passes this share of the window. | +| `compress.absorb.excludeTools` | string[] | [] | — | Tool names excluded from absorption. | +| `compress.absorb.toolName` | string | "absorb" | — | Registered name of the absorb tool. | +| `compress.absorb.mediaOnly` | boolean | true | — | Restrict absorption to tool results that carry media (images). Image bytes are exempt from minToolTokens since they are not counted in text. Set false to restore legacy full-text absorption of any large result. | | `compress.ccr.enabled` | boolean | true (proxy mode) | — | Enable the content-cache-and-retrieve block. | | `compress.ccr.minToolTokens` | number | ≈4000 | — | Minimum estimated token size of a tool result eligible for CCR storage. | | `compress.ccr.excludeTools` | string[] | [] | — | Tool names excluded from CCR storage. | @@ -1346,12 +1353,25 @@ For each request, the proxy resolves the settings by longest-URL-prefix match (t - **Status:** ACTIVE - **Description:** Controls **fold-state reconciliation** (#1921): what happens when a client resends history whose *content* has drifted from what was compressed — e.g. a fork re-serialized earlier turns (#1908), a client rewriting message bytes in place, or a provider normalizing whitespace. By default message identity is a content hash, so a one-byte churn silently drops the affected messages out of every compression block and the raw originals re-enter the wire ("fold drift"). In `"repair"` mode the proxy classifies the drift per message before the fold runs: stable tool-call anchors (`tool_use_id`) and normalized-identity anchors (NFC text, CRLF/whitespace-collapsed) re-anchor compression blocks onto the new message ids, so the fold survives client-side churn byte-for-byte-equivalent to a clean resend; genuinely *edited* messages (normalized text differs, or length drifts beyond `max(256, 25%)`) are left to re-enter the wire unfolded — honest, never a silent rewrite — and the next compression pass re-folds them. `"warn"` runs the same classifier but only logs what it found; `"off"` disables the layer entirely (pre-#1921 behavior). The environment variable `BILI_FOLD_RECONCILE` overrides the configured value for one process. Anchor metadata is persisted per session (`foldAnchors` in the session record, capped at 16384 entries) and covers the messages each block actually references. A one-line `info` is logged when the system prompt changes without any message churn (system-only drift never touches fold state). +#### `absorb` + +- **Type:** `object` (`{ enabled?, minToolTokens?, contextThresholdPct?, excludeTools?, toolName? }`) +- **Default:** *(disabled — the feature is off unless you set `enabled: true`)* +- **Status:** ACTIVE +- **Description:** Opt-in **instant tool-result compression** (issue #605, via the `acp-kernel` absorb API). When enabled, large tool results get a forced `[ACP absorb]` instruction at result time; the model distills the result into a compact summary via the `absorb` tool, and the original tool-call/tool-result pair is hidden from the wire from the next turn on — keeping mid-session pressure lower between fold rounds. Sub-fields (merged deepest-wins like every other CompressSettings field): + - `enabled: boolean` — master switch; anything other than `true` keeps the feature fully off (no tool, no prompt, no markers). + - `minToolTokens: number` — only results at or above this many tokens are prompted (kernel default 1000). + - `contextThresholdPct: number|percent-string` — only prompt once usage reaches this fraction of `modelContextLimit` (`0` = size gate alone; `"75%"` is accepted). + - `excludeTools: string[]` — tool-name patterns never absorbed. **Known limitation:** a no-op on tool *results* until [ranxianglei/acp-kernel#213](https://github.com/ranxianglei/acp-kernel/issues/213) ships (wire projections don't carry `toolName` on results, so the kernel's name guard can't fire). + - `toolName: string` — rename the injected tool (default `"absorb"`); the advertised/injected schema, the system-prompt section and per-session adjudication all follow the name in both lanes. **Lane governance (#1359):** plugin mode governs the *entire* `absorb` block by the **base** config, so a renamed tool is advertised under that name and executed under it (they can never diverge); provider/model-level `absorb.*` overrides are **proxy-lane-only** (the proxy injects and adjudicates the merged name). A load-time warning lists any provider/model `absorb.*` field whose value diverges from base. + Injection follows the wire's native-tool surface: proxy mode injects the tool (under the per-request resolved name) + a system-prompt section on the anthropic/openai/responses native-tools wires, plugin mode advertises it in the plugin manifest **only while enabled** (hosts register the manifest's tools verbatim — advertising a disabled tool breaks them, #1192; 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). + #### `ccr` - **Type:** `object` (`{ enabled?, minToolTokens?, excludeTools?, toolName?, maxHeadChars? }`) - **Default:** *(on in proxy mode since v2 (#1179) — unset resolves to `enabled: true`; an explicit `enabled: false` at any level opts out. Plugin lanes stay opt-in: they arm only on an explicit global `enabled: true`, #1271/#1273.)* - **Status:** ACTIVE (v2 — proxy mode by default; plugin mode on the anthropic + openai wires when explicitly enabled) -- **Description:** **Content-addressed message store** / built-in CCR (issue #1097/#1179, via the `acp-kernel` CCR API, acp-kernel >= 0.0.84). Instead of carrying oversized tool results 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. Sub-fields (merged deepest-wins like every other CompressSettings field): +- **Description:** **Content-addressed message store** / built-in CCR (issue #1097/#1179, 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. **Defaults to `true`** when unset at every level (#1179); an explicit `false` at any level wins and keeps the feature fully off (no placeholders, no tool). Plugin-mode arming additionally requires an explicit global `true` (#1273) — the manifest only advertises what the operator explicitly enabled. - `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). @@ -1373,7 +1393,7 @@ For each request, the proxy resolves the settings by longest-URL-prefix match (t - **Type:** `boolean` - **Default:** `false` - **Status:** ACTIVE -- **Description:** **Persistent model reminders** (issue [ranxianglei/billion-context-pi#433](https://github.com/ranxianglei/billion-context-pi/issues/433), via the `acp-kernel` rules API), **off by default** — set `compress.rules: true` to enable it (the #1399 decision: once enabled the model has full rights over session rules and may call it unprompted, including delete/clear). When enabled, an `acp_rule` tool is injected alongside the ACP tools: calling it with a short `rule` argument records a principle-level reminder that is **hard-protected** from compression (the call + result stay in context across every fold), and omitting the argument lists the recorded rules. Passing `delete` with a rule id (e.g. `"rule3"`) removes that one rule (`Removed ruleN: `); passing `clear: true` removes every rule. The two are mutually exclusive with each other and with `rule` — one operation per call — so a stale or accidentally-recorded rule can be deleted instead of hedged with a counter-rule. Guidance on *when* to record (user-emphasized lessons, behaviors the user asks to remember, major pitfalls hit) lives entirely in the tool description — nothing is added to the system prompt. Kernel limits apply (50 rules × 300 chars each); duplicate text is rejected pointing at the existing id. Injection follows the wire's native-tool surface: proxy mode injects the tool on the anthropic/openai/responses native-tools wires, plugin mode advertises it in the plugin manifest **only while enabled** (hosts register the manifest's tools verbatim — advertising a disabled tool breaks them, #1192; execution gated per session). Recorded rules persist across restarts in the session state; removals persist the same way. Requires `acp-kernel` >= 0.0.70 (>= 0.0.84 for delete/clear). Humans get the full operation set without going through the model: pi/omp expose a native `/acp-rule` command with the same semantics as the tool — bare `/acp-rule` lists every recorded rule, `/acp-rule ` records one directly, `/acp-rule remove ` deletes one, and bare `/acp-rule clear` wipes all recorded rules (`clear ` records instead of wiping) (#1251; remove/clear added in #1399). +- **Description:** **Persistent model reminders** (issue [ranxianglei/billion-context-pi#433](https://github.com/ranxianglei/billion-context-pi/issues/433), via the `acp-kernel` rules API), **off by default** — set `compress.rules: true` to enable it (the #1399 decision: once enabled the model has full rights over session rules and may call it unprompted, including delete/clear). When enabled, an `acp_rule` tool is injected alongside the ACP tools: calling it with a short `rule` argument records a principle-level reminder that is **hard-protected** from compression (the call + result stay in context across every fold), and omitting the argument lists the recorded rules. Passing `delete` with a rule id (e.g. `"rule3"`) removes that one rule (`Removed ruleN: `); passing `clear: true` removes every rule. The two are mutually exclusive with each other and with `rule` — one operation per call — so a stale or accidentally-recorded rule can be deleted instead of hedged with a counter-rule. Guidance on *when* to record (user-emphasized lessons, behaviors the user asks to remember, major pitfalls hit) lives entirely in the tool description — nothing is added to the system prompt. Kernel limits apply (50 rules × 300 chars each); duplicate text is rejected pointing at the existing id. Injection follows the wire's native-tool surface exactly like `absorb`: proxy mode injects the tool on the anthropic/openai/responses native-tools wires, plugin mode advertises it in the plugin manifest **only while enabled** (hosts register the manifest's tools verbatim — advertising a disabled tool breaks them, #1192; execution gated per session). Recorded rules persist across restarts in the session state; removals persist the same way. Requires `acp-kernel` >= 0.0.70 (>= 0.0.84 for delete/clear). Humans get the full operation set without going through the model: pi/omp expose a native `/acp-rule` command with the same semantics as the tool — bare `/acp-rule` lists every recorded rule, `/acp-rule ` records one directly, `/acp-rule remove ` deletes one, and bare `/acp-rule clear` wipes all recorded rules (`clear ` records instead of wiping) (#1251; remove/clear added in #1399). #### `reasoning` diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 0746e8f46f..bef35fc1e8 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -249,10 +249,11 @@ | `compress.neverPreserveRecentTools` | string[] ([] valid) | ["decompress", "search_context", "read", "bash"] (kernel) | — | 从近期保护区排除(立即可压);空数组合法=不排除任何工具(最大保护)。 | | `compress.preserveRecentTools` | string[] | n/a (subtraction form) | — | 减法形式:近期区工具减去本列表得到完全保护;此处空数组按笔误拒绝。 | | `compress.visibilityMarkers` | boolean | true | — | 在 compress/decompress/search_context/acp_status 结果后追加可见性标记;关闭可抑制模型模仿叙述。 | -| `compress.rules` | boolean | false | — | 通过注入的 acp_rule 工具提供持久模型提醒——对折叠硬保护;pi/omp 提供 /acp-rule 命令。 | +| `compress.rules` | boolean | false | — | 通过注入的 acp_rule 工具提供持久模型提醒——对折叠硬保护;pi/omp 提供 /acp-rule 命令。仅代理模式生效——插件模式下宿主自管工具面,此项无效。 | | `compress.injectTool` | boolean | true | ACP_COMPRESS_TOOL | 向客户端注册 acp_compress 工具(仅全局层级生效)。 | | `compress.injectNudge` | boolean | true | ACP_COMPRESS_NUDGE | 窗口填满过程中发送增长提醒(仅全局层级生效)。 | | `compress.reasoning` | { drop?, threshold? } | drop true · threshold 2048 | — | 丢弃超过 2048 字符的已结束轮次推理块(drop 默认 true);严格推理上游需设 drop:false。 | +| `compress.absorb` | object | opt-in (disabled) | — | 可选即时蒸馏:把工具结果蒸馏成短摘要,原文进内容库。默认仅图片类结果参与。 | | `compress.ccr` | object | enabled in proxy mode since v2 | — | 内容缓存与回取:大输出无损存到会话旁、替换为首段摘录+指针,模型用 acp_retrieve 取回原文;无上限、永不清除。 | | `compress.search` | object | off | — | search_context 的检索行为设置;当前含 planAware 重排。 | | `compress.search.planAware` | boolean | false | — | 开启后 search_context 候选按当前计划状态重排;关闭时结果逐字节不变。 | @@ -261,6 +262,12 @@ | `compress.outputSteering` | object { enabled?, verbosityLevel?, effortRouting? } | enabled false · verbosityLevel 2 | — | 向系统提示词尾部附加简洁度指令;只收紧机械延续请求。 | | `compress.priceProfile` | { w?, r?, q? } | unset (registry, kernel fallback {1, 0.1, 4}) | — | w/r/q 相对输入价的比率(写入/缓存读/输出);仅供报告,不影响触发条件或线上行为。 | | `compress.acknowledgePromptsRisk` | boolean | false | — | 必须先置 true,自定义 prompts 才会生效。 | +| `compress.absorb.enabled` | boolean | false | — | 启用 absorb 蒸馏块。 | +| `compress.absorb.minToolTokens` | number | 1000 | — | 可被吸收的工具结果的最小估算 token 大小。 | +| `compress.absorb.contextThresholdPct` | number \| % | unset | — | 仅当上下文使用率超过窗口该占比时才吸收。 | +| `compress.absorb.excludeTools` | string[] | [] | — | 排除出吸收范围的工具名。 | +| `compress.absorb.toolName` | string | "absorb" | — | absorb 工具的注册名。 | +| `compress.absorb.mediaOnly` | boolean | true | — | 仅吸收携带媒体(图片)的工具结果;图片字节不计入文本 token,故豁免 minToolTokens 门槛。置 false 恢复对任意大结果的全文吸收(旧行为)。 | | `compress.ccr.enabled` | boolean | true (proxy mode) | — | 启用内容缓存与回取块。 | | `compress.ccr.minToolTokens` | number | ≈4000 | — | 可进入 CCR 存储的工具结果的最小估算 token 大小。 | | `compress.ccr.excludeTools` | string[] | [] | — | 排除出 CCR 存储的工具名。 | @@ -1351,12 +1358,25 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 - **状态:** ACTIVE - **说明:** 控制**折叠状态和解**(#1921):当客户端重发的历史与已压缩内容发生漂移时怎么处理 —— 例如 fork 重新序列化了早前轮次(#1908)、客户端原地改写消息字节、或 provider 规范化空白。默认情况下消息身份是内容哈希,一字节漂移就会让受影响消息从所有压缩块中静默脱落,原文重回线上(“折叠漂移”)。`"repair"` 模式在折叠运行前逐条分类漂移:稳定的工具调用锚(`tool_use_id`)与规范化身份锚(NFC 文本、CRLF/空白折叠)把压缩块重锚到新消息 id 上,折叠在客户端漂移下存活、字节等价于一次干净重发;真正被**编辑**的消息(规范化文本不同,或长度漂移超过 `max(256, 25%)`)则诚实地重回线上(绝不静默改写)——由下一轮压缩重新折叠。`"warn"` 运行同一分类器但只记录发现;`"off"` 完全关闭该层(#1921 之前的行为)。环境变量 `BILI_FOLD_RECONCILE` 可为单个进程覆盖配置值。锚元数据按会话持久化(会话记录中的 `foldAnchors`,上限 16384 条),覆盖每个块实际引用的消息。系统提示词在无消息漂移时变化会记录一行 `info`(system-only 漂移永不影响折叠状态)。 +#### `absorb` + +- **类型:** `object`(`{ enabled?, minToolTokens?, contextThresholdPct?, excludeTools?, toolName? }`) +- **默认值:** *(禁用 — 除非显式设置 `enabled: true`,该特性完全关闭)* +- **状态:** ACTIVE +- **说明:** 可选开启的**即时工具结果压缩**(issue #605,经由 `acp-kernel` absorb API)。启用时,大工具结果在到达即被附带强制的 `[ACP absorb]` 指令;模型通过 `absorb` 工具将结果蒸馏为紧凑摘要,原 tool-call/tool-result 配对从下一轮起在线上隐藏 —— 使折叠轮之间的中间会话压力更低。子字段(按字段最深层级胜出,与其他 CompressSettings 字段一致): + - `enabled: boolean` — 主开关;任何值不为 `true` 时特性完全关闭(无工具、无提示、无标记)。 + - `minToolTokens: number` — 仅达到此 token 数的结果被附带提示(内核默认 1000)。 + - `contextThresholdPct: number|percent-string` — 仅当用量达到 `modelContextLimit` 的此比例时附带提示(`0` = 仅尺寸门槛;`"75%"` 接受)。 + - `excludeTools: string[]` — 永不吸收的工具名模式。**已知限制:对工具*结果*目前无效,直到 [ranxianglei/acp-kernel#213](https://github.com/ranxianglei/acp-kernel/issues/213) 修复发布**(wire 投影不把 `toolName` 携带在结果上,内核名称守卫无法命中)。 + - `toolName: string` — 重命名注入工具(默认 `"absorb"`);声明/注入的模式、系统提示段与按会话裁决在两条车道中都跟随该名称。**车道治理(#1359):** 插件模式用**基础**配置治理整个 `absorb` 块,因此重命名后的工具既以该名声明、也以该名执行(二者永不背离);provider/model 层的 `absorb.*` 覆盖**仅限代理车道**(代理注入并裁决合并后的名称)。加载时会输出一条警告,列出任何取值与基础值不同的 provider/model `absorb.*` 字段。 + 注入跟随线上原生工具面:代理模式在 anthropic/openai/responses 原生工具线上注入工具(按每请求解析的名称)+ 系统提示段,插件模式在插件清单中广告它(MCP shell 自动拾取)。Responses **marker/文本协议**路由不支持(无原生工具面 — 强制的 absorb 指令不可满足),标题生成请求(`max_tokens ≤ 200`)跳过注入如压缩提示一样。吸收配对在重启后保持隐藏(在会话状态持久化)。 + #### `ccr` - **类型:** `object`(`{ enabled?, minToolTokens?, excludeTools?, toolName?, maxHeadChars? }`) - **默认值:** *(全车道默认关闭(#1207 决策)— 未设置时保持关闭;在任意层级显式设置 `enabled: true` 方可启用(建议先本地验证)。插件通道同样需全局 `enabled: true` 才武装(#1271/#1273))* - **状态:** ACTIVE(v2 — 全车道 opt-in;插件通道限 anthropic + openai wire,显式开启后生效) -- **说明:** **内容寻址消息存储**/内置 CCR(issue #1097/#1179,经 `acp-kernel` CCR API,需 acp-kernel >= 0.0.84)。超大工具结果不再永远挂在线上: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 只花一次廉价工具调用。子字段(按字段最深层级胜出,与其他 CompressSettings 字段一致): +- **说明:** **内容寻址消息存储**/内置 CCR(issue #1097/#1179,经 `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`**(#1179);任意层级显式 `false` 优先,特性完全关闭(无占位符、无工具)。插件模式武装还需全局显式 `true`(#1273)——插件清单只广播运维者显式开启的能力。 - `minToolTokens: number` — 仅达到此 token 数的工具结果被 ID 引用(kernel 默认 `4000`);更小的结果保持原样。 - `excludeTools: string[]` — 从不存储的工具名模式(允许 glob 后缀;kernel 默认为空)。 @@ -1378,7 +1398,7 @@ ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报 - **类型:** `boolean` - **默认值:** `false` - **状态:** ACTIVE -- **说明:** **持久化模型提醒**(issue [ranxianglei/billion-context-pi#433](https://github.com/ranxianglei/billion-context-pi/issues/433),经由 `acp-kernel` rules API),**默认关闭**——设 `compress.rules: true` 显式开启(#1399 定案:开启后模型对会话规则有全权,可未经提示自主调用,含 delete/clear)。启用后,一个 `acp_rule` 工具随 ACP 工具一同注入:传入简短的 `rule` 参数调用可记录一条**原则级提醒**,该提醒受**硬性保护**免于压缩(调用及结果在每次折叠中都保留在上下文中);省略参数则列出已记录的规则。传入 `delete`(规则 id,如 `"rule3"`)删除该条规则(返回 `Removed ruleN: `);传入 `clear: true` 清空全部规则。两者互斥,且均与 `rule` 互斥——一次调用只能执行一种操作——因此过时或误记的规则可以直接删除,而无需再写一条"作废声明"对冲。关于*何时*记录(用户强调的教训、用户要求记住的行为、遇到的重大陷阱)的指导完全写在工具描述里——不向系统提示词添加任何内容。受内核限制约束(50 条规则 × 每条 300 字符);重复文本会被拒绝并指向已有的 id。注入跟随线上原生工具面:代理模式在 anthropic/openai/responses 原生工具线上注入该工具,插件模式在插件清单中声明(执行按会话门控)。已记录的规则在会话状态中持久化,跨重启保留;删除同样持久化。要求 `acp-kernel` >= 0.0.70(delete/clear 需要 >= 0.0.84)。人也可以不经过模型执行全部操作:pi/omp 提供原生 `/acp-rule` 命令,语义与工具一致 —— 裸 `/acp-rule` 列出全部已记录规则,`/acp-rule <文本>` 直接记录一条,`/acp-rule remove ` 删除一条,裸 `/acp-rule clear` 清空全部(`clear <文本>` 是记录而非清空)(#1251;remove/clear 由 #1399 补齐)。 +- **说明:** **持久化模型提醒**(issue [ranxianglei/billion-context-pi#433](https://github.com/ranxianglei/billion-context-pi/issues/433),经由 `acp-kernel` rules API),**默认关闭**——设 `compress.rules: true` 显式开启(#1399 定案:开启后模型对会话规则有全权,可未经提示自主调用,含 delete/clear)。启用后,一个 `acp_rule` 工具随 ACP 工具一同注入:传入简短的 `rule` 参数调用可记录一条**原则级提醒**,该提醒受**硬性保护**免于压缩(调用及结果在每次折叠中都保留在上下文中);省略参数则列出已记录的规则。传入 `delete`(规则 id,如 `"rule3"`)删除该条规则(返回 `Removed ruleN: `);传入 `clear: true` 清空全部规则。两者互斥,且均与 `rule` 互斥——一次调用只能执行一种操作——因此过时或误记的规则可以直接删除,而无需再写一条"作废声明"对冲。关于*何时*记录(用户强调的教训、用户要求记住的行为、遇到的重大陷阱)的指导完全写在工具描述里——不向系统提示词添加任何内容。受内核限制约束(50 条规则 × 每条 300 字符);重复文本会被拒绝并指向已有的 id。注入跟随线上原生工具面,与 `absorb` 完全一致:代理模式在 anthropic/openai/responses 原生工具线上注入该工具,插件模式在插件清单中声明(执行按会话门控)。已记录的规则在会话状态中持久化,跨重启保留;删除同样持久化。要求 `acp-kernel` >= 0.0.70(delete/clear 需要 >= 0.0.84)。人也可以不经过模型执行全部操作:pi/omp 提供原生 `/acp-rule` 命令,语义与工具一致 —— 裸 `/acp-rule` 列出全部已记录规则,`/acp-rule <文本>` 直接记录一条,`/acp-rule remove ` 删除一条,裸 `/acp-rule clear` 清空全部(`clear <文本>` 是记录而非清空)(#1251;remove/clear 由 #1399 补齐)。 #### `reasoning` diff --git a/PLUGIN.md b/PLUGIN.md index 2fadda12d1..25edffade9 100644 --- a/PLUGIN.md +++ b/PLUGIN.md @@ -53,7 +53,7 @@ Fetch once at plugin startup. } ``` -Register the tools natively with your agent, in whichever wire format your agent speaks — register exactly what `toolNames` lists. `acp_rule` is advertised only when the proxy's config enables it (`compress.rules: true`, opt-in). Calling a tool that is known but disabled answers `200` with an explanatory `result` instead of an error (#1192). If `protocolVersion` is higher than you know, still register the tools — extra fields in schemas are ignored by agents. +Register the tools natively with your agent, in whichever wire format your agent speaks — register exactly what `toolNames` lists. `absorb` appears only when the proxy's config enables it; `acp_rule` is advertised only when the proxy's config enables it (`compress.rules: true`, opt-in). Calling a tool that is known but disabled answers `200` with an explanatory `result` instead of an error (#1192). If `protocolVersion` is higher than you know, still register the tools — extra fields in schemas are ignored by agents. ### 2. Request headers @@ -98,7 +98,7 @@ Notes: - Execution happens **under the session lock**, against the same view the model was shown on the last request (refs match what the model sees). - Optional `expectedRevision` is the opaque `parentRevision` from a snapshot. It requires an explicit `conversationId` and is checked under the same session lock immediately before execution. A stale revision returns `409 PARENT_REVISION_CONFLICT` without executing the tool. This also avoids witness routing to another conversation. Existing callers omitting it retain their routing and execution behavior. - `compress` mutates state. Inline `decompress` marks a block restored for re-folding; range restores queue content for the next request. Successful restores also update the estimated context footprint. `search_context` / `acp_status` are read-only. -- Errors: `400` invalid JSON / missing `conversationId` / unknown tool, `404` unknown conversation (no model request has arrived with that conversation id yet), `500` execution failure. A **known but disabled** opt-in tool (`acp_rule`) is not an error: it answers `200` with `ok: true` and a `result` explaining that the feature is off on this proxy (#1192). +- Errors: `400` invalid JSON / missing `conversationId` / unknown tool, `404` unknown conversation (no model request has arrived with that conversation id yet), `500` execution failure. A **known but disabled** opt-in tool (`absorb` / `acp_rule`) is not an error: it answers `200` with `ok: true` and a `result` explaining that the feature is off on this proxy (#1192). ### 4. `GET /__bili/plugin/status?conversationId=` diff --git a/TECHNICAL-NOTES.md b/TECHNICAL-NOTES.md index 7bace5a754..78f7909dc0 100644 --- a/TECHNICAL-NOTES.md +++ b/TECHNICAL-NOTES.md @@ -345,7 +345,7 @@ bypass and side lanes never earn credit): compression reduction. Remedy: **degrade to proxy mode** — the single `pluginMode` flip (`pluginAgent !== undefined && !pluginLaneDegraded(session)`) re-injects the ACP tools wire-side, re-arms the compress rewriter, CCR - stamping, and **keeps the nudge** (the tools are back). When wire + stamping and absorb, and **keeps the nudge** (the tools are back). When wire injection is unavailable (`compress.injectTool=false` → `degradeAvailable` false) the remedy falls back to **suppress-nudge**. Recovery: the live plugin header (`pluginLaneRestore`, hoisted BEFORE the `pluginMode` flip so @@ -403,7 +403,7 @@ functions, exit audits): reasoning-echo strip — the thinking channel is no longer cleaned. 2. **Model prose is manageable.** Render-tag echoes, marker lines, bili-internal text, degenerate open/close residue, and (when - instructed) whole tool-call emissions are stripped from the visible + absorb-instructed) whole tool-call emissions are stripped from the visible text channel only. 3. **Model output bound for files is byte-identical (#1039).** Tool-call arguments never enter any filter, whatever bytes they carry. diff --git a/TECHNICAL-NOTES.zh-CN.md b/TECHNICAL-NOTES.zh-CN.md index bf55388326..7293c350b6 100644 --- a/TECHNICAL-NOTES.zh-CN.md +++ b/TECHNICAL-NOTES.zh-CN.md @@ -102,7 +102,7 @@ bili 永不拥有用户数据:每个被启动的客户端都跑在**真实 home* 三种故障形态共享 `src/session-self-heal.ts` 里的同一族检测器,每请求评估一次(prepare 之后,仅有 nudge 的主车道;旁路/侧请求车道不积累信用): -- **D2 僵尸插件车道** —— 插件已卸载(或 MCP 子进程已死)但已开着的客户端窗口仍带着插件绑定(粘滞 `metadata.pluginAgent`)。信号:连续 5 轮(`SELF_HEAL_WINDOW`)被 nudge、**无** `x-bili-plugin` 头、线上**无** bili 工具声明、**零**压缩缩减。补救:**降级为代理模式** —— 单点 `pluginMode` 翻转(`pluginAgent !== undefined && !pluginLaneDegraded(session)`)线上回注 ACP 工具、重新接管压缩 rewriter/CCR 盖章,并**保留 nudge**(工具回来了)。线上注入不可用时(`compress.injectTool=false` → `degradeAvailable=false`)退为**静默 nudge**。恢复:**活的**插件头(`pluginLaneRestore`,在 `pluginMode` 翻转**之前**调用,首个带头请求即恢复 plugin 模式 —— 恢复信号绝不能用粘滞 `pluginAgent`,否则降级一轮后即被自己清掉)。绑定保留、永不抹除。 +- **D2 僵尸插件车道** —— 插件已卸载(或 MCP 子进程已死)但已开着的客户端窗口仍带着插件绑定(粘滞 `metadata.pluginAgent`)。信号:连续 5 轮(`SELF_HEAL_WINDOW`)被 nudge、**无** `x-bili-plugin` 头、线上**无** bili 工具声明、**零**压缩缩减。补救:**降级为代理模式** —— 单点 `pluginMode` 翻转(`pluginAgent !== undefined && !pluginLaneDegraded(session)`)线上回注 ACP 工具、重新接管压缩 rewriter/CCR 盖章/absorb,并**保留 nudge**(工具回来了)。线上注入不可用时(`compress.injectTool=false` → `degradeAvailable=false`)退为**静默 nudge**。恢复:**活的**插件头(`pluginLaneRestore`,在 `pluginMode` 翻转**之前**调用,首个带头请求即恢复 plugin 模式 —— 恢复信号绝不能用粘滞 `pluginAgent`,否则降级一轮后即被自己清掉)。绑定保留、永不抹除。 - **D1 nudge 空转**(非插件泛化)—— 连续 5 轮被 nudge 却零缩减、零 bili 工具调用。补救:在全部四处 `willInjectNudge` 门加 `&& !nudgeSuppressed(session)`(覆盖 emergency 路径)。恢复:任一次压缩缩减或 bili 工具调用即解除——或者在 `SELF_HEAL_SUPPRESS_CAP_MS`(10 分钟)内两者皆无时,抑制到时自解并重新武装(#3004/#3008:上下文单调增长时永远产生不了恢复证据,纯证据式解除曾把建议通道锁死一个多小时而用量停在 ~75%)。重新武装是有界的:再空转完整一个 `SELF_HEAL_WINDOW`(5 轮)才会再次抑制;决策梯的 fallback 逃逸在抑制期间仍可打出一次性 legacy 建议(`consumeFallback` 重置计数)。 - **D3 网关杀摘要** —— **非流式** preflight 摘要以 HTTP 524/504(Cloudflare 类)死亡时不再原地重试(旧的 transient 重试对着死网关每次烧 ~100 秒);会话在**首次**命中即学习 `metadata.preflightStreamSummary = true` 并改用 SSE 重取摘要 —— 与手动 `compress.streamSummary` 旋钮(#2133)同一学习存储,自动武装。级联双向被尊重:任一级显式 `streamSummary: false`(global → provider → model)即操作员 opt-out —— 两条学习路径都不武装、已学习的旧标志被忽略(`PreflightDeps.streamSummaryOff`)。 @@ -113,7 +113,7 @@ bili 永不拥有用户数据:每个被启动的客户端都跑在**真实 home* 一个 PR 把三条整治腿合并(refs-run 残留 #2023、退化闭名剥离+出口审计网 #2190、emission 整段丢弃+签名思考 verbatim #2066),并统一受四条硬规则约束 —— 覆盖**所有**文本改写面(loop 适配器、插件直通、JSON strip 函数、出口审计): 1. **思考通道逐字节不动。** Anthropic `thinking`、Google `thought`、OpenAI `reasoning_content`/`reasoning`、Responses reasoning summaries 原样透传 —— 哪怕长得像标签回声。理由:reasoning 回放(DeepSeek 式)与签名校验验证的就是这些字节,任何改写都会让它们失步并卡死会话(#1960/KDD#10)。适配器侧走 `createIdentityStreamFilter`,strip 函数(`stripOpenaiChatText`、`stripResponsesText`)同样不动它们(纵深防御)。**注意:这翻转了 #1881 时代的 reasoning 回声剥离 —— 思考通道不再清洗。** -2. **模型的正文输出可以治理。** 渲染标签回声、marker 行、bili 内部文本、退化开/闭名残骸,以及(受指令时)整段工具调用 emission,只在可见文本通道剥离。 +2. **模型的正文输出可以治理。** 渲染标签回声、marker 行、bili 内部文本、退化开/闭名残骸,以及(absorb 受指令时)整段工具调用 emission,只在可见文本通道剥离。 3. **模型写入文件的内容逐字节不变(#1039)。** 工具调用 arguments 无论携带什么字节都不进任何过滤器。 4. **用户发送的消息永不被改写。** 用户自己片段的逐字回声在武装清洗下也保留(#463 豁免,按请求文本匹配,不按形状)。 diff --git a/kernel/package-lock.json b/kernel/package-lock.json index f57461d1fa..6b1cfc297d 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.128", + "version": "0.0.129", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.128", + "version": "0.0.129", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/kernel/package.json b/kernel/package.json index f927d49acf..71da725e34 100644 --- a/kernel/package.json +++ b/kernel/package.json @@ -1,6 +1,6 @@ { "name": "acp-kernel", - "version": "0.0.128", + "version": "0.0.129", "description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.", "license": "MIT", "author": "ranxianglei", diff --git a/kernel/src/absorb.ts b/kernel/src/absorb.ts index 0865425599..b1365b3972 100644 --- a/kernel/src/absorb.ts +++ b/kernel/src/absorb.ts @@ -4,6 +4,7 @@ import { isStoredPlaceholderText } from "./ccr.js"; import { collectLatestProtected, collectProtectedToolCallIds, + hasMediaPayload, isMessageLatestProtected, isMessageProtectedWithPairing, matchToolPattern, @@ -108,6 +109,7 @@ export function isAbsorbCandidate( for (const pattern of cfg.excludeTools) { if (msg.toolName && matchToolPattern(msg.toolName, pattern)) return false; } + if (cfg.mediaOnly && !hasMediaPayload(msg)) return false; return true; } @@ -170,7 +172,10 @@ export function appendAbsorbPrompts( const text = msg.text ?? ""; if (text.includes(ABSORB_PROMPT_MARKER)) return msg; const tokens = countTokens(text); - if (tokens < cfg.minToolTokens) return msg; + // #3402: image bytes are not in msg.text, so countTokens(text) never reaches + // minToolTokens for a media result — exempt it or the feature is dead. + const mediaExempt = cfg.mediaOnly && hasMediaPayload(msg); + if (!mediaExempt && tokens < cfg.minToolTokens) return msg; const ref = refForRaw(state.messageRefs, msg.id); if (!ref || ref === BLOCKED_REF) return msg; promptedCount++; @@ -305,6 +310,15 @@ export function applyAbsorb(input: AbsorbInput): AbsorbOutcome { resultText: `absorb failed: ref ${input.ref} is a ${target.contentType}, not a tool result.`, }; } + // #3371 discipline: screening and execution must share one judgment — a + // mediaOnly session never absorbs a target that isAbsorbCandidate would skip. + if (cfg.mediaOnly && !hasMediaPayload(target)) { + return { + state: input.state, + ok: false, + resultText: `absorb failed: ${input.ref} carries no media payload — absorb is scoped to image-bearing results (compress.absorb.mediaOnly).`, + }; + } if (isAcpOrConfiguredTool(target.toolName, cfg)) { return { state: input.state, diff --git a/kernel/src/types.ts b/kernel/src/types.ts index 00c9479f88..5968b0ae6a 100644 --- a/kernel/src/types.ts +++ b/kernel/src/types.ts @@ -130,6 +130,8 @@ export interface AbsorbConfig { contextThresholdPct: number; /** Tool-name patterns (glob suffix allowed) never absorbable, independent of protectedTools. */ excludeTools: string[]; + /** #3402: when true, only image-bearing tool results are candidates, and they bypass minToolTokens. */ + mediaOnly?: boolean; } /** Emitted when the session's irreducible floor (system prompt + block diff --git a/kernel/tests/absorb.test.ts b/kernel/tests/absorb.test.ts index d4d24cb968..caba5271a6 100644 --- a/kernel/tests/absorb.test.ts +++ b/kernel/tests/absorb.test.ts @@ -8,6 +8,7 @@ import { hideAbsorbedMessages, isAbsorbCandidate, parseAbsorbInput, + DEFAULT_ABSORB_CONFIG, ABSORB_PROMPT_MARKER, } from "../src/absorb.js"; import { createCore } from "../src/compress.js"; @@ -66,6 +67,10 @@ function session(): CoreMessage[] { ]; } +// mediaOnly is pinned false here on purpose: these tests exercise the absorb +// MECHANISM (prompting/hiding/stats/skip rules) in isolation over text results. +// The #3402 media-scope gate (default true) is covered by dedicated tests at +// the bottom of this file — mixing it in would make every fixture need a payload. function absorbConfig( overrides: Partial = {}, ): Config["absorb"] { @@ -75,10 +80,17 @@ function absorbConfig( minToolTokens: 1000, contextThresholdPct: 0, excludeTools: [], + mediaOnly: false, ...overrides, }; } +function mediaResult(id: string, callId: string, toolName = "screenshot"): CoreMessage { + return Object.assign(smallResult(id, callId, toolName), { + imageBase64: "iVBORw0KGgoAAAANSUhEUg==", + }); +} + function stateWithRefs(messages: CoreMessage[]): CompressionState { const state = createInitialState(); state.messageRefs = assignRefs(messages, { @@ -408,6 +420,7 @@ test("absorbed records survive applyCompression cloning", () => { tokenCount: 100, }).state; const config = defaultConfig(200000, { + absorb: absorbConfig(), compress: { minCompressRange: 0, maxSummaryLength: 20000, @@ -479,3 +492,66 @@ test("absorb config validation reports range errors", () => { ); assert.ok(errors.some((e) => e.includes("absorb.contextThresholdPct"))); }); + +// --- #3402 media-scope gate (revival of absorb, scoped to image-bearing results) --- + +test("isAbsorbCandidate: mediaOnly gates on hasMediaPayload", () => { + const textRes = bigResult("r1", "call1"); + const mediaRes = mediaResult("r2", "call2"); + const on = defaultConfig(200000, { absorb: absorbConfig({ mediaOnly: true }) }); + const off = defaultConfig(200000, { absorb: absorbConfig({ mediaOnly: false }) }); + assert.equal(isAbsorbCandidate(textRes, on), false, "mediaOnly on + no media → not a candidate"); + assert.equal(isAbsorbCandidate(mediaRes, on), true, "mediaOnly on + media → candidate"); + assert.equal(isAbsorbCandidate(textRes, off), true, "mediaOnly off + no media → candidate (old behavior)"); + assert.equal(isAbsorbCandidate(mediaRes, off), true, "mediaOnly off + media → candidate"); +}); + +test("appendAbsorbPrompts: mediaOnly marks image results (even small) and skips text results", () => { + const build = (): CoreMessage[] => [ + toolCall("c1", "call1", "read"), + bigResult("r1", "call1", "read"), + toolCall("c2", "call2", "screenshot"), + mediaResult("r2", "call2", "screenshot"), + ]; + const onMsgs = build(); + const onState = stateWithRefs(onMsgs); + const onCfg = defaultConfig(200000, { absorb: absorbConfig({ mediaOnly: true }) }); + const onRes = appendAbsorbPrompts(onMsgs, onState, onCfg, 5000, countTokens); + assert.ok(!onRes.messages.find((m) => m.id === "r1")!.text!.includes(ABSORB_PROMPT_MARKER), "text-only result NOT marked under mediaOnly"); + assert.ok(onRes.messages.find((m) => m.id === "r2")!.text!.includes(ABSORB_PROMPT_MARKER), "small image result IS marked (exempt from minToolTokens)"); + assert.equal(onRes.promptedCount, 1); + + const offMsgs = build(); + const offState = stateWithRefs(offMsgs); + const offCfg = defaultConfig(200000, { absorb: absorbConfig({ mediaOnly: false }) }); + const offRes = appendAbsorbPrompts(offMsgs, offState, offCfg, 5000, countTokens); + assert.ok(offRes.messages.find((m) => m.id === "r1")!.text!.includes(ABSORB_PROMPT_MARKER), "old behavior: large text result marked"); + assert.ok(!offRes.messages.find((m) => m.id === "r2")!.text!.includes(ABSORB_PROMPT_MARKER), "old behavior: tiny result below bar stays unmarked"); +}); + +test("applyAbsorb: mediaOnly rejects a non-media target (mirror of screening)", () => { + const messages: CoreMessage[] = [ + toolCall("c1", "call1", "read"), + bigResult("r1", "call1", "read"), + toolCall("c2", "call2", "screenshot"), + mediaResult("r2", "call2", "screenshot"), + ]; + const state = stateWithRefs(messages); + const cfg = defaultConfig(200000, { absorb: absorbConfig({ mediaOnly: true }) }); + const refused = applyAbsorb({ ref: refForRaw(state.messageRefs, "r1")!, summary: "s", messages, state, config: cfg }); + assert.equal(refused.ok, false); + assert.ok(refused.resultText.includes("no media payload")); + const accepted = applyAbsorb({ ref: refForRaw(state.messageRefs, "r2")!, summary: "s", messages, state, config: cfg }); + assert.ok(accepted.ok, accepted.resultText); + assert.equal(accepted.state.absorbed!.length, 1); +}); + +test("kernel ships mediaOnly neutral; the media-scoped default is host policy", () => { + // The engine exposes the capability with a neutral default (unset → legacy + // full-text when enabled) so engine goldens stay untouched; bili's product + // stance (media-scoped by default, #3402) is applied one layer up in + // resolveAbsorbSettings — pinned in tests/absorb.test.ts ("maps settings + // absorb onto kernel AbsorbConfig"). + assert.equal(defaultConfig(100000).absorb?.mediaOnly, undefined); + assert.equal(DEFAULT_ABSORB_CONFIG.mediaOnly, undefined); +}); diff --git a/package-lock.json b/package-lock.json index c15be29ef8..f74ad3c502 100644 --- a/package-lock.json +++ b/package-lock.json @@ -46,7 +46,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.128", + "version": "0.0.129", "dev": true, "license": "MIT", "devDependencies": { diff --git a/reference/architecture.md b/reference/architecture.md index d7356800f6..f929892475 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -66,7 +66,7 @@ billion-context/ │ ├── image-tokens.ts # Image token cost estimation (pixels/bytes modes, #767) │ ├── image-compress.ts # Image-compression policy stamping per request │ ├── image-note.ts # Image-block placeholder → rich note rendering -│ ├── proxy-tool.ts # isProxyToolFor — client-named tools routed to the bili proxy +│ ├── absorb.ts # absorb tool (kernel ACP_TOOL_NAMES extension) │ ├── compress-loop-responses.ts # Compress loop for Responses API format │ ├── compress-settings.ts # Three-level compress config merge (global→provider→model) │ ├── compress-tool.ts # ACP tool surface — thin re-export from acp-kernel diff --git a/src/absorb.ts b/src/absorb.ts new file mode 100644 index 0000000000..38dfa50ba7 --- /dev/null +++ b/src/absorb.ts @@ -0,0 +1,126 @@ +import { + ABSORB_TOOL_NAME, + applyAbsorb, + appendAbsorbPrompts, + defaultCountTokens, + hideAbsorbedMessages, + parseAbsorbInput, + type AbsorbConfig, + type AbsorbOutcome, + type CompressionState, + type Config, + type CoreMessage, +} from "acp-kernel"; +import { PROXY_TOOL_NAMES, READ_ORIGINAL_IMAGE_TOOL_NAME, RULE_TOOL_NAME } from "./compress-tool.js"; +import { effectiveRulesEnabled } from "./rules-feature.js"; +import { ccrEnabled, retrieveToolName } from "./store.js"; +import { log as loggerLog } from "./logger.js"; +import type { Session } from "./session.js"; +import { toolFail, toolOk, type ProxyToolResult } from "./proxy-tool-result.js"; + +const EFFECTIVE_ABSORB_KEY = "effectiveAbsorb"; + +export function absorbEnabled(config: Config): boolean { + return config.absorb?.enabled === true; +} + +export function absorbToolName(config: Config): string { + return config.absorb?.toolName ?? ABSORB_TOOL_NAME; +} + +// The kernel's ACP_TOOL_NAMES deliberately excludes absorb (hosts register it +// opt-in), so adjudication must check the resolved config alongside the static +// set. Two config sources exist: wire paths carry the per-request resolved +// Config, while the plugin tool API resolves sessions without a request +// context and falls back to the base kernel Config — prepare* therefore stores +// the last resolved absorb block per session for that path to read. +export function effectiveAbsorbConfig(session: Session | undefined, fallback: Config): AbsorbConfig | undefined { + const meta = session?.metadata[EFFECTIVE_ABSORB_KEY]; + if (meta && typeof meta === "object" && typeof (meta as AbsorbConfig).enabled === "boolean") { + return meta as AbsorbConfig; + } + return fallback.absorb; +} + +export function storeEffectiveAbsorb(session: Session, config: Config): void { + session.metadata[EFFECTIVE_ABSORB_KEY] = config.absorb ?? null; +} + +export function isProxyToolFor(name: string, session: Session | undefined, config: Config): boolean { + if (PROXY_TOOL_NAMES.has(name)) return true; + // #3400: read_original_image executes server-side on EVERY lane — even + // where it is not registered in the manifest (codex) a hallucinated call + // must never reach the client as an unsupported tool. + if (name === READ_ORIGINAL_IMAGE_TOOL_NAME) return true; + const absorb = effectiveAbsorbConfig(session, config); + if (absorb?.enabled === true && name === (absorb.toolName ?? ABSORB_TOOL_NAME)) return true; + if (ccrEnabled(session) && name === retrieveToolName(session)) return true; + return effectiveRulesEnabled(session, config) && name === RULE_TOOL_NAME; +} + +// Per-turn view transform: drop absorbed tool-call/result pairs, then append +// the forced [ACP absorb] instruction to every eligible large tool result. +// Hiding is unconditional so recorded absorptions stay hidden even if the +// feature is disabled mid-session; prompting self-gates inside the kernel on +// config.absorb.enabled (processTurn already injects markers from the same +// config — the append here is an idempotent safety net). Returns a new array +// that shares message refs with the input. +export function applyAbsorbView(messages: CoreMessage[], state: CompressionState, config: Config, tokenCount: number): CoreMessage[] { + const hidden = hideAbsorbedMessages(messages, state); + const prompted = appendAbsorbPrompts(hidden, state, config, tokenCount, defaultCountTokens); + return prompted.messages; +} + +type AbsorbExecCtx = { + config: Config; + messages: CoreMessage[]; + session: Session; + log?: (msg: string) => void; +}; + +// Execute one absorb call against the session (streaming loop, JSON rewrite +// and the plugin tool API share this). Returns model-facing result text; +// failures carry FAILED so visibility markers render ❌ and the loop's +// repeat-failure guard reasons about them uniformly with compress failures. +export function executeAbsorb(args: unknown, callId: string | undefined, absorb: AbsorbConfig, ctx: AbsorbExecCtx): ProxyToolResult { + const log = ctx.log ?? ((msg: string) => loggerLog("info", msg)); + const parsed = parseAbsorbInput(args, callId, (w) => log(`[acp-absorb] ${w}`)); + if (!parsed) { + return toolFail('[absorb FAILED: invalid input — expected { ref: "mNNNNN", summary: "..." }]'); + } + const before = ctx.session.state.absorbed?.length ?? 0; + let outcome: AbsorbOutcome; + try { + outcome = applyAbsorb({ + ref: parsed.ref, + summary: parsed.summary, + absorbCallId: parsed.absorbCallId, + messages: ctx.messages, + state: ctx.session.state, + config: { ...ctx.config, absorb }, + }); + } catch (err) { + log(`[acp-absorb] error: ${String(err)}`); + return toolFail(`[absorb FAILED: ${String(err)}]`); + } + ctx.session.state = outcome.state; + if (!outcome.ok) { + log(`[acp-absorb] ${outcome.resultText}`); + return toolFail(`[absorb FAILED: ${outcome.resultText}]`); + } + // ok=true also covers the no-op re-absorb ("already absorbed"); detect a + // real absorption by the appended record so credits net exactly once. + const records = outcome.state.absorbed ?? []; + const record = records.length > before ? records[records.length - 1] : undefined; + if (record) { + // Same credit netting as compress (#252): the re-request re-sends the + // unfolded history, so usage reports over-report until the next + // prepare hides the pair. + ctx.session.stats.compressCreditTokens = (ctx.session.stats.compressCreditTokens ?? 0) + record.tokensReclaimed; + ctx.session.stats.lastInputTokens = Math.max(0, ctx.session.stats.lastInputTokens - record.tokensReclaimed); + log(`[acp-absorb] ${outcome.resultText} (credit -${record.tokensReclaimed} tok)`); + } else { + log(`[acp-absorb] ${outcome.resultText}`); + } + return toolOk(outcome.resultText); +} diff --git a/src/agent/opencode-v2.ts b/src/agent/opencode-v2.ts index 6462c3b7ec..c29c882972 100644 --- a/src/agent/opencode-v2.ts +++ b/src/agent/opencode-v2.ts @@ -48,7 +48,7 @@ // openai tool list, src/compress-tool.ts) because reload-based refresh is not // available on all observed surfaces. -import { ACP_TOOLS_OPENAI } from "../compress-tool.js"; +import { ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI } from "../compress-tool.js"; import { asciiHeaderValue, fetchManifest, fetchProxyVersion, fetchStatus, fitNoticeDescription, forwardTool, postIdentityRegister, proxyBaseFromEnv, proxyBaseFromUrl, reportCompactionBoundary, reportRuntimeInfoOnChange, V2_SYNTHETIC_TEXT } from "./shared.js"; import { createForkAdopter } from "./fork-adopt.js"; import { logHostEvent } from "./host-log.js"; @@ -140,7 +140,7 @@ const TOOL_DELTA_RETRY_MS = 15000; // Kill switch = fully inert (same semantics as detectProxyBase): gates header stamping, tool forwarding, compaction reporting. const pluginDisabled = (): boolean => process.env.BILLION_CONTEXT_PLUGIN === "0"; -const V2_BILI_TOOLS = ACP_TOOLS_OPENAI.map((t) => ({ +const V2_BILI_TOOLS = [...ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI].map((t) => ({ name: t.function.name, description: t.function.description, input: t.function.parameters, diff --git a/src/compress-settings.ts b/src/compress-settings.ts index 06fa06e538..65b74b5cfa 100644 --- a/src/compress-settings.ts +++ b/src/compress-settings.ts @@ -1,4 +1,4 @@ -import { DEFAULT_CCR_CONFIG, defaultPrompts, resolvePrompts, createPackResolver, defaultPackSources, isValidPackName, type Config, type CcrConfig, 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 NamedProviderRecipe, type ProviderRoutes } from "./config.js"; import { configDir } from "./paths.js"; @@ -52,6 +52,10 @@ export function mergeCompress( // value still beats a global-level new-name value. The merged output // always carries the canonical name only. const rangeOf = (s?: CompressSettings): number | undefined => s?.minCompressRangeChars ?? s?.minCompressRange; + // `absorb` is a second nested-object field, merged sub-field-wise exactly + // 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 ccrLevels = [global?.ccr, provider?.ccr, model?.ccr].filter(Boolean) as NonNullable[]; const searchLevels = [global?.search, provider?.search, model?.search].filter(Boolean) as NonNullable[]; const reasoningLevels = [global?.reasoning, provider?.reasoning, model?.reasoning].filter(Boolean) as NonNullable[]; @@ -59,7 +63,7 @@ export function mergeCompress( const outputSteeringLevels = [global?.outputSteering, provider?.outputSteering, model?.outputSteering].filter(Boolean) as NonNullable[]; const priceProfileLevels = [global?.priceProfile, provider?.priceProfile, model?.priceProfile].filter(Boolean) as NonNullable[]; // `tierNudgeTokens` is another nested-object field merged sub-field-wise - // like `prompts`: a model-level t2 must not discard a provider-level t1. + // like `absorb`: a model-level t2 must not discard a provider-level t1. const tierNudgeLevels = [global?.tierNudgeTokens, provider?.tierNudgeTokens, model?.tierNudgeTokens].filter(Boolean) as NonNullable[]; return { modelContextLimit: pick("modelContextLimit"), @@ -81,6 +85,7 @@ export function mergeCompress( preserveRecentTools: pick("preserveRecentTools"), prompts: promptLevels.length > 0 ? Object.assign({}, ...promptLevels) : undefined, acknowledgePromptsRisk: pick("acknowledgePromptsRisk"), + absorb: absorbLevels.length > 0 ? Object.assign({}, ...absorbLevels) : undefined, ccr: ccrLevels.length > 0 ? Object.assign({}, ...ccrLevels) : undefined, search: searchLevels.length > 0 ? Object.assign({}, ...searchLevels) : undefined, rules: pick("rules"), @@ -88,7 +93,7 @@ export function mergeCompress( visibilityMarkers: pick("visibilityMarkers"), streamSummary: pick("streamSummary"), // `reasoning` is a third nested-object field merged sub-field-wise - // exactly like `prompts`: a model-level `threshold` must not + // exactly like `absorb`/`prompts`: a model-level `threshold` must not // discard a provider-level `drop: false`. reasoning: reasoningLevels.length > 0 ? Object.assign({}, ...reasoningLevels) : undefined, reasoningGuard: reasoningGuardLevels.length > 0 ? Object.assign({}, ...reasoningGuardLevels) : undefined, @@ -237,6 +242,10 @@ export function hasCompressSettings(s: CompressSettings): boolean { * "read"]` is the one-entry #1198/#1277 remedy, no built-in list * restating/freezing. Whole-array replace, deepest level wins; unset * passes `base.preserveRecentTools` through. + * - `absorb` → `absorb` (kernel AbsorbConfig; unset fields inherit the + * kernel DEFAULT_ABSORB_CONFIG, so a partial user block still resolves + * 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. * - `ccr` → `ccr` (kernel CcrConfig, acp-kernel >= 0.0.84; the kernel runs @@ -255,6 +264,24 @@ export function hasCompressSettings(s: CompressSettings): boolean { // (validateConfig does not flag unknown keys). export type ResolvedKernelConfig = Config & { preserveRecentTools?: string[]; externalSummary?: ExternalSummarySettings }; +// Single source of truth for raw->resolved absorb: applyCompressSettings and the +// plugin-lane base stamp (#1359) both go through this or they drift apart. +export function resolveAbsorbSettings(s: CompressSettings["absorb"]): AbsorbConfig | undefined { + if (s === undefined) return undefined; + const d = DEFAULT_ABSORB_CONFIG; + return { + enabled: s.enabled === true, + toolName: s.toolName ?? d.toolName, + minToolTokens: s.minToolTokens ?? d.minToolTokens, + contextThresholdPct: s.contextThresholdPct !== undefined ? parsePercent(s.contextThresholdPct) : d.contextThresholdPct, + excludeTools: s.excludeTools ?? [...d.excludeTools], + // #3402: bili's default stance is media-scoped — the revival exists to fix + // image pile-up (#3398) and to kill the old full-text over-compression. The + // kernel ships mediaOnly neutral (unset); this is the single place the + // default-on policy is applied. Explicit false restores pre-revival full-text. + mediaOnly: s.mediaOnly ?? true, + }; +} export function applyCompressSettings(base: Config, limit: number, s: CompressSettings, recipes: Record = {}): ResolvedKernelConfig { const nudge = { ...base.nudge }; @@ -281,6 +308,7 @@ export function applyCompressSettings(base: Config, limit: number, s: CompressSe } const tiers = { ...base.tiers }; if (s.tiers !== undefined) tiers.enabled = s.tiers; + const absorb = resolveAbsorbSettings(s.absorb); // #1207 owner decision: CCR is opt-in on every lane — no default-on // else-branch; an unset `s.ccr` leaves `base.ccr` untouched (off). let ccr: CcrConfig | undefined; @@ -321,6 +349,7 @@ export function applyCompressSettings(base: Config, limit: number, s: CompressSe // HERE, at resolve time — the executor still receives a fully // expanded ExternalSummarySettings and never sees references. externalSummary: s.externalSummary !== undefined ? expandExternalSummaryChainTolerant(s.externalSummary, recipes) : (base as ResolvedKernelConfig).externalSummary, + ...(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 5f6471dab7..e679fdf047 100644 --- a/src/compress-tool.ts +++ b/src/compress-tool.ts @@ -12,6 +12,9 @@ */ import { parseCompressArgs, + ABSORB_TOOL, + ABSORB_TOOL_GOOGLE, + ABSORB_TOOL_OPENAI, DECOMPRESS_TOOL, DECOMPRESS_TOOL_GOOGLE, DECOMPRESS_TOOL_NAME, @@ -85,9 +88,14 @@ export { buildCompressSystemPrompt, buildCompressTextSystemPrompt, buildCompressHybridSystemPrompt, + ABSORB_TOOL_NAME, + ABSORB_TOOL, + ABSORB_TOOL_OPENAI, + ABSORB_TOOL_GOOGLE, + buildAbsorbSystemPrompt, RULE_TOOL_NAME, } from "acp-kernel"; -export type { ParsedRange } from "acp-kernel"; +export type { ParsedRange, AbsorbConfig } from "acp-kernel"; export { ACP_TOOL_NAMES as PROXY_TOOL_NAMES, ACP_MUTATING_TOOLS as MUTATING_PROXY_TOOLS, ACP_READONLY_TOOLS as READONLY_PROXY_TOOLS } from "acp-kernel"; // #1881: the ACP-TAGS NEVER-echo prohibition must not disappear with injectTool. @@ -328,6 +336,29 @@ export function ptcNoteMode(names: Iterable): AcpNoteMode { return "none"; } +// The kernel ships no Responses-format absorb const (the four ACP tools have +// *_RESPONSES variants; absorb is host-registered opt-in). Synthesize it in +// the same flat shape as SEARCH_CONTEXT_TOOL_RESPONSES. +export const ABSORB_TOOL_RESPONSES = { + type: "function", + name: ABSORB_TOOL_OPENAI.function.name, + description: ABSORB_TOOL_OPENAI.function.description, + parameters: ABSORB_TOOL_OPENAI.function.parameters, +}; + +// #1359: absorb wire shapes parameterized by name so registration (manifest + +// per-request injection) follows the resolved `absorb.toolName` in BOTH lanes — +// a renamed tool must be advertised, injected, and adjudicated under one name. +// At the default name this is byte-identical to the static consts above. +export function absorbToolsFor(name: string) { + return { + anthropic: { name, description: ABSORB_TOOL.description, input_schema: ABSORB_TOOL.input_schema }, + openai: { type: "function" as const, function: { name, description: ABSORB_TOOL_OPENAI.function.description, parameters: ABSORB_TOOL_OPENAI.function.parameters } }, + responses: { type: "function" as const, name, description: ABSORB_TOOL_OPENAI.function.description, parameters: ABSORB_TOOL_OPENAI.function.parameters }, + google: { name, description: ABSORB_TOOL_GOOGLE.description, parameters: ABSORB_TOOL_GOOGLE.parameters }, + }; +} + // The reconciled kernel (acp-kernel#332) ships RULE_TOOL_NAME + the rule state // helpers but no wire tool objects. Synthesize all four wire shapes here so // every injection point (wire helpers, plugin manifest) serves one definition. diff --git a/src/config.ts b/src/config.ts index 2fbf335111..c56a92dfd1 100644 --- a/src/config.ts +++ b/src/config.ts @@ -389,6 +389,36 @@ export type CompressSettings = { * builtin (`default`, `lean`)]. Deepest-wins like every other field; * unknown names fall back to the identity surface. Kernel >= 0.0.66. */ promptPack?: string; + /** Instant tool-result absorption (kernel absorb API, acp-kernel >= 0.0.54). + * When `enabled`, eligible large tool results carry a forced [ACP absorb] + * instruction and the model distills them via the injected `absorb` tool; + * the original output is then hidden from every wire view until the next + * fold round (and its token cost is netted out of usage credits, like + * compress). Maps to kernel `Config.absorb`. Off unless explicitly + * enabled at some level. NOT supported on Responses marker/text-protocol + * routes (no native tool surface there). */ + absorb?: { + /** Enable absorb for this scope. Absent/false = off (kernel semantics). */ + enabled?: boolean; + /** Tool results smaller than this many tokens never get the absorb + * instruction (kernel default 1000). */ + minToolTokens?: number; + /** Only emit instructions once context usage reaches this fraction of + * the model window (kernel `contextThresholdPct`). Accepts a ratio + * (0.5) or percent string ("50%"); 0 = size gate alone (kernel + * default). */ + contextThresholdPct?: number | string; + /** Tool names whose results are never absorbable (glob-suffix + * patterns, e.g. "read"). Kernel default: none. */ + excludeTools?: string[]; + /** Rename the wire tool (default "absorb"). Must stay unique against + * the client's own tool names or the agent will call its own tool. */ + toolName?: string; + /** [#3402] When true (default), only image-bearing tool results are + * absorb candidates and they bypass minToolTokens; false restores the + * old behavior of marking any result ≥ minToolTokens. */ + mediaOnly?: boolean; + }; /** [#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 → @@ -398,7 +428,7 @@ export type CompressSettings = { * 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 `prompts`. */ + * level. Merged sub-field-wise across the three levels like `absorb`. */ ccr?: { /** Enable CCR for this scope. Absent/false = off (kernel semantics). */ enabled?: boolean; @@ -424,7 +454,7 @@ export type CompressSettings = { * are all reused unchanged. Disabled or no plan state in context ⇒ * byte-identical output to the plain lexical ranking. Off by default; * measure before enabling in shared configs. Merged sub-field-wise across - * the three levels like `ccr`. See src/decompress-shared.ts. */ + * the three levels like `absorb`/`ccr`. See src/decompress-shared.ts. */ search?: { /** Enable plan-aware re-ranking + steering for search_context. * Absent/false = off (byte-identical output). */ @@ -448,7 +478,7 @@ export type CompressSettings = { * billion-context-pi #336/#339 and opencode-acp #377). Compress turns * are hard-exempt from compression, so their reasoning is otherwise an * unreclaimable context floor. Merged sub-field-wise across the three - * config levels like `prompts`. */ + * config levels like `absorb`. */ reasoning?: { /** Master switch (default true). Set `drop: false` per-provider for * models whose reasoning must round-trip unmodified. */ @@ -463,7 +493,7 @@ export type CompressSettings = { * hits the lattice AND carries an encrypted_content blob, bili buffers the * response, replays its own reasoning plus a continue nudge (up to maxContinue * rounds), and folds to ONE response with true summed usage. Merged sub-field-wise - * across the three levels like `reasoning`; off unless enabled at some + * across the three levels like `absorb`/`reasoning`; off unless enabled at some * level. See src/reasoning-guard.ts. */ reasoningGuard?: ReasoningGuardConfig; /** [#1093] Output-side compression levers — verbosity steering (a conciseness @@ -485,7 +515,7 @@ export type CompressSettings = { * unset profile yields byte-identical reports to pre-#1279 behavior. The * last request's effective value is stamped onto the session and used by * every report face. Merged sub-field-wise across the three levels like - * `prompts`. */ + * `absorb`. */ priceProfile?: { w?: number; r?: number; q?: number }; /** [#1921] Fold-state reconciliation — how the proxy reacts when the * resent history no longer contains a folded message's content-hash id @@ -890,7 +920,7 @@ export interface DshFileSettings { const NAMED_PROVIDER_ROUTING_FIELDS = ["compress", "models", "proxy", "passthrough", "compressProtocol", "protocol", "compat", "imageBilling", "imageTokenCap"] as const; // Once-per-signature dedup so hot-reload / repeated launcher loads don't spam -// the same named-provider warning. +// the same named-provider warning (same pattern as the absorb warnings below). const seenNamedProviderWarnings = new Set(); function warnNamedProviderOnce(signature: string, message: string): void { @@ -1146,6 +1176,45 @@ export function allowDshCompactionState(env: NodeJS.ProcessEnv): { enabled: bool return { enabled: fileAllow, source: fileAllow ? "file" : null }; } +// #1359: provider/model absorb.* overrides apply only to the proxy lane (plugin +// lane follows the base block). Warn once per load so the divergence isn't silent. +const ABSORB_DIVERGENCE_FIELDS = ["enabled", "toolName", "minToolTokens", "contextThresholdPct", "excludeTools", "mediaOnly"] as const; +const seenAbsorbDivergenceWarnings = new Set(); + +function normAbsorbValue(field: string, v: unknown): unknown { + if (field === "contextThresholdPct" && typeof v === "string") { + const m = v.match(/^(-?\d+(?:\.\d+)?)\s*%$/); + if (m) return Number(m[1]) / 100; + } + return v; +} + +export function findAbsorbPluginDivergences(routes: ProviderRoutes, baseAbsorb?: CompressSettings["absorb"]): string[] { + const out: string[] = []; + const check = (scope: string, level?: CompressSettings["absorb"]) => { + if (!level) return; + for (const field of ABSORB_DIVERGENCE_FIELDS) { + if (level[field] === undefined) continue; + if (JSON.stringify(normAbsorbValue(field, level[field])) !== JSON.stringify(normAbsorbValue(field, baseAbsorb?.[field]))) { + out.push(`${scope}.absorb.${field}=${JSON.stringify(level[field])} (base=${JSON.stringify(baseAbsorb?.[field])})`); + } + } + }; + for (const [url, route] of Object.entries(routes)) { + check(url, route.compress?.absorb); + for (const [model, entry] of Object.entries(route.models ?? {})) check(`${url}/${model}`, entry.compress?.absorb); + } + return out; +} + +function warnAbsorbPluginDivergences(routes: ProviderRoutes, baseAbsorb?: CompressSettings["absorb"]): void { + const divs = findAbsorbPluginDivergences(routes, baseAbsorb); + if (divs.length === 0) return; + const sig = divs.join("\u0000"); + if (seenAbsorbDivergenceWarnings.has(sig)) return; + seenAbsorbDivergenceWarnings.add(sig); + loggerLog("warn", `[acp-config] provider/model absorb override diverges from base [${divs.join("; ")}] — plugin-mode sessions follow the base value, proxy-mode sessions honor the override (#1359)`); +} /** [#1345] A provider/model-level `ccr` field that diverges from what plugin @@ -1250,6 +1319,7 @@ export function loadOptions(env: NodeJS.ProcessEnv = process.env): ProxyOptions const upstream = (env.ACP_UPSTREAM ?? fileConfig.upstream ?? "https://api.anthropic.com").replace(/\/$/, ""); const routes = loadRoutes(env); const namedProviders = loadNamedProviders(env); + warnAbsorbPluginDivergences(routes, fileConfig.compress?.absorb); warnCcrPluginDivergences(routes, fileConfig.compress); const passthrough = passthroughState(env); const modelContextLimit = parseInt(env.ACP_MODEL_CONTEXT_LIMIT ?? `${fileConfig.modelContextLimit ?? 200000}`, 10); @@ -1914,13 +1984,13 @@ const COMPRESS_SETTING_FIELDS = new Set([ "outputHeadroomMaxPct", "tiers", "protectedLatestTools", "protectedTools", "neverPreserveRecentTools", "preserveRecentTools", "visibilityMarkers", "rules", "injectTool", "injectNudge", - "acknowledgePromptsRisk", "ccr", "search", + "acknowledgePromptsRisk", "absorb", "ccr", "search", "prompts", "promptPack", "reasoningGuard", "outputSteering", "priceProfile", "reconcile", "streamSummary", ]); // Deduped per unique key set per process (same pattern as -// seenNamedProviderWarnings): loadConfigFile() runs on every config read, +// seenAbsorbDivergenceWarnings): loadConfigFile() runs on every config read, // so without this one startup would log the same warning several times. const seenUnknownTopLevelKeySignatures = new Set(); @@ -2109,14 +2179,14 @@ function warnInertStripImages(obj: Record): void { } let seenInertStripImagesSignature = ""; -// #3344: two compress sub-blocks were REMOVED wholesale — `absorb` (#3293, the -// tool-result distillation feature) and `imageCompression` (#3266, image +// #3344: compress.imageCompression was REMOVED wholesale (#3266, image // pre-compression + the image_full restore channel). Old configs that still set -// them get a one-time inert notice instead of a silent no-op: the parse -// whitelist (COMPRESS_SETTING_FIELDS) no longer carries the fields, so they are -// dropped at EVERY compress level they can appear (global / provider / model). +// it get a one-time inert notice instead of a silent no-op: the parse whitelist +// (COMPRESS_SETTING_FIELDS) no longer carries the field, so it is dropped at +// EVERY compress level it can appear (global / provider / model). (`absorb` was +// removed by #3293 but REVIVED by #3402/#3422 — it parses again and is NOT inert.) // Dedup by dead-key path signature (#1815 style): re-warn when the set changes. -const INERT_REMOVED_COMPRESS_KEYS = ["absorb", "imageCompression"] as const; +const INERT_REMOVED_COMPRESS_KEYS = ["imageCompression"] as const; let seenInertRemovedCompressKeysSignature = ""; function warnInertRemovedCompressKeys(obj: Record): void { const hits: string[] = []; @@ -2146,7 +2216,7 @@ function warnInertRemovedCompressKeys(obj: Record): void { const sig = hits.join(","); if (sig === "" || sig === seenInertRemovedCompressKeysSignature) return; seenInertRemovedCompressKeysSignature = sig; - loggerLog("warn", `[acp-config] ${hits.join(", ")} is INERT — these compress sub-blocks were removed and no longer do anything at any level: compress.absorb (tool-result distillation feature, #3293) and compress.imageCompression (image pre-compression + image_full restore channel, #3266). Remove them from the config; oversized tool results are now handled by compress.ccr, and images pass through byte-identical.`); + loggerLog("warn", `[acp-config] ${hits.join(", ")} is INERT — compress.imageCompression was removed and no longer does anything at any level (image pre-compression + image_full restore channel, #3266). Remove it from the config; images pass through byte-identical.`); } function warnInertResignPassthrough(obj: Record): void { @@ -2575,6 +2645,37 @@ export function parseCompressSettings(v: unknown): (CompressSettings & { injectT if (typeof obj.acknowledgePromptsRisk !== "boolean") ok = false; else out.acknowledgePromptsRisk = obj.acknowledgePromptsRisk; } + if ("absorb" in obj && obj.absorb !== undefined) { + const a = obj.absorb; + if (!a || typeof a !== "object" || Array.isArray(a)) { + ok = false; + } else { + const ao = a as Record; + const cleaned: NonNullable = {}; + for (const key of ["enabled", "minToolTokens", "contextThresholdPct", "excludeTools", "toolName", "mediaOnly"] as const) { + if (!(key in ao)) continue; + const v = ao[key]; + if (key === "enabled" || key === "mediaOnly") { + if (typeof v !== "boolean") { ok = false; continue; } + if (key === "enabled") cleaned.enabled = v; + else cleaned.mediaOnly = v; + } else if (key === "minToolTokens") { + if (typeof v !== "number" || !Number.isFinite(v)) { ok = false; continue; } + cleaned.minToolTokens = v; + } else if (key === "contextThresholdPct") { + if (!numberOrPercent(v)) { ok = false; continue; } + cleaned.contextThresholdPct = typeof v === "string" ? v.trim() : 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.absorb = cleaned; + } + } if ("ccr" in obj && obj.ccr !== undefined) { const c = obj.ccr; if (!c || typeof c !== "object" || Array.isArray(c)) { diff --git a/src/loop/core.ts b/src/loop/core.ts index 095de4b43a..703aa184ad 100644 --- a/src/loop/core.ts +++ b/src/loop/core.ts @@ -15,11 +15,12 @@ import type { BiliMessage } from "acp-kernel/wire"; import { coreToAnthropic } from "acp-kernel/wire"; import { parseCompressInput, + ABSORB_TOOL_NAME, COMPRESS_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_NAME, RULE_TOOL_NAME, } from "../compress-tool.js"; -import { isProxyToolFor } from "../proxy-tool.js"; +import { effectiveAbsorbConfig, executeAbsorb, isProxyToolFor } from "../absorb.js"; import { effectiveRulesEnabled, executeRule } from "../rules-feature.js"; import { ccrEnabled, drainPendingRetrievals, executeRetrieve, imageRescaleToolArmed, retrieveToolName } from "../store.js"; import { applyImageRescalePass, executeReadOriginalImage } from "../image-rescale.js"; @@ -56,6 +57,7 @@ export function buildVisibilityMarker(toolName: string, result: string): string decompress: "📤", search_context: "🔍", acp_status: "📊", + absorb: "🫧", }; const icon = failed ? "❌" : (icons[toolName] ?? "📦"); @@ -140,6 +142,10 @@ const TRANSPORT_FAILURE_LABELS: Record = { unknown: "network error", }; +function activeAbsorbToolName(session: Session, config: Config): string | undefined { + const a = effectiveAbsorbConfig(session, config); + return a?.enabled === true ? a.toolName ?? ABSORB_TOOL_NAME : undefined; +} // Canonical args for repeat-detection: key-order/whitespace differences must not // defeat an identical-call match, so parse + re-stringify with sorted keys. @@ -365,6 +371,10 @@ export function executeProxyTool( if (toolName === "acp_cache") { return handleAcpCache(ctx.session, args); } + const absorb = effectiveAbsorbConfig(ctx.session, ctx.config); + if (absorb?.enabled === true && toolName === (absorb.toolName ?? ABSORB_TOOL_NAME)) { + return executeAbsorb(args, callId, absorb, ctx); + } if (effectiveRulesEnabled(ctx.session, ctx.config) && toolName === RULE_TOOL_NAME) { return executeRule(args, ctx); } @@ -1304,9 +1314,10 @@ export async function* runCompressLoop( // its refresh is a harmless identical-view re-fold. Falls back // to the pre-compress view (previous behavior) if the host hook // is absent or throws. + const absorbName = activeAbsorbToolName(ctx.session, ctx.config); if ( ctx.refreshFolded && - proxyResults.some((pr) => (pr.name === "compress") && !pr.result.includes("FAILED")) + proxyResults.some((pr) => (pr.name === "compress" || pr.name === absorbName) && !pr.result.includes("FAILED")) ) { try { const refreshed = await ctx.refreshFolded(coreMessages); @@ -1458,9 +1469,10 @@ export async function* runCompressLoop( if (blocks) committedBlocks += Number(blocks[1]); } } + const absorbed = proxyResults.some((pr) => pr.name === activeAbsorbToolName(ctx.session, ctx.config) && !pr.result.includes("FAILED")); const note = committedTokens > 0 ? `compression committed (~${committedTokens.toLocaleString("en-US")} tokens saved${committedBlocks > 0 ? ` in ${committedBlocks} block(s)` : ""}); ` - : ""; + : absorbed ? "context work committed; " : ""; const msg = `${note}the follow-up request to the model failed (${TRANSPORT_FAILURE_LABELS[kind]}) before any response — your context is intact, resend your last message to continue`; ctx.log(`[acp-loop] round ${round}: re-request transport failure (${kind}); ending turn in-band instead of throwing (#1453)`); loggerLog("error", `[acp-loop] re-request transport failure after ${round} round(s): ${formatUpstreamError(e, requestOptions.url, ctx.proxyUrl, ctx.skippedProxy)}`); diff --git a/src/plugin.ts b/src/plugin.ts index 46cf485253..8b9fa0b10c 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -10,10 +10,10 @@ import { getStore, storageStatusLabel } from "./persist.js"; import { cloneStoreForRefs } from "./store.js"; import { acquireInFlight, createSession, getSession, publishForkSession, diagnoseSuccessWithoutUsage, effectiveConfig, findSessionByCanonicalId, listSessions, markCompactionBoundary, markDirty, peekSession, releaseInFlight, statusInputBaseline, withSessionLock, type Session } from "./session.js"; import { clientConversationHeader } from "./session-id.js"; -import { BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, PROXY_TOOL_NAMES, READ_ORIGINAL_IMAGE_TOOL, READ_ORIGINAL_IMAGE_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_OPENAI, READ_ORIGINAL_IMAGE_TOOL_RESPONSES, RETRIEVE_TOOL_NAME, RULE_TOOL, RULE_TOOL_NAME, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, SEARCH_CONTEXT_TOOL_NAME, retrieveToolsFor, withAcpNotes, withAcpNotesOpenAI } from "./compress-tool.js"; +import { ABSORB_TOOL_NAME, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, PROXY_TOOL_NAMES, READ_ORIGINAL_IMAGE_TOOL, READ_ORIGINAL_IMAGE_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_OPENAI, READ_ORIGINAL_IMAGE_TOOL_RESPONSES, RETRIEVE_TOOL_NAME, RULE_TOOL, RULE_TOOL_NAME, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, SEARCH_CONTEXT_TOOL_NAME, absorbToolsFor, retrieveToolsFor, withAcpNotes, withAcpNotesOpenAI } from "./compress-tool.js"; import { externalSummaryEnabled, externalSummaryMaxToolDurationMs, withExternalSummaryTools } from "./external-summary-surface.js"; import { salvageCompressArgs } from "./plugin-compress-salvage.js"; -import { isProxyToolFor } from "./proxy-tool.js"; +import { absorbEnabled, effectiveAbsorbConfig, isProxyToolFor } from "./absorb.js"; import { effectiveRulesEnabled, rulesEnabled } from "./rules-feature.js"; import { executeProxyToolAsync } from "./loop/core.js"; import type { ProxyToolResult } from "./proxy-tool-result.js"; @@ -812,6 +812,9 @@ export function handlePluginManifest(res: import("node:http").ServerResponse, co // enablement stays enforced at execution (isProxyToolFor / executeProxyTool). // #1359: the advertised name is the base-config toolName, matching the // plugin-lane gate (which adjudicates the same base block). + const absorbOn = absorbEnabled(config); + const absorbName = config.absorb?.toolName ?? ABSORB_TOOL_NAME; + const absorbTools = absorbOn ? absorbToolsFor(absorbName) : undefined; const rulesOn = rulesEnabled(config); // [#1271] acp_retrieve is advertised only while the base config enables CCR (same // #1192 conservative rule as absorb/acp_rule). The proxy wires it on the anthropic/ @@ -852,11 +855,11 @@ export function handlePluginManifest(res: import("node:http").ServerResponse, co proxy: "billion-context", version: VERSION, commit: BUILD_COMMIT, - toolNames: [...PROXY_TOOL_NAMES, ...(rulesOn ? [RULE_TOOL_NAME] : []), ...(ccrOn ? [ccrName] : []), ...(rescaleOn ? [READ_ORIGINAL_IMAGE_TOOL_NAME] : [])], + toolNames: [...PROXY_TOOL_NAMES, ...(absorbTools ? [absorbName] : []), ...(rulesOn ? [RULE_TOOL_NAME] : []), ...(ccrOn ? [ccrName] : []), ...(rescaleOn ? [READ_ORIGINAL_IMAGE_TOOL_NAME] : [])], tools: { - anthropic: [...acpAnthropic, ...(rulesOn ? [RULE_TOOL] : []), ...(ccrTools ? [ccrTools.anthropic] : []), ...(rescaleOn ? [READ_ORIGINAL_IMAGE_TOOL] : [])], - openai: [...acpOpenai, ...(rulesOn ? [RULE_TOOL_OPENAI] : []), ...(ccrTools ? [ccrTools.openai] : []), ...(rescaleOn ? [READ_ORIGINAL_IMAGE_TOOL_OPENAI] : [])], - responses: [...acpResponses, ...(rulesOn ? [RULE_TOOL_RESPONSES] : []), ...(rescaleOn ? [READ_ORIGINAL_IMAGE_TOOL_RESPONSES] : [])], + anthropic: [...acpAnthropic, ...(absorbTools ? [absorbTools.anthropic] : []), ...(rulesOn ? [RULE_TOOL] : []), ...(ccrTools ? [ccrTools.anthropic] : []), ...(rescaleOn ? [READ_ORIGINAL_IMAGE_TOOL] : [])], + openai: [...acpOpenai, ...(absorbTools ? [absorbTools.openai] : []), ...(rulesOn ? [RULE_TOOL_OPENAI] : []), ...(ccrTools ? [ccrTools.openai] : []), ...(rescaleOn ? [READ_ORIGINAL_IMAGE_TOOL_OPENAI] : [])], + responses: [...acpResponses, ...(absorbTools ? [absorbTools.responses] : []), ...(rulesOn ? [RULE_TOOL_RESPONSES] : []), ...(rescaleOn ? [READ_ORIGINAL_IMAGE_TOOL_RESPONSES] : [])], }, headers: { agent: PLUGIN_AGENT_HEADER, conversation: PLUGIN_CONVERSATION_HEADER, contextWindow: PLUGIN_CONTEXT_WINDOW_HEADER, maxOutput: PLUGIN_MAX_OUTPUT_HEADER, model: PLUGIN_MODEL_HEADER, instructionsMutable: PLUGIN_INSTRUCTIONS_MUTABLE_HEADER }, toolEndpoint: "/__bili/plugin/tool", @@ -1328,6 +1331,8 @@ export async function handlePluginFork(payload: string, res: ServerResponse): Pr * name is not one of the known opt-in tools (a truly unknown tool keeps the * generic 400 with its allowed list). */ function disabledOptionalToolNote(tool: string, session: Session, config: Config): string | undefined { + const absorbName = effectiveAbsorbConfig(session, config)?.toolName ?? ABSORB_TOOL_NAME; + if (tool === absorbName) return `${tool} is not enabled on this bili proxy (compress.absorb.enabled is not true) — nothing was absorbed.`; if (tool === RULE_TOOL_NAME) return `${tool} is not enabled on this bili proxy (compress.rules.enabled is not true) — nothing was recorded.`; if (!ccrEnabled(session) && tool === retrieveToolName(session)) return `${tool} is not enabled on this bili proxy (compress.ccr.enabled is not true) — nothing was retrieved.`; return undefined; @@ -1732,7 +1737,7 @@ export async function handlePluginTool( if (!isProxyToolFor(tool, session, deps.config)) { // #1192: a known opt-in tool disabled by this session's effective config // (registered from a manifest served while it was enabled) answers with - // model-facing text on the same channel executeRule use + // model-facing text on the same channel executeRule/executeAbsorb use // for failures — a hard 400 would surface as an unfixable red error card. const note = disabledOptionalToolNote(tool, session, deps.config); if (note !== undefined) { @@ -1742,6 +1747,8 @@ export async function handlePluginTool( return; } const allowed = [...PROXY_TOOL_NAMES]; + const absorb = effectiveAbsorbConfig(session, deps.config); + if (absorb?.enabled === true) allowed.push(absorb.toolName ?? ABSORB_TOOL_NAME); if (effectiveRulesEnabled(session, deps.config)) allowed.push(RULE_TOOL_NAME); res.writeHead(400, { "content-type": "application/json" }); res.end(JSON.stringify({ ok: false, error: `unknown tool "${tool}" (expected one of: ${allowed.join(", ")})` })); diff --git a/src/preflight.ts b/src/preflight.ts index 715e7c58a9..fa6126e3fc 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -11,6 +11,7 @@ import { } from "acp-kernel"; import { buildCompressSystemPrompt, parseCompressInput } from "./compress-tool.js"; import { IMAGE_PLACEHOLDER, imagePlaceholdersForSummary } from "./image-note.js"; +import { applyAbsorbView } from "./absorb.js"; import { adoptContentStore, ccrLoopConfig, contentStoreOf } from "./store.js"; import { applyRanges, normalizeRangeOrder, type RewriteCtx } from "./stream.js"; import { fetchWithTimeout, isTransientUpstreamError, replayMaxAttempts, replayBackoffMs, sleep, UpstreamHttpError } from "./fetch-util.js"; @@ -1637,7 +1638,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa }); deps.session.state = turn.state; adoptContentStore(deps.session, turn.contentStore); - const view = turn.messages; + const view = applyAbsorbView(turn.messages, turn.state, activeConfig, currentTokens); const est = applyEstimateCalibration(estimateCoreMessages(view) + wireOverhead, kFactor, kOrigin, deps.upstreamOrigin) + imageReserve; result.payloadEstimate = est; // #2952 review: keep the char-count UPPER bound fresh through recovery. @@ -1806,6 +1807,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa 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); // Floor on the session's measured input baseline: the upstream's // input_tokens also covers the system prompt + tool definitions, which // are not in turn.messages, so the direct estimate can undershoot. diff --git a/src/proxy-tool.ts b/src/proxy-tool.ts deleted file mode 100644 index 3f11159dd6..0000000000 --- a/src/proxy-tool.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { READ_ORIGINAL_IMAGE_TOOL_NAME, RULE_TOOL_NAME, PROXY_TOOL_NAMES } from "./compress-tool.js"; -import { ccrEnabled, retrieveToolName } from "./store.js"; -import { effectiveRulesEnabled } from "./rules-feature.js"; -import type { Config } from "acp-kernel"; -import type { Session } from "./session.js"; - -export function isProxyToolFor(name: string, session: Session | undefined, config: Config): boolean { - if (PROXY_TOOL_NAMES.has(name)) return true; - // #3400: read_original_image executes server-side on EVERY lane — even - // where it is not registered in the manifest (codex) a hallucinated call - // must never reach the client as an unsupported tool. - if (name === READ_ORIGINAL_IMAGE_TOOL_NAME) return true; - if (ccrEnabled(session) && name === retrieveToolName(session)) return true; - return effectiveRulesEnabled(session, config) && name === RULE_TOOL_NAME; -} diff --git a/src/server.ts b/src/server.ts index a2d4fc103d..4225814ccc 100644 --- a/src/server.ts +++ b/src/server.ts @@ -73,7 +73,8 @@ import { type GoogleSystemInstruction, type GoogleTool, } from "acp-kernel/wire"; -import { COMPRESS_TOOL, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_GOOGLE_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, BILI_ACP_READONLY_TOOLS_RESPONSES, BILI_ACP_READONLY_TOOLS_RESPONSES_NO_RANGE, COMPRESS_TOOL_NAME, RULE_TOOL, RULE_TOOL_GOOGLE, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, retrieveToolsFor, buildAcpTagsOnlyPrompt } from "./compress-tool.js"; +import { ABSORB_TOOL_NAME, COMPRESS_TOOL, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_GOOGLE_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, BILI_ACP_READONLY_TOOLS_RESPONSES, BILI_ACP_READONLY_TOOLS_RESPONSES_NO_RANGE, COMPRESS_TOOL_NAME, RULE_TOOL, RULE_TOOL_GOOGLE, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, absorbToolsFor, retrieveToolsFor, buildAcpTagsOnlyPrompt } from "./compress-tool.js"; +import { absorbEnabled, storeEffectiveAbsorb } from "./absorb.js"; import { ccrEnabled, ccrLoopConfig, ccrPluginWireOk, contentStoreOf, executeRetrieve, pruneExpiredRetrievals, reconcileReloadedRetrievals, renderRetrievalNotes, retrieveToolName, snapshotPendingRetrievals, snapshotRetrievalNotes, storeEffectiveCcr, type CcrSettings } from "./store.js"; import { buildIncomingImageIndex, pruneRetrieveImgExports } from "./image-restore.js"; import { rulesEnabled, storeEffectiveRules } from "./rules-feature.js"; diff --git a/src/server/inject.ts b/src/server/inject.ts index c7316e9ce5..6c342f7102 100644 --- a/src/server/inject.ts +++ b/src/server/inject.ts @@ -3,7 +3,8 @@ import { applyAcpToolOverrides, defaultPrompts } from "acp-kernel"; import type { ProxyOptions } from "../config.js"; import { type AnthropicRequestBody, type GoogleFunctionDeclaration, type GoogleTool, type OpenAITool } from "acp-kernel/wire"; import { appendSystemText } from "../util.js"; -import { BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_GOOGLE_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, packOwnsDescription, ptcNoteMode, withAcpNotes, withAcpNotesOpenAI, withMarkerIntegrityNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; +import { absorbEnabled, absorbToolName } from "../absorb.js"; +import { BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_TOOLS_GOOGLE_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, packOwnsDescription, ptcNoteMode, withAcpNotes, withAcpNotesOpenAI, withMarkerIntegrityNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; import { externalSummaryEnabled, withExternalSummaryTools } from "../external-summary-surface.js"; import { forceTextProtocol as knobForceTextProtocol, renderNone as knobRenderNone } from "../knobs.js"; @@ -27,6 +28,7 @@ export function injectSystem( const tagsOnly = buildAcpTagsOnlyPrompt("function", prompts, surface?.promptSections); if (tagsOnly) parts.push(tagsOnly); } + if (opts.compress.injectTool && absorbEnabled(config)) parts.push(buildAbsorbSystemPrompt(absorbToolName(config))); if (parts.length === 0) return parsed.system; // #1876: APPEND the prompt as a trailing block (client blocks byte-exact) // instead of merging everything into one block via kernel buildSystem. diff --git a/src/server/prepare-anthropic.ts b/src/server/prepare-anthropic.ts index e66aabc73d..1f292c589e 100644 --- a/src/server/prepare-anthropic.ts +++ b/src/server/prepare-anthropic.ts @@ -14,7 +14,8 @@ import { nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; -import { READ_ORIGINAL_IMAGE_TOOL, RULE_TOOL, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withSummaryBudgetNote } from "../compress-tool.js"; +import { ABSORB_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL, RULE_TOOL, absorbToolsFor, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withSummaryBudgetNote } from "../compress-tool.js"; +import { applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, dropRetrievals, imageRescaleToolArmed, pruneExpiredRetrievals, reconcileReloadedRetrievals, renderRetrievalNotes, retrieveToolName, snapshotPendingRetrievals, snapshotRetrievalNotes } from "../store.js"; import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; import { autoFoldEngaged, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; @@ -29,6 +30,7 @@ import { imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js"; import { applyImageRescalePass } from "../image-rescale.js"; import { isAutoModeClassifier } from "../auto-mode-forge.js"; import { countSystemAndToolsTokens, emergencyNudge, projectThinkingMass } from "./budget.js"; +import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { injectSystem, injectTool } from "./inject.js"; import { resolveLaneDecisions } from "./lane-decisions.js"; @@ -220,14 +222,23 @@ export async function prepareAnthropic( // compression trigger fires until overflow. See effectiveTokenCount. const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, msgs, inboundImageTokens); const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); + // Absorb markers are injected by the kernel's processTurn from + // config.absorb. With no channel to call the tool (injection off), + // strip absorb from the loop config so the REQUIRED instruction never + // reaches the wire. Hiding recorded absorptions is unaffected + // (applyAbsorbView hides regardless of enablement). + const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); + const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); + const absorbActive = absorbBlock?.enabled === true && opts.compress.injectTool; // acp_rule has no processTurn side effect (no markers/instructions are - // ever injected into messages) — only tool availability matters. + // ever injected into messages), so unlike absorb it needs no loop- + // config stripping — only tool availability matters. const rulesActive = rulesEnabled(config) && opts.compress.injectTool; // [#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 = ccrLoopConfig(session, config); + const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); // #2432/#2658: dsh desktop's native compaction can LAND without ever // transiting bili — its summarizer calls ctx.llm.stream() directly and // manual /compact / idle-session paths have no ALS attribution, so the @@ -283,7 +294,9 @@ export async function prepareAnthropic( const gap = foldCoverage(foldCoveredBefore, msgs.map((m) => m.id)); if (gap) log("warn", `[${sessionId}] [acp-drift] fold coverage mismatch: ${gap.matched}/${gap.expected} covered message id(s) present in resent history — ${gap.expected - gap.matched} covered id(s) missing from resent history — mutation (content edit invalidates content-hash refs, fold silently lost) or client-side deletion/truncation (benign, message no longer on the wire); observability complement to the #1328 overflow rescue (#1195)`); } + storeEffectiveAbsorb(session, loopConfig); storeEffectiveRules(session, config); + turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); turn.messages = attachSubagentSessions(turn.messages, session); // Drop sub-viability fragments before any consumer sees them: a tiny // range in the list makes batched compress attempts fail atomically @@ -376,7 +389,7 @@ export async function prepareAnthropic( // the compress prompt on round-2, the exact F2 seam the matrix pins). systemOut = stampAnthropicSystemCacheControl(systemOut, anthropicCacheMarks !== undefined); if (injectTools) { - toolsOut = injectTool(parsed.tools, [...(rulesActive ? [RULE_TOOL] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).anthropic] : []), ...(imageRescaleToolArmed(session) ? [READ_ORIGINAL_IMAGE_TOOL] : [])], surface?.toolPrompts, ccrEnabled(session), externalSummaryEnabled(config)); + toolsOut = injectTool(parsed.tools, [...(absorbActive ? [absorbTools.anthropic] : []), ...(rulesActive ? [RULE_TOOL] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).anthropic] : []), ...(imageRescaleToolArmed(session) ? [READ_ORIGINAL_IMAGE_TOOL] : [])], surface?.toolPrompts, ccrEnabled(session), externalSummaryEnabled(config)); } // Nudge as a separate trailing user message (cache-friendly): the // system block stays byte-stable so the prefix cache survives. diff --git a/src/server/prepare-google.ts b/src/server/prepare-google.ts index d9315e35d9..577ef21757 100644 --- a/src/server/prepare-google.ts +++ b/src/server/prepare-google.ts @@ -13,7 +13,8 @@ import { compressBreakerArmed } from "../stream.js"; import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; import { recordConflict } from "../conflict-watch.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; -import { READ_ORIGINAL_IMAGE_TOOL_GOOGLE, RULE_TOOL_GOOGLE, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; +import { ABSORB_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_GOOGLE, RULE_TOOL_GOOGLE, absorbToolsFor, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; +import { absorbToolName, applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, imageRescaleToolArmed, retrieveToolName } from "../store.js"; import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; import { autoFoldEngaged, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; @@ -24,6 +25,7 @@ import { imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js"; import { applyImageRescalePass } from "../image-rescale.js"; import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge } from "./budget.js"; import { estimateCoreMessages } from "../preflight.js"; +import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { injectGoogleTool, injectTool } from "./inject.js"; import { resolveLaneDecisions } from "./lane-decisions.js"; @@ -123,10 +125,14 @@ export async function prepareGoogle( const knownRefsBefore = new Set(Object.keys(session.state.messageRefs.byRaw)); const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, msgs, imageReserveFor(session, "google", parsed, opts, upstreamOrigin)); const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); + const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); + const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); + const absorbActive = absorbBlock?.enabled === true && shouldInject; // acp_rule has no processTurn side effect (no markers/instructions are - // ever injected into messages) — only tool availability matters. + // ever injected into messages), so unlike absorb it needs no loop- + // config stripping — only tool availability matters. const rulesActive = rulesEnabled(config) && shouldInject; - const loopConfig = ccrLoopConfig(session, config); + const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); // #2432/#2658: same post-landing recovery as the openai/responses/ // anthropic lanes — dsh desktop's native compaction can land without // transiting bili (direct-bypass / allowDshCompaction / future gate @@ -170,7 +176,9 @@ export async function prepareGoogle( const gap = foldCoverage(foldCoveredBefore, msgs.map((m) => m.id)); if (gap) log("warn", `[${sessionId}] [acp-drift] fold coverage mismatch: ${gap.matched}/${gap.expected} covered message id(s) present in resent history — ${gap.expected - gap.matched} covered id(s) missing from resent history — mutation (content edit invalidates content-hash refs, fold silently lost) or client-side deletion/truncation (benign, message no longer on the wire); observability complement to the #1328 overflow rescue (#1195)`); } + storeEffectiveAbsorb(session, loopConfig); storeEffectiveRules(session, config); + turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); turn.messages = attachSubagentSessions(turn.messages, session); // Drop sub-viability fragments before any consumer sees them: a tiny // range in the list makes batched compress attempts fail atomically @@ -235,6 +243,7 @@ export async function prepareGoogle( const tagsOnly = buildAcpTagsOnlyPrompt("function", prompts, surface?.promptSections); if (tagsOnly) sysParts.push(tagsOnly); } + if (absorbActive) sysParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); googleOutboundSystem = sysParts.join("\n\n"); // Untouched when nothing was added beyond the client's own text: the // original `systemInstruction` object then rides through byte-identical @@ -242,7 +251,7 @@ export async 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, [...(rulesActive ? [RULE_TOOL_GOOGLE] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).google] : []), ...(imageRescaleToolArmed(session) ? [READ_ORIGINAL_IMAGE_TOOL_GOOGLE] : [])], surface?.toolPrompts, ccrEnabled(session), externalSummaryEnabled(config)); + toolsOut = injectGoogleTool(parsed.tools, [...(absorbActive ? [absorbTools.google] : []), ...(rulesActive ? [RULE_TOOL_GOOGLE] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).google] : []), ...(imageRescaleToolArmed(session) ? [READ_ORIGINAL_IMAGE_TOOL_GOOGLE] : [])], surface?.toolPrompts, ccrEnabled(session), externalSummaryEnabled(config)); } if (sysNotes.length > 0) { rebuiltContents = appendGoogleNudge(rebuiltContents, sysNotes.join("\n\n---\n\n")); diff --git a/src/server/prepare-openai.ts b/src/server/prepare-openai.ts index 5c38f0a41d..3a6ea9aa20 100644 --- a/src/server/prepare-openai.ts +++ b/src/server/prepare-openai.ts @@ -14,7 +14,8 @@ import { nudgeSuppressed } from "../session-self-heal.js"; import { compressBreakerArmed } from "../stream.js"; import { applyCompactionArchive, coveredRealHistoryIds, detectUnannouncedHistoryRewrite, foldCoverage, markCompactionBoundary, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type PendingRetrieval, type Session } from "../session.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; -import { READ_ORIGINAL_IMAGE_TOOL_OPENAI, RULE_TOOL_OPENAI, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; +import { ABSORB_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_OPENAI, RULE_TOOL_OPENAI, absorbToolsFor, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressSystemPrompt, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; +import { absorbToolName, applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, dropRetrievals, imageRescaleToolArmed, pruneExpiredRetrievals, reconcileReloadedRetrievals, renderRetrievalNotes, retrieveToolName, snapshotPendingRetrievals, snapshotRetrievalNotes } from "../store.js"; import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; import { autoFoldEngaged, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; @@ -27,6 +28,7 @@ import { logRawStripCounts, markerArtifactCount, stripRawStage } from "../marker import { imageRescaleMaxDim, renderNone as knobRenderNone } from "../knobs.js"; import { applyImageRescalePass } from "../image-rescale.js"; import { clampOutgoingOutput, countSystemAndToolsTokens, dshLedgerFloorTokens, emergencyNudge } from "./budget.js"; +import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { injectOpenaiTool, injectTool } from "./inject.js"; import { resolveLaneDecisions } from "./lane-decisions.js"; @@ -120,8 +122,14 @@ export async function prepareOpenai( const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, msgs, imageReserveFor(session, "openai", parsed, opts, billingUpstream ?? upstreamOrigin)); const activeBefore = new Set(session.state.blocks.filter((b) => b.active).map((b) => b.blockId)); + // Absorb markers ride in the kernel's processTurn output (gated by + // config.absorb). Title-gen requests skip ALL injection for + // prefix-cache stability, so strip absorb from the loop config there. + const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); + const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); + const absorbActive = absorbBlock?.enabled === true && shouldInject; const rulesActive = rulesEnabled(config) && shouldInject; - const loopConfig = ccrLoopConfig(session, config); + const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); // #2432: dsh desktop's native compaction can LAND without ever // transiting bili — its summarizer calls ctx.llm.stream() directly, // and manual /compact / idle-session paths have no ALS attribution so @@ -181,7 +189,9 @@ export async function prepareOpenai( const gap = foldCoverage(foldCoveredBefore, msgs.map((m) => m.id)); if (gap) log("warn", `[${sessionId}] [acp-drift] fold coverage mismatch: ${gap.matched}/${gap.expected} covered message id(s) present in resent history — ${gap.expected - gap.matched} covered id(s) missing from resent history — mutation (content edit invalidates content-hash refs, fold silently lost) or client-side deletion/truncation (benign, message no longer on the wire); observability complement to the #1328 overflow rescue (#1195)`); } + storeEffectiveAbsorb(session, loopConfig); storeEffectiveRules(session, config); + turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); turn.messages = attachSubagentSessions(turn.messages, session); // Drop sub-viability fragments before any consumer sees them: a tiny // range in the list makes batched compress attempts fail atomically @@ -266,6 +276,7 @@ export async function prepareOpenai( const tagsOnly = buildAcpTagsOnlyPrompt("function", prompts, surface?.promptSections); if (tagsOnly) sysParts.push(tagsOnly); } + if (absorbActive) sysParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); rebuiltMessages = injectOpenaiSystem(rebuiltMessages, sysParts); if (sysNotes.length > 0) { rebuiltMessages = [...rebuiltMessages, ...sysNotes.map((text) => ({ role: "user" as const, content: text }))]; @@ -276,7 +287,7 @@ export async function prepareOpenai( // avoids double-counting it. openaiOutboundSystem = sysParts.join("\n\n"); if (injectTools) { - toolsOut = injectOpenaiTool(parsed.tools, [...(rulesActive ? [RULE_TOOL_OPENAI] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).openai] : []), ...(imageRescaleToolArmed(session) ? [READ_ORIGINAL_IMAGE_TOOL_OPENAI] : [])], surface?.toolPrompts, ccrEnabled(session), externalSummaryEnabled(config)); + toolsOut = injectOpenaiTool(parsed.tools, [...(absorbActive ? [absorbTools.openai] : []), ...(rulesActive ? [RULE_TOOL_OPENAI] : []), ...(ccrEnabled(session) ? [retrieveToolsFor(retrieveToolName(session)).openai] : []), ...(imageRescaleToolArmed(session) ? [READ_ORIGINAL_IMAGE_TOOL_OPENAI] : [])], surface?.toolPrompts, ccrEnabled(session), externalSummaryEnabled(config)); } // Nudge as a separate trailing user message (cache-friendly). Injected // in BOTH modes (#451): plugin agents supply the ACP tools but have no diff --git a/src/server/prepare-responses.ts b/src/server/prepare-responses.ts index ddbe952673..310f3c4756 100644 --- a/src/server/prepare-responses.ts +++ b/src/server/prepare-responses.ts @@ -1,8 +1,9 @@ import http from "node:http"; -import type { CompressionCore, Config, CoreMessage, NudgeDecision, PackSurface, Prompts } from "acp-kernel"; +import type { AbsorbConfig, CompressionCore, Config, CoreMessage, NudgeDecision, PackSurface, Prompts } from "acp-kernel"; import { defaultCountTokens, renderNudgeText, viableRanges } from "acp-kernel"; import { injectResponsesDeveloperMessage, type BiliMessage, type ResponseInputItem, type ResponsesProjection, type ResponsesRequestBody } from "acp-kernel/wire"; import type { CompressReasoningConfig } from "../reasoning-drop.js"; +import { resolveAbsorbSettings } from "../compress-settings.js"; import { resolveCompressProtocol, type CompressSettings, type ProxyOptions } from "../config.js"; import { currentCalibrationFactor, strippedResponseIdWarning } from "../util.js"; import { applyReasoningDrop, diagNudge, diagTagSummary, deriveTitle, effectiveTokenCount, imageBillingFor, imageReserveFor, imageTokenCapFor, isAutoInjectedNotification, planReasoningDrop, repairResponsesAssistantOrdering, reapOrphansLogged, runNudgeDecision, stripKernelSummaries, usageGradeInputBaseline, warnResponsesReasoningPairs, type Prepared } from "../server.js"; @@ -16,7 +17,8 @@ import { compressBreakerArmed } from "../stream.js"; import { coveredRealHistoryIds, foldCoverage, markDirty, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, REWRITE_MIN_INCOMING_TOTAL, snapshotMessages, type Session } from "../session.js"; import { carriesDshLocalCompactionSummary, DSH_LOCAL_COMPACTION_MIN_MISSING } from "./dsh-compaction-guard.js"; import { recordConflict } from "../conflict-watch.js"; -import { BILI_ACP_READONLY_TOOLS_RESPONSES, BILI_ACP_READONLY_TOOLS_RESPONSES_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, READ_ORIGINAL_IMAGE_TOOL_RESPONSES, RULE_TOOL_RESPONSES, buildAcpTagsOnlyPrompt, buildCompressHybridSystemPrompt, buildCompressSystemPrompt, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; +import { ABSORB_TOOL_NAME, BILI_ACP_READONLY_TOOLS_RESPONSES, BILI_ACP_READONLY_TOOLS_RESPONSES_NO_RANGE, BILI_ACP_TOOLS_RESPONSES, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, READ_ORIGINAL_IMAGE_TOOL_RESPONSES, RULE_TOOL_RESPONSES, absorbToolsFor, buildAbsorbSystemPrompt, buildAcpTagsOnlyPrompt, buildCompressHybridSystemPrompt, buildCompressSystemPrompt, retrieveToolsFor, withFirstSightDrain, withMarkerIntegrityNote, withSummaryBudgetNote, withProjectAttribution } from "../compress-tool.js"; +import { absorbToolName, applyAbsorbView, storeEffectiveAbsorb } from "../absorb.js"; import { adoptContentStore, ccrEnabled, ccrLoopConfig, contentStoreOf, imageRescaleToolArmed, retrieveToolName } from "../store.js"; import { rulesEnabled, storeEffectiveRules } from "../rules-feature.js"; import { autoFoldEngaged, externalSummaryEnabled, growthFoldingArmed } from "../external-summary-surface.js"; @@ -210,8 +212,14 @@ export async function prepareResponses( log("info", `[${sessionId}] input items: ${Array.isArray(parsed.input) ? parsed.input.map((i: ResponseInputItem) => i.type).join(",") : "(string)"}`); } const { tokens: tokenCount, source: tokenCountSource } = effectiveTokenCount(session, msgs, imageReserveFor(session, "responses", parsed, opts, billingUpstream ?? upstreamOrigin)); + // Absorb markers ride in the kernel's processTurn output (gated by + // config.absorb). The marker/text protocol has no native tool channel, + // so strip absorb from the loop config there (both modes). + const absorbBlock = effectiveAbsorbBlock(pluginMode, config, opts.compress.absorb); + const absorbTools = absorbToolsFor(absorbBlock?.toolName ?? ABSORB_TOOL_NAME); + const absorbActive = absorbBlock?.enabled === true && shouldInject && !isCompactionTrigger && !responsesTextProtocol; const rulesActive = rulesEnabled(config) && shouldInject && !isCompactionTrigger && !responsesTextProtocol; - const loopConfig = ccrLoopConfig(session, config); + const loopConfig = ccrLoopConfig(session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); // #2372/#2432: LOCAL auto-compaction — the client compacts on its own // and the call never transits the proxy; the first the proxy hears of // it is this request replaying [compaction summary, retained tail…]. @@ -285,7 +293,9 @@ export async function prepareResponses( const gap = foldCoverage(foldCoveredBefore, msgs.map((m) => m.id)); if (gap) log("warn", `[${sessionId}] [acp-drift] fold coverage mismatch: ${gap.matched}/${gap.expected} covered message id(s) present in resent history — ${gap.expected - gap.matched} covered id(s) missing from resent history — mutation (content edit invalidates content-hash refs, fold silently lost) or client-side deletion/truncation (benign, message no longer on the wire); observability complement to the #1328 overflow rescue (#1195)`); } + storeEffectiveAbsorb(session, loopConfig); storeEffectiveRules(session, config); + turn.messages = applyAbsorbView(turn.messages, session.state, loopConfig, tokenCount); turn.messages = attachSubagentSessions(turn.messages, session); // Drop sub-viability fragments before any consumer sees them: a tiny // range in the list makes batched compress attempts fail atomically @@ -338,6 +348,7 @@ export async function prepareResponses( if (compressSurfaceOn && !isCompactionTrigger && !knobNoCompressPrompt()) { const prompt = withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(responsesTextProtocol ? buildCompressHybridSystemPrompt(prompts, surface?.promptSections) : buildCompressSystemPrompt(prompts, surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)); const devParts = [...projection.systemParts, ...forgedSummaries, prompt]; + if (absorbActive) devParts.push(buildAbsorbSystemPrompt(absorbToolName(loopConfig))); const devContent = devParts.join("\n\n---\n\n"); responsesDevContent = devContent; if (forgedSummaries.length > 0) log("debug", `[${sessionId}] [inject] ${forgedSummaries.length} captured summary block(s) re-injected into developer message`); @@ -346,7 +357,7 @@ export async function prepareResponses( // #1712: decompress's startId/endId execute only on CCR-armed // sessions (#1179) — serve the no-range schema otherwise. const ccrOn = ccrEnabled(session); - const respExtra = [...(rulesActive ? [RULE_TOOL_RESPONSES] : []), ...(ccrOn ? [retrieveToolsFor(retrieveToolName(session)).responses] : []), ...(imageRescaleToolArmed(session) ? [READ_ORIGINAL_IMAGE_TOOL_RESPONSES] : [])]; + const respExtra = [...(absorbActive ? [absorbTools.responses] : []), ...(rulesActive ? [RULE_TOOL_RESPONSES] : []), ...(ccrOn ? [retrieveToolsFor(retrieveToolName(session)).responses] : []), ...(imageRescaleToolArmed(session) ? [READ_ORIGINAL_IMAGE_TOOL_RESPONSES] : [])]; toolsOut = responsesTextProtocol ? injectResponsesTool(parsed.tools, ccrOn ? BILI_ACP_READONLY_TOOLS_RESPONSES : BILI_ACP_READONLY_TOOLS_RESPONSES_NO_RANGE, surface?.toolPrompts, externalSummaryEnabled(config)) : injectResponsesTool(parsed.tools, respExtra.length > 0 ? [...(ccrOn ? BILI_ACP_TOOLS_RESPONSES : BILI_ACP_TOOLS_RESPONSES_NO_RANGE), ...respExtra] : (ccrOn ? BILI_ACP_TOOLS_RESPONSES : BILI_ACP_TOOLS_RESPONSES_NO_RANGE), surface?.toolPrompts, externalSummaryEnabled(config)); @@ -653,7 +664,10 @@ export function prepareResponsesCompact( let transformOk = false; try { const projection = responsesToCore(forgeBody); - const compactConfig = ccrLoopConfig(session, config); + // 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 = ccrLoopConfig(session, { ...config, absorb: undefined }); const turn = core.processTurn({ messages: projection.msgs, state: session.state, config: compactConfig, tokenCount: usageGradeInputBaseline(session), renderTags: knobRenderNone() ? "none" : "text-only", contentStore: contentStoreOf(session) }); session.state = turn.state; adoptContentStore(session, turn.contentStore); @@ -664,7 +678,7 @@ export function prepareResponsesCompact( session.contentStoreDirty = prevStoreDirty; return base; } - const viewed = turn.messages; + const viewed = applyAbsorbView(turn.messages, turn.state, compactConfig, usageGradeInputBaseline(session)); const processed = repairResponsesAssistantOrdering(stripKernelSummaries(viewed, turn.state), projection.msgs); let output = patchResponsesInput(projection, processed); if (typeof output === "string") { @@ -731,6 +745,9 @@ export function resolvePromptCacheKey( // per-request merged block (provider/model overrides apply); plugin lane uses // the base block so the manifest's advertised name and the gate's adjudicated // name always agree — provider/model absorb.* overrides are proxy-lane-only. +export function effectiveAbsorbBlock(pluginMode: boolean, config: Config, baseAbsorb?: CompressSettings["absorb"]): AbsorbConfig | undefined { + return pluginMode ? resolveAbsorbSettings(baseAbsorb) : config.absorb; +} // #2867: canonical incoming system/developer view of a Responses body — the // top-level instructions first (mirroring the kernel's responsesToCore diff --git a/src/server/relay.ts b/src/server/relay.ts index ae90f136dd..0297f5b5f2 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -6,6 +6,7 @@ import fs from "node:fs"; import path from "node:path"; import { type CompressionCore, type Config, type CoreMessage, defaultCountTokens, defaultPrompts, resolveOutputSteeringConfig } from "acp-kernel"; import { type BiliMessage } from "acp-kernel/wire"; +import { absorbToolName, applyAbsorbView } from "../absorb.js"; import { APIG_RESIGN_SCHEME, resignApig } from "../apig-resign.js"; import { credentialFingerprint, noteClientAbort, noteForwardedBody, noteForwardedImageFacts, noteInboundMessages, settleUsageReport } from "../cache-ledger.js"; import { warnCacheCollapse } from "../cache-warn.js"; @@ -14,7 +15,7 @@ import { applyCompatDropFields, dropCompatFieldsJson, resolveCompatDropFields } import { applyCompatRoles, applyCompatRolesJson, detectRoleRejection, detectSystemPlacementError, hasOffHeadSystem, resolveCompatRoles, type CompatRoles } from "../compat-roles.js"; import { compressLoopResponsesJson } from "../compress-loop-responses.js"; import { resolveCompress } from "../compress-settings.js"; -import { buildCompressHybridSystemPrompt, buildCompressSystemPrompt, withMarkerIntegrityNote, withProjectAttribution, withSummaryBudgetNote } from "../compress-tool.js"; +import { buildAbsorbSystemPrompt, buildCompressHybridSystemPrompt, buildCompressSystemPrompt, withMarkerIntegrityNote, withProjectAttribution, withSummaryBudgetNote } from "../compress-tool.js"; import { findRoute, resolveDeclaredProtocol, type ProxyOptions } from "../config.js"; import { makeContinuationRefetch } from "../degenerate-retry.js"; import { dumpRejectedBody } from "../error-dump.js"; @@ -52,6 +53,7 @@ import { applyOutputSteering, applyOutputSteeringJson } from "../output-steering import { estimateWireOverhead } from "./budget.js"; import { buildForwardHeaders, connectionNamedHeaders, RESPONSE_ONLY_STRIP_HEADERS, safeSessionId, UPSTREAM_HOP_HEADERS } from "./headers.js"; import { bodyDumpEnabled, logDumpFailure } from "./observability.js"; +import { effectiveAbsorbBlock } from "./prepare-responses.js"; import { awaitDrain, bufferToStream, dumpStreamToFile, pipeThrough, readStreamToBuffer } from "./stream-io.js"; import { buildForwardTarget, outboundPayloadBreakdown, registerRequestAbort, beginStreamKeepalive, effectiveTokenCount, googlePathKind, imageBillingFor, imageTokenCapFor, inboundResignCredential, outboundContextEstimates, repairResponsesAssistantOrdering, resolveUpstream, stripKernelSummaries, usageGradeInputBaseline, type Prepared } from "../server.js"; @@ -1492,13 +1494,30 @@ export async function forward( // Same absorb gate as prepare*: the section only exists where the // tool is callable, keeping loop re-requests byte-consistent with // the first request (prefix-cache anchor). - const loopConfig = ccrLoopConfig(prepared.session, config); + const absorbBlock = effectiveAbsorbBlock(prepared.pluginMode === true, config, opts.compress.absorb); + const absorbActive = absorbBlock?.enabled === true && opts.compress.injectTool && !textProtocol; + const loopConfig = ccrLoopConfig(prepared.session, { ...config, absorb: absorbActive ? absorbBlock : undefined }); + // Cache-seam: the compress<->absorb join must be byte-identical to + // the steady prepare* paths. Every steady wire joins the absorb + // section with a plain "\n\n" (sysParts.join / injectSystem parts + // join); the historical "---" divider here existed ONLY on round-2 + // and broke the system-element prefix on every fold while absorb + // was armed (probe: cache-seam-probes P1+P2, chat wire diverged at + // the absorb boundary char). + const absorbSection = absorbActive + ? `\n\n${buildAbsorbSystemPrompt(absorbToolName(loopConfig))}` + : ""; const visibilityMarkers = resolveCompress(opts.routes, route?.rewrittenUrl, (parsedReq as { model?: string }).model ?? prepared.model, opts.compress).visibilityMarkers ?? true; // #3298 cache-seam parity: every steady prepare* path appends the // project-attribution outermost, so the round-2 rebuild must too or // the head bytes drift at every fold. - const systemPrompt = withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)); - const adapter = pickAdapter(prepared.protocol, parsedReq, textProtocol, prepared.responsesProjection, prepared.anthropicSystem, prepared.openaiSystemText, undefined, prepared.google, prepared.systemNotes, opts.streamErrorShape, prepared.anthropicCacheMarks, prepared.anthropicClientCacheControls, false); + // absorbSection sits OUTSIDE withProjectAttribution: every steady + // prepare* path appends the attribution to the compress prompt and + // only THEN pushes the absorb section (sysParts order). Nesting it + // inside flipped the two sections on round-2 and broke the + // system-element prefix at every fold (cache-seam-probes P1+P2/P3). + const systemPrompt = withProjectAttribution(withMarkerIntegrityNote(withSummaryBudgetNote(textProtocol ? buildCompressHybridSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections) : buildCompressSystemPrompt(prepared.prompts ?? defaultPrompts, prepared.surface?.promptSections), externalSummaryEnabled(config)), visibilityMarkers)) + absorbSection; + const adapter = pickAdapter(prepared.protocol, parsedReq, textProtocol, prepared.responsesProjection, prepared.anthropicSystem, prepared.openaiSystemText, absorbActive ? absorbToolName(loopConfig) : undefined, prepared.google, prepared.systemNotes, opts.streamErrorShape, prepared.anthropicCacheMarks, prepared.anthropicClientCacheControls, absorbActive); const refreshFolded = async (current: CoreMessage[]): Promise => { return withSessionLock(prepared.session, async () => { // #422: mirror the prepare's fold with the post-compress state so @@ -1533,7 +1552,7 @@ export async function forward( // crossings — absorb prompts appeared/disappeared mid-history // and broke the prefix cache on every fold while the // threshold was being straddled (probe: cache-seam-probes). - const viewed = turn.messages; + const viewed = applyAbsorbView(turn.messages, turn.state, loopConfig, effectiveTokenCount(prepared.session, turn.messages).tokens); const records = current.filter((m) => typeof m.id === "string" && m.id.startsWith("acp_loop_")); // #1548: strip only when the compress call rides INBOUND history (client persists // it). Ephemeral acp_loop_* pairs are never re-sent by proxy-mode clients; stripping diff --git a/src/server/side-request.ts b/src/server/side-request.ts index e21555d7b8..0433e36494 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -3,7 +3,7 @@ import { estimateRawBodyTokens } from "../preflight.js"; import { imageTokensInParsedBody } from "../image-tokens.js"; import { reserveOutputHeadroom, shouldReserveOutputHeadroom, systemCarrierTexts } from "../util.js"; import { type ResolvedImageBilling } from "../image-tokens.js"; -import { ACP_TOOL_NAMES, RETRIEVE_TOOL_NAME, RULE_TOOL_NAME } from "acp-kernel"; +import { ACP_TOOL_NAMES, ABSORB_TOOL_NAME, RETRIEVE_TOOL_NAME, RULE_TOOL_NAME } from "acp-kernel"; import { READ_ORIGINAL_IMAGE_TOOL_NAME } from "../compress-tool.js"; // #388: side requests (title-gen etc.) share the main session key but must not @@ -186,6 +186,7 @@ export function resolveSideLane(s: SideLaneSignals): SideLaneDecision { // guaranteed bili-owned, and any host tool vetoes the demotion. export const BILI_TOOL_NAMES: ReadonlySet = new Set([ ...ACP_TOOL_NAMES, + ABSORB_TOOL_NAME, RULE_TOOL_NAME, RETRIEVE_TOOL_NAME, READ_ORIGINAL_IMAGE_TOOL_NAME, diff --git a/src/stream-google.ts b/src/stream-google.ts index 723493f4ce..54dc5dc18a 100644 --- a/src/stream-google.ts +++ b/src/stream-google.ts @@ -1,7 +1,7 @@ import type { CompressionCore, Config, CoreMessage } from "acp-kernel"; import type { GooglePart } from "acp-kernel/wire"; import type { Session } from "./session.js"; -import { isProxyToolFor } from "./proxy-tool.js"; +import { effectiveAbsorbConfig, isProxyToolFor } from "./absorb.js"; import { executeProxyToolAsync } from "./loop/core.js"; import { drainPendingRetrievals } from "./store.js"; import { runJsonRewriteAsync, type JsonToolCall, type RewriteCtx } from "./stream.js"; @@ -26,7 +26,7 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator[1]): { toolNames: string[]; tools: Record } { + let body = ""; + const res = { writeHead: () => {}, end: (b: string) => { body = b; } } as unknown as Parameters[0]; + handlePluginManifest(res, config); + return JSON.parse(body) as { toolNames: string[]; tools: Record }; +} + +function namesOnWire(wire: unknown[]): string[] { + return wire.map((t) => { + const o = t as Record; + return typeof o.name === "string" ? o.name : String((o.function as { name?: unknown } | undefined)?.name ?? ""); + }); +} + +// — manifest advertising follows base absorb.toolName (the #1359 core) — + +test("handlePluginManifest: absorb NOT advertised when disabled", () => { + const m = readManifest(defaultConfig(200_000)); + assert.ok(!m.toolNames.includes("absorb"), "disabled by default → not advertised"); +}); + +test("handlePluginManifest: enabled absorb with default name advertises 'absorb'", () => { + const m = readManifest({ ...defaultConfig(200_000), absorb: { ...DEFAULT_ABSORB_CONFIG, enabled: true } }); + assert.ok(m.toolNames.includes("absorb"), "default name advertised"); + for (const wire of ["anthropic", "openai", "responses"]) { + assert.ok(namesOnWire(m.tools[wire] ?? []).includes("absorb"), `${wire} carries 'absorb'`); + } +}); + +test("handlePluginManifest: base absorb.toolName rename is advertised, not the static name", () => { + const m = readManifest({ ...defaultConfig(200_000), absorb: { ...DEFAULT_ABSORB_CONFIG, enabled: true, toolName: "my_absorb" } }); + assert.ok(m.toolNames.includes("my_absorb"), "renamed tool advertised"); + assert.ok(!m.toolNames.includes("absorb"), "static name no longer advertised after rename"); + for (const wire of ["anthropic", "openai", "responses"]) { + const names = namesOnWire(m.tools[wire] ?? []); + assert.ok(names.includes("my_absorb"), `${wire} carries the renamed tool`); + assert.ok(!names.includes("absorb"), `${wire} must not carry the static name`); + } +}); + +// — per-session gate: the advertised (renamed) name is the only accepted one — + +test("gate: plugin-lane stamp makes the renamed name the sole accepted absorb tool", () => { + const base = defaultConfig(200_000); + const renamed: Config = { ...base, absorb: { ...DEFAULT_ABSORB_CONFIG, enabled: true, toolName: "my_absorb", minToolTokens: 1 } }; + const s = getSession(`t-absorb-plugin-${Math.random().toString(36).slice(2)}`); + storeEffectiveAbsorb(s, renamed); + assert.equal(isProxyToolFor("my_absorb", s, base), true, "renamed (advertised) name accepted"); + assert.equal(isProxyToolFor("absorb", s, base), false, "static name rejected after rename"); +}); + +// #3293 guard: the plugin-lane prompt/mark gate and the manifest advertisement must +// key off the SAME base absorb block, else a turn prompts for a tool the host never +// registered (the unknown-tool spin that got absorb deleted in #3309). Drift re-trips #3293. +test("#3293: plugin-lane absorb prompt gate ⟺ manifest advertisement (same base block)", () => { + const cases: Array<{ label: string; absorb?: CompressSettings["absorb"] }> = [ + { label: "unset (default-off)" }, + { label: "explicitly disabled", absorb: { enabled: false } }, + { label: "enabled default-name", absorb: { enabled: true } }, + { label: "enabled renamed", absorb: { enabled: true, toolName: "my_absorb" } }, + ]; + for (const c of cases) { + const compress = { injectTool: true, ...(c.absorb !== undefined ? { absorb: c.absorb } : {}) }; + // Mirror the two production sources that must stay in sync: admin.ts:336 (manifest) + // and prepare-responses.ts:741 (prompt gate). + const resolved = applyCompressSettings(defaultConfig(200_000), 200_000, compress as Parameters[2]); + const name = resolved.absorb?.toolName ?? DEFAULT_ABSORB_CONFIG.toolName; + const advertised = readManifest(resolved).toolNames.includes(name); + const promptGate = effectiveAbsorbBlock(true, resolved, c.absorb)?.enabled === true; + assert.equal(promptGate, advertised, `${c.label}: prompt gate (${promptGate}) must equal manifest advertisement (${advertised})`); + } +}); + +// — load-time per-field divergence diagnostic — + +test("findAbsorbPluginDivergences: aligned / unset levels report nothing", () => { + const u = "http://u"; + assert.deepEqual(findAbsorbPluginDivergences({ [u]: {} }, { enabled: true, minToolTokens: 100 }), []); + assert.deepEqual(findAbsorbPluginDivergences({ [u]: { compress: { absorb: undefined } } }, { enabled: true }), []); + assert.deepEqual(findAbsorbPluginDivergences({ [u]: { compress: { absorb: { enabled: true } } } }, { enabled: true }), []); +}); + +test("findAbsorbPluginDivergences: route-level rename diverges from base", () => { + const u = "http://u"; + const divs = findAbsorbPluginDivergences( + { [u]: { compress: { absorb: { enabled: true, toolName: "route_absorb" } } } }, + { enabled: true }, + ); + assert.equal(divs.length, 1, `expected exactly the toolName field, got ${JSON.stringify(divs)}`); + assert.match(divs[0]!, /toolName=\s*"route_absorb"/); +}); + +test("findAbsorbPluginDivergences: model-level field diverges; only fields the level SETS are reported", () => { + const u = "http://u"; + const divs = findAbsorbPluginDivergences( + { [u]: { models: { m1: { compress: { absorb: { minToolTokens: 999 } } } } } }, + { minToolTokens: 100, contextThresholdPct: 0.5, excludeTools: ["bash"] }, + ); + assert.equal(divs.length, 1, `only the set field (minToolTokens) diverges, got ${JSON.stringify(divs)}`); + assert.match(divs[0]!, /m1\.absorb\.minToolTokens=\s*999/); + assert.ok(!divs.join("").includes("contextThresholdPct"), "unset-at-level field not reported"); + assert.ok(!divs.join("").includes("excludeTools"), "unset-at-level field not reported"); +}); + +test("findAbsorbPluginDivergences: percent form normalizes to the fraction form", () => { + const u = "http://u"; + assert.deepEqual( + findAbsorbPluginDivergences({ [u]: { compress: { absorb: { contextThresholdPct: "80%" } } } }, { contextThresholdPct: 0.8 }), + [], + "'80%' == 0.8 → no divergence", + ); + const divs = findAbsorbPluginDivergences({ [u]: { compress: { absorb: { contextThresholdPct: "90%" } } } }, { contextThresholdPct: 0.8 }); + assert.equal(divs.length, 1, "'90%' != 0.8 → divergence"); +}); + +// — e2e lane governance (real server through the proxy) — + +type Rig = { proxyPort: number; upstreamPort: number; forwards: string[]; proxy: http.Server; upstream: http.Server }; + +function okJson(): string { + return JSON.stringify({ + id: "chatcmpl-1", + object: "chat.completion", + choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], + usage: { prompt_tokens: 10, completion_tokens: 3, total_tokens: 13 }, + }); +} + +async function startRig(baseCompress: Record, routeCompress?: Record): Promise { + const forwards: string[] = []; + const upstream = http.createServer((req, res) => { + let b = ""; + req.on("data", (c) => (b += c)); + req.on("end", () => { + forwards.push(b); + res.writeHead(200, { "content-type": "application/json" }); + res.end(okJson()); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as AddressInfo).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const url = `http://127.0.0.1:${upstreamPort}`; + const routes = routeCompress ? { [url]: { compress: routeCompress } } : { [url]: {} }; + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes, + modelContextLimit: 200_000, + kernelConfig: defaultConfig(200_000), + compress: baseCompress, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + } as unknown as ProxyOptions); + await once(proxy, "listening"); + return { proxyPort: (proxy.address() as AddressInfo).port, upstreamPort, forwards, proxy, upstream }; +} + +async function closeRig(rig: Rig): Promise { + rig.proxy.close(); + rig.proxy.closeIdleConnections?.(); + await once(rig.proxy, "close"); + rig.upstream.close(); + rig.upstream.closeIdleConnections?.(); + await once(rig.upstream, "close"); +} + +async function post(rig: Rig, convId: string, pluginMode: boolean, messages: unknown[]): Promise { + const headers: Record = { "content-type": "application/json", "x-acp-session": convId }; + if (pluginMode) headers["x-bili-plugin"] = "test-agent"; + const res = await fetch(`http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`, { + method: "POST", + headers, + body: JSON.stringify({ model: MODEL, max_tokens: 64_000, messages }), + }); + const txt = await res.text(); + if (res.status !== 200) throw new Error(`proxy returned ${res.status}: ${txt}`); +} + +const SIMPLE_MSGS = (): unknown[] => [ + { role: "user", content: "hello" }, + { role: "assistant", content: "hi there" }, +]; + +test("e2e: base rename → plugin lane advertises, stamps, and executes the renamed tool", async () => { + const baseCompress = { injectTool: true, injectNudge: false, absorb: { enabled: true, toolName: "my_absorb", minToolTokens: 1 } }; + const rig = await startRig(baseCompress); + try { + // Manifest (built from the base config, as server.ts does) advertises the rename. + const m = readManifest(applyCompressSettings(defaultConfig(200_000), 200_000, baseCompress as Parameters[2])); + assert.ok(m.toolNames.includes("my_absorb"), "manifest advertises the base-renamed tool"); + + // A plugin-mode turn stamps the session with the base (renamed) name. + await post(rig, "abs-e2e-a", true, SIMPLE_MSGS()); + const sess = listSessions().find((s) => effectiveAbsorbConfig(s, defaultConfig(200_000))?.enabled === true); + assert.ok(sess, "a session armed absorb"); + assert.equal(effectiveAbsorbConfig(sess!, defaultConfig(200_000))?.toolName, "my_absorb", "plugin lane stamps the base (renamed) name"); + + // THE core repro: the advertised tool runs through the REAL plugin endpoint + // (pre-fix it 400'd because the gate expected the renamed name while the + // manifest advertised the static one). + const tr = await fetch(`http://127.0.0.1:${rig.proxyPort}/__bili/plugin/tool`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ conversationId: "abs-e2e-a", tool: "my_absorb", args: { ref: "m00001", summary: "x" } }), + }); + const tj = JSON.parse(await tr.text()) as { ok: boolean; error?: string }; + assert.equal(tr.status, 200, `advertised tool must not 400; got ${tr.status}: ${JSON.stringify(tj)}`); + assert.equal(tj.ok, true, "gate accepts the advertised (renamed) absorb tool"); + } finally { + await closeRig(rig); + } +}); + +test("e2e: route rename → plugin lane stays on base, proxy lane honors the override", async () => { + const baseCompress = { injectTool: true, injectNudge: false, absorb: { enabled: true, minToolTokens: 1 } }; + const routeCompress = { absorb: { enabled: true, toolName: "route_absorb", minToolTokens: 1 } }; + const rig = await startRig(baseCompress, routeCompress); + try { + // Plugin lane: the route override must NOT leak into the base-governed stamp. + const beforeP = new Set(listSessions().map((s) => s.id)); + await post(rig, "abs-e2e-b1", true, SIMPLE_MSGS()); + const pSess = listSessions().find((s) => !beforeP.has(s.id)); + assert.ok(pSess, "plugin turn created a session"); + assert.equal(effectiveAbsorbConfig(pSess!, defaultConfig(200_000))?.toolName, "absorb", "plugin lane follows the BASE name, ignoring the route override"); + + // Proxy lane: the merged (route) name governs, and the injected schema carries it. + const beforeX = new Set(listSessions().map((s) => s.id)); + await post(rig, "abs-e2e-b2", false, SIMPLE_MSGS()); + const xSess = listSessions().find((s) => !beforeX.has(s.id)); + assert.ok(xSess, "proxy turn created a session"); + assert.equal(effectiveAbsorbConfig(xSess!, defaultConfig(200_000))?.toolName, "route_absorb", "proxy lane honors the route override"); + const lastFwd = rig.forwards[rig.forwards.length - 1]!; + assert.ok(lastFwd.includes('"route_absorb"'), "proxy wire injects the renamed absorb tool"); + } finally { + await closeRig(rig); + } +}); diff --git a/tests/absorb.test.ts b/tests/absorb.test.ts new file mode 100644 index 0000000000..5ff7be1c0d --- /dev/null +++ b/tests/absorb.test.ts @@ -0,0 +1,273 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { + createCore, + createInitialState, + defaultConfig, + DEFAULT_ABSORB_CONFIG, + type AbsorbConfig, + type AbsorbRecord, + type Config, + type CoreMessage, +} from "acp-kernel"; +import { + applyAbsorbView, + absorbEnabled, + absorbToolName, + effectiveAbsorbConfig, + executeAbsorb, + isProxyToolFor, + storeEffectiveAbsorb, +} from "../src/absorb.ts"; +import { mergeCompress, applyCompressSettings } from "../src/compress-settings.ts"; +import { parseCompressSettings } from "../src/config.ts"; +import { handlePluginManifest } from "../src/plugin.ts"; +import { getSession, type Session } from "../src/session.ts"; +import { SessionStore } from "../src/persist.ts"; + +const BIG_TEXT = "line of build output ".repeat(700); // ~3.7K tokens + +function freshMsgs(): CoreMessage[] { + return [ + { id: "u1", role: "user", contentType: "text", text: "run a big build" }, + { id: "a-tc", role: "assistant", contentType: "tool-call", toolName: "bash", toolCallId: "call_1", text: JSON.stringify({ command: "npm test" }) }, + { id: "t-res", role: "tool", contentType: "tool-result", toolCallId: "call_1", text: BIG_TEXT }, + ]; +} + +function makeTurn() { + const core = createCore(); + const msgs = freshMsgs(); + // mediaOnly off: these host-plumbing tests exercise the credit/view + // mechanism over a TEXT result; the #3402 media-scope gate is covered separately. + const config: Config = { ...defaultConfig(200000), absorb: { ...DEFAULT_ABSORB_CONFIG, enabled: true, minToolTokens: 100, mediaOnly: false } }; + const turn = core.processTurn({ messages: msgs, state: createInitialState(), config, tokenCount: 0, renderTags: "text-only" }); + const byRef = Object.entries(turn.state.messageRefs.byRef ?? {}); + const refFor = (rawId: string): string => byRef.find(([, raw]) => raw === rawId)?.[0] ?? ""; + return { core, turn, config, refFor }; +} + +function makeSession(): Session { + const session = getSession(`t-absorb-${Math.random().toString(36).slice(2)}`); + session.stats.lastInputTokens = 100_000; + return session; +} + +test("absorbEnabled / absorbToolName respect config", () => { + assert.equal(absorbEnabled(defaultConfig(200000)), false); + assert.equal(absorbEnabled({ ...defaultConfig(200000), absorb: { ...DEFAULT_ABSORB_CONFIG, enabled: true } }), true); + assert.equal(absorbToolName({ ...defaultConfig(200000), absorb: { ...DEFAULT_ABSORB_CONFIG, enabled: true, toolName: "acp_absorb" } }), "acp_absorb"); + assert.equal(absorbToolName(defaultConfig(200000)), "absorb"); +}); + +test("isProxyToolFor: static ACP names always proxy; absorb only when effectively enabled", () => { + const base = defaultConfig(200000); + assert.equal(isProxyToolFor("compress", undefined, base), true); + assert.equal(isProxyToolFor("absorb", undefined, base), false); + assert.equal(isProxyToolFor("bash", undefined, base), false); + + const on: Config = { ...base, absorb: { ...DEFAULT_ABSORB_CONFIG, enabled: true } }; + assert.equal(isProxyToolFor("absorb", undefined, on), true); + + const session = getSession(`t-absorb-adjudicate-${Math.random().toString(36).slice(2)}`); + storeEffectiveAbsorb(session, on); + // Plugin tool API reads the per-session stored block even when the fallback + // (base kernel config) has the feature off. + assert.equal(isProxyToolFor("absorb", session, base), true); + assert.equal(isProxyToolFor("compress", session, base), true); + + storeEffectiveAbsorb(session, base); + assert.equal(isProxyToolFor("absorb", session, on), false); + + const renamed: Config = { ...base, absorb: { ...DEFAULT_ABSORB_CONFIG, enabled: true, toolName: "acp_absorb" } }; + assert.equal(isProxyToolFor("acp_absorb", undefined, renamed), true); + assert.equal(isProxyToolFor("absorb", undefined, renamed), false); +}); + +test("effectiveAbsorbConfig: session metadata wins over fallback", () => { + const on: AbsorbConfig = { ...DEFAULT_ABSORB_CONFIG, enabled: true, minToolTokens: 555 }; + const session = getSession(`t-absorb-meta-${Math.random().toString(36).slice(2)}`); + storeEffectiveAbsorb(session, { ...defaultConfig(200000), absorb: on }); + assert.deepEqual(effectiveAbsorbConfig(session, defaultConfig(200000)), on); + // defaultConfig() ships the kernel-default (disabled) block, so storing a + // plain base config records "absorb off" for this session and shadows a + // more permissive fallback. + storeEffectiveAbsorb(session, defaultConfig(200000)); + const eff = effectiveAbsorbConfig(session, { ...defaultConfig(200000), absorb: on }); + assert.equal(eff?.enabled, false); +}); + +test("executeAbsorb: success records absorption, credits net exactly once", () => { + const { turn, config, refFor } = makeTurn(); + const ref = refFor("t-res"); + assert.ok(ref, "tool result must have a ref"); + const session = makeSession(); + session.state = turn.state; + const before = session.stats.compressCreditTokens ?? 0; + const result = executeAbsorb({ ref, summary: "Build passed; 42 tests ok." }, "call_x", config.absorb!, { + config, + messages: turn.messages, + session, + }).text; + assert.ok(!result.includes("FAILED"), result); + assert.match(result, /^absorbed m\d+/); + assert.equal(session.state.absorbed?.length, 1); + const record = session.state.absorbed![0]; + assert.ok(record.tokensReclaimed > 0); + assert.equal(session.state.stats.absorbedTokens, record.tokensReclaimed); + assert.equal(session.stats.compressCreditTokens, before + record.tokensReclaimed); + assert.equal(session.stats.lastInputTokens, 100_000 - record.tokensReclaimed); + + // Re-absorbing the same ref is an ok=true no-op and must NOT credit again. + const again = executeAbsorb({ ref, summary: "retry" }, "call_y", config.absorb!, { config, messages: turn.messages, session }).text; + assert.ok(!again.includes("FAILED"), again); + assert.match(again, /already absorbed/); + assert.equal(session.state.absorbed?.length, 1); + assert.equal(session.stats.compressCreditTokens, before + record.tokensReclaimed); +}); + +test("executeAbsorb: bad ref and invalid input fail with FAILED marker", () => { + const { turn, config } = makeTurn(); + const session = makeSession(); + session.state = turn.state; + const badRef = executeAbsorb({ ref: "m99999", summary: "x" }, undefined, config.absorb!, { config, messages: turn.messages, session }).text; + assert.match(badRef, /\[absorb FAILED: absorb failed: ref m99999/); + const badInput = executeAbsorb({ summary: "no ref" }, undefined, config.absorb!, { config, messages: turn.messages, session }).text; + assert.match(badInput, /\[absorb FAILED: invalid input/); + assert.equal((session.state.absorbed ?? []).length, 0); +}); + +test("kernel gating: prompts honor minToolTokens/contextThresholdPct at processTurn time", () => { + // Gates are evaluated when processTurn renders the turn, so each scenario + // needs its own turn computed under that config (reusing one rendered view + // across configs would see markers already baked in). + const run = (absorb: AbsorbConfig, tokenCount = 100_000): boolean => { + const core = createCore(); + const cfg: Config = { ...defaultConfig(200000), absorb }; + const turn = core.processTurn({ messages: freshMsgs(), state: createInitialState(), config: cfg, tokenCount, renderTags: "text-only" }); + return turn.messages.some((m) => (m.text ?? "").includes("[ACP absorb]")); + }; + // mediaOnly pinned off: these scenarios isolate the token/context gates; + // the #3402 media-scope gate is covered separately in the kernel suite. + const A = { ...DEFAULT_ABSORB_CONFIG, mediaOnly: false }; + assert.equal(run({ ...A, enabled: true, minToolTokens: 100 }), true, "large eligible result is prompted"); + assert.equal(run({ ...A, enabled: true, minToolTokens: 100_000 }), false, "minToolTokens gate"); + assert.equal(run({ ...A, enabled: true, minToolTokens: 100, contextThresholdPct: 0.8 }), false, "below context threshold (50% usage)"); + assert.equal(run({ ...A, enabled: true, minToolTokens: 100, contextThresholdPct: 0.8 }, 180_000), true, "above context threshold (90% usage)"); + // excludeTools is a known no-op on tool RESULTS: the wire projections build + // result CoreMessages without toolName, so isAbsorbCandidate's name guard + // never fires (ranxianglei/acp-kernel#213). Asserting current behavior so a + // kernel fix flips this line red instead of silently changing semantics. + assert.equal(run({ ...A, enabled: true, minToolTokens: 100, excludeTools: ["bash"] }), true, "excluded tool name (kernel limitation)"); + assert.equal(run({ ...A, enabled: false, minToolTokens: 100 }), false, "feature disabled"); +}); + +test("applyAbsorbView: hides recorded pairs even after the feature is disabled", () => { + const { turn, config, refFor } = makeTurn(); + const session = makeSession(); + session.state = turn.state; + const ref = refFor("t-res"); + const result = executeAbsorb({ ref, summary: "short" }, undefined, config.absorb!, { config, messages: turn.messages, session }).text; + assert.ok(!result.includes("FAILED"), result); + const hidden = applyAbsorbView(turn.messages, session.state, defaultConfig(200000), 100_000); + assert.deepEqual(hidden.map((m) => m.id), ["u1"], "both halves of the absorbed pair are hidden"); + assert.ok(!hidden.some((m) => (m.text ?? "").includes("[ACP absorb]"))); +}); + +test("mergeCompress: absorb sub-fields merge deepest-wins like other nested fields", () => { + const merged = mergeCompress( + { absorb: { enabled: true, minToolTokens: 500 } }, + { absorb: { contextThresholdPct: "75%" } }, + { absorb: { excludeTools: ["bash"] } }, + ); + assert.deepEqual(merged.absorb, { enabled: true, minToolTokens: 500, contextThresholdPct: "75%", excludeTools: ["bash"] }); + const partial = mergeCompress({ absorb: { enabled: true } }, undefined, { absorb: { toolName: "x" } }); + assert.deepEqual(partial.absorb, { enabled: true, toolName: "x" }); + assert.equal(mergeCompress(undefined, undefined, undefined).absorb, undefined); +}); + +test("applyCompressSettings: maps settings absorb onto kernel AbsorbConfig with defaults + percent parse", () => { + const base = defaultConfig(200000); + const out = applyCompressSettings(base, 200_000, { absorb: { enabled: true, contextThresholdPct: "80%" } }); + assert.deepEqual(out.absorb, { + enabled: true, + toolName: "absorb", + minToolTokens: 4000, + contextThresholdPct: 0.8, + excludeTools: [], + mediaOnly: true, + }); + const numeric = applyCompressSettings(base, 200_000, { absorb: { enabled: false, minToolTokens: 250, contextThresholdPct: 0.5 } }); + assert.equal(numeric.absorb?.enabled, false); + assert.equal(numeric.absorb?.minToolTokens, 250); + assert.equal(numeric.absorb?.contextThresholdPct, 0.5); + const absent = applyCompressSettings(base, 200_000, {}); + assert.deepEqual(absent.absorb, base.absorb, "absent settings leave the base absorb block untouched"); +}); + +test("parseCompressSettings: validates absorb sub-object field-by-field, rejects whole block on any bad field", () => { + const good = parseCompressSettings({ absorb: { enabled: true, minToolTokens: 100, contextThresholdPct: "75%", excludeTools: ["bash"], toolName: "abs" } }); + assert.deepEqual(good?.absorb, { enabled: true, minToolTokens: 100, contextThresholdPct: "75%", excludeTools: ["bash"], toolName: "abs" }); + assert.equal(parseCompressSettings({ absorb: { enabled: "yes" } }), undefined); + assert.equal(parseCompressSettings({ absorb: { minToolTokens: "big" } }), undefined); + assert.equal(parseCompressSettings({ absorb: { contextThresholdPct: "abc" } }), undefined); + assert.equal(parseCompressSettings({ absorb: { excludeTools: "bash" } }), undefined); + assert.equal(parseCompressSettings({ absorb: { toolName: "" } }), undefined); + assert.equal(parseCompressSettings({ absorb: { mediaOnly: false } })?.absorb?.mediaOnly, false); + assert.equal(parseCompressSettings({ absorb: { mediaOnly: "yes" } }), undefined); + assert.equal(parseCompressSettings({ absorb: [] }), undefined); + // A bad absorb field rejects sibling fields too (whole-block convention). + assert.equal(parseCompressSettings({ tiers: false, absorb: { enabled: 1 } }), undefined); +}); + +// #1192: hosts register manifest tools verbatim, so a disabled absorb must not +// be advertised at all — only an absorb-enabled config may list it. +test("handlePluginManifest: absorb advertised on all three wires only when enabled", () => { + let body = ""; + const res = { writeHead: () => {}, end: (b: string) => { body = b; } } as unknown as Parameters[0]; + const data = (): { + toolNames: string[]; + tools: { anthropic: { name: string; input_schema?: unknown }[]; openai: { name?: string; function?: { name: string } }[]; responses: { name?: string; type?: string }[] }; + } => JSON.parse(body); + + handlePluginManifest(res, defaultConfig(200_000)); + let d = data(); + assert.ok(!d.toolNames.includes("absorb"), "disabled by default → not advertised"); + assert.ok(!d.tools.anthropic.some((t) => t.name === "absorb")); + assert.ok(!d.tools.openai.some((t) => t.function?.name === "absorb")); + assert.ok(!d.tools.responses.some((t) => t.name === "absorb")); + + handlePluginManifest(res, { ...defaultConfig(200_000), absorb: { ...DEFAULT_ABSORB_CONFIG, enabled: true } }); + d = data(); + assert.ok(d.toolNames.includes("absorb")); + const anthro = d.tools.anthropic.find((t) => t.name === "absorb"); + assert.ok(anthro, "anthropic schema present"); + const schema = anthro!.input_schema as { required?: string[] }; + assert.deepEqual(schema.required, ["ref", "summary"]); + assert.ok(d.tools.openai.some((t) => t.function?.name === "absorb")); + assert.ok(d.tools.responses.some((t) => t.name === "absorb")); +}); + +test("persist round-trip: absorbed records and absorbedTokens survive save/load", () => { + const dir = mkdtempSync(path.join(tmpdir(), "bili-absorb-")); + const store = new SessionStore({ dir, debounceMs: 0 }); + const session = getSession(`t-absorb-persist-${Math.random().toString(36).slice(2)}`, { protocol: "anthropic" }); + const record: AbsorbRecord = { + toolCallId: "call_1", + callMessageId: "a-tc", + resultMessageId: "t-res", + summary: "Build passed.", + tokensReclaimed: 4321, + createdAt: Date.now(), + }; + session.state.absorbed = [record]; + session.state.stats.absorbedTokens = 4321; + assert.equal(store.flushSync(session), true); + const loaded = store.loadSync(session.id, { protocol: "anthropic" }); + assert.ok(loaded, "session must reload"); + assert.deepEqual(loaded!.state.absorbed, [record], "absorbed records must survive restart"); + assert.equal(loaded!.state.stats.absorbedTokens, 4321); +}); diff --git a/tests/cache-seam-fuzz.test.ts b/tests/cache-seam-fuzz.test.ts index 849ebe16c7..25058fc6f1 100644 --- a/tests/cache-seam-fuzz.test.ts +++ b/tests/cache-seam-fuzz.test.ts @@ -41,6 +41,7 @@ interface Scenario { thresholdBytes: number; plan: Array; gapless: boolean; + absorbPct: number; ccr: boolean; toolsOnClient: boolean; } @@ -69,6 +70,7 @@ function genScenario(rnd: () => number, seed: number): Scenario { thresholdBytes: (34 + Math.floor(rnd() * 40)) * 1024, plan, gapless: rnd() < 0.5, + absorbPct: rnd() < 0.5 ? 0.05 : 0, ccr: rnd() < 0.5, toolsOnClient: rnd() < 0.6, }; @@ -179,6 +181,7 @@ async function drive(sc: Scenario, sessionId: string): Promise { compress: { injectTool: true, injectNudge: true, + absorb: { enabled: true, contextThresholdPct: sc.absorbPct }, ...(sc.ccr ? { ccr: { enabled: true, minToolTokens: 50 } } : {}), }, promptCache: { routing: "auto" }, diff --git a/tests/cache-seam-probes.test.ts b/tests/cache-seam-probes.test.ts index 6e9f902445..446fe23c43 100644 --- a/tests/cache-seam-probes.test.ts +++ b/tests/cache-seam-probes.test.ts @@ -7,6 +7,11 @@ // P1 tools array — the tools list is hashed into the provider prefix; any // per-request tool-list mutation breaks the cache at // element 0. (Also pinned matrix-wide via normOf.) +// P2 absorb view — with absorb.contextThresholdPct > 0 the absorb-prompt +// gate is token-count-fed; round-2 must use the SAME +// source as the steady paths or prompts flicker +// mid-history across every fold while the session +// straddles the threshold (#1592 family). // P3 CCR armed — with compress.ccr.enabled the oversized tool result // rides the wire as a placeholder; the placeholder element // must stay byte-identical across folds (round-2 and the @@ -29,6 +34,7 @@ type Item = Record; type Msg = Record; const THRESHOLD = 60 * 1024; +const ABSORB_PROMPT_FRAGMENT = "[ACP absorb]"; const CCR_PLACEHOLDER_FRAGMENT = "[acp-stored"; const FILLER = (seed: number, kb: number): string => { @@ -108,6 +114,7 @@ function startUpstream(captured: string[], plan: Array): htt } interface RigOpts { + absorbThresholdPct?: number; ccr?: boolean; turns?: number; bigToolFrom?: number; @@ -136,6 +143,7 @@ async function driveChat(sessionId: string, opts: RigOpts): Promise { compress: { injectTool: true, injectNudge: true, + absorb: { enabled: true, contextThresholdPct: opts.absorbThresholdPct ?? 0 }, ...(opts.ccr === true ? { ccr: { enabled: true, minToolTokens: 50 } } : {}), }, promptCache: { routing: "auto" }, @@ -228,12 +236,12 @@ function assertPairSeamStable(prev: string, cur: string, label: string): void { const emergencyTrunc = JSON.stringify(mb[k] ?? "").includes("[truncated for context space]"); assert.ok( k >= solidA || summaryNew || emergencyTrunc, - `${label}: mid-history divergence at message[${k}] of ${ma.length} (round2=${isRound2(b)}) — CCR view flipped between adjacent requests`, + `${label}: mid-history divergence at message[${k}] of ${ma.length} (round2=${isRound2(b)}) — absorb/CCR view flipped between adjacent requests`, ); } -test("probe P1: tools array byte-stable across folds (estimate-grade session)", { timeout: 180_000 }, async () => { - const bodies = await driveChat("seam-p2", {}); +test("probe P1+P2: tools stable and absorb prompts do not flicker across folds (estimate-grade session, threshold 5%)", { timeout: 180_000 }, async () => { + const bodies = await driveChat("seam-p2", { absorbThresholdPct: 0.05 }); assert.ok(bodies.length >= 10, `expected >= 10 requests, got ${bodies.length}`); let folds = 0; for (let i = 1; i < bodies.length; i++) { diff --git a/tests/ccr-v2.test.ts b/tests/ccr-v2.test.ts index 37bab7da07..3785542780 100644 --- a/tests/ccr-v2.test.ts +++ b/tests/ccr-v2.test.ts @@ -64,7 +64,7 @@ function fold(opts: { ccr: boolean; preStore?: MessageContentStore }): FoldResul const config = applyCompressSettings( defaultConfig(200_000), 200_000, - { ccr: opts.ccr ? { enabled: true, minToolTokens: 50 } : { enabled: false, minToolTokens: 50 } }, + { absorb: { enabled: true, minToolTokens: 50 }, ccr: opts.ccr ? { enabled: true, minToolTokens: 50 } : { enabled: false, minToolTokens: 50 } }, ) as Config; const session = getSession(`ccrv2-${Math.random().toString(36).slice(2)}`); if (opts.ccr) storeEffectiveCcr(session, { enabled: true, minToolTokens: 50 }); diff --git a/tests/fix-1897-leaked-tools-side.test.ts b/tests/fix-1897-leaked-tools-side.test.ts index 93a575c639..9d98a01a2a 100644 --- a/tests/fix-1897-leaked-tools-side.test.ts +++ b/tests/fix-1897-leaked-tools-side.test.ts @@ -5,7 +5,7 @@ import { once } from "node:events"; import { defaultConfig, createInitialState } from "acp-kernel"; import { startServer, stripLeakedBiliTools, BILI_TOOL_NAMES } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; -import { PROXY_TOOL_NAMES, RETRIEVE_TOOL_NAME, RULE_TOOL_NAME } from "../src/compress-tool.ts"; +import { PROXY_TOOL_NAMES, ABSORB_TOOL_NAME, RETRIEVE_TOOL_NAME, RULE_TOOL_NAME } from "../src/compress-tool.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { getSession, _resetSessionsForTest } from "../src/session.ts"; @@ -59,9 +59,9 @@ test("stripLeakedBiliTools: google functionDeclarations (one entry may declare m }); test("stripLeakedBiliTools: opt-in bili extras count as bili-owned", () => { - const body = { model: "m", tools: [openAiTool(RULE_TOOL_NAME), openAiTool(RETRIEVE_TOOL_NAME)] }; + const body = { model: "m", tools: [openAiTool(ABSORB_TOOL_NAME), openAiTool(RULE_TOOL_NAME), openAiTool(RETRIEVE_TOOL_NAME)] }; assert.equal(stripLeakedBiliTools(body), true); - for (const n of [RULE_TOOL_NAME, RETRIEVE_TOOL_NAME]) { + for (const n of [ABSORB_TOOL_NAME, RULE_TOOL_NAME, RETRIEVE_TOOL_NAME]) { assert.ok(BILI_TOOL_NAMES.has(n), `${n} in the bili set`); } }); diff --git a/tests/fix-decompress-tofile.test.ts b/tests/fix-decompress-tofile.test.ts index 0441c30860..bbdf6bdcfa 100644 --- a/tests/fix-decompress-tofile.test.ts +++ b/tests/fix-decompress-tofile.test.ts @@ -134,7 +134,7 @@ function foldCcr() { const config = applyCompressSettings( defaultConfig(200_000), 200_000, - { ccr: { enabled: true, minToolTokens: 50 } }, + { absorb: { enabled: true, minToolTokens: 50 }, ccr: { enabled: true, minToolTokens: 50 } }, ) as Config; const session = getSession(`tfrange-${Math.random().toString(36).slice(2)}`); storeEffectiveCcr(session, { enabled: true, minToolTokens: 50 }); diff --git a/tests/fork-ccr-adoption.test.ts b/tests/fork-ccr-adoption.test.ts index b40133efac..66617304f8 100644 --- a/tests/fork-ccr-adoption.test.ts +++ b/tests/fork-ccr-adoption.test.ts @@ -54,7 +54,7 @@ function ccrConfig(on: boolean): Config { // whole fold range nondeterministically. defaultConfig(LIMIT, { preserveRecentMessages: 0, preserveRecentTokens: 0, compress: { minCompressRange: 10, minSummaryLength: 20, maxSummaryLength: 5000 } }), LIMIT, - { ccr: { enabled: on, minToolTokens: 50 } }, + { absorb: { enabled: true, minToolTokens: 50 }, ccr: { enabled: on, minToolTokens: 50 } }, ) as Config; } diff --git a/tests/golden/storage-render/anthropic.json b/tests/golden/storage-render/anthropic.json index 699bd64cba..258b7a7879 100644 --- a/tests/golden/storage-render/anthropic.json +++ b/tests/golden/storage-render/anthropic.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "7975db3b6782fc36a06cabe7f70d6c0813a26254ea16b1bfdfa712482cf1a18b", + "stateDigest": "00a49d95b7ffd5e5c76e9f17d1d5405293aa1a7d08e4ed7fa778b77b5e8cb788", "sectionDigests": { "state": "fae14b25a41f5844", "blockContents": "dc7d13d250c94bb4", "stats": "cff3c2546898aeb3", - "metadata": "b04f2fb422a5529b" + "metadata": "5413c879755acf18" }, "stateKeyDigests": { "blocks": "1cbe172309e4490d", diff --git a/tests/golden/storage-render/chat.json b/tests/golden/storage-render/chat.json index 8c467cd788..3af63534ec 100644 --- a/tests/golden/storage-render/chat.json +++ b/tests/golden/storage-render/chat.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "b9621b1ad7556db259a9f03d3c37df81e77668fcc0c2be78513f066ea06dc750", + "stateDigest": "7453eabb99ead81805a1bf2d6630232df2e02ab10772b7b03dd88495cfc90d17", "sectionDigests": { "state": "d5bc5306f840723d", "blockContents": "94a58f96d7c5cfc3", "stats": "59842ce5415d3a4d", - "metadata": "b323123d4fc029f7" + "metadata": "d05f9d95a0dd2fce" }, "stateKeyDigests": { "blocks": "9aa73e5fb9f93d6c", diff --git a/tests/golden/storage-render/google.json b/tests/golden/storage-render/google.json index ec0df6c804..d76d38a238 100644 --- a/tests/golden/storage-render/google.json +++ b/tests/golden/storage-render/google.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "842b1818adac8210db66fd5cc923a4e0e8788e1209d8457cd4d167b1c45ae8e1", + "stateDigest": "48c0fbd96f53485e9b8109ae7e24cc1cf7d0b0a265591f7f4e0b04689dbbdb86", "sectionDigests": { "state": "2a2af1c92fcb4f88", "blockContents": "473264d9df8ba338", "stats": "660f1797bf2044c4", - "metadata": "a97a1e2bf9f13c94" + "metadata": "4d073f1e812dcb89" }, "stateKeyDigests": { "blocks": "8e14f0879bf7ebd7", diff --git a/tests/golden/storage-render/responses.json b/tests/golden/storage-render/responses.json index 55ba150bf4..a7cd175ed7 100644 --- a/tests/golden/storage-render/responses.json +++ b/tests/golden/storage-render/responses.json @@ -80,12 +80,12 @@ "foldAt": 9, "editedAt": 12, "summaryFrom": 9, - "stateDigest": "918b8cef2fc95712c66910e344b5e7e132f2500906c1151a7bdea1d84c2602e9", + "stateDigest": "05efdc2df5d4ef3a4d9f8f3c98de81668dc33eb81d2e3300b18dde92d9bdb54d", "sectionDigests": { "state": "6546d2e5f2826284", "blockContents": "8460729b2654a816", "stats": "e3a3aeb598c183ff", - "metadata": "15c1da97b893af8c" + "metadata": "44e07f67bbeb366b" }, "stateKeyDigests": { "blocks": "7fff1dfb54a617cf", diff --git a/tests/golden/wire-contract/bili.json b/tests/golden/wire-contract/bili.json index eef6e13d04..95b5629723 100644 --- a/tests/golden/wire-contract/bili.json +++ b/tests/golden/wire-contract/bili.json @@ -1057,6 +1057,28 @@ } }, "synthesized": { + "absorbResponses": { + "type": "function", + "name": "absorb", + "description": "Distill a tool result into a compact summary you write. REQUIRED immediately after a tool result ends with an [ACP absorb] instruction: pass its ref and the distilled essentials (outcome, key values, paths:lines, errors, decisions). The original output is then removed from context; your summary is the durable record.", + "parameters": { + "type": "object", + "properties": { + "ref": { + "type": "string", + "description": "mNNNNN ref of the tool result to absorb (from the [ACP absorb] instruction)" + }, + "summary": { + "type": "string", + "description": "Distilled essentials of the tool result — this replaces the original output in context" + } + }, + "required": [ + "ref", + "summary" + ] + } + }, "ruleAnthropic": { "name": "acp_rule", "description": "Record a short, principle-level reminder so it survives context compression — the call and its result are protected and stay in context. Record when: the user calls out or repeatedly emphasizes a lesson; the user asks you to remember or follow a behavior; you personally hit a major pitfall worth remembering long-term. Keep each rule to one short line. Omit the rule argument to list recorded rules. To remove a recorded rule pass delete with its id (e.g. \"rule3\"); to remove every recorded rule pass clear: true. delete and clear are mutually exclusive with each other and with rule — use one operation per call.", diff --git a/tests/golden/wire-contract/manifest-optin.json b/tests/golden/wire-contract/manifest-optin.json index f5b355b46d..ebdb07b6b7 100644 --- a/tests/golden/wire-contract/manifest-optin.json +++ b/tests/golden/wire-contract/manifest-optin.json @@ -10,6 +10,7 @@ "search_context", "acp_status", "acp_cache", + "absorb", "acp_rule", "acp_retrieve", "read_original_image" @@ -215,6 +216,27 @@ } } }, + { + "name": "absorb", + "description": "Distill a tool result into a compact summary you write. REQUIRED immediately after a tool result ends with an [ACP absorb] instruction: pass its ref and the distilled essentials (outcome, key values, paths:lines, errors, decisions). The original output is then removed from context; your summary is the durable record.", + "input_schema": { + "type": "object", + "properties": { + "ref": { + "type": "string", + "description": "mNNNNN ref of the tool result to absorb (from the [ACP absorb] instruction)" + }, + "summary": { + "type": "string", + "description": "Distilled essentials of the tool result — this replaces the original output in context" + } + }, + "required": [ + "ref", + "summary" + ] + } + }, { "name": "acp_rule", "description": "Record a short, principle-level reminder so it survives context compression — the call and its result are protected and stay in context. Record when: the user calls out or repeatedly emphasizes a lesson; the user asks you to remember or follow a behavior; you personally hit a major pitfall worth remembering long-term. Keep each rule to one short line. Omit the rule argument to list recorded rules. To remove a recorded rule pass delete with its id (e.g. \"rule3\"); to remove every recorded rule pass clear: true. delete and clear are mutually exclusive with each other and with rule — use one operation per call.", @@ -484,6 +506,30 @@ } } }, + { + "type": "function", + "function": { + "name": "absorb", + "description": "Distill a tool result into a compact summary you write. REQUIRED immediately after a tool result ends with an [ACP absorb] instruction: pass its ref and the distilled essentials (outcome, key values, paths:lines, errors, decisions). The original output is then removed from context; your summary is the durable record.", + "parameters": { + "type": "object", + "properties": { + "ref": { + "type": "string", + "description": "mNNNNN ref of the tool result to absorb (from the [ACP absorb] instruction)" + }, + "summary": { + "type": "string", + "description": "Distilled essentials of the tool result — this replaces the original output in context" + } + }, + "required": [ + "ref", + "summary" + ] + } + } + }, { "type": "function", "function": { @@ -744,6 +790,28 @@ } } }, + { + "type": "function", + "name": "absorb", + "description": "Distill a tool result into a compact summary you write. REQUIRED immediately after a tool result ends with an [ACP absorb] instruction: pass its ref and the distilled essentials (outcome, key values, paths:lines, errors, decisions). The original output is then removed from context; your summary is the durable record.", + "parameters": { + "type": "object", + "properties": { + "ref": { + "type": "string", + "description": "mNNNNN ref of the tool result to absorb (from the [ACP absorb] instruction)" + }, + "summary": { + "type": "string", + "description": "Distilled essentials of the tool result — this replaces the original output in context" + } + }, + "required": [ + "ref", + "summary" + ] + } + }, { "type": "function", "name": "acp_rule", diff --git a/tests/issue1192-disabled-tools.test.ts b/tests/issue1192-disabled-tools.test.ts index e7c38afe3c..d1c2057c01 100644 --- a/tests/issue1192-disabled-tools.test.ts +++ b/tests/issue1192-disabled-tools.test.ts @@ -1,7 +1,7 @@ // #1192: dsh registered the manifest's tools verbatim, so it called acp_rule // even though compress.rules was disabled — every call died with a hard // 400 "unknown tool". The manifest side (only advertise enabled opt-in tools) -// is covered by the plugin-protocol / rules suites; this pins the +// is covered by the plugin-protocol / rules / absorb suites; this pins the // execution side: a known-but-disabled opt-in tool answers with a model-facing // explanation (ok:true), while a genuinely unknown tool still 400s. Since // #1399 the feature is on by default, so "disabled" here means an explicit @@ -56,6 +56,13 @@ describe("#1192: known-but-disabled opt-in tools get an explanation, not a hard assert.match(String(r.json.result), /acp_rule is not enabled on this bili proxy \(compress\.rules\.enabled is not true\)/); }); + it("absorb disabled → ok:true explanation", async () => { + const r = await call("absorb", { ref: "m00001", summary: "x" }); + assert.equal(r.status, 200); + assert.equal(r.json.ok, true); + assert.match(String(r.json.result), /absorb is not enabled on this bili proxy \(compress\.absorb\.enabled is not true\)/); + }); + it("truly unknown tool keeps the 400 with the allowed list", async () => { const r = await call("definitely_not_a_tool"); assert.equal(r.status, 400); diff --git a/tests/issue2204-decompress-to-file-outcome.test.ts b/tests/issue2204-decompress-to-file-outcome.test.ts index 5ea6f2c8e8..f9fdd879cd 100644 --- a/tests/issue2204-decompress-to-file-outcome.test.ts +++ b/tests/issue2204-decompress-to-file-outcome.test.ts @@ -68,7 +68,7 @@ function foldCcr() { const config = applyCompressSettings( defaultConfig(200_000), 200_000, - { ccr: { enabled: true, minToolTokens: 50 } }, + { absorb: { enabled: true, minToolTokens: 50 }, ccr: { enabled: true, minToolTokens: 50 } }, ) as Config; const session = getSession(`tfr2204-${Math.random().toString(36).slice(2)}`); storeEffectiveCcr(session, { enabled: true, minToolTokens: 50 }); diff --git a/tests/opencode-native.test.ts b/tests/opencode-native.test.ts index c69c2aa4c6..23f51b4647 100644 --- a/tests/opencode-native.test.ts +++ b/tests/opencode-native.test.ts @@ -29,14 +29,14 @@ after(() => { import type { NativeInterceptState } from "../src/agent/native-intercept.ts"; import type { V2HttpRequestEvent, V2PluginContext, V2State } from "../src/agent/opencode-v2.ts"; -import { ACP_TOOLS_OPENAI } from "../src/compress-tool.ts"; +import { ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI } from "../src/compress-tool.ts"; const { shouldBootstrapNativeOpencode, createNativeRoute, planNativeOpencode, armNativeOpencode, verifyAttachAndRecover, _resetNativeStateForTest, _setSpawnForTest, _stateRespawnForTest, _noteRoutedForTest } = await import("../src/agent/opencode-native.ts"); const { createOpencodeV2Setup } = await import("../src/agent/opencode-v2.ts"); const { markNativeHost, nativeAttachOrigin } = await import("../src/agent/native-bootstrap.ts"); const nativeDefault = (await import("../src/agent/opencode-native.ts")).default; -const EXPECTED_TOOLS = [...ACP_TOOLS_OPENAI.map((t) => t.function.name)]; +const EXPECTED_TOOLS = [...ACP_TOOLS_OPENAI.map((t) => t.function.name), ABSORB_TOOL_OPENAI.function.name]; const MODEL_URL = "https://api.anthropic.com/v1/messages"; const sleep = (ms: number): Promise => new Promise((r) => setTimeout(r, ms)); diff --git a/tests/opencode-v2.test.ts b/tests/opencode-v2.test.ts index a512f55d4e..798a80c771 100644 --- a/tests/opencode-v2.test.ts +++ b/tests/opencode-v2.test.ts @@ -9,10 +9,10 @@ import test from "node:test"; process.env.NODE_ENV = "test"; import biliOpencodePlugin from "../src/agent/opencode.ts"; -import { ACP_TOOLS_OPENAI, RETRIEVE_TOOL_NAME, retrieveToolsFor } from "../src/compress-tool.ts"; +import { ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI, RETRIEVE_TOOL_NAME, retrieveToolsFor } from "../src/compress-tool.ts"; import { fetchManifest, fitNoticeDescription } from "../src/agent/shared.ts"; -const EXPECTED_TOOLS = [...ACP_TOOLS_OPENAI.map((t) => t.function.name)]; +const EXPECTED_TOOLS = [...ACP_TOOLS_OPENAI.map((t) => t.function.name), ABSORB_TOOL_OPENAI.function.name]; const LONG_PANEL = Array.from({ length: 30 }, (_, i) => `panel line ${String(i + 1).padStart(2, "0")}: ${"x".repeat(38)}`).join("\n"); const LONG_REPORT = Array.from({ length: 200 }, (_, i) => `report line ${String(i + 1).padStart(3, "0")}: ${"y".repeat(38)}`).join("\n"); @@ -230,7 +230,7 @@ test("v2 setup: registers the bundled bili tools synchronously with exact schema assert.equal(t.options?.codemode, false); assert.equal(t.options?.permission, "allow"); } - const compressSrc = [...ACP_TOOLS_OPENAI].find((t) => t.function.name === "compress")!; + const compressSrc = [...ACP_TOOLS_OPENAI, ABSORB_TOOL_OPENAI].find((t) => t.function.name === "compress")!; assert.deepEqual(fake.addedTools.find((t) => t.name === "compress")!.input, compressSrc.function.parameters); } finally { cleanup(); diff --git a/tests/plugin-protocol.test.ts b/tests/plugin-protocol.test.ts index 44497c5d1c..2ec142a2c2 100644 --- a/tests/plugin-protocol.test.ts +++ b/tests/plugin-protocol.test.ts @@ -88,7 +88,7 @@ async function startHarness( scripts: string[][], kernelConfig?: Config, // #1278: the user-facing `compress` block (loadOptions → opts.compress), distinct - // from kernelConfig. Tests pass rules here to exercise the REAL path the + // from kernelConfig. Tests pass absorb/rules here to exercise the REAL path the // file config takes, not the kernelConfig shortcut the pre-fix manifest read. compress?: Partial, ): Promise { @@ -239,7 +239,7 @@ test("plugin manifest serves the exact wire tool schemas, headers and version", assert.equal(manifest.ok, true); assert.equal(manifest.protocolVersion, 1); assert.ok(/^\d+\.\d+\.\d+/.test(manifest.version), `version looks wrong: ${manifest.version}`); - // #1192: every extra tool (acp_rule) is opt-in and only advertised + // #1192: every extra tool (absorb, acp_rule) is opt-in and only advertised // when enabled — hosts register manifest tools verbatim, so a disabled // tool must not be listed. acp_cache stays in the base toolset. assert.deepEqual([...manifest.toolNames].sort(), ["acp_cache", "acp_status", "compress", "decompress", "read_original_image", "search_context"]); @@ -292,7 +292,68 @@ test("plugin manifest: acp_rule advertised when rules enabled in config", async } }); +// #1192 (regression): with absorb + rules enabled in the base config the +// manifest advertises all eight tools again — enabling the features must not +// shrink the toolset dsh/pi/omp/MCP shims register. +test("plugin manifest: absorb and acp_rule advertised when enabled in config", async () => { + const h = await startHarness([textScript()], { ...defaultConfig(400_000), absorb: { enabled: true, toolName: "absorb", minToolTokens: 4000, contextThresholdPct: 0, excludeTools: [] }, rules: { enabled: true } }); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/__bili/plugin/manifest`); + assert.equal(resp.status, 200); + const manifest = (await resp.json()) as { toolNames: string[]; tools: Record>> }; + assert.deepEqual([...manifest.toolNames].sort(), ["absorb", "acp_cache", "acp_rule", "acp_status", "compress", "decompress", "read_original_image", "search_context"]); + for (const shape of ["anthropic", "openai", "responses"] as const) { + const listed = manifest.tools[shape]!.map((t) => { + const fn = t.function as { name?: unknown } | undefined; + return typeof t.name === "string" ? t.name : (typeof fn?.name === "string" ? fn.name : ""); + }); + assert.ok(listed.includes("absorb"), `${shape} has absorb`); + assert.ok(listed.includes("acp_rule"), `${shape} has acp_rule`); + assert.equal(listed.length, 8); + } + } finally { + await h.close(); + } +}); + +// #1278 (regression): the pre-fix manifest read opts.kernelConfig (kernel defaults), +// so an absorb enabled in the user's GLOBAL compress block — where loadOptions puts +// the file config — was never advertised. Pi then registered no callable tool for the +// [ACP absorb] prompts the wire emits. Enabling via the global compress block (the real +// path) must now advertise absorb on every wire shape. +test("plugin manifest: absorb advertised when enabled in the global compress block", async () => { + const h = await startHarness([textScript()], undefined, { absorb: { enabled: true } }); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/__bili/plugin/manifest`); + assert.equal(resp.status, 200); + const manifest = (await resp.json()) as { toolNames: string[]; tools: Record>> }; + assert.deepEqual([...manifest.toolNames].sort(), ["absorb", "acp_cache", "acp_status", "compress", "decompress", "read_original_image", "search_context"]); + for (const shape of ["anthropic", "openai", "responses"] as const) { + const listed = manifest.tools[shape]!.map((t) => { + const fn = t.function as { name?: unknown } | undefined; + return typeof t.name === "string" ? t.name : (typeof fn?.name === "string" ? fn.name : ""); + }); + assert.ok(listed.includes("absorb"), `${shape} has absorb`); + assert.equal(listed.length, 7); + } + } finally { + await h.close(); + } +}); +// #1278: an explicit global disable keeps absorb out of the manifest (guards against +// flipping the logic to "advertise whenever an absorb key is present"). +test("plugin manifest: absorb omitted when the global compress block disables it", async () => { + const h = await startHarness([textScript()], undefined, { absorb: { enabled: false } }); + try { + const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/__bili/plugin/manifest`); + assert.equal(resp.status, 200); + const manifest = (await resp.json()) as { toolNames: string[] }; + assert.deepEqual([...manifest.toolNames].sort(), ["acp_cache", "acp_status", "compress", "decompress", "read_original_image", "search_context"]); + } finally { + await h.close(); + } +}); test("plugin mode: no wire tool injection, philosophy still injected, native compress tool_use passes through untouched", async () => { const h = await startHarness([compressToolScript()]); diff --git a/tests/removed-compress-keys-inert.test.ts b/tests/removed-compress-keys-inert.test.ts index 2f7bb521a9..111be4b0a8 100644 --- a/tests/removed-compress-keys-inert.test.ts +++ b/tests/removed-compress-keys-inert.test.ts @@ -34,22 +34,22 @@ function withConfigFile(body: string, fn: () => void): void { const familyWarns = (captured: Captured[]) => captured.filter((e) => e.level === "warn" && e.msg.includes("is INERT") && /absorb|imageCompression/.test(e.msg)); -test("#3344: removed compress.absorb / compress.imageCompression warn INERT once (root level) and are ignored", () => { +// `absorb` was removed by #3293 but REVIVED by #3402/#3422 — it parses again +// and is NOT inert; only imageCompression (#3266) stays in the removed family. +test("#3344: removed compress.imageCompression warns INERT once (root level) and is ignored; revived absorb parses", () => { const { captured, stop } = captureLogs(); withConfigFile( `{"compress":{"absorb":{"enabled":true,"minToolTokens":500},"imageCompression":{"enabled":true,"quality":60}}}`, () => { const cfg = loadConfigFile(); - // Both keys are gone from the typed surface — the parse whitelist - // (COMPRESS_SETTING_FIELDS) no longer carries them, so they drop. const cs = parseCompressSettings(cfg.compress ?? {}); - assert.equal((cs as Record).absorb, undefined); + assert.deepEqual((cs as Record).absorb, { enabled: true, minToolTokens: 500 }); assert.equal((cs as Record).imageCompression, undefined); const warns = familyWarns(captured); assert.equal(warns.length, 1, `expected exactly one family INERT warning, got: ${warns.map((w) => w.msg).join(" | ")}`); - assert.match(warns[0]!.msg, /compress\.absorb/); + assert.doesNotMatch(warns[0]!.msg, /compress\.absorb/); assert.match(warns[0]!.msg, /compress\.imageCompression/); - assert.match(warns[0]!.msg, /were removed/); + assert.match(warns[0]!.msg, /was removed/); // Second load of the SAME content: deduped (one notice per process). loadConfigFile(); assert.equal(familyWarns(captured).length, 1); @@ -58,7 +58,7 @@ test("#3344: removed compress.absorb / compress.imageCompression warn INERT once stop(); }); -test("#3344: removed keys at provider and model levels are named by full path", () => { +test("#3344: removed key at provider and model levels is named by full path; provider-level absorb does not warn", () => { const { captured, stop } = captureLogs(); withConfigFile( `{"providers":{"https://u.example":{"compress":{"absorb":{"enabled":true}},"models":{"gpt-x":{"compress":{"imageCompression":{"enabled":true}}}}}}}`, @@ -66,7 +66,7 @@ test("#3344: removed keys at provider and model levels are named by full path", loadConfigFile(); const warns = familyWarns(captured); assert.equal(warns.length, 1, `expected exactly one family INERT warning, got: ${warns.map((w) => w.msg).join(" | ")}`); - assert.match(warns[0]!.msg, /providers\["https:\/\/u\.example"\]\.compress\.absorb/); + assert.doesNotMatch(warns[0]!.msg, /compress\.absorb/); assert.match(warns[0]!.msg, /providers\["https:\/\/u\.example"\]\.models\["gpt-x"\]\.compress\.imageCompression/); }, ); diff --git a/tests/rules.test.ts b/tests/rules.test.ts index 45eb51f888..6e1d8c0fd5 100644 --- a/tests/rules.test.ts +++ b/tests/rules.test.ts @@ -9,7 +9,7 @@ import { RULE_TOOL_NAME, type Config, } from "acp-kernel"; -import { isProxyToolFor } from "../src/proxy-tool.js"; +import { isProxyToolFor } from "../src/absorb.ts"; import { mergeCompress, applyCompressSettings } from "../src/compress-settings.ts"; import { parseCompressSettings } from "../src/config.ts"; import { RULE_TOOL, RULE_TOOL_GOOGLE, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES } from "../src/compress-tool.ts"; diff --git a/tests/search-plan-aware.test.ts b/tests/search-plan-aware.test.ts index 11e335b974..ea5bac1e30 100644 --- a/tests/search-plan-aware.test.ts +++ b/tests/search-plan-aware.test.ts @@ -55,7 +55,7 @@ type Folded = { core: ReturnType; config: Config; session: Re function foldBlocks(ccr: boolean): Folded { const core = createCore(); - const config = applyCompressSettings(defaultConfig(200_000), 200_000, { ccr: { enabled: ccr, minToolTokens: 50 } }) as Config; + const config = applyCompressSettings(defaultConfig(200_000), 200_000, { absorb: { enabled: true, minToolTokens: 50 }, ccr: { enabled: ccr, minToolTokens: 50 } }) as Config; const session = getSession(`sp-${Math.random().toString(36).slice(2)}`); if (ccr) storeEffectiveCcr(session, { enabled: true, minToolTokens: 50 }); const turn = core.processTurn({ messages: makeMsgs(40), state: session.state, config, tokenCount: 9999, renderTags: "text-only" }); diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index 84ed990089..b10caedb05 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -344,13 +344,13 @@ test("(H) extracted relay module keeps the side-lane boundary (#1440 P2 cut 4)", ); }); -// (#3293): absorb was removed from bili entirely — the kernel's absorb tool -// name must no longer be bili-owned: any "absorb" tool in a request is -// host/client-originated and vetoes side-lane demotion like any other foreign -// tool. Lands alongside the src/server/side-request.ts edit per #2170 -// (lane logic moves ⇔ a dedicated side-lane suite moves with it). -test("(I) post-#3293: the removed absorb tool name is no longer bili-owned", () => { - assert.ok(!BILI_TOOL_NAMES.has(ABSORB_TOOL_NAME), "absorb is out of the bili-owned set"); +// (#3293 removed absorb; #3402/#3422 revive it): the kernel's absorb tool name +// is bili-owned again — a session-sidecar request carrying it is demoted by +// #1897 like any other bili-owned tool. Lands alongside the +// src/server/side-request.ts edit per #2170 (lane logic moves ⇔ a dedicated +// side-lane suite moves with it). +test("(I) revival (#3402): the absorb tool name is bili-owned again", () => { + assert.ok(BILI_TOOL_NAMES.has(ABSORB_TOOL_NAME), "absorb is back in the bili-owned set"); assert.ok(BILI_TOOL_NAMES.has(RULE_TOOL_NAME), "rules tool still bili-owned"); assert.ok(BILI_TOOL_NAMES.has(RETRIEVE_TOOL_NAME), "retrieve tool still bili-owned"); }); diff --git a/tests/store.test.ts b/tests/store.test.ts index e80d89e806..339a886588 100644 --- a/tests/store.test.ts +++ b/tests/store.test.ts @@ -38,6 +38,7 @@ function toolResult(): CoreMessage[] { function ccrConfig() { return applyCompressSettings(defaultConfig(200000), 200_000, { + absorb: { enabled: true, minToolTokens: 50 }, ccr: { enabled: true, minToolTokens: 50 }, }); } @@ -110,7 +111,7 @@ test("applyCompressSettings: maps ccr onto kernel CcrConfig with DEFAULT_CCR_CON assert.equal(off.ccr?.enabled, false); }); -test("integration: kernel processTurn ID-references the oversized tool result (ccr enabled)", () => { +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")!; diff --git a/tests/wire-contract-inventory.ts b/tests/wire-contract-inventory.ts index dfa618667a..be25476a55 100644 --- a/tests/wire-contract-inventory.ts +++ b/tests/wire-contract-inventory.ts @@ -10,6 +10,7 @@ import * as K from "acp-kernel"; import { defaultConfig } from "acp-kernel"; import { handlePluginManifest } from "../src/plugin.ts"; import { + ABSORB_TOOL_RESPONSES, BILI_ACP_READONLY_TOOLS_RESPONSES, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_GOOGLE, @@ -124,6 +125,7 @@ function buildBiliGolden(): Record { decompressGoogle: BILI_DECOMPRESS_TOOL_GOOGLE, }, synthesized: { + absorbResponses: ABSORB_TOOL_RESPONSES, ruleAnthropic: RULE_TOOL, ruleOpenAi: RULE_TOOL_OPENAI, ruleResponses: RULE_TOOL_RESPONSES, @@ -154,6 +156,6 @@ export function buildGoldens(): GoldenSpec[] { { name: "kernel.json", layer: "acp-kernel exports (pinned)", build: buildKernelGolden }, { name: "bili.json", layer: "bili-built surfaces (compress-tool.ts)", build: buildBiliGolden }, { name: "manifest-default.json", layer: "plugin manifest, default config", build: () => captureManifest(defaultConfig(100_000)) }, - { name: "manifest-optin.json", layer: "plugin manifest, rules+ccr enabled", build: () => captureManifest({ ...defaultConfig(100_000), rules: { enabled: true }, ccr: { ...K.DEFAULT_CCR_CONFIG, enabled: true } }) }, + { name: "manifest-optin.json", layer: "plugin manifest, absorb+rules+ccr enabled", build: () => captureManifest({ ...defaultConfig(100_000), absorb: { ...K.DEFAULT_ABSORB_CONFIG, enabled: true }, rules: { enabled: true }, ccr: { ...K.DEFAULT_CCR_CONFIG, enabled: true } }) }, ]; } diff --git a/tests/wire-contract.test.ts b/tests/wire-contract.test.ts index d4c0c3fd08..cd151d4fd9 100644 --- a/tests/wire-contract.test.ts +++ b/tests/wire-contract.test.ts @@ -45,6 +45,7 @@ import { BILI_DECOMPRESS_TOOL_OPENAI, BILI_DECOMPRESS_TOOL_RESPONSES, BILI_DECOMPRESS_TOOL_GOOGLE, + ABSORB_TOOL_RESPONSES, READ_ORIGINAL_IMAGE_TOOL_NAME, RULE_TOOL, RULE_TOOL_OPENAI, @@ -292,6 +293,7 @@ const BILI_ARRAYS: Array<[string, Wire]> = [ const BILI_INDIVIDUALS: Array<[string, Wire]> = [ ["BILI_SEARCH_CONTEXT_TOOL", "anthropic"], ["BILI_SEARCH_CONTEXT_TOOL_OPENAI", "openai-chat"], ["BILI_SEARCH_CONTEXT_TOOL_RESPONSES", "responses"], ["BILI_SEARCH_CONTEXT_TOOL_GOOGLE", "google"], ["BILI_DECOMPRESS_TOOL", "anthropic"], ["BILI_DECOMPRESS_TOOL_OPENAI", "openai-chat"], ["BILI_DECOMPRESS_TOOL_RESPONSES", "responses"], ["BILI_DECOMPRESS_TOOL_GOOGLE", "google"], + ["ABSORB_TOOL_RESPONSES", "responses"], ["RULE_TOOL", "anthropic"], ["RULE_TOOL_OPENAI", "openai-chat"], ["RULE_TOOL_RESPONSES", "responses"], ["RULE_TOOL_GOOGLE", "google"], ]; @@ -299,7 +301,7 @@ const BILI_CONSTANTS: Record = { BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_RESPONSES, BILI_ACP_TOOLS_GOOGLE, BILI_ACP_READONLY_TOOLS_RESPONSES, BILI_SEARCH_CONTEXT_TOOL, BILI_SEARCH_CONTEXT_TOOL_OPENAI, BILI_SEARCH_CONTEXT_TOOL_RESPONSES, BILI_SEARCH_CONTEXT_TOOL_GOOGLE, BILI_DECOMPRESS_TOOL, BILI_DECOMPRESS_TOOL_OPENAI, BILI_DECOMPRESS_TOOL_RESPONSES, BILI_DECOMPRESS_TOOL_GOOGLE, - RULE_TOOL, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, RULE_TOOL_GOOGLE, + ABSORB_TOOL_RESPONSES, RULE_TOOL, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, RULE_TOOL_GOOGLE, RETRIEVE_TOOLS: retrieveToolsFor("acp_retrieve"), }; @@ -365,25 +367,25 @@ test("wire-contract C: default manifest serves only legal base tools", () => { assertManifestLegal(m, "default"); const anthNames = new Set(m.tools.anthropic.map((t) => t.name)); for (const n of BILI_ACP_TOOLS_ANTHROPIC.map((t) => t.name)) assert.ok(anthNames.has(n), `default manifest: base tool ${n} present`); - for (const optIn of ["acp_retrieve"]) assert.ok(!anthNames.has(optIn), `default manifest: ${optIn} absent when disabled (#1192)`); - assert.ok(!anthNames.has("acp_rule"), "default manifest: acp_rule absent — opt-in extra (#1192 discipline)"); + for (const optIn of ["absorb", "acp_retrieve"]) assert.ok(!anthNames.has(optIn), `default manifest: ${optIn} absent when disabled (#1192)`); + assert.ok(!anthNames.has("acp_rule"), "default manifest: acp_rule absent — opt-in like absorb (#1192 discipline)"); const offM = captureManifest({ ...defaultConfig(100_000), rules: { enabled: false } }); assert.ok(!new Set(offM.tools.anthropic.map((t) => t.name)).has("acp_rule"), "disabled manifest: acp_rule absent (#1192)"); assertCompressFormsKept(m.tools.anthropic.find((t) => t.name === "compress") as ToolShape, "manifest-default"); }); -test("wire-contract C: opt-in manifest advertises acp_rule/acp_retrieve legally", () => { - const config = { ...defaultConfig(100_000), rules: { enabled: true }, ccr: { ...K.DEFAULT_CCR_CONFIG, enabled: true } }; +test("wire-contract C: opt-in manifest advertises absorb/acp_rule/acp_retrieve legally", () => { + const config = { ...defaultConfig(100_000), absorb: { ...K.DEFAULT_ABSORB_CONFIG, enabled: true }, rules: { enabled: true }, ccr: { ...K.DEFAULT_CCR_CONFIG, enabled: true } }; const m = captureManifest(config); assertManifestLegal(m, "optin"); const anthNames = new Set(m.tools.anthropic.map((t) => t.name)); const openAiNames = new Set(m.tools.openai.map((t) => toolNameOf("openai-chat", t))); const respNames = new Set(m.tools.responses.map((t) => t.name)); - for (const n of ["acp_rule", "acp_retrieve"]) { + for (const n of ["absorb", "acp_rule", "acp_retrieve"]) { assert.ok(anthNames.has(n), `optin manifest: anthropic advertises ${n}`); assert.ok(openAiNames.has(n), `optin manifest: openai advertises ${n}`); } - assert.ok(respNames.has("acp_rule"), "optin manifest: responses advertises acp_rule"); + for (const n of ["absorb", "acp_rule"]) assert.ok(respNames.has(n), `optin manifest: responses advertises ${n}`); assert.ok(!respNames.has("acp_retrieve"), "optin manifest: responses does NOT advertise acp_retrieve (#1271)"); assertCompressFormsKept(m.tools.anthropic.find((t) => t.name === "compress") as ToolShape, "manifest-optin"); }); @@ -580,12 +582,12 @@ for (const lane of LANES) { }); } -test("wire-contract D: opt-in lane (rules+ccr) forwards the extended surface legally", async () => { +test("wire-contract D: opt-in lane (absorb+rules+ccr) forwards the extended surface legally", async () => { const lane = LANES[0]; const fake = await startFakeUpstream(lane.wire); let rig: Rig | undefined; try { - rig = await startRig(fake.url, lane.model, { rules: true, ccr: { enabled: true } }); + rig = await startRig(fake.url, lane.model, { absorb: { enabled: true }, rules: true, ccr: { enabled: true } }); const res = await fetch(`${rig.proxyUrl}${lane.path}`, { method: "POST", headers: { "content-type": "application/json", "x-api-key": "test", "x-acp-session": "wc-anth-optin" }, @@ -595,8 +597,8 @@ test("wire-contract D: opt-in lane (rules+ccr) forwards the extended surface leg await res.text(); assert.equal(fake.violations.length, 0, `opt-in fake violations:\n${fake.violations.join("\n")}`); const fwdNames = extractForwardedNames(lane.wire, fake.requests[0].body); - const expected = [...BASE_EXPECTED[lane.wire], ...lane.clientToolNames, "acp_rule", "acp_retrieve"].sort(); - assert.deepEqual(fwdNames.sort(), expected, "opt-in lane forwards base + acp_rule (rules:true) + acp_retrieve + client tools"); + const expected = [...BASE_EXPECTED[lane.wire], ...lane.clientToolNames, "acp_rule", "absorb", "acp_retrieve"].sort(); + assert.deepEqual(fwdNames.sort(), expected, "opt-in lane forwards base + acp_rule (rules:true) + absorb/acp_retrieve + client tools"); const fwdTools = (fake.requests[0].body as ToolShape).tools; const compress = (Array.isArray(fwdTools) ? fwdTools : []).find((t) => (t as ToolShape).name === "compress") as ToolShape; assert.ok(compress, "opt-in forwarded body carries compress"); diff --git a/website/config-reference/compress.yaml b/website/config-reference/compress.yaml index 894aaafb52..87b4db99c0 100644 --- a/website/config-reference/compress.yaml +++ b/website/config-reference/compress.yaml @@ -219,10 +219,10 @@ entries: group: compress id: ct.rules render: compress-row - desc_en: "Persistent model reminders delivered through an injected acp_rule tool, hard-protected from folds; pi/omp expose an /acp-rule command." - desc_zh: "通过注入的 acp_rule 工具提供持久模型提醒——对折叠硬保护;pi/omp 提供 /acp-rule 命令。" - site_en: "Off by default: persistent model reminders delivered through an injected acp_rule tool, hard-protected from folds. pi/omp expose an /acp-rule command." - site_zh: "默认关:通过注入的 acp_rule 工具提供持久模型提醒——对折叠硬保护。pi/omp 提供 /acp-rule 命令。" + desc_en: "Persistent model reminders delivered through an injected acp_rule tool, hard-protected from folds; pi/omp expose an /acp-rule command. Proxy-mode only — inert in plugin mode, where the host owns the tool surface." + desc_zh: "通过注入的 acp_rule 工具提供持久模型提醒——对折叠硬保护;pi/omp 提供 /acp-rule 命令。仅代理模式生效——插件模式下宿主自管工具面,此项无效。" + site_en: "Off by default: persistent model reminders delivered through an injected acp_rule tool, hard-protected from folds. pi/omp expose an /acp-rule command. Proxy-mode only — inert in plugin mode." + site_zh: "默认关:通过注入的 acp_rule 工具提供持久模型提醒——对折叠硬保护。pi/omp 提供 /acp-rule 命令。仅代理模式生效——插件模式下无效。" - key: compress.injectTool type: boolean default: "true" @@ -255,6 +255,16 @@ entries: desc_zh: "丢弃超过 2048 字符的已结束轮次推理块(drop 默认 true);严格推理上游需设 drop:false。" site_en: "{ drop?, threshold? } — drop finished-round reasoning runs longer than 2048 chars (drop: true by default); strict-reasoning upstreams need drop:false." site_zh: "{ drop?, threshold? }——丢弃超过 2048 字符的已结束轮次推理块(drop 默认 true);严格推理上游需设 drop:false。" + - key: compress.absorb + type: object + default: "opt-in (disabled)" + group: compress + id: co.absorb + render: obj-item + desc_en: "Opt-in instant distillation of tool results into short abstracts; originals go to the content store. By default only image-bearing results qualify." + desc_zh: "可选即时蒸馏:把工具结果蒸馏成短摘要,原文进内容库。默认仅图片类结果参与。" + site_en: "absorb — opt-in instant distillation of tool results into short abstracts; originals go to the content store; by default only image-bearing results qualify (enabled, minToolTokens (default 1000), contextThresholdPct, excludeTools, mediaOnly (default true), toolName \"absorb\")." + site_zh: "absorb —— 可选即时蒸馏:把工具结果蒸馏成短摘要,原文进内容库;默认仅图片类结果参与(enabled, minToolTokens(默认 1000), contextThresholdPct, excludeTools, mediaOnly(默认 true), toolName \"absorb\")。" - key: compress.ccr type: object default: "enabled in proxy mode since v2" @@ -335,6 +345,42 @@ entries: desc_zh: "必须先置 true,自定义 prompts 才会生效。" site_en: "acknowledgePromptsRisk — must be true before custom prompts take effect." site_zh: "acknowledgePromptsRisk —— 必须先置 true,自定义 prompts 才会生效。" + - key: compress.absorb.enabled + type: boolean + default: "false" + group: compress + desc_en: "Enable the absorb distillation block." + desc_zh: "启用 absorb 蒸馏块。" + - key: compress.absorb.minToolTokens + type: number + default: "1000" + group: compress + desc_en: "Minimum estimated token size of a tool result eligible for absorption." + desc_zh: "可被吸收的工具结果的最小估算 token 大小。" + - key: compress.absorb.contextThresholdPct + type: "number | %" + default: "unset" + group: compress + desc_en: "Only absorb when context usage passes this share of the window." + desc_zh: "仅当上下文使用率超过窗口该占比时才吸收。" + - key: compress.absorb.excludeTools + type: "string[]" + default: "[]" + group: compress + desc_en: "Tool names excluded from absorption." + desc_zh: "排除出吸收范围的工具名。" + - key: compress.absorb.toolName + type: string + default: "\"absorb\"" + group: compress + desc_en: "Registered name of the absorb tool." + desc_zh: "absorb 工具的注册名。" + - key: compress.absorb.mediaOnly + type: boolean + default: "true" + group: compress + desc_en: "Restrict absorption to tool results that carry media (images). Image bytes are exempt from minToolTokens since they are not counted in text. Set false to restore legacy full-text absorption of any large result." + desc_zh: "仅吸收携带媒体(图片)的工具结果;图片字节不计入文本 token,故豁免 minToolTokens 门槛。置 false 恢复对任意大结果的全文吸收(旧行为)。" - key: compress.ccr.enabled type: boolean default: "true (proxy mode)" diff --git a/website/docs/index.html b/website/docs/index.html index 0f5bddaed9..17563f1cfe 100644 --- a/website/docs/index.html +++ b/website/docs/index.html @@ -199,7 +199,7 @@

compress — every knob

neverPreserveRecentToolsstring[] ([] valid)Excluded from the recent protection zone (immediately compressible); default ["decompress", "search_context", "read", "bash"]. An empty array is valid — it excludes nothing (maximum protection). preserveRecentToolsstring[]Subtraction form: recent-zone tools minus this list get full protection. An empty array here is rejected as a typo. visibilityMarkersbooleanAppend 📦/❌ visibility markers after compress/decompress/search_context/acp_status results (on by default; off stops imitated narration). - rulesbooleanOff by default: persistent model reminders delivered through an injected acp_rule tool, hard-protected from folds. pi/omp expose an /acp-rule command. + rulesbooleanOff by default: persistent model reminders delivered through an injected acp_rule tool, hard-protected from folds. pi/omp expose an /acp-rule command. Proxy-mode only — inert in plugin mode. injectToolbooleanRegister the acp_compress tool for clients (on by default; global level only). injectNudgebooleanSend growth nudges as the window fills (on by default; global level only). reasoning{ drop?, threshold? }{ drop?, threshold? } — drop finished-round reasoning runs longer than 2048 chars (drop: true by default); strict-reasoning upstreams need drop:false. @@ -208,6 +208,7 @@

compress — every knob

    +
  • absorb — opt-in instant distillation of tool results into short abstracts; originals go to the content store; by default only image-bearing results qualify (enabled, minToolTokens (default 1000), contextThresholdPct, excludeTools, mediaOnly (default true), toolName "absorb").
  • ccr — content cache & retrieve: large outputs are stored losslessly beside the session and replaced by a head excerpt plus pointer; the model fetches originals back via acp_retrieve. No cap or eviction; on by default in proxy mode since v2 (enabled, minToolTokens (~4000), excludeTools, toolName "acp_retrieve", maxHeadChars).
  • search — search-behavior settings for search_context; currently holds planAware.
  • search.planAware — off by default: when on, search_context candidates are re-ranked against current plan state (latest-wins todos + last user turn); off keeps byte-identical lexical results.
  • diff --git a/website/main.js b/website/main.js index 3f43534348..f212d1fabb 100644 --- a/website/main.js +++ b/website/main.js @@ -82,10 +82,11 @@ "ct.nprt": "Excluded from the recent protection zone (immediately compressible); default [\"decompress\", \"search_context\", \"read\", \"bash\"]. An empty array is valid — it excludes nothing (maximum protection).", "ct.prt2": "Subtraction form: recent-zone tools minus this list get full protection. An empty array here is rejected as a typo.", "ct.vm": "Append 📦/❌ visibility markers after compress/decompress/search_context/acp_status results (on by default; off stops imitated narration).", - "ct.rules": "Off by default: persistent model reminders delivered through an injected acp_rule tool, hard-protected from folds. pi/omp expose an /acp-rule command.", + "ct.rules": "Off by default: persistent model reminders delivered through an injected acp_rule tool, hard-protected from folds. pi/omp expose an /acp-rule command. Proxy-mode only — inert in plugin mode.", "ct.it": "Register the acp_compress tool for clients (on by default; global level only).", "ct.in": "Send growth nudges as the window fills (on by default; global level only).", "ct.rz": "{ drop?, threshold? } — drop finished-round reasoning runs longer than 2048 chars (drop: true by default); strict-reasoning upstreams need drop:false.", + "co.absorb": "absorb — opt-in instant distillation of tool results into short abstracts; originals go to the content store; by default only image-bearing results qualify (enabled, minToolTokens (default 1000), contextThresholdPct, excludeTools, mediaOnly (default true), toolName \"absorb\").", "co.ccr": "ccr — content cache & retrieve: large outputs are stored losslessly beside the session and replaced by a head excerpt plus pointer; the model fetches originals back via acp_retrieve. No cap or eviction; on by default in proxy mode since v2 (enabled, minToolTokens (~4000), excludeTools, toolName \"acp_retrieve\", maxHeadChars).", "co.searchRoot": "search — search-behavior settings for search_context; currently holds planAware.", "co.search": "search.planAware — off by default: when on, search_context candidates are re-ranked against current plan state (latest-wins todos + last user turn); off keeps byte-identical lexical results.", @@ -322,10 +323,11 @@ "ct.nprt": "从近期保护区排除(立即可压);默认 [\"decompress\", \"search_context\", \"read\", \"bash\"]。空数组合法=不排除任何工具(最大保护)。", "ct.prt2": "减法形式:近期区工具减去本列表得到完全保护;此处空数组按笔误拒绝。", "ct.vm": "在 compress/decompress/search_context/acp_status 结果后追加 📦/❌ 可见性标记(默认开;关闭可抑制模型模仿叙述)。", - "ct.rules": "默认关:通过注入的 acp_rule 工具提供持久模型提醒——对折叠硬保护。pi/omp 提供 /acp-rule 命令。", + "ct.rules": "默认关:通过注入的 acp_rule 工具提供持久模型提醒——对折叠硬保护。pi/omp 提供 /acp-rule 命令。仅代理模式生效——插件模式下无效。", "ct.it": "向客户端注册 acp_compress 工具(默认开;仅全局层级生效)。", "ct.in": "窗口填满过程中发送增长提醒(默认开;仅全局层级生效)。", "ct.rz": "{ drop?, threshold? }——丢弃超过 2048 字符的已结束轮次推理块(drop 默认 true);严格推理上游需设 drop:false。", + "co.absorb": "absorb —— 可选即时蒸馏:把工具结果蒸馏成短摘要,原文进内容库;默认仅图片类结果参与(enabled, minToolTokens(默认 1000), contextThresholdPct, excludeTools, mediaOnly(默认 true), toolName \"absorb\")。", "co.ccr": "ccr —— 内容缓存与回取:大输出无损存到会话旁、替换为首段摘录+指针,模型用 acp_retrieve 取回原文;无上限、永不清除;自 v2 起代理模式默认开(enabled, minToolTokens(~4000), excludeTools, toolName \"acp_retrieve\", maxHeadChars)。", "co.searchRoot": "search —— search_context 的检索行为设置;当前含 planAware。", "co.search": "search.planAware —— 默认关:开启后 search_context 候选按当前计划状态重排(todo 最新优先 + 上一轮用户消息);关闭时结果逐字节不变。",