riffer 0.45.0 → 0.46.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b166b910624129bd5b64b9443caba3b5c781b691a7453b5640cd132c21cd72c2
4
- data.tar.gz: d64178e66b49945647393430b57f84a501ec7c0bfbe866b20f6f1b6af44ef570
3
+ metadata.gz: 569f1c5929847037c97cc5552eb38a8609db6a1e67ce99b8135961d50aa7a0b5
4
+ data.tar.gz: c8e03583eb04ff2c8922b8d554c10f5a47fb4421b15600905ba21b0d69031aed
5
5
  SHA512:
6
- metadata.gz: 36ce62e6539e22a01c5f8523278203b0307a2753c8e3af8e683133b741144417b360721862249d2c64fb118211f134e7b968d681e4c31e20f9ca051768e9aef6
7
- data.tar.gz: 39ac8834695876cd923e3dffe7065045f7296e418056e18eae9da4811ba04c886b546342a4a4586d92ac529ec456a18e0b272b0598eb70c72371af2dbefebce3
6
+ metadata.gz: 6a0380696a959b4c291a5bf52c03eb7a1e06971a60ded5d058311bbdfe9efd99e58bf94506980e2d4b7b0e21257773894d11be4d1a896ea1e7e3ba8e2ce3359e
7
+ data.tar.gz: 965f4d1fe6b97f4bd887a1fddd310c7a4cb81b5a0789cf8b91c58c49796abe79f396ec410b6008895275bd16281593a29f53d870e591124ade85b38c62a8fb22
@@ -1,3 +1,3 @@
1
1
  {
2
- ".": "0.45.0"
2
+ ".": "0.46.1"
3
3
  }
