Skip to content

Add Testing::Cassette: record sessions and replay them without the CLI - #185

Closed
ya-luotao wants to merge 1 commit into
mainfrom
testing-cassette
Closed

ya-luotao wants to merge 1 commit into
mainfrom
testing-cassette

Conversation

@ya-luotao

@ya-luotao ya-luotao commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Adds ClaudeAgentSDK::Testing::Cassette (require 'claude_agent_sdk/testing/cassette'): record a session's stream-JSON frames to a JSONL file once, then replay them in tests with no CLI subprocess and no API call.

cassette = ClaudeAgentSDK::Testing::Cassette.new('spec/cassettes/weather.jsonl') # :once — records if missing, else replays
ClaudeAgentSDK.ask('Weather?', options: opts, transport: cassette.transport(opts))
ClaudeAgentSDK::Client.open(options: opts, **cassette.client_args) { |client| ... }
cassette.verify!

How it works

  • The replay plays the CLI's side of the conversation: it yields recorded CLI frames, waits for each frame the SDK writes and compares it, and waits for end_input / close. Hooks, can_use_tool and SDK MCP tools run for real, and what they send back is compared with the recording (match: :strict by default, :type, or any #callable).
  • Request ids the SDK generates are remapped; back-to-back writes (replies to concurrent control requests) may arrive in any order.
  • Mismatches raise Cassette::MismatchError from the transport's read loop, so they surface from query / ask / Client. Raising from #write would be swallowed: Query#handle_control_request treats a failed write of a control response as a callback failure (verified with a prototype).
  • After the session, verify! pairs anything the SDK still wrote, ended or closed once it stopped reading, and reports mismatches, unreached events and unopened connections.
  • A recording is only published when every transport call completed and every connection is closed. A call that raised or was cancelled, a close with calls still in flight, or a failed write of the file leaves the existing file alone, and verify! raises Cassette::Error.
  • Options fingerprint and PINNED_CLI_VERSION drift only warn.

Built only on the public Transport contract: no existing library code changes. Not loaded by a plain require 'claude_agent_sdk' (pinned in require_surface_spec).

Deliberate trade-off

CLI output the SDK never read (an early break or disconnect) is not reported. A replay cannot tell whether test code used what the SDK delivered, so docs/testing.md tells users to assert on the result. A strict mode with an opt-in for truncation can be added later.

Testing

  • 32 examples in spec/unit/testing/cassette_spec.rb. Recordings go through the real SubprocessCLITransport against FakeClaude; the executable is deleted before replay. Concurrency cases (close vs. in-flight write, publish ordering, concurrent closes, cancelled close) are pinned with method-level TracePoint hooks or stub transports, not source line numbers.
  • Each fix was mutation-checked: reverting it fails its spec.
  • Four rounds of independent adversarial review; a 96-scenario record/replay lifecycle matrix (thread/inline, hooks, String/Enumerator/empty/break queries, Client variants) passes.
  • bundle exec rspec 4404/0, bundle exec rubocop clean, bundle exec rake rbs:validate rbs:test 3993/0.

Not yet done (P2): redaction, RSpec sugar, a recorded real-CLI integration example.


Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

ClaudeAgentSDK::Testing::Cassette (require 'claude_agent_sdk/testing/cassette')
records a session's stream-JSON frames to a JSONL file through the real
transport and replays them in tests by playing the CLI's side: hooks,
can_use_tool and SDK MCP tools run for real, and every frame the SDK writes
is compared with the recording. Mismatches raise MismatchError from the read
loop, so they reach query / ask / Client; verify! reports what a replay
never reached. Options-fingerprint and pinned-CLI drift only warn.

Built only on the public Transport contract; no existing code changes.

Amp-Thread-ID: https://ampcode.com/threads/T-01a19fec-39e9-76e0-9914-6c8ab560a0c9
Co-authored-by: ya-luotao <luotao@hey.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T09:52:35.013944Z 3d066d5 PR opened
🔒 Security Review ✅ Completed 2026-11-03T09:55:35.237408Z 3d066d5 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@ya-luotao ya-luotao closed this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants