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