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
37 changes: 31 additions & 6 deletions src/acp-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,35 @@ function formatDeadRefSpans(refs: string[]): string {
return spans.length > 8 ? `${spans.slice(0, 8).join(", ")} (+${spans.length - 8} more)` : spans.join(", ");
}

// #3105: one history rewrite can archive hundreds of blocks sharing one reason
// (#2695); an archived block can be neither compressed nor decompressed, so the
// default aggregates by reason (constant size) and per-block ids are opt-in +
// capped instead of listed verbatim (~20KB receipts before).
const PRE_COMPACT_ARCHIVE_DETAIL_CAP = 50;

function formatPreCompactionArchive(archive: Record<string, { at: number; reason: string }>, detail: boolean): string[] {
const ids = Object.keys(archive);
const lines: string[] = [
`PRE-COMPACTION ARCHIVE — ${ids.length} block(s): content left the session history (client native compaction or an unannounced history rewrite) and decompress is unavailable.`,
];
if (detail) {
const shown = ids.slice(0, PRE_COMPACT_ARCHIVE_DETAIL_CAP);
for (const id of shown) lines.push(` ${id} — ${archive[id]!.reason}`);
if (ids.length > shown.length) lines.push(` (+${ids.length - shown.length} more; showing first ${shown.length} of ${ids.length})`);
return lines;
}
const byReason = new Map<string, number>();
for (const id of ids) {
const reason = archive[id]!.reason;
byReason.set(reason, (byReason.get(reason) ?? 0) + 1);
}
for (const [reason, count] of [...byReason.entries()].sort((a, b) => b[1] - a[1])) {
lines.push(` ${count}: ${reason}`);
}
lines.push(` per-block ids + reasons: acp_status({ archiveDetail: true })`);
return lines;
}

