Skip to content

Bug: preemptive-compaction hook built but never registered in hooks.json #2180

@voravitl

Description

@voravitl

Summary

The preemptive-compaction hook is built and exists in dist/hooks/preemptive-compaction/index.js, but it is never registered in hooks/hooks.json under PostToolUse. This means the HUD context warning feature silently does nothing.

Root Cause

  1. Not registered in hooks.json — the hook script exists in dist/ but no entry is added to hooks.json during build/install.
  2. In-memory state doesn't work for shell hooks — the original createPreemptiveCompactionHook() uses an in-memory Map() to track accumulated token counts, but each hook invocation spawns a new process, so state is always lost. The hook would never reach its threshold.
  3. Token estimation is unrealistic — the original hook only counts tool_response content characters ÷ 4 as token estimate. A single tool response would need to be 680k+ characters to trigger at 170k tokens — practically never happens.

Environment

  • OMC version: 4.10.2
  • Claude Code version: 2.1.90
  • macOS (Darwin)

Workaround

I rewrote the hook to use the transcript-file approach (same as pre-tool-enforcer and context-guard-stop) — reading input_tokens and context_window from the transcript's last 4KB. This is accurate and works reliably across process boundaries.

Key changes:

  • Reads transcript_path from stdin JSON (provided by Claude Code)
  • Uses resolveTranscriptPath() for git worktree compatibility
  • File-based cooldown (1 min per cwd in /tmp/) to prevent warning spam
  • Warns at 70%, critical at 90%

Suggested Fix

Register the hook in hooks.json under PostToolUse and switch to transcript-based token reading instead of in-memory accumulation.

Would be happy to contribute a PR if helpful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    agentsRelated to agent definitionsbugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions