diff --git a/docs/msgid-infrastructure.md b/docs/msgid-infrastructure.md index deb4f2fbf7..2c2c39d43b 100644 --- a/docs/msgid-infrastructure.md +++ b/docs/msgid-infrastructure.md @@ -21,7 +21,7 @@ Discipline: collectors only "obtain and pre-screen"; they hold NO identity-adjud Four carriers, **ALL stripped before forwarding to the model upstream** — the model wire still carries no message-level ids (#2821 doctrine): -1. **Envelope field** `biliMsgIds`: parallel array on full-resend model bodies, length-locked to messages, same lifecycle (stripped at ingress, src/server/prepare-anthropic.ts et al.). +1. **Envelope field** `biliMsgIds`: parallel array on full-resend model bodies, length-locked to the **post-hoist core count** (not the wire element count) — clients prove core-alignment via the kernel `wireIndex` before stamping (src/agent/pi-msgids.ts `coreAlignEnvelopeIds`; any mismatch bails and the request ships byte-identical). Wired: pi + omp full-resend (src/agent/pi.ts `stampMsgIdEnvelope`), gated by `destinationRoutedThroughProxy` — a blind tunnel never gets stamped (#1403). Same lifecycle (stripped at ingress, src/server/prepare-anthropic.ts et al.). 2. **Incremental turn payload** `msgIds`: envelope on `POST /__bili/plugin/turn` (src/agent/incremental-client.ts); server re-stamps `biliMsgIds` (src/incremental-lane.ts). 3. **Watermark headers** `x-bili-pf-tail-ids` (v2-v4, #5403/#5418): window segment only = last anchor element + all elements added since K, percent-encoded per segment, comma-separated (commas inside ids travel as `%2C`, separators stay literal); sibling `x-bili-pf-count` count stamp. pf-* headers are **protocol headers, not hop headers**: stripped before upstream via `UPSTREAM_HOP_HEADERS`. 4. **Ledger-seq sibling** `biliLedgerSeqs` / `ledgerSeq` stamps (#4316 L3 PR2/3): 1-based positive-int parallel arrays, validated by `validateMsgSeqs`, same strict-validate-then-degrade lifecycle. diff --git a/reference/architecture.md b/reference/architecture.md index 41bc0ffb6a..5eeb5f201b 100644 --- a/reference/architecture.md +++ b/reference/architecture.md @@ -234,7 +234,7 @@ billion-context/ │ │ ├── opencode-v2-msgids.ts # Opencode V2 per-turn msgid extraction + header stamp (#5825) │ │ ├── pf-channel.ts # Watermark channel client state machine: learn, capture, emit (#5403) │ │ ├── pi-inline.ts # Pi inline lane: serves INLINE_ORIGIN traffic in-process (#3278) -│ │ ├── pi-msgids.ts # Pi wire-branch lockstep walker resolving host msgids (#4491) +│ │ ├── pi-msgids.ts # Pi wire-branch lockstep walker resolving host msgids (#4491) + core-alignment proof for the full-resend envelope (#6525) │ │ ├── pi-subagent-registry.ts # Self-registration into pi-subagents required-child extensions (#2185) │ │ ├── tool-guardrails.ts # Tool-output guardrails migrated from billion-context-pi (#3066) │ │ └── dsh-native-client-react-shim.d.ts # Ambient "react" types for the dsh bundle (#1590) diff --git a/src/agent/pi-msgids.ts b/src/agent/pi-msgids.ts index a25f636cac..b9ae9dac93 100644 --- a/src/agent/pi-msgids.ts +++ b/src/agent/pi-msgids.ts @@ -44,6 +44,11 @@ // stripped with pi's own regex; cache_control decorations are ignored (the // wire may add them; they never change identity). +// #6525: the core-alignment proof below runs the KERNEL's own wire→core +// projection (the same one the proxy runs before pinning), so a stamp that +// passes it is guaranteed to be the exact array the server will validate. +import { anthropicToCore, openaiToCore } from "acp-kernel/wire"; + type FpPart = { t: "text" | "img" | "tooluse" | "toolres" | "think" | "redact"; v: string }; /** Mirror of pi's sanitizeSurrogates (utils/sanitize-unicode.js): strips @@ -465,3 +470,53 @@ export function laneMsgIdsForOpenAiWire(wire: unknown, branch: unknown): string[ const leftover = take(); return leftover === "exhausted" ? ids : undefined; } + +// #6525 (发): CORE-alignment proof shared by every client's full-resend +// msgid envelope stamp (dsh #6532 inlines the same logic; pi/omp call this). +// Input = the WALKER's wire-aligned output (openai wire keeps its leading +// bili-ps-N hoist slots) + the wire messages + the full payload. Returns the +// core-aligned id array — == the POST-hoist core count the server validates +// — or undefined when a 1:1 host pin is not provable; the caller must then +// send NO envelope (byte-identical to before, never a misaligned array). +// The kernel's wireIndex is the alignment authority: a wire element that +// splits into N cores (or vanishes) has no 1:1 host id, so the whole stamp +// bails. Duplicates are refused here too (validateMsgIds would reject them +// server-side anyway — refusing locally skips the one-time warn path). +export function coreAlignEnvelopeIds( + fmt: "openai" | "anthropic", + wireAligned: string[], + wire: unknown, + parsed: Record, +): string[] | undefined { + if (!Array.isArray(wire)) return undefined; + // The walk's contract is 1:1 with the element count — a mismatched pair + // here would silently misalign the envelope, so refuse it outright. + if (wireAligned.length !== wire.length) return undefined; + // OpenAI wire only: the leading system/developer run becomes synthetic + // bili-ps-N slots (== the kernel's hoisted prefix) — drop them; the + // envelope must be CORE-aligned (the server validates against the + // post-hoist core count). Anthropic has no hoist (system is a separate + // field), so lead stays 0 there. A bili-ps- slot anywhere PAST the lead + // run means a mid-body system the walker could not place → bail. + let lead = 0; + while (lead < wireAligned.length && (wireAligned[lead] as string).startsWith("bili-ps-")) lead += 1; + for (let i = lead; i < wireAligned.length; i++) { + if ((wireAligned[i] as string).startsWith("bili-ps-")) return undefined; + } + const coreAligned = wireAligned.slice(lead); + if (coreAligned.length === 0) return undefined; + const flat = fmt === "anthropic" + ? anthropicToCore(parsed as Parameters[0]) + : openaiToCore(parsed as Parameters[0]); + const perEl = new Map(); + for (const c of flat.msgs) { + const w = c.wireIndex; + if (w === undefined) continue; + perEl.set(w, (perEl.get(w) ?? 0) + 1); + } + for (let i = lead; i < wire.length; i++) { + if ((perEl.get(i) ?? 0) !== 1) return undefined; // element split into N cores (or vanished) — no 1:1 host pin exists + } + if (new Set(coreAligned).size !== coreAligned.length) return undefined; + return coreAligned; +} diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 83792937a7..92f2e82756 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -17,7 +17,8 @@ import { isModelApiUrl, nativeInterceptInstalled } from "./native-intercept.js"; import { wirePiSubagents, piCommandsEnabled } from "./pi-subagents.js"; import { wireToolGuardrails } from "./tool-guardrails.js"; import { clearIncrementalProbe, noteHostMsgIdHint, INCREMENTAL_TURN_MARKER_HEADER, incrementalEnvelopeConversationId, incrementalWireHash, noteIncrementalRejection, noteIncrementalAck, planIncrementalTurn, probeIncrementalTurns, pullIncrementalReceipt, sweepStale, wireMessagesLookAnthropic, type HostMsgIdHintEntry, type IncrementalWireMessage } from "./incremental-client.js"; -import { laneMsgIdsForOpenAiWire, laneMsgIdsForWire, lanePinnableContent } from "./pi-msgids.js"; +import { coreAlignEnvelopeIds, laneMsgIdsForOpenAiWire, laneMsgIdsForWire, lanePinnableContent } from "./pi-msgids.js"; +import { detectWireFormat } from "acp-kernel/wire"; import * as pfChannel from "./pf-channel.js"; import { validateMsgIds } from "../msgid-pin.js"; import type { DirectSendPrep } from "./native-intercept.js"; @@ -513,35 +514,11 @@ export function stampPromptCacheKey(event: unknown, ctx: Ctx, agent: string): Re noteOmpCoworkBypass(ctx.model?.id ?? (ctx.model?.baseUrl ?? "")); return undefined; } - if (!destinationRoutedThroughProxy(ctx.model?.baseUrl)) { - // #1579: native omp mode routes at the FETCH layer — the model - // baseUrl stays the real upstream, so the launcher-shaped checks - // (a /bili/-wrapped URL, the BILLION_CONTEXT_PROXY origin, the MITM - // whitelist) can never match and #1403's gate silently killed the - // identity stamp for EVERY native session. Mirror the interceptor's - // own predicate instead: when the native intercept is installed it - // WILL rewrite any isModelApiUrl request URL to /bili/. - // The interceptor judges the FULL request URL while ctx carries only - // the baseUrl prefix (".../v1" — not suffix-matched by itself), so - // probe both the bare baseUrl and its natural chat-completions - // expansion; the proxy consumes (and strips) the stamped pck exactly - // as on the launcher lanes. Anything the interceptor would NOT - // rewrite keeps #1403's guarantee — never stamp where the proxy - // cannot see it. The install flag alone is not routing evidence — it - // outlives routing (failed bootstrap, or proxy death + respawn - // give-up: onGiveUp clears BILLION_CONTEXT_PROXY but not the flag), - // and an un-routed stamped request rides verbatim into strict-schema - // upstreams (#1403's 400). Live claim = same env-keyed signal - // ownsCompaction / registerTools / runtime-info use; bootstrap - // rewrites it on (re-)establishment so degrades/recovery track the - // traffic. Round 1 inside the pre-bootstrap window misses the stamp - // and self-heals on round 2. - if (!nativeInterceptInstalled()) return undefined; - if (detectProxyBase(ctx.model?.baseUrl) === undefined) return undefined; - const base = ctx.model?.baseUrl ?? ""; - const expanded = /\/v\d+\/?$/.test(base) ? `${base.replace(/\/+$/, "")}/chat/completions` : base; - if (!isModelApiUrl(base) && !isModelApiUrl(expanded)) return undefined; - } + // #6525: the routing/visibility gate now lives in bodyProxyVisible — + // shared with the full-resend msgid envelope stamp so the two stamp + // paths (pck #268, envelope #6525) can never drift. The #1579/#1403 + // native-fetch rationale moved with it (see bodyProxyVisible). + if (!bodyProxyVisible(ctx, agent)) return undefined; const payload = (event as { payload?: unknown } | undefined)?.payload; if (payload === null || typeof payload !== "object" || Array.isArray(payload)) return undefined; const p = payload as Record; @@ -553,6 +530,104 @@ export function stampPromptCacheKey(event: unknown, ctx: Ctx, agent: string): Re return { ...p, prompt_cache_key: sid }; } +// #6525: single source of truth for "the proxy will see this request's +// BODY". Body fields the proxy consumes and strips (prompt_cache_key #268, +// the biliMsgIds envelope #6525) may be stamped ONLY when true — a +// blind-tunneled destination never sees the field, and stamping there rides +// it verbatim into strict-schema upstreams (#1403's 400). Mirrors +// stampPromptCacheKey's gate exactly so the pck and envelope stamp paths can +// never drift. Rationale (from the pck gate it replaces): native omp mode +// routes at the FETCH layer — the model baseUrl stays the real upstream, so +// the launcher-shaped checks (a /bili/-wrapped URL, the BILLION_CONTEXT_PROXY +// origin, the MITM whitelist) can never match and #1403's gate silently +// killed the identity stamp for EVERY native session. Mirror the +// interceptor's own predicate instead: when the native intercept is +// installed it WILL rewrite any isModelApiUrl request URL to +// /bili/. The interceptor judges the FULL request URL while ctx +// carries only the baseUrl prefix (".../v1" — not suffix-matched by +// itself), so probe both the bare baseUrl and its natural chat-completions +// expansion; the proxy consumes (and strips) the stamped field exactly as on +// the launcher lanes. Anything the interceptor would NOT rewrite keeps +// #1403's guarantee — never stamp where the proxy cannot see it. The install +// flag alone is not routing evidence — it outlives routing (failed +// bootstrap, or proxy death + respawn give-up: onGiveUp clears +// BILLION_CONTEXT_PROXY but not the flag), and an un-routed stamped request +// rides verbatim into strict-schema upstreams. Round 1 inside the +// pre-bootstrap window misses the stamp and self-heals on round 2. +function bodyProxyVisibleForTest(ctx: Ctx, agent: string): boolean { + return bodyProxyVisible(ctx, agent); +} +export { bodyProxyVisibleForTest as _bodyProxyVisibleForTest }; +function bodyProxyVisible(ctx: Ctx, agent: string): boolean { + const profile = hostProfileFor(agent); + // Cowork transport bypasses the fetch intercept (#2583) — same reason it + // withholds the pck stamp: the field would ride verbatim into Anthropic's + // strict schema (#1403). The one-time diagnostic note stays at the pck + // site; this is the pure predicate. + if (profile.coworkTransportPossible && ompCoworkTransportBypassesFetch(ctx.model, process.env)) return false; + if (destinationRoutedThroughProxy(ctx.model?.baseUrl)) return true; + if (!nativeInterceptInstalled()) return false; + if (detectProxyBase(ctx.model?.baseUrl) === undefined) return false; + const base = ctx.model?.baseUrl ?? ""; + const expanded = /\/v\d+\/?$/.test(base) ? `${base.replace(/\/+$/, "")}/chat/completions` : base; + return isModelApiUrl(base) || isModelApiUrl(expanded); +} + +// #6525 (发): full-resend turns ride the msgid envelope too — the +// LANE-INDEPENDENT send path. The incremental lane stamps its own msgIds on +// the delta it diverts (and early-returns before this line); this covers the +// turns the lane shape-gates away (#3433: OpenAI-family full resends) plus +// every other full-resend for both pi and omp. Core-aligned via the kernel +// wireIndex proof (coreAlignEnvelopeIds); ANY mismatch bails to no field — +// byte-identical to today, never a misaligned array. The pairing source is +// the live branch (ctx.sessionManager.getBranch) — the same source the +// incremental lane and the watermark channel use, so no new collector. +function resolveEnvelopeIdsForFullResend(parsed: Record, ctx: Ctx): string[] | undefined { + try { + const fmt = detectWireFormat(parsed); + if (fmt !== "openai" && fmt !== "anthropic") return undefined; // google/responses stay content-derived (#6525 v1) + const wire = parsed.messages; + if (!Array.isArray(wire) || wire.length === 0) return undefined; + let branch: unknown; + try { + branch = ctx.sessionManager?.getBranch?.(); + } catch { + branch = undefined; + } + if (branch === null || branch === undefined) return undefined; + const wireAligned = fmt === "anthropic" + ? laneMsgIdsForWire(wire, branch) + : laneMsgIdsForOpenAiWire(wire, branch); + if (wireAligned === undefined) return undefined; // pairing not provable — degrade + return coreAlignEnvelopeIds(fmt, wireAligned, wire, parsed); + } catch { + return undefined; // kernel converter threw on a malformed payload — never stamp + } +} + +// #6525 (发): stamp the core-aligned host ids onto the outgoing full-resend +// body. Returns the payload to send (base + biliMsgIds), or undefined to +// leave the request exactly as-is. base = the pck-stamped payload when the +// pck stamp fired (omp), else the original event payload (pi / omp-not- +// visible). Gated by bodyProxyVisible so a blind-tunneled request is never +// stamped (#1403). +export { resolveEnvelopeIdsForFullResend as _resolveEnvelopeIdsForFullResendForTest, stampMsgIdEnvelope as _stampMsgIdEnvelopeForTest }; +function stampMsgIdEnvelope( + event: unknown, + ctx: Ctx, + agent: string, + pck: Record | undefined, +): Record | undefined { + if (!bodyProxyVisible(ctx, agent)) return undefined; + const baseRaw = pck !== null && typeof pck === "object" && !Array.isArray(pck) ? pck : (event as { payload?: unknown } | undefined)?.payload; + if (baseRaw === null || typeof baseRaw !== "object" || Array.isArray(baseRaw)) return undefined; + const base = baseRaw as Record; + if (base.biliMsgIds !== undefined) return undefined; // already stamped — never double-stamp + const ids = resolveEnvelopeIdsForFullResend(base, ctx); + if (ids === undefined) return undefined; + return { ...base, biliMsgIds: ids }; +} + function fmtTok(n: number): string { if (n < 1000) return String(n); if (n < 1_000_000) return `${(n / 1000).toFixed(1)}K`; @@ -1598,7 +1673,13 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM logHostEvent("error", "[native-client]", `bili-plugin(${agent}): incremental turn skipped (${err instanceof Error ? err.message : String(err)}) — full resend this turn`); } } - return stampPromptCacheKey(event, ctx, agent); + // #6525 (发): full-resend turns also carry the msgid envelope + // (the lane-independent send path). stampMsgIdEnvelope folds in + // the pck stamp when it fired (base = pck-stamped payload for + // omp, original payload for pi) and returns undefined to leave + // the request byte-identical when the envelope can't be stamped. + const pck = stampPromptCacheKey(event, ctx, agent); + return stampMsgIdEnvelope(event, ctx, agent, pck) ?? pck; }); // #3248 incremental turns: the ack channel. after_provider_response // fires with status + headers BEFORE the stream is consumed, so the diff --git a/tests/issue6525-pi-omp-envelope.test.ts b/tests/issue6525-pi-omp-envelope.test.ts new file mode 100644 index 0000000000..594ebc6ad3 --- /dev/null +++ b/tests/issue6525-pi-omp-envelope.test.ts @@ -0,0 +1,244 @@ +/** #6525 (发): pi/omp full-resend msgid envelope — the LANE-INDEPENDENT send + * path. The incremental lane already stamps `msgIds` on the deltas it diverts + * (anthropic wire only); this is the envelope (`biliMsgIds`) on EVERY + * full-resend body — core-aligned via the kernel wireIndex proof, any + * mismatch bails to no field (byte-identical to today). Tests: + * A. coreAlignEnvelopeIds — the core-alignment proof itself + * B. _bodyProxyVisibleForTest — the #1403 gate (blind tunnel: never stamp) + * C. _resolveEnvelopeIdsForFullResendForTest — resolver (walker + proof) + * D. _stampMsgIdEnvelopeForTest — the stamp (base + biliMsgIds, guards) + * No new msgid VALIDATOR is introduced here (the parity-matrix discipline): + * pairing reuses the lane walkers; the server re-validates at pin time. */ +import assert from "node:assert/strict"; +import test from "node:test"; +import { coreAlignEnvelopeIds } from "../src/agent/pi-msgids.ts"; +import { + _bodyProxyVisibleForTest, + _resolveEnvelopeIdsForFullResendForTest, + _stampMsgIdEnvelopeForTest, +} from "../src/agent/pi.ts"; + +// --------------------------------------------------------------------------- +// A. coreAlignEnvelopeIds +// --------------------------------------------------------------------------- + +/** The 1:1 openai conversation: lead system hoist + a plain tool round. + * Kernel projection (verified against acp-kernel/wire): user→1 core, + * assistant(content null, one tool_call)→1 core, tool→1 core, + * assistant text→1 core. Every element lands on exactly one core. */ +const OAI_WIRE_1TO1 = [ + { role: "system", content: "you are a test" }, + { role: "user", content: "q" }, + { role: "assistant", content: null, tool_calls: [{ id: "call_1", type: "function", function: { name: "t", arguments: "{}" } }] }, + { role: "tool", tool_call_id: "call_1", content: "result" }, + { role: "assistant", content: "done" }, +]; +const OAI_ALIGNED_1TO1 = ["bili-ps-0", "u1", "a2", "t3", "a4"]; + +test("coreAlignEnvelopeIds: openai 1:1 with lead system → core ids (lead dropped)", () => { + const parsed = { messages: OAI_WIRE_1TO1, model: "m" }; + assert.deepEqual( + coreAlignEnvelopeIds("openai", OAI_ALIGNED_1TO1, OAI_WIRE_1TO1, parsed), + ["u1", "a2", "t3", "a4"], + ); +}); + +test("coreAlignEnvelopeIds: openai assistant text+tool_call splits one element into two cores → bail", () => { + const wire = [ + { role: "user", content: "q" }, + { role: "assistant", content: "talking", tool_calls: [{ id: "call_1", type: "function", function: { name: "t", arguments: "{}" } }] }, + ]; + assert.equal(coreAlignEnvelopeIds("openai", ["u0", "a1"], wire, { messages: wire, model: "m" }), undefined); +}); + +test("coreAlignEnvelopeIds: openai user [text, image] splits into two cores → bail", () => { + const wire = [ + { role: "user", content: [{ type: "text", text: "see" }, { type: "image_url", image_url: { url: "http://x/i.png" } }] }, + { role: "user", content: "b" }, + ]; + assert.equal(coreAlignEnvelopeIds("openai", ["u0", "u1"], wire, { messages: wire, model: "m" }), undefined); +}); + +test("coreAlignEnvelopeIds: openai two tool_calls on one assistant → bail", () => { + const wire = [ + { role: "user", content: "q" }, + { + role: "assistant", + content: null, + tool_calls: [ + { id: "call_1", type: "function", function: { name: "t", arguments: "{}" } }, + { id: "call_2", type: "function", function: { name: "t", arguments: "{}" } }, + ], + }, + ]; + assert.equal(coreAlignEnvelopeIds("openai", ["u0", "a1"], wire, { messages: wire, model: "m" }), undefined); +}); + +test("coreAlignEnvelopeIds: a bili-ps- slot past the lead run (mid-body system) → bail", () => { + const wire = [ + { role: "system", content: "sys" }, + { role: "user", content: "q" }, + { role: "system", content: "mid" }, + ]; + assert.equal(coreAlignEnvelopeIds("openai", ["bili-ps-0", "u1", "bili-ps-2"], wire, { messages: wire, model: "m" }), undefined); +}); + +test("coreAlignEnvelopeIds: lead-only (everything hoisted) → nothing to align → bail", () => { + assert.equal(coreAlignEnvelopeIds("openai", ["bili-ps-0"], [{ role: "system", content: "s" }], { messages: [{ role: "system", content: "s" }], model: "m" }), undefined); +}); + +test("coreAlignEnvelopeIds: duplicate id in the aligned tail → bail (no mis-pin)", () => { + const wire = [ + { role: "user", content: "q" }, + { role: "user", content: "r" }, + ]; + assert.equal(coreAlignEnvelopeIds("openai", ["u1", "u1"], wire, { messages: wire, model: "m" }), undefined); +}); + +test("coreAlignEnvelopeIds: aligned array not 1:1 with the wire element count → bail", () => { + const wire = [ + { role: "user", content: "q" }, + { role: "user", content: "r" }, + ]; + assert.equal(coreAlignEnvelopeIds("openai", ["u1"], wire, { messages: wire, model: "m" }), undefined); +}); + +test("coreAlignEnvelopeIds: anthropic 1:1 (system is a separate field — no lead drop)", () => { + const wire = [ + { role: "user", content: "q" }, + { role: "assistant", content: [{ type: "text", text: "a" }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "tu_1", content: "r" }] }, + ]; + const parsed = { system: "sys", messages: wire, model: "claude" }; + assert.deepEqual( + coreAlignEnvelopeIds("anthropic", ["u0", "a1", "t2"], wire, parsed), + ["u0", "a1", "t2"], + ); +}); + +test("coreAlignEnvelopeIds: non-array wire → bail without throwing", () => { + assert.equal(coreAlignEnvelopeIds("openai", ["u0"], "garbage", { messages: "garbage", model: "m" }), undefined); +}); + +// --------------------------------------------------------------------------- +// B. bodyProxyVisible — the #1403 gate +// --------------------------------------------------------------------------- + +test("bodyProxyVisible: a /bili/-wrapped baseUrl is proxy-visible", () => { + const visible = _bodyProxyVisibleForTest({ model: { baseUrl: "http://127.0.0.1:9999/bili/https://upstream.example.com/v1" } }, "pi"); + assert.equal(visible, true); +}); + +test("bodyProxyVisible: a plain upstream baseUrl (blind tunnel) is NOT proxy-visible", () => { + const saved = { proxy: process.env.BILLION_CONTEXT_PROXY, mitm: process.env.BILI_MITM_HOSTS }; + delete process.env.BILLION_CONTEXT_PROXY; + delete process.env.BILI_MITM_HOSTS; + try { + const visible = _bodyProxyVisibleForTest({ model: { baseUrl: "https://api.upstream.example.com/v1" } }, "pi"); + assert.equal(visible, false); + } finally { + if (saved.proxy === undefined) delete process.env.BILLION_CONTEXT_PROXY; + else process.env.BILLION_CONTEXT_PROXY = saved.proxy; + if (saved.mitm === undefined) delete process.env.BILI_MITM_HOSTS; + else process.env.BILI_MITM_HOSTS = saved.mitm; + } +}); + +// --------------------------------------------------------------------------- +// C. resolveEnvelopeIdsForFullResend +// --------------------------------------------------------------------------- + +/** A pi branch that pairs 1:1 onto OAI_WIRE_1TO1 (the walker's lockstep + * contract): entry id is the host msgid the envelope must carry. */ +function pairBranch() { + return [ + { type: "message", id: "u1", message: { role: "user", content: "q" } }, + { type: "message", id: "a2", message: { role: "assistant", content: [{ type: "toolCall", id: "call_1", name: "t", arguments: {} }] } }, + { type: "message", id: "t3", message: { role: "toolResult", toolCallId: "call_1", content: [{ type: "text", text: "result" }] } }, + { type: "message", id: "a4", message: { role: "assistant", content: [{ type: "text", text: "done" }] } }, + ]; +} + +const ctxWithBranch = (branch: unknown) => ({ + model: { baseUrl: "http://127.0.0.1:9999/bili/https://upstream.example.com/v1" }, + sessionManager: { getBranch: () => branch }, +}); + +test("resolveEnvelopeIds: openai full-resend pairs the branch → core-aligned host ids", () => { + const parsed = { messages: OAI_WIRE_1TO1, model: "m" }; + assert.deepEqual(_resolveEnvelopeIdsForFullResendForTest(parsed, ctxWithBranch(pairBranch())), ["u1", "a2", "t3", "a4"]); +}); + +test("resolveEnvelopeIds: a google wire (contents array) is out of scope → undefined", () => { + const parsed = { contents: [{ role: "user", parts: [{ text: "hi" }] }], model: "gemini" }; + assert.equal(_resolveEnvelopeIdsForFullResendForTest(parsed, ctxWithBranch(pairBranch())), undefined); +}); + +test("resolveEnvelopeIds: no branch available → undefined (never guess)", () => { + const parsed = { messages: OAI_WIRE_1TO1, model: "m" }; + assert.equal(_resolveEnvelopeIdsForFullResendForTest(parsed, { model: { baseUrl: "http://127.0.0.1:9999/bili/x" } }), undefined); +}); + +test("resolveEnvelopeIds: branch shorter than the wire (dropped message) → undefined", () => { + const parsed = { messages: OAI_WIRE_1TO1, model: "m" }; + const short = pairBranch().slice(0, 3); + assert.equal(_resolveEnvelopeIdsForFullResendForTest(parsed, ctxWithBranch(short)), undefined); +}); + +test("resolveEnvelopeIds: getBranch throwing is a bail, not a throw", () => { + const parsed = { messages: OAI_WIRE_1TO1, model: "m" }; + const ctx = { + model: { baseUrl: "http://127.0.0.1:9999/bili/x" }, + sessionManager: { getBranch: () => { throw new Error("manager gone"); } }, + }; + assert.equal(_resolveEnvelopeIdsForFullResendForTest(parsed, ctx), undefined); +}); + +// --------------------------------------------------------------------------- +// D. stampMsgIdEnvelope +// --------------------------------------------------------------------------- + +test("stamp: pi visible + pairable → payload gets biliMsgIds, everything else intact", () => { + const payload: Record = { messages: OAI_WIRE_1TO1, model: "m", stream: true }; + const out = _stampMsgIdEnvelopeForTest({ payload }, ctxWithBranch(pairBranch()), "pi", undefined); + assert.deepEqual(out, { messages: OAI_WIRE_1TO1, model: "m", stream: true, biliMsgIds: ["u1", "a2", "t3", "a4"] }); + assert.notEqual(out, payload); // spread copy — the original payload is not mutated + assert.equal(payload.biliMsgIds, undefined); +}); + +test("stamp: blind tunnel (not proxy-visible) → undefined → byte-identical request", () => { + const saved = { proxy: process.env.BILLION_CONTEXT_PROXY, mitm: process.env.BILI_MITM_HOSTS }; + delete process.env.BILLION_CONTEXT_PROXY; + delete process.env.BILI_MITM_HOSTS; + try { + const payload = { messages: OAI_WIRE_1TO1, model: "m" }; + const ctx = { model: { baseUrl: "https://api.upstream.example.com/v1" }, sessionManager: { getBranch: () => pairBranch() } }; + assert.equal(_stampMsgIdEnvelopeForTest({ payload }, ctx, "pi", undefined), undefined); + } finally { + if (saved.proxy === undefined) delete process.env.BILLION_CONTEXT_PROXY; + else process.env.BILLION_CONTEXT_PROXY = saved.proxy; + if (saved.mitm === undefined) delete process.env.BILI_MITM_HOSTS; + else process.env.BILI_MITM_HOSTS = saved.mitm; + } +}); + +test("stamp: omp pck object is the base — the envelope rides beside prompt_cache_key", () => { + const pck = { messages: OAI_WIRE_1TO1, model: "m", prompt_cache_key: "session-x" }; + const out = _stampMsgIdEnvelopeForTest({ payload: { messages: OAI_WIRE_1TO1, model: "m" } }, ctxWithBranch(pairBranch()), "omp", pck); + assert.deepEqual(out, { messages: OAI_WIRE_1TO1, model: "m", prompt_cache_key: "session-x", biliMsgIds: ["u1", "a2", "t3", "a4"] }); +}); + +test("stamp: already-stamped payload is never re-stamped", () => { + const payload = { messages: OAI_WIRE_1TO1, model: "m", biliMsgIds: ["existing"] }; + assert.equal(_stampMsgIdEnvelopeForTest({ payload }, ctxWithBranch(pairBranch()), "pi", undefined), undefined); +}); + +test("stamp: unpairable resolver result → undefined (the request ships as-is)", () => { + const payload = { messages: OAI_WIRE_1TO1, model: "m" }; + assert.equal(_stampMsgIdEnvelopeForTest({ payload }, ctxWithBranch(pairBranch().slice(0, 2)), "pi", undefined), undefined); +}); + +test("stamp: non-object payload → undefined without throwing", () => { + assert.equal(_stampMsgIdEnvelopeForTest({ payload: "garbage" }, ctxWithBranch(pairBranch()), "pi", undefined), undefined); + assert.equal(_stampMsgIdEnvelopeForTest({}, ctxWithBranch(pairBranch()), "pi", undefined), undefined); +});