claude-agent-sdk 0.18.0 → 0.19.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ca7129acd0fb9330d1a49307fde85efff7febaf503bcbda5dbe63d82b3f6476
4
- data.tar.gz: 49568fac24a25b129c9f97e5a8ef6c2cd0a3f4db6be6f721c69be7ea31261da5
3
+ metadata.gz: cbcc1484868ae2ed502513727fd7df53192fc0177eed594433ad7c0332b3287a
4
+ data.tar.gz: b052d3f7993aa1f4d5a2b4fc3bdb01f7cf974f939c67e82a7bac173cb0b65a8f
5
5
  SHA512:
6
- metadata.gz: 54a7f40056f3b97db66a31e34dab0d659432731bef991e7262b32d0a09efd419248ea6f150fe2906292b281f293b964dde85cb0e7b9bdd7d220b0d8fb57a8441
7
- data.tar.gz: 430e1f1d708a2b75b425523fd87d1a0aab8ef68850def2c66205fcb0d20552f374d2f92feb03217a301410d5b016d7c465eeac97b0aacfc497bafea0412c69ff
6
+ metadata.gz: b50d94d3879214f2bed9c42e4fe20a989c84e5de26a36a907c1b1093dc344a66a6b37e53a3d60a997f92ce84b4795871d55da32056a235a70617e4dd0cf85552
7
+ data.tar.gz: 7899caf95b4c5da6e5f4e004ae57456e19e18b6d2be35469e88bd14f1333be4d3053fa8138dd037616c9a15ce2a9985164d065bbdb0026af90b759b31646cddb
data/CHANGELOG.md CHANGED
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.19.0] - 2026-06-29
11
+
12
+ ### Added
13
+ - `TaskUpdatedMessage` — typed `system`/`task_updated` lifecycle events (Python SDK 0.2.101 / #1016 parity). A background task's terminal state can arrive *only* as a `task_updated` patch with no accompanying `TaskNotificationMessage` (e.g. a `TaskStop`-killed task reports `status: 'killed'` here), so consumers tracking active task IDs no longer hang waiting for a notification that never comes. `status` is derived from `patch['status']` (parsed defensively — a non-Hash/absent patch falls back to `{}`, `task_id` defaults to `''` so it is never nil, and parsing never raises). New `TASK_UPDATED_STATUSES` and `TERMINAL_TASK_STATUSES` constants; the latter spans both lifecycle vocabularies (`task_notification` reports `stopped`, `task_updated` reports the raw `killed`) so a terminal status from *either* message clears active-task tracking.
14
+
15
+ ### Fixed
16
+ - Malformed CLI message content now raises a descriptive `MessageParseError` instead of an opaque `TypeError`/`NoMethodError` (Python SDK #1058 parity): a non-Hash content block (e.g. a bare String) and an assistant `content` that is not an Array are both caught with a clear message carrying the full payload, rather than crashing deep inside block parsing on `block[:type]`.
17
+
10
18
  ## [0.18.0] - 2026-06-12
11
19
 
12
20
  ### Added
data/README.md CHANGED
@@ -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.18.0'
71
+ gem 'claude-agent-sdk', '~> 0.19.0'
72
72
  ```
73
73
 
74
74
  Then `bundle install`, or install directly: `gem install claude-agent-sdk`.
data/docs/types.md CHANGED
@@ -42,22 +42,30 @@ System message with metadata. Task lifecycle events are typed subclasses.
42
42
 
43
43
  ```ruby
44
44
  class SystemMessage
45
- attr_accessor :subtype, # String ('init', 'task_started', 'task_progress', 'task_notification', etc.)
45
+ attr_accessor :subtype, # String ('init', 'task_started', 'task_progress', 'task_notification', 'task_updated', etc.)
46
46
  :data # Hash
47
47
  end
48
48
 
49
49
  # Typed subclasses (all inherit from SystemMessage, so is_a?(SystemMessage) still works)
50
50
  class TaskStartedMessage < SystemMessage
51
- attr_accessor :task_id, :description, :uuid, :session_id, :tool_use_id, :task_type
51
+ attr_accessor :task_id, :description, :uuid, :session_id, :tool_use_id, :task_type, :workflow_name, :prompt
52
52
  end
53
53
 
54
54
  class TaskProgressMessage < SystemMessage
55
- attr_accessor :task_id, :description, :usage, :uuid, :session_id, :tool_use_id, :last_tool_name
55
+ attr_accessor :task_id, :description, :usage, :uuid, :session_id, :tool_use_id, :last_tool_name, :summary
56
56
  end
57
57
 
58
58
  class TaskNotificationMessage < SystemMessage
59
59
  attr_accessor :task_id, :status, :output_file, :summary, :uuid, :session_id, :tool_use_id, :usage
60
60
  end
61
+
62
+ # Background task lifecycle state change. `status` is derived from patch["status"].
63
+ # A terminal task can arrive *only* as a TaskUpdatedMessage (no TaskNotificationMessage) —
64
+ # e.g. a TaskStop-killed task reports status "killed" here. Clear tracked task IDs on a
65
+ # terminal status (see TERMINAL_TASK_STATUSES) from *either* message.
66
+ class TaskUpdatedMessage < SystemMessage
67
+ attr_accessor :task_id, :patch, :status, :uuid, :session_id
68
+ end
61
69
  ```
62
70
 
63
71
  ### ResultMessage
@@ -183,5 +191,7 @@ end
183
191
  | `HOOK_EVENTS` | Available hook events |
184
192
  | `ASSISTANT_MESSAGE_ERRORS` | Possible error types in AssistantMessage |
185
193
  | `TASK_NOTIFICATION_STATUSES` | Task lifecycle notification statuses (`completed`, `failed`, `stopped`) |
194
+ | `TASK_UPDATED_STATUSES` | `task_updated` patch statuses (`pending`, `running`, `paused`, `completed`, `failed`, `killed`) |
195
+ | `TERMINAL_TASK_STATUSES` | Statuses meaning a task has finished — spans both vocabularies (`completed`, `failed`, `stopped`, `killed`); clear active-task tracking on any of these |
186
196
  | `MCP_SERVER_CONNECTION_STATUSES` | MCP server connection states (`connected`, `failed`, `needs-auth`, `pending`, `disabled`) |
187
197
  | `EFFORT_LEVELS` | Effort levels (`low`, `medium`, `high`, `xhigh`, `max`) |
@@ -51,7 +51,7 @@ module ClaudeAgentSDK
51
51
  raise MessageParseError.new("Missing content in user message", data: data) unless content
52
52
 
53
53
  if content.is_a?(Array)
54
- content_blocks = content.map { |block| parse_content_block(block) }
54
+ content_blocks = parse_content_blocks(content, data)
55
55
  UserMessage.new(content: content_blocks, uuid: uuid, parent_tool_use_id: parent_tool_use_id,
56
56
  tool_use_result: tool_use_result)
57
57
  else
@@ -63,8 +63,9 @@ module ClaudeAgentSDK
63
63
  def self.parse_assistant_message(data)
64
64
  content = data.dig(:message, :content)
65
65
  raise MessageParseError.new("Missing content in assistant message", data: data) unless content
66
+ raise MessageParseError.new("Invalid assistant content (expected Array, got #{content.class})", data: data) unless content.is_a?(Array)
66
67
 
67
- content_blocks = content.map { |block| parse_content_block(block) }
68
+ content_blocks = parse_content_blocks(content, data)
68
69
  AssistantMessage.new(
69
70
  content: content_blocks,
70
71
  model: data.dig(:message, :model),
@@ -96,7 +97,14 @@ module ClaudeAgentSDK
96
97
  'elicitation_complete' => ElicitationCompleteMessage,
97
98
  'task_started' => TaskStartedMessage,
98
99
  'task_progress' => TaskProgressMessage,
99
- 'task_notification' => TaskNotificationMessage
100
+ 'task_notification' => TaskNotificationMessage,
101
+ # task_updated carries `status` inside `patch` (not at the top level) and
102
+ # defaults task_id to "" — it derives those defensively in its own
103
+ # constructor (see TaskUpdatedMessage), so it dispatches through the table
104
+ # like every other system subtype. `data` is always symbol-keyed here:
105
+ # `parse` rejects any message lacking a `:type` symbol key, so a
106
+ # string-keyed hash never reaches these classes.
107
+ 'task_updated' => TaskUpdatedMessage
100
108
  }.freeze
101
109
 
102
110
  def self.parse_system_message(data)
@@ -132,6 +140,18 @@ module ClaudeAgentSDK
132
140
  PromptSuggestionMessage.new(data)
133
141
  end
134
142
 
143
+ # Maps a content Array to typed blocks, guarding each element. A non-Hash
144
+ # block (e.g. a bare String or nil from a malformed CLI message) raises a
145
+ # descriptive MessageParseError carrying the full message rather than an
146
+ # opaque TypeError/NoMethodError from `block[:type]` deep in parsing.
147
+ def self.parse_content_blocks(content, data)
148
+ content.map do |block|
149
+ raise MessageParseError.new("Invalid content block (expected Hash, got #{block.class})", data: data) unless block.is_a?(Hash)
150
+
151
+ parse_content_block(block)
152
+ end
153
+ end
154
+
135
155
  # Accepts blocks with either symbol or string keys — live CLI messages
136
156
  # arrive symbol-keyed (parsed via `symbolize_names: true`), session
137
157
  # transcripts arrive string-keyed (parsed via `symbolize_names: false`).
@@ -333,6 +333,19 @@ module ClaudeAgentSDK
333
333
  # Task lifecycle notification statuses
334
334
  TASK_NOTIFICATION_STATUSES = %w[completed failed stopped].freeze
335
335
 
336
+ # Possible status values reported inside a `task_updated` patch.
337
+ # pending/running/paused are non-terminal; completed/failed/killed are
338
+ # terminal. Note: task_updated reports the raw "killed"; the CLI maps that to
339
+ # "stopped" only when it emits a task_notification.
340
+ TASK_UPDATED_STATUSES = %w[pending running paused completed failed killed].freeze
341
+
342
+ # Task statuses that mean the task has finished and should be cleared from any
343
+ # "active task" tracking. Spans both lifecycle vocabularies: task_notification
344
+ # reports "stopped" (the CLI's mapped form of a killed task) while task_updated
345
+ # reports the raw "killed". Treat the status of a TaskNotificationMessage and a
346
+ # TaskUpdatedMessage the same way.
347
+ TERMINAL_TASK_STATUSES = %w[completed failed stopped killed].freeze
348
+
336
349
  # Typed usage data for task progress and notifications
337
350
  class TaskUsage < Type
338
351
  attr_accessor :total_tokens, :tool_uses, :duration_ms
@@ -356,11 +369,44 @@ module ClaudeAgentSDK
356
369
  attr_accessor :task_id, :description, :usage, :uuid, :session_id, :tool_use_id, :last_tool_name, :summary
357
370
  end
358
371
 
359
- # Task notification system message (task completed/failed/stopped)
372
+ # Task notification system message (task completed/failed/stopped).
373
+ #
374
+ # Note: not every terminal task emits this message. Background tasks may
375
+ # instead report completion only via a TaskUpdatedMessage whose patch["status"]
376
+ # is terminal (see TERMINAL_TASK_STATUSES). Consumers tracking active task IDs
377
+ # should clear them on a terminal status from *either* message.
360
378
  class TaskNotificationMessage < SystemMessage
361
379
  attr_accessor :task_id, :status, :output_file, :summary, :uuid, :session_id, :tool_use_id, :usage
362
380
  end
363
381
 
382
+ # Task updated system message (background task lifecycle state change).
383
+ #
384
+ # The CLI emits system/task_updated events as a task moves through its
385
+ # lifecycle. `patch` carries the changed fields (e.g. status, end_time); when
386
+ # patch["status"] is terminal (see TERMINAL_TASK_STATUSES) the task has
387
+ # finished. A background task's terminal state can arrive *only* as a
388
+ # TaskUpdatedMessage with no accompanying TaskNotificationMessage — e.g. a task
389
+ # stopped via TaskStop reports status "killed" here and the matching
390
+ # notification is sometimes suppressed. Consumers tracking active task IDs
391
+ # should clear them on a terminal status from *either* message.
392
+ #
393
+ # Parsed defensively in the constructor — a lifecycle event must never raise:
394
+ # `status` is derived from patch["status"] (not a top-level field); a non-Hash
395
+ # or absent patch falls back to {}; and `task_id` defaults to "" (never nil,
396
+ # matching the Python SDK) so consumers can rely on it always being a String.
397
+ # The full patch is preserved on `#patch` for callers that need more than the
398
+ # status.
399
+ class TaskUpdatedMessage < SystemMessage
400
+ attr_accessor :task_id, :patch, :status, :uuid, :session_id
401
+
402
+ def initialize(attributes = {})
403
+ super
404
+ @task_id ||= ''
405
+ @patch = {} unless @patch.is_a?(Hash)
406
+ @status = @patch[:status]
407
+ end
408
+ end
409
+
364
410
  # Result message with cost and usage information
365
411
  class ResultMessage < Type
366
412
  attr_accessor :subtype, :duration_ms, :duration_api_ms, :is_error,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ClaudeAgentSDK
4
- VERSION = '0.18.0'
4
+ VERSION = '0.19.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: claude-agent-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.0
4
+ version: 0.19.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Community Contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-06-12 00:00:00.000000000 Z
11
+ date: 2026-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async