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
38 changes: 37 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,12 +564,27 @@ type Prepared = {
* in forward() rebuilds the SAME system prompt the request was prepared
* with. */
prompts?: Prompts;
/** #388: side request (title-gen etc.) — transport with render-tag strip
* only. Skips preflight (handle() returns before it), the fake-completion
* retry wrapper, the compress loop, and every usage-sniffing pipe; the
* #460 strip pipes in forward() run with session=undefined. */
sidePassthrough?: boolean;
/** Set when a codex native-compaction request was intercepted and a
* success response was forged locally (BILI_CODEX_COMPACT=intercept +
* gate passed). forward() serves `body` without contacting upstream. */
codexForge?: { kind: "endpoint" | "trigger"; body: string; contentType: string };
};

// #388: side requests (title-gen etc.) share the main session key but must not
// touch kernel state. Identified by a tiny output budget (same heuristic as
// prepareOpenai's isTitleGen); a missing/non-positive budget is never a side req.
const SIDE_REQUEST_MAX_TOKENS = 200;
export function isSideRequest(parsed: unknown): boolean {
if (!parsed || typeof parsed !== "object") return false;
const p = parsed as Record<string, unknown>;
const raw = p.max_tokens ?? p.max_completion_tokens ?? p.max_output_tokens;
return typeof raw === "number" && raw > 0 && raw <= SIDE_REQUEST_MAX_TOKENS;
}

