/** * Anthropic memory-tool backend adapter (ADR-007 Wave 4a, migration-map 4a). * * Anthropic's client-side memory tool (docs.claude.com memory-tool) expects * the HARNESS to execute file-like commands (view/create/str_replace/insert/ * delete/rename) against a virtual `/memories` filesystem. This module maps * those commands onto astramem memories so any Claude app using the memory * tool can be backed by the daemon — see docs/memory-tool-adapter.md for the * full command-mapping table and limitations. * * Virtual filesystem shape: * /memories -> directory listing of ".md" files * /memories/.md -> markdown bullet list of current valid * memories of that type, one line per * memory: "- [] " * /memories/.md/ -> addresses a single memory (delete only) * * `` must be one of the MemoryType enum values (decision, fact, * lesson, command, todo, note, event). For create/insert, an unrecognized * type slug in the path falls back to 'note' rather than erroring — the * model may invent filenames the harness has never seen before. * * Design rulings (binding, see dispatch prompt for Wave 4a): * - create/insert: file_text/insert_text becomes ONE memory's text (no * bullet parsing — parsing markdown bullets back into memories is lossy * and was explicitly rejected). Redacted via redactIfEnabled, inserted * via MemoryRepo.insertWithCreateEvent (mirrors POST /remember), but * WITHOUT embedding metadata — embedding is not required synchronously, * the memory is FTS-searchable immediately and vec-indexed on reembed. * - str_replace: finds the single valid memory of that type whose text * contains old_str, then supersedes it (insert new + MemoryEventRepo * .supersede) rather than mutating the row in place — every memory * write goes through the ADR-002 event log. * - delete: whole-file (`/memories/.md`) erases every current * memory of that type; per-id (`/memories/.md/`) erases just * that one. Both use MemoryEventRepo.erase (hard delete + tombstone * event, ADR-006 W5) — never a soft invalidate. * - rename: NOT supported v1 (a "rename" would mean changing an atom's * type, which isn't modeled) — always returns an error result. * - Unknown paths/commands never throw; they return { error }. */ import type { DB } from '../storage/db.js'; import type { Config } from '../config/config.js'; export type MemoryToolCommandName = 'view' | 'create' | 'str_replace' | 'insert' | 'delete' | 'rename'; /** * Loose input shape — mirrors the fields Anthropic's memory tool sends per * command (see docs/memory-tool-adapter.md § command mapping). Fields not * relevant to a given command are simply ignored. */ export interface MemoryToolCommand { command: MemoryToolCommandName | string; path: string; view_range?: [number, number]; file_text?: string; old_str?: string; new_str?: string; insert_line?: number; insert_text?: string; old_path?: string; new_path?: string; } export type MemoryToolResult = { content: string; } | { error: string; }; /** * Maps one Anthropic memory-tool command onto astramem memories. Never * throws — every failure path (bad path, unknown command, storage error) * comes back as { error } so the caller can relay it straight into a * tool_result without its own try/catch. */ export declare function handleMemoryToolCommand(db: DB, config: Config, cmd: MemoryToolCommand): MemoryToolResult;