diff --git a/src/acp-status.ts b/src/acp-status.ts index 784aa6aaf3..a6e767d0d0 100644 --- a/src/acp-status.ts +++ b/src/acp-status.ts @@ -19,6 +19,7 @@ import { pckAblations } from "./pck-probe.js"; import { getUnrecognizedPathStats } from "./server/observability.js"; import { ccrEnabled, ccrLoopConfig, contentStoreOf } from "./store.js"; import { coveredRefSpan } from "./decompress-shared.js"; +import { forkAdoptionSkippedOf } from "./fork-adoption.js"; import { preCompactionArchiveOf, statusInputBaseline, type Session } from "./session.js"; import { compressBreakerDetail, compressLastFailureCause } from "./stream.js"; import { describeAdvisory, getAdvisoryState } from "./advisory.js"; @@ -180,6 +181,17 @@ export function handleAcpStatus(args: Record, ctx: AcpStatusCtx }); if (scope) return toolOk(base); const extra: string[] = []; + // #3925 → #3011 C9: a fork that started with zero inherited blocks used to + // be invisible here — "COMPRESSED BLOCKS — none" reads as "nothing folded + // yet", not "inheritance was skipped". Name the gap and its recovery path: + // the lineage link recorded at fork detection means a bili restart lets the + // startup sweep inherit the parent's blocks. Suppressed once any active + // block exists (the gap is closed by then, however it was closed). + const forkSkipped = forkAdoptionSkippedOf(ctx.session); + if (forkSkipped && !ctx.session.state.blocks.some((b) => b.active)) { + extra.push(""); + extra.push(`FORK ADOPTION SKIPPED — this session forked from ${forkSkipped.parentId} with ${forkSkipped.blocks} parent block(s) (~${Math.round(forkSkipped.tokens / 1000)}K tokens) NOT inherited because forkAdoption is disabled; the parent's already-folded history replays raw and will re-fold from scratch. Restarting bili heals this session — the recorded lineage link lets the startup sweep inherit those blocks (the sweep follows resumeInheritance, on by default); set "forkAdoption": true (or BILI_FORK_ADOPTION=1) so future forks adopt at their first request instead of waiting for a restart.`); + } // #2366: everything in the report above is LOCAL ESTIMATES (defaultCountTokens); // on CJK-heavy routes upstream billing runs 2–4× higher, so an agent judging // pressure from those figures misreads a filling window as healthy and folds diff --git a/src/fork-adoption.ts b/src/fork-adoption.ts index 3d15fffdf1..a89d421ab2 100644 --- a/src/fork-adoption.ts +++ b/src/fork-adoption.ts @@ -296,11 +296,52 @@ export function applyForkAdoption(session: Session, plan: ForkAdoptionPlan, pare markDirty(session); } +/** #3925 → #3011 C9: what acp_status renders while a fork runs with zero + * inherited blocks — the "starting fresh despite an adoptable parent" gap. */ +interface ForkAdoptionSkipped { + parentId: string; + blocks: number; + tokens: number; +} + +/** Typed read of the forkAdoptionSkipped metadata field (session.metadata is + * a free Record; validate the shape on the way out). */ +export function forkAdoptionSkippedOf(session: Session): ForkAdoptionSkipped | undefined { + const raw = session.metadata.forkAdoptionSkipped; + if (typeof raw !== "object" || raw === null) return undefined; + const r = raw as Record; + if (typeof r.parentId !== "string" || typeof r.blocks !== "number" || typeof r.tokens !== "number") return undefined; + return { parentId: r.parentId, blocks: r.blocks, tokens: r.tokens }; +} + +/** #2986/#3925: the lineage link must be durable BEFORE the next request — + * markDirty alone persists on the debounce cadence, and a non-graceful exit + * inside that window strands the child unlinked forever (the boot sweep and + * the read-time decompress/search fallback both key off this link). Fire- + * and-forget on purpose: this runs on the model-request hot path, so we must + * not stall the response (mirrors persistLineageLinkNow in server/handle.ts). */ +function persistForkLineageNow(session: Session, log: (level: string, msg: string) => void): void { + void getStore().writeNow(session).catch((err) => { + log("warn", `[fork-adoption] lineage durable write failed for ${session.id}: ${String(err)} (#2986)`); + }); +} + /** Server hook: on a fresh anonymous fork (via "new" + lineage "forked"), * find the parent (memory first, then disk), measure what is adoptable, and * seed the fresh session when enabled. Always logs the adoptable inventory — * the measurement ework asked for in #629 — so operators can size the win - * before turning adoption on. */ + * before turning adoption on. + * + * #3925 → #3011 C9: records the persistent derivedFromSessionId link at + * detection time whatever the adoption outcome — the same unconditional link + * #1333 writes for explicitly derived conversations. Without it a bail left + * NO trace: the boot sweep (reconcileDerivedLineage) only iterates sessions + * carrying derivedFromSessionId, and decompress/search_context's read-time + * parent fallback (derivedAncestorSessions) walks that same link — so an + * unlinked child is never healed across a restart and its search stays blind + * for life. The link copies nothing at link time; adoption still judges per + * block in raw-id set semantics, so a mis-guessed parent can at most be + * searched read-only, never adopted from. */ export function maybeAdoptForkBlocks(args: { session: Session; parentId: string; @@ -323,15 +364,29 @@ export function maybeAdoptForkBlocks(args: { return; } const plan = planForkAdoption(parent, new Set(incomingMsgs.map((m) => m.id))); + // Link once the parent is confirmed loadable (a dangling link to a + // vanished id would only churn the read-time walk for nothing). + const justLinked = session.metadata.derivedFromSessionId === undefined; + if (justLinked) { + session.metadata.derivedFrom = parentId; + session.metadata.derivedFromSessionId = parentId; + } if (plan.adoptedActive === 0) { - log("info", `[fork-adoption] fork of ${parentId}: no fully-present blocks to adopt (${plan.straddled} straddle the fork point); starting fresh (#629)`); + if (justLinked) persistForkLineageNow(session, log); + log("info", `[fork-adoption] fork of ${parentId}: no fully-present blocks to adopt (${plan.straddled} straddle the fork point); starting fresh; lineage linked so the startup sweep can inherit later folds (#629/#3925)`); return; } if (!enabled) { - log("info", `[fork-adoption] fork of ${parentId}: parent has ${plan.adoptedActive} adoptable block(s) covering ~${plan.adoptedTokens} tokens; forkAdoption disabled, starting fresh (set forkAdoption: true or BILI_FORK_ADOPTION=1)`); + session.metadata.forkAdoptionSkipped = { parentId, blocks: plan.adoptedActive, tokens: plan.adoptedTokens }; + persistForkLineageNow(session, log); + log("warn", `[fork-adoption] fork of ${parentId}: parent has ${plan.adoptedActive} adoptable block(s) covering ~${plan.adoptedTokens} tokens; forkAdoption disabled, starting fresh (set forkAdoption: true or BILI_FORK_ADOPTION=1). Lineage linked — a bili restart lets the startup sweep inherit those blocks, and acp_status shows the gap (#3925)`); return; } applyForkAdoption(session, plan, parent); + // #3351 discipline: close the derived-blocks gate ONLY on real block + // adoption — the boot-sweep retry door stays open otherwise. + session.metadata.derivedBlocksAdopted = true; + persistForkLineageNow(session, log); log("info", `[fork-adoption] session ${session.id} adopted ${plan.adoptedActive} block(s) (~${plan.adoptedTokens} tokens, refs up to ${plan.maxRef || "n/a"}) from parent ${parentId} across fork (#629)`); // #1341: carry the originals behind the refs the fork now advertises as // retrievable — covered messages of the adopted blocks ∪ refs cited by diff --git a/tests/acp-status.test.ts b/tests/acp-status.test.ts index 01ccac12ac..c35afcc90f 100644 --- a/tests/acp-status.test.ts +++ b/tests/acp-status.test.ts @@ -203,3 +203,25 @@ test("acp_status renders the ACTIVE SURFACE line from the session's pack stamp", assert.ok(line, "ACTIVE SURFACE line present once stamped"); assert.ok(line!.startsWith("ACTIVE SURFACE: pack=lean | host=billion-context "), `host identity rendered (got: ${line})`); }); + +test("#3925: acp_status names a skipped fork adoption and stays quiet once blocks exist", () => { + const ctx = makeCtx12(); + assert.ok(!handleAcpStatus({}, ctx).text.includes("FORK ADOPTION SKIPPED"), "no line without the marker (negative gate)"); + + ctx.session.metadata.forkAdoptionSkipped = { parentId: "pfa-parent-1", blocks: 7, tokens: 431_500 }; + const skipped = handleAcpStatus({}, ctx).text; + assert.ok(skipped.includes("FORK ADOPTION SKIPPED"), "line appears while zero active blocks"); + assert.ok(skipped.includes("pfa-parent-1"), "line names the parent session"); + assert.ok(skipped.includes("7 parent block(s)"), "line names the skipped inventory"); + assert.ok(skipped.includes("BILI_FORK_ADOPTION=1"), "line carries the recovery knob"); + + // Malformed marker must not render (shape validation on the way out). + ctx.session.metadata.forkAdoptionSkipped = { parentId: 42 }; + assert.ok(!handleAcpStatus({}, ctx).text.includes("FORK ADOPTION SKIPPED"), "malformed marker renders nothing"); + + // Once any active block exists the gap is closed — line goes away. + ctx.session.metadata.forkAdoptionSkipped = { parentId: "pfa-parent-1", blocks: 7, tokens: 431_500 }; + applyRanges(parseCompressInput(JSON.parse(COMPRESS_ARGS)), ctx); + assert.ok(ctx.session.state.blocks.some((b) => b.active), "fixture sanity: an active block now exists"); + assert.ok(!handleAcpStatus({}, ctx).text.includes("FORK ADOPTION SKIPPED"), "suppressed once blocks exist"); +}); diff --git a/tests/fork-adoption.test.ts b/tests/fork-adoption.test.ts index 97b897b67d..47630243fa 100644 --- a/tests/fork-adoption.test.ts +++ b/tests/fork-adoption.test.ts @@ -6,6 +6,7 @@ import { defaultConfig } from "acp-kernel"; import { startServer } from "../src/server.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { listSessions } from "../src/session.ts"; +import { forkAdoptionSkippedOf } from "../src/fork-adoption.ts"; import type { ProxyOptions } from "../src/config.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; @@ -183,7 +184,7 @@ function proxyOpts(relayUrl: string, forkAdoption: boolean): ProxyOptions { }; } -async function runForkScenario(forkAdoption: boolean): Promise<{ parentBlocks: number; parentBlockIds: string[]; childBlockIds: string[]; childBlocks: number; childContents: number; lineage: string | undefined }> { +async function runForkScenario(forkAdoption: boolean): Promise<{ parentId: string; parentBlocks: number; parentBlockIds: string[]; childBlockIds: string[]; childBlocks: number; childContents: number; lineage: string | undefined; derivedFromSessionId: string | undefined; derivedBlocksAdopted: boolean; forkAdoptionSkipped: ReturnType }> { const run = `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`; _setStoreForTest(new SessionStore({ enabled: false })); setRegistryForTest({}); @@ -243,6 +244,11 @@ async function runForkScenario(forkAdoption: boolean): Promise<{ parentBlocks: n childBlocks: child.state.blocks.filter((b) => b.active).length, childContents: child.state.blocks.filter((b) => child.blockContents.has(b.blockId)).length, lineage: (child.metadata.anonymousPrefixAffinity as { lineage?: { reason?: string } } | undefined)?.lineage?.reason, + // #3925 → #3011 C9: the persistent lineage link + adoption markers + // must be on the child regardless of the adoption outcome. + derivedFromSessionId: typeof child.metadata.derivedFromSessionId === "string" ? child.metadata.derivedFromSessionId : undefined, + derivedBlocksAdopted: child.metadata.derivedBlocksAdopted === true, + forkAdoptionSkipped: forkAdoptionSkippedOf(child), }; assert.equal(snapshot.lineage, "forked", "child lineage must record the fork"); @@ -268,7 +274,7 @@ async function runForkScenario(forkAdoption: boolean): Promise<{ parentBlocks: n assert.ok(followUp.length > 0, "fork follow-up must produce a reply"); assert.equal(parent.state.blocks.length, parentIdCount, "parent blocks must be untouched (copy-on-fork)"); - return { ...snapshot, parentBlockIds: [...snapshot.parentBlockIds], childBlockIds: [...snapshot.childBlockIds] }; + return { ...snapshot, parentId: parent.id, parentBlockIds: [...snapshot.parentBlockIds], childBlockIds: [...snapshot.childBlockIds] }; } finally { await close(proxy); await close(relay); @@ -281,6 +287,11 @@ test("anonymous fork adopts the parent's fully-present compression blocks (#629) assert.equal(result.childContents, result.childBlocks, "adopted blocks must carry their summaries"); const adopted = result.parentBlockIds.filter((id) => result.childBlockIds.includes(id)); assert.ok(adopted.length >= 1, `child must carry the parent's block id (parent=${JSON.stringify(result.parentBlockIds)} child=${JSON.stringify(result.childBlockIds)})`); + // #3925 → #3011 C9: the lineage link is recorded and the derived-blocks + // gate closes on real adoption; nothing is marked skipped. + assert.equal(result.derivedFromSessionId, result.parentId, "adopting child must link its parent persistently"); + assert.equal(result.derivedBlocksAdopted, true, "derived-blocks gate must close on real block adoption (#3351)"); + assert.equal(result.forkAdoptionSkipped, undefined, "no skipped marker when adoption succeeded"); }); test("forkAdoption disabled leaves the fork at zero compression state", async () => { @@ -288,4 +299,14 @@ test("forkAdoption disabled leaves the fork at zero compression state", async () assert.equal(result.lineage, "forked", "fork lineage is still recorded"); assert.equal(result.childBlocks, 0, "disabled adoption must not seed blocks"); assert.equal(result.childContents, 0, "disabled adoption must not seed summaries"); + // #3925 → #3011 C9: even when adoption bails, the persistent lineage link + // must be recorded — without it the startup sweep can never heal the child + // across a restart and decompress/search_context stay parent-blind for + // life. The skipped marker carries what was left on the table. + assert.equal(result.derivedFromSessionId, result.parentId, "bailed fork must still link its parent persistently"); + assert.equal(result.derivedBlocksAdopted, false, "bailed fork must NOT close the derived-blocks gate (boot-sweep door stays open)"); + assert.ok(result.forkAdoptionSkipped, "bailed fork must record the skipped-adoption marker for acp_status"); + assert.equal(result.forkAdoptionSkipped?.parentId, result.parentId, "skipped marker must name the parent"); + assert.ok((result.forkAdoptionSkipped?.blocks ?? 0) >= 1, "skipped marker must count the adoptable blocks that were left behind"); + assert.ok((result.forkAdoptionSkipped?.tokens ?? 0) > 0, "skipped marker must carry the token inventory"); });