Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/absorb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { ACP_TOOL_NAMES, ABSORB_TOOL_NAME } from "./compress-tools.js";
import { isStoredPlaceholderText } from "./ccr.js";
import {
collectLatestProtected,
collectRulePairProtection,
isMessageLatestProtected,
isMessageProtected,
isMessageRuleProtected,
matchToolPattern,
type LatestProtected,
type RulePairProtection,
} from "./protected.js";
import type {
AbsorbConfig,
Expand Down Expand Up @@ -87,13 +90,15 @@ export function isAbsorbCandidate(
msg: CoreMessage,
config: Config,
latest?: LatestProtected,
rule?: RulePairProtection,
): boolean {
if (msg.contentType !== "tool-result" || !msg.toolCallId) return false;
if (isStoredPlaceholderText(msg.text ?? "")) return false;
const cfg = resolveAbsorbConfig(config);
if (isAcpOrConfiguredTool(msg.toolName, cfg)) return false;
if (isMessageProtected(msg, config)) return false;
if (latest && isMessageLatestProtected(msg, latest)) return false;
if (rule && isMessageRuleProtected(msg, rule)) return false;
for (const pattern of cfg.excludeTools) {
if (msg.toolName && matchToolPattern(msg.toolName, pattern)) return false;
}
Expand Down Expand Up @@ -152,8 +157,9 @@ export function appendAbsorbPrompts(

let promptedCount = 0;
const latest = collectLatestProtected(messages, config);
const ruleProt = collectRulePairProtection(messages);
const out = messages.map((msg) => {
if (!isAbsorbCandidate(msg, config, latest)) return msg;
if (!isAbsorbCandidate(msg, config, latest, ruleProt)) return msg;
if (absorbedIds.has(msg.id)) return msg;
const text = msg.text ?? "";
if (text.includes(ABSORB_PROMPT_MARKER)) return msg;
Expand Down Expand Up @@ -305,7 +311,8 @@ export function applyAbsorb(input: AbsorbInput): AbsorbOutcome {
isMessageLatestProtected(
target,
collectLatestProtected(input.messages, input.config),
)
) ||
isMessageRuleProtected(target, collectRulePairProtection(input.messages))
) {
return {
state: input.state,
Expand Down
14 changes: 12 additions & 2 deletions src/ccr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import type {
} from "./types.js";
import { refForRaw, BLOCKED_REF } from "./refs.js";
import { ACP_TOOL_NAMES } from "./compress-tools.js";
import { isMessageProtected, matchToolPattern } from "./protected.js";
import {
collectRulePairProtection,
isMessageProtected,
isMessageRuleProtected,
matchToolPattern,
} from "./protected.js";
import { hasStoredRef, retrieveByRef, storeOriginal } from "./content-store.js";
import type {
MessageContentStore,
Expand Down Expand Up @@ -271,6 +276,7 @@ export function storeLargeResults(
}
}

const ruleProt = collectRulePairProtection(input.messages);
const updated = input.messages.map((message) => {
if (message.contentType !== "tool-result") return message;
const text = message.text ?? "";
Expand All @@ -290,7 +296,11 @@ export function storeLargeResults(
) {
return message;
}
if (isMessageProtected(message, input.config)) return message;
if (
isMessageProtected(message, input.config) ||
isMessageRuleProtected(message, ruleProt)
)
return message;
const tokens = input.countTokens(text);
if (tokens < cfg.minToolTokens) return message;
const ref = refForRaw(input.state.messageRefs, message.id);
Expand Down
10 changes: 9 additions & 1 deletion src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ import { createRenderRefsNode } from "./render-refs.js";
import type { RenderStrategy } from "./render-refs.js";
import {
collectLatestProtected,
collectRulePairProtection,
hasMediaPayload,
isMessageLatestProtected,
isMessageProtected,
isMessageRuleProtected,
} from "./protected.js";
import { adjustBoundariesForToolPairs } from "./tool-pairs.js";
import { adjustBoundariesForReasoningPairs } from "./reasoning-pairs.js";
Expand Down Expand Up @@ -599,11 +601,15 @@ const assignRefsNode: PipelineNode = {
const latest = hasProtection
? collectLatestProtected(io.messages, ctx.config)
: undefined;
// Built-in conditional protection (no config gate): live acp_rule record
// pairs get a BLOCKED ref exactly like always-protected tools.
const ruleProt = collectRulePairProtection(io.messages);
// Media payloads (image/file sidecars) ride outside msg.text; folding one
// destroys it permanently (#1188), so media messages always get a BLOCKED
// ref — never advertised, never folded — regardless of tool-protection config.
const protectedFn = (m: CoreMessage) =>
hasMediaPayload(m) ||
isMessageRuleProtected(m, ruleProt) ||
(hasProtection
? isMessageProtected(m, ctx.config) ||
(latest ? isMessageLatestProtected(m, latest) : false)
Expand Down Expand Up @@ -1263,6 +1269,7 @@ function filterProtectedToolMessages(
const removedIds = new Set<string>();
const latest = collectLatestProtected(messages, config);
for (const id of latest.callIds) protectedCallIds.add(id);
const ruleProt = collectRulePairProtection(messages);
for (const msg of messages) {
if (isMessageProtected(msg, config) && msg.toolCallId) {
protectedCallIds.add(msg.toolCallId);
Expand All @@ -1275,7 +1282,8 @@ function filterProtectedToolMessages(
if (
hasMediaPayload(msg) ||
isMessageProtected(msg, config) ||
isMessageLatestProtected(msg, latest)
isMessageLatestProtected(msg, latest) ||
isMessageRuleProtected(msg, ruleProt)
) {
removedIds.add(id);
if (msg.toolCallId) protectedCallIds.add(msg.toolCallId);
Expand Down
5 changes: 3 additions & 2 deletions src/crush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import {
isAbsorbCandidate,
resolveAbsorbConfig,
} from "./absorb.js";
import { matchToolPattern } from "./protected.js";
import { collectRulePairProtection, matchToolPattern } from "./protected.js";
import { defaultCountTokens, type TokenCountFn } from "./tokenize.js";
import type {
AbsorbConfig,
Expand Down Expand Up @@ -1798,8 +1798,9 @@ export function applyCrushToMessages(
let distilledCount = 0;
let changed = false;
const out: CoreMessage[] = [];
const ruleProt = collectRulePairProtection(messages);
for (const msg of messages) {
if (!isAbsorbCandidate(msg, config)) {
if (!isAbsorbCandidate(msg, config, undefined, ruleProt)) {
out.push(msg);
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,14 @@ export {
} from "./search.js";
export {
collectLatestProtected,
collectRulePairProtection,
hasMediaPayload,
isMessageLatestProtected,
isMessageProtected,
isMessageRuleProtected,
matchToolPattern,
type LatestProtected,
type RulePairProtection,
} from "./protected.js";
export {
runPipeline,
Expand Down
117 changes: 112 additions & 5 deletions src/protected.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import type { Config, CoreMessage } from "./types.js";
import { RULE_TOOL_NAME } from "./rules.js";

/** Tools that are ALWAYS protected, regardless of user config. These are ACP's
* own metadata tools whose records must remain in context: compress calls
* carry the summaries that decompress/search rely on, and the system prompt
* treats past compress calls as load-bearing metadata. acp_rule calls record
* persistent user rules that are re-injected every turn — compressing them
* away would silently lose what the rules state. Letting any of these be
* compressed away breaks decompress and the "summary is historical" contract. */
export const ALWAYS_PROTECTED_TOOLS = ["compress", "acp_rule"] as const;
* treats past compress calls as load-bearing metadata. Letting any of these
* be compressed away breaks decompress and the "summary is historical"
* contract.
*
* acp_rule pairs used to live here too; they moved to conditional protection
* (collectRulePairProtection below): a recorded rule's pair stays protected
* while the rule is still live, and once the host removes/clears the rule the
* pair becomes ordinary foldable content (billion-context#1177). */
export const ALWAYS_PROTECTED_TOOLS = ["compress"] as const;

/** Tool results that must NEVER participate in the soft-protected recent zone
* (preserveRecentMessages / preserveRecentTokens / last user message).
Expand Down Expand Up @@ -208,3 +213,105 @@ export function hasMediaPayload(msg: CoreMessage): boolean {
function isObjWith(v: unknown, key: string, value: unknown): boolean {
return typeof v === "object" && v !== null && (v as Record<string, unknown>)[key] === value;
}

/** Result-text prefixes emitted by host acp_rule executors (billion-context's
* executeRule is the reference implementation): successful record / removal /
* full-clear. They form a closed partition of liveness events — no other
* executor outcome starts with them (listings start with a digit or "No",
* errors with lowercase words). */
const RULE_RECORD_RESULT_RE = /^Recorded (rule\d+): /;
const RULE_REMOVE_RESULT_RE = /^Removed (rule\d+): /;
const RULE_CLEAR_RESULT_RE = /^Cleared \d+ rule\(s\)\./;

export interface RulePairProtection {
/** toolCallIds whose still-live record pair (call AND result) stays protected. */
callIds: Set<string>;
/** Message ids of protected halves lacking a pairable toolCallId, plus
* fail-closed malformed record results. */
msgIds: Set<string>;
}

/**
* Conditional protection for acp_rule pairs, derived from the visible message
* stream itself — no state dependency, because plugin-mode hosts fold in their
* own kernel copy, which never sees the server-side rules table, while the
* message stream is identical in both modes (billion-context#1177).
*
* Liveness walk over acp_rule tool-results in message order: `Recorded ruleN:`
* adds N, `Removed ruleN:` removes N, `Cleared k rule(s).` clears all. A
* record pair is protected iff its id survives the walk (ids are never
* re-issued, so final-set membership equals per-event liveness). Every other
* acp_rule exchange — removals, clears, listings, failures — is ordinary
* compressible content: they are event traces, not persistent reminders, and
* pinning them forever would recreate the accumulation problem the feature
* exists to fix.
*
* Fail-closed: a result starting `Recorded ` with an unparseable id keeps its
* pair protected (format drift must never silently fold a live rule). Without
* any removal/clear events the walk protects every record pair — behaviorally
* identical to the old unconditional name-based protection, so this change is
* a no-op until hosts actually delete rules.
*/
export function collectRulePairProtection(
messages: CoreMessage[],
): RulePairProtection {
const callNameById = new Map<string, string>();
for (const m of messages) {
if (m.contentType === "tool-call" && m.toolCallId && m.toolName) {
callNameById.set(m.toolCallId, m.toolName);
}
}
const isRuleResult = (m: CoreMessage): boolean =>
m.contentType === "tool-result" &&
(m.toolName === RULE_TOOL_NAME ||
(m.toolCallId != null &&
callNameById.get(m.toolCallId) === RULE_TOOL_NAME));

const live = new Set<string>();
const malformedMsgIds = new Set<string>();
const callIds = new Set<string>();
for (const m of messages) {
if (!isRuleResult(m)) continue;
const text = m.text ?? "";
const removedId = RULE_REMOVE_RESULT_RE.exec(text)?.[1];
if (removedId !== undefined) {
live.delete(removedId);
continue;
}
if (RULE_CLEAR_RESULT_RE.test(text)) {
live.clear();
continue;
}
const recordedId = RULE_RECORD_RESULT_RE.exec(text)?.[1];
if (recordedId !== undefined) {
live.add(recordedId);
} else if (text.startsWith("Recorded ")) {
malformedMsgIds.add(m.id);
if (m.toolCallId) callIds.add(m.toolCallId);
}
}

const msgIds = new Set<string>(malformedMsgIds);
for (const m of messages) {
if (!isRuleResult(m)) continue;
const recordedId = RULE_RECORD_RESULT_RE.exec(m.text ?? "")?.[1];
if (recordedId === undefined || !live.has(recordedId)) continue;
if (m.toolCallId) callIds.add(m.toolCallId);
else msgIds.add(m.id);
}
return { callIds, msgIds };
}

/** True when msg is half of a still-live record pair per
* collectRulePairProtection (both halves share the pair's toolCallId), or a
* fail-closed malformed record result. */
export function isMessageRuleProtected(
msg: CoreMessage,
protection: RulePairProtection,
): boolean {
if (msg.contentType !== "tool-call" && msg.contentType !== "tool-result") {
return false;
}
if (protection.msgIds.has(msg.id)) return true;
return msg.toolCallId != null && protection.callIds.has(msg.toolCallId);
}
7 changes: 6 additions & 1 deletion src/recommend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ import type { CompressionState } from "./types.js";
import {
collectLatestProtected,
collectProtectedToolCallIds,
collectRulePairProtection,
hasMediaPayload,
isMessageLatestProtected,
isMessageProtectedWithPairing,
isMessageRuleProtected,
isNeverPreserveRecent,
} from "./protected.js";
import { countMessageTokens } from "./tokenize.js";
Expand Down Expand Up @@ -177,6 +179,8 @@ export function buildCompressibleRanges(
// matches tool-results).
const latest = collectLatestProtected(messages, config);
for (const id of latest.callIds) protectedCallIds.add(id);
// acp_rule record pairs: conditional protection derived from the stream.
const ruleProt = collectRulePairProtection(messages);

// Segmentation is array adjacency, never ref arithmetic: surface-replacing
// hosts leave holes in the ref map (compressed messages leave the array, refs
Expand Down Expand Up @@ -209,7 +213,8 @@ export function buildCompressibleRanges(

if (
isMessageProtectedWithPairing(msg, config, protectedCallIds) ||
isMessageLatestProtected(msg, latest)
isMessageLatestProtected(msg, latest) ||
isMessageRuleProtected(msg, ruleProt)
) {
protectedMsgs.push({
ref,
Expand Down
9 changes: 6 additions & 3 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import type { CompressionState, Config, RuleRecord } from "./types.js";
/**
* Persistent rules ("acp_rule") — durable reminders recorded for future turns
* (kernel #282, from billion-context-pi#433). The adapter injects
* formatRulesForPrompt(state) into the system prompt every turn, and acp_rule
* tool calls/results are hard-protected from compression
* (ALWAYS_PROTECTED_TOOLS), so a recorded rule survives context compaction.
* formatRulesForPrompt(state) into the system prompt every turn. A recorded
* rule's call/result pair stays protected from compression while the rule is
* live — protection is derived from the message stream itself
* (collectRulePairProtection in protected.ts), and once the host removes or
* clears the rule the pair becomes ordinary foldable content
* (billion-context#1177).
*
* State-mutating helpers: hosts hold live CompressionState objects (a session's
* single state instance) and discard returned states — these helpers update
Expand Down
8 changes: 5 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,18 @@ export interface TerminalEscapeSignal {
}

/** A persistent reminder recorded via the acp_rule tool (see rules.ts).
* Injected into the system prompt every turn by the adapter and hard-protected
* from compression. Ids are never re-issued (see nextRuleId). */
* Injected into the system prompt every turn by the adapter; its record pair
* stays protected from compression while the rule is live (collectRulePairProtection).
* Ids are never re-issued (see nextRuleId). */
export interface RuleRecord {
id: string;
text: string;
}

/** Persistent-rule ("acp_rule") feature settings (see rules.ts). Absent or
* enabled !== true = feature off — the adapter never injects the tool; kernel
* protection is unconditional but side-effect-free without acp_rule messages.
* pair-protection is stream-derived and side-effect-free without acp_rule
* messages (live record pairs protected, other exchanges foldable).
* Deviates from kernel#282's original "no Config field" stance: hosts gate the
* feature and carry their limit overrides through the resolved Config
* (billion-context#750 reads config.rules.enabled + resolveRuleLimits). */
Expand Down
Loading
Loading