#4337·rtk

hook claude: JSON-escaped command occasionally corrupts \b into a raw backspace, causing Claude Code to reject the tool call

Author: iwan-uschkaCreated Sep 28, 2026Updated Sep 28, 2026
Labelsbugarea:clipriority:mediumneeds-reproduction

Summary

rtk hook claude (used as a Claude Code PreToolUse:Bash hook) occasionally emits malformed JSON for a rewritten command: a regex/tool escape sequence that should round-trip as the two literal characters \b (backslash + b) gets serialized as the single JSON escape \b, which decodes to an actual backspace byte (0x08) instead. Claude Code's schema validator then rejects the whole tool call: "command contains control characters that would be hidden in the approval dialog".

Exact incident (from the Claude Code session transcript)

Agent issued this Bash command:

grep -n "SomeFunctionName\b" -A 40 <repo>/Sources/SomeModule/somefile.c | head -80

This is a normal, valid command — \b is grep's word-boundary escape, two literal characters.

The PreToolUse:Bash hook wrapper is:

input=$(cat); result=$(echo "$input" | rtk hook claude); if [ -n "$result" ] && echo "$result" | jq -e '.hookSpecificOutput.updatedInput.command | strings | startswith("rtk ") and (startswith("rtk proxy ") | not)' > /dev/null 2>&1; then echo "$result" | jq '.hookSpecificOutput.permissionDecision = "allow"'; else printf '%s' "$result"; fi

rtk hook claude's raw stdout for this call, verbatim:

{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecisionReason":"RTK auto-rewrite","updatedInput":{"command":"rtk grep -n \"SomeFunctionName\b\" -A 40 <repo>/Sources/SomeModule/somefile.c | head -80","description":"Look at SomeFunctionName implementation for failure conditions"}}}

Note the \"SomeFunctionName\b\" — a single-backslash JSON escape. Decoding this JSON (confirmed with Python's json.loads) yields the command string ...SomeFunctionName\x08"... — a literal backspace byte where a literal backslash+b was expected. Correct output would have escaped the backslash as \\b (three raw characters in the JSON text: \, \, b).

Claude Code's own denial for the resulting tool call:

PreToolUse hook for Bash returned updatedInput that failed schema validation: [
  {
    "code": "custom",
    "path": ["command"],
    "message": "command contains control characters that would be hidden in the approval dialog"
  }
]

Reproduction attempts

Reconstructing the exact PreToolUse hook input payload from the transcript (same session id, same command text, same description) and piping it through rtk hook claude five times in a row, immediately after finding this, produced correctly-escaped output (\\b) every time — no corruption. So this is not a deterministic per-input bug; feeding the identical input fresh does not reproduce it.

A concrete lead, not confirmed: rtk's tracking feature ([tracking] enabled = true in config.toml) writes every invocation to a single shared history.db file (in this installation, ~34 MB and growing continuously). Each PreToolUse hook call spawns a fresh rtk process, and in an environment like Claude Code's afk-tickets driver, several of these can run concurrently across tickets/repos. Lock contention or a shared code path around that DB write, occurring at the same time as command-JSON serialization, seems like a plausible trigger for an intermittent, timing-dependent escaping bug — but this hasn't been verified against rtk's actual source or internals.

Impact

When it hits, the corrupted JSON kills the entire tool call with no indication that rtk's rewrite (rather than the original command or Claude Code itself) caused it — the agent just sees an opaque schema-validation failure.

Ask

Given the non-determinism and the correlation with a possibly-shared/concurrent write path, this looks like a concurrency issue in whatever code serializes the rewritten command to JSON, rather than a straightforward one-input escaping bug. Filing primarily so it's on record; happy to share more detail from the original session transcript (full sequence of tool calls around the incident) if that helps narrow it down.