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
56 changes: 56 additions & 0 deletions devlog/2026-09-27_responses-provider-id-scope/REQ.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# REQ: Preserve provider-issued reasoning IDs in Responses input sanitization

Issue: https://github.com/ranxianglei/billion-context/issues/1474

## Problem

`sanitizeResponsesInputIds` (src/loop/adapter-responses.ts, ingress via
`prepareResponses`, src/server.ts:3444) rewrites **any** Responses `input[]`
item whose string `id` exceeds 64 chars to `msg-fix-<hashId>` — without
checking the item's `type`. Provider-issued opaque items (`reasoning` with
`rs_*` ids, `function_call` with `fc_*` ids, `custom_tool_call`,
`item_reference`, `compaction`) therefore get renamed on every request.
Upstreams that validate id shape/namespace (e.g. the Copilot Responses API:
"Expected an ID that begins with 'rs'") reject the rewritten id with HTTP 400
on every turn, because the client keeps replaying its original long id and
bili rewrites it again each time.

## Root cause

The generic length rewrite was added for #242 (commit 30b33244) to heal
rollouts poisoned by over-long **Bili-generated** ids (pre-hashId round-2
mapping embedded the full 54-char upstream message id → 66 chars). Its scope
was written as "any id" instead of "Bili's own namespace". It renames
identifiers bili does not own; provider-issued ids are validated by their
owner's rules and carry replay correspondence (reasoning id ↔
`encrypted_content`), so they must cross ingress byte-identical.

Current bili never emits ids > 64 chars (all synthesized ids ≤ ~30 chars), so
the only ids that can reach the rewrite path today are historical
`msg-proxy-*` poison or provider-owned ids — the latter being exactly the
breakage.

## Acceptance criteria

