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:
@@ -2,7 +2,8 @@ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import * as os from 'node:os';
|
||||
import { MindDB, WaggleDB, WorkspaceManager, AwarenessLayer, workspaceDataScope } from '@waggle/core';
|
||||
import { WaggleDB, WorkspaceManager, AwarenessLayer, workspaceDataScope } from '@waggle/core';
|
||||
import { createInMemoryMemoryStore, workspaceScope, type MemoryStore } from '@waggle/memory-contract';
|
||||
import {
|
||||
generateMorningBriefing,
|
||||
checkStaleWorkspaces,
|
||||
@@ -15,7 +16,7 @@ describe('Proactive Handlers', () => {
|
||||
let tmpDir: string;
|
||||
let wsManager: WorkspaceManager;
|
||||
let waggleDb: WaggleDB;
|
||||
const mindDbs = new Map<string, MindDB>();
|
||||
let memory: MemoryStore;
|
||||
|
||||
/** Awareness items of one workspace, kept in waggle.db under its scope. */
|
||||
function workspaceAwareness(workspaceId: string): AwarenessLayer {
|
||||
@@ -27,19 +28,7 @@ describe('Proactive Handlers', () => {
|
||||
dataDir: tmpDir,
|
||||
workspaceManager: wsManager,
|
||||
waggleDb,
|
||||
getWorkspaceMindDb: (workspaceId: string) => {
|
||||
const existing = mindDbs.get(workspaceId);
|
||||
if (existing) return existing;
|
||||
const mindPath = path.join(tmpDir, 'workspaces', workspaceId, 'workspace.mind');
|
||||
if (!fs.existsSync(mindPath)) return null;
|
||||
try {
|
||||
const db = new MindDB(mindPath);
|
||||
mindDbs.set(workspaceId, db);
|
||||
return db;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
},
|
||||
memory,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -47,13 +36,10 @@ describe('Proactive Handlers', () => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-proactive-'));
|
||||
wsManager = new WorkspaceManager(tmpDir);
|
||||
waggleDb = WaggleDB.openInDataDir(tmpDir);
|
||||
memory = createInMemoryMemoryStore();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
for (const [, db] of mindDbs) {
|
||||
try { db.close(); } catch { /* already closed */ }
|
||||
}
|
||||
mindDbs.clear();
|
||||
waggleDb.close();
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
});
|
||||
@@ -61,13 +47,13 @@ describe('Proactive Handlers', () => {
|
||||
// ── Morning Briefing ─────────────────────────────────────────────
|
||||
|
||||
describe('generateMorningBriefing', () => {
|
||||
it('returns null when no workspaces exist', () => {
|
||||
it('returns null when no workspaces exist', async () => {
|
||||
const ctx = makeContext();
|
||||
const result = generateMorningBriefing(ctx);
|
||||
const result = await generateMorningBriefing(ctx);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null when no pending items and no stale workspaces', () => {
|
||||
it('returns null when no pending items and no stale workspaces', async () => {
|
||||
// Create a workspace with a recent session file
|
||||
const ws = wsManager.create({ name: 'Active Project', group: 'Work' });
|
||||
const sessionsDir = path.join(tmpDir, 'workspaces', ws.id, 'sessions');
|
||||
@@ -75,11 +61,11 @@ describe('Proactive Handlers', () => {
|
||||
fs.writeFileSync(path.join(sessionsDir, '2026-03-19.jsonl'), '{"role":"user","content":"hello"}\n');
|
||||
|
||||
const ctx = makeContext();
|
||||
const result = generateMorningBriefing(ctx);
|
||||
const result = await generateMorningBriefing(ctx);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('includes workspace summaries when pending items exist', () => {
|
||||
it('includes workspace summaries when pending items exist', async () => {
|
||||
const ws = wsManager.create({ name: 'My Research', group: 'Work' });
|
||||
|
||||
const awareness = workspaceAwareness(ws.id);
|
||||
@@ -87,7 +73,7 @@ describe('Proactive Handlers', () => {
|
||||
awareness.add('pending', 'Waiting for feedback', 0);
|
||||
|
||||
const ctx = makeContext();
|
||||
const result = generateMorningBriefing(ctx);
|
||||
const result = await generateMorningBriefing(ctx);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.type).toBe('morning_briefing');
|
||||
@@ -96,7 +82,7 @@ describe('Proactive Handlers', () => {
|
||||
expect(result!.body).toContain('My Research');
|
||||
});
|
||||
|
||||
it('reports stale workspaces in briefing', () => {
|
||||
it('reports stale workspaces in briefing', async () => {
|
||||
const ws = wsManager.create({ name: 'Old Project', group: 'Archive' });
|
||||
|
||||
// Create a session file with old modification time
|
||||
@@ -109,23 +95,36 @@ describe('Proactive Handlers', () => {
|
||||
fs.utimesSync(sessionFile, oldTime, oldTime);
|
||||
|
||||
const ctx = makeContext();
|
||||
const result = generateMorningBriefing(ctx);
|
||||
const result = await generateMorningBriefing(ctx);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.body).toContain('not visited in 14+ days');
|
||||
});
|
||||
|
||||
it("lists this week's decisions per workspace", async () => {
|
||||
const ws = wsManager.create({ name: 'Decisive Project', group: 'Work' });
|
||||
await memory.remember({ scope: workspaceScope(ws.id), text: 'Ship the beta on Monday', kind: 'decision' });
|
||||
await memory.remember({ scope: workspaceScope(ws.id), text: 'Lunch notes from the offsite' });
|
||||
|
||||
const result = await generateMorningBriefing(makeContext());
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.body).toContain('Recent decisions');
|
||||
expect(result!.body).toContain('**Decisive Project**: Ship the beta on Monday');
|
||||
expect(result!.body).not.toContain('Lunch notes');
|
||||
});
|
||||
});
|
||||
|
||||
// ── Stale Workspace Check ────────────────────────────────────────
|
||||
|
||||
describe('checkStaleWorkspaces', () => {
|
||||
it('returns empty array when no workspaces', () => {
|
||||
it('returns empty array when no workspaces', async () => {
|
||||
const ctx = makeContext();
|
||||
const result = checkStaleWorkspaces(ctx);
|
||||
const result = await checkStaleWorkspaces(ctx);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('detects stale workspaces based on session activity', () => {
|
||||
it('detects stale workspaces based on session activity', async () => {
|
||||
const ws = wsManager.create({ name: 'Forgotten Project', group: 'Old' });
|
||||
|
||||
// Create a session file with old modification time
|
||||
@@ -137,7 +136,7 @@ describe('Proactive Handlers', () => {
|
||||
fs.utimesSync(sessionFile, oldTime, oldTime);
|
||||
|
||||
const ctx = makeContext();
|
||||
const result = checkStaleWorkspaces(ctx);
|
||||
const result = await checkStaleWorkspaces(ctx);
|
||||
|
||||
expect(result.length).toBe(1);
|
||||
expect(result[0].type).toBe('stale_workspace');
|
||||
@@ -145,18 +144,18 @@ describe('Proactive Handlers', () => {
|
||||
expect(result[0].workspaceId).toBe(ws.id);
|
||||
});
|
||||
|
||||
it('skips recently active workspaces', () => {
|
||||
it('skips recently active workspaces', async () => {
|
||||
const ws = wsManager.create({ name: 'Active Project', group: 'Work' });
|
||||
const sessionsDir = path.join(tmpDir, 'workspaces', ws.id, 'sessions');
|
||||
fs.mkdirSync(sessionsDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(sessionsDir, '2026-03-19.jsonl'), '{"role":"user","content":"hello"}\n');
|
||||
|
||||
const ctx = makeContext();
|
||||
const result = checkStaleWorkspaces(ctx);
|
||||
const result = await checkStaleWorkspaces(ctx);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('uses creation date when no sessions exist', () => {
|
||||
it('uses creation date when no sessions exist', async () => {
|
||||
// Create workspace with backdated creation
|
||||
const ws = wsManager.create({ name: 'Empty Old Workspace', group: 'Test' });
|
||||
// Backdate the workspace config
|
||||
@@ -166,11 +165,25 @@ describe('Proactive Handlers', () => {
|
||||
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
||||
|
||||
const ctx = makeContext();
|
||||
const result = checkStaleWorkspaces(ctx);
|
||||
const result = await checkStaleWorkspaces(ctx);
|
||||
|
||||
expect(result.length).toBe(1);
|
||||
expect(result[0].title).toContain('Empty Old Workspace');
|
||||
});
|
||||
|
||||
it('counts the memories of a stale workspace', async () => {
|
||||
const ws = wsManager.create({ name: 'Quiet Workspace', group: 'Test' });
|
||||
const configPath = path.join(tmpDir, 'workspaces', ws.id, 'workspace.json');
|
||||
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
||||
config.created = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString();
|
||||
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
||||
await memory.remember({ scope: workspaceScope(ws.id), text: 'First memory' });
|
||||
await memory.remember({ scope: workspaceScope(ws.id), text: 'Second memory' });
|
||||
|
||||
const result = await checkStaleWorkspaces(makeContext());
|
||||
|
||||
expect(result[0].body).toBe('This workspace has 2 memories that may need attention.');
|
||||
});
|
||||
});
|
||||
|
||||
// ── Task Reminder ────────────────────────────────────────────────
|
||||
@@ -233,17 +246,17 @@ describe('Proactive Handlers', () => {
|
||||
// ── Capability Suggestion ────────────────────────────────────────
|
||||
|
||||
describe('suggestCapabilities', () => {
|
||||
it('returns null when no workspaces and capabilities installed', () => {
|
||||
it('returns null when no workspaces and capabilities installed', async () => {
|
||||
// Create a fake skills directory to simulate installed capabilities
|
||||
fs.mkdirSync(path.join(tmpDir, 'skills', 'some-skill'), { recursive: true });
|
||||
const ctx = makeContext();
|
||||
const result = suggestCapabilities(ctx);
|
||||
const result = await suggestCapabilities(ctx);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('suggests capability packs when none are installed', () => {
|
||||
it('suggests capability packs when none are installed', async () => {
|
||||
const ctx = makeContext();
|
||||
const result = suggestCapabilities(ctx);
|
||||
const result = await suggestCapabilities(ctx);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.type).toBe('capability_suggestion');
|
||||
@@ -251,17 +264,15 @@ describe('Proactive Handlers', () => {
|
||||
expect(result!.actionUrl).toBe('/skills');
|
||||
});
|
||||
|
||||
it('returns null when capabilities installed and low memory count', () => {
|
||||
it('returns null when capabilities installed and low memory count', async () => {
|
||||
// Create skills dir (simulates installed capabilities)
|
||||
fs.mkdirSync(path.join(tmpDir, 'skills', 'research-skill'), { recursive: true });
|
||||
// Create a workspace with few frames
|
||||
// Create a workspace with few memories
|
||||
const ws = wsManager.create({ name: 'Small Project', group: 'Test' });
|
||||
const mindPath = path.join(tmpDir, 'workspaces', ws.id, 'workspace.mind');
|
||||
const db = new MindDB(mindPath);
|
||||
mindDbs.set(ws.id, db);
|
||||
await memory.remember({ scope: workspaceScope(ws.id), text: 'One small memory' });
|
||||
|
||||
const ctx = makeContext();
|
||||
const result = suggestCapabilities(ctx);
|
||||
const result = await suggestCapabilities(ctx);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user