35 Commits

Author SHA1 Message Date
Alex Newman 562194612e Remove advanced search functionality from search-server and SessionSearch classes
- Deleted the advanced_search tool from the search-server.ts file, which combined full-text search with structured filters for observations and sessions.
- Removed the advancedSearch method from the SessionSearch class, which handled the logic for performing advanced searches using FTS5 and structured filters.
2025-10-21 19:12:36 -04:00
Alex Newman 02bce8a6e6 Enhance observation parsing and querying functionality
- Filter out observation type from concepts array in parseObservations function to ensure types and concepts are treated as separate dimensions. Added logging for removed types.
- Update prompts documentation to clarify that the observation type must not be included in the concepts array.
- Modify search-server to provide clearer guidance on result limits, emphasizing starting with smaller limits to avoid exceeding token limits.
- Refactor SessionSearch methods to accept options for limit, offset, and orderBy parameters, improving flexibility in querying observations by concept and type.
2025-10-21 19:01:11 -04:00
Alex Newman e9bcb7e9db Enhance search tips and tool descriptions for improved user guidance
- Updated the formatSearchTips function to emphasize the importance of using index format first for token efficiency.
- Revised descriptions for various search tools to include reminders about starting with index format and using full format only for specific items of interest.
- Clarified output format descriptions to recommend index format for initial searches.
2025-10-21 17:36:22 -04:00
Alex Newman ef572ec032 Refactor summary generation from session-level to request-level
Changed summary prompts to generate discrete per-request summaries instead of cumulative session summaries. This provides better chronological memory where each summary is a clean unit representing one request/response cycle.

Changes:
- Renamed buildFinalizePrompt() to buildSummaryPrompt() in src/sdk/prompts.ts
- Updated prompt text to focus on "THIS REQUEST" rather than "this session"
- Updated all import and function call sites in worker-service.ts and worker.ts
- Added IMPORTANT warning to emphasize request-level scope

Expected behavior: Each summary will now describe only what happened during that specific request, eliminating cumulative recaps.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 17:27:14 -04:00
Alex Newman 3cb99ab7f4 Swap 'Completed' and 'Learned' output formatting in context hook for clarity; adjust color coding accordingly 2025-10-21 16:41:27 -04:00
Alex Newman 20136121d8 Refactor context hook output formatting to improve readability; remove unnecessary line breaks 2025-10-21 16:40:14 -04:00
Alex Newman df0f3fd96c Enhance context hook output formatting and add file aggregation for sessions
- Updated contextHook to support colorized output for terminal and JSON format for hooks.
- Introduced ANSI color codes for improved readability in terminal output.
- Modified the output structure to include session details with color formatting.
- Added a new method in SessionStore to aggregate files read and modified from observations for a session.
- Improved error handling for JSON parsing of file data in the new method.
2025-10-21 16:37:16 -04:00
Alex Newman 32f45a1100 Add search tips and format options for observation and session search results
- Introduced `formatSearchTips` function to provide helpful search tips in the output.
- Added formatting options for search results, allowing users to choose between 'index' (titles/dates only) and 'full' (complete details).
- Updated handlers for observation and session search tools to accommodate the new format option.
- Enhanced result formatting to include date information and improved output structure.
2025-10-21 15:34:39 -04:00
Alex Newman 1ea692e0b0 chore: Update license information and add user settings manager script 2025-10-21 15:12:34 -04:00
Alex Newman d275715974 Release v4.1.0 - Graceful session cleanup and MCP search server restoration
### Changed
- Graceful session cleanup: Cleanup hook now marks sessions as completed instead of sending DELETE requests to worker
- Natural worker shutdown: Workers now finish pending operations (like summary generation) before terminating
- Restored MCP search server: Re-enabled full-text search capabilities from backup

### Fixed
- Session summaries no longer interrupted by aggressive cleanup during session end
- Workers can now complete final operations before shutdown

### Dependencies
- Updated @anthropic-ai/claude-agent-sdk to ^0.1.23
- Added @modelcontextprotocol/sdk ^1.20.1
- Added zod-to-json-schema ^3.24.6

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 01:07:56 -04:00
Alex Newman fd4684dcb3 Refactor search server to use Model Context Protocol SDK; update tool handling and response formatting
- Replaced `createSdkMcpServer` with `Server` from `@modelcontextprotocol/sdk/server/index.js`.
- Updated tool definitions to use structured input schemas with Zod.
- Enhanced response formatting for search results, combining multiple results into a single text response.
- Added new tools for advanced search and recent session context retrieval.
- Improved error handling and logging throughout the server.
2025-10-21 01:03:35 -04:00
Alex Newman c42444e06c Refactor cleanupHook to remove HTTP session deletion and mark session as completed in the database 2025-10-21 00:59:06 -04:00
Alex Newman cb2f2a0432 fix: Update command in SessionStart hook for correct node_modules path and remove package.json 2025-10-20 18:18:07 -04:00
Alex Newman 692bb07dfe fix: Add pm2 dependency to package.json for improved process management 2025-10-20 18:07:46 -04:00
Alex Newman 651989c423 refactor: Replace TypeScript bootstrap with bash dependency checks
Simplified dependency installation by moving from TypeScript runtime bootstrap to bash-based checks in plugin manifest. This reduces complexity and code size while maintaining the same functionality.

