index.ts 39 KB

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