From 0c2fb090fb6ca32922664bfea618a122d920aed6 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Mon, 19 Oct 2026 09:23:53 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20fork=20prefix=20gate=20is=20advisory?= =?UTF-8?q?=20=E2=80=94=20adopt=20drifted=20fork=20children=20with=20a=20c?= =?UTF-8?q?lassified=20warn=20(#3741)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ordered-prefix 409 was a permanent dead-end: the rejected first request never incremented stats.requests, so every retry re-hit the same gate with no clear or TTL path (field report: dsh fork child stuck after an in-place proxy upgrade re-keyed message ids, #3642 canonical re-key generation). Routing is the session-id binding (recordPluginSession -> resolveConversation); the byte-exact pre-gate added no correctness fold reconcile (#3641 order-over-hash) doesn't already enforce — it only produced unrecoverable 409s. Now: mismatched first request is adopted, logs one classified warn (#3743 reason taxonomy), publicForkPrefix stays false so the side-lane demotion heuristics keep their normal say. Cost: a divergent child's first request goes out unfolded — a one-time token spike, never corruption. Gate stays one-shot (stats.requests > 0 bypasses it). --- CHANGELOG.md | 2 + src/server/handle.ts | 34 +++++++++++----- tests/plugin-fork.test.ts | 86 +++++++++++++++------------------------ 3 files changed, 58 insertions(+), 64 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f129729362..e9a78860aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ### Changes +- **Fork 子会话不再因前缀漂移永久 409(#3741,owner 决策:门降 advisory)**:dsh/客户端 fork 出的子会话,其首个请求如果与继承的有序前缀不匹配(升级换代重排消息 id、宿主改写 body 等),过去直接 409 且被拒请求不计数——重试永远撞同一扇门,子会话确定性卡死、无任何自愈或清除路径。现在改为**照常收养**:session-id 绑定本来就是真正的唤醒路由,折叠覆盖差异由 reconcile(#3641 order-over-hash)兜底修复;漂移首请求付出一次性未折叠 token 峰,正确性无损。每次收养打一条带分类的 warn(reason 沿用 #3743 诊断:id-drift / hash-drift-under-same-id / prefix-longer-than-incoming 等),`publicForkPrefix` 在漂移时保持 false,side-lane 降级启发照常发言。测试:三种漂移形态全部 200+转发+父会话不动+恰一条 warn(one-shot:首请求后 stats.requests>0 绕过检查)。 + - **Automatic upgrades now stagger across a window instead of landing within minutes; a public policy file controls the window fleet-wide (#3619).** Every bili instance that discovers a new version draws ONE random moment inside a delay window (built-in default **15 min – 48 h**), persists it, and upgrades when it arrives — restarts never re-roll. Before this, a release hit every auto-updating install within ~15–30 minutes, leaving no time to stop a bad one mid-rollout (#3401/#3600/#3394 were all shipped-too-fast incidents). The window and the emergency controls live in `auto-update-policy.json` in this repo, fetched anonymously (plain GET + ETag, zero telemetry, fail-open — an unreachable or malformed file falls back to local defaults): `hold` freezes automatic upgrades fleet-wide, `blocked` skips listed versions, `pinned` sets an upgrade ceiling, and per-version `overrides` tune the window (`{ "range": ">=0.1.200", "maxDelay": "1h" }`). Delay fields take human-readable durations — `"15m"`, `"2d"`, `"1d12h"` (units s/m/h/d/w, combinable; plain numbers still mean raw ms); malformed values are ignored with a warning, never guessed. The file steers timing/skip/pin only — it can never control what gets installed (no URLs, no commands; target versions must exist in the npm packument). Exits: manual `bili update` bypasses `hold`/stagger (still respects `blocked`/`pinned`), `update.staggerMinDelayMs: 0` + `update.staggerMaxDelayMs: 0` (or the `BILI_UPDATE_STAGGER_*_MS` twins) restore legacy immediate upgrades on one machine, `ACP_AUTO_UPDATE=0` disables auto-updates entirely. The update-driven e2e suites pin stagger 0/0 + an unroutable policy URL — they test periodic-update mechanics, not rollout timing. - **openai wire: non-array content tolerance (kernel 0.0.133, #3658 follow-up)** — the #3642 per-part rewrite iterated `messages[].content` and threw `TypeError: m.content is not iterable` on clients that emit a lone content-part object instead of an array (#2824 corpus caught it). Non-array non-string content now degrades to the legacy empty-text core (byte-compatible ids and bytes with the pre-#3642 codec) instead of throwing. Also regenerates the storage-render chat goldens for the documented one-time canonical re-key (tool-pair ids), and inverts the retired reconcile-off id-churn control into an absorption canary: with reconcile OFF, tool_call id rewrites + argument re-serialization must not disturb fold coverage (canonical ids absorb them by construction). - **protocol-stable core identity: the same logical conversation now yields byte-identical core message ids in all four dialects (#3642).** Wire codecs previously baked dialect accidents into message identity: anthropic used `"[image]"` as image text while openai deliberately kept `""` ("switching the seed would move every pre-existing shape"), tool-call ids embedded the provider's `toolCallId` bytes, raw JSON tool arguments hashed verbatim (key order = new id), and responses' empty-text reasoning cells seeded from the provider's volatile `item.id`. Switch protocols mid-session (opencode #3640: `/chat/completions` → `/v1/messages`) and ~20-68% of ids churned, tripping the fold-destroyed misdiagnosis #3641 just class-fixed. Identity now derives from a canonical form shared by all four codecs: a new `kernel/src/wire/identity.ts` (canonicalToolArgs: JSON args parsed → recursive key sort → compact stringify, verbatim for non-JSON; canonicalOpaqueSeed: provider `item.id`/`status` stripped; image marker `[image]` everywhere), the `toolCallId` slot retired from the seed (cluster suffixes disambiguate same-shape calls, deleting one sibling is local damage), and granularity normalized to one core per content part (anthropic's existing shape — its lanes take zero granularity re-keying, though tool-call/tool-result cores still re-seed since the `toolCallId` slot is retired; openai/responses/google split runs, with accumulators re-merging on rebuild so the wire stays byte-stable). The kernel first-user pin now spans the opening message's whole core run, so a multimodal opening message keeps its image bytes through folds (#781/#2607). Cross-protocol golden: text/tool/bash conversation → identical id arrays in all four dialects; canonical key-sort makes `{"file","line"}` == `{"line","file"}`. Image cells are now stamped `imagePart: true` at parse time (never text-compared — tag injection rewrites markers mid-render, which previously made image cores render as text and lose the real bytes, #767 billing fallout). Upgrade cost is one re-key of stored ids for openai/responses/google lanes; the #3641 reconcile tiers (word-bag claims, k-th image family pairing, toolCallId channel, epoch gate) absorb it without re-folding. diff --git a/src/server/handle.ts b/src/server/handle.ts index 2402f1ca86..6d0f45f504 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -1334,13 +1334,27 @@ async function handleRequest( publicForkPrefix = true; return true; } - // #3741: the 409 used to be a total blind spot — classify the - // mismatch server-side and echo the detail back so the client - // log alone points at the cause (id drift vs flag drift vs shape). - loggerLog("warn", `[fork-prefix] ${sessionId}: first child request rejected — ${verdict.reason} (protocol=${protocol})`); - res.writeHead(409, { "content-type": "application/json" }); - res.end(JSON.stringify({ ok: false, code: "FORK_PREFIX_CONFLICT", error: "first child request does not match its inherited ordered prefix", detail: verdict.reason })); - return false; + // #3741 resolution (owner decision): the ordered-prefix gate is + // now ADVISORY, not blocking. The session-id binding above is + // what actually wakes the fork child (client conversation id → + // registered child session), and fold reconcile — #3641's + // order-over-hash doctrine — repairs coverage drift on resent + // history, so a byte-exact pre-gate adds no correctness the + // pipeline below doesn't already enforce. What it DID add was a + // permanent 409 dead-end: any drift the gate couldn't tolerate + // (in-place proxy upgrade re-keying message ids — #3741's field + // report, host body rewrite, legacy-hash adoption) stuck the + // child conversation forever, because the rejected first request + // never increments stats.requests, so every retry re-hit the same + // gate with no clear or TTL path. Adopt with a classified warn + // (reason taxonomy from the #3743 diagnostics) and let reconcile + // settle the inherited folds; a divergent child's first request + // goes out unfolded — a one-time token spike, never corruption. + // publicForkPrefix stays false on mismatch: the side-lane + // heuristics below keep their normal say instead of trusting an + // unverified inherited prefix to veto demotion. + loggerLog("warn", `[fork-prefix] ${sessionId}: inherited prefix mismatch — adopting anyway (${verdict.reason}; protocol=${protocol})`); + return true; }); if (!accepted) return; } finally { @@ -1907,9 +1921,9 @@ async function handleRequest( const dshTitleSidecar = pluginAgentHeader(req.headers) === "dsh" && requestAgent !== "main" && isDshTitleRequest(parsed); const sideIntent = isSideRequest(parsed, requestAgent) || dshTitleSidecar; // #388/#2157 follow-up: side requests must not touch kernel state under - // a public-fork receipt either. The receipt's first-request 409 - // discipline above has already accepted this request (inherited prefix - // matched, or the child has live traffic), and a side request reads + // a public-fork receipt either. The receipt's first-request check above + // has already accepted this request (inherited prefix matched, or live + // traffic, or an advisory adoption per #3741), and a side request reads // none of the kernel state fork adoption maintains — so the historical // !publicForkPrefix veto here only had the effect of running // full-history-replaying side calls (dsh title-gen under a forked diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index 081f6d4aa6..b3a4eae2e9 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -12,6 +12,7 @@ import { applyAbsorb, buildStoredPlaceholder, createContentStore, defaultConfig, import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { setLogCapture } from "../src/logger.ts"; import { _resetPluginStateForTest, forkParentRevisionLegacy, rememberPluginMessages, resolveConversation } from "../src/plugin.ts"; import { _resetSessionsForTest, getSession, markDirty } from "../src/session.ts"; import { _setForTest } from "../src/registry.ts"; @@ -527,72 +528,49 @@ test("HTTP exact fork materializes its inherited summary with equivalent SDK tex } finally { await h.close(); } }); -test("HTTP first fork request rejects a mismatched inherited prefix without forwarding or mutating state", { timeout: 60_000 }, async () => { +test("HTTP first fork request adopts a mismatched inherited prefix with a classified warn (#3741 advisory gate)", { timeout: 60_000 }, async () => { const h = await harness(); + const warns: string[] = []; + setLogCapture((level, msg) => { if (level === "warn") warns.push(msg); }); try { await compress(h); const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; assert.equal((await h.request("/__bili/plugin/fork", forkRequest(snapshot))).status, 201); const child = (await h.request("/__bili/plugin/snapshot?conversationId=child")).body; const requests = h.forwarded.length; - // #3741: every rejection class must carry a classified detail, not just a code. - for (const { messages, details } of [ - { messages: [{ ...h.messages[0], content: "changed semantic content" }, h.messages[1], { role: "user", content: "continue child" }], details: [/cell-mismatch@0/, /id-drift/] }, - { messages: [h.messages[1], h.messages[0], { role: "user", content: "reordered prefix" }], details: [/cell-mismatch@0/, /id-drift/] }, - { messages: h.messages.slice(0, 1), details: [/prefix-longer-than-incoming \(expected=2 incoming=1\)/] }, + // Three drift shapes that used to 409: semantic change under the prefix, + // reordering, and a too-short replay. All must now be ADOPTED: the + // session-id binding woke the fork child and fold reconcile settles the + // inherited folds — a divergent child's first request simply goes out + // unfolded (token spike, never corruption) instead of sticking the + // conversation with a permanent 409 dead-end. + for (const messages of [ + [{ ...h.messages[0], content: "changed semantic content" }, h.messages[1], { role: "user", content: "continue child" }], + [h.messages[1], h.messages[0], { role: "user", content: "reordered prefix" }], + h.messages.slice(0, 1), ]) { const response = await fetch(`${h.origin}/bili/${h.upstreamUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "ekko-agent", "x-bili-plugin-conversation": "child" }, body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages }) }); const body = await response.text(); - assert.equal(response.status, 409, body); - const parsed = JSON.parse(body); - assert.equal(parsed.code, "FORK_PREFIX_CONFLICT"); - for (const pattern of details) assert.match(String(parsed.detail), pattern, body); - assert.equal(h.forwarded.length, requests); - assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=child")).body.parentRevision, child.parentRevision); + assert.equal(response.status, 200, body); + assert.ok(h.forwarded.length > requests, "mismatched first request must be forwarded (adopted), not rejected"); + // the child's traffic must never touch the parent session assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=parent")).body.parentRevision, snapshot.parentRevision); } - // #3741: same-id hash drift (an adapter-attached flag flipped under unchanged bytes) - // must classify separately from id drift — that distinction is what triage keys on. - const storedCell = resolveConversation("child").session!.pluginSnapshot?.[0]; - assert(storedCell); - const savedThinkingTokens = storedCell.thinkingTokens; - storedCell.thinkingTokens = 7; - try { - const response = await fetch(`${h.origin}/bili/${h.upstreamUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "ekko-agent", "x-bili-plugin-conversation": "child" }, body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages: [...h.messages.slice(0, 2), { role: "user", content: "flag drift" }] }) }); - const body = await response.text(); - assert.equal(response.status, 409, body); - const parsed = JSON.parse(body); - assert.equal(parsed.code, "FORK_PREFIX_CONFLICT"); - assert.match(String(parsed.detail), /cell-mismatch@0/, body); - assert.match(String(parsed.detail), /hash-drift-under-same-id/, body); - assert.ok(!String(parsed.detail).includes("id-drift"), body); - } finally { - storedCell.thinkingTokens = savedThinkingTokens; - } - // #3741: a broken ref mapping with id+hash intact must classify as ref-broken - // ALONE — inventing a hash-drift label for two equal hashes would send triage - // after a phantom adapter-flag flip (pruned mappings are the real cause). - const childSession = resolveConversation("child").session!; - const brokenCell = childSession.pluginSnapshot?.[0]; - assert(brokenCell); - const savedRef = childSession.state.messageRefs.byRaw[brokenCell.id]; - delete childSession.state.messageRefs.byRaw[brokenCell.id]; - try { - const response = await fetch(`${h.origin}/bili/${h.upstreamUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "ekko-agent", "x-bili-plugin-conversation": "child" }, body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages: [...h.messages.slice(0, 2), { role: "user", content: "ref drift" }] }) }); - const body = await response.text(); - assert.equal(response.status, 409, body); - const parsed = JSON.parse(body); - assert.equal(parsed.code, "FORK_PREFIX_CONFLICT"); - assert.match(String(parsed.detail), /cell-mismatch@0/, body); - assert.match(String(parsed.detail), /ref-broken/, body); - assert.ok(!String(parsed.detail).includes("hash-drift-under-same-id"), body); - assert.ok(!String(parsed.detail).includes("id-drift"), body); - } finally { - if (savedRef !== undefined) childSession.state.messageRefs.byRaw[brokenCell.id] = savedRef; - } - await sendModel(h, "child", [...h.messages.slice(0, 2), { role: "user", content: "valid retry" }]); - assert(JSON.stringify(h.forwarded.at(-1)).includes(summary)); - } finally { await h.close(); } + // Advisory signal: the gate is one-shot (first request only — after it, + // stats.requests > 0 bypasses the check entirely), so exactly one + // classified warn is expected, from the first adopted mismatch; the + // 2nd/3rd requests ride the live-traffic path with no check at all. + const forkWarns = warns.filter((w) => w.includes("[fork-prefix]") && w.includes("adopting anyway")); + assert.equal(forkWarns.length, 1, JSON.stringify(warns.filter((w) => w.includes("fork-prefix")))); + assert.ok(/id-drift|hash-drift-under-same-id|prefix-longer-than-incoming/.test(forkWarns[0]!), forkWarns[0]); + assert.notEqual((await h.request("/__bili/plugin/snapshot?conversationId=child")).body.parentRevision, child.parentRevision); + // The child keeps working afterwards — no dead-end, no manual escape. + await sendModel(h, "child", [...h.messages.slice(0, 2), { role: "user", content: "valid follow-up" }]); + assert(JSON.stringify(h.forwarded.at(-1)).includes("changed semantic content") || JSON.stringify(h.forwarded.at(-1)).includes(summary)); + } finally { + setLogCapture(null); + await h.close(); + } }); test("HTTP persistence failure leaves no child and retry can succeed", { timeout: 60_000 }, async () => { From 3e9b4fab6052d3d1300cf34874c7f195b47635bc Mon Sep 17 00:00:00 2001 From: awork Date: Mon, 19 Oct 2026 09:42:04 +0800 Subject: [PATCH 2/4] docs: sync fork-adopt parity-contract comment with the advisory gate (#3757 review) The HASH PARITY CONTRACT still described the pre-#3757 behavior (409 FORK_PREFIX_CONFLICT, permanently poisoned child). A mismatched first request is now adopted with a classified warn; the real cost of hash drift is losing the verified-prefix fast path plus one unfolded first request. --- src/agent/fork-adopt.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/agent/fork-adopt.ts b/src/agent/fork-adopt.ts index 7d6146ca92..c4a0ba7428 100644 --- a/src/agent/fork-adopt.ts +++ b/src/agent/fork-adopt.ts @@ -22,10 +22,13 @@ import { ACP_NAME_ALT } from "../loop/tag-echo-filter.js"; * HASH PARITY CONTRACT: forkStableJson/forkIdentityHashOf/forkOrderHashOf * below are byte-for-byte replicas of src/plugin.ts stableJson/ * forkMessageIdentityHash/forkOrderHash. The server's publicForkInputCheck - * rejects the child's first request (409 FORK_PREFIX_CONFLICT) when the - * replayed prefix does not hash-match the receipt, so any drift here poisons - * the child conversation permanently. The parity is pinned by - * tests/dsh-fork-adopt.test.ts against the exported plugin.ts hash. + * compares the replayed prefix against the receipt on the child's first + * request: a mismatch no longer rejects — it adopts with a classified warn + * and fold reconcile settles the inherited folds (#3741) — but the child + * loses the verified-prefix fast path (publicForkPrefix stays false, so the + * side-lane heuristics keep their say) and pays one unfolded first request. + * The parity is pinned by tests/dsh-fork-adopt.test.ts against the exported + * plugin.ts hash. * * RESPONSES PIPELINE PARITY CONTRACT: the forkResponses* functions below are * byte-for-byte replicas of the server's responses pre-projection pipeline From fb2f6fc46b9f4194858d06b61cb7ff6c9824afd7 Mon Sep 17 00:00:00 2001 From: awork Date: Mon, 19 Oct 2026 09:42:04 +0800 Subject: [PATCH 3/4] test: pin #3743 classifier branches orphaned by the adoption inversion (#3757 review) Inverting the 409 sub-cases removed the only coverage of the hash-drift-under-same-id and ref-broken verdict branches (including #3743's fix that a pruned ref reports ref-broken alone, never hash-drift). Pin both directly against publicForkInputCheck with negative assertions so a future classifier regression cannot ship silently behind the advisory warn. --- tests/plugin-fork.test.ts | 45 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index b3a4eae2e9..43ed76de37 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -13,7 +13,7 @@ import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { setLogCapture } from "../src/logger.ts"; -import { _resetPluginStateForTest, forkParentRevisionLegacy, rememberPluginMessages, resolveConversation } from "../src/plugin.ts"; +import { _resetPluginStateForTest, forkParentRevisionLegacy, publicForkInputCheck, rememberPluginMessages, resolveConversation } from "../src/plugin.ts"; import { _resetSessionsForTest, getSession, markDirty } from "../src/session.ts"; import { _setForTest } from "../src/registry.ts"; import { recordToolWitness, resetToolRingForTest } from "../src/tool-ring.ts"; @@ -573,6 +573,49 @@ test("HTTP first fork request adopts a mismatched inherited prefix with a classi } }); +test("publicForkInputCheck keeps the #3743 taxonomy pinned per class (hash-drift vs ref-broken)", { timeout: 60_000 }, async () => { + // The advisory adoption above only pins id-drift/prefix-longer-than-incoming through the warn; + // these two branches lost their HTTP sub-cases when the 409 assertions were inverted, so pin + // them directly — including #3743's fix that a pruned ref (id+hash intact) reports ref-broken + // ALONE and must not be mislabeled hash-drift. + const h = await harness(); + try { + await compress(h); + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + assert.equal((await h.request("/__bili/plugin/fork", forkRequest(snapshot))).status, 201); + const child = resolveConversation("child").session!; + const cell = child.pluginSnapshot![0]; + const body = { model: "claude-test", max_tokens: 1024, stream: false, messages: [...h.messages.slice(0, 2), { role: "user", content: "continue child" }] }; + assert.deepEqual(publicForkInputCheck(child, "anthropic", body), { ok: true }, "intact snapshot verifies clean before any mutation"); + const savedThinkingTokens = cell.thinkingTokens; + cell.thinkingTokens = 7; + try { + const verdict = publicForkInputCheck(child, "anthropic", body); + assert.equal(verdict.ok, false); + if (!verdict.ok) { + assert.match(verdict.reason, /cell-mismatch@0 hash-drift-under-same-id/); + assert.doesNotMatch(verdict.reason, /id-drift|ref-broken/); + } + } finally { + cell.thinkingTokens = savedThinkingTokens; + } + const savedRef = child.state.messageRefs.byRaw[cell.id]; + assert.notEqual(savedRef, undefined); + delete child.state.messageRefs.byRaw[cell.id]; + try { + const verdict = publicForkInputCheck(child, "anthropic", body); + assert.equal(verdict.ok, false); + if (!verdict.ok) { + assert.match(verdict.reason, /cell-mismatch@0 ref-broken \(rawId=[^ ]+ ref=MISSING\)/); + assert.doesNotMatch(verdict.reason, /id-drift|hash-drift-under-same-id/); + } + } finally { + child.state.messageRefs.byRaw[cell.id] = savedRef; + } + assert.deepEqual(publicForkInputCheck(child, "anthropic", body), { ok: true }, "restored state verifies clean again"); + } finally { await h.close(); } +}); + test("HTTP persistence failure leaves no child and retry can succeed", { timeout: 60_000 }, async () => { const h = await harness(); try { From d1da179a592aa8086d2946ccf17c1f71d4092374 Mon Sep 17 00:00:00 2001 From: awork Date: Mon, 19 Oct 2026 09:42:04 +0800 Subject: [PATCH 4/4] test: cover an advisory-adopted side request on the #388 lane (#3757 review) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lane-guard pairing for the handle.ts lane-adjacent edit, with real coverage: a drift-mismatched first request that is a declared side call (dsh title-gen shape) must be adopted AND still diverted to the #388 passthrough without touching child kernel state — reachable only after the gate stopped rejecting. --- tests/fork-side-request-lane.test.ts | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/fork-side-request-lane.test.ts b/tests/fork-side-request-lane.test.ts index bbe95764fe..2afb2ffc67 100644 --- a/tests/fork-side-request-lane.test.ts +++ b/tests/fork-side-request-lane.test.ts @@ -126,3 +126,42 @@ test("fork-child title-gen rides the #388 passthrough and never pollutes the chi await h.close(); } }); + +test("an advisory-adopted first request that is a declared side request still rides the #388 passthrough (#3741)", async () => { + const h = await harness(); + try { + // 1. fork parent -> child at the full 3-message prefix + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body as { status: string; parentRevision: string; orderedMessages: unknown[] }; + assert.ok(snapshot.status !== "error"); + const orderedMessages = snapshot.orderedMessages.slice(0, 3); + const fork = await h.request("/__bili/plugin/fork", { protocolVersion: 1, parentConversationId: "parent", childConversationId: "forkchild", parentRevision: snapshot.parentRevision, branchPoint: { messageCount: 3, orderHash: hash(orderedMessages) }, orderedMessages, idempotencyKey: "fork-title-adopted" }); + assert.equal(fork.status, 201, JSON.stringify(fork.body)); + const revBefore = ((await h.request("/__bili/plugin/snapshot?conversationId=forkchild")).body as { parentRevision: string }).parentRevision; + + // 2. FIRST child request is a dsh title-gen shape (agent=title) whose replayed history + // DRIFTS from the inherited prefix. Pre-#3757 this 409'd forever; now the advisory gate + // adopts it — and declared side intent must STILL divert it to the #388 passthrough + // (publicForkPrefix stays false on mismatch, so the lane heuristics keep their say). + const driftedHistory = h.messages.map((m, i) => i === 0 ? { ...m, content: "drifted first message" } : m); + const titleMessages = [...driftedHistory, { role: "assistant", content: "child continuation" }, { role: "user", content: "Generate a title." }]; + const upstreamHitsBefore = h.forwarded.length; + await h.dshSend("forkchild", titleMessages, "title", "You generate short titles.", 1024); + assert.equal(h.forwarded.length, upstreamHitsBefore + 1, "adopted side request still reaches the upstream"); + + const session = resolveConversation("forkchild").session!; + assert.ok(session.metadata.publicForkReceipt !== undefined, "fork receipt recorded on the child session"); + assert.equal(session.stats.requests, 0, "a declared side request never counts as a fork-child main request, even when adopted"); + const revAfter = ((await h.request("/__bili/plugin/snapshot?conversationId=forkchild")).body as { parentRevision: string }).parentRevision; + assert.equal(revAfter, revBefore, "adopted title-gen must not grow the fork-child session snapshot"); + assert.ok(!JSON.stringify(session.pluginSnapshot).includes("Generate a title."), "title instruction never lands in the child's kernel session"); + const titleForward = h.forwarded.at(-1)! as { messages?: { content: unknown }[] }; + assert.ok(titleForward.messages?.some((m) => JSON.stringify(m.content).includes("Generate a title.")), "the forwarded title request carries the title instruction"); + + // 3. the child's first MAIN turn afterwards still runs the full pipeline normally: + // the adoption was one-shot advisory signal, not a lane reclassification. + await h.dshSend("forkchild", [...titleMessages, { role: "assistant", content: "A Title" }, { role: "user", content: "continue working" }], "main", "MAIN OPERATING SYSTEM", 256); + assert.equal(session.stats.requests, 1, "the next main turn counts as a fork-child main request"); + } finally { + await h.close(); + } +});