Files
billion-context/kernel/tests/compress-tools.test.ts
ework-agent 6b33fd21a9 feat: gate the #5765 block-lifecycle tools behind compress.blockOps (default off)
Owner ruling (review of this PR): acp_retire / acp_restore / acp_rewrite
must sit behind ONE switch, default OFF, injected only when ON — the
always-on surface costs ~0.7k estimated tokens per request on every
session of an enabled proxy.

Follows the absorb opt-in pattern end to end:
- kernel: definitions + name consts stay exported, but the 3 tools leave
  the default arrays (ACP_TOOLS_{OPENAI,ANTHROPIC,RESPONSES,GOOGLE},
  ACP_READONLY_TOOLS_RESPONSES, ACP_TOOL_NAMES); they stay in
  ACP_MUTATING_TOOLS (dispatch must drive the compress loop when enabled).
  New BLOCK_OP_TOOL_NAMES set + buildBlockOpsSystemPrompt(lane) section
  builder; the default prompts are back to "four context-management
  tools". New Config.blockOps { enabled } (off by default); absorb/ccr
  tool-name predicates include the block ops.
- host: compress.blockOps config (3-level merge like absorb);
  blockOpsEnabled / effectiveBlockOpsConfig / storeEffectiveBlockOps
  (per-session, plugin tool API parity); isProxyToolFor, the proxy-lane
  inject* extras, the responses hybrid lane, the round-2 cache seam, the
  plugin manifest + tool API, the MCP dialect note and the system prompt
  all inject/advertise the 3 only while enabled; the MCP note, prompt
  and per-session metadata stay byte-identical to pre-#5765 while off
  (zero footprint: no metadata key written while off — the storage-render
  goldens match master byte-for-byte).
- dispatch: enabled calls run the kernel ops exactly as before; a stray
  call while disabled gets a model-facing "not enabled on this bili proxy
  (compress.blockOps.enabled is not true)" receipt and never touches state.
- wire-contract goldens revert to the master shapes (the default surface
  is unchanged), and this commit supersedes the three earlier fix commits
  on this branch (test-pin updates + storage-render regen + payload
  retunes): with the default surface back to the master shape, those
  test files return to the master versions and the 27 red CI tests are
  green again without any calibration changes.
- docs: compress.blockOps in configuration.md/.zh-CN.md + website seed;
  kernel 0.0.194.

Verified: kernel 1656/1656; host targeted 201/201 (the 15 previously
red CI files + block-ops/snapshot/wire-contract), 348/348 + 63/63
surface/config invariant files; typecheck + build green.
2026-11-11 11:12:54 +08:00

