#14136: feat: add agent collapse safeguards and fix TUI display on abort
agents
stale
Cluster:
Error Handling in Agent Tools
Hi! This PR adds some basic safeguards to prevent agents from running indefinitely or getting stuck in tool execution loops. It also fixes several UI issues in the TUI when these safeguards are triggered.
Key Improvements:
Agent Safeguards:
Max Tool-calls Turns: Stop the agent if it exceeds a maximum number of tool calls (if configured).
Loop Detection: Abort the run if the agent calls the same tool with identical arguments repeatedly(if loopDetection = true).
TUI Fixes:
Fixed the issue where the TUI would stay in the "Running" state after an agent was aborted by a safeguard.
Resolved the (I think the /stop command still has the issue)
```(no output)```
message in the chat log by ensuring the abort reason is properly emitted before the agent stops.
Verification: Added unit tests to ensure the safeguard logic works as expected.
Configuration:
Users can now tune these in the openclaw.json:
json
{
"agents": {
"defaults": {
"safeguards": {
"maxTurns": 30,
"loopDetection": true,
"loopThreshold": 3
}
}
}
}
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR introduces configurable embedded-agent safeguards (max tool-call turns + simple loop detection) and wires them into the embedded PI session subscription pipeline. It also adjusts the embedded runner’s abort flow to emit an assistant message + lifecycle:end event when a safeguard abort occurs, aiming to prevent the TUI from showing “(no output)” and leaving the run stuck in a running state.
Main touchpoints are `src/agents/pi-embedded-subscribe.ts` (safeguard state + checking) and `src/agents/pi-embedded-runner/run/attempt.ts` (abort event emission). Config is extended via `src/config/types.agent-defaults.ts` + `src/config/zod-schema.agent-defaults.ts`.
<h3>Confidence Score: 3/5</h3>
- Moderately safe, but needs cleanup before merge.
- Core safeguard wiring looks coherent and config schema/types align, but the PR includes a stray `console.log` in the embedded event handler and some small correctness/maintainability issues (redundant state reset, test code hygiene) that should be addressed to avoid noisy output and CI failures.
- src/agents/pi-embedded-subscribe.handlers.ts, src/agents/pi-embedded-subscribe.ts, src/agents/pi-embedded-subscribe.safeguards.test.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#9085: fix: improve stability for terminated responses and telegram retries
by vladdick88 · 2026-02-04
78.2%
#14734: test(agents): guard against stale allowAgents in existing sessions
by davidahmann · 2026-02-12
77.7%
#2541: fix(agents): add error handling to orphaned message cleanup
by Episkey-G · 2026-01-27
77.4%
#7085: test: skip flaky workspace-paths & safe-bins tests on non-Linux/CI ...
by ThinkIbrokeIt · 2026-02-02
77.4%
#8332: fix: add per-tool-call timeout to prevent agent hangs (v2 - fixes m...
by vishaltandale00 · 2026-02-03
77.3%
#21115: fix(agent): immediately abort active run on stop/abort message during…
by anillBhoi · 2026-02-19
77.1%
#13792: Claude/openclaw token efficiency q ni2s
by imecostalima · 2026-02-11
76.6%
#8086: feat(security): Add prompt injection guard rail
by bobbythelobster · 2026-02-03
76.3%
#6095: feat(gateway): support modular guardrails extensions for securing a...
by Reapor-Yurnero · 2026-02-01
76.3%
#9861: fix(agents): re-run tool_use/tool_result repair after limitHistoryT...
by CyberSinister · 2026-02-05
76.3%