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
49 changes: 40 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1488,17 +1488,48 @@ export function warnUnknownTopLevelKeys(obj: Record<string, unknown>): void {
loggerLog("warn", `[acp-config] ignoring unknown top-level config key(s): ${unknown.join(", ")}${hint}`);
}

/** Read the JSON config file fresh on every call (no cache): the file is small,
* hot-reloadable (web-UI Apply rewrites it), and test seams mutate it between
* calls. Exported so src/knobs.ts can resolve the file tier of every knob
* through this single reader (#2030). */
// #2078: parse-once cache for loadConfigFile(). Every knob resolves its file
// tier through this reader per call (~5-10 calls per request once #2030
// landed), and each call used to do a full readFileSync + JSON.parse. The raw
// file TEXT is the invalidation key, deliberately NOT (mtime, size): back-to-
// back rewrites can land on the same sub-ms mtime on common filesystems
// (measured), so a stat-keyed cache serves stale values right after a web-UI
// Apply or a test-seam write. Re-reading the text per call is a page-cache hit
// (~µs); JSON.parse is the expensive half and now runs only when content
// actually changed. Hot-reload stays exact: any external rewrite is visible on
// the next call; a missing file is never cached (reappearing files show up
// immediately). Callers treat the returned object as READ-ONLY (shared across
// calls until the content changes) — audited: all current consumers only read
// fields.
let configFileCache: { raw: string; value: FileConfig } | null = null;

export function loadConfigFile(): FileConfig {
const parsed = safeReadJson(configFile());
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
warnUnknownTopLevelKeys(parsed as Record<string, unknown>);
return parsed as FileConfig;
const path = configFile();
let raw: string | undefined;
let value: FileConfig | undefined;
try {
raw = readFileSync(path, "utf8").replace(/^\uFEFF/, "");
const cached = configFileCache;
if (cached && cached.raw === raw) return cached.value;
const parsed: unknown = JSON.parse(raw);
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
warnUnknownTopLevelKeys(parsed as Record<string, unknown>);
value = parsed as FileConfig;
} else {
value = {};
}
} catch (e) {
// Same surface as safeReadJson: silent on ENOENT, loud otherwise.
// Corrupt-but-present content IS cached (as {}) so one bad save does
// not re-log and re-fail-parse on every knob call until it is fixed.
if ((e as NodeJS.ErrnoException).code !== "ENOENT") {
loggerLog("error", `[acp-config] failed to parse ${path}: ${String(e)}`);
}
if (raw !== undefined) value = {};
}
return {};
if (raw === undefined || value === undefined) return {};
configFileCache = { raw, value };
return value;
}

/** File shape of ONE scheme's `resign` block (see FileConfig.resign). */
Expand Down
10 changes: 7 additions & 3 deletions src/knobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
// bootstrap variable, or host-side posture read in a foreign process — and
// the read site must say so in a comment.
//
// Resolution happens on every call (deliberately uncached): the config file
// is small, hot-reloadable (web-UI Apply rewrites it), and tests mutate both
// process.env and the file between calls.
// Resolution happens on every call: the ENV tier is read live (deployments and
// test seams set these after import), while the FILE tier goes through
// loadConfigFile()'s content-keyed parse cache (#2078 — the old
// deliberately-uncached readFileSync+JSON.parse per call cost ~5-10 parses per
// request). The raw file text is the invalidation key, so any rewrite is
// visible on the next call — hot-reload (web-UI Apply) and file-mutating test
// seams keep working unchanged.

import path from "node:path";
import { stateDir } from "./paths.js";
Expand Down
55 changes: 43 additions & 12 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5575,19 +5575,40 @@ function outboundPayloadBreakdown(
return { textEstimate, overheadEstimate, imageTokens, payloadEstimate: textEstimate + overheadEstimate + imageTokens, armEstimate };
}

