Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions commands/conductor-queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// commands/conductor-queue.js
//
// `bobby conductor next` and `bobby conductor queue` (BOB-180). Kept in its own file so
// commands/conductor.js only imports and calls registerQueueCommands. `queue` only reads.
// `next` reads the sessions and the board and writes one claim file (see lib/conductor/queue.js).

import { findStudioRoot, loadConductorConfig } from '../lib/conductor/config.js';
import { pickNext, queueView, renderNext, renderQueue } from '../lib/conductor/queue.js';
import { assertM4Host } from '../lib/conductor/host.js';
import { error } from '../lib/colors.js';

function fail(e) {
error(e.message);
process.exit(1);
}

export function registerQueueCommands(conductor) {
conductor
.command('next')
.description('Pull the next ticket from the ranked queue (.bobby/conductor-queue.yml): WIP cap, governor, then finish-first. Records a claim. M4 only')
.option('--lane <name>', 'Name of the lane pulling, recorded on the claim')
.action((opts) => {
try {
assertM4Host();
const studioRoot = findStudioRoot();
const config = loadConductorConfig(studioRoot);
const result = pickNext({ studioRoot, config, lane: opts.lane ?? null });
if (result.kind === 'refused') {
error(`next refused: ${result.reason}`);
process.exit(1);
}
console.log(renderNext(result));
} catch (e) {
fail(e);
}
});

conductor
.command('queue')
.description('The ranked queue with each ticket\'s stage and whether a lane or a claim holds it (read-only)')
.action(() => {
try {
const studioRoot = findStudioRoot();
const config = loadConductorConfig(studioRoot);
console.log(renderQueue(queueView({ studioRoot, config })));
} catch (e) {
fail(e);
}
});
}
4 changes: 4 additions & 0 deletions commands/conductor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
inboxDir, assertInboxDir, resolveRecipient, buildMessage, appendMessage,
listInboxSessions, readMessages, cursorWithin, renderMessages,
} from '../lib/conductor/inbox.js';
import { registerQueueCommands } from './conductor-queue.js';
import { success, error, dim } from '../lib/colors.js';

function fail(e) {
Expand All @@ -45,6 +46,7 @@ export function registerConductor(program) {
console.log(dim(` hook_state_dir: ${values.hook_state_dir}`));
console.log(dim(` governor: min_free_ram_mb ${values.governor.min_free_ram_mb}, max_docker_stacks ${values.governor.max_docker_stacks}`));
console.log(dim(` reaper: stack_idle_hours ${values.reaper.stack_idle_hours}`));
console.log(dim(` wip: max_lanes ${values.wip.max_lanes}; claims: ttl_minutes ${values.claims.ttl_minutes}`));
} catch (e) {
fail(e);
}
Expand Down Expand Up @@ -174,4 +176,6 @@ export function registerConductor(program) {
fail(e);
}
});

registerQueueCommands(conductor);
}
22 changes: 21 additions & 1 deletion lib/conductor/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const CONDUCTOR_FILE = 'conductor.yml';
const INIT_GOVERNOR = { min_free_ram_mb: 4096, max_docker_stacks: 4 };
// BOB-177: CC set 2 hours in the BOB-173 scope.
const INIT_REAPER = { stack_idle_hours: 2 };
// BOB-180: CC set 6 lanes (BOB-173 decision, 2026-11-08). The claim TTL was not named by CC;
// 30 minutes covers the gap between a lane pulling a ticket and its session showing the ID.
const INIT_WIP = { max_lanes: 6 };
const INIT_CLAIMS = { ttl_minutes: 30 };

