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

@@ -14,7 +14,8 @@
* Personal preferences, Response format, and Closed-world rewrite are never trimmed.
*/
import type { MemoryFrame, Importance } from '@waggle/core';
import type { MemoryImportance } from '@waggle/memory-contract';
import type { RecallFrame } from './recall-frame.js';
import type { AgentPersona } from './personas.js';
import type { ModelTier } from './model-tier.js';
import { detectTaskShape, type TaskShape, type TaskShapeType } from './task-shape.js';
@@ -24,8 +25,8 @@ import { logTurnEvent } from './turn-context.js';
// ── Types ────────────────────────────────────────────────────────────
export interface RecalledMemory {
workspace: MemoryFrame[];
personal: MemoryFrame[];
workspace: RecallFrame[];
personal: RecallFrame[];
/** False if the injection scanner blocked the recall — assembler must ignore. */
scanSafe: boolean;
/**
@@ -157,12 +158,11 @@ const FRAME_LIMITS: Record<ModelTier, number> = {
};
/** Numeric weight for frame ranking — higher = more important. */
const IMPORTANCE_WEIGHT: Record<Importance, number> = {
const IMPORTANCE_WEIGHT: Record<MemoryImportance, number> = {
critical: 4,
important: 3,
normal: 2,
temporary: 1,
deprecated: 0,
};
/**
@@ -294,9 +294,9 @@ const TRUNCATION_ORDER = ['Recent changes', 'Active work', 'State'] as const;
// ── Helpers ──────────────────────────────────────────────────────────
function selectFrames(frames: ReadonlyArray<MemoryFrame>, limit: number): MemoryFrame[] {
const seen = new Set<number>();
const deduped: MemoryFrame[] = [];
function selectFrames(frames: ReadonlyArray<RecallFrame>, limit: number): RecallFrame[] {
const seen = new Set<string>();
const deduped: RecallFrame[] = [];
for (const f of frames) {
if (!seen.has(f.id)) {
seen.add(f.id);
@@ -306,7 +306,7 @@ function selectFrames(frames: ReadonlyArray<MemoryFrame>, limit: number): Memory
deduped.sort((a, b) => {
const weightDelta = IMPORTANCE_WEIGHT[b.importance] - IMPORTANCE_WEIGHT[a.importance];
if (weightDelta !== 0) return weightDelta;
return b.id - a.id;
return b.created_at.localeCompare(a.created_at);
});
return deduped.slice(0, limit);
}
@@ -338,7 +338,7 @@ function renderPersona(persona: AgentPersona): string {
return lines.join('\n');
}
function renderFrames(frames: MemoryFrame[]): string {
function renderFrames(frames: RecallFrame[]): string {
// W4.1 (#1): date-prefixed — matches recallMemory's `[YYYY-MM-DD, importance]`
// line format so the temporal anchor survives the assembled path too.
return frames.map(f => `- [${f.created_at?.slice(0, 10) ?? 'unknown'}, ${f.importance}] ${f.content}`).join('\n');
@@ -431,7 +431,7 @@ export class PromptAssembler {
// ── Recalled memory — only when the upstream injection scan passed ──
// Brief §8: recallMemory already scans; assembler must not re-scan, and
// must ignore recall entirely when scanSafe is false.
const recalledFrames: MemoryFrame[] = [];
const recalledFrames: RecallFrame[] = [];
if (!closedWorldRewrite && input.recalled.scanSafe && input.recalled.renderedText) {
// W4.5: pre-rendered multi-lane block — carries its own header
// ('# Recalled Memories' + provenance + temporal guidance). Subject