refactor(memory): run agent recall, memory tools and the server on the memory contract (P1.5)

- Orchestrator, memory tools, context loader and pattern write-back use a
  MemoryStore and a workspace scope instead of MindDB layers; the knowledge
  graph tools, cognify, memory linker and feedback handler are gone (graph
  hidden in v1, ADR-0002).
- The server opens one mem0 store in <dataDir>/memory/, embedded by the
  in-process model on first use, and extracts facts about the user in the
  background with the model the user chose (memoryExtraction setting).
- Legacy .mind memories are copied into the store once, in the background;
  the legacy files are only read.
- Weaver consolidation, harvest, wiki, memory import, memory lanes, vector
  backfill and index reconciliation are removed; agent runs, loops, sub-agent
  results, connector fetch and the failed-turn capture write through the
  contract. Workspace sessions no longer hold a mind handle.
- FileIndexer and the install audit move to waggle.db; recall date helpers and
  the reranker are copied into core.
- The CLI REPL opens the same memory store.
This commit is contained in:
shumeiko.it
2026-10-24 11:56:26 +02:00
parent f3eb6b6652
commit 784ca770b1
86 changed files with 2520 additions and 8444 deletions

View File

@@ -4,32 +4,26 @@
* A "Loop" is a stateful, memory-powered scheduled automation for knowledge
* work. Each tick composes pieces Waggle already ships:
*
* recall (HybridSearch) → what does this workspace's memory say about the goal?
* recall (memory search) → what does this workspace's memory say about the goal?
* prior state (Awareness)→ what did the PREVIOUS tick report? (cross-tick spine)
* maker (toolless chat) → produce a report grounded in the above
* checker (LLMJudge) → verify the report against a rubric (the gate)
* write-back (FrameStore + Awareness) → persist into the local mind
* write-back (memory + Awareness) → persist into local memory
*
* L1 / report-only guarantee: the maker is a plain text completion with NO
* tools wired, so a tick cannot send an email, write a file, or touch any
* external system — the only side effect is writing to the workspace's own
* `.mind` (local, sovereign). This is what makes Loop v0 safe by construction;
* memory (local, sovereign). This is what makes Loop v0 safe by construction;
* the headless ConfirmationGate deny-default (confirmation.ts) is the second
* line of defence for the day tools are added.
*
* Everything here is reused infrastructure — no new abstractions. The executor
* is extracted from index.ts only so it can be unit-tested with an in-memory
* MindDB + a stubbed chat function.
* memory store + a stubbed chat function.
*/
import {
AwarenessLayer,
FrameStore,
SessionStore,
HybridSearch,
type MindDB,
type Embedder,
} from '@waggle/core';
import type { AwarenessLayer } from '@waggle/core';
import type { MemoryScope, MemoryStore } from '@waggle/memory-contract';
import { LLMJudge, scanForInjection } from '@waggle/agent';
/** Minimal slice of a CronSchedule the loop executor needs (decoupled for tests). */
@@ -50,12 +44,11 @@ export interface LoopLogger {
export interface LoopTickDeps {
schedule: LoopSchedule;
/** The already-resolved workspace (or personal) mind for this loop. */
mindDb: MindDB;
memory: MemoryStore;
/** The workspace (or personal) memory this loop reads and writes. */
scope: MemoryScope;
/** Awareness items of the same scope, in waggle.db; the loop keeps its state there. */
awareness: AwarenessLayer;
/** Embedder for recall-by-meaning. */
embedder: Embedder;
/** Toolless completion against the local proxy/LiteLLM. */
chat: LoopChat;
log: LoopLogger;
@@ -196,14 +189,14 @@ export function buildMakerPrompt(opts: {
}
/**
* Run one Loop tick. Pure-ish: all I/O goes through `deps` (mindDb, embedder,
* Run one Loop tick. Pure-ish: all I/O goes through `deps` (memory, awareness,
* chat). Throws are intentionally NOT swallowed for the maker/judge LLM path so
* the scheduler records the failure in cron_execution_history and auto-disables
* after repeated failures (feeds the engine-liveness/health story); recall and
* memory-write failures ARE caught (best-effort, must not fail the whole tick).
*/
export async function runLoopTick(deps: LoopTickDeps): Promise<LoopTickResult> {
const { schedule, mindDb, awareness, embedder, chat, log } = deps;
const { schedule, memory, scope, awareness, chat, log } = deps;
const spec = parseLoopSpec(schedule.job_config);
if (!spec) {
@@ -233,8 +226,8 @@ export async function runLoopTick(deps: LoopTickDeps): Promise<LoopTickResult> {
// Recall prior context by meaning (best-effort — a recall failure must not kill the tick).
let recalled = '';
try {
const hits = await new HybridSearch(mindDb, embedder).search(spec.query, { limit: 8 });
recalled = hits.map(h => h.frame.content).join('\n---\n');
const hits = await memory.search(spec.query, { scopes: [scope], limit: 8 });
recalled = hits.map(h => h.text).join('\n---\n');
} catch (err) {
log.warn(`[loop] "${schedule.name}" recall failed: ${err instanceof Error ? err.message : String(err)}`);
}
@@ -291,17 +284,20 @@ export async function runLoopTick(deps: LoopTickDeps): Promise<LoopTickResult> {
log.warn(`[loop] "${schedule.name}" checker failed: ${err instanceof Error ? err.message : String(err)}`);
}
// Write the report into the local mind (the durable spine). Best-effort.
// Write the report into local memory (the durable spine). Best-effort.
let wrote = false;
if (spec.writeToMemory) {
try {
// FK: memory_frames.gop_id references sessions — ensure the 'loop' session
// exists before createIFrame (mirrors connector_fetch's 'harvest' ensure).
new SessionStore(mindDb).ensure('loop', 'loop', 'Loop automation outputs');
const header = `[Loop: ${schedule.name}] ${new Date().toISOString()}${score !== undefined ? ` · score ${score.toFixed(2)}` : ''}`;
// ISO timestamp in the body keeps each tick's frame distinct (defeats the
// content-hash dedup that would otherwise collapse identical reports).
new FrameStore(mindDb).createIFrame('loop', `${header}\n\n${report}`, 'normal', 'agent_inferred');
// ISO timestamp in the body keeps each tick's memory distinct (the store
// does not write identical text twice in one scope).
await memory.remember({
scope,
text: `${header}\n\n${report}`,
kind: 'note',
source: 'agent_inferred',
origin: 'loop',
});
wrote = true;
} catch (err) {
log.warn(`[loop] "${schedule.name}" memory write failed: ${err instanceof Error ? err.message : String(err)}`);