From 7fe7eaff0d4c65605175aaf2fba5587a309e1d47 Mon Sep 17 00:00:00 2001 From: Benjamin Carrasco Date: Sat, 5 Dec 2026 18:14:01 +0100 Subject: [PATCH] feat(model): model.mjs lane prints the model and effort a lane starts on (#321) Lane rows live in MODEL_TABLE: mechanical on haiku at low effort, research on sonnet at medium, build on the worker row at medium (high when the expected tier is 2 or unknown), and fable at high only for a build lane with two failed lanes (--failed). An unknown kind, bad arguments or an unreadable ticket exit 2 with the usage line. --- plugins/bstack/scripts/guard.mjs | 14 +++++---- plugins/bstack/scripts/lib/models.mjs | 18 +++++++++-- plugins/bstack/scripts/model.mjs | 43 ++++++++++++++++++++++++--- tests/judge.test.mjs | 35 +++++++++++++++++++++- 4 files changed, 97 insertions(+), 13 deletions(-) diff --git a/plugins/bstack/scripts/guard.mjs b/plugins/bstack/scripts/guard.mjs index 5b048c9..d43a4b9 100644 --- a/plugins/bstack/scripts/guard.mjs +++ b/plugins/bstack/scripts/guard.mjs @@ -182,17 +182,21 @@ function repoExposure(repo, cwd, deps) { } function workerFacts(ref, cwd, deps, exposureOfRepo) { - const repo = ref[1] ?? ref[3]; try { - const issue = JSON.parse(deps.gh(cwd, ['issue', 'view', ref[2] ?? ref[4], ...(repo ? ['-R', repo] : []), '--json', 'body,url'])); - const expectedTier = Number(EXPECTED_TIER.exec(String(issue.body ?? ''))?.[1] ?? NaN); - const home = /^https:\/\/github\.com\/([\w.-]+\/[\w.-]+)\/issues\/\d+$/.exec(String(issue.url ?? ''))?.[1]; - return { expectedTier, exposure: home ? exposureOfRepo(home) : 'external' }; + return ticketFacts(ref[2] ?? ref[4], ref[1] ?? ref[3], cwd, deps, exposureOfRepo); } catch { return {}; } } +// The expected tier and the exposure of a ticket's repo; throws when the ticket cannot be read. +export function ticketFacts(number, repo, cwd, deps, exposureOfRepo = home => repoExposure(home, cwd, deps)) { + const issue = JSON.parse(deps.gh(cwd, ['issue', 'view', number, ...(repo ? ['-R', repo] : []), '--json', 'body,url'])); + const expectedTier = Number(EXPECTED_TIER.exec(String(issue.body ?? ''))?.[1] ?? NaN); + const home = /^https:\/\/github\.com\/([\w.-]+\/[\w.-]+)\/issues\/\d+$/.exec(String(issue.url ?? ''))?.[1]; + return { expectedTier, exposure: home ? exposureOfRepo(home) : 'external' }; +} + const REAL_HOST = { platform: process.platform, env: process.env, tmpdir: () => os.tmpdir() }; const withPaths = host => ({ ...host, paths: pathsOn(host.platform) }); diff --git a/plugins/bstack/scripts/lib/models.mjs b/plugins/bstack/scripts/lib/models.mjs index 721fdf8..e91585a 100644 --- a/plugins/bstack/scripts/lib/models.mjs +++ b/plugins/bstack/scripts/lib/models.mjs @@ -4,7 +4,8 @@ // when it has the value. `model: null` runs the session's model; `unknown` is a judge whose PR or // tier the guard could not read; `needed: false` is a change that merges without a judge, though one // started on it gets this model. The judge rows are the owner's decision on #226; a later change to -// it changes rows here, and nothing else. +// it changes rows here, and nothing else. A lane row without `model` starts on the worker row's model; +// `{ min }` holds for a count at least that high. export const MODEL_TABLE = { judge: [ { unknown: true, model: 'opus' }, @@ -17,10 +18,21 @@ export const MODEL_TABLE = { { expectedTier: [0, 1], exposure: 'internal', model: 'sonnet' }, { model: null }, ], + lane: [ + { kind: 'mechanical', model: 'haiku', effort: 'low' }, + { kind: 'research', model: 'sonnet', effort: 'medium' }, + { kind: 'build', failedLanes: { min: 2 }, model: 'fable', effort: 'high' }, + { kind: 'build', expectedTier: [0, 1], effort: 'medium' }, + { kind: 'build', effort: 'high' }, + ], }; -const CHOICES = new Set(['model', 'needed']); -const holds = (want, have) => (Array.isArray(want) ? want.includes(have) : want === have); +const CHOICES = new Set(['model', 'needed', 'effort']); +const holds = (want, have) => { + if (Array.isArray(want)) return want.includes(have); + if (want && typeof want === 'object') return have >= want.min; + return want === have; +}; export const rowFor = (role, facts) => (MODEL_TABLE[role] ?? []) .find(row => Object.entries(row).every(([field, want]) => CHOICES.has(field) || holds(want, facts[field]))) ?? null; diff --git a/plugins/bstack/scripts/model.mjs b/plugins/bstack/scripts/model.mjs index 79b8339..d150835 100644 --- a/plugins/bstack/scripts/model.mjs +++ b/plugins/bstack/scripts/model.mjs @@ -1,21 +1,56 @@ #!/usr/bin/env node // The model a judge or worker starts on, from MODEL_TABLE, for a workflow stage that starts agents // without the Agent tool and so without the guard's hook (#227). Prints the model, `session` for the -// session's model, or `none` for a change that needs no judge. -import { agentRow, realDeps } from './guard.mjs'; +// session's model, or `none` for a change that needs no judge. `lane` prints the model and effort a +// lane starts on as JSON (#321); `--failed` counts the ticket's failed lanes on the largest regular model. +import { agentRow, realDeps, ticketFacts } from './guard.mjs'; import { isMain } from './lib/is-main.mjs'; +import { rowFor } from './lib/models.mjs'; -const USAGE = 'usage: model.mjs judge -R owner/repo | model.mjs worker [-R owner/repo]'; +const USAGE = 'usage: model.mjs judge -R owner/repo | model.mjs worker [-R owner/repo]' + + ' | model.mjs lane build|research|mechanical [] [-R owner/repo] [--failed ]'; +const REPO = /^[\w.-]+\/[\w.-]+$/; +const COUNT = /^\d+$/; export function modelOf(args, deps, cwd) { + if (args[0] === 'lane') return JSON.stringify(laneOf(args.slice(1), deps, cwd)); const [role, number, flag, repo] = args; - const valid = /^\d+$/.test(number ?? '') && (flag === undefined || (flag === '-R' && /^[\w.-]+\/[\w.-]+$/.test(repo ?? ''))); + const valid = COUNT.test(number ?? '') && (flag === undefined || (flag === '-R' && REPO.test(repo ?? ''))); if (!valid || !(role === 'worker' || (role === 'judge' && repo))) throw new Error(USAGE); const prompt = role === 'judge' ? `PR ${number} in ${repo}` : `${repo ?? ''}#${number}`; const row = agentRow(role, prompt, cwd, deps); return row && row.needed !== false ? row.model ?? 'session' : 'none'; } +function laneOf([kind, ...rest], deps, cwd) { + const { issue, repo, failed } = laneArgs(rest); + let ticket = {}; + if (issue) { + try { + ticket = ticketFacts(issue, repo, cwd, deps); + } catch (e) { + throw new Error(`cannot read ticket ${repo ?? ''}#${issue}: ${String(e.message ?? e).split('\n')[0]}\n${USAGE}`); + } + } + const row = rowFor('lane', { kind, failedLanes: failed, ...ticket }); + if (!row) throw new Error(USAGE); + const model = 'model' in row ? row.model : rowFor('worker', ticket)?.model; + return { model: model ?? 'session', effort: row.effort }; +} + +function laneArgs(rest) { + const out = { failed: 0 }; + for (let i = 0; i < rest.length; i++) { + const [arg, value] = [rest[i], rest[i + 1]]; + if (arg === '-R' && REPO.test(value ?? '') && !out.repo) { out.repo = value; i++; } + else if (arg === '--failed' && COUNT.test(value ?? '')) { out.failed = Number(value); i++; } + else if (COUNT.test(arg) && i === 0) out.issue = arg; + else throw new Error(USAGE); + } + if (out.repo && !out.issue) throw new Error(USAGE); + return out; +} + if (isMain(import.meta.url)) { try { process.stdout.write(`${modelOf(process.argv.slice(2), realDeps, process.cwd())}\n`); diff --git a/tests/judge.test.mjs b/tests/judge.test.mjs index c0a2cf2..904ea05 100644 --- a/tests/judge.test.mjs +++ b/tests/judge.test.mjs @@ -247,7 +247,7 @@ test('other agents start unchanged and read nothing', () => { }); test('the model choice is one table: judge rows by tier and risk path, worker rows by expected tier and exposure', () => { - assert.deepEqual(Object.keys(MODEL_TABLE).sort(), ['judge', 'worker']); + assert.deepEqual(Object.keys(MODEL_TABLE).sort(), ['judge', 'lane', 'worker']); assert.deepEqual(MODEL_TABLE.judge.map(r => r.model), ['opus', 'opus', 'opus', 'sonnet', 'sonnet']); assert.equal(MODEL_TABLE.judge.at(-1).needed, false); assert.equal(MODEL_TABLE.worker.at(-1).model, null); @@ -264,6 +264,39 @@ test('#227: a workflow stage asks the same table for the model on the command li for (const args of [[], ['judge', '5'], ['judge', 'x', '-R', 'o/r'], ['builder', '5', '-R', 'o/r']]) assert.throws(() => ask(args, {}), /usage: model\.mjs/); }); +test('#321: a lane starts on the model and effort of its kind, a build lane on the worker row', async () => { + const { modelOf } = await import('../plugins/bstack/scripts/model.mjs'); + const lane = (args, opts = {}) => JSON.parse(modelOf(['lane', ...args], github(opts).d, '/repo')); + const internal = tier => ({ exposure: 'internal\n', issues: { 12: ticket(tier) } }); + assert.deepEqual(lane(['mechanical']), { model: 'haiku', effort: 'low' }); + assert.deepEqual(lane(['research']), { model: 'sonnet', effort: 'medium' }); + assert.deepEqual(lane(['mechanical', '12', '-R', 'o/r'], internal(2)), { model: 'haiku', effort: 'low' }); + assert.deepEqual(lane(['build', '12', '-R', 'o/r'], internal(1)), { model: 'sonnet', effort: 'medium' }); + assert.deepEqual(lane(['build', '12'], { issues: { 12: ticket(1) } }), { model: 'session', effort: 'medium' }); + assert.deepEqual(lane(['build', '12', '-R', 'o/r'], internal(2)), { model: 'session', effort: 'high' }); + assert.deepEqual(lane(['build']), { model: 'session', effort: 'high' }, 'no ticket, no known tier: the riskier effort'); + assert.deepEqual(lane(['build', '12', '-R', 'o/r', '--failed', '2'], internal(1)), { model: 'fable', effort: 'high' }); + assert.deepEqual(lane(['build', '--failed', '3']), { model: 'fable', effort: 'high' }); + assert.deepEqual(lane(['build', '12', '-R', 'o/r', '--failed', '1'], internal(1)), { model: 'sonnet', effort: 'medium' }); + for (const kind of ['research', 'mechanical']) assert.notEqual(lane([kind, '--failed', '5']).model, 'fable', `${kind} never gets the strongest model`); +}); + +test('#321: only a build lane with two failed lanes selects the strongest model, and the lane rows set every effort', () => { + const strongest = MODEL_TABLE.lane.filter(r => r.model === 'fable'); + assert.deepEqual(strongest, [{ kind: 'build', failedLanes: { min: 2 }, model: 'fable', effort: 'high' }]); + for (const role of ['judge', 'worker']) assert.ok(MODEL_TABLE[role].every(r => r.model !== 'fable')); + assert.ok(MODEL_TABLE.lane.every(r => ['low', 'medium', 'high'].includes(r.effort))); +}); + +test('#321: an unknown kind, bad arguments or a ticket that cannot be read exit with the usage line', async () => { + const { modelOf } = await import('../plugins/bstack/scripts/model.mjs'); + const ask = args => modelOf(['lane', ...args], github({ issues: { 12: ticket(1) } }).d, '/repo'); + for (const args of [[], ['review'], ['build', 'x'], ['build', '12', '-R'], ['build', '-R', 'o/r'], ['build', '--failed'], ['build', '--failed', 'two'], ['build', '12', '13']]) { + assert.throws(() => ask(args), /usage: model\.mjs/, JSON.stringify(args)); + } + assert.throws(() => ask(['build', '13', '-R', 'o/r']), /cannot read ticket o\/r#13[\s\S]*usage: model\.mjs/); +}); + test('PR 267 F1: a blocking result on a head closes the gate for that head, whatever result comes after it', () => { const blocked = [verdict('PASS'), interrogate(2)]; for (const opts of [{ diff: DATA_DIFF }, { diff: CODE_DIFF, riskPaths: 'app/**\n' }]) {