Skip to content

fix(agent): recover panics into machine-readable agent_error output - #1043

Merged
edenreich merged 1 commit into
mainfrom
fix/headless-panic-agent-error
Aug 10, 2026
Merged

edenreich merged 1 commit into
mainfrom
fix/headless-panic-agent-error

Conversation

@edenreich

Copy link
Copy Markdown
Contributor

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's parseAgentError, infer-action) missed the failure entirely. A recover() in cmd/headless.go alone cannot restore the pre-#1041 contract, because the command's goroutine never sees those panics.

Changes

  • internal/agent: new EventDrivenAgent.recoverPanic helper converts a panic into the existing failStream path (ChatErrorEvent + StateError + loop nudge). Deferred at every goroutine root that runs task code: processEvents, startStreaming, executeTools, and the ExecuteToolInternal closure used by the approving/blocking states.
  • Parallel tool workers (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, where recoverPanic surfaces it.
  • RunWithStream setup goroutine: recovers and publishes ChatErrorEvent directly, before chatEvents closes.
  • cmd/headless.go: the EmitPreRunError defer moves above container construction (storage init panics by design) and folds in a recover, so main-goroutine panics also produce the agent_error line and a non-zero exit.

No renderer changes needed — all three formats already turn ChatErrorEvent into agent_error (JSON, truncated to 3500) or RUN_ERROR (ag-ui) and return a non-nil error, which exits 1.

Testing

  • TestProcessEvents_RecoversPanicToChatError (counterfeiter FakeStateHandler, newly generated) verifies a panicking state handler surfaces as ChatErrorEvent instead of crashing.
  • Manual repro per the issue: with a panic("boom") injected in executeToolInternal and the mock gateway, infer headless --format json emits {"type":"agent_error","message":"agent panic: boom"} and exits 1; --format ag-ui emits RUN_ERROR. Injection reverted.
  • go test -race ./... and task precommit:run pass.

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
@edenreich
edenreich requested a review from a team as a code owner August 10, 2026 21:45
@edenreich
edenreich merged commit ab8a17d into main Aug 10, 2026
1 check passed
@edenreich
edenreich deleted the fix/headless-panic-agent-error branch August 10, 2026 21:52
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.166.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Headless panics crash without a machine-readable agent_error line

1 participant