Skip to content

Expose Cluster machines over RPC and Web Workers via ClusterMachineProxy - #313

Merged
SandroMaglione merged 1 commit into
mainfrom
cluster-machine-rpc-worker
Nov 25, 2026
Merged

SandroMaglione merged 1 commit into
mainfrom
cluster-machine-rpc-worker

Conversation

@SandroMaglione

Copy link
Copy Markdown
Member

Summary

  • Add ClusterMachineProxy to @typeonce/effect-machine/cluster to expose ClusterMachine adapters over any effect/rpc transport — including Web Workers, SharedWorker, Node worker threads, WebSockets, and HTTP — following Effect Cluster's EntityProxy / EntityProxyServer pattern.
  • Provide toRpcGroup, layerHandlers, toLayer, and client so a single-node Cluster (SingleRunner + SqlMachineStorage over SQLite-WASM / PGlite / SQLite) can run inside a Web Worker while the main thread drives and observes machines (send, snapshot, changes, join) with only RpcClient.Protocol and Scope (zero Sharding or Storage required).
  • Share validated wire-snapshot encoding and joinChanges helpers in clusterRead.ts so DefinitionIdMismatch, VersionMismatch, InvalidCheckpoint, transformed/class state values, and final outputs behave identically across local and RPC clients.
  • Add end-to-end Web Worker MessageChannel / MessagePort tests (RpcServer.layerProtocolWorkerRunner ↔ RpcClient.layerProtocolWorker) with SQLite + SqlMachineStorage covering decoded class/date fields, local InvalidEvent rejection, MachineFailedError & ReadError propagation, multi-machine merged RpcGroup with transactional cross-machine sends, timers, and worker reload recovery, plus TSTyche type tests and Cluster documentation.

Changeset

  • Added or updated for a library or package-metadata change
  • Not required because this PR does not change a publishable package

Validation

  • pnpm check
  • Automated type-performance measurement passed or was not required (pnpm perf:types passed locally)
  • Automated runtime- and memory-performance measurement passed or was not required (no changes to planning or runtime hot paths)

@github-actions

Copy link
Copy Markdown
Contributor

Type performance

Measured with TypeScript 6.0.3 and skipLibCheck=true.

Scenario Base PR Difference
Effect only 55 55 0 (0.0%)
Import effect-machine 55 55 0 (0.0%)
Machine.state (3 children) 2,396 2,396 0 (0.0%)
Machine.make (3 states, 2 events) 8,573 8,573 0 (0.0%)
machine.handle (3 states, 2 transitions) 45,085 45,085 0 (0.0%)
declared transition (10 named branches) 71,452 71,452 0 (0.0%)
atomic construction, guards, and reentry 83,762 83,762 0 (0.0%)
registered invocation (input-taking Effect) 41,264 41,264 0 (0.0%)
registered invocations (Stream, Logic, child) 89,562 89,562 0 (0.0%)
machine.handle (depth 24) 191,846 191,846 0 (0.0%)
machine.handle (wide depth 16) 215,905 215,905 0 (0.0%)
machine.handle (parallel/history/choice) 106,289 106,289 0 (0.0%)
machine definition (3 independent implementations) 97,653 97,653 0 (0.0%)
machine exact input/output/error/services 86,092 86,092 0 (0.0%)
execution adapter readiness 75,016 75,016 0 (0.0%)

Marginal instantiations are measured against the matching setup without that API call:

Scenario Base PR Difference
Import effect-machine 0 0 0
Machine.state (3 children) 2,341 2,341 0 (0.0%)
Machine.make (3 states, 2 events) 6,169 6,169 0 (0.0%)
machine.handle (3 states, 2 transitions) 36,512 36,512 0 (0.0%)
declared transition (10 named branches) 63,258 63,258 0 (0.0%)
atomic construction, guards, and reentry 68,807 68,807 0 (0.0%)
registered invocation (input-taking Effect) 34,454 34,454 0 (0.0%)
registered invocations (Stream, Logic, child) 53,504 53,504 0 (0.0%)
machine.handle (depth 24) 176,165 176,165 0 (0.0%)
machine.handle (wide depth 16) 201,170 201,170 0 (0.0%)
machine.handle (parallel/history/choice) 94,523 94,523 0 (0.0%)
machine definition (3 independent implementations) 87,449 87,449 0 (0.0%)
machine exact input/output/error/services 74,268 74,268 0 (0.0%)
execution adapter readiness 26,876 26,876 0 (0.0%)
Check times (informational)
Scenario Base PR
Effect only 0.02s 0.02s
Import effect-machine 0.02s 0.02s
Machine.state (3 children) 0.06s 0.06s
Machine.make (3 states, 2 events) 0.10s 0.11s
machine.handle (3 states, 2 transitions) 0.22s 0.22s
declared transition (10 named branches) 0.30s 0.30s
atomic construction, guards, and reentry 0.35s 0.35s
registered invocation (input-taking Effect) 0.21s 0.22s
registered invocations (Stream, Logic, child) 0.39s 0.40s
machine.handle (depth 24) 0.41s 0.40s
machine.handle (wide depth 16) 0.45s 0.45s
machine.handle (parallel/history/choice) 0.40s 0.40s
machine definition (3 independent implementations) 0.37s 0.37s
machine exact input/output/error/services 0.35s 0.35s
execution adapter readiness 0.31s 0.30s

