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
30 changes: 21 additions & 9 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3550,10 +3550,14 @@ function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: string; cont
}

// Zero-baseline sessions are judged conservatively ONLY when they arrived
// anonymously (prefix-affinity forks/reloads, #553): they carry the full raw
// history but no measurement yet, so feeding 0 blinds the nudge (usage 0%,
// growth ref 0) and no compression trigger fires until overflow. Explicit-
// identity zero-baseline sessions previously stayed at 0 on the assumption
// anonymously AND hold no usage-grade anchor yet (prefix-affinity
// mints/forks/reloads, #553): they carry the full raw history but no
// measurement yet, so feeding 0 blinds the nudge (usage 0%, growth ref 0)
// and no compression trigger fires until overflow. An anchored anonymous
// session continues a MEASURED lineage — it sizes on the anchor exactly like
// an explicit session (#2033); the raw-history bound remains ONLY for the
// genuinely anchor-less case below. Explicit-identity zero-baseline sessions
// previously stayed at 0 on the assumption
// that they "self-heal via the next measured usage report" — an assumption
// that breaks for upstreams that NEVER report usage (ChatGPT-login backends,
// #728): lastInputTokens stays 0 for the whole session, and the kernel's
Expand Down Expand Up @@ -3581,9 +3585,6 @@ function effectiveTokenCount(session: Session, msgs: CoreMessage[], inboundImage
// the >100% ghost class, and the next real usage report overwrites it.
if (session.stats.lastInputTokens > 0 && (session.stats.lastInputTokensSource === "usage" || session.stats.lastInputTokensSource === "overflow-arm")) return { tokens: session.stats.lastInputTokens, source: "usage" };
const raw = estimateCoreMessagesUpper(msgs) + inboundImageTokens;
if (session.metadata.anonymousPrefixAffinity) return { tokens: raw, source: "estimate" };
const est = session.stats.localInputEstimate ?? 0;
if (est <= 0) return { tokens: 0, source: "estimate" };
// #1569/#1839: while the latest baseline is not usage-grade (the transient
// window right after a failed turn), sizing on ANY re-derived view is how
// ghosts enter: #1569 first tried min(est, raw) — the char-count upper
Expand All @@ -3596,10 +3597,21 @@ function effectiveTokenCount(session: Session, msgs: CoreMessage[], inboundImage
// itself. Growth between reports is backstopped by preflight (it measures
// the actual outbound payload before every forward) and the nudge
// reference re-anchors as soon as the next usage lands (#1595).
// Never-reporting upstreams (#553/#728) keep the fail-closed upper bound:
// their anchor stays absent.
// #2033: this check runs BEFORE the anonymous-prefix-affinity fallback so
// an anchored anonymous session (a measured lineage continued through pfa-*
// resolution) sizes on the anchor too — pre-fix the anonymous early return
// handed it the full raw-history bound right after one failed turn, the
// exact ghost path #1839 closed for explicit sessions. Never-reporting
// upstreams (#553/#728) are unaffected: their anchor stays absent, so the
// fail-closed upper bounds below still apply.
const grade = session.stats.lastUsageGradeTokens;
if (grade !== undefined && grade > 0) return { tokens: grade, source: "usage" };
// #553: zero-baseline ANCHOR-LESS anonymous sessions (prefix-affinity
// mints/forks/reloads) fall back to the raw-history upper bound — see the
// function header for why feeding 0 blinds them.
if (session.metadata.anonymousPrefixAffinity) return { tokens: raw, source: "estimate" };
const est = session.stats.localInputEstimate ?? 0;
if (est <= 0) return { tokens: 0, source: "estimate" };
return { tokens: Math.min(est, raw), source: "estimate" };
}

Expand Down
243 changes: 243 additions & 0 deletions tests/issue2033-anonymous-anchor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
import assert from "node:assert/strict";
import http from "node:http";
import { once } from "node:events";
import test from "node:test";

process.env.NODE_ENV = "test";

// #2033: effectiveTokenCount took the anonymous-prefix-affinity branch BEFORE
// checking lastUsageGradeTokens (src/server.ts), so an anonymous session that
// held a REAL usage anchor but whose latest turn wrote an estimate-grade
// failure arm (armFailureShrink after a 5xx with no usage report) sized the
// next turn on the FULL RAW HISTORY upper bound instead of the anchor — the
// exact ghost path #1839 closed for explicit-identity sessions, which DO reach
// the later anchor check. Pinned here with a genuine anonymous client (no
// identity headers — prefix affinity resolves the pfa-* session, the
// tests/fork-adoption.test.ts harness shape):
// A1. success-with-usage → 503-no-usage → appended turn on the same resolved
// session: the nudge must size on the usage anchor (source "usage"), not
// the raw-history bound; no spurious preflight summary calls, no spurious
// nudge injection.
// A2. anchor-less anonymous session (never-reporting upstream, #553/#728):
// the conservative raw-history fallback stays intact — the nudge is not
// blinded to 0 and stays estimate-grade.

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 { listSessions, _resetSessionsForTest, type Session } from "../src/session.ts";
import { setLogCapture } from "../src/logger.ts";

const WINDOW = 120_000;
const NUDGE_MARKER = "Context limit reached";
const ANCHOR_INPUT = 5000;

// Dense ASCII code fixture (same shape as the #1839/#1569 suites): the
// char-count upper bound of this history sits well ABOVE the 5000-token usage
// anchor (so the pre-fix raw-bound ghost is observable) but far below the 120k
// window (so NO turn may trigger preflight — any summary call means the sizing
// regressed).
const LINE = (i: number) =>
`const handler_${i} = (req: Request, res: Response) => { res.status(200).json({ status: "ok", id: ${i}, ts: Date.now() }); };`;
const HEAVY = (i: number) => `CODE_${i}_` + LINE(i).repeat(62);

type Msg = { role: string; content: unknown };

function baseConversation(): Msg[] {
const msgs: Msg[] = [];
for (let i = 0; i < 12; i++) {
msgs.push({ role: i % 2 === 0 ? "user" : "assistant", content: i < 8 ? HEAVY(i) : `note_${i}_short tail ${i}` });
}
return msgs;
}

type VerdictRule = { when?: string; status?: number; body?: string };

/** Content-routed OpenAI relay: a streaming chat completion gets the verdict of
* the FIRST rule whose `when` substring appears in its LAST message's content,
* else 200 SSE (usage reported iff reportUsage). Routing by the last message
* (not the whole body) matters: every turn resends the FULL history, so a
* marker in an earlier turn would keep matching every later turn. Non-stream
* calls (kernel summary lane) always get a plain completion. */
function makeRelay(rules: VerdictRule[], reportUsage: boolean) {
const received: Buffer[] = [];
let nonStream = 0;
const server = http.createServer((req, res) => {
const chunks: Buffer[] = [];
req.on("data", (c: Buffer) => chunks.push(c));
req.on("end", () => {
const raw = Buffer.concat(chunks);
received.push(raw);
let parsed: Record<string, unknown> = {};
try {
parsed = JSON.parse(raw.toString("utf8"));
} catch {
/* non-JSON — treat as non-stream */
}
if (parsed.stream !== true) {
nonStream += 1;
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({
id: "msg_summary", object: "chat.completion", created: 0, model: "gpt-test",
choices: [{ index: 0, message: { role: "assistant", content: "SUMMARY: ok." }, finish_reason: "stop" }],
usage: { prompt_tokens: 500, completion_tokens: 50, total_tokens: 550 },
}));
return;
}
const msgsArr = Array.isArray(parsed.messages) ? (parsed.messages as Array<Record<string, unknown>>) : [];
const lastMsg = msgsArr[msgsArr.length - 1];
const tail = typeof lastMsg?.content === "string" ? lastMsg.content : JSON.stringify(lastMsg?.content ?? "");
const rule = rules.find((r) => r.when !== undefined && tail.includes(r.when));
if (rule) {
res.writeHead(rule.status ?? 500, { "content-type": "application/json" });
res.end(rule.body ?? "");
return;
}
res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" });
res.write(`data: ${JSON.stringify({ id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: { role: "assistant", content: "ok" } }] })}\n\n`);
const done: Record<string, unknown> = { id: "c1", object: "chat.completion.chunk", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] };
if (reportUsage) done.usage = { prompt_tokens: ANCHOR_INPUT, completion_tokens: 3 };
res.write(`data: ${JSON.stringify(done)}\n\n`);
res.write("data: [DONE]\n\n");
res.end();
});
});
return { server, received, nonStreamCalls: () => nonStream };
}

