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
4 changes: 2 additions & 2 deletions kernel/package-lock.json

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

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.200",
"version": "0.0.201",
"description": "Framework-agnostic context-compression engine (model-driven, 3-tier LSM). Pure core: no host dependency.",
"license": "MIT",
"author": "ranxianglei",
Expand Down
1 change: 1 addition & 0 deletions kernel/src/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,7 @@ const hideCompressCallsNode: PipelineNode = {
io.state,
io.messages,
ctx.config.isCompressTool,
ctx.config.strandedProtectedPredecessor,
);
// #3769: fold the newly-swept orphans into the persistent monotonic set so
// they stay hidden across turns/restarts/upgrades, then consume the fold
Expand Down
26 changes: 26 additions & 0 deletions kernel/src/hide-consumed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,29 @@ function compactCompressText(text: string | undefined): string | null {
return changed ? prefix + out : null;
}

// #5864: does the assistant-message run ending at index `i` contain a protected
// (signed thinking / redacted_thinking) core anywhere before it? A run is the
// maximal stretch of consecutive assistant-role cores — exactly what
// coreToAnthropic renders as ONE assistant message — so the scan stops at the
// first non-assistant core. Lets the caller keep a compress pair whose thinking
// sibling is separated from it by intervening text/tool cores in the same turn.
function runHasStrandedProtectedPredecessor(
messages: CoreMessage[],
i: number,
pred: (m: CoreMessage) => boolean,
): boolean {
for (let j = i - 1; j >= 0 && messages[j]!.role === "assistant"; j--) {
if (pred(messages[j]!)) return true;
}
return false;
}

