Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/server/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import { buildForgeResponse, classifierForgeVariant, estimateCjkTokens, isAutoMo
import { extractConversationId, isV1BackgroundSideRequest, isV1InternalRequest, unwrapV1Internal } from "../v1internal.js";
import { anthropicBetaContextWindow, BILI_HOP_HEADER, capRegistryWindowByStandard, expandedContextSuffixWindow, launcherContextWindow, launcherMaxOutput, windowSourceLogged } from "./context-window.js";
import { NO_IDENTITY_MESSAGE, safeSessionId } from "./headers.js";
import { demoteGate, hasLeakedBiliToolsOnly, isDshAutoReviewRequest, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, resolveSideLane, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools } from "./side-request.js";
import { demoteGate, hasLeakedBiliToolsOnly, isClaudeCodeTitleRequest, isDshAutoReviewRequest, isDshTitleRequest, isServerToolUtilityCall, isSideRequest, 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";
Expand Down Expand Up @@ -999,6 +999,16 @@ async function handleRequest(
// proxy-mode traffic never reaches it because the plugin header gate
// fails first).
const sideAgent = pluginRequestAgentHeader(req.headers);
// #5135: claude code session-title sidecar (proxy mode, header-less):
// fires ~1ms ahead of the first main turn under the SAME
// x-claude-code-session-id. Recognize it by shape so it neither claims
// the raw-key anchor in the #4774 order resolver (side requests resolve
// VERBATIM — #2156) nor touches kernel state downstream (#388
// passthrough). Evidence-per-client: gated on the claude-code
// conversation source — the exact population #4774 routes through the
// resolver — never opened globally.
const ccTitleSidecar = sideAgent !== "main" && claudeAnthropicPersonaOrderApplies(req.headers)
&& isClaudeCodeTitleRequest(protocol, parsed);
const sideRequestLike = !countTokens && !responsesCompact && protocol !== null
&& (isSideRequest(parsed, sideAgent)
// #2503: dsh title sidecar — its output budget grew past the
Expand All @@ -1017,6 +1027,9 @@ async function handleRequest(
// incident topology.
|| (pluginAgentHeader(req.headers) === "dsh" && sideAgent !== "main"
&& isDshAutoReviewRequest(protocol, parsed))
// #5135: the claude code title sidecar stays off the raw-key
// anchor claim and off the kernel pipeline entirely.
|| ccTitleSidecar
|| (pluginAgentHeader(req.headers) !== undefined && sideAgent !== "main"
&& req.headers["x-bili-ws-lane"] === undefined
&& hasLeakedBiliToolsOnly(parsed)
Expand Down Expand Up @@ -2107,7 +2120,10 @@ async function handleRequest(
const dshReviewSidecar = requestAgent !== "main" && isDshAutoReviewRequest(protocol, parsed)
&& (pluginAgentHeader(req.headers) === "dsh"
|| (typeof session.metadata.pluginAgent === "string" && session.metadata.pluginAgent === "dsh"));
const sideIntent = isSideRequest(parsed, requestAgent) || dshTitleSidecar || dshReviewSidecar;
// #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, requestAgent) || dshTitleSidecar || dshReviewSidecar || ccTitleSidecar;
// #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
Expand All @@ -2131,7 +2147,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: serverToolUtility ? "server-tool utility call (#2500)" : dshTitleSidecar ? "dsh title-gen sidecar (#2503)" : dshReviewSidecar ? "dsh auto-review sidecar (#4438)" : undefined });
const sideLane = resolveSideLane({ countTokens, responsesCompact, protocol, stripApplied: demotedSide, sideIntent, requestAgent, sideLabel: serverToolUtility ? "server-tool utility call (#2500)" : dshTitleSidecar ? "dsh title-gen sidecar (#2503)" : dshReviewSidecar ? "dsh auto-review sidecar (#4438)" : ccTitleSidecar ? "claude-code title-gen sidecar (#5135)" : 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
Expand Down
45 changes: 45 additions & 0 deletions src/server/side-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,51 @@ export function isDshAutoReviewRequest(protocol: string | null, parsed: unknown)
return users === 1 && userHasContent;
}

// #5135: Claude Code's session-title sidecar (terminal CLI, proxy mode — no
// x-bili-plugin stamp). It fires at session start UNDER THE SAME
// x-claude-code-session-id as the first main turn, ~1ms ahead of it. Field
// shape (0.1.209, three machines): a single user message wrapping the first
// prompt in \x3csession\x3e…\x3c/session\x3e, no tools, an output budget past
// the <=200 heuristic, and its OWN system prompt — so pre-fix it entered the
// #4774 order resolver on the virgin id and CLAIMED the raw key as kernel
// anchor; the main turn then arrived tagChanged and history-disjoint and
// forked onto a |sub:<fp> lane for the session lifetime (#2156 form: side
// requests must never claim the anchor). A main turn can never match: it
// carries a tools surface and multi-turn history; the one-entry bar rejects
// every history-replaying request. The caller must gate on
// claudeAnthropicPersonaOrderApplies (evidence-per-client discipline): the
// wrapper alone is not client proof.
export const CLAUDE_CODE_TITLE_SESSION_TAG = "\x3csession\x3e";

export function isClaudeCodeTitleRequest(protocol: string | null, parsed: unknown): boolean {
if (protocol === null || !parsed || typeof parsed !== "object") return false;
const p = parsed as Record<string, unknown>;
// Same no-tools veto as isSideRequest/isDshTitleRequest/isDshAutoReviewRequest.
if (Array.isArray(p.tools) && p.tools.length > 0) return false;
const entries = protocol === "responses" ? p.input : protocol === "google" ? p.contents : p.messages;
// Exactly ONE entry total: on the anthropic wire the system rides top-level,
// so any extra messages[] row (a role:system entry included) is a replayed
// history, not the one-shot title call.
if (!Array.isArray(entries) || entries.length !== 1) return false;
const entry = entries[0];
if (entry === null || typeof entry !== "object") return false;
const rec = entry as Record<string, unknown>;
if (rec.role !== "user") return false;
if (rec.type !== undefined && rec.type !== "message") 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<string, unknown>).text === "string") {
text += (b as Record<string, unknown>).text as string;
}
}
}
return text.trimStart().startsWith(CLAUDE_CODE_TITLE_SESSION_TAG);
}

// #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
Expand Down
Loading
Loading