Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag
### 压缩

- **压缩区间跨越受保护消息时在边界截断,不再挖洞(#3953)**:此前模型选的区间如果中途扫过受保护消息(最近 N 条/最近一条用户消息),受保护消息会被从覆盖名单里挖掉、两侧照折——其回复(在受保护提问之后的消息)被摘要盖住,而摘要锚点落在提问之前,模型看到"答案摘要排在自己的问题上方"。现在区间在首个受保护消息处截断:从该位置起(受保护消息与其后的回复)全部保持可见,因果序不再被打穿。受保护消息恰在区间尾部的常规擦边(咨询式保护区原本设计的形状)行为逐字节不变;截断会清空区间或会劈开工具调用/结果对的形状退回原挖洞语义(吸收保护的吸收对不受影响)。
- **compress-from-store:被逐出的会话对着统一存储的原始视图折叠,不再永久拒收(#3982,根治 #3918 Phase 4)**:超过常驻池预算(默认 512 MiB)的大会话每次模型请求后都被逐出,活内存视图永不存活,`compress` 工具因此永久 REFUSED——而统一存储(#3911)本就每次保存把完整原始客户端视图镜像进 messages 表。现在 applyRanges 与外部摘要链在"有效视图为空"时先从统一正史恢复视图(只读、只窥探——绝不隐式建库;任一行不可解析维持诚实拒收),折叠落在持久化 state 上,与"下一轮模型请求后重试"路径完全同构。活视图在场时存储从不被咨询(零行为变化);无存储/legacy 模式拒收回执逐字节不变。

### 存储

Expand Down
3 changes: 2 additions & 1 deletion src/external-summary-compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { collectBlockContent } from "acp-kernel";
import { buildCompressSystemPrompt, parseCompressInput, type ParsedRange } from "./compress-tool.js";
import { configuredSummaryPlan } from "./external-summary-runtime.js";
import type { ResolvedKernelConfig } from "./compress-settings.js";
import { applyRanges, noVisibleContextRefusal, normalizeRangeOrder, type RewriteCtx } from "./stream.js";
import { applyRanges, noVisibleContextRefusal, normalizeRangeOrder, recoverEvictedView, type RewriteCtx } from "./stream.js";
import { compressResult, type ProxyToolResult } from "./proxy-tool-result.js";
import { imagePlaceholders } from "./image-note.js";
import type { SummaryWork } from "./external-summary.js";
Expand Down Expand Up @@ -44,6 +44,7 @@ export async function applyConfiguredCompression(input: unknown, ctx: RewriteCtx
const parsed = parseCompressInput(withOptionalSummaries(input), callId);
if (parsed.ranges.length === 0) return applyRanges(parsed, ctx);
normalizeRangeOrder(parsed.ranges);
recoverEvictedView(ctx);
const noView = noVisibleContextRefusal(ctx, parsed.ranges);
if (noView) return noView;
const state = ctx.session.state;
Expand Down
27 changes: 27 additions & 0 deletions src/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { readdir, readFile, rm } from "node:fs/promises";
import * as path from "node:path";
const { join } = path;
import { StateStore, flatFileNameFor, type PersistedEnvelope, type StateStoreCodec } from "acp-kernel/persist";
import type { BiliMessage } from "acp-kernel/wire";
import { sessionsDir } from "./paths.js";
import {
persistEnabled as knobPersistEnabled,
Expand Down Expand Up @@ -3021,6 +3022,32 @@ function peekStoreForStamp(): SessionStore | null {
return _store;
}

/** #3982 compress-from-store: recover the raw client view for an evicted
* session from the unified transcript. Read-only and peek-only — the module
* singleton must already exist (never construct a store on a compress path);
* a disabled/unavailable sidecar, zero rows, or ANY unparsable row returns
* null so callers keep their honest refusal instead of folding against a
* half-readable view. Rows are the raw client view caliber (#2627 — what a
* live model request would have re-sent), persisted on every save via the
* raw-tail ingest. */
export function peekStoredTailView(sessionId: string): BiliMessage[] | null {
const store = peekStoreForStamp();
if (!store) return null;
const rows = store.unifiedTailViewRows(sessionId);
if (!rows || rows.length === 0) return null;
const messages: BiliMessage[] = [];
for (const row of rows) {
try {
const parsed = JSON.parse(row.content) as BiliMessage;
if (typeof parsed?.id !== "string" || typeof parsed?.role !== "string") return null;
messages.push(parsed);
} catch {
return null;
}
}
return messages;
}

export function getStore(): SessionStore {
if (!_store) {
_store = new SessionStore({ enabled: persistEnabled(), log: defaultLogger });
Expand Down
33 changes: 28 additions & 5 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { executeSearchContextTarget, resolveDecompress } from "./decompress-shar
import { ONE_CALL_HINT } from "acp-kernel";
import { adoptContentStore, contentStoreOf, ccrEnabled, drainPendingRetrievals, executeRetrieve, retrieveToolName } from "./store.js";
import { recordThreadTail } from "./server/thread-tracking.js";
import { peekStoredTailView } from "./persist.js";
import { imagePlaceholdersForSummary } from "./image-note.js";
import { containsBiliInternalText, containsMarkerLineText, containsRenderTagText, containsToolCallEmissionText, stripAcpTags } from "./loop/tag-echo-filter.js";
import { maxShrinkPerCompress } from "./fetch-util.js";
Expand Down Expand Up @@ -656,6 +657,22 @@ export function noVisibleContextRefusal(
return compressResult(receipt, "refused", 0, "gate:no-visible-context");
}

/** #3982: evicted-session view recovery — when the effective live view is
* empty, restore the raw client view from the unified transcript (persisted
* on every save, #3911) BEFORE the no-visible-context gate above. Peek-only
* (a store must already exist); any unparsable row keeps the honest refusal.
* A non-empty live view stays authoritative — the store is never consulted
* for it. Shared by the plain-compress lane (applyRanges) and the external-
* summary lane (applyConfiguredCompression). */
export function recoverEvictedView(ctx: RewriteCtx): void {
if ((ctx.compressMessages ?? ctx.messages).length > 0) return;
const recovered = peekStoredTailView(ctx.session.id);
if (recovered && recovered.length > 0) {
ctx.compressMessages = recovered;
ctx.log(`[acp-proxy: view recovered from the unified store — ${recovered.length} message(s); folding against the persisted raw view (#3982)]`);
}
}

function formatMiB(bytes: number | undefined): string {
if (bytes === undefined) return "?";
return `${(bytes / (1024 * 1024)).toFixed(1)} MiB`;
Expand Down Expand Up @@ -787,6 +804,7 @@ export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, ctx:
const ids = ctx.messages.slice(0, 10).map((m) => `${saltedMsgIdForLog(m.id)}(${(m.text ?? "").length}c)`).join(", ");
ctx.log(`[acp-proxy: first msg ids: ${ids}]`);
}
recoverEvictedView(ctx);
const noView = noVisibleContextRefusal(ctx, ranges, { loopTracking: trackLoop });
if (noView) return noView;
// #2749: reject summaries that are byte-identical to a display stub the
Expand Down Expand Up @@ -919,17 +937,21 @@ export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, ctx:
}
// 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.
// the next processTurn folds them) — or in ctx.compressMessages when an
// EMPTY-view fold recovered its source from the unified store (#3982);
// collecting against [] would leave those blocks uncached for later
// decompress/range-restore. Storing the text here lets decompress work
// in later rounds where neither carries the originals.
// Two views are cached so decompress can honor the `full` flag: `one`
// (direct messages + nested child summaries) and `full` (all originals).
// Leaf blocks have no active nested children, so both kernel paths
// emit byte-identical text — persist a single copy in that case
// (#401: the duplicate was 50% of blockContents bytes on disk).
const contentSource = ctx.messages.length > 0 ? ctx.messages : (ctx.compressMessages ?? ctx.messages);
for (const b of res.state.blocks) {
if (beforeIds.has(b.blockId)) continue;
const full = collectBlockContent(res.state, b, ctx.messages, { full: true });
const one = collectBlockContent(res.state, b, ctx.messages, { full: false });
const full = collectBlockContent(res.state, b, contentSource, { full: true });
const one = collectBlockContent(res.state, b, contentSource, { full: false });
if (full.count > 0 || one.count > 0) {
const sameView = one.text === full.text && one.count === full.count;
cacheBlockContent(ctx.session, b.blockId, {
Expand Down Expand Up @@ -987,7 +1009,8 @@ export function applyRanges(parsed: ReturnType<typeof parseCompressInput>, ctx:
// intact, all on one line for the client marker) so the model stops
// inspecting state and lets the original turn continue.
const minChars = ctx.config.compress.minCompressRange;
const totalChars = ctx.messages.reduce((n, m) => n + (m.text ?? "").length, 0);
const totalChars = (ctx.messages.length > 0 ? ctx.messages : (ctx.compressMessages ?? ctx.messages))
.reduce((n, m) => n + (m.text ?? "").length, 0);
const noViableAnywhere = minChars > 0 && totalChars < minChars
? ` This conversation holds only ${totalChars} char(s) — below the ${minChars}-char minimum, so NO range can succeed yet; do not retry compress or call acp_status/search_context about it — continue answering the user's task.`
: "";
Expand Down
158 changes: 158 additions & 0 deletions tests/compress-from-store.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// #3982 compress-from-store: an evicted session (no live in-memory view)
// must fold against the unified-store view instead of refusing forever
// (#3918 Phase 4). These tests pin:
// 1. view recovered from the store → compress proceeds, block lands, refs
// resolve — the fold is identical to the after-model-request path;
// 2. no store (legacy/hermetic) → the #3126 refusal stays byte-identical;
// 3. a corrupt row → refuse (never guess from a half-readable view);
// 4. a live view present → the store is never consulted (zero behavior
// change for the resident fast path).
// The recovery is read-only (peek, never creates a store) and only fires when
// the effective view is EMPTY — a partial live view stays authoritative.
import test from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { DatabaseSync } from "node:sqlite";
import type { CoreMessage } from "acp-kernel";
import { createCore, createInitialState, defaultConfig, assignRefs, emptyRefMap } from "acp-kernel";
import { parseCompressInput } from "../src/compress-tool.ts";
import { applyRanges, type RewriteCtx } from "../src/stream.ts";
import { SessionStore, _setStoreForTest } from "../src/persist.ts";

type Ctx = Omit<RewriteCtx, "log"> & { log: (m: string) => void; logs: string[] };

const MESSAGES = Array.from({ length: 80 }, (_, i) => ({
id: `raw${i}`,
role: (i % 2 ? "assistant" : "user") as "assistant" | "user",
text: "lorem ipsum dolor sit amet conseq ".repeat(8) + i,
})) as unknown as CoreMessage[];

const SPEC = () => parseCompressInput({ content: [{ startId: "m00010", endId: "m00040", summary: "y".repeat(100) }] });

function makeCtx(opts?: { id?: string; messages?: CoreMessage[]; refs?: boolean }): Ctx {
const logs: string[] = [];
const ctx: Ctx = {
core: createCore(),
config: defaultConfig(200000, { preserveRecentMessages: 5, preserveRecentTokens: 0 }),
messages: opts?.messages ?? [],
session: {
id: opts?.id ?? "sess-cfs",
meta: { protocol: "anthropic" },
stats: { requests: 0, tokensSaved: 0, inputTokens: 0, cachedTokens: 0, outputTokens: 0, cacheSamples: 0, lastInputTokens: 0, compressCreditTokens: 0, contextTokens: 0, retrieveCalls: 0, retrieveHits: 0, retrieveMisses: 0, storedBytes: 0, storeBytesSaved: 0, rangeRestores: 0 },
metadata: {},
state: createInitialState(),
createdAt: Date.now(),
lastSeen: Date.now(),
blockContents: new Map(),
inFlight: 0,
persisted: false,
pendingRetrievals: [],
},
log: (m: string) => { logs.push(m); },
logs,
};
if (opts?.refs) {
ctx.session.state.messageRefs = assignRefs(MESSAGES, { existing: emptyRefMap(), nextIndex: 0 }).map;
}
return ctx;
}

function makeStoredSession(id: string, lastMessages: CoreMessage[]) {
const ctx = makeCtx({ id });
return {
...ctx.session,
lastMessages: lastMessages as never,
lastMessagesFolded: false,
pluginSnapshot: undefined,
contentStore: undefined,
} as never;
}

test("compress-from-store: evicted view is recovered from the unified store and the fold lands (#3982)", async () => {
const dir = mkdtempSync(join(tmpdir(), "cfs-store-"));
process.env.BILI_PERSIST_ZSTD = "0";
const store = new SessionStore({ enabled: true, dir, debounceMs: 0 });
_setStoreForTest(store);
try {
await store.writeNow(makeStoredSession("sess-cfs", MESSAGES));
const ctx = makeCtx({ id: "sess-cfs", refs: true });
const res = applyRanges(SPEC(), ctx);
assert.equal(res.outcome, "applied", `expected a real fold, got: ${(res.text ?? "").slice(0, 200)}`);
assert.ok(ctx.session.state.blocks.length > 0, "block landed in state");
assert.ok(ctx.logs.some((l) => l.includes("view recovered from the unified store")), "recovery is logged");
const active = ctx.session.state.blocks.filter((b) => b.active);
assert.equal(active.length, 1);
assert.equal(active[0].effectiveMessageIds.length, 31, "m00010..m00040 coverage");
// #3982 review: a recovered fold must cache its block content like the
// live-view path does — collecting against the empty ctx.messages would
// leave later decompress/range-restore of this block empty.
const cached = ctx.session.blockContents.get(active[0].blockId);
assert.ok(cached && cached.full.count === 31, "recovered fold caches block content for later decompress");
} finally {
await store.closeQuietly();
rmSync(dir, { recursive: true, force: true });
_setStoreForTest(new SessionStore({ enabled: false }));
delete process.env.BILI_PERSIST_ZSTD;
}
});

test("compress-from-store: no store available → the #3126 refusal stays byte-identical", () => {
_setStoreForTest(new SessionStore({ enabled: false }));
const ctx = makeCtx({ id: "sess-nostore", refs: true });
const res = applyRanges(SPEC(), ctx);
assert.equal(res.outcome, "refused");
assert.match(res.reason ?? "", /no-visible-context/);
assert.ok(res.text.includes("no live message view"), "honest refusal text preserved");
assert.equal(ctx.session.state.blocks.length, 0, "nothing folded");
});

test("compress-from-store: a corrupt stored row refuses instead of guessing", async () => {
const dir = mkdtempSync(join(tmpdir(), "cfs-bad-"));
process.env.BILI_PERSIST_ZSTD = "0";
const store = new SessionStore({ enabled: true, dir, debounceMs: 0 });
_setStoreForTest(store);
try {
await store.writeNow(makeStoredSession("sess-bad", MESSAGES));
const db = new DatabaseSync(join(dir, "index.db"));
db.exec(`UPDATE content SET bytes = CAST('{"broken' AS BLOB) WHERE hash = (SELECT content_hash FROM messages WHERE session_id = 'sess-bad' AND seq = 5)`);
db.close();
const ctx = makeCtx({ id: "sess-bad", refs: true });
const res = applyRanges(SPEC(), ctx);
assert.equal(res.outcome, "refused", "corrupt row must not produce a half-view fold");
assert.match(res.reason ?? "", /no-visible-context/);
assert.equal(ctx.session.state.blocks.length, 0);
} finally {
await store.closeQuietly();
rmSync(dir, { recursive: true, force: true });
_setStoreForTest(new SessionStore({ enabled: false }));
delete process.env.BILI_PERSIST_ZSTD;
}
});

test("compress-from-store: a live view keeps the store out of the path", async () => {
const dir = mkdtempSync(join(tmpdir(), "cfs-live-"));
process.env.BILI_PERSIST_ZSTD = "0";
const store = new SessionStore({ enabled: true, dir, debounceMs: 0 });
_setStoreForTest(store);
try {
// Stored rows deliberately diverge from the live view: if the store
// were consulted despite the live view, the fold would cover the
// wrong ids.
const staleRows = MESSAGES.map((m, i) => ({ ...m, id: `stale${i}`, text: "stale " + m.text }));
await store.writeNow(makeStoredSession("sess-live", staleRows));
const ctx = makeCtx({ id: "sess-live", messages: MESSAGES, refs: true });
const res = applyRanges(SPEC(), ctx);
assert.equal(res.outcome, "applied", "live view folds normally");
assert.ok(!ctx.logs.some((l) => l.includes("view recovered from the unified store")), "no recovery when a live view exists");
const active = ctx.session.state.blocks.filter((b) => b.active);
assert.equal(active.length, 1);
assert.ok(active[0].effectiveMessageIds.includes("raw10"), "coverage follows the LIVE view ids");
} finally {
await store.closeQuietly();
rmSync(dir, { recursive: true, force: true });
_setStoreForTest(new SessionStore({ enabled: false }));
delete process.env.BILI_PERSIST_ZSTD;
}
});
Loading