Skip to content

Lazily prepare bridged train prompts - #1801

Merged
xeophon merged 1 commit into
feat/nano-as-v1from
codex/v1-lazy-bridge-preparation
Jun 21, 2026
Merged

Lazily prepare bridged train prompts#1801
xeophon merged 1 commit into
feat/nano-as-v1from
codex/v1-lazy-bridge-preparation

Conversation

@xeophon

@xeophon xeophon commented Jun 21, 2026

Copy link
Copy Markdown
Member

Overview

Reduce training-client preparation work for incremental renderer bridges by reusing the typed prompt already owned by the intercepted turn and materializing the full wire prompt only when fallback rendering is required.

Why

The interception path already parses the request into PendingTurn.prompt. The train client nevertheless parsed every raw message again, discarded that duplicate typed prompt, and converted the complete authoritative prompt back to wire dictionaries.

On a successful renderer bridge, generate() receives prebuilt prompt_ids and does not render or otherwise consume those full wire messages. The context-wide parse and conversion were therefore pure transient work on the bridge-hit path.

Changes

  • Reuse turn.prompt and parse only the request's tool definitions when a turn is available.
  • Convert only turn.tail before checking and invoking the incremental bridge.
  • Build complete wire messages lazily when the bridge is ineligible, has no usable token anchor, or returns None.
  • Preserve full request parsing for calls without a turn and preserve exact full-prompt rendering for every fallback path.

Performance

A PEP 723 benchmark exercised a successful bridge over 20,001 messages for nine measured loops, using time.perf_counter() for wall time and tracemalloc for Python allocation peak.

Metric Before After Absolute saving
Median preparation time 85.712 ms 0.950 ms 84.762 ms
Median traced allocation peak 9.170 MiB 0.010 MiB 9.160 MiB

This is a 90.22× speedup, 98.89% less wall time, and 99.89% lower traced peak allocation for the measured bridge-hit workload.


Note

Low Risk
Localized optimization in train-client preparation with explicit preservation of full parsing/rendering on non-bridge paths; no auth, security, or persistence changes.

Overview
Train client prompt prep on intercepted turns no longer re-parses the full request or converts the entire prompt to wire form when an incremental renderer bridge succeeds.

When a PendingTurn is present, the client reuses turn.prompt and only parses tool definitions from the body. It wires just turn.tail for bridge eligibility checks and bridge_to_next_turn, since bridged generate() calls use prebuilt prompt_ids and do not need the full message list. Full wire messages are built only when bridging is skipped or fails (prompt_ids is None). Calls without a turn still use dialect.parse_request unchanged.

Reviewed by Cursor Bugbot for commit da20e33. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Lazily prepare full prompt in TrainClient.get_response for bridged train turns

  • When a PendingTurn is present, get_response now assigns prompt directly from turn.prompt and parses tools via parse_tools, skipping a full dialect.parse_request call.
  • In the bridged path, only the tail messages are passed to bridge_to_next_turn and generation; full wire prompt rendering is deferred to the fallback path when bridging fails.
  • _is_valid_incremental_tail now receives the already-tail-only wire_messages directly instead of slicing a full message list.
  • Behavioral Change: prompt IDs from a successful bridge bypass full prompt rendering entirely; fallback behavior is unchanged.

Macroscope summarized da20e33.

@macroscopeapp

macroscopeapp Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved

Performance optimization that defers prompt conversion until needed. The changes are self-contained, use existing utilities, and don't alter the logical flow - only when computation occurs.

You can customize Macroscope's approvability policy. Learn more.

@xeophon
xeophon merged commit c05bd7d into feat/nano-as-v1 Jun 21, 2026
5 checks passed
pull Bot pushed a commit to Stars1233/verifiers that referenced this pull request Jun 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant