Skip to content
Open
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ Start a new thread in T3 Code, in any project, on your usual model at medium eff
- Each ticket becomes a lane: a child thread started with `delegate_task` in a worktree from the default branch, with a brief of Goal, Done when, Constraints and Result form (`scripts/lane.mjs brief`). The lane is a full bstack session: it builds, starts its own judge and merges.
- A lane answers with JSON that matches `skills/orchestrator/lane-result.schema.json` (`scripts/lane.mjs result` checks it). The orchestrator reads no diff, log or transcript.
- A spec runs as a queue (`/bstack:orchestrator kør #318 i owner/repo`): a research lane names the areas each ticket touches, and `scripts/queue.mjs` puts tickets that share an area in one lane in blocker order and starts the next ready ticket when a lane merges, at most three builders at once. It ends when the spec is closed, or lists the tickets that wait for you.
- It answers technical questions itself and records them on the ticket. Questions on the stop list, and about goal, scope or priority, come to you together, each with a recommended answer, while the other lanes go on.
- It answers technical questions itself and records them on the ticket. Questions on the stop list, and about goal, scope or priority, come to you together, each with a recommended answer, while the other lanes go on. Each question is also a comment on its issue, labelled `ready-for-human`.
- After each finished goal, and after a compaction, it restarts itself in the same thread with a fresh context and rebuilds its picture from `scripts/tracker.mjs board` and the T3 thread list (`scripts/lane.mjs lanes`). Running lanes are picked up, and a question already asked is not asked again.

## What the hooks enforce

Expand Down
29 changes: 25 additions & 4 deletions plugins/bstack/scripts/lane.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#!/usr/bin/env node
// The two ends of a lane the orchestrator starts (#323): the brief it sends, and the result it reads back.
// The two ends of a lane the orchestrator starts (#323): the brief it sends, and the result it reads back;
// and, after a renewal (#326), the lanes it started, read from the T3 thread list.
// lane.mjs brief < {"goal", "done", "constraints": [...]} or {"goal", "done", "open": true}
// lane.mjs result < the lane's last message
// lane.mjs lanes [<earlier orchestrator thread id> ...] < t3_thread_list output, one page or an array of pages
// `result` prints the lane result as compact JSON when it matches lane-result.schema.json, else exits 1
// with what did not fit.
// with what did not fit. `lanes` keeps the threads whose parent is the orchestrator and whose title starts
// with `<owner/repo>#<n>`, and prints them with the repos and the `-R` arguments for `tracker.mjs board`.
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { isMain } from './lib/is-main.mjs';

const USAGE = 'usage: lane.mjs brief|result < input';
const USAGE = 'usage: lane.mjs brief|result|lanes [<earlier orchestrator thread id> ...] < input';
export const SCHEMA_FILE = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'skills', 'orchestrator', 'lane-result.schema.json');
export const SCHEMA = JSON.parse(fs.readFileSync(SCHEMA_FILE, 'utf8'));

Expand Down Expand Up @@ -77,9 +80,27 @@ export function result(text) {
return JSON.stringify(value);
}

const LANE_TITLE = /^([\w.-]+\/[\w.-]+)#(\d+)\b/;
const FINISHED = new Set(['idle', 'completed', 'interrupted', 'failed', 'cancelled', 'rolled_back']);

export function lanes(pages, parents = []) {
const list = [pages].flat();
const current = list.find(p => p.currentThreadId)?.currentThreadId;
const own = new Set([current, ...parents].filter(Boolean));
if (!own.size) throw new Error('no currentThreadId in the thread list; pass the orchestrator thread id');
const found = list.flatMap(p => p.threads ?? [])
.filter(t => own.has(t.parentThreadId))
.map(t => ({ t, m: LANE_TITLE.exec(t.title ?? '') }))
.filter(({ m }) => m)
.map(({ t, m }) => ({ repo: m[1], ticket: Number(m[2]), thread: t.threadId, status: t.status, running: !FINISHED.has(t.status), link: t.link }));
const repos = [...new Set(found.map(l => l.repo))].sort();
return { orchestrator: [...own], lanes: found, repos, board: repos.flatMap(r => ['-R', r]).join(' ') };
}

const MODES = {
brief: input => brief(JSON.parse(input)),
result: input => `${result(input)}\n`,
lanes: (input, parents) => `${JSON.stringify(lanes(JSON.parse(input), parents))}\n`,
};

if (isMain(import.meta.url)) {
Expand All @@ -89,7 +110,7 @@ if (isMain(import.meta.url)) {
process.exit(2);
}
try {
process.stdout.write(mode(fs.readFileSync(0, 'utf8').replace(/^/, '')));
process.stdout.write(mode(fs.readFileSync(0, 'utf8').replace(/^/, ''), process.argv.slice(3)));
} catch (e) {
process.stderr.write(`${e.message}\n`);
process.exit(1);
Expand Down
20 changes: 17 additions & 3 deletions plugins/bstack/skills/orchestrator/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ As `bstack:plan` sizes it: one ticket; a spec with tickets; or questions to the

## Start a lane

One lane per ticket, in the ticket's repo, in its own worktree.
One lane per ticket, in the ticket's repo, in its own worktree. Never a second one: a ticket that is claimed, or that a lane from `lane.mjs lanes` (see Renewal) still runs, already has its lane.
1. Model: `node "${CLAUDE_PLUGIN_ROOT}/scripts/model.mjs" lane build <n> -R <owner/repo>` prints `{ model, effort }` (`research` or `mechanical` for those kinds; `--failed <n>` after failed lanes). Map the family to the provider's model id from `orchestrator_capabilities`; `session` means the orchestrator's own model.
2. Brief: `node "${CLAUDE_PLUGIN_ROOT}/scripts/lane.mjs" brief` with `{ "goal", "done", "constraints": [...] }` on stdin. It prints Goal, Done when, Constraints and Result form, in English; it refuses numbered steps, because a brief says what, never how. An open task (research, a question) gets `{ "goal", "done", "open": true }`: a goal and a done-when only.
3. Project: the `t3_project_list` entry whose `repositoryIdentity` names the repo. A repo that moved keeps its old owner there, so compare `gh repo view <remoteUrl> --json nameWithOwner`. No entry: `t3_project_clone` into a folder next to the other projects, then `t3_project_create` on it. Both need a full-access thread; when T3 refuses them (`capability_denied`), registering the repo is an owner question (access), recommended answer: the owner adds the folder as a project in T3.
4. `delegate_task` with that brief as `task`, `isolation: "worktree"`, `mode: "async"`, a `title` of `#<n> <ticket title>`, and `target`: that `projectId`, a Claude `providerInstanceId` that `canRunChildTask` and is not at its limit, `model`, and `options: { "effort": ... }`. When T3 refuses the `projectId` ("A local task runs in this thread's project"), start the lane without it and add the constraint "Work in your own git worktree of <the repo's clone>, made from origin's default branch; remove it when done."
4. `delegate_task` with that brief as `task`, `isolation: "worktree"`, `mode: "async"`, a `title` of `<owner/repo>#<n> <ticket title>` (a renewal finds its lanes by it), and `target`: that `projectId`, a Claude `providerInstanceId` that `canRunChildTask` and is not at its limit, `model`, and `options: { "effort": ... }`. When T3 refuses the `projectId` ("A local task runs in this thread's project"), start the lane without it and add the constraint "Work in your own git worktree of <the repo's clone>, made from origin's default branch; remove it when done."

Constraints every build brief carries:
- Build issue <url> in <owner/repo> through the bstack ticket and ship playbooks; a `bstack:judge` you start judges.
Expand Down Expand Up @@ -49,13 +49,16 @@ When the owner answers, remove the label and send the answer to the lane, or sta
Pipe the lane's last message to `node "${CLAUDE_PLUGIN_ROOT}/scripts/lane.mjs" result`. It prints the result as JSON matching [lane-result.schema.json](lane-result.schema.json), or what did not fit. A result that does not fit goes back to the lane with `t3_thread_send` and `outputSchema` set to the schema file; that is not reading its work.

- `merged`: read the issue and PR states back from GitHub, then report.
- A lane nothing waits on any more (merged and reported, or closed at the cap) gets its thread settled (`t3_thread_organize`); a lane with an open question stays unsettled, so a renewal still finds it.
- `needs_owner` or `blocked` with `questions`, or a pending question in the lane (`t3_pending_request list` on its thread): sort each question.
- **Technical** (how to build, which tool, a test approach, and any reversible choice inside the ticket's scope, even one that shapes behaviour): answer it yourself, send the answer to the lane's thread with `t3_thread_send`, and record it on the ticket in one comment (`gh issue comment`: the question, the answer, why).
- **The owner's**: on the stop list of the session rules, or about goal, scope, priority or a product choice no experiment can settle. Collect it; the other lanes go on.
- `capped`: see [At the cap](#at-the-cap).
- A judge verdict UNPROVEN (the lane reports what proof could not run where it is): the gate stays shut. Start a fresh judge where that proof can run, as a lane on a machine that has it (`t3_environment_links`), with the goal "Run bstack:judge on PR <n> in <owner/repo>" and done when its verdict for the head is posted. Only when no machine has it is it the owner's: the OK line the gate names.
- A lane that failed on the account limit (`Claude API rate limit reached`): start it again on another account from its pushed branch, or report it.

Before ending a turn with owner questions, ask them all at once, each with your recommended answer, and record each on the issue it is about: one comment (`Owner question:` the question, `Recommended:` your answer) and the label `ready-for-human`, so a renewal finds it on the board and does not ask it again. When the owner answers, send the answer to the lane that asked, comment it on the issue and remove the label.

## At the cap

A `capped` lane hands over its PR, head and open findings. Choose one:
Expand All @@ -65,7 +68,18 @@ A `capped` lane hands over its PR, head and open findings. Choose one:

Never merge over an open finding about data loss, security or access: that goes to the owner, as does a refused OK that another approach cannot fix. A ticket whose acceptance criterion still fails stays open: before the merge, the lane replaces `Closes #<n>` in the PR body with `Part of #<n>`, and the ticket gets a comment naming the follow-up issues.

Before ending a turn with owner questions, ask them all at once, each with your recommended answer. Send the answers to the lanes that asked, and record each on its ticket.
## Renewal

The context is thrown away on purpose, so the thread never wears out. Renew after every finished goal (its report is sent), and first thing when the context was compacted (it starts with a summary of earlier conversation); never in the middle of a turn that still starts lanes or asks the owner.
1. `t3_thread_configure` without arguments gives this thread's id. `t3_thread_send` to it with `freshContext: true`, `mode: "queue"` and the message `Renewal: load the bstack:orchestrator skill and rebuild the picture from GitHub and the T3 thread list.` Then end the turn. The message carries no state: no tickets, PRs or answers.
2. When T3 refuses a fresh session for the thread's own session, start a successor instead: `t3_thread_launch` in the same project with the same message plus `Earlier orchestrator thread: <id>`, give the owner its link, and settle this thread (`t3_thread_organize`). Lanes it started still wake this thread: then send their result on to the successor with `t3_thread_send` and do nothing else.

After a renewal, before anything else:
- `t3_thread_list` with `allProjects: true`, `includeSubagents: true` and `settled: false`, every page, piped as an array to `node "${CLAUDE_PLUGIN_ROOT}/scripts/lane.mjs" lanes [<earlier orchestrator thread id>]`. It prints this thread's lanes (repo, ticket, thread, status, `running`), the repos, and the `-R` arguments for the board.
- `node "${CLAUDE_PLUGIN_ROOT}/scripts/tracker.mjs" board <those -R arguments>`: claimed tickets with their PRs, open PRs with checks, verdict and rounds, and `readyForHuman`, the owner questions already asked.
- Running lanes are picked up, not restarted: they wake this thread when they finish. A finished lane whose ticket is still open and claimed: read its result once (its last message, `t3_thread_read` with `limit: 1`) and go on as when a lane wakes it.
- An issue in `readyForHuman` has its question asked: name it as waiting, never ask it again.
- Tell the owner the picture in one short message: running lanes, open PRs, waiting questions.

## To the owner

Expand Down
44 changes: 42 additions & 2 deletions tests/lane.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { spawnSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { SCHEMA, SCHEMA_FILE, brief, check, result } from '../plugins/bstack/scripts/lane.mjs';
import { SCHEMA, SCHEMA_FILE, brief, check, lanes, result } from '../plugins/bstack/scripts/lane.mjs';

const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const SCRIPT = path.join(ROOT, 'plugins', 'bstack', 'scripts', 'lane.mjs');
Expand Down Expand Up @@ -73,8 +73,48 @@ test('the command prints the result or exits 1 with the reason', () => {
assert.equal(run('nope', '').status, 2);
});

// #326: a renewed orchestrator finds its lanes in the T3 thread list by parent and title alone.
const ME = 'thread:orchestrator';
const thread = (title, status, parentThreadId = ME) => ({ threadId: `t-${title}`, title, status, parentThreadId, link: `[${title}](t3-thread://x)` });
const PAGE = {
currentThreadId: ME,
threads: [
thread('o/r#7 Build the board', 'running'),
thread('o/r#8 Renewal', 'completed'),
thread('a.b/c-d#12 Fix the export', 'waiting'),
thread('o/r#9 Someone else', 'running', 'thread:other'),
thread('Plan the spec', 'running'),
{ ...thread('o/r#10 no parent', 'running'), parentThreadId: null },
],
};

test('lanes are the threads this orchestrator started, named <owner/repo>#<n>', () => {
const out = lanes(PAGE);
assert.deepEqual(out.lanes.map(l => [l.repo, l.ticket, l.status, l.running]), [
['o/r', 7, 'running', true], ['o/r', 8, 'completed', false], ['a.b/c-d', 12, 'waiting', true],
]);
assert.deepEqual(out.repos, ['a.b/c-d', 'o/r']);
assert.equal(out.board, '-R a.b/c-d -R o/r');
assert.equal(out.lanes[0].thread, 't-o/r#7 Build the board');
});

test('a successor also picks up the lanes of the thread it replaced, over several pages', () => {
const next = { currentThreadId: 'thread:successor', threads: [thread('o/r#11 New lane', 'starting', 'thread:successor')] };
const out = lanes([{ ...PAGE, currentThreadId: 'thread:successor' }, next], [ME]);
assert.deepEqual(out.lanes.map(l => l.ticket), [7, 8, 12, 11]);
assert.throws(() => lanes({ threads: [] }), /currentThreadId/);
});

test('the lanes command reads the thread list from stdin', () => {
const ok = spawnSync(process.execPath, [SCRIPT, 'lanes', 'thread:other'], { input: `${JSON.stringify(PAGE)}`, encoding: 'utf8' });
assert.equal(ok.status, 0, ok.stderr);
assert.deepEqual(JSON.parse(ok.stdout).lanes.map(l => l.ticket), [7, 8, 12, 9]);
});

test('the skill points at the schema file and the commands it runs', () => {
const skill = fs.readFileSync(path.join(ROOT, 'plugins', 'bstack', 'skills', 'orchestrator', 'SKILL.md'), 'utf8');
assert.match(skill, /\]\(lane-result\.schema\.json\)/);
for (const cmd of ['scripts/model.mjs" lane', 'scripts/lane.mjs" brief', 'scripts/lane.mjs" result']) assert.ok(skill.includes(cmd), cmd);
for (const cmd of ['scripts/model.mjs" lane', 'scripts/lane.mjs" brief', 'scripts/lane.mjs" result', 'scripts/lane.mjs" lanes', 'scripts/tracker.mjs" board']) assert.ok(skill.includes(cmd), cmd);
assert.ok(skill.includes('`<owner/repo>#<n> <ticket title>`'), 'lane titles are what a renewal finds lanes by');
assert.match(skill, /freshContext: true/);
});
Loading