@10xagents/connector-multica 0.0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs ADDED
@@ -0,0 +1,2272 @@
1
+ import { accessSync, constants, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
2
+ import { homedir, tmpdir } from "node:os";
3
+ import { delimiter, join, relative, sep } from "node:path";
4
+ import { execFileSync, spawnSync } from "node:child_process";
5
+ import { crc32 } from "node:zlib";
6
+ import { structuredPatch } from "diff";
7
+ // src/multica.ts
8
+ /** Where the macOS Desktop app keeps its copy of the CLI. */
9
+ const DESKTOP_CLI = "/Applications/Multica.app/Contents/Resources/app.asar.unpacked/resources/bin/multica";
10
+ const executable = (path) => {
11
+ try {
12
+ accessSync(path, constants.X_OK);
13
+ return true;
14
+ } catch {
15
+ return false;
16
+ }
17
+ };
18
+ /** `multica` on `PATH`, as `which` finds it. */
19
+ const onPath = (env) => (env.PATH ?? "").split(delimiter).filter(Boolean).map((dir) => join(dir, "multica")).find(executable);
20
+ /**
21
+ * The CLI to run: `$MULTICA`, then `$MULTICA_CLI_PATH`, then `multica` on `PATH`, then the macOS
22
+ * Desktop app's. Throws when there's none.
23
+ */
24
+ const findMultica = (env, desktop = DESKTOP_CLI) => {
25
+ const found = env.MULTICA || env.MULTICA_CLI_PATH || onPath(env);
26
+ if (found) return found;
27
+ if (existsSync(desktop)) return desktop;
28
+ throw new Error("multica CLI not found: install it, or set MULTICA to its path");
29
+ };
30
+ /**
31
+ * The environment `multica` runs in. Every inherited `MULTICA_*` variable goes, since the CLI
32
+ * prefers them to the profile's own token, server and workspace: an agent run's would otherwise
33
+ * sync another account. Only the workspace this call is for comes back.
34
+ */
35
+ const childEnv = (env, workspace) => {
36
+ const clean = {};
37
+ for (const [key, value] of Object.entries(env)) if (value !== void 0 && !key.startsWith("MULTICA_")) clean[key] = value;
38
+ if (workspace) clean.MULTICA_WORKSPACE_ID = workspace;
39
+ return clean;
40
+ };
41
+ /** The home directory, which is also where `multica` runs, away from any project's config. */
42
+ const homeOf = (env) => env.HOME || homedir();
43
+ /** The text `multica <args>` prints. Throws with its error output when it fails. */
44
+ const run = (cli, args, workspace, input, label = args) => {
45
+ try {
46
+ return execFileSync(cli.bin, [...cli.profileArgs, ...args], {
47
+ encoding: "utf8",
48
+ maxBuffer: 268435456,
49
+ cwd: homeOf(cli.env),
50
+ env: childEnv(cli.env, workspace),
51
+ stdio: [
52
+ input === void 0 ? "ignore" : "pipe",
53
+ "pipe",
54
+ "pipe"
55
+ ],
56
+ ...input === void 0 ? {} : { input }
57
+ });
58
+ } catch (error) {
59
+ const output = error && typeof error === "object" && "stderr" in error ? error.stderr : "";
60
+ const message = (typeof output === "string" ? output : Buffer.isBuffer(output) ? output.toString("utf8") : "").trim() || (error instanceof Error ? error.message : String(error));
61
+ throw new Error(`multica ${label.slice(0, 3).join(" ")}: ${message}`, { cause: error });
62
+ }
63
+ };
64
+ /** `multica <args> --output json`, parsed. */
65
+ const runJson = (cli, args, workspace) => {
66
+ const out = run(cli, [
67
+ ...args,
68
+ "--output",
69
+ "json"
70
+ ], workspace, void 0, args);
71
+ try {
72
+ return JSON.parse(out);
73
+ } catch (error) {
74
+ throw new Error(`multica ${args.slice(0, 3).join(" ")}: printed no JSON`, { cause: error });
75
+ }
76
+ };
77
+ // src/profiles.ts
78
+ /**
79
+ * The profiles that hold a token, by name: `""` is the default profile. `multica auth logout`
80
+ * leaves a config without a token, and an unreadable config logs nobody in.
81
+ */
82
+ const listProfiles = (home) => {
83
+ const root = join(home, ".multica");
84
+ const dir = join(root, "profiles");
85
+ const configs = [["", join(root, "config.json")]];
86
+ if (existsSync(dir)) for (const name of readdirSync(dir).toSorted()) configs.push([name, join(dir, name, "config.json")]);
87
+ return configs.filter(([, config]) => {
88
+ try {
89
+ const parsed = JSON.parse(readFileSync(config, "utf8"));
90
+ return !!parsed && typeof parsed === "object" && "token" in parsed && !!parsed.token;
91
+ } catch {
92
+ return false;
93
+ }
94
+ }).map(([name]) => name);
95
+ };
96
+ /**
97
+ * The profile to use: the one given, the only one there is, or the one the person picks. Throws
98
+ * when there are several and no terminal to ask in.
99
+ */
100
+ const pickProfile = async (home, given, ask, log) => {
101
+ if (given !== void 0) return given;
102
+ const found = listProfiles(home);
103
+ if (found.length === 0) throw new Error("no logged-in Multica CLI profile under ~/.multica: log in with the Desktop app or `multica login`");
104
+ if (found.length === 1) return found[0] ?? "";
105
+ if (!ask) {
106
+ const names = found.map((name) => name || "\"\" for the default").join(", ");
107
+ throw new Error(`several Multica profiles: pass --profile <name> (${names})`);
108
+ }
109
+ found.forEach((name, index) => log(` ${index + 1}. ${name || "(default)"}`));
110
+ const answer = await ask("Profile (number): ");
111
+ const picked = found[Number(answer) - 1];
112
+ if (picked === void 0) throw new Error(`no profile ${answer}`);
113
+ return picked;
114
+ };
115
+ // src/pull.ts
116
+ const isRow = (value) => !!value && typeof value === "object" && !Array.isArray(value);
117
+ const rows = (value, what) => {
118
+ if (!Array.isArray(value)) throw new Error(`multica ${what}: expected a list`);
119
+ return value.filter(isRow);
120
+ };
121
+ const row = (value, what) => {
122
+ if (!isRow(value)) throw new Error(`multica ${what}: expected an object`);
123
+ return value;
124
+ };
125
+ /** A string or number field as text. Anything else is empty. */
126
+ const text = (value) => typeof value === "string" ? value : typeof value === "number" || typeof value === "boolean" ? String(value) : "";
127
+ /** Reads the workspace `target` syncs to, with the profile and workspace `options` choose. */
128
+ const pull = async (target, options, context) => {
129
+ const log = options.log ?? (() => {});
130
+ const bin = findMultica(context.env, context.desktop);
131
+ const profile = await pickProfile(homeOf(context.env), options.profile, options.ask, log);
132
+ const cli = {
133
+ bin,
134
+ profileArgs: profile ? ["--profile", profile] : [],
135
+ env: context.env
136
+ };
137
+ log(`Profile: ${profile || "(default)"}`);
138
+ const want = options.workspace ?? target.workspace;
139
+ const found = rows(runJson(cli, ["workspace", "list"]), "workspace list").find((ws) => [
140
+ ws.id,
141
+ ws.slug,
142
+ ws.name
143
+ ].includes(want));
144
+ if (!found) throw new Error(`no workspace ${want} for this profile`);
145
+ const workspace = {
146
+ id: text(found.id),
147
+ name: text(found.name),
148
+ slug: text(found.slug)
149
+ };
150
+ log(`Workspace: ${workspace.name}`);
151
+ const id = workspace.id;
152
+ const json = (args, scoped = true) => runJson(cli, args, scoped ? id : void 0);
153
+ const agentContext = text(row(json([
154
+ "workspace",
155
+ "get",
156
+ id
157
+ ], false), "workspace get").context);
158
+ const members = rows(json([
159
+ "workspace",
160
+ "member",
161
+ "list",
162
+ id
163
+ ], false), "workspace member list").map((member) => ({
164
+ user_id: text(member.user_id),
165
+ name: text(member.name),
166
+ ...member.email === void 0 ? {} : { email: text(member.email) }
167
+ }));
168
+ const skills = rows(json(["skill", "list"]), "skill list").map((listed) => {
169
+ const full = row(json([
170
+ "skill",
171
+ "get",
172
+ text(listed.id),
173
+ "--with-content"
174
+ ]), "skill get");
175
+ const files = { "SKILL.md": text(full.content) };
176
+ for (const file of rows(full.files ?? [], "skill get")) files[text(file.path)] = text(file.content);
177
+ return {
178
+ id: text(listed.id),
179
+ name: text(listed.name),
180
+ files
181
+ };
182
+ });
183
+ const agents = rows(json(["agent", "list"]), "agent list").map((listed) => {
184
+ const full = row(json([
185
+ "agent",
186
+ "get",
187
+ text(listed.id)
188
+ ]), "agent get");
189
+ return {
190
+ id: text(listed.id),
191
+ name: text(listed.name),
192
+ description: text(full.description),
193
+ model: text(full.model),
194
+ thinking_level: text(full.thinking_level),
195
+ max_concurrent_tasks: text(full.max_concurrent_tasks),
196
+ instructions: text(full.instructions),
197
+ skills: rows(full.skills ?? [], "agent get").map((skill) => ({
198
+ name: text(skill.name),
199
+ enabled: skill.enabled !== false
200
+ }))
201
+ };
202
+ });
203
+ let squad = null;
204
+ const squadName = target.squad?.name;
205
+ if (squadName !== void 0) {
206
+ const listed = rows(json(["squad", "list"]), "squad list").find((candidate) => candidate.name === squadName);
207
+ if (listed) {
208
+ const full = row(json([
209
+ "squad",
210
+ "get",
211
+ text(listed.id)
212
+ ]), "squad get");
213
+ squad = {
214
+ id: text(listed.id),
215
+ name: squadName,
216
+ instructions: text(full.instructions),
217
+ leader_id: text(full.leader_id),
218
+ members: rows(json([
219
+ "squad",
220
+ "member",
221
+ "list",
222
+ text(listed.id)
223
+ ]), "squad member list").map((member) => ({
224
+ member_id: text(member.member_id),
225
+ member_type: text(member.member_type),
226
+ role: text(member.role)
227
+ }))
228
+ };
229
+ }
230
+ }
231
+ const workflow = pullWorkflow(json);
232
+ const projects = rows(json(["project", "list"]), "project list").map((project) => ({
233
+ id: text(project.id),
234
+ name: text(project.title ?? project.name)
235
+ }));
236
+ return {
237
+ connector: "multica",
238
+ cli,
239
+ profile,
240
+ workspace,
241
+ context: agentContext,
242
+ members,
243
+ skills,
244
+ agents,
245
+ squad,
246
+ workflow,
247
+ projects
248
+ };
249
+ };
250
+ const pullWorkflow = (json) => {
251
+ let listed;
252
+ try {
253
+ listed = rows(json(["workflow", "list"]), "workflow list");
254
+ } catch (error) {
255
+ const message = error instanceof Error ? error.message : String(error);
256
+ if (/unknown command|unknown flag/i.test(message)) return { problem: "this multica CLI has no `workflow` command: update it, or build the workflow by hand" };
257
+ if (/returned 404/.test(message)) return { problem: "this Multica server has no workflows: update it, or build the workflow by hand" };
258
+ throw error;
259
+ }
260
+ const found = listed.find((workflow) => workflow.is_default === true);
261
+ if (!found) return { problem: "Multica listed no Default workflow" };
262
+ const full = row(json([
263
+ "workflow",
264
+ "get",
265
+ text(found.id)
266
+ ]), "workflow get");
267
+ return {
268
+ id: text(found.id),
269
+ steps: rows(full.steps ?? [], "workflow get").map(toStep),
270
+ project_ids: Array.isArray(full.project_ids) ? full.project_ids.map(text) : []
271
+ };
272
+ };
273
+ /** A step as Multica returns it, with only the fields the sync compares. */
274
+ const toStep = (step) => {
275
+ const handler = isRow(step.handler) ? step.handler : null;
276
+ return {
277
+ status_key: text(step.status_key),
278
+ ...handler ? { handler: {
279
+ type: text(handler.type),
280
+ ...handler.id === void 0 || handler.id === null ? {} : { id: text(handler.id) }
281
+ } } : {},
282
+ ...step.instructions === void 0 ? {} : { instructions: text(step.instructions) },
283
+ ...step.next_status_key ? { next_status_key: text(step.next_status_key) } : {},
284
+ ...step.back_status_key ? { back_status_key: text(step.back_status_key) } : {}
285
+ };
286
+ };
287
+ // src/text.ts
288
+ /** Text that ends in exactly one newline, so a stored copy and a file compare alike. */
289
+ const norm = (text) => `${(text ?? "").replace(/\n*$/, "")}\n`;
290
+ /** How `live` differs from `setup`, compared as they are. */
291
+ const textDiff = (live, setup) => {
292
+ if (live === setup) return {
293
+ lines: 0,
294
+ diff: ""
295
+ };
296
+ const patch = structuredPatch("live", "setup", live, setup, "", "", { context: 3 });
297
+ let added = 0;
298
+ let removed = 0;
299
+ const out = [];
300
+ for (const hunk of patch.hunks) {
301
+ out.push(`@@ -${hunk.oldStart},${hunk.oldLines} +${hunk.newStart},${hunk.newLines} @@`);
302
+ for (const line of hunk.lines) {
303
+ if (line.startsWith("+")) added++;
304
+ else if (line.startsWith("-")) removed++;
305
+ out.push(line);
306
+ }
307
+ }
308
+ return {
309
+ lines: Math.max(added, removed),
310
+ diff: out.join("\n")
311
+ };
312
+ };
313
+ /** "1 line differs", "3 lines differ". */
314
+ const differ = (lines) => `${lines} ${lines === 1 ? "line differs" : "lines differ"}`;
315
+ /** A value, cut to 70 characters for one line of output. */
316
+ const preview = (text) => text.length > 70 ? `${text.slice(0, 70)}…` : text;
317
+ /** Code points, as Multica counts them. */
318
+ const length = (text) => Array.from(text).length;
319
+ // src/skills.ts
320
+ /** Every file under `dir`, as absolute paths, sorted, without `.DS_Store`. */
321
+ const walk = (dir) => readdirSync(dir).toSorted().flatMap((entry) => {
322
+ const path = join(dir, entry);
323
+ if (entry === ".DS_Store") return [];
324
+ return statSync(path).isDirectory() ? walk(path) : [path];
325
+ });
326
+ const posix = (path) => path.split(sep).join("/");
327
+ /** A skill's files, by path inside its folder, each normalised as Multica stores it. */
328
+ const skillFiles = (skill) => new Map(walk(skill.dir).map((path) => [posix(relative(skill.dir, path)), norm(readFileSync(path, "utf8"))]));
329
+ /** The `name` in a SKILL.md's frontmatter, or `""`. */
330
+ const frontmatterName = (text) => /^---\r?\n([\s\S]*?\r?\n)---/.exec(text)?.[1]?.match(/^name:[ \t]*(.*)$/m)?.[1]?.trim().replace(/^(["'])(.*)\1$/, "$2") ?? "";
331
+ const BINARY = /\.(png|jpe?g|gif|webp|bmp|tiff|ico|heic|ttf|otf|woff2?|eot|zip|gz|tar|bz2|7z|rar|pdf|docx?|xlsx?|pptx?|mp3|mp4|wav|avi|mov|webm|m4a|flac|exe|dll|so|dylib|class|jar|wasm|db|sqlite3?|pyc)$/i;
332
+ /**
333
+ * A file Multica's import leaves out: dotfiles, a second `skill.md`, licences and binaries
334
+ * (`skill_import_archive.go`, `skill/binary.go`). The sync compares every file, so a file like
335
+ * that would show as a change on every run.
336
+ */
337
+ const leftOut = (path) => path.split("/").some((segment) => segment.startsWith(".")) || path !== "SKILL.md" && /(^|\/)skill\.md$/i.test(path) || /(^|\/)license(\.md|\.txt)?$/i.test(path) || BINARY.test(path);
338
+ /**
339
+ * What Multica's import would change about a skill: a SKILL.md that's missing or names another
340
+ * skill, and files it would leave out.
341
+ */
342
+ const checkSkill = (skill) => {
343
+ const issues = [];
344
+ const at = (path) => join(skill.dir, path);
345
+ let files;
346
+ try {
347
+ files = walk(skill.dir).map((path) => posix(relative(skill.dir, path)));
348
+ } catch (error) {
349
+ return [{
350
+ file: skill.dir,
351
+ field: "(dir)",
352
+ message: `can't be read: ${error instanceof Error ? error.message : String(error)}`
353
+ }];
354
+ }
355
+ if (!files.includes("SKILL.md")) issues.push({
356
+ file: at("SKILL.md"),
357
+ field: "(file)",
358
+ message: "is missing: every skill needs one"
359
+ });
360
+ else {
361
+ const named = frontmatterName(readFileSync(at("SKILL.md"), "utf8"));
362
+ if (named !== skill.name) issues.push({
363
+ file: at("SKILL.md"),
364
+ field: "name",
365
+ message: named ? `must be ${skill.name}, like its folder (it says ${named}): Multica names a skill after it` : `is missing: write name: ${skill.name}, like its folder`
366
+ });
367
+ }
368
+ for (const path of files) if (leftOut(path)) issues.push({
369
+ file: at(path),
370
+ field: "(file)",
371
+ message: "Multica leaves this file out of an import: rename or remove it"
372
+ });
373
+ return issues;
374
+ };
375
+ /** A zip of the skill's folder, stored without compression, with the folder at its root. */
376
+ const zipSkill = (skill) => {
377
+ const locals = [];
378
+ const centrals = [];
379
+ let offset = 0;
380
+ let count = 0;
381
+ for (const path of walk(skill.dir)) {
382
+ const name = Buffer.from(`${skill.name}/${posix(relative(skill.dir, path))}`);
383
+ const data = readFileSync(path);
384
+ const sum = crc32(data);
385
+ const local = Buffer.alloc(30);
386
+ local.writeUInt32LE(67324752, 0);
387
+ local.writeUInt16LE(20, 4);
388
+ local.writeUInt16LE(2048, 6);
389
+ local.writeUInt16LE(33, 12);
390
+ local.writeUInt32LE(sum, 14);
391
+ local.writeUInt32LE(data.length, 18);
392
+ local.writeUInt32LE(data.length, 22);
393
+ local.writeUInt16LE(name.length, 26);
394
+ const central = Buffer.alloc(46);
395
+ central.writeUInt32LE(33639248, 0);
396
+ central.writeUInt16LE(20, 4);
397
+ central.writeUInt16LE(20, 6);
398
+ central.writeUInt16LE(2048, 8);
399
+ central.writeUInt16LE(33, 14);
400
+ central.writeUInt32LE(sum, 16);
401
+ central.writeUInt32LE(data.length, 20);
402
+ central.writeUInt32LE(data.length, 24);
403
+ central.writeUInt16LE(name.length, 28);
404
+ central.writeUInt32LE(offset, 42);
405
+ locals.push(local, name, data);
406
+ centrals.push(central, name);
407
+ offset += local.length + name.length + data.length;
408
+ count++;
409
+ }
410
+ const directory = Buffer.concat(centrals);
411
+ const end = Buffer.alloc(22);
412
+ end.writeUInt32LE(101010256, 0);
413
+ end.writeUInt16LE(count, 8);
414
+ end.writeUInt16LE(count, 10);
415
+ end.writeUInt32LE(directory.length, 12);
416
+ end.writeUInt32LE(offset, 16);
417
+ return Buffer.concat([
418
+ ...locals,
419
+ directory,
420
+ end
421
+ ]);
422
+ };
423
+ // src/workflow.ts
424
+ /** Multica's built-in statuses, by key, with their names. */
425
+ const BUILT_IN = {
426
+ backlog: "Backlog",
427
+ todo: "Todo",
428
+ in_progress: "In Progress",
429
+ in_review: "In Review",
430
+ blocked: "Blocked",
431
+ done: "Done",
432
+ cancelled: "Cancelled"
433
+ };
434
+ const isBuiltIn = (key) => Object.hasOwn(BUILT_IN, key);
435
+ /** Multica keeps these first, in this order. */
436
+ const HEAD = ["backlog", "todo"];
437
+ /** Multica keeps these last, in this order. */
438
+ const TAIL = [
439
+ "blocked",
440
+ "done",
441
+ "cancelled"
442
+ ];
443
+ const FIXED = [...HEAD, ...TAIL];
444
+ /** Every workflow has these. */
445
+ const REQUIRED = [
446
+ "todo",
447
+ "done",
448
+ "cancelled"
449
+ ];
450
+ /** These take no handoff. */
451
+ const NO_HANDOFF = /* @__PURE__ */ new Set([
452
+ "backlog",
453
+ "done",
454
+ "cancelled"
455
+ ]);
456
+ /** The most characters Multica takes in a step's instructions. */
457
+ const MAX_STEP = 8e3;
458
+ /** The most characters Multica takes in an agent's description. */
459
+ const MAX_DESCRIPTION = 255;
460
+ /** `plan_review` → `Plan Review`. */
461
+ const titleCase = (key) => key.split(/[_-]+/).filter(Boolean).map((word) => `${word.charAt(0).toUpperCase()}${word.slice(1)}`).join(" ");
462
+ /**
463
+ * The Multica name of each column: a built-in column's own, the first workflow's custom columns
464
+ * by their `name` (or their key, in title case), and every later workflow's prefixed with the
465
+ * workflow's id, in title case.
466
+ */
467
+ const columnNames = (workflows) => Object.fromEntries(workflows.map((workflow, index) => [workflow.id, Object.fromEntries(workflow.columns.map((column) => {
468
+ const builtIn = BUILT_IN[column.key];
469
+ if (builtIn !== void 0) return [column.key, builtIn];
470
+ const own = column.name ?? titleCase(column.key);
471
+ return [column.key, index === 0 ? own : `${titleCase(workflow.id)} ${own}`];
472
+ }))]));
473
+ const ref = (workflow, key) => `${workflow}\0${key}`;
474
+ const toColumn = (workflow, column) => ({
475
+ name: column.name,
476
+ ...isBuiltIn(column.key) ? { key: column.key } : { category: column.category ?? "started" },
477
+ handoff: column.handoff,
478
+ ...column.instructions === void 0 ? {} : { instructions: column.instructions },
479
+ from: [{
480
+ workflow,
481
+ key: column.key
482
+ }]
483
+ });
484
+ /** Merges `rendered`'s workflows, in Multica's order, and reports what can't merge. */
485
+ const mergeWorkflows = (rendered) => {
486
+ const issues = [];
487
+ const workflows = rendered.workflows;
488
+ const place = /* @__PURE__ */ new Map();
489
+ const columns = [];
490
+ const shared = /* @__PURE__ */ new Map();
491
+ const add = (workflow, key, column) => {
492
+ const builtIn = isBuiltIn(key);
493
+ const existing = builtIn ? shared.get(key) : void 0;
494
+ if (existing !== void 0) {
495
+ place.set(ref(workflow, key), existing);
496
+ columns[existing]?.from.push({
497
+ workflow,
498
+ key
499
+ });
500
+ return;
501
+ }
502
+ place.set(ref(workflow, key), columns.length);
503
+ if (builtIn) shared.set(key, columns.length);
504
+ columns.push(column);
505
+ };
506
+ const all = workflows.flatMap((workflow) => workflow.columns.map((column) => ({
507
+ workflow: workflow.id,
508
+ column
509
+ })));
510
+ for (const key of HEAD) for (const { workflow, column } of all.filter((item) => item.column.key === key)) add(workflow, key, toColumn(workflow, column));
511
+ for (const { workflow, column } of all.filter((item) => !FIXED.includes(item.column.key))) add(workflow, column.key, toColumn(workflow, column));
512
+ for (const key of TAIL) for (const { workflow, column } of all.filter((item) => item.column.key === key)) add(workflow, key, toColumn(workflow, column));
513
+ const seen = /* @__PURE__ */ new Set();
514
+ for (const { workflow, column } of all) {
515
+ const at = place.get(ref(workflow, column.key));
516
+ const merged = at === void 0 ? void 0 : columns[at];
517
+ if (at === void 0 || !merged) continue;
518
+ const next = column.next === void 0 ? void 0 : place.get(ref(workflow, column.next));
519
+ const back = column.back === void 0 ? void 0 : place.get(ref(workflow, column.back));
520
+ if (!seen.has(at)) {
521
+ seen.add(at);
522
+ if (next !== void 0) merged.next = next;
523
+ if (back !== void 0) merged.back = back;
524
+ continue;
525
+ }
526
+ const first = merged.from[0];
527
+ const conflicts = [
528
+ column.handoff !== merged.handoff ? "handoff" : "",
529
+ (column.instructions ?? "") !== (merged.instructions ?? "") ? "step text" : "",
530
+ next !== merged.next ? "next" : "",
531
+ back !== merged.back ? "back" : ""
532
+ ].filter(Boolean);
533
+ if (conflicts.length > 0 && first) issues.push({
534
+ field: `workflows.${workflow}.columns.${column.key}`,
535
+ message: `${merged.name} is shared with workflow ${first.workflow}, which gives it another ${conflicts.join(", ")}: Multica has one ${merged.name}, so give both the same`
536
+ });
537
+ }
538
+ return {
539
+ columns,
540
+ issues
541
+ };
542
+ };
543
+ /** What Multica would refuse in a target's workflows, or would put back another way. */
544
+ const checkWorkflows = (rendered, merged) => {
545
+ const issues = [...merged.issues];
546
+ for (const workflow of rendered.workflows) {
547
+ const field = (key) => `workflows.${workflow.id}.columns.${key}`;
548
+ const keys = workflow.columns.map((column) => column.key);
549
+ const fixed = keys.filter((key) => FIXED.includes(key));
550
+ const expected = FIXED.filter((key) => keys.includes(key));
551
+ const firstMiddle = keys.findIndex((key) => !FIXED.includes(key));
552
+ const lastMiddle = keys.findLastIndex((key) => !FIXED.includes(key));
553
+ const lastHead = Math.max(-1, ...HEAD.map((key) => keys.indexOf(key)));
554
+ const tailPlaces = TAIL.map((key) => keys.indexOf(key)).filter((index) => index >= 0);
555
+ const firstTail = Math.min(keys.length, ...tailPlaces);
556
+ if (fixed.join() !== expected.join() || firstMiddle >= 0 && (lastHead > firstMiddle || firstTail < lastMiddle)) issues.push({
557
+ field: `workflows.${workflow.id}.columns`,
558
+ message: "list backlog and todo first, and blocked, done and cancelled last, as Multica does"
559
+ });
560
+ for (const column of workflow.columns) {
561
+ if (NO_HANDOFF.has(column.key) && column.handoff !== "none") issues.push({
562
+ field: `${field(column.key)}.handoff`,
563
+ message: `Multica hands nothing off in ${BUILT_IN[column.key]}: make it none`
564
+ });
565
+ const size = length(column.instructions ?? "");
566
+ if (size > 8e3) issues.push({
567
+ field: `workflows.${workflow.id}.steps.${column.key}`,
568
+ message: `is ${size} characters, over Multica's ${MAX_STEP}`
569
+ });
570
+ }
571
+ }
572
+ const keys = new Set(merged.columns.flatMap((column) => column.key ? [column.key] : []));
573
+ for (const key of REQUIRED) if (!keys.has(key)) issues.push({
574
+ field: "workflows",
575
+ message: `no workflow has a ${key} column, and Multica needs one`
576
+ });
577
+ const names = /* @__PURE__ */ new Map();
578
+ const builtInNames = new Set(Object.values(BUILT_IN).map((name) => name.toLowerCase()));
579
+ for (const column of merged.columns) {
580
+ if (column.key) continue;
581
+ const from = column.from[0];
582
+ const field = from ? `workflows.${from.workflow}.columns.${from.key}.name` : "workflows";
583
+ const lower = column.name.toLowerCase();
584
+ const earlier = names.get(lower);
585
+ if (builtInNames.has(lower)) issues.push({
586
+ field,
587
+ message: `${column.name} is the name of a built-in Multica status`
588
+ });
589
+ else if (earlier) {
590
+ const other = earlier.from[0];
591
+ issues.push({
592
+ field,
593
+ message: `${column.name} is also the name of ${other ? `workflow ${other.workflow}'s ${other.key}` : "another column"}: Multica names a status once`
594
+ });
595
+ } else names.set(lower, column);
596
+ }
597
+ return issues;
598
+ };
599
+ // src/steps.ts
600
+ const builtInKeys = new Set(Object.keys(BUILT_IN));
601
+ /**
602
+ * The custom columns' status keys, by place. Multica keeps a workflow's steps in the order
603
+ * written, and a key says nothing about its status's name, since a renamed status keeps its
604
+ * key. So the n-th custom key is the n-th custom column's, and when the counts differ, none is
605
+ * known.
606
+ */
607
+ const customKeys = (liveKeys, specs) => {
608
+ const custom = liveKeys.filter((key) => !builtInKeys.has(key));
609
+ const places = specs.flatMap((spec, index) => spec.key ? [] : [index]);
610
+ if (custom.length !== places.length) return /* @__PURE__ */ new Map();
611
+ return new Map(places.map((place, index) => [place, custom[index] ?? ""]));
612
+ };
613
+ /** The status key of the column at `place`, when it's known. */
614
+ const keyAt = (specs, keys, place) => specs[place]?.key ?? keys.get(place);
615
+ /**
616
+ * One step as Multica takes it: by key, or by name for a custom column whose key isn't known
617
+ * (Multica reuses the status with that name, or creates it), with its handoff and the moves
618
+ * whose target has a key.
619
+ */
620
+ const stepFor = (specs, keys, place) => {
621
+ const spec = specs[place];
622
+ if (!spec) throw new Error(`no column at ${place}`);
623
+ const key = keyAt(specs, keys, place);
624
+ const step = key ? { status_key: key } : { new_status: {
625
+ name: spec.name,
626
+ category: spec.category ?? "started"
627
+ } };
628
+ if (spec.handler) {
629
+ step.handler = spec.handler;
630
+ step.instructions = spec.instructions;
631
+ }
632
+ const next = spec.next === void 0 ? void 0 : keyAt(specs, keys, spec.next);
633
+ const back = spec.back === void 0 ? void 0 : keyAt(specs, keys, spec.back);
634
+ if (next) step.next_status_key = next;
635
+ if (back) step.back_status_key = back;
636
+ return step;
637
+ };
638
+ /** Every step, in order. */
639
+ const stepsFor = (specs, keys) => specs.map((_, place) => stepFor(specs, keys, place));
640
+ // src/apply.ts
641
+ const OPS = /* @__PURE__ */ new Set([
642
+ "skill-import",
643
+ "agent-update",
644
+ "agent-skills-add",
645
+ "squad-instructions",
646
+ "squad-member-add",
647
+ "squad-member-role",
648
+ "squad-leader",
649
+ "workflow",
650
+ "context"
651
+ ]);
652
+ const isData = (value) => !!value && typeof value === "object" && "op" in value && typeof value.op === "string" && OPS.has(value.op) && "cli" in value && "workspace" in value;
653
+ /** Makes `changes` in order. One that fails, like a skill another account owns, leaves the rest. */
654
+ const applyChanges = (changes) => {
655
+ const failed = [];
656
+ const dir = mkdtempSync(join(tmpdir(), "10xagents-multica-"));
657
+ let workflowFailed = false;
658
+ try {
659
+ let files = 0;
660
+ const file = (name, content) => {
661
+ const path = join(dir, `${++files}-${name}`);
662
+ writeFileSync(path, content);
663
+ return path;
664
+ };
665
+ for (const change of changes) {
666
+ if (change.manual || change.note) continue;
667
+ try {
668
+ if (!isData(change.data)) throw new Error("not a change the Multica connector planned");
669
+ const data = change.data;
670
+ if (data.op === "workflow") try {
671
+ writeWorkflow(data.cli, data.workspace, data, file);
672
+ } catch (error) {
673
+ workflowFailed = true;
674
+ throw error;
675
+ }
676
+ else if (data.op === "context") {
677
+ if (workflowFailed) throw new Error("held back, since the workflow's write failed");
678
+ run(data.cli, [
679
+ "workspace",
680
+ "update",
681
+ data.workspace,
682
+ "--context-stdin"
683
+ ], data.workspace, data.context);
684
+ } else applyOne(data, file);
685
+ } catch (error) {
686
+ failed.push({
687
+ change,
688
+ message: error instanceof Error ? error.message : String(error)
689
+ });
690
+ }
691
+ }
692
+ } finally {
693
+ rmSync(dir, {
694
+ recursive: true,
695
+ force: true
696
+ });
697
+ }
698
+ return { failed };
699
+ };
700
+ const applyOne = (data, file) => {
701
+ const { cli, workspace } = data;
702
+ const call = (args) => run(cli, args, workspace);
703
+ switch (data.op) {
704
+ case "skill-import":
705
+ call([
706
+ "skill",
707
+ "import",
708
+ "--file",
709
+ file(`${data.skill.name}.zip`, zipSkill(data.skill)),
710
+ "--on-conflict",
711
+ "overwrite"
712
+ ]);
713
+ return;
714
+ case "agent-update":
715
+ call([
716
+ "agent",
717
+ "update",
718
+ data.agent,
719
+ ...data.args
720
+ ]);
721
+ return;
722
+ case "agent-skills-add": {
723
+ const listed = runJson(cli, ["skill", "list"], workspace);
724
+ const matched = (Array.isArray(listed) ? listed : []).filter((skill) => !!skill && typeof skill === "object" && "name" in skill && "id" in skill).filter((skill) => data.skills.includes(String(skill.name)));
725
+ if (matched.length > 0) {
726
+ const ids = matched.map((skill) => String(skill.id));
727
+ call([
728
+ "agent",
729
+ "skills",
730
+ "add",
731
+ data.agent,
732
+ "--skill-ids",
733
+ ids.join(",")
734
+ ]);
735
+ }
736
+ const found = new Set(matched.map((skill) => String(skill.name)));
737
+ const missing = data.skills.filter((name) => !found.has(name));
738
+ if (missing.length > 0) throw new Error(`not in the workspace, so not attached: ${missing.join(", ")}`);
739
+ return;
740
+ }
741
+ case "squad-instructions":
742
+ call([
743
+ "squad",
744
+ "update",
745
+ data.squad,
746
+ "--instructions",
747
+ data.instructions
748
+ ]);
749
+ return;
750
+ case "squad-member-add":
751
+ call([
752
+ "squad",
753
+ "member",
754
+ "add",
755
+ data.squad,
756
+ "--member-id",
757
+ data.agent,
758
+ "--type",
759
+ "agent",
760
+ "--role",
761
+ data.role
762
+ ]);
763
+ return;
764
+ case "squad-member-role":
765
+ call([
766
+ "squad",
767
+ "member",
768
+ "set-role",
769
+ data.squad,
770
+ "--member-id",
771
+ data.agent,
772
+ "--member-type",
773
+ "agent",
774
+ "--role",
775
+ data.role
776
+ ]);
777
+ return;
778
+ case "squad-leader":
779
+ call([
780
+ "squad",
781
+ "update",
782
+ data.squad,
783
+ "--leader",
784
+ data.agent
785
+ ]);
786
+ return;
787
+ default: throw new Error(`can't apply ${data.op} here`);
788
+ }
789
+ };
790
+ /**
791
+ * Writes the workflow in two passes. The first names every custom column, so Multica creates or
792
+ * reuses its status, and sets only the moves whose target has a key. The second sets the moves
793
+ * to custom columns, with the keys the first one made.
794
+ */
795
+ const writeWorkflow = (cli, workspace, data, file) => {
796
+ const steps = (keys) => file("steps.json", JSON.stringify(stepsFor(data.specs, keys), null, 2));
797
+ const first = stepsFor(data.specs, /* @__PURE__ */ new Map());
798
+ const written = runJson(cli, [
799
+ "workflow",
800
+ "update",
801
+ data.workflow,
802
+ "--steps-file",
803
+ steps(/* @__PURE__ */ new Map())
804
+ ], workspace);
805
+ const writtenSteps = written && typeof written === "object" && "steps" in written && Array.isArray(written.steps) ? written.steps.filter((step) => !!step && typeof step === "object").map(toStep) : [];
806
+ const made = customKeys(writtenSteps.map((step) => step.status_key), data.specs);
807
+ const custom = data.specs.filter((spec) => !spec.key).length;
808
+ if (made.size !== custom) throw new Error("the written workflow's custom columns don't line up with the setup's");
809
+ const second = stepsFor(data.specs, made);
810
+ if (JSON.stringify(second) !== JSON.stringify(first)) run(cli, [
811
+ "workflow",
812
+ "update",
813
+ data.workflow,
814
+ "--steps-file",
815
+ steps(made)
816
+ ], workspace);
817
+ };
818
+ // src/bench/read.ts
819
+ const isObject = (value) => !!value && typeof value === "object" && !Array.isArray(value);
820
+ const isNamed = (value) => isObject(value) && typeof value.id === "string" && typeof value.name === "string";
821
+ const isIssue = (value) => isObject(value) && typeof value.id === "string" && typeof value.created_at === "string";
822
+ const isRun = (value) => isObject(value) && typeof value.id === "string" && typeof value.agent_id === "string" && typeof value.status === "string";
823
+ const isEntry = (value) => isObject(value);
824
+ const isComment = (value) => isObject(value) && typeof value.created_at === "string";
825
+ /** The records in a list that have the fields `guard` checks. */
826
+ const list = (value, guard) => Array.isArray(value) ? value.filter((item) => guard(item)) : [];
827
+ /** The list itself, or the list a field of an object holds. */
828
+ const listIn = (value, field, guard) => Array.isArray(value) ? list(value, guard) : isObject(value) ? list(value[field], guard) : [];
829
+ /** What `multica <args>` printed, and what it wrote to standard error, where it warns of a cut timeline. */
830
+ const spawn = (cli, args, workspace) => {
831
+ const result = spawnSync(cli.bin, [...cli.profileArgs, ...args], {
832
+ encoding: "utf8",
833
+ maxBuffer: 268435456,
834
+ cwd: homeOf(cli.env),
835
+ env: childEnv(cli.env, workspace),
836
+ stdio: [
837
+ "ignore",
838
+ "pipe",
839
+ "pipe"
840
+ ]
841
+ });
842
+ if (result.error || result.status !== 0) {
843
+ const reason = (result.stderr || result.error?.message || "").trim();
844
+ throw new Error(`multica ${args.slice(0, 3).join(" ")}: ${reason}`, { cause: result.error });
845
+ }
846
+ return {
847
+ stdout: result.stdout,
848
+ warnings: result.stderr ?? ""
849
+ };
850
+ };
851
+ const spawnJson = (cli, args, workspace) => {
852
+ const { stdout, warnings } = spawn(cli, [
853
+ ...args,
854
+ "--output",
855
+ "json"
856
+ ], workspace);
857
+ return {
858
+ data: JSON.parse(stdout),
859
+ warnings
860
+ };
861
+ };
862
+ /** Opens the target's workspace for reading, with the profile the query names or the only one. */
863
+ const openSource = async (query, context) => {
864
+ const log = query.log ?? (() => {});
865
+ const bin = findMultica(context.env, context.desktop);
866
+ const profile = await pickProfile(homeOf(context.env), query.profile, query.ask, log);
867
+ const cli = {
868
+ bin,
869
+ profileArgs: profile ? ["--profile", profile] : [],
870
+ env: context.env
871
+ };
872
+ const want = query.target.workspace;
873
+ const workspaces = list(runJson(cli, ["workspace", "list"]), isNamed);
874
+ if (workspaces.length === 0) throw new Error("this profile reaches no workspace: log in with another one");
875
+ const chosen = workspaces.find((ws) => [
876
+ ws.id,
877
+ ws.slug,
878
+ ws.name
879
+ ].includes(want));
880
+ if (!chosen) throw new Error(`no workspace ${want} for this profile: ${workspaces.map((ws) => ws.slug || ws.name).join(", ")}`);
881
+ const workspace = {
882
+ id: chosen.id,
883
+ name: chosen.name,
884
+ slug: chosen.slug ?? ""
885
+ };
886
+ const json = (args) => runJson(cli, args, workspace.id);
887
+ const notes = [];
888
+ const agentNames = new Map(list(json([
889
+ "agent",
890
+ "list",
891
+ "--include-archived"
892
+ ]), isNamed).map((a) => [a.id, a.name]));
893
+ const byId = /* @__PURE__ */ new Map();
894
+ const listArgs = [
895
+ "issue",
896
+ "list",
897
+ "--sort",
898
+ "created_at",
899
+ "--direction",
900
+ "asc",
901
+ "--limit",
902
+ "100"
903
+ ];
904
+ for (let offset = 0;; offset += 100) {
905
+ const page = json([
906
+ ...listArgs,
907
+ "--offset",
908
+ String(offset)
909
+ ]);
910
+ const issues = listIn(page, "issues", isIssue);
911
+ for (const issue of issues) byId.set(issue.id, issue);
912
+ if (!(isObject(page) && page.has_more === true) || issues.length === 0) break;
913
+ }
914
+ const runs = /* @__PURE__ */ new Map();
915
+ const timelines = /* @__PURE__ */ new Map();
916
+ const comments = /* @__PURE__ */ new Map();
917
+ let fromServer = false;
918
+ const serverTimeline = async (issueId, statusOnly) => {
919
+ const file = join(homeOf(context.env), ".multica", ...profile ? ["profiles", profile] : [], "config.json");
920
+ let config;
921
+ try {
922
+ config = JSON.parse(readFileSync(file, "utf8"));
923
+ } catch (error) {
924
+ throw new Error(`can't read the server's address and token from ${file}`, { cause: error });
925
+ }
926
+ const fields = isObject(config) ? config : {};
927
+ const token = typeof fields.token === "string" ? fields.token : "";
928
+ if (!token) throw new Error(`${file} holds no token: log in with this profile first`);
929
+ const url = ((typeof fields.server_url === "string" ? fields.server_url : "") || "https://api.multica.ai").replace(/^ws/, "http").replace(/\/+$/, "");
930
+ const response = await fetch(`${url}/api/issues/${encodeURIComponent(issueId)}/timeline`, { headers: {
931
+ authorization: `Bearer ${token}`,
932
+ "x-workspace-id": workspace.id
933
+ } });
934
+ if (!response.ok) throw new Error(`timeline ${issueId}: the server answered HTTP ${response.status}`);
935
+ const body = await response.json();
936
+ const entries = listIn(body, "entries", isEntry);
937
+ const cut = response.headers.get("x-timeline-truncated");
938
+ return {
939
+ data: statusOnly ? entries.filter((e) => e.type === "activity" && e.action === "status_changed") : entries,
940
+ warnings: cut ? `warning: timeline truncated by the server cap (${cut})` : ""
941
+ };
942
+ };
943
+ const readTimeline = async (issueId, statusOnly) => {
944
+ if (!fromServer) try {
945
+ const { data, warnings } = spawnJson(cli, [
946
+ "issue",
947
+ "timeline",
948
+ issueId,
949
+ ...statusOnly ? ["--action", "status_changed"] : []
950
+ ], workspace.id);
951
+ return {
952
+ data: list(data, isEntry),
953
+ warnings
954
+ };
955
+ } catch (error) {
956
+ if (!(error instanceof Error) || !/unknown (flag: --|command "timeline")/.test(error.message)) throw error;
957
+ fromServer = true;
958
+ notes.push("Your multica CLI has no `issue timeline`, so timelines came from the server, with your profile's token.");
959
+ }
960
+ return serverTimeline(issueId, statusOnly);
961
+ };
962
+ const scratch = mkdtempSync(join(tmpdir(), "10xagents-bench-"));
963
+ return {
964
+ workspace,
965
+ agentNames,
966
+ issues: [...byId.values()],
967
+ notes,
968
+ runs: (issueId) => {
969
+ let read = runs.get(issueId);
970
+ if (!read) {
971
+ read = list(json([
972
+ "issue",
973
+ "runs",
974
+ issueId
975
+ ]), isRun);
976
+ runs.set(issueId, read);
977
+ }
978
+ return read;
979
+ },
980
+ timeline: (issueId, statusOnly) => {
981
+ const key = `${issueId}:${statusOnly}`;
982
+ let read = timelines.get(key);
983
+ if (!read) {
984
+ read = readTimeline(issueId, statusOnly);
985
+ timelines.set(key, read);
986
+ }
987
+ return read;
988
+ },
989
+ comments: (issueId) => {
990
+ let read = comments.get(issueId);
991
+ if (!read) {
992
+ read = listIn(json([
993
+ "issue",
994
+ "comment",
995
+ "list",
996
+ issueId,
997
+ "--full"
998
+ ]), "comments", isComment);
999
+ comments.set(issueId, read);
1000
+ }
1001
+ return read;
1002
+ },
1003
+ attachment: (attachment) => {
1004
+ try {
1005
+ const dir = join(scratch, attachment.id);
1006
+ mkdirSync(dir, { recursive: true });
1007
+ let path = join(dir, attachment.filename ?? "");
1008
+ const printed = spawn(cli, [
1009
+ "attachment",
1010
+ "download",
1011
+ attachment.id,
1012
+ "-o",
1013
+ dir
1014
+ ], workspace.id).stdout;
1015
+ try {
1016
+ const parsed = JSON.parse(printed);
1017
+ if (isObject(parsed) && typeof parsed.path === "string") path = parsed.path;
1018
+ } catch {}
1019
+ return readFileSync(path, "utf8");
1020
+ } catch (error) {
1021
+ log(` couldn't read ${attachment.filename ?? attachment.id}: ${error instanceof Error ? error.message : String(error)}`);
1022
+ return null;
1023
+ }
1024
+ },
1025
+ close: () => rmSync(scratch, {
1026
+ recursive: true,
1027
+ force: true
1028
+ })
1029
+ };
1030
+ };
1031
+ // src/bench/shared.ts
1032
+ /** Milliseconds since the epoch, or `NaN` without a date. */
1033
+ const ms = (value) => value ? Date.parse(value) : NaN;
1034
+ /** The finite values at or above 0, sorted. */
1035
+ const finite = (xs) => xs.filter((x) => typeof x === "number" && Number.isFinite(x) && x >= 0).toSorted((a, b) => a - b);
1036
+ /** Nearest rank: the smallest value with at least `p` of the sorted `xs` at or below it. */
1037
+ const pct = (xs, p) => xs.length ? xs[Math.max(0, Math.ceil(p * xs.length) - 1)] ?? NaN : NaN;
1038
+ const sum = (xs) => xs.reduce((a, b) => a + b, 0);
1039
+ const mean = (xs) => xs.length ? sum(xs) / xs.length : NaN;
1040
+ /** Groups `xs` by `key`, in the order keys first appear. */
1041
+ const by = (xs, key) => {
1042
+ const map = /* @__PURE__ */ new Map();
1043
+ for (const x of xs) map.set(key(x), [...map.get(key(x)) ?? [], x]);
1044
+ return map;
1045
+ };
1046
+ /** Counts each value, most first. */
1047
+ const count = (xs) => {
1048
+ const map = /* @__PURE__ */ new Map();
1049
+ for (const x of xs) map.set(x, (map.get(x) ?? 0) + 1);
1050
+ return [...map].toSorted((a, b) => b[1] - a[1]);
1051
+ };
1052
+ /** Collects metric results for one segment. */
1053
+ const collector = (segment) => {
1054
+ const results = [];
1055
+ const add = (id, x, slice) => {
1056
+ results.push({
1057
+ id,
1058
+ segment,
1059
+ value: x !== null && Number.isFinite(x) ? x : null,
1060
+ ...slice ? { by: slice } : {}
1061
+ });
1062
+ };
1063
+ return {
1064
+ results,
1065
+ add
1066
+ };
1067
+ };
1068
+ /**
1069
+ * Dollars per million tokens at Anthropic's list prices, as Multica's own table has them
1070
+ * (`server/internal/metrics/pricing.go`), with five-minute cache writes. A model it doesn't know is
1071
+ * priced as Opus 5.5.
1072
+ */
1073
+ const PRICES = {
1074
+ "claude-opus-5-5": {
1075
+ input: 4,
1076
+ output: 20,
1077
+ cacheRead: .2,
1078
+ cacheWrite: 5
1079
+ },
1080
+ "claude-sonnet-5-5": {
1081
+ input: 2,
1082
+ output: 10,
1083
+ cacheRead: .1,
1084
+ cacheWrite: 2.5
1085
+ },
1086
+ "claude-haiku-5-5": {
1087
+ input: .1,
1088
+ output: .5,
1089
+ cacheRead: .01,
1090
+ cacheWrite: .125
1091
+ },
1092
+ "claude-fable-5-1": {
1093
+ input: 10,
1094
+ output: 50,
1095
+ cacheRead: .25,
1096
+ cacheWrite: 12.5
1097
+ }
1098
+ };
1099
+ /** Prices usage, and remembers the models it had to price as Opus 5.5. */
1100
+ const pricer = () => {
1101
+ const unpriced = /* @__PURE__ */ new Set();
1102
+ const costOf = (entries) => entries.reduce((total, u) => {
1103
+ const key = Object.keys(PRICES).find((k) => (u.model ?? "").includes(k));
1104
+ if (!key) unpriced.add(u.model || "unnamed");
1105
+ const p = PRICES[key ?? "claude-opus-5-5"] ?? {
1106
+ input: 0,
1107
+ output: 0,
1108
+ cacheRead: 0,
1109
+ cacheWrite: 0
1110
+ };
1111
+ return total + ((u.input_tokens ?? 0) * p.input + (u.output_tokens ?? 0) * p.output + (u.cache_read_tokens ?? 0) * p.cacheRead + (u.cache_write_tokens ?? 0) * p.cacheWrite) / 1e6;
1112
+ }, 0);
1113
+ return {
1114
+ costOf,
1115
+ unpriced
1116
+ };
1117
+ };
1118
+ /** A comment's or summary's first words, without the heading marks and emoji before them. */
1119
+ const heading = (text) => (text ?? "").replace(/^[#\s]*/, "").replace(/^[^\p{L}]+/u, "");
1120
+ /** The workflow's built-in steps, where QA also runs without the key being off. */
1121
+ const BUILT_IN_STEPS = /* @__PURE__ */ new Set([
1122
+ "backlog",
1123
+ "todo",
1124
+ "in_progress",
1125
+ "in_review",
1126
+ "done",
1127
+ "blocked",
1128
+ "cancelled"
1129
+ ]);
1130
+ /** A run's cost: unknown when it ran without reporting usage, and nothing when it never started. */
1131
+ const runCost = (usage, started, costOf) => usage?.length ? costOf(usage) : started ? null : 0;
1132
+ // src/bench/requests.ts
1133
+ const REQUEST_METRICS = [
1134
+ "requests-touched",
1135
+ "requests",
1136
+ "requests-done",
1137
+ "requests-stopped",
1138
+ "requests-open",
1139
+ "requests-left-out",
1140
+ "lead-time-p50",
1141
+ "lead-time-p90",
1142
+ "people-actions-p50",
1143
+ "people-actions-p90",
1144
+ "waiting-on-people-p50",
1145
+ "waiting-on-people-p90",
1146
+ "waiting-on-people-share",
1147
+ "review-rounds-mean",
1148
+ "plan-reviews-mean",
1149
+ "runs-per-request-p50",
1150
+ "runs-per-request-p90",
1151
+ "agent-time-per-request-p50",
1152
+ "agent-time-per-request-p90",
1153
+ "cost-per-request-p50",
1154
+ "cost-per-request-p90",
1155
+ "requests-cost-known",
1156
+ "requests-cost",
1157
+ "requests-failed-runs"
1158
+ ];
1159
+ const commentKind = (summary) => {
1160
+ const s = heading(summary);
1161
+ if (!s) return "assignment or status";
1162
+ if (/^Wakeup: sub-issues closed/i.test(s)) return "stage barrier";
1163
+ if (/^Wakeup:/i.test(s)) return "wakeup";
1164
+ if (/^(FIX|DELIVERY|RESTACK|SYNTHESIS|STAGE REVIEW)\b/.test(s)) return "review request";
1165
+ if (/^PLAN v\d+.*in review/.test(s)) return "plan";
1166
+ return "comment";
1167
+ };
1168
+ const LIVE = /* @__PURE__ */ new Set([
1169
+ "queued",
1170
+ "dispatched",
1171
+ "claimed",
1172
+ "running"
1173
+ ]);
1174
+ const CLOSED = /* @__PURE__ */ new Set(["done", "cancelled"]);
1175
+ const SAME_ACTION = 12e4;
1176
+ const RUN_GRACE = 6e4;
1177
+ const hours = (x) => x / 36e5;
1178
+ const lastTouch = (i) => ms(i.last_activity_at ?? i.updated_at ?? i.created_at);
1179
+ const isReview = (run) => /^QA$/i.test(run.agent);
1180
+ const during = (r, t) => r.started <= t && t <= r.end + RUN_GRACE;
1181
+ const causedBy = (r, t) => ["assignment or status", "stage barrier"].includes(r.kind) && r.created >= t - 1e3 && r.created <= t;
1182
+ const timeWhen = (a, b, moments, holds) => {
1183
+ const cuts = [
1184
+ a,
1185
+ ...moments.filter((m) => m > a && m < b).toSorted((x, y) => x - y),
1186
+ b
1187
+ ];
1188
+ let total = 0;
1189
+ for (let i = 1; i < cuts.length; i++) {
1190
+ const from = cuts[i - 1] ?? 0;
1191
+ const to = cuts[i] ?? 0;
1192
+ if (to > from && holds((from + to) / 2)) total += to - from;
1193
+ }
1194
+ return total;
1195
+ };
1196
+ const escape = (x) => x.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&");
1197
+ const reviews = (run) => isReview(run) && run.attempt <= 1 && !(run.status === "cancelled" && !(run.started > 0));
1198
+ /** Measures the requests whose first run falls in the window. */
1199
+ const measureRequests = async (source, query) => {
1200
+ const { since, until, split } = query;
1201
+ const { costOf, unpriced } = pricer();
1202
+ const notes = [];
1203
+ const log = query.log ?? (() => {});
1204
+ const children = /* @__PURE__ */ new Map();
1205
+ for (const issue of source.issues) {
1206
+ if (!issue.parent_issue_id) continue;
1207
+ children.set(issue.parent_issue_id, [...children.get(issue.parent_issue_id) ?? [], issue]);
1208
+ }
1209
+ const treeOf = (issue) => [issue, ...(children.get(issue.id) ?? []).flatMap(treeOf)];
1210
+ const candidates = source.issues.filter((i) => !i.parent_issue_id && ms(i.created_at) < until).map((i) => ({
1211
+ request: i,
1212
+ tree: treeOf(i)
1213
+ })).filter(({ tree }) => Math.max(...tree.map(lastTouch)) >= since);
1214
+ log(`${candidates.length} requests touched in the window; reading their runs…`);
1215
+ const readIssue = async (issue) => {
1216
+ const { data, warnings } = await source.timeline(issue.id, false);
1217
+ const entries = data.toSorted((a, b) => ms(a.created_at) - ms(b.created_at)).map((e) => ({
1218
+ ...e,
1219
+ issue: issue.id
1220
+ }));
1221
+ const changes = entries.filter((e) => e.type === "activity" && e.action === "status_changed");
1222
+ const statusAt = (t) => {
1223
+ let status = changes[0]?.details?.from ?? issue.status;
1224
+ for (const c of changes) {
1225
+ if (ms(c.created_at) > t) break;
1226
+ status = c.details?.to ?? status;
1227
+ }
1228
+ return status;
1229
+ };
1230
+ return {
1231
+ issue,
1232
+ entries,
1233
+ changes,
1234
+ statusAt,
1235
+ runs: source.runs(issue.id).map((r) => {
1236
+ const kind = commentKind(r.trigger_summary);
1237
+ const created = ms(r.created_at);
1238
+ const started = ms(r.started_at);
1239
+ const completed = ms(r.completed_at);
1240
+ return {
1241
+ issue: issue.id,
1242
+ agent: source.agentNames.get(r.agent_id) ?? r.agent_id,
1243
+ status: r.status,
1244
+ attempt: r.attempt ?? 1,
1245
+ kind,
1246
+ step: kind === "assignment or status" ? statusAt(created + 1e3) ?? "" : "",
1247
+ created,
1248
+ started,
1249
+ completed,
1250
+ end: completed > 0 ? completed : LIVE.has(r.status) ? 1e400 : Math.max(created, started || 0),
1251
+ cost: runCost(r.usage, started > 0, costOf)
1252
+ };
1253
+ }),
1254
+ truncated: /truncated/i.test(warnings)
1255
+ };
1256
+ };
1257
+ const startsReview = (run) => reviews(run) && (run.kind === "review request" || query.reviewSteps.includes(run.step));
1258
+ const startsPlanReview = (run) => reviews(run) && (run.kind === "plan" || query.planSteps.includes(run.step));
1259
+ const trees = [];
1260
+ const unreadable = [];
1261
+ for (const { request, tree } of candidates) try {
1262
+ const read = [];
1263
+ for (const issue of tree) read.push(await readIssue(issue));
1264
+ trees.push({
1265
+ request,
1266
+ read
1267
+ });
1268
+ } catch (error) {
1269
+ unreadable.push(request.identifier ?? request.id);
1270
+ log(` skipped ${request.identifier ?? request.id}: ${error instanceof Error ? error.message : String(error)}`);
1271
+ }
1272
+ if (candidates.length && !trees.length) throw new Error(`couldn't read any of the ${candidates.length} requests`);
1273
+ const filed = trees.filter((t) => t.request.creator_type === "agent").map((t) => ({
1274
+ ...t,
1275
+ name: new RegExp(`(?<![\\w-])(${[t.request.identifier, t.request.id].filter((x) => !!x).map(escape).join("|")})(?![\\w-])`)
1276
+ }));
1277
+ const namedIn = (entries) => filed.filter((f) => entries.some((e) => e.type === "comment" && f.name.test(e.content ?? "")));
1278
+ const rows = [];
1279
+ const truncated = [];
1280
+ const unknown = [];
1281
+ let unstarted = 0;
1282
+ let agentMade = 0;
1283
+ let outside = 0;
1284
+ let unplaced = 0;
1285
+ const qaSteps = /* @__PURE__ */ new Map();
1286
+ for (const { request, read } of trees) {
1287
+ const root = read[0];
1288
+ if (!root) continue;
1289
+ const runsAll = read.flatMap((r) => r.runs).toSorted((a, b) => a.created - b.created);
1290
+ const start = runsAll.find((r) => r.started > 0)?.created ?? NaN;
1291
+ if (!Number.isFinite(start)) {
1292
+ unstarted++;
1293
+ continue;
1294
+ }
1295
+ if (!(start >= since && start < until)) {
1296
+ outside++;
1297
+ continue;
1298
+ }
1299
+ if (request.creator_type === "agent") {
1300
+ agentMade++;
1301
+ continue;
1302
+ }
1303
+ const partsAt = (t) => read.slice(1).filter((o) => ms(o.issue.created_at) <= t);
1304
+ const standing = (t) => {
1305
+ const status = root.statusAt(t);
1306
+ const parts = partsAt(t);
1307
+ const outcome = status === "done" && parts.every((o) => CLOSED.has(o.statusAt(t) ?? "")) ? "done" : ["cancelled", "backlog"].includes(status ?? "") ? "stopped" : "open";
1308
+ const lastTo = (r) => r.changes.findLast((c) => ms(c.created_at) <= t && c.details?.to === r.statusAt(t));
1309
+ const closes = [root, ...outcome === "done" ? parts : []].map(lastTo);
1310
+ return {
1311
+ outcome,
1312
+ at: closes.every(Boolean) ? Math.max(...closes.map((c) => ms(c?.created_at))) : NaN
1313
+ };
1314
+ };
1315
+ const placed = split.placeRequest(start, (t) => standing(t).outcome === "open");
1316
+ if (!placed) {
1317
+ unplaced++;
1318
+ continue;
1319
+ }
1320
+ const { segment, horizon } = placed;
1321
+ const { outcome, at } = standing(horizon);
1322
+ if (outcome !== "open" && !Number.isFinite(at)) {
1323
+ unknown.push(request.identifier ?? request.id);
1324
+ continue;
1325
+ }
1326
+ if (outcome !== "open" && at < start) {
1327
+ unstarted++;
1328
+ continue;
1329
+ }
1330
+ const end = outcome === "open" ? NaN : at;
1331
+ const runs = runsAll.filter((r) => r.created < horizon);
1332
+ for (const r of runs) if (isReview(r) && r.step) qaSteps.set(r.step, (qaSteps.get(r.step) ?? 0) + 1);
1333
+ if (read.some((r) => r.truncated)) truncated.push(request.identifier ?? request.id);
1334
+ const entries = read.flatMap((r) => r.entries).filter((e) => ms(e.created_at) < horizon);
1335
+ const linked = namedIn(entries).flatMap((f) => f.read);
1336
+ const blockedAt = (t) => read.some((x) => x.statusAt(t) === "blocked");
1337
+ const counts = (r, t) => !r.linked || blockedAt(t);
1338
+ const squadRuns = [...runs, ...linked.flatMap((r) => r.runs).map((r) => ({
1339
+ ...r,
1340
+ linked: true
1341
+ }))];
1342
+ const squadEntries = [...entries, ...linked.flatMap((r) => r.entries).filter((e) => ms(e.created_at) < horizon && blockedAt(ms(e.created_at)))];
1343
+ const byPerson = (o) => squadEntries.some((e) => e.actor_type !== "agent" && ms(e.created_at) >= o.created - 5e3 && ms(e.created_at) <= o.created + 1e3);
1344
+ const idle = squadRuns.filter((r) => r.kind === "wakeup" && !isReview(r) && !squadEntries.some((e) => e.actor_type === "agent" && e.action === "status_changed" && during(r, ms(e.created_at))) && !squadRuns.some((o) => o !== r && during(r, o.created) && !byPerson(o) && !(o.attempt > 1 && o.agent === r.agent && o.issue === r.issue && o.kind === r.kind)));
1345
+ const working = squadRuns.filter((r) => !idle.includes(r));
1346
+ const busy = (t) => working.some((r) => r.created <= t && r.end > t && !causedBy(r, t) && counts(r, t));
1347
+ const qaMerging = (e, t) => runs.some((r) => r.issue === e.issue && isReview(r) && r.started <= t && r.end + RUN_GRACE > t);
1348
+ const parked = (t) => {
1349
+ const status = root.statusAt(t);
1350
+ const closedAt = ms(root.changes.findLast((c) => ms(c.created_at) <= t)?.created_at);
1351
+ const takenUp = status !== "backlog" && working.some((r) => r.issue === root.issue.id && r.kind === "comment" && r.created > closedAt && r.created <= t);
1352
+ return (status === "backlog" || CLOSED.has(status ?? "") && !takenUp && partsAt(t).every((o) => CLOSED.has(o.statusAt(t) ?? ""))) && !working.some((r) => r.created <= t && r.end > t && counts(r, t));
1353
+ };
1354
+ const moments = [...read.flatMap((r) => [ms(r.issue.created_at), ...r.changes.map((c) => ms(c.created_at))]), ...working.flatMap((r) => [r.created, r.end])].filter(Number.isFinite);
1355
+ const parkedIn = (a, b) => b > a ? timeWhen(a, b, moments, parked) : 0;
1356
+ const last = outcome === "open" ? horizon : end;
1357
+ const byPeople = entries.filter((e) => {
1358
+ const t = ms(e.created_at);
1359
+ if (!(t > start + SAME_ACTION && t <= last)) return false;
1360
+ if (e.actor_type === "member") return e.type === "comment" || ["status_changed", "assignee_changed"].includes(e.action ?? "");
1361
+ return e.type === "activity" && e.action === "status_changed" && e.details?.to === "done" && e.actor_type !== "agent" && !qaMerging(e, t);
1362
+ }).map((e) => ms(e.created_at)).toSorted((a, b) => a - b);
1363
+ const actions = [];
1364
+ for (const t of byPeople) if (!(t - (actions.at(-1) ?? -1e400) <= SAME_ACTION)) actions.push(t);
1365
+ const idleEntry = (e) => idle.some((r) => r.issue === e.issue && during(r, ms(e.created_at))) && e.action !== "status_changed";
1366
+ const agentEntries = squadEntries.filter((e) => e.actor_type === "agent" && !idleEntry(e)).map((e) => ms(e.created_at));
1367
+ let waited = 0;
1368
+ let previous = start;
1369
+ for (const t of actions) {
1370
+ const lastSquad = Math.max(start, ...agentEntries.filter((m) => m < t), ...working.filter((r) => !causedBy(r, t)).flatMap((r) => [
1371
+ r.created,
1372
+ r.started,
1373
+ r.completed
1374
+ ].filter((m) => counts(r, m))).filter((m) => m < t));
1375
+ const from = Math.max(lastSquad, previous);
1376
+ if (!busy(t)) waited += t - from - parkedIn(from, t);
1377
+ previous = t;
1378
+ }
1379
+ const costs = runs.map((r) => r.cost);
1380
+ rows.push({
1381
+ request: request.identifier ?? request.id,
1382
+ segment,
1383
+ created: request.created_at,
1384
+ firstRun: new Date(start).toISOString(),
1385
+ outcome,
1386
+ leadHours: hours(end - start - parkedIn(start, end)),
1387
+ issues: read.length,
1388
+ linkedIssues: namedIn(entries).map(({ request: f }) => f.identifier ?? f.id),
1389
+ runs: runs.length,
1390
+ failedRuns: runs.filter((r) => r.status === "failed").length,
1391
+ agentHours: hours(runs.filter((r) => r.started > 0 && r.completed > 0).reduce((total, r) => total + Math.max(0, r.completed - r.started), 0)),
1392
+ cost: costs.includes(null) ? null : sum(costs.map((c) => c ?? 0)),
1393
+ costKnown: sum(costs.map((c) => c ?? 0)),
1394
+ reviewRounds: runs.filter(startsReview).length,
1395
+ planReviews: runs.filter(startsPlanReview).length,
1396
+ peopleActions: actions.length,
1397
+ waitedHours: hours(waited)
1398
+ });
1399
+ }
1400
+ const metrics = [];
1401
+ for (const segment of split.segments) {
1402
+ const { results, add } = collector(segment);
1403
+ const all = rows.filter((r) => r.segment === segment);
1404
+ const done = all.filter((r) => r.outcome === "done");
1405
+ const spread = (id, xs) => {
1406
+ const values = finite(xs);
1407
+ add(`${id}-p50`, pct(values, .5));
1408
+ add(`${id}-p90`, pct(values, .9));
1409
+ };
1410
+ add("requests", all.length);
1411
+ for (const outcome of [
1412
+ "done",
1413
+ "stopped",
1414
+ "open"
1415
+ ]) add(`requests-${outcome}`, all.filter((r) => r.outcome === outcome).length);
1416
+ spread("lead-time", done.map((r) => r.leadHours));
1417
+ spread("runs-per-request", done.map((r) => r.runs));
1418
+ spread("agent-time-per-request", done.map((r) => r.agentHours));
1419
+ spread("cost-per-request", done.map((r) => r.cost));
1420
+ add("requests-cost-known", done.filter((r) => r.cost !== null).length);
1421
+ add("review-rounds-mean", mean(done.map((r) => r.reviewRounds)));
1422
+ add("plan-reviews-mean", mean(done.map((r) => r.planReviews)));
1423
+ spread("people-actions", done.map((r) => r.peopleActions));
1424
+ spread("waiting-on-people", done.map((r) => r.waitedHours));
1425
+ const lead = sum(finite(done.map((r) => r.leadHours)));
1426
+ add("waiting-on-people-share", lead ? sum(done.map((r) => r.waitedHours)) / lead : NaN);
1427
+ add("requests-cost", sum(all.map((r) => r.costKnown)));
1428
+ add("requests-failed-runs", sum(all.map((r) => r.failedRuns)));
1429
+ metrics.push(...results);
1430
+ }
1431
+ metrics.push({
1432
+ id: "requests-touched",
1433
+ value: candidates.length
1434
+ });
1435
+ const reasons = [
1436
+ ["first run before --since or after --until", outside],
1437
+ ["no run worked on it yet", unstarted],
1438
+ ["an agent created it", agentMade],
1439
+ ["the timeline lost the move that closed it", unknown.length],
1440
+ ["the CLI couldn't read it", unreadable.length],
1441
+ ["started while neither compared setup was live", unplaced]
1442
+ ];
1443
+ for (const [reason, n] of reasons) metrics.push({
1444
+ id: "requests-left-out",
1445
+ by: { reason },
1446
+ value: n
1447
+ });
1448
+ if (unknown.length) notes.push(`The timeline lost the move that closed: ${unknown.join(", ")}.`);
1449
+ if (unreadable.length) notes.push(`The CLI couldn't read: ${unreadable.join(", ")}.`);
1450
+ if (qaSteps.size) {
1451
+ const stepsLine = [...qaSteps].map(([step, n]) => `${step} ${n}`).join(", ");
1452
+ notes.push(`QA's runs from a move or an assignment were at: ${stepsLine}.`);
1453
+ }
1454
+ const strange = [...qaSteps.keys()].filter((step) => !BUILT_IN_STEPS.has(step) && !query.reviewSteps.includes(step) && !query.planSteps.includes(step));
1455
+ if (strange.length) notes.push(`QA ran at ${strange.join(", ")}, which is neither a --review-step (${query.reviewSteps.join(", ")}) nor a --plan-step (${query.planSteps.join(", ")}): if one is your review or plan step, pass its key.`);
1456
+ if (truncated.length) notes.push(`The server cut short a timeline in these requests, so their counts miss the oldest entries: ${truncated.join(", ")}.`);
1457
+ if (unpriced.size) notes.push(`Priced as Opus 5.5: ${[...unpriced].join(", ")}.`);
1458
+ return {
1459
+ metrics,
1460
+ rows,
1461
+ notes
1462
+ };
1463
+ };
1464
+ // src/bench/timing.ts
1465
+ const TIMING_METRICS = [
1466
+ "runs",
1467
+ "failed-runs",
1468
+ "queue-wait-p50",
1469
+ "queue-wait-p90",
1470
+ "run-time-p50",
1471
+ "run-time-p90",
1472
+ "queue-hours",
1473
+ "run-hours",
1474
+ "run-cost-p50",
1475
+ "run-cost",
1476
+ "failure-causes",
1477
+ "peak-concurrent-runs",
1478
+ "review-cycles",
1479
+ "review-cycles-with-wakeup",
1480
+ "review-cycle-p50",
1481
+ "review-cycle-p90",
1482
+ "review-cycle-share"
1483
+ ];
1484
+ const KINDS = [
1485
+ [/^FIX\s+[—-]\s+mechanical/, "FIX — mechanical"],
1486
+ [/^FIX\s+[—-]\s+conflict/, "FIX — conflict"],
1487
+ [/^FIX\s+[—-]\s+owner/, "FIX — owner"],
1488
+ [/^FIX\s+[—-]\s+PLAN/, "FIX — PLAN"],
1489
+ [/^FIX\b/, "FIX"],
1490
+ [/^DELIVERY\b/, "DELIVERY"],
1491
+ [/^RESTACK\b/, "RESTACK"],
1492
+ [/^VERDICT:\s*FAIL/, "VERDICT FAIL"],
1493
+ [/^VERDICT:\s*CONFLICT/, "VERDICT CONFLICT"],
1494
+ [/^VERDICT:\s*PLAN WRONG/, "PLAN WRONG"],
1495
+ [/^VERDICT/, "VERDICT (other)"],
1496
+ [/^PLAN v\d+.*in review/, "PLAN in review"],
1497
+ [/^PLAN v\d+.*approved/, "PLAN approved"],
1498
+ [/^PLAN REVIEW/, "PLAN REVIEW"],
1499
+ [/^PLAN INFEASIBLE/, "PLAN INFEASIBLE"],
1500
+ [/^REPLAN/, "REPLAN"],
1501
+ [/^SYNTHESIS\b/, "SYNTHESIS"],
1502
+ [/^STAGE REVIEW\b/, "STAGE REVIEW"],
1503
+ [/^FINISHED/, "FINISHED"],
1504
+ [/^QUESTIONS/, "QUESTIONS"],
1505
+ [/^BLOCKED/, "BLOCKED"],
1506
+ [/^ESCALATE/, "ESCALATE"],
1507
+ [/^Delegated task/i, "failure notice"]
1508
+ ];
1509
+ const kindOf = (summary) => {
1510
+ const s = heading(summary);
1511
+ if (!s) return "assignment or status";
1512
+ if (/^Wakeup: sub-issues closed/i.test(s)) return "stage barrier";
1513
+ if (/^Wakeup:/i.test(s)) return "wakeup";
1514
+ return KINDS.find(([re]) => re.test(s))?.[1] ?? "comment";
1515
+ };
1516
+ const HANDOFFS = /* @__PURE__ */ new Set([
1517
+ "DELIVERY",
1518
+ "FIX",
1519
+ "FIX — mechanical",
1520
+ "FIX — conflict",
1521
+ "FIX — owner",
1522
+ "FIX — PLAN",
1523
+ "RESTACK",
1524
+ "SYNTHESIS",
1525
+ "STAGE REVIEW"
1526
+ ]);
1527
+ const statusAt = (issue, changes, t) => {
1528
+ let status = changes[0]?.details?.from ?? issue.status;
1529
+ for (const c of changes) {
1530
+ if (ms(c.created_at) > t) break;
1531
+ status = c.details?.to ?? status;
1532
+ }
1533
+ return status;
1534
+ };
1535
+ /** Measures the runs created in the window, and the review cycles that start in it. */
1536
+ const measureTiming = async (source, query) => {
1537
+ const { since, until, split } = query;
1538
+ const { costOf, unpriced } = pricer();
1539
+ const log = query.log ?? (() => {});
1540
+ const notes = [];
1541
+ const inWindow = (t) => t >= since && t < until;
1542
+ const recent = source.issues.filter((i) => ms(i.last_activity_at ?? i.updated_at ?? i.created_at) >= since && ms(i.created_at) < until);
1543
+ log(`${source.issues.length} issues, ${recent.length} touched in the window; reading their runs…`);
1544
+ const runs = [];
1545
+ const histories = [];
1546
+ let unread = 0;
1547
+ const truncated = [];
1548
+ for (const issue of recent) {
1549
+ let list;
1550
+ try {
1551
+ list = source.runs(issue.id);
1552
+ } catch (error) {
1553
+ log(` skipped ${issue.identifier ?? issue.id}: ${error instanceof Error ? error.message : String(error)}`);
1554
+ continue;
1555
+ }
1556
+ let changes = null;
1557
+ if (list.some((r) => kindOf(r.trigger_summary) === "assignment or status")) try {
1558
+ const { data, warnings } = await source.timeline(issue.id, true);
1559
+ const cut = /truncated by the server cap \(([^)]*)\)/i.exec(warnings);
1560
+ if (cut ? /activit/i.test(cut[1] ?? "") : /truncated/i.test(warnings)) truncated.push(issue.identifier ?? issue.id);
1561
+ changes = data.toSorted((a, b) => ms(a.created_at) - ms(b.created_at));
1562
+ } catch (error) {
1563
+ unread++;
1564
+ log(` no steps for ${issue.identifier ?? issue.id}: ${error instanceof Error ? error.message : String(error)}`);
1565
+ }
1566
+ const history = [];
1567
+ for (const r of list) {
1568
+ const kind = kindOf(r.trigger_summary);
1569
+ const usage = (r.usage ?? []).reduce((total, u) => ({
1570
+ input: total.input + (u.input_tokens ?? 0),
1571
+ output: total.output + (u.output_tokens ?? 0),
1572
+ cacheRead: total.cacheRead + (u.cache_read_tokens ?? 0),
1573
+ cacheWrite: total.cacheWrite + (u.cache_write_tokens ?? 0)
1574
+ }), {
1575
+ input: 0,
1576
+ output: 0,
1577
+ cacheRead: 0,
1578
+ cacheWrite: 0
1579
+ });
1580
+ const created = ms(r.created_at);
1581
+ const record = {
1582
+ run: r.id,
1583
+ segment: inWindow(created) ? split.segmentOf(created) ?? null : null,
1584
+ issue: issue.identifier ?? issue.id,
1585
+ issueId: issue.id,
1586
+ parent: issue.parent_issue_id ?? null,
1587
+ agent: source.agentNames.get(r.agent_id) ?? r.agent_id,
1588
+ leader: Boolean(r.is_leader_task),
1589
+ priority: r.priority ?? null,
1590
+ status: r.status,
1591
+ failureReason: r.status === "failed" ? r.failure_reason || "unknown" : null,
1592
+ kind: kind === "assignment or status" && changes ? `at ${statusAt(issue, changes, created + 1e3) ?? ""}` : kind,
1593
+ attempt: r.attempt,
1594
+ created: r.created_at,
1595
+ started: r.started_at,
1596
+ completed: r.completed_at,
1597
+ queueMin: (ms(r.started_at) - created) / 6e4,
1598
+ runMin: (ms(r.completed_at) - ms(r.started_at)) / 6e4,
1599
+ usage,
1600
+ models: [...new Set((r.usage ?? []).map((u) => u.model).filter((m) => !!m))],
1601
+ cost: runCost(r.usage, ms(r.started_at) > 0, costOf)
1602
+ };
1603
+ history.push(record);
1604
+ if (inWindow(created)) runs.push(record);
1605
+ }
1606
+ histories.push({
1607
+ issue,
1608
+ history
1609
+ });
1610
+ }
1611
+ const reviewer = runs.find((r) => /^QA$/i.test(r.agent))?.agent ?? "QA";
1612
+ const reviewKinds = new Set(query.reviewSteps.map((step) => `at ${step}`));
1613
+ const cycles = [];
1614
+ for (const { issue, history } of histories) {
1615
+ const ordered = history.filter((r) => r.created).toSorted((a, b) => ms(a.created) - ms(b.created));
1616
+ let current = null;
1617
+ for (const r of ordered) {
1618
+ if (r.agent !== reviewer) {
1619
+ if (r.kind.startsWith("at ")) current = null;
1620
+ continue;
1621
+ }
1622
+ if (current && (r.kind === "wakeup" || (r.attempt ?? 0) > 1)) current.runs.push(r);
1623
+ else if (HANDOFFS.has(r.kind) || reviewKinds.has(r.kind)) {
1624
+ current = {
1625
+ issue: issue.identifier ?? issue.id,
1626
+ kind: r.kind,
1627
+ runs: [r],
1628
+ start: ms(r.created)
1629
+ };
1630
+ if (inWindow(current.start)) cycles.push(current);
1631
+ }
1632
+ }
1633
+ if (current && cycles.at(-1) === current && query.reviewSteps.includes(issue.status ?? "")) current.pending = true;
1634
+ }
1635
+ const cycleRows = [];
1636
+ for (const c of cycles) {
1637
+ const first = c.runs[0];
1638
+ const last = c.runs.at(-1);
1639
+ const segment = split.segmentOf(c.start);
1640
+ if (!first || !last || segment === void 0 || c.pending) continue;
1641
+ if (last.status !== "completed" || !c.runs.every((r) => r.completed)) continue;
1642
+ const total = (ms(last.completed) - ms(first.created)) / 6e4;
1643
+ const queue = sum(c.runs.map((r) => Math.max(0, r.queueMin || 0)));
1644
+ const work = sum(c.runs.map((r) => Math.max(0, r.runMin || 0)));
1645
+ const costs = c.runs.map((r) => r.cost);
1646
+ cycleRows.push({
1647
+ issue: c.issue,
1648
+ segment,
1649
+ kind: c.kind,
1650
+ wakeups: c.runs.filter((r) => r.kind === "wakeup").length,
1651
+ total,
1652
+ queue,
1653
+ work,
1654
+ ciGap: Math.max(0, total - queue - work),
1655
+ cost: costs.includes(null) ? null : sum(costs.map((x) => x ?? 0))
1656
+ });
1657
+ }
1658
+ const metrics = [];
1659
+ for (const segment of split.segments) {
1660
+ const { results, add } = collector(segment);
1661
+ const inSegment = runs.filter((r) => r.segment === segment);
1662
+ const summarise = (group, slice) => {
1663
+ const queue = finite(group.map((r) => r.queueMin));
1664
+ const run = finite(group.map((r) => r.runMin));
1665
+ const cost = finite(group.filter((r) => ms(r.started) > 0 || r.cost !== 0).map((r) => r.cost));
1666
+ add("runs", group.length, slice);
1667
+ add("failed-runs", group.filter((r) => r.status === "failed").length, slice);
1668
+ add("queue-wait-p50", pct(queue, .5), slice);
1669
+ add("queue-wait-p90", pct(queue, .9), slice);
1670
+ add("run-time-p50", pct(run, .5), slice);
1671
+ add("run-time-p90", pct(run, .9), slice);
1672
+ add("queue-hours", sum(queue) / 60, slice);
1673
+ add("run-hours", sum(run) / 60, slice);
1674
+ add("run-cost-p50", pct(cost, .5), slice);
1675
+ add("run-cost", cost.length ? sum(cost) : NaN, slice);
1676
+ };
1677
+ add("runs", inSegment.length);
1678
+ for (const [seat, group] of by(inSegment, (r) => r.agent)) summarise(group, { seat });
1679
+ for (const [, group] of by(inSegment, (r) => `${r.agent} · ${r.kind}`)) {
1680
+ const first = group[0];
1681
+ if (first) summarise(group, {
1682
+ seat: first.agent,
1683
+ kind: first.kind
1684
+ });
1685
+ }
1686
+ const failures = inSegment.filter((r) => r.failureReason);
1687
+ for (const [, group] of by(failures, (r) => `${r.agent}\u0000${r.failureReason ?? ""}`)) {
1688
+ const first = group[0];
1689
+ if (first) add("failure-causes", group.length, {
1690
+ seat: first.agent,
1691
+ reason: first.failureReason ?? ""
1692
+ });
1693
+ }
1694
+ for (const [seat, group] of by(inSegment, (r) => r.agent)) {
1695
+ const events = group.filter((r) => r.started && r.completed).flatMap((r) => [[ms(r.started), 1], [ms(r.completed), -1]]).toSorted((a, b) => a[0] - b[0] || a[1] - b[1]);
1696
+ let now = 0;
1697
+ let peak = 0;
1698
+ for (const [, d] of events) peak = Math.max(peak, now += d);
1699
+ add("peak-concurrent-runs", peak, { seat });
1700
+ }
1701
+ const segmentCycles = cycleRows.filter((c) => c.segment === segment);
1702
+ add("review-cycles", segmentCycles.length);
1703
+ add("review-cycles-with-wakeup", segmentCycles.filter((c) => c.wakeups > 0).length);
1704
+ const totalTime = Math.max(1, sum(segmentCycles.map((c) => c.total)));
1705
+ for (const [part, key] of [
1706
+ ["total", "total"],
1707
+ ["queue", "queue"],
1708
+ ["work", "work"],
1709
+ ["ci-gap", "ciGap"]
1710
+ ]) {
1711
+ const xs = finite(segmentCycles.map((c) => c[key]));
1712
+ add("review-cycle-p50", pct(xs, .5), { part });
1713
+ add("review-cycle-p90", pct(xs, .9), { part });
1714
+ add("review-cycle-share", segmentCycles.length ? sum(xs) / totalTime : NaN, { part });
1715
+ }
1716
+ metrics.push(...results);
1717
+ }
1718
+ const strange = [...new Set(runs.filter((r) => r.agent === reviewer && r.kind.startsWith("at ")).map((r) => r.kind.slice(3)).filter((step) => !BUILT_IN_STEPS.has(step) && !query.reviewSteps.includes(step) && !query.planSteps.includes(step)))];
1719
+ if (strange.length) notes.push(`${reviewer} ran at ${strange.join(", ")}, which is neither a --review-step (${query.reviewSteps.join(", ")}) nor a --plan-step (${query.planSteps.join(", ")}): if one is your review or plan step, pass its key.`);
1720
+ if (unread) notes.push(`${unread} issues had an unreadable timeline.`);
1721
+ if (truncated.length) notes.push(`The server cut these timelines short, so early steps may be off: ${truncated.join(", ")}.`);
1722
+ if (unpriced.size) notes.push(`Priced as Opus 5.5: ${[...unpriced].join(", ")}.`);
1723
+ return {
1724
+ metrics,
1725
+ runs,
1726
+ cycles: cycleRows,
1727
+ notes
1728
+ };
1729
+ };
1730
+ // src/bench/verdicts.ts
1731
+ const VERDICT_METRICS = [
1732
+ "first-verdicts",
1733
+ "first-fail-blockers",
1734
+ "blockers-per-first-fail",
1735
+ "first-fails-only-mechanical",
1736
+ "blocker-outcomes",
1737
+ "fix-answers",
1738
+ "issues-by-pass-round"
1739
+ ];
1740
+ const section = (text, name) => {
1741
+ const lines = (text ?? "").split("\n");
1742
+ const start = lines.findIndex((l) => new RegExp(`^##\\s+${name}\\b`, "i").test(l));
1743
+ if (start < 0) return [];
1744
+ const end = lines.findIndex((l, i) => i > start && /^##\s/.test(l));
1745
+ return lines.slice(start + 1, end < 0 ? void 0 : end).filter((l) => l.trim());
1746
+ };
1747
+ const ledger = (line) => {
1748
+ const m = /^\s*(?:\d+\.|-)\s*(B\d+)\s*[·—-]?\s*(.*)$/.exec(line);
1749
+ if (!m) return null;
1750
+ const rest = m[2] ?? "";
1751
+ const raised = /raised r(\d+) as `?([a-z]+)`?/.exec(rest);
1752
+ const state = /\b(open|fixed r\d+|withdrawn r\d+|nit r\d+)\b/.exec(rest);
1753
+ return {
1754
+ id: m[1] ?? "",
1755
+ raisedRound: raised ? Number(raised[1]) : null,
1756
+ origin: raised?.[2] ?? null,
1757
+ state: state?.[1]?.replace(/ r\d+$/, "") ?? null,
1758
+ text: rest
1759
+ };
1760
+ };
1761
+ const answer = (line) => {
1762
+ const m = /^\s*\d+\.\s*(B\d+)\s*(?:→|->)\s*(fixed|rebutted|reading|not mechanical|not fixed)\b[:\s]*(.*)$/i.exec(line);
1763
+ return m ? {
1764
+ id: m[1] ?? "",
1765
+ answer: (m[2] ?? "").toLowerCase()
1766
+ } : null;
1767
+ };
1768
+ const finding = ({ id, origin, state, raisedRound }) => ({
1769
+ id,
1770
+ origin,
1771
+ state,
1772
+ raisedRound
1773
+ });
1774
+ const isVerdict = (e) => e.type === "verdict";
1775
+ /** Measures the work issues with a verdict in the window. */
1776
+ const measureVerdicts = (source, query) => {
1777
+ const { since, until, split } = query;
1778
+ const log = query.log ?? (() => {});
1779
+ const inWindow = (at) => ms(at) >= since && ms(at) < until;
1780
+ const recent = source.issues.filter((i) => ms(i.last_activity_at ?? i.updated_at ?? i.created_at) >= since && ms(i.created_at) < until);
1781
+ log(`${recent.length} issues touched in the window; reading their verdicts…`);
1782
+ const results = [];
1783
+ for (const issue of recent) {
1784
+ let comments;
1785
+ try {
1786
+ comments = source.comments(issue.id);
1787
+ } catch (error) {
1788
+ log(` skipped ${issue.identifier ?? issue.id}: ${error instanceof Error ? error.message : String(error)}`);
1789
+ continue;
1790
+ }
1791
+ const events = [];
1792
+ for (const c of comments.toSorted((a, b) => ms(a.created_at) - ms(b.created_at))) {
1793
+ const head = heading((c.content ?? "").split("\n")[0]);
1794
+ const author = source.agentNames.get(c.author_id ?? "") ?? c.author_type ?? "";
1795
+ if (head.startsWith("VERDICT:")) {
1796
+ const result = /^VERDICT:\s*(PASS|FAIL|PLAN WRONG|CONFLICT|ESCALATE|PENDING CI)\b/.exec(head)?.[1]?.toUpperCase() ?? "?";
1797
+ const report = (c.attachments ?? []).find((a) => /^(verdict-r\d+|integration-review-.+|escalate)\.md$/.test(a.filename ?? ""));
1798
+ const text = report ? source.attachment(report) : null;
1799
+ const reasons = new Set(section(c.content, "Blocking").map(ledger).filter((f) => f !== null).map((f) => f.id));
1800
+ const blocking = section(text, "Blocking").map(ledger).filter((f) => f !== null).map(finding);
1801
+ for (const id of reasons) if (!blocking.some((f) => f.id === id)) blocking.push({
1802
+ id,
1803
+ origin: null,
1804
+ state: null,
1805
+ raisedRound: null
1806
+ });
1807
+ events.push({
1808
+ type: "verdict",
1809
+ at: c.created_at,
1810
+ author,
1811
+ result,
1812
+ round: Number(/\(round (\d+)\)/.exec(head)?.[1]) || null,
1813
+ report: report?.filename ?? null,
1814
+ blocking,
1815
+ settled: section(text, "Settled").map(ledger).filter((f) => f !== null).map(finding),
1816
+ nits: section(text, "Nits").length,
1817
+ needsDecision: section(c.content, "Needs a decision").length
1818
+ });
1819
+ } else if (/^(FIX|DELIVERY|RESTACK)\b/.test(head)) events.push({
1820
+ type: (head.split(/\s/)[0] ?? "").toUpperCase(),
1821
+ variant: /^FIX\s+[—-]\s+(mechanical|owner|conflict|PLAN)\b/i.exec(head)?.[1]?.toLowerCase() ?? null,
1822
+ at: c.created_at,
1823
+ author,
1824
+ answers: (c.content ?? "").split("\n").map(answer).filter((a) => a !== null)
1825
+ });
1826
+ }
1827
+ const before = events.filter((e) => ms(e.at) < until);
1828
+ const firstInWindow = before.find((e) => e.type === "verdict" && ms(e.at) >= since);
1829
+ if (!firstInWindow) continue;
1830
+ results.push({
1831
+ issue: issue.identifier ?? issue.id,
1832
+ parent: issue.parent_issue_id ?? null,
1833
+ repository: /github\.com\/([^/]+\/[^/]+)\//.exec(issue.metadata?.pr_url ?? "")?.[1] ?? null,
1834
+ segment: split.segmentOf(ms(firstInWindow.at)) ?? null,
1835
+ events: before
1836
+ });
1837
+ }
1838
+ const firsts = results.map((r) => ({
1839
+ r,
1840
+ v: r.events.filter(isVerdict).find((e) => !["PENDING CI", "?"].includes(e.result))
1841
+ })).filter((x) => !!x.v && inWindow(x.v.at));
1842
+ const endOf = (r, id) => {
1843
+ for (const e of r.events.toReversed()) {
1844
+ if (!isVerdict(e)) continue;
1845
+ const f = [...e.blocking, ...e.settled].find((x) => x.id === id);
1846
+ if (f) return f.state ?? (e.blocking.includes(f) ? "open" : "settled");
1847
+ }
1848
+ return "unknown";
1849
+ };
1850
+ const metrics = [];
1851
+ for (const segment of split.segments) {
1852
+ const issues = results.filter((r) => r.segment === segment);
1853
+ const segmentFirsts = firsts.filter(({ v }) => split.segmentOf(ms(v.at)) === segment);
1854
+ const answers = results.flatMap((r) => r.events.filter((e) => ms(e.at) >= since && split.segmentOf(ms(e.at)) === segment)).flatMap((e) => "answers" in e ? e.answers : []);
1855
+ if (issues.length === 0 && segmentFirsts.length === 0) continue;
1856
+ const { results: out, add } = collector(segment);
1857
+ add("first-verdicts", segmentFirsts.length);
1858
+ for (const [result, n] of count(segmentFirsts.map(({ v }) => v.result))) add("first-verdicts", n, { result });
1859
+ const failed = segmentFirsts.filter(({ v }) => v.result === "FAIL");
1860
+ const blockers = failed.flatMap(({ r, v }) => v.blocking.map((f) => ({
1861
+ r,
1862
+ f
1863
+ })));
1864
+ add("first-fail-blockers", blockers.length);
1865
+ for (const [origin, n] of count(blockers.map(({ f }) => f.origin ?? "not in a report"))) add("first-fail-blockers", n, { origin });
1866
+ add("blockers-per-first-fail", blockers.length / Math.max(1, failed.length));
1867
+ add("first-fails-only-mechanical", failed.filter(({ v }) => v.blocking.length && v.blocking.every((f) => f.origin === "mechanical")).length);
1868
+ for (const [state, n] of count(blockers.map(({ r, f }) => endOf(r, f.id)))) add("blocker-outcomes", n, { state });
1869
+ add("fix-answers", answers.length);
1870
+ for (const [kind, n] of count(answers.map((a) => a.answer))) add("fix-answers", n, { answer: kind });
1871
+ const rounds = issues.map((r) => {
1872
+ const pass = r.events.filter(isVerdict).find((e) => e.result === "PASS");
1873
+ return pass ? `PASS at round ${pass.round ?? "?"}` : "no PASS yet";
1874
+ });
1875
+ for (const [round, n] of count(rounds)) add("issues-by-pass-round", n, { round });
1876
+ metrics.push(...out);
1877
+ }
1878
+ return {
1879
+ metrics,
1880
+ rows: results,
1881
+ notes: []
1882
+ };
1883
+ };
1884
+ // src/bench/index.ts
1885
+ /** Every metric the Multica connector reports. */
1886
+ const MULTICA_METRICS = [
1887
+ ...REQUEST_METRICS,
1888
+ ...TIMING_METRICS,
1889
+ ...VERDICT_METRICS
1890
+ ];
1891
+ /** Measures everything from `source`. Tests hand it a source of fixtures. */
1892
+ const benchFrom = async (source, query) => {
1893
+ (query.log ?? (() => {}))(`Workspace ${source.workspace.name}`);
1894
+ const requests = await measureRequests(source, query);
1895
+ const timing = await measureTiming(source, query);
1896
+ const verdicts = measureVerdicts(source, query);
1897
+ return {
1898
+ metrics: [
1899
+ ...requests.metrics,
1900
+ ...timing.metrics,
1901
+ ...verdicts.metrics
1902
+ ],
1903
+ rows: {
1904
+ requests: requests.rows,
1905
+ runs: timing.runs,
1906
+ cycles: timing.cycles,
1907
+ verdicts: verdicts.rows
1908
+ },
1909
+ notes: [
1910
+ ...source.notes,
1911
+ ...requests.notes,
1912
+ ...timing.notes,
1913
+ ...verdicts.notes
1914
+ ]
1915
+ };
1916
+ };
1917
+ /** Measures the target's workspace on Multica. */
1918
+ const bench = async (query, context) => {
1919
+ const source = await openSource(query, context);
1920
+ try {
1921
+ return await benchFrom(source, query);
1922
+ } finally {
1923
+ source.close();
1924
+ }
1925
+ };
1926
+ // src/plan.ts
1927
+ /** The agent fields `agent update` sets, with their flags. */
1928
+ const SETTINGS = [
1929
+ ["description", "description"],
1930
+ ["model", "model"],
1931
+ ["thinking_level", "thinking-level"],
1932
+ ["max_concurrent_tasks", "max-concurrent-tasks"]
1933
+ ];
1934
+ const memberName = (member) => member.name.toLowerCase();
1935
+ /** Every change that makes `live` match `rendered`, in the order `apply` makes them. */
1936
+ const planChanges = (rendered, live) => {
1937
+ const changes = [];
1938
+ const data = (operation) => ({
1939
+ ...operation,
1940
+ cli: live.cli,
1941
+ workspace: live.workspace.id
1942
+ });
1943
+ const change = (kind, id, summary, operation, diff = "") => changes.push({
1944
+ kind,
1945
+ id,
1946
+ summary,
1947
+ ...diff ? { diff } : {},
1948
+ data: data(operation)
1949
+ });
1950
+ const manual = (kind, id, summary) => changes.push({
1951
+ kind,
1952
+ id,
1953
+ summary: `! ${summary}`,
1954
+ manual: true
1955
+ });
1956
+ const note = (kind, id, summary) => changes.push({
1957
+ kind,
1958
+ id,
1959
+ summary: `· ${summary}`,
1960
+ note: true
1961
+ });
1962
+ for (const skill of rendered.skills) {
1963
+ const files = skillFiles(skill);
1964
+ const found = live.skills.find((candidate) => candidate.name === skill.name);
1965
+ if (!found) {
1966
+ change("skill", skill.name, `+ ${skill.name} (new)`, {
1967
+ op: "skill-import",
1968
+ skill
1969
+ });
1970
+ continue;
1971
+ }
1972
+ const notes = [];
1973
+ let diffs = "";
1974
+ for (const [path, text] of files) {
1975
+ const have = found.files[path];
1976
+ if (have === void 0) {
1977
+ notes.push(`+ ${path}`);
1978
+ continue;
1979
+ }
1980
+ const { lines, diff } = textDiff(norm(have), text);
1981
+ if (lines) {
1982
+ notes.push(`~ ${path} (${differ(lines)})`);
1983
+ diffs += `--- ${path}\n${diff}\n`;
1984
+ }
1985
+ }
1986
+ for (const path of Object.keys(found.files)) if (!files.has(path)) notes.push(`- ${path}`);
1987
+ if (notes.length > 0) change("skill", skill.name, `~ ${skill.name}: ${notes.join(", ")}`, {
1988
+ op: "skill-import",
1989
+ skill
1990
+ }, diffs);
1991
+ }
1992
+ const skillNames = new Set(rendered.skills.map((skill) => skill.name));
1993
+ const liveById = /* @__PURE__ */ new Map();
1994
+ for (const agent of rendered.agents) {
1995
+ const found = live.agents.find((candidate) => candidate.name === agent.name);
1996
+ if (!found) {
1997
+ manual("agent", agent.id, `${agent.name}: no agent with this name; create it first`);
1998
+ continue;
1999
+ }
2000
+ liveById.set(agent.id, found);
2001
+ const args = [];
2002
+ const notes = [];
2003
+ const { lines, diff } = textDiff(norm(found.instructions), norm(agent.instructions));
2004
+ if (lines) {
2005
+ args.push("--instructions", agent.instructions.replace(/\n+$/, ""));
2006
+ notes.push(`instructions (${differ(lines)})`);
2007
+ }
2008
+ const want = {
2009
+ description: agent.description,
2010
+ model: agent.model.id,
2011
+ thinking_level: agent.model.thinking,
2012
+ max_concurrent_tasks: String(agent.concurrency)
2013
+ };
2014
+ for (const [key, flag] of SETTINGS) {
2015
+ const have = found[key];
2016
+ if (want[key] && have !== want[key]) {
2017
+ args.push(`--${flag}`, want[key]);
2018
+ notes.push(`${flag}: ${preview(have)} → ${preview(want[key])}`);
2019
+ }
2020
+ }
2021
+ if (args.length > 0) change("agent", agent.id, `~ ${agent.name} ${notes.join(", ")}`, {
2022
+ op: "agent-update",
2023
+ agent: found.id,
2024
+ args
2025
+ }, diff);
2026
+ const wanted = [...new Set(agent.skills)].filter((name) => skillNames.has(name));
2027
+ const missing = wanted.filter((name) => !found.skills.some((skill) => skill.name === name));
2028
+ for (const name of wanted) if (found.skills.some((skill) => skill.name === name && !skill.enabled)) manual("agent", agent.id, `${agent.name}: ${name} is attached but disabled; switch it on in the agent's Skills tab`);
2029
+ if (missing.length > 0) change("agent", agent.id, `+ ${agent.name} skills: ${missing.join(", ")}`, {
2030
+ op: "agent-skills-add",
2031
+ agent: found.id,
2032
+ skills: missing
2033
+ });
2034
+ }
2035
+ if (rendered.squad) {
2036
+ const squad = rendered.squad;
2037
+ const found = live.squad;
2038
+ if (!found) manual("squad", squad.name, `no squad named ${squad.name}; create it first`);
2039
+ else {
2040
+ const { lines, diff } = textDiff(norm(found.instructions), norm(squad.instructions));
2041
+ if (lines) change("squad", squad.name, `~ ${squad.name} instructions (${differ(lines)})`, {
2042
+ op: "squad-instructions",
2043
+ squad: found.id,
2044
+ instructions: squad.instructions.replace(/\n+$/, "")
2045
+ }, diff);
2046
+ for (const agent of rendered.agents) {
2047
+ const liveAgent = liveById.get(agent.id);
2048
+ if (!liveAgent) continue;
2049
+ const role = squad.members[agent.id] ?? agent.role;
2050
+ const member = found.members.find((candidate) => candidate.member_id === liveAgent.id && candidate.member_type === "agent");
2051
+ if (!member) change("squad", squad.name, `+ ${agent.name} joins ${squad.name}`, {
2052
+ op: "squad-member-add",
2053
+ squad: found.id,
2054
+ agent: liveAgent.id,
2055
+ role
2056
+ });
2057
+ else if (role && member.role !== role) change("squad", squad.name, `~ ${agent.name} role: ${preview(member.role)} → ${preview(role)}`, {
2058
+ op: "squad-member-role",
2059
+ squad: found.id,
2060
+ agent: liveAgent.id,
2061
+ role
2062
+ });
2063
+ }
2064
+ const leader = rendered.agents.find((agent) => agent.id === squad.leader);
2065
+ const liveLeader = liveById.get(squad.leader);
2066
+ if (leader && liveLeader && found.leader_id !== liveLeader.id) change("squad", squad.name, `~ leader → ${leader.name}`, {
2067
+ op: "squad-leader",
2068
+ squad: found.id,
2069
+ agent: liveLeader.id
2070
+ });
2071
+ }
2072
+ }
2073
+ const held = planWorkflow(rendered, live, {
2074
+ change,
2075
+ manual,
2076
+ note,
2077
+ liveById
2078
+ });
2079
+ if (!rendered.sharedContext) note("context", "context", "the setup gives this workspace no Agent context: left alone");
2080
+ else {
2081
+ const { lines, diff } = textDiff(norm(live.context), norm(rendered.sharedContext));
2082
+ if (lines && held) note("context", "context", `Agent context (${differ(lines)}): waits for the workflow`);
2083
+ else if (lines) change("context", "context", `~ Agent context (${differ(lines)})`, {
2084
+ op: "context",
2085
+ context: rendered.sharedContext
2086
+ }, diff);
2087
+ }
2088
+ return changes;
2089
+ };
2090
+ /** A move's status key as `plan` compares it: `""` for none, `?` while it isn't known. */
2091
+ const moveKey = (specs, keys, to) => to === void 0 ? "" : keyAt(specs, keys, to) ?? "?";
2092
+ /** Plans the Default workflow. Returns true when something only a person can do holds it back. */
2093
+ const planWorkflow = (rendered, live, { change, manual, note, liveById }) => {
2094
+ if (rendered.workflows.length === 0) return false;
2095
+ if ("problem" in live.workflow) {
2096
+ manual("workflow", "default", live.workflow.problem);
2097
+ return true;
2098
+ }
2099
+ const workflow = live.workflow;
2100
+ const merged = mergeWorkflows(rendered);
2101
+ let unresolved = false;
2102
+ const reported = /* @__PURE__ */ new Set();
2103
+ const once = (key, summary) => {
2104
+ unresolved = true;
2105
+ if (!reported.has(key)) manual("workflow", "default", summary);
2106
+ reported.add(key);
2107
+ };
2108
+ const owner = rendered.target.vars.owner;
2109
+ const handlerOf = (handoff) => {
2110
+ if (handoff.startsWith("agent:")) {
2111
+ const id = handoff.slice(6);
2112
+ const agent = rendered.agents.find((candidate) => candidate.id === id);
2113
+ const found = liveById.get(id) ?? live.agents.find((candidate) => candidate.name === agent?.name);
2114
+ if (found) return {
2115
+ type: "agent",
2116
+ id: found.id
2117
+ };
2118
+ once(`agent:${id}`, `Agent: ${agent?.name ?? id}: no agent with this name; create it first`);
2119
+ return;
2120
+ }
2121
+ if (handoff === "owner") {
2122
+ if (owner === void 0 || owner === "") {
2123
+ once("owner", "owner: the target sets no vars.owner, so no member takes the owner's steps");
2124
+ return;
2125
+ }
2126
+ const want = String(owner).toLowerCase();
2127
+ let found = live.members.filter((member) => memberName(member) === want);
2128
+ if (found.length === 0) found = live.members.filter((member) => memberName(member).split(/\s+/)[0] === want);
2129
+ const [only] = found;
2130
+ if (found.length === 1 && only) {
2131
+ if (!reported.has("owner")) note("workflow", "default", `owner: ${String(owner)} is ${only.name}${only.email ? ` <${only.email}>` : ""}`);
2132
+ reported.add("owner");
2133
+ return {
2134
+ type: "member",
2135
+ id: only.user_id
2136
+ };
2137
+ }
2138
+ once("owner", found.length > 0 ? `owner: ${String(owner)} matches ${found.map((member) => member.name).join(" and ")}: write the full name in vars.owner` : `owner: ${String(owner)}: no workspace member has this name`);
2139
+ return;
2140
+ }
2141
+ };
2142
+ const specs = merged.columns.map((column) => {
2143
+ const handler = handlerOf(column.handoff);
2144
+ return {
2145
+ name: column.name,
2146
+ ...column.key ? { key: column.key } : { category: column.category ?? "started" },
2147
+ ...handler ? { handler } : {},
2148
+ instructions: column.instructions ?? "",
2149
+ ...column.next === void 0 ? {} : { next: column.next },
2150
+ ...column.back === void 0 ? {} : { back: column.back }
2151
+ };
2152
+ });
2153
+ if (unresolved) return true;
2154
+ const liveSteps = workflow.steps;
2155
+ const liveKeys = liveSteps.map((step) => step.status_key);
2156
+ const keys = customKeys(liveKeys, specs);
2157
+ const want = stepsFor(specs, keys);
2158
+ const wanted = specs.map((_, place) => keyAt(specs, keys, place));
2159
+ const notes = [];
2160
+ let diffs = "";
2161
+ specs.forEach((spec, place) => {
2162
+ const key = wanted[place];
2163
+ if (!key || !liveKeys.includes(key)) notes.push(`+ ${spec.name}`);
2164
+ });
2165
+ for (const dropped of liveKeys.filter((key) => !wanted.includes(key))) notes.push(`- ${dropped}`);
2166
+ const order = liveKeys.filter((key) => wanted.includes(key));
2167
+ if (order.join() !== wanted.filter((key) => key && order.includes(key)).join()) notes.push("column order");
2168
+ specs.forEach((spec, place) => {
2169
+ const step = liveSteps.find((candidate) => candidate.status_key === wanted[place]);
2170
+ const target = want[place];
2171
+ if (!step || !target) return;
2172
+ const have = step.handler ?? { type: "none" };
2173
+ const wantHandler = target.handler ?? { type: "none" };
2174
+ const haveId = "id" in have ? have.id ?? "" : "";
2175
+ const wantId = "id" in wantHandler ? wantHandler.id : "";
2176
+ if (have.type !== wantHandler.type || haveId !== wantId) notes.push(`${spec.name} handoff`);
2177
+ else if (target.handler) {
2178
+ const { lines, diff } = textDiff(norm(step.instructions), norm(target.instructions));
2179
+ if (lines) {
2180
+ notes.push(`${spec.name} instructions (${differ(lines)})`);
2181
+ diffs += `--- ${spec.name}\n${diff}\n`;
2182
+ }
2183
+ }
2184
+ const wantMoves = `${moveKey(specs, keys, spec.next)}|${moveKey(specs, keys, spec.back)}`;
2185
+ if (`${step.next_status_key ?? ""}|${step.back_status_key ?? ""}` !== wantMoves) notes.push(`${spec.name} moves`);
2186
+ });
2187
+ if (notes.length > 0) change("workflow", "default", `~ Default workflow: ${notes.join(", ")}`, {
2188
+ op: "workflow",
2189
+ workflow: workflow.id,
2190
+ specs
2191
+ }, diffs);
2192
+ const on = new Set(workflow.project_ids);
2193
+ const command = [
2194
+ "multica",
2195
+ ...live.cli.profileArgs,
2196
+ "--workspace-id",
2197
+ live.workspace.id
2198
+ ].join(" ");
2199
+ for (const project of live.projects.filter((candidate) => !on.has(candidate.id))) note("project", project.id, `${project.name}: on another workflow; switch it back with \`${command} project workflow set ${project.id} default\``);
2200
+ return false;
2201
+ };
2202
+ // src/index.ts
2203
+ /** The platform this connector syncs to. */
2204
+ const platform = "multica";
2205
+ const isLive = (live) => live.connector === platform;
2206
+ /** What Multica would refuse in `rendered`, checked before anything is read. */
2207
+ const validate = (rendered) => {
2208
+ const issues = [];
2209
+ for (const agent of rendered.agents) {
2210
+ const size = length(agent.description);
2211
+ if (size > 255) issues.push({
2212
+ field: `agents.${agent.id}.description`,
2213
+ message: `is ${size} characters, over Multica's 255`
2214
+ });
2215
+ }
2216
+ for (const skill of rendered.skills) issues.push(...checkSkill(skill));
2217
+ if (rendered.squad && !rendered.squad.instructions.trim()) issues.push({
2218
+ field: "squad.instructions",
2219
+ message: "renders empty: add squad.md to the setup or the target, or point squad.instructions at a file"
2220
+ });
2221
+ issues.push(...checkWorkflows(rendered, mergeWorkflows(rendered)));
2222
+ return issues;
2223
+ };
2224
+ /** A Multica connector that reads its environment from `options`. */
2225
+ const createConnector = (options = {}) => ({
2226
+ id: platform,
2227
+ capabilities: {
2228
+ sharedContext: true,
2229
+ workflowPerIssue: false,
2230
+ squads: true,
2231
+ skills: true,
2232
+ metrics: MULTICA_METRICS
2233
+ },
2234
+ runFolders: /multica.workspaces/i,
2235
+ columnNames,
2236
+ validate,
2237
+ place: (live) => {
2238
+ if (!isLive(live)) throw new Error("not a Multica connector's live state");
2239
+ return {
2240
+ name: live.workspace.name,
2241
+ pull: {
2242
+ profile: live.profile,
2243
+ workspace: live.workspace.id
2244
+ }
2245
+ };
2246
+ },
2247
+ pull: (target, pullOptions = {}) => pull(target, pullOptions, {
2248
+ env: options.env ?? process.env,
2249
+ ...options.desktop === void 0 ? {} : { desktop: options.desktop }
2250
+ }),
2251
+ plan: (rendered, live) => {
2252
+ if (!isLive(live)) return Promise.reject(/* @__PURE__ */ new Error("not a Multica connector's live state"));
2253
+ try {
2254
+ return Promise.resolve(planChanges(rendered, live));
2255
+ } catch (error) {
2256
+ return Promise.reject(error instanceof Error ? error : new Error(String(error)));
2257
+ }
2258
+ },
2259
+ bench: (query) => bench(query, {
2260
+ env: options.env ?? process.env,
2261
+ ...options.desktop === void 0 ? {} : { desktop: options.desktop }
2262
+ }),
2263
+ apply: (changes) => {
2264
+ try {
2265
+ return Promise.resolve(applyChanges(changes));
2266
+ } catch (error) {
2267
+ return Promise.reject(error instanceof Error ? error : new Error(String(error)));
2268
+ }
2269
+ }
2270
+ });
2271
+ const connector = createConnector();
2272
+ export { BUILT_IN, DESKTOP_CLI, MAX_DESCRIPTION, MAX_STEP, MULTICA_METRICS, benchFrom, childEnv, columnNames, connector, connector as default, createConnector, findMultica, listProfiles, mergeWorkflows, platform, validate };