Kiro icon indicating copy to clipboard operation
Kiro copied to clipboard

kiro-cli 2.6.1: headless session crashes with Bedrock ValidationException "duplicate Ids" after preToolUse hook blocks the same tool call twice

Open MarcelKarras opened this issue 5 months ago • 6 comments

Before opening, please confirm:

Operating System

macOS 26.5.1 (arm64, Apple Silicon)

Kiro Version

kiro-cli 2.6.1

Bug Description

In a headless session (kiro-cli chat --no-interactive --trust-all-tools --agent <agent>), when a preToolUse hook blocks the same tool call twice in a row (hook command exits with code 2 both times), the client builds an invalid conversation history: the retried fs_write reuses the original tooluse_… ID, so the next request contains two toolResult blocks with the same ID in one message. Bedrock rejects the request with a ValidationException, the session dies (panic-style error report pointing at crates/chat-cli/src/cli/chat/mod.rs:1905), and the process exits with a misleading final error.

Sequence observed:

  1. Model calls fs_write on a file; the preToolUse hook denies it (exit 2, reason on stderr). Kiro correctly reports PreToolHook blocked the tool execution: … and feeds the reason back to the model.

  2. The model retries the same write without satisfying the hook; the hook denies it again (exit 2).

  3. Immediately after the second consecutive block, the next API request fails:

    Kiro is having trouble responding right now:
       0: Failed to send the request: An unknown error occurred: ValidationException
       ...
       5: Error { code: "ValidationException", message: "Bedrock error message: The toolResult blocks at messages.24.content contain duplicate Ids: tooluse_ffcWvFpZOIectHyIfUOV5d", aws_request_id: "c98d629d-59c5-437c-83b6-aa78fbc876ab" }
    
    Location:
       crates/chat-cli/src/cli/chat/mod.rs:1905
    
  4. The session then terminates with:

    error: Tool approval required but --no-interactive was specified. Use --trust-all-tools to automatically approve tools.
    

    This final message is doubly wrong: --trust-all-tools was passed (the session banner even confirmed "All tools are now trusted"), and the actual failure is the duplicate-ID ValidationException, not an approval problem.

A single hook block is handled fine; the crash needs two consecutive blocks of the same tool call. This makes any policy-enforcing preToolUse hook (lint gates, write protection, TDD gates) a session-killer in headless/CI usage as soon as the model retries once.

Steps to Reproduce

  1. Create a minimal agent at ~/.kiro/agents/block-repro.json whose preToolUse hook always blocks writes:

    {
      "name": "block-repro",
      "description": "Repro: preToolUse hook that always blocks fs_write",
      "tools": ["*"],
      "hooks": {
        "preToolUse": [
          { "matcher": "fs_write", "command": "echo 'write blocked by policy' >&2; exit 2" },
          { "matcher": "write", "command": "echo 'write blocked by policy' >&2; exit 2" }
        ]
      }
    }
    
  2. Run a headless chat that makes the model attempt the same write at least twice:

    kiro-cli chat --no-interactive --trust-all-tools --agent block-repro \
      "Create a file named hello.txt containing 'hello'. If the write is rejected, retry the exact same write once more."
    
  3. Observe: first block is reported normally; after the model retries and is blocked a second time, the session crashes with the Bedrock ValidationException ("toolResult blocks … contain duplicate Ids: tooluse_…") at crates/chat-cli/src/cli/chat/mod.rs:1905, followed by the spurious error: Tool approval required but --no-interactive was specified.

Any agent config whose preToolUse hook exits 2 twice for the same tool call reproduces it.

Expected Behavior

  • Each retried tool call gets a unique tooluse ID in the conversation history (or each hook denial produces exactly one toolResult per ID), so the request stays valid and the session survives any number of hook denials. Hook-blocked tool calls should be a recoverable, repeatable condition — the model should be able to keep correcting course.
  • If an API-level ValidationException does occur, the CLI should fail with the real error and a non-zero exit, not follow up with the unrelated/incorrect "Tool approval required but --no-interactive was specified" message while --trust-all-tools is active.

Conversation ID

n/a (headless kiro-cli chat --no-interactive); AWS request ID of the failing call: c98d629d-59c5-437c-83b6-aa78fbc876ab

Additional Context

Full stderr tail of the crashing session (ANSI stripped; the hook-denial reason is shortened — it appeared twice in a row, the second consecutive block of the same fs_write immediately preceding the failure):

PreToolHook blocked the tool execution: fs_write on /private/var/folders/.../project/src/calc.js blocked. [policy reason returned to the model]

