phronomy 0.17.0 → 0.19.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 (107) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +122 -1134
  3. data/README.md +70 -1280
  4. data/benchmark/bench_regression.rb +25 -5
  5. data/docs/changelog/0.14-and-earlier.md +1137 -0
  6. data/docs/decisions/008-orchestrator-uses-os-threads.md +46 -48
  7. data/docs/decisions/009-state-store-abstraction.md +1 -1
  8. data/docs/decisions/010-cooperative-first-concurrency.md +155 -235
  9. data/docs/decisions/014-unified-persistence-durable-state.md +268 -0
  10. data/docs/features.md +93 -0
  11. data/docs/getting-started.md +387 -0
  12. data/docs/migrations/0.15.md +35 -0
  13. data/docs/migrations/0.16.md +43 -0
  14. data/docs/migrations/0.19.md +148 -0
  15. data/docs/runtime-and-concurrency.md +349 -0
  16. data/examples/workflows/generic_task_event_mapping.rb +14 -6
  17. data/lib/phronomy/agent/agent_invocation_session_builder.rb +2 -2
  18. data/lib/phronomy/agent/async_event_api.rb +3 -3
  19. data/lib/phronomy/agent/base.rb +116 -55
  20. data/lib/phronomy/agent/context/capability/base.rb +13 -3
  21. data/lib/phronomy/agent/context_assembler.rb +13 -3
  22. data/lib/phronomy/agent/execution_coordinator.rb +423 -252
  23. data/lib/phronomy/agent/journal_projection.rb +5 -1
  24. data/lib/phronomy/agent/shared_state.rb +2 -0
  25. data/lib/phronomy/agent/tool_executor.rb +29 -71
  26. data/lib/phronomy/agent/tool_invocation.rb +97 -47
  27. data/lib/phronomy/agent/tool_invocation_session_builder.rb +55 -161
  28. data/lib/phronomy/configuration.rb +7 -30
  29. data/lib/phronomy/diagnostics.rb +12 -41
  30. data/lib/phronomy/engine/concurrency/async_queue.rb +5 -188
  31. data/lib/phronomy/engine/concurrency/cancellation_scope.rb +6 -7
  32. data/lib/phronomy/engine/concurrency/cancellation_token.rb +48 -3
  33. data/lib/phronomy/engine/concurrency/deadline.rb +2 -3
  34. data/lib/phronomy/engine/concurrency/offload_pool.rb +696 -0
  35. data/lib/phronomy/engine/concurrency/pool_registry.rb +5 -5
  36. data/lib/phronomy/engine/event_loop.rb +170 -193
  37. data/lib/phronomy/engine/fsm_session.rb +6 -4
  38. data/lib/phronomy/engine/runtime/timer_queue.rb +48 -71
  39. data/lib/phronomy/engine/runtime/timer_service.rb +13 -21
  40. data/lib/phronomy/engine/runtime.rb +52 -158
  41. data/lib/phronomy/engine/task.rb +136 -277
  42. data/lib/phronomy/llm_adapter/base.rb +14 -14
  43. data/lib/phronomy/llm_adapter/ruby_llm.rb +3 -4
  44. data/lib/phronomy/llm_adapter.rb +2 -2
  45. data/lib/phronomy/metrics.rb +15 -30
  46. data/lib/phronomy/multi_agent/fan_out_invocation.rb +146 -0
  47. data/lib/phronomy/multi_agent/fan_out_session_builder.rb +125 -0
  48. data/lib/phronomy/multi_agent/handoff.rb +1 -0
  49. data/lib/phronomy/multi_agent/orchestrator.rb +147 -99
  50. data/lib/phronomy/multi_agent/team_coordinator.rb +2 -0
  51. data/lib/phronomy/persistence/in_memory.rb +113 -8
  52. data/lib/phronomy/persistence.rb +12 -3
  53. data/lib/phronomy/testing/eval/comparison.rb +23 -0
  54. data/lib/phronomy/testing/eval/dataset.rb +27 -0
  55. data/lib/phronomy/testing/eval/eval_case.rb +13 -0
  56. data/lib/phronomy/testing/eval/eval_result.rb +16 -0
  57. data/lib/phronomy/testing/eval/metrics.rb +43 -0
  58. data/lib/phronomy/testing/eval/runner.rb +52 -0
  59. data/lib/phronomy/testing/eval/scorer/base.rb +15 -0
  60. data/lib/phronomy/testing/eval/scorer/exact_match.rb +25 -0
  61. data/lib/phronomy/testing/eval/scorer/includes_scorer.rb +25 -0
  62. data/lib/phronomy/testing/eval/scorer/llm_judge.rb +46 -0
  63. data/lib/phronomy/testing/eval/scorer.rb +10 -0
  64. data/lib/phronomy/testing/eval.rb +9 -0
  65. data/lib/phronomy/testing/fake_clock.rb +6 -53
  66. data/lib/phronomy/testing.rb +2 -6
  67. data/lib/phronomy/tools/agent.rb +141 -6
  68. data/lib/phronomy/vector_store/async_backend.rb +21 -17
  69. data/lib/phronomy/vector_store/base.rb +2 -2
  70. data/lib/phronomy/vector_store/embeddings/base.rb +6 -5
  71. data/lib/phronomy/version.rb +1 -1
  72. data/lib/phronomy/workflow.rb +10 -9
  73. data/lib/phronomy/workflow_runner.rb +363 -99
  74. data/lib/phronomy.rb +10 -4
  75. data/scripts/api_snapshot.rb +4 -4
  76. metadata +25 -33
  77. data/lib/phronomy/engine/concurrency/blocking_adapter_pool.rb +0 -561
  78. data/lib/phronomy/engine/runtime/deterministic_scheduler.rb +0 -439
  79. data/lib/phronomy/engine/runtime/fake_scheduler.rb +0 -165
  80. data/lib/phronomy/engine/runtime/runtime_metrics.rb +0 -116
  81. data/lib/phronomy/engine/runtime/scheduler.rb +0 -98
  82. data/lib/phronomy/engine/runtime/scheduler_timer_adapter.rb +0 -79
  83. data/lib/phronomy/engine/runtime/task_registry.rb +0 -95
  84. data/lib/phronomy/engine/runtime/thread_scheduler.rb +0 -30
  85. data/lib/phronomy/engine/task/backend.rb +0 -80
  86. data/lib/phronomy/engine/task/deferred_backend.rb +0 -73
  87. data/lib/phronomy/engine/task/fiber_backend.rb +0 -157
  88. data/lib/phronomy/engine/task/immediate_backend.rb +0 -89
  89. data/lib/phronomy/engine/task/mapped_backend.rb +0 -90
  90. data/lib/phronomy/engine/task/thread_backend.rb +0 -84
  91. data/lib/phronomy/engine/task_group.rb +0 -193
  92. data/lib/phronomy/eval/comparison.rb +0 -47
  93. data/lib/phronomy/eval/dataset.rb +0 -45
  94. data/lib/phronomy/eval/eval_case.rb +0 -17
  95. data/lib/phronomy/eval/eval_result.rb +0 -29
  96. data/lib/phronomy/eval/metrics.rb +0 -66
  97. data/lib/phronomy/eval/runner.rb +0 -94
  98. data/lib/phronomy/eval/scorer/base.rb +0 -22
  99. data/lib/phronomy/eval/scorer/exact_match.rb +0 -31
  100. data/lib/phronomy/eval/scorer/includes_scorer.rb +0 -32
  101. data/lib/phronomy/eval/scorer/llm_judge.rb +0 -72
  102. data/lib/phronomy/eval/scorer.rb +0 -9
  103. data/lib/phronomy/eval.rb +0 -7
  104. data/lib/phronomy/state_store/base.rb +0 -48
  105. data/lib/phronomy/state_store/in_memory.rb +0 -62
  106. data/lib/phronomy/testing/fake_scheduler.rb +0 -104
  107. data/lib/phronomy/testing/scheduler_helpers.rb +0 -68
