refactor(server): read workspace state, briefings and workspaces from the memory contract (P1.5)
- Workspace state, the "Workspace Now" block, the home briefing and the proactive handlers read the workspace's memory through server.memory: decisions are memories of kind 'decision', critical ones or ones worded as decisions; summaries list memories most important first. They no longer open workspace .mind files, so the builders are async and ProactiveContext carries `memory` instead of `getWorkspaceMindDb`. - The home briefing counts extracted personal facts that are neither confirmed nor archived as awaiting review (memory-status.ts). - Workspace routes count memories through the contract, seed template starter memories with remember() in the background (origin 'workspace-template'), export memories through list(), and deleting a workspace forgets its memory scope and clears its awareness items before the directory is removed; the delete is refused if forgetting fails. agentState.releaseWorkspace() replaces closeWorkspaceMind for callers. - The command palette searches personal memory and the active workspace's memory (UF-033). - chat.ts drops the MindDB-flake diagnostics; the mind cache they guarded no longer serves chat turns.
This commit is contained in:
@@ -5,7 +5,9 @@
|
||||
* - PATCH/PUT accept `status` (archive/restore/pause) and `description`,
|
||||
* persist via WorkspaceManager, and reject invalid status values with 400.
|
||||
* - Rename round-trips.
|
||||
* - DELETE closes the workspace mind before removing the directory.
|
||||
* - DELETE forgets the workspace's memories and awareness items, releases
|
||||
* the workspace, then removes the directory.
|
||||
* - POST with a template seeds its starter memories in the background.
|
||||
*
|
||||
* Scaffolding follows home.test.ts: Fastify inject + plain-object decorators,
|
||||
* but with a REAL WorkspaceManager on a tmp dir so persistence is verified.
|
||||
@@ -17,7 +19,13 @@ import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import { WorkspaceManager } from '@waggle/core';
|
||||
import { AwarenessLayer, WaggleDB, WorkspaceManager, workspaceDataScope } from '@waggle/core';
|
||||
import {
|
||||
createInMemoryMemoryStore,
|
||||
PERSONAL_SCOPE,
|
||||
workspaceScope,
|
||||
type MemoryStore,
|
||||
} from '@waggle/memory-contract';
|
||||
import { workspaceRoutes, toStateItemViews } from '../../src/local/routes/workspaces.js';
|
||||
import type { StateItem } from '../../src/local/workspace-state.js';
|
||||
|
||||
@@ -25,7 +33,9 @@ describe('workspace lifecycle routes', () => {
|
||||
let tmpDir: string;
|
||||
let manager: WorkspaceManager;
|
||||
let server: FastifyInstance;
|
||||
let closeWorkspaceMind: ReturnType<typeof vi.fn>;
|
||||
let releaseWorkspace: ReturnType<typeof vi.fn>;
|
||||
let memory: MemoryStore;
|
||||
let waggleDb: WaggleDB;
|
||||
let wsId: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
@@ -33,12 +43,16 @@ describe('workspace lifecycle routes', () => {
|
||||
manager = new WorkspaceManager(tmpDir);
|
||||
wsId = manager.create({ name: 'Side Project', group: 'Personal' }).id;
|
||||
|
||||
closeWorkspaceMind = vi.fn();
|
||||
releaseWorkspace = vi.fn();
|
||||
memory = createInMemoryMemoryStore();
|
||||
waggleDb = new WaggleDB(':memory:');
|
||||
server = Fastify({ logger: false });
|
||||
server.decorate('workspaceManager', manager);
|
||||
server.decorate('memory', memory);
|
||||
server.decorate('waggleDb', waggleDb);
|
||||
server.decorate('agentState', {
|
||||
activateWorkspaceMind: () => undefined,
|
||||
closeWorkspaceMind,
|
||||
releaseWorkspace,
|
||||
});
|
||||
// localConfig intentionally absent — see file header.
|
||||
await server.register(workspaceRoutes);
|
||||
@@ -46,6 +60,7 @@ describe('workspace lifecycle routes', () => {
|
||||
|
||||
afterEach(async () => {
|
||||
await server.close();
|
||||
waggleDb.close();
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
@@ -172,14 +187,52 @@ describe('workspace lifecycle routes', () => {
|
||||
expect(new Set(ids).size).toBe(ids.length);
|
||||
});
|
||||
|
||||
it('DELETE closes the workspace mind, removes the directory, and 404s after', async () => {
|
||||
it('DELETE forgets the workspace memory, releases it, removes the directory, and 404s after', async () => {
|
||||
const otherId = manager.create({ name: 'Other Project', group: 'Personal' }).id;
|
||||
await memory.remember({ scope: workspaceScope(wsId), text: 'Side project memory' });
|
||||
await memory.remember({ scope: workspaceScope(otherId), text: 'Other project memory' });
|
||||
await memory.remember({ scope: PERSONAL_SCOPE, text: 'Personal memory' });
|
||||
new AwarenessLayer(waggleDb, workspaceDataScope(wsId)).add('task', 'Side project task');
|
||||
new AwarenessLayer(waggleDb, workspaceDataScope(otherId)).add('task', 'Other project task');
|
||||
|
||||
const res = await server.inject({ method: 'DELETE', url: `/api/workspaces/${wsId}` });
|
||||
expect(res.statusCode).toBe(204);
|
||||
expect(closeWorkspaceMind).toHaveBeenCalledWith(wsId);
|
||||
expect(releaseWorkspace).toHaveBeenCalledWith(wsId);
|
||||
expect(await memory.count({ scopes: [workspaceScope(wsId)] })).toBe(0);
|
||||
expect(await memory.count({ scopes: [workspaceScope(otherId), PERSONAL_SCOPE] })).toBe(2);
|
||||
expect(new AwarenessLayer(waggleDb, workspaceDataScope(wsId)).getAll()).toEqual([]);
|
||||
expect(new AwarenessLayer(waggleDb, workspaceDataScope(otherId)).getAll()).toHaveLength(1);
|
||||
expect(manager.get(wsId)).toBeNull();
|
||||
expect(fs.existsSync(path.join(tmpDir, 'workspaces', wsId))).toBe(false);
|
||||
|
||||
const after = await server.inject({ method: 'DELETE', url: `/api/workspaces/${wsId}` });
|
||||
expect(after.statusCode).toBe(404);
|
||||
});
|
||||
|
||||
it('DELETE keeps the workspace when its memory cannot be forgotten', async () => {
|
||||
vi.spyOn(memory, 'forgetMatching').mockRejectedValueOnce(new Error('store unavailable'));
|
||||
|
||||
const res = await server.inject({ method: 'DELETE', url: `/api/workspaces/${wsId}` });
|
||||
|
||||
expect(res.statusCode).toBe(500);
|
||||
expect(releaseWorkspace).not.toHaveBeenCalled();
|
||||
expect(manager.get(wsId)).not.toBeNull();
|
||||
});
|
||||
|
||||
it('POST with a template seeds its starter memories into the new workspace', async () => {
|
||||
const res = await server.inject({
|
||||
method: 'POST',
|
||||
url: '/api/workspaces',
|
||||
payload: { name: 'Research Desk', group: 'Work', templateId: 'research-project' },
|
||||
});
|
||||
expect(res.statusCode).toBe(201);
|
||||
const id = res.json().id as string;
|
||||
|
||||
await vi.waitFor(async () => {
|
||||
expect(await memory.count({ scopes: [workspaceScope(id)] })).toBe(3);
|
||||
});
|
||||
const seeded = await memory.list({ scopes: [workspaceScope(id)] });
|
||||
expect(seeded.every((m) => m.origin === 'workspace-template' && m.source === 'system')).toBe(true);
|
||||
expect(seeded.map((m) => m.text)).toContain('This workspace is dedicated to research and investigation.');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user