Changes:
- Added bash conditional dependency checks to all 5 hooks in hooks.json
- Check runs before each hook: [ ! -d "${CLAUDE_PLUGIN_ROOT}/scripts/node_modules" ] && cd "${CLAUDE_PLUGIN_ROOT}/scripts" && npm install || true
- Reverted all hook TypeScript files to use simple static imports (removed dynamic imports)
- Removed src/shared/bootstrap.ts (44 lines)
- Removed ensureDependencies() calls from all hook entry points

Benefits:
- Simpler architecture using native bash instead of TypeScript
- Net reduction of 157 lines of code
- No runtime overhead when dependencies already installed
- Uses plugin manifest's command hook feature as intended
- Faster and more efficient

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-20 17:01:24 -04:00
Alex Newman 9dd8b180ac fix: Use dynamic imports in hooks to fix better-sqlite3 loading
Changed all hook entry points to use dynamic imports after bootstrap runs. This ensures that better-sqlite3 is installed before Node.js attempts to resolve the import.

Changes:
- Modified src/bin/hooks/*.ts to call ensureDependencies() before dynamic import
- Moved from static `import { hook } from '...'` to `const { hook } = await import('...')`
- This delays module resolution until after npm install completes
- Bumped version to 4.0.6

The previous approach failed because static imports are resolved at module link time, before any runtime code (including ensureDependencies) executes.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-20 16:54:28 -04:00
Alex Newman 6fb9570291 feat: Release v4.0.5 - Auto-install dependencies via bootstrap hooks
Fixed better-sqlite3 distribution by implementing self-bootstrapping hooks
that auto-install dependencies on first run. This eliminates the need for
users to have native compilation tools or manually install dependencies.

## Solution

Instead of bundling 25MB of better-sqlite3 binaries in git or requiring
manual npm install, hooks now bootstrap themselves on first execution:

1. Created `src/shared/bootstrap.ts` with `ensureDependencies()` function
2. Added bootstrap calls to all hook entry points (context, new, save, summary, cleanup)
3. Created `plugin/scripts/package.json` declaring better-sqlite3 dependency
4. Bootstrap checks if `node_modules` exists, runs `npm install` if missing
5. npm automatically downloads prebuilt better-sqlite3 binary for user's platform

## Changes

**Core Bootstrap System:**
- Added src/shared/bootstrap.ts: Auto-install dependencies using npm
- Modified all hooks (context, new, save, summary, cleanup) to call ensureDependencies()
- Created plugin/scripts/package.json with better-sqlite3 dependency

**Build & Distribution:**
- Removed node_modules copying logic from build script
- Build output is now compact (hooks + package.json, no binaries)
- Updated marketplace.json to point to GitHub for direct installation

**Documentation:**
- Updated README: GitHub Marketplace installation is now recommended method
- Installation instructions emphasize no compilation needed
- Version bumped to 4.0.5 throughout

## Benefits

-  No git bloat (repo stays small, no 25MB binaries committed)
-  No compilation needed (npm downloads prebuilt binaries)
-  Works on all platforms (npm handles platform-specific binaries)
-  Zero manual steps (hooks bootstrap themselves automatically)
-  Idempotent (skips install if dependencies already exist)

Installation now works via simple:
```
/plugin marketplace add https://github.com/thedotmack/claude-mem
/plugin install claude-mem
```

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-20 16:44:26 -04:00
Alex Newman f849a69506 fix: Move ensureWorkerRunning to start of hooks to prevent race condition
All hooks now call ensureWorkerRunning() BEFORE querying the database. This
ensures the worker's orphaned session cleanup runs before hooks check for
active sessions, preventing 404 errors when hooks try to use sessions that
don't exist in worker memory after a restart.

Hook order now:
1. ensureWorkerRunning() - starts worker, runs cleanup
2. Query DB - cleanup already marked orphaned sessions as failed
3. Use session - only valid sessions are processed

Fixed in:
- new-hook: Line 26, before DB queries
- save-hook: Line 37, before DB queries
- summary-hook: Line 24, before DB queries
- cleanup-hook: Line 50, before DB queries

This prevents the race condition where hooks would read session status before
cleanup ran, then get 404 from worker after cleanup marked sessions failed.
2025-10-19 02:01:11 -04:00
Alex Newman daf368e343 Refactor code structure for improved readability and maintainability 2025-10-19 01:45:51 -04:00
Alex Newman be0ab12789 Refactor worker startup logic to use PM2 with improved error handling
- Updated `new-hook.js` to streamline logging and session management.
- Enhanced database schema initialization and migration processes.
- Improved error handling for worker startup in `worker-utils.ts`, ensuring PM2 binary and ecosystem config existence before spawning.
- Added detailed error messages for missing dependencies and spawn failures.
2025-10-19 01:13:20 -04:00
Alex Newman 7ff611feb5 Refactor hooks and worker service for improved error handling and initialization
- Removed try-catch blocks in new-hook, save-hook, and summary-hook for cleaner flow.
- Enhanced error handling in save and summary hooks to throw errors instead of logging and returning.
- Introduced ensureWorkerRunning utility to manage worker service lifecycle and health checks.
- Replaced dynamic port allocation with a fixed port for the worker service.
- Simplified path management and removed unused port allocator utility.
- Added database schema initialization for fresh installations and improved migration handling.
2025-10-19 00:57:49 -04:00
Alex Newman 002f7a94b8 feat: Release v4.0.0 - Plugin data directory and auto-starting worker
BREAKING CHANGES:
- Data directory moved from ~/.claude-mem/ to ${CLAUDE_PLUGIN_ROOT}/data/
- Fresh start required - no migration from v3.x databases
- Worker service now auto-starts on SessionStart hook

New Features:
- MCP Search Server with 6 specialized search tools
- FTS5 full-text search across observations and sessions
- Auto-starting worker service in SessionStart hook
- Citation support for search results (claude-mem:// URIs)

Changes:
- Updated paths.ts to use CLAUDE_PLUGIN_ROOT for data directory
- Added worker auto-start logic to context hook
- Updated worker service to write port file to plugin data dir
- Bumped version to 4.0.0 in package.json and plugin.json
- Created comprehensive CHANGELOG.md documenting v4.0.0 changes
- Updated README.md with v4.0.0 breaking changes and features
- Rebuilt all hooks and worker service

Technical Improvements:
- Improved error handling and graceful degradation
- Structured logging across all components
- Enhanced plugin integration with Claude Code

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-19 00:05:56 -04:00
Alex Newman bc41e367c0 fix: Update date formatting in context hook to use localized string representation 2025-10-18 21:14:42 -04:00
Alex Newman 5dd663730b fix: Refactor context hook to use proper SessionStart hookSpecificOutput format
The context hook was not appearing in Claude Code sessions because it was
outputting plain text to stdout instead of using the required JSON structure
for SessionStart hooks.

Changes:
- src/hooks/context.ts: Changed contextHook to return string instead of void,
  removing direct console.log calls to make it more reusable
- src/bin/hooks/context-hook.ts: Wrap contextHook output in hookSpecificOutput
  JSON structure with hookEventName "SessionStart" and additionalContext field
- Both TTY and stdin code paths now properly format and exit with code 0

Fixes the issue where recent session context was not being injected at session
start. Tested with npm run test:context - hook now properly outputs JSON with
recent sessions formatted as markdown.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-18 21:03:57 -04:00
Alex Newman 56167c47a2 feat: Implement Claude-mem MCP Search Server with session and observation search capabilities
- Added search functionality for observations and sessions using full-text search.
- Implemented formatting functions for search results with citations.
- Created multiple tools for searching by various criteria including concept, file, type, and advanced search.
- Integrated structured filters and pagination options for search queries.
- Established error handling for search operations and server initialization.
2025-10-18 20:45:41 -04:00
Alex Newman 115270c35e feat: Implement session management and search functionality
- Added SDK session, observation, and summary types to types.ts.
- Refactored worker-service to use SessionStore for session management.
- Created SessionSearch class for FTS5 full-text search and structured queries.
- Implemented SessionStore for CRUD operations on SDK sessions, observations, and summaries.
- Added migrations for database schema updates, including new columns and constraints.
- Enhanced search capabilities with filters for projects, types, concepts, and date ranges.
2025-10-18 20:15:55 -04:00
Alex Newman c27682c799 Refactor summary and session retrieval logic in HooksDatabase
- Updated SQL queries in summary-hook.js and HooksDatabase.ts to improve performance and clarity.
- Introduced a subquery for fetching recent sessions with status, ensuring correct ordering and grouping.
- Enhanced logging functionality for better debugging and error tracking.
- Added comprehensive documentation in README.md to outline the new features and usage instructions.
2025-10-18 19:44:14 -04:00
Alex Newman 1fbba55aa3 Refactor summary and context handling in hooks
- Updated `summary-hook.js` to improve logging and session handling.
- Modified `context.ts` to fetch recent sessions with status and summary info, enhancing output formatting.
- Added new methods in `HooksDatabase.ts` for retrieving recent sessions and their summaries.
- Improved observation retrieval logic in `context.ts` to display relevant information for active sessions.
- Enhanced prompt documentation in `prompts.ts` to clarify output expectations.
- Refactored logger methods in `logger.ts` to instance methods for better encapsulation.
2025-10-18 19:29:45 -04:00
Alex Newman 05f3889deb feat(logging): Implement structured logging across the application
- Introduced a new Logger utility to standardize logging with correlation IDs and structured context.
- Replaced console.error and console.log statements with logger methods in various modules including save.ts, summary.ts, parser.ts, HooksDatabase.ts, and worker-service.ts.
- Enhanced error handling and logging for better traceability of observations and summaries.
- Made observations.text nullable in the database schema to support structured fields.
- Added correlation IDs for tracking observations through the processing pipeline.
2025-10-18 19:15:52 -04:00
Alex Newman 874815770a fix: Add missing process.exit(0) calls in hook entry points
All 4 hook entry point scripts were missing process.exit(0) after successful
execution, causing Node processes to hang indefinitely instead of returning
control to Claude Code with exit code 0.

Root cause: In commit 6f62a56, process.exit(0) calls were removed from the
hook functions but were never added to the entry point scripts that wrap them.

Fixed files:
- src/bin/hooks/save-hook.ts (PostToolUse)
- src/bin/hooks/new-hook.ts (UserPromptSubmit)
- src/bin/hooks/summary-hook.ts (Stop)
- src/bin/hooks/context-hook.ts (SessionStart)

This restores proper hook exit behavior and prevents Claude Code from waiting
indefinitely for hook completion.
2025-10-18 18:49:11 -04:00
Alex Newman 81101ef1a6 feat: Enhance observation and summary structures in hooks
- Updated observation schema to include hierarchical fields: title, subtitle, facts, narrative, concepts, files_read, and files_modified.
- Modified the save-hook and summary-hook scripts to accommodate the new observation structure.
- Added migration logic to the HooksDatabase for adding new fields to the observations table.
- Refactored the parser to extract new fields from XML formatted observations.
- Adjusted prompt generation to reflect the new observation format and requirements.
- Updated worker service to handle new observation and summary structures.
2025-10-18 17:34:24 -04:00
Alex Newman 015b38c763 Refactor database hooks to add new session tracking features
- Introduced `getSessionById` method in HooksDatabase to retrieve session details by ID.
- Updated context, new, save, and summary hooks to utilize the new `getSessionById` method.
- Enhanced session management by adding `worker_port` and `prompt_counter` columns to relevant tables.
- Improved logging in WorkerService to provide clearer output during session initialization and processing.
- Removed redundant error logging in favor of more informative console logs.
2025-10-17 16:42:23 -04:00
Alex Newman be936d8413 Enhance HooksDatabase and WorkerService functionality
- Updated save-hook.js and summary-hook.js to include new database columns for tracking worker ports and prompt numbers.
- Implemented migration logic to remove UNIQUE constraints from session_summaries table and added necessary indices.
- Modified HooksDatabase methods to return boolean values indicating success or failure of updates.
- Changed logging from error to info level in WorkerService for better clarity on session management and initialization.
2025-10-17 16:32:20 -04:00
Alex Newman d4a71c994d feat: Enhance session management with prompt tracking
- Added prompt_number to observations and session summaries for better tracking.
- Implemented prompt counter in SDK sessions to manage user prompts effectively.
- Updated database schema to include prompt tracking columns and removed unique constraints on session summaries.
- Modified hooks to utilize prompt_number in observations and summaries.
- Changed worker service to handle summarize requests instead of finalize, keeping the SDK agent active.
- Improved logging for better debugging and tracking of prompt numbers across sessions.
2025-10-17 16:23:11 -04:00
Alex Newman 372854948c feat: Implement Worker Service with session management and SDK integration
- Added WorkerService to handle long-running HTTP service with session management.
- Implemented endpoints for initializing, observing, finalizing, checking status, and deleting sessions.
- Integrated with Claude SDK for processing observations and generating responses.
- Added port allocator utility to dynamically find available ports for the service.
- Configured TypeScript settings for the project.
2025-10-17 15:59:36 -04:00