Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/msgid-infrastructure.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion reference/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions src/agent/pi-msgids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, unknown>,
): 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<typeof anthropicToCore>[0])
: openaiToCore(parsed as Parameters<typeof openaiToCore>[0]);
const perEl = new Map<number, number>();
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;
}
143 changes: 112 additions & 31 deletions src/agent/pi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 <proxy>/bili/<url>.
// 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<string, unknown>;
Expand All @@ -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
// <proxy>/bili/<url>. 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<string, unknown>, 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<string, unknown> | undefined,
): Record<string, unknown> | 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<string, unknown>;
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`;
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading