Skip to content

fix: record the real model, drop injected turns - #8

Merged
tobixen merged 1 commit into
mainfrom
fix/hook-model-and-synthetic-turns
Oct 4, 2026
Merged

tobixen merged 1 commit into
mainfrom
fix/hook-model-and-synthetic-turns

Conversation

@tobixen

@tobixen tobixen commented Aug 26, 2026

Copy link
Copy Markdown
Member

I have had some problems with this one, and I've mostly delegated to Claude to figure out of it. Claude came up with an overly long suggestion for a commit message, I've hand-edited it, but the description below is left-as-is as for now.


⚠️ AI-generated by Claude Opus 5 via Claude Code on behalf of tobixen,
from the prompt(s):

I have big problems with this one, i.e. in ~/caldav - it now seems to record "sonnet" for all the work I do with opus, and it leaks lots of output from subtasks/subagents into the commit messages. Most of them have been rewritten, but have a look at the reflog there.
(Claude came up with some alternative design solutions) I'm not well-enough into the details of this project to have strong opinions on this - so I would like to hear some recommendations and arguments. My five cents: I think that there is a risk that jq is not installed everywhere. At the other hand, this project already contains python code, and python has the json library built-in.
(Based on more input from Claude) If jq is an undocumented hard dependency today, then the "no new dependency"-argument seems invalid for the python-script idea. (...) However, keeping things in python probably gives easier maintenance in the long run. Don't ask me how to make this bugfix, just do it in the best possible way.
/review-and-push

Fixes two bugs in the Claude Code UserPromptSubmit hook that between them
made the recorded prompt history untrustworthy.

The recorded model was always claude-sonnet-4-6

The hook did jq -r '.model // "claude-sonnet-4-6"', but the payload has no
model field, so the fallback fired on every prompt. In one repository, all
594 archived prompt files carry that name while the session transcripts show
a different model throughout.

The model is now read from the session transcript. It is a heuristic: a
transcript names a model only once it has answered, so the first prompt after
a /model switch is still attributed to the previous one, and the first
prompt of a session — where nothing has answered yet — records unknown
rather than a guess.

Claude Code's own injected turns were recorded as user prompts

.prompt was written unconditionally, but background-task notifications,
peer messages and auto-continuation arrive through the same hook. 84 of those
594 files are <task-notification> blobs, which were then copied verbatim
into commit messages. Turns are now filtered on the payload's source field,
with a prefix check on the known wrapper tags for payloads that omit it.

The hook is now Python

Both fixes need real parsing, so the shell one-liner in
.claude/settings.json is replaced by a stdlib-only script installed to
.claude/hooks/record-prompt.py by prepare-ai-repository. This also drops
jq, previously a hard dependency that was documented nowhere and whose
absence silently produced empty prompt files.

The hook command guards the file and forces exit 0, because python3 exits 2
for a missing file and Claude Code treats exit 2 on this hook as "block
processing, erase original prompt".

Also fixed

  • An unreadable transcript, non-UTF-8 stdin, or a model id containing /
    each dropped the prompt instead of recording it.
  • Prompt files are created with O_EXCL, so two sessions recording in the
    same second cannot overwrite each other.
  • Prompts written in the same second were listed newest-first.
  • record-ai-prompt read its sequence number from the wrong dash-separated
    field of the timestamp — the minute, not the sequence.
  • prepare-ai-repository appended a blank line to
    .github/assistant-guidelines.md on every run.
  • name-tests-test demanded *_test.py while this project uses test_*.py,
    so it failed on every test file.

Testing

98 tests pass, tox passes, all pre-commit hooks pass. New tests cover the
source filtering, transcript model lookup, and each of the ways a prompt
could previously be lost; the fixes were mutation-checked by reverting them
and confirming the tests go red.

Upgrading an existing repository requires re-running
pre-commit run --hook-stage manual prepare-ai-repository
, which installs
the new script and rewrites the hook command.


⚠️ AI-generated by Claude Opus 5 via Claude Code on behalf of tobixen; see the prompts at the top.

The UserPromptSubmit hook did `jq -r '.model // "claude-sonnet-4-6"'`,
but that payload has no `model` field.  Possibly this is a change in
behaviour in Claude Code.

The model is now read from the session transcript named by
transcript_path - the heuristics is not 100% reliable.  The first prompt
after a /model switch may be misattributed to the model switched away
from.  The new fallback is "unknown" rather than a guess.

The hook also wrote `.prompt` unconditionally.  Claude Code submits
machine-injected turns through UserPromptSubmit, this caused noise and
potentially data leakages.  Payload is now filtered by `source` ("user"
or "sdk" for the human, and "system", "loop_wakeup", "schedule_wakeup"
or "poll_event" otherwise).

Both fixes need real parsing, so the shell one-liner in
.claude/settings.json is replaced by a stdlib-only Python script,
bundled as package data and installed to .claude/hooks/record-prompt.py
by prepare-ai-repository.  It cannot import this package — the console
scripts live in pre-commit's isolated cache venv, off the assistant's
PATH — hence a self-contained copy.  This also removes jq, previously
a hard, undocumented dependency.

A clean-context review found other bugs and issues that have been
fixed in this commit.

My setup stopped me from pushing the commit until I had declared that
I've personally reviewed it.  I have looked through the changeset, but
I admit that it was not a very thorough review.

Prompt: I have big problems with this one, i.e. in ~/caldav - it now seems to record "sonnet" for all the work I do with opus, and it leaks lots of output from subtasks/subagents into the commit messages.  Most of them have been rewritten, but have a look at the reflog there.
Followup-Prompt: (answer to a clarifying question) I'm not well-enough into the details of this project to have strong opinions on this - so I would like to hear some recommendations and arguments.  My five cents:  I think that there is a risk that jq is not installed everywhere.  At the other hand, this project already contains python code, and python has the json library built-in.
Followup-Prompt: (answer to a clarifying question) If jq is an undocumented hard dependency today, then the "no new dependency"-argument seems invalid for the python-script idea.  If it's easier to toss in a jq-command somewhere else, then it's just to do it.  However, keeping things in python probably gives easier maintenance in the long run.  Don't ask me how to make this bugfix, just do it in the best possible way.
Followup-Prompt: /review-and-push [stage-2 review; the "clean-context review" mentioned above is its findings, fixed after approval to fix all of them]
Followup-Prompt: document returned with a comment [the push-review gate document, with the commit message rewritten by hand]
Followup-Prompt: (answer to a clarifying question) It has been working earlier, and I'm quite sure the original logic was not hallucinated up.  So most likely some versions of Claude Code have used the model field

Co-authored-by: Claude Opus 5 (AI) <noreply@anthropic.com>
Reviewed-by: Tobias Brox <tobias@redpill-linpro.com>
@qodo-code-review

Copy link
Copy Markdown

ⓘ Qodo reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing

@tobixen
tobixen merged commit 247d337 into main Oct 4, 2026
6 checks passed
@tobixen

tobixen commented Oct 4, 2026

Copy link
Copy Markdown
Member Author

It was complicated testing the changes without having a version number, so I'll just merge this, release and test it. I'm having a hard time fighting with this commit hook.

@niccokunzmann

Copy link
Copy Markdown
Member

Yeah. Same. AI writes the tools for itself ;)
Thanks for the fixes! Looking foreard to the release.

@tobixen
tobixen deleted the fix/hook-model-and-synthetic-turns branch October 6, 2026 09:26
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.

2 participants