loop.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /**
  2. * The agent loop driver: one `runLoop()` invocation drives one agent for its
  3. * whole lifetime. Error-contained at the turn level — a throwing plugin ends
  4. * the turn, never kills the loop. See the JSDoc on `runLoop()` for the full
  5. * lifecycle pseudo-code.
  6. *
  7. * @module dsh-agent-loop/loop
  8. */
  9. import type { Context } from 'cordis'
  10. import type { FinishReason, GenerateOptions, Message } from '@deepseek-ai/dsh-llm'
  11. import { BlockAssembler, HarnessError } from '@deepseek-ai/dsh-llm'
  12. import type { Session, TurnEndReason, TurnTrigger } from '@deepseek-ai/dsh-session'
  13. import { renderPrompt } from '@deepseek-ai/dsh-system-prompt'
  14. import type {} from '@deepseek-ai/dsh-tools'
  15. import type { ReactLoopAgent } from './agent.ts'
  16. /** An Error with an optional machine-readable code (e.g., from LlmError or a throwing plugin). */
  17. type CodedError = Error & { code?: string }
  18. /**
  19. * Normalize an arbitrary thrown value into a coded Error. A real Error passes
  20. * through (its `code`, if any, is preserved by {@link errorData}); a non-Error
  21. * throw is wrapped in a {@link HarnessError} with code `UNKNOWN` and the
  22. * original value chained as `cause`, so a bad throw still carries a routable
  23. * code instead of degrading to a bare message.
  24. */
  25. function toError(error: unknown): CodedError {
  26. return error instanceof Error ? error : new HarnessError(String(error), 'UNKNOWN', { cause: error })
  27. }
  28. /**
  29. * Map a model-call {@link FinishReason} to the step error it should raise, or
  30. * `undefined` when the step completed normally.
  31. *
  32. * Adapters report provider/transport failures one of two sanctioned ways (see
  33. * the StreamChunk contract in dsh-llm): throw from `stream()` (handled by the
  34. * caller's try/catch), OR end the stream with a finish-error/aborted chunk
  35. * (the only option for adapters that can't throw mid-stream, e.g.
  36. * library-backed ones). This translates the latter into a thrown step error
  37. * so the turn ends error/aborted (the failure recorded on `turn/end.reason`),
  38. * never as a normal `completed` assistant message.
  39. *
  40. * `FinishReason` is merge-extensible (plugins/adapters can add `kind`s), so
  41. * the switch handles the known terminal-failure kinds and treats every other
  42. * kind — `stop`, `tool-calls`, `max-tokens`, future additions — as success.
  43. */
  44. function finishError(finish: FinishReason): CodedError | undefined {
  45. switch (finish.kind) {
  46. case 'error': {
  47. const error: CodedError = new Error(finish.message)
  48. if (finish.code !== undefined) error.code = finish.code
  49. return error
  50. }
  51. case 'aborted': {
  52. const error: CodedError = new Error('model stream aborted')
  53. error.code = 'ABORTED'
  54. return error
  55. }
  56. // stop / tool-calls / max-tokens / plugin-added kinds → not a failure.
  57. default:
  58. return undefined
  59. }
  60. }
  61. /**
  62. * Build the `{ message, code? }` part of an error payload, omitting the
  63. * `code` key entirely when absent (exactOptionalPropertyTypes-correct).
  64. */
  65. function errorData(err: CodedError): { message: string; code?: string } {
  66. return { message: err.message, ...typeof err.code === 'string' ? { code: err.code } : {} }
  67. }
  68. /**
  69. * The turn-end contribution of a step's *successful* finish, or `undefined`
  70. * when the step finished ordinarily (a plain `completed`).
  71. *
  72. * {@link finishError} has already converted `error`/`aborted` finishes into
  73. * thrown step errors, so the finishes that reach here are `stop`,
  74. * `tool-calls`, `max-tokens`, or a future merge-extensible kind. Only
  75. * `max-tokens` carries forward as a distinct {@link TurnEndReason}: a step that
  76. * hit the output-token ceiling ended the turn cut-short rather than by the
  77. * model's choice. `stop`/`tool-calls`/unknown kinds contribute nothing beyond
  78. * the default `completed`. {@link runTurn} applies this with the rule "any
  79. * `max-tokens` step in the turn makes the turn end `max-tokens`".
  80. */
  81. function stepFinishReason(finish: FinishReason): TurnEndReason | undefined {
  82. switch (finish.kind) {
  83. case 'max-tokens':
  84. return { kind: 'max-tokens' }
  85. // stop / tool-calls / plugin-added kinds → no turn-end contribution
  86. // beyond the default `completed`. FinishReason is merge-extensible, so a
  87. // default (not assertNever) handles unknown kinds as ordinary success.
  88. default:
  89. return undefined
  90. }
  91. }
  92. /**
  93. * Ambient handles the loop driver receives from the agent. Decouples the
  94. * pure function `runLoop` from the mutable ReactLoopAgent fields, making the
  95. * loop testable without a real agent.
  96. */
  97. export interface LoopHandle {
  98. setStatus(status: 'idle' | 'running'): void
  99. setAbort(controller: AbortController | undefined): void
  100. /** Resolves when the agent is disposed — unblocks the idle wait. */
  101. disposed: Promise<void>
  102. isDisposed(): boolean
  103. /**
  104. * Whether a `cancel()` is pending for the current turn. The driver checks this
  105. * at every decision point where a turn could start or continue (right after
  106. * the idle wait, after the `running` flip, before each step, and at the
  107. * continuation gate) and drops the about-to-run / continuing turn. Reset once
  108. * per loop iteration via {@link clearCancel} after the turn returns, so the
  109. * marker governs exactly one cancellation and never leaks to a later prompt.
  110. */
  111. isCancelled(): boolean
  112. /**
  113. * The resolved reason for the pending cancel (`reason ?? 'cancelled'`), read
  114. * by the marker branches (pre-step / continuation) so a turn dropped where no
  115. * `AbortController` carries the reason still records the caller's
  116. * `cancel(reason)` value — matching the mid-step abort path. Only meaningful
  117. * when {@link isCancelled} is true.
  118. */
  119. cancelReason(): string
  120. /** Clear the cancel marker (called once per iteration after the turn returns). */
  121. clearCancel(): void
  122. /**
  123. * Settle pending `whenIdle()` waiters WITHOUT a status transition. Used by the
  124. * pre-step cancel-skip path: it drops the about-to-run turn and re-parks at the
  125. * idle wait, so no `running→idle` transition fires to settle a `whenIdle()`
  126. * waiter that was registered in the pre-step window — this settles it directly
  127. * (it emits no `agent/status`, so an ACP `agent/status` listener never sees a
  128. * spurious idle that would resolve a freshly-queued prompt as cancelled).
  129. */
  130. settleIdle(): void
  131. }
  132. /**
  133. * The agent loop. One invocation drives one agent for its whole lifetime:
  134. *
  135. * ```
  136. * forever:
  137. * wait for queued messages (idle)
  138. * TURN (error-contained — a throwing plugin ends the turn, never the loop):
  139. * drain queued → 'turn/start' → session('user/message'…) → emit agent/turn-start
  140. * STEP loop:
  141. * drain steering → session('steering/message') ⟵ catches late steering
  142. * session('step/start'); emit agent/step-start ⟵ append before emit (the event-sourcing RFC)
  143. * assembly = ctx.systemPrompt.assemble() ⟵ waterfall system-prompt/assemble
  144. * req = {model, system, tools, messages: session.deriveMessages(), signal}
  145. * req = waterfall agent/request ⟵ hooks/compaction/model-switch
  146. * stream ctx.llm.stream(req) ⟵ waterfall llm/stream (raw chunks)
  147. * session('assistant/chunk'); emit agent/stream-chunk
  148. * msg = waterfall agent/step-result ⟵ BEFORE the log append, so the
  149. * session('assistant/message' {content, usage?}) session records what actually ran
  150. * each tool-call in msg (sequential, abort-checked):
  151. * session('tool/call'); ctx.tools.execute() ⟵ waterfall tools/execute
  152. * session('tool/result')
  153. * drain steering → session('steering/message'); emit agent/steering
  154. * emit agent/step-end
  155. * cont = waterfall agent/turn-continuation(default = hadToolCalls || steered)
  156. * if !cont && steering arrived from step-end/continuation listeners: cont = true
  157. * if !cont: break
  158. * session('turn/end'); emit agent/turn-end
  159. * await ctx.parallel('session/flush', session) ⟵ durability checkpoint
  160. * re-enqueue leftover steering as queued ⟵ steering is never stranded
  161. * idle (emit agent/status) unless more queued
  162. * ```
  163. */
  164. export async function runLoop(ctx: Context, agent: ReactLoopAgent, handle: LoopHandle): Promise<void> {
  165. const { session } = agent
  166. while (!handle.isDisposed()) {
  167. await agent.inbox.waitForQueued(handle.disposed)
  168. if (handle.isDisposed()) break
  169. // Pre-step cancel (window 1): a `cancel()` landed after a `send()` woke the
  170. // idle wait but before we flip to `running`. The cancelled queued/steering
  171. // work is already cleared by `cancel()`. Clear the marker, then:
  172. // - if NOTHING new is queued, drop the about-to-run turn and re-park,
  173. // settling any `whenIdle()` waiter DIRECTLY (no running→idle transition
  174. // fires here to settle it) and WITHOUT emitting `agent/status` (an ACP
  175. // listener must not see a spurious idle that resolves a freshly-queued
  176. // prompt as cancelled);
  177. // - if a NEW prompt was queued AFTER the cancel (a send() that raced in
  178. // before the loop resumed), the marker was for the cancelled work only —
  179. // fall through and run the new prompt's turn. Do NOT settle waiters here:
  180. // a whenIdle() waiter must wait for that new turn's running→idle, not
  181. // resolve before it runs (the quiescence contract).
  182. if (handle.isCancelled()) {
  183. handle.clearCancel()
  184. if (!agent.inbox.hasQueued) {
  185. handle.settleIdle()
  186. continue
  187. }
  188. }
  189. handle.setStatus('running')
  190. // Pre-step cancel (window 2): `setStatus('running')` emits `agent/status`
  191. // SYNCHRONOUSLY, so a `running` listener can `cancel()` in the gap between the
  192. // check above and `runTurn`. Mirror window 1: clear the marker, then
  193. // - if NOTHING new is queued, drop the about-to-run turn and transition
  194. // back to `idle` (`running` was already emitted, so a real idle
  195. // transition balances the status AND settles `whenIdle()` waiters);
  196. // - if a NEW prompt was queued AFTER the cancel (a `running` listener that
  197. // cancels then sends), the marker was for the cancelled work only — fall
  198. // through and run the new prompt's turn (status is already `running`), so
  199. // a `whenIdle()` waiter resolves on THAT turn's running→idle, not before
  200. // it runs. Settling here would resolve quiescence while the replacement
  201. // is still queued and unrun (the same early-resolve race window 1 fixes).
  202. if (handle.isCancelled()) {
  203. handle.clearCancel()
  204. if (!agent.inbox.hasQueued) {
  205. handle.setStatus('idle')
  206. continue
  207. }
  208. }
  209. // Re-derive the turn number from the log each iteration (do NOT keep a local
  210. // counter): an idle `agent.inject()` can append its own one-shot turn while
  211. // the loop waits above, so the next real turn must continue from whatever
  212. // turn number is actually last in the log — a stale counter would collide.
  213. const turn = lastTurnNumber(session) + 1
  214. try {
  215. await runTurn(ctx, agent, handle, turn)
  216. } catch (error: unknown) {
  217. // Backstop: runTurn rethrows only a PRE-turn throw (the invariant guard
  218. // before turn/start) — no turn/start was appended, so no turn is open and
  219. // none is owed. A session `error` here would land outside any turn (after
  220. // the previous turn/end), where the persistence backend drops it as a
  221. // crash tail (the turn-enclosure RFC). Report via agent/error + the logger only; the
  222. // driver survives and moves on.
  223. const err = toError(error)
  224. ctx.logger.warn(`agent "${agent.id}": turn ${turn} failed before it started: ${err.message}`)
  225. try {
  226. ctx.emit('agent/error', agent, turn, 0, err)
  227. } catch { /* contained: a throwing agent/error listener must not kill the driver */ }
  228. }
  229. // Reset the cancel marker UNCONDITIONALLY here, after the turn returns and
  230. // before the next iteration's idle wait. NOT gated on the idle transition
  231. // below: a `send()` that lands during the cancelled turn's flush window makes
  232. // `hasQueued` true at the `setStatus('idle')` guard, so an idle-gated reset
  233. // would never fire and the stale marker would wrongly drop that next prompt's
  234. // turn. Resetting per iteration scopes the marker to exactly the turn that was
  235. // cancelled.
  236. handle.clearCancel()
  237. // Steering that arrived too late to join this turn (turn-end listeners,
  238. // flush) becomes a queued message — it must never be stranded. (A cancelled
  239. // turn already cleared its steering, so there is nothing to re-enqueue.)
  240. for (const message of agent.inbox.drainSteering()) {
  241. agent.inbox.enqueue(message)
  242. }
  243. if (!agent.inbox.hasQueued) handle.setStatus('idle')
  244. }
  245. }
  246. async function runTurn(ctx: Context, agent: ReactLoopAgent, handle: LoopHandle, turn: number): Promise<void> {
  247. const { session } = agent
  248. // --- Pre-turn. A throw here (the invariant guard) is owed NO turn/end —
  249. // turn/start has not been appended — so it propagates to runLoop's backstop
  250. // untouched. The queued messages are drained here but appended AFTER
  251. // turn/start (below), so every event in the log lives inside a turn.
  252. const queued = agent.inbox.drainQueued()
  253. const first = queued[0]
  254. /* v8 ignore next 3 -- invariant guard: runLoop only calls runTurn when hasQueued */
  255. if (!first) throw new Error('runTurn invariant violated: no queued message at turn start')
  256. const trigger: TurnTrigger = { kind: 'message', source: first.source }
  257. let reason: TurnEndReason = { kind: 'completed' }
  258. let step = 0
  259. let turnEnded = false
  260. let stepOpen = false
  261. let errorReported = false
  262. // Close the open step exactly once (idempotent via stepOpen). The
  263. // agent/step-end emit is contained: a throwing step-end listener must not
  264. // abort finalization and strand the turn open (turn/end balance > notifying
  265. // one bad listener). Appended before the emit (the event-sourcing RFC append-before-emit).
  266. const closeStep = (): boolean => {
  267. if (!stepOpen) return false
  268. stepOpen = false
  269. // Session.append pushes step/end BEFORE notifying session/event listeners,
  270. // so a throwing listener leaves step/end in the log (balance holds) but
  271. // would otherwise abort finalization. Contain it and surface it as a turn
  272. // error below — the same outcome as a throwing agent/step-end listener.
  273. let failure: unknown
  274. try {
  275. session.append('step/end', { turn, step })
  276. } catch (error: unknown) {
  277. failure = error
  278. }
  279. try {
  280. ctx.emit('agent/step-end', agent, turn, step)
  281. } catch (error: unknown) {
  282. failure ??= error
  283. }
  284. // A throwing step/end session-event listener OR a throwing agent/step-end
  285. // listener surfaces as a turn error via failTurn (idempotent). This prevents
  286. // a throwing listener from producing a silent "completed" turn when the step
  287. // itself succeeded, AND keeps finalization going when closeStep runs from
  288. // the outer catch.
  289. if (failure !== undefined) {
  290. failTurn(toError(failure))
  291. return true
  292. }
  293. return false
  294. }
  295. // Record a step/turn failure exactly once: set the error reason (carrying the
  296. // failing `step` — the durable failure lives entirely on turn/end.reason, there
  297. // is no separate session error event) and emit agent/error (contained — trap: a
  298. // throwing agent/error listener must not re-escape and strand the turn).
  299. // Disposal and abort set `reason` directly without calling this (they are not
  300. // failures).
  301. const failTurn = (err: CodedError): void => {
  302. if (errorReported) return
  303. errorReported = true
  304. // Set the error reason ONLY while the turn is still open — closeTurn appends
  305. // turn/end with it. If the turn has already ended (the only way here: a
  306. // throwing agent/turn-end listener after closeTurn(true) already appended
  307. // turn/end), the reason can no longer affect the durable log, so log the late
  308. // throw directly instead — otherwise the listener exception would vanish.
  309. if (!turnEnded) {
  310. reason = { kind: 'error', step, ...errorData(err) }
  311. } else {
  312. ctx.logger.warn(`agent "${agent.id}": agent/turn-end listener threw after turn ${turn} closed: ${err.message}`)
  313. }
  314. try {
  315. ctx.emit('agent/error', agent, turn, step, err)
  316. } catch {
  317. // contained: the error is already captured (on `reason`, or via the logger
  318. // above); a throwing agent/error listener must not prevent the turn from
  319. // closing.
  320. }
  321. }
  322. // Close the turn exactly once (idempotent via turnEnded). `emit` is false on
  323. // the error path (the failure was already surfaced via agent/error) and true
  324. // on the normal/inline-error path. A throwing agent/turn-end listener on the
  325. // normal path escapes to the outer catch, which surfaces it via failTurn —
  326. // turn/end is already appended, so balance holds either way.
  327. const closeTurn = (emit: boolean): void => {
  328. if (turnEnded) return
  329. turnEnded = true
  330. // Session.append pushes turn/end BEFORE notifying session/event listeners,
  331. // so a throwing listener leaves turn/end in the log (the turn is balanced)
  332. // but would otherwise escape — from the outer catch's closeTurn(false) it
  333. // would propagate to the runLoop backstop, and from the normal-path
  334. // closeTurn(true) it would skip the agent/turn-end emit. Contain it: the
  335. // boundary is durable either way, and finalization must not abort on a bad
  336. // listener. (On the normal path the outer catch also re-runs closeTurn,
  337. // which is an idempotent no-op once turnEnded is set.)
  338. try {
  339. session.append('turn/end', { turn, reason })
  340. } catch (error: unknown) {
  341. ctx.logger.warn(`agent "${agent.id}": session/event listener threw on turn/end at turn ${turn}: ${toError(error).message}`)
  342. }
  343. if (emit) ctx.emit('agent/turn-end', agent, turn, reason)
  344. }
  345. try {
  346. // --- Turn boundary. Once turn/start is appended, a turn/end is owed no
  347. // matter what throws below; the catch + closeTurn guarantee it (the catch
  348. // decides "owed" from the log via isTurnOpen, so even a throwing turn/start
  349. // listener — append pushes before notifying — still gets its turn/end).
  350. session.append('turn/start', { turn, trigger })
  351. // Record the queued user messages INSIDE the turn (after turn/start), so
  352. // every event in the log is turn-enclosed. turn/end is now owed, so a throw
  353. // while appending these is caught below and the turn is still closed.
  354. for (const message of queued) {
  355. session.append('user/message', { content: message.content, source: message.source })
  356. }
  357. ctx.emit('agent/turn-start', agent, turn)
  358. while (true) {
  359. step += 1
  360. // Steering from the previous round's step-end/continuation listeners
  361. // (or turn-start listeners on the first step) joins before the request.
  362. drainSteering(ctx, agent, turn)
  363. session.append('step/start', { turn, step })
  364. stepOpen = true
  365. ctx.emit('agent/step-start', agent, turn, step)
  366. const abort = new AbortController()
  367. handle.setAbort(abort)
  368. // Cancel landing in the step-start window: a synchronous `agent/turn-start`
  369. // or `agent/step-start` listener (both fire before this point) can have
  370. // called `cancel()`, and `runStep` would otherwise run a full extra step
  371. // with no AbortController having observed it. Check the marker AFTER
  372. // setAbort (so the next-iteration drain sees a clean controller) and before
  373. // `runStep`: drop the step, end the turn `aborted`. closeStep balances the
  374. // already-appended step/start.
  375. if (handle.isCancelled()) {
  376. handle.setAbort(undefined)
  377. reason = { kind: 'aborted', reason: handle.cancelReason() }
  378. closeStep()
  379. break
  380. }
  381. let stepOutcome: { hadToolCalls: boolean; finish: FinishReason } | { error: Error }
  382. try {
  383. stepOutcome = await runStep(ctx, agent, turn, step, abort.signal)
  384. } catch (error: unknown) {
  385. stepOutcome = { error: toError(error) }
  386. } finally {
  387. handle.setAbort(undefined)
  388. }
  389. if ('error' in stepOutcome) {
  390. // Steering that arrived during the failed step stays in the inbox —
  391. // runLoop re-enqueues it as a queued message, so an abort-then-steer
  392. // starts a fresh turn instead of being silently consumed.
  393. closeStep()
  394. const { error } = stepOutcome
  395. if (handle.isDisposed()) {
  396. reason = { kind: 'disposed' }
  397. } else if (abort.signal.aborted) {
  398. /* v8 ignore next -- signal.reason always set: cancel()/disposal provide a default */
  399. reason = { kind: 'aborted', reason: String(abort.signal.reason ?? 'aborted') }
  400. } else {
  401. failTurn(error)
  402. }
  403. break
  404. }
  405. // The successful step's finish reason carries forward: a `max-tokens`
  406. // step makes the whole turn end `max-tokens` (the ACP RFC's rule "any
  407. // max-tokens step surfaces as max-tokens"). `stepFinishReason` returns
  408. // `max-tokens` or `undefined`, so a later ordinary step never resets a
  409. // max-tokens turn back to completed, and a never-truncated turn keeps the
  410. // default `completed`. The disposal/abort/error branches above and the
  411. // continuation-window disposal check below override this — they win.
  412. const stepReason = stepFinishReason(stepOutcome.finish)
  413. if (stepReason) reason = stepReason
  414. // Steering that arrived during streaming/tool execution.
  415. const steered = drainSteering(ctx, agent, turn)
  416. if (closeStep()) break
  417. const defaultDecision = stepOutcome.hadToolCalls || steered
  418. let shouldContinue: boolean
  419. try {
  420. shouldContinue = await ctx.waterfall(
  421. 'agent/turn-continuation', agent, turn, defaultDecision,
  422. () => Promise.resolve(defaultDecision),
  423. )
  424. } catch (error: unknown) {
  425. // A broken continuation plugin ends the turn, not the loop.
  426. failTurn(toError(error))
  427. break
  428. }
  429. // Steering from step-end/continuation listeners (the /goal pattern)
  430. // demands the model see it — it overrides a negative decision; the
  431. // next iteration's drain records it.
  432. if (!shouldContinue && agent.inbox.hasSteering) shouldContinue = true
  433. // A cancel that landed during the continuation window — after the step's
  434. // AbortController was cleared (setAbort(undefined)) but before the next
  435. // step starts — has no controller to observe it, so the turn-scoped marker
  436. // ends the turn here. cancel() also cleared the steering FIFO, so the
  437. // override above did not re-arm continuation.
  438. if (handle.isCancelled()) {
  439. reason = { kind: 'aborted', reason: handle.cancelReason() }
  440. break
  441. }
  442. if (!shouldContinue || handle.isDisposed()) {
  443. /* v8 ignore next -- disposal during continuation-decision window is a narrow race; error-path disposal is covered elsewhere */
  444. if (handle.isDisposed()) reason = { kind: 'disposed' }
  445. break
  446. }
  447. }
  448. // Normal / inline-error loop exit: close the turn and notify.
  449. closeTurn(true)
  450. } catch (error: unknown) {
  451. // Decide whether this turn was ever opened from the LOG, not a flag.
  452. // Session.append pushes the event BEFORE notifying session/event listeners,
  453. // so a throwing listener on the `turn/start` append leaves turn/start in the
  454. // log even though execution never reached the lines after that append.
  455. // Gating on a "turn started" boolean would skip turn/end and leave a
  456. // permanently OPEN turn that poisons the next turn/replay (the turn-enclosure RFC). We
  457. // check the log for THIS turn's turn/start: present means a turn/end is owed
  458. // (or was already appended — closeTurn/failTurn are idempotent, so running
  459. // them again is a safe no-op that still preserves the disposed/error reason
  460. // chosen below). Absent means the turn/start append threw BEFORE its push (a
  461. // non-serializable trigger — impossible for our fixed trigger); nothing was
  462. // opened, so rethrow to the runLoop backstop.
  463. const turnStartLogged = session.events.some(e => e.type === 'turn/start' && e.data.turn === turn)
  464. if (!turnStartLogged) throw error
  465. closeStep()
  466. // Choose the close reason. Disposal wins only if no error was already
  467. // reported: a turn disposed mid-step sets reason=disposed in the step-error
  468. // branch (without reporting an error), and if closeTurn(true)'s turn-end
  469. // emit then throws, we land here and must PRESERVE disposed rather than
  470. // overwrite it with the listener's throw. Otherwise a boundary-emit throw
  471. // on a live agent is a real failure → failTurn. (errorReported is mutated
  472. // only inside the failTurn closure, which the analyzer can't follow, hence
  473. // the inline lint-disable.)
  474. if (handle.isDisposed() && !errorReported) { // eslint-disable-line @typescript-eslint/no-unnecessary-condition
  475. reason = { kind: 'disposed' }
  476. } else {
  477. failTurn(toError(error))
  478. }
  479. closeTurn(false)
  480. }
  481. // Durability checkpoint: persistence plugins drain write-behind buffers.
  482. // A failing persistence plugin is reported but doesn't kill the agent.
  483. try {
  484. await ctx.parallel('session/flush', session)
  485. } catch (error: unknown) {
  486. // The turn is already closed (turn/end appended above) and flush must run
  487. // AFTER turn/end to be a checkpoint — so there is no in-turn position left
  488. // for a session `error` event. Appending one here would land it after the
  489. // last turn/end, where the persistence backend treats it as a crash tail
  490. // and drops it on resume (the turn-enclosure RFC: every event is turn-enclosed). Report
  491. // the failure via agent/error + the logger only; persistence keeps the
  492. // buffered events for the next flush/dispose, so nothing is lost.
  493. const err = toError(error)
  494. ctx.logger.warn(`agent "${agent.id}": session/flush failed at turn ${turn}: ${err.message}`)
  495. try {
  496. ctx.emit('agent/error', agent, turn, step, err)
  497. } catch {
  498. // contained: a throwing agent/error listener must not escape the loop.
  499. }
  500. }
  501. }
  502. /** Drain the steering queue into the session. Returns whether any arrived. */
  503. function drainSteering(ctx: Context, agent: ReactLoopAgent, turn: number): boolean {
  504. const messages = agent.inbox.drainSteering()
  505. for (const message of messages) {
  506. agent.session.append('steering/message', { turn, content: message.content, source: message.source })
  507. ctx.emit('agent/steering', agent, turn, message.content, message.source)
  508. }
  509. return messages.length > 0
  510. }
  511. /** One step: assemble request → stream model → record → execute tools. */
  512. async function runStep(
  513. ctx: Context,
  514. agent: ReactLoopAgent,
  515. turn: number,
  516. step: number,
  517. signal: AbortSignal,
  518. ): Promise<{ hadToolCalls: boolean; finish: FinishReason }> {
  519. const { session, options } = agent
  520. // --- Request assembly ---
  521. const assembly = await ctx.systemPrompt.assemble()
  522. const system = [renderPrompt(assembly), options.systemPrompt ?? '']
  523. .filter(text => text.length > 0)
  524. .join('\n\n')
  525. let request: GenerateOptions = {
  526. model: options.model ?? '',
  527. messages: session.deriveMessages(),
  528. ...system ? { system } : {},
  529. ...assembly.tools.length > 0 ? { tools: assembly.tools } : {},
  530. signal,
  531. }
  532. request = await ctx.waterfall('agent/request', agent, turn, step, request, () => Promise.resolve(request))
  533. if (!request.model) {
  534. throw new Error(`agent "${agent.id}" has no model: set AgentOptions.model or supply one via the agent/request waterfall`)
  535. }
  536. // --- Model call (streaming-first; raw chunks are the replay record) ---
  537. const assembler = new BlockAssembler()
  538. for await (const chunk of ctx.llm.stream(request)) {
  539. /* v8 ignore next -- signal.reason always set: cancel()/disposal provide a default */
  540. if (signal.aborted) throw new Error(String(signal.reason ?? 'aborted'))
  541. session.append('assistant/chunk', { turn, step, chunk })
  542. ctx.emit('agent/stream-chunk', agent, turn, step, chunk)
  543. assembler.push(chunk)
  544. }
  545. // Adapters report provider/transport failures one of two sanctioned ways
  546. // (see the StreamChunk contract in dsh-llm): throw from stream() — already
  547. // handled by the caller's try/catch — OR end the stream with a
  548. // finish-error/aborted chunk. finishError() maps the latter to the step
  549. // error to raise (turn ends error/aborted, not a normal completed message).
  550. const stepError = finishError(assembler.finish)
  551. if (stepError) throw stepError
  552. if (assembler.finish.kind === 'max-tokens') {
  553. let message: Message = withoutToolCalls(assembler.message())
  554. message = withoutToolCalls(await ctx.waterfall('agent/step-result', agent, turn, step, message, () => Promise.resolve(message)))
  555. // Fire the assistant/message when there is content OR usage: a max-tokens
  556. // step can be cut off with empty content but still carry token accounting,
  557. // and assistant/message is the only host for usage (there is no standalone
  558. // usage event). An empty-content assistant/message is skipped by
  559. // deriveMessages(), so hosting usage on it never injects a spurious assistant
  560. // turn into derived history.
  561. if (message.content.length > 0 || assembler.usage) {
  562. session.append('assistant/message', { turn, step, content: message.content, ...(assembler.usage ? { usage: assembler.usage } : {}) })
  563. }
  564. return { hadToolCalls: false, finish: assembler.finish }
  565. }
  566. // The step-result waterfall runs BEFORE the session append so the log (the
  567. // source of truth for derived history and replay) records the message that
  568. // tool dispatch actually uses.
  569. let message: Message = assembler.message()
  570. message = await ctx.waterfall('agent/step-result', agent, turn, step, message, () => Promise.resolve(message))
  571. // Same content-or-usage guard as the max-tokens branch: a step that finishes
  572. // with neither assembled content nor usage (e.g. a bare `stop` finish that
  573. // streamed nothing) records no assistant/message — an empty-content message
  574. // exists only to host usage, and deriveMessages() skips it either way, so
  575. // appending one with no usage would be a pure trace-only row.
  576. if (message.content.length > 0 || assembler.usage) {
  577. session.append('assistant/message', { turn, step, content: message.content, ...(assembler.usage ? { usage: assembler.usage } : {}) })
  578. }
  579. // --- Tool execution (sequential; parallel execution is a TODO) ---
  580. // ToolRegistry.execute converts tool failures (including aborts) into
  581. // isError results, so abort is re-checked around every call here.
  582. const toolCalls = message.content.filter(block => block.type === 'tool-call')
  583. for (const call of toolCalls) {
  584. /* v8 ignore next -- signal.reason always set: cancel()/disposal provide a default */
  585. if (signal.aborted) throw new Error(String(signal.reason ?? 'aborted'))
  586. session.append('tool/call', { turn, step, callId: call.id, name: call.name, arguments: call.arguments })
  587. let parsedArguments: unknown
  588. try {
  589. parsedArguments = call.arguments ? JSON.parse(call.arguments) : {}
  590. } catch {
  591. parsedArguments = call.arguments
  592. }
  593. const result = await ctx.tools.execute({
  594. callId: call.id,
  595. name: call.name,
  596. arguments: parsedArguments,
  597. agent,
  598. signal,
  599. })
  600. session.append('tool/result', {
  601. turn, step,
  602. // The correlation id MUST be the loop's authoritative call.id (the
  603. // model-transcript id that deriveMessages turns into toolCallId), NOT
  604. // result.callId — a tools/execute waterfall listener returning a
  605. // mismatched id would otherwise orphan the call↔result pairing in the
  606. // next model request. A listener-internal id, if ever needed, belongs in
  607. // a separate diagnostic field, never overloaded onto callId.
  608. callId: call.id,
  609. content: result.content,
  610. isError: result.isError,
  611. ...result.error ? { error: result.error } : {},
  612. })
  613. // signal CAN flip during the await above (abort() inside a tool);
  614. // the analyzer can't see through the await boundary.
  615. /* v8 ignore start -- signal.reason default unreachable: cancel()/disposal always set it */
  616. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  617. if (signal.aborted) throw new Error(String(signal.reason ?? 'aborted'))
  618. /* v8 ignore stop */
  619. }
  620. return { hadToolCalls: toolCalls.length > 0, finish: assembler.finish }
  621. }
  622. function withoutToolCalls(message: Message): Message {
  623. return { ...message, content: message.content.filter(block => block.type !== 'tool-call') }
  624. }
  625. /** The last turn number in a (possibly seeded) session log, or 0. */
  626. export function lastTurnNumber(session: Session): number {
  627. const lastStart = session.events.findLast(event => event.type === 'turn/start')
  628. return lastStart?.data.turn ?? 0
  629. }
  630. /**
  631. * Whether a turn is currently open in the session log (a `turn/start` with no
  632. * matching later `turn/end`). Decided from the LOG, not agent status: status
  633. * can be `running` while no turn is open (an `agent/status` listener firing
  634. * before `turn/start`, or the post-`turn/end` flush window before status
  635. * returns to idle), so status is not a reliable open-turn signal. Used by
  636. * `inject()` to choose between appending into an open turn vs. wrapping the
  637. * injection in its own one-shot turn (the turn-enclosure RFC).
  638. */
  639. export function isTurnOpen(session: Session): boolean {
  640. const last = session.events.findLast(e => e.type === 'turn/start' || e.type === 'turn/end')
  641. return last?.type === 'turn/start'
  642. }