Repository navigation
fix: record the real model, drop injected turns - #8
Merged
Merged
Conversation
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 reviews are paused because the subscription is no longer active. Ask your workspace admin to reactivate the subscription to resume reviews. Manage billing |
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. |
Member
|
Yeah. Same. AI writes the tools for itself ;) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
from the prompt(s):
Fixes two bugs in the Claude Code
UserPromptSubmithook that between themmade the recorded prompt history untrustworthy.
The recorded model was always
claude-sonnet-4-6The hook did
jq -r '.model // "claude-sonnet-4-6"', but the payload has nomodelfield, so the fallback fired on every prompt. In one repository, all594 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
/modelswitch is still attributed to the previous one, and the firstprompt of a session — where nothing has answered yet — records
unknownrather than a guess.
Claude Code's own injected turns were recorded as user prompts
.promptwas 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 verbatiminto commit messages. Turns are now filtered on the payload's
sourcefield,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.jsonis replaced by a stdlib-only script installed to.claude/hooks/record-prompt.pybyprepare-ai-repository. This also dropsjq, previously a hard dependency that was documented nowhere and whoseabsence silently produced empty prompt files.
The hook command guards the file and forces exit 0, because
python3exits 2for a missing file and Claude Code treats exit 2 on this hook as "block
processing, erase original prompt".
Also fixed
/each dropped the prompt instead of recording it.
O_EXCL, so two sessions recording in thesame second cannot overwrite each other.
record-ai-promptread its sequence number from the wrong dash-separatedfield of the timestamp — the minute, not the sequence.
prepare-ai-repositoryappended a blank line to.github/assistant-guidelines.mdon every run.name-tests-testdemanded*_test.pywhile this project usestest_*.py,so it failed on every test file.
Testing
98 tests pass,
toxpasses, all pre-commit hooks pass. New tests cover thesourcefiltering, transcript model lookup, and each of the ways a promptcould 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 installsthe new script and rewrites the hook command.