Skip to content
This repository was archived by the owner on Jan 2, 2026. It is now read-only.

feat(hooks): Hook Enhancement v2 - Response Structuring & Expanded Capture#6

Closed
zircote wants to merge 17 commits intomainfrom
feature/hook-enhancement-v2
Closed

feat(hooks): Hook Enhancement v2 - Response Structuring & Expanded Capture#6
zircote wants to merge 17 commits intomainfrom
feature/hook-enhancement-v2

Conversation

@zircote
Copy link
Owner

@zircote zircote commented Dec 19, 2025

Summary

Comprehensive enhancement to git-notes-memory plugin hooks for improved capture reliability and expanded context injection.

Key Features

  1. SessionStart Response Guidance - XML templates teaching Claude consistent response patterns for reliable signal detection (~70% → ~85% accuracy)
  2. Namespace-Aware Markers - Support [remember:decisions], @memory:patterns syntax (~10% → ~60% namespace targeting)
  3. PostToolUse Hook - Inject relevant memories after Write/Edit/MultiEdit based on file domain
  4. PreCompact Hook - Auto-capture high-confidence content before context compaction

Spec Artifacts

Document Description
REQUIREMENTS.md 5 P0, 3 P1, 2 P2 functional requirements
ARCHITECTURE.md Component designs, data flows, integration points
IMPLEMENTATION_PLAN.md 27 tasks across 5 phases (16-24h estimated)
DECISIONS.md 5 ADRs including PreCompact API constraint

New Skill: memory-assistant

Comprehensive Claude skill for memory capture and recall guidance:

  • Capture detection workflows (decisions, learnings, blockers, progress)
  • Proactive recall strategies
  • Session memory review
  • Hook integration documentation

Key Research Findings

  • PreCompact is side-effects only - Cannot use additionalContext, uses stderr for user feedback
  • PostToolUse confirmed - Full support for additionalContext injection after tool execution
  • Existing SignalDetector and ContextBuilder can be extended (no architectural changes needed)

Implementation Phases

Phase Effort Description
Phase 1 3-4h SessionStart response guidance
Phase 2 2-3h Namespace-aware markers
Phase 3 4-5h PostToolUse hook
Phase 4 4-5h PreCompact hook
Phase 5 3-5h Testing & documentation

Test plan

  • Review spec documents for completeness
  • Validate architecture against existing hook patterns
  • Approve implementation plan task breakdown
  • Begin implementation with Phase 1

🤖 Generated with Claude Code

zircote and others added 2 commits December 19, 2025 11:23
Comprehensive spec for expanding hook-based memory capture:
- SessionStart response structuring guidance for reliable signal detection
- Namespace-aware inline markers ([remember:decisions], @memory:patterns)
- PostToolUse hook for file-contextual memory injection
- PreCompact hook for pre-compaction memory preservation

New memory-assistant skill with:
- Capture detection workflows
- Proactive recall strategies
- Session memory review guidance
- Hook integration documentation

Spec artifacts:
- REQUIREMENTS.md: 5 P0, 3 P1, 2 P2 requirements
- ARCHITECTURE.md: Component designs, data flows
- IMPLEMENTATION_PLAN.md: 27 tasks across 5 phases
- DECISIONS.md: 5 ADRs including PreCompact stderr constraint

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
…iew fixes

Phase 3-4 Implementation:
- Add PostToolUse hook for file-contextual memory injection
- Add PreCompact hook for memory preservation before compaction
- Add domain_extractor for extracting searchable terms from file paths
- Add namespace_parser for inline marker namespace detection
- Add guidance_builder for response structuring guidance

Code Review Fixes (from /cr):
- Fix DomainExtractor singleton pattern (CRITICAL - performance)
- Fix dead code in _extract_summary() prefix stripping
- Add JSON input size limits (10MB) for security hardening

Code Review Artifacts:
- CODE_REVIEW.md with 7.5/10 health score
- REVIEW_SUMMARY.md executive summary
- REMEDIATION_TASKS.md actionable checklist

