This commit is contained in:
Oleg Maslov
2026-09-02 10:14:22 +02:00
parent 0c3e2ead3b
commit b20b138fe4
771 changed files with 161561 additions and 9027 deletions

View File

@@ -1,4 +1,12 @@
import type { MindDB, FrameStore, MemoryFrame, Importance, SessionStore, KnowledgeGraph } from '@waggle/core';
import {
evaluateExternalMemoryIngress,
type MindDB,
type FrameStore,
type MemoryFrame,
type Importance,
type SessionStore,
type KnowledgeGraph,
} from '@waggle/core';
const IMPORTANCE_UPGRADE: Record<string, Importance> = {
temporary: 'normal',
@@ -24,6 +32,9 @@ export class MemoryWeaver {
// Merge I-frame + P-frames into consolidated content
const parts = [state.iframe.content, ...state.pframes.map(p => p.content)];
const mergedContent = parts.join('\n---\n');
if ([mergedContent, parts.join('\n'), parts.join('')].some(
content => evaluateExternalMemoryIngress({ content }).action !== 'allow',
)) return null;
// Create new consolidated I-frame
const consolidated = this.frames.createIFrame(gopId, mergedContent, 'normal');
@@ -42,47 +53,41 @@ export class MemoryWeaver {
decayFrames(): number {
const raw = this.db.getDatabase();
// Delete deprecated frames with zero access count
// First get the IDs for FTS cleanup
// Select deprecated frames with zero access count for canonical cleanup.
const toDelete = raw.prepare(
"SELECT id FROM memory_frames WHERE importance = 'deprecated' AND access_count = 0"
).all() as { id: number }[];
if (toDelete.length === 0) return 0;
const ids = toDelete.map(r => r.id);
const placeholders = ids.map(() => '?').join(',');
// Delete from FTS index
raw.prepare(
`DELETE FROM memory_frames_fts WHERE rowid IN (${placeholders})`
).run(...ids);
// Delete the frames
const result = raw.prepare(
`DELETE FROM memory_frames WHERE id IN (${placeholders})`
).run(...ids);
return result.changes;
let deleted = 0;
for (const { id } of toDelete) {
if (this.frames.delete(id)) deleted++;
}
return deleted;
}
strengthenFrames(tempThreshold = 10, normalThreshold = 25): number {
const raw = this.db.getDatabase();
let upgraded = 0;
const candidates = raw.prepare(
'SELECT id, content FROM memory_frames WHERE importance = ? AND access_count >= ?',
);
const promote = raw.prepare(
'UPDATE memory_frames SET importance = ? WHERE id = ? AND content = ? AND importance = ? AND access_count >= ?',
);
// Upgrade temporary → normal
const tempResult = raw.prepare(`
UPDATE memory_frames SET importance = 'normal'
WHERE importance = 'temporary' AND access_count >= ?
`).run(tempThreshold);
upgraded += tempResult.changes;
for (const frame of candidates.all('temporary', tempThreshold) as Array<{ id: number; content: string }>) {
if (evaluateExternalMemoryIngress({ content: frame.content }).action !== 'allow') continue;
upgraded += promote.run('normal', frame.id, frame.content, 'temporary', tempThreshold).changes;
}
// Upgrade normal → important
const normalResult = raw.prepare(`
UPDATE memory_frames SET importance = 'important'
WHERE importance = 'normal' AND access_count >= ?
`).run(normalThreshold);
upgraded += normalResult.changes;
for (const frame of candidates.all('normal', normalThreshold) as Array<{ id: number; content: string }>) {
if (evaluateExternalMemoryIngress({ content: frame.content }).action !== 'allow') continue;
upgraded += promote.run('important', frame.id, frame.content, 'normal', normalThreshold).changes;
}
return upgraded;
}
@@ -102,9 +107,13 @@ export class MemoryWeaver {
if (allContent.length === 0) return null;
const summaryContent = allContent.join('\n---\n');
if ([summaryContent, allContent.join('\n'), allContent.join('')].some(
content => evaluateExternalMemoryIngress({ content }).action !== 'allow',
)) return null;
// Create a summary session
const summarySession = this.sessions.create('daily-summary');
const summaryContent = allContent.join('\n---\n');
return this.frames.createIFrame(summarySession.gop_id, summaryContent, 'important');
}
@@ -179,15 +188,20 @@ export class MemoryWeaver {
for (let j = i + 1; j < ids.length; j++) {
const pairKey = `${Math.min(ids[i], ids[j])}:${Math.max(ids[i], ids[j])}`;
if (linkedPairs.has(pairKey)) continue;
linkedPairs.add(pairKey);
// Find the gop_id of the base frame
const baseFrame = allFrames.find(f => f.id === ids[i]);
if (!baseFrame) continue;
const referencedFrame = allFrames.find(f => f.id === ids[j]);
if (!baseFrame || !referencedFrame) continue;
const description = `Shared entity: ${entityName}`;
const bContent = JSON.stringify({ description, references: [ids[j]] });
const sourceContent = [baseFrame.content, referencedFrame.content];
if ([entityName, bContent, ...sourceContent, sourceContent.join('\n'), sourceContent.join('')].some(
content => evaluateExternalMemoryIngress({ content }).action !== 'allow',
)) continue;
linkedPairs.add(pairKey);
this.frames.createBFrame(
baseFrame.gop_id,
`Shared entity: ${entityName}`,
description,
ids[i],
[ids[j]]
);
@@ -204,12 +218,16 @@ export class MemoryWeaver {
* Takes pre-extracted session summary and key points, creates an important frame
* that persists across consolidation cycles.
*/
distillSessionContent(sessionDate: string, summary: string, keyPoints: string[]): MemoryFrame {
distillSessionContent(sessionDate: string, summary: string, keyPoints: string[]): MemoryFrame | null {
const parts = [`Session (${sessionDate}): ${summary}`];
if (keyPoints.length > 0) {
parts.push('Key points: ' + keyPoints.join('; '));
}
const content = parts.join('. ');
const components = [sessionDate, summary, ...keyPoints];
if ([content, components.join('\n'), components.join('')].some(
projection => evaluateExternalMemoryIngress({ content: projection }).action !== 'allow',
)) return null;
// Replace-on-update: re-distilling the same session (same date+summary,
// evolving key points) must update the one distilled frame. createIFrame's
@@ -230,25 +248,33 @@ export class MemoryWeaver {
}
consolidateProject(projectId: string): MemoryFrame | null {
if (evaluateExternalMemoryIngress({ content: projectId }).action !== 'allow') return null;
const projectSessions = this.sessions.getByProject(projectId);
const closedSessions = projectSessions.filter(s => s.status === 'closed' || s.status === 'archived');
if (closedSessions.length === 0) return null;
const allContent: string[] = [];
const sourceContent: string[] = [];
for (const session of closedSessions) {
const latestI = this.frames.getLatestIFrame(session.gop_id);
if (latestI) {
allContent.push(`[${session.gop_id}] ${latestI.content}`);
sourceContent.push(latestI.content);
}
}
if (allContent.length === 0) return null;
const consolidatedContent = allContent.join('\n---\n');
if ([consolidatedContent, sourceContent.join('\n'), sourceContent.join('')].some(
content => evaluateExternalMemoryIngress({ content }).action !== 'allow',
)) return null;
const consolidationSession = this.sessions.create(projectId);
return this.frames.createIFrame(
consolidationSession.gop_id,
allContent.join('\n---\n'),
consolidatedContent,
'important'
);
}