← Back to PRs

#14136: feat: add agent collapse safeguards and fix TUI display on abort

by liangweigain-create open 2026-02-11 15:07 View on GitHub →
agents stale
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