Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
9bcd9c4
fix: honor Request cancellation in the inline engine
Afloat16 Nov 12, 2026
1c6d2c5
Merge current master into PR #6504 for validation
Afloat16 Nov 13, 2026
c6369fb
fix(tests): pin pure-sqlite selection in notice-registry suite
Nov 13, 2026
b1bc17b
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in CCR cold-disk-restore …
Nov 14, 2026
552fcfe
Merge remote-tracking branch 'origin/master' into w5p
ranxianglei Nov 14, 2026
cd8bc8a
Merge remote-tracking branch 'origin/master' into ci-fixes
Nov 14, 2026
7492c35
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in native opencode e2e lane
Nov 14, 2026
45f93db
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in native dsh e2e lane
Nov 14, 2026
d67cc43
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in pi codex-responses WS …
Nov 14, 2026
c392237
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in pi package-native e2e …
Nov 14, 2026
974cd4c
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in fork-ccr-adoption suite
Nov 14, 2026
271abba
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in subagent-sessions e2e …
Nov 14, 2026
5d47196
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in dsh-persona e2e lane
Nov 14, 2026
1ca4087
fix(tests): pin BILI_STORAGE_JSON_WRITER=on in update-state e2e lane
Nov 14, 2026
abd1dd4
Merge upstream master and ci-fixes to refresh PR validation
Afloat16 Nov 14, 2026
c5b41b7
Merge master into 2026-11-12_inline-request-abort-signal
Afloat16 Nov 14, 2026
dc0efc3
fix: classify inline-lane cancellation by signal state, not reason name
Nov 14, 2026
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
7 changes: 6 additions & 1 deletion src/agent/pi-inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export function installInlineTransport(): boolean {
} catch (err) {
// Caller-side cancel is not an engine failure — propagate it:
// degrading would consume the one-shot warning and re-send a
// request the caller already abandoned.
// request the caller already abandoned. The engine rejects with
// the signal's REASON, which for a custom abort
// (controller.abort(new Error(...))) is not an AbortError — so
// classify by the signal's state, not the reason's name.
const cancelSignal = init?.signal !== undefined ? init.signal : input instanceof Request ? input.signal : undefined;
if (cancelSignal?.aborted) throw err;
if (err instanceof Error && err.name === "AbortError") throw err;
const msg = err instanceof Error ? err.message : String(err);
if (url.startsWith(MODEL_PREFIX)) {
Expand Down
4 changes: 3 additions & 1 deletion src/inline-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,17 @@ function responseFrom(res: InlineRes): Response {

function makeDispatcher(ctx: EngineContext): InlineEngine["dispatch"] {
return async (input, init, rawFetch) => {
const signal = init?.signal !== undefined ? init.signal : input instanceof Request ? input.signal : undefined;
signal?.throwIfAborted();
const req = await toInlineReq(input, init);
signal?.throwIfAborted();
const res = new InlineRes();
// Spawn-mode cancel parity (#3278 review): honor the caller's
// AbortSignal. Destroying res drives the pipeline's own close plumbing
// (handle()'s decodingAbort, relay's clientAbort) exactly like a real
// socket close does over HTTP, stopping the upstream round trip; the
// raced promise makes the caller reject with the abort reason like
// undici would instead of running to completion and burning tokens.
const signal = init?.signal;
let onAbort: (() => void) | undefined;
let abortPromise: Promise<never> | undefined;
if (signal != null) {
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/e2e-update-state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ test("self-update keeps live compression state: folds survive the OLD→NEW hop
envBase.BILI_UPDATE_POLICY_URL = "http://127.0.0.1:1/no-policy.json";
envBase.BILI_PERSIST_DEBOUNCE_MS = "200";
envBase.BILI_SESSIONS_DIR = sessionsDir;
// Pure-sqlite default (5a80e2b8e, #6480) turns the JSON envelope
// writer OFF, but this lane asserts on the on-disk .json session file
// (state survival across the update). Pin the writer ON so the
// envelope this test was written for is still produced.
envBase.BILI_STORAGE_JSON_WRITER = "on";

const port = await freePort();
proxy = spawnProxy(installDir, port, envBase);
Expand Down
130 changes: 130 additions & 0 deletions tests/inline-request-signal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { after, test } from "node:test";
import assert from "node:assert/strict";
import http from "node:http";
import { once } from "node:events";
import type { AddressInfo } from "node:net";

function deferred() {
let resolve!: () => void;
const promise = new Promise<void>((done) => { resolve = done; });
return { promise, resolve };
}

interface Fixture {
started: ReturnType<typeof deferred>;
closed: ReturnType<typeof deferred>;
response?: http.ServerResponse;
stream?: boolean;
}
const pending = new Map<string, Fixture>();
let upstreamHits = 0;
const upstream = http.createServer((req, res) => {
req.resume();
req.on("end", () => {
upstreamHits++;
const fixture = pending.get(req.url ?? "");
if (!fixture) { res.end("complete"); return; }
fixture.response = res;
res.once("close", fixture.closed.resolve);
if (fixture.stream) {
res.writeHead(200, { "content-type": "application/octet-stream" });
res.write("first");
}
fixture.started.resolve();
});
});
upstream.listen(0, "127.0.0.1");
await once(upstream, "listening");
const origin = `http://127.0.0.1:${(upstream.address() as AddressInfo).port}`;
const ie = await import("../src/inline-engine.ts");

after(async () => {
upstream.closeAllConnections();
await new Promise<void>((resolve) => upstream.close(() => resolve()));
const { flushAllSessions } = await import("../src/session.ts");
await flushAllSessions();
ie._resetInlineEngineForTest();
});

async function bounded<T>(promise: Promise<T>): Promise<T> {
let timer: NodeJS.Timeout | undefined;
try {
return await Promise.race([
promise,
new Promise<never>((_, reject) => {
timer = setTimeout(() => reject(new Error("inline cancellation did not settle")), 3000);
}),
]);
} finally { clearTimeout(timer); }
}

for (const explicitUndefined of [false, true]) {
test(`Request.signal cancels before headers (${explicitUndefined ? "undefined signal override" : "omitted init"})`, { timeout: 30_000 }, async () => {
const engine = await ie.getInlineEngine();
const endpoint = `/pending-${explicitUndefined}`;
const fixture: Fixture = { started: deferred(), closed: deferred() };
pending.set(endpoint, fixture);
const controller = new AbortController();
const reason = new Error("request caller cancelled");
const request = new Request(`${ie.INLINE_ORIGIN}/bili/${origin}${endpoint}`, { signal: controller.signal });
const call = engine.dispatch(request, explicitUndefined ? { signal: undefined } : undefined, globalThis.fetch);
try {
await bounded(fixture.started.promise);
controller.abort(reason);
await assert.rejects(bounded(call), (error: unknown) => error === reason);
await bounded(fixture.closed.promise);
} finally {
fixture.response?.end("released");
await call.then((response) => response.text(), () => {}).catch(() => {});
pending.delete(endpoint);
}
});
}

test("Request.signal aborts a committed body and closes the live upstream", { timeout: 30_000 }, async () => {
const engine = await ie.getInlineEngine();
const endpoint = "/stream";
const fixture: Fixture = { started: deferred(), closed: deferred(), stream: true };
pending.set(endpoint, fixture);
const controller = new AbortController();
const request = new Request(`${ie.INLINE_ORIGIN}/bili/${origin}${endpoint}`, { signal: controller.signal });
const response = await bounded(engine.dispatch(request, {}, globalThis.fetch));
const reader = response.body!.getReader();
let read: ReturnType<typeof reader.read> | undefined;
try {
assert.equal(new TextDecoder().decode((await bounded(reader.read())).value), "first");
controller.abort();
read = reader.read();
await assert.rejects(bounded(read), /abort/i);
await bounded(fixture.closed.promise);
} finally {
fixture.response?.end();
await read?.catch(() => {});
reader.releaseLock();
pending.delete(endpoint);
}
});

test("a pre-aborted Request never reaches the upstream", { timeout: 30_000 }, async () => {
const engine = await ie.getInlineEngine();
const controller = new AbortController();
const reason = new Error("already cancelled");
controller.abort(reason);
const request = new Request(`${ie.INLINE_ORIGIN}/bili/${origin}/ok`, { signal: controller.signal });
const before = upstreamHits;
await assert.rejects(engine.dispatch(request, undefined, globalThis.fetch), (error: unknown) => error === reason);
assert.equal(upstreamHits, before);
});

for (const override of ["signal", "null"] as const) {
test(`init.signal=${override} overrides an aborted Request signal`, { timeout: 30_000 }, async () => {
const engine = await ie.getInlineEngine();
const controller = new AbortController();
controller.abort();
const request = new Request(`${ie.INLINE_ORIGIN}/bili/${origin}/ok`, { signal: controller.signal });
const init = { signal: override === "null" ? null : new AbortController().signal };
const response = await engine.dispatch(request, init, globalThis.fetch);
assert.equal(response.status, 200);
assert.equal(await response.text(), "complete");
});
}
53 changes: 53 additions & 0 deletions tests/pi-inline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,56 @@ test("#3278 engine: caller abort cancels the in-process round trip (spawn-mode p
await new Promise((r) => setTimeout(r, 300));
assert.equal(slowCompleted, false, "upstream round trip must not complete after the caller aborted");
});

test("#6504 transport: a custom-reason Request.signal cancel propagates, never degrades to a direct resend", async () => {
// The engine rejects with the signal's REASON. For a custom abort
// (controller.abort(new Error(...))) that reason is NOT an AbortError, so
// the transport must classify the cancel by the signal's state — not by
// the reason's name — or it would degrade: consume the one-shot warning
// and re-send an already-abandoned request direct, uncompressed.
const ie = await import("../src/inline-engine.ts");
ie._resetInlineEngineForTest();
const reason = new Error("caller cancelled");
ie._setEngineFactoryForTest(async () => ({
origin: ie.INLINE_ORIGIN,
dispatch: async (input: string | URL | Request, init?: RequestInit): Promise<Response> => {
// Mirror the engine's signal resolution + pre-header abort reject.
const signal = init?.signal !== undefined ? init.signal : input instanceof Request ? input.signal : undefined;
await new Promise<void>((_, reject) => {
if (signal?.aborted) reject(signal.reason);
else signal?.addEventListener("abort", () => reject(signal.reason), { once: true });
});
return new Response("unreachable");
},
dispatchHttp: async () => {},
wsOrigin: Promise.resolve(undefined),
webOrigin: Promise.resolve(undefined),
}));
try {
await freshChain();
const ac = new AbortController();
const req = new Request(`${ie.INLINE_ORIGIN}/bili/${upstreamOrigin}/v1/messages`, {
method: "POST",
body: "{}",
signal: ac.signal,
});
const before = hits.length;
const settled = fetch(req).then(
(r) => { throw new Error(`expected cancellation, got HTTP ${r.status}`); },
(e: unknown) => e,
);
setTimeout(() => ac.abort(reason), 50);
const err = await withTimeout(settled, 10_000, "cancel propagation") as Error;
// The caller's custom reason must propagate verbatim.
assert.equal(err, reason, "the caller's abort reason must propagate verbatim");
// No direct resend: the real upstream must stay untouched.
assert.equal(hits.length, before, "a cancelled request must not be re-sent direct");
} finally {
const piInline = await import("../src/agent/pi-inline.ts");
const fc = await import("../src/agent/fetch-chain/index.ts");
piInline._resetInlineTransportForTest();
fc.resetFetchChainForTest();
ie._setEngineFactoryForTest(undefined);
ie._resetInlineEngineForTest();
}
});
Loading