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

perf(console): memoize recall explain and per-refresh parses; add bench-recall (ADR-456)

ruva8544dfe130d
diff --git a/plugins/ruflo-console/hooks/data/recall.ts b/plugins/ruflo-console/hooks/data/recall.ts
index e49077d082..bc19830e17 100644
--- a/plugins/ruflo-console/hooks/data/recall.ts
+++ b/plugins/ruflo-console/hooks/data/recall.ts
@@ -92,8 +92,23 @@ export function parseRanked(text: string | null): Ranked | null {
export type Scored = { entry: RankedEntry; match: number; score: number }
-/** What the hook's getContext would surface for this prompt against these entries, best first; an empty list when nothing clears the threshold. */
+/** An entry's trigram set, built once per entry object: the render re-explains the picked prompt on every refresh (profiled: trigrams() was 70% of the time). */
+const gramsOf = new WeakMap<RankedEntry, Set<string>>()
+
+const entryGrams = (entry: RankedEntry): Set<string> => {
+ let grams = gramsOf.get(entry)
+
+ if (grams === undefined) gramsOf.set(entry, (grams = trigrams(entry.words)))
+
+ return grams
+}
+
+let lastExplain: { prompt: string; entries: readonly RankedEntry[]; out: Scored[] } | null = null
+
+/** What the hook's getContext would surface for this prompt against these entries, best first; an empty list when nothing clears the threshold. The last answer is kept for the same prompt over the same entries array (a new ranked parse is a new array). */
export function explainPrompt(prompt: string, entries: readonly RankedEntry[]): Scored[] {
+ if (lastExplain !== null && lastExplain.prompt === prompt && lastExplain.entries === entries) return lastExplain.out
+
const words = tokenize(prompt)
if (words.length === 0) return []
@@ -102,13 +117,17 @@ export function explainPrompt(prompt: string, entries: readonly RankedEntry[]):
const out: Scored[] = []
for (const entry of entries) {
- const match = jaccard(grams, trigrams(entry.words))
+ const match = jaccard(grams, entryGrams(entry))
const score = ALPHA * match + (1 - ALPHA) * entry.pageRank
if (score >= MIN_SCORE) out.push({ entry, match, score })
}
- return out.sort((a, b) => b.score - a.score).slice(0, TOP_K)
+ const best = out.sort((a, b) => b.score - a.score).slice(0, TOP_K)
+
+ lastExplain = { prompt, entries, out: best }
+
+ return best
}
/** One session's last recall: only ids are recorded (the prompt and the scores are not). */
@@ -194,6 +213,8 @@ export const recallOf = (snapshot: object | null | undefined): RecallFacts | nul
const status = (read: { text: string | null; reason?: string }): ReadStatus => (read.text !== null ? 'ok' : ((read.reason ?? 'missing') as ReadStatus))
let lastRanked: { text: string; parsed: Ranked | null } | null = null
+let lastModels: { text: string; parsed: NeuralPattern[] | null } | null = null
+let lastOutcomes: { text: string; parsed: PastPrompt[] } | null = null
/** Reads the four files (and stats the bank); never rejects. `fs.read` of a file over its cap is refused by readBounded. */
export async function readRecall(fs: ReaderFs, cache: ReadCache, cwd: string): Promise<RecallFacts> {
@@ -215,11 +236,14 @@ export async function readRecall(fs: ReaderFs, cache: ReadCache, cwd: string): P
// The ranked file is parsed again only when its text changed: the snapshot runs on a timer.
if (ranked.text !== null && lastRanked?.text !== ranked.text) lastRanked = { text: ranked.text, parsed: parseRanked(ranked.text) }
+ if (models.text !== null && lastModels?.text !== models.text) lastModels = { text: models.text, parsed: parseNeuralStore(models.text) }
+ if (outcomes.text !== null && lastOutcomes?.text !== outcomes.text) lastOutcomes = { text: outcomes.text, parsed: parsePrompts(outcomes.text) }
+
return {
ranked: ranked.text === null ? null : (lastRanked?.parsed ?? null),
sessions,
- prompts: parsePrompts(outcomes.text),
- neural: parseNeuralStore(models.text),
+ prompts: outcomes.text === null ? [] : (lastOutcomes?.parsed ?? []),
+ neural: models.text === null ? null : (lastModels?.parsed ?? null),
reads: { ranked: status(ranked), sessions: listed === null ? 'missing' : 'ok', prompts: status(outcomes), neural: status(models), bank: bankStat === undefined ? 'missing' : (bankStat.size ?? 0) > READ_MAX ? 'too-large' : 'ok' },
}
}
diff --git a/plugins/ruflo-console/scripts/bench-recall.mjs b/plugins/ruflo-console/scripts/bench-recall.mjs
new file mode 100644
index 0000000000..427c840522
--- /dev/null
+++ b/plugins/ruflo-console/scripts/bench-recall.mjs
@@ -0,0 +1,99 @@
+// @ts-check
+/**
+ * ADR-456 benchmark: the recall feature's data functions and row render at 1k and 10k ranked entries (the neural store is
+ * capped at 1000 patterns by the parser, so it is the same at both sizes). Median wall ms over N runs per step, and
+ * approximate bytes allocated per call (heapUsed delta after gc(); NODE_OPTIONS=--expose-gc, else n/a).
+ * NODE_OPTIONS=--expose-gc npx -y tsx plugins/ruflo-console/scripts/bench-recall.mjs [runs] [--profile]
+ * The ranked file is bounded by READ_MAX (2 MB) in the console, so the 10k size is also measured as raw text size.
+ */
+import { explainPrompt, lifecycleOrder, parseNeuralStore, parsePrompts, parseRanked, readRecall, wouldPrune } from '../hooks/data/recall.ts'
+import { lifecycleRows, pickPrompt, recallRows } from '../hooks/views/recall-rows.ts'
+import { newState } from '../hooks/state.ts'
+
+const RUNS = Math.max(5, Number(process.argv[2]) || 30)
+const NOW = 1_791_250_000_000
+const VOCAB = Array.from({ length: 600 }, (_, i) => `term${(i * 7919).toString(36)}word`)
+let seed = 42
+const rnd = () => ((seed = (seed * 1664525 + 1013904223) >>> 0) / 2 ** 32)
+const pick = () => VOCAB[Math.floor(rnd() * VOCAB.length)]
+
+const rankedText = (/** @type {number} */ n) =>
+ JSON.stringify({ computedAt: NOW - 5000, entries: Array.from({ length: n }, (_, i) => ({ id: `e-${i}`, summary: `pattern ${i} about ${pick()} and ${pick()}`, category: 'code', confidence: rnd(), pageRank: rnd() / 10, accessCount: i % 9, words: Array.from({ length: 12 }, pick) })) })
+const neuralText = JSON.stringify({ patterns: Object.fromEntries(Array.from({ length: 1000 }, (_, i) => [`p${i}`, { id: `pattern-${i}`, name: `n ${pick()}`, type: 'code', content: `content ${pick()} ${pick()}`, createdAt: new Date(NOW - i * 60_000).toISOString(), usageCount: i % 4, metadata: { verdict: 'success' } }])) })
+const outcomesText = JSON.stringify({ outcomes: Array.from({ length: 500 }, (_, i) => ({ task: `task ${i % 40} ${pick()}`, agent: 'coder', success: i % 2 === 0, timestamp: new Date(NOW - i * 1000).toISOString() })) })
+const PROMPT = `fix the ${pick()} handling in the ${pick()} ${pick()} module`
+
+const median = (/** @type {number[]} */ xs) => [...xs].sort((a, b) => a - b)[Math.floor(xs.length / 2)] ?? 0
+const gc = /** @type {(() => void) | undefined} */ (globalThis.gc)
+
+/** @returns {Promise<{ ms: number; kib: string }>} */
+async function measure(/** @type {() => unknown} */ fn) {
+ for (let i = 0; i < 3; i++) await fn()
+ const times = []
+
+ for (let i = 0; i < RUNS; i++) {
+ const t = process.hrtime.bigint()
+
+ await fn()
+ times.push(Number(process.hrtime.bigint() - t) / 1e6)
+ }
+
+ let kib = 'n/a'
+
+ if (gc !== undefined) {
+ gc()
+ const before = process.memoryUsage().heapUsed
+ const reps = 10
+
+ for (let i = 0; i < reps; i++) await fn()
+ kib = `${Math.round((process.memoryUsage().heapUsed - before) / reps / 1024)}`
+ }
+
+ return { ms: median(times), kib }
+}
+
+const row = (/** @type {string} */ name, /** @type {{ ms: number; kib: string }} */ r) => console.log(`${name.padEnd(34)} median ${r.ms.toFixed(3).padStart(9)} ms · ~${r.kib.padStart(6)} KiB/call`)
+
+const element = (/** @type {string} */ type) => (/** @type {Record<string, unknown>} */ props) => ({ type, props })
+const kit = /** @type {never} */ ({ Box: element('Box'), Text: element('Text'), Button: element('Button'), Input: element('Input'), Raster: element('Raster') })
+const act = /** @type {never} */ (new Proxy({}, { get: () => () => undefined }))
+
+console.log(`runs per step: ${RUNS} · gc ${gc === undefined ? 'unavailable' : 'exposed'}`)
+
+for (const size of [1000, 10_000]) {
+ const text = rankedText(size)
+ const ranked = parseRanked(text)
+
+ if (ranked === null) throw new Error('ranked did not parse')
+
+ const files = /** @type {Record<string, string>} */ ({ '/w/.claude-flow/data/ranked-context.json': text, '/w/.claude-flow/neural/models.json': neuralText, '/w/.claude-flow/routing-outcomes.json': outcomesText })
+ const fs = {
+ read: async (/** @type {string} */ p) => files[p] ?? Promise.reject(new Error('ENOENT')),
+ stat: async (/** @type {string} */ p) => (files[p] !== undefined ? { mtimeMs: 1, size: files[p].length, kind: 'file' } : undefined),
+ list: async () => [],
+ }
+ const cache = new Map()
+ const facts = await readRecall(fs, cache, '/w')
+ const state = newState({})
+
+ state.cwd = '/w'
+ state.snapshot = /** @type {never} */ ({ recall: { ...facts, ranked } })
+
+ const ctx = /** @type {never} */ ({ kit, state, nowMs: NOW, columns: 140, pictures: new Map(), act })
+
+ console.log(`\n== ${size} ranked entries (${(text.length / 1e6).toFixed(2)} MB of text${text.length > 2_000_000 ? ', over the 2 MB READ_MAX: the console would not read it' : ''}) ==`)
+ row('parseRanked', await measure(() => parseRanked(text)))
+ row('readRecall (cache warm, refresh)', await measure(() => readRecall(fs, cache, '/w')))
+ let k = 0
+ row('explainPrompt (same prompt, repeat)', await measure(() => explainPrompt(PROMPT, ranked.entries)))
+ row('explainPrompt (new prompt each call)', await measure(() => explainPrompt(`${PROMPT} ${k++}x${pick()}`, ranked.entries)))
+ row('parseNeuralStore (1000)', await measure(() => parseNeuralStore(neuralText)))
+ row('parsePrompts (500)', await measure(() => parsePrompts(outcomesText)))
+ row('lifecycleOrder + wouldPrune', await measure(() => { const p = parseNeuralStore(neuralText) ?? []; lifecycleOrder(p); wouldPrune(p, 1) }))
+ pickPrompt(null)
+ row('recallRows (no prompt picked)', await measure(() => recallRows(ctx)))
+ pickPrompt(PROMPT)
+ row('recallRows (prompt picked)', await measure(() => recallRows(ctx)))
+ pickPrompt(null)
+ row('lifecycleRows', await measure(() => lifecycleRows(ctx)))
+}