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

@@ -7,10 +7,10 @@
* GET /api/artifacts/:id one (resolves owning workspace)
* PATCH /api/artifacts/:id edit; Archive = status:'archived' (A8)
* DELETE /api/artifacts/:id hard delete (A8)
* GET /api/artifacts/search-related federated (artifacts+memories+tasks)
* GET /api/artifacts/search-related federated (artifacts+memories+sessions+tasks)
*
* Uses a tmp `localConfig.dataDir` so the artifacts.json / tasks.jsonl writes are
* isolated and cleaned up. A ':memory:' personal mind backs the memory federation.
* isolated and cleaned up. An in-memory memory store backs the memory federation.
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
@@ -19,42 +19,39 @@ import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { randomUUID } from 'node:crypto';
import { MindDB, FrameStore, SessionStore } from '@waggle/core';
import {
createInMemoryMemoryStore, PERSONAL_SCOPE, workspaceScope, type MemoryStore,
} from '@waggle/memory-contract';
import { artifactRoutes } from '../../src/local/routes/artifacts.js';
import { sessionRoutes } from '../../src/local/routes/sessions.js';
function createTestServer(db: MindDB, dataDir: string) {
function createTestServer(memory: MemoryStore, dataDir: string) {
const server = Fastify({ logger: false });
server.decorate('localConfig', { dataDir });
server.decorate('workspaceManager', {
list: () => [{ id: 'ws-test', name: 'Test Workspace' }],
get: (id: string) => id === 'ws-test' ? { id, name: 'Test Workspace' } : undefined,
});
server.decorate('agentState', {
getWorkspaceMindDb: () => undefined,
listWorkspaces: () => [],
});
server.decorate('multiMind', { personal: db });
server.decorate('memory', memory);
server.register(artifactRoutes);
server.register(sessionRoutes);
return server;
}
describe('Artifact Center routes (Phase 2C)', () => {
let db: MindDB;
let memory: MemoryStore;
let dataDir: string;
let server: ReturnType<typeof Fastify>;
beforeEach(() => {
dataDir = path.join(os.tmpdir(), `waggle-art-${randomUUID()}`);
fs.mkdirSync(dataDir, { recursive: true });
db = new MindDB(':memory:');
server = createTestServer(db, dataDir);
memory = createInMemoryMemoryStore();
server = createTestServer(memory, dataDir);
});
afterEach(async () => {
await server.close();
db.close();
// Best-effort: emitAuditEvent opens a long-lived audit.db connection (by
// design — the sidecar keeps it open), so on Windows the file can still be
// locked here. A temp-dir cleanup race must not fail a passing test; the OS
@@ -231,26 +228,41 @@ describe('Artifact Center routes (Phase 2C)', () => {
expect((await server.inject({ method: 'GET', url: '/api/artifacts/search-related' })).statusCode).toBe(400);
});
it('search-related federates artifacts + memories + tasks (agents empty in v1)', async () => {
it('search-related federates artifacts + memories + sessions + tasks (agents empty in v1)', async () => {
// artifact
await createArtifact({ title: 'Germany GTM deck', kind: 'presentation', workspaceId: 'ws-test' });
// memory in the personal mind
const session = new SessionStore(db).ensureActive();
new FrameStore(db).createIFrame(session.gop_id, 'Germany GTM strategy notes', 'normal', 'user_stated');
// memories: personal and the workspace's, plus a non-matching one
await memory.remember({ scope: PERSONAL_SCOPE, text: 'Germany GTM strategy notes', source: 'user_stated' });
await memory.remember({ scope: workspaceScope('ws-test'), text: 'Germany pricing decision', kind: 'decision' });
await memory.remember({ scope: PERSONAL_SCOPE, text: 'France launch notes' });
// task in the workspace tasks.jsonl
const wsDir = path.join(dataDir, 'workspaces', 'ws-test');
fs.mkdirSync(wsDir, { recursive: true });
fs.mkdirSync(path.join(wsDir, 'sessions'), { recursive: true });
const now = new Date().toISOString();
fs.writeFileSync(
path.join(wsDir, 'tasks.jsonl'),
JSON.stringify({ id: 't1', title: 'Germany GTM follow-up', status: 'open', createdAt: now, updatedAt: now }) + '\n',
);
// chat session that mentions the query
fs.writeFileSync(
path.join(wsDir, 'sessions', 'session-1.jsonl'),
[
JSON.stringify({ type: 'meta', title: 'Market planning', created: now }),
JSON.stringify({ role: 'user', content: 'Which cities in Germany first?' }),
].join('\n') + '\n',
);
const res = await server.inject({ method: 'GET', url: '/api/artifacts/search-related?q=germany' });
expect(res.statusCode).toBe(200);
const body = res.json();
expect(body.artifacts.length).toBeGreaterThanOrEqual(1);
expect(body.memories.length).toBeGreaterThanOrEqual(1);
expect(body.memories.map((m: { content: string }) => m.content).sort())
.toEqual(['Germany GTM strategy notes', 'Germany pricing decision']);
expect(body.memories.find((m: { scope: string }) => m.scope === 'workspace'))
.toMatchObject({ workspaceId: 'ws-test', kind: 'decision', status: 'active' });
expect(body.sessions).toEqual([
expect.objectContaining({ id: 'session-1', title: 'Market planning', workspaceId: 'ws-test' }),
]);
expect(body.tasks.length).toBeGreaterThanOrEqual(1);
expect(body.agents).toEqual([]);
});