Skip to content
Merged
2 changes: 1 addition & 1 deletion kernel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "acp-kernel",
"version": "0.0.116",
"version": "0.0.117",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
70 changes: 56 additions & 14 deletions kernel/src/protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,28 @@ export function matchToolMessagePattern(
);
}

/** Host-projected virtual tool path for a non-tool message (billion-context
* #2446 family): plain-text durable-state carriers classified by the host
* run through the same pattern machinery as tool exchanges. Tool-call /
* tool-result messages are never re-projected — the classifier's domain is
* exactly the messages that carry no toolName, so a tool exchange keeps its
* existing semantics byte-for-byte. */
export function virtualToolPathOf(
msg: CoreMessage,
config: Pick<Config, "messagePathOf">,
): string | undefined {
if (
msg.contentType === "tool-call" ||
msg.contentType === "tool-result"
) {
return undefined;
}
return config.messagePathOf?.(msg);
}

export function isMessageProtected(
msg: CoreMessage,
config: Pick<Config, "protectedTools" | "isToolProtected" | "isMessageProtected">,
config: Pick<Config, "protectedTools" | "isToolProtected" | "isMessageProtected" | "messagePathOf">,
): boolean {
// Host-declared message-level protection (#2419): applies to ANY content
// type and outranks everything below — the host owns the decision.
Expand All @@ -194,6 +213,16 @@ export function isMessageProtected(
(msg.contentType !== "tool-call" && msg.contentType !== "tool-result") ||
!msg.toolName
) {
// Host-projected virtual tool path (#2446 family): a plain-text carrier
// matched by a protectedTools pattern is protected exactly like a tool
// exchange would be. Path-pattern semantics are identical ("durable"
// matches "durable/…" and its descendants; "*" never crosses "/").
const virtualPath = virtualToolPathOf(msg, config);
if (virtualPath) {
for (const pattern of config.protectedTools) {
if (matchToolPath(virtualPath, pattern)) return true;
}
}
return false;
}

Expand Down Expand Up @@ -266,22 +295,30 @@ export interface LatestProtected {

export function collectLatestProtected(
messages: CoreMessage[],
config: Pick<Config, "protectedLatestTools">,
config: Pick<Config, "protectedLatestTools" | "messagePathOf">,
): LatestProtected {
const callIds = new Set<string>();
const msgIds = new Set<string>();
const patterns = config.protectedLatestTools ?? [];
if (patterns.length === 0) return { callIds, msgIds };
for (const pattern of patterns) {
// Path patterns (containing `/`) protect the latest instance PER PATH —
// `skill/*` keeps the newest load of every skill, not just one (#1947).
// Plain tool-name patterns keep the single-latest semantics.
// `skill/*` keeps the newest load of every skill, not just one (#1947),
// and `durable/*` keeps the newest instance of every host-projected
// durable carrier identity. Plain tool-name patterns keep the
// single-latest semantics.
if (pattern.includes("/")) {
const lastByPath = new Map<string, CoreMessage>();
for (const m of messages) {
if (m.contentType !== "tool-call" || !m.toolName) continue;
if (!matchToolMessagePattern(m, pattern)) continue;
lastByPath.set(toolPathOf(m), m);
if (m.contentType === "tool-call" && m.toolName) {
if (!matchToolMessagePattern(m, pattern)) continue;
lastByPath.set(toolPathOf(m), m);
continue;
}
const virtualPath = virtualToolPathOf(m, config);
if (virtualPath && matchToolPath(virtualPath, pattern)) {
lastByPath.set(virtualPath, m);
}
}
for (const last of lastByPath.values()) {
if (last.toolCallId) callIds.add(last.toolCallId);
Expand All @@ -291,11 +328,14 @@ export function collectLatestProtected(
}
let last: CoreMessage | undefined;
for (const m of messages) {
if (
m.contentType === "tool-call" &&
m.toolName &&
matchToolMessagePattern(m, pattern)
) {
if (m.contentType === "tool-call" && m.toolName) {
if (matchToolMessagePattern(m, pattern)) {
last = m;
}
continue;
}
const virtualPath = virtualToolPathOf(m, config);
if (virtualPath && matchToolPath(virtualPath, pattern)) {
last = m;
}
}
Expand All @@ -307,12 +347,14 @@ export function collectLatestProtected(
}

/** True when msg is a latest-protected tool-call, or the tool-result paired to
* one (by toolCallId). */
* one (by toolCallId). msgIds entries may also be latest TEXT carriers
* (host-projected virtual tool paths carry no toolCallId), so the id
* membership check runs for every content type. */
export function isMessageLatestProtected(
msg: CoreMessage,
latest: LatestProtected,
): boolean {
if (msg.contentType === "tool-call" && latest.msgIds.has(msg.id)) return true;
if (latest.msgIds.has(msg.id)) return true;
if (
(msg.contentType === "tool-call" || msg.contentType === "tool-result") &&
msg.toolCallId &&
Expand Down
13 changes: 13 additions & 0 deletions kernel/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,19 @@ export interface Config {
* media payloads are protectable). Must be pure and cheap: it runs once
* per message per turn. */
isMessageProtected?: (msg: CoreMessage) => boolean;
/** Host-projected VIRTUAL TOOL PATH for messages that are not tool
* exchanges (plain-text durable-state carriers, billion-context#2446
* family): when set, the host classifier maps a message to a canonical
* path such as "durable/skill-catalog", and the SAME protectedTools /
* protectedLatestTools pattern machinery that governs tool exchanges then
* governs these messages too — all-instance protection via protectedTools,
* latest-per-path via protectedLatestTools. This keeps the POLICY in
* config while the host only owns the dialect→identity translation (the
* same projection principle as #1947's skill/<name>). Tool-call /
* tool-result messages are NEVER re-projected — the classifier's domain is
* exactly the messages that carry no toolName. Must be pure and cheap: it
* runs once per message per protection scan. */
messagePathOf?: (msg: CoreMessage) => string | undefined;
/** Tool-name patterns (glob suffix allowed) protected in their LATEST
* instance only: the newest matching tool-call and its paired tool-result
* are protected from compression; older instances remain compressible. For
Expand Down
145 changes: 145 additions & 0 deletions kernel/tests/protected-virtual-paths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import {
isMessageProtected,
collectLatestProtected,
isMessageLatestProtected,
virtualToolPathOf,
} from "../src/protected.js";
import { defaultConfig } from "../src/config.js";
import type { CoreMessage } from "../src/types.js";

// #2446 family: host-projected VIRTUAL TOOL PATHS. A host classifier maps
// plain-text durable-state carriers to canonical `durable/<name>` paths via
// Config.messagePathOf; the SAME protectedTools / protectedLatestTools
// pattern machinery that governs tool exchanges then governs them. Tool
// exchanges are never re-projected.

function text(id: string, t: string): CoreMessage {
return { id, role: "user", contentType: "text", text: t };
}

function toolCall(id: string, toolName: string, callId: string): CoreMessage {
return {
id,
role: "assistant",
contentType: "tool-call",
toolName,
toolCallId: callId,
text: "{}",
};
}

/** Classifier fixture: `<catalog>` text → durable/skill-catalog, `<rules>` →
* durable/agents-md; a tool-call carrying the same marker text must be
* IGNORED (projection domain is non-tool messages only). */
function classifier(msg: CoreMessage): string | undefined {
if (msg.contentType === "tool-call" || msg.contentType === "tool-result") {
return "should-never-be-consulted";
}
if (msg.text?.includes("<catalog>")) return "durable/skill-catalog";
if (msg.text?.includes("<rules>")) return "durable/agents-md";
return undefined;
}

function cfg(overrides: Partial<Parameters<typeof isMessageProtected>[1]>) {
const base = defaultConfig(200_000);
return { ...base, messagePathOf: classifier, ...overrides } as Parameters<
typeof isMessageProtected
>[1];
}

test("virtualToolPathOf: never projects tool exchanges, passes through text", () => {
const c = { messagePathOf: classifier };
assert.equal(virtualToolPathOf(text("t1", "<catalog>"), c), "durable/skill-catalog");
assert.equal(virtualToolPathOf(text("t2", "plain"), c), undefined);
assert.equal(
virtualToolPathOf(toolCall("c1", "skill", "call-1"), c),
undefined,
"tool-call is never re-projected even when the classifier could match",
);
assert.equal(virtualToolPathOf({ ...toolCall("c2", "skill", "call-2"), text: "<catalog>" }, c), undefined);
});

test("protectedTools patterns govern virtual paths: exact, first-segment, and glob", () => {
const catalog = text("t1", "<catalog>");
assert.equal(
isMessageProtected(catalog, cfg({ protectedTools: ["durable/skill-catalog"] })),
true,
"exact path pattern protects the matching carrier",
);
assert.equal(
isMessageProtected(catalog, cfg({ protectedTools: ["durable"] })),
true,
"a single-segment pattern matches the first path segment (durable ≡ durable/*)",
);
assert.equal(
isMessageProtected(catalog, cfg({ protectedTools: ["durable/*"] })),
true,
"glob covers every durable identity",
);
assert.equal(
isMessageProtected(text("t2", "<rules>"), cfg({ protectedTools: ["durable/skill-catalog"] })),
false,
"a pattern for ANOTHER identity does not protect this carrier",
);
assert.equal(
isMessageProtected(text("t3", "plain prose"), cfg({ protectedTools: ["durable/*"] })),
false,
"unclassified text stays consumable",
);
assert.equal(
isMessageProtected(catalog, cfg({ protectedTools: [] })),
false,
"no patterns → no protection (policy lives in config)",
);
// Case-insensitivity parity with the tool channel (#1725):
assert.equal(
isMessageProtected(catalog, cfg({ protectedTools: ["Durable/Skill-Catalog"] })),
true,
"pattern matching stays case-insensitive on both sides",
);
});

test("virtual-path protection composes with tool-channel protection in one config", () => {
const c = cfg({ protectedTools: ["read"] });
assert.equal(isMessageProtected(toolCall("c1", "read", "call-1"), c), true, "tool channel unchanged");
assert.equal(isMessageProtected(text("t1", "<catalog>"), c), false, "durable/* not configured → text unprotected");
const c2 = cfg({ protectedTools: ["read", "durable/*"] });
assert.equal(isMessageProtected(toolCall("c2", "read", "call-2"), c2), true);
assert.equal(isMessageProtected(text("t2", "<catalog>"), c2), true, "both channels protected together");
});

test("protectedLatestTools path patterns keep the LATEST instance per durable identity", () => {
const messages = [
text("a", "<catalog> v1"),
text("b", "plain filler"),
text("c", "<catalog> v2"), // newest skill-catalog
text("d", "<rules> v1"), // only agents-md instance
];
const latest = collectLatestProtected(messages, cfg({ protectedLatestTools: ["durable/*"], protectedTools: [] }));
assert.equal(isMessageLatestProtected(text("a", "<catalog> v1"), latest), false, "older catalog instance is foldable");
assert.equal(isMessageLatestProtected(text("c", "<catalog> v2"), latest), true, "newest catalog instance survives");
assert.equal(isMessageLatestProtected(text("d", "<rules> v1"), latest), true, "every identity keeps its own latest");
assert.equal(isMessageLatestProtected(text("b", "plain filler"), latest), false);
});

test("protectedLatestTools plain-name patterns: virtual carriers join single-latest semantics", () => {
const messages = [text("a", "<catalog> v1"), text("b", "<rules> v1"), text("c", "<catalog> v2")];
const latest = collectLatestProtected(messages, cfg({ protectedLatestTools: ["durable"], protectedTools: [] }));
assert.equal(isMessageLatestProtected(text("a", "<catalog> v1"), latest), false);
assert.equal(isMessageLatestProtected(text("b", "<rules> v1"), latest), false, "not the latest durable carrier overall");
assert.equal(isMessageLatestProtected(text("c", "<catalog> v2"), latest), true, "the LAST durable carrier wins");
});

test("protectedLatestTools for TOOLS is unchanged when a classifier is attached", () => {
const messages = [
toolCall("c1", "todo_list", "call-1"),
toolCall("c2", "todo_list", "call-2"),
text("t", "<catalog>"),
];
const latest = collectLatestProtected(messages, cfg({ protectedLatestTools: ["todo_list"] }));
assert.equal(isMessageLatestProtected(messages[0]!, latest), false, "older todo_list still foldable");
assert.equal(isMessageLatestProtected(messages[1]!, latest), true, "newest todo_list still protected");
assert.equal(isMessageLatestProtected(messages[2]!, latest), false, "carrier not in latest scope (pattern is tool-only)");
});
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ export type CompressSettings = {
* Default: none — opt in per client/agent, since tool names are
* client-specific. */
protectedLatestTools?: string[];
/* (durable virtual paths) dsh lanes project their durable-state TEXT
* carriers to `durable/<name>` identities (#2446 family: skill catalog,
* mcp catalog, agents-md baseline, runtime-context snapshot), so the
* same pattern machinery governs them: `"durable/*"` keeps the LATEST
* instance of every carrier (all four are cumulative snapshots — the
* older catalogs/baselines fold away). Default when nothing is
* configured: `durable/*` latest-per-identity in
* `protectedLatestTools` (pi-reference single-live-copy semantics —
* skill/MCP visibility can never be lost, superseded snapshots
* fold). */
/** Tool patterns whose tool-calls AND paired results are NEVER
* compressed — every instance, full history (kernel `protectedTools`,
* hard exclusion: matching refs render as `BLOCKED`, so neither suggested
Expand All @@ -270,6 +280,12 @@ export type CompressSettings = {
* Default: none — opt in per client/agent, since tool names are
* client-specific. */
protectedTools?: string[];
/* (durable virtual paths) any pattern whose first path segment is
* `durable` takes over the whole durable policy for that lane
* (whole-identity takeover): bili attaches only the identity
* classifier and injects NO default — e.g. `"durable/agents-md"`
* protects just the workspace baseline while skill/mcp catalogs fold
* normally. Unregistered lanes get no projection at all. */
/** Tool-name patterns EXCLUDED from the soft-protected recent zone —
* matching tool results inside the recent zone become compressible
* immediately instead of aging out first (kernel
Expand Down
Loading
Loading