Skip to content
Merged
3 changes: 3 additions & 0 deletions scripts/live-files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export const LIVE_FILES = [
// Native-zstd absence is simulated in an isolated Node child; it binds
// no sockets and changes only that child's built-in module exports.
"tests/content-encoding-zstd.test.ts",
// One Linux-only Node child reads fixture system CAs and exits within 10s;
// no child ports or network I/O. In-process HTTPS fixtures use listen(0).
"tests/upstream-ca-default-trust.test.ts",
];

/** Band width per live file: the zone ladder drifts +1 per EADDRINUSE retry;
Expand Down
7 changes: 5 additions & 2 deletions src/loop/adapter-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,13 +624,16 @@ export function createGoogleAdapter(
});
continue;
}
const clean = tagFilter.push(part.text);
// A signature may belong to ordinary text, without thought:true.
// Preserve its bytes and part boundary on both replay paths.
const signed = typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0;
const clean = signed ? part.text : tagFilter.push(part.text);
if (clean !== part.text) {
editedParts ??= [...parts];
editedParts[i] = { ...part, text: clean };
}
if (clean.length === 0) continue;
partEvents.push({ kind: "text", delta: clean, rawHandled: true });
partEvents.push({ kind: "text", delta: clean, rawHandled: true, ...(signed ? { rawGooglePart: part } : {}) });
}
// Decide wire output only after every prose sibling has been
// cleaned. Each complete frame belongs to one event, regardless
Expand Down
14 changes: 9 additions & 5 deletions src/loop/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { maybeJudgeContinuity } from "../invariant-judge.js";
import type { SeparatorSlot } from "../server/responses-ordering.js";
import { countImagesInParsedBody, countImagesInRawBody } from "../image-tokens.js";
import { diagnoseSuccessWithoutUsage, lastCompressSuffix, releaseNudgeHold, withSessionLock, type Session } from "../session.js";
import type { BiliMessage } from "acp-kernel/wire";
import type { BiliMessage, GooglePart } from "acp-kernel/wire";
import { coreToAnthropic } from "acp-kernel/wire";
import {
parseCompressInput,
Expand Down Expand Up @@ -329,7 +329,7 @@ export interface RequestOptions {
// arrive instead of being re-laid out as [reasoning…, text, tool…].
type RoundPart =
| { kind: "reasoning"; text: string; signature: string }
| { kind: "text"; text: string }
| { kind: "text"; text: string; rawGooglePart?: GooglePart }
| { kind: "redacted"; data: string }
// #3158: a Gemini part whose only payload is a thoughtSignature (no text,
// no functionCall). Recorded in stream order so a proxy-tool rebuild puts
Expand All @@ -340,7 +340,7 @@ export type ParsedStreamEvent =
// rawHandled: the adapter carries this delta in another event's complete
// frame (or buffers that frame until tool settlement). Keep accounting it,
// but do not synthesize a second client frame in continuation rounds.
| { kind: "text"; delta: string; raw?: Buffer; rawHandled?: boolean }
| { kind: "text"; delta: string; raw?: Buffer; rawHandled?: boolean; rawGooglePart?: GooglePart }
// round2-hold: withheld marks a reasoning PAYLOAD the adapter kept off the
// client wire (bili-owned round ≥ 2 on a signing upstream — see
// ParseStreamOpts.upstreamSignsThinking). It still feeds the loop's
Expand Down Expand Up @@ -970,8 +970,8 @@ export async function* runCompressLoop(
if (ev.kind === "text") {
assistantText += ev.delta;
let tp = roundParts[roundParts.length - 1];
if (!tp || tp.kind !== "text") {
tp = { kind: "text", text: "" };
if (!tp || tp.kind !== "text" || tp.rawGooglePart !== undefined || ev.rawGooglePart !== undefined) {
tp = { kind: "text", text: "", ...(ev.rawGooglePart ? { rawGooglePart: ev.rawGooglePart } : {}) };
roundParts.push(tp);
}
tp.text += ev.delta;
Expand Down Expand Up @@ -1538,6 +1538,10 @@ export async function* runCompressLoop(
role: "assistant",
contentType: "text",
text: part.text,
...(part.rawGooglePart ? {
googleThoughtSignature: part.rawGooglePart.thoughtSignature,
rawGoogleParts: [part.rawGooglePart],
} : {}),
});
} else if (part.kind === "standalone_signature") {
// #3158: put the standalone thoughtSignature back at its
Expand Down
19 changes: 12 additions & 7 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3766,10 +3766,11 @@ export async function pipePluginChatWithStrip(
// field, so an interleaved thought/text pair in one frame never
// shares held-back state.
const field = p["thought"] === true ? "thinking" : "text";
const signed = typeof p["thoughtSignature"] === "string" && p["thoughtSignature"].length > 0;
// Whole-field tool-call emission on an absorb-instructed
// request (m00885) — see processOpenai. Degenerate-close
// residue (#2190) rides the same routing.
if (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !mayStartDegenerateRenderTag(raw) && !(absorbInstructed === true && mayStartToolCallEmission(raw)) && !anyPending()) {
if (signed || (!mayStartRenderTag(raw) && !mayStartMarkerLine(raw) && !mayStartBiliInternal(raw) && !mayStartDegenerateRenderTag(raw) && !(absorbInstructed === true && mayStartToolCallEmission(raw)) && !anyPending())) {
if (raw.length > 0) {
keptText = true;
appendReply(field, index, raw);
Expand Down Expand Up @@ -5207,12 +5208,16 @@ function stripGoogleChunk<T>(obj: T, drop: boolean = false, requestText?: string
...cand,
content: {
...cont,
parts: (cont["parts"] as unknown[]).map((p) =>
// #1960/KDD#10: thought parts carry a thoughtSignature — leave them byte-for-byte.
p && typeof p === "object" && (p as Record<string, unknown>)["thought"] !== true && typeof (p as Record<string, unknown>)["text"] === "string"
? { ...(p as Record<string, unknown>), text: stripAcpTags((p as Record<string, unknown>)["text"] as string, drop, requestText) }
: p,
),
parts: (cont["parts"] as unknown[]).map((p) => {
if (!p || typeof p !== "object") return p;
const part = p as Record<string, unknown>;
// #1960/KDD#10: ordinary text can carry a signature too.
return part["thought"] !== true
&& !(typeof part["thoughtSignature"] === "string" && part["thoughtSignature"].length > 0)
&& typeof part["text"] === "string"
? { ...part, text: stripAcpTags(part["text"], drop, requestText) }
: p;
}),
},
};
});
Expand Down
57 changes: 44 additions & 13 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,12 @@ export function selfAddressedLocalPath(reqUrl: string, localPort: number | undef
const port = u.port !== "" ? Number(u.port) : u.protocol === "https:" ? 443 : 80;
if (port !== localPort) return undefined;
const host = u.hostname.replace(/^\[|\]$/g, "").toLowerCase();
const mine = host === "0.0.0.0" || host === "::" || host === "localhost" || localMachineIps().has(normalizeIpLiteral(host));
// #7022 review: classifyIp covers the whole loopback range, not just
// configured interfaces — the tunnel-side fix stopped enumerating
// 127.0.0.x in localMachineIps(), so an unconfigured alias like
// 127.0.0.5:selfport must still be recognized as self here (else it
// would fall through to tunnel classification and 403, #562 self-layer).
const mine = host === "0.0.0.0" || host === "::" || host === "localhost" || classifyIp(host) === "loopback" || localMachineIps().has(normalizeIpLiteral(host));
if (!mine) return undefined;
return u.pathname + u.search;
} catch {
Expand Down Expand Up @@ -1564,8 +1569,8 @@ export async function startServer(opts: ProxyOptions): Promise<http.Server> {
// callback stacks on the server and a later successful bind fires ALL
// accumulated claimOrAnnounce closures at once (double announce,
// double registration, MaxListenersExceededWarning on long waits).
// Only our own callbacks ever use this event.
server.removeAllListeners("listening");
server.on("listening", installProcessHandlers);
server.listen(port, opts.host, claimOrAnnounce);
};
attemptListen(opts.port);
Expand Down Expand Up @@ -1714,7 +1719,7 @@ export async function startServer(opts: ProxyOptions): Promise<http.Server> {
// Node 20+ aborts the process on these by default. Log loudly and flush.
let suppressedWriteErrors = 0;
let suppressedRaceErrors = 0;
process.on("uncaughtException", (err) => {
const onUncaughtException = (err: Error): void => {
if (isStreamWriteError(err)) {
// Belt-and-suspenders for #1233: the logger's own stderr path is
// guarded and never reaches here; a stream-write error that does
Expand All @@ -1739,10 +1744,10 @@ export async function startServer(opts: ProxyOptions): Promise<http.Server> {
return;
}
log("error", `uncaughtException: ${String(err?.stack ?? err)}`);
});
process.on("unhandledRejection", (reason) => {
};
const onUnhandledRejection = (reason: unknown): void => {
log("error", `unhandledRejection: ${String(reason)}`);
});
};
// Graceful shutdown: flush all dirty sessions to disk so a restart does
// not lose recent compression state. SIGKILL/power loss cannot flush, but
// debounced writes keep disk within ~500ms of in-memory state.
Expand Down Expand Up @@ -1816,14 +1821,40 @@ export async function startServer(opts: ProxyOptions): Promise<http.Server> {
// #3544: POST /__bili/shutdown (stale-proxy rotation from a newer
// generation's bring-up) takes this exact graceful path.
engine.lifecycleHooks.requestShutdown = (reason) => shutdown(reason);
process.on("SIGTERM", () => shutdown("SIGTERM"));
process.on("SIGINT", () => shutdown("SIGINT"));
// Windows never delivers SIGTERM (Node can listen but the kernel won't
// raise it). Ctrl+Break (and most service managers / `taskkill` / NSSM)
// raise SIGBREAK, so hook it to the same graceful-shutdown path there.
if (process.platform === "win32") {
process.on("SIGBREAK", () => shutdown("SIGBREAK"));
const onSigterm = () => shutdown("SIGTERM");
const onSigint = () => shutdown("SIGINT");
const onSigbreak = () => shutdown("SIGBREAK");
let processHandlersInstalled = false;
function installProcessHandlers(): void {
if (processHandlersInstalled) return;
processHandlersInstalled = true;
process.on("uncaughtException", onUncaughtException);
process.on("unhandledRejection", onUnhandledRejection);
process.on("SIGTERM", onSigterm);
process.on("SIGINT", onSigint);
// Windows service managers deliver SIGBREAK instead of SIGTERM.
if (process.platform === "win32") process.on("SIGBREAK", onSigbreak);
}
const removeProcessHandlers = (): void => {
if (!processHandlersInstalled) return;
// #7026 review: the close event fires when the listener stops accepting,
// i.e. BEFORE the session flush in finishDrain. Stripping the handlers
// here would leave a second Ctrl+C during the flush window unhandled —
// the process would die with unflushed compression state. Keep them
// until the process actually exits; a closed embedded server that never
// shut down (port-ladder retry path) still cleans up normally.
if (shuttingDown) return;
processHandlersInstalled = false;
process.removeListener("uncaughtException", onUncaughtException);
process.removeListener("unhandledRejection", onUnhandledRejection);
process.removeListener("SIGTERM", onSigterm);
process.removeListener("SIGINT", onSigint);
if (process.platform === "win32") process.removeListener("SIGBREAK", onSigbreak);
};
// A closed embedded server must not retain signal-driven shutdown of the
// hosting process. Port-claim retries can close and re-listen this server.
installProcessHandlers();
server.on("close", removeProcessHandlers);
// Launcher children have no console and TerminateProcess leaves no room
// for a flush (#414): they watch the launcher pid and run the graceful
// path themselves when it disappears (≤2s after the parent exits).
Expand Down
14 changes: 9 additions & 5 deletions src/stream-google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator<Json
const containsEcho = (text: string): boolean => containsRenderTagText(text) || containsMarkerLineText(text) || containsBiliInternalText(text) || (absorbArmed && containsToolCallEmissionText(text));
// Capture request evidence before a yielded proxy tool can change the context.
const requestText = absorbArmed && parts.some(part => part && typeof part === "object"
&& (part as { thought?: boolean }).thought !== true && typeof part.text === "string" && containsEcho(part.text))
&& (part as { thought?: boolean }).thought !== true
&& !(typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0)
&& typeof part.text === "string" && containsEcho(part.text))
? JSON.stringify(ctx.messages) : undefined;
for (const part of parts) {
if (!part || typeof part !== "object") continue;
Expand All @@ -46,10 +48,12 @@ function* rewriteGoogleJsonSteps(body: unknown, ctx: RewriteCtx): Generator<Json
continue;
}
if (fc) sawReal = true;
// #1960/KDD#10: a thought part carries a thoughtSignature the client
// echoes back verbatim — rewriting its text desyncs the signature and
// bricks replay. Strip prose only from non-thought parts.
if ((part as { thought?: boolean }).thought !== true && typeof part.text === "string" && containsEcho(part.text)) {
// #1960/KDD#10: thought parts and signed ordinary text are replayed
// verbatim. Rewriting their text desyncs the signed payload; strip
// prose only from unsigned, non-thought parts.
if ((part as { thought?: boolean }).thought !== true
&& !(typeof part.thoughtSignature === "string" && part.thoughtSignature.length > 0)
&& typeof part.text === "string" && containsEcho(part.text)) {
ctx.log(`[tag-echo] stripped: non-stream google output ACP echo (render tags/markers/internal artifacts): ${part.text.slice(0, 120).replace(/\n/g, " ")}`);
part.text = stripAcpTags(part.text, absorbArmed, requestText);
}
Expand Down
11 changes: 7 additions & 4 deletions src/tunnel-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,6 @@ export function localMachineIps(): Set<string> {
if (ni.address) ips.add(ni.address.toLowerCase());
}
}
// The whole 127/8 loopback range routes to this machine.
for (let i = 0; i <= 255; i++) ips.add(`127.0.0.${i}`);
localAddrCache = { at: now, ips };
}
return localAddrCache.ips;
Expand Down Expand Up @@ -355,8 +353,13 @@ export async function checkTunnelDestination(origin: string, ctx: TunnelCheckCon
// destination routes to the local host, so our own port through them is
// a self-loop even though neither literal sits in the interface set.
if (ctx.selfPort !== undefined && port === ctx.selfPort) {
const mine = (ctx.localIps ?? localMachineIps)();
if (ips.some((ip) => ip === "0.0.0.0" || ip === "::" || mine.has(ip.toLowerCase()) || mine.has(`::ffff:${ip.toLowerCase()}`))) {
const mine = new Set(Array.from((ctx.localIps ?? localMachineIps)(), normalizeIpLiteral));
if (ips.some((address) => {
const ip = normalizeIpLiteral(address);
// Loopback is a range, not a list of configured interfaces. DNS
// answers also need the mapped-IPv4 normalization literals get.
return ip === "0.0.0.0" || classifyIp(ip) === "loopback" || mine.has(ip);
})) {
return { ok: false, code: "self", message: "the bili tunnel may not target the proxy itself" };
}
}
Expand Down
17 changes: 10 additions & 7 deletions src/upstream-ca.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// #3776 sub-defect 2: CA trust material for bili's OWN outbound upstream fetch.
// Node's default TLS trust is the built-in Mozilla bundle + NODE_EXTRA_CA_CERTS;
// it does NOT read the OS certificate store, so a security-software MITM root
// (Kaspersky / corporate DLP) installed in the Windows/Mac store is untrusted and
// Node's default TLS trust may omit the OS certificate store, so a security-software MITM root
// (Kaspersky / corporate DLP) installed in the Windows/Mac store can be untrusted and
// every request dies with SELF_SIGNED_CERT_IN_CHAIN (#3773). network.caFile /
// BILI_CA_FILE points at that vendor's PEM; we MERGE it with the built-ins (never
// replace) so all other upstreams keep working. When unset we return undefined and
// BILI_CA_FILE points at that vendor's PEM; we MERGE it with Node's effective trust
// so all other upstreams keep working. When unset we return undefined and
// the dispatcher uses Node's default trust byte-for-byte (no connect override).

import { readFileSync, statSync } from "node:fs";
Expand Down Expand Up @@ -47,7 +46,7 @@ export function upstreamCaIdentity(now = Date.now()): string {
return lastIdentity;
}

/** Merged CA list (built-ins + NODE_EXTRA_CA_CERTS + network.caFile) for bili's
/** Merged CA list (Node's effective trust + NODE_EXTRA_CA_CERTS + network.caFile) for bili's
* own fetch, or undefined when no caFile is configured — in which case the
* dispatcher must NOT set connect.ca and Node's default trust applies unchanged.
* The merge (not replace) matters: an explicit undici connect.ca REPLACES the
Expand All @@ -56,7 +55,11 @@ export function upstreamExtraCa(): string[] | undefined {
const identity = upstreamCaIdentity();
if (identity === "none") return undefined;
if (cacheKey === identity) return cacheValue;
const merged: string[] = [...tls.rootCertificates];
// Node 22.15+ exposes the effective store, including opted-in system CAs
// and programmatic defaults. rootCertificates contains only bundled roots.
const merged: string[] = typeof tls.getCACertificates === "function"
? [...tls.getCACertificates("default")]
: [...tls.rootCertificates];
const nodeExtra = process.env["NODE_EXTRA_CA_CERTS"];
// Node treats NODE_EXTRA_CA_CERTS as ONE file path — no list separator on any
// platform (verified against Node 22: the whole value is loaded as a single
Expand Down
Loading
Loading