export function hideConsumedCompressCalls(
state: CompressionState,
messages: CoreMessage[],
isCompressTool: (toolName: string) => boolean = (name) =>
name === COMPRESS_TOOL_NAME,
strandedProtectedPredecessor: (m: CoreMessage) => boolean = () => false,
): HideConsumedResult {
const allBlockCallIds = new Set<string>();
const activeCallIds = new Set<string>();
Expand Down Expand Up @@ -352,6 +370,14 @@ export function hideConsumedCompressCalls(
}
}
}
// #5864: hiding this pair would strand a signed thinking-family block that
// sits earlier in the SAME assistant message — an intermediate text/tool
// core between the thinking and the call is common, so the immediate
// predecessor alone is not enough. Signed thinking must round-trip with its
// tool_use sibling (#1658/#1960/#684) or upstream rejects the altered
// replay. Keep every compress pair whose assistant run holds a protected
// predecessor, not just the one right after the thinking.
if (hide && runHasStrandedProtectedPredecessor(messages, i, strandedProtectedPredecessor)) hide = false;
if (hide) hiddenCallIds.add(callId);
else keepCallIds.add(callId);
}
Expand Down
12 changes: 12 additions & 0 deletions kernel/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,18 @@ export interface Config {
* it (billion-context#2970). Unset = bare-name only (byte-identical to
* before). Must be pure and cheap: runs once per message per pass. */
isCompressTool?: (toolName: string) => boolean;
/** Host-declared predicate marking a core whose position must never become
* the FINAL block of its assistant message — a signed Anthropic
* thinking/redacted_thinking block (#1658/#1960: signed thinking must
* round-trip with its tool_use sibling and cannot be dropped or rewritten).
* When the core immediately preceding a compress call matches,
* hideConsumedCompressCalls keeps that call+result pair VISIBLE instead of
* hiding it, so the thinking block is never stranded as a thinking-final
* assistant message (upstream 400 "the final block in an assistant message
* cannot be thinking", billion-context#5864). Unset = no protection
* (byte-identical to before). Must be pure and cheap: runs once per hidden
* compress call per pass. */
strandedProtectedPredecessor?: (msg: CoreMessage) => boolean;
/** Host-declared message-level hard protection (billion-context#2419):
* when set, ANY message for which the predicate returns true is excluded
* from compression exactly like a protectedTools hit — BLOCKED ref in
Expand Down
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.

13 changes: 12 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,17 @@ export function isMcpBiliCompress(toolName: string): boolean {
toolName.slice(MCP_BILI_TOOL_PREFIX.length) === "compress");
}

// #5864: twin of server.ts hasSignedThinkingBlock (#1658/#1960 doctrine) — a
// signed Anthropic thinking/redacted_thinking core must never be stranded as
// the final block of its assistant message. hideConsumedCompressCalls uses it
// to keep a preceding compress pair visible instead of hiding it. Structural
// param (only the two wire fields it reads) so it stays assignable to the
// kernel's (msg: CoreMessage) => boolean Config field.
export function isStrandedProtectedPredecessor(m: { contentType?: string; thinkingSignature?: string; rawAnthropicBlock?: unknown }): boolean {
if (m.contentType === "reasoning") return typeof m.thinkingSignature === "string" && m.thinkingSignature.length > 0;
return (m.rawAnthropicBlock as { type?: unknown } | undefined)?.type === "redacted_thinking";
}

export function loadOptions(env: NodeJS.ProcessEnv = process.env): ProxyOptions {
// --- Source 1: JSON config file (~/.config/billion-context/billion-context.json) ---
// The canonical, user-editable config. Loaded first so env vars below can
Expand Down Expand Up @@ -1787,7 +1798,7 @@ export function loadOptions(env: NodeJS.ProcessEnv = process.env): ProxyOptions
modelContextLimit,
// #2578: host-side extension of the kernel's bare-name hard protection
// to the MCP-prefixed forms of bili's own tools.
kernelConfig: { ...defaultConfig(modelContextLimit), isToolProtected: isMcpBiliHardProtected, isCompressTool: isMcpBiliCompress },
kernelConfig: { ...defaultConfig(modelContextLimit), isToolProtected: isMcpBiliHardProtected, isCompressTool: isMcpBiliCompress, strandedProtectedPredecessor: isStrandedProtectedPredecessor },
compress: {
...(fileConfig.compress ?? {}),
injectTool: (env.ACP_COMPRESS_TOOL ?? (fileConfig.compress?.injectTool === false ? "0" : "1")) !== "0",
Expand Down
2 changes: 1 addition & 1 deletion src/loop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1653,7 +1653,7 @@ export async function* runCompressLoop(
(pr) => (pr.name === "compress" || pr.name === "decompress") && proxyToolResultFailed(pr.result),
);
if (!ctx.textProtocol && !anyCompressFailed) {
const hidden = hideConsumedCompressCalls(ctx.session.state, coreMessages, ctx.config.isCompressTool);
const hidden = hideConsumedCompressCalls(ctx.session.state, coreMessages, ctx.config.isCompressTool, ctx.config.strandedProtectedPredecessor);
if (hidden.hidden > 0) {
ctx.log(`[acp-loop] round ${round} hideConsumed hid ${hidden.hidden} compress record(s)`);
replaceMessages(coreMessages, hidden.messages);
Expand Down
11 changes: 7 additions & 4 deletions src/stream.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createHash, randomBytes } from "node:crypto";
import { collectBlockContent, countMessageTokens, defaultCountTokens, formatRanges, formatTagTime, hideConsumedCompressCalls, indexToRef, parseBoundary, storeCoveredOriginals, viableRanges, type CompressionCore, type Config, type CompressionBlock, type CompressibleRange, type CoreMessage, type CompressionState, type NudgeDecision, type CompressParseDiagnostics, refToIndex } from "acp-kernel";
import { loadConfigFile, resolveMessageTimestamps } from "./config.js";
import { isMcpBiliCompress, isStrandedProtectedPredecessor, loadConfigFile, resolveMessageTimestamps } from "./config.js";
import { excludeDeadRefRanges, handleAcpStatus, liveCompressionRanges } from "./acp-status.js";
import { handleAcpCache, prefixTokensBeforeRef, recordCacheFoldsFromBlocks } from "./cache-ledger.js";
import { type Session, cacheBlockContent, markDirty, residentPressure, scrubTurnSeparatorIds, statusInputBaseline, truncatedUpstreamOf, withoutTurnSeparators } from "./session.js";
Expand Down Expand Up @@ -907,19 +907,22 @@ function formatMiB(bytes: number | undefined): string {
// variant: re-folding the same block silently SHRINKS its canonical summary).
// The stub set is harvested from the kernel's OWN outbound rewrite of the
// current messages rather than mirrored from constants: whatever fragments the
// model can actually see right now are exactly what gets rejected. Exact match
// model can actually see right now are exactly what gets rejected. The scan
// mirrors the outbound hide decision (#2970 MCP-name recognition + #5864
// strand guard) so a pair the outbound path keeps visible is scanned here too.
// Exact match
// only — legitimate prose (summaries containing an ellipsis, short summaries,
// ±1-char transcription drift) can never trip it.
export function visibleSummaryStubs(messages: CoreMessage[], state: CompressionState): Set<string> {
let hidden: { messages: CoreMessage[] };
try {
hidden = hideConsumedCompressCalls(state, messages);
hidden = hideConsumedCompressCalls(state, messages, isMcpBiliCompress, isStrandedProtectedPredecessor);
} catch {
return new Set();
}
const stubs = new Set<string>();
for (const m of hidden.messages) {
if (m.contentType !== "tool-call" || m.toolName !== COMPRESS_TOOL_NAME || typeof m.text !== "string") continue;
if (m.contentType !== "tool-call" || !m.toolName || !isMcpBiliCompress(m.toolName) || typeof m.text !== "string") continue;
let parsed: ReturnType<typeof parseCompressInput>;
try {
// #5161: quiet — this scan re-parses past calls on every fold; in
Expand Down
Loading
Loading