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(ruflo-mods): rollup and budget start over at each session.start, so /clear gets its own session (#3795)

Dragan Spiridonovffd882365e31
diff --git a/plugins/ruflo-mods/hooks/cost/index.ts b/plugins/ruflo-mods/hooks/cost/index.ts
index ddcdc7c726..8b9406a1da 100644
--- a/plugins/ruflo-mods/hooks/cost/index.ts
+++ b/plugins/ruflo-mods/hooks/cost/index.ts
@@ -15,7 +15,15 @@ export function registerCost(on: On, state: ModState, options: ModOptions) {
const limit = options.costBudgetUsd
if (limit === undefined) return
state.budget = { level: 'OK', limit }
- let announced: BudgetLevel = 'OK' // the highest rung told; a fall and a second rise stay quiet
+ let announced: BudgetLevel = 'OK' // the highest rung told this session; a fall and a second rise stay quiet
+
+ // The budget is per session, and /clear starts a new one in the same process
+ // with its cost counted from zero: the ladder and the hard stop start over.
+ on('session.start', { surface: /^[\s\S]*$/ }, ($, e, next) => {
+ announced = 'OK'
+ state.budget = { level: 'OK', limit }
+ return next(e)
+ })
on('session.measure', async ($, e, next) => {
const result = await next(e)
diff --git a/plugins/ruflo-mods/hooks/rollup/index.ts b/plugins/ruflo-mods/hooks/rollup/index.ts
index 314ed6fa53..2f139c5664 100644
--- a/plugins/ruflo-mods/hooks/rollup/index.ts
+++ b/plugins/ruflo-mods/hooks/rollup/index.ts
@@ -15,11 +15,13 @@ export type RollupState = {
/** The highest cost rung the session reached. */
rung: BudgetLevel
written: boolean
+ /** The shared routed/tightened counters at this session's start: a record carries only this session's share. */
+ base: { routed: number; tightened: number }
/** The ledger as last read or written, for the report. */
recent: Rollup[]
}
-export const rollupState = (): RollupState => ({ enabled: false, tools: 0, denied: 0, spawns: 0, rung: 'OK', written: false, recent: [] })
+export const rollupState = (): RollupState => ({ enabled: false, tools: 0, denied: 0, spawns: 0, rung: 'OK', written: false, base: { routed: 0, tightened: 0 }, recent: [] })
/**
* Session rollup (ADR-451 item 6): observability only. Off unless
@@ -34,6 +36,8 @@ export function registerRollup(on: On, state: ModState) {
r.enabled = true
on('session.start', { surface: /^[\s\S]*$/ }, async ($, e, next) => {
+ // /clear ends one session and starts the next in the same process: each gets its own record.
+ Object.assign(r, { tools: 0, denied: 0, spawns: 0, rung: 'OK', written: false, base: { routed: state.routed, tightened: state.tightened } })
const result = await next(e)
try {
r.recent = readLedger(await $.store.get(LEDGER))
@@ -68,7 +72,7 @@ export function registerRollup(on: On, state: ModState) {
r.written = true
try {
if (isRaised(r.rung, state.budget.level)) r.rung = state.budget.level
- const record: Rollup = { at: await $.clock.now(), tools: r.tools, routed: state.routed, tightened: state.tightened, denied: r.denied, spawns: r.spawns, cost: r.rung }
+ const record: Rollup = { at: await $.clock.now(), tools: r.tools, routed: state.routed - r.base.routed, tightened: state.tightened - r.base.tightened, denied: r.denied, spawns: r.spawns, cost: r.rung }
if (state.probe.enabled) {
const names = [...state.probe.registered]
record.probe = `${names.filter(n => (state.probe.fired.get(n) ?? 0) > 0).length}/${names.length}`
diff --git a/plugins/ruflo-mods/tests/cost.test.ts b/plugins/ruflo-mods/tests/cost.test.ts
index c479b5d621..a2ea3ee0b9 100644
--- a/plugins/ruflo-mods/tests/cost.test.ts
+++ b/plugins/ruflo-mods/tests/cost.test.ts
@@ -27,6 +27,25 @@ describe('cost', () => {
expect(JSON.parse((await $.command.run(run('consumer-snapshot'))).text ?? 'null').budget).toEqual({ level: 'HARD_STOP', usd: 2.4, limit: 2 })
})
+ test('/clear starts a new session: the ladder is told again and the hard stop lifts', { options: { costBudgetUsd: 2, costHardStop: true } }, async ($, on) => {
+ world(on)
+ const toasts: string[] = []
+ on('ui.toast', ($, e) => (toasts.push(e.text), { value: undefined }))
+ on('session.measure', ($, e) => ({ changed: e.changed }))
+ on('agent.spawn', () => ({ model: 'sonnet' }))
+ const spawn = () =>
+ $.agent.spawn({ prompt: 'p', description: 'd', subagentType: 'coder' } as Parameters<typeof $.agent.spawn>[0])
+ await $.session.start(START)
+ await $.session.measure(measure(2.4))
+ expect((await spawn()).deny).toMatch(/costHardStop/)
+
+ // The engine counts the new session's cost from zero after /clear (measured live on 2.1.289).
+ await $.session.start({ ...START, source: 'clear' } as never)
+ expect(await spawn()).toEqual({ model: 'sonnet' })
+ await $.session.measure(measure(1.1))
+ expect(toasts.map(t => t.split(':')[0])).toEqual(['ruflo budget HARD_STOP', 'ruflo budget INFO'])
+ })
+
test('a rung announced once stays quiet when cost falls and rises again', { options: { costBudgetUsd: 2 } }, async ($, on) => {
world(on)
const toasts: string[] = []
diff --git a/plugins/ruflo-mods/tests/rollup.test.ts b/plugins/ruflo-mods/tests/rollup.test.ts
index ecbbb6fd4a..6b756c97bb 100644
--- a/plugins/ruflo-mods/tests/rollup.test.ts
+++ b/plugins/ruflo-mods/tests/rollup.test.ts
@@ -113,6 +113,18 @@ describe('sessionRollup option (ADR-451 item 6)', () => {
expect((s.kv.get('sessionRollup') as Rollup[]).map(r => r.at)).toEqual([1, NOW])
})
+ test('/clear in one process: each session gets its own record with its own counters', { options: { sessionRollup: true } }, async ($, on) => {
+ boot(on)
+ const s = store(on)
+ await $.session.start(START)
+ await $.tool.call({ tool: 'Read', file_path: 'a' } as never)
+ await $.session.end({ ...(END as object), reason: 'clear' } as never)
+ await $.session.start({ ...START, source: 'clear' } as never)
+ for (const f of ['b', 'c', 'd']) await $.tool.call({ tool: 'Read', file_path: f } as never)
+ await $.session.end(END)
+ expect((s.kv.get('sessionRollup') as Rollup[]).map(r => r.tools)).toEqual([1, 3])
+ })
+
test('a hostile stored ledger is treated as empty and replaced by one clean record', { options: { sessionRollup: true } }, async ($, on) => {
boot(on)
const s = store(on, { __proto__: 1, length: 1e9, 0: { at: 1 } })