mirror of
https://github.com/rohitg00/agentmemory.git
synced 2026-09-14 20:16:33 +08:00
9c27eff610
Adds mem::reflect: traverses knowledge graph, groups related memories by concept clusters, synthesizes higher-order insights via LLM. New files: - src/prompts/reflect.ts — LLM prompt for insight synthesis - src/functions/reflect.ts — 4 functions: reflect, insight-list, insight-search, insight-decay-sweep - test/reflect.test.ts — 13 tests New type: Insight (confidence-scored, fingerprint-deduped, decayable) New KV scope: mem:insights New MCP tools: memory_reflect, memory_insight_list (in ESSENTIAL set) New REST: POST /reflect, GET /insights, POST /insights/search Hooks into consolidation pipeline as "reflect" tier after semantic Review fixes applied: - Fixed project filter no-op (was discarding filter result) - Fixed BFS depth tracking (standard level-count pattern) - Added attribute-order constraint to LLM prompt - Whitelisted fields in api::insight-search (no raw body passthrough) - Fixed Jaccard fallback to compare document-id sets not concept strings 43 MCP tools, 103 REST endpoints, 627 tests passing
4.5 KiB
4.5 KiB
agentmemory — Agent Instructions
Architecture
agentmemory is a persistent memory system for AI coding agents, built on iii-engine's three primitives (Worker/Function/Trigger). Everything goes through registerFunction/registerTrigger/sdk.trigger() — never bypass iii-engine with standalone SQLite or in-process alternatives.
- Engine: iii-sdk (WebSocket to iii-engine on port 49134)
- State: File-based SQLite via iii-engine's StateModule (
./data/state_store.db) - Build: TypeScript → ESM via tsdown, output to
dist/ - Test: vitest (
npm testexcludes integration tests)
Consistency Rules
When adding or removing MCP tools, you MUST update ALL of the following:
src/mcp/tools-registry.ts— tool definition +getAllTools()arraysrc/mcp/server.ts— handler case in themcp::tools::callswitchsrc/triggers/api.ts— REST endpoint registrationsrc/index.ts— function registration + endpoint count in the log linetest/mcp-standalone.test.ts— tool count assertionREADME.md— tool counts (search for "MCP tools")plugin/.claude-plugin/plugin.json— tool count in description
When adding REST endpoints, you MUST update:
src/triggers/api.ts— endpoint registrationsrc/index.ts— endpoint count in the log lineREADME.md— endpoint count (search for "REST endpoints" and "endpoints on port")
When bumping version, you MUST update ALL of the following:
package.json— version fieldsrc/version.ts— VERSION constant and type unionsrc/types.ts— ExportData version unionsrc/functions/export-import.ts— supportedVersions settest/export-import.test.ts— version assertionplugin/.claude-plugin/plugin.json— version field
When adding new KV scopes:
src/state/schema.ts— add to the KV objectsrc/types.ts— add the corresponding interface
When adding new audit operations:
src/types.ts— add to AuditEntry.operation union type
Code Patterns
Function Registration
sdk.registerFunction(
{ id: "mem::your-function" },
async (data: { ... }) => {
// validate inputs
// do work via kv.get/kv.set/kv.list
// record audit via recordAudit()
return { success: true, ... };
},
);
REST Endpoint Registration
sdk.registerFunction({ id: "api::your-endpoint" }, async (req: ApiRequest) => {
const denied = checkAuth(req, secret);
if (denied) return denied;
const body = req.body as Record<string, unknown>;
// validate + whitelist fields (never pass raw body to sdk.trigger)
const result = await sdk.trigger("mem::your-function", { ... });
return { status_code: 200, body: result };
});
sdk.registerTrigger({
type: "http",
function_id: "api::your-endpoint",
config: { api_path: "/agentmemory/your-path", http_method: "POST" },
});
MCP Tool Handler
case "memory_your_tool": {
// validate args with typeof checks
// parse CSV args: args.field.split(",").map(t => t.trim()).filter(Boolean)
const result = await sdk.trigger("mem::your-function", { ... });
return { status_code: 200, body: { content: [{ type: "text", text: JSON.stringify(result, null, 2) }] } };
}
Hook Scripts
Hook scripts in src/hooks/ are standalone Node.js scripts (no iii-sdk import). They read JSON from stdin, make HTTP calls to the REST API, and exit. Always use try/catch with AbortSignal.timeout() for best-effort calls.
Coding Standards
- TypeScript, ESM only (
"type": "module") - No code comments explaining WHAT — use clear naming instead
- Use
fingerprintId()for content-addressable dedup,generateId()for unique IDs - Parallel operations where possible (
Promise.allfor independent kv writes/reads) - Input validation at system boundaries (MCP handlers, REST endpoints)
- REST endpoints must whitelist fields — never pass raw request body to
sdk.trigger() - Use
recordAudit()for state-changing operations - Timestamps: capture once with
new Date().toISOString()and reuse
Testing
- All tests must pass before PR:
npm test(613+ tests) - Mock pattern:
vi.mock("iii-sdk")with mocksdk.trigger,kv.get/set/list - Test files go in
test/with.test.tsextension - Follow existing patterns in
test/crystallize.test.tsfor function tests
Current Stats (v0.7.0)
- 43 MCP tools (8 visible by default,
AGENTMEMORY_TOOLS=allfor all) - 103 REST endpoints
- 6 MCP resources, 3 MCP prompts
- 12 hooks, 4 skills
- 50+ iii functions
- 627 tests