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.
This commit is contained in:
shumeiko.it
2026-10-24 18:09:43 +02:00
parent 4dbab2cb8b
commit 8a9ba09ad9
6 changed files with 61 additions and 10 deletions

View File

@@ -26,6 +26,17 @@ 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)

View File

@@ -23,6 +23,7 @@ import {
memoryCreatedAt,
memoryLifecycle,
workspaceIdOf,
writeScopeForWorkspace,
} from '../memory-records.js';
/**
@@ -223,7 +224,10 @@ export const memoryCenterRoutes: FastifyPluginAsync = async (server) => {
return reply.status(400).send({ error: 'Memory content could not be saved.' });
}
const workspace = b.workspace ?? b.workspaceId;
const scope = workspace && server.workspaceManager.get(workspace) ? workspaceScope(workspace) : PERSONAL_SCOPE;
const scope = writeScopeForWorkspace(server, workspace);
if (!scope) {
return reply.status(404).send({ error: 'workspace_not_found', message: `Workspace ${workspace} does not exist` });
}
const [change] = await server.memory.remember({
scope,

View File

@@ -26,9 +26,15 @@ import {
memoryLifecycle,
scopeForWorkspace,
workspaceIdOf,
writeScopeForWorkspace,
type MemoryLifecycle,
} from '../memory-records.js';
const workspaceNotFound = (workspaceId: string) => ({
error: 'workspace_not_found',
message: `Workspace ${workspaceId} does not exist`,
});
// ── UX-Refactor Phase 1 (S01) quick-capture kinds ─────────────────────────
type QuickCaptureKind = 'note' | 'task' | 'link' | 'file';
const QUICK_CAPTURE_KINDS: readonly QuickCaptureKind[] = ['note', 'task', 'link', 'file'];
@@ -311,7 +317,9 @@ export const memoryRoutes: FastifyPluginAsync = async (server) => {
});
}
const imp = asImportance(importance) ?? 'normal';
const scope = scopeForWorkspace(server, request.body?.workspace ?? request.body?.workspaceId);
const workspaceId = request.body?.workspace ?? request.body?.workspaceId;
const scope = writeScopeForWorkspace(server, workspaceId);
if (!scope) return reply.status(404).send(workspaceNotFound(workspaceId!));
const [change] = await server.memory.remember({
scope,
@@ -450,9 +458,10 @@ export const memoryRoutes: FastifyPluginAsync = async (server) => {
return reply.status(400).send({ error: 'Memory content could not be saved.' });
}
// Workspace memory when the workspace exists, else personal memory.
// Workspace memory, or personal memory when no workspace is named.
// Provenance is user_stated: quick capture is the user speaking directly.
const scope = scopeForWorkspace(server, workspaceId);
const scope = writeScopeForWorkspace(server, workspaceId);
if (!scope) return reply.status(404).send(workspaceNotFound(workspaceId!));
const [change] = await server.memory.remember({
scope,
text: content,