-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathparallel-tickets.jsx
More file actions
344 lines (311 loc) · 16.1 KB
/
Copy pathparallel-tickets.jsx
File metadata and controls
344 lines (311 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/**
* <ParallelTickets> — Triage → Wave-by-wave parallel execution → Merge queue.
*
* Pipeline:
* 1. Triage (Claude Opus) reads every ticket, builds a dependency graph,
* and groups tickets into waves. Tickets within a wave are independent
* and can run in parallel; waves run sequentially.
* 2. For each wave, every ticket runs concurrently in its own worktree:
* implement (Sonnet, no browse / no docs) →
* if implementer asked for docs: researcher (Haiku) writes them to wiki/ →
* review (gpt-5.5 xhigh)
* The loop exits only when the reviewer approves.
* 3. After a wave finishes, branches are merged back into main one-by-one
* (MergeQueue, maxConcurrency=1) before the next wave starts.
*
* Subscriptions: Claude roles run via the Claude Code CLI; the reviewer runs
* via the Pi/Codex CLI — both use the user's existing subscriptions.
*/
import { Sequence, Parallel, Loop, MergeQueue, Worktree } from "smithers-orchestrator";
import { ClaudeCodeAgent, PiAgent } from "smithers-orchestrator";
import { createExampleSmithers } from "./_example-kit.js";
import { z } from "zod";
import TriagePrompt from "./prompts/parallel-tickets/triage.mdx";
import ImplementPrompt from "./prompts/parallel-tickets/implement.mdx";
import ResearchPrompt from "./prompts/parallel-tickets/research.mdx";
import ReviewPrompt from "./prompts/parallel-tickets/review.mdx";
import MergePrompt from "./prompts/parallel-tickets/merge.mdx";
// ─── Schemas ───────────────────────────────────────────────────────────────
const ticketSchema = z.object({
id: z.string(),
title: z.string(),
description: z.string(),
files: z.array(z.string()).default([]),
dependsOn: z.array(z.string()).default([]),
acceptanceCriteria: z.string().optional(),
});
const triageSchema = z.object({
tickets: z.array(ticketSchema),
waves: z.array(z.object({
index: z.number(),
ticketIds: z.array(z.string()),
rationale: z.string(),
})),
summary: z.string(),
});
const implementSchema = z.object({
ticketId: z.string(),
status: z.enum(["complete", "needs_docs"]),
branch: z.string(),
summary: z.string(),
filesChanged: z.array(z.string()).default([]),
commitCount: z.number().default(0),
docRequests: z.array(z.object({
topic: z.string(),
questions: z.array(z.string()),
})).default([]),
});
const researchSchema = z.object({
ticketId: z.string(),
requestsAddressed: z.array(z.string()),
docsWritten: z.array(z.string()), // paths inside wiki/
summary: z.string(),
});
const reviewSchema = z.object({
ticketId: z.string(),
approved: z.boolean(),
feedback: z.string(),
issues: z.array(z.object({
severity: z.enum(["blocker", "major", "minor", "nit"]),
file: z.string(),
description: z.string(),
suggestion: z.string(),
})).default([]),
});
const mergeSchema = z.object({
ticketId: z.string(),
branch: z.string(),
status: z.enum(["merged", "conflict", "failed"]),
note: z.string(),
});
const reportSchema = z.object({
totalTickets: z.number(),
waves: z.number(),
merged: z.number(),
failed: z.number(),
summary: z.string(),
});
const { Workflow, Task, smithers, outputs } = createExampleSmithers({
triage: triageSchema,
implement: implementSchema,
research: researchSchema,
review: reviewSchema,
merge: mergeSchema,
report: reportSchema,
});
// ─── Agents ────────────────────────────────────────────────────────────────
const TRIAGE_MODEL = "claude-opus-4-5";
const IMPLEMENTER_MODEL = "claude-sonnet-4-6";
const RESEARCHER_MODEL = "claude-haiku-4-5";
const REVIEWER_MODEL = "gpt-5.5";
/** Triage: reads every ticket, builds the dep graph, groups into parallel waves. */
const triageAgent = new ClaudeCodeAgent({
model: TRIAGE_MODEL,
instructions: `You are the Triage agent. You read all tickets and the
codebase, infer dependencies between tickets, and partition them into "waves":
sets of tickets that can run in parallel because no ticket in the wave depends
on another ticket in the same wave.
Rules:
- A ticket may only land in wave N if all of its dependsOn tickets are in waves 0..N-1.
- Prefer wider waves over narrower ones — maximize parallelism.
- If two tickets touch the same files in conflicting ways, treat that as a
hidden dependency and put the lower-priority one in a later wave.
- Keep ticket IDs stable across the run.`,
});
/** Implementer: works inside its worktree. Cannot read external docs or browse.
* If it needs information, it emits docRequests to be fulfilled by the researcher. */
const implementerAgent = new ClaudeCodeAgent({
model: IMPLEMENTER_MODEL,
permissionMode: "acceptEdits",
// Hard ban on browsing and external docs. The implementer may only consult
// code in the worktree and files written into wiki/ by the researcher.
disallowedTools: ["WebFetch", "WebSearch"],
instructions: `You are the Implementer. You work inside an isolated git
worktree on a single ticket. Make focused, atomic commits with conventional
prefixes. You MUST NOT browse the web or attempt to read external docs.
If — and only if — you cannot proceed without external information, emit a
status of "needs_docs" with a list of docRequests describing what you need.
Then stop. Do not invent answers. Do not commit a half-baked guess.
When you resume, the researcher will have written authoritative notes into
the worktree's wiki/ folder. Read those before continuing.
Otherwise, complete the ticket: implement the change, commit it on the
ticket's branch, and report status "complete" with the branch name and a
summary of what you did.`,
});
/** Researcher: fulfills implementer doc requests by writing notes into wiki/. */
const researcherAgent = new ClaudeCodeAgent({
model: RESEARCHER_MODEL,
permissionMode: "acceptEdits",
instructions: `You are the Researcher. The implementer is blocked and has
asked specific questions. You may use any tools — reading code, searching
the web, consulting external docs — to answer them.
Write your findings as concise, authoritative markdown into the worktree's
wiki/ folder, one file per topic. Reference exact APIs, version numbers,
and links. Then report which requests you addressed and the paths you wrote.`,
});
/** Reviewer: GPT-5.5 thinking="xhigh" via Codex subscription.
* May research freely. Loop only exits when this agent approves. */
const reviewerAgent = new PiAgent({
provider: "openai-codex",
model: REVIEWER_MODEL,
mode: "rpc",
thinking: "xhigh",
tools: ["read", "grep", "bash"],
});
/** Merge: serialised one-at-a-time merge of approved branches into main. */
const mergeAgent = new ClaudeCodeAgent({
model: IMPLEMENTER_MODEL,
permissionMode: "acceptEdits",
allowedTools: ["Bash", "Read", "Grep"],
instructions: `You are the Merge agent. Fast-forward (or rebase if needed)
the given ticket branch onto main, run the project's build/test gates, and
push. If the merge would conflict, mark status "conflict" and stop — do not
attempt to resolve conflicts in this step. Report exactly one merge result.`,
});
// ─── Workflow ──────────────────────────────────────────────────────────────
export default smithers((ctx) => {
const triage = ctx.outputMaybe("triage", { nodeId: "triage" });
const allImpls = ctx.outputs.implement ?? [];
const allReviews = ctx.outputs.review ?? [];
const allMerges = ctx.outputs.merge ?? [];
const latestImplFor = (ticketId) => {
const rows = allImpls.filter((r) => r.ticketId === ticketId);
return rows[rows.length - 1];
};
const latestReviewFor = (ticketId) => {
const rows = allReviews.filter((r) => r.ticketId === ticketId);
return rows[rows.length - 1];
};
const maxParallel = ctx.input.maxParallel ?? 6;
const maxIterations = ctx.input.maxIterations ?? 6;
const baseBranch = ctx.input.baseBranch ?? "main";
return (
<Workflow name="parallel-tickets">
<Sequence>
{/* ═══ TRIAGE ═══ */}
<Task id="triage" output={outputs.triage} agent={triageAgent}>
<TriagePrompt
directory={ctx.input.directory}
tickets={ctx.input.tickets}
baseBranch={baseBranch}
/>
</Task>
{/* ═══ WAVES ═══
Static unroll over triage.waves. Each wave is a Sequence of
{parallel review-loops, then serial merges}. Waves execute
top-to-bottom because they live inside the outer Sequence. */}
{triage?.waves.map((wave) => {
const waveTickets = wave.ticketIds
.map((tid) => triage.tickets.find((t) => t.id === tid))
.filter((t) => t != null);
return (
<Sequence key={`wave-${wave.index}`}>
{/* Per-ticket review loops, run in parallel worktrees */}
<Parallel maxConcurrency={maxParallel}>
{waveTickets.map((ticket) => {
const latestImpl = latestImplFor(ticket.id);
const latestReview = latestReviewFor(ticket.id);
const isApproved = latestReview?.approved === true;
const needsDocs = latestImpl?.status === "needs_docs";
return (
<Worktree
key={ticket.id}
path={`.worktrees/${ticket.id}`}
branch={`ticket/${ticket.id}`}
baseBranch={baseBranch}
>
<Loop
until={isApproved}
maxIterations={maxIterations}
onMaxReached="return-last"
>
<Sequence>
{/* Implement (or fix from review feedback) */}
<Task
id={`implement-${ticket.id}`}
output={outputs.implement}
agent={implementerAgent}
retries={1}
timeoutMs={20 * 60_000}
>
<ImplementPrompt
ticket={ticket}
previousImpl={latestImpl}
review={latestReview}
/>
</Task>
{/* Researcher only runs when implementer asked for docs */}
<Task
id={`research-${ticket.id}`}
output={outputs.research}
agent={researcherAgent}
skipIf={!needsDocs}
timeoutMs={10 * 60_000}
>
<ResearchPrompt
ticket={ticket}
docRequests={latestImpl?.docRequests ?? []}
/>
</Task>
{/* Reviewer is skipped while waiting on docs */}
<Task
id={`review-${ticket.id}`}
output={outputs.review}
agent={reviewerAgent}
skipIf={needsDocs}
timeoutMs={15 * 60_000}
>
<ReviewPrompt
ticket={ticket}
impl={latestImpl}
/>
</Task>
</Sequence>
</Loop>
</Worktree>
);
})}
</Parallel>
{/* Serial merge of every branch in this wave before
we start the next wave. */}
<MergeQueue id={`merge-wave-${wave.index}`} maxConcurrency={1}>
{waveTickets.map((ticket) => {
const review = latestReviewFor(ticket.id);
return (
<Task
key={ticket.id}
id={`merge-${ticket.id}`}
output={outputs.merge}
agent={mergeAgent}
skipIf={review?.approved !== true}
retries={1}
timeoutMs={10 * 60_000}
>
<MergePrompt
ticketId={ticket.id}
branch={`ticket/${ticket.id}`}
baseBranch={baseBranch}
/>
</Task>
);
})}
</MergeQueue>
</Sequence>
);
})}
{/* ═══ FINAL REPORT ═══ */}
<Task id="report" output={outputs.report}>
{{
totalTickets: triage?.tickets.length ?? 0,
waves: triage?.waves.length ?? 0,
merged: allMerges.filter((m) => m.status === "merged").length,
failed: allMerges.filter((m) => m.status !== "merged").length,
summary: triage
? `Processed ${triage.tickets.length} tickets across ${triage.waves.length} wave(s); ${allMerges.filter((m) => m.status === "merged").length} merged.`
: "Triage did not produce a plan.",
}}
</Task>
</Sequence>
</Workflow>
);
});