🤖 refactor: deepen streamContextBuilder into a TurnContextAssembler - #4011
Open
ibetitsmike wants to merge 7 commits into
Open
🤖 refactor: deepen streamContextBuilder into a TurnContextAssembler#4011ibetitsmike wants to merge 7 commits into
ibetitsmike wants to merge 7 commits into
Conversation
Mechanical rename only; behavior is unchanged.
Move provider history slicing and filtering into the prompt assembler module.
Centralize message conversion and cache breakpoints, and make StreamManager consume the assembled payload.
Route replay reconstruction through the production prompt assembler and centralize MCP warning formatting.
Move request preparation and cache placement coverage to TurnContextAssembler and remove hop-coupled assertions.
Align module and replay documentation with the centralized prompt assembler.
Drop payload fields no caller reads, share per-turn assembler closures across the initial build and rebuild paths, refresh stale StreamManager cache comments, and table-drive the ancestor plan tests.
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
This comment has been minimized.
This comment has been minimized.
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.
Summary
One module now owns per-turn prompt synthesis end to end.
src/node/services/turnContextAssembler.ts(renamed fromstreamContextBuilder.ts) assembles the complete provider payload behind a singleassemblePromptPayload(history, state, options) -> PromptPayloadseam: ADR-0003 boundary slicing, interrupted sentinels, the message pipeline conversion, and every cache breakpoint (message-level, system row, tools). AIService's primary, refusal-fallback, thinking-override, and step-0 rebuild paths, plus replay reconstruction, all converge on that one entry point, and StreamManager now consumes provider-ready payloads.Background
Answering "what does the model actually see" previously traced a 9-hop chain across AgentSession, AIService, streamContextBuilder, messagePipeline, and StreamManager, and
replay/replayRequestBuilder.tsmaintained a lockstep hand-copy of the same sequence. streamContextBuilder was shallow: it owned system-prompt snippets while slicing, sanitation, sentinels, and cache breakpoints lived elsewhere. Refactor #2 of the 2026-08-29 architecture review series (evidence measured at main f04e0f8). The ADR-0003 boundary model itself is unchanged; only its request-path invocation moved.Implementation
prepareProviderRequestMessages(boundary slicing + workflow-row/empty-assistant filtering) moved from aiService into the assembler.assemblePromptPayloadcomposes slicing,addInterruptedSentinel,prepareMessagesForProvider(messagePipeline stays a pure library), and cache application (createCachedSystemMessage/createOpenAICachedSystemMessage/applyCacheControlToTools) into onePromptPayload.Net LOC (production and tests, stated separately)
The only irreducible addition is the
PromptPayloadinterface and assembler entry point, which replace the five duplicated assembly sequences.Validation
make static-checkgreen; whole-file runs green: aiService (96), streamManager (111), turnContextAssembler (19), messagePipeline (17), replayVerify (7+3).Risks
Touches the request-assembly path for every turn, including refusal-fallback and mid-turn thinking rebuilds; a regression here would malform provider requests broadly. Mitigation: byte-for-byte replay verification reconstructs real requests through the same production entry point, and the UAT matrix covered the fallback-relevant scenarios live.
Generated with
xum• Model:anthropic:claude-fable-5• Thinking:xhigh• Cost:$56.70