@typeonce/effect-machine 0.36.0 → 0.38.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/README.md +57 -0
  2. package/dist/Machine.d.ts +30 -0
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +25 -0
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/declaration.d.ts.map +1 -1
  7. package/dist/internal/machine/declaration.js +1 -0
  8. package/dist/internal/machine/declaration.js.map +1 -1
  9. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  10. package/dist/internal/machine/executionPlan.js +4 -2
  11. package/dist/internal/machine/executionPlan.js.map +1 -1
  12. package/dist/internal/machine/invocation.d.ts +1 -0
  13. package/dist/internal/machine/invocation.d.ts.map +1 -1
  14. package/dist/internal/machine/invocation.js +26 -3
  15. package/dist/internal/machine/invocation.js.map +1 -1
  16. package/dist/internal/machine/invocationDefinition.d.ts +3 -1
  17. package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
  18. package/dist/internal/machine/invocationDefinition.js.map +1 -1
  19. package/dist/internal/machine/machine.d.ts +6 -0
  20. package/dist/internal/machine/machine.d.ts.map +1 -1
  21. package/dist/internal/machine/machine.js +2 -0
  22. package/dist/internal/machine/machine.js.map +1 -1
  23. package/dist/internal/machine/observation.d.ts +3 -0
  24. package/dist/internal/machine/observation.d.ts.map +1 -0
  25. package/dist/internal/machine/observation.js +28 -0
  26. package/dist/internal/machine/observation.js.map +1 -0
  27. package/dist/internal/testing/machine/arbitrary.d.ts +4 -2
  28. package/dist/internal/testing/machine/arbitrary.d.ts.map +1 -1
  29. package/dist/internal/testing/machine/arbitrary.js +5 -4
  30. package/dist/internal/testing/machine/arbitrary.js.map +1 -1
  31. package/dist/internal/testing/machine/finiteModel.d.ts +2 -2
  32. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  33. package/dist/internal/testing/machine/finiteModel.js +39 -33
  34. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  35. package/dist/internal/testing/machine/referenceModel.d.ts.map +1 -1
  36. package/dist/internal/testing/machine/referenceModel.js +6 -5
  37. package/dist/internal/testing/machine/referenceModel.js.map +1 -1
  38. package/dist/internal/testing/machine/runtime.d.ts +8 -9
  39. package/dist/internal/testing/machine/runtime.d.ts.map +1 -1
  40. package/dist/internal/testing/machine/runtime.js +16 -14
  41. package/dist/internal/testing/machine/runtime.js.map +1 -1
  42. package/dist/internal/testing/machine/verification.js +7 -7
  43. package/dist/internal/testing/machine/verification.js.map +1 -1
  44. package/dist/testing/MachineTest.d.ts +6 -6
  45. package/dist/testing/MachineTest.d.ts.map +1 -1
  46. package/dist/testing/MachineTest.js +2 -2
  47. package/docs/agent-guide.md +20 -0
  48. package/package.json +4 -4
  49. package/src/Machine.ts +47 -0
  50. package/src/internal/machine/declaration.ts +1 -0
  51. package/src/internal/machine/executionPlan.ts +4 -2
  52. package/src/internal/machine/invocation.ts +34 -3
  53. package/src/internal/machine/invocationDefinition.ts +1 -0
  54. package/src/internal/machine/machine.ts +3 -0
  55. package/src/internal/machine/observation.ts +39 -0
  56. package/src/internal/testing/machine/arbitrary.ts +11 -4
  57. package/src/internal/testing/machine/finiteModel.ts +87 -59
  58. package/src/internal/testing/machine/referenceModel.ts +6 -5
  59. package/src/internal/testing/machine/runtime.ts +24 -22
  60. package/src/internal/testing/machine/verification.ts +13 -13
  61. package/src/testing/MachineTest.ts +6 -6
package/README.md CHANGED
@@ -397,6 +397,43 @@ address it from Effects; resolvers use `enqueue.sendTo` and `enqueue.stop`.
397
397
  Duplicate active IDs fail with `ChildAlreadyExistsError` and preserve the
398
398
  existing child. Parent-protocol compatibility is checked at each spawn.
399
399
 
400
+ ## External observation and tracing
401
+
402
+ Use `Machine.waitFor(ref, predicate)` when an external Effect needs a particular
403
+ current or subsequent published snapshot. Type predicates narrow the result.
404
+ It does not send an event or prove that a particular event caused the snapshot.
405
+ Prefer `ref.join` for final output, invocation `onDone` / `onFailure` for workflow
406
+ behavior, atom selectors for UI reads, and `ref.changes` for continuous observation.
407
+
408
+ ```ts
409
+ const ready = yield * Machine.waitFor(ref, isReady).pipe(Effect.timeout("5 seconds"))
410
+ ```
411
+
412
+ The predicate runs first, including for terminal snapshots. An unmatched failure
413
+ preserves its `Cause`; an unmatched stop fails with `StoppedError`; completion
414
+ without a match fails with `Cause.NoSuchElementError`. A predicate exception is
415
+ a defect. Observation is lazy, has no default timeout, and releases its
416
+ subscription on completion or interruption without stopping the machine. It does
417
+ not replay historical snapshots or expose intermediate macrostep configurations.
418
+
419
+ Effect, Stream, and timer invocations run inside an Effect span named
420
+ `Machine.invoke`. Application spans inside that work inherit the invocation span.
421
+ Configure the application's ordinary Effect tracer and exporter; no Machine-specific
422
+ exporter or inspection subscription is required. Standard Effect tracing and
423
+ sampling controls apply, including `Effect.withTracerEnabled(false)`.
424
+
425
+ Spans identify `machine.id`, `machine.sessionId`, `machine.state.path`,
426
+ `machine.invoke.id`, `machine.invoke.source`, `machine.invoke.kind`, and
427
+ `machine.invoke.sessionId`. Source is the registered name; invocation IDs can be
428
+ overridden independently. Payloads are not recorded automatically. Execution spans
429
+ end when the Effect, Stream consumption, or timer settles, including cancellation;
430
+ they do not change the lifetime of resources held in an enclosing Scope. Source
431
+ construction keeps its existing startup failure boundary. This does not add
432
+ machine-lifetime spans or propagate the trace context of each sender through the
433
+ mailbox. Tracing can add diagnostic Cause annotations without changing failure
434
+ values or interruption semantics. Even without an exporter, tracing has runtime
435
+ cost.
436
+
400
437
  ## Reactivity
