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): bound route-candidate parsing to the five the picture draws; add bench-pipeline (ADR-455)

ruvb0767e0df060
diff --git a/plugins/ruflo-console/hooks/data/pipeline.ts b/plugins/ruflo-console/hooks/data/pipeline.ts
index 5e8e22310e..284fd766bb 100644
--- a/plugins/ruflo-console/hooks/data/pipeline.ts
+++ b/plugins/ruflo-console/hooks/data/pipeline.ts
@@ -47,6 +47,9 @@ export function stageNote(stage: PipeStage): string {
return stage.state === 'stale' ? `stale ${ageWords(stage.ageMs)}` : `${ageWords(stage.ageMs)} ago`
}
+/** The route picture draws at most this many candidates, so the parser stops here: a long route query costs the same as a short one. */
+export const MAX_CANDIDATES = 5
+
export type RouteOwner = 'mods' | 'classic' | 'unseated'
export type RouteCandidate = { agent: string; /** 0..1, or null where the router printed none. */ confidence: number | null }
export type RouteSource = 'mods' | 'router-query' | 'none'
@@ -70,7 +73,8 @@ export function candidatesFromLines(lines: readonly string[]): RouteCandidate[]
const out: RouteCandidate[] = [{ agent: first[1] as string, confidence: percent(first[2]) }]
- for (const line of lines.slice(1)) {
+ for (let i = 1; i < lines.length && out.length < MAX_CANDIDATES; i++) {
+ const line = lines[i] as string
const alt = /^\s+or (\S+)(?: · (\d{1,3})%)?/.exec(line)
if (alt !== null) out.push({ agent: alt[1] as string, confidence: percent(alt[2]) })
diff --git a/plugins/ruflo-console/hooks/gfx/pipeline.ts b/plugins/ruflo-console/hooks/gfx/pipeline.ts
index e78f063a25..7ee0fc97cb 100644
--- a/plugins/ruflo-console/hooks/gfx/pipeline.ts
+++ b/plugins/ruflo-console/hooks/gfx/pipeline.ts
@@ -3,7 +3,7 @@
* functions of the models in data/pipeline.ts and a size; each returns a grid of exactly the size asked for. A stale stage
* is drawn dim, an absent one dim with n/a: no colour here means a number the console did not read.
*/
-import { stageNote, type PipeStage, type RouteModel, type RouteOwner } from '../data/pipeline'
+import { MAX_CANDIDATES, stageNote, type PipeStage, type RouteModel, type RouteOwner } from '../data/pipeline'
import { COLOR, Grid, ramp } from './raster'
export const PIPELINE_ROWS = 5
@@ -45,14 +45,14 @@ const OWNER_WORDS: Record<RouteOwner, { text: string; color: number }> = {
}
/** Rows the route picture needs: the owner line, one per candidate (at most five), and the source line. */
-export const routeRows = (model: RouteModel): number => 2 + Math.max(1, Math.min(5, model.candidates.length))
+export const routeRows = (model: RouteModel): number => 2 + Math.max(1, Math.min(MAX_CANDIDATES, model.candidates.length))
/** The last route: who owns routing, then each candidate with a confidence bar, the pick first and brightest. */
export function routePicture(model: RouteModel, columns: number): Grid {
const rows = routeRows(model)
const grid = new Grid(columns, rows)
const owner = OWNER_WORDS[model.owner]
- const shown = model.candidates.slice(0, 5)
+ const shown = model.candidates.slice(0, MAX_CANDIDATES)
const bar = Math.max(6, Math.min(24, columns - 34))
grid.text(0, 0, `â—† ${owner.text}`, owner.color)
diff --git a/plugins/ruflo-console/scripts/bench-pipeline.mjs b/plugins/ruflo-console/scripts/bench-pipeline.mjs
new file mode 100644
index 0000000000..d758ef9d76
--- /dev/null
+++ b/plugins/ruflo-console/scripts/bench-pipeline.mjs
@@ -0,0 +1,64 @@
+// @ts-check
+/**
+ * ADR-455 benchmark: the Learning page's pipeline diagram and route picture. The only input that scales is the route query's
+ * output (`nn-route` lines: one `or agent · N%` per runner-up), so size = lines handed to candidatesFromLines/routeModelOf. The
+ * four-stage diagram is fixed-size and measured once. Median of runs, p99, and approximate bytes allocated per call
+ * (heapUsed delta over a batch after gc(); run with --expose-gc, else n/a).
+ * NODE_OPTIONS=--expose-gc npx -y tsx plugins/ruflo-console/scripts/bench-pipeline.mjs [runs]
+ */
+import { candidatesFromLines, pipeStagesOf, routeModelOf } from '../hooks/data/pipeline.ts'
+import { pipelineDiagram, routePicture } from '../hooks/gfx/pipeline.ts'
+
+const RUNS = Math.max(20, Number(process.argv[2]) || 200)
+const NOW = Date.now()
+const gc = /** @type {(() => void) | undefined} */ (globalThis.gc)
+const linesOf = (/** @type {number} */ n) => ['→ coder · 87% · knn', ...Array.from({ length: n - 1 }, (_, i) => ` or agent-${i} · ${i % 100}%`)]
+const stat = (/** @type {number[]} */ samples) => {
+ const sorted = [...samples].sort((a, b) => a - b)
+ const at = (/** @type {number} */ q) => sorted[Math.min(sorted.length - 1, Math.floor(sorted.length * q))] ?? 0
+
+ return `median ${at(0.5).toFixed(4)} ms · p99 ${at(0.99).toFixed(4)} ms`
+}
+const measure = (/** @type {string} */ name, /** @type {() => unknown} */ fn) => {
+ for (let i = 0; i < 20; i++) fn()
+
+ const times = []
+
+ for (let i = 0; i < RUNS; i++) {
+ const start = process.hrtime.bigint()
+
+ fn()
+ times.push(Number(process.hrtime.bigint() - start) / 1e6)
+ }
+
+ let bytes = 'n/a'
+
+ if (gc !== undefined) {
+ gc()
+ const before = process.memoryUsage().heapUsed
+
+ for (let i = 0; i < 100; i++) fn()
+ bytes = `~${Math.round((process.memoryUsage().heapUsed - before) / 100 / 1024)} KiB`
+ }
+
+ console.log(`${name.padEnd(34)} ${stat(times)} · alloc/call ${bytes}`)
+}
+
+const stages = [{ name: 'RETRIEVE', count: 120 }, { name: 'JUDGE', count: 340 }, { name: 'DISTILL', count: 12 }, { name: 'CONSOLIDATE', count: 3 }]
+const at = { RETRIEVE: NOW - 60_000, JUDGE: NOW - 2 * 86_400_000, DISTILL: NOW - 5000 }
+
+console.log(`runs ${RUNS}`)
+measure('pipeStagesOf + diagram (4 stages)', () => pipelineDiagram(pipeStagesOf(stages, at, NOW), 110))
+
+for (const n of [1000, 10_000]) {
+ const lines = linesOf(n)
+ const lab = { id: 'nn-route', label: 'route', lines }
+ const input = { seated: true, classicOwnsRoute: false, route: null, lab }
+ const model = routeModelOf(input)
+
+ console.log(`-- ${n} route-query lines (model keeps ${model.candidates.length} candidates)`)
+ measure(`candidatesFromLines ${n}`, () => candidatesFromLines(lines))
+ measure(`routeModelOf ${n}`, () => routeModelOf(input))
+ measure(`routePicture ${n}`, () => routePicture(model, 110).toRaster('route'))
+ measure(`routeModelOf+routePicture ${n}`, () => routePicture(routeModelOf(input), 110).toRaster('route'))
+}
diff --git a/plugins/ruflo-console/tests/pipeline.spec.ts b/plugins/ruflo-console/tests/pipeline.spec.ts
index 1e70407402..b24579bf10 100644
--- a/plugins/ruflo-console/tests/pipeline.spec.ts
+++ b/plugins/ruflo-console/tests/pipeline.spec.ts
@@ -1,7 +1,7 @@
/** The learning pipeline diagram and the router picture (ADR-455): staleness is a clock rule, nothing is drawn that was not read. */
import { describe, expect, it } from 'vitest'
-import { candidatesFromLines, pipeStagesOf, routeModelOf, routeOwnerOf, stageNote, stageStateOf, STALE_AFTER_MS } from '../hooks/data/pipeline'
+import { candidatesFromLines, MAX_CANDIDATES, pipeStagesOf, routeModelOf, routeOwnerOf, stageNote, stageStateOf, STALE_AFTER_MS } from '../hooks/data/pipeline'
import { pipelineDiagram, routePicture, routeRows } from '../hooks/gfx/pipeline'
import { COLOR, type Grid } from '../hooks/gfx/raster'
import { newState } from '../hooks/state'
@@ -83,6 +83,14 @@ describe('the route model', () => {
expect(candidatesFromLines(['→ x · 900%'])[0]?.confidence).toBe(1)
})
+ it('stops at the five the picture draws, keeping the pick and the first runner-ups in order', () => {
+ const lines = ['→ a · 90%', ...Array.from({ length: 10_000 }, (_, i) => ` or r${i} · 5%`)]
+ const found = candidatesFromLines(lines)
+
+ expect(found).toHaveLength(MAX_CANDIDATES)
+ expect(found.map(c => c.agent)).toEqual(['a', 'r0', 'r1', 'r2', 'r3'])
+ })
+
it('prefers a route query with candidates, else the mod pick, else says none', () => {
const lab = { id: 'nn-route', label: 'which agent for "fix"', lines: ['→ coder · 80%', ' or tester · 10%'] }