Files
waggle-os/packages/agent/tests/r2-recall-closure.test.ts
shumeiko.it ac23bbd7a6 test(agent): move the agent suites onto the memory contract (P1.5)
Rewrites the orchestrator, recall, compaction, save_memory and assembler
suites for MemoryStore (createInMemoryMemoryStore; mem0 with the word
embedder for the M3b integration test), string ids, async prompt building
and setWorkspace(). Creation times are set with fake timers, since a write
cannot set its own. Stores of Waggle's own data (identity, traces) are built
on WaggleDB; the session-manager stubs use the new create() signature.

Kept behaviour: importance lane (cap, active scope, temporary exclusion),
profile and event lanes, date window and its fallback, catch-up, injection
blocking of recall and preloaded context, autosave safety, decision consent,
compaction update-in-place, reranker wiring.

New tests: recall with a workspace active versus personal only;
fetchRecentMemories ordering; toRecallFrame; context loader; save_memory
routing, dedup, rate limit, dramatic-claim downgrade and contradiction
scope; search_memory and search_all_workspaces; pattern write-back kinds,
scopes and sign gate; read_other_workspace; reranker off unless
WAGGLE_RERANKER=1, wider rerank pool, failure fallbacks.

Deleted, because the behaviour is gone:
- w46-rawdetail-recall.test.ts: the raw-turn (RAWDETAIL) recall lane.
- orchestrator.test.ts, query_knowledge (3 tests) and the recall-hardening
  M4 "Key Knowledge" test: the knowledge graph is not in v1 (ADR-0002).
- orchestrator-context-frames "profile override": scoring profiles.
- Assertions on I/P frame types, GOP sessions, FTS rows, the "Memory Facts"
  lane, cognify stages and session/entity counts were replaced by the
  equivalent contract-level checks.
2026-10-24 13:19:06 +02:00

67 lines
3.4 KiB
TypeScript

/**
* R5 / R2 closure — the memory-loop sign gate must be closed *end to end*.
*
* R2 wired `isSelfIncapacityAssertion()` into the autoSave `save()` chokepoint
* so DEFECT-2 poison is coerced to `temporary` importance. Its contract
* (memory-sign-gate.ts) is that `temporary` memories are *recall-excluded*.
*
* Search ranks by relevance and does not filter on importance, so a
* sign-gated memory is still retrievable by search; recallMemory must drop it
* before it re-enters the prompt. This is the behavioral proof of the closed
* loop, not just of the classifier.
*/
import { describe, it, expect, beforeEach } from 'vitest';
import { createInMemoryMemoryStore, PERSONAL_SCOPE, type MemoryStore } from '@waggle/memory-contract';
import { Orchestrator } from '../src/orchestrator.js';
describe('R2 sign-gate is closed end-to-end (temporary excluded from semantic recall)', () => {
let memory: MemoryStore;
let orchestrator: Orchestrator;
beforeEach(() => {
memory = createInMemoryMemoryStore();
orchestrator = new Orchestrator({ memory });
});
it('does not surface a sign-gated (temporary) DEFECT-2 memory via recallMemory, but still recalls the authoritative decision', async () => {
// A self-incapacity assertion — the R2 save-side gate coerces this exact
// class to `temporary` (orchestrator.ts:757). Seed it at the importance the
// gate produces.
await orchestrator.executeTool('save_memory', {
content:
"run npm install -g @modelcontextprotocol/server-filesystem yourself and restart the session before I can use it",
importance: 'temporary',
});
// The authoritative truth on the same topic — the control. Recall MUST
// still surface this; the exclusion has to be specific to `temporary`.
await orchestrator.executeTool('save_memory', {
content:
'Decision: the MCP filesystem connector is installed and wired for this workspace',
importance: 'important',
});
// Precondition: seeds landed at the intended importances (fences off the
// save_memory dramatic-claim downgrade / dedup confounds — if either fired
// this fails here with a clear signal rather than passing vacuously).
const records = await memory.list({ scopes: [PERSONAL_SCOPE] });
const poison = records.find(r => r.text.includes('npm install -g @modelcontextprotocol'));
const decision = records.find(r => r.text.includes('connector is installed and wired'));
expect(poison?.importance, 'poison memory must be stored as temporary').toBe('temporary');
expect(decision?.importance, 'decision memory must be stored as important').toBe('important');
// The search itself still finds the poison; only recall may drop it.
const hits = await memory.search('how do I install the mcp filesystem connector', { scopes: [PERSONAL_SCOPE] });
expect(hits.some(h => h.text.includes('npm install -g @modelcontextprotocol'))).toBe(true);
const result = await orchestrator.recallMemory(
'how do I install the mcp filesystem connector',
);
// The closed-loop invariant: a sign-gated memory must NOT re-enter the
// prompt as authoritative recall…
expect(result.text).not.toContain('run npm install -g @modelcontextprotocol');
// …while legitimate, authoritative memory on the same query still surfaces.
expect(result.text).toContain('connector is installed and wired');
});
});