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

@@ -1,22 +1,21 @@
import type {
MindDB,
IdentityLayer,
AwarenessLayer,
FrameStore,
SessionStore,
HybridSearch,
KnowledgeGraph,
Embedder,
Importance,
FrameSource,
} from '@waggle/core';
import type { CognifyPipeline } from './cognify.js';
import type { FeedbackHandler } from './feedback-handler.js';
import {
PERSONAL_SCOPE,
workspaceScope,
MEMORY_IMPORTANCE,
MEMORY_SOURCES,
type MemoryHit,
type MemoryImportance,
type MemoryKind,
type MemoryScope,
type MemorySource,
type MemoryStore,
} from '@waggle/memory-contract';
import type { IdentityLayer, AwarenessLayer } from '@waggle/core';
import type { RiskLevel } from '@waggle/shared';
import { createCoreLogger } from '@waggle/core';
import { detectContradiction } from './contradiction-detector.js';
import { scanForInjection } from './injection-scanner.js';
import { normalizeForDedup, cosineSimilarity, detectDramaticClaims, deriveConfidence, type ConfidenceLevel } from './text-analysis.js';
import { normalizeForDedup, detectDramaticClaims, deriveConfidence, type ConfidenceLevel } from './text-analysis.js';
export type { ConfidenceLevel } from './text-analysis.js';
export { formatCombinedResult } from './result-formatter.js';
@@ -43,16 +42,6 @@ export interface ToolDefinition {
checkAvailability?: () => boolean;
}
/** Workspace-specific layers for dual-mind routing */
interface WorkspaceLayers {
db: MindDB;
frames: FrameStore;
sessions: SessionStore;
search: HybridSearch;
knowledge: KnowledgeGraph;
cognify: CognifyPipeline;
}
/** Tracks tool utilization across the current session */
export interface ToolUtilizationTracker {
/** Record that a tool was used */
@@ -83,22 +72,15 @@ export function createToolUtilizationTracker(totalAvailable: number): ToolUtiliz
};
}
export interface MindToolDeps {
db: MindDB;
memory: MemoryStore;
identity: IdentityLayer;
awareness: AwarenessLayer;
frames: FrameStore;
sessions: SessionStore;
search: HybridSearch;
knowledge: KnowledgeGraph;
cognify?: CognifyPipeline;
feedback?: FeedbackHandler;
/** F16: Optional embedder for semantic dedup (cosine similarity) */
embedder?: Embedder;
/** Dynamic accessor for workspace layers — checked at call time */
getWorkspaceLayers?: () => WorkspaceLayers | null;
/** W5.7: Accessor for ALL workspace search instances (for cross-workspace search) */
getAllWorkspaceSearches?: () => Array<{ workspaceId: string; workspaceName: string; search: HybridSearch }>;
/** Active workspace, checked at call time; null when only personal memory is active. */
getWorkspaceId?: () => string | null;
/** W5.7: workspaces searched by search_all_workspaces. */
listWorkspaces?: () => Array<{ id: string; name: string }>;
/** Optional tool utilization tracker for session-level stats */
toolUtilizationTracker?: ToolUtilizationTracker;
/**
@@ -109,12 +91,28 @@ export interface MindToolDeps {
saveCounter?: { count: number };
}
/** Kinds the agent may choose when it saves a memory. */
const SAVE_KINDS: readonly MemoryKind[] = ['fact', 'preference', 'decision', 'event', 'note'];
/** Recent memories compared against a new one for contradictions. */
const CONTRADICTION_WINDOW = 50;
/** Search hits compared against a new memory for normalized duplicates. */
const DEDUP_CANDIDATES = 20;
function formatHit(hit: MemoryHit, index: number): string {
return `[${index + 1}] (score: ${hit.score.toFixed(3)}, date: ${hit.createdAt.slice(0, 10)}, kind: ${hit.kind}, importance: ${hit.importance}, source: ${hit.source})\n${hit.text}`;
}
export function createMindTools(deps: MindToolDeps): ToolDefinition[] {
// W2.10: Per-session memory save rate limiter (prevents memory flooding attacks)
// Review C2: use externally-owned counter when provided so limit survives
// tool-array reconstruction (persona switch, workspace change, MCP reconnect).
const MAX_SAVES_PER_SESSION = 50;
const saveCounter = deps.saveCounter ?? { count: 0 };
const activeWorkspace = (): MemoryScope | null => {
const id = deps.getWorkspaceId?.() ?? null;
return id ? workspaceScope(id) : null;
};
return [
{
@@ -160,15 +158,10 @@ export function createMindTools(deps: MindToolDeps): ToolDefinition[] {
type: 'object',
properties: {
query: { type: 'string', description: 'What to search for' },
profile: {
type: 'string',
enum: ['balanced', 'recent', 'important', 'connected'],
description: 'Scoring profile to use',
},
scope: {
type: 'string',
enum: ['all', 'personal', 'workspace'],
description: 'Which mind to search (default: all)',
description: 'Which memory to search (default: all)',
},
limit: { type: 'number', description: 'Max results to return' },
importance: {
@@ -178,57 +171,57 @@ export function createMindTools(deps: MindToolDeps): ToolDefinition[] {
},
since: {
type: 'string',
description: 'ISO date — only return frames created after this date (e.g., "2026-03-14")',
description: 'ISO date — only return memories created after this date (e.g., "2026-03-14")',
},
until: {
type: 'string',
description: 'ISO date — only return frames created before this date (e.g., "2026-03-21")',
description: 'ISO date — only return memories created before this date (e.g., "2026-03-21")',
},
},
required: ['query'],
},
execute: async (args) => {
const limit = (args.limit as number) ?? 10;
const limit = typeof args.limit === 'number' && args.limit > 0 ? args.limit : 10;
const scope = (args.scope as string) ?? 'all';
const query = args.query as string;
const profile = (args.profile as 'balanced') ?? 'balanced';
const query = String(args.query ?? '');
const importanceFilter = (args.importance as string) ?? 'all';
// F20: Temporal filtering params
const since = args.since as string | undefined;
const until = args.until as string | undefined;
const wsLayers = deps.getWorkspaceLayers?.();
const since = typeof args.since === 'string' ? args.since : undefined;
const until = typeof args.until === 'string' ? args.until : undefined;
const ws = activeWorkspace();
// F14: exact importance filter, applied after ranking
const byImportance = (hits: MemoryHit[]): MemoryHit[] =>
importanceFilter === 'all' ? hits : hits.filter(h => h.importance === importanceFilter);
const search = (target: MemoryScope, max: number): Promise<MemoryHit[]> =>
deps.memory.search(query, {
scopes: [target],
limit: importanceFilter === 'all' ? max : max * 5,
...(since && { since }),
...(until && { until: until.length === 10 ? `${until}T23:59:59.999Z` : until }),
});
const sections: string[] = [];
// F14: Helper to filter results by importance level
const filterByImportance = <T extends { frame: { importance: string } }>(results: T[]): T[] => {
if (importanceFilter === 'all') return results;
return results.filter(r => r.frame.importance === importanceFilter);
};
// W2.6: Search workspace mind FIRST (results shown before personal, boosted priority)
if (wsLayers && (scope === 'all' || scope === 'workspace')) {
const wsResults = filterByImportance(await wsLayers.search.search(query, { limit, profile, since, until }));
// W2.6: Search workspace memory FIRST (results shown before personal)
if (ws && (scope === 'all' || scope === 'workspace')) {
const wsResults = byImportance(await search(ws, limit)).slice(0, limit);
if (wsResults.length > 0) {
sections.push('## Workspace Memory');
sections.push(...wsResults.map((r, i) =>
`[${i + 1}] (score: ${r.finalScore.toFixed(3)}, date: ${r.frame.created_at?.slice(0, 10) ?? 'unknown'}, type: ${r.frame.frame_type}, importance: ${r.frame.importance}, source: ${r.frame.source ?? 'user_stated'})\n${r.frame.content}`
));
sections.push(...wsResults.map(formatHit));
}
}
// Search personal mind (shown after workspace results, with reduced limit when workspace is active)
// Search personal memory (with reduced limit when a workspace is active)
if (scope === 'all' || scope === 'personal') {
// W2.6: When inside a workspace, cap personal results to reduce noise
const personalLimit = (wsLayers && scope === 'all') ? Math.min(limit, 3) : limit;
const personalResults = filterByImportance(await deps.search.search(query, { limit: personalLimit, profile, since, until }));
const personalLimit = (ws && scope === 'all') ? Math.min(limit, 3) : limit;
const personalResults = byImportance(await search(PERSONAL_SCOPE, personalLimit)).slice(0, personalLimit);
if (personalResults.length > 0) {
if (wsLayers) {
if (ws) {
sections.push('## Personal Memory\n_(Cross-workspace — not specific to this workspace)_');
}
sections.push(...personalResults.map((r, i) =>
`[${i + 1}] (score: ${r.finalScore.toFixed(3)}, date: ${r.frame.created_at?.slice(0, 10) ?? 'unknown'}, type: ${r.frame.frame_type}, importance: ${r.frame.importance}, source: ${r.frame.source ?? 'user_stated'})\n${r.frame.content}`
));
sections.push(...personalResults.map(formatHit));
}
}
@@ -242,40 +235,11 @@ export function createMindTools(deps: MindToolDeps): ToolDefinition[] {
}
}
if (sections.length > 0) return sections.join('\n\n');
// Fallback: LIKE scan on both minds
const allFallback: string[] = [];
const keywords = query.toLowerCase().split(/\s+/).filter(w => w.length > 2);
for (const [label, db] of [['personal', deps.db], ...(wsLayers ? [['workspace', wsLayers.db] as const] : [])] as const) {
const raw = db.getDatabase();
type FallbackFrameRow = { id: number; content: string; frame_type: string; importance: string };
let fallbackFrames: FallbackFrameRow[];
if (keywords.length > 0) {
const likeClauses = keywords.map(() => "LOWER(content) LIKE '%' || ? || '%' ESCAPE '\\'").join(' OR ');
const likeParams = keywords.map(k => k.replace(/\\/g, '\\\\').replace(/%/g, '\\%').replace(/_/g, '\\_'));
fallbackFrames = raw.prepare(
`SELECT id, content, frame_type, importance FROM memory_frames WHERE ${likeClauses} ORDER BY id DESC LIMIT ?`
).all(...likeParams, limit) as FallbackFrameRow[];
} else {
fallbackFrames = raw.prepare(
'SELECT id, content, frame_type, importance FROM memory_frames ORDER BY id DESC LIMIT ?'
).all(limit) as FallbackFrameRow[];
}
if (fallbackFrames.length > 0) {
if (wsLayers) allFallback.push(`## ${label === 'workspace' ? 'Workspace' : 'Personal'} Memory`);
allFallback.push(...fallbackFrames.map((f, i) =>
`[${i + 1}] (type: ${f.frame_type}, importance: ${f.importance})\n${f.content}`
));
}
}
if (allFallback.length === 0) return 'No relevant memories found.';
return allFallback.join('\n\n');
if (sections.length === 0) return 'No relevant memories found.';
return sections.join('\n\n');
},
},
// W5.7: Cross-workspace search — searches ALL workspace minds + personal
// W5.7: Cross-workspace search — searches ALL workspaces + personal
{
name: 'search_all_workspaces',
description: 'Search across ALL workspaces and personal memory simultaneously. Use for cross-project queries like "what should I work on?" or "find everything about topic X across all projects".',
@@ -289,31 +253,26 @@ export function createMindTools(deps: MindToolDeps): ToolDefinition[] {
required: ['query'],
},
execute: async (args) => {
const query = args.query as string;
const limitPerWs = (args.limit as number) ?? 5;
const query = String(args.query ?? '');
const limitPerWs = typeof args.limit === 'number' && args.limit > 0 ? args.limit : 5;
const sections: string[] = [];
const short = (hit: MemoryHit, i: number): string =>
`[${i + 1}] (score: ${hit.score.toFixed(3)}, importance: ${hit.importance})\n${hit.text}`;
// Search personal mind
const personalResults = await deps.search.search(query, { limit: limitPerWs, profile: 'balanced' });
const personalResults = await deps.memory.search(query, { scopes: [PERSONAL_SCOPE], limit: limitPerWs });
if (personalResults.length > 0) {
sections.push('## Personal Memory');
sections.push(...personalResults.map((r, i) =>
`[${i + 1}] (score: ${r.finalScore.toFixed(3)}, importance: ${r.frame.importance})\n${r.frame.content}`
));
sections.push(...personalResults.map(short));
}
// Search all workspace minds
const allWs = deps.getAllWorkspaceSearches?.() ?? [];
for (const { workspaceId, workspaceName, search: wsSearch } of allWs) {
for (const { id, name } of deps.listWorkspaces?.() ?? []) {
try {
const wsResults = await wsSearch.search(query, { limit: limitPerWs, profile: 'balanced' });
const wsResults = await deps.memory.search(query, { scopes: [workspaceScope(id)], limit: limitPerWs });
if (wsResults.length > 0) {
sections.push(`## Workspace: ${workspaceName} (${workspaceId})`);
sections.push(...wsResults.map((r, i) =>
`[${i + 1}] (score: ${r.finalScore.toFixed(3)}, importance: ${r.frame.importance})\n${r.frame.content}`
));
sections.push(`## Workspace: ${name} (${id})`);
sections.push(...wsResults.map(short));
}
} catch (err) { log.warn(`search_all_workspaces: skipped workspace ${workspaceId}`, err); }
} catch (err) { log.warn(`search_all_workspaces: skipped workspace ${id}`, err); }
}
if (sections.length === 0) return 'No relevant memories found across any workspace.';
@@ -322,18 +281,23 @@ export function createMindTools(deps: MindToolDeps): ToolDefinition[] {
},
{
name: 'save_memory',
description: 'Save a new memory. Defaults to workspace mind when active.\n\n'
description: 'Save a new memory. Defaults to workspace memory when a workspace is active.\n\n'
+ 'CRITICAL ROUTING RULES:\n'
+ '- target="workspace" (DEFAULT): ALL project data, client data, decisions, tasks, meeting notes, research findings, confidential information, names, budgets, timelines. '
+ 'Workspace memories are ISOLATED — they cannot be seen from other workspaces. Use this for ANYTHING workspace-specific.\n'
+ '- target="personal": ONLY for user preferences (favorite color, communication style), personal facts (name, role, timezone), '
+ 'and cross-workspace knowledge (general skills, tool preferences). Personal memories are shared across ALL workspaces.\n\n'
+ 'When in doubt, use workspace. Never save client names, budgets, confidential data, or project-specific information to personal mind.',
+ 'When in doubt, use workspace. Never save client names, budgets, confidential data, or project-specific information to personal memory.',
offlineCapable: true,
parameters: {
type: 'object',
properties: {
content: { type: 'string', description: 'The memory content to save' },
kind: {
type: 'string',
enum: [...SAVE_KINDS],
description: 'What the memory is: fact (default), preference, decision, event (something that happened, with its date in the text), note.',
},
importance: {
type: 'string',
enum: ['critical', 'important', 'normal', 'temporary'],
@@ -342,7 +306,7 @@ export function createMindTools(deps: MindToolDeps): ToolDefinition[] {
target: {
type: 'string',
enum: ['workspace', 'personal'],
description: 'Which mind to save to. Workspace (default): ALL project/client/confidential data — isolated per workspace. Personal: ONLY user preferences and cross-workspace knowledge — shared across all workspaces.',
description: 'Which memory to save to. Workspace (default): ALL project/client/confidential data — isolated per workspace. Personal: ONLY user preferences and cross-workspace knowledge — shared across all workspaces.',
},
source: {
type: 'string',
@@ -358,11 +322,11 @@ export function createMindTools(deps: MindToolDeps): ToolDefinition[] {
required: ['content'],
},
execute: async (args) => {
const content = args.content as string;
const content = typeof args.content === 'string' ? args.content.trim() : '';
if (!content) return 'Memory save skipped: content is empty.';
// Review C1: scan LLM-controlled content for prompt-injection payloads
// BEFORE any DB write. The pre:memory-write hook is a cancellation gate,
// not a scanner — if no hook is registered, content goes straight to SQLite.
// BEFORE any write.
const scan = scanForInjection(content, 'user_input');
if (!scan.safe) {
return `Memory save blocked: content contains suspected prompt-injection payload (${scan.flags.join(', ')}). The content was NOT saved.`;
@@ -376,30 +340,30 @@ export function createMindTools(deps: MindToolDeps): ToolDefinition[] {
}
// Untrusted args: validate against the canonical unions, default on mismatch.
const VALID_IMPORTANCE: readonly Importance[] = ['critical', 'important', 'normal', 'temporary', 'deprecated'];
const VALID_SOURCE: readonly FrameSource[] = ['user_stated', 'tool_verified', 'agent_inferred', 'import', 'system'];
let importance: Importance =
typeof args.importance === 'string' && (VALID_IMPORTANCE as readonly string[]).includes(args.importance)
? (args.importance as Importance)
let importance: MemoryImportance =
typeof args.importance === 'string' && (MEMORY_IMPORTANCE as readonly string[]).includes(args.importance)
? (args.importance as MemoryImportance)
: 'normal';
const kind: MemoryKind =
typeof args.kind === 'string' && (SAVE_KINDS as readonly string[]).includes(args.kind)
? (args.kind as MemoryKind)
: 'fact';
const target = typeof args.target === 'string' ? args.target : 'workspace';
const source: FrameSource =
typeof args.source === 'string' && (VALID_SOURCE as readonly string[]).includes(args.source)
? (args.source as FrameSource)
const source: MemorySource =
typeof args.source === 'string' && (MEMORY_SOURCES as readonly string[]).includes(args.source)
? (args.source as MemorySource)
: 'user_stated';
const wsLayers = deps.getWorkspaceLayers?.();
const ws = activeWorkspace();
// F6: Derive confidence from source if not explicitly provided
const confidence: ConfidenceLevel = (args.confidence as ConfidenceLevel) ?? deriveConfidence(source);
// F7: Strict workspace routing — respect target parameter, with safety guardrail
// When target='workspace' AND workspace is active → save to workspace only
// When target='personal' → save to personal, UNLESS content appears workspace-specific
let effectiveTarget = target;
// B1-guardrail: Detect workspace-specific content being routed to personal mind
// If workspace is active and content contains confidential/project signals, redirect to workspace
if (effectiveTarget === 'personal' && wsLayers != null) {
// B1-guardrail: Detect workspace-specific content being routed to personal memory
// If a workspace is active and content contains confidential/project signals, redirect to workspace
if (effectiveTarget === 'personal' && ws != null) {
const workspaceSpecificPatterns = [
/\bconfidential\b/i, /\bclient\b/i, /\bbudget\b/i, /\bengagement\b/i,
/\bdeliverable\b/i, /\bmilestone\b/i, /\btimeline\b/i, /\bdeadline\b/i,
@@ -408,17 +372,14 @@ export function createMindTools(deps: MindToolDeps): ToolDefinition[] {
/\bproject\b.*\b(?:plan|status|update)\b/i,
/\bcontract\b/i, /\bproposal\b/i, /\bnda\b/i, /\bsow\b/i,
];
const hasWorkspaceSignal = workspaceSpecificPatterns.some(p => p.test(content));
if (hasWorkspaceSignal) {
if (workspaceSpecificPatterns.some(p => p.test(content))) {
effectiveTarget = 'workspace';
}
}
const useWorkspace = effectiveTarget === 'workspace' && wsLayers != null;
const targetFrames = useWorkspace ? wsLayers!.frames : deps.frames;
const targetSessions = useWorkspace ? wsLayers!.sessions : deps.sessions;
const targetCognify = useWorkspace ? wsLayers!.cognify : deps.cognify;
const targetDb = useWorkspace ? wsLayers!.db : deps.db;
const useWorkspace = effectiveTarget === 'workspace' && ws != null;
const targetScope = useWorkspace ? ws! : PERSONAL_SCOPE;
const otherScope = useWorkspace ? PERSONAL_SCOPE : ws;
const mindLabel = useWorkspace ? 'workspace' : 'personal';
// F22: Detect dramatic claims and flag them
@@ -432,166 +393,57 @@ export function createMindTools(deps: MindToolDeps): ToolDefinition[] {
dramaticFlag = ` [flag: dramatic_claim (${dramaticClaims.join(', ')})]`;
}
// ── Dedup checks (F7 cross-mind + F16 enhanced) ──────────────
// Helper: check a single database for exact or normalized duplicates
const checkDedupInDb = async (db: MindDB, label: string): Promise<string | null> => {
// ── Dedup (F7 cross-memory + F16 normalized) ──────────────
// Identical text in the target scope is caught by the store itself;
// this also catches normalized matches and copies in the other scope.
const normalized = normalizeForDedup(content);
const findDuplicate = async (scope: MemoryScope): Promise<string | null> => {
if (!normalized) return null;
try {
const raw = db.getDatabase();
// Exact-match dedup (original W2.4 check)
const exactMatch = raw.prepare(
"SELECT id, content FROM memory_frames WHERE content = ? LIMIT 1"
).get(content) as { id: number; content: string } | undefined;
if (exactMatch) {
return `Memory already exists in ${label} mind (frame ${exactMatch.id}) — skipped duplicate save.`;
}
// F16: Normalized string comparison
const normalizedContent = normalizeForDedup(content);
if (normalizedContent.length > 0) {
// Fetch recent frames for normalized comparison (cap at 100 to avoid perf issues)
const recentFrames = raw.prepare(
"SELECT id, content FROM memory_frames ORDER BY id DESC LIMIT 100"
).all() as Array<{ id: number; content: string }>;
for (const frame of recentFrames) {
if (normalizeForDedup(frame.content) === normalizedContent) {
return `Memory already exists in ${label} mind (frame ${frame.id}, normalized match) — skipped duplicate save.`;
}
}
}
// F16: Embedding-based similarity dedup (if embedder available)
if (deps.embedder && normalizedContent.length > 0) {
try {
const candidateFrames = raw.prepare(
"SELECT id, content FROM memory_frames ORDER BY id DESC LIMIT 50"
).all() as Array<{ id: number; content: string }>;
if (candidateFrames.length > 0) {
const newEmbedding = await deps.embedder.embed(content);
const candidateTexts = candidateFrames.map(f => f.content);
const candidateEmbeddings = await deps.embedder.embedBatch(candidateTexts);
for (let i = 0; i < candidateEmbeddings.length; i++) {
const similarity = cosineSimilarity(newEmbedding, candidateEmbeddings[i]);
if (similarity > 0.95) {
return `Memory already exists in ${label} mind (frame ${candidateFrames[i].id}, semantic similarity: ${similarity.toFixed(3)}) — skipped duplicate save.`;
}
}
}
} catch { /* non-blocking — continue if embedding dedup fails */ }
}
} catch { /* non-blocking — proceed with save if dedup check fails */ }
return null;
};
// F7: Cross-mind dedup — check BOTH workspace and personal mind before saving
const targetDedupResult = await checkDedupInDb(targetDb, mindLabel);
if (targetDedupResult) return targetDedupResult;
// Check the OTHER mind for cross-mind duplicates
if (useWorkspace) {
// Saving to workspace — also check personal mind
const crossDedupResult = await checkDedupInDb(deps.db, 'personal');
if (crossDedupResult) {
return crossDedupResult.replace('skipped duplicate save', 'skipped cross-mind duplicate save');
const candidates = await deps.memory.search(content, { scopes: [scope], limit: DEDUP_CANDIDATES });
const match = candidates.find(c => normalizeForDedup(c.text) === normalized);
return match ? match.id : null;
} catch {
return null; // non-blocking — proceed with save if dedup check fails
}
} else if (wsLayers) {
// Saving to personal — also check workspace mind
const crossDedupResult = await checkDedupInDb(wsLayers.db, 'workspace');
if (crossDedupResult) {
return crossDedupResult.replace('skipped duplicate save', 'skipped cross-mind duplicate save');
};
const sameScopeDuplicate = await findDuplicate(targetScope);
if (sameScopeDuplicate) {
return `Memory already exists in ${mindLabel} memory (${sameScopeDuplicate}) — skipped duplicate save.`;
}
if (otherScope) {
const crossDuplicate = await findDuplicate(otherScope);
if (crossDuplicate) {
const otherLabel = useWorkspace ? 'personal' : 'workspace';
return `Memory already exists in ${otherLabel} memory (${crossDuplicate}) — skipped cross-memory duplicate save.`;
}
}
// Skills 2.0 gap K: write-path conflict detection.
// Check recent frames for contradictions BEFORE saving. When found,
// still save the new frame (conflicts are data, not errors) but
// emit a correction improvement signal and flag the response so
// the agent surfaces the conflict to the user.
// Skills 2.0 gap K: write-path conflict detection. Conflicts are data,
// not errors: the memory is still saved, and the response is flagged
// so the agent surfaces the conflict to the user.
let conflictFlag = '';
try {
const raw = targetDb.getDatabase();
const recentRows = raw.prepare(
"SELECT content FROM memory_frames ORDER BY id DESC LIMIT 50",
).all() as Array<{ content: string }>;
const contradiction = detectContradiction(content, recentRows);
const recent = await deps.memory.list({ scopes: [targetScope], limit: CONTRADICTION_WINDOW });
const contradiction = detectContradiction(content, recent.map(r => ({ content: r.text })));
if (contradiction.isContradiction && contradiction.conflictsWith) {
conflictFlag = ` [flag: contradicts_existing (${contradiction.conflictsWith.slice(0, 80).replace(/\s+/g, ' ').trim()}…)]`;
}
} catch { /* non-blocking — contradiction check failure proceeds with save */ }
// Use cognify pipeline if available (extracts entities + indexes for search)
if (targetCognify) {
// PR3.5 (review H-1): pass the agent's chosen source through cognify —
// dropping it stored every cognify-path memory as 'user_stated' while
// the response string still claimed the real source (a provenance lie).
const result = await targetCognify.cognify(content, importance, undefined, undefined, source);
return `Memory saved to ${mindLabel} mind (importance: ${importance}, source: ${source}, confidence: ${confidence}${dramaticFlag}${conflictFlag}, entities: ${result.entitiesExtracted}, relations: ${result.relationsCreated}).`;
const [change] = await deps.memory.remember({
scope: targetScope,
text: content,
kind,
importance,
source,
origin: 'agent',
metadata: { confidence },
});
if (change?.event === 'unchanged') {
return `Memory already exists in ${mindLabel} memory (${change.record.id}) — skipped duplicate save.`;
}
// Fallback: raw frame creation (no entity extraction or vector indexing)
const active = targetSessions.getActive();
let gopId: string;
if (active.length === 0) {
const session = targetSessions.create();
gopId = session.gop_id;
} else {
gopId = active[0].gop_id;
}
const latestI = targetFrames.getLatestIFrame(gopId);
if (latestI) {
targetFrames.createPFrame(gopId, content, latestI.id, importance, source);
} else {
targetFrames.createIFrame(gopId, content, importance, source);
}
return `Memory saved to ${mindLabel} mind (importance: ${importance}, source: ${source}, confidence: ${confidence}${dramaticFlag}${conflictFlag}).`;
},
},
{
name: 'query_knowledge',
description: 'Query the knowledge graph for entities and their relationships. Use query="*" to list all known entities.',
offlineCapable: true,
parameters: {
type: 'object',
properties: {
query: { type: 'string', description: 'Entity name to search for, or "*" to list all entities' },
type: { type: 'string', description: 'Filter by entity type (e.g., person, project, technology, concept, organization)' },
},
required: ['query'],
},
execute: async (args) => {
const query = args.query as string;
const entityType = args.type as string | undefined;
let entities;
// F5: Wildcard support — list all entities
if (query === '*' || query.toLowerCase() === 'all') {
entities = deps.knowledge.getEntitiesByType(entityType ?? '');
} else if (entityType) {
entities = deps.knowledge.getEntitiesByType(entityType);
entities = entities.filter(e => e.name.toLowerCase().includes(query.toLowerCase()));
} else {
entities = deps.knowledge.searchEntities(query);
}
if (entities.length === 0) return `No entities found matching "${query}".`;
const maxEntities = (query === '*' || query.toLowerCase() === 'all') ? 50 : 10;
const results: string[] = [];
for (const entity of entities.slice(0, maxEntities)) {
const rels = deps.knowledge.getRelationsFrom(entity.id);
const relStr = rels.length > 0
? rels.map(r => {
const target = deps.knowledge.getEntity(r.target_id);
return ` → ${r.relation_type} → ${target?.name ?? 'unknown'} (confidence: ${r.confidence})`;
}).join('\n')
: ' (no outgoing relations)';
results.push(`${entity.entity_type}: ${entity.name}\n${relStr}`);
}
return results.join('\n\n');
return `Memory saved to ${mindLabel} memory (kind: ${kind}, importance: ${importance}, source: ${source}, confidence: ${confidence}${dramaticFlag}${conflictFlag}).`;
},
},
{
@@ -612,46 +464,5 @@ export function createMindTools(deps: MindToolDeps): ToolDefinition[] {
return `Task added: "${item.content}" (priority: ${item.priority})`;
},
},
{
name: 'correct_knowledge',
description: 'Correct or invalidate a knowledge entity. Use entity_id from query_knowledge results.',
parameters: {
type: 'object',
properties: {
entity_id: { type: 'number', description: 'The entity ID to update' },
action: {
type: 'string',
enum: ['correct', 'invalidate'],
description: 'Whether to correct or invalidate the entity',
},
updates: {
type: 'object',
description: 'For correct: property updates to merge. For invalidate: { reason: "why it is wrong" }',
},
},
required: ['entity_id', 'action'],
},
execute: async (args) => {
if (!deps.feedback) {
return 'Feedback handler not available. Cannot correct knowledge.';
}
const entityId = args.entity_id as number;
const action = args.action as string;
const updates = (args.updates as Record<string, unknown>) ?? {};
if (action === 'correct') {
deps.feedback.correctEntity(entityId, updates);
return `Entity ${entityId} corrected with updates: ${JSON.stringify(updates)}`;
} else if (action === 'invalidate') {
const reason = (updates.reason as string) ?? 'No reason provided';
deps.feedback.invalidateEntity(entityId, reason);
return `Entity ${entityId} invalidated. Reason: ${reason}`;
}
return `Unknown action "${action}". Use "correct" or "invalidate".`;
},
},
];
}