Files
waggle-os/packages/server/src/local/workspace-state.ts
shumeiko.it e9ed8c0765 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.
2026-10-24 13:19:06 +02:00

389 lines
14 KiB
TypeScript

/**
* Workspace State — structured current-state reconstruction for a workspace.
*
* Assembles typed, classified items from workspace memory, session files,
* and awareness layer into a coherent model of "what's going on now."
*
* Design principles:
* - Freshness comes from timestamps, not item type (a blocker can be stale)
* - Stale means "not recently touched", not "unimportant"
* - Compounding quality > quantity — deduplicate, distill, don't spam
*/
import fs from 'node:fs';
import path from 'node:path';
import { AwarenessLayer, WaggleDB, workspaceDataScope } from '@waggle/core';
import {
workspaceScope,
type MemoryRecord,
type MemoryScope,
type MemoryStore,
} from '@waggle/memory-contract';
import {
extractProgressItems,
extractOpenQuestions,
classifyThreads,
type ProgressItem,
type OpenQuestion,
type ThreadInfo,
} from './routes/sessions.js';
import { sanitizeExtracted } from './routes/session-utils.js';
// ── Types ──────────────────────────────────────────────────────────────
export type Freshness = 'fresh' | 'aging' | 'stale';
export type StateSource = 'memory' | 'session' | 'awareness';
export interface StateItem {
content: string;
freshness: Freshness;
source: StateSource;
sourceId?: string;
dateLastTouched: string; // ISO date (YYYY-MM-DD)
}
export interface WorkspaceState {
/** Currently active threads (fresh sessions, active awareness items) */
active: StateItem[];
/** Unresolved questions / undecided items from recent sessions */
openQuestions: StateItem[];
/** Tasks mentioned but not yet completed */
pending: StateItem[];
/** Items explicitly blocked */
blocked: StateItem[];
/** Recently completed work */
completed: StateItem[];
/** Threads not touched in 7+ days */
stale: StateItem[];
/** Recent decisions from memory */
recentDecisions: StateItem[];
/** Derived: prioritized list of what to do next */
nextActions: string[];
}
// ── Freshness calculation ──────────────────────────────────────────────
/** Thresholds in days */
const FRESH_DAYS = 2;
const AGING_DAYS = 7;
export function computeFreshness(dateStr: string): Freshness {
const date = new Date(dateStr);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffDays = diffMs / (86400 * 1000);
if (diffDays < FRESH_DAYS) return 'fresh';
if (diffDays < AGING_DAYS) return 'aging';
return 'stale';
}
// ── Workspace memory reads ─────────────────────────────────────────────
/** 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);
}
/** The newest decisions of a scope, temporary memories excluded. */
export async function listDecisions(
memory: MemoryStore,
scope: MemoryScope,
max: number,
): Promise<MemoryRecord[]> {
const recent = await memory.list({ scopes: [scope], minImportance: 'normal', limit: DECISION_SCAN });
return recent.filter(isDecision).slice(0, max);
}
/**
* 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;
}
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 ──────────────────────────────────
function extractAwarenessItems(waggleDb: WaggleDB, workspaceId: string): StateItem[] {
const items = new AwarenessLayer(waggleDb, workspaceDataScope(workspaceId)).getAll();
return items
.filter(i => i.category === 'task' || i.category === 'action' || i.category === 'pending')
.map(i => ({
content: i.content,
freshness: computeFreshness(i.created_at),
source: 'awareness' as StateSource,
sourceId: String(i.id),
dateLastTouched: i.created_at.slice(0, 10),
}));
}
// ── Progress items → StateItems ────────────────────────────────────────
function progressToStateItems(items: ProgressItem[], type: 'task' | 'completed' | 'blocker'): StateItem[] {
return items
.filter(p => p.type === type)
.map(p => ({
content: p.content,
freshness: computeFreshness(p.date),
source: 'session' as StateSource,
sourceId: p.sessionId,
dateLastTouched: p.date,
}));
}
// ── Open questions → StateItems ────────────────────────────────────────
function questionsToStateItems(questions: OpenQuestion[]): StateItem[] {
return questions.map(q => ({
content: q.content,
freshness: computeFreshness(q.date),
source: 'session' as StateSource,
sourceId: q.sessionId,
dateLastTouched: q.date,
}));
}
// ── Threads → StateItems ───────────────────────────────────────────────
function threadsToStateItems(threads: ThreadInfo[], freshness: Freshness): StateItem[] {
return threads
.filter(t => t.freshness === freshness || (freshness === 'fresh' && t.freshness === 'fresh'))
.map(t => ({
content: t.title,
freshness: t.freshness,
source: 'session' as StateSource,
sourceId: t.sessionId,
dateLastTouched: t.lastActive.slice(0, 10),
}));
}
// ── Next-action derivation (priority cascade) ──────────────────────────
function deriveNextActions(state: Omit<WorkspaceState, 'nextActions'>, max: number): string[] {
const actions: string[] = [];
// 1. Blocked items (by freshness — fresher blockers first)
const freshBlockers = state.blocked.filter(b => b.freshness !== 'stale');
for (const b of freshBlockers) {
if (actions.length >= max) break;
actions.push(`Resolve: ${b.content}`);
}
// 2. Open questions
const freshQuestions = state.openQuestions.filter(q => q.freshness !== 'stale');
for (const q of freshQuestions) {
if (actions.length >= max) break;
actions.push(`Decide: ${q.content}`);
}
// 3. Pending tasks
const freshPending = state.pending.filter(p => p.freshness !== 'stale');
for (const p of freshPending) {
if (actions.length >= max) break;
actions.push(p.content);
}
// 4. Stale threads with work
for (const s of state.stale) {
if (actions.length >= max) break;
actions.push(`Resume: ${s.content}`);
}
// 5. Fallback
if (actions.length === 0 && state.recentDecisions.length > 0) {
actions.push('Review recent decisions and plan next steps');
}
return actions.slice(0, max);
}
// ── Main builder ───────────────────────────────────────────────────────
interface WsManagerLike {
get: (id: string) => { id: string; name: string } | null | undefined;
}
export interface BuildWorkspaceStateOpts {
dataDir: string;
workspaceId: string;
wsManager: WsManagerLike;
/** waggle.db, which holds the workspace's awareness items. */
waggleDb: WaggleDB;
memory: MemoryStore;
}
/** 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;
// ── Memory-sourced state ─────────────────────────────────────────
let recentDecisions: StateItem[] = [];
let awarenessItems: StateItem[] = [];
try {
const scope = workspaceScope(workspaceId);
if (await memory.count({ scopes: [scope] }) === 0) return null;
recentDecisions = (await listDecisions(memory, scope, 5)).map(toDecisionItem);
} catch {
return null;
}
// Awareness items live in waggle.db; failing to read them must not drop the
// memory-sourced state above.
try { awarenessItems = extractAwarenessItems(waggleDb, workspaceId); } catch { /* non-blocking */ }
// ── Session-sourced state ────────────────────────────────────────
const sessionsDir = path.join(dataDir, 'workspaces', workspaceId, 'sessions');
let progress: ProgressItem[] = [];
let openQuestions: OpenQuestion[] = [];
let threads: ThreadInfo[] = [];
if (fs.existsSync(sessionsDir)) {
try { progress = extractProgressItems(sessionsDir, 10); } catch { /* non-blocking */ }
try { openQuestions = extractOpenQuestions(sessionsDir, 10); } catch { /* non-blocking */ }
try { threads = classifyThreads(sessionsDir, 20); } catch { /* non-blocking */ }
}
// ── Assemble state ──────────────────────────────────────────────
const pending = progressToStateItems(progress, 'task').slice(0, 5);
const blocked = progressToStateItems(progress, 'blocker').slice(0, 5);
const completed = progressToStateItems(progress, 'completed').slice(0, 5);
// Active = fresh threads + awareness items
const activeThreadItems = threadsToStateItems(threads.filter(t => t.freshness === 'fresh'), 'fresh').slice(0, 5);
const active = [...awarenessItems, ...activeThreadItems].slice(0, 5);
// Stale = threads not recently touched (but potentially still important!)
const staleThreads = threadsToStateItems(threads.filter(t => t.freshness === 'stale'), 'stale').slice(0, 3);
const openQuestionItems = questionsToStateItems(openQuestions).slice(0, 5);
const stateWithoutActions = {
active,
openQuestions: openQuestionItems,
pending,
blocked,
completed,
stale: staleThreads,
recentDecisions: recentDecisions.slice(0, 5),
};
const nextActions = deriveNextActions(stateWithoutActions, 5);
return { ...stateWithoutActions, nextActions };
}
// ── Formatter (for system prompt injection) ────────────────────────────
export function formatWorkspaceStatePrompt(state: WorkspaceState, workspaceName: string): string {
const sections: string[] = [];
sections.push(`# Workspace Now — ${workspaceName}`);
sections.push('');
if (state.active.length > 0) {
sections.push('## Active');
for (const a of state.active) {
sections.push(`- ${a.content}${a.freshness === 'aging' ? ' (aging)' : ''}`);
}
sections.push('');
}
if (state.openQuestions.length > 0) {
sections.push('## Open Questions');
for (const q of state.openQuestions) {
sections.push(`- ${q.content}${q.freshness !== 'fresh' ? ` (${q.freshness})` : ''}`);
}
sections.push('');
}
if (state.blocked.length > 0) {
sections.push('## Blocked');
for (const b of state.blocked) {
sections.push(`- ${b.content}${b.freshness !== 'fresh' ? ` (${b.freshness})` : ''}`);
}
sections.push('');
}
if (state.pending.length > 0) {
sections.push('## Pending');
for (const p of state.pending) {
sections.push(`- ${p.content}`);
}
sections.push('');
}
if (state.recentDecisions.length > 0) {
sections.push('## Recent Decisions');
for (const d of state.recentDecisions) {
sections.push(`- ${d.content}${d.freshness !== 'fresh' ? ` (${d.freshness})` : ''}`);
}
sections.push('');
}
if (state.completed.length > 0) {
sections.push('## Completed');
for (const c of state.completed) {
sections.push(`- ${c.content}`);
}
sections.push('');
}
if (state.stale.length > 0) {
sections.push('## Needs Attention (stale)');
for (const s of state.stale) {
sections.push(`- ${s.content} — not touched recently`);
}
sections.push('');
}
if (state.nextActions.length > 0) {
sections.push('## Likely Next Actions');
for (const a of state.nextActions) {
sections.push(`- ${a}`);
}
sections.push('');
}
return sections.join('\n').trimEnd();
}