Fix: Agents creating duplicate .claude-flow and .swarm folders in subdirectories
Problem
Agents are creating duplicate .claude-flow and .swarm folders in subdirectories instead of using the main workspace root folders. This causes:
- Fragmented memory storage across multiple locations
- Agents not sharing state properly
- Increased disk usage with redundant folders
- Confusion about which folder contains the actual data
Root Cause
The code was using process.cwd() to determine folder paths, which returns the current working directory. When agents operate in subdirectories, they create local folders instead of using the project root folders.
Locations Affected
Multiple duplicate folders were found:
./docker/docker-test/.swarm./examples/litellm/.claude-flow./benchmark/.claude-flow./docs/.claude-flow./claude-flow-wiki/.claude-flow- And several others...
Solution Implemented
Created a project root detection utility that:
- Searches upward from any directory to find the main project root
- Prioritizes the main claude-flow package root over subdirectory markers
- Always returns paths relative to the true project root
- Ensures all agents use the same
.claude-flowand.swarmfolders
Files Modified
- Created:
src/utils/project-root.js- New utility for project root detection - Updated:
src/memory/sqlite-store.js- Use project root instead of process.cwd() - Updated:
src/memory/migration.js- Use project root for paths - Updated:
src/memory/shared-memory.js- Use project root for database paths - Updated:
src/memory/unified-memory-manager.js- Use project root for config paths
Testing
Verified that from any subdirectory, the utility correctly identifies the project root and returns the main workspace folders.
Impact
- All agents will now use the same centralized folders
- Memory and state will be properly shared
- No more duplicate folder creation in subdirectories
- Consistent behavior regardless of where agents operate
Fixes #763
✅ Fix Verified and Implemented
The issue has been resolved with the implementation of a robust project root detection mechanism.
Verification Results
Tested the new project root detection from various subdirectories:
From root directory:
- Project Root:
/workspaces/claude-code-flow✅ - Claude Flow Dir:
/workspaces/claude-code-flow/.claude-flow✅ - Swarm Dir:
/workspaces/claude-code-flow/.swarm✅
From src/memory subdirectory:
- Project Root:
/workspaces/claude-code-flow✅ - Claude Flow Dir:
/workspaces/claude-code-flow/.claude-flow✅ - Swarm Dir:
/workspaces/claude-code-flow/.swarm✅
From examples/litellm subdirectory (previously problematic):
- Project Root:
/workspaces/claude-code-flow✅ - Claude Flow Dir:
/workspaces/claude-code-flow/.claude-flow✅ - Swarm Dir:
/workspaces/claude-code-flow/.swarm✅
Key Improvements
- Intelligent Root Detection: The new utility prioritizes the main project root even when subdirectories have their own
.claude-flowfolders - Consistent Paths: All agents now use the same root-level folders regardless of their working directory
- Backwards Compatible: Falls back to
process.cwd()if no project root is found - Cached Results: Project root is cached to avoid repeated filesystem lookups
Next Steps
- Monitor agent behavior to ensure no new duplicate folders are created
- Consider cleaning up existing duplicate folders in subdirectories
- Update documentation to reflect the new behavior
The fix ensures all agents properly coordinate through the main workspace folders as intended.