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:
@@ -246,9 +246,7 @@ Tools:
|
||||
- save_memory: Save important facts. Defaults to WORKSPACE mind. Use target="personal" for: user preferences, communication style, corrections about YOU, cross-workspace knowledge.
|
||||
- get_identity: Who you are (always from personal mind).
|
||||
- get_awareness: Current tasks, active items, flags.
|
||||
- query_knowledge: Query your knowledge graph for entities and relationships.
|
||||
- add_task: Track a task in your awareness layer.
|
||||
- correct_knowledge: Fix or invalidate a knowledge entity.
|
||||
|
||||
**Save routing rules:**
|
||||
- Project decisions, meeting notes, task outcomes → workspace mind
|
||||
|
||||
@@ -1,262 +0,0 @@
|
||||
import type {
|
||||
FrameStore,
|
||||
SessionStore,
|
||||
KnowledgeGraph,
|
||||
HybridSearch,
|
||||
Importance,
|
||||
FrameSource,
|
||||
} from '@waggle/core';
|
||||
import { createCoreLogger } from '@waggle/core';
|
||||
import { extractEntities, extractRelations, type ExtractedEntity } from './entity-extractor.js';
|
||||
import { MemoryLinker, type MemoryLink } from './memory-linker.js';
|
||||
import { logTurnEvent } from './turn-context.js';
|
||||
|
||||
const log = createCoreLogger('cognify');
|
||||
|
||||
export interface CognifyConfig {
|
||||
frames: FrameStore;
|
||||
sessions: SessionStore;
|
||||
knowledge: KnowledgeGraph;
|
||||
search: HybridSearch;
|
||||
enableLinking?: boolean;
|
||||
}
|
||||
|
||||
export interface CognifyResult {
|
||||
frameId: number;
|
||||
entitiesExtracted: number;
|
||||
relationsCreated: number;
|
||||
relatedFrames?: MemoryLink[];
|
||||
}
|
||||
|
||||
export class CognifyPipeline {
|
||||
private frames: FrameStore;
|
||||
private sessions: SessionStore;
|
||||
private knowledge: KnowledgeGraph;
|
||||
private search: HybridSearch;
|
||||
private linker?: MemoryLinker;
|
||||
|
||||
constructor(config: CognifyConfig) {
|
||||
this.frames = config.frames;
|
||||
this.sessions = config.sessions;
|
||||
this.knowledge = config.knowledge;
|
||||
this.search = config.search;
|
||||
if (config.enableLinking) {
|
||||
this.linker = new MemoryLinker({ search: config.search });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Full cognify pipeline: save frame -> extract entities -> enrich graph -> index for search.
|
||||
* H-AUDIT-1: optional turnId threads trace propagation through entity + relation extraction.
|
||||
*/
|
||||
async cognify(
|
||||
content: string,
|
||||
importance: Importance = 'normal',
|
||||
gopId?: string,
|
||||
turnId?: string,
|
||||
/** Provenance class for the written frame (PR3.5 honesty: agent-extracted
|
||||
* memories must NOT inherit the schema default 'user_stated'). Omit to let
|
||||
* FrameStore's default apply (back-compat for callers that don't classify). */
|
||||
source?: FrameSource,
|
||||
): Promise<CognifyResult> {
|
||||
logTurnEvent(turnId, { stage: 'cognify.enter', contentChars: content.length, importance, gopId });
|
||||
// 1. Ensure a session exists
|
||||
const resolvedGopId = gopId ?? this.ensureSession();
|
||||
|
||||
// 2. Save a frame (I-frame if none exists, P-frame otherwise)
|
||||
const latestI = this.frames.getLatestIFrame(resolvedGopId);
|
||||
const frame = latestI
|
||||
? this.frames.createPFrame(resolvedGopId, content, latestI.id, importance, source)
|
||||
: this.frames.createIFrame(resolvedGopId, content, importance, source);
|
||||
|
||||
// 3. Extract entities from content (guard against very long content)
|
||||
const maxContentLength = 10_000;
|
||||
const trimmedContent = content.slice(0, maxContentLength);
|
||||
const extracted = extractEntities(trimmedContent);
|
||||
|
||||
// 4. Upsert entities into KnowledgeGraph
|
||||
const entityIds = this.upsertEntities(extracted, frame.id);
|
||||
|
||||
// 5. Create co-occurrence relations between entities found in same text
|
||||
let relationsCreated = this.createCoOccurrenceRelations(entityIds);
|
||||
|
||||
// 5b. Extract semantic relations (led_by, reports_to, depends_on, etc.)
|
||||
relationsCreated += this.createSemanticRelations(content, extracted);
|
||||
|
||||
// 6. Index the frame for vector search
|
||||
await this.search.indexFrame(frame.id, content);
|
||||
|
||||
// 7. Find related frames if linking is enabled
|
||||
let relatedFrames: MemoryLink[] | undefined;
|
||||
if (this.linker) {
|
||||
relatedFrames = await this.linker.findRelated(content);
|
||||
relatedFrames = relatedFrames.filter(r => r.frameId !== frame.id);
|
||||
}
|
||||
|
||||
const result = {
|
||||
frameId: frame.id,
|
||||
entitiesExtracted: entityIds.length,
|
||||
relationsCreated,
|
||||
relatedFrames,
|
||||
};
|
||||
logTurnEvent(turnId, { stage: 'cognify.exit', frameId: frame.id, entitiesExtracted: entityIds.length, relationsCreated });
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cognify an existing frame — extract entities, build relations, re-index.
|
||||
* Used for post-harvest processing of imported frames.
|
||||
* H-AUDIT-1: optional turnId for trace propagation.
|
||||
*/
|
||||
async cognifyFrame(frameId: number, turnId?: string): Promise<CognifyResult | null> {
|
||||
logTurnEvent(turnId, { stage: 'cognify.frame.enter', frameId });
|
||||
const frame = this.frames.getById(frameId);
|
||||
if (!frame) return null;
|
||||
|
||||
const maxContentLength = 10_000;
|
||||
const content = frame.content.slice(0, maxContentLength);
|
||||
const extracted = extractEntities(content);
|
||||
const entityIds = this.upsertEntities(extracted, frame.id);
|
||||
let relationsCreated = this.createCoOccurrenceRelations(entityIds);
|
||||
|
||||
relationsCreated += this.createSemanticRelations(content, extracted);
|
||||
|
||||
// Re-index for search
|
||||
try {
|
||||
await this.search.indexFrame(frame.id, frame.content);
|
||||
} catch (err) {
|
||||
log.warn(`indexFrame failed for frame ${frame.id}`, err);
|
||||
}
|
||||
|
||||
const frameResult = {
|
||||
frameId: frame.id,
|
||||
entitiesExtracted: entityIds.length,
|
||||
relationsCreated,
|
||||
};
|
||||
logTurnEvent(turnId, { stage: 'cognify.frame.exit', frameId: frame.id, entitiesExtracted: entityIds.length, relationsCreated });
|
||||
return frameResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cognify a batch of existing frames. Returns summary stats.
|
||||
* H-AUDIT-1: optional turnId propagates into per-frame cognify calls.
|
||||
*/
|
||||
async cognifyBatch(frameIds: number[], turnId?: string): Promise<{ processed: number; entities: number; relations: number }> {
|
||||
logTurnEvent(turnId, { stage: 'cognify.batch.enter', frameCount: frameIds.length });
|
||||
let processed = 0;
|
||||
let entities = 0;
|
||||
let relations = 0;
|
||||
// Sequential: each frame's cognify may produce entities used by the next frame's relation linking
|
||||
for (const id of frameIds) {
|
||||
const result = await this.cognifyFrame(id, turnId);
|
||||
if (result) {
|
||||
processed++;
|
||||
entities += result.entitiesExtracted;
|
||||
relations += result.relationsCreated;
|
||||
}
|
||||
}
|
||||
logTurnEvent(turnId, { stage: 'cognify.batch.exit', processed, entities, relations });
|
||||
return { processed, entities, relations };
|
||||
}
|
||||
|
||||
private ensureSession(): string {
|
||||
// Review (cognify Major #1): use the transaction-wrapped SessionStore.ensureActive()
|
||||
// method. The previous getActive() + create() sequence was racy — two concurrent
|
||||
// callers on a fresh mind both saw no active session and both created one, splitting
|
||||
// frames across twin sessions. Same fix pattern as autoSaveFromExchange (commit b8ffe8e).
|
||||
return this.sessions.ensureActive().gop_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Upsert entities: if an entity with the same type+name exists, skip it;
|
||||
* otherwise create it. Returns the entity IDs (existing or new).
|
||||
*/
|
||||
private upsertEntities(extracted: ExtractedEntity[], frameId?: number): number[] {
|
||||
const ids: number[] = [];
|
||||
// Pre-fetch entities by type to avoid N queries in the loop
|
||||
const typeCache = new Map<string, { id: number; name: string }[]>();
|
||||
const types = new Set(extracted.map(e => e.type));
|
||||
for (const type of types) {
|
||||
typeCache.set(type, this.knowledge.getEntitiesByType(type).map(ent => ({
|
||||
id: ent.id,
|
||||
name: ent.name.toLowerCase(),
|
||||
})));
|
||||
}
|
||||
|
||||
for (const e of extracted) {
|
||||
const cached = typeCache.get(e.type) ?? [];
|
||||
const nameLower = e.name.toLowerCase();
|
||||
const existing = cached.find(ent => ent.name === nameLower);
|
||||
if (existing) {
|
||||
ids.push(existing.id);
|
||||
} else {
|
||||
const created = this.knowledge.createEntity(e.type, e.name, {
|
||||
confidence: e.confidence,
|
||||
source: 'cognify',
|
||||
});
|
||||
ids.push(created.id);
|
||||
// Add to cache so subsequent dupes in this batch are caught
|
||||
cached.push({ id: created.id, name: nameLower });
|
||||
}
|
||||
}
|
||||
// Link every extracted entity to its frame (kg_entity_frames bridge — contextual scoring).
|
||||
if (frameId !== undefined) {
|
||||
for (const id of ids) this.knowledge.linkEntityToFrame(id, frameId);
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create co-occurrence relations between all pairs of entities found
|
||||
* in the same text. Uses "co_occurs_with" relation type.
|
||||
* Returns the number of new relations created.
|
||||
*/
|
||||
private createCoOccurrenceRelations(entityIds: number[]): number {
|
||||
let count = 0;
|
||||
for (let i = 0; i < entityIds.length; i++) {
|
||||
for (let j = i + 1; j < entityIds.length; j++) {
|
||||
const sourceId = entityIds[i];
|
||||
const targetId = entityIds[j];
|
||||
|
||||
// Check if relation already exists
|
||||
const existingRels = this.knowledge.getRelationsFrom(sourceId, 'co_occurs_with');
|
||||
const alreadyExists = existingRels.some(r => r.target_id === targetId);
|
||||
if (!alreadyExists) {
|
||||
this.knowledge.createRelation(sourceId, targetId, 'co_occurs_with', 0.8, {
|
||||
source: 'cognify',
|
||||
});
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract semantic relations (led_by, reports_to, depends_on, etc.)
|
||||
* from content and upsert them into the knowledge graph.
|
||||
* Returns the number of new relations created.
|
||||
*/
|
||||
private createSemanticRelations(content: string, extracted: ExtractedEntity[]): number {
|
||||
let count = 0;
|
||||
const relations = extractRelations(content, extracted);
|
||||
for (const rel of relations) {
|
||||
try {
|
||||
const srcEntity = this.knowledge.searchEntities(rel.source, 5)
|
||||
.find(e => e.name.toLowerCase() === rel.source.toLowerCase());
|
||||
const tgtEntity = this.knowledge.searchEntities(rel.target, 5)
|
||||
.find(e => e.name.toLowerCase() === rel.target.toLowerCase());
|
||||
if (srcEntity && tgtEntity) {
|
||||
const existing = this.knowledge.getRelationsFrom(srcEntity.id, rel.relationType);
|
||||
if (!existing.some(r => r.target_id === tgtEntity.id)) {
|
||||
this.knowledge.createRelation(srcEntity.id, tgtEntity.id, rel.relationType, rel.confidence, { source: 'semantic' });
|
||||
count++;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
log.warn('semantic relation extraction failed', err);
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,9 @@
|
||||
/**
|
||||
* Recent-context loaders for the system prompt + PromptAssembler.
|
||||
*
|
||||
* Extracted from orchestrator.ts (PR-F, 2026-05-27) — was ~165L of mixed
|
||||
* SQL + formatting + injection-scanning in the Orchestrator class body.
|
||||
* Lifting these as free functions over the mind layers narrows the
|
||||
* Orchestrator's surface and makes the two views (string for direct
|
||||
* prompt inclusion, typed for assembler) easier to keep aligned.
|
||||
* Extracted from orchestrator.ts (PR-F, 2026-05-27). Lifting these as free
|
||||
* functions over memory narrows the Orchestrator's surface and keeps the two
|
||||
* views (string for direct prompt inclusion, typed for assembler) aligned.
|
||||
*
|
||||
* Two views, same backing data:
|
||||
* - `loadRecentContext`: pre-formatted markdown string for the system
|
||||
@@ -13,30 +11,28 @@
|
||||
* - `loadRecentContextFrames`: typed `ContextFrames` for the
|
||||
* PromptAssembler layer (injection scan is the assembler's job)
|
||||
*
|
||||
* Personal preferences always come from personal mind (cross-workspace
|
||||
* continuity); other queries route to workspace mind when active.
|
||||
* Personal preferences always come from personal memory (cross-workspace
|
||||
* continuity); everything else comes from the workspace when one is active.
|
||||
*/
|
||||
|
||||
import {
|
||||
type MindDB,
|
||||
type MemoryFrame,
|
||||
type AwarenessLayer,
|
||||
createCoreLogger,
|
||||
} from '@waggle/core';
|
||||
PERSONAL_SCOPE,
|
||||
workspaceScope,
|
||||
type MemoryRecord,
|
||||
type MemoryScope,
|
||||
type MemoryStore,
|
||||
} from '@waggle/memory-contract';
|
||||
import { type AwarenessLayer, createCoreLogger } from '@waggle/core';
|
||||
import { scanForInjection } from './injection-scanner.js';
|
||||
import { CONTEXT_PREVIEW_LENGTH } from './content-constants.js';
|
||||
import { toRecallFrame, type RecallFrame } from './recall-frame.js';
|
||||
|
||||
const logger = createCoreLogger('context-loader');
|
||||
|
||||
/**
|
||||
* Mind layers the context loaders read from. Workspace is optional —
|
||||
* when null, personal mind is used for both frames and preferences.
|
||||
*/
|
||||
export interface ContextLoaderDeps {
|
||||
/** Personal mind DB — queried for personal preferences regardless of workspace */
|
||||
personalDb: MindDB;
|
||||
/** Workspace mind DB if active (else null) */
|
||||
workspaceDb: MindDB | null;
|
||||
memory: MemoryStore;
|
||||
/** Active workspace, or null for personal memory only. */
|
||||
workspaceId: string | null;
|
||||
/** Awareness layer (always personal) */
|
||||
awareness: AwarenessLayer;
|
||||
}
|
||||
@@ -45,60 +41,59 @@ export interface ContextLoaderDeps {
|
||||
* Typed snapshot for `PromptAssembler`. Caller is responsible for
|
||||
* injection-scanning before composing into a prompt.
|
||||
*
|
||||
* `stateFrames`: I-frames (identity/state snapshots).
|
||||
* `recentChanges`: P-frames (deltas) + B-frames (background notes).
|
||||
* `stateFrames`: critical and important memories.
|
||||
* `recentChanges`: the most recent other memories.
|
||||
* `activeWork`: structured awareness items (tasks, actions, pending, flags).
|
||||
* `keyEntities`: most-connected KG entities (workspace when active).
|
||||
* `personalPreferences`: cross-workspace preference/correction frames.
|
||||
* `keyEntities`: knowledge-graph entities; empty while the graph is not part
|
||||
* of memory (ADR-0002).
|
||||
* `personalPreferences`: cross-workspace preferences.
|
||||
*/
|
||||
export interface ContextFrames {
|
||||
stateFrames: MemoryFrame[];
|
||||
recentChanges: MemoryFrame[];
|
||||
stateFrames: RecallFrame[];
|
||||
recentChanges: RecallFrame[];
|
||||
activeWork: Array<{ category: string; content: string; priority: number }>;
|
||||
keyEntities: Array<{ name: string; type: string }>;
|
||||
personalPreferences: string[];
|
||||
}
|
||||
|
||||
/** Compact row shape returned by `fetchRecentFrames` */
|
||||
export interface RecentFrameRow {
|
||||
id: number;
|
||||
content: string;
|
||||
frame_type: string;
|
||||
importance: string;
|
||||
source: string;
|
||||
created_at: string;
|
||||
}
|
||||
const IMPORTANCE_RANK: Record<string, number> = { critical: 0, important: 1, normal: 2, temporary: 3 };
|
||||
const PREFERENCES_K = 5;
|
||||
|
||||
/**
|
||||
* Fetch recent frames ordered by importance then recency. Used by both
|
||||
* loadRecentContext and the recallMemory catch-up branch. Excludes
|
||||
* 'deprecated' always; optionally excludes 'temporary' (R2 sign-gate
|
||||
* authoritative-recall filter).
|
||||
* Recent memories of one scope, ordered by importance then recency. Used by
|
||||
* the context loaders and the catch-up branch of recall. Optionally leaves
|
||||
* out 'temporary' memories (R2 sign-gate authoritative-recall filter).
|
||||
*/
|
||||
export function fetchRecentFrames(
|
||||
db: MindDB,
|
||||
export async function fetchRecentMemories(
|
||||
memory: MemoryStore,
|
||||
scope: MemoryScope,
|
||||
limit: number,
|
||||
opts?: { excludeTemporary?: boolean },
|
||||
): RecentFrameRow[] {
|
||||
const raw = db.getDatabase();
|
||||
const excludeTemp = opts?.excludeTemporary ?? false;
|
||||
const whereClause = excludeTemp
|
||||
? `WHERE importance != 'deprecated' AND importance != 'temporary'`
|
||||
: `WHERE importance != 'deprecated'`;
|
||||
return raw.prepare(
|
||||
`SELECT id, content, frame_type, importance, source, created_at
|
||||
FROM memory_frames
|
||||
${whereClause}
|
||||
ORDER BY
|
||||
CASE importance
|
||||
WHEN 'critical' THEN 0
|
||||
WHEN 'important' THEN 1
|
||||
WHEN 'normal' THEN 2
|
||||
ELSE 3
|
||||
END,
|
||||
id DESC
|
||||
LIMIT ?`
|
||||
).all(limit) as RecentFrameRow[];
|
||||
): Promise<MemoryRecord[]> {
|
||||
const [important, recent] = await Promise.all([
|
||||
memory.list({ scopes: [scope], minImportance: 'important', limit }),
|
||||
memory.list({ scopes: [scope], limit: limit * 2 }),
|
||||
]);
|
||||
const seen = new Set<string>();
|
||||
const merged: MemoryRecord[] = [];
|
||||
for (const r of [...important, ...recent]) {
|
||||
if (seen.has(r.id)) continue;
|
||||
if (opts?.excludeTemporary && r.importance === 'temporary') continue;
|
||||
seen.add(r.id);
|
||||
merged.push(r);
|
||||
}
|
||||
return merged
|
||||
.sort((a, b) => (IMPORTANCE_RANK[a.importance] ?? 9) - (IMPORTANCE_RANK[b.importance] ?? 9)
|
||||
|| b.createdAt.localeCompare(a.createdAt))
|
||||
.slice(0, limit);
|
||||
}
|
||||
|
||||
function primaryScope(deps: ContextLoaderDeps): MemoryScope {
|
||||
return deps.workspaceId ? workspaceScope(deps.workspaceId) : PERSONAL_SCOPE;
|
||||
}
|
||||
|
||||
async function personalPreferences(memory: MemoryStore): Promise<MemoryRecord[]> {
|
||||
return memory.list({ scopes: [PERSONAL_SCOPE], kinds: ['preference'], limit: PREFERENCES_K });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,38 +101,23 @@ export function fetchRecentFrames(
|
||||
* injection. Used by the legacy `buildSystemPrompt` path. On a positive
|
||||
* scan, returns '' so the poisoned content never enters the prompt.
|
||||
*/
|
||||
export function loadRecentContext(deps: ContextLoaderDeps, limit = 5): string {
|
||||
// Use workspace mind for recent context when available (it's more relevant)
|
||||
const primaryDb = deps.workspaceDb ?? deps.personalDb;
|
||||
const raw = primaryDb.getDatabase();
|
||||
|
||||
export async function loadRecentContext(deps: ContextLoaderDeps, limit = 5): Promise<string> {
|
||||
// Recent memories — prioritized by importance, then recency (A3 fix)
|
||||
const recentFrames = fetchRecentFrames(primaryDb, limit);
|
||||
const [recent, prefs] = await Promise.all([
|
||||
fetchRecentMemories(deps.memory, primaryScope(deps), limit),
|
||||
personalPreferences(deps.memory),
|
||||
]);
|
||||
|
||||
// Active tasks (from personal awareness — always available)
|
||||
const awarenessCtx = deps.awareness.toContext();
|
||||
|
||||
// Top knowledge entities (from workspace if available).
|
||||
// UNION ALL avoids `OR` in the JOIN, which defeats both relation
|
||||
// indexes (idx_relations_source, idx_relations_target) at 1M+ relations.
|
||||
const topEntities = raw.prepare(
|
||||
`SELECT ke.name, ke.entity_type, COUNT(rc.entity_id) as rel_count
|
||||
FROM knowledge_entities ke
|
||||
LEFT JOIN (
|
||||
SELECT source_id AS entity_id FROM knowledge_relations
|
||||
UNION ALL
|
||||
SELECT target_id AS entity_id FROM knowledge_relations
|
||||
) rc ON rc.entity_id = ke.id
|
||||
GROUP BY ke.id ORDER BY rel_count DESC LIMIT 10`
|
||||
).all() as Array<{ name: string; entity_type: string; rel_count: number }>;
|
||||
|
||||
const parts: string[] = [];
|
||||
|
||||
if (recentFrames.length > 0) {
|
||||
const source = deps.workspaceDb ? 'Workspace' : 'Personal';
|
||||
if (recent.length > 0) {
|
||||
const source = deps.workspaceId ? 'Workspace' : 'Personal';
|
||||
parts.push(`## Recent ${source} Memory`);
|
||||
for (const f of recentFrames) {
|
||||
parts.push(`- [${f.importance}] ${f.content.slice(0, CONTEXT_PREVIEW_LENGTH)}`);
|
||||
for (const r of recent) {
|
||||
parts.push(`- [${r.importance}] ${r.text.slice(0, CONTEXT_PREVIEW_LENGTH)}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,35 +126,20 @@ export function loadRecentContext(deps: ContextLoaderDeps, limit = 5): string {
|
||||
parts.push(awarenessCtx);
|
||||
}
|
||||
|
||||
if (topEntities.length > 0) {
|
||||
parts.push('\n## Key Knowledge');
|
||||
parts.push(topEntities.map(e => `${e.entity_type}: ${e.name}`).join(', '));
|
||||
}
|
||||
|
||||
// E4: Always include personal preferences (cross-workspace continuity)
|
||||
{
|
||||
const prefDb = deps.personalDb.getDatabase();
|
||||
const personalPrefs = prefDb.prepare(
|
||||
`SELECT content FROM memory_frames
|
||||
WHERE importance != 'deprecated'
|
||||
AND (content LIKE 'User preference:%' OR content LIKE 'Correction from user:%'
|
||||
OR content LIKE 'Style note:%' OR content LIKE 'Workspace topic:%')
|
||||
ORDER BY id DESC LIMIT 5`
|
||||
).all() as Array<{ content: string }>;
|
||||
if (personalPrefs.length > 0) {
|
||||
const label = deps.workspaceDb
|
||||
? 'Personal Preferences (across all workspaces)'
|
||||
: 'Personal Preferences';
|
||||
parts.push(`\n## ${label}`);
|
||||
for (const p of personalPrefs) {
|
||||
parts.push(`- ${p.content.slice(0, CONTEXT_PREVIEW_LENGTH)}`);
|
||||
}
|
||||
if (prefs.length > 0) {
|
||||
const label = deps.workspaceId
|
||||
? 'Personal Preferences (across all workspaces)'
|
||||
: 'Personal Preferences';
|
||||
parts.push(`\n## ${label}`);
|
||||
for (const p of prefs) {
|
||||
parts.push(`- ${p.text.slice(0, CONTEXT_PREVIEW_LENGTH)}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Review #1: scan preloaded context for injection before it enters the
|
||||
// system prompt. Harvested personal preferences and workspace frames
|
||||
// can carry poisoned instructions.
|
||||
// system prompt. Saved preferences and workspace memories can carry
|
||||
// poisoned instructions.
|
||||
const joined = parts.join('\n');
|
||||
const scan = scanForInjection(joined, 'tool_output');
|
||||
if (!scan.safe) {
|
||||
@@ -194,69 +159,30 @@ export function loadRecentContext(deps: ContextLoaderDeps, limit = 5): string {
|
||||
* Pure data — injection scanning is the assembler's responsibility (it
|
||||
* has the tier context needed to decide what to drop vs sanitize).
|
||||
*/
|
||||
export function loadRecentContextFrames(deps: ContextLoaderDeps, limit = 10): ContextFrames {
|
||||
const primaryDb = deps.workspaceDb ?? deps.personalDb;
|
||||
const raw = primaryDb.getDatabase();
|
||||
export async function loadRecentContextFrames(deps: ContextLoaderDeps, limit = 10): Promise<ContextFrames> {
|
||||
const [recent, prefs] = await Promise.all([
|
||||
fetchRecentMemories(deps.memory, primaryScope(deps), limit),
|
||||
personalPreferences(deps.memory),
|
||||
]);
|
||||
|
||||
const frameRows = raw.prepare(
|
||||
`SELECT id, frame_type, gop_id, t, base_frame_id, content, importance, source,
|
||||
access_count, created_at, last_accessed
|
||||
FROM memory_frames
|
||||
WHERE importance != 'deprecated'
|
||||
ORDER BY
|
||||
CASE importance
|
||||
WHEN 'critical' THEN 0
|
||||
WHEN 'important' THEN 1
|
||||
WHEN 'normal' THEN 2
|
||||
ELSE 3
|
||||
END,
|
||||
id DESC
|
||||
LIMIT ?`
|
||||
).all(limit) as MemoryFrame[];
|
||||
|
||||
const stateFrames: MemoryFrame[] = [];
|
||||
const recentChanges: MemoryFrame[] = [];
|
||||
for (const f of frameRows) {
|
||||
if (f.frame_type === 'I') stateFrames.push(f);
|
||||
else recentChanges.push(f);
|
||||
const stateFrames: RecallFrame[] = [];
|
||||
const recentChanges: RecallFrame[] = [];
|
||||
for (const r of recent) {
|
||||
if (r.importance === 'critical' || r.importance === 'important') stateFrames.push(toRecallFrame(r));
|
||||
else recentChanges.push(toRecallFrame(r));
|
||||
}
|
||||
|
||||
const awarenessItems = deps.awareness.getAll();
|
||||
const activeWork = awarenessItems.map(item => ({
|
||||
const activeWork = deps.awareness.getAll().map(item => ({
|
||||
category: item.category,
|
||||
content: item.content,
|
||||
priority: item.priority,
|
||||
}));
|
||||
|
||||
const topEntities = raw.prepare(
|
||||
`SELECT ke.name, ke.entity_type, COUNT(rc.entity_id) as rel_count
|
||||
FROM knowledge_entities ke
|
||||
LEFT JOIN (
|
||||
SELECT source_id AS entity_id FROM knowledge_relations
|
||||
UNION ALL
|
||||
SELECT target_id AS entity_id FROM knowledge_relations
|
||||
) rc ON rc.entity_id = ke.id
|
||||
GROUP BY ke.id ORDER BY rel_count DESC LIMIT 10`
|
||||
).all() as Array<{ name: string; entity_type: string; rel_count: number }>;
|
||||
|
||||
const keyEntities = topEntities.map(e => ({ name: e.name, type: e.entity_type }));
|
||||
|
||||
const prefDb = deps.personalDb.getDatabase();
|
||||
const prefRows = prefDb.prepare(
|
||||
`SELECT content FROM memory_frames
|
||||
WHERE importance != 'deprecated'
|
||||
AND (content LIKE 'User preference:%' OR content LIKE 'Correction from user:%'
|
||||
OR content LIKE 'Style note:%' OR content LIKE 'Workspace topic:%')
|
||||
ORDER BY id DESC LIMIT 5`
|
||||
).all() as Array<{ content: string }>;
|
||||
|
||||
const personalPreferences = prefRows.map(p => p.content);
|
||||
|
||||
return {
|
||||
stateFrames,
|
||||
recentChanges,
|
||||
activeWork,
|
||||
keyEntities,
|
||||
personalPreferences,
|
||||
keyEntities: [],
|
||||
personalPreferences: prefs.map(p => p.text),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -11,15 +11,14 @@
|
||||
* "always allow" grants land in Phase B.3.
|
||||
*/
|
||||
|
||||
import type { MindDB } from '@waggle/core';
|
||||
import { HybridSearch } from '@waggle/core';
|
||||
import { workspaceScope, type MemoryStore } from '@waggle/memory-contract';
|
||||
import type { ToolDefinition } from './tools.js';
|
||||
|
||||
export interface CrossWorkspaceToolDeps {
|
||||
/** The workspace this tool instance is running in (the "source"). */
|
||||
sourceWorkspaceId: string;
|
||||
/** Returns the MindDB for the requested workspace, or null if unknown / closed. */
|
||||
getMindForWorkspace: (workspaceId: string) => MindDB | null;
|
||||
/** Memory of every workspace; this tool reads only the target workspace's scope. */
|
||||
memory: MemoryStore;
|
||||
/** Returns the list of known workspace IDs + display names (for discovery). */
|
||||
listWorkspaces: () => Array<{ id: string; name: string }>;
|
||||
/** Returns a file listing for the given workspace (workspace-scoped path). */
|
||||
@@ -36,12 +35,10 @@ export interface CrossWorkspaceToolDeps {
|
||||
* that need binary should extend this signature before wiring it).
|
||||
*/
|
||||
readWorkspaceFile?: (workspaceId: string, relativePath: string) => Promise<string>;
|
||||
/** The embedder used for semantic search. */
|
||||
embedder: import('@waggle/core').Embedder;
|
||||
}
|
||||
|
||||
export function createCrossWorkspaceTools(deps: CrossWorkspaceToolDeps): ToolDefinition[] {
|
||||
const { sourceWorkspaceId, getMindForWorkspace, listWorkspaces, listWorkspaceFiles, readWorkspaceFile, embedder } = deps;
|
||||
const { sourceWorkspaceId, memory, listWorkspaces, listWorkspaceFiles, readWorkspaceFile } = deps;
|
||||
|
||||
const readOtherWorkspace: ToolDefinition = {
|
||||
name: 'read_other_workspace',
|
||||
@@ -49,7 +46,7 @@ export function createCrossWorkspaceTools(deps: CrossWorkspaceToolDeps): ToolDef
|
||||
'Search memory in another workspace. READ-ONLY — never writes.',
|
||||
'Use when the user references something they worked on in a different project.',
|
||||
'Requires the target workspace ID. Call list_workspaces first if unsure.',
|
||||
'Returns ranked memory frames with the target workspace explicitly named',
|
||||
'Returns ranked memories with the target workspace explicitly named',
|
||||
'so you can cite the source clearly ("In Workspace B, you decided...").',
|
||||
'First use on a new target workspace prompts the user for approval.',
|
||||
].join(' '),
|
||||
@@ -94,9 +91,8 @@ export function createCrossWorkspaceTools(deps: CrossWorkspaceToolDeps): ToolDef
|
||||
});
|
||||
}
|
||||
|
||||
const mind = getMindForWorkspace(targetId);
|
||||
if (!mind) {
|
||||
const known = listWorkspaces();
|
||||
const known = listWorkspaces();
|
||||
if (!known.some(w => w.id === targetId)) {
|
||||
return JSON.stringify({
|
||||
error: `Workspace "${targetId}" not found or not accessible.`,
|
||||
knownWorkspaces: known.map(w => ({ id: w.id, name: w.name })),
|
||||
@@ -104,9 +100,8 @@ export function createCrossWorkspaceTools(deps: CrossWorkspaceToolDeps): ToolDef
|
||||
}
|
||||
|
||||
try {
|
||||
const search = new HybridSearch(mind, embedder);
|
||||
const results = await search.search(query, { limit, profile: 'balanced' });
|
||||
const targetName = listWorkspaces().find(w => w.id === targetId)?.name ?? targetId;
|
||||
const results = await memory.search(query, { scopes: [workspaceScope(targetId)], limit });
|
||||
const targetName = known.find(w => w.id === targetId)?.name ?? targetId;
|
||||
|
||||
if (results.length === 0) {
|
||||
return JSON.stringify({
|
||||
@@ -120,10 +115,10 @@ export function createCrossWorkspaceTools(deps: CrossWorkspaceToolDeps): ToolDef
|
||||
}
|
||||
|
||||
const matches = results.map(r => ({
|
||||
content: r.frame.content.slice(0, 500),
|
||||
importance: r.frame.importance,
|
||||
createdAt: r.frame.created_at,
|
||||
score: r.finalScore,
|
||||
content: r.text.slice(0, 500),
|
||||
importance: r.importance,
|
||||
createdAt: r.createdAt,
|
||||
score: r.score,
|
||||
}));
|
||||
|
||||
return JSON.stringify({
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
import type { KnowledgeGraph } from '@waggle/core';
|
||||
|
||||
export class FeedbackHandler {
|
||||
private kg: KnowledgeGraph;
|
||||
|
||||
constructor(kg: KnowledgeGraph) {
|
||||
this.kg = kg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Correct an entity's properties by merging updates into existing properties.
|
||||
* Adds a `last_corrected` timestamp to track when the correction happened.
|
||||
*/
|
||||
correctEntity(entityId: number, updates: Record<string, unknown>): void {
|
||||
const entity = this.kg.getEntity(entityId);
|
||||
if (!entity) return;
|
||||
|
||||
const existingProps: Record<string, unknown> = JSON.parse(entity.properties || '{}');
|
||||
this.kg.updateEntity(entityId, {
|
||||
properties: { ...existingProps, ...updates, last_corrected: new Date().toISOString() },
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate an entity by retiring it (setting valid_to) and recording the reason.
|
||||
*/
|
||||
invalidateEntity(entityId: number, reason: string): void {
|
||||
const entity = this.kg.getEntity(entityId);
|
||||
if (!entity) return;
|
||||
|
||||
// Store the invalidation reason in properties before retiring
|
||||
const existingProps: Record<string, unknown> = JSON.parse(entity.properties || '{}');
|
||||
this.kg.updateEntity(entityId, {
|
||||
properties: { ...existingProps, invalidation_reason: reason },
|
||||
});
|
||||
|
||||
// Retire the entity (sets valid_to to now)
|
||||
this.kg.retireEntity(entityId);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
export { Orchestrator, type OrchestratorConfig } from './orchestrator.js';
|
||||
export { Orchestrator, type OrchestratorConfig, type RecallOptions, type ContextFrames } from './orchestrator.js';
|
||||
export { toRecallFrame, type RecallFrame } from './recall-frame.js';
|
||||
export {
|
||||
generateTurnId,
|
||||
logTurnEvent,
|
||||
@@ -212,7 +213,6 @@ export { scanForInjection, type ScanResult } from './injection-scanner.js';
|
||||
export { checkGrounding, extractClaimedSpecifics, type GroundingResult, type ClaimedSpecific } from './grounding-check.js';
|
||||
export { CostTracker, DEFAULT_MODEL_PRICING, type ModelPricing, type UsageStats, type UsageEntry } from './cost-tracker.js';
|
||||
export { extractEntities, type ExtractedEntity } from './entity-extractor.js';
|
||||
export { CognifyPipeline, type CognifyConfig, type CognifyResult } from './cognify.js';
|
||||
export {
|
||||
TraceRecorder, truncate as truncateTraceText, scrubSecrets,
|
||||
type TraceHandle, type FinalizeOptions as TraceFinalizeOptions,
|
||||
@@ -250,7 +250,6 @@ export {
|
||||
BUILTIN_HARNESSES, getHarnessById, matchHarness,
|
||||
researchVerifyHarness, codeReviewFixHarness, documentDraftHarness,
|
||||
} from './builtin-harnesses.js';
|
||||
export { FeedbackHandler } from './feedback-handler.js';
|
||||
export { checkResponseQuality, type QualityIssue } from './quality-controller.js';
|
||||
export { HookRegistry, type HookEvent, type HookContext, type HookResult, type HookActivityEntry, type HookFn } from './hooks.js';
|
||||
export { loadHooksFromConfig } from './hook-loader.js';
|
||||
@@ -288,7 +287,6 @@ export { extractEntitiesWithLLM, type LLMCallFn as EntityLLMCallFn } from './ent
|
||||
export { createSkillTools, type SkillToolsDeps } from './skill-tools.js';
|
||||
export { SkillRecommender, type SkillRecommendation, type SkillRecommenderDeps } from './skill-recommender.js';
|
||||
export { createSubAgentTools, ROLE_TOOL_PRESETS, type SubAgentToolsDeps, type SubAgentDef, type SubAgentResult } from './subagent-tools.js';
|
||||
export { MemoryLinker, type MemoryLink } from './memory-linker.js';
|
||||
export { CapabilityRouter, type CapabilityRoute, type CapabilitySource, type CapabilityRouterDeps, type ConnectorInfo } from './capability-router.js';
|
||||
export {
|
||||
searchCapabilities, validateInstallCandidate, loadStarterSkillsMeta,
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
import type { HybridSearch, SearchResult } from '@waggle/core';
|
||||
|
||||
export interface MemoryLink {
|
||||
frameId: number;
|
||||
content: string;
|
||||
score: number;
|
||||
}
|
||||
|
||||
export class MemoryLinker {
|
||||
private search: HybridSearch;
|
||||
private threshold: number;
|
||||
|
||||
constructor(config: { search: HybridSearch; threshold?: number }) {
|
||||
this.search = config.search;
|
||||
this.threshold = config.threshold ?? 0.1;
|
||||
}
|
||||
|
||||
async findRelated(content: string, limit = 5): Promise<MemoryLink[]> {
|
||||
const results = await this.search.search(content, { limit });
|
||||
return results
|
||||
.filter(r => r.finalScore >= this.threshold)
|
||||
.map(r => ({
|
||||
frameId: r.frame.id,
|
||||
content: r.frame.content,
|
||||
score: r.finalScore,
|
||||
}));
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Pattern-based memory write-back. Inspects (userMsg, assistantMsg) for
|
||||
* preference / decision / correction / structured-finding signals and writes
|
||||
* the distilled frames into the appropriate mind store.
|
||||
* the distilled memories into personal or workspace memory.
|
||||
*
|
||||
* Extracted from Orchestrator.autoSaveFromExchange (PR-A, 2026-05-27).
|
||||
* Behavior preserved verbatim; only the call surface moved.
|
||||
@@ -14,23 +14,21 @@
|
||||
*
|
||||
* SOTA target: this is the regex-based predecessor of an LLM-driven memory
|
||||
* writer (PR-H). The patterns here are deliberately conservative — false-
|
||||
* positive suppression matters more than recall, because every saved frame
|
||||
* positive suppression matters more than recall, because every saved memory
|
||||
* becomes recallable context.
|
||||
*/
|
||||
|
||||
import {
|
||||
type FrameStore,
|
||||
type SessionStore,
|
||||
type Importance,
|
||||
type FrameSource,
|
||||
type MemoryFrame,
|
||||
type MindDB,
|
||||
type TeamSync,
|
||||
createCoreLogger,
|
||||
evaluateExternalMemoryIngress,
|
||||
} from '@waggle/core';
|
||||
PERSONAL_SCOPE,
|
||||
workspaceScope,
|
||||
type MemoryImportance,
|
||||
type MemoryKind,
|
||||
type MemoryRecord,
|
||||
type MemorySource,
|
||||
type MemoryStore,
|
||||
} from '@waggle/memory-contract';
|
||||
import { createCoreLogger, evaluateExternalMemoryIngress } from '@waggle/core';
|
||||
import { isSelfIncapacityAssertion } from './memory-sign-gate.js';
|
||||
import type { CognifyPipeline } from './cognify.js';
|
||||
import {
|
||||
MIN_CONTENT_LENGTH,
|
||||
DEDUP_SLICE_LENGTH,
|
||||
@@ -43,26 +41,14 @@ import {
|
||||
const logger = createCoreLogger('pattern-write-back');
|
||||
|
||||
/**
|
||||
* Stores + facilities the extractor needs to persist frames to the right
|
||||
* mind (workspace vs personal) and optionally push to team-sync. The seam is
|
||||
* kept tight on purpose — the extractor doesn't need to know about identity,
|
||||
* awareness, search, or knowledge layers.
|
||||
* Where the extractor writes. Preferences, corrections and style notes go to
|
||||
* personal memory; decisions and work output go to the active workspace, or
|
||||
* to personal memory when no workspace is active.
|
||||
*/
|
||||
export interface PatternWriteBackDeps {
|
||||
/** Personal mind layers (preferences, corrections, style notes route here) */
|
||||
personal: {
|
||||
db: MindDB;
|
||||
frames: FrameStore;
|
||||
sessions: SessionStore;
|
||||
};
|
||||
/** Workspace mind layers (decisions, work output route here). Null = personal-only mode. */
|
||||
workspace: {
|
||||
frames: FrameStore;
|
||||
sessions: SessionStore;
|
||||
cognify: CognifyPipeline;
|
||||
} | null;
|
||||
/** Optional team-sync client; receives pushFrame for workspace writes only */
|
||||
teamSync: TeamSync | null;
|
||||
memory: MemoryStore;
|
||||
/** Active workspace, or null for personal-only mode. */
|
||||
workspaceId: string | null;
|
||||
}
|
||||
|
||||
// ── Pattern definitions (lifted from inline orchestrator function) ──
|
||||
@@ -135,93 +121,64 @@ type SaveTarget = 'workspace' | 'personal';
|
||||
|
||||
/**
|
||||
* Post-response heuristic write-back. Scans (userMsg, assistantMsg) for
|
||||
* save-worthy signals and writes distilled memory frames. Returns short
|
||||
* save-worthy signals and writes distilled memories. Returns short
|
||||
* previews of what was saved (DEDUP_SLICE_LENGTH chars each).
|
||||
*
|
||||
* Routing:
|
||||
* target='workspace' → uses workspace cognify+frames when active, else personal
|
||||
* target='workspace' → the active workspace's memory, else personal
|
||||
* target='personal' → always personal (preferences, corrections, style)
|
||||
*/
|
||||
export async function runPatternWriteBack(
|
||||
deps: PatternWriteBackDeps,
|
||||
userMsg: string,
|
||||
assistantMsg: string,
|
||||
opts?: { traceId?: string },
|
||||
opts?: { traceId?: string; sessionId?: string },
|
||||
): Promise<string[]> {
|
||||
const saved: string[] = [];
|
||||
|
||||
// Helper: persist one frame, applying the sign-gate downgrade and the
|
||||
// team-sync push for workspace writes. Returns the created frame so
|
||||
// teamSync.pushFrame gets the actual write, not whatever is "latest" at
|
||||
// read time (the bug that motivated Review #9).
|
||||
// Helper: persist one memory, applying the ingress guard and the sign-gate
|
||||
// downgrade. Returns the stored record (an identical earlier memory when the
|
||||
// text already exists), or null when the guard refused it.
|
||||
const save = async (
|
||||
content: string,
|
||||
importance: Importance,
|
||||
importance: MemoryImportance,
|
||||
kind: MemoryKind,
|
||||
target: SaveTarget = 'workspace',
|
||||
// PR3.5 honesty (review H-1): this is a heuristic EXTRACTOR, so its writes
|
||||
// default to 'agent_inferred' — NOT the schema default 'user_stated', which
|
||||
// would make the Memory-Trust provenance pill claim the user said things the
|
||||
// agent inferred. Genuinely user-authored frames (preferences, corrections)
|
||||
// pass 'user_stated' explicitly at their call sites.
|
||||
source: FrameSource = 'agent_inferred',
|
||||
): Promise<MemoryFrame | null> => {
|
||||
// default to 'agent_inferred'. Genuinely user-authored memories
|
||||
// (preferences, corrections) pass 'user_stated' explicitly.
|
||||
source: MemorySource = 'agent_inferred',
|
||||
): Promise<MemoryRecord | null> => {
|
||||
if (evaluateExternalMemoryIngress({ content }).action !== 'allow') return null;
|
||||
|
||||
// R2 sign gate (DEFECT-2): self-incapacity assertions persist at
|
||||
// 'temporary' so they're audit-visible but cannot re-enter the prompt as
|
||||
// authoritative recall (recall path excludes 'temporary').
|
||||
// authoritative recall (recall excludes 'temporary').
|
||||
if (importance !== 'temporary' && isSelfIncapacityAssertion(content)) {
|
||||
logger.debug('autoSave sign-gate: self-incapacity frame downgraded to temporary', {
|
||||
logger.debug('autoSave sign-gate: self-incapacity memory downgraded to temporary', {
|
||||
preview: content.slice(0, DEDUP_SLICE_LENGTH),
|
||||
});
|
||||
importance = 'temporary';
|
||||
}
|
||||
const useWorkspace = target === 'workspace' && deps.workspace !== null;
|
||||
const frames = useWorkspace ? deps.workspace!.frames : deps.personal.frames;
|
||||
const sessions = useWorkspace ? deps.workspace!.sessions : deps.personal.sessions;
|
||||
const cognify = useWorkspace ? deps.workspace!.cognify : null;
|
||||
const scope = target === 'workspace' && deps.workspaceId
|
||||
? workspaceScope(deps.workspaceId)
|
||||
: PERSONAL_SCOPE;
|
||||
|
||||
let createdFrame: MemoryFrame | null = null;
|
||||
|
||||
if (cognify) {
|
||||
const result = await cognify.cognify(content, importance, undefined, undefined, source);
|
||||
createdFrame = frames.getById(result.frameId) ?? null;
|
||||
} else {
|
||||
// ensureActive is transaction-wrapped — concurrent saves on a fresh
|
||||
// mind don't race into twin sessions with frames split across them.
|
||||
const session = sessions.ensureActive();
|
||||
const gopId = session.gop_id;
|
||||
const latestI = frames.getLatestIFrame(gopId);
|
||||
createdFrame = latestI
|
||||
? frames.createPFrame(gopId, content, latestI.id, importance, source)
|
||||
: frames.createIFrame(gopId, content, importance, source);
|
||||
}
|
||||
// PR3.5 memory↔trace backlink: the execution-trace id that produced this
|
||||
// memory, so the "Why did you do that?" view can resolve the decision.
|
||||
// An identical earlier memory keeps the trace that originally wrote it.
|
||||
const [change] = await deps.memory.remember({
|
||||
scope,
|
||||
text: content,
|
||||
kind,
|
||||
importance,
|
||||
source,
|
||||
origin: 'chat-write-back',
|
||||
...(opts?.sessionId && { sessionId: opts.sessionId }),
|
||||
...(opts?.traceId && { metadata: { trace_id: opts.traceId } }),
|
||||
});
|
||||
saved.push(content.slice(0, DEDUP_SLICE_LENGTH));
|
||||
|
||||
// PR3.5 frame↔trace backlink: stamp the execution-trace id that produced
|
||||
// this frame so the Memory-Trust "Why did you do that?" view can resolve
|
||||
// the real decision (GET /api/memory/:id/trace). Additive metadata only —
|
||||
// never touches content/importance/dedup, so it's byte-identical for any
|
||||
// caller that doesn't pass a traceId (every benchmark path).
|
||||
if (opts?.traceId && createdFrame) {
|
||||
let existingMeta: Record<string, unknown> = {};
|
||||
try {
|
||||
existingMeta = createdFrame.metadata ? JSON.parse(createdFrame.metadata) as Record<string, unknown> : {};
|
||||
} catch { /* malformed metadata → start clean */ }
|
||||
// Preserve an existing backlink (review L-1): createIFrame dedups on
|
||||
// content hash, so createdFrame may be an OLDER frame already linked to
|
||||
// the trace that originally wrote it. Keep that originating trace rather
|
||||
// than re-pointing it to this turn's re-assertion.
|
||||
if (existingMeta.trace_id === undefined) {
|
||||
frames.setMetadata(createdFrame.id, JSON.stringify({ ...existingMeta, trace_id: opts.traceId }));
|
||||
}
|
||||
}
|
||||
|
||||
if (deps.teamSync && useWorkspace && createdFrame) {
|
||||
deps.teamSync.pushFrame(createdFrame).catch(() => { /* non-blocking */ });
|
||||
}
|
||||
|
||||
return createdFrame;
|
||||
return change?.record ?? null;
|
||||
};
|
||||
|
||||
// ── Early gate: short messages skip unless they look like a decision ack ──
|
||||
@@ -243,7 +200,7 @@ export async function runPatternWriteBack(
|
||||
const sentences = userMsg.split(/[.!?\n]+/).filter(s => pat.test(s));
|
||||
if (sentences.length > 0) {
|
||||
// User stated the preference directly → genuine 'user_stated' provenance.
|
||||
await save(`User preference: ${sentences[0].trim()}`, 'normal', 'personal', 'user_stated');
|
||||
await save(`User preference: ${sentences[0].trim()}`, 'normal', 'preference', 'personal', 'user_stated');
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -253,13 +210,8 @@ export async function runPatternWriteBack(
|
||||
if (saved.length === 0 && userMsg.length > MIN_CONTENT_LENGTH) {
|
||||
for (const { pattern, note } of STYLE_SIGNALS) {
|
||||
if (pattern.test(userMsg)) {
|
||||
const personalRaw = deps.personal.db.getDatabase();
|
||||
const existing = personalRaw.prepare(
|
||||
`SELECT id FROM memory_frames WHERE content = ? LIMIT 1`
|
||||
).get(note) as { id: number } | undefined;
|
||||
if (!existing) {
|
||||
await save(note, 'normal', 'personal');
|
||||
}
|
||||
// The store does not write identical text twice in one scope.
|
||||
await save(note, 'normal', 'preference', 'personal');
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -294,13 +246,13 @@ export async function runPatternWriteBack(
|
||||
}
|
||||
|
||||
if (decisionText) {
|
||||
await save(`Decision: ${decisionText}`, 'important');
|
||||
await save(`Decision: ${decisionText}`, 'important', 'decision');
|
||||
}
|
||||
|
||||
// ── Pattern: user correction ──
|
||||
if (CORRECTION_PATTERNS.some(p => p.test(userMsg))) {
|
||||
// User stated the correction directly → genuine 'user_stated' provenance.
|
||||
await save(`Correction from user: ${userMsg.slice(0, CONTEXT_PREVIEW_LENGTH)}`, 'important', 'personal', 'user_stated');
|
||||
await save(`Correction from user: ${userMsg.slice(0, CONTEXT_PREVIEW_LENGTH)}`, 'important', 'fact', 'personal', 'user_stated');
|
||||
}
|
||||
|
||||
// ── Pattern: research output with external sources ──
|
||||
@@ -317,7 +269,7 @@ export async function runPatternWriteBack(
|
||||
...(urls.length > 0 ? [`Sources: ${urls.join(', ')}`] : []),
|
||||
].join('. ');
|
||||
if (findingSummary.length > 20) {
|
||||
await save(`Research findings: ${findingSummary.slice(0, FINDINGS_SLICE_LENGTH)}`, 'important');
|
||||
await save(`Research findings: ${findingSummary.slice(0, FINDINGS_SLICE_LENGTH)}`, 'important', 'summary');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,7 +290,7 @@ export async function runPatternWriteBack(
|
||||
// not a user-stated fact. Persist it audit-visible but at 'temporary'
|
||||
// so the recall path (which excludes 'temporary') can't re-surface a
|
||||
// confabulated specific as authoritative memory on a later turn.
|
||||
if ((await save(`Recommendation: ${text.slice(0, RECALL_LINE_LENGTH)}`, 'temporary')) !== null) {
|
||||
if ((await save(`Recommendation: ${text.slice(0, RECALL_LINE_LENGTH)}`, 'temporary', 'note')) !== null) {
|
||||
savedStructured = true;
|
||||
break inlineDecision;
|
||||
}
|
||||
@@ -351,7 +303,7 @@ export async function runPatternWriteBack(
|
||||
s.startsWith('User preference:') || s.startsWith('Correction from user:') || s.startsWith('Decision:')
|
||||
);
|
||||
if (!alreadyCapturedUser) {
|
||||
if ((await save(`User asked: ${userMsg.slice(0, RECALL_LINE_LENGTH)}`, 'temporary')) !== null) {
|
||||
if ((await save(`User asked: ${userMsg.slice(0, RECALL_LINE_LENGTH)}`, 'temporary', 'note')) !== null) {
|
||||
savedStructured = true;
|
||||
}
|
||||
}
|
||||
@@ -370,7 +322,7 @@ export async function runPatternWriteBack(
|
||||
const prefix = heading ? `${heading}: ` : 'Key points: ';
|
||||
// Agent-extracted bullets from its own reply — audit-visible but
|
||||
// 'temporary' (recall-excluded) so confabulated specifics can't loop back.
|
||||
if ((await save(`${prefix}${keyPoints.slice(0, FINDINGS_SLICE_LENGTH)}`, 'temporary')) !== null) {
|
||||
if ((await save(`${prefix}${keyPoints.slice(0, FINDINGS_SLICE_LENGTH)}`, 'temporary', 'note')) !== null) {
|
||||
savedStructured = true;
|
||||
}
|
||||
}
|
||||
@@ -386,7 +338,7 @@ export async function runPatternWriteBack(
|
||||
: firstMeaningful || 'Work output produced';
|
||||
// Agent's own summary of its reply — audit-visible but 'temporary'
|
||||
// (recall-excluded) to sever the confabulation→save→recall-as-fact loop.
|
||||
await save(`Work completed: ${summary.slice(0, RECALL_LINE_LENGTH)}`, 'temporary');
|
||||
await save(`Work completed: ${summary.slice(0, RECALL_LINE_LENGTH)}`, 'temporary', 'note');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
32
packages/agent/src/recall-frame.ts
Normal file
32
packages/agent/src/recall-frame.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* The shape recalled memory takes on its way into a prompt.
|
||||
*
|
||||
* Field names follow the old frame rows (`content`, `created_at`) because the
|
||||
* prompt assembler and its tests render them that way; values come from a
|
||||
* contract `MemoryRecord`.
|
||||
*/
|
||||
import type { MemoryImportance, MemoryKind, MemoryRecord, MemorySource } from '@waggle/memory-contract';
|
||||
|
||||
export interface RecallFrame {
|
||||
readonly id: string;
|
||||
readonly content: string;
|
||||
readonly kind: MemoryKind;
|
||||
readonly importance: MemoryImportance;
|
||||
readonly source: MemorySource;
|
||||
/** ISO 8601. */
|
||||
readonly created_at: string;
|
||||
/** Relevance within one result list; 1 for memories picked by a lane. */
|
||||
readonly score: number;
|
||||
}
|
||||
|
||||
export function toRecallFrame(record: MemoryRecord, score = 1): RecallFrame {
|
||||
return {
|
||||
id: record.id,
|
||||
content: record.text,
|
||||
kind: record.kind,
|
||||
importance: record.importance,
|
||||
source: record.source,
|
||||
created_at: record.createdAt,
|
||||
score,
|
||||
};
|
||||
}
|
||||
@@ -2,7 +2,7 @@ export interface AgentCapabilities {
|
||||
tools: Array<{ name: string; description: string }>;
|
||||
skills: string[];
|
||||
model: string;
|
||||
memoryStats: { frameCount: number; sessionCount: number; entityCount: number };
|
||||
memoryStats: { memoryCount: number };
|
||||
mode: 'local' | 'team';
|
||||
version: string;
|
||||
}
|
||||
@@ -16,9 +16,9 @@ export function buildSelfAwareness(caps: AgentCapabilities): string {
|
||||
lines.push(`Model: ${caps.model}`);
|
||||
|
||||
// Memory stats — helps agent understand its own knowledge depth
|
||||
const { frameCount, sessionCount, entityCount } = caps.memoryStats;
|
||||
if (frameCount > 0) {
|
||||
lines.push(`Memory: ${frameCount} memories across ${sessionCount} sessions, ${entityCount} knowledge entities.`);
|
||||
const { memoryCount } = caps.memoryStats;
|
||||
if (memoryCount > 0) {
|
||||
lines.push(`Memory: ${memoryCount} memories.`);
|
||||
lines.push('You have prior context. Use search_memory to recall relevant information before responding.');
|
||||
} else {
|
||||
lines.push('Memory: empty — this appears to be a fresh start. Learn the user\'s preferences and save important context.');
|
||||
|
||||
@@ -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".`;
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user