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:
@@ -61,6 +61,7 @@ Baseline: commit `b20b138` of `git.os-dev.tech/oleg.m/waggle-os`.
|
|||||||
| UF-030 | WhatsApp setup hint suggests pairing from the "Message yourself" chat, which is never read | Fixed | Web UI, channels |
|
| UF-030 | WhatsApp setup hint suggests pairing from the "Message yourself" chat, which is never read | Fixed | Web UI, channels |
|
||||||
| UF-031 | Team sync shares the save_memory confirmation instead of the memory | Open | Team mode, memory |
|
| UF-031 | Team sync shares the save_memory confirmation instead of the memory | Open | Team mode, memory |
|
||||||
| UF-032 | Team capability policies are always read from the team "default" | Open | Team mode, governance |
|
| UF-032 | Team capability policies are always read from the team "default" | Open | Team mode, governance |
|
||||||
|
| UF-033 | Command palette memory search uses whichever workspace the Memory app opened last | Fixed | Sidecar, command palette |
|
||||||
|
|
||||||
## Entries
|
## Entries
|
||||||
|
|
||||||
@@ -324,3 +325,12 @@ Entries UF-010 to UF-019 come from the UI audit `anti-slop/audit-001-2026-10-14.
|
|||||||
- Cause: the slug is read from `teamServer.teamSlug` in `config.json`, a field `TeamServerConfig` does not have and `POST /api/team/connect` never writes, so it always falls back to `'default'`. The workspace's `teamId` is not used.
|
- Cause: the slug is read from `teamServer.teamSlug` in `config.json`, a field `TeamServerConfig` does not have and `POST /api/team/connect` never writes, so it always falls back to `'default'`. The workspace's `teamId` is not used.
|
||||||
- What we did: nothing in the old code. Team mode is postponed in the community edition (ADR-0003) and the lookup was removed with it in P1.1.
|
- What we did: nothing in the old code. Team mode is postponed in the community edition (ADR-0003) and the lookup was removed with it in P1.1.
|
||||||
- Recommendation: fetch `/api/teams/<workspace teamId>/capability-policies` for the workspace the turn runs in, and cache per team rather than per workspace.
|
- Recommendation: fetch `/api/teams/<workspace teamId>/capability-policies` for the workspace the turn runs in, and cache per team rather than per workspace.
|
||||||
|
|
||||||
|
### UF-033. Command palette memory search uses whichever workspace the Memory app opened last
|
||||||
|
- Status: Fixed
|
||||||
|
- Found: 2026-10-24
|
||||||
|
- Where: at commit `b20b138`, `packages/server/src/local/routes/command.ts:111` (`server.multiMind.search(q, searchScope, perFacet)`); the workspace is set only by `ensureWorkspaceMind()` in `packages/server/src/local/routes/memory.ts:184-192`
|
||||||
|
- Symptom: memory results in the Ctrl+K palette (scope 'all' or 'workspace') come from the workspace last viewed in the Memory app, not from the workspace the user is in; before the Memory app is opened, workspace memory is not searched at all. If the mind cache has evicted that workspace, the memory group is silently empty.
|
||||||
|
- Cause: `MultiMind` holds one borrowed workspace handle that only the memory routes set; the palette reuses it without choosing a workspace.
|
||||||
|
- What we did: the palette searches personal memory and the memory of the active workspace (`agentState.activeWorkspaceId`) through the memory contract.
|
||||||
|
- Recommendation: pass the workspace the palette was opened in (or the active workspace) to the memory search instead of relying on shared `MultiMind` state.
|
||||||
|
|||||||
@@ -335,6 +335,8 @@ export interface AgentState {
|
|||||||
getWorkspaceMindDb: (workspaceId: string) => import('@waggle/core').MindDB | null;
|
getWorkspaceMindDb: (workspaceId: string) => import('@waggle/core').MindDB | null;
|
||||||
/** Close and remove a workspace MindDB from cache. Used before workspace deletion to prevent EBUSY. */
|
/** Close and remove a workspace MindDB from cache. Used before workspace deletion to prevent EBUSY. */
|
||||||
closeWorkspaceMind: (workspaceId: string) => void;
|
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. */
|
/** List all workspaces (id + name). Used by global memory search. */
|
||||||
listWorkspaces: () => Array<{ id: string; name: string }>;
|
listWorkspaces: () => Array<{ id: string; name: string }>;
|
||||||
/** Currently active workspace ID (null = personal only) */
|
/** Currently active workspace ID (null = personal only) */
|
||||||
@@ -1301,6 +1303,9 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
|||||||
setActiveWorkspaceId(null);
|
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 => {
|
const getWorkspaceMindDb = (workspaceId: string): MindDB | null => {
|
||||||
if (!wsManager.get(workspaceId)) return null;
|
if (!wsManager.get(workspaceId)) return null;
|
||||||
@@ -1328,6 +1333,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
|||||||
activateWorkspaceMind: activateWorkspaceMind,
|
activateWorkspaceMind: activateWorkspaceMind,
|
||||||
getWorkspaceMindDb,
|
getWorkspaceMindDb,
|
||||||
closeWorkspaceMind,
|
closeWorkspaceMind,
|
||||||
|
releaseWorkspace,
|
||||||
listWorkspaces: () => wsManager.list().map(w => ({ id: w.id, name: w.name })),
|
listWorkspaces: () => wsManager.list().map(w => ({ id: w.id, name: w.name })),
|
||||||
activeWorkspaceId,
|
activeWorkspaceId,
|
||||||
subagentOrchestrator: null,
|
subagentOrchestrator: null,
|
||||||
@@ -1418,7 +1424,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
|||||||
const proactiveCtx: ProactiveContext = {
|
const proactiveCtx: ProactiveContext = {
|
||||||
dataDir: fullConfig.dataDir,
|
dataDir: fullConfig.dataDir,
|
||||||
workspaceManager: wsManager,
|
workspaceManager: wsManager,
|
||||||
getWorkspaceMindDb,
|
memory,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1456,12 +1462,12 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
|||||||
try {
|
try {
|
||||||
switch (proactiveConfig.action) {
|
switch (proactiveConfig.action) {
|
||||||
case 'morning_briefing': {
|
case 'morning_briefing': {
|
||||||
const briefing = generateMorningBriefing(proactiveCtx);
|
const briefing = await generateMorningBriefing(proactiveCtx);
|
||||||
if (briefing) emitProactive(briefing);
|
if (briefing) emitProactive(briefing);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'stale_workspace_check': {
|
case 'stale_workspace_check': {
|
||||||
const staleAlerts = checkStaleWorkspaces(proactiveCtx);
|
const staleAlerts = await checkStaleWorkspaces(proactiveCtx);
|
||||||
for (const alert of staleAlerts) emitProactive(alert);
|
for (const alert of staleAlerts) emitProactive(alert);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1471,7 +1477,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'capability_suggestion': {
|
case 'capability_suggestion': {
|
||||||
const suggestion = suggestCapabilities(proactiveCtx);
|
const suggestion = await suggestCapabilities(proactiveCtx);
|
||||||
if (suggestion) emitProactive(suggestion);
|
if (suggestion) emitProactive(suggestion);
|
||||||
break;
|
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.
|
* toast system picks them up.
|
||||||
*
|
*
|
||||||
* Separate from the Team proactive service (ProactiveService) which uses
|
* 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 fs from 'node:fs';
|
||||||
import path from 'node:path';
|
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';
|
import { materialFingerprint } from './notification-gate.js';
|
||||||
|
|
||||||
// ── Types ──────────────────────────────────────────────────────────────
|
// ── Types ──────────────────────────────────────────────────────────────
|
||||||
@@ -32,8 +33,7 @@ export interface ProactiveMessage {
|
|||||||
export interface ProactiveContext {
|
export interface ProactiveContext {
|
||||||
dataDir: string;
|
dataDir: string;
|
||||||
workspaceManager: WorkspaceManager;
|
workspaceManager: WorkspaceManager;
|
||||||
/** Get a cached workspace MindDB (opens on demand). Returns null if workspace not found. */
|
memory: MemoryStore;
|
||||||
getWorkspaceMindDb: (workspaceId: string) => import('@waggle/core').MindDB | null;
|
|
||||||
/** waggle.db, which holds awareness items. */
|
/** waggle.db, which holds awareness items. */
|
||||||
waggleDb: import('@waggle/core').WaggleDB;
|
waggleDb: import('@waggle/core').WaggleDB;
|
||||||
}
|
}
|
||||||
@@ -68,14 +68,21 @@ function countPendingAwareness(ctx: ProactiveContext, workspaceId: string): numb
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Count memory frames in a workspace mind. */
|
/** Recent memories scanned for the morning briefing's highlights. */
|
||||||
function countMemoryFrames(ctx: ProactiveContext, workspaceId: string): number {
|
const HIGHLIGHT_SCAN = 200;
|
||||||
const db = ctx.getWorkspaceMindDb(workspaceId);
|
|
||||||
if (!db) return 0;
|
/** 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 {
|
try {
|
||||||
const raw = db.getDatabase();
|
return await ctx.memory.count({ scopes: [workspaceScope(workspaceId)] });
|
||||||
const row = raw.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number } | undefined;
|
|
||||||
return row?.cnt ?? 0;
|
|
||||||
} catch {
|
} catch {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -96,7 +103,7 @@ function hasInstalledCapabilities(dataDir: string): boolean {
|
|||||||
// ── Handlers ───────────────────────────────────────────────────────────
|
// ── Handlers ───────────────────────────────────────────────────────────
|
||||||
|
|
||||||
/** Generate morning briefing across all workspaces. */
|
/** 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();
|
const workspaces = ctx.workspaceManager.list();
|
||||||
if (workspaces.length === 0) return null;
|
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 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
|
for (const ws of workspaces.slice(0, 5)) { // cap at 5 workspaces for brevity
|
||||||
const db = ctx.getWorkspaceMindDb(ws.id);
|
|
||||||
if (!db) continue;
|
|
||||||
try {
|
try {
|
||||||
const raw = db.getDatabase();
|
const recent = await ctx.memory.list({
|
||||||
const decisions = raw.prepare(
|
scopes: [workspaceScope(ws.id)],
|
||||||
`SELECT content FROM memory_frames
|
since: weekAgo,
|
||||||
WHERE (importance IN ('critical', 'important') OR content LIKE 'Decision%' OR content LIKE '%decided%')
|
limit: HIGHLIGHT_SCAN,
|
||||||
AND created_at > datetime('now', '-7 day')
|
});
|
||||||
ORDER BY id DESC LIMIT 2`
|
const decisions = recent.filter(isHighlight).slice(0, 2);
|
||||||
).all() as Array<{ content: string }>;
|
|
||||||
if (decisions.length > 0) {
|
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 */ }
|
} catch { /* non-blocking */ }
|
||||||
}
|
}
|
||||||
@@ -177,7 +182,7 @@ export function generateMorningBriefing(ctx: ProactiveContext): ProactiveMessage
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Check for stale workspaces (not visited in 14+ days). */
|
/** 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();
|
const workspaces = ctx.workspaceManager.list();
|
||||||
if (workspaces.length === 0) return [];
|
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));
|
const idleDays = Math.floor((now - referenceDate.getTime()) / (24 * 60 * 60 * 1000));
|
||||||
|
|
||||||
if (idleDays >= 14) {
|
if (idleDays >= 14) {
|
||||||
const frameCount = countMemoryFrames(ctx, ws.id);
|
const frameCount = await countMemories(ctx, ws.id);
|
||||||
|
|
||||||
messages.push({
|
messages.push({
|
||||||
type: 'stale_workspace',
|
type: 'stale_workspace',
|
||||||
title: `"${ws.name}" hasn't been visited in ${idleDays} days`,
|
title: `"${ws.name}" hasn't been visited in ${idleDays} days`,
|
||||||
body: frameCount > 0
|
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.',
|
: 'Consider archiving or revisiting this workspace.',
|
||||||
workspaceId: ws.id,
|
workspaceId: ws.id,
|
||||||
actionUrl: `/workspaces/${ws.id}`,
|
actionUrl: `/workspaces/${ws.id}`,
|
||||||
@@ -207,7 +212,7 @@ export function checkStaleWorkspaces(ctx: ProactiveContext): ProactiveMessage[]
|
|||||||
dedupeKey: `proactive:stale_workspace:${ws.id}`,
|
dedupeKey: `proactive:stale_workspace:${ws.id}`,
|
||||||
// Fingerprint on identity, not idleDays — so a still-stale workspace
|
// Fingerprint on identity, not idleDays — so a still-stale workspace
|
||||||
// doesn't re-nag every day. Re-fires only if it's touched (new
|
// 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() }),
|
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. */
|
/** 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 the user has no installed capabilities, suggest exploring the catalog
|
||||||
if (!hasInstalledCapabilities(ctx.dataDir)) {
|
if (!hasInstalledCapabilities(ctx.dataDir)) {
|
||||||
return {
|
return {
|
||||||
@@ -264,7 +269,7 @@ export function suggestCapabilities(ctx: ProactiveContext): ProactiveMessage | n
|
|||||||
// Count total memories across all workspaces
|
// Count total memories across all workspaces
|
||||||
let totalFrames = 0;
|
let totalFrames = 0;
|
||||||
for (const ws of workspaces) {
|
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
|
// 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>;
|
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 {
|
function isIncompleteCompletionError(error: unknown): boolean {
|
||||||
return typeof error === 'object'
|
return typeof error === 'object'
|
||||||
&& error !== null
|
&& 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
|
// Workspace Now — inject structured context so the agent is grounded on first turn
|
||||||
if (includePersistedMemory && includeConversationDerivedWorkspaceState && workspaceId) {
|
if (includePersistedMemory && includeConversationDerivedWorkspaceState && workspaceId) {
|
||||||
try {
|
try {
|
||||||
const nowBlock = buildWorkspaceNowBlock({
|
const nowBlock = await buildWorkspaceNowBlock({
|
||||||
dataDir: server.localConfig.dataDir,
|
dataDir: server.localConfig.dataDir,
|
||||||
waggleDb: server.waggleDb,
|
waggleDb: server.waggleDb,
|
||||||
|
memory: server.memory,
|
||||||
workspaceId,
|
workspaceId,
|
||||||
wsManager: server.workspaceManager,
|
wsManager: server.workspaceManager,
|
||||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
|
||||||
cronSchedules: server.cronStore.list(),
|
cronSchedules: server.cronStore.list(),
|
||||||
});
|
});
|
||||||
if (nowBlock) {
|
if (nowBlock) {
|
||||||
@@ -1725,12 +1707,12 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
|
|||||||
return 'Conversation-derived workspace state is disabled for this turn.';
|
return 'Conversation-derived workspace state is disabled for this turn.';
|
||||||
}
|
}
|
||||||
if (!effectiveWorkspace) return 'No workspace state available.';
|
if (!effectiveWorkspace) return 'No workspace state available.';
|
||||||
const block = buildWorkspaceNowBlock({
|
const block = await buildWorkspaceNowBlock({
|
||||||
dataDir: server.localConfig.dataDir,
|
dataDir: server.localConfig.dataDir,
|
||||||
waggleDb: server.waggleDb,
|
waggleDb: server.waggleDb,
|
||||||
|
memory: server.memory,
|
||||||
workspaceId: effectiveWorkspace,
|
workspaceId: effectiveWorkspace,
|
||||||
wsManager: server.workspaceManager,
|
wsManager: server.workspaceManager,
|
||||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
|
||||||
cronSchedules: server.cronStore.list(),
|
cronSchedules: server.cronStore.list(),
|
||||||
});
|
});
|
||||||
if (!block) return 'No workspace state available.';
|
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
|
// turns per #13, and for injected-runner turns like every other
|
||||||
// write-back seam in this route). The summary aggregates
|
// write-back seam in this route). The summary aggregates
|
||||||
// tool/connector output, so scan it before it can enter durable
|
// tool/connector output, so scan it before it can enter durable
|
||||||
// memory; fail-soft with the W4A closed-DB guard so persistence
|
// memory; fail soft so persistence never fails the turn.
|
||||||
// never fails the turn.
|
|
||||||
if (!hasCustomRunner && allowMemoryPersistence) {
|
if (!hasCustomRunner && allowMemoryPersistence) {
|
||||||
const summaryScan = scanForInjection(compressionResult.summary, 'tool_output');
|
const summaryScan = scanForInjection(compressionResult.summary, 'tool_output');
|
||||||
if (summaryScan.score >= 0.7) {
|
if (summaryScan.score >= 0.7) {
|
||||||
@@ -2469,11 +2450,7 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
|
|||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throwIfTurnAborted();
|
throwIfTurnAborted();
|
||||||
if (isClosedDbError(e)) {
|
log.warn(`[context-compression] summary persist failed: ${e instanceof Error ? e.message : 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}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3215,19 +3192,9 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
|
|||||||
if (saved.length > 0) {
|
if (saved.length > 0) {
|
||||||
sendEvent('step', { content: `Auto-saved ${saved.length} memor${saved.length === 1 ? 'y' : 'ies'} from this exchange.` });
|
sendEvent('step', { content: `Auto-saved ${saved.length} memor${saved.length === 1 ? 'y' : 'ies'} from this exchange.` });
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch {
|
||||||
throwIfTurnAborted();
|
throwIfTurnAborted();
|
||||||
// Non-blocking. W4A: a closed-handle failure here is the signature
|
// Non-blocking — the turn succeeds without the auto-save.
|
||||||
// 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),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
* POST /api/command/execute — alias → commandRegistry.execute
|
* POST /api/command/execute — alias → commandRegistry.execute
|
||||||
*
|
*
|
||||||
* Surfaces federated by /search (CommandResultType in parens):
|
* Surfaces federated by /search (CommandResultType in parens):
|
||||||
* - memory → server.multiMind.search() (`memory`)
|
* - memory → server.memory.search() (`memory`)
|
||||||
* - workspaces → server.workspaceManager.list() (`workspace`)
|
* - workspaces → server.workspaceManager.list() (`workspace`)
|
||||||
* - skills → loadSkills(dataDir) (`skill`)
|
* - skills → loadSkills(dataDir) (`skill`)
|
||||||
* - sessions → searchSessions() over each workspace's sessions (`session`)
|
* - 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 { CommandResult, Command } from '@waggle/shared';
|
||||||
import type { Orchestrator, LoadedSkill } from '@waggle/agent';
|
import type { Orchestrator, LoadedSkill } from '@waggle/agent';
|
||||||
import { loadSkills } 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 { buildWorkspaceNowBlock, formatWorkspaceNowPrompt } from './workspace-context.js';
|
||||||
import { searchSessions } from './session-utils.js';
|
import { searchSessions } from './session-utils.js';
|
||||||
import { interpretCommand } from '../command-interpret.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. */
|
/** Truncate a content blob into a single-line subtitle. */
|
||||||
function toSubtitle(content: string, max = 120): string {
|
function toSubtitle(content: string, max = 120): string {
|
||||||
const oneLine = content.replace(/\s+/g, ' ').trim();
|
const oneLine = content.replace(/\s+/g, ' ').trim();
|
||||||
@@ -104,17 +115,21 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
|
|||||||
}));
|
}));
|
||||||
}, logWarn));
|
}, logWarn));
|
||||||
|
|
||||||
// Memory — FTS over personal + active workspace minds.
|
// Memory — ranked search over personal memory and the active workspace's.
|
||||||
results.push(...safeFederate('memory', () => {
|
results.push(...await safeFederateAsync('memory', async () => {
|
||||||
const searchScope = scope === 'personal' || scope === 'workspace' ? scope : 'all';
|
const activeWorkspaceId = server.agentState.activeWorkspaceId;
|
||||||
const frames = server.multiMind.search(q, searchScope, perFacet);
|
const scopes: MemoryScope[] = [];
|
||||||
return frames.map((frame): CommandResult => ({
|
if (scope !== 'workspace') scopes.push(PERSONAL_SCOPE);
|
||||||
id: `memory:${frame.id}`,
|
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',
|
type: 'memory',
|
||||||
title: toSubtitle(frame.content, 80),
|
title: toSubtitle(hit.text, 80),
|
||||||
subtitle: frame.created_at ? new Date(frame.created_at).toLocaleString('en-US') : undefined,
|
subtitle: new Date(hit.createdAt).toLocaleString('en-US'),
|
||||||
category: 'search',
|
category: 'search',
|
||||||
action: { route: `/memory?frame=${encodeURIComponent(frame.id)}` },
|
action: { route: `/memory?frame=${encodeURIComponent(hit.id)}` },
|
||||||
}));
|
}));
|
||||||
}, logWarn));
|
}, logWarn));
|
||||||
|
|
||||||
@@ -249,12 +264,12 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
getWorkspaceState: async (): Promise<string> => {
|
getWorkspaceState: async (): Promise<string> => {
|
||||||
const block = buildWorkspaceNowBlock({
|
const block = await buildWorkspaceNowBlock({
|
||||||
dataDir: server.localConfig.dataDir,
|
dataDir: server.localConfig.dataDir,
|
||||||
waggleDb: server.waggleDb,
|
waggleDb: server.waggleDb,
|
||||||
|
memory: server.memory,
|
||||||
workspaceId: effectiveWorkspaceId,
|
workspaceId: effectiveWorkspaceId,
|
||||||
wsManager: server.workspaceManager,
|
wsManager: server.workspaceManager,
|
||||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
|
||||||
cronSchedules: server.cronStore.list(),
|
cronSchedules: server.cronStore.list(),
|
||||||
});
|
});
|
||||||
if (!block) return 'No workspace state available.';
|
if (!block) return 'No workspace state available.';
|
||||||
@@ -287,12 +302,12 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
|
|||||||
// (awareness + recent sessions + pending). Best-effort.
|
// (awareness + recent sessions + pending). Best-effort.
|
||||||
let memoryContext = '';
|
let memoryContext = '';
|
||||||
try {
|
try {
|
||||||
const block = buildWorkspaceNowBlock({
|
const block = await buildWorkspaceNowBlock({
|
||||||
dataDir: server.localConfig.dataDir,
|
dataDir: server.localConfig.dataDir,
|
||||||
waggleDb: server.waggleDb,
|
waggleDb: server.waggleDb,
|
||||||
|
memory: server.memory,
|
||||||
workspaceId: workspaceId ?? 'default',
|
workspaceId: workspaceId ?? 'default',
|
||||||
wsManager: server.workspaceManager,
|
wsManager: server.workspaceManager,
|
||||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
|
||||||
cronSchedules: server.cronStore.list(),
|
cronSchedules: server.cronStore.list(),
|
||||||
});
|
});
|
||||||
if (block) memoryContext = formatWorkspaceNowPrompt(block);
|
if (block) memoryContext = formatWorkspaceNowPrompt(block);
|
||||||
|
|||||||
@@ -61,12 +61,12 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
|
|||||||
},
|
},
|
||||||
|
|
||||||
getWorkspaceState: async (): Promise<string> => {
|
getWorkspaceState: async (): Promise<string> => {
|
||||||
const block = buildWorkspaceNowBlock({
|
const block = await buildWorkspaceNowBlock({
|
||||||
dataDir: server.localConfig.dataDir,
|
dataDir: server.localConfig.dataDir,
|
||||||
waggleDb: server.waggleDb,
|
waggleDb: server.waggleDb,
|
||||||
|
memory: server.memory,
|
||||||
workspaceId: effectiveWorkspaceId,
|
workspaceId: effectiveWorkspaceId,
|
||||||
wsManager: server.workspaceManager,
|
wsManager: server.workspaceManager,
|
||||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
|
||||||
cronSchedules: server.cronStore.list(),
|
cronSchedules: server.cronStore.list(),
|
||||||
});
|
});
|
||||||
if (!block) return 'No workspace state available.';
|
if (!block) return 'No workspace state available.';
|
||||||
|
|||||||
@@ -12,11 +12,11 @@
|
|||||||
* (typed WorkspaceState with `nextActions`, pending/blocked/recentDecisions).
|
* (typed WorkspaceState with `nextActions`, pending/blocked/recentDecisions).
|
||||||
* - `buildTimeAwareGreeting()` / `buildUpcomingSchedules()` —
|
* - `buildTimeAwareGreeting()` / `buildUpcomingSchedules()` —
|
||||||
* `packages/server/src/local/routes/workspace-context.ts:119,169`.
|
* `packages/server/src/local/routes/workspace-context.ts:119,169`.
|
||||||
* - `IdentityLayer` — `@waggle/core` (mind/identity.ts), read off the personal
|
* - `IdentityLayer` — `@waggle/core` (identity.ts), the profile card in
|
||||||
* mind for the user's name (B8: Home greets by name via existing identity).
|
* waggle.db, for the user's name (B8: Home greets by name via existing identity).
|
||||||
* - `server.workspaceManager` (list/get/getMindPath) + `server.agentState`
|
* - `server.workspaceManager` (list/get) + `server.cronStore`
|
||||||
* (activateWorkspaceMind) + `server.cronStore` (list/getExecutionHistory) +
|
* (list/getExecutionHistory) + `server.memory` + `server.waggleDb` +
|
||||||
* `server.multiMind.personal` + `getAuditDb()` (events.ts) — sidecar decorators.
|
* `getAuditDb()` (events.ts) — sidecar decorators.
|
||||||
*
|
*
|
||||||
* Founder decisions honored: A2 (Home Cockpit personal-only v1 — team/shared
|
* Founder decisions honored: A2 (Home Cockpit personal-only v1 — team/shared
|
||||||
* workspaces are excluded from the cross-workspace fan-out; team-overnight rows
|
* workspaces are excluded from the cross-workspace fan-out; team-overnight rows
|
||||||
@@ -25,8 +25,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import type { FastifyPluginAsync } from 'fastify';
|
import type { FastifyPluginAsync } from 'fastify';
|
||||||
import { FrameStore, IdentityLayer } from '@waggle/core';
|
import { IdentityLayer } from '@waggle/core';
|
||||||
import { normalizeToMemory } from './memory-center.js';
|
import { PERSONAL_SCOPE } from '@waggle/memory-contract';
|
||||||
|
import { memoryStatus } from '../memory-status.js';
|
||||||
import { buildWorkspaceState } from '../workspace-state.js';
|
import { buildWorkspaceState } from '../workspace-state.js';
|
||||||
import {
|
import {
|
||||||
buildTimeAwareGreeting,
|
buildTimeAwareGreeting,
|
||||||
@@ -74,7 +75,7 @@ interface HomeBriefing {
|
|||||||
upNext: UpNextItem[];
|
upNext: UpNextItem[];
|
||||||
activeModels?: string[];
|
activeModels?: string[];
|
||||||
isFirstRun: boolean;
|
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;
|
needsReviewCount: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,10 +114,8 @@ const MAX_UP_NEXT = 8;
|
|||||||
const OVERNIGHT_DEFAULT_HOURS = 24;
|
const OVERNIGHT_DEFAULT_HOURS = 24;
|
||||||
/** Per-schedule cron-history rows to scan for the overnight window. */
|
/** Per-schedule cron-history rows to scan for the overnight window. */
|
||||||
const CRON_HISTORY_SCAN = 50;
|
const CRON_HISTORY_SCAN = 50;
|
||||||
/** J08 needs-review scan bound — matches the 200-frame window the Memory
|
/** J08 needs-review scan bound: high enough to count every extracted fact. */
|
||||||
* Center list itself fetches (MemoryCenterTab limit=200), so the Home count
|
const NEEDS_REVIEW_SCAN = 10_000;
|
||||||
* always agrees with what the deep-linked "Needs review" view shows. */
|
|
||||||
const NEEDS_REVIEW_SCAN = 200;
|
|
||||||
/** Priority boost: each pending/blocked item ranks a workspace as if touched
|
/** Priority boost: each pending/blocked item ranks a workspace as if touched
|
||||||
* this much more recently (PRD §12.1 "ranked by recency AND priority"). */
|
* this much more recently (PRD §12.1 "ranked by recency AND priority"). */
|
||||||
const PENDING_BOOST_MS = 3_600_000; // 1 hour per pending item
|
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;
|
let anyStateForWs = false;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const state = buildWorkspaceState({
|
const state = await buildWorkspaceState({
|
||||||
dataDir: server.localConfig.dataDir,
|
dataDir: server.localConfig.dataDir,
|
||||||
waggleDb: server.waggleDb,
|
waggleDb: server.waggleDb,
|
||||||
|
memory: server.memory,
|
||||||
workspaceId: ws.id,
|
workspaceId: ws.id,
|
||||||
wsManager: server.workspaceManager,
|
wsManager: server.workspaceManager,
|
||||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
|
||||||
});
|
});
|
||||||
if (state) {
|
if (state) {
|
||||||
anyMemory = true;
|
anyMemory = true;
|
||||||
@@ -376,15 +375,18 @@ export const homeRoutes: FastifyPluginAsync = async (server) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── J08 (D6): personal-mind memories awaiting review ───────────
|
// ── J08 (D6): personal memories awaiting review ────────────────
|
||||||
// Personal-only by design: harvest (C33) stamps status:'unreviewed' on
|
// Facts the background extraction learned about the user stay unreviewed
|
||||||
// personal-mind imports, and the deep-linked Memory Center "Needs review"
|
// until confirmed. Personal-only by design: the deep-linked Memory Center
|
||||||
// view is personal-mind-only — a cross-mind count would not match it.
|
// "Needs review" view is personal-only — a cross-scope count would not match it.
|
||||||
let needsReviewCount = 0;
|
let needsReviewCount = 0;
|
||||||
try {
|
try {
|
||||||
for (const f of new FrameStore(server.multiMind.personal).getRecent(NEEDS_REVIEW_SCAN)) {
|
const extracted = await server.memory.list({
|
||||||
if (normalizeToMemory(f, 'personal').status === 'unreviewed') needsReviewCount++;
|
scopes: [PERSONAL_SCOPE],
|
||||||
}
|
origin: 'extraction',
|
||||||
|
limit: NEEDS_REVIEW_SCAN,
|
||||||
|
});
|
||||||
|
needsReviewCount = extracted.filter((record) => memoryStatus(record) === 'unreviewed').length;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.warn('briefing: needs-review scan failed', (err as Error).message);
|
log.warn('briefing: needs-review scan failed', (err as Error).message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
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 { assertSafeSegment } from './validate.js';
|
||||||
import { extractProgressItems, type ProgressItem } from './sessions.js';
|
|
||||||
import {
|
import {
|
||||||
buildWorkspaceState,
|
buildWorkspaceState,
|
||||||
formatWorkspaceStatePrompt,
|
listByImportance,
|
||||||
type WorkspaceState,
|
type WorkspaceState,
|
||||||
} from '../workspace-state.js';
|
} from '../workspace-state.js';
|
||||||
|
|
||||||
@@ -34,30 +34,30 @@ export type { WorkspaceState } from '../workspace-state.js';
|
|||||||
// ── Workspace Manager interface (minimal, passed via opts) ─────────────
|
// ── Workspace Manager interface (minimal, passed via opts) ─────────────
|
||||||
|
|
||||||
interface WsManagerLike {
|
interface WsManagerLike {
|
||||||
get: (id: string) => { id: string; name: string } | null;
|
get: (id: string) => { id: string; name: string } | null | undefined;
|
||||||
getMindPath: (id: string) => string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Summary builder (mirrors composeWorkspaceSummary from workspaces.ts) ──
|
// ── Summary builder (mirrors composeWorkspaceSummary from workspaces.ts) ──
|
||||||
|
|
||||||
|
/** `memories` are ordered most important first (see `listByImportance`). */
|
||||||
function buildCompactSummary(
|
function buildCompactSummary(
|
||||||
frames: Array<{ content: string; importance: string; created_at: string }>,
|
memories: MemoryRecord[],
|
||||||
memoryCount: number,
|
memoryCount: number,
|
||||||
sessionCount: number,
|
sessionCount: number,
|
||||||
): string {
|
): string {
|
||||||
const parts: string[] = [];
|
const parts: string[] = [];
|
||||||
|
|
||||||
// What this workspace is about (from important/critical frames)
|
// What this workspace is about (from important/critical memories)
|
||||||
const important = frames.filter(f => f.importance === 'critical' || f.importance === 'important');
|
const important = memories.filter(m => m.importance === 'critical' || m.importance === 'important');
|
||||||
const workContextFrames = frames.filter(f =>
|
const workContextMemories = memories.filter(m =>
|
||||||
f.content.toLowerCase().includes('project') ||
|
m.text.toLowerCase().includes('project') ||
|
||||||
f.content.toLowerCase().includes('workspace') ||
|
m.text.toLowerCase().includes('workspace') ||
|
||||||
f.content.toLowerCase().includes('working on'),
|
m.text.toLowerCase().includes('working on'),
|
||||||
);
|
);
|
||||||
|
|
||||||
const aboutFrame = important[0] ?? workContextFrames[0] ?? frames[0];
|
const aboutMemory = important[0] ?? workContextMemories[0] ?? memories[0];
|
||||||
if (aboutFrame) {
|
if (aboutMemory) {
|
||||||
const aboutLine = aboutFrame.content.split('\n')[0].replace(/\.\s*$/, '').trim();
|
const aboutLine = aboutMemory.text.split('\n')[0].replace(/\.\s*$/, '').trim();
|
||||||
const aboutText = aboutLine.length > 140 ? aboutLine.slice(0, 137) + '...' : aboutLine;
|
const aboutText = aboutLine.length > 140 ? aboutLine.slice(0, 137) + '...' : aboutLine;
|
||||||
if (aboutText.length > 10) {
|
if (aboutText.length > 10) {
|
||||||
parts.push(aboutText + '.');
|
parts.push(aboutText + '.');
|
||||||
@@ -65,7 +65,7 @@ function buildCompactSummary(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Current state (activity level + recency)
|
// 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 now = new Date();
|
||||||
const lastDate = mostRecent ? new Date(mostRecent) : null;
|
const lastDate = mostRecent ? new Date(mostRecent) : null;
|
||||||
const daysSince = lastDate ? Math.floor((now.getTime() - lastDate.getTime()) / (86400 * 1000)) : 0;
|
const daysSince = lastDate ? Math.floor((now.getTime() - lastDate.getTime()) / (86400 * 1000)) : 0;
|
||||||
@@ -221,206 +221,79 @@ export function buildUpcomingSchedules(schedules: CronScheduleLike[], workspaceI
|
|||||||
|
|
||||||
// ── Main builder ───────────────────────────────────────────────────────
|
// ── Main builder ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
export function buildWorkspaceNowBlock(opts: {
|
/** Null when the workspace is unknown or has no memories yet. */
|
||||||
|
export async function buildWorkspaceNowBlock(opts: {
|
||||||
dataDir: string;
|
dataDir: string;
|
||||||
workspaceId: string;
|
workspaceId: string;
|
||||||
wsManager: WsManagerLike;
|
wsManager: WsManagerLike;
|
||||||
/** waggle.db, which holds the workspace's awareness items. */
|
/** waggle.db, which holds the workspace's awareness items. */
|
||||||
waggleDb: WaggleDB;
|
waggleDb: WaggleDB;
|
||||||
/** @deprecated Read-only context construction no longer mutates global workspace state. */
|
memory: MemoryStore;
|
||||||
activateWorkspaceMind?: (id: string) => boolean;
|
|
||||||
/** Optional cron schedules for upcoming schedule display */
|
/** Optional cron schedules for upcoming schedule display */
|
||||||
cronSchedules?: CronScheduleLike[];
|
cronSchedules?: CronScheduleLike[];
|
||||||
}): WorkspaceNowBlock | null {
|
}): Promise<WorkspaceNowBlock | null> {
|
||||||
const { dataDir, workspaceId, wsManager, waggleDb } = opts;
|
const { dataDir, workspaceId, wsManager, waggleDb, memory } = opts;
|
||||||
|
|
||||||
// Path-traversal guard: workspaceId becomes a path segment below
|
// Path-traversal guard: workspaceId becomes a path segment below
|
||||||
// (getMindPath + dataDir/workspaces/<workspaceId>/sessions). Reject any
|
// (dataDir/workspaces/<workspaceId>/sessions). Reject any value containing
|
||||||
// value containing illegal characters before it touches the filesystem.
|
// illegal characters before it touches the filesystem.
|
||||||
assertSafeSegment(workspaceId, 'workspaceId');
|
assertSafeSegment(workspaceId, 'workspaceId');
|
||||||
|
|
||||||
const ws = wsManager.get(workspaceId);
|
const ws = wsManager.get(workspaceId);
|
||||||
if (!ws) return null;
|
if (!ws) return null;
|
||||||
|
|
||||||
const mindPath = wsManager.getMindPath(workspaceId);
|
const structuredState = await buildWorkspaceState({
|
||||||
if (!fs.existsSync(mindPath)) return null;
|
|
||||||
|
|
||||||
// ── Try structured state first (new path) ────────────────────
|
|
||||||
const structuredState = buildWorkspaceState({
|
|
||||||
dataDir,
|
dataDir,
|
||||||
workspaceId,
|
workspaceId,
|
||||||
wsManager,
|
wsManager,
|
||||||
waggleDb,
|
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) {
|
const progressItems: string[] = [];
|
||||||
// Convert structured state → WorkspaceNowBlock for backward compat
|
for (const p of structuredState.pending.slice(0, 3)) {
|
||||||
const activeThreads = structuredState.active.slice(0, 3).map(a => {
|
progressItems.push(`[task] ${p.content}`);
|
||||||
const ago = formatRelativeTime(new Date(a.dateLastTouched).getTime());
|
}
|
||||||
const label = a.content.length > 60 ? a.content.slice(0, 57) + '...' : a.content;
|
for (const b of structuredState.blocked.slice(0, 2)) {
|
||||||
return `${label} (${ago})`;
|
progressItems.push(`[blocker] ${b.content}`);
|
||||||
});
|
}
|
||||||
|
for (const c of structuredState.completed.slice(0, 2)) {
|
||||||
const progressItems: string[] = [];
|
progressItems.push(`[completed] ${c.content}`);
|
||||||
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,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── 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 summary = '';
|
||||||
let decisions: string[] = [];
|
|
||||||
let memoryCount = 0;
|
let memoryCount = 0;
|
||||||
let wsDb: MindDB | null = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
wsDb = new MindDB(mindPath);
|
const scope = workspaceScope(workspaceId);
|
||||||
const raw = wsDb.getDatabase();
|
memoryCount = await memory.count({ scopes: [scope] });
|
||||||
|
const memories = await listByImportance(memory, scope, 8);
|
||||||
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 sessDir = path.join(dataDir, 'workspaces', workspaceId, 'sessions');
|
const sessDir = path.join(dataDir, 'workspaces', workspaceId, 'sessions');
|
||||||
const sessCount = fs.existsSync(sessDir)
|
const sessCount = fs.existsSync(sessDir)
|
||||||
? fs.readdirSync(sessDir).filter(f => f.endsWith('.jsonl')).length
|
? fs.readdirSync(sessDir).filter(f => f.endsWith('.jsonl')).length
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
if (frames.length > 0) {
|
if (memories.length > 0) {
|
||||||
summary = buildCompactSummary(frames, memoryCount, sessCount);
|
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 {
|
} catch {
|
||||||
if (wsDb) { try { wsDb.close(); } catch { /* ignore */ } }
|
// Non-blocking — the block renders without a summary
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract progress items (cap at 5)
|
// Compute last active from active threads or structured state
|
||||||
const sessionsDir = path.join(dataDir, 'workspaces', workspaceId, 'sessions');
|
const lastActiveThread = structuredState.active[0]?.dateLastTouched ?? null;
|
||||||
let rawProgress: ProgressItem[] = [];
|
const greeting = buildTimeAwareGreeting(lastActiveThread, { frameCount: memoryCount });
|
||||||
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 });
|
|
||||||
const pendingTasks = extractPendingTasks(progressItems);
|
const pendingTasks = extractPendingTasks(progressItems);
|
||||||
const upcomingSchedules = buildUpcomingSchedules(opts.cronSchedules ?? [], workspaceId);
|
const upcomingSchedules = buildUpcomingSchedules(opts.cronSchedules ?? [], workspaceId);
|
||||||
|
|
||||||
@@ -428,12 +301,13 @@ export function buildWorkspaceNowBlock(opts: {
|
|||||||
workspaceName: ws.name as string,
|
workspaceName: ws.name as string,
|
||||||
summary,
|
summary,
|
||||||
recentDecisions: decisions,
|
recentDecisions: decisions,
|
||||||
activeThreads: [],
|
activeThreads,
|
||||||
progressItems,
|
progressItems: progressItems.slice(0, 5),
|
||||||
nextActions: nextActions.slice(0, 3),
|
nextActions: structuredState.nextActions.slice(0, 3),
|
||||||
greeting,
|
greeting,
|
||||||
pendingTasks,
|
pendingTasks,
|
||||||
upcomingSchedules,
|
upcomingSchedules,
|
||||||
|
structuredState,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import os from 'node:os';
|
import os from 'node:os';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import Database from 'better-sqlite3';
|
|
||||||
import type { FastifyPluginAsync } from 'fastify';
|
import type { FastifyPluginAsync } from 'fastify';
|
||||||
import { z } from 'zod';
|
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 { assertSafeSegment } from './validate.js';
|
||||||
import { validateBody } from '../../validate-body.js';
|
import { validateBody } from '../../validate-body.js';
|
||||||
|
|
||||||
@@ -49,7 +49,13 @@ const updateWorkspaceSchema = z.object({
|
|||||||
}).strict();
|
}).strict();
|
||||||
import { extractProgressItems, type ProgressItem } from './sessions.js';
|
import { extractProgressItems, type ProgressItem } from './sessions.js';
|
||||||
import { readFileRegistry, type FileRegistryEntry } from './ingest.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 { buildTimeAwareGreeting, buildUpcomingSchedules } from './workspace-context.js';
|
||||||
import { emitAuditEvent, getAuditDb } from './events.js';
|
import { emitAuditEvent, getAuditDb } from './events.js';
|
||||||
import { createLogger } from '../logger.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."
|
* 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(
|
function composeWorkspaceSummary(
|
||||||
frames: Array<{ content: string; importance: string; created_at: string }>,
|
memories: MemoryRecord[],
|
||||||
memoryCount: number,
|
memoryCount: number,
|
||||||
decisions: Array<{ content: string; created_at: string }>,
|
|
||||||
sessionCount: number,
|
sessionCount: number,
|
||||||
): string {
|
): string {
|
||||||
const parts: string[] = [];
|
const parts: string[] = [];
|
||||||
|
|
||||||
// ── What this workspace is about (from important/critical frames) ──
|
// ── What this workspace is about (from important/critical memories) ──
|
||||||
const important = frames.filter(f => f.importance === 'critical' || f.importance === 'important');
|
const important = memories.filter(m => m.importance === 'critical' || m.importance === 'important');
|
||||||
const workContextFrames = frames.filter(f =>
|
const workContextMemories = memories.filter(m =>
|
||||||
f.content.toLowerCase().includes('project') ||
|
m.text.toLowerCase().includes('project') ||
|
||||||
f.content.toLowerCase().includes('workspace') ||
|
m.text.toLowerCase().includes('workspace') ||
|
||||||
f.content.toLowerCase().includes('working on'),
|
m.text.toLowerCase().includes('working on'),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Find the best "about" frame
|
// Find the best "about" memory
|
||||||
const aboutFrame = important[0] ?? workContextFrames[0] ?? frames[0];
|
const aboutMemory = important[0] ?? workContextMemories[0] ?? memories[0];
|
||||||
const aboutLine = aboutFrame.content.split('\n')[0].replace(/\.\s*$/, '').trim();
|
const aboutLine = aboutMemory.text.split('\n')[0].replace(/\.\s*$/, '').trim();
|
||||||
const aboutText = aboutLine.length > 140 ? aboutLine.slice(0, 137) + '...' : aboutLine;
|
const aboutText = aboutLine.length > 140 ? aboutLine.slice(0, 137) + '...' : aboutLine;
|
||||||
if (aboutText.length > 10) {
|
if (aboutText.length > 10) {
|
||||||
parts.push(aboutText + '.');
|
parts.push(aboutText + '.');
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Current state (activity level + recency) ──
|
// ── 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 now = new Date();
|
||||||
const lastDate = mostRecent ? new Date(mostRecent) : null;
|
const lastDate = mostRecent ? new Date(mostRecent) : null;
|
||||||
const daysSince = lastDate ? Math.floor((now.getTime() - lastDate.getTime()) / (86400 * 1000)) : 0;
|
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
|
* Wave R (Lane B): per-workspace memory count for the list route. Returns
|
||||||
* READONLY raw sqlite connection (no MindDB schema-init, no sqlite-vec load) and
|
* undefined on any error so the card omits the count rather than fabricating a 0.
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
function countWorkspaceMemories(mindPath: string): number | undefined {
|
async function countWorkspaceMemories(memory: MemoryStore, workspaceId: string): Promise<number | undefined> {
|
||||||
if (!fs.existsSync(mindPath)) return undefined;
|
|
||||||
let db: import('better-sqlite3').Database | undefined;
|
|
||||||
try {
|
try {
|
||||||
db = new Database(mindPath, { readonly: true, fileMustExist: true });
|
return await memory.count({ scopes: [workspaceScope(workspaceId)] });
|
||||||
const row = db.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number };
|
|
||||||
return row.cnt;
|
|
||||||
} catch {
|
} catch {
|
||||||
return undefined;
|
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.
|
// 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.
|
// - sessionCount: a readdir of the same sessions dir /:id/context counts.
|
||||||
// - memoryCount: a readonly COUNT(*) over the workspace mind (countWorkspace-
|
// - memoryCount: the workspace's memory count — the R-round "living
|
||||||
// Memories) — this is the R-round "living identity" unlock. It opens a
|
// identity" unlock. Bounded to the user's own handful of workspaces.
|
||||||
// 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.
|
|
||||||
// - lastSessionTitle: the newest session's title, for the card's "Last: …"
|
// - lastSessionTitle: the newest session's title, for the card's "Last: …"
|
||||||
// activity body.
|
// activity body.
|
||||||
// Every enrichment omits its field on any error — never a fabricated 0/string.
|
// 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 {
|
try {
|
||||||
const sessionsDir = path.join(server.localConfig.dataDir, 'workspaces', ws.id, 'sessions');
|
const sessionsDir = path.join(server.localConfig.dataDir, 'workspaces', ws.id, 'sessions');
|
||||||
const sessionCount = fs.existsSync(sessionsDir)
|
const sessionCount = fs.existsSync(sessionsDir)
|
||||||
? fs.readdirSync(sessionsDir).filter((f) => f.endsWith('.jsonl')).length
|
? fs.readdirSync(sessionsDir).filter((f) => f.endsWith('.jsonl')).length
|
||||||
: 0;
|
: 0;
|
||||||
const memoryCount = countWorkspaceMemories(server.workspaceManager.getMindPath(ws.id));
|
const memoryCount = await countWorkspaceMemories(server.memory, ws.id);
|
||||||
const lastSessionTitle = readLastSessionTitle(sessionsDir);
|
const lastSessionTitle = readLastSessionTitle(sessionsDir);
|
||||||
return {
|
return {
|
||||||
...ws,
|
...ws,
|
||||||
@@ -298,7 +290,7 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
|||||||
} catch {
|
} catch {
|
||||||
return ws;
|
return ws;
|
||||||
}
|
}
|
||||||
});
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
// POST /api/workspaces — create workspace
|
// 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') {
|
if (resolvedTemplateId && resolvedTemplateId !== 'blank') {
|
||||||
try {
|
try {
|
||||||
const { BUILT_IN_TEMPLATES } = await import('./workspace-templates.js');
|
const { BUILT_IN_TEMPLATES } = await import('./workspace-templates.js');
|
||||||
const tpl = BUILT_IN_TEMPLATES.find(t => t.id === resolvedTemplateId);
|
const starterMemory = BUILT_IN_TEMPLATES.find(t => t.id === resolvedTemplateId)?.starterMemory ?? [];
|
||||||
if (tpl?.starterMemory?.length) {
|
// Written in the background: the first memory write may wait for the
|
||||||
const mindPath = server.workspaceManager.getMindPath(ws.id);
|
// embedding model to load, and workspace creation must not.
|
||||||
const wsDb = new MindDB(mindPath);
|
void (async () => {
|
||||||
const raw = wsDb.getDatabase();
|
for (const text of starterMemory) {
|
||||||
// Create a synthetic session for onboarding frames (FK enforced)
|
if (!server.workspaceManager.get(ws.id)) return; // deleted meanwhile
|
||||||
raw.prepare(
|
await server.memory.remember({
|
||||||
"INSERT OR IGNORE INTO sessions (gop_id, project_id, status, summary) VALUES (?, ?, 'closed', 'Workspace template starter content')"
|
scope: workspaceScope(ws.id),
|
||||||
).run(`onboarding-${ws.id}`, ws.id);
|
text,
|
||||||
// Seed each starter memory as an I-frame
|
kind: 'note',
|
||||||
const insert = raw.prepare(
|
source: 'system',
|
||||||
"INSERT INTO memory_frames (frame_type, gop_id, t, content, importance, source) VALUES ('I', ?, ?, ?, 'normal', 'system')"
|
origin: 'workspace-template',
|
||||||
);
|
});
|
||||||
for (let i = 0; i < tpl.starterMemory.length; i++) {
|
|
||||||
insert.run(`onboarding-${ws.id}`, i, tpl.starterMemory[i]);
|
|
||||||
}
|
}
|
||||||
// These raw INSERTs bypass FrameStore.indexFts(), so the starter frames
|
})().catch((err: unknown) => {
|
||||||
// would be present in memory_frames but invisible to FTS5 keyword search.
|
log.warn(`[waggle] Starter memory seeding failed:`, (err as Error).message);
|
||||||
// Reconcile the FTS index to make them searchable.
|
});
|
||||||
reconcileFtsIndex(wsDb);
|
|
||||||
wsDb.close();
|
|
||||||
}
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Non-blocking — workspace creation succeeds even if memory seeding fails
|
// Non-blocking — workspace creation succeeds even if memory seeding fails
|
||||||
log.warn(`[waggle] Starter memory seeding failed:`, (err as Error).message);
|
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 recentMemories: Array<{ content: string; importance: string; source: string; date: string }> = [];
|
||||||
let recentDecisions: Array<{ content: string; source: string; date: string }> = [];
|
let recentDecisions: Array<{ content: string; source: string; date: string }> = [];
|
||||||
|
|
||||||
const mindPath = server.workspaceManager.getMindPath(id);
|
try {
|
||||||
if (fs.existsSync(mindPath)) {
|
// Opening a workspace makes it the active one
|
||||||
try {
|
server.agentState.activateWorkspaceMind(id);
|
||||||
// Activate workspace mind to access its data
|
|
||||||
server.agentState.activateWorkspaceMind(id);
|
|
||||||
|
|
||||||
// Read workspace memory stats and recent frames
|
const scope = workspaceScope(id);
|
||||||
const wsDb = new MindDB(mindPath);
|
memoryCount = await server.memory.count({ scopes: [scope] });
|
||||||
const raw = wsDb.getDatabase();
|
|
||||||
|
|
||||||
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
|
recentMemories = memories.map(m => ({
|
||||||
// D2: Order by importance first (matching A3 preloaded-context fix), then recency
|
content: m.text.slice(0, 200),
|
||||||
const frames = raw.prepare(
|
importance: m.importance,
|
||||||
`SELECT content, importance, source, created_at FROM memory_frames
|
source: m.source,
|
||||||
WHERE importance != 'deprecated' AND importance != 'temporary'
|
date: m.createdAt.slice(0, 10),
|
||||||
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 = frames.map(f => ({
|
recentDecisions = (await listDecisions(server.memory, scope, 5)).map(m => {
|
||||||
content: f.content.slice(0, 200),
|
const firstLine = m.text.split('\n')[0];
|
||||||
importance: f.importance,
|
const sentenceMatch = firstLine.match(/^(.+?\.\s)(?=[A-Z])/);
|
||||||
source: f.source,
|
const text = sentenceMatch
|
||||||
date: f.created_at?.slice(0, 10) ?? 'unknown',
|
? 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)
|
// A6: Build structured summary with session count
|
||||||
const decisionFrames = raw.prepare(
|
if (memories.length > 0) {
|
||||||
`SELECT content, source, created_at FROM memory_frames
|
// Count sessions from filesystem (we'll have accurate count later, estimate here)
|
||||||
WHERE importance != 'deprecated' AND importance != 'temporary'
|
const sessDir = path.join(server.localConfig.dataDir, 'workspaces', id, 'sessions');
|
||||||
AND (content LIKE 'Decision%' OR content LIKE '%decided%'
|
const sessCount = fs.existsSync(sessDir) ? fs.readdirSync(sessDir).filter(f => f.endsWith('.jsonl')).length : 0;
|
||||||
OR content LIKE '%decision made%' OR content LIKE '%chose %'
|
summary = composeWorkspaceSummary(memories, memoryCount, sessCount);
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
|
// If workspace memory can't be read, continue with empty context
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Gather session info ──────────────────────────────────
|
// ── Gather session info ──────────────────────────────────
|
||||||
@@ -715,12 +676,12 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
|||||||
// ── Structured workspace state (Slice 6) ──────────────
|
// ── Structured workspace state (Slice 6) ──────────────
|
||||||
let workspaceState: WorkspaceState | null = null;
|
let workspaceState: WorkspaceState | null = null;
|
||||||
try {
|
try {
|
||||||
workspaceState = buildWorkspaceState({
|
workspaceState = await buildWorkspaceState({
|
||||||
dataDir: server.localConfig.dataDir,
|
dataDir: server.localConfig.dataDir,
|
||||||
waggleDb: server.waggleDb,
|
waggleDb: server.waggleDb,
|
||||||
|
memory: server.memory,
|
||||||
workspaceId: id,
|
workspaceId: id,
|
||||||
wsManager: server.workspaceManager,
|
wsManager: server.workspaceManager,
|
||||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
|
||||||
});
|
});
|
||||||
} catch { /* non-blocking */ }
|
} catch { /* non-blocking */ }
|
||||||
|
|
||||||
@@ -775,9 +736,9 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
|||||||
} catch { /* non-blocking — cron store may not be available */ }
|
} catch { /* non-blocking — cron store may not be available */ }
|
||||||
|
|
||||||
// ── Wave 6.5 (DISABLED): Cross-workspace relevance hints ──
|
// ── Wave 6.5 (DISABLED): Cross-workspace relevance hints ──
|
||||||
// Security review (cowork/Code-Review_MultiMind_April-2026.md Critical #1) flagged
|
// A security review (Critical #1) flagged the original implementation as a
|
||||||
// the original implementation as a privacy leak: it iterated every workspace the
|
// privacy leak: it iterated every workspace the user owns, read each
|
||||||
// user owns, opened each MindDB directly, and returned 80-char content snippets in
|
// 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
|
// 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.
|
// 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;
|
let state: WorkspaceState | null = null;
|
||||||
try {
|
try {
|
||||||
state = buildWorkspaceState({
|
state = await buildWorkspaceState({
|
||||||
dataDir: server.localConfig.dataDir,
|
dataDir: server.localConfig.dataDir,
|
||||||
waggleDb: server.waggleDb,
|
waggleDb: server.waggleDb,
|
||||||
|
memory: server.memory,
|
||||||
workspaceId: id,
|
workspaceId: id,
|
||||||
wsManager: server.workspaceManager,
|
wsManager: server.workspaceManager,
|
||||||
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
|
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.warn(`state build failed for ${id}:`, (err as Error).message);
|
log.warn(`state build failed for ${id}:`, (err as Error).message);
|
||||||
@@ -1024,10 +985,21 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
|||||||
if (!existing) {
|
if (!existing) {
|
||||||
return reply.status(404).send({ error: 'Workspace not found' });
|
return reply.status(404).send({ error: 'Workspace not found' });
|
||||||
}
|
}
|
||||||
// A6: Close workspace mind DB before filesystem deletion to prevent EBUSY
|
const id = request.params.id;
|
||||||
server.agentState.closeWorkspaceMind(request.params.id);
|
// Its memories and awareness items live outside the workspace directory.
|
||||||
server.workspaceManager.delete(request.params.id);
|
// Forget them first: a workspace created later under the same name gets
|
||||||
emitAuditEvent(server, { workspaceId: request.params.id, eventType: 'workspace_delete' });
|
// 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();
|
return reply.status(204).send();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1045,21 +1017,16 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ── Gather memories ──────────────────────────────────────────
|
// ── Gather memories ──────────────────────────────────────────
|
||||||
const memories: Array<{ content: string; importance: string; created_at: string; frame_type?: string }> = [];
|
const memories: Array<{ content: string; importance: string; created_at: string; kind: string }> = [];
|
||||||
const mindPath = server.workspaceManager.getMindPath(id);
|
try {
|
||||||
if (fs.existsSync(mindPath)) {
|
const records = await server.memory.list({ scopes: [workspaceScope(id)], limit: 500 });
|
||||||
try {
|
memories.push(...records.map(r => ({
|
||||||
const wsDb = new MindDB(mindPath);
|
content: r.text,
|
||||||
const raw = wsDb.getDatabase();
|
importance: r.importance,
|
||||||
const frames = raw.prepare(
|
created_at: r.createdAt,
|
||||||
`SELECT content, importance, created_at, frame_type FROM memory_frames
|
kind: r.kind,
|
||||||
WHERE importance != 'deprecated'
|
})));
|
||||||
ORDER BY id DESC LIMIT 500`
|
} catch { /* non-blocking */ }
|
||||||
).all() as Array<{ content: string; importance: string; created_at: string; frame_type: string }>;
|
|
||||||
memories.push(...frames);
|
|
||||||
wsDb.close();
|
|
||||||
} catch { /* non-blocking */ }
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── Gather pinned items ──────────────────────────────────────
|
// ── Gather pinned items ──────────────────────────────────────
|
||||||
let pinnedItems: Array<{ id: string; messageContent: string; pinnedAt: string; label?: string }> = [];
|
let pinnedItems: Array<{ id: string; messageContent: string; pinnedAt: string; label?: string }> = [];
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* Workspace State — structured current-state reconstruction for a workspace.
|
* 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."
|
* and awareness layer into a coherent model of "what's going on now."
|
||||||
*
|
*
|
||||||
* Design principles:
|
* Design principles:
|
||||||
@@ -12,7 +12,13 @@
|
|||||||
|
|
||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
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 {
|
import {
|
||||||
extractProgressItems,
|
extractProgressItems,
|
||||||
extractOpenQuestions,
|
extractOpenQuestions,
|
||||||
@@ -72,42 +78,62 @@ export function computeFreshness(dateStr: string): Freshness {
|
|||||||
return 'stale';
|
return 'stale';
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Decision extraction from memory frames ─────────────────────────────
|
// ── Workspace memory reads ─────────────────────────────────────────────
|
||||||
|
|
||||||
interface DecisionFrame {
|
/** Recent memories scanned for decisions. */
|
||||||
id: number;
|
const DECISION_SCAN = 200;
|
||||||
content: string;
|
/** Wording that marks a note as a decision when its kind does not. */
|
||||||
created_at: string;
|
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(
|
/** The newest decisions of a scope, temporary memories excluded. */
|
||||||
raw: ReturnType<MindDB['getDatabase']>,
|
export async function listDecisions(
|
||||||
|
memory: MemoryStore,
|
||||||
|
scope: MemoryScope,
|
||||||
max: number,
|
max: number,
|
||||||
): StateItem[] {
|
): Promise<MemoryRecord[]> {
|
||||||
const frames = raw.prepare(
|
const recent = await memory.list({ scopes: [scope], minImportance: 'normal', limit: DECISION_SCAN });
|
||||||
`SELECT id, content, created_at FROM memory_frames
|
return recent.filter(isDecision).slice(0, max);
|
||||||
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[];
|
|
||||||
|
|
||||||
return frames.slice(0, max).map(f => {
|
/**
|
||||||
const firstLine = f.content.split('\n')[0];
|
* Up to `limit` memories of a scope, most important first and newest first
|
||||||
const sentenceMatch = firstLine.match(/^(.+?\.\s)(?=[A-Z])/);
|
* within one importance level; temporary memories are excluded.
|
||||||
const text = sentenceMatch ? sentenceMatch[1] : firstLine;
|
*/
|
||||||
|
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 {
|
function toDecisionItem(record: MemoryRecord): StateItem {
|
||||||
// Strip cursor/markdown artifacts (leaking `|` pipes etc.) + tail-truncate.
|
const firstLine = record.text.split('\n')[0];
|
||||||
content: sanitizeExtracted(text, 150),
|
const sentenceMatch = firstLine.match(/^(.+?\.\s)(?=[A-Z])/);
|
||||||
freshness: computeFreshness(f.created_at),
|
const text = sentenceMatch ? sentenceMatch[1] : firstLine;
|
||||||
source: 'memory' as StateSource,
|
|
||||||
sourceId: String(f.id),
|
return {
|
||||||
dateLastTouched: f.created_at.slice(0, 10),
|
// 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 ──────────────────────────────────
|
// ── Active items from awareness layer ──────────────────────────────────
|
||||||
@@ -209,8 +235,7 @@ function deriveNextActions(state: Omit<WorkspaceState, 'nextActions'>, max: numb
|
|||||||
// ── Main builder ───────────────────────────────────────────────────────
|
// ── Main builder ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
interface WsManagerLike {
|
interface WsManagerLike {
|
||||||
get: (id: string) => { id: string; name: string } | null;
|
get: (id: string) => { id: string; name: string } | null | undefined;
|
||||||
getMindPath: (id: string) => string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface BuildWorkspaceStateOpts {
|
export interface BuildWorkspaceStateOpts {
|
||||||
@@ -219,40 +244,25 @@ export interface BuildWorkspaceStateOpts {
|
|||||||
wsManager: WsManagerLike;
|
wsManager: WsManagerLike;
|
||||||
/** waggle.db, which holds the workspace's awareness items. */
|
/** waggle.db, which holds the workspace's awareness items. */
|
||||||
waggleDb: WaggleDB;
|
waggleDb: WaggleDB;
|
||||||
/** @deprecated Read-only state construction no longer mutates global workspace state. */
|
memory: MemoryStore;
|
||||||
activateWorkspaceMind?: (id: string) => boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildWorkspaceState(opts: BuildWorkspaceStateOpts): WorkspaceState | null {
|
/** Null when the workspace is unknown, has no memories yet, or its memory cannot be read. */
|
||||||
const { dataDir, workspaceId, wsManager, waggleDb } = opts;
|
export async function buildWorkspaceState(opts: BuildWorkspaceStateOpts): Promise<WorkspaceState | null> {
|
||||||
|
const { dataDir, workspaceId, wsManager, waggleDb, memory } = opts;
|
||||||
|
|
||||||
const ws = wsManager.get(workspaceId);
|
const ws = wsManager.get(workspaceId);
|
||||||
if (!ws) return null;
|
if (!ws) return null;
|
||||||
|
|
||||||
const mindPath = wsManager.getMindPath(workspaceId);
|
|
||||||
if (!fs.existsSync(mindPath)) return null;
|
|
||||||
|
|
||||||
// ── Memory-sourced state ─────────────────────────────────────────
|
// ── Memory-sourced state ─────────────────────────────────────────
|
||||||
let recentDecisions: StateItem[] = [];
|
let recentDecisions: StateItem[] = [];
|
||||||
let awarenessItems: StateItem[] = [];
|
let awarenessItems: StateItem[] = [];
|
||||||
let wsDb: MindDB | null = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
wsDb = new MindDB(mindPath);
|
const scope = workspaceScope(workspaceId);
|
||||||
const raw = wsDb.getDatabase();
|
if (await memory.count({ scopes: [scope] }) === 0) return null;
|
||||||
|
recentDecisions = (await listDecisions(memory, scope, 5)).map(toDecisionItem);
|
||||||
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;
|
|
||||||
} catch {
|
} catch {
|
||||||
if (wsDb) { try { wsDb.close(); } catch { /* ignore */ } }
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ describe('local server default workspace wiring', () => {
|
|||||||
const defaultWorkspaceId = server.workspaceManager.getDefault();
|
const defaultWorkspaceId = server.workspaceManager.getDefault();
|
||||||
expect(defaultWorkspaceId).toBeTruthy();
|
expect(defaultWorkspaceId).toBeTruthy();
|
||||||
expect(server.agentState.activeWorkspaceId).toBe(defaultWorkspaceId);
|
expect(server.agentState.activeWorkspaceId).toBe(defaultWorkspaceId);
|
||||||
expect(server.agentState.getWorkspaceMindDb(defaultWorkspaceId!)).toBeTruthy();
|
expect(server.agentState.orchestrator.getWorkspaceId()).toBe(defaultWorkspaceId);
|
||||||
expect(server.agentState.getWorkspaceMindDb('default')).toBeNull();
|
expect(server.workspaceManager.get('default')).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -6,12 +6,11 @@
|
|||||||
* (B8 / PRD §12.1 "greeting with user name") — unit + via inject.
|
* (B8 / PRD §12.1 "greeting with user name") — unit + via inject.
|
||||||
* - applyPriorityRanking(): recency + bounded pending-item boost
|
* - applyPriorityRanking(): recency + bounded pending-item boost
|
||||||
* (PRD §12.1 "ranked by recency and priority") — unit.
|
* (PRD §12.1 "ranked by recency and priority") — unit.
|
||||||
* - needsReviewCount (J08/D6): personal-mind frames with metadata status
|
* - needsReviewCount (J08/D6): extracted personal facts that are neither
|
||||||
* 'unreviewed' are counted; the count matches what the Memory Center
|
* confirmed nor archived are counted.
|
||||||
* "Needs review" view lists.
|
|
||||||
*
|
*
|
||||||
* Scaffolding follows memory-center.test.ts: Fastify inject + MindDB(':memory:')
|
* Scaffolding: Fastify inject + an in-memory memory store + plain-object
|
||||||
* + plain-object decorators, plus an in-memory waggle.db for the profile card.
|
* decorators, plus an in-memory waggle.db for the profile card.
|
||||||
* localConfig is intentionally absent → the
|
* localConfig is intentionally absent → the
|
||||||
* buildWorkspaceState call inside the briefing loop throws and is absorbed by
|
* buildWorkspaceState call inside the briefing loop throws and is absorbed by
|
||||||
* the route's per-workspace try/catch (pendingCount degrades to 0), and
|
* 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 { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||||
import Fastify from 'fastify';
|
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 {
|
import {
|
||||||
homeRoutes,
|
homeRoutes,
|
||||||
personalizeGreeting,
|
personalizeGreeting,
|
||||||
applyPriorityRanking,
|
applyPriorityRanking,
|
||||||
type RecentWorkspaceCard,
|
type RecentWorkspaceCard,
|
||||||
} from '../../src/local/routes/home.js';
|
} from '../../src/local/routes/home.js';
|
||||||
import { memoryCenterRoutes } from '../../src/local/routes/memory-center.js';
|
|
||||||
import {
|
import {
|
||||||
buildUpcomingSchedules,
|
buildUpcomingSchedules,
|
||||||
type CronScheduleLike,
|
type CronScheduleLike,
|
||||||
@@ -43,7 +42,7 @@ interface TestWorkspace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function createTestServer(
|
function createTestServer(
|
||||||
db: MindDB,
|
memory: MemoryStore,
|
||||||
workspaces: TestWorkspace[] = [],
|
workspaces: TestWorkspace[] = [],
|
||||||
cronSchedules: CronScheduleLike[] = [],
|
cronSchedules: CronScheduleLike[] = [],
|
||||||
) {
|
) {
|
||||||
@@ -51,18 +50,7 @@ function createTestServer(
|
|||||||
const waggleDb = new WaggleDB(':memory:');
|
const waggleDb = new WaggleDB(':memory:');
|
||||||
server.decorate('waggleDb', waggleDb);
|
server.decorate('waggleDb', waggleDb);
|
||||||
server.addHook('onClose', async () => waggleDb.close());
|
server.addHook('onClose', async () => waggleDb.close());
|
||||||
server.decorate('multiMind', {
|
server.decorate('memory', memory);
|
||||||
personal: db,
|
|
||||||
getFrameStore: () => undefined,
|
|
||||||
search: () => [],
|
|
||||||
workspace: undefined,
|
|
||||||
setWorkspace: () => {},
|
|
||||||
});
|
|
||||||
server.decorate('agentState', {
|
|
||||||
getWorkspaceMindDb: () => undefined,
|
|
||||||
activateWorkspaceMind: () => undefined,
|
|
||||||
listWorkspaces: () => [],
|
|
||||||
});
|
|
||||||
server.decorate('workspaceManager', {
|
server.decorate('workspaceManager', {
|
||||||
list: () => workspaces,
|
list: () => workspaces,
|
||||||
get: (id: string) => workspaces.find((w) => w.id === id),
|
get: (id: string) => workspaces.find((w) => w.id === id),
|
||||||
@@ -73,7 +61,6 @@ function createTestServer(
|
|||||||
});
|
});
|
||||||
// localConfig intentionally absent — see file header.
|
// localConfig intentionally absent — see file header.
|
||||||
server.register(homeRoutes);
|
server.register(homeRoutes);
|
||||||
server.register(memoryCenterRoutes);
|
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,17 +115,16 @@ describe('applyPriorityRanking (P2 — recency + pending boost)', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('GET /api/home/briefing (P2 — J08 needsReviewCount + greeting)', () => {
|
describe('GET /api/home/briefing (P2 — J08 needsReviewCount + greeting)', () => {
|
||||||
let db: MindDB;
|
let memory: MemoryStore;
|
||||||
let server: ReturnType<typeof Fastify>;
|
let server: ReturnType<typeof Fastify>;
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
await server.close();
|
await server.close();
|
||||||
db.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
function boot(workspaces: TestWorkspace[] = []) {
|
function boot(workspaces: TestWorkspace[] = []) {
|
||||||
db = new MindDB(':memory:');
|
memory = createInMemoryMemoryStore();
|
||||||
server = createTestServer(db, workspaces);
|
server = createTestServer(memory, workspaces);
|
||||||
}
|
}
|
||||||
|
|
||||||
it('empty mind → needsReviewCount 0, isFirstRun true', async () => {
|
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);
|
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();
|
boot();
|
||||||
// Seed via the same routes the product uses: create (status active) then
|
const extracted = (text: string, metadata: Record<string, unknown> = {}) => memory.remember({
|
||||||
// flip one to unreviewed — the C33 harvest lifecycle state.
|
scope: PERSONAL_SCOPE, text, origin: 'extraction', source: 'agent_inferred', kind: 'profile', metadata,
|
||||||
const m1 = await server.inject({
|
|
||||||
method: 'POST', url: '/api/memory',
|
|
||||||
payload: { content: 'Imported fact awaiting review.' },
|
|
||||||
});
|
});
|
||||||
expect(m1.statusCode).toBe(200);
|
await extracted('Prefers short answers.');
|
||||||
const m2 = await server.inject({
|
await extracted('Lives in Belgrade.');
|
||||||
method: 'POST', url: '/api/memory',
|
await extracted('Works on Waggle.', { confirmedAt: '2026-10-24T10:00:00.000Z' });
|
||||||
payload: { content: 'Already-reviewed fact.' },
|
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' });
|
const res = await server.inject({ method: 'GET', url: '/api/home/briefing' });
|
||||||
expect(res.statusCode).toBe(200);
|
expect(res.statusCode).toBe(200);
|
||||||
expect(res.json().needsReviewCount).toBe(1);
|
expect(res.json().needsReviewCount).toBe(2);
|
||||||
|
|
||||||
// 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);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('briefing slices recent workspaces to 6, newest first (route-level ranking wiring)', async () => {
|
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)', () => {
|
describe('upNext schedule aggregation (global jobs dedup + future-only)', () => {
|
||||||
let db: MindDB;
|
let memory: MemoryStore;
|
||||||
let server: ReturnType<typeof Fastify>;
|
let server: ReturnType<typeof Fastify>;
|
||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
await server.close();
|
await server.close();
|
||||||
db.close();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const FUTURE = new Date(Date.now() + 60 * 60 * 1000).toISOString();
|
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 () => {
|
it('a global schedule surfaces once across multiple workspaces, not once per workspace', async () => {
|
||||||
db = new MindDB(':memory:');
|
server = createTestServer(createInMemoryMemoryStore(), threeWorkspaces, [
|
||||||
server = createTestServer(db, threeWorkspaces, [
|
|
||||||
globalSchedule(1, 'Memory compaction', FUTURE),
|
globalSchedule(1, 'Memory compaction', FUTURE),
|
||||||
]);
|
]);
|
||||||
const res = await server.inject({ method: 'GET', url: '/api/home/briefing' });
|
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 () => {
|
it('past-due schedules never display as upcoming', async () => {
|
||||||
db = new MindDB(':memory:');
|
server = createTestServer(createInMemoryMemoryStore(), threeWorkspaces, [
|
||||||
server = createTestServer(db, threeWorkspaces, [
|
|
||||||
globalSchedule(1, 'Memory compaction', PAST),
|
globalSchedule(1, 'Memory compaction', PAST),
|
||||||
globalSchedule(2, 'Harvest sync', FUTURE),
|
globalSchedule(2, 'Harvest sync', FUTURE),
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -2,43 +2,47 @@ import { describe, it, expect, vi } from 'vitest';
|
|||||||
import os from 'node:os';
|
import os from 'node:os';
|
||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
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';
|
import { buildWorkspaceNowBlock } from '../../src/local/routes/workspace-context.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* R6-006 path-traversal regression.
|
* R6-006 path-traversal regression.
|
||||||
*
|
*
|
||||||
* `buildWorkspaceNowBlock` builds session-directory paths from an unvalidated
|
* `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
|
* enabling an existence/count probe outside the workspaces root. A guard
|
||||||
* (assertSafeSegment) now runs at the entry of the helper and THROWS a
|
* (assertSafeSegment) now runs at the entry of the helper and THROWS a
|
||||||
* { statusCode: 400 } error before the value touches the filesystem; the
|
* { statusCode: 400 } error before the value touches the filesystem; the
|
||||||
* calling route handlers propagate that to Fastify's default error handler.
|
* calling route handlers propagate that to Fastify's default error handler.
|
||||||
*
|
*
|
||||||
* These are unit tests against the exported helper directly (a full route
|
* 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', () => {
|
describe('R6-006 workspace-context path traversal', () => {
|
||||||
// A throwaway data dir; the guard fires before this is ever read for the
|
// 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.
|
// malicious cases, and the valid case short-circuits on a null wsManager.get.
|
||||||
const tmpDataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'wctx-traversal-'));
|
const tmpDataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'wctx-traversal-'));
|
||||||
|
const waggleDb = new WaggleDB(':memory:');
|
||||||
|
|
||||||
const maliciousIds = ['../evil', '..%2f..', '../../etc', 'ws/../../secret', '..\\evil'];
|
const maliciousIds = ['../evil', '..%2f..', '../../etc', 'ws/../../secret', '..\\evil'];
|
||||||
|
|
||||||
for (const bad of maliciousIds) {
|
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 = {
|
const wsManager = {
|
||||||
get: vi.fn(() => ({ id: bad, name: 'evil' })),
|
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;
|
let thrown: unknown;
|
||||||
try {
|
try {
|
||||||
buildWorkspaceNowBlock({
|
await buildWorkspaceNowBlock({
|
||||||
dataDir: tmpDataDir,
|
dataDir: tmpDataDir,
|
||||||
workspaceId: bad,
|
workspaceId: bad,
|
||||||
wsManager,
|
wsManager,
|
||||||
activateWorkspaceMind,
|
waggleDb,
|
||||||
|
memory,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
thrown = e;
|
thrown = e;
|
||||||
@@ -46,32 +50,27 @@ describe('R6-006 workspace-context path traversal', () => {
|
|||||||
|
|
||||||
expect(thrown).toBeInstanceOf(Error);
|
expect(thrown).toBeInstanceOf(Error);
|
||||||
expect((thrown as { statusCode?: number }).statusCode).toBe(400);
|
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.get).not.toHaveBeenCalled();
|
||||||
expect(wsManager.getMindPath).not.toHaveBeenCalled();
|
expect(count).not.toHaveBeenCalled();
|
||||||
expect(activateWorkspaceMind).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 validId = 'workspace-123_AB';
|
||||||
const wsManager = {
|
const wsManager = {
|
||||||
// Return null so the helper cleanly returns null after the guard passes —
|
// Return null so the helper cleanly returns null after the guard passes —
|
||||||
// proving the guard did not reject a valid segment.
|
// proving the guard did not reject a valid segment.
|
||||||
get: vi.fn(() => null),
|
get: vi.fn(() => null),
|
||||||
getMindPath: vi.fn(() => path.join(tmpDataDir, 'mind.sqlite')),
|
|
||||||
};
|
};
|
||||||
const activateWorkspaceMind = vi.fn(() => true);
|
|
||||||
|
|
||||||
let result: unknown;
|
const result = await buildWorkspaceNowBlock({
|
||||||
expect(() => {
|
dataDir: tmpDataDir,
|
||||||
result = buildWorkspaceNowBlock({
|
workspaceId: validId,
|
||||||
dataDir: tmpDataDir,
|
wsManager,
|
||||||
workspaceId: validId,
|
waggleDb,
|
||||||
wsManager,
|
memory: createInMemoryMemoryStore(),
|
||||||
activateWorkspaceMind,
|
});
|
||||||
});
|
|
||||||
}).not.toThrow();
|
|
||||||
|
|
||||||
// Guard passed, so execution proceeded to the (mocked) wsManager.get.
|
// Guard passed, so execution proceeded to the (mocked) wsManager.get.
|
||||||
expect(wsManager.get).toHaveBeenCalledWith(validId);
|
expect(wsManager.get).toHaveBeenCalledWith(validId);
|
||||||
|
|||||||
@@ -5,7 +5,9 @@
|
|||||||
* - PATCH/PUT accept `status` (archive/restore/pause) and `description`,
|
* - PATCH/PUT accept `status` (archive/restore/pause) and `description`,
|
||||||
* persist via WorkspaceManager, and reject invalid status values with 400.
|
* persist via WorkspaceManager, and reject invalid status values with 400.
|
||||||
* - Rename round-trips.
|
* - 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,
|
* Scaffolding follows home.test.ts: Fastify inject + plain-object decorators,
|
||||||
* but with a REAL WorkspaceManager on a tmp dir so persistence is verified.
|
* 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 os from 'node:os';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import Fastify, { type FastifyInstance } from 'fastify';
|
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 { workspaceRoutes, toStateItemViews } from '../../src/local/routes/workspaces.js';
|
||||||
import type { StateItem } from '../../src/local/workspace-state.js';
|
import type { StateItem } from '../../src/local/workspace-state.js';
|
||||||
|
|
||||||
@@ -25,7 +33,9 @@ describe('workspace lifecycle routes', () => {
|
|||||||
let tmpDir: string;
|
let tmpDir: string;
|
||||||
let manager: WorkspaceManager;
|
let manager: WorkspaceManager;
|
||||||
let server: FastifyInstance;
|
let server: FastifyInstance;
|
||||||
let closeWorkspaceMind: ReturnType<typeof vi.fn>;
|
let releaseWorkspace: ReturnType<typeof vi.fn>;
|
||||||
|
let memory: MemoryStore;
|
||||||
|
let waggleDb: WaggleDB;
|
||||||
let wsId: string;
|
let wsId: string;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
@@ -33,12 +43,16 @@ describe('workspace lifecycle routes', () => {
|
|||||||
manager = new WorkspaceManager(tmpDir);
|
manager = new WorkspaceManager(tmpDir);
|
||||||
wsId = manager.create({ name: 'Side Project', group: 'Personal' }).id;
|
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 = Fastify({ logger: false });
|
||||||
server.decorate('workspaceManager', manager);
|
server.decorate('workspaceManager', manager);
|
||||||
|
server.decorate('memory', memory);
|
||||||
|
server.decorate('waggleDb', waggleDb);
|
||||||
server.decorate('agentState', {
|
server.decorate('agentState', {
|
||||||
activateWorkspaceMind: () => undefined,
|
activateWorkspaceMind: () => undefined,
|
||||||
closeWorkspaceMind,
|
releaseWorkspace,
|
||||||
});
|
});
|
||||||
// localConfig intentionally absent — see file header.
|
// localConfig intentionally absent — see file header.
|
||||||
await server.register(workspaceRoutes);
|
await server.register(workspaceRoutes);
|
||||||
@@ -46,6 +60,7 @@ describe('workspace lifecycle routes', () => {
|
|||||||
|
|
||||||
afterEach(async () => {
|
afterEach(async () => {
|
||||||
await server.close();
|
await server.close();
|
||||||
|
waggleDb.close();
|
||||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -172,14 +187,52 @@ describe('workspace lifecycle routes', () => {
|
|||||||
expect(new Set(ids).size).toBe(ids.length);
|
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}` });
|
const res = await server.inject({ method: 'DELETE', url: `/api/workspaces/${wsId}` });
|
||||||
expect(res.statusCode).toBe(204);
|
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(manager.get(wsId)).toBeNull();
|
||||||
expect(fs.existsSync(path.join(tmpDir, 'workspaces', wsId))).toBe(false);
|
expect(fs.existsSync(path.join(tmpDir, 'workspaces', wsId))).toBe(false);
|
||||||
|
|
||||||
const after = await server.inject({ method: 'DELETE', url: `/api/workspaces/${wsId}` });
|
const after = await server.inject({ method: 'DELETE', url: `/api/workspaces/${wsId}` });
|
||||||
expect(after.statusCode).toBe(404);
|
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 { NotificationGate, materialFingerprint, getNotificationGate } from '../src/local/notification-gate.js';
|
||||||
import { emitNotification } from '../src/local/routes/notifications.js';
|
import { emitNotification } from '../src/local/routes/notifications.js';
|
||||||
import { suggestCapabilities, type ProactiveContext } from '../src/local/proactive-handlers.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';
|
const STORE_FILE = 'notification-fingerprints.json';
|
||||||
|
|
||||||
@@ -156,15 +157,18 @@ describe('emitNotification anti-nag gate', () => {
|
|||||||
expect(emitted).toHaveLength(2);
|
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 wsManager = new WorkspaceManager(tmpDir);
|
||||||
|
const waggleDb = new WaggleDB(':memory:');
|
||||||
const ctx: ProactiveContext = {
|
const ctx: ProactiveContext = {
|
||||||
dataDir: tmpDir,
|
dataDir: tmpDir,
|
||||||
workspaceManager: wsManager,
|
workspaceManager: wsManager,
|
||||||
getWorkspaceMindDb: () => null,
|
memory: createInMemoryMemoryStore(),
|
||||||
|
waggleDb,
|
||||||
};
|
};
|
||||||
// No skills installed → the "capability packs" suggestion, carrying a dedupeKey + hash.
|
// 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).not.toBeNull();
|
||||||
expect(msg!.dedupeKey).toBeDefined();
|
expect(msg!.dedupeKey).toBeDefined();
|
||||||
expect(msg!.materialHash).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 fs from 'node:fs';
|
||||||
import * as path from 'node:path';
|
import * as path from 'node:path';
|
||||||
import * as os from 'node:os';
|
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 {
|
import {
|
||||||
generateMorningBriefing,
|
generateMorningBriefing,
|
||||||
checkStaleWorkspaces,
|
checkStaleWorkspaces,
|
||||||
@@ -15,7 +16,7 @@ describe('Proactive Handlers', () => {
|
|||||||
let tmpDir: string;
|
let tmpDir: string;
|
||||||
let wsManager: WorkspaceManager;
|
let wsManager: WorkspaceManager;
|
||||||
let waggleDb: WaggleDB;
|
let waggleDb: WaggleDB;
|
||||||
const mindDbs = new Map<string, MindDB>();
|
let memory: MemoryStore;
|
||||||
|
|
||||||
/** Awareness items of one workspace, kept in waggle.db under its scope. */
|
/** Awareness items of one workspace, kept in waggle.db under its scope. */
|
||||||
function workspaceAwareness(workspaceId: string): AwarenessLayer {
|
function workspaceAwareness(workspaceId: string): AwarenessLayer {
|
||||||
@@ -27,19 +28,7 @@ describe('Proactive Handlers', () => {
|
|||||||
dataDir: tmpDir,
|
dataDir: tmpDir,
|
||||||
workspaceManager: wsManager,
|
workspaceManager: wsManager,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
getWorkspaceMindDb: (workspaceId: string) => {
|
memory,
|
||||||
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;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,13 +36,10 @@ describe('Proactive Handlers', () => {
|
|||||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-proactive-'));
|
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-proactive-'));
|
||||||
wsManager = new WorkspaceManager(tmpDir);
|
wsManager = new WorkspaceManager(tmpDir);
|
||||||
waggleDb = WaggleDB.openInDataDir(tmpDir);
|
waggleDb = WaggleDB.openInDataDir(tmpDir);
|
||||||
|
memory = createInMemoryMemoryStore();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
for (const [, db] of mindDbs) {
|
|
||||||
try { db.close(); } catch { /* already closed */ }
|
|
||||||
}
|
|
||||||
mindDbs.clear();
|
|
||||||
waggleDb.close();
|
waggleDb.close();
|
||||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||||
});
|
});
|
||||||
@@ -61,13 +47,13 @@ describe('Proactive Handlers', () => {
|
|||||||
// ── Morning Briefing ─────────────────────────────────────────────
|
// ── Morning Briefing ─────────────────────────────────────────────
|
||||||
|
|
||||||
describe('generateMorningBriefing', () => {
|
describe('generateMorningBriefing', () => {
|
||||||
it('returns null when no workspaces exist', () => {
|
it('returns null when no workspaces exist', async () => {
|
||||||
const ctx = makeContext();
|
const ctx = makeContext();
|
||||||
const result = generateMorningBriefing(ctx);
|
const result = await generateMorningBriefing(ctx);
|
||||||
expect(result).toBeNull();
|
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
|
// Create a workspace with a recent session file
|
||||||
const ws = wsManager.create({ name: 'Active Project', group: 'Work' });
|
const ws = wsManager.create({ name: 'Active Project', group: 'Work' });
|
||||||
const sessionsDir = path.join(tmpDir, 'workspaces', ws.id, 'sessions');
|
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');
|
fs.writeFileSync(path.join(sessionsDir, '2026-03-19.jsonl'), '{"role":"user","content":"hello"}\n');
|
||||||
|
|
||||||
const ctx = makeContext();
|
const ctx = makeContext();
|
||||||
const result = generateMorningBriefing(ctx);
|
const result = await generateMorningBriefing(ctx);
|
||||||
expect(result).toBeNull();
|
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 ws = wsManager.create({ name: 'My Research', group: 'Work' });
|
||||||
|
|
||||||
const awareness = workspaceAwareness(ws.id);
|
const awareness = workspaceAwareness(ws.id);
|
||||||
@@ -87,7 +73,7 @@ describe('Proactive Handlers', () => {
|
|||||||
awareness.add('pending', 'Waiting for feedback', 0);
|
awareness.add('pending', 'Waiting for feedback', 0);
|
||||||
|
|
||||||
const ctx = makeContext();
|
const ctx = makeContext();
|
||||||
const result = generateMorningBriefing(ctx);
|
const result = await generateMorningBriefing(ctx);
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
expect(result).not.toBeNull();
|
||||||
expect(result!.type).toBe('morning_briefing');
|
expect(result!.type).toBe('morning_briefing');
|
||||||
@@ -96,7 +82,7 @@ describe('Proactive Handlers', () => {
|
|||||||
expect(result!.body).toContain('My Research');
|
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' });
|
const ws = wsManager.create({ name: 'Old Project', group: 'Archive' });
|
||||||
|
|
||||||
// Create a session file with old modification time
|
// Create a session file with old modification time
|
||||||
@@ -109,23 +95,36 @@ describe('Proactive Handlers', () => {
|
|||||||
fs.utimesSync(sessionFile, oldTime, oldTime);
|
fs.utimesSync(sessionFile, oldTime, oldTime);
|
||||||
|
|
||||||
const ctx = makeContext();
|
const ctx = makeContext();
|
||||||
const result = generateMorningBriefing(ctx);
|
const result = await generateMorningBriefing(ctx);
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
expect(result).not.toBeNull();
|
||||||
expect(result!.body).toContain('not visited in 14+ days');
|
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 ────────────────────────────────────────
|
// ── Stale Workspace Check ────────────────────────────────────────
|
||||||
|
|
||||||
describe('checkStaleWorkspaces', () => {
|
describe('checkStaleWorkspaces', () => {
|
||||||
it('returns empty array when no workspaces', () => {
|
it('returns empty array when no workspaces', async () => {
|
||||||
const ctx = makeContext();
|
const ctx = makeContext();
|
||||||
const result = checkStaleWorkspaces(ctx);
|
const result = await checkStaleWorkspaces(ctx);
|
||||||
expect(result).toEqual([]);
|
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' });
|
const ws = wsManager.create({ name: 'Forgotten Project', group: 'Old' });
|
||||||
|
|
||||||
// Create a session file with old modification time
|
// Create a session file with old modification time
|
||||||
@@ -137,7 +136,7 @@ describe('Proactive Handlers', () => {
|
|||||||
fs.utimesSync(sessionFile, oldTime, oldTime);
|
fs.utimesSync(sessionFile, oldTime, oldTime);
|
||||||
|
|
||||||
const ctx = makeContext();
|
const ctx = makeContext();
|
||||||
const result = checkStaleWorkspaces(ctx);
|
const result = await checkStaleWorkspaces(ctx);
|
||||||
|
|
||||||
expect(result.length).toBe(1);
|
expect(result.length).toBe(1);
|
||||||
expect(result[0].type).toBe('stale_workspace');
|
expect(result[0].type).toBe('stale_workspace');
|
||||||
@@ -145,18 +144,18 @@ describe('Proactive Handlers', () => {
|
|||||||
expect(result[0].workspaceId).toBe(ws.id);
|
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 ws = wsManager.create({ name: 'Active Project', group: 'Work' });
|
||||||
const sessionsDir = path.join(tmpDir, 'workspaces', ws.id, 'sessions');
|
const sessionsDir = path.join(tmpDir, 'workspaces', ws.id, 'sessions');
|
||||||
fs.mkdirSync(sessionsDir, { recursive: true });
|
fs.mkdirSync(sessionsDir, { recursive: true });
|
||||||
fs.writeFileSync(path.join(sessionsDir, '2026-03-19.jsonl'), '{"role":"user","content":"hello"}\n');
|
fs.writeFileSync(path.join(sessionsDir, '2026-03-19.jsonl'), '{"role":"user","content":"hello"}\n');
|
||||||
|
|
||||||
const ctx = makeContext();
|
const ctx = makeContext();
|
||||||
const result = checkStaleWorkspaces(ctx);
|
const result = await checkStaleWorkspaces(ctx);
|
||||||
expect(result).toEqual([]);
|
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
|
// Create workspace with backdated creation
|
||||||
const ws = wsManager.create({ name: 'Empty Old Workspace', group: 'Test' });
|
const ws = wsManager.create({ name: 'Empty Old Workspace', group: 'Test' });
|
||||||
// Backdate the workspace config
|
// Backdate the workspace config
|
||||||
@@ -166,11 +165,25 @@ describe('Proactive Handlers', () => {
|
|||||||
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
||||||
|
|
||||||
const ctx = makeContext();
|
const ctx = makeContext();
|
||||||
const result = checkStaleWorkspaces(ctx);
|
const result = await checkStaleWorkspaces(ctx);
|
||||||
|
|
||||||
expect(result.length).toBe(1);
|
expect(result.length).toBe(1);
|
||||||
expect(result[0].title).toContain('Empty Old Workspace');
|
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 ────────────────────────────────────────────────
|
// ── Task Reminder ────────────────────────────────────────────────
|
||||||
@@ -233,17 +246,17 @@ describe('Proactive Handlers', () => {
|
|||||||
// ── Capability Suggestion ────────────────────────────────────────
|
// ── Capability Suggestion ────────────────────────────────────────
|
||||||
|
|
||||||
describe('suggestCapabilities', () => {
|
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
|
// Create a fake skills directory to simulate installed capabilities
|
||||||
fs.mkdirSync(path.join(tmpDir, 'skills', 'some-skill'), { recursive: true });
|
fs.mkdirSync(path.join(tmpDir, 'skills', 'some-skill'), { recursive: true });
|
||||||
const ctx = makeContext();
|
const ctx = makeContext();
|
||||||
const result = suggestCapabilities(ctx);
|
const result = await suggestCapabilities(ctx);
|
||||||
expect(result).toBeNull();
|
expect(result).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('suggests capability packs when none are installed', () => {
|
it('suggests capability packs when none are installed', async () => {
|
||||||
const ctx = makeContext();
|
const ctx = makeContext();
|
||||||
const result = suggestCapabilities(ctx);
|
const result = await suggestCapabilities(ctx);
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
expect(result).not.toBeNull();
|
||||||
expect(result!.type).toBe('capability_suggestion');
|
expect(result!.type).toBe('capability_suggestion');
|
||||||
@@ -251,17 +264,15 @@ describe('Proactive Handlers', () => {
|
|||||||
expect(result!.actionUrl).toBe('/skills');
|
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)
|
// Create skills dir (simulates installed capabilities)
|
||||||
fs.mkdirSync(path.join(tmpDir, 'skills', 'research-skill'), { recursive: true });
|
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 ws = wsManager.create({ name: 'Small Project', group: 'Test' });
|
||||||
const mindPath = path.join(tmpDir, 'workspaces', ws.id, 'workspace.mind');
|
await memory.remember({ scope: workspaceScope(ws.id), text: 'One small memory' });
|
||||||
const db = new MindDB(mindPath);
|
|
||||||
mindDbs.set(ws.id, db);
|
|
||||||
|
|
||||||
const ctx = makeContext();
|
const ctx = makeContext();
|
||||||
const result = suggestCapabilities(ctx);
|
const result = await suggestCapabilities(ctx);
|
||||||
expect(result).toBeNull();
|
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 fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import os from 'node:os';
|
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 {
|
import {
|
||||||
buildWorkspaceNowBlock,
|
buildWorkspaceNowBlock,
|
||||||
formatWorkspaceNowPrompt,
|
formatWorkspaceNowPrompt,
|
||||||
@@ -17,18 +23,23 @@ import {
|
|||||||
*
|
*
|
||||||
* The chat route code:
|
* The chat route code:
|
||||||
* if (workspaceId) {
|
* if (workspaceId) {
|
||||||
* const nowBlock = buildWorkspaceNowBlock({ ... });
|
* const nowBlock = await buildWorkspaceNowBlock({ ... });
|
||||||
* if (nowBlock) prompt += '\n\n' + formatWorkspaceNowPrompt(nowBlock);
|
* if (nowBlock) prompt += '\n\n' + formatWorkspaceNowPrompt(nowBlock);
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
describe('Context Injection — system prompt integration', () => {
|
describe('Context Injection — system prompt integration', () => {
|
||||||
let tmpDir: string;
|
let tmpDir: string;
|
||||||
|
let memory: MemoryStore;
|
||||||
|
let waggleDb: WaggleDB;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ctx-inject-'));
|
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ctx-inject-'));
|
||||||
|
memory = createInMemoryMemoryStore();
|
||||||
|
waggleDb = WaggleDB.openInDataDir(tmpDir);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
waggleDb.close();
|
||||||
try {
|
try {
|
||||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||||
} catch {
|
} catch {
|
||||||
@@ -38,8 +49,8 @@ describe('Context Injection — system prompt integration', () => {
|
|||||||
|
|
||||||
// ── Helpers ───────────────────────────────────────────────────────────
|
// ── Helpers ───────────────────────────────────────────────────────────
|
||||||
|
|
||||||
function setupWorkspace(id: string, opts?: {
|
async function setupWorkspace(id: string, opts?: {
|
||||||
frames?: Array<{ content: string; importance: string }>;
|
memories?: Array<{ text: string; importance: MemoryImportance }>;
|
||||||
sessions?: Array<{ title: string; messages: Array<{ role: string; content: string }> }>;
|
sessions?: Array<{ title: string; messages: Array<{ role: string; content: string }> }>;
|
||||||
}) {
|
}) {
|
||||||
const wsDir = path.join(tmpDir, 'workspaces', id);
|
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() }),
|
JSON.stringify({ id, name: `Test ${id}`, group: 'test', created: new Date().toISOString() }),
|
||||||
);
|
);
|
||||||
|
|
||||||
const mindPath = path.join(wsDir, 'workspace.mind');
|
for (const memo of opts?.memories ?? []) {
|
||||||
if (opts?.frames && opts.frames.length > 0) {
|
await memory.remember({ scope: workspaceScope(id), text: memo.text, importance: memo.importance });
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts?.sessions) {
|
if (opts?.sessions) {
|
||||||
@@ -86,26 +85,24 @@ describe('Context Injection — system prompt integration', () => {
|
|||||||
function makeManager(workspaces: Map<string, { id: string; name: string }>) {
|
function makeManager(workspaces: Map<string, { id: string; name: string }>) {
|
||||||
return {
|
return {
|
||||||
get: (id: string) => workspaces.get(id) ?? null,
|
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
|
* Simulate what buildSystemPrompt does: build a base prompt, then conditionally
|
||||||
* append the workspace context. This mirrors the injection logic in chat.ts.
|
* 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;
|
let prompt = basePrompt;
|
||||||
|
|
||||||
if (workspaceId && wsManager) {
|
if (workspaceId && wsManager) {
|
||||||
try {
|
try {
|
||||||
const nowBlock = buildWorkspaceNowBlock({
|
const nowBlock = await buildWorkspaceNowBlock({
|
||||||
dataDir: tmpDir,
|
dataDir: tmpDir,
|
||||||
workspaceId,
|
workspaceId,
|
||||||
wsManager,
|
wsManager,
|
||||||
activateWorkspaceMind: noopActivate,
|
waggleDb,
|
||||||
|
memory,
|
||||||
});
|
});
|
||||||
if (nowBlock) {
|
if (nowBlock) {
|
||||||
prompt += '\n\n' + formatWorkspaceNowPrompt(nowBlock);
|
prompt += '\n\n' + formatWorkspaceNowPrompt(nowBlock);
|
||||||
@@ -120,9 +117,9 @@ describe('Context Injection — system prompt integration', () => {
|
|||||||
|
|
||||||
// ── Test 1: No workspaceId → no "Workspace Now" section ─────────────
|
// ── 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 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).not.toContain('# Workspace Now');
|
||||||
expect(result).toBe(basePrompt);
|
expect(result).toBe(basePrompt);
|
||||||
@@ -130,15 +127,15 @@ describe('Context Injection — system prompt integration', () => {
|
|||||||
|
|
||||||
// ── Test 2: With workspaceId + data → includes "Workspace Now" ──────
|
// ── 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([
|
const wsManager = makeManager(new Map([
|
||||||
['rich-ws', { id: 'rich-ws', name: 'Rich Project' }],
|
['rich-ws', { id: 'rich-ws', name: 'Rich Project' }],
|
||||||
]));
|
]));
|
||||||
|
|
||||||
setupWorkspace('rich-ws', {
|
await setupWorkspace('rich-ws', {
|
||||||
frames: [
|
memories: [
|
||||||
{ content: 'Working on the Waggle platform, a workspace-native AI agent.', importance: 'important' },
|
{ text: 'Working on the Waggle platform, a workspace-native AI agent.', importance: 'important' },
|
||||||
{ content: 'Decision: Use SQLite for local persistence.', importance: 'critical' },
|
{ text: 'Decision: Use SQLite for local persistence.', importance: 'critical' },
|
||||||
],
|
],
|
||||||
sessions: [
|
sessions: [
|
||||||
{
|
{
|
||||||
@@ -152,7 +149,7 @@ describe('Context Injection — system prompt integration', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const basePrompt = '# Who You Are\n\nYou are Waggle.';
|
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('# Workspace Now');
|
||||||
expect(result).toContain('Rich Project');
|
expect(result).toContain('Rich Project');
|
||||||
@@ -166,12 +163,12 @@ describe('Context Injection — system prompt integration', () => {
|
|||||||
|
|
||||||
// ── Test 3: With workspaceId for empty/missing workspace → graceful ─
|
// ── 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([
|
const wsManager = makeManager(new Map([
|
||||||
['empty-ws', { id: 'empty-ws', name: 'Empty Project' }],
|
['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');
|
const wsDir = path.join(tmpDir, 'workspaces', 'empty-ws');
|
||||||
fs.mkdirSync(wsDir, { recursive: true });
|
fs.mkdirSync(wsDir, { recursive: true });
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
@@ -180,17 +177,17 @@ describe('Context Injection — system prompt integration', () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const basePrompt = '# Who You Are\n\nYou are Waggle.';
|
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).not.toContain('# Workspace Now');
|
||||||
expect(result).toBe(basePrompt);
|
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 wsManager = makeManager(new Map());
|
||||||
|
|
||||||
const basePrompt = '# Who You Are\n\nYou are Waggle.';
|
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).not.toContain('# Workspace Now');
|
||||||
expect(result).toBe(basePrompt);
|
expect(result).toBe(basePrompt);
|
||||||
@@ -198,29 +195,29 @@ describe('Context Injection — system prompt integration', () => {
|
|||||||
|
|
||||||
// ── Test 4: Cache invalidation — different workspaceId → different prompt ─
|
// ── 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([
|
const wsManager = makeManager(new Map([
|
||||||
['ws-a', { id: 'ws-a', name: 'Project Alpha' }],
|
['ws-a', { id: 'ws-a', name: 'Project Alpha' }],
|
||||||
['ws-b', { id: 'ws-b', name: 'Project Beta' }],
|
['ws-b', { id: 'ws-b', name: 'Project Beta' }],
|
||||||
]));
|
]));
|
||||||
|
|
||||||
setupWorkspace('ws-a', {
|
await setupWorkspace('ws-a', {
|
||||||
frames: [
|
memories: [
|
||||||
{ content: 'Alpha is a front-end framework project.', importance: 'important' },
|
{ text: 'Alpha is a front-end framework project.', importance: 'important' },
|
||||||
{ content: 'Decision: Use React for Alpha.', importance: 'critical' },
|
{ text: 'Decision: Use React for Alpha.', importance: 'critical' },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
setupWorkspace('ws-b', {
|
await setupWorkspace('ws-b', {
|
||||||
frames: [
|
memories: [
|
||||||
{ content: 'Beta is a backend API project.', importance: 'important' },
|
{ text: 'Beta is a backend API project.', importance: 'important' },
|
||||||
{ content: 'Decision: Use Rust for Beta.', importance: 'critical' },
|
{ text: 'Decision: Use Rust for Beta.', importance: 'critical' },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
const basePrompt = '# Who You Are\n\nYou are Waggle.';
|
const basePrompt = '# Who You Are\n\nYou are Waggle.';
|
||||||
const promptA = simulatePromptBuild(basePrompt, 'ws-a', wsManager);
|
const promptA = await simulatePromptBuild(basePrompt, 'ws-a', wsManager);
|
||||||
const promptB = simulatePromptBuild(basePrompt, 'ws-b', wsManager);
|
const promptB = await simulatePromptBuild(basePrompt, 'ws-b', wsManager);
|
||||||
|
|
||||||
// Both should have Workspace Now
|
// Both should have Workspace Now
|
||||||
expect(promptA).toContain('# 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 ──────
|
// ── 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([
|
const wsManager = makeManager(new Map([
|
||||||
['pos-ws', { id: 'pos-ws', name: 'Position Test' }],
|
['pos-ws', { id: 'pos-ws', name: 'Position Test' }],
|
||||||
]));
|
]));
|
||||||
|
|
||||||
setupWorkspace('pos-ws', {
|
await setupWorkspace('pos-ws', {
|
||||||
frames: [
|
memories: [
|
||||||
{ content: 'Testing prompt positioning.', importance: 'important' },
|
{ text: 'Testing prompt positioning.', importance: 'important' },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
const basePrompt = 'BASE_PROMPT_START\nSome agent instructions.\nBASE_PROMPT_END';
|
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
|
// Base prompt should appear first
|
||||||
expect(result.startsWith('BASE_PROMPT_START')).toBe(true);
|
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 fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import os from 'node:os';
|
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';
|
import { buildWorkspaceNowBlock, formatWorkspaceNowPrompt, type WorkspaceNowBlock } from '../../src/local/routes/workspace-context.js';
|
||||||
|
|
||||||
describe('workspace-context', () => {
|
describe('workspace-context', () => {
|
||||||
let tmpDir: string;
|
let tmpDir: string;
|
||||||
let waggleDb: WaggleDB;
|
let waggleDb: WaggleDB;
|
||||||
|
let memory: MemoryStore;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ctx-test-'));
|
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ctx-test-'));
|
||||||
waggleDb = WaggleDB.openInDataDir(tmpDir);
|
waggleDb = WaggleDB.openInDataDir(tmpDir);
|
||||||
|
memory = createInMemoryMemoryStore();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -24,9 +32,9 @@ describe('workspace-context', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Helper: create a workspace directory structure with a mind DB
|
// Helper: create a workspace directory structure with memories
|
||||||
function setupWorkspace(id: string, opts?: {
|
async function setupWorkspace(id: string, opts?: {
|
||||||
frames?: Array<{ content: string; importance: string }>;
|
memories?: Array<{ text: string; importance: MemoryImportance }>;
|
||||||
sessions?: Array<{ title: string; messages: Array<{ role: string; content: string }> }>;
|
sessions?: Array<{ title: string; messages: Array<{ role: string; content: string }> }>;
|
||||||
}) {
|
}) {
|
||||||
const wsDir = path.join(tmpDir, 'workspaces', id);
|
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() }),
|
JSON.stringify({ id, name: `Test Workspace ${id}`, group: 'test', created: new Date().toISOString() }),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Create mind DB with frames
|
for (const memo of opts?.memories ?? []) {
|
||||||
const mindPath = path.join(wsDir, 'workspace.mind');
|
await memory.remember({ scope: workspaceScope(id), text: memo.text, importance: memo.importance });
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create session files
|
// Create session files
|
||||||
@@ -76,29 +68,26 @@ describe('workspace-context', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { mindPath, wsDir };
|
return { wsDir };
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeManager(workspaces: Map<string, { id: string; name: string }>) {
|
function makeManager(workspaces: Map<string, { id: string; name: string }>) {
|
||||||
return {
|
return {
|
||||||
get: (id: string) => workspaces.get(id) ?? null,
|
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 ──────────────
|
// ── 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 wsManager = makeManager(new Map());
|
||||||
|
|
||||||
const result = buildWorkspaceNowBlock({
|
const result = await buildWorkspaceNowBlock({
|
||||||
dataDir: tmpDir,
|
dataDir: tmpDir,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
workspaceId: 'does-not-exist',
|
workspaceId: 'does-not-exist',
|
||||||
wsManager,
|
wsManager,
|
||||||
activateWorkspaceMind: noopActivate,
|
memory,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).toBeNull();
|
expect(result).toBeNull();
|
||||||
@@ -106,43 +95,20 @@ describe('workspace-context', () => {
|
|||||||
|
|
||||||
// ── Test 2: Returns null for workspace with no data ──────────────
|
// ── Test 2: Returns null for workspace with no data ──────────────
|
||||||
|
|
||||||
it('returns null for workspace with no mind file', () => {
|
it('returns null for workspace without memories', async () => {
|
||||||
const wsDir = path.join(tmpDir, 'workspaces', 'empty-ws');
|
|
||||||
fs.mkdirSync(wsDir, { recursive: true });
|
|
||||||
|
|
||||||
const wsManager = makeManager(new Map([
|
const wsManager = makeManager(new Map([
|
||||||
['empty-ws', { id: 'empty-ws', name: 'Empty Workspace' }],
|
['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,
|
dataDir: tmpDir,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
workspaceId: 'empty-ws',
|
workspaceId: 'empty-ws',
|
||||||
wsManager,
|
wsManager,
|
||||||
activateWorkspaceMind: noopActivate,
|
memory,
|
||||||
});
|
|
||||||
|
|
||||||
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,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).toBeNull();
|
expect(result).toBeNull();
|
||||||
@@ -150,17 +116,16 @@ describe('workspace-context', () => {
|
|||||||
|
|
||||||
// ── Test 3: Returns correctly shaped block ───────────────────────
|
// ── 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([
|
const wsManager = makeManager(new Map([
|
||||||
['rich-ws', { id: 'rich-ws', name: 'Rich Workspace' }],
|
['rich-ws', { id: 'rich-ws', name: 'Rich Workspace' }],
|
||||||
]));
|
]));
|
||||||
const activateWorkspaceMind = vi.fn(() => true);
|
|
||||||
|
|
||||||
setupWorkspace('rich-ws', {
|
await setupWorkspace('rich-ws', {
|
||||||
frames: [
|
memories: [
|
||||||
{ content: 'Working on the Waggle project, a workspace-native AI agent platform.', importance: 'important' },
|
{ text: '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' },
|
{ text: 'Decision: Use SQLite for local storage instead of PostgreSQL.', importance: 'critical' },
|
||||||
{ content: 'Implemented the memory frame system with FTS5 search.', importance: 'normal' },
|
{ text: 'Implemented the memory frame system with FTS5 search.', importance: 'normal' },
|
||||||
],
|
],
|
||||||
sessions: [
|
sessions: [
|
||||||
{
|
{
|
||||||
@@ -173,16 +138,15 @@ describe('workspace-context', () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = buildWorkspaceNowBlock({
|
const result = await buildWorkspaceNowBlock({
|
||||||
dataDir: tmpDir,
|
dataDir: tmpDir,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
workspaceId: 'rich-ws',
|
workspaceId: 'rich-ws',
|
||||||
wsManager,
|
wsManager,
|
||||||
activateWorkspaceMind,
|
memory,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
expect(result).not.toBeNull();
|
||||||
expect(activateWorkspaceMind).not.toHaveBeenCalled();
|
|
||||||
expect(result!.workspaceName).toBe('Rich Workspace');
|
expect(result!.workspaceName).toBe('Rich Workspace');
|
||||||
expect(result!.summary).toBeTruthy();
|
expect(result!.summary).toBeTruthy();
|
||||||
expect(result!.summary.length).toBeGreaterThan(10);
|
expect(result!.summary.length).toBeGreaterThan(10);
|
||||||
@@ -198,15 +162,15 @@ describe('workspace-context', () => {
|
|||||||
|
|
||||||
// ── Test 4: Caps at correct limits ───────────────────────────────
|
// ── 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([
|
const wsManager = makeManager(new Map([
|
||||||
['capped-ws', { id: 'capped-ws', name: 'Capped Workspace' }],
|
['capped-ws', { id: 'capped-ws', name: 'Capped Workspace' }],
|
||||||
]));
|
]));
|
||||||
|
|
||||||
// Create many decisions and sessions
|
// Create many decisions and sessions
|
||||||
const frames = [];
|
const memories: Array<{ text: string; importance: MemoryImportance }> = [];
|
||||||
for (let i = 0; i < 10; i++) {
|
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 = [];
|
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,
|
dataDir: tmpDir,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
workspaceId: 'capped-ws',
|
workspaceId: 'capped-ws',
|
||||||
wsManager,
|
wsManager,
|
||||||
activateWorkspaceMind: noopActivate,
|
memory,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
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 fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import os from 'node:os';
|
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 {
|
import {
|
||||||
buildWorkspaceState,
|
buildWorkspaceState,
|
||||||
formatWorkspaceStatePrompt,
|
formatWorkspaceStatePrompt,
|
||||||
computeFreshness,
|
computeFreshness,
|
||||||
|
listByImportance,
|
||||||
type WorkspaceState,
|
type WorkspaceState,
|
||||||
} from '../../src/local/workspace-state.js';
|
} from '../../src/local/workspace-state.js';
|
||||||
|
|
||||||
describe('workspace-state', () => {
|
describe('workspace-state', () => {
|
||||||
let tmpDir: string;
|
let tmpDir: string;
|
||||||
let waggleDb: WaggleDB;
|
let waggleDb: WaggleDB;
|
||||||
|
let memory: MemoryStore;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ws-state-'));
|
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ws-state-'));
|
||||||
waggleDb = WaggleDB.openInDataDir(tmpDir);
|
waggleDb = WaggleDB.openInDataDir(tmpDir);
|
||||||
|
memory = createInMemoryMemoryStore();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -24,8 +34,8 @@ describe('workspace-state', () => {
|
|||||||
try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* */ }
|
try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* */ }
|
||||||
});
|
});
|
||||||
|
|
||||||
function setupWorkspace(id: string, opts?: {
|
async function setupWorkspace(id: string, opts?: {
|
||||||
frames?: Array<{ content: string; importance: string; created_at?: string }>;
|
memories?: Array<{ text: string; importance: MemoryImportance; kind?: MemoryKind }>;
|
||||||
sessions?: Array<{
|
sessions?: Array<{
|
||||||
title: string;
|
title: string;
|
||||||
messages: Array<{ role: string; content: string }>;
|
messages: Array<{ role: string; content: string }>;
|
||||||
@@ -36,24 +46,13 @@ describe('workspace-state', () => {
|
|||||||
const wsDir = path.join(tmpDir, 'workspaces', id);
|
const wsDir = path.join(tmpDir, 'workspaces', id);
|
||||||
fs.mkdirSync(wsDir, { recursive: true });
|
fs.mkdirSync(wsDir, { recursive: true });
|
||||||
|
|
||||||
const mindPath = path.join(wsDir, 'workspace.mind');
|
for (const memo of opts?.memories ?? []) {
|
||||||
if (opts?.frames && opts.frames.length > 0) {
|
await memory.remember({
|
||||||
const db = new MindDB(mindPath);
|
scope: workspaceScope(id),
|
||||||
const raw = db.getDatabase();
|
text: memo.text,
|
||||||
raw.prepare(
|
importance: memo.importance,
|
||||||
`INSERT INTO sessions (gop_id, status, started_at)
|
...(memo.kind ? { kind: memo.kind } : {}),
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Awareness items live in waggle.db under the workspace's scope, not in the mind.
|
// 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 }>) {
|
function makeManager(workspaces: Map<string, { id: string; name: string }>) {
|
||||||
return {
|
return {
|
||||||
get: (id: string) => workspaces.get(id) ?? null,
|
get: (id: string) => workspaces.get(id) ?? null,
|
||||||
getMindPath: (id: string) => path.join(tmpDir, 'workspaces', id, 'workspace.mind'),
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const noopActivate = (_id: string) => true;
|
|
||||||
|
|
||||||
// ── computeFreshness ──────────────────────────────────────────
|
// ── computeFreshness ──────────────────────────────────────────
|
||||||
|
|
||||||
describe('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 ───────────────────────────────────────
|
// ── buildWorkspaceState ───────────────────────────────────────
|
||||||
|
|
||||||
describe('buildWorkspaceState', () => {
|
describe('buildWorkspaceState', () => {
|
||||||
it('returns null for non-existent workspace', () => {
|
it('returns null for non-existent workspace', async () => {
|
||||||
const result = buildWorkspaceState({
|
const result = await buildWorkspaceState({
|
||||||
dataDir: tmpDir,
|
dataDir: tmpDir,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
workspaceId: 'nonexistent',
|
workspaceId: 'nonexistent',
|
||||||
wsManager: makeManager(new Map()),
|
wsManager: makeManager(new Map()),
|
||||||
activateWorkspaceMind: noopActivate,
|
memory,
|
||||||
});
|
});
|
||||||
expect(result).toBeNull();
|
expect(result).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns null for workspace with no mind file', () => {
|
it('returns null for workspace with no memories', async () => {
|
||||||
const wsDir = path.join(tmpDir, 'workspaces', 'empty');
|
await setupWorkspace('empty');
|
||||||
fs.mkdirSync(wsDir, { recursive: true });
|
// 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,
|
dataDir: tmpDir,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
workspaceId: 'empty',
|
workspaceId: 'empty',
|
||||||
wsManager: makeManager(new Map([['empty', { id: 'empty', name: 'Empty' }]])),
|
wsManager: makeManager(new Map([['empty', { id: 'empty', name: 'Empty' }]])),
|
||||||
activateWorkspaceMind: noopActivate,
|
memory,
|
||||||
});
|
});
|
||||||
expect(result).toBeNull();
|
expect(result).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns null for workspace with zero memory frames', () => {
|
it('returns state with decisions from workspace memory', async () => {
|
||||||
fs.mkdirSync(path.join(tmpDir, 'workspaces', 'noframes'), { recursive: true });
|
await setupWorkspace('ws1', {
|
||||||
const db = new MindDB(path.join(tmpDir, 'workspaces', 'noframes', 'workspace.mind'));
|
memories: [
|
||||||
db.close();
|
{ text: 'Decision: Use SQLite for local storage', importance: 'critical' },
|
||||||
|
{ text: 'Working on the authentication module', importance: 'normal' },
|
||||||
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' },
|
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = buildWorkspaceState({
|
const result = await buildWorkspaceState({
|
||||||
dataDir: tmpDir,
|
dataDir: tmpDir,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
workspaceId: 'ws1',
|
workspaceId: 'ws1',
|
||||||
wsManager: makeManager(new Map([['ws1', { id: 'ws1', name: 'Project Alpha' }]])),
|
wsManager: makeManager(new Map([['ws1', { id: 'ws1', name: 'Project Alpha' }]])),
|
||||||
activateWorkspaceMind,
|
memory,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
expect(result).not.toBeNull();
|
||||||
expect(activateWorkspaceMind).not.toHaveBeenCalled();
|
|
||||||
expect(result!.recentDecisions.length).toBeGreaterThanOrEqual(1);
|
expect(result!.recentDecisions.length).toBeGreaterThanOrEqual(1);
|
||||||
expect(result!.recentDecisions[0].content).toContain('SQLite');
|
expect(result!.recentDecisions[0].content).toContain('SQLite');
|
||||||
expect(result!.recentDecisions[0].source).toBe('memory');
|
expect(result!.recentDecisions[0].source).toBe('memory');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('extracts pending tasks from sessions', () => {
|
it('takes decisions from their kind or wording, never from temporary memories', async () => {
|
||||||
setupWorkspace('ws2', {
|
await setupWorkspace('ws8', {
|
||||||
frames: [{ content: 'Project context established', importance: 'normal' }],
|
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: [{
|
sessions: [{
|
||||||
title: 'Planning',
|
title: 'Planning',
|
||||||
messages: [
|
messages: [
|
||||||
@@ -202,12 +226,12 @@ describe('workspace-state', () => {
|
|||||||
}],
|
}],
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = buildWorkspaceState({
|
const result = await buildWorkspaceState({
|
||||||
dataDir: tmpDir,
|
dataDir: tmpDir,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
workspaceId: 'ws2',
|
workspaceId: 'ws2',
|
||||||
wsManager: makeManager(new Map([['ws2', { id: 'ws2', name: 'API Project' }]])),
|
wsManager: makeManager(new Map([['ws2', { id: 'ws2', name: 'API Project' }]])),
|
||||||
activateWorkspaceMind: noopActivate,
|
memory,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
expect(result).not.toBeNull();
|
||||||
@@ -216,9 +240,9 @@ describe('workspace-state', () => {
|
|||||||
expect(result!.pending.length).toBeGreaterThanOrEqual(1);
|
expect(result!.pending.length).toBeGreaterThanOrEqual(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('extracts open questions from sessions', () => {
|
it('extracts open questions from sessions', async () => {
|
||||||
setupWorkspace('ws3', {
|
await setupWorkspace('ws3', {
|
||||||
frames: [{ content: 'Working on infrastructure', importance: 'normal' }],
|
memories: [{ text: 'Working on infrastructure', importance: 'normal' }],
|
||||||
sessions: [{
|
sessions: [{
|
||||||
title: 'Infra discussion',
|
title: 'Infra discussion',
|
||||||
messages: [
|
messages: [
|
||||||
@@ -229,22 +253,22 @@ describe('workspace-state', () => {
|
|||||||
}],
|
}],
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = buildWorkspaceState({
|
const result = await buildWorkspaceState({
|
||||||
dataDir: tmpDir,
|
dataDir: tmpDir,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
workspaceId: 'ws3',
|
workspaceId: 'ws3',
|
||||||
wsManager: makeManager(new Map([['ws3', { id: 'ws3', name: 'Infra' }]])),
|
wsManager: makeManager(new Map([['ws3', { id: 'ws3', name: 'Infra' }]])),
|
||||||
activateWorkspaceMind: noopActivate,
|
memory,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
expect(result).not.toBeNull();
|
||||||
expect(result!.openQuestions.length).toBeGreaterThanOrEqual(1);
|
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;
|
const tenDays = 10 * 24 * 60 * 60 * 1000;
|
||||||
setupWorkspace('ws4', {
|
await setupWorkspace('ws4', {
|
||||||
frames: [{ content: 'Multi-thread workspace', importance: 'normal' }],
|
memories: [{ text: 'Multi-thread workspace', importance: 'normal' }],
|
||||||
sessions: [
|
sessions: [
|
||||||
{
|
{
|
||||||
title: 'Fresh thread',
|
title: 'Fresh thread',
|
||||||
@@ -264,12 +288,12 @@ describe('workspace-state', () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = buildWorkspaceState({
|
const result = await buildWorkspaceState({
|
||||||
dataDir: tmpDir,
|
dataDir: tmpDir,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
workspaceId: 'ws4',
|
workspaceId: 'ws4',
|
||||||
wsManager: makeManager(new Map([['ws4', { id: 'ws4', name: 'Multi' }]])),
|
wsManager: makeManager(new Map([['ws4', { id: 'ws4', name: 'Multi' }]])),
|
||||||
activateWorkspaceMind: noopActivate,
|
memory,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
expect(result).not.toBeNull();
|
||||||
@@ -279,9 +303,9 @@ describe('workspace-state', () => {
|
|||||||
expect(result!.stale.some(s => s.content === 'Old thread')).toBe(true);
|
expect(result!.stale.some(s => s.content === 'Old thread')).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('derives nextActions from blockers and pending items', () => {
|
it('derives nextActions from blockers and pending items', async () => {
|
||||||
setupWorkspace('ws5', {
|
await setupWorkspace('ws5', {
|
||||||
frames: [{ content: 'Active project', importance: 'normal' }],
|
memories: [{ text: 'Active project', importance: 'normal' }],
|
||||||
sessions: [{
|
sessions: [{
|
||||||
title: 'Work session',
|
title: 'Work session',
|
||||||
messages: [
|
messages: [
|
||||||
@@ -292,12 +316,12 @@ describe('workspace-state', () => {
|
|||||||
}],
|
}],
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = buildWorkspaceState({
|
const result = await buildWorkspaceState({
|
||||||
dataDir: tmpDir,
|
dataDir: tmpDir,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
workspaceId: 'ws5',
|
workspaceId: 'ws5',
|
||||||
wsManager: makeManager(new Map([['ws5', { id: 'ws5', name: 'Blocked' }]])),
|
wsManager: makeManager(new Map([['ws5', { id: 'ws5', name: 'Blocked' }]])),
|
||||||
activateWorkspaceMind: noopActivate,
|
memory,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
expect(result).not.toBeNull();
|
||||||
@@ -308,11 +332,11 @@ describe('workspace-state', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('returns all expected fields', () => {
|
it('returns all expected fields', async () => {
|
||||||
setupWorkspace('ws6', {
|
await setupWorkspace('ws6', {
|
||||||
frames: [
|
memories: [
|
||||||
{ content: 'Decision: Use React for frontend', importance: 'critical' },
|
{ text: 'Decision: Use React for frontend', importance: 'critical' },
|
||||||
{ content: 'Project initialized', importance: 'normal' },
|
{ text: 'Project initialized', importance: 'normal' },
|
||||||
],
|
],
|
||||||
sessions: [{
|
sessions: [{
|
||||||
title: 'Setup session',
|
title: 'Setup session',
|
||||||
@@ -323,12 +347,12 @@ describe('workspace-state', () => {
|
|||||||
}],
|
}],
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = buildWorkspaceState({
|
const result = await buildWorkspaceState({
|
||||||
dataDir: tmpDir,
|
dataDir: tmpDir,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
workspaceId: 'ws6',
|
workspaceId: 'ws6',
|
||||||
wsManager: makeManager(new Map([['ws6', { id: 'ws6', name: 'Full' }]])),
|
wsManager: makeManager(new Map([['ws6', { id: 'ws6', name: 'Full' }]])),
|
||||||
activateWorkspaceMind: noopActivate,
|
memory,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
expect(result).not.toBeNull();
|
||||||
@@ -344,9 +368,9 @@ describe('workspace-state', () => {
|
|||||||
expect(Array.isArray(result!.nextActions)).toBe(true);
|
expect(Array.isArray(result!.nextActions)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("lists the workspace's awareness items from waggle.db as active, not other scopes", () => {
|
it("lists the workspace's awareness items from waggle.db as active, not other scopes", async () => {
|
||||||
setupWorkspace('ws7', {
|
await setupWorkspace('ws7', {
|
||||||
frames: [{ content: 'Project with open tasks', importance: 'normal' }],
|
memories: [{ text: 'Project with open tasks', importance: 'normal' }],
|
||||||
awareness: [
|
awareness: [
|
||||||
{ category: 'task', content: 'Ship the installer', priority: 2 },
|
{ category: 'task', content: 'Ship the installer', priority: 2 },
|
||||||
{ category: 'flag', content: 'Context flag only', priority: 1 },
|
{ 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).add('task', 'Personal errand', 5);
|
||||||
new AwarenessLayer(waggleDb, workspaceDataScope('other')).add('task', 'Other workspace task', 5);
|
new AwarenessLayer(waggleDb, workspaceDataScope('other')).add('task', 'Other workspace task', 5);
|
||||||
|
|
||||||
const result = buildWorkspaceState({
|
const result = await buildWorkspaceState({
|
||||||
dataDir: tmpDir,
|
dataDir: tmpDir,
|
||||||
waggleDb,
|
waggleDb,
|
||||||
workspaceId: 'ws7',
|
workspaceId: 'ws7',
|
||||||
wsManager: makeManager(new Map([['ws7', { id: 'ws7', name: 'Tasks' }]])),
|
wsManager: makeManager(new Map([['ws7', { id: 'ws7', name: 'Tasks' }]])),
|
||||||
|
memory,
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
expect(result).not.toBeNull();
|
||||||
|
|||||||
@@ -25,8 +25,6 @@ describe('Workspace & Session API', () => {
|
|||||||
'utf-8'
|
'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 });
|
server = await buildLocalServer({ dataDir, port: 0 });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import os from 'node:os';
|
import os from 'node:os';
|
||||||
import { MindDB } from '@waggle/core';
|
|
||||||
import { buildLocalServer } from '../src/local/index.js';
|
import { buildLocalServer } from '../src/local/index.js';
|
||||||
import type { FastifyInstance } from 'fastify';
|
import type { FastifyInstance } from 'fastify';
|
||||||
import { injectWithAuth } from './test-utils.js';
|
import { injectWithAuth } from './test-utils.js';
|
||||||
@@ -32,11 +31,6 @@ describe('Workspace Templates API', () => {
|
|||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-ws-templates-'));
|
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 });
|
server = await buildLocalServer({ dataDir: tmpDir });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user