fix(agent-loop): append step/start before emitting agent/step-start (P1-6)

Every loop boundary appends the session event before emitting the Cordis
event (ADR 0003's append-before-emit rule) — except step/start, which was
inverted. A listener on agent/step-start that inspected session.events could
not see the step it was just told had started.

- Swap the two lines so session.append('step/start') precedes the emit.
- Fix the two stale pseudo-code copies (the runLoop JSDoc STEP-loop block and
  docs/architecture.md) so neither shows step-start emitted before the append.
- Regression test: a step-start listener observes the matching step/start
  event already at the tail of session.events. Verified the test fails on the
  pre-fix (emit-first) order.
This commit is contained in:
Tianyi Cui
2026-06-14 23:18:41 +08:00
parent bcfff6f1ee
commit 9d5b3ab832
3 changed files with 32 additions and 3 deletions
+2 -2
View File
@@ -94,7 +94,7 @@ export interface LoopHandle {
* drain queued → session('user/message'…) → 'turn/start' → emit agent/turn-start
* STEP loop:
* drain steering → session('steering/message') ⟵ catches late steering
* emit agent/step-start
* session('step/start'); emit agent/step-start ⟵ append before emit (ADR 0003)
* assembly = ctx.systemPrompt.assemble() ⟵ waterfall system-prompt/assemble
* req = {model, system, tools, messages: session.deriveMessages(), signal}
* req = waterfall agent/request ⟵ hooks/compaction/model-switch
@@ -174,8 +174,8 @@ async function runTurn(ctx: Context, agent: LoopAgent, handle: LoopHandle, turn:
// (or turn-start listeners on the first step) joins before the request.
drainSteering(ctx, agent, turn)
ctx.emit('agent/step-start', agent, turn, step)
session.append('step/start', { turn, step })
ctx.emit('agent/step-start', agent, turn, step)
const abort = new AbortController()
handle.setAbort(abort)