phronomy 0.6.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 (143) hide show
  1. checksums.yaml +4 -4
  2. data/.mutant.yml +22 -0
  3. data/CHANGELOG.md +488 -0
  4. data/CONTRIBUTING.md +102 -0
  5. data/README.md +374 -36
  6. data/RELEASE_CHECKLIST.md +86 -0
  7. data/Rakefile +33 -0
  8. data/SECURITY.md +80 -0
  9. data/benchmark/baseline.json +9 -0
  10. data/benchmark/bench_agent_invoke.rb +105 -0
  11. data/benchmark/bench_context_assembler.rb +46 -0
  12. data/benchmark/bench_regression.rb +172 -0
  13. data/benchmark/bench_token_estimator.rb +44 -0
  14. data/benchmark/bench_tool_schema.rb +69 -0
  15. data/benchmark/bench_vector_store.rb +39 -0
  16. data/benchmark/bench_workflow.rb +55 -0
  17. data/benchmark/run_all.rb +118 -0
  18. data/docs/decisions/001-rubyllm-as-provider-layer.md +42 -0
  19. data/docs/decisions/002-workflow-context-immutability.md +42 -0
  20. data/docs/decisions/003-event-loop-singleton.md +48 -0
  21. data/docs/decisions/004-invoke-timeout-is-not-cancellation.md +75 -0
  22. data/docs/decisions/005-static-knowledge-class-level-cache.md +45 -0
  23. data/docs/decisions/006-no-built-in-guardrails.md +66 -0
  24. data/docs/decisions/007-mcp-is-beta-stability.md +51 -0
  25. data/docs/decisions/008-orchestrator-uses-os-threads.md +52 -0
  26. data/docs/decisions/009-state-store-abstraction.md +141 -0
  27. data/docs/decisions/010-cooperative-first-concurrency.md +248 -0
  28. data/lib/phronomy/agent/base.rb +416 -49
  29. data/lib/phronomy/agent/before_completion_context.rb +1 -0
  30. data/lib/phronomy/agent/checkpoint.rb +1 -0
  31. data/lib/phronomy/agent/concerns/before_completion.rb +6 -0
  32. data/lib/phronomy/agent/concerns/error_translation.rb +45 -0
  33. data/lib/phronomy/agent/concerns/guardrailable.rb +3 -0
  34. data/lib/phronomy/agent/concerns/retryable.rb +12 -1
  35. data/lib/phronomy/agent/concerns/suspendable.rb +19 -0
  36. data/lib/phronomy/agent/fsm.rb +44 -52
  37. data/lib/phronomy/agent/handoff.rb +3 -0
  38. data/lib/phronomy/agent/orchestrator.rb +191 -54
  39. data/lib/phronomy/agent/parallel_tool_chat.rb +87 -13
  40. data/lib/phronomy/agent/react_agent.rb +16 -6
  41. data/lib/phronomy/agent/runner.rb +2 -0
  42. data/lib/phronomy/agent/shared_state.rb +11 -0
  43. data/lib/phronomy/agent/suspend_signal.rb +2 -0
  44. data/lib/phronomy/agent/team_coordinator.rb +17 -5
  45. data/lib/phronomy/async_queue.rb +155 -0
  46. data/lib/phronomy/blocking_adapter_pool.rb +435 -0
  47. data/lib/phronomy/cancellation_scope.rb +123 -0
  48. data/lib/phronomy/cancellation_token.rb +133 -0
  49. data/lib/phronomy/concurrency_gate.rb +155 -0
  50. data/lib/phronomy/configuration.rb +168 -2
  51. data/lib/phronomy/context/assembler.rb +6 -0
  52. data/lib/phronomy/context/compaction_context.rb +2 -0
  53. data/lib/phronomy/context/context_version_cache.rb +2 -0
  54. data/lib/phronomy/context/token_budget.rb +3 -0
  55. data/lib/phronomy/context/token_estimator.rb +9 -2
  56. data/lib/phronomy/context/trigger_context.rb +1 -0
  57. data/lib/phronomy/context/trim_context.rb +4 -0
  58. data/lib/phronomy/deadline.rb +63 -0
  59. data/lib/phronomy/diagnostics.rb +62 -0
  60. data/lib/phronomy/embeddings/base.rb +22 -2
  61. data/lib/phronomy/embeddings/ruby_llm_embeddings.rb +6 -2
  62. data/lib/phronomy/eval/comparison.rb +2 -0
  63. data/lib/phronomy/eval/dataset.rb +4 -0
  64. data/lib/phronomy/eval/metrics.rb +6 -0
  65. data/lib/phronomy/eval/runner.rb +11 -9
  66. data/lib/phronomy/eval/scorer/base.rb +1 -0
  67. data/lib/phronomy/eval/scorer/exact_match.rb +2 -0
  68. data/lib/phronomy/eval/scorer/includes_scorer.rb +2 -0
  69. data/lib/phronomy/eval/scorer/llm_judge.rb +2 -0
  70. data/lib/phronomy/event_loop.rb +275 -30
  71. data/lib/phronomy/fsm_session.rb +57 -4
  72. data/lib/phronomy/generator_verifier.rb +2 -0
  73. data/lib/phronomy/guardrail/base.rb +3 -0
  74. data/lib/phronomy/guardrail/prompt_injection_guardrail.rb +58 -0
  75. data/lib/phronomy/invocation_context.rb +152 -0
  76. data/lib/phronomy/knowledge_source/base.rb +24 -2
  77. data/lib/phronomy/knowledge_source/entity_knowledge.rb +7 -2
  78. data/lib/phronomy/knowledge_source/rag_knowledge.rb +8 -4
  79. data/lib/phronomy/knowledge_source/static_knowledge.rb +7 -2
  80. data/lib/phronomy/llm_adapter/base.rb +104 -0
  81. data/lib/phronomy/llm_adapter/ruby_llm.rb +41 -0
  82. data/lib/phronomy/llm_adapter.rb +20 -0
  83. data/lib/phronomy/loader/base.rb +1 -0
  84. data/lib/phronomy/loader/csv_loader.rb +2 -0
  85. data/lib/phronomy/loader/markdown_loader.rb +2 -0
  86. data/lib/phronomy/loader/plain_text_loader.rb +1 -0
  87. data/lib/phronomy/metrics.rb +38 -0
  88. data/lib/phronomy/output_parser/base.rb +1 -0
  89. data/lib/phronomy/output_parser/json_parser.rb +22 -3
  90. data/lib/phronomy/output_parser/structured_parser.rb +2 -0
  91. data/lib/phronomy/prompt_template.rb +5 -0
  92. data/lib/phronomy/runnable.rb +20 -3
  93. data/lib/phronomy/runtime/deterministic_scheduler.rb +412 -0
  94. data/lib/phronomy/runtime/fake_scheduler.rb +165 -0
  95. data/lib/phronomy/runtime/gate_registry.rb +52 -0
  96. data/lib/phronomy/runtime/pool_registry.rb +57 -0
  97. data/lib/phronomy/runtime/runtime_metrics.rb +117 -0
  98. data/lib/phronomy/runtime/scheduler.rb +98 -0
  99. data/lib/phronomy/runtime/scheduler_timer_adapter.rb +79 -0
  100. data/lib/phronomy/runtime/task_registry.rb +48 -0
  101. data/lib/phronomy/runtime/thread_scheduler.rb +30 -0
  102. data/lib/phronomy/runtime/timer_queue.rb +106 -0
  103. data/lib/phronomy/runtime/timer_service.rb +42 -0
  104. data/lib/phronomy/runtime.rb +374 -0
  105. data/lib/phronomy/splitter/base.rb +2 -0
  106. data/lib/phronomy/splitter/fixed_size_splitter.rb +2 -0
  107. data/lib/phronomy/splitter/recursive_splitter.rb +2 -0
  108. data/lib/phronomy/state_store/base.rb +48 -0
  109. data/lib/phronomy/state_store/in_memory.rb +62 -0
  110. data/lib/phronomy/task/backend.rb +80 -0
  111. data/lib/phronomy/task/fiber_backend.rb +157 -0
  112. data/lib/phronomy/task/immediate_backend.rb +89 -0
  113. data/lib/phronomy/task/thread_backend.rb +84 -0
  114. data/lib/phronomy/task.rb +275 -0
  115. data/lib/phronomy/task_group.rb +265 -0
  116. data/lib/phronomy/testing/fake_clock.rb +109 -0
  117. data/lib/phronomy/testing/fake_scheduler.rb +104 -0
  118. data/lib/phronomy/testing/scheduler_helpers.rb +59 -0
  119. data/lib/phronomy/testing.rb +12 -0
  120. data/lib/phronomy/tool/agent_tool.rb +1 -0
  121. data/lib/phronomy/tool/base.rb +298 -28
  122. data/lib/phronomy/tool/mcp_tool.rb +103 -17
  123. data/lib/phronomy/tool/scope_policy.rb +50 -0
  124. data/lib/phronomy/tool_executor.rb +106 -0
  125. data/lib/phronomy/tracing/base.rb +3 -0
  126. data/lib/phronomy/tracing/langfuse_tracer.rb +2 -0
  127. data/lib/phronomy/tracing/open_telemetry_tracer.rb +36 -0
  128. data/lib/phronomy/vector_store/async_backend.rb +110 -0
  129. data/lib/phronomy/vector_store/base.rb +40 -7
  130. data/lib/phronomy/vector_store/in_memory.rb +16 -7
  131. data/lib/phronomy/vector_store/pgvector.rb +40 -9
  132. data/lib/phronomy/vector_store/redis_search.rb +29 -8
  133. data/lib/phronomy/version.rb +1 -1
  134. data/lib/phronomy/workflow.rb +147 -11
  135. data/lib/phronomy/workflow_context.rb +83 -6
  136. data/lib/phronomy/workflow_runner.rb +106 -7
  137. data/lib/phronomy.rb +112 -1
  138. data/scripts/api_snapshot.rb +91 -0
  139. data/scripts/check_api_annotations.rb +68 -0
  140. data/scripts/check_private_enforcement.rb +93 -0
  141. data/scripts/check_readme_runnable.rb +98 -0
  142. data/scripts/run_mutation.sh +46 -0
  143. metadata +83 -2
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ class Runtime
5
+ # Registry and lifecycle manager for {BlockingAdapterPool} instances.
6
+ #
7
+ # Maintains one unnamed "default" pool (accessed via {#default_pool}) and
8
+ # an arbitrary number of named pools (accessed via {#named_pool}).
9
+ # All pools are shut down together by {#shutdown}.
10
+ # @api private
11
+ class PoolRegistry
12
+ def initialize
13
+ @mutex = Mutex.new
14
+ @pools = {}
15
+ @default = nil
16
+ end
17
+
18
+ # Returns (or lazily creates) the unnamed default pool.
19
+ # @param pool_size [Integer]
20
+ # @param queue_size [Integer]
21
+ # @return [BlockingAdapterPool]
22
+ # @api private
23
+ def default_pool(pool_size: 10, queue_size: 100)
24
+ @default ||= BlockingAdapterPool.new(
25
+ name: :default,
26
+ pool_size: pool_size,
27
+ queue_size: queue_size
28
+ )
29
+ end
30
+
31
+ # Returns (or lazily creates) a named pool.
32
+ # @param name [Symbol, String]
33
+ # @param size [Integer]
34
+ # @param queue_size [Integer]
35
+ # @return [BlockingAdapterPool]
36
+ # @api private
37
+ def named_pool(name, size: 10, queue_size: 100)
38
+ @mutex.synchronize do
39
+ @pools[name.to_sym] ||= BlockingAdapterPool.new(
40
+ name: name,
41
+ pool_size: size,
42
+ queue_size: queue_size
43
+ )
44
+ end
45
+ end
46
+
47
+ # Shuts down the default pool and all named pools.
48
+ # @return [void]
49
+ # @api private
50
+ def shutdown
51
+ @default&.shutdown
52
+ pools = @mutex.synchronize { @pools.values.dup }
53
+ pools.each(&:shutdown)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ class Runtime
5
+ # Internal store for task-centric counters and latency samples.
6
+ #
7
+ # All access is mutex-protected. The ring buffers for wait/run times are
8
+ # bounded to {WINDOW} samples so that long-lived runtimes do not grow
9
+ # unbounded.
10
+ # @api private
11
+ class RuntimeMetrics
12
+ WINDOW = 1000
13
+ private_constant :WINDOW
14
+
15
+ def initialize
16
+ @mutex = Mutex.new
17
+ @active_by_type = Hash.new(0)
18
+ @wait_ms = []
19
+ @run_ms = []
20
+ @cancelled = Hash.new(0)
21
+ @failed = Hash.new(0)
22
+ @starvation_count = 0
23
+ end
24
+
25
+ # Records that a new task of +type+ has been spawned.
26
+ # @param type [Symbol]
27
+ # @return [void]
28
+ # @api private
29
+ def record_start(type)
30
+ @mutex.synchronize { @active_by_type[type] += 1 }
31
+ end
32
+
33
+ # Appends a wait-time sample (milliseconds from spawn to start).
34
+ # @param wait_ms [Float]
35
+ # @return [void]
36
+ # @api private
37
+ def record_wait(wait_ms)
38
+ @mutex.synchronize do
39
+ @wait_ms << wait_ms
40
+ @wait_ms.shift if @wait_ms.size > WINDOW
41
+ end
42
+ end
43
+
44
+ # Records completion of a task (decrements active count, appends run time).
45
+ # @param type [Symbol]
46
+ # @param outcome [:completed, :cancelled, :failed]
47
+ # @param run_start_ms [Integer] monotonic millisecond timestamp from task start
48
+ # @return [void]
49
+ # @api private
50
+ def record_end(type, outcome, run_start_ms)
51
+ run_ms = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) - run_start_ms
52
+ @mutex.synchronize do
53
+ @active_by_type[type] = [@active_by_type[type] - 1, 0].max
54
+ @run_ms << run_ms
55
+ @run_ms.shift if @run_ms.size > WINDOW
56
+ case outcome
57
+ when :cancelled then @cancelled[type] += 1
58
+ when :failed then @failed[type] += 1
59
+ end
60
+ end
61
+ end
62
+
63
+ # Increments the CPU-starvation counter (task ran without yielding over
64
+ # the configured +blocking_detect_threshold_ms+ threshold).
65
+ # @return [void]
66
+ # @api private
67
+ def increment_starvation
68
+ @mutex.synchronize { @starvation_count += 1 }
69
+ end
70
+
71
+ # Returns the current starvation counter value.
72
+ # @return [Integer]
73
+ # @api private
74
+ def starvation_count
75
+ @mutex.synchronize { @starvation_count }
76
+ end
77
+
78
+ # Returns the full task-centric metrics hash (see {Runtime#task_snapshot}).
79
+ # @return [Hash{Symbol => Numeric}]
80
+ # @api private
81
+ def snapshot
82
+ @mutex.synchronize do
83
+ active = @active_by_type.dup
84
+ wait = @wait_ms.dup
85
+ run = @run_ms.dup
86
+ cancelled = @cancelled.values.sum
87
+ failed = @failed.values.sum
88
+ starvation = @starvation_count
89
+ {
90
+ active_agent_tasks: active[:agent].to_i,
91
+ active_tool_tasks: active[:tool].to_i,
92
+ active_workflow_tasks: active[:workflow].to_i,
93
+ active_rag_tasks: active[:rag].to_i,
94
+ active_llm_tasks: active[:llm].to_i,
95
+ task_wait_time_p50_ms: _percentile(wait, 50),
96
+ task_wait_time_p95_ms: _percentile(wait, 95),
97
+ task_run_time_p50_ms: _percentile(run, 50),
98
+ task_run_time_p95_ms: _percentile(run, 95),
99
+ cancelled_tasks: cancelled,
100
+ failed_tasks: failed,
101
+ non_yield_threshold_violation_count: starvation
102
+ }
103
+ end
104
+ end
105
+
106
+ private
107
+
108
+ def _percentile(samples, pct)
109
+ return 0.0 if samples.empty?
110
+
111
+ sorted = samples.sort
112
+ idx = ((pct / 100.0) * (sorted.size - 1)).round
113
+ sorted[idx].round(3)
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ class Runtime
5
+ # Abstract base class for Runtime scheduler backends.
6
+ #
7
+ # A scheduler is responsible for turning +Runtime#spawn+ calls into
8
+ # runnable {Task} objects. Concrete subclasses decide whether tasks
9
+ # execute on threads, Fibers, or some other execution primitive.
10
+ #
11
+ # The interface is intentionally minimal: callers only see {Task}
12
+ # objects and never interact with the scheduler directly.
13
+ class Scheduler
14
+ # Thread-local key under which the active scheduler is stored.
15
+ # Shared with {Task::FiberBackend} (same symbol).
16
+ # @api private
17
+ SCHEDULER_KEY = :phronomy_deterministic_scheduler
18
+
19
+ # Returns the scheduler currently dispatching on this OS thread, or +nil+
20
+ # when running outside a cooperative (Fiber-based) scheduler context.
21
+ #
22
+ # Uses +Thread#thread_variable_get+ (not +Thread#[]+) so that the value is
23
+ # visible across all Fibers running on the same OS thread.
24
+ #
25
+ # @return [Scheduler, nil]
26
+ # @api private
27
+ def self.current
28
+ Thread.current.thread_variable_get(SCHEDULER_KEY)
29
+ end
30
+
31
+ # Creates a new scheduler-aware signal object for this scheduler.
32
+ # Signalling primitives use this instead of +ConditionVariable+ when a
33
+ # cooperative scheduler is active.
34
+ #
35
+ # Default implementation raises +NotImplementedError+. Subclasses that
36
+ # support cooperative suspension (e.g. {DeterministicScheduler}) must
37
+ # override this.
38
+ #
39
+ # @return [Object] an opaque signal handle understood by {#wait_for_signal}
40
+ # and {#raise_signal}
41
+ # @api private
42
+ def new_signal
43
+ raise NotImplementedError, "#{self.class}#new_signal is not implemented"
44
+ end
45
+
46
+ # Suspends the current execution unit (Fiber or Thread) until +signal+ is
47
+ # raised via {#raise_signal}.
48
+ #
49
+ # @param signal [Object] signal handle returned by {#new_signal}
50
+ # @return [void]
51
+ # @api private
52
+ def wait_for_signal(signal)
53
+ raise NotImplementedError, "#{self.class}#wait_for_signal is not implemented"
54
+ end
55
+
56
+ # Wakes up one waiter suspended on +signal+.
57
+ #
58
+ # @param signal [Object] signal handle returned by {#new_signal}
59
+ # @return [void]
60
+ # @api private
61
+ def raise_signal(signal)
62
+ raise NotImplementedError, "#{self.class}#raise_signal is not implemented"
63
+ end
64
+
65
+ # Wakes up all waiters suspended on +signal+.
66
+ #
67
+ # @param signal [Object] signal handle returned by {#new_signal}
68
+ # @return [void]
69
+ # @api private
70
+ def raise_signal_all(signal)
71
+ raise NotImplementedError, "#{self.class}#raise_signal_all is not implemented"
72
+ end
73
+
74
+ # Spawns a new task.
75
+ #
76
+ # @param name [String, nil] optional human-readable label
77
+ # @param parent [Task, nil] parent task for cancellation propagation
78
+ # @yield block to execute concurrently (or synchronously, depending on
79
+ # the concrete scheduler)
80
+ # @return [Task]
81
+ # @api private
82
+ def spawn(name:, parent:, &block)
83
+ raise NotImplementedError, "#{self.class}#spawn is not implemented"
84
+ end
85
+
86
+ # Cooperative yield point.
87
+ #
88
+ # Default implementation is a no-op. Thread-based subclasses should
89
+ # override with +Thread.pass+; fiber-based subclasses should switch to the
90
+ # next runnable fiber.
91
+ # @return [void]
92
+ # @api private
93
+ def yield
94
+ # no-op by default; subclasses override
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ class Runtime
5
+ # A drop-in replacement for {TimerQueue} that delegates timer scheduling to
6
+ # a {DeterministicScheduler} instead of spawning a dedicated background OS thread.
7
+ #
8
+ # When a {Runtime} is backed by {DeterministicScheduler} (e.g. the +:fiber+
9
+ # runtime backend), {Runtime#timer_queue} returns an instance of this adapter
10
+ # rather than a {TimerQueue}. This eliminates the +phronomy-timer-queue+
11
+ # background thread for Fiber-based runtimes.
12
+ #
13
+ # Timer callbacks are fired during {DeterministicScheduler#run_until_idle}
14
+ # when {DeterministicScheduler#autorun?} is +true+ (i.e. the +:fiber+ backend).
15
+ # They can also be fired explicitly by calling
16
+ # {DeterministicScheduler#fire_real_timers}.
17
+ #
18
+ # == Known Limitation (Issue #331)
19
+ #
20
+ # Timers that require an actual wall-clock sleep (e.g. a deadline of 10 s
21
+ # from now that will not be reached until real time elapses) will not fire
22
+ # automatically: +run_until_idle+ does not block waiting for future deadlines.
23
+ # This is an accepted limitation of the current stepping-stone implementation.
24
+ # Full resolution requires integrating the cooperative scheduler with the
25
+ # {EventLoop} tick cycle so that a single event-loop iteration checks both
26
+ # ready Fibers and expired wall-clock timers.
27
+ #
28
+ # Use the +:thread+ runtime backend (default) for production workloads that
29
+ # depend on real-time deadline enforcement.
30
+ #
31
+ # @see DeterministicScheduler#schedule_real_after
32
+ # @see DeterministicScheduler#fire_real_timers
33
+ # Bridges wall-clock timers to the cooperative {DeterministicScheduler}.
34
+ #
35
+ # Registers a recurring timer callback with the scheduler's {TimerQueue}
36
+ # so that Fiber-based tasks can await real time without blocking OS threads.
37
+ # @api private
38
+ class SchedulerTimerAdapter
39
+ # @param scheduler [DeterministicScheduler]
40
+ # @api private
41
+ def initialize(scheduler)
42
+ @scheduler = scheduler
43
+ end
44
+
45
+ # Schedules a one-shot callback to fire after +seconds+ from now.
46
+ # Delegates to {DeterministicScheduler#schedule_real_after}.
47
+ #
48
+ # Raises {PoolShutdownError} after {#shutdown} has been called, matching
49
+ # the behaviour of {TimerQueue#schedule}.
50
+ #
51
+ # @param seconds [Numeric] delay before the callback fires
52
+ # @yield called when the deadline is reached
53
+ # @return [self]
54
+ # @api private
55
+ def schedule(seconds:, &callback)
56
+ raise Phronomy::PoolShutdownError, "SchedulerTimerAdapter has been shut down" if @stopped
57
+
58
+ @scheduler.schedule_real_after(seconds, &callback)
59
+ self
60
+ end
61
+
62
+ # No-op: there is no background thread to stop.
63
+ # Present for API compatibility with {TimerQueue}.
64
+ # @return [self]
65
+ # @api private
66
+ def shutdown
67
+ @stopped = true
68
+ self
69
+ end
70
+
71
+ # Returns the number of pending (not yet fired) callbacks.
72
+ # @return [Integer]
73
+ # @api private
74
+ def pending_count
75
+ @scheduler.pending_real_timer_count
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ class Runtime
5
+ # Internal registry of active {Task} instances for a {Runtime}.
6
+ #
7
+ # Tracks every task that has been spawned but not yet completed so that
8
+ # {Runtime#shutdown} can drain them. Tasks that complete synchronously
9
+ # (e.g. under {FakeScheduler} / {ImmediateBackend}) deregister themselves
10
+ # before the caller returns from {Runtime#spawn}, so they are never added
11
+ # to the registry in the first place.
12
+ # @api private
13
+ class TaskRegistry
14
+ def initialize
15
+ @mutex = Mutex.new
16
+ @tasks = []
17
+ end
18
+
19
+ # Adds +task+ to the registry unless it already completed synchronously.
20
+ # @param task [Task]
21
+ # @return [void]
22
+ # @api private
23
+ def register(task)
24
+ @mutex.synchronize { @tasks << task unless task.done? }
25
+ end
26
+
27
+ # Removes +task+ from the registry (called from the task's ensure block).
28
+ # @param task [Task]
29
+ # @return [void]
30
+ # @api private
31
+ def deregister(task)
32
+ @mutex.synchronize { @tasks.delete(task) }
33
+ end
34
+
35
+ # Waits for all registered tasks to finish (used by {Runtime#shutdown}).
36
+ # @return [void]
37
+ # @api private
38
+ def drain
39
+ tasks = @mutex.synchronize { @tasks.dup }
40
+ tasks.each do |t|
41
+ t.join
42
+ rescue
43
+ nil
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ class Runtime
5
+ # Thread-based scheduler: spawns each task in a new OS thread.
6
+ #
7
+ # This is the default scheduler used by {Runtime} in production.
8
+ # It delegates directly to {Task.spawn} with the default
9
+ # {Task::ThreadBackend}, preserving the pre-282 behaviour exactly.
10
+ # @api private
11
+ class ThreadScheduler < Scheduler
12
+ # Spawns +block+ as a new {Task} backed by a Thread.
13
+ #
14
+ # @param name [String, nil]
15
+ # @param parent [Task, nil]
16
+ # @return [Task]
17
+ # @api private
18
+ def spawn(name:, parent:, &block)
19
+ Task.spawn(name: name, parent: parent, &block)
20
+ end
21
+
22
+ # Yields the current thread's time slice to other runnable threads.
23
+ # @return [void]
24
+ # @api private
25
+ def yield
26
+ Thread.pass
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ class Runtime
5
+ # A thread-safe timer queue backed by a single background thread.
6
+ #
7
+ # Replaces the pattern of spawning one +Thread.new { sleep(t); callback }+
8
+ # per deadline. Any number of timers share a single background thread that
9
+ # sleeps until the earliest pending deadline.
10
+ #
11
+ # Use {#schedule} to register a one-shot callback; call {#shutdown} when the
12
+ # queue is no longer needed (e.g. on process exit) to stop the background
13
+ # thread cleanly.
14
+ class TimerQueue
15
+ # @param clock [#call] zero-argument callable that returns the current
16
+ # monotonic time in seconds (defaults to +Process::CLOCK_MONOTONIC+).
17
+ # Override in tests to inject a fake clock.
18
+ # @api private
19
+ def initialize(clock: -> { Process.clock_gettime(Process::CLOCK_MONOTONIC) })
20
+ @clock = clock
21
+ @heap = [] # [[fire_at, callback], ...]
22
+ @mutex = Mutex.new
23
+ @cond = ConditionVariable.new
24
+ @stopped = false
25
+ @thread = Thread.new { run_loop }
26
+ @thread.name = "phronomy-timer-queue"
27
+ end
28
+
29
+ # Schedule a one-shot callback to fire after +seconds+ from now.
30
+ #
31
+ # @param seconds [Numeric] delay before the callback fires
32
+ # @yield called (in the timer thread) when the deadline is reached
33
+ # @return [self]
34
+ # @api private
35
+ def schedule(seconds:, &callback)
36
+ fire_at = @clock.call + seconds.to_f
37
+ @mutex.synchronize do
38
+ raise Phronomy::PoolShutdownError, "TimerQueue has been shut down" if @stopped
39
+ insert_sorted(fire_at, callback)
40
+ @cond.signal
41
+ end
42
+ self
43
+ end
44
+
45
+ # Stop the background thread. Pending (un-fired) callbacks are discarded.
46
+ #
47
+ # @return [self]
48
+ # @api private
49
+ def shutdown
50
+ @mutex.synchronize do
51
+ @stopped = true
52
+ @cond.signal
53
+ end
54
+ @thread.join
55
+ self
56
+ end
57
+
58
+ # Number of pending (not yet fired) callbacks. Primarily for testing.
59
+ # @return [Integer]
60
+ # @api private
61
+ def pending_count
62
+ @mutex.synchronize { @heap.size }
63
+ end
64
+
65
+ private
66
+
67
+ def insert_sorted(fire_at, callback)
68
+ @heap << [fire_at, callback]
69
+ @heap.sort_by! { |(t, _)| t }
70
+ end
71
+
72
+ def run_loop
73
+ loop do
74
+ callback = next_callback
75
+ break if callback == :stopped
76
+ begin
77
+ callback&.call
78
+ rescue => e
79
+ Phronomy.configuration.logger&.error { "[TimerQueue] callback raised #{e.class}: #{e.message}" }
80
+ end
81
+ end
82
+ end
83
+
84
+ def next_callback
85
+ @mutex.synchronize do
86
+ loop do
87
+ return :stopped if @stopped
88
+
89
+ if @heap.empty?
90
+ @cond.wait(@mutex)
91
+ else
92
+ now = @clock.call
93
+ fire_at, = @heap.first
94
+ if fire_at <= now
95
+ return @heap.shift[1]
96
+ else
97
+ remaining = fire_at - now
98
+ @cond.wait(@mutex, remaining)
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phronomy
4
+ class Runtime
5
+ # Lazy-initialised timer service for a {Runtime} instance.
6
+ #
7
+ # Returns a {SchedulerTimerAdapter} when the backing scheduler is a
8
+ # {DeterministicScheduler} (enabling virtual-time integration for the
9
+ # `:fiber` backend), or a standard {TimerQueue} (OS-thread backed) for all
10
+ # other schedulers.
11
+ # @api private
12
+ class TimerService
13
+ # @param scheduler [Scheduler]
14
+ # @api private
15
+ def initialize(scheduler)
16
+ @scheduler = scheduler
17
+ @mutex = Mutex.new
18
+ @timer = nil
19
+ end
20
+
21
+ # Returns (or lazily creates) the timer queue for this runtime.
22
+ # @return [TimerQueue, SchedulerTimerAdapter]
23
+ # @api private
24
+ def timer_queue
25
+ @mutex.synchronize do
26
+ @timer ||= if @scheduler.is_a?(DeterministicScheduler)
27
+ SchedulerTimerAdapter.new(@scheduler)
28
+ else
29
+ TimerQueue.new
30
+ end
31
+ end
32
+ end
33
+
34
+ # Shuts down the timer queue if it was started.
35
+ # @return [void]
36
+ # @api private
37
+ def shutdown
38
+ @mutex.synchronize { @timer&.shutdown }
39
+ end
40
+ end
41
+ end
42
+ end