|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import { fileURLToPath } from 'url'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Create the agent's persistent memory scaffold, container-side, at boot. |
| 7 | + * |
| 8 | + * The runner owns its own workspace: it writes the memory tree straight into |
| 9 | + * `/workspace/agent` (the host-backed, RW group dir, so it persists across the |
| 10 | + * ephemeral container). No host-side step, nothing mounted in. |
| 11 | + * |
| 12 | + * The default `definition.md` / `index.md` live as real markdown templates next |
| 13 | + * to this module (under `memory-templates/`) — not as strings in code — so the |
| 14 | + * doctrine is editable as markdown and the agent receives an unescaped copy. |
| 15 | + * They ship in the mounted `/app/src` tree, so no image change is needed. |
| 16 | + * |
| 17 | + * Idempotent — only writes what's missing, so the agent's own edits and |
| 18 | + * accumulated memory are never clobbered on a later wake. Provider-agnostic: |
| 19 | + * the runner makes no assumption about which harness is running — a provider |
| 20 | + * opts in via `usesMemoryScaffold`. |
| 21 | + */ |
| 22 | +const TEMPLATES_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), 'memory-templates'); |
| 23 | + |
| 24 | +export function ensureMemoryScaffold(baseDir = '/workspace/agent'): void { |
| 25 | + const memoryDir = path.join(baseDir, 'memory'); |
| 26 | + const systemDir = path.join(memoryDir, 'system'); |
| 27 | + |
| 28 | + for (const dir of [systemDir, path.join(memoryDir, 'memories'), path.join(memoryDir, 'data')]) { |
| 29 | + fs.mkdirSync(dir, { recursive: true }); |
| 30 | + } |
| 31 | + |
| 32 | + copyTemplateIfMissing('definition.md', path.join(systemDir, 'definition.md')); |
| 33 | + copyTemplateIfMissing('index.md', path.join(memoryDir, 'index.md')); |
| 34 | +} |
| 35 | + |
| 36 | +function copyTemplateIfMissing(template: string, dest: string): void { |
| 37 | + if (fs.existsSync(dest)) return; |
| 38 | + fs.copyFileSync(path.join(TEMPLATES_DIR, template), dest); |
| 39 | +} |
0 commit comments