refactor(server): search memory through the contract in artifacts, agent runs, route proposals and onboarding (P1.5)

- Artifact Center related search lists personal and workspace memories
  through server.memory and finds related chat sessions in the session
  files (UF-034).
- /api/agent/run searches the requested workspace's memory, or personal
  memory when no known workspace is given.
- Executor briefs search only the workspace's own memory and leave out
  temporary memories in the query; route proposals no longer borrow a
  mind from the cache.
- Onboarding treats any personal memory as evidence of prior use.
- The service no longer creates personal.mind on a fresh install, and
  isFirstRun also looks for waggle.db.
This commit is contained in:
shumeiko.it
2026-10-24 12:50:15 +02:00
parent e9ed8c0765
commit f310333970
13 changed files with 239 additions and 219 deletions

View File

@@ -2,12 +2,16 @@ import type { FastifyPluginAsync } from 'fastify';
import type {
Artifact, ArtifactKind, ArtifactStatus, Memory, RelatedRef, RelatedSearchResult,
} from '@waggle/shared';
import { FrameStore, SessionStore } from '@waggle/core';
import path from 'node:path';
import {
PERSONAL_SCOPE, workspaceScope, type MemoryRecord, type MemoryScope,
} from '@waggle/memory-contract';
import {
readArtifactIndex, addArtifact, getArtifactInWorkspace,
patchArtifactInWorkspace, deleteArtifactFromWorkspace, type NewArtifactInput,
} from './artifact-index.js';
import { normalizeToMemory } from './memory-center.js';
import { memoryStatus } from '../memory-status.js';
import { searchSessions } from './session-utils.js';
import { readTasks } from './tasks.js';
import { emitAuditEvent } from './events.js';
import { assertSafeSegment } from './validate.js';
@@ -52,7 +56,6 @@ const MAX_LIST = 200;
const MAX_WORKSPACE_FANOUT = 24;
const PER_SOURCE_CAP = 10;
const MEMORY_SCAN = 100;
const SESSION_SCAN = 50;
const asKind = (v: unknown): ArtifactKind | undefined =>
ARTIFACT_KINDS.includes(v as ArtifactKind) ? (v as ArtifactKind) : undefined;
@@ -63,6 +66,34 @@ const clampStr = (s: unknown, max: number): string => String(s ?? '').slice(0, m
const clampStrArray = (a: unknown, maxItems: number, maxLen: number): string[] =>
Array.isArray(a) ? a.slice(0, maxItems).map((x) => clampStr(x, maxLen)) : [];
/** First non-empty line of a memory, capped for display. */
function memoryTitle(text: string): string {
const firstLine = text.split('\n').find((l) => l.trim().length > 0)?.trim() ?? text.trim();
return firstLine.length > 80 ? `${firstLine.slice(0, 79)}…` : firstLine;
}
const stringArray = (v: unknown): string[] | undefined =>
Array.isArray(v) ? v.filter((x): x is string => typeof x === 'string') : undefined;
/** Project a memory into the shared `Memory` shape the Artifact Center renders. */
function toRelatedMemory(record: MemoryRecord): Memory {
const meta = record.metadata;
return {
id: record.id,
kind: record.kind === 'decision' || record.kind === 'preference' ? record.kind : 'fact',
title: typeof meta.title === 'string' && meta.title.trim() ? meta.title : memoryTitle(record.text),
content: record.text,
scope: record.scope.kind,
...(record.scope.kind === 'workspace' ? { workspaceId: record.scope.workspaceId } : {}),
source: record.source,
importance: record.importance,
tags: stringArray(meta.tags),
status: memoryStatus(record),
createdAt: record.createdAt,
...(record.updatedAt ? { updatedAt: record.updatedAt } : {}),
};
}
/** Case-insensitive substring match over an artifact's title + tags. */
function artifactMatches(a: Artifact, ql: string): boolean {
if (a.title.toLowerCase().includes(ql)) return true;
@@ -159,23 +190,23 @@ export const artifactRoutes: FastifyPluginAsync = async (server) => {
.sort((a, b) => (b.updatedAt ?? '').localeCompare(a.updatedAt ?? ''))
.slice(0, PER_SOURCE_CAP);
// memories — personal + (scoped|all) workspace minds, via the shared normalizer.
// memories — recent personal memories, then each (scoped|all) workspace's.
const memories: Memory[] = [];
try {
const scan = (db: import('@waggle/core').MindDB | null | undefined, mind: string, wsId?: string) => {
if (!db || memories.length >= PER_SOURCE_CAP) return;
for (const f of new FrameStore(db).getRecent(MEMORY_SCAN)) {
const m = normalizeToMemory(f, mind, wsId);
const scan = async (scope: MemoryScope) => {
if (memories.length >= PER_SOURCE_CAP) return;
for (const record of await server.memory.list({ scopes: [scope], limit: MEMORY_SCAN })) {
const m = toRelatedMemory(record);
if (m.content.toLowerCase().includes(ql) || m.title.toLowerCase().includes(ql)) {
memories.push(m);
if (memories.length >= PER_SOURCE_CAP) break;
}
}
};
scan(server.multiMind.personal, 'personal');
await scan(PERSONAL_SCOPE);
for (const wsId of workspaceIds(only)) {
if (memories.length >= PER_SOURCE_CAP) break;
scan(server.agentState.getWorkspaceMindDb(wsId), 'workspace', wsId);
await scan(workspaceScope(wsId));
}
} catch {
// degrade to whatever was collected
@@ -197,29 +228,21 @@ export const artifactRoutes: FastifyPluginAsync = async (server) => {
// skip
}
// sessions — bounded best-effort: recent active sessions whose summary matches.
// (Full closed-session search lives on the dedicated /sessions/search route.)
// sessions — chat sessions whose title, summary or messages match, the
// same search the dedicated /sessions/search route runs.
const sessions: RelatedRef[] = [];
try {
const scanSessions = (db: import('@waggle/core').MindDB | null | undefined, wsId?: string) => {
if (!db || sessions.length >= PER_SOURCE_CAP) return;
for (const s of new SessionStore(db).getActive().slice(0, SESSION_SCAN)) {
const hay = `${s.summary ?? ''} ${s.gop_id}`.toLowerCase();
if (hay.includes(ql)) {
sessions.push({
id: s.gop_id,
title: s.summary?.slice(0, 120) || s.gop_id,
workspaceId: wsId,
kind: s.status,
});
if (sessions.length >= PER_SOURCE_CAP) break;
}
}
};
scanSessions(server.multiMind.personal);
for (const wsId of workspaceIds(only)) {
if (sessions.length >= PER_SOURCE_CAP) break;
scanSessions(server.agentState.getWorkspaceMindDb(wsId), wsId);
const sessionsDir = path.join(dataDir, 'workspaces', wsId, 'sessions');
for (const hit of searchSessions(sessionsDir, q, PER_SOURCE_CAP - sessions.length)) {
sessions.push({
id: hit.sessionId,
title: hit.title.slice(0, 120),
workspaceId: wsId,
...(hit.snippets[0] ? { snippet: hit.snippets[0].text } : {}),
});
}
}
} catch {
// skip