Skip to content

trace: leave Claude Code's background-task notifications out of the prompt count - #208

Open
Muhammad Abdullah Butt (1AbdullahButt) wants to merge 1 commit into
microsoft:mainfrom
1AbdullahButt:fix/claude-task-notification-prompts
Open

Muhammad Abdullah Butt (1AbdullahButt) wants to merge 1 commit into
microsoft:mainfrom
1AbdullahButt:fix/claude-task-notification-prompts

Conversation

@1AbdullahButt

Copy link
Copy Markdown
Contributor

Fixes #207

When a background task finishes, Claude Code tells the model in a user entry of its own, marked origin.kind: "task-notification" (2.1.317). _is_prompt accepted it, so claude_prompt_count counted it as a prompt the person gave. That count is the session's prompts in the fold. Tool pruning adds it to each unused tool's prompt streak, so a session that ran background commands moved a deny list closer than the prompts typed in it. History import reads the same count for its no_prompt and headless checks.

This PR adds _is_notification, which reads origin.kind, and leaves those entries out of claude_prompt_count. _is_prompt is unchanged, so claude_prompt_turns still starts a turn at a notification. The model answers it in a turn of its own, and merging that into the earlier prompt's turn would change the loop guard's envelope.

The test is in tests/test_trace.py, next to the existing prompt-count test. One typed prompt, then a notification shaped like the ones 2.1.317 writes. The test checks that the count is 1 and that the log still reads as two turns.

How it was produced: Claude Code, an AI coding agent, found the bug by running the trace readers over the Claude Code logs on my machine. It also wrote the fix and the test. I directed the work.

What was checked, on Windows 11 with Python 3.10, on main at 81e32e5:

  • Without the fix, the new test fails: the count is 2.
  • With the fix, tests/test_trace.py and tests/test_history_import.py pass.
  • Full tests run: python -m pytest tests reports 888 passed, 24 skipped and 0 failed. The skips need tools this machine does not have. I did not run experiments/tests.
  • Over the 223 Claude Code logs on my machine (2.1.295 to 2.1.317), the count drops by the 354 entries that carry origin.kind == "task-notification", and nothing else changes.

🤖 Generated with Claude Code

…rompt count

Claude Code tells the model a background task finished in a user entry
of its own (`origin.kind` "task-notification"). `_is_prompt` accepted it,
so `claude_prompt_count` counted it as a prompt the person gave, and tool
pruning's prompt evidence ran ahead of what was typed. The notification
still starts a turn, so `claude_prompt_turns` is unchanged.

Fixes microsoft#207

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

trace: Claude Code's background-task notifications count as prompts the person gave

1 participant