Ray's Knowledge Base
Unlock

Debug a wrong lesson from the failure hook by replaying the failure payload

RecipeVerified 29 Sep 2026Holds project: rkb
Recipe. When to use it, the steps, and what showed that they work.

When to use#

A PostToolUseFailure hook injected a lesson that has nothing to do with the error, or none when one should match, and you want to see why and check a fix.

Steps#

  1. Save the failure as a hook payload, with the exact command and error text (a Python json.dumps keeps quotes and newlines right):

    {"session_id": "replay-1", "cwd": "<repo>", "tool_name": "Bash", "tool_input": {"command": "<command>"}, "error": "<error text>"}
    
  2. Replay it with a throwaway state folder, so the "once per session" rule and your real session log do not interfere:

    rm -rf $S/state; XDG_STATE_HOME=$S/state rkb hook tool-failed < $S/payload.json
    

    Output with <rkb-lesson id=...> means it injected; no output means it did not.

  3. Add RKB_NO_MODEL=1 to test the word-coverage fallback that runs when the model is cold or times out; without it you test the model path.

  4. To see the search query the hook built, print rkb_core::hooks::error_query(command, error) from a scratch test, then run rkb search "<query>" --format json and look at ranked_by and each relevance.

  5. After a fix, build and replay the same payload; it must give the expected result.

Evidence#

rkb, 2026-09-29: a sed syntax error injected an unrelated pi lesson. The replay reproduced it; the printed query showed a line of plain grep output taken as an error line (the error words lacked invalid) and a 3-word line counted as a strong match. After the fix (rkb commit 4b129d3), the same replay injected nothing, with rkb 0.0.6 in 0.56 s.

Also found by searching

  • PostToolUseFailure hook injected <rkb-lesson id=...> about pi for a sed syntax error
  • the failure hook keeps suggesting an unrelated lesson for my error, I want to reproduce why
  • how do I replay a tool failure payload through rkb hook tool-failed to debug which lesson it picks?
  • XDG_STATE_HOME throwaway state, RKB_NO_MODEL=1 fallback, print error_query to see the search terms
  • PostToolUseFailure hook injected an irrelevant rkb lesson for my error, how do I debug
  • how do I replay a failed tool call through the rkb hook to see which lesson matches?
  • rkb hook shows no lesson when one should match, test with a payload json
  • simulate hook payload session_id cwd tool_name error with throwaway state folder