Files
waggle-os/packages/agent/tests/prompt-assembler-feature-flag.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

110 lines
4.9 KiB
TypeScript

import { describe, it, expect, beforeEach } from 'vitest';
import { createInMemoryMemoryStore } from '@waggle/memory-contract';
import { Orchestrator } from '../src/orchestrator.js';
import { FEATURE_FLAGS, isEnabled } from '../src/feature-flags.js';
/**
* Brief §12.4 — feature-flag snapshot tests. Proves:
* - PROMPT_ASSEMBLER flag exists and follows env-var convention.
* - W4 flip (2026-06-11): flag is DEFAULT ON; WAGGLE_PROMPT_ASSEMBLER=0 is
* the kill switch (vitest.setup.ts pins tests to 0 — legacy-path suites
* stay deterministic; assembler suites opt in explicitly).
* - Flag-off buildSystemPrompt path is stable — regression guard.
* - Flag-on path (buildAssembledPrompt) returns well-formed AssembledPrompt.
*/
describe('PROMPT_ASSEMBLER feature flag', () => {
it('flag key exists on FEATURE_FLAGS', () => {
expect('PROMPT_ASSEMBLER' in FEATURE_FLAGS).toBe(true);
});
it('flag is default-ON with kill switch WAGGLE_PROMPT_ASSEMBLER=0', () => {
const envValue = process.env['WAGGLE_PROMPT_ASSEMBLER'];
const expectedFlag = envValue !== '0';
expect(FEATURE_FLAGS.PROMPT_ASSEMBLER).toBe(expectedFlag);
expect(isEnabled('PROMPT_ASSEMBLER')).toBe(expectedFlag);
});
it('test environment pins the flag OFF (vitest.setup.ts invariant)', () => {
// vitest.setup.ts sets WAGGLE_PROMPT_ASSEMBLER=0 unless already set —
// the broad suites exercise the legacy path; eval harness conditions
// C/D/F override with =1 explicitly.
if (process.env['WAGGLE_PROMPT_ASSEMBLER'] === '0') {
expect(FEATURE_FLAGS.PROMPT_ASSEMBLER).toBe(false);
}
});
});
describe('Orchestrator prompt paths — flag-off regression protection', () => {
let orchestrator: Orchestrator;
beforeEach(() => {
orchestrator = new Orchestrator({
memory: createInMemoryMemoryStore(),
model: 'claude-opus-4-7',
});
});
it('buildSystemPrompt() is stable across successive calls on a fresh memory', async () => {
const a = await orchestrator.buildSystemPrompt();
const b = await orchestrator.buildSystemPrompt();
expect(a).toBe(b);
});
it('buildSystemPrompt() does not mention PromptAssembler sections by default', async () => {
const out = await orchestrator.buildSystemPrompt();
expect(out).not.toContain('# Response format');
expect(out).not.toContain('# Recent changes');
});
it('buildAssembledPrompt() returns well-formed output on a fresh memory', async () => {
const assembled = await orchestrator.buildAssembledPrompt('test query', null);
expect(typeof assembled.system).toBe('string');
expect(assembled.system.length).toBeGreaterThan(0);
expect(assembled.userPrefix).toBe('');
expect(assembled.debug.tier).toBe('frontier'); // opus-4-7 → frontier
expect(assembled.debug.totalChars).toBe(assembled.system.length);
expect(Array.isArray(assembled.debug.sectionsIncluded)).toBe(true);
});
it('buildAssembledPrompt() maps tier correctly for small models', async () => {
const smallOrch = new Orchestrator({
memory: createInMemoryMemoryStore(),
model: 'gemma-4-31b',
});
const assembled = await smallOrch.buildAssembledPrompt('plan me a migration from A to B', null);
expect(assembled.debug.tier).toBe('small');
});
it('buildAssembledPrompt() excludes all outside context from closed-world rewrites', async () => {
orchestrator.setGoalAncestry({ project: 'Outside project context' });
orchestrator.getAwareness().add('task', 'Ship immediately from prior context', 10);
await orchestrator.executeTool('save_memory', {
content: 'Outside memory says the release is safe.',
importance: 'important',
});
const ordinary = await orchestrator.buildAssembledPrompt('What is the current project context?', null);
const closedWorld = await orchestrator.buildAssembledPrompt(
'Rewrite this memo and add no new claims: API tests pass.',
null,
);
expect(ordinary.system).toContain('Outside project context');
expect(closedWorld.debug.closedWorldRewrite).toBe(true);
expect(closedWorld.system).toContain('# Closed-world rewrite');
expect(closedWorld.system).not.toContain('Outside project context');
expect(closedWorld.system).not.toContain('Ship immediately from prior context');
expect(closedWorld.system).not.toContain('Outside memory says the release is safe.');
expect(closedWorld.debug.sectionsIncluded).toEqual(['Closed-world rewrite']);
});
it('buildSystemPrompt() and buildAssembledPrompt() produce different shapes (flag-off vs PA path)', async () => {
const legacy = await orchestrator.buildSystemPrompt();
const assembled = await orchestrator.buildAssembledPrompt('compare MECE vs BPMN', null);
// Both contain identity scaffolding, but the PA output has structured section headers
// the legacy path lacks (e.g., "## Persona:" when persona=null, "# State" via frame selection).
// The raw strings won't be equal.
expect(assembled.system).not.toBe(legacy);
});
});