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
176 changes: 170 additions & 6 deletions CONFIGURATION.md

Large diffs are not rendered by default.

174 changes: 169 additions & 5 deletions CONFIGURATION.zh-CN.md

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,8 @@ type FileConfig = {
maxShrinkPerCompress?: number;
/** Outbound proxy keep-alive ceiling, 0 for one-shot (was BILI_PROXY_KEEPALIVE_MAX_MS; default 55000). */
proxyKeepAliveMaxMs?: number;
/** Post-response close linger budget (#1982; was env-only BILI_POST_RESPONSE_LINGER_MS; default 5000). */
postResponseLingerMs?: number;
};
/** Session persistence knobs (#2030) — was BILI_PERSIST_* env-only. */
persist?: {
Expand Down Expand Up @@ -1445,13 +1447,14 @@ const KNOWN_TOP_LEVEL_KEYS = new Set([
"port", "host", "upstream", "providersPath", "providers", "proxy",
"modelContextLimit", "sessionHeader", "log", "debug", "dumpSse",
"passthrough", "autoUpdate", "autoRestartOnUpdate", "updateTag",
"advisoryCheck", "advisoryUrl", "upstreamProxy", "upstreamProxyMode",
"advisoryCheck", "advisoryUrl", "releaseNotesCheck", "releaseNotesUrl",
"upstreamProxy", "upstreamProxyMode",
"logFile", "compress", "promptCache", "mitm", "maskHosts",
"subagentSplit", "forkAdoption", "resumeInheritance",
"chainContentDetection", "chainEgressStamp", "stableSystemAnchor",
"compat", "imageBilling", "claude", "native", "resign",
"compat", "imageBilling", "imageTokenCap", "claude", "native", "resign",
// #2030 subsystem blocks:
"network", "persist", "sessions", "update", "diagnostics",
"network", "persist", "sessions", "plugin", "update", "diagnostics",
"fakeCompletion", "codexCompact", "ccrRetrievalTtlMs", "decompressTmpCap",
]);

Expand Down
12 changes: 12 additions & 0 deletions src/knobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,18 @@ export function proxyKeepAliveMaxMs(): number {
: Number.isFinite(v) && v > 0 ? Math.floor(v) : PROXY_KEEPALIVE_MAX_MS;
}

export const POST_RESPONSE_LINGER_MS_DEFAULT = 5_000;

/** BILI_POST_RESPONSE_LINGER_MS > network.postResponseLingerMs > 5000 (#1982
* post-response close linger budget; mirrors nginx lingering_time). Env tier
* preserves the historical parseInt parsing byte-exact: set (even empty/
* garbage) wins and non-numeric or non-positive values fall back to the
* default without consulting the file tier. */
export function postResponseLingerMs(): number {
const v = tInt("BILI_POST_RESPONSE_LINGER_MS", fileNetwork().postResponseLingerMs);
return Number.isFinite(v) && v > 0 ? v : POST_RESPONSE_LINGER_MS_DEFAULT;
}

export const MITM_HANDSHAKE_TIMEOUT_MS_DEFAULT = 10_000;

/** BILI_MITM_HANDSHAKE_TIMEOUT_MS > mitm.handshakeTimeoutMs > 10000. */
Expand Down
18 changes: 4 additions & 14 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ import {
keepResponseId as knobKeepResponseId,
noCompressPrompt as knobNoCompressPrompt,
noInjectTool as knobNoInjectTool,
postResponseLingerMs as knobPostResponseLingerMs,
preflightDeadEndCooldownMs as knobPreflightDeadEndCooldownMs,
preflightHoldGraceMs as knobPreflightHoldGraceMs,
rawDumpDir as knobRawDumpDir,
Expand Down Expand Up @@ -386,17 +387,6 @@ export function googleModelFromPath(urlPath: string): string | undefined {
}


// #1982: budget for the post-response close linger (see installPostResponseLinger).
// Default 5s mirrors nginx's lingering_time: a peer that FINs promptly costs one
// RTT of extra hold; a silent peer costs at most this window per fd.
// Env-overridable like BILI_MITM_HANDSHAKE_TIMEOUT_MS (tests + operator tuning);
// non-numeric or non-positive values fall back to the default.
const POST_RESPONSE_LINGER_MS_DEFAULT = 5_000;
function postResponseLingerMs(): number {
const v = Number.parseInt(process.env.BILI_POST_RESPONSE_LINGER_MS ?? "", 10);
return Number.isFinite(v) && v > 0 ? v : POST_RESPONSE_LINGER_MS_DEFAULT;
}

export async function startServer(opts: ProxyOptions): Promise<http.Server> {
// Configure the tee logger (file + stderr) BEFORE any logging so the very
// first line (persist status) lands in the file too.
Expand Down Expand Up @@ -601,7 +591,7 @@ export async function startServer(opts: ProxyOptions): Promise<http.Server> {
if (socket.destroyed) return;
if (why === "backstop") {
rec.lingerBackstopAt = performance.now();
log("warn", `[conn#${rec.id}] ${rec.kind} linger backstop: no peer close signal ${postResponseLingerMs()}ms after post-response close — destroying (peer may see RST/ECONNRESET)`);
log("warn", `[conn#${rec.id}] ${rec.kind} linger backstop: no peer close signal ${knobPostResponseLingerMs()}ms after post-response close — destroying (peer may see RST/ECONNRESET)`);
} else if (why === "peer-fin") {
log("debug", `[conn#${rec.id}] ${rec.kind} linger complete: peer close signal received — closing cleanly`);
}
Expand All @@ -619,15 +609,15 @@ export async function startServer(opts: ProxyOptions): Promise<http.Server> {
return origDestroy();
}
armed = true;
log("info", `[conn#${rec.id}] ${rec.kind} post-response close: lingering for peer close signal (budget ${postResponseLingerMs()}ms)`);
log("info", `[conn#${rec.id}] ${rec.kind} post-response close: lingering for peer close signal (budget ${knobPostResponseLingerMs()}ms)`);
// http leaves the socket paused between requests; without resume()
// the peer's EOF would never reach us and every linger would run
// out on the backstop. Draining also removes unread recv residual
// (the Linux RST trigger) across the whole window.
socket.resume();
socket.once("end", () => finishLinger("peer-fin"));
socket.once("error", () => finishLinger("error"));
backstopTimer = setTimeout(() => finishLinger("backstop"), postResponseLingerMs());
backstopTimer = setTimeout(() => finishLinger("backstop"), knobPostResponseLingerMs());
backstopTimer.unref?.();
return socket;
};
Expand Down
14 changes: 10 additions & 4 deletions tests/issue1815-unknown-top-level-keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ test("#1815: multiple unknown keys list together; fixing some re-warns about the
}
});

test("#1815: the full documented top-level surface produces no warning", () => {
test("#1815: the full documented top-level surface produces no warning (#2062)", () => {
const { captured, stop } = captureLogs();
try {
withConfigFile(
Expand All @@ -112,12 +112,18 @@ test("#1815: the full documented top-level surface produces no warning", () => {
providersPath: "./providers.json", providers: {}, proxy: "http://127.0.0.1:7890",
modelContextLimit: 200000, sessionHeader: true, log: true, debug: true, dumpSse: false,
passthrough: {}, autoUpdate: true, autoRestartOnUpdate: true, updateTag: "latest",
advisoryCheck: false, advisoryUrl: "", upstreamProxy: "", upstreamProxyMode: "direct",
advisoryCheck: false, advisoryUrl: "", releaseNotesCheck: false, releaseNotesUrl: "",
upstreamProxy: "", upstreamProxyMode: "direct",
logFile: "", compress: { promptPack: "lean" }, promptCache: {}, mitm: {}, maskHosts: true,
subagentSplit: true, forkAdoption: false, resumeInheritance: true,
chainContentDetection: true, chainEgressStamp: false, stableSystemAnchor: false,
compat: {}, imageBilling: "auto",
claude: { nativePort: 8901 }, native: { attachExternal: false },
compat: {}, imageBilling: "auto", imageTokenCap: 4096,
resign: {}, claude: { nativePort: 8901 }, native: { attachExternal: false },
network: { upstreamTimeoutMs: 60000 }, persist: { enabled: true },
sessions: { max: 64 }, plugin: { snapshotCapBytes: 1048576 },
update: { checkIntervalMs: 180000 }, diagnostics: { dumpBody: false },
fakeCompletion: { retries: 0 }, codexCompact: "intercept",
ccrRetrievalTtlMs: 600000, decompressTmpCap: 50,
}),
() => {
loadRoutes();
Expand Down
19 changes: 18 additions & 1 deletion tests/knobs-config-precedence.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ test("defaults: no env, no file", () => {
assert.equal(knobs.preflightHoldGraceMs(), 30_000);
assert.equal(knobs.preflightDeadEndCooldownMs(), 5 * 60_000);
assert.equal(knobs.proxyKeepAliveMaxMs(), PROXY_KEEPALIVE_MAX_MS);
assert.equal(knobs.postResponseLingerMs(), 5_000);
assert.equal(knobs.mitmHandshakeTimeoutMs(), 10_000);
assert.equal(knobs.persistEnabled(), true);
assert.equal(knobs.persistZstdEnabled(), false);
Expand Down Expand Up @@ -97,6 +98,7 @@ test("file tier: every migrated block resolves from the config file", () => {
clientErrorBackstopMs: 45000, exposureLogIntervalMs: 0, streamKeepAliveMs: 20000,
preflightHoldMs: 40000, preflightDeadEndCooldownMs: 60000, replayRetryMax: 5,
replayRetryBaseMs: 250, maxShrinkPerCompress: 0.4, proxyKeepAliveMaxMs: 60000,
postResponseLingerMs: 12345,
},
persist: { enabled: false, zstd: true, debounceMs: 900, tailTokens: 4096, epermAlertThreshold: 9, epermAlertRepeatMs: 1200 },
sessions: { max: 64, gc: { enabled: true, maxAgeDays: 3, maxTokens: 2000000, intervalMs: 60000 } },
Expand Down Expand Up @@ -127,6 +129,7 @@ test("file tier: every migrated block resolves from the config file", () => {
assert.equal(knobs.replayBaseDelayMs(), 250);
assert.equal(knobs.maxShrinkPerCompress(), 0.4);
assert.equal(knobs.proxyKeepAliveMaxMs(), 60000);
assert.equal(knobs.postResponseLingerMs(), 12345);
assert.equal(knobs.mitmHandshakeTimeoutMs(), 1234);
assert.equal(knobs.persistEnabled(), false);
assert.equal(knobs.persistZstdEnabled(), true);
Expand Down Expand Up @@ -162,16 +165,18 @@ test("file tier: every migrated block resolves from the config file", () => {
});

test("env tier always wins over the file (live test seams)", () => {
setConfig({ network: { upstreamTimeoutMs: 60000, replayRetryMax: 5 }, persist: { enabled: false }, diagnostics: { renderNone: true, compressProtocol: "text" } });
setConfig({ network: { upstreamTimeoutMs: 60000, replayRetryMax: 5, postResponseLingerMs: 12345 }, persist: { enabled: false }, diagnostics: { renderNone: true, compressProtocol: "text" } });
withEnv({
BILI_UPSTREAM_TIMEOUT_MS: "999",
BILI_REPLAY_RETRY_MAX: "2",
BILI_POST_RESPONSE_LINGER_MS: "777",
BILI_PERSIST: "1",
ACP_RENDER_NONE: "",
ACP_COMPRESS_PROTOCOL: "tools",
}, () => {
assert.equal(knobs.upstreamTimeoutMs(), 999);
assert.equal(knobs.replayMaxAttempts(), 2);
assert.equal(knobs.postResponseLingerMs(), 777);
// persistEnabled env tier: only "0"/"false" disable; "1" forces on over file false
assert.equal(knobs.persistEnabled(), true);
// a SET (even empty) env var owns the knob: historical truthy check
Expand Down Expand Up @@ -216,6 +221,18 @@ test("set env owns the knob: garbage env resolves exactly as pre-migration", ()
});
});

test("post-response linger: strict parseInt tier — set-but-empty/garbage env never consults the file (#1982)", () => {
setConfig({ network: { postResponseLingerMs: 12345 } });
withEnv({ BILI_POST_RESPONSE_LINGER_MS: "junk" }, () => {
assert.equal(knobs.postResponseLingerMs(), 5_000);
});
// pre-migration parser was Number.parseInt(env ?? ""): an EMPTY export parsed
// to NaN → default, unlike the tIntLoose family where empty means unset
withEnv({ BILI_POST_RESPONSE_LINGER_MS: "" }, () => {
assert.equal(knobs.postResponseLingerMs(), 5_000);
});
});

test("garbage file values fall back to defaults, never throw", () => {
setConfig({ network: { upstreamTimeoutMs: "fast", replayRetryMax: -3 }, persist: { tailTokens: null }, ccrRetrievalTtlMs: "whenever", plugin: { snapshotCapBytes: "huge" } });
assert.equal(knobs.upstreamTimeoutMs(), UPSTREAM_TIMEOUT_MS);
Expand Down
Loading