function lastNudgeSized(logs: string[], sessionId: string): number {
const re = new RegExp(`\\[${sessionId}\\] nudge .*?usage=\\d+% \\((\\d+)/`, "g");
let m: RegExpExecArray | null;
let last = -1;
while ((m = re.exec(logs.join("\n"))) !== null) last = Number(m[1]);
return last;
}

function streamedBodies(received: Buffer[]): string[] {
return received.map((b) => b.toString("utf8")).filter((t) => {
try {
return (JSON.parse(t) as Record<string, unknown>).stream === true;
} catch {
return false;
}
});
}

async function startHarness(script: VerdictRule[], reportUsage: boolean) {
const logs: string[] = [];
setLogCapture((level, msg) => { logs.push(`${level} ${msg}`); });
_resetSessionsForTest();
const relay = makeRelay(script, reportUsage);
relay.server.listen(0, "127.0.0.1");
await once(relay.server, "listening");
const upstreamPort = (relay.server.address() as { port: number }).port;
_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}`]: { models: { "gpt-test": { context: WINDOW } } } },
modelContextLimit: WINDOW,
kernelConfig: defaultConfig(WINDOW),
compress: { injectTool: true, injectNudge: true },
promptCache: { routing: "auto" },
sessionHeader: "x-acp-session",
log: true,
debug: false,
passthrough: false,
compat: { roles: {} }, streamErrorShape: "protocol", passthroughSource: null, autoRestartOnUpdate: false, updateTag: "latest", advisoryCheck: false, releaseNotesCheck: false,
autoUpdate: false,
mitm: { enabled: false, domains: [] },
} as ProxyOptions);
await once(proxy, "listening");
const url = `http://127.0.0.1:${(proxy.address() as { port: number }).port}/bili/http://127.0.0.1:${upstreamPort}/v1/chat/completions`;
// NO identity headers — genuine anonymous client; prefix affinity resolves
// the pfa-* session (#309/#553 lane).
const post = (messages: Msg[]) => fetch(url, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ model: "gpt-test", max_tokens: 1024, stream: true, messages }),
});
const anonSession = (): Session => {
const s = listSessions().find((x) => x.metadata.anonymousPrefixAffinity);
assert.ok(s, "an anonymous prefix-affinity session exists");
return s!;
};
const close = async () => {
await new Promise<void>((r) => proxy.close(() => r()));
await new Promise<void>((r) => relay.server.close(() => r()));
};
return { logs, relay, post, anonSession, close };
}

