Lazily prepare bridged train prompts - #1801
Merged
Merged
Conversation
Contributor
ApprovabilityVerdict: 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. |
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.
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 prebuiltprompt_idsand 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
turn.promptand parse only the request's tool definitions when a turn is available.turn.tailbefore checking and invoking the incremental bridge.None.Performance
A PEP 723 benchmark exercised a successful bridge over 20,001 messages for nine measured loops, using
time.perf_counter()for wall time andtracemallocfor Python allocation peak.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
PendingTurnis present, the client reusesturn.promptand only parses tool definitions from the body. It wires justturn.tailfor bridge eligibility checks andbridge_to_next_turn, since bridgedgenerate()calls use prebuiltprompt_idsand 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 usedialect.parse_requestunchanged.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_responsefor bridged train turnsPendingTurnis present,get_responsenow assignspromptdirectly fromturn.promptand parses tools viaparse_tools, skipping a fulldialect.parse_requestcall.bridge_to_next_turnand generation; full wire prompt rendering is deferred to the fallback path when bridging fails._is_valid_incremental_tailnow receives the already-tail-onlywire_messagesdirectly instead of slicing a full message list.Macroscope summarized da20e33.