/**
* Nearest ancestor of `startDir` whose .bobbyrc.yml carries a `studio:` key.
Expand Down Expand Up @@ -66,7 +70,19 @@ function validateConfig(parsed, file) {
}
const governor = validateGovernor(parsed.governor, file);
const reaper = validateReaper(parsed.reaper, file);
return { projects: [...projects], hook_state_dir: dir, governor, reaper };
const wip = validatePositiveInt(parsed.wip, file, 'wip', 'max_lanes');
const claims = validatePositiveInt(parsed.claims, file, 'claims', 'ttl_minutes');
return { projects: [...projects], hook_state_dir: dir, governor, reaper, wip, claims };
}

// A section with one required positive-integer key: { <key>: n }.
function validatePositiveInt(section, file, name, key) {
if (section === undefined || section === null) throw fail(file, `${name}.${key}`, 'is missing');
if (typeof section !== 'object' || Array.isArray(section)) throw fail(file, name, 'must be a mapping');
if (!(key in section) || section[key] === null) throw fail(file, `${name}.${key}`, 'is missing');
const n = section[key];
if (!Number.isInteger(n) || n <= 0) throw fail(file, `${name}.${key}`, 'must be a positive integer');
return { [key]: n };
}

function validateReaper(reaper, file) {
Expand Down Expand Up @@ -130,6 +146,8 @@ export function initConductorConfig(studioRoot, { projects } = {}) {
hook_state_dir: path.join(os.homedir(), '.bobby', 'hook-state'),
governor: { ...INIT_GOVERNOR },
reaper: { ...INIT_REAPER },
wip: { ...INIT_WIP },
claims: { ...INIT_CLAIMS },
};
const body = [
'# bobby conductor config. Written by `bobby conductor init`; every key is required.',
Expand All @@ -139,6 +157,8 @@ export function initConductorConfig(studioRoot, { projects } = {}) {
'# governor.min_free_ram_mb: `conductor check` refuses below this much free RAM (CC, 2026-11-08).',
'# governor.max_docker_stacks: `conductor check stack` refuses at this many running stacks (CC, 2026-11-08).',
'# reaper.stack_idle_hours: `conductor reap --stacks` stops a ticket stack idle this long with no working lane (CC, 2026-11-08).',
'# wip.max_lanes: `conductor next` refuses when this many live lane sessions (working or waiting on CC) exist (CC, 2026-11-08).',
'# claims.ttl_minutes: a ticket `conductor next` handed out is not handed out again for this long.',
YAML.stringify(values),
].join('\n');
fs.mkdirSync(path.dirname(file), { recursive: true });
Expand Down
251 changes: 251 additions & 0 deletions lib/conductor/queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
// lib/conductor/queue.js
//
// The ranked priority queue and `bobby conductor next` (BOB-180).
//
// The queue is one file CC edits by hand, <studio root>/.bobby/conductor-queue.yml: a
// `queue:` list of ticket IDs across the conductor boards, top first. Nothing outside the
// list is ever picked, and nothing here starts a session (v1 is pull-only: a lane calls
// `conductor next` and runs what it is told).
//
// `next`, in order:
// 1. queue file missing, malformed or naming an unknown ticket = Error. An empty list
// = "queue empty".
// 2. WIP: live sessions that are working or waiting on CC (the session reader's
// `working` and `blocked` groups, process alive) must stay under wip.max_lanes.
// 3. Governor: the BOB-175 lane check (free RAM).
// 4. Candidates: queued tickets not done, not blocked, with no live lane naming them
// (the BOB-177 name/cwd match) and no unexpired claim.
// 5. Finish-first: the candidate furthest along the pipeline (STAGE_ORDER: shipping,
// testing, reviewing, ... planning, backlog); ties break by queue order.
// 6. The pick is recorded in <studio root>/.bobby/conductor/claims.yml so two lanes never
// pull the same ticket. The file is rewritten whole, keeping every entry; a claim
// older than claims.ttl_minutes is ignored when read.

import fs from 'fs';
import path from 'path';
import YAML from 'yaml';
import { readAgents, classifySessions } from './sessions.js';
import { boardTickets, laneStateFor } from './stacks.js';
import { checkLane } from './governor.js';
import { readMemory } from './resources.js';
import { run as defaultRun } from './exec.js';
import { STAGE_ORDER } from '../tickets.js';

export const QUEUE_FILE = 'conductor-queue.yml';
const CLAIMS_DIR = 'conductor';
const CLAIMS_FILE = 'claims.yml';
const TICKET_ID_RE = /^[A-Za-z][A-Za-z0-9]*-\d+$/;
const MINUTE_MS = 60_000;

// Stage -> `bobby run` agent. Stages with no agent of their own are run by `bobby run next`,
// which dispatches on the ticket's current stage. Every STAGE_ORDER stage is listed; an
// unlisted stage is an error, not a guess.
const STAGE_AGENT = {
backlog: 'plan',
planning: 'plan',
building: 'build',
security: 'security',
reviewing: 'review',
testing: 'test',
shipping: 'ship',
'design-research': 'next',
'design-analyze': 'next',
'design-mockup': 'next',
'design-spec': 'next',
'define-brief': 'next',
'define-personas': 'next',
'define-journeys': 'next',
'define-features': 'next',
'define-blueprint': 'next',
};

export function queueFilePath(studioRoot) {
return path.join(studioRoot, '.bobby', QUEUE_FILE);
}

export function claimsFilePath(studioRoot) {
return path.join(studioRoot, '.bobby', CLAIMS_DIR, CLAIMS_FILE);
}

/** Ranked ticket IDs, top first. Missing file, bad shape or a duplicate = Error. [] is a valid queue. */
export function loadQueue(studioRoot) {
const file = queueFilePath(studioRoot);
if (!fs.existsSync(file)) {
throw new Error(`${file} does not exist. Create it with a "queue:" list of ticket IDs (an empty list is fine); \`conductor next\` has no built-in list.`);
}
let parsed;
try {
parsed = YAML.parse(fs.readFileSync(file, 'utf8'));
} catch (e) {
throw new Error(`${file}: invalid YAML (${e.message})`);
}
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed) || !('queue' in parsed)) {
throw new Error(`${file}: expected a mapping with a "queue" key (a list of ticket IDs)`);
}
const { queue } = parsed;
if (!Array.isArray(queue)) throw new Error(`${file}: key "queue" must be a list of ticket IDs`);
const seen = new Set();
for (const id of queue) {
if (typeof id !== 'string' || !TICKET_ID_RE.test(id)) {
throw new Error(`${file}: "${String(id)}" is not a ticket ID like RF-1234`);
}
if (seen.has(id.toUpperCase())) throw new Error(`${file}: ticket ${id} is listed twice`);
seen.add(id.toUpperCase());
}
return [...queue];
}

