Stream Claude CLI steps and inference trace IDs - #567
Open
solvemproblr wants to merge 3 commits into
Open
Conversation
solvemproblr
marked this pull request as ready for review
August 8, 2026 23:01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
stream-jsonoutput as it arrives over SSHIssue
ClaudeSDKAgentlaunches Claude Code as a separate process in the environment workspace. Two in-process assumptions therefore broke:trace_id.Original affected trace: https://hud.ai/trace/80c7c75a-db92-436b-a8c4-d0cc38745d79
Baseline streaming reproduction: https://hud.ai/trace/8163171d6ecb4e9fa6b34c179cd5d7e1
Solution
The agent now opens a streaming SSH process and incrementally translates Claude assistant, tool-use, tool-result, and terminal result messages into canonical HUD steps. Tool calls are paired with their results, process cleanup remains bounded on cancellation, and SSH reconnects only before process creation so commands are never replayed.
When the CLI is routed through the HUD gateway, the agent also exports the active trace context through Claude Code's
ANTHROPIC_CUSTOM_HEADERScontract asTrace-Id: <id>. Direct Anthropic requests do not receive HUD-specific trace metadata.Impact
Claude CLI rollouts now appear in HUD while they are running, with per-turn tool activity and file-change timing consistent with other SDK agents. Their gateway inference usage can also be attributed to the rollout trace.
Fixed streaming reproduction using the same deployed environment and task: https://hud.ai/trace/82e0cc87517d4c6495e7e074d2aeb0d2
Validation
21 passedin the focused Claude SDK agent and SSH client test filesgit diff --checkpasses1.0; the fixed trace contains 12 turns and 29 timestamped spans across the live executionNote
Medium Risk
Changes the Claude SDK execution and SSH streaming path and gateway header wiring, but behavior is covered by focused tests and preserves existing trace metadata on finish.
Overview
ClaudeSDKAgent no longer waits for the remote
claudeprocess to finish before HUD sees progress. It opens a streaming SSH session, parsesstream-jsonlines as they arrive, and records assistant and tool steps with timestamps that reflect when each turn actually happened—not a single aggregate step at exit.Assistant messages are mapped through a shared
ClaudeAgent._message_to_agent_stephelper so CLI streaming matches the in-process Messages API agent (usage, tool calls, citations). Tool results are paired with pending tool-use IDs; exit without a result message or unmatched tool calls is treated as an error. Cancellation closes the remote process with a bounded wait.When the CLI uses the HUD gateway, the active rollout trace ID is passed via
ANTHROPIC_CUSTOM_HEADERS(Trace-Id: …) so gateway inference logs can be attributed to the rollout trace.SSHClient adds
create_processwith the same reconnect-before-open behavior asrun, without replaying commands after a process has started. Tests cover streaming turn ordering, trace header forwarding, and SSH reconnect on stream open.Reviewed by Cursor Bugbot for commit bd59c73. Bugbot is set up for automated code reviews on this repo. Configure here.