refactor(server): serve memory and Memory Center from the memory contract (P1.5)

The memory HTTP API reads and writes server.memory instead of the legacy
MultiMind, FrameStore, SessionStore, KnowledgeGraph, MindErasure,
SuppressionStore and RawArchive objects.

- memory.ts: search (personal, workspace, all, global), frames list, write,
  edit and delete, stats and quick capture on the contract. Ids are strings.
  The access-count route is gone. New GET /api/memory/capabilities and
  PUT /api/memory/settings { extraction } for background fact extraction.
- memory-center.ts: list, get, create, patch, archive, confirm, trace,
  delete, erase and merge on the contract. Archive sets importance
  'temporary' (skipped by recall) and remembers the old importance; confirm
  stamps metadata.confirmedAt; 'unreviewed' means extracted in the
  background and not confirmed. Erase forgets one memory or every memory of
  a chat session. The original-source and suppression routes are removed:
  there is no raw archive and no harvest to re-import from.
- knowledge.ts returns an empty graph (not part of v1).
- export.ts writes memories/personal.json and memories/workspace-<id>.json
  from the contract.
- identity.ts loses a dead resolver; the erase safety check recognizes
  waggle.db.
- memory-records.ts holds the scope, status and creation-time helpers.
- normalizeToMemory stays in memory-center-legacy.ts until home.ts and
  artifacts.ts read memory through the contract.
- @waggle/shared MemoryKind follows the contract's kinds; Memory gains
  origin and sessionId.

Tests: new memory-routes and memory-center tests on the in-memory store;
local-mode and data-export tests seed memory through the contract; tests of
removed behaviour (source, suppression, sticky erasure, graph projection,
legacy normalizer, old erase and stats) are deleted.
This commit is contained in:
shumeiko.it
2026-10-24 12:22:46 +02:00
parent 88fbc0a9e7
commit 06d4532598
19 changed files with 1369 additions and 3034 deletions

View File

@@ -3,7 +3,8 @@ import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { Readable } from 'node:stream';
import { MindDB, FrameStore, SessionStore, WorkspaceManager, WaggleConfig, VaultStore } from '@waggle/core';
import { WorkspaceManager, WaggleConfig, VaultStore } from '@waggle/core';
import { PERSONAL_SCOPE, workspaceScope } from '@waggle/memory-contract';
import { buildLocalServer } from '../src/local/index.js';
import type { FastifyInstance } from 'fastify';
import { injectWithAuth } from './test-utils.js';
@@ -73,24 +74,16 @@ function extractZipFileContent(buffer: Buffer, targetName: string): string | nul
describe('Data Export (GDPR)', () => {
let server: FastifyInstance;
let tmpDir: string;
let workspaceId: string;
beforeAll(async () => {
// Create a temp directory for test data
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-export-test-'));
// Create personal.mind with test data
const personalPath = path.join(tmpDir, 'personal.mind');
const mind = new MindDB(personalPath);
const sessions = new SessionStore(mind);
const frames = new FrameStore(mind);
const s1 = sessions.create('test-export');
frames.createIFrame(s1.gop_id, 'Export test memory frame', 'normal');
frames.createIFrame(s1.gop_id, 'Another memory for export', 'important');
mind.close();
// Create a workspace with a session
const wsManager = new WorkspaceManager(tmpDir);
const ws = wsManager.create({ name: 'Test Export WS', group: 'Testing' });
workspaceId = ws.id;
const sessionsDir = path.join(tmpDir, 'workspaces', ws.id, 'sessions');
fs.mkdirSync(sessionsDir, { recursive: true });
fs.writeFileSync(
@@ -113,8 +106,11 @@ describe('Data Export (GDPR)', () => {
const vault = new VaultStore(tmpDir);
vault.set('anthropic', 'sk-ant-secret-key-12345678', { models: ['claude-sonnet-4-6'] });
// Build the local server
// Build the local server, then write memories through the contract
server = await buildLocalServer({ dataDir: tmpDir });
await server.memory.remember({ scope: PERSONAL_SCOPE, text: 'Export test memory', importance: 'normal' });
await server.memory.remember({ scope: PERSONAL_SCOPE, text: 'Another memory for export', importance: 'important' });
await server.memory.remember({ scope: workspaceScope(workspaceId), text: 'Workspace memory for export' });
}, 60_000);
afterAll(async () => {
@@ -142,6 +138,8 @@ describe('Data Export (GDPR)', () => {
const hasVaultMeta = fileNames.some(n => n === 'vault-metadata.json');
expect(hasMemories).toBe(true);
expect(fileNames).toContain('memories/personal.json');
expect(fileNames).toContain(`memories/workspace-${workspaceId}.json`);
expect(hasSessions).toBe(true);
expect(hasWorkspaces).toBe(true);
expect(hasSettings).toBe(true);
@@ -177,9 +175,6 @@ describe('Data Export (GDPR)', () => {
it('export works with empty data (new installation)', async () => {
// Create a fresh server with empty data
const emptyDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-export-empty-'));
const emptyPersonalPath = path.join(emptyDir, 'personal.mind');
const emptyMind = new MindDB(emptyPersonalPath);
emptyMind.close();
const emptyServer = await buildLocalServer({ dataDir: emptyDir });
try {
@@ -191,7 +186,7 @@ describe('Data Export (GDPR)', () => {
const fileNames = extractZipFileNames(buffer);
// Even with empty data, core files should exist
expect(fileNames).toContain('memories/personal-frames.json');
expect(fileNames).toContain('memories/personal.json');
expect(fileNames).toContain('settings.json');
expect(fileNames).toContain('vault-metadata.json');
} finally {
@@ -200,6 +195,14 @@ describe('Data Export (GDPR)', () => {
}
});
it('a workspace export holds only that workspace\'s memories', async () => {
const res = await injectWithAuth(server, { method: 'POST', url: '/api/export', payload: { workspaceId } });
expect(res.statusCode).toBe(200);
const fileNames = extractZipFileNames(Buffer.from(res.rawPayload));
expect(fileNames).toContain(`memories/workspace-${workspaceId}.json`);
expect(fileNames).not.toContain('memories/personal.json');
});
it('export ZIP has reasonable size (not empty)', async () => {
const res = await injectWithAuth(server, { method: 'POST', url: '/api/export' });
const buffer = Buffer.from(res.rawPayload);