robot_lab 0.2.6 → 0.2.7
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.
- checksums.yaml +4 -4
- data/README.md +124 -64
- data/docs/api/core/index.md +41 -15
- data/docs/api/core/memory.md +247 -29
- data/docs/api/core/network.md +255 -33
- data/docs/api/core/result.md +120 -32
- data/docs/api/core/robot.md +551 -61
- data/docs/api/core/state.md +87 -197
- data/docs/api/core/tool.md +165 -20
- data/docs/api/errors.md +110 -17
- data/docs/api/hooks.md +469 -0
- data/docs/api/index.md +80 -7
- data/docs/api/mcp/client.md +129 -35
- data/docs/api/mcp/index.md +164 -23
- data/docs/api/mcp/server.md +27 -3
- data/docs/api/mcp/transports.md +94 -22
- data/docs/api/messages/index.md +26 -3
- data/docs/api/messages/text-message.md +33 -11
- data/docs/api/messages/tool-call-message.md +27 -4
- data/docs/api/messages/tool-result-message.md +23 -4
- data/docs/api/messages/user-message.md +45 -8
- data/docs/api/skills.md +519 -0
- data/docs/api/streaming/context.md +28 -5
- data/docs/api/streaming/index.md +57 -11
- data/docs/api/support.md +846 -0
- data/docs/architecture/core-concepts.md +79 -31
- data/docs/architecture/index.md +86 -11
- data/docs/architecture/message-flow.md +66 -29
- data/docs/architecture/network-orchestration.md +145 -38
- data/docs/architecture/robot-execution.md +172 -90
- data/docs/architecture/state-management.md +31 -12
- data/docs/concepts.md +176 -21
- data/docs/examples/basic-chat.md +72 -19
- data/docs/examples/index.md +117 -31
- data/docs/examples/mcp-server.md +154 -45
- data/docs/examples/multi-robot-network.md +91 -21
- data/docs/examples/tool-usage.md +104 -37
- data/docs/getting-started/configuration.md +284 -97
- data/docs/getting-started/installation.md +53 -41
- data/docs/getting-started/quick-start.md +51 -6
- data/docs/guides/building-robots.md +258 -50
- data/docs/guides/creating-networks.md +214 -30
- data/docs/guides/hooks.md +141 -54
- data/docs/guides/knowledge.md +35 -4
- data/docs/guides/mcp-integration.md +211 -44
- data/docs/guides/memory.md +103 -12
- data/docs/guides/observability.md +95 -47
- data/docs/guides/streaming.md +184 -125
- data/docs/guides/using-tools.md +237 -17
- data/docs/index.md +36 -4
- data/examples/01_simple_robot.rb +2 -2
- data/examples/02_tools.rb +14 -4
- data/examples/03_network.rb +12 -7
- data/examples/04_mcp.rb +11 -4
- data/examples/05_streaming.rb +8 -5
- data/examples/06_prompt_templates.rb +13 -9
- data/examples/07_network_memory.rb +5 -5
- data/examples/08_llm_config.rb +20 -15
- data/examples/09_chaining.rb +4 -4
- data/examples/11_network_introspection.rb +4 -4
- data/examples/12_message_bus.rb +2 -2
- data/examples/13_spawn.rb +2 -2
- data/examples/14_rusty_circuit/README.md +1 -0
- data/examples/14_rusty_circuit/comic.rb +7 -3
- data/examples/14_rusty_circuit/display.rb +14 -0
- data/examples/14_rusty_circuit/heckler.rb +8 -6
- data/examples/14_rusty_circuit/open_mic.rb +17 -6
- data/examples/14_rusty_circuit/scout.rb +17 -10
- data/examples/15_memory_network_and_bus/editorial_pipeline.rb +14 -10
- data/examples/15_memory_network_and_bus/linux_writer.rb +2 -2
- data/examples/15_memory_network_and_bus/os_editor.rb +3 -1
- data/examples/15_memory_network_and_bus/os_writer.rb +4 -1
- data/examples/16_writers_room/writer.rb +22 -22
- data/examples/16_writers_room/writers_room.rb +2 -0
- data/examples/17_skills.rb +14 -13
- data/examples/18_rails/README.md +20 -1
- data/examples/18_rails/app/controllers/chat_controller.rb +5 -1
- data/examples/18_rails/app/jobs/robot_run_job.rb +11 -5
- data/examples/18_rails/app/robots/chat_robot.rb +11 -0
- data/examples/18_rails/config/initializers/robot_lab.rb +8 -0
- data/examples/19_token_tracking.rb +25 -9
- data/examples/20_circuit_breaker.rb +10 -7
- data/examples/21_learning_loop.rb +42 -16
- data/examples/22_context_compression.rb +23 -23
- data/examples/23_convergence.rb +24 -17
- data/examples/24_structured_delegation.rb +13 -8
- data/examples/25_history_search.rb +12 -8
- data/examples/27_incident_response/incident_response.rb +31 -13
- data/examples/28_mcp_discovery.rb +17 -13
- data/examples/29_ractor_tools.rb +4 -2
- data/examples/30_ractor_network.rb +22 -17
- data/examples/31_launch_assessment.rb +20 -9
- data/examples/32_newsletter_reader.rb +7 -2
- data/examples/33_stock_predictor.rb +34 -13
- data/examples/34_agentskills.rb +7 -3
- data/examples/35_hooks.rb +18 -8
- data/examples/README.md +199 -45
- data/examples/common.rb +79 -11
- data/examples/xyzzy.rb +8 -1
- data/lib/robot_lab/config.rb +10 -5
- data/lib/robot_lab/names.rb +402 -0
- data/lib/robot_lab/robot/agent_skill_matching.rb +1 -3
- data/lib/robot_lab/robot/bus_messaging.rb +16 -8
- data/lib/robot_lab/robot/template_rendering.rb +16 -3
- data/lib/robot_lab/robot.rb +23 -2
- data/lib/robot_lab/version.rb +1 -1
- data/lib/robot_lab.rb +21 -15
- data/mkdocs.yml +6 -1
- metadata +7 -2
data/docs/api/errors.md
CHANGED
|
@@ -16,9 +16,13 @@ StandardError
|
|
|
16
16
|
├── RobotLab::BusError
|
|
17
17
|
├── RobotLab::RactorBoundaryError
|
|
18
18
|
├── RobotLab::ToolError
|
|
19
|
-
|
|
19
|
+
├── RobotLab::BudgetExceeded
|
|
20
|
+
└── RobotLab::AwaitTimeout
|
|
20
21
|
```
|
|
21
22
|
|
|
23
|
+
`AwaitTimeout` is defined in `lib/robot_lab/memory.rb` and raised by
|
|
24
|
+
[`Memory#get`](core/memory.md#get) when a blocking read expires.
|
|
25
|
+
|
|
22
26
|
Additionally, `DelegationFuture` defines its own scoped error:
|
|
23
27
|
|
|
24
28
|
```
|
|
@@ -49,6 +53,8 @@ Raised when configuration is invalid or missing required values.
|
|
|
49
53
|
|
|
50
54
|
```ruby
|
|
51
55
|
# Example: missing API key, invalid template path
|
|
56
|
+
begin
|
|
57
|
+
robot.run("hello")
|
|
52
58
|
rescue RobotLab::ConfigurationError => e
|
|
53
59
|
puts "Bad config: #{e.message}"
|
|
54
60
|
end
|
|
@@ -58,15 +64,27 @@ end
|
|
|
58
64
|
|
|
59
65
|
## RobotLab::DependencyError < ConfigurationError
|
|
60
66
|
|
|
61
|
-
Raised when a required optional gem dependency is not installed.
|
|
67
|
+
Raised when a required optional gem dependency is not installed. There are three
|
|
68
|
+
distinct raise sites in core, each with its own message:
|
|
69
|
+
|
|
70
|
+
| Raised by | Missing dependency | Message |
|
|
71
|
+
|-----------|--------------------|---------|
|
|
72
|
+
| `TextAnalysis.require_classifier!` — reached from `Convergence`, `HistoryCompressor`, `Robot#compress_history`, `Robot#search_history` | `classifier` gem | `The 'classifier' gem is required for text analysis features. Add it to your Gemfile: gem 'classifier', '~> 2.3'` |
|
|
73
|
+
| `Memory`'s document-store methods (`store_document`, `search_documents`, `document_keys`, `delete_document`) | `robot_lab-document_store` gem | `document storage requires the robot_lab-document_store gem. Add \`gem 'robot_lab-document_store'\` to your Gemfile.` |
|
|
74
|
+
| `Network#run` under `parallel_mode: :ractor` | `robot_lab-ractor` gem | `parallel_mode: :ractor requires the robot_lab-ractor gem. Add \`gem 'robot_lab-ractor'\` to your Gemfile.` |
|
|
62
75
|
|
|
63
76
|
```ruby
|
|
64
|
-
|
|
77
|
+
begin
|
|
78
|
+
robot.compress_history
|
|
65
79
|
rescue RobotLab::DependencyError => e
|
|
66
|
-
|
|
80
|
+
warn e.message
|
|
67
81
|
end
|
|
68
82
|
```
|
|
69
83
|
|
|
84
|
+
Two places swallow it rather than propagate: `auto_compact: :context_window`
|
|
85
|
+
logs the message at `:warn` and skips compaction, and
|
|
86
|
+
`MCP::ServerDiscovery` rescues it and falls back.
|
|
87
|
+
|
|
70
88
|
---
|
|
71
89
|
|
|
72
90
|
## RobotLab::InferenceError
|
|
@@ -74,6 +92,8 @@ end
|
|
|
74
92
|
Raised when LLM inference fails (API errors, timeouts, rate limits).
|
|
75
93
|
|
|
76
94
|
```ruby
|
|
95
|
+
begin
|
|
96
|
+
robot.run("hello")
|
|
77
97
|
rescue RobotLab::InferenceError => e
|
|
78
98
|
puts "LLM call failed: #{e.message}"
|
|
79
99
|
end
|
|
@@ -94,13 +114,22 @@ robot = RobotLab.build(
|
|
|
94
114
|
)
|
|
95
115
|
|
|
96
116
|
begin
|
|
97
|
-
robot.run("Run all steps.")
|
|
117
|
+
robot.run("Run all steps.", tools: :inherit)
|
|
98
118
|
rescue RobotLab::ToolLoopError => e
|
|
99
|
-
puts e.message
|
|
119
|
+
puts e.message
|
|
120
|
+
# "Circuit breaker triggered: 11 tool calls exceeded max_tool_rounds (10)"
|
|
100
121
|
robot.clear_messages # required before reuse
|
|
101
122
|
end
|
|
102
123
|
```
|
|
103
124
|
|
|
125
|
+
The message format is:
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
Circuit breaker triggered: <N> tool calls exceeded max_tool_rounds (<M>)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`N` is the call count that tripped the breaker (always `M + 1`); `M` is the configured limit.
|
|
132
|
+
|
|
104
133
|
---
|
|
105
134
|
|
|
106
135
|
## RobotLab::ToolNotFoundError
|
|
@@ -114,6 +143,8 @@ Raised when a tool name is referenced but cannot be found in the `ToolManifest`.
|
|
|
114
143
|
Raised when MCP server communication fails (connection refused, timeout, protocol error).
|
|
115
144
|
|
|
116
145
|
```ruby
|
|
146
|
+
begin
|
|
147
|
+
robot.run("hello", mcp: :inherit, tools: :inherit)
|
|
117
148
|
rescue RobotLab::MCPError => e
|
|
118
149
|
puts "MCP failed: #{e.message}"
|
|
119
150
|
end
|
|
@@ -128,6 +159,8 @@ end
|
|
|
128
159
|
Raised when message bus communication fails (no bus configured, channel not found).
|
|
129
160
|
|
|
130
161
|
```ruby
|
|
162
|
+
begin
|
|
163
|
+
robot.send_message(to: :bob, content: "hi")
|
|
131
164
|
rescue RobotLab::BusError => e
|
|
132
165
|
puts "Bus error: #{e.message}"
|
|
133
166
|
end
|
|
@@ -139,37 +172,65 @@ end
|
|
|
139
172
|
|
|
140
173
|
Raised when a value cannot be made Ractor-shareable before crossing a Ractor boundary (e.g., a live `IO` object, a `Proc`, or an object with mutable state).
|
|
141
174
|
|
|
175
|
+
!!! note "The error class is core; the machinery that raises it is not"
|
|
176
|
+
`RobotLab::RactorBoundaryError` is defined in core
|
|
177
|
+
(`lib/robot_lab/error.rb`) so hosts can rescue it without loading anything
|
|
178
|
+
extra. But `RactorBoundary`, `RactorWorkerPool`, and `RactorMemoryProxy` are
|
|
179
|
+
**not defined in core** — they ship in the separate **`robot_lab-ractor`**
|
|
180
|
+
gem. Core only *references* `RactorBoundary.freeze_deep` from
|
|
181
|
+
`Network#build_robot_spec`, which is reachable only under
|
|
182
|
+
`parallel_mode: :ractor`, which itself raises `DependencyError` unless that
|
|
183
|
+
gem is loaded.
|
|
184
|
+
|
|
142
185
|
```ruby
|
|
186
|
+
require "robot_lab"
|
|
187
|
+
require "robot_lab/ractor" # provides RactorBoundary and the worker pool
|
|
188
|
+
|
|
143
189
|
begin
|
|
144
190
|
RobotLab::RactorBoundary.freeze_deep({ io: StringIO.new })
|
|
145
191
|
rescue RobotLab::RactorBoundaryError => e
|
|
146
|
-
puts e.message
|
|
192
|
+
puts e.message
|
|
147
193
|
end
|
|
148
194
|
```
|
|
149
195
|
|
|
150
|
-
Raised proactively by `RactorWorkerPool#submit` and `RactorMemoryProxy#set` before any Ractor is involved.
|
|
151
|
-
|
|
152
196
|
---
|
|
153
197
|
|
|
154
198
|
## RobotLab::ToolError
|
|
155
199
|
|
|
156
|
-
|
|
200
|
+
An opt-in error type for tool failures. Core raises it nowhere itself — you raise
|
|
201
|
+
it from your own `execute`, and the `robot_lab-ractor` worker pool raises it when
|
|
202
|
+
unwrapping a failure that happened inside a Ractor.
|
|
203
|
+
|
|
204
|
+
`ToolError.new(message, retryable: true)` marks a specific instance as retryable.
|
|
205
|
+
|
|
206
|
+
When a tool raises inside `Tool#call`, the error is caught and returned to the
|
|
207
|
+
LLM as text rather than propagating (unless the class sets
|
|
208
|
+
`self.raise_on_error = true`). RobotLab appends `" (retryable)"` whenever
|
|
209
|
+
`RobotLab::Errors.retryable?(error)` is true, so the model itself can see that
|
|
210
|
+
another attempt is worth trying:
|
|
157
211
|
|
|
158
212
|
```ruby
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
213
|
+
class Fetch < RobotLab::Tool
|
|
214
|
+
description "Fetch a record"
|
|
215
|
+
param :id, type: "string", desc: "record id"
|
|
216
|
+
|
|
217
|
+
def execute(id:)
|
|
218
|
+
raise RobotLab::ToolError.new("upstream unavailable", retryable: true)
|
|
219
|
+
end
|
|
163
220
|
end
|
|
221
|
+
|
|
222
|
+
Fetch.new.call({ "id" => "1" })
|
|
223
|
+
# => "Error (fetch): upstream unavailable (retryable)"
|
|
164
224
|
```
|
|
165
225
|
|
|
166
|
-
|
|
226
|
+
Unlike the generic `StandardError` path, the `ToolError` path writes **nothing**
|
|
227
|
+
to the logger. See [Tool#call](core/tool.md#call).
|
|
167
228
|
|
|
168
229
|
---
|
|
169
230
|
|
|
170
231
|
## RobotLab::BudgetExceeded
|
|
171
232
|
|
|
172
|
-
Raised when a Robot's configured `token_budget` or `cost_budget` is already exhausted **before** an LLM call is attempted (see [Budgets](../guides/observability.md#budgets-token
|
|
233
|
+
Raised when a Robot's configured `token_budget` or `cost_budget` is already exhausted **before** an LLM call is attempted (see [Budgets](../guides/observability.md#budgets-token-cost) and `RobotLab::Budget::Ledger`). This is distinct from the pre-existing `InferenceError` "Token budget exceeded" message, which covers a call that *completed* but pushed cumulative usage over budget — `BudgetExceeded` means the call was refused outright, before any tokens were spent.
|
|
173
234
|
|
|
174
235
|
```ruby
|
|
175
236
|
robot = RobotLab.build(name: "capped", system_prompt: "...", cost_budget: 0.50)
|
|
@@ -181,6 +242,38 @@ rescue RobotLab::BudgetExceeded => e
|
|
|
181
242
|
end
|
|
182
243
|
```
|
|
183
244
|
|
|
245
|
+
Raised by `Budget::Ledger#reserve!`; the message format is
|
|
246
|
+
`"budget exceeded for <dimension>: <committed + amount> > <limit>"` where
|
|
247
|
+
`<dimension>` is `tokens` or `cost`.
|
|
248
|
+
|
|
249
|
+
The post-call `InferenceError` messages are formatted differently:
|
|
250
|
+
|
|
251
|
+
| Dimension | Message |
|
|
252
|
+
|-----------|---------|
|
|
253
|
+
| `token_budget` | `Token budget exceeded: <N> tokens used, budget is <M>` |
|
|
254
|
+
| `cost_budget` | `Cost budget exceeded: $0.523100 used, budget is $0.500000` (both values via `%.6f`) |
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
258
|
+
## RobotLab::AwaitTimeout
|
|
259
|
+
|
|
260
|
+
Raised by [`Memory#get`](core/memory.md#get) when a blocking read
|
|
261
|
+
(`wait: true` or `wait: <seconds>`) expires before the key is written.
|
|
262
|
+
|
|
263
|
+
```ruby
|
|
264
|
+
begin
|
|
265
|
+
memory.get(:sentiment, wait: 30)
|
|
266
|
+
rescue RobotLab::AwaitTimeout => e
|
|
267
|
+
puts e.message # "Timeout waiting for :sentiment after 30 seconds"
|
|
268
|
+
end
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
With multiple keys the timeout is applied **per missing key**, so
|
|
272
|
+
`get(:a, :b, wait: 30)` can block for up to 60 seconds before raising.
|
|
273
|
+
|
|
274
|
+
Not retryable per `RobotLab::Errors.retryable?` — it falls into the
|
|
275
|
+
"everything else" bucket.
|
|
276
|
+
|
|
184
277
|
---
|
|
185
278
|
|
|
186
279
|
## Retryable Errors
|
|
@@ -192,7 +285,7 @@ end
|
|
|
192
285
|
| `InferenceError` (and subclasses, except `ToolLoopError`) | Always |
|
|
193
286
|
| `ToolLoopError` | Never — it's a circuit breaker; retrying immediately re-triggers the same loop |
|
|
194
287
|
| `MCPError` / `ToolError` | Only when raised with `retryable: true` at the raise site |
|
|
195
|
-
| Everything else (`ConfigurationError`, `ToolNotFoundError`, `DependencyError`, `RactorBoundaryError`, `BusError`, `BudgetExceeded`, non-RobotLab errors) | Never |
|
|
288
|
+
| Everything else (`ConfigurationError`, `ToolNotFoundError`, `DependencyError`, `RactorBoundaryError`, `BusError`, `BudgetExceeded`, `AwaitTimeout`, `DelegationTimeout`, non-RobotLab errors) | Never |
|
|
196
289
|
|
|
197
290
|
```ruby
|
|
198
291
|
begin
|
data/docs/api/hooks.md
ADDED
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
# Hooks API
|
|
2
|
+
|
|
3
|
+
Class-level reference for the hook system. For the conceptual walkthrough —
|
|
4
|
+
families, firing order, registration levels, and worked examples — see the
|
|
5
|
+
[Hook System guide](../guides/hooks.md). This page documents the public methods
|
|
6
|
+
of the classes that implement it.
|
|
7
|
+
|
|
8
|
+
```mermaid
|
|
9
|
+
classDiagram
|
|
10
|
+
class Hook {
|
|
11
|
+
<<abstract>>
|
|
12
|
+
+namespace() Symbol
|
|
13
|
+
+call(hook_name, ctx, block) Object
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
class HookRegistry {
|
|
17
|
+
+on(handler_class, context) Registration
|
|
18
|
+
+registrations_for(hook_name) Array
|
|
19
|
+
+registrations() Array
|
|
20
|
+
+clear() Array
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class Registration {
|
|
24
|
+
<<Data>>
|
|
25
|
+
+handler_class Class
|
|
26
|
+
+context Hash
|
|
27
|
+
+namespace() Symbol
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
class Hooks {
|
|
31
|
+
<<module>>
|
|
32
|
+
+run(family, ctx, registries, per_run_hooks) Object
|
|
33
|
+
+call(hook_name, ctx, registries, per_run_hooks) void
|
|
34
|
+
+around(hook_name, ctx, registries, per_run_hooks) Object
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
class HookContext {
|
|
38
|
+
+event Symbol
|
|
39
|
+
+metadata ExtensionState
|
|
40
|
+
+ext(name) DotState
|
|
41
|
+
+local() DotState
|
|
42
|
+
+with_namespace(ns) Object
|
|
43
|
+
+to_h() Hash
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
HookContext <|-- RunHookContext
|
|
47
|
+
RunHookContext <|-- LlmGenerationHookContext
|
|
48
|
+
HookContext <|-- ToolCallHookContext
|
|
49
|
+
HookContext <|-- NetworkRunHookContext
|
|
50
|
+
HookContext <|-- TaskHookContext
|
|
51
|
+
HookContext <|-- LearnHookContext
|
|
52
|
+
HookContext <|-- CompactionHookContext
|
|
53
|
+
|
|
54
|
+
HookRegistry --> Registration : builds
|
|
55
|
+
Hooks --> HookRegistry : reads
|
|
56
|
+
Hooks --> HookContext : namespaces
|
|
57
|
+
Hook <|-- Narrator
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## RobotLab::Hook
|
|
63
|
+
|
|
64
|
+
Base class for hook handlers. Subclasses implement hook methods as **class**
|
|
65
|
+
methods; any method a subclass does not define is silently skipped when that hook
|
|
66
|
+
fires.
|
|
67
|
+
|
|
68
|
+
### namespace / namespace=
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
class TimerHook < RobotLab::Hook; end
|
|
72
|
+
TimerHook.namespace # => :timer_hook
|
|
73
|
+
|
|
74
|
+
class Tracer < RobotLab::Hook
|
|
75
|
+
self.namespace = :trace
|
|
76
|
+
end
|
|
77
|
+
Tracer.namespace # => :trace
|
|
78
|
+
|
|
79
|
+
RobotLab::Hook.namespace # => nil (the base class has none)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
The namespace isolates a handler's `ctx.local` scratchpad from every other
|
|
83
|
+
handler. It defaults to the snake_case form of the **last** segment of the class
|
|
84
|
+
name, so `MyExt::Tracer` becomes `:tracer`. `HookRegistry#on` raises
|
|
85
|
+
`ArgumentError` when `namespace` is `nil`, which is why the base `Hook` class
|
|
86
|
+
itself can never be registered.
|
|
87
|
+
|
|
88
|
+
`inherited` resets `@namespace` to `nil` on each subclass, so a namespace set on
|
|
89
|
+
a parent handler is **not** inherited by its subclasses — each re-derives its own.
|
|
90
|
+
|
|
91
|
+
### call
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
HandlerClass.call(hook_name, context) # point / before / after hook
|
|
95
|
+
HandlerClass.call(hook_name, context) { ... } # around hook
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Dispatch one hook to this handler. This is the method `Hooks.call_registration`
|
|
99
|
+
invokes; you rarely call it directly.
|
|
100
|
+
|
|
101
|
+
| Case | Behavior |
|
|
102
|
+
|------|----------|
|
|
103
|
+
| Handler defines `hook_name` | Called with `context` (and the block, for around hooks) |
|
|
104
|
+
| Handler does not define it, block given | The block is called directly — **around passthrough**, so the chain never breaks |
|
|
105
|
+
| Handler does not define it, no block | Silent no-op |
|
|
106
|
+
|
|
107
|
+
The check is `singleton_class.public_method_defined?(hook_name)`, so a hook
|
|
108
|
+
defined as a *private* class method never fires.
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## RobotLab::HookRegistry
|
|
113
|
+
|
|
114
|
+
The store behind `RobotLab.hooks`, `network.hooks`, and `robot.hooks`. Each of
|
|
115
|
+
those three is a separate instance; a run consults all three.
|
|
116
|
+
|
|
117
|
+
### on
|
|
118
|
+
|
|
119
|
+
```ruby
|
|
120
|
+
registration = registry.on(HandlerClass, context: nil)
|
|
121
|
+
# => RobotLab::HookRegistry::Registration
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Register a handler class.
|
|
125
|
+
|
|
126
|
+
| Name | Type | Default | Description |
|
|
127
|
+
|------|------|---------|-------------|
|
|
128
|
+
| `handler_class` | `Class` | **required** | Must be a subclass of `RobotLab::Hook` |
|
|
129
|
+
| `context` | `Hash`, `nil` | `nil` | Defaults merged into the handler's `ctx.local` before each callback (via `DotState#merge_defaults`, so an already-set key is not overwritten) |
|
|
130
|
+
|
|
131
|
+
**Raises `ArgumentError`** when `handler_class` is not a `RobotLab::Hook`
|
|
132
|
+
subclass, or when its `namespace` is `nil`.
|
|
133
|
+
|
|
134
|
+
`RobotLab.on`, `network.on`, and `robot.on` are thin delegators to this method.
|
|
135
|
+
|
|
136
|
+
### registrations_for
|
|
137
|
+
|
|
138
|
+
```ruby
|
|
139
|
+
registry.registrations_for(:before_run) # => Array<Registration>
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Only the registrations whose handler class actually implements `hook_name`. This
|
|
143
|
+
is the filter that makes an unimplemented hook a no-op rather than an error, and
|
|
144
|
+
it is what `Hooks.registrations` calls on each registry.
|
|
145
|
+
|
|
146
|
+
### registrations
|
|
147
|
+
|
|
148
|
+
```ruby
|
|
149
|
+
registry.registrations # => Array<Registration>
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Every registration, as a defensive copy (`@registrations.dup`) — mutating the
|
|
153
|
+
returned array does not affect the registry.
|
|
154
|
+
|
|
155
|
+
Used by `Network#ractor_hook_classes_for` to collect the handler classes that
|
|
156
|
+
must cross a Ractor boundary under `parallel_mode: :ractor`.
|
|
157
|
+
|
|
158
|
+
### clear
|
|
159
|
+
|
|
160
|
+
```ruby
|
|
161
|
+
registry.clear # => the emptied internal array
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Remove every registration **in place**. Note the difference from
|
|
165
|
+
`RobotLab.clear_hooks!`, which replaces the global registry object entirely
|
|
166
|
+
rather than emptying it.
|
|
167
|
+
|
|
168
|
+
### HookRegistry::Registration
|
|
169
|
+
|
|
170
|
+
A `Data` value object pairing a handler with its optional context.
|
|
171
|
+
|
|
172
|
+
| Member / method | Type | Description |
|
|
173
|
+
|-----------------|------|-------------|
|
|
174
|
+
| `handler_class` | `Class` | The `RobotLab::Hook` subclass |
|
|
175
|
+
| `context` | `Hash`, `nil` | Per-registration defaults (defaults to `nil`) |
|
|
176
|
+
| `namespace` | `Symbol` | Delegates to `handler_class.namespace` |
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## RobotLab::Hooks
|
|
181
|
+
|
|
182
|
+
The dispatcher (`module_function`, so every method is called as
|
|
183
|
+
`RobotLab::Hooks.foo`). Core calls into it; extension authors normally write
|
|
184
|
+
handler classes instead. It is documented here because a host that drives
|
|
185
|
+
RobotLab objects directly — or an extension that adds its own hook family — needs
|
|
186
|
+
the contract.
|
|
187
|
+
|
|
188
|
+
### run
|
|
189
|
+
|
|
190
|
+
```ruby
|
|
191
|
+
RobotLab::Hooks.run(family, context, registries:, per_run_hooks: nil) { core_work }
|
|
192
|
+
# => the block's return value
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Execute a full hook family around a block. In order: every `before_<family>`,
|
|
196
|
+
then the `around_<family>` chain wrapping the block, then
|
|
197
|
+
[`set_result`](#set_result), then every `after_<family>`.
|
|
198
|
+
|
|
199
|
+
| Name | Type | Description |
|
|
200
|
+
|------|------|-------------|
|
|
201
|
+
| `family` | `Symbol` | `:run`, `:llm_generation`, `:tool_call`, `:network_run`, `:task`, `:compaction`, `:learn` |
|
|
202
|
+
| `context` | `HookContext` | The family's context object |
|
|
203
|
+
| `registries` | `Array<HookRegistry, nil>` | Consulted in order; `nil` entries are compacted away |
|
|
204
|
+
| `per_run_hooks` | `Class`, `Array<Class>`, `nil` | Handler classes active only for this call (`robot.run(..., hooks:)`) |
|
|
205
|
+
|
|
206
|
+
**Error path:** `run` rescues `Exception` (not just `StandardError`, so
|
|
207
|
+
`Timeout::Error` and `SignalException` are covered), assigns it to
|
|
208
|
+
`context.error` when the context has an `error=` writer, fires `on_error`, and
|
|
209
|
+
**re-raises**. Hooks never swallow an exception.
|
|
210
|
+
|
|
211
|
+
### call
|
|
212
|
+
|
|
213
|
+
```ruby
|
|
214
|
+
RobotLab::Hooks.call(hook_name, context, registries:, per_run_hooks: nil)
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
Fire one non-around hook across all matching registrations. This is how the point
|
|
218
|
+
hooks `:on_compaction` and `:on_learn` are dispatched from inside the core block
|
|
219
|
+
of their family — which is why they do not compose with `around_*` handlers and
|
|
220
|
+
have no chainable result.
|
|
221
|
+
|
|
222
|
+
### around
|
|
223
|
+
|
|
224
|
+
```ruby
|
|
225
|
+
RobotLab::Hooks.around(hook_name, context, registries:, per_run_hooks: nil) { ... }
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
Build and invoke just the around chain for one hook name, without the
|
|
229
|
+
before/after phases.
|
|
230
|
+
|
|
231
|
+
### registrations
|
|
232
|
+
|
|
233
|
+
```ruby
|
|
234
|
+
RobotLab::Hooks.registrations(hook_name, registries, per_run_hooks)
|
|
235
|
+
# => Array<Registration>
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
The complete ordered list for one hook name: each registry's
|
|
239
|
+
`registrations_for(hook_name)` concatenated in registry order, followed by the
|
|
240
|
+
per-run entries. This ordering is what produces **global → network → robot →
|
|
241
|
+
per-run** execution.
|
|
242
|
+
|
|
243
|
+
### error_registrations
|
|
244
|
+
|
|
245
|
+
```ruby
|
|
246
|
+
RobotLab::Hooks.error_registrations(family, registries, per_run_hooks)
|
|
247
|
+
# => Array<Registration>
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
`on_error` registrations, but **only** for the `:run`, `:network_run`, and
|
|
251
|
+
`:task` families — every other family returns `[]`. This is the mechanism behind
|
|
252
|
+
the guide's statement that `:llm_generation`, `:tool_call`, `:compaction`, and
|
|
253
|
+
`:learn` have no `on_error` variant.
|
|
254
|
+
|
|
255
|
+
### call_all
|
|
256
|
+
|
|
257
|
+
```ruby
|
|
258
|
+
RobotLab::Hooks.call_all(registrations, hook_name, context)
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Invoke each registration in order for a non-around hook.
|
|
262
|
+
|
|
263
|
+
### call_around
|
|
264
|
+
|
|
265
|
+
```ruby
|
|
266
|
+
RobotLab::Hooks.call_around(registrations, hook_name, context) { core_work }
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
Fold the registrations into a nested chain and call it. The list is `reverse`d
|
|
270
|
+
before folding, so the **first** registration ends up outermost — a global
|
|
271
|
+
`around_run` wraps a robot-level one, not the other way round. A handler that
|
|
272
|
+
does not implement the hook passes the block straight through.
|
|
273
|
+
|
|
274
|
+
### call_registration
|
|
275
|
+
|
|
276
|
+
```ruby
|
|
277
|
+
RobotLab::Hooks.call_registration(registration, hook_name, context, &block)
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Invoke one registration with its namespace active. It wraps the call in
|
|
281
|
+
`context.with_namespace(registration.namespace)` and, when the registration
|
|
282
|
+
carries a `context:` hash, merges those values into
|
|
283
|
+
`context.ext(namespace)` as defaults first. This is what makes `ctx.local`
|
|
284
|
+
resolve to the *calling handler's* private `DotState`.
|
|
285
|
+
|
|
286
|
+
### per_run_entries
|
|
287
|
+
|
|
288
|
+
```ruby
|
|
289
|
+
RobotLab::Hooks.per_run_entries(hook_name, hooks)
|
|
290
|
+
# => Array<Registration>
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
Turn `robot.run(msg, hooks: HandlerClass)` or `hooks: [A, B]` into
|
|
294
|
+
`Registration`s, skipping any class that does not implement `hook_name`. Per-run
|
|
295
|
+
registrations carry no `context:`.
|
|
296
|
+
|
|
297
|
+
### set_result
|
|
298
|
+
|
|
299
|
+
```ruby
|
|
300
|
+
RobotLab::Hooks.set_result(context, family, result)
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
Publish a family's return value onto its context so `after_*` handlers can read
|
|
304
|
+
it. The mapping is fixed:
|
|
305
|
+
|
|
306
|
+
| Family | Assigned to |
|
|
307
|
+
|--------|-------------|
|
|
308
|
+
| `:run` | `context.response` |
|
|
309
|
+
| `:network_run`, `:task` | `context.result` |
|
|
310
|
+
| `:llm_generation` | `context.generation_response` |
|
|
311
|
+
| `:tool_call`, `:compaction`, `:learn` | *(nothing — those contexts carry their result in a family-specific accessor set by the core block)* |
|
|
312
|
+
|
|
313
|
+
Each assignment is guarded by `respond_to?`, so a custom context lacking the
|
|
314
|
+
writer is simply skipped.
|
|
315
|
+
|
|
316
|
+
---
|
|
317
|
+
|
|
318
|
+
## RobotLab::HookContext
|
|
319
|
+
|
|
320
|
+
Base class for every hook context. The family-specific subclasses below add the
|
|
321
|
+
accessors a handler actually reads.
|
|
322
|
+
|
|
323
|
+
| Method | Returns | Description |
|
|
324
|
+
|--------|---------|-------------|
|
|
325
|
+
| `event` | `Symbol` | The family (`:run`, `:tool_call`, …), symbolized at construction |
|
|
326
|
+
| `metadata` | `ExtensionState` | The per-run namespace store; defaults to a fresh `ExtensionState` |
|
|
327
|
+
| `ext(name)` | `DotState` | Another handler's namespaced state — `ctx.ext(:timer).start_time` |
|
|
328
|
+
| `local` | `DotState` | **This** handler's state. Raises `ArgumentError: No hook namespace active` outside a `with_namespace` block |
|
|
329
|
+
| `with_namespace(ns) { \|ctx\| }` | block's value | Sets the active namespace for the duration of the block and restores the previous one in an `ensure`. Called by `Hooks.call_registration` |
|
|
330
|
+
| `to_h` | `Hash` | Snapshot of every public reader (excluding `to_h` and `local`) plus `metadata: metadata.to_h` |
|
|
331
|
+
|
|
332
|
+
!!! warning "`ctx.local` lives for exactly one run"
|
|
333
|
+
`metadata` is a fresh `ExtensionState` per `HookContext`, and a new context is
|
|
334
|
+
built per run. Use a class-level accessor on the handler for cross-run state.
|
|
335
|
+
|
|
336
|
+
### Context subclasses
|
|
337
|
+
|
|
338
|
+
Readers are read-only unless marked **rw**.
|
|
339
|
+
|
|
340
|
+
#### RunHookContext (`event: :run`)
|
|
341
|
+
|
|
342
|
+
| Accessor | | Description |
|
|
343
|
+
|----------|--|-------------|
|
|
344
|
+
| `robot` | | The robot being run |
|
|
345
|
+
| `network` | | Owning `Network`, or `nil` when standalone |
|
|
346
|
+
| `task` | | The `Task` wrapper, when run from a pipeline |
|
|
347
|
+
| `memory` | | The resolved run memory |
|
|
348
|
+
| `config` | | The robot's effective `RunConfig` |
|
|
349
|
+
| `request` | rw | The user message. **Writable** — assign in `before_run` to rewrite the prompt |
|
|
350
|
+
| `response` | rw | The `RobotResult`; set by `Hooks.set_result` before `after_run` |
|
|
351
|
+
| `error` | rw | Set by `Hooks.run` before `on_error` |
|
|
352
|
+
|
|
353
|
+
#### LlmGenerationHookContext (`event: :run`) — subclass of `RunHookContext`
|
|
354
|
+
|
|
355
|
+
Adds `iteration` (Integer, defaults to `0`) and `generation_response` (**rw**,
|
|
356
|
+
set by `set_result`). Note it inherits `event: :run` from its parent; the family
|
|
357
|
+
name comes from the hook method names, not from `event`.
|
|
358
|
+
|
|
359
|
+
#### ToolCallHookContext (`event: :tool_call`)
|
|
360
|
+
|
|
361
|
+
| Accessor | | Description |
|
|
362
|
+
|----------|--|-------------|
|
|
363
|
+
| `tool` | | The tool instance |
|
|
364
|
+
| `tool_name` | | `tool.name`, falling back to `tool.class.name` |
|
|
365
|
+
| `tool_args` | | Arguments the LLM supplied |
|
|
366
|
+
| `robot` | | Owning robot, or `nil` |
|
|
367
|
+
| `tool_result` | rw | The result. Assign it in `around_tool_call` **without** calling the block to block a tool |
|
|
368
|
+
| `tool_error` | rw | Set by `Tool#call` when `execute` raised |
|
|
369
|
+
|
|
370
|
+
#### NetworkRunHookContext (`event: :network_run`)
|
|
371
|
+
|
|
372
|
+
`network`, `memory`, `config`; **rw**: `context` (the run params hash), `result`, `error`.
|
|
373
|
+
|
|
374
|
+
#### TaskHookContext (`event: :task`)
|
|
375
|
+
|
|
376
|
+
`network`, `task`, `task_name` (`task.name`), `robot` (falls back to
|
|
377
|
+
`task.robot`), `memory`, `config`; **rw**: `result`, `error`.
|
|
378
|
+
|
|
379
|
+
#### LearnHookContext (`event: :learn`)
|
|
380
|
+
|
|
381
|
+
| Accessor | | Description |
|
|
382
|
+
|----------|--|-------------|
|
|
383
|
+
| `robot` | | The robot learning |
|
|
384
|
+
| `text` | | The stripped learning text |
|
|
385
|
+
| `learnings_before` | | Frozen copy of `robot.learnings` prior to the write |
|
|
386
|
+
| `stored` | rw | Set `true` by core when the learning was actually appended (i.e. not deduplicated away) |
|
|
387
|
+
| `error` | rw | |
|
|
388
|
+
|
|
389
|
+
#### CompactionHookContext (`event: :compaction`)
|
|
390
|
+
|
|
391
|
+
| Accessor | | Description |
|
|
392
|
+
|----------|--|-------------|
|
|
393
|
+
| `robot` | | |
|
|
394
|
+
| `messages_before` | | Frozen copy of `chat.messages` before compaction |
|
|
395
|
+
| `config` | | The effective `RunConfig` |
|
|
396
|
+
| `strategy` | | `:context_window`, or `:custom` when `auto_compact` is a `Proc` |
|
|
397
|
+
| `compacted_messages` | rw | Assign in `on_compaction` to **replace** the core algorithm |
|
|
398
|
+
| `error` | rw | |
|
|
399
|
+
| `handled?` | | `!compacted_messages.nil?` — core skips its own compaction when true |
|
|
400
|
+
|
|
401
|
+
---
|
|
402
|
+
|
|
403
|
+
## ExtensionState and DotState
|
|
404
|
+
|
|
405
|
+
The two-level store behind `ctx.local` / `ctx.ext`.
|
|
406
|
+
|
|
407
|
+
### ExtensionState
|
|
408
|
+
|
|
409
|
+
| Method | Returns | Description |
|
|
410
|
+
|--------|---------|-------------|
|
|
411
|
+
| `ext(name)` | `DotState` | The `DotState` for a namespace, auto-created on first access |
|
|
412
|
+
| `to_h` | `Hash` | `{ namespace => state_hash }` for every namespace touched |
|
|
413
|
+
|
|
414
|
+
### DotState
|
|
415
|
+
|
|
416
|
+
A schemaless dot-access bag.
|
|
417
|
+
|
|
418
|
+
| Method | Returns | Description |
|
|
419
|
+
|--------|---------|-------------|
|
|
420
|
+
| `method_missing` | value / assigned value | `state.foo` reads, `state.foo = 1` writes. Any key is allowed |
|
|
421
|
+
| `merge_defaults(hash)` | `self` | Writes each pair **only when the key is absent** — this is how a registration's `context:` becomes defaults rather than an override |
|
|
422
|
+
| `to_h` | `Hash` | A `dup` of the backing hash |
|
|
423
|
+
|
|
424
|
+
!!! warning "`DotState#respond_to_missing?` always returns true"
|
|
425
|
+
`state.respond_to?(:anything)` is `true`, and reading a key that was never
|
|
426
|
+
set returns `nil` instead of raising `NoMethodError`. A typo in a `ctx.local`
|
|
427
|
+
reader fails silently.
|
|
428
|
+
|
|
429
|
+
---
|
|
430
|
+
|
|
431
|
+
## Module-level hook methods
|
|
432
|
+
|
|
433
|
+
```ruby
|
|
434
|
+
RobotLab.hooks # => the global HookRegistry (memoized)
|
|
435
|
+
RobotLab.on(HandlerClass, context: nil)
|
|
436
|
+
RobotLab.clear_hooks! # => replaces the global registry with a new one
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
`clear_hooks!` swaps in a brand-new `HookRegistry`; any `Registration` object you
|
|
440
|
+
held from before is orphaned. Use it in test teardown to avoid leaking handlers
|
|
441
|
+
between examples.
|
|
442
|
+
|
|
443
|
+
### with_hook_scope / current_hook_scope
|
|
444
|
+
|
|
445
|
+
```ruby
|
|
446
|
+
RobotLab.with_hook_scope(registries, per_run_hooks) { ... }
|
|
447
|
+
RobotLab.current_hook_scope # => { registries: [...], per_run_hooks: ... } or nil
|
|
448
|
+
```
|
|
449
|
+
|
|
450
|
+
Publishes the active run's registries and per-run handlers in a
|
|
451
|
+
`Thread.current` slot for the duration of the block, restoring the previous value
|
|
452
|
+
in an `ensure`. `Robot::Hooking#run` opens the scope; `Tool#call` reads it so a
|
|
453
|
+
tool invoked deep inside RubyLLM's tool loop still resolves the same registries
|
|
454
|
+
(including the network's and any `hooks:` passed to that one `run`) instead of
|
|
455
|
+
falling back to `[RobotLab.hooks, robot.hooks]`.
|
|
456
|
+
|
|
457
|
+
!!! note "Thread-local, so it does not cross a thread boundary"
|
|
458
|
+
A tool that executes on another thread — or a `delegate(async: true)` target —
|
|
459
|
+
sees `current_hook_scope == nil` and falls back to the global + robot
|
|
460
|
+
registries. Network-scoped and per-run hooks will not fire for it.
|
|
461
|
+
|
|
462
|
+
---
|
|
463
|
+
|
|
464
|
+
## See Also
|
|
465
|
+
|
|
466
|
+
- [Hook System guide](../guides/hooks.md) — families, ordering, and applied patterns
|
|
467
|
+
- [Observability & Safety](../guides/observability.md) — `Narrator`, budgets, doom-loop detection
|
|
468
|
+
- [Support API](support.md) — `Narrator`, the one `Hook` subclass shipped in core
|
|
469
|
+
- [Robot: on](core/robot.md#on) · [Network: on](core/network.md#on)
|