Repository navigation
fix(agent): recover panics into machine-readable agent_error output - #1043
Merged
Merged
Conversation
Since #1041 consolidated headless mode on RunWithStream, the state machine and tool executors run on their own goroutines, so a panic unwound as a raw Go stack trace on stderr and stdout consumers (channel manager, infer-action) saw silence. Recover panics at every agent goroutine root - the event loop, streaming, tool execution (including the parallel tool workers, which capture the first panic and re-panic it on the collector) - and route them through the existing failStream path, so all renderers emit the agent_error line (JSON) or RUN_ERROR (ag-ui) and the process exits non-zero. runHeadless additionally recovers main-goroutine panics into EmitPreRunError. Resolves #1042
Contributor
|
🎉 This PR is included in version 0.166.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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.
Resolves #1042
Summary
Since #1041 consolidated headless mode on
RunWithStream, the state machine and tool executors run on their own goroutines — a panic there unwound as a raw Go stack trace on stderr while stdout stayed silent, so machine consumers (channel manager'sparseAgentError, infer-action) missed the failure entirely. Arecover()incmd/headless.goalone cannot restore the pre-#1041 contract, because the command's goroutine never sees those panics.Changes
internal/agent: newEventDrivenAgent.recoverPanichelper converts a panic into the existingfailStreampath (ChatErrorEvent+StateError+ loop nudge). Deferred at every goroutine root that runs task code:processEvents,startStreaming,executeTools, and theExecuteToolInternalclosure used by the approving/blocking states.executeToolCallsParallel): a worker panic can't be recovered by the collector's goroutine, so workers capture the first panic (logged with its original stack) and re-panic it on the collector, whererecoverPanicsurfaces it.RunWithStreamsetup goroutine: recovers and publishesChatErrorEventdirectly, beforechatEventscloses.cmd/headless.go: theEmitPreRunErrordefer moves above container construction (storage init panics by design) and folds in a recover, so main-goroutine panics also produce theagent_errorline and a non-zero exit.No renderer changes needed — all three formats already turn
ChatErrorEventintoagent_error(JSON, truncated to 3500) orRUN_ERROR(ag-ui) and return a non-nil error, which exits 1.Testing
TestProcessEvents_RecoversPanicToChatError(counterfeiterFakeStateHandler, newly generated) verifies a panicking state handler surfaces asChatErrorEventinstead of crashing.panic("boom")injected inexecuteToolInternaland the mock gateway,infer headless --format jsonemits{"type":"agent_error","message":"agent panic: boom"}and exits 1;--format ag-uiemitsRUN_ERROR. Injection reverted.go test -race ./...andtask precommit:runpass.