Skip to content

fix(hooks): V3 MCP hook handlers now persist data to database#1059

Open
ahmedibrahim085 wants to merge 1 commit intoruvnet:mainfrom
ahmedibrahim085:fix/hooks-persistence-stubs
Open

fix(hooks): V3 MCP hook handlers now persist data to database#1059
ahmedibrahim085 wants to merge 1 commit intoruvnet:mainfrom
ahmedibrahim085:fix/hooks-persistence-stubs

Conversation

@ahmedibrahim085
Copy link

Summary

V3 MCP hook handlers (hooksPostEdit, hooksPostTask, hooksPostCommand) were returning success responses but not actually persisting any data. This PR fixes them to call getRealStoreFunction() and store to the database with HNSW indexing.

Problem Evidence

Before (Stub Handlers)

hooksPostEdit (lines 621-645):

return {
  recorded: true,  // ← LIES - nothing was recorded
  ...
};

hooksPostTask (lines 1049-1080):

return {
  duration: Math.floor(Math.random() * 300) + 60, // ← FAKE random data
  learningUpdates: {
    patternsUpdated: success ? 2 : 1,             // ← FAKE - nothing updated
  },
};

Statusline (hooks.ts:3314-3318):

patterns = Math.floor(sizeKB / 2);  // ← FAKE: just file_size / 2KB!

Database Reality:

sqlite3 .swarm/memory.db "SELECT COUNT(*) FROM patterns"
-- Result: 0 (despite statusline showing 1888)

Solution

Updated handlers to call getRealStoreFunction() (which already exists at line 50-60):

Handler Before After
hooksPostEdit Returned {recorded: true} Stores to patterns namespace with HNSW
hooksPostTask Returned fake random data Stores to trajectories namespace with HNSW
hooksPostCommand Returned {recorded: true} Stores to commands namespace with HNSW
getLearningStats file_size / 2KB Real SELECT COUNT(*) queries

Test Results

✅ post-edit: PERSISTED - "Pattern stored with HNSW indexing"
✅ post-task: PERSISTED - "Trajectory stored with HNSW indexing"  
✅ post-command: PERSISTED - "Command pattern stored with HNSW indexing"

Database verification after fix:

sqlite3 .swarm/memory.db "SELECT namespace, COUNT(*) FROM memory_entries GROUP BY namespace"
-- patterns|1
-- trajectories|1
-- commands|1

Files Changed

File Changes
v3/@claude-flow/cli/src/mcp-tools/hooks-tools.ts +131 lines - Fixed 3 handlers
v3/@claude-flow/cli/src/commands/hooks.ts +42 lines - Fixed statusline

Impact

  • Pattern learning works: Edit patterns now persist for cross-session learning
  • Trajectory tracking works: Task outcomes stored for routing optimization
  • Command history tracked: Commands and success/failure persisted
  • Honest metrics: Statusline shows real data instead of fake calculations

Related


🤖 Generated with Claude Code

BREAKING FIX: hooksPostEdit, hooksPostTask, and hooksPostCommand were
returning success without actually storing any data. This fix makes
them call getRealStoreFunction() to persist to the memory database.

Changes:
- hooksPostEdit: Now stores to 'patterns' namespace with HNSW indexing
- hooksPostTask: Now stores to 'trajectories' namespace with HNSW indexing
- hooksPostCommand: Now stores to 'commands' namespace with HNSW indexing
- getLearningStats: Now queries real SELECT COUNT(*) instead of file_size/2KB

Before:
- handlers returned {recorded: true} without database INSERT
- hooksPostTask returned Math.random() for duration (fake data)
- statusline showed file_size/2KB as "patterns" count (misleading)

After:
- handlers call storeEntry() with generateEmbeddingFlag: true
- real pattern IDs returned from database
- statusline queries actual table row counts

Verified:
- ✅ post-edit: PERSISTED - "Pattern stored with HNSW indexing"
- ✅ post-task: PERSISTED - "Trajectory stored with HNSW indexing"
- ✅ post-command: PERSISTED - "Command pattern stored with HNSW indexing"

Closes ruvnet#1058
@ahmedibrahim085
Copy link
Author

Independent Verification Results

I independently discovered the same issues and can confirm this PR fixes them. Here are my findings:

Problem Verified

CLI trajectory commands fail with MCP error:

[ERROR] Intelligence error: Failed to execute MCP tool 'hooks_intelligence': Cannot convert undefined or null to object

But MCP tools work when called directly from Claude Code:

{
  "trajectoryId": "traj-xxx",
  "status": "recording",
  "implementation": "real-trajectory-tracking"
}

Current Workaround (Partial)

The memory store CLI command works, so hooks using cf_memory_store_gate successfully store data:

Namespace Entries Searchable
patterns 5 ✅ Score 0.41
trajectories 3 ✅ Score 0.46
commands 1 ✅ Score 0.33

However, SONA learning and EWC++ consolidation don't trigger because hooksPostEdit, hooksPostTask, hooksPostCommand return fake success without calling getRealStoreFunction().

Tested Components

Component CLI (npx) MCP (direct)
hooks intelligence trajectory-start ❌ Fails ✅ Works
hooks intelligence trajectory-step ❌ Fails ✅ Works
hooks intelligence trajectory-end ❌ Fails ✅ Works
hooks intelligence --showStatus ❌ Fails N/A
memory store ✅ Works ✅ Works
memory search ✅ Works ✅ Works

Request

This fix would enable proper learning for users running hooks via bash scripts. The getRealStoreFunction() already exists at line 23 and works - just needs to be called in the stub handlers.

Would appreciate expedited review as this affects all hook-based learning workflows.


Tested on: claude-flow v3.0.0-alpha.190, Node.js v22, macOS

blackms added a commit to blackms/claude-flow that referenced this pull request Feb 6, 2026
…istence

fix(hooks): V3 MCP hook handlers now persist data (upstream ruvnet#1059)
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.

fix(hooks): V3 MCP hook handlers are stubs that don't persist data

1 participant