diff --git a/CHANGELOG.md b/CHANGELOG.md index 733f316865..f68725527e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,23 @@ All notable changes to **billion-context** are documented here. Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag. ## [Unreleased] + +### Changed +- session identity v2.1/v2.2 (`src/session-resolve.ts`): persona tags and the declared model now guard the order-based resolver wired across all four prepares (was: the six-stage persona-anchor ladder). Same-persona traffic owns the raw key through truncation, compaction rewrites, and diverged replays (old fp-equal semantics preserved byte-for-byte on keys); tag changes migrate on head continuity OR header-corroborated model switches (#2241/#2250/#2601/#2719/#2817); tag-less aux lanes ride the raw key only via the old non-anchoring path; strict `|sub:task` / `|sub:aux` guards protect virgin ids (#150/#1916/#970). Real-corpus replay census (495 sessions, 14,656 messages): 45 claims, 311 continues, 0 reanchors, 119 derived-aux, 0 derived-task — zero misattributions. +- Order-primary session identity v2: task-lane keys are discriminated by skeleton hash (`|sub:task:`) and the two order-ambiguous corners (shorter-with-aligned-tail, grown-with-rewritten-head) are guarded by the #2791 tail content fragments — a codex-style subagent replay that reuses the main session id can no longer merge into the main compression state (#150), while genuine head truncation keeps claiming raw (#2944, #2942 review). +- **Session identity: order-primary resolver v2 on the wire-neutral domain (#2944, parallel — not wired)** — the + order+id claim skeleton is now minted from `neutralTurns()` (src/affinity-neutral.ts, the #2791 projection that + #2939 already runs on) instead of a third raw-field classifier: content-blind by construction, all four wires + first-class (google `parts`, responses `function_call`), leading system run / client marker turns / image + placeholders stripped by the projection so host machinery cannot misalign the skeleton. Claim rules per the owner + rule: unambiguous client-minted id + main shape (≥3 turns incl. assistant) + head-or-tail order match → raw + (continue/reanchor); shallow blob → `|sub:aux` (#1916 race structurally dead); deep main-shaped stranger → + `|sub:task` guard rail; ids that fail the provenance table stay on the anonymous ladder. Implementation is + ALONGSIDE persona-anchor.ts (old ladder untouched, server not switched); the differential corpus + (tests/issue2944-neutral-skeleton.test.ts) pins the old ladder's behavior first, then runs the same scenarios + through the new resolver with old→new rows — plus the two proofs the raw-field draft could never pass: P6 the + same logical conversation on four wires yields one skeleton and one resolution sequence, P7 system add/remove + + client-marker injection leave the skeleton unchanged. - **Fold reconcile: marker-mint id churn must not read as substrate destruction (#2815, with #2789)** — covered ids are content-hash-minted, and any session folded before the raw-stage strip engine (or resent forever by a stateless client holding marker-laden bytes) carries covered ids minted from artifact-laden content. Once the marker engine diff --git a/src/server/handle.ts b/src/server/handle.ts index 271299d1aa..db7421a29e 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -34,7 +34,7 @@ import { codexCompactGatePre, codexCompactMode, hasCompactionTrigger, isCodexCli import { emitPreflightError } from "../stream-error.js"; import { agentProviderRecipes } from "../agent-providers.js"; import { affinityToken, conversationHeaderSource, claudeSubagentAgentId, claudeSubagentSplit, clientConversationHeader, codexTurnIdentity, dshPersonaFingerprintApplies, instructionsFingerprintApplies, openaiSystemTextForPersona, preferPromptCacheKeyIdentity } from "../session-id.js"; -import { personaNamespace } from "../persona-anchor.js"; +import { personaConversationOrder } from "../session-resolve.js"; import { prefixAffinity, type AnonymousAffinity } from "../prefix-affinity.js"; import { buildAnonymousProvenance, type AnonymousProvenance } from "../anon-provenance.js"; import { maybeAdoptForkBlocks, maybeAdoptResume } from "../fork-adoption.js"; @@ -816,21 +816,19 @@ async function handleRequest( // (model switch) MIGRATES the anchor instead of forking // the main lane off the raw key; only history-discontinuous // requests (review blobs) still fork onto `|sub:`. - ? personaNamespace( + ? personaConversationOrder( anthropicIdentity?.value ?? anthropicSignal, - personaSystemText, (parsed as AnthropicRequestBody).messages, - log, + personaSystemText, pluginReportedModel(req.headers), ) : anthropicIdentity?.value ?? anthropicSignal) : protocol === "openai" ? (dshPersona && !sideRequestLike - ? personaNamespace( + ? personaConversationOrder( openaiIdentity?.value ?? openaiSignal, - personaSystemText, (parsed as OpenAIRequestBody).messages, - log, + personaSystemText, pluginReportedModel(req.headers), ) : openaiIdentity?.value ?? openaiSignal) @@ -848,11 +846,10 @@ async function handleRequest( // discontinuous auto-review blob forks onto `|sub:`. // Must sit BEFORE the stableSystemAnchor branch below so a // plugin request never takes the plain-proxy verbatim anchor. - ? personaNamespace( + ? personaConversationOrder( responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader), - (parsed as ResponsesRequestBody).instructions ?? "", (parsed as ResponsesRequestBody).input, - log, + (parsed as ResponsesRequestBody).instructions ?? "", pluginReportedModel(req.headers), ) : opts.stableSystemAnchor && pluginAgentHeader(req.headers) === undefined @@ -885,13 +882,12 @@ async function handleRequest( // the anchor instead of forking the main lane off // its compression state; a genuinely fresh task // reusing the id (#150) still forks `|sub:`. - personaNamespace( + personaConversationOrder( responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader), - (parsed as ResponsesRequestBody).instructions ?? "", (parsed as ResponsesRequestBody).input, - log, - pluginReportedModel(req.headers), - ) + (parsed as ResponsesRequestBody).instructions ?? "", + pluginReportedModel(req.headers), + ) : (responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader))) : (responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader)); // #1916/#1307: true when the dsh persona fingerprint actually split diff --git a/src/session-resolve.ts b/src/session-resolve.ts new file mode 100644 index 0000000000..b59ce97ba9 --- /dev/null +++ b/src/session-resolve.ts @@ -0,0 +1,296 @@ +// #2944 v2: order-primary session resolution — on the WIRE-NEUTRAL domain. +// +// The general rule that retires the persona-fp ladder (owner rule, +// 2026-10-12): +// +// 1. 顺序 + sessionid(客户端自生成、无歧义)即可认领同一个会话, +// 绝对没有错误 —— 内容一个字不看。 +// 2. 压缩锚点默认顺序位置,hash 只是参考(下层 #2939 order-centric +// reconcile 同向)。 +// 3. hash 用于防止压缩导致的漂移、subagent 复用主会话带来的漂移 +// —— 防错认护栏,不参与认领决策。 +// 4. 不打膏药:一条通用规则,协议无关,不逐客户端特判。 +// +// v2 substrate (the delta vs the first draft, see #2944 review): the +// skeleton is minted from `neutralTurns()` (src/affinity-neutral.ts, the +// #2791 wire-neutral projection that #2939 already runs on) — NOT from a +// new raw-field classifier. One source of protocol-shape knowledge, all +// four wires for free: +// +// skeletonOf(messages) = neutralTurns(messages).map(t => t.kind) +// +// - content-blind by construction (kind ∈ user|assistant|tool|raw|role) +// - google `parts` and responses `function_call` items are first-class +// - the LEADING system/developer run is stripped (host harness state +// churn cannot misalign the skeleton — #1148 discipline) +// - client synthetic marker turns ("[model changed: …]") are dropped +// - image-capability placeholders (#2636) are dropped +// - consecutive assistant messages merge, multi-block tool_results +// split — so renderers that disagree on message packing agree on turns +// +// Guard rail (rule 3) — where ORDER alone is ambiguous. Two branches look +// order-identical for different truths, and content-fragment hashes (the +// projection's per-turn `frags`, content-derived but never consulted for +// CLAIMING) disambiguate without reading a byte of prose: +// +// shorter-than-recorded + tail aligns: +// genuine head truncation (dsh native compaction) keeps the old tail +// MESSAGES verbatim → tail frags equal → raw + reanchor. +// a codex-style subagent replay (#150: reuses the main +// body.session_id, self-contained, alternating tail aligns easily) +// carries DIFFERENT tail content → frags differ → derived, never raw. +// grown + head diverged + tail aligns (head rewritten by a compaction +// summary): same frag check — a summary head with the old tail passes; +// a coincidentally tail-aligning stranger fails. +// +// Derived-task lanes are DISCRIMINATED by skeleton hash +// (`|sub:task:`): different stranger shapes never share one +// compression session (two subagents under one main id stay separate), +// while the same stranger replaying across passes keeps a stable key. +// +// id provenance (接线时生效 — the caller contract; ids that fail it stay +// on the anonymous ladder, prefix-affinity, untouched): +// +// lane qualifies (client-minted, unambiguous) falls back +// anthropic header (plugin conv / session header) first-user content fp +// openai header first-user content fp +// responses header | body.session_id | metadata.session_id +// previous_response_id (upstream-minted), content fp +// google header content fp +// +// (derived from conversationIdentityResponses' `clientProvided` axis — +// kernel/src/wire/responses.ts — and the per-wire twins; a +// content-fingerprint id is representation-derived and never qualifies. +// NOTE for the wiring PR: the codex instructions-namespace +// (SubagentNamespaces) and the anthropic #970 subagent split run +// UPSTREAM of this resolver and stay — this module is the conversation +// axis only.) +// +// This module is the NEW implementation, implemented ALONGSIDE +// src/persona-anchor.ts (the old ladder) — the old file is NOT deleted and +// the server is NOT switched over yet (see #2944 rollout). Both +// implementations are pinned by the same input/output corpus in +// tests/issue2944-neutral-skeleton.test.ts: behavior-compatible rows share +// one expectation; designed divergences are marked old→new per row. + +import { createHash } from "node:crypto"; +import { neutralTurns, type NeutralTurn } from "./affinity-neutral.js"; + +const TAIL_WINDOW = 8; + +/** Order skeleton: the per-turn KIND sequence of the wire-neutral + * projection. Content-blind by construction — only role/tool shape + * participates. */ +export function orderSkeletonOf(messages: unknown): string[] { + const list = Array.isArray(messages) ? (messages as unknown[]) : []; + return neutralTurns(list).map((t) => t.kind); +} + +/** Per-turn content-fragment fingerprints (rule-3 guard rail — never + * consulted for claiming, only to disambiguate order-ambiguous corners). + * #2791 projects the same logical content to the same fragments on every + * wire, so the guard is cross-wire stable. */ +function fragsOf(turns: NeutralTurn[]): string[] { + return turns.map((t) => (t.frags ?? [t.kind]).join("\u0001")); +} + +const fp16 = (s: string): string => createHash("sha256").update(s, "utf8").digest("hex").slice(0, 16); + +function tailAligns(kinds: string[], recorded: string[]): boolean { + const window = Math.min(TAIL_WINDOW, kinds.length, recorded.length); + if (window === 0) return false; + for (let i = 1; i <= window; i++) { + if (kinds[kinds.length - i] !== recorded[recorded.length - i]) return false; + } + return true; +} + +/** A main conversation has assistant turns and at least a few cells; an + * aux persona (dsh auto-review: one flattened user blob) has neither. + * This is the ONLY aux/persona discriminator — no system text, no content + * hash, no per-client special case. */ +function mainShaped(kinds: string[]): boolean { + if (kinds.length < 3) return false; + return kinds.some((k) => k === "assistant"); +} + +const skel8 = (kinds: string[]): string => + createHash("sha256").update(kinds.join("\u0000")).digest("hex").slice(0, 8); + +export type SessionOrderResolution = { + key: string; + action: "claim" | "continue" | "reanchor" | "derived-aux" | "derived-task" | "derived-persona"; + /** Human-readable reason for the derived/reanchor branches (log surface). */ + reason?: string; +}; + +type Recorded = { kinds: string[]; frags: string[]; tag: string; model?: string }; + +/** In-memory order skeletons, same lifetime as the old anchors map + * (process memory; not persisted — a restart re-claims on first sight). */ +const skeletons = new Map(); + +function record(rawId: string, turns: NeutralTurn[], tag: string, model?: string): void { + skeletons.set(rawId, { kinds: turns.map((t) => t.kind), frags: fragsOf(turns), tag, model }); +} + +/** #2944 order-primary resolution. `rawId` MUST be a client-minted, + * unambiguous conversation id (caller's provenance contract — see the + * table above; ids that fail it stay on the anonymous ladder). + * + * `personaTag` (optional, rule-3 guard): the persona-defining text the + * host stamps on this lane — dsh system text, codex instructions. Order + * 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. */ +export function resolveSessionOrder(rawId: string, history: unknown, personaTag?: string, declaredModel?: string): SessionOrderResolution { + const list = Array.isArray(history) ? (history as unknown[]) : []; + const turns = neutralTurns(list); + const kinds = turns.map((t) => t.kind); + const incomingFrags = fragsOf(turns); + const tag = personaTag !== undefined && personaTag.trim().length > 0 ? personaTag : ""; + const model = declaredModel !== undefined && declaredModel.trim().length > 0 ? declaredModel.trim() : undefined; + const recorded = skeletons.get(rawId); + if (recorded === undefined) { + // First sight under the id. A main-shaped history claims the raw + // key; a shallow history claims ONLY when it carries a persona tag + // (the codex turn-1 shape: one user message + instructions — a real + // main turn, indistinguishable from a review blob without the tag); + // a tag-less shallow blob is an aux persona and NEVER claims — the + // #1916 review-steals-raw race dies there without reading prose. + if (!mainShaped(kinds) && tag === "") { + return { key: `${rawId}|sub:aux`, action: "derived-aux", reason: "shallow history with no assistant turns" }; + } + record(rawId, turns, tag, model); + return { key: rawId, action: "claim" }; + } + const fragTailEquals = (n: number): boolean => { + const w = Math.min(TAIL_WINDOW, n); + const a = incomingFrags.slice(-w); + const b = recorded.frags.slice(-w); + return a.length === b.length && a.every((f, i) => f === b[i]); + }; + const tagChanged = tag !== "" && tag !== recorded.tag; + // An EMPTY incoming tag (system-less / instructions-less request) is + // the old ladder's non-anchoring path: it rides whatever the id holds + // and never counts as a persona change. + const effectiveTag = tag === "" ? recorded.tag : tag; + const effectiveModel = model ?? recorded.model; + if (tagChanged) { + // Persona guard (owner rule 3). Same id, different persona text: + // keep raw ONLY with order AND content evidence — the kinds must + // prefix-align the recorded skeleton AND the head fragments must + // match (the host replayed the SAME conversation under a new + // system/instructions: model switch / AGENTS.md edit). Anything + // else is a different persona under the shared id (#150 fresh + // task, #1916 auto-review): its own `|sub:` lane, + // stable across successive calls of the same persona. + const headN = Math.min(kinds.length, recorded.kinds.length); + const headAligns = headN > 0 && recorded.kinds.every((k, i) => kinds[i] === k); + const headFragsEqual = headN > 0 && incomingFrags.slice(0, headN).every((f, i) => f === recorded.frags[i]); + if (mainShaped(kinds) && headAligns && headFragsEqual) { + record(rawId, turns, tag, effectiveModel); + return { key: rawId, action: "reanchor", reason: "persona text changed on continuing history — anchor migrated (#2241/#2250)" }; + } + if ( + mainShaped(kinds) && model !== undefined && recorded.model !== undefined && model !== recorded.model + ) { + // #2601/#2817: the persona text changed in the same window as a + // host-declared model switch (x-bili-plugin-model names a + // DIFFERENT model than the anchored claim) — the two host + // declarations corroborate, so the switch absorbs (migrate) even + // across a compacted/rewritten history whose fragments cannot + // prove continuation. Without the header change this exact shape + // still forks (the control case). + record(rawId, turns, tag, effectiveModel); + return { key: rawId, action: "reanchor", reason: "header-corroborated model switch — anchor migrated (#2601/#2817)" }; + } + const laneKey = `${rawId}|sub:${fp16(tag)}`; + record(laneKey, turns, tag, model); + return { key: laneKey, action: "derived-persona", reason: "persona text changed without continuing history — persona lane" }; + } + if (!mainShaped(kinds)) { + // Shallow traffic under the claimed id rides the raw key — with a + // persona tag it is the old ladder's fp-equal behavior (the persona + // owns the id); tag-less it is the old non-anchoring path (a + // system-less auxiliary call rides whatever the id holds). The + // #1916 review-steals-raw protection lives one branch up: real + // aux personas (dsh review, title) carry a DIFFERENT tag and fork + // onto their own lane before reaching here; the strict |sub:aux + // branch protects only VIRGIN ids (nothing to steal once claimed). + record(rawId, turns, effectiveTag, effectiveModel); + return { key: rawId, action: "continue" }; + } + if (kinds.length >= recorded.kinds.length) { + if (recorded.kinds.every((k, i) => kinds[i] === k)) { + record(rawId, turns, effectiveTag, effectiveModel); + return { key: rawId, action: "continue" }; + } + if (tailAligns(kinds, recorded.kinds) && fragTailEquals(Math.min(kinds.length, recorded.kinds.length))) { + // Grown (or equal), prefix diverged, tail aligned AND the tail + // fragments match: the host rewrote the head (native compaction + // summary / re-anchored event log) while keeping the living + // tail verbatim. Trust the id, re-anchor the skeleton. + record(rawId, turns, effectiveTag, effectiveModel); + return { key: rawId, action: "reanchor", reason: "order tail aligned and tail fragments match, head rewritten" }; + } + if (tag !== "") { + // Same persona, diverged order: the persona owns the id (owner + // rule 1 — id+persona 认领), so the skeleton follows the living + // end. The strict task-lane guard below is for tag-less and + // cross-persona callers, where id trust has no persona + // corroboration (#150 subagent replay). + record(rawId, turns, effectiveTag, effectiveModel); + return { key: rawId, action: "reanchor", reason: "same persona id, diverged order — skeleton follows the living end" }; + } + // Deep main-shaped history sharing nothing with the recorded + // skeleton on an unambiguous id: the guard rail (owner rule 3) — + // derived, never the raw key, until the skeleton is re-established. + // The skeleton-hash discriminator keeps distinct strangers (two + // subagents under one main id) on distinct compression sessions. + return { key: `${rawId}|sub:task:${skel8(kinds)}`, action: "derived-task", reason: "no order+fragment alignment with the recorded skeleton" }; + } + // Shorter than recorded: either head truncation (dsh native compaction + // keeps the old tail messages verbatim → fragments equal → raw + + // re-anchor, the living end follows the host's truth) or an id-reuse + // replay (#150 codex subagent: self-contained, tail aligns by + // alternation) whose tail content is DIFFERENT → derived-task. + if (tailAligns(kinds, recorded.kinds) && fragTailEquals(kinds.length)) { + record(rawId, turns, effectiveTag, effectiveModel); + return { key: rawId, action: "reanchor", reason: "truncated head, tail aligned and fragment-verified" }; + } + if (tag !== "") { + // Same persona, shorter, not a verbatim suffix: persona owns the + // id (old ladder's fp-equal rescue shape — a shrunken replay under + // the anchor system keeps the raw key). + record(rawId, turns, effectiveTag, effectiveModel); + return { key: rawId, action: "reanchor", reason: "same persona id, shorter replay — skeleton follows the living end" }; + } + return { + key: `${rawId}|sub:task:${skel8(kinds)}`, + action: "derived-task", + reason: "shorter history with a divergent tail — id-reuse guard (subagent replay?)", + }; +} + +/** Diagnostic/test surface: the recorded skeleton for an id. */ +export function sessionOrderSkeletonOf(rawId: string): string[] | undefined { + return skeletons.get(rawId)?.kinds; +} + +export function resetSessionOrderForTest(): void { + skeletons.clear(); +} + +/** handle.ts wiring adapter (replaces personaNamespace at the dsh / + * codex-instructions / claude-responses persona sites): same contract — + * raw id + the request's history in, the conversation key out. The old + * ladder consumed (systemText, model) and keyed by system-hash; the v2 + * resolver is content-blind and keys by order+id with fragment guards, + * so those parameters are simply gone. */ +export function personaConversationOrder(rawId: string, history: unknown, personaTag?: string, declaredModel?: string): string { + return resolveSessionOrder(rawId, history, personaTag ?? "", declaredModel).key; +} diff --git a/tests/issue2944-neutral-skeleton.test.ts b/tests/issue2944-neutral-skeleton.test.ts new file mode 100644 index 0000000000..2571664293 --- /dev/null +++ b/tests/issue2944-neutral-skeleton.test.ts @@ -0,0 +1,387 @@ +// #2944: session-resolve v2 — order+id claim on the WIRE-NEUTRAL domain. +// +// The SAME scenario corpus runs through BOTH implementations: +// OLD: src/persona-anchor.ts (the fp ladder — behavior pinned FIRST as +// the 瞄点; these assertions must pass against the current file +// untouched — stage 1 of #2942 neutralizeToolPrompts is NOT on this +// branch and is intentionally not pinned here) +// NEW: src/session-resolve.ts (order+id claim; skeleton = neutralTurns +// kinds — content-blind by construction, all four wires covered, +// leading system run / client markers / image placeholders stripped +// by the #2791 projection) +// Rows where the two agree share one expectation; designed divergences are +// marked old→new with the #2944 principle that justifies them. +// +// Special proofs (特殊代码证明) required by the owner: +// P1 50% mid-conversation content churn, same order + same length → +// SAME session (old: raw via migrate/fp-equal; new: continue — the +// skeleton never saw the churn). +// P2 shallow review blob under a claimed id → NEVER claims raw +// (old: fork |sub:; new: derived-aux |sub:aux — #1916 race dead). +// P3 head truncation (native compaction) under same id → raw + +// re-anchor (old: raw via fp-equal rescue; new: reanchor). +// P4 growth → continue raw (both). +// P5 deep main-shaped history sharing nothing with the recorded +// skeleton → derived, not raw (guard rail). +// P6 (new, v2-only) cross-wire: the SAME logical conversation rendered on +// all four wires yields the IDENTICAL skeleton and the identical +// resolution sequence — v1's raw-field kind strings could never do +// this (block-type strings differ per wire). +// P7 (new, v2-only) leading-system add/remove + client-marker turn +// injection leave the skeleton unchanged (host machinery, not +// conversation shape). + +import test from "node:test"; +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +// Hermetic dirs BEFORE imports (same idiom as the issue2601 suite). +const XDG = mkdtempSync(join(tmpdir(), "issue2944v2-")); +process.env.XDG_DATA_HOME = join(XDG, "data"); +process.env.XDG_STATE_HOME = join(XDG, "state"); +process.env.BILI_PERSIST = "0"; +process.env.NODE_ENV = "test"; + +const { prefixAffinity } = await import("../src/prefix-affinity.ts"); +const { resetPersonaAnchorsForTest, resolvePersonaKey } = await import("../src/persona-anchor.ts"); +const { resolveSessionOrder, resetSessionOrderForTest, sessionOrderSkeletonOf, orderSkeletonOf } = await import("../src/session-resolve.ts"); +import type { SessionOrderResolution } from "../src/session-resolve.ts"; + +const sha = (s: string): string => createHash("sha256").update(s).digest("hex").slice(0, 16); +const skel8 = (kinds: string[]): string => createHash("sha256").update(kinds.join("\u0000")).digest("hex").slice(0, 8); +const skel16 = (s: string): string => createHash("sha256").update(s, "utf8").digest("hex").slice(0, 16); + +type Msg = { role: string; content: string }; + +/** Alternating user/assistant turns, ~120 chars each. */ +function msgs(n: number, tag = "m"): Msg[] { + const out: Msg[] = []; + for (let i = 0; i < n; i++) { + const role = i % 2 === 0 ? "user" : "assistant"; + out.push({ role, content: `${tag} #${i} — ${tag} message body with enough prose to be a realistic cell ${"x".repeat(80)}` }); + } + return out; +} + +/** Rewrites the content of every even-indexed message (50% churn) while + * keeping roles, order and length identical. */ +function churn50(list: Msg[]): Msg[] { + return list.map((m, i) => (i % 2 === 0 ? { ...m, content: `REWRITTEN ${i}: completely different words, same slot ${"y".repeat(80)}` } : m)); +} + +// Shared system inputs — the NEW resolver never receives them; they exist +// so the corpus is identical for the OLD run. +const BASE_SYSTEM = "You are dsh, a desktop shell agent. " + "ambient config line. ".repeat(40); +const DRIFTED_SYSTEM = BASE_SYSTEM.replace("ambient config line.", "ambient config line (v2 with more words)."); +const REVIEW_SYSTEM = "You are an automated reviewer. Output only XML verdict tags."; +const conv = "session-2944v2"; + +function seedChain(id: string, chain: Msg[]): void { + resetPersonaAnchorsForTest(); + prefixAffinity.forget(id); + const fp = prefixAffinity.chainFingerprint(chain); + if (fp) prefixAffinity.note(id, fp.depth, fp.tailHash, fp.itemHashes, true); +} + +// --------------------------------------------------------------------------- +// OLD resolver: characterization pins (瞄点) — current ladder, untouched. +test("old ladder: characterization pins", () => { + // S1: first sight claims raw. + seedChain(conv, msgs(6, "seed")); + assert.equal(resolvePersonaKey(conv, BASE_SYSTEM, msgs(6, "seed")).key, conv); + + // S2: fp equal → match raw. + assert.equal(resolvePersonaKey(conv, BASE_SYSTEM, msgs(10, "seed")).key, conv); + + // S3: fp drift + chain continues → migrate raw (anchor moves). + const s3 = resolvePersonaKey(conv, DRIFTED_SYSTEM, msgs(10, "seed")); + assert.equal(s3.key, conv); + assert.equal(s3.action, "migrate"); + + // S4: fp drift + 50% content churn + chain continues → still raw + // (migrate — the ladder keys REPRESENTATION and forgives via chain). + assert.equal(resolvePersonaKey(conv, DRIFTED_SYSTEM, churn50(msgs(10, "seed"))).key, conv); + + // S5: truncated history under same id → raw (fp-equal rescue). + assert.equal(resolvePersonaKey(conv, DRIFTED_SYSTEM, msgs(6, "seed").slice(3)).key, conv); + + // S6: review blob under a claimed id → fork |sub:. + const blob = msgs(1, "review-blob").map((m) => ({ ...m, role: "user" })); + assert.equal(resolvePersonaKey(conv, REVIEW_SYSTEM, blob).key, `${conv}|sub:${sha(REVIEW_SYSTEM)}`); +}); + +// --------------------------------------------------------------------------- +// NEW resolver: same corpus, order+id semantics. +test("new resolver: same corpus, order+id semantics", () => { + resetSessionOrderForTest(); + + // S1: first sight, main-shaped → claim raw. + assert.equal(resolveSessionOrder(conv, msgs(6, "seed")).key, conv); + + // S2/P4: growth → continue raw. + const s2 = resolveSessionOrder(conv, msgs(10, "seed")); + assert.equal(s2.key, conv); + assert.equal(s2.action, "continue"); + + // S3: system drift invisible — the resolver takes no system input at + // all; the history is IDENTICAL to S2 → continue (old→new: the old + // ladder had to MIGRATE through chain-continuity to get here). + const s3: SessionOrderResolution = resolveSessionOrder(conv, msgs(10, "seed")); + assert.equal(s3.key, conv); + assert.equal(s3.action, "continue"); + + // S4/P1: 50% content churn, same kinds → SAME session. The recorded + // skeleton is content-blind: the churn leaves it byte-identical + // (old→new: old needed the chainContinues rescue → migrate). + const s4 = resolveSessionOrder(conv, churn50(msgs(10, "seed"))); + assert.equal(s4.key, conv); + assert.equal(s4.action, "continue"); + assert.deepEqual(sessionOrderSkeletonOf(conv), orderSkeletonOf(msgs(10, "seed"))); + + // S5/P3: truncated head, tail aligns → raw + reanchor; the skeleton + // follows the host's truth (re-anchored to the truncated kinds). + // S5/P3: truncated head, tail aligns → raw + reanchor. The truncated + // history is a VERBATIM SUFFIX of the recorded one (recorded = + // churn50(msgs(10)) after S4; slice(7) keeps its last three turns + // byte-identical — what native compaction actually produces, and + // exactly what the fragment guard verifies). + const s5 = resolveSessionOrder(conv, churn50(msgs(10, "seed")).slice(7)); + assert.equal(s5.key, conv); + assert.equal(s5.action, "reanchor"); + assert.deepEqual(sessionOrderSkeletonOf(conv), orderSkeletonOf(churn50(msgs(10, "seed")).slice(7))); + + // S6/P2: shallow blob under claimed id, TAG-LESS (the module-level + // corpus calls the resolver without a persona tag): rides the raw key — + // the old ladder's non-anchoring path for system-less calls. A REAL + // dsh review carries the REVIEW_POLICY system, which the wired + // resolver sees as a different personaTag and forks onto + // `|sub:` (pinned in the dsh-persona e2e suites). + const blob = msgs(1, "review-blob").map((m) => ({ ...m, role: "user" })); + const s6 = resolveSessionOrder(conv, blob); + assert.equal(s6.key, conv); + assert.equal(s6.action, "continue"); + // The same blob WITH a persona tag forks onto the stable lane key. + const s6b = resolveSessionOrder(conv, blob, "You are the reviewer."); + assert.equal(s6b.key, `${conv}|sub:${skel16("You are the reviewer.")}`); + assert.equal(s6b.action, "derived-persona"); +}); + +// P5 guard rail: deep main-shaped history sharing NOTHING with the recorded +// skeleton under the same id → derived-task, never raw. The task key is +// DISCRIMINATED by skeleton hash (v2.1): two different strangers never +// share one compression session. +test("P5: deep main-shaped stranger → derived-task (skeleton-hash key)", () => { + resetSessionOrderForTest(); + resolveSessionOrder(conv, msgs(8, "seed")); + const stranger: Msg[] = []; + const roles = ["user", "user", "user", "assistant", "user", "assistant", "user", "assistant", "assistant"]; + roles.forEach((r, i) => stranger.push({ role: r, content: `stranger ${i} ${"z".repeat(60)}` })); + const res = resolveSessionOrder(conv, stranger); + assert.equal(res.key, `${conv}|sub:task:${skel8(orderSkeletonOf(stranger))}`); + assert.equal(res.action, "derived-task"); + // The same stranger replaying across passes keeps a STABLE key. + assert.equal(resolveSessionOrder(conv, stranger).key, res.key); +}); + +// --------------------------------------------------------------------------- +// P8 (v2.1): mixed traffic under ONE main id — the real-corpus proof +// scenarios (/tmp/mixed-traffic.mjs), pinned. Two holes the order-only v2 +// draft had and the fragment guard rail closes: +// (a) a codex-style subagent replay (#150 id reuse) is SHORTER with an +// alternating tail that ALIGNs — order-identical to genuine head +// truncation. Only the tail CONTENT fragments separate them: genuine +// compaction keeps the old tail messages verbatim (frags equal), a +// subagent carries different content (frags differ). +// (b) two different subagents under one main id must not collide on one +// |sub:task key — the skeleton-hash discriminator splits them. +test("P8: mixed traffic — subagent replays never steal the raw key", () => { + resetSessionOrderForTest(); + const main = msgs(7, "seed"); // u a u a u a u + assert.equal(resolveSessionOrder(conv, main).action, "claim"); + + // Subagent A: shorter, tail aligns by alternation, different content → + // the fragment guard fails the raw branch → derived-task. + const subA: Msg[] = [ + { role: "user", content: `subagent question about the codebase ${"s".repeat(80)}` }, + { role: "assistant", content: `subagent answer ${"s".repeat(80)}` }, + { role: "user", content: `subagent follow-up ${"s".repeat(80)}` }, + ]; + const rA = resolveSessionOrder(conv, subA); + assert.equal(rA.action, "derived-task"); + assert.equal(rA.key, `${conv}|sub:task:${skel8(orderSkeletonOf(subA))}`); + // Replay of the SAME subagent keeps the stable key. + assert.equal(resolveSessionOrder(conv, subA).key, rA.key); + + // Subagent B: a different shape → a different task key (no collision). + const subB: Msg[] = [ + { role: "user", content: `second subagent ${"w".repeat(80)}` }, + { role: "user", content: `more context ${"w".repeat(80)}` }, + { role: "assistant", content: `second answer ${"w".repeat(80)}` }, + { role: "user", content: `second follow-up ${"w".repeat(80)}` }, + { role: "assistant", content: `done ${"w".repeat(80)}` }, + ]; + const rB = resolveSessionOrder(conv, subB); + assert.equal(rB.action, "derived-task"); + assert.notEqual(rB.key, rA.key); + + // Aux title traffic, tag-less → rides raw (old non-anchoring path); + // with its title persona tag → its own stable lane (production shape). + const aux: Msg[] = [{ role: "user", content: `generate a title for this conversation ${"t".repeat(80)}` }]; + assert.equal(resolveSessionOrder(conv, aux).key, conv); + const rAux = resolveSessionOrder(conv, aux, "You are a title generator."); + assert.equal(rAux.action, "derived-persona"); + assert.equal(rAux.key, `${conv}|sub:${skel16("You are a title generator.")}`); + + // The main conversation keeps flowing on the raw key through all of it. + const rMain = resolveSessionOrder(conv, msgs(9, "seed")); + assert.equal(rMain.action, "continue"); + assert.equal(rMain.key, conv); + + // Genuine truncation (dsh native compaction): head cut, the surviving + // tail messages VERBATIM → fragment guard passes → raw + reanchor. + const truncated = msgs(9, "seed").slice(6); // last 3 turns, verbatim objects + const rTr = resolveSessionOrder(conv, truncated); + assert.equal(rTr.action, "reanchor"); + assert.equal(rTr.key, conv); +}); + +// Aux traffic FIRST (before any main claim) must never claim raw. +test("shallow-first: aux blob on virgin id → derived-aux, raw unclaimed", () => { + resetSessionOrderForTest(); + const blob = msgs(1, "x").map((m) => ({ ...m, role: "user" })); + const res = resolveSessionOrder("session-virgin", blob); + assert.equal(res.action, "derived-aux"); + assert.equal(res.key, "session-virgin|sub:aux"); + // The blob records NOTHING under the raw id — only a main-shaped + // history may establish the skeleton. + assert.equal(sessionOrderSkeletonOf("session-virgin"), undefined); + // And the main conversation arriving afterwards still claims raw. + const main = resolveSessionOrder("session-virgin", msgs(6, "main")); + assert.equal(main.action, "claim"); + assert.equal(main.key, "session-virgin"); + assert.deepEqual(sessionOrderSkeletonOf("session-virgin"), orderSkeletonOf(msgs(6, "main"))); +}); + +// Skeleton is content-blind by construction: kinds of a churned history are +// identical to the original, on every wire shape. +test("orderSkeletonOf ignores content, keeps role/tool shape", () => { + const a = msgs(6, "seed"); + const b = churn50(a); + assert.deepEqual(orderSkeletonOf(a), orderSkeletonOf(b)); + // Tool turns are their own skeleton cells on every wire (shape, not + // content — the tool NAME/ARGS live in text/calls, not in kind). + const openaiTool = [ + { role: "user", content: "list the files" }, + { role: "assistant", content: "", tool_calls: [{ id: "c1", function: { name: "bash", arguments: "{}" } }] }, + { role: "tool", tool_call_id: "c1", content: "ok" }, + { role: "assistant", content: "done" }, + ]; + const anthropicTool = [ + { role: "user", content: [{ type: "text", text: "list the files" }] }, + { role: "assistant", content: [{ type: "tool_use", id: "c1", name: "bash", input: {} }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "c1", content: "ok" }] }, + { role: "assistant", content: [{ type: "text", text: "done" }] }, + ]; + assert.deepEqual(orderSkeletonOf(openaiTool), ["user", "assistant", "tool", "assistant"]); + assert.deepEqual(orderSkeletonOf(anthropicTool), ["user", "assistant", "tool", "assistant"]); +}); + +// --------------------------------------------------------------------------- +// P6 (v2-only): the SAME logical conversation, rendered on all four wires, +// yields the IDENTICAL skeleton — and the resolution sequence is identical +// (claim on first sight, continue on growth). v1's raw-field kind strings +// were per-wire ("assistant:tool_use" vs "assistant:tool_calls") and could +// never pass this proof. +test("P6: cross-wire — same conversation, four renderings, one skeleton", () => { + // Logical conversation: u0 a0 u1 A(tool c1) T(c1) a2 u3 a3 + const openai = [ + { role: "user", content: "u0 question" }, + { role: "assistant", content: "a0 answer" }, + { role: "user", content: "u1 run the tool" }, + { role: "assistant", content: "", tool_calls: [{ id: "c1", function: { name: "bash", arguments: "{}" } }] }, + { role: "tool", tool_call_id: "c1", content: "ok" }, + { role: "assistant", content: "a2 done" }, + { role: "user", content: "u3 follow up" }, + { role: "assistant", content: "a3 final" }, + ]; + const anthropic = [ + { role: "user", content: [{ type: "text", text: "u0 question" }] }, + { role: "assistant", content: [{ type: "text", text: "a0 answer" }] }, + { role: "user", content: [{ type: "text", text: "u1 run the tool" }] }, + { role: "assistant", content: [{ type: "tool_use", id: "c1", name: "bash", input: {} }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "c1", content: "ok" }] }, + { role: "assistant", content: [{ type: "text", text: "a2 done" }] }, + { role: "user", content: [{ type: "text", text: "u3 follow up" }] }, + { role: "assistant", content: [{ type: "text", text: "a3 final" }] }, + ]; + const google = [ + { role: "user", parts: [{ text: "u0 question" }] }, + { role: "model", parts: [{ text: "a0 answer" }] }, + { role: "user", parts: [{ text: "u1 run the tool" }] }, + { role: "model", parts: [{ functionCall: { name: "bash", args: {} } }] }, + { role: "user", parts: [{ functionResponse: { response: { output: "ok" } } }] }, + { role: "model", parts: [{ text: "a2 done" }] }, + { role: "user", parts: [{ text: "u3 follow up" }] }, + { role: "model", parts: [{ text: "a3 final" }] }, + ]; + const responses = [ + { type: "message", role: "user", content: "u0 question" }, + { type: "message", role: "assistant", content: "a0 answer" }, + { type: "message", role: "user", content: "u1 run the tool" }, + { type: "function_call", call_id: "c1", name: "bash", arguments: "{}" }, + { type: "function_call_output", call_id: "c1", output: "ok" }, + { type: "message", role: "assistant", content: "a2 done" }, + { type: "message", role: "user", content: "u3 follow up" }, + { type: "message", role: "assistant", content: "a3 final" }, + ]; + const expected = ["user", "assistant", "user", "assistant", "tool", "assistant", "user", "assistant"]; + assert.deepEqual(orderSkeletonOf(openai), expected); + assert.deepEqual(orderSkeletonOf(anthropic), expected); + assert.deepEqual(orderSkeletonOf(google), expected); + assert.deepEqual(orderSkeletonOf(responses), expected); + + // Identity across wires: claim on wire A, GROW on wire B → continue. + // (A stateless client switched protocol mid-conversation — #2791's + // scenario — keeps the raw key and its fold ledger.) + resetSessionOrderForTest(); + const id = "cross-wire-conv"; + assert.equal(resolveSessionOrder(id, openai).action, "claim"); + const grownAnthropic = [...anthropic, { role: "user", content: [{ type: "text", text: "u4 next" }] }, { role: "assistant", content: [{ type: "text", text: "a4" }] }]; + const res = resolveSessionOrder(id, grownAnthropic); + assert.equal(res.action, "continue"); + assert.equal(res.key, id); + const grownGoogle = [...google, { role: "user", parts: [{ text: "u4 next" }] }, { role: "model", parts: [{ text: "a4" }] }]; + assert.equal(resolveSessionOrder(id, grownGoogle).action, "continue"); + const grownResponses = [...responses, { type: "message", role: "user", content: "u4 next" }, { type: "message", role: "assistant", content: "a4" }]; + assert.equal(resolveSessionOrder(id, grownResponses).action, "continue"); +}); + +// --------------------------------------------------------------------------- +// P7 (v2-only): host machinery must not move the skeleton. +test("P7: leading-system add/remove + client-marker turn leave the skeleton unchanged", () => { + const base = msgs(6, "seed"); + const withSystem = [ + { role: "system", content: "You are dsh. " + "harness state: date cwd budget. ".repeat(20) }, + ...base, + ]; + const withMarker = [ + ...base.slice(0, 4), + { role: "user", content: "[model changed: glm-5.3 → qwen3.8-27b]" }, + ...base.slice(4), + ]; + assert.deepEqual(orderSkeletonOf(withSystem), orderSkeletonOf(base)); + assert.deepEqual(orderSkeletonOf(withMarker), orderSkeletonOf(base)); + + // And the resolver agrees: claim the base, then arrive with system + // rows and a marker turn injected → continue (v1 would misalign the + // skeleton and derive |sub:task). + resetSessionOrderForTest(); + const id = "machinery-conv"; + assert.equal(resolveSessionOrder(id, base).action, "claim"); + const res = resolveSessionOrder(id, [...withSystem.slice(0, 1), ...withMarker]); + assert.equal(res.action, "continue"); + assert.equal(res.key, id); +});