-
-
Notifications
You must be signed in to change notification settings - Fork 8
feat: cross-run project memory (.ralph/memory.md) #277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { join } from 'node:path'; | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
| import { appendProjectMemory, formatMemoryPrompt, readProjectMemory } from '../memory.js'; | ||
|
|
||
| describe('Project Memory', () => { | ||
| let testDir: string; | ||
|
|
||
| beforeEach(() => { | ||
| testDir = mkdtempSync(join(tmpdir(), 'ralph-memory-test-')); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rmSync(testDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe('readProjectMemory', () => { | ||
| it('should return undefined when no memory exists', () => { | ||
| expect(readProjectMemory(testDir)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should read existing memory', () => { | ||
| const ralphDir = join(testDir, '.ralph'); | ||
| mkdirSync(ralphDir, { recursive: true }); | ||
| writeFileSync(join(ralphDir, 'memory.md'), '## 2026-03-01\nThis project uses pnpm\n'); | ||
|
|
||
| const result = readProjectMemory(testDir); | ||
| expect(result).toContain('pnpm'); | ||
| }); | ||
|
|
||
| it('should return undefined for empty memory file', () => { | ||
| const ralphDir = join(testDir, '.ralph'); | ||
| mkdirSync(ralphDir, { recursive: true }); | ||
| writeFileSync(join(ralphDir, 'memory.md'), ''); | ||
|
|
||
|
|
||
| expect(readProjectMemory(testDir)).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('appendProjectMemory', () => { | ||
| it('should create .ralph directory and memory file', () => { | ||
| appendProjectMemory(testDir, 'Tests are in __tests__/'); | ||
|
|
||
| const memoryPath = join(testDir, '.ralph', 'memory.md'); | ||
| expect(existsSync(memoryPath)).toBe(true); | ||
|
|
||
| const content = readFileSync(memoryPath, 'utf-8'); | ||
| expect(content).toContain('Tests are in __tests__/'); | ||
| expect(content).toMatch(/^## \d{4}-\d{2}-\d{2}/); | ||
| }); | ||
|
|
||
| it('should append multiple entries', () => { | ||
| appendProjectMemory(testDir, 'Entry 1'); | ||
| appendProjectMemory(testDir, 'Entry 2'); | ||
|
|
||
| const content = readFileSync(join(testDir, '.ralph', 'memory.md'), 'utf-8'); | ||
| expect(content).toContain('Entry 1'); | ||
| expect(content).toContain('Entry 2'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('formatMemoryPrompt', () => { | ||
| it('should format memory as a prompt section', () => { | ||
| const result = formatMemoryPrompt('This project uses pnpm'); | ||
| expect(result).toContain('Project Memory'); | ||
| expect(result).toContain('pnpm'); | ||
| expect(result).toContain('.ralph/memory.md'); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /** | ||
| * Project Memory | ||
| * | ||
| * Persistent memory file (.ralph/memory.md) that survives across separate `ralph run` invocations. | ||
| * The agent can learn project conventions, tool preferences, and patterns over time. | ||
| */ | ||
|
|
||
| import { appendFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
|
|
||
| const MEMORY_FILE = 'memory.md'; | ||
| const MAX_MEMORY_BYTES = 8 * 1024; // 8KB max — keeps context window usage reasonable | ||
|
|
||
| /** | ||
| * Read the project memory file. | ||
| * Returns undefined if no memory exists yet. | ||
| */ | ||
| export function readProjectMemory(cwd: string): string | undefined { | ||
| try { | ||
| const memoryPath = join(cwd, '.ralph', MEMORY_FILE); | ||
| if (!existsSync(memoryPath)) return undefined; | ||
|
|
||
| const content = readFileSync(memoryPath, 'utf-8').trim(); | ||
| if (!content) return undefined; | ||
|
|
||
| // Truncate if too large (keep the most recent entries) | ||
| if (Buffer.byteLength(content) > MAX_MEMORY_BYTES) { | ||
| const entries = content.split(/^## /m).filter((e) => e.trim()); | ||
| let result = ''; | ||
| // Build from newest to oldest, staying under budget | ||
| for (let i = entries.length - 1; i >= 0; i--) { | ||
| const entry = `## ${entries[i]}`; | ||
| if (Buffer.byteLength(result + entry) > MAX_MEMORY_BYTES) break; | ||
| result = entry + result; | ||
| } | ||
| return result.trim() || undefined; | ||
| } | ||
|
|
||
| return content; | ||
| } catch { | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Append an entry to the project memory file. | ||
| */ | ||
| export function appendProjectMemory(cwd: string, entry: string): void { | ||
| try { | ||
| const ralphDir = join(cwd, '.ralph'); | ||
| if (!existsSync(ralphDir)) mkdirSync(ralphDir, { recursive: true }); | ||
|
|
||
| const memoryPath = join(ralphDir, MEMORY_FILE); | ||
| const timestamp = new Date().toISOString().split('T')[0]; | ||
| const formatted = `## ${timestamp}\n${entry.trim()}\n\n`; | ||
|
|
||
| appendFileSync(memoryPath, formatted); | ||
| } catch { | ||
| // Non-critical — don't break the loop | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Format memory content as a prompt section for injection into agent context. | ||
| */ | ||
| export function formatMemoryPrompt(memory: string): string { | ||
| return `## Project Memory (from previous runs) | ||
| The following notes were saved from previous ralph-starter runs on this project. | ||
| Use them to understand project conventions and avoid repeating mistakes. | ||
|
|
||
| ${memory} | ||
|
|
||
| If you discover new project conventions or important patterns, append them to \`.ralph/memory.md\`. | ||
| `; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.