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:
82
packages/server/src/local/memory-records.ts
Normal file
82
packages/server/src/local/memory-records.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* How the memory HTTP routes read and present contract records (ADR-0007).
|
||||
*
|
||||
* The Memory Center keeps its own fields in record metadata:
|
||||
* - `archived` (boolean) and `archivedImportance`: an archived memory is set to
|
||||
* importance 'temporary', which chat recall skips; unarchiving restores the
|
||||
* importance it had.
|
||||
* - `confirmedAt` (ISO 8601): the user confirmed a memory extracted in the background.
|
||||
* - `title`, `tags`, `evidence`, `relatedMemoryIds`, `mergedIntoId`.
|
||||
* Chat write-back stores `trace_id`; the legacy import stores `legacy_created_at`.
|
||||
*/
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import {
|
||||
MEMORY_IMPORTANCE,
|
||||
MEMORY_KINDS,
|
||||
PERSONAL_SCOPE,
|
||||
workspaceScope,
|
||||
type MemoryImportance,
|
||||
type MemoryKind,
|
||||
type MemoryRecord,
|
||||
type MemoryScope,
|
||||
} from '@waggle/memory-contract';
|
||||
|
||||
export type MemoryLifecycle = 'active' | 'unreviewed' | 'archived';
|
||||
|
||||
/** The origin background fact extraction writes with (see memory-extraction.ts). */
|
||||
const EXTRACTION_ORIGIN = 'extraction';
|
||||
|
||||
/** The workspace's scope when the workspace exists, else personal memory. */
|
||||
export function scopeForWorkspace(server: FastifyInstance, workspaceId: string | undefined): MemoryScope {
|
||||
return workspaceId && server.workspaceManager.get(workspaceId)
|
||||
? workspaceScope(workspaceId)
|
||||
: PERSONAL_SCOPE;
|
||||
}
|
||||
|
||||
/** Personal memory plus every workspace's memory. */
|
||||
export function allScopes(server: FastifyInstance): MemoryScope[] {
|
||||
let workspaceIds: string[] = [];
|
||||
try {
|
||||
workspaceIds = server.workspaceManager.list().map((ws) => ws.id);
|
||||
} catch { /* no workspace directory yet */ }
|
||||
return [PERSONAL_SCOPE, ...workspaceIds.map((id) => workspaceScope(id))];
|
||||
}
|
||||
|
||||
export function workspaceIdOf(record: MemoryRecord): string | undefined {
|
||||
return record.scope.kind === 'workspace' ? record.scope.workspaceId : undefined;
|
||||
}
|
||||
|
||||
export function asImportance(value: unknown): MemoryImportance | undefined {
|
||||
return MEMORY_IMPORTANCE.includes(value as MemoryImportance) ? (value as MemoryImportance) : undefined;
|
||||
}
|
||||
|
||||
export function asKind(value: unknown): MemoryKind | undefined {
|
||||
return MEMORY_KINDS.includes(value as MemoryKind) ? (value as MemoryKind) : undefined;
|
||||
}
|
||||
|
||||
/** Extracted from a chat in the background and not yet confirmed by the user. */
|
||||
export function awaitsReview(record: MemoryRecord): boolean {
|
||||
return record.origin === EXTRACTION_ORIGIN
|
||||
&& record.source === 'agent_inferred'
|
||||
&& typeof record.metadata.confirmedAt !== 'string';
|
||||
}
|
||||
|
||||
export function memoryLifecycle(record: MemoryRecord): MemoryLifecycle {
|
||||
if (record.metadata.archived === true) return 'archived';
|
||||
return awaitsReview(record) ? 'unreviewed' : 'active';
|
||||
}
|
||||
|
||||
/**
|
||||
* When the memory was first written. Memories copied from the legacy engine
|
||||
* keep their original time in metadata; the store's own time is the copy time.
|
||||
*/
|
||||
export function memoryCreatedAt(record: MemoryRecord): string {
|
||||
const legacy = record.metadata.legacy_created_at;
|
||||
if (typeof legacy === 'string') {
|
||||
// SQLite's datetime('now') is UTC without a zone: "YYYY-MM-DD HH:MM:SS".
|
||||
const iso = /^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$/.test(legacy) ? `${legacy.replace(' ', 'T')}Z` : legacy;
|
||||
const time = Date.parse(iso);
|
||||
if (Number.isFinite(time)) return new Date(time).toISOString();
|
||||
}
|
||||
return record.createdAt;
|
||||
}
|
||||
Reference in New Issue
Block a user