diff --git a/src/anon-provenance.ts b/src/anon-provenance.ts new file mode 100644 index 0000000000..e070a7514c --- /dev/null +++ b/src/anon-provenance.ts @@ -0,0 +1,98 @@ +// #2879: provenance capture for ANONYMOUS (prefix-affinity-resolved) sessions. +// A pfa-* ledger's producer was otherwise unknowable after the fact: the file +// name hashes the id, every such ledger shared ONE display label, and the mint +// log carried no client fingerprint (#2847 misread four parallel pfa-* ledgers +// as twin files of one session). Capture is FIRST-WRITER-WINS per session id +// (the mint request IS the producer; fork semantics bind one pfa-* id to one +// lineage). Attribution-grade by design: header NAMES only (values NEVER — +// they may carry credentials), UA truncated, first-message preview collapsed, +// URL-masked and capped. + +import { maskUrlsInText } from "./log-mask.js"; + +export interface AnonymousProvenance { + ua?: string; + /** Header NAMES only, sorted (values never recorded). */ + headers: string[]; + /** x-bili-plugin marker value: the host extension announced its lane (e.g. + * "pi") yet produced NO usable conversation identity — the stamp-failure + * signature (toolsReady / sessionId gap), the key H1 discriminator. */ + pluginAgent?: string; + /** First user-role message preview (fallback: first message) — system + * prompts are persona-shared and carry no signal; the first user turn + * separates a main-history replay from a fresh sub-agent task. */ + firstMsg?: string; + remote?: string; + at: number; +} + +const MAX_PREVIEW_CHARS = 160; +const MAX_UA_CHARS = 120; +const MAX_HEADER_NAMES = 48; + +function firstHeaderValue(h: Record, name: string): string | undefined { + const v = h[name.toLowerCase()]; + if (typeof v === "string") return v.length > 0 ? v : undefined; + if (Array.isArray(v) && typeof v[0] === "string" && v[0].length > 0) return v[0]; + return undefined; +} + +function partText(v: unknown): string | undefined { + if (typeof v === "string") return v.length > 0 ? v : undefined; + if (Array.isArray(v)) { + for (const part of v) { + if (typeof part === "string") { + if (part.length > 0) return part; + continue; + } + if (part !== null && typeof part === "object") { + const p = part as Record; + if (typeof p.text === "string" && p.text.length > 0) return p.text; + if (typeof p.content === "string" && p.content.length > 0) return p.content; + } + } + return undefined; + } + return undefined; +} + +function previewOf(messages: unknown[]): string | undefined { + if (!Array.isArray(messages) || messages.length === 0) return undefined; + const objects = messages.filter((m): m is Record => m !== null && typeof m === "object"); + const target = objects.find((m) => m.role === "user") ?? objects[0] ?? messages[0]; + let raw: string | undefined; + if (typeof target === "string") { + raw = target; + } else if (target !== null && typeof target === "object") { + const o = target as Record; + // responses: input items {role, content:string|Part[]} · openai / + // anthropic: messages {role, content} · google: contents {parts:[{text}]} + raw = partText(o.content) ?? partText(o.parts) ?? partText(o.input) ?? JSON.stringify(target); + } + if (raw === undefined) return undefined; + const flat = maskUrlsInText(raw.replace(/\s+/g, " ")).trim(); + return flat.length > 0 ? flat.slice(0, MAX_PREVIEW_CHARS) : undefined; +} + +export function buildAnonymousProvenance( + headers: Record, + messages: unknown[], + remote?: string, +): AnonymousProvenance { + const h = headers ?? {}; + const names = Object.keys(h) + .filter((n) => n.length > 0) + .sort() + .slice(0, MAX_HEADER_NAMES); + const ua = firstHeaderValue(h, "user-agent"); + const pluginAgent = firstHeaderValue(h, "x-bili-plugin"); + const firstMsg = previewOf(messages); + return { + ...(ua !== undefined ? { ua: ua.slice(0, MAX_UA_CHARS) } : {}), + headers: names, + ...(pluginAgent !== undefined ? { pluginAgent: pluginAgent.slice(0, 64) } : {}), + ...(firstMsg !== undefined ? { firstMsg } : {}), + ...(remote !== undefined ? { remote } : {}), + at: Date.now(), + }; +} diff --git a/src/server/handle.ts b/src/server/handle.ts index 1af75f07f0..11bdeed9c1 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -36,6 +36,7 @@ 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 { prefixAffinity, type AnonymousAffinity } from "../prefix-affinity.js"; +import { buildAnonymousProvenance, type AnonymousProvenance } from "../anon-provenance.js"; import { maybeAdoptForkBlocks, maybeAdoptResume } from "../fork-adoption.js"; import { resolveClaudeTranscriptLineage } from "../claude-transcript-lineage.js"; import { consumePluginRegisterFor, pluginAgentHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginReportedModel, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, publicForkInputMatches, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "../plugin.js"; @@ -968,6 +969,9 @@ async function handleRequest( return affinityMessagesCache; }; let anonAffinity: AnonymousAffinity | null = null; + // #2879: captured on the MINT request only (see the else branch below) + // so the pfa-* ledger's producer stays attributable after the fact. + let anonProvenance: AnonymousProvenance | undefined; if (!clientProvided) { anonAffinity = prefixAffinity.resolve(affinityMessageList()); if (!anonAffinity) { @@ -988,9 +992,15 @@ async function handleRequest( log("info", `[prefix-affinity] anonymous ${protocol} request → session ${anonAffinity.sessionId} (prefix match depth=${anonAffinity.matchedDepth}/${anonAffinity.incomingDepth}, tail=${anonAffinity.tailHash.slice(0, 8)}; fork semantics: diverged histories split on their next request)`); loggerLog("info", `[prefix-affinity] session ${anonAffinity.sessionId} matched at depth ${anonAffinity.matchedDepth}/${anonAffinity.incomingDepth} (tail=${anonAffinity.tailHash.slice(0, 8)})`); } else { + // #2879: this fresh mint is the ONE observable moment the + // producer exists — capture its fingerprint here or the pfa-* + // ledger is forever unattributable (H1/H2 could not be told + // apart from #2847's files). First-writer-wins downstream. + anonProvenance = buildAnonymousProvenance(req.headers, affinityMessageList(), req.socket?.remoteAddress); + const provSuffix = `; ua=${JSON.stringify(anonProvenance.ua ?? "-")} plugin=${anonProvenance.pluginAgent ?? "-"} first=${JSON.stringify(anonProvenance.firstMsg ?? "-")}`; const lineage = anonAffinity.lineage ? `; lineage=${anonAffinity.lineage.reason} of ${anonAffinity.lineage.parents.join(",")}` : ""; - log("info", `[prefix-affinity] new anonymous session ${anonAffinity.sessionId} (depth=${anonAffinity.incomingDepth}, tail=${anonAffinity.tailHash.slice(0, 8)}${lineage})`); - loggerLog("info", `[prefix-affinity] new session ${anonAffinity.sessionId} at depth ${anonAffinity.incomingDepth} (tail=${anonAffinity.tailHash.slice(0, 8)}${lineage})`); + log("info", `[prefix-affinity] new anonymous session ${anonAffinity.sessionId} (depth=${anonAffinity.incomingDepth}, tail=${anonAffinity.tailHash.slice(0, 8)}${lineage}${provSuffix})`); + loggerLog("info", `[prefix-affinity] new session ${anonAffinity.sessionId} at depth ${anonAffinity.incomingDepth} (tail=${anonAffinity.tailHash.slice(0, 8)}${lineage}${provSuffix})`); // #2791: the fresh mint just orphaned an identified conversation. // Every per-item hash diverged (cross-wire re-serialization), so // the exact/simhash/fork ladders could not even SEE the overlap — @@ -1077,8 +1087,12 @@ async function handleRequest( // ONLY the client's own value (opencode x-session-affinity, codex // body.session_id), so a user can tell at a glance which client // owns a session. Pi sends nothing: prefix-affinity-resolved - // sessions get the "prefix-affinity" label, others stay empty - // (shown as "—" in the UI). + // sessions get the "prefix-affinity " label — suffixed with + // the MINT request's tail fragment (#2879) so N parallel anonymous + // ledgers stop sharing one identical label, the shape #2847 + // misread as twin files of a single session. First-write-wins keeps + // it stable for the session's life; pre-#2879 sessions keep the + // bare label (no migration). Others stay empty ("—" in the UI). const bodyIdentity = responsesIdentity ?? openaiIdentity ?? anthropicIdentity ?? googleIdentity; const affinity = affinityToken(bodyIdentity ?? { value: clientConv ?? conversation, @@ -1088,7 +1102,7 @@ async function handleRequest( const clientLabel = bodyIdentity?.clientProvided ? bodyIdentity.value : clientConversationHeader(req.headers); - const session = getSession(sessionId, { protocol, upstreamOrigin, label: clientLabel ?? (anonAffinity ? "prefix-affinity" : undefined) }); + const session = getSession(sessionId, { protocol, upstreamOrigin, label: clientLabel ?? (anonAffinity ? `prefix-affinity ${anonAffinity.tailHash.slice(0, 8)}` : undefined) }); if (designNamespaced && session.metadata.personaNamespace !== true) session.metadata.personaNamespace = true; let publicForkPrefix = false; if (!countTokens && !responsesCompact && session.metadata.publicForkReceipt !== undefined) { @@ -1126,11 +1140,15 @@ async function handleRequest( if (anonAffinity) { prefixAffinity.note(sessionId, anonAffinity.incomingDepth, anonAffinity.tailHash, anonAffinity.itemHashes, false, anonAffinity.sketches, anonAffinity.userFlags, undefined, anonAffinity.tailFragments); scheduleAffinityPersist(); + // The rest of this stamp is latest-wins; `producer` is NOT — the + // mint request's fingerprint must survive every later turn (#2879). + const existingProducer = (session.metadata.anonymousPrefixAffinity as { producer?: AnonymousProvenance } | undefined)?.producer; session.metadata.anonymousPrefixAffinity = { depth: anonAffinity.incomingDepth, tailHash: anonAffinity.tailHash, via: anonAffinity.via, ...(anonAffinity.lineage ? { lineage: anonAffinity.lineage } : {}), + ...(existingProducer ? { producer: existingProducer } : anonProvenance ? { producer: anonProvenance } : {}), }; } else if (clientProvided) { // #1486: track identified clients' chains too, so a resume that diff --git a/src/session.ts b/src/session.ts index 59c75882e3..2c9866171d 100644 --- a/src/session.ts +++ b/src/session.ts @@ -4,6 +4,7 @@ import { log as loggerLog } from "./logger.js"; import { getStore } from "./persist.js"; import { maxSessions as knobMaxSessions } from "./knobs.js"; import { durableMessageToolPaths, withDurableProtection } from "./durable-message-guards.js"; +import type { AnonymousProvenance } from "./anon-provenance.js"; import type { WireProtocol } from "./util.js"; export type BlockView = { text: string; count: number }; @@ -413,6 +414,8 @@ type AnonymousPrefixAffinityStamp = { tailHash: string; via: "prefix" | "new"; lineage?: { parents: string[]; reason: "truncated" | "forked"; sharedPrefix?: number }; + /** #2879: first-writer provenance of the mint request (see anon-provenance.ts). */ + producer?: AnonymousProvenance; }; // #833: wire paths resolve the kernel Config per request (global → provider → diff --git a/tests/issue2879-anon-provenance.test.ts b/tests/issue2879-anon-provenance.test.ts new file mode 100644 index 0000000000..28abe5b80a --- /dev/null +++ b/tests/issue2879-anon-provenance.test.ts @@ -0,0 +1,208 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +// #2879: pfa-* anonymous ledgers were unattributable after the fact — hashed +// file names, ONE shared "prefix-affinity" label for every ledger, and a mint +// log line with no client fingerprint (#2847 misread four parallel ledgers as +// twin files of one session). Pinned here with genuine anonymous clients +// (no identity headers — prefix affinity mints pfa-* sessions, same hermetic +// shape as tests/issue2033-anonymous-anchor.test.ts): +// A. the MINT request's fingerprint lands on metadata.anonymousPrefixAffinity +// .producer (ua / header names / first-user-message preview / remote) and +// the ledger gets a UNIQUE "prefix-affinity " label; +// B. two distinct anonymous conversations mint two sessions with DISTINCT +// labels (the shared-label misread is gone); +// C. producer is first-writer-wins — later turns with a different UA/headers +// never clobber the mint fingerprint; +// D. an x-bili-plugin marker WITHOUT a usable conversation identity records +// pluginAgent — the host-extension stamp-failure signature. + +const hermeticRoot = fs.mkdtempSync(path.join(os.tmpdir(), "bc-2879-anonprov-")); +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = hermeticRoot; +test.after(() => fs.rmSync(hermeticRoot, { recursive: true, force: true })); + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { listSessions, _resetSessionsForTest, type Session } from "../src/session.ts"; +import { setLogCapture } from "../src/logger.ts"; + +const WINDOW = 120_000; +type Msg = { role: string; content: unknown }; + +function makeRelay() { + const server = http.createServer((_req, res) => { + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + res.write(`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "ok" } }] })}\n\n`); + res.write(`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] })}\n\n`); + res.write("data: [DONE]\n\n"); + res.end(); + }); + return server; +} + +async function startHarness() { + const logs: string[] = []; + setLogCapture((level, msg) => { logs.push(`${level} ${msg}`); }); + _resetSessionsForTest(); + const relay = makeRelay(); + relay.listen(0, "127.0.0.1"); + await once(relay, "listening"); + const upstreamPort = (relay.address() as { port: number }).port; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: WINDOW } } } }, + modelContextLimit: WINDOW, + kernelConfig: defaultConfig(WINDOW), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: true, + debug: false, + passthrough: false, + compat: { roles: {} }, streamErrorShape: "protocol", passthroughSource: null, autoRestartOnUpdate: false, updateTag: "latest", advisoryCheck: false, releaseNotesCheck: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); + await once(proxy, "listening"); + const url = `http://127.0.0.1:${(proxy.address() as { port: number }).port}/bili/http://127.0.0.1:${upstreamPort}/v1/chat/completions`; + // NO identity headers by default — genuine anonymous client (#309 lane). + const post = (messages: Msg[], extraHeaders: Record = {}) => fetch(url, { + method: "POST", + headers: { "content-type": "application/json", "user-agent": "bc-test-client/1.0", ...extraHeaders }, + body: JSON.stringify({ model: "gpt-test", max_tokens: 1024, stream: true, messages }), + }); + const anonSessions = (): Session[] => listSessions().filter((s) => s.id.startsWith("pfa-") && s.metadata.anonymousPrefixAffinity); + const close = async () => { + await new Promise((r) => proxy.close(() => r())); + await new Promise((r) => relay.close(() => r())); + }; + return { logs, post, anonSessions, close }; +} + +test("#2879 A: mint captures producer provenance + unique label (e2e)", async () => { + const h = await startHarness(); + try { + const base: Msg[] = [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "PROVENANCE SENTINEL ALPHA: refactor the billing module for issue 2879" }, + { role: "assistant", content: "Understood, starting the refactor." }, + ]; + const r1 = await h.post(base, { "x-test-marker": "v1" }); + assert.equal(r1.status, 200, "anonymous turn succeeds"); + await r1.text(); + const [s] = h.anonSessions(); + assert.ok(s, "a pfa-* session was minted"); + const stamp = s!.metadata.anonymousPrefixAffinity as { tailHash: string; producer?: Record }; + assert.ok(stamp.producer, "producer provenance stamped at mint"); + assert.equal(stamp.producer.ua, "bc-test-client/1.0"); + const headerNames = stamp.producer.headers as string[]; + for (const expected of ["user-agent", "content-type", "x-test-marker"]) { + assert.ok(headerNames.includes(expected), `header name list carries ${expected} (${headerNames.join(",")})`); + } + assert.equal(typeof stamp.producer.firstMsg, "string"); + assert.ok(String(stamp.producer.firstMsg).startsWith("PROVENANCE SENTINEL ALPHA"), `first user message preview captured (${stamp.producer.firstMsg})`); + assert.equal(typeof stamp.producer.remote, "string"); + assert.ok((stamp.producer.remote as string).length > 0); + assert.ok(typeof stamp.producer.at === "number"); + assert.equal(stamp.producer.pluginAgent, undefined, "no plugin marker sent → no pluginAgent"); + // The shared-label misread fix: unique per-ledger label, stable tail fragment. + assert.equal(s!.meta.label, `prefix-affinity ${stamp.tailHash.slice(0, 8)}`, "label is the unique prefix-affinity form"); + const mintLog = h.logs.find((l) => l.includes(`new anonymous session ${s!.id}`)); + assert.ok(mintLog, "mint log line present"); + assert.ok(mintLog!.includes('ua="bc-test-client/1.0"'), `mint log carries ua (${mintLog})`); + assert.ok(mintLog!.includes('plugin=-'), "mint log carries plugin slot (no marker sent)"); + assert.ok(mintLog!.includes("PROVENANCE SENTINEL ALPHA"), "mint log carries first-message preview"); + } finally { + await h.close(); + } +}); + +test("#2879 B: distinct anonymous conversations get distinct labels (e2e)", async () => { + const h = await startHarness(); + try { + const convA: Msg[] = [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "SENTINEL BRAVO: first conversation, analyze the network traces" }, + ]; + const convB: Msg[] = [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "SENTINEL CHARLIE: second conversation, draft the release notes" }, + ]; + const rA = await h.post(convA); + assert.equal(rA.status, 200); + await rA.text(); + const rB = await h.post(convB); + assert.equal(rB.status, 200); + await rB.text(); + const sessions = h.anonSessions(); + assert.equal(sessions.length, 2, `two distinct pfa-* sessions minted (got ${sessions.length})`); + const labels = sessions.map((s) => s.meta.label); + assert.notEqual(labels[0], labels[1], `labels differ (${labels.join(" | ")})`); + for (const label of labels) assert.ok(label?.startsWith("prefix-affinity "), `label keeps the family prefix (${label})`); + const previews = sessions.map((s) => (s.metadata.anonymousPrefixAffinity as { producer?: { firstMsg?: string } }).producer?.firstMsg ?? ""); + assert.ok(previews.some((p) => p.startsWith("SENTINEL BRAVO")), "conv-A preview attributed to its own ledger"); + assert.ok(previews.some((p) => p.startsWith("SENTINEL CHARLIE")), "conv-B preview attributed to its own ledger"); + } finally { + await h.close(); + } +}); + +test("#2879 C: producer is first-writer-wins across turns (e2e)", async () => { + const h = await startHarness(); + try { + const base: Msg[] = [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "SENTINEL DELTA: long running task, step one" }, + { role: "assistant", content: "Step one done." }, + ]; + const r1 = await h.post(base, { "x-test-marker": "v1" }); + assert.equal(r1.status, 200); + await r1.text(); + const turn2: Msg[] = [...base, { role: "user", content: "step two?" }]; + const r2 = await h.post(turn2, { "user-agent": "bc-other-client/9.9", "x-later-header": "late" }); + assert.equal(r2.status, 200); + await r2.text(); + const [s] = h.anonSessions(); + const producer = (s!.metadata.anonymousPrefixAffinity as { producer?: Record }).producer!; + assert.equal(producer.ua, "bc-test-client/1.0", "mint UA survives a later turn with a different UA"); + const headerNames = producer.headers as string[]; + assert.ok(headerNames.includes("x-test-marker"), "mint header list preserved"); + assert.ok(!headerNames.includes("x-later-header"), "later-turn headers do not leak into the mint fingerprint"); + assert.ok(String(producer.firstMsg).startsWith("SENTINEL DELTA"), "first-message preview stays the mint conversation's"); + } finally { + await h.close(); + } +}); + +test("#2879 D: x-bili-plugin marker without conversation identity records pluginAgent (e2e)", async () => { + const h = await startHarness(); + try { + const msgs: Msg[] = [ + { role: "system", content: "You are a helpful assistant." }, + { role: "user", content: "SENTINEL ECHO: extension loaded but stamp failed" }, + ]; + const r1 = await h.post(msgs, { "x-bili-plugin": "pi" }); + assert.equal(r1.status, 200); + await r1.text(); + const [s] = h.anonSessions(); + assert.ok(s, "anonymous resolution still applies (marker alone is not a conversation identity)"); + const producer = (s!.metadata.anonymousPrefixAffinity as { producer?: Record }).producer!; + assert.equal(producer.pluginAgent, "pi", "plugin lane recorded — the stamp-failure signature"); + } finally { + await h.close(); + } +});