diff --git a/.github/workflows/ci-e2e-dsh.yml b/.github/workflows/ci-e2e-dsh.yml new file mode 100644 index 0000000000..5c404471fb --- /dev/null +++ b/.github/workflows/ci-e2e-dsh.yml @@ -0,0 +1,65 @@ +name: e2e-dsh-plugin-lane + +# #2170 measure 3: the dsh plugin lane (anthropic wire, x-bili-plugin:dsh +# headers, fork/title-gen side traffic) is the surface where the #2156/#2157/ +# #2164 regression chain lived. This lane runs every dsh/fork/side-shaped +# suite in one named place so a lane regression is visible on the PR as its +# own red check, not buried in the general test matrix. Zero tokens: all +# upstreams are in-process mocks. +on: + pull_request: + paths: + - "src/server.ts" + - "src/server/**" + - "src/session.ts" + - "src/plugin.ts" + - "tests/*fork*.test.ts" + - "tests/*side*.test.ts" + - "tests/*dsh*.test.ts" + - ".github/workflows/ci-e2e-dsh.yml" + workflow_dispatch: + +concurrency: + group: e2e-dsh-plugin-lane-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + e2e-dsh-plugin-lane: + name: dsh plugin lane (fork adoption + side-request isolation + lane truth tables) + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: npm + + - name: Install deps + run: npm ci + + # tsx resolves `acp-kernel` to its built dist (kernel/ is a workspace + # package) — same prerequisite as the main test matrix. + - name: Build kernel + run: npm --prefix kernel run build + + - name: Run dsh plugin lane suites (sequential — concurrent runs flake on shared XDG dirs) + run: | + set -euo pipefail + for f in \ + side-lane-truth-table \ + side-lane-invariants \ + fork-side-request-lane \ + side-request-isolation \ + fix-1075-side-request \ + dsh-persona-fingerprint \ + plugin-fork \ + fork-adoption \ + fork-ccr-adoption \ + fork-nudge-trigger \ + truncation-fork \ + preflight-fork-estimate; do + echo "=== tests/$f.test.ts ===" + node --import tsx --test "tests/$f.test.ts" + done diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0be531fb25..13f717d25b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -146,3 +146,40 @@ jobs: with: node-version: 22 - run: node tools/gen-config-docs.mjs check-coverage + + # #2170 lane guard: the side-lane decision (#388 passthrough — src/server/ + # side-request.ts + the lane region of src/server.ts) is regression-critical + # (the #2156/#2157/#2164 chain). Changing it without touching its dedicated + # suites has silently shipped regressions before; this job makes the pairing + # mechanical: lane logic moves ⇔ a side-lane suite moves with it. + lane-guard: + runs-on: ubuntu-latest + if: github.event_name == 'pull_request' + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Side-lane test discipline (#2170) + run: | + set -euo pipefail + LANE_SUITE_RE='tests/(fork-side-request-lane|side-lane-truth-table|side-lane-invariants|side-request-isolation)\.test\.ts' + # Lane logic = src/server/side-request.ts, or lane-symbol lines in src/server.ts. + LANE_DIFF="" + if git diff origin/master...HEAD -- src/server/side-request.ts | grep -qE '^[+-]'; then + LANE_DIFF="src/server/side-request.ts" + fi + if git diff origin/master...HEAD -- src/server.ts | grep -Eq '^[+-].*(demotedSide|sideIntent|sideRequestLike|demoteGate|resolveSideLane|SIDE_REQUEST_MAX_TOKENS|isSideRequest|stripLeakedBiliTools|side request|side passthrough|sidePassthrough|side-lane)'; then + LANE_DIFF="${LANE_DIFF:+$LANE_DIFF, }src/server.ts (lane symbols)" + fi + if [[ -z "$LANE_DIFF" ]]; then + echo "✓ no side-lane logic changes" + exit 0 + fi + echo "side-lane logic changed: $LANE_DIFF" + if git diff --name-only origin/master...HEAD | grep -Eq "$LANE_SUITE_RE"; then + echo "✓ side-lane suites updated alongside lane logic" + exit 0 + fi + echo "::error::Side-lane logic changed without updating any dedicated side-lane suite ($LANE_SUITE_RE)." + echo "The lane gates are truth-table-pinned (tests/side-lane-truth-table.test.ts) and behavior-pinned (tests/side-lane-invariants.test.ts, fork-side-request-lane, side-request-isolation) — a gate edit must land with the suite edit. See #2170." + exit 1 diff --git a/src/server.ts b/src/server.ts index c5dc9d335a..798d2d147f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -50,7 +50,7 @@ import { } from "acp-kernel/wire"; import { responsesToCoreWithToolImages as responsesToCore, patchResponsesInputWithToolImages as patchResponsesInput, mergeAdjacentConfigurationUpdates } from "./responses-tool-output.js"; import { reconcileFoldCoverage, noteSystemPromptFingerprint, resolveFoldReconcileMode } from "./fold-reconcile.js"; -import { diagnoseSuccessWithoutUsage, getSession, hasProcessedState, listSessions, peekSession, type PendingRetrieval, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, totalInFlight, withSessionLock, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, snapshotMessages, applyCompactionArchive, detectUnannouncedHistoryRewrite, markCompactionBoundary, storeEffectiveConfig, foldCoverage, REWRITE_MIN_INCOMING_TOTAL } from "./session.js"; +import { diagnoseSuccessWithoutUsage, getSession, hasProcessedState, listSessions, peekSession, type PendingRetrieval, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, totalInFlight, withSessionLock, markNativeCompactionBoundary, reconcileNativeCompactionBoundary, snapshotMessages, applyCompactionArchive, detectUnannouncedHistoryRewrite, markCompactionBoundary, storeEffectiveConfig, foldCoverage, splitSessionWarnings, REWRITE_MIN_INCOMING_TOTAL } from "./session.js"; import { detectStaleInstall } from "./update.js"; import { getAdvisoryState, cannotResolveTarget } from "./advisory.js"; import { PACKAGE_NAME, VERSION } from "./version.js"; @@ -152,7 +152,7 @@ import { buildForwardHeaders, connectionNamedHeaders, NO_IDENTITY_MESSAGE, RESPO import { installWebSocketBridge } from "./ws-bridge.js"; import { codexResponsesCodec, responsesCodec } from "./responses-ws.js"; import { currentFetchTransport } from "./fetch-transport.js"; -import { hasLeakedBiliToolsOnly, isSideRequest, outputBudgetField, restoreOutputBudget, SIDE_REQUEST_MAX_TOKENS, sideRequestGuard, stripLeakedBiliTools } from "./server/side-request.js"; +import { demoteGate, hasLeakedBiliToolsOnly, isSideRequest, outputBudgetField, resolveSideLane, restoreOutputBudget, SIDE_REQUEST_MAX_TOKENS, sideRequestGuard, stripLeakedBiliTools } from "./server/side-request.js"; import { dshCompactionRefusal, isDshCompactionCall } from "./server/dsh-compaction-guard.js"; import { clampOutgoingOutput, countSystemAndToolsTokens, emergencyNudge, estimateInputTokens, estimateWireOverhead, projectThinkingMass } from "./server/budget.js"; import { awaitDrain, bufferToStream, dumpStreamToFile, pipeThrough, readStreamToBuffer } from "./server/stream-io.js"; @@ -1270,6 +1270,8 @@ function adminTrustedHostnames(bindHost: string): Set { // #924: one-time-per-model log for the output-budget fallback (request carries // no budget → configured/registry max output) — same pattern as windowSourceLogged. const headroomFallbackLogged = new Set(); +// #2170 split-session canary: one log line per conversation base per process. +const splitWarnedBases = new Set(); // #2096: dedupe the post-reservation effective-window line per model|value — // the reserved window varies per request (max_tokens), so key on both. @@ -2297,6 +2299,20 @@ async function handle( ? (anthropicIdentity?.value ?? anthropicSignal) : undefined; const personaForked = rawPersonaIdentity !== undefined && conversation !== rawPersonaIdentity; + // #2170 measure 4: stamp sessions deliberately namespaced onto a + // `|sub:` key — #970 claude subagents, #1916/#1307/#1314 dsh persona- + // fork reviews, codex/claude-over-Responses instructions personas — + // so the split-session canary can tell a DESIGNED split from the + // #2165 drift shape (see splitSessionWarnings in src/session.ts). + // personaForked covers the anthropic/openai wires; the responses wire + // needs its own check (codexTurn keys verbatim and must not count). + const designNamespaced = personaForked + || (protocol === "responses" + && codexTurn === undefined + && instructionsFingerprintApplies(req.headers) + && !sideRequestLike + && responsesIdentity !== undefined + && conversation !== responsesIdentity.value); // The session ID is the client-provided conversation value VERBATIM — // no hash, no protocol/credential/upstream dimensions (#286): those // are all mutable mid-conversation (bearer rotation, relay switching, @@ -2433,6 +2449,7 @@ async function handle( ? bodyIdentity.value : clientConversationHeader(req.headers); const session = getSession(sessionId, { protocol, upstreamOrigin, label: clientLabel ?? (anonAffinity ? "prefix-affinity" : undefined) }); + if (designNamespaced && session.metadata.personaNamespace !== true) session.metadata.personaNamespace = true; let publicForkPrefix = false; if (!countTokens && !responsesCompact && session.metadata.publicForkReceipt !== undefined) { acquireInFlight(session); @@ -2789,9 +2806,12 @@ async function handle( // are identified by the #1699 persona header instead). const wsLaneEnvelope = req.headers["x-bili-ws-lane"] !== undefined; const requestAgent = pluginRequestAgentHeader(req.headers); + // #2170 measure 1: the gate conjuncts live in side-request.ts as the + // pure, truth-table-tested demoteGate(); the two request-body effects + // stay here and stay lazy — detectAcpArtifacts only runs when the cheap + // gate holds, the strip only when the artifact scan came back clean. // Explicit main intent and a verified public-fork prefix each veto heuristic demotion. - const demotedSide = !countTokens && !responsesCompact && protocol !== null && pluginMode - && requestAgent !== "main" && !wsLaneEnvelope && !publicForkPrefix + const demotedSide = demoteGate({ countTokens, responsesCompact, protocol, pluginMode, requestAgent, wsLaneEnvelope, publicForkPrefix }) && detectAcpArtifacts(bodyBuffer, parsed) === null && stripLeakedBiliTools(parsed); // #546: restore a client-shrunk output budget BEFORE the side gate so a @@ -2867,7 +2887,10 @@ async function handle( // header) can false-positive it — only intent-certain side // identification (declared side agent, or a tool-less tiny budget) // diverts under a receipt. - if (!countTokens && !responsesCompact && protocol !== null && (demotedSide || sideIntent)) { + // #2170 measure 1: the decision itself is resolveSideLane() (pure, + // truth-table-tested); demotedSide ⊆ lane==="side" by construction. + const sideLane = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied: demotedSide, sideIntent, requestAgent }); + if (sideLane.lane === "side") { // #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, @@ -2898,7 +2921,7 @@ async function handle( logRequestCost(log, session.id, inboundMsgs, inboundBytes, reqT0); return; } - const sideReason = demotedSide ? "leaked bili tools stripped (#1897)" : requestAgent !== undefined ? `agent=${requestAgent}` : `max_tokens<=${SIDE_REQUEST_MAX_TOKENS}`; + const sideReason = sideLane.reason; log("info", `[${session.id}] side request (${sideReason}) → passthrough + tag strip only, kernel state untouched`); // #1897: a demoted request was mutated (tools stripped) — re-serialize // the parsed body so the leak is actually gone from the wire. @@ -2926,6 +2949,20 @@ async function handle( await forward(req, res, opts, sideBody, sidePrepared, core, reqConfig, log, route, instanceId, affinity); return; } + // #2170 measure 4 (runtime canary): every legitimately side-shaped + // LANE-ELIGIBLE request returned inside the lane above. count_tokens, + // /responses/compact and session-less (protocol-less) requests are + // deliberately NOT lane-eligible — they route to their own handling + // below, so they are excluded here (ework review finding B). If a + // lane-eligible side-intent or demoted request reaches the full + // pipeline anyway, the lane contract is broken (the #2157/#2164 + // regression class: side traffic touching kernel state). Count it on + // the session and say it loudly — with the pure resolveSideLane() + // this is unreachable by construction; any future drift trips it. + if (!countTokens && !responsesCompact && protocol !== null && (sideIntent || demotedSide)) { + session.metadata.sideEffectLeaks = (typeof session.metadata.sideEffectLeaks === "number" ? session.metadata.sideEffectLeaks : 0) + 1; + log("warn", `[${session.id}] SIDE-EFFECT LEAK (#2170 canary): ${sideLane.reason} request entered the full pipeline — expected the #388 side passthrough; kernel state pollution likely (cf. #2156/#2164)`); + } // #987: the window is NEVER learned from traffic — no self-heal read // here. Only the one-shot emergency shrink (armed on the overflow // itself) reacts to a wrong declared window. @@ -7627,7 +7664,14 @@ async function sendStatus(res: http.ServerResponse, opts: ProxyOptions): Promise // fs hiccup: report running state only, never fail the status endpoint } res.writeHead(200, { "content-type": "application/json" }); - res.end(JSON.stringify({ version: VERSION, diskVersion, stale, autoRestartOnUpdate: opts.autoRestartOnUpdate, advisory: currentAdvisoryPayload(), inFlight: totalInFlight(), conflicts: summarizeConflicts(listSessions()) }, null, 2)); + const splitWarnings = splitSessionWarnings(listSessions()); + for (const w of splitWarnings) { + if (!splitWarnedBases.has(w.base)) { + splitWarnedBases.add(w.base); + loggerLog("warn", `split-session canary (#2170): conversation ${w.base} has live traffic under multiple session keys (design persona forks are excluded): ${w.sessions.map((s) => `${s.id} (requests=${s.requests})`).join("; ")}. For a non-persona host this is the #2165 failure shape (stolen anchor / never-compressing split) — investigate if unexpected.`); + } + } + res.end(JSON.stringify({ version: VERSION, diskVersion, stale, autoRestartOnUpdate: opts.autoRestartOnUpdate, advisory: currentAdvisoryPayload(), inFlight: totalInFlight(), splitSessions: splitWarnings, conflicts: summarizeConflicts(listSessions()) }, null, 2)); } async function sendOverview(res: http.ServerResponse, opts: ProxyOptions): Promise { diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 9f6333b605..9abc2c9d0c 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -33,6 +33,59 @@ export function isSideRequest(parsed: unknown, requestAgent?: string): boolean { return typeof raw === "number" && raw > 0 && raw <= SIDE_REQUEST_MAX_TOKENS; } +// #2170 measure 1: the two lane gates extracted from src/server.ts so they have +// ONE home and an exhaustive truth-table test. They are PURE — the two effects +// that guard demotion (detectAcpArtifacts' full-history re-encode and the +// stripLeakedBiliTools mutation) stay in the caller and MUST stay lazily +// evaluated after demoteGate (never hoisted — see the sideRequestLike note in +// src/server.ts: the artifact scan may only run for the all-bili subset). +export interface DemoteGateSignals { + countTokens: boolean; + responsesCompact: boolean; + protocol: WireProtocol | null; + pluginMode: boolean; + requestAgent: string | undefined; + wsLaneEnvelope: boolean; + publicForkPrefix: boolean; +} +// Heuristic demotion eligibility — every conjunct is cheap (no request-body +// scan). `publicForkPrefix` deliberately vetoes: a public-fork child's EARLY +// mainline turns (raw inherited prefix, no artifacts yet, no agent header) +// false-positive the leak-shape heuristic; once anchored, the fork's side +// requests enter the lane via `sideIntent` instead (which resolves without +// any fork input — the #2164 fix). +export function demoteGate(s: DemoteGateSignals): boolean { + return !s.countTokens && !s.responsesCompact && s.protocol !== null && s.pluginMode + && s.requestAgent !== "main" && !s.wsLaneEnvelope && !s.publicForkPrefix; +} + +export interface SideLaneSignals { + countTokens: boolean; + responsesCompact: boolean; + protocol: WireProtocol | null; + // stripLeakedBiliTools() ran and stripped (only ever true when demoteGate + // held and detectAcpArtifacts returned null — the caller's short-circuit). + stripApplied: boolean; + sideIntent: boolean; + requestAgent: string | undefined; +} +export type SideLaneDecision = { lane: "side" | "main"; demoted: boolean; reason: string }; +// The #388 lane decision. `demoted` ⊆ `lane === "side"`. No fork input can +// reach here: intent-certain side requests ride the lane under a fork receipt +// (#2164) — that property is structural (there is no fork field to consult). +export function resolveSideLane(s: SideLaneSignals): SideLaneDecision { + if (s.countTokens || s.responsesCompact || s.protocol === null) { + return { lane: "main", demoted: false, reason: "not a model turn" }; + } + if (s.stripApplied) { + return { lane: "side", demoted: true, reason: "leaked bili tools stripped (#1897)" }; + } + if (s.sideIntent) { + return { lane: "side", demoted: false, reason: s.requestAgent !== undefined ? `agent=${s.requestAgent}` : `max_tokens<=${SIDE_REQUEST_MAX_TOKENS}` }; + } + return { lane: "main", demoted: false, reason: "main turn" }; +} + // #1897: hosts like omp register bili's ACP tools as first-class extension tools // and include them in EVERY model request — including side requests (title-gen), // which carry no host action tools of their own. The title request defeats both diff --git a/src/session.ts b/src/session.ts index 654f6d2f3a..ef08a3c3b0 100644 --- a/src/session.ts +++ b/src/session.ts @@ -644,6 +644,48 @@ export function listSessions(): Session[] { return [...sessions.values()].sort((a, b) => b.lastSeen - a.lastSeen); } +// #2170 measure 4 (runtime canary): the #2165 failure shape — one dsh +// conversation's traffic split across BOTH the raw id and a `|sub:` fork, +// both live at once (the raw session steals the compressions / anchor while +// the fork carries the real turns). Surface any such split on /__bili/status +// so an operator sees it without digging through logs. +// +// Design splits are excluded: sessions namespaced onto `|sub:` ON PURPOSE — +// #970 claude subagent conversations and #1916/#1307/#1314 dsh persona-fork +// reviews — carry session.metadata.personaNamespace = true (stamped at +// request time in server.ts) and never count toward a warning; without this +// every active Auto-tier dsh conversation (review fork beside main turns) +// would cry wolf. What remains actionable: ≥2 fresh sessions on one base +// where ≥1 carried traffic (requests>0) and ≥1 is an UNMARKED `|sub:` child. +// The #2165 report itself was "empty raw twin + live fork", so a traffic-less +// raw twin does NOT disqualify a warning — only the stale (>freshness window) +// and the fully idle (nothing ever carried traffic) groups stay silent. +export interface SplitSessionWarning { + base: string; + sessions: { id: string; requests: number; lastSeen: number }[]; +} +export const SPLIT_CANARY_FRESH_MS = 10 * 60 * 1000; +export function splitSessionWarnings(sessions: Session[], now: number = Date.now()): SplitSessionWarning[] { + const byBase = new Map(); + for (const s of sessions) { + if (s.metadata?.personaNamespace === true) continue; // designed split (#970/#1916) + const base = s.id.split("|sub:")[0]; + const arr = byBase.get(base); + if (arr) arr.push(s); else byBase.set(base, [s]); + } + const out: SplitSessionWarning[] = []; + for (const [base, group] of byBase) { + if (group.length < 2) continue; + const fresh = group.filter((s) => now - (s.lastSeen ?? 0) < SPLIT_CANARY_FRESH_MS); + const anyTraffic = fresh.some((s) => (s.stats?.requests ?? 0) > 0); + const anyChild = fresh.some((s) => s.id.includes("|sub:")); + if (fresh.length >= 2 && anyTraffic && anyChild) { + out.push({ base, sessions: fresh.map((s) => ({ id: s.id, requests: s.stats?.requests ?? 0, lastSeen: s.lastSeen ?? 0 })) }); + } + } + return out; +} + /** Read-only in-memory lookup. Unlike getSession, never creates or reloads a * session — used by the plugin tool API, which must not conjure state for a * conversation it has never seen. */ diff --git a/tests/advisory-endpoint-parity.test.ts b/tests/advisory-endpoint-parity.test.ts index ce78fc6349..0525251b4f 100644 --- a/tests/advisory-endpoint-parity.test.ts +++ b/tests/advisory-endpoint-parity.test.ts @@ -9,12 +9,16 @@ import test from "node:test"; import assert from "node:assert/strict"; import http from "node:http"; import { once } from "node:events"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path 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 { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { _resetAdvisoryWatcherForTest, _setAdvisoryStateForTest } from "../src/advisory.ts"; +import { rmrf } from "./tmp-rm.ts"; process.env.BILI_PERSIST_ZSTD = "0"; @@ -51,6 +55,12 @@ async function fetchWire(port: number, path: string): Promise { } test("#2152: /__bili/overview carries the same advisory field as /__bili/status (banner data source)", async () => { + // Hermetic sessions dir: /__bili/overview walks the disk store, and a missing + // dir rejects loudly by design (#1937) — on a fresh CI runner the ambient + // default has never existed, so pin an existing temp dir (#2152 went red). + const prevSessionsDir = process.env.BILI_SESSIONS_DIR; + const sessionsDir = mkdtempSync(path.join(tmpdir(), "bili-advisory-parity-")); + process.env.BILI_SESSIONS_DIR = sessionsDir; _setStoreForTest(new SessionStore({ enabled: false })); setRegistryForTest({}); const upstreamPort = await freePort(); // never contacted — management endpoints only @@ -119,6 +129,8 @@ test("#2152: /__bili/overview carries the same advisory field as /__bili/status assert.equal(o.advisory!.id, "bc-2026-002"); assert.equal(o.advisory!.targetFailed, true); } finally { + if (prevSessionsDir === undefined) delete process.env.BILI_SESSIONS_DIR; else process.env.BILI_SESSIONS_DIR = prevSessionsDir; + rmrf(sessionsDir); _setAdvisoryStateForTest({}); _resetAdvisoryWatcherForTest(); await close(proxy); diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts new file mode 100644 index 0000000000..83de60cef6 --- /dev/null +++ b/tests/side-lane-invariants.test.ts @@ -0,0 +1,212 @@ +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, listSessions, SPLIT_CANARY_FRESH_MS, splitSessionWarnings, type Session } from "../src/session.ts"; +import { _setForTest } from "../src/registry.ts"; +import { resetToolRingForTest } from "../src/tool-ring.ts"; + +// #2170 measure 2: behavioral invariants of the #388 side lane, on the dsh +// plugin wire (the host whose regression started this — #2156/#2157/#2164). +// (A) transparency: side requests are OBSERVATION-transparent — upstream hit, +// kernel state (requests / parentRevision / usage baseline / snapshot +// contents) bit-for-bit unchanged. +// (B) ordering-independence: interleavings of main and side traffic converge +// to the same kernel state as the main-only baseline (#2164 class: the +// bug only manifested when a side request arrived AFTER a fork anchor). +// (C) split-session canary unit: the #2165 detection helper fires only on a +// genuinely live split. +process.env.NODE_ENV = "test"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-side-inv-")); +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; + +async function harness() { + 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: false, debounceMs: 60000 }); + _setStoreForTest(store); + _setForTest({}); + const forwarded: Record[] = []; + const upstream = await new Promise<{ url: string; close: () => Promise }>((resolve) => { + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + 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(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 } })); + }); + }); + server.listen(0, "127.0.0.1", () => resolve({ url: `http://127.0.0.1:${(server.address() as { port: number }).port}`, close: async () => { server.close(); } })); + }); + const proxy = await startServer({ port: 0, host: "127.0.0.1", upstream: upstream.url, routes: { [upstream.url]: { 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 origin = `http://127.0.0.1:${(proxy.address() as { port: number }).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() }; + }; + // dsh-shaped request sender (anthropic wire: system TOP-LEVEL, messages replay raw) + const dshSend = async (conversationId: string, msgs: unknown[], agent: string | undefined, system: string, maxTokens = 1024) => { + const headers: Record = { "content-type": "application/json", "x-acp-session": conversationId, "x-bili-plugin": "dsh", "x-bili-plugin-conversation": conversationId }; + if (agent !== undefined) headers["x-bili-plugin-agent"] = agent; + const response = await fetch(`${origin}/bili/${upstream.url}/v1/messages`, { method: "POST", headers, body: JSON.stringify({ model: "claude-test", max_tokens: maxTokens, stream: false, system, messages: msgs }) }); + assert.equal(response.status, 200, await response.text()); + }; + return { request, forwarded, dshSend, origin, upstreamUrl: upstream.url, close: async () => { store.cancelAll(); proxy.close(); upstream.close(); await new Promise((r2) => setTimeout(r2, 50)); } }; +} + +const mainMsg = (n: number) => ({ role: "user", content: `main turn ${n} `.repeat(120) }); + +test("(A) side requests are observation-transparent: kernel state and upstream traffic", async () => { + const h = await harness(); + try { + const conv = "plain"; + await h.dshSend(conv, [mainMsg(0)], "main", "MAIN OPERATING SYSTEM", 256); + const session = resolveConversation(conv).session!; + const requests0 = session.stats.requests; + const rev0 = ((await h.request("/__bili/plugin/snapshot?conversationId=" + conv)).body as { parentRevision: string }).parentRevision; + const status0 = (await h.request("/__bili/plugin/status?conversationId=" + conv)).body as { contextTokens: number | null; inputTokens: number }; + + // side shape 1: declared side agent, full-history replay (dsh title-gen) + const hits0 = h.forwarded.length; + await h.dshSend(conv, [mainMsg(0), { role: "user", content: "Generate a title." }], "title", "You generate short titles.", 1024); + assert.equal(h.forwarded.length, hits0 + 1, "title request reaches the upstream"); + // side shape 2: budget heuristic, NO agent header (max_tokens <= 200) + await h.dshSend(conv, [mainMsg(0), { role: "user", content: "summarize" }], undefined, "MAIN OPERATING SYSTEM", 64); + assert.equal(h.forwarded.length, hits0 + 2, "tiny-budget side request reaches the upstream"); + // side shape 3: title again after the second main turn (post-anchor ordering) + await h.dshSend(conv, [mainMsg(0), { role: "assistant", content: "a" }, mainMsg(1), { role: "user", content: "Generate a title." }], "title", "You generate short titles.", 1024); + assert.equal(h.forwarded.length, hits0 + 3, "second title request reaches the upstream"); + + const requests1 = session.stats.requests; + const rev1 = ((await h.request("/__bili/plugin/snapshot?conversationId=" + conv)).body as { parentRevision: string }).parentRevision; + const status1 = (await h.request("/__bili/plugin/status?conversationId=" + conv)).body as { contextTokens: number | null; inputTokens: number }; + assert.equal(requests1, requests0, `side requests must not count as main requests (${requests0} -> ${requests1})`); + assert.equal(rev1, rev0, `side requests must not grow the snapshot (${rev0} -> ${rev1})`); + assert.equal(status1.inputTokens, status0.inputTokens, "side requests must not touch the usage baseline"); + assert.equal(status1.contextTokens, status0.contextTokens, "side requests must not touch the context estimate"); + assert.ok(!JSON.stringify(session.pluginSnapshot).includes("Generate a title."), "title instruction never lands in the kernel session"); + const titleForward = h.forwarded.at(-1)! as { messages?: { content: unknown }[] }; + assert.ok(titleForward.messages?.some((m) => JSON.stringify(m.content).includes("Generate a title.")), "the forwarded title request carries the title instruction"); + // the side-effect canary (#2170 measure 4) stays silent: no leak counted + assert.equal(session.metadata.sideEffectLeaks, undefined, "no side-effect leak may be counted on a healthy lane"); + } finally { + await h.close(); + } +}); + +test("(B) main/side interleavings converge to the main-only baseline (ordering independence)", async () => { + // deterministic LCG so a failure is reproducible from the seed in the log + const runScenario = async (seed: number, includeSide: boolean) => { + const h = await harness(); + try { + let s = seed; + const rnd = () => ((s = (s * 1103515245 + 12345) & 0x7fffffff) / 0x7fffffff); + const conv = "fuzz"; + // main turn stream = fixed; side pool = 3 requests; rnd only picks + // WHEN each side request fires (between main turns), not its shape. + const sidePool = [ + () => h.dshSend(conv, [mainMsg(0), { role: "user", content: "Generate a title." }], "title", "You generate short titles.", 1024), + () => h.dshSend(conv, [mainMsg(0), { role: "user", content: "summarize" }], undefined, "MAIN OPERATING SYSTEM", 64), + () => h.dshSend(conv, [mainMsg(0), mainMsg(1), { role: "user", content: "Generate a title." }], "title", "You generate short titles.", 1024), + ]; + let sideCursor = 0; + const mainTurn = async (n: number) => { + await h.dshSend(conv, [mainMsg(n)], "main", "MAIN OPERATING SYSTEM", 256); + if (includeSide && sideCursor < sidePool.length && rnd() < 0.45) { + await sidePool[sideCursor++](); + } + }; + await mainTurn(0); + await mainTurn(1); + // drain any remaining side requests so every scenario forwards 4 main + 3 side + if (includeSide) while (sideCursor < sidePool.length) await sidePool[sideCursor++](); + await mainTurn(2); + await mainTurn(3); + const session = resolveConversation(conv).session!; + const rev = ((await h.request("/__bili/plugin/snapshot?conversationId=" + conv)).body as { parentRevision: string }).parentRevision; + const status = (await h.request("/__bili/plugin/status?conversationId=" + conv)).body as { contextTokens: number | null; inputTokens: number }; + return { requests: session.stats.requests, rev, inputTokens: status.inputTokens, contextTokens: status.contextTokens, forwards: h.forwarded.length, expectedForwards: includeSide ? 7 : 4 }; + } finally { + await h.close(); + } + }; + const baseline = await runScenario(1, false); + assert.deepEqual({ requests: baseline.requests, forwards: baseline.forwards, expectedForwards: baseline.expectedForwards }, { requests: 4, forwards: 4, expectedForwards: 4 }, "baseline sanity: 4 main turns, 4 forwards"); + for (const seed of [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]) { + const r = await runScenario(seed, true); + assert.equal(r.forwards, 7, `seed ${seed}: every side request still reaches the upstream`); + assert.equal(r.requests, baseline.requests, `seed ${seed}: side traffic must not change the request count (${baseline.requests} != ${r.requests})`); + assert.equal(r.rev, baseline.rev, `seed ${seed}: side traffic must not change the snapshot revision`); + assert.equal(r.inputTokens, baseline.inputTokens, `seed ${seed}: side traffic must not change the usage baseline`); + assert.equal(r.contextTokens, baseline.contextTokens, `seed ${seed}: side traffic must not change the context estimate`); + } +}); + +test("(C) splitSessionWarnings: design persona forks excluded, drift shapes fire (#2165 shape)", () => { + const now = 1_000_000; + const mk = (id: string, requests: number, ageMs: number, persona = false): Session => ({ id, lastSeen: now - ageMs, createdAt: now - ageMs - 1000, stats: { requests }, metadata: persona ? { personaNamespace: true } : {} } as unknown as Session); + const mkSub = (base: string, sub: string, requests: number, ageMs: number, persona = false) => mk(`${base}|sub:${sub}`, requests, ageMs, persona); + // drift split: raw id + unmarked fork, both carried traffic, both fresh → warns + const drift = splitSessionWarnings([mk("A", 3, 60_000), mkSub("A", "fp1", 5, 60_000)], now); + assert.equal(drift.length, 1); + assert.equal(drift[0].base, "A"); + assert.equal(drift[0].sessions.length, 2); + // #2165's exact reported shape: traffic-less raw twin + LIVE fork → warns + // (the raw twin held the anchor/compressions while the fork carried turns) + assert.equal(splitSessionWarnings([mk("A", 0, 60_000), mkSub("A", "fp1", 5, 60_000)], now).length, 1, "empty raw + live unmarked fork is the #2165 shape"); + // single session: never warns + assert.equal(splitSessionWarnings([mk("B", 3, 60_000)], now).length, 0); + // design persona fork (marked): NEVER warns, whatever the traffic mix + assert.equal(splitSessionWarnings([mk("P", 3, 60_000), mkSub("P", "fp", 5, 60_000, true)], now).length, 0, "a persona-marked child is a designed split (#970/#1916)"); + assert.equal(splitSessionWarnings([mk("P", 0, 60_000), mkSub("P", "fp", 5, 60_000, true)], now).length, 0); + // split but stale (older than the freshness window) + assert.equal(splitSessionWarnings([mk("D", 3, SPLIT_CANARY_FRESH_MS + 1), mkSub("D", "fp1", 5, 60_000)], now).length, 0, "stale twins are history, not a live split"); + // fully idle group (nothing ever carried traffic) + assert.equal(splitSessionWarnings([mk("I", 0, 60_000), mkSub("I", "fp", 0, 60_000)], now).length, 0, "an idle pair never carried traffic — not a live split"); + // unrelated sessions never group together + assert.equal(splitSessionWarnings([mk("E", 3, 1_000), mkSub("F", "x", 3, 1_000)], now).length, 0); + // mixed: one marked + one UNMARKED child beside the raw → the unmarked pair still warns + const mixed = splitSessionWarnings([mk("M", 3, 60_000), mkSub("M", "design", 9, 60_000, true), mkSub("M", "drift", 1, 60_000)], now); + assert.equal(mixed.length, 1, "a marked sibling must not hide a drifting twin"); + assert.ok(!mixed[0].sessions.some((s) => s.id.includes("design")), "the marked child itself stays out of the warning"); +}); + +test("(D) a design dsh persona fork (auto-review) does NOT trip the split canary", async () => { + const h = await harness(); + try { + const conv = "persona"; + // main turn anchors the raw key + await h.dshSend(conv, [mainMsg(0)], "main", "MAIN OPERATING SYSTEM", 256); + // dsh auto-review classifyRisk shape: same conversation id, fixed + // REVIEW_POLICY system, full-budget model turn (NOT a side request — + // budget 300, no tools, no side agent) → keyed by design onto + // `|sub:` (#1916/#1307/#1314) and rides the main pipeline there. + await h.dshSend(conv, [mainMsg(0), { role: "user", content: "REVIEW: tool call risk assessment" }], undefined, "REVIEW_POLICY: classify the risk of the proposed tool call. Answer SAFE or UNSAFE.", 300); + const all = listSessions(); + const kids = all.filter((s) => s.id.startsWith(conv + "|") || s.id.includes(conv + "|sub:")); + assert.equal(kids.length, 1, `expected exactly one persona-fork child, got ${all.map((s) => s.id).join(", ")}`); + assert.ok((kids[0].stats?.requests ?? 0) >= 1, "the review turn rode the child's main pipeline"); + assert.equal(kids[0].metadata.personaNamespace, true, "the child is stamped as a designed split"); + assert.equal(splitSessionWarnings(all).length, 0, "healthy persona traffic must not cry wolf"); + const status = (await h.request("/__bili/status")).body as { splitSessions?: unknown[] }; + assert.deepEqual(status.splitSessions, [], "/__bili/status stays clean for design persona forks"); + } finally { + await h.close(); + } +}); diff --git a/tests/side-lane-truth-table.test.ts b/tests/side-lane-truth-table.test.ts new file mode 100644 index 0000000000..83874c722b --- /dev/null +++ b/tests/side-lane-truth-table.test.ts @@ -0,0 +1,109 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { demoteGate, isSideRequest, resolveSideLane, SIDE_REQUEST_MAX_TOKENS } from "../src/server/side-request.ts"; + +// #2170 measure 1: exhaustive truth-table pin for the two extracted lane +// decision functions. These tables ARE the regression net: any future edit to +// a conjunct, a veto, or a reason string changes a cell here and forces the +// author to also touch tests/ — which is exactly what the lane-guard CI job +// (#2170 measure 5) demands. The reference formulas below are the pre-#2170 +// inline expressions from src/server.ts, copied verbatim. + +const AGENT_STATES: (string | undefined)[] = [undefined, "main", "title"]; + +test("demoteGate: exhaustive over every signal combination vs the reference conjunction", () => { + for (const countTokens of [false, true]) { + for (const responsesCompact of [false, true]) { + for (const protocol of [null, "anthropic", "responses"] as const) { + for (const pluginMode of [false, true]) { + for (const requestAgent of AGENT_STATES) { + for (const wsLaneEnvelope of [false, true]) { + for (const publicForkPrefix of [false, true]) { + const got = demoteGate({ countTokens, responsesCompact, protocol, pluginMode, requestAgent, wsLaneEnvelope, publicForkPrefix }); + // reference: the original inline expression (pre-#2170 server.ts) + const want = !countTokens && !responsesCompact && protocol !== null && pluginMode + && requestAgent !== "main" && !wsLaneEnvelope && !publicForkPrefix; + assert.equal(got, want, JSON.stringify({ countTokens, responsesCompact, protocol, pluginMode, requestAgent, wsLaneEnvelope, publicForkPrefix })); + } + } + } + } + } + } + } +}); + +test("demoteGate: the #2164 fork veto is structural — publicForkPrefix=true always vetoes, for every other signal combination", () => { + let combos = 0; + for (const countTokens of [false, true]) { + for (const responsesCompact of [false, true]) { + for (const protocol of [null, "anthropic"] as const) { + for (const pluginMode of [false, true]) { + for (const requestAgent of AGENT_STATES) { + for (const wsLaneEnvelope of [false, true]) { + combos++; + assert.equal(demoteGate({ countTokens, responsesCompact, protocol, pluginMode, requestAgent, wsLaneEnvelope, publicForkPrefix: true }), false, "a verified public-fork prefix must veto heuristic demotion in every configuration"); + } + } + } + } + } + } + assert.ok(combos >= 24, `exercised ${combos} combinations`); +}); + +test("resolveSideLane: exhaustive over every signal combination vs the reference expression", () => { + for (const countTokens of [false, true]) { + for (const responsesCompact of [false, true]) { + for (const protocol of [null, "anthropic"] as const) { + for (const stripApplied of [false, true]) { + for (const sideIntent of [false, true]) { + for (const requestAgent of AGENT_STATES) { + const d = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied, sideIntent, requestAgent }); + // reference: the original inline lane condition (pre-#2170 server.ts) + const side = !countTokens && !responsesCompact && protocol !== null && (stripApplied || sideIntent); + assert.equal(d.lane, side ? "side" : "main", JSON.stringify({ countTokens, responsesCompact, protocol, stripApplied, sideIntent, requestAgent })); + assert.equal(d.demoted, side && stripApplied, `demoted ⊆ side, and equals the strip: ${JSON.stringify(d)}`); + if (d.lane === "main") { + const expected = countTokens || responsesCompact || protocol === null ? "not a model turn" : "main turn"; + assert.equal(d.reason, expected, `main-lane reason: ${JSON.stringify(d)}`); + } + } + } + } + } + } + } +}); + +test("resolveSideLane: reason strings for the three side shapes", () => { + assert.equal(resolveSideLane({ countTokens: false, responsesCompact: false, protocol: "anthropic", stripApplied: true, sideIntent: false, requestAgent: undefined }).reason, "leaked bili tools stripped (#1897)"); + assert.equal(resolveSideLane({ countTokens: false, responsesCompact: false, protocol: "anthropic", stripApplied: false, sideIntent: true, requestAgent: "title" }).reason, "agent=title"); + assert.equal(resolveSideLane({ countTokens: false, responsesCompact: false, protocol: "anthropic", stripApplied: false, sideIntent: true, requestAgent: undefined }).reason, `max_tokens<=${SIDE_REQUEST_MAX_TOKENS}`); +}); + +test("resolveSideLane: no fork input exists — intent-certain side requests ride the lane under a fork receipt (#2164 fix pin)", () => { + // The #2157→#2164 regression was exactly this cell: a fork-child + // title-gen. resolveSideLane has no fork field to consult, so the cell + // cannot regress without removing the function's inputs (a visible, + // reviewable change). Pin the decision: + const d = resolveSideLane({ countTokens: false, responsesCompact: false, protocol: "anthropic", stripApplied: false, sideIntent: true, requestAgent: "title" }); + assert.equal(d.lane, "side"); + assert.equal(d.demoted, false); + // and the fork-vetoed demote shape stays OFF the strip path while still + // riding via intent (the two gates are complementary, not redundant): + assert.equal(demoteGate({ countTokens: false, responsesCompact: false, protocol: "anthropic", pluginMode: true, requestAgent: "title", wsLaneEnvelope: false, publicForkPrefix: true }), false); +}); + +test("SIDE_REQUEST_MAX_TOKENS stays 200 (budget-heuristic width is load-bearing)", () => { + assert.equal(SIDE_REQUEST_MAX_TOKENS, 200); +}); + +test("isSideRequest sanity anchors for the table above (agent wins, main never demotes, tools veto, budget path)", () => { + assert.equal(isSideRequest({}, "title"), true); + assert.equal(isSideRequest({ max_tokens: 64 }, "main"), false); + assert.equal(isSideRequest({ max_tokens: 64, tools: [{ name: "x" }] }, undefined), false); + assert.equal(isSideRequest({ max_tokens: 64 }, undefined), true); + assert.equal(isSideRequest({ max_tokens: 1024 }, undefined), false); + assert.equal(isSideRequest({ tools: [{ name: "bili_compact" }] }, undefined), false); +});