[GH-ISSUE #2322] [Issue]: Agent tool-call errors abort the whole agent run instead of being fed back to the model for self-correction #1149

Closed
opened 2026-10-21 05:27:58 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @Romuromylus on GitHub (Jun 5, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2322

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Medium — Correctness / robustness. When a tool-using agent (Spotify DJ, haptic, or any custom-tool agent) makes a tool call with missing/invalid arguments, the agent tool loop re-throws the error and aborts the entire agent run, producing no usable output for the turn. The model is never given the error to self-correct. This is asymmetric with the rest of the codebase, which feeds tool errors back to the model.

Expected behavior

A tool-call error should be captured and returned to the model as a role:"tool" result with success:false, so the model can retry/recover within the remaining tool rounds — exactly as the main-character tool loop already does.

Actual behavior

src/engine/agents-runtime/executor/agent-executor.ts:372-378, inside executeAgentWithTools' per-tool-call loop:

let toolResult: string;
try {
  toolResult = await toolContext.executeToolCall(tc);
} catch (err) {
  logger.error(err, "[agent-tools] %s %s failed", config.type, tc.function.name);
  throw err;
}

The re-throw unwinds the round loop and is caught only by executeAgent's outer try (catch at 279-281), returning makeError(...). executeBuiltInTool (tools-runtime.ts:331) calls toolError() (throw new Error(message)) for every missing required arg — roll_dice/notation, update_game_state/type+target+key, search_lorebook/query, spotify_play/uri|uris, write_chat_variable/key, etc. — and executeToolCall routes built-ins straight there with no try/catch.

This is asymmetric: the main-character tool loop (start-generation.ts:3426-3448) catches the throw, sets success=false, and pushes the error back as the tool result; the Spotify playback fallback (agent-executor.ts:562-570) likewise returns a structured error. Only the agent tool loop hard-aborts.

Steps to reproduce

  1. Enable chat tools (chat.metadata.enableTools) with a tool-using agent active (e.g. Spotify DJ).
  2. Have the agent's LLM emit a tool_call missing a required argument (common with small/local models).
  3. executeBuiltInTool → toolError() throws; the whole agent run aborts with makeError, wasting the turn.

Impact area

  • Runtime systems: generation, world-state, visuals, tracker, or haptics
  • React-free engine behavior, contracts, agents, or capability ports

Environment

  • Branch or commit: refactor @ b0e5561d
  • Run mode: Tauri desktop (shared engine; also server mode)
  • OS + version: not relevant (static source audit)
  • Node + pnpm versions: not relevant
  • Rust version, if relevant: not relevant
  • Remote Runtime URL configured? not relevant (applies to both)
  • Provider, model, or integration, if relevant: any tool-using agent; worst with small/local models

Logs, screenshots, or video

Static source audit; cited to file:line above.

Regression notes

Found via static audit of the refactor branch; not verified against main. Gated behind opt-in enableTools + an active tool-using agent; blast radius is one wasted agent turn (recoverable next turn), hence medium.

Additional context

Suggested fix: in the catch at line 375, do not re-throw on tool errors — capture extractErrorMessage(err), emit a tool-result success:false, and push role:"tool" content stringifyToolResult({ error: ... }) with tool_call_id: tc.id so the model can retry within MAX_TOOL_ROUNDS. Re-throw only on AbortSignal cancellation, mirroring start-generation.ts:3426-3448.

Template check

Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 5, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2322 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Medium — Correctness / robustness.** When a tool-using agent (Spotify DJ, haptic, or any custom-tool agent) makes a tool call with missing/invalid arguments, the agent tool loop re-throws the error and aborts the entire agent run, producing no usable output for the turn. The model is never given the error to self-correct. This is asymmetric with the rest of the codebase, which feeds tool errors back to the model. ## Expected behavior A tool-call error should be captured and returned to the model as a `role:"tool"` result with `success:false`, so the model can retry/recover within the remaining tool rounds — exactly as the main-character tool loop already does. ## Actual behavior `src/engine/agents-runtime/executor/agent-executor.ts:372-378`, inside `executeAgentWithTools`' per-tool-call loop: ```ts let toolResult: string; try { toolResult = await toolContext.executeToolCall(tc); } catch (err) { logger.error(err, "[agent-tools] %s %s failed", config.type, tc.function.name); throw err; } ``` The re-throw unwinds the round loop and is caught only by `executeAgent`'s outer try (catch at 279-281), returning `makeError(...)`. `executeBuiltInTool` (`tools-runtime.ts:331`) calls `toolError()` (`throw new Error(message)`) for every missing required arg — `roll_dice`/notation, `update_game_state`/type+target+key, `search_lorebook`/query, `spotify_play`/uri|uris, `write_chat_variable`/key, etc. — and `executeToolCall` routes built-ins straight there with no try/catch. This is asymmetric: the main-character tool loop (`start-generation.ts:3426-3448`) catches the throw, sets `success=false`, and pushes the error back as the tool result; the Spotify playback fallback (`agent-executor.ts:562-570`) likewise returns a structured error. Only the agent tool loop hard-aborts. ## Steps to reproduce 1. Enable chat tools (`chat.metadata.enableTools`) with a tool-using agent active (e.g. Spotify DJ). 2. Have the agent's LLM emit a tool_call missing a required argument (common with small/local models). 3. `executeBuiltInTool` → `toolError()` throws; the whole agent run aborts with `makeError`, wasting the turn. ## Impact area - [x] Runtime systems: generation, world-state, visuals, tracker, or haptics - [x] React-free engine behavior, contracts, agents, or capability ports ## Environment - Branch or commit: refactor @ b0e5561d - Run mode: Tauri desktop (shared engine; also server mode) - OS + version: not relevant (static source audit) - Node + pnpm versions: not relevant - Rust version, if relevant: not relevant - Remote Runtime URL configured? not relevant (applies to both) - Provider, model, or integration, if relevant: any tool-using agent; worst with small/local models ## Logs, screenshots, or video Static source audit; cited to file:line above. ## Regression notes Found via static audit of the refactor branch; not verified against main. Gated behind opt-in `enableTools` + an active tool-using agent; blast radius is one wasted agent turn (recoverable next turn), hence medium. ## Additional context Suggested fix: in the catch at line 375, do not re-throw on tool errors — capture `extractErrorMessage(err)`, emit a tool-result `success:false`, and push `role:"tool"` content `stringifyToolResult({ error: ... })` with `tool_call_id: tc.id` so the model can retry within `MAX_TOOL_ROUNDS`. Re-throw only on `AbortSignal` cancellation, mirroring `start-generation.ts:3426-3448`. ## Template check Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:27:58 +07:00
Author
Owner

@Romuromylus commented on GitHub (Jun 6, 2026):

Fixed by #2397, merged into refactor.

<!-- gh-comment-id:4637945980 --> @Romuromylus commented on GitHub (Jun 6, 2026): Fixed by #2397, merged into `refactor`.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1149
No description provided.