#10261: fix(agents): prevent exec tool errors from leaking to channels (#9651)
agents
stale
Cluster:
Tool Execution and Error Handling
## Summary
- When the exec tool returns a non-zero exit code, stderr/stdout content was forwarded as a visible message to the active messaging channel (WhatsApp, Telegram, etc.), leaking internal details
- Skip calling `emitToolOutput()` when `isToolError` is true in `handleToolExecutionEnd()`, so error content stays in the agent context only and is never forwarded to channels
Fixes #9651
## Test plan
- [x] 4 new tests for `handleToolExecutionEnd()`: successful output emission, error output suppression (isError flag), error output suppression (isToolResultError detection), lastToolError recording
- [x] All 4 tests pass
- [x] `pnpm check` passes (0 warnings, 0 errors)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
- Updates `handleToolExecutionEnd` to skip `emitToolOutput()` when the tool execution is considered an error (`isError` or `isToolResultError`).
- Adds a new Vitest suite exercising success emission, suppression on `isError`, suppression via `isToolResultError`, and `lastToolError` recording.
- Intended to prevent exec/batch stderr/stdout from being forwarded to external messaging channels by avoiding the tool-output emission path.
<h3>Confidence Score: 3/5</h3>
- This PR likely addresses one leak path, but may still allow error tool output to be surfaced via tool result events.
- Change correctly gates `emitToolOutput()` behind `!isToolError` and adds tests for that behavior, but `emitAgentEvent` still includes `result: sanitizedResult` even on error; if any downstream consumer forwards tool result events to external channels, the original leak may persist. Confidence is reduced until the full external-forwarding path is confirmed/redacted.
- src/agents/pi-embedded-subscribe.handlers.tools.ts (error tool result emission via emitAgentEvent)
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#18934: fix(agents): suppress exec tool output from channel delivery
by BinHPdev · 2026-02-17
82.4%
#11854: fix: resolve per-agent tools.exec config in pi-tools
by Yida-Dev · 2026-02-08
79.5%
#18916: fix(agents): suppress memory tool delivery to prevent reply drop
by BinHPdev · 2026-02-17
79.0%
#7525: Agents: skip errored tool calls during pairing
by justinhuangcode · 2026-02-02
78.8%
#18415: fix(agents): suppress benign exec exit code 1 from tool error surfa...
by aldoeliacim · 2026-02-16
78.8%
#14734: test(agents): guard against stale allowAgents in existing sessions
by davidahmann · 2026-02-12
78.4%
#19235: fix(telegram): tool error warnings no longer overwrite streamed rep...
by gatewaybuddy · 2026-02-17
78.2%
#4844: fix(agents): skip error/aborted assistant messages in transcript re...
by lailoo · 2026-01-30
78.1%
#19632: fix: suppressToolErrors now suppresses exec tool failure notifications
by Gitjay11 · 2026-02-18
77.8%
#20382: fix: move suppressToolErrors check before mutating tool check
by klawdius-noodle · 2026-02-18
77.8%