58a9554bb3
- Created new tables for SDK sessions and observations - Implemented HooksDatabase for CRUD operations - Developed four hook functions: context, new, save, and summary - Added CLI commands for each hook - Established comprehensive test suite with all tests passing feat: Complete Phase 2 implementation with SDK worker process and XML parsing - Developed SDK prompts for initializing and processing observations - Implemented XML parser for SDK responses - Created SDK worker process to handle background observation processing - Verified integration with HooksDatabase and added tests for all components feat: Complete Phase 3 integration with comprehensive testing and validation - Verified all hook functions with database integration - Created integration and end-to-end tests for session lifecycle - Ensured non-blocking operations and performance requirements met - Updated CLI commands for hook architecture and installation flow - Documented success criteria and next steps for real-world testing
4.2 KiB
4.2 KiB
Phase 2 Implementation Prompt
Use this prompt to start a new chat for Phase 2 implementation:
Context
I'm implementing a refactor of the claude-mem memory system based on REFACTOR-PLAN.md.
Phase 1 is complete (see PHASE1-COMPLETE.md):
- ✅ Database schema with migration 004
- ✅ HooksDatabase shared layer
- ✅ All four hook functions (context, new, save, summary)
- ✅ CLI integration and tests passing
Task
Implement Phase 2: SDK Worker Process
According to REFACTOR-PLAN.md (lines 296-423), I need to:
-
Create SDK Worker Process (
src/sdk/worker.ts)- Uses Agent SDK streaming input mode
- AsyncIterable message generator that:
- Yields initial prompt
- Polls observation_queue table
- Yields observation prompts
- Handles FINALIZE message
- Parses SDK responses for
<observation>and<summary>XML blocks - Stores results using HooksDatabase methods
-
Create SDK Prompts (
src/sdk/prompts.ts)buildInitPrompt()- Initialize agent (see REFACTOR-PLAN.md:537-595)buildObservationPrompt()- Send tool observation (see REFACTOR-PLAN.md:601-634)buildFinalizePrompt()- Request summary (see REFACTOR-PLAN.md:640-692)
-
Create XML Parser (
src/sdk/parser.ts)- Parse
<observation>blocks with<type>and<text> - Parse
<summary>blocks with 8 required fields - Extract file arrays from
<file>child elements
- Parse
-
Update newHook (src/hooks/new.ts)
- Uncomment SDK worker spawn code
- Pass session ID to worker
- Detached process with stdio: 'ignore'
-
Test End-to-End
- Create test that simulates full lifecycle
- Verify observations are queued, processed, and stored
- Verify summary generation works
Key Requirements
From REFACTOR-PLAN.md:
- Use
@anthropic-ai/claude-agent-sdkquery function with streaming input mode - Model:
claude-sonnet-4-5 - Use
disallowedTools: ['Glob', 'Grep', 'ListMcpResourcesTool', 'WebSearch'] - Message generator yields
{ role: "user", content: string }objects - Capture SDK session ID from system init message
- Poll observation queue every 1 second
- Use AbortController for graceful cancellation
- Parse XML with a library (not regex) - suggest fast-xml-parser
- Store observations and summaries using HooksDatabase methods
Architecture Reference
The SDK worker is a synthesis engine that:
- Receives tool observations (not raw data)
- Extracts meaningful insights
- Stores atomic observations in SQLite
- Generates structured summaries at session end
See REFACTOR-PLAN.md (lines 69-119) for the full architecture diagram.
Files to Create
src/sdk/worker.ts- Main SDK worker processsrc/sdk/prompts.ts- Prompt builderssrc/sdk/parser.ts- XML response parsersrc/sdk/index.ts- Exportstest-phase2.ts- End-to-end tests
Files to Modify
- src/hooks/new.ts - Spawn worker process
- package.json - May need to add fast-xml-parser dependency
Testing Strategy
- Unit tests for prompts (verify prompt structure)
- Unit tests for parser (verify XML parsing)
- Integration test for worker (mock SDK responses)
- End-to-end test (simulate full observation → summary flow)
Success Criteria
- SDK worker runs as detached process
- Worker polls observation queue continuously
- Worker sends observations to Claude SDK
- Worker parses
<observation>and<summary>XML correctly - Worker stores results in database using HooksDatabase
- Worker handles FINALIZE message and exits gracefully
- All tests pass
- No blocking of main Claude Code session
Notes
- Keep hooks fast and non-blocking (they already are)
- SDK worker is fire-and-forget background process
- Use HooksDatabase methods (already implemented in Phase 1)
- Follow the exact prompt formats from REFACTOR-PLAN.md
- Use proper TypeScript types from Agent SDK
Start with: Create the SDK prompts module first, then the parser, then the worker. Test each piece before integrating.