Files
ruflo/plugins/ruflo-console/hooks/views/learning.ts
T
Dragan SpiridonovandRuflo & AQE d89973b4dc fix(console): draw pictures on Claude Code Desktop as Svg, decide by surface
On Claude Code Desktop every picture was missing: no banner, no title
art, no topology, map, gauge or chart, and not even the text lines meant
to stand in for them. The host completes every resolved table with every
element name, and on the desktop its Raster draws an empty fragment, so
`kit.Raster !== undefined` always held and the console drew empty boxes.

- kitForSurface(): the terminal gets its Raster and no Svg; every other
  surface (desktop, vscode, mobile) gets Svg and no Raster. Applied to the
  Pane and the band. On the terminal nothing changes: the same Rasters,
  keys, text and fallbacks.
- gfx/svg.ts: the same Grid as one SVG document. Every Block Elements
  glyph (U+2580 to U+259F) and every braille glyph is drawn as
  rectangles; other glyphs are placed cell by cell (a control character,
  a value past Unicode or a character XML cannot hold is a blank cell);
  colours are kept, the default background stays transparent, text is
  escaped. A picture that would pass the host's 131,072-character Svg
  limit is not sent, so one large picture cannot invalidate the whole
  tree; it then falls back as on a surface with no pictures (its text line
  where it has one).
- A surface that draws Svg gets still frames, since nothing blits an Svg:
  nothing strikes in and the motion stops, as with fps 0, and the
  decorative light (the header's sweep, the title's shimmer, the palette's
  sweep) is off instead of caught mid-way. The boot keeps its real time so
  its progress shows, and a picture of data is drawn as of the render. The
  terminal keeps its moving light, with fps 0 too.
- art(): one helper for every picture site (header, title art, boot,
  Orbital art, picture(), learning, memmap, palette). An Svg's alt is the
  picture's fallback line without "needs a terminal".
- The frame loop runs while the view has pictures, as before, so the
  menu entry and the Learning pulse still move on the desktop; it only
  rebuilds and blits when a Raster is mounted, so the desktop no longer
  rebuilds every picture per frame and blits keys that were never mounted.
  The band's mark animates on the terminal only.