function outboundContextEstimate(prepared: Prepared, wireBody: string, opts: ProxyOptions, upstream: string): number {
/** #2078: `parsed` carries the caller's pre-parsed send body so forward() does
* not pay a second full JSON.parse of the largest payload in flight per
* request. Three states: undefined = parse here (legacy callers), object =
* project from it (numbers identical to re-parsing the same string), null =
* caller already tried and failed → keep the prepared projection and skip the
* doomed re-parses inside the helpers (they return 0 on unparseable input). */
export function outboundContextEstimate(
prepared: Prepared,
wireBody: string,
opts: ProxyOptions,
upstream: string,
parsed?: Record<string, unknown> | null,
): number {
let msgs = prepared.processedMessages.length > 0 ? prepared.processedMessages : prepared.originalMessages;
try {
const parsed = JSON.parse(wireBody);
const project = (value: unknown): void => {
switch (prepared.protocol) {
case "anthropic": msgs = anthropicToCore(parsed as AnthropicRequestBody).msgs; break;
case "openai": msgs = openaiToCore(parsed as OpenAIRequestBody).msgs; break;
case "responses": msgs = responsesToCore(parsed as ResponsesRequestBody).msgs; break;
case "google": msgs = googleToCore(parsed as GoogleRequestBody).msgs; break;
}
} catch { /* Preserve the prepared projection if the wire codec cannot project a provider extension. */ }
return estimateCoreMessagesUpper(msgs) + estimateWireOverhead(prepared.protocol, wireBody)
+ imageReserveFor(prepared.session, prepared.protocol, wireBody, opts, upstream);
case "anthropic": msgs = anthropicToCore(value as AnthropicRequestBody).msgs; break;
case "openai": msgs = openaiToCore(value as OpenAIRequestBody).msgs; break;
case "responses": msgs = responsesToCore(value as ResponsesRequestBody).msgs; break;
case "google": msgs = googleToCore(value as GoogleRequestBody).msgs; break;
}
};
let raw: string | Record<string, unknown>;
if (parsed === undefined) {
try { project(JSON.parse(wireBody)); } catch { /* Preserve the prepared projection if the wire codec cannot project a provider extension. */ }
raw = wireBody;
} else if (parsed !== null) {
try { project(parsed); } catch { /* Preserve the prepared projection if the wire codec cannot project a provider extension. */ }
raw = parsed;
} else {
raw = "";
}
return estimateCoreMessagesUpper(msgs) + estimateWireOverhead(prepared.protocol, raw)
+ imageReserveFor(prepared.session, prepared.protocol, raw, opts, upstream);
}

