MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / createExecutionEngine

Function createExecutionEngine

packages/core/execution/src/engine.ts:532–822  ·  view source on GitHub ↗
(
  config: ExecutionEngineConfig<E>,
)

Source from the content-addressed store, hash-verified

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

Callers 11

makeExecutionStackFunction · 0.90
engine.test.tsFile · 0.90
startMcpHarnessFunction · 0.90
createExecutorMcpServerFunction · 0.90
openSessionFunction · 0.90
makeMcpFetchFunction · 0.90
startHarnessFunction · 0.90
createServerHandlersFunction · 0.90
localFixedExecutionLayerFunction · 0.90
startHarnessFunction · 0.90

Calls 12

buildExecuteDescriptionFunction · 0.90
annotateExecuteOutcomeFunction · 0.85
makeFullInvokerFunction · 0.85
recordSettledOutcomeFunction · 0.85
awaitCompletionOrPauseFunction · 0.85
annotateExecutionOutcomeFunction · 0.85
clearMethod · 0.80
executeMethod · 0.65
syncMethod · 0.65
deleteMethod · 0.65
getMethod · 0.65
setMethod · 0.65

Tested by 5

startMcpHarnessFunction · 0.72
openSessionFunction · 0.72
makeMcpFetchFunction · 0.72
startHarnessFunction · 0.72
startHarnessFunction · 0.72