Skip to content

[Bug] v8.5.2: Excessive observer sessions created during startup-recovery - 13,000+ orphaned files #514

@Suibianlahaha

Description

@Suibianlahaha

Bug Description

After upgrading to claude-mem v8.5.2, the plugin created 13,000+ orphaned observer session files over 2 days (Dec 31 - Jan 2). Each file contains only the initialization message "Hello memory agent, you are continuing to observe..." with no actual observations.

This is likely a severe side effect of the startup-recovery mechanism reported in #510.

Impact

  • Token consumption: Each observer session calls the LLM API even though no actual work is being observed
  • Financial cost: Thousands of unnecessary API calls
  • Storage waste: 13,000+ .jsonl files created
  • UX degradation: claude -r command shows thousands of useless sessions
  • Database integrity: Database shows normal session counts (~30/day) but filesystem has 13,000+ files

Statistics

File System Analysis (.jsonl session files)

Date Files Created
Dec 30, 2025 ~30 (normal)
Dec 31, 2025 11,555 ⚠️
Jan 1-2, 2026 1,770 ⚠️
Total (abnormal) ~13,325 files

Database Analysis (sdk_sessions table)

Date Sessions in DB Status
Dec 30, 2025 11 All completed
Dec 31, 2025 13 All completed
Jan 1, 2026 30 All completed

Key Finding: Database shows normal activity (~13-30 sessions/day), but filesystem has 13,000+ files. This indicates observer sessions are being created but not properly tracked in the database.

Session File Content

Each orphaned session file contains only:

{
  "type": "queue-operation",
  "operation": "dequeue",
  "timestamp": "2026-01-01T16:30:42.480Z",
  "sessionId": "[session-uuid]"
}

{
  "parentUuid": null,
  "type": "user",
  "message": {
    "role": "user",
    "content": "Hello memory agent, you are continuing to observe the primary Claude session.\n\n<observed_from_primary_session>..."
  },
  "sessionId": "[session-uuid]"
}

No actual tool executions or observations are recorded.

Environment

  • claude-mem version: 8.5.2
  • Claude Code version: 2.0.76
  • OS: macOS (Darwin 25.2.0)
  • Provider: Custom API endpoint (via ANTHROPIC_BASE_URL)
  • Worker service: Running on port 37777
  • Start date of issue: December 31, 2025

Root Cause Hypothesis

Based on Issue #510, the startup-recovery mechanism in worker-service.ts:748 always uses sdkAgent regardless of CLAUDE_MEM_PROVIDER setting:

session.generatorPromise = this.sdkAgent.startSession(session, this)

Hypothesis: When worker restarts:

  1. startSessionProcessor() is called for each pending session
  2. Each call creates a NEW observer session file
  3. The observer session initializes with "Hello memory agent..." message
  4. But if there's no actual activity to observe, the session remains empty
  5. This creates a loop where every recovery attempt spawns more empty observer sessions

Logs Evidence

From claude-mem-2025-12-31.log:

[INFO ] [SYSTEM] Auto-recovered 4 sessions with pending work {totalPending=4, started=4, sessionIds=2833,2910,2916,2924}

Multiple recovery attempts may have created multiple observer sessions for the same pending work.

Steps to Reproduce

  1. Install claude-mem v8.5.2
  2. Use Claude Code normally for a few sessions
  3. Restart the worker service (either manually or due to version mismatch)
  4. Observe the creation of numerous .jsonl files in ~/.claude/projects/-[username]/
  5. Check database: sqlite3 ~/.claude-mem/claude-mem.db "SELECT COUNT(*) FROM sdk_sessions WHERE status='active'"
  6. Run claude -r to see hundreds/thousands of "memory agent" sessions

Expected Behavior

  • Startup-recovery should:
    • Resume existing sessions, not create new ones
    • Respect the CLAUDE_MEM_PROVIDER setting
    • Clean up failed recovery attempts
  • Orphaned session files should not be created
  • Database and filesystem should be in sync

Actual Behavior

  • Startup-recovery creates thousands of observer session files
  • Files exist in filesystem but not properly tracked in database
  • Each observer session makes an LLM API call with the initialization prompt
  • Sessions appear in claude -r list, cluttering the UI

Related Issues

Temporary Workaround

Disabled auto-observation in ~/.claude-mem/settings.json:

{
  "CLAUDE_MEM_CONTEXT_OBSERVATIONS": "0",
  "CLAUDE_MEM_CONTEXT_FULL_COUNT": "0",
  "CLAUDE_MEM_CONTEXT_SESSION_COUNT": "0",
  "CLAUDE_MEM_DISABLED": "true"
}

Then killed all worker processes:

pkill -f "claude-mem.*worker-service"
pkill -f "claude-mem.*mcp-server"

Cleanup Commands

To remove orphaned session files (backup first!):

# Backup
mkdir -p ~/.claude/backup/$(date +%Y%m%d)
find ~/.claude/projects/-[username] -maxdepth 1 -name "*.jsonl" -newermt "2025-12-31" -exec mv {} ~/.claude/backup/$(date +%Y%m%d)/ \;

# Mark database sessions as completed
sqlite3 ~/.claude-mem/claude-mem.db "UPDATE sdk_sessions SET status='completed', completed_at=datetime('now') WHERE status='active';"

Additional Notes

  • Issue started after plugin update on Dec 31, 2025
  • Previous version (before v8.5.2) did not have this problem
  • The problem persists even after stopping/starting the worker
  • Token consumption is significant: each observer session makes at least one API call

Severity

HIGH - This bug:

  • Wastes API tokens/money
  • Degrades user experience significantly
  • Creates storage waste
  • May affect all users who upgraded to v8.5.2
  • Makes the claude -r command unusable

Request

Please investigate the startup-recovery mechanism and fix the orphaned session creation bug. A hotfix would be greatly appreciated given the financial impact.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions