From 016544bc10629f27a95701473ea02739ca1f97f2 Mon Sep 17 00:00:00 2001 From: Benjamin Carrasco Date: Sat, 5 Dec 2026 18:59:05 +0100 Subject: [PATCH] feat(orchestrator): renew in the same thread from GitHub and the T3 thread list The orchestrator throws its context away after each finished goal and after a compaction (T3 fresh session, or a successor thread where T3 refuses it), then rebuilds its picture from `tracker.mjs board` and `lane.mjs lanes`, which reads its lanes from the T3 thread list by parent and `#` title. Owner questions live on their issue as a comment plus `ready-for-human`, so a renewal does not ask them again. --- README.md | 3 +- plugins/bstack/scripts/lane.mjs | 29 ++++++++++++-- plugins/bstack/skills/orchestrator/SKILL.md | 20 ++++++++-- tests/lane.test.mjs | 44 ++++++++++++++++++++- 4 files changed, 86 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index f6bc362..f2e1bab 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,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. -- 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 diff --git a/plugins/bstack/scripts/lane.mjs b/plugins/bstack/scripts/lane.mjs index f9274d9..f2f51dd 100644 --- a/plugins/bstack/scripts/lane.mjs +++ b/plugins/bstack/scripts/lane.mjs @@ -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 [ ...] < 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 `#`, 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 [ ...] < 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')); @@ -71,9 +74,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)) { @@ -83,7 +104,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); diff --git a/plugins/bstack/skills/orchestrator/SKILL.md b/plugins/bstack/skills/orchestrator/SKILL.md index 992431c..7165b79 100644 --- a/plugins/bstack/skills/orchestrator/SKILL.md +++ b/plugins/bstack/skills/orchestrator/SKILL.md @@ -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 -R ` prints `{ model, effort }` (`research` or `mechanical` for those kinds; `--failed ` 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 --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 `# `, 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 , 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 `# ` (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 , made from origin's default branch; remove it when done." Constraints every build brief carries: - Build issue in through the bstack ticket and ship playbooks; a `bstack:judge` you start judges. @@ -38,13 +38,27 @@ Then end the turn. T3 wakes the orchestrator when a lane finishes or asks; never 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`: report it with its open findings; what to do at the cap is the owner's until its own ticket says otherwise. - 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. Send the answers to the lanes that asked, and record each on its ticket. +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. + +## 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: `, 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 []`. 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 `: 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 diff --git a/tests/lane.test.mjs b/tests/lane.test.mjs index 1474da2..6ffc4a1 100644 --- a/tests/lane.test.mjs +++ b/tests/lane.test.mjs @@ -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'); @@ -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 #', () => { + 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('`# `'), 'lane titles are what a renewal finds lanes by'); + assert.match(skill, /freshContext: true/); });