test(server): move the remaining server tests onto the memory contract (P1.5)

- Workspace sessions, fleet runs, agent groups, loops, the file indexer,
  external tool runs, chat and the health endpoint are checked against the
  memory store instead of .mind files. Assertions on mind-cache pins and
  releases go with the pins; the drain and cancellation checks stay.
- Delete the sticky-erasure test (harvest import was removed) and the
  session-distillation test (distillation on workspace activation was
  removed).
- The saved-history retry test checks the conversation only: the failed
  user turn is kept in workspace memory by design (#3).
This commit is contained in:
shumeiko.it
2026-10-24 12:50:23 +02:00
parent 74a4e55be0
commit 122b45990e
12 changed files with 274 additions and 785 deletions

View File

@@ -2,7 +2,8 @@ import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { MindDB, FrameStore, SessionStore, WaggleConfig } from '@waggle/core';
import { WaggleConfig } from '@waggle/core';
import { PERSONAL_SCOPE, workspaceScope, type MemoryRecord } from '@waggle/memory-contract';
import { buildLocalServer } from '../src/local/index.js';
import type { FastifyInstance } from 'fastify';
import type { AgentLoopConfig, AgentResponse } from '@waggle/agent';
@@ -50,6 +51,12 @@ function parseSSE(raw: string): Array<{ event: string; data: string }> {
return events;
}
/** A memory with exactly this text in personal memory or any workspace's memory. */
async function findMemory(server: FastifyInstance, text: string): Promise<MemoryRecord | undefined> {
const scopes = [PERSONAL_SCOPE, ...server.workspaceManager.list().map(ws => workspaceScope(ws.id))];
return (await server.memory.list({ scopes, limit: 1000 })).find(record => record.text === text);
}
describe('Chat Streaming API', () => {
let server: FastifyInstance;
let tmpDir: string;
@@ -114,15 +121,6 @@ describe('Chat Streaming API', () => {
beforeAll(async () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-chat-test-'));
// Create personal.mind with test data
const personalPath = path.join(tmpDir, 'personal.mind');
const mind = new MindDB(personalPath);
const sessions = new SessionStore(mind);
const frames = new FrameStore(mind);
const s1 = sessions.create('test-project');
frames.createIFrame(s1.gop_id, 'Waggle chat test content', 'normal');
mind.close();
// Mock agent runner that simulates streaming tokens
const mockAgentRunner = async (config: AgentLoopConfig): Promise<AgentResponse> => {
if (config.onToken) {
@@ -455,9 +453,10 @@ describe('Chat Streaming API', () => {
// ...but the raw user turn was still persisted to memory (write decoupled
// from generation success), so it is recallable on the next turn.
const persisted = server.agentState.orchestrator.getFrames().findDuplicate(seed);
expect(persisted).not.toBeNull();
expect(persisted!.content).toContain('my horse is named Comet');
const persisted = await findMemory(server, seed);
expect(persisted).toBeDefined();
expect(persisted!.text).toContain('my horse is named Comet');
expect(persisted!.source).toBe('user_stated');
});
it('keeps a failed broad no-change request in chat history without writing it to memory', async () => {
@@ -479,7 +478,7 @@ describe('Chat Streaming API', () => {
});
expect(parseSSE(res.body).filter(event => event.event === 'error')).toHaveLength(1);
expect(server.agentState.orchestrator.getFrames().findDuplicate(seed)).toBeNull();
expect(await findMemory(server, seed)).toBeUndefined();
const transcript = loadSessionMessages(
tmpDir,
authorizedWorkspace!,
@@ -1148,11 +1147,9 @@ describe('Chat Streaming API', () => {
let switchedAfterAuthorization = false;
expect(server.agentState.activateWorkspaceMind(memberWorkspace.id)).toBe(true);
server.agentState.createSessionOrchestrator = ((workspaceMind?: MindDB) => {
const requestOrchestrator = workspaceMind
? originalCreateSessionOrchestrator(workspaceMind)
: originalCreateSessionOrchestrator();
if (workspaceMind && !switchedAfterAuthorization) {
server.agentState.createSessionOrchestrator = ((workspaceId?: string) => {
const requestOrchestrator = originalCreateSessionOrchestrator(workspaceId);
if (workspaceId && !switchedAfterAuthorization) {
switchedAfterAuthorization = true;
expect(server.agentState.activateWorkspaceMind(viewerWorkspace.id)).toBe(true);
const saveMemory = requestOrchestrator.getTools()
@@ -1180,12 +1177,11 @@ describe('Chat Streaming API', () => {
expect(server.agentState.activeWorkspaceId).toBe(viewerWorkspace.id);
expect(memoryWrite).toBeDefined();
await memoryWrite;
const memberMind = server.agentState.getWorkspaceMindDb(memberWorkspace.id);
const viewerMind = server.agentState.getWorkspaceMindDb(viewerWorkspace.id);
expect(memberMind).not.toBeNull();
expect(viewerMind).not.toBeNull();
expect(new FrameStore(memberMind!).findDuplicate(memoryMarker)).not.toBeNull();
expect(new FrameStore(viewerMind!).findDuplicate(memoryMarker)).toBeNull();
const inScope = async (workspaceId: string) =>
(await server.memory.list({ scopes: [workspaceScope(workspaceId)], limit: 500 }))
.some(record => record.text === memoryMarker);
expect(await inScope(memberWorkspace.id)).toBe(true);
expect(await inScope(viewerWorkspace.id)).toBe(false);
expect(
server.agentState.sessionHistories.get(
chatSessionStateKey(memberWorkspace.id, sessionId),
@@ -1380,7 +1376,7 @@ describe('Chat Streaming API', () => {
const personalServer = await buildLocalServer({ dataDir: personalDir });
const initiallyActiveWorkspace = personalServer.agentState.activeWorkspaceId;
expect(initiallyActiveWorkspace).toBeTruthy();
personalServer.agentState.closeWorkspaceMind(initiallyActiveWorkspace!);
personalServer.agentState.releaseWorkspace(initiallyActiveWorkspace!);
expect(personalServer.agentState.activeWorkspaceId).toBeNull();
personalServer.workspaceManager.ensure('default', {
name: 'default',
@@ -1736,7 +1732,7 @@ describe('Chat Streaming API', () => {
};
try {
server.agentState.closeWorkspaceMind(previousActiveWorkspace!);
server.agentState.releaseWorkspace(previousActiveWorkspace!);
expect(server.agentState.activeWorkspaceId).toBeNull();
const legacyResponse = await injectWithAuth(server, {
method: 'POST',
@@ -2002,10 +1998,7 @@ describe('Chat Streaming API', () => {
}),
'utf-8',
);
const workspaceMind = new MindDB(
path.join(recoveryDir, 'workspaces', 'default', 'workspace.mind'),
);
workspaceMind.close();
fs.writeFileSync(path.join(recoveryDir, 'workspaces', 'default', 'workspace.mind'), '');
fs.writeFileSync(managedCandidate, managedContent, 'utf-8');
let recoveryServer = await buildLocalServer({ dataDir: recoveryDir });
@@ -2177,8 +2170,6 @@ describe('Chat LiteLLM key routing (regression: no_db_connection)', () => {
beforeAll(async () => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-chat-keyroute-'));
const mind = new MindDB(path.join(tmpDir, 'personal.mind'));
mind.close();
server = await buildLocalServer({ dataDir: tmpDir });