riffer 0.44.0 → 0.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.release-please-manifest.json +1 -1
  3. data/CHANGELOG.md +24 -0
  4. data/docs/AGENTS.md +13 -0
  5. data/docs/AGENT_LIFECYCLE.md +39 -14
  6. data/docs/AGENT_LOOP.md +8 -9
  7. data/docs/CONFIGURATION.md +24 -17
  8. data/docs/GUARDRAILS.md +4 -4
  9. data/docs/MESSAGES.md +14 -11
  10. data/docs/STREAM_EVENTS.md +1 -1
  11. data/docs/TOOL_ADVANCED.md +2 -0
  12. data/docs/TRACING.md +4 -2
  13. data/docs/providers/CUSTOM_PROVIDERS.md +1 -1
  14. data/docs/providers/MOCK_PROVIDER.md +18 -11
  15. data/lib/riffer/agent/outcome.rb +52 -0
  16. data/lib/riffer/agent/response.rb +12 -33
  17. data/lib/riffer/agent/run.rb +54 -16
  18. data/lib/riffer/agent/session.rb +2 -0
  19. data/lib/riffer/messages/assistant.rb +24 -2
  20. data/lib/riffer/messages/base.rb +11 -19
  21. data/lib/riffer/providers/amazon_bedrock.rb +3 -0
  22. data/lib/riffer/providers/anthropic.rb +9 -0
  23. data/lib/riffer/providers/base.rb +1 -0
  24. data/lib/riffer/providers/finish_reason.rb +1 -1
  25. data/lib/riffer/providers/gemini.rb +10 -1
  26. data/lib/riffer/providers/open_ai.rb +30 -23
  27. data/lib/riffer/providers/open_router.rb +21 -7
  28. data/lib/riffer/runner/fibers.rb +12 -8
  29. data/lib/riffer/version.rb +1 -1
  30. data/sig/_private/async.rbs +4 -0
  31. data/sig/generated/riffer/agent/outcome.rbs +41 -0
  32. data/sig/generated/riffer/agent/response.rbs +11 -26
  33. data/sig/generated/riffer/agent/run.rbs +18 -7
  34. data/sig/generated/riffer/messages/assistant.rbs +14 -2
  35. data/sig/generated/riffer/providers/open_ai.rbs +8 -3
  36. data/sig/generated/riffer/providers/open_router.rbs +10 -2
  37. data/sig/generated/riffer/runner/fibers.rbs +2 -0
  38. metadata +3 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f7862ed670d852eee1e076386032449773a8782424fbfc9bc80e96aab61b1e3
4
- data.tar.gz: b520d7629441e6acd75a677bc3d094359f5a5b7f31c2986d6aadd51f2935b3e1
3
+ metadata.gz: ac304f75fface8720f1e2565ea2064b643abb2f1c29f5a9dfe5281820e9d3986
4
+ data.tar.gz: de5695cffa30697e0356de921227caffdab12ff806e143166369c36e3ae010d6
5
5
  SHA512:
6
- metadata.gz: '069d1d96193748ffdc48eb95a8fe22b57c6293a33e7ba4658a01fda4e5c4a7847145bb234605c0507059830c300dc2d85ec295ecf2d59de20265385644d6dc89'
7
- data.tar.gz: 86bbcfb5408f3b5ba0fbb327dff9c2a98c0ee410e3ce4ab72c89a3a66d8aaa8e4d095e2ee14bc32c814caa28503cf0d099095441a60eb4cb9e11b7639f522d65
6
+ metadata.gz: dfe9da5a5cb0a8a1147846766acc79b518a9f7025d1d95fbfddce3604e2315afafc04762b339879eee2db875aba5cc296056045f213f47a17cb9f730e17f3e07
7
+ data.tar.gz: b548a4b44b001b79ee295969af5a868f50d0e847a183dc9433e5b23d4b1e7c9effed3ea64a76a8f5022db959a65394d8f8ff28bba9a6d46484841322483ebe02
@@ -1,3 +1,3 @@
1
1
  {
2
- ".": "0.44.0"
2
+ ".": "0.46.0"
3
3
  }
