🌊 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
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
perf(console): fix per-frame transcript re-parse, quadratic journal fold, repeated tail reads; add workflow and autopilot benchmarks (ADR-467)
ruv15933793f14f
diff --git a/plugins/ruflo-console/hooks/ap-live.ts b/plugins/ruflo-console/hooks/ap-live.tsindex 79ef7fba25..e5ecbc7c4c 100644--- a/plugins/ruflo-console/hooks/ap-live.ts+++ b/plugins/ruflo-console/hooks/ap-live.ts@@ -19,8 +19,8 @@ import { trackerOf } from './data/cost-ledger'import { evaluate, outcomesOf, promote, propose, reviewTrials, tierOf, tunablesFrom, lastHash } from './data/ap-adapt'import { AUTOPILOT_DIR, ENVELOPE_FILE, KILL_FILE, open, type Envelope, type Sealed, type Spend } from './data/ap-envelope'import { anatoleFact, classifyTask, effectOf, killSeen, preflightAll, spendArgvs, spendOf, type ToolCheck } from './data/ap-guard'-import { appendArgv, encodeLine, JOURNAL_FILE, JOURNAL_MAX_BYTES, parseJournal, touchArgv, type JournalEvent } from './data/ap-journal'-import { digestText, emptyLoop, foldJournal, skipSet, snapshotEvents, tick, type EffectFact, type Facts, type LoopState, type TaskFact } from './data/ap-loop'+import { appendArgv, encodeLine, JOURNAL_FILE, JOURNAL_MAX_BYTES, touchArgv, type JournalEvent } from './data/ap-journal'+import { digestText, emptyLoop, foldJournal, replayJournal, skipSet, snapshotEvents, tick, type EffectFact, type Facts, type LoopState, type TaskFact } from './data/ap-loop'import type { Preflight } from './data/ap-loop'import type { NoticeDraft } from './notices'@@ -111,10 +111,10 @@ export async function refreshAutopilot(state: State, host: Host, nowMs: number =store.error = journal.reason === 'missing' ? null : `the journal was not read: ${journal.reason}`store.journalBytes = journal.reason === 'too-large' ? (journal.size ?? JOURNAL_MAX_BYTES) : 0} else {- const parsed = parseJournal(journal.text)+ const replayed = replayJournal(journal.text)- store.loop = foldJournal(parsed.events)- store.badLines = parsed.bad+ store.loop = replayed.loop+ store.badLines = replayed.badstore.journalBytes = journal.text.lengthstore.error = null}diff --git a/plugins/ruflo-console/hooks/data/ap-journal.ts b/plugins/ruflo-console/hooks/data/ap-journal.tsindex 8a038ebca7..b6f70a5626 100644--- a/plugins/ruflo-console/hooks/data/ap-journal.ts+++ b/plugins/ruflo-console/hooks/data/ap-journal.ts@@ -145,8 +145,8 @@ const KEEP = new Set(['t', 'envHash', 'hash', 'prev', 'id', 'task', 'day', 'anat/** The line to append (with its newline). Free text is washed again here, so an event built from raw text is safe to write. */export function encodeLine(event: JournalEvent): string {- const clean = JSON.parse(JSON.stringify(event, (key, value: unknown) => (typeof value === 'string' && !KEEP.has(key) ? wash(value, 300) : value))) as JournalEvent- const line = JSON.stringify(clean)+ // One pass: the replacer washes each string as it is written (a parse of the washed text back into an object, to stringify it again, produced the same bytes).+ const line = JSON.stringify(event, (key, value: unknown) => (typeof value === 'string' && !KEEP.has(key) ? wash(value, 300) : value))return `${line.length > LINE_MAX ? JSON.stringify({ t: 'pause', at: event.at, reason: 'an event was too long to journal' }) : line}\n`}diff --git a/plugins/ruflo-console/hooks/data/ap-loop.ts b/plugins/ruflo-console/hooks/data/ap-loop.tsindex 2f7ad21cf3..ab32bc81c0 100644--- a/plugins/ruflo-console/hooks/data/ap-loop.ts+++ b/plugins/ruflo-console/hooks/data/ap-loop.ts@@ -2,6 +2,8 @@* The autopilot's step machine (ADR-466 §2, §4, §5). Pure: `foldJournal(events)` is the whole state, `tick(state, facts)` answers what* to journal and whether to start one step. No I/O, no clock (the time is in `facts`), so every rule is testable and a crash can only* lose what was not journaled yet.+ * The one thing held between calls is `replayJournal`'s last answer, keyed by the journal's text: a result is a pure function of that text, and+ * `foldJournal` copies its base, so handing the same state out twice cannot be seen.** The order of the checks in `tick` IS the safety property: the kill switch is looked at before anything else and a stopped loop never* acts again without an explicit `start` event; an in-flight step is settled (finished, timed out, or found lost) before any new one@@ -9,7 +11,7 @@* and a task with a started or done step is never started again.*/import { classAllowed, pathAllowed, sha256, type Envelope, type Spend } from './ap-envelope'-import type { Anatole, JournalEvent, Receipt } from './ap-journal'+import { parseJournal, type Anatole, type JournalEvent, type Receipt } from './ap-journal'export type Phase = 'idle' | 'running' | 'paused' | 'stopped'export type StepRec = { id: string; task: string; cls: string; attempt: number; startedAt: number; deadline: number; status: 'started' | 'done' | 'failed'; verified?: boolean; why?: string; tier: string; par?: number; endedAt?: number }@@ -45,6 +47,22 @@ export const PAUSE_AT = 0.8/** The loop's state as the journal says it. `base` is a checkpoint to continue from. */export function foldJournal(events: readonly JournalEvent[], base: LoopState = emptyLoop()): LoopState {const s: LoopState = { ...base, steps: base.steps.map(step => ({ ...step })), parked: base.parked.map(p => ({ ...p })), receipts: [...base.receipts] }+ // Indexes over what the loop below looks up on every event. A scan per event made a fold quadratic (100k events took 10.8 s; bench-autopilot.mjs):+ // steps by id (ids are unique: a repeat is refused), the one unanswered park of an id, and parks by task in file order.+ const stepById = new Map<string, StepRec>()+ const openPark = new Map<string, ParkedRec>()+ const parksByTask = new Map<string, ParkedRec[]>()+ const indexPark = (p: ParkedRec): void => {+ const list = parksByTask.get(p.task)++ if (list === undefined) parksByTask.set(p.task, [p])+ else list.push(p)++ if (p.answer === undefined && !openPark.has(p.id)) openPark.set(p.id, p)+ }++ for (const step of s.steps) if (!stepById.has(step.id)) stepById.set(step.id, step)+ for (const p of s.parked) indexPark(p)for (const e of events) {switch (e.t) {@@ -53,18 +71,21 @@ export function foldJournal(events: readonly JournalEvent[], base: LoopState = ebreakcase 'step.started':// A repeated id (a replayed line) changes nothing: one step per id.- if (!s.steps.some(step => step.id === e.id)) {- s.steps.push({ id: e.id, task: e.task, cls: e.cls, attempt: e.attempt, startedAt: e.at, deadline: e.deadline, status: 'started', tier: e.tier, ...(e.par !== undefined && { par: e.par }) })+ if (!stepById.has(e.id)) {+ const made: StepRec = { id: e.id, task: e.task, cls: e.cls, attempt: e.attempt, startedAt: e.at, deadline: e.deadline, status: 'started', tier: e.tier, ...(e.par !== undefined && { par: e.par }) }- const once = s.parked.find(p => p.task === e.task && p.answer === 'once' && p.isUsed !== true)+ s.steps.push(made)+ stepById.set(made.id, made)++ const once = parksByTask.get(e.task)?.find(p => p.answer === 'once' && p.isUsed !== true)if (once !== undefined) once.isUsed = true}breakcase 'step.done': {- const step = s.steps.find(x => x.id === e.id && x.status === 'started')+ const step = stepById.get(e.id)- if (step !== undefined) {+ if (step !== undefined && step.status === 'started') {Object.assign(step, { status: 'done', verified: e.verified, endedAt: e.at })s.failures = 0}@@ -72,9 +93,9 @@ export function foldJournal(events: readonly JournalEvent[], base: LoopState = e}case 'step.failed': {- const step = s.steps.find(x => x.id === e.id && x.status === 'started')+ const step = stepById.get(e.id)- if (step !== undefined) {+ if (step !== undefined && step.status === 'started') {Object.assign(step, { status: 'failed', why: e.why, endedAt: e.at })s.failures += 1s.lastFailureAt = e.at@@ -83,12 +104,20 @@ export function foldJournal(events: readonly JournalEvent[], base: LoopState = e}case 'parked':- if (!s.parked.some(p => p.id === e.id && p.answer === undefined)) s.parked.push({ id: e.id, task: e.task, question: e.question, at: e.at })+ if (!openPark.has(e.id)) {+ const made: ParkedRec = { id: e.id, task: e.task, question: e.question, at: e.at }++ s.parked.push(made)+ indexPark(made)+ }breakcase 'answered': {- const held = s.parked.find(p => p.id === e.id && p.answer === undefined)+ const held = openPark.get(e.id)- if (held !== undefined) held.answer = e.answer+ if (held !== undefined) {+ held.answer = e.answer+ openPark.delete(e.id)+ }break}@@ -116,6 +145,23 @@ export function foldJournal(events: readonly JournalEvent[], base: LoopState = ereturn compactState(s)}+let lastReplay: { text: string; loop: LoopState; bad: number } | null = null++/**+ * A journal's text as the loop it says, and how many of its lines were not events. The console re-reads the journal on every tick (every minute, for weeks) and+ * the read cache hands back the SAME text while the file is unchanged, so the last answer is kept and a tick that finds nothing new replays nothing. The+ * state is never mutated by a caller (foldJournal copies its base), so handing the same object out twice is safe.+ */+export function replayJournal(text: string): { loop: LoopState; bad: number } {+ if (lastReplay !== null && lastReplay.text === text) return lastReplay++ const parsed = parseJournal(text)++ lastReplay = { text, loop: foldJournal(parsed.events), bad: parsed.bad }++ return lastReplay+}+/** Old finished steps are forgotten so weeks of running stay small; started steps and unanswered parks are never dropped. */export function compactState(s: LoopState): LoopState {const live = s.steps.filter(step => step.status === 'started')diff --git a/plugins/ruflo-console/hooks/data/wf-drill-io.ts b/plugins/ruflo-console/hooks/data/wf-drill-io.tsindex b119ed0d11..f6709cd6df 100644--- a/plugins/ruflo-console/hooks/data/wf-drill-io.ts+++ b/plugins/ruflo-console/hooks/data/wf-drill-io.ts@@ -37,6 +37,8 @@ export const isBound = (): boolean => bound !== nullexport function resetDrillIo(): void {bound = nullparsedMemo.clear()+ parsedChars = 0+ parseCount = 0tails.clear()notes.clear()results.clear()@@ -73,6 +75,36 @@ const hold = <V>(map: Map<string, V>, key: string, value: V, max = MAX_HELD): voexport const noteOf = (key: string): Note | undefined => notes.get(key)const parsedMemo = new Map<string, { src: string; isTail: boolean; parsed: Parsed }>()+/**+ * The parse memo is bounded by the text it holds, not by a count of files: a live project has 6 runs x 60 agents, and a memo of 24 entries was+ * walked past by every frame that asked for all of them (the search panel does), so each draw parsed ~340 transcripts again (bench-workflows.mjs:+ * 66 ms a frame). A parsed transcript is no bigger than its text, so this budget caps what the memo can keep; the oldest go first.+ */+export const PARSED_BUDGET_CHARS = 24_000_000+const PARSED_MAX_ENTRIES = 1024+let parsedChars = 0+let parseCount = 0++/** What the parse memo holds and how many parses it has done: what a test or a bench reads to prove a frame parsed nothing. */+export const parsedStats = (): { entries: number; chars: number; parses: number } => ({ entries: parsedMemo.size, chars: parsedChars, parses: parseCount })++function holdParsed(path: string, entry: { src: string; isTail: boolean; parsed: Parsed }): void {+ const old = parsedMemo.get(path)++ if (old !== undefined) parsedChars -= old.src.length++ parsedMemo.delete(path)+ parsedMemo.set(path, entry)+ parsedChars += entry.src.length++ for (const [key, held] of parsedMemo) {+ if (key === path || (parsedChars <= PARSED_BUDGET_CHARS && parsedMemo.size <= PARSED_MAX_ENTRIES)) break++ parsedMemo.delete(key)+ parsedChars -= held.src.length+ }+}+const tails = new Map<string, { text: string; size: number }>()export type Text = { text: string; isTail: boolean }@@ -105,7 +137,9 @@ export function parsedOf(cache: ReadCache, agent: WfAgent): Parsed | null {const parsed = parseActivity(text.text, text.isTail)- hold(parsedMemo, path, { src: text.text, isTail: text.isTail, parsed })+ parseCount += 1++ holdParsed(path, { src: text.text, isTail: text.isTail, parsed })return parsed}diff --git a/plugins/ruflo-console/hooks/data/wf-search.ts b/plugins/ruflo-console/hooks/data/wf-search.tsindex 91bcde9f89..0b05e73b3b 100644--- a/plugins/ruflo-console/hooks/data/wf-search.ts+++ b/plugins/ruflo-console/hooks/data/wf-search.ts@@ -27,6 +27,11 @@ export type SearchInput = {runs: readonly WfRun[]/** The transcript of an agent as already parsed, or null where it has not been read. */parsed: (agent: WfAgent) => Parsed | null+ /**+ * Whether the agent's transcript is in memory, WITHOUT parsing it. Once the scan cap stops the scan the remaining agents are only counted, and+ * counting must not cost a parse of each (a search over 360 large transcripts parsed all 360 to print two numbers). Absent: `parsed` is asked.+ */+ isHeld?: (agent: WfAgent) => booleantasks?: readonly MissionTaskRef[]}@@ -83,6 +88,13 @@ export function search(input: SearchInput, query: string): SearchResult {if (agent.ruflo !== undefined) continue+ if (out.isCapped && input.isHeld !== undefined) {+ if (input.isHeld(agent)) out.scanned += 1+ else if (agent.transcriptPath !== undefined) out.unread += 1++ continue+ }+const parsed = input.parsed(agent)if (parsed === null) {diff --git a/plugins/ruflo-console/hooks/data/workflows-read.ts b/plugins/ruflo-console/hooks/data/workflows-read.tsindex 6deab5d4dc..921bd9f6e4 100644--- a/plugins/ruflo-console/hooks/data/workflows-read.ts+++ b/plugins/ruflo-console/hooks/data/workflows-read.ts@@ -46,6 +46,23 @@ function factsOf(path: string, text: string, isTail: boolean): TranscriptFacts {return facts}+/**+ * What a tail-read transcript parsed to, by path, with the size and mtime the folder listing gave for the file. A transcript over the cap is+ * tail-read, and a refresh used to read 400 KB again from every one of them (360 agents: ~144 MB a tick) only to find the same text. While the+ * listing's size and mtime are unchanged the file is unchanged, as the read cache already assumes for whole files, so the facts are reused and the+ * read is skipped. The text itself is not kept (only the few figures parsed from it), and a listing with no mtime is never trusted.+ */+const tailFacts = new Map<string, { size: number; mtimeMs: number; facts: TranscriptFacts }>()++function rememberTail(path: string, size: number, mtimeMs: number | undefined, facts: TranscriptFacts): void {+ if (mtimeMs === undefined) return++ tailFacts.delete(path)+ tailFacts.set(path, { size, mtimeMs, facts })++ if (tailFacts.size > PARSED_MAX) tailFacts.delete(tailFacts.keys().next().value as string)+}+const safeList = (fs: ReaderFs, path: string) => fs.list(path).catch(() => [] as Awaited<ReturnType<ReaderFs['list']>>)export async function readWorkflowRuns(fs: WorkflowFs, cache: ReadCache, options: { configDir: string | null; cwd: string; nowMs: number; maxRuns?: number }): Promise<WorkflowRuns> {@@ -77,21 +94,36 @@ export async function readWorkflowRuns(fs: WorkflowFs, cache: ReadCache, optionsagentIds.map(async agentId => {const path = under(dir, `agent-${agentId}.jsonl`)const meta = await text(under(dir, `agent-${agentId}.meta.json`), 20_000)- const size = entries.find(entry => entry.name === `agent-${agentId}.jsonl`)?.size ?? 0+ const listed = entries.find(entry => entry.name === `agent-${agentId}.jsonl`)+ const size = listed?.size ?? 0let transcript: string | null = nulllet isTail = false+ let kept: TranscriptFacts | undefinedif (record === null && size > 0) {if (size <= TRANSCRIPT_CAP) transcript = await text(path, TRANSCRIPT_CAP)else if (fs.readTail !== undefined) {- transcript = await fs.readTail(path, TAIL_BYTES).catch(() => null)- isTail = transcript !== null+ const held = listed?.mtimeMs === undefined ? undefined : tailFacts.get(path)++ if (held !== undefined && held.size === size && held.mtimeMs === listed?.mtimeMs) {+ // Unchanged since the last read: its figures stand in for the text, which buildRun only ever turns into these.+ kept = held.facts+ transcript = ''+ isTail = true+ } else {+ transcript = await fs.readTail(path, TAIL_BYTES).catch(() => null)+ isTail = transcript !== null+ }}if (transcript === null) skipped.push(path)}- return [agentId, { meta, transcript, isTail, path, ...(transcript !== null && { facts: factsOf(path, transcript, isTail) }) }] as const+ const facts = kept ?? (transcript === null ? undefined : factsOf(path, transcript, isTail))++ if (kept === undefined && isTail && facts !== undefined) rememberTail(path, size, listed?.mtimeMs, facts)++ return [agentId, { meta, transcript, isTail, path, ...(facts !== undefined && { facts }) }] as const}),),)diff --git a/plugins/ruflo-console/hooks/views/wf-search.ts b/plugins/ruflo-console/hooks/views/wf-search.tsindex 128dd86d93..7db45b8cae 100644--- a/plugins/ruflo-console/hooks/views/wf-search.ts+++ b/plugins/ruflo-console/hooks/views/wf-search.ts@@ -8,29 +8,39 @@import type { RenderElement } from 'claude-code'import { MAX_QUERY, flatHits, HIT_NAME, HIT_ORDER, MAX_SHOWN, MIN_QUERY, SCAN_CHARS, search, type Hit, type MissionTaskRef, type SearchResult } from '../data/wf-search'-import { isBound, LOAD_BUDGET, loadRun, parsedOf } from '../data/wf-drill-io'+import { isBound, LOAD_BUDGET, loadRun, parsedOf, textOf } from '../data/wf-drill-io'import { button, clip, col, row, text, THEME } from './common'import { drillOf, jumpTo, setQuery } from './wf-detail'import { registerSlot, type SlotEnv } from './wf-slots'-const memo = new WeakMap<object, { query: string; sig: string; result: SearchResult }>()+const memo = new WeakMap<object, { query: string; runs: number; tasks: number; texts: (string | null)[]; tails: boolean[]; result: SearchResult }>()const tasksOf = (env: SlotEnv): MissionTaskRef[] =>(env.ctx.state.snapshot?.missions?.missions ?? []).flatMap(mission => mission.plan.tasks.map(task => ({ mission: mission.objective, id: task.id, title: task.title, status: task.status }))).slice(0, 400)-/** The search for this frame, kept while the query, the runs and what is parsed in memory are the same (a frame is drawn far more often than any of them changes). */+/**+ * The search for this frame, kept while the query, the runs and the transcript TEXT in memory are the same (a frame is drawn far more often than any of+ * them changes). The text is compared by identity, agent by agent: the read cache hands back the very same string while a file is unchanged, so+ * this is one pointer test each and nothing is parsed to find out that nothing changed. Parsing happens inside `search`, lazily, only as far as its scan cap goes.+ */export function resultOf(env: SlotEnv, query: string): SearchResult {+ // A query too short to search looks at nothing, so nothing is read or summed for it: the box is drawn on every frame, and most frames have no query.+ if (query.trim().slice(0, MAX_QUERY).length < MIN_QUERY) return search({ runs: [], parsed: () => null }, query)+const cache = env.ctx.state.cache- const parsed = env.runs.flatMap(run => run.phases.flatMap(phase => phase.agents)).map(agent => (agent.ruflo === undefined ? parsedOf(cache, agent) : null))- const sig = `${env.runs.length}:${parsed.map(p => p?.entries.length ?? -1).join(',')}:${tasksOf(env).length}`- const held = memo.get(env.ctx.state)+ const agents = env.runs.flatMap(run => run.phases.flatMap(phase => phase.agents))+ const held = agents.map(agent => (agent.ruflo === undefined ? textOf(cache, agent) : null))+ const texts = held.map(entry => entry?.text ?? null)+ const tails = held.map(entry => entry?.isTail === true)+ const tasks = tasksOf(env)+ const last = memo.get(env.ctx.state)- if (held !== undefined && held.query === query && held.sig === sig) return held.result+ if (last !== undefined && last.query === query && last.runs === env.runs.length && last.tasks === tasks.length && last.texts.length === texts.length && texts.every((text, i) => text === last.texts[i] && tails[i] === last.tails[i])) return last.result- const byAgent = new Map(env.runs.flatMap(run => run.phases.flatMap(phase => phase.agents)).map((agent, i) => [agent, parsed[i] ?? null] as const))- const result = search({ runs: env.runs, parsed: agent => byAgent.get(agent) ?? null, tasks: tasksOf(env) }, query)+ const index = new Map(agents.map((agent, i) => [agent, i] as const))+ const result = search({ runs: env.runs, parsed: agent => (agent.ruflo === undefined ? parsedOf(cache, agent) : null), isHeld: agent => texts[index.get(agent) ?? -1] != null, tasks }, query)- memo.set(env.ctx.state, { query, sig, result })+ memo.set(env.ctx.state, { query, runs: env.runs.length, tasks: tasks.length, texts, tails, result })return result}diff --git a/plugins/ruflo-console/scripts/bench-autopilot.mjs b/plugins/ruflo-console/scripts/bench-autopilot.mjsnew file mode 100644index 0000000000..8412db9686--- /dev/null+++ b/plugins/ruflo-console/scripts/bench-autopilot.mjs@@ -0,0 +1,182 @@+// @ts-check+/**+ * ADR-466 benchmark: the autopilot's pure core under the load a loop that runs for weeks puts on it. Plain node, every input is+ * generated here, nothing touches the project. Median and p99 wall time per case, plus approximate bytes allocated per call+ * (heapUsed delta after gc(); needs NODE_OPTIONS=--expose-gc, else n/a). Every case also prints a short digest of what it+ * computed, so a BEFORE and an AFTER run can be compared for the SAME answer, not only the same speed.+ * NODE_OPTIONS=--expose-gc npx -y tsx plugins/ruflo-console/scripts/bench-autopilot.mjs [--check]+ * `--check` exits 1 when a case misses its budget (the budgets are in BUDGET_MS below).+ * Cases: tick (the step machine, steps/s), journal parse + fold + encode + append at 8k lines (what one refresh replays: the+ * journal is rotated at ~1.2 MB) and at 100k lines (stress: the pure functions must stay linear), one refresh with an unchanged+ * journal (what the timer does every minute) and a state of 100k events replayed from a checkpoint.+ */+import { appendFileSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'+import { tmpdir } from 'node:os'+import { join } from 'node:path'++import { hashOf, sha256 } from '../hooks/data/ap-envelope.ts'+import { appendArgv, encodeLine, parseJournal } from '../hooks/data/ap-journal.ts'+import * as loop from '../hooks/data/ap-loop.ts'++const { foldJournal, tick, snapshotEvents, summarize } = loop+const replayJournal = /** @type {((text: string) => {loop: unknown}) | undefined} */ (/** @type {Record<string, unknown>} */ (loop).replayJournal)+const gc = /** @type {(() => void) | undefined} */ (globalThis.gc)+const ms = (/** @type {bigint} */ start) => Number(process.hrtime.bigint() - start) / 1e6+const T0 = Date.parse('2026-10-06T00:00:00.000Z')+const H = 3_600_000+const failed = /** @type {string[]} */ ([])+const check = process.argv.includes('--check')+/** Budgets a loop that wakes once a minute can afford: the work of ONE timer tick, in ms (median). */+const BUDGET_MS = { 'replay 8k lines (parse+fold)': 40, 'refresh, journal unchanged (8k)': 1, 'tick (one pass)': 0.25, 'fold 100k lines': 400, 'parse 100k lines': 400 }++const digest = (/** @type {unknown} */ value) => sha256(JSON.stringify(value)).slice(0, 12)++/** @type {(name: string, fn: () => unknown, iterations?: number, show?: (out: unknown) => string) => unknown} */+function bench(name, fn, iterations = 15, show = out => digest(out)) {+ let out = fn()++ for (let i = 0; i < 2; i++) out = fn()++ const times = []++ for (let i = 0; i < iterations; i++) {+ const start = process.hrtime.bigint()++ out = fn()+ times.push(ms(start))+ }++ times.sort((a, b) => a - b)++ const median = times[Math.floor(times.length / 2)] ?? 0+ let bytes = 'n/a'++ if (gc !== undefined) {+ gc()+ const before = process.memoryUsage().heapUsed++ for (let i = 0; i < 3; i++) fn()+ bytes = `~${Math.round(Math.max(0, process.memoryUsage().heapUsed - before) / 3 / 1024)} KiB`+ }++ const budget = /** @type {Record<string, number>} */ (BUDGET_MS)[name]+ const over = budget !== undefined && median > budget++ if (over) failed.push(`${name}: median ${median.toFixed(3)} ms > ${budget} ms`)++ console.log(`${name.padEnd(36)} median ${median.toFixed(3).padStart(10)} ms · p99 ${(times[Math.min(times.length - 1, Math.floor(times.length * 0.99))] ?? 0).toFixed(3).padStart(10)} ms · alloc/call ${bytes.padStart(10)} · digest ${show(out)}${over ? ' · OVER BUDGET' : ''}`)++ return out+}++const ENV = { name: 'night shift', toolClasses: ['edit', 'read', 'test'], paths: ['/work/repo'], repos: ['ruvnet/ruflo'], network: [], secretEnv: [], spend: { hourUsd: 2, dayUsd: 10, totalUsd: 40 }, concurrency: 2, maxDurationMs: 90 * 24 * H, verify: [['true']], acceptWithoutAnatole: false }+const envHash = hashOf(/** @type {never} */ (ENV))++/** A journal of about `n` events, shaped like weeks of running: steps that start and finish, a failure now and then, parks and answers, beats. */+function journalOf(/** @type {number} */ n) {+ const events = /** @type {import('../hooks/data/ap-journal.ts').JournalEvent[]} */ ([{ t: 'start', at: T0, envHash, revision: 1, anatole: 'on' }])+ let at = T0+ let i = 0++ while (events.length < n) {+ at += 20_000+ i += 1++ const sid = `s-${String(i).padStart(16, '0')}`+ const task = `t${i}`++ events.push({ t: 'step.started', at, id: sid, task, cls: 'edit', attempt: 1, deadline: at + 600_000, tier: 'mid', par: 1 })+ events.push(i % 11 === 0 ? { t: 'step.failed', at: at + 5000, id: sid, why: 'timed out' } : { t: 'step.done', at: at + 5000, id: sid, verified: i % 7 !== 0 })++ if (i % 13 === 0) events.push({ t: 'parked', at, id: `p-${i}`, task: `u${i}`, question: 'which class is it?' })+ if (i % 13 === 0) events.push({ t: 'answered', at: at + 1, id: `p-${i}`, answer: i % 2 ? 'once' : 'deny' })+ if (i % 5 === 0) events.push({ t: 'beat', at })+ }++ return events.slice(0, n)+}++const text8k = journalOf(8000).map(encodeLine).join('')+const events100k = journalOf(100_000)+const text100k = events100k.map(encodeLine).join('')++console.log(`node ${process.version} · journal 8k = ${(text8k.length / 1024).toFixed(0)} KiB · 100k = ${(text100k.length / 1024 / 1024).toFixed(1)} MiB (the console rotates at ~1.2 MiB)`)++// 1. The journal.+const parsed8 = /** @type {ReturnType<typeof parseJournal>} */ (bench('parse 8k lines', () => parseJournal(text8k), 15, out => String(/** @type {{events: unknown[]}} */ (out).events.length)))++bench('fold 8k events', () => foldJournal(parsed8.events), 15, out => digest(summarize(/** @type {never} */ (out), T0 + 90 * 24 * H)))+bench('replay 8k lines (parse+fold)', () => foldJournal(parseJournal(text8k).events), 15, out => digest(summarize(/** @type {never} */ (out), T0 + 90 * 24 * H)))++// What the timer does every minute: the read cache hands back the same text, so a journal that did not change is not replayed (absent before ADR-466 bench work: the line above is what it cost).+if (replayJournal !== undefined) bench('refresh, journal unchanged (8k)', () => replayJournal(text8k).loop, 50, out => digest(summarize(/** @type {never} */ (out), T0 + 90 * 24 * H)))++const parsed100 = /** @type {ReturnType<typeof parseJournal>} */ (bench('parse 100k lines', () => parseJournal(text100k), 5, out => String(/** @type {{events: unknown[]}} */ (out).events.length)))++bench('fold 100k lines', () => foldJournal(parsed100.events), 3, out => digest(summarize(/** @type {never} */ (out), T0 + 90 * 24 * H)))++// A checkpoint, then the next 100 events on top of it: what appendEvents does after every write.+const base = foldJournal(parsed100.events.slice(0, 99_900))++bench('fold 100 events onto a checkpoint', () => foldJournal(parsed100.events.slice(99_900), base), 30, out => digest(summarize(/** @type {never} */ (out), T0 + 90 * 24 * H)))++// 2. Encoding and appending.+bench('encodeLine x 1000', () => parsed100.events.slice(0, 1000).map(encodeLine).join('').length, 15, String)+bench('encodeLine x 100k', () => parsed100.events.map(encodeLine).join('').length, 3, String)++const dir = mkdtempSync(join(tmpdir(), 'ap-bench-'))++try {+ const file = join(dir, 'journal.jsonl')++ writeFileSync(file, '')++ // The console writes through `dd oflag=append` (a spawn per batch); this is the floor: the same bytes through one O_APPEND write.+ bench('append 100 events (O_APPEND floor)', () => (appendFileSync(file, parsed100.events.slice(0, 100).map(encodeLine).join('')), 100), 15, String)+ console.log(`${'append argv'.padEnd(36)} ${JSON.stringify(appendArgv('/j'))}`)+ bench('read + replay the appended file', () => foldJournal(parseJournal(readFileSync(file, 'utf8')).events), 7, out => digest(summarize(/** @type {never} */ (out), T0 + 90 * 24 * H)))+} finally {+ rmSync(dir, { recursive: true, force: true })+}++// 3. The step machine.+const facts = (/** @type {Record<string, unknown>} */ over = {}) => ({ nowMs: T0 + 5 * H, killSeen: false, envelope: ENV, anatole: 'on', spend: { hourUsd: 0, dayUsd: 0, totalUsd: 1 }, task: { id: 'tx', title: 'do tx', cls: 'edit', hardDeny: null, path: '/work/repo/src/a.ts' }, effects: {}, orphans: new Set(), tunables: { parallelism: 1, retries: 1, stepTimeoutMs: 600_000, tierOf: () => 'mid' }, preflight: {}, ...over })+const running = foldJournal([{ t: 'start', at: T0, envHash, revision: 1, anatole: 'on' }])+const busy = foldJournal(parsed8.events)++bench('tick (one pass)', () => tick(running, /** @type {never} */ (facts())), 200, out => digest(/** @type {{status: string}} */ (out).status))+bench('tick over 8k-event state', () => tick(busy, /** @type {never} */ (facts({ nowMs: T0 + 90 * 24 * H - H, task: { id: 'tz', title: 'do tz', cls: 'edit', hardDeny: null, path: '/work/repo/src/a.ts' } }))), 200, out => digest(/** @type {{status: string}} */ (out).status))++// Throughput: a tick, its events folded back, repeated; each step is done the next tick.+const throughput = (/** @type {number} */ steps) => {+ let s = running+ let at = T0 + H+ let done = 0++ for (let i = 0; i < steps; i++) {+ const open = s.steps.filter(step => step.status === 'started')+ const effects = Object.fromEntries(open.map(step => [step.id, 'done']))+ const d = tick(s, /** @type {never} */ (facts({ nowMs: at, effects, task: { id: `x${i}`, title: `do x${i}`, cls: 'edit', hardDeny: null, path: '/work/repo/src/a.ts' }, spend: { hourUsd: 0, dayUsd: 0, totalUsd: 1 } })))++ s = foldJournal(d.events, s)+ done += d.act === null ? 0 : 1+ at += 1000+ }++ return { done, phase: s.phase, steps: s.steps.length }+}+const started = process.hrtime.bigint()+const out = throughput(20_000)+const took = ms(started)++console.log(`${'step machine, tick+fold x 20000'.padEnd(36)} ${took.toFixed(1)} ms total · ${Math.round(20_000 / (took / 1000))} ticks/s · digest ${digest(out)} (${JSON.stringify(out)})`)++// 4. Memory of a long state: a replay of 100k events keeps KEEP_STEPS finished steps, not 33k.+const long = foldJournal(parsed100.events)++console.log(`${'state after 100k events'.padEnd(36)} steps kept ${long.steps.length} · parked ${long.parked.length} · receipts ${long.receipts.length} · snapshot ${snapshotEvents(long).length} events`)++if (check && failed.length > 0) {+ console.error(`over budget:\n ${failed.join('\n ')}`)+ process.exit(1)+}diff --git a/plugins/ruflo-console/scripts/bench-workflows.mjs b/plugins/ruflo-console/scripts/bench-workflows.mjsnew file mode 100644index 0000000000..726d8f50c6--- /dev/null+++ b/plugins/ruflo-console/scripts/bench-workflows.mjs@@ -0,0 +1,485 @@+// @ts-check+/**+ * ADR-458..466 benchmark: the Workflows page under load. Plain node, no fixtures on disk: every input is generated here with the shapes+ * Claude Code writes, and every page is drawn through the REAL page and slots into a recording kit (the same stand-in the other benches+ * use; the engine's own layout is not in it). Median and p99 per case, bytes allocated per call (heapUsed delta after gc(); needs+ * NODE_OPTIONS=--expose-gc, else n/a), and a digest of what the case computed so a BEFORE and an AFTER run can be compared for the same+ * ANSWER, not only the same speed.+ * NODE_OPTIONS=--expose-gc npx -y tsx plugins/ruflo-console/scripts/bench-workflows.mjs [--check] [--quick]+ * `--check` exits 1 when a case misses its budget (BUDGET_MS); `--quick` skips the 50 MB cases.+ * Cases: transcript parse at 1 / 3 / 5 / 50 MB; the folder reader at 1 / 20 / 200 runs on disk (cold, refreshed unchanged, one agent grown)+ * and with 5 MB / 50 MB transcripts (tail reads); a whole refresh with the page open and closed; the page frame (what one draw costs)+ * at 1 / 20 / 200 runs and with the drill open; search across every parsed transcript; a replay step; a conversation thread fed 1000 messages.+ */+import { newState } from '../hooks/state.ts'+import { parseActivity } from '../hooks/data/wf-activity.ts'+import { allRuns, pick } from '../hooks/data/workflows-nav.ts'+import { buildTimeline, boardAt, newReplay, stepReplay } from '../hooks/data/wf-replay.ts'+import { search } from '../hooks/data/wf-search.ts'+import { addMessage, newConvo } from '../hooks/data/wf-convo.ts'+import { parseTranscript } from '../hooks/data/workflows.ts'+import { readWorkflowRuns, slugOf, TAIL_BYTES } from '../hooks/data/workflows-read.ts'+import { bindDrill, loadTranscript, parsedOf, resetDrillIo } from '../hooks/data/wf-drill-io.ts'+import { workflowsActions } from '../hooks/wf-actions.ts'+import { refreshWorkflows, workflowsModelOf } from '../hooks/wf-live.ts'+import { conversationRows } from '../hooks/views/wf-convo.ts'+import { liveOf, wireConvo } from '../hooks/wf-convo-live.ts'+import { workflowsPage } from '../hooks/views/wf-page.ts'+import { resetSlots, slotsFor } from '../hooks/views/wf-slots.ts'+import { registerDrill, setQuery } from '../hooks/views/wf-detail.ts'+import { registerSearch } from '../hooks/views/wf-search.ts'+import { sha256 } from '../hooks/data/ap-envelope.ts'++const QUICK = process.argv.includes('--quick')+const CHECK = process.argv.includes('--check')+/** `--only parse,frame` runs those sections only (parse, reader, tail, refresh, frame, drill, search, replay, convo). */+const ONLY = process.argv.includes('--only') ? (process.argv[process.argv.indexOf('--only') + 1] ?? '').split(',') : null+/** `--iters N` overrides the iteration count of the frame cases (for a profile that the frame, not the set-up, dominates). */+const ITERS = process.argv.includes('--iters') ? Number(process.argv[process.argv.indexOf('--iters') + 1]) || 0 : 0+const want = (/** @type {string} */ name) => ONLY === null || ONLY.includes(name)+const gc = /** @type {(() => void) | undefined} */ (globalThis.gc)+const ms = (/** @type {bigint} */ start) => Number(process.hrtime.bigint() - start) / 1e6+const NOW = Date.parse('2026-10-06T02:00:00.000Z')+const iso = (/** @type {number} */ s) => new Date(NOW - 3_600_000 + s * 1000).toISOString()+const failed = /** @type {string[]} */ ([])+const digest = (/** @type {unknown} */ value) => sha256(JSON.stringify(value) ?? 'undefined').slice(0, 12)++/** What one draw of the page, or one timer refresh, may cost in this stand-in (ms, median): a terminal frame is ~16 ms and the page shares it with everything else. */+const BUDGET_MS = /** @type {Record<string, number>} */ ({+ 'frame: page, 200 runs on disk': 6,+ 'frame: page, drill open on an agent': 8,+ 'frame: page, search query active': 8,+ 'frame: conversation, 1000 messages': 3,+ 'refresh: page closed': 0.05,+ 'refresh: page open, nothing changed': 60,+ 'search: miss over 360 transcripts': 120,+ 'search: hit': 120,+ 'replay: boardAt, 2000 agents': 12,+})+const BUDGET_P99_MS = 16++/** @type {(name: string, fn: () => unknown, iterations?: number, show?: (out: unknown) => string) => unknown} */+function bench(name, fn, iterations = 15, show = out => digest(out)) {+ let out = fn()++ for (let i = 0; i < 2; i++) out = fn()++ const times = []++ for (let i = 0; i < iterations; i++) {+ const start = process.hrtime.bigint()++ out = fn()+ times.push(ms(start))+ }++ return report(name, times, out, show, () => void fn())+}++/** @type {(name: string, fn: () => Promise<unknown>, iterations?: number, show?: (out: unknown) => string) => Promise<unknown>} */+async function benchAsync(name, fn, iterations = 10, show = out => digest(out)) {+ let out = await fn()++ for (let i = 0; i < 1; i++) out = await fn()++ const times = []++ for (let i = 0; i < iterations; i++) {+ const start = process.hrtime.bigint()++ out = await fn()+ times.push(ms(start))+ }++ return report(name, times, out, show, null, fn)+}++/** @type {(name: string, times: number[], out: unknown, show: (out: unknown) => string, sync: (() => void) | null, async?: () => Promise<unknown>) => Promise<unknown> | unknown} */+function report(name, times, out, show, sync, async) {+ times.sort((a, b) => a - b)++ const median = times[Math.floor(times.length / 2)] ?? 0+ const p99 = times[Math.min(times.length - 1, Math.floor(times.length * 0.99))] ?? 0+ const budget = BUDGET_MS[name]+ const over = budget !== undefined && (median > budget || p99 > Math.max(BUDGET_P99_MS, budget * 3))+ const line = (/** @type {string} */ bytes) => {+ if (over) failed.push(`${name}: median ${median.toFixed(3)} ms (budget ${budget}), p99 ${p99.toFixed(3)} ms`)++ console.log(`${name.padEnd(46)} median ${median.toFixed(3).padStart(10)} ms · p99 ${p99.toFixed(3).padStart(10)} ms · alloc/call ${bytes.padStart(10)} · digest ${show(out)}${over ? ' · OVER BUDGET' : ''}`)++ return out+ }++ if (gc === undefined) return line('n/a')++ gc()++ const before = process.memoryUsage().heapUsed++ if (sync !== null) {+ for (let i = 0; i < 3; i++) sync()++ return line(`~${Math.round(Math.max(0, process.memoryUsage().heapUsed - before) / 3 / 1024)} KiB`)+ }++ return (async () => {+ for (let i = 0; i < 3; i++) await /** @type {() => Promise<unknown>} */ (async)()++ return line(`~${Math.round(Math.max(0, process.memoryUsage().heapUsed - before) / 3 / 1024)} KiB`)+ })()+}++// ---------------------------------------------------------------------------------------------------------- generated inputs++/** A transcript of about `bytes` characters: user/assistant turns, tool calls paired with results, streamed duplicates, the way Claude Code writes them. */+function transcriptOfBytes(/** @type {number} */ bytes, /** @type {number} */ salt = 0) {+ const out = /** @type {string[]} */ ([JSON.stringify({ type: 'user', timestamp: iso(0), message: { role: 'user', content: `task ${salt}` } })])+ let size = out[0]?.length ?? 0+ let i = 0++ while (size < bytes) {+ i += 1++ const usage = { input_tokens: 2, cache_creation_input_tokens: 500, cache_read_input_tokens: 100_000 + i, output_tokens: 50 }+ const call = { type: 'assistant', timestamp: iso(i), message: { id: `m${salt}-${i}`, model: 'claude-sonnet-5-5', role: 'assistant', content: [{ type: 'text', text: `step ${i}: look at the parser and run the tests` }, { type: 'tool_use', id: `t${salt}-${i}`, name: i % 3 ? 'Bash' : 'Read', input: i % 3 ? { command: `cargo test --lib module_${i % 40}`, description: 'run' } : { file_path: `/work/proj/src/mod_${i % 90}.ts` } }], usage } }+ const back = { type: 'user', timestamp: iso(i + 0.5), message: { role: 'user', content: [{ type: 'tool_result', tool_use_id: `t${salt}-${i}`, content: `ok ${'line of output '.repeat(6 + (i % 9))}` }] } }++ for (const entry of [call, call, back]) {+ const text = JSON.stringify(entry)++ out.push(text)+ size += text.length + 1+ }+ }++ return `${out.join('\n')}\n`+}++const PHASES = ['Build', 'Tune', 'Review', 'Ship']+const journalOf = (/** @type {number} */ agents) => [JSON.stringify({ type: 'launched' }), ...Array.from({ length: agents }, (_, i) => JSON.stringify({ type: 'started', key: `v2:a${i}`, agentId: `a${i}`, label: `agent ${i}`, phase: PHASES[i % 4] }))].join('\n') + '\n'+const CONFIG = '/home/u/.claude'+const CWD = '/work/proj'+const ROOT = `${CONFIG}/projects/${slugOf(CWD)}`+const SESSION = '11111111-2222-3333-4444-555555555555'++/** A run on a recorded disk: the newest `heavy` runs carry `agents` transcripts of `bytes` each, the rest only a journal and tiny agent files. */+function disk(/** @type {{ runs: number; heavy: number; agents: number; bytes: number; sizeOf?: number }} */ o) {+ /** @type {Map<string, { text: string; mtimeMs: number; size: number }>} */+ const files = new Map()+ /** @type {Map<string, Map<string, boolean>>} */+ const index = new Map()+ const transcripts = new Map(Array.from({ length: Math.min(o.agents, 8) }, (_, k) => [k, transcriptOfBytes(o.bytes, k)]))+ const put = (/** @type {string} */ path, /** @type {string} */ text, /** @type {number} */ mtimeMs, /** @type {number} */ size = text.length) => {+ files.set(path, { text, mtimeMs, size })++ const parts = path.split('/')++ for (let i = 1; i < parts.length; i++) {+ const parent = parts.slice(0, i).join('/') || '/'+ const kids = index.get(parent) ?? new Map()++ kids.set(/** @type {string} */ (parts[i]), i === parts.length - 1)+ index.set(parent, kids)+ }+ }++ for (let r = 0; r < o.runs; r++) {+ const heavy = r < o.heavy+ const n = heavy ? o.agents : 3+ const dir = `${ROOT}/${SESSION}/subagents/workflows/wf_r${String(r).padStart(3, '0')}`+ const mtime = NOW - r * 60_000++ put(`${dir}/journal.jsonl`, journalOf(n), mtime)++ for (let a = 0; a < n; a++) {+ put(`${dir}/agent-a${a}.meta.json`, JSON.stringify({ description: `agent ${a}`, workflowPhase: PHASES[a % 4] }), mtime)+ put(`${dir}/agent-a${a}.jsonl`, heavy ? (transcripts.get(a % 8) ?? '') : '{"type":"user","message":{"content":"x"}}\n', mtime, heavy && o.sizeOf !== undefined ? o.sizeOf : undefined)+ }+ }++ const tail = transcriptOfBytes(TAIL_BYTES, 99)+ const fs = {+ reads: { read: 0, tail: 0 },+ read: async (/** @type {string} */ path) => (fs.reads.read++, files.get(path)?.text ?? Promise.reject(new Error('ENOENT'))),+ stat: async (/** @type {string} */ path) => {+ const held = files.get(path)++ return held !== undefined ? { mtimeMs: held.mtimeMs, size: held.size, kind: 'file' } : index.has(path) ? { mtimeMs: NOW, kind: 'directory' } : Promise.reject(new Error('ENOENT'))+ },+ list: async (/** @type {string} */ path) => [...(index.get(path.replace(/\/+$/, '')) ?? new Map()).entries()].map(([name, isFile]) => ({ name, kind: isFile ? 'file' : 'dir', mtimeMs: files.get(`${path.replace(/\/+$/, '')}/${name}`)?.mtimeMs ?? NOW - 1000, size: files.get(`${path.replace(/\/+$/, '')}/${name}`)?.size ?? 0 })),+ readTail: async (/** @type {string} */ _path, /** @type {number} */ _bytes) => (fs.reads.tail++, tail),+ }++ return { fs, files, grow: (/** @type {string} */ path) => { const held = files.get(path); if (held !== undefined) files.set(path, { text: `${held.text}${JSON.stringify({ type: 'user', message: { content: 'more' } })}\n`, mtimeMs: held.mtimeMs + 1000, size: held.size + 50 }) } }+}++const summary = (/** @type {import('../hooks/data/workflows.ts').WfRun[]} */ runs) => runs.map(run => [run.id, run.state, run.total, run.done, run.running, run.totalTokens, run.phases.map(p => p.agents.map(a => [a.id, a.state, a.tokens, a.toolCalls, a.lastTool]))])++// ---------------------------------------------------------------------------------------------------------- a recording kit and a page+const element = (/** @type {string} */ kind) => (/** @type {Record<string, unknown>} */ props) => ({ kind, props })+const kit = /** @type {never} */ ({ Box: element('Box'), Text: element('Text'), Button: element('Button'), Input: element('Input'), Raster: element('Raster') })+const noop = () => undefined+const countOf = (/** @type {unknown} */ node) => {+ let n = 0+ const walk = (/** @type {unknown} */ value) => {+ if (Array.isArray(value)) value.forEach(walk)+ else if (typeof value === 'object' && value !== null) {+ n += 1+ walk(/** @type {{props?: {children?: unknown}}} */ (value).props?.children)+ }+ }++ walk(node)++ return n+}++function hostOver(/** @type {ReturnType<typeof disk>['fs']} */ fs) {+ return /** @type {never} */ ({ fs, invalidate: noop, run: async () => ({ exitCode: 1, stdout: '' }), every: noop, toast: noop, home: async () => '/home/u' })+}++async function worldOf(/** @type {ReturnType<typeof disk>} */ d) {+ const state = newState({})+ const host = hostOver(d.fs)++ state.view = 'workflows'+ state.configDir = CONFIG+ state.cwd = CWD+ state.isInteractive = false+ wireConvo(state, host)+ await refreshWorkflows(state, host, true, NOW)++ return { state, host }+}++function frameOf(/** @type {Awaited<ReturnType<typeof worldOf>>} */ world, columns = 110) {+ const act = /** @type {never} */ ({ workflows: workflowsActions(world.state, world.host, /** @type {never} */ ({ ask: noop })), focus: noop, view: noop })+ const ctx = /** @type {never} */ ({ kit, state: world.state, nowMs: NOW, columns, pictures: new Map(), act })++ return { ctx, tree: () => workflowsPage(ctx) }+}++// ---------------------------------------------------------------------------------------------------------- run+console.log(`node ${process.version} · 110 columns · ${QUICK ? 'quick (no 50 MB)' : 'full'}`)++// 1. Transcript parse, linear in the text. The product reads a transcript whole up to 3 MB and tails 400 KB past that; bigger sizes are the stress the parsers must survive.+console.log('\n# transcript parse')++for (const mb of want('parse') ? (QUICK ? [1, 3, 5] : [1, 3, 5, 50]) : []) {+ const text = transcriptOfBytes(mb * 1_000_000)+ const runs = mb >= 50 ? 1 : 5++ bench(`parseTranscript ${mb} MB`, () => parseTranscript(text), runs)+ bench(`parseActivity ${mb} MB`, () => { const p = parseActivity(text); return [p.entries.length, p.calls.length, p.dropped, p.files.length] }, runs)+}++// 2. The folder reader.+console.log('\n# folder reader (6 runs read, however many are on disk)')++for (const runs of want('reader') ? [1, 20, 200] : []) {+ const d = disk({ runs, heavy: 6, agents: 30, bytes: 60_000 })+ const cache = new Map()+ const opts = { configDir: CONFIG, cwd: CWD, nowMs: NOW }+ const read = () => readWorkflowRuns(/** @type {never} */ (d.fs), cache, opts)+ const start = process.hrtime.bigint()+ const first = await read()++ console.log(`${`reader ${runs} runs: first read (cold)`.padEnd(46)} ${ms(start).toFixed(1).padStart(10)} ms · ${first.runs.length} runs · ${first.runs[0]?.total ?? 0} agents a run · fs reads ${d.fs.reads.read}`)+ await benchAsync(`reader ${runs} runs: refresh, unchanged`, async () => summary((await read()).runs), 12)++ const victim = `${ROOT}/${SESSION}/subagents/workflows/wf_r000/agent-a0.jsonl`++ await benchAsync(`reader ${runs} runs: refresh, one agent grew`, async () => { d.grow(victim); return summary((await read()).runs) }, 12, () => 'n/a (grows)')+}++console.log('\n# folder reader, transcripts over the read cap (tail reads)')++for (const mb of want('tail') ? (QUICK ? [5] : [5, 50]) : []) {+ const d = disk({ runs: 6, heavy: 6, agents: 60, bytes: 2000, sizeOf: mb * 1_000_000 })+ const cache = new Map()+ const opts = { configDir: CONFIG, cwd: CWD, nowMs: NOW }+ const read = () => readWorkflowRuns(/** @type {never} */ (d.fs), cache, opts)++ await read()+ d.fs.reads.tail = 0+ await read()++ const tailReads = d.fs.reads.tail++ await benchAsync(`reader ${mb} MB x 360 agents: refresh, unchanged`, async () => summary((await read()).runs), 8)+ console.log(`${' '.repeat(46)} tail reads per refresh: ${tailReads} (each ${TAIL_BYTES / 1000} KB)`)+}++// 3. A whole refresh, open and closed.+console.log('\n# refresh through the page (clean + notices + after-read hooks)')++if (want('refresh')) {+ const d = disk({ runs: 20, heavy: 6, agents: 60, bytes: 60_000 })+ const world = await worldOf(d)+ const { state, host } = world+ let t = NOW++ await benchAsync('refresh: page open, nothing changed', async () => { await refreshWorkflows(state, host, true, (t += 1000)); return summary(state.wf.read?.runs ?? []) }, 12)++ state.view = 'overview'+ await benchAsync('refresh: page closed', async () => { await refreshWorkflows(state, host, true, (t += 1000)); return 'closed' }, 200, () => 'closed')+ state.view = 'workflows'+}++// 4. The page frame.+console.log('\n# page frame (what one draw costs)')++for (const runs of want('frame') ? [1, 20, 200] : []) {+ resetSlots()+ registerDrill()+ registerSearch()++ const world = await worldOf(disk({ runs, heavy: Math.min(6, runs), agents: 60, bytes: 20_000 }))+ const frame = frameOf(world)++ bench(`frame: page, ${runs === 200 ? '200 runs on disk' : `${runs} run${runs === 1 ? '' : 's'}`}`, () => countOf(frame.tree()), ITERS || 40, out => String(out))+}++if (want('drill')) {+ resetSlots()+ resetDrillIo()+ registerDrill()+ registerSearch()++ const world = await worldOf(disk({ runs: 6, heavy: 6, agents: 60, bytes: 400_000 }))++ bindDrill(world.state, /** @type {never} */ ({ fs: world.host.fs, run: world.host.run, invalidate: noop }))++ const model = workflowsModelOf(world.state, NOW)+ const here = pick(world.state.wf.ui, model?.runs ?? [])++ if (here.agent === null) throw new Error('no agent to open')++ await loadTranscript(here.agent)+ world.state.wf.ui = { ...here.ui, isInspecting: true }++ const frame = frameOf(world)+ const parsed = parsedOf(world.state.cache, here.agent)+ const find = (/** @type {unknown} */ node, /** @type {string} */ key, /** @type {unknown[]} */ out = []) => {+ if (Array.isArray(node)) node.forEach(child => find(child, key, out))+ else if (typeof node === 'object' && node !== null) {+ if (/** @type {{props?: {key?: string}}} */ (node).props?.key === key) out.push(node)+ find(/** @type {{props?: {children?: unknown}}} */ (node).props?.children, key, out)+ }++ return out+ }+ const envNow = () => {+ const m = workflowsModelOf(world.state, NOW)+ const h = pick(world.state.wf.ui, m?.runs ?? [])++ return /** @type {never} */ ({ ctx: frame.ctx, runs: m?.runs ?? [], run: h.run, phase: h.phase, agent: h.agent, ui: h.ui, nowMs: NOW })+ }+ const press = (/** @type {string} */ verb) => void slotsFor('key').find(slot => slot.id === `drill-${verb}`)?.run(envNow())++ // Open the drill and walk it down to the agent's Activity, as a person does (the same presses tests/wf-drill-search.spec.ts makes).+ const open = find(frame.tree(), 'wf-drill-open')[0]++ if (open === undefined) throw new Error('the drill has no open button')++ ;/** @type {{props: {onPress: () => void}}} */ (open).props.onPress()+ press('in')+ press('in')++ console.log(`${' '.repeat(46)} agent transcript: ${parsed?.entries.length ?? 0} entries kept (${parsed?.dropped ?? 0} older dropped)`)+ bench('frame: page, drill open on an agent', () => countOf(frame.tree()), ITERS || 40, out => String(out))++ // A query typed in the search box: every frame asks for the search of all 360 transcripts. The first draw parses them; after it, a frame must not.+ const typed = process.hrtime.bigint()++ setQuery(envNow(), 'module_7')+ countOf(frame.tree())+ console.log(`${'search typed: first draw (parses all)'.padEnd(46)} ${ms(typed).toFixed(1).padStart(10)} ms`)+ bench('frame: page, search query active', () => countOf(frame.tree()), ITERS || 40, out => String(out))+}++// 5. Search over every parsed transcript.+console.log('\n# search')++if (want('search')) {+ const d = disk({ runs: 6, heavy: 6, agents: 60, bytes: 200_000 })+ const world = await worldOf(d)+ const runs = allRuns(world.state.wf.read?.runs ?? [], null, [], NOW)+ const memo = new Map()+ const parsedFor = (/** @type {import('../hooks/data/workflows.ts').WfAgent} */ agent) => {+ const path = agent.transcriptPath++ if (path === undefined) return null++ const held = memo.get(path)++ if (held !== undefined) return held++ const text = d.files.get(path)?.text ?? null+ const parsed = text === null ? null : parseActivity(text)++ memo.set(path, parsed)++ return parsed+ }++ for (const run of runs) for (const phase of run.phases) for (const agent of phase.agents) parsedFor(agent)++ const brief = (/** @type {unknown} */ out) => { const r = /** @type {import('../hooks/data/wf-search.ts').SearchResult} */ (out); return `${r.scanned} scanned · ${Object.values(r.counts).join('/')} · ${r.chars} chars${r.isCapped ? ' capped' : ''}` }++ bench('search: miss over 360 transcripts', () => search({ runs, parsed: parsedFor }, 'zzqqxx-not-here'), 9, brief)+ bench('search: hit', () => search({ runs, parsed: parsedFor }, 'module_7'), 9, brief)+ bench('search: run name only (2 chars)', () => search({ runs: runs.slice(0, 1), parsed: () => null }, 'ag'), 40, brief)+}++// 6. Replay.+console.log('\n# replay')++for (const agents of want('replay') ? [60, 2000] : []) {+ const n = agents+ const input = { id: 'wf_replay', phases: PHASES }+ const run = /** @type {import('../hooks/data/workflows.ts').WfRun} */ (/** @type {unknown} */ ({+ id: input.id, name: 'replay', kind: 'workflow', state: 'completed', total: n, done: n, running: 0, failed: 0, idle: 0, totalTokens: n * 1000, isTokensPartial: false, hasRecord: true,+ phases: PHASES.map((title, p) => ({ title, done: 0, total: 0, running: 0, failed: 0, agents: Array.from({ length: Math.ceil(n / 4) }, (_, i) => { const k = i * 4 + p; return { id: `a${k}`, label: `agent ${k}`, phase: title, state: 'done', startedMs: NOW + k * 1000, elapsedMs: 30_000, tokens: 1000, toolCalls: 3, hasWorktree: false } }) })),+ }))+ const tl = buildTimeline(run)+ const mid = Math.floor(tl.events.length / 2)+ let ui = newReplay()++ bench(`replay: buildTimeline, ${agents} agents`, () => buildTimeline(run).events.length, 30, String)+ bench(`replay: boardAt, ${agents} agents`, () => { const b = boardAt(run, tl, mid); return [b.running, b.done, b.total] }, 30)+ bench(`replay: stepReplay next, ${agents} agents`, () => { ui = stepReplay(ui, tl, 'next', NOW); return ui.step > tl.events.length ? 0 : 1 }, 200, () => 'n/a')+}++// 7. A conversation thread fed 1000 messages (the thread keeps its newest 100, each up to 4000 characters).+console.log('\n# conversation')++if (want('convo')) {+ const state = newState({})+ const host = hostOver(disk({ runs: 1, heavy: 1, agents: 2, bytes: 1000 }).fs)++ wireConvo(state, host)++ const live = liveOf(state)+ const convo = newConvo()+ const body = 'a long answer with several sentences in it. '.repeat(90)++ bench('thread: add 1000 messages', () => { const c = newConvo(); for (let i = 0; i < 1000; i++) addMessage(c, 'claude', { atMs: NOW + i, who: i % 2 ? 'target' : 'you', text: `${i} ${body}`, state: i % 2 ? 'reply' : 'you', tokensIn: 10, tokensOut: 20 }); return [c.threads.get('claude')?.msgs.length, c.threads.get('claude')?.dropped] }, 15)++ for (let i = 0; i < 1000; i++) addMessage(convo, 'claude', { atMs: NOW + i, who: i % 2 ? 'target' : 'you', text: `${i} ${body}`, state: i % 2 ? 'reply' : 'you', tokensIn: 10, tokensOut: 20 })++ live.convo = convo+ live.convo.picked = 'claude'+ state.view = 'workflows'++ const act = /** @type {never} */ ({ workflows: { setUi: noop, ask: noop }, focus: noop, view: noop })+ const ctx = /** @type {never} */ ({ kit, state, nowMs: NOW, columns: 110, pictures: new Map(), act })+ const env = /** @type {never} */ ({ ctx, runs: [], run: null, phase: null, agent: null, ui: state.wf.ui, nowMs: NOW })++ bench('frame: conversation, 1000 messages', () => countOf(conversationRows(env)), 40, out => String(out))+}++if (CHECK && failed.length > 0) {+ console.error(`\nover budget:\n ${failed.join('\n ')}`)+ process.exit(1)+}diff --git a/plugins/ruflo-console/tests/bench-guards.spec.ts b/plugins/ruflo-console/tests/bench-guards.spec.tsnew file mode 100644index 0000000000..d62e676e2d--- /dev/null+++ b/plugins/ruflo-console/tests/bench-guards.spec.ts@@ -0,0 +1,403 @@+/**+ * What the benchmarks fixed, held in place (scripts/bench-workflows.mjs, scripts/bench-autopilot.mjs): the autopilot's journal fold stays linear and+ * says what the old quadratic one said, the journal encoder says the same bytes in one pass, an unchanged journal is not replayed, the parse memo holds+ * a live project's transcripts, a frame with no query parses nothing, the search stops parsing where its scan cap stops, and an unchanged oversized+ * transcript is not read again. Pure and fast (the one timing bound is 40x above the measured figure and 10x below the old one). Run with+ * npx vitest run plugins/ruflo-console/tests/bench-guards.spec.ts --testTimeout=30000+ */+import { describe, expect, it } from 'vitest'++import { hashOf } from '../hooks/data/ap-envelope'+import { encodeLine, parseJournal, wash, type JournalEvent } from '../hooks/data/ap-journal'+import { compactState, emptyLoop, foldJournal, replayJournal, type LoopState, type ParkedRec, type StepRec } from '../hooks/data/ap-loop'+import { parseActivity, type Parsed } from '../hooks/data/wf-activity'+import { PARSED_BUDGET_CHARS, parsedOf, parsedStats, resetDrillIo } from '../hooks/data/wf-drill-io'+import { search, SCAN_CHARS } from '../hooks/data/wf-search'+import { readWorkflowRuns } from '../hooks/data/workflows-read'+import type { WfAgent, WfRun } from '../hooks/data/workflows'+import { resultOf } from '../hooks/views/wf-search'+import { click, frame, inputOf, liveFiles, useDrill, worldOf } from './fixtures/wf-drill-world'++const T0 = Date.parse('2026-10-06T00:00:00.000Z')+const ENV_HASH = hashOf({ name: 'x', toolClasses: ['edit'], paths: ['/w'], repos: [], network: [], secretEnv: [], spend: { hourUsd: 1, dayUsd: 2, totalUsd: 3 }, concurrency: 1, maxDurationMs: 3_600_000, verify: [], acceptWithoutAnatole: false })++/** The fold as it was before the indexes: a scan of the step and park lists for every event. The reference the fast one must agree with. */+function slowFold(events: readonly JournalEvent[], base: LoopState = emptyLoop()): LoopState {+ const s: LoopState = { ...base, steps: base.steps.map(step => ({ ...step })), parked: base.parked.map(p => ({ ...p })), receipts: [...base.receipts] }++ for (const e of events) {+ switch (e.t) {+ case 'start':+ Object.assign(s, { phase: 'running', startedAtMs: e.at, envHash: e.envHash, revision: e.revision, anatole: e.anatole, reason: null, failures: 0, lastFailureAt: null })+ break+ case 'step.started':+ if (!s.steps.some(step => step.id === e.id)) {+ s.steps.push({ id: e.id, task: e.task, cls: e.cls, attempt: e.attempt, startedAt: e.at, deadline: e.deadline, status: 'started', tier: e.tier, ...(e.par !== undefined && { par: e.par }) })++ const once = s.parked.find(p => p.task === e.task && p.answer === 'once' && p.isUsed !== true)++ if (once !== undefined) once.isUsed = true+ }+ break+ case 'step.done': {+ const step = s.steps.find(x => x.id === e.id && x.status === 'started')++ if (step !== undefined) {+ Object.assign(step, { status: 'done', verified: e.verified, endedAt: e.at })+ s.failures = 0+ }+ break+ }+ case 'step.failed': {+ const step = s.steps.find(x => x.id === e.id && x.status === 'started')++ if (step !== undefined) {+ Object.assign(step, { status: 'failed', why: e.why, endedAt: e.at })+ s.failures += 1+ s.lastFailureAt = e.at+ }+ break+ }+ case 'parked':+ if (!s.parked.some(p => p.id === e.id && p.answer === undefined)) s.parked.push({ id: e.id, task: e.task, question: e.question, at: e.at })+ break+ case 'answered': {+ const held = s.parked.find(p => p.id === e.id && p.answer === undefined)++ if (held !== undefined) held.answer = e.answer+ break+ }+ case 'pause':+ if (s.phase === 'running') Object.assign(s, { phase: 'paused', reason: e.reason })+ break+ case 'resume':+ if (s.phase === 'paused') Object.assign(s, { phase: 'running', reason: null, failures: 0 })+ break+ case 'stop':+ Object.assign(s, { phase: 'stopped', reason: e.reason })+ break+ case 'beat':+ s.lastBeatAt = e.at+ break+ case 'adapt':+ s.receipts.push(e.receipt)+ break+ case 'digest':+ s.lastDigestDay = e.day+ break+ }+ }++ return compactState(s)+}++/** A seeded random journal that leans on every rule the indexes carry: repeated step ids, repeated parks of one id, answers before and after, `once` answers used by a later step of the task. */+function randomJournal(seed: number, n: number): JournalEvent[] {+ let x = seed++ const next = (): number => {+ x = (Math.imul(x, 1664525) + 1013904223) >>> 0++ return x / 2 ** 32+ }+ const pick = <T,>(list: readonly T[]): T => list[Math.floor(next() * list.length)] as T+ const events: JournalEvent[] = [{ t: 'start', at: T0, envHash: ENV_HASH, revision: 1, anatole: 'on' }]++ for (let i = 0; i < n; i++) {+ const at = T0 + i * 1000+ const sid = `s${Math.floor(next() * 40)}`+ const task = `t${Math.floor(next() * 25)}`+ const park = `p${Math.floor(next() * 15)}`+ const roll = next()++ if (roll < 0.25) events.push({ t: 'step.started', at, id: sid, task, cls: pick(['edit', 'read']), attempt: 1 + Math.floor(next() * 3), deadline: at + 60_000, tier: 'mid' })+ else if (roll < 0.4) events.push({ t: 'step.done', at, id: sid, verified: next() < 0.5 })+ else if (roll < 0.5) events.push({ t: 'step.failed', at, id: sid, why: 'timed out' })+ else if (roll < 0.65) events.push({ t: 'parked', at, id: park, task, question: 'which class?' })+ else if (roll < 0.8) events.push({ t: 'answered', at, id: park, answer: pick(['once', 'deny'] as const) })+ else if (roll < 0.85) events.push({ t: 'pause', at, reason: 'r' })+ else if (roll < 0.9) events.push({ t: 'resume', at })+ else if (roll < 0.95) events.push({ t: 'beat', at })+ else events.push({ t: 'digest', at, day: '2026-10-06' })+ }++ return events+}++describe('foldJournal', () => {+ it('agrees with the scan-per-event fold on random journals, from empty and from a checkpoint', () => {+ for (const seed of [1, 2, 3, 4, 5, 6, 7, 8]) {+ const events = randomJournal(seed, 600)++ expect(foldJournal(events)).toEqual(slowFold(events))++ const base = foldJournal(events.slice(0, 300))++ expect(foldJournal(events.slice(300), base)).toEqual(slowFold(events.slice(300), base))+ }+ })++ it('stays linear: 60,000 events fold in well under a second (the quadratic fold took about four)', () => {+ const events: JournalEvent[] = [{ t: 'start', at: T0, envHash: ENV_HASH, revision: 1, anatole: 'on' }]++ for (let i = 0; i < 30_000; i++) {+ events.push({ t: 'step.started', at: T0 + i, id: `s${i}`, task: `t${i}`, cls: 'edit', attempt: 1, deadline: T0 + i + 1000, tier: 'mid' })+ events.push({ t: 'step.done', at: T0 + i + 1, id: `s${i}`, verified: true })+ }++ const from = performance.now()+ const state = foldJournal(events)+ const took = performance.now() - from++ expect(state.steps).toHaveLength(400)+ expect(state.steps.every(step => step.status === 'done')).toBe(true)+ expect(took).toBeLessThan(1500)+ })++ it('keeps one step per id and never reopens a finished one', () => {+ const state = foldJournal([+ { t: 'start', at: T0, envHash: ENV_HASH, revision: 1, anatole: 'on' },+ { t: 'step.started', at: T0 + 1, id: 'a', task: 't', cls: 'edit', attempt: 1, deadline: T0 + 99, tier: 'mid' },+ { t: 'step.done', at: T0 + 2, id: 'a', verified: true },+ { t: 'step.started', at: T0 + 3, id: 'a', task: 't', cls: 'edit', attempt: 1, deadline: T0 + 99, tier: 'mid' },+ { t: 'step.failed', at: T0 + 4, id: 'a', why: 'late' },+ ])++ expect(state.steps.map((step: StepRec) => [step.id, step.status])).toEqual([['a', 'done']])+ expect(state.failures).toBe(0)+ })++ it('marks an `once` answer used by the first step of its task, and parks one open question per id', () => {+ const state = foldJournal([+ { t: 'start', at: T0, envHash: ENV_HASH, revision: 1, anatole: 'on' },+ { t: 'parked', at: T0 + 1, id: 'p', task: 't', question: 'q' },+ { t: 'parked', at: T0 + 2, id: 'p', task: 't', question: 'again' },+ { t: 'answered', at: T0 + 3, id: 'p', answer: 'once' },+ { t: 'step.started', at: T0 + 4, id: 's1', task: 't', cls: 'edit', attempt: 1, deadline: T0 + 99, tier: 'mid' },+ { t: 'step.started', at: T0 + 5, id: 's2', task: 't', cls: 'edit', attempt: 2, deadline: T0 + 99, tier: 'mid' },+ ])++ expect(state.parked.map((p: ParkedRec) => [p.id, p.answer, p.isUsed])).toEqual([['p', 'once', true]])+ })+})++describe('encodeLine', () => {+ it('writes the bytes the parse-and-restringify version wrote', () => {+ const legacy = (event: JournalEvent): string => {+ const keep = new Set(['t', 'envHash', 'hash', 'prev', 'id', 'task', 'day', 'anatole', 'answer', 'direction'])+ const clean = JSON.parse(JSON.stringify(event, (key, value: unknown) => (typeof value === 'string' && !keep.has(key) ? wash(value, 300) : value))) as JournalEvent+ const line = JSON.stringify(clean)++ return `${line.length > 2_000 ? JSON.stringify({ t: 'pause', at: event.at, reason: 'an event was too long to journal' }) : line}\n`+ }+ const samples: JournalEvent[] = [+ ...randomJournal(9, 80),+ { t: 'parked', at: T0, id: 'p1', task: 't', question: '\u001b[31mred\u001b[0m and spaced with key sk-ABCDEFGHIJKLMNOPQRSTUVWX' },+ { t: 'step.failed', at: T0, id: 's', why: 'x'.repeat(5000) },+ ]++ for (const event of samples) expect(encodeLine(event)).toBe(legacy(event))+ })+})++describe('replayJournal', () => {+ const text = randomJournal(11, 300).map(encodeLine).join('')++ it('is the parse and the fold, and hands the same answer back for the same text', () => {+ const first = replayJournal(text)++ expect(first.loop).toEqual(foldJournal(parseJournal(text).events))+ expect(first.bad).toBe(parseJournal(text).bad)+ expect(replayJournal(text)).toBe(first)+ expect(replayJournal(`${text}{"t":"beat","at":${T0 + 999_999}}\n`).loop.lastBeatAt).toBe(T0 + 999_999)+ expect(replayJournal(text).loop).toEqual(first.loop)+ })++ it('counts the lines that are not events', () => {+ expect(replayJournal(`${text}not json\n{"t":"nope","at":1}\n`).bad).toBe(2)+ })+})++describe('the parse memo', () => {+ const jsonl = (id: string): string => `${JSON.stringify({ type: 'user', timestamp: new Date(T0).toISOString(), message: { role: 'user', content: `task ${id}` } })}\n`+ const agentOf = (i: number): WfAgent => ({ id: `a${i}`, label: `agent ${i}`, phase: 'Build', state: 'running', transcriptPath: `/p/agent-a${i}.jsonl` }) as unknown as WfAgent++ it('keeps a live project of 340 transcripts parsed (it held 24), so a second pass parses nothing', () => {+ resetDrillIo()++ const cache = new Map<string, { mtimeMs: number; size: number; text: string }>()+ const agents = Array.from({ length: 340 }, (_, i) => agentOf(i))++ for (const [i, agent] of agents.entries()) cache.set(agent.transcriptPath as string, { mtimeMs: 1, size: 1, text: jsonl(String(i)) })++ const first = agents.map(agent => parsedOf(cache, agent))+ const parses = parsedStats().parses++ expect(parses).toBe(340)+ expect(agents.map(agent => parsedOf(cache, agent)).every((parsed, i) => parsed === first[i])).toBe(true)+ expect(parsedStats().parses).toBe(parses)+ resetDrillIo()+ })++ it('is bounded by the text it holds: the oldest go first past the budget, the newest stays', () => {+ resetDrillIo()++ const line = jsonl('x')+ const big = line.repeat(Math.ceil(9_000_000 / line.length))+ const cache = new Map<string, { mtimeMs: number; size: number; text: string }>()+ const agents = [0, 1, 2].map(agentOf)++ for (const agent of agents) cache.set(agent.transcriptPath as string, { mtimeMs: 1, size: 1, text: `${big}${agent.id}\n` })++ for (const agent of agents) parsedOf(cache, agent)++ expect(parsedStats().chars).toBeLessThanOrEqual(PARSED_BUDGET_CHARS)+ expect(parsedStats().entries).toBe(2)++ const before = parsedStats().parses++ parsedOf(cache, agents[2] as WfAgent)+ expect(parsedStats().parses).toBe(before)+ parsedOf(cache, agents[0] as WfAgent)+ expect(parsedStats().parses).toBe(before + 1)+ resetDrillIo()+ })+})++describe('the search panel on the page', () => {+ useDrill()++ it('parses nothing for a frame with no query, and once for a query however often it is drawn again', async () => {+ const world = await worldOf(liveFiles())++ frame(world)+ frame(world)++ // Nothing is searched, so nothing is parsed for it: at most the agent under the cursor is (the drill's own panel), never every agent of every run.+ const idle = parsedStats().parses++ expect(idle).toBeLessThanOrEqual(1)+ frame(world)+ expect(parsedStats().parses).toBe(idle)++ ;(inputOf(frame(world).tree)?.props.onInput as (v: string) => void)('ls -la')++ frame(world)++ const once = parsedStats().parses++ expect(once).toBeGreaterThan(idle)+ expect(once).toBeLessThanOrEqual(2)++ const again = frame(world)++ frame(world)+ expect(parsedStats().parses).toBe(once)+ expect(resultOf(again.env, 'ls -la')).toBe(resultOf(again.env, 'ls -la'))+ click(again.tree, 'wf-hit-tool-0')+ })+})++describe('search stops parsing where its cap stops scanning', () => {+ const entry = (text: string): Parsed['entries'][number] => ({ kind: 'message', index: 0, role: 'assistant', body: { text, total: text.length, isCut: false } })+ const parsedOfText = (text: string): Parsed => ({ entries: [entry(text)], calls: [], files: [], isTail: false, dropped: 0, skipped: 0 })+ const agent = (i: number): WfAgent => ({ id: `a${i}`, label: `agent ${i}`, phase: 'Build', state: 'done', transcriptPath: `/p/agent-a${i}.jsonl` }) as unknown as WfAgent+ const run = (agents: WfAgent[]): WfRun => ({ id: 'wf_x', name: 'run', kind: 'workflow', state: 'completed', total: agents.length, done: agents.length, running: 0, failed: 0, idle: 0, phases: [{ title: 'Build', agents, done: agents.length, total: agents.length, running: 0, failed: 0 }] }) as unknown as WfRun++ it('counts the agents past the cap without asking for their parse, and says the same numbers', () => {+ const agents = [agent(0), agent(1), agent(2), agent(3)]+ const huge = parsedOfText('x'.repeat(SCAN_CHARS + 10))+ const asked: string[] = []+ const input = { runs: [run(agents)], parsed: (a: WfAgent) => (asked.push(a.id), a.id === 'a3' ? null : huge) }+ const lazy = search({ ...input, isHeld: a => a.id !== 'a3' }, 'needle')+ const askedLazy = [...asked]++ asked.length = 0++ const eager = search(input, 'needle')++ expect(lazy).toEqual(eager)+ expect(lazy.isCapped).toBe(true)+ expect(lazy.scanned).toBe(3)+ expect(lazy.unread).toBe(1)+ expect(askedLazy).toEqual(['a0', 'a1'])+ expect(asked).toEqual(['a0', 'a1', 'a2', 'a3'])+ })+})++describe('the page search over transcripts in memory', () => {+ it('looks at no run, agent or text for a query too short to search (the box is drawn on every frame)', () => {+ const env = { ctx: { state: { cache: new Map(), snapshot: null } }, get runs(): never { throw new Error('the runs were walked') } } as never++ expect(resultOf(env, '')).toMatchObject({ isSearched: false, scanned: 0, chars: 0 })+ expect(resultOf(env, ' x ')).toMatchObject({ isSearched: false })+ })++ it('parses only as far as the scan cap goes and still counts every agent it did not read', () => {+ resetDrillIo()++ const line = JSON.stringify({ type: 'user', timestamp: new Date(T0).toISOString(), message: { role: 'user', content: 'word '.repeat(1200) } })+ const huge = `${Array.from({ length: Math.ceil(SCAN_CHARS / 6000) + 20 }, () => line).join('\n')}\n`+ const small = `${line}\n`+ const agents = [huge, small, small].map((text, i) => ({ id: `a${i}`, label: `agent ${i}`, phase: 'Build', state: 'done', transcriptPath: `/p/agent-a${i}.jsonl`, text }))+ const cache = new Map(agents.map(agent => [agent.transcriptPath, { mtimeMs: 1, size: 1, text: agent.text }]))+ const wfRun = { id: 'wf_x', name: 'run', kind: 'workflow', state: 'completed', total: 3, done: 3, running: 0, failed: 0, idle: 0, phases: [{ title: 'Build', agents, done: 3, total: 3, running: 0, failed: 0 }] } as unknown as WfRun+ const env = { ctx: { state: { cache, snapshot: null } }, runs: [wfRun] } as never+ const result = resultOf(env, 'zzzz-not-there')++ expect(result.isCapped).toBe(true)+ expect(result.scanned).toBe(3)+ expect(result.unread).toBe(0)+ expect(parsedStats().parses).toBe(1)+ expect(resultOf(env, 'zzzz-not-there')).toBe(result)+ expect(parsedStats().parses).toBe(1)+ resetDrillIo()+ })+})++describe('the folder reader and a transcript over the read cap', () => {+ const CONFIG = '/home/u/.claude'+ const DIR = `${CONFIG}/projects/-work-proj/11111111-2222-3333-4444-555555555555/subagents/workflows/wf_big`+ const tail = `ial line cut by the tail read\n${JSON.stringify({ type: 'assistant', timestamp: new Date(T0).toISOString(), message: { id: 'm1', model: 'claude-sonnet-5-5', role: 'assistant', content: [{ type: 'tool_use', id: 't', name: 'Bash' }], usage: { input_tokens: 1, cache_creation_input_tokens: 2, cache_read_input_tokens: 3, output_tokens: 4 } } })}\n`+ const files: Record<string, { text: string; size: number; mtimeMs: number }> = {+ [`${DIR}/journal.jsonl`]: { text: `${JSON.stringify({ type: 'launched' })}\n${JSON.stringify({ type: 'started', key: 'v2:a1', agentId: 'a1', label: 'one', phase: 'Build' })}\n`, size: 100, mtimeMs: T0 },+ [`${DIR}/agent-a1.meta.json`]: { text: JSON.stringify({ description: 'one', workflowPhase: 'Build' }), size: 60, mtimeMs: T0 },+ [`${DIR}/agent-a1.jsonl`]: { text: '', size: 50_000_000, mtimeMs: T0 },+ }+ const dirs = (path: string): string[] => [...new Set(Object.keys(files).filter(file => file.startsWith(`${path}/`)).map(file => file.slice(path.length + 1).split('/')[0] as string))]+ const fsOver = (counter: { tails: number }) => ({+ read: async (path: string) => files[path]?.text ?? Promise.reject(new Error('ENOENT')),+ stat: async (path: string) => (files[path] === undefined ? Promise.reject(new Error('ENOENT')) : { mtimeMs: files[path].mtimeMs, size: files[path].size, kind: 'file' as const }),+ list: async (path: string) => (dirs(path).length === 0 ? Promise.reject(new Error('ENOENT')) : dirs(path).map(name => ({ name, mtimeMs: files[`${path}/${name}`]?.mtimeMs ?? T0, size: files[`${path}/${name}`]?.size }))),+ readTail: async () => (counter.tails++, tail),+ })++ it('reads the tail once while the listing says the file is unchanged, again when it changes, and never trusts a listing with no mtime', async () => {+ const counter = { tails: 0 }+ const fs = fsOver(counter)+ const options = { configDir: CONFIG, cwd: '/work/proj', nowMs: T0 + 5000 }+ const read = () => readWorkflowRuns(fs as never, new Map(), options)+ const figures = (r: Awaited<ReturnType<typeof read>>) => r.runs.map(x => x.phases.map(p => p.agents.map(a => [a.tokens, a.toolCalls, a.lastTool, a.model])))+ const first = await read()++ expect(counter.tails).toBe(1)+ expect(figures(first)).toEqual([[[[10, 1, 'Bash', 'claude-sonnet-5-5']]]])++ expect(figures(await read())).toEqual(figures(first))+ expect(counter.tails).toBe(1)++ ;(files[`${DIR}/agent-a1.jsonl`] as { mtimeMs: number }).mtimeMs = T0 + 1000+ await read()+ expect(counter.tails).toBe(2)++ ;(files[`${DIR}/agent-a1.jsonl`] as { size: number }).size = 50_000_100+ await read()+ expect(counter.tails).toBe(3)++ const noMtime = { ...fs, list: async (path: string) => (await fs.list(path)).map(({ mtimeMs: _m, ...rest }) => rest) }++ await readWorkflowRuns(noMtime as never, new Map(), options)+ await readWorkflowRuns(noMtime as never, new Map(), options)+ expect(counter.tails).toBe(5)+ })+})diff --git a/v3/docs/adr/ADR-467-console-bench-optimizations.md b/v3/docs/adr/ADR-467-console-bench-optimizations.mdnew file mode 100644index 0000000000..7f9b28413f--- /dev/null+++ b/v3/docs/adr/ADR-467-console-bench-optimizations.md@@ -0,0 +1,66 @@+# ADR 467: Workflows page and autopilot: measured costs, the fixes, and the gate that holds them++Status: Accepted++Date: 2026-10-06++Builds on: ADR-458 (workflows reader), ADR-459 (drill and search), ADR-464 (page slots), ADR-466 (mission autopilot)++## 1. Context++The Workflows page is drawn on every frame while it is open and the autopilot ticks once a minute for weeks. Neither had been+measured under the load a real project and a long run put on them. `scripts/bench-workflows.mjs` and `scripts/bench-autopilot.mjs`+now do: every input is generated (no fixtures on disk), the page is drawn through the real page and slots into a recording kit+(the engine's own layout is not in it), and each case prints median, p99, bytes allocated and a digest of what it computed, so a+BEFORE and an AFTER run can be compared for the same answer and not only the same speed. All digests were identical across the change.++## 2. Findings and decisions++Measured on one host (node 22), BEFORE at 640abdaeb, AFTER on this branch. Frame cases: 6 runs x 60 agents of transcript in memory.++| Case | Before | After | Cause and fix |+|---|---|---|---|+| Page frame, 20 runs / 200 on disk | 64 / 61 ms | 0.06 / 0.08 ms | The parse memo held 24 transcripts; the search panel asked for all ~340 on every frame, so each draw parsed them again. The memo is now bounded by the text it holds (24 M characters), and a query too short to search reads nothing. |+| Page frame, drill open | 1488 ms | 0.07 ms | Same cause. |+| Page frame, a query typed | 1436 ms | 0.12 ms | Same cause; the search memo now compares the transcript text by identity, and `search` parses lazily only as far as its scan cap (new optional `isHeld` counts the rest without a parse). First draw after typing: 1537 ms to 327 ms. |+| Tail reads of transcripts over the read cap, per refresh (360 agents) | 360 x 400 KB | 0 | Each refresh re-read every oversized transcript. The listing's size and mtime now stand for "unchanged" and the parsed figures (not the text) are kept. |+| `foldJournal`, 100,000 events | 15.5 s (10.8 s earlier run) | 53 ms | A scan of the step and park lists per event (quadratic). Indexes by step id, open park id and park task. 8k events: 65 ms to 2 ms. |+| Replay of an 8k-line journal on each tick | 83 ms | 0.000 ms when unchanged (10 ms when changed) | The console replayed the journal every minute; the read cache returns the same text while the file is unchanged, so the last replay is kept (`replayJournal`, one slot, keyed by the text). |+| `encodeLine` x 100,000 | 209 ms | 131 ms | A parse of the washed text, to stringify it again, produced the same bytes: one pass. |++Not changed, with the figure: `parseTranscript` 5 MB 21 ms and `parseActivity` 5 MB 44 ms (linear, read only when the file changes); 50 MB+parses once in 0.3 / 0.6 s and is not a product path (a file over 3 MB is tail-read, 400 KB); a refresh with the page open, nothing+changed, 5 ms; closed, 0.000 ms; the reader at 200 runs on disk, 1.7 ms; replay `boardAt` at 2000 agents 0.6 ms; a conversation+thread fed 1000 messages (it keeps 100) draws in 0.04 ms, adding them costs 30 us each, nearly all `maskSecrets`; `tick` 0.02 ms, 10,000+tick+fold a second. The credential mask is the largest single cost left in every parse (23% of a profile) and is left alone: no cheaper+prefilter was proved to mask exactly what it masks.++## 3. The gate++Budgets are in the scripts (median ms for one draw or one timer tick; p99 within the larger of 16 ms or three times the budget):+page frame 6, drill frame 8, query-active frame 8, conversation frame 3, refresh closed 0.05, refresh open 60, search 120, replay+`boardAt` at 2000 agents 12, journal replay 40, `tick` 0.25, fold of 100k events 400. `--check` exits 1 on a miss.++ NODE_OPTIONS=--expose-gc npx -y tsx plugins/ruflo-console/scripts/bench-workflows.mjs --check [--quick] [--only frame,search]+ NODE_OPTIONS=--expose-gc npx -y tsx plugins/ruflo-console/scripts/bench-autopilot.mjs --check++`tests/bench-guards.spec.ts` holds the fixes without timing: the new fold agrees with the old scan-per-event fold on seeded random journals, the+encoder writes the old bytes, an unchanged journal is not replayed, a live project's transcripts stay parsed, a frame with no query parses+nothing, search stops parsing at its cap, and an unchanged oversized transcript is not read twice. Each of its guards was mutation-checked+(reverting the fix makes it fail). One test is timing-sensitive: 60,000 events fold in under 1.5 s (measured 40 ms).++## 4. Consequences++- No file format changed: the journal bytes are identical (a test proves it), the tail figures and the parse memo are in memory only.+- The tail skip needs the host's `fs.list` to return `mtimeMs` (the reader's own run ordering already does); without it the reads happen as before.+- `replayJournal` is one slot: two consoles with different journals in one process would replace each other's answer (correctly, never wrongly).+- The parse memo can hold up to 24 M characters of transcript text as parsed records.++## 5. Not done++A transcript that grows is still parsed whole when it changes (a refresh reads only unchanged files for free; a delta parse was not built).+Frame costs are of the recording kit, not the engine's layout.++## 6. Rollback++Revert the commit. Nothing else depends on it; the scripts and the spec go with it.