Repository navigation
fix(hooks): stop the Stop hook nudge reaching the user twice - #720
Conversation
No additional code defects found from the diff-only review. |
Closes Tencent#719. Claude Code prints the Stop payload as "Stop hook feedback". The payload opened with "Print the following message verbatim to the user", so the user read the order, then read the message again from the model. The reminder documented in docs/usage-guide.md carries no such line. - buildHint returns the message. The relay order is added only where the host hides the payload: Cursor's followup_message and the stash that UserPromptSubmit delivers. - The stashed hint runs last in the combined payload, so "the following" has an end. The votes nudge instructs the model and is not relayed. - codex-internal and tcodex join STOP_STDOUT_UNSUPPORTED_TOOLS. They run the same Codex, which rejects Stop additionalContext, so their users lost the hint with no stash to recover it. - formatStopHookOutput loses the codex systemMessage branch. Every caller checks the set first, so nothing has reached it since it shipped. - The votes nudge and the recall rule's declaration section are English, matching the wording compileRecallRulesBlock already ships.
Review findings on the previous commit. - formatStopHookOutput is shape only again. relayWhenHidden decides whether a message addressed to the user needs the model to pass it on, and the contribute hint is the only caller. Wrapping the channel also wrapped the recall declaration nudge, which instructs the model and would have been read out to Cursor users as terminal noise. - stopStdoutUnsupported lowercases the tool id. Removing the codex branch took the only toLowerCase with it, so `--tool Codex` would have taken the Stop path Codex rejects. - CODEX_TOOL_IDS lives in agent-format beside ToolName, and the Stop gate and the hook-format detector both read it, so a new variant is added once. - The votes nudge test moves to hook-handlers, where the function lives. The formatter test no longer claims to guard Tencent#719; relayWhenHidden and the stash test do, and reintroducing the bug turns the stash test red.
4cdccc1 to
cbb7940
Compare
|
Rebased onto 1.
|
| tool | Stop stdout field | hint in Stop | order in Stop | stashed | order in stash | next-prompt field | hint delivered |
|---|---|---|---|---|---|---|---|
claude |
Stop.additionalContext |
yes | no | no | — | — | yes |
opencode |
Stop.additionalContext |
yes | no | no | — | — | yes |
cursor |
followup_message |
yes | yes | no | — | — | yes |
codebuddy |
(empty) | — | — | yes | yes | UserPromptSubmit.additionalContext |
yes |
codex |
(empty) | — | — | yes | yes | UserPromptSubmit.additionalContext |
yes |
codex-internal |
(empty) | — | — | yes | yes | UserPromptSubmit.additionalContext |
yes |
tcodex |
(empty) | — | — | yes | yes | UserPromptSubmit.additionalContext |
yes |
The shared stashed UserPromptSubmit path, which is what you flagged
CodeBuddy is unchanged. I rebuilt dist/ from origin/main's sources in this same worktree and diffed the captured run against the rebuilt branch:
$ diff before-codebuddy.txt after-codebuddy.txt && echo IDENTICAL
IDENTICALSame field, same single relay order, same delivery on the next prompt. The order moved from buildHint to the stash write, and for CodeBuddy those are the same byte.
OpenCode is the fix. Before (built from origin/main sources) and after:
before after
opencode Stop.additionalContext Stop.additionalContext
carries order : True carries order : False
→ user reads the order, then the → user reads the message once
same message again from the model
Text the OpenCode user now reads, verbatim from the payload:
[teamai] This session may contain a problem worth documenting: you interrupted the AI twice, the AI retried failing tools 8 times.
Task: Fix the auth retry loop then add regression coverage
Consider running /teamai-share-learnings to summarize what you learned and share it with your team.
CodeBuddy's next-prompt payload keeps the order, because its host shows the user nothing:
Print the following message verbatim to the user (do NOT paraphrase, summarize, or explain its origin):
[teamai] This session may contain a problem worth documenting: you interrupted the AI twice, the AI retried failing tools 8 times.
...
Mutation check
Restoring the prefix in buildHint and rebuilding turns OpenCode's Stop payload back to carries order : True, and double-prefixes CodeBuddy's stash — the two symptoms the split was meant to separate.
The Test Plan in the description has been rewritten to match all of the above.
|
cbb7940 to
7af3b52
Compare
|
Findings
Per instruction, I reviewed the diff only and did not run tests or builds. |
… deterministic The three win32 login-shell fallback cases never stubbed SHELL, so they read the host's. detectShellProfile short-circuits to .zshrc when SHELL says zsh, which means these passed on CI (SHELL unset) and failed on every zsh developer machine. Test-only; detectShellProfile is unchanged.
…'s discarded stdout Review of Tencent#720 raised two points, both correct. CODEX_TOOL_IDS lived in resources/agent-format.ts, so utils/tool-names.ts — a leaf utility that hook-handlers.ts imports statically on every dispatch — took a dependency on a module that pulls yaml, gray-matter, smol-toml and builtin-hooks. The constant moves to tool-names.ts and ToolName comes back as a type-only import, which is erased. hooks.ts and agent-format.ts read it from there. hostShowsStopPayload classified OpenCode as a host that displays the Stop payload. OpenCode's plugin spawns hook-dispatch with stdout ignored, so no payload reaches the session on any event. The classification is inert either way for OpenCode, but the comment said otherwise; it now names the gap, and opencode-hooks.test.ts pins the constraint so a future change that starts relying on that stdout has to come through the test.
|
Both new findings were right. The OpenCode one in particular — I claimed delivery I had not proven, and the claim was false. Details below, followed by the earlier rounds for anyone reading this cold. New round[P1] OpenCode delivery was claimed, not provenYou are correct on every part of it. // src/opencode-hooks.ts
const child = spawn('teamai', args, {
stdio: ['pipe', 'ignore', 'ignore'],
});and the generated plugin says so in its own header: "OpenCode cannot inject a hook's stdout back into the session, so this runs the dispatch purely for its side effects." My record drove The consequence is worse than "the classification is wrong": on OpenCode the hint reaches neither the user nor the model, on So there is no viable delivery path to implement inside #719's scope. #719 is "the nudge is printed twice"; OpenCode's is "the nudge is never printed", a missing delivery path that predates this PR and needs its own issue and its own design (the plugin would have to return context from What this PR now does instead of claiming otherwise:
If you would rather this PR not touch OpenCode's classification at all until that issue exists, say so and I will drop the comment and the test and leave only the corrected description. [P2]
|
The PR description includes a detailed test plan and real-CLI end-to-end verification record, so no testing-description blocker. |
|
On the remaining [P2]: you are right that it does not trace to #719. I kept it because removing it re-opens the [P1] that blocked this PR in round 1. Round 1 blocked on the Test Plan claiming green while $ npx vitest run src/__tests__/shell-profile.test.ts -> 3 failed | 22 passed
$ env -u SHELL npx vitest run src/__tests__/shell-profile.test.ts -> 25 passedThree win32 cases never stub So without the commit there are only two outcomes, and both cost more than the rule they would satisfy:
It is one |
Summary
The Stop hook nudge reached the user twice. Claude Code prints the Stop payload as "Stop hook feedback", and the payload opened with an order addressed to the model, so the user read the order and then read the message again from the model. The reminder documented in
docs/usage-guide.mdcarries no such line.The order belongs to the message, not to the channel that carries it:
Which leaves one rule per delivery path, and no path where the user reads an order meant for the model:
hookSpecificOutput.additionalContextfollowup_messageadditionalContext, after a stashThree more defects surfaced while reading that dispatch, all in this PR:
codex-internalandtcodexare supported targets whose hook command carries the raw id, so they missed the set, took the Stop path Codex rejects, and never reached the stash that would have recovered the hint. ThesystemMessagebranch was added by the same commit that addedcodexto the set (09a2efd, #440), so every caller has gated it out since the day it shipped.CODEX_TOOL_IDSnow lives inutils/tool-names.ts, and both the Stop gate and the hook-format detector read it, so a future variant is added once.Evidence
The repro from the issue, run against the built CLI. It seeds a session with friction and reads the field Claude Code renders.
Before (installed 0.24.0, and
origin/main):After:
The Codex variants, before and after, through the same binary:
The regression test is red on the original bug. Restoring the prefix in
buildHintand rerunning:Test Plan
Every item ran on this branch and passed. Host: macOS,
SHELL=/bin/zsh.npx tsc --noEmitcleannpm run buildoknpx vitest run268 files, 3791 passed, 1 skipped, 0 failednpm run test:e2e42 files, 172 passed, 26 skippedcursor,codex-internal,tcodexUserPromptSubmitdelivery diffed byte-for-byte against adist/rebuilt fromorigin/mainsources: identicalbuildHintturnscodex-stop-hint.test.tsred and double-prefixes CodeBuddy's stashrelayWhenHidden,stopStdoutUnsupported, the stash carrying the order while the printed copy does not, the Codex variants stashing, the nudge and rule section being English, and OpenCode's discarded stdoutReal-CLI record
Every row is the built CLI driven through the installed hook entry point,
node dist/index.js hook-dispatch <event> --tool <id>, with an isolatedHOMEper tool, seeded with a high-friction session (50 tool calls / 7 unique tools,
then a Stop carrying 2 interrupts and 8 tool errors). "order" is the
Print the following message verbatim to the userline.UserPromptSubmit.additionalContextStop.additionalContextUserPromptSubmit.additionalContextStop.additionalContextcursorfollowup_messagecodex-internalUserPromptSubmit.additionalContexttcodexUserPromptSubmit.additionalContextCodeBuddy — unchanged. This is the shared stashed
UserPromptSubmitpath thereview flagged.
dist/was rebuilt fromorigin/mainsources in the sameworktree and the captured run diffed against the branch:
Same field, same single relay order, same delivery on the next prompt. The order
moved from
buildHintto the stash write, and for CodeBuddy those are the samebyte.
OpenCode — correcting an earlier claim in this description. An earlier
revision said the hint "reaches the user" on OpenCode. That was wrong, and the
review was right to block it. The generated plugin spawns the dispatch with
stdout discarded:
and the generated file says so itself: "OpenCode cannot inject a hook's stdout
back into the session, so this runs the dispatch purely for its side effects."
So on OpenCode the hint reaches neither the user nor the model, on
origin/mainand on this branch alike. Adding
opencodetoSTOP_STDOUT_UNSUPPORTED_TOOLSwould not help either: the
UserPromptSubmitpayload the stash is deliveredthrough goes out of the same ignored stdout. My table row came from driving
hook-dispatchdirectly, which never goes through the adapter — that was thehole in the record, and this row now states the adapter's behaviour instead.
That gap predates #719 and is a missing delivery path, not a double-delivery
bug, so it is not fixed here. What this PR does about it:
hostShowsStopPayloadno longer implies OpenCode displays anything, and
opencode-hooks.test.tspinsthe discarded stdout so the next change that starts relying on it has to come
through the test rather than repeat the claim. It needs its own issue.
Review points addressed
CODEX_TOOL_IDSmoved fromresources/agent-format.tstoutils/tool-names.ts.hook-handlers.tsimportstool-names.tsstatically on every dispatch, and theold placement made that leaf utility depend on a module pulling yaml,
gray-matter, smol-toml and builtin-hooks.
ToolNameis now a type-only import,erased at runtime. Measured honestly: those three packages are already at the
top level of the bundle through another path, and
hook-dispatch stoptimed ata 141 ms median before and 140 ms after (n=12), so this buys module hygiene and
a dependency that points the right way, not a measurable latency win.
One pre-existing flake, in a file this branch does not touch
local-agent.test.ts > emits hint only once per sessionId: ~2 of 3 full-suiteruns, 0 of 8 runs of that file alone. It does not appear in
git diff origin/main...HEAD --name-only. Cause not pinned; it needs its ownissue. The run recorded above is green.
Provider matrix: not applicable. This PR changes hook output shape and tool-id matching, and touches no clone, auth or provider path.
Type of Change
Related Issues
Closes #719.
Merge Danger
Door: two-way.
Nothing is deleted from a user's machine and no state migrates. Reverting restores the previous strings on the next release, and the only durable artifact is
pendingHintin~/.teamai/sessions/*.json, which is consumed on the next prompt and rewritten every session.Blast Radius: the reminder text every member reads at the end of a session.
Three things a reviewer should weigh. The nudge wording changes for everyone, which is the point, but it is the most visible string the CLI prints. Cursor now receives the relay order from a different place in the code, so a Cursor user is the one to check if the reminder stops arriving. And the recall rule in
src/builtin-rules.tsis now English in its declaration section and Chinese elsewhere: that split was chosen deliberately, because translating the whole rule changes deployed content for every team with recall on and belongs in its own PR.Notes for Reviewers
src/builtin-rules.tsis the file to look at first if the mixed language bothers you. The alternative was translating all 51 lines ofTEAMAI_RECALL_RULE_CONTENT, which lands in every agent directory on the next pull.The
<!-- teamai:referenced-doc-ids: ... -->marker is written by three producers (hook-handlers.ts,builtin-rules.ts,pull.ts) and read by one parser (transcript-parser.ts). That split predates this PR, which only translates two of the producers, so extracting a shared constant is left out rather than smuggled in. It is the obvious next cleanup if the marker ever changes.teamai-wikiis still never pruned from machines that came from 0.13.0 or 0.16.x, and the other two privateCODEX_TOOLScopies incoauthor-reconcile.tsandmcp-format.tswere left alone because they are not hook-delivery paths. Both are follow-ups rather than omissions.