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:
shumeiko.it
2026-10-24 12:50:07 +02:00
parent 4a1c4c4c8e
commit e9ed8c0765
23 changed files with 823 additions and 857 deletions

View File

@@ -335,6 +335,8 @@ export interface AgentState {
getWorkspaceMindDb: (workspaceId: string) => import('@waggle/core').MindDB | null;
/** Close and remove a workspace MindDB from cache. Used before workspace deletion to prevent EBUSY. */
closeWorkspaceMind: (workspaceId: string) => void;
/** Close a workspace's live sessions and detach it from the shared orchestrator. Used before workspace deletion. */
releaseWorkspace: (workspaceId: string) => void;
/** List all workspaces (id + name). Used by global memory search. */
listWorkspaces: () => Array<{ id: string; name: string }>;
/** Currently active workspace ID (null = personal only) */
@@ -1301,6 +1303,9 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
setActiveWorkspaceId(null);
}
};
// Until the legacy mind cache is removed, releasing a workspace also closes
// its cached mind so the workspace directory can be deleted (A6 EBUSY).
const releaseWorkspace = closeWorkspaceMind;
const getWorkspaceMindDb = (workspaceId: string): MindDB | null => {
if (!wsManager.get(workspaceId)) return null;
@@ -1328,6 +1333,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
activateWorkspaceMind: activateWorkspaceMind,
getWorkspaceMindDb,
closeWorkspaceMind,
releaseWorkspace,
listWorkspaces: () => wsManager.list().map(w => ({ id: w.id, name: w.name })),
activeWorkspaceId,
subagentOrchestrator: null,
@@ -1418,7 +1424,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
const proactiveCtx: ProactiveContext = {
dataDir: fullConfig.dataDir,
workspaceManager: wsManager,
getWorkspaceMindDb,
memory,
waggleDb,
};
@@ -1456,12 +1462,12 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
try {
switch (proactiveConfig.action) {
case 'morning_briefing': {
const briefing = generateMorningBriefing(proactiveCtx);
const briefing = await generateMorningBriefing(proactiveCtx);
if (briefing) emitProactive(briefing);
break;
}
case 'stale_workspace_check': {
const staleAlerts = checkStaleWorkspaces(proactiveCtx);
const staleAlerts = await checkStaleWorkspaces(proactiveCtx);
for (const alert of staleAlerts) emitProactive(alert);
break;
}
@@ -1471,7 +1477,7 @@ export async function buildLocalServer(config: Partial<LocalConfig> = {}) {
break;
}
case 'capability_suggestion': {
const suggestion = suggestCapabilities(proactiveCtx);
const suggestion = await suggestCapabilities(proactiveCtx);
if (suggestion) emitProactive(suggestion);
break;
}

View 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';
}

View File

@@ -7,12 +7,13 @@
* toast system picks them up.
*
* Separate from the Team proactive service (ProactiveService) which uses
* Drizzle/PostgreSQL. These handlers operate on local workspace/mind data.
* Drizzle/PostgreSQL. These handlers operate on local workspace and memory data.
*/
import fs from 'node:fs';
import path from 'node:path';
import { AwarenessLayer, workspaceDataScope, type WorkspaceManager, type WorkspaceConfig } from '@waggle/core';
import { AwarenessLayer, workspaceDataScope, type WorkspaceManager } from '@waggle/core';
import { workspaceScope, type MemoryRecord, type MemoryStore } from '@waggle/memory-contract';
import { materialFingerprint } from './notification-gate.js';
// ── Types ──────────────────────────────────────────────────────────────
@@ -32,8 +33,7 @@ export interface ProactiveMessage {
export interface ProactiveContext {
dataDir: string;
workspaceManager: WorkspaceManager;
/** Get a cached workspace MindDB (opens on demand). Returns null if workspace not found. */
getWorkspaceMindDb: (workspaceId: string) => import('@waggle/core').MindDB | null;
memory: MemoryStore;
/** waggle.db, which holds awareness items. */
waggleDb: import('@waggle/core').WaggleDB;
}
@@ -68,14 +68,21 @@ function countPendingAwareness(ctx: ProactiveContext, workspaceId: string): numb
}
}
/** Count memory frames in a workspace mind. */
function countMemoryFrames(ctx: ProactiveContext, workspaceId: string): number {
const db = ctx.getWorkspaceMindDb(workspaceId);
if (!db) return 0;
/** Recent memories scanned for the morning briefing's highlights. */
const HIGHLIGHT_SCAN = 200;
/** A briefing highlight: a decision or an important memory. */
function isHighlight(record: MemoryRecord): boolean {
return record.kind === 'decision'
|| record.importance === 'critical'
|| record.importance === 'important'
|| /^decision|decided/i.test(record.text);
}
/** Count the memories of a workspace. */
async function countMemories(ctx: ProactiveContext, workspaceId: string): Promise<number> {
try {
const raw = db.getDatabase();
const row = raw.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number } | undefined;
return row?.cnt ?? 0;
return await ctx.memory.count({ scopes: [workspaceScope(workspaceId)] });
} catch {
return 0;
}
@@ -96,7 +103,7 @@ function hasInstalledCapabilities(dataDir: string): boolean {
// ── Handlers ───────────────────────────────────────────────────────────
/** Generate morning briefing across all workspaces. */
export function generateMorningBriefing(ctx: ProactiveContext): ProactiveMessage | null {
export async function generateMorningBriefing(ctx: ProactiveContext): Promise<ProactiveMessage | null> {
const workspaces = ctx.workspaceManager.list();
if (workspaces.length === 0) return null;
@@ -120,21 +127,19 @@ export function generateMorningBriefing(ctx: ProactiveContext): ProactiveMessage
}
}
// W5.8: Also extract recent decisions and memory highlights per workspace
// W5.8: Also extract this week's decisions and memory highlights per workspace
const decisionParts: string[] = [];
const weekAgo = new Date(now - 7 * 24 * 60 * 60 * 1000).toISOString();
for (const ws of workspaces.slice(0, 5)) { // cap at 5 workspaces for brevity
const db = ctx.getWorkspaceMindDb(ws.id);
if (!db) continue;
try {
const raw = db.getDatabase();
const decisions = raw.prepare(
`SELECT content FROM memory_frames
WHERE (importance IN ('critical', 'important') OR content LIKE 'Decision%' OR content LIKE '%decided%')
AND created_at > datetime('now', '-7 day')
ORDER BY id DESC LIMIT 2`
).all() as Array<{ content: string }>;
const recent = await ctx.memory.list({
scopes: [workspaceScope(ws.id)],
since: weekAgo,
limit: HIGHLIGHT_SCAN,
});
const decisions = recent.filter(isHighlight).slice(0, 2);
if (decisions.length > 0) {
decisionParts.push(`**${ws.name}**: ${decisions.map(d => d.content.slice(0, 80)).join(' | ')}`);
decisionParts.push(`**${ws.name}**: ${decisions.map(d => d.text.slice(0, 80)).join(' | ')}`);
}
} catch { /* non-blocking */ }
}
@@ -177,7 +182,7 @@ export function generateMorningBriefing(ctx: ProactiveContext): ProactiveMessage
}
/** Check for stale workspaces (not visited in 14+ days). */
export function checkStaleWorkspaces(ctx: ProactiveContext): ProactiveMessage[] {
export async function checkStaleWorkspaces(ctx: ProactiveContext): Promise<ProactiveMessage[]> {
const workspaces = ctx.workspaceManager.list();
if (workspaces.length === 0) return [];
@@ -193,13 +198,13 @@ export function checkStaleWorkspaces(ctx: ProactiveContext): ProactiveMessage[]
const idleDays = Math.floor((now - referenceDate.getTime()) / (24 * 60 * 60 * 1000));
if (idleDays >= 14) {
const frameCount = countMemoryFrames(ctx, ws.id);
const frameCount = await countMemories(ctx, ws.id);
messages.push({
type: 'stale_workspace',
title: `"${ws.name}" hasn't been visited in ${idleDays} days`,
body: frameCount > 0
? `This workspace has ${frameCount} memory frame${frameCount === 1 ? '' : 's'} that may need attention.`
? `This workspace has ${frameCount} memor${frameCount === 1 ? 'y' : 'ies'} that may need attention.`
: 'Consider archiving or revisiting this workspace.',
workspaceId: ws.id,
actionUrl: `/workspaces/${ws.id}`,
@@ -207,7 +212,7 @@ export function checkStaleWorkspaces(ctx: ProactiveContext): ProactiveMessage[]
dedupeKey: `proactive:stale_workspace:${ws.id}`,
// Fingerprint on identity, not idleDays — so a still-stale workspace
// doesn't re-nag every day. Re-fires only if it's touched (new
// reference time) or its frame count changes.
// reference time) or its memory count changes.
materialHash: materialFingerprint({ wsId: ws.id, frameCount, referenceMs: referenceDate.getTime() }),
});
}
@@ -243,7 +248,7 @@ export function checkPendingTasks(ctx: ProactiveContext): ProactiveMessage[] {
}
/** Suggest capabilities based on usage patterns. */
export function suggestCapabilities(ctx: ProactiveContext): ProactiveMessage | null {
export async function suggestCapabilities(ctx: ProactiveContext): Promise<ProactiveMessage | null> {
// If the user has no installed capabilities, suggest exploring the catalog
if (!hasInstalledCapabilities(ctx.dataDir)) {
return {
@@ -264,7 +269,7 @@ export function suggestCapabilities(ctx: ProactiveContext): ProactiveMessage | n
// Count total memories across all workspaces
let totalFrames = 0;
for (const ws of workspaces) {
totalFrames += countMemoryFrames(ctx, ws.id);
totalFrames += await countMemories(ctx, ws.id);
}
// If accumulating significant memories but no connectors, suggest connectors

View File

@@ -87,24 +87,6 @@ export { MAX_CONTEXT_MESSAGES, applyContextWindow, buildSkillPromptSection } fro
export type AgentRunner = (config: AgentLoopConfig) => Promise<AgentResponse>;
/**
* W4A diagnostics — the "MindDB flake". A long chat turn holds a workspace
* MindDB handle obtained from the process-wide MultiMindCache (LRU, maxOpen:20).
* If ≥20 OTHER workspaces are touched mid-turn (home-briefing fan-out, weaver
* timers, cross-workspace tools), the cache evicts + `.close()`s THIS turn's
* handle, and the post-response DB write-backs below throw better-sqlite3's
* native "The database connection is not open". Those catches are non-blocking
* and swallow it silently, which is exactly why the flake is invisible. This
* predicate lets us surface a structured warn at the failure seam WITHOUT
* changing behavior (the write still fails soft) so the root cause can be
* confirmed live. The real fix (pin-aware eviction) lives in the OSS substrate
* (packages/hive-mind-core/src/multi-mind-cache.ts) and is out of scope here.
*/
function isClosedDbError(e: unknown): boolean {
const msg = e instanceof Error ? e.message : String(e);
return /database (connection|handle) is not open|database is closed/i.test(msg);
}
function isIncompleteCompletionError(error: unknown): boolean {
return typeof error === 'object'
&& error !== null
@@ -1030,12 +1012,12 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
// Workspace Now — inject structured context so the agent is grounded on first turn
if (includePersistedMemory && includeConversationDerivedWorkspaceState && workspaceId) {
try {
const nowBlock = buildWorkspaceNowBlock({
const nowBlock = await buildWorkspaceNowBlock({
dataDir: server.localConfig.dataDir,
waggleDb: server.waggleDb,
memory: server.memory,
workspaceId,
wsManager: server.workspaceManager,
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
cronSchedules: server.cronStore.list(),
});
if (nowBlock) {
@@ -1725,12 +1707,12 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
return 'Conversation-derived workspace state is disabled for this turn.';
}
if (!effectiveWorkspace) return 'No workspace state available.';
const block = buildWorkspaceNowBlock({
const block = await buildWorkspaceNowBlock({
dataDir: server.localConfig.dataDir,
waggleDb: server.waggleDb,
memory: server.memory,
workspaceId: effectiveWorkspace,
wsManager: server.workspaceManager,
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
cronSchedules: server.cronStore.list(),
});
if (!block) return 'No workspace state available.';
@@ -2451,8 +2433,7 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
// turns per #13, and for injected-runner turns like every other
// write-back seam in this route). The summary aggregates
// tool/connector output, so scan it before it can enter durable
// memory; fail-soft with the W4A closed-DB guard so persistence
// never fails the turn.
// memory; fail soft so persistence never fails the turn.
if (!hasCustomRunner && allowMemoryPersistence) {
const summaryScan = scanForInjection(compressionResult.summary, 'tool_output');
if (summaryScan.score >= 0.7) {
@@ -2469,11 +2450,7 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
}
} catch (e) {
throwIfTurnAborted();
if (isClosedDbError(e)) {
log.warn(`[context-compression] summary persist skipped — mind handle closed mid-turn (session=${sessionId})`);
} else {
log.warn(`[context-compression] summary persist failed: ${e instanceof Error ? e.message : e}`);
}
log.warn(`[context-compression] summary persist failed: ${e instanceof Error ? e.message : e}`);
}
}
}
@@ -3215,19 +3192,9 @@ ${wsConfig?.templateId ? `- Workspace template: ${wsConfig.templateId} — tailo
if (saved.length > 0) {
sendEvent('step', { content: `Auto-saved ${saved.length} memor${saved.length === 1 ? 'y' : 'ies'} from this exchange.` });
}
} catch (e) {
} catch {
throwIfTurnAborted();
// Non-blocking. W4A: a closed-handle failure here is the signature
// of the MultiMindCache evicting this turn's mind mid-flight — log
// it with context so the flake is observable, but still fail soft.
if (isClosedDbError(e)) {
log.warn('[waggle][W4A] workspace mind handle closed mid-turn during auto-save', {
workspaceId: effectiveWorkspace,
sessionId,
seam: 'autoSaveFromExchange',
error: e instanceof Error ? e.message : String(e),
});
}
// Non-blocking — the turn succeeds without the auto-save.
}
}
}

View File

@@ -13,7 +13,7 @@
* POST /api/command/execute — alias → commandRegistry.execute
*
* Surfaces federated by /search (CommandResultType in parens):
* - memory → server.multiMind.search() (`memory`)
* - memory → server.memory.search() (`memory`)
* - workspaces → server.workspaceManager.list() (`workspace`)
* - skills → loadSkills(dataDir) (`skill`)
* - sessions → searchSessions() over each workspace's sessions (`session`)
@@ -31,6 +31,7 @@ import type { FastifyPluginAsync } from 'fastify';
import type { CommandResult, Command } from '@waggle/shared';
import type { Orchestrator, LoadedSkill } from '@waggle/agent';
import { loadSkills } from '@waggle/agent';
import { PERSONAL_SCOPE, workspaceScope, type MemoryScope } from '@waggle/memory-contract';
import { buildWorkspaceNowBlock, formatWorkspaceNowPrompt } from './workspace-context.js';
import { searchSessions } from './session-utils.js';
import { interpretCommand } from '../command-interpret.js';
@@ -60,6 +61,16 @@ function safeFederate<T>(label: string, fn: () => T[], log: (m: string) => void)
}
}
async function safeFederateAsync<T>(label: string, fn: () => Promise<T[]>, log: (m: string) => void): Promise<T[]> {
try {
return await fn();
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
log(`command/search: ${label} federation failed — ${message}`);
return [];
}
}
/** Truncate a content blob into a single-line subtitle. */
function toSubtitle(content: string, max = 120): string {
const oneLine = content.replace(/\s+/g, ' ').trim();
@@ -104,17 +115,21 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
}));
}, logWarn));
// Memory — FTS over personal + active workspace minds.
results.push(...safeFederate('memory', () => {
const searchScope = scope === 'personal' || scope === 'workspace' ? scope : 'all';
const frames = server.multiMind.search(q, searchScope, perFacet);
return frames.map((frame): CommandResult => ({
id: `memory:${frame.id}`,
// Memory — ranked search over personal memory and the active workspace's.
results.push(...await safeFederateAsync('memory', async () => {
const activeWorkspaceId = server.agentState.activeWorkspaceId;
const scopes: MemoryScope[] = [];
if (scope !== 'workspace') scopes.push(PERSONAL_SCOPE);
if (scope !== 'personal' && activeWorkspaceId) scopes.push(workspaceScope(activeWorkspaceId));
if (scopes.length === 0) return [];
const hits = await server.memory.search(q, { scopes, limit: perFacet });
return hits.map((hit): CommandResult => ({
id: `memory:${hit.id}`,
type: 'memory',
title: toSubtitle(frame.content, 80),
subtitle: frame.created_at ? new Date(frame.created_at).toLocaleString('en-US') : undefined,
title: toSubtitle(hit.text, 80),
subtitle: new Date(hit.createdAt).toLocaleString('en-US'),
category: 'search',
action: { route: `/memory?frame=${encodeURIComponent(frame.id)}` },
action: { route: `/memory?frame=${encodeURIComponent(hit.id)}` },
}));
}, logWarn));
@@ -249,12 +264,12 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
}
},
getWorkspaceState: async (): Promise<string> => {
const block = buildWorkspaceNowBlock({
const block = await buildWorkspaceNowBlock({
dataDir: server.localConfig.dataDir,
waggleDb: server.waggleDb,
memory: server.memory,
workspaceId: effectiveWorkspaceId,
wsManager: server.workspaceManager,
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
cronSchedules: server.cronStore.list(),
});
if (!block) return 'No workspace state available.';
@@ -287,12 +302,12 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
// (awareness + recent sessions + pending). Best-effort.
let memoryContext = '';
try {
const block = buildWorkspaceNowBlock({
const block = await buildWorkspaceNowBlock({
dataDir: server.localConfig.dataDir,
waggleDb: server.waggleDb,
memory: server.memory,
workspaceId: workspaceId ?? 'default',
wsManager: server.workspaceManager,
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
cronSchedules: server.cronStore.list(),
});
if (block) memoryContext = formatWorkspaceNowPrompt(block);

View File

@@ -61,12 +61,12 @@ export const commandRoutes: FastifyPluginAsync = async (server) => {
},
getWorkspaceState: async (): Promise<string> => {
const block = buildWorkspaceNowBlock({
const block = await buildWorkspaceNowBlock({
dataDir: server.localConfig.dataDir,
waggleDb: server.waggleDb,
memory: server.memory,
workspaceId: effectiveWorkspaceId,
wsManager: server.workspaceManager,
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
cronSchedules: server.cronStore.list(),
});
if (!block) return 'No workspace state available.';

View File

@@ -12,11 +12,11 @@
* (typed WorkspaceState with `nextActions`, pending/blocked/recentDecisions).
* - `buildTimeAwareGreeting()` / `buildUpcomingSchedules()` —
* `packages/server/src/local/routes/workspace-context.ts:119,169`.
* - `IdentityLayer` — `@waggle/core` (mind/identity.ts), read off the personal
* mind for the user's name (B8: Home greets by name via existing identity).
* - `server.workspaceManager` (list/get/getMindPath) + `server.agentState`
* (activateWorkspaceMind) + `server.cronStore` (list/getExecutionHistory) +
* `server.multiMind.personal` + `getAuditDb()` (events.ts) — sidecar decorators.
* - `IdentityLayer` — `@waggle/core` (identity.ts), the profile card in
* waggle.db, for the user's name (B8: Home greets by name via existing identity).
* - `server.workspaceManager` (list/get) + `server.cronStore`
* (list/getExecutionHistory) + `server.memory` + `server.waggleDb` +
* `getAuditDb()` (events.ts) — sidecar decorators.
*
* Founder decisions honored: A2 (Home Cockpit personal-only v1 — team/shared
* workspaces are excluded from the cross-workspace fan-out; team-overnight rows
@@ -25,8 +25,9 @@
*/
import type { FastifyPluginAsync } from 'fastify';
import { FrameStore, IdentityLayer } from '@waggle/core';
import { normalizeToMemory } from './memory-center.js';
import { IdentityLayer } from '@waggle/core';
import { PERSONAL_SCOPE } from '@waggle/memory-contract';
import { memoryStatus } from '../memory-status.js';
import { buildWorkspaceState } from '../workspace-state.js';
import {
buildTimeAwareGreeting,
@@ -74,7 +75,7 @@ interface HomeBriefing {
upNext: UpNextItem[];
activeModels?: string[];
isFirstRun: boolean;
/** J08 (D6): personal-mind memories with status 'unreviewed' (C33 imports). */
/** J08 (D6): extracted personal facts the user has not confirmed yet. */
needsReviewCount: number;
}
@@ -113,10 +114,8 @@ const MAX_UP_NEXT = 8;
const OVERNIGHT_DEFAULT_HOURS = 24;
/** Per-schedule cron-history rows to scan for the overnight window. */
const CRON_HISTORY_SCAN = 50;
/** J08 needs-review scan bound — matches the 200-frame window the Memory
* Center list itself fetches (MemoryCenterTab limit=200), so the Home count
* always agrees with what the deep-linked "Needs review" view shows. */
const NEEDS_REVIEW_SCAN = 200;
/** J08 needs-review scan bound: high enough to count every extracted fact. */
const NEEDS_REVIEW_SCAN = 10_000;
/** Priority boost: each pending/blocked item ranks a workspace as if touched
* this much more recently (PRD §12.1 "ranked by recency AND priority"). */
const PENDING_BOOST_MS = 3_600_000; // 1 hour per pending item
@@ -300,12 +299,12 @@ export const homeRoutes: FastifyPluginAsync = async (server) => {
let anyStateForWs = false;
try {
const state = buildWorkspaceState({
const state = await buildWorkspaceState({
dataDir: server.localConfig.dataDir,
waggleDb: server.waggleDb,
memory: server.memory,
workspaceId: ws.id,
wsManager: server.workspaceManager,
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
});
if (state) {
anyMemory = true;
@@ -376,15 +375,18 @@ export const homeRoutes: FastifyPluginAsync = async (server) => {
}
}
// ── J08 (D6): personal-mind memories awaiting review ───────────
// Personal-only by design: harvest (C33) stamps status:'unreviewed' on
// personal-mind imports, and the deep-linked Memory Center "Needs review"
// view is personal-mind-only — a cross-mind count would not match it.
// ── J08 (D6): personal memories awaiting review ────────────────
// Facts the background extraction learned about the user stay unreviewed
// until confirmed. Personal-only by design: the deep-linked Memory Center
// "Needs review" view is personal-only — a cross-scope count would not match it.
let needsReviewCount = 0;
try {
for (const f of new FrameStore(server.multiMind.personal).getRecent(NEEDS_REVIEW_SCAN)) {
if (normalizeToMemory(f, 'personal').status === 'unreviewed') needsReviewCount++;
}
const extracted = await server.memory.list({
scopes: [PERSONAL_SCOPE],
origin: 'extraction',
limit: NEEDS_REVIEW_SCAN,
});
needsReviewCount = extracted.filter((record) => memoryStatus(record) === 'unreviewed').length;
} catch (err) {
log.warn('briefing: needs-review scan failed', (err as Error).message);
}

View File

@@ -1,11 +1,11 @@
import fs from 'node:fs';
import path from 'node:path';
import { MindDB, type WaggleDB } from '@waggle/core';
import type { WaggleDB } from '@waggle/core';
import { workspaceScope, type MemoryRecord, type MemoryStore } from '@waggle/memory-contract';
import { assertSafeSegment } from './validate.js';
import { extractProgressItems, type ProgressItem } from './sessions.js';
import {
buildWorkspaceState,
formatWorkspaceStatePrompt,
listByImportance,
type WorkspaceState,
} from '../workspace-state.js';
@@ -34,30 +34,30 @@ export type { WorkspaceState } from '../workspace-state.js';
// ── Workspace Manager interface (minimal, passed via opts) ─────────────
interface WsManagerLike {
get: (id: string) => { id: string; name: string } | null;
getMindPath: (id: string) => string;
get: (id: string) => { id: string; name: string } | null | undefined;
}
// ── Summary builder (mirrors composeWorkspaceSummary from workspaces.ts) ──
/** `memories` are ordered most important first (see `listByImportance`). */
function buildCompactSummary(
frames: Array<{ content: string; importance: string; created_at: string }>,
memories: MemoryRecord[],
memoryCount: number,
sessionCount: number,
): string {
const parts: string[] = [];
// What this workspace is about (from important/critical frames)
const important = frames.filter(f => f.importance === 'critical' || f.importance === 'important');
const workContextFrames = frames.filter(f =>
f.content.toLowerCase().includes('project') ||
f.content.toLowerCase().includes('workspace') ||
f.content.toLowerCase().includes('working on'),
// What this workspace is about (from important/critical memories)
const important = memories.filter(m => m.importance === 'critical' || m.importance === 'important');
const workContextMemories = memories.filter(m =>
m.text.toLowerCase().includes('project') ||
m.text.toLowerCase().includes('workspace') ||
m.text.toLowerCase().includes('working on'),
);
const aboutFrame = important[0] ?? workContextFrames[0] ?? frames[0];
if (aboutFrame) {
const aboutLine = aboutFrame.content.split('\n')[0].replace(/\.\s*$/, '').trim();
const aboutMemory = important[0] ?? workContextMemories[0] ?? memories[0];
if (aboutMemory) {
const aboutLine = aboutMemory.text.split('\n')[0].replace(/\.\s*$/, '').trim();
const aboutText = aboutLine.length > 140 ? aboutLine.slice(0, 137) + '...' : aboutLine;
if (aboutText.length > 10) {
parts.push(aboutText + '.');
@@ -65,7 +65,7 @@ function buildCompactSummary(
}
// Current state (activity level + recency)
const mostRecent = frames[0]?.created_at?.slice(0, 10) ?? '';
const mostRecent = memories[0]?.createdAt.slice(0, 10) ?? '';
const now = new Date();
const lastDate = mostRecent ? new Date(mostRecent) : null;
const daysSince = lastDate ? Math.floor((now.getTime() - lastDate.getTime()) / (86400 * 1000)) : 0;
@@ -221,206 +221,79 @@ export function buildUpcomingSchedules(schedules: CronScheduleLike[], workspaceI
// ── Main builder ───────────────────────────────────────────────────────
export function buildWorkspaceNowBlock(opts: {
/** Null when the workspace is unknown or has no memories yet. */
export async function buildWorkspaceNowBlock(opts: {
dataDir: string;
workspaceId: string;
wsManager: WsManagerLike;
/** waggle.db, which holds the workspace's awareness items. */
waggleDb: WaggleDB;
/** @deprecated Read-only context construction no longer mutates global workspace state. */
activateWorkspaceMind?: (id: string) => boolean;
memory: MemoryStore;
/** Optional cron schedules for upcoming schedule display */
cronSchedules?: CronScheduleLike[];
}): WorkspaceNowBlock | null {
const { dataDir, workspaceId, wsManager, waggleDb } = opts;
}): Promise<WorkspaceNowBlock | null> {
const { dataDir, workspaceId, wsManager, waggleDb, memory } = opts;
// Path-traversal guard: workspaceId becomes a path segment below
// (getMindPath + dataDir/workspaces/<workspaceId>/sessions). Reject any
// value containing illegal characters before it touches the filesystem.
// (dataDir/workspaces/<workspaceId>/sessions). Reject any value containing
// illegal characters before it touches the filesystem.
assertSafeSegment(workspaceId, 'workspaceId');
const ws = wsManager.get(workspaceId);
if (!ws) return null;
const mindPath = wsManager.getMindPath(workspaceId);
if (!fs.existsSync(mindPath)) return null;
// ── Try structured state first (new path) ────────────────────
const structuredState = buildWorkspaceState({
const structuredState = await buildWorkspaceState({
dataDir,
workspaceId,
wsManager,
waggleDb,
memory,
});
if (!structuredState) return null;
// Convert structured state → WorkspaceNowBlock for backward compat
const activeThreads = structuredState.active.slice(0, 3).map(a => {
const ago = formatRelativeTime(new Date(a.dateLastTouched).getTime());
const label = a.content.length > 60 ? a.content.slice(0, 57) + '...' : a.content;
return `${label} (${ago})`;
});
if (structuredState) {
// Convert structured state → WorkspaceNowBlock for backward compat
const activeThreads = structuredState.active.slice(0, 3).map(a => {
const ago = formatRelativeTime(new Date(a.dateLastTouched).getTime());
const label = a.content.length > 60 ? a.content.slice(0, 57) + '...' : a.content;
return `${label} (${ago})`;
});
const progressItems: string[] = [];
for (const p of structuredState.pending.slice(0, 3)) {
progressItems.push(`[task] ${p.content}`);
}
for (const b of structuredState.blocked.slice(0, 2)) {
progressItems.push(`[blocker] ${b.content}`);
}
for (const c of structuredState.completed.slice(0, 2)) {
progressItems.push(`[completed] ${c.content}`);
}
const decisions = structuredState.recentDecisions.slice(0, 3).map(d => d.content);
// Build summary from mind DB (still needed — structuredState doesn't carry it)
let summary = '';
let memoryCount = 0;
let wsDb: MindDB | null = null;
try {
wsDb = new MindDB(mindPath);
const raw = wsDb.getDatabase();
memoryCount = (raw.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number }).cnt;
const frames = raw.prepare(
`SELECT content, importance, created_at FROM memory_frames
WHERE importance != 'deprecated' AND importance != 'temporary'
ORDER BY CASE importance
WHEN 'critical' THEN 1 WHEN 'important' THEN 2
WHEN 'normal' THEN 3 ELSE 4 END,
id DESC LIMIT 8`,
).all() as Array<{ content: string; importance: string; created_at: string }>;
const sessDir = path.join(dataDir, 'workspaces', workspaceId, 'sessions');
const sessCount = fs.existsSync(sessDir)
? fs.readdirSync(sessDir).filter(f => f.endsWith('.jsonl')).length
: 0;
if (frames.length > 0) {
summary = buildCompactSummary(frames, memoryCount, sessCount);
}
wsDb.close();
wsDb = null;
} catch {
if (wsDb) { try { wsDb.close(); } catch { /* */ } }
}
// Compute last active from active threads or structured state
const lastActiveThread = structuredState.active[0]?.dateLastTouched ?? null;
const greeting = buildTimeAwareGreeting(lastActiveThread, { frameCount: memoryCount });
const pendingTasks = extractPendingTasks(progressItems);
const upcomingSchedules = buildUpcomingSchedules(opts.cronSchedules ?? [], workspaceId);
return {
workspaceName: ws.name as string,
summary,
recentDecisions: decisions,
activeThreads,
progressItems: progressItems.slice(0, 5),
nextActions: structuredState.nextActions.slice(0, 3),
greeting,
pendingTasks,
upcomingSchedules,
structuredState,
};
const progressItems: string[] = [];
for (const p of structuredState.pending.slice(0, 3)) {
progressItems.push(`[task] ${p.content}`);
}
for (const b of structuredState.blocked.slice(0, 2)) {
progressItems.push(`[blocker] ${b.content}`);
}
for (const c of structuredState.completed.slice(0, 2)) {
progressItems.push(`[completed] ${c.content}`);
}
// ── Fallback: legacy path (no structured state available) ────
const decisions = structuredState.recentDecisions.slice(0, 3).map(d => d.content);
// Summary from workspace memory (structuredState doesn't carry it)
let summary = '';
let decisions: string[] = [];
let memoryCount = 0;
let wsDb: MindDB | null = null;
try {
wsDb = new MindDB(mindPath);
const raw = wsDb.getDatabase();
memoryCount = (raw.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number }).cnt;
if (memoryCount === 0) {
wsDb.close();
return null;
}
const frames = raw.prepare(
`SELECT content, importance, created_at FROM memory_frames
WHERE importance != 'deprecated' AND importance != 'temporary'
ORDER BY CASE importance
WHEN 'critical' THEN 1 WHEN 'important' THEN 2
WHEN 'normal' THEN 3 ELSE 4 END,
id DESC LIMIT 8`,
).all() as Array<{ content: string; importance: string; created_at: string }>;
const scope = workspaceScope(workspaceId);
memoryCount = await memory.count({ scopes: [scope] });
const memories = await listByImportance(memory, scope, 8);
const sessDir = path.join(dataDir, 'workspaces', workspaceId, 'sessions');
const sessCount = fs.existsSync(sessDir)
? fs.readdirSync(sessDir).filter(f => f.endsWith('.jsonl')).length
: 0;
if (frames.length > 0) {
summary = buildCompactSummary(frames, memoryCount, sessCount);
if (memories.length > 0) {
summary = buildCompactSummary(memories, memoryCount, sessCount);
}
// Extract decisions inline (legacy)
const decisionFrames = raw.prepare(
`SELECT content FROM memory_frames
WHERE importance != 'deprecated' AND importance != 'temporary'
AND (content LIKE 'Decision%' OR content LIKE '%decided%'
OR content LIKE '%decision made%' OR content LIKE '%chose %'
OR content LIKE '%selected %' OR content LIKE '%agreed %'
OR importance = 'critical')
ORDER BY id DESC LIMIT 5`,
).all() as Array<{ content: string }>;
decisions = decisionFrames.slice(0, 3).map(f => {
const firstLine = f.content.split('\n')[0];
const sentenceMatch = firstLine.match(/^(.+?\.\s)(?=[A-Z])/);
const text = sentenceMatch
? sentenceMatch[1].trim()
: (firstLine.length > 150 ? firstLine.slice(0, 147) + '...' : firstLine);
return text.replace(/\.\s*$/, '');
});
wsDb.close();
wsDb = null;
} catch {
if (wsDb) { try { wsDb.close(); } catch { /* ignore */ } }
return null;
// Non-blocking — the block renders without a summary
}
// Extract progress items (cap at 5)
const sessionsDir = path.join(dataDir, 'workspaces', workspaceId, 'sessions');
let rawProgress: ProgressItem[] = [];
if (fs.existsSync(sessionsDir)) {
try { rawProgress = extractProgressItems(sessionsDir, 5); } catch { /* */ }
}
const progressItems = rawProgress.slice(0, 5).map(p => `[${p.type}] ${p.content}`);
// Derive next actions
const nextActions: string[] = [];
for (const p of rawProgress) {
if (p.type === 'blocker' && nextActions.length < 3) nextActions.push(`Resolve: ${p.content}`);
}
for (const p of rawProgress) {
if (p.type === 'task' && nextActions.length < 3) nextActions.push(p.content);
}
if (nextActions.length === 0 && decisions.length > 0) {
nextActions.push('Review recent decisions and determine next steps');
}
// Legacy path: derive last-active from session files
let legacyLastActive: string | null = null;
if (fs.existsSync(sessionsDir)) {
try {
const files = fs.readdirSync(sessionsDir).filter(f => f.endsWith('.jsonl'));
let maxMtime = 0;
for (const file of files) {
const stat = fs.statSync(path.join(sessionsDir, file));
if (stat.mtimeMs > maxMtime) maxMtime = stat.mtimeMs;
}
if (maxMtime > 0) legacyLastActive = new Date(maxMtime).toISOString();
} catch { /* */ }
}
const greeting = buildTimeAwareGreeting(legacyLastActive, { frameCount: memoryCount });
// Compute last active from active threads or structured state
const lastActiveThread = structuredState.active[0]?.dateLastTouched ?? null;
const greeting = buildTimeAwareGreeting(lastActiveThread, { frameCount: memoryCount });
const pendingTasks = extractPendingTasks(progressItems);
const upcomingSchedules = buildUpcomingSchedules(opts.cronSchedules ?? [], workspaceId);
@@ -428,12 +301,13 @@ export function buildWorkspaceNowBlock(opts: {
workspaceName: ws.name as string,
summary,
recentDecisions: decisions,
activeThreads: [],
progressItems,
nextActions: nextActions.slice(0, 3),
activeThreads,
progressItems: progressItems.slice(0, 5),
nextActions: structuredState.nextActions.slice(0, 3),
greeting,
pendingTasks,
upcomingSchedules,
structuredState,
};
}

View File

@@ -1,10 +1,10 @@
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import Database from 'better-sqlite3';
import type { FastifyPluginAsync } from 'fastify';
import { z } from 'zod';
import { MindDB, createFileStore, reconcileFtsIndex } from '@waggle/core';
import { AwarenessLayer, createFileStore, workspaceDataScope } from '@waggle/core';
import { workspaceScope, type MemoryRecord, type MemoryStore } from '@waggle/memory-contract';
import { assertSafeSegment } from './validate.js';
import { validateBody } from '../../validate-body.js';
@@ -49,7 +49,13 @@ const updateWorkspaceSchema = z.object({
}).strict();
import { extractProgressItems, type ProgressItem } from './sessions.js';
import { readFileRegistry, type FileRegistryEntry } from './ingest.js';
import { buildWorkspaceState, type WorkspaceState, type StateItem } from '../workspace-state.js';
import {
buildWorkspaceState,
listByImportance,
listDecisions,
type WorkspaceState,
type StateItem,
} from '../workspace-state.js';
import { buildTimeAwareGreeting, buildUpcomingSchedules } from './workspace-context.js';
import { emitAuditEvent, getAuditDb } from './events.js';
import { createLogger } from '../logger.js';
@@ -68,34 +74,34 @@ function isValidModelId(model: string): boolean {
/**
* A6: Compose a structured workspace summary — the "return reward moment."
* Produces a narrative summary with: what this workspace is about, recent state, key decisions.
* Produces a narrative summary with: what this workspace is about and recent state.
* `memories` are ordered most important first (see `listByImportance`).
*/
function composeWorkspaceSummary(
frames: Array<{ content: string; importance: string; created_at: string }>,
memories: MemoryRecord[],
memoryCount: number,
decisions: Array<{ content: string; created_at: string }>,
sessionCount: number,
): string {
const parts: string[] = [];
// ── What this workspace is about (from important/critical frames) ──
const important = frames.filter(f => f.importance === 'critical' || f.importance === 'important');
const workContextFrames = frames.filter(f =>
f.content.toLowerCase().includes('project') ||
f.content.toLowerCase().includes('workspace') ||
f.content.toLowerCase().includes('working on'),
// ── What this workspace is about (from important/critical memories) ──
const important = memories.filter(m => m.importance === 'critical' || m.importance === 'important');
const workContextMemories = memories.filter(m =>
m.text.toLowerCase().includes('project') ||
m.text.toLowerCase().includes('workspace') ||
m.text.toLowerCase().includes('working on'),
);
// Find the best "about" frame
const aboutFrame = important[0] ?? workContextFrames[0] ?? frames[0];
const aboutLine = aboutFrame.content.split('\n')[0].replace(/\.\s*$/, '').trim();
// Find the best "about" memory
const aboutMemory = important[0] ?? workContextMemories[0] ?? memories[0];
const aboutLine = aboutMemory.text.split('\n')[0].replace(/\.\s*$/, '').trim();
const aboutText = aboutLine.length > 140 ? aboutLine.slice(0, 137) + '...' : aboutLine;
if (aboutText.length > 10) {
parts.push(aboutText + '.');
}
// ── Current state (activity level + recency) ──
const mostRecent = frames[0]?.created_at?.slice(0, 10) ?? '';
const mostRecent = memories[0]?.createdAt.slice(0, 10) ?? '';
const now = new Date();
const lastDate = mostRecent ? new Date(mostRecent) : null;
const daysSince = lastDate ? Math.floor((now.getTime() - lastDate.getTime()) / (86400 * 1000)) : 0;
@@ -190,25 +196,14 @@ const TEMPLATE_WELCOME: Record<string, string> = {
};
/**
* Wave R (Lane B): cheap per-workspace memory count for the list route. Opens a
* READONLY raw sqlite connection (no MindDB schema-init, no sqlite-vec load) and
* runs a single COUNT(*) — far lighter than the full MindDB the /:id/context
* route uses, and it never touches the MultiMindCache, so the LRU-eviction
* hazard that originally kept memoryCount off this route does not apply. Returns
* undefined on any error (missing file / table / lock) so the card omits the
* count rather than fabricating a 0.
* Wave R (Lane B): per-workspace memory count for the list route. Returns
* undefined on any error so the card omits the count rather than fabricating a 0.
*/
function countWorkspaceMemories(mindPath: string): number | undefined {
if (!fs.existsSync(mindPath)) return undefined;
let db: import('better-sqlite3').Database | undefined;
async function countWorkspaceMemories(memory: MemoryStore, workspaceId: string): Promise<number | undefined> {
try {
db = new Database(mindPath, { readonly: true, fileMustExist: true });
const row = db.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number };
return row.cnt;
return await memory.count({ scopes: [workspaceScope(workspaceId)] });
} catch {
return undefined;
} finally {
try { db?.close(); } catch { /* already closed / never opened */ }
}
}
@@ -273,21 +268,18 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
}
// Wave F (fix 1a) + Wave R (Lane B): enrich each row with real card signal.
// - sessionCount: a readdir of the same sessions dir /:id/context counts.
// - memoryCount: a readonly COUNT(*) over the workspace mind (countWorkspace-
// Memories) — this is the R-round "living identity" unlock. It opens a
// lightweight readonly connection per row (no schema init, no vec load) and
// never touches the MultiMindCache, sidestepping the LRU hazard the prior
// comment warned about. Bounded to the user's own handful of workspaces.
// - memoryCount: the workspace's memory count — the R-round "living
// identity" unlock. Bounded to the user's own handful of workspaces.
// - lastSessionTitle: the newest session's title, for the card's "Last: …"
// activity body.
// Every enrichment omits its field on any error — never a fabricated 0/string.
return workspaces.map((ws) => {
return Promise.all(workspaces.map(async (ws) => {
try {
const sessionsDir = path.join(server.localConfig.dataDir, 'workspaces', ws.id, 'sessions');
const sessionCount = fs.existsSync(sessionsDir)
? fs.readdirSync(sessionsDir).filter((f) => f.endsWith('.jsonl')).length
: 0;
const memoryCount = countWorkspaceMemories(server.workspaceManager.getMindPath(ws.id));
const memoryCount = await countWorkspaceMemories(server.memory, ws.id);
const lastSessionTitle = readLastSessionTitle(sessionsDir);
return {
...ws,
@@ -298,7 +290,7 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
} catch {
return ws;
}
});
}));
});
// POST /api/workspaces — create workspace
@@ -469,32 +461,27 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
});
}
// M2-5: Seed template starter memory as visible Memory frames
// M2-5: Seed template starter memory as visible workspace memories
if (resolvedTemplateId && resolvedTemplateId !== 'blank') {
try {
const { BUILT_IN_TEMPLATES } = await import('./workspace-templates.js');
const tpl = BUILT_IN_TEMPLATES.find(t => t.id === resolvedTemplateId);
if (tpl?.starterMemory?.length) {
const mindPath = server.workspaceManager.getMindPath(ws.id);
const wsDb = new MindDB(mindPath);
const raw = wsDb.getDatabase();
// Create a synthetic session for onboarding frames (FK enforced)
raw.prepare(
"INSERT OR IGNORE INTO sessions (gop_id, project_id, status, summary) VALUES (?, ?, 'closed', 'Workspace template starter content')"
).run(`onboarding-${ws.id}`, ws.id);
// Seed each starter memory as an I-frame
const insert = raw.prepare(
"INSERT INTO memory_frames (frame_type, gop_id, t, content, importance, source) VALUES ('I', ?, ?, ?, 'normal', 'system')"
);
for (let i = 0; i < tpl.starterMemory.length; i++) {
insert.run(`onboarding-${ws.id}`, i, tpl.starterMemory[i]);
const starterMemory = BUILT_IN_TEMPLATES.find(t => t.id === resolvedTemplateId)?.starterMemory ?? [];
// Written in the background: the first memory write may wait for the
// embedding model to load, and workspace creation must not.
void (async () => {
for (const text of starterMemory) {
if (!server.workspaceManager.get(ws.id)) return; // deleted meanwhile
await server.memory.remember({
scope: workspaceScope(ws.id),
text,
kind: 'note',
source: 'system',
origin: 'workspace-template',
});
}
// These raw INSERTs bypass FrameStore.indexFts(), so the starter frames
// would be present in memory_frames but invisible to FTS5 keyword search.
// Reconcile the FTS index to make them searchable.
reconcileFtsIndex(wsDb);
wsDb.close();
}
})().catch((err: unknown) => {
log.warn(`[waggle] Starter memory seeding failed:`, (err as Error).message);
});
} catch (err) {
// Non-blocking — workspace creation succeeds even if memory seeding fails
log.warn(`[waggle] Starter memory seeding failed:`, (err as Error).message);
@@ -544,72 +531,46 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
let recentMemories: Array<{ content: string; importance: string; source: string; date: string }> = [];
let recentDecisions: Array<{ content: string; source: string; date: string }> = [];
const mindPath = server.workspaceManager.getMindPath(id);
if (fs.existsSync(mindPath)) {
try {
// Activate workspace mind to access its data
server.agentState.activateWorkspaceMind(id);
try {
// Opening a workspace makes it the active one
server.agentState.activateWorkspaceMind(id);
// Read workspace memory stats and recent frames
const wsDb = new MindDB(mindPath);
const raw = wsDb.getDatabase();
const scope = workspaceScope(id);
memoryCount = await server.memory.count({ scopes: [scope] });
memoryCount = (raw.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number }).cnt;
// Get recent important memories for the summary
// D2: Order by importance first (matching A3 preloaded-context fix), then recency
const memories = await listByImportance(server.memory, scope, 8);
// Get recent important memories for the summary
// D2: Order by importance first (matching A3 preloaded-context fix), then recency
const frames = raw.prepare(
`SELECT content, importance, source, created_at FROM memory_frames
WHERE importance != 'deprecated' AND importance != 'temporary'
ORDER BY CASE importance
WHEN 'critical' THEN 1 WHEN 'important' THEN 2
WHEN 'normal' THEN 3 ELSE 4 END,
id DESC LIMIT 8`
).all() as Array<{ content: string; importance: string; source: string; created_at: string }>;
recentMemories = memories.map(m => ({
content: m.text.slice(0, 200),
importance: m.importance,
source: m.source,
date: m.createdAt.slice(0, 10),
}));
recentMemories = frames.map(f => ({
content: f.content.slice(0, 200),
importance: f.importance,
source: f.source,
date: f.created_at?.slice(0, 10) ?? 'unknown',
}));
recentDecisions = (await listDecisions(server.memory, scope, 5)).map(m => {
const firstLine = m.text.split('\n')[0];
const sentenceMatch = firstLine.match(/^(.+?\.\s)(?=[A-Z])/);
const text = sentenceMatch
? sentenceMatch[1].trim()
: (firstLine.length > 150 ? firstLine.slice(0, 147) + '...' : firstLine);
return {
content: text.replace(/\.\s*$/, ''),
source: m.source,
date: m.createdAt.slice(0, 10),
};
});
// Extract decision-like memories (moved before summary so we can pass them)
const decisionFrames = raw.prepare(
`SELECT content, source, created_at FROM memory_frames
WHERE importance != 'deprecated' AND importance != 'temporary'
AND (content LIKE 'Decision%' OR content LIKE '%decided%'
OR content LIKE '%decision made%' OR content LIKE '%chose %'
OR content LIKE '%selected %' OR content LIKE '%agreed %'
OR importance = 'critical')
ORDER BY id DESC LIMIT 5`
).all() as Array<{ content: string; source: string; created_at: string }>;
recentDecisions = decisionFrames.map(f => {
const firstLine = f.content.split('\n')[0];
const sentenceMatch = firstLine.match(/^(.+?\.\s)(?=[A-Z])/);
const text = sentenceMatch
? sentenceMatch[1].trim()
: (firstLine.length > 150 ? firstLine.slice(0, 147) + '...' : firstLine);
return {
content: text.replace(/\.\s*$/, ''),
source: f.source,
date: f.created_at?.slice(0, 10) ?? 'unknown',
};
});
// A6: Build structured summary with decisions + session count
if (frames.length > 0) {
// Count sessions from filesystem (we'll have accurate count later, estimate here)
const sessDir = path.join(server.localConfig.dataDir, 'workspaces', id, 'sessions');
const sessCount = fs.existsSync(sessDir) ? fs.readdirSync(sessDir).filter(f => f.endsWith('.jsonl')).length : 0;
summary = composeWorkspaceSummary(frames, memoryCount, decisionFrames, sessCount);
}
wsDb.close();
} catch {
// If workspace mind can't be read, continue with empty context
// A6: Build structured summary with session count
if (memories.length > 0) {
// Count sessions from filesystem (we'll have accurate count later, estimate here)
const sessDir = path.join(server.localConfig.dataDir, 'workspaces', id, 'sessions');
const sessCount = fs.existsSync(sessDir) ? fs.readdirSync(sessDir).filter(f => f.endsWith('.jsonl')).length : 0;
summary = composeWorkspaceSummary(memories, memoryCount, sessCount);
}
} catch {
// If workspace memory can't be read, continue with empty context
}
// ── Gather session info ──────────────────────────────────
@@ -715,12 +676,12 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
// ── Structured workspace state (Slice 6) ──────────────
let workspaceState: WorkspaceState | null = null;
try {
workspaceState = buildWorkspaceState({
workspaceState = await buildWorkspaceState({
dataDir: server.localConfig.dataDir,
waggleDb: server.waggleDb,
memory: server.memory,
workspaceId: id,
wsManager: server.workspaceManager,
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
});
} catch { /* non-blocking */ }
@@ -775,9 +736,9 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
} catch { /* non-blocking — cron store may not be available */ }
// ── Wave 6.5 (DISABLED): Cross-workspace relevance hints ──
// Security review (cowork/Code-Review_MultiMind_April-2026.md Critical #1) flagged
// the original implementation as a privacy leak: it iterated every workspace the
// user owns, opened each MindDB directly, and returned 80-char content snippets in
// A security review (Critical #1) flagged the original implementation as a
// privacy leak: it iterated every workspace the user owns, read each
// workspace's memory directly, and returned 80-char content snippets in
// the HTTP response with NO grant check and no approval gate. That is a complete
// bypass of the `read_other_workspace` agent-tool approval contract.
//
@@ -841,12 +802,12 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
let state: WorkspaceState | null = null;
try {
state = buildWorkspaceState({
state = await buildWorkspaceState({
dataDir: server.localConfig.dataDir,
waggleDb: server.waggleDb,
memory: server.memory,
workspaceId: id,
wsManager: server.workspaceManager,
activateWorkspaceMind: server.agentState.activateWorkspaceMind,
});
} catch (err) {
log.warn(`state build failed for ${id}:`, (err as Error).message);
@@ -1024,10 +985,21 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
if (!existing) {
return reply.status(404).send({ error: 'Workspace not found' });
}
// A6: Close workspace mind DB before filesystem deletion to prevent EBUSY
server.agentState.closeWorkspaceMind(request.params.id);
server.workspaceManager.delete(request.params.id);
emitAuditEvent(server, { workspaceId: request.params.id, eventType: 'workspace_delete' });
const id = request.params.id;
// Its memories and awareness items live outside the workspace directory.
// Forget them first: a workspace created later under the same name gets
// the same id and must not inherit them.
try {
await server.memory.forgetMatching({ scopes: [workspaceScope(id)] });
new AwarenessLayer(server.waggleDb, workspaceDataScope(id)).clear();
} catch (err) {
log.warn(`could not forget the memory of workspace ${id}:`, (err as Error).message);
return reply.status(500).send({ error: 'Could not delete the workspace memory; the workspace was kept' });
}
// A6: Release the workspace before filesystem deletion to prevent EBUSY
server.agentState.releaseWorkspace(id);
server.workspaceManager.delete(id);
emitAuditEvent(server, { workspaceId: id, eventType: 'workspace_delete' });
return reply.status(204).send();
});
@@ -1045,21 +1017,16 @@ export const workspaceRoutes: FastifyPluginAsync = async (server) => {
}
// ── Gather memories ──────────────────────────────────────────
const memories: Array<{ content: string; importance: string; created_at: string; frame_type?: string }> = [];
const mindPath = server.workspaceManager.getMindPath(id);
if (fs.existsSync(mindPath)) {
try {
const wsDb = new MindDB(mindPath);
const raw = wsDb.getDatabase();
const frames = raw.prepare(
`SELECT content, importance, created_at, frame_type FROM memory_frames
WHERE importance != 'deprecated'
ORDER BY id DESC LIMIT 500`
).all() as Array<{ content: string; importance: string; created_at: string; frame_type: string }>;
memories.push(...frames);
wsDb.close();
} catch { /* non-blocking */ }
}
const memories: Array<{ content: string; importance: string; created_at: string; kind: string }> = [];
try {
const records = await server.memory.list({ scopes: [workspaceScope(id)], limit: 500 });
memories.push(...records.map(r => ({
content: r.text,
importance: r.importance,
created_at: r.createdAt,
kind: r.kind,
})));
} catch { /* non-blocking */ }
// ── Gather pinned items ──────────────────────────────────────
let pinnedItems: Array<{ id: string; messageContent: string; pinnedAt: string; label?: string }> = [];

View File

@@ -1,7 +1,7 @@
/**
* Workspace State — structured current-state reconstruction for a workspace.
*
* Assembles typed, classified items from memory frames, session files,
* Assembles typed, classified items from workspace memory, session files,
* and awareness layer into a coherent model of "what's going on now."
*
* Design principles:
@@ -12,7 +12,13 @@
import fs from 'node:fs';
import path from 'node:path';
import { AwarenessLayer, MindDB, WaggleDB, workspaceDataScope } from '@waggle/core';
import { AwarenessLayer, WaggleDB, workspaceDataScope } from '@waggle/core';
import {
workspaceScope,
type MemoryRecord,
type MemoryScope,
type MemoryStore,
} from '@waggle/memory-contract';
import {
extractProgressItems,
extractOpenQuestions,
@@ -72,42 +78,62 @@ export function computeFreshness(dateStr: string): Freshness {
return 'stale';
}
// ── Decision extraction from memory frames ─────────────────────────────
// ── Workspace memory reads ─────────────────────────────────────────────
interface DecisionFrame {
id: number;
content: string;
created_at: string;
/** Recent memories scanned for decisions. */
const DECISION_SCAN = 200;
/** Wording that marks a note as a decision when its kind does not. */
const DECISION_WORDING = /^decision|decided|decision made|chose |selected |agreed /i;
/** A decision: its kind says so, it is critical, or its wording records one. */
function isDecision(record: MemoryRecord): boolean {
return record.kind === 'decision' || record.importance === 'critical' || DECISION_WORDING.test(record.text);
}
function extractDecisionItems(
raw: ReturnType<MindDB['getDatabase']>,
/** The newest decisions of a scope, temporary memories excluded. */
export async function listDecisions(
memory: MemoryStore,
scope: MemoryScope,
max: number,
): StateItem[] {
const frames = raw.prepare(
`SELECT id, content, created_at FROM memory_frames
WHERE importance != 'deprecated' AND importance != 'temporary'
AND (content LIKE 'Decision%' OR content LIKE '%decided%'
OR content LIKE '%decision made%' OR content LIKE '%chose %'
OR content LIKE '%selected %' OR content LIKE '%agreed %'
OR importance = 'critical')
ORDER BY id DESC LIMIT ?`,
).all(max + 2) as DecisionFrame[];
): Promise<MemoryRecord[]> {
const recent = await memory.list({ scopes: [scope], minImportance: 'normal', limit: DECISION_SCAN });
return recent.filter(isDecision).slice(0, max);
}
return frames.slice(0, max).map(f => {
const firstLine = f.content.split('\n')[0];
const sentenceMatch = firstLine.match(/^(.+?\.\s)(?=[A-Z])/);
const text = sentenceMatch ? sentenceMatch[1] : firstLine;
/**
* Up to `limit` memories of a scope, most important first and newest first
* within one importance level; temporary memories are excluded.
*/
export async function listByImportance(
memory: MemoryStore,
scope: MemoryScope,
limit: number,
): Promise<MemoryRecord[]> {
const records: MemoryRecord[] = [];
for (const level of ['critical', 'important', 'normal'] as const) {
if (records.length >= limit) break;
// Every memory above `level` is already in `records`, so the newest
// `limit + records.length` memories at or above `level` contain the
// newest `limit` memories at `level`.
const candidates = await memory.list({ scopes: [scope], minImportance: level, limit: limit + records.length });
records.push(...candidates.filter(r => r.importance === level).slice(0, limit - records.length));
}
return records;
}
return {
// Strip cursor/markdown artifacts (leaking `|` pipes etc.) + tail-truncate.
content: sanitizeExtracted(text, 150),
freshness: computeFreshness(f.created_at),
source: 'memory' as StateSource,
sourceId: String(f.id),
dateLastTouched: f.created_at.slice(0, 10),
};
});
function toDecisionItem(record: MemoryRecord): StateItem {
const firstLine = record.text.split('\n')[0];
const sentenceMatch = firstLine.match(/^(.+?\.\s)(?=[A-Z])/);
const text = sentenceMatch ? sentenceMatch[1] : firstLine;
return {
// Strip cursor/markdown artifacts (leaking `|` pipes etc.) + tail-truncate.
content: sanitizeExtracted(text, 150),
freshness: computeFreshness(record.createdAt),
source: 'memory',
sourceId: record.id,
dateLastTouched: record.createdAt.slice(0, 10),
};
}
// ── Active items from awareness layer ──────────────────────────────────
@@ -209,8 +235,7 @@ function deriveNextActions(state: Omit<WorkspaceState, 'nextActions'>, max: numb
// ── Main builder ───────────────────────────────────────────────────────
interface WsManagerLike {
get: (id: string) => { id: string; name: string } | null;
getMindPath: (id: string) => string;
get: (id: string) => { id: string; name: string } | null | undefined;
}
export interface BuildWorkspaceStateOpts {
@@ -219,40 +244,25 @@ export interface BuildWorkspaceStateOpts {
wsManager: WsManagerLike;
/** waggle.db, which holds the workspace's awareness items. */
waggleDb: WaggleDB;
/** @deprecated Read-only state construction no longer mutates global workspace state. */
activateWorkspaceMind?: (id: string) => boolean;
memory: MemoryStore;
}
export function buildWorkspaceState(opts: BuildWorkspaceStateOpts): WorkspaceState | null {
const { dataDir, workspaceId, wsManager, waggleDb } = opts;
/** Null when the workspace is unknown, has no memories yet, or its memory cannot be read. */
export async function buildWorkspaceState(opts: BuildWorkspaceStateOpts): Promise<WorkspaceState | null> {
const { dataDir, workspaceId, wsManager, waggleDb, memory } = opts;
const ws = wsManager.get(workspaceId);
if (!ws) return null;
const mindPath = wsManager.getMindPath(workspaceId);
if (!fs.existsSync(mindPath)) return null;
// ── Memory-sourced state ─────────────────────────────────────────
let recentDecisions: StateItem[] = [];
let awarenessItems: StateItem[] = [];
let wsDb: MindDB | null = null;
try {
wsDb = new MindDB(mindPath);
const raw = wsDb.getDatabase();
const memoryCount = (raw.prepare('SELECT COUNT(*) as cnt FROM memory_frames').get() as { cnt: number }).cnt;
if (memoryCount === 0) {
wsDb.close();
return null;
}
recentDecisions = extractDecisionItems(raw, 5);
wsDb.close();
wsDb = null;
const scope = workspaceScope(workspaceId);
if (await memory.count({ scopes: [scope] }) === 0) return null;
recentDecisions = (await listDecisions(memory, scope, 5)).map(toDecisionItem);
} catch {
if (wsDb) { try { wsDb.close(); } catch { /* ignore */ } }
return null;
}