diff --git a/src/server/handle.ts b/src/server/handle.ts index 99f4b06e25..17a6e0b4d7 100644 --- a/src/server/handle.ts +++ b/src/server/handle.ts @@ -70,7 +70,7 @@ import { buildForgeResponse, classifierForgeVariant, estimateCjkTokens, isAutoMo import { extractConversationId, isV1BackgroundSideRequest, isV1InternalRequest, unwrapV1Internal } from "../v1internal.js"; import { anthropicBetaContextWindow, anthropicBetaMessageThreads, BILI_HOP_HEADER, capRegistryWindowByStandard, expandedContextSuffixWindow, launcherContextWindow, launcherMaxOutput, noteUnresolvedModelSession, UNRESOLVED_MODEL_SESSION_THRESHOLD, windowSourceLogged } from "./context-window.js"; import { NO_IDENTITY_MESSAGE, safeSessionId } from "./headers.js"; -import { demoteGate, hasLeakedBiliToolsOnly, hasSingleShotBudget, isAudioOutputCall, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isResponsesServerToolSingleShot, isServerToolUtilityCall, isSideRequest, isStructuredOutputCall, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; +import { demoteGate, hasLeakedBiliToolsOnly, hasSingleShotBudget, isAudioOutputCall, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshSingleShotSidecar, isDshTitleRequest, isMcodeOneShotAuxRequest, isMcodeTitleRequest, isOpencodeTitleRequest, isResponsesServerToolSingleShot, isServerToolUtilityCall, isSideRequest, isStructuredOutputCall, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js"; import { DSH_COMPACTION_SHAPE_MSGS, DSH_REFUSAL_STORM_LOG_EVERY, detectPassthroughDshCompactionCall, dshCompactionRefusal, dshPassthroughGuardStats, isDshCompactionCall } from "./dsh-compaction-guard.js"; import { buildDshCompactionForgeBody, renderDshAuthoredCheckpoint } from "./dsh-authored-compaction.js"; import { artifactSeedHit, detectAcpArtifacts } from "./chain-artifacts.js"; @@ -322,6 +322,17 @@ function memProbe(debug: boolean, log: (level: string, msg: string) => void, ses log("debug", `[${sessionId}] mem-probe ${stage}: heap=${mi(mu.heapUsed)} external=${mi(mu.external)} rss=${mi(mu.rss)}${payloadBytes !== undefined ? ` payload=${payloadBytes}B` : ""}`); } +// #6884: non-empty header presence, proto-agnostically over Node's header map +// (string values, string[] tolerated for folded multi-values; anything else +// counts as absent). Lane evidence for the wire-client sidecar clauses in +// handleRequest — same trust tier as the verbatim session-id roster +// (src/session-id.ts): the header is client-stamped, not bili-derived. +function hasNonEmptyHeader(h: Record, name: string): boolean { + const v = h[name]; + if (typeof v === "string") return v.length > 0; + if (Array.isArray(v)) return v.some((x) => typeof x === "string" && x.length > 0); + return false; +} async function handleRequest( req: http.IncomingMessage, res: http.ServerResponse, @@ -1267,6 +1278,58 @@ async function handleRequest( // small maxTokens and no tools must enter the kernel pipeline. const declaredPiMainTurn = rawDeclaredPurpose === PLUGIN_PURPOSE_CONVERSATION && pluginAgentHeader(req.headers) === "pi"; + // #6884 wire-client legs (the cross-agent session-identity audit; + // follow-on to #7077): every client on the verbatim roster + // (src/session-id.ts) mints ONE id per logical conversation and + // source review confirmed the lifecycles are healthy — but each fires + // auxiliary LLM calls (titles, compaction, suggestions, memory, + // naming) UNDER THE SAME id, and those shapes defeat isSideRequest, + // so they enter the main session's kernel pipeline as conversation + // turns. Each clause is gated on the client's OWN wire signal (its + // session header / its compaction header / its constant User-Agent) — + // the same trust tier as the roster itself; no clause identifies a + // client from body shape alone. Upstream evidence per client: + // - grok-shell: main turns ALWAYS stamp x-grok-turn-idx + // (turn.rs:3002-3003); every auxiliary path (/btw, compaction, + // title refresh, recap, memory, ...) stamps x-grok-session-id + // WITHOUT the turn index — grok's own test-support contract + // (README:32): "a non empty x-grok-turn-idx makes it foreground, + // else a non empty x-grok-req-id makes it auxiliary". One header + // clause covers every present AND future auxiliary path. + // - mcode: title-gen's entire tool surface is the single + // submit_session_title tool; the prompt suggestion (default on, + // every accepted answer) and the Goal evaluator are tool-less, + // budget-less single user messages reusing the main turn's + // headers (prompt-suggestion-service.ts:655-692). + // - goose: every auxiliary (naming, compaction, tool-pair summaries, + // labels, adversarial review, apps, summarize) is the dsh + // single-shot shape — no tools + exactly one user + a system + // carrier holding the context — so isDshSingleShotSidecar covers + // it as-is; the lane evidence is the roster header + // agent-session-id. Residual, accepted: the permission judge + // carries one tool (#546 main rung) and a user who configures + // zero tools loses the first turn only (#5687 class). + // - CodeBuddy: the compaction request itself carries x-compaction + // on the wire (identity-codebuddy.ts) — consume it. Deliberately + // NOT x-context-compacted: that marks "context already compacted" + // and appears on ordinary POST-compaction MAIN turns; consuming + // it would demote the real mainline. + // - opencode: the built-in title agent fires concurrently with the + // first main turn of every root session, under the root's own id, + // no tools, no budget, single user framed by a fixed template + // (session/title.ts:37-41); the User-Agent (app.ts:22 constant + // format) is the lane evidence and the template text the shape. + const grokAuxSidecar = hasNonEmptyHeader(req.headers, "x-grok-session-id") + && !hasNonEmptyHeader(req.headers, "x-grok-turn-idx"); + const mcodeLane = hasNonEmptyHeader(req.headers, "x-mavis-session-id"); + const mcodeTitleSidecar = mcodeLane && isMcodeTitleRequest(parsed); + const mcodeOneShotSidecar = mcodeLane && isMcodeOneShotAuxRequest(protocol, parsed); + const gooseSingleShotSidecar = hasNonEmptyHeader(req.headers, "agent-session-id") + && isDshSingleShotSidecar(protocol, parsed); + const codebuddyCompactionSidecar = hasNonEmptyHeader(req.headers, "x-compaction"); + const opencodeTitleSidecar = isOpencodeTitleRequest(protocol, parsed) + && (typeof req.headers["user-agent"] === "string" + && req.headers["user-agent"].startsWith("opencode/")); const sideRequestLike = !countTokens && !responsesCompact && protocol !== null && (isSideRequest(parsed, declaredPiMainTurn ? "main" : sideAgent, declaredPurpose) // #2503: dsh title sidecar — its output budget grew past the @@ -1297,6 +1360,14 @@ async function handleRequest( // #5135: the claude code title sidecar stays off the raw-key // anchor claim and off the kernel pipeline entirely. || ccTitleSidecar + // #6884: the wire-client auxiliary sidecars (clause evidence + // above) — same raw-key/anchor discipline as ccTitleSidecar: + // verbatim passthrough, kernel state untouched, no |sub: fork. + || grokAuxSidecar + || mcodeTitleSidecar || mcodeOneShotSidecar + || gooseSingleShotSidecar + || codebuddyCompactionSidecar + || opencodeTitleSidecar || (pluginAgentHeader(req.headers) !== undefined && sideAgent !== "main" && req.headers["x-bili-ws-lane"] === undefined && hasLeakedBiliToolsOnly(parsed) @@ -2689,7 +2760,8 @@ async function handleRequest( // #5135: ccTitleSidecar joins the intent set so the title call rides // the #388 passthrough (kernel state untouched) like every other // recognized side request. - const sideIntent = isSideRequest(parsed, declaredPiMainTurn ? "main" : requestAgent, declaredPurpose) || dshTitleSidecar || dshReviewSidecar || dshSingleShotSidecar || ccTitleSidecar; + const sideIntent = isSideRequest(parsed, declaredPiMainTurn ? "main" : requestAgent, declaredPurpose) || dshTitleSidecar || dshReviewSidecar || dshSingleShotSidecar || ccTitleSidecar + || grokAuxSidecar || mcodeTitleSidecar || mcodeOneShotSidecar || gooseSingleShotSidecar || codebuddyCompactionSidecar || opencodeTitleSidecar; // #388/#2157 follow-up: side requests must not touch kernel state under // a public-fork receipt either. The receipt's first-request check above // has already accepted this request (inherited prefix matched, or live @@ -2713,7 +2785,7 @@ async function handleRequest( // #2500 server-tool utility shape) diverts under a receipt. // #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, sideLabel: declaredPurpose !== undefined ? `declared purpose=${declaredPurpose} (#6880)` : serverToolUtility ? "server-tool utility call (#2500)" : structuredOutputUtility ? "structured-output utility call (#5073)" : audioOutputUtility ? "audio-output utility call (#6636)" : dshTitleSidecar ? "dsh title-gen sidecar (#2503)" : dshReviewSidecar ? "dsh auto-review sidecar (#4438)" : dshSingleShotSidecar ? "dsh single-shot sidecar (#5656)" : ccTitleSidecar ? "claude-code title-gen sidecar (#5135)" : undefined }); + const sideLane = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied: demotedSide, sideIntent, requestAgent, sideLabel: declaredPurpose !== undefined ? `declared purpose=${declaredPurpose} (#6880)` : serverToolUtility ? "server-tool utility call (#2500)" : structuredOutputUtility ? "structured-output utility call (#5073)" : audioOutputUtility ? "audio-output utility call (#6636)" : dshTitleSidecar ? "dsh title-gen sidecar (#2503)" : dshReviewSidecar ? "dsh auto-review sidecar (#4438)" : dshSingleShotSidecar ? "dsh single-shot sidecar (#5656)" : ccTitleSidecar ? "claude-code title-gen sidecar (#5135)" : grokAuxSidecar ? "grok auxiliary sidecar (#6884)" : mcodeTitleSidecar ? "mcode title sidecar (#6884)" : mcodeOneShotSidecar ? "mcode one-shot aux sidecar (#6884)" : gooseSingleShotSidecar ? "goose single-shot sidecar (#6884)" : codebuddyCompactionSidecar ? "codebuddy compaction sidecar (#6884)" : opencodeTitleSidecar ? "opencode title-gen sidecar (#6884)" : undefined }); 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 diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 7971a35cd4..81c5cc8886 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -487,6 +487,136 @@ export function isClaudeCodeTitleRequest(protocol: string | null, parsed: unknow return text.trimStart().startsWith(CLAUDE_CODE_TITLE_SESSION_TAG); } +// #6884 (wire-client legs of the cross-agent session-identity audit; follow-on +// to the pi purpose declaration in #7077): every client on the verbatim +// session-id roster (src/session-id.ts — grok-shell, mcode, goose, CodeBuddy, +// opencode) mints ONE id per logical conversation and per-client source review +// confirmed the lifecycle is healthy (forks/subagents mint fresh ids with a +// parent pointer). The exposure is elsewhere: each fires auxiliary LLM calls +// (titles, compaction, suggestions, memory, naming) UNDER THE SAME id, and +// their body shapes defeat isSideRequest (no tools plus a missing, small or +// large budget), so they would enter the main session's kernel pipeline as +// conversation turns. Each predicate below is a SHAPE only — the caller must +// gate it on the client's own wire signal (its session header / compaction +// header / constant User-Agent), the same trust tier as the roster itself. + +// mcode's title sidecar calls the model with EXACTLY ONE tool — +// `submit_session_title` (session-title-model-adapter.ts:17, pinned budget +// TITLE_MAX_TOKENS=1000) — under the main conversation's mvs_ id. A main turn +// never matches: it carries mcode's full tool surface beside the title tool, +// so the length-1 bar rejects every real turn. Name is read proto- +// agnostically (openai `{function:{name}}` and flat `{name}`). +export const MCODE_TITLE_TOOL = "submit_session_title"; +export function isMcodeTitleRequest(parsed: unknown): boolean { + if (!parsed || typeof parsed !== "object") return false; + const tools = (parsed as Record).tools; + if (!Array.isArray(tools) || tools.length !== 1) return false; + const t = tools[0]; + if (t === null || typeof t !== "object") return false; + const o = t as Record; + if (o.name === MCODE_TITLE_TOOL) return true; + const fn = o.function; + return fn !== null && typeof fn === "object" && (fn as Record).name === MCODE_TITLE_TOOL; +} + +// mcode's prompt suggestion (default ON — fired after every accepted answer) +// and its Goal evaluator send a tool-less, BUDGET-LESS single user message +// reusing the main turn's resolved headers, including x-mavis-session-id +// (prompt-suggestion-service.ts:655-692; the evaluator's 32k budget variant +// keeps the same shape). Budget-less defeats the <=200 rung and no-tools +// defeats the #546 main rung, so isSideRequest can never see them — hence +// this dedicated clause. A real mcode main turn vetoes at every bar: it +// carries the full tool surface, and turn 2+ replays assistant/tool history +// (the role loop below rejects any non-user entry). Residual, accepted (same +// class as #5687): a tool-less FIRST main turn under a zero-tools config rides +// the side lane for that one turn; the next turn replays history and rejoins +// the full pipeline. No system carrier is required (the suggestion sends +// none) — that is the difference from isDshSingleShotSidecar. +export function isMcodeOneShotAuxRequest(protocol: string | null, parsed: unknown): boolean { + if (protocol === null || !parsed || typeof parsed !== "object") return false; + const p = parsed as Record; + // Same no-tools veto as isSideRequest/isDshTitleRequest: any action surface + // marks an agent turn. + if (Array.isArray(p.tools) && p.tools.length > 0) return false; + const entries = protocol === "responses" ? p.input : protocol === "google" ? p.contents : p.messages; + if (!Array.isArray(entries)) return false; + let users = 0; + let userHasContent = false; + for (const entry of entries) { + if (entry === null || typeof entry !== "object") continue; + const rec = entry as Record; + const role = rec.role; + if (role === "system" || role === "developer") continue; + // The replay veto: ANY assistant/tool turn is a history resend, not a + // one-shot auxiliary call. + if (role !== "user") return false; + if (rec.type !== undefined && rec.type !== "message") return false; + users += 1; + const c = rec.content ?? rec.parts; + if (typeof c === "string") { + if (c.length > 0) userHasContent = true; + } else if (Array.isArray(c) && c.length > 0) { + userHasContent = true; + } + } + return users === 1 && userHasContent; +} + +// opencode's built-in title agent (on by default) fires CONCURRENTLY with the +// first main turn of every root session, under the root session's OWN id +// (runner/llm.ts:202): a single user message framed by a fixed wire template +// (session/title.ts:37-41), no tools, and NO output budget (kind==="title" → +// options {} — see the #1699 note at the top of this file). The #1699 defense +// covers only PLUGIN installs (x-bili-plugin-agent: title); the caller gates +// this clause on the opencode User-Agent (app.ts:22 — a constant format +// "opencode///", not a plugin signal), so this closes +// the pure-proxy gap. Both frame variants are pinned: the first-turn call and +// the regeneration call (≤8000 chars of history, same framing). +export const OPENCODE_TITLE_FRAME_PREFIXES: readonly string[] = [ + "Here is the first message of the conversation:", + "Here is the conversation so far:", +]; +export function isOpencodeTitleRequest(protocol: string | null, parsed: unknown): boolean { + if (protocol === null || !parsed || typeof parsed !== "object") return false; + const p = parsed as Record; + // No-tools veto (same discipline as every clause above). + if (Array.isArray(p.tools) && p.tools.length > 0) return false; + // Budget-LESS is part of the pinned shape (kind==="title" sends options {}): + // a budgeted single-user request is not the title agent's wire form. + if (outputBudgetField(p) !== null) return false; + // ONE user entry total: on the chat wires the system prompt rides INSIDE + // messages[] (so the entry count cannot be the bar — that is the #3149 + // class of bug); any assistant/tool row is a replayed history and vetoes, + // and a second user entry does too. + const entries = protocol === "responses" ? p.input : protocol === "google" ? p.contents : p.messages; + if (!Array.isArray(entries)) return false; + let userText: string | null = null; + for (const entry of entries) { + if (entry === null || typeof entry !== "object") continue; + const rec = entry as Record; + const role = rec.role; + if (role === "system" || role === "developer") continue; + if (role !== "user") return false; + if (rec.type !== undefined && rec.type !== "message") return false; + if (userText !== null) return false; + const c = rec.content ?? rec.parts; + let text = ""; + if (typeof c === "string") { + text = c; + } else if (Array.isArray(c)) { + for (const b of c) { + if (b !== null && typeof b === "object" && typeof (b as Record).text === "string") { + text += (b as Record).text as string; + } + } + } + userText = text; + } + if (userText === null) return false; + const t = userText.trimStart(); + return OPENCODE_TITLE_FRAME_PREFIXES.some((prefix) => t.startsWith(prefix)); +} + // #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 diff --git a/src/session-id.ts b/src/session-id.ts index e12327a225..e0b220ee9f 100644 --- a/src/session-id.ts +++ b/src/session-id.ts @@ -61,8 +61,11 @@ export function conversationHeaderSource(headers: Record / btw-), so the session id is the only trustworthy + // one there). Client-specific name — same trust class as the claude // header above: without it the request is anonymous and falls to // prefix-affinity, which forks every turn because grok-shell reorders // its replayed history head between turns. diff --git a/tests/e2e-session-identity.test.ts b/tests/e2e-session-identity.test.ts index b10ccbb674..7da555271e 100644 --- a/tests/e2e-session-identity.test.ts +++ b/tests/e2e-session-identity.test.ts @@ -279,7 +279,11 @@ test("e2e session identity: anthropic x-claude-code-session-id survives x-api-ke test("e2e session identity: anthropic x-mavis-session-id survives x-api-key rotation (#1050)", async () => { const h = await startHarness(); try { - const body = anthropicBody(); + // #6884: a real mcode main turn carries the agent's tool surface; a + // tools-less single-user body on the mcode lane is exactly the + // one-shot auxiliary shape bili now demotes, so the fixture keeps the + // (identity-irrelevant) tool to stay a main turn. + const body = { ...anthropicBody(), tools: [{ type: "function", function: { name: "bash", description: "run a command", parameters: { type: "object", properties: {} } } }] }; await post(h, 0, "/v1/messages", body, { "x-api-key": "sk-mm-old", "x-mavis-session-id": "mcode-sess-1" }); await post(h, 0, "/v1/messages", body, { "x-api-key": "sk-mm-new", "x-mavis-session-id": "mcode-sess-1" }); const sessions = await getSessions(h); diff --git a/tests/issue6884-wire-client-sidecars.test.ts b/tests/issue6884-wire-client-sidecars.test.ts new file mode 100644 index 0000000000..08866b605f --- /dev/null +++ b/tests/issue6884-wire-client-sidecars.test.ts @@ -0,0 +1,280 @@ +/** + * #6884: the wire-client legs of the cross-agent session-identity audit + * (discussions/6884; follow-on to #7077's pi purpose declaration). + * + * Source review of all five verbatim-roster clients (grok-shell, mcode, + * goose, CodeBuddy, opencode) confirmed their session-id lifecycles are + * HEALTHY — one id per logical conversation, forks/subagents mint fresh ids. + * The exposure this file pins: each fires auxiliary LLM calls (titles, + * compaction, suggestions, memory, naming) UNDER THE SAME id, and those + * bodies defeat isSideRequest's heuristics (no tools + missing/small/large + * budget), so pre-change they entered the main session's kernel pipeline as + * conversation turns — one-shot pollution of the real conversation's + * compression state. + * + * The clauses (src/server/handle.ts, gated on the client's own wire signal — + * same trust tier as the roster in src/session-id.ts): + * - grok-shell: x-grok-session-id WITHOUT x-grok-turn-idx → side (main turns + * always stamp the turn index; grok's own test-support contract). + * - mcode: x-mavis-session-id + (single submit_session_title tool | + * tool-less budget-less single user with no history). + * - goose: agent-session-id + the dsh single-shot structural shape. + * - CodeBuddy: x-compaction present → side; x-context-compacted does NOT + * (it appears on ordinary post-compaction MAIN turns). + * - opencode: User-Agent "opencode/" + the pinned title template shape. + */ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; +import { mkdtempSync, rmSync, mkdirSync } 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"; +import { isMcodeOneShotAuxRequest, isMcodeTitleRequest, isOpencodeTitleRequest, MCODE_TITLE_TOOL, OPENCODE_TITLE_FRAME_PREFIXES } from "../src/server/side-request.ts"; + +process.env.NODE_ENV = "test"; +const testRoot = mkdtempSync(join(tmpdir(), "bili-issue6884w-")); +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; + +// The shape a wire-client auxiliary call that DEFEATS the heuristics has: +// no tools, an output budget ABOVE the <=200 side heuristic (so neither the +// budget rung nor the #546 tools rung can see it), a system carrier, one +// user message. Pre-change this entered the main kernel pipeline. +const AUX_LARGE_BUDGET_BODY = { + model: "fake-a", max_completion_tokens: 8192, stream: false, + messages: [ + { role: "system", content: "You are a compact summarizer." }, + { role: "user", content: "summarize the conversation so far into bullets" }, + ], +}; + +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; + mkdirSync(join(dir, "billion-context", "sessions"), { recursive: true }); + mkdirSync(join(dir, "sessions"), { recursive: true }); + _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({ 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 } })); + }); + }); + 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: { "fake-a": { 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}`; + // `extra` carries the client's own wire signal. Every request also + // carries x-acp-session as a fallback id so the session exists even for + // clients whose own header is absent (the control cases); the + // client-specific roster headers outrank it in the resolution ladder. + const send = async (conversationId: string, body: Record, extra: Record = {}) => { + const headers: Record = { "content-type": "application/json", "x-acp-session": conversationId, ...extra }; + return fetch(`${origin}/bili/${upstream.url}/v1/chat/completions`, { method: "POST", headers, body: JSON.stringify(body) }).then(async (res) => ({ status: res.status, body: (await res.json()) as Record })); + }; + const requests = (id: string) => resolveConversation(id).session?.stats.requests ?? 0; + return { forwarded, send, requests, origin, close: async () => { store.cancelAll(); proxy.close(); upstream.close(); await new Promise((r2) => setTimeout(r2, 50)); } }; +} + +test("grok-shell: session id WITHOUT the turn index → side passthrough (kernel untouched)", async () => { + const h = await harness(); + try { + const r = await h.send("gk-aux", AUX_LARGE_BUDGET_BODY, { "x-grok-session-id": "gk-aux" }); + assert.equal(r.status, 200); + assert.equal(h.requests("gk-aux"), 0, "auxiliary call (no turn-idx) must not run the full pipeline"); + assert.equal(h.forwarded.length, 1); + assert.deepEqual(h.forwarded.at(-1), AUX_LARGE_BUDGET_BODY, "side passthrough forwards verbatim"); + } finally { + await h.close(); + } +}); + +test("grok-shell: session id WITH the turn index → main pipeline (the foreground contract)", async () => { + const h = await harness(); + try { + const r = await h.send("gk-main", AUX_LARGE_BUDGET_BODY, { "x-grok-session-id": "gk-main", "x-grok-turn-idx": "3" }); + assert.equal(r.status, 200); + assert.equal(h.requests("gk-main"), 1, "a turn-stamped request is a foreground main turn even in aux-ish shape"); + } finally { + await h.close(); + } +}); + +test("grok-shell: without its session header the shape falls back to the heuristics (gate is the header, not the body)", async () => { + const h = await harness(); + try { + const r = await h.send("gk-plain", AUX_LARGE_BUDGET_BODY); + assert.equal(r.status, 200); + assert.equal(h.requests("gk-plain"), 1, "no grok header → no clause → legacy main-lane classification"); + } finally { + await h.close(); + } +}); + +test("mcode: title sidecar (single submit_session_title tool) → side", async () => { + const h = await harness(); + try { + const body = { + model: "fake-a", max_completion_tokens: 1000, stream: false, + tools: [{ type: "function", function: { name: "submit_session_title", description: "Submit the session title.", parameters: { type: "object", properties: { title: { type: "string" } } } } }], + messages: [{ role: "user", content: "Generate a concise title for: fix the flaky test in the parser" }], + }; + const r = await h.send("mc-title", body, { "x-mavis-session-id": "mc-title" }); + assert.equal(r.status, 200); + assert.equal(h.requests("mc-title"), 0, "the title call must not run the full pipeline"); + } finally { + await h.close(); + } +}); + +test("mcode: prompt-suggestion one-shot (no tools, no budget, single user) → side", async () => { + const h = await harness(); + try { + const body = { model: "fake-a", stream: false, messages: [{ role: "user", content: "Suggest a concise follow-up question to the user. Reply with only the question." }] }; + const r = await h.send("mc-sugg", body, { "x-mavis-session-id": "mc-sugg" }); + assert.equal(r.status, 200); + assert.equal(h.requests("mc-sugg"), 0, "budget-less single user defeats the legacy heuristics — the clause must catch it"); + } finally { + await h.close(); + } +}); + +test("mcode: without its session header the same bodies stay heuristic-classified (control)", async () => { + const h = await harness(); + try { + const suggestion = { model: "fake-a", stream: false, messages: [{ role: "user", content: "Suggest a concise follow-up question to the user." }] }; + const r1 = await h.send("mc-plain-sugg", suggestion); + assert.equal(r1.status, 200); + assert.equal(h.requests("mc-plain-sugg"), 1, "no mcode header → legacy main-lane classification (budget-less is never a side req)"); + const title = { + model: "fake-a", max_completion_tokens: 1000, stream: false, + tools: [{ type: "function", function: { name: "submit_session_title", description: "Submit the session title.", parameters: { type: "object" } } }], + messages: [{ role: "user", content: "Generate a concise title for: hello" }], + }; + const r2 = await h.send("mc-plain-title", title); + assert.equal(r2.status, 200); + assert.equal(h.requests("mc-plain-title"), 1, "the tool surface is a main-turn marker (#546) without the mcode gate"); + } finally { + await h.close(); + } +}); + +test("goose: single-shot aux shape under agent-session-id → side; the same shape without the header → main", async () => { + const h = await harness(); + try { + const body = { model: "fake-a", stream: false, messages: [{ role: "system", content: "You name conversations. Reply with a short title." }, { role: "user", content: "debug the wifi reconnect loop" }] }; + const r1 = await h.send("gs-naming", body, { "agent-session-id": "gs-naming" }); + assert.equal(r1.status, 200); + assert.equal(h.requests("gs-naming"), 0, "the goose naming call (no tools, no budget, single user) must not run the full pipeline"); + const r2 = await h.send("gs-plain", body); + assert.equal(r2.status, 200); + assert.equal(h.requests("gs-plain"), 1, "without the goose lane header the shape keeps the legacy classification"); + } finally { + await h.close(); + } +}); + +test("goose: a tool-carrying main turn keeps the full pipeline even under agent-session-id", async () => { + const h = await harness(); + try { + const body = { + model: "fake-a", max_completion_tokens: 8192, stream: false, + tools: [{ type: "function", function: { name: "read_file", description: "read", parameters: { type: "object" } } }], + messages: [{ role: "system", content: "You are Goose." }, { role: "user", content: "read the manifest" }], + }; + const r = await h.send("gs-main", body, { "agent-session-id": "gs-main" }); + assert.equal(r.status, 200); + assert.equal(h.requests("gs-main"), 1, "no-tools veto: any action surface marks a main turn (#546)"); + } finally { + await h.close(); + } +}); + +test("CodeBuddy: x-compaction → side; x-context-compacted does NOT demote (post-compaction main turns keep the pipeline)", async () => { + const h = await harness(); + try { + // The compaction request itself: large budget, no tools — heuristic- + // invisible; the wire header is the identity. + const r1 = await h.send("cb-compact", AUX_LARGE_BUDGET_BODY, { "x-conversation-id": "cb-compact", "x-compaction": "summary" }); + assert.equal(r1.status, 200); + assert.equal(h.requests("cb-compact"), 0, "the compaction generation request must ride the side passthrough"); + // A post-compaction ordinary main turn carries x-context-compacted — + // consuming it would demote the real mainline (the deliberate veto). + const mainTurn = { model: "fake-a", max_completion_tokens: 8192, stream: false, messages: [{ role: "user", content: "keep going with the refactoring" }, { role: "assistant", content: "done" }, { role: "user", content: "and now the tests" }] }; + const r2 = await h.send("cb-main", mainTurn, { "x-conversation-id": "cb-main", "x-context-compacted": "1" }); + assert.equal(r2.status, 200); + assert.equal(h.requests("cb-main"), 1, "x-context-compacted alone must never demote a main turn"); + } finally { + await h.close(); + } +}); + +test("opencode: title-gen shape under the opencode UA → side; other UAs keep the legacy classification", async () => { + const h = await harness(); + try { + const body = { model: "fake-a", stream: false, messages: [{ role: "system", content: "You name conversations. Be concise." }, { role: "user", content: OPENCODE_TITLE_FRAME_PREFIXES[0] + "\n\n\nfix the flaky test\n\n\nReply with only the title." }] }; + const r1 = await h.send("oc-title", body, { "user-agent": "opencode/dev/2.3.0/opencode", "x-opencode-session-id": "oc-title" }); + assert.equal(r1.status, 200); + assert.equal(h.requests("oc-title"), 0, "the pure-proxy title-gen (no tools, no budget) must not enter the main pipeline"); + // Regeneration variant. + const regen = { ...body, messages: [{ role: "system", content: "You name conversations." }, { role: "user", content: OPENCODE_TITLE_FRAME_PREFIXES[1] + "\n\n…\n\nReply with only the title." }] }; + const r2 = await h.send("oc-regen", regen, { "user-agent": "opencode/dev/2.3.0/opencode", "x-opencode-session-id": "oc-regen" }); + assert.equal(r2.status, 200); + assert.equal(h.requests("oc-regen"), 0, "the regeneration frame is pinned too"); + // Same bytes, non-opencode UA: the lane evidence is absent → legacy. + const r3 = await h.send("oc-plain", body, { "user-agent": "curl/8.5.0" }); + assert.equal(r3.status, 200); + assert.equal(h.requests("oc-plain"), 1, "without the opencode UA the shape keeps the legacy classification"); + } finally { + await h.close(); + } +}); + +test("isMcodeTitleRequest: exact one-tool shape only (pure predicate pins)", () => { + const tool = { type: "function", function: { name: MCODE_TITLE_TOOL } }; + assert.equal(isMcodeTitleRequest({ tools: [tool] }), true, "openai function form"); + assert.equal(isMcodeTitleRequest({ tools: [{ name: MCODE_TITLE_TOOL }] }), true, "flat name form"); + assert.equal(isMcodeTitleRequest({ tools: [tool, { type: "function", function: { name: "bash" } }] }), false, "main turn: other tools beside the title tool"); + assert.equal(isMcodeTitleRequest({ tools: [{ type: "function", function: { name: "bash" } }] }), false, "different single tool"); + assert.equal(isMcodeTitleRequest({}), false, "no tools"); + assert.equal(isMcodeTitleRequest(null), false, "null body"); +}); + +test("isMcodeOneShotAuxRequest: single user, no history, no tools (pure predicate pins)", () => { + assert.equal(isMcodeOneShotAuxRequest("openai", { messages: [{ role: "user", content: "suggest a follow-up" }] }), true, "budget-less single user"); + assert.equal(isMcodeOneShotAuxRequest("openai", { messages: [{ role: "system", content: "sys" }, { role: "user", content: "one" }, { role: "assistant", content: "two" }] }), false, "assistant history vetoes"); + assert.equal(isMcodeOneShotAuxRequest("openai", { messages: [{ role: "user", content: "one" }, { role: "tool", content: "result", tool_call_id: "c1" }] }), false, "tool result vetoes"); + assert.equal(isMcodeOneShotAuxRequest("openai", { tools: [{ type: "function", function: { name: "bash" } }], messages: [{ role: "user", content: "one" }] }), false, "tools veto (#546)"); + assert.equal(isMcodeOneShotAuxRequest("openai", { messages: [{ role: "user", content: "" }] }), false, "empty user is not a one-shot call"); + assert.equal(isMcodeOneShotAuxRequest(null, { messages: [{ role: "user", content: "one" }] }), false, "protocol null"); +}); + +test("isOpencodeTitleRequest: both frames, budget-less, one entry (pure predicate pins)", () => { + for (const prefix of OPENCODE_TITLE_FRAME_PREFIXES) { + assert.equal(isOpencodeTitleRequest("openai", { messages: [{ role: "user", content: prefix + "\n\nx" }] }), true, `frame ${JSON.stringify(prefix)}`); + } + assert.equal(isOpencodeTitleRequest("openai", { max_tokens: 512, messages: [{ role: "user", content: OPENCODE_TITLE_FRAME_PREFIXES[0] }] }), false, "a budget is not the title agent's wire form"); + assert.equal(isOpencodeTitleRequest("openai", { tools: [{ type: "function", function: { name: "bash" } }], messages: [{ role: "user", content: OPENCODE_TITLE_FRAME_PREFIXES[0] }] }), false, "tools veto"); + assert.equal(isOpencodeTitleRequest("openai", { messages: [{ role: "user", content: OPENCODE_TITLE_FRAME_PREFIXES[0] }, { role: "assistant", content: "t" }] }), false, "history vetoes"); + assert.equal(isOpencodeTitleRequest("openai", { messages: [{ role: "user", content: "Here is a message about conversations: x" }] }), false, "near-miss text"); +}); diff --git a/tests/server-process-listeners.test.ts b/tests/server-process-listeners.test.ts index a736ffd7e8..f3258cb9bd 100644 --- a/tests/server-process-listeners.test.ts +++ b/tests/server-process-listeners.test.ts @@ -45,6 +45,26 @@ function restoreListeners(before: ReturnType): void { } } +// #7026 review follow-up (windows CI red, discussions/6884 audit): on POSIX, +// process.kill(self, sig) dispatches to Node's signal handlers. On WINDOWS the +// same call terminates the process (TerminateProcess) WITHOUT running the +// handlers — the runtime only honors real console control events, and Node's +// own docs state "On Windows, the signal being used to terminate a process +// cannot be changed. SIGTERM is sent." (Verified on a win10 box: SIGINT and +// SIGTERM self-kills die the process with the handler never invoked.) The +// original test 4 therefore killed its own child on the windows-latest CI +// runner before any assertion could run. Invoke the installed handler directly +// instead: it exercises the identical shutdown() path a console Ctrl+C would +// deliver. +const sendSignal = (sig: NodeJS.Signals): void => { + if (process.platform === "win32") { + const listeners = processEvents.listeners(sig) as Array<(...args: unknown[]) => void>; + listeners[listeners.length - 1]?.(); + return; + } + process.kill(process.pid, sig); +}; + test("closing each server releases its process handlers across repeated lifecycles", async () => { const before = snapshot(); const servers: Server[] = []; @@ -124,21 +144,31 @@ test("graceful shutdown keeps signal handlers until the process exits", async () const server = await start(); try { const serving = snapshot(); - process.kill(process.pid, "SIGINT"); + sendSignal("SIGINT"); await once(server, "close"); // Let the shutdown chain reach (the stubbed) process.exit first: the // test process must not drain out from under the assertions while the - // flush is still running. - await new Promise((resolve) => setTimeout(resolve, 500)); + // flush is still running. Bounded wait instead of a fixed 500ms sleep + // — each flush stage is deadline-capped at EXIT_FLUSH_DEADLINE_MS + // (5s), so a fixed sleep races slow volumes (windows CI first). + for (let i = 0; i < 600 && exits.length === 0; i += 1) { + await new Promise((resolve) => setTimeout(resolve, 10)); + } assertSameListeners(serving); // Second Ctrl+C during the flush window: must be absorbed by the // idempotent shutdown(), not delivered to Node's default handler. - process.kill(process.pid, "SIGTERM"); + sendSignal("SIGTERM"); await new Promise((resolve) => setImmediate(resolve)); assertSameListeners(serving); assert.ok(exits.length <= 1); } finally { restoreListeners(before); + // This is the file's LAST test: if the shutdown chain has not reached + // the stubbed exit yet, keep the stub in place — a late REAL exit(0) + // would kill the runner before the TAP summary is written. The node + // --test child ends via event-loop drain, not process.exit, so the + // pending chain's exit(0) is then harmlessly recorded instead of fired. + if (exits.length === 0) return; process.exit = realExit; } }); diff --git a/tests/side-lane-invariants.test.ts b/tests/side-lane-invariants.test.ts index 1d23a43f70..ce6c6d70ab 100644 --- a/tests/side-lane-invariants.test.ts +++ b/tests/side-lane-invariants.test.ts @@ -367,7 +367,7 @@ test("(G) extracted pipeline module keeps the side-lane boundary (#1440 P2 cut 3 const text = readFileSync(fileURLToPath(new URL("../src/server/handle.ts", import.meta.url)), "utf8"); const laneInternals = /\bSIDE_REQUEST_MAX_TOKENS\b|\bSIDE_REQUEST_AGENTS\b|\bDemoteGateSignals\b|\bSideLaneSignals\b|\bSideLaneDecision\b|\bBILI_TOOL_NAMES\b|\bOutputBudgetField\b|\boutputBudgetField\b|\breadOutputBudget\b|\bwriteOutputBudget\b|\b_resetNoOutputCeilingWarningsForTest\b/; assert.doesNotMatch(text, laneInternals, "handle.ts must stay decoupled from the side-lane engine internals"); - const allowlist = new Set(["hasLeakedBiliToolsOnly", "hasSingleShotBudget", "isAudioOutputCall", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isResponsesServerToolSingleShot", "isServerToolUtilityCall", "isSideRequest", "isStructuredOutputCall", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); + const allowlist = new Set(["hasLeakedBiliToolsOnly", "hasSingleShotBudget", "isAudioOutputCall", "isClaudeCodeTitleRequest", "isDshAutoReviewRequest", "isDshSingleShotSidecar", "isDshTitleRequest", "isMcodeOneShotAuxRequest", "isMcodeTitleRequest", "isOpencodeTitleRequest", "isResponsesServerToolSingleShot", "isServerToolUtilityCall", "isSideRequest", "isStructuredOutputCall", "resolveSideLane", "demoteGate", "stripLeakedBiliTools", "restoreOutputBudget", "sideRequestGuard"]); let sawImport = false; for (const m of text.matchAll(/\{([^}]*)\}\s*from\s*["'][^"']*side-request\.js["']/g)) { sawImport = true;