claude-agent-sdk 0.30.0 → 0.31.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -0
- data/README.md +4 -4
- data/docs/configuration.md +17 -0
- data/docs/errors.md +53 -0
- data/docs/sessions.md +42 -0
- data/docs/types.md +71 -3
- data/lib/claude_agent_sdk/command_builder.rb +24 -0
- data/lib/claude_agent_sdk/errors.rb +147 -0
- data/lib/claude_agent_sdk/message_parser.rb +38 -3
- data/lib/claude_agent_sdk/query.rb +69 -29
- data/lib/claude_agent_sdk/session_resume.rb +223 -33
- data/lib/claude_agent_sdk/sessions.rb +112 -19
- data/lib/claude_agent_sdk/types.rb +180 -0
- data/lib/claude_agent_sdk/version.rb +1 -1
- data/lib/claude_agent_sdk.rb +32 -27
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ec4f48758138c2a833ba6adc419753f8b8c1b7b2555824c5b2a497d6ab191c8a
|
|
4
|
+
data.tar.gz: 48787d59ffcbd3fa6c90b953234064065aad8cfc87ddc85d77f042d87a9bb250
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: dce3bf872a4bfe12c72248a9f036d2c706600679e9724200eef80da47a093c4e1d7be3e9ec7b895105a35f56f07dce26d8516b03c26c040fd60f8a53b594a785
|
|
7
|
+
data.tar.gz: 4a51034acf467515e897a8739b3f0dc0c85392bd88d88fe90b21251af98f78b29a7fc5877305e7136c7fa5cf9a0e5b64edd718d20794e90fbb14614619ff6246
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,32 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.31.0] - 2026-08-28
|
|
11
|
+
|
|
12
|
+
Syncs the gem with Python SDK **0.2.147** (previously 0.2.134). The intervening Python releases 0.2.135/136/138/139/141–147 only bump the CLI binary Python bundles; this gem does not vendor a CLI, so they carry no Ruby-side change.
|
|
13
|
+
|
|
14
|
+
### Added
|
|
15
|
+
- **`ConversationResetMessage`** (port of Python [#1196](https://github.com/anthropics/claude-agent-sdk-python/pull/1196), v0.2.137). The CLI announces a mid-session transcript discard (`/clear`, and any other flow that replaces the conversation without ending the connection) with a top-level `conversation_reset` frame. It was previously dropped by the parser's forward-compatibility fallthrough, so applications never saw resets — including ones they did not initiate. Carries `new_conversation_id`, `uuid` and `session_id`. A reset also **zeroes the running totals** on subsequent `ResultMessage`s (`total_cost_usd` and friends), so snapshot them when this arrives. `new_conversation_id` is not the next `session_id`; read that from the following message.
|
|
16
|
+
- **Compatibility:** this widens the `Message` union with a frame that was previously dropped silently. Code that raises on an unrecognized message class (`case msg ... else raise`) will now see it — on the first `/clear` of a long-lived session.
|
|
17
|
+
- **`origin` on `UserMessage` and `ResultMessage`** (port of Python [#1199](https://github.com/anthropics/claude-agent-sdk-python/pull/1199), v0.2.137). In streaming/`Client` mode one connection interleaves the turns the application sends with turns the session injects on its own — background-task notifications, fired scheduled-task prompts, MCP channel messages, messages relayed from peer sessions. `origin` distinguishes them, so a consumer can tell "this result answers my prompt" from a task-notification follow-up. The CLI's object is passed through **verbatim** — including keys this version does not model, so newer origin kinds stay visible — and anything that is not an object with a String `kind` reads as `nil`. **Keys are Symbols and non-`kind` keys keep the CLI's camelCase spelling** (`origin[:fromSession]`), unlike the Python SDK's string-keyed equivalent. Prompts sent through `query()` / `Client#query` arrive unattributed unless the host stamps `origin: { kind: 'human' }` itself.
|
|
18
|
+
- **`ClaudeAgentOptions#forward_subagent_text`** (port of Python [#1206](https://github.com/anthropics/claude-agent-sdk-python/pull/1206), v0.2.140). Forwards a subagent's text and thinking blocks as messages in the stream, not just its `tool_use` / `tool_result` blocks, so consumers can render the full nested transcript. Negotiated on the control-protocol handshake, so it applies to both `query()` and `Client`. Matches the TypeScript SDK's `forwardSubagentText`.
|
|
19
|
+
- **`ClaudeAgentOptions#resume_drops_turn`** (port of Python [#1198](https://github.com/anthropics/claude-agent-sdk-python/pull/1198), v0.2.137). Completes the truncating-resume pair alongside the existing `resume_session_at`: names the user prompt whose turn a truncating resume intends to discard, and the CLI refuses the resume if anything past the fork point is not attributable to that turn — so a caller can rewind to "before my last prompt" without silently dropping a queued message or task notification the session absorbed mid-turn and the caller never observed. A refusal surfaces as a `ResultError` whose message contains `Resume rejected by --resume-drops-turn:`; treat it as deterministic rather than retrying. Forwarded in equals form, and an empty string is forwarded rather than dropped so the CLI rejects it as malformed instead of the SDK silently disarming a guard the caller believes is armed. As in the TypeScript and Python SDKs, no SDK-side validation of the option combination is applied.
|
|
20
|
+
- **`ClaudeAgentSDK::ResultError`** (port of Python [#1205](https://github.com/anthropics/claude-agent-sdk-python/pull/1205), v0.2.140). When a run fails, the CLI emits a `result` with `is_error: true` and then exits non-zero on purpose, for shell-script consumers; the trailing failure carried nothing beyond "exit code 1". The SDK now raises a typed `ResultError` carrying `subtype`, `errors`, `result`, `api_error_status`, `terminal_reason`, `session_id`, the raw `data`, and `original_error` (the bare exit failure it replaced), so callers can branch on *why* a run failed without string matching. It **subclasses `ProcessError`**, so existing `rescue ProcessError` handlers keep working — rescue `ResultError` first to reach the structured fields.
|
|
21
|
+
- **`SessionMessage#parent_agent_id`**, and `parent_tool_use_id` is now populated for subagent transcripts (port of Python [#1207](https://github.com/anthropics/claude-agent-sdk-python/pull/1207), v0.2.140). `get_subagent_messages` and `get_subagent_messages_from_store` previously returned `parent_tool_use_id: nil` for every message, losing the link to the Agent `tool_use` block in the parent session that spawned the subagent. Both are now recovered from the `agent-<id>.meta.json` sidecar (or the `agent_metadata` entry in a `SessionStore`), and stay `nil` when it is missing or unusable.
|
|
22
|
+
|
|
23
|
+
### Fixed
|
|
24
|
+
- **A refused or failed resume no longer reports a bare "exit code 1" to in-flight control requests** (port of Python [#1198](https://github.com/anthropics/claude-agent-sdk-python/pull/1198)). The CLI reports a rejected resume as an error result on stdout and then exits **before** answering the SDK's `initialize`. The read loop already replaced the generic process failure with the CLI's own error text for the message stream, but pending control requests — including that in-flight handshake — were signalled with the raw exception first, so callers saw `Command failed with exit code 1` with the actual reason discarded. Pending requests now receive the same enriched `ResultError`. This also improves resuming a nonexistent session, which takes the same path.
|
|
25
|
+
- **`can_use_tool` no longer has its permission requests cut off when it is the only bidirectional feature in use.** The check deciding whether the CLI may still send control requests needing a reply considered only SDK MCP servers and hooks, so an `Enumerator` prompt using `can_use_tool` with neither of those closed stdin as soon as the input ended; any later permission `control_request` then failed CLI-side with "Stream closed". This was a live defect on an already-supported path, independent of the string-prompt change below.
|
|
26
|
+
- **Store-backed resume now seeds the caller's `settings.json` and `cowork_settings.json`** into the temporary config directory (port of Python [#1197](https://github.com/anthropics/claude-agent-sdk-python/pull/1197), v0.2.137). Only `.credentials.json` and `.claude.json` were copied, leaving behind `apiKeyHelper` — a fourth authentication mechanism alongside the credentials file, the macOS Keychain and environment variables — plus the user's `env`, `hooks` and `permissions`. A host authenticating solely via `apiKeyHelper` therefore failed with **"Not logged in"** the moment it resumed from a store, with nothing in the error pointing at why. Both files pass through a transform dropping `enabledPlugins` / `extraKnownMarketplaces` (which would reconcile against the always-empty temporary plugin cache and network-install every declared marketplace on each resume) and `env.CLAUDE_CONFIG_DIR` (which would point the subprocess's config reads back out of the temporary directory). A UTF-8 BOM is tolerated, content that is not a JSON object is copied through byte-for-byte, and the result is written `0600`.
|
|
27
|
+
- **A seed file that cannot be read no longer aborts an otherwise-valid resume.** These files are best-effort enrichment of the temporary config directory: a failure other than "not found" is now logged and skipped (removing any partial destination) rather than propagating. Readers also check for a regular file before opening, so a directory or a FIFO in place of a config file is skipped instead of raising — or, for a FIFO, hanging the resume forever.
|
|
28
|
+
- **An unusable subagent metadata sidecar no longer breaks every later resume.** `JSON.parse` is lenient about illegal bytes inside an otherwise well-formed UTF-8-tagged document, returning a Hash holding invalidly-encoded values that only fail later, at generate time. Session import persisted such a Hash as an `agent_metadata` entry, and every subsequent resume through that store then died re-serializing it with an opaque `JSON::GeneratorError` — permanently, until the store entry was repaired externally. Unusable bytes are now treated as an absent sidecar, matching the helper's documented contract. The same regular-file guard prevents a FIFO sidecar from hanging `get_subagent_messages`.
|
|
29
|
+
- **`settings.json` containing a lone surrogate escape is no longer copied through unstripped.** Ruby's `JSON.parse` rejects lone surrogates outright, which fell back to the byte-for-byte passthrough — so `enabledPlugins` survived and the resumed CLI went on network-installing marketplaces on every resume, the exact misbehavior the seeding transform exists to prevent.
|
|
30
|
+
- **Redacting `.credentials.json` no longer aborts a resume on a serialization failure**, and its comment now describes what the code actually does; the rescue covered only parse failures, so a generate failure propagated and took down a resume that every other seed-file path is designed to survive.
|
|
31
|
+
|
|
32
|
+
### Changed
|
|
33
|
+
- **`can_use_tool` now works with a String prompt** (port of Python [#1204](https://github.com/anthropics/claude-agent-sdk-python/pull/1204), v0.2.140). The callback previously required an `Enumerator` prompt and raised `ArgumentError` otherwise. The SDK is always streaming internally, so the restriction was unnecessary once stdin is held open for the permission round-trip. Validation of the `can_use_tool` / `permission_prompt_tool_name` conflict and the shadowing advisory are now shared by `query()` and `Client#connect` instead of duplicated.
|
|
34
|
+
|
|
35
|
+
|
|
10
36
|
## [0.30.0] - 2026-08-09
|
|
11
37
|
|
|
12
38
|
### Added
|
data/README.md
CHANGED
|
@@ -25,7 +25,7 @@ All three SDKs share the same underlying mechanism: they spawn the `claude` CLI
|
|
|
25
25
|
| Hooks (all 27 events) | ✅ | ✅ | ✅ |
|
|
26
26
|
| Permission callbacks | ✅ | ✅ | ✅ |
|
|
27
27
|
| Structured output | ✅ | ✅ | ✅ |
|
|
28
|
-
| All
|
|
28
|
+
| All 25 message types | ✅ | partial | ✅ |
|
|
29
29
|
| [Sandbox](https://github.com/anthropic-experimental/sandbox-runtime) settings | ✅ | partial | ✅ |
|
|
30
30
|
| Bare mode (`--bare`) | ✅ | ✅ | ✅ |
|
|
31
31
|
| File checkpointing & rewind | ✅ | ✅ | ✅ |
|
|
@@ -36,7 +36,7 @@ All three SDKs share the same underlying mechanism: they spawn the `claude` CLI
|
|
|
36
36
|
| Custom transport (pluggable I/O) | — | — | ✅ |
|
|
37
37
|
| Rails integration | — | — | ✅ |
|
|
38
38
|
|
|
39
|
-
**Where Ruby goes further:** Built-in OpenTelemetry observer with Langfuse flow diagram support — no third-party instrumentation library needed. Custom transport support lets you swap the subprocess for any I/O layer (e.g., connect to a remote Claude Code instance over SSH or a container). Rails integration provides a `configure` block for initializers with thread-safe observer factories, and plays well with ActionCable for real-time streaming. Full typed coverage for all
|
|
39
|
+
**Where Ruby goes further:** Built-in OpenTelemetry observer with Langfuse flow diagram support — no third-party instrumentation library needed. Custom transport support lets you swap the subprocess for any I/O layer (e.g., connect to a remote Claude Code instance over SSH or a container). Rails integration provides a `configure` block for initializers with thread-safe observer factories, and plays well with ActionCable for real-time streaming. Full typed coverage for all 25 CLI message types and all 27 hook events.
|
|
40
40
|
|
|
41
41
|
**What's missing:** The Ruby gem does not bundle the `claude` CLI binary (`npm install -g @anthropic-ai/claude-code`).
|
|
42
42
|
|
|
@@ -68,7 +68,7 @@ Add this line to your application's Gemfile:
|
|
|
68
68
|
gem 'claude-agent-sdk', github: 'ya-luotao/claude-agent-sdk-ruby'
|
|
69
69
|
|
|
70
70
|
# Or use a stable version from RubyGems
|
|
71
|
-
gem 'claude-agent-sdk', '~> 0.
|
|
71
|
+
gem 'claude-agent-sdk', '~> 0.31.0'
|
|
72
72
|
```
|
|
73
73
|
|
|
74
74
|
Then `bundle install`, or install directly: `gem install claude-agent-sdk`.
|
|
@@ -272,7 +272,7 @@ options = ClaudeAgentSDK::ClaudeAgentOptions.new(
|
|
|
272
272
|
|---------|-------------|
|
|
273
273
|
| [quick_start.rb](https://github.com/ya-luotao/claude-agent-sdk-ruby/blob/main/examples/quick_start.rb) | Basic `query()` usage with options |
|
|
274
274
|
| [client_example.rb](https://github.com/ya-luotao/claude-agent-sdk-ruby/blob/main/examples/client_example.rb) | Interactive Client usage |
|
|
275
|
-
| [message_types_example.rb](https://github.com/ya-luotao/claude-agent-sdk-ruby/blob/main/examples/message_types_example.rb) | Handling all
|
|
275
|
+
| [message_types_example.rb](https://github.com/ya-luotao/claude-agent-sdk-ruby/blob/main/examples/message_types_example.rb) | Handling all 25 SDK message types |
|
|
276
276
|
| [streaming_input_example.rb](https://github.com/ya-luotao/claude-agent-sdk-ruby/blob/main/examples/streaming_input_example.rb) | Streaming input for multi-turn conversations |
|
|
277
277
|
| [session_resumption_example.rb](https://github.com/ya-luotao/claude-agent-sdk-ruby/blob/main/examples/session_resumption_example.rb) | Multi-turn conversations with session persistence |
|
|
278
278
|
| [structured_output_example.rb](https://github.com/ya-luotao/claude-agent-sdk-ruby/blob/main/examples/structured_output_example.rb) | JSON schema structured output |
|
data/docs/configuration.md
CHANGED
|
@@ -223,6 +223,23 @@ options = ClaudeAgentSDK::ClaudeAgentOptions.new(
|
|
|
223
223
|
|
|
224
224
|
See [examples/bare_mode_example.rb](https://github.com/ya-luotao/claude-agent-sdk-ruby/blob/main/examples/bare_mode_example.rb).
|
|
225
225
|
|
|
226
|
+
## Forwarding Subagent Text
|
|
227
|
+
|
|
228
|
+
By default only `tool_use` / `tool_result` blocks from subagents (spawned via
|
|
229
|
+
the Agent tool) reach the message stream, as `AssistantMessage` /
|
|
230
|
+
`UserMessage` objects whose `parent_tool_use_id` is the spawning Agent
|
|
231
|
+
`tool_use` id — enough for a progress heartbeat. Set `forward_subagent_text`
|
|
232
|
+
to forward the subagent's **text and thinking** blocks the same way, so you
|
|
233
|
+
can render the full nested transcript:
|
|
234
|
+
|
|
235
|
+
```ruby
|
|
236
|
+
options = ClaudeAgentSDK::ClaudeAgentOptions.new(forward_subagent_text: true)
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Matches the TypeScript SDK's `forwardSubagentText`. The capability is
|
|
240
|
+
negotiated on the control-protocol handshake, so it applies to both
|
|
241
|
+
`ClaudeAgentSDK.query` and `Client`.
|
|
242
|
+
|
|
226
243
|
## File Checkpointing & Rewind
|
|
227
244
|
|
|
228
245
|
Enable file checkpointing to revert file changes to a previous state:
|
data/docs/errors.md
CHANGED
|
@@ -31,6 +31,12 @@ rescue ClaudeAgentSDK::ControlRequestTimeoutError
|
|
|
31
31
|
puts "Control protocol timed out — consider increasing the timeout"
|
|
32
32
|
rescue ClaudeAgentSDK::CLINotFoundError
|
|
33
33
|
puts "Please install Claude Code"
|
|
34
|
+
rescue ClaudeAgentSDK::ResultError => e
|
|
35
|
+
# More specific than ProcessError — must be rescued first.
|
|
36
|
+
case e.terminal_reason
|
|
37
|
+
when 'api_error' then puts "API failed (HTTP #{e.api_error_status}): #{e.result}"
|
|
38
|
+
else puts "Run failed (#{e.subtype}): #{e.errors.join('; ')}"
|
|
39
|
+
end
|
|
34
40
|
rescue ClaudeAgentSDK::ProcessError => e
|
|
35
41
|
puts "Process failed with exit code: #{e.exit_code}"
|
|
36
42
|
rescue ClaudeAgentSDK::CLIJSONDecodeError => e
|
|
@@ -38,6 +44,40 @@ rescue ClaudeAgentSDK::CLIJSONDecodeError => e
|
|
|
38
44
|
end
|
|
39
45
|
```
|
|
40
46
|
|
|
47
|
+
## Terminal Error Results
|
|
48
|
+
|
|
49
|
+
When a run fails, the CLI emits a `result` message with `is_error: true` (which
|
|
50
|
+
you still receive as a `ResultMessage`) and *then* exits non-zero on purpose,
|
|
51
|
+
for shell-script consumers. That trailing process failure carries nothing
|
|
52
|
+
beyond "exit code 1", so the SDK replaces it with a `ResultError` carrying the
|
|
53
|
+
payload the CLI already reported — you can branch on *why* the run failed
|
|
54
|
+
without matching on strings:
|
|
55
|
+
|
|
56
|
+
```ruby
|
|
57
|
+
begin
|
|
58
|
+
ClaudeAgentSDK.query(prompt: "...") { |m| handle(m) }
|
|
59
|
+
rescue ClaudeAgentSDK::ResultError => e
|
|
60
|
+
retry_later if e.terminal_reason == 'api_error' # overloaded / timeout
|
|
61
|
+
widen_budget if e.subtype == 'error_max_turns'
|
|
62
|
+
raise
|
|
63
|
+
end
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`ResultError` subclasses `ProcessError`, so existing `rescue ProcessError`
|
|
67
|
+
handlers keep working unchanged — but rescue `ResultError` **first** if you
|
|
68
|
+
want the structured fields.
|
|
69
|
+
|
|
70
|
+
The exception message prefers, in order: the CLI's `errors[]`, then `result`,
|
|
71
|
+
then a non-`success` `subtype`, then the HTTP status. A run that ends on an API
|
|
72
|
+
failure arrives as `subtype: "success"` with `is_error: true` and the prose in
|
|
73
|
+
`result`, which is why `subtype` alone is not used as the message.
|
|
74
|
+
|
|
75
|
+
A refused resume (a nonexistent session, or a `resume_drops_turn` guard
|
|
76
|
+
failure) reaches you the same way — including on a control request such as the
|
|
77
|
+
initial handshake that was still in flight when the CLI exited. Match on
|
|
78
|
+
`Resume rejected by --resume-drops-turn:` in the message and treat it as
|
|
79
|
+
deterministic: clear the fork target and resume plainly rather than retrying.
|
|
80
|
+
|
|
41
81
|
## Configuring Timeout
|
|
42
82
|
|
|
43
83
|
The control request timeout defaults to **1200 seconds** (20 minutes) to accommodate long-running agent sessions. Override it via environment variable:
|
|
@@ -70,6 +110,19 @@ class ProcessError < ClaudeSDKError
|
|
|
70
110
|
:stderr # String | nil
|
|
71
111
|
end
|
|
72
112
|
|
|
113
|
+
# Raised when the CLI exits after reporting a terminal error result.
|
|
114
|
+
# Subclasses ProcessError, so existing `rescue ProcessError` keeps working.
|
|
115
|
+
class ResultError < ProcessError
|
|
116
|
+
attr_reader :subtype, # String | nil ('error_max_turns', 'error_during_execution', ...)
|
|
117
|
+
:errors, # Array<String> - error strings from the CLI (may be empty)
|
|
118
|
+
:result, # String | nil - result text; holds the "API Error: ..." prose
|
|
119
|
+
:api_error_status, # Integer | nil - HTTP status of the failing API call
|
|
120
|
+
:terminal_reason, # String | nil - why the run ended ('api_error', 'max_turns', ...)
|
|
121
|
+
:session_id, # String | nil
|
|
122
|
+
:data, # Hash - raw `result` payload as emitted by the CLI
|
|
123
|
+
:original_error # ProcessError | nil - the bare exit error this replaced
|
|
124
|
+
end
|
|
125
|
+
|
|
73
126
|
# Raised when JSON parsing fails
|
|
74
127
|
class CLIJSONDecodeError < ClaudeSDKError
|
|
75
128
|
attr_reader :line, # String - The line that failed to parse
|
data/docs/sessions.md
CHANGED
|
@@ -49,6 +49,8 @@ messages = ClaudeAgentSDK.get_subagent_messages(session_id: "uuid-here", agent_i
|
|
|
49
49
|
|
|
50
50
|
With `directory:` given, only that project and its git worktrees are searched (no global fallback). Store-backed counterparts: `list_subagents_from_store` / `get_subagent_messages_from_store`.
|
|
51
51
|
|
|
52
|
+
> Each returned `SessionMessage` carries `parent_tool_use_id` — the id of the Agent `tool_use` block in the parent session that spawned this subagent — and `parent_agent_id`, the spawning subagent's id for nested subagents. Both are read from the `agent-<id>.meta.json` sidecar beside the transcript (or the `agent_metadata` entry in a `SessionStore`), and are `nil` when it is missing or unusable.
|
|
53
|
+
|
|
52
54
|
## Renaming a Session
|
|
53
55
|
|
|
54
56
|
```ruby
|
|
@@ -113,6 +115,46 @@ ClaudeAgentSDK.query(
|
|
|
113
115
|
|
|
114
116
|
`resume_session_at` requires `resume`; the SDK raises `ArgumentError` from `CommandBuilder` when this constraint is violated, matching the underlying CLI's validation but surfacing it synchronously in the caller's stack.
|
|
115
117
|
|
|
118
|
+
### Validating what the truncation discards
|
|
119
|
+
|
|
120
|
+
A bare `resume_session_at` silently drops everything after the fork point —
|
|
121
|
+
including a queued user message or a task notification the session absorbed
|
|
122
|
+
mid-turn that you never observed. `resume_drops_turn` names the user prompt
|
|
123
|
+
whose turn you *intend* to discard, and the CLI refuses the resume if anything
|
|
124
|
+
past the fork point is not attributable to that turn:
|
|
125
|
+
|
|
126
|
+
```ruby
|
|
127
|
+
ClaudeAgentSDK.query(
|
|
128
|
+
prompt: 'try a different approach',
|
|
129
|
+
options: ClaudeAgentSDK::ClaudeAgentOptions.new(
|
|
130
|
+
resume: session_id,
|
|
131
|
+
resume_session_at: last_kept_entry_uuid,
|
|
132
|
+
resume_drops_turn: discarded_prompt_uuid
|
|
133
|
+
)
|
|
134
|
+
) { |m| handle(m) }
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Rule of thumb: set `resume_session_at` to the **last transcript entry of the
|
|
138
|
+
turn you are keeping** (whatever its type), and `resume_drops_turn` to the
|
|
139
|
+
prompt UUID of the turn immediately after it — the next `SessionMessage` with
|
|
140
|
+
`type == 'user'` from `get_session_messages`, or the `uuid` you supplied on a
|
|
141
|
+
streamed user message.
|
|
142
|
+
|
|
143
|
+
With structured output (`output_format`) or end-turn MCP tools, a kept turn
|
|
144
|
+
ends on entries *after* its last assistant message, so forking at the assistant
|
|
145
|
+
UUID is refused by design.
|
|
146
|
+
|
|
147
|
+
A refusal surfaces as a `ResultError` whose message contains
|
|
148
|
+
`Resume rejected by --resume-drops-turn:`. Treat it as **deterministic** —
|
|
149
|
+
clear the pending fork target and resume plainly; do not retry the same
|
|
150
|
+
request. Leave `resume_drops_turn` unset to keep the unvalidated behavior.
|
|
151
|
+
|
|
152
|
+
Unlike `resume_session_at`, the SDK applies no combination validation to
|
|
153
|
+
`resume_drops_turn` and defers entirely to the CLI. An empty string is
|
|
154
|
+
forwarded rather than dropped, so the CLI rejects it as a malformed
|
|
155
|
+
declaration instead of the SDK silently disarming a guard you believe is
|
|
156
|
+
armed.
|
|
157
|
+
|
|
116
158
|
## Mirroring to a `SessionStore`
|
|
117
159
|
|
|
118
160
|
By default Claude Code writes session transcripts to local disk under
|
data/docs/types.md
CHANGED
|
@@ -6,7 +6,8 @@ See [lib/claude_agent_sdk/types.rb](https://github.com/ya-luotao/claude-agent-sd
|
|
|
6
6
|
|
|
7
7
|
```ruby
|
|
8
8
|
# Union type of all possible messages
|
|
9
|
-
Message = UserMessage | AssistantMessage | SystemMessage | ResultMessage
|
|
9
|
+
Message = UserMessage | AssistantMessage | SystemMessage | ResultMessage |
|
|
10
|
+
StreamEvent | RateLimitEvent | ConversationResetMessage
|
|
10
11
|
```
|
|
11
12
|
|
|
12
13
|
### UserMessage
|
|
@@ -18,7 +19,8 @@ class UserMessage
|
|
|
18
19
|
attr_accessor :content, # String | Array<ContentBlock>
|
|
19
20
|
:uuid, # String | nil - Unique ID for rewind support
|
|
20
21
|
:parent_tool_use_id, # String | nil
|
|
21
|
-
:tool_use_result
|
|
22
|
+
:tool_use_result, # Hash | nil - Tool result data when message is a tool response
|
|
23
|
+
:origin # Hash | nil - message provenance (see Message Origin below)
|
|
22
24
|
end
|
|
23
25
|
```
|
|
24
26
|
|
|
@@ -91,7 +93,8 @@ class ResultMessage
|
|
|
91
93
|
:uuid, # String | nil
|
|
92
94
|
:fast_mode_state, # String | nil ('off', 'cooldown', 'on')
|
|
93
95
|
:api_error_status, # Integer | nil (HTTP status on api_error subtype)
|
|
94
|
-
:terminal_reason
|
|
96
|
+
:terminal_reason, # String | nil (see below)
|
|
97
|
+
:origin # Hash | nil - origin of the triggering user message (see below)
|
|
95
98
|
end
|
|
96
99
|
```
|
|
97
100
|
|
|
@@ -109,6 +112,71 @@ optional `canonicalModel` (canonical id used for the pricing lookup, which can
|
|
|
109
112
|
differ from the raw model-string key for provider-specific ids/aliases) and
|
|
110
113
|
`provider` (`'firstParty'`, `'bedrock'`, `'vertex'`, ...).
|
|
111
114
|
|
|
115
|
+
## Message Origin
|
|
116
|
+
|
|
117
|
+
`UserMessage#origin` and `ResultMessage#origin` carry the provenance of a
|
|
118
|
+
user-role turn. In streaming/`Client` mode one connection interleaves the turns
|
|
119
|
+
your application sends with turns the session injects on its own — background
|
|
120
|
+
task notifications, fired scheduled-task prompts, MCP channel messages,
|
|
121
|
+
messages relayed from peer sessions. `origin` tells them apart:
|
|
122
|
+
|
|
123
|
+
```ruby
|
|
124
|
+
if result.origin.nil? || result.origin[:kind] == 'human'
|
|
125
|
+
# a turn this application submitted
|
|
126
|
+
elsif result.origin[:kind] == 'task-notification'
|
|
127
|
+
# follow-up turn driven by a background task
|
|
128
|
+
end
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
The Hash is passed through from the CLI **verbatim**, so:
|
|
132
|
+
|
|
133
|
+
- **Keys are Symbols**, and non-`kind` keys keep the CLI's camelCase spelling —
|
|
134
|
+
`origin[:fromSession]`, `origin[:senderTaskId]`, `origin[:verifiedPeerPid]`.
|
|
135
|
+
(The Python SDK's equivalent is string-keyed; do not port `origin["kind"]`
|
|
136
|
+
literally.)
|
|
137
|
+
- Keys this SDK version does not model still reach you, so newer CLI origin
|
|
138
|
+
kinds stay visible.
|
|
139
|
+
- Anything that is not an object with a String `kind` reads as `nil`.
|
|
140
|
+
|
|
141
|
+
Only `kind` is always present. Known kinds — treat anything unrecognized as
|
|
142
|
+
"not human":
|
|
143
|
+
|
|
144
|
+
`human`, `channel`, `peer`, `task-notification`, `coordinator`,
|
|
145
|
+
`unclassified`, `observer`, `auto-continuation`, `observer-activity`
|
|
146
|
+
|
|
147
|
+
For `kind == 'task-notification'`, `origin[:subkind]` may be
|
|
148
|
+
`scheduled-trigger` (a scheduled task's prompt fired) or `peer-send-message`
|
|
149
|
+
(a message from another of your sessions); it is absent for ordinary
|
|
150
|
+
background-task notifications.
|
|
151
|
+
|
|
152
|
+
`nil` means the CLI did not attribute the message. Prompts you send through
|
|
153
|
+
`ClaudeAgentSDK.query` or `Client#query` arrive that way unless you stamp
|
|
154
|
+
`origin: { kind: 'human' }` on the message Hash yourself — only the `human`
|
|
155
|
+
kind is honored from an SDK host. Tool-result messages never carry an origin.
|
|
156
|
+
|
|
157
|
+
### ConversationResetMessage
|
|
158
|
+
|
|
159
|
+
Emitted when the session's conversation is replaced without ending the
|
|
160
|
+
connection — after `/clear`, or any other flow that discards the transcript
|
|
161
|
+
mid-session.
|
|
162
|
+
|
|
163
|
+
```ruby
|
|
164
|
+
class ConversationResetMessage
|
|
165
|
+
attr_accessor :new_conversation_id, # String - id of the fresh conversation
|
|
166
|
+
:uuid, # String - unique ID of this message
|
|
167
|
+
:session_id # String - the session that was reset
|
|
168
|
+
end
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
A reset clears the conversation history **and zeroes the running totals**
|
|
172
|
+
reported on subsequent `ResultMessage` objects (`total_cost_usd`, and the
|
|
173
|
+
rest). If you accumulate those across a long-lived session, snapshot them when
|
|
174
|
+
this message arrives.
|
|
175
|
+
|
|
176
|
+
`new_conversation_id` is **not** the `session_id` of subsequent messages — it
|
|
177
|
+
is an opaque id for keying an empty transcript in a UI (and for discarding a
|
|
178
|
+
cached session title). Read the new session id from the next message.
|
|
179
|
+
|
|
112
180
|
## Content Block Types
|
|
113
181
|
|
|
114
182
|
```ruby
|
|
@@ -222,6 +222,7 @@ module ClaudeAgentSDK
|
|
|
222
222
|
# flags. The equals form always binds the value to its flag.
|
|
223
223
|
cmd.push("--resume=#{@options.resume}") if @options.resume
|
|
224
224
|
append_resume_session_at(cmd)
|
|
225
|
+
append_resume_drops_turn(cmd)
|
|
225
226
|
cmd.push("--session-id=#{@options.session_id}") if @options.session_id
|
|
226
227
|
end
|
|
227
228
|
|
|
@@ -240,6 +241,29 @@ module ClaudeAgentSDK
|
|
|
240
241
|
cmd.push("--resume-session-at=#{@options.resume_session_at}")
|
|
241
242
|
end
|
|
242
243
|
|
|
244
|
+
# `--resume-drops-turn=<prompt-uuid>` declares, alongside
|
|
245
|
+
# `--resume-session-at`, which user prompt's turn this truncating resume
|
|
246
|
+
# intends to discard. The CLI validates at load time that every transcript
|
|
247
|
+
# entry after the fork point is attributable to that turn and refuses the
|
|
248
|
+
# resume otherwise — so a caller can rewind to "before my last prompt"
|
|
249
|
+
# without silently dropping a queued message or task notification the
|
|
250
|
+
# session absorbed mid-turn that the caller never observed. A refusal
|
|
251
|
+
# surfaces as an `error_during_execution` result whose message starts with
|
|
252
|
+
# `Resume rejected by --resume-drops-turn:`.
|
|
253
|
+
#
|
|
254
|
+
# No SDK-side validation of the option combination (resume /
|
|
255
|
+
# resume_session_at): like the TypeScript and Python SDKs this defers to
|
|
256
|
+
# the CLI.
|
|
257
|
+
def append_resume_drops_turn(cmd)
|
|
258
|
+
# `.nil?`, not truthiness: an empty string is forwarded so the CLI
|
|
259
|
+
# rejects it as a malformed declaration. Dropping it here would silently
|
|
260
|
+
# disarm the guard the caller believes is armed.
|
|
261
|
+
return if @options.resume_drops_turn.nil?
|
|
262
|
+
|
|
263
|
+
# Equals form for the same reason as --resume above.
|
|
264
|
+
cmd.push("--resume-drops-turn=#{@options.resume_drops_turn}")
|
|
265
|
+
end
|
|
266
|
+
|
|
243
267
|
# Sandbox gating is `!nil?` throughout — Python's `sandbox is not None`.
|
|
244
268
|
# Booleans and {} are forwarded verbatim: an explicit `sandbox: false`
|
|
245
269
|
# must reach the CLI so it can override a sandbox enabled in the
|
|
@@ -38,6 +38,153 @@ module ClaudeAgentSDK
|
|
|
38
38
|
end
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
+
# Raised when the CLI exits after reporting a terminal error result.
|
|
42
|
+
#
|
|
43
|
+
# The CLI ends a failed run by emitting a +result+ message with
|
|
44
|
+
# +is_error: true+ (yielded to you as a ResultMessage) and *then* exiting
|
|
45
|
+
# non-zero, on purpose, for shell-script consumers. This exception replaces
|
|
46
|
+
# the bare "exit code 1" ProcessError for that case and carries the
|
|
47
|
+
# result's payload, so callers can branch on *why* the run failed without
|
|
48
|
+
# string matching:
|
|
49
|
+
#
|
|
50
|
+
# begin
|
|
51
|
+
# ClaudeAgentSDK.query(prompt: '...') { |message| ... }
|
|
52
|
+
# rescue ClaudeAgentSDK::ResultError => e
|
|
53
|
+
# if e.terminal_reason == 'api_error' # e.g. overloaded / timeout
|
|
54
|
+
# retry_later
|
|
55
|
+
# elsif e.subtype == 'error_max_turns'
|
|
56
|
+
# ...
|
|
57
|
+
# end
|
|
58
|
+
# end
|
|
59
|
+
#
|
|
60
|
+
# It subclasses ProcessError, so existing +rescue ProcessError+ handlers
|
|
61
|
+
# keep working.
|
|
62
|
+
#
|
|
63
|
+
# Every structured field is type-narrowed: a payload whose +subtype+ is not
|
|
64
|
+
# a String (or whose +api_error_status+ is not an Integer, ...) reads back
|
|
65
|
+
# as nil rather than leaking the raw value, so callers can branch on these
|
|
66
|
+
# without re-validating. #data always holds the payload as the CLI sent it.
|
|
67
|
+
class ResultError < ProcessError
|
|
68
|
+
# The result subtype ("error_max_turns", "error_during_execution", ... —
|
|
69
|
+
# or "success" when the agent loop itself completed but the last turn was
|
|
70
|
+
# an API error).
|
|
71
|
+
attr_reader :subtype
|
|
72
|
+
|
|
73
|
+
# Error strings reported by the CLI (may be empty). Normalized the same
|
|
74
|
+
# way the exception text is built, so the two never disagree.
|
|
75
|
+
attr_reader :errors
|
|
76
|
+
|
|
77
|
+
# The result text, if any. For API failures this holds the
|
|
78
|
+
# "API Error: ..." prose.
|
|
79
|
+
attr_reader :result
|
|
80
|
+
|
|
81
|
+
# HTTP status of the failing API call, if any.
|
|
82
|
+
attr_reader :api_error_status
|
|
83
|
+
|
|
84
|
+
# Why the run ended (e.g. "api_error", "max_turns"), if reported.
|
|
85
|
+
attr_reader :terminal_reason
|
|
86
|
+
|
|
87
|
+
# Session the result belongs to, if reported.
|
|
88
|
+
attr_reader :session_id
|
|
89
|
+
|
|
90
|
+
# The raw +result+ message payload as emitted by the CLI.
|
|
91
|
+
attr_reader :data
|
|
92
|
+
|
|
93
|
+
# The ProcessError this replaced (the bare "exit code 1" exit).
|
|
94
|
+
#
|
|
95
|
+
# Ruby only populates #cause for an exception raised inside a rescue
|
|
96
|
+
# block; the read loop hands this one to the message queue instead of
|
|
97
|
+
# raising it there, so #cause is nil and the original exit error would be
|
|
98
|
+
# lost without an explicit accessor. Mirrors Python's __cause__ chaining.
|
|
99
|
+
attr_reader :original_error
|
|
100
|
+
|
|
101
|
+
# Reading a `result` payload: shared by the structured attributes below
|
|
102
|
+
# and by .error_text, which is the whole point — the exception's fields
|
|
103
|
+
# and its message are derived from the same normalization, so they can
|
|
104
|
+
# never disagree. Private to ResultError (Python keeps the equivalent
|
|
105
|
+
# helpers module-private as _normalize_result_errors); callers outside
|
|
106
|
+
# go through .error_text.
|
|
107
|
+
module Payload
|
|
108
|
+
module_function
|
|
109
|
+
|
|
110
|
+
# Normalize the +errors+ field of a +result+ frame to clean strings.
|
|
111
|
+
#
|
|
112
|
+
# The CLI emits an Array of Strings; tolerate a bare String (older or
|
|
113
|
+
# buggy emitters), treat anything else as empty, and drop non-String or
|
|
114
|
+
# blank entries.
|
|
115
|
+
def normalize_errors(raw)
|
|
116
|
+
raw = [raw] if raw.is_a?(String)
|
|
117
|
+
return [] unless raw.is_a?(Array)
|
|
118
|
+
|
|
119
|
+
raw.filter_map { |e| e.strip if e.is_a?(String) && !e.strip.empty? }
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Read a payload field, tolerating both key forms.
|
|
123
|
+
#
|
|
124
|
+
# Wire messages reach the SDK with symbolized keys, but a payload
|
|
125
|
+
# reconstructed by a caller (or replayed from JSON.parse without
|
|
126
|
+
# symbolize_names) uses Strings.
|
|
127
|
+
def field(data, key)
|
|
128
|
+
return nil unless data.is_a?(Hash)
|
|
129
|
+
|
|
130
|
+
data.key?(key) ? data[key] : data[key.to_s]
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
private_constant :Payload
|
|
134
|
+
|
|
135
|
+
# Pick the most informative text from a `result` frame with is_error.
|
|
136
|
+
#
|
|
137
|
+
# Terminal errors the CLI raises itself (error_max_turns,
|
|
138
|
+
# error_during_execution, ...) carry their prose in errors[]. A run that
|
|
139
|
+
# ends on an API failure instead arrives as subtype "success" with
|
|
140
|
+
# is_error true, an empty errors[] and the "API Error: ..." prose in
|
|
141
|
+
# `result` — falling back to the subtype there produced the self-
|
|
142
|
+
# contradictory "Claude Code returned an error result: success". Prefer
|
|
143
|
+
# errors[], then `result`, then a non-success subtype, then the HTTP
|
|
144
|
+
# status, mirroring the TypeScript SDK's choice of `result` for the
|
|
145
|
+
# `success` subtype.
|
|
146
|
+
#
|
|
147
|
+
# Public because the read loop builds the exception message from it, and
|
|
148
|
+
# because it is the documented way to get the same one-line summary out
|
|
149
|
+
# of a raw error result you already hold (an is_error ResultMessage the
|
|
150
|
+
# CLI emitted before exiting). Mirrors Python's _error_result_text.
|
|
151
|
+
def self.error_text(data)
|
|
152
|
+
errors = Payload.normalize_errors(Payload.field(data, :errors))
|
|
153
|
+
return errors.join('; ') unless errors.empty?
|
|
154
|
+
|
|
155
|
+
result = Payload.field(data, :result)
|
|
156
|
+
return result.strip if result.is_a?(String) && !result.strip.empty?
|
|
157
|
+
|
|
158
|
+
subtype = Payload.field(data, :subtype)
|
|
159
|
+
return subtype if subtype.is_a?(String) && !subtype.empty? && subtype != 'success'
|
|
160
|
+
|
|
161
|
+
status = Payload.field(data, :api_error_status)
|
|
162
|
+
return "API error (HTTP #{status})" unless status.nil?
|
|
163
|
+
|
|
164
|
+
'unknown error'
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def initialize(message, data: nil, exit_code: nil, stderr: nil, original_error: nil)
|
|
168
|
+
data = {} unless data.is_a?(Hash)
|
|
169
|
+
@data = data
|
|
170
|
+
@original_error = original_error
|
|
171
|
+
|
|
172
|
+
subtype = Payload.field(data, :subtype)
|
|
173
|
+
@subtype = subtype.is_a?(String) ? subtype : nil
|
|
174
|
+
@errors = Payload.normalize_errors(Payload.field(data, :errors))
|
|
175
|
+
result = Payload.field(data, :result)
|
|
176
|
+
@result = result.is_a?(String) ? result : nil
|
|
177
|
+
status = Payload.field(data, :api_error_status)
|
|
178
|
+
@api_error_status = status.is_a?(Integer) ? status : nil
|
|
179
|
+
reason = Payload.field(data, :terminal_reason)
|
|
180
|
+
@terminal_reason = reason.is_a?(String) ? reason : nil
|
|
181
|
+
session_id = Payload.field(data, :session_id)
|
|
182
|
+
@session_id = session_id.is_a?(String) ? session_id : nil
|
|
183
|
+
|
|
184
|
+
super(message, exit_code: exit_code, stderr: stderr)
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
41
188
|
# Raised when unable to decode JSON from CLI output
|
|
42
189
|
class CLIJSONDecodeError < ClaudeSDKError
|
|
43
190
|
attr_reader :line, :original_error
|
|
@@ -25,6 +25,8 @@ module ClaudeAgentSDK
|
|
|
25
25
|
parse_stream_event(data)
|
|
26
26
|
when 'rate_limit_event'
|
|
27
27
|
parse_rate_limit_event(data)
|
|
28
|
+
when 'conversation_reset'
|
|
29
|
+
parse_conversation_reset_message(data)
|
|
28
30
|
when 'tool_progress'
|
|
29
31
|
parse_tool_progress_message(data)
|
|
30
32
|
when 'auth_status'
|
|
@@ -53,16 +55,32 @@ module ClaudeAgentSDK
|
|
|
53
55
|
content = message_data[:content]
|
|
54
56
|
raise MessageParseError.new("Missing content in user message", data: data) unless content
|
|
55
57
|
|
|
58
|
+
origin = parse_origin(data)
|
|
59
|
+
|
|
56
60
|
if content.is_a?(Array)
|
|
57
61
|
content_blocks = parse_content_blocks(content, data)
|
|
58
62
|
UserMessage.new(content: content_blocks, uuid: uuid, parent_tool_use_id: parent_tool_use_id,
|
|
59
|
-
tool_use_result: tool_use_result)
|
|
63
|
+
tool_use_result: tool_use_result, origin: origin)
|
|
60
64
|
else
|
|
61
65
|
UserMessage.new(content: content, uuid: uuid, parent_tool_use_id: parent_tool_use_id,
|
|
62
|
-
tool_use_result: tool_use_result)
|
|
66
|
+
tool_use_result: tool_use_result, origin: origin)
|
|
63
67
|
end
|
|
64
68
|
end
|
|
65
69
|
|
|
70
|
+
# Returns `data[:origin]` when it is a well-formed origin object.
|
|
71
|
+
#
|
|
72
|
+
# Passed through as-is — including keys and kinds this SDK version doesn't
|
|
73
|
+
# model, and with the wire key spelling untouched (`:fromSession` stays
|
|
74
|
+
# camelCase) — so newer CLI origin kinds/fields stay visible to callers.
|
|
75
|
+
# Anything that is not a Hash with a String `:kind` is treated as absent.
|
|
76
|
+
def self.parse_origin(data)
|
|
77
|
+
origin = data[:origin]
|
|
78
|
+
return origin if origin.is_a?(Hash) && origin[:kind].is_a?(String)
|
|
79
|
+
|
|
80
|
+
nil
|
|
81
|
+
end
|
|
82
|
+
private_class_method :parse_origin
|
|
83
|
+
|
|
66
84
|
def self.parse_assistant_message(data)
|
|
67
85
|
message_data = data[:message]
|
|
68
86
|
# A non-Hash message (malformed CLI output) raised a raw TypeError from
|
|
@@ -124,7 +142,10 @@ module ClaudeAgentSDK
|
|
|
124
142
|
end
|
|
125
143
|
|
|
126
144
|
def self.parse_result_message(data)
|
|
127
|
-
|
|
145
|
+
# `origin` is overwritten with the validated value (or nil) rather than
|
|
146
|
+
# letting the base class assign the raw field: a malformed origin must
|
|
147
|
+
# read as absent, not be surfaced verbatim.
|
|
148
|
+
ResultMessage.new(data.merge(origin: parse_origin(data)))
|
|
128
149
|
end
|
|
129
150
|
|
|
130
151
|
def self.parse_stream_event(data)
|
|
@@ -135,6 +156,20 @@ module ClaudeAgentSDK
|
|
|
135
156
|
RateLimitEvent.new(data.merge(raw_data: data))
|
|
136
157
|
end
|
|
137
158
|
|
|
159
|
+
# `/clear` (or any other mid-session transcript discard) resets the
|
|
160
|
+
# conversation without ending the connection. Every field is required —
|
|
161
|
+
# a frame missing one is malformed CLI output, not a forward-compatible
|
|
162
|
+
# variant, so it raises rather than yielding a half-built message.
|
|
163
|
+
def self.parse_conversation_reset_message(data)
|
|
164
|
+
ConversationResetMessage.new(
|
|
165
|
+
new_conversation_id: data.fetch(:new_conversation_id),
|
|
166
|
+
uuid: data.fetch(:uuid),
|
|
167
|
+
session_id: data.fetch(:session_id)
|
|
168
|
+
)
|
|
169
|
+
rescue KeyError => e
|
|
170
|
+
raise MessageParseError.new("Missing required field in conversation_reset message: #{e.key}", data: data)
|
|
171
|
+
end
|
|
172
|
+
|
|
138
173
|
def self.parse_tool_progress_message(data)
|
|
139
174
|
ToolProgressMessage.new(data)
|
|
140
175
|
end
|