Skip to content

[PR] fix: invariant judge flags stranded tool-use/tool-result pairs at egress (#4862) - #5003

Closed
ranxianglei wants to merge 2 commits into
masterfrom
2026-10-28_judge-tool-pairing-4862
Closed

ranxianglei wants to merge 2 commits into
masterfrom
2026-10-28_judge-tool-pairing-4862

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

What & why

#4862's production logs showed the exact failure shape this closes: invariant-judge reported clean fold verdict on every request that upstream then rejected with 400 tool_use ids were found without tool_result blocks immediately after. The judge's I1/I2 invariants cover rewrite/drop/insert/fold-boundary drift but never tool-pairing integrity — provider validity outlives the fold boundary, so a misapplied fold can strand one side of a pair while I1/I2 stay green.

#4774 removes the known trigger source (header-less claude-code subagents no longer ride the parent session id); this PR closes the residual egress gap named in its review: any future path that splits a pair now surfaces as a judge violation instead of a silent clean verdict.

How

src/invariant-judge.ts gains I3 (exit pairing):

  • pairingOrphans() scans one side of the wire for stranded calls (tool-call id with no tool-result before the next assistant turn) and stranded results (tool-result id with no call anywhere).
  • Attribution-gated (the judge's charter is "whose fault"): an orphan whose twin is already orphaned inbound is client-originated state forwarded verbatim — not a rewrite. Twin matching rides the module's own identity relation (stableEqual), so CCR/crush/truncation substitutes and re-minted ids (KDD#11) match by content, never by raw bytes or id.
  • Advisory only, exactly like I1/I2: same invariantJudgeEnabled arm/disarm, warns through the existing sink, never blocks traffic, never touches bytes.

Behavior change disclosure

before after
Request where bili strands one side of a tool pair judge logs clean ... verdict (debug); first signal is the upstream 400 judge logs I1/I2 violation: unpaired-tool-call / unpaired-tool-result (warn) before egress

No traffic behavior, wire bytes, config surface, or persistence change — observability only, on the existing default-armed advisory layer.

Tests

  • 7 unit rows in tests/invariant-judge.test.ts, both directions per gate discipline: POS (paired round-trip; openai multi-message parallel answers; summary carrier inside the answer region; verbatim client-originated orphan; re-minted orphan — KDD#11) and NEG (the exact [Bug][claude] 0.1.207: subagent requests fail with 400 "tool_use ids were found without tool_result blocks" right after a fold in the parent session #4862 shape: carrier replaces only the result side while I1/I2 stay green → red via pairing; stranded result direction).
  • E2E live proof in tests/invariant-judge-e2e.test.ts on the REAL anthropic codec: paired tool_use/tool_result round-trip stays clean (proves both codecs preserve toolCallId), fault-injected stranded call fires unpaired-tool-call.
  • The existing suite caught one false positive during development (CCR placeholder twin vs full-text inbound under an orphaned result) — fixed by switching twin matching from raw text signatures to stableEqual; all pre-existing judge tests pass unchanged.

Duplicate screening

查重: 无 — searched open+closed issues/PRs for an invariant-judge pairing/egress-gap entry; none exists. Related: #4862 (symptom + checklist), #4774 (trigger removal), #3842/#3938 (judge design), #2821/#4186 (identity/exemption doctrine reused here).

Part of #4862.

…ess (#4862)

#4862 leaves one mechanism after #4774 removes its trigger: the outbound
invariant judge (I1/I2) never checks tool_use/tool_result pairing, so a
payload whose pair a misapplied fold split still earns a "clean fold
verdict" while upstream 400s on every retry.

Add I3 (exit pairing) to src/invariant-judge.ts:
- pairingOrphans() scans one side of the wire for stranded calls (no result
  before the next assistant turn) and stranded results (no call at all);
- attribution-gated: only BILI-introduced breaks count — an orphan whose twin
  is already orphaned inbound matches via stableEqual (CCR/crush/truncation
  substitutes, re-minted ids per KDD#11), keeping client-originated state and
  codec-dependent id gaps out of the violation set;
- advisory only: same arm/disarm as I1/I2, warns through the existing sink,
  never blocks traffic or touches bytes.

Tests: 7 unit rows (POS: paired round-trip, openai multi-message answers,
carrier inside answer region, verbatim client orphan, re-minted orphan; NEG:
the #4862 carrier-replaces-result-side shape, stranded result) plus an e2e
live proof on the real anthropic codec. The existing suite caught one false
positive during development (CCR placeholder twin) — fixed by riding
stableEqual instead of raw text signatures.

Part of #4862 (egress-gap sub-defect; trigger removal = #4774).
@ranxianglei ranxianglei changed the title fix: invariant judge flags stranded tool-use/tool-result pairs at egress (#4862) [PR] fix: invariant judge flags stranded tool-use/tool-result pairs at egress (#4862) Oct 27, 2026
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-28_judge-tool-pairing-4862 (bdc32ad)

Latest build — install this exact version

npm install -g billion-context@0.0.6550-pr.5003.233a1d8

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Recent builds (commit → version)

commit version build
bdc32ad billion-context@0.0.6550-pr.5003.233a1d8 build
55a24b6 billion-context@0.0.6541-pr.5003.92fda28 build

Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr5003.tgz
npm install -g package

Install instructions are refreshed on each push.

…nvention (#5003 review)

Review nit: the I3 block, pairingOrphans() and the re-touched
exemption-overflow/return lines landed at 4-space indent against the
module's 2-space style, adding whitespace churn to the diff.
Whitespace-only, zero behavior change (git diff -w empty).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant