Skip to content

perf(platform): evaluate automations faster — template fast path, scope views, runner pool - #4693

Merged
yannickmonney merged 5 commits into
mainfrom
perf/automation-engine
Oct 10, 2026
Merged

yannickmonney merged 5 commits into
mainfrom
perf/automation-engine

Conversation

@yannickmonney

Copy link
Copy Markdown
Contributor

What this changes

The first three slices of the faster automation engine. None of them changes what a run does; each makes it cheaper.

  • Benches (perf.bench.ts, stepper.perf.bench.ts): what a run costs, with the cases the engine cannot run yet listed as todo. The baseline replaces the design's estimates. Its execute() figures did not reproduce: today's engine was 3–4× faster than estimated, and the per-step copy of the run's values was the dominant cost.

  • Template fast path (template-fast.ts): a {{ }} unit that is a path into the scope, a literal, or a path with a ?? / || fallback is planned once from its parse and read straight from the scope. It answers exactly what the runner would: a JSON copy of the read. Any read it cannot be sure of goes to the runner, which keeps its own error text:

    • a missing base;
    • an inherited member or __proto__;
    • a value a JSON copy would change (a Date, an object without a prototype, an array hole, an own toJSON).

    Traced evaluations keep the runner, so probes and the run record are unchanged. A process-wide switch turns the fast path off.

  • Scope views (execute/scope.ts): a step's scope is a view of the run's values, not a copy, since nothing in the engine writes to one. The server suites hand out frozen copies instead, so any write to a scope throws in tests.

  • Runner pool (runners/node-vm.ts, runners/tenant.ts, backend/lib/code-runner.ts):

    • Up to AUTOMATION_RUNNER_PROCESSES runner processes per backend process. By default two for the api, and one per core but one, at most four, for a worker.
    • A process starts only when the others each hold two evaluations, and one beyond the first stops after five idle minutes.
    • A kill or a crash fails only what its own process held.
    • Waiting evaluations queue per organization (each stepper turn runs as its run's) and are served in turn.
    • The five places that installed the runner share one helper.

Numbers (Apple M2 Max, Node 22.22)

Case Before After
One forEach item's five template units, beside 0 / 1 / 5 MB of earlier output 2.2 / 4.7 / 18.0 ms 0.04 ms flat
One path into a 5 MB output 54 ms 0.014 ms
One runner-evaluated unit beside 5 MB (cloned) 0.58 ms
execute(), 40 transforms, chain / independent 140 / 134 ms 31 / 31 ms
… recorded as a durable run records it 176 / 172 ms 72 / 71 ms
16 CPU-bound transform bodies at once, 1 / 2 / 4 processes 309 ms 309 / 155 / 77 ms

Proof

  • A seeded property holds the fast path to the real runner over 2,000 generated units and scopes, including the in-memory values a JSON copy changes; there are 0 disagreements.
  • Pool tests cover processes running at once, growth only when full, idle retirement, a kill failing only its own process's work, and tenants served in turn. Each passed 5 of 5 runs.
  • Platform typecheck clean; server suite 19,518 tests with frozen scopes (two timing tests fail only under full parallel load and pass alone); automations UI 1,157; docs 688; lint:links, lint:manual, knip.
  • Integration lanes on fresh Postgres, 211/211 checks across 14 lanes: checkAutomations, checkAutomationRunLifecycle, checkAutomationTriggerDelivery, checkAutomationNodeRuns, checkAutomationAgentNode, checkAutomationStepDestroyPending, checkScheduledAgentStarts, checkInPlaceCompletionCycle, checkTaskAutomationOccupancy, checkWorkflowTurnReattach, checkMcpAuthoringParity, checkManagedAutomationConfiguration, checkTriggerPauseAfterFailures and checkEventScopeAndIsolation.

AUTOMATION_RUNNER_PROCESSES is documented in .env.example, the backend README and the environment reference (EN/DE/FR).

Follow-up: on the api, a request's evaluations still share one queue. Only the stepper names its tenant so far.

Benches for what a run costs today: one item's templates beside small and large earlier outputs, the code runner warm and under CPU load, forty transforms through execute() with and without the record, and a durable loop of connector reads with its progress writes. Cases the engine cannot run yet are listed as todo.
A unit that is a path into the scope, a literal, or a path with a ?? or || fallback is planned once from its parse and read straight from the scope, answering exactly what the runner would: a JSON copy of the read. Any read it cannot be sure of (a missing base, an inherited member, a value a JSON copy would change) goes to the runner, which keeps its error text. Traced evaluations keep the runner, so probes and the run record are unchanged. A process-wide switch turns the fast path off; a seeded property holds it to the runner over 2,000 units.
…a copy

A scope no longer copies the run's input and every earlier output on each step, item and condition; it is a view, since nothing in the engine writes to one: what crosses into the runner is serialized there, and what the fast path reads it copies. The server suites hand out frozen copies instead, so a write to a scope anywhere throws. One item's templates now cost the same beside 5 MB of earlier output as beside none, and forty transforms run four times faster.
A backend process now evaluates templates and transform bodies in up to AUTOMATION_RUNNER_PROCESSES runner processes (by default two for the api, and one per core but one, at most four, for a worker). Each is the supervised child one runner was. A process starts only when the others each hold two evaluations, one beyond the first retires after five idle minutes, and a kill fails only what its own process held. Waiting evaluations queue per organization, each stepper turn as its run's, and are served in turn. The five places that installed the runner now share one helper. Sixteen CPU-bound bodies take 77 ms on four processes against 309 ms on one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant