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,13 @@ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import os from 'node:os';
|
||||
import { MindDB } from '@waggle/core';
|
||||
import { WaggleDB } from '@waggle/core';
|
||||
import {
|
||||
createInMemoryMemoryStore,
|
||||
workspaceScope,
|
||||
type MemoryImportance,
|
||||
type MemoryStore,
|
||||
} from '@waggle/memory-contract';
|
||||
import {
|
||||
buildWorkspaceNowBlock,
|
||||
formatWorkspaceNowPrompt,
|
||||
@@ -17,18 +23,23 @@ import {
|
||||
*
|
||||
* The chat route code:
|
||||
* if (workspaceId) {
|
||||
* const nowBlock = buildWorkspaceNowBlock({ ... });
|
||||
* const nowBlock = await buildWorkspaceNowBlock({ ... });
|
||||
* if (nowBlock) prompt += '\n\n' + formatWorkspaceNowPrompt(nowBlock);
|
||||
* }
|
||||
*/
|
||||
describe('Context Injection — system prompt integration', () => {
|
||||
let tmpDir: string;
|
||||
let memory: MemoryStore;
|
||||
let waggleDb: WaggleDB;
|
||||
|
||||
beforeEach(() => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ctx-inject-'));
|
||||
memory = createInMemoryMemoryStore();
|
||||
waggleDb = WaggleDB.openInDataDir(tmpDir);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
waggleDb.close();
|
||||
try {
|
||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||
} catch {
|
||||
@@ -38,8 +49,8 @@ describe('Context Injection — system prompt integration', () => {
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────
|
||||
|
||||
function setupWorkspace(id: string, opts?: {
|
||||
frames?: Array<{ content: string; importance: string }>;
|
||||
async function setupWorkspace(id: string, opts?: {
|
||||
memories?: Array<{ text: string; importance: MemoryImportance }>;
|
||||
sessions?: Array<{ title: string; messages: Array<{ role: string; content: string }> }>;
|
||||
}) {
|
||||
const wsDir = path.join(tmpDir, 'workspaces', id);
|
||||
@@ -50,20 +61,8 @@ describe('Context Injection — system prompt integration', () => {
|
||||
JSON.stringify({ id, name: `Test ${id}`, group: 'test', created: new Date().toISOString() }),
|
||||
);
|
||||
|
||||
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) {
|
||||
raw.prepare(
|
||||
`INSERT INTO memory_frames (gop_id, frame_type, content, importance, created_at) VALUES ('session:test', 'I', ?, ?, datetime('now'))`,
|
||||
).run(frame.content, frame.importance);
|
||||
}
|
||||
db.close();
|
||||
for (const memo of opts?.memories ?? []) {
|
||||
await memory.remember({ scope: workspaceScope(id), text: memo.text, importance: memo.importance });
|
||||
}
|
||||
|
||||
if (opts?.sessions) {
|
||||
@@ -86,26 +85,24 @@ describe('Context Injection — system prompt integration', () => {
|
||||
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;
|
||||
|
||||
/**
|
||||
* Simulate what buildSystemPrompt does: build a base prompt, then conditionally
|
||||
* append the workspace context. This mirrors the injection logic in chat.ts.
|
||||
*/
|
||||
function simulatePromptBuild(basePrompt: string, workspaceId?: string, wsManager?: ReturnType<typeof makeManager>) {
|
||||
async function simulatePromptBuild(basePrompt: string, workspaceId?: string, wsManager?: ReturnType<typeof makeManager>) {
|
||||
let prompt = basePrompt;
|
||||
|
||||
if (workspaceId && wsManager) {
|
||||
try {
|
||||
const nowBlock = buildWorkspaceNowBlock({
|
||||
const nowBlock = await buildWorkspaceNowBlock({
|
||||
dataDir: tmpDir,
|
||||
workspaceId,
|
||||
wsManager,
|
||||
activateWorkspaceMind: noopActivate,
|
||||
waggleDb,
|
||||
memory,
|
||||
});
|
||||
if (nowBlock) {
|
||||
prompt += '\n\n' + formatWorkspaceNowPrompt(nowBlock);
|
||||
@@ -120,9 +117,9 @@ describe('Context Injection — system prompt integration', () => {
|
||||
|
||||
// ── Test 1: No workspaceId → no "Workspace Now" section ─────────────
|
||||
|
||||
it('buildSystemPrompt without workspaceId produces no "Workspace Now" section', () => {
|
||||
it('buildSystemPrompt without workspaceId produces no "Workspace Now" section', async () => {
|
||||
const basePrompt = '# Who You Are\n\nYou are Waggle.';
|
||||
const result = simulatePromptBuild(basePrompt);
|
||||
const result = await simulatePromptBuild(basePrompt);
|
||||
|
||||
expect(result).not.toContain('# Workspace Now');
|
||||
expect(result).toBe(basePrompt);
|
||||
@@ -130,15 +127,15 @@ describe('Context Injection — system prompt integration', () => {
|
||||
|
||||
// ── Test 2: With workspaceId + data → includes "Workspace Now" ──────
|
||||
|
||||
it('buildSystemPrompt with workspaceId for workspace with data includes "Workspace Now"', () => {
|
||||
it('buildSystemPrompt with workspaceId for workspace with data includes "Workspace Now"', async () => {
|
||||
const wsManager = makeManager(new Map([
|
||||
['rich-ws', { id: 'rich-ws', name: 'Rich Project' }],
|
||||
]));
|
||||
|
||||
setupWorkspace('rich-ws', {
|
||||
frames: [
|
||||
{ content: 'Working on the Waggle platform, a workspace-native AI agent.', importance: 'important' },
|
||||
{ content: 'Decision: Use SQLite for local persistence.', importance: 'critical' },
|
||||
await setupWorkspace('rich-ws', {
|
||||
memories: [
|
||||
{ text: 'Working on the Waggle platform, a workspace-native AI agent.', importance: 'important' },
|
||||
{ text: 'Decision: Use SQLite for local persistence.', importance: 'critical' },
|
||||
],
|
||||
sessions: [
|
||||
{
|
||||
@@ -152,7 +149,7 @@ describe('Context Injection — system prompt integration', () => {
|
||||
});
|
||||
|
||||
const basePrompt = '# Who You Are\n\nYou are Waggle.';
|
||||
const result = simulatePromptBuild(basePrompt, 'rich-ws', wsManager);
|
||||
const result = await simulatePromptBuild(basePrompt, 'rich-ws', wsManager);
|
||||
|
||||
expect(result).toContain('# Workspace Now');
|
||||
expect(result).toContain('Rich Project');
|
||||
@@ -166,12 +163,12 @@ describe('Context Injection — system prompt integration', () => {
|
||||
|
||||
// ── Test 3: With workspaceId for empty/missing workspace → graceful ─
|
||||
|
||||
it('buildSystemPrompt with workspaceId for empty workspace has no "Workspace Now" (graceful)', () => {
|
||||
it('buildSystemPrompt with workspaceId for empty workspace has no "Workspace Now" (graceful)', async () => {
|
||||
const wsManager = makeManager(new Map([
|
||||
['empty-ws', { id: 'empty-ws', name: 'Empty Project' }],
|
||||
]));
|
||||
|
||||
// Create workspace dir but no mind file
|
||||
// Create the workspace directory but no memories
|
||||
const wsDir = path.join(tmpDir, 'workspaces', 'empty-ws');
|
||||
fs.mkdirSync(wsDir, { recursive: true });
|
||||
fs.writeFileSync(
|
||||
@@ -180,17 +177,17 @@ describe('Context Injection — system prompt integration', () => {
|
||||
);
|
||||
|
||||
const basePrompt = '# Who You Are\n\nYou are Waggle.';
|
||||
const result = simulatePromptBuild(basePrompt, 'empty-ws', wsManager);
|
||||
const result = await simulatePromptBuild(basePrompt, 'empty-ws', wsManager);
|
||||
|
||||
expect(result).not.toContain('# Workspace Now');
|
||||
expect(result).toBe(basePrompt);
|
||||
});
|
||||
|
||||
it('buildSystemPrompt with workspaceId for non-existent workspace has no "Workspace Now"', () => {
|
||||
it('buildSystemPrompt with workspaceId for non-existent workspace has no "Workspace Now"', async () => {
|
||||
const wsManager = makeManager(new Map());
|
||||
|
||||
const basePrompt = '# Who You Are\n\nYou are Waggle.';
|
||||
const result = simulatePromptBuild(basePrompt, 'does-not-exist', wsManager);
|
||||
const result = await simulatePromptBuild(basePrompt, 'does-not-exist', wsManager);
|
||||
|
||||
expect(result).not.toContain('# Workspace Now');
|
||||
expect(result).toBe(basePrompt);
|
||||
@@ -198,29 +195,29 @@ describe('Context Injection — system prompt integration', () => {
|
||||
|
||||
// ── Test 4: Cache invalidation — different workspaceId → different prompt ─
|
||||
|
||||
it('different workspaceId produces different prompt content', () => {
|
||||
it('different workspaceId produces different prompt content', async () => {
|
||||
const wsManager = makeManager(new Map([
|
||||
['ws-a', { id: 'ws-a', name: 'Project Alpha' }],
|
||||
['ws-b', { id: 'ws-b', name: 'Project Beta' }],
|
||||
]));
|
||||
|
||||
setupWorkspace('ws-a', {
|
||||
frames: [
|
||||
{ content: 'Alpha is a front-end framework project.', importance: 'important' },
|
||||
{ content: 'Decision: Use React for Alpha.', importance: 'critical' },
|
||||
await setupWorkspace('ws-a', {
|
||||
memories: [
|
||||
{ text: 'Alpha is a front-end framework project.', importance: 'important' },
|
||||
{ text: 'Decision: Use React for Alpha.', importance: 'critical' },
|
||||
],
|
||||
});
|
||||
|
||||
setupWorkspace('ws-b', {
|
||||
frames: [
|
||||
{ content: 'Beta is a backend API project.', importance: 'important' },
|
||||
{ content: 'Decision: Use Rust for Beta.', importance: 'critical' },
|
||||
await setupWorkspace('ws-b', {
|
||||
memories: [
|
||||
{ text: 'Beta is a backend API project.', importance: 'important' },
|
||||
{ text: 'Decision: Use Rust for Beta.', importance: 'critical' },
|
||||
],
|
||||
});
|
||||
|
||||
const basePrompt = '# Who You Are\n\nYou are Waggle.';
|
||||
const promptA = simulatePromptBuild(basePrompt, 'ws-a', wsManager);
|
||||
const promptB = simulatePromptBuild(basePrompt, 'ws-b', wsManager);
|
||||
const promptA = await simulatePromptBuild(basePrompt, 'ws-a', wsManager);
|
||||
const promptB = await simulatePromptBuild(basePrompt, 'ws-b', wsManager);
|
||||
|
||||
// Both should have Workspace Now
|
||||
expect(promptA).toContain('# Workspace Now');
|
||||
@@ -238,19 +235,19 @@ describe('Context Injection — system prompt integration', () => {
|
||||
|
||||
// ── Test 5: Verify the formatted block is positioned correctly ──────
|
||||
|
||||
it('workspace context is appended after the base prompt, not prepended', () => {
|
||||
it('workspace context is appended after the base prompt, not prepended', async () => {
|
||||
const wsManager = makeManager(new Map([
|
||||
['pos-ws', { id: 'pos-ws', name: 'Position Test' }],
|
||||
]));
|
||||
|
||||
setupWorkspace('pos-ws', {
|
||||
frames: [
|
||||
{ content: 'Testing prompt positioning.', importance: 'important' },
|
||||
await setupWorkspace('pos-ws', {
|
||||
memories: [
|
||||
{ text: 'Testing prompt positioning.', importance: 'important' },
|
||||
],
|
||||
});
|
||||
|
||||
const basePrompt = 'BASE_PROMPT_START\nSome agent instructions.\nBASE_PROMPT_END';
|
||||
const result = simulatePromptBuild(basePrompt, 'pos-ws', wsManager);
|
||||
const result = await simulatePromptBuild(basePrompt, 'pos-ws', wsManager);
|
||||
|
||||
// Base prompt should appear first
|
||||
expect(result.startsWith('BASE_PROMPT_START')).toBe(true);
|
||||
|
||||
Reference in New Issue
Block a user