Repository navigation
feat: improve Claude Code headless integration with streaming events - #744
Conversation
|
@infer CI is failing - can you fix this? |
|
Model: Todos
Pull Request✅ Infer Result: SuccessAll tasks are complete. The CI failure has been fixed and pushed. No further work is needed. Model: Tokens: 1,993,461 in · 5,729 out · 1,999,190 total (45 requests) Tool calls: 46 total · 96% success rate
|
|
@infer CI is still failing - can you fix it? |
|
Model: Todos
Pull Request✅ Infer Result: SuccessAll tasks are complete. Here's a summary of what was fixed: SummaryThe CI failure in PR #744 was caused by a pre-existing test bug in Issue 1:
|
…stream Verified event shapes against real claude 2.1.197 stream-json output and captured a golden fixture: - New result_metadata SSE event carrying session_id, model, durations (duration_ms, duration_api_ms, ttft_ms, ttft_stream_ms), num_turns, stop/terminal reason, total_cost_usd, permission_denials, and the full token breakdown including cache splits - Fix permission_denials typing: it is an array of objects on the wire; the previous []string made the entire result line fail to unmarshal, dropping usage and message_stop - Hook events actually arrive as type=system with subtype hook_started/hook_response; forward the raw line as hook_event (the previous case "hook" was dead code) - Stamp the model from system/init on every chunk - system_init event now includes cwd and permission_mode - Always reap the claude process via deferred cmd.Wait so ctx-cancelled streams don't leave zombies - Raise scanner cap to 10MB for large tool-result lines - Skip typed non-chunk events in processStreamEvent instead of error-logging every turn - Split transformMessage into per-type helpers (fixes funlen) and flatten test nesting (fixes nestif)
484c856 to
b48ad5e
Compare
|
🎉 This PR is included in version 0.132.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Summary
Improves the Claude Code headless integration by consuming
claude -p --output-format stream-json --verbose --include-hook-eventsincrementally and surfacing every datum needed for downstream metrics capture in bothinfer agentandinfer chatmodes. All event shapes were verified against real claude 2.1.197 stream-json output, and a captured golden fixture (internal/infra/adapters/testdata/stream_events.jsonl) locks them in.Changes
--include-hook-eventsflag added tobuildArgs()result_metadataSSE event (emitted beforemessage_stop):session_id,model,subtype,is_error,duration_ms,duration_api_ms,ttft_ms,ttft_stream_ms,num_turns,stop_reason,terminal_reason,total_cost_usd,permission_denials, and the full token breakdown including cache splitspermission_denialstyping fix: it is an array of objects ({tool_name, tool_use_id, tool_input}) on the wire; the previous[]stringmade the whole result line fail to unmarshal, silently dropping usage andmessage_stop. Now kept asjson.RawMessageand forwarded verbatimtype: "system"with subtypehook_started/hook_response(the previouscase "hook"never matched); the raw line is forwarded as ahook_eventSSE so per-tool/hook timings are derivabletool_failureSSE event when a tool result hasis_error: true(tool_use_id+ error message)system_initSSE event withsession_id,cwd,model,permission_mode,claude_code_version,toolssystem/init), so chunks no longer carry"model": ""cmd.Wait()reaps the claude process on all return paths (no zombies on cancellation); malformed JSON lines are logged and skippedprocessStreamEventskips typed non-chunk events instead of error-logging every turn (both chat and agent modes share this path)Metrics data captured (for the follow-up OTLP push, #741)
gen_ai.client.token.usage(input/output)result_metadata.usageincl. cache splitsgen_ai.client.operation.durationresult_metadata.duration_ms/duration_api_msgen_ai.client.operation.time_to_first_chunkresult_metadata.ttft_ms/ttft_stream_msgen_ai.execute_tool.durationhook_eventlifecycle timestampserror.type)tool_failureevents +is_erroron tool-result deltas;result_metadata.permission_denialsinference_gateway.tool_callsgen_ai.request.modelmodelon every chunk,system_init.model,result_metadata.modelresult_metadata.total_cost_usdAcceptance Criteria Coverage
system/hook_started|hook_response, forwarded raw)Closes #743