feat: add canonical session context boundaries

Make SessionManager projections authoritative for provider requests, add append-only context edits and actionable turn boundaries, and preserve existing queue scheduling during continuation and recovery.
This commit is contained in:
Mario Zechner 2026-09-21 16:29:12 +02:00
parent 10d1ad621f
commit 466db0fecd
30 changed files with 3636 additions and 308 deletions

View file

@ -91,6 +91,8 @@ interface AgentSession {
// State access
agent: Agent;
sessionManager: SessionManager;
refreshContext(): void;
model: Model | undefined;
thinkingLevel: ThinkingLevel;
messages: AgentMessage[];
@ -251,8 +253,8 @@ const state = session.agent.state;
// state.streamingMessage?: AgentMessage - current partial assistant message
// state.errorMessage?: string - latest assistant error
// Replace messages (useful for branching or restoration)
session.agent.state.messages = messages; // copies the top-level array
// Model-visible messages are projected from session.sessionManager.
// agent.state.messages is a refreshed inspection cache; do not assign it for restoration.
// Replace tools
session.agent.state.tools = tools; // copies the top-level array
@ -261,6 +263,15 @@ session.agent.state.tools = tools; // copies the top-level array
await session.agent.waitForIdle();
```
Provider requests use `session.sessionManager` as the canonical finalized context. Assigning `session.agent.state.messages` does not replace persisted context and may be overwritten at the next request boundary. Restore externally stored history when constructing the session instead:
```typescript
const restoredManager = SessionManager.inMemory(process.cwd(), { id: sessionId }, entries);
const { session } = await createAgentSession({ sessionManager: restoredManager });
```
For an existing session, use `session.navigateTree(entryId)` to move its active branch. Use `session.sessionManager.appendMessage(...)` plus `session.refreshContext()` only when intentionally appending externally managed entries.
### Events
Subscribe to events to receive streaming output and lifecycle notifications.