index.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /**
  2. * Concrete agent-loop plugin: creates scoped ReactLoopAgents, publishes them
  3. * through the agent/session registries, and owns their ordered teardown.
  4. *
  5. * @module @deepseek-ai/dsh-agent-loop
  6. */
  7. import { Context, FiberState, Service } from '@deepseek-ai/cordis'
  8. import { randomUUID } from 'node:crypto'
  9. import z from '@deepseek-ai/schemastery'
  10. import { z as zod } from 'zod'
  11. import { brandString } from '@deepseek-ai/dsh-brand'
  12. import { emitAgentEvent } from '@deepseek-ai/dsh-agent'
  13. import type {
  14. Agent,
  15. AgentFactory,
  16. AgentHandle,
  17. AgentOptions,
  18. AgentSetup,
  19. CreateAgentOptions,
  20. ResumeAgentOptions,
  21. SessionStartSource,
  22. TurnBoundaryProjection,
  23. } from '@deepseek-ai/dsh-agent'
  24. import { errorChain, ReasoningEffortId } from '@deepseek-ai/dsh-llm'
  25. import type {} from '@deepseek-ai/dsh-settings'
  26. import { interruptedTurnClosers, SessionLogOffset, SessionPreparation, SessionSeq } from '@deepseek-ai/dsh-session'
  27. import type { Session, SessionHeader, SessionId } from '@deepseek-ai/dsh-session'
  28. import type {} from '@deepseek-ai/dsh-system-prompt'
  29. import type {} from '@deepseek-ai/dsh-tools'
  30. import type {} from '@deepseek-ai/dsh-session-projection'
  31. import type { ProjectionDefinition } from '@deepseek-ai/dsh-session-projection'
  32. import { SessionPersistenceNotFoundError } from '@deepseek-ai/dsh-session-persistence'
  33. import type { SessionHandle, SessionPersistence } from '@deepseek-ai/dsh-session-persistence'
  34. import { ReactLoopAgent } from './agent.ts'
  35. import { DEFAULT_MAX_PARALLEL_TOOL_CALLS } from './constants.ts'
  36. /** Fiber states that cannot own or serve a new lifecycle. */
  37. const INACTIVE_STATES: ReadonlySet<FiberState> = new Set([
  38. FiberState.UNLOADING,
  39. FiberState.DISPOSED,
  40. FiberState.FAILED,
  41. ])
  42. const turnBoundaryProjectionSchema: zod.ZodType<TurnBoundaryProjection> = zod.object({
  43. openTurnStartSeq: zod.number().int().nonnegative().transform(SessionSeq).nullable(),
  44. lastStepStartSeq: zod.number().int().nonnegative().transform(SessionSeq).nullable(),
  45. lastStepBoundary: zod.object({
  46. kind: zod.union([zod.literal('start'), zod.literal('end')]),
  47. seq: zod.number().int().nonnegative().transform(SessionSeq),
  48. }).nullable(),
  49. lastTurn: zod.number().int().nonnegative(),
  50. })
  51. /** Host projection of agent turn and step boundaries. */
  52. export const turnBoundaryProjectionDefinition = {
  53. key: 'turnBoundary',
  54. stateVersion: 2,
  55. stateSchema: turnBoundaryProjectionSchema,
  56. init: () => ({
  57. openTurnStartSeq: null,
  58. lastStepStartSeq: null,
  59. lastStepBoundary: null,
  60. lastTurn: 0,
  61. }),
  62. apply: (state, event) => {
  63. switch (event.type) {
  64. case 'turn/start':
  65. return {
  66. ...state,
  67. openTurnStartSeq: event.seq,
  68. lastTurn: event.data.turn,
  69. }
  70. case 'turn/end':
  71. return {
  72. ...state,
  73. openTurnStartSeq: null,
  74. }
  75. case 'step/start':
  76. return {
  77. ...state,
  78. lastStepStartSeq: event.seq,
  79. lastStepBoundary: { kind: 'start', seq: event.seq },
  80. }
  81. case 'step/end':
  82. return {
  83. ...state,
  84. lastStepBoundary: { kind: 'end', seq: event.seq },
  85. }
  86. default:
  87. return state
  88. }
  89. },
  90. } satisfies ProjectionDefinition<'turnBoundary', TurnBoundaryProjection>
  91. /** Factory-level ownership: live agent teardowns plus config startup work. */
  92. class FactoryOwnership {
  93. private accepting = true
  94. private readonly teardown = new AbortController()
  95. private readonly inactive = Promise.withResolvers<void>()
  96. private readonly liveAgents = new Set<() => Promise<void>>()
  97. private startupTasks = new Set<Promise<void>>()
  98. constructor(private readonly fiber: Context['fiber']) {}
  99. /** Aborts (reason: `agent loop is not active` error) when factory teardown begins. */
  100. get signal(): AbortSignal {
  101. return this.teardown.signal
  102. }
  103. isActive(): boolean {
  104. return this.accepting && !INACTIVE_STATES.has(this.fiber.state)
  105. }
  106. /** Track one live agent's shared teardown until it has run. */
  107. track(dispose: () => Promise<void>): () => void {
  108. this.liveAgents.add(dispose)
  109. return () => { this.liveAgents.delete(dispose) }
  110. }
  111. /** Join config startup work that begins before an agent exists. */
  112. trackStartup(job: Promise<void>): void {
  113. this.startupTasks.add(job)
  114. const forget = () => { this.startupTasks.delete(job) }
  115. void job.then(forget, forget)
  116. }
  117. /** Join one public create/resume continuation; factory dispose awaits its settlement. */
  118. trackWrapper(job: Promise<unknown>): void {
  119. this.trackStartup(job.then(() => undefined, () => undefined))
  120. }
  121. /** Resolve `task`, or stop waiting when factory teardown begins. */
  122. async waitWhileActive(job: Promise<void>): Promise<void> {
  123. await Promise.race([job, this.inactive.promise])
  124. }
  125. async dispose(): Promise<void> {
  126. this.accepting = false
  127. this.teardown.abort(new Error('agent loop is not active'))
  128. this.inactive.resolve()
  129. await Promise.all([
  130. ...[...this.liveAgents].map(dispose => dispose()),
  131. ...this.startupTasks,
  132. ])
  133. }
  134. }
  135. /** Await `operation`, or throw the signal's reason as soon as it aborts. */
  136. async function raceAbort<T>(operation: PromiseLike<T> | T, signal: AbortSignal, id: SessionId): Promise<T> {
  137. const toAbortError = (): Error => signal.reason instanceof Error
  138. ? signal.reason
  139. : new Error(`agent "${id}" creation aborted`, { cause: signal.reason })
  140. if (signal.aborted) throw toAbortError()
  141. const aborted = Promise.withResolvers<never>()
  142. const listener = (): void => { aborted.reject(toAbortError()) }
  143. signal.addEventListener('abort', listener, { once: true })
  144. try {
  145. return await Promise.race([Promise.resolve(operation), aborted.promise])
  146. } finally {
  147. signal.removeEventListener('abort', listener)
  148. }
  149. }
  150. /** Start an abortable operation and release a value that arrives after cancellation. */
  151. async function raceAbortCall<T>(
  152. operation: () => PromiseLike<T> | T,
  153. signal: AbortSignal,
  154. id: SessionId,
  155. releaseAbandoned?: (value: T) => void,
  156. ): Promise<T> {
  157. if (signal.aborted) {
  158. throw signal.reason instanceof Error
  159. ? signal.reason
  160. : new Error(`agent "${id}" creation aborted`, { cause: signal.reason })
  161. }
  162. const pending = Promise.resolve().then(operation)
  163. try {
  164. return await raceAbort(pending, signal, id)
  165. } catch (error: unknown) {
  166. // oxlint-disable-next-line typescript/no-unnecessary-condition -- the signal can abort while the operation is awaited.
  167. if (signal.aborted && releaseAbandoned !== undefined) {
  168. void pending.then(releaseAbandoned, () => undefined)
  169. }
  170. throw error
  171. }
  172. }
  173. /** Resolve the deployment-wide scheduler cap at the owning config boundary. */
  174. function resolveMaxParallelToolCalls(value: number | undefined): number {
  175. const maxParallelToolCalls = value ?? DEFAULT_MAX_PARALLEL_TOOL_CALLS
  176. if (!Number.isInteger(maxParallelToolCalls) || maxParallelToolCalls < 1) {
  177. throw new Error('maxParallelToolCalls must be a positive integer')
  178. }
  179. return maxParallelToolCalls
  180. }
  181. /** Reject an output-token cap that cannot be represented exactly on the request wire. */
  182. function assertAgentOptions(options: AgentOptions): void {
  183. if (options.maxTokens !== undefined
  184. && (!Number.isSafeInteger(options.maxTokens) || options.maxTokens <= 0)) {
  185. throw new TypeError('agent maxTokens must be a positive safe integer')
  186. }
  187. }
  188. /** One session's owned write handle plus the count of events already stored through it. */
  189. interface StoredSession {
  190. readonly handle: SessionHandle
  191. storedCount: number
  192. }
  193. /** Prepared-but-unpublished agent resources sharing one memoized teardown. */
  194. interface PreparedAgent {
  195. agent: ReactLoopAgent
  196. /** Aborts when the factory unloads, the caller cancels, or teardown begins — ends any setup await. */
  197. signal: AbortSignal
  198. /** Enter registries, announce, notify session-start, and start the machine. */
  199. publish(source: SessionStartSource): AgentHandle
  200. /** Reverse teardown: stop the machine, unregister, unwind the scope. Memoized. */
  201. dispose(): Promise<void>
  202. }
  203. declare module '@deepseek-ai/cordis' {
  204. interface Context {
  205. agentLoop: AgentLoop
  206. /**
  207. * Launcher-owned exact session identities for configured agents, keyed by
  208. * the agent's config `id` and set with `ctx.provide()` before any Loader
  209. * entry mounts (see {@link CONFIGURED_AGENT_IDENTITIES_KEY}). A launcher
  210. * owns identity because only it knows whether the session already exists,
  211. * while the `cordis.yml` row keeps the model route as ordinary patchable
  212. * config. An entry with no matching key keeps its configured identity.
  213. */
  214. configuredAgentIdentities?: ConfiguredAgentIdentities
  215. }
  216. interface Events {
  217. /**
  218. * A declarative agent entry failed before it could publish a live agent.
  219. * Consumers that buffer work for the configured identity use this
  220. * transient signal to reject that work instead of waiting forever. Normal
  221. * factory teardown suppresses failures from the cancelled startup attempt.
  222. * @param payload.sessionId - exact shared agent/session identity that failed startup.
  223. * @param payload.error - persistence, setup, or publication failure.
  224. * @mode emit
  225. */
  226. 'agent-loop/config-start-failed'(payload: { sessionId: SessionId; error: unknown }): void
  227. }
  228. }
  229. export { DEFAULT_MAX_PARALLEL_TOOL_CALLS }
  230. /**
  231. * One launcher-selected session identity for a configured agent. `resume`
  232. * distinguishes rehydrating existing persisted history from creating the
  233. * session fresh under that exact id, which the two config keys express as
  234. * `resumeSessionId` and `sessionId`.
  235. */
  236. export interface LauncherAgentIdentity {
  237. /** Exact session id to create fresh or resume. */
  238. id: SessionId
  239. /** Resume existing persisted history instead of creating the session fresh. */
  240. resume: boolean
  241. }
  242. /** Launcher-selected identities keyed by the configured agent's `id`. */
  243. export interface ConfiguredAgentIdentities extends Readonly<Record<string, LauncherAgentIdentity>> {}
  244. /**
  245. * Context key a launcher sets before any Loader entry mounts
  246. * (`ctx.provide(CONFIGURED_AGENT_IDENTITIES_KEY, identities)`) to fix
  247. * configured agents' session identities without a config key, so an overlay
  248. * repointing the row's model route cannot drop them.
  249. */
  250. export const CONFIGURED_AGENT_IDENTITIES_KEY = 'configuredAgentIdentities'
  251. /**
  252. * Apply launcher-owned identities over the configured agents, replacing both
  253. * identity keys for every entry the launcher named so a config-supplied
  254. * identity can never survive alongside a launcher-supplied one.
  255. * @param agents - the configured agent entries.
  256. * @param identities - launcher identities keyed by configured agent `id`, or `undefined`.
  257. * @returns the entries with launcher-owned identities applied.
  258. */
  259. function applyLauncherIdentities(
  260. agents: Config['agents'],
  261. identities: ConfiguredAgentIdentities | undefined,
  262. ): Config['agents'] {
  263. if (identities === undefined) return agents
  264. return agents.map((agent) => {
  265. const identity = identities[agent.id]
  266. if (identity === undefined) return agent
  267. const { sessionId: _sessionId, resumeSessionId: _resumeSessionId, ...rest } = agent
  268. return identity.resume
  269. ? { ...rest, resumeSessionId: identity.id }
  270. : { ...rest, sessionId: identity.id }
  271. })
  272. }
  273. /** Settings namespace carrying the tool-call parallelism a user owns. */
  274. export const AGENT_LOOP_SETTINGS_NAMESPACE = 'agent-loop'
  275. /**
  276. * The agent-loop fields a user owns. Deliberately a strict subset of
  277. * {@link Config}: `agents` is a boot-time composition array consumed once when
  278. * the service starts, so a stored change could only look like it had an effect.
  279. */
  280. export interface AgentLoopSettings {
  281. /** Maximum parallel-safe calls in flight per agent step. */
  282. maxParallelToolCalls: number
  283. }
  284. /** Schema of the agent-loop settings section. */
  285. export const AGENT_LOOP_SETTINGS_SCHEMA: z<AgentLoopSettings> = z.object({
  286. maxParallelToolCalls: z.number().step(1).min(1).default(DEFAULT_MAX_PARALLEL_TOOL_CALLS),
  287. })
  288. /** Agent-loop plugin configuration. */
  289. export interface Config {
  290. /**
  291. * Maximum parallel-safe calls in flight per agent step. `1` is serial;
  292. * omission defaults to {@link DEFAULT_MAX_PARALLEL_TOOL_CALLS}.
  293. */
  294. maxParallelToolCalls?: number
  295. /** Agents created or resumed at plugin startup. */
  296. agents: (AgentOptions & {
  297. /** Stable config label used in logs and as the fresh combined-id prefix. */
  298. id: string
  299. /** Optional stable identity; remounts resume its materialized history, while first use creates it fresh. */
  300. sessionId?: SessionId
  301. /** Optional workspace for a fresh session. */
  302. cwd?: string
  303. /** Persisted session to resume instead of creating a fresh session. */
  304. resumeSessionId?: SessionId
  305. })[]
  306. }
  307. /** Agent-loop configuration after defaults and load-time validation. */
  308. type ResolvedConfig = Config & { maxParallelToolCalls: number }
  309. /** Reject self-contained identity conflicts before any configured agent starts. */
  310. function validateConfiguredAgents(agents: Config['agents']): void {
  311. const exactIdentities = new Map<SessionId, string>()
  312. for (const { id, sessionId, resumeSessionId } of agents) {
  313. const hasResumeId = resumeSessionId !== undefined && resumeSessionId !== ''
  314. if (sessionId !== undefined && hasResumeId) {
  315. throw new Error(`agent "${id}": sessionId and resumeSessionId are mutually exclusive`)
  316. }
  317. const exactIdentity = hasResumeId ? resumeSessionId : sessionId
  318. if (exactIdentity === undefined) continue
  319. const firstId = exactIdentities.get(exactIdentity)
  320. if (firstId !== undefined) {
  321. throw new Error(`agents "${firstId}" and "${id}" use duplicate exact session identity "${exactIdentity}"`)
  322. }
  323. exactIdentities.set(exactIdentity, id)
  324. }
  325. }
  326. /** Concrete agent factory and driver service. */
  327. export class AgentLoop extends Service implements AgentFactory {
  328. static inject = ['agents', 'sessions', 'llm', 'tools', 'systemPrompt', 'sessionProjections']
  329. /** Runtime schema for declarative agents. */
  330. static Config = z.object({
  331. maxParallelToolCalls: z.number().step(1).min(1).default(DEFAULT_MAX_PARALLEL_TOOL_CALLS),
  332. agents: z.array(z.object({
  333. id: z.string().required(),
  334. sessionId: z.string().min(1),
  335. provider: z.string(),
  336. model: z.string(),
  337. reasoningEffort: z.string().min(1) as z<ReturnType<typeof ReasoningEffortId>>,
  338. maxTokens: z.number().step(1).min(1).max(Number.MAX_SAFE_INTEGER),
  339. cwd: z.string(),
  340. resumeSessionId: z.string(),
  341. })).default([]),
  342. }) as z<Config>
  343. /** Validated configuration owned by the agent-loop service. */
  344. readonly config: ResolvedConfig
  345. private readonly ownership: FactoryOwnership
  346. /** Plain holder prevents Cordis from re-tracing the factory's dependency context through a caller shadow. */
  347. private readonly runtime: { ctx: Context }
  348. constructor(ctx: Context, config: Config) {
  349. super(ctx, 'agentLoop')
  350. const entry: AgentLoopSettings = {
  351. maxParallelToolCalls: resolveMaxParallelToolCalls(config.maxParallelToolCalls),
  352. }
  353. let source: () => AgentLoopSettings = () => entry
  354. this.config = {
  355. ...config,
  356. agents: applyLauncherIdentities(config.agents, ctx.get(CONFIGURED_AGENT_IDENTITIES_KEY)),
  357. // Read through on every scheduler decision: `tool-calls.ts` destructures
  358. // this at the start of each group, so a committed change caps the next
  359. // group without disturbing the one in flight.
  360. get maxParallelToolCalls() {
  361. return source().maxParallelToolCalls
  362. },
  363. }
  364. ctx.inject(['settings'], (settingsCtx) => {
  365. settingsCtx.settings.installSection(ctx, AGENT_LOOP_SETTINGS_NAMESPACE, AGENT_LOOP_SETTINGS_SCHEMA, entry, {
  366. // The schema admits any integer above zero; `resolveMaxParallelToolCalls`
  367. // owns the whole rule, so refusing here keeps the running scheduler on
  368. // its last good cap instead of failing at the next tool group.
  369. validate: value => void resolveMaxParallelToolCalls(value.maxParallelToolCalls),
  370. setSource: (current) => {
  371. source = current
  372. },
  373. // Nothing is derived from the cap: the getter above is the only reader.
  374. onChange: () => {},
  375. })
  376. })
  377. validateConfiguredAgents(this.config.agents)
  378. // Register only after every config validation above has passed, so a
  379. // rejected constructor leaves no projection unit behind.
  380. ctx.sessionProjections.register(turnBoundaryProjectionDefinition)
  381. this.ownership = new FactoryOwnership(ctx.fiber)
  382. this.runtime = { ctx }
  383. ctx.effect(() => () => this.ownership.dispose(), 'agentLoop.transactions()')
  384. ctx.effect(() => ctx.agents.setFactory(this), 'agentLoop.setFactory()')
  385. ctx.systemPrompt.variable('provider', context => context.agent?.options.provider)
  386. ctx.systemPrompt.variable('model', context => context.agent?.options.model)
  387. ctx.systemPrompt.variable('cwd', context => context.agent?.session.header.cwd)
  388. for (const { id, sessionId, cwd, resumeSessionId, ...options } of this.config.agents) {
  389. const meta = cwd === undefined ? {} : { cwd }
  390. if (resumeSessionId === undefined || resumeSessionId === '') {
  391. const configuredId = sessionId ?? brandString<SessionId>(`${id}-session-${randomUUID()}`)
  392. const persistence = sessionId === undefined ? undefined : ctx.get('sessionPersistence')
  393. if (persistence === undefined) {
  394. const startup = this.create(configuredId, options, meta).then(() => undefined, (error: unknown) => {
  395. this.reportConfiguredStartupFailure(id, 'restore', configuredId, error)
  396. })
  397. this.ownership.trackStartup(startup)
  398. } else {
  399. const startup = this.restoreOrCreateConfigured(ctx, persistence, configuredId, options, meta).catch((error: unknown) => {
  400. this.reportConfiguredStartupFailure(id, 'restore', configuredId, error)
  401. })
  402. this.ownership.trackStartup(startup)
  403. }
  404. continue
  405. }
  406. ctx.effect(() => {
  407. const fiber = ctx.inject(['sessionPersistence'], (childCtx: Context) => {
  408. void this.resumeWith(ctx, childCtx.sessionPersistence, {
  409. resumeSessionId,
  410. agentOptions: options,
  411. }).catch((error: unknown) => {
  412. this.reportConfiguredStartupFailure(id, 'resume', resumeSessionId, error)
  413. })
  414. })
  415. return fiber.dispose
  416. }, `agentLoop.resume(${id})`)
  417. }
  418. }
  419. /** Report a contained declarative-start failure to identity-bound consumers. */
  420. private reportConfiguredStartupFailure(
  421. configId: string,
  422. action: 'restore' | 'resume',
  423. sessionId: SessionId,
  424. error: unknown,
  425. ): void {
  426. if (!this.ownership.isActive()) return
  427. this.ctx.logger.warn(`agent "${configId}": config-driven ${action} of "${sessionId}" failed: ${errorChain(error)}`)
  428. const args: unknown[] = ['agent-loop/config-start-failed', { sessionId, error }]
  429. for (const callback of this.ctx.events.dispatch('emit', args)) {
  430. try {
  431. const returned: unknown = callback(...args)
  432. void Promise.resolve(returned).catch((listenerError: unknown) => {
  433. this.ctx.logger.warn(`agent "${configId}": config-start-failed listener rejected: ${errorChain(listenerError)}`)
  434. })
  435. } catch (listenerError: unknown) {
  436. this.ctx.logger.warn(`agent "${configId}": config-start-failed listener threw: ${errorChain(listenerError)}`)
  437. }
  438. }
  439. }
  440. /** Restore a materialized exact config identity on remount, or create it on first use. */
  441. private async restoreOrCreateConfigured(
  442. ownerCtx: Context,
  443. persistence: SessionPersistence,
  444. sessionId: SessionId,
  445. agentOptions: AgentOptions,
  446. meta: Pick<SessionHeader, 'cwd'>,
  447. ): Promise<void> {
  448. await this.waitForDrainingConfiguredIdentity(ownerCtx, sessionId)
  449. if (!this.ownership.isActive()) return
  450. try {
  451. await this.resumeWith(ownerCtx, persistence, { resumeSessionId: sessionId, agentOptions })
  452. return
  453. } catch (error: unknown) {
  454. if (!this.ownership.isActive()) return
  455. // Only a genuinely absent stored session falls back to first creation;
  456. // corruption, ownership conflicts, and backend failures stay loud.
  457. if (!(error instanceof SessionPersistenceNotFoundError)) throw error
  458. }
  459. await this.create(sessionId, agentOptions, meta)
  460. }
  461. /** Wait for a draining same-id lifecycle to finish registry teardown. */
  462. private async waitForDrainingConfiguredIdentity(ownerCtx: Context, sessionId: SessionId): Promise<void> {
  463. // Only an id still occupying a registry needs waiting for; a live healthy
  464. // occupant is a collision the create/resume below will surface itself.
  465. if (ownerCtx.agents.get(sessionId) === undefined && ownerCtx.sessions.get(sessionId) === undefined) return
  466. const released = Promise.withResolvers<void>()
  467. const checkReleased = (): void => {
  468. if (ownerCtx.agents.get(sessionId) === undefined && ownerCtx.sessions.get(sessionId) === undefined) {
  469. released.resolve()
  470. }
  471. }
  472. const disposeAgentListener = ownerCtx.on('agent/disposed', () => { checkReleased() })
  473. const disposeSessionListener = ownerCtx.on('session/disposed', checkReleased)
  474. try {
  475. checkReleased()
  476. await this.ownership.waitWhileActive(released.promise)
  477. } finally {
  478. disposeAgentListener()
  479. disposeSessionListener()
  480. }
  481. }
  482. /**
  483. * Construct the driver, scope, and one memoized reverse teardown for a new
  484. * agent. The teardown is registered with the factory and the owner fiber
  485. * BEFORE publication, so a mid-setup unload rolls everything back; `signal`
  486. * fuses caller cancellation with lifecycle teardown for setup awaits.
  487. */
  488. private prepare(
  489. ownerCtx: Context,
  490. id: SessionId,
  491. options: AgentOptions,
  492. session: Session,
  493. callerSignal?: AbortSignal,
  494. handle?: SessionHandle,
  495. ): PreparedAgent {
  496. assertAgentOptions(options)
  497. ownerCtx.fiber.assertActive()
  498. // Every caller reaches prepare() synchronously from a service method
  499. // whose Cordis dispatch already requires the live factory fiber, or
  500. // re-checks ownership itself after its awaits (resume's load barrier).
  501. /* v8 ignore next -- unreachable backstop, see above */
  502. if (!this.ownership.isActive()) throw new Error('agent loop is not active')
  503. if (callerSignal?.aborted) {
  504. throw callerSignal.reason instanceof Error
  505. ? callerSignal.reason
  506. : new Error(`agent "${id}" creation aborted`, { cause: callerSignal.reason })
  507. }
  508. const loopCtx = this.runtime.ctx
  509. // Deactivation fuses three owners, each with its own reason: the caller's
  510. // cancellation signal, the owner fiber's unload, and factory teardown.
  511. // It is registered BEFORE any resource exists, over mutable slots, so an
  512. // unload arriving while the scope is still minting finds a working
  513. // disposer instead of a leak.
  514. const abort = new AbortController()
  515. const onCallerAbort = (): void => {
  516. abort.abort(callerSignal?.reason instanceof Error
  517. ? callerSignal.reason
  518. : new Error(`agent "${id}" creation aborted`, { cause: callerSignal?.reason }))
  519. }
  520. const onFactoryTeardown = (): void => { abort.abort(this.ownership.signal.reason) }
  521. callerSignal?.addEventListener('abort', onCallerAbort, { once: true })
  522. this.ownership.signal.addEventListener('abort', onFactoryTeardown, { once: true })
  523. let machine: ReactLoopAgent | undefined
  524. let detachSession: (() => void) | undefined
  525. let detachAgent: (() => void) | undefined
  526. let disposing: Promise<void> | undefined
  527. const machineReady = Promise.withResolvers<void>()
  528. // Reverse teardown, memoized so every racing owner awaits one quiescence:
  529. // stop the machine, drain and close the session's write path, leave the
  530. // registries, unwind the scope, release bookkeeping.
  531. const dispose = (ownerTriggered = false): Promise<void> => (disposing ??= (async () => {
  532. abort.abort(new Error(`agent "${id}" lifecycle disposed`))
  533. callerSignal?.removeEventListener('abort', onCallerAbort)
  534. this.ownership.signal.removeEventListener('abort', onFactoryTeardown)
  535. // Teardown failures are collected, never swallowed: registry, scope,
  536. // and ownership cleanup always run to quiescence, then the memoized
  537. // disposal rejects with what failed so every racing owner observes it.
  538. const failures: unknown[] = []
  539. try {
  540. // Disposal IS a disposed-cause cancel followed by quiescence. New work
  541. // sent after this point is the sender's bug — the registries are about
  542. // to drop the agent, so nothing should still hold it.
  543. if (machine === undefined) await machineReady.promise
  544. if (machine !== undefined) {
  545. machine.cancel({ kind: 'disposed' })
  546. await machine.whenIdle()
  547. await machine.scope.dispose()
  548. }
  549. } catch (error: unknown) {
  550. failures.push(error)
  551. }
  552. // The loop above committed its closing events synchronously into the
  553. // session; handle close drains them durably before releasing the write
  554. // path. The close drain can be the first operation that surfaces a
  555. // durability failure, so its error is retained, not logged away.
  556. try {
  557. await handle?.close()
  558. } catch (error: unknown) {
  559. failures.push(error)
  560. }
  561. try {
  562. detachAgent?.()
  563. detachSession?.()
  564. } finally {
  565. untrack()
  566. if (!ownerTriggered) await unfollowOwner()
  567. }
  568. if (failures.length === 1) throw failures[0]
  569. if (failures.length > 1) {
  570. throw new AggregateError(failures, `agent "${id}" disposal failed`)
  571. }
  572. })())
  573. const untrack = this.ownership.track(dispose)
  574. let unfollowOwner: () => Promise<void> | void
  575. try {
  576. unfollowOwner = ownerCtx.effect(() => () => {
  577. // Owner disposal owns the same quiescence boundary. Its teardown skips
  578. // unregistering this already-running owner effect from inside itself.
  579. if (disposing !== undefined) return
  580. abort.abort(new Error(`agent "${id}" setup aborted: owner disposed during setup`))
  581. return dispose(true)
  582. }, `agentLoop.lifecycle(${id})`)
  583. /* v8 ignore start -- ctx.effect throws only on an inactive fiber, which assertActive() above already rejected */
  584. } catch (error: unknown) {
  585. untrack()
  586. callerSignal?.removeEventListener('abort', onCallerAbort)
  587. this.ownership.signal.removeEventListener('abort', onFactoryTeardown)
  588. throw error
  589. }
  590. /* v8 ignore stop */
  591. const assertLive = (): void => {
  592. if (!abort.signal.aborted) return
  593. // Every fused abort source carries an Error reason: onCallerAbort and
  594. // raceAbort wrap non-Error caller reasons, and the factory/lifecycle
  595. // owners abort with constructed Errors.
  596. /* v8 ignore next -- unreachable String() arm, see above */
  597. throw abort.signal.reason instanceof Error ? abort.signal.reason : new Error(String(abort.signal.reason))
  598. }
  599. try {
  600. const agent = machine = new ReactLoopAgent(loopCtx, id, options, session)
  601. machineReady.resolve()
  602. assertLive()
  603. return {
  604. agent,
  605. signal: abort.signal,
  606. publish: (source) => {
  607. assertLive()
  608. detachSession = agent.ctx.sessions.enter(session)
  609. // The mounted backend routes announced live events into the active
  610. // write handle by session id; the loop only owns the handle itself.
  611. detachAgent = loopCtx.agents.enter(agent, ownerCtx.agent)
  612. agent.ctx.sessions.announce(session)
  613. assertLive()
  614. loopCtx.agents.announce(agent)
  615. assertLive()
  616. // A synchronous announce/session-start listener may have started
  617. // teardown; the machine is already live (delivery works from the
  618. // session-start extension point), so only the liveness recheck is owed.
  619. emitAgentEvent(loopCtx, agent, 'agent/session-start', { source })
  620. assertLive()
  621. return { agent, dispose }
  622. },
  623. dispose,
  624. }
  625. } catch (error: unknown) {
  626. machineReady.resolve()
  627. // Rollback swallows a disposal rejection: the setup failure is primary.
  628. void dispose().catch(() => {})
  629. throw error
  630. }
  631. }
  632. /**
  633. * Create an agent and session under one caller-supplied identity, owned by
  634. * the accessing fiber. Constructor-driven config calls mint a fresh combined
  635. * id before entering this boundary. When a persistence backend is mounted,
  636. * the session's durable identity and any seed are stored before publication.
  637. * @param id - shared agent/session identity.
  638. * @param options - concrete loop options.
  639. * @param meta - optional fresh-session workspace metadata.
  640. * @returns the published running agent.
  641. */
  642. async create(id: SessionId, options: AgentOptions = {}, meta: Pick<SessionHeader, 'cwd'> = {}): Promise<Agent> {
  643. using preparation = SessionPreparation.create(this.runtime.ctx.sessions.prepare(id, { meta }))
  644. const stored = await this.createStoredSession(preparation.session)
  645. let prepared: PreparedAgent
  646. try {
  647. prepared = this.prepare(this.ctx, id, options, preparation.session, undefined, stored?.handle)
  648. } catch (error: unknown) {
  649. await stored?.handle.close().catch(() => {})
  650. throw error
  651. }
  652. try {
  653. await this.appendUnstoredSuffix(stored, preparation.session)
  654. return prepared.publish('startup').agent
  655. } catch (error: unknown) {
  656. // Rollback swallows a disposal rejection: the setup failure is primary.
  657. void prepared.dispose().catch(() => {})
  658. throw error
  659. }
  660. }
  661. /**
  662. * Take a fresh session's write ownership when persistence is mounted.
  663. * Nothing is appended here: the constructor seed (which never re-emits
  664. * through `session/event`) is stored by `appendUnstoredSuffix` at the
  665. * publication commit point, so a failed or cancelled validation or setup
  666. * closes an unmaterialized handle and leaves no stored residue — the same
  667. * id can be created again.
  668. * @param session - the unpublished session to store.
  669. * @param signal - optional cancellation forwarded to the backend create.
  670. * @returns the owned handle and stored cursor, or `undefined` without a backend.
  671. */
  672. private async createStoredSession(session: Session, signal?: AbortSignal): Promise<StoredSession | undefined> {
  673. const persistence = this.runtime.ctx.get('sessionPersistence')
  674. if (persistence === undefined) return undefined
  675. const handle = await persistence.create(session.header, {
  676. inheritedEventCount: session.inheritedEventCount,
  677. ...signal === undefined ? {} : { signal },
  678. })
  679. return { handle, storedCount: 0 }
  680. }
  681. /**
  682. * Durably store the session events appended since the last stored cursor.
  683. * Pre-publication appends (constructor seed markers, setup-window events
  684. * such as delegation policy records) never re-emit through `session/event`,
  685. * so publication must flush them through the handle before live events
  686. * start routing into it.
  687. * @param stored - the session's owned handle and stored cursor, if any.
  688. * @param session - the unpublished session whose suffix is stored.
  689. */
  690. private async appendUnstoredSuffix(stored: StoredSession | undefined, session: Session): Promise<void> {
  691. if (stored === undefined) return
  692. const suffix = session.snapshotEvents(SessionLogOffset(stored.storedCount))
  693. if (suffix.length > 0) await stored.handle.append(suffix)
  694. // Advance by what was stored, not to `session.seq`: an event appended
  695. // during the await must stay unstored for the next flush.
  696. stored.storedCount += suffix.length
  697. }
  698. /**
  699. * Create an owned agent on a caller-supplied session id.
  700. * @param ownerCtx - caller context that structurally owns the lifecycle.
  701. * @param options - identities, session seed/metadata, loop options, setup, and cancellation.
  702. * @returns the published handle.
  703. */
  704. async createAgent(ownerCtx: Context, options: CreateAgentOptions): Promise<AgentHandle> {
  705. const preparation = SessionPreparation.create(this.runtime.ctx.sessions.prepare(options.sessionId, {
  706. ...options.seed === undefined ? {} : { seed: options.seed },
  707. ...options.meta === undefined ? {} : { meta: options.meta },
  708. ...options.inheritedEventCount === undefined ? {} : { inheritedEventCount: options.inheritedEventCount },
  709. }))
  710. const published = (async () => {
  711. let stored: StoredSession | undefined
  712. try {
  713. // raceAbortCall normalizes a pre-aborted or mid-create abort and
  714. // closes a handle that finishes creating after abandonment.
  715. stored = options.signal === undefined
  716. ? await this.createStoredSession(preparation.session)
  717. : await raceAbortCall(
  718. () => this.createStoredSession(preparation.session, options.signal),
  719. options.signal,
  720. options.sessionId,
  721. (abandoned) => { void abandoned?.handle.close().catch(() => {}) },
  722. )
  723. } catch (error: unknown) {
  724. preparation[Symbol.dispose]()
  725. throw error
  726. }
  727. return this.setupAndPublish(
  728. ownerCtx,
  729. options.sessionId,
  730. preparation,
  731. options.agentOptions ?? {},
  732. options.setup,
  733. options.signal,
  734. 'startup',
  735. stored,
  736. )
  737. })()
  738. this.ownership.trackWrapper(published)
  739. return published
  740. }
  741. /** Prepare one Agent around an acquired Session, run setup, and publish it. */
  742. private async setupAndPublish(
  743. ownerCtx: Context,
  744. id: SessionId,
  745. preparation: SessionPreparation,
  746. agentOptions: AgentOptions,
  747. setup: AgentSetup | undefined,
  748. signal: AbortSignal | undefined,
  749. source: SessionStartSource,
  750. stored?: StoredSession,
  751. ): Promise<AgentHandle> {
  752. using ownedPreparation = preparation
  753. const session = ownedPreparation.session
  754. let prepared: PreparedAgent
  755. try {
  756. prepared = this.prepare(ownerCtx, id, agentOptions, session, signal, stored?.handle)
  757. } catch (error: unknown) {
  758. await stored?.handle.close().catch(() => {})
  759. throw error
  760. }
  761. try {
  762. const setupCommit = await raceAbort(setup?.(prepared.agent.ctx), prepared.signal, id)
  763. setupCommit?.commit()
  764. await this.appendUnstoredSuffix(stored, session)
  765. return prepared.publish(source)
  766. } catch (error: unknown) {
  767. // Rollback swallows a disposal rejection (a failing final handle close):
  768. // the setup failure is the primary error the caller must see.
  769. await prepared.dispose().catch(() => {})
  770. throw error
  771. }
  772. }
  773. /**
  774. * Resume an owned agent from the configured persistence service.
  775. * @param ownerCtx - caller context that owns load, setup, and the live lifecycle.
  776. * @param options - persisted identity, loop options, setup, and cancellation.
  777. * @returns the published handle.
  778. */
  779. async resume(ownerCtx: Context, options: ResumeAgentOptions): Promise<AgentHandle> {
  780. const persistence = this.runtime.ctx.get('sessionPersistence')
  781. if (persistence === undefined) {
  782. throw new Error('cannot resume: session persistence is not configured (load a dsh-session-persistence backend)')
  783. }
  784. return this.resumeWith(ownerCtx, persistence, options)
  785. }
  786. /** Resume through an explicit persistence handle used by the deferred config path. */
  787. private resumeWith(
  788. ownerCtx: Context,
  789. persistence: SessionPersistence,
  790. options: ResumeAgentOptions,
  791. ): Promise<AgentHandle> {
  792. const id = options.resumeSessionId
  793. const published = (async () => {
  794. // The open and read may outlive their owner: race them against caller
  795. // cancellation, owner-fiber unload, and factory teardown so a
  796. // never-settling backend cannot pin the identity.
  797. const ownerAbort = new AbortController()
  798. const unfollowOwner = ownerCtx.effect(() => () => {
  799. ownerAbort.abort(new Error(`agent "${id}" setup aborted: owner disposed during setup`))
  800. }, `agentLoop.resume-load(${id})`)
  801. const fused = AbortSignal.any([
  802. ...options.signal === undefined ? [] : [options.signal],
  803. ownerAbort.signal,
  804. this.ownership.signal,
  805. ])
  806. let handle: SessionHandle | undefined
  807. let stored: StoredSession | undefined
  808. let preparation: SessionPreparation | undefined
  809. try {
  810. try {
  811. // Taking write ownership FIRST excludes a concurrent resume of the
  812. // same id (in this process, a live agent's handle holds the claim).
  813. handle = await raceAbortCall(
  814. () => persistence.open(id, 'write', { signal: fused }),
  815. fused,
  816. id,
  817. (abandoned) => { void abandoned.close() },
  818. )
  819. // Semantic crash repair is the agent layer's job: persistence hands
  820. // back the physically valid log; an interrupted final turn receives
  821. // synthetic closers (missing tool errors, step/end, turn/end) that
  822. // are appended through the same handle as an ordinary batch.
  823. const coldRead = await handle.read(0, undefined, { signal: fused })
  824. fused.throwIfAborted()
  825. const persisted = coldRead.events
  826. const closers = interruptedTurnClosers(persisted)
  827. if (closers.length > 0) await handle.append(closers)
  828. preparation = SessionPreparation.create(this.runtime.ctx.sessions.prepare(id, {
  829. seed: [...persisted, ...closers],
  830. meta: structuredClone(handle.header),
  831. inheritedEventCount: handle.inheritedEventCount,
  832. eventState: coldRead.eventState,
  833. }))
  834. stored = { handle, storedCount: persisted.length + closers.length }
  835. await this.appendUnstoredSuffix(stored, preparation.session)
  836. } finally {
  837. await unfollowOwner()
  838. }
  839. ownerCtx.fiber.assertActive()
  840. if (!this.ownership.isActive()) throw new Error('agent loop is not active')
  841. const owned = stored
  842. handle = undefined // ownership passes to setupAndPublish/prepare
  843. return await this.setupAndPublish(
  844. ownerCtx,
  845. id,
  846. preparation,
  847. options.agentOptions ?? {},
  848. options.setup,
  849. options.signal,
  850. 'resume',
  851. owned,
  852. )
  853. } finally {
  854. preparation?.[Symbol.dispose]()
  855. await handle?.close().catch(() => {})
  856. }
  857. })()
  858. this.ownership.trackWrapper(published)
  859. return published
  860. }
  861. }
  862. export default AgentLoop