| 545 | }; |
| 546 | |
| 547 | export const createExecutionEngine = <E extends Cause.YieldableError = CodeExecutionError>( |
| 548 | config: ExecutionEngineConfig<E>, |
| 549 | ): ExecutionEngine<E> => { |
| 550 | const { executor, codeExecutor, toolDiscoveryProvider = defaultToolDiscoveryProvider } = config; |
| 551 | const pausedExecutions = new Map<string, InternalPausedExecution<E>>(); |
| 552 | // Every sandbox fiber `startPausableExecution` still has in flight. |
| 553 | // |
| 554 | // Those fibers are daemons (`Effect.forkDetach`) so a pause can outlive the |
| 555 | // caller that observed it. But they close over `executor`, and the executor |
| 556 | // closes over the FumaDB handle the host opened for whatever scope built THIS |
| 557 | // engine — `makeFumaClient` captures `db` at construction, not per operation. |
| 558 | // A host that builds one engine per HTTP request therefore needs a way to end |
| 559 | // that fiber's life with the request; otherwise it wakes up after the |
| 560 | // request's postgres pool has been closed and every query it makes lands on a |
| 561 | // dead pool. `shutdown` below is that seam. |
| 562 | const liveSandboxFibers = new Set<Fiber.Fiber<ExecuteResult, E>>(); |
| 563 | // Outcomes of executions that already settled (resumed to completion, hit a |
| 564 | // new pause, or died while paused). MCP clients retry `resume` when a |
| 565 | // response gets lost in transit; without this cache the retry of an |
| 566 | // already-delivered resume answers "no paused execution" (observed in |
| 567 | // production seconds after a successful resume). Bounded FIFO — pause |
| 568 | // volume is tiny (human approvals), so a small window is plenty. |
| 569 | const settledOutcomes = new Map<string, Exit.Exit<ExecutionResult, E>>(); |
| 570 | const SETTLED_OUTCOME_LIMIT = 64; |
| 571 | const settledExecutionIds = new Set<string>(); |
| 572 | const SETTLED_EXECUTION_ID_LIMIT = 1024; |
| 573 | // Resumes whose outcome is still being computed, so a concurrent duplicate |
| 574 | // awaits the same result instead of missing the (already-consumed) pause. |
| 575 | const pendingResumes = new Map< |
| 576 | string, |
| 577 | { |
| 578 | readonly outcome: Deferred.Deferred<ExecutionResult, E>; |
| 579 | readonly orgWriteAccess: OrgWriteAccessState; |
| 580 | } |
| 581 | >(); |
| 582 | |
| 583 | // Exits (not just successes) so a replayed failure re-fails through the |
| 584 | // typed channel — hosts render engine failures opaquely, and a replay must |
| 585 | // not bypass that by flattening the cause into result text. |
| 586 | const recordSettledOutcome = (executionId: string, exit: Exit.Exit<ExecutionResult, E>): void => { |
| 587 | settledExecutionIds.add(executionId); |
| 588 | while (settledExecutionIds.size > SETTLED_EXECUTION_ID_LIMIT) { |
| 589 | const oldest = settledExecutionIds.keys().next().value; |
| 590 | if (oldest === undefined) break; |
| 591 | settledExecutionIds.delete(oldest); |
| 592 | } |
| 593 | settledOutcomes.set(executionId, exit); |
| 594 | while (settledOutcomes.size > SETTLED_OUTCOME_LIMIT) { |
| 595 | const oldest = settledOutcomes.keys().next().value; |
| 596 | if (oldest === undefined) break; |
| 597 | settledOutcomes.delete(oldest); |
| 598 | } |
| 599 | }; |
| 600 | |
| 601 | /** |
| 602 | * Race a running fiber against the pause queue. Returns when either |
| 603 | * the fiber completes or an elicitation handler fires (whichever |
| 604 | * comes first). Re-used by both executeWithPause and resume. |