Skip to content

armgit / ruflo

Public Imported from GitHub
0

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

fix(console): facts read after a run read not in a render, spend class, peer home guard, convoTargets option parser (ADR-465)

ruva7706df5056a
diff --git a/plugins/ruflo-console/hooks/data/wf-targets.ts b/plugins/ruflo-console/hooks/data/wf-targets.ts
index f0d6097ccb..aa928a2dc0 100644
--- a/plugins/ruflo-console/hooks/data/wf-targets.ts
+++ b/plugins/ruflo-console/hooks/data/wf-targets.ts
@@ -29,6 +29,15 @@ export type Target = {
ref: string
}
+/** The one option this feature reads, checked the way ADR-462's guard options are: a non-string is the default (empty), and the text is capped. */
+export type ConvoOptions = { convoTargets: string }
+
+export const convoOptionsOf = (raw: unknown): ConvoOptions => {
+ const value = (typeof raw === 'object' && raw !== null ? (raw as Record<string, unknown>).convoTargets : undefined) as unknown
+
+ return { convoTargets: typeof value === 'string' ? value.slice(0, 2000) : '' }
+}
+
export type Endpoint = { name: string; baseUrl: string; keyEnv: string; model: string }
export type Config = { endpoints: Endpoint[]; rooms: string[]; channels: string[]; errors: string[] }
diff --git a/plugins/ruflo-console/hooks/views/wf-convo.ts b/plugins/ruflo-console/hooks/views/wf-convo.ts
index 003898a479..6e34453825 100644
--- a/plugins/ruflo-console/hooks/views/wf-convo.ts
+++ b/plugins/ruflo-console/hooks/views/wf-convo.ts
@@ -14,7 +14,7 @@ import { fanOutOf } from '../data/wf-convo'
import { parseMentions, type Target } from '../data/wf-targets'
import type { Host } from '../host'
import type { State } from '../state'
-import { hostOf, liveOf, refreshFacts, relaySpec, saveTranscript, sendSpec, stopWatch, targetsFor, watchSpec, wireConvo } from '../wf-convo-live'
+import { hostOf, liveOf, relaySpec, saveTranscript, sendSpec, stopWatch, targetsFor, watchSpec, wireConvo } from '../wf-convo-live'
import { button, clip, col, kv, row, text, THEME, type Ctx } from './common'
import { fold } from './wf-fold'
import { registerSlot, type SlotEnv } from './wf-slots'
@@ -96,8 +96,6 @@ export function conversationRows(env: SlotEnv): RenderElement[] {
if (hostOf(state) === null) return [text(ctx, 'The console is not wired to a host here: nothing can be sent.', { dimColor: true })]
- void refreshFacts(state, env.nowMs)
-
const targets = targetsFor(state, env.runs)
const redraw = (): void => ctx.act.workflows.setUi({})
const picked = targets.find(target => target.id === live.convo.picked) ?? targets[0] ?? null
diff --git a/plugins/ruflo-console/hooks/wf-convo-live.ts b/plugins/ruflo-console/hooks/wf-convo-live.ts
index a5824b19ad..67d00027e6 100644
--- a/plugins/ruflo-console/hooks/wf-convo-live.ts
+++ b/plugins/ruflo-console/hooks/wf-convo-live.ts
@@ -16,6 +16,7 @@ import { plain } from './data/parse'
import type { WfRun } from './data/workflows'
import type { Host } from './host'
import { CLI_PREFIXES, type State } from './state'
+import { afterRead } from './wf-live'
export type Live = { convo: Convo; draft: string; relayDraft: string; config: Config; peers: PeerRef[]; hasCodex: boolean; home: string | null; checkedAtMs: number; said: string | null; timers: Map<string, { cancel: () => void }> }
@@ -35,10 +36,21 @@ export function liveOf(state: State): Live {
return held
}
-/** The wiring: the host, and the option string that names the endpoints, rooms and channels (nothing secret: a key is a variable's name). */
+let isHooked = false
+
+/**
+ * The wiring: the host, and the option string that names the endpoints, rooms and channels (nothing secret: a key is a variable's name). The
+ * facts the target list depends on are read after each read of the run folders (`afterRead`, which runs only while the page is open), never
+ * from a render: a view calls nothing on the engine.
+ */
export function wireConvo(state: State, host: Host, optionText?: string): void {
hosts.set(state, host)
if (optionText !== undefined) liveOf(state).config = parseConfig(optionText)
+
+ if (isHooked) return
+
+ isHooked = true
+ afterRead.push((s, _h, _before, _runs, nowMs) => void refreshFacts(s, nowMs))
}
export const hostOf = (state: State): Host | null => hosts.get(state) ?? null
@@ -112,6 +124,8 @@ export function sendSpec(state: State, targets: readonly Target[], body: string)
if (deps === null) return { spec: null, why: 'the console is not wired to a host here' }
if (targets.length === 0) return { spec: null, why: 'pick a target, or name one with @name' }
+ if (live.home === null && targets.some(target => target.transport === 'peer')) return { spec: null, why: 'the home folder is not known yet, so the federation helper cannot be located: nothing is sent to a peer' }
+
const made = targets.map(target => ({ target, built: payloadOf(target, body, deps) }))
const bad = made.find(entry => !entry.built.ok)
@@ -124,7 +138,7 @@ export function sendSpec(state: State, targets: readonly Target[], body: string)
args: [],
shows: shows.slice(0, 1800),
expect: 'each target\'s answer, or why it did not answer, in its own thread',
- declared: leaving.length > 0 ? 'network' : 'write',
+ declared: targets.some(target => target.cost === 'session-turn' || target.cost === 'metered' || target.cost === 'peer-session') ? 'spend' : leaving.length > 0 ? 'network' : 'write',
note: `${leaving.length > 0 ? `LEAVES THIS MACHINE for ${leaving.map(target => `${target.id} (${target.leaves})`).join(', ')}. ` : 'Stays on this machine. '}${made.map(entry => (entry.built.ok ? `${entry.target.id}: ${entry.built.payload.note} Cost: ${entry.target.costText}.` : '')).join(' ')}`.slice(0, 900),
timeoutMs: 600_000,
run: async () => {
diff --git a/plugins/ruflo-console/tests/wf-control-views.spec.ts b/plugins/ruflo-console/tests/wf-control-views.spec.ts
index d073b5199b..21fcebf388 100644
--- a/plugins/ruflo-console/tests/wf-control-views.spec.ts
+++ b/plugins/ruflo-console/tests/wf-control-views.spec.ts
@@ -17,7 +17,7 @@ import { resetControl, stopSlotSpec, wireWfControl } from '../hooks/views/wf-con
import { workflowsPage } from '../hooks/views/wf-page'
import { slotsFor, type SlotEnv } from '../hooks/views/wf-slots'
import { workflowsActions } from '../hooks/wf-actions'
-import { liveOf, resetConvo, saveTranscript, sendSpec, stopWatch, watchSpec } from '../hooks/wf-convo-live'
+import { liveOf, refreshFacts, resetConvo, saveTranscript, sendSpec, stopWatch, watchSpec } from '../hooks/wf-convo-live'
import { targetsFor } from '../hooks/wf-convo-live'
type El = { kind: string; props: Record<string, unknown> }
@@ -203,17 +203,20 @@ describe('the control tab', () => {
describe('the Conversation board', () => {
beforeEach(() => resetFolds())
+ let current: State | null = null
const open = (config?: string): { ctx: Ctx; state: State; asked: ReturnType<typeof world>['asked']; f: Fake } => {
const w = world()
const f = fake()
resetConvo(w.state)
wireWfConvo(w.state, f.host, config)
+ current = w.state
return { ctx: w.ctx, state: w.state, asked: w.asked, f }
}
const draw = (ctx: Ctx, run: WfRun = wfRun()): unknown => conversationRows(envOf(ctx, run))
- const settle = async (): Promise<void> => void (await new Promise(resolve => setTimeout(resolve, 5)))
+ /** The facts are read after a read of the run folders (never from a render): this is what that hook runs. */
+ const settle = async (): Promise<void> => refreshFacts(current as State, 1_700_000_000_000)
it('lists the targets with what leaves the machine, the cost class and how the reply arrives, once the facts are read', async () => {
const { ctx, state } = open('bbs:ops; endpoint:a=https://a.example.com/v1|NONE|m1')
@@ -249,7 +252,7 @@ describe('the Conversation board', () => {
expect(asked[0]?.spec?.shows).toContain('a: POST https://a.example.com/v1/chat/completions Authorization: Bearer ‹from $KEY_A›')
expect(asked[0]?.spec?.shows).toContain('"content":"what is raft?"')
expect(asked[0]?.spec?.shows).not.toContain('key-value-1234567')
- expect(asked[0]?.spec?.declared).toBe('network')
+ expect(asked[0]?.spec?.declared).toBe('spend')
expect(asked[0]?.spec?.note).toMatch(/LEAVES THIS MACHINE for a \(internet\)/)
expect(f.http).toHaveLength(0)
@@ -367,6 +370,24 @@ describe('the Conversation board', () => {
expect(saved[1]).toMatchObject({ spec: null, why: expect.stringMatching(/nothing to save/) })
})
+ it('a render reads nothing from the host: the target list changes only after the facts hook ran', async () => {
+ const { ctx, state, f } = open()
+
+ draw(ctx)
+ expect(f.runs).toHaveLength(0)
+ expect(targetsFor(state, [wfRun()]).some(target => target.id === 'peer-zenbook')).toBe(false)
+ await settle()
+ expect(f.runs.map(call => call.argv[0])).toEqual(['which'])
+ expect(targetsFor(state, [wfRun()]).some(target => target.id === 'peer-zenbook')).toBe(true)
+ })
+
+ it('a peer send is refused while the home folder is unknown (argv is not a shell: ~ would be literal)', () => {
+ const { state } = open()
+ const peer = { id: 'peer-zenbook', label: 'peer zenbook', transport: 'peer', leaves: 'tailnet', leavesText: 'x', cost: 'peer-session', costText: 'x', arrival: 'immediate', arrivalText: 'x', ref: 'zenbook' } as never
+
+ expect(sendSpec(state, [peer], 'hello')).toMatchObject({ spec: null, why: expect.stringMatching(/home folder is not known/) })
+ })
+
it('asking no one, or asking with no host, is refused with the reason and no spec', () => {
const { state } = open()
diff --git a/plugins/ruflo-console/tests/wf-convo.spec.ts b/plugins/ruflo-console/tests/wf-convo.spec.ts
index 8bb9c38d0f..2a3af1d50c 100644
--- a/plugins/ruflo-console/tests/wf-convo.spec.ts
+++ b/plugins/ruflo-console/tests/wf-convo.spec.ts
@@ -7,7 +7,7 @@ import { describe, expect, it } from 'vitest'
import type { HiveInfo } from '../hooks/data/parse'
import { addMessage, applyFetch, compareOf, fanOutOf, fromBbs, fromChannel, lastAnswer, MAX_MSGS, MAX_THREADS, newConvo, pollParams, POLL_MAX, recordSend, relayBody, statsOf, threadOf, transcriptMarkdown, transcriptName, TRANSCRIPT_MAX } from '../hooks/data/wf-convo'
import { chatBody, parseChat, payloadOf, sendTo, taskNote, type SendDeps } from '../hooks/data/wf-send'
-import { endpointsOf, isBaseUrl, MAX_FANOUT, OPENROUTER, parseConfig, parseMentions, peersOf, targetsOf, type Target } from '../hooks/data/wf-targets'
+import { convoOptionsOf, endpointsOf, isBaseUrl, MAX_FANOUT, OPENROUTER, parseConfig, parseMentions, peersOf, targetsOf, type Target } from '../hooks/data/wf-targets'
/** Deliberately not shaped like any vendor key: only the explicit scrub of the named variable's value can mask it. */
const KEY = 'k9x2-plain-42'
@@ -63,6 +63,13 @@ describe('the config option and the targets', () => {
expect(isBaseUrl('http://evil.example.com/v1')).toBe(false)
})
+ it('the convoTargets option is a capped string, and anything else is empty', () => {
+ expect(convoOptionsOf({ convoTargets: 'bbs:ops' })).toEqual({ convoTargets: 'bbs:ops' })
+ expect(convoOptionsOf({ convoTargets: 5 })).toEqual({ convoTargets: '' })
+ expect(convoOptionsOf(undefined)).toEqual({ convoTargets: '' })
+ expect(convoOptionsOf({ convoTargets: 'x'.repeat(5000) }).convoTargets).toHaveLength(2000)
+ })
+
it('offers OpenRouter by default unless the person names their own, and reads peers from the helper\'s file', () => {
expect(endpointsOf(parseConfig('')).map(entry => entry.name)).toEqual(['openrouter'])
expect(endpointsOf(parseConfig('endpoint:openrouter=https://openrouter.ai/api/v1|OTHER_KEY|x/y')).map(entry => entry.keyEnv)).toEqual(['OTHER_KEY'])