test("#2033 A1: anchored anonymous session keeps sizing on the usage anchor after a failed turn (e2e)", async () => {
const h = await startHarness([
{ when: "step two?", status: 503, body: '{"error":"upstream unavailable"}' },
], true);
try {
const base = baseConversation();
const r1 = await h.post(base);
assert.equal(r1.status, 200, "turn 1 succeeds");
await r1.text();
let s = h.anonSession();
assert.ok(s.id.startsWith("pfa-"), `anonymous session resolved by prefix affinity (${s.id})`);
assert.equal(s.stats.lastUsageGradeTokens, ANCHOR_INPUT, "real usage report anchored the session");
assert.equal(s.stats.lastInputTokensSource, "usage");

const msgs2: Msg[] = [...base, { role: "assistant", content: "ok, done." }, { role: "user", content: "step two?" }];
const r2 = await h.post(msgs2);
assert.equal(r2.status, 503, "upstream 503 passes through");
await r2.text();
s = h.anonSession();
// The #604 arm IS still written — load-bearing for the emergency-band
// rescue on anchor-less sessions — but it carries ESTIMATE grade and
// must not reach a sizing decision while a usage-grade anchor exists.
assert.ok(s.stats.lastInputTokens > ANCHOR_INPUT, `failure arm raised the baseline to an outbound estimate (${s.stats.lastInputTokens})`);
assert.equal(s.stats.lastInputTokensSource, "estimate", "the arm is tagged estimate-grade");
assert.ok(h.logs.some((l) => l.includes("armed emergency shrink with local estimate")), "armFailureShrink warn present");

const msgs3: Msg[] = [...msgs2, { role: "assistant", content: "working." }, { role: "user", content: "step three?" }];
const r3 = await h.post(msgs3);
assert.equal(r3.status, 200, "turn 3 succeeds");
await r3.text();
s = h.anonSession();
const sized = lastNudgeSized(h.logs, s.id);
assert.ok(sized >= 0, "nudge diagnostic present");
// Pre-fix this returned the char-count upper bound of the FULL
// resubmitted history (58701 here, measured pre-fix): the anonymous early return ran
// before the usage-grade anchor check, the exact ghost path #1839
// closed for explicit sessions.
assert.equal(sized, ANCHOR_INPUT, "anchored anonymous session sizes on the last real usage report, not the raw-history bound");
assert.equal(s.stats.contextTokensSource, "usage", "display grade follows the anchor, not the estimate");
assert.equal(h.relay.nonStreamCalls(), 0, "no preflight summary calls");
const streamed = streamedBodies(h.relay.received);
assert.ok(!streamed[2].includes(NUDGE_MARKER), "no spurious nudge injection on the healthy turn");
} finally {
await h.close();
}
});

test("#2033 A2: anchor-less anonymous session keeps the raw-history fallback (nudge not blinded, #553/#728)", async () => {
const h = await startHarness([], false); // never-reporting upstream
try {
const base = baseConversation();
const r1 = await h.post(base);
assert.equal(r1.status, 200, "turn 1 succeeds");
await r1.text();
const msgs2: Msg[] = [...base, { role: "assistant", content: "ok, done." }, { role: "user", content: "step two?" }];
const r2 = await h.post(msgs2);
assert.equal(r2.status, 200, "turn 2 succeeds");
await r2.text();
const s = h.anonSession();
assert.equal(s.stats.lastUsageGradeTokens, undefined, "never-reporting upstream wrote no anchor");
const sized = lastNudgeSized(h.logs, s.id);
assert.ok(sized > 0, `raw-history fallback kept the nudge measurable (got ${sized}; 0 would blind it — #553)`);
assert.equal(s.stats.contextTokensSource, "estimate", "fallback stays estimate-grade");
assert.equal(h.relay.nonStreamCalls(), 0, "no preflight summary calls");
} finally {
await h.close();
}
});
Loading