Kiro is having trouble responding right now:
   0: Failed to send the request: An unknown error occurred: ValidationException
   1: An unknown error occurred: ValidationException
   2: unhandled error (ValidationException)
   3: service error
   4: unhandled error (ValidationException)
   5: Error { code: "ValidationException", message: "Bedrock error message: The toolResult blocks at messages.24.content contain duplicate Ids: tooluse_ffcWvFpZOIectHyIfUOV5d", aws_request_id: "c98d629d-59c5-437c-83b6-aa78fbc876ab" }

Location:
   crates/chat-cli/src/cli/chat/mod.rs:1905

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.
error: Tool approval required but --no-interactive was specified. Use --trust-all-tools to automatically approve tools.

Possibly related (other ValidationException session-killers with different triggers): #5068, #4947, #7796.

MarcelKarras avatar Jun 10 '26 20:06 MarcelKarras

🤖 Potential Duplicate Detected

This issue appears to be similar to:

What happens next?

  • ⏰ This issue will be automatically closed in 3 days
  • 🏷️ If this is not a duplicate, you can prevent automatic closure by adding a comment or reacting with 👎 to this message.
  • 💬 Comment on the original issue if you have additional information

Why is this marked as duplicate? Both issues involve PreToolUse hooks not working correctly - the new issue describes crashes when hooks block tool calls twice, while #8264 reports PreToolUse hooks not working correctly on Windows. Both involve the same hook mechanism failing in different ways.

github-actions[bot] avatar Jun 10 '26 20:06 github-actions[bot]

Not a duplicate of #8264. That issue reports the opposite behavior: on Windows 11, a preToolUse hook exiting 2 does not block the tool call at all (enforcement broken; the file gets written anyway), and it explicitly notes the hooks work fine on macOS.

This issue is about what happens after blocking works correctly (macOS, kiro-cli 2.6.1): when the same tool call is denied twice in a row, the conversation history ends up with two toolResult blocks sharing one tooluse_… ID, Bedrock rejects the next request with a ValidationException ("contain duplicate Ids"), and the headless session crashes at crates/chat-cli/src/cli/chat/mod.rs:1905 — followed by a spurious "Tool approval required but --no-interactive was specified" even though --trust-all-tools was set.

Same subsystem (preToolUse hooks), different bug: #8264 = hook denial not enforced on Windows; this one = hook denial enforced, but the retry corrupts the request payload and kills the session.

MarcelKarras avatar Jun 11 '26 07:06 MarcelKarras

Thank you for your feedback! 🙏

The duplicate label has been removed and this issue has been sent back for maintainer review.

A maintainer will review this issue shortly.

github-actions[bot] avatar Jun 11 '26 07:06 github-actions[bot]

This looks like the important boundary is between approval state and hook-enforced denial state.

If a preToolUse hook exits 2, I would treat that tool call as completed from the transcript's perspective: it has exactly one terminal toolResult for that tool_use_id, with the denial reason returned to the model. If the model retries, the retry should either be a new tool call with a new id, or the client should avoid appending another result block for the original id. What should never happen is two terminal results for the same id in one message.

A useful acceptance test might be:

  1. Run kiro-cli chat --no-interactive --trust-all-tools with a preToolUse hook that denies fs_write twice.
  2. Confirm the second denial remains a normal hook-denied outcome, not a request-history corruption.
  3. Confirm the process exits or continues with the real hook/validation state, not Tool approval required when all tools were already trusted.
  4. Assert the serialized outbound message never contains duplicate toolResult ids.

That keeps the semantics clean for headless use: --trust-all-tools can remove interactive approval prompts, but it should not override a policy hook; the hook denial should remain fail-closed, auditable, and repeatable without corrupting conversation state.

rpelevin avatar Jun 11 '26 10:06 rpelevin

Thanks for the detailed report and root-cause analysis. The hooks system and conversation-history builder have been significantly reworked since CLI 2.6.1 — including fixes to tool-result ID management and hook payload handling.

Could you retest on the latest version (CLI 2.15.0) and let us know whether the duplicate-ID crash still occurs when a preToolUse hook blocks the same tool call twice in a headless session?

To update: download from kiro.dev.

AnilMaktala avatar Jul 29 '26 20:07 AnilMaktala

This issue has been automatically closed due to inactivity. It has been 7 days since we requested additional information.

If you still need help with this issue, please feel free to reopen it or create a new issue with the requested details.

github-actions[bot] avatar Aug 06 '26 00:08 github-actions[bot]