refactor(core): extract session model request preparation#37210
Merged
Conversation
e6f461c to
1c32cb5
Compare
6210730 to
8df62f2
Compare
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.
What
Extract provider-request preparation from
SessionRunnerLLMinto a Location-scopedSessionModelRequestmodule.The runner now owns durable orchestration only: instruction sync, pending promotion, compaction, provider streaming, durable publication, retries, and tool settlement.
SessionModelRequest.prepareowns the complete model-request shape: system and history messages, selected model headers, prompt-cache identity, tool definitions, Step limits, and Session context hooks.This is the next behavior-preserving slice toward transient
session.generate; it adds no public operation yet.How
packages/core/src/session/model-request.tsprepares one canonicalLLMRequestfromSessionContext.Loaded.resolveToolCall(name)capability, so the runner does not reconstruct hook-removal or Step-limit policy.LLM.requestnormalization, avoiding the previous construct-then-reconstruct pass while preserving the same canonical hook values.plans/session-generate.mduses the same outbound model-request vocabulary.packages/core/src/session/runner/llm.tsdelegates preparation and otherwise preserves its existing provider-event and durability flow.Three simplify passes reviewed reuse, interface depth, behavioral equivalence, and hot-path allocations. The final pass removed the speculative one-case operation wrapper, hook-removal runner branches, duplicate history copies, duplicate request normalization, and redundant blocked-tool state.
Scope
This PR does not add
session.generate, alter Session context selection, change instruction persistence, initiate transient compaction, or expose a public/plugin/API surface.The first operation remains a durable Step. A later caller can add transient generation behind the same module once read-only instruction assembly and revisioned Session snapshots are available.
Testing
bun typecheckinpackages/corebun run testinpackages/core: 1,313 passed, 6 skippedbun run test session-runner.test.ts session-runner-recorded.test.ts session-runner-tool-registry.test.ts plugin-hooks.test.ts: 146 passedbunx prettier --check packages/core/src/session/model-request.ts packages/core/src/session/context.ts packages/core/src/session/runner/llm.ts plans/session-generate.mdbunx ast-grep scan -c script/ast-grep/sgconfig.yml packages/core/src/session/model-request.ts packages/core/src/session/context.ts packages/core/src/session/runner/llm.tssession/model-request.ts; two pre-existing warnings remain in unchanged manual-compaction code insession/runner/llm.tsFlow
sequenceDiagram participant Runner as SessionRunner participant Request as SessionModelRequest participant Hooks as Session context hooks participant LLM as LLMClient participant Tools as Tool settlement Runner->>Request: prepare(context, step) Request->>Hooks: transform system, messages, tools Hooks-->>Request: mutated request draft Request-->>Runner: request + tool-call capability Runner->>LLM: stream(request) LLM-->>Runner: LLM events Runner->>Request: resolveToolCall(name) Request-->>Runner: reject or settle capability Runner->>Tools: settle(call)