@@ -2,51 +2,49 @@
2
2
 
3
3
  ## Status
4
4
 
5
- Accepted
6
-
7
- ## Context
8
-
9
- `Agent::Orchestrator#dispatch_parallel` runs multiple sub-agent invocations
10
- concurrently. The Ruby concurrency primitives available are:
11
-
12
- 1. **OS threads** (`Thread`): true OS-level threads, subject to Ruby's GVL for
13
- CPU-bound work, but I/O-bound work (LLM API calls, tool HTTP requests)
14
- releases the GVL and runs in parallel.
15
- 2. **Ractors**: actor-model isolation, no shared mutable state between Ractors.
16
- True parallel for CPU-bound work but requires strict object isolation.
17
- 3. **Fibers / async**: cooperative concurrency via Fiber scheduler (e.g.,
18
- `async` gem). Non-blocking I/O without multiple threads.
19
- 4. **`concurrent-ruby` thread pool**: managed pool of OS threads.
20
-
21
- LLM calls and tool invocations are overwhelmingly I/O-bound (HTTP requests).
22
- Under the GVL, OS threads are sufficient to achieve meaningful parallelism for
23
- these workloads. Ractors require that all objects passed between them are
24
- shareable, which is incompatible with RubyLLM's mutable chat objects and
25
- `WorkflowContext` instances without significant refactoring.
26
-
27
- Fibers require an async-compatible HTTP library stack throughout (RubyLLM,
28
- Faraday, etc.), which is not guaranteed today.
29
-
30
- ## Decision
31
-
32
- `dispatch_parallel` spawns one OS thread per task using Ruby's `Thread.new`.
33
- A `max_concurrency:` cap (default: unlimited) uses a `Mutex`-guarded counter to
34
- limit the number of simultaneously active threads when specified.
35
-
36
- ## Consequences
37
-
38
- **Positive:**
39
- - Transparent parallelism for I/O-bound LLM/tool calls with no dependency
40
- changes.
41
- - Compatible with all Ruby versions in the support matrix (3.2, 3.3, 3.4, head).
42
- - Simple to reason about: each task is an independent thread; results are
43
- collected in input order.
44
-
45
- **Negative / Tradeoffs:**
46
- - CPU-bound work inside agents does not benefit from true parallelism due to
47
- the GVL. (In practice, agents are almost always I/O-bound.)
48
- - Spawning many threads simultaneously (no `max_concurrency:`) can exhaust
49
- system thread limits under high load. Users should set `max_concurrency:` for
50
- large fan-outs.
51
- - Ractor-based isolation (if ever needed for security sandboxing) would require
52
- significant API changes to `WorkflowContext` and RubyLLM integration.
5
+ **Superseded by ADR-010 on 2026-08-13.**
6
+
7
+ This file is retained as decision history. Its original implementation — one OS
8
+ Thread per `dispatch_parallel` child — is no longer the active architecture.
9
+
10
+ ## Historical context
11
+
12
+ The original Orchestrator implementation needed concurrent subagent execution
13
+ before Phronomy had a common EventLoop/FSMSession control model. It therefore
14
+ used `Thread.new` per child Agent, optionally bounded by `max_concurrency`.
15
+
16
+ That choice provided straightforward parallelism for I/O-heavy Agent calls but
17
+ also tied logical child concurrency directly to OS-thread count.
18
+
19
+ ## Superseding decision
20
+
21
+ Phronomy now models fan-out as a parent `FanOutInvocation` / FSMSession:
22
+
23
+ ```text
24
+ FanOut FSMSession
25
+ |
26
+ +-- start child Agent A asynchronously
27
+ +-- start child Agent B asynchronously
28
+ +-- start child Agent C asynchronously
29
+ |
30
+ +-- child completion events
31
+ |
32
+ +-- aggregate / fail / timeout / cancel
33
+ ```
34
+
35
+ `max_concurrency` limits how many child Agent invocations are active. It does
36
+ not create a corresponding set of Threads.
37
+
38
+ Each child Agent runs through the common Agent FSMSession/EventLoop lifecycle.
39
+ When a child performs synchronous work that must not run on EventLoop, only that
40
+ synchronous operation is executed on the bounded `OffloadPool`.
41
+
42
+ Therefore:
43
+
44
+ - `dispatch_parallel` does not create raw Threads;
45
+ - no `Runtime#spawn` or Task execution backend is involved;
46
+ - child completion is delivered by EventLoop events;
47
+ - result ordering, fail/skip policy, timeout, and cancellation belong to the
48
+ FanOut state machine.
49
+
50
+ See ADR-010 for the current concurrency model and Thread boundary.
@@ -1,6 +1,6 @@
1
1
  # ADR 009: StateStore Abstraction for Workflow Persistence