- tests: svg-pictures.spec.ts (vitest) for the writer, the kit, the still
  frames (on every page in every look: header, title and palette the same
  at any moment, every other picture equal to fps 0's) and the frame loop
  by surface; surface-pictures.test.ts mounts the 33 main views and the
  agent drill-down of a fixture agent in the bbs, plain and orbital looks
  on terminal and desktop (7 tests, all fail on the parent commit). It
  imports claude-code/testing, so it is listed in
  scripts/ci-test-excluded.txt with the console's other kit tests. The old desktop test only asserted "no Raster",
  which held while every picture was gone; it now asserts the Svgs arrive.

Fixes #4111

Co-Authored-By: Ruflo & AQE
2026-10-12 19:38:12 +02:00

151 lines
9.4 KiB
TypeScript

import type { RenderElement } from 'claude-code'
import type { Intelligence } from '../data/cli'
import { ago, art, col, count, kv, live, pct, picture, row, section, sourceLine, starts, text, THEME, type Ctx } from './common'
import { stagesOf } from './frames'
import { CARD_COLUMNS, hasCards } from './card'
import { pipeStagesOf, routeModelOf, stageNote } from '../data/pipeline'
import { pipelineDiagram, routePicture } from '../gfx/pipeline'
import type { Grid } from '../gfx/raster'
import { settingsOf } from '../settings'
import { resultRows } from './automate'
import { pulseRows } from './learning-pulse'
import { neuralActionRows } from './neural'
/** A picture drawn here from the real state (the registry in frames.ts is not touched): as the surface draws it, else its words. */
function drawn(ctx: Ctx, key: string, grid: Grid, fallback: string): RenderElement {
return art(ctx, grid, key, fallback) ?? text(ctx, fallback, { dimColor: true })
}
/** The width a picture is drawn to: a page in cards is narrower by the border and padding, as in frames.ts. */
const pictureWidth = (ctx: Ctx): number => Math.max(20, Math.min(200, hasCards(ctx.columns) ? ctx.columns - CARD_COLUMNS : ctx.columns))
/** The two switches that decide whether ruflo learns at all, folded away: each is a confirm-gated `ruflo config set`. */
function configRows(ctx: Ctx): RenderElement[] {
const core = settingsOf(ctx.state).core
const keys = [
{ key: 'neural.enabled', title: 'Neural learning', help: 'SONA / MoE pattern learning' },
{ key: 'hooks.enabled', title: 'Hooks', help: 'hooks that learn from your edits and routes' },
]
// The header says where each switch stands, so the state reads without opening the section; each change still asks first.
const summary = keys.map(item => `${item.title.toLowerCase()} ${core?.get(item.key) === 'true' ? 'on' : core?.get(item.key) === 'false' ? 'off' : 'n/a'}`).join(' · ')
return section(
ctx,
'learn-config',
'Settings & configuration',
`${summary} · each change asks first`,
[
...keys.map(item => {
const value = core?.get(item.key)
return row(
ctx,
[
ctx.kit.Text({ bold: true, color: THEME.head, children: ` ${item.title.padEnd(16)}` }),
ctx.kit.Text({ color: value === 'true' ? THEME.ok : value === 'false' ? THEME.bad : THEME.info, children: ` ${value === undefined ? 'n/a (open Settings to read it)' : value === 'true' ? 'on' : 'off'} ` }),
ctx.kit.Button({ key: `learn-cfg-on-${item.key}`, label: ' ● on ', plain: true, onPress: () => ctx.act.settings.core(item.key, 'true') }),
ctx.kit.Button({ key: `learn-cfg-off-${item.key}`, label: ' ○ off ', plain: true, onPress: () => ctx.act.settings.core(item.key, 'false') }),
ctx.kit.Text({ dimColor: true, wrap: 'truncate-end', children: ` ${item.help}` }),
],
`learn-cfg-${item.key}`,
)
}),
text(ctx, ' every other setting is in Settings (s): the router, memory and swarm keys, the AI preferences', { dimColor: true }),
],
false,
)
}
/** The Learning Lab's actions on this page, folded: what was learned, training, pretrain, consolidate, the pattern store, the router. */
function actionRows(ctx: Ctx): RenderElement[] {
return section(ctx, 'learn-actions', 'Self-learning actions', 'train, pretrain, consolidate, teach a pattern, route: the answer opens under the button', neuralActionRows(ctx, true), false)
}
/**
* What ruflo has learned, from its own stores: the router's last pick and how routed tasks turned out, the model tier
* router's tallies, and SONA's trajectory and pattern counts. A rate always carries its N. The router stays open; the pipeline
* and SONA fold away, each header saying what it reads.
*/
export function learningView(ctx: Ctx): RenderElement {
const { state, nowMs } = ctx
const snap = state.snapshot
const route = state.ruflo.route
const outcomes = snap?.outcomes ?? null
const router = snap?.router ?? null
const neural = snap?.neural ?? null
const intel = live<Intelligence>(state.probes.get('intelligence'))
// The mod owns `route` only where no classic route hook runs it (ADR-404): with `owned` empty it routes nothing and records nothing here.
const owned = state.ruflo.snapshot?.owned
const classicOwnsRoute = owned !== undefined && !owned.includes('route')
const lastOutcomeMs = outcomes?.points[outcomes.points.length - 1]?.atMs
const staleOutcomes = lastOutcomeMs !== undefined && nowMs - lastOutcomeMs > 24 * 3_600_000
const routeModel = routeModelOf({ seated: state.ruflo.snapshot !== null, classicOwnsRoute, route, lab: state.lab.result })
const routeFallback = `route: ${routeModel.owner} owns routing · ${routeModel.candidates.map(c => `${c.agent} ${c.confidence === null ? 'n/a' : pct(c.confidence)}`).join(' · ') || 'no route recorded'}`
// Each stage's age is its source's own timestamp; CONSOLIDATE's counter has none, so its age is n/a and it is never dimmed for age.
const pipeStages = pipeStagesOf(stagesOf(state), { RETRIEVE: neural?.lastAdaptationMs, JUDGE: lastOutcomeMs, DISTILL: neural?.lastAdaptationMs }, nowMs)
const routerRows: RenderElement[] = [
kv(
ctx,
'last pick',
route !== null
? `${route.agent} ${pct(route.confidence)} · ${route.matched ? 'keyword match' : 'no match, default'} · ${route.reason} (a prior, not a calibrated probability)`
: state.ruflo.snapshot === null
? 'n/a — ruflo-mods not seated, so no in-process route'
: classicOwnsRoute
? 'n/a — in this project the classic hook-handler owns routing, so ruflo-mods stands down and records no picks (see /ruflo-mods)'
: 'n/a — no prompt routed yet this session',
),
kv(
ctx,
'routed outcomes',
outcomes === null || outcomes.total === 0
? 'n/a — no routing-outcomes.json'
: `${outcomes.successes}/${outcomes.total} succeeded (${pct(outcomes.successes / outcomes.total)} success rate, N=${outcomes.total}) · last ${ago(lastOutcomeMs, nowMs)}${staleOutcomes ? ' · nothing recorded since' : ''}`,
outcomes !== null && outcomes.total > 0 ? (staleOutcomes ? THEME.warn : THEME.info) : undefined,
),
drawn(ctx, 'route', routePicture(routeModel, pictureWidth(ctx)), routeFallback),
picture(ctx, 'curve', `running success rate over ${outcomes?.total ?? 0} outcomes`),
text(ctx, 'running success rate of routed tasks, oldest left (router accuracy over N outcomes); new outcomes draw in', { dimColor: true }),
kv(
ctx,
'model router',
router === null
? 'n/a — no .swarm/model-router-state.json'
: `${count(router.decisions)} decisions · ${router.distribution.filter(entry => entry.count > 0).map(entry => `${entry.model} ${entry.count}`).join(' · ') || 'none'} · avg confidence ${router.avgConfidence === undefined ? 'n/a' : router.avgConfidence.toFixed(2)} · ${ago(router.updatedMs, nowMs)}`,
),
]
const pipelineRows: RenderElement[] = [
drawn(ctx, 'pipeline-stages', pipelineDiagram(pipeStages, pictureWidth(ctx)), pipeStages.map(stage => `${stage.name} ${stage.count ?? 'n/a'} (${stageNote(stage)})`).join(' → ')),
...stagesOf(state).map((stage, i) => text(ctx, ` ${stage.name.toLowerCase()}: ${stage.source} · ${stageNote(pipeStages[i] as (typeof pipeStages)[number])}`, { dimColor: true })),
text(ctx, ` a stage dims when its source has gained nothing for 24h; CONSOLIDATE has no timestamp, so its age is n/a`, { dimColor: true }),
picture(ctx, 'patterns', `patterns since load: ${state.history.patterns.map(sample => sample.value).join(' ') || 'n/a'}`),
]
const sonaRows: RenderElement[] = [
kv(ctx, 'trajectories', neural === null ? 'n/a — no .claude-flow/neural/stats.json' : count(neural.trajectories)),
kv(ctx, 'patterns learned', neural === null ? 'n/a' : count(neural.patterns)),
kv(ctx, 'signals', neural === null ? 'n/a' : count(neural.signals)),
kv(ctx, 'last learned', neural?.lastAdaptationMs !== undefined ? ago(neural.lastAdaptationMs, nowMs) : 'n/a', THEME.info),
kv(ctx, 'SONA patterns', snap?.sona === null || snap?.sona === undefined ? 'n/a — no .swarm/sona-patterns.json' : String(snap.sona.patterns)),
kv(
ctx,
'live engine',
intel === null
? sourceLine(state.probes.get('intelligence'), nowMs, 'n/a').text
: `${count(intel.trajectories)} trajectories · ${count(intel.patterns)} patterns · MoE ${count(intel.moeDecisions)} decisions · EWC ${count(intel.ewcConsolidations)} consolidations · neural router ${intel.neuralRouter ?? 'n/a'}`,
),
text(ctx, 'live engine: hooks_intelligence_stats in a fresh CLI process, so in-memory counters start at zero there', { dimColor: true }),
]
const rows: RenderElement[] = [
...section(ctx, 'learn-router', 'Router', 'ruflo-mods · routing-outcomes.json', routerRows, true),
...section(ctx, 'learn-pipeline', 'Pipeline', 'RETRIEVE → JUDGE → DISTILL → CONSOLIDATE', pipelineRows, true),
...section(ctx, 'learn-sona', 'SONA · ReasoningBank', 'neural/stats.json', sonaRows, false),
]
// Nothing learned yet: the loop starts from the repository itself.
if ((snap?.sona === null || snap?.sona === undefined) && (neural === null || neural === undefined) && snap?.isRufloProject === true) rows.push(starts(ctx, 'Nothing learned yet in this project.', ['pretrain']))
return col(ctx, [...resultRows(ctx, ['nn-']), ...pulseRows(ctx), ...rows, ...configRows(ctx), ...actionRows(ctx)], 'learning')
}