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
feat(hooks): Hook Enhancement v2 - Response Structuring & Expanded Capture#6
Conversation
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>
There was a problem hiding this comment.
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:patternssyntax 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 |
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>
2b55236 to
365a913
Compare
… 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>
365a913 to
9a6ba56
Compare
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>
70651e7 to
780e86a
Compare
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>
- 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>
- 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>
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>
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Comprehensive enhancement to git-notes-memory plugin hooks for improved capture reliability and expanded context injection.
Key Features
[remember:decisions],@memory:patternssyntax (~10% → ~60% namespace targeting)Spec Artifacts
New Skill: memory-assistant
Comprehensive Claude skill for memory capture and recall guidance:
Key Research Findings
additionalContext, uses stderr for user feedbackImplementation Phases
Test plan
🤖 Generated with Claude Code