2
2
 
3
- **Status**: Accepted
3
+ **Status**: Superseded by ADR-014
4
4
  **Date**: 2025-01
5
5
  **Issue**: [#250](https://github.com/Raizo-TCS/phronomy/issues/250)
6
6
 
@@ -1,255 +1,175 @@
1
- # ADR-010: Cooperative-First Concurrency BlockingAdapterPool for Uncontrollable I/O
1
+ # ADR-010: EventLoop / FSMSession First Concurrency
2
2
 
3
3
  ## Status
4
4
 
5
- Accepted — updated 2026-05-25 to document current scheduler landscape and
6
- production-cooperative roadmap (Issues #331, #332, #334).
7
-
8
- > **Historical implementation note (2026-08-09):** The implementation-detail
9
- > inventory below is intentionally preserved as decision history. Compatibility
10
- > APIs subsequently removed from the active contract — including the
11
- > `runtime_backend :cooperative` alias and direct Runtime singleton replacement —
12
- > must not be read as current API guidance. The cooperative-first layering
13
- > principle remains the decision; use README/current source for current symbols.
5
+ Accepted — revised for the OffloadPool execution boundary.
14
6
 
15
7
  ## Context
16
8
 
17
- Phronomy provides its own concurrency primitives:
18
-
19
- - **`Phronomy::Runtime`** task scheduler; backend is configurable.
20
- The current production default is `:thread` (`ThreadScheduler` / `ThreadBackend`).
21
- See the backend landscape table below for all supported values.
22
- *(Historical note: early versions used `:cooperative` as the default; it is now
23
- a deprecated alias for `:immediate`.)*
24
- - **`Phronomy::EventLoop`** — singleton event dispatcher; drives the cooperative
25
- task cycle.
26
-
27
- Early implementations occasionally forced `ThreadBackend` inside framework
28
- components (e.g., `Agent::FSM#spawn_agent_task` in commit `0cb8510`) to work
29
- around a perceived limitation: if an agent makes a blocking I/O call with the
30
- cooperative backend active, it blocks the calling thread.
31
-
32
- This approach was identified as wrong for two reasons:
33
-
34
- 1. **Misplaced responsibility** — threading the *application's* I/O at the
35
- *framework* layer prevents the `runtime_backend` configuration from being
36
- honored and removes the app developer's control over the concurrency model.
37
- 2. **Violation of layering** — the framework should not assume that its callers
38
- are cooperative-scheduler-aware; it should provide primitives and let the
39
- caller decide the backend.
9
+ Phronomy must support many concurrently waiting Agent, Workflow, ToolInvocation,
10
+ and MultiAgent lifecycles without allocating one OS Thread per logical task.
11
+ At the same time, application and third-party code may contain synchronous work
12
+ that must not execute on the single Runtime EventLoop thread.
40
13
 
41
- There is a separate, legitimate category of blocking I/O: third-party gems
42
- (RubyLLM, ActiveRecord, Redis, Faraday, etc.) that perform blocking system
43
- calls internally and cannot be made non-blocking from the Phronomy side.
44
- These must be handled differently from application-controlled I/O.
14
+ Classifying arbitrary application work as I/O-bound, CPU-bound, or external
15
+ process work is not a responsibility the framework can reliably infer. The
16
+ architecturally relevant distinction for Phronomy is whether a unit of work can
17
+ safely run to completion on EventLoop or must be moved off the control thread.
45
18
 
46
19
  ## Decision
47
20
 
48
- ### Runtime backend landscape
49
-
50
- | Backend | Scheduler class | Role | Production use? |
51
- |---------|-----------------|------|----------------|
52
- | `:thread` | `ThreadScheduler` / `ThreadBackend` | **Default.** One OS thread per task. Provides true parallelism for blocking I/O workflows. | Yes |
53
- | `:immediate` | `FakeScheduler` / `ImmediateBackend` | **Unit test double.** Tasks run synchronously on the caller's thread; no extra threads. | Tests only |
54
- | `:fiber` | `DeterministicScheduler` / `FiberBackend` | **Experimental validation backend.** Runs tasks as Ruby Fibers to verify that framework components are truly non-blocking. Use in CI to catch inadvertent blocking; never use in production. Not a planned production replacement for `:thread`; preemptive scheduling will not be added. | No |
55
-
56
- Note: `:cooperative` is a deprecated alias for `:immediate` and must not be used in new code.
57
-
58
- ### Rule 1 — Cooperative-first for core control
59
-
60
- The core control flow of every Phronomy component **Agent, Workflow, Tool
61
- orchestration, Orchestrator, RAG pipeline, Streaming** MUST be implemented
62
- using cooperative task / event / scheduler primitives:
63
-
21
+ Framework lifecycle coordination uses one Runtime-owned EventLoop and explicit
22
+ FSMSession state/events. **Task is a completion handle**, not an execution
23
+ backend.
24
+
25
+ Phronomy defines two Tool execution modes:
26
+
27
+ - `:cooperative` short, EventLoop-safe work, or a specialized asynchronous
28
+ implementation that starts another Phronomy lifecycle and immediately returns
29
+ a completion handle.
30
+ - `:offloaded` — synchronous work that must not run on EventLoop. It executes in
31
+ the bounded `OffloadPool`.
32
+
33
+ `OffloadPool` is the thread execution boundary for synchronous work that must be
34
+ kept off EventLoop. It may contain blocking I/O, CPU-bound Ruby work, or other
35
+ application-defined long-running synchronous calls.
36
+
37
+ Workload classification such as I/O-bound versus CPU-bound is application-owned.
38
+ Phronomy does not provide separate `:blocking_io`, `:cpu_bound`, or
39
+ `:external_process` Tool execution modes.
40
+
41
+ Logical waits are never offloaded merely to obtain concurrency. Waiting for an
42
+ Agent, Workflow, ToolInvocation, approval, timer, or another Task is represented
43
+ as FSMSession state plus a later EventLoop event.
44
+
45
+ ## Runtime model
46
+
47
+ ```text
48
+ Runtime
49
+ ├─ EventLoop (one control-plane OS Thread)
50
+ │ └─ FSMSession
51
+ │ ├─ Agent
52
+ │ ├─ Workflow
53
+ │ ├─ ToolInvocation
54
+ │ └─ MultiAgent fan-out
55
+ ├─ OffloadPool (bounded OS Threads)
56
+ │ ├─ blocking I/O
57
+ │ ├─ CPU-bound synchronous work
58
+ │ └─ other long synchronous work
59
+ ├─ named OffloadPools
60
+ └─ EventLoop-driven timers
61
+
62
+ Task = completion handle
64
63
  ```
65
- Runtime.instance.spawn(name: "...") { ... } # respects configured backend
66
- EventLoop.instance.post { ... } # event dispatch
67
- ```
68
-
69
- Do NOT force `ThreadBackend` or `ThreadScheduler` inside framework components
70
- unless Rule 2 explicitly applies.
71
-
72
- ### Rule 2 — ThreadScheduler only for framework-owned infinite loops
73
-
74
- A framework component MAY use a dedicated `ThreadScheduler` (or `ThreadBackend`)
75
- if and only if **not** threading would unconditionally block the framework's own
76
- infinite loop.
77
-
78
- The only current example satisfying this criterion:
79
64
 
80
- ```ruby
81
- # EventLoop#start — the run_loop is the framework's own infinite dispatch loop.
82
- # It MUST run in its own thread; the cooperative backend cannot yield itself.
83
- thread_runtime = Phronomy::Runtime.new(
84
- scheduler: Phronomy::Runtime::ThreadScheduler.new
85
- )
86
- @task = thread_runtime.spawn(name: "event-loop") { run_loop }
65
+ ## Capacity and starvation
66
+
67
+ The default OffloadPool is a shared bounded resource. CPU-heavy work can occupy
68
+ slots that would otherwise be available to I/O, and slow I/O can do the same in
69
+ reverse. Phronomy guarantees bounded worker count, bounded queue depth,
70
+ backpressure, timeout/cancellation settlement, abandoned-worker accounting,
71
+ metrics, and lifecycle shutdown. It does **not** guarantee work-class fairness,
72
+ CPU isolation, core reservation, or CPU-bound speedup.
73
+
74
+ Applications own capacity planning through `offload_pool_size` and
75
+ `offload_queue_size`. Where isolation is required, applications may use
76
+ `Runtime#pool(name, size:, queue_size:)` to create independent named OffloadPool
77
+ resource domains.
78
+
79
+ ## EventLoop admission rule
80
+
81
+ An EventLoop action must not block while waiting for a free OffloadPool queue
82
+ slot. Framework-owned EventLoop-origin submissions therefore use non-blocking
83
+ admission (`on_full: :raise`) and propagate `BackpressureError` through the
84
+ normal FSM/completion path.
85
+
86
+ External management threads may deliberately choose other admission policies
87
+ when blocking the caller is acceptable.
88
+
89
+ ## Timeout and cancellation
90
+
91
+ An OffloadPool submit-time timeout settles the caller-facing PendingOperation.
92
+ It does not asynchronously interrupt a running worker Thread. If execution has
93
+ already started, the operation becomes abandoned, the worker may continue until
94
+ the submitted synchronous call returns, and that eventual worker result is
95
+ discarded.
96
+
97
+ The cancellation token passed to `OffloadPool#submit` follows the same
98
+ caller-facing settlement model:
99
+
100
+ - cancellation before worker execution prevents the submitted block from
101
+ starting;
102
+ - cancellation after worker execution starts settles the caller-facing
103
+ PendingOperation immediately, marks the operation abandoned, and allows the
104
+ worker to continue until the synchronous call returns;
105
+ - cancellation does not use `Thread#raise`;
106
+ - application code may observe the same CancellationToken and terminate its own
107
+ synchronous operation cooperatively.
108
+
109
+ A submit token with a monotonic deadline is connected to the Runtime timer queue,
110
+ so deadline expiry becomes explicit cancellation without a polling Thread.
111
+
112
+ `PendingOperation#blocking_wait(timeout:)` is a low-level synchronous bridge for
113
+ non-EventLoop callers such as tests and diagnostics. Its timeout is waiter-local:
114
+ it raises `TimeoutError` only to that caller and does not settle the
115
+ PendingOperation, cancel the submitted operation, or mark it abandoned.
116
+ PendingOperation does not define a waiter-local cancellation token;
117
+ operation-wide cancellation is represented only by the token passed to
118
+ `OffloadPool#submit`.
119
+
120
+ Independent notification callbacks are fault-isolated. A `StandardError` from one
121
+ `CancellationToken#on_cancel`, `Task#on_complete`, or
122
+ `PendingOperation#on_complete` subscriber is logged and does not suppress later
123
+ subscribers. This rule applies to notification fan-out; continuation or
124
+ transformation callbacks still report their own failures through the operation
125
+ they construct.
126
+
127
+ `abandoned_count` and the exported `offload_pool_abandoned_total` metric are
128
+ cumulative: they count operations whose caller-facing submit timeout or submit
129
+ cancellation settled after worker execution had already started.
130
+ `abandoned_active_count` and `offload_pool_abandoned_active` are current-state
131
+ values: they count only abandoned operations whose synchronous worker is still
132
+ occupying OffloadPool capacity.
133
+
134
+ ## CPU-bound work
135
+
136
+ CPU-bound work is allowed through `:offloaded`. Thread offload protects the
137
+ EventLoop from direct long synchronous execution but does not remove CRuby GVL
138
+ contention or physical CPU contention. Those are explicitly outside the core
139
+ OffloadPool guarantee.
140
+
141
+ A future subprocess capability may provide process isolation, hard process
142
+ termination, stdout/stderr capture, and CPU-worker separation. That future
143
+ implementation belongs to the offload subsystem and does not reintroduce a
144
+ Tool-level `:external_process` execution class.
145
+
146
+ ## Prohibited pattern
147
+
148
+ ```text
149
+ OffloadPool worker
150
+ → child_agent.invoke_async
151
+ → wait_result
87
152
  ```
88
153
 
89
- **Legitimate ThreadScheduler exceptions (exhaustive list):**
90
-
91
- | Component | Reason |
92
- |---|---|
93
- | `EventLoop#start` | `run_loop` is the framework's own `while @running` infinite dispatch loop; running it on the shared scheduler would consume the scheduler, preventing all other tasks from running |
94
-
95
- **Handler constraints for EventLoop:**
96
-
97
- - Handler code runs **on the EventLoop thread**. Do not perform blocking
98
- operations (database, LLM, HTTP) directly inside a handler — this stalls all
99
- session processing.
100
- - Do **not** call `Workflow#invoke` from within a handler. That call blocks
101
- until the EventLoop processes events, causing a deadlock. Use the async
102
- pattern: schedule work via `Runtime.instance.spawn` or `BlockingAdapterPool`,
103
- then post results back with `EventLoop#post`.
104
-
105
- All other framework components — including FSM, orchestration, RAG, streaming —
106
- do NOT own an infinite loop and therefore MUST use `Runtime.instance.spawn`.
107
-
108
- ### Rule 3 — BlockingAdapterPool for uncontrollable blocking I/O
154
+ and equivalently for Workflow/ToolInvocation/Task lifecycles.
109
155
 
110
- Third-party gems whose internal I/O Phronomy cannot control (RubyLLM, ActiveRecord,
111
- Redis client, Faraday, etc.) MUST be isolated behind a bounded
112
- `BlockingAdapterPool`.
156
+ That pattern converts a logical wait into worker-slot occupancy and can create
157
+ pool starvation. The correct model is:
113
158
 
159
+ ```text
160
+ parent FSMSession
161
+ → start child lifecycle
162
+ → return completion handle immediately
163
+ → child settles
164
+ → post parent EventLoop event
114
165
  ```
115
- ┌─────────────────────────────────────┐
116
- │ Cooperative EventLoop / Runtime │
117
- │ (FakeScheduler, ImmediateBackend) │
118
- │ │
119
- │ Agent ──► BlockingAdapterPool ──► RubyLLM (HTTP)
120
- │ RAG ──► BlockingAdapterPool ──► ActiveRecord / Redis
121
- └─────────────────────────────────────┘
122
-
123
- ┌─────────┴─────────┐
124
- │ Thread pool │ ← bounded (max_threads:)
125
- │ (OS threads) │
126
- └───────────────────┘
127
- ```
128
-
129
- Properties of `BlockingAdapterPool`:
130
- - Uses OS threads internally (they are the correct tool for I/O that releases
131
- the GVL).
132
- - **Always bounded** — configurable `max_threads:` (no unbounded `Thread.new`).
133
- - Returns a `Phronomy::Task`-compatible future so the cooperative layer can
134
- await results without blocking the EventLoop.
135
- - Is the **only** place in the framework that creates raw threads for I/O.
136
-
137
- > **Note:** `dispatch_parallel` (ADR-008) currently creates one thread per
138
- > sub-agent via `Thread.new`. This predates the `BlockingAdapterPool` concept
139
- > and is a candidate for future migration. Until that migration, it remains an
140
- > accepted exception per ADR-008.
141
-
142
- ## Scheduler Landscape (as of 2026-05-25)
143
-
144
- Three scheduler backends currently exist:
145
-
146
- | Symbol / Class | Status | Purpose |
147
- |---|---|---|
148
- | `:thread` — `ThreadScheduler` / `ThreadBackend` | **Production default** | One OS thread per task; GVL-releasing I/O works transparently |
149
- | `:immediate` — `FakeScheduler` / `ImmediateBackend` | Test / CI | Synchronous; block runs to completion before `spawn` returns; no threads |
150
- | `:fiber` — `DeterministicScheduler` / `FiberBackend` | **Experimental validation backend** | Tick-based Fiber scheduler with virtual clock; enables deterministic concurrency tests without wall-clock timers. Named backend added in Issue #334. |
151
-
152
- ### `:cooperative` deprecation
153
-
154
- `:cooperative` was a silent alias for `:immediate` (mapped to `FakeScheduler`).
155
- As of Issue #332, it now emits a `WARN`-level deprecation message and must not
156
- be used in new code. Issue #334 introduced `:fiber` as the named experimental
157
- validation backend; `:cooperative` is retained only for backwards compatibility.
158
-
159
- ### DeterministicScheduler as a stepping stone
160
-
161
- `DeterministicScheduler` is the foundation for the long-term production
162
- cooperative runtime goal. It provides:
163
-
164
- - Ready-queue dispatch, virtual timer heap, scheduler signal API
165
- - `FiberBackend` wrapping tasks as Fibers
166
- - `tick` / `run_until_idle` / `advance(seconds)` for deterministic test control
167
-
168
- **Remaining gaps versus a full production cooperative runtime:**
169
-
170
- - No real-wall-clock timer integration (`TimerQueue` runs as a background thread — Issue #331)
171
- - No `BlockingAdapterPool` integration for LLM/network calls (Issue #280)
172
- - Scheduler must be driven manually (`tick`) rather than event-loop-driven
173
-
174
- The minimum delta to promote `DeterministicScheduler` to production use:
175
-
176
- 1. Integrate `TimerQueue` into the scheduler `tick` cycle (eliminates timer thread — Issue #331)
177
- 2. Implement `BlockingAdapterPool` and wire LLM/tool calls through it (Issue #280)
178
- 3. ~~Expose as a named `runtime_backend` (`:fiber`)~~ — **done** (Issue #334)
179
- 4. Add real-wall-clock integration (replace virtual time with `Process.clock_gettime`)
180
-
181
- Progress is tracked in Issues #331 and #280.
182
-
183
- ### TimerQueue background thread
184
-
185
- `Runtime::TimerQueue` currently runs as a dedicated background OS thread
186
- (`Thread.new { run_loop }`). This is an accepted interim implementation — it is
187
- robust for production but violates the cooperative-first goal. The long-term
188
- target is to integrate timer firing into the scheduler's own `tick` cycle so
189
- that no separate timer thread is needed. Progress tracked in Issue #331.
190
166
 
191
167
  ## Consequences
192
168
 
193
- ### Positive
194
- truly means cooperative for all framework-controlled paths.
195
- - Thread creation is isolated and bounded; no accidental unbounded thread
196
- proliferation from deep inside the framework.
197
- - The concurrency model is layered and explicit:
198
- cooperative layer adapter boundary bounded thread pool → blocking gem.
199
- - Tests can reliably use `FakeScheduler` by default and opt in to `:thread`
200
- only where true concurrency is required.
201
- - `DeterministicScheduler` + `FiberBackend` provide a deterministic test
202
- foundation for cooperative concurrency without wall-clock timers.
203
-
204
- ### Negative / Tradeoffs
205
- - `BlockingAdapterPool` is not yet implemented (as of ADR-010 acceptance).
206
- Until it is, callers that need real blocking I/O must opt in to `:thread`
207
- backend explicitly (e.g., in tests via `around` blocks).
208
- - `dispatch_parallel` remains on raw threads (ADR-008) until migrated.
209
- - `TimerQueue` runs as a background OS thread (interim; see Issue #331).
210
- - `DeterministicScheduler` backs the named `:fiber` backend (Issue #334
211
- resolved). It remains experimental and not for production use; the remaining
212
- steps toward a full production cooperative runtime are `TimerQueue`
213
- integration (Issue #331) and `BlockingAdapterPool` completion (Issue #280).
214
-
215
- ## Derived Checklist
216
-
217
- When writing or reviewing any Phronomy component, apply this checklist:
218
-
219
- | Question | Correct action |
220
- |---|---|
221
- | Does this component own a `while true` / `loop do` that blocks forever? | May use dedicated `ThreadScheduler` (Rule 2) |
222
- | Does this component call into app/agent code (LLM, tools)? | `Runtime.instance.spawn` — no ThreadBackend (Rule 1) |
223
- | Does this component call a blocking gem (RubyLLM, AR, Redis)? | Route through `BlockingAdapterPool` (Rule 3) |
224
- | Is this a test that needs real concurrency (sleep + cancel)? | `around` block with `c.runtime_backend = :thread` opt-in |
225
- | Anything else | Default: `Runtime.instance.spawn` |
226
-
227
- ## Related ADRs and Issues
228
-
229
- - ADR-008: Orchestrator Uses OS Threads for Parallel Dispatch (predates this
230
- ADR; `dispatch_parallel` is a future migration candidate)
231
- - Issue #331: TimerQueue scheduler integration (long-term — eliminate timer thread)
232
- - Issue #334: Promote DeterministicScheduler to production cooperative runtime
233
- - Issue #332: `:cooperative` alias deprecation (resolved 2026-05-25)
234
- - Issue #280: MCP transports behind BlockingAdapterPool (pending)
235
-
236
- ## Non-goals
237
-
238
- The following capabilities are intentionally out of scope for this framework's
239
- concurrency layer:
240
-
241
- - **CPU-bound process pool** — A `ProcessPoolExecutor` equivalent is not part
242
- of core framework by default. CPU-intensive tool work belongs at the
243
- application layer (fork, Sidekiq, etc.). A separate ADR would be required
244
- to introduce one.
245
- - **External process manager** — Spawning, monitoring, or restarting external
246
- subprocesses is not currently a framework responsibility. A separate ADR
247
- would be required.
248
- - **Preemptive scheduling** — The cooperative-first model is non-preemptive by
249
- design. Introducing a preemptive scheduler or promoting `:fiber` to
250
- production default is not currently planned; a separate ADR would be required.
251
- - **Additional ToolExecutor core execution routes** — Only `:cooperative` and
252
- `:blocking_io` are core dispatch routes. `:cpu_bound` and `:external_process`
253
- are compatibility aliases that fall back to `:blocking_io` with a warning.
254
- Any genuinely new core execution route requires a new ADR.
255
-
169
+ - There is one explicit framework continuation model: FSMSession + EventLoop.
170
+ - `Task` stays thread-free and represents settlement only.
171
+ - Tool execution classification becomes `:cooperative` / `:offloaded`.
172
+ - CPU/I/O classification and resource sizing are application responsibilities.
173
+ - Named pools remain available for application-managed resource isolation.
174
+ - Production Fiber execution is not part of the architecture.
175
+ - Raw production Threads remain confined to EventLoop and OffloadPool.