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:
@@ -335,6 +335,8 @@ export interface AgentState {
|
||||
getWorkspaceMindDb: (workspaceId: string) => import('@waggle/core').MindDB | null;
|
||||
/** Close and remove a workspace MindDB from cache. Used before workspace deletion to prevent EBUSY. */
|
||||
closeWorkspaceMind: (workspaceId: string) => void;
|
||||
/** Close a workspace's live sessions and detach it from the shared orchestrator. Used before workspace deletion. */
|
||||
releaseWorkspace: (workspaceId: string) => void;
|
||||
/** List all workspaces (id + name). Used by global memory search. */
|
||||
listWorkspaces: () => Array<{ id: string; name: string }>;
|
||||
/** Currently active workspace ID (null = personal only) */
|
||||
@@ -1301,6 +1303,9 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
setActiveWorkspaceId(null);
|
||||
}
|
||||
};
|
||||
// Until the legacy mind cache is removed, releasing a workspace also closes
|
||||
// its cached mind so the workspace directory can be deleted (A6 EBUSY).
|
||||
const releaseWorkspace = closeWorkspaceMind;
|
||||
|
||||
const getWorkspaceMindDb = (workspaceId: string): MindDB | null => {
|
||||
if (!wsManager.get(workspaceId)) return null;
|
||||
@@ -1328,6 +1333,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
activateWorkspaceMind: activateWorkspaceMind,
|
||||
getWorkspaceMindDb,
|
||||
closeWorkspaceMind,
|
||||
releaseWorkspace,
|
||||
listWorkspaces: () => wsManager.list().map(w => ({ id: w.id, name: w.name })),
|
||||
activeWorkspaceId,
|
||||
subagentOrchestrator: null,
|
||||
@@ -1418,7 +1424,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
const proactiveCtx: ProactiveContext = {
|
||||
dataDir: fullConfig.dataDir,
|
||||
workspaceManager: wsManager,
|
||||
getWorkspaceMindDb,
|
||||
memory,
|
||||
waggleDb,
|
||||
};
|
||||
|
||||
@@ -1456,12 +1462,12 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
try {
|
||||
switch (proactiveConfig.action) {
|
||||
case 'morning_briefing': {
|
||||
const briefing = generateMorningBriefing(proactiveCtx);
|
||||
const briefing = await generateMorningBriefing(proactiveCtx);
|
||||
if (briefing) emitProactive(briefing);
|
||||
break;
|
||||
}
|
||||
case 'stale_workspace_check': {
|
||||
const staleAlerts = checkStaleWorkspaces(proactiveCtx);
|
||||
const staleAlerts = await checkStaleWorkspaces(proactiveCtx);
|
||||
for (const alert of staleAlerts) emitProactive(alert);
|
||||
break;
|
||||
}
|
||||
@@ -1471,7 +1477,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
||||
break;
|
||||
}
|
||||
case 'capability_suggestion': {
|
||||
const suggestion = suggestCapabilities(proactiveCtx);
|
||||
const suggestion = await suggestCapabilities(proactiveCtx);
|
||||
if (suggestion) emitProactive(suggestion);
|
||||
break;
|
||||
}
|
||||
|
||||
15
packages/server/src/local/memory-status.ts
Normal file
15
packages/server/src/local/memory-status.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* The review status the Memory Center shows for a memory (shared `MemoryStatus`).
|
||||
*/
|
||||
import type { MemoryStatus } from '@waggle/shared';
|
||||
import type { MemoryRecord } from '@waggle/memory-contract';
|
||||
|
||||
/**
|
||||
* Archived when its metadata says so; facts the background extraction learned
|
||||
* stay unreviewed until the user confirms them; everything else is active.
|
||||
*/
|
||||
export function memoryStatus(record: MemoryRecord): Extract<MemoryStatus, 'active' | 'unreviewed' | 'archived'> {
|
||||
if (record.metadata.archived === true) return 'archived';
|
||||
if (record.origin === 'extraction' && !record.metadata.confirmedAt) return 'unreviewed';
|
||||
return 'active';
|
||||
}
|
||||
@@ -7,12 +7,13 @@
|
||||
* toast system picks them up.
|
||||
*
|
||||
* Separate from the Team proactive service (ProactiveService) which uses
|
||||
* Drizzle/PostgreSQL. These handlers operate on local workspace/mind data.
|
||||
* Drizzle/PostgreSQL. These handlers operate on local workspace and memory data.
|
||||
*/
|
||||
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { AwarenessLayer, workspaceDataScope, type WorkspaceManager, type WorkspaceConfig } from '@waggle/core';
|
||||
import { AwarenessLayer, workspaceDataScope, type WorkspaceManager } from '@waggle/core';
|
||||
import { workspaceScope, type MemoryRecord, type MemoryStore } from '@waggle/memory-contract';
|
||||
import { materialFingerprint } from './notification-gate.js';
|
||||
|
||||
// ── Types ──────────────────────────────────────────────────────────────
|
||||
@@ -32,8 +33,7 @@ export interface ProactiveMessage {
|
||||
export interface ProactiveContext {
|
||||
dataDir: string;
|
||||
workspaceManager: WorkspaceManager;
|
||||
/** Get a cached workspace MindDB (opens on demand). Returns null if workspace not found. */
|
||||
getWorkspaceMindDb: (workspaceId: string) => import('@waggle/core').MindDB | null;
|
||||
memory: MemoryStore;
|
||||
/** waggle.db, which holds awareness items. */
|
||||
waggleDb: import('@waggle/core').WaggleDB;
|
||||
}
|
||||
@@ -68,14 +68,21 @@ function countPendingAwareness(ctx: ProactiveContext, workspaceId: string): numb
|
||||
}
|
||||
}
|
||||
|
||||
/** Count memory frames in a workspace mind. */
|
||||
function countMemoryFrames(ctx: ProactiveContext, workspaceId: string): number {
|
||||
const db = ctx.getWorkspaceMindDb(workspaceId);
|
||||
if (!db) return 0;
|
||||
/** Recent memories scanned for the morning briefing's highlights. */
|
||||
const HIGHLIGHT_SCAN = 200;
|
||||
|
||||
/** A briefing highlight: a decision or an important memory. */
|
||||
function isHighlight(record: MemoryRecord): boolean {
|
||||
return record.kind === 'decision'
|
||||
|| record.importance === 'critical'
|
||||
|| record.importance === 'important'
|
||||
|| /^decision|decided/i.test(record.text);
|
||||
}
|
||||
|
||||
/** Count the memories of a workspace. */
|
||||
async function countMemories(ctx: ProactiveContext, workspaceId: string): Promise<number> {
|
||||
try {
|
||||
const raw = db.getDatabase();
|
||||
const row = raw.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number } | undefined;
|
||||
return row?.cnt ?? 0;
|
||||
return await ctx.memory.count({ scopes: [workspaceScope(workspaceId)] });
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
@@ -96,7 +103,7 @@ function hasInstalledCapabilities(dataDir: string): boolean {
|
||||
// ── Handlers ───────────────────────────────────────────────────────────
|
||||
|
||||
/** Generate morning briefing across all workspaces. */
|
||||
export function generateMorningBriefing(ctx: ProactiveContext): ProactiveMessage | null {
|
||||
export async function generateMorningBriefing(ctx: ProactiveContext): Promise<ProactiveMessage | null> {
|
||||
const workspaces = ctx.workspaceManager.list();
|
||||
if (workspaces.length === 0) return null;
|
||||
|
||||
@@ -120,21 +127,19 @@ export function generateMorningBriefing(ctx: ProactiveContext): ProactiveMessage
|
||||
}
|
||||
}
|
||||
|
||||
// W5.8: Also extract recent decisions and memory highlights per workspace
|
||||
// W5.8: Also extract this week's decisions and memory highlights per workspace
|
||||
const decisionParts: string[] = [];
|
||||
const weekAgo = new Date(now - 7 * 24 * 60 * 60 * 1000).toISOString();
|
||||
for (const ws of workspaces.slice(0, 5)) { // cap at 5 workspaces for brevity
|
||||
const db = ctx.getWorkspaceMindDb(ws.id);
|
||||
if (!db) continue;
|
||||
try {
|
||||
const raw = db.getDatabase();
|
||||
const decisions = raw.prepare(
|
||||
`SELECT content FROM memory_frames
|
||||
WHERE (importance IN ('critical', 'important') OR content LIKE 'Decision%' OR content LIKE '%decided%')
|
||||
AND created_at > datetime('now', '-7 day')
|
||||
ORDER BY id DESC LIMIT 2`
|
||||
).all() as Array<{ content: string }>;
|
||||
const recent = await ctx.memory.list({
|
||||
scopes: [workspaceScope(ws.id)],
|
||||
since: weekAgo,
|
||||
limit: HIGHLIGHT_SCAN,
|
||||
});
|
||||
const decisions = recent.filter(isHighlight).slice(0, 2);
|
||||
if (decisions.length > 0) {
|
||||
decisionParts.push(`**${ws.name}**: ${decisions.map(d => d.content.slice(0, 80)).join(' | ')}`);
|
||||
decisionParts.push(`**${ws.name}**: ${decisions.map(d => d.text.slice(0, 80)).join(' | ')}`);
|
||||
}
|
||||
} catch { /* non-blocking */ }
|
||||
}
|
||||
@@ -177,7 +182,7 @@ export function generateMorningBriefing(ctx: ProactiveContext): ProactiveMessage
|
||||
}
|
||||
|
||||
/** Check for stale workspaces (not visited in 14+ days). */
|
||||
export function checkStaleWorkspaces(ctx: ProactiveContext): ProactiveMessage[] {
|
||||
export async function checkStaleWorkspaces(ctx: ProactiveContext): Promise<ProactiveMessage[]> {
|
||||
const workspaces = ctx.workspaceManager.list();
|
||||
if (workspaces.length === 0) return [];
|
||||
|
||||
@@ -193,13 +198,13 @@ export function checkStaleWorkspaces(ctx: ProactiveContext): ProactiveMessage[]
|
||||
const idleDays = Math.floor((now - referenceDate.getTime()) / (24 * 60 * 60 * 1000));
|
||||
|
||||
if (idleDays >= 14) {
|
||||
const frameCount = countMemoryFrames(ctx, ws.id);
|
||||
const frameCount = await countMemories(ctx, ws.id);
|
||||
|
||||
messages.push({
|
||||
type: 'stale_workspace',
|
||||
title: `"${ws.name}" hasn't been visited in ${idleDays} days`,
|
||||
body: frameCount > 0
|
||||
? `This workspace has ${frameCount} memory frame${frameCount === 1 ? '' : 's'} that may need attention.`
|
||||
? `This workspace has ${frameCount} memor${frameCount === 1 ? 'y' : 'ies'} that may need attention.`
|
||||
: 'Consider archiving or revisiting this workspace.',
|
||||
workspaceId: ws.id,
|
||||
actionUrl: `/workspaces/${ws.id}`,
|
||||
@@ -207,7 +212,7 @@ export function checkStaleWorkspaces(ctx: ProactiveContext): ProactiveMessage[]
|
||||
dedupeKey: `proactive:stale_workspace:${ws.id}`,
|
||||
// Fingerprint on identity, not idleDays — so a still-stale workspace
|
||||
// doesn't re-nag every day. Re-fires only if it's touched (new
|
||||
// reference time) or its frame count changes.
|
||||
// reference time) or its memory count changes.
|
||||
materialHash: materialFingerprint({ wsId: ws.id, frameCount, referenceMs: referenceDate.getTime() }),
|
||||
});
|
||||
}
|
||||
@@ -243,7 +248,7 @@ export function checkPendingTasks(ctx: ProactiveContext): ProactiveMessage[] {
|
||||
}
|
||||
|
||||
/** Suggest capabilities based on usage patterns. */
|
||||
export function suggestCapabilities(ctx: ProactiveContext): ProactiveMessage | null {
|
||||
export async function suggestCapabilities(ctx: ProactiveContext): Promise<ProactiveMessage | null> {
|
||||
// If the user has no installed capabilities, suggest exploring the catalog
|
||||
if (!hasInstalledCapabilities(ctx.dataDir)) {
|
||||
return {
|
||||
@@ -264,7 +269,7 @@ export function suggestCapabilities(ctx: ProactiveContext): ProactiveMessage | n
|
||||
// Count total memories across all workspaces
|
||||
let totalFrames = 0;
|
||||
for (const ws of workspaces) {
|
||||
totalFrames += countMemoryFrames(ctx, ws.id);
|
||||
totalFrames += await countMemories(ctx, ws.id);
|
||||
}
|
||||
|
||||
// If accumulating significant memories but no connectors, suggest connectors
|
||||
|
||||
@@ -87,24 +87,6 @@ export { MAX_CONTEXT_MESSAGES, applyContextWindow, buildSkillPromptSection } fro
|
||||
|
||||
export type AgentRunner = (config: AgentLoopConfig) => Promise<AgentResponse>;
|
||||
|
||||
/**
|
||||
* W4A diagnostics — the "MindDB flake". A long chat turn holds a workspace
|
||||
* MindDB handle obtained from the process-wide MultiMindCache (LRU, maxOpen:20).
|
||||
* If ≥20 OTHER workspaces are touched mid-turn (home-briefing fan-out, weaver
|
||||
* timers, cross-workspace tools), the cache evicts + `.close()`s THIS turn's
|
||||
* handle, and the post-response DB write-backs below throw better-sqlite3's
|
||||
* native "The database connection is not open". Those catches are non-blocking
|
||||
* and swallow it silently, which is exactly why the flake is invisible. This
|
||||
* predicate lets us surface a structured warn at the failure seam WITHOUT
|
||||
* changing behavior (the write still fails soft) so the root cause can be
|
||||
* confirmed live. The real fix (pin-aware eviction) lives in the OSS substrate
|
||||
* (packages/hive-mind-core/src/multi-mind-cache.ts) and is out of scope here.
|
||||
*/
|
||||
function isClosedDbError(e: unknown): boolean {
|
||||
const msg = e instanceof Error ? e.message : String(e);
|
||||
return /database (connection|handle) is not open|database is closed/i.test(msg);
|
||||
}
|
||||
|
||||
function isIncompleteCompletionError(error: unknown): boolean {
|
||||
return typeof error === 'object'
|
||||
&& error !== null
|
||||
@@ -1030,12 +1012,12 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
|
||||
// Workspace Now — inject structured context so the agent is grounded on first turn
|
||||
if (includePersistedMemory && includeConversationDerivedWorkspaceState && workspaceId) {
|
||||
try {
|
||||
const nowBlock = buildWorkspaceNowBlock({
|
||||
const nowBlock = await buildWorkspaceNowBlock({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
memory: server.memory,
|
||||
workspaceId,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
cronSchedules: server.cronStore.list(),
|
||||
});
|
||||
if (nowBlock) {
|
||||
@@ -1725,12 +1707,12 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
|
||||
return 'Conversation-derived workspace state is disabled for this turn.';
|
||||
}
|
||||
if (!effectiveWorkspace) return 'No workspace state available.';
|
||||
const block = buildWorkspaceNowBlock({
|
||||
const block = await buildWorkspaceNowBlock({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
memory: server.memory,
|
||||
workspaceId: effectiveWorkspace,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
cronSchedules: server.cronStore.list(),
|
||||
});
|
||||
if (!block) return 'No workspace state available.';
|
||||
@@ -2451,8 +2433,7 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
|
||||
// turns per #13, and for injected-runner turns like every other
|
||||
// write-back seam in this route). The summary aggregates
|
||||
// tool/connector output, so scan it before it can enter durable
|
||||
// memory; fail-soft with the W4A closed-DB guard so persistence
|
||||
// never fails the turn.
|
||||
// memory; fail soft so persistence never fails the turn.
|
||||
if (!hasCustomRunner && allowMemoryPersistence) {
|
||||
const summaryScan = scanForInjection(compressionResult.summary, 'tool_output');
|
||||
if (summaryScan.score >= 0.7) {
|
||||
@@ -2469,11 +2450,7 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
|
||||
}
|
||||
} catch (e) {
|
||||
throwIfTurnAborted();
|
||||
if (isClosedDbError(e)) {
|
||||
log.warn(`[context-compression] summary persist skipped — mind handle closed mid-turn (session=${sessionId})`);
|
||||
} else {
|
||||
log.warn(`[context-compression] summary persist failed: ${e instanceof Error ? e.message : e}`);
|
||||
}
|
||||
log.warn(`[context-compression] summary persist failed: ${e instanceof Error ? e.message : e}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3215,19 +3192,9 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
|
||||
if (saved.length > 0) {
|
||||
sendEvent('step', { content: `Auto-saved ${saved.length} memor${saved.length === 1 ? 'y' : 'ies'} from this exchange.` });
|
||||
}
|
||||
} catch (e) {
|
||||
} catch {
|
||||
throwIfTurnAborted();
|
||||
// Non-blocking. W4A: a closed-handle failure here is the signature
|
||||
// of the MultiMindCache evicting this turn's mind mid-flight — log
|
||||
// it with context so the flake is observable, but still fail soft.
|
||||
if (isClosedDbError(e)) {
|
||||
log.warn('[waggle][W4A] workspace mind handle closed mid-turn during auto-save', {
|
||||
workspaceId: effectiveWorkspace,
|
||||
sessionId,
|
||||
seam: 'autoSaveFromExchange',
|
||||
error: e instanceof Error ? e.message : String(e),
|
||||
});
|
||||
}
|
||||
// Non-blocking — the turn succeeds without the auto-save.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
* POST /api/command/execute — alias → commandRegistry.execute
|
||||
*
|
||||
* Surfaces federated by /search (CommandResultType in parens):
|
||||
* - memory → server.multiMind.search() (`memory`)
|
||||
* - memory → server.memory.search() (`memory`)
|
||||
* - workspaces → server.workspaceManager.list() (`workspace`)
|
||||
* - skills → loadSkills(dataDir) (`skill`)
|
||||
* - sessions → searchSessions() over each workspace's sessions (`session`)
|
||||
@@ -31,6 +31,7 @@ import type { FastifyPluginAsync } from 'fastify';
|
||||
import type { CommandResult, Command } from '@waggle/shared';
|
||||
import type { Orchestrator, LoadedSkill } from '@waggle/agent';
|
||||
import { loadSkills } from '@waggle/agent';
|
||||
import { PERSONAL_SCOPE, workspaceScope, type MemoryScope } from '@waggle/memory-contract';
|
||||
import { buildWorkspaceNowBlock, formatWorkspaceNowPrompt } from './workspace-context.js';
|
||||
import { searchSessions } from './session-utils.js';
|
||||
import { interpretCommand } from '../command-interpret.js';
|
||||
@@ -60,6 +61,16 @@ function safeFederate<T>(label: string, fn: () => T[], log: (m: string) => void)
|
||||
}
|
||||
}
|
||||
|
||||
async function safeFederateAsync<T>(label: string, fn: () => Promise<T[]>, log: (m: string) => void): Promise<T[]> {
|
||||
try {
|
||||
return await fn();
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
log(`command/search: ${label} federation failed — ${message}`);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/** Truncate a content blob into a single-line subtitle. */
|
||||
function toSubtitle(content: string, max = 120): string {
|
||||
const oneLine = content.replace(/\s+/g, ' ').trim();
|
||||
@@ -104,17 +115,21 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
|
||||
}));
|
||||
}, logWarn));
|
||||
|
||||
// Memory — FTS over personal + active workspace minds.
|
||||
results.push(...safeFederate('memory', () => {
|
||||
const searchScope = scope === 'personal' || scope === 'workspace' ? scope : 'all';
|
||||
const frames = server.multiMind.search(q, searchScope, perFacet);
|
||||
return frames.map((frame): CommandResult => ({
|
||||
id: `memory:${frame.id}`,
|
||||
// Memory — ranked search over personal memory and the active workspace's.
|
||||
results.push(...await safeFederateAsync('memory', async () => {
|
||||
const activeWorkspaceId = server.agentState.activeWorkspaceId;
|
||||
const scopes: MemoryScope[] = [];
|
||||
if (scope !== 'workspace') scopes.push(PERSONAL_SCOPE);
|
||||
if (scope !== 'personal' && activeWorkspaceId) scopes.push(workspaceScope(activeWorkspaceId));
|
||||
if (scopes.length === 0) return [];
|
||||
const hits = await server.memory.search(q, { scopes, limit: perFacet });
|
||||
return hits.map((hit): CommandResult => ({
|
||||
id: `memory:${hit.id}`,
|
||||
type: 'memory',
|
||||
title: toSubtitle(frame.content, 80),
|
||||
subtitle: frame.created_at ? new Date(frame.created_at).toLocaleString('en-US') : undefined,
|
||||
title: toSubtitle(hit.text, 80),
|
||||
subtitle: new Date(hit.createdAt).toLocaleString('en-US'),
|
||||
category: 'search',
|
||||
action: { route: `/memory?frame=${encodeURIComponent(frame.id)}` },
|
||||
action: { route: `/memory?frame=${encodeURIComponent(hit.id)}` },
|
||||
}));
|
||||
}, logWarn));
|
||||
|
||||
@@ -249,12 +264,12 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
|
||||
}
|
||||
},
|
||||
getWorkspaceState: async (): Promise<string> => {
|
||||
const block = buildWorkspaceNowBlock({
|
||||
const block = await buildWorkspaceNowBlock({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
memory: server.memory,
|
||||
workspaceId: effectiveWorkspaceId,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
cronSchedules: server.cronStore.list(),
|
||||
});
|
||||
if (!block) return 'No workspace state available.';
|
||||
@@ -287,12 +302,12 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
|
||||
// (awareness + recent sessions + pending). Best-effort.
|
||||
let memoryContext = '';
|
||||
try {
|
||||
const block = buildWorkspaceNowBlock({
|
||||
const block = await buildWorkspaceNowBlock({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
memory: server.memory,
|
||||
workspaceId: workspaceId ?? 'default',
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
cronSchedules: server.cronStore.list(),
|
||||
});
|
||||
if (block) memoryContext = formatWorkspaceNowPrompt(block);
|
||||
|
||||
@@ -61,12 +61,12 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
|
||||
},
|
||||
|
||||
getWorkspaceState: async (): Promise<string> => {
|
||||
const block = buildWorkspaceNowBlock({
|
||||
const block = await buildWorkspaceNowBlock({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
memory: server.memory,
|
||||
workspaceId: effectiveWorkspaceId,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
cronSchedules: server.cronStore.list(),
|
||||
});
|
||||
if (!block) return 'No workspace state available.';
|
||||
|
||||
@@ -12,11 +12,11 @@
|
||||
* (typed WorkspaceState with `nextActions`, pending/blocked/recentDecisions).
|
||||
* - `buildTimeAwareGreeting()` / `buildUpcomingSchedules()` —
|
||||
* `packages/server/src/local/routes/workspace-context.ts:119,169`.
|
||||
* - `IdentityLayer` — `@waggle/core` (mind/identity.ts), read off the personal
|
||||
* mind for the user's name (B8: Home greets by name via existing identity).
|
||||
* - `server.workspaceManager` (list/get/getMindPath) + `server.agentState`
|
||||
* (activateWorkspaceMind) + `server.cronStore` (list/getExecutionHistory) +
|
||||
* `server.multiMind.personal` + `getAuditDb()` (events.ts) — sidecar decorators.
|
||||
* - `IdentityLayer` — `@waggle/core` (identity.ts), the profile card in
|
||||
* waggle.db, for the user's name (B8: Home greets by name via existing identity).
|
||||
* - `server.workspaceManager` (list/get) + `server.cronStore`
|
||||
* (list/getExecutionHistory) + `server.memory` + `server.waggleDb` +
|
||||
* `getAuditDb()` (events.ts) — sidecar decorators.
|
||||
*
|
||||
* Founder decisions honored: A2 (Home Cockpit personal-only v1 — team/shared
|
||||
* workspaces are excluded from the cross-workspace fan-out; team-overnight rows
|
||||
@@ -25,8 +25,9 @@
|
||||
*/
|
||||
|
||||
import type { FastifyPluginAsync } from 'fastify';
|
||||
import { FrameStore, IdentityLayer } from '@waggle/core';
|
||||
import { normalizeToMemory } from './memory-center.js';
|
||||
import { IdentityLayer } from '@waggle/core';
|
||||
import { PERSONAL_SCOPE } from '@waggle/memory-contract';
|
||||
import { memoryStatus } from '../memory-status.js';
|
||||
import { buildWorkspaceState } from '../workspace-state.js';
|
||||
import {
|
||||
buildTimeAwareGreeting,
|
||||
@@ -74,7 +75,7 @@ interface HomeBriefing {
|
||||
upNext: UpNextItem[];
|
||||
activeModels?: string[];
|
||||
isFirstRun: boolean;
|
||||
/** J08 (D6): personal-mind memories with status 'unreviewed' (C33 imports). */
|
||||
/** J08 (D6): extracted personal facts the user has not confirmed yet. */
|
||||
needsReviewCount: number;
|
||||
}
|
||||
|
||||
@@ -113,10 +114,8 @@ const MAX_UP_NEXT = 8;
|
||||
const OVERNIGHT_DEFAULT_HOURS = 24;
|
||||
/** Per-schedule cron-history rows to scan for the overnight window. */
|
||||
const CRON_HISTORY_SCAN = 50;
|
||||
/** J08 needs-review scan bound — matches the 200-frame window the Memory
|
||||
* Center list itself fetches (MemoryCenterTab limit=200), so the Home count
|
||||
* always agrees with what the deep-linked "Needs review" view shows. */
|
||||
const NEEDS_REVIEW_SCAN = 200;
|
||||
/** J08 needs-review scan bound: high enough to count every extracted fact. */
|
||||
const NEEDS_REVIEW_SCAN = 10_000;
|
||||
/** Priority boost: each pending/blocked item ranks a workspace as if touched
|
||||
* this much more recently (PRD §12.1 "ranked by recency AND priority"). */
|
||||
const PENDING_BOOST_MS = 3_600_000; // 1 hour per pending item
|
||||
@@ -300,12 +299,12 @@ export const homeRoutes: FastifyPluginAsync = async (server) => {
|
||||
let anyStateForWs = false;
|
||||
|
||||
try {
|
||||
const state = buildWorkspaceState({
|
||||
const state = await buildWorkspaceState({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
memory: server.memory,
|
||||
workspaceId: ws.id,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
});
|
||||
if (state) {
|
||||
anyMemory = true;
|
||||
@@ -376,15 +375,18 @@ export const homeRoutes: FastifyPluginAsync = async (server) => {
|
||||
}
|
||||
}
|
||||
|
||||
// ── J08 (D6): personal-mind memories awaiting review ───────────
|
||||
// Personal-only by design: harvest (C33) stamps status:'unreviewed' on
|
||||
// personal-mind imports, and the deep-linked Memory Center "Needs review"
|
||||
// view is personal-mind-only — a cross-mind count would not match it.
|
||||
// ── J08 (D6): personal memories awaiting review ────────────────
|
||||
// Facts the background extraction learned about the user stay unreviewed
|
||||
// until confirmed. Personal-only by design: the deep-linked Memory Center
|
||||
// "Needs review" view is personal-only — a cross-scope count would not match it.
|
||||
let needsReviewCount = 0;
|
||||
try {
|
||||
for (const f of new FrameStore(server.multiMind.personal).getRecent(NEEDS_REVIEW_SCAN)) {
|
||||
if (normalizeToMemory(f, 'personal').status === 'unreviewed') needsReviewCount++;
|
||||
}
|
||||
const extracted = await server.memory.list({
|
||||
scopes: [PERSONAL_SCOPE],
|
||||
origin: 'extraction',
|
||||
limit: NEEDS_REVIEW_SCAN,
|
||||
});
|
||||
needsReviewCount = extracted.filter((record) => memoryStatus(record) === 'unreviewed').length;
|
||||
} catch (err) {
|
||||
log.warn('briefing: needs-review scan failed', (err as Error).message);
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { MindDB, type WaggleDB } from '@waggle/core';
|
||||
import type { WaggleDB } from '@waggle/core';
|
||||
import { workspaceScope, type MemoryRecord, type MemoryStore } from '@waggle/memory-contract';
|
||||
import { assertSafeSegment } from './validate.js';
|
||||
import { extractProgressItems, type ProgressItem } from './sessions.js';
|
||||
import {
|
||||
buildWorkspaceState,
|
||||
formatWorkspaceStatePrompt,
|
||||
listByImportance,
|
||||
type WorkspaceState,
|
||||
} from '../workspace-state.js';
|
||||
|
||||
@@ -34,30 +34,30 @@ export type { WorkspaceState } from '../workspace-state.js';
|
||||
// ── Workspace Manager interface (minimal, passed via opts) ─────────────
|
||||
|
||||
interface WsManagerLike {
|
||||
get: (id: string) => { id: string; name: string } | null;
|
||||
getMindPath: (id: string) => string;
|
||||
get: (id: string) => { id: string; name: string } | null | undefined;
|
||||
}
|
||||
|
||||
// ── Summary builder (mirrors composeWorkspaceSummary from workspaces.ts) ──
|
||||
|
||||
/** `memories` are ordered most important first (see `listByImportance`). */
|
||||
function buildCompactSummary(
|
||||
frames: Array<{ content: string; importance: string; created_at: string }>,
|
||||
memories: MemoryRecord[],
|
||||
memoryCount: number,
|
||||
sessionCount: number,
|
||||
): string {
|
||||
const parts: string[] = [];
|
||||
|
||||
// What this workspace is about (from important/critical frames)
|
||||
const important = frames.filter(f => f.importance === 'critical' || f.importance === 'important');
|
||||
const workContextFrames = frames.filter(f =>
|
||||
f.content.toLowerCase().includes('project') ||
|
||||
f.content.toLowerCase().includes('workspace') ||
|
||||
f.content.toLowerCase().includes('working on'),
|
||||
// What this workspace is about (from important/critical memories)
|
||||
const important = memories.filter(m => m.importance === 'critical' || m.importance === 'important');
|
||||
const workContextMemories = memories.filter(m =>
|
||||
m.text.toLowerCase().includes('project') ||
|
||||
m.text.toLowerCase().includes('workspace') ||
|
||||
m.text.toLowerCase().includes('working on'),
|
||||
);
|
||||
|
||||
const aboutFrame = important[0] ?? workContextFrames[0] ?? frames[0];
|
||||
if (aboutFrame) {
|
||||
const aboutLine = aboutFrame.content.split('\n')[0].replace(/\.\s*$/, '').trim();
|
||||
const aboutMemory = important[0] ?? workContextMemories[0] ?? memories[0];
|
||||
if (aboutMemory) {
|
||||
const aboutLine = aboutMemory.text.split('\n')[0].replace(/\.\s*$/, '').trim();
|
||||
const aboutText = aboutLine.length > 140 ? aboutLine.slice(0, 137) + '...' : aboutLine;
|
||||
if (aboutText.length > 10) {
|
||||
parts.push(aboutText + '.');
|
||||
@@ -65,7 +65,7 @@ function buildCompactSummary(
|
||||
}
|
||||
|
||||
// Current state (activity level + recency)
|
||||
const mostRecent = frames[0]?.created_at?.slice(0, 10) ?? '';
|
||||
const mostRecent = memories[0]?.createdAt.slice(0, 10) ?? '';
|
||||
const now = new Date();
|
||||
const lastDate = mostRecent ? new Date(mostRecent) : null;
|
||||
const daysSince = lastDate ? Math.floor((now.getTime() - lastDate.getTime()) / (86400 * 1000)) : 0;
|
||||
@@ -221,206 +221,79 @@ export function buildUpcomingSchedules(schedules: CronScheduleLike[], workspaceI
|
||||
|
||||
// ── Main builder ───────────────────────────────────────────────────────
|
||||
|
||||
export function buildWorkspaceNowBlock(opts: {
|
||||
/** Null when the workspace is unknown or has no memories yet. */
|
||||
export async function buildWorkspaceNowBlock(opts: {
|
||||
dataDir: string;
|
||||
workspaceId: string;
|
||||
wsManager: WsManagerLike;
|
||||
/** waggle.db, which holds the workspace's awareness items. */
|
||||
waggleDb: WaggleDB;
|
||||
/** @deprecated Read-only context construction no longer mutates global workspace state. */
|
||||
activateWorkspaceMind?: (id: string) => boolean;
|
||||
memory: MemoryStore;
|
||||
/** Optional cron schedules for upcoming schedule display */
|
||||
cronSchedules?: CronScheduleLike[];
|
||||
}): WorkspaceNowBlock | null {
|
||||
const { dataDir, workspaceId, wsManager, waggleDb } = opts;
|
||||
}): Promise<WorkspaceNowBlock | null> {
|
||||
const { dataDir, workspaceId, wsManager, waggleDb, memory } = opts;
|
||||
|
||||
// Path-traversal guard: workspaceId becomes a path segment below
|
||||
// (getMindPath + dataDir/workspaces/<workspaceId>/sessions). Reject any
|
||||
// value containing illegal characters before it touches the filesystem.
|
||||
// (dataDir/workspaces/<workspaceId>/sessions). Reject any value containing
|
||||
// illegal characters before it touches the filesystem.
|
||||
assertSafeSegment(workspaceId, 'workspaceId');
|
||||
|
||||
const ws = wsManager.get(workspaceId);
|
||||
if (!ws) return null;
|
||||
|
||||
const mindPath = wsManager.getMindPath(workspaceId);
|
||||
if (!fs.existsSync(mindPath)) return null;
|
||||
|
||||
// ── Try structured state first (new path) ────────────────────
|
||||
const structuredState = buildWorkspaceState({
|
||||
const structuredState = await buildWorkspaceState({
|
||||
dataDir,
|
||||
workspaceId,
|
||||
wsManager,
|
||||
waggleDb,
|
||||
memory,
|
||||
});
|
||||
if (!structuredState) return null;
|
||||
|
||||
// Convert structured state → WorkspaceNowBlock for backward compat
|
||||
const activeThreads = structuredState.active.slice(0, 3).map(a => {
|
||||
const ago = formatRelativeTime(new Date(a.dateLastTouched).getTime());
|
||||
const label = a.content.length > 60 ? a.content.slice(0, 57) + '...' : a.content;
|
||||
return `${label} (${ago})`;
|
||||
});
|
||||
|
||||
if (structuredState) {
|
||||
// Convert structured state → WorkspaceNowBlock for backward compat
|
||||
const activeThreads = structuredState.active.slice(0, 3).map(a => {
|
||||
const ago = formatRelativeTime(new Date(a.dateLastTouched).getTime());
|
||||
const label = a.content.length > 60 ? a.content.slice(0, 57) + '...' : a.content;
|
||||
return `${label} (${ago})`;
|
||||
});
|
||||
|
||||
const progressItems: string[] = [];
|
||||
for (const p of structuredState.pending.slice(0, 3)) {
|
||||
progressItems.push(`[task] ${p.content}`);
|
||||
}
|
||||
for (const b of structuredState.blocked.slice(0, 2)) {
|
||||
progressItems.push(`[blocker] ${b.content}`);
|
||||
}
|
||||
for (const c of structuredState.completed.slice(0, 2)) {
|
||||
progressItems.push(`[completed] ${c.content}`);
|
||||
}
|
||||
|
||||
const decisions = structuredState.recentDecisions.slice(0, 3).map(d => d.content);
|
||||
|
||||
// Build summary from mind DB (still needed — structuredState doesn't carry it)
|
||||
let summary = '';
|
||||
let memoryCount = 0;
|
||||
let wsDb: MindDB | null = null;
|
||||
try {
|
||||
wsDb = new MindDB(mindPath);
|
||||
const raw = wsDb.getDatabase();
|
||||
memoryCount = (raw.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number }).cnt;
|
||||
const frames = raw.prepare(
|
||||
`SELECT content, importance, created_at FROM memory_frames
|
||||
WHERE importance != 'deprecated' AND importance != 'temporary'
|
||||
ORDER BY CASE importance
|
||||
WHEN 'critical' THEN 1 WHEN 'important' THEN 2
|
||||
WHEN 'normal' THEN 3 ELSE 4 END,
|
||||
id DESC LIMIT 8`,
|
||||
).all() as Array<{ content: string; importance: string; created_at: string }>;
|
||||
|
||||
const sessDir = path.join(dataDir, 'workspaces', workspaceId, 'sessions');
|
||||
const sessCount = fs.existsSync(sessDir)
|
||||
? fs.readdirSync(sessDir).filter(f => f.endsWith('.jsonl')).length
|
||||
: 0;
|
||||
|
||||
if (frames.length > 0) {
|
||||
summary = buildCompactSummary(frames, memoryCount, sessCount);
|
||||
}
|
||||
wsDb.close();
|
||||
wsDb = null;
|
||||
} catch {
|
||||
if (wsDb) { try { wsDb.close(); } catch { /* */ } }
|
||||
}
|
||||
|
||||
// Compute last active from active threads or structured state
|
||||
const lastActiveThread = structuredState.active[0]?.dateLastTouched ?? null;
|
||||
const greeting = buildTimeAwareGreeting(lastActiveThread, { frameCount: memoryCount });
|
||||
const pendingTasks = extractPendingTasks(progressItems);
|
||||
const upcomingSchedules = buildUpcomingSchedules(opts.cronSchedules ?? [], workspaceId);
|
||||
|
||||
return {
|
||||
workspaceName: ws.name as string,
|
||||
summary,
|
||||
recentDecisions: decisions,
|
||||
activeThreads,
|
||||
progressItems: progressItems.slice(0, 5),
|
||||
nextActions: structuredState.nextActions.slice(0, 3),
|
||||
greeting,
|
||||
pendingTasks,
|
||||
upcomingSchedules,
|
||||
structuredState,
|
||||
};
|
||||
const progressItems: string[] = [];
|
||||
for (const p of structuredState.pending.slice(0, 3)) {
|
||||
progressItems.push(`[task] ${p.content}`);
|
||||
}
|
||||
for (const b of structuredState.blocked.slice(0, 2)) {
|
||||
progressItems.push(`[blocker] ${b.content}`);
|
||||
}
|
||||
for (const c of structuredState.completed.slice(0, 2)) {
|
||||
progressItems.push(`[completed] ${c.content}`);
|
||||
}
|
||||
|
||||
// ── Fallback: legacy path (no structured state available) ────
|
||||
const decisions = structuredState.recentDecisions.slice(0, 3).map(d => d.content);
|
||||
|
||||
// Summary from workspace memory (structuredState doesn't carry it)
|
||||
let summary = '';
|
||||
let decisions: string[] = [];
|
||||
let memoryCount = 0;
|
||||
let wsDb: MindDB | null = null;
|
||||
|
||||
try {
|
||||
wsDb = new MindDB(mindPath);
|
||||
const raw = wsDb.getDatabase();
|
||||
|
||||
memoryCount = (raw.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number }).cnt;
|
||||
if (memoryCount === 0) {
|
||||
wsDb.close();
|
||||
return null;
|
||||
}
|
||||
|
||||
const frames = raw.prepare(
|
||||
`SELECT content, importance, created_at FROM memory_frames
|
||||
WHERE importance != 'deprecated' AND importance != 'temporary'
|
||||
ORDER BY CASE importance
|
||||
WHEN 'critical' THEN 1 WHEN 'important' THEN 2
|
||||
WHEN 'normal' THEN 3 ELSE 4 END,
|
||||
id DESC LIMIT 8`,
|
||||
).all() as Array<{ content: string; importance: string; created_at: string }>;
|
||||
const scope = workspaceScope(workspaceId);
|
||||
memoryCount = await memory.count({ scopes: [scope] });
|
||||
const memories = await listByImportance(memory, scope, 8);
|
||||
|
||||
const sessDir = path.join(dataDir, 'workspaces', workspaceId, 'sessions');
|
||||
const sessCount = fs.existsSync(sessDir)
|
||||
? fs.readdirSync(sessDir).filter(f => f.endsWith('.jsonl')).length
|
||||
: 0;
|
||||
|
||||
if (frames.length > 0) {
|
||||
summary = buildCompactSummary(frames, memoryCount, sessCount);
|
||||
if (memories.length > 0) {
|
||||
summary = buildCompactSummary(memories, memoryCount, sessCount);
|
||||
}
|
||||
|
||||
// Extract decisions inline (legacy)
|
||||
const decisionFrames = raw.prepare(
|
||||
`SELECT content FROM memory_frames
|
||||
WHERE importance != 'deprecated' AND importance != 'temporary'
|
||||
AND (content LIKE 'Decision%' OR content LIKE '%decided%'
|
||||
OR content LIKE '%decision made%' OR content LIKE '%chose %'
|
||||
OR content LIKE '%selected %' OR content LIKE '%agreed %'
|
||||
OR importance = 'critical')
|
||||
ORDER BY id DESC LIMIT 5`,
|
||||
).all() as Array<{ content: string }>;
|
||||
|
||||
decisions = decisionFrames.slice(0, 3).map(f => {
|
||||
const firstLine = f.content.split('\n')[0];
|
||||
const sentenceMatch = firstLine.match(/^(.+?\.\s)(?=[A-Z])/);
|
||||
const text = sentenceMatch
|
||||
? sentenceMatch[1].trim()
|
||||
: (firstLine.length > 150 ? firstLine.slice(0, 147) + '...' : firstLine);
|
||||
return text.replace(/\.\s*$/, '');
|
||||
});
|
||||
|
||||
wsDb.close();
|
||||
wsDb = null;
|
||||
} catch {
|
||||
if (wsDb) { try { wsDb.close(); } catch { /* ignore */ } }
|
||||
return null;
|
||||
// Non-blocking — the block renders without a summary
|
||||
}
|
||||
|
||||
// Extract progress items (cap at 5)
|
||||
const sessionsDir = path.join(dataDir, 'workspaces', workspaceId, 'sessions');
|
||||
let rawProgress: ProgressItem[] = [];
|
||||
if (fs.existsSync(sessionsDir)) {
|
||||
try { rawProgress = extractProgressItems(sessionsDir, 5); } catch { /* */ }
|
||||
}
|
||||
const progressItems = rawProgress.slice(0, 5).map(p => `[${p.type}] ${p.content}`);
|
||||
|
||||
// Derive next actions
|
||||
const nextActions: string[] = [];
|
||||
for (const p of rawProgress) {
|
||||
if (p.type === 'blocker' && nextActions.length < 3) nextActions.push(`Resolve: ${p.content}`);
|
||||
}
|
||||
for (const p of rawProgress) {
|
||||
if (p.type === 'task' && nextActions.length < 3) nextActions.push(p.content);
|
||||
}
|
||||
if (nextActions.length === 0 && decisions.length > 0) {
|
||||
nextActions.push('Review recent decisions and determine next steps');
|
||||
}
|
||||
|
||||
// Legacy path: derive last-active from session files
|
||||
let legacyLastActive: string | null = null;
|
||||
if (fs.existsSync(sessionsDir)) {
|
||||
try {
|
||||
const files = fs.readdirSync(sessionsDir).filter(f => f.endsWith('.jsonl'));
|
||||
let maxMtime = 0;
|
||||
for (const file of files) {
|
||||
const stat = fs.statSync(path.join(sessionsDir, file));
|
||||
if (stat.mtimeMs > maxMtime) maxMtime = stat.mtimeMs;
|
||||
}
|
||||
if (maxMtime > 0) legacyLastActive = new Date(maxMtime).toISOString();
|
||||
} catch { /* */ }
|
||||
}
|
||||
|
||||
const greeting = buildTimeAwareGreeting(legacyLastActive, { frameCount: memoryCount });
|
||||
// Compute last active from active threads or structured state
|
||||
const lastActiveThread = structuredState.active[0]?.dateLastTouched ?? null;
|
||||
const greeting = buildTimeAwareGreeting(lastActiveThread, { frameCount: memoryCount });
|
||||
const pendingTasks = extractPendingTasks(progressItems);
|
||||
const upcomingSchedules = buildUpcomingSchedules(opts.cronSchedules ?? [], workspaceId);
|
||||
|
||||
@@ -428,12 +301,13 @@ export function buildWorkspaceNowBlock(opts: {
|
||||
workspaceName: ws.name as string,
|
||||
summary,
|
||||
recentDecisions: decisions,
|
||||
activeThreads: [],
|
||||
progressItems,
|
||||
nextActions: nextActions.slice(0, 3),
|
||||
activeThreads,
|
||||
progressItems: progressItems.slice(0, 5),
|
||||
nextActions: structuredState.nextActions.slice(0, 3),
|
||||
greeting,
|
||||
pendingTasks,
|
||||
upcomingSchedules,
|
||||
structuredState,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import Database from 'better-sqlite3';
|
||||
import type { FastifyPluginAsync } from 'fastify';
|
||||
import { z } from 'zod';
|
||||
import { MindDB, createFileStore, reconcileFtsIndex } from '@waggle/core';
|
||||
import { AwarenessLayer, createFileStore, workspaceDataScope } from '@waggle/core';
|
||||
import { workspaceScope, type MemoryRecord, type MemoryStore } from '@waggle/memory-contract';
|
||||
import { assertSafeSegment } from './validate.js';
|
||||
import { validateBody } from '../../validate-body.js';
|
||||
|
||||
@@ -49,7 +49,13 @@ const updateWorkspaceSchema = z.object({
|
||||
}).strict();
|
||||
import { extractProgressItems, type ProgressItem } from './sessions.js';
|
||||
import { readFileRegistry, type FileRegistryEntry } from './ingest.js';
|
||||
import { buildWorkspaceState, type WorkspaceState, type StateItem } from '../workspace-state.js';
|
||||
import {
|
||||
buildWorkspaceState,
|
||||
listByImportance,
|
||||
listDecisions,
|
||||
type WorkspaceState,
|
||||
type StateItem,
|
||||
} from '../workspace-state.js';
|
||||
import { buildTimeAwareGreeting, buildUpcomingSchedules } from './workspace-context.js';
|
||||
import { emitAuditEvent, getAuditDb } from './events.js';
|
||||
import { createLogger } from '../logger.js';
|
||||
@@ -68,34 +74,34 @@ function isValidModelId(model: string): boolean {
|
||||
|
||||
/**
|
||||
* A6: Compose a structured workspace summary — the "return reward moment."
|
||||
* Produces a narrative summary with: what this workspace is about, recent state, key decisions.
|
||||
* Produces a narrative summary with: what this workspace is about and recent state.
|
||||
* `memories` are ordered most important first (see `listByImportance`).
|
||||
*/
|
||||
function composeWorkspaceSummary(
|
||||
frames: Array<{ content: string; importance: string; created_at: string }>,
|
||||
memories: MemoryRecord[],
|
||||
memoryCount: number,
|
||||
decisions: Array<{ content: string; created_at: string }>,
|
||||
sessionCount: number,
|
||||
): string {
|
||||
const parts: string[] = [];
|
||||
|
||||
// ── What this workspace is about (from important/critical frames) ──
|
||||
const important = frames.filter(f => f.importance === 'critical' || f.importance === 'important');
|
||||
const workContextFrames = frames.filter(f =>
|
||||
f.content.toLowerCase().includes('project') ||
|
||||
f.content.toLowerCase().includes('workspace') ||
|
||||
f.content.toLowerCase().includes('working on'),
|
||||
// ── What this workspace is about (from important/critical memories) ──
|
||||
const important = memories.filter(m => m.importance === 'critical' || m.importance === 'important');
|
||||
const workContextMemories = memories.filter(m =>
|
||||
m.text.toLowerCase().includes('project') ||
|
||||
m.text.toLowerCase().includes('workspace') ||
|
||||
m.text.toLowerCase().includes('working on'),
|
||||
);
|
||||
|
||||
// Find the best "about" frame
|
||||
const aboutFrame = important[0] ?? workContextFrames[0] ?? frames[0];
|
||||
const aboutLine = aboutFrame.content.split('\n')[0].replace(/\.\s*$/, '').trim();
|
||||
// Find the best "about" memory
|
||||
const aboutMemory = important[0] ?? workContextMemories[0] ?? memories[0];
|
||||
const aboutLine = aboutMemory.text.split('\n')[0].replace(/\.\s*$/, '').trim();
|
||||
const aboutText = aboutLine.length > 140 ? aboutLine.slice(0, 137) + '...' : aboutLine;
|
||||
if (aboutText.length > 10) {
|
||||
parts.push(aboutText + '.');
|
||||
}
|
||||
|
||||
// ── Current state (activity level + recency) ──
|
||||
const mostRecent = frames[0]?.created_at?.slice(0, 10) ?? '';
|
||||
const mostRecent = memories[0]?.createdAt.slice(0, 10) ?? '';
|
||||
const now = new Date();
|
||||
const lastDate = mostRecent ? new Date(mostRecent) : null;
|
||||
const daysSince = lastDate ? Math.floor((now.getTime() - lastDate.getTime()) / (86400 * 1000)) : 0;
|
||||
@@ -190,25 +196,14 @@ const TEMPLATE_WELCOME: Record<string, string> = {
|
||||
};
|
||||
|
||||
/**
|
||||
* Wave R (Lane B): cheap per-workspace memory count for the list route. Opens a
|
||||
* READONLY raw sqlite connection (no MindDB schema-init, no sqlite-vec load) and
|
||||
* runs a single COUNT(*) — far lighter than the full MindDB the /:id/context
|
||||
* route uses, and it never touches the MultiMindCache, so the LRU-eviction
|
||||
* hazard that originally kept memoryCount off this route does not apply. Returns
|
||||
* undefined on any error (missing file / table / lock) so the card omits the
|
||||
* count rather than fabricating a 0.
|
||||
* Wave R (Lane B): per-workspace memory count for the list route. Returns
|
||||
* undefined on any error so the card omits the count rather than fabricating a 0.
|
||||
*/
|
||||
function countWorkspaceMemories(mindPath: string): number | undefined {
|
||||
if (!fs.existsSync(mindPath)) return undefined;
|
||||
let db: import('better-sqlite3').Database | undefined;
|
||||
async function countWorkspaceMemories(memory: MemoryStore, workspaceId: string): Promise<number | undefined> {
|
||||
try {
|
||||
db = new Database(mindPath, { readonly: true, fileMustExist: true });
|
||||
const row = db.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number };
|
||||
return row.cnt;
|
||||
return await memory.count({ scopes: [workspaceScope(workspaceId)] });
|
||||
} catch {
|
||||
return undefined;
|
||||
} finally {
|
||||
try { db?.close(); } catch { /* already closed / never opened */ }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -273,21 +268,18 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
||||
}
|
||||
// Wave F (fix 1a) + Wave R (Lane B): enrich each row with real card signal.
|
||||
// - sessionCount: a readdir of the same sessions dir /:id/context counts.
|
||||
// - memoryCount: a readonly COUNT(*) over the workspace mind (countWorkspace-
|
||||
// Memories) — this is the R-round "living identity" unlock. It opens a
|
||||
// lightweight readonly connection per row (no schema init, no vec load) and
|
||||
// never touches the MultiMindCache, sidestepping the LRU hazard the prior
|
||||
// comment warned about. Bounded to the user's own handful of workspaces.
|
||||
// - memoryCount: the workspace's memory count — the R-round "living
|
||||
// identity" unlock. Bounded to the user's own handful of workspaces.
|
||||
// - lastSessionTitle: the newest session's title, for the card's "Last: …"
|
||||
// activity body.
|
||||
// Every enrichment omits its field on any error — never a fabricated 0/string.
|
||||
return workspaces.map((ws) => {
|
||||
return Promise.all(workspaces.map(async (ws) => {
|
||||
try {
|
||||
const sessionsDir = path.join(server.localConfig.dataDir, 'workspaces', ws.id, 'sessions');
|
||||
const sessionCount = fs.existsSync(sessionsDir)
|
||||
? fs.readdirSync(sessionsDir).filter((f) => f.endsWith('.jsonl')).length
|
||||
: 0;
|
||||
const memoryCount = countWorkspaceMemories(server.workspaceManager.getMindPath(ws.id));
|
||||
const memoryCount = await countWorkspaceMemories(server.memory, ws.id);
|
||||
const lastSessionTitle = readLastSessionTitle(sessionsDir);
|
||||
return {
|
||||
...ws,
|
||||
@@ -298,7 +290,7 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
||||
} catch {
|
||||
return ws;
|
||||
}
|
||||
});
|
||||
}));
|
||||
});
|
||||
|
||||
// POST /api/workspaces — create workspace
|
||||
@@ -469,32 +461,27 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
||||
});
|
||||
}
|
||||
|
||||
// M2-5: Seed template starter memory as visible Memory frames
|
||||
// M2-5: Seed template starter memory as visible workspace memories
|
||||
if (resolvedTemplateId && resolvedTemplateId !== 'blank') {
|
||||
try {
|
||||
const { BUILT_IN_TEMPLATES } = await import('./workspace-templates.js');
|
||||
const tpl = BUILT_IN_TEMPLATES.find(t => t.id === resolvedTemplateId);
|
||||
if (tpl?.starterMemory?.length) {
|
||||
const mindPath = server.workspaceManager.getMindPath(ws.id);
|
||||
const wsDb = new MindDB(mindPath);
|
||||
const raw = wsDb.getDatabase();
|
||||
// Create a synthetic session for onboarding frames (FK enforced)
|
||||
raw.prepare(
|
||||
"INSERT OR IGNORE INTO sessions (gop_id, project_id, status, summary) VALUES (?, ?, 'closed', 'Workspace template starter content')"
|
||||
).run(`onboarding-${ws.id}`, ws.id);
|
||||
// Seed each starter memory as an I-frame
|
||||
const insert = raw.prepare(
|
||||
"INSERT INTO memory_frames (frame_type, gop_id, t, content, importance, source) VALUES ('I', ?, ?, ?, 'normal', 'system')"
|
||||
);
|
||||
for (let i = 0; i < tpl.starterMemory.length; i++) {
|
||||
insert.run(`onboarding-${ws.id}`, i, tpl.starterMemory[i]);
|
||||
const starterMemory = BUILT_IN_TEMPLATES.find(t => t.id === resolvedTemplateId)?.starterMemory ?? [];
|
||||
// Written in the background: the first memory write may wait for the
|
||||
// embedding model to load, and workspace creation must not.
|
||||
void (async () => {
|
||||
for (const text of starterMemory) {
|
||||
if (!server.workspaceManager.get(ws.id)) return; // deleted meanwhile
|
||||
await server.memory.remember({
|
||||
scope: workspaceScope(ws.id),
|
||||
text,
|
||||
kind: 'note',
|
||||
source: 'system',
|
||||
origin: 'workspace-template',
|
||||
});
|
||||
}
|
||||
// These raw INSERTs bypass FrameStore.indexFts(), so the starter frames
|
||||
// would be present in memory_frames but invisible to FTS5 keyword search.
|
||||
// Reconcile the FTS index to make them searchable.
|
||||
reconcileFtsIndex(wsDb);
|
||||
wsDb.close();
|
||||
}
|
||||
})().catch((err: unknown) => {
|
||||
log.warn(`[waggle] Starter memory seeding failed:`, (err as Error).message);
|
||||
});
|
||||
} catch (err) {
|
||||
// Non-blocking — workspace creation succeeds even if memory seeding fails
|
||||
log.warn(`[waggle] Starter memory seeding failed:`, (err as Error).message);
|
||||
@@ -544,72 +531,46 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
||||
let recentMemories: Array<{ content: string; importance: string; source: string; date: string }> = [];
|
||||
let recentDecisions: Array<{ content: string; source: string; date: string }> = [];
|
||||
|
||||
const mindPath = server.workspaceManager.getMindPath(id);
|
||||
if (fs.existsSync(mindPath)) {
|
||||
try {
|
||||
// Activate workspace mind to access its data
|
||||
server.agentState.activateWorkspaceMind(id);
|
||||
try {
|
||||
// Opening a workspace makes it the active one
|
||||
server.agentState.activateWorkspaceMind(id);
|
||||
|
||||
// Read workspace memory stats and recent frames
|
||||
const wsDb = new MindDB(mindPath);
|
||||
const raw = wsDb.getDatabase();
|
||||
const scope = workspaceScope(id);
|
||||
memoryCount = await server.memory.count({ scopes: [scope] });
|
||||
|
||||
memoryCount = (raw.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number }).cnt;
|
||||
// Get recent important memories for the summary
|
||||
// D2: Order by importance first (matching A3 preloaded-context fix), then recency
|
||||
const memories = await listByImportance(server.memory, scope, 8);
|
||||
|
||||
// Get recent important memories for the summary
|
||||
// D2: Order by importance first (matching A3 preloaded-context fix), then recency
|
||||
const frames = raw.prepare(
|
||||
`SELECT content, importance, source, created_at FROM memory_frames
|
||||
WHERE importance != 'deprecated' AND importance != 'temporary'
|
||||
ORDER BY CASE importance
|
||||
WHEN 'critical' THEN 1 WHEN 'important' THEN 2
|
||||
WHEN 'normal' THEN 3 ELSE 4 END,
|
||||
id DESC LIMIT 8`
|
||||
).all() as Array<{ content: string; importance: string; source: string; created_at: string }>;
|
||||
recentMemories = memories.map(m => ({
|
||||
content: m.text.slice(0, 200),
|
||||
importance: m.importance,
|
||||
source: m.source,
|
||||
date: m.createdAt.slice(0, 10),
|
||||
}));
|
||||
|
||||
recentMemories = frames.map(f => ({
|
||||
content: f.content.slice(0, 200),
|
||||
importance: f.importance,
|
||||
source: f.source,
|
||||
date: f.created_at?.slice(0, 10) ?? 'unknown',
|
||||
}));
|
||||
recentDecisions = (await listDecisions(server.memory, scope, 5)).map(m => {
|
||||
const firstLine = m.text.split('\n')[0];
|
||||
const sentenceMatch = firstLine.match(/^(.+?\.\s)(?=[A-Z])/);
|
||||
const text = sentenceMatch
|
||||
? sentenceMatch[1].trim()
|
||||
: (firstLine.length > 150 ? firstLine.slice(0, 147) + '...' : firstLine);
|
||||
return {
|
||||
content: text.replace(/\.\s*$/, ''),
|
||||
source: m.source,
|
||||
date: m.createdAt.slice(0, 10),
|
||||
};
|
||||
});
|
||||
|
||||
// Extract decision-like memories (moved before summary so we can pass them)
|
||||
const decisionFrames = raw.prepare(
|
||||
`SELECT content, source, created_at FROM memory_frames
|
||||
WHERE importance != 'deprecated' AND importance != 'temporary'
|
||||
AND (content LIKE 'Decision%' OR content LIKE '%decided%'
|
||||
OR content LIKE '%decision made%' OR content LIKE '%chose %'
|
||||
OR content LIKE '%selected %' OR content LIKE '%agreed %'
|
||||
OR importance = 'critical')
|
||||
ORDER BY id DESC LIMIT 5`
|
||||
).all() as Array<{ content: string; source: string; created_at: string }>;
|
||||
|
||||
recentDecisions = decisionFrames.map(f => {
|
||||
const firstLine = f.content.split('\n')[0];
|
||||
const sentenceMatch = firstLine.match(/^(.+?\.\s)(?=[A-Z])/);
|
||||
const text = sentenceMatch
|
||||
? sentenceMatch[1].trim()
|
||||
: (firstLine.length > 150 ? firstLine.slice(0, 147) + '...' : firstLine);
|
||||
return {
|
||||
content: text.replace(/\.\s*$/, ''),
|
||||
source: f.source,
|
||||
date: f.created_at?.slice(0, 10) ?? 'unknown',
|
||||
};
|
||||
});
|
||||
|
||||
// A6: Build structured summary with decisions + session count
|
||||
if (frames.length > 0) {
|
||||
// Count sessions from filesystem (we'll have accurate count later, estimate here)
|
||||
const sessDir = path.join(server.localConfig.dataDir, 'workspaces', id, 'sessions');
|
||||
const sessCount = fs.existsSync(sessDir) ? fs.readdirSync(sessDir).filter(f => f.endsWith('.jsonl')).length : 0;
|
||||
summary = composeWorkspaceSummary(frames, memoryCount, decisionFrames, sessCount);
|
||||
}
|
||||
|
||||
wsDb.close();
|
||||
} catch {
|
||||
// If workspace mind can't be read, continue with empty context
|
||||
// A6: Build structured summary with session count
|
||||
if (memories.length > 0) {
|
||||
// Count sessions from filesystem (we'll have accurate count later, estimate here)
|
||||
const sessDir = path.join(server.localConfig.dataDir, 'workspaces', id, 'sessions');
|
||||
const sessCount = fs.existsSync(sessDir) ? fs.readdirSync(sessDir).filter(f => f.endsWith('.jsonl')).length : 0;
|
||||
summary = composeWorkspaceSummary(memories, memoryCount, sessCount);
|
||||
}
|
||||
} catch {
|
||||
// If workspace memory can't be read, continue with empty context
|
||||
}
|
||||
|
||||
// ── Gather session info ──────────────────────────────────
|
||||
@@ -715,12 +676,12 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
||||
// ── Structured workspace state (Slice 6) ──────────────
|
||||
let workspaceState: WorkspaceState | null = null;
|
||||
try {
|
||||
workspaceState = buildWorkspaceState({
|
||||
workspaceState = await buildWorkspaceState({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
memory: server.memory,
|
||||
workspaceId: id,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
});
|
||||
} catch { /* non-blocking */ }
|
||||
|
||||
@@ -775,9 +736,9 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
||||
} catch { /* non-blocking — cron store may not be available */ }
|
||||
|
||||
// ── Wave 6.5 (DISABLED): Cross-workspace relevance hints ──
|
||||
// Security review (cowork/Code-Review_MultiMind_April-2026.md Critical #1) flagged
|
||||
// the original implementation as a privacy leak: it iterated every workspace the
|
||||
// user owns, opened each MindDB directly, and returned 80-char content snippets in
|
||||
// A security review (Critical #1) flagged the original implementation as a
|
||||
// privacy leak: it iterated every workspace the user owns, read each
|
||||
// workspace's memory directly, and returned 80-char content snippets in
|
||||
// the HTTP response with NO grant check and no approval gate. That is a complete
|
||||
// bypass of the `read_other_workspace` agent-tool approval contract.
|
||||
//
|
||||
@@ -841,12 +802,12 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
||||
|
||||
let state: WorkspaceState | null = null;
|
||||
try {
|
||||
state = buildWorkspaceState({
|
||||
state = await buildWorkspaceState({
|
||||
dataDir: server.localConfig.dataDir,
|
||||
waggleDb: server.waggleDb,
|
||||
memory: server.memory,
|
||||
workspaceId: id,
|
||||
wsManager: server.workspaceManager,
|
||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
||||
});
|
||||
} catch (err) {
|
||||
log.warn(`state build failed for ${id}:`, (err as Error).message);
|
||||
@@ -1024,10 +985,21 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
||||
if (!existing) {
|
||||
return reply.status(404).send({ error: 'Workspace not found' });
|
||||
}
|
||||
// A6: Close workspace mind DB before filesystem deletion to prevent EBUSY
|
||||
server.agentState.closeWorkspaceMind(request.params.id);
|
||||
server.workspaceManager.delete(request.params.id);
|
||||
emitAuditEvent(server, { workspaceId: request.params.id, eventType: 'workspace_delete' });
|
||||
const id = request.params.id;
|
||||
// Its memories and awareness items live outside the workspace directory.
|
||||
// Forget them first: a workspace created later under the same name gets
|
||||
// the same id and must not inherit them.
|
||||
try {
|
||||
await server.memory.forgetMatching({ scopes: [workspaceScope(id)] });
|
||||
new AwarenessLayer(server.waggleDb, workspaceDataScope(id)).clear();
|
||||
} catch (err) {
|
||||
log.warn(`could not forget the memory of workspace ${id}:`, (err as Error).message);
|
||||
return reply.status(500).send({ error: 'Could not delete the workspace memory; the workspace was kept' });
|
||||
}
|
||||
// A6: Release the workspace before filesystem deletion to prevent EBUSY
|
||||
server.agentState.releaseWorkspace(id);
|
||||
server.workspaceManager.delete(id);
|
||||
emitAuditEvent(server, { workspaceId: id, eventType: 'workspace_delete' });
|
||||
return reply.status(204).send();
|
||||
});
|
||||
|
||||
@@ -1045,21 +1017,16 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
||||
}
|
||||
|
||||
// ── Gather memories ──────────────────────────────────────────
|
||||
const memories: Array<{ content: string; importance: string; created_at: string; frame_type?: string }> = [];
|
||||
const mindPath = server.workspaceManager.getMindPath(id);
|
||||
if (fs.existsSync(mindPath)) {
|
||||
try {
|
||||
const wsDb = new MindDB(mindPath);
|
||||
const raw = wsDb.getDatabase();
|
||||
const frames = raw.prepare(
|
||||
`SELECT content, importance, created_at, frame_type FROM memory_frames
|
||||
WHERE importance != 'deprecated'
|
||||
ORDER BY id DESC LIMIT 500`
|
||||
).all() as Array<{ content: string; importance: string; created_at: string; frame_type: string }>;
|
||||
memories.push(...frames);
|
||||
wsDb.close();
|
||||
} catch { /* non-blocking */ }
|
||||
}
|
||||
const memories: Array<{ content: string; importance: string; created_at: string; kind: string }> = [];
|
||||
try {
|
||||
const records = await server.memory.list({ scopes: [workspaceScope(id)], limit: 500 });
|
||||
memories.push(...records.map(r => ({
|
||||
content: r.text,
|
||||
importance: r.importance,
|
||||
created_at: r.createdAt,
|
||||
kind: r.kind,
|
||||
})));
|
||||
} catch { /* non-blocking */ }
|
||||
|
||||
// ── Gather pinned items ──────────────────────────────────────
|
||||
let pinnedItems: Array<{ id: string; messageContent: string; pinnedAt: string; label?: string }> = [];
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Workspace State — structured current-state reconstruction for a workspace.
|
||||
*
|
||||
* Assembles typed, classified items from memory frames, session files,
|
||||
* Assembles typed, classified items from workspace memory, session files,
|
||||
* and awareness layer into a coherent model of "what's going on now."
|
||||
*
|
||||
* Design principles:
|
||||
@@ -12,7 +12,13 @@
|
||||
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { AwarenessLayer, MindDB, WaggleDB, workspaceDataScope } from '@waggle/core';
|
||||
import { AwarenessLayer, WaggleDB, workspaceDataScope } from '@waggle/core';
|
||||
import {
|
||||
workspaceScope,
|
||||
type MemoryRecord,
|
||||
type MemoryScope,
|
||||
type MemoryStore,
|
||||
} from '@waggle/memory-contract';
|
||||
import {
|
||||
extractProgressItems,
|
||||
extractOpenQuestions,
|
||||
@@ -72,42 +78,62 @@ export function computeFreshness(dateStr: string): Freshness {
|
||||
return 'stale';
|
||||
}
|
||||
|
||||
// ── Decision extraction from memory frames ─────────────────────────────
|
||||
// ── Workspace memory reads ─────────────────────────────────────────────
|
||||
|
||||
interface DecisionFrame {
|
||||
id: number;
|
||||
content: string;
|
||||
created_at: string;
|
||||
/** Recent memories scanned for decisions. */
|
||||
const DECISION_SCAN = 200;
|
||||
/** Wording that marks a note as a decision when its kind does not. */
|
||||
const DECISION_WORDING = /^decision|decided|decision made|chose |selected |agreed /i;
|
||||
|
||||
/** A decision: its kind says so, it is critical, or its wording records one. */
|
||||
function isDecision(record: MemoryRecord): boolean {
|
||||
return record.kind === 'decision' || record.importance === 'critical' || DECISION_WORDING.test(record.text);
|
||||
}
|
||||
|
||||
function extractDecisionItems(
|
||||
raw: ReturnType<MindDB['getDatabase']>,
|
||||
/** The newest decisions of a scope, temporary memories excluded. */
|
||||
export async function listDecisions(
|
||||
memory: MemoryStore,
|
||||
scope: MemoryScope,
|
||||
max: number,
|
||||
): StateItem[] {
|
||||
const frames = raw.prepare(
|
||||
`SELECT id, content, created_at FROM memory_frames
|
||||
WHERE importance != 'deprecated' AND importance != 'temporary'
|
||||
AND (content LIKE 'Decision%' OR content LIKE '%decided%'
|
||||
OR content LIKE '%decision made%' OR content LIKE '%chose %'
|
||||
OR content LIKE '%selected %' OR content LIKE '%agreed %'
|
||||
OR importance = 'critical')
|
||||
ORDER BY id DESC LIMIT ?`,
|
||||
).all(max + 2) as DecisionFrame[];
|
||||
): Promise<MemoryRecord[]> {
|
||||
const recent = await memory.list({ scopes: [scope], minImportance: 'normal', limit: DECISION_SCAN });
|
||||
return recent.filter(isDecision).slice(0, max);
|
||||
}
|
||||
|
||||
return frames.slice(0, max).map(f => {
|
||||
const firstLine = f.content.split('\n')[0];
|
||||
const sentenceMatch = firstLine.match(/^(.+?\.\s)(?=[A-Z])/);
|
||||
const text = sentenceMatch ? sentenceMatch[1] : firstLine;
|
||||
/**
|
||||
* Up to `limit` memories of a scope, most important first and newest first
|
||||
* within one importance level; temporary memories are excluded.
|
||||
*/
|
||||
export async function listByImportance(
|
||||
memory: MemoryStore,
|
||||
scope: MemoryScope,
|
||||
limit: number,
|
||||
): Promise<MemoryRecord[]> {
|
||||
const records: MemoryRecord[] = [];
|
||||
for (const level of ['critical', 'important', 'normal'] as const) {
|
||||
if (records.length >= limit) break;
|
||||
// Every memory above `level` is already in `records`, so the newest
|
||||
// `limit + records.length` memories at or above `level` contain the
|
||||
// newest `limit` memories at `level`.
|
||||
const candidates = await memory.list({ scopes: [scope], minImportance: level, limit: limit + records.length });
|
||||
records.push(...candidates.filter(r => r.importance === level).slice(0, limit - records.length));
|
||||
}
|
||||
return records;
|
||||
}
|
||||
|
||||
return {
|
||||
// Strip cursor/markdown artifacts (leaking `|` pipes etc.) + tail-truncate.
|
||||
content: sanitizeExtracted(text, 150),
|
||||
freshness: computeFreshness(f.created_at),
|
||||
source: 'memory' as StateSource,
|
||||
sourceId: String(f.id),
|
||||
dateLastTouched: f.created_at.slice(0, 10),
|
||||
};
|
||||
});
|
||||
function toDecisionItem(record: MemoryRecord): StateItem {
|
||||
const firstLine = record.text.split('\n')[0];
|
||||
const sentenceMatch = firstLine.match(/^(.+?\.\s)(?=[A-Z])/);
|
||||
const text = sentenceMatch ? sentenceMatch[1] : firstLine;
|
||||
|
||||
return {
|
||||
// Strip cursor/markdown artifacts (leaking `|` pipes etc.) + tail-truncate.
|
||||
content: sanitizeExtracted(text, 150),
|
||||
freshness: computeFreshness(record.createdAt),
|
||||
source: 'memory',
|
||||
sourceId: record.id,
|
||||
dateLastTouched: record.createdAt.slice(0, 10),
|
||||
};
|
||||
}
|
||||
|
||||
// ── Active items from awareness layer ──────────────────────────────────
|
||||
@@ -209,8 +235,7 @@ function deriveNextActions(state: Omit<WorkspaceState, 'nextActions'>, max: numb
|
||||
// ── Main builder ───────────────────────────────────────────────────────
|
||||
|
||||
interface WsManagerLike {
|
||||
get: (id: string) => { id: string; name: string } | null;
|
||||
getMindPath: (id: string) => string;
|
||||
get: (id: string) => { id: string; name: string } | null | undefined;
|
||||
}
|
||||
|
||||
export interface BuildWorkspaceStateOpts {
|
||||
@@ -219,40 +244,25 @@ export interface BuildWorkspaceStateOpts {
|
||||
wsManager: WsManagerLike;
|
||||
/** waggle.db, which holds the workspace's awareness items. */
|
||||
waggleDb: WaggleDB;
|
||||
/** @deprecated Read-only state construction no longer mutates global workspace state. */
|
||||
activateWorkspaceMind?: (id: string) => boolean;
|
||||
memory: MemoryStore;
|
||||
}
|
||||
|
||||
export function buildWorkspaceState(opts: BuildWorkspaceStateOpts): WorkspaceState | null {
|
||||
const { dataDir, workspaceId, wsManager, waggleDb } = opts;
|
||||
/** Null when the workspace is unknown, has no memories yet, or its memory cannot be read. */
|
||||
export async function buildWorkspaceState(opts: BuildWorkspaceStateOpts): Promise<WorkspaceState | null> {
|
||||
const { dataDir, workspaceId, wsManager, waggleDb, memory } = opts;
|
||||
|
||||
const ws = wsManager.get(workspaceId);
|
||||
if (!ws) return null;
|
||||
|
||||
const mindPath = wsManager.getMindPath(workspaceId);
|
||||
if (!fs.existsSync(mindPath)) return null;
|
||||
|
||||
// ── Memory-sourced state ─────────────────────────────────────────
|
||||
let recentDecisions: StateItem[] = [];
|
||||
let awarenessItems: StateItem[] = [];
|
||||
let wsDb: MindDB | null = null;
|
||||
|
||||
try {
|
||||
wsDb = new MindDB(mindPath);
|
||||
const raw = wsDb.getDatabase();
|
||||
|
||||
const memoryCount = (raw.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number }).cnt;
|
||||
if (memoryCount === 0) {
|
||||
wsDb.close();
|
||||
return null;
|
||||
}
|
||||
|
||||
recentDecisions = extractDecisionItems(raw, 5);
|
||||
|
||||
wsDb.close();
|
||||
wsDb = null;
|
||||
const scope = workspaceScope(workspaceId);
|
||||
if (await memory.count({ scopes: [scope] }) === 0) return null;
|
||||
recentDecisions = (await listDecisions(memory, scope, 5)).map(toDecisionItem);
|
||||
} catch {
|
||||
if (wsDb) { try { wsDb.close(); } catch { /* ignore */ } }
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ describe('local server default workspace wiring', () => {
|
||||
const defaultWorkspaceId = server.workspaceManager.getDefault();
|
||||
expect(defaultWorkspaceId).toBeTruthy();
|
||||
expect(server.agentState.activeWorkspaceId).toBe(defaultWorkspaceId);
|
||||
expect(server.agentState.getWorkspaceMindDb(defaultWorkspaceId!)).toBeTruthy();
|
||||
expect(server.agentState.getWorkspaceMindDb('default')).toBeNull();
|
||||
expect(server.agentState.orchestrator.getWorkspaceId()).toBe(defaultWorkspaceId);
|
||||
expect(server.workspaceManager.get('default')).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,12 +6,11 @@
|
||||
* (B8 / PRD §12.1 "greeting with user name") — unit + via inject.
|
||||
* - applyPriorityRanking(): recency + bounded pending-item boost
|
||||
* (PRD §12.1 "ranked by recency and priority") — unit.
|
||||
* - needsReviewCount (J08/D6): personal-mind frames with metadata status
|
||||
* 'unreviewed' are counted; the count matches what the Memory Center
|
||||
* "Needs review" view lists.
|
||||
* - needsReviewCount (J08/D6): extracted personal facts that are neither
|
||||
* confirmed nor archived are counted.
|
||||
*
|
||||
* Scaffolding follows memory-center.test.ts: Fastify inject + MindDB(':memory:')
|
||||
* + plain-object decorators, plus an in-memory waggle.db for the profile card.
|
||||
* Scaffolding: Fastify inject + an in-memory memory store + plain-object
|
||||
* decorators, plus an in-memory waggle.db for the profile card.
|
||||
* localConfig is intentionally absent → the
|
||||
* buildWorkspaceState call inside the briefing loop throws and is absorbed by
|
||||
* the route's per-workspace try/catch (pendingCount degrades to 0), and
|
||||
@@ -20,14 +19,14 @@
|
||||
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import Fastify from 'fastify';
|
||||
import { MindDB, IdentityLayer, WaggleDB } from '@waggle/core';
|
||||
import { IdentityLayer, WaggleDB } from '@waggle/core';
|
||||
import { createInMemoryMemoryStore, PERSONAL_SCOPE, workspaceScope, type MemoryStore } from '@waggle/memory-contract';
|
||||
import {
|
||||
homeRoutes,
|
||||
personalizeGreeting,
|
||||
applyPriorityRanking,
|
||||
type RecentWorkspaceCard,
|
||||
} from '../../src/local/routes/home.js';
|
||||
import { memoryCenterRoutes } from '../../src/local/routes/memory-center.js';
|
||||
import {
|
||||
buildUpcomingSchedules,
|
||||
type CronScheduleLike,
|
||||
@@ -43,7 +42,7 @@ interface TestWorkspace {
|
||||
}
|
||||
|
||||
function createTestServer(
|
||||
db: MindDB,
|
||||
memory: MemoryStore,
|
||||
workspaces: TestWorkspace[] = [],
|
||||
cronSchedules: CronScheduleLike[] = [],
|
||||
) {
|
||||
@@ -51,18 +50,7 @@ function createTestServer(
|
||||
const waggleDb = new WaggleDB(':memory:');
|
||||
server.decorate('waggleDb', waggleDb);
|
||||
server.addHook('onClose', async () => waggleDb.close());
|
||||
server.decorate('multiMind', {
|
||||
personal: db,
|
||||
getFrameStore: () => undefined,
|
||||
search: () => [],
|
||||
workspace: undefined,
|
||||
setWorkspace: () => {},
|
||||
});
|
||||
server.decorate('agentState', {
|
||||
getWorkspaceMindDb: () => undefined,
|
||||
activateWorkspaceMind: () => undefined,
|
||||
listWorkspaces: () => [],
|
||||
});
|
||||
server.decorate('memory', memory);
|
||||
server.decorate('workspaceManager', {
|
||||
list: () => workspaces,
|
||||
get: (id: string) => workspaces.find((w) => w.id === id),
|
||||
@@ -73,7 +61,6 @@ function createTestServer(
|
||||
});
|
||||
// localConfig intentionally absent — see file header.
|
||||
server.register(homeRoutes);
|
||||
server.register(memoryCenterRoutes);
|
||||
return server;
|
||||
}
|
||||
|
||||
@@ -128,17 +115,16 @@ describe('applyPriorityRanking (P2 — recency + pending boost)', () => {
|
||||
});
|
||||
|
||||
describe('GET /api/home/briefing (P2 — J08 needsReviewCount + greeting)', () => {
|
||||
let db: MindDB;
|
||||
let memory: MemoryStore;
|
||||
let server: ReturnType<typeof Fastify>;
|
||||
|
||||
afterEach(async () => {
|
||||
await server.close();
|
||||
db.close();
|
||||
});
|
||||
|
||||
function boot(workspaces: TestWorkspace[] = []) {
|
||||
db = new MindDB(':memory:');
|
||||
server = createTestServer(db, workspaces);
|
||||
memory = createInMemoryMemoryStore();
|
||||
server = createTestServer(memory, workspaces);
|
||||
}
|
||||
|
||||
it('empty mind → needsReviewCount 0, isFirstRun true', async () => {
|
||||
@@ -150,35 +136,25 @@ describe('GET /api/home/briefing (P2 — J08 needsReviewCount + greeting)', () =
|
||||
expect(briefing.needsReviewCount).toBe(0);
|
||||
});
|
||||
|
||||
it('counts only personal-mind memories with status unreviewed', async () => {
|
||||
it('counts extracted personal facts that are neither confirmed nor archived', async () => {
|
||||
boot();
|
||||
// Seed via the same routes the product uses: create (status active) then
|
||||
// flip one to unreviewed — the C33 harvest lifecycle state.
|
||||
const m1 = await server.inject({
|
||||
method: 'POST', url: '/api/memory',
|
||||
payload: { content: 'Imported fact awaiting review.' },
|
||||
const extracted = (text: string, metadata: Record<string, unknown> = {}) => memory.remember({
|
||||
scope: PERSONAL_SCOPE, text, origin: 'extraction', source: 'agent_inferred', kind: 'profile', metadata,
|
||||
});
|
||||
expect(m1.statusCode).toBe(200);
|
||||
const m2 = await server.inject({
|
||||
method: 'POST', url: '/api/memory',
|
||||
payload: { content: 'Already-reviewed fact.' },
|
||||
await extracted('Prefers short answers.');
|
||||
await extracted('Lives in Belgrade.');
|
||||
await extracted('Works on Waggle.', { confirmedAt: '2026-10-24T10:00:00.000Z' });
|
||||
await extracted('Used to live in Novi Sad.', { archived: true });
|
||||
// Memories the user saved are not up for review.
|
||||
await memory.remember({ scope: PERSONAL_SCOPE, text: 'Saved by the user.', source: 'user_stated' });
|
||||
// Workspace memories are not counted: the "Needs review" view is personal.
|
||||
await memory.remember({
|
||||
scope: workspaceScope('w1'), text: 'Workspace fact.', origin: 'extraction', source: 'agent_inferred',
|
||||
});
|
||||
expect(m2.statusCode).toBe(200);
|
||||
|
||||
const patch = await server.inject({
|
||||
method: 'PATCH', url: `/api/memory/${m1.json().id}`,
|
||||
payload: { status: 'unreviewed' },
|
||||
});
|
||||
expect(patch.statusCode).toBe(200);
|
||||
|
||||
const res = await server.inject({ method: 'GET', url: '/api/home/briefing' });
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.json().needsReviewCount).toBe(1);
|
||||
|
||||
// The count must agree with what the deep-linked "Needs review" view shows
|
||||
// — same query the Memory Center fires (status filter + limit=200).
|
||||
const list = await server.inject({ method: 'GET', url: '/api/memory?status=unreviewed&limit=200' });
|
||||
expect(list.json().count).toBe(1);
|
||||
expect(res.json().needsReviewCount).toBe(2);
|
||||
});
|
||||
|
||||
it('briefing slices recent workspaces to 6, newest first (route-level ranking wiring)', async () => {
|
||||
@@ -220,12 +196,11 @@ describe('GET /api/home/briefing (P2 — J08 needsReviewCount + greeting)', () =
|
||||
});
|
||||
|
||||
describe('upNext schedule aggregation (global jobs dedup + future-only)', () => {
|
||||
let db: MindDB;
|
||||
let memory: MemoryStore;
|
||||
let server: ReturnType<typeof Fastify>;
|
||||
|
||||
afterEach(async () => {
|
||||
await server.close();
|
||||
db.close();
|
||||
});
|
||||
|
||||
const FUTURE = new Date(Date.now() + 60 * 60 * 1000).toISOString();
|
||||
@@ -248,8 +223,7 @@ describe('upNext schedule aggregation (global jobs dedup + future-only)', () =>
|
||||
}));
|
||||
|
||||
it('a global schedule surfaces once across multiple workspaces, not once per workspace', async () => {
|
||||
db = new MindDB(':memory:');
|
||||
server = createTestServer(db, threeWorkspaces, [
|
||||
server = createTestServer(createInMemoryMemoryStore(), threeWorkspaces, [
|
||||
globalSchedule(1, 'Memory compaction', FUTURE),
|
||||
]);
|
||||
const res = await server.inject({ method: 'GET', url: '/api/home/briefing' });
|
||||
@@ -260,8 +234,7 @@ describe('upNext schedule aggregation (global jobs dedup + future-only)', () =>
|
||||
});
|
||||
|
||||
it('past-due schedules never display as upcoming', async () => {
|
||||
db = new MindDB(':memory:');
|
||||
server = createTestServer(db, threeWorkspaces, [
|
||||
server = createTestServer(createInMemoryMemoryStore(), threeWorkspaces, [
|
||||
globalSchedule(1, 'Memory compaction', PAST),
|
||||
globalSchedule(2, 'Harvest sync', FUTURE),
|
||||
]);
|
||||
|
||||
@@ -2,43 +2,47 @@ import { describe, it, expect, vi } from 'vitest';
|
||||
import os from 'node:os';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { WaggleDB } from '@waggle/core';
|
||||
import { createInMemoryMemoryStore } from '@waggle/memory-contract';
|
||||
import { buildWorkspaceNowBlock } from '../../src/local/routes/workspace-context.js';
|
||||
|
||||
/**
|
||||
* R6-006 path-traversal regression.
|
||||
*
|
||||
* `buildWorkspaceNowBlock` builds session-directory paths from an unvalidated
|
||||
* `workspaceId` (getMindPath + dataDir/workspaces/<workspaceId>/sessions),
|
||||
* `workspaceId` (dataDir/workspaces/<workspaceId>/sessions),
|
||||
* enabling an existence/count probe outside the workspaces root. A guard
|
||||
* (assertSafeSegment) now runs at the entry of the helper and THROWS a
|
||||
* { statusCode: 400 } error before the value touches the filesystem; the
|
||||
* calling route handlers propagate that to Fastify's default error handler.
|
||||
*
|
||||
* These are unit tests against the exported helper directly (a full route
|
||||
* inject is impractical — the route needs heavy mind/wsManager decorators).
|
||||
* inject is impractical — the route needs heavy memory/wsManager decorators).
|
||||
*/
|
||||
describe('R6-006 workspace-context path traversal', () => {
|
||||
// A throwaway data dir; the guard fires before this is ever read for the
|
||||
// malicious cases, and the valid case short-circuits on a null wsManager.get.
|
||||
const tmpDataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'wctx-traversal-'));
|
||||
const waggleDb = new WaggleDB(':memory:');
|
||||
|
||||
const maliciousIds = ['../evil', '..%2f..', '../../etc', 'ws/../../secret', '..\\evil'];
|
||||
|
||||
for (const bad of maliciousIds) {
|
||||
it(`rejects traversal workspaceId ${JSON.stringify(bad)} with a 400 error before any fs access`, () => {
|
||||
it(`rejects traversal workspaceId ${JSON.stringify(bad)} with a 400 error before any fs access`, async () => {
|
||||
const wsManager = {
|
||||
get: vi.fn(() => ({ id: bad, name: 'evil' })),
|
||||
getMindPath: vi.fn(() => path.join(tmpDataDir, 'mind.sqlite')),
|
||||
};
|
||||
const activateWorkspaceMind = vi.fn(() => true);
|
||||
const memory = createInMemoryMemoryStore();
|
||||
const count = vi.spyOn(memory, 'count');
|
||||
|
||||
let thrown: unknown;
|
||||
try {
|
||||
buildWorkspaceNowBlock({
|
||||
await buildWorkspaceNowBlock({
|
||||
dataDir: tmpDataDir,
|
||||
workspaceId: bad,
|
||||
wsManager,
|
||||
activateWorkspaceMind,
|
||||
waggleDb,
|
||||
memory,
|
||||
});
|
||||
} catch (e) {
|
||||
thrown = e;
|
||||
@@ -46,32 +50,27 @@ describe('R6-006 workspace-context path traversal', () => {
|
||||
|
||||
expect(thrown).toBeInstanceOf(Error);
|
||||
expect((thrown as { statusCode?: number }).statusCode).toBe(400);
|
||||
// Guard short-circuits BEFORE the path-building dependencies are touched.
|
||||
// Guard short-circuits BEFORE the workspace and its memory are touched.
|
||||
expect(wsManager.get).not.toHaveBeenCalled();
|
||||
expect(wsManager.getMindPath).not.toHaveBeenCalled();
|
||||
expect(activateWorkspaceMind).not.toHaveBeenCalled();
|
||||
expect(count).not.toHaveBeenCalled();
|
||||
});
|
||||
}
|
||||
|
||||
it('does NOT reject a normal valid workspaceId', () => {
|
||||
it('does NOT reject a normal valid workspaceId', async () => {
|
||||
const validId = 'workspace-123_AB';
|
||||
const wsManager = {
|
||||
// Return null so the helper cleanly returns null after the guard passes —
|
||||
// proving the guard did not reject a valid segment.
|
||||
get: vi.fn(() => null),
|
||||
getMindPath: vi.fn(() => path.join(tmpDataDir, 'mind.sqlite')),
|
||||
};
|
||||
const activateWorkspaceMind = vi.fn(() => true);
|
||||
|
||||
let result: unknown;
|
||||
expect(() => {
|
||||
result = buildWorkspaceNowBlock({
|
||||
dataDir: tmpDataDir,
|
||||
workspaceId: validId,
|
||||
wsManager,
|
||||
activateWorkspaceMind,
|
||||
});
|
||||
}).not.toThrow();
|
||||
const result = await buildWorkspaceNowBlock({
|
||||
dataDir: tmpDataDir,
|
||||
workspaceId: validId,
|
||||
wsManager,
|
||||
waggleDb,
|
||||
memory: createInMemoryMemoryStore(),
|
||||
});
|
||||
|
||||
// Guard passed, so execution proceeded to the (mocked) wsManager.get.
|
||||
expect(wsManager.get).toHaveBeenCalledWith(validId);
|
||||
|
||||
@@ -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.');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,7 +6,8 @@ import { EventEmitter } from 'node:events';
|
||||
import { NotificationGate, materialFingerprint, getNotificationGate } from '../src/local/notification-gate.js';
|
||||
import { emitNotification } from '../src/local/routes/notifications.js';
|
||||
import { suggestCapabilities, type ProactiveContext } from '../src/local/proactive-handlers.js';
|
||||
import { WorkspaceManager } from '@waggle/core';
|
||||
import { WaggleDB, WorkspaceManager } from '@waggle/core';
|
||||
import { createInMemoryMemoryStore } from '@waggle/memory-contract';
|
||||
|
||||
const STORE_FILE = 'notification-fingerprints.json';
|
||||
|
||||
@@ -156,15 +157,18 @@ describe('emitNotification anti-nag gate', () => {
|
||||
expect(emitted).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('suppresses a repeated identical proactive payload on the second emit', () => {
|
||||
it('suppresses a repeated identical proactive payload on the second emit', async () => {
|
||||
const wsManager = new WorkspaceManager(tmpDir);
|
||||
const waggleDb = new WaggleDB(':memory:');
|
||||
const ctx: ProactiveContext = {
|
||||
dataDir: tmpDir,
|
||||
workspaceManager: wsManager,
|
||||
getWorkspaceMindDb: () => null,
|
||||
memory: createInMemoryMemoryStore(),
|
||||
waggleDb,
|
||||
};
|
||||
// No skills installed → the "capability packs" suggestion, carrying a dedupeKey + hash.
|
||||
const msg = suggestCapabilities(ctx);
|
||||
const msg = await suggestCapabilities(ctx);
|
||||
waggleDb.close();
|
||||
expect(msg).not.toBeNull();
|
||||
expect(msg!.dedupeKey).toBeDefined();
|
||||
expect(msg!.materialHash).toBeDefined();
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
77
packages/server/tests/routes/command-search.test.ts
Normal file
77
packages/server/tests/routes/command-search.test.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* Command palette search (GET /api/command/search): the memory facet searches
|
||||
* personal memory and the active workspace's memory through the memory contract.
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import {
|
||||
createInMemoryMemoryStore,
|
||||
PERSONAL_SCOPE,
|
||||
workspaceScope,
|
||||
type MemoryStore,
|
||||
} from '@waggle/memory-contract';
|
||||
import { commandRoutes } from '../../src/local/routes/command.js';
|
||||
|
||||
describe('GET /api/command/search — memory facet', () => {
|
||||
let dataDir: string;
|
||||
let memory: MemoryStore;
|
||||
let server: FastifyInstance;
|
||||
|
||||
async function boot(activeWorkspaceId: string | null): Promise<void> {
|
||||
server = Fastify({ logger: false });
|
||||
server.decorate('localConfig', { dataDir } as never);
|
||||
server.decorate('memory', memory);
|
||||
server.decorate('workspaceManager', { list: () => [] } as never);
|
||||
server.decorate('agentState', {
|
||||
activeWorkspaceId,
|
||||
commandRegistry: { search: () => [], list: () => [] },
|
||||
} as never);
|
||||
await server.register(commandRoutes);
|
||||
}
|
||||
|
||||
async function memoryTitles(url: string): Promise<string[]> {
|
||||
const res = await server.inject({ method: 'GET', url });
|
||||
expect(res.statusCode).toBe(200);
|
||||
return (res.json().results as Array<{ type: string; title: string }>)
|
||||
.filter((r) => r.type === 'memory')
|
||||
.map((r) => r.title)
|
||||
.sort();
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-command-search-'));
|
||||
memory = createInMemoryMemoryStore();
|
||||
await memory.remember({ scope: PERSONAL_SCOPE, text: 'Personal budget review notes' });
|
||||
await memory.remember({ scope: workspaceScope('alpha'), text: 'Alpha budget decision' });
|
||||
await memory.remember({ scope: workspaceScope('beta'), text: 'Beta budget draft' });
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await server.close();
|
||||
fs.rmSync(dataDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it('searches personal memory and the active workspace, not other workspaces', async () => {
|
||||
await boot('alpha');
|
||||
expect(await memoryTitles('/api/command/search?q=budget'))
|
||||
.toEqual(['Alpha budget decision', 'Personal budget review notes']);
|
||||
});
|
||||
|
||||
it('narrows to one scope when asked', async () => {
|
||||
await boot('alpha');
|
||||
expect(await memoryTitles('/api/command/search?q=budget&scope=personal'))
|
||||
.toEqual(['Personal budget review notes']);
|
||||
expect(await memoryTitles('/api/command/search?q=budget&scope=workspace'))
|
||||
.toEqual(['Alpha budget decision']);
|
||||
});
|
||||
|
||||
it('searches only personal memory when no workspace is active', async () => {
|
||||
await boot(null);
|
||||
expect(await memoryTitles('/api/command/search?q=budget'))
|
||||
.toEqual(['Personal budget review notes']);
|
||||
expect(await memoryTitles('/api/command/search?q=budget&scope=workspace')).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -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);
|
||||
|
||||
@@ -1,17 +1,25 @@
|
||||
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 { MindDB, WaggleDB } from '@waggle/core';
|
||||
import { WaggleDB } from '@waggle/core';
|
||||
import {
|
||||
createInMemoryMemoryStore,
|
||||
workspaceScope,
|
||||
type MemoryImportance,
|
||||
type MemoryStore,
|
||||
} from '@waggle/memory-contract';
|
||||
import { buildWorkspaceNowBlock, formatWorkspaceNowPrompt, type WorkspaceNowBlock } from '../../src/local/routes/workspace-context.js';
|
||||
|
||||
describe('workspace-context', () => {
|
||||
let tmpDir: string;
|
||||
let waggleDb: WaggleDB;
|
||||
let memory: MemoryStore;
|
||||
|
||||
beforeEach(() => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ctx-test-'));
|
||||
waggleDb = WaggleDB.openInDataDir(tmpDir);
|
||||
memory = createInMemoryMemoryStore();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -24,9 +32,9 @@ describe('workspace-context', () => {
|
||||
}
|
||||
});
|
||||
|
||||
// Helper: create a workspace directory structure with a mind DB
|
||||
function setupWorkspace(id: string, opts?: {
|
||||
frames?: Array<{ content: string; importance: string }>;
|
||||
// Helper: create a workspace directory structure with memories
|
||||
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);
|
||||
@@ -38,24 +46,8 @@ describe('workspace-context', () => {
|
||||
JSON.stringify({ id, name: `Test Workspace ${id}`, group: 'test', created: new Date().toISOString() }),
|
||||
);
|
||||
|
||||
// Create mind DB with frames
|
||||
const mindPath = path.join(wsDir, 'workspace.mind');
|
||||
if (opts?.frames && opts.frames.length > 0) {
|
||||
const db = new MindDB(mindPath);
|
||||
const raw = db.getDatabase();
|
||||
// Insert a session first (frames require a session via gop_id FK)
|
||||
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 });
|
||||
}
|
||||
|
||||
// Create session files
|
||||
@@ -76,29 +68,26 @@ describe('workspace-context', () => {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// ── Test 1: Returns null for non-existent workspace ──────────────
|
||||
|
||||
it('returns null for non-existent workspace', () => {
|
||||
it('returns null for non-existent workspace', async () => {
|
||||
const wsManager = makeManager(new Map());
|
||||
|
||||
const result = buildWorkspaceNowBlock({
|
||||
const result = await buildWorkspaceNowBlock({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'does-not-exist',
|
||||
wsManager,
|
||||
activateWorkspaceMind: noopActivate,
|
||||
memory,
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
@@ -106,43 +95,20 @@ describe('workspace-context', () => {
|
||||
|
||||
// ── Test 2: Returns null for workspace with no data ──────────────
|
||||
|
||||
it('returns null for workspace with no mind file', () => {
|
||||
const wsDir = path.join(tmpDir, 'workspaces', 'empty-ws');
|
||||
fs.mkdirSync(wsDir, { recursive: true });
|
||||
|
||||
it('returns null for workspace without memories', async () => {
|
||||
const wsManager = makeManager(new Map([
|
||||
['empty-ws', { id: 'empty-ws', name: 'Empty Workspace' }],
|
||||
]));
|
||||
await setupWorkspace('empty-ws', {
|
||||
sessions: [{ title: 'Chat only', messages: [{ role: 'user', content: 'Hello there' }] }],
|
||||
});
|
||||
|
||||
const result = buildWorkspaceNowBlock({
|
||||
const result = await buildWorkspaceNowBlock({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'empty-ws',
|
||||
wsManager,
|
||||
activateWorkspaceMind: noopActivate,
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null for workspace with mind file but zero frames', () => {
|
||||
const wsManager = makeManager(new Map([
|
||||
['zero-ws', { id: 'zero-ws', name: 'Zero Frames' }],
|
||||
]));
|
||||
|
||||
// Create a mind DB with no frames
|
||||
setupWorkspace('zero-ws', { frames: [] });
|
||||
// MindDB file exists but has no frames — we need the file to exist
|
||||
const mindPath = path.join(tmpDir, 'workspaces', 'zero-ws', 'workspace.mind');
|
||||
const db = new MindDB(mindPath);
|
||||
db.close();
|
||||
|
||||
const result = buildWorkspaceNowBlock({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'zero-ws',
|
||||
wsManager,
|
||||
activateWorkspaceMind: noopActivate,
|
||||
memory,
|
||||
});
|
||||
|
||||
expect(result).toBeNull();
|
||||
@@ -150,17 +116,16 @@ describe('workspace-context', () => {
|
||||
|
||||
// ── Test 3: Returns correctly shaped block ───────────────────────
|
||||
|
||||
it('returns correctly shaped block for workspace with data', () => {
|
||||
it('returns correctly shaped block for workspace with data', async () => {
|
||||
const wsManager = makeManager(new Map([
|
||||
['rich-ws', { id: 'rich-ws', name: 'Rich Workspace' }],
|
||||
]));
|
||||
const activateWorkspaceMind = vi.fn(() => true);
|
||||
|
||||
setupWorkspace('rich-ws', {
|
||||
frames: [
|
||||
{ content: 'Working on the Waggle project, a workspace-native AI agent platform.', importance: 'important' },
|
||||
{ content: 'Decision: Use SQLite for local storage instead of PostgreSQL.', importance: 'critical' },
|
||||
{ content: 'Implemented the memory frame system with FTS5 search.', importance: 'normal' },
|
||||
await setupWorkspace('rich-ws', {
|
||||
memories: [
|
||||
{ text: 'Working on the Waggle project, a workspace-native AI agent platform.', importance: 'important' },
|
||||
{ text: 'Decision: Use SQLite for local storage instead of PostgreSQL.', importance: 'critical' },
|
||||
{ text: 'Implemented the memory frame system with FTS5 search.', importance: 'normal' },
|
||||
],
|
||||
sessions: [
|
||||
{
|
||||
@@ -173,16 +138,15 @@ describe('workspace-context', () => {
|
||||
],
|
||||
});
|
||||
|
||||
const result = buildWorkspaceNowBlock({
|
||||
const result = await buildWorkspaceNowBlock({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'rich-ws',
|
||||
wsManager,
|
||||
activateWorkspaceMind,
|
||||
memory,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(activateWorkspaceMind).not.toHaveBeenCalled();
|
||||
expect(result!.workspaceName).toBe('Rich Workspace');
|
||||
expect(result!.summary).toBeTruthy();
|
||||
expect(result!.summary.length).toBeGreaterThan(10);
|
||||
@@ -198,15 +162,15 @@ describe('workspace-context', () => {
|
||||
|
||||
// ── Test 4: Caps at correct limits ───────────────────────────────
|
||||
|
||||
it('caps decisions at 3, threads at 3, progress at 5', () => {
|
||||
it('caps decisions at 3, threads at 3, progress at 5', async () => {
|
||||
const wsManager = makeManager(new Map([
|
||||
['capped-ws', { id: 'capped-ws', name: 'Capped Workspace' }],
|
||||
]));
|
||||
|
||||
// Create many decisions and sessions
|
||||
const frames = [];
|
||||
const memories: Array<{ text: string; importance: MemoryImportance }> = [];
|
||||
for (let i = 0; i < 10; i++) {
|
||||
frames.push({ content: `Decision: chose option ${i} for the architecture component ${i}`, importance: 'critical' as const });
|
||||
memories.push({ text: `Decision: chose option ${i} for the architecture component ${i}`, importance: 'critical' as const });
|
||||
}
|
||||
|
||||
const sessions = [];
|
||||
@@ -220,14 +184,14 @@ describe('workspace-context', () => {
|
||||
});
|
||||
}
|
||||
|
||||
setupWorkspace('capped-ws', { frames, sessions });
|
||||
await setupWorkspace('capped-ws', { memories, sessions });
|
||||
|
||||
const result = buildWorkspaceNowBlock({
|
||||
const result = await buildWorkspaceNowBlock({
|
||||
dataDir: tmpDir,
|
||||
waggleDb,
|
||||
workspaceId: 'capped-ws',
|
||||
wsManager,
|
||||
activateWorkspaceMind: noopActivate,
|
||||
memory,
|
||||
});
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -25,8 +25,6 @@ describe('Workspace & Session API', () => {
|
||||
'utf-8'
|
||||
);
|
||||
|
||||
// Create personal.mind as empty file (MindDB will init schema)
|
||||
// Don't create it — let MultiMind handle it
|
||||
server = await buildLocalServer({ dataDir, port: 0 });
|
||||
});
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import os from 'node:os';
|
||||
import { MindDB } from '@waggle/core';
|
||||
import { buildLocalServer } from '../src/local/index.js';
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import { injectWithAuth } from './test-utils.js';
|
||||
@@ -32,11 +31,6 @@ describe('Workspace Templates API', () => {
|
||||
beforeAll(async () => {
|
||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ws-templates-'));
|
||||
|
||||
// Create personal.mind (required by buildLocalServer)
|
||||
const personalPath = path.join(tmpDir, 'personal.mind');
|
||||
const mind = new MindDB(personalPath);
|
||||
mind.close();
|
||||
|
||||
server = await buildLocalServer({ dataDir: tmpDir });
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user