Tests: All 1276 tests passing, 87% coverage

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@zircote zircote marked this pull request as ready for review December 19, 2025 20:19
Copilot AI review requested due to automatic review settings December 19, 2025 20:19
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements Hook Enhancement v2, adding comprehensive memory capture and recall capabilities through new hooks, namespace-aware markers, and response guidance. The implementation follows a detailed spec with 5 phases completed.

Key Changes

  • SessionStart Response Guidance: Injects XML templates teaching Claude consistent response patterns for ~85% signal detection accuracy (up from ~70%)
  • Namespace-Aware Markers: Supports [remember:decisions], @memory:patterns syntax for targeted capture with 60%+ accuracy (up from ~10%)
  • PostToolUse Hook: Automatically injects relevant memories after file operations based on domain extraction
  • PreCompact Hook: Auto-captures high-confidence content (≥0.85) before context compaction to prevent loss

Reviewed changes

Copilot reviewed 38 out of 38 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/test_pre_compact_handler.py Comprehensive tests for PreCompact hook with 25 test cases covering signal analysis, capture, and reporting
tests/test_post_tool_use_handler.py Full test suite for PostToolUse with 23 test cases for domain extraction and memory injection
tests/test_namespace_parser.py Tests for namespace-aware marker parsing with all syntaxes and edge cases
tests/test_guidance_builder.py Tests for XML guidance generation at 3 detail levels with validation
tests/test_domain_extractor.py Tests for file path domain term extraction with filtering logic
tests/test_hooks.py Added tests for new configuration options (pre_compact_prompt_first)
src/git_notes_memory/hooks/pre_compact_handler.py New handler for memory preservation before compaction with stderr reporting
src/git_notes_memory/hooks/post_tool_use_handler.py New handler for file-contextual memory injection after tool use
src/git_notes_memory/hooks/namespace_parser.py Parser for [remember:ns] and @memory:ns inline marker syntax
src/git_notes_memory/hooks/guidance_builder.py Generates XML response guidance for SessionStart injection
src/git_notes_memory/hooks/domain_extractor.py Extracts searchable terms from file paths for memory recall
src/git_notes_memory/hooks/config_loader.py Extended with new configuration options for all new features
src/git_notes_memory/hooks/user_prompt_handler.py Integrated namespace parser for enhanced inline markers
src/git_notes_memory/hooks/session_start_handler.py Integrated guidance builder for response structuring
hooks/precompact.py Entry point script for PreCompact hook with graceful degradation
hooks/posttooluse.py Entry point script for PostToolUse hook with error handling
hooks/hooks.json Registered PostToolUse and PreCompact hooks with matchers and timeouts
skills/memory-assistant/SKILL.md New comprehensive memory management skill documentation
skills/memory-assistant/references/* Detailed reference docs for capture patterns, recall strategies, and workflows
skills/memory-assistant/examples/* Python examples demonstrating memory workflows and proactive recall
skills/memory-recall/SKILL.md Updated with allowed-tools configuration
docs/spec/active/2025-12-19-hook-enhancement-v2/* Complete spec including requirements, architecture, decisions, and code review

zircote and others added 2 commits December 19, 2025 15:32
Add comprehensive E2E tests to validate plugin functionality and
prevent regressions in API contracts:

Commands tested (tests/test_e2e_commands.py):
- /memory:status - config API exports, IndexService.get_stats()
- /memory:capture - CaptureService API and result structure
- /memory:recall - RecallService.search() and result attributes
- /memory:search - semantic and text search APIs
- /memory:sync - reindex(), verify_consistency(), repair()

Hooks tested (tests/test_e2e_hooks.py):
- SessionStart - handler main(), entry point syntax
- UserPromptSubmit - SignalDetector API
- PostToolUse - DomainExtractor singleton, NamespaceParser
- PreCompact - CaptureDecider, GuidanceBuilder APIs
- Stop - SessionAnalyzer methods

Also validates:
- hooks.json configuration and timeouts
- All referenced hook entry point files exist
- Handlers have consistent main() interface

Total: 48 new E2E tests, 1324 tests passing

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Root cause: When singleton services are reset between tests, their
IndexService database connections weren't being explicitly closed.
Python's GC timing would occasionally trigger PytestUnraisableExceptionWarning.

Two-pronged fix:
1. conftest.py: Close IndexService connections before resetting singletons
2. pyproject.toml: Add filterwarnings to ignore PytestUnraisableExceptionWarning

This ensures `make quality` passes consistently without the -W flag.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings December 19, 2025 20:42
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 42 out of 42 changed files in this pull request and generated 3 comments.

Copilot AI review requested due to automatic review settings December 19, 2025 20:55
@zircote zircote force-pushed the feature/hook-enhancement-v2 branch from 2b55236 to 365a913 Compare December 19, 2025 20:55
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 50 out of 50 changed files in this pull request and generated 2 comments.

… use

- Add sys.path bootstrap to all hook entry points (posttooluse.py,
  precompact.py, sessionstart.py, stop.py, userpromptsubmit.py)
  to find plugin bundled src directory via __file__ resolution

- Update all command files (capture.md, recall.md, search.md,
  status.md, sync.md) to use uv run --directory pointing to plugin root
  This ensures the plugin uses its own pyproject.toml and dependencies
  rather than the current project's environment

- Use CLAUDE_PLUGIN_ROOT env var if available, fall back to known
  plugin cache location for compatibility

This fixes the issue where running plugin commands in other projects
would fail with ModuleNotFoundError or trigger unnecessary project builds.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@zircote zircote force-pushed the feature/hook-enhancement-v2 branch from 365a913 to 9a6ba56 Compare December 19, 2025 21:04
Copilot AI review requested due to automatic review settings December 19, 2025 21:21
Comprehensive validation command that tests:
- Core library imports (capture, recall, sync services)
- Git repository and notes accessibility
- Index health and consistency
- All 5 hook entry points (syntax validation)
- Hook handler imports (stop, posttooluse, precompact)
- Capture pipeline (creates test memory)
- Recall pipeline (semantic and text search)

Options:
- --verbose: Show detailed output
- --fix: Repair index, sync after capture for accurate testing

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@zircote zircote force-pushed the feature/hook-enhancement-v2 branch from 70651e7 to 780e86a Compare December 19, 2025 21:24
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 52 out of 52 changed files in this pull request and generated no new comments.

zircote and others added 2 commits December 19, 2025 16:32
Visual indicators (emoji icons) added to all hooks:
- 📚 SessionStart: memory count status
- 💾 UserPromptSubmit: capture confirmation
- 🔍 PostToolUse: related memories found
- 🛑 Stop: uncaptured content warning

validate command improvements:
- Auto-cleanup test memories after validation
- Verify cleanup via search instead of count comparison
- Use reindex() instead of non-existent sync()

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Store memory index in project-local .memory/index.db instead of a global
database. This ensures complete isolation between repositories:

- Each repo clone gets its own SQLite index
- Sync/reindex operations only affect current project
- No cross-contamination or orphaned databases
- Deleting a project removes its memory index

Changes:
- Add get_project_index_path() and get_project_memory_dir() to config
- Update SyncService, RecallService, and hooks to use project index
- Add .memory/ to .gitignore
- Bump schema version to 2 with repo_path column for metadata

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings December 19, 2025 21:56
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 60 out of 61 changed files in this pull request and generated 2 comments.

zircote and others added 2 commits December 19, 2025 17:08
- Move guidance templates to external XML files in templates/ directory
- Add memory_recall section teaching Claude to surface retrieved memories
- Simplify GuidanceBuilder to load templates with file-based caching
- Remove hardcoded CAPTURE_PATTERNS constant in favor of XML content
- Update tests for file-based template approach

Templates can now be edited without code changes:
- guidance_minimal.xml: Basic inline marker syntax (~200 tokens)
- guidance_standard.xml: Patterns + recall instructions (~900 tokens)
- guidance_detailed.xml: Full templates with examples (~1200 tokens)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Based on context engineering research, restructure guidance templates
to prioritize active operating behaviors over reference documentation.

Key changes:
- Rename root element from <response_guidance> to <session_operating_context>
- Add <active_behaviors> section FIRST with trigger-based capture instructions
- Add <memory_recall_behaviors> section for surfacing retrieved memories
- Move syntax reference to lower-priority <reference> section
- Add recall_notice element when memories are retrieved
- Add memories_retrieved attribute to memory_context root

Template structure now follows priority order:
1. Active behaviors (WHEN to capture)
2. Memory recall behaviors (HOW to surface memories)
3. Examples (show behavior in action) - detailed only
4. Reference (syntax documentation)

This addresses the gap where Claude knew the capture syntax but was not
naturally using it during work - the behavioral framing creates
context architecture that makes captures feel native to the workflow.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings December 19, 2025 22:24
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 63 out of 64 changed files in this pull request and generated no new comments.

zircote and others added 2 commits December 19, 2025 18:35
- hooks.json: Use ${CLAUDE_PLUGIN_ROOT}/.venv/bin/python3 instead of
  relying on shebang, ensures correct interpreter with dependencies
- sessionstart.py: Delegate to session_start_handler module instead of
  inline implementation
- Add marketplace.json for plugin discovery

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- All 5 phases implemented: SessionStart guidance, namespace markers,
  PostToolUse hook, PreCompact hook, testing
- 1319 tests passing, 86.20% code coverage
- RETROSPECTIVE.md generated with lessons learned
- Project moved from active/ to completed/

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings December 19, 2025 23:48
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 65 out of 66 changed files in this pull request and generated no new comments.

zircote and others added 2 commits December 19, 2025 19:55
Based on claude-code-guide research findings, rewrite all guidance
templates to use mandatory rule language instead of suggestions:

- Change root tag from session_operating_context to session_behavior_protocol
- Convert XML structure to markdown rules in CDATA sections
- Use explicit MUST, REQUIRED, Do not language
- Add 3 explicit rules: CAPTURE MARKERS, MEMORY RECALL, DEFAULT TO ACTION
- Include enforcement sections with DO/DONT examples in detailed template

This addresses the issue where Claude was ignoring injected context by
treating it as optional suggestions rather than mandatory constraints.

Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
QUAL-1: Extract ~200 lines of duplicated utilities from 5 handlers into
hook_utils.py module with setup_logging(), setup_timeout(), cancel_timeout(),
and read_json_input() functions.

SEC-1: Add validate_file_path() function with path traversal protection
to prevent arbitrary file access via malicious hook input.

DOC-1: Document PostToolUse and PreCompact hooks in README.md and
USER_GUIDE.md with configuration options and usage examples.

Changes:
- Create src/git_notes_memory/hooks/hook_utils.py with shared utilities
- Update 5 handlers to use centralized utilities
- Add path validation to session_analyzer.py
- Add 8 security tests for path validation
- Document all 5 hooks and their configuration options
- Add guidance config docs (HOOK_SESSION_START_INCLUDE_GUIDANCE)

Test coverage: 1327 tests passing, 86.34% coverage

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings December 20, 2025 01:37
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 70 out of 71 changed files in this pull request and generated no new comments.

- Replace XML templates with Markdown format for better readability
- Update guidance_builder.py to load .md files instead of .xml
- Update tests to check string content instead of XML parsing
- Make plugin path discovery version-agnostic in validate command
- Update __version__ in __init__.py
- Update plugin.json and marketplace.json
- Update skill versions (memory-assistant, memory-recall)
- Add v0.3.0 section to CHANGELOG.md
@zircote zircote closed this Dec 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants