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:
@@ -169,7 +169,7 @@ function walkSize(dir: string, dataDirRoot: string): { fileCount: number; totalB
|
||||
* (a) the basename starts with "waggle" / ".waggle" / contains "waggle",
|
||||
* OR
|
||||
* (b) the dir contains at least one of the recognized Waggle artifacts
|
||||
* (personal.mind, vault.db, config.json with a Waggle-shaped key).
|
||||
* (waggle.db, a legacy personal.mind, vault.db, config.json with a Waggle-shaped key).
|
||||
*
|
||||
* Refusing on a foreign path means a misconfigured or malicious marker
|
||||
* can't trick the wipe into deleting the user's home dir or similar.
|
||||
@@ -199,7 +199,7 @@ export function assertDataDirIsSafeToWipe(dataDir: string): string | null {
|
||||
if (looksLikeWaggleByName) return null;
|
||||
|
||||
// Fallback: contains a Waggle-shaped artifact?
|
||||
const waggleArtifacts = ['personal.mind', 'config.json', 'vault.db', '.vault-key'];
|
||||
const waggleArtifacts = ['waggle.db', 'personal.mind', 'config.json', 'vault.db', '.vault-key'];
|
||||
for (const artifact of waggleArtifacts) {
|
||||
if (fs.existsSync(path.join(resolved, artifact))) return null;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
* Export routes — GDPR-compliant "Download my data" endpoint.
|
||||
*
|
||||
* POST /api/export — generates and returns a ZIP file containing:
|
||||
* - memories/ — all memory frames from .mind DB (JSON)
|
||||
* - memories/ — personal and per-workspace memories (memory contract records, JSON)
|
||||
* - sessions/ — all session .jsonl files as markdown transcripts
|
||||
* - workspaces/ — workspace configs (JSON)
|
||||
* - settings.json — current settings with API keys masked
|
||||
@@ -14,10 +14,14 @@ import path from 'node:path';
|
||||
import { PassThrough } from 'node:stream';
|
||||
import type { FastifyPluginAsync } from 'fastify';
|
||||
import archiver from 'archiver';
|
||||
import { FrameStore, MindDB, WaggleConfig } from '@waggle/core';
|
||||
import { WaggleConfig } from '@waggle/core';
|
||||
import { PERSONAL_SCOPE, workspaceScope, type MemoryScope } from '@waggle/memory-contract';
|
||||
import { exportSessionToMarkdown } from './sessions.js';
|
||||
import { emitAuditEvent } from './events.js';
|
||||
|
||||
/** Every memory of a scope. */
|
||||
const EXPORT_ALL = 1_000_000;
|
||||
|
||||
function maskApiKey(key: string): string {
|
||||
if (!key || key.length < 12) return '****';
|
||||
return key.slice(0, 7) + '...' + key.slice(-4);
|
||||
@@ -37,57 +41,23 @@ export const exportRoutes: FastifyPluginAsync = async (server) => {
|
||||
const passthrough = new PassThrough();
|
||||
archive.pipe(passthrough);
|
||||
|
||||
// ── 1. Memories (from personal mind) ────────────────────────────
|
||||
// D1: Skip personal mind when exporting a single workspace (privacy for agency/multi-client)
|
||||
if (!scopedWorkspaceId) {
|
||||
try {
|
||||
const personalDb = server.multiMind.personal;
|
||||
const frameStore = new FrameStore(personalDb);
|
||||
const frames = frameStore.list({ limit: 100000 });
|
||||
archive.append(JSON.stringify(frames, null, 2), { name: 'memories/personal-frames.json' });
|
||||
} catch {
|
||||
archive.append('[]', { name: 'memories/personal-frames.json' });
|
||||
}
|
||||
}
|
||||
|
||||
// Export workspace mind frames too
|
||||
// F1 fix: Load MindDB from disk when not in cache. The cached getter
|
||||
// (getWorkspaceMindDb) may return null if the workspace was never
|
||||
// activated during this server session, even though frames exist on disk.
|
||||
// D1: When scopedWorkspaceId is provided, export only that workspace
|
||||
// ── 1. Memories (personal and per workspace) ───────────────────
|
||||
// D1: When scopedWorkspaceId is provided, export only that workspace and
|
||||
// skip personal memory (privacy for agency/multi-client).
|
||||
const allWorkspaces = server.workspaceManager.list();
|
||||
const workspaces = scopedWorkspaceId
|
||||
? allWorkspaces.filter(ws => ws.id === scopedWorkspaceId)
|
||||
: allWorkspaces;
|
||||
for (const ws of workspaces) {
|
||||
let ownedDb: MindDB | null = null;
|
||||
const memoryFiles: Array<{ scope: MemoryScope; name: string }> = [
|
||||
...(scopedWorkspaceId ? [] : [{ scope: PERSONAL_SCOPE, name: 'memories/personal.json' }]),
|
||||
...workspaces.map(ws => ({ scope: workspaceScope(ws.id), name: `memories/workspace-${ws.id}.json` })),
|
||||
];
|
||||
for (const { scope, name } of memoryFiles) {
|
||||
try {
|
||||
// Try the cached getter first (avoids re-opening if already loaded)
|
||||
let wsDb = server.agentState.getWorkspaceMindDb(ws.id);
|
||||
|
||||
// Fallback: open directly from disk if the cache returned null
|
||||
if (!wsDb) {
|
||||
const mindPath = path.join(dataDir, 'workspaces', ws.id, 'workspace.mind');
|
||||
if (fs.existsSync(mindPath)) {
|
||||
ownedDb = new MindDB(mindPath);
|
||||
wsDb = ownedDb;
|
||||
}
|
||||
}
|
||||
|
||||
if (wsDb) {
|
||||
const wsFrameStore = new FrameStore(wsDb);
|
||||
const wsFrames = wsFrameStore.list({ limit: 100000 });
|
||||
archive.append(JSON.stringify(wsFrames, null, 2), {
|
||||
name: `memories/workspace-${ws.id}-frames.json`,
|
||||
});
|
||||
}
|
||||
const records = await server.memory.list({ scopes: [scope], limit: EXPORT_ALL });
|
||||
archive.append(JSON.stringify(records, null, 2), { name });
|
||||
} catch {
|
||||
// Skip inaccessible workspace minds
|
||||
} finally {
|
||||
// Close the DB we opened (don't leak handles for export-only opens)
|
||||
if (ownedDb) {
|
||||
try { ownedDb.close(); } catch { /* already closed */ }
|
||||
}
|
||||
archive.append('[]', { name });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// CC Sesija A A1.1 follow-up — /api/identity sidecar route.
|
||||
//
|
||||
// Backs the get_identity Tauri command + adapter.getIdentity(). Returns the
|
||||
// IdentityLayer record from packages/core/src/mind/identity.ts when configured,
|
||||
// profile card (IdentityLayer in waggle.db, ADR-0007) when configured,
|
||||
// or a placeholder shape (configured: false, all fields null) when not.
|
||||
//
|
||||
// The placeholder path matches the shape returned by the Tauri command's
|
||||
@@ -47,24 +47,7 @@ function placeholder(note?: string): IdentityResponseShape {
|
||||
}
|
||||
|
||||
export const identityRoutes: FastifyPluginAsync = async (server) => {
|
||||
/**
|
||||
* Resolve the MindDB to read the identity record from. Defaults to personal
|
||||
* mind; ?workspace=<id> selects a workspace-scoped identity (workspaces own
|
||||
* their own identity rows because the mind/identity.ts schema is per-DB).
|
||||
*/
|
||||
function resolveDb(workspaceId: string | undefined): { db: ReturnType<typeof server.multiMind.personal.getDatabase> | null; whichMind: string } {
|
||||
if (workspaceId && workspaceId !== 'personal') {
|
||||
const wsMindDb = server.agentState?.getWorkspaceMindDb?.(workspaceId);
|
||||
if (wsMindDb) {
|
||||
return { db: wsMindDb.getDatabase(), whichMind: workspaceId };
|
||||
}
|
||||
}
|
||||
const personal = server.multiMind?.personal;
|
||||
if (!personal) return { db: null, whichMind: 'personal' };
|
||||
return { db: personal.getDatabase(), whichMind: 'personal' };
|
||||
}
|
||||
|
||||
// GET /api/identity?workspace=<id> — read identity (defaults to personal mind)
|
||||
// GET /api/identity?workspace=<id> — read identity (defaults to the personal profile)
|
||||
server.get<{ Querystring: { workspace?: string; workspaceId?: string } }>(
|
||||
'/api/identity',
|
||||
async (request, reply) => {
|
||||
@@ -76,7 +59,7 @@ export const identityRoutes: FastifyPluginAsync = async (server) => {
|
||||
try {
|
||||
const layer = new IdentityLayer(server.waggleDb, identityScope(server, wsId));
|
||||
if (!layer.exists()) {
|
||||
return reply.send(placeholder('identity not configured for this mind'));
|
||||
return reply.send(placeholder('identity not configured for this scope'));
|
||||
}
|
||||
const id = layer.get();
|
||||
return reply.send({
|
||||
@@ -97,8 +80,8 @@ export const identityRoutes: FastifyPluginAsync = async (server) => {
|
||||
},
|
||||
);
|
||||
|
||||
// POST /api/identity — create or update the identity record. Single-row
|
||||
// table per MindDB (id = 1), so create + update collapse into upsert.
|
||||
// POST /api/identity — create or update the identity record. One profile per
|
||||
// scope in waggle.db, so create + update collapse into upsert.
|
||||
server.post<{
|
||||
Body: {
|
||||
workspace?: string;
|
||||
|
||||
@@ -1,135 +1,19 @@
|
||||
import type { FastifyPluginAsync } from 'fastify';
|
||||
import { KnowledgeGraph } from '@waggle/core';
|
||||
import type { MindDB } from '@waggle/core';
|
||||
import { assertSafeSegment } from './validate.js';
|
||||
|
||||
interface KGRow {
|
||||
id: number;
|
||||
name?: string;
|
||||
type?: string;
|
||||
entity_type?: string;
|
||||
source_id?: number;
|
||||
target_id?: number;
|
||||
relation_type?: string;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
function extractKGFromMind(mindDb: MindDB): { entities: KGRow[]; relations: KGRow[] } {
|
||||
try {
|
||||
const raw = mindDb.getDatabase();
|
||||
const entities = raw.prepare(
|
||||
'SELECT * FROM knowledge_entities WHERE valid_to IS NULL ORDER BY name'
|
||||
).all() as KGRow[];
|
||||
const relations = raw.prepare(
|
||||
'SELECT * FROM knowledge_relations WHERE valid_to IS NULL ORDER BY id'
|
||||
).all() as KGRow[];
|
||||
return { entities, relations };
|
||||
} catch {
|
||||
return { entities: [], relations: [] };
|
||||
}
|
||||
}
|
||||
|
||||
// The FE knowledge-graph contract (KGNode {id,label,type} / KGEdge
|
||||
// {source,target,relationship}) differs from the raw SQLite columns
|
||||
// (entity_type/name, source_id/target_id/relation_type). The route used to
|
||||
// return raw rows, so every node read as type "Unknown" and no edge resolved
|
||||
// (FE filtered on undefined .source/.target). Project to the contract as the
|
||||
// FINAL step on every return path — AFTER the scope==='all' numeric-id merge,
|
||||
// so the id-offset math (which reads numeric ids) still runs on raw rows.
|
||||
interface ProjectedNode { id: string; label: string; type: string }
|
||||
interface ProjectedEdge { source: string; target: string; relationship: string }
|
||||
|
||||
const asStr = (v: unknown): string | undefined =>
|
||||
typeof v === 'string' && v.length > 0 ? v : undefined;
|
||||
|
||||
function projectKG(kg: { entities: KGRow[]; relations: KGRow[] }): {
|
||||
nodes: ProjectedNode[];
|
||||
edges: ProjectedEdge[];
|
||||
} {
|
||||
const nodes: ProjectedNode[] = kg.entities.map((e) => ({
|
||||
id: String(e.id),
|
||||
label: asStr(e.name) ?? String(e.id),
|
||||
type: asStr(e.entity_type) ?? asStr(e.type) ?? 'unknown',
|
||||
}));
|
||||
const edges: ProjectedEdge[] = kg.relations
|
||||
.filter((r) => r.source_id != null && r.target_id != null)
|
||||
.map((r) => ({
|
||||
source: String(r.source_id),
|
||||
target: String(r.target_id),
|
||||
relationship: asStr(r.relation_type) ?? asStr(r.relationship) ?? 'related',
|
||||
}));
|
||||
return { nodes, edges };
|
||||
}
|
||||
|
||||
/**
|
||||
* The knowledge graph is not part of v1 (ADR-0002): the memory store reports
|
||||
* `capabilities.knowledgeGraph: false` and the UI hides the graph. The route
|
||||
* stays so callers that still ask get an empty graph in the shape the UI reads
|
||||
* (KGNode {id,label,type} / KGEdge {source,target,relationship}).
|
||||
*/
|
||||
export const knowledgeRoutes: FastifyPluginAsync = async (server) => {
|
||||
// GET /api/memory/graph?workspace=:id&scope=all|personal|current
|
||||
server.get<{
|
||||
Querystring: { workspace?: string; workspaceId?: string; scope?: string };
|
||||
}>('/api/memory/graph', async (request, reply) => {
|
||||
Querystring: { workspace?: string; workspaceId?: string };
|
||||
}>('/api/memory/graph', async (request) => {
|
||||
const workspaceId = request.query.workspace ?? request.query.workspaceId;
|
||||
const scope = request.query.scope;
|
||||
|
||||
if (scope === 'all') {
|
||||
const merged = { entities: [] as KGRow[], relations: [] as KGRow[] };
|
||||
let idOffset = 0;
|
||||
|
||||
// Personal mind
|
||||
const personal = extractKGFromMind(server.multiMind.personal);
|
||||
for (const e of personal.entities) {
|
||||
merged.entities.push({ ...e, _source: 'personal' });
|
||||
}
|
||||
for (const r of personal.relations) {
|
||||
merged.relations.push({ ...r, _source: 'personal' });
|
||||
}
|
||||
idOffset = Math.max(
|
||||
...merged.entities.map(e => e.id ?? 0),
|
||||
...merged.relations.map(r => r.id ?? 0),
|
||||
0
|
||||
) + 1000;
|
||||
|
||||
// Each workspace
|
||||
const workspaces = server.agentState.listWorkspaces();
|
||||
for (const ws of workspaces) {
|
||||
const wsDb = server.agentState.getWorkspaceMindDb(ws.id);
|
||||
if (!wsDb) continue;
|
||||
const wsKG = extractKGFromMind(wsDb);
|
||||
|
||||
for (const e of wsKG.entities) {
|
||||
merged.entities.push({
|
||||
...e,
|
||||
id: (e.id ?? 0) + idOffset,
|
||||
_source: ws.name,
|
||||
});
|
||||
}
|
||||
for (const r of wsKG.relations) {
|
||||
merged.relations.push({
|
||||
...r,
|
||||
id: (r.id ?? 0) + idOffset,
|
||||
source_id: (r.source_id ?? 0) + idOffset,
|
||||
target_id: (r.target_id ?? 0) + idOffset,
|
||||
_source: ws.name,
|
||||
});
|
||||
}
|
||||
idOffset += 100000;
|
||||
}
|
||||
|
||||
return projectKG(merged);
|
||||
}
|
||||
|
||||
if (scope === 'personal' || (!workspaceId && !scope)) {
|
||||
return projectKG(extractKGFromMind(server.multiMind.personal));
|
||||
}
|
||||
|
||||
// Current/specific workspace
|
||||
if (workspaceId) {
|
||||
assertSafeSegment(workspaceId, 'workspace');
|
||||
const wsDb = server.agentState.getWorkspaceMindDb(workspaceId);
|
||||
if (!wsDb) {
|
||||
return { nodes: [], edges: [] };
|
||||
}
|
||||
return projectKG(extractKGFromMind(wsDb));
|
||||
}
|
||||
|
||||
if (workspaceId) assertSafeSegment(workspaceId, 'workspace');
|
||||
return { nodes: [], edges: [] };
|
||||
});
|
||||
};
|
||||
|
||||
38
packages/server/src/local/routes/memory-center-legacy.ts
Normal file
38
packages/server/src/local/routes/memory-center-legacy.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import type { MemoryFrame } from '@waggle/core';
|
||||
import type { Memory, MemoryStatus } from '@waggle/shared';
|
||||
import { asKind } from '../memory-records.js';
|
||||
|
||||
/**
|
||||
* @deprecated Projects a legacy frame-store row into the Memory shape. Only
|
||||
* `home.ts` and `artifacts.ts` still read frames; once they read memory through
|
||||
* the contract (`recordToMemory` in memory-center.ts), delete this file.
|
||||
*/
|
||||
export function normalizeToMemory(frame: MemoryFrame, mind: string, workspaceId?: string): Memory {
|
||||
let meta: Record<string, unknown> = {};
|
||||
try {
|
||||
const parsed: unknown = typeof frame.metadata === 'string' && frame.metadata ? JSON.parse(frame.metadata) : {};
|
||||
if (parsed && typeof parsed === 'object') meta = parsed as Record<string, unknown>;
|
||||
} catch { /* malformed metadata reads as empty */ }
|
||||
const content = frame.content ?? '';
|
||||
const importance: Memory['importance'] = frame.importance ?? 'normal';
|
||||
const status: MemoryStatus = typeof meta.status === 'string'
|
||||
? meta.status as MemoryStatus
|
||||
: importance === 'deprecated'
|
||||
? 'deprecated'
|
||||
: importance === 'temporary' && frame.source === 'agent_inferred' ? 'unreviewed' : 'active';
|
||||
const firstLine = content.split('\n').find((l) => l.trim().length > 0)?.trim() ?? content.trim();
|
||||
return {
|
||||
id: String(frame.id),
|
||||
kind: asKind(meta.kind) ?? 'fact',
|
||||
title: typeof meta.title === 'string' && meta.title.trim()
|
||||
? meta.title
|
||||
: firstLine.length > 80 ? `${firstLine.slice(0, 79)}…` : firstLine,
|
||||
content,
|
||||
scope: mind === 'workspace' ? 'workspace' : 'personal',
|
||||
workspaceId,
|
||||
source: frame.source ?? 'user_stated',
|
||||
importance,
|
||||
status,
|
||||
createdAt: frame.created_at ?? new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,15 +1,33 @@
|
||||
import type { FastifyPluginAsync } from 'fastify';
|
||||
import type { SearchScope, Importance, FrameSource, MemoryFrame } from '@waggle/core';
|
||||
import {
|
||||
AwarenessLayer,
|
||||
WaggleConfig,
|
||||
workspaceDataScope,
|
||||
evaluateExternalMemoryIngress,
|
||||
FrameStore,
|
||||
KnowledgeGraph,
|
||||
SessionStore,
|
||||
} from '@waggle/core';
|
||||
import { extractEntities } from '@waggle/agent';
|
||||
import {
|
||||
MEMORY_SOURCES,
|
||||
MemoryNotFoundError,
|
||||
PERSONAL_SCOPE,
|
||||
workspaceScope,
|
||||
type MemoryHit,
|
||||
type MemoryImportance,
|
||||
type MemoryKind,
|
||||
type MemoryRecord,
|
||||
type MemoryScope,
|
||||
type MemorySource,
|
||||
} from '@waggle/memory-contract';
|
||||
import { emitAuditEvent } from './events.js';
|
||||
import {
|
||||
allScopes,
|
||||
asImportance,
|
||||
asKind,
|
||||
memoryCreatedAt,
|
||||
memoryLifecycle,
|
||||
scopeForWorkspace,
|
||||
workspaceIdOf,
|
||||
type MemoryLifecycle,
|
||||
} from '../memory-records.js';
|
||||
|
||||
// ── UX-Refactor Phase 1 (S01) quick-capture kinds ─────────────────────────
|
||||
type QuickCaptureKind = 'note' | 'task' | 'link' | 'file';
|
||||
@@ -141,617 +159,280 @@ export function sanitizeFrameContent(content: string): string {
|
||||
return chunks.join('');
|
||||
}
|
||||
|
||||
/** Normalize SQLite snake_case MemoryFrame fields to camelCase UI Frame shape. */
|
||||
function normalizeFrame(raw: Record<string, unknown>): Record<string, unknown> {
|
||||
// F24: Determine source_mind. For search results from MultiMind, `_mind` is set
|
||||
// explicitly. For MultiMindSearchResult, the `source` field is 'personal' | 'workspace'
|
||||
// which would overwrite the frame's provenance source. We detect this and split correctly.
|
||||
const mindSource = raw._mind as string | undefined;
|
||||
const rawSource = raw.source as string | undefined;
|
||||
// If source is 'personal' or 'workspace', it's the MultiMindSearchResult mind label,
|
||||
// not the frame's provenance source. Use it for source_mind, fall back to DB provenance.
|
||||
const isMindLabel = rawSource === 'personal' || rawSource === 'workspace';
|
||||
const sourceMind = mindSource ?? (isMindLabel ? rawSource : 'personal');
|
||||
const provenance = isMindLabel ? (raw._provenance_source as string ?? 'user_stated') : (rawSource ?? 'user_stated');
|
||||
/** A memory as the frame endpoints (`/api/memory/search`, `/api/memory/frames`) return it. */
|
||||
export interface MemoryFrameResponse {
|
||||
id: string;
|
||||
content: string;
|
||||
kind: MemoryKind;
|
||||
importance: MemoryImportance;
|
||||
source: MemorySource;
|
||||
/** Which memory it lives in: personal or a workspace's. */
|
||||
mind: 'personal' | 'workspace';
|
||||
workspaceId: string | null;
|
||||
status: MemoryLifecycle;
|
||||
timestamp: string;
|
||||
/** Search relevance; only on search results. */
|
||||
score?: number;
|
||||
/** Global search: the workspace's name. */
|
||||
workspaceName?: string;
|
||||
}
|
||||
|
||||
export function toFrameResponse(record: MemoryRecord | MemoryHit, workspaceName?: string): MemoryFrameResponse {
|
||||
return {
|
||||
id: raw.id,
|
||||
content: raw.content,
|
||||
// F7: Preserve provenance source from DB
|
||||
source: provenance,
|
||||
// F24: Which mind this frame came from — UI can show badge/icon
|
||||
source_mind: sourceMind,
|
||||
// Legacy alias for backward compat
|
||||
mind: sourceMind,
|
||||
frameType: raw.frame_type ?? raw.frameType ?? 'I',
|
||||
importance: raw.importance ?? 'normal',
|
||||
timestamp: raw.created_at ?? raw.timestamp ?? new Date().toISOString(),
|
||||
score: raw.score,
|
||||
gop: raw.gop_id ?? raw.gop,
|
||||
accessCount: raw.access_count,
|
||||
// I3: Team attribution (present for synced frames)
|
||||
...(raw.author_id || raw.authorId ? { authorId: raw.author_id ?? raw.authorId } : {}),
|
||||
...(raw.author_name || raw.authorName ? { authorName: raw.author_name ?? raw.authorName } : {}),
|
||||
// Global search: which workspace this frame belongs to
|
||||
...(raw._workspace_name ? { workspaceName: raw._workspace_name } : {}),
|
||||
id: record.id,
|
||||
content: record.text,
|
||||
kind: record.kind,
|
||||
importance: record.importance,
|
||||
source: record.source,
|
||||
mind: record.scope.kind,
|
||||
workspaceId: workspaceIdOf(record) ?? null,
|
||||
status: memoryLifecycle(record),
|
||||
timestamp: memoryCreatedAt(record),
|
||||
...('score' in record ? { score: record.score } : {}),
|
||||
...(workspaceName ? { workspaceName } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
function parseLimit(value: string | undefined, fallback: number): number {
|
||||
const parsed = value ? parseInt(value, 10) : NaN;
|
||||
return Number.isFinite(parsed) && parsed > 0 ? Math.min(parsed, 500) : fallback;
|
||||
}
|
||||
|
||||
/** Memory routes on the memory contract (ADR-0007). Ids are unique across scopes. */
|
||||
export const memoryRoutes: FastifyPluginAsync = async (server) => {
|
||||
/**
|
||||
* Ensure multiMind has the correct workspace mind loaded before searching.
|
||||
* Uses the server's workspace mind cache to avoid closing/reopening DBs.
|
||||
*/
|
||||
function ensureWorkspaceMind(workspaceId: string): void {
|
||||
const wsDb = server.agentState.getWorkspaceMindDb(workspaceId);
|
||||
if (!wsDb) return;
|
||||
// Use setWorkspace (not switchWorkspace) to avoid opening duplicate connections.
|
||||
// The cache owns DB lifecycle — multiMind just borrows the reference.
|
||||
if (server.multiMind.workspace !== wsDb) {
|
||||
server.multiMind.setWorkspace(wsDb);
|
||||
}
|
||||
}
|
||||
|
||||
function searchAllWorkspaces(srv: typeof server, query: string, limit: number): Array<Omit<MemoryFrame, 'source'> & { source?: string; _mind?: string; _workspace_name?: string }> {
|
||||
const results: Array<Omit<MemoryFrame, 'source'> & { source?: string; _mind?: string; _workspace_name?: string }> = [];
|
||||
|
||||
// Search personal mind
|
||||
const personalResults = srv.multiMind.search(query, 'personal', limit);
|
||||
for (const r of personalResults) {
|
||||
results.push({ ...r, _mind: 'personal' });
|
||||
}
|
||||
|
||||
// Search each workspace mind via the cache
|
||||
const workspaces = srv.agentState.listWorkspaces?.() ?? [];
|
||||
for (const ws of workspaces) {
|
||||
const wsDb = srv.agentState.getWorkspaceMindDb(ws.id);
|
||||
if (!wsDb) continue;
|
||||
try {
|
||||
const db = wsDb.getDatabase();
|
||||
const FTS_STOP_WORDS = new Set(['the', 'a', 'an', 'is', 'are', 'was', 'were', 'be', 'been', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'could', 'should', 'to', 'of', 'in', 'for', 'on', 'with', 'at', 'by', 'from', 'as', 'this', 'that', 'it', 'its', 'and', 'or', 'but', 'not']);
|
||||
const safeQuery = query
|
||||
.split(/\s+/)
|
||||
.map(w => w.replace(/[^\w]/g, ''))
|
||||
.filter(w => w.length > 2 && !FTS_STOP_WORDS.has(w.toLowerCase()))
|
||||
.map(w => `"${w.replace(/"/g, '')}"`)
|
||||
.join(' OR ');
|
||||
if (!safeQuery) continue;
|
||||
|
||||
const rows = db.prepare(`
|
||||
SELECT mf.* FROM memory_frames_fts fts
|
||||
JOIN memory_frames mf ON mf.id = fts.rowid
|
||||
WHERE fts.content MATCH ?
|
||||
ORDER BY rank LIMIT ?
|
||||
`).all(safeQuery, Math.ceil(limit / 2)) as MemoryFrame[];
|
||||
|
||||
for (const r of rows) {
|
||||
results.push({ ...r, _mind: 'workspace', _workspace_name: ws.name });
|
||||
}
|
||||
} catch { /* skip inaccessible workspace */ }
|
||||
}
|
||||
|
||||
results.sort((a, b) => (b.created_at ?? '').localeCompare(a.created_at ?? ''));
|
||||
return results.slice(0, limit);
|
||||
}
|
||||
|
||||
function recoverSearchProvenance(mind: unknown, frameId: unknown): string | undefined {
|
||||
if (mind !== 'personal' && mind !== 'workspace') return undefined;
|
||||
const id = typeof frameId === 'number' ? frameId : Number(frameId);
|
||||
if (!Number.isFinite(id)) return undefined;
|
||||
const workspaceNames = (): Map<string, string> => {
|
||||
try {
|
||||
const frame = server.multiMind.getFrameStore(mind)?.getById(id);
|
||||
return frame?.source;
|
||||
return new Map(server.workspaceManager.list().map((ws) => [ws.id, ws.name]));
|
||||
} catch {
|
||||
return undefined;
|
||||
return new Map();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// GET /api/memory/search?q=query&scope=all|global&workspace=wsId&since=ISO&until=ISO
|
||||
const extractionSettings = () => ({
|
||||
capabilities: server.memory.capabilities,
|
||||
settings: { extraction: new WaggleConfig(server.localConfig.dataDir).getMemoryExtraction() },
|
||||
});
|
||||
|
||||
// GET /api/memory/capabilities — what the memory store supports, and whether
|
||||
// facts are extracted from chats in the background. The UI hides what is off.
|
||||
server.get('/api/memory/capabilities', async () => extractionSettings());
|
||||
|
||||
// PUT /api/memory/settings — turn background fact extraction on or off.
|
||||
server.put<{ Body: { extraction?: unknown } }>('/api/memory/settings', async (request, reply) => {
|
||||
const { extraction } = request.body ?? {};
|
||||
if (typeof extraction !== 'boolean') {
|
||||
return reply.status(400).send({ error: 'extraction must be a boolean' });
|
||||
}
|
||||
new WaggleConfig(server.localConfig.dataDir).setMemoryExtraction(extraction);
|
||||
return extractionSettings();
|
||||
});
|
||||
|
||||
// GET /api/memory/search?q=&scope=personal|workspace|all|global&workspace=&limit=&since=&until=
|
||||
// Ranked by mem0's hybrid vector and keyword search. 'all' is personal memory
|
||||
// plus the given workspace's; 'global' is personal memory plus every workspace.
|
||||
server.get<{
|
||||
Querystring: { q?: string; scope?: string; limit?: string; workspace?: string; workspaceId?: string; since?: string; until?: string };
|
||||
}>('/api/memory/search', async (request, reply) => {
|
||||
// P0-4: Accept both 'workspace' and 'workspaceId'
|
||||
const { q, scope, limit, workspace: ws, workspaceId: wsId, since, until } = request.query;
|
||||
const workspace = ws ?? wsId;
|
||||
const { q, scope, limit, since, until } = request.query;
|
||||
const workspace = request.query.workspace ?? request.query.workspaceId;
|
||||
if (!q) {
|
||||
return reply.status(400).send({ error: 'q (query) parameter is required' });
|
||||
}
|
||||
|
||||
const searchScope = (scope === 'personal' || scope === 'workspace' || scope === 'all' || scope === 'global')
|
||||
? scope
|
||||
: 'all';
|
||||
|
||||
// Ensure workspace mind is loaded for workspace/all scope searches
|
||||
if (workspace && (searchScope === 'workspace' || searchScope === 'all')) {
|
||||
ensureWorkspaceMind(workspace);
|
||||
}
|
||||
|
||||
const maxResults = limit ? parseInt(limit, 10) : 20;
|
||||
|
||||
// `source` here may be the MultiMind mind label ('personal'/'workspace'), which
|
||||
// is NOT a FrameSource — so omit the frame's source and re-add it as a plain
|
||||
// string (see the mind-label handling below).
|
||||
let rawResults: Array<Omit<MemoryFrame, 'source'> & { source?: string }>;
|
||||
|
||||
if (searchScope === 'global') {
|
||||
rawResults = searchAllWorkspaces(server, q, maxResults);
|
||||
const workspaceKnown = !!workspace && !!server.workspaceManager.get(workspace);
|
||||
let scopes: MemoryScope[];
|
||||
if (scope === 'global') {
|
||||
scopes = allScopes(server);
|
||||
} else if (scope === 'personal') {
|
||||
scopes = [PERSONAL_SCOPE];
|
||||
} else if (scope === 'workspace') {
|
||||
scopes = workspaceKnown ? [workspaceScope(workspace!)] : [];
|
||||
} else {
|
||||
rawResults = server.multiMind.search(q, searchScope as SearchScope, maxResults);
|
||||
scopes = workspaceKnown ? [PERSONAL_SCOPE, workspaceScope(workspace!)] : [PERSONAL_SCOPE];
|
||||
}
|
||||
if (scopes.length === 0) return { results: [], count: 0 };
|
||||
|
||||
// F20: Temporal filtering — filter by since/until ISO date strings
|
||||
if (since || until) {
|
||||
rawResults = rawResults.filter(r => {
|
||||
const ts = r.created_at;
|
||||
if (!ts) return true;
|
||||
if (since && ts < since) return false;
|
||||
if (until && ts > until) return false;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
// F24: MultiMindSearchResult.source is the mind label ('personal'/'workspace'),
|
||||
// which overwrites the frame's original provenance source. Preserve both.
|
||||
const results = rawResults.map(r => {
|
||||
const obj = r as unknown as Record<string, unknown>;
|
||||
// The frame's DB source column was overwritten by MultiMind search.
|
||||
// Stash it so normalizeFrame can recover both values.
|
||||
// We can detect: if source is 'personal'/'workspace', it's the mind label.
|
||||
// The original frame source can be recovered from the DB if needed, but
|
||||
// for the common case we mark _mind from the MultiMindSearchResult.source.
|
||||
if (obj.source === 'personal' || obj.source === 'workspace') {
|
||||
obj._mind = obj.source;
|
||||
// D7: Recover original provenance from the frame's DB row if available.
|
||||
// The HybridSearch result includes the full frame — check for _original_source
|
||||
// or fall back to the frame object's nested source field.
|
||||
const frameObj = (r as { frame?: { source?: string } }).frame;
|
||||
const dbSource = frameObj?.source ?? recoverSearchProvenance(obj.source, obj.id);
|
||||
if (dbSource && dbSource !== 'personal' && dbSource !== 'workspace') {
|
||||
obj._provenance_source = dbSource;
|
||||
}
|
||||
}
|
||||
return normalizeFrame(obj);
|
||||
const hits = await server.memory.search(q, {
|
||||
scopes,
|
||||
limit: parseLimit(limit, 20),
|
||||
...(since && { since }),
|
||||
...(until && { until }),
|
||||
});
|
||||
const names = scope === 'global' ? workspaceNames() : null;
|
||||
const results = hits.map((hit) => {
|
||||
const wsId = workspaceIdOf(hit);
|
||||
return toFrameResponse(hit, wsId ? names?.get(wsId) : undefined);
|
||||
});
|
||||
|
||||
return { results, count: results.length };
|
||||
});
|
||||
|
||||
// GET /api/memory/frames?workspace=wsId&limit=50&since=ISO&until=ISO
|
||||
// Returns recent frames without requiring a search query — used by Memory tab initial load.
|
||||
// GET /api/memory/frames?workspace=&limit=&since=&until= — newest personal
|
||||
// memories plus the workspace's, without a query (Timeline initial load).
|
||||
server.get<{
|
||||
Querystring: { workspace?: string; workspaceId?: string; limit?: string; since?: string; until?: string };
|
||||
}>('/api/memory/frames', async (request, reply) => {
|
||||
// P0-4: Accept both 'workspace' and 'workspaceId'
|
||||
const { workspace: ws, workspaceId: wsId, limit, since, until } = request.query;
|
||||
const workspace = ws ?? wsId;
|
||||
const maxResults = limit ? parseInt(limit, 10) : 50;
|
||||
|
||||
const raw: Array<Record<string, unknown>> = [];
|
||||
|
||||
// Personal mind frames
|
||||
const personalStore = server.multiMind.getFrameStore('personal');
|
||||
if (personalStore) {
|
||||
const pFrames = personalStore.getRecent(maxResults);
|
||||
raw.push(...pFrames.map(f => ({ ...(f as unknown as Record<string, unknown>), _mind: 'personal' })));
|
||||
}
|
||||
|
||||
// Workspace mind frames (use cached MindDB directly — no switchWorkspace needed)
|
||||
if (workspace) {
|
||||
const wsDb = server.agentState.getWorkspaceMindDb(workspace);
|
||||
if (wsDb) {
|
||||
const wsStore = new FrameStore(wsDb);
|
||||
const wFrames = wsStore.getRecent(maxResults);
|
||||
raw.push(...wFrames.map(f => ({ ...(f as unknown as Record<string, unknown>), _mind: 'workspace' })));
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by created_at descending and limit
|
||||
raw.sort((a, b) =>
|
||||
String(b.created_at ?? '').localeCompare(String(a.created_at ?? '')),
|
||||
);
|
||||
|
||||
// F20: Temporal filtering — filter by since/until ISO date strings
|
||||
let filtered = raw;
|
||||
if (since || until) {
|
||||
filtered = raw.filter(r => {
|
||||
const ts = String(r.created_at ?? '');
|
||||
if (!ts) return true;
|
||||
if (since && ts < since) return false;
|
||||
if (until && ts > until) return false;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
const results = filtered.slice(0, maxResults).map(normalizeFrame);
|
||||
}>('/api/memory/frames', async (request) => {
|
||||
const { limit, since, until } = request.query;
|
||||
const scope = scopeForWorkspace(server, request.query.workspace ?? request.query.workspaceId);
|
||||
const records = await server.memory.list({
|
||||
scopes: scope.kind === 'workspace' ? [PERSONAL_SCOPE, scope] : [PERSONAL_SCOPE],
|
||||
limit: parseLimit(limit, 50),
|
||||
...(since && { since }),
|
||||
...(until && { until }),
|
||||
});
|
||||
const results = records.map((record) => toFrameResponse(record));
|
||||
return { results, count: results.length };
|
||||
});
|
||||
|
||||
// W5.9: POST /api/memory/frames — direct memory write API
|
||||
// For bulk loading, pipeline output, testing. Bypasses agent loop.
|
||||
// F13: Optional entity extraction via ?extract=true (default: true)
|
||||
// POST /api/memory/frames — direct memory write (bulk loading, pipelines, tests).
|
||||
// An unknown workspace falls back to personal memory; identical text in the
|
||||
// same scope is not written twice.
|
||||
server.post<{
|
||||
Body: {
|
||||
content: string;
|
||||
content?: unknown;
|
||||
workspace?: string;
|
||||
workspaceId?: string;
|
||||
importance?: string;
|
||||
source?: string;
|
||||
};
|
||||
Querystring: {
|
||||
extract?: string;
|
||||
kind?: string;
|
||||
};
|
||||
}>('/api/memory/frames', async (request, reply) => {
|
||||
// P0-4: Accept both 'workspace' and 'workspaceId'
|
||||
const { content: rawContent, workspace: ws, workspaceId: wsId, importance, source } = request.body ?? {};
|
||||
const workspace = ws ?? wsId;
|
||||
const { content: rawContent, importance, source, kind } = request.body ?? {};
|
||||
if (typeof rawContent !== 'string' || !rawContent) {
|
||||
return reply.status(400).send({ error: 'content is required' });
|
||||
}
|
||||
// M4: Sanitize content to prevent stored XSS
|
||||
const content = sanitizeFrameContent(rawContent);
|
||||
// This is the exact full projection that can reach FrameStore, FTS, entity
|
||||
// extraction, and audit persistence. Reject it before any of those stores
|
||||
// (including the otherwise-created active session) can be mutated.
|
||||
const ingressDecision = evaluateExternalMemoryIngress({ content });
|
||||
if (ingressDecision.action !== 'allow') {
|
||||
const content = sanitizeFrameContent(rawContent).trim();
|
||||
if (!content || evaluateExternalMemoryIngress({ content }).action !== 'allow') {
|
||||
return reply.status(400).send({ error: 'Memory content could not be saved.' });
|
||||
}
|
||||
|
||||
const VALID_IMPORTANCE: readonly Importance[] = ['critical', 'important', 'normal', 'temporary', 'deprecated'];
|
||||
const imp: Importance = VALID_IMPORTANCE.includes(importance as Importance)
|
||||
? (importance as Importance)
|
||||
: 'normal';
|
||||
// D6: Validate source to prevent raw SQLite CHECK constraint errors
|
||||
const VALID_SOURCES: readonly FrameSource[] = ['user_stated', 'tool_verified', 'agent_inferred', 'import', 'system'];
|
||||
const src: FrameSource = (source ?? 'import') as FrameSource;
|
||||
if (!VALID_SOURCES.includes(src)) {
|
||||
const src = (source ?? 'import') as MemorySource;
|
||||
if (!MEMORY_SOURCES.includes(src)) {
|
||||
return reply.status(400).send({
|
||||
error: `Invalid source "${src}". Valid values: ${VALID_SOURCES.join(', ')}`,
|
||||
error: `Invalid source "${src}". Valid values: ${MEMORY_SOURCES.join(', ')}`,
|
||||
});
|
||||
}
|
||||
const imp = asImportance(importance) ?? 'normal';
|
||||
const scope = scopeForWorkspace(server, request.body?.workspace ?? request.body?.workspaceId);
|
||||
|
||||
// F13: determine whether to run entity extraction (default: true)
|
||||
const extractParam = request.query?.extract;
|
||||
const shouldExtract = extractParam !== 'false' && extractParam !== '0';
|
||||
|
||||
// Determine target mind
|
||||
let targetDb;
|
||||
let mindLabel: string = 'personal';
|
||||
if (workspace) {
|
||||
targetDb = server.agentState.getWorkspaceMindDb(workspace);
|
||||
mindLabel = 'workspace';
|
||||
}
|
||||
if (!targetDb) {
|
||||
targetDb = server.multiMind.personal;
|
||||
mindLabel = 'personal';
|
||||
}
|
||||
|
||||
const frames = new FrameStore(targetDb);
|
||||
const sessions = new SessionStore(targetDb);
|
||||
|
||||
// Get or create active session
|
||||
const active = sessions.getActive();
|
||||
let gopId: string;
|
||||
if (active.length === 0) {
|
||||
const session = sessions.create();
|
||||
gopId = session.gop_id;
|
||||
} else {
|
||||
gopId = active[0].gop_id;
|
||||
}
|
||||
|
||||
// BUG-R1-01: Dedup check at route level — catches both I-Frames and P-Frames
|
||||
const existingDup = frames.findDuplicate(content);
|
||||
if (existingDup) {
|
||||
const [change] = await server.memory.remember({
|
||||
scope,
|
||||
text: content,
|
||||
kind: asKind(kind) ?? 'fact',
|
||||
importance: imp,
|
||||
source: src,
|
||||
origin: 'api',
|
||||
});
|
||||
if (!change) return reply.status(500).send({ error: 'Memory could not be saved.' });
|
||||
const { record } = change;
|
||||
if (change.event === 'unchanged') {
|
||||
return {
|
||||
saved: false,
|
||||
duplicate: true,
|
||||
frameId: existingDup.id,
|
||||
mind: mindLabel,
|
||||
message: 'Identical content already exists. Access count updated.',
|
||||
frameId: record.id,
|
||||
mind: record.scope.kind,
|
||||
message: 'Identical content already exists.',
|
||||
};
|
||||
}
|
||||
|
||||
// Create frame
|
||||
const latestI = frames.getLatestIFrame(gopId);
|
||||
let frame;
|
||||
if (latestI) {
|
||||
frame = frames.createPFrame(gopId, content, latestI.id, imp, src);
|
||||
} else {
|
||||
frame = frames.createIFrame(gopId, content, imp, src);
|
||||
}
|
||||
|
||||
// F13: Run entity extraction and KG enrichment after storing the frame
|
||||
let extraction: { entitiesExtracted: number; relationsCreated: number } | undefined;
|
||||
if (shouldExtract) {
|
||||
try {
|
||||
const extracted = extractEntities(content);
|
||||
if (extracted.length > 0) {
|
||||
const knowledge = new KnowledgeGraph(targetDb);
|
||||
const entityIds: number[] = [];
|
||||
|
||||
// Upsert entities: skip if same type+name already exists
|
||||
for (const e of extracted) {
|
||||
const existing = knowledge.getEntitiesByType(e.type)
|
||||
.find(ent => ent.name.toLowerCase() === e.name.toLowerCase());
|
||||
if (existing) {
|
||||
entityIds.push(existing.id);
|
||||
} else {
|
||||
const created = knowledge.createEntity(e.type, e.name, {
|
||||
confidence: e.confidence,
|
||||
source: 'bulk-import',
|
||||
});
|
||||
entityIds.push(created.id);
|
||||
}
|
||||
}
|
||||
|
||||
// Create co-occurrence relations between entities found in same text
|
||||
let relCount = 0;
|
||||
for (let i = 0; i < entityIds.length; i++) {
|
||||
for (let j = i + 1; j < entityIds.length; j++) {
|
||||
const existingRels = knowledge.getRelationsFrom(entityIds[i], 'co_occurs_with');
|
||||
const alreadyExists = existingRels.some(r => r.target_id === entityIds[j]);
|
||||
if (!alreadyExists) {
|
||||
knowledge.createRelation(entityIds[i], entityIds[j], 'co_occurs_with', 0.8, {
|
||||
source: 'bulk-import',
|
||||
});
|
||||
relCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extraction = {
|
||||
entitiesExtracted: entityIds.length,
|
||||
relationsCreated: relCount,
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
// Entity extraction failure is non-blocking — frame is already saved
|
||||
}
|
||||
}
|
||||
|
||||
// F2: Audit trail — memory write
|
||||
emitAuditEvent(server, {
|
||||
workspaceId: workspace ?? 'personal',
|
||||
workspaceId: workspaceIdOf(record) ?? 'personal',
|
||||
eventType: 'memory_write',
|
||||
input: JSON.stringify({ content: content.slice(0, 500), importance: imp, source: src }),
|
||||
output: JSON.stringify({ frameId: frame.id, mind: mindLabel }),
|
||||
output: JSON.stringify({ frameId: record.id, mind: record.scope.kind }),
|
||||
});
|
||||
|
||||
return {
|
||||
saved: true,
|
||||
frameId: frame.id,
|
||||
mind: mindLabel,
|
||||
importance: imp,
|
||||
source: src,
|
||||
...(extraction ? { extraction } : {}),
|
||||
frameId: record.id,
|
||||
mind: record.scope.kind,
|
||||
kind: record.kind,
|
||||
importance: record.importance,
|
||||
source: record.source,
|
||||
};
|
||||
});
|
||||
|
||||
// F1: GET /api/memory/stats — dedicated memory statistics endpoint
|
||||
// GET /api/memory/stats?workspace=&scope=all-minds — memory counts.
|
||||
// scope=all-minds without a workspace adds up every workspace's count (counts
|
||||
// only, never content; valid because this loopback sidecar has one user).
|
||||
server.get<{
|
||||
Querystring: { workspace?: string; workspaceId?: string; scope?: string };
|
||||
}>('/api/memory/stats', async (request) => {
|
||||
// P0-4: Accept both 'workspace' and 'workspaceId'
|
||||
const workspaceId = request.query.workspace ?? request.query.workspaceId;
|
||||
const personalDb = server.multiMind.personal;
|
||||
const personalFrames = new FrameStore(personalDb);
|
||||
const personalCount = personalFrames.list({ limit: 100000 }).length;
|
||||
|
||||
let workspaceCount = 0;
|
||||
let workspaceEntities = 0;
|
||||
let workspaceRelations = 0;
|
||||
|
||||
// W2D: count entities/relations directly instead of listing them. The old
|
||||
// path used getEntitiesByType('') → getEntities(limit 500), capping the
|
||||
// entity count at 500/mind, then ran an N+1 getRelationsFrom loop over only
|
||||
// those ≤500 alphabetical entities — so the displayed totals were undercounts.
|
||||
const countRelations = (wsDb: NonNullable<ReturnType<typeof server.agentState.getWorkspaceMindDb>>): number => {
|
||||
const row = wsDb.getDatabase().prepare(
|
||||
'SELECT COUNT(*) as cnt FROM knowledge_relations WHERE valid_to IS NULL',
|
||||
).get() as { cnt: number };
|
||||
return row.cnt;
|
||||
};
|
||||
|
||||
const countMind = (wsDb: ReturnType<typeof server.agentState.getWorkspaceMindDb>) => {
|
||||
if (!wsDb) return;
|
||||
const wsFrames = new FrameStore(wsDb);
|
||||
workspaceCount += wsFrames.list({ limit: 100000 }).length;
|
||||
const wsKg = new KnowledgeGraph(wsDb);
|
||||
workspaceEntities += wsKg.getEntityCount();
|
||||
workspaceRelations += countRelations(wsDb);
|
||||
};
|
||||
|
||||
const personalCount = await server.memory.count({ scopes: [PERSONAL_SCOPE] });
|
||||
let workspaceScopes: MemoryScope[] = [];
|
||||
if (workspaceId) {
|
||||
countMind(server.agentState.getWorkspaceMindDb(workspaceId));
|
||||
const scope = scopeForWorkspace(server, workspaceId);
|
||||
if (scope.kind === 'workspace') workspaceScopes = [scope];
|
||||
} else if (request.query.scope === 'all-minds') {
|
||||
// === MIND ISOLATION (founder directive 2026-06-12) ===
|
||||
// Workspace minds are SEPARATE stores; no memory may leak between
|
||||
// users. This branch aggregates COUNTS ONLY (never content), is
|
||||
// explicit opt-in via ?scope=all-minds, and is valid ONLY on this
|
||||
// single-user loopback sidecar where every listed workspace belongs
|
||||
// to the local user. Do NOT port this aggregation to the multi-user
|
||||
// cloud server — there, totals must be scoped to the caller's ACL.
|
||||
try {
|
||||
for (const ws of server.workspaceManager.list()) {
|
||||
countMind(server.agentState.getWorkspaceMindDb(ws.id));
|
||||
}
|
||||
} catch { /* workspace enumeration unavailable — personal-only total */ }
|
||||
workspaceScopes = allScopes(server).filter((scope) => scope.kind === 'workspace');
|
||||
}
|
||||
|
||||
const personalKg = new KnowledgeGraph(personalDb);
|
||||
const personalEntityCount = personalKg.getEntityCount();
|
||||
const personalRelations = countRelations(personalDb);
|
||||
|
||||
const workspaceCount = workspaceScopes.length > 0
|
||||
? await server.memory.count({ scopes: workspaceScopes })
|
||||
: 0;
|
||||
return {
|
||||
personal: {
|
||||
frameCount: personalCount,
|
||||
entityCount: personalEntityCount,
|
||||
relationCount: personalRelations,
|
||||
},
|
||||
workspace: workspaceId ? {
|
||||
frameCount: workspaceCount,
|
||||
entityCount: workspaceEntities,
|
||||
relationCount: workspaceRelations,
|
||||
} : null,
|
||||
total: {
|
||||
frameCount: personalCount + workspaceCount,
|
||||
entityCount: personalEntityCount + workspaceEntities,
|
||||
relationCount: personalRelations + workspaceRelations,
|
||||
},
|
||||
personal: { frameCount: personalCount },
|
||||
workspace: workspaceId ? { frameCount: workspaceCount } : null,
|
||||
total: { frameCount: personalCount + workspaceCount },
|
||||
};
|
||||
});
|
||||
|
||||
// Q22: PUT /api/memory/frames/:id — edit a memory frame's content and/or importance
|
||||
// PUT /api/memory/frames/:id — edit a memory's text and/or importance.
|
||||
server.put<{
|
||||
Params: { id: string };
|
||||
Body: { content: string; importance?: string };
|
||||
Querystring: { workspace?: string; workspaceId?: string };
|
||||
Body: { content?: unknown; importance?: string };
|
||||
}>('/api/memory/frames/:id', async (request, reply) => {
|
||||
const frameId = parseInt(request.params.id, 10);
|
||||
if (isNaN(frameId)) {
|
||||
return reply.status(400).send({ error: 'Invalid frame ID' });
|
||||
}
|
||||
|
||||
const { content: rawContent, importance } = request.body ?? {};
|
||||
if (typeof rawContent !== 'string' || !rawContent) {
|
||||
return reply.status(400).send({ error: 'content is required' });
|
||||
}
|
||||
|
||||
// M4: Sanitize content to prevent stored XSS
|
||||
const content = sanitizeFrameContent(rawContent);
|
||||
if (evaluateExternalMemoryIngress({ content }).action !== 'allow') {
|
||||
const content = sanitizeFrameContent(rawContent).trim();
|
||||
if (!content || evaluateExternalMemoryIngress({ content }).action !== 'allow') {
|
||||
return reply.status(400).send({ error: 'Memory content could not be saved.' });
|
||||
}
|
||||
|
||||
// D6: Validate importance if provided
|
||||
const VALID_IMPORTANCE: readonly Importance[] = ['critical', 'important', 'normal', 'temporary', 'deprecated'];
|
||||
if (importance && !VALID_IMPORTANCE.includes(importance as Importance)) {
|
||||
return reply.status(400).send({
|
||||
error: `Invalid importance "${importance}". Valid values: ${VALID_IMPORTANCE.join(', ')}`,
|
||||
});
|
||||
}
|
||||
// After validation, `importance` is either undefined or a valid Importance.
|
||||
const imp: Importance | undefined = importance as Importance | undefined;
|
||||
|
||||
const workspace = request.query.workspace ?? request.query.workspaceId;
|
||||
|
||||
// Try workspace mind first, then personal
|
||||
let updated: Record<string, unknown> | undefined;
|
||||
let mindLabel = 'personal';
|
||||
if (workspace) {
|
||||
const wsDb = server.agentState.getWorkspaceMindDb(workspace);
|
||||
if (wsDb) {
|
||||
const wsFrames = new FrameStore(wsDb);
|
||||
const result = wsFrames.update(frameId, content, imp);
|
||||
if (result) {
|
||||
updated = result as unknown as Record<string, unknown>;
|
||||
mindLabel = 'workspace';
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!updated) {
|
||||
const personalFrames = new FrameStore(server.multiMind.personal);
|
||||
const result = personalFrames.update(frameId, content, imp);
|
||||
if (result) {
|
||||
updated = result as unknown as Record<string, unknown>;
|
||||
mindLabel = 'personal';
|
||||
}
|
||||
const imp = importance === undefined ? undefined : asImportance(importance);
|
||||
if (importance !== undefined && !imp) {
|
||||
return reply.status(400).send({ error: `Invalid importance "${importance}"` });
|
||||
}
|
||||
|
||||
if (!updated) {
|
||||
return reply.status(404).send({ error: 'Frame not found' });
|
||||
let updated: MemoryRecord;
|
||||
try {
|
||||
updated = await server.memory.update(request.params.id, { text: content, ...(imp && { importance: imp }) });
|
||||
} catch (err) {
|
||||
if (err instanceof MemoryNotFoundError) return reply.status(404).send({ error: 'Frame not found' });
|
||||
throw err;
|
||||
}
|
||||
|
||||
// F2: Audit trail — memory edit
|
||||
emitAuditEvent(server, {
|
||||
workspaceId: workspace ?? 'personal',
|
||||
workspaceId: workspaceIdOf(updated) ?? 'personal',
|
||||
eventType: 'memory_write',
|
||||
input: JSON.stringify({ frameId, content: content.slice(0, 500), importance }),
|
||||
output: JSON.stringify({ frameId, mind: mindLabel, action: 'edit' }),
|
||||
input: JSON.stringify({ frameId: updated.id, content: content.slice(0, 500), importance: imp }),
|
||||
output: JSON.stringify({ frameId: updated.id, mind: updated.scope.kind, action: 'edit' }),
|
||||
});
|
||||
|
||||
return { ...normalizeFrame({ ...updated, _mind: mindLabel }), updated: true };
|
||||
return { ...toFrameResponse(updated), updated: true };
|
||||
});
|
||||
|
||||
// C5: PATCH /api/memory/frames/:id/access — atomic access_count increment.
|
||||
// Replaces the previous PUT-with-local-count read-modify-write dance, which
|
||||
// had a race under concurrent access. SQL `access_count = access_count + 1`
|
||||
// is serialized by SQLite.
|
||||
server.patch<{
|
||||
Params: { id: string };
|
||||
Querystring: { workspace?: string; workspaceId?: string };
|
||||
}>('/api/memory/frames/:id/access', async (request, reply) => {
|
||||
const frameId = parseInt(request.params.id, 10);
|
||||
if (isNaN(frameId)) {
|
||||
return reply.status(400).send({ error: 'Invalid frame ID' });
|
||||
}
|
||||
|
||||
const workspace = request.query.workspace ?? request.query.workspaceId;
|
||||
|
||||
let newCount: number | undefined;
|
||||
let mindLabel = 'personal';
|
||||
if (workspace) {
|
||||
const wsDb = server.agentState.getWorkspaceMindDb(workspace);
|
||||
if (wsDb) {
|
||||
newCount = new FrameStore(wsDb).touch(frameId);
|
||||
if (newCount !== undefined) mindLabel = 'workspace';
|
||||
}
|
||||
}
|
||||
if (newCount === undefined) {
|
||||
newCount = new FrameStore(server.multiMind.personal).touch(frameId);
|
||||
}
|
||||
|
||||
if (newCount === undefined) {
|
||||
return reply.status(404).send({ error: 'Frame not found' });
|
||||
}
|
||||
|
||||
return { frameId, accessed: true, accessCount: newCount, mind: mindLabel };
|
||||
});
|
||||
|
||||
// L2: DELETE /api/memory/frames/:id — delete a memory frame by ID
|
||||
server.delete<{
|
||||
Params: { id: string };
|
||||
Querystring: { workspace?: string; workspaceId?: string };
|
||||
}>('/api/memory/frames/:id', async (request, reply) => {
|
||||
const frameId = parseInt(request.params.id, 10);
|
||||
if (isNaN(frameId)) {
|
||||
return reply.status(400).send({ error: 'Invalid frame ID' });
|
||||
}
|
||||
|
||||
// BUG-R1-02: Accept both 'workspace' and 'workspaceId' for workspace-specific deletion
|
||||
const workspace = request.query.workspace ?? request.query.workspaceId;
|
||||
|
||||
// Try workspace mind first, then personal
|
||||
let deleted = false;
|
||||
if (workspace) {
|
||||
const wsDb = server.agentState.getWorkspaceMindDb(workspace);
|
||||
if (wsDb) {
|
||||
const wsFrames = new FrameStore(wsDb);
|
||||
deleted = wsFrames.delete(frameId);
|
||||
}
|
||||
}
|
||||
if (!deleted) {
|
||||
const personalFrames = new FrameStore(server.multiMind.personal);
|
||||
deleted = personalFrames.delete(frameId);
|
||||
}
|
||||
|
||||
if (!deleted) {
|
||||
// DELETE /api/memory/frames/:id — forget a memory.
|
||||
server.delete<{ Params: { id: string } }>('/api/memory/frames/:id', async (request, reply) => {
|
||||
const record = await server.memory.get(request.params.id);
|
||||
if (!record || !(await server.memory.forget(record.id))) {
|
||||
return reply.status(404).send({ error: 'Frame not found' });
|
||||
}
|
||||
// F2: Audit trail — memory delete
|
||||
emitAuditEvent(server, {
|
||||
workspaceId: workspace ?? 'personal',
|
||||
workspaceId: workspaceIdOf(record) ?? 'personal',
|
||||
eventType: 'memory_delete',
|
||||
input: JSON.stringify({ frameId }),
|
||||
input: JSON.stringify({ frameId: record.id }),
|
||||
});
|
||||
return reply.status(200).send({ deleted: true, frameId });
|
||||
return reply.status(200).send({ deleted: true, frameId: record.id });
|
||||
});
|
||||
|
||||
// UX-Refactor Phase 1 (S01): POST /api/quick-capture — Home Cockpit quick
|
||||
// capture. Writes a memory frame (the durable record) and, for task-type
|
||||
// captures, an awareness `task` row so the capture surfaces in the workspace
|
||||
// state's active/pending lanes. Returns `{ frameId }` per `_phase1-contract.md`
|
||||
// §3. Reuses the FrameStore/SessionStore frame-write path above (no new store).
|
||||
// capture. Writes a note memory (the durable record) and, for task captures,
|
||||
// an awareness `task` row so the capture shows in the workspace state's
|
||||
// active/pending lanes.
|
||||
server.post<{
|
||||
Body: { kind?: string; content?: string; workspaceId?: string };
|
||||
}>('/api/quick-capture', async (request, reply) => {
|
||||
@@ -764,59 +445,50 @@ export const memoryRoutes: FastifyPluginAsync = async (server) => {
|
||||
: 'note';
|
||||
|
||||
// M4: Sanitize content to prevent stored XSS (same path as /memory/frames).
|
||||
const content = sanitizeFrameContent(rawContent.trim());
|
||||
if (evaluateExternalMemoryIngress({ content }).action !== 'allow') {
|
||||
const content = sanitizeFrameContent(rawContent.trim()).trim();
|
||||
if (!content || evaluateExternalMemoryIngress({ content }).action !== 'allow') {
|
||||
return reply.status(400).send({ error: 'Memory content could not be saved.' });
|
||||
}
|
||||
|
||||
// Resolve the target mind — workspace when provided + open, else personal.
|
||||
let targetDb;
|
||||
let mindLabel: string = 'personal';
|
||||
if (workspaceId) {
|
||||
targetDb = server.agentState.getWorkspaceMindDb(workspaceId);
|
||||
if (targetDb) mindLabel = 'workspace';
|
||||
}
|
||||
if (!targetDb) {
|
||||
targetDb = server.multiMind.personal;
|
||||
mindLabel = 'personal';
|
||||
}
|
||||
|
||||
const frames = new FrameStore(targetDb);
|
||||
const sessions = new SessionStore(targetDb);
|
||||
const active = sessions.getActive();
|
||||
const gopId = active.length > 0 ? active[0].gop_id : sessions.create().gop_id;
|
||||
|
||||
// Provenance is user_stated — quick capture is the user speaking directly.
|
||||
const latestI = frames.getLatestIFrame(gopId);
|
||||
const frame = latestI
|
||||
? frames.createPFrame(gopId, content, latestI.id, 'normal', 'user_stated')
|
||||
: frames.createIFrame(gopId, content, 'normal', 'user_stated');
|
||||
// Workspace memory when the workspace exists, else personal memory.
|
||||
// Provenance is user_stated: quick capture is the user speaking directly.
|
||||
const scope = scopeForWorkspace(server, workspaceId);
|
||||
const [change] = await server.memory.remember({
|
||||
scope,
|
||||
text: content,
|
||||
kind: 'note',
|
||||
source: 'user_stated',
|
||||
origin: 'quick-capture',
|
||||
metadata: { captureKind: kind },
|
||||
});
|
||||
if (!change) return reply.status(500).send({ error: 'Memory could not be saved.' });
|
||||
const { record } = change;
|
||||
|
||||
// Task captures additionally seed an awareness `task` row so they appear in
|
||||
// the workspace-state active/pending lanes (read by S01/S02 builders).
|
||||
let awarenessId: number | undefined;
|
||||
if (kind === 'task') {
|
||||
try {
|
||||
const awarenessScope = mindLabel === 'workspace' && workspaceId ? workspaceDataScope(workspaceId) : 'personal';
|
||||
const awarenessScope = scope.kind === 'workspace' ? workspaceDataScope(scope.workspaceId) : 'personal';
|
||||
awarenessId = new AwarenessLayer(server.waggleDb, awarenessScope).add('task', content, 0, undefined, {
|
||||
context: 'quick-capture',
|
||||
status: 'open',
|
||||
}).id;
|
||||
} catch {
|
||||
// Non-blocking — the frame is already saved; awareness is best-effort.
|
||||
// Non-blocking — the memory is already saved; awareness is best-effort.
|
||||
}
|
||||
}
|
||||
|
||||
emitAuditEvent(server, {
|
||||
workspaceId: workspaceId ?? 'personal',
|
||||
workspaceId: workspaceIdOf(record) ?? 'personal',
|
||||
eventType: 'memory_write',
|
||||
input: JSON.stringify({ kind, content: content.slice(0, 500), source: 'quick-capture' }),
|
||||
output: JSON.stringify({ frameId: frame.id, mind: mindLabel }),
|
||||
output: JSON.stringify({ frameId: record.id, mind: record.scope.kind }),
|
||||
});
|
||||
|
||||
return {
|
||||
frameId: String(frame.id),
|
||||
mind: mindLabel,
|
||||
frameId: record.id,
|
||||
mind: record.scope.kind,
|
||||
kind,
|
||||
...(awarenessId !== undefined ? { awarenessId } : {}),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user