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:
@@ -1,22 +1,32 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import os from 'node:os';
|
||||
import { AwarenessLayer, MindDB, WaggleDB, workspaceDataScope, type AwarenessCategory } from '@waggle/core';
|
||||
import { AwarenessLayer, WaggleDB, workspaceDataScope, type AwarenessCategory } from '@waggle/core';
|
||||
import {
|
||||
createInMemoryMemoryStore,
|
||||
workspaceScope,
|
||||
type MemoryImportance,
|
||||
type MemoryKind,
|
||||
type MemoryStore,
|
||||
} from '@waggle/memory-contract';
|
||||
import {
|
||||
buildWorkspaceState,
|
||||
formatWorkspaceStatePrompt,
|
||||
computeFreshness,
|
||||
listByImportance,
|
||||
type WorkspaceState,
|
||||
} from '../../src/local/workspace-state.js';
|
||||
|
||||
describe('workspace-state', () => {
|
||||
let tmpDir: string;
|
||||
let waggleDb: WaggleDB;
|
||||
let memory: MemoryStore;
|
||||
|
||||
beforeEach(() => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ws-state-'));
|
||||
waggleDb = WaggleDB.openInDataDir(tmpDir);
|
||||
memory = createInMemoryMemoryStore();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -24,8 +34,8 @@ describe('workspace-state', () => {
|
||||
try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* */ }
|
||||
});
|
||||
|
||||
function setupWorkspace(id: string, opts?: {
|
||||
frames?: Array<{ content: string; importance: string; created_at?: string }>;
|
||||
async function setupWorkspace(id: string, opts?: {
|
||||
memories?: Array<{ text: string; importance: MemoryImportance; kind?: MemoryKind }>;
|
||||
sessions?: Array<{
|
||||
title: string;
|
||||
messages: Array<{ role: string; content: string }>;
|
||||
@@ -36,24 +46,13 @@ describe('workspace-state', () => {
|
||||
const wsDir = path.join(tmpDir, 'workspaces', id);
|
||||
fs.mkdirSync(wsDir, { recursive: true });
|
||||
|
||||
const mindPath = path.join(wsDir, 'workspace.mind');
|
||||
if (opts?.frames && opts.frames.length > 0) {
|
||||
const db = new MindDB(mindPath);
|
||||
const raw = db.getDatabase();
|
||||
raw.prepare(
|
||||
`INSERT INTO sessions (gop_id, status, started_at)
|
||||
VALUES ('session:test', 'active', datetime('now'))`,
|
||||
).run();
|
||||
|
||||
for (const frame of opts.frames) {
|
||||
const createdAt = frame.created_at ?? new Date().toISOString();
|
||||
raw.prepare(
|
||||
`INSERT INTO memory_frames (gop_id, frame_type, content, importance, created_at)
|
||||
VALUES ('session:test', 'I', ?, ?, ?)`,
|
||||
).run(frame.content, frame.importance, createdAt);
|
||||
}
|
||||
|
||||
db.close();
|
||||
for (const memo of opts?.memories ?? []) {
|
||||
await memory.remember({
|
||||
scope: workspaceScope(id),
|
||||
text: memo.text,
|
||||
importance: memo.importance,
|
||||
...(memo.kind ? { kind: memo.kind } : {}),
|
||||
});
|
||||
}
|
||||
|
||||
// Awareness items live in waggle.db under the workspace's scope, not in the mind.
|
||||
@@ -87,18 +86,15 @@ describe('workspace-state', () => {
|
||||
}
|
||||
}
|
||||
|
||||
return { mindPath, wsDir };
|
||||
return { wsDir };
|
||||
}
|
||||
|
||||
function makeManager(workspaces: Map<string, { id: string; name: string }>) {
|
||||
return {
|
||||
get: (id: string) => workspaces.get(id) ?? null,
|
||||
getMindPath: (id: string) => path.join(tmpDir, 'workspaces', id, 'workspace.mind'),
|
||||
};
|
||||
}
|
||||
|
||||
const noopActivate = (_id: string) => true;
|
||||
|
||||
// ── computeFreshness ──────────────────────────────────────────
|
||||
|
||||
describe('computeFreshness', () => {
|
||||
@@ -122,76 +118,104 @@ describe('workspace-state', () => {
|
||||
});
|
||||
});
|
||||
|
||||
// ── listByImportance ──────────────────────────────────────────
|
||||
|
||||
describe('listByImportance', () => {
|
||||
it('lists the most important memories first and skips temporary ones', async () => {
|
||||
const scope = workspaceScope('ws');
|
||||
const seed: Array<[string, MemoryImportance]> = [
|
||||
['normal one', 'normal'],
|
||||
['critical one', 'critical'],
|
||||
['temporary one', 'temporary'],
|
||||
['important one', 'important'],
|
||||
['normal two', 'normal'],
|
||||
];
|
||||
for (const [text, importance] of seed) await memory.remember({ scope, text, importance });
|
||||
|
||||
expect((await listByImportance(memory, scope, 2)).map(r => r.text))
|
||||
.toEqual(['critical one', 'important one']);
|
||||
const all = (await listByImportance(memory, scope, 10)).map(r => r.text);
|
||||
expect(all.slice(0, 2)).toEqual(['critical one', 'important one']);
|
||||
expect(all.slice(2).sort()).toEqual(['normal one', 'normal two']);
|
||||
});
|
||||
});
|
||||
|
||||
// ── buildWorkspaceState ───────────────────────────────────────
|
||||
|
||||
describe('buildWorkspaceState', () => {
|
||||
it('returns null for non-existent workspace', () => {
|
||||
const result = buildWorkspaceState({
|
||||
it('returns null for non-existent workspace', async () => {
|
||||
const result = await buildWorkspaceState({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'nonexistent',
|
||||
wsManager: makeManager(new Map()),
|
||||
activateWorkspaceMind: noopActivate,
|
||||
memory,
|
||||
});
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null for workspace with no mind file', () => {
|
||||
const wsDir = path.join(tmpDir, 'workspaces', 'empty');
|
||||
fs.mkdirSync(wsDir, { recursive: true });
|
||||
it('returns null for workspace with no memories', async () => {
|
||||
await setupWorkspace('empty');
|
||||
// Another workspace's memories do not count.
|
||||
await setupWorkspace('other', { memories: [{ text: 'Other workspace memory', importance: 'normal' }] });
|
||||
|
||||
const result = buildWorkspaceState({
|
||||
const result = await buildWorkspaceState({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'empty',
|
||||
wsManager: makeManager(new Map([['empty', { id: 'empty', name: 'Empty' }]])),
|
||||
activateWorkspaceMind: noopActivate,
|
||||
memory,
|
||||
});
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null for workspace with zero memory frames', () => {
|
||||
fs.mkdirSync(path.join(tmpDir, 'workspaces', 'noframes'), { recursive: true });
|
||||
const db = new MindDB(path.join(tmpDir, 'workspaces', 'noframes', 'workspace.mind'));
|
||||
db.close();
|
||||
|
||||
const result = buildWorkspaceState({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'noframes',
|
||||
wsManager: makeManager(new Map([['noframes', { id: 'noframes', name: 'No Frames' }]])),
|
||||
activateWorkspaceMind: noopActivate,
|
||||
});
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('returns state with decisions from memory frames', () => {
|
||||
const activateWorkspaceMind = vi.fn(() => true);
|
||||
setupWorkspace('ws1', {
|
||||
frames: [
|
||||
{ content: 'Decision: Use SQLite for local storage', importance: 'critical' },
|
||||
{ content: 'Working on the authentication module', importance: 'normal' },
|
||||
it('returns state with decisions from workspace memory', async () => {
|
||||
await setupWorkspace('ws1', {
|
||||
memories: [
|
||||
{ text: 'Decision: Use SQLite for local storage', importance: 'critical' },
|
||||
{ text: 'Working on the authentication module', importance: 'normal' },
|
||||
],
|
||||
});
|
||||
|
||||
const result = buildWorkspaceState({
|
||||
const result = await buildWorkspaceState({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'ws1',
|
||||
wsManager: makeManager(new Map([['ws1', { id: 'ws1', name: 'Project Alpha' }]])),
|
||||
activateWorkspaceMind,
|
||||
memory,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(activateWorkspaceMind).not.toHaveBeenCalled();
|
||||
expect(result!.recentDecisions.length).toBeGreaterThanOrEqual(1);
|
||||
expect(result!.recentDecisions[0].content).toContain('SQLite');
|
||||
expect(result!.recentDecisions[0].source).toBe('memory');
|
||||
});
|
||||
|
||||
it('extracts pending tasks from sessions', () => {
|
||||
setupWorkspace('ws2', {
|
||||
frames: [{ content: 'Project context established', importance: 'normal' }],
|
||||
it('takes decisions from their kind or wording, never from temporary memories', async () => {
|
||||
await setupWorkspace('ws8', {
|
||||
memories: [
|
||||
{ text: 'Ship on Fridays only', importance: 'normal', kind: 'decision' },
|
||||
{ text: 'We agreed to keep the old API', importance: 'normal' },
|
||||
{ text: 'Decision: scratch idea', importance: 'temporary' },
|
||||
{ text: 'Lunch was good', importance: 'normal' },
|
||||
],
|
||||
});
|
||||
|
||||
const result = await buildWorkspaceState({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'ws8',
|
||||
wsManager: makeManager(new Map([['ws8', { id: 'ws8', name: 'Decisions' }]])),
|
||||
memory,
|
||||
});
|
||||
|
||||
expect(result!.recentDecisions.map(d => d.content).sort())
|
||||
.toEqual(['Ship on Fridays only', 'We agreed to keep the old API']);
|
||||
});
|
||||
|
||||
it('extracts pending tasks from sessions', async () => {
|
||||
await setupWorkspace('ws2', {
|
||||
memories: [{ text: 'Project context established', importance: 'normal' }],
|
||||
sessions: [{
|
||||
title: 'Planning',
|
||||
messages: [
|
||||
@@ -202,12 +226,12 @@ describe('workspace-state', () => {
|
||||
}],
|
||||
});
|
||||
|
||||
const result = buildWorkspaceState({
|
||||
const result = await buildWorkspaceState({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'ws2',
|
||||
wsManager: makeManager(new Map([['ws2', { id: 'ws2', name: 'API Project' }]])),
|
||||
activateWorkspaceMind: noopActivate,
|
||||
memory,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
@@ -216,9 +240,9 @@ describe('workspace-state', () => {
|
||||
expect(result!.pending.length).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
|
||||
it('extracts open questions from sessions', () => {
|
||||
setupWorkspace('ws3', {
|
||||
frames: [{ content: 'Working on infrastructure', importance: 'normal' }],
|
||||
it('extracts open questions from sessions', async () => {
|
||||
await setupWorkspace('ws3', {
|
||||
memories: [{ text: 'Working on infrastructure', importance: 'normal' }],
|
||||
sessions: [{
|
||||
title: 'Infra discussion',
|
||||
messages: [
|
||||
@@ -229,22 +253,22 @@ describe('workspace-state', () => {
|
||||
}],
|
||||
});
|
||||
|
||||
const result = buildWorkspaceState({
|
||||
const result = await buildWorkspaceState({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'ws3',
|
||||
wsManager: makeManager(new Map([['ws3', { id: 'ws3', name: 'Infra' }]])),
|
||||
activateWorkspaceMind: noopActivate,
|
||||
memory,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.openQuestions.length).toBeGreaterThanOrEqual(1);
|
||||
});
|
||||
|
||||
it('classifies fresh and stale threads', () => {
|
||||
it('classifies fresh and stale threads', async () => {
|
||||
const tenDays = 10 * 24 * 60 * 60 * 1000;
|
||||
setupWorkspace('ws4', {
|
||||
frames: [{ content: 'Multi-thread workspace', importance: 'normal' }],
|
||||
await setupWorkspace('ws4', {
|
||||
memories: [{ text: 'Multi-thread workspace', importance: 'normal' }],
|
||||
sessions: [
|
||||
{
|
||||
title: 'Fresh thread',
|
||||
@@ -264,12 +288,12 @@ describe('workspace-state', () => {
|
||||
],
|
||||
});
|
||||
|
||||
const result = buildWorkspaceState({
|
||||
const result = await buildWorkspaceState({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'ws4',
|
||||
wsManager: makeManager(new Map([['ws4', { id: 'ws4', name: 'Multi' }]])),
|
||||
activateWorkspaceMind: noopActivate,
|
||||
memory,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
@@ -279,9 +303,9 @@ describe('workspace-state', () => {
|
||||
expect(result!.stale.some(s => s.content === 'Old thread')).toBe(true);
|
||||
});
|
||||
|
||||
it('derives nextActions from blockers and pending items', () => {
|
||||
setupWorkspace('ws5', {
|
||||
frames: [{ content: 'Active project', importance: 'normal' }],
|
||||
it('derives nextActions from blockers and pending items', async () => {
|
||||
await setupWorkspace('ws5', {
|
||||
memories: [{ text: 'Active project', importance: 'normal' }],
|
||||
sessions: [{
|
||||
title: 'Work session',
|
||||
messages: [
|
||||
@@ -292,12 +316,12 @@ describe('workspace-state', () => {
|
||||
}],
|
||||
});
|
||||
|
||||
const result = buildWorkspaceState({
|
||||
const result = await buildWorkspaceState({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'ws5',
|
||||
wsManager: makeManager(new Map([['ws5', { id: 'ws5', name: 'Blocked' }]])),
|
||||
activateWorkspaceMind: noopActivate,
|
||||
memory,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
@@ -308,11 +332,11 @@ describe('workspace-state', () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('returns all expected fields', () => {
|
||||
setupWorkspace('ws6', {
|
||||
frames: [
|
||||
{ content: 'Decision: Use React for frontend', importance: 'critical' },
|
||||
{ content: 'Project initialized', importance: 'normal' },
|
||||
it('returns all expected fields', async () => {
|
||||
await setupWorkspace('ws6', {
|
||||
memories: [
|
||||
{ text: 'Decision: Use React for frontend', importance: 'critical' },
|
||||
{ text: 'Project initialized', importance: 'normal' },
|
||||
],
|
||||
sessions: [{
|
||||
title: 'Setup session',
|
||||
@@ -323,12 +347,12 @@ describe('workspace-state', () => {
|
||||
}],
|
||||
});
|
||||
|
||||
const result = buildWorkspaceState({
|
||||
const result = await buildWorkspaceState({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'ws6',
|
||||
wsManager: makeManager(new Map([['ws6', { id: 'ws6', name: 'Full' }]])),
|
||||
activateWorkspaceMind: noopActivate,
|
||||
memory,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
@@ -344,9 +368,9 @@ describe('workspace-state', () => {
|
||||
expect(Array.isArray(result!.nextActions)).toBe(true);
|
||||
});
|
||||
|
||||
it("lists the workspace's awareness items from waggle.db as active, not other scopes", () => {
|
||||
setupWorkspace('ws7', {
|
||||
frames: [{ content: 'Project with open tasks', importance: 'normal' }],
|
||||
it("lists the workspace's awareness items from waggle.db as active, not other scopes", async () => {
|
||||
await setupWorkspace('ws7', {
|
||||
memories: [{ text: 'Project with open tasks', importance: 'normal' }],
|
||||
awareness: [
|
||||
{ category: 'task', content: 'Ship the installer', priority: 2 },
|
||||
{ category: 'flag', content: 'Context flag only', priority: 1 },
|
||||
@@ -355,11 +379,12 @@ describe('workspace-state', () => {
|
||||
new AwarenessLayer(waggleDb).add('task', 'Personal errand', 5);
|
||||
new AwarenessLayer(waggleDb, workspaceDataScope('other')).add('task', 'Other workspace task', 5);
|
||||
|
||||
const result = buildWorkspaceState({
|
||||
const result = await buildWorkspaceState({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'ws7',
|
||||
wsManager: makeManager(new Map([['ws7', { id: 'ws7', name: 'Tasks' }]])),
|
||||
memory,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
|
||||
Reference in New Issue
Block a user