export function handleAcpStatus(args: Record<string, unknown>, ctx: AcpStatusCtx): ProxyToolResult {
const scope = typeof args.scope === "string" ? (args.scope as "compressed" | "uncompressed") : undefined;
const view = typeof args.view === "string" ? (args.view as "ranges" | "messages") : undefined;
Expand Down Expand Up @@ -271,13 +300,9 @@ export function handleAcpStatus(args: Record<string, unknown>, ctx: AcpStatusCtx
extra.push("");
extra.push(`STORAGE — ${storageStatusLabel()}`);
const archive = preCompactionArchiveOf(ctx.session);
const archivedIds = Object.keys(archive);
if (archivedIds.length > 0) {
if (Object.keys(archive).length > 0) {
extra.push("");
extra.push(`PRE-COMPACTION ARCHIVE — ${archivedIds.length} block(s): content was replaced by the client's native compaction summary, so it is no longer in the session history and decompress is unavailable.`);
for (const id of archivedIds) {
extra.push(` ${id} — ${archive[id].reason}`);
}
extra.push(...formatPreCompactionArchive(archive, args.archiveDetail === true));
}
// #1206: conflict evidence (third-party compression plugin detected, or
// runtime signs another compressor rewrote history) — visible here so the
Expand Down
60 changes: 57 additions & 3 deletions tests/compaction-archive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,68 @@ test("compaction archive: decompress on an archived block gives an explicit reas
assert.match(out, /no longer reachable/);
});

test("compaction archive: acp_status surfaces the archive instead of listing it as active", () => {
test("compaction archive: acp_status surfaces the archive aggregated by default, per-block on demand (#3105)", () => {
const { session, blockId, tail } = sessionWithActiveBlock();
runShortenedTurn(session, tail, true);
const ctx: LoopCtx = { core: createCore(), config: defaultConfig(200000), messages: tail, session, log: (_msg: string) => {} };
const out = executeProxyTool("acp_status", {}, ctx).text;
assert.match(out, /PRE-COMPACTION ARCHIVE/);
assert.match(out, /PRE-COMPACTION ARCHIVE — 1 block\(s\)/);
assert.match(out, /decompress is unavailable/);
assert.ok(out.includes(blockId), "acp_status names the archived block");
assert.doesNotMatch(out, /^\s{2}b\S+ — /m, "default view aggregates by reason — no per-block lines (#3105)");
assert.match(out, /1: content replaced by client native compaction summary/);
assert.match(out, /acp_status\(\{ archiveDetail: true \}\)/, "default view points at the on-demand detail call");
const detail = executeProxyTool("acp_status", { archiveDetail: true }, ctx).text;
const perBlock = detail.match(/^\s{2}(b\S+) — .+$/gm) ?? [];
assert.ok(perBlock.some((line) => line.trim().startsWith(`${blockId} —`)), `detail mode names the archived block (${blockId}) with its reason`);
});

// #3105 fixtures: archives seeded directly on the metadata (the render path
// under test reads preCompactionArchiveOf, which trusts that record).
function sessionWithArchive(entries: Record<string, { at: number; reason: string }>): Session {
const session = makeSession();
session.metadata.preCompactionArchive = entries;
return session;
}

function archiveCtx(session: Session): LoopCtx {
return { core: createCore(), config: defaultConfig(200000), messages: [], session, log: (_msg: string) => {} };
}

test("compaction archive: hundreds of same-reason blocks aggregate to a constant-size section (#3105)", () => {
const reason = "covered substrate destroyed by unannounced client history rewrite (#2695)";
const entries: Record<string, { at: number; reason: string }> = {};
for (let i = 1; i <= 300; i++) entries[`b${i}`] = { at: i, reason };
const out = executeProxyTool("acp_status", {}, archiveCtx(sessionWithArchive(entries))).text;
assert.match(out, /PRE-COMPACTION ARCHIVE — 300 block\(s\)/);
assert.equal((out.match(/unannounced client history rewrite/g) ?? []).length, 1, "reason stated once, not once per block");
assert.match(out, /300: covered substrate destroyed by unannounced client history rewrite \(#2695\)/);
assert.doesNotMatch(out, /^\s{2}b\d+ — /m, "no per-block lines in the default view");
const section = out.split("PRE-COMPACTION ARCHIVE — ")[1]!.split("\n\n")[0]!;
assert.equal(section.split("\n").length, 3, `section is header + reason-count + hint only, constant size for 300 blocks (got ${section.length} chars)`);
});

test("compaction archive: distinct reasons keep separate accurate counts (#3105)", () => {
const entries: Record<string, { at: number; reason: string }> = {
b1: { at: 1, reason: "content replaced by client native compaction summary" },
b2: { at: 2, reason: "content replaced by client native compaction summary" },
b3: { at: 3, reason: "content replaced by client native compaction summary" },
b4: { at: 4, reason: "covered substrate destroyed by unannounced client history rewrite (#2695)" },
b5: { at: 5, reason: "covered substrate destroyed by unannounced client history rewrite (#2695)" },
};
const out = executeProxyTool("acp_status", {}, archiveCtx(sessionWithArchive(entries))).text;
assert.match(out, /PRE-COMPACTION ARCHIVE — 5 block\(s\)/);
assert.match(out, /3: content replaced by client native compaction summary/);
assert.match(out, /2: covered substrate destroyed by unannounced client history rewrite \(#2695\)/);
});

test("compaction archive: archiveDetail lists per-block ids capped at 50 with an omitted count (#3105)", () => {
const reason = "covered substrate destroyed by unannounced client history rewrite (#2695)";
const entries: Record<string, { at: number; reason: string }> = {};
for (let i = 1; i <= 60; i++) entries[`b${i}`] = { at: i, reason };
const detail = executeProxyTool("acp_status", { archiveDetail: true }, archiveCtx(sessionWithArchive(entries))).text;
assert.match(detail, /PRE-COMPACTION ARCHIVE — 60 block\(s\)/);
assert.equal(detail.match(/^\s{2}b\d+ — /gm)?.length, 50, "per-block listing capped at 50");
assert.match(detail, /\(\+10 more; showing first 50 of 60\)/, "omitted count made explicit");
});

test("zombie reap (#2695): decompress on a reaped block names the reap reason, not \"not found\"", () => {
Expand Down
14 changes: 14 additions & 0 deletions tests/marker-render-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ test("render contract: every report variant is consumed whole (SECTION_HEADS can
}
});

test("render contract: pre-compaction archive sections are consumed whole (#3105)", () => {
const { ctx } = makeCtx();
const archive: Record<string, { at: number; reason: string }> = {};
for (let i = 1; i <= 60; i++) archive[`b${i}`] = { at: i, reason: "covered substrate destroyed by unannounced client history rewrite (#2695)" };
ctx.session.metadata.preCompactionArchive = archive;
for (const args of [{}, { archiveDetail: true }] as Record<string, unknown>[]) {
const report = handleAcpStatus(args, ctx).text;
assert.match(report, /PRE-COMPACTION ARCHIVE/);
const standalone = consumeAcpStatusBody(echo(report));
assert.equal(standalone.bodies, 1, `${JSON.stringify(args)}: head + body recognized`);
assert.equal(standalone.text.trim(), "", `${JSON.stringify(args)}: whole report consumed (residual: ${JSON.stringify(standalone.text.slice(0, 160))})`);
}
});

test("render contract: consumer leaves non-report marker lines alone", () => {
// A bare head with no report grammar behind it (plain marker line) is
// owned by MARKER_LINE, not this consumer.
Expand Down
Loading