Skip to content

refactor: Claude Code recall is a thin shim over hooklib#12

Open
rainxchzed wants to merge 1 commit into
mainfrom
refactor/hooklib-shared-core
Open

refactor: Claude Code recall is a thin shim over hooklib#12
rainxchzed wants to merge 1 commit into
mainfrom
refactor/hooklib-shared-core

Conversation

@rainxchzed
Copy link
Copy Markdown
Contributor

What

Collapses the architect's HIGH finding from the visibility review: the Claude Code adapter reimplemented the whole recall core inline instead of using the shared hooklib that Codex consumes as a 5-line shim — and the compaction feature got bolted onto that fork, so the richer recall core was the off-spec one.

This lifts the Claude-Code-only behaviors into hooklib (rather than deleting them, which would regress the 0.5.0 fixes), then makes Claude Code a thin shim like Codex.

Pure refactor — behavior-neutral. Full suite 383 passed / 1 skip, identical to before.

Lifted into hooklib (all opt-in; Codex's existing calls unchanged)

  • read_stdin_json() gains the 4MB cap → Codex gets the hardening for free
  • build_recall_block(..., fresh=True)fresh=False skips the project reindex + 500-row pool re-mirror (the compaction perf path)
  • classify_event(payload, default_event)default_event wins over a missing hook_event_name
  • emit(obj, *, note, event) — event-aware (suppress JSON on PostCompact's verbatim-stdout channel) + pipe-safe (swallow BrokenPipe); defaults preserve every caller
  • emit_recall(block, event, is_compaction) — format-per-event (additionalContext vs plain stdout) with the compaction framing
  • compaction_already_served / record_compaction_served — the dedup guard + state.json breadcrumb, host-agnostic

Result

claude_code/hook_recall.py: 360 → 175 lines. main() orchestrates via hooklib; the duplicated _merged_store / _mirror_pool_into_index / _read_stdin_json / _emit / _emit_block / _classify_event / dedup bodies are gone. The "never break the session" guards (recall-failure _note, emit broken-pipe) are preserved exactly. hook_compact still passes default_event="PostCompact".

Also: codex/paths.py gains read_state() (parity, so the lifted dedup works for any host); hook_capture.py references hooklib._MAX_STDIN_BYTES.

Tests

Re-pointed the moved-symbol references in test_compaction_fixes.py at their new home (hooklib.classify_event/emit/read_stdin_json/_MAX_STDIN_BYTES, and the fresh= behavior via build_recall_block) — the behavioral assertions are unchanged. Every existing compaction/visibility/distill/Codex suite passes against the refactor; that's the regression guarantee for a pure refactor.

This is the first of the deferred 0.5.0-architecture items; directory-split storage + the smaller ones land as separate PRs before 0.5.0 ships.

🤖 Generated with Claude Code

…e fork)

The visibility review's HIGH architectural finding: the Claude Code adapter
reimplemented the whole recall core inline instead of using the shared hooklib
that Codex consumes cleanly — and the compaction feature (PR #9) got bolted onto
that stranded fork, so the richer of the two recall cores was the off-spec one.

This collapses the fork by LIFTING the Claude-Code-only behaviors INTO hooklib
(not deleting them — that would regress the 0.5.0 fixes), then making Claude Code
a thin shim like Codex. Pure refactor: full suite 383 passed / 1 skip, identical
to before.

Lifted into hooklib (all opt-in, so Codex's existing calls are unchanged):
- read_stdin_json() now has the 4MB cap (Codex gains the hardening for free)
- build_recall_block(..., fresh=True) — fresh=False skips the project reindex +
  500-row pool re-mirror (the compaction perf path)
- classify_event(payload, default_event) — event/source, default_event wins
- emit(obj, *, note, event) — event-aware (suppress JSON on PostCompact) +
  pipe-safe (swallow BrokenPipe); defaults preserve every existing caller
- emit_recall(block, event, is_compaction) — format-per-event (additionalContext
  vs plain stdout) with the compaction framing
- compaction_already_served / record_compaction_served — the dedup guard +
  state.json breadcrumb, host-agnostic (uses paths_mod.read_state/update_state)

claude_code/hook_recall.py: 360 → 175 lines. main() now orchestrates via hooklib;
the duplicated _merged_store / _mirror_pool_into_index / _read_stdin_json / _emit /
_emit_block / _classify_event / dedup bodies are gone. The background pool-sync
cadence (_maybe_sync_pool/_sync_due/run_sync) stays local — it's Claude-Code config,
not the duplicated recall core. The "never break the session" guards (recall-failure
_note, emit broken-pipe) are preserved exactly. hook_compact still passes
default_event="PostCompact".

Also: codex/paths.py gains read_state() (parity, so the lifted dedup works for any
host); hook_capture.py references hooklib._MAX_STDIN_BYTES.

Tests: re-pointed the moved-symbol references in test_compaction_fixes.py at their
new home (hooklib.classify_event/emit/read_stdin_json/_MAX_STDIN_BYTES, and the
fresh= behavior via build_recall_block) — the behavioral assertions are unchanged.
All existing compaction/visibility/distill/Codex suites pass against the refactor.

Co-Authored-By: Claude Opus 4.8 <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.

1 participant