diff --git a/lib/conductor/sessions.js b/lib/conductor/sessions.js index 7a107b9..9ee2e61 100644 --- a/lib/conductor/sessions.js +++ b/lib/conductor/sessions.js @@ -6,20 +6,26 @@ // blocked state "blocked" (waiting on CC) // done state "done" // unknown any other value, or none. Kept visible with its raw state and never -// treated as done. +// treated as done. (The status board splits this value into groups for +// display, see groupSessions; the classification itself, which the reapers +// read, does not change.) // // Live process (BOB-185). `claude agents --json --all` keeps a session that was ended // earlier in the list as state `done` with no `pid`. A session is `live` // only when it carries a pid (a positive integer). A `done` session that is not live // is already stopped: there is nothing for the reaper to end and it is not counted as -// a live lane. +// a live lane. The same holds for a session claude lists as `stopped` or `failed` with +// no pid (BOB-184): the status board groups all three as stopped. import fs from 'fs'; import path from 'path'; import { run as defaultRun } from './exec.js'; const KNOWN_STATES = ['working', 'blocked', 'done']; -const SUCCESSOR_RE = /bg ([0-9a-f]+)/; +const ENDED_STATES = ['done', 'stopped', 'failed']; +// handoff-spawn writes `"" (bg , ) from `; anchoring on `(bg ` keeps a +// session name that happens to contain "bg " from being read as the successor. +const SUCCESSOR_RE = /\(bg ([0-9a-f]+)[,)]/; /** Raw session list from claude. Throws on a failed command or bad JSON. */ export function readAgents({ run = defaultRun } = {}) { @@ -71,6 +77,7 @@ export function classifySessions(rawSessions, { hookStateDir, now = Date.now() } cwd: s.cwd ?? null, state: classifyState(s.state), raw_state: s.state ?? null, + status: typeof s.status === 'string' ? s.status : null, waiting_for: s.waitingFor ?? null, started_at: Number.isFinite(s.startedAt) ? new Date(s.startedAt).toISOString() : null, age_ms: Number.isFinite(s.startedAt) ? Math.max(0, now - s.startedAt) : null, @@ -91,14 +98,21 @@ export function classifySessions(rawSessions, { hookStateDir, now = Date.now() } /** * Group lane records: working, waiting_on_cc (blocked), done (finished, process still - * live), stopped (done, no live process: already ended), unknown. + * live), stopped (raw state done, stopped or failed with no live process: already ended), + * interactive (a terminal session with no state, only a status such as idle or busy; one that + * does carry an unrecognised state stays unknown so that state is shown), + * unknown (anything else, kept visible). A session with a state of working or blocked is + * in its own group whatever its kind. */ export function groupSessions(sessions) { - return { - working: sessions.filter(s => s.state === 'working'), - waiting_on_cc: sessions.filter(s => s.state === 'blocked'), - done: sessions.filter(s => s.state === 'done' && s.live), - stopped: sessions.filter(s => s.state === 'done' && !s.live), - unknown: sessions.filter(s => s.state === 'unknown'), - }; + const groups = { working: [], waiting_on_cc: [], done: [], stopped: [], interactive: [], unknown: [] }; + for (const s of sessions) { + if (s.state === 'working') groups.working.push(s); + else if (s.state === 'blocked') groups.waiting_on_cc.push(s); + else if (s.state === 'done' && s.live) groups.done.push(s); + else if (ENDED_STATES.includes(s.raw_state) && !s.live) groups.stopped.push(s); + else if (s.kind === 'interactive' && s.raw_state == null) groups.interactive.push(s); + else groups.unknown.push(s); + } + return groups; } diff --git a/lib/conductor/status.js b/lib/conductor/status.js index 6011284..b986c9e 100644 --- a/lib/conductor/status.js +++ b/lib/conductor/status.js @@ -31,6 +31,7 @@ export function buildStatus(studioRoot, { run, now = Date.now() } = {}) { waiting_on_cc: groups.waiting_on_cc, done: groups.done, stopped: groups.stopped, + interactive: groups.interactive, unknown: groups.unknown, handoffs: sessions.filter(s => s.handed_off), inbox: inboxStatus(inboxDir(studioRoot), sessions, { now, staleMinutes: config.inbox.stale_minutes }), @@ -69,7 +70,8 @@ function laneLine(s, { detail = false } = {}) { const name = s.name || '(unnamed)'; const parts = [` ${laneId(s)} ${name}`, dim(`${formatAge(s.age_ms)} ${shortPath(s.cwd)}`)]; if (detail && s.waiting_for) parts.push(dim(`waiting for: ${s.waiting_for}`)); - if (s.state === 'unknown') parts.push(dim(`[state: ${s.raw_state ?? 'none'}${s.kind ? `, ${s.kind}` : ''}]`)); + if (s.kind === 'interactive' && s.raw_state == null) parts.push(dim(`[interactive, status: ${s.status ?? 'none'}]`)); + else if (s.state === 'unknown') parts.push(dim(`[state: ${s.raw_state ?? 'none'}${s.kind ? `, ${s.kind}` : ''}]`)); return parts.join(' '); } @@ -85,7 +87,15 @@ export function renderStatus(status) { section(lines, 'WORKING', status.working); section(lines, 'WAITING ON CC', status.waiting_on_cc, { detail: true }); section(lines, 'DONE', status.done); - lines.push(`${bold('STOPPED')} ${dim(`(${status.stopped.length}) already ended, no live process; not reaped`)}`, ''); + // Stopped sessions are a count with the raw state broken out; a failed one is listed, since + // it ended without finishing. + const byState = {}; + for (const s of status.stopped) byState[s.raw_state] = (byState[s.raw_state] || 0) + 1; + const breakdown = Object.entries(byState).map(([state, n]) => `${state} ${n}`).join(', '); + lines.push(`${bold('STOPPED')} ${dim(`(${status.stopped.length}) already ended, no live process; not reaped${breakdown ? `: ${breakdown}` : ''}`)}`); + for (const s of status.stopped.filter(x => x.raw_state === 'failed')) lines.push(laneLine(s)); + lines.push(''); + section(lines, 'INTERACTIVE', status.interactive); section(lines, 'UNKNOWN STATE', status.unknown); lines.push(`${bold('HANDOFFS')} ${dim(`(${status.handoffs.length})`)}`); diff --git a/test/commands/conductor.test.js b/test/commands/conductor.test.js index 0ade794..112688d 100644 --- a/test/commands/conductor.test.js +++ b/test/commands/conductor.test.js @@ -140,20 +140,40 @@ describe('bobby conductor', () => { expect(waiting).toContain('lane two'); expect(waiting).toContain('approve edit'); expect(waiting).not.toContain('lane one'); - expect(out.slice(at('DONE'), at('UNKNOWN STATE'))).toContain('lane three'); - expect(out.slice(at('UNKNOWN STATE'))).toContain('state: stopped'); + expect(at('INTERACTIVE')).toBeLessThan(at('UNKNOWN STATE')); + expect(out.slice(at('DONE'), at('STOPPED'))).toContain('lane three'); + expect(out.slice(at('UNKNOWN STATE'), at('HANDOFFS'))).toContain('state: stopped'); + expect(out.slice(at('INTERACTIVE'), at('UNKNOWN STATE'))).toContain('interactive, status: none'); + }); + + test('stopped/failed background sessions are counted by raw state and interactive ones listed with their status, not lumped into UNKNOWN STATE', () => { + const agents = agentsFixture(); + agents.push({ id: 'abab0001', sessionId: 'abab0001-0000-4000-8000-000000000000', name: 'ended one', cwd: '/studio', kind: 'background', startedAt: 1, state: 'stopped' }); + agents.push({ id: 'abab0002', sessionId: 'abab0002-0000-4000-8000-000000000000', name: 'crashed one', cwd: '/studio', kind: 'background', startedAt: 1, state: 'failed' }); + agents.push({ sessionId: 'abab0003-0000-4000-8000-000000000000', name: 'terminal one', cwd: '/studio', kind: 'interactive', startedAt: 1, status: 'busy', pid: 7 }); + fs.writeFileSync(agentsFile, JSON.stringify(agents)); + const out = bobbyIn(['status']).stdout; + const at = (s) => out.indexOf(s); + expect(out).toMatch(/STOPPED \(2\)\s+already ended, no live process; not reaped: stopped 1, failed 1/); + expect(out.slice(at('STOPPED'), at('INTERACTIVE'))).toMatch(/crashed one.*\[state: failed, background\]/); + expect(out.slice(at('STOPPED'), at('INTERACTIVE'))).not.toContain('ended one'); + expect(out.slice(at('INTERACTIVE'), at('UNKNOWN STATE'))).toMatch(/terminal one.*\[interactive, status: busy\]/); + expect(out.slice(at('UNKNOWN STATE'), at('HANDOFFS'))).not.toMatch(/ended one|crashed one|terminal one/); + const data = JSON.parse(bobbyIn(['status', '--json']).stdout); + expect(data.stopped.map(s => s.name)).toEqual(['ended one', 'crashed one']); + expect(data.interactive.map(s => [s.name, s.status])).toEqual([['interactive', null], ['terminal one', 'busy']]); }); test('--json parses and carries the same groups', () => { const r = bobbyIn(['status', '--json']); expect(r.status).toBe(0); const data = JSON.parse(r.stdout); - expect(Object.keys(data)).toEqual(['generated_at', 'working', 'waiting_on_cc', 'done', 'stopped', 'unknown', 'handoffs', 'inbox', 'board', 'machine', 'stacks']); + expect(Object.keys(data)).toEqual(['generated_at', 'working', 'waiting_on_cc', 'done', 'stopped', 'interactive', 'unknown', 'handoffs', 'inbox', 'board', 'machine', 'stacks']); expect(data.working.map(s => s.name)).toEqual(['lane one']); expect(data.waiting_on_cc.map(s => s.name)).toEqual(['lane two']); expect(data.done.map(s => s.name)).toEqual(['lane three']); const text = bobbyIn(['status']).stdout; - for (const group of ['working', 'waiting_on_cc', 'done', 'unknown']) { + for (const group of ['working', 'waiting_on_cc', 'done', 'interactive', 'unknown']) { for (const s of data[group]) expect(text).toContain(s.name); } }); @@ -383,7 +403,7 @@ describe('bobby conductor', () => { }); test('status shows a handoff whose successor is not live as an orphaned handoff', () => { - fs.writeFileSync(path.join(hookDir, 'cccc3333-0000-4000-8000-000000000000.handoff'), 'bg ffff9999\n'); + fs.writeFileSync(path.join(hookDir, 'cccc3333-0000-4000-8000-000000000000.handoff'), '"lane" (bg ffff9999, claude-opus-5-5) from /x/h.md\n'); const r = bobbyIn(['status']); expect(r.stdout).toMatch(/orphaned handoff/); const dry = bobbyIn(['reap']); @@ -394,7 +414,7 @@ describe('bobby conductor', () => { const agents = agentsFixture(); agents.push({ id: 'abab0001', sessionId: 'abab0001-0000-4000-8000-000000000000', name: 'old stopped lane', cwd: '/studio', kind: 'background', startedAt: 1, state: 'done' }); fs.writeFileSync(agentsFile, JSON.stringify(agents)); - fs.writeFileSync(path.join(hookDir, 'abab0001-0000-4000-8000-000000000000.handoff'), 'bg ffff9999\n'); + fs.writeFileSync(path.join(hookDir, 'abab0001-0000-4000-8000-000000000000.handoff'), '"lane" (bg ffff9999, claude-opus-5-5) from /x/h.md\n'); const r = bobbyIn(['status']); expect(r.stdout).toMatch(/old stopped lane\s+successor ffff9999 not live/); expect(r.stdout).not.toMatch(/orphaned handoff/); diff --git a/test/lib/conductor/reaper.test.js b/test/lib/conductor/reaper.test.js index 3ba83ba..5226298 100644 --- a/test/lib/conductor/reaper.test.js +++ b/test/lib/conductor/reaper.test.js @@ -34,7 +34,7 @@ describe('conductor reaper', () => { let hookDir; beforeEach(() => { hookDir = fs.mkdtempSync(path.join(os.tmpdir(), 'bobby-reap-')); }); afterEach(() => { fs.rmSync(hookDir, { recursive: true, force: true }); }); - const marker = (id, successor) => fs.writeFileSync(path.join(hookDir, `${sid(id)}.handoff`), `bg ${successor}\n`); + const marker = (id, successor) => fs.writeFileSync(path.join(hookDir, `${sid(id)}.handoff`), `"lane" (bg ${successor}, claude-opus-5-5) from /x/handoff.md\n`); const doReap = (agents, opts = {}, mockOpts) => { const m = mockRun(agents, mockOpts); const report = reap({ hookStateDir: hookDir, run: m.run, now: 10, ...opts }); diff --git a/test/lib/conductor/sessions.test.js b/test/lib/conductor/sessions.test.js index fef92e4..bc65973 100644 --- a/test/lib/conductor/sessions.test.js +++ b/test/lib/conductor/sessions.test.js @@ -32,7 +32,9 @@ describe('conductor sessions', () => { expect(g.working.map(s => s.id)).toEqual(['aaaa1111']); expect(g.waiting_on_cc.map(s => s.id)).toEqual(['bbbb2222']); expect(g.done.map(s => s.id)).toEqual(['cccc3333']); - expect(g.unknown.map(s => [s.id, s.raw_state])).toEqual([['dddd4444', 'stopped'], [null, null]]); + // dddd4444 is `stopped` but still carries a pid, so it stays visible as unknown + expect(g.unknown.map(s => [s.id, s.raw_state])).toEqual([['dddd4444', 'stopped']]); + expect(g.interactive.map(s => [s.id, s.raw_state])).toEqual([[null, null]]); }); test('an unrecognised state is never treated as done', () => { @@ -56,6 +58,38 @@ describe('conductor sessions', () => { expect(g.working.map(s => s.id)).toEqual(['aaaa0004']); }); + test('stopped and failed background sessions with no pid are grouped as stopped; interactive sessions get their own group; the rest stays unknown', () => { + const raw = [ + { id: 'aaaa0001', sessionId: 'aaaa0001-x', kind: 'background', state: 'stopped' }, + { id: 'aaaa0002', sessionId: 'aaaa0002-x', kind: 'background', state: 'failed' }, + { id: 'aaaa0003', sessionId: 'aaaa0003-x', kind: 'background', state: 'done' }, + { id: 'aaaa0004', sessionId: 'aaaa0004-x', kind: 'background', state: 'stopped', pid: 12 }, + { id: 'aaaa0005', sessionId: 'aaaa0005-x', kind: 'background', state: 'finished' }, + { id: 'aaaa0006', sessionId: 'aaaa0006-x', kind: 'background' }, + { sessionId: 'bbbb0001-x', kind: 'interactive', status: 'idle', pid: 20 }, + { sessionId: 'bbbb0002-x', kind: 'interactive', status: 'busy', pid: 21 }, + { sessionId: 'bbbb0003-x', kind: 'interactive', status: 'idle', state: 'odd', pid: 22 }, + ]; + const sessions = classifySessions(raw, { hookStateDir: hookDir, now: NOW }); + const g = groupSessions(sessions); + expect(g.stopped.map(s => [s.id, s.raw_state])).toEqual([['aaaa0001', 'stopped'], ['aaaa0002', 'failed'], ['aaaa0003', 'done']]); + expect(g.interactive.map(s => [s.session_id, s.status])).toEqual([['bbbb0001-x', 'idle'], ['bbbb0002-x', 'busy']]); + // a stopped state that still has a live pid is odd: it stays visible as unknown, not hidden as stopped + expect(g.unknown.map(s => s.id)).toEqual(['aaaa0004', 'aaaa0005', 'aaaa0006', null]); + expect(g.unknown.at(-1).session_id).toBe('bbbb0003-x'); + // the classification the reapers read is unchanged: every one of these is still state "unknown" + expect(sessions.filter(s => s.raw_state !== 'done').every(s => s.state === 'unknown')).toBe(true); + }); + + test('the successor id is read from `(bg `, not from a session name that contains "bg "', () => { + const raw = agentsFixture(); + fs.writeFileSync(path.join(hookDir, `${raw[2].sessionId}.handoff`), + '"fix bg dddd4444 lane" (bg aaaa1111, claude-opus-5-5) from /x/handoff.md'); + expect(classify(raw).find(s => s.id === 'cccc3333')).toMatchObject({ successor: 'aaaa1111' }); + fs.writeFileSync(path.join(hookDir, `${raw[2].sessionId}.handoff`), '"fix bg dddd4444 lane" from /x/handoff.md'); + expect(classify(raw).find(s => s.id === 'cccc3333')).toMatchObject({ handed_off: true, successor: null }); + }); + test('age comes from startedAt and the injected clock; waitingFor is carried', () => { const s = classify().find(x => x.id === 'bbbb2222'); expect(s.age_ms).toBe(5 * 3_600_000);