1. Length-based rewrite applies ONLY to ids starting with `msg-proxy-`
(the shape #242 healed). Everything else passes ingress byte-identical.
2. No prefix-compliant substitutes (`rs_<hash>` etc.) for provider ids —
prefix compliance ≠ acceptance, and it breaks identity/replay
correspondence.
3. Existing `msg-proxy-*` assistant-message handling (omit the local id on
full-message replay) is unchanged.
4. Regression coverage: over-64 ids on `reasoning`, `function_call`,
`custom_tool_call`, `item_reference` and provider `message` items remain
byte-identical; over-64 `msg-proxy-*` ids keep the deterministic
`msg-fix-*` healing; `tests/responses-proxy-id-replay.test.ts:55-70`
updated to the new expectation.

## Constraints / behavior change disclosure

Old → new: previously ANY over-64 `input[].id` was rewritten to
`msg-fix-*`; afterwards only over-64 ids starting with `msg-proxy-` are.
A client that invented its own >64-char non-`msg-proxy` message ids would no
longer be healed (they pass through as-is). In practice such ids only ever
came from old bili versions (all `msg-proxy-*`) or from the provider itself,
so no real healing case is lost. This wire-shape change requires human review
(§7.4 auto-merge gate: wire/message-shape changes stay human).
31 changes: 31 additions & 0 deletions devlog/2026-09-27_responses-provider-id-scope/WORKLOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Worklog

## 2026-09-27

- Root cause: `sanitizeResponsesInputIds` (src/loop/adapter-responses.ts)
rewrote ANY over-64-char `input[].id` to `msg-fix-<hashId>` regardless of
item type. The generic scope came from the #242 healing (30b33244), whose
poisoning class is specifically over-long `msg-proxy-*` ids — but it also
renames provider-issued opaque ids (reasoning `rs_*`, function_call
`fc_*`, custom_tool_call, item_reference, compaction), which upstreams with
id-shape validation (Copilot Responses API) reject with HTTP 400 every
turn (#1474).
- Fix: gate the length rewrite on `rec.id.startsWith("msg-proxy-")`.
Provider/client-owned ids now cross ingress byte-identical (wire fidelity);
Bili's own namespace keeps the deterministic #242 healing. Branch 1
(`msg-proxy-*` assistant message → omit id on replay) untouched. No
prefix-compliant substitutes introduced (prefix compliance ≠ acceptance;
renaming breaks reasoning id ↔ encrypted_content correspondence).
- Key files: src/loop/adapter-responses.ts (doc comment + branch-2 gate),
tests/responses-proxy-id-replay.test.ts (stale generic-rewrite test
replaced by byte-identical provider-item coverage + scoped-healing test),
tests/responses-round2-lifecycle.test.ts (#242 regression, unchanged,
still green).
- Verified: rebased onto master @ cae30bb3 (post-#1448; target function
verified unchanged there) — typecheck ✅, full suite 2884 pass / 0 fail /
12 skip (gated e2e) ✅, build ✅. Real-client E2E runs in CI (diff touches
src/loop/**).
- Behavior change disclosed in PR + issue reply: previously any over-64
input id was rewritten; now only over-64 `msg-proxy-*` ids are. Non-Bili
invented >64-char ids are no longer healed (they did not exist outside
old bili versions, which all used `msg-proxy-*`).
13 changes: 9 additions & 4 deletions src/loop/adapter-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ interface FunctionCallBuffer {
const RESPONSES_ITEM_ID_MAX = 64;

/**
* Heal client rollouts already poisoned with over-long ids (they 400 every
* request otherwise). Rewrites in place, deterministically, so repeated
* requests keep referencing the same replacement id (#242).
* Heal client rollouts already poisoned with over-long Bili-generated ids
* (they 400 every request otherwise). Only the msg-proxy-* namespace bili
* owns is rewritten — in place and deterministically, so repeated requests
* keep referencing the same replacement id (#242). Provider-issued opaque
* ids (reasoning rs_*, function_call fc_*, ...) validate against their
* owner's shape rules and carry replay correspondence, so they must reach
* the upstream byte-identical (#1474).
*/
export function normalizeResponsesMessageItems(input: unknown): number {
if (!Array.isArray(input)) return 0;
Expand Down Expand Up @@ -63,7 +67,8 @@ export function sanitizeResponsesInputIds(input: unknown): void {
delete rec.id;
continue;
}
if (typeof rec?.id === "string" && rec.id.length > RESPONSES_ITEM_ID_MAX) {
if (typeof rec?.id === "string" && rec.id.startsWith("msg-proxy-")
&& rec.id.length > RESPONSES_ITEM_ID_MAX) {
rec.id = `msg-fix-${hashId(rec.id)}`;
}
}
Expand Down
37 changes: 30 additions & 7 deletions tests/responses-proxy-id-replay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,44 @@ test("native ids, references, non-assistant items and incomplete messages remain
}
});

test("existing 64-character boundary and long-id healing stay unchanged", () => {
const input = [
test("provider-issued over-64 ids stay byte-identical across item types (#1474)", () => {
const longId = "x".repeat(80);
const input: unknown[] = [
{ type: "message", role: "assistant", id: longId, content: "answer" },
{ type: "message", role: "user", id: longId, content: "question" },
{ type: "reasoning", id: `rs_${"a".repeat(100)}`, encrypted_content: "opaque", summary: [] },
{ type: "function_call", id: `fc_${"b".repeat(100)}`, call_id: `call_${"c".repeat(80)}`, name: "tool", arguments: "{}" },
{ type: "custom_tool_call", id: `ctc_${"d".repeat(100)}`, call_id: "call_2", name: "tool", input: "text" },
{ type: "item_reference", id: `item_${"e".repeat(100)}` },
{ type: "compaction", id: `comp_${"f".repeat(100)}`, encrypted_content: "opaque" },
];
const expected = structuredClone(input);
sanitizeResponsesInputIds(input);
assert.deepEqual(input, expected, "ids bili does not own reach the upstream byte-identical");
});

test("over-64 id healing stays scoped to the msg-proxy namespace (#242/#1474)", () => {
const poisoned = `msg-proxy-2-${"x".repeat(60)}`;
const input: Record<string, unknown>[] = [
{ type: "message", role: "assistant", id: "x".repeat(64), content: "answer" },
{ type: "message", role: "assistant", id: "x".repeat(65), content: "answer" },
{ type: "function_call", id: "x".repeat(65), call_id: "y".repeat(80) },
{ type: "message", role: "user", id: poisoned, content: [] },
];
sanitizeResponsesInputIds(input);
assert.equal(input[0].id, "x".repeat(64));
assert.match(input[1].id, /^msg-fix-/);
assert.ok(input[1].id.length <= 64);
assert.equal(input[2].id, input[1].id);
assert.equal(input[0].id, "x".repeat(64), "64-char boundary preserved");
assert.equal(input[1].id, "x".repeat(65), "over-64 ids outside Bili's namespace are untouched");
assert.equal(input[2].id, "x".repeat(65));
assert.equal(input[2].call_id, "y".repeat(80));
const healed = String(input[3].id);
assert.match(healed, /^msg-fix-/);
assert.ok(healed.length <= 64);
const expected = structuredClone(input);
sanitizeResponsesInputIds(input);
assert.deepEqual(input, expected);
assert.deepEqual(input, expected, "cleanup is idempotent");
const again: Record<string, unknown>[] = [{ type: "message", role: "user", id: poisoned, content: [] }];
sanitizeResponsesInputIds(again);
assert.equal(String(again[0].id), healed, "rewrite is deterministic per source id");
});

test("emitText lifecycle keeps its local id while the next full-message replay omits it", () => {
Expand Down
Loading