Skip to content

fix(hooks): skip tool_result messages when counting human exchanges#779

Open
arnoldwender wants to merge 2 commits intoMemPalace:developfrom
arnoldwender:fix/hooks-tool-result-counting
Open

fix(hooks): skip tool_result messages when counting human exchanges#779
arnoldwender wants to merge 2 commits intoMemPalace:developfrom
arnoldwender:fix/hooks-tool-result-counting

Conversation

@arnoldwender
Copy link
Copy Markdown
Contributor

@arnoldwender arnoldwender commented Apr 13, 2026

Summary

Fixes #549 — exchange count was inflated by counting tool_result messages as human turns.

In Claude Code JSONL, every tool response (Bash, Read, Grep, subagent results) is emitted with role: "user" and content as a list of {"type": "tool_result", ...} blocks. Both counting paths treated these as real human messages, so a session with 10 real turns but 50 tool calls was reported as 60 exchanges — causing premature save-hook triggers at the wrong frequency.

Changes

  • mempalace/hooks_cli.py _count_human_messages(): In the elif isinstance(content, list): branch, added an early continue that fires when all blocks in the list have "type": "tool_result". The guard is placed before the existing <command-message> check, leaving that logic untouched.
  • hooks/mempal_save_hook.sh: Added the identical tool_result guard to the inline Python script (also before the <command-message> check).

Both changes are purely additive — no existing counting logic was modified, only a new continue path inserted for tool_result-only content lists.

Test plan

  • All 32 existing tests/test_hooks_cli.py tests pass (verified locally)
  • A new test test_count_skips_tool_result_messages should be added to directly exercise the new guard — noted for follow-up
  • Manual: create a JSONL transcript with 10 real human messages plus 50 tool_result entries; confirm count returns 10, not 60

@arnoldwender arnoldwender changed the title fix(kg): prevent edge duplication with partial UNIQUE index and atomic upsert fix(hooks): skip tool_result messages when counting human exchanges Apr 13, 2026
@arnoldwender arnoldwender force-pushed the fix/hooks-tool-result-counting branch from 7aa0be3 to f26f695 Compare April 13, 2026 10:04
@igorls igorls added area/hooks Claude Code hook scripts (Stop, PreCompact, SessionStart) bug Something isn't working labels Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/hooks Claude Code hook scripts (Stop, PreCompact, SessionStart) bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: save hook counts tool_result messages as human messages, inflating exchange count

2 participants