Files
waggle-os/packages/server/src/local/run-memory.ts
shumeiko.it 784ca770b1 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.
2026-10-24 11:56:26 +02:00

69 lines
2.3 KiB
TypeScript

/**
* Where agent-run results land in memory: a short index entry in personal
* memory and the full result in the workspace's memory (ADR-0007).
*/
import {
PERSONAL_SCOPE,
RESERVED_METADATA_KEYS,
workspaceScope,
type MemoryStore,
} from '@waggle/memory-contract';
import type { CollaborationRunMemoryRefs } from '@waggle/shared';
export interface RunResultMemoryInput {
workspaceId: string;
/** Short index entry kept in personal memory. */
personalText: string;
/** The full result, kept in the workspace's memory. */
workspaceText: string;
/** Run identifiers (runId, roomId…). Keys the contract reserves get a `run_` prefix. */
metadata: Record<string, unknown>;
}
function runMetadata(metadata: Record<string, unknown>): Record<string, unknown> {
const reserved: readonly string[] = RESERVED_METADATA_KEYS;
return Object.fromEntries(
Object.entries(metadata).map(([key, value]) => [reserved.includes(key) ? `run_${key}` : key, value]),
);
}
/** Writes both entries; a failed write is reflected in the returned status. */
export async function recordRunResult(
memory: MemoryStore,
input: RunResultMemoryInput,
): Promise<CollaborationRunMemoryRefs> {
const personalFrameIds: string[] = [];
const workspaceFrameIds: Record<string, string[]> = {};
const metadata = runMetadata(input.metadata);
try {
const [change] = await memory.remember({
scope: PERSONAL_SCOPE,
text: input.personalText,
kind: 'note',
source: 'agent_inferred',
origin: 'agent-runs',
metadata,
});
if (change) personalFrameIds.push(change.record.id);
} catch { /* workspace result remains authoritative */ }
try {
const [change] = await memory.remember({
scope: workspaceScope(input.workspaceId),
text: input.workspaceText,
kind: 'note',
source: 'agent_inferred',
origin: 'agent-runs',
metadata,
});
if (change) workspaceFrameIds[input.workspaceId] = [change.record.id];
} catch { /* reflected in the status below */ }
const personalOk = personalFrameIds.length > 0;
const workspaceOk = (workspaceFrameIds[input.workspaceId]?.length ?? 0) > 0;
return {
status: personalOk && workspaceOk ? 'complete' : personalOk || workspaceOk ? 'partial' : 'failed',
personalFrameIds,
workspaceFrameIds,
};
}