Files
waggle-os/packages/server/src/local/memory-records.ts
shumeiko.it 8a9ba09ad9 fix(server): refuse memory writes for a workspace that does not exist (UF-039)
POST /api/memory/frames, /api/quick-capture and the Memory Center's
POST /api/memory saved a memory meant for an unknown workspace into
personal memory, which every workspace reads. They now answer 404
workspace_not_found and write nothing; a request without a workspace
still goes to personal memory.
2026-10-24 18:09:43 +02:00

94 lines
3.7 KiB
TypeScript

/**
* 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';
/**
* Where a write goes: personal memory when no workspace is named, the
* workspace's memory when it exists, and null for a workspace that does not
* exist. Callers refuse that write instead of saving into personal memory,
* which every workspace reads (UF-039).
*/
export function writeScopeForWorkspace(server: FastifyInstance, workspaceId: string | undefined): MemoryScope | null {
if (!workspaceId) return PERSONAL_SCOPE;
return server.workspaceManager.get(workspaceId) ? workspaceScope(workspaceId) : null;
}
/** 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;
}