mirror of
https://github.com/ranxianglei/billion-context.git
synced 2026-11-23 01:58:16 +08:00
fix: decompress loses content across rounds (cache originals at compress time)
decompress used collectBlockContent(state, block, ctx.messages) which matches
block.effectiveMessageIds against CoreMessage.id in ctx.messages. But
ctx.messages is the POST-FOLD view (prepared.processedMessages): messages
already compressed in an earlier round are replaced by their summary and are
absent, so collectBlockContent returns 0 items. The block was then
deactivated unconditionally, discarding it and making later retries fail.
A second, deeper issue: message ids are raw-${idx}, regenerated every round
by responsesToCore, so ids are not stable across rounds — even keeping the
pre-fold messages would not guarantee a match.
Fix: cache the original content at compress time, when the source messages
are still present in this round's ctx.messages (a freshly-compressed block
has not been folded yet — folding happens in the next processTurn). The
cache lives on the session (session.blockContents: Map<blockId, {text,count}>).
decompress reads from the cache first and only falls back to
collectBlockContent. deactivateBlock now runs only when content was actually
recovered (count>0 or a cache hit), so a 0-result no longer poisons the block.
Applied to both the chat path (compress-loop.ts) and the responses path
(compress-loop-responses.ts). Originals are captured in stream.ts applyRanges
right after applyCompression, by diffing block ids before/after and calling
collectBlockContent on the new blocks with full:true.
This commit is contained in:
@@ -96,10 +96,21 @@ function executeProxyTool(
|
||||
const block = ctx.core.decompress(blockId, ctx.session.state);
|
||||
if (!block) return `[Block ${blockId} not found]`;
|
||||
const full = args.full === true;
|
||||
const collected = collectBlockContent(ctx.session.state, block, ctx.messages, { full });
|
||||
ctx.session.state = deactivateBlock(ctx.session.state, [blockId]);
|
||||
const header = `[Restored block ${blockId} — ${collected.count} item(s)${full ? ", full" : ""}]`;
|
||||
const body = collected.text || block.summary;
|
||||
const cached = ctx.session.blockContents.get(blockId);
|
||||
let body: string;
|
||||
let count: number;
|
||||
if (cached) {
|
||||
body = cached.text;
|
||||
count = cached.count;
|
||||
} else {
|
||||
const collected = collectBlockContent(ctx.session.state, block, ctx.messages, { full });
|
||||
body = collected.text || block.summary;
|
||||
count = collected.count;
|
||||
}
|
||||
if (count > 0 || cached) {
|
||||
ctx.session.state = deactivateBlock(ctx.session.state, [blockId]);
|
||||
}
|
||||
const header = `[Restored block ${blockId} — ${count} item(s)${full ? ", full" : ""}]`;
|
||||
const safeBlockId = blockId.replace(/[^a-zA-Z0-9_-]/g, "-");
|
||||
const outPath = body.length > 10000 ? join(tmpdir(), `acp-decompress-${safeBlockId}-${Date.now()}.txt`) : null;
|
||||
if (outPath) {
|
||||
|
||||
+21
-4
@@ -53,10 +53,27 @@ function executeProxyTool(
|
||||
const block = ctx.core.decompress(blockId, ctx.session.state);
|
||||
if (!block) return `[Block ${blockId} not found]`;
|
||||
const full = args.full === true;
|
||||
const collected = collectBlockContent(ctx.session.state, block, ctx.messages, { full });
|
||||
ctx.session.state = deactivateBlock(ctx.session.state, [blockId]);
|
||||
const header = `[Restored block ${blockId} — ${collected.count} item(s)${full ? ", full" : ""}]`;
|
||||
const body = collected.text || block.summary;
|
||||
// Prefer the content cached at compress time (cross-round safe).
|
||||
// collectBlockContent only sees the post-fold ctx.messages, where
|
||||
// originals are already replaced by summaries — so it returns 0 items
|
||||
// for any block created in an earlier round.
|
||||
const cached = ctx.session.blockContents.get(blockId);
|
||||
let body: string;
|
||||
let count: number;
|
||||
if (cached) {
|
||||
body = cached.text;
|
||||
count = cached.count;
|
||||
} else {
|
||||
const collected = collectBlockContent(ctx.session.state, block, ctx.messages, { full });
|
||||
body = collected.text || block.summary;
|
||||
count = collected.count;
|
||||
}
|
||||
// Only deactivate when we actually recovered content. Deactivating on a
|
||||
// 0-count result would discard the block and make later retries harder.
|
||||
if (count > 0 || cached) {
|
||||
ctx.session.state = deactivateBlock(ctx.session.state, [blockId]);
|
||||
}
|
||||
const header = `[Restored block ${blockId} — ${count} item(s)${full ? ", full" : ""}]`;
|
||||
// Security: never honor args.toFile in proxy mode — it is an
|
||||
// untrusted path from the model/client and enables path traversal
|
||||
// (e.g. overwriting ~/.bashrc or project source). Always write to a
|
||||
|
||||
@@ -8,6 +8,11 @@ export type Session = {
|
||||
requests: number;
|
||||
condensedToolResults: number;
|
||||
tokensSaved: number;
|
||||
/** Original content of compressed blocks, captured at compress time when
|
||||
* the source messages are still present in the request. decompress reads
|
||||
* from here instead of scanning ctx.messages (which only holds the
|
||||
* post-compression / folded view and loses originals across rounds). */
|
||||
blockContents: Map<string, { text: string; count: number }>;
|
||||
};
|
||||
|
||||
// KNOWN LIMITATION: sessions live in process memory. A proxy restart (deploy /
|
||||
@@ -39,6 +44,7 @@ export function getSession(id: string): Session {
|
||||
requests: 0,
|
||||
condensedToolResults: 0,
|
||||
tokensSaved: 0,
|
||||
blockContents: new Map(),
|
||||
};
|
||||
sessions.set(id, session);
|
||||
return session;
|
||||
|
||||
+11
-1
@@ -1,4 +1,4 @@
|
||||
import type { CompressionCore, Config, CoreMessage, CompressionState } from "acp-kernel";
|
||||
import { collectBlockContent, type CompressionCore, type Config, type CoreMessage, type CompressionState } from "acp-kernel";
|
||||
import type { Session } from "./session.js";
|
||||
import { COMPRESS_TOOL_NAME, parseCompressInput } from "./compress-tool.js";
|
||||
import { normalizeSseLineEndings } from "./sse-util.js";
|
||||
@@ -164,7 +164,17 @@ export function applyRanges(ranges: ReturnType<typeof parseCompressInput>, ctx:
|
||||
state: ctx.session.state,
|
||||
config: ctx.config,
|
||||
});
|
||||
const beforeIds = new Set(ctx.session.state.blocks.map((b) => b.blockId));
|
||||
ctx.session.state = res.state;
|
||||
// Cache original content for newly-created blocks. At compress time the
|
||||
// source messages are still in ctx.messages (this round's view, before
|
||||
// the next processTurn folds them). Storing the text here lets decompress
|
||||
// work in later rounds where ctx.messages no longer carries the originals.
|
||||
for (const b of res.state.blocks) {
|
||||
if (beforeIds.has(b.blockId)) continue;
|
||||
const collected = collectBlockContent(res.state, b, ctx.messages, { full: true });
|
||||
if (collected.count > 0) ctx.session.blockContents.set(b.blockId, { text: collected.text, count: collected.count });
|
||||
}
|
||||
const r = res.result;
|
||||
const detail = ranges.map((rg) => `${rg.startRef}–${rg.endRef}`).join(", ");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user