183 lines
6.4 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import {
COMPRESS_TOOL,
COMPRESS_TOOL_OPENAI,
COMPRESS_TOOL_RESPONSES,
ACP_TOOLS_OPENAI,
ACP_TOOLS_ANTHROPIC,
ACP_TOOLS_RESPONSES,
ACP_TOOLS_GOOGLE,
ACP_READONLY_TOOLS_RESPONSES,
ACP_TOOL_NAMES,
ACP_MUTATING_TOOLS,
ACP_READONLY_TOOLS,
BLOCK_OP_TOOL_NAMES,
ACP_RETIRE_TOOL_NAME,
ACP_RESTORE_TOOL_NAME,
ACP_REWRITE_TOOL_NAME,
ACP_TEXT_OPEN,
ACP_TEXT_CLOSE,
ACP_STATUS_OPEN,
ACP_STATUS_CLOSE,
ACP_SEARCH_OPEN,
ACP_SEARCH_CLOSE,
ACP_DECOMPRESS_OPEN,
ACP_DECOMPRESS_CLOSE,
buildCompressSystemPrompt,
buildCompressTextSystemPrompt,
buildCompressHybridSystemPrompt,
buildBlockOpsSystemPrompt,
parseCompressInput,
} from "../src/compress-tools.js";
test("ACP tool sets classify every ACP tool by mutation semantics", () => {
// #5765 block ops are opt-in (config.blockOps.enabled, default off) — like
// absorb/acp_retrieve they stay OUT of the always-on name set so the default
// manifest/surface never advertises them.
assert.deepEqual([...ACP_TOOL_NAMES].sort(), [
"acp_cache",
"acp_status",
"compress",
"decompress",
"search_context",
]);
// …but they are mutating session-state tools (same tier as compress/
// decompress): when enabled their calls must drive the compress loop.
assert.deepEqual([...ACP_MUTATING_TOOLS].sort(), [
"acp_restore",
"acp_retire",
"acp_rewrite",
"compress",
"decompress",
]);
assert.deepEqual([...BLOCK_OP_TOOL_NAMES].sort(), [
"acp_restore",
"acp_retire",
"acp_rewrite",
]);
assert.deepEqual([...ACP_READONLY_TOOLS].sort(), [
"acp_cache",
"acp_status",
"search_context",
]);
});
test("#5765: block ops stay out of the default tool arrays and prompts (opt-in)", () => {
const names = (arr: Array<{ name?: string; function?: { name?: string } }>) =>
arr.map((t) => t.name ?? t.function?.name);
for (const arr of [ACP_TOOLS_ANTHROPIC, ACP_TOOLS_OPENAI, ACP_TOOLS_RESPONSES, ACP_TOOLS_GOOGLE, ACP_READONLY_TOOLS_RESPONSES]) {
for (const n of BLOCK_OP_TOOL_NAMES) assert.ok(!names(arr).includes(n), `default array must not expose ${n}`);
}
for (const n of BLOCK_OP_TOOL_NAMES) assert.ok(!ACP_TOOL_NAMES.has(n));
// default prompts never advertise a tool the default tools array lacks
assert.ok(!buildCompressSystemPrompt().includes("acp_retire"));
assert.ok(buildCompressSystemPrompt().includes("You have four context-management tools"));
assert.ok(!buildCompressHybridSystemPrompt().includes("acp_retire"));
// the opt-in section advertises exactly the three names on both lanes
for (const lane of ["function", "hybrid"] as const) {
const section = buildBlockOpsSystemPrompt(lane);
for (const n of [ACP_RETIRE_TOOL_NAME, ACP_RESTORE_TOOL_NAME, ACP_REWRITE_TOOL_NAME]) {
assert.ok(section.includes(n), `${lane} section must mention ${n}`);
}
}
assert.ok(buildBlockOpsSystemPrompt("function").includes("BLOCK-LIFECYCLE MANAGEMENT"));
assert.ok(buildBlockOpsSystemPrompt("hybrid").includes("block-management function tools"));
});
test("tool arrays expose compress in every wire format", () => {
const names = (arr: Array<{ name?: string; function?: { name?: string } }>) =>
arr.map((t) => t.name ?? t.function?.name);
assert.ok(names(ACP_TOOLS_ANTHROPIC).includes("compress"));
assert.ok(names(ACP_TOOLS_OPENAI).includes("compress"));
assert.ok(names(ACP_TOOLS_RESPONSES).includes("compress"));
assert.equal(names(ACP_READONLY_TOOLS_RESPONSES).includes("compress"), false);
assert.equal(COMPRESS_TOOL.name, "compress");
assert.equal(COMPRESS_TOOL_OPENAI.function?.name, "compress");
assert.equal(
COMPRESS_TOOL_OPENAI.function.parameters.properties.content.type,
"string",
);
assert.equal(
COMPRESS_TOOL_OPENAI.function.parameters.properties.content.anyOf,
undefined,
);
});
test("text tags are paired delimiters", () => {
assert.equal(ACP_TEXT_OPEN, "<acp_compress>");
assert.equal(ACP_TEXT_CLOSE, "</acp_compress>");
assert.ok(ACP_STATUS_OPEN.startsWith("<acp_status"));
assert.ok(ACP_STATUS_CLOSE.startsWith("</acp_status"));
assert.ok(ACP_SEARCH_OPEN.startsWith("<acp_search"));
assert.ok(ACP_SEARCH_CLOSE.startsWith("</acp_search"));
assert.ok(ACP_DECOMPRESS_OPEN.startsWith("<acp_decompress"));
assert.ok(ACP_DECOMPRESS_CLOSE.startsWith("</acp_decompress"));
});
test("prompt builders produce non-empty guidance mentioning the compress tool", () => {
for (const p of [
buildCompressSystemPrompt(),
buildCompressTextSystemPrompt(),
buildCompressHybridSystemPrompt(),
]) {
assert.ok(p.includes("compress"));
assert.ok(p.length > 1000);
}
assert.ok(buildCompressTextSystemPrompt().includes(ACP_TEXT_OPEN));
assert.ok(buildCompressHybridSystemPrompt().includes(ACP_TEXT_OPEN));
});
test("parseCompressInput accepts single object, array content, and JSON-string content", () => {
const warns: string[] = [];
const onWarn = (m: string) => warns.push(m);
const range = {
startId: "m00001",
endId: "m00005",
summary: "s",
topic: "t",
};
const single = parseCompressInput(range, "call-1", onWarn);
assert.equal(single.length, 1);
assert.equal(single[0]?.compressCallId, "call-1");
assert.equal(single[0]?.startRef, "m00001");
assert.equal(single[0]?.endRef, "m00005");
const arr = parseCompressInput(
{ content: [range, { startId: "m00006", endId: "m00009", summary: "s2" }] },
"call-2",
onWarn,
);
assert.equal(arr.length, 2);
assert.equal(arr[1]?.topic, undefined);
// JSON-string content field (non-strict providers stringify array args)
const json = parseCompressInput(
{ content: JSON.stringify([range]) },
"call-3",
onWarn,
);
assert.equal(json.length, 1);
assert.equal(json[0]?.topic, "t");
// endId/endRef spellings both accepted
assert.equal(
parseCompressInput(
{ startRef: "m1", endRef: "m2", summary: "s" },
"c",
onWarn,
).length,
1,
);
assert.deepEqual(warns, []);
});
test("parseCompressInput rejects malformed payloads and warns", () => {
const warns: string[] = [];
const onWarn = (m: string) => warns.push(m);
assert.deepEqual(parseCompressInput(null, "c", onWarn), []);
assert.deepEqual(parseCompressInput({ content: "nope" }, "c", onWarn), []);
assert.deepEqual(
parseCompressInput({ content: [{ startId: "m1" }] }, "c", onWarn),
[],
);
assert.ok(warns.length > 0);
});