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-console): key the mission ledger by project (#4237) (#4242)

kevin4487dd0c9b5e
diff --git a/plugins/ruflo-console/hooks/mission-control.ts b/plugins/ruflo-console/hooks/mission-control.ts
index 1a3e6936b7..4e02dec7e0 100644
--- a/plugins/ruflo-console/hooks/mission-control.ts
+++ b/plugins/ruflo-console/hooks/mission-control.ts
@@ -29,8 +29,12 @@ export { cancelSpec, createSpec, createWhy, dispatchSpec, resultOf, setPaused }
export type { Derived, LedgerEvent, LedgerTask, McState, McTab, MissionActions, MissionRecord } from './mission-types'
const states = new WeakMap<State, McState>()
+/** The pre-#4237 ledger key, shared by every project on the machine. Read once per project as a fallback, never written for a known cwd. */
export const LEDGER_KEY = 'mission-ledger'
+/** The mission ledger is per project, like the UI view (`storeKeyOf`): one machine-wide key let one project's active mission drive another's. */
+export const ledgerKeyOf = (cwd: string): string => (cwd === '' ? LEDGER_KEY : `${LEDGER_KEY}:${cwd}`)
+
export function mcOf(state: State): McState {
let found = states.get(state)
@@ -148,11 +152,16 @@ export function record(mission: MissionRecord, event: Omit<LedgerEvent, 'seq' |
export function saveLedger(state: State, host: Host): void {
const mc = mcOf(state)
- void host.storeSet(LEDGER_KEY, { active: mc.active, missions: [...mc.missions.values()].slice(-20) }).catch(() => undefined)
+ void host.storeSet(ledgerKeyOf(state.cwd), { active: mc.active, missions: [...mc.missions.values()].slice(-20) }).catch(() => undefined)
}
export async function loadLedger(state: State, host: Host): Promise<void> {
- const saved = await host.storeGet(LEDGER_KEY).catch(() => undefined)
+ const key = ledgerKeyOf(state.cwd)
+ const own = await host.storeGet(key).catch(() => undefined)
+ // A project with no ledger of its own falls back to the old machine-wide one, keeping only missions not recorded for another project and
+ // never its `active`: that was whichever project wrote last (#4237).
+ const isLegacy = own === undefined && key !== LEDGER_KEY
+ const saved = isLegacy ? await host.storeGet(LEDGER_KEY).catch(() => undefined) : own
const missions = (saved as { missions?: unknown } | undefined)?.missions
const mc = mcOf(state)
@@ -161,12 +170,16 @@ export async function loadLedger(state: State, host: Host): Promise<void> {
for (const raw of missions.slice(0, 20)) {
const m = raw as MissionRecord
+ if (m?.cwd !== undefined && m.cwd !== state.cwd) continue
+
const isShaped = Array.isArray(m?.tasks) && m.tasks.every(task => typeof task?.id === 'string' && Array.isArray(task.dependsOn)) && Array.isArray(m.events)
// Auto-run never survives a restart: a new session starts by asking.
if (typeof m?.id === 'string' && /^msn_[a-f0-9]{24}$/.test(m.id) && isShaped) mc.missions.set(m.id, { ...m, auto: false, ...(Array.isArray(m.adrs) && { adrs: m.adrs.filter(file => typeof file === 'string' && /^[A-Za-z0-9][A-Za-z0-9._ -]{0,119}\.md$/.test(file)).slice(0, 8) }) })
}
+ if (isLegacy) return
+
const active = (saved as { active?: unknown }).active
if (typeof active === 'string' && mc.missions.has(active)) mc.active = active
diff --git a/plugins/ruflo-console/hooks/mission-create.ts b/plugins/ruflo-console/hooks/mission-create.ts
index 4aeaf6be67..d85eff8194 100644
--- a/plugins/ruflo-console/hooks/mission-create.ts
+++ b/plugins/ruflo-console/hooks/mission-create.ts
@@ -267,6 +267,7 @@ export function createSpec(state: State, host: Host, onDone: () => void): Action
cancelled: false,
auto: false,
createdAtMs: Date.now(),
+ ...(state.cwd !== '' && { cwd: state.cwd }),
}
record(record0, { type: 'mission.created', status: 'draft' })
diff --git a/plugins/ruflo-console/hooks/mission-types.ts b/plugins/ruflo-console/hooks/mission-types.ts
index a4ef28d992..80ab426e3f 100644
--- a/plugins/ruflo-console/hooks/mission-types.ts
+++ b/plugins/ruflo-console/hooks/mission-types.ts
@@ -24,6 +24,8 @@ export type MissionRecord = {
loop?: unknown
/** ADR-480: the file names of the project's ADRs attached to this mission (validated where read). */
adrs?: string[]
+ /** The project directory the mission was created in (#4237); absent on missions saved before it was recorded. */
+ cwd?: string
}
export type McTab = 'plan' | 'tasks' | 'agents' | 'evidence' | 'record' | 'loop'
export type Derived = 'done' | 'running' | 'ready' | 'waiting' | 'failed' | 'cancelled'
diff --git a/plugins/ruflo-console/tests/adr-integration.spec.ts b/plugins/ruflo-console/tests/adr-integration.spec.ts
index 3f86f9c4c2..808cdc5868 100644
--- a/plugins/ruflo-console/tests/adr-integration.spec.ts
+++ b/plugins/ruflo-console/tests/adr-integration.spec.ts
@@ -14,7 +14,7 @@ import { adrOf, loadAdrs } from '../hooks/adr'
import { adrBlockFor, attachedOf, DIGEST_FILE, draftSpec, mirrorDigest, scopeCheck, setAttached, suggestFor, syncAdrDigest } from '../hooks/adr-mission'
import { contextSection, claudeActions } from '../hooks/mission-claude'
import { missionContextText } from '../hooks/mission-context'
-import { instructionOf, loadLedger, LEDGER_KEY, mcOf } from '../hooks/mission-control'
+import { instructionOf, ledgerKeyOf, loadLedger, LEDGER_KEY, mcOf } from '../hooks/mission-control'
import type { MissionRecord } from '../hooks/mission-types'
import { settingsOf } from '../hooks/settings'
import { newState } from '../hooks/state'
@@ -39,7 +39,7 @@ describe('attach, suggest and detach on a mission', () => {
await setAttached(state, host as never, '0003-use-graphql.md', true)
await setAttached(state, host as never, '0003-use-graphql.md', true)
expect(attachedOf(m)).toEqual(['0003-use-graphql.md'])
- expect((stored.get(LEDGER_KEY) as { missions: MissionRecord[] }).missions[0]?.adrs).toEqual(['0003-use-graphql.md'])
+ expect((stored.get(ledgerKeyOf(state.cwd)) as { missions: MissionRecord[] }).missions[0]?.adrs).toEqual(['0003-use-graphql.md'])
expect(m.events.filter(event => event.type === 'adr.attached')).toHaveLength(2)
await setAttached(state, host as never, '../../etc/passwd', true)
diff --git a/plugins/ruflo-console/tests/mission-ledger-per-project.spec.ts b/plugins/ruflo-console/tests/mission-ledger-per-project.spec.ts
new file mode 100644
index 0000000000..3f761e8dc1
--- /dev/null
+++ b/plugins/ruflo-console/tests/mission-ledger-per-project.spec.ts
@@ -0,0 +1,94 @@
+/**
+ * #4237: the mission ledger was stored under one machine-wide key, so the active mission of one project showed up (and was injected into the
+ * prompt) in every other project, and two sessions in different projects overwrote each other. Run with
+ * npx vitest run plugins/ruflo-console/tests/mission-ledger-per-project.spec.ts
+ */
+import { describe, expect, it } from 'vitest'
+
+import type { Host } from '../hooks/host'
+import { activeMission, ledgerKeyOf, loadLedger, LEDGER_KEY, mcOf, saveLedger } from '../hooks/mission-control'
+import type { MissionRecord } from '../hooks/mission-types'
+import { newState, type State } from '../hooks/state'
+
+const A = '/home/me/project-a'
+const B = '/home/me/project-b'
+
+const mission = (id: string, extra: Partial<MissionRecord> = {}): MissionRecord => ({
+ id,
+ objective: `goal ${id}`,
+ profile: 'feature',
+ rigor: 'standard',
+ tasks: [],
+ acceptance: [],
+ events: [],
+ paused: false,
+ cancelled: false,
+ auto: false,
+ createdAtMs: 1,
+ ...extra,
+})
+
+const ID_A = 'msn_aaaaaaaaaaaaaaaaaaaaaaaa'
+const ID_B = 'msn_bbbbbbbbbbbbbbbbbbbbbbbb'
+const ID_OLD = 'msn_cccccccccccccccccccccccc'
+
+function storeHost(stored: Map<string, unknown>): Host {
+ return { storeGet: async (key: string) => stored.get(key), storeSet: async (key: string, value: unknown) => void stored.set(key, value) } as unknown as Host
+}
+
+function stateIn(cwd: string): State {
+ const state = newState()
+ state.cwd = cwd
+ return state
+}
+
+const flush = () => new Promise(resolve => setTimeout(resolve, 0))
+
+describe('mission ledger per project (#4237)', () => {
+ it("does not show project A's active mission in project B", async () => {
+ const stored = new Map<string, unknown>()
+ const host = storeHost(stored)
+ const a = stateIn(A)
+ mcOf(a).missions.set(ID_A, mission(ID_A, { cwd: A }))
+ mcOf(a).active = ID_A
+ saveLedger(a, host)
+ await flush()
+
+ const b = stateIn(B)
+ await loadLedger(b, host)
+ expect(activeMission(b)).toBeNull()
+ expect(mcOf(b).missions.size).toBe(0)
+
+ const again = stateIn(A)
+ await loadLedger(again, host)
+ expect(activeMission(again)?.id).toBe(ID_A)
+ })
+
+ it('sessions in two projects keep their own ledgers', async () => {
+ const stored = new Map<string, unknown>()
+ const host = storeHost(stored)
+ const a = stateIn(A)
+ const b = stateIn(B)
+ mcOf(a).missions.set(ID_A, mission(ID_A, { cwd: A }))
+ mcOf(a).active = ID_A
+ mcOf(b).missions.set(ID_B, mission(ID_B, { cwd: B }))
+ mcOf(b).active = ID_B
+ saveLedger(a, host)
+ saveLedger(b, host)
+ await flush()
+
+ const reloaded = stateIn(A)
+ await loadLedger(reloaded, host)
+ expect([...mcOf(reloaded).missions.keys()]).toEqual([ID_A])
+ expect(stored.has(ledgerKeyOf(A))).toBe(true)
+ expect(stored.has(ledgerKeyOf(B))).toBe(true)
+ })
+
+ it('reads the old shared ledger once, without adopting its active mission or other projects\' missions', async () => {
+ const stored = new Map<string, unknown>([[LEDGER_KEY, { active: ID_OLD, missions: [mission(ID_OLD), mission(ID_A, { cwd: A })] }]])
+ const b = stateIn(B)
+ await loadLedger(b, storeHost(stored))
+ expect([...mcOf(b).missions.keys()]).toEqual([ID_OLD])
+ expect(activeMission(b)).toBeNull()
+ })
+})