Status: In progress
Add a Session operation that prepares one request from the Session's active model context, appends a transient prompt, executes exactly one Physical Attempt, returns the assistant text, and leaves the Session unchanged:
const text = yield * session.generate({ sessionID, prompt: "Summarize where we left off." })
The operation belongs to Session because its meaning depends on Session History, the selected agent and model, instructions, provider transforms, hooks, and prompt-cache identity. The existing stateless Generate.text module remains unaware of Session.
This feature should deepen the Session request-preparation module rather than add a second approximation of the runner. The durable runner and session.generate must share preparation, then diverge before provider output acquires durable consequences.
SessionRunner.attemptStep currently interleaves six distinct concerns:
session.generate needs the middle of that sequence without the durable work on either side. Calling session.prompt would admit durable input and enter the full loop. Calling Generate.text would lose Session context and cache identity. Forking would preserve context but create temporary durable state and cleanup obligations.
The desired architecture makes request preparation independently callable while preserving one canonical implementation.
session.prompt
-> SessionAdmission
-> SessionExecution
-> SessionContext.select/load
-> SessionModelRequest.prepare
-> LLMClient.stream
-> SessionSettlement
session.generate
-> SessionContext.select
-> SessionHistory.preview
-> SessionGenerate request construction
-> LLMClient.generate
-> return text
The modules have distinct jobs:
SessionAdmission records and promotes durable input.SessionContext resolves a read-only, internally consistent view of what the selected agent would see.SessionModelRequest converts durable Step context into a provider request paired with tool capability.SessionGenerate owns the one tool-free transient request shape rather than threading generation through the durable modules.LLMClient executes one provider request without deciding what becomes durable.SessionSettlement gives streamed provider events their durable Session meaning, executes local tools, records usage, and decides continuation.SessionCompaction replaces oversized active history and remains a durable Session operation.Durability becomes a property of admission and settlement, not request preparation or provider execution.
The Location-scoped request module keeps its durable Step interface:
interface SessionModelRequest {
readonly prepare: (input: { context: SessionContext.Loaded; step: number }) => Effect<PreparedSessionModelRequest>
}
type PreparedSessionModelRequest = {
request: LLM.Request
resolveToolCall: (name: string) => ToolCallResolution
}
prepare hides:
Durable prepare advertises permitted tools and returns the capability used by settlement, except when the agent's Step limit disables tools. SessionGenerate constructs its request with no tool definitions and tool choice none, independently of agent permissions. Avoid a generic step | generate | compaction operation union or independently selectable behavior flags; the two operations own their concrete request shapes.
Request preparation must not call InstructionState.prepare, promote pending input, or initiate compaction. Those operations mutate the Session.
Split instruction behavior into two phases:
resolve and assemble current instruction context read-only
commit the canonical model's instruction state durable
Both a durable Step and session.generate resolve and assemble instructions. Only durable execution commits instruction-state changes. A transient request must not make the false durable claim that the canonical Session model saw an instruction update.
The active history and committed instruction state are loaded from one consistent database view. A concurrent durable Step may advance the Session afterward; the already prepared request remains immutable. No reusable snapshot or revision protocol is needed for this operation.
Pending inputs remain excluded. They are not Session History until promotion, and session.generate must not alter admission order or expose queued work early.
If the Session currently has an unsettled assistant message, generation stops history at that boundary. In particular, it never appends the transient user prompt after an unresolved tool call.
Use the existing LLMClient interface directly. The durable runner consumes llm.stream(prepared.request), while session.generate calls llm.generate(prepared.request) to collect the same event stream into its existing LLMResponse model. No additional provider-attempt module is needed.
LLMClient.generate collects exactly one provider stream. It does not retry as a new logical Step, execute tools, continue after tool calls, publish Session events, capture filesystem snapshots, or update Session usage.
The internal result is the collected text string. Keeping richer evidence internal avoids prematurely committing a public transport contract.
If a provider returns tool calls, collection records and ignores them. No tool hook or execution path runs. Assistant text, including an empty string, remains a successful result. Empty text is required for cache-warming calls.
Normal Step preparation may discover that active context requires compaction. Compaction is durable and usually requires another provider call. Automatically compacting from session.generate would violate both transcript immutability and the exactly-one-attempt contract.
The first operation should use history after the latest completed compaction and fail with a typed context-overflow error when that snapshot cannot fit. It must not initiate compaction.
Transient in-memory compaction can be considered later as a separate operation or explicit policy. It should not silently weaken the first contract.
The first contract should state:
session.generateuses the latest committed model context captured when request preparation begins. Later Session changes do not alter the in-flight request.
The operation does not acquire ownership of the durable Session Drain and does not fail merely because the Session is running. This keeps transient generation independent from durable scheduling.
A later recap integration can suppress stale output by comparing the Session aggregate sequence captured by that caller. Core does not expose a generic revision protocol before a concrete consumer needs one.
The existing Session context hook runs because it participates in normal request preparation. Admission, projection, Session settlement, and tool-execution hooks do not run because those stages do not occur. Add operation metadata only when a concrete hook consumer needs it; do not introduce a speculative operation union.
The no-mutation guarantee covers OpenCode's durable Session state. Arbitrary plugin hooks may still perform external side effects.
Start with the smallest useful interface:
type SessionGenerateInput = {
sessionID: SessionID
prompt: string
}
The operation:
prompt only to the in-memory provider request.Files, agent attachments, usage, model identity, finish metadata, and revision are follow-up extensions. They should be added only when a concrete caller needs them.
Each stage should preserve existing durable runner behavior before the next stage starts.
Add focused tests around a normal Step's prepared request. Pin:
Use a recording LLM adapter rather than reproducing request-construction logic in tests.
Move instruction source loading and read-only assembly behind one internal interface. Keep InstructionState.prepare in the durable runner path. Verify that normal Steps produce byte-equivalent instruction context and unchanged durable instruction events.
This commit should not add session.generate.
Move model resolution, history selection, request construction, tool definitions, cache identity, and context hooks into the Location-scoped SessionModelRequest module. Make the durable runner its only caller first.
Verify the recorded normal request before and after extraction. Keep compaction detection and pending promotion outside the new module if putting them inside would make preparation mutate state.
Make the runner explicitly pass llm.stream(prepared.request) into durable settlement. Keep event publication, tool execution, snapshots, retries, usage, and continuation behavior unchanged.
This stage should make the durable path read as orchestration:
promote -> snapshot -> compact if required -> prepare -> attempt -> settle
Session.generate OperationUse read-only context and request preparation, append one transient user message, disable tools, collect one attempt, and return text. Add tests proving that messages, pending inputs, instruction state, Session events, and usage remain unchanged.
Test concurrent Session advancement with deterministic synchronization around request dispatch. The generated request should retain its captured context while the source Session advances independently.
Add POST /api/session/:sessionID/generate to Protocol, a thin Server handler, generated Promise and Effect clients, and ctx.session.generate in the V2 plugin context. Regenerate clients from the assembled HttpApi; do not edit generated files manually.
Implement recap outside Core using session.generate. The recap integration owns idle/focus policy, the recap prompt, stale-result suppression, output cleaning, and display. Core owns only session-authentic transient generation.
The implementation is complete when tests establish these laws:
session.generate leaves Session messages and pending inputs unchanged.llm.generate invocation and no continuation."".This path mutates Session History, enters the agent loop, may execute tools, and changes usage. Deleting projected messages afterward cannot undo durable events or external effects.
A fork is useful for a prototype but creates durable state, emits events, requires reliable deletion, and can execute tools unless the normal runner is changed anyway. It does not establish the reusable preparation seam.
Generate.text intentionally knows nothing about Session. Teaching it Session semantics reverses the intended dependency and duplicates request preparation outside the runner.
persist: false Runner ModePersistence, tool execution, admission, compaction, and continuation are separate behaviors. One flag would leave callers responsible for understanding unsafe combinations and would keep the current concerns interleaved.
The narrow implementation is expected to touch Core Session and runner modules, Protocol, Server, generated clients, and the V2 plugin context. It should require no database migration and no new durable event.
The architectural work is larger than the endpoint. Most risk lies in extracting instruction and request preparation without changing normal Step behavior. The staged sequence keeps that risk observable and gives every new seam two real callers before broadening its interface.