Repository navigation
Releases
Release 4.3.0
Remediation of the 2026-09 principal architecture review (#74, #76). Seven reproduced defects are fixed, one new error class is added, and nothing requires a major version. Coverage of src/ remains 100%.
Fixed
Work no longer continues under a lock the machine may have lost. When releasing a held lock failed, the machine rejected that operation but kept draining its queue. Later operations saw the mutex still flagged as acquired, skipped acquisition, and ran. If the unlock had in fact succeeded remotely with only its reply lost, another worker could hold the lock at the same time. Now every queued, chained, or later operation rejects with the new LockOwnershipUncertainError until a manual releaseLock() succeeds. If it cannot, discard the machine and rebuild it from persisted state.
Behavior change. Code that kept using a machine after a failed release now gets
LockOwnershipUncertainErrorrejections where it previously saw silent success. The operation whose release failed still rejects with the release error, as before. A defensivereleaseLock()of a lock the machine does not hold is still reported toonReleaseError, but never blocks the machine.
A synchronous adapter throw no longer disables LockAdapterMutex. If an adapter's acquireLock() threw synchronously, the rejected attempt was cached forever, and every later acquire failed without reaching the adapter.
Composite conditions no longer hide re-entrant calls. AndComposite, OrComposite, and Not awaited every child, even a plain boolean, which ended the machine's re-entrancy check early. A later child that called back into the same machine deadlocked instead of throwing ReentrancyError.
The built graph can no longer be rewritten through getTransitions(). It returned the state's internal Set, so any caller could add or remove edges for every machine sharing the process.
Distinct transitions no longer collide. Transition identity joined names with NUL, and names may contain NUL. A colliding edge was silently dropped, or reported as a false duplicate.
Async diagnostic hooks can no longer crash Node. A rejected promise from an async onReleaseError or onChainedOperationError escaped as an unhandled rejection, which terminates Node under its default settings.
Performance
Large queues drain in linear time. Array.shift() made draining quadratic. 100k queued operations took about 1.3s to drain, and now take about 5ms (#63).
Added
LockOwnershipUncertainErrorwith codelockOwnershipUncertain. Itscauseis the release failure.FinitaErrorforwardsErrorOptions, so subclasses can setcause.
Compatibility
- Composite return types widen.
AndComposite,OrComposite, andNotnow declareMaybePromise<boolean>, matchingConditionInterface. Code that calls.then()directly on their result should useawaitinstead. getTransitions()returns an array snapshot. Its declared type is stillIterable<TransitionInterface>.
Documentation
- Locks and persisted state. A lock serializes execution but does not refresh a machine's in-memory state, so two machines built from one snapshot can both commit the same transition.
docs/mutex.mdnow describes the supported ownership models and a load-inside-the-lock pattern. - The operation flow in
docs/core.mdmatches the engine. The lock is taken before the event is validated, and the flow shows event observers, guard selection, and where failures stop an operation. - The
StatemachineOptionstable lists every option.
Also
Development dependencies and CI actions are updated (#78), which also cleared the OSV-Scanner failure blocking CI. Deferred follow-ups are tracked in #72 (subject types through the graph, v5), #73 (bounding observer-chained operations), and #77 (Vitest 4).
v4.2.0
Remediation of the 2026-08 architecture review (#52). Two reproduced bugs fixed, four API-consistency gaps closed, no breaking changes. Coverage of src/ remains 100% across 389 tests.
Fixed
A failed lock release is no longer silent. MutexInterface.releaseLock() signals failure two ways — it throws, or it returns false. Only the throw was handled. A false return (what a PostgreSQL advisory unlock or a Redis DEL that removed nothing produces) meant the operation resolved successfully, onReleaseError never fired, and isLockAcquired() stayed true forever — after which every subsequent operation piggybacked on the stuck lock and never released it. Both failure modes now raise the new LockCanNotBeReleasedError.
Behavior change to check if you use a custom mutex. An operation whose release fails now rejects where it previously resolved. Implementations conforming to
MaybePromise<boolean>are unaffected, and the defaultNullMutexalways returnstrue— but a mutex returningundefined(untyped JavaScript, or a loose test double) will now surface an error instead of passing silently.
whenIdle() no longer deadlocks. It was missing the re-entrancy guard triggerEvent/checkTransitions received in v4. Awaiting it inside an observer could never resolve — the machine cannot reach idle while the runner is blocked on that very callback — so the machine wedged permanently and silently. It now throws ReentrancyError.
LockAdapterMutex no longer double-acquires. The acquired flag is only set after the adapter resolves, so two overlapping acquireLock() calls both passed the guard and acquired twice on a non-idempotent adapter. Overlapping calls now share one in-flight acquire; a failed acquire is still retryable.
Observer accessors return snapshots. getBeforeObservers(), getAfterObservers(), and Event.getObservers() handed out their live collections, so a later detach mutated a list a caller already held.
Added
Factoryaccepts engine options — a third constructor argument typedFactoryStatemachineOptions, forwardingmaxQueueLength,maxAutomaticHops,autoreleaseLock,onChainedOperationError, andonReleaseErrorto every machine it creates. Previously factory-created machines silently ran on defaults, with no back-pressure and no diagnostic sinks — precisely the fleet-of-machines case those options exist for. The three fields the factory derives per subject (initialStateName,mutex,transitionSelector) are excluded at the type level.LockCanNotBeReleasedError— typed error for the failed-release path, with codelockCanNotBeReleased.AmbiguousTransitionError.candidates— the competing transitions (target, event, condition, weight), also rendered into the message. A bare count never identified the culprits.Statemachine.releaseLock()now reports failures throughonReleaseError. ItsPromise<void>signature is unchanged; returning the boolean is breaking and is queued for v5 (#55).
Documentation
Release-failure semantics, the factory options template, whenIdle re-entrancy, and two things easy to misread from the API alone: Timeout schedules nothing — it only fires when something drives the machine — and event observers are shared by every machine built from the same process.
Also
Deprecation notices previously marked "removed in v4" are re-dated to v5, and the Codecov project floor moved from 80% to 95% (#65).
Outstanding work from the review is tracked in #64, with the breaking items batched under the v5 milestone.
Full changelog: v4.1.0...v4.2.0
Release 4.1.0
- Merge pull request #38 from camcima/fix/architecture-review-remediation (1b39bbc)
- test: pin chained-enqueue queue-limit rejection; clarify docs (9afdf94)
- test: cover checkTransitions branch of QueueLimitExceededError (3a92827)
- feat: add onReleaseError diagnostic hook for lock release failures (f6058bc)
- feat: add maxQueueLength back-pressure option (83a425b)
- feat: add onChainedOperationError sink and document completion boundary (0d94ddf)
- feat: add Statemachine.whenIdle() to await full queue drain (14d601b)
- fix: make Statemachine observer attach idempotent (aeba9db)
- fix: keep ProcessBuilder reusable after a failed build (457da0c)
- fix: restrict graph rankdir/direction options to valid values (6607d81)
- fix: freeze State and Transition to match documented immutability (d466c7d)
- fix: validate transition weights and WeightTransition epsilon (98b07b2)
- docs: fix unsafe Redis lock example, document mutex ownership contract (08cd042)
- Merge pull request #31 from camcima/dependabot/github_actions/actions/checkout-7 (01ee42f)
- build(deps): bump actions/checkout from 6 to 7 (b7b45e0)
- Merge pull request #27 from camcima/dependabot/github_actions/codecov/codecov-action-7 (ecbd461)
- build(deps): bump codecov/codecov-action from 6 to 7 (3588652)
- Merge pull request #34 from camcima/ci/codecov-test-results-action (d076565)
- ci(codecov): upload test results via codecov-action (drop deprecated action) (f17bbc0)
- Merge pull request #33 from camcima/ci/codecov-tokenless-oidc (cdbb369)
- ci(codecov): switch to tokenless OIDC upload (bc56248)
Release 4.0.0
- Merge pull request #29 from camcima/fix/code-review-v3.0.1 (b53efe3)
- ci(osv-scanner): filter dev-only undici/esbuild/js-yaml advisories (e7f48e0)
- fix(statemachine): guard every event observer and transition condition against re-entrancy (cb80489)
- build(deps-dev): bump vitest to 3.2.6 to fix GHSA-5xrq-8626-4rwp (8911c05)
- test: close remaining coverage gaps to 100% across all metrics (b71d0fe)
- docs: document ReentrancyError; correct and complete migration notes (c9d0cf7)
- fix(statemachine): surface lock-release failures; guard selector; restore zero-arg callback (129280b)
- fix(statemachine): reject empty initialStateName instead of silently restarting (36272aa)
- docs: update observer/event/builder docs and migration notes for review fixes (1064703)
- refactor(condition): extract CompositeCondition base for And/OrComposite (8ff3365)
- refactor: share isNamed/nameOrString via src/util (ba9178b)
- fix(condition): timeout throws InvalidSubjectError; allocation-free time check (4ab8a8e)
- fix(graph): escape backslashes in DOT/mermaid output; guard event lookup in labels (2f3fd72)
- test(event): replace stale getInvokeArgs assertions with new args-delivery contract (b42f843)
- fix(event): pass invoke args to observers; drop racy invokeArgs field; inline Dispatcher (8bb6581)
- fix(statemachine): validate maxAutomaticHops is a positive integer (e9a4001)
- fix(statemachine): bound automatic loops with maxAutomaticHops instead of first-revisit detection (16cc1b5)
- fix(statemachine): reject (not throw) on re-entrancy; broaden re-entrancy tests (5ad2514)
- fix(statemachine): throw ReentrancyError on re-entrant trigger instead of deadlocking (3655026)
- docs(observer): clarify onEnter ifStateName contract and test intent (847b363)
- test(observer): add pre-fix-failing regression for stale onEnter lock acquisition (0fb4b83)
- fix(observer): onEnter fires only for rest states via enqueue ifStateName guard (726024a)
- test(observer): prove StatefulStatusChanger isolation with two machines; pin precedence (0332853)
- fix(observer): statefulStatusChanger writes to frame.subject; correct factory docs (d72ce2b)
- test(observer): assert frame.subject referential identity; document logger omission (1a65fc2)
- feat(observer): expose subject on TransitionFrame; share one frame per transition (44ae06c)
- fix(statemachine): snapshot observer lists before notifying (eed53a7)
- fix(statemachine): settle caller promise only after mutex release completes (7501123)
- fix(statemachine): kick runner from EnqueueContext.enqueue; single enqueue path (7e84451)
- refactor(builder): capture condition name once; assert padded-condition error code (67b07ff)
- fix(builder): validate state names; unify name rule across state/event/condition (ac7b38d)
- test(builder): assert weight-conflict payload and message (9f6cfb5)
- fix(builder): detect conflicting weights on duplicate transitions, unify identity key (fbd571d)
- fix(selector): make WeightTransition epsilon ties order-independent (44f30d2)
- docs: add code review report for v3.0.1 (39d4459)
Release 3.0.1
v3.0.0 — execution engine rewrite, typed errors, hardened builder
First v3 release. Major rewrite landed across 55 commits in PR #17 plus follow-up fixes.
Highlights
Graph + execution rewrite
- New
ProcessBuilderwith two-phase build: everyTransitiontargets a realStateinstance, even under cycles and self-loops. Statemachineexecution engine rewritten to pump operations through a FIFO queue, separating the active-transition filter from the transition selector and properly modeling lifecycle phases (proposed → before-observers → commit → after-observers → optional auto-chain).- New observer surfaces:
BeforeTransitionObserverandAfterTransitionObserverwithTransitionFrame/ProposedTransitionFrameimmutable payloads.
Typed error hierarchy
- New
FinitaErrorbase class withcodediscriminant. - New typed errors:
ProcessFinalizedError,GraphValidationError,DuplicateTransitionError,StateNotFoundError,StateEventNotFoundError,ProcessNotFoundError,InvalidSubjectError,AmbiguousTransitionError,AutomaticTransitionCycleError. - Stricter input validation: whitespace-padded event names rejected; empty/whitespace condition names rejected.
GraphBuilder.addStatemade idempotent (closes #14).
Packaging and CI
prepublishOnlyhook +engines.noderange tightened (Node ≥20).- CI matrix: Node 20, 22, 24.
- New CI job:
npm pack --dry-runverification gate. - Codecov configured with project floor at 80%, no per-diff patch threshold.
Review-driven bug fixes folded into 3.0.0
runOperationhonors an already-held mutex viaisAcquired()short-circuit. Manual lock management withautoreleaseLock: falsenow composes correctly with non-idempotent mutexes (`pg_try_advisory_lock`, redis `SET NX`).- Builder treats different condition object instances as conflicts regardless of name. Same reference dedups; different references throw
DuplicateTransitionError. - Event observers fire on event resolution, not state change. Self-transitions and false-condition matched events now fire
event.attach()-style observers, matching the documented Observer Lifecycle.
Breaking changes from v2
- API: state machines must be built via
ProcessBuilderand frozen before use. DirectaddState/addTransitionon graphs is gone. - Observers: before/after observers now operate on immutable
TransitionFrame/ProposedTransitionFrame. Event-attached observers fire whenever the event is resolved on the current state. - Conditions: dedup is by object identity, not by name. Two
CallbackCondition("foo", ...)instances with different bodies now throwDuplicateTransitionErrorinstead of silently merging. - Errors: all framework errors now extend
FinitaErrorand carry acodediscriminant. - Validation: whitespace-padded event names and empty/whitespace condition names now throw
GraphValidationErrorat build time.
See `docs/migration/v2-to-v3.md` for the full migration guide.
Quality gates
- 278 tests across 19 files
- Coverage: 98.62% statements, 99.55% functions, 95.57% branches, 98.62% lines
- `pnpm lint`, `pnpm build`, `pnpm format:check`, `npm pack --dry-run` — all clean
- 76.6 kB tarball, 9 files
🤖 Generated with Claude Code