From 92e9e5ac7dc3f9346c940d39c2b8890d81a65fb3 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 05:31:44 +0800 Subject: [PATCH 1/3] fix(session-resolve): keep the raw key when a tag change rides on a host-side history rewrite (#4778) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DSH NEXT's goal auto-continuation rewrites the prior per-turn system messages in its own log (persisted empties resented on every later request) while each turn also ships a fresh leading system prompt. The mid-history rewrite breaks BOTH tag-change migration rungs (head fragments, byte-exact chain), so the whole main conversation was re-keyed onto an empty |sub: lane — orphaning the root lane's fold state and forcing a multi-million-token preflight on every request until a restart minted yet another lane. Add a third tag-change rung: when the recorded body still sits verbatim inside the incoming (>= CONTINUATION_MIN_RUN consecutive same-index content-cell fragment agreements, harness-state cells excluded from both sequences before alignment) AND the incoming extends past the recorded, migrate instead of fork. Guardrails preserved by design: self-contained strangers under a reused id share no positional content (overlap gate), stale shorter replays cannot rewind the live skeleton (length gate), shallow aux blobs never qualify (mainShaped gate), scattered sub-threshold agreement still forks (threshold gate). Regression suite: tests/issue4778-dsh-goal-subfork.test.ts (unit state machine + e2e dsh openai lane; verified to fail pre-fix on exactly the goal rows). Targeted persona/session suites (9 files, 67 tests) + typecheck green locally. --- src/session-resolve.ts | 62 ++++- tests/issue4778-dsh-goal-subfork.test.ts | 298 +++++++++++++++++++++++ 2 files changed, 357 insertions(+), 3 deletions(-) create mode 100644 tests/issue4778-dsh-goal-subfork.test.ts diff --git a/src/session-resolve.ts b/src/session-resolve.ts index 5ec7e014d4..432bd4ccbe 100644 --- a/src/session-resolve.ts +++ b/src/session-resolve.ts @@ -127,6 +127,39 @@ function mainShaped(kinds: string[]): boolean { const skel8 = (kinds: string[]): string => createHash("sha256").update(kinds.join("\u0000")).digest("hex").slice(0, 8); +/** #4778: harness-state roles carry per-turn HOST state (date, budget, + * model banner — dsh NEXT stamps a DIFFERENT system prompt on every turn + * and rewrites prior ones in its own log on goal continuations) and zero + * conversation identifying power. */ +const HARNESS_ROLES = new Set(["system", "developer"]); + +/** #4778: longest run of CONSECUTIVE same-index content-fragment agreements + * between two skeletons, compared over CONVERSATION CELLS ONLY — the + * harness-state cells are excluded from both sequences BEFORE alignment, + * because the host inserts/removes/empties them around its own bookkeeping + * and must not shift the content-cell alignment. Exact per-cell sha256 + * comparison — no fuzzy similarity (MESSAGE-IDENTITY doctrine); a run of + * CONTINUATION_MIN_RUN agreements is cryptographically impossible between + * two distinct conversations. */ +function maxAlignedFragRun(kindsA: string[], fragsA: string[], kindsB: string[], fragsB: string[]): number { + const fa: string[] = []; + const fb: string[] = []; + for (let i = 0; i < fragsA.length; i++) if (!HARNESS_ROLES.has(kindsA[i])) fa.push(fragsA[i]); + for (let j = 0; j < fragsB.length; j++) if (!HARNESS_ROLES.has(kindsB[j])) fb.push(fragsB[j]); + const n = Math.min(fa.length, fb.length); + let best = 0; + let cur = 0; + for (let i = 0; i < n; i++) { + cur = fa[i] === fb[i] ? cur + 1 : 0; + if (cur > best) best = cur; + } + return best; +} + +/** #4778: minimum same-index agreement run proving that a tag-changed + * history is the SAME conversation with host-side edits, not a stranger. */ +const CONTINUATION_MIN_RUN = 8; + export type SessionOrderResolution = { key: string; action: "claim" | "continue" | "reanchor" | "derived-aux" | "derived-task" | "derived-persona"; @@ -172,9 +205,12 @@ function record(rawId: string, turns: NeutralTurn[], tagHash: string, model?: st * claims; the tag only guards the lanes order alone cannot tell apart: * a tag change on CONTINUING history (model switch / AGENTS.md edit — * kinds prefix-align AND head fragments match) migrates the raw key - * (#2241/#2250); a tag change with different content forks onto - * `|sub:` — byte-identical to the old ladder's fork - * keys, so persisted fork lanes keep their sessions across the upgrade. */ + * (#2241/#2250); a tag change whose recorded body still sits verbatim at + * its own indices inside the incoming (host-side rewrite of its own log — + * #4778) migrates too; only a tag change with NO positional continuation + * evidence forks onto `|sub:` — byte-identical to the + * old ladder's fork keys, so persisted fork lanes keep their sessions + * across the upgrade. */ export function resolveSessionOrder(rawId: string, history: unknown, personaTag?: string, declaredModel?: string): SessionOrderResolution { const list = Array.isArray(history) ? (history as unknown[]) : []; const turns = neutralTurns(list); @@ -259,6 +295,26 @@ export function resolveSessionOrder(rawId: string, history: unknown, personaTag? record(rawId, turns, tagHash, effectiveModel); return { key: rawId, action: "reanchor", reason: "persona text changed on a history that continues the raw key’s persisted chain — anchor migrated (post-restart persona race #2241/#2250)" }; } + if ( + mainShaped(kinds) && + kinds.length >= recorded.kinds.length && + maxAlignedFragRun(kinds, incomingFrags, recorded.kinds, recorded.frags) >= CONTINUATION_MIN_RUN + ) { + // #4778: the host rewrote PART of its own persisted history while + // the persona text changed (dsh goal auto-continuation clears the + // prior per-turn system messages; the cleared cells break BOTH the + // head-fragment rung above and the byte-exact chain rung) — but + // the recorded conversation still sits inside the incoming at its + // own indices. A long same-index content-fragment agreement run + // cannot occur between distinct conversations, so this is the + // SAME conversation with host-side edits → migrate, never fork. + // Both guards keep the #150/#1916 defenses intact: a self- + // contained stranger under a reused id shares no positional + // content (overlap ~0), and a STALE shorter replay must not + // rewind the live skeleton (the length gate). + record(rawId, turns, tagHash, effectiveModel); + return { key: rawId, action: "reanchor", reason: "persona text changed but the recorded history continues inside the incoming — anchor migrated (#4778)" }; + } const laneKey = `${rawId}|sub:${fp16(tag)}`; record(laneKey, turns, tagHash, model); return { key: laneKey, action: "derived-persona", reason: "persona text changed without continuing history — persona lane" }; diff --git a/tests/issue4778-dsh-goal-subfork.test.ts b/tests/issue4778-dsh-goal-subfork.test.ts new file mode 100644 index 0000000000..0d9712f085 --- /dev/null +++ b/tests/issue4778-dsh-goal-subfork.test.ts @@ -0,0 +1,298 @@ +// #4778: dsh goal auto-continuation forks the MAIN conversation onto an +// empty `|sub:` lane. DSH NEXT stamps a DIFFERENT system prompt on every +// turn, and on a goal continuation its log rewrite materializes the prior +// turns' system prompts as persisted EMPTY entries that ride along in every +// later resend. The mid-history rewrite breaks BOTH tag-change migration +// rungs (head fragments, byte-exact chain) while the persona tag keeps +// drifting, so the whole main conversation was re-keyed onto `|sub:` +// — orphaning the root lane's fold state and forcing a multi-million-token +// preflight on every request until a restart minted yet another lane. +// +// Fix (src/session-resolve.ts): a third tag-change rung — when the recorded +// body still sits verbatim inside the incoming (≥ CONTINUATION_MIN_RUN +// consecutive same-index content-cell fragment agreements, harness-state +// cells excluded from both sequences before alignment) AND the incoming +// extends past the recorded, the anchor migrates instead of forking. +// +// Gate discipline (#3617): the bug shape migrates, and EACH guardrail leg +// (overlap, length, mainShaped, threshold) keeps its designed fork alive. + +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { createHash } from "node:crypto"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { orderSkeletonOf, resetSessionOrderForTest, resolveSessionOrder, sessionOrderSkeletonOf } from "../src/session-resolve.ts"; +import { _resetPluginStateForTest } from "../src/plugin.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { getSession, peekSession, _resetSessionsForTest } from "../src/session.ts"; + +process.env.NODE_ENV = "test"; +process.env.BILI_PERSIST = "0"; + +// Hermetic state dirs (#7.2: no environmental luck) — a shared prefix- +// affinity snapshot from another run would noise the chain rung. +const testRoot = mkdtempSync(join(tmpdir(), "bili-4778-")); +test.after(() => rmSync(testRoot, { recursive: true, force: true })); +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = testRoot; + +type M = { + role: string; + content?: string | null; + tool_calls?: { id: string; type: string; function: { name: string; arguments: string } }[]; + tool_call_id?: string; +}; +const fp16 = (s: string): string => createHash("sha256").update(s, "utf8").digest("hex").slice(0, 16); +const forkKey = (id: string, tag: string): string => `${id}|sub:${fp16(tag)}`; + +/** Persona tag as handle.ts computes it on the openai lane: every non-empty + * system text joined "\n\n" (openaiSystemTextForPersona). */ +const tagOf = (history: M[]): string => + history.filter((m) => m.role === "system" && typeof m.content === "string" && m.content !== "").map((m) => m.content as string).join("\n\n"); + +/** One persisted coding turn: user → tool call → tool result → final + * answer (four projected cells, unambiguously main-shaped). The per-turn + * system entry is HOST bookkeeping — absent until the goal rewrite below. */ +const turn = (i: number, label: string): M[] => { + const callId = `call-${label}-${i}`; + return [ + { role: "user", content: `${label}-u${i}-` + "u".repeat(120) }, + { role: "assistant", content: null, tool_calls: [{ id: callId, type: "function", function: { name: "bash", arguments: JSON.stringify({ cmd: `${label}-cmd-${i}` }) } }] }, + { role: "tool", tool_call_id: callId, content: `${label}-out-${i}-` + "o".repeat(120) }, + { role: "assistant", content: `${label}-a${i}-` + "a".repeat(120) }, + ]; +}; + +/** One wire request: the CURRENT leading system prompt + the persisted + * transcript + the current turn's user prompt. */ +const request = (currentSys: string, tail: M[]): M[] => [ + { role: "system", content: currentSys }, + ...tail, +]; + +/** The goal rewrite: every prior turn gains its (now EMPTY) system entry. */ +const withClearedSystems = (perTurn: M[][]): M[] => + perTurn.flatMap((t) => [{ role: "system", content: "" }, ...t]); + +const plain = (n: number, label: string): M[] => + Array.from({ length: n }, (_, i): M => ({ role: i % 2 === 0 ? "user" : "assistant", content: `${label}-${i + 1}-` + "x".repeat(120) })); + +// --------------------------------------------------------------------------- +// Unit: the resolver state machine against the issue's exact topology. +// --------------------------------------------------------------------------- +test("#4778 regression: goal continuation (cleared mid-history systems + per-turn system drift) keeps the RAW key", () => { + resetSessionOrderForTest(); + const id = "sess-4778-goal"; + // Turns 0..4 settle on the raw key — per-turn system drift rides the + // pre-existing head-fragment rung (the session's lifetime steady state: + // append-only transcript, drifting leading system). + const done: M[][] = []; + let transcript: M[] = []; + for (let i = 0; i < 5; i++) { + const t = turn(i, "main"); + const req = request(`sys-prompt-v${i}`, [...transcript, t[0]]); + assert.equal(resolveSessionOrder(id, req, tagOf(req)).key, id, `turn ${i} lands on the raw key`); + done.push(t); + transcript = [...transcript, ...t]; + } + // THE GOAL TURN: the log rewrite materializes the prior turns' system + // prompts as persisted EMPTY entries (resent from here on) and appends + // the continuation's user message under yet another system prompt. + // Pre-fix this request returned `{ key: |sub:, action: + // "derived-persona" }`. + const goalUser: M = { role: "user", content: "continue the goal-" + "g".repeat(120) }; + const rewritten = withClearedSystems(done); + const goalReq = request("sys-prompt-goal", [...rewritten, goalUser]); + const goalRes = resolveSessionOrder(id, goalReq, tagOf(goalReq)); + assert.equal(goalRes.key, id, "goal turn keeps the RAW key"); + assert.equal(goalRes.action, "reanchor"); + assert.match(goalRes.reason ?? "", /#4778/); + // The skeleton re-anchored to the host's living truth (empty system + // entries included) so the next request resolves off the healed shape. + assert.deepEqual(sessionOrderSkeletonOf(id), orderSkeletonOf(goalReq)); + + // Steady state: the next turn appends under a fresh system prompt — + // raw key again even though the goal turn's own (empty) system entry + // shifted the raw index alignment. + const goalAsst: M = { role: "assistant", content: "goal-answer-" + "a".repeat(120) }; + const afterGoal: M[] = [...rewritten, { role: "system", content: "" }, goalUser, goalAsst, ...turn(5, "main")]; + const nextReq = request("sys-prompt-v5", afterGoal); + assert.equal(resolveSessionOrder(id, nextReq, tagOf(nextReq)).key, id, "post-goal turn keeps the raw key"); +}); + +test("#4778 control: a self-contained stranger under the same id still forks (overlap gate)", () => { + resetSessionOrderForTest(); + const id = "sess-4778-stranger"; + resolveSessionOrder(id, plain(8, "main"), "sys-A"); + const stranger = plain(10, "stranger"); + const res = resolveSessionOrder(id, stranger, "sys-B"); + assert.equal(res.action, "derived-persona"); + assert.equal(res.key, forkKey(id, "sys-B")); +}); + +test("#4778 control: a STALE shorter replay must not rewind the live skeleton (length gate)", () => { + resetSessionOrderForTest(); + const id = "sess-4778-stale"; + resolveSessionOrder(id, plain(12, "main"), "sys-A"); + // Verbatim first ten cells (agreement run 10 ≥ 8) but the server holds + // two NEWER turns — the replay is stale, not a continuation. + const res = resolveSessionOrder(id, plain(12, "main").slice(0, 10), "sys-B"); + assert.equal(res.action, "derived-persona"); + assert.equal(res.key, forkKey(id, "sys-B")); +}); + +test("#4778 control: a shallow review blob with a different tag still forks (mainShaped gate)", () => { + resetSessionOrderForTest(); + const id = "sess-4778-blob"; + resolveSessionOrder(id, plain(8, "main"), "sys-A"); + const blob: M[] = [{ role: "user", content: "Conversation transcript (flattened):\nuser: build it\nDecide: risky?" }]; + const res = resolveSessionOrder(id, blob, "review-policy"); + assert.equal(res.action, "derived-persona"); + assert.equal(res.key, forkKey(id, "review-policy")); +}); + +test("#4778 control: scattered agreement below the threshold stays a fork (threshold gate)", () => { + resetSessionOrderForTest(); + const id = "sess-4778-thresh"; + resolveSessionOrder(id, plain(8, "main"), "sys-A"); + // Three middle cells rewritten: agreement runs of 2 and 3, both below + // CONTINUATION_MIN_RUN — extended or not, this is not provably the + // same conversation. + const edited = plain(10, "main").map((m, i) => (i >= 2 && i <= 4 ? { ...m, content: `edited-${i}-` + "e".repeat(120) } : m)); + const res = resolveSessionOrder(id, edited, "sys-B"); + assert.equal(res.action, "derived-persona"); + assert.equal(res.key, forkKey(id, "sys-B")); +}); + +test("#4778: a full byte-exact resend under a drifted system migrates (interaction guard)", () => { + resetSessionOrderForTest(); + const id = "sess-4778-resend"; + resolveSessionOrder(id, plain(10, "main"), "sys-A"); + const res = resolveSessionOrder(id, plain(10, "main"), "sys-B"); + assert.equal(res.key, id); + assert.equal(res.action, "reanchor"); +}); + +// --------------------------------------------------------------------------- +// e2e: the issue's literal topology through the wired dsh openai lane. +// --------------------------------------------------------------------------- +type Rig = { proxyPort: number; upstreamPort: number; proxy: http.Server; upstream: http.Server }; + +async function startRig(): Promise { + const upstream = http.createServer((req, res) => { + let b = ""; + req.on("data", (c: Buffer) => (b += c.toString("utf8"))); + req.on("end", () => { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + id: "chatcmpl-1", + object: "chat.completion", + choices: [{ index: 0, message: { role: "assistant", content: "ok" }, finish_reason: "stop" }], + usage: { prompt_tokens: 10, completion_tokens: 3, total_tokens: 13 }, + })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetSessionOrderForTest(); + const dir = mkdtempSync(join(testRoot, "run-")); + for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: {} }, + modelContextLimit: 200_000, + kernelConfig: defaultConfig(200_000), + compress: { injectTool: true, injectNudge: false }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + compat: { roles: {} }, + streamErrorShape: "protocol", + passthroughSource: null, + autoRestartOnUpdate: false, + updateTag: "latest", + advisoryCheck: false, + releaseNotesCheck: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + return { proxyPort: (proxy.address() as { port: number }).port, upstreamPort, proxy, upstream }; +} + +async function closeRig(rig: Rig): Promise { + rig.proxy.close(); + rig.proxy.closeIdleConnections?.(); + await once(rig.proxy, "close"); + rig.upstream.close(); + rig.upstream.closeIdleConnections?.(); + await once(rig.upstream, "close"); +} + +test("e2e dsh openai lane #4778: goal continuation with cleared mid-history systems stays on the RAW session", async () => { + const rig = await startRig(); + const CONV = "dshc-4778"; + try { + const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/chat/completions`; + const headers: Record = { "content-type": "application/json", "x-bili-plugin": "dsh", "x-bili-plugin-conversation": CONV }; + const tools = [{ type: "function", function: { name: "bash", description: "run", parameters: { type: "object", properties: {} } } }]; + const post = (messages: M[]): Promise => + fetch(url, { method: "POST", headers, body: JSON.stringify({ model: "gpt-6-astra", max_tokens: 64_000, tools, messages }) }); + + // Four ordinary turns settle the raw session (append-only + // transcript, drifting leading system — same model as the unit block). + const done: M[][] = []; + let transcript: M[] = []; + for (let i = 0; i < 4; i++) { + const t = turn(i, "main"); + const r = await post(request(`e2e-sys-v${i}`, [...transcript, t[0]])); + assert.equal(r.status, 200); + await r.text(); + done.push(t); + transcript = [...transcript, ...t]; + } + assert.ok(getSession(CONV), "raw session exists"); + assert.equal(getSession(CONV).stats.requests, 4, "settle turns ride the raw session"); + const refsAfterSettle = Object.keys(getSession(CONV).state.messageRefs.byRaw).length; + + // THE GOAL TURN: the log rewrite materializes the prior turns' + // system prompts as persisted EMPTY entries, a new leading system, + // the continuation's user message appended. + const goalUser: M = { role: "user", content: "continue the goal-" + "g".repeat(120) }; + const rewritten = withClearedSystems(done); + const goalReq = request("e2e-sys-goal", [...rewritten, goalUser]); + const rg = await post(goalReq); + assert.equal(rg.status, 200); + await rg.text(); + assert.equal(getSession(CONV).stats.requests, 5, "goal turn rides the RAW session (pre-fix: fork + stuck preflight)"); + assert.equal(peekSession(forkKey(CONV, tagOf(goalReq))), undefined, "no `|sub:` session was minted for the main lane"); + assert.ok(Object.keys(getSession(CONV).state.messageRefs.byRaw).length > refsAfterSettle, "refs keep accumulating on the raw session"); + + // Steady state: the next turn appends under yet another system. + const afterGoal: M[] = [...rewritten, { role: "system", content: "" }, goalUser, { role: "assistant", content: "ok" }, ...turn(4, "main")]; + const rn = await post(request("e2e-sys-v6", afterGoal)); + assert.equal(rn.status, 200); + await rn.text(); + assert.equal(getSession(CONV).stats.requests, 6, "post-goal turn keeps riding the raw session"); + assert.equal(peekSession(forkKey(CONV, tagOf(request("e2e-sys-v6", afterGoal)))), undefined, "still no fork session"); + } finally { + await closeRig(rig); + } +}); From dfadc21b339bfa45c6ec03b2f13e723ce6bf0507 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 15:22:50 +0800 Subject: [PATCH 2/3] fix(ci): sync lockfile recorded versions with acp-kernel 0.0.165 (#4599) master window commits 293ae6542/de5e03659 bumped kernel/package.json to 0.0.165 without regenerating the lockfiles; lock-sync re-resolution then rewrites the stale recorded versions (root 0.0.163, kernel 0.0.164) and fails. Pure recorded-version sync via npm install --package-lock-only; no dependency or semantic change. --- kernel/package-lock.json | 4 ++-- package-lock.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/package-lock.json b/kernel/package-lock.json index da011d94e1..4c5e2d79e9 100644 --- a/kernel/package-lock.json +++ b/kernel/package-lock.json @@ -1,12 +1,12 @@ { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "acp-kernel", - "version": "0.0.164", + "version": "0.0.165", "license": "MIT", "devDependencies": { "@types/node": "^22.0.0", diff --git a/package-lock.json b/package-lock.json index 380212c96e..15a6e3f4e9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ }, "kernel": { "name": "acp-kernel", - "version": "0.0.163", + "version": "0.0.165", "dev": true, "license": "MIT", "devDependencies": { From f4e1b33265d2f117821696060a9ac934fa095024 Mon Sep 17 00:00:00 2001 From: ci-check Date: Mon, 26 Oct 2026 18:09:39 +0800 Subject: [PATCH 3/3] =?UTF-8?q?test:=20pin=20the=20cross-wire=20provider-s?= =?UTF-8?q?witch=20topology=20(anthropic=E2=86=92openai)=20on=20the=20#477?= =?UTF-8?q?8=20continuation=20rung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Second #4778 trigger reported in-thread: a mid-session provider switch re-keys the conversation onto an empty |sub: lane when the new wire resends per-turn system cells the old wire never carried in-array. Clean re-renders were already absorbed by the head-fragment rung (#2791 wire-neutral projection); the dsh-NEXT mid-history-systems shape needs the #4778 rung (red pre-fix, green post-fix). The reasoning-asymmetry row pins the honest boundary: thinking text rides the fragment on the wire that carries it, so a renderer that drops it still forks — making the identity hash think-blind is an owner decision, not a silent formula change. --- tests/issue4778-dsh-goal-subfork.test.ts | 125 +++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/tests/issue4778-dsh-goal-subfork.test.ts b/tests/issue4778-dsh-goal-subfork.test.ts index 0d9712f085..524e795da1 100644 --- a/tests/issue4778-dsh-goal-subfork.test.ts +++ b/tests/issue4778-dsh-goal-subfork.test.ts @@ -180,6 +180,131 @@ test("#4778: a full byte-exact resend under a drifted system migrates (interacti assert.equal(res.action, "reanchor"); }); +// --------------------------------------------------------------------------- +// Cross-wire provider switch (second #4778 trigger reported in-thread: a +// mid-session anthropic→openai provider switch re-keyed the conversation +// onto an empty `|sub:` lane). The two lanes feed the SAME resolver +// DIFFERENT WIRE SHAPES of the same logical transcript — the #2791 +// wire-neutral projection (src/affinity-neutral.ts) exists precisely so the +// shapes project to identical turns. Pinned at resolver level: +// T-A clean re-render on the new wire → head-fragment rung migrates +// T-B + per-turn system cells resent ONLY on the new wire +// (dsh NEXT persists them in its log) → the #4778 continuation +// rung migrates (red pre-fix) +// T-D + reasoning asymmetry (thinking rides the old wire only) → honest +// fork: the identity hash stays byte-exact per cell; making it +// think-blind is an owner decision (see #4778 checklist), not a +// silent formula change here. +// --------------------------------------------------------------------------- + +type Block = Record; +type AM = { role: string; content?: string | null | Block[] }; + +/** One logical turn rendered on the ANTHROPIC wire (tool_use / tool_result + * blocks). `thinking` models a reasoning model whose thinking blocks ride + * the anthropic render only. */ +const athTurn = (i: number, label: string, thinking: boolean): AM[] => { + const cid = `cu-${label}-${i}`; + return [ + { role: "user", content: `${label}-u${i}-` + "u".repeat(120) }, + { role: "assistant", content: [ + ...(thinking ? [{ type: "thinking", thinking: `${label}-think-${i}-` + "t".repeat(80) }] : []), + { type: "tool_use", id: cid, name: "bash", input: { cmd: `${label}-cmd-${i}` } }, + ] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: cid, content: `${label}-out-${i}-` + "o".repeat(120) }] }, + { role: "assistant", content: [ + ...(thinking ? [{ type: "thinking", thinking: `${label}-final-think-${i}-` + "t".repeat(80) }] : []), + { type: "text", text: `${label}-a${i}-` + "a".repeat(120) }, + ] }, + ]; +}; + +/** The SAME logical turn re-rendered on the OPENAI wire (no reasoning + * fields — the renderer-divergence shape documented in #2791). */ +const oaiTurn = (i: number, label: string): M[] => { + const cid = `cu-${label}-${i}`; + return [ + { role: "user", content: `${label}-u${i}-` + "u".repeat(120) }, + { role: "assistant", content: null, tool_calls: [{ id: cid, type: "function", function: { name: "bash", arguments: JSON.stringify({ cmd: `${label}-cmd-${i}` }) } }] }, + { role: "tool", tool_call_id: cid, content: `${label}-out-${i}-` + "o".repeat(120) }, + { role: "assistant", content: `${label}-a${i}-` + "a".repeat(120) }, + ]; +}; + +test("#4778 family: clean cross-wire provider switch (anthropic→openai) keeps the RAW key (head-fragment rung)", () => { + resetSessionOrderForTest(); + const id = "sess-4778-xwire-clean"; + // Anthropic lane: the system prompt rides the TOP-LEVEL field (passed as + // the tag argument, as handle.ts does with systemTextsForSplit), never + // the messages array — so the recorded skeleton is pure conversation. + let msgs: AM[] = []; + for (let i = 0; i < 4; i++) { + const t = athTurn(i, "main", false); + assert.equal(resolveSessionOrder(id, [...msgs, t[0]], `ath-sys-v${i}`).key, id, `settle ${i} lands on the raw key`); + msgs = [...msgs, ...t]; + } + // THE SWITCH: the whole transcript re-rendered on the openai wire under + // a new leading system + the continuation prompt. + const switched: M[] = [ + { role: "system", content: "oai-sys-switch" }, + ...[0, 1, 2, 3].flatMap((i) => oaiTurn(i, "main")), + { role: "user", content: "after the switch-" + "s".repeat(120) }, + ]; + const res = resolveSessionOrder(id, switched, tagOf(switched)); + assert.equal(res.key, id, "the switched request keeps the RAW key"); + assert.equal(res.action, "reanchor"); +}); + +test("#4778 family: cross-wire switch WITH per-turn system cells resent only on the new wire keeps the RAW key (continuation rung)", () => { + resetSessionOrderForTest(); + const id = "sess-4778-xwire-midsys"; + let msgs: AM[] = []; + for (let i = 0; i < 4; i++) { + const t = athTurn(i, "main", false); + assert.equal(resolveSessionOrder(id, [...msgs, t[0]], `ath-sys-v${i}`).key, id, `settle ${i} lands on the raw key`); + msgs = [...msgs, ...t]; + } + // dsh NEXT persists its per-turn system prompts in its OWN log and + // resents them as mid-history system messages on the new wire — cells + // the anthropic lane never carried in-array. Pre-fix this request + // returned `{ key: |sub:, action: "derived-persona" }`. + const switched: M[] = [ + { role: "system", content: "oai-sys-switch" }, + ...[0, 1, 2, 3].flatMap((i) => [{ role: "system", content: `persisted-sys-v${i}` }, ...oaiTurn(i, "main")] as M[]), + { role: "user", content: "after the switch-" + "s".repeat(120) }, + ]; + const res = resolveSessionOrder(id, switched, tagOf(switched)); + assert.equal(res.key, id, "the switched request keeps the RAW key despite the new-wire-only system cells"); + assert.equal(res.action, "reanchor"); + assert.match(res.reason ?? "", /#4778/); +}); + +test("#4778 family control: reasoning asymmetry across the switch still forks (documented boundary)", () => { + resetSessionOrderForTest(); + const id = "sess-4778-xwire-think"; + // The pre-switch model was a thinker: its thinking blocks ride the + // anthropic render and are ABSENT from the openai re-render (the + // #2791-documented renderer divergence). Assistant-cell fragments then + // disagree on every turn, so no same-index agreement run reaches + // CONTINUATION_MIN_RUN — the resolver must stay honest and fork rather + // than guess. (Making the identity hash think-blind is an owner + // decision registered on #4778; this row pins the current bar.) + let msgs: AM[] = []; + for (let i = 0; i < 4; i++) { + const t = athTurn(i, "main", true); + assert.equal(resolveSessionOrder(id, [...msgs, t[0]], `ath-sys-v${i}`).key, id, `settle ${i} lands on the raw key`); + msgs = [...msgs, ...t]; + } + const switched: M[] = [ + { role: "system", content: "oai-sys-switch" }, + ...[0, 1, 2, 3].flatMap((i) => [{ role: "system", content: `persisted-sys-v${i}` }, ...oaiTurn(i, "main")] as M[]), + { role: "user", content: "after the switch-" + "s".repeat(120) }, + ]; + const res = resolveSessionOrder(id, switched, tagOf(switched)); + assert.equal(res.action, "derived-persona"); + assert.equal(res.key, forkKey(id, tagOf(switched))); +}); + // --------------------------------------------------------------------------- // e2e: the issue's literal topology through the wired dsh openai lane. // ---------------------------------------------------------------------------