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
18 changes: 14 additions & 4 deletions src/agent/native-intercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,10 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
// #5403/#5418: the watermark handshake (x-bili-pf-count / -tail-ids)
// is emitted ONCE here for every routed model request — lanes differ
// only in where their ids are captured (pfChannel.pfCaptureIds).
const pfExtra = pfChannel.pfRequestHeaders(outgoingHeadersOf(input, init) ?? {});
// pfRequestHeaders must see the MERGED view: the conversation header
// may come from headersFor (dsh lane) rather than the client request (#5813).
const pfOutgoing = outgoingHeadersOf(input, init) ?? {};
const pfExtra = pfChannel.pfRequestHeaders({ ...pfOutgoing, ...(routedExtra ?? {}) });
const allExtra = { ...(routedExtra ?? {}), ...pfExtra };
captureStampedWindow(allExtra); // #5996: remember this lane's resolved window
let target = url;
Expand Down Expand Up @@ -2119,7 +2122,12 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
// #5418: hosts that dial the proxy directly (dsh profile bundles,
// #809 attach probes) land here already addressed — stamp + learn
// here or their watermark channel silently never seeds.
const selfStamp = withHeaders(input, hoplessForLoopback(url, init), pfChannel.pfRequestHeaders(outgoingHeadersOf(input, init) ?? {}));
// Same merge discipline as the routed branch (#5813): headersFor may
// be the sole source of the conversation header.
const selfRouted = state.headersFor?.(url);
const selfOutgoing = outgoingHeadersOf(input, init) ?? {};
const selfPfExtra = pfChannel.pfRequestHeaders({ ...selfOutgoing, ...(selfRouted ?? {}) });
const selfStamp = withHeaders(input, hoplessForLoopback(url, init), { ...(selfRouted ?? {}), ...selfPfExtra });
const resp = await send(selfStamp.input, selfStamp.init);
pfChannel.pfLearnStamp(outgoingHeadersOf(selfStamp.input, selfStamp.init) ?? {}, resp.status, resp.headers);
noteDispatchMode("proxy");
Expand All @@ -2138,8 +2146,10 @@ export function installNativeFetchIntercept(initialState: NativeInterceptState):
state.onDispatch?.(rewritten, "rewrite");
// #5403/#5418: same single chokepoint as the routed branch — the
// rewrite path is where attach-mode hosts (and the V1 fetch patch)
// ride the proxy without a settings overlay.
const branchExtra = { ...(state.headersFor?.(url) ?? {}), ...pfChannel.pfRequestHeaders(outgoingHeadersOf(input, init) ?? {}), ...(resignExtra ?? {}) };
// ride the proxy without a settings overlay. Same merged-view fix (#5813).
const rwRouted = state.headersFor?.(url) ?? {};
const rwOutgoing = outgoingHeadersOf(input, init) ?? {};
const branchExtra = { ...rwRouted, ...pfChannel.pfRequestHeaders({ ...rwOutgoing, ...rwRouted }), ...(resignExtra ?? {}) };
captureStampedWindow(branchExtra); // #5996: remember this lane's resolved window
try {
const stamped = withHeaders(first, hoplessForLoopback(rewritten, init), branchExtra);
Expand Down
71 changes: 71 additions & 0 deletions tests/pf-channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,77 @@ test("chokepoint: no conversation header ⇒ nothing pf at all", async () => {
assert.equal(sink[0].headers["x-bili-pf-tail-ids"], undefined);
});

// #5813 regression: the dsh lane stamps x-bili-plugin-conversation via
// headersFor, NOT in the client's own request. pfRequestHeaders must see the
// merged view or it can never find the sid and the count header is silently
// dropped every turn (the original bug: 3795 evaluations, 0 hits).
test("chokepoint: conversation header from headersFor (not client) still emits count (#5813)", async () => {
const sid = "sid-headersfor-1";
const { sink } = await withPfPatch(async (fetch, sinkIn) => {
// turn 1: client sends NO conversation header; headersFor provides it
await fetch("http://127.0.0.1:8199/v1/chat/completions", {
method: "POST",
body: "{}",
});
// simulate the proxy stamping the response
pfChannel.pfLearnStamp({ [CONVO]: sid }, 200, new Headers({ "x-bili-pf-seq": "5" }));
pfChannel.pfCaptureIds(sid, ["a", "b", "c", "d", "e", "f"]);
// turn 2: same shape — client still has no convo header
await fetch("http://127.0.0.1:8199/v1/chat/completions", {
method: "POST",
body: "{}",
});
assert.equal(sinkIn.length, 2);
assert.equal(sinkIn[0].headers["x-bili-pf-count"], undefined, "turn 1 unseeded");
}, {
headersFor: () => ({ [CONVO]: sid, "x-bili-plugin": "dsh" }),
});
assert.equal(sink[0].headers[CONVO], sid, "headersFor convo header lands on the wire");
assert.equal(sink[1].headers["x-bili-pf-count"], "5", "count emitted from headersFor-provided sid");
assert.equal(sink[1].headers["x-bili-pf-tail-ids"], "e,f", "tail-ids ride alongside");
});

test("chokepoint self-branch: conversation header from headersFor still emits count (#5813)", async () => {
const sid = "sid-self-headersfor-1";
const { sink } = await withPfPatch(async (fetch, sinkIn) => {
await fetch("http://127.0.0.1:41001/v1/chat/completions", {
method: "POST",
body: "{}",
});
pfChannel.pfLearnStamp({ [CONVO]: sid }, 200, new Headers({ "x-bili-pf-seq": "3" }));
pfChannel.pfCaptureIds(sid, ["x1", "x2", "x3", "x4"]);
await fetch("http://127.0.0.1:41001/v1/chat/completions", {
method: "POST",
body: "{}",
});
assert.equal(sinkIn[0].headers["x-bili-pf-count"], undefined, "self branch turn 1 unseeded");
}, {
headersFor: () => ({ [CONVO]: sid, "x-bili-plugin": "dsh" }),
});
assert.equal(sink[1].headers["x-bili-pf-count"], "3", "self branch emits count from headersFor sid");
assert.equal(sink[1].headers["x-bili-pf-tail-ids"], "x3,x4");
});

test("chokepoint routed branch: conversation header from headersFor still emits count (#5813)", async () => {
const sid = "sid-routed-headersfor-1";
const { sink } = await withPfPatch(async (fetch, sinkIn) => {
// already-routed /bili/ model URL (launcher settings-overlay shape);
// the client carries NO conversation header — headersFor is its sole
// source, exactly like the rewrite/self shapes above
const url = "http://127.0.0.1:41001/bili/http://127.0.0.1:8199/v1/chat/completions";
await fetch(url, { method: "POST", body: "{}" });
pfChannel.pfLearnStamp({ [CONVO]: sid }, 200, new Headers({ "x-bili-pf-seq": "5" }));
pfChannel.pfCaptureIds(sid, ["r1", "r2", "r3", "r4", "r5", "r6"]);
await fetch(url, { method: "POST", body: "{}" });
assert.equal(sinkIn[0].headers["x-bili-pf-count"], undefined, "routed branch turn 1 unseeded");
}, {
headersFor: () => ({ [CONVO]: sid, "x-bili-plugin": "dsh" }),
});
assert.equal(sink[0].headers[CONVO], sid, "headersFor convo header lands on the wire");
assert.equal(sink[1].headers["x-bili-pf-count"], "5", "routed branch emits count from headersFor sid");
assert.equal(sink[1].headers["x-bili-pf-tail-ids"], "r5,r6");
});

// --- openai-chat family resolver (#5403/#5418 protocol-unified capture) ---

test("laneMsgIdsForOpenAiWire: leading system slot gets a position token, rest align to branch", () => {
Expand Down
Loading