Skip to content

fix(agent): keep model focused after failed tool calls - #64

Open
syf2211 wants to merge 1 commit into
KlaatAI:mainfrom
syf2211:fix/tool-failure-focus-18
Open

fix(agent): keep model focused after failed tool calls#64
syf2211 wants to merge 1 commit into
KlaatAI:mainfrom
syf2211:fix/tool-failure-focus-18

Conversation

@syf2211

@syf2211 syf2211 commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Summary

When a tool call fails, the agent can pivot to unrelated tasks instead of retrying or asking for clarification. This PR adds a static system-prompt guardrail and injects a per-round focus hint after failed tool results.

Motivation

Fixes #18 — reported scenario: after browser_navigate 404, the model ignored the user's correction and started reading unrelated local files via MCP filesystem tools.

Changes

  • Add isToolFailure() helper detecting built-in Error: results, MCP errors, and non-zero run_command exits (excludes doom-loop Refused: guidance)
  • Add TOOL_FAILURE_FOCUS_HINT injected into API messages after any failed tool round
  • Wire injection into TUI (repl.ts), headless agent, and ACP agent loops
  • Add system-prompt bullet under Tool policy
  • Unit tests for failure detection

Tests

  • bun test src/agent/tool-failure-focus.test.ts — 7 pass
  • bun test — 452 pass
  • bun run typecheck — pass
  • bun run build — pass

Notes

This is a prompt-level guardrail, not a hard tool-category block. It follows the same injection pattern as the existing doom-loop recovery guidance. No server-side routing changes required.

Add a system-prompt guardrail and inject a per-round focus hint when
built-in or MCP tools fail, covering TUI, headless, and ACP loops.

Fixes KlaatAI#18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Model derails from user intent after a failed tool call

1 participant