async function preflightCompressIfNeeded(
Expand Down Expand Up @@ -6305,8 +6326,18 @@ async function forward(
// not clobber the slot.
if (prepared?.session && !prepared.sidePassthrough && req.method !== "GET" && req.method !== "HEAD") {
const sentBody = typeof wireBody === "string" ? wireBody : wireBody.toString("utf8");
// #2078: ONE parse of the outbound body for the whole seam block — the
// estimate's projection, its wire-overhead term, and the image reserve
// each used to re-parse this same string independently.
let sentParsed: Record<string, unknown> | null;
try {
const p = JSON.parse(sentBody);
sentParsed = p && typeof p === "object" && !Array.isArray(p) ? (p as Record<string, unknown>) : null;
} catch {
sentParsed = null;
}
// Publish this send, not a historical usage baseline with a fresh timestamp.
const estimate = outboundContextEstimate(prepared, sentBody, opts, upstreamUrl);
const estimate = outboundContextEstimate(prepared, sentBody, opts, upstreamUrl, sentParsed);
prepared.session.stats.localInputEstimate = estimate;
prepared.session.stats.contextTokens = estimate;
prepared.session.stats.contextTokensSource = "estimate";
Expand Down
14 changes: 9 additions & 5 deletions src/server/budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,16 @@ export function projectThinkingMass(msgs: BiliMessage[], input: ThinkingMassInpu
* the preflight trigger fires ~10-20K late on agent clients with big tool
* manifests: text alone "fits" while the real billed input already overflows
* the window. Same term estimateInputTokens applies to the output clamp (#467). */
export function estimateWireOverhead(protocol: "anthropic" | "openai" | "responses" | "google", body: string | Buffer): number {
export function estimateWireOverhead(protocol: "anthropic" | "openai" | "responses" | "google", body: string | Buffer | Record<string, unknown>): number {
let parsed: Record<string, unknown>;
try {
parsed = JSON.parse(typeof body === "string" ? body : body.toString("utf8")) as Record<string, unknown>;
} catch {
return 0;
if (typeof body === "object" && !Buffer.isBuffer(body)) {
parsed = body;
} else {
try {
parsed = JSON.parse(typeof body === "string" ? body : body.toString("utf8")) as Record<string, unknown>;
} catch {
return 0;
}
}
const sysRaw = protocol === "responses"
? parsed.instructions
Expand Down
113 changes: 113 additions & 0 deletions tests/config-file-cache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import test from "node:test";

process.env.NODE_ENV = "test";

import { loadConfigFile } from "../src/config.ts";
import { setLogCapture } from "../src/logger.ts";

// #2078: loadConfigFile() feeds the file tier of every knob (#2030) — ~5-10
// calls per request used to each do readFileSync+JSON.parse. The cache keys on
// the RAW FILE TEXT (not mtime/size: back-to-back writes can share a sub-ms
// mtime, which would serve stale values right after a web-UI Apply), so any
// external rewrite is visible on the next call while unchanged content never
// re-parses. Explicit past Dates keep every step deterministic.

const T0 = new Date("2026-01-01T00:00:00Z");
const T1 = new Date("2026-01-02T00:00:00Z");
const T2 = new Date("2026-01-03T00:00:00Z");

const root = fs.mkdtempSync(path.join(os.tmpdir(), "bili-cfg-cache-"));
const savedConfigEnv = process.env.BILI_CONFIG_FILE;

test.after(() => {
setLogCapture(null);
if (savedConfigEnv === undefined) delete process.env.BILI_CONFIG_FILE;
else process.env.BILI_CONFIG_FILE = savedConfigEnv;
fs.rmSync(root, { recursive: true, force: true });
});

function pointAt(name: string): string {
const f = path.join(root, name);
process.env.BILI_CONFIG_FILE = f;
return f;
}

function writeAt(f: string, content: string, t: Date): void {
fs.writeFileSync(f, content);
fs.utimesSync(f, t, t);
}

test("#2078: unchanged file serves the same cached object (no re-read)", () => {
const f = pointAt("stable.json");
writeAt(f, JSON.stringify({ update: { checkIntervalMs: 111 } }), T0);
assert.deepEqual(loadConfigFile(), { update: { checkIntervalMs: 111 } });
assert.equal(loadConfigFile(), loadConfigFile());
});

test("#2078: hot reload — a real rewrite is visible on the next call", () => {
const f = pointAt("reload.json");
writeAt(f, JSON.stringify({ update: { checkIntervalMs: 111 } }), T0);
assert.equal(loadConfigFile().update?.checkIntervalMs, 111);
writeAt(f, JSON.stringify({ update: { checkIntervalMs: 222 } }), T1);
assert.equal(loadConfigFile().update?.checkIntervalMs, 222);
});

test("#2078: same-size rewrite with restored mtime is still visible (content is truth)", () => {
const f = pointAt("stale.json");
writeAt(f, JSON.stringify({ update: { checkIntervalMs: 111 } }), T0);
assert.equal(loadConfigFile().update?.checkIntervalMs, 111);
// Same byte length, mtime forced back to T0. A stat-keyed cache would serve
// the stale value here — the exact failure mode measured on real FSes
// (back-to-back writes sharing one sub-ms mtime). Content keying sees 222.
writeAt(f, JSON.stringify({ update: { checkIntervalMs: 222 } }), T0);
assert.equal(loadConfigFile().update?.checkIntervalMs, 222, "changed content must be visible even with identical mtime+size");
});

test("#2078: identical-content rewrite does not re-parse (same object reference)", () => {
const f = pointAt("identical.json");
const content = JSON.stringify({ update: { checkIntervalMs: 111 } });
writeAt(f, content, T0);
const first = loadConfigFile();
writeAt(f, content, T1); // cp -p / rsync --times style restoration
assert.equal(loadConfigFile(), first, "unchanged content serves the cached object");
});

test("#2078: missing file → {} without throwing", () => {
pointAt("never-created.json");
assert.deepEqual(loadConfigFile(), {});
});

test("#2078: malformed file logs once per change, not once per call", () => {
const f = pointAt("malformed.json");
const seen: string[] = [];
setLogCapture((_level, msg) => { if (msg.includes("failed to parse")) seen.push(msg); });
try {
writeAt(f, '{"update": oops', T0);
for (let i = 0; i < 5; i++) assert.deepEqual(loadConfigFile(), {});
assert.equal(seen.length, 1, `first corruption logs once across 5 calls (got ${seen.length})`);
writeAt(f, JSON.stringify({ update: { checkIntervalMs: 9 } }), T1);
assert.equal(loadConfigFile().update?.checkIntervalMs, 9);
loadConfigFile();
assert.equal(seen.length, 1, "valid rewrite logs nothing");
writeAt(f, '{"other": [', T2);
assert.deepEqual(loadConfigFile(), {});
loadConfigFile();
assert.equal(seen.length, 2, "second distinct corruption logs exactly once more");
} finally {
setLogCapture(null);
}
});

test("#2078: switching BILI_CONFIG_FILE re-resolves", () => {
const a = pointAt("a.json");
writeAt(a, JSON.stringify({ update: { checkIntervalMs: 1 } }), T0);
const b = pointAt("b.json");
writeAt(b, JSON.stringify({ update: { checkIntervalMs: 2 } }), T0);
assert.equal(loadConfigFile().update?.checkIntervalMs, 2);
process.env.BILI_CONFIG_FILE = a;
assert.equal(loadConfigFile().update?.checkIntervalMs, 1);
});
76 changes: 76 additions & 0 deletions tests/issue2078-outbound-estimate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import assert from "node:assert/strict";
import test from "node:test";

process.env.NODE_ENV = "test";

import { outboundContextEstimate } from "../src/server.ts";

// #2078: forward() now parses the sent wire body ONCE and hands the object to
// outboundContextEstimate instead of letting it re-parse the largest payload
// per request. The three input modes must be numerically identical:
// undefined → legacy self-parse (any future caller without a pre-parsed body)
// object → project straight from the parse-once result
// null → parse failed upstream; helpers receive "" so they yield the
// same zeros the legacy in-function catch used to produce
// Fakes stay minimal: empty message views + no images, so the estimate reduces
// to projection + overhead + image-reserve(0) and no Session internals are
// touched (imageReserveFor early-returns before session access).

type PreparedLike = Parameters<typeof outboundContextEstimate>[0];
type OptsLike = Parameters<typeof outboundContextEstimate>[2];
type Proto = Parameters<typeof outboundContextEstimate>[0]["protocol"];

const OPTS = { routes: {} } as unknown as OptsLike;
const UPSTREAM = "https://upstream.example/v1";

function makePrepared(protocol: Proto): PreparedLike {
return {
body: "",
session: { id: "t-2078" },
processedMessages: [],
originalMessages: [],
protocol,
stream: false,
compressInjected: false,
} as unknown as PreparedLike;
}

const FIXTURES: Array<[Proto, Record<string, unknown>]> = [
["anthropic", { model: "m", max_tokens: 100, system: "S".repeat(4000), messages: [{ role: "user", content: [{ type: "text", text: "hello world" }] }], tools: [{ name: "t", description: "D".repeat(2000), input_schema: {} }] }],
["openai", { model: "m", messages: [{ role: "system", content: "SYS ".repeat(300) }, { role: "user", content: "hello world" }], tools: [{ type: "function", function: { name: "t", description: "D".repeat(2000) } }] }],
["responses", { model: "m", instructions: "I".repeat(4000), input: [{ type: "message", role: "user", content: "hello world" }], tools: [{ type: "function", name: "t", description: "D".repeat(2000) }] }],
["google", { model: "m", systemInstruction: { parts: [{ text: "G".repeat(4000) }] }, contents: [{ role: "user", parts: [{ text: "hello world" }] }] }],
];

test("#2078: pre-parsed object ≡ legacy self-parse, all protocols, codec path live", () => {
for (const [protocol, obj] of FIXTURES) {
const s = JSON.stringify(obj);
const prepared = makePrepared(protocol);
const legacy = outboundContextEstimate(prepared, s, OPTS, UPSTREAM);
const preparsed = outboundContextEstimate(prepared, s, OPTS, UPSTREAM, obj);
assert.ok(legacy > 0, `${protocol}: estimate path must be live (got ${legacy})`);
assert.equal(preparsed, legacy, `${protocol}: pre-parsed mode must equal legacy mode`);
}
});

test("#2078: unparseable body — null mode ≡ legacy fallback (both zero here)", () => {
const prepared = makePrepared("openai");
const legacy = outboundContextEstimate(prepared, "{not json", OPTS, UPSTREAM);
const nullMode = outboundContextEstimate(prepared, "{not json", OPTS, UPSTREAM, null);
assert.equal(nullMode, legacy);
// Empty message views + unprojectable body: projection 0, overhead 0, no images.
assert.equal(legacy, 0);
});

test("#2078: estimate tracks wire-body content in BOTH modes (projection not dead)", () => {
const small = { model: "m", max_tokens: 100, system: "S".repeat(100), messages: [{ role: "user", content: [{ type: "text", text: "hi" }] }] };
const big = { ...small, system: "S".repeat(9000) };
const ss = JSON.stringify(small);
const sb = JSON.stringify(big);
const ps = makePrepared("anthropic");
const pb = makePrepared("anthropic");
const legacyDelta = outboundContextEstimate(pb, sb, OPTS, UPSTREAM) - outboundContextEstimate(ps, ss, OPTS, UPSTREAM);
const preparsedDelta = outboundContextEstimate(pb, sb, OPTS, UPSTREAM, big) - outboundContextEstimate(ps, ss, OPTS, UPSTREAM, small);
assert.ok(legacyDelta > 500, `legacy mode must see the bigger system (delta ${legacyDelta})`);
assert.ok(preparsedDelta > 500, `pre-parsed mode must see the bigger system (delta ${preparsedDelta})`);
});
Loading
Loading