Compare commits

..

11 Commits

Author SHA1 Message Date
Alex Newman bd619229b2 chore: bump version to 9.0.10
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-26 15:51:43 -05:00
Alexander Knigge 182097ef1c fix: resolve path format mismatch in folder CLAUDE.md generation (#794) (#813)
The isDirectChild() function failed to match files when the API used
absolute paths (/Users/x/project/app/api) but the database stored
relative paths (app/api/router.py). This caused all folder CLAUDE.md
files to incorrectly show "No recent activity".

Changes:
- Create shared path-utils module with proper path normalization
- Implement suffix matching strategy for mixed path formats
- Update SessionSearch.ts to use shared utilities
- Update regenerate-claude-md.ts to use shared utilities (was using
  outdated broken logic)
- Prevent spurious directory creation from malformed paths
- Add comprehensive test coverage for path matching edge cases

This is the proper fix for #794, replacing PR #809 which only masked
the bug by skipping file creation when "no activity" was shown.

Co-authored-by: bigphoot <bigphoot@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-26 15:48:31 -05:00
Alex Newman 0b7ecedcd7 docs: update CHANGELOG.md for v9.0.9 2026-01-25 23:59:58 -05:00
Alex Newman da01e4bba0 chore: bump version to 9.0.9 2026-01-25 23:59:26 -05:00
Max Millien 7c3bfadd5e fix: Prevent creation of new CLAUDE.md files if no activity is present. (#809) 2026-01-25 23:57:55 -05:00
Alex Newman a8bb625513 docs: update CHANGELOG.md for v9.0.8 2026-01-25 20:13:04 -05:00
Alex Newman bab8f554bd chore: bump version to 9.0.8
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 20:12:26 -05:00
Alexander Knigge c1b5b2a783 fix: prevent zombie process accumulation via PID registry and signal propagation (Issue #737) (#806)
* Fix zombie process accumulation (Issue #737)

Problem: Claude haiku subprocesses spawned by the SDK weren't terminating
properly, causing zombie process accumulation (user reported 155 processes
consuming 51GB RAM).

Root causes:
1. SDK's SpawnedProcess interface hides subprocess PIDs
2. deleteSession() didn't verify subprocess exit
3. abort() was fire-and-forget with no confirmation
4. No mechanism to track or clean up orphaned processes

Solution:
- Add ProcessRegistry module to track spawned Claude subprocesses
- Use SDK's spawnClaudeCodeProcess option to capture PIDs via custom spawn
- Pass signal parameter to enable AbortController integration
- Wait for subprocess exit in deleteSession() with 5s timeout
- Escalate to SIGKILL if graceful exit fails
- Add orphan reaper running every 5 minutes as safety net

Files changed:
- src/services/worker/ProcessRegistry.ts (new): PID registry and reaper
- src/services/worker/SDKAgent.ts: Use custom spawn to capture PIDs
- src/services/worker/SessionManager.ts: Verify subprocess exit on delete
- src/services/worker-service.ts: Start/stop orphan reaper

Fixes #737

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

* fix: address code review feedback

- Replace busy-wait polling with event-based proc.once('exit')
- Detect and warn about multiple processes per session (race condition)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

---------

Co-authored-by: bigphoot <bigphoot@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 20:10:11 -05:00
Alex Newman 67651669a1 9.0.7 2026-01-25 12:47:28 -05:00
Alex Newman ae454cfc01 feat: add SDK exports for consumer app integration
- Create standalone dist/sdk/ module with parseObservations, buildObservationPrompt, parseSummary
- Add package.json exports for 'claude-mem/sdk' and 'claude-mem/modes/*'
- Add TypeScript declarations

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 12:47:24 -05:00
Alex Newman fa218b0d71 docs: update CHANGELOG.md for v9.0.6 2026-01-22 18:49:00 -05:00
19 changed files with 787 additions and 243 deletions
+1 -1
View File
@@ -10,7 +10,7 @@
"plugins": [
{
"name": "claude-mem",
"version": "9.0.6",
"version": "9.0.10",
"source": "./plugin",
"description": "Persistent memory system for Claude Code - context compression across sessions"
}
+67 -48
View File
@@ -2,6 +2,73 @@
All notable changes to claude-mem.
## [v9.0.9] - 2026-01-26
## Bug Fixes
### Prevent Creation of Empty CLAUDE.md Files (#809)
Previously, claude-mem would create new `CLAUDE.md` files in project directories even when there was no activity to display, cluttering codebases with empty context files showing only "*No recent activity*".
**What changed:** The `updateFolderClaudeMdFiles` function now checks if the formatted content contains no activity before writing. If a `CLAUDE.md` file doesn't already exist and there's nothing to show, it will be skipped entirely. Existing files will still be updated to reflect "No recent activity" if that's the current state.
**Impact:** Cleaner project directories - only folders with actual activity will have `CLAUDE.md` context files created.
Thanks to @maxmillienjr for this contribution!
## [v9.0.8] - 2026-01-26
## Fix: Prevent Zombie Process Accumulation (Issue #737)
This release fixes a critical issue where Claude haiku subprocesses spawned by the SDK weren't terminating properly, causing zombie process accumulation. One user reported 155 processes consuming 51GB RAM.
### Root Causes Addressed
- SDK's SpawnedProcess interface hides subprocess PIDs
- `deleteSession()` didn't verify subprocess exit
- `abort()` was fire-and-forget with no confirmation
- No mechanism to track or clean up orphaned processes
### Solution
- **ProcessRegistry module**: Tracks spawned Claude subprocesses via PID
- **Custom spawn**: Uses SDK's `spawnClaudeCodeProcess` option to capture PIDs
- **Signal propagation**: Passes signal parameter to enable AbortController integration
- **Graceful shutdown**: Waits for subprocess exit in `deleteSession()` with 5s timeout
- **SIGKILL escalation**: Force-kills processes that don't exit gracefully
- **Orphan reaper**: Safety net running every 5 minutes to clean up any missed processes
- **Race detection**: Warns about multiple processes per session (race condition indicator)
### Files Changed
- `src/services/worker/ProcessRegistry.ts` (new): PID registry and reaper
- `src/services/worker/SDKAgent.ts`: Use custom spawn to capture PIDs
- `src/services/worker/SessionManager.ts`: Verify subprocess exit on delete
- `src/services/worker-service.ts`: Start/stop orphan reaper
**Full Changelog**: https://github.com/thedotmack/claude-mem/compare/v9.0.7...v9.0.8
Fixes #737
## [v9.0.6] - 2026-01-22
## Windows Console Popup Fix
This release eliminates the annoying console window popups that Windows users experienced when claude-mem spawned background processes.
### Fixed
- **Windows console popups eliminated** - Daemon spawn and Chroma operations no longer create visible console windows (#748, #708, #681, #676)
- **Race condition in PID file writing** - Worker now writes its own PID file after listen() succeeds, ensuring reliable process tracking on all platforms
### Changed
- **Chroma temporarily disabled on Windows** - Vector search is disabled on Windows while we migrate to a popup-free architecture. Keyword search and all other memory features continue to work. A follow-up release will re-enable Chroma.
- **Slash command discoverability** - Added YAML frontmatter to `/do` and `/make-plan` commands
### Technical Details
- Uses WMIC for detached process spawning on Windows
- PID file location unchanged, but now written by worker process
- Cross-platform: Linux/macOS behavior unchanged
### Contributors
- @bigph00t (Alexander Knigge)
## [v9.0.5] - 2026-01-14
## Major Worker Service Cleanup
@@ -1239,51 +1306,3 @@ This represents a major reliability improvement for Windows users, eliminating c
**Full Changelog**: https://github.com/thedotmack/claude-mem/compare/v7.3.4...v7.3.5
## [v7.3.4] - 2025-12-17
Patch release for bug fixes and minor improvements
## [v7.3.3] - 2025-12-16
## What's Changed
- Remove all better-sqlite3 references from codebase (#357)
**Full Changelog**: https://github.com/thedotmack/claude-mem/compare/v7.3.2...v7.3.3
## [v7.3.2] - 2025-12-16
## 🪟 Windows Console Fix
Fixes blank console windows appearing for Windows 11 users during claude-mem operations.
### What Changed
- **Windows**: Uses PowerShell `Start-Process -WindowStyle Hidden` to properly hide worker process
- **Security**: Added PowerShell string escaping to follow security best practices
- **Unix/Mac**: No changes (continues to work as before)
### Root Cause
The issue was caused by a Node.js limitation where `windowsHide: true` doesn't work with `detached: true` in `child_process.spawn()`. This affects both Bun and Node.js since Bun inherits Node.js process spawning semantics.
See: https://github.com/nodejs/node/issues/21825
### Security Note
While all paths in the PowerShell command are application-controlled (not user input), we've added proper escaping to follow security best practices. If an attacker could modify bun installation paths or plugin directories, they would already have full filesystem access including the database.
### Related
- Fixes #304 (Multiple visible console windows)
- Merged PR #339
- Testing documented in PR #315
### Breaking Changes
None - fully backward compatible.
---
**Full Changelog**: https://github.com/thedotmack/claude-mem/compare/v7.3.1...v7.3.2
+16 -1
View File
@@ -1,6 +1,6 @@
{
"name": "claude-mem",
"version": "9.0.6",
"version": "9.0.10",
"description": "Memory compression system for Claude Code - persist context across sessions",
"keywords": [
"claude",
@@ -26,6 +26,21 @@
"url": "https://github.com/thedotmack/claude-mem/issues"
},
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
},
"./sdk": {
"types": "./dist/sdk/index.d.ts",
"import": "./dist/sdk/index.js"
},
"./modes/*": "./plugin/modes/*"
},
"files": [
"dist",
"plugin"
],
"engines": {
"node": ">=18.0.0",
"bun": ">=1.0.0"
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "claude-mem",
"version": "9.0.6",
"version": "9.0.10",
"description": "Persistent memory system for Claude Code - seamlessly preserve context across sessions",
"author": {
"name": "Alex Newman"
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "claude-mem-plugin",
"version": "9.0.6",
"version": "9.0.10",
"private": true,
"description": "Runtime dependencies for claude-mem bundled hooks",
"type": "module",
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+13 -34
View File
@@ -43,8 +43,10 @@ interface ObservationRow {
discovery_tokens: number | null;
}
// Import shared formatting utilities
// Import shared utilities
import { formatTime, groupByDate } from '../src/shared/timeline-formatting.js';
import { isDirectChild } from '../src/shared/path-utils.js';
import { replaceTaggedContent } from '../src/utils/claude-md-utils.js';
// Type icon map (matches ModeManager)
const TYPE_ICONS: Record<string, string> = {
@@ -135,19 +137,6 @@ function walkDirectoriesWithIgnore(dir: string, folders: Set<string>, depth: num
}
}
/**
* Check if a file is a direct child of a folder (not in a subfolder)
* @param filePath - File path like "src/services/foo.ts"
* @param folderPath - Folder path like "src/services"
* @returns true if file is directly in folder, false if in a subfolder
*/
function isDirectChild(filePath: string, folderPath: string): boolean {
if (!filePath.startsWith(folderPath + '/')) return false;
const remainder = filePath.slice(folderPath.length + 1);
// If remainder contains a slash, it's in a subfolder
return !remainder.includes('/');
}
/**
* Check if an observation has any files that are direct children of the folder
*/
@@ -288,37 +277,27 @@ function formatObservationsForClaudeMd(observations: ObservationRow[], folderPat
/**
* Write CLAUDE.md file with tagged content preservation
* Note: For the CLI regenerate tool, we DO create directories since the user
* explicitly requested regeneration. This differs from the runtime behavior
* which only writes to existing folders.
*/
function writeClaudeMdToFolder(folderPath: string, newContent: string): void {
function writeClaudeMdToFolderForRegenerate(folderPath: string, newContent: string): void {
const claudeMdPath = path.join(folderPath, 'CLAUDE.md');
const tempFile = `${claudeMdPath}.tmp`;
// For regenerate CLI, we create the folder if needed
mkdirSync(folderPath, { recursive: true });
// Read existing content if file exists
let existingContent = '';
if (existsSync(claudeMdPath)) {
existingContent = readFileSync(claudeMdPath, 'utf-8');
}
const startTag = '<claude-mem-context>';
const endTag = '</claude-mem-context>';
let finalContent: string;
if (!existingContent) {
finalContent = `${startTag}\n${newContent}\n${endTag}`;
} else {
const startIdx = existingContent.indexOf(startTag);
const endIdx = existingContent.indexOf(endTag);
if (startIdx !== -1 && endIdx !== -1) {
finalContent = existingContent.substring(0, startIdx) +
`${startTag}\n${newContent}\n${endTag}` +
existingContent.substring(endIdx + endTag.length);
} else {
finalContent = existingContent + `\n\n${startTag}\n${newContent}\n${endTag}`;
}
}
// Use shared utility to preserve user content outside tags
const finalContent = replaceTaggedContent(existingContent, newContent);
// Atomic write: temp file + rename
writeFileSync(tempFile, finalContent);
renameSync(tempFile, claudeMdPath);
}
@@ -450,7 +429,7 @@ function regenerateFolder(
// Format using relative path for display, write to absolute path
const formatted = formatObservationsForClaudeMd(observations, relativeFolder);
writeClaudeMdToFolder(absoluteFolder, formatted);
writeClaudeMdToFolderForRegenerate(absoluteFolder, formatted);
return { success: true, observationCount: observations.length };
} catch (error) {
+2
View File
@@ -0,0 +1,2 @@
export * from './parser.js';
export * from './prompts.js';
+6 -1
View File
@@ -3,5 +3,10 @@
<!-- This section is auto-generated by claude-mem. Edit content outside the tags. -->
*No recent activity*
### Jan 25, 2026
| ID | Time | T | Title | Read |
|----|------|---|-------|------|
| #41877 | 12:09 PM | ⚖️ | Deploy Existing Consumer Preview Without Creating New Packages | ~361 |
| #41873 | 12:03 PM | 🔵 | Claude-mem mode configuration system types documented | ~504 |
</claude-mem-context>
+3 -11
View File
@@ -2,6 +2,7 @@ import { Database } from 'bun:sqlite';
import { TableNameRow } from '../../types/database.js';
import { DATA_DIR, DB_PATH, ensureDir } from '../../shared/paths.js';
import { logger } from '../../utils/logger.js';
import { isDirectChild } from '../../shared/path-utils.js';
import {
ObservationSearchResult,
SessionSummarySearchResult,
@@ -336,15 +337,6 @@ export class SessionSearch {
return this.db.prepare(sql).all(...params) as ObservationSearchResult[];
}
/**
* Check if a file is a direct child of a folder (not in a subfolder)
*/
private isDirectChild(filePath: string, folderPath: string): boolean {
if (!filePath.startsWith(folderPath + '/')) return false;
const remainder = filePath.slice(folderPath.length + 1);
return !remainder.includes('/');
}
/**
* Check if an observation has any files that are direct children of the folder
*/
@@ -354,7 +346,7 @@ export class SessionSearch {
try {
const files = JSON.parse(filesJson);
if (Array.isArray(files)) {
return files.some(f => this.isDirectChild(f, folderPath));
return files.some(f => isDirectChild(f, folderPath));
}
} catch {}
return false;
@@ -372,7 +364,7 @@ export class SessionSearch {
try {
const files = JSON.parse(filesJson);
if (Array.isArray(files)) {
return files.some(f => this.isDirectChild(f, folderPath));
return files.some(f => isDirectChild(f, folderPath));
}
} catch {}
return false;
+22
View File
@@ -69,6 +69,9 @@ import { SearchRoutes } from './worker/http/routes/SearchRoutes.js';
import { SettingsRoutes } from './worker/http/routes/SettingsRoutes.js';
import { LogsRoutes } from './worker/http/routes/LogsRoutes.js';
// Process management for zombie cleanup (Issue #737)
import { startOrphanReaper, reapOrphanedProcesses } from './worker/ProcessRegistry.js';
/**
* Build JSON status output for hook framework communication.
* This is a pure function extracted for testability.
@@ -121,6 +124,9 @@ export class WorkerService {
private initializationComplete: Promise<void>;
private resolveInitialization!: () => void;
// Orphan reaper cleanup function (Issue #737)
private stopOrphanReaper: (() => void) | null = null;
constructor() {
// Initialize the promise that will resolve when background initialization completes
this.initializationComplete = new Promise((resolve) => {
@@ -303,6 +309,16 @@ export class WorkerService {
this.resolveInitialization();
logger.info('SYSTEM', 'Background initialization complete');
// Start orphan reaper to clean up zombie processes (Issue #737)
this.stopOrphanReaper = startOrphanReaper(() => {
const activeIds = new Set<number>();
for (const [id] of this.sessionManager['sessions']) {
activeIds.add(id);
}
return activeIds;
});
logger.info('SYSTEM', 'Started orphan reaper (runs every 5 minutes)');
// Auto-recover orphaned queues (fire-and-forget with error logging)
this.processPendingQueues(50).then(result => {
if (result.sessionsStarted > 0) {
@@ -404,6 +420,12 @@ export class WorkerService {
* Shutdown the worker service
*/
async shutdown(): Promise<void> {
// Stop orphan reaper before shutdown (Issue #737)
if (this.stopOrphanReaper) {
this.stopOrphanReaper();
this.stopOrphanReaper = null;
}
await performGracefulShutdown({
server: this.server.getHttpServer(),
sessionManager: this.sessionManager,
+252
View File
@@ -0,0 +1,252 @@
/**
* ProcessRegistry: Track spawned Claude subprocesses
*
* Fixes Issue #737: Claude haiku subprocesses don't terminate properly,
* causing zombie process accumulation (user reported 155 processes / 51GB RAM).
*
* Root causes:
* 1. SDK's SpawnedProcess interface hides subprocess PIDs
* 2. deleteSession() doesn't verify subprocess exit before cleanup
* 3. abort() is fire-and-forget with no confirmation
*
* Solution:
* - Use SDK's spawnClaudeCodeProcess option to capture PIDs
* - Track all spawned processes with session association
* - Verify exit on session deletion with timeout + SIGKILL escalation
* - Safety net orphan reaper runs every 5 minutes
*/
import { spawn, exec, ChildProcess } from 'child_process';
import { promisify } from 'util';
import { logger } from '../../utils/logger.js';
const execAsync = promisify(exec);
interface TrackedProcess {
pid: number;
sessionDbId: number;
spawnedAt: number;
process: ChildProcess;
}
// PID Registry - tracks spawned Claude subprocesses
const processRegistry = new Map<number, TrackedProcess>();
/**
* Register a spawned process in the registry
*/
export function registerProcess(pid: number, sessionDbId: number, process: ChildProcess): void {
processRegistry.set(pid, { pid, sessionDbId, spawnedAt: Date.now(), process });
logger.info('PROCESS', `Registered PID ${pid} for session ${sessionDbId}`, { pid, sessionDbId });
}
/**
* Unregister a process from the registry
*/
export function unregisterProcess(pid: number): void {
processRegistry.delete(pid);
logger.debug('PROCESS', `Unregistered PID ${pid}`, { pid });
}
/**
* Get process info by session ID
* Warns if multiple processes found (indicates race condition)
*/
export function getProcessBySession(sessionDbId: number): TrackedProcess | undefined {
const matches: TrackedProcess[] = [];
for (const [, info] of processRegistry) {
if (info.sessionDbId === sessionDbId) matches.push(info);
}
if (matches.length > 1) {
logger.warn('PROCESS', `Multiple processes found for session ${sessionDbId}`, {
count: matches.length,
pids: matches.map(m => m.pid)
});
}
return matches[0];
}
/**
* Get all active PIDs (for debugging)
*/
export function getActiveProcesses(): Array<{ pid: number; sessionDbId: number; ageMs: number }> {
const now = Date.now();
return Array.from(processRegistry.values()).map(info => ({
pid: info.pid,
sessionDbId: info.sessionDbId,
ageMs: now - info.spawnedAt
}));
}
/**
* Wait for a process to exit with timeout, escalating to SIGKILL if needed
* Uses event-based waiting instead of polling to avoid CPU overhead
*/
export async function ensureProcessExit(tracked: TrackedProcess, timeoutMs: number = 5000): Promise<void> {
const { pid, process: proc } = tracked;
// Already exited?
if (proc.killed || proc.exitCode !== null) {
unregisterProcess(pid);
return;
}
// Wait for graceful exit with timeout using event-based approach
const exitPromise = new Promise<void>((resolve) => {
proc.once('exit', () => resolve());
});
const timeoutPromise = new Promise<void>((resolve) => {
setTimeout(resolve, timeoutMs);
});
await Promise.race([exitPromise, timeoutPromise]);
// Check if exited gracefully
if (proc.killed || proc.exitCode !== null) {
unregisterProcess(pid);
return;
}
// Timeout: escalate to SIGKILL
logger.warn('PROCESS', `PID ${pid} did not exit after ${timeoutMs}ms, sending SIGKILL`, { pid, timeoutMs });
try {
proc.kill('SIGKILL');
} catch {
// Already dead
}
// Brief wait for SIGKILL to take effect
await new Promise(resolve => setTimeout(resolve, 200));
unregisterProcess(pid);
}
/**
* Kill system-level orphans (ppid=1 on Unix)
* These are Claude processes whose parent died unexpectedly
*/
async function killSystemOrphans(): Promise<number> {
if (process.platform === 'win32') {
return 0; // Windows doesn't have ppid=1 orphan concept
}
try {
const { stdout } = await execAsync(
'ps -eo pid,ppid,args 2>/dev/null | grep -E "claude.*haiku|claude.*output-format" | grep -v grep'
);
let killed = 0;
for (const line of stdout.trim().split('\n')) {
if (!line) continue;
const match = line.trim().match(/^(\d+)\s+(\d+)/);
if (match && parseInt(match[2]) === 1) { // ppid=1 = orphan
const orphanPid = parseInt(match[1]);
logger.warn('PROCESS', `Killing system orphan PID ${orphanPid}`, { pid: orphanPid });
try {
process.kill(orphanPid, 'SIGKILL');
killed++;
} catch {
// Already dead or permission denied
}
}
}
return killed;
} catch {
return 0; // No matches or error
}
}
/**
* Reap orphaned processes - both registry-tracked and system-level
*/
export async function reapOrphanedProcesses(activeSessionIds: Set<number>): Promise<number> {
let killed = 0;
// Registry-based: kill processes for dead sessions
for (const [pid, info] of processRegistry) {
if (activeSessionIds.has(info.sessionDbId)) continue; // Active = safe
logger.warn('PROCESS', `Killing orphan PID ${pid} (session ${info.sessionDbId} gone)`, { pid, sessionDbId: info.sessionDbId });
try {
info.process.kill('SIGKILL');
killed++;
} catch {
// Already dead
}
unregisterProcess(pid);
}
// System-level: find ppid=1 orphans
killed += await killSystemOrphans();
return killed;
}
/**
* Create a custom spawn function for SDK that captures PIDs
*
* The SDK's spawnClaudeCodeProcess option allows us to intercept subprocess
* creation and capture the PID before the SDK hides it.
*/
export function createPidCapturingSpawn(sessionDbId: number) {
return (spawnOptions: {
command: string;
args: string[];
cwd?: string;
env?: NodeJS.ProcessEnv;
signal?: AbortSignal;
}) => {
const child = spawn(spawnOptions.command, spawnOptions.args, {
cwd: spawnOptions.cwd,
env: spawnOptions.env,
stdio: ['pipe', 'pipe', 'pipe'],
signal: spawnOptions.signal, // CRITICAL: Pass signal for AbortController integration
windowsHide: true
});
// Register PID
if (child.pid) {
registerProcess(child.pid, sessionDbId, child);
// Auto-unregister on exit
child.on('exit', () => {
if (child.pid) {
unregisterProcess(child.pid);
}
});
}
// Return SDK-compatible interface
return {
stdin: child.stdin,
stdout: child.stdout,
get killed() { return child.killed; },
get exitCode() { return child.exitCode; },
kill: child.kill.bind(child),
on: child.on.bind(child),
once: child.once.bind(child),
off: child.off.bind(child)
};
};
}
/**
* Start the orphan reaper interval
* Returns cleanup function to stop the interval
*/
export function startOrphanReaper(getActiveSessionIds: () => Set<number>, intervalMs: number = 5 * 60 * 1000): () => void {
const interval = setInterval(async () => {
try {
const activeIds = getActiveSessionIds();
const killed = await reapOrphanedProcesses(activeIds);
if (killed > 0) {
logger.info('PROCESS', `Reaper cleaned up ${killed} orphaned processes`, { killed });
}
} catch (error) {
logger.error('PROCESS', 'Reaper error', {}, error as Error);
}
}, intervalMs);
// Return cleanup function
return () => clearInterval(interval);
}
+5 -1
View File
@@ -20,6 +20,7 @@ import { USER_SETTINGS_PATH } from '../../shared/paths.js';
import type { ActiveSession, SDKUserMessage } from '../worker-types.js';
import { ModeManager } from '../domain/ModeManager.js';
import { processAgentResponse, type WorkerRef } from './agents/index.js';
import { createPidCapturingSpawn, getProcessBySession, ensureProcessExit } from './ProcessRegistry.js';
// Import Agent SDK (assumes it's installed)
// @ts-ignore - Agent SDK types may not be available
@@ -99,6 +100,7 @@ export class SDKAgent {
// Run Agent SDK query loop
// Only resume if we have a captured memory session ID
// Use custom spawn to capture PIDs for zombie process cleanup (Issue #737)
const queryResult = query({
prompt: messageGenerator,
options: {
@@ -109,7 +111,9 @@ export class SDKAgent {
...(hasRealMemorySessionId && session.lastPromptNumber > 1 && { resume: session.memorySessionId }),
disallowedTools,
abortController: session.abortController,
pathToClaudeCodeExecutable: claudePath
pathToClaudeCodeExecutable: claudePath,
// Custom spawn function captures PIDs to fix zombie process accumulation
spawnClaudeCodeProcess: createPidCapturingSpawn(session.sessionDbId)
}
});
+16 -4
View File
@@ -14,6 +14,7 @@ import { logger } from '../../utils/logger.js';
import type { ActiveSession, PendingMessage, PendingMessageWithId, ObservationData } from '../worker-types.js';
import { PendingMessageStore } from '../sqlite/PendingMessageStore.js';
import { SessionQueueProcessor } from '../queue/SessionQueueProcessor.js';
import { getProcessBySession, ensureProcessExit } from './ProcessRegistry.js';
export class SessionManager {
private dbManager: DatabaseManager;
@@ -256,6 +257,7 @@ export class SessionManager {
/**
* Delete a session (abort SDK agent and cleanup)
* Verifies subprocess exit to prevent zombie process accumulation (Issue #737)
*/
async deleteSession(sessionDbId: number): Promise<void> {
const session = this.sessions.get(sessionDbId);
@@ -265,17 +267,27 @@ export class SessionManager {
const sessionDuration = Date.now() - session.startTime;
// Abort the SDK agent
// 1. Abort the SDK agent
session.abortController.abort();
// Wait for generator to finish
// 2. Wait for generator to finish
if (session.generatorPromise) {
await session.generatorPromise.catch(error => {
await session.generatorPromise.catch(() => {
logger.debug('SYSTEM', 'Generator already failed, cleaning up', { sessionId: session.sessionDbId });
});
}
// Cleanup
// 3. Verify subprocess exit with 5s timeout (Issue #737 fix)
const tracked = getProcessBySession(sessionDbId);
if (tracked && !tracked.process.killed && tracked.process.exitCode === null) {
logger.debug('SESSION', `Waiting for subprocess PID ${tracked.pid} to exit`, {
sessionId: sessionDbId,
pid: tracked.pid
});
await ensureProcessExit(tracked, 5000);
}
// 4. Cleanup
this.sessions.delete(sessionDbId);
this.sessionQueues.delete(sessionDbId);
+82
View File
@@ -0,0 +1,82 @@
/**
* Shared path utilities for CLAUDE.md file generation
*
* These utilities handle path normalization and matching, particularly
* for comparing absolute and relative paths in folder CLAUDE.md generation.
*
* @see Issue #794 - Path format mismatch causes folder CLAUDE.md files to show "No recent activity"
*/
/**
* Normalize path separators to forward slashes, collapse consecutive slashes,
* and remove trailing slashes.
*
* @example
* normalizePath('app\\api\\router.py') // 'app/api/router.py'
* normalizePath('app//api///router.py') // 'app/api/router.py'
* normalizePath('app/api/') // 'app/api'
*/
export function normalizePath(p: string): string {
return p.replace(/\\/g, '/').replace(/\/+/g, '/').replace(/\/+$/, '');
}
/**
* Check if a file is a direct child of a folder (not in a subfolder).
*
* Handles path format mismatches where folderPath may be absolute but
* filePath is stored as relative in the database.
*
* NOTE: This uses suffix matching which assumes both paths are relative to
* the same project root. It may produce false positives if used across
* different project roots, but this is mitigated by project-scoped queries.
*
* @param filePath - Path to the file (e.g., "app/api/router.py" or "/Users/x/project/app/api/router.py")
* @param folderPath - Path to the folder (e.g., "app/api" or "/Users/x/project/app/api")
* @returns true if file is directly in folder, false if in a subfolder or different folder
*
* @example
* // Same format (both relative)
* isDirectChild('app/api/router.py', 'app/api') // true
* isDirectChild('app/api/v1/router.py', 'app/api') // false (in subfolder)
*
* @example
* // Mixed format (absolute folder, relative file) - fixes #794
* isDirectChild('app/api/router.py', '/Users/dev/project/app/api') // true
*/
export function isDirectChild(filePath: string, folderPath: string): boolean {
const normFile = normalizePath(filePath);
const normFolder = normalizePath(folderPath);
// Strategy 1: Direct prefix match (both paths in same format)
if (normFile.startsWith(normFolder + '/')) {
const remainder = normFile.slice(normFolder.length + 1);
return !remainder.includes('/');
}
// Strategy 2: Handle absolute folderPath with relative filePath
// e.g., folderPath="/Users/x/project/app/api" and filePath="app/api/router.py"
const folderSegments = normFolder.split('/');
const fileSegments = normFile.split('/');
if (fileSegments.length < 2) return false; // Need at least folder/file
const fileDir = fileSegments.slice(0, -1).join('/'); // Directory part of file
const fileName = fileSegments[fileSegments.length - 1]; // Actual filename
// Check if folder path ends with the file's directory path
if (normFolder.endsWith('/' + fileDir) || normFolder === fileDir) {
// File is a direct child (no additional subdirectories)
return !fileName.includes('/');
}
// Check if file's directory is contained at the end of folder path
// by progressively checking suffixes
for (let i = 0; i < folderSegments.length; i++) {
const folderSuffix = folderSegments.slice(i).join('/');
if (folderSuffix === fileDir) {
return true;
}
}
return false;
}
+24 -7
View File
@@ -6,7 +6,7 @@
* <claude-mem-context> tags.
*/
import { existsSync, readFileSync, writeFileSync, renameSync, mkdirSync } from 'fs';
import { existsSync, readFileSync, writeFileSync, renameSync } from 'fs';
import path from 'path';
import os from 'os';
import { logger } from './logger.js';
@@ -76,8 +76,8 @@ export function replaceTaggedContent(existingContent: string, newContent: string
if (startIdx !== -1 && endIdx !== -1) {
return existingContent.substring(0, startIdx) +
`${startTag}\n${newContent}\n${endTag}` +
existingContent.substring(endIdx + endTag.length);
`${startTag}\n${newContent}\n${endTag}` +
existingContent.substring(endIdx + endTag.length);
}
// If no tags exist, append tagged content at end
@@ -86,17 +86,22 @@ export function replaceTaggedContent(existingContent: string, newContent: string
/**
* Write CLAUDE.md file to folder with atomic writes.
* Creates directory structure if needed.
* Only writes to existing folders; skips non-existent paths to prevent
* creating spurious directory structures from malformed paths.
*
* @param folderPath - Absolute path to the folder
* @param folderPath - Absolute path to the folder (must already exist)
* @param newContent - Content to write inside tags
*/
export function writeClaudeMdToFolder(folderPath: string, newContent: string): void {
const claudeMdPath = path.join(folderPath, 'CLAUDE.md');
const tempFile = `${claudeMdPath}.tmp`;
// Ensure directory exists
mkdirSync(folderPath, { recursive: true });
// Only write to folders that already exist - never create new directories
// This prevents creating spurious folder structures from malformed paths
if (!existsSync(folderPath)) {
logger.debug('FOLDER_INDEX', 'Skipping non-existent folder', { folderPath });
return;
}
// Read existing content if file exists
let existingContent = '';
@@ -320,6 +325,18 @@ export async function updateFolderClaudeMdFiles(
}
const formatted = formatTimelineForClaudeMd(result.content[0].text);
// Fix for #794: Don't create new CLAUDE.md files if there's no activity
// But update existing ones to show "No recent activity" if they already exist
const claudeMdPath = path.join(folderPath, 'CLAUDE.md');
const hasNoActivity = formatted.includes('*No recent activity*');
const fileExists = existsSync(claudeMdPath);
if (hasNoActivity && !fileExists) {
logger.debug('FOLDER_INDEX', 'Skipping empty CLAUDE.md creation', { folderPath });
continue;
}
writeClaudeMdToFolder(folderPath, formatted);
logger.debug('FOLDER_INDEX', 'Updated CLAUDE.md', { folderPath });
@@ -0,0 +1,124 @@
import { describe, expect, test } from 'bun:test';
import { isDirectChild, normalizePath } from '../../../src/shared/path-utils.js';
/**
* Tests for path matching logic, specifically the isDirectChild() algorithm
* Covers fix for issue #794: Path format mismatch causes folder CLAUDE.md files to show "No recent activity"
*
* These tests validate the shared path-utils module which is used by:
* - SessionSearch.ts (runtime folder CLAUDE.md generation)
* - regenerate-claude-md.ts (CLI regeneration tool)
*/
describe('isDirectChild path matching', () => {
describe('same path format', () => {
test('returns true for direct child with relative paths', () => {
expect(isDirectChild('app/api/router.py', 'app/api')).toBe(true);
});
test('returns true for direct child with absolute paths', () => {
expect(isDirectChild('/Users/dev/project/app/api/router.py', '/Users/dev/project/app/api')).toBe(true);
});
test('returns false for files in subdirectory with relative paths', () => {
expect(isDirectChild('app/api/v1/router.py', 'app/api')).toBe(false);
});
test('returns false for files in subdirectory with absolute paths', () => {
expect(isDirectChild('/Users/dev/project/app/api/v1/router.py', '/Users/dev/project/app/api')).toBe(false);
});
test('returns false for unrelated paths', () => {
expect(isDirectChild('lib/utils/helper.py', 'app/api')).toBe(false);
});
});
describe('mixed path formats (absolute folder, relative file) - fixes #794', () => {
test('returns true when absolute folder ends with relative file directory', () => {
// This is the exact bug case from #794
expect(isDirectChild('app/api/router.py', '/Users/dev/project/app/api')).toBe(true);
});
test('returns true for deeply nested folder match', () => {
expect(isDirectChild('src/components/Button.tsx', '/home/user/project/src/components')).toBe(true);
});
test('returns false for files in subdirectory of matched folder', () => {
expect(isDirectChild('app/api/v1/router.py', '/Users/dev/project/app/api')).toBe(false);
});
test('returns false when file path does not match folder suffix', () => {
expect(isDirectChild('lib/api/router.py', '/Users/dev/project/app/api')).toBe(false);
});
});
describe('path normalization', () => {
test('handles Windows backslash paths', () => {
expect(isDirectChild('app\\api\\router.py', 'app\\api')).toBe(true);
});
test('handles mixed slashes', () => {
expect(isDirectChild('app/api\\router.py', 'app\\api')).toBe(true);
});
test('handles trailing slashes on folder path', () => {
expect(isDirectChild('app/api/router.py', 'app/api/')).toBe(true);
});
test('handles double slashes (path normalization bug)', () => {
expect(isDirectChild('app//api/router.py', 'app/api')).toBe(true);
});
test('collapses multiple consecutive slashes', () => {
expect(isDirectChild('app///api///router.py', 'app//api//')).toBe(true);
});
});
describe('edge cases', () => {
test('returns false for single segment file path', () => {
expect(isDirectChild('router.py', '/Users/dev/project/app/api')).toBe(false);
});
test('returns false for empty paths', () => {
expect(isDirectChild('', 'app/api')).toBe(false);
expect(isDirectChild('app/api/router.py', '')).toBe(false);
});
test('handles root-level folders', () => {
expect(isDirectChild('src/file.ts', '/project/src')).toBe(true);
});
test('prevents false positive from partial segment match', () => {
// "api" folder should not match "api-v2" folder
expect(isDirectChild('app/api-v2/router.py', '/Users/dev/project/app/api')).toBe(false);
});
test('handles similar folder names correctly', () => {
// "components" should not match "components-old"
expect(isDirectChild('src/components-old/Button.tsx', '/project/src/components')).toBe(false);
});
});
});
describe('normalizePath', () => {
test('converts backslashes to forward slashes', () => {
expect(normalizePath('app\\api\\router.py')).toBe('app/api/router.py');
});
test('collapses consecutive slashes', () => {
expect(normalizePath('app//api///router.py')).toBe('app/api/router.py');
});
test('removes trailing slashes', () => {
expect(normalizePath('app/api/')).toBe('app/api');
expect(normalizePath('app/api///')).toBe('app/api');
});
test('handles Windows UNC paths', () => {
expect(normalizePath('\\\\server\\share\\file.txt')).toBe('/server/share/file.txt');
});
test('preserves leading slash for absolute paths', () => {
expect(normalizePath('/Users/dev/project')).toBe('/Users/dev/project');
});
});
+24 -6
View File
@@ -147,8 +147,22 @@ describe('formatTimelineForClaudeMd', () => {
});
describe('writeClaudeMdToFolder', () => {
it('should create CLAUDE.md in new folder', () => {
const folderPath = join(tempDir, 'new-folder');
it('should skip non-existent folders (fix for spurious directory creation)', () => {
const folderPath = join(tempDir, 'non-existent-folder');
const content = '# Recent Activity\n\nTest content';
// Should not throw, should silently skip
writeClaudeMdToFolder(folderPath, content);
// Folder and CLAUDE.md should NOT be created
expect(existsSync(folderPath)).toBe(false);
const claudeMdPath = join(folderPath, 'CLAUDE.md');
expect(existsSync(claudeMdPath)).toBe(false);
});
it('should create CLAUDE.md in existing folder', () => {
const folderPath = join(tempDir, 'existing-folder');
mkdirSync(folderPath, { recursive: true });
const content = '# Recent Activity\n\nTest content';
writeClaudeMdToFolder(folderPath, content);
@@ -180,20 +194,22 @@ describe('writeClaudeMdToFolder', () => {
expect(fileContent).not.toContain('Old content');
});
it('should create nested directories', () => {
it('should not create nested directories (fix for spurious directory creation)', () => {
const folderPath = join(tempDir, 'deep', 'nested', 'folder');
const content = 'Nested content';
// Should not throw, should silently skip
writeClaudeMdToFolder(folderPath, content);
// Nested directories should NOT be created
const claudeMdPath = join(folderPath, 'CLAUDE.md');
expect(existsSync(claudeMdPath)).toBe(true);
expect(existsSync(join(tempDir, 'deep'))).toBe(true);
expect(existsSync(join(tempDir, 'deep', 'nested'))).toBe(true);
expect(existsSync(claudeMdPath)).toBe(false);
expect(existsSync(join(tempDir, 'deep'))).toBe(false);
});
it('should not leave .tmp file after write (atomic write)', () => {
const folderPath = join(tempDir, 'atomic-test');
mkdirSync(folderPath, { recursive: true });
const content = 'Atomic write test';
writeClaudeMdToFolder(folderPath, content);
@@ -218,6 +234,7 @@ describe('updateFolderClaudeMdFiles', () => {
it('should fetch timeline and write CLAUDE.md', async () => {
const folderPath = join(tempDir, 'api-test');
mkdirSync(folderPath, { recursive: true }); // Folder must exist - we no longer create directories
const filePath = join(folderPath, 'test.ts');
const apiResponse = {
@@ -412,6 +429,7 @@ describe('updateFolderClaudeMdFiles', () => {
it('should write CLAUDE.md to resolved projectRoot path', async () => {
const subfolderPath = join(tempDir, 'project-root-write-test', 'src', 'utils');
mkdirSync(subfolderPath, { recursive: true }); // Folder must exist - we no longer create directories
const apiResponse = {
content: [{