loop.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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 with a logged `error` event, never as a
  38. * 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','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: append the single `error` event
  296. // (only while the turn is still open — see below), set the error reason, and
  297. // emit agent/error (contained — trap: a throwing agent/error listener must not
  298. // re-escape and strand the turn). Disposal and abort set `reason` directly
  299. // without calling this (no `error` event for those — they are not failures).
  300. const failTurn = (err: CodedError): void => {
  301. if (errorReported) return
  302. errorReported = true
  303. // Only append the session `error` INSIDE the turn (before turn/end). If the
  304. // turn has already ended — the only way here is a throwing agent/turn-end
  305. // listener after closeTurn(true) already appended turn/end — appending now
  306. // would land the error AFTER the last turn/end, where the persistence
  307. // backend treats it as a crash tail and drops it on resume (the turn-enclosure RFC). In
  308. // that case report via agent/error + the logger only; the turn is balanced.
  309. if (!turnEnded) {
  310. // Set `reason` BEFORE the append: Session.append pushes the error event
  311. // before notifying session/event listeners, so a throwing listener would
  312. // otherwise leave `reason` unset (and closeTurn would record the wrong
  313. // reason / the outer catch would skip closeTurn). The append is contained
  314. // — the error event is already in the log either way; a throwing listener
  315. // must not abort finalization.
  316. reason = { kind: 'error', ...errorData(err) }
  317. try {
  318. session.append('error', { turn, step, ...errorData(err) })
  319. } catch (appendError: unknown) {
  320. ctx.logger.warn(`agent "${agent.id}": session/event listener threw on the error event at turn ${turn}: ${toError(appendError).message}`)
  321. }
  322. } else {
  323. ctx.logger.warn(`agent "${agent.id}": agent/turn-end listener threw after turn ${turn} closed: ${err.message}`)
  324. }
  325. try {
  326. ctx.emit('agent/error', agent, turn, step, err)
  327. } catch {
  328. // contained: the error is already logged; a throwing agent/error
  329. // listener must not prevent the turn from closing.
  330. }
  331. }
  332. // Close the turn exactly once (idempotent via turnEnded). `emit` is false on
  333. // the error path (the failure was already surfaced via agent/error) and true
  334. // on the normal/inline-error path. A throwing agent/turn-end listener on the
  335. // normal path escapes to the outer catch, which surfaces it via failTurn —
  336. // turn/end is already appended, so balance holds either way.
  337. const closeTurn = (emit: boolean): void => {
  338. if (turnEnded) return
  339. turnEnded = true
  340. // Session.append pushes turn/end BEFORE notifying session/event listeners,
  341. // so a throwing listener leaves turn/end in the log (the turn is balanced)
  342. // but would otherwise escape — from the outer catch's closeTurn(false) it
  343. // would propagate to the runLoop backstop, and from the normal-path
  344. // closeTurn(true) it would skip the agent/turn-end emit. Contain it: the
  345. // boundary is durable either way, and finalization must not abort on a bad
  346. // listener. (On the normal path the outer catch also re-runs closeTurn,
  347. // which is an idempotent no-op once turnEnded is set.)
  348. try {
  349. session.append('turn/end', { turn, reason })
  350. } catch (error: unknown) {
  351. ctx.logger.warn(`agent "${agent.id}": session/event listener threw on turn/end at turn ${turn}: ${toError(error).message}`)
  352. }
  353. if (emit) ctx.emit('agent/turn-end', agent, turn, reason)
  354. }
  355. try {
  356. // --- Turn boundary. Once turn/start is appended, a turn/end is owed no
  357. // matter what throws below; the catch + closeTurn guarantee it (the catch
  358. // decides "owed" from the log via isTurnOpen, so even a throwing turn/start
  359. // listener — append pushes before notifying — still gets its turn/end).
  360. session.append('turn/start', { turn, trigger })
  361. // Record the queued user messages INSIDE the turn (after turn/start), so
  362. // every event in the log is turn-enclosed. turn/end is now owed, so a throw
  363. // while appending these is caught below and the turn is still closed.
  364. for (const message of queued) {
  365. session.append('user/message', { content: message.content, source: message.source })
  366. }
  367. ctx.emit('agent/turn-start', agent, turn)
  368. while (true) {
  369. step += 1
  370. // Steering from the previous round's step-end/continuation listeners
  371. // (or turn-start listeners on the first step) joins before the request.
  372. drainSteering(ctx, agent, turn)
  373. session.append('step/start', { turn, step })
  374. stepOpen = true
  375. ctx.emit('agent/step-start', agent, turn, step)
  376. const abort = new AbortController()
  377. handle.setAbort(abort)
  378. // Cancel landing in the step-start window: a synchronous `agent/turn-start`
  379. // or `agent/step-start` listener (both fire before this point) can have
  380. // called `cancel()`, and `runStep` would otherwise run a full extra step
  381. // with no AbortController having observed it. Check the marker AFTER
  382. // setAbort (so the next-iteration drain sees a clean controller) and before
  383. // `runStep`: drop the step, end the turn `aborted`. closeStep balances the
  384. // already-appended step/start.
  385. if (handle.isCancelled()) {
  386. handle.setAbort(undefined)
  387. reason = { kind: 'aborted', reason: handle.cancelReason() }
  388. closeStep()
  389. break
  390. }
  391. let stepOutcome: { hadToolCalls: boolean; finish: FinishReason } | { error: Error }
  392. try {
  393. stepOutcome = await runStep(ctx, agent, turn, step, abort.signal)
  394. } catch (error: unknown) {
  395. stepOutcome = { error: toError(error) }
  396. } finally {
  397. handle.setAbort(undefined)
  398. }
  399. if ('error' in stepOutcome) {
  400. // Steering that arrived during the failed step stays in the inbox —
  401. // runLoop re-enqueues it as a queued message, so an abort-then-steer
  402. // starts a fresh turn instead of being silently consumed.
  403. closeStep()
  404. const { error } = stepOutcome
  405. if (handle.isDisposed()) {
  406. reason = { kind: 'disposed' }
  407. } else if (abort.signal.aborted) {
  408. /* v8 ignore next -- signal.reason always set: cancel()/disposal provide a default */
  409. reason = { kind: 'aborted', reason: String(abort.signal.reason ?? 'aborted') }
  410. } else {
  411. failTurn(error)
  412. }
  413. break
  414. }
  415. // The successful step's finish reason carries forward: a `max-tokens`
  416. // step makes the whole turn end `max-tokens` (the ACP RFC's rule "any
  417. // max-tokens step surfaces as max-tokens"). `stepFinishReason` returns
  418. // `max-tokens` or `undefined`, so a later ordinary step never resets a
  419. // max-tokens turn back to completed, and a never-truncated turn keeps the
  420. // default `completed`. The disposal/abort/error branches above and the
  421. // continuation-window disposal check below override this — they win.
  422. const stepReason = stepFinishReason(stepOutcome.finish)
  423. if (stepReason) reason = stepReason
  424. // Steering that arrived during streaming/tool execution.
  425. const steered = drainSteering(ctx, agent, turn)
  426. if (closeStep()) break
  427. const defaultDecision = stepOutcome.hadToolCalls || steered
  428. let shouldContinue: boolean
  429. try {
  430. shouldContinue = await ctx.waterfall(
  431. 'agent/turn-continuation', agent, turn, defaultDecision,
  432. () => Promise.resolve(defaultDecision),
  433. )
  434. } catch (error: unknown) {
  435. // A broken continuation plugin ends the turn, not the loop.
  436. failTurn(toError(error))
  437. break
  438. }
  439. // Steering from step-end/continuation listeners (the /goal pattern)
  440. // demands the model see it — it overrides a negative decision; the
  441. // next iteration's drain records it.
  442. if (!shouldContinue && agent.inbox.hasSteering) shouldContinue = true
  443. // A cancel that landed during the continuation window — after the step's
  444. // AbortController was cleared (setAbort(undefined)) but before the next
  445. // step starts — has no controller to observe it, so the turn-scoped marker
  446. // ends the turn here. cancel() also cleared the steering FIFO, so the
  447. // override above did not re-arm continuation.
  448. if (handle.isCancelled()) {
  449. reason = { kind: 'aborted', reason: handle.cancelReason() }
  450. break
  451. }
  452. if (!shouldContinue || handle.isDisposed()) {
  453. /* v8 ignore next -- disposal during continuation-decision window is a narrow race; error-path disposal is covered elsewhere */
  454. if (handle.isDisposed()) reason = { kind: 'disposed' }
  455. break
  456. }
  457. }
  458. // Normal / inline-error loop exit: close the turn and notify.
  459. closeTurn(true)
  460. } catch (error: unknown) {
  461. // Decide whether this turn was ever opened from the LOG, not a flag.
  462. // Session.append pushes the event BEFORE notifying session/event listeners,
  463. // so a throwing listener on the `turn/start` append leaves turn/start in the
  464. // log even though execution never reached the lines after that append.
  465. // Gating on a "turn started" boolean would skip turn/end and leave a
  466. // permanently OPEN turn that poisons the next turn/replay (the turn-enclosure RFC). We
  467. // check the log for THIS turn's turn/start: present means a turn/end is owed
  468. // (or was already appended — closeTurn/failTurn are idempotent, so running
  469. // them again is a safe no-op that still preserves the disposed/error reason
  470. // chosen below). Absent means the turn/start append threw BEFORE its push (a
  471. // non-serializable trigger — impossible for our fixed trigger); nothing was
  472. // opened, so rethrow to the runLoop backstop.
  473. const turnStartLogged = session.events.some(e => e.type === 'turn/start' && e.data.turn === turn)
  474. if (!turnStartLogged) throw error
  475. closeStep()
  476. // Choose the close reason. Disposal wins only if no error was already
  477. // reported: a turn disposed mid-step sets reason=disposed in the step-error
  478. // branch (without reporting an error), and if closeTurn(true)'s turn-end
  479. // emit then throws, we land here and must PRESERVE disposed rather than
  480. // overwrite it with the listener's throw. Otherwise a boundary-emit throw
  481. // on a live agent is a real failure → failTurn. (errorReported is mutated
  482. // only inside the failTurn closure, which the analyzer can't follow, hence
  483. // the inline lint-disable.)
  484. if (handle.isDisposed() && !errorReported) { // eslint-disable-line @typescript-eslint/no-unnecessary-condition
  485. reason = { kind: 'disposed' }
  486. } else {
  487. failTurn(toError(error))
  488. }
  489. closeTurn(false)
  490. }
  491. // Durability checkpoint: persistence plugins drain write-behind buffers.
  492. // A failing persistence plugin is reported but doesn't kill the agent.
  493. try {
  494. await ctx.parallel('session/flush', session)
  495. } catch (error: unknown) {
  496. // The turn is already closed (turn/end appended above) and flush must run
  497. // AFTER turn/end to be a checkpoint — so there is no in-turn position left
  498. // for a session `error` event. Appending one here would land it after the
  499. // last turn/end, where the persistence backend treats it as a crash tail
  500. // and drops it on resume (the turn-enclosure RFC: every event is turn-enclosed). Report
  501. // the failure via agent/error + the logger only; persistence keeps the
  502. // buffered events for the next flush/dispose, so nothing is lost.
  503. const err = toError(error)
  504. ctx.logger.warn(`agent "${agent.id}": session/flush failed at turn ${turn}: ${err.message}`)
  505. try {
  506. ctx.emit('agent/error', agent, turn, step, err)
  507. } catch {
  508. // contained: a throwing agent/error listener must not escape the loop.
  509. }
  510. }
  511. }
  512. /** Drain the steering queue into the session. Returns whether any arrived. */
  513. function drainSteering(ctx: Context, agent: ReactLoopAgent, turn: number): boolean {
  514. const messages = agent.inbox.drainSteering()
  515. for (const message of messages) {
  516. agent.session.append('steering/message', { turn, content: message.content, source: message.source })
  517. ctx.emit('agent/steering', agent, turn, message.content, message.source)
  518. }
  519. return messages.length > 0
  520. }
  521. /** One step: assemble request → stream model → record → execute tools. */
  522. async function runStep(
  523. ctx: Context,
  524. agent: ReactLoopAgent,
  525. turn: number,
  526. step: number,
  527. signal: AbortSignal,
  528. ): Promise<{ hadToolCalls: boolean; finish: FinishReason }> {
  529. const { session, options } = agent
  530. // --- Request assembly ---
  531. const assembly = await ctx.systemPrompt.assemble()
  532. const system = [renderPrompt(assembly), options.systemPrompt ?? '']
  533. .filter(text => text.length > 0)
  534. .join('\n\n')
  535. let request: GenerateOptions = {
  536. model: options.model ?? '',
  537. messages: session.deriveMessages(),
  538. ...system ? { system } : {},
  539. ...assembly.tools.length > 0 ? { tools: assembly.tools } : {},
  540. signal,
  541. }
  542. request = await ctx.waterfall('agent/request', agent, turn, step, request, () => Promise.resolve(request))
  543. if (!request.model) {
  544. throw new Error(`agent "${agent.id}" has no model: set AgentOptions.model or supply one via the agent/request waterfall`)
  545. }
  546. // --- Model call (streaming-first; raw chunks are the replay record) ---
  547. const assembler = new BlockAssembler()
  548. for await (const chunk of ctx.llm.stream(request)) {
  549. /* v8 ignore next -- signal.reason always set: cancel()/disposal provide a default */
  550. if (signal.aborted) throw new Error(String(signal.reason ?? 'aborted'))
  551. session.append('assistant/chunk', { turn, step, chunk })
  552. ctx.emit('agent/stream-chunk', agent, turn, step, chunk)
  553. assembler.push(chunk)
  554. }
  555. // Adapters report provider/transport failures one of two sanctioned ways
  556. // (see the StreamChunk contract in dsh-llm): throw from stream() — already
  557. // handled by the caller's try/catch — OR end the stream with a
  558. // finish-error/aborted chunk. finishError() maps the latter to the step
  559. // error to raise (turn ends error/aborted, not a normal completed message).
  560. const stepError = finishError(assembler.finish)
  561. if (stepError) throw stepError
  562. if (assembler.finish.kind === 'max-tokens') {
  563. let message: Message = withoutToolCalls(assembler.message())
  564. message = withoutToolCalls(await ctx.waterfall('agent/step-result', agent, turn, step, message, () => Promise.resolve(message)))
  565. if (message.content.length > 0) {
  566. session.append('assistant/message', { turn, step, content: message.content })
  567. }
  568. if (assembler.usage) {
  569. session.append('usage', { turn, step, usage: assembler.usage })
  570. }
  571. return { hadToolCalls: false, finish: assembler.finish }
  572. }
  573. // The step-result waterfall runs BEFORE the session append so the log (the
  574. // source of truth for derived history and replay) records the message that
  575. // tool dispatch actually uses.
  576. let message: Message = assembler.message()
  577. message = await ctx.waterfall('agent/step-result', agent, turn, step, message, () => Promise.resolve(message))
  578. session.append('assistant/message', { turn, step, content: message.content })
  579. if (assembler.usage) {
  580. session.append('usage', { turn, step, usage: assembler.usage })
  581. }
  582. // --- Tool execution (sequential; parallel execution is a TODO) ---
  583. // ToolRegistry.execute converts tool failures (including aborts) into
  584. // isError results, so abort is re-checked around every call here.
  585. const toolCalls = message.content.filter(block => block.type === 'tool-call')
  586. for (const call of toolCalls) {
  587. /* v8 ignore next -- signal.reason always set: cancel()/disposal provide a default */
  588. if (signal.aborted) throw new Error(String(signal.reason ?? 'aborted'))
  589. session.append('tool/call', { turn, step, callId: call.id, name: call.name, arguments: call.arguments })
  590. let parsedArguments: unknown
  591. try {
  592. parsedArguments = call.arguments ? JSON.parse(call.arguments) : {}
  593. } catch {
  594. parsedArguments = call.arguments
  595. }
  596. const result = await ctx.tools.execute({
  597. callId: call.id,
  598. name: call.name,
  599. arguments: parsedArguments,
  600. agent,
  601. signal,
  602. })
  603. session.append('tool/result', {
  604. turn, step,
  605. // The correlation id MUST be the loop's authoritative call.id (the
  606. // model-transcript id that deriveMessages turns into toolCallId), NOT
  607. // result.callId — a tools/execute waterfall listener returning a
  608. // mismatched id would otherwise orphan the call↔result pairing in the
  609. // next model request. A listener-internal id, if ever needed, belongs in
  610. // a separate diagnostic field, never overloaded onto callId.
  611. callId: call.id,
  612. content: result.content,
  613. isError: result.isError,
  614. ...result.error ? { error: result.error } : {},
  615. })
  616. // signal CAN flip during the await above (abort() inside a tool);
  617. // the analyzer can't see through the await boundary.
  618. /* v8 ignore start -- signal.reason default unreachable: cancel()/disposal always set it */
  619. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  620. if (signal.aborted) throw new Error(String(signal.reason ?? 'aborted'))
  621. /* v8 ignore stop */
  622. }
  623. return { hadToolCalls: toolCalls.length > 0, finish: assembler.finish }
  624. }
  625. function withoutToolCalls(message: Message): Message {
  626. return { ...message, content: message.content.filter(block => block.type !== 'tool-call') }
  627. }
  628. /** The last turn number in a (possibly seeded) session log, or 0. */
  629. export function lastTurnNumber(session: Session): number {
  630. const lastStart = session.events.findLast(event => event.type === 'turn/start')
  631. return lastStart?.data.turn ?? 0
  632. }
  633. /**
  634. * Whether a turn is currently open in the session log (a `turn/start` with no
  635. * matching later `turn/end`). Decided from the LOG, not agent status: status
  636. * can be `running` while no turn is open (an `agent/status` listener firing
  637. * before `turn/start`, or the post-`turn/end` flush window before status
  638. * returns to idle), so status is not a reliable open-turn signal. Used by
  639. * `inject()` to choose between appending into an open turn vs. wrapping the
  640. * injection in its own one-shot turn (the turn-enclosure RFC).
  641. */
  642. export function isTurnOpen(session: Session): boolean {
  643. const last = session.events.findLast(e => e.type === 'turn/start' || e.type === 'turn/end')
  644. return last?.type === 'turn/start'
  645. }