/** Claims by ticket ID: { <ID>: { claimed_at (ISO), lane (string|null) } }. A missing file = no claims yet. */
export function readClaims(studioRoot) {
const file = claimsFilePath(studioRoot);
if (!fs.existsSync(file)) return {};
let parsed;
try {
parsed = YAML.parse(fs.readFileSync(file, 'utf8'));
} catch (e) {
throw new Error(`${file}: invalid YAML (${e.message})`);
}
const claims = parsed && typeof parsed === 'object' ? parsed.claims : undefined;
if (!claims || typeof claims !== 'object' || Array.isArray(claims)) {
throw new Error(`${file}: expected a mapping with a "claims" mapping`);
}
for (const [id, c] of Object.entries(claims)) {
if (!c || typeof c.claimed_at !== 'string' || Number.isNaN(Date.parse(c.claimed_at))) {
throw new Error(`${file}: claim ${id} needs an ISO "claimed_at"`);
}
}
return claims;
}

function isClaimed(claims, id, { ttlMinutes, now }) {
const c = claims[id];
return Boolean(c) && now - Date.parse(c.claimed_at) < ttlMinutes * MINUTE_MS;
}

/** Record `id` as claimed. The file is rewritten whole with every existing entry kept. */
export function writeClaim(studioRoot, id, { lane, now }) {
const claims = { ...readClaims(studioRoot), [id]: { claimed_at: new Date(now).toISOString(), lane: lane ?? null } };
const file = claimsFilePath(studioRoot);
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, `# Written by \`bobby conductor next\`. A claim older than claims.ttl_minutes is ignored.\n${YAML.stringify({ claims })}`);
}

/** The `bobby run` command for a ticket in `stage`, with the project it lives on. */
export function runHint(project, id, stage) {
const agent = STAGE_AGENT[stage];
if (!agent) throw new Error(`no \`bobby run\` agent is known for stage "${stage}"`);
return `BOBBY_PROJECT=${project} bobby run ${agent} ${id}`;
}

/** Live sessions that count against wip.max_lanes: working or waiting on CC, process alive. */
export function laneSessions(sessions) {
return sessions.filter(s => s.live && (s.state === 'working' || s.state === 'blocked'));
}

/**
* Resolve each queued ID to its board ticket. An ID on no conductor board = Error.
* Returns [{ position (1-based), id, project, stage }].
*/
export function resolveQueue(queue, tickets) {
return queue.map((raw, i) => {
const m = /^([A-Za-z][A-Za-z0-9]*)-(\d+)$/.exec(raw);
const found = tickets.get(`${m[1].toUpperCase()}-${Number(m[2])}`);
if (!found) throw new Error(`queue names ${raw}, which is not on any conductor board`);
return { position: i + 1, id: found.id, project: found.project, stage: found.stage, blocked: found.blocked };
});
}

function rankOf(stage) {
if (!(stage in STAGE_ORDER)) throw new Error(`queued ticket stage "${stage}" has no finish-first rank`);
return STAGE_ORDER[stage];
}

function snapshot({ studioRoot, config, run, now }) {
const sessions = classifySessions(readAgents({ run }), { hookStateDir: config.hook_state_dir, now });
const resolved = resolveQueue(loadQueue(studioRoot), boardTickets(studioRoot, config.projects));
return { sessions, resolved };
}