function isTrustedAdminOrigin(origin: string | undefined, host: string | undefined, trustedHosts: Set<string>): boolean {
// Host must be one of OUR listen identities regardless of whether an
Expand Down Expand Up @@ -1226,6 +1241,27 @@ async function handle(
// exactly one system at index 0, #377) accept it and the head system
// message stays byte-stable for the prefix cache.
const pluginMode = pluginAgent !== undefined;
// #388: side requests (title-gen etc.) share the main session key but
// must not touch kernel state (processTurn/snapshot/usage would pollute
// the main view). Forward with a minimal prepared marked sidePassthrough:
// the #460 render-tag strip pipes still run (response hygiene), while
// preflight / fake-completion retry / the loop / usage sniffing are all
// skipped. processedMessages stays empty so the loop can never engage.
if (!countTokens && !responsesCompact && protocol !== null && isSideRequest(parsed)) {
log("info", `[${session.id}] side request (max_tokens<=${SIDE_REQUEST_MAX_TOKENS}) → passthrough + tag strip only, kernel state untouched`);
const sidePrepared: Prepared = {
body: bodyBuffer,
session,
processedMessages: [],
originalMessages: [],
protocol,
stream: (parsed as { stream?: unknown }).stream === true,
compressInjected: false,
sidePassthrough: true,
};
await forward(req, res, opts, bodyBuffer, sidePrepared, core, reqConfig, log, route, instanceId, affinity);
return;
}
// Self-heal the context window: a prior upstream overflow may have
// taught us the real window (forward()'s overflow detection persists it
// to metadata.learnedContextLimits, keyed by model, or the legacy scalar
Expand Down Expand Up @@ -2808,7 +2844,7 @@ async function forward(
// skipped the trailing clearUpstreamTimer and leaked a live 10-minute
// timer plus its socket for the full window.
try {
if (prepared !== null && prepared.stream && maxFakeCompletionRetries() > 0) {
if (prepared !== null && prepared.stream && !prepared.sidePassthrough && maxFakeCompletionRetries() > 0) {
const resolvedBuf = await resolveFakeCompletion(upstream.body, {
protocol: prepared.protocol,
body,
Expand Down
2 changes: 1 addition & 1 deletion tests/issue411-abort-usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async function register(rig: Rig, conversationId: string): Promise<void> {
}

function modelBody(stream: boolean): string {
return JSON.stringify({ model: "i411-model", max_tokens: 10, stream, messages: [{ role: "user", content: "hello" }] });
return JSON.stringify({ model: "i411-model", max_tokens: 8192, stream, messages: [{ role: "user", content: "hello" }] });
}

async function waitFor(cond: () => boolean, ms = 3000): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion tests/issue473-plugin-fake-completion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ test("#473 plugin anthropic: fake completion retried with the hint merged; recov
const res = await fetch(rig.modelUrl(), {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ model: "i473-model", max_tokens: 64, stream: true, messages: [{ role: "user", content: "call the weather tool" }] }),
body: JSON.stringify({ model: "i473-model", max_tokens: 1024, stream: true, messages: [{ role: "user", content: "call the weather tool" }] }),
});
assert.equal(res.status, 200);
const text = await readAll(res);
Expand Down
4 changes: 2 additions & 2 deletions tests/launcher-plugin-mode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function postModel(rig: Rig, sessionHeader?: string): Promise<Response> {
return fetch(rig.modelUrl(), {
method: "POST",
headers,
body: JSON.stringify({ model: "l162-model", max_tokens: 10, stream: true, messages: [{ role: "user", content: "hello" }] }),
body: JSON.stringify({ model: "l162-model", max_tokens: 8192, stream: true, messages: [{ role: "user", content: "hello" }] }),
});
}

Expand Down Expand Up @@ -195,7 +195,7 @@ test("launcher identity binding survives model switches (one conversation, multi
const postUpstream2 = (): Promise<Response> => fetch(`http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${port2}/v1/messages`, {
method: "POST",
headers: { "content-type": "application/json", "x-claude-code-session-id": "sess-switch" },
body: JSON.stringify({ model: "l162-model", max_tokens: 10, stream: true, messages: [{ role: "user", content: "hello" }] }),
body: JSON.stringify({ model: "l162-model", max_tokens: 8192, stream: true, messages: [{ role: "user", content: "hello" }] }),
});
// First model (upstream A): binds via identity as usual.
await register(rig, "sess-switch", { agent: "omp", identity: true });
Expand Down
5 changes: 4 additions & 1 deletion tests/proxy-noinject-tag-strip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,13 @@ test("#475 G3: fake-completion buffering + streaming request answered with JSON
const completion = { id: "chatcmpl_fc_json", object: "chat.completion", created: 1, model: "gpt-test", choices: [{ index: 0, message: { role: "assistant", content: full }, finish_reason: "stop" }] };
const h = await startHarness({ injectTool: false, injectNudge: false }, [[JSON.stringify(completion)]], true);
try {
// Not a side request (budget > 200) so the fake-completion buffering
// block runs — this test guards against it draining upstream.body
// before the G3 JSON strip branch reads responseBody.
const resp = await fetch(`http://127.0.0.1:${h.proxyPort}/bili/http://127.0.0.1:${h.upstreamPort}/v1/chat/completions`, {
method: "POST",
headers: { "content-type": "application/json", "x-acp-session": "noinject-fc-json" },
body: JSON.stringify({ model: "gpt-test", max_tokens: 100, stream: true, messages: [{ role: "user", content: "hello" }] }),
body: JSON.stringify({ model: "gpt-test", max_tokens: 4096, stream: true, messages: [{ role: "user", content: "hello" }] }),
});
assert.equal(resp.status, 200);
let bodyText = "";
Expand Down
200 changes: 200 additions & 0 deletions tests/side-request-isolation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import test from "node:test";
import assert from "node:assert/strict";
import http from "node:http";
import { once } from "node:events";
import { defaultConfig, createInitialState } from "acp-kernel";
import { startServer, type ProxyOptions, isSideRequest } from "../src/server.ts";
import { SessionStore, _setStoreForTest } from "../src/persist.ts";
import { _setForTest as setRegistryForTest } from "../src/registry.ts";
import { getSession } from "../src/session.ts";

// #388: side requests (title-gen / small utility calls) share the main session
// key but must not touch kernel state. The proxy routes them as pure passthrough
// (no processTurn / snapshot / usage capture) so the main session's view, nudge
// baseline and per-block survivedCount are driven ONLY by main requests.

test("isSideRequest: tiny output budget across protocol field names", () => {
assert.equal(isSideRequest({ max_tokens: 100 }), true);
assert.equal(isSideRequest({ max_tokens: 200 }), true, "boundary 200 is a side request");
assert.equal(isSideRequest({ max_tokens: 201 }), false, "201 is NOT a side request");
assert.equal(isSideRequest({ max_completion_tokens: 150 }), true, "openai max_completion_tokens");
assert.equal(isSideRequest({ max_output_tokens: 50 }), true, "responses max_output_tokens");
assert.equal(isSideRequest({ max_tokens: 8192 }), false, "normal budget is not a side request");
assert.equal(isSideRequest({}), false, "no budget → not a side request");
assert.equal(isSideRequest(null), false, "null body");
assert.equal(isSideRequest(undefined), false, "undefined body");
assert.equal(isSideRequest("not-an-object"), false, "non-object body");
assert.equal(isSideRequest({ max_tokens: 0 }), false, "zero budget");
assert.equal(isSideRequest({ max_tokens: -5 }), false, "negative budget");
assert.equal(isSideRequest({ max_tokens: "100" }), false, "string budget is not a number");
});

const MODEL = "claude-sonnet-4-5";
const SESSION = "side-iso-sess";
const MAIN_INPUT_TOKENS = 50_000;
const SIDE_INPUT_TOKENS = 56;

function okSse(inputTokens: number): string {
return (
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "m1", role: "assistant", usage: { input_tokens: inputTokens } } })}\n\n` +
`event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n` +
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n` +
`event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n` +
`event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 3 } })}\n\n` +
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`
);
}

function mainConversation(n: number): Array<{ role: string; content: string }> {
const msgs: Array<{ role: string; content: string }> = [];
for (let i = 0; i < n; i++) {
msgs.push({ role: i % 2 === 0 ? "user" : "assistant", content: `Main message ${i} ${"z".repeat(500)}` });
}
return msgs;
}

interface Rig {
proxyPort: number;
upstreamPort: number;
proxy: http.Server;
upstream: http.Server;
/** When set, served verbatim for side requests (max_tokens<=200) instead
* of the generated okSse stream. */
sideScript: string | null;
}

async function startRig(): Promise<Rig> {
const rig: Rig = { proxyPort: 0, upstreamPort: 0, proxy: null as unknown as http.Server, upstream: null as unknown as http.Server, sideScript: null };
const upstream = http.createServer((req, res) => {
const chunks: Buffer[] = [];
req.on("data", (c: Buffer) => chunks.push(c));
req.on("end", () => {
const raw = Buffer.concat(chunks).toString("utf8");
let parsed: { max_tokens?: number } = {};
try { parsed = JSON.parse(raw); } catch { /* keep {} */ }
// Side requests (tiny max_tokens) report a TINY context; main requests
// report a large one. The proxy must NOT capture the side request's
// usage — that is exactly the pollution this regression guards.
const isSide = typeof parsed.max_tokens === "number" && parsed.max_tokens <= 200;
res.writeHead(200, { "content-type": "text/event-stream" });
res.end(isSide && rig.sideScript ? rig.sideScript : okSse(isSide ? SIDE_INPUT_TOKENS : MAIN_INPUT_TOKENS));
});
});
upstream.listen(0, "127.0.0.1");
await once(upstream, "listening");
const upstreamPort = upstream.address().port as number;

_setStoreForTest(new SessionStore({ enabled: false }));
setRegistryForTest({});
const proxy = await startServer({
port: 0,
host: "127.0.0.1",
upstream: "http://127.0.0.1",
routes: { [`http://127.0.0.1:${upstreamPort}`]: {} },
modelContextLimit: 200_000,
kernelConfig: defaultConfig(200_000),
compress: { injectTool: true, injectNudge: true },
promptCache: { routing: "auto" },
sessionHeader: "x-acp-session",
log: false,
debug: false,
passthrough: false,
autoUpdate: false,
mitm: { enabled: false, domains: [] },
} as ProxyOptions);
await once(proxy, "listening");
const proxyPort = proxy.address().port as number;
rig.proxy = proxy; rig.upstream = upstream; rig.proxyPort = proxyPort; rig.upstreamPort = upstreamPort;
return rig;
}

async function closeRig(rig: Rig): Promise<void> {
rig.proxy.close();
await once(rig.proxy, "close");
rig.upstream.close();
await once(rig.upstream, "close");
}

test("e2e: side request response still gets render-tag stripping (#460 contract)", async () => {
const LT = "\x3c";
const GT = "\x3e";
const OPEN_MARK = `${LT}acp `;
const CLOSE_MARK = `${LT}/acp${GT}`;
const rig = await startRig();
try {
const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/messages`;
const headers: Record<string, string> = { "content-type": "application/json", "x-acp-session": SESSION };
// Main request first so the session has a real view.
const r1 = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 1024, stream: true, messages: mainConversation(8) }) });
await r1.text();
const stateAfterMain = JSON.stringify(getSession(SESSION).state);
// Side request whose stream echoes a render tag (a model echoing the
// compressed history into a title). The strip pipes must still run for
// side requests — kernel state untouched, response hygiene intact.
const tagged = `title: ${LT}acp tokens="12" type="text"${GT}m00009${LT}/acp${GT}ok`;
const sse =
`event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "m1", role: "assistant", usage: { input_tokens: SIDE_INPUT_TOKENS } } })}\n\n` +
`event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: tagged } })}\n\n` +
`event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`;
rig.sideScript = sse;
const r2 = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 100, stream: true, messages: [{ role: "user", content: "Generate a short title." }] }) });
assert.equal(r2.status, 200);
let raw = "";
for await (const chunk of r2.body) raw += Buffer.from(chunk).toString("utf8");
assert.equal(raw.includes(OPEN_MARK), false, "side-request stream leaked a render open tag");
assert.equal(raw.includes(CLOSE_MARK), false, "side-request stream leaked a render close tag");
assert.equal(JSON.stringify(getSession(SESSION).state), stateAfterMain, "tag-strip pipe must not touch kernel state (session stays off)");
} finally {
await closeRig(rig);
}
});

test("e2e: anthropic side request (title-gen) leaves main session kernel state untouched", async () => {
const rig = await startRig();
try {
const url = `http://127.0.0.1:${rig.proxyPort}/bili/http://127.0.0.1:${rig.upstreamPort}/v1/messages`;
const headers: Record<string, string> = { "content-type": "application/json", "x-acp-session": SESSION };

