Skip to content
Merged
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
98 changes: 98 additions & 0 deletions src/anon-provenance.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>, 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<string, unknown>;
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<string, unknown> => 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<string, unknown>;
// 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<string, unknown>,
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(),
};
}
28 changes: 23 additions & 5 deletions src/server/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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) {
Expand All @@ -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 —
Expand Down Expand Up @@ -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 <tail8>" 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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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 →
Expand Down
Loading
Loading