phronomy 0.7.0 → 0.7.1

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 (70) hide show
  1. checksums.yaml +4 -4
  2. data/.mutant.yml +8 -7
  3. data/CHANGELOG.md +151 -1
  4. data/README.md +155 -32
  5. data/Rakefile +33 -0
  6. data/benchmark/baseline.json +1 -1
  7. data/benchmark/bench_regression.rb +1 -0
  8. data/docs/decisions/004-invoke-timeout-is-not-cancellation.md +24 -0
  9. data/docs/decisions/006-no-built-in-guardrails.md +20 -2
  10. data/docs/decisions/010-cooperative-first-concurrency.md +248 -0
  11. data/lib/phronomy/agent/base.rb +250 -65
  12. data/lib/phronomy/agent/concerns/suspendable.rb +15 -0
  13. data/lib/phronomy/agent/fsm.rb +41 -64
  14. data/lib/phronomy/agent/orchestrator.rb +146 -121
  15. data/lib/phronomy/agent/parallel_tool_chat.rb +79 -22
  16. data/lib/phronomy/agent/react_agent.rb +8 -0
  17. data/lib/phronomy/async_queue.rb +155 -0
  18. data/lib/phronomy/blocking_adapter_pool.rb +435 -0
  19. data/lib/phronomy/cancellation_scope.rb +123 -0
  20. data/lib/phronomy/cancellation_token.rb +43 -2
  21. data/lib/phronomy/concurrency_gate.rb +155 -0
  22. data/lib/phronomy/configuration.rb +142 -0
  23. data/lib/phronomy/deadline.rb +63 -0
  24. data/lib/phronomy/diagnostics.rb +62 -0
  25. data/lib/phronomy/embeddings/base.rb +17 -0
  26. data/lib/phronomy/eval/runner.rb +9 -9
  27. data/lib/phronomy/event_loop.rb +181 -43
  28. data/lib/phronomy/fsm_session.rb +50 -4
  29. data/lib/phronomy/guardrail/prompt_injection_guardrail.rb +58 -0
  30. data/lib/phronomy/invocation_context.rb +152 -0
  31. data/lib/phronomy/knowledge_source/base.rb +18 -0
  32. data/lib/phronomy/llm_adapter/base.rb +104 -0
  33. data/lib/phronomy/llm_adapter/ruby_llm.rb +41 -0
  34. data/lib/phronomy/llm_adapter.rb +20 -0
  35. data/lib/phronomy/metrics.rb +38 -0
  36. data/lib/phronomy/runtime/deterministic_scheduler.rb +412 -0
  37. data/lib/phronomy/runtime/fake_scheduler.rb +165 -0
  38. data/lib/phronomy/runtime/gate_registry.rb +52 -0
  39. data/lib/phronomy/runtime/pool_registry.rb +57 -0
  40. data/lib/phronomy/runtime/runtime_metrics.rb +117 -0
  41. data/lib/phronomy/runtime/scheduler.rb +98 -0
  42. data/lib/phronomy/runtime/scheduler_timer_adapter.rb +79 -0
  43. data/lib/phronomy/runtime/task_registry.rb +48 -0
  44. data/lib/phronomy/runtime/thread_scheduler.rb +30 -0
  45. data/lib/phronomy/runtime/timer_queue.rb +106 -0
  46. data/lib/phronomy/runtime/timer_service.rb +42 -0
  47. data/lib/phronomy/runtime.rb +374 -0
  48. data/lib/phronomy/task/backend.rb +80 -0
  49. data/lib/phronomy/task/fiber_backend.rb +157 -0
  50. data/lib/phronomy/task/immediate_backend.rb +89 -0
  51. data/lib/phronomy/task/thread_backend.rb +84 -0
  52. data/lib/phronomy/task.rb +275 -0
  53. data/lib/phronomy/task_group.rb +265 -0
  54. data/lib/phronomy/testing/fake_clock.rb +109 -0
  55. data/lib/phronomy/testing/fake_scheduler.rb +104 -0
  56. data/lib/phronomy/testing/scheduler_helpers.rb +59 -0
  57. data/lib/phronomy/testing.rb +12 -0
  58. data/lib/phronomy/tool/base.rb +110 -2
  59. data/lib/phronomy/tool/mcp_tool.rb +47 -16
  60. data/lib/phronomy/tool/scope_policy.rb +50 -0
  61. data/lib/phronomy/tool_executor.rb +106 -0
  62. data/lib/phronomy/tracing/open_telemetry_tracer.rb +34 -0
  63. data/lib/phronomy/vector_store/async_backend.rb +110 -0
  64. data/lib/phronomy/vector_store/base.rb +7 -0
  65. data/lib/phronomy/version.rb +1 -1
  66. data/lib/phronomy/workflow.rb +52 -5
  67. data/lib/phronomy/workflow_context.rb +29 -2
  68. data/lib/phronomy/workflow_runner.rb +74 -3
  69. data/lib/phronomy.rb +42 -0
  70. metadata +40 -2
