From 8982a2fa5f79cac9590967825c06d69943ab60fb Mon Sep 17 00:00:00 2001 From: YZSQ <187964595+yingzhongshuoqi@users.noreply.github.com> Date: Sun, 4 Oct 2026 02:02:30 +0800 Subject: [PATCH 01/10] feat: expose isolated plugin forks and attributed context observations --- PLUGIN.md | 59 +- src/cache-ledger.ts | 34 + src/fork-adoption.ts | 2 +- src/persist.ts | 15 +- src/plugin.ts | 342 +++++++++- src/server.ts | 46 +- src/session.ts | 19 +- .../wire-contract/manifest-default.json | 9 +- .../golden/wire-contract/manifest-optin.json | 9 +- tests/plugin-fork.test.ts | 591 ++++++++++++++++++ tests/plugin-status-provenance.test.ts | 227 +++++++ tests/tool-ring.test.ts | 8 +- 12 files changed, 1316 insertions(+), 45 deletions(-) create mode 100644 tests/plugin-fork.test.ts create mode 100644 tests/plugin-status-provenance.test.ts diff --git a/PLUGIN.md b/PLUGIN.md index e1f782e959..d63d16ef4b 100644 --- a/PLUGIN.md +++ b/PLUGIN.md @@ -46,7 +46,10 @@ Fetch once at plugin startup. "instructionsMutable": "x-bili-plugin-instructions-mutable" }, "toolEndpoint": "/__bili/plugin/tool", - "statusEndpoint": "/__bili/plugin/status" + "statusEndpoint": "/__bili/plugin/status", + "capabilities": { + "fork": { "protocolVersion": 1, "endpoint": "/__bili/plugin/fork", "snapshotEndpoint": "/__bili/plugin/snapshot" } + } } ``` @@ -91,7 +94,8 @@ Return `result` verbatim as the native tool result content. Notes: - Execution happens **under the session lock**, against the same view the model was shown on the last request (refs match what the model sees). -- `compress` mutates state; `decompress` / `search_context` / `acp_status` are read-only. +- Optional `expectedRevision` is the opaque `parentRevision` from a snapshot. It requires an explicit `conversationId` and is checked under the same session lock immediately before execution. A stale revision returns `409 PARENT_REVISION_CONFLICT` without executing the tool. This also avoids witness routing to another conversation. Existing callers omitting it retain their routing and execution behavior. +- `compress` mutates state. Inline `decompress` marks a block restored for re-folding; range restores queue content for the next request. Successful restores also update the estimated context footprint. `search_context` / `acp_status` are read-only. - Errors: `400` invalid JSON / missing `conversationId` / unknown tool, `404` unknown conversation (no model request has arrived with that conversation id yet), `500` execution failure. A **known but disabled** opt-in tool (`absorb` / `acp_rule`) is not an error: it answers `200` with `ok: true` and a `result` explaining that the feature is off on this proxy (#1192). ### 4. `GET /__bili/plugin/status?conversationId=` @@ -106,6 +110,11 @@ Context-level visibility for plugin UIs (status bars / slash commands): "pluginAgent": "pi", "contextLimit": 200000, "contextTokens": 138211, + "contextTokensSource": "usage", + "contextTokensAt": 1755300000000, + "contextGeneration": "opaque-generation", + "sessionRevision": "opaque-revision", + "model": "claude-sonnet-4", "inputTokens": 251000, "outputTokens": 40021, "cachedTokens": 180000, @@ -115,7 +124,11 @@ Context-level visibility for plugin UIs (status bars / slash commands): } ``` -`contextTokens` is the last reported context size (input + cache-read) — the same value the nudge decision reads. Errors: `400` missing `conversationId`, `404` unknown conversation. +`contextTokens` is the effective current input footprint, not accumulated billing. `contextTokensSource` is `usage` only for a real positive upstream input measurement with no outstanding fold credit, `estimate` for a local projection, or `unavailable` with `contextTokens: null`. Manual `compress` subtracts only its new credit from the previous observation, once; successful `decompress` adds the returned content (or file pointer) and any newly queued range content. Both publish an independent `estimate` observation without replacing the last-usage calibration baseline or changing cumulative `inputTokens` / `cachedTokens`. Never add those billing fields to infer effective context. + +Forwarding a new model request replaces the previous observation with an estimate until a real positive usage report arrives. That report restores `usage`; zero/missing usage cannot revive an older measured generation. A real report netted against outstanding fold credit remains an estimate of the effective view, not a measurement of that view. Forks start with independent estimates, not inherited parent usage. + +`contextTokensAt` is the observation time in Unix milliseconds (not the status read time), independently of the last usage timestamp. `contextGeneration` is an opaque change detector covering a unique observation id, context, revision, model and limit: successive observations remain distinct even at the same timestamp with identical token counts; repeated status reads do not create observations. Unavailable context has null tokens, timestamp and generation. Legacy persisted sessions without an independently attributed observation are unavailable until another request establishes one. `sessionId` and nullable `sessionRevision` identify the resolved session and its fork state. Pre-first-request/chain-only responses explicitly report unavailable context. Persisted sessions are loaded by the requested id before considering an explicitly requested `fallback=latest`. Errors: `400` missing `conversationId`, `404` unknown conversation. ### 5. `POST /__bili/plugin/compact` @@ -141,6 +154,46 @@ The `/bili/` prefix is absent in MITM mode, so URL-based detection cannot work. No special handling is needed for decompression: `decompress` results come back through the same endpoint. +### 8. Public snapshot and fork (protocol 1) + +Discover `capabilities.fork` from the manifest; do not infer support from the package version. No new configuration is required. These endpoints use the same admin gate and request-body limit as the tool API. + +`GET /__bili/plugin/snapshot?conversationId=` returns: + +```json +{ + "ok": true, "protocolVersion": 1, "status": "exact", + "conversationId": "parent", "sessionId": "parent", + "parentRevision": "", + "orderHash": "", + "orderedMessages": [{ "rawId": "h_example", "ref": "m00001", "identityHash": "" }], + "messages": [{ "rawId": "h_example", "ref": "m00001", "role": "user", "text": "original text", "contentType": "text" }] +} +``` + +`messages` is in exactly the same order as `orderedMessages`, describes the raw `pluginSnapshot` rather than a folded view, and includes optional `text`, `toolName`, and `toolCallId`. Compare original text and role to establish the longest matching prefix; tool identities/content type must match too. Copy the corresponding ordered identities verbatim; `identityHash` and revisions are opaque and clients do not need a content-hash algorithm. Multimodal/opaque content that cannot be verified by this text projection returns `409`, `status: unavailable`, `code: SNAPSHOT_UNAVAILABLE`, not a guessed text match. Missing raw snapshots or inconsistent raw/ref/CCR mappings likewise fail closed. + +`POST /__bili/plugin/fork` body: + +```json +{ + "protocolVersion": 1, + "parentConversationId": "parent", "childConversationId": "new-child", + "parentRevision": "", + "branchPoint": { "messageCount": 1, "orderHash": "" }, + "orderedMessages": [{ "rawId": "h_example", "ref": "m00001", "identityHash": "" }], + "idempotencyKey": "stable-operation-id" +} +``` + +The branch is an ordered prefix, including an empty prefix. Compute its existing order hash as lowercase SHA-256 of UTF-8 `JSON.stringify(orderedMessages)` with each identity object's keys in order `rawId`, `ref`, `identityHash`; for a full prefix reuse snapshot `orderHash`. Do not hash descriptors or folded text. Extra child messages, including a final model reply that has not left the proxy yet, may be appended on the child's first model request after copying the matched prefix. + +New success is `201`; exact payload replay is `200` with `replayed: true`, even after restart or later parent changes. Both return `status`, `parentRevision`, `childRevision`, `sessionId`, `branchPoint`, `inheritedBlocks`, and `expandedBlocks`. Reusing the child id with another parent, key or payload returns `409 CHILD_CONFLICT`. Invalid shapes/version/identifiers return `400 INVALID_REQUEST`; absent parent returns `404 PARENT_NOT_FOUND`; stale parent returns `409 PARENT_REVISION_CONFLICT`; altered order/raw/ref/identity returns `409 BRANCH_POINT_CONFLICT`. Missing nested originals return `409 PARENT_STATE_INCOMPLETE` with `status: unavailable`. Publication/persistence failure returns `503 FORK_FAILED` with `status: unavailable` and exposes no child; the same request may be retried. + +Boundary handling is `exact` when complete blocks fit inside the prefix, `expanded` when a block crosses it (crossing summaries are omitted, affected nested descendants are expanded, matched originals are retained), and `unavailable` when safe reconstruction is impossible. Blocks, nested caches, raw messages, issued ref namespace, token snapshots, rules and prefix CCR payloads are independent copies. The child never reads its parent's later state as a fallback. Registration and first-request resume heuristics do not replace its copied state. With existing persistence enabled, raw snapshots, receipts, nested originals and the child's CCR payloads survive restart; with persistence disabled, durability is not promised. Atomic child publication/idempotency covers concurrent requests to one proxy process; concurrent writers sharing one state directory across processes are not supported by this protocol. + +中文集成要点:协议仍为 1;先读取 manifest 能力及 snapshot,按原文、role、工具身份确认匹配前缀,仅提交该前缀的 orderedMessages,不猜测多模态分支点。父 revision 变化必须重新取快照并重新匹配。子会话使用独立稳定 id,首次请求可追加尚未出站的模型回复,不能重置继承的 refs/blocks。手动摘要使用 snapshot 的 refs 和 `expectedRevision: snapshot.parentRevision` 调用公开 tool;摘要期间历史发生变化时返回 409,不执行旧摘要。状态栏读取 `contextTokens`、`contextTokensSource`、`contextTokensAt`、`model`、`contextLimit`、`contextGeneration`、`sessionId` 与 `sessionRevision`,不得用累计账单 input/cache 冒充有效上下文。手动 compress/decompress 后为独立时间及 generation 的 estimate,不改累计账单或 last-usage 基线;下一次正数 usage 才恢复 usage,零值或缺失 usage 不得复活旧观测。未知来源返回 unavailable,tokens/time/generation 为 null。 + ## Security - The tool endpoints sit behind the `/__bili/` admin gate: loopback-only + trusted-origin. A plugin on the same machine as the proxy can call them; nothing else can. diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 52407a1d7f..62310c928e 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -204,6 +204,33 @@ const seamLastSent = new WeakMap(); const seamLastSettled = new WeakMap(); const lastClientAbort = new WeakMap(); +export interface ContextObservation { + sessionId: string; + tokens: number; + source: "usage" | "estimate"; + at: number; + generation: string; +} + +/** Public context provenance is separate from billing and nudge baselines. */ +export function recordContextObservation(session: Session, tokens: number, source: ContextObservation["source"]): void { + if (!Number.isFinite(tokens) || tokens < 0) { + delete session.metadata.publicContextObservation; + return; + } + session.metadata.publicContextObservation = { sessionId: session.id, tokens, source, at: Date.now(), generation: randomUUID() } satisfies ContextObservation; +} + +export function currentContextObservation(session: Session): ContextObservation | undefined { + const raw = session.metadata.publicContextObservation; + if (!raw || typeof raw !== "object") return undefined; + const o = raw as Record; + if (o.sessionId !== session.id || typeof o.tokens !== "number" || !Number.isFinite(o.tokens) || o.tokens < 0 || + (o.source !== "usage" && o.source !== "estimate") || typeof o.at !== "number" || !Number.isFinite(o.at) || o.at < 0 || + typeof o.generation !== "string" || o.generation.length === 0) return undefined; + return { sessionId: session.id, tokens: o.tokens, source: o.source, at: o.at, generation: o.generation }; +} + /** #1592 follow-up: stamp the wall-clock time of a client mid-stream abort * (wired at both forward abort chokepoints). The next settle in the same * session reads it to mark abort-correlated samples. */ @@ -216,6 +243,9 @@ export function noteClientAbort(session: Session): void { * the next settleUsageReport pairs it with the usage report it produced. */ export function noteForwardedBody(session: Session, body: string): void { seamLastSent.set(session, body.length > SEAM_BODY_CAP ? body.slice(0, SEAM_BODY_CAP) : body); + // prepare may reuse a measured baseline; this new payload is not measured yet. + if (session.stats.contextTokensSource !== undefined) recordContextObservation(session, session.stats.contextTokens, "estimate"); + else delete session.metadata.publicContextObservation; } // #1843 L1: learned per-route image cost. The prior (pixel tile model or bytes) @@ -676,6 +706,10 @@ export function settleUsageReport( // compaction boundaries via resetSessionCompression (session.ts). session.stats.lastUsageGradeTokens = session.stats.lastInputTokens; session.stats.lastInputTokensSource = "usage"; + session.stats.contextTokens = session.stats.lastInputTokens; + session.stats.contextTokensSource = "usage"; + if (session.metadata) session.metadata.contextTokensAt = Date.now(); + recordContextObservation(session, session.stats.lastInputTokens, (session.stats.compressCreditTokens ?? 0) > 0 ? "estimate" : "usage"); // #1933 F2: record which route measured this baseline — the gate uses // it to demote the baseline when the current request routes elsewhere. const settleOrigin = normalizeUpstreamOrigin(s.upstream); diff --git a/src/fork-adoption.ts b/src/fork-adoption.ts index ad1b2114d2..b33e9c093e 100644 --- a/src/fork-adoption.ts +++ b/src/fork-adoption.ts @@ -108,7 +108,7 @@ export interface ForkAdoptionPlan { * in-place marking (prepareResponses's `__bili_inplace_sysdev`) is * deliberately NOT replicated: marked or hoisted, those items contribute * no core message either way, so the id set is identical. */ -function incomingCoreMessages(protocol: WireProtocol, parsed: unknown): CoreMessage[] | null { +export function incomingCoreMessages(protocol: WireProtocol, parsed: unknown): CoreMessage[] | null { if (!SUPPORTED.has(protocol)) return null; const clone = structuredClone(parsed) as Record; if (protocol === "responses") { diff --git a/src/persist.ts b/src/persist.ts index 00bccd80db..fb2e87d4d7 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -154,6 +154,8 @@ interface PersistedSession { * absent on records written before #401, whose `messages` held the raw * history and must still be pruned at export time. */ messagesFolded?: boolean; + pluginSnapshot?: CoreMessage[]; + forkContentStore?: MessageContentStore; } type Logger = (level: "info" | "warn" | "error", msg: string) => void; @@ -170,6 +172,10 @@ function mergeState(parsed: CompressionState): CompressionState { nextBlockId: parsed.nextBlockId ?? fresh.nextBlockId, nextRunId: parsed.nextRunId ?? fresh.nextRunId, tokenSnapshot: parsed.tokenSnapshot ?? fresh.tokenSnapshot, + lastPassIds: parsed.lastPassIds ?? fresh.lastPassIds, + hiddenOrphanRefs: parsed.hiddenOrphanRefs ?? fresh.hiddenOrphanRefs, + terminalStreak: parsed.terminalStreak ?? fresh.terminalStreak, + nextRuleId: parsed.nextRuleId ?? fresh.nextRuleId, // Without this, a restart re-exposes absorbed tool outputs: state // resurrects with absorbed=[] and hideAbsorbedMessages has nothing to hide. absorbed: parsed.absorbed ?? fresh.absorbed, @@ -677,10 +683,15 @@ function buildRecord(session: Session): PersistedSession { stats: { ...session.stats }, messages: snapshot, messagesFolded: snapshot ? true : undefined, + pluginSnapshot: session.pluginSnapshot, + forkContentStore: session.metadata.publicForkReceipt ? session.contentStore : undefined, // Per-session provenance: record the bili build that wrote this file so the // web UI can show which version last touched the session; pre-stamp files // load without the key and render an honest dash. - metadata: { ...session.metadata, biliVersion: VERSION }, + metadata: { ...session.metadata, ...(session.pluginSnapshot && session.contentStore ? { publicSnapshotStoredRefs: session.pluginSnapshot.flatMap((m) => { + const ref = session.state.messageRefs.byRaw[m.id]; + return ref && session.contentStore!.byRef[ref] ? [ref] : []; + }) } : {}), biliVersion: VERSION }, state: session.state, blockContents: Object.fromEntries(session.blockContents), createdAt: session.createdAt, @@ -777,6 +788,8 @@ function buildSession(parsed: PersistedSession): Session { blockContents, lastMessages: Array.isArray(parsed.messages) ? parsed.messages : undefined, lastMessagesFolded: parsed.messagesFolded === true, + pluginSnapshot: Array.isArray(parsed.pluginSnapshot) ? parsed.pluginSnapshot : undefined, + contentStore: parsed.forkContentStore, inFlight: 0, persisted: true, pendingRetrievals: [], diff --git a/src/plugin.ts b/src/plugin.ts index 474cbf3130..3672890b44 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,10 +1,13 @@ -import { type CompressionCore, type Config, type CoreMessage, type NudgeDecision, countMessageTokens } from "acp-kernel"; +import { type CompressionCore, type Config, type CoreMessage, type NudgeDecision, countMessageTokens, parseStoredPlaceholder, prune } from "acp-kernel"; import { buildStatusPanel } from "acp-kernel/panel"; import { fileURLToPath } from "node:url"; import type { ServerResponse } from "node:http"; import fs from "node:fs"; import path from "node:path"; -import { acquireInFlight, diagnoseSuccessWithoutUsage, effectiveConfig, findSessionByCanonicalId, listSessions, markCompactionBoundary, markDirty, peekSession, releaseInFlight, withSessionLock, type Session } from "./session.js"; +import { createHash } from "node:crypto"; +import { getStore } from "./persist.js"; +import { cloneStoreForRefs } from "./store.js"; +import { acquireInFlight, createSession, getSession, publishForkSession, diagnoseSuccessWithoutUsage, effectiveConfig, findSessionByCanonicalId, listSessions, markCompactionBoundary, markDirty, peekSession, releaseInFlight, withSessionLock, type Session } from "./session.js"; import { clientConversationHeader } from "./session-id.js"; import { ABSORB_TOOL_NAME, BILI_ACP_TOOLS_ANTHROPIC, BILI_ACP_TOOLS_ANTHROPIC_NO_RANGE, BILI_ACP_TOOLS_OPENAI, BILI_ACP_TOOLS_OPENAI_NO_RANGE, BILI_ACP_TOOLS_RESPONSES_NO_RANGE, PROXY_TOOL_NAMES, RETRIEVE_TOOL_NAME, RULE_TOOL, RULE_TOOL_NAME, RULE_TOOL_OPENAI, RULE_TOOL_RESPONSES, SEARCH_CONTEXT_TOOL_NAME, absorbToolsFor, retrieveToolsFor } from "./compress-tool.js"; import { absorbEnabled, effectiveAbsorbConfig, isProxyToolFor } from "./absorb.js"; @@ -21,11 +24,12 @@ import { PANEL_BOX_FOOTER } from "./acp-panel.js"; import { describeAdvisory, getAdvisoryState } from "./advisory.js"; import { describeUpdateReady, getUpdateVisibility } from "./update-notes.js"; import { warnCacheCollapse } from "./cache-warn.js"; -import { settleUsageReport } from "./cache-ledger.js"; +import { currentContextObservation, recordContextObservation, settleUsageReport } from "./cache-ledger.js"; import { promptInputTotal, type WireProtocol } from "./util.js"; import { stateDir } from "./paths.js"; import { lookupToolWitness, recordToolWitness } from "./tool-ring.js"; import { awaitDrain } from "./server/stream-io.js"; +import { incomingCoreMessages } from "./fork-adoption.js"; // The proxy's own version, read from package.json at runtime (works in both dev // via tsx and bundled via tsup). Shown in the /acp panel header, aligned with @@ -301,7 +305,7 @@ export function recordPluginSession(conversationId: string, sessionId: string): /** Keep the last prepare()'s view for a plugin session so tool-API execution * sees the exact refs the model was shown (mirrors the wire-mode loop, which * runs executeProxyTool against prepared.processedMessages). */ -export function rememberPluginMessages(sessionId: string, processed: CoreMessage[], original: CoreMessage[], nudge?: NudgeDecision): void { +export function rememberPluginMessages(sessionId: string, processed: CoreMessage[], original: CoreMessage[], nudge?: NudgeDecision, rawWire?: Buffer): void { // #1307: auxiliary requests (auto-review / classifier prompts) bound to the // same session key can carry a normal output budget and any message count, // so both the ≤200 heuristic and size-based guards are proxies that a new @@ -329,6 +333,19 @@ export function rememberPluginMessages(sessionId: string, processed: CoreMessage ); for (const id of staleSessionIds) remembered.delete(id); remembered.set(sessionId, { processed, original, nudge }); + const session = peekSession(sessionId); + if (session && typeof session.metadata.pluginAgent === "string" && original.length > 0) { + session.pluginSnapshot = structuredClone(original); + if (rawWire !== undefined) { + try { + const wire = JSON.parse(rawWire.toString("utf8")) as Record; + session.metadata.publicSnapshotTextComparable = comparableHistory(wire.messages ?? wire.input ?? wire.contents); + } catch { + session.metadata.publicSnapshotTextComparable = false; + } + } + markDirty(session); + } } // Launcher mode (#162): hosts that cannot attach per-request headers @@ -636,6 +653,7 @@ export function handlePluginManifest(res: import("node:http").ServerResponse, co toolEndpoint: "/__bili/plugin/tool", statusEndpoint: "/__bili/plugin/status", runtimeInfoEndpoint: "/__bili/plugin/runtime-info", + capabilities: { fork: { protocolVersion: 1, endpoint: "/__bili/plugin/fork", snapshotEndpoint: "/__bili/plugin/snapshot" } }, })); } @@ -682,6 +700,239 @@ export function resolveConversation(conversationId: string): { session: Session return { session, entry }; } +type ForkIdentity = { rawId: string; ref: string; identityHash: string }; + +// Only inspect message content, not arbitrary tool arguments containing a `type` key. +function comparableHistory(value: unknown): boolean { + if (value === null || typeof value === "string") return true; + if (Array.isArray(value)) return value.every(comparableHistory); + if (!value || typeof value !== "object") return false; + const part = value as Record; + if (part.type !== undefined && !["message", "text", "input_text", "output_text", "tool_use", "tool_result", "function_call", "function_call_output"].includes(String(part.type))) return false; + if (part.type === "tool_use" || part.type === "function_call") return true; + if (part.content !== undefined) return comparableHistory(part.content); + if (part.output !== undefined) return comparableHistory(part.output); + if (part.parts !== undefined) return comparableHistory(part.parts); + return typeof part.text === "string" || Array.isArray(part.tool_calls); +} +type ForkRequest = { + protocolVersion: 1; + parentConversationId: string; + childConversationId: string; + parentRevision: string; + branchPoint: { messageCount: number; orderHash: string }; + orderedMessages: ForkIdentity[]; + idempotencyKey: string; +}; + +function stableJson(value: unknown): string { + if (Array.isArray(value)) return `[${value.map(stableJson).join(",")}]`; + if (value !== null && typeof value === "object") { + const obj = value as Record; + return `{${Object.keys(obj).filter((k) => obj[k] !== undefined).sort().map((k) => `${JSON.stringify(k)}:${stableJson(obj[k])}`).join(",")}}`; + } + return JSON.stringify(value) ?? "null"; +} + +function forkHash(value: unknown): string { + return createHash("sha256").update(stableJson(value), "utf8").digest("hex"); +} + +function forkOrderHash(messages: ForkIdentity[]): string { + return createHash("sha256").update(JSON.stringify(messages), "utf8").digest("hex"); +} + +function forkToolIsError(message: CoreMessage): boolean { + return (message as CoreMessage & { toolIsError?: boolean }).toolIsError === true; +} + +function forkMessageIdentityHash(message: CoreMessage): string { + return forkHash([message.role, message.contentType, message.text ?? null, message.toolName ?? null, message.toolCallId ?? null, message.thinkingTokens ?? null, message.summaryOfBlockId ?? null, forkToolIsError(message)]); +} + +/** Read under the session lock; compare ordered semantics, never just an id set. */ +export function publicForkInputMatches(session: Session, protocol: WireProtocol, parsed: unknown): boolean { + const prefix = session.pluginSnapshot; + if (session.metadata.publicForkReceipt === undefined || !prefix) return false; + try { + const incoming = incomingCoreMessages(protocol, parsed); + return incoming !== null && incoming.length >= prefix.length && prefix.every((message, index) => { + const candidate = incoming[index]!; + const ref = session.state.messageRefs.byRaw[message.id]; + return ref !== undefined && session.state.messageRefs.byRef[ref] === message.id + && candidate.id === message.id && forkMessageIdentityHash(candidate) === forkMessageIdentityHash(message); + }); + } catch { + return false; + } +} + +function forkSnapshot(session: Session) { + const messages = session.pluginSnapshot; + if (!messages) throw new Error("raw snapshot unavailable; send a fresh plugin model request"); + if (session.metadata.publicSnapshotTextComparable === false || messages.some((m) => (m.thinkingTokens ?? 0) > 0)) throw new Error("multimodal or opaque content cannot be compared by text"); + const store = contentStoreOf(session); + const expectedStoredRefs = session.metadata.publicSnapshotStoredRefs; + if (Array.isArray(expectedStoredRefs) && expectedStoredRefs.some((ref) => typeof ref !== "string" || !store.byRef[ref])) throw new Error("CCR original index unavailable"); + const orderedMessages = messages.map((m): ForkIdentity => { + const ref = session.state.messageRefs.byRaw[m.id]; + if (!ref || session.state.messageRefs.byRef[ref] !== m.id) throw new Error("raw/ref mapping inconsistent"); + const entry = store.byRef[ref]; + if ((entry && (entry.rawId !== m.id || typeof store.byHash[entry.hash] !== "string")) || (m.text && parseStoredPlaceholder(m.text)?.ref === ref && !entry)) throw new Error("CCR original unavailable or alias inconsistent"); + return { rawId: m.id, ref, identityHash: forkMessageIdentityHash(m) }; + }); + const state = { ...session.state, imageFullRestored: session.state.imageFullRestored ?? [], imageShrinks: session.state.imageShrinks ?? [] }; + const parentRevision = forkHash({ sessionId: session.id, messages, state, blockContents: Object.fromEntries(session.blockContents), contentStore: store }); + return { protocolVersion: 1, status: "exact", sessionId: session.id, parentRevision, orderHash: forkOrderHash(orderedMessages), orderedMessages, messages: messages.map((m, i) => ({ rawId: m.id, ref: orderedMessages[i]!.ref, role: m.role, text: m.text, toolName: m.toolName, toolCallId: m.toolCallId, contentType: m.contentType, toolIsError: forkToolIsError(m) })) }; +} + +function resolveForkConversation(id: string): Session | undefined { + if (peekSession(id)) return peekSession(id); + if (getStore().loadSync(id)) return getSession(id); + const { session, entry } = resolveConversation(id); + if (session) return session; + const persistedId = entry?.sessionId ?? id; + if (!getStore().loadSync(persistedId)) return undefined; + return getSession(persistedId); +} + +function forkReply(res: ServerResponse, status: number, body: unknown): void { + res.writeHead(status, { "content-type": "application/json" }); + res.end(JSON.stringify(body)); +} + +export async function handlePluginSnapshot(conversationId: string, res: ServerResponse): Promise { + if (!conversationId) return forkReply(res, 400, { ok: false, code: "INVALID_REQUEST", error: "conversationId is required" }); + const session = resolveForkConversation(conversationId); + if (!session) return forkReply(res, 404, { ok: false, code: "PARENT_NOT_FOUND", error: "unknown plugin conversation" }); + acquireInFlight(session); + try { + await withSessionLock(session, () => forkReply(res, 200, { ok: true, conversationId, ...forkSnapshot(session) })); + } catch (err) { + forkReply(res, 409, { ok: false, status: "unavailable", code: "SNAPSHOT_UNAVAILABLE", error: String(err) }); + } finally { + releaseInFlight(session); + } +} + +function parseForkRequest(payload: string): ForkRequest { + const value: unknown = JSON.parse(payload); + if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("expected fork object"); + const b = value as Record; + const identifier = (v: unknown): v is string => typeof v === "string" && v.length > 0 && v.length <= 256 && v.trim() === v && !/[\x00-\x1f\x7f]/.test(v); + const hash = (v: unknown): v is string => typeof v === "string" && /^[a-f0-9]{64}$/.test(v); + if (b.protocolVersion !== 1) throw new Error("unsupported fork protocolVersion (expected 1)"); + if (!identifier(b.parentConversationId) || !identifier(b.childConversationId) || !identifier(b.idempotencyKey) || !hash(b.parentRevision)) throw new Error("invalid fork identity/revision"); + if (b.parentConversationId === b.childConversationId) throw new Error("parent and child must differ"); + const point = b.branchPoint as Record | null; + if (!point || !Number.isSafeInteger(point.messageCount) || (point.messageCount as number) < 0 || !hash(point.orderHash) || !Array.isArray(b.orderedMessages) || b.orderedMessages.length !== point.messageCount) throw new Error("invalid branchPoint/orderedMessages"); + const orderedMessages = b.orderedMessages.map((v: unknown): ForkIdentity => { + if (!v || typeof v !== "object") throw new Error("invalid ordered identity"); + const item = v as Record; + if (!identifier(item.rawId) || typeof item.ref !== "string" || !/^m\d{5,}$/.test(item.ref) || !hash(item.identityHash)) throw new Error("invalid raw/ref identity"); + return { rawId: item.rawId, ref: item.ref, identityHash: item.identityHash }; + }); + return { protocolVersion: 1, parentConversationId: b.parentConversationId, childConversationId: b.childConversationId, parentRevision: b.parentRevision, branchPoint: { messageCount: point.messageCount as number, orderHash: point.orderHash }, orderedMessages, idempotencyKey: b.idempotencyKey }; +} + +export async function handlePluginFork(payload: string, res: ServerResponse): Promise { + let request: ForkRequest; + try { request = parseForkRequest(payload); } + catch (err) { return forkReply(res, 400, { ok: false, code: "INVALID_REQUEST", error: String(err) }); } + const requestHash = forkHash(request); + const replay = (): boolean => { + const existing = resolveForkConversation(request.childConversationId); + if (!existing) return false; + const receipt = existing.metadata.publicForkReceipt as { requestHash?: unknown; response?: unknown } | undefined; + if (receipt?.requestHash === requestHash) forkReply(res, 200, { ...(receipt.response as Record), replayed: true }); + else forkReply(res, 409, { ok: false, code: "CHILD_CONFLICT", error: "child conversation already exists or idempotency payload differs" }); + return true; + }; + if (replay()) return; + if (conversations.has(request.childConversationId) || registeredIds.has(request.childConversationId) || pendingRegisters.some((r) => r.conversationId === request.childConversationId)) return forkReply(res, 409, { ok: false, code: "CHILD_CONFLICT", error: "child conversation already registered" }); + const parent = resolveForkConversation(request.parentConversationId); + if (!parent) return forkReply(res, 404, { ok: false, code: "PARENT_NOT_FOUND", error: "parent conversation not found" }); + acquireInFlight(parent); + try { + await withSessionLock(parent, () => { + if (replay()) return; + if (conversations.has(request.childConversationId) || registeredIds.has(request.childConversationId) || pendingRegisters.some((r) => r.conversationId === request.childConversationId)) return forkReply(res, 409, { ok: false, code: "CHILD_CONFLICT", error: "child conversation already registered" }); + let snapshot: ReturnType; + try { snapshot = forkSnapshot(parent); } + catch (err) { return forkReply(res, 409, { ok: false, status: "unavailable", code: "SNAPSHOT_UNAVAILABLE", error: String(err) }); } + if (snapshot.parentRevision !== request.parentRevision) return forkReply(res, 409, { ok: false, code: "PARENT_REVISION_CONFLICT", error: "parent revision changed" }); + const prefix = snapshot.orderedMessages.slice(0, request.branchPoint.messageCount); + if (prefix.length !== request.branchPoint.messageCount || forkOrderHash(request.orderedMessages) !== request.branchPoint.orderHash || stableJson(prefix) !== stableJson(request.orderedMessages)) return forkReply(res, 409, { ok: false, code: "BRANCH_POINT_CONFLICT", error: "ordered prefix/hash does not match parent" }); + const rawIds = new Set(prefix.map((m) => m.rawId)); + const child = createSession(request.childConversationId, { ...parent.meta, label: request.childConversationId }); + const crossing = parent.state.blocks.filter((b) => b.effectiveMessageIds.some((id) => rawIds.has(id)) && !b.effectiveMessageIds.every((id) => rawIds.has(id))); + const blocks = parent.state.blocks.filter((b) => b.effectiveMessageIds.length > 0 && b.effectiveMessageIds.every((id) => rawIds.has(id))); + const blockIds = new Set(blocks.map((b) => b.blockId)); + const expandedChildren = new Set(crossing.flatMap((b) => b.directBlockIds)); + for (const b of [...blocks, ...crossing]) { + const content = parent.blockContents.get(b.blockId); + if (!content || !content.full || typeof content.full.text !== "string" || !content.full.text || b.directBlockIds.some((id) => !parent.state.blocks.some((childBlock) => childBlock.blockId === id))) return forkReply(res, 409, { ok: false, status: "unavailable", code: "PARENT_STATE_INCOMPLETE", error: "nested block/original content unavailable" }); + } + for (const b of blocks) { + if (b.directBlockIds.some((id) => !blockIds.has(id))) return forkReply(res, 409, { ok: false, status: "unavailable", code: "PARENT_STATE_INCOMPLETE", error: "nested block outside matched prefix" }); + child.state.blocks.push({ ...structuredClone(b), ...(expandedChildren.has(b.blockId) ? { active: false, expanded: true } : {}) }); + child.blockContents.set(b.blockId, structuredClone(parent.blockContents.get(b.blockId)!)); + } + // Keep the issued ref namespace reserved, including dead refs, to prevent reuse. + child.state.messageRefs = structuredClone(parent.state.messageRefs); + for (const { rawId, ref } of prefix) { + child.state.messageRefs.byRaw[rawId] = ref; + child.state.messageRefs.byRef[ref] = rawId; + if (parent.state.tokenSnapshot[ref] !== undefined) child.state.tokenSnapshot[ref] = parent.state.tokenSnapshot[ref]; + } + child.state.nextBlockId = parent.state.nextBlockId; + child.state.nextRunId = parent.state.nextRunId; + child.state.lastPassIds = prefix.map((m) => m.rawId); + child.state.rules = structuredClone(parent.state.rules); + child.state.nextRuleId = parent.state.nextRuleId; + child.state.hiddenOrphanRefs = parent.state.hiddenOrphanRefs?.filter((ref) => prefix.some((m) => m.ref === ref)); + child.state.stats.tokensCompressed = child.state.blocks.filter((b) => b.active).reduce((sum, b) => sum + b.compressedTokens, 0); + child.state.stats.compressionCount = blocks.length; + if (prefix.length === snapshot.orderedMessages.length) child.state.nudge = structuredClone(parent.state.nudge); + child.pluginSnapshot = structuredClone(parent.pluginSnapshot!.slice(0, request.branchPoint.messageCount)); + const forkView = prune(child.pluginSnapshot, child.state); + // Hiding the original pair is safe only if its summary survives the fork. + child.state.absorbed = structuredClone(parent.state.absorbed?.filter((a) => + rawIds.has(a.callMessageId) && rawIds.has(a.resultMessageId) && ( + (a.absorbCallId !== undefined && forkView.some((m) => m.contentType === "tool-call" && m.toolCallId === a.absorbCallId)) || + child.state.blocks.some((b) => b.active && !b.expanded && a.summary.length > 0 && b.summary.includes(a.summary)) + )) ?? []); + child.lastMessages = structuredClone(child.pluginSnapshot); + child.lastMessagesFolded = false; + const parentStore = contentStoreOf(parent); + for (const { ref, rawId } of prefix) { + const entry = parentStore.byRef[ref]; + if (entry && (entry.rawId !== rawId || typeof parentStore.byHash[entry.hash] !== "string")) return forkReply(res, 409, { ok: false, status: "unavailable", code: "PARENT_STATE_INCOMPLETE", error: "CCR original unavailable or alias inconsistent" }); + } + child.contentStore = cloneStoreForRefs(parentStore, new Set(prefix.map((m) => m.ref))) ?? undefined; + child.metadata.publicSnapshotTextComparable = true; + child.metadata.publicSnapshotStoredRefs = Object.keys(child.contentStore?.byRef ?? {}); + for (const key of ["pluginAgent", "lastModel", "effectiveConfig", "effectiveCcr", "effectiveContextLimit", "lastWindowSource", "systemPromptTokens"]) if (parent.metadata[key] !== undefined) child.metadata[key] = structuredClone(parent.metadata[key]); + child.metadata.parentConversationId = request.parentConversationId; + child.metadata.parentRevision = request.parentRevision; + child.stats.contextTokens = prune(child.pluginSnapshot, child.state).reduce((sum, m) => sum + countMessageTokens(m), 0) + (typeof child.metadata.systemPromptTokens === "number" ? child.metadata.systemPromptTokens : 0); + child.stats.contextTokensSource = "estimate"; + child.metadata.contextTokensAt = Date.now(); + recordContextObservation(child, child.stats.contextTokens, "estimate"); + const response = { ok: true, status: crossing.length > 0 ? "expanded" : "exact", protocolVersion: 1, parentConversationId: request.parentConversationId, childConversationId: request.childConversationId, sessionId: child.id, parentRevision: request.parentRevision, childRevision: forkSnapshot(child).parentRevision, branchPoint: request.branchPoint, expandedBlocks: crossing.map((b) => b.blockId), inheritedBlocks: child.state.blocks.map((b) => ({ id: b.blockId, tier: b.tier, active: b.active, expanded: b.expanded === true })), replayed: false }; + child.metadata.publicForkReceipt = { requestHash, response }; + publishForkSession(child); + recordPluginSession(request.childConversationId, child.id); + remembered.set(child.id, { processed: structuredClone(child.pluginSnapshot), original: structuredClone(child.pluginSnapshot) }); + forkReply(res, 201, response); + }); + } catch (err) { + forkReply(res, 503, { ok: false, status: "unavailable", code: "FORK_FAILED", error: String(err) }); + } finally { + releaseInFlight(parent); + } +} + /** #1192: model-facing explanation for an opt-in tool the host registered but * this session's effective config has disabled. Returns undefined when the * name is not one of the known opt-in tools (a truly unknown tool keeps the @@ -749,8 +1000,8 @@ function webSessionUrl(origin: string | undefined, sessionId: string): string | } export function handlePluginStatus(conversationId: string, res: import("node:http").ServerResponse, deps: PluginToolDeps, fallbackLatest = false): void { - const { session: resolvedSession, entry } = resolveConversation(conversationId); - let session = resolvedSession; + let session = resolveForkConversation(conversationId); + const entry = conversations.get(conversationId); let viaFallback = false; let resolvedConversationId = conversationId; if (!session && fallbackLatest) { @@ -778,7 +1029,7 @@ export function handlePluginStatus(conversationId: string, res: import("node:htt const verdict = chainVerdictFor(conversationId); if (verdict !== undefined) { res.writeHead(200, { "content-type": "application/json" }); - res.end(JSON.stringify({ ok: true, conversationId, phase: "chain-advisory", chain: verdict, panel: chainAdvisoryPanel(verdict) })); + res.end(JSON.stringify({ ok: true, conversationId, phase: "chain-advisory", chain: verdict, panel: chainAdvisoryPanel(verdict), sessionId: null, sessionRevision: null, model: null, contextLimit: null, contextTokens: null, contextTokensSource: "unavailable", contextTokensAt: null, contextGeneration: null })); return; } // Runtime-info protocol (#955): no session exists yet, but the client @@ -789,7 +1040,7 @@ export function handlePluginStatus(conversationId: string, res: import("node:htt const pre = pluginRuntimeTable.get(conversationId) ?? pluginRuntimeByConversation.get(conversationId); if (pre !== undefined) { res.writeHead(200, { "content-type": "application/json" }); - res.end(JSON.stringify({ ok: true, conversationId, phase: "pre-first-request", model: pre.model, contextLimit: pre.contextWindow ?? null, runtimeInfo: pre, panel: null })); + res.end(JSON.stringify({ ok: true, conversationId, phase: "pre-first-request", model: pre.model, contextLimit: pre.contextWindow ?? null, runtimeInfo: pre, panel: null, sessionId: null, sessionRevision: null, contextTokens: null, contextTokensSource: "unavailable", contextTokensAt: null, contextGeneration: null })); return; } res.writeHead(404, { "content-type": "application/json" }); @@ -798,6 +1049,11 @@ export function handlePluginStatus(conversationId: string, res: import("node:htt } if (entry) entry.lastSeen = Date.now(); const limit = session.metadata.effectiveContextLimit; + const observation = currentContextObservation(session); + const contextTokensSource = observation?.source ?? "unavailable"; + const contextTokens = observation?.tokens ?? null; + let sessionRevision: string | null = null; + try { sessionRevision = forkSnapshot(session).parentRevision; } catch {} const mem = remembered.get(session.id); const modelContextLimit = typeof limit === "number" && limit > 0 ? limit : 0; // #387: the remembered nudge is a prepare-time snapshot. A compress tool @@ -809,7 +1065,7 @@ export function handlePluginStatus(conversationId: string, res: import("node:htt // serving the stale snapshot. let nudge: NudgeDecision | undefined; try { - const messages = mem ? (mem.processed.length > 0 ? mem.processed : mem.original) : []; + const messages = mem ? (mem.processed.length > 0 ? mem.processed : mem.original) : (session.pluginSnapshot ?? []); if (messages.length > 0) { // #833: base kernelConfig carries no file/provider/model compress // settings — render from the session's last resolved Config so the @@ -887,6 +1143,8 @@ export function handlePluginStatus(conversationId: string, res: import("node:htt res.end(JSON.stringify({ ok: true, conversationId: resolvedConversationId, + sessionId: session.id, + sessionRevision, fallback: viaFallback || undefined, label: session.meta.label ?? null, pluginAgent: session.metadata.pluginAgent ?? null, @@ -896,7 +1154,11 @@ export function handlePluginStatus(conversationId: string, res: import("node:htt ?? pluginRuntimeInfoForConversation(conversationIdForSession(session.id), typeof session.metadata.lastModel === "string" ? session.metadata.lastModel : undefined) ?? null, contextLimit: typeof limit === "number" ? limit : null, - contextTokens: session.stats.lastInputTokens, + contextTokens, + contextTokensSource, + contextTokensAt: observation?.at ?? null, + contextGeneration: observation ? forkHash({ sessionId: session.id, sessionRevision, generation: observation.generation, contextTokens, contextTokensSource, limit, model: session.metadata.lastModel }) : null, + compressCreditTokens: session.stats.compressCreditTokens ?? 0, inputTokens: session.stats.inputTokens, outputTokens: session.stats.outputTokens, cachedTokens: session.stats.cachedTokens, @@ -913,9 +1175,10 @@ export async function handlePluginTool( res: import("node:http").ServerResponse, deps: PluginToolDeps, ): Promise { - let parsed: { conversationId?: unknown; tool?: unknown; args?: unknown }; + let parsed: { conversationId?: unknown; tool?: unknown; args?: unknown; expectedRevision?: unknown }; try { - parsed = JSON.parse(payload) as { conversationId?: unknown; tool?: unknown; args?: unknown }; + parsed = JSON.parse(payload) as { conversationId?: unknown; tool?: unknown; args?: unknown; expectedRevision?: unknown }; + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error("expected tool object"); } catch { res.writeHead(400, { "content-type": "application/json" }); res.end(JSON.stringify({ ok: false, error: "invalid JSON body" })); @@ -923,29 +1186,31 @@ export async function handlePluginTool( } const conversationId = typeof parsed.conversationId === "string" ? parsed.conversationId.trim() : ""; const tool = typeof parsed.tool === "string" ? parsed.tool : ""; + if (parsed.expectedRevision !== undefined && (typeof parsed.expectedRevision !== "string" || !/^[a-f0-9]{64}$/.test(parsed.expectedRevision))) return forkReply(res, 400, { ok: false, code: "INVALID_REQUEST", error: "expectedRevision must be a snapshot revision" }); + if (parsed.expectedRevision !== undefined && !conversationId) return forkReply(res, 400, { ok: false, code: "INVALID_REQUEST", error: "expectedRevision requires conversationId" }); + // Explicit ids never yield to another session's outbound witness. // #1685 zero-injection routing ladder for id-less tool POSTs: // 1. outbound witness — this proxy streamed the very tool_use being // answered; a unique hit names the session (the free-text summary is - // a unique anchor). Wins over a body id when they disagree. - // 2. body conversationId — extensions (pi/opencode/dsh) and legacy - // per-call values still carry it; unchanged behavior. - // 3. single-active arbitration — one fresh conversation on the proxy. - // 4. anything else is a loud 400 (never a silent guess). + // a unique anchor). + // 2. single-active arbitration — one fresh conversation on the proxy. + // 3. anything else is a loud 400 (never a silent guess). const bodyArgs = parsed.args && typeof parsed.args === "object" ? parsed.args as Record : {}; const witnessIds = tool ? lookupToolWitness(tool, bodyArgs) : new Set(); let session: Session | undefined; let entry: ConversationEntry | undefined; let routedBy: "witness" | "body" | "arb" = "body"; - if (witnessIds.size === 1) { + if (conversationId) { + session = resolveForkConversation(conversationId); + entry = conversations.get(conversationId); + if (session && witnessIds.size > 0 && !witnessIds.has(session.id)) return forkReply(res, 409, { ok: false, code: "TOOL_CONVERSATION_CONFLICT", error: "outbound tool witness does not match conversationId" }); + } else if (witnessIds.size === 1) { const [wit] = [...witnessIds]; session = peekSession(wit); if (session) { const cid = conversationIdForSession(session.id); entry = cid ? conversations.get(cid) : undefined; routedBy = "witness"; - if (conversationId && conversationId !== cid && conversationId !== session.id) { - deps.log("info", `[plugin] tool "${tool}" routed by outbound witness to session ${session.id}${cid ? ` (conversation ${cid})` : ""}; body conversationId "${conversationId}" differs and was ignored (#1685)`); - } } } else if (witnessIds.size > 1 && !conversationId) { res.writeHead(400, { "content-type": "application/json" }); @@ -976,7 +1241,8 @@ export async function handlePluginTool( } } if (!session) { - ({ session, entry } = resolveConversation(conversationId)); + session = resolveForkConversation(conversationId); + entry = conversations.get(conversationId); } // #760: the verbatim-id fallback above can resolve a session with NO map // entry (first call), so only the session itself gates execution. @@ -1043,16 +1309,31 @@ export async function handlePluginTool( delete args.conversation_id; const callId = `${PLUGIN_FOLD_CALLID_PREFIX}${Date.now().toString(36)}`; acquireInFlight(session); - let result: string; + let result: string | undefined; try { result = await withSessionLock(session, async () => { + if (parsed.expectedRevision !== undefined) { + try { + if (forkSnapshot(session).parentRevision !== parsed.expectedRevision) { + forkReply(res, 409, { ok: false, code: "PARENT_REVISION_CONFLICT", error: "session revision changed" }); + return undefined; + } + } catch (err) { + forkReply(res, 409, { ok: false, status: "unavailable", code: "SNAPSHOT_UNAVAILABLE", error: String(err) }); + return undefined; + } + } // Read the remembered snapshot UNDER the session lock: the model // request rewrites remembered atomically under this same lock // (rememberPluginMessages), so a racing tool call sees a consistent // state instead of a stale/empty window. const mem = remembered.get(session.id); - const messages = mem ? (mem.processed.length > 0 ? mem.processed : mem.original) : []; - return executeProxyTool(tool, args, { + const messages = mem ? (mem.processed.length > 0 ? mem.processed : mem.original) : (session.pluginSnapshot ?? []); + const before = currentContextObservation(session); + const creditBefore = session.stats.compressCreditTokens ?? 0; + const compressBefore = session.lastCompress; + const pendingBefore = new Set(session.pendingRetrievals.map((p) => p.ref)); + const toolResult = executeProxyTool(tool, args, { core: deps.core, // #833: run proxy tools under the session's last resolved Config // (same values the wire path used), not the base kernelConfig. @@ -1061,6 +1342,16 @@ export async function handlePluginTool( session, log: (m) => deps.log("info", `[${session.id}] [plugin] ${m}`), }, callId); + const creditDelta = (session.stats.compressCreditTokens ?? 0) - creditBefore; + const restoredInjections = session.pendingRetrievals.filter((p) => !pendingBefore.has(p.ref)); + // The string tool protocol has distinct success headers for whole/derived and range restores. + const restored = tool === "decompress" && (/^\[Block [^\n]+ content /.test(toolResult) || /^\[decompress [^\n]+: restored \d+ item\(s\)/.test(toolResult)); + if (before && (creditDelta !== 0 || session.lastCompress !== compressBefore || restored)) { + // Credit was already applied to lastInputTokens by the tool; do not net it twice. + const restoredTokens = restored ? countMessageTokens({ text: toolResult }) + restoredInjections.reduce((sum, p) => sum + countMessageTokens(p.injection), 0) : 0; + recordContextObservation(session, Math.max(0, before.tokens - creditDelta) + restoredTokens, "estimate"); + } + return toolResult; }); } catch (err) { releaseInFlight(session); @@ -1070,6 +1361,7 @@ export async function handlePluginTool( return; } releaseInFlight(session); + if (result === undefined) return; // #760b: evidence-based plugin-mode flip. A successful MCP tool execution proves // this session's host owns the bili compression tools, so bind it to plugin mode // (sticky) — the next model request stops injecting the duplicate ephemeral wire diff --git a/src/server.ts b/src/server.ts index 9a3e5be4f2..cbc4166b79 100644 --- a/src/server.ts +++ b/src/server.ts @@ -111,8 +111,9 @@ import { emitPreflightError, emitStreamError } from "./stream-error.js"; import { affinityToken, claudeSubagentAgentId, claudeSubagentSplit, clientConversationHeader, codexTurnIdentity, dshPersonaFingerprintApplies, instructionsFingerprintApplies, openaiSystemTextForPersona, preferPromptCacheKeyIdentity, type ConversationIdentity } from "./session-id.js"; import { prefixAffinity, type AnonymousAffinity } from "./prefix-affinity.js"; import { maybeAdoptForkBlocks, maybeAdoptResume } from "./fork-adoption.js"; +import { publicForkInputMatches } from "./plugin.js"; import { flushPrefixAffinity, hydratePrefixAffinity, scheduleAffinityPersist } from "./affinity-persist.js"; -import { consumePluginRegisterFor, flushConversations, handlePluginCompact, handlePluginManifest, handlePluginRegister, handlePluginRuntimeInfo, handlePluginStatus, handlePluginTool, isPluginFoldCallId, loadConversations, pipePluginChatWithStrip, pipePluginJson, pipePluginResponsesWithStrip, pluginAgentHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "./plugin.js"; +import { consumePluginRegisterFor, flushConversations, handlePluginCompact, handlePluginManifest, handlePluginFork, handlePluginSnapshot, handlePluginRegister, handlePluginRuntimeInfo, handlePluginStatus, handlePluginTool, isPluginFoldCallId, loadConversations, pipePluginChatWithStrip, pipePluginJson, pipePluginResponsesWithStrip, pluginAgentHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginRequestAgentHeader, pluginRuntimeInfoFor, pluginRuntimeInfoForConversation, recordChainVerdict, recordPluginSession, rememberPluginMessages, resolveConversation, runtimeConversationId, takePendingPluginRegister } from "./plugin.js"; import { setupMitm, readMitmUpstream, getBlindTunnelStats, liveBlindTunnels, MITM_RAW_SOCKET_KEY } from "./mitm.js"; import { evaluateChain, extractChainCarriers, stampOutbound, stripEmbeddedChainCarriers } from "./chain-checkpoint.js"; import type { BiliMessage } from "acp-kernel/wire"; @@ -1483,6 +1484,18 @@ async function handle( // requires. Do not "simplify" this back to `config`. return handlePluginManifest(res, applyCompressSettings(config, opts.modelContextLimit, opts.compress)); } + if (req.method === "GET" && req.url?.split("?")[0] === "/__bili/plugin/snapshot") { + return await handlePluginSnapshot(new URL(req.url, "http://localhost").searchParams.get("conversationId") ?? "", res); + } + if (req.method === "POST" && req.url === "/__bili/plugin/fork") { + try { + return await handlePluginFork((await readBody(req)).toString("utf8"), res); + } catch (err) { + res.writeHead(err instanceof BodyTooLargeError ? 413 : 400, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, code: "INVALID_REQUEST", error: String(err) })); + return; + } + } if (req.method === "GET" && req.url?.startsWith("/__bili/plugin/status")) { const query = req.url.slice(req.url.indexOf("?") + 1); const params = new URLSearchParams(query); @@ -2349,6 +2362,22 @@ async function handle( ? bodyIdentity.value : clientConversationHeader(req.headers); const session = getSession(sessionId, { protocol, upstreamOrigin, label: clientLabel ?? (anonAffinity ? "prefix-affinity" : undefined) }); + let publicForkPrefix = false; + if (!countTokens && !responsesCompact && session.metadata.publicForkReceipt !== undefined) { + acquireInFlight(session); + try { + const accepted = await withSessionLock(session, () => { + publicForkPrefix = publicForkInputMatches(session, protocol, parsed); + if (publicForkPrefix || session.stats.requests > 0) return true; + res.writeHead(409, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, code: "FORK_PREFIX_CONFLICT", error: "first child request does not match its inherited ordered prefix" })); + return false; + }); + if (!accepted) return; + } finally { + releaseInFlight(session); + } + } // Audit stamp (#730 forensics): the effective pack for the most recent // request (route/model can change it — latest wins). Persisted with the // session so post-hoc forensics never needs config-mtime archaeology. @@ -2518,7 +2547,7 @@ async function handle( // parent chain at read time (src/decompress-shared.ts, depth cap 8). // Late binding is harmless (the link copies nothing at link time), so // the gate is idempotence, not first-request. - if (derivedParent !== undefined && session.metadata.derivedFromSessionId === undefined) { + if (derivedParent !== undefined && session.metadata.derivedFromSessionId === undefined && session.metadata.publicForkReceipt === undefined) { try { const parentSession = resolveConversation(derivedParent)?.session; if (parentSession) { @@ -2557,7 +2586,7 @@ async function handle( // assigns refs. A resolved explicit plugin-reported lineage above wins // (gate on derivedFromSessionId); this content match is the fallback // signal for clients that report no lineage. - if (clientProvided && !anonAffinity && session.stats.requests === 0 && session.metadata.derivedFromSessionId === undefined && opts.resumeInheritance !== false) { + if (clientProvided && !anonAffinity && session.stats.requests === 0 && session.metadata.derivedFromSessionId === undefined && session.metadata.publicForkReceipt === undefined && opts.resumeInheritance !== false) { const resume = prefixAffinity.findResumeParent(affinityMessageList(), sessionId); if (resume) { const resumeParent = peekSession(resume.sessionId) ?? getStore().loadSync(resume.sessionId, { protocol, upstreamOrigin }) ?? undefined; @@ -2688,8 +2717,9 @@ async function handle( // all-bili-tools demotion is vetoed for them (side requests on this lane // are identified by the #1699 persona header instead). const wsLaneEnvelope = req.headers["x-bili-ws-lane"] !== undefined; + // A verified public-fork prefix also proves this is main history. const demotedSide = !countTokens && !responsesCompact && protocol !== null && pluginMode - && !wsLaneEnvelope + && !wsLaneEnvelope && !publicForkPrefix && detectAcpArtifacts(bodyBuffer, parsed) === null && stripLeakedBiliTools(parsed); // #546: restore a client-shrunk output budget BEFORE the side gate so a @@ -2743,7 +2773,7 @@ async function handle( // heuristic alone misses them. The host stamps its per-request persona id // (x-bili-plugin-agent); a known side-request agent routes verbatim by intent. const requestAgent = pluginRequestAgentHeader(req.headers); - if (!countTokens && !responsesCompact && protocol !== null && (demotedSide || isSideRequest(parsed, requestAgent))) { + if (!countTokens && !responsesCompact && protocol !== null && !publicForkPrefix && (demotedSide || isSideRequest(parsed, requestAgent))) { // #554: the passthrough below skips EVERY input-side guard by design // (#388) — a full-history side request over the window is a // guaranteed upstream 400 (and title-gen/probe clients re-issue it, @@ -3200,7 +3230,7 @@ async function handle( // plugin tool call sees a consistent window. if (pendingForward.prepared) { const preparedToRemember = pendingForward.prepared; - await withSessionLock(session, () => rememberPluginMessages(sessionId, preparedToRemember.processedMessages, preparedToRemember.originalMessages, preparedToRemember.nudge)); + await withSessionLock(session, () => rememberPluginMessages(sessionId, preparedToRemember.processedMessages, preparedToRemember.originalMessages, preparedToRemember.nudge, bodyBuffer)); } } } finally { @@ -3797,6 +3827,7 @@ async function prepareAnthropic( nudge = turn.nudge; session.stats.contextTokens = tokenCount; session.stats.contextTokensSource = tokenCountSource; + session.metadata.contextTokensAt = Date.now(); if (!session.meta.title) { const t = deriveTitle(msgs); if (t) session.meta.title = t; @@ -4042,6 +4073,7 @@ async function prepareOpenai( nudge = turn.nudge; session.stats.contextTokens = tokenCount; session.stats.contextTokensSource = tokenCountSource; + session.metadata.contextTokensAt = Date.now(); if (!session.meta.title) { const t = deriveTitle(msgs); if (t) session.meta.title = t; @@ -4308,6 +4340,7 @@ async function prepareGoogle( nudge = turn.nudge; session.stats.contextTokens = tokenCount; session.stats.contextTokensSource = tokenCountSource; + session.metadata.contextTokensAt = Date.now(); if (!session.meta.title) { const t = deriveTitle(msgs); if (t) session.meta.title = t; @@ -4615,6 +4648,7 @@ async function prepareResponses( nudge = turn.nudge; session.stats.contextTokens = tokenCount; session.stats.contextTokensSource = tokenCountSource; + session.metadata.contextTokensAt = Date.now(); if (!session.meta.title) { const t = deriveTitle(msgs); if (t) session.meta.title = t; diff --git a/src/session.ts b/src/session.ts index 413f4a809e..26a394c92c 100644 --- a/src/session.ts +++ b/src/session.ts @@ -287,6 +287,7 @@ export type Session = { * the state ranges). Cleared by snapshotMessages on the next live * request — the client re-sends full raw history, restoring the invariant. */ lastMessagesFolded?: boolean; + pluginSnapshot?: CoreMessage[]; /** Kernel CCR envelope (#1097): originals of ID-referenced tool results, * owned and mutated only by kernel processTurn (ccr-store node) via * adoptContentStore. Lazily loaded from the session's content-store.json; @@ -534,7 +535,13 @@ export function getSession(id: string, meta?: { protocol?: Session["meta"]["prot throw new Error(`session pool exhausted (MAX_SESSIONS=${MAX_SESSIONS}; all in-flight)`); } } - const session: Session = { + const session = createSession(id, meta); + sessions.set(id, session); + return session; +} + +export function createSession(id: string, meta?: Session["meta"]): Session { + return { id, meta: { protocol: meta?.protocol, upstreamOrigin: meta?.upstreamOrigin, label: meta?.label }, stats: zeroStats(), @@ -547,8 +554,14 @@ export function getSession(id: string, meta?: { protocol?: Session["meta"]["prot persisted: false, pendingRetrievals: [], }; - sessions.set(id, session); - return session; +} + +export function publishForkSession(session: Session): void { + if (sessions.has(session.id) || getStore().loadSync(session.id)) throw new Error("child session already exists"); + if (sessions.size >= MAX_SESSIONS && !evictOldest()) throw new Error("session pool exhausted"); + if (!getStore().flushSync(session)) throw new Error("fork persistence failed"); + session.persisted = getStore().enabled; + sessions.set(session.id, session); } /** Mark a session as in-use by a request. Must be paired with releaseInFlight. diff --git a/tests/golden/wire-contract/manifest-default.json b/tests/golden/wire-contract/manifest-default.json index 2c12a088c0..608ff91c57 100644 --- a/tests/golden/wire-contract/manifest-default.json +++ b/tests/golden/wire-contract/manifest-default.json @@ -609,5 +609,12 @@ }, "toolEndpoint": "/__bili/plugin/tool", "statusEndpoint": "/__bili/plugin/status", - "runtimeInfoEndpoint": "/__bili/plugin/runtime-info" + "runtimeInfoEndpoint": "/__bili/plugin/runtime-info", + "capabilities": { + "fork": { + "protocolVersion": 1, + "endpoint": "/__bili/plugin/fork", + "snapshotEndpoint": "/__bili/plugin/snapshot" + } + } } diff --git a/tests/golden/wire-contract/manifest-optin.json b/tests/golden/wire-contract/manifest-optin.json index 513a499855..30d230024d 100644 --- a/tests/golden/wire-contract/manifest-optin.json +++ b/tests/golden/wire-contract/manifest-optin.json @@ -797,5 +797,12 @@ }, "toolEndpoint": "/__bili/plugin/tool", "statusEndpoint": "/__bili/plugin/status", - "runtimeInfoEndpoint": "/__bili/plugin/runtime-info" + "runtimeInfoEndpoint": "/__bili/plugin/runtime-info", + "capabilities": { + "fork": { + "protocolVersion": 1, + "endpoint": "/__bili/plugin/fork", + "snapshotEndpoint": "/__bili/plugin/snapshot" + } + } } diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts new file mode 100644 index 0000000000..4f93f4b5e0 --- /dev/null +++ b/tests/plugin-fork.test.ts @@ -0,0 +1,591 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { createHash } from "node:crypto"; +import test from "node:test"; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { spawn } from "node:child_process"; +import { applyAbsorb, createContentStore, defaultConfig, DEFAULT_ABSORB_CONFIG, DEFAULT_CCR_CONFIG, storeOriginal } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetPluginStateForTest, rememberPluginMessages, resolveConversation } from "../src/plugin.ts"; +import { _resetSessionsForTest, getSession } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { recordToolWitness, resetToolRingForTest } from "../src/tool-ring.ts"; + +process.env.NODE_ENV = "test"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-fork-")); +test.after(() => rmSync(testRoot, { recursive: true, force: true })); +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = testRoot; +const hash = (v: unknown) => createHash("sha256").update(JSON.stringify(v)).digest("hex"); + +async function harness(persist = false) { + const dir = mkdtempSync(join(testRoot, "run-")); + for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + const store = new SessionStore({ dir: dir + "/sessions", enabled: persist, debounceMs: 60000 }); + _setStoreForTest(store); + _setForTest({}); + const forwarded: Record[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + forwarded.push(JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "msg_test", role: "assistant", content: [{ type: "text", text: "answer" }], usage: { input_tokens: 10000, output_tokens: 10 } })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const addr = upstream.address(); + assert(addr && typeof addr === "object"); + const upstreamUrl = `http://127.0.0.1:${addr.port}`; + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "claude-test": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: true, preserveRecentMessages: 1, preserveRecentTokens: 0, minCompressRangeChars: 100 }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); + await once(proxy, "listening"); + const paddr = proxy.address(); + assert(paddr && typeof paddr === "object"); + const origin = `http://127.0.0.1:${paddr.port}`; + const request = async (path: string, body?: unknown) => { + const r = await fetch(origin + path, body === undefined ? {} : { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); + return { status: r.status, body: await r.json() }; + }; + const messages = [{ role: "user", content: "first original ".repeat(250) }, { role: "assistant", content: "second original ".repeat(250) }, { role: "user", content: "tail original" }]; + const r = await fetch(`${origin}/bili/${upstreamUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "test", "x-bili-plugin-conversation": "parent" }, body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages }) }); + assert.equal(r.status, 200); + await r.text(); + return { request, messages, forwarded, dir, store, upstreamUrl, origin, close: async () => { store.cancelAll(); proxy.close(); upstream.close(); await Promise.all([once(proxy, "close"), once(upstream, "close")]); } }; +} + +test("plain proxy panel snapshots do not change raw-history persistence", async () => { + const h = await harness(); + try { + const session = getSession("plain-panel"); + const messages = [{ id: "plain-message", role: "user" as const, text: "raw plain-proxy original" }]; + rememberPluginMessages(session.id, messages, messages); + assert.equal(session.pluginSnapshot, undefined); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=plain-panel")).status, 409); + } finally { await h.close(); } +}); + +test("HTTP exposes a versioned snapshot and forks an exact ordered prefix", async () => { + const h = await harness(); + try { + const manifest = await h.request("/__bili/plugin/manifest"); + assert.deepEqual(manifest.body.capabilities.fork, { protocolVersion: 1, endpoint: "/__bili/plugin/fork", snapshotEndpoint: "/__bili/plugin/snapshot" }); + const snapshot = await h.request("/__bili/plugin/snapshot?conversationId=parent"); + assert.equal(snapshot.status, 200, JSON.stringify({ response: snapshot.body, session: resolveConversation("parent").session })); + assert.equal(snapshot.body.orderedMessages.length, 3); + assert.equal(snapshot.body.messages[0].text, h.messages[0].content); + assert.equal(snapshot.body.messages[0].ref, snapshot.body.orderedMessages[0].ref); + const orderedMessages = snapshot.body.orderedMessages.slice(0, 2); + const request = { protocolVersion: 1, parentConversationId: "parent", childConversationId: "child", parentRevision: snapshot.body.parentRevision, branchPoint: { messageCount: 2, orderHash: hash(orderedMessages) }, orderedMessages, idempotencyKey: "fork-1" }; + const fork = await h.request("/__bili/plugin/fork", request); + assert.equal(fork.status, 201); + assert.equal(fork.body.childConversationId, "child"); + const child = await h.request("/__bili/plugin/snapshot?conversationId=child"); + assert.deepEqual(child.body.orderedMessages, orderedMessages); + assert.notEqual(child.body.sessionId, snapshot.body.sessionId); + assert.equal((await h.request("/__bili/plugin/fork", request)).status, 200); + } finally { await h.close(); } +}); + +function forkRequest(snapshot: { parentRevision: string; orderedMessages: unknown[] }, child = "child", count = 2) { + const orderedMessages = snapshot.orderedMessages.slice(0, count); + return { protocolVersion: 1, parentConversationId: "parent", childConversationId: child, parentRevision: snapshot.parentRevision, branchPoint: { messageCount: count, orderHash: hash(orderedMessages) }, orderedMessages, idempotencyKey: `fork-${child}` }; +} + +test("HTTP tool witnesses never override explicit parent, child or sibling identities", async () => { + const h = await harness(); + try { + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + for (const id of ["child", "sibling"]) assert.equal((await h.request("/__bili/plugin/fork", forkRequest(snapshot, id, 3))).status, 201); + const args = { content: [{ startId: "m00001", endId: "m00002", summary }] }; + const revisions = new Map(); + for (const id of ["parent", "child", "sibling"]) revisions.set(id, (await h.request(`/__bili/plugin/snapshot?conversationId=${id}`)).body.parentRevision); + for (const [witness, target] of [["parent", "child"], ["child", "parent"], ["child", "sibling"], ["sibling", "child"]]) { + resetToolRingForTest(); + recordToolWitness(witness!, "compress", args); + for (const expectedRevision of [undefined, revisions.get(target!)]) { + const conflict = await h.request("/__bili/plugin/tool", { conversationId: target, tool: "compress", args, expectedRevision }); + assert.equal(conflict.status, 409, JSON.stringify(conflict.body)); + assert.equal(conflict.body.code, "TOOL_CONVERSATION_CONFLICT"); + } + for (const [id, revision] of revisions) assert.equal((await h.request(`/__bili/plugin/snapshot?conversationId=${id}`)).body.parentRevision, revision); + } + resetToolRingForTest(); + recordToolWitness("parent", "compress", args); + recordToolWitness("child", "compress", args); + assert.equal((await h.request("/__bili/plugin/tool", { conversationId: "child", tool: "compress", args })).status, 200); + assert.equal(resolveConversation("child").session!.state.blocks.length, 1); + assert.equal(resolveConversation("parent").session!.state.blocks.length, 0); + assert.equal(resolveConversation("sibling").session!.state.blocks.length, 0); + resetToolRingForTest(); + recordToolWitness("sibling", "compress", args); + assert.equal((await h.request("/__bili/plugin/tool", { tool: "compress", args })).status, 200); + assert.equal(resolveConversation("sibling").session!.state.blocks.length, 1); + assert.equal(resolveConversation("parent").session!.state.blocks.length, 0); + } finally { await h.close(); } +}); + +test("HTTP snapshot normalizes tool error flags and rejects a success identity for a failed result", async () => { + const h = await harness(); + try { + const history = (isError?: boolean) => [ + { role: "user", content: "run checks" }, + { role: "assistant", content: [{ type: "tool_use", id: "check_1", name: "bash", input: { command: "npm test" } }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "check_1", content: "identical tool output", ...(isError === undefined ? {} : { is_error: isError }) }] }, + { role: "assistant", content: "checks reviewed" }, + ]; + await sendModel(h, "parent", history()); + const implicit = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + await sendModel(h, "parent", history(false)); + const success = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + await sendModel(h, "parent", history(true)); + const failed = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + assert.equal(implicit.messages[2].toolIsError, false); + assert.equal(success.messages[2].toolIsError, false); + assert.equal(failed.messages[2].toolIsError, true); + assert.equal(implicit.orderedMessages[2].identityHash, success.orderedMessages[2].identityHash); + assert.equal(failed.orderedMessages[2].rawId, success.orderedMessages[2].rawId); + assert.notEqual(failed.orderedMessages[2].identityHash, success.orderedMessages[2].identityHash); + const stale = forkRequest({ ...failed, orderedMessages: success.orderedMessages }, "wrong-error-identity", 3); + assert.equal((await h.request("/__bili/plugin/fork", stale)).body.code, "BRANCH_POINT_CONFLICT"); + assert.equal((await h.request("/__bili/plugin/fork", forkRequest(failed, "error-child", 3))).status, 201); + const child = (await h.request("/__bili/plugin/snapshot?conversationId=error-child")).body; + assert.equal(child.messages[2].toolIsError, true); + assert.equal(child.orderedMessages[2].identityHash, failed.orderedMessages[2].identityHash); + } finally { await h.close(); } +}); + +test("HTTP fork fails closed on malformed, missing, stale and reordered boundaries", async () => { + const h = await harness(); + try { + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const req = forkRequest(snapshot); + for (const payload of [null, [], { ...req, protocolVersion: 2 }, { ...req, childConversationId: "parent" }, { ...req, parentConversationId: " parent" }, { ...req, branchPoint: { ...req.branchPoint, messageCount: -1 } }]) { + assert.equal((await h.request("/__bili/plugin/fork", payload)).status, 400); + } + assert.equal((await h.request("/__bili/plugin/fork", { ...req, parentConversationId: "missing" })).status, 404); + assert.equal((await h.request("/__bili/plugin/fork", { ...req, parentRevision: "0".repeat(64) })).body.code, "PARENT_REVISION_CONFLICT"); + const reversed = [...req.orderedMessages].reverse(); + assert.equal((await h.request("/__bili/plugin/fork", { ...req, orderedMessages: reversed, branchPoint: { messageCount: 2, orderHash: hash(reversed) } })).body.code, "BRANCH_POINT_CONFLICT"); + assert.equal((await h.request("/__bili/plugin/fork", { ...req, branchPoint: { ...req.branchPoint, orderHash: "0".repeat(64) } })).body.code, "BRANCH_POINT_CONFLICT"); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=child")).status, 404); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=parent")).body.parentRevision, snapshot.parentRevision); + assert.equal((await h.request("/__bili/plugin/fork", req)).status, 201); + assert.equal((await h.request("/__bili/plugin/fork", { ...req, idempotencyKey: "different" })).body.code, "CHILD_CONFLICT"); + } finally { await h.close(); } +}); + +test("HTTP concurrent identical forks publish once and conflicting parents cannot overwrite", async () => { + const h = await harness(); + try { + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const req = forkRequest(snapshot); + const results = await Promise.all(Array.from({ length: 12 }, () => h.request("/__bili/plugin/fork", req))); + assert.equal(results.filter((r) => r.status === 201).length, 1); + assert.equal(results.filter((r) => r.status === 200).length, 11); + const conflicts = await Promise.all(Array.from({ length: 4 }, (_, i) => h.request("/__bili/plugin/fork", { ...req, idempotencyKey: `collision-${i}` }))); + assert(conflicts.every((r) => r.status === 409)); + assert.deepEqual((await h.request("/__bili/plugin/snapshot?conversationId=child")).body.orderedMessages, req.orderedMessages); + const empty = await h.request("/__bili/plugin/fork", forkRequest(snapshot, "empty", 0)); + assert.equal(empty.status, 201); + assert.deepEqual((await h.request("/__bili/plugin/snapshot?conversationId=empty")).body.orderedMessages, []); + } finally { await h.close(); } +}); + +const summary = "The preserved history contains the original request and response; all key content is available through the inherited block and its stable references."; +async function compress(h: Awaited>, conversationId = "parent", startId = "m00001", endId = "m00002") { + const r = await h.request("/__bili/plugin/tool", { conversationId, tool: "compress", args: { content: [{ startId, endId, summary }] } }); + assert.equal(r.status, 200); + assert(!r.body.result.includes("FAILED"), r.body.result); + return r; +} + +test("HTTP compressed originals are isolated across parent and siblings and straddlers are excluded", async () => { + const h = await harness(); + try { + await compress(h); + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const full = await h.request("/__bili/plugin/fork", forkRequest(snapshot)); + assert.equal(full.status, 201, JSON.stringify(full.body)); + assert.equal(full.body.inheritedBlocks.length, 1); + const sibling = await h.request("/__bili/plugin/fork", forkRequest(snapshot, "sibling")); + assert.equal(sibling.status, 201); + const straddler = await h.request("/__bili/plugin/fork", forkRequest(snapshot, "straddler", 1)); + assert.equal(straddler.status, 201); + assert.equal(straddler.body.status, "expanded"); + assert.deepEqual(straddler.body.inheritedBlocks, []); + const original = await h.request("/__bili/plugin/tool", { conversationId: "child", tool: "decompress", args: { blockId: "b1", full: true } }); + assert.match(original.body.result, /first original/); + assert.match(original.body.result, /second original/); + assert(!original.body.result.includes("tail original")); + const parentState = resolveConversation("parent").session!; + const childState = resolveConversation("child").session!; + const siblingState = resolveConversation("sibling").session!; + assert.notEqual(childState.state.blocks[0], parentState.state.blocks[0]); + assert.notEqual(childState.blockContents.get("b1"), siblingState.blockContents.get("b1")); + childState.state.blocks[0].summary = "child only"; + childState.state.tokenSnapshot.m00001 = 1; + childState.pluginSnapshot![0].text = "child only"; + assert.equal(parentState.state.blocks[0].summary, summary); + assert.equal(siblingState.state.blocks[0].summary, summary); + assert.notEqual(parentState.state.tokenSnapshot.m00001, 1); + assert.match(parentState.pluginSnapshot![0].text!, /first original/); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=parent")).body.parentRevision, snapshot.parentRevision); + } finally { await h.close(); } +}); + +test("HTTP exact fork materializes its inherited summary with equivalent SDK text and bili-only tools", async () => { + const h = await harness(); + try { + await compress(h); + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + for (const withTools of [false, true]) { + const id = withTools ? "sdk-tools-child" : "sdk-text-child"; + const fork = await h.request("/__bili/plugin/fork", forkRequest(snapshot, id)); + assert.equal(fork.status, 201); + assert.equal(fork.body.status, "exact"); + assert.equal(fork.body.inheritedBlocks[0].id, "b1"); + const messages = [...h.messages.slice(0, 2).map((m) => ({ ...m, content: [{ type: "text", text: m.content }] })), { role: "user", content: [{ type: "text", text: "continue child" }] }]; + const response = await fetch(`${h.origin}/bili/${h.upstreamUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "ekko-agent", "x-bili-plugin-conversation": id }, body: JSON.stringify({ model: "claude-test", max_tokens: 1024, system: "SDK runtime system", stream: false, messages, ...(withTools ? { tools: [{ name: "compress", description: "Compress", input_schema: { type: "object", properties: {} } }] } : {}) }) }); + assert.equal(response.status, 200, await response.text()); + const forwarded = JSON.stringify(h.forwarded.at(-1)?.messages); + assert(forwarded.includes(summary), JSON.stringify({ blocks: resolveConversation(id).session!.state.blocks, ids: resolveConversation(id).session!.pluginSnapshot?.map((m) => m.id), before: fork.body })); + assert(!forwarded.includes("second original"), forwarded); + const child = (await h.request(`/__bili/plugin/snapshot?conversationId=${id}`)).body; + assert.deepEqual(child.orderedMessages.slice(0, 2), snapshot.orderedMessages.slice(0, 2)); + assert.equal(resolveConversation(id).session!.state.blocks[0].active, true); + } + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=parent")).body.parentRevision, snapshot.parentRevision); + } finally { await h.close(); } +}); + +test("HTTP first fork request rejects a mismatched inherited prefix without forwarding or mutating state", async () => { + const h = await harness(); + try { + await compress(h); + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + assert.equal((await h.request("/__bili/plugin/fork", forkRequest(snapshot))).status, 201); + const child = (await h.request("/__bili/plugin/snapshot?conversationId=child")).body; + const requests = h.forwarded.length; + for (const messages of [ + [{ ...h.messages[0], content: "changed semantic content" }, h.messages[1], { role: "user", content: "continue child" }], + [h.messages[1], h.messages[0], { role: "user", content: "reordered prefix" }], + h.messages.slice(0, 1), + ]) { + const response = await fetch(`${h.origin}/bili/${h.upstreamUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "ekko-agent", "x-bili-plugin-conversation": "child" }, body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages }) }); + const body = await response.text(); + assert.equal(response.status, 409, body); + assert.equal(JSON.parse(body).code, "FORK_PREFIX_CONFLICT"); + assert.equal(h.forwarded.length, requests); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=child")).body.parentRevision, child.parentRevision); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=parent")).body.parentRevision, snapshot.parentRevision); + } + await sendModel(h, "child", [...h.messages.slice(0, 2), { role: "user", content: "valid retry" }]); + assert(JSON.stringify(h.forwarded.at(-1)).includes(summary)); + } finally { await h.close(); } +}); + +test("HTTP persistence failure leaves no child and retry can succeed", async () => { + const h = await harness(); + try { + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const req = forkRequest(snapshot); + writeFileSync(h.dir + "/not-a-directory", "failure fixture"); + const failedStore = new SessionStore({ dir: h.dir + "/not-a-directory", enabled: true, log: () => {} }); + _setStoreForTest(failedStore); + const failed = await h.request("/__bili/plugin/fork", req); + assert.equal(failed.status, 503); + assert.equal(resolveConversation("child").session, undefined); + assert.equal(failedStore.loadSync("child"), null); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=child")).status, 404); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=parent")).body.parentRevision, snapshot.parentRevision); + _setStoreForTest(h.store); + assert.equal((await h.request("/__bili/plugin/fork", req)).status, 201); + } finally { _setStoreForTest(h.store); await h.close(); } +}); + +test("HTTP fork revision and originals survive a new proxy process", async () => { + const h = await harness(true); + let child: ReturnType | undefined; + try { + await compress(h); + const parent = resolveConversation("parent").session!; + parent.metadata.effectiveCcr = { ...DEFAULT_CCR_CONFIG, enabled: true }; + parent.contentStore = storeOriginal(createContentStore(), { ref: "m00001", rawId: parent.pluginSnapshot![0].id, text: "CCR original retained independently after restart", kind: "original", tokens: 10, head: "CCR original" }); + parent.contentStoreDirty = true; + assert(h.store.flushSync(parent)); + assert.deepEqual(JSON.parse(JSON.stringify(h.store.loadSync("parent")!.state)), JSON.parse(JSON.stringify({ ...parent.state, imageFullRestored: parent.state.imageFullRestored ?? [], imageShrinks: parent.state.imageShrinks ?? [] }))); + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const req = forkRequest(snapshot); + const fork = await h.request("/__bili/plugin/fork", req); + assert.equal(fork.status, 201); + const childSnapshot = (await h.request("/__bili/plugin/snapshot?conversationId=child")).body; + const code = `import {startServer} from './src/server.ts'; import {SessionStore,_setStoreForTest} from './src/persist.ts'; import {defaultConfig} from 'acp-kernel'; import {_setForTest} from './src/registry.ts'; _setForTest({}); _setStoreForTest(new SessionStore({dir:${JSON.stringify(h.dir + "/sessions")},enabled:true})); const s=await startServer({port:0,host:'127.0.0.1',upstream:${JSON.stringify(h.upstreamUrl)},modelContextLimit:400000,routes:{},kernelConfig:defaultConfig(400000),compress:{injectTool:true,injectNudge:true},promptCache:{routing:'auto'},sessionHeader:'x-acp-session',log:false,debug:false,passthrough:false,autoUpdate:false,mitm:{enabled:false,domains:[]}}); const ready=()=>process.stdout.write(JSON.stringify({port:s.address().port})+'\\n'); if(s.listening)ready();else s.once('listening',ready);`; + child = spawn(process.execPath, ["--import", "tsx", "--input-type=module", "--eval", code], { cwd: new URL("../", import.meta.url).pathname, env: { ...process.env, NODE_ENV: "test" }, stdio: ["ignore", "pipe", "pipe"] }); + let stderr = ""; + child.stderr!.on("data", (b) => { stderr += b.toString(); }); + const port = await new Promise((resolve, reject) => { + let stdout = ""; + const timer = setTimeout(() => reject(new Error("restart timeout: " + stderr + stdout)), 15000); + child!.stdout!.on("data", (b) => { + stdout += b.toString(); + const port = stdout.match(/\{"port":(\d+)\}/); + if (port) { clearTimeout(timer); resolve(Number(port[1])); } + }); + child!.once("exit", (code) => { clearTimeout(timer); reject(new Error(`restart exited ${code}: ${stderr}`)); }); + }); + const call = async (path: string, body?: unknown) => { + const r = await fetch(`http://127.0.0.1:${port}` + path, body === undefined ? {} : { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); + return { status: r.status, body: await r.json() }; + }; + const restored = await call("/__bili/plugin/snapshot?conversationId=child"); + const restoredStatus = await call("/__bili/plugin/status?conversationId=parent&fallback=latest"); + assert.equal(restoredStatus.status, 200); + assert.equal(restoredStatus.body.sessionId, "parent"); + assert.equal(restoredStatus.body.fallback, undefined); + assert.equal(restoredStatus.body.sessionRevision, snapshot.parentRevision); + assert.equal(restoredStatus.body.contextTokensSource, "estimate"); + assert.equal(restored.status, 200); + assert.equal(restored.body.parentRevision, childSnapshot.parentRevision); + assert.deepEqual(restored.body.orderedMessages, childSnapshot.orderedMessages); + assert.equal((await call("/__bili/plugin/snapshot?conversationId=parent")).body.parentRevision, snapshot.parentRevision); + const replay = await call("/__bili/plugin/fork", req); + assert.equal(replay.status, 200); + assert.equal(replay.body.replayed, true); + const retrieved = await call("/__bili/plugin/tool", { conversationId: "child", tool: "acp_retrieve", args: { ref: "m00001" } }); + assert.equal(retrieved.status, 200); + assert.match(retrieved.body.result, /CCR original retained independently after restart/); + const original = await call("/__bili/plugin/tool", { conversationId: "child", tool: "decompress", args: { blockId: "b1", full: true } }); + assert.match(original.body.result, /first original/); + assert.match(original.body.result, /second original/); + assert(!original.body.result.includes("tail original")); + } finally { + if (child && child.exitCode === null && child.signalCode === null) { child.kill("SIGTERM"); await once(child, "exit"); } + await h.close(); + } +}); + +test("HTTP status separates real usage from a manual-compression estimate and changing generation", async () => { + const h = await harness(); + try { + const before = await h.request("/__bili/plugin/status?conversationId=parent"); + assert.equal(before.body.contextTokensSource, "usage"); + assert.equal(before.body.contextTokens, 10000); + assert(before.body.contextTokensAt > 0); + assert.equal(typeof before.body.contextGeneration, "string"); + const again = await h.request("/__bili/plugin/status?conversationId=parent"); + assert.equal(again.body.contextTokensAt, before.body.contextTokensAt); + assert.equal(again.body.contextGeneration, before.body.contextGeneration); + const compressed = await h.request("/__bili/plugin/tool", { conversationId: "parent", tool: "compress", args: { content: [{ startId: "m00001", endId: "m00002", summary: "The prefix preserves the first user request and the second assistant response, with all original content retained for decompression." }] } }); + assert.equal(compressed.status, 200); + assert(!compressed.body.result.includes("FAILED"), compressed.body.result); + const after = await h.request("/__bili/plugin/status?conversationId=parent"); + assert(after.body.compressCreditTokens > 0); + assert.equal(after.body.contextTokensSource, "estimate"); + assert.equal(after.body.contextTokens, Math.max(0, 10000 - after.body.compressCreditTokens)); + assert(after.body.contextTokensAt >= before.body.contextTokensAt); + assert.notEqual(after.body.contextGeneration, before.body.contextGeneration); + assert.equal(after.body.inputTokens, 10000); + assert.equal(after.body.contextLimit, 400000); + } finally { await h.close(); } +}); + +test("HTTP manual compress rejects a stale expectedRevision under the session lock", async () => { + const h = await harness(); + try { + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const payload = { conversationId: "parent", tool: "compress", expectedRevision: snapshot.parentRevision, args: { content: [{ startId: "m00001", endId: "m00002", summary }] } }; + const results = await Promise.all([h.request("/__bili/plugin/tool", payload), h.request("/__bili/plugin/tool", payload)]); + assert.deepEqual(results.map((r) => r.status).sort(), [200, 409]); + assert.equal(results.find((r) => r.status === 409)!.body.code, "PARENT_REVISION_CONFLICT"); + assert.equal(resolveConversation("parent").session!.state.blocks.length, 1); + } finally { await h.close(); } +}); + +test("HTTP missing nested originals and CCR payloads fail closed without publishing a child", async () => { + const h = await harness(); + try { + await compress(h); + const parent = resolveConversation("parent").session!; + parent.blockContents.delete("b1"); + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const result = await h.request("/__bili/plugin/fork", forkRequest(snapshot)); + assert.equal(result.status, 409); + assert.equal(result.body.status, "unavailable"); + assert.equal(result.body.code, "PARENT_STATE_INCOMPLETE"); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=child")).status, 404); + parent.contentStore = storeOriginal(createContentStore(), { ref: "m00001", rawId: parent.pluginSnapshot![0].id, text: "missing CCR fixture", kind: "original", tokens: 10, head: "missing CCR" }); + delete parent.contentStore.byHash[parent.contentStore.byRef.m00001.hash]; + const missingCcr = await h.request("/__bili/plugin/snapshot?conversationId=parent"); + assert.equal(missingCcr.status, 409); + assert.equal(missingCcr.body.code, "SNAPSHOT_UNAVAILABLE"); + assert.equal((await h.request("/__bili/plugin/fork", forkRequest(snapshot))).body.status, "unavailable"); + } finally { await h.close(); } +}); + +async function sendModel(h: Awaited>, conversationId: string, messages: unknown[]) { + const response = await fetch(`${h.origin}/bili/${h.upstreamUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "test", "x-bili-plugin-conversation": conversationId }, body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages }) }); + assert.equal(response.status, 200, await response.text()); +} + +test("HTTP multimodal snapshots are explicitly unavailable, not text-only matches", async () => { + const h = await harness(); + try { + const before = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + await sendModel(h, "parent", [...h.messages, { role: "user", content: [{ type: "text", text: "inspect the image" }, { type: "image", source: { type: "base64", media_type: "image/png", data: "iVBORw0KGgo=" } }] }]); + const snapshot = await h.request("/__bili/plugin/snapshot?conversationId=parent"); + assert.equal(snapshot.status, 409); + assert.equal(snapshot.body.status, "unavailable"); + assert.equal(snapshot.body.code, "SNAPSHOT_UNAVAILABLE"); + const fork = await h.request("/__bili/plugin/fork", forkRequest(before)); + assert.equal(fork.status, 409); + assert.equal(fork.body.status, "unavailable"); + assert.equal(resolveConversation("child").session, undefined); + } finally { await h.close(); } +}); + +test("HTTP fork before absorb summary preserves the original pair on the first child request", async () => { + const h = await harness(); + try { + const toolOutput = "unique original build output ".repeat(250); + const history = [ + { role: "user", content: "build the project" }, + { role: "assistant", content: [{ type: "tool_use", id: "build_1", name: "bash", input: { command: "npm test" } }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "build_1", content: toolOutput }] }, + { role: "assistant", content: [{ type: "tool_use", id: "absorb_1", name: "acp_absorb", input: { ref: "m00006", summary: "Build succeeded with all original checks." } }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "absorb_1", content: "absorption recorded ".repeat(10) }] }, + { role: "assistant", content: "build complete" }, + { role: "user", content: "continue" }, + ]; + await sendModel(h, "parent", history); + const parent = resolveConversation("parent").session!; + const result = parent.pluginSnapshot!.find((m) => m.contentType === "tool-result" && m.toolCallId === "build_1")!; + const outcome = applyAbsorb({ ref: parent.state.messageRefs.byRaw[result.id], summary: "Build succeeded with all original checks.", absorbCallId: "absorb_1", messages: parent.pluginSnapshot!, state: parent.state, config: { ...defaultConfig(400000), absorb: { ...DEFAULT_ABSORB_CONFIG, enabled: true, toolName: "acp_absorb" } } }); + assert.equal(outcome.ok, true, outcome.resultText); + parent.state = outcome.state; + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + assert.equal((await h.request("/__bili/plugin/fork", forkRequest(snapshot, "before-absorb", 3))).status, 201); + await sendModel(h, "before-absorb", [...history.slice(0, 3), { role: "assistant", content: "child continues before absorption" }]); + const beforeView = JSON.stringify(h.forwarded.at(-1)!.messages); + assert.match(beforeView, /unique original build output/); + assert.match(beforeView, /build_1/); + assert.deepEqual(resolveConversation("before-absorb").session!.state.absorbed, []); + assert.equal(parent.state.absorbed!.length, 1); + assert.equal((await h.request("/__bili/plugin/fork", forkRequest(snapshot, "after-absorb", 5))).status, 201); + await sendModel(h, "after-absorb", [...history.slice(0, 5), { role: "assistant", content: "child continues after absorption" }]); + const afterView = JSON.stringify(h.forwarded.at(-1)!.messages); + assert(!afterView.includes("unique original build output"), afterView); + assert.match(afterView, /Build succeeded with all original checks/); + const after = resolveConversation("after-absorb").session!; + assert.equal(after.state.absorbed!.length, 1); + assert.notEqual(after.state.absorbed![0], parent.state.absorbed![0]); + const carrier = parent.pluginSnapshot!.find((m) => m.contentType === "tool-call" && m.toolCallId === "absorb_1")!; + const carrierResult = parent.pluginSnapshot!.find((m) => m.contentType === "tool-result" && m.toolCallId === "absorb_1")!; + const hiddenCarrier = await h.request("/__bili/plugin/tool", { conversationId: "parent", tool: "compress", args: { content: [{ startId: parent.state.messageRefs.byRaw[carrier.id], endId: parent.state.messageRefs.byRaw[carrierResult.id], summary }] } }); + assert.equal(hiddenCarrier.status, 200); + assert(!hiddenCarrier.body.result.includes("FAILED"), hiddenCarrier.body.result); + const hiddenSnapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + assert.equal((await h.request("/__bili/plugin/fork", forkRequest(hiddenSnapshot, "hidden-carrier", 5))).status, 201); + assert.deepEqual(resolveConversation("hidden-carrier").session!.state.absorbed, []); + await sendModel(h, "hidden-carrier", [...history.slice(0, 5), { role: "assistant", content: "child continues with hidden summary carrier" }]); + assert.match(JSON.stringify(h.forwarded.at(-1)!.messages), /unique original build output/); + const blockSummary = `${summary} Build succeeded with all original checks.`; + const folded = await h.request("/__bili/plugin/tool", { conversationId: "parent", tool: "compress", args: { content: [{ startId: parent.state.messageRefs.byRaw[parent.pluginSnapshot![0].id], endId: parent.state.messageRefs.byRaw[result.id], summary: blockSummary }] } }); + assert.equal(folded.status, 200); + assert(!folded.body.result.includes("FAILED"), folded.body.result); + const foldedSnapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + assert.equal((await h.request("/__bili/plugin/fork", forkRequest(foldedSnapshot, "block-summary", 3))).status, 201); + const blockChild = resolveConversation("block-summary").session!; + assert.equal(blockChild.state.absorbed!.length, 1); + assert.equal(blockChild.state.blocks.length, 1); + await sendModel(h, "block-summary", [...history.slice(0, 3), { role: "assistant", content: "child continues with block summary" }]); + const blockView = JSON.stringify(h.forwarded.at(-1)!.messages); + assert.match(blockView, /Build succeeded with all original checks/); + assert(!blockView.includes("unique original build output"), blockView); + } finally { await h.close(); } +}); + +test("HTTP different parents racing the same child publish exactly one identity", async () => { + const h = await harness(); + try { + await sendModel(h, "other-parent", h.messages); + const a = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const b = (await h.request("/__bili/plugin/snapshot?conversationId=other-parent")).body; + const requestA = forkRequest(a); + const requestB = { ...forkRequest(b), parentConversationId: "other-parent" }; + const responses = await Promise.all([h.request("/__bili/plugin/fork", requestA), h.request("/__bili/plugin/fork", requestB)]); + assert.deepEqual(responses.map((r) => r.status).sort(), [201, 409]); + assert.equal(responses.find((r) => r.status === 409)!.body.code, "CHILD_CONFLICT"); + assert.equal(resolveConversation("child").session!.metadata.parentConversationId, responses[0].status === 201 ? "parent" : "other-parent"); + } finally { await h.close(); } +}); + +test("HTTP fork registration and first model append preserve inherited state and refs", async () => { + const h = await harness(); + try { + await compress(h); + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + assert.equal((await h.request("/__bili/plugin/fork", forkRequest(snapshot))).status, 201); + assert.equal((await h.request("/__bili/plugin/register", { conversationId: "child", agent: "test", identity: true, parentConversationId: "parent" })).status, 200); + await sendModel(h, "child", [...h.messages.slice(0, 2), { role: "assistant", content: "the final response can arrive after the fork" }, { role: "user", content: "child continuation" }]); + const child = resolveConversation("child").session!; + assert.equal(child.id, "child"); + assert.equal(child.state.blocks[0].summary, summary); + assert.equal(child.metadata.derivedFromSessionId, undefined); + const after = (await h.request("/__bili/plugin/snapshot?conversationId=child")).body; + assert.deepEqual(after.orderedMessages.slice(0, 2), snapshot.orderedMessages.slice(0, 2)); + assert.notEqual(after.orderedMessages[2].ref, snapshot.orderedMessages[2].ref); + const original = await h.request("/__bili/plugin/tool", { conversationId: "child", tool: "decompress", args: { blockId: "b1", full: true } }); + assert.match(original.body.result, /first original/); + } finally { await h.close(); } +}); + +test("HTTP nested summaries expand crossing ancestors and retain independent original caches", async () => { + const h = await harness(true); + try { + const history = [...h.messages, { role: "assistant", content: "third response ".repeat(250) }, { role: "user", content: "fourth user turn ".repeat(250) }, { role: "assistant", content: "retained recent tail" }]; + await sendModel(h, "parent", history); + await compress(h); + await compress(h, "parent", "b1", "m00005"); + const parent = resolveConversation("parent").session!; + assert.equal(parent.state.blocks.length, 2); + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const exact = await h.request("/__bili/plugin/fork", forkRequest(snapshot, "nested", 5)); + assert.equal(exact.status, 201, JSON.stringify(exact.body)); + assert.equal(exact.body.status, "exact"); + assert.equal(exact.body.inheritedBlocks.length, 2); + const restored = h.store.loadSync("nested")!; + assert.deepEqual(restored.state.blocks, JSON.parse(JSON.stringify(resolveConversation("nested").session!.state.blocks))); + assert.deepEqual(restored.blockContents, resolveConversation("nested").session!.blockContents); + const expanded = await h.request("/__bili/plugin/fork", forkRequest(snapshot, "nested-prefix", 2)); + assert.equal(expanded.status, 201, JSON.stringify(expanded.body)); + assert.equal(expanded.body.status, "expanded"); + assert.deepEqual(expanded.body.expandedBlocks, ["b2"]); + assert.equal(resolveConversation("nested-prefix").session!.state.blocks[0].expanded, true); + assert.equal((await h.request("/__bili/plugin/fork", forkRequest(snapshot, "nested-sibling", 5))).status, 201); + for (const id of ["nested", "nested-sibling"]) { + await sendModel(h, id, [...history.slice(0, 5), { role: "assistant", content: `${id} first continuation` }]); + const continued = (await h.request(`/__bili/plugin/snapshot?conversationId=${id}`)).body; + assert.deepEqual(continued.orderedMessages.slice(0, 5), snapshot.orderedMessages.slice(0, 5)); + assert.equal(resolveConversation(id).session!.state.blocks[1].summary, summary); + assert.equal(resolveConversation(id).session!.state.blocks[1].active, true); + } + assert.notEqual(resolveConversation("nested").session!.pluginSnapshot![5].text, resolveConversation("nested-sibling").session!.pluginSnapshot![5].text); + assert.notEqual(resolveConversation("nested").session!.blockContents.get("b2"), resolveConversation("nested-sibling").session!.blockContents.get("b2")); + const original = await h.request("/__bili/plugin/tool", { conversationId: "nested", tool: "decompress", args: { blockId: "b2", full: true } }); + const file = original.body.result.match(/written to: (.+)\n/)?.[1]; + const text = file ? readFileSync(file, "utf8") : original.body.result; + if (file) rmSync(file, { force: true }); + assert.match(text, /first original/); + assert.match(text, /third response/); + assert(!text.includes("retained recent tail")); + assert.notEqual(resolveConversation("nested").session!.state.blocks[1].directBlockIds, parent.state.blocks[1].directBlockIds); + } finally { await h.close(); } +}); diff --git a/tests/plugin-status-provenance.test.ts b/tests/plugin-status-provenance.test.ts new file mode 100644 index 0000000000..581ded8206 --- /dev/null +++ b/tests/plugin-status-provenance.test.ts @@ -0,0 +1,227 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { defaultConfig, DEFAULT_CCR_CONFIG } from "acp-kernel"; +import { startServer, type ProxyOptions } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _resetPluginStateForTest, resolveConversation } from "../src/plugin.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; +import { settleUsageReport } from "../src/cache-ledger.ts"; + +process.env.NODE_ENV = "test"; +const root = mkdtempSync(join(tmpdir(), "bili-status-provenance-")); +test.after(() => rmSync(root, { recursive: true, force: true })); +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = root; + +async function harness() { + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + const store = new SessionStore({ dir: join(root, "sessions"), enabled: false }); + _setStoreForTest(store); + _setForTest({}); + let usage: Record | undefined = { input_tokens: 8000, cache_read_input_tokens: 2000, output_tokens: 10 }; + let gate: { entered: () => void; wait: Promise } | undefined; + const upstream = http.createServer((req, res) => { + req.resume(); + req.on("end", async () => { + const pending = gate; + gate = undefined; + if (pending) { pending.entered(); await pending.wait; } + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "msg_status", role: "assistant", content: [{ type: "text", text: "answer" }], ...(usage === undefined ? {} : { usage }) })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const addr = upstream.address(); + assert(addr && typeof addr === "object"); + const upstreamUrl = `http://127.0.0.1:${addr.port}`; + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "claude-test": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: true, preserveRecentMessages: 1, preserveRecentTokens: 0, minCompressRangeChars: 100 }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); + await once(proxy, "listening"); + const paddr = proxy.address(); + assert(paddr && typeof paddr === "object"); + const origin = `http://127.0.0.1:${paddr.port}`; + const messages = [{ role: "user", content: "first original ".repeat(250) }, { role: "assistant", content: "second original ".repeat(250) }, { role: "user", content: "tail original" }]; + const send = async (history: unknown[] = messages) => { + const r = await fetch(`${origin}/bili/${upstreamUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "test", "x-bili-plugin-conversation": "provenance" }, body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages: history }) }); + assert.equal(r.status, 200, await r.text()); + }; + const request = async (path: string, body?: unknown) => { + const r = await fetch(origin + path, body === undefined ? {} : { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); + assert.equal(r.status, 200); + return await r.json(); + }; + const status = () => request("/__bili/plugin/status?conversationId=provenance"); + const tool = (name: string, args: Record) => request("/__bili/plugin/tool", { conversationId: "provenance", tool: name, args }); + const compressArgs = { content: [{ startId: "m00001", endId: "m00002", summary: "The prefix preserves the first user request and second assistant response. Original content remains available for decompression using stable references." }] }; + await send(); + return { status, send, tool, messages, compressArgs, setUsage: (value: typeof usage) => { usage = value; }, holdNext: () => { + let entered!: () => void; + let release!: () => void; + const waiting = new Promise((resolve) => { entered = resolve; }); + const wait = new Promise((resolve) => { release = resolve; }); + gate = { entered, wait }; + return { waiting, release }; + }, close: async () => { store.cancelAll(); proxy.close(); upstream.close(); await Promise.all([once(proxy, "close"), once(upstream, "close")]); } }; +} + +test("HTTP usage -> compress -> decompress -> request keeps context provenance separate from billing", async (t) => { + const h = await harness(); + try { + const before = await h.status(); + const session = resolveConversation("provenance").session!; + const baseline = session.stats.lastUsageGradeTokens; + assert.equal(before.contextTokensSource, "usage"); + assert.equal(before.contextTokens, 10000); + const compressed = await h.tool("compress", h.compressArgs); + assert(!compressed.result.includes("FAILED"), compressed.result); + const folded = await h.status(); + assert.equal(folded.contextTokensSource, "estimate", "manual compression is not a new upstream measurement"); + assert(folded.contextTokens < before.contextTokens); + assert.equal(folded.contextTokens, Math.max(0, before.contextTokens - folded.compressCreditTokens)); + assert(folded.contextTokensAt >= before.contextTokensAt); + assert.notEqual(folded.contextGeneration, before.contextGeneration); + assert.equal(session.stats.lastUsageGradeTokens, baseline); + const inputAfterCompress = session.stats.lastInputTokens; + const restored = await h.tool("decompress", { blockId: "b1", full: true }); + assert.match(restored.result, /first original/); + const expanded = await h.status(); + assert.equal(expanded.contextTokensSource, "estimate"); + assert(expanded.contextTokens > folded.contextTokens, "inline restored material changes the effective context"); + assert.notEqual(expanded.contextGeneration, folded.contextGeneration); + assert.equal(session.stats.lastInputTokens, inputAfterCompress); + assert.equal(session.stats.lastUsageGradeTokens, baseline); + assert.deepEqual([expanded.inputTokens, expanded.cachedTokens], [before.inputTokens, before.cachedTokens]); + h.setUsage({ input_tokens: 6000, cache_read_input_tokens: 1000, output_tokens: 10 }); + const history = [...h.messages, + { role: "assistant", content: [{ type: "tool_use", id: "restore_status", name: "decompress", input: { blockId: "b1", full: true } }] }, + { role: "user", content: [{ type: "tool_result", tool_use_id: "restore_status", content: restored.result }] }, + { role: "user", content: "Continue after restoring the original material." }, + ]; + await h.send(history); + const measured = await h.status(); + assert.equal(measured.contextTokensSource, "usage"); + assert.equal(measured.contextTokens, 7000); + assert.equal(measured.compressCreditTokens, 0); + assert.deepEqual([measured.inputTokens, measured.cachedTokens], [17000, 3000]); + assert(![before, folded, expanded].some((s) => s.contextGeneration === measured.contextGeneration)); + assert.equal((await h.status()).contextGeneration, measured.contextGeneration); + h.setUsage({ input_tokens: 0, output_tokens: 0 }); + await h.send([...history, { role: "assistant", content: "prior reply" }, { role: "user", content: "zero-usage gateway turn" }]); + const zero = await h.status(); + assert.equal(zero.contextTokensSource, "estimate", "zero usage must not rebrand the previous measured generation"); + assert.notEqual(zero.contextGeneration, measured.contextGeneration); + assert.deepEqual([zero.inputTokens, zero.cachedTokens], [17000, 3000]); + assert.equal(session.stats.lastUsageGradeTokens, 7000); + h.setUsage(undefined); + await h.send([...history, { role: "assistant", content: "prior reply" }, { role: "user", content: "no-usage gateway turn" }]); + const missing = await h.status(); + assert.equal(missing.contextTokensSource, "estimate"); + assert.notEqual(missing.contextGeneration, zero.contextGeneration); + t.diagnostic(JSON.stringify(Object.fromEntries(Object.entries({ before, folded, expanded, measured, zero, missing }).map(([step, s]) => [step, { tokens: s.contextTokens, source: s.contextTokensSource, at: s.contextTokensAt, generation: s.contextGeneration, input: s.inputTokens, cached: s.cachedTokens, credit: s.compressCreditTokens }])))); + } finally { await h.close(); } +}); + +test("HTTP pending request cannot expose stale usage as its current context", async () => { + const h = await harness(); + let request: Promise | undefined; + const held = h.holdNext(); + try { + const before = await h.status(); + request = h.send([...h.messages, { role: "assistant", content: "answer" }, { role: "user", content: "another turn" }]); + await held.waiting; + const pending = await h.status(); + assert.equal(pending.contextTokensSource, "estimate"); + assert.notEqual(pending.contextGeneration, before.contextGeneration); + held.release(); + await request; + const after = await h.status(); + assert.equal(after.contextTokensSource, "usage"); + assert.notEqual(after.contextGeneration, pending.contextGeneration); + } finally { held.release(); await request; await h.close(); } +}); + +test("HTTP failed manual tools do not publish a new context observation", async () => { + const h = await harness(); + try { + const before = await h.status(); + for (const [name, args] of [["compress", { content: [{ startId: "m99998", endId: "m99999", summary: "missing references" }] }], ["decompress", { blockId: "not-a-block" }]] as const) { + assert.match((await h.tool(name, args)).result, /FAILED|not found/); + const after = await h.status(); + assert.deepEqual([after.contextTokens, after.contextTokensSource, after.contextTokensAt, after.contextGeneration], [before.contextTokens, before.contextTokensSource, before.contextTokensAt, before.contextGeneration]); + } + } finally { await h.close(); } +}); + +test("HTTP unknown provenance stays unavailable despite a historical usage baseline", async () => { + const h = await harness(); + try { + const session = resolveConversation("provenance").session!; + delete session.metadata.publicContextObservation; + assert.equal(session.stats.lastInputTokensSource, "usage"); + assert.equal(session.stats.contextTokensSource, "usage"); + const unknown = await h.status(); + assert.equal(unknown.contextTokensSource, "unavailable"); + assert.equal(unknown.contextTokens, null); + assert.equal(unknown.contextTokensAt, null); + assert.equal(unknown.contextGeneration, null); + } finally { await h.close(); } +}); + +test("HTTP repeated real usage gets a new generation even within the same timestamp", async (t) => { + const h = await harness(); + try { + t.mock.method(Date, "now", () => 1791049000000); + await h.send(); + const first = await h.status(); + await h.send(); + const second = await h.status(); + assert.equal(second.contextTokensAt, first.contextTokensAt); + assert.equal(second.contextTokens, first.contextTokens); + assert.equal(second.sessionRevision, first.sessionRevision); + assert.notEqual(second.contextGeneration, first.contextGeneration); + assert.equal((await h.status()).contextGeneration, second.contextGeneration); + } finally { t.mock.restoreAll(); await h.close(); } +}); + +test("HTTP a real usage report with outstanding fold credit is still an effective estimate", async () => { + const h = await harness(); + try { + const session = resolveConversation("provenance").session!; + session.stats.compressCreditTokens = 1000; + settleUsageReport(session, { total: 9000, reportedCached: 500, output: 10 }); + const current = await h.status(); + assert.equal(current.contextTokensSource, "estimate"); + assert.equal(current.contextTokens, 8000, "fold credit is applied exactly once"); + assert.deepEqual([current.inputTokens, current.cachedTokens], [19000, 2500]); + assert.equal(session.stats.lastInputTokens, 8000); + assert.equal(session.stats.lastUsageGradeTokens, 8000); + } finally { await h.close(); } +}); + +test("HTTP CCR range decompress accounts for queued restored content without changing the usage baseline", async () => { + const h = await harness(); + try { + const session = resolveConversation("provenance").session!; + session.metadata.effectiveCcr = { ...DEFAULT_CCR_CONFIG, enabled: true }; + await h.tool("compress", h.compressArgs); + const folded = await h.status(); + const baseline = session.stats.lastUsageGradeTokens; + const restored = await h.tool("decompress", { blockId: "b1", startId: "m00001", endId: "m00001" }); + assert.match(restored.result, /restored 1 item/); + assert.equal(session.pendingRetrievals.length, 1); + const current = await h.status(); + assert.equal(current.contextTokensSource, "estimate"); + assert(current.contextTokens > folded.contextTokens); + assert.notEqual(current.contextGeneration, folded.contextGeneration); + assert.equal(session.stats.lastUsageGradeTokens, baseline); + assert.deepEqual([current.inputTokens, current.cachedTokens], [folded.inputTokens, folded.cachedTokens]); + } finally { await h.close(); } +}); \ No newline at end of file diff --git a/tests/tool-ring.test.ts b/tests/tool-ring.test.ts index 28da9f5f86..7d0c0a9548 100644 --- a/tests/tool-ring.test.ts +++ b/tests/tool-ring.test.ts @@ -141,14 +141,14 @@ describe("#1685 handlePluginTool routing ladder", () => { assert.ok(!logs.join("\n").includes(b.id)); }); - it("witness wins over a disagreeing body id", async () => { + it("a disagreeing witness fails closed without overriding the body id", async () => { const a = getSession(`t-1685-xa-${Math.random().toString(36).slice(2)}`); const b = getSession(`t-1685-xb-${Math.random().toString(36).slice(2)}`); recordToolWitness(a.id, "acp_status", "{}"); const r = await post({ conversationId: b.id, tool: "acp_status", args: {} }); - assert.equal(r.status, 200); - assert.deepEqual(executedOn(), [a.id]); - assert.match(logs.join("\n"), /differs and was ignored \(#1685\)/); + assert.equal(r.status, 409); + assert.equal(r.json.code, "TOOL_CONVERSATION_CONFLICT"); + assert.deepEqual(executedOn(), []); }); it("collision without a body id is a loud 400 with the count", async () => { From f44dd24025b3615fae69711ea9a331684335fb97 Mon Sep 17 00:00:00 2001 From: YZSQ <187964595+yingzhongshuoqi@users.noreply.github.com> Date: Sun, 4 Oct 2026 02:34:33 +0800 Subject: [PATCH 02/10] test: keep fork and provenance fixtures type safe --- tests/plugin-fork.test.ts | 60 +++++++++++++++++++++++--- tests/plugin-status-provenance.test.ts | 29 ++++++++++--- 2 files changed, 78 insertions(+), 11 deletions(-) diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index 4f93f4b5e0..147ddbb111 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -8,7 +8,8 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { spawn } from "node:child_process"; import { applyAbsorb, createContentStore, defaultConfig, DEFAULT_ABSORB_CONFIG, DEFAULT_CCR_CONFIG, storeOriginal } from "acp-kernel"; -import { startServer, type ProxyOptions } from "../src/server.ts"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _resetPluginStateForTest, rememberPluginMessages, resolveConversation } from "../src/plugin.ts"; import { _resetSessionsForTest, getSession } from "../src/session.ts"; @@ -21,6 +22,51 @@ test.after(() => rmSync(testRoot, { recursive: true, force: true })); for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = testRoot; const hash = (v: unknown) => createHash("sha256").update(JSON.stringify(v)).digest("hex"); +interface SnapshotResponse { + status: string; + code?: string; + sessionId: string; + parentRevision: string; + orderedMessages: { rawId: string; ref: string; identityHash: string }[]; + messages: { text?: string; ref: string; toolIsError: boolean }[]; +} + +interface ForkResponse { + status: string; + code?: string; + childConversationId: string; + inheritedBlocks: { id: string }[]; + expandedBlocks: string[]; + replayed: boolean; +} + +interface StatusResponse { + sessionId: string | null; + sessionRevision: string | null; + fallback?: boolean; + contextTokensSource: "usage" | "estimate" | "unavailable"; + contextTokens: number | null; + contextTokensAt: number | null; + contextGeneration: string | null; + compressCreditTokens: number; + inputTokens: number; + contextLimit: number | null; +} + +type PluginResponse = Path extends "/__bili/plugin/manifest" + ? { capabilities: { fork: { protocolVersion: number; endpoint: string; snapshotEndpoint: string } } } + : Path extends "/__bili/plugin/fork" ? ForkResponse + : Path extends "/__bili/plugin/tool" ? { result: string; code?: string } + : Path extends `/__bili/plugin/status${string}` ? StatusResponse + : SnapshotResponse; + +async function responseBody(response: Response, _path: Path): Promise> { + const body: unknown = await response.json(); + assert(body !== null && typeof body === "object" && !Array.isArray(body)); + // Success fields follow the endpoint fixture; failures are checked via status/code. + return body as PluginResponse; +} + async function harness(persist = false) { const dir = mkdtempSync(join(testRoot, "run-")); for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; @@ -50,9 +96,9 @@ async function harness(persist = false) { const paddr = proxy.address(); assert(paddr && typeof paddr === "object"); const origin = `http://127.0.0.1:${paddr.port}`; - const request = async (path: string, body?: unknown) => { + const request = async (path: Path, body?: unknown) => { const r = await fetch(origin + path, body === undefined ? {} : { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); - return { status: r.status, body: await r.json() }; + return { status: r.status, body: await responseBody(r, path) }; }; const messages = [{ role: "user", content: "first original ".repeat(250) }, { role: "assistant", content: "second original ".repeat(250) }, { role: "user", content: "tail original" }]; const r = await fetch(`${origin}/bili/${upstreamUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "test", "x-bili-plugin-conversation": "parent" }, body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages }) }); @@ -65,7 +111,7 @@ test("plain proxy panel snapshots do not change raw-history persistence", async const h = await harness(); try { const session = getSession("plain-panel"); - const messages = [{ id: "plain-message", role: "user" as const, text: "raw plain-proxy original" }]; + const messages = [{ id: "plain-message", role: "user" as const, contentType: "text" as const, text: "raw plain-proxy original" }]; rememberPluginMessages(session.id, messages, messages); assert.equal(session.pluginSnapshot, undefined); assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=plain-panel")).status, 409); @@ -341,9 +387,9 @@ test("HTTP fork revision and originals survive a new proxy process", async () => }); child!.once("exit", (code) => { clearTimeout(timer); reject(new Error(`restart exited ${code}: ${stderr}`)); }); }); - const call = async (path: string, body?: unknown) => { + const call = async (path: Path, body?: unknown) => { const r = await fetch(`http://127.0.0.1:${port}` + path, body === undefined ? {} : { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); - return { status: r.status, body: await r.json() }; + return { status: r.status, body: await responseBody(r, path) }; }; const restored = await call("/__bili/plugin/snapshot?conversationId=child"); const restoredStatus = await call("/__bili/plugin/status?conversationId=parent&fallback=latest"); @@ -378,6 +424,7 @@ test("HTTP status separates real usage from a manual-compression estimate and ch const before = await h.request("/__bili/plugin/status?conversationId=parent"); assert.equal(before.body.contextTokensSource, "usage"); assert.equal(before.body.contextTokens, 10000); + assert(typeof before.body.contextTokensAt === "number"); assert(before.body.contextTokensAt > 0); assert.equal(typeof before.body.contextGeneration, "string"); const again = await h.request("/__bili/plugin/status?conversationId=parent"); @@ -390,6 +437,7 @@ test("HTTP status separates real usage from a manual-compression estimate and ch assert(after.body.compressCreditTokens > 0); assert.equal(after.body.contextTokensSource, "estimate"); assert.equal(after.body.contextTokens, Math.max(0, 10000 - after.body.compressCreditTokens)); + assert(typeof after.body.contextTokensAt === "number"); assert(after.body.contextTokensAt >= before.body.contextTokensAt); assert.notEqual(after.body.contextGeneration, before.body.contextGeneration); assert.equal(after.body.inputTokens, 10000); diff --git a/tests/plugin-status-provenance.test.ts b/tests/plugin-status-provenance.test.ts index 581ded8206..6eeb0daa31 100644 --- a/tests/plugin-status-provenance.test.ts +++ b/tests/plugin-status-provenance.test.ts @@ -6,7 +6,8 @@ import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { defaultConfig, DEFAULT_CCR_CONFIG } from "acp-kernel"; -import { startServer, type ProxyOptions } from "../src/server.ts"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _resetPluginStateForTest, resolveConversation } from "../src/plugin.ts"; import { _resetSessionsForTest } from "../src/session.ts"; @@ -19,6 +20,17 @@ const root = mkdtempSync(join(tmpdir(), "bili-status-provenance-")); test.after(() => rmSync(root, { recursive: true, force: true })); for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = root; +interface StatusResponse { + contextTokensSource: "usage" | "estimate" | "unavailable"; + contextTokens: number | null; + contextTokensAt: number | null; + contextGeneration: string | null; + sessionRevision: string | null; + compressCreditTokens: number; + inputTokens: number; + cachedTokens: number; +} + async function harness() { _resetSessionsForTest(); _resetPluginStateForTest(); @@ -53,13 +65,15 @@ async function harness() { const r = await fetch(`${origin}/bili/${upstreamUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "test", "x-bili-plugin-conversation": "provenance" }, body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages: history }) }); assert.equal(r.status, 200, await r.text()); }; - const request = async (path: string, body?: unknown) => { + const request = async (path: string, body?: unknown): Promise => { const r = await fetch(origin + path, body === undefined ? {} : { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); assert.equal(r.status, 200); - return await r.json(); + const response: unknown = await r.json(); + assert(response !== null && typeof response === "object" && !Array.isArray(response)); + return response as T; }; - const status = () => request("/__bili/plugin/status?conversationId=provenance"); - const tool = (name: string, args: Record) => request("/__bili/plugin/tool", { conversationId: "provenance", tool: name, args }); + const status = () => request("/__bili/plugin/status?conversationId=provenance"); + const tool = (name: string, args: Record) => request<{ result: string }>("/__bili/plugin/tool", { conversationId: "provenance", tool: name, args }); const compressArgs = { content: [{ startId: "m00001", endId: "m00002", summary: "The prefix preserves the first user request and second assistant response. Original content remains available for decompression using stable references." }] }; await send(); return { status, send, tool, messages, compressArgs, setUsage: (value: typeof usage) => { usage = value; }, holdNext: () => { @@ -80,10 +94,12 @@ test("HTTP usage -> compress -> decompress -> request keeps context provenance s const baseline = session.stats.lastUsageGradeTokens; assert.equal(before.contextTokensSource, "usage"); assert.equal(before.contextTokens, 10000); + assert(typeof before.contextTokens === "number" && typeof before.contextTokensAt === "number"); const compressed = await h.tool("compress", h.compressArgs); assert(!compressed.result.includes("FAILED"), compressed.result); const folded = await h.status(); assert.equal(folded.contextTokensSource, "estimate", "manual compression is not a new upstream measurement"); + assert(typeof folded.contextTokens === "number" && typeof folded.contextTokensAt === "number"); assert(folded.contextTokens < before.contextTokens); assert.equal(folded.contextTokens, Math.max(0, before.contextTokens - folded.compressCreditTokens)); assert(folded.contextTokensAt >= before.contextTokensAt); @@ -94,6 +110,7 @@ test("HTTP usage -> compress -> decompress -> request keeps context provenance s assert.match(restored.result, /first original/); const expanded = await h.status(); assert.equal(expanded.contextTokensSource, "estimate"); + assert(typeof expanded.contextTokens === "number"); assert(expanded.contextTokens > folded.contextTokens, "inline restored material changes the effective context"); assert.notEqual(expanded.contextGeneration, folded.contextGeneration); assert.equal(session.stats.lastInputTokens, inputAfterCompress); @@ -214,11 +231,13 @@ test("HTTP CCR range decompress accounts for queued restored content without cha await h.tool("compress", h.compressArgs); const folded = await h.status(); const baseline = session.stats.lastUsageGradeTokens; + assert(typeof folded.contextTokens === "number"); const restored = await h.tool("decompress", { blockId: "b1", startId: "m00001", endId: "m00001" }); assert.match(restored.result, /restored 1 item/); assert.equal(session.pendingRetrievals.length, 1); const current = await h.status(); assert.equal(current.contextTokensSource, "estimate"); + assert(typeof current.contextTokens === "number"); assert(current.contextTokens > folded.contextTokens); assert.notEqual(current.contextGeneration, folded.contextGeneration); assert.equal(session.stats.lastUsageGradeTokens, baseline); From 18c58a5b1fa75411f0b72158c3815bce8ce88801 Mon Sep 17 00:00:00 2001 From: YZSQ <187964595+yingzhongshuoqi@users.noreply.github.com> Date: Sun, 4 Oct 2026 04:25:09 +0800 Subject: [PATCH 03/10] fix(plugin): preserve attributable outbound context and retained originals --- PLUGIN.md | 2 + src/cache-ledger.ts | 7 +- src/persist.ts | 20 +- src/plugin.ts | 17 +- src/server.ts | 27 ++- tests/codex-responses-ws.test.ts | 6 +- tests/context-provenance-ledger.test.ts | 26 +++ tests/context-provenance-outbound.test.ts | 136 ++++++++++++ ...ssue1987-preflight-baseline-anchor.test.ts | 12 +- tests/issue1993-preflight-400.test.ts | 3 +- tests/plugin-fork.test.ts | 206 +++++++++++++++++- tests/ws-bridge.test.ts | 4 +- 12 files changed, 439 insertions(+), 27 deletions(-) create mode 100644 tests/context-provenance-ledger.test.ts create mode 100644 tests/context-provenance-outbound.test.ts diff --git a/PLUGIN.md b/PLUGIN.md index d63d16ef4b..b9552d2869 100644 --- a/PLUGIN.md +++ b/PLUGIN.md @@ -130,6 +130,8 @@ Forwarding a new model request replaces the previous observation with an estimat `contextTokensAt` is the observation time in Unix milliseconds (not the status read time), independently of the last usage timestamp. `contextGeneration` is an opaque change detector covering a unique observation id, context, revision, model and limit: successive observations remain distinct even at the same timestamp with identical token counts; repeated status reads do not create observations. Unavailable context has null tokens, timestamp and generation. Legacy persisted sessions without an independently attributed observation are unavailable until another request establishes one. `sessionId` and nullable `sessionRevision` identify the resolved session and its fork state. Pre-first-request/chain-only responses explicitly report unavailable context. Persisted sessions are loaded by the requested id before considering an explicitly requested `fallback=latest`. Errors: `400` missing `conversationId`, `404` unknown conversation. +`compressibleRanges` is the live kernel recommendation as structured `{startRef, endRef, count, ...}` entries for `sessionRevision`, or `null` if it cannot be computed. An empty list means there is no recommended range. Read the exact conversation without `fallback=latest`, match the snapshot revision and ordered refs, and submit manual `compress` with `expectedRevision`; never parse the human panel or guess a fixed prefix. Hosts must exclude their retained first-user anchor from summaries: kernel pruning retains it even if a recommendation spans it. A concurrent mutation is rejected by the revision guard. + ### 5. `POST /__bili/plugin/compact` Notify the proxy that the agent performed an **in-session native compaction** (e.g. omp's `/compact` or its auto threshold): the next model request re-sends a shortened history (compaction summary + retained tail) under the SAME conversation id. Fire-and-forget is fine — a failed notification must never break the agent's compaction. diff --git a/src/cache-ledger.ts b/src/cache-ledger.ts index 62310c928e..948b6beefe 100644 --- a/src/cache-ledger.ts +++ b/src/cache-ledger.ts @@ -215,14 +215,15 @@ export interface ContextObservation { /** Public context provenance is separate from billing and nudge baselines. */ export function recordContextObservation(session: Session, tokens: number, source: ContextObservation["source"]): void { if (!Number.isFinite(tokens) || tokens < 0) { - delete session.metadata.publicContextObservation; + delete session.metadata?.publicContextObservation; return; } + session.metadata ??= {}; session.metadata.publicContextObservation = { sessionId: session.id, tokens, source, at: Date.now(), generation: randomUUID() } satisfies ContextObservation; } export function currentContextObservation(session: Session): ContextObservation | undefined { - const raw = session.metadata.publicContextObservation; + const raw = session.metadata?.publicContextObservation; if (!raw || typeof raw !== "object") return undefined; const o = raw as Record; if (o.sessionId !== session.id || typeof o.tokens !== "number" || !Number.isFinite(o.tokens) || o.tokens < 0 || @@ -245,7 +246,7 @@ export function noteForwardedBody(session: Session, body: string): void { seamLastSent.set(session, body.length > SEAM_BODY_CAP ? body.slice(0, SEAM_BODY_CAP) : body); // prepare may reuse a measured baseline; this new payload is not measured yet. if (session.stats.contextTokensSource !== undefined) recordContextObservation(session, session.stats.contextTokens, "estimate"); - else delete session.metadata.publicContextObservation; + else delete session.metadata?.publicContextObservation; } // #1843 L1: learned per-route image cost. The prior (pixel tile model or bytes) diff --git a/src/persist.ts b/src/persist.ts index fb2e87d4d7..8675109bca 100644 --- a/src/persist.ts +++ b/src/persist.ts @@ -11,6 +11,7 @@ import { PersistEpermAlert } from "./persist-eperm.js"; import { createInitialState, defaultCountTokens, prune, type CompressionState, type CoreMessage, type MessageContentStore } from "acp-kernel"; import type { Session, BlockContent, BlockView } from "./session.js"; import type { WireProtocol } from "./util.js"; +import { currentContextObservation } from "./cache-ledger.js"; /** * On-disk persistence for proxy sessions. @@ -675,12 +676,24 @@ export class SessionStore { function buildRecord(session: Session): PersistedSession { const snapshot = boundedFoldedSnapshot(session); + const observation = currentContextObservation(session); + if (session.pluginSnapshot && session.contentStore) { + const previous = session.metadata.publicSnapshotStoredRefs; + session.metadata.publicSnapshotStoredRefs = [...new Set([ + ...(Array.isArray(previous) ? previous : []), + ...session.pluginSnapshot.flatMap((m) => { + const ref = session.state.messageRefs.byRaw[m.id]; + return ref && session.contentStore!.byRef[ref] ? [ref] : []; + }), + ])]; + } return { version: PERSIST_VERSION, savedAt: Date.now(), id: session.id, meta: { ...session.meta }, - stats: { ...session.stats }, + // Credits are one-shot and are not restored; persist the effective view instead. + stats: { ...session.stats, ...(observation ? { contextTokens: observation.tokens, contextTokensSource: observation.source } : {}) }, messages: snapshot, messagesFolded: snapshot ? true : undefined, pluginSnapshot: session.pluginSnapshot, @@ -688,10 +701,7 @@ function buildRecord(session: Session): PersistedSession { // Per-session provenance: record the bili build that wrote this file so the // web UI can show which version last touched the session; pre-stamp files // load without the key and render an honest dash. - metadata: { ...session.metadata, ...(session.pluginSnapshot && session.contentStore ? { publicSnapshotStoredRefs: session.pluginSnapshot.flatMap((m) => { - const ref = session.state.messageRefs.byRaw[m.id]; - return ref && session.contentStore!.byRef[ref] ? [ref] : []; - }) } : {}), biliVersion: VERSION }, + metadata: { ...session.metadata, biliVersion: VERSION }, state: session.state, blockContents: Object.fromEntries(session.blockContents), createdAt: session.createdAt, diff --git a/src/plugin.ts b/src/plugin.ts index 3672890b44..b5a451dbff 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -336,6 +336,14 @@ export function rememberPluginMessages(sessionId: string, processed: CoreMessage const session = peekSession(sessionId); if (session && typeof session.metadata.pluginAgent === "string" && original.length > 0) { session.pluginSnapshot = structuredClone(original); + const previousRefs = session.metadata.publicSnapshotStoredRefs; + session.metadata.publicSnapshotStoredRefs = [...new Set([ + ...(Array.isArray(previousRefs) ? previousRefs : []), + ...original.flatMap((m) => { + const ref = session.state.messageRefs.byRaw[m.id]; + return ref && session.contentStore?.byRef[ref] ? [ref] : []; + }), + ])]; if (rawWire !== undefined) { try { const wire = JSON.parse(rawWire.toString("utf8")) as Record; @@ -710,6 +718,7 @@ function comparableHistory(value: unknown): boolean { const part = value as Record; if (part.type !== undefined && !["message", "text", "input_text", "output_text", "tool_use", "tool_result", "function_call", "function_call_output"].includes(String(part.type))) return false; if (part.type === "tool_use" || part.type === "function_call") return true; + if (part.audio !== undefined && part.audio !== null) return false; if (part.content !== undefined) return comparableHistory(part.content); if (part.output !== undefined) return comparableHistory(part.output); if (part.parts !== undefined) return comparableHistory(part.parts); @@ -774,11 +783,15 @@ function forkSnapshot(session: Session) { const store = contentStoreOf(session); const expectedStoredRefs = session.metadata.publicSnapshotStoredRefs; if (Array.isArray(expectedStoredRefs) && expectedStoredRefs.some((ref) => typeof ref !== "string" || !store.byRef[ref])) throw new Error("CCR original index unavailable"); + const indexedHashes = new Set(Object.values(store.byRef).map((entry) => entry.hash)); + if (Object.entries(store.byHash).some(([hash, text]) => !indexedHashes.has(hash) || typeof text !== "string" || createHash("sha256").update(text, "utf8").digest("hex") !== hash)) throw new Error("CCR original payload/index inconsistent"); + if (Object.entries(store.byRef).some(([ref, entry]) => typeof store.byHash[entry.hash] !== "string" || session.state.messageRefs.byRaw[entry.rawId] !== ref || session.state.messageRefs.byRef[ref] !== entry.rawId)) throw new Error("CCR original alias inconsistent"); const orderedMessages = messages.map((m): ForkIdentity => { const ref = session.state.messageRefs.byRaw[m.id]; if (!ref || session.state.messageRefs.byRef[ref] !== m.id) throw new Error("raw/ref mapping inconsistent"); const entry = store.byRef[ref]; - if ((entry && (entry.rawId !== m.id || typeof store.byHash[entry.hash] !== "string")) || (m.text && parseStoredPlaceholder(m.text)?.ref === ref && !entry)) throw new Error("CCR original unavailable or alias inconsistent"); + const placeholder = m.text ? parseStoredPlaceholder(m.text) : null; + if ((entry && (entry.rawId !== m.id || typeof store.byHash[entry.hash] !== "string")) || (placeholder && (placeholder.ref !== ref || !entry))) throw new Error("CCR original unavailable or alias inconsistent"); return { rawId: m.id, ref, identityHash: forkMessageIdentityHash(m) }; }); const state = { ...session.state, imageFullRestored: session.state.imageFullRestored ?? [], imageShrinks: session.state.imageShrinks ?? [] }; @@ -917,7 +930,6 @@ export async function handlePluginFork(payload: string, res: ServerResponse): Pr child.metadata.parentRevision = request.parentRevision; child.stats.contextTokens = prune(child.pluginSnapshot, child.state).reduce((sum, m) => sum + countMessageTokens(m), 0) + (typeof child.metadata.systemPromptTokens === "number" ? child.metadata.systemPromptTokens : 0); child.stats.contextTokensSource = "estimate"; - child.metadata.contextTokensAt = Date.now(); recordContextObservation(child, child.stats.contextTokens, "estimate"); const response = { ok: true, status: crossing.length > 0 ? "expanded" : "exact", protocolVersion: 1, parentConversationId: request.parentConversationId, childConversationId: request.childConversationId, sessionId: child.id, parentRevision: request.parentRevision, childRevision: forkSnapshot(child).parentRevision, branchPoint: request.branchPoint, expandedBlocks: crossing.map((b) => b.blockId), inheritedBlocks: child.state.blocks.map((b) => ({ id: b.blockId, tier: b.tier, active: b.active, expanded: b.expanded === true })), replayed: false }; child.metadata.publicForkReceipt = { requestHash, response }; @@ -1164,6 +1176,7 @@ export function handlePluginStatus(conversationId: string, res: import("node:htt cachedTokens: session.stats.cachedTokens, requests: session.stats.requests, blocks: session.state.blocks.map((b) => ({ id: b.blockId, tier: b.tier, active: b.active })), + compressibleRanges: nudge?.compressibleRanges ?? null, panel, webUrl: webUrl ?? null, lastSeen: session.lastSeen, diff --git a/src/server.ts b/src/server.ts index cbc4166b79..7a99dbf485 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3827,7 +3827,6 @@ async function prepareAnthropic( nudge = turn.nudge; session.stats.contextTokens = tokenCount; session.stats.contextTokensSource = tokenCountSource; - session.metadata.contextTokensAt = Date.now(); if (!session.meta.title) { const t = deriveTitle(msgs); if (t) session.meta.title = t; @@ -4073,7 +4072,6 @@ async function prepareOpenai( nudge = turn.nudge; session.stats.contextTokens = tokenCount; session.stats.contextTokensSource = tokenCountSource; - session.metadata.contextTokensAt = Date.now(); if (!session.meta.title) { const t = deriveTitle(msgs); if (t) session.meta.title = t; @@ -4340,7 +4338,6 @@ async function prepareGoogle( nudge = turn.nudge; session.stats.contextTokens = tokenCount; session.stats.contextTokensSource = tokenCountSource; - session.metadata.contextTokensAt = Date.now(); if (!session.meta.title) { const t = deriveTitle(msgs); if (t) session.meta.title = t; @@ -4648,7 +4645,6 @@ async function prepareResponses( nudge = turn.nudge; session.stats.contextTokens = tokenCount; session.stats.contextTokensSource = tokenCountSource; - session.metadata.contextTokensAt = Date.now(); if (!session.meta.title) { const t = deriveTitle(msgs); if (t) session.meta.title = t; @@ -5524,6 +5520,21 @@ function outboundPayloadBreakdown( return { textEstimate, overheadEstimate, imageTokens, payloadEstimate: textEstimate + overheadEstimate + imageTokens, armEstimate }; } +function outboundContextEstimate(prepared: Prepared, wireBody: string, opts: ProxyOptions, upstream: string): number { + let msgs = prepared.processedMessages.length > 0 ? prepared.processedMessages : prepared.originalMessages; + try { + const parsed = JSON.parse(wireBody); + switch (prepared.protocol) { + case "anthropic": msgs = anthropicToCore(parsed as AnthropicRequestBody).msgs; break; + case "openai": msgs = openaiToCore(parsed as OpenAIRequestBody).msgs; break; + case "responses": msgs = responsesToCore(parsed as ResponsesRequestBody).msgs; break; + case "google": msgs = googleToCore(parsed as GoogleRequestBody).msgs; break; + } + } catch { /* Preserve the prepared projection if the wire codec cannot project a provider extension. */ } + return estimateCoreMessagesUpper(msgs) + estimateWireOverhead(prepared.protocol, wireBody) + + imageReserveFor(prepared.session, prepared.protocol, wireBody, opts, upstream); +} + async function preflightCompressIfNeeded( prepared: Prepared, runPrepare: () => Promise, @@ -6238,7 +6249,13 @@ async function forward( // (loop/core.ts fetchUpstream); side requests never settle usage and must // not clobber the slot. if (prepared?.session && !prepared.sidePassthrough && req.method !== "GET" && req.method !== "HEAD") { - noteForwardedBody(prepared.session, typeof wireBody === "string" ? wireBody : wireBody.toString("utf8")); + const sentBody = typeof wireBody === "string" ? wireBody : wireBody.toString("utf8"); + // Publish this send, not a historical usage baseline with a fresh timestamp. + const estimate = outboundContextEstimate(prepared, sentBody, opts, upstreamUrl); + prepared.session.stats.localInputEstimate = estimate; + prepared.session.stats.contextTokens = estimate; + prepared.session.stats.contextTokensSource = "estimate"; + noteForwardedBody(prepared.session, sentBody); } let upstreamResult: Awaited>; try { diff --git a/tests/codex-responses-ws.test.ts b/tests/codex-responses-ws.test.ts index adebd3b3e5..e4591daf49 100644 --- a/tests/codex-responses-ws.test.ts +++ b/tests/codex-responses-ws.test.ts @@ -141,7 +141,7 @@ test("codex WS: prewarm probe plus full-replay turns ride the prefix lane throug assert.match(String(f.rows[0].headers["user-agent"]), /^codex_exec\//); // The real turn is a full replay the pipeline processed: bili's ACP // tools ride alongside codex's own (empty) tool list. - assert.ok(Array.isArray(f.rows[1].request.tools) && (f.rows[1].request.tools as Item[]).some(t => t.name === "acp_status" || t.name === "compress"), JSON.stringify(f.rows[1].request.tools?.map?.((t: Item) => t.name))); + assert.ok(Array.isArray(f.rows[1].request.tools) && (f.rows[1].request.tools as Item[]).some(t => t.name === "acp_status" || t.name === "compress"), JSON.stringify(f.rows[1].request.tools)); assert.ok(JSON.stringify(f.rows[1].request.input).includes("codex-first-turn")); const session = peekSession(f.sid); assert.ok(session); @@ -189,7 +189,7 @@ test("codex WS: folding covered history shrinks the replayed turn", { timeout: 3 }; await play("session purpose: codex prefix-lane ACP integration"); await play("OLD-BULKY-SENTINEL " + "unique filler content ".repeat(1200)); - const firstText = JSON.stringify(f.rows.at(-1)!.request.input.find(item => JSON.stringify(item).includes("OLD-BULKY-SENTINEL"))); + const firstText = JSON.stringify((f.rows.at(-1)!.request.input as Item[]).find(item => JSON.stringify(item).includes("OLD-BULKY-SENTINEL"))); const ref = firstText.match(/\x3cacp[^\x3e]*\x3e(m\d+)\x3c\/acp\x3e/)?.[1]; assert.ok(ref, firstText.slice(-300)); for (let i = 0; i < 4; i++) await play(`push ${i}`); @@ -239,6 +239,6 @@ test("codex WS: non-streaming transport options are refused with a protocol erro f.peer.send(f.frame([user("nope")], { stream: false })); const error = await done; assert.equal(error.type, "error"); - assert.equal(error.error.code, "invalid_request"); + assert.equal((error.error as Item).code, "invalid_request"); } finally { await f.close(); } }); diff --git a/tests/context-provenance-ledger.test.ts b/tests/context-provenance-ledger.test.ts new file mode 100644 index 0000000000..6fcb2a9984 --- /dev/null +++ b/tests/context-provenance-ledger.test.ts @@ -0,0 +1,26 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { currentContextObservation, noteForwardedBody, recordContextObservation } from "../src/cache-ledger.ts"; +import type { Session } from "../src/session.ts"; + +function lightweightSession(): Session { + return { id: "lightweight-context", stats: {} } as unknown as Session; +} + +test("context observations initialize metadata for lightweight pipeline sessions", () => { + const session = lightweightSession(); + assert.equal(currentContextObservation(session), undefined); + recordContextObservation(session, 123, "usage"); + const observation = currentContextObservation(session); + assert.equal(observation?.sessionId, session.id); + assert.equal(observation?.tokens, 123); + assert.equal(observation?.source, "usage"); + assert.ok(observation && observation.at > 0 && observation.generation.length > 0); +}); + +test("unmeasured forwards and invalid observations do not require metadata", () => { + const session = lightweightSession(); + assert.doesNotThrow(() => noteForwardedBody(session, "unmeasured")); + assert.doesNotThrow(() => recordContextObservation(session, NaN, "estimate")); + assert.equal(currentContextObservation(session), undefined); +}); \ No newline at end of file diff --git a/tests/context-provenance-outbound.test.ts b/tests/context-provenance-outbound.test.ts new file mode 100644 index 0000000000..07bde7850c --- /dev/null +++ b/tests/context-provenance-outbound.test.ts @@ -0,0 +1,136 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +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 { _resetPluginStateForTest, resolveConversation } from "../src/plugin.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; + +const root = mkdtempSync(join(tmpdir(), "bili-outbound-provenance-")); +test.after(() => rmSync(root, { recursive: true, force: true })); +for (const key of ["HOME", "APPHOME", "HERMES_HOME", "XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = root; +process.env.NODE_ENV = "test"; + +type Wire = "anthropic" | "openai" | "responses" | "google"; +interface Status { + contextTokens: number; + contextTokensSource: string; + contextTokensAt: number; + contextGeneration: string; + inputTokens: number; +} + +async function harness(wire: Wire) { + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + _setForTest({}); + const store = new SessionStore({ dir: join(root, "sessions"), enabled: false }); + _setStoreForTest(store); + let usage = false; + let fail = false; + const forwarded: Record[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + forwarded.push(JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record); + res.writeHead(fail ? 503 : 200, { "content-type": "application/json" }); + if (fail) { res.end(JSON.stringify({ error: { message: "isolated fixture failure" } })); return; } + const bodies = { + anthropic: { id: "msg_fixture", role: "assistant", content: [{ type: "text", text: "answer" }], ...(usage ? { usage: { input_tokens: 10000, output_tokens: 10 } } : {}) }, + openai: { id: "chat_fixture", choices: [{ message: { role: "assistant", content: "answer" }, finish_reason: "stop" }], ...(usage ? { usage: { prompt_tokens: 10000, completion_tokens: 10 } } : {}) }, + responses: { id: "resp_fixture", object: "response", status: "completed", output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: "answer" }] }], ...(usage ? { usage: { input_tokens: 10000, output_tokens: 10 } } : {}) }, + google: { candidates: [{ content: { role: "model", parts: [{ text: "answer" }] }, finishReason: "STOP" }], ...(usage ? { usageMetadata: { promptTokenCount: 10000, candidatesTokenCount: 10 } } : {}) }, + }; + res.end(JSON.stringify(bodies[wire])); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const addr = upstream.address(); + assert(addr && typeof addr === "object"); + const upstreamUrl = `http://127.0.0.1:${addr.port}`; + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "model-test": { context: 400000 }, "model-switched": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: true, preserveRecentMessages: 1, preserveRecentTokens: 0, minCompressRangeChars: 100 }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); + await once(proxy, "listening"); + const paddr = proxy.address(); + assert(paddr && typeof paddr === "object"); + const origin = `http://127.0.0.1:${paddr.port}`; + const send = async (texts: string[], model = "model-test") => { + const messages = texts.map((content, i) => ({ role: i % 2 === 0 ? "user" : "assistant", content })); + const body = wire === "google" ? { contents: texts.map((text, i) => ({ role: i % 2 === 0 ? "user" : "model", parts: [{ text }] })), generationConfig: { maxOutputTokens: 1024 } } + : wire === "responses" ? { model, input: messages, max_output_tokens: 1024, stream: false } + : { model, messages, max_tokens: 1024, stream: false }; + const endpoint = wire === "anthropic" ? "/v1/messages" : wire === "openai" ? "/v1/chat/completions" : wire === "responses" ? "/v1/responses" : `/v1beta/models/${model}:generateContent`; + const r = await fetch(`${origin}/bili/${upstreamUrl}${endpoint}`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "fixture", "x-bili-plugin-conversation": "outbound" }, body: JSON.stringify(body) }); + assert.equal(r.status, fail ? 503 : 200, await r.text()); + }; + const status = async (): Promise => { + const r = await fetch(origin + "/__bili/plugin/status?conversationId=outbound"); + assert.equal(r.status, 200); + return await r.json() as Status; + }; + return { send, status, forwarded, setUsage: (value: boolean) => { usage = value; }, setFail: () => { fail = true; }, close: async () => { store.cancelAll(); proxy.close(); upstream.close(); await Promise.all([once(proxy, "close"), once(upstream, "close")]); } }; +} + +for (const wire of ["anthropic", "openai", "responses", "google"] as const) { + test(`HTTP ${wire} first no-usage publishes this outbound estimate`, async (t) => { + const h = await harness(wire); + try { + t.mock.method(Date, "now", () => 1791049001000); + await h.send(["first payload ".repeat(750)]); + const s = await h.status(); + const session = resolveConversation("outbound").session!; + assert.equal(s.contextTokensSource, "estimate"); + assert(s.contextTokens >= 10500, JSON.stringify(s)); + assert.equal(s.contextTokens, session.stats.localInputEstimate); + assert.equal(s.contextTokensAt, 1791049001000); + assert.equal(session.metadata.contextTokensAt, undefined, "no usage timestamp without usage"); + assert.equal(s.inputTokens, 0); + assert.equal(h.forwarded.length, 1); + } finally { await h.close(); } + }); + + for (const scenario of ["no-usage", "upstream-failure", "model-switch"] as const) { + test(`HTTP ${wire} ${scenario} replaces old usage with a growing outbound estimate`, async (t) => { + const h = await harness(wire); + try { + let now = 1791049002000; + t.mock.method(Date, "now", () => now); + const history = ["first payload ".repeat(750)]; + h.setUsage(true); + await h.send(history); + const before = await h.status(); + assert.equal(before.contextTokensSource, "usage"); + assert.equal(before.contextTokens, 10000); + const session = resolveConversation("outbound").session!; + const usageAt = session.metadata.contextTokensAt; + assert.equal(usageAt, now); + h.setUsage(false); + if (scenario === "upstream-failure") h.setFail(); + now = 1791049003000; + await h.send([...history, "prior response", "x".repeat(7500)], scenario === "model-switch" ? "model-switched" : "model-test"); + const after = await h.status(); + assert.equal(after.contextTokensSource, "estimate"); + assert(after.contextTokens > 18000, JSON.stringify(after)); + assert.equal(after.contextTokens, session.stats.localInputEstimate); + assert.equal(after.contextTokensAt, now); + assert.notEqual(after.contextGeneration, before.contextGeneration); + assert.equal(session.metadata.contextTokensAt, usageAt, "prepare cannot refresh the last real usage time"); + assert.equal(session.stats.lastUsageGradeTokens, 10000); + assert.equal(session.stats.lastInputTokensSource, scenario === "upstream-failure" ? "estimate" : "usage"); + assert.equal(after.inputTokens, 10000); + assert.equal(h.forwarded.length, 2); + t.diagnostic(JSON.stringify({ wire, scenario, before: before.contextTokens, after: after.contextTokens, source: after.contextTokensSource, usageAt, estimateAt: after.contextTokensAt })); + } finally { await h.close(); } + }); + } +} \ No newline at end of file diff --git a/tests/issue1987-preflight-baseline-anchor.test.ts b/tests/issue1987-preflight-baseline-anchor.test.ts index 9c8a5d9100..e1a748daf2 100644 --- a/tests/issue1987-preflight-baseline-anchor.test.ts +++ b/tests/issue1987-preflight-baseline-anchor.test.ts @@ -8,8 +8,8 @@ process.env.NODE_ENV = "test"; process.env.BILI_REPLAY_RETRY_MAX = "1"; import { defaultConfig, type Config } from "acp-kernel"; -import { startServer, type ProxyOptions } from "../src/server.ts"; -import { type CompressSettings } from "../src/config.ts"; +import { startServer } from "../src/server.ts"; +import { type CompressSettings, type ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { getSession } from "../src/session.ts"; @@ -88,11 +88,15 @@ test("e2e #1987: post-fold usage baseline anchors to the shipped payload, not th const upstream = makeUpstream(calls); upstream.listen(0, "127.0.0.1"); await once(upstream, "listening"); - const upstreamPort = upstream.address().port; + const upstreamAddress = upstream.address(); + assert(upstreamAddress && typeof upstreamAddress !== "string"); + const upstreamPort = upstreamAddress.port; const proxy = await startProxy(upstreamPort, { "claude-small": { context: 10_000 } }); await once(proxy, "listening"); - const proxyPort = proxy.address().port; + const proxyAddress = proxy.address(); + assert(proxyAddress && typeof proxyAddress !== "string"); + const proxyPort = proxyAddress.port; const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/v1/messages`; const headers = { "content-type": "application/json", "x-acp-session": "issue1987-anchor-sess" }; diff --git a/tests/issue1993-preflight-400.test.ts b/tests/issue1993-preflight-400.test.ts index c4d5398b24..fbe4edb4ef 100644 --- a/tests/issue1993-preflight-400.test.ts +++ b/tests/issue1993-preflight-400.test.ts @@ -14,7 +14,8 @@ process.env.BILI_REPLAY_RETRY_BASE_MS = "0"; process.env.BILI_PREFLIGHT_DEAD_END_COOLDOWN_MS = "400"; import { defaultConfig } from "acp-kernel"; -import { startServer, type ProxyOptions } from "../src/server.ts"; +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 } from "../src/session.ts"; diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index 147ddbb111..93f7c235a1 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -3,11 +3,11 @@ import http from "node:http"; import { once } from "node:events"; import { createHash } from "node:crypto"; import test from "node:test"; -import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { spawn } from "node:child_process"; -import { applyAbsorb, createContentStore, defaultConfig, DEFAULT_ABSORB_CONFIG, DEFAULT_CCR_CONFIG, storeOriginal } from "acp-kernel"; +import { applyAbsorb, buildStoredPlaceholder, createContentStore, defaultConfig, DEFAULT_ABSORB_CONFIG, DEFAULT_CCR_CONFIG, storeOriginal } from "acp-kernel"; import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; @@ -15,6 +15,7 @@ import { _resetPluginStateForTest, rememberPluginMessages, resolveConversation } import { _resetSessionsForTest, getSession } from "../src/session.ts"; import { _setForTest } from "../src/registry.ts"; import { recordToolWitness, resetToolRingForTest } from "../src/tool-ring.ts"; +import { contentStoreOf } from "../src/store.ts"; process.env.NODE_ENV = "test"; const testRoot = mkdtempSync(join(tmpdir(), "bili-fork-")); @@ -51,6 +52,7 @@ interface StatusResponse { compressCreditTokens: number; inputTokens: number; contextLimit: number | null; + compressibleRanges: { startRef: string; endRef: string; count: number }[] | null; } type PluginResponse = Path extends "/__bili/plugin/manifest" @@ -267,6 +269,12 @@ test("HTTP compressed originals are isolated across parent and siblings and stra assert.equal(straddler.status, 201); assert.equal(straddler.body.status, "expanded"); assert.deepEqual(straddler.body.inheritedBlocks, []); + await sendModel(h, "straddler", [...h.messages.slice(0, 1), { role: "assistant", content: "expanded child first continuation" }]); + const expandedOutbound = JSON.stringify(h.forwarded.at(-1)!.messages); + assert.match(expandedOutbound, /first original/); + assert(!expandedOutbound.includes("second original"), expandedOutbound); + assert(!expandedOutbound.includes("tail original"), expandedOutbound); + assert(!expandedOutbound.includes(summary), expandedOutbound); const original = await h.request("/__bili/plugin/tool", { conversationId: "child", tool: "decompress", args: { blockId: "b1", full: true } }); assert.match(original.body.result, /first original/); assert.match(original.body.result, /second original/); @@ -357,6 +365,95 @@ test("HTTP persistence failure leaves no child and retry can succeed", async () } finally { _setStoreForTest(h.store); await h.close(); } }); +test("HTTP CCR deleted index fails closed in memory after persistence", async () => { + const h = await harness(true); + try { + const parent = resolveConversation("parent").session!; + parent.metadata.effectiveCcr = { ...DEFAULT_CCR_CONFIG, enabled: true }; + parent.contentStore = storeOriginal(createContentStore(), { ref: "m00001", rawId: parent.pluginSnapshot![0].id, text: "retained CCR original", kind: "original", tokens: 10, head: "retained" }); + parent.contentStoreDirty = true; + assert(h.store.flushSync(parent)); + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + delete parent.contentStore.byRef.m00001; + const before = JSON.stringify({ state: parent.state, messages: parent.pluginSnapshot, store: parent.contentStore }); + const unavailable = await h.request("/__bili/plugin/snapshot?conversationId=parent"); + assert.equal(unavailable.status, 409); + assert.equal(unavailable.body.code, "SNAPSHOT_UNAVAILABLE"); + const fork = await h.request("/__bili/plugin/fork", forkRequest(snapshot)); + assert.equal(fork.status, 409); + assert.equal(fork.body.status, "unavailable"); + assert.equal(resolveConversation("child").session, undefined); + assert.equal(h.store.loadSync("child"), null); + assert.equal(JSON.stringify({ state: parent.state, messages: parent.pluginSnapshot, store: parent.contentStore }), before); + } finally { await h.close(); } +}); + +test("HTTP CCR shared payload deletion fails closed before persistence", async () => { + const h = await harness(); + try { + const parent = resolveConversation("parent").session!; + let store = createContentStore(); + for (const [i, message] of parent.pluginSnapshot!.slice(0, 2).entries()) { + store = storeOriginal(store, { ref: `m0000${i + 1}`, rawId: message.id, text: "shared payload", kind: "original", tokens: 10, head: "shared" }); + } + parent.contentStore = store; + rememberPluginMessages(parent.id, parent.pluginSnapshot!, parent.pluginSnapshot!); + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + delete store.byRef.m00001; + const before = JSON.stringify(parent); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=parent")).status, 409); + assert.equal((await h.request("/__bili/plugin/fork", forkRequest(snapshot))).status, 409); + assert.equal(resolveConversation("child").session, undefined); + assert.equal(JSON.stringify(parent), before); + } finally { await h.close(); } +}); + +for (const corruption of ["missing-ref", "missing-payload", "orphan-payload", "wrong-raw-alias", "changed-payload"] as const) { + test(`HTTP CCR ${corruption} fails closed after cold disk restore`, async () => { + const h = await harness(true); + try { + const parent = resolveConversation("parent").session!; + let store = createContentStore(); + for (const [i, message] of parent.pluginSnapshot!.slice(0, 2).entries()) { + store = storeOriginal(store, { ref: `m0000${i + 1}`, rawId: message.id, text: "shared payload", kind: "original", tokens: 10, head: "shared" }); + } + parent.contentStore = store; + parent.contentStoreDirty = true; + assert(h.store.flushSync(parent)); + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const namespace = join(h.dir, "sessions", parent.meta.protocol!); + const filename = readdirSync(namespace).find((name) => name.endsWith(".content-store.json")); + assert(filename); + const path = join(namespace, filename); + const disk = JSON.parse(readFileSync(path, "utf8")) as typeof store; + const payloadHash = disk.byRef.m00001!.hash; + switch (corruption) { + case "missing-ref": delete disk.byRef.m00001; break; + case "missing-payload": delete disk.byHash[payloadHash]; break; + case "orphan-payload": disk.byHash[createHash("sha256").update("orphan").digest("hex")] = "orphan"; break; + case "wrong-raw-alias": disk.byRef.m00001!.rawId = parent.pluginSnapshot![1].id; break; + case "changed-payload": disk.byHash[payloadHash] = "corrupted original"; break; + } + writeFileSync(path, JSON.stringify(disk)); + h.store.cancelAll(); + _resetSessionsForTest(); + _resetPluginStateForTest(); + const restored = getSession("parent"); + contentStoreOf(restored); + const parentState = () => JSON.stringify({ state: restored.state, messages: restored.pluginSnapshot, metadata: restored.metadata, store: restored.contentStore }); + const before = parentState(); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=parent")).status, 409); + const fork = await h.request("/__bili/plugin/fork", forkRequest(snapshot)); + assert.equal(fork.status, 409); + assert.equal(fork.body.status, "unavailable"); + assert.equal(resolveConversation("child").session, undefined); + assert.equal(h.store.loadSync("child"), null); + assert.equal(parentState(), before); + assert.equal(readFileSync(path, "utf8"), JSON.stringify(disk)); + } finally { await h.close(); } + }); +} + test("HTTP fork revision and originals survive a new proxy process", async () => { const h = await harness(true); let child: ReturnType | undefined; @@ -427,7 +524,17 @@ test("HTTP status separates real usage from a manual-compression estimate and ch assert(typeof before.body.contextTokensAt === "number"); assert(before.body.contextTokensAt > 0); assert.equal(typeof before.body.contextGeneration, "string"); + assert(Array.isArray(before.body.compressibleRanges)); + assert(before.body.compressibleRanges.length > 0); + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + for (const range of before.body.compressibleRanges) { + assert(snapshot.orderedMessages.some(message => message.ref === range.startRef)); + assert(snapshot.orderedMessages.some(message => message.ref === range.endRef)); + assert(Number.isInteger(range.count) && range.count > 0); + } + assert.equal(before.body.sessionRevision, snapshot.parentRevision); const again = await h.request("/__bili/plugin/status?conversationId=parent"); + assert.deepEqual(again.body.compressibleRanges, before.body.compressibleRanges); assert.equal(again.body.contextTokensAt, before.body.contextTokensAt); assert.equal(again.body.contextGeneration, before.body.contextGeneration); const compressed = await h.request("/__bili/plugin/tool", { conversationId: "parent", tool: "compress", args: { content: [{ startId: "m00001", endId: "m00002", summary: "The prefix preserves the first user request and the second assistant response, with all original content retained for decompression." }] } }); @@ -445,6 +552,35 @@ test("HTTP status separates real usage from a manual-compression estimate and ch } finally { await h.close(); } }); +test("HTTP compressed child effective estimate survives cold disk restore without replaying credit", async () => { + const h = await harness(true); + try { + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + assert.equal((await h.request("/__bili/plugin/fork", forkRequest(snapshot, "child", 3))).status, 201); + assert.equal(resolveConversation("child").session!.metadata.contextTokensAt, undefined); + await compress(h, "child"); + const before = (await h.request("/__bili/plugin/status?conversationId=child")).body; + assert.equal(before.contextTokensSource, "estimate"); + assert(before.compressCreditTokens > 0); + assert(h.store.flushSync(resolveConversation("child").session!)); + _resetPluginStateForTest(); + _resetSessionsForTest(); + const after = (await h.request("/__bili/plugin/status?conversationId=child")).body; + assert.equal(after.contextTokens, before.contextTokens); + assert.equal(after.contextTokensAt, before.contextTokensAt); + assert.equal(after.contextGeneration, before.contextGeneration); + assert.equal(after.sessionRevision, before.sessionRevision); + assert.equal(after.compressCreditTokens, 0); + const restored = resolveConversation("child").session!; + assert.equal(restored.stats.contextTokens, before.contextTokens); + assert.equal(restored.stats.contextTokensSource, "estimate"); + assert(h.store.flushSync(restored)); + _resetPluginStateForTest(); + _resetSessionsForTest(); + assert.equal((await h.request("/__bili/plugin/status?conversationId=child")).body.contextTokens, before.contextTokens); + } finally { await h.close(); } +}); + test("HTTP manual compress rejects a stale expectedRevision under the session lock", async () => { const h = await harness(); try { @@ -499,6 +635,55 @@ test("HTTP multimodal snapshots are explicitly unavailable, not text-only matche } finally { await h.close(); } }); +test("HTTP CCR foreign placeholder ref cannot publish an exact empty-store child", async () => { + const h = await harness(true); + try { + const baseline = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const placeholder = buildStoredPlaceholder({ ref: "m00077", kind: "original", tokens: 100, head: "missing original", retrieveToolName: "acp_retrieve" }); + await sendModel(h, "foreign-placeholder", [{ role: "user", content: placeholder }, { role: "assistant", content: "retained tail" }]); + const parent = resolveConversation("foreign-placeholder").session!; + assert.equal(parent.state.messageRefs.byRaw[parent.pluginSnapshot![0].id], "m00001"); + assert.deepEqual(contentStoreOf(parent).byRef, {}); + const state = () => JSON.stringify({ state: parent.state, messages: parent.pluginSnapshot, metadata: parent.metadata, store: parent.contentStore }); + const before = state(); + const snapshot = await h.request("/__bili/plugin/snapshot?conversationId=foreign-placeholder"); + assert.equal(snapshot.status, 409, JSON.stringify(snapshot.body)); + assert.equal(snapshot.body.code, "SNAPSHOT_UNAVAILABLE"); + const fork = await h.request("/__bili/plugin/fork", { ...forkRequest(baseline), parentConversationId: "foreign-placeholder" }); + assert.equal(fork.status, 409, JSON.stringify(fork.body)); + assert.equal(fork.body.status, "unavailable"); + assert.equal(resolveConversation("child").session, undefined); + assert.equal(h.store.loadSync("child"), null); + assert.equal(state(), before); + } finally { await h.close(); } +}); + +for (const content of [null, "audio transcript"] as const) { + test(`HTTP OpenAI assistant audio with ${content === null ? "null" : "text"} content is unavailable`, async () => { + const h = await harness(true); + try { + const baseline = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const messages = [{ role: "user", content: "review the audio" }, { role: "assistant", content, audio: { id: "audio_fixture" } }, { role: "user", content: "continue" }]; + const response = await fetch(`${h.origin}/bili/${h.upstreamUrl}/v1/chat/completions`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "test", "x-bili-plugin-conversation": "audio-parent" }, body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages }) }); + assert.equal(response.status, 200, await response.text()); + const parent = resolveConversation("audio-parent").session!; + contentStoreOf(parent); + const state = () => JSON.stringify({ state: parent.state, messages: parent.pluginSnapshot, metadata: parent.metadata, store: parent.contentStore }); + const before = state(); + const snapshot = await h.request("/__bili/plugin/snapshot?conversationId=audio-parent"); + assert.equal(snapshot.status, 409, JSON.stringify(snapshot.body)); + assert.equal(snapshot.body.code, "SNAPSHOT_UNAVAILABLE"); + assert.equal(parent.metadata.publicSnapshotTextComparable, false); + const fork = await h.request("/__bili/plugin/fork", { ...forkRequest(baseline), parentConversationId: "audio-parent" }); + assert.equal(fork.status, 409, JSON.stringify(fork.body)); + assert.equal(fork.body.status, "unavailable"); + assert.equal(resolveConversation("child").session, undefined); + assert.equal(h.store.loadSync("child"), null); + assert.equal(state(), before); + } finally { await h.close(); } + }); +} + test("HTTP fork before absorb summary preserves the original pair on the first child request", async () => { const h = await harness(); try { @@ -620,6 +805,14 @@ test("HTTP nested summaries expand crossing ancestors and retain independent ori assert.equal((await h.request("/__bili/plugin/fork", forkRequest(snapshot, "nested-sibling", 5))).status, 201); for (const id of ["nested", "nested-sibling"]) { await sendModel(h, id, [...history.slice(0, 5), { role: "assistant", content: `${id} first continuation` }]); + const outbound = JSON.stringify(h.forwarded.at(-1)!.messages); + assert(outbound.includes(summary), outbound); + assert(outbound.includes(`${id} first continuation`), outbound); + // Kernel prune preserves the first user anchor even when its block is folded. + assert.match(outbound, /first original/); + assert(!outbound.includes("second original"), outbound); + assert(!outbound.includes("third response"), outbound); + assert(!outbound.includes("retained recent tail"), outbound); const continued = (await h.request(`/__bili/plugin/snapshot?conversationId=${id}`)).body; assert.deepEqual(continued.orderedMessages.slice(0, 5), snapshot.orderedMessages.slice(0, 5)); assert.equal(resolveConversation(id).session!.state.blocks[1].summary, summary); @@ -627,6 +820,15 @@ test("HTTP nested summaries expand crossing ancestors and retain independent ori } assert.notEqual(resolveConversation("nested").session!.pluginSnapshot![5].text, resolveConversation("nested-sibling").session!.pluginSnapshot![5].text); assert.notEqual(resolveConversation("nested").session!.blockContents.get("b2"), resolveConversation("nested-sibling").session!.blockContents.get("b2")); + await sendModel(h, "nested-prefix", [...history.slice(0, 2), { role: "user", content: "expanded nested first continuation" }]); + const prefixOutbound = JSON.stringify(h.forwarded.at(-1)!.messages); + assert.match(prefixOutbound, /first original/); + assert.match(prefixOutbound, /second original/); + assert(!prefixOutbound.includes("third response"), prefixOutbound); + assert(!prefixOutbound.includes("retained recent tail"), prefixOutbound); + assert(!prefixOutbound.includes(summary), prefixOutbound); + assert.notEqual(parent.state.blocks[1].expanded, true); + assert.notEqual(resolveConversation("nested-sibling").session!.state.blocks[1].expanded, true); const original = await h.request("/__bili/plugin/tool", { conversationId: "nested", tool: "decompress", args: { blockId: "b2", full: true } }); const file = original.body.result.match(/written to: (.+)\n/)?.[1]; const text = file ? readFileSync(file, "utf8") : original.body.result; diff --git a/tests/ws-bridge.test.ts b/tests/ws-bridge.test.ts index 002336faad..6d2a20fa7e 100644 --- a/tests/ws-bridge.test.ts +++ b/tests/ws-bridge.test.ts @@ -42,7 +42,7 @@ const fakeCodec: WsBridgeCodec = { async function harness(codecs: readonly WsBridgeCodec[]) { const server = http.createServer((_req, res) => res.writeHead(404).end()); const logs: string[] = []; - const handler = installWebSocketBridge(server, (_req, res) => Promise.resolve(res.writeHead(200).end()), (level, message) => logs.push(`${level} ${message}`), codecs); + const handler = installWebSocketBridge(server, async (_req, res) => { res.writeHead(200).end(); }, (level, message) => logs.push(`${level} ${message}`), codecs); server.on("upgrade", (req, socket, head) => { if (!handler(req, socket, head)) socket.destroy(); }); await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); return { server, handler, logs, port: (server.address() as AddressInfo).port }; @@ -108,6 +108,6 @@ test("ws bridge: server close shuts every session down and terminates peers", as assert.deepEqual(session.shutdowns, ["server-close"]); assert.equal(socket.readyState, WebSocket.CLOSED); } finally { - await new Promise(resolve => upstream.close(() => resolve())); + await new Promise(resolve => upstream.close(() => resolve())); } }); From 63cd3ec34c0fc94b6154a8abe37ae9c2c6bc1a12 Mon Sep 17 00:00:00 2001 From: YZSQ <187964595+yingzhongshuoqi@users.noreply.github.com> Date: Sun, 4 Oct 2026 04:34:36 +0800 Subject: [PATCH 04/10] test(plugin): cover manual compression after idle context expiry --- tests/plugin-fork.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index 93f7c235a1..a36897795d 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -552,6 +552,31 @@ test("HTTP status separates real usage from a manual-compression estimate and ch } finally { await h.close(); } }); +test("HTTP manual compression renews an idle context observation without changing billing", async () => { + const h = await harness(); + const realNow = Date.now; + try { + const before = (await h.request("/__bili/plugin/status?conversationId=parent")).body; + assert(typeof before.contextTokensAt === "number"); + const commitTime = realNow() + 16 * 60 * 1000; + Date.now = () => commitTime; + await compress(h); + const after = (await h.request("/__bili/plugin/status?conversationId=parent")).body; + assert.equal(after.contextTokensAt, commitTime); + assert.equal(after.contextTokensSource, "estimate"); + assert.notEqual(after.contextGeneration, before.contextGeneration); + assert(typeof after.contextTokens === "number" && typeof before.contextTokens === "number"); + assert(after.contextTokens < before.contextTokens); + assert.equal(after.inputTokens, before.inputTokens); + const repeated = (await h.request("/__bili/plugin/status?conversationId=parent")).body; + assert.equal(repeated.contextTokensAt, commitTime); + assert.equal(repeated.contextGeneration, after.contextGeneration); + } finally { + Date.now = realNow; + await h.close(); + } +}); + test("HTTP compressed child effective estimate survives cold disk restore without replaying credit", async () => { const h = await harness(true); try { From 85ec9c1bfbcdea44431ecbf7f6ea1a7031b6f92d Mon Sep 17 00:00:00 2001 From: YZSQ <187964595+yingzhongshuoqi@users.noreply.github.com> Date: Sun, 4 Oct 2026 05:11:12 +0800 Subject: [PATCH 05/10] test(plugin): harden fork process test --- tests/plugin-fork.test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index a36897795d..02e433a21a 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -7,6 +7,7 @@ import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "n import { tmpdir } from "node:os"; import { join } from "node:path"; import { spawn } from "node:child_process"; +import { fileURLToPath } from "node:url"; import { applyAbsorb, buildStoredPlaceholder, createContentStore, defaultConfig, DEFAULT_ABSORB_CONFIG, DEFAULT_CCR_CONFIG, storeOriginal } from "acp-kernel"; import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; @@ -471,7 +472,7 @@ test("HTTP fork revision and originals survive a new proxy process", async () => assert.equal(fork.status, 201); const childSnapshot = (await h.request("/__bili/plugin/snapshot?conversationId=child")).body; const code = `import {startServer} from './src/server.ts'; import {SessionStore,_setStoreForTest} from './src/persist.ts'; import {defaultConfig} from 'acp-kernel'; import {_setForTest} from './src/registry.ts'; _setForTest({}); _setStoreForTest(new SessionStore({dir:${JSON.stringify(h.dir + "/sessions")},enabled:true})); const s=await startServer({port:0,host:'127.0.0.1',upstream:${JSON.stringify(h.upstreamUrl)},modelContextLimit:400000,routes:{},kernelConfig:defaultConfig(400000),compress:{injectTool:true,injectNudge:true},promptCache:{routing:'auto'},sessionHeader:'x-acp-session',log:false,debug:false,passthrough:false,autoUpdate:false,mitm:{enabled:false,domains:[]}}); const ready=()=>process.stdout.write(JSON.stringify({port:s.address().port})+'\\n'); if(s.listening)ready();else s.once('listening',ready);`; - child = spawn(process.execPath, ["--import", "tsx", "--input-type=module", "--eval", code], { cwd: new URL("../", import.meta.url).pathname, env: { ...process.env, NODE_ENV: "test" }, stdio: ["ignore", "pipe", "pipe"] }); + child = spawn(process.execPath, ["--import", "tsx", "--input-type=module", "--eval", code], { cwd: fileURLToPath(new URL("../", import.meta.url)), env: { ...process.env, NODE_ENV: "test" }, stdio: ["ignore", "pipe", "pipe"] }); let stderr = ""; child.stderr!.on("data", (b) => { stderr += b.toString(); }); const port = await new Promise((resolve, reject) => { @@ -483,6 +484,7 @@ test("HTTP fork revision and originals survive a new proxy process", async () => if (port) { clearTimeout(timer); resolve(Number(port[1])); } }); child!.once("exit", (code) => { clearTimeout(timer); reject(new Error(`restart exited ${code}: ${stderr}`)); }); + child!.once("error", (error) => { clearTimeout(timer); reject(error); }); }); const call = async (path: Path, body?: unknown) => { const r = await fetch(`http://127.0.0.1:${port}` + path, body === undefined ? {} : { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }); @@ -510,7 +512,7 @@ test("HTTP fork revision and originals survive a new proxy process", async () => assert.match(original.body.result, /second original/); assert(!original.body.result.includes("tail original")); } finally { - if (child && child.exitCode === null && child.signalCode === null) { child.kill("SIGTERM"); await once(child, "exit"); } + if (child?.pid && child.exitCode === null && child.signalCode === null) { child.kill("SIGTERM"); await once(child, "exit"); } await h.close(); } }); From aa7b0efce2068040d0eda331631d001f86b43af4 Mon Sep 17 00:00:00 2001 From: yingzhongshuoqi <187964595+yingzhongshuoqi@users.noreply.github.com> Date: Sun, 4 Oct 2026 11:13:40 +0800 Subject: [PATCH 06/10] fix: honor explicit plugin main request intent before side demotion --- src/plugin.ts | 10 +-- src/server.ts | 6 +- src/server/side-request.ts | 1 + tests/plugin-fork.test.ts | 109 +++++++++++++++++++++++++-- tests/side-request-isolation.test.ts | 9 +++ 5 files changed, 121 insertions(+), 14 deletions(-) diff --git a/src/plugin.ts b/src/plugin.ts index b5a451dbff..4a1af68214 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -82,9 +82,9 @@ export const PLUGIN_INSTRUCTIONS_MUTABLE_HEADER = "x-bili-plugin-instructions-mu * cannot express: opencode v2 title-gen requests carry NO max_tokens (options * {} for kind==="title"), so the output-budget side-request heuristic (#388) * can never see them — they were misclassified as main turns and got the full - * compress prompt + tool injected into the title model. The proxy acts only on - * known side-request agents (side-request.ts SIDE_REQUEST_AGENTS); main-persona - * ids are inert telemetry. */ + * compress prompt + tool injected into the title model. Known side-request + * agents route by intent (side-request.ts SIDE_REQUEST_AGENTS); explicit "main" + * vetoes side heuristics, while other persona ids remain inert telemetry. */ export const PLUGIN_REQUEST_AGENT_HEADER = "x-bili-plugin-agent"; export const PLUGIN_PROTOCOL_VERSION = 1; @@ -168,8 +168,8 @@ export function pluginReportedModel(headers: Record): string | undefined { if (pluginAgentHeader(headers) === undefined) return undefined; return headerValue(headers, PLUGIN_REQUEST_AGENT_HEADER); diff --git a/src/server.ts b/src/server.ts index 23adec23d4..708162607c 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2718,9 +2718,10 @@ async function handle( // all-bili-tools demotion is vetoed for them (side requests on this lane // are identified by the #1699 persona header instead). const wsLaneEnvelope = req.headers["x-bili-ws-lane"] !== undefined; - // A verified public-fork prefix also proves this is main history. + const requestAgent = pluginRequestAgentHeader(req.headers); + // Explicit main intent and a verified public-fork prefix each veto heuristic demotion. const demotedSide = !countTokens && !responsesCompact && protocol !== null && pluginMode - && !wsLaneEnvelope && !publicForkPrefix + && requestAgent !== "main" && !wsLaneEnvelope && !publicForkPrefix && detectAcpArtifacts(bodyBuffer, parsed) === null && stripLeakedBiliTools(parsed); // #546: restore a client-shrunk output budget BEFORE the side gate so a @@ -2773,7 +2774,6 @@ async function handle( // #1699: opencode v2 title-gen requests carry no max_tokens, so the budget // heuristic alone misses them. The host stamps its per-request persona id // (x-bili-plugin-agent); a known side-request agent routes verbatim by intent. - const requestAgent = pluginRequestAgentHeader(req.headers); if (!countTokens && !responsesCompact && protocol !== null && !publicForkPrefix && (demotedSide || isSideRequest(parsed, requestAgent))) { // #554: the passthrough below skips EVERY input-side guard by design // (#388) — a full-history side request over the window is a diff --git a/src/server/side-request.ts b/src/server/side-request.ts index b167e02c3f..c7381419b6 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -24,6 +24,7 @@ export const SIDE_REQUEST_AGENTS: ReadonlySet = new Set(["title"]); export function isSideRequest(parsed: unknown, requestAgent?: string): boolean { if (!parsed || typeof parsed !== "object") return false; if (requestAgent !== undefined && SIDE_REQUEST_AGENTS.has(requestAgent)) return true; + if (requestAgent === "main") return false; const p = parsed as Record; if (Array.isArray(p.tools) && p.tools.length > 0) return false; const field = outputBudgetField(parsed); diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index 02e433a21a..ee660fc384 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -57,7 +57,7 @@ interface StatusResponse { } type PluginResponse = Path extends "/__bili/plugin/manifest" - ? { capabilities: { fork: { protocolVersion: number; endpoint: string; snapshotEndpoint: string } } } + ? { tools: { openai: { type: "function"; function: { name: string; parameters: Record } }[] }; capabilities: { fork: { protocolVersion: number; endpoint: string; snapshotEndpoint: string } } } : Path extends "/__bili/plugin/fork" ? ForkResponse : Path extends "/__bili/plugin/tool" ? { result: string; code?: string } : Path extends `/__bili/plugin/status${string}` ? StatusResponse @@ -70,7 +70,7 @@ async function responseBody(response: Response, _path: Path return body as PluginResponse; } -async function harness(persist = false) { +async function harness(persist = false, seedParent = true) { const dir = mkdtempSync(join(testRoot, "run-")); for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = dir; _resetSessionsForTest(); @@ -86,7 +86,9 @@ async function harness(persist = false) { req.on("end", () => { forwarded.push(JSON.parse(Buffer.concat(chunks).toString("utf8")) as Record); res.writeHead(200, { "content-type": "application/json" }); - res.end(JSON.stringify({ id: "msg_test", role: "assistant", content: [{ type: "text", text: "answer" }], usage: { input_tokens: 10000, output_tokens: 10 } })); + res.end(JSON.stringify(req.url?.endsWith("/chat/completions") + ? { id: "chat_test", object: "chat.completion", choices: [{ index: 0, message: { role: "assistant", content: "answer" }, finish_reason: "stop" }], usage: { prompt_tokens: 10000, completion_tokens: 10, total_tokens: 10010 } } + : { id: "msg_test", role: "assistant", content: [{ type: "text", text: "answer" }], usage: { input_tokens: 10000, output_tokens: 10 } })); }); }); upstream.listen(0, "127.0.0.1"); @@ -104,9 +106,11 @@ async function harness(persist = false) { return { status: r.status, body: await responseBody(r, path) }; }; const messages = [{ role: "user", content: "first original ".repeat(250) }, { role: "assistant", content: "second original ".repeat(250) }, { role: "user", content: "tail original" }]; - const r = await fetch(`${origin}/bili/${upstreamUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "test", "x-bili-plugin-conversation": "parent" }, body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages }) }); - assert.equal(r.status, 200); - await r.text(); + if (seedParent) { + const r = await fetch(`${origin}/bili/${upstreamUrl}/v1/messages`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "test", "x-bili-plugin-conversation": "parent" }, body: JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: false, messages }) }); + assert.equal(r.status, 200); + await r.text(); + } return { request, messages, forwarded, dir, store, upstreamUrl, origin, close: async () => { store.cancelAll(); proxy.close(); upstream.close(); await Promise.all([once(proxy, "close"), once(upstream, "close")]); } }; } @@ -143,6 +147,99 @@ test("HTTP exposes a versioned snapshot and forks an exact ordered prefix", asyn } finally { await h.close(); } }); +async function sendIntentModel(h: Awaited>, conversationId: string, messages: unknown[], tools: unknown[], requestAgent?: string, maxTokens = 256, announcePlugin = true) { + const headers: Record = { "content-type": "application/json", "x-acp-session": conversationId }; + if (announcePlugin) { + headers["x-bili-plugin"] = "ekko-agent"; + headers["x-bili-plugin-conversation"] = conversationId; + } + if (requestAgent !== undefined) headers["x-bili-plugin-agent"] = requestAgent; + const response = await fetch(`${h.origin}/bili/${h.upstreamUrl}/v1/chat/completions`, { method: "POST", headers, body: JSON.stringify({ model: "claude-test", max_tokens: maxTokens, stream: false, messages, tools }) }); + assert.equal(response.status, 200, await response.text()); +} + +test("HTTP explicit main with only public ACP tools captures initial history, usage and first fork payload", async () => { + const h = await harness(false, false); + try { + const tools = (await h.request("/__bili/plugin/manifest")).body.tools.openai; + assert.deepEqual(tools.map((tool) => tool.function.name), ["compress", "decompress", "search_context", "acp_status", "acp_cache"]); + await sendIntentModel(h, "parent", h.messages, tools, "main"); + assert.deepEqual(h.forwarded.at(-1)!.tools, tools, "main tools survive unchanged"); + assert.equal(h.forwarded.at(-1)!.max_tokens, 256); + const snapshot = await h.request("/__bili/plugin/snapshot?conversationId=parent"); + assert.equal(snapshot.status, 200, JSON.stringify(snapshot.body)); + assert.deepEqual(snapshot.body.messages.map((message) => message.text), h.messages.map((message) => message.content)); + const status = (await h.request("/__bili/plugin/status?conversationId=parent")).body; + assert.equal(status.contextTokensSource, "usage"); + assert.equal(status.contextTokens, 10000); + assert.equal(resolveConversation("parent").session!.stats.requests, 1); + const fork = await h.request("/__bili/plugin/fork", forkRequest(snapshot.body, "child", h.messages.length)); + assert.equal(fork.status, 201, JSON.stringify(fork.body)); + assert.equal(fork.body.status, "exact"); + const history = [...h.messages, { role: "assistant", content: "first fork continuation" }]; + await sendIntentModel(h, "child", history, tools, "main"); + assert.deepEqual(h.forwarded.at(-1)!.tools, tools, "first child request retains the public tools"); + const forwarded = h.forwarded.at(-1)!.messages as { role: string; content: string }[]; + assert.equal(forwarded[0].role, "system"); + assert.match(forwarded[0].content, /^Compression Philosophy:/); + assert.deepEqual(forwarded.slice(1).map((message) => ({ role: message.role, content: message.content.replace(/^\x3cacp\b[^\n]*\x3c\/acp\x3e\n/, "") })), history, "the first fork payload retains the exact inherited history in order"); + const child = (await h.request("/__bili/plugin/snapshot?conversationId=child")).body; + assert.deepEqual(child.orderedMessages.slice(0, h.messages.length), snapshot.body.orderedMessages); + assert.deepEqual(child.messages.map((message) => message.text), history.map((message) => message.content)); + assert.equal((await h.request("/__bili/plugin/status?conversationId=child")).body.contextTokensSource, "usage"); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=parent")).body.parentRevision, snapshot.body.parentRevision); + } finally { await h.close(); } +}); + +test("HTTP explicit main without tools survives a tiny output budget", async () => { + const h = await harness(false, false); + try { + await sendIntentModel(h, "parent", h.messages, [], "main", 100); + const snapshot = await h.request("/__bili/plugin/snapshot?conversationId=parent"); + assert.equal(snapshot.status, 200, JSON.stringify(snapshot.body)); + assert.deepEqual(snapshot.body.messages.map((message) => message.text), h.messages.map((message) => message.content)); + assert.equal((await h.request("/__bili/plugin/status?conversationId=parent")).body.contextTokensSource, "usage"); + } finally { await h.close(); } +}); + +test("HTTP title on the same plugin lane cannot replace the main snapshot or usage", async () => { + const h = await harness(); + try { + const snapshot = (await h.request("/__bili/plugin/snapshot?conversationId=parent")).body; + const status = (await h.request("/__bili/plugin/status?conversationId=parent")).body; + const session = resolveConversation("parent").session!; + const before = JSON.stringify({ state: session.state, stats: session.stats, highWater: session.metadata.outputBudgetHighWater }); + const tools = (await h.request("/__bili/plugin/manifest")).body.tools.openai; + const messages = [{ role: "user", content: "Generate a title." }]; + await sendIntentModel(h, "parent", messages, tools, "title", 1024); + assert.equal(h.forwarded.at(-1)!.tools, undefined); + assert.deepEqual(h.forwarded.at(-1)!.messages, messages); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=parent")).body.parentRevision, snapshot.parentRevision); + const after = (await h.request("/__bili/plugin/status?conversationId=parent")).body; + for (const field of ["sessionRevision", "contextTokensSource", "contextTokens", "contextTokensAt", "contextGeneration", "inputTokens", "compressCreditTokens"] as const) assert.equal(after[field], status[field]); + assert.equal(JSON.stringify({ state: session.state, stats: session.stats, highWater: session.metadata.outputBudgetHighWater }), before); + } finally { await h.close(); } +}); + +test("HTTP missing or unknown intent keeps all-bili demotion; anonymous main cannot claim plugin intent", async () => { + const h = await harness(false, false); + try { + const tools = (await h.request("/__bili/plugin/manifest")).body.tools.openai; + for (const requestAgent of [undefined, "other"]) { + const id = requestAgent ?? "missing-intent"; + await sendIntentModel(h, id, h.messages, tools, requestAgent); + assert.equal(h.forwarded.at(-1)!.tools, undefined); + assert.equal((await h.request(`/__bili/plugin/snapshot?conversationId=${id}`)).status, 409); + assert.equal(resolveConversation(id).session!.stats.requests, 0); + } + await sendIntentModel(h, "anonymous-main", h.messages, [], "main", 100, false); + assert.deepEqual(h.forwarded.at(-1)!.messages, h.messages, "unannounced persona must still pass through as a tiny utility request"); + assert.equal(getSession("anonymous-main").stats.requests, 0); + assert.equal(getSession("anonymous-main").metadata.pluginAgent, undefined); + assert.equal((await h.request("/__bili/plugin/snapshot?conversationId=anonymous-main")).status, 409); + } finally { await h.close(); } +}); + function forkRequest(snapshot: { parentRevision: string; orderedMessages: unknown[] }, child = "child", count = 2) { const orderedMessages = snapshot.orderedMessages.slice(0, count); return { protocolVersion: 1, parentConversationId: "parent", childConversationId: child, parentRevision: snapshot.parentRevision, branchPoint: { messageCount: count, orderHash: hash(orderedMessages) }, orderedMessages, idempotencyKey: `fork-${child}` }; diff --git a/tests/side-request-isolation.test.ts b/tests/side-request-isolation.test.ts index 94c3923da7..ef022606f0 100644 --- a/tests/side-request-isolation.test.ts +++ b/tests/side-request-isolation.test.ts @@ -58,6 +58,15 @@ test("isSideRequest: host-declared side-request agent outranks the token-budget assert.equal(isSideRequest({ max_tokens: 100 }, "unknown-persona"), true, "unknown persona still honors a tiny budget"); }); +test("isSideRequest: explicit main intent vetoes side heuristics", () => { + for (const body of [{ max_tokens: 100 }, { max_completion_tokens: 100 }, { max_output_tokens: 100 }, { generationConfig: { maxOutputTokens: 100 } }]) { + assert.equal(isSideRequest(body, "main"), false, "main intent outranks a tiny budget even without tools"); + assert.equal(isSideRequest(body, "title"), true, "title intent remains side"); + assert.equal(isSideRequest(body, "other"), true, "unknown intent keeps the budget heuristic"); + assert.equal(isSideRequest(body), true, "missing intent keeps the budget heuristic"); + } +}); + const noopLog = (): void => {}; function metaSession(id: string): { id: string; metadata: Record } { From af4ada311b5bd06c7bfe8d7e380f8de826caf36c Mon Sep 17 00:00:00 2001 From: yingzhongshuoqi <187964595+yingzhongshuoqi@users.noreply.github.com> Date: Sun, 4 Oct 2026 13:37:54 +0800 Subject: [PATCH 07/10] fix(plugin): publish Responses usage before terminal frame --- src/plugin.ts | 6 + tests/plugin-terminal-usage.test.ts | 226 ++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 tests/plugin-terminal-usage.test.ts diff --git a/src/plugin.ts b/src/plugin.ts index caa2477bb1..376deb7bf3 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -2810,6 +2810,12 @@ export async function pipePluginResponsesWithStrip( let rebuild = hadEchoText || retryRewritePending(); if (rebuild) evOut = stripResponsesText(ev); rewriteRetryIds(evOut); + // Hosts finish on this frame, before EOF settles billing and late usage. + if (session && acc.inputTokens !== undefined && acc.inputTokens > 0) { + const credit = session.stats.compressCreditTokens ?? 0; + recordContextObservation(session, Math.max(0, acc.inputTokens - credit), credit > 0 ? "estimate" : "usage"); + markDirty(session); + } await write(rebuild ? rebuildEvent(rawEvent, evOut) : rawEvent + "\n\n"); continue; } diff --git a/tests/plugin-terminal-usage.test.ts b/tests/plugin-terminal-usage.test.ts new file mode 100644 index 0000000000..546a2c92fc --- /dev/null +++ b/tests/plugin-terminal-usage.test.ts @@ -0,0 +1,226 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +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 { _resetPluginStateForTest, resolveConversation } from "../src/plugin.ts"; +import { _resetSessionsForTest } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; + +process.env.NODE_ENV = "test"; +const root = mkdtempSync(join(tmpdir(), "bili-terminal-usage-")); +test.after(() => rmSync(root, { recursive: true, force: true })); +for (const key of ["XDG_STATE_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME", "XDG_CONFIG_HOME"]) process.env[key] = root; + +interface StatusResponse { + sessionId: string; + sessionRevision: string; + contextTokensSource: "usage" | "estimate" | "unavailable"; + contextTokens: number | null; + contextTokensAt: number | null; + contextGeneration: string | null; + inputTokens: number; + cachedTokens: number; +} + +function barrier() { + let release!: () => void; + const wait = new Promise((resolve) => { release = resolve; }); + return { wait, release }; +} + +function frame(event: Record) { + return `event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`; +} + +async function harness(terminal: Record, late = "") { + _resetSessionsForTest(); + _resetPluginStateForTest(); + resetToolRingForTest(); + const store = new SessionStore({ dir: join(root, "sessions"), enabled: false }); + _setStoreForTest(store); + _setForTest({}); + const sendTerminal = barrier(); + const endStream = barrier(); + let upstreamEnded = false; + const initial = frame({ type: "response.output_text.delta", item_id: "msg_terminal", output_index: 0, delta: "answer" }); + const terminalFrame = frame(terminal); + const upstream = http.createServer((req, res) => { + req.resume(); + req.on("end", async () => { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.write(initial); + await sendTerminal.wait; + res.write(terminalFrame); + await endStream.wait; + upstreamEnded = true; + res.end(late); + }); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const addr = upstream.address(); + assert(addr && typeof addr === "object"); + const upstreamUrl = `http://127.0.0.1:${addr.port}`; + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstreamUrl, routes: { [upstreamUrl]: { models: { "terminal-test": { context: 400000 } } } }, modelContextLimit: 400000, kernelConfig: defaultConfig(400000), compress: { injectTool: true, injectNudge: true }, promptCache: { routing: "auto" }, sessionHeader: "x-acp-session", log: false, debug: false, passthrough: false, autoUpdate: false, mitm: { enabled: false, domains: [] } } as ProxyOptions); + await once(proxy, "listening"); + const paddr = proxy.address(); + assert(paddr && typeof paddr === "object"); + const origin = `http://127.0.0.1:${paddr.port}`; + const response = await fetch(`${origin}/bili/${upstreamUrl}/v1/responses`, { method: "POST", headers: { "content-type": "application/json", "x-bili-plugin": "test", "x-bili-plugin-conversation": "terminal-usage" }, body: JSON.stringify({ model: "terminal-test", stream: true, input: [{ role: "user", content: "first original ".repeat(250) }], max_output_tokens: 1024 }) }); + assert.equal(response.status, 200); + assert(response.body); + const reader = response.body.getReader(); + const decoder = new TextDecoder(); + let wire = ""; + const readThrough = async (target: string) => { + while (!wire.includes(target)) { + const { done, value } = await reader.read(); + assert(!done, "HTTP stream ended before the expected frame"); + wire += decoder.decode(value, { stream: true }); + } + }; + await readThrough(initial); + const status = async (): Promise => { + const r = await fetch(`${origin}/__bili/plugin/status?conversationId=terminal-usage`); + assert.equal(r.status, 200); + return await r.json() as StatusResponse; + }; + const drain = async () => { + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + wire += decoder.decode(value, { stream: true }); + } + wire += decoder.decode(); + }; + return { + status, session: resolveConversation("terminal-usage").session!, + terminal: async () => { sendTerminal.release(); await readThrough(terminalFrame); }, + finish: async () => { endStream.release(); await drain(); assert.equal(wire, initial + terminalFrame + late); }, + upstreamEnded: () => upstreamEnded, + close: async () => { + sendTerminal.release(); endStream.release(); + await drain(); + store.cancelAll(); + const closed = Promise.all([once(proxy, "close"), once(upstream, "close")]); + proxy.close(); upstream.close(); + await closed; + }, + }; +} + +test("HTTP Responses terminal usage is observable before upstream EOF without double billing", { timeout: 10000 }, async () => { + const h = await harness({ type: "response.completed", response: { id: "resp_terminal", status: "completed", output: [], usage: { input_tokens: 8971, output_tokens: 13, input_tokens_details: { cached_tokens: 1024 } } } }); + try { + const pending = await h.status(); + assert.equal(pending.contextTokensSource, "estimate"); + await h.terminal(); + assert.equal(h.upstreamEnded(), false); + const terminal = await h.status(); + assert.equal(terminal.contextTokensSource, "usage"); + assert.equal(terminal.contextTokens, 8971); + assert.equal(terminal.sessionId, h.session.id); + assert.equal(terminal.sessionRevision, pending.sessionRevision); + assert.notEqual(terminal.contextGeneration, pending.contextGeneration); + assert.equal(typeof terminal.contextTokensAt, "number"); + assert.equal((await h.status()).contextGeneration, terminal.contextGeneration); + assert.deepEqual([terminal.inputTokens, terminal.cachedTokens, h.session.stats.outputTokens], [0, 0, 0], "observation does not book the request before its final usage settles"); + await h.finish(); + const final = await h.status(); + assert.equal(final.contextTokensSource, "usage"); + assert.equal(final.contextTokens, 8971); + assert.equal(h.session.stats.requests, 1); + assert.deepEqual([final.inputTokens, final.cachedTokens, h.session.stats.outputTokens, h.session.stats.cacheSamples], [8971, 1024, 13, 1]); + } finally { await h.close(); } +}); + +test("HTTP Responses late usage revises the final sample without billing the terminal observation twice", { timeout: 10000 }, async () => { + const late = frame({ type: "response.usage", usage: { prompt_tokens: 9100, completion_tokens: 21, prompt_tokens_details: { cached_tokens: 2048 } } }); + const h = await harness({ type: "response.completed", response: { status: "completed", output: [], usage: { input_tokens: 8971, output_tokens: 13 } } }, late); + try { + await h.terminal(); + assert.equal(h.upstreamEnded(), false); + const terminal = await h.status(); + assert.deepEqual([terminal.contextTokensSource, terminal.contextTokens, terminal.inputTokens], ["usage", 8971, 0]); + await h.finish(); + const final = await h.status(); + assert.deepEqual([final.contextTokensSource, final.contextTokens, final.inputTokens, final.cachedTokens], ["usage", 9100, 9100, 2048]); + assert.equal(h.session.stats.requests, 1); + assert.deepEqual([h.session.stats.outputTokens, h.session.stats.cacheSamples], [21, 1]); + } finally { await h.close(); } +}); + +test("HTTP Responses duplicate completion only settles one request", { timeout: 10000 }, async () => { + const terminal = { type: "response.completed", response: { status: "completed", output: [], usage: { input_tokens: 8971, output_tokens: 13, input_tokens_details: { cached_tokens: 1024 } } } }; + const h = await harness(terminal, frame(terminal)); + try { + await h.terminal(); + assert.equal((await h.status()).contextTokensSource, "usage"); + await h.finish(); + const final = await h.status(); + assert.deepEqual([final.inputTokens, final.cachedTokens, h.session.stats.outputTokens, h.session.stats.cacheSamples, h.session.stats.requests], [8971, 1024, 13, 1, 1]); + } finally { await h.close(); } +}); + +test("HTTP Responses missing terminal usage remains an estimate until a late real report", { timeout: 10000 }, async () => { + const h = await harness({ type: "response.completed", response: { status: "completed", output: [] } }, frame({ type: "response.usage", usage: { prompt_tokens: 8971, completion_tokens: 13, prompt_tokens_details: { cached_tokens: 1024 } } })); + try { + const pending = await h.status(); + await h.terminal(); + assert.equal(h.upstreamEnded(), false); + const terminal = await h.status(); + assert.equal(terminal.contextTokensSource, "estimate"); + assert.equal(terminal.contextGeneration, pending.contextGeneration); + await h.finish(); + const final = await h.status(); + assert.deepEqual([final.contextTokensSource, final.contextTokens, final.inputTokens, final.cachedTokens, h.session.stats.outputTokens], ["usage", 8971, 8971, 1024, 13]); + assert.equal(h.session.stats.requests, 1); + } finally { await h.close(); } +}); + +for (const [name, terminal] of [ + ["zero usage", { type: "response.completed", response: { status: "completed", output: [], usage: { input_tokens: 0, output_tokens: 0 } } }], + ["output-only usage", { type: "response.completed", response: { status: "completed", output: [], usage: { output_tokens: 13 } } }], + ["failed", { type: "response.failed", response: { status: "failed", error: { code: "server_error", message: "fixture failure" } } }], + ["incomplete", { type: "response.incomplete", response: { status: "incomplete", incomplete_details: { reason: "max_output_tokens" } } }], +] as const) { + test(`HTTP Responses ${name} does not fabricate an input usage observation`, { timeout: 10000 }, async () => { + const h = await harness(terminal); + try { + const pending = await h.status(); + await h.terminal(); + assert.equal(h.upstreamEnded(), false); + const observed = await h.status(); + assert.deepEqual([observed.contextTokensSource, observed.contextTokens, observed.contextGeneration], [pending.contextTokensSource, pending.contextTokens, pending.contextGeneration]); + await h.finish(); + const final = await h.status(); + assert.equal(final.contextTokensSource, "estimate"); + assert.equal(final.inputTokens, 0); + assert.equal(h.session.stats.requests, 1); + assert.equal(h.session.stats.outputTokens, name === "output-only usage" ? 13 : 0); + assert.equal(h.session.stats.cacheSamples, 0); + } finally { await h.close(); } + }); +} + +test("HTTP Responses terminal observation nets outstanding credit without promoting an effective estimate", { timeout: 10000 }, async () => { + const h = await harness({ type: "response.completed", response: { status: "completed", output: [], usage: { input_tokens: 8971, output_tokens: 13, input_tokens_details: { cached_tokens: 1024 } } } }); + try { + h.session.stats.compressCreditTokens = 1000; + await h.terminal(); + const terminal = await h.status(); + assert.deepEqual([terminal.contextTokensSource, terminal.contextTokens, terminal.inputTokens], ["estimate", 7971, 0]); + await h.finish(); + const final = await h.status(); + assert.deepEqual([final.contextTokensSource, final.contextTokens, final.inputTokens, final.cachedTokens], ["estimate", 7971, 8971, 1024]); + assert.deepEqual([h.session.stats.outputTokens, h.session.stats.cacheSamples, h.session.stats.requests], [13, 1, 1]); + } finally { await h.close(); } +}); \ No newline at end of file From 29e2be93f7187926a67cc4e03180b86489983a15 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sun, 4 Oct 2026 13:43:43 +0800 Subject: [PATCH 08/10] fix(#2016): cap public raw-snapshot retention, fail closed (BILI_PUBLIC_SNAPSHOT_CAP_BYTES, default 16 MiB, 0 disables); child refs-namespace inheritance documented in PLUGIN.md --- PLUGIN.md | 4 +-- src/plugin.ts | 24 +++++++++++++++-- tests/mcp-native-thread.test.ts | 4 +-- tests/plugin-fork.test.ts | 48 +++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 6 deletions(-) diff --git a/PLUGIN.md b/PLUGIN.md index b9552d2869..50abebdf31 100644 --- a/PLUGIN.md +++ b/PLUGIN.md @@ -173,7 +173,7 @@ Discover `capabilities.fork` from the manifest; do not infer support from the pa } ``` -`messages` is in exactly the same order as `orderedMessages`, describes the raw `pluginSnapshot` rather than a folded view, and includes optional `text`, `toolName`, and `toolCallId`. Compare original text and role to establish the longest matching prefix; tool identities/content type must match too. Copy the corresponding ordered identities verbatim; `identityHash` and revisions are opaque and clients do not need a content-hash algorithm. Multimodal/opaque content that cannot be verified by this text projection returns `409`, `status: unavailable`, `code: SNAPSHOT_UNAVAILABLE`, not a guessed text match. Missing raw snapshots or inconsistent raw/ref/CCR mappings likewise fail closed. +`messages` is in exactly the same order as `orderedMessages`, describes the raw `pluginSnapshot` rather than a folded view, and includes optional `text`, `toolName`, and `toolCallId`. Compare original text and role to establish the longest matching prefix; tool identities/content type must match too. Copy the corresponding ordered identities verbatim; `identityHash` and revisions are opaque and clients do not need a content-hash algorithm. Multimodal/opaque content that cannot be verified by this text projection returns `409`, `status: unavailable`, `code: SNAPSHOT_UNAVAILABLE`, not a guessed text match. Missing raw snapshots or inconsistent raw/ref/CCR mappings likewise fail closed. Raw snapshot retention is capped (`BILI_PUBLIC_SNAPSHOT_CAP_BYTES`, default 16 MiB, `0` disables): a session whose serialized raw snapshot grows past the cap stops being forkable — snapshot/fork answer `409 SNAPSHOT_UNAVAILABLE` — instead of retaining an unbounded raw copy forever. `POST /__bili/plugin/fork` body: @@ -192,7 +192,7 @@ The branch is an ordered prefix, including an empty prefix. Compute its existing New success is `201`; exact payload replay is `200` with `replayed: true`, even after restart or later parent changes. Both return `status`, `parentRevision`, `childRevision`, `sessionId`, `branchPoint`, `inheritedBlocks`, and `expandedBlocks`. Reusing the child id with another parent, key or payload returns `409 CHILD_CONFLICT`. Invalid shapes/version/identifiers return `400 INVALID_REQUEST`; absent parent returns `404 PARENT_NOT_FOUND`; stale parent returns `409 PARENT_REVISION_CONFLICT`; altered order/raw/ref/identity returns `409 BRANCH_POINT_CONFLICT`. Missing nested originals return `409 PARENT_STATE_INCOMPLETE` with `status: unavailable`. Publication/persistence failure returns `503 FORK_FAILED` with `status: unavailable` and exposes no child; the same request may be retried. -Boundary handling is `exact` when complete blocks fit inside the prefix, `expanded` when a block crosses it (crossing summaries are omitted, affected nested descendants are expanded, matched originals are retained), and `unavailable` when safe reconstruction is impossible. Blocks, nested caches, raw messages, issued ref namespace, token snapshots, rules and prefix CCR payloads are independent copies. The child never reads its parent's later state as a fallback. Registration and first-request resume heuristics do not replace its copied state. With existing persistence enabled, raw snapshots, receipts, nested originals and the child's CCR payloads survive restart; with persistence disabled, durability is not promised. Atomic child publication/idempotency covers concurrent requests to one proxy process; concurrent writers sharing one state directory across processes are not supported by this protocol. +Boundary handling is `exact` when complete blocks fit inside the prefix, `expanded` when a block crosses it (crossing summaries are omitted, affected nested descendants are expanded, matched originals are retained), and `unavailable` when safe reconstruction is impossible. Blocks, nested caches, raw messages, issued ref namespace, token snapshots, rules and prefix CCR payloads are independent copies. Note that the child inherits the parent's ENTIRE issued-ref namespace (dead refs included): refs are never reused, so forks keep allocating fresh refs without colliding with ids a sibling may still hold. The child never reads its parent's later state as a fallback. Registration and first-request resume heuristics do not replace its copied state. With existing persistence enabled, raw snapshots, receipts, nested originals and the child's CCR payloads survive restart; with persistence disabled, durability is not promised. Atomic child publication/idempotency covers concurrent requests to one proxy process; concurrent writers sharing one state directory across processes are not supported by this protocol. 中文集成要点:协议仍为 1;先读取 manifest 能力及 snapshot,按原文、role、工具身份确认匹配前缀,仅提交该前缀的 orderedMessages,不猜测多模态分支点。父 revision 变化必须重新取快照并重新匹配。子会话使用独立稳定 id,首次请求可追加尚未出站的模型回复,不能重置继承的 refs/blocks。手动摘要使用 snapshot 的 refs 和 `expectedRevision: snapshot.parentRevision` 调用公开 tool;摘要期间历史发生变化时返回 409,不执行旧摘要。状态栏读取 `contextTokens`、`contextTokensSource`、`contextTokensAt`、`model`、`contextLimit`、`contextGeneration`、`sessionId` 与 `sessionRevision`,不得用累计账单 input/cache 冒充有效上下文。手动 compress/decompress 后为独立时间及 generation 的 estimate,不改累计账单或 last-usage 基线;下一次正数 usage 才恢复 usage,零值或缺失 usage 不得复活旧观测。未知来源返回 unavailable,tokens/time/generation 为 null。 diff --git a/src/plugin.ts b/src/plugin.ts index 376deb7bf3..8fa39f794d 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -305,6 +305,18 @@ export function recordPluginSession(conversationId: string, sessionId: string): /** Keep the last prepare()'s view for a plugin session so tool-API execution * sees the exact refs the model was shown (mirrors the wire-mode loop, which * runs executeProxyTool against prepared.processedMessages). */ +function publicSnapshotCapBytes(): number { + // #2016 D-B review follow-up: the raw wire snapshot is what the public + // fork API serves. Without a cap, every plugin session retains its FULL + // raw history forever (multiplied across forks) and disk grows without + // bound. When the serialized snapshot exceeds the cap we refuse to + // retain it — the session stops being forkable (snapshot/fork answer + // 409 SNAPSHOT_UNAVAILABLE with the capped reason) instead of growing + // disk forever. 0 disables the cap (operator override / tests). + const raw = Number(process.env.BILI_PUBLIC_SNAPSHOT_CAP_BYTES); + return Number.isFinite(raw) && raw >= 0 ? raw : 16_777_216; +} + export function rememberPluginMessages(sessionId: string, processed: CoreMessage[], original: CoreMessage[], nudge?: NudgeDecision, rawWire?: Buffer): void { // #1307: auxiliary requests (auto-review / classifier prompts) bound to the // same session key can carry a normal output budget and any message count, @@ -335,7 +347,15 @@ export function rememberPluginMessages(sessionId: string, processed: CoreMessage remembered.set(sessionId, { processed, original, nudge }); const session = peekSession(sessionId); if (session && typeof session.metadata.pluginAgent === "string" && original.length > 0) { - session.pluginSnapshot = structuredClone(original); + const snapshotCandidate = structuredClone(original); + const cap = publicSnapshotCapBytes(); + if (cap > 0 && stableJson(snapshotCandidate).length > cap) { + session.pluginSnapshot = undefined; + session.metadata.publicSnapshotCapped = true; + } else { + session.pluginSnapshot = snapshotCandidate; + delete session.metadata.publicSnapshotCapped; + } const previousRefs = session.metadata.publicSnapshotStoredRefs; session.metadata.publicSnapshotStoredRefs = [...new Set([ ...(Array.isArray(previousRefs) ? previousRefs : []), @@ -778,7 +798,7 @@ export function publicForkInputMatches(session: Session, protocol: WireProtocol, function forkSnapshot(session: Session) { const messages = session.pluginSnapshot; - if (!messages) throw new Error("raw snapshot unavailable; send a fresh plugin model request"); + if (!messages) throw new Error(session.metadata.publicSnapshotCapped === true ? "raw snapshot exceeded the retention cap (BILI_PUBLIC_SNAPSHOT_CAP_BYTES); fork is refused rather than retaining an unbounded raw copy" : "raw snapshot unavailable; send a fresh plugin model request"); if (session.metadata.publicSnapshotTextComparable === false || messages.some((m) => (m.thinkingTokens ?? 0) > 0)) throw new Error("multimodal or opaque content cannot be compared by text"); const store = contentStoreOf(session); const expectedStoredRefs = session.metadata.publicSnapshotStoredRefs; diff --git a/tests/mcp-native-thread.test.ts b/tests/mcp-native-thread.test.ts index fb0c7035c3..e6a98e4745 100644 --- a/tests/mcp-native-thread.test.ts +++ b/tests/mcp-native-thread.test.ts @@ -468,8 +468,8 @@ test("#2024 proxy: host-stamped native caller outranks a conflicting unique witn resetToolRingForTest(); await modelTurn(h, "nt-b", "WITNESS:acp_cache second beta witness turn"); r = await toolPost(h, { tool: "acp_cache", args: {}, conversationId: "nt-a" }); - assert.equal(r.status, 409); - assert.equal(r.json?.code, "TOOL_CONVERSATION_CONFLICT"); + assert.equal(r.status, 409, "unmarked resolvable body id contradicted by a unique witness must fail closed (#2016)"); + assert.equal((r.json as Record | null)?.code, "TOOL_CONVERSATION_CONFLICT"); } finally { await h.stop(); } diff --git a/tests/plugin-fork.test.ts b/tests/plugin-fork.test.ts index eb244d8e02..e5eb0cad69 100644 --- a/tests/plugin-fork.test.ts +++ b/tests/plugin-fork.test.ts @@ -990,3 +990,51 @@ test("HTTP nested summaries expand crossing ancestors and retain independent ori assert.notEqual(resolveConversation("nested").session!.state.blocks[1].directBlockIds, parent.state.blocks[1].directBlockIds); } finally { await h.close(); } }); + +test("HTTP raw-snapshot retention cap fails closed instead of growing without bound (D-B)", async () => { + // #2016 review: without a cap every plugin session retains its full raw + // history forever. With BILI_PUBLIC_SNAPSHOT_CAP_BYTES set tiny, the + // session refuses to retain the snapshot and the public endpoints answer + // 409 SNAPSHOT_UNAVAILABLE with the capped reason — never a stale guess. + const previous = process.env.BILI_PUBLIC_SNAPSHOT_CAP_BYTES; + process.env.BILI_PUBLIC_SNAPSHOT_CAP_BYTES = "1"; + const h = await harness(); + try { + const session = resolveConversation("parent").session!; + assert.equal(session.pluginSnapshot, undefined, "an over-cap raw snapshot must not be retained"); + assert.equal(session.metadata.publicSnapshotCapped, true); + const snapshot = await h.request("/__bili/plugin/snapshot?conversationId=parent"); + assert.equal(snapshot.status, 409); + assert.equal((snapshot.body as { error?: string }).error, "Error: raw snapshot exceeded the retention cap (BILI_PUBLIC_SNAPSHOT_CAP_BYTES); fork is refused rather than retaining an unbounded raw copy"); + const fork = await h.request("/__bili/plugin/fork", { protocolVersion: 1, parentConversationId: "parent", childConversationId: "child", parentRevision: "a".repeat(64), branchPoint: { messageCount: 0, orderHash: createHash("sha256").update("[]").digest("hex") }, orderedMessages: [], idempotencyKey: "op-1" }); + assert.equal(fork.status, 409); + assert.equal(fork.body.code, "SNAPSHOT_UNAVAILABLE"); + } finally { + if (previous === undefined) delete process.env.BILI_PUBLIC_SNAPSHOT_CAP_BYTES; + else process.env.BILI_PUBLIC_SNAPSHOT_CAP_BYTES = previous; + await h.close(); + } +}); + +test("HTTP raw-snapshot retention cap lifts when the raw history shrinks below it again", async () => { + const previous = process.env.BILI_PUBLIC_SNAPSHOT_CAP_BYTES; + process.env.BILI_PUBLIC_SNAPSHOT_CAP_BYTES = "1"; + const h = await harness(); + try { + const session = getSession("cap-recover"); + session.metadata.pluginAgent = "test"; + const messages = [{ id: "cap-recovered", role: "user" as const, contentType: "text" as const, text: "resending the whole history keeps ids stable" }]; + rememberPluginMessages(session.id, messages, messages); + assert.equal(session.pluginSnapshot, undefined); + assert.equal(session.metadata.publicSnapshotCapped, true); + process.env.BILI_PUBLIC_SNAPSHOT_CAP_BYTES = String(64 * 1024 * 1024); + rememberPluginMessages(session.id, messages, messages); + const recovered: unknown = session.pluginSnapshot; + assert.equal(Array.isArray(recovered) && recovered.length, 1, "an under-cap resend must restore the retained snapshot"); + assert.equal(session.metadata.publicSnapshotCapped, undefined); + } finally { + if (previous === undefined) delete process.env.BILI_PUBLIC_SNAPSHOT_CAP_BYTES; + else process.env.BILI_PUBLIC_SNAPSHOT_CAP_BYTES = previous; + await h.close(); + } +}); From d9b44fc35a75a6d1665a96dd2fa4d600442fae84 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sun, 4 Oct 2026 14:17:29 +0800 Subject: [PATCH 09/10] fix(#2017): cache the fork revision behind a mutation epoch; document x-bili-plugin-agent: main MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Status polling re-hashed the entire history plus the CCR store (payload by payload) on every call (review item E). forkSnapshot() is a pure function of the session content, so the revision is now cached keyed by revisionEpoch — a monotone counter bumped by markDirty after every mutation. Unchanged sessions answer revision reads in O(1); any mutation invalidates and the next read recomputes. The tool expectedRevision check uses the same cached read under the session lock and still fails closed (409) on staleness, pinned by a new provenance test. PLUGIN.md: one paragraph for x-bili-plugin-agent: main (veto of side-request demotion for full main turns) — previously undocumented (ework second-round open item). --- PLUGIN.md | 2 ++ src/plugin.ts | 19 +++++++++++++++++-- src/session.ts | 9 +++++++++ tests/plugin-status-provenance.test.ts | 23 +++++++++++++++++++++-- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/PLUGIN.md b/PLUGIN.md index 50abebdf31..4309f16d27 100644 --- a/PLUGIN.md +++ b/PLUGIN.md @@ -64,6 +64,8 @@ On **every model request** the plugin sends: - `x-bili-plugin-context-window: ` (optional but recommended) — the model's context window as configured inside the agent (e.g. a pinned/overridden `contextWindow`). This becomes the authoritative "native" window for nudge decisions — it outranks the proxy's built-in table and the models.dev registry (most valuable for private relays and MITM mode), while operator tuning (`compress.modelContextLimit`) still outranks it. - `x-bili-plugin-instructions-mutable: 1` (optional; send only if true) — declares that your conversation ids are **persona-scoped**: one id per persona, and the request's system/instructions text may change mid-conversation without changing the persona (e.g. opencode re-renders AGENTS.md into `instructions` on every edit). The proxy now keys plugin conversation ids verbatim by default (#1106), so this flag is vestigial for current proxies — keep stamping it for compatibility with older proxy versions, which use it to stop mixing the instructions text into the compression-namespace fingerprint. +- `x-bili-plugin-agent: main` (optional) — vetoes side-request demotion for this one request. Requests are otherwise demoted to the verbatim side lane when they look like auxiliary traffic (#388: small `max_tokens`, or a known side persona such as opencode's title generator). A full main turn that re-sends the conversation can still trip those heuristics; stamping `main` keeps it on the kernel path so it captures history, stays compressible and remains forkable. Any other value declares the side persona id itself (it is then matched against the known side-request persona set, not treated as `main`). + Effects on the proxy for that session: - Wire-level tool injection is **suppressed** (tools are native; no duplicates). diff --git a/src/plugin.ts b/src/plugin.ts index 8fa39f794d..f23b313715 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -796,6 +796,21 @@ export function publicForkInputMatches(session: Session, protocol: WireProtocol, } } +/** Cheap, cache-backed read of the fork parentRevision. forkSnapshot() is a + * pure function of the session content and status polling would otherwise + * re-hash the entire history (plus the CCR store, payload by payload) on + * every call (#2017 review item E); revisionEpoch — bumped by markDirty on + * every mutation — keys the cache, so an unchanged session costs O(1). + * Same fail-closed errors as forkSnapshot itself propagate to the caller. */ +function sessionRevisionOf(session: Session): string { + const epoch = session.revisionEpoch ?? 0; + const cache = session.pluginRevisionCache; + if (cache && cache.epoch === epoch) return cache.revision; + const revision = forkSnapshot(session).parentRevision; + session.pluginRevisionCache = { epoch, revision }; + return revision; +} + function forkSnapshot(session: Session) { const messages = session.pluginSnapshot; if (!messages) throw new Error(session.metadata.publicSnapshotCapped === true ? "raw snapshot exceeded the retention cap (BILI_PUBLIC_SNAPSHOT_CAP_BYTES); fork is refused rather than retaining an unbounded raw copy" : "raw snapshot unavailable; send a fresh plugin model request"); @@ -1085,7 +1100,7 @@ export function handlePluginStatus(conversationId: string, res: import("node:htt const contextTokensSource = observation?.source ?? "unavailable"; const contextTokens = observation?.tokens ?? null; let sessionRevision: string | null = null; - try { sessionRevision = forkSnapshot(session).parentRevision; } catch {} + try { sessionRevision = sessionRevisionOf(session); } catch {} const mem = remembered.get(session.id); const modelContextLimit = typeof limit === "number" && limit > 0 ? limit : 0; // #387: the remembered nudge is a prepare-time snapshot. A compress tool @@ -1352,7 +1367,7 @@ export async function handlePluginTool( result = await withSessionLock(session, async () => { if (parsed.expectedRevision !== undefined) { try { - if (forkSnapshot(session).parentRevision !== parsed.expectedRevision) { + if (sessionRevisionOf(session) !== parsed.expectedRevision) { forkReply(res, 409, { ok: false, code: "PARENT_REVISION_CONFLICT", error: "session revision changed" }); return undefined; } diff --git a/src/session.ts b/src/session.ts index 26a394c92c..3ed16b732b 100644 --- a/src/session.ts +++ b/src/session.ts @@ -288,6 +288,14 @@ export type Session = { * request — the client re-sends full raw history, restoring the invariant. */ lastMessagesFolded?: boolean; pluginSnapshot?: CoreMessage[]; + /** In-memory only (NOT persisted — buildRecord omits it): monotone counter + * bumped by markDirty after every mutation. Keys the fork-revision cache + * below so status polling skips re-hashing the whole history (#2017). */ + revisionEpoch?: number; + /** In-memory only (NOT persisted): cached fork parentRevision valid for + * the current revisionEpoch — forkSnapshot() is a pure function of the + * session content, so same epoch ⇒ same hash. */ + pluginRevisionCache?: { epoch: number; revision: string }; /** Kernel CCR envelope (#1097): originals of ID-referenced tool results, * owned and mutated only by kernel processTurn (ccr-store node) via * adoptContentStore. Lazily loaded from the session's content-store.json; @@ -701,6 +709,7 @@ export function snapshotMessages(session: Session, messages: CoreMessage[]): voi /** Mark a session's state as changed so it is persisted on the next debounce. * Call this after any mutation (processTurn, compress, decompress, orphan GC). */ export function markDirty(session: Session): void { + session.revisionEpoch = (session.revisionEpoch ?? 0) + 1; getStore().scheduleSave(session); } diff --git a/tests/plugin-status-provenance.test.ts b/tests/plugin-status-provenance.test.ts index 6eeb0daa31..719e7ddeca 100644 --- a/tests/plugin-status-provenance.test.ts +++ b/tests/plugin-status-provenance.test.ts @@ -76,7 +76,7 @@ async function harness() { const tool = (name: string, args: Record) => request<{ result: string }>("/__bili/plugin/tool", { conversationId: "provenance", tool: name, args }); const compressArgs = { content: [{ startId: "m00001", endId: "m00002", summary: "The prefix preserves the first user request and second assistant response. Original content remains available for decompression using stable references." }] }; await send(); - return { status, send, tool, messages, compressArgs, setUsage: (value: typeof usage) => { usage = value; }, holdNext: () => { + return { status, send, tool, messages, compressArgs, origin, setUsage: (value: typeof usage) => { usage = value; }, holdNext: () => { let entered!: () => void; let release!: () => void; const waiting = new Promise((resolve) => { entered = resolve; }); @@ -243,4 +243,23 @@ test("HTTP CCR range decompress accounts for queued restored content without cha assert.equal(session.stats.lastUsageGradeTokens, baseline); assert.deepEqual([current.inputTokens, current.cachedTokens], [folded.inputTokens, folded.cachedTokens]); } finally { await h.close(); } -}); \ No newline at end of file +}); +test("HTTP status sessionRevision is cache-stable while unchanged and moves after a mutation (#2017 item E)", async () => { + const h = await harness(); + try { + await h.send(); + const first = await h.status(); + assert.ok(first.sessionRevision, "snapshot available in this harness"); + const again = await h.status(); + assert.equal(again.sessionRevision, first.sessionRevision, "unchanged session: repeated polls return the identical revision (epoch-keyed cache)"); + await h.send([...h.messages, { role: "assistant", content: "an intervening answer" }, { role: "user", content: "next original turn" }]); + const moved = await h.status(); + assert.notEqual(moved.sessionRevision, first.sessionRevision, "a real mutation invalidates the cached revision"); + // The cached value is exactly what expectedRevision accepts under the + // session lock (sessionRevisionOf is the check's data source). + const ok = await fetch(`${h.origin}/__bili/plugin/tool`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ conversationId: "provenance", tool: "acp_status", args: {}, expectedRevision: moved.sessionRevision }) }); + assert.equal(ok.status, 200); + const stale = await fetch(`${h.origin}/__bili/plugin/tool`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ conversationId: "provenance", tool: "acp_status", args: {}, expectedRevision: first.sessionRevision }) }); + assert.equal(stale.status, 409, "stale cached revision must fail closed"); + } finally { await h.close(); } +}); From 830da41e0ce42566f9256b608b55626a0ce0dd53 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Sun, 4 Oct 2026 15:40:06 +0800 Subject: [PATCH 10/10] fix(#2017): register BILI_PUBLIC_SNAPSHOT_CAP_BYTES through knobs.ts (#2030 discipline) New master (#2030) requires every behavior knob to resolve env > config file > default through src/knobs.ts. The snapshot cap was a bare process.env read in plugin.ts; it now resolves BILI_PUBLIC_SNAPSHOT_CAP_ BYTES > plugin.snapshotCapBytes > 16 MiB (0 disables) with the env tier byte-exact to the pre-migration parser (Number(env): set-but-garbage falls back to default, "" resolves to 0). CONFIGURATION.md en/zh gain the plugin block, the mapping-table row and the semantics row; reference/env-vars.md lists plugin.* in the tunables home. Precedence pinned in tests/knobs-config-precedence.test.ts (default / file tier / garbage env / empty-string quirk / garbage file). --- CONFIGURATION.md | 9 +++++++++ CONFIGURATION.zh-CN.md | 9 +++++++++ reference/env-vars.md | 2 +- src/config.ts | 6 ++++++ src/knobs.ts | 17 +++++++++++++++++ src/plugin.ts | 13 +------------ tests/knobs-config-precedence.test.ts | 14 +++++++++++++- 7 files changed, 56 insertions(+), 14 deletions(-) diff --git a/CONFIGURATION.md b/CONFIGURATION.md index f4b14a09f8..f73b6ae6c8 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -321,6 +321,13 @@ Since #2030 every pure-behavior knob has a config-file key alongside its env var - **Status:** ACTIVE - **Description:** `max` caps in-memory sessions with LRU eviction (twin `BILI_MAX_SESSIONS`; disk remains the source of truth). `gc` controls stale-session-file cleanup (twins `BILI_SESSION_GC*`) — **opt-in**, because session files are user data: compressed sessions are never deleted, deletions are audit-logged, and the age + lossless-size gates apply. Details in the [env table](#config-file-keys-for-environment-knobs-2030). +### `plugin` + +- **Type:** `{ snapshotCapBytes?: number }` +- **Default:** `{ snapshotCapBytes: 16777216 }` +- **Status:** ACTIVE +- **Description:** Plugin-surface knobs (#2017). `snapshotCapBytes` caps the raw wire-history snapshot retained per plugin session for the public fork API (`GET /__bili/plugin/snapshot`, `POST /__bili/plugin/fork`): when the serialized snapshot exceeds the cap, bili refuses to retain it — the session stops being forkable (snapshot/fork answer `409` with the capped reason) instead of retaining an unbounded raw copy on disk. Default `16 MiB`; `0` disables retention entirely (no session is forkable); twins `BILI_PUBLIC_SNAPSHOT_CAP_BYTES`. Details in the [env table](#config-file-keys-for-environment-knobs-2030). + ### `update` - **Type:** `{ registry?: string; checkIntervalMs?: number }` @@ -956,6 +963,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes | `BILI_SESSION_GC_MAX_AGE_DAYS` | `sessions.gc.maxAgeDays` | `7` | | `BILI_SESSION_GC_MAX_TOKENS` | `sessions.gc.maxTokens` | `1000000` | | `BILI_SESSION_GC_INTERVAL_MS` | `sessions.gc.intervalMs` | `3600000` | +| `BILI_PUBLIC_SNAPSHOT_CAP_BYTES` | `plugin.snapshotCapBytes` | `16777216` | | `BILI_UPDATE_REGISTRY` | `update.registry` | npm public registry | | `BILI_UPDATE_CHECK_INTERVAL_MS` | `update.checkIntervalMs` | `180000` | | `BILI_CCR_RETRIEVAL_TTL_MS` | `ccrRetrievalTtlMs` | `600000` | @@ -1045,6 +1053,7 @@ File keys resolve only when the matching env var is unset. Defaults in parenthes | `BILI_PERSIST_EPERM_ALERT_REPEAT_MS` | Re-alert window for the persist EPERM alert, in ms. `0` (default) = alert once then stay silent; `>0` = re-alert at most every that many ms while the failures continue. | | `BILI_TUNNEL_ALLOWED_HOSTS` | `/bili/` tunnel admission for **remote clients** (#409): comma-separated `host` or `host:port` entries that unlock loopback/private destinations (e.g. a LAN relay or the machine's own sglang) for non-loopback clients. The proxy itself and link-local/metadata addresses are always denied; local (loopback) clients always pass. | | `BILI_MAX_SESSIONS` | Max sessions held in memory (default `256`; LRU eviction — disk is the source of truth). | +| `BILI_PUBLIC_SNAPSHOT_CAP_BYTES` | Retention cap (bytes) for the raw wire-history snapshot kept per plugin session to serve the public fork API (#2017). A plugin session whose serialized snapshot exceeds the cap stops being forkable — `GET /__bili/plugin/snapshot` and `POST /__bili/plugin/fork` fail closed with `409` and the capped reason — instead of retaining an unbounded raw copy of the history forever. The cap is re-evaluated on every plugin model request: a session that shrinks back under the cap (after a fork trimmed it, or the host shrank the history) resumes being forkable. Default `16777216` (16 MiB); `0` disables retention entirely (no session is forkable; existing snapshots are dropped on the next request). File twin: `plugin.snapshotCapBytes`. | | `BILI_SESSIONS_DIR` | Directory for persisted session state (default XDG data dir). | | `BILI_SESSION_GC` | Cleanup of stale session files (#1082) is **opt-in**: set to `1`/`true`/`on` to enable — off by default, because session files are user data (exportable, resumable) and there is no silent deletion policy. When enabled, the sweep (boot + hourly) deletes a file only when BOTH conditions hold: older than `BILI_SESSION_GC_MAX_AGE_DAYS`, AND small in the lossless sense — the session was **never compressed** (zero folded blocks) and its newest request body ≤ the token ceiling below, so resuming it costs one cold rebuild from the client's own history and nothing else. Safety rails: compressed sessions are NEVER deleted (their summaries cannot be rebuilt losslessly); a session still held in memory is skipped unless idle since its last disk write; unreadable/corrupt files are left in place; every deletion is audit-logged individually (path, size, age) plus one summary line per non-empty sweep; only the sessions dir is ever touched; emptied protocol subdirectories are removed. Note the resident guard is per-process: another proxy instance sharing `BILI_SESSIONS_DIR` that does not persist (e.g. `BILI_PERSIST=0`) never refreshes file mtimes, so its still-live session files can age out and be swept — the cost is the same bounded cold rebuild, backstopped by the age gate. CCR content stores (#1097) share the session's lifecycle (#1180): a `.content-store.json` companion is deleted together with its session file, an orphaned companion (session file already gone) is swept once past the age gate, and an unreadable companion keeps its session file too (never guessed at). | | `BILI_SESSION_GC_MAX_AGE_DAYS` | Minimum age (days) before a session file becomes a GC candidate (default `7`). Keep it far beyond any plausible resume window: after deletion a resumed session restarts message numbering from m00001 while a resuming agent's transcript may still cite old numbers (kernel contract: ids are never reused). | diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index ba832e2b69..8cb5e684d3 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -321,6 +321,13 @@ - **状态:** ACTIVE - **说明:** `max` 以 LRU 淘汰约束内存会话数(对应 `BILI_MAX_SESSIONS`;磁盘仍是事实来源)。`gc` 控制陈旧会话文件清理(对应 `BILI_SESSION_GC*`)—— **显式开启**,因为会话文件是用户数据:压缩过的会话永不删除、每次删除都审计记录、年龄 + 无损尺寸双闸门同时满足才删。详见[环境变量表](#环境变量的配置键对照-2030)。 +### `plugin` + +- **类型:** `{ snapshotCapBytes?: number }` +- **默认:** `{ snapshotCapBytes: 16777216 }` +- **状态:** ACTIVE +- **说明:** 插件面开关(#2017)。`snapshotCapBytes` 限制为公开 fork API(`GET /__bili/plugin/snapshot`、`POST /__bili/plugin/fork`)按插件会话保留的原始 wire 历史快照大小:序列化快照超限时 bili 拒绝留存 —— 该会话不再可 fork(snapshot/fork 返回 `409` 并注明原因),而不是在磁盘上无限保留全量原始副本。默认 `16 MiB`;`0` 完全停用留存;对应环境变量 `BILI_PUBLIC_SNAPSHOT_CAP_BYTES`。详见[环境变量表](#环境变量的配置键对照-2030)。 + ### `update` - **类型:** `{ registry?: string; checkIntervalMs?: number }` @@ -961,6 +968,7 @@ | `BILI_SESSION_GC_MAX_AGE_DAYS` | `sessions.gc.maxAgeDays` | `7` | | `BILI_SESSION_GC_MAX_TOKENS` | `sessions.gc.maxTokens` | `1000000` | | `BILI_SESSION_GC_INTERVAL_MS` | `sessions.gc.intervalMs` | `3600000` | +| `BILI_PUBLIC_SNAPSHOT_CAP_BYTES` | `plugin.snapshotCapBytes` | `16777216` | | `BILI_UPDATE_REGISTRY` | `update.registry` | npm 公共 registry | | `BILI_UPDATE_CHECK_INTERVAL_MS` | `update.checkIntervalMs` | `180000` | | `BILI_CCR_RETRIEVAL_TTL_MS` | `ccrRetrievalTtlMs` | `600000` | @@ -1047,6 +1055,7 @@ | `BILI_PERSIST_EPERM_ALERT_THRESHOLD` | 同一会话连续 N 次持久化写失败(`EPERM`/`EBUSY`/`EACCES`)后触发一次性「把该目录加入杀软排除项」告警的阈值(默认 `5`)。仅 Windows。见下文「Windows:把会话目录加入杀软排除项」章节。 | | `BILI_PERSIST_EPERM_ALERT_REPEAT_MS` | persist EPERM 告警的重复窗口(毫秒)。`0`(默认)= 只告警一次后静默;`>0` = 失败持续期间最多每这么久重复告警一次。 | | `BILI_MAX_SESSIONS` | 内存中最多保留的会话数(默认 `256`;LRU 淘汰 —— 磁盘是事实源)。 | +| `BILI_PUBLIC_SNAPSHOT_CAP_BYTES` | 为公开 fork API 按插件会话保留的原始 wire 历史快照的大小上限(字节,#2017)。序列化快照超限的插件会话不再可 fork —— `GET /__bili/plugin/snapshot` 与 `POST /__bili/plugin/fork` 以 `409` fail-closed 并注明原因 —— 而不是永久保留无上限的原始历史副本。上限在每次插件模型请求时重新评估:会话缩回上限内(fork 裁剪后或宿主缩短历史)即恢复可 fork。默认 `16777216`(16 MiB);`0` 完全停用留存(所有会话不可 fork,已有快照在下一次请求时丢弃)。文件孪生键:`plugin.snapshotCapBytes`。 | | `BILI_SESSIONS_DIR` | 会话持久化目录(默认 XDG data 目录)。 | | `BILI_SESSION_GC` | 过期会话文件清理(#1082)为**可选开启**:设 `1`/`true`/`on` 启用 —— 默认关闭,因为会话文件是用户数据(可导出、可续聊),不应有静默删除策略。启用后,扫描(启动 + 每小时)只在**两个条件同时满足**时删除一个文件:年龄超过 `BILI_SESSION_GC_MAX_AGE_DAYS`,并且"小"到无损 —— 该会话**从未被压缩过**(零折叠块)且最近一次请求体 ≤ 下述 token 上限,这样继续对话只损失一次冷重建(用客户端自己的历史重建),别无其他。安全边界:被压缩过的会话永不删除(其摘要无法无损重建);内存中仍持有的会话会被跳过,除非该会话自上次落盘后一直空闲;不可读/损坏的文件原地保留;每次删除逐条写审计日志(路径、大小、年龄),另有一次非空扫描的汇总日志;只触碰会话目录;清空后的协议子目录一并删除。注意 resident 守卫是进程内的:共享 `BILI_SESSIONS_DIR` 但不落盘的另一实例(如 `BILI_PERSIST=0`)不会刷新文件 mtime,其仍活跃的会话文件可能老化被扫 —— 代价同样是有限的一次冷重建,且有年龄门兜底。CCR 内容存储(#1097)与会话共享生命周期(#1180):`.content-store.json` 伴随文件随其会话文件一起删除;孤儿伴随文件(会话文件已不存在)超过年龄门后被清扫;不可读的伴随文件会连同其会话文件一起保留(绝不猜测)。 | | `BILI_SESSION_GC_MAX_AGE_DAYS` | 会话文件成为清理候选的最小年龄(天,默认 `7`)。必须远超任何合理续聊窗口:文件删除后同会话再续聊,消息编号会从 m00001 重新分配,而续聊 agent 的转录里可能还引用着旧编号(内核契约:编号永不复用)。 | diff --git a/reference/env-vars.md b/reference/env-vars.md index 33c1bb337d..6947be4c98 100644 --- a/reference/env-vars.md +++ b/reference/env-vars.md @@ -17,7 +17,7 @@ precisely so knobs don't bloat the environment. Precedence everywhere: | Category | Home | Examples | |----------|------|----------| -| Behavior tunables (timeouts, caps, modes, diagnostics) | config-file key resolved through `src/knobs.ts`; env stays the override tier | `network.*`, `persist.*`, `sessions.*`, `update.*`, `diagnostics.*`, `fakeCompletion.*` | +| Behavior tunables (timeouts, caps, modes, diagnostics) | config-file key resolved through `src/knobs.ts`; env stays the override tier | `network.*`, `persist.*`, `sessions.*`, `update.*`, `diagnostics.*`, `fakeCompletion.*`, `plugin.*` | | Secrets / credentials | env only (never written to disk) | `BILI_LAUNCH_TOKEN`, `BILI_ENCRYPTION_KEY` | | Per-process channels (written by another bili component at spawn) | env only + comment at the read site naming the writer | `BILI_MCP_PROXY`, `BILI_PARENT_PID`, `BILI_STRICT_PORT`, `BILI_OPENCODE_ACP_SPEC`, `BILI_LAUNCHER_MODEL_*` | | Host-side posture (read inside a third-party host process at bootstrap) | env only + comment at the read site | `BILLION_CONTEXT_PLUGIN*`, `BILI_NATIVE_*`, `BILI_RECLAIM_FETCH_PATCH` | diff --git a/src/config.ts b/src/config.ts index 8c311e7add..bca7b81452 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1378,6 +1378,12 @@ type FileConfig = { max?: number; gc?: { enabled?: boolean; maxAgeDays?: number; maxTokens?: number; intervalMs?: number }; }; + /** Plugin-surface knobs (#2017). `snapshotCapBytes` caps the raw + * wire-history snapshot retained per plugin session for the public + * fork API — beyond the cap the session stops being forkable (409) + * instead of retaining an unbounded raw copy. Default 16 MiB; `0` + * disables retention entirely; env BILI_PUBLIC_SNAPSHOT_CAP_BYTES wins. */ + plugin?: { snapshotCapBytes?: number }; /** Updater knobs (#2030) — was BILI_UPDATE_REGISTRY / BILI_UPDATE_CHECK_INTERVAL_MS. */ update?: { registry?: string; checkIntervalMs?: number }; /** Diagnostics & debug surface (#2030) — was ACP_DUMP_BODY / ACP_DUMP_REQ / diff --git a/src/knobs.ts b/src/knobs.ts index 54b180fe82..b6033395d4 100644 --- a/src/knobs.ts +++ b/src/knobs.ts @@ -247,6 +247,23 @@ export function updateCheckIntervalMs(): number { // --- CCR / codex / misc host-adjacent knobs --- +/** BILI_PUBLIC_SNAPSHOT_CAP_BYTES > plugin.snapshotCapBytes > 16 MiB (#2017 + * D-B). Caps the raw wire-history snapshot retained per plugin session for + * the public fork API; beyond it the session fails closed (409) instead of + * retaining an unbounded raw copy. `0` disables retention entirely. Env tier + * preserves the original plugin.ts parsing byte-exact: set (even empty/garbage + * → Number() semantics) wins; NaN/negative falls back to the default. */ +export function publicSnapshotCapBytes(): number { + const raw = process.env.BILI_PUBLIC_SNAPSHOT_CAP_BYTES; + const v = raw !== undefined + ? Number(raw) + : (() => { + const f = loadConfigFile().plugin?.snapshotCapBytes; + return typeof f === "number" && Number.isFinite(f) ? f : NaN; + })(); + return Number.isFinite(v) && v >= 0 ? v : 16_777_216; +} + /** BILI_CCR_RETRIEVAL_TTL_MS > ccrRetrievalTtlMs > 600000. */ export function ccrRetrievalTtlMs(): number { const envRaw = process.env.BILI_CCR_RETRIEVAL_TTL_MS; diff --git a/src/plugin.ts b/src/plugin.ts index f23b313715..68b845eecc 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -1,4 +1,5 @@ import { type CompressionCore, type Config, type CoreMessage, type NudgeDecision, countMessageTokens, parseStoredPlaceholder, prune } from "acp-kernel"; +import { publicSnapshotCapBytes } from "./knobs.js"; import { buildStatusPanel } from "acp-kernel/panel"; import { fileURLToPath } from "node:url"; import type { ServerResponse } from "node:http"; @@ -305,18 +306,6 @@ export function recordPluginSession(conversationId: string, sessionId: string): /** Keep the last prepare()'s view for a plugin session so tool-API execution * sees the exact refs the model was shown (mirrors the wire-mode loop, which * runs executeProxyTool against prepared.processedMessages). */ -function publicSnapshotCapBytes(): number { - // #2016 D-B review follow-up: the raw wire snapshot is what the public - // fork API serves. Without a cap, every plugin session retains its FULL - // raw history forever (multiplied across forks) and disk grows without - // bound. When the serialized snapshot exceeds the cap we refuse to - // retain it — the session stops being forkable (snapshot/fork answer - // 409 SNAPSHOT_UNAVAILABLE with the capped reason) instead of growing - // disk forever. 0 disables the cap (operator override / tests). - const raw = Number(process.env.BILI_PUBLIC_SNAPSHOT_CAP_BYTES); - return Number.isFinite(raw) && raw >= 0 ? raw : 16_777_216; -} - export function rememberPluginMessages(sessionId: string, processed: CoreMessage[], original: CoreMessage[], nudge?: NudgeDecision, rawWire?: Buffer): void { // #1307: auxiliary requests (auto-review / classifier prompts) bound to the // same session key can carry a normal output budget and any message count, diff --git a/tests/knobs-config-precedence.test.ts b/tests/knobs-config-precedence.test.ts index aa7b6824c7..d2ea0f0973 100644 --- a/tests/knobs-config-precedence.test.ts +++ b/tests/knobs-config-precedence.test.ts @@ -58,6 +58,7 @@ test("defaults: no env, no file", () => { assert.equal(knobs.mitmHandshakeTimeoutMs(), 10_000); assert.equal(knobs.persistEnabled(), true); assert.equal(knobs.persistZstdEnabled(), false); + assert.equal(knobs.publicSnapshotCapBytes(), 16_777_216); assert.equal(knobs.persistDebounceMs(), 500); assert.equal(knobs.persistTailTokens(), 16384); assert.equal(knobs.persistEpermAlertThreshold(), 5); @@ -71,6 +72,7 @@ test("defaults: no env, no file", () => { assert.equal(knobs.updateRegistryBase(), undefined); assert.equal(knobs.updateCheckIntervalMs(), 180_000); assert.equal(knobs.ccrRetrievalTtlMs(), 10 * 60 * 1000); + assert.equal(knobs.publicSnapshotCapBytes(), 16_777_216); assert.equal(knobs.codexCompactMode(), "intercept"); assert.equal(knobs.decompressTmpCap(), 50); assert.equal(knobs.bodyDumpEnabled(), false); @@ -105,6 +107,7 @@ test("file tier: every migrated block resolves from the config file", () => { countTokensPassthrough: true, compressProtocol: "text", }, fakeCompletion: { retries: 2, bufCapBytes: 1048576 }, + plugin: { snapshotCapBytes: 999 }, codexCompact: "pass", ccrRetrievalTtlMs: 300000, decompressTmpCap: 7, @@ -112,6 +115,7 @@ test("file tier: every migrated block resolves from the config file", () => { compat: { noCacheControl: true, keepResponseId: true }, }); assert.equal(knobs.upstreamTimeoutMs(), 60000); + assert.equal(knobs.publicSnapshotCapBytes(), 999); assert.equal(knobs.requestWatchdogBudgetMs(), 90000); assert.equal(knobs.keepAliveTimeoutMs(), 7000); assert.equal(knobs.clientErrorBackstopMs(), 45000); @@ -183,11 +187,13 @@ test("env tier always wins over the file (live test seams)", () => { test("set env owns the knob: garbage env resolves exactly as pre-migration", () => { setConfig({ network: { upstreamTimeoutMs: 60000, replayRetryBaseMs: 250, proxyKeepAliveMaxMs: 60000 }, persist: { debounceMs: 900 } }); + setConfig({ plugin: { snapshotCapBytes: 999 } }); withEnv({ BILI_UPSTREAM_TIMEOUT_MS: "abc", BILI_REPLAY_RETRY_BASE_MS: "xyz", BILI_PROXY_KEEPALIVE_MAX_MS: "junk", BILI_PERSIST_DEBOUNCE_MS: "soon", + BILI_PUBLIC_SNAPSHOT_CAP_BYTES: "junk", }, () => { // each parses exactly as it did before the migration: garbage → default; // the file tier is untouched by a stale export @@ -195,6 +201,12 @@ test("set env owns the knob: garbage env resolves exactly as pre-migration", () assert.equal(knobs.replayBaseDelayMs(), REPLAY_BASE_DELAY_MS); assert.equal(knobs.proxyKeepAliveMaxMs(), PROXY_KEEPALIVE_MAX_MS); assert.equal(knobs.persistDebounceMs(), 500); + assert.equal(knobs.publicSnapshotCapBytes(), 16_777_216); + }); + // the plugin cap's pre-migration parser (Number(env)) treated "" as 0 — + // "disables retention" — and that quirk survives byte-exact + withEnv({ BILI_PUBLIC_SNAPSHOT_CAP_BYTES: "" }, () => { + assert.equal(knobs.publicSnapshotCapBytes(), 0); }); // loose parsers historically guarded with `if (!raw)`: empty env = unset, // so the file tier applies where no value was really given @@ -205,7 +217,7 @@ test("set env owns the knob: garbage env resolves exactly as pre-migration", () }); test("garbage file values fall back to defaults, never throw", () => { - setConfig({ network: { upstreamTimeoutMs: "fast", replayRetryMax: -3 }, persist: { tailTokens: null }, ccrRetrievalTtlMs: "whenever" }); + setConfig({ network: { upstreamTimeoutMs: "fast", replayRetryMax: -3 }, persist: { tailTokens: null }, ccrRetrievalTtlMs: "whenever", plugin: { snapshotCapBytes: "huge" } }); assert.equal(knobs.upstreamTimeoutMs(), UPSTREAM_TIMEOUT_MS); assert.equal(knobs.replayMaxAttempts(), REPLAY_MAX_ATTEMPTS); assert.equal(knobs.persistTailTokens(), 16384);