Type instantiations are the comparison metric. Check time varies with runner load and is informational only.

@github-actions

Copy link
Copy Markdown
Contributor

Runtime performance

Median of 5 independent benchmark processes on AMD EPYC 7763 64-Core Processor with Node v24.22.3.

Pull request baseline

Scenario Effect Machine
Plan counter transitions 115,285 transitions/s
Drain burst with terminal fence 403,419 increments/s
Drain burst with a change observer 375,218 increments/s
Lookup and send to one child 292,285 increments/s
Start and stop a machine 115,247 machines/s
Start and stop a parent with one child 23,343 families/s
Complete an Effect invocation with tracing enabled 6,170 invocations/s
Complete an Effect invocation with tracing disabled 7,279 invocations/s
Plan transitions through a compound state 101,939 transitions/s
Plan transitions through parallel regions 79,695 transitions/s
Drain burst through a compound state 341,580 events/s
Drain burst through two parallel regions 318,036 events/s
Drain a compound-state burst with a change observer 314,949 events/s

Process runtime reference points

Scenario Effect Machine
Start and stop a raw generic process 28,477 processes/s
Start and stop a raw compiled process 66,498 processes/s
Memory profile Effect Machine
Idle machine 1.9 KiB
Raw generic managed process 11.3 KiB
Raw compiled process 3.2 KiB
Two independent idle machines 3.5 KiB
Idle parent with one child 6.1 KiB
Parent with observed child registry 9.7 KiB
Parent with observed invoked child snapshots 7.1 KiB

Effect Machine change from base

Metric Base Base variability PR PR variability Difference
Plan counter transitions 115,489 transitions/s 2.0% MAD 115,285 transitions/s 0.7% MAD -0.2%
Drain burst with terminal fence 407,471 increments/s 0.7% MAD 403,419 increments/s 0.4% MAD -1.0%
Drain burst with a change observer 375,099 increments/s 0.9% MAD 375,218 increments/s 1.2% MAD +0.0%
Lookup and send to one child 291,568 increments/s 1.4% MAD 292,285 increments/s 1.1% MAD +0.2%
Start and stop a machine 115,128 machines/s 1.0% MAD 115,247 machines/s 0.8% MAD +0.1%
Start and stop a parent with one child 23,748 families/s 0.9% MAD 23,343 families/s 1.5% MAD -1.7%
Complete an Effect invocation with tracing enabled 6,111 invocations/s 1.0% MAD 6,170 invocations/s 0.9% MAD +1.0%
Complete an Effect invocation with tracing disabled 7,189 invocations/s 0.4% MAD 7,279 invocations/s 0.7% MAD +1.3%
Plan transitions through a compound state 100,058 transitions/s 1.2% MAD 101,939 transitions/s 0.7% MAD +1.9%
Plan transitions through parallel regions 77,761 transitions/s 0.5% MAD 79,695 transitions/s 1.7% MAD +2.5%
Drain burst through a compound state 341,936 events/s 0.9% MAD 341,580 events/s 0.6% MAD -0.1%
Drain burst through two parallel regions 318,555 events/s 1.1% MAD 318,036 events/s 0.4% MAD -0.2%
Drain a compound-state burst with a change observer 319,400 events/s 1.0% MAD 314,949 events/s 1.2% MAD -1.4%
Idle machine heap per unit 1.9 KiB 1.5% MAD 1.9 KiB 0.3% MAD +0.5%
Raw generic managed process heap per unit 11.3 KiB 0.0% MAD 11.3 KiB 0.0% MAD +0.0%
Raw compiled process heap per unit 3.2 KiB 0.1% MAD 3.2 KiB 0.1% MAD +1.1%
Two independent idle machines heap per unit 3.6 KiB 0.7% MAD 3.5 KiB 0.0% MAD -0.7%
Idle parent with one child heap per unit 6.1 KiB 0.0% MAD 6.1 KiB 0.0% MAD -0.0%
Parent with observed child registry heap per unit 9.7 KiB 0.0% MAD 9.7 KiB 0.0% MAD -0.0%
Parent with observed invoked child snapshots heap per unit 7.1 KiB 0.2% MAD 7.1 KiB 0.0% MAD +0.2%

Process runtime reference change from base

Metric Base Base variability PR PR variability Difference
Start and stop a raw generic process 28,180 processes/s 2.4% MAD 28,477 processes/s 3.0% MAD +1.1%
Start and stop a raw compiled process 67,531 processes/s 1.5% MAD 66,498 processes/s 0.4% MAD -1.5%

Regression guard

No large, noise-adjusted throughput or heap regressions detected.

Versions and interpretation
  • Effect Machine: 0.47.1

Higher throughput is better; lower heap is better. Variability is the median absolute deviation across independent processes, relative to their median. Small differences on shared GitHub-hosted hardware remain informational; the required guard rejects only large changes beyond the measured noise allowance.

@SandroMaglione
SandroMaglione merged commit 5867725 into main Nov 25, 2026
9 checks passed
@SandroMaglione
SandroMaglione deleted the cluster-machine-rpc-worker branch November 25, 2026 02:00
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