High (loop pipeline): agent/step-result now runs before the
assistant/message append so the session log records what tool dispatch
actually uses; abort is honored between tool calls, not just
mid-stream; steering drains at step start, pending steering overrides
a negative turn-continuation decision (/goal pattern), and leftover
steering is re-enqueued as queued messages so it is never stranded;
exceptions from turn-continuation listeners and session/flush are
contained to the turn (error event + agent/error) instead of killing
the driver loop.
Medium: disposal emits agent/status('disposed') and mid-turn disposal
records reason 'disposed'; duplicate LLM adapter registration throws
(all-or-nothing); SessionEvent is a real discriminated union (casts
removed); model-less agents fail with a clear actionable error unless
agent/request supplies a model.
Low: agent/queued and agent/steering carry the resolved MessageSource;
streamBlocks() yields strictly in stream order and flushes delta-only
blocks (matches generate()); BlockAssembler freezes blocks on
block-end and ignores stragglers from malformed streams; turn
numbering is a counter seeded from the log (fork-safe); LoopAgent's
stop disposer is infallible (a throwing status listener cannot skip
registry cleanup); AgentLoop.create uses a generator effect so stop
and unregister are independent disposables; SessionStore wires
onAppend inside its effect.
21 regression tests added (review-fixes.spec.ts), organized by
finding. Docs updated: loop pseudocode (status emissions, ordering,
error containment, steering guarantees) and waterfall composition
caveat in docs/architecture.md; AGENTS.md notes that excessive tests
are welcome.
162 lines
5.1 KiB
TypeScript
162 lines
5.1 KiB
TypeScript
import { Context, Service } from 'cordis'
|
|
import type { ContentBlock, Message, MessageSource } from '@deepseek-ai/dsh-llm'
|
|
import type { SessionEvent, SessionEventMap, SessionEventType } from './types.ts'
|
|
|
|
export * from './types.ts'
|
|
|
|
declare module 'cordis' {
|
|
interface Context {
|
|
sessions: SessionStore
|
|
}
|
|
|
|
interface Events {
|
|
/** A session was created in the store. */
|
|
'session/created'(session: Session): void
|
|
/** An event was appended to a session log (sync, fire-and-forget). */
|
|
'session/event'(session: Session, event: SessionEvent): void
|
|
/**
|
|
* Awaited durability checkpoint. The agent loop awaits
|
|
* `ctx.parallel('session/flush', session)` at every turn end; persistence
|
|
* plugins (JSONL, sqlite — TODO, future phase) drain their write-behind
|
|
* buffers here and on fiber dispose.
|
|
*/
|
|
'session/flush'(session: Session): Promise<void> | void
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Renders a `context/message` or `steering/message` event as a tagged
|
|
* synthetic user-role message (the system-reminder pattern: zero adapter
|
|
* burden, models distinguish it from real user prompts by the envelope).
|
|
*
|
|
* TODO(review): revisit the envelope once a real adapter exists.
|
|
*/
|
|
function renderTagged(tag: string, content: ContentBlock[], source: MessageSource): ContentBlock[] {
|
|
const open = `<${tag} source=${JSON.stringify(source.kind)}>`
|
|
const close = `</${tag}>`
|
|
return [
|
|
{ type: 'text', text: open },
|
|
...content,
|
|
{ type: 'text', text: close },
|
|
]
|
|
}
|
|
|
|
/**
|
|
* An event-sourced session: an append-only log of {@link SessionEvent}s.
|
|
*
|
|
* Plain class (not a Service) — create instances via `ctx.sessions.create()`.
|
|
* Seeding with an existing event log replays/forks a session.
|
|
*/
|
|
export class Session {
|
|
private log: SessionEvent[] = []
|
|
/** Set by the store so appends are observable; no-op when detached. */
|
|
onAppend?: (event: SessionEvent) => void
|
|
|
|
constructor(public readonly id: string, seed?: SessionEvent[]) {
|
|
if (seed) this.log = [...seed]
|
|
}
|
|
|
|
get events(): readonly SessionEvent[] {
|
|
return this.log
|
|
}
|
|
|
|
get seq(): number {
|
|
return this.log.length
|
|
}
|
|
|
|
/** Append one event. Synchronous — the hot path never blocks on I/O. */
|
|
append<T extends SessionEventType>(type: T, data: SessionEventMap[T]): SessionEvent<T> {
|
|
const event = { type, seq: this.log.length, time: Date.now(), data } as SessionEvent<T>
|
|
this.log.push(event)
|
|
this.onAppend?.(event)
|
|
return event
|
|
}
|
|
|
|
/**
|
|
* Derive the LLM message history from the event log.
|
|
*
|
|
* - `user/message` → user message
|
|
* - `assistant/message` → assistant message (chunks are skipped — they are
|
|
* replay/UI data; the assembled message is authoritative for history)
|
|
* - `tool/result` → user message carrying a tool-result block
|
|
* - `context/message` / `steering/message` → tagged synthetic user messages
|
|
* at their chronological position
|
|
*/
|
|
deriveMessages(): Message[] {
|
|
const messages: Message[] = []
|
|
for (const event of this.log) {
|
|
switch (event.type) {
|
|
case 'user/message': {
|
|
messages.push({ role: 'user', content: event.data.content })
|
|
break
|
|
}
|
|
case 'assistant/message': {
|
|
messages.push({ role: 'assistant', content: event.data.content })
|
|
break
|
|
}
|
|
case 'tool/result': {
|
|
const { callId, content, isError } = event.data
|
|
messages.push({
|
|
role: 'user',
|
|
content: [{ type: 'tool-result', toolCallId: callId, content, isError }],
|
|
})
|
|
break
|
|
}
|
|
case 'context/message': {
|
|
const { content, source } = event.data
|
|
messages.push({ role: 'user', content: renderTagged('context', content, source) })
|
|
break
|
|
}
|
|
case 'steering/message': {
|
|
const { content, source } = event.data
|
|
messages.push({ role: 'user', content: renderTagged('steering', content, source) })
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return messages
|
|
}
|
|
}
|
|
|
|
/**
|
|
* In-memory session store (`ctx.sessions`).
|
|
*
|
|
* Persistence is intentionally not implemented here — persistence plugins
|
|
* subscribe to `session/event` and flush on `session/flush` / dispose.
|
|
*/
|
|
export class SessionStore extends Service {
|
|
private store = new Map<string, Session>()
|
|
private counter = 0
|
|
|
|
constructor(ctx: Context) {
|
|
super(ctx, 'sessions')
|
|
}
|
|
|
|
/** Create a session. `seed` replays/forks an existing event log. */
|
|
create(id?: string, seed?: SessionEvent[]): Session {
|
|
id ??= `session-${++this.counter}`
|
|
if (this.store.has(id)) throw new Error(`session "${id}" already exists`)
|
|
const session = new Session(id, seed)
|
|
this.ctx.effect(() => {
|
|
session.onAppend = (event) => this.ctx.emit('session/event', session, event)
|
|
this.store.set(id, session)
|
|
this.ctx.emit('session/created', session)
|
|
return () => {
|
|
session.onAppend = undefined
|
|
this.store.delete(id)
|
|
}
|
|
}, 'sessions.create()')
|
|
return session
|
|
}
|
|
|
|
get(id: string): Session | undefined {
|
|
return this.store.get(id)
|
|
}
|
|
|
|
list(): Session[] {
|
|
return [...this.store.values()]
|
|
}
|
|
}
|
|
|
|
export default SessionStore
|