Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Aug 2, 2025

This PR implements a hierarchical memory management system as requested in issue #6602. The feature allows Roo Code to automatically load memory files (like CLAUDE.md or Roorules.md) recursively from parent directories when reading files.

Summary of Changes

Core Implementation

  • HierarchicalMemoryManager: New class that handles recursive directory walking and memory file loading
  • Duplicate Prevention: Uses a Set to track already-loaded memory files to prevent duplicates
  • ApiMessage Extension: Added isHierarchicalMemory flag to distinguish memory messages from regular messages

Integration Points

  • readFileTool: Integrated memory loading after file approval
  • Task: Added memory injection and state management
  • Context Compression: Hierarchical memories are automatically included in the compression process

UI Components

  • ContextManagementSettings: Added toggle and file name configuration
  • HierarchicalMemoryModal: New modal for viewing loaded memory files
  • TaskActions: Added memory view button that appears when memories are loaded

Configuration

  • Global Settings: Added enableHierarchicalMemory and hierarchicalMemoryFileNames settings
  • Default: Feature is disabled by default, with "Roorules.md" as the default memory file name

Testing

  • Comprehensive test suite for HierarchicalMemoryManager
  • Tests cover duplicate prevention, error handling, and edge cases
  • All existing tests continue to pass

How It Works

  1. When a file is read via readFileTool, the system checks if hierarchical memory is enabled
  2. If enabled, it walks up the directory tree from the file's location to the workspace root
  3. At each directory level, it looks for configured memory files (e.g., Roorules.md)
  4. Found memory files are loaded and marked with isHierarchicalMemory: true
  5. These memories are injected into the conversation context before the file content
  6. Users can view loaded memories via the memory view button in the UI

Benefits

  • Reduces token waste by automatically loading relevant context
  • Improves context management for large projects
  • Maintains backward compatibility (disabled by default)
  • Provides transparency through the memory viewer UI

Fixes #6602


Important

Implements a hierarchical memory management system with new classes, UI components, and settings, enhancing context management by loading memory files recursively from parent directories.

  • Core Implementation:
    • Introduces HierarchicalMemoryManager class for recursive directory walking and memory file loading.
    • Prevents duplicate memory file loading using a Set.
    • Extends ApiMessage with isHierarchicalMemory flag.
  • Integration Points:
    • Integrates memory loading in readFileTool() after file approval.
    • Adds memory injection and state management in Task class.
    • Includes hierarchical memories in context compression.
  • UI Components:
    • Adds ContextManagementSettings for toggle and file name configuration.
    • Introduces HierarchicalMemoryModal for viewing loaded memory files.
    • Updates TaskActions with a memory view button.
  • Configuration:
    • Adds enableHierarchicalMemory and hierarchicalMemoryFileNames to global settings.
    • Feature is disabled by default, with "Roorules.md" as default memory file name.
  • Testing:
    • Comprehensive tests for HierarchicalMemoryManager covering duplicates, errors, and edge cases.
    • Existing tests remain unaffected.

This description was created by Ellipsis for a2e6ec5. You can customize this summary. It will automatically update as commits are pushed.

- Add HierarchicalMemoryManager class to recursively load memory files
- Update ApiMessage interface with isHierarchicalMemory flag
- Integrate memory loading into readFileTool
- Add UI controls in ContextManagementSettings
- Create HierarchicalMemoryModal for viewing loaded memories
- Add memory view button to TaskActions
- Implement context compression compatibility
- Add comprehensive tests for HierarchicalMemoryManager
- Add translation keys for new UI elements

Fixes #6602
@roomote roomote bot requested review from cte and mrubens as code owners August 2, 2025 15:47
@roomote roomote bot requested a review from jr as a code owner August 2, 2025 15:47
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. enhancement New feature or request UI/UX UI/UX related or focused labels Aug 2, 2025
Copy link
Contributor Author

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed my own code and found bugs I didn't know I was capable of writing.

if (exists) {
const body = await fs.readFile(full, "utf8")
messages.push({
role: "user",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentional? The hierarchical memory messages are created with role "user" but the issue specification shows "system" role in the examples. This could affect how the AI interprets these memory instructions.

this.read.add(full)
}
} catch (e: any) {
if (e.code !== "ENOENT") console.error(e)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential memory leak: The read Set never gets cleared except when explicitly calling clearCache(). For long-running tasks, this could accumulate many file paths. Consider:

Suggested change
if (e.code !== "ENOENT") console.error(e)
} catch (e: any) {
if (e.code !== "ENOENT") {
console.error(`Failed to read memory file ${full}:`, e)
}
}

Also, should we implement a size limit or clear the cache when the task ends?

} = state ?? {}

// Load hierarchical memory for approved files
if (cline.memoryManager) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Race condition risk: Multiple approved files trigger memory loading sequentially. If files are in the same directory, the timing could cause issues. Consider collecting unique directories first:

Suggested change
if (cline.memoryManager) {
// Load hierarchical memory for approved files
if (cline.memoryManager) {
const approvedFiles = fileResults.filter((result) => result.status === "approved")
const uniqueDirs = new Set<string>()
for (const fileResult of approvedFiles) {
const fullPath = path.resolve(cline.cwd, fileResult.path)
uniqueeDirs.add(path.dirname(fullPath))
}
for (const dir of uniqueDirs) {
const memoryMessages = await cline.memoryManager.loadFor(dir, cline.cwd)
if (memoryMessages.length > 0) {
await cline.injectHierarchicalMemory(memoryMessages)
}
}
}

expect(result).toHaveLength(1)
})

it.skip("should handle Windows-style paths", async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Windows path test is skipped. Could we add proper cross-platform path handling tests? This is important for Windows users.

<h3 className="text-sm font-medium mb-2 text-vscode-foreground">
{t("chat:hierarchicalMemory.loadedFiles")}
</h3>
<div className="space-y-1 overflow-y-auto max-h-[calc(80vh-200px)]">
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UI enhancement opportunity: When many memory files are loaded, users might benefit from a search/filter feature. Consider adding a simple text filter above the file list?

@roomote roomote bot mentioned this pull request Aug 2, 2025
4 tasks
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Aug 2, 2025
@daniel-lxs
Copy link
Member

Closing, not on the roadmap

@daniel-lxs daniel-lxs closed this Aug 2, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Aug 2, 2025
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Aug 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:XL This PR changes 500-999 lines, ignoring generated files. UI/UX UI/UX related or focused

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Layered and modular memory management

4 participants