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, beforeEach, afterEach, vi } from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
import { MindDB, WaggleDB, AwarenessLayer, type Embedder } from '@waggle/core';
import { WaggleDB, AwarenessLayer } from '@waggle/core';
import { createInMemoryMemoryStore, PERSONAL_SCOPE, type MemoryStore } from '@waggle/memory-contract';
import {
runLoopTick,
parseLoopSpec,
@@ -25,22 +26,12 @@ function makeChat(report = 'Report: nothing materially new since the last run.')
});
}
// Minimal embedder stub. Recall is best-effort in runLoopTick (wrapped in
// try/catch), so even if the vector path rejects, the tick proceeds.
const embedder = {
dimensions: 384,
embed: async () => new Float32Array(384),
embedBatch: async (texts: string[]) => texts.map(() => new Float32Array(384)),
getActiveProvider: () => 'stub',
getStatus: () => ({ modelName: 'stub' }),
} as unknown as Embedder;
const silentLog: LoopLogger = { info: () => {}, warn: () => {} };
function framesIn(db: MindDB, gop: string): Array<{ content: string; source: string }> {
return db.getDatabase()
.prepare('SELECT content, source FROM memory_frames WHERE gop_id = ?')
.all(gop) as Array<{ content: string; source: string }>;
/** Memories the loop wrote. */
async function loopMemories(memory: MemoryStore): Promise<Array<{ content: string; source: string }>> {
const records = await memory.list({ scopes: [PERSONAL_SCOPE], origin: 'loop' });
return records.map(r => ({ content: r.text, source: r.source }));
}
/** Seed a prior-tick awareness row so the cost floor has a lastTickAt to read. */
@@ -52,19 +43,18 @@ function seedPriorTick(awareness: AwarenessLayer, scheduleId: number, lastTickAt
describe('runLoopTick (Loop v0 — L1 report-only)', () => {
let tmpDir: string;
let db: MindDB;
let memory: MemoryStore;
let waggleDb: WaggleDB;
let awareness: AwarenessLayer;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-loop-'));
db = new MindDB(path.join(tmpDir, 'test.mind'));
memory = createInMemoryMemoryStore();
waggleDb = new WaggleDB(':memory:');
awareness = new AwarenessLayer(waggleDb);
});
afterEach(() => {
db.close();
waggleDb.close();
fs.rmSync(tmpDir, { recursive: true, force: true });
});
@@ -79,20 +69,20 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
};
}
it('writes a report frame to the loop gop with source agent_inferred', async () => {
it('writes a report memory with origin loop and source agent_inferred', async () => {
const chat = makeChat();
const res = await runLoopTick({ schedule: schedule(), mindDb: db, awareness, embedder, chat, log: silentLog });
const res = await runLoopTick({ schedule: schedule(), memory, scope: PERSONAL_SCOPE, awareness, chat, log: silentLog });
expect(res.skipped).toBeFalsy();
expect(res.wrote).toBe(true);
const frames = framesIn(db, 'loop');
const frames = await loopMemories(memory);
expect(frames).toHaveLength(1);
expect(frames[0].source).toBe('agent_inferred');
expect(frames[0].content).toContain('Report: nothing materially new');
});
it('records the judge score and surfaces it in the result', async () => {
const res = await runLoopTick({ schedule: schedule(), mindDb: db, awareness, embedder, chat: makeChat(), log: silentLog });
const res = await runLoopTick({ schedule: schedule(), memory, scope: PERSONAL_SCOPE, awareness, chat: makeChat(), log: silentLog });
// weighted 0.8*0.5 + 0.7*0.3 + 0.9*0.2 = 0.79 (length penalty 1 for a short report)
expect(res.score).toBeCloseTo(0.79, 2);
});
@@ -102,7 +92,7 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
// lastTickAt = now, which would otherwise throttle the second).
const noFloor = JSON.stringify({ prompt: 'Observe the pipeline.', minIntervalMs: 0 });
const chat1 = makeChat('First run: 3 deals at risk.');
await runLoopTick({ schedule: schedule({ job_config: noFloor }), mindDb: db, awareness, embedder, chat: chat1, log: silentLog });
await runLoopTick({ schedule: schedule({ job_config: noFloor }), memory, scope: PERSONAL_SCOPE, awareness, chat: chat1, log: silentLog });
// Prior state lives in waggle.db under the namespaced status key.
const items = new AwarenessLayer(waggleDb).getByStatus('loop:1');
@@ -111,7 +101,7 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
// Second tick: the maker prompt must carry the prior report.
const chat2 = makeChat('Second run: 1 new at-risk deal.');
await runLoopTick({ schedule: schedule({ job_config: noFloor }), mindDb: db, awareness, embedder, chat: chat2, log: silentLog });
await runLoopTick({ schedule: schedule({ job_config: noFloor }), memory, scope: PERSONAL_SCOPE, awareness, chat: chat2, log: silentLog });
const makerCall = chat2.mock.calls.find(c => !String(c[0]).includes('Return the JSON now'));
expect(makerCall?.[0]).toContain('First run: 3 deals at risk');
@@ -124,26 +114,26 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
it('still reports when the judge output is unparseable (score undefined, no throw)', async () => {
// Judge returns junk → LLMJudge yields parsed:false, score stays undefined.
const chat = vi.fn(async () => 'not json at all');
const res = await runLoopTick({ schedule: schedule(), mindDb: db, awareness, embedder, chat, log: silentLog });
const res = await runLoopTick({ schedule: schedule(), memory, scope: PERSONAL_SCOPE, awareness, chat, log: silentLog });
expect(res.skipped).toBeFalsy();
expect(res.score).toBeUndefined();
expect(framesIn(db, 'loop')).toHaveLength(1); // the report itself is "not json at all" — still written
expect(await loopMemories(memory)).toHaveLength(1); // the report itself is "not json at all" — still written
});
it('throttles a tick whose last real run (awareness lastTickAt) is within the cost floor', async () => {
const chat = makeChat();
seedPriorTick(awareness, 1, new Date(Date.now() - 60_000).toISOString()); // ran 1 min ago
const res = await runLoopTick({ schedule: schedule(), mindDb: db, awareness, embedder, chat, log: silentLog });
const res = await runLoopTick({ schedule: schedule(), memory, scope: PERSONAL_SCOPE, awareness, chat, log: silentLog });
expect(res.skipped).toBe(true);
expect(res.reason).toBe('within min interval');
expect(chat).not.toHaveBeenCalled();
expect(framesIn(db, 'loop')).toHaveLength(0);
expect(await loopMemories(memory)).toHaveLength(0);
});
it('runs when the last real run is older than the cost floor', async () => {
const chat = makeChat();
seedPriorTick(awareness, 1, new Date(Date.now() - (LOOP_MIN_INTERVAL_MS + 60_000)).toISOString());
const res = await runLoopTick({ schedule: schedule(), mindDb: db, awareness, embedder, chat, log: silentLog });
const res = await runLoopTick({ schedule: schedule(), memory, scope: PERSONAL_SCOPE, awareness, chat, log: silentLog });
expect(res.skipped).toBeFalsy();
expect(chat).toHaveBeenCalled();
});
@@ -155,7 +145,7 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
// when last_run_at looks "just now".
const chat = makeChat();
const justNowSqlite = new Date().toISOString().slice(0, 19).replace('T', ' '); // 'YYYY-MM-DD HH:MM:SS'
const res = await runLoopTick({ schedule: schedule({ last_run_at: justNowSqlite }), mindDb: db, awareness, embedder, chat, log: silentLog });
const res = await runLoopTick({ schedule: schedule({ last_run_at: justNowSqlite }), memory, scope: PERSONAL_SCOPE, awareness, chat, log: silentLog });
expect(res.skipped).toBeFalsy();
expect(chat).toHaveBeenCalled();
});
@@ -164,7 +154,7 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
const chat = makeChat();
const res = await runLoopTick({
schedule: schedule({ job_config: '{}' }),
mindDb: db, awareness, embedder, chat, log: silentLog,
memory, scope: PERSONAL_SCOPE, awareness, chat, log: silentLog,
});
expect(res.skipped).toBe(true);
expect(res.reason).toBe('no prompt');
@@ -175,11 +165,11 @@ describe('runLoopTick (Loop v0 — L1 report-only)', () => {
const chat = makeChat();
const res = await runLoopTick({
schedule: schedule({ job_config: JSON.stringify({ prompt: 'observe', writeToMemory: false }) }),
mindDb: db, awareness, embedder, chat, log: silentLog,
memory, scope: PERSONAL_SCOPE, awareness, chat, log: silentLog,
});
expect(res.skipped).toBeFalsy();
expect(res.wrote).toBeFalsy();
expect(framesIn(db, 'loop')).toHaveLength(0);
expect(await loopMemories(memory)).toHaveLength(0);
});
});
@@ -241,16 +231,16 @@ describe('parseProposal', () => {
describe('runLoopTick — assist mode (L2 proposals)', () => {
let tmpDir: string;
let db: MindDB;
let memory: MemoryStore;
let waggleDb: WaggleDB;
let awareness: AwarenessLayer;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-loop-a-'));
db = new MindDB(path.join(tmpDir, 'test.mind'));
memory = createInMemoryMemoryStore();
waggleDb = new WaggleDB(':memory:');
awareness = new AwarenessLayer(waggleDb);
});
afterEach(() => { db.close(); waggleDb.close(); fs.rmSync(tmpDir, { recursive: true, force: true }); });
afterEach(() => { waggleDb.close(); fs.rmSync(tmpDir, { recursive: true, force: true }); });
function assistChat(report: string, fence: string) {
return vi.fn(async (prompt: string) => {
@@ -264,7 +254,7 @@ describe('runLoopTick — assist mode (L2 proposals)', () => {
it('report mode never proposes an action', async () => {
const chat = vi.fn(async () => 'A plain report, no fence.');
const res = await runLoopTick({ schedule: sched({ prompt: 'sweep' }), mindDb: db, awareness, embedder, chat, log: silentLog });
const res = await runLoopTick({ schedule: sched({ prompt: 'sweep' }), memory, scope: PERSONAL_SCOPE, awareness, chat, log: silentLog });
expect(res.proposedAction).toBeUndefined();
});
@@ -272,20 +262,20 @@ describe('runLoopTick — assist mode (L2 proposals)', () => {
const chat = assistChat('Three deals went quiet.', '{"tool":"send_email","args":{"to":"x@y.z"},"summary":"nudge them"}');
const res = await runLoopTick({
schedule: sched({ prompt: 'sweep', mode: 'assist' }),
mindDb: db, awareness, embedder, chat, log: silentLog,
memory, scope: PERSONAL_SCOPE, awareness, chat, log: silentLog,
});
expect(res.proposedAction).toEqual({ tool: 'send_email', args: { to: 'x@y.z' }, summary: 'nudge them' });
expect(res.summary).toContain('Three deals went quiet');
expect(res.summary).not.toContain('```json');
// The frame written to memory must be the clean report, not the JSON fence.
const frames = db.getDatabase().prepare("SELECT content FROM memory_frames WHERE gop_id='loop'").all() as Array<{ content: string }>;
// The memory written must be the clean report, not the JSON fence.
const frames = await loopMemories(memory);
expect(frames).toHaveLength(1);
expect(frames[0].content).not.toContain('```json');
});
it('assist mode with a {none:true} proposal yields no action', async () => {
const chat = assistChat('Nothing actionable today.', '{"none":true}');
const res = await runLoopTick({ schedule: sched({ prompt: 'sweep', mode: 'assist' }), mindDb: db, awareness, embedder, chat, log: silentLog });
const res = await runLoopTick({ schedule: sched({ prompt: 'sweep', mode: 'assist' }), memory, scope: PERSONAL_SCOPE, awareness, chat, log: silentLog });
expect(res.proposedAction).toBeUndefined();
expect(res.summary).toContain('Nothing actionable');
});