feat(diagnostics): structured bridge-failure reasons#16
Open
mvanhorn wants to merge 1 commit into
Open
Conversation
bridge_to_next_turn returns list[int] | None. When the bridge bails, the caller has no way to learn which of the 6 README-documented failure modes hit. Adds renderers.diagnostics.diagnose_bridge that returns a typed BridgeDiagnostic so prime-rl and verifiers can observe bridge health per-turn during rollouts instead of discovering 32/64 silent drops after training. - renderers/diagnostics.py (~245 LOC): BridgeFailureReason StrEnum covering all 6 documented modes plus UNKNOWN_TEMPLATE_CLOSE for the DefaultRenderer fall-through. BridgeDiagnostic dataclass carries the reason, message_index, token_span, and a short detail string suitable for a logger.info line. diagnose_bridge orchestrates: contract-level checks first (assistant in extension, default renderer, truncation_zeroed_anchor), then runs the bridge and a fresh render and classifies the first divergent token. Per-renderer hints (Qwen3 tool_call_id, GPT-OSS harmony channels) intentionally stay out of the protocol; the fall-through is BPE_DRIFT, which empirically covers the majority case. - tests/test_diagnostics.py (~155 LOC): pure-function tests that exercise every branch with a small _StubRenderer rather than a real tokenizer, so the diagnostic suite runs in 30ms without HuggingFace model downloads. - renderers/__init__.py: exports BridgeFailureReason, BridgeDiagnostic, and diagnose_bridge. No changes to the Renderer protocol; no new dependencies. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
bridge_to_next_turnreturnslist[int] | None. When it returnsNone, the caller has no way to know which of the 6 README-documentedfailure modes hit. The README's own empirical table (Qwen3.5-35B-A3B
apply_chat_template— discovery today requires noticing 77-vs-64training samples after the run.
Demo
13 seconds: the current
Nonereturn, the 7-reason enum, an exampleBridgeDiagnostic, and the per-turn log line it enables.What changes
renderers/diagnostics.py:BridgeFailureReasonStrEnumcoversthe 6 documented modes (
ASSISTANT_IN_EXTENSION,TRUNCATION_ZEROED_ANCHOR,BPE_DRIFT,BOOL_ROUND_TRIP,TOOL_CALL_XML_DRIFT,THINKING_STRIPPED) plusUNKNOWN_TEMPLATE_CLOSEfor theDefaultRendererfall-through.BridgeDiagnosticdataclass carries(reason, message_index, token_span, detail)suitable for onelogger.infoline per turn.diagnose_bridge(renderer, prev_prompt_ids, prev_completion_ids, new_messages, *, tools=None) -> BridgeDiagnostic | NonereturnsNonewhen the bridge succeeds cleanly. Otherwise:reject_assistant_in_extension→ASSISTANT_IN_EXTENSION,DefaultRenderer→UNKNOWN_TEMPLATE_CLOSE,len(prev_prompt) > tokenizer.model_max_length→TRUNCATION_ZEROED_ANCHOR.bridge_to_next_turnandrender_ids, locatethe first divergent token. Classify single-token bool literals as
BOOL_ROUND_TRIP; fall through toBPE_DRIFT(empiricalmajority). Per-renderer hints (Qwen3
tool_call_id, GPT-OSSharmony channels) intentionally stay outside the protocol.
tests/test_diagnostics.py: 9 pure-function tests driven by a small_StubRendererso the suite runs in ~30ms without HuggingFacedownloads. Covers every branch.
renderers/__init__.py: exportsBridgeFailureReason,BridgeDiagnostic,diagnose_bridge.Surface stability
RendererProtocol.enum.StrEnum(3.11+) anddataclasses.dataclass.test_enum_str_values_stable)so downstream log consumers and dashboards stay stable across
refactors.
Note on placement
Diagnostics could live in
verifiersinstead. The reasons live withthe renderer's bridge logic (and reuse base.py helpers like
reject_assistant_in_extension), so this PR keeps them here.Happy to move it across if you'd rather verifiers / prime-rl own
the surface, or to add a docs page (
Bridge Diagnostics) alongsidethe existing
Bridge Contractpage.Built with Claude Code (Opus 4.7).