data/CHANGELOG.md CHANGED
@@ -5,6 +5,31 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.46.1](https://github.com/janeapp/riffer/compare/riffer/v0.46.0...riffer/v0.46.1) (2026-09-11)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * accept JSON integers for Float params ([#429](https://github.com/janeapp/riffer/issues/429)) ([928c33b](https://github.com/janeapp/riffer/commit/928c33b1125bf013f66c8b736191bc70eb313ccd))
14
+ * **agent:** type stream's enumerator as returning Response ([#428](https://github.com/janeapp/riffer/issues/428)) ([4d2b2d2](https://github.com/janeapp/riffer/commit/4d2b2d2aa08d1d95b97226e84180284349fff441))
15
+
16
+ ## [0.46.0](https://github.com/janeapp/riffer/compare/riffer/v0.45.0...riffer/v0.46.0) (2026-09-08)
17
+
18
+
19
+ ### ⚠ BREAKING CHANGES
20
+
21
+ * Response#blocked?, Response#interrupted?, and Response#interrupt_reason are removed. Check response.outcome.reason for :guardrail_blocked, :interrupted, or :max_steps instead; response.tripwire still carries the full tripwire object. Response.new now requires outcome:.
22
+
23
+ ### Features
24
+
25
+ * report how a run ended through Response#outcome ([#425](https://github.com/janeapp/riffer/issues/425)) ([2ba2157](https://github.com/janeapp/riffer/commit/2ba2157668cd4a2b013da190dd647ae131accfa3))
26
+
27
+
28
+ ### Bug Fixes
29
+
30
+ * **anthropic:** merge caller output_config with structured output ([#422](https://github.com/janeapp/riffer/issues/422)) ([e1a60e6](https://github.com/janeapp/riffer/commit/e1a60e6f082dbf7d4ee9d151e96e56bdf3f26359))
31
+ * **runner:** make Fibers runner safe inside a running Async reactor ([#424](https://github.com/janeapp/riffer/issues/424)) ([47d3706](https://github.com/janeapp/riffer/commit/47d3706dbeac1e456bbf8365361cf16741b09adf))
32
+
8
33
  ## [0.45.0](https://github.com/janeapp/riffer/compare/riffer/v0.44.0...riffer/v0.45.0) (2026-09-03)
9
34
 
10
35
 
data/docs/AGENTS.md CHANGED
@@ -168,6 +168,19 @@ end
168
168
 
169
169
  The LLM response is automatically parsed and validated against the schema. Access the result via `response.structured_output`.
170
170
 
171
+ When the response is not valid JSON or does not satisfy the schema, `response.structured_output` is `nil`, `response.outcome.reason` is `:invalid_structured_output`, and `response.outcome.detail` carries the parse or validation message:
172
+
173
+ ```ruby
174
+ response = SentimentAgent.generate('Analyze: "I love this!"')
175
+
176
+ if response.outcome.success?
177
+ response.structured_output # => {sentiment: "positive", score: 0.95}
178
+ else
179
+ response.outcome.reason # => :invalid_structured_output
180
+ response.outcome.detail # => "score is required"
181
+ end
182
+ ```
183
+
171
184
  #### Nested Objects
172
185
 
173
186
  Use `Hash` with a block to define nested object schemas:
@@ -30,9 +30,8 @@ agent.generate(prompt = nil, files: nil)
30
30
  ```ruby
31
31
  # New conversation (class method — recommended for simple calls)
32
32
  response = MyAgent.generate('Hello', context: {user_id: 123})
33
- puts response.content # Access the response text
34
- puts response.blocked? # Check if guardrail blocked (always false without guardrails)
35
- puts response.interrupted? # Check if a callback interrupted the loop
33
+ puts response.content # Access the response text
34
+ puts response.outcome.reason # How the run ended (:completed, :guardrail_blocked, :interrupted, ...)
36
35
 
37
36
  # New conversation (instance method — when you need message history or callbacks)
38
37
  agent = MyAgent.new(context: {user_id: 123})
@@ -60,6 +59,14 @@ response = MyAgent.generate('What is in this image?', files: [
60
59
 
61
60
  Streams a response as an Enumerator. Same prompt/files semantics as `generate`.
62
61
 
62
+ Consuming the enumerator with a block returns the same `Riffer::Agent::Response` that `generate` would, so you can stream events to the user and still inspect the final outcome:
63
+
64
+ ```ruby
65
+ response = MyAgent.stream('Tell me a story').each { |event| handle(event) }
66
+ response.outcome.reason # => :completed
67
+ response.content
68
+ ```
69
+
63
70
  ```ruby
64
71
  # New conversation (class method — recommended for simple calls)
65
72
  MyAgent.stream('Tell me a story').each do |event|
@@ -142,9 +149,9 @@ Works with both `generate` and `stream`. Only emits agent-generated messages (As
142
149
 
143
150
  Callbacks can interrupt the agent loop. This is useful for human-in-the-loop approval, cost limits, or content filtering.
144
151
 
145
- Use `agent.interrupt!` (or the lower-level `throw :riffer_interrupt`) to stop the loop. The response will have `interrupted?` set to `true` and contain the accumulated content up to the point of interruption.
152
+ Use `agent.interrupt!` (or the lower-level `throw :riffer_interrupt`) to stop the loop. The response's `outcome.reason` will be `:interrupted` and `content` will hold the accumulated content up to the point of interruption.
146
153
 
147
- An optional reason can be passed to `interrupt!`. It is available via `interrupt_reason` on the response (generate) or `reason` on the `Interrupt` event (stream):
154
+ An optional reason can be passed to `interrupt!`. It is available via `outcome.detail` on the response (generate) or `reason` on the `Interrupt` event (stream):
148
155
 
149
156
  ```ruby
150
157
  agent = MyAgent.new
@@ -155,9 +162,9 @@ agent.session.on_message do |msg|
155
162
  end
156
163
 
157
164
  response = agent.generate('Call the tool')
158
- response.interrupted? # => true
159
- response.interrupt_reason # => "needs human approval"
160
- response.content # => last assistant content before interrupt
165
+ response.outcome.reason # => :interrupted
166
+ response.outcome.detail # => "needs human approval"
167
+ response.content # => last assistant content before interrupt
161
168
  ```
162
169
 
163
170
  **Streaming** — interrupts emit an `Interrupt` event:
@@ -188,7 +195,7 @@ agent.session.on_message { |msg| throw :riffer_interrupt if needs_approval?(msg)
188
195
 
189
196
  response = agent.generate('Do something risky')
190
197
 
191
- if response.interrupted?
198
+ if response.outcome.reason == :interrupted
192
199
  approve_action(agent.session.messages)
193
200
  response = agent.generate('Approved, go ahead') # executes pending tools, then calls the LLM
194
201
  # or: agent.generate # resume without a new turn
@@ -301,23 +308,49 @@ agent.context[:skills] # the Skills::Context, if skills configured
301
308
 
302
309
  ## Response Attributes
303
310
 
304
- `Riffer::Agent::Response` is returned by `generate`:
311
+ `Riffer::Agent::Response` is returned by `generate`. Start with `response.outcome`: its `reason` says how the run ended, and everything else on the response is detail for that reason. `response.content` and `response.structured_output` are only meaningful when the reason is `:completed`; see [response.outcome](#responseoutcome) for the full vocabulary.
305
312
 
306
313
  | Attribute | Type | Description |
307
314
  | ---------------------- | --------------------------- | ------------------------------------------------------------------------------------------------ |
308
315
  | `content` | `String` | The response text |
316
+ | `outcome` | `Outcome` | How the run ended — `reason` and optional `detail` (see below) |
309
317
  | `structured_output` | `Hash` / `nil` | Parsed and validated structured output (see below) |
310
- | `blocked?` | `Boolean` | `true` if a guardrail tripwire fired |
311
318
  | `tripwire` | `Tripwire` / `nil` | The guardrail tripwire that blocked the request |
312
319
  | `modified?` | `Boolean` | `true` if a guardrail modified the content |
313
320
  | `modifications` | `Array` | List of guardrail modifications applied |
314
- | `interrupted?` | `Boolean` | `true` if the loop was interrupted |
315
- | `interrupt_reason` | `String` / `Symbol` / `nil` | The reason passed to `throw :riffer_interrupt` |
316
321
  | `messages` | `Array` | Full message history from the conversation |
317
322
  | `healed_tool_call_ids` | `Array[String]` | `tool_call` ids filled with placeholder results during interrupt healing (else `[]`) |
318
323
  | `token_usage` | `TokenUsage` / `nil` | Aggregate `Riffer::Providers::TokenUsage` across this run's LLM calls (`nil` when none reported) |
319
324
  | `steps` | `Integer` | LLM calls made during this run (`0` when a before-guardrail blocks first); not the session's cumulative count |
320
325
 
326
+ ### response.outcome
327
+
328
+ `response.outcome` is a `Riffer::Agent::Outcome` — the single place to read how the run ended. `reason` is always one of the values below; `detail` is a `String` with the specifics when there are any, else `nil`. `outcome.success?` is shorthand for `reason == :completed`.
329
+
330
+ | Reason | Source | `detail` |
331
+ | ---------------------------- | ---------------------------------------------------------- | ----------------------------------------- |
332
+ | `:completed` | The loop ended normally | `nil` |
333
+ | `:guardrail_blocked` | A guardrail tripwire fired (`tripwire` is set) | The tripwire reason |
334
+ | `:max_steps` | The `max_steps` limit was reached | `nil` |
335
+ | `:interrupted` | A callback called `interrupt!` / `throw :riffer_interrupt` | The interrupt reason, or `nil` |
336
+ | `:length`, `:content_filter`, `:context_window`, `:malformed_output`, `:error`, `:other` | The assistant message's normalized `finish_reason` (see [Messages — Finish Reasons](MESSAGES.md#finish-reasons)) | The provider's raw finish value (`finish_reason_raw`), or `nil` |
337
+ | `:invalid_structured_output` | The final message failed JSON parsing or schema validation | The parse or validation error |
338
+
339
+ When several apply, the most causal wins: a guardrail block outranks an interrupt, an interrupt outranks the provider's finish reason, and the provider's finish reason outranks a structured output failure. A run that hit `:length` and therefore produced invalid JSON reports `:length`.
340
+
341
+ ```ruby
342
+ agent = MyAgent.new
343
+ response = agent.generate('Hello')
344
+
345
+ case response.outcome.reason
346
+ when :completed then puts response.content
347
+ when :guardrail_blocked then puts "Blocked: #{response.outcome.detail}"
348
+ when :interrupted, :max_steps then response = agent.generate('Continue')
349
+ when :invalid_structured_output then warn response.outcome.detail
350
+ else warn "Provider stopped early: #{response.outcome.reason}"
351
+ end
352
+ ```
353
+
321
354
  ### response.structured_output
322
355
 
323
356
  When structured output is configured, the LLM response is parsed as JSON and validated against the schema. The validated result is available as `response.structured_output`:
@@ -328,7 +361,7 @@ response.content # => raw JSON string from the LLM
328
361
  response.structured_output # => {sentiment: "positive", score: 0.95}
329
362
  ```
330
363
 
331
- Returns `nil` when structured output is not configured or when validation fails.
364
+ Returns `nil` when structured output is not configured, or when parsing or validation fails — in which case `response.outcome.reason` is `:invalid_structured_output` and `response.outcome.detail` carries the error.
332
365
 
333
366
  The assistant message in the message history stores the parsed hash, so you can access structured output directly from persisted messages:
334
367
 
data/docs/AGENT_LOOP.md CHANGED
@@ -34,7 +34,7 @@ The agent loop normally runs until the LLM produces a response with no tool call
34
34
  Guardrails are registered at class definition time and run automatically on every request. When a guardrail calls `block`, it sets a **tripwire** that stops the loop immediately. The LLM is never called (for `:before` guardrails) or its response is discarded (for `:after` guardrails).
35
35
 
36
36
  - **When to use:** Policy enforcement that should always apply — content filtering, input validation, length limits.
37
- - **Response:** `response.blocked?` returns `true`, `response.tripwire` contains the reason and metadata.
37
+ - **Response:** `response.outcome.reason` is `:guardrail_blocked`, `response.tripwire` contains the reason and metadata.
38
38
  - **Streaming:** Yields a `GuardrailTripwire` event.
39
39
  - **Resumable:** No. A tripwire is a hard stop. The caller must change the input and start a new `generate`/`stream` call.
40
40
 
@@ -45,7 +45,7 @@ class MyAgent < Riffer::Agent
45
45
  end
46
46
 
47
47
  response = MyAgent.generate('blocked input')
48
- response.blocked? # => true
48
+ response.outcome.reason # => :guardrail_blocked
49
49
  response.tripwire.reason # => "Content policy violation"
50
50
  ```
51
51
 
@@ -54,7 +54,7 @@ response.tripwire.reason # => "Content policy violation"
54
54
  Callbacks registered with `on_message` can call `agent.interrupt!` (or `throw :riffer_interrupt`) to pause the loop at any point — after receiving an assistant message, after a tool result, etc. The caller controls exactly when and why to interrupt.
55
55
 
56
56
  - **When to use:** Flow control that depends on runtime decisions — human-in-the-loop approval, budget tracking, conditional pausing.
57
- - **Response:** `response.interrupted?` returns `true`, `response.interrupt_reason` contains the optional reason.
57
+ - **Response:** `response.outcome.reason` is `:interrupted`, `response.outcome.detail` contains the optional reason.
58
58
  - **Streaming:** Yields an `Interrupt` event with a `reason` attribute.
59
59
  - **Resumable:** Yes. Call `generate('Continue')` or `stream('Continue')` on the same agent instance to resume. For cross-process resume, pass persisted messages as an array to a new agent. Pending tool calls are automatically executed before the LLM loop resumes.
60
60
 
@@ -65,8 +65,8 @@ agent.session.on_message do |msg|
65
65
  end
66
66
 
67
67
  response = agent.generate('Do something risky')
68
- response.interrupted? # => true
69
- response.interrupt_reason # => "approval needed"
68
+ response.outcome.reason # => :interrupted
69
+ response.outcome.detail # => "approval needed"
70
70
  response = agent.generate('Approved, continue') # continues where it left off
71
71
  ```
72
72
 
@@ -75,7 +75,7 @@ response = agent.generate('Approved, continue') # continues where it left off
75
75
  The `max_steps` class method caps the number of LLM call steps in the tool-use loop. When the step count reaches the limit, the loop interrupts automatically with reason `:max_steps`.
76
76
 
77
77
  - **When to use:** Safety net to prevent runaway tool-use loops — useful when agents have access to many tools or operate autonomously.
78
- - **Response:** `response.interrupted?` returns `true`, `response.interrupt_reason` is `:max_steps`.
78
+ - **Response:** `response.outcome.reason` is `:max_steps`.
79
79
  - **Streaming:** Yields an `Interrupt` event with `reason: :max_steps`.
80
80
  - **Resumable:** Yes. Call `generate('Continue')` or `stream('Continue')` on the same agent instance to resume. For cross-process resume, pass persisted messages as an array to a new agent. Pending tool calls are automatically executed before the LLM loop resumes.
81
81
 
@@ -86,8 +86,7 @@ class MyAgent < Riffer::Agent
86
86
  end
87
87
 
88
88
  response = MyAgent.generate('Do a complex task')
89
- response.interrupted? # => true (if 8 steps were reached)
90
- response.interrupt_reason # => :max_steps
89
+ response.outcome.reason # => :max_steps (if 8 steps were reached)
91
90
  ```
92
91
 
93
92
  ### Unhandled Exceptions
@@ -101,6 +100,6 @@ If a guardrail, provider call, or other internal code raises an exception, it pr
101
100
  | Defined | At class level (`guardrail :before`) | At instance level (`on_message`) | At class level (`max_steps 8`) |
102
101
  | Fires | Automatically on every request | When callback logic decides | When step count reaches limit |
103
102
  | Resumable | No | Yes (call `generate`/`stream` again) | Yes (call `generate`/`stream` again) |
104
- | Response flag | `blocked?` | `interrupted?` | `interrupted?` |
103
+ | Outcome reason | `:guardrail_blocked` | `:interrupted` | `:max_steps` |
105
104
  | Stream event | `GuardrailTripwire` | `Interrupt` | `Interrupt` |
106
105
  | Purpose | Policy enforcement | Flow control | Runaway loop prevention |
@@ -198,19 +198,19 @@ Per file, riffer applies this policy, in order:
198
198
 
199
199
  1. **Already inline data** — nothing to download. If `sha256:` was given, it's verified against the existing bytes regardless of any other setting below.
200
200
  2. **Provider can't accept the file at all** — raises `Riffer::FileUnsupportedError`.
201
- 3. **Provider accepts a URL as-is** — passed straight through, untouched, *unless* `sha256:` was given, in which case riffer downloads and verifies anyway (a caller who set `sha256:` is asking for integrity verification, not a passthrough).
201
+ 3. **Provider accepts a URL as-is** — passed straight through, untouched, _unless_ `sha256:` was given, in which case riffer downloads and verifies anyway (a caller who set `sha256:` is asking for integrity verification, not a passthrough).
202
202
  4. **Provider needs the bytes inline** — riffer downloads the file, verifying `sha256:` if given.
203
203
 
204
204
  Every download in step 3 or 4 is gated by `allow_downloads`; with it `false` (the default), reaching either of those steps raises `Riffer::FileDownloadsDisabledError` instead of fetching anything. This means upgrading to a riffer version with this feature never starts downloading arbitrary URLs on your behalf — you have to opt in.
205
205
 
206
- | Option | Description |
207
- | ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
208
- | `allow_downloads` | Whether riffer may download a `FilePart`'s URL. Accepts booleans or `'true'`/`'false'`/`'1'`/`'0'`. Defaults to `false`. |
209
- | `max_bytes` | Maximum size, in bytes, of a downloaded file; the download is aborted once the streamed body exceeds this, independent of (and regardless of a missing/lying) `content-length` header. Defaults to `3_500_000`. |
210
- | `timeout` | Open and read timeout, in seconds, for a single download attempt. Defaults to `60`. |
211
- | `max_per_message` | Maximum number of files allowed on a single user message; checked against each message as originally authored, before consecutive messages are merged. `nil` (default) means uncapped. |
212
- | `runner` | A `Riffer::Runner` instance that resolves every file across a call's messages. Defaults to `Riffer::Runner::Sequential.new`; assign `Riffer::Runner::Threaded.new` (or `Riffer::Runner::Fibers.new` inside a fiber-based host) to resolve multiple files concurrently. |
213
- | `downloader` | The object that fetches a URL's bytes; must respond to `#call(url, max_bytes:, timeout:)` returning the raw (not base64-encoded) file content. Riffer caches it as base64 or raw bytes, whichever the provider actually needs, rather than producing both. Defaults to `Riffer::Files::Downloader.new`, which fetches over HTTPS only, following up to 3 redirects. Assign your own to add logging/metrics, or to fetch from a non-HTTPS store (e.g. `s3://`). |
206
+ | Option | Description |
207
+ | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
208
+ | `allow_downloads` | Whether riffer may download a `FilePart`'s URL. Accepts booleans or `'true'`/`'false'`/`'1'`/`'0'`. Defaults to `false`. |
209
+ | `max_bytes` | Maximum size, in bytes, of a downloaded file; the download is aborted once the streamed body exceeds this, independent of (and regardless of a missing/lying) `content-length` header. Defaults to `3_500_000`. |
210
+ | `timeout` | Open and read timeout, in seconds, for a single download attempt. Defaults to `60`. |
211
+ | `max_per_message` | Maximum number of files allowed on a single user message; checked against each message as originally authored, before consecutive messages are merged. `nil` (default) means uncapped. |
212
+ | `runner` | A `Riffer::Runner` instance that resolves every file across a call's messages. Defaults to `Riffer::Runner::Sequential.new`; assign `Riffer::Runner::Threaded.new` (or `Riffer::Runner::Fibers.new` inside a fiber-based host) to resolve multiple files concurrently. |
213
+ | `downloader` | The object that fetches a URL's bytes; must respond to `#call(url, max_bytes:, timeout:)` returning the raw (not base64-encoded) file content. Riffer caches it as base64 or raw bytes, whichever the provider actually needs, rather than producing both. Defaults to `Riffer::Files::Downloader.new`, which fetches over HTTPS only, following up to 3 redirects. Assign your own to add logging/metrics, or to fetch from a non-HTTPS store (e.g. `s3://`). |
214
214
 
215
215
  A file that fails resolution raises a `Riffer::FileError` subclass — `Riffer::FileUnsupportedError`, `Riffer::FileDownloadsDisabledError`, `Riffer::TooManyFilesError`, `Riffer::FileChecksumMismatchError`, `Riffer::FileTooLargeError`, `Riffer::FileDownloadError`, or `Riffer::FileEncodingError` — so callers can `rescue Riffer::FileError` for any attachment problem, or a specific subclass to handle one case.
216
216
 
@@ -345,14 +345,15 @@ end
345
345
 
346
346
  ### Anthropic
347
347
 
348
- | Option | Description |
349
- | ------------- | ------------------------------------------- |
350
- | `temperature` | Sampling temperature |
351
- | `max_tokens` | Maximum tokens in response |
352
- | `top_p` | Nucleus sampling parameter |
353
- | `top_k` | Top-k sampling parameter |
354
- | `thinking` | Extended thinking config hash (Claude 3.7+) |
355
- | `web_search` | Enable web search (`true` or config hash) |
348
+ | Option | Description |
349
+ | --------------- | ------------------------------------------- |
350
+ | `temperature` | Sampling temperature |
351
+ | `max_tokens` | Maximum tokens in response |
352
+ | `top_p` | Nucleus sampling parameter |
353
+ | `top_k` | Top-k sampling parameter |
354
+ | `thinking` | Extended thinking config hash (Claude 3.7+) |
355
+ | `output_config` | Output config hash (e.g. `effort`) |
356
+ | `web_search` | Enable web search (`true` or config hash) |
356
357
 
357
358
  ```ruby
358
359
  class MyAgent < Riffer::Agent
@@ -365,6 +366,12 @@ class ReasoningAgent < Riffer::Agent
365
366
  model 'anthropic/claude-haiku-4-5-20251001'
366
367
  model_options thinking: {type: "enabled", budget_tokens: 10000}
367
368
  end
369
+
370
+ # With an output effort level
371
+ class EffortAgent < Riffer::Agent
372
+ model 'anthropic/claude-opus-5'
373
+ model_options output_config: {effort: "high"}
374
+ end
368
375
  ```
369
376
 
370
377
  ## Environment Variables
data/docs/GUARDRAILS.md CHANGED
@@ -28,10 +28,10 @@ class MyAgent < Riffer::Agent
28
28
  end
29
29
 
30
30
  response = MyAgent.generate("Hello!")
31
- response.blocked? # => false
31
+ response.outcome.reason # => :completed
32
32
 
33
33
  response = MyAgent.generate("You are a badword")
34
- response.blocked? # => true
34
+ response.outcome.reason # => :guardrail_blocked
35
35
  response.tripwire.reason # => "Profanity detected"
36
36
  ```
37
37
 
@@ -195,7 +195,7 @@ end
195
195
  response = MyAgent.generate("Hello")
196
196
 
197
197
  response.content # The response text
198
- response.blocked? # true if a guardrail blocked execution
198
+ response.outcome.reason # :guardrail_blocked if a guardrail blocked execution
199
199
  response.tripwire # Tripwire object with block details (if blocked)
200
200
  response.modified? # true if any guardrail transformed data
201
201
  response.modifications # Array of Modification records
@@ -206,7 +206,7 @@ response.modifications # Array of Modification records
206
206
  ```ruby
207
207
  response = MyAgent.generate("Hello")
208
208
 
209
- if response.blocked?
209
+ if response.outcome.reason == :guardrail_blocked
210
210
  puts "Blocked: #{response.tripwire.reason}"
211
211
  puts "Phase: #{response.tripwire.phase}"
212
212
  puts "Guardrail: #{response.tripwire.guardrail}"
data/docs/MESSAGES.md CHANGED
@@ -49,7 +49,8 @@ msg.role # => :assistant
49
49
  msg.content # => "I'm doing well, thank you!"
50
50
  msg.tool_calls # => []
51
51
  msg.token_usage # => nil or Riffer::Providers::TokenUsage
52
- msg.finish_reason # => nil or a normalized Symbol (see below)
52
+ msg.finish_reason # => nil or a normalized Symbol (see below)
53
+ msg.finish_reason_raw # => nil or the provider's raw wire value (e.g. "max_tokens")
53
54
 
54
55
  # Response with tool calls
55
56
  msg = Riffer::Messages::Assistant.new("", tool_calls: [
@@ -94,7 +95,7 @@ The cache buckets are subsets of `input_tokens`, never additions to it — summi
94
95
  | `:error` | The provider reported an error finish. |
95
96
  | `:other` | A provider-specific value with no normalized equivalent. |
96
97
 
97
- `finish_reason` is `nil` when the provider doesn't report one. The provider's raw wire value travels alongside on the `FinishReasonDone` stream event and the `riffer.finish_reason.raw` trace attribute — for OpenRouter that is the upstream model's `native_finish_reason`, and for a failed OpenAI response it is the error code. Use `finish_reason` to detect truncation without parsing provider responses:
98
+ `finish_reason` is `nil` when the provider doesn't report one. The provider's raw wire value travels alongside as `finish_reason_raw` on the message (round-tripped through `to_h` / `from_hash`), on the `FinishReasonDone` stream event, and as the `riffer.finish_reason.raw` trace attribute — for OpenRouter that is the upstream model's `native_finish_reason`, and for a failed OpenAI response it is the error code. Use `finish_reason` to detect truncation without parsing provider responses:
98
99
 
99
100
  ```ruby
100
101
  response = agent.generate("Summarize this document")
@@ -103,7 +104,7 @@ retry_with_higher_limit if agent.session.messages.last.finish_reason == :length
103
104
 
104
105
  #### Structured Output on Messages
105
106
 
106
- When an agent has `structured_output` configured, the final assistant message stores the parsed hash directly. The `structured_output?` predicate checks for a non-nil value:
107
+ When an agent has `structured_output` configured, the final assistant message stores the parsed hash directly. The message holds the parsed JSON, not the schema-validated result; schema validation is reported on `response.outcome` (see [Agent Lifecycle — response.outcome](AGENT_LIFECYCLE.md#responseoutcome)). The `structured_output?` predicate checks for a non-nil value:
107
108
 
108
109
  ```ruby
109
110
  msg = Riffer::Messages::Assistant.new('{"sentiment":"positive"}', structured_output: {sentiment: "positive"})
@@ -215,7 +215,7 @@ Emitted when the agent loop is interrupted. This can happen in two ways:
215
215
  - An `on_message` callback calls `agent.interrupt!` or `throw :riffer_interrupt` (reason is a String or `nil`).
216
216
  - The `max_steps` limit is reached (reason is the Symbol `:max_steps`).
217
217
 
218
- This is the streaming equivalent of `Response#interrupted?` in generate mode.
218
+ This is the streaming equivalent of `response.outcome.reason == :interrupted` (or `:max_steps`) in generate mode.
219
219
 
220
220
  ```ruby
221
221
  # Callback interrupt with a string reason
data/docs/TOOLS.md CHANGED
@@ -115,6 +115,8 @@ Options:
115
115
 
116
116
  `Riffer::Params::Boolean` is the preferred way to declare boolean parameters. `TrueClass` and `FalseClass` continue to work for backwards compatibility.
117
117
 
118
+ A `Float` param accepts a whole number too, since JSON Schema's `number` covers integers — a model returning `120` for a `Float` is valid, and the validated value is coerced to `120.0`. `Integer` stays strict: `1.0` is rejected, matching JSON Schema's `integer`.
119
+
118
120
  ### Nested Parameters
119
121
 
120
122
  Tool params support the same nested DSL as structured output — nested objects (`Hash` with block), typed arrays (`Array, of:`), and arrays of objects (`Array` with block). See the [structured output section in Agents](AGENTS.md#nested-objects) for full syntax.
@@ -220,6 +220,8 @@ end
220
220
 
221
221
  Fibers use cooperative scheduling — they yield control at I/O boundaries (network calls, file reads, sleep). CPU-bound tools will not benefit from the fibers runtime. Be mindful of fiber-local state (`Fiber.[]`) and note that `Thread.current[]` values are shared across all fibers in the same thread.
222
222
 
223
+ The fibers runtime is safe to use inside an existing reactor. When called from plain Ruby it starts its own reactor and blocks until every tool call finishes. When the process is already inside an Async task — for example under Falcon, or inside an `Async do ... end` block — it joins the current task instead of starting a nested reactor, so it is safe to call from request handlers.
224
+
223
225
  ### Custom Runtimes
224
226
 
225
227
  Create a custom runtime by subclassing `Riffer::Tools::Runtime` and overriding the private `dispatch_tool_call` method:
data/docs/TRACING.md CHANGED
@@ -113,6 +113,8 @@ Any tags passed to `#generate` / `#stream` via `tags:` are stamped on **all four
113
113
  | `gen_ai.usage.cache_read.input_tokens` | int | When the provider reported cache reads |
114
114
  | `gen_ai.usage.cache_creation.input_tokens` | int | When the provider reported cache writes |
115
115
  | `riffer.cost` | float | When every call in the run was priced |
116
+ | `riffer.outcome.reason` | string | Always — the `response.outcome.reason` |
117
+ | `riffer.outcome.detail` | string | When `response.outcome.detail` is present |
116
118
  | `riffer.interrupt.reason` | string | On interrupt (e.g. approval needed, max steps) |
117
119
  | `riffer.tripwire.guardrail` | string | On a guardrail tripwire, when the guardrail is named |
118
120
  | `riffer.tripwire.reason` | string | On a guardrail tripwire |
@@ -271,7 +273,7 @@ When enabled, content is serialized as GenAI-semconv JSON strings. File attachme
271
273
  The span and attribute shape is a public, versioned contract, in two tiers:
272
274
 
273
275
  - **`gen_ai.*`** tracks the OpenTelemetry GenAI semantic conventions, pinned to schema version `1.37.0`. That convention is still "Development" status upstream and its attribute names may change; Riffer absorbs such renames deliberately in a release, never silently, with a CHANGELOG entry.
274
- - **`riffer.*`** is Riffer-owned (`riffer.steps`, `riffer.cost`, `riffer.interrupt.reason`, `riffer.tripwire.*`, `riffer.guardrail.*`, `riffer.finish_reason.raw`) and changes only through a normal version bump and CHANGELOG entry.
276
+ - **`riffer.*`** is Riffer-owned (`riffer.steps`, `riffer.cost`, `riffer.outcome.*`, `riffer.interrupt.reason`, `riffer.tripwire.*`, `riffer.guardrail.*`, `riffer.finish_reason.raw`) and changes only through a normal version bump and CHANGELOG entry.
275
277
 
276
278
  The semantic-convention schema version is a documented pin rather than a span attribute — the OpenTelemetry Ruby API can't attach a schema URL to a tracer. The runtime version signal is the instrumentation scope: every span carries scope name `riffer` at the gem version that emitted it. Pin the Riffer version your dashboards depend on, and watch the CHANGELOG for tracing entries before upgrading.
277
279
 
@@ -21,13 +21,14 @@ Use `stub_response` to queue responses:
21
21
  ```ruby
22
22
  # Get the provider instance from the agent
23
23
  agent = TestableAgent.new
24
- provider = agent.send(:provider_instance)
24
+ provider = agent.provider
25
25
 
26
26
  # Stub a simple text response
27
27
  provider.stub_response("Hello, I'm here to help!")
28
28
 
29
29
  # Now generate will return the stubbed response
30
30
  response = agent.generate("Hi")
31
+ response.content
31
32
  # => "Hello, I'm here to help!"
32
33
  ```
33
34
 
@@ -55,10 +56,10 @@ provider.stub_response("First response")
55
56
  provider.stub_response("Second response")
56
57
  provider.stub_response("Third response")
57
58
 
58
- agent.generate("Message 1") # => "First response"
59
- agent.generate("Message 2") # => "Second response"
60
- agent.generate("Message 3") # => "Third response"
61
- agent.generate("Message 4") # => "Mock response" (default)
59
+ agent.generate("Message 1").content # => "First response"
60
+ agent.generate("Message 2").content # => "Second response"
61
+ agent.generate("Message 3").content # => "Third response"
62
+ agent.generate("Message 4").content # => "Mock response" (default)
62
63
  ```
63
64
 
64
65
  ## Inspecting Calls
@@ -92,7 +93,7 @@ require 'minitest/autorun'
92
93
  class MyAgentTest < Minitest::Test
93
94
  def setup
94
95
  @agent = TestableAgent.new
95
- @provider = @agent.send(:provider_instance)
96
+ @provider = @agent.provider
96
97
  end
97
98
 
98
99
  def test_generates_response
@@ -146,18 +147,24 @@ text_done = events.find { |e| e.is_a?(Riffer::StreamEvents::TextDone) }
146
147
 
147
148
  ## Web Search
148
149
 
149
- The test provider emits web search events when `web_search: true` is passed to the stream call:
150
+ The mock provider emits web search events when `web_search: true` is set in the agent's `model_options`:
150
151
 
151
152
  ```ruby
152
- provider.stub_response("Here are the latest results.")
153
+ class SearchingAgent < Riffer::Agent
154
+ model 'mock/any'
155
+ model_options web_search: true
156
+ end
157
+
158
+ agent = SearchingAgent.new
159
+ agent.provider.stub_response("Here are the latest results.")
153
160
 
154
161
  events = []
155
- agent.stream("What's new in Ruby?", web_search: true).each { |e| events << e }
162
+ agent.stream("What's new in Ruby?").each { |e| events << e }
156
163
 
157
164
  # Events include WebSearchStatus and WebSearchDone before text events
158
- search_deltas = events.select { |e| e.is_a?(Riffer::StreamEvents::WebSearchStatus) }
165
+ search_statuses = events.select { |e| e.is_a?(Riffer::StreamEvents::WebSearchStatus) }
159
166
  search_done = events.find { |e| e.is_a?(Riffer::StreamEvents::WebSearchDone) }
160
- search_done.query # => "test search query"
167
+ search_done.query # => "mock search query"
161
168
  search_done.sources # => [{title: "Example", url: "https://example.com"}]
162
169
  ```
163
170
 
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ # How a run ended — the single place to read whether the agent completed
5
+ # normally and, if not, why. +detail+ carries the specifics when there are any:
6
+ # the tripwire reason, the interrupt reason, the provider's raw finish value,
7
+ # or the structured output parse/validation error.
8
+ #
9
+ # response = agent.generate("Analyze this")
10
+ # case response.outcome.reason
11
+ # when :completed then puts response.structured_output
12
+ # when :invalid_structured_output then warn response.outcome.detail
13
+ # end
14
+ class Riffer::Agent::Outcome
15
+ # Finish reasons that end a turn normally; every other finish reason means the
16
+ # provider cut the turn short and surfaces as the run's outcome verbatim.
17
+ NORMAL_FINISH_REASONS = %i[stop tool_calls].freeze #: Array[Symbol]
18
+
19
+ # Derived from the provider vocabulary so a new finish reason becomes an
20
+ # outcome without a second list to update.
21
+ PROVIDER_STOP_REASONS = (Riffer::Providers::FinishReason::VALUES - NORMAL_FINISH_REASONS).freeze #: Array[Symbol]
22
+
23
+ # The vocabulary every run ends in.
24
+ VALUES = (%i[completed guardrail_blocked interrupted max_steps invalid_structured_output] +
25
+ PROVIDER_STOP_REASONS).freeze #: Array[Symbol]
26
+
27
+ # Why the run ended.
28
+ attr_reader :reason #: Symbol
29
+
30
+ # Human-readable specifics for +reason+, when there are any.
31
+ attr_reader :detail #: String?
32
+
33
+ # Raises Riffer::ArgumentError when +reason+ is outside VALUES.
34
+ #--
35
+ #: (reason: Symbol, ?detail: String?) -> void
36
+ def initialize(reason:, detail: nil)
37
+ unless VALUES.include?(reason)
38
+ raise Riffer::ArgumentError, "reason must be one of #{VALUES.inspect}, got #{reason.inspect}"
39
+ end
40
+
41
+ @reason = reason
42
+ @detail = detail
43
+ end
44
+
45
+ # Returns true when the run completed normally.
46
+ #
47
+ #--
48
+ #: () -> bool
49
+ def success?
50
+ reason == :completed
51
+ end
52
+ end