data/CHANGELOG.md CHANGED
@@ -5,6 +5,30 @@ 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.0](https://github.com/janeapp/riffer/compare/riffer/v0.45.0...riffer/v0.46.0) (2026-09-08)
9
+
10
+
11
+ ### ⚠ BREAKING CHANGES
12
+
13
+ * 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:.
14
+
15
+ ### Features
16
+
17
+ * report how a run ended through Response#outcome ([#425](https://github.com/janeapp/riffer/issues/425)) ([2ba2157](https://github.com/janeapp/riffer/commit/2ba2157668cd4a2b013da190dd647ae131accfa3))
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * **anthropic:** merge caller output_config with structured output ([#422](https://github.com/janeapp/riffer/issues/422)) ([e1a60e6](https://github.com/janeapp/riffer/commit/e1a60e6f082dbf7d4ee9d151e96e56bdf3f26359))
23
+ * **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))
24
+
25
+ ## [0.45.0](https://github.com/janeapp/riffer/compare/riffer/v0.44.0...riffer/v0.45.0) (2026-09-03)
26
+
27
+
28
+ ### Features
29
+
30
+ * expand finish reason vocabulary and close provider gaps ([#419](https://github.com/janeapp/riffer/issues/419)) ([daf91a6](https://github.com/janeapp/riffer/commit/daf91a61956ce8da626a590d30307e11e90d474b))
31
+
8
32
  ## [0.44.0](https://github.com/janeapp/riffer/compare/riffer/v0.43.0...riffer/v0.44.0) (2026-08-28)
9
33
 
10
34
 
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})
@@ -142,9 +141,9 @@ Works with both `generate` and `stream`. Only emits agent-generated messages (As
142
141
 
143
142
  Callbacks can interrupt the agent loop. This is useful for human-in-the-loop approval, cost limits, or content filtering.
144
143
 
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.
144
+ 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
145
 
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):
146
+ 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
147
 
149
148
  ```ruby
150
149
  agent = MyAgent.new
@@ -155,9 +154,9 @@ agent.session.on_message do |msg|
155
154
  end
156
155
 
157
156
  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
157
+ response.outcome.reason # => :interrupted
158
+ response.outcome.detail # => "needs human approval"
159
+ response.content # => last assistant content before interrupt
161
160
  ```
162
161
 
163
162
  **Streaming** — interrupts emit an `Interrupt` event:
@@ -188,7 +187,7 @@ agent.session.on_message { |msg| throw :riffer_interrupt if needs_approval?(msg)
188
187
 
189
188
  response = agent.generate('Do something risky')
190
189
 
191
- if response.interrupted?
190
+ if response.outcome.reason == :interrupted
192
191
  approve_action(agent.session.messages)
193
192
  response = agent.generate('Approved, go ahead') # executes pending tools, then calls the LLM
194
193
  # or: agent.generate # resume without a new turn
@@ -301,23 +300,49 @@ agent.context[:skills] # the Skills::Context, if skills configured
301
300
 
302
301
  ## Response Attributes
303
302
 
304
- `Riffer::Agent::Response` is returned by `generate`:
303
+ `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
304
 
306
305
  | Attribute | Type | Description |
307
306
  | ---------------------- | --------------------------- | ------------------------------------------------------------------------------------------------ |
308
307
  | `content` | `String` | The response text |
308
+ | `outcome` | `Outcome` | How the run ended — `reason` and optional `detail` (see below) |
309
309
  | `structured_output` | `Hash` / `nil` | Parsed and validated structured output (see below) |
310
- | `blocked?` | `Boolean` | `true` if a guardrail tripwire fired |
311
310
  | `tripwire` | `Tripwire` / `nil` | The guardrail tripwire that blocked the request |
312
311
  | `modified?` | `Boolean` | `true` if a guardrail modified the content |
313
312
  | `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
313
  | `messages` | `Array` | Full message history from the conversation |
317
314
  | `healed_tool_call_ids` | `Array[String]` | `tool_call` ids filled with placeholder results during interrupt healing (else `[]`) |
318
315
  | `token_usage` | `TokenUsage` / `nil` | Aggregate `Riffer::Providers::TokenUsage` across this run's LLM calls (`nil` when none reported) |
319
316
  | `steps` | `Integer` | LLM calls made during this run (`0` when a before-guardrail blocks first); not the session's cumulative count |
320
317
 
318
+ ### response.outcome
319
+
320
+ `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`.
321
+
322
+ | Reason | Source | `detail` |
323
+ | ---------------------------- | ---------------------------------------------------------- | ----------------------------------------- |
324
+ | `:completed` | The loop ended normally | `nil` |
325
+ | `:guardrail_blocked` | A guardrail tripwire fired (`tripwire` is set) | The tripwire reason |
326
+ | `:max_steps` | The `max_steps` limit was reached | `nil` |
327
+ | `:interrupted` | A callback called `interrupt!` / `throw :riffer_interrupt` | The interrupt reason, or `nil` |
328
+ | `: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` |
329
+ | `:invalid_structured_output` | The final message failed JSON parsing or schema validation | The parse or validation error |
330
+
331
+ 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`.
332
+
333
+ ```ruby
334
+ agent = MyAgent.new
335
+ response = agent.generate('Hello')
336
+
337
+ case response.outcome.reason
338
+ when :completed then puts response.content
339
+ when :guardrail_blocked then puts "Blocked: #{response.outcome.detail}"
340
+ when :interrupted, :max_steps then response = agent.generate('Continue')
341
+ when :invalid_structured_output then warn response.outcome.detail
342
+ else warn "Provider stopped early: #{response.outcome.reason}"
343
+ end
344
+ ```
345
+
321
346
  ### response.structured_output
322
347
 
323
348
  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 +353,7 @@ response.content # => raw JSON string from the LLM
328
353
  response.structured_output # => {sentiment: "positive", score: 0.95}
329
354
  ```
330
355
 
331
- Returns `nil` when structured output is not configured or when validation fails.
356
+ 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
357
 
333
358
  The assistant message in the message history stores the parsed hash, so you can access structured output directly from persisted messages:
334
359
 
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: [
@@ -83,16 +84,18 @@ The cache buckets are subsets of `input_tokens`, never additions to it — summi
83
84
 
84
85
  `finish_reason` carries the same meaning for every provider — each adapter maps its raw wire value (Anthropic's `end_turn`, OpenAI's response status, Gemini's `STOP`, …) into a normalized vocabulary:
85
86
 
86
- | Value | Meaning |
87
- | ----------------- | --------------------------------------------------------------- |
88
- | `:stop` | The model finished its turn naturally (or hit a stop sequence). |
89
- | `:length` | Output was truncated at the max-token limit. |
90
- | `:tool_calls` | The model stopped to call tools. |
91
- | `:content_filter` | A provider safety system blocked or cut the response. |
92
- | `:error` | The provider reported an error finish. |
93
- | `:other` | A provider-specific value with no normalized equivalent. |
87
+ | Value | Meaning |
88
+ | ------------------- | ----------------------------------------------------------------------------------------------------------------------- |
89
+ | `:stop` | The model finished its turn naturally (or hit a stop sequence). |
90
+ | `:length` | Output was truncated at the max-token limit. |
91
+ | `:tool_calls` | The model stopped to call tools. |
92
+ | `:content_filter` | A provider safety system blocked or cut the response. |
93
+ | `:context_window` | Input plus output hit the model's context window; trim or compact history rather than raising `max_tokens`. |
94
+ | `:malformed_output` | The model emitted output the provider could not parse, such as an invalid tool call; retry or nudge rather than backing off. |
95
+ | `:error` | The provider reported an error finish. |
96
+ | `:other` | A provider-specific value with no normalized equivalent. |
94
97
 
95
- `finish_reason` is `nil` when the provider doesn't report one. Use it 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:
96
99
 
97
100
  ```ruby
98
101
  response = agent.generate("Summarize this document")
@@ -101,7 +104,7 @@ retry_with_higher_limit if agent.session.messages.last.finish_reason == :length
101
104
 
102
105
  #### Structured Output on Messages
103
106
 
104
- 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:
105
108
 
106
109
  ```ruby
107
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
@@ -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 |
@@ -153,7 +155,7 @@ Usage on this span is the run total, aggregated across every step. See [Token us
153
155
  | `gen_ai.output.messages` | string | When `capture_messages` is on (JSON) |
154
156
  | `error.type` | string | On an unhandled exception |
155
157
 
156
- `gen_ai.response.finish_reasons` is an array of exactly one normalized value, from the fixed vocabulary `stop`, `length`, `tool_calls`, `content_filter`, `error`, `other`. When the provider's raw wire value carries more nuance than the normalized one, the raw string is preserved on `riffer.finish_reason.raw`.
158
+ `gen_ai.response.finish_reasons` is an array of exactly one normalized value, from the fixed vocabulary `stop`, `length`, `tool_calls`, `content_filter`, `context_window`, `malformed_output`, `error`, `other`. When the provider's raw wire value carries more nuance than the normalized one, the raw string is preserved on `riffer.finish_reason.raw`.
157
159
 
158
160
  ## `execute_tool {tool}` — the tool call span
159
161
 
@@ -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
 
@@ -252,7 +252,7 @@ Riffer::StreamEvents::FinishReasonDone.new(
252
252
 
253
253
  ## Finish Reasons
254
254
 
255
- `Riffer::Providers::FinishReason` is the same kind of normalized contract — map your provider's raw finish/stop value into the vocabulary defined in [Messages — Finish Reasons](../MESSAGES.md#finish-reasons) (`:stop`, `:length`, `:tool_calls`, `:content_filter`, `:error`, `:other`), keeping the raw wire value alongside:
255
+ `Riffer::Providers::FinishReason` is the same kind of normalized contract — map your provider's raw finish/stop value into the vocabulary defined in [Messages — Finish Reasons](../MESSAGES.md#finish-reasons) (`:stop`, `:length`, `:tool_calls`, `:content_filter`, `:context_window`, `:malformed_output`, `:error`, `:other`), keeping the raw wire value alongside:
256
256
 
257
257
  ```ruby
258
258
  def extract_finish_reason(response)
@@ -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