Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(memory): enforce TTL when retrieving and searching live rows

Rudy Celeklif2a8a415429b
diff --git a/v3/@claude-flow/cli/__tests__/memory-ttl-visibility.test.ts b/v3/@claude-flow/cli/__tests__/memory-ttl-visibility.test.ts
new file mode 100644
index 0000000000..9d5d0567e5
--- /dev/null
+++ b/v3/@claude-flow/cli/__tests__/memory-ttl-visibility.test.ts
@@ -0,0 +1,54 @@
+import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+import Database from 'better-sqlite3';
+import { mkdtempSync, rmSync } from 'node:fs';
+import { join } from 'node:path';
+import { tmpdir } from 'node:os';
+vi.mock('../src/memory/intelligence.js',()=>({recordSignalProcessed(){}}));
+import * as bridge from '../src/memory/memory-bridge.js';
+let db:Database.Database,root:string,dbPath:string;
+beforeEach(()=>{
+ root=mkdtempSync(join(tmpdir(),'ruflo-ttl-'));dbPath=join(root,'memory.db');db=new Database(dbPath);
+ const cache=new Map();bridge.__setMemoryBridgeRegistryForTests({getAgentDB:()=>({database:db}),get:(name:string)=>name==='tieredCache'?cache:null});
+});
+afterEach(()=>{vi.restoreAllMocks();vi.unstubAllEnvs();bridge._resetRegistryCacheForTest();if(db.open)db.close();rmSync(root,{recursive:true,force:true});});
+async function seed(){
+ const now=Date.now();vi.spyOn(Date,'now').mockReturnValue(now-2000);
+ await bridge.bridgeStoreEntry({key:'expired',value:'searchable',ttl:1,generateEmbeddingFlag:false,dbPath});
+ vi.restoreAllMocks();
+ await bridge.bridgeStoreEntry({key:'retained',value:'searchable',generateEmbeddingFlag:false,dbPath});
+ db.prepare('UPDATE memory_entries SET embedding = ?, embedding_dimensions = 3').run('[1,0,0]');
+}
+it('excludes expired TTL rows from native retrieve, list, lexical search and vector scans',async()=>{
+ await seed();
+ expect(await bridge.bridgeGetEntry({key:'expired',dbPath})).toMatchObject({found:false});
+ const list=await bridge.bridgeListEntries({dbPath});expect(list).toMatchObject({total:1});expect(list!.entries.map(e=>e.key)).toEqual(['retained']);
+ const search=await bridge.bridgeSearchEntries({query:'searchable',threshold:0,dbPath});expect(search!.results.map(e=>e.key)).toEqual(['retained']);
+ const vectors=await bridge.bridgeGetAllEmbeddings({dimensions:3,dbPath});expect(vectors!.map(e=>e.key)).toEqual(['retained']);
+});
+it('expires a cached row without requiring another database write',async()=>{
+ await bridge.bridgeStoreEntry({key:'short',value:'searchable',ttl:1,generateEmbeddingFlag:false,dbPath});
+ expect(await bridge.bridgeGetEntry({key:'short',dbPath})).toMatchObject({found:true});
+ vi.spyOn(Date,'now').mockReturnValue(Date.now()+2000);
+ expect(await bridge.bridgeGetEntry({key:'short',dbPath})).toMatchObject({found:false});
+});
+it('applies the same expiration rule to WASM retrieve and list',async()=>{
+ await seed();db.close();bridge._resetRegistryCacheForTest();vi.stubEnv('CLAUDE_FLOW_DISABLE_BRIDGE','1');
+ const memory=await import('../src/memory/memory-initializer.js');
+ expect(await memory.getEntry({key:'expired',dbPath})).toMatchObject({found:false});
+ const list=await memory.listEntries({dbPath});expect(list).toMatchObject({total:1});expect(list.entries.map(e=>e.key)).toEqual(['retained']);
+});
+
+it.each(['native', 'wasm'])('keeps legacy rows without expires_at readable through %s',async backend=>{
+ await seed();db.exec('ALTER TABLE memory_entries DROP COLUMN expires_at');db.close();bridge._resetRegistryCacheForTest();
+ if(backend==='native') {
+ db=new Database(dbPath);
+ bridge.__setMemoryBridgeRegistryForTests({getAgentDB:()=>({database:db}),get:()=>null});
+ expect(await bridge.bridgeGetEntry({key:'retained',dbPath})).toMatchObject({found:true});
+ expect(await bridge.bridgeListEntries({dbPath})).toMatchObject({total:2});
+ } else {
+ vi.stubEnv('CLAUDE_FLOW_DISABLE_BRIDGE','1');
+ const memory=await import('../src/memory/memory-initializer.js');
+ expect(await memory.getEntry({key:'retained',dbPath})).toMatchObject({found:true});
+ expect(await memory.listEntries({dbPath})).toMatchObject({total:2});
+ }
+});
diff --git a/v3/@claude-flow/cli/src/memory/live-memory-row.ts b/v3/@claude-flow/cli/src/memory/live-memory-row.ts
new file mode 100644
index 0000000000..84c7d6d4dd
--- /dev/null
+++ b/v3/@claude-flow/cli/src/memory/live-memory-row.ts
@@ -0,0 +1,4 @@
+/** TTL is a read-time visibility rule, independent of background cleanup. */
+export function liveMemoryRowSql(): string {
+ return `(status = 'active' OR status IS NULL) AND (expires_at IS NULL OR expires_at > ${Date.now()})`;
+}
diff --git a/v3/@claude-flow/cli/src/memory/memory-bridge.ts b/v3/@claude-flow/cli/src/memory/memory-bridge.ts
index 9935b51e35..415e3adc67 100644
--- a/v3/@claude-flow/cli/src/memory/memory-bridge.ts
+++ b/v3/@claude-flow/cli/src/memory/memory-bridge.ts
@@ -17,6 +17,7 @@
* @module v3/cli/memory-bridge
*/
+import { liveMemoryRowSql } from './live-memory-row.js';
import * as path from 'path';
import * as crypto from 'crypto';
import { createRequire } from 'node:module';
@@ -782,6 +783,13 @@ export function ensureBridgeSchema(db: { exec: (sql: string) => unknown }): bool
throw err;
}
}
+ // Reads now enforce TTL, including on stores created before expires_at
+ // existed. A missing expiration means the legacy row does not expire.
+ try {
+ db.exec('ALTER TABLE memory_entries ADD COLUMN expires_at INTEGER');
+ } catch (error) {
+ if (!/duplicate column name:\s*expires_at/i.test(error instanceof Error ? error.message : String(error))) throw error;
+ }
db.exec(`CREATE INDEX IF NOT EXISTS idx_bridge_ns ON memory_entries(namespace)`);
db.exec(`CREATE INDEX IF NOT EXISTS idx_bridge_key ON memory_entries(key)`);
db.exec(`CREATE INDEX IF NOT EXISTS idx_bridge_status ON memory_entries(status)`);
@@ -1318,7 +1326,7 @@ export async function bridgeSearchEntries(options: {
const stmt = ctx.db.prepare(`
SELECT id, key, namespace, content, embedding, provenance_type
FROM memory_entries
- WHERE ${ACTIVE_MEMORY_ROW_SQL} ${whereExtra}
+ WHERE ${liveMemoryRowSql()} ${whereExtra}
ORDER BY updated_at DESC
LIMIT 1000
`);
@@ -1475,7 +1483,7 @@ export async function bridgeListEntries(options: {
// the `status = 'active'` filter matched zero. Treat NULL as
// "legacy-active" — the safe default for any entry that predates the
// status column.
- const statusFilter = ACTIVE_MEMORY_ROW_SQL;
+ const statusFilter = liveMemoryRowSql();
// Count
let total = 0;
@@ -1569,7 +1577,7 @@ export async function bridgeGetEntry(options: {
const cacheKey = `entry:${safeNs}:${safeKey}`;
dropCacheIfDbChangedElsewhere(registry, ctx);
const cached = await cacheGet(registry, cacheKey);
- if (cached && cached.content) {
+ if (cached && cached.content && (cached.expiresAt == null || cached.expiresAt > Date.now())) {
return {
success: true,
found: true,
@@ -1594,9 +1602,9 @@ export async function bridgeGetEntry(options: {
let row: any;
try {
const stmt = ctx.db.prepare(`
- SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at, tags
+ SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at, tags, expires_at
FROM memory_entries
- WHERE ${ACTIVE_MEMORY_ROW_SQL} AND key = ? AND namespace = ?
+ WHERE ${liveMemoryRowSql()} AND key = ? AND namespace = ?
LIMIT 1
`);
row = stmt.get(key, namespace);
@@ -1635,7 +1643,7 @@ export async function bridgeGetEntry(options: {
};
// Phase 2: Populate cache for next read
- await cacheSet(registry, cacheKey, entry);
+ await cacheSet(registry, cacheKey, { ...entry, expiresAt: row.expires_at });
return { success: true, found: true, cacheHit: false, entry };
} catch {
@@ -1973,7 +1981,7 @@ export async function bridgeSearchBruteForceCosine(
const stmt = ctx.db.prepare(`
SELECT id, key, namespace, content, embedding
FROM memory_entries
- WHERE status = 'active' AND embedding IS NOT NULL ${nsFilter}
+ WHERE ${liveMemoryRowSql()} AND embedding IS NOT NULL ${nsFilter}
ORDER BY updated_at DESC
LIMIT 10000
`);
@@ -3443,7 +3451,7 @@ export async function bridgeGetAllEmbeddings(options?: {
const rows: any[] = ctx.db.prepare(`
SELECT id, key, namespace, embedding
FROM memory_entries
- WHERE status = 'active' AND embedding IS NOT NULL
+ WHERE ${liveMemoryRowSql()} AND embedding IS NOT NULL
LIMIT ?
`).all(maxRows);
diff --git a/v3/@claude-flow/cli/src/memory/memory-initializer.ts b/v3/@claude-flow/cli/src/memory/memory-initializer.ts
index 634c4df7ea..ebd6ce5633 100644
--- a/v3/@claude-flow/cli/src/memory/memory-initializer.ts
+++ b/v3/@claude-flow/cli/src/memory/memory-initializer.ts
@@ -9,6 +9,7 @@
* @module v3/cli/memory-initializer
*/
+import { liveMemoryRowSql } from './live-memory-row.js';
import * as fs from 'fs';
import * as path from 'path';
import { AsyncLocalStorage } from 'node:async_hooks';
@@ -3193,8 +3194,8 @@ export async function searchEntries(options: {
// query (no extra round-trip) so a provenance-filtered search
// still gets RaBitQ's speedup instead of falling back to brute
// force.
- const stmt = db.prepare('SELECT content, embedding, provenance_type FROM memory_entries WHERE id = ? AND status = ?');
- stmt.bind([candidate.id, 'active']);
+ const stmt = db.prepare(`SELECT content, embedding, provenance_type FROM memory_entries WHERE id = ? AND ${liveMemoryRowSql()}`);
+ stmt.bind([candidate.id]);
if (stmt.step()) {
const [content, embeddingJson, provenanceTypeVal] = stmt.get() as [string, string | null, string | null];
if (provenanceFilter?.length && !provenanceFilter.includes(provenanceTypeVal || 'unknown')) {
@@ -3256,29 +3257,28 @@ export async function searchEntries(options: {
const db = new SQL.Database(fileBuffer);
const provenanceByKey = new Map<string, string>();
for (const r of filtered) {
- const stmt = db.prepare('SELECT provenance_type FROM memory_entries WHERE namespace = ? AND key = ? LIMIT 1');
+ const stmt = db.prepare(`SELECT provenance_type FROM memory_entries WHERE namespace = ? AND key = ? AND ${liveMemoryRowSql()} LIMIT 1`);
stmt.bind([r.namespace, r.key]);
if (stmt.step()) {
- provenanceByKey.set(`${r.namespace}::${r.key}`, (stmt.get()[0] as string | null) || 'unknown');
+ provenanceByKey.set(JSON.stringify([r.namespace, r.key]), (stmt.get()[0] as string | null) || 'unknown');
}
stmt.free();
}
db.close();
filtered = filtered
- .map(r => ({ ...r, provenanceType: provenanceByKey.get(`${r.namespace}::${r.key}`) || 'unknown' }));
+ .filter(r => provenanceByKey.has(JSON.stringify([r.namespace, r.key])))
+ .map(r => ({ ...r, provenanceType: provenanceByKey.get(JSON.stringify([r.namespace, r.key])) || 'unknown' }));
if (provenanceFilter?.length) {
filtered = filtered.filter(r => provenanceFilter.includes(r.provenanceType!));
}
} catch {
- // A requested trust filter fails closed. Unfiltered callers retain
- // backward-compatible results with an explicit unknown label.
- filtered = provenanceFilter?.length
- ? []
- : filtered.map(r => ({ ...r, provenanceType: 'unknown' }));
+ // An ANN hit alone cannot prove the row is still live. Fall back
+ // to the authoritative SQL scan if liveness cannot be checked.
+ filtered = [];
}
}
- if (!provenanceFilter?.length || filtered.length >= limit) {
+ if (filtered.length >= limit) {
return {
success: true,
results: filtered.slice(0, limit),
@@ -3300,7 +3300,7 @@ export async function searchEntries(options: {
// Get entries with embeddings
// ADR-323: build the WHERE clause incrementally so namespace and
// provenance filters compose (both, either, or neither).
- const whereClauses = [`status = 'active'`];
+ const whereClauses = [liveMemoryRowSql()];
const whereParams: (string)[] = [];
if (effectiveNamespace !== 'all') {
whereClauses.push('namespace = ?');
@@ -3489,7 +3489,7 @@ export async function listEntries(options: {
// that predate the status column may have NULL after migration.
// See memory-bridge.ts:bridgeListEntries for full context.
// Get total count
- const whereClauses = [ACTIVE_MEMORY_ROW_SQL];
+ const whereClauses = [liveMemoryRowSql()];
const whereParams: string[] = [];
if (namespace) {
whereClauses.push('namespace = ?');
@@ -3665,7 +3665,7 @@ export async function getEntry(options: {
const getStmt = db.prepare(`
SELECT id, key, namespace, content, embedding, access_count, created_at, updated_at, tags
FROM memory_entries
- WHERE ${ACTIVE_MEMORY_ROW_SQL}
+ WHERE ${liveMemoryRowSql()}
AND key = ?
AND namespace = ?
LIMIT 1