/**
* `conductor next`. Returns one of
* { kind: 'empty' }
* { kind: 'refused', reason }
* { kind: 'none', skipped: [{ id, why }] } nothing in the queue is pullable
* { kind: 'picked', ticket: { id, project, stage, position }, hint, skipped }
* Throws on any unreadable input; a failing probe is never read as "nothing to do".
*/
export function pickNext({ studioRoot, config, run = defaultRun, now = Date.now(), lane = null, memory = null }) {
const { sessions, resolved } = snapshot({ studioRoot, config, run, now });
if (resolved.length === 0) return { kind: 'empty' };

const active = laneSessions(sessions).length;
const cap = config.wip.max_lanes;
if (active >= cap) {
return { kind: 'refused', reason: `${active} live lane session(s) (working or waiting on CC) >= wip.max_lanes ${cap}` };
}
const governor = checkLane(config, memory || readMemory({ run }));
if (!governor.ok) return { kind: 'refused', reason: governor.reason };

const claims = readClaims(studioRoot);
const lanes = laneSessions(sessions);
const skipped = [];
const candidates = [];
for (const t of resolved) {
if (t.stage === 'done') { skipped.push({ id: t.id, why: 'done' }); continue; }
if (t.stage === 'blocked' || t.blocked) { skipped.push({ id: t.id, why: 'blocked' }); continue; }
const held = laneStateFor(t.id, lanes);
if (held !== 'none') { skipped.push({ id: t.id, why: `a lane already holds it (${held})` }); continue; }
if (isClaimed(claims, t.id, { ttlMinutes: config.claims.ttl_minutes, now })) {
skipped.push({ id: t.id, why: `claimed by ${claims[t.id].lane || 'another lane'} within ${config.claims.ttl_minutes} min` });
continue;
}
candidates.push({ ...t, rank: rankOf(t.stage) });
}
if (candidates.length === 0) return { kind: 'none', skipped };

// Lowest STAGE_ORDER = furthest along; Array#sort is stable, so queue order breaks ties.
const pick = [...candidates].sort((a, b) => a.rank - b.rank)[0];
writeClaim(studioRoot, pick.id, { lane, now });
return {
kind: 'picked',
ticket: { id: pick.id, project: pick.project, stage: pick.stage, position: pick.position },
hint: runHint(pick.project, pick.id, pick.stage),
skipped,
};
}

/** `conductor queue`: every ranked ticket with its stage and whether a lane or a claim holds it. */
export function queueView({ studioRoot, config, run = defaultRun, now = Date.now() }) {
const { sessions, resolved } = snapshot({ studioRoot, config, run, now });
const lanes = laneSessions(sessions);
const claims = readClaims(studioRoot);
return resolved.map(t => ({
position: t.position,
id: t.id,
project: t.project,
stage: t.stage,
blocked: t.stage === 'blocked' || t.blocked,
lane: laneStateFor(t.id, lanes),
claimed: isClaimed(claims, t.id, { ttlMinutes: config.claims.ttl_minutes, now }),
}));
}

export function renderQueue(rows) {
if (rows.length === 0) return 'queue empty';
return rows.map(r => {
const notes = [];
if (r.blocked) notes.push('blocked');
notes.push(r.lane === 'none' ? 'no lane' : `lane ${r.lane}`);
if (r.claimed) notes.push('claimed');
return ` ${String(r.position).padStart(2)}. ${r.id} ${r.stage} (${notes.join(', ')})`;
}).join('\n');
}

export function renderNext(result) {
if (result.kind === 'empty') return 'queue empty';
if (result.kind === 'none') {
return ['nothing to pull: no queued ticket is pullable', ...result.skipped.map(s => ` ${s.id} ${s.why}`)].join('\n');
}
const t = result.ticket;
return [`${t.id} ${t.stage} (queue #${t.position}, project ${t.project})`, ` ${result.hint}`].join('\n');
}
4 changes: 2 additions & 2 deletions lib/conductor/stacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ export function mapStoppedStacks(stopped, { tickets }) {
});
}

/** Map of ticket key (see ticketKeyFromProject) -> { id, project, stage } across the conductor boards. */
/** Map of ticket key (see ticketKeyFromProject) -> { id, project, stage, blocked } across the conductor boards. */
export function boardTickets(studioRoot, projects) {
const map = new Map();
for (const project of projects) {
const config = configForProject(studioRoot, project);
for (const t of listTickets(resolveTicketsDir(studioRoot, config))) {
const key = ticketKey(t.id);
if (key) map.set(key, { id: t.id, project, stage: t.stage || 'backlog' });
if (key) map.set(key, { id: t.id, project, stage: t.stage || 'backlog', blocked: Boolean(t.blocked) });
}
}
return map;
Expand Down
Loading
Loading