From aaafd26dcd0ee50467a8126d903daac2763e4d53 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Tue, 13 Oct 2026 01:38:50 +0800 Subject: [PATCH] fix: persona-lineage follow requires candidates to hold refs (#2948) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit livePersonaDescendant selected its target on freshness + traffic alone, so host-stamped management calls (compress/decompress/acp_status) were redirected onto ref-less aux lanes — dsh auto-review blobs fork onto their own |sub: lane and carry perpetual traffic without ever rendering the main conversation. Every redirected compress then died with 'every ref is unknown to this session' (highest ref=none) while auto-fold kept working on the parent: manual compression dead for the whole conversation, silently. personaDescendantsOf now filters candidates on non-empty state.messageRefs.byRef. A ref-holding sibling still wins over a fresher ref-less one (filter-then-pick), so the intended #2601 follow is preserved. New hermetic suite pins both directions and fails on the pre-fix code. --- src/plugin.ts | 27 ++- .../issue2948-aux-lane-lineage-guard.test.ts | 200 ++++++++++++++++++ 2 files changed, 222 insertions(+), 5 deletions(-) create mode 100644 tests/issue2948-aux-lane-lineage-guard.test.ts diff --git a/src/plugin.ts b/src/plugin.ts index 74e6c7038b..5d408194f2 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1065,16 +1065,30 @@ function resolveForkConversation(id: string): Session | undefined { * traffic for this long is not "live" anymore and the parent id routes home. */ const PERSONA_LINEAGE_FRESH_MS = 10 * 60 * 1000; +/** #2948: does the session hold usable refs? A `|sub:` descendant that never + * rendered a message holds none — the aux/side-lane shape: dsh auto-review + * blobs fork onto their own lane and carry perpetual traffic (stats.requests + * bumped, kernel state untouched / turns failing mid-prepare), so they look + * "live" under every freshness criterion yet can serve NOTHING a management + * call needs. Every compress/decompress/acp_status routed there fails + * "every ref is unknown to this session" (the #2948 breaker), so such a lane + * can never be a lineage-follow target. byRef is the sharp test: blocked + * entries live in byRaw only and are unusable anyway. */ +function holdsRefs(s: Session): boolean { + return Object.keys(s.state.messageRefs?.byRef ?? {}).length > 0; +} + /** #2601: persona-fork descendants of a raw host id — sessions minted by the * persona-anchor fork branch (`${rawId}|sub:${fp}`, persona-anchor.ts) that - * actually carried traffic and are fresh. Grouping by `id.split("|sub:")[0]` - * is the house idiom (splitSessionWarnings, #2170 canary). Sorted by - * lastSeen descending (listSessions order). */ + * actually carried traffic, are fresh, AND hold refs (#2948: freshness alone + * selected ref-less aux lanes — see holdsRefs). Grouping by + * `id.split("|sub:")[0]` is the house idiom (splitSessionWarnings, #2170 + * canary). Sorted by lastSeen descending (listSessions order). */ function personaDescendantsOf(baseId: string): Session[] { if (baseId.includes("|sub:")) return []; const prefix = `${baseId}|sub:`; const now = Date.now(); - return listSessions().filter((s) => s.id.startsWith(prefix) && (s.stats?.requests ?? 0) > 0 && now - (s.lastSeen ?? 0) <= PERSONA_LINEAGE_FRESH_MS); + return listSessions().filter((s) => s.id.startsWith(prefix) && (s.stats?.requests ?? 0) > 0 && now - (s.lastSeen ?? 0) <= PERSONA_LINEAGE_FRESH_MS && holdsRefs(s)); } /** #2601: the lineage's live session when `parent` (resolved from the host's @@ -1084,7 +1098,10 @@ function personaDescendantsOf(baseId: string): Session[] { * id; binding them to the frozen parent is the #2601 "every ref is unknown * to this session" breaker. Undefined when the parent is still the live * session (switch back to the raw persona self-heals: the parent then has - * the newest lastSeen). */ + * the newest lastSeen) or when no descendant holds refs (#2948: a ref-less + * aux lane is not a target — following it reproduces the same breaker it + * exists to cure; candidates are filtered in personaDescendantsOf, so a + * ref-holding sibling still wins over a fresher ref-less one). */ function livePersonaDescendant(parent: Session): Session | undefined { if (parent.id.includes("|sub:")) return undefined; return personaDescendantsOf(parent.id).find((s) => (s.lastSeen ?? 0) > (parent.lastSeen ?? 0)); diff --git a/tests/issue2948-aux-lane-lineage-guard.test.ts b/tests/issue2948-aux-lane-lineage-guard.test.ts new file mode 100644 index 0000000000..d9aaa97a82 --- /dev/null +++ b/tests/issue2948-aux-lane-lineage-guard.test.ts @@ -0,0 +1,200 @@ +// #2948: livePersonaDescendant picked its target on freshness + traffic alone, +// so a host-stamped management call (compress/decompress/acp_status) was +// redirected onto a `|sub:` descendant that carries PERPETUAL traffic but +// rendered nothing — the dsh auto-review aux lane (fixed system prompt + a +// fresh flattened blob before every tool call): stats.requests bumped every +// turn, kernel state untouched, ref registry permanently empty. Every +// redirected compress then died with "every ref is unknown to this session" +// (`highest ref=none`) — manual compression dead for the whole conversation +// while auto-fold kept working on the parent (silent failure). Production +// proof: same session, bili 0.1.192 → 3× outcome=applied; 0.1.195 (first with +// livePersonaDescendant) → 13× highest-ref=none, all behind the redirect log +// line naming the aux fork. +// Fix under test: personaDescendantsOf now requires candidates to HOLD REFS +// (non-empty state.messageRefs.byRef). Consequences pinned below: +// A. a ref-less fresh descendant is never followed (the incident shape); +// B. a REAL ref-holding fork is STILL followed even when a ref-less aux +// lane is fresher than it (filter-then-pick; #2601 preserved). +import test, { after } from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import path from "node:path"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { createHash } from "node:crypto"; + +// Hermetic state dirs BEFORE importing the server modules (same discipline as +// issue2601-persona-fork-lineage.test.ts). +const xdgRoot = mkdtempSync(path.join(tmpdir(), "bc-e2e-2948-aux-guard-")); +process.env.XDG_STATE_HOME = path.join(xdgRoot, "state"); +process.env.XDG_CACHE_HOME = path.join(xdgRoot, "cache"); +process.env.XDG_DATA_HOME = path.join(xdgRoot, "data"); +process.env.XDG_CONFIG_HOME = path.join(xdgRoot, "config"); + +// Imported AFTER the XDG env above. +const { defaultConfig } = await import("acp-kernel"); +const { startServer } = await import("../src/server.ts"); +const { SessionStore, _setStoreForTest } = await import("../src/persist.ts"); +const { _setForTest: setRegistryForTest } = await import("../src/registry.ts"); +const { resetToolRingForTest } = await import("../src/tool-ring.ts"); +const { _resetPluginStateForTest } = await import("../src/plugin.ts"); +const { forwardTool } = await import("../src/agent/shared.ts"); +const { getSession, peekSession, _resetSessionsForTest } = await import("../src/session.ts"); + +after(() => { + delete process.env.XDG_STATE_HOME; + delete process.env.XDG_CACHE_HOME; + delete process.env.XDG_DATA_HOME; + delete process.env.XDG_CONFIG_HOME; +}); + +process.env.NODE_ENV = "test"; +process.env.BILI_PERSIST = "0"; + +const SYSTEM_A = "You are DeepSeek Harness, the main coding agent.\nModel: GLM-A.\nWorkspace: /tmp."; +const SYSTEM_B = "You are DeepSeek Harness, the main coding agent.\nModel: GLM-B.\nWorkspace: /tmp."; +const forkKey = (conv: string, system: string): string => + `${conv}|sub:${createHash("sha256").update(system, "utf8").digest("hex").slice(0, 16)}`; +const long = (s: string): string => `${s}-` + "z".repeat(120); +const msgs = (n: number, tag: string): { role: string; content: string }[] => + Array.from({ length: n }, (_, i) => ({ role: i % 2 === 0 ? "user" : "assistant", content: long(`${tag}-${i + 1}`) })); + +type Rig = { baseUrl: string; chatUrl: string; proxy: http.Server; relay: http.Server }; + +function startRelay(): http.Server { + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: "r1", object: "chat.completion", + choices: [{ index: 0, message: { role: "assistant", content: "plain reply" }, finish_reason: "stop" }], + usage: { prompt_tokens: 100, completion_tokens: 5 }, + })); + }); + }); + server.listen(0, "127.0.0.1"); + return server; +} + +async function startRig(): Promise { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + resetToolRingForTest(); + _resetPluginStateForTest(); + _resetSessionsForTest(); + const relay = startRelay(); + await once(relay, "listening"); + const relayPort = (relay.address() as { port: number }).port; + const opts = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${relayPort}`]: { models: { "gpt-test": { context: 1_000_000 } }, compressProtocol: "marker" } }, + modelContextLimit: 1_000_000, + kernelConfig: defaultConfig(1_000_000, {}), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + compat: { roles: {} }, streamErrorShape: "protocol", passthroughSource: null, autoRestartOnUpdate: false, updateTag: "latest", advisoryCheck: false, releaseNotesCheck: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as unknown as Parameters[0]; + const proxy = await startServer(opts); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + return { + baseUrl: `http://127.0.0.1:${proxyPort}`, + chatUrl: `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${relayPort}/v1/chat/completions`, + proxy, + relay, + }; +} + +async function stopRig(rig: Rig): Promise { + rig.proxy.close(); + await once(rig.proxy, "close"); + rig.relay.close(); + await once(rig.relay, "close"); +} + +async function dshTurn(rig: Rig, system: string, history: { role: string; content: string }[], conv: string): Promise { + const res = await fetch(rig.chatUrl, { + method: "POST", + headers: { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": conv }, + body: JSON.stringify({ model: "gpt-test", max_tokens: 64_000, messages: [{ role: "system", content: system }, ...history] }), + }); + assert.equal(res.status, 200, "dsh turn must succeed"); + await res.json(); +} + +/** Seed a resident `|sub:` descendant exactly as the production aux lane + * presents itself: carried traffic, strictly newer lastSeen than everything + * else in the lineage — but zero refs (kernel state never touched). */ +function seedAuxLane(id: string, requests: number, lastSeen: number): void { + const s = getSession(id); + s.stats.requests = requests; + s.lastSeen = lastSeen; + assert.deepEqual(Object.keys(s.state.messageRefs.byRef), [], "seeded aux lane holds no refs"); +} + +test("#2948 e2e: a ref-less aux lane with perpetual traffic is never a lineage target", async () => { + const rig = await startRig(); + const CONV = "dshc-2948-a"; + const AUX = `${CONV}|sub:auxdeadbeef0000`; + try { + // Main conversation under the raw key — the parent holds refs. + await dshTurn(rig, SYSTEM_A, msgs(6, "main"), CONV); + const parent = peekSession(CONV); + assert.ok(parent, "parent session exists under the raw key"); + assert.ok(Object.keys(parent.state.messageRefs.byRef).length > 0, "the parent rendered messages and holds refs"); + + // The aux lane: fresher than the parent, heavy traffic, no refs. + seedAuxLane(AUX, 1574, Date.now() + 50); + + // Pre-fix, this candidate satisfied EVERY selection criterion + // (prefix match + requests > 0 + fresh + strictly newer) — the guard + // is what keeps it out, not any other filter. + const out = await forwardTool(rig.baseUrl, CONV, "acp_cache", {}, undefined, true); + assert.equal(out.failed, false, "the management call itself succeeds"); + assert.ok(!out.text.includes("|sub:"), "no redirect into the ref-less aux lane — served by the parent"); + assert.ok(!out.text.includes(AUX), "the aux lane's id never reaches the receipt"); + } finally { + await stopRig(rig); + } +}); + +test("#2948 e2e: a ref-holding fork still wins over a fresher ref-less aux lane (#2601 preserved)", async () => { + const rig = await startRig(); + const CONV = "dshc-2948-b"; + const FORK = forkKey(CONV, SYSTEM_B); + const AUX = `${CONV}|sub:auxdeadbeef0001`; + try { + // Legitimate persona fork: model switch with DISCONTINUOUS history + // (the #2601 shape) — the fork renders real turns and holds refs. + await dshTurn(rig, SYSTEM_A, msgs(6, "main"), CONV); + await dshTurn(rig, SYSTEM_B, msgs(3, "switched"), CONV); + const fork = peekSession(FORK); + assert.ok(fork, "the discontinuous switch forked onto |sub:"); + assert.ok(Object.keys(fork.state.messageRefs.byRef).length > 0, "the real fork holds refs"); + + // The production configuration: the aux lane is FRESHER than the real + // fork (its blob fires before every tool call of the live turn). + seedAuxLane(AUX, 4096, (fork.lastSeen ?? 0) + 50); + + // Filter-then-pick: the ref-holding sibling must win over the fresher + // ref-less one. Pre-fix this routed to AUX and every compress died + // with "every ref is unknown to this session". + const out = await forwardTool(rig.baseUrl, CONV, "acp_cache", {}, undefined, true); + assert.equal(out.failed, false, "the management call succeeds"); + assert.ok(out.text.includes(FORK), "the host-stamped call is served from the ref-holding fork"); + assert.ok(!out.text.includes(AUX), "the fresher ref-less aux lane is not selected"); + } finally { + await stopRig(rig); + } +});