// Main request 1: a normal turn (large max_tokens) → kernel state mutates.
const r1 = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 1024, stream: true, messages: mainConversation(8) }) });
assert.equal(r1.status, 200);
await r1.text();

const s1 = getSession(SESSION);
assert.ok(s1, "session exists after the main request");
assert.notEqual(JSON.stringify(s1.state), JSON.stringify(createInitialState()), "main request must mutate kernel state");
assert.ok(Object.keys(s1.state.messageRefs.byRaw).length > 0, "refs assigned to the main messages");
assert.equal(s1.stats.lastInputTokens, MAIN_INPUT_TOKENS, "main request usage captured as the nudge baseline");
assert.ok(s1.lastMessages && s1.lastMessages.length > 0, "message snapshot set to the main request view");

const stateAfterMain1 = JSON.stringify(s1.state);
const statsAfterMain1 = JSON.stringify(s1.stats);
const snapshotAfterMain1 = JSON.stringify(s1.lastMessages);
const requestsAfterMain1 = s1.stats.requests;

// Side request: title-gen (tiny max_tokens) on the SAME session key.
const r2 = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 100, stream: true, messages: [{ role: "user", content: "Generate a short title for this conversation." }] }) });
assert.equal(r2.status, 200);
await r2.text();

const s2 = getSession(SESSION);
assert.ok(s2, "session still exists after the side request");
assert.equal(JSON.stringify(s2.state), stateAfterMain1, "side request must NOT mutate kernel state (refs / survivedCount / nudge baseline)");
assert.equal(JSON.stringify(s2.stats), statsAfterMain1, "side request must NOT mutate stats");
assert.equal(JSON.stringify(s2.lastMessages), snapshotAfterMain1, "side request must NOT clobber the message snapshot (bili export view)");
assert.equal(s2.stats.lastInputTokens, MAIN_INPUT_TOKENS, "side request's tiny usage must NOT overwrite the main nudge baseline");
assert.equal(s2.stats.requests, requestsAfterMain1, "side request must NOT increment the main request counter");

// Main request 2: a normal turn again → kernel state advances (monotonic).
const r3 = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 1024, stream: true, messages: mainConversation(9) }) });
assert.equal(r3.status, 200);
await r3.text();

const s3 = getSession(SESSION);
assert.ok(s3, "session exists after main request 2");
assert.notEqual(JSON.stringify(s3.state), stateAfterMain1, "main request 2 must advance kernel state (driven only by main requests)");
assert.ok(s3.stats.requests > requestsAfterMain1, "main request 2 increments the request counter");
} finally {
await closeRig(rig);
}
});
Loading