401
438
 
402
439
  `AtomMachine` runs one lazy machine instance per `AtomRegistry`:
@@ -559,6 +596,26 @@ pass complete decoded objects when defining scenarios manually. Pure planner
559
596
  tests do not execute invokes or time. Use a started machine and a probe when
560
597
  those semantics matter.
561
598
 
599
+ Generated scenarios, finite models, and runtime commands use
600
+ `effect/unstable/arbitrary/Arbitrary`. Custom generator options accept native
601
+ `Arbitrary` values. For example:
602
+
603
+ ```ts
604
+ import * as Arbitrary from "effect/unstable/arbitrary/Arbitrary"
605
+
606
+ const generated = MachineTest.scenarios(Counter, { maxEvents: 20 })
607
+ const samples = yield* Arbitrary.sampleEffect(generated.arbitrary, {
608
+ count: 10,
609
+ seed: 42
610
+ })
611
+ ```
612
+
613
+ Use `Arbitrary.schema(schema)` to derive a generator and `Arbitrary.array(item,
614
+ { maxLength })` for custom sequences. `Arbitrary.checkEffect` checks properties
615
+ and returns a replay token for a failing case. With `@effect/vitest`, configure
616
+ property runs using `{ arbitrary: { runs: 100, seed: 42 } }`. FastCheck replay
617
+ paths and seeds do not preserve the same generated cases after migration.
618
+
562
619
  ## Entrypoints
563
620
 
564
621
  ```ts
package/dist/Machine.d.ts CHANGED
@@ -5687,6 +5687,36 @@ export declare const stopChild: {
5687
5687
  * @since 0.4.0
5688
5688
  */
5689
5689
  export declare const watch: <State, Event, Error = never, Output = never>(ref: MachineRef<State, Event, Error, Output>) => Stream.Stream<RuntimeOutcome<State, Error, Output>>;
5690
+ /**
5691
+ * Waits for the first current or subsequent published snapshot matching a predicate.
5692
+ *
5693
+ * Use for external Effect coordination and tests. Prefer atom selectors for UI
5694
+ * reads, invocation outcomes for workflow behavior, and `ref.join` for output.
5695
+ * This observes state; it does not acknowledge or correlate a sent event.
5696
+ *
5697
+ * The predicate runs before terminal classification, so explicitly matching a
5698
+ * done, error, or stopped snapshot succeeds. Otherwise an error preserves its
5699
+ * Cause, stopping fails with `StoppedError`, and completion without a match
5700
+ * fails with `Cause.NoSuchElementError`. Predicate exceptions are defects.
5701
+ *
5702
+ * Evaluation subscribes lazily. Interruption releases only the subscription,
5703
+ * never the machine. There is no default timeout; compose `Effect.timeout`.
5704
+ * Type predicates narrow the returned snapshot. Intermediate microsteps and
5705
+ * historical snapshots are not observed.
5706
+ *
5707
+ * @example
5708
+ * ```ts
5709
+ * const snapshot = yield* Machine.waitFor(ref, isReady).pipe(Effect.timeout("5 seconds"))
5710
+ * ```
5711
+ * @category combinators
5712
+ * @since 0.37.0
5713
+ */
5714
+ export declare const waitFor: {
5715
+ <State, Error, Output, Narrowed extends RuntimeSnapshot<State, Error, Output>>(predicate: (snapshot: RuntimeSnapshot<State, Error, Output>) => snapshot is Narrowed): <Event, Emitted>(ref: MachineRef<State, Event, Error, Output, Emitted>) => Effect.Effect<Narrowed, Error | StoppedError | Cause.NoSuchElementError>;
5716
+ <State, Error, Output>(predicate: (snapshot: RuntimeSnapshot<State, Error, Output>) => boolean): <Event, Emitted>(ref: MachineRef<State, Event, Error, Output, Emitted>) => Effect.Effect<RuntimeSnapshot<State, Error, Output>, Error | StoppedError | Cause.NoSuchElementError>;
5717
+ <State, Event, Error, Output, Emitted, Narrowed extends RuntimeSnapshot<State, Error, Output>>(ref: MachineRef<State, Event, Error, Output, Emitted>, predicate: (snapshot: RuntimeSnapshot<State, Error, Output>) => snapshot is Narrowed): Effect.Effect<Narrowed, Error | StoppedError | Cause.NoSuchElementError>;
5718
+ <State, Event, Error, Output, Emitted>(ref: MachineRef<State, Event, Error, Output, Emitted>, predicate: (snapshot: RuntimeSnapshot<State, Error, Output>) => boolean): Effect.Effect<RuntimeSnapshot<State, Error, Output>, Error | StoppedError | Cause.NoSuchElementError>;
5719
+ };
5690
5720
  /**
5691
5721
  * Prepares a fresh machine without initializing it.
5692
5722
  *