mirror of
https://github.com/ranxianglei/billion-context.git
synced 2026-11-21 17:37:42 +08:00
Merge pull request #7412 from ranxianglei/batch/afloat16-b6-2026-11-20
[PR] batch-6: 4 low-risk community fixes (verbatim --no-ff merges, authorship preserved)
This commit is contained in:
+24
-4
@@ -1,5 +1,6 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { DUMP_PLAINTEXT_BANNER } from "./dump-banner.js";
|
||||
import { dumpsDir } from "./paths.js";
|
||||
import { log as loggerLog } from "./logger.js";
|
||||
@@ -57,7 +58,28 @@ function capText(text: string, budget: number, escaped = false): string {
|
||||
return prefix + marker(text.length - prefix.length);
|
||||
}
|
||||
|
||||
// Timestamps and sanitized session ids are descriptive, not unique: multiple
|
||||
// failures can arrive in one millisecond, including from different processes.
|
||||
// Each incident owns a fresh path; exclusive creation also prevents replacing
|
||||
// existing evidence if that path is already present.
|
||||
function writeIncident(dir: string, prefix: string, sid: string, status: number, text: string): string {
|
||||
const stamp = Date.now();
|
||||
const out = path.join(dir, `${prefix}-${stamp}-${sid}-${status}.json`);
|
||||
try {
|
||||
fs.writeFileSync(out, text, { flag: "wx" });
|
||||
return out;
|
||||
} catch (err) {
|
||||
if ((err as NodeJS.ErrnoException).code !== "EEXIST") throw err;
|
||||
}
|
||||
// Keep the historical name when it is free. On a collision, bound the
|
||||
// descriptive session portion so the unique suffix still fits a filename.
|
||||
const unique = path.join(dir, `${prefix}-${stamp}-${sid.slice(0, 100)}-${status}-${randomUUID()}.json`);
|
||||
fs.writeFileSync(unique, text, { flag: "wx" });
|
||||
return unique;
|
||||
}
|
||||
|
||||
/** Write the rejected forwarded body to `<dumpDir>/err-<ts>-<sid>-<status>.json`
|
||||
* (with a unique suffix on collision)
|
||||
* when BILI_DUMP_4XX=1. Returns the file path, or null when disabled/skipped/failed. */
|
||||
export function dumpRejectedBody(status: number, sessionId: string, body: string | Buffer): string | null {
|
||||
if (!knobDump4xxEnabled()) return null;
|
||||
@@ -73,8 +95,7 @@ export function dumpRejectedBody(status: number, sessionId: string, body: string
|
||||
const dir = dumpsDir();
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
const sid = sessionId.replace(/[^a-zA-Z0-9_-]/g, "_");
|
||||
const out = path.join(dir, `err-${Date.now()}-${sid}-${status}.json`);
|
||||
fs.writeFileSync(out, `${DUMP_PLAINTEXT_BANNER}\n${text}`);
|
||||
const out = writeIncident(dir, "err", sid, status, `${DUMP_PLAINTEXT_BANNER}\n${text}`);
|
||||
loggerLog("info", `[dump] upstream ${status} rejected body written to ${out}`);
|
||||
gcDumpDirIfConfigured(dir);
|
||||
return out;
|
||||
@@ -117,8 +138,7 @@ export function dumpSummaryRejection(status: number, sessionId: string, request:
|
||||
const dir = dumpsDir();
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
const sid = sessionId.replace(/[^a-zA-Z0-9_-]/g, "_");
|
||||
const out = path.join(dir, `summary-err-${Date.now()}-${sid}-${status}.json`);
|
||||
fs.writeFileSync(out, `${DUMP_PLAINTEXT_BANNER}\n${JSON.stringify(envelope, null, 2)}`);
|
||||
const out = writeIncident(dir, "summary-err", sid, status, `${DUMP_PLAINTEXT_BANNER}\n${JSON.stringify(envelope, null, 2)}`);
|
||||
loggerLog("info", `[dump] upstream ${status} rejected summary exchange written to ${out}`);
|
||||
gcDumpDirIfConfigured(dir);
|
||||
return out;
|
||||
|
||||
+26
-9
@@ -113,13 +113,15 @@ export function maskUrlForLog(url: string): string {
|
||||
// carried their inline credentials verbatim into logs / diagnostics / publish
|
||||
// blobs. No leading \b on purpose: a word-char-glued URL (xhttps://…) must
|
||||
// stay masked exactly as before; over-matching is safe, under-matching leaks.
|
||||
const EMBEDDED_URL_RE = /[a-z][a-z0-9+.-]*:\/\/[^\s"'<>]+/gi;
|
||||
// Try each scheme-character run once; retain any non-letter prefix outside
|
||||
// the URL rather than retrying the same suffix from every letter in the run.
|
||||
const EMBEDDED_URL_RE = /(?<![a-z0-9+.-])([0-9+.-]*)([a-z][a-z0-9+.-]*:\/\/[^\s"'<>]+)/gi;
|
||||
|
||||
/** Mask every scheme://… URL embedded anywhere in an arbitrary string
|
||||
* (request paths like /bili/http://relay.internal/v1/..., error text,
|
||||
* socks5://user:pass@host forwarding addresses — #4191). */
|
||||
export function maskUrlsInText(text: string): string {
|
||||
return text.replace(EMBEDDED_URL_RE, (m) => maskUrlForLog(m));
|
||||
return text.replace(EMBEDDED_URL_RE, (_m, prefix: string, url: string) => prefix + maskUrlForLog(url));
|
||||
}
|
||||
|
||||
/** #3811: publish-path URL masking for artifacts that land in a PUBLIC GitHub
|
||||
@@ -145,7 +147,7 @@ function maskUrlForPublish(url: string): string {
|
||||
* wherever they appear in serialized text — object values AND object keys (the
|
||||
* providers table uses upstream URLs as keys). */
|
||||
export function maskUrlsForPublish(text: string): string {
|
||||
return text.replace(EMBEDDED_URL_RE, (m) => maskUrlForPublish(m));
|
||||
return text.replace(EMBEDDED_URL_RE, (_m, prefix: string, url: string) => prefix + maskUrlForPublish(url));
|
||||
}
|
||||
|
||||
function escapeRegExp(s: string): string {
|
||||
@@ -230,10 +232,10 @@ export function maskUrlForDiagnostics(url: string): string {
|
||||
* unparseable fragments pass through (the IP scrubber neutralizes bare IP
|
||||
* literals separately). */
|
||||
export function maskUrlsForDiagnostics(text: string): string {
|
||||
return text.replace(EMBEDDED_URL_RE, (m) => {
|
||||
return text.replace(EMBEDDED_URL_RE, (m, prefix: string, url: string) => {
|
||||
try {
|
||||
void new URL(m);
|
||||
return maskUrlForDiagnostics(m);
|
||||
void new URL(url);
|
||||
return prefix + maskUrlForDiagnostics(url);
|
||||
} catch {
|
||||
return m;
|
||||
}
|
||||
@@ -292,7 +294,20 @@ function isIpv4Literal(s: string): boolean {
|
||||
}
|
||||
|
||||
function isLoopbackIp(ip: string): boolean {
|
||||
return ip === "::1" || ip === "0:0:0:0:0:0:0:1" || ip === "0.0.0.0" || ip.startsWith("127.");
|
||||
if (ip === "::1" || ip === "0:0:0:0:0:0:0:1" || ip === "0.0.0.0") return true;
|
||||
if (!ip.startsWith("127.")) return false;
|
||||
if (isIpv4Literal(ip)) return true;
|
||||
// The prefix alone also matches private DNS names such as
|
||||
// 127.relay.internal. Preserve numeric aliases accepted by Node URLs
|
||||
// (127.1, 127.0x1, etc.) only when they normalize to an actual IPv4
|
||||
// loopback address; never interpret URL syntax as part of a host.
|
||||
if (!/^[0-9a-fx.]+$/i.test(ip)) return false;
|
||||
try {
|
||||
const normalized = new URL(`http://${ip}`).hostname;
|
||||
return normalized.startsWith("127.") && isIpv4Literal(normalized);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** #4254: is this a loopback host name/address? Loopback identifies a LOCAL
|
||||
@@ -310,8 +325,10 @@ export function isLoopbackHost(host: string): boolean {
|
||||
* is masked with its port kept. Used by the upstream-failure egress path so one
|
||||
* message never mixes a raw loopback next to a masked <private-host>. */
|
||||
export function maskHostPortForDisplay(target: string): string {
|
||||
const i = target.lastIndexOf(":");
|
||||
if (i <= 0) return isLoopbackHost(target) ? target : maskHostForLog(target);
|
||||
// A bracketed address can have no port (new URL("http://[::1]").host).
|
||||
// Only the colon after the closing bracket can delimit its port.
|
||||
const i = target.startsWith("[") ? target.indexOf("]") + 1 : target.lastIndexOf(":");
|
||||
if (i <= 0 || i === target.length) return isLoopbackHost(target) ? target : maskHostForLog(target);
|
||||
const host = target.slice(0, i);
|
||||
return `${isLoopbackHost(host) ? host : maskHostForLog(host)}${target.slice(i)}`;
|
||||
}
|
||||
|
||||
+5
-5
@@ -355,8 +355,8 @@ export function buildSessionReportSvg(d: ReportData, opts: ReportOpts = {}): Svg
|
||||
else {
|
||||
const CH = 150, PL = 52, PR = 14, PT = 8, PB = 20;
|
||||
const top = y + PT, bot = y + CH - PB, left = PAD + PL, right = W - PAD - PR;
|
||||
const vals = lines.map((l) => Number(l.input) || 0);
|
||||
let maxV = Math.max.apply(null, vals.concat(win ? [win] : []).concat(Number(d.systemPromptTokens) ? [Number(d.systemPromptTokens)] : []));
|
||||
let maxV = lines.reduce((max, l) => Math.max(max, Number(l.input) || 0), -Infinity);
|
||||
maxV = Math.max(maxV, win || -Infinity, Number(d.systemPromptTokens) || -Infinity);
|
||||
if (!(maxV > 0)) maxV = 1;
|
||||
const Xp = (i: number) => left + (lines.length <= 1 ? (right - left) / 2 : (i / (lines.length - 1)) * (right - left));
|
||||
const Yp = (v: number) => bot - (v / maxV) * (bot - top);
|
||||
@@ -511,9 +511,9 @@ export function buildSessionReportSvg(d: ReportData, opts: ReportOpts = {}): Svg
|
||||
}
|
||||
const wAll = uLines.filter((ln) => (ln.at || 0) >= gs && (ln.at || 0) <= geT + 120000).map((ln) => ln.input || 0);
|
||||
if (!wAll.length) return;
|
||||
const gPeakAll = Math.max(...wAll);
|
||||
const gPeak = Math.max(...wAll.filter((v) => v >= 0.25 * gPeakAll));
|
||||
const gBase = preMin !== null && preMin >= 0.25 * gPeak ? preMin : Math.min(...wAll.filter((v) => v >= 0.25 * gPeak));
|
||||
const gPeakAll = wAll.reduce((max, v) => Math.max(max, v), -Infinity);
|
||||
const gPeak = wAll.filter((v) => v >= 0.25 * gPeakAll).reduce((max, v) => Math.max(max, v), -Infinity);
|
||||
const gBase = preMin !== null && preMin >= 0.25 * gPeak ? preMin : wAll.filter((v) => v >= 0.25 * gPeak).reduce((min, v) => Math.min(min, v), Infinity);
|
||||
const gGrow = Math.max(0, gPeak - gBase);
|
||||
gPts.push({ at: geT, s: folded, g: gGrow, pct: gGrow > 0 ? Math.round(folded / gGrow * 1000) / 10 : null });
|
||||
gPrevEnd = geT;
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
import test, { type TestContext } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { dumpRejectedBody, dumpSummaryRejection } from "../src/error-dump.js";
|
||||
import { stripDumpBanner } from "../src/dump-banner.js";
|
||||
import { setLogCapture } from "../src/logger.js";
|
||||
import { rmrf } from "./tmp-rm.js";
|
||||
|
||||
const NOW = 1_700_000_000_000;
|
||||
|
||||
function setup(t: TestContext): string {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-incident-collision-"));
|
||||
const previousGate = process.env.BILI_DUMP_4XX;
|
||||
const previousDir = process.env.ACP_DUMP_DIR;
|
||||
process.env.BILI_DUMP_4XX = "1";
|
||||
process.env.ACP_DUMP_DIR = dir;
|
||||
setLogCapture(() => {});
|
||||
t.mock.method(Date, "now", () => NOW);
|
||||
t.after(async () => {
|
||||
if (previousGate === undefined) delete process.env.BILI_DUMP_4XX; else process.env.BILI_DUMP_4XX = previousGate;
|
||||
if (previousDir === undefined) delete process.env.ACP_DUMP_DIR; else process.env.ACP_DUMP_DIR = previousDir;
|
||||
setLogCapture(null);
|
||||
await rmrf(dir);
|
||||
});
|
||||
return dir;
|
||||
}
|
||||
|
||||
function contents(file: string): string {
|
||||
return stripDumpBanner(fs.readFileSync(file, "utf8"));
|
||||
}
|
||||
|
||||
test("same-millisecond rejected bodies preserve both incident snapshots", (t) => {
|
||||
const dir = setup(t);
|
||||
const first = dumpRejectedBody(400, "session", "first rejected body");
|
||||
assert.ok(first);
|
||||
const original = fs.readFileSync(first, "utf8");
|
||||
const second = dumpRejectedBody(400, "session", "second rejected body");
|
||||
assert.ok(second);
|
||||
assert.notEqual(second, first);
|
||||
assert.equal(fs.readFileSync(first, "utf8"), original);
|
||||
assert.equal(contents(first), "first rejected body");
|
||||
assert.equal(contents(second), "second rejected body");
|
||||
assert.equal(fs.readdirSync(dir).length, 2);
|
||||
});
|
||||
|
||||
test("same-millisecond summary failures keep both exchanges", (t) => {
|
||||
setup(t);
|
||||
const first = dumpSummaryRejection(413, "session", "request one", "response one");
|
||||
const second = dumpSummaryRejection(413, "session", "request two", "response two");
|
||||
assert.ok(first && second);
|
||||
assert.notEqual(first, second);
|
||||
assert.deepEqual(JSON.parse(contents(first)), { status: 413, request: "request one", response: "response one" });
|
||||
assert.deepEqual(JSON.parse(contents(second)), { status: 413, request: "request two", response: "response two" });
|
||||
});
|
||||
|
||||
for (const kind of ["main", "summary"] as const) {
|
||||
test(`${kind} dumps do not replace evidence left by another process`, (t) => {
|
||||
const dir = setup(t);
|
||||
const prefix = kind === "main" ? "err" : "summary-err";
|
||||
const existing = path.join(dir, `${prefix}-${NOW}-session-400.json`);
|
||||
fs.writeFileSync(existing, "existing incident evidence\n");
|
||||
const out = kind === "main"
|
||||
? dumpRejectedBody(400, "session", "new rejected body")
|
||||
: dumpSummaryRejection(400, "session", "new request", "new response");
|
||||
assert.ok(out);
|
||||
assert.notEqual(out, existing);
|
||||
assert.equal(fs.readFileSync(existing, "utf8"), "existing incident evidence\n");
|
||||
assert.equal(fs.readdirSync(dir).length, 2);
|
||||
});
|
||||
}
|
||||
|
||||
test("sanitized session-name collisions do not overwrite another session", (t) => {
|
||||
setup(t);
|
||||
const first = dumpRejectedBody(400, "team/session", "session with slash");
|
||||
const second = dumpRejectedBody(400, "team_session", "session with underscore");
|
||||
assert.ok(first && second);
|
||||
assert.notEqual(first, second);
|
||||
assert.equal(contents(first), "session with slash");
|
||||
assert.equal(contents(second), "session with underscore");
|
||||
});
|
||||
|
||||
test("a burst preserves each incident and its returned path", (t) => {
|
||||
const dir = setup(t);
|
||||
const files = Array.from({ length: 32 }, (_, i) => dumpRejectedBody(429, "burst", `request ${i}`));
|
||||
assert.equal(new Set(files).size, files.length);
|
||||
for (const [i, file] of files.entries()) {
|
||||
assert.ok(file);
|
||||
assert.equal(contents(file), `request ${i}`);
|
||||
}
|
||||
assert.equal(fs.readdirSync(dir).length, files.length);
|
||||
});
|
||||
|
||||
test("a collision suffix still fits when the existing session filename is long", (t) => {
|
||||
setup(t);
|
||||
const session = "s".repeat(200);
|
||||
const first = dumpSummaryRejection(400, session, "first request", "first response");
|
||||
const second = dumpSummaryRejection(400, session, "second request", "second response");
|
||||
assert.ok(first && second);
|
||||
assert.notEqual(first, second);
|
||||
assert.equal((JSON.parse(contents(first)) as { request: string }).request, "first request");
|
||||
assert.equal((JSON.parse(contents(second)) as { request: string }).request, "second request");
|
||||
});
|
||||
|
||||
test("a non-collision write failure stays fail-soft and is not retried", (t) => {
|
||||
const dir = setup(t);
|
||||
const original = fs.writeFileSync;
|
||||
let attempts = 0;
|
||||
t.mock.method(fs, "writeFileSync", (...args: Parameters<typeof fs.writeFileSync>) => {
|
||||
if (typeof args[0] === "string" && args[0].startsWith(dir + path.sep)) {
|
||||
attempts++;
|
||||
throw Object.assign(new Error("read-only fixture"), { code: "EROFS" });
|
||||
}
|
||||
return original(...args);
|
||||
});
|
||||
assert.equal(dumpRejectedBody(400, "session", "body"), null);
|
||||
assert.equal(attempts, 1);
|
||||
assert.deepEqual(fs.readdirSync(dir), []);
|
||||
});
|
||||
@@ -346,14 +346,8 @@ async function bringUp(h: Harness2) {
|
||||
return { proxy, origin: `http://127.0.0.1:${paddr.port}`, upstreamUrl };
|
||||
}
|
||||
|
||||
// The fill must NOT be one homogeneous character run: these requests mint a
|
||||
// FRESH ANONYMOUS session (no session header), whose provenance path masks the
|
||||
// ENTIRE first message through maskUrlsInText BEFORE truncating to the preview
|
||||
// cap. That masker's scheme-prefix scan ([a-z][a-z0-9+.-]*://) backtracks
|
||||
// O(n²) over long scheme-char runs — a megabyte of 'x' spins the event loop
|
||||
// for minutes (pre-existing since the #4191 any-scheme regex; tracked
|
||||
// separately). Whitespace-separated words keep the byte size identical while
|
||||
// keeping the scan linear.
|
||||
// Keep this held-forwarding fixture prose-shaped. Long unbroken input in the
|
||||
// anonymous provenance URL masker is covered by log-mask-linear.test.ts.
|
||||
const BIG_TURN_WORDS = "lorem ipsum dolor sit amet ";
|
||||
function bigTurn(model: string, targetBytes: number): unknown {
|
||||
const overhead = JSON.stringify({ model, messages: [{ role: "user", content: "" }], max_tokens: 64, stream: false }).length;
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { runInNewContext } from "node:vm";
|
||||
import { buildAnonymousProvenance } from "../src/anon-provenance.js";
|
||||
import { maskUrlsForDiagnostics, maskUrlsForPublish, maskUrlsInText, setMaskHostsEnabled } from "../src/log-mask.js";
|
||||
|
||||
const maskers = [
|
||||
["log", maskUrlsInText],
|
||||
["diagnostics", maskUrlsForDiagnostics],
|
||||
["publish", maskUrlsForPublish],
|
||||
] as const;
|
||||
|
||||
for (const [name, mask] of maskers) {
|
||||
test(`${name} URL masking makes bounded progress over a long non-URL run`, () => {
|
||||
const prefix = "a1.+-".repeat(40_000);
|
||||
const input = `${prefix}:/ https://alice:synthetic-password@relay.internal/v1?token=synthetic#fragment`;
|
||||
const output: unknown = runInNewContext("mask(input)", { mask, input }, { timeout: 2_000 });
|
||||
assert.equal(output, `${prefix}:/ https://<private-host>/v1`);
|
||||
});
|
||||
|
||||
test(`${name} URL masking preserves scheme-prefix and Unicode boundaries`, () => {
|
||||
const suffix = "https://alice:synthetic-password@relay.internal:8443/v1?q=synthetic#fragment";
|
||||
for (const prefix of ["", "123", ".+-", "123.+-", "_", "é", "中文", "😀", "\u200b", "\u0000", "\n", "\t"]) {
|
||||
assert.equal(mask(`${prefix}${suffix}`), `${prefix}https://<private-host>:8443/v1`, JSON.stringify(prefix));
|
||||
}
|
||||
assert.equal(mask(`99ABC${suffix}`), "99abchttps://<private-host>:8443/v1");
|
||||
assert.equal(mask(`x${suffix}`), "xhttps://<private-host>:8443/v1");
|
||||
assert.equal(mask("123://not-a-scheme words mailto:user@example.test"), "123://not-a-scheme words mailto:user@example.test");
|
||||
});
|
||||
|
||||
test(`${name} URL masking retains multiple URLs and surrounding punctuation`, () => {
|
||||
const input = '123https://u:p@api.openai.com/v1?q=secret "éftp://u:p@relay.internal/pub" <ws://u:p@relay.internal/ws> _socks5h://u:p@relay.internal:1080';
|
||||
assert.equal(mask(input), '123https://api.openai.com/v1 "éftp://<private-host>/pub" <ws://<private-host>/ws> _socks5h://<private-host>:1080');
|
||||
});
|
||||
}
|
||||
|
||||
test("anonymous provenance can preview a long unbroken first user message", () => {
|
||||
const text = "x".repeat(200_000);
|
||||
const result: unknown = runInNewContext(
|
||||
"build({}, [{ role: 'user', content: text }]).firstMsg",
|
||||
{ build: buildAnonymousProvenance, text },
|
||||
{ timeout: 2_000 },
|
||||
);
|
||||
assert.equal(result, "x".repeat(160));
|
||||
});
|
||||
|
||||
test("malformed URL fragments preserve each surface's existing fallback", () => {
|
||||
assert.equal(maskUrlsInText("123http://[bad"), "123<unparseable-url>");
|
||||
assert.equal(maskUrlsForPublish("123http://[bad"), "123<unparseable-url>");
|
||||
assert.equal(maskUrlsForDiagnostics("123http://[bad"), "123http://[bad");
|
||||
for (const [, mask] of maskers) assert.equal(mask(".+http:// \"https://\""), ".+http:// \"https://\"");
|
||||
});
|
||||
|
||||
test("host-mask opt-out keeps credentials scrubbed and never weakens publishing", () => {
|
||||
const input = "123https://alice:synthetic-password@relay.internal/v1?q=synthetic#fragment";
|
||||
setMaskHostsEnabled(false);
|
||||
try {
|
||||
assert.equal(maskUrlsInText(input), "123https://relay.internal/v1");
|
||||
assert.equal(maskUrlsForDiagnostics(input), "123https://relay.internal/v1");
|
||||
assert.equal(maskUrlsForPublish(input), "123https://<private-host>/v1");
|
||||
} finally {
|
||||
setMaskHostsEnabled(true);
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,96 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import {
|
||||
isLoopbackHost, isMaskHostsEnabled, maskHostPortForDisplay,
|
||||
maskUrlForDiagnostics, maskUrlsForDiagnostics, setMaskHostsEnabled,
|
||||
} from "../src/log-mask.js";
|
||||
import { describeUpstreamFailure } from "../src/upstream-proxy.js";
|
||||
import { buildProxyFailureResponse, withUpstreamFailure } from "../src/upstream-fail.js";
|
||||
import { logHostEvent } from "../src/agent/host-log.js";
|
||||
import { defaultLogFile } from "../src/paths.js";
|
||||
import { rmrf } from "./tmp-rm.js";
|
||||
|
||||
test("DNS names starting with 127 are not loopback addresses", () => {
|
||||
for (const host of ["127.customer.internal", "127.0.0.1.customer.internal", "127.dead.beef"]) {
|
||||
assert.equal(isLoopbackHost(host), false, host);
|
||||
assert.equal(maskHostPortForDisplay(`${host}:8443`), "<private-host>:8443", host);
|
||||
}
|
||||
});
|
||||
|
||||
test("diagnostic URL masking protects 127-prefixed private DNS names", () => {
|
||||
for (const scheme of ["https", "ws", "socks5h"]) {
|
||||
const input = `${scheme}://alice:synthetic-password@127.customer.internal:8443/v1?q=synthetic#fragment`;
|
||||
assert.equal(maskUrlForDiagnostics(input), `${scheme}://<private-host>:8443/v1`);
|
||||
assert.equal(maskUrlsForDiagnostics(`dial ${input} failed`), `dial ${scheme}://<private-host>:8443/v1 failed`);
|
||||
}
|
||||
});
|
||||
|
||||
test("the actual failure response never exposes a loopback-looking DNS name", () => {
|
||||
const host = "127.customer.internal";
|
||||
const error = Object.assign(new Error(`getaddrinfo ENOTFOUND ${host}`), { code: "ENOTFOUND" });
|
||||
const failure = describeUpstreamFailure(error, `https://${host}:8443/v1`);
|
||||
assert.equal(failure.detail.host, "<private-host>:8443");
|
||||
const response = buildProxyFailureResponse(failure.message, withUpstreamFailure(error, failure.detail));
|
||||
assert.equal(response.status, 502);
|
||||
const body = JSON.parse(response.body) as { error: string; upstreamFailure: { host: string } };
|
||||
assert.equal(body.error, "acp-proxy failure: dns ENOTFOUND <private-host>:8443");
|
||||
assert.equal(body.upstreamFailure.host, "<private-host>:8443");
|
||||
assert.ok(!response.body.includes(host));
|
||||
});
|
||||
|
||||
test("host-event file output masks loopback-looking private DNS names", async () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-loopback-hostname-"));
|
||||
const previous = process.env.XDG_STATE_HOME;
|
||||
process.env.XDG_STATE_HOME = dir;
|
||||
try {
|
||||
logHostEvent("warn", "[test-host]", "dial https://127.customer.internal:8443/v1 failed");
|
||||
const output = fs.readFileSync(defaultLogFile(), "utf8");
|
||||
assert.match(output, /dial https:\/\/<private-host>:8443\/v1 failed/);
|
||||
assert.ok(!output.includes("127.customer.internal"));
|
||||
} finally {
|
||||
if (previous === undefined) delete process.env.XDG_STATE_HOME; else process.env.XDG_STATE_HOME = previous;
|
||||
await rmrf(dir);
|
||||
}
|
||||
});
|
||||
|
||||
test("bracketed IPv6 without a port is not split at an address colon", () => {
|
||||
assert.equal(maskHostPortForDisplay("[::1]"), "[::1]");
|
||||
assert.equal(maskHostPortForDisplay("[2001:db8::1]"), "<private-host>");
|
||||
const failure = describeUpstreamFailure(new Error("failed"), "http://[::1]/v1");
|
||||
assert.equal(failure.detail.host, "[::1]");
|
||||
});
|
||||
|
||||
test("genuine loopback names and IPv4 aliases remain actionable", () => {
|
||||
for (const host of ["localhost", "LOCALHOST", "0.0.0.0", "127.0.0.1", "127.255.255.255", "127.1", "127.0.1", "127.0x1", "127.000.1"]) {
|
||||
assert.equal(isLoopbackHost(host), true, host);
|
||||
assert.equal(maskHostPortForDisplay(`${host}:8443`), `${host}:8443`, host);
|
||||
const origin = `http://${host}:8443`;
|
||||
assert.equal(maskUrlForDiagnostics(origin), origin, "clean loopback origins remain byte-identical");
|
||||
}
|
||||
assert.equal(isLoopbackHost("[::1]"), true);
|
||||
assert.equal(isLoopbackHost("0:0:0:0:0:0:0:1"), true);
|
||||
assert.equal(maskHostPortForDisplay("[::1]:9000"), "[::1]:9000");
|
||||
});
|
||||
|
||||
test("invalid numeric addresses and URL syntax are not loopback hosts", () => {
|
||||
for (const host of ["127.1.2.999", "127.0x1000000", "127..1", "127.0.0.1/path", "127.0.0.1@private.example", "127.0.0.1?query", "127.0.0.1\n", "localhost.private.example"]) {
|
||||
assert.equal(isLoopbackHost(host), false, JSON.stringify(host));
|
||||
}
|
||||
});
|
||||
|
||||
test("public hosts and explicit private-host visibility keep their existing policy", () => {
|
||||
assert.equal(maskHostPortForDisplay("api.openai.com:443"), "api.openai.com:443");
|
||||
assert.equal(maskHostPortForDisplay("relay.internal:443"), "<private-host>:443");
|
||||
const previous = isMaskHostsEnabled();
|
||||
setMaskHostsEnabled(false);
|
||||
try {
|
||||
assert.equal(maskHostPortForDisplay("127.customer.internal:8443"), "127.customer.internal:8443");
|
||||
assert.equal(maskUrlForDiagnostics("https://alice:synthetic-password@127.customer.internal/v1?q=synthetic#fragment"), "https://127.customer.internal/v1");
|
||||
assert.equal(maskHostPortForDisplay("[2001:db8::1]:8443"), "[2001:db8::1]:8443");
|
||||
} finally {
|
||||
setMaskHostsEnabled(previous);
|
||||
}
|
||||
});
|
||||
@@ -136,3 +136,54 @@ test("svg: recovery chart derives old-stock brackets from server-shaped episodes
|
||||
assert.ok(svg.includes("310%"), "true >100% value labeled");
|
||||
assert.ok(svg.includes('r="6" fill="none" stroke="#0969da"'), "over-100% halo dot");
|
||||
});
|
||||
|
||||
test("svg: the complete long-session trajectory survives the call-argument limit", () => {
|
||||
const count = 200_000;
|
||||
const lines = Array.from({ length: count }, (_, i) => ({ seq: i + 1, input: i + 1, cached: i / 2 }));
|
||||
const { svg } = buildSessionReportSvg({ ledger: { lines } }, { lang: "en" });
|
||||
const points = svg.match(/<polyline points="([^"]+)" fill="none" stroke="#0969da" stroke-width="1\.6"\/>/);
|
||||
assert.ok(points, "the complete input trajectory is present");
|
||||
assert.equal(points[1].split(" ").length, count, "large histories are not silently truncated");
|
||||
assert.match(svg, />200K<\/text>/, "the last sample determines the numeric axis");
|
||||
assert.match(svg, />200000<\/text>/, "the final request is retained in the detail table");
|
||||
assert.ok(svg.endsWith("</svg>"));
|
||||
});
|
||||
|
||||
test("svg: a long recovery window retains its measured growth extrema", () => {
|
||||
const count = 200_000;
|
||||
const at = 1_760_000_000_000;
|
||||
const lines = Array.from({ length: count }, (_, i) => ({ at: at + i, input: i === count - 1 ? 300_000 : 120_000, cached: 60_000 }));
|
||||
const { svg } = buildSessionReportSvg({ ledger: {
|
||||
lines,
|
||||
recovery: {
|
||||
events: [
|
||||
{ at, kind: "n", pending: 100_000, paired: true },
|
||||
{ at: at + count, kind: "f", folded: 90_000, mode: "n", pending: 100_000, pct: 90 }
|
||||
],
|
||||
episodes: [{ at, pending: 100_000, folded: 90_000, pct: 90 }]
|
||||
}
|
||||
} }, { lang: "en" });
|
||||
assert.match(svg, /<title>growth 180K → folded 90K · 50%<\/title>/);
|
||||
assert.match(svg, />300K<\/text>/, "the full window's peak is visible");
|
||||
assert.ok(!svg.includes("NaN") && !svg.includes("Infinity"));
|
||||
});
|
||||
|
||||
test("svg: axis reference values and recovery outlier filtering retain their semantics", () => {
|
||||
for (const [contextWindow, systemPromptTokens, axis] of [[400_000, 50_000, "400K"], [50_000, 500_000, "500K"], [0, 0, "300K"]] as const) {
|
||||
const at = 1_760_000_000_000;
|
||||
const { svg } = buildSessionReportSvg({ contextWindow, systemPromptTokens, ledger: {
|
||||
lines: [
|
||||
{ at: at - 1, input: 10_000 },
|
||||
{ at, input: 120_000 },
|
||||
{ at: at + 1, input: 20_000 },
|
||||
{ at: at + 2, input: 300_000 }
|
||||
],
|
||||
recovery: {
|
||||
events: [{ at, kind: "n", pending: 100_000 }, { at: at + 3, kind: "f", folded: 90_000, mode: "n", pct: 90 }],
|
||||
episodes: [{ at, folded: 90_000, pending: 100_000, pct: 90 }]
|
||||
}
|
||||
} }, { lang: "en" });
|
||||
assert.ok(svg.includes(">" + axis + "</text>"), axis);
|
||||
assert.match(svg, /<title>growth 180K → folded 90K · 50%<\/title>/, "sub-quarter-peak samples do not set the growth floor");
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user