@@ -0,0 +1,248 @@
1
+ # ADR-010: Cooperative-First Concurrency — BlockingAdapterPool for Uncontrollable I/O
2
+
3
+ ## Status
4
+
5
+ Accepted — updated 2026-05-25 to document current scheduler landscape and
6
+ production-cooperative roadmap (Issues #331, #332, #334).
7
+
8
+ ## Context
9
+
10
+ Phronomy provides its own concurrency primitives:
11
+
12
+ - **`Phronomy::Runtime`** — task scheduler; backend is configurable.
13
+ The current production default is `:thread` (`ThreadScheduler` / `ThreadBackend`).
14
+ See the backend landscape table below for all supported values.
15
+ *(Historical note: early versions used `:cooperative` as the default; it is now
16
+ a deprecated alias for `:immediate`.)*
17
+ - **`Phronomy::EventLoop`** — singleton event dispatcher; drives the cooperative
18
+ task cycle.
19
+
20
+ Early implementations occasionally forced `ThreadBackend` inside framework
21
+ components (e.g., `Agent::FSM#spawn_agent_task` in commit `0cb8510`) to work
22
+ around a perceived limitation: if an agent makes a blocking I/O call with the
23
+ cooperative backend active, it blocks the calling thread.
24
+
25
+ This approach was identified as wrong for two reasons:
26
+
27
+ 1. **Misplaced responsibility** — threading the *application's* I/O at the
28
+ *framework* layer prevents the `runtime_backend` configuration from being
29
+ honored and removes the app developer's control over the concurrency model.
30
+ 2. **Violation of layering** — the framework should not assume that its callers
31
+ are cooperative-scheduler-aware; it should provide primitives and let the
32
+ caller decide the backend.
33
+
34
+ There is a separate, legitimate category of blocking I/O: third-party gems
35
+ (RubyLLM, ActiveRecord, Redis, Faraday, etc.) that perform blocking system
36
+ calls internally and cannot be made non-blocking from the Phronomy side.
37
+ These must be handled differently from application-controlled I/O.
38
+
39
+ ## Decision
40
+
41
+ ### Runtime backend landscape
42
+
43
+ | Backend | Scheduler class | Role | Production use? |
44
+ |---------|-----------------|------|----------------|
45
+ | `:thread` | `ThreadScheduler` / `ThreadBackend` | **Default.** One OS thread per task. Provides true parallelism for blocking I/O workflows. | Yes |
46
+ | `:immediate` | `FakeScheduler` / `ImmediateBackend` | **Unit test double.** Tasks run synchronously on the caller's thread; no extra threads. | Tests only |
47
+ | `: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 |
48
+
49
+ Note: `:cooperative` is a deprecated alias for `:immediate` and must not be used in new code.
50
+
51
+ ### Rule 1 — Cooperative-first for core control
52
+
53
+ The core control flow of every Phronomy component — **Agent, Workflow, Tool
54
+ orchestration, Orchestrator, RAG pipeline, Streaming** — MUST be implemented
55
+ using cooperative task / event / scheduler primitives:
56
+
57
+ ```
58
+ Runtime.instance.spawn(name: "...") { ... } # respects configured backend
59
+ EventLoop.instance.post { ... } # event dispatch
60
+ ```
61
+
62
+ Do NOT force `ThreadBackend` or `ThreadScheduler` inside framework components
63
+ unless Rule 2 explicitly applies.
64
+
65
+ ### Rule 2 — ThreadScheduler only for framework-owned infinite loops
66
+
67
+ A framework component MAY use a dedicated `ThreadScheduler` (or `ThreadBackend`)
68
+ if and only if **not** threading would unconditionally block the framework's own
69
+ infinite loop.
70
+
71
+ The only current example satisfying this criterion:
72
+
73
+ ```ruby
74
+ # EventLoop#start — the run_loop is the framework's own infinite dispatch loop.
75
+ # It MUST run in its own thread; the cooperative backend cannot yield itself.
76
+ thread_runtime = Phronomy::Runtime.new(
77
+ scheduler: Phronomy::Runtime::ThreadScheduler.new
78
+ )
79
+ @task = thread_runtime.spawn(name: "event-loop") { run_loop }
80
+ ```
81
+
82
+ **Legitimate ThreadScheduler exceptions (exhaustive list):**
83
+
84
+ | Component | Reason |
85
+ |---|---|
86
+ | `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 |
87
+
88
+ **Handler constraints for EventLoop:**
89
+
90
+ - Handler code runs **on the EventLoop thread**. Do not perform blocking
91
+ operations (database, LLM, HTTP) directly inside a handler — this stalls all
92
+ session processing.
93
+ - Do **not** call `Workflow#invoke` from within a handler. That call blocks
94
+ until the EventLoop processes events, causing a deadlock. Use the async
95
+ pattern: schedule work via `Runtime.instance.spawn` or `BlockingAdapterPool`,
96
+ then post results back with `EventLoop#post`.
97
+
98
+ All other framework components — including FSM, orchestration, RAG, streaming —
99
+ do NOT own an infinite loop and therefore MUST use `Runtime.instance.spawn`.
100
+
101
+ ### Rule 3 — BlockingAdapterPool for uncontrollable blocking I/O
102
+
103
+ Third-party gems whose internal I/O Phronomy cannot control (RubyLLM, ActiveRecord,
104
+ Redis client, Faraday, etc.) MUST be isolated behind a bounded
105
+ `BlockingAdapterPool`.
106
+
107
+ ```
108
+ ┌─────────────────────────────────────┐
109
+ │ Cooperative EventLoop / Runtime │
110
+ │ (FakeScheduler, ImmediateBackend) │
111
+ │ │
112
+ │ Agent ──► BlockingAdapterPool ──► RubyLLM (HTTP)
113
+ │ RAG ──► BlockingAdapterPool ──► ActiveRecord / Redis
114
+ └─────────────────────────────────────┘
115
+
116
+ ┌─────────┴─────────┐
117
+ │ Thread pool │ ← bounded (max_threads:)
118
+ │ (OS threads) │
119
+ └───────────────────┘
120
+ ```
121
+
122
+ Properties of `BlockingAdapterPool`:
123
+ - Uses OS threads internally (they are the correct tool for I/O that releases
124
+ the GVL).
125
+ - **Always bounded** — configurable `max_threads:` (no unbounded `Thread.new`).
126
+ - Returns a `Phronomy::Task`-compatible future so the cooperative layer can
127
+ await results without blocking the EventLoop.
128
+ - Is the **only** place in the framework that creates raw threads for I/O.
129
+
130
+ > **Note:** `dispatch_parallel` (ADR-008) currently creates one thread per
131
+ > sub-agent via `Thread.new`. This predates the `BlockingAdapterPool` concept
132
+ > and is a candidate for future migration. Until that migration, it remains an
133
+ > accepted exception per ADR-008.
134
+
135
+ ## Scheduler Landscape (as of 2026-05-25)
136
+
137
+ Three scheduler backends currently exist:
138
+
139
+ | Symbol / Class | Status | Purpose |
140
+ |---|---|---|
141
+ | `:thread` — `ThreadScheduler` / `ThreadBackend` | **Production default** | One OS thread per task; GVL-releasing I/O works transparently |
142
+ | `:immediate` — `FakeScheduler` / `ImmediateBackend` | Test / CI | Synchronous; block runs to completion before `spawn` returns; no threads |
143
+ | `: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. |
144
+
145
+ ### `:cooperative` deprecation
146
+
147
+ `:cooperative` was a silent alias for `:immediate` (mapped to `FakeScheduler`).
148
+ As of Issue #332, it now emits a `WARN`-level deprecation message and must not
149
+ be used in new code. Issue #334 introduced `:fiber` as the named experimental
150
+ validation backend; `:cooperative` is retained only for backwards compatibility.
151
+
152
+ ### DeterministicScheduler as a stepping stone
153
+
154
+ `DeterministicScheduler` is the foundation for the long-term production
155
+ cooperative runtime goal. It provides:
156
+
157
+ - Ready-queue dispatch, virtual timer heap, scheduler signal API
158
+ - `FiberBackend` wrapping tasks as Fibers
159
+ - `tick` / `run_until_idle` / `advance(seconds)` for deterministic test control
160
+
161
+ **Remaining gaps versus a full production cooperative runtime:**
162
+
163
+ - No real-wall-clock timer integration (`TimerQueue` runs as a background thread — Issue #331)
164
+ - No `BlockingAdapterPool` integration for LLM/network calls (Issue #280)
165
+ - Scheduler must be driven manually (`tick`) rather than event-loop-driven
166
+
167
+ The minimum delta to promote `DeterministicScheduler` to production use:
168
+
169
+ 1. Integrate `TimerQueue` into the scheduler `tick` cycle (eliminates timer thread — Issue #331)
170
+ 2. Implement `BlockingAdapterPool` and wire LLM/tool calls through it (Issue #280)
171
+ 3. ~~Expose as a named `runtime_backend` (`:fiber`)~~ — **done** (Issue #334)
172
+ 4. Add real-wall-clock integration (replace virtual time with `Process.clock_gettime`)
173
+
174
+ Progress is tracked in Issues #331 and #280.
175
+
176
+ ### TimerQueue background thread
177
+
178
+ `Runtime::TimerQueue` currently runs as a dedicated background OS thread
179
+ (`Thread.new { run_loop }`). This is an accepted interim implementation — it is
180
+ robust for production but violates the cooperative-first goal. The long-term
181
+ target is to integrate timer firing into the scheduler's own `tick` cycle so
182
+ that no separate timer thread is needed. Progress tracked in Issue #331.
183
+
184
+ ## Consequences
185
+
186
+ ### Positive
187
+ truly means cooperative for all framework-controlled paths.
188
+ - Thread creation is isolated and bounded; no accidental unbounded thread
189
+ proliferation from deep inside the framework.
190
+ - The concurrency model is layered and explicit:
191
+ cooperative layer → adapter boundary → bounded thread pool → blocking gem.
192
+ - Tests can reliably use `FakeScheduler` by default and opt in to `:thread`
193
+ only where true concurrency is required.
194
+ - `DeterministicScheduler` + `FiberBackend` provide a deterministic test
195
+ foundation for cooperative concurrency without wall-clock timers.
196
+
197
+ ### Negative / Tradeoffs
198
+ - `BlockingAdapterPool` is not yet implemented (as of ADR-010 acceptance).
199
+ Until it is, callers that need real blocking I/O must opt in to `:thread`
200
+ backend explicitly (e.g., in tests via `around` blocks).
201
+ - `dispatch_parallel` remains on raw threads (ADR-008) until migrated.
202
+ - `TimerQueue` runs as a background OS thread (interim; see Issue #331).
203
+ - `DeterministicScheduler` backs the named `:fiber` backend (Issue #334
204
+ resolved). It remains experimental and not for production use; the remaining
205
+ steps toward a full production cooperative runtime are `TimerQueue`
206
+ integration (Issue #331) and `BlockingAdapterPool` completion (Issue #280).
207
+
208
+ ## Derived Checklist
209
+
210
+ When writing or reviewing any Phronomy component, apply this checklist:
211
+
212
+ | Question | Correct action |
213
+ |---|---|
214
+ | Does this component own a `while true` / `loop do` that blocks forever? | May use dedicated `ThreadScheduler` (Rule 2) |
215
+ | Does this component call into app/agent code (LLM, tools)? | `Runtime.instance.spawn` — no ThreadBackend (Rule 1) |
216
+ | Does this component call a blocking gem (RubyLLM, AR, Redis)? | Route through `BlockingAdapterPool` (Rule 3) |
217
+ | Is this a test that needs real concurrency (sleep + cancel)? | `around` block with `c.runtime_backend = :thread` opt-in |
218
+ | Anything else | Default: `Runtime.instance.spawn` |
219
+
220
+ ## Related ADRs and Issues
221
+
222
+ - ADR-008: Orchestrator Uses OS Threads for Parallel Dispatch (predates this
223
+ ADR; `dispatch_parallel` is a future migration candidate)
224
+ - Issue #331: TimerQueue scheduler integration (long-term — eliminate timer thread)
225
+ - Issue #334: Promote DeterministicScheduler to production cooperative runtime
226
+ - Issue #332: `:cooperative` alias deprecation (resolved 2026-05-25)
227
+ - Issue #280: MCP transports behind BlockingAdapterPool (pending)
228
+
229
+ ## Non-goals
230
+
231
+ The following capabilities are intentionally out of scope for this framework's
232
+ concurrency layer:
233
+
234
+ - **CPU-bound process pool** — A `ProcessPoolExecutor` equivalent is not part
235
+ of core framework by default. CPU-intensive tool work belongs at the
236
+ application layer (fork, Sidekiq, etc.). A separate ADR would be required
237
+ to introduce one.
238
+ - **External process manager** — Spawning, monitoring, or restarting external
239
+ subprocesses is not currently a framework responsibility. A separate ADR
240
+ would be required.
241
+ - **Preemptive scheduling** — The cooperative-first model is non-preemptive by
242
+ design. Introducing a preemptive scheduler or promoting `:fiber` to
243
+ production default is not currently planned; a separate ADR would be required.
244
+ - **Additional ToolExecutor core execution routes** — Only `:cooperative` and
245
+ `:blocking_io` are core dispatch routes. `:cpu_bound` and `:external_process`
246
+ are compatibility aliases that fall back to `:blocking_io` with a warning.
247
+ Any genuinely new core execution route requires a new ADR.
248
+