@voyager-crew/cli 0.4.0 → 0.5.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/cli.mjs CHANGED
@@ -3,10 +3,9 @@
3
3
  // plugins/ask/index.ts
4
4
  import { basename as basename3 } from "node:path";
5
5
 
6
- // src/plugins/api.ts
7
- function definePlugin(plugin) {
8
- return plugin;
9
- }
6
+ // src/cli/presets.ts
7
+ import { readFile as readFile2 } from "node:fs/promises";
8
+ import { resolve } from "node:path";
10
9
 
11
10
  // node_modules/@voyager-crew/bridge-protocol/src/read.ts
12
11
  var ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;
@@ -272,6 +271,7 @@ var POLL_HOLD_MS = 25000;
272
271
  var TASK_ACK_DEADLINE_MS = 1e4;
273
272
  var EXTENSION_OFFLINE_MS = 90000;
274
273
  var MAX_JSON_BODY_BYTES = 1024 * 1024;
274
+ var MAX_INFLIGHT_TASKS = 16;
275
275
  var MAX_BLOB_CHUNK_BYTES = 4 * 1024 * 1024;
276
276
  var EXTENSION_ORIGIN_PATTERNS = [
277
277
  /^chrome-extension:\/\/[a-p]{32}$/,
@@ -948,6 +948,11 @@ function envelopeReader(data) {
948
948
  };
949
949
  }
950
950
  var anyData = (raw) => raw;
951
+ function readMaxTasks(raw) {
952
+ if (typeof raw !== "number" || !Number.isSafeInteger(raw) || raw < 1)
953
+ return 1;
954
+ return Math.min(raw, MAX_INFLIGHT_TASKS);
955
+ }
951
956
  var readOpCapability = (raw, path, issues) => {
952
957
  const rec = readObject(raw, path, issues);
953
958
  return {
@@ -1027,12 +1032,13 @@ var READERS = {
1027
1032
  taskId: requireString(entry, "taskId", itemPath, list, ID),
1028
1033
  state: requireLiteral(entry, "state", itemPath, list, ["acked", "running"])
1029
1034
  };
1030
- }, { max: 8 }),
1035
+ }, { max: MAX_INFLIGHT_TASKS }),
1031
1036
  policy: {
1032
1037
  revision: requireInt(policy, "revision", policyPath, issues),
1033
1038
  state: requireLiteral(policy, "state", policyPath, issues, POLICY_STATES)
1034
1039
  },
1035
- ...rec.account === undefined ? {} : { account: readAccountPlan(rec.account) }
1040
+ ...rec.account === undefined ? {} : { account: readAccountPlan(rec.account) },
1041
+ ...rec.maxTasks === undefined ? {} : { maxTasks: readMaxTasks(rec.maxTasks) }
1036
1042
  };
1037
1043
  },
1038
1044
  pollResponse: (raw, path, issues) => {
@@ -1155,6 +1161,43 @@ function toBridgeError(thrown) {
1155
1161
  return bridgeError("bad-request", `internal error: ${message}`);
1156
1162
  }
1157
1163
 
1164
+ // src/cli/presets.ts
1165
+ var BUILT_IN_PRESETS = {
1166
+ math: "Mathematical research session. This question comes from a long-running mathematics project. Treat it as research: expect a long session and prepare for hours of real work; depth and rigor come before speed. Every conclusion is either derived from the material given, verified, or marked explicitly as a conjecture together with what evidence would settle it. Work from the exact statement below rather than a paraphrase of it."
1167
+ };
1168
+ async function resolvePreset(value, cwd) {
1169
+ if (value === undefined)
1170
+ return "";
1171
+ if (typeof value !== "string") {
1172
+ throw failure("bad-request", "--preset needs a built-in name or a UTF-8 text file");
1173
+ }
1174
+ if (Object.hasOwn(BUILT_IN_PRESETS, value))
1175
+ return BUILT_IN_PRESETS[value];
1176
+ const label = JSON.stringify(value);
1177
+ let text;
1178
+ try {
1179
+ text = await readFile2(resolve(cwd, value), "utf8");
1180
+ } catch {
1181
+ throw failure("bad-request", `--preset ${label}: use a built-in name (math) or a readable UTF-8 text file`);
1182
+ }
1183
+ if (Array.from(text).length > 20000) {
1184
+ throw failure("bad-request", `--preset ${label}: file exceeds 20,000 characters`);
1185
+ }
1186
+ const trimmed = text.trim();
1187
+ if (trimmed === "") {
1188
+ throw failure("bad-request", `--preset ${label}: file is empty after trimming whitespace`);
1189
+ }
1190
+ return trimmed;
1191
+ }
1192
+
1193
+ // src/plugins/api.ts
1194
+ function conversationConflictKey(conversationId) {
1195
+ return `conversation ${conversationId}`;
1196
+ }
1197
+ function definePlugin(plugin) {
1198
+ return plugin;
1199
+ }
1200
+
1158
1201
  // src/plugins/sendGuard.ts
1159
1202
  var SENDING_CHECKPOINT = "sending";
1160
1203
  var BEFORE_CLICK = new Set([
@@ -1192,8 +1235,8 @@ async function sendOnce(ctx, params) {
1192
1235
 
1193
1236
  // src/util/attach/stage.ts
1194
1237
  import { createHash as createHash2 } from "node:crypto";
1195
- import { copyFile, constants as constants2, readFile as readFile4, realpath, stat } from "node:fs/promises";
1196
- import { basename, join as join3, resolve } from "node:path";
1238
+ import { copyFile, constants as constants2, readFile as readFile5, realpath, stat } from "node:fs/promises";
1239
+ import { basename, join as join3, resolve as resolve2 } from "node:path";
1197
1240
 
1198
1241
  // src/util/mime.ts
1199
1242
  var TYPES = {
@@ -1718,17 +1761,17 @@ var Zip = /* @__PURE__ */ function() {
1718
1761
 
1719
1762
  // src/util/attach/zipFolder.ts
1720
1763
  import { createHash } from "node:crypto";
1721
- import { readFile as readFile3 } from "node:fs/promises";
1764
+ import { readFile as readFile4 } from "node:fs/promises";
1722
1765
  import { deflateRawSync } from "node:zlib";
1723
1766
 
1724
1767
  // src/util/files.ts
1725
1768
  import { randomBytes } from "node:crypto";
1726
1769
  import { constants } from "node:fs";
1727
- import { chmod, open, readFile as readFile2, rename, unlink } from "node:fs/promises";
1770
+ import { chmod, open, readFile as readFile3, rename, unlink } from "node:fs/promises";
1728
1771
  import { dirname, join as join2 } from "node:path";
1729
1772
  async function readJsonFile(path) {
1730
1773
  try {
1731
- return JSON.parse(await readFile2(path, "utf8"));
1774
+ return JSON.parse(await readFile3(path, "utf8"));
1732
1775
  } catch (error) {
1733
1776
  if (isErrno(error, "ENOENT"))
1734
1777
  return;
@@ -1836,7 +1879,7 @@ async function zipFiles(files, root, outPath, maxBytes) {
1836
1879
  const tooLarge = () => failure("bad-request", `the zip of ${root} is over ${formatBytes(maxBytes)}; the largest folders are ${largestFolders(files).map((entry) => `${entry.path} (${formatBytes(entry.bytes)})`).join(", ")}`, { limit: maxBytes, largest: largestFolders(files) });
1837
1880
  const sorted = [...files].sort((a, b) => a.relative < b.relative ? -1 : 1);
1838
1881
  for (const file of sorted) {
1839
- const data = await readFile3(file.absolute);
1882
+ const data = await readFile4(file.absolute);
1840
1883
  const entry = new DeflatedEntry(`${root}/${checkEntryPath(file.relative)}`, data.length, crc32(data), deflateRawSync(data, { level: LEVEL }));
1841
1884
  zip.add(entry);
1842
1885
  entry.emit();
@@ -1859,7 +1902,7 @@ async function zipFiles(files, root, outPath, maxBytes) {
1859
1902
  // src/util/attach/stage.ts
1860
1903
  var EXAMPLES = 5;
1861
1904
  async function sha256File(path) {
1862
- return createHash2("sha256").update(await readFile4(path)).digest("hex");
1905
+ return createHash2("sha256").update(await readFile5(path)).digest("hex");
1863
1906
  }
1864
1907
  async function stageFolder(source, taken, options, maxBytes) {
1865
1908
  const folder = await realpath(source);
@@ -1929,7 +1972,7 @@ async function stageAttachments(inputs, options) {
1929
1972
  const taken = new Set;
1930
1973
  const staged = [];
1931
1974
  for (const input of inputs) {
1932
- const source = resolve(options.cwd, input);
1975
+ const source = resolve2(options.cwd, input);
1933
1976
  let stats;
1934
1977
  try {
1935
1978
  stats = await stat(source);
@@ -2281,7 +2324,13 @@ async function loadConfig(paths) {
2281
2324
  const port = isRecord2(raw) ? raw.port : undefined;
2282
2325
  const valid = typeof port === "number" && Number.isInteger(port) && port > 0 && port < 65536;
2283
2326
  const driveRemote = isRecord2(raw) && typeof raw.driveRemote === "string" ? raw.driveRemote.trim() : "";
2284
- return { port: valid ? port : DEFAULT_PORT, ...driveRemote === "" ? {} : { driveRemote } };
2327
+ const maxJobs = isRecord2(raw) ? raw.maxParallelJobs : undefined;
2328
+ const usable2 = typeof maxJobs === "number" && Number.isSafeInteger(maxJobs) && maxJobs >= 1;
2329
+ return {
2330
+ port: valid ? port : DEFAULT_PORT,
2331
+ ...driveRemote === "" ? {} : { driveRemote },
2332
+ ...maxJobs === undefined ? {} : usable2 ? { maxParallelJobs: maxJobs } : { maxParallelJobs: 1, invalidMaxParallelJobs: JSON.stringify(maxJobs) }
2333
+ };
2285
2334
  }
2286
2335
 
2287
2336
  // src/state/paths.ts
@@ -2566,6 +2615,7 @@ async function prepare(fg, argv) {
2566
2615
  const asked = argv.positionals.join(" ").trim();
2567
2616
  if (asked === "")
2568
2617
  throw failure("bad-request", 'usage: voyager ask "<prompt>"');
2618
+ const preset = await resolvePreset(argv.options.preset, fg.cwd);
2569
2619
  const model = parseAskModel(argv.options.model);
2570
2620
  const project = typeof argv.options.project === "string" ? argv.options.project.trim() : undefined;
2571
2621
  if (project !== undefined) {
@@ -2588,6 +2638,7 @@ async function prepare(fg, argv) {
2588
2638
  fg.note(`repo: ChatGPT is asked to write only to ${repo.branch}`);
2589
2639
  const timebox = parseTimebox(argv.options.timebox);
2590
2640
  const prompt = [
2641
+ preset,
2591
2642
  asked,
2592
2643
  drive.block,
2593
2644
  repo === undefined ? "" : repoPromptBlock(repo),
@@ -2645,14 +2696,12 @@ async function run2(ctx, input) {
2645
2696
  const shown = await callProjectOp(ctx, "project.open", { projectId, view: "chat" });
2646
2697
  await ctx.checkpoint("project", { projectId, url: shown.url });
2647
2698
  }
2648
- const model = await chooseModel(ctx, input.model ?? DEFAULT_ASK_MODEL);
2649
2699
  const result = await askInTab(ctx, input, {
2650
2700
  startedAt,
2651
2701
  blobs,
2652
2702
  attachments,
2653
2703
  existing,
2654
- opened,
2655
- model
2704
+ opened
2656
2705
  });
2657
2706
  if (input.repo !== undefined)
2658
2707
  checkRepoReach(input.repo, result);
@@ -2664,25 +2713,28 @@ async function askInTab(ctx, input, {
2664
2713
  blobs,
2665
2714
  attachments,
2666
2715
  existing,
2667
- opened,
2668
- model
2716
+ opened
2669
2717
  }) {
2670
- if (blobs.length > 0) {
2671
- const bytes = blobs.reduce((sum, blob) => sum + blob.size, 0);
2672
- const attached = await ctx.ops.call("composer.attach", { blobs }, { timeoutMs: attachTimeoutMs(bytes) });
2673
- await ctx.checkpoint("attached", {
2674
- names: attached.attached.map((entry) => entry.name),
2675
- bytes
2676
- });
2677
- }
2678
- const preamble = attachmentPreamble(attachments);
2679
- const text = preamble === "" ? input.prompt : `${input.prompt}
2718
+ const { model, sent } = await ctx.ops.exclusive(async () => {
2719
+ const chosen = await chooseModel(ctx, input.model ?? DEFAULT_ASK_MODEL);
2720
+ if (blobs.length > 0) {
2721
+ const bytes = blobs.reduce((sum, blob) => sum + blob.size, 0);
2722
+ const attached = await ctx.ops.call("composer.attach", { blobs }, { timeoutMs: attachTimeoutMs(bytes) });
2723
+ await ctx.checkpoint("attached", {
2724
+ names: attached.attached.map((entry) => entry.name),
2725
+ bytes
2726
+ });
2727
+ }
2728
+ const preamble = attachmentPreamble(attachments);
2729
+ const text = preamble === "" ? input.prompt : `${input.prompt}
2680
2730
 
2681
2731
  ${preamble}`;
2682
- const sent = await sendOnce(ctx, {
2683
- text,
2684
- ...blobs.length > 0 ? { expectAttachments: blobs.map((blob) => blob.name) } : {},
2685
- ...existing === undefined ? {} : { expect: { conversationId: existing } }
2732
+ const confirmed = await sendOnce(ctx, {
2733
+ text,
2734
+ ...blobs.length > 0 ? { expectAttachments: blobs.map((blob) => blob.name) } : {},
2735
+ ...existing === undefined ? {} : { expect: { conversationId: existing } }
2736
+ });
2737
+ return { model: chosen, sent: confirmed };
2686
2738
  });
2687
2739
  const sentAt = Date.now();
2688
2740
  if (existing !== undefined && sent.conversationId !== existing) {
@@ -2754,6 +2806,10 @@ var askPlugin = definePlugin({
2754
2806
  summary: "Ask ChatGPT (a new conversation, or --chat / --continue) and save the reply",
2755
2807
  args: [{ name: "prompt", summary: "the message to send", variadic: true }],
2756
2808
  options: {
2809
+ preset: {
2810
+ type: "string",
2811
+ summary: "prepend a built-in preset (math) or a UTF-8 text file to the prompt"
2812
+ },
2757
2813
  "keep-tab": { type: "boolean", summary: "leave the bridge tab open afterwards" },
2758
2814
  chat: {
2759
2815
  type: "string",
@@ -2804,6 +2860,7 @@ var askPlugin = definePlugin({
2804
2860
  }
2805
2861
  },
2806
2862
  prepare,
2863
+ conflictKey: (input) => input.conversationId === undefined ? undefined : conversationConflictKey(input.conversationId),
2807
2864
  run: run2,
2808
2865
  present: presentResponse
2809
2866
  }
@@ -2824,12 +2881,13 @@ function readFingerprint(raw) {
2824
2881
  };
2825
2882
  }
2826
2883
  async function load(state) {
2827
- let raw;
2828
2884
  try {
2829
- raw = await state.read();
2885
+ return parse(await state.read());
2830
2886
  } catch {
2831
2887
  return {};
2832
2888
  }
2889
+ }
2890
+ function parse(raw) {
2833
2891
  const projects = {};
2834
2892
  if (!isRecord2(raw) || !isRecord2(raw.uploads))
2835
2893
  return projects;
@@ -2851,14 +2909,16 @@ async function fingerprintOf(state, projectId, name) {
2851
2909
  return Object.hasOwn(projects, projectId) && Object.hasOwn(projects[projectId], name) ? projects[projectId][name] : undefined;
2852
2910
  }
2853
2911
  async function rememberUpload(state, projectId, name, fingerprint) {
2854
- const projects = await load(state);
2855
- const files = { ...Object.hasOwn(projects, projectId) ? projects[projectId] : {} };
2856
- if (fingerprint)
2857
- files[name] = fingerprint;
2858
- else
2859
- delete files[name];
2860
- projects[projectId] = files;
2861
- await state.write({ version: 1, uploads: projects });
2912
+ await state.update((raw) => {
2913
+ const projects = parse(raw);
2914
+ const files = { ...Object.hasOwn(projects, projectId) ? projects[projectId] : {} };
2915
+ if (fingerprint)
2916
+ files[name] = fingerprint;
2917
+ else
2918
+ delete files[name];
2919
+ projects[projectId] = files;
2920
+ return { version: 1, uploads: projects };
2921
+ });
2862
2922
  }
2863
2923
 
2864
2924
  // plugins/project/index.ts
@@ -3071,6 +3131,7 @@ var projectPlugin = definePlugin({
3071
3131
  "keep-tab": { type: "boolean", summary: "leave the bridge tab open afterwards" }
3072
3132
  },
3073
3133
  prepare: prepare2,
3134
+ conflictKey: (input) => input.action === "create" || input.action === "upload" || input.action === "remove" ? "project changes" : undefined,
3074
3135
  run: run3,
3075
3136
  present
3076
3137
  }
@@ -3211,7 +3272,7 @@ class PluginRegistry {
3211
3272
  // package.json
3212
3273
  var package_default = {
3213
3274
  name: "@voyager-crew/cli",
3214
- version: "0.4.0",
3275
+ version: "0.5.1",
3215
3276
  description: "Ask ChatGPT from your terminal or your coding agent, through the Voyager browser extension",
3216
3277
  keywords: [
3217
3278
  "chatgpt",
@@ -3258,7 +3319,7 @@ var package_default = {
3258
3319
  },
3259
3320
  devDependencies: {
3260
3321
  "@types/node": "^24.9.0",
3261
- "@voyager-crew/bridge-protocol": "^0.4.0",
3322
+ "@voyager-crew/bridge-protocol": "^0.5.0",
3262
3323
  fflate: "0.8.2",
3263
3324
  oxfmt: "^0.65.0",
3264
3325
  oxlint: "^1.80.0",
@@ -3308,13 +3369,13 @@ async function extensionPluginCommand(argv) {
3308
3369
  return argv.length === 0 ? 2 : 0;
3309
3370
  }
3310
3371
  const npx = process.platform === "win32" ? "npx.cmd" : "npx";
3311
- return new Promise((resolve2) => {
3372
+ return new Promise((resolve3) => {
3312
3373
  const child = spawn(npx, scaffoldArgs(argv), { stdio: "inherit" });
3313
3374
  child.on("error", (error) => {
3314
3375
  note(`voyager plugin: could not run npx (${error.message}); install Node.js 18 or later`);
3315
- resolve2(2);
3376
+ resolve3(2);
3316
3377
  });
3317
- child.on("exit", (code) => resolve2(code ?? 2));
3378
+ child.on("exit", (code) => resolve3(code ?? 2));
3318
3379
  });
3319
3380
  }
3320
3381
 
@@ -3323,7 +3384,7 @@ import { spawn as spawn2 } from "node:child_process";
3323
3384
  import { closeSync, openSync } from "node:fs";
3324
3385
 
3325
3386
  // src/state/tokens.ts
3326
- import { chmod as chmod3, readFile as readFile5 } from "node:fs/promises";
3387
+ import { chmod as chmod3, readFile as readFile6 } from "node:fs/promises";
3327
3388
  async function ensureCliToken(paths) {
3328
3389
  const existing = await readCliToken(paths);
3329
3390
  if (existing !== undefined) {
@@ -3346,7 +3407,7 @@ async function ensureCliToken(paths) {
3346
3407
  }
3347
3408
  async function readCliToken(paths) {
3348
3409
  try {
3349
- const token = (await readFile5(paths.cliToken, "utf8")).trim();
3410
+ const token = (await readFile6(paths.cliToken, "utf8")).trim();
3350
3411
  return token === "" ? undefined : token;
3351
3412
  } catch (error) {
3352
3413
  if (isErrno(error, "ENOENT"))
@@ -3460,7 +3521,7 @@ async function ensureServe(paths) {
3460
3521
  }
3461
3522
  const deadline = Date.now() + START_TIMEOUT_MS;
3462
3523
  while (Date.now() < deadline) {
3463
- await new Promise((resolve2) => setTimeout(resolve2, 100));
3524
+ await new Promise((resolve3) => setTimeout(resolve3, 100));
3464
3525
  const client = await connectServe(paths);
3465
3526
  if (client !== undefined)
3466
3527
  return client;
@@ -3526,7 +3587,7 @@ function parseDuration(input) {
3526
3587
  }
3527
3588
 
3528
3589
  // src/cli/args.ts
3529
- function parse(argv, defs) {
3590
+ function parse2(argv, defs) {
3530
3591
  const options = {};
3531
3592
  for (const [name, def] of Object.entries(defs)) {
3532
3593
  options[name] = {
@@ -3568,7 +3629,7 @@ function waitTimeout(args) {
3568
3629
 
3569
3630
  // src/cli/commands/info.ts
3570
3631
  async function statusCommand(paths, argv) {
3571
- const args = parse(argv, { json: { type: "boolean", summary: "JSON output" } });
3632
+ const args = parse2(argv, { json: { type: "boolean", summary: "JSON output" } });
3572
3633
  const client = await ensureServe(paths);
3573
3634
  const status = await client.request("GET", BRIDGE_PATHS.status);
3574
3635
  if (args.options.json === true) {
@@ -3589,11 +3650,46 @@ async function statusCommand(paths, argv) {
3589
3650
  const age = seenAt === undefined ? "" : `, seen ${Math.max(0, Math.round((Date.now() - seenAt) / 60000))} min ago`;
3590
3651
  printLine(`${describeAccount(ext.account)}${ext.account ? age : ""}`);
3591
3652
  }
3592
- printLine(status.activeJob === undefined ? "active job: none" : `active job: ${status.activeJob.jobId}${status.activeJob.op ? ` (${status.activeJob.op})` : ""}`);
3653
+ for (const line of statusJobLines(status))
3654
+ printLine(line);
3593
3655
  return 0;
3594
3656
  }
3657
+ function statusJobLines(status, now = Date.now()) {
3658
+ const active = status.activeJobs ?? (status.activeJob === undefined ? [] : [status.activeJob]);
3659
+ const turns = active.length > 1;
3660
+ const named = active.map((job) => {
3661
+ const turn = !turns ? undefined : job.pageTurn === "holding" ? "has the page turn" : job.pageTurn === "waiting" ? "waiting for the page turn" : undefined;
3662
+ const notes = [job.op, turn].filter((part) => part !== undefined);
3663
+ return `${job.jobId}${notes.length > 0 ? ` (${notes.join(", ")})` : ""}`;
3664
+ });
3665
+ const label = named.length > 1 ? "active jobs" : "active job";
3666
+ const lines = [`${label}: ${named.length === 0 ? "none" : named.join(", ")}`];
3667
+ const parallel = parallelLine(status, now);
3668
+ if (parallel !== undefined)
3669
+ lines.push(parallel);
3670
+ return lines;
3671
+ }
3672
+ function parallelLine(status, now) {
3673
+ const ext = status.extension;
3674
+ const last = ext.lastPollAt;
3675
+ if (!ext.paired || last === null || now - last > EXTENSION_OFFLINE_MS)
3676
+ return;
3677
+ const offered = ext.maxTasks;
3678
+ const maxJobs = status.maxJobs ?? 1;
3679
+ if (offered === undefined) {
3680
+ return maxJobs > 1 ? `parallel jobs: up to ${maxJobs} at once` : undefined;
3681
+ }
3682
+ if (offered === null) {
3683
+ return `parallel jobs: not in Voyager ${ext.version ?? "?"} (one job at a time; a newer Voyager runs several)`;
3684
+ }
3685
+ if (offered <= 1)
3686
+ return "parallel jobs: off (one job at a time; turn it on in the Voyager popup)";
3687
+ if (maxJobs >= offered)
3688
+ return `parallel jobs: up to ${maxJobs} at once`;
3689
+ return `parallel jobs: up to ${maxJobs} at once (maxParallelJobs in config.json; the Voyager popup allows ${offered})`;
3690
+ }
3595
3691
  async function commandsCommand(paths, argv) {
3596
- const args = parse(argv, { json: { type: "boolean", summary: "JSON output" } });
3692
+ const args = parse2(argv, { json: { type: "boolean", summary: "JSON output" } });
3597
3693
  const client = await ensureServe(paths);
3598
3694
  const listed2 = await client.request("GET", BRIDGE_PATHS.commands);
3599
3695
  if (args.options.json === true) {
@@ -3749,11 +3845,10 @@ async function submitAndWait(client, registry, args, submit) {
3749
3845
  ({ jobId } = await submit());
3750
3846
  } catch (error) {
3751
3847
  if (error instanceof BridgeFailure && error.code === "busy") {
3752
- const active = String(error.error.details?.activeJobId ?? "?");
3753
3848
  if (args.json)
3754
3849
  printJson({ ok: false, error: error.error });
3755
3850
  else
3756
- note(`busy: job ${active} is running. Wait with voyager job wait ${active}; jobs are not queued.`);
3851
+ note(busyNote(error.error));
3757
3852
  return EXIT.busy;
3758
3853
  }
3759
3854
  throw error;
@@ -3769,6 +3864,17 @@ async function submitAndWait(client, registry, args, submit) {
3769
3864
  note(`job ${jobId}`);
3770
3865
  return waitAndPresent(client, registry, jobId, { ...args, account: account2 });
3771
3866
  }
3867
+ function busyNote(error) {
3868
+ const active = String(error.details?.activeJobId ?? "?");
3869
+ const maxJobs = error.details?.maxJobs;
3870
+ if (typeof maxJobs !== "number" || maxJobs <= 1) {
3871
+ return `busy: job ${active} is running. Wait with voyager job wait ${active}; jobs are not queued.`;
3872
+ }
3873
+ const others = error.details?.activeJobIds;
3874
+ const several = error.details?.conflict === undefined && Array.isArray(others) && others.length > 1;
3875
+ const which = several ? ` (running: ${others.join(", ")})` : "";
3876
+ return `busy: ${error.message}. Wait with voyager job wait ${active}${which}.`;
3877
+ }
3772
3878
  async function waitAndPresent(client, registry, jobId, args) {
3773
3879
  const outcome = await waitForJob(client, jobId, args.timeoutMs, (event) => {
3774
3880
  const line = args.json ? undefined : describeEvent(event);
@@ -3790,7 +3896,7 @@ async function waitAndPresent(client, registry, jobId, args) {
3790
3896
  return presentJob(outcome.job, registry, args.json);
3791
3897
  }
3792
3898
  async function callCommand(paths, registry, argv) {
3793
- const args = parse(argv, {
3899
+ const args = parse2(argv, {
3794
3900
  ...WAIT_OPTIONS,
3795
3901
  params: { type: "string", summary: "op params as JSON" }
3796
3902
  });
@@ -3813,7 +3919,7 @@ async function callCommand(paths, registry, argv) {
3813
3919
  }
3814
3920
  async function jobCommand(paths, registry, argv) {
3815
3921
  const [sub, ...rest] = argv;
3816
- const args = parse(rest, WAIT_OPTIONS);
3922
+ const args = parse2(rest, WAIT_OPTIONS);
3817
3923
  const json = args.options.json === true;
3818
3924
  const jobId = args.positionals[0];
3819
3925
  const needId = () => {
@@ -3863,7 +3969,7 @@ var CLI_LOCAL_PATHS = {
3863
3969
 
3864
3970
  // src/cli/commands/pair.ts
3865
3971
  async function pairCommand(paths, argv) {
3866
- const args = parse(argv, {
3972
+ const args = parse2(argv, {
3867
3973
  revoke: { type: "boolean", summary: "forget the paired extension" },
3868
3974
  json: { type: "boolean", summary: "JSON lines on stdout" }
3869
3975
  });
@@ -3890,7 +3996,7 @@ async function pairCommand(paths, argv) {
3890
3996
  note("The code works once and expires in 5 minutes. Waiting…");
3891
3997
  }
3892
3998
  for (;; ) {
3893
- await new Promise((resolve2) => setTimeout(resolve2, 500));
3999
+ await new Promise((resolve3) => setTimeout(resolve3, 500));
3894
4000
  const state = await client.request("GET", CLI_LOCAL_PATHS.pairingCode(code));
3895
4001
  if (state.state === "paired") {
3896
4002
  if (json) {
@@ -3994,7 +4100,7 @@ function terminalConfirm() {
3994
4100
  };
3995
4101
  }
3996
4102
  async function pluginCommand(paths, registry, found, argv) {
3997
- const args = parse(argv, { ...WAIT_OPTIONS, ...found.def.options });
4103
+ const args = parse2(argv, { ...WAIT_OPTIONS, ...found.def.options });
3998
4104
  const confirm = terminalConfirm();
3999
4105
  const fg = {
4000
4106
  cwd: process.cwd(),
@@ -4021,7 +4127,7 @@ async function pluginCommand(paths, registry, found, argv) {
4021
4127
  }
4022
4128
 
4023
4129
  // src/cli/commands/serve.ts
4024
- import { readFile as readFile8 } from "node:fs/promises";
4130
+ import { readFile as readFile9 } from "node:fs/promises";
4025
4131
 
4026
4132
  // src/server/http.ts
4027
4133
  import { createServer } from "node:http";
@@ -4291,7 +4397,7 @@ class IncomingBlobStore {
4291
4397
  }
4292
4398
 
4293
4399
  // src/jobs/jobRunner.ts
4294
- import { join as join11 } from "node:path";
4400
+ import { join as join12 } from "node:path";
4295
4401
 
4296
4402
  // src/server/extensionState.ts
4297
4403
  function policyFailure(state, op) {
@@ -4311,6 +4417,7 @@ class ExtensionState {
4311
4417
  ops = null;
4312
4418
  policy = null;
4313
4419
  account = null;
4420
+ maxTasks = null;
4314
4421
  opNames() {
4315
4422
  return this.ops === null ? null : new Set(this.ops.map((op) => op.name));
4316
4423
  }
@@ -4319,12 +4426,78 @@ class ExtensionState {
4319
4426
  this.ops = null;
4320
4427
  this.policy = null;
4321
4428
  this.account = null;
4429
+ this.maxTasks = null;
4322
4430
  }
4323
4431
  }
4324
4432
 
4325
- // src/jobs/jobStore.ts
4326
- import { appendFile, mkdir as mkdir6, readdir as readdir5, readFile as readFile6, rm as rm4 } from "node:fs/promises";
4433
+ // src/state/pluginState.ts
4434
+ import { mkdir as mkdir6 } from "node:fs/promises";
4327
4435
  import { join as join10 } from "node:path";
4436
+ var PLUGIN_NAME = /^[a-z][a-z0-9-]{0,63}$/;
4437
+ function pluginStateStore(paths, plugin) {
4438
+ if (!PLUGIN_NAME.test(plugin))
4439
+ throw new Error(`not a plugin name: ${plugin}`);
4440
+ const file = join10(paths.pluginState, `${plugin}.json`);
4441
+ return {
4442
+ read: () => readJsonFile(file),
4443
+ write: async (value) => {
4444
+ await mkdir6(paths.pluginState, { recursive: true, mode: 448 });
4445
+ await writeJsonAtomic(file, value);
4446
+ }
4447
+ };
4448
+ }
4449
+ function withSerialUpdates(base, last) {
4450
+ return {
4451
+ read: () => base.read(),
4452
+ write: (value) => base.write(value),
4453
+ update: (change) => {
4454
+ const next = last.current.then(async () => {
4455
+ const current = await base.read().catch(() => {
4456
+ return;
4457
+ });
4458
+ await base.write(change(current));
4459
+ });
4460
+ last.current = next.catch(() => {
4461
+ return;
4462
+ });
4463
+ return next;
4464
+ }
4465
+ };
4466
+ }
4467
+
4468
+ // src/jobs/admission.ts
4469
+ function jobCapacity(extensionMaxTasks, configured) {
4470
+ const extension = extensionMaxTasks ?? 1;
4471
+ return configured === undefined ? extension : Math.max(1, Math.min(extension, configured));
4472
+ }
4473
+ function admissionFailure(running, next, capacity) {
4474
+ const first = running[0];
4475
+ if (first === undefined)
4476
+ return;
4477
+ const details = {
4478
+ activeJobId: first.jobId,
4479
+ activeJobIds: running.map((job) => job.jobId),
4480
+ maxJobs: capacity
4481
+ };
4482
+ if (capacity <= 1)
4483
+ return failure("busy", "another job is running; it is not queued", details);
4484
+ if (next.alone || running.some((job) => job.alone)) {
4485
+ return failure("busy", "voyager call runs only while no other job does; it is not queued", details);
4486
+ }
4487
+ if (running.length >= capacity) {
4488
+ return failure("busy", `${running.length} jobs are running, as many as may run at once; it is not queued`, details);
4489
+ }
4490
+ const key = next.conflictKey;
4491
+ const clash = key === undefined ? undefined : running.find((job) => job.conflictKeys.includes(key));
4492
+ if (clash !== undefined) {
4493
+ return failure("busy", `job ${clash.jobId} is working on ${next.conflictKey}; it is not queued`, { ...details, activeJobId: clash.jobId, conflict: next.conflictKey });
4494
+ }
4495
+ return;
4496
+ }
4497
+
4498
+ // src/jobs/jobStore.ts
4499
+ import { appendFile, mkdir as mkdir7, readdir as readdir5, readFile as readFile7, rm as rm4 } from "node:fs/promises";
4500
+ import { join as join11 } from "node:path";
4328
4501
  var TERMINAL_STATUSES = new Set([
4329
4502
  "succeeded",
4330
4503
  "failed",
@@ -4355,7 +4528,7 @@ class JobStore {
4355
4528
  if (job === undefined || isTerminal(job.status))
4356
4529
  continue;
4357
4530
  await this.loadEvents(jobId);
4358
- await rm4(join10(this.jobDir(jobId), "incoming"), { recursive: true, force: true });
4531
+ await rm4(join11(this.jobDir(jobId), "incoming"), { recursive: true, force: true });
4359
4532
  if (sendInterrupted(job.checkpoints)) {
4360
4533
  const error = sendUnverified("serve stopped before the send was confirmed; the prompt may have been sent", { reason: "serve restarted" });
4361
4534
  this.update(job, { status: "failed", error });
@@ -4385,7 +4558,7 @@ class JobStore {
4385
4558
  this.jobs.set(job.jobId, job);
4386
4559
  this.events.set(job.jobId, []);
4387
4560
  this.enqueue(job.jobId, async () => {
4388
- await mkdir6(this.outputDir(job.jobId), { recursive: true, mode: 448 });
4561
+ await mkdir7(this.outputDir(job.jobId), { recursive: true, mode: 448 });
4389
4562
  });
4390
4563
  this.persist(job);
4391
4564
  return job;
@@ -4402,7 +4575,7 @@ class JobStore {
4402
4575
  this.events.set(jobId, list);
4403
4576
  const event = { seq: (list.at(-1)?.seq ?? 0) + 1, at: this.now(), type, data };
4404
4577
  list.push(event);
4405
- this.enqueue(jobId, () => appendFile(join10(this.jobDir(jobId), "events.jsonl"), `${JSON.stringify(event)}
4578
+ this.enqueue(jobId, () => appendFile(join11(this.jobDir(jobId), "events.jsonl"), `${JSON.stringify(event)}
4406
4579
  `, {
4407
4580
  mode: 384
4408
4581
  }));
@@ -4419,13 +4592,13 @@ class JobStore {
4419
4592
  return ready;
4420
4593
  const set = this.waiters.get(jobId) ?? new Set;
4421
4594
  this.waiters.set(jobId, set);
4422
- return new Promise((resolve2) => {
4595
+ return new Promise((resolve3) => {
4423
4596
  const waiter = {
4424
4597
  after,
4425
4598
  wake: (events) => {
4426
4599
  clearTimeout(timer);
4427
4600
  set.delete(waiter);
4428
- resolve2(events);
4601
+ resolve3(events);
4429
4602
  }
4430
4603
  };
4431
4604
  const timer = setTimeout(() => waiter.wake([]), Math.min(waitMs, MAX_WAIT_MS));
@@ -4451,10 +4624,10 @@ class JobStore {
4451
4624
  };
4452
4625
  }
4453
4626
  jobDir(jobId) {
4454
- return join10(this.paths.jobs, jobId);
4627
+ return join11(this.paths.jobs, jobId);
4455
4628
  }
4456
4629
  outputDir(jobId) {
4457
- return join10(this.jobDir(jobId), "output");
4630
+ return join11(this.jobDir(jobId), "output");
4458
4631
  }
4459
4632
  async flush(jobId) {
4460
4633
  const ids = jobId === undefined ? [...this.writes.keys()] : [jobId];
@@ -4476,10 +4649,10 @@ class JobStore {
4476
4649
  }
4477
4650
  persist(job) {
4478
4651
  const snapshot = JSON.parse(JSON.stringify(job));
4479
- this.enqueue(job.jobId, () => writeJsonAtomic(join10(this.jobDir(job.jobId), "job.json"), snapshot));
4652
+ this.enqueue(job.jobId, () => writeJsonAtomic(join11(this.jobDir(job.jobId), "job.json"), snapshot));
4480
4653
  }
4481
4654
  enqueue(jobId, write) {
4482
- const previous = this.writes.get(jobId) ?? mkdir6(this.jobDir(jobId), { recursive: true, mode: 448 }).then(() => {
4655
+ const previous = this.writes.get(jobId) ?? mkdir7(this.jobDir(jobId), { recursive: true, mode: 448 }).then(() => {
4483
4656
  return;
4484
4657
  });
4485
4658
  const next = previous.then(write).then(() => {
@@ -4505,7 +4678,7 @@ class JobStore {
4505
4678
  async load(jobId) {
4506
4679
  if (!/^[A-Za-z0-9_-]{1,128}$/.test(jobId))
4507
4680
  return;
4508
- const raw = await readJsonFile(join10(this.jobDir(jobId), "job.json"));
4681
+ const raw = await readJsonFile(join11(this.jobDir(jobId), "job.json"));
4509
4682
  if (!isRecord2(raw) || raw.jobId !== jobId || typeof raw.status !== "string")
4510
4683
  return;
4511
4684
  const job = raw;
@@ -4519,7 +4692,7 @@ class JobStore {
4519
4692
  return cached;
4520
4693
  let text = "";
4521
4694
  try {
4522
- text = await readFile6(join10(this.jobDir(jobId), "events.jsonl"), "utf8");
4695
+ text = await readFile7(join11(this.jobDir(jobId), "events.jsonl"), "utf8");
4523
4696
  } catch (error) {
4524
4697
  if (!isErrno(error, "ENOENT"))
4525
4698
  throw error;
@@ -4548,6 +4721,154 @@ function summarize(job) {
4548
4721
  };
4549
4722
  }
4550
4723
 
4724
+ // src/jobs/pageTurns.ts
4725
+ var PASSIVE_OPS = new Set([
4726
+ "model.inspect",
4727
+ "turns.waitReply",
4728
+ "turns.read",
4729
+ "files.download",
4730
+ "project.list",
4731
+ "catalog.refresh"
4732
+ ]);
4733
+ function takesPageTurn(op) {
4734
+ return !PASSIVE_OPS.has(op);
4735
+ }
4736
+ var PAGE_TURN_GRACE_MS = 60000;
4737
+ var CLEANUP_TURN_WAIT_MS = 60000;
4738
+
4739
+ class PageTurns {
4740
+ holder;
4741
+ depth = 0;
4742
+ queue = [];
4743
+ holderDue = 0;
4744
+ holderOp;
4745
+ overdueTimer;
4746
+ graceMs;
4747
+ cleanupWaitMs;
4748
+ constructor(options = {}) {
4749
+ this.graceMs = options.graceMs ?? PAGE_TURN_GRACE_MS;
4750
+ this.cleanupWaitMs = options.cleanupWaitMs ?? CLEANUP_TURN_WAIT_MS;
4751
+ }
4752
+ async take(jobId, options = {}) {
4753
+ const { signal } = options;
4754
+ if (signal?.aborted)
4755
+ throw failure("cancelled", "job was cancelled");
4756
+ if (this.holder === undefined || this.holder === jobId)
4757
+ this.grant(jobId);
4758
+ else
4759
+ await this.wait(jobId, signal, options.cleanup === true);
4760
+ let given = false;
4761
+ return () => {
4762
+ if (given)
4763
+ return;
4764
+ given = true;
4765
+ this.giveBack();
4766
+ };
4767
+ }
4768
+ opStarted(jobId, op, deadlineMs) {
4769
+ if (this.holder !== jobId)
4770
+ return;
4771
+ this.holderOp = op;
4772
+ this.due(deadlineMs + this.graceMs);
4773
+ }
4774
+ opEnded(jobId) {
4775
+ if (this.holder !== jobId)
4776
+ return;
4777
+ this.holderOp = undefined;
4778
+ this.due(this.graceMs);
4779
+ }
4780
+ current() {
4781
+ return this.holder;
4782
+ }
4783
+ waiting() {
4784
+ return this.queue.map((waiter) => waiter.jobId);
4785
+ }
4786
+ wait(jobId, signal, cleanup) {
4787
+ if (Date.now() >= this.holderDue)
4788
+ return Promise.reject(this.overdueFailure());
4789
+ return new Promise((resolve3, reject) => {
4790
+ const leave = () => {
4791
+ clearTimeout(cleanupTimer);
4792
+ signal?.removeEventListener("abort", onAbort);
4793
+ const index = this.queue.indexOf(waiter);
4794
+ if (index < 0)
4795
+ return false;
4796
+ this.queue.splice(index, 1);
4797
+ return true;
4798
+ };
4799
+ const waiter = {
4800
+ jobId,
4801
+ grant: () => {
4802
+ leave();
4803
+ resolve3();
4804
+ },
4805
+ fail: (error) => {
4806
+ if (leave())
4807
+ reject(error);
4808
+ }
4809
+ };
4810
+ const onAbort = () => waiter.fail(failure("cancelled", "job was cancelled"));
4811
+ const cleanupTimer = cleanup ? setTimeout(() => waiter.fail(this.overdueFailure(this.cleanupWaitMs)), this.cleanupWaitMs) : undefined;
4812
+ this.queue.push(waiter);
4813
+ signal?.addEventListener("abort", onAbort, { once: true });
4814
+ this.armOverdue();
4815
+ });
4816
+ }
4817
+ due(fromNowMs) {
4818
+ this.holderDue = Date.now() + fromNowMs;
4819
+ this.armOverdue();
4820
+ }
4821
+ armOverdue() {
4822
+ clearTimeout(this.overdueTimer);
4823
+ this.overdueTimer = undefined;
4824
+ if (this.holder === undefined || this.queue.length === 0)
4825
+ return;
4826
+ this.overdueTimer = setTimeout(() => {
4827
+ if (Date.now() < this.holderDue) {
4828
+ this.armOverdue();
4829
+ return;
4830
+ }
4831
+ const error = this.overdueFailure();
4832
+ for (const waiter of this.queue.slice())
4833
+ waiter.fail(error);
4834
+ }, Math.max(0, this.holderDue - Date.now()));
4835
+ }
4836
+ overdueFailure(cleanupWaitMs) {
4837
+ const holder = this.holder ?? "?";
4838
+ const step = this.holderOp === undefined ? "between its steps" : `in ${this.holderOp}`;
4839
+ const why = cleanupWaitMs === undefined ? `job ${holder} has held the ChatGPT page past the time it may take (${step})` : `job ${holder} still holds the ChatGPT page (${step}) after ${Math.round(cleanupWaitMs / 1000)} s`;
4840
+ return failure("busy", `${why}; nothing was sent for this step`, {
4841
+ activeJobId: this.holder,
4842
+ pageTurn: true,
4843
+ ...this.holderOp === undefined ? {} : { holderOp: this.holderOp }
4844
+ });
4845
+ }
4846
+ grant(jobId) {
4847
+ if (this.holder !== jobId) {
4848
+ this.holder = jobId;
4849
+ this.holderOp = undefined;
4850
+ this.due(this.graceMs);
4851
+ }
4852
+ this.depth += 1;
4853
+ }
4854
+ giveBack() {
4855
+ this.depth -= 1;
4856
+ if (this.depth > 0)
4857
+ return;
4858
+ this.holder = undefined;
4859
+ this.holderOp = undefined;
4860
+ const next = this.queue[0];
4861
+ if (next === undefined) {
4862
+ clearTimeout(this.overdueTimer);
4863
+ this.overdueTimer = undefined;
4864
+ return;
4865
+ }
4866
+ this.grant(next.jobId);
4867
+ next.grant();
4868
+ this.armOverdue();
4869
+ }
4870
+ }
4871
+
4551
4872
  // src/jobs/opsClient.ts
4552
4873
  var DEFAULT_OP_TIMEOUT_MS = {
4553
4874
  "conversation.open": 60000,
@@ -4577,7 +4898,18 @@ function defaultOpTimeout(op, params) {
4577
4898
  return DEFAULT_OP_TIMEOUT_MS[op];
4578
4899
  }
4579
4900
  function createOpsClient(hooks) {
4901
+ const { jobId, signal, slot, turns } = hooks;
4902
+ const inTurn = async (work, cleanup = false) => {
4903
+ const giveBack = await turns.take(jobId, { signal, cleanup });
4904
+ try {
4905
+ return await work();
4906
+ } finally {
4907
+ await slot.idle(jobId);
4908
+ giveBack();
4909
+ }
4910
+ };
4580
4911
  return {
4912
+ exclusive: (work) => inTurn(work),
4581
4913
  async call(op, params, options = {}) {
4582
4914
  if (hooks.signal.aborted)
4583
4915
  throw failure("cancelled", "job was cancelled");
@@ -4594,20 +4926,26 @@ function createOpsClient(hooks) {
4594
4926
  if (!checked.ok) {
4595
4927
  throw failure("bad-request", `invalid params for ${op}`, { issues: checked.issues });
4596
4928
  }
4597
- hooks.onStart(op);
4598
- try {
4599
- const result = await hooks.slot.run({
4600
- jobId: hooks.jobId,
4601
- op,
4602
- params: checked.value,
4603
- deadlineMs: options.timeoutMs ?? defaultOpTimeout(op, checked.value),
4604
- signal: hooks.signal,
4605
- onProgress: (event) => hooks.onProgress(op, event)
4606
- });
4607
- return result;
4608
- } finally {
4609
- hooks.onEnd(op);
4610
- }
4929
+ const deadlineMs = options.timeoutMs ?? defaultOpTimeout(op, checked.value);
4930
+ const run5 = async () => {
4931
+ hooks.onStart(op);
4932
+ turns.opStarted(jobId, op, deadlineMs);
4933
+ try {
4934
+ const result = await slot.run({
4935
+ jobId,
4936
+ op,
4937
+ params: checked.value,
4938
+ deadlineMs,
4939
+ signal,
4940
+ onProgress: (event) => hooks.onProgress(op, event)
4941
+ });
4942
+ return result;
4943
+ } finally {
4944
+ turns.opEnded(jobId);
4945
+ hooks.onEnd(op);
4946
+ }
4947
+ };
4948
+ return takesPageTurn(op) ? inTurn(run5, op === "conversation.close") : run5();
4611
4949
  }
4612
4950
  };
4613
4951
  }
@@ -4615,18 +4953,27 @@ function createOpsClient(hooks) {
4615
4953
  // src/jobs/jobRunner.ts
4616
4954
  class JobRunner {
4617
4955
  deps;
4618
- active;
4956
+ active = new Map;
4619
4957
  memoryState = new Map;
4958
+ turns;
4959
+ stateUpdates = new Map;
4620
4960
  constructor(deps) {
4621
4961
  this.deps = deps;
4962
+ this.turns = new PageTurns(deps.pageTurns);
4622
4963
  }
4623
- submit(plugin, command, input) {
4624
- if (this.active !== undefined) {
4625
- throw failure("busy", "another job is running; it is not queued", {
4626
- activeJobId: this.active.job.jobId
4627
- });
4628
- }
4964
+ submit(plugin, command, input, options = {}) {
4629
4965
  const def = this.deps.registry.command(plugin, command);
4966
+ const running = [...this.active.values()].map((entry) => ({
4967
+ jobId: entry.job.jobId,
4968
+ alone: entry.alone,
4969
+ conflictKeys: [...entry.conflictKeys]
4970
+ }));
4971
+ const alone = plugin === CALL_PLUGIN;
4972
+ const conflictKey = conflictKeyOf(def, input);
4973
+ const capacity = jobCapacity(this.deps.extension.maxTasks, options.maxParallelJobs);
4974
+ const busy = admissionFailure(running, { alone, conflictKey }, capacity);
4975
+ if (busy !== undefined)
4976
+ throw busy;
4630
4977
  if (def === undefined)
4631
4978
  throw failure("bad-request", `unknown command ${plugin} ${command}`);
4632
4979
  if (!this.deps.isPaired()) {
@@ -4645,31 +4992,40 @@ class JobRunner {
4645
4992
  const job = this.deps.store.create(plugin, command, input);
4646
4993
  const controller = new AbortController;
4647
4994
  this.deps.store.appendEvent(job.jobId, "status", { status: "running" });
4995
+ const conflictKeys = new Set(conflictKey === undefined ? [] : [conflictKey]);
4648
4996
  const done = this.execute(job, def, controller);
4649
- this.active = { job, controller, done };
4997
+ this.active.set(job.jobId, { job, controller, done, alone, conflictKeys });
4650
4998
  return job;
4651
4999
  }
4652
- activeJob() {
4653
- if (this.active === undefined)
4654
- return;
4655
- const op = this.deps.slot.current()?.op;
4656
- return op === undefined ? { jobId: this.active.job.jobId } : { jobId: this.active.job.jobId, op };
5000
+ activeJobs() {
5001
+ const holder = this.turns.current();
5002
+ const waiting = new Set(this.turns.waiting());
5003
+ return [...this.active.keys()].map((jobId) => {
5004
+ const op = this.deps.slot.taskOf(jobId)?.op;
5005
+ const pageTurn = holder === jobId ? "holding" : waiting.has(jobId) ? "waiting" : undefined;
5006
+ return {
5007
+ jobId,
5008
+ ...op === undefined ? {} : { op },
5009
+ ...pageTurn === undefined ? {} : { pageTurn }
5010
+ };
5011
+ });
4657
5012
  }
4658
5013
  async cancel(jobId) {
4659
5014
  const job = await this.deps.store.get(jobId);
4660
5015
  if (job === undefined)
4661
5016
  throw failure("not-found", `no job ${jobId}`);
4662
- if (this.active?.job !== job) {
5017
+ const active = this.active.get(jobId);
5018
+ if (active?.job !== job) {
4663
5019
  throw failure("gone", `job ${jobId} has already ended (${job.status})`);
4664
5020
  }
4665
5021
  if (job.status !== "cancelling") {
4666
5022
  this.setStatus(job, "cancelling");
4667
- this.active.controller.abort();
5023
+ active.controller.abort();
4668
5024
  }
4669
5025
  return job;
4670
5026
  }
4671
5027
  async settled() {
4672
- await this.active?.done;
5028
+ await Promise.all([...this.active.values()].map((entry) => entry.done));
4673
5029
  }
4674
5030
  async execute(job, def, controller) {
4675
5031
  await Promise.resolve();
@@ -4689,7 +5045,7 @@ class JobRunner {
4689
5045
  }
4690
5046
  status = statusForError(error, job.checkpoints);
4691
5047
  }
4692
- await this.deps.slot.idle();
5048
+ await this.deps.slot.idle(job.jobId);
4693
5049
  this.deps.blobs.forgetJob(job.jobId);
4694
5050
  await this.deps.incoming.forgetJob(job.jobId).catch((error2) => {
4695
5051
  this.deps.log.warn("could not delete unsaved downloads", {
@@ -4711,7 +5067,7 @@ class JobRunner {
4711
5067
  ...error === undefined ? {} : { error }
4712
5068
  });
4713
5069
  this.deps.log.info("job finished", { jobId: job.jobId, status, code: error?.code });
4714
- this.active = undefined;
5070
+ this.active.delete(job.jobId);
4715
5071
  }
4716
5072
  context(job, controller) {
4717
5073
  const { store, slot, extension, log } = this.deps;
@@ -4719,6 +5075,7 @@ class JobRunner {
4719
5075
  jobId: job.jobId,
4720
5076
  signal: controller.signal,
4721
5077
  slot,
5078
+ turns: this.turns,
4722
5079
  available: () => extension.opNames(),
4723
5080
  policy: () => extension.policy?.state,
4724
5081
  onStart: (op) => {
@@ -4738,10 +5095,13 @@ class JobRunner {
4738
5095
  ops: ops2,
4739
5096
  blobs: {
4740
5097
  register: (path, options) => this.deps.blobs.register(job.jobId, path, options),
4741
- save: (blob, name) => this.deps.incoming.take(job.jobId, blob, join11(store.outputDir(job.jobId), "files"), name)
5098
+ save: (blob, name) => this.deps.incoming.take(job.jobId, blob, join12(store.outputDir(job.jobId), "files"), name)
4742
5099
  },
4743
5100
  progress: (event) => store.appendEvent(job.jobId, "progress", event),
4744
5101
  checkpoint: async (name, data) => {
5102
+ if (typeof data.conversationId === "string") {
5103
+ this.active.get(job.jobId)?.conflictKeys.add(conversationConflictKey(data.conversationId));
5104
+ }
4745
5105
  job.checkpoints[name] = data;
4746
5106
  store.update(job, {});
4747
5107
  store.appendEvent(job.jobId, "checkpoint", { name, data });
@@ -4753,10 +5113,7 @@ class JobRunner {
4753
5113
  });
4754
5114
  }
4755
5115
  },
4756
- state: this.deps.pluginState?.(job.plugin) ?? {
4757
- read: async () => this.memoryState.get(job.plugin),
4758
- write: async (value) => void this.memoryState.set(job.plugin, value)
4759
- },
5116
+ state: this.pluginState(job.plugin),
4760
5117
  log: {
4761
5118
  info: (message, fields) => {
4762
5119
  store.appendEvent(job.jobId, "log", { level: "info", message, ...fields });
@@ -4769,6 +5126,18 @@ class JobRunner {
4769
5126
  }
4770
5127
  };
4771
5128
  }
5129
+ pluginState(plugin) {
5130
+ const base = this.deps.pluginState?.(plugin) ?? {
5131
+ read: async () => this.memoryState.get(plugin),
5132
+ write: async (value) => void this.memoryState.set(plugin, value)
5133
+ };
5134
+ let last = this.stateUpdates.get(plugin);
5135
+ if (last === undefined) {
5136
+ last = { current: Promise.resolve() };
5137
+ this.stateUpdates.set(plugin, last);
5138
+ }
5139
+ return withSerialUpdates(base, last);
5140
+ }
4772
5141
  async remember(job, result) {
4773
5142
  const conversationId = result?.conversationId;
4774
5143
  if (this.deps.rememberConversation === undefined || typeof conversationId !== "string")
@@ -4796,6 +5165,13 @@ class JobRunner {
4796
5165
  this.deps.store.appendEvent(job.jobId, "status", { status });
4797
5166
  }
4798
5167
  }
5168
+ function conflictKeyOf(def, input) {
5169
+ try {
5170
+ return def?.conflictKey?.(input);
5171
+ } catch {
5172
+ return;
5173
+ }
5174
+ }
4799
5175
  function statusForError(error, checkpoints) {
4800
5176
  if (error.code === "cancelled")
4801
5177
  return "cancelled";
@@ -4812,10 +5188,12 @@ function statusForError(error, checkpoints) {
4812
5188
  var FINISHED_MEMORY = 64;
4813
5189
 
4814
5190
  class TaskSlot {
4815
- entry;
5191
+ entries = new Map;
4816
5192
  finished = new Map;
4817
5193
  idleWaiters = [];
4818
5194
  listeners = new Set;
5195
+ polling = false;
5196
+ changePending = false;
4819
5197
  offlineTimer;
4820
5198
  now;
4821
5199
  ackDeadlineMs;
@@ -4830,12 +5208,12 @@ class TaskSlot {
4830
5208
  this.abortResendMs = options.abortResendMs ?? 2000;
4831
5209
  }
4832
5210
  async run(input) {
4833
- await this.idle();
5211
+ await this.idle(input.jobId);
4834
5212
  if (input.signal?.aborted)
4835
5213
  throw failure("cancelled", "job was cancelled");
4836
- if (this.entry !== undefined)
5214
+ if (this.taskOf(input.jobId) !== undefined)
4837
5215
  return this.run(input);
4838
- return new Promise((resolve2, reject) => {
5216
+ return new Promise((resolve3, reject) => {
4839
5217
  const entry = {
4840
5218
  task: {
4841
5219
  taskId: newId("t"),
@@ -4852,11 +5230,11 @@ class TaskSlot {
4852
5230
  abortSentAt: 0,
4853
5231
  lastSeq: -1,
4854
5232
  settled: false,
4855
- resolve: resolve2,
5233
+ resolve: resolve3,
4856
5234
  reject,
4857
5235
  ...input.onProgress === undefined ? {} : { onProgress: input.onProgress }
4858
5236
  };
4859
- this.entry = entry;
5237
+ this.entries.set(entry.task.taskId, entry);
4860
5238
  this.armOffline();
4861
5239
  input.signal?.addEventListener("abort", () => this.abandon(entry, "cancelled"), {
4862
5240
  once: true
@@ -4864,76 +5242,87 @@ class TaskSlot {
4864
5242
  this.changed();
4865
5243
  });
4866
5244
  }
4867
- current() {
4868
- const entry = this.entry;
4869
- return entry && { taskId: entry.task.taskId, jobId: entry.task.jobId, op: entry.task.op };
5245
+ tasks() {
5246
+ return [...this.entries.values()].map(view);
5247
+ }
5248
+ taskOf(jobId) {
5249
+ const entry = [...this.entries.values()].find((item) => item.task.jobId === jobId);
5250
+ return entry && view(entry);
4870
5251
  }
4871
- idle() {
4872
- if (this.entry === undefined)
5252
+ idle(jobId) {
5253
+ if (this.isIdle(jobId))
4873
5254
  return Promise.resolve();
4874
- return new Promise((resolve2) => this.idleWaiters.push(resolve2));
5255
+ return new Promise((wake) => this.idleWaiters.push({ jobId, wake }));
4875
5256
  }
4876
5257
  onChange(listener) {
4877
5258
  this.listeners.add(listener);
4878
5259
  return () => this.listeners.delete(listener);
4879
5260
  }
4880
5261
  notePoll() {
4881
- if (this.entry !== undefined)
5262
+ if (this.entries.size > 0)
4882
5263
  this.armOffline();
4883
5264
  }
4884
5265
  poll(view) {
4885
- const entry = this.entry;
4886
- if (entry === undefined)
4887
- return;
5266
+ this.polling = true;
5267
+ try {
5268
+ const waiting = [...this.entries.values()].filter((entry) => this.settleByPoll(entry, view));
5269
+ const abort = waiting.find((entry) => entry.abandoned !== undefined);
5270
+ if (abort !== undefined)
5271
+ return this.sendAbort(abort);
5272
+ const next = waiting.find((entry) => entry.state === "queued" || entry.state === "dispatched");
5273
+ return next === undefined ? undefined : this.dispatch(next);
5274
+ } finally {
5275
+ this.polling = false;
5276
+ if (this.changePending) {
5277
+ this.changePending = false;
5278
+ queueMicrotask(() => this.changed());
5279
+ }
5280
+ }
5281
+ }
5282
+ settleByPoll(entry, view) {
4888
5283
  const { taskId } = entry.task;
4889
5284
  const listed2 = view.inflight.has(taskId);
4890
5285
  if (entry.abandoned !== undefined) {
4891
- if (!entry.delivered) {
5286
+ if (!entry.delivered || entry.abortDelivered && !listed2) {
4892
5287
  this.release(entry);
4893
- return;
4894
- }
4895
- if (entry.abortDelivered && !listed2) {
4896
- this.release(entry);
4897
- return;
5288
+ return false;
4898
5289
  }
4899
- if (!entry.abortDelivered || this.now() - entry.abortSentAt >= this.abortResendMs) {
4900
- entry.abortDelivered = true;
4901
- entry.abortSentAt = this.now();
4902
- clearTimeout(entry.resendTimer);
4903
- entry.resendTimer = setTimeout(() => this.changed(), this.abortResendMs);
4904
- return { kind: "abort", taskId, reason: entry.abandoned };
4905
- }
4906
- return;
5290
+ return !entry.abortDelivered || this.now() - entry.abortSentAt >= this.abortResendMs;
4907
5291
  }
4908
5292
  if ((entry.state === "acked" || entry.state === "running") && !listed2) {
4909
5293
  this.settle(entry, failure("extension-reloaded", `extension lost task ${taskId} (${entry.task.op})`));
4910
5294
  this.release(entry);
4911
- return;
5295
+ return false;
4912
5296
  }
4913
5297
  if (entry.state === "dispatched" && listed2) {
4914
5298
  entry.state = "acked";
4915
5299
  clearTimeout(entry.ackTimer);
4916
- return;
5300
+ return false;
4917
5301
  }
4918
5302
  if (entry.state === "queued") {
4919
5303
  const refused = policyFailure(view.policy, entry.task.op);
4920
5304
  if (refused !== undefined) {
4921
5305
  this.settle(entry, refused);
4922
5306
  this.release(entry);
4923
- return;
5307
+ return false;
4924
5308
  }
4925
5309
  if (view.ops !== null && !view.ops.has(entry.task.op)) {
4926
5310
  this.settle(entry, failure("unsupported-op", `the extension does not offer ${entry.task.op}`, {
4927
5311
  available: [...view.ops]
4928
5312
  }));
4929
5313
  this.release(entry);
4930
- return;
5314
+ return false;
4931
5315
  }
4932
- return this.dispatch(entry);
5316
+ return true;
4933
5317
  }
4934
- if (entry.state === "dispatched")
4935
- return this.dispatch(entry);
4936
- return;
5318
+ return entry.state === "dispatched";
5319
+ }
5320
+ sendAbort(entry) {
5321
+ entry.abortDelivered = true;
5322
+ entry.abortSentAt = this.now();
5323
+ clearTimeout(entry.resendTimer);
5324
+ entry.resendTimer = setTimeout(() => this.changed(), this.abortResendMs);
5325
+ return { kind: "abort", taskId: entry.task.taskId, reason: entry.abandoned ?? "cancelled" };
4937
5326
  }
4938
5327
  ack(taskId) {
4939
5328
  const entry = this.match(taskId);
@@ -4992,13 +5381,8 @@ class TaskSlot {
4992
5381
  this.release(entry);
4993
5382
  return reply;
4994
5383
  }
4995
- abandonCurrent(reason) {
4996
- if (this.entry !== undefined)
4997
- this.abandon(this.entry, reason);
4998
- }
4999
5384
  close() {
5000
- const entry = this.entry;
5001
- if (entry !== undefined) {
5385
+ for (const entry of this.entries.values()) {
5002
5386
  this.settle(entry, failure("server-unreachable", "voyager serve stopped"));
5003
5387
  this.release(entry);
5004
5388
  }
@@ -5012,7 +5396,7 @@ class TaskSlot {
5012
5396
  entry.state = "dispatched";
5013
5397
  clearTimeout(entry.ackTimer);
5014
5398
  entry.ackTimer = setTimeout(() => {
5015
- if (this.entry === entry && entry.state === "dispatched" && entry.abandoned === undefined) {
5399
+ if (this.holds(entry) && entry.state === "dispatched" && entry.abandoned === undefined) {
5016
5400
  entry.state = "queued";
5017
5401
  this.changed();
5018
5402
  }
@@ -5020,7 +5404,7 @@ class TaskSlot {
5020
5404
  return { kind: "task", task: { ...entry.task } };
5021
5405
  }
5022
5406
  abandon(entry, reason) {
5023
- if (this.entry !== entry || entry.abandoned !== undefined)
5407
+ if (!this.holds(entry) || entry.abandoned !== undefined)
5024
5408
  return;
5025
5409
  const message = reason === "timeout" ? `${entry.task.op} timed out` : "job was cancelled";
5026
5410
  this.settle(entry, failure(reason, message));
@@ -5036,11 +5420,10 @@ class TaskSlot {
5036
5420
  armOffline() {
5037
5421
  clearTimeout(this.offlineTimer);
5038
5422
  this.offlineTimer = setTimeout(() => {
5039
- const entry = this.entry;
5040
- if (entry === undefined)
5041
- return;
5042
- this.settle(entry, failure("extension-offline", `no poll from the extension for ${this.extensionOfflineMs / 1000}s`));
5043
- this.release(entry);
5423
+ for (const entry of this.entries.values()) {
5424
+ this.settle(entry, failure("extension-offline", `no poll from the extension for ${this.extensionOfflineMs / 1000}s`));
5425
+ this.release(entry);
5426
+ }
5044
5427
  }, this.extensionOfflineMs);
5045
5428
  }
5046
5429
  settle(entry, error, value) {
@@ -5053,13 +5436,16 @@ class TaskSlot {
5053
5436
  entry.resolve(value);
5054
5437
  }
5055
5438
  release(entry) {
5056
- if (this.entry !== entry)
5439
+ if (!this.holds(entry))
5057
5440
  return;
5058
5441
  clearTimeout(entry.ackTimer);
5059
5442
  clearTimeout(entry.deadlineTimer);
5060
5443
  clearTimeout(entry.resendTimer);
5061
- clearTimeout(this.offlineTimer);
5062
- this.offlineTimer = undefined;
5444
+ this.entries.delete(entry.task.taskId);
5445
+ if (this.entries.size === 0) {
5446
+ clearTimeout(this.offlineTimer);
5447
+ this.offlineTimer = undefined;
5448
+ }
5063
5449
  this.finished.set(entry.task.taskId, entry.abandoned === undefined ? "done" : "abandoned");
5064
5450
  while (this.finished.size > FINISHED_MEMORY) {
5065
5451
  const oldest = this.finished.keys().next().value;
@@ -5067,38 +5453,37 @@ class TaskSlot {
5067
5453
  break;
5068
5454
  this.finished.delete(oldest);
5069
5455
  }
5070
- this.entry = undefined;
5071
- for (const wake of this.idleWaiters.splice(0))
5072
- wake();
5456
+ for (const waiter of this.idleWaiters.filter((item) => this.isIdle(item.jobId))) {
5457
+ this.idleWaiters.splice(this.idleWaiters.indexOf(waiter), 1);
5458
+ waiter.wake();
5459
+ }
5073
5460
  this.changed();
5074
5461
  }
5462
+ holds(entry) {
5463
+ return this.entries.get(entry.task.taskId) === entry;
5464
+ }
5465
+ isIdle(jobId) {
5466
+ if (jobId === undefined)
5467
+ return this.entries.size === 0;
5468
+ return [...this.entries.values()].every((entry) => entry.task.jobId !== jobId);
5469
+ }
5075
5470
  match(taskId) {
5076
- return this.entry?.task.taskId === taskId ? this.entry : undefined;
5471
+ return this.entries.get(taskId);
5077
5472
  }
5078
5473
  unknown(taskId) {
5079
5474
  return this.finished.has(taskId) ? { kind: "gone" } : { kind: "not-found" };
5080
5475
  }
5081
5476
  changed() {
5477
+ if (this.polling) {
5478
+ this.changePending = true;
5479
+ return;
5480
+ }
5082
5481
  for (const listener of this.listeners)
5083
5482
  listener();
5084
5483
  }
5085
5484
  }
5086
-
5087
- // src/state/pluginState.ts
5088
- import { mkdir as mkdir7 } from "node:fs/promises";
5089
- import { join as join12 } from "node:path";
5090
- var PLUGIN_NAME = /^[a-z][a-z0-9-]{0,63}$/;
5091
- function pluginStateStore(paths, plugin) {
5092
- if (!PLUGIN_NAME.test(plugin))
5093
- throw new Error(`not a plugin name: ${plugin}`);
5094
- const file = join12(paths.pluginState, `${plugin}.json`);
5095
- return {
5096
- read: () => readJsonFile(file),
5097
- write: async (value) => {
5098
- await mkdir7(paths.pluginState, { recursive: true, mode: 448 });
5099
- await writeJsonAtomic(file, value);
5100
- }
5101
- };
5485
+ function view(entry) {
5486
+ return { taskId: entry.task.taskId, jobId: entry.task.jobId, op: entry.task.op };
5102
5487
  }
5103
5488
 
5104
5489
  // src/util/log.ts
@@ -5357,6 +5742,19 @@ var JOB = "([A-Za-z0-9_-]{1,128})";
5357
5742
  var MAX_EVENTS_WAIT_S = 25;
5358
5743
  function cliRoutes(core) {
5359
5744
  const { pairing, extension, runner, store, registry } = core;
5745
+ let warnedLimit;
5746
+ const limits = async () => {
5747
+ const config = await loadConfig(core.paths).catch((error) => {
5748
+ core.log.warn("config.json could not be read", { error: String(error) });
5749
+ return { maxParallelJobs: undefined, invalidMaxParallelJobs: undefined };
5750
+ });
5751
+ const invalid = config.invalidMaxParallelJobs;
5752
+ if (invalid !== undefined && invalid !== warnedLimit) {
5753
+ core.log.warn("maxParallelJobs in config.json is not a whole number of 1 or more; running one job at a time", { maxParallelJobs: invalid });
5754
+ }
5755
+ warnedLimit = invalid;
5756
+ return config.maxParallelJobs === undefined ? {} : { maxParallelJobs: config.maxParallelJobs };
5757
+ };
5360
5758
  const requireJob = async (jobId) => {
5361
5759
  const job = await store.get(jobId);
5362
5760
  if (job === undefined)
@@ -5368,9 +5766,12 @@ function cliRoutes(core) {
5368
5766
  method: "GET",
5369
5767
  pattern: /^\/v1\/status$/,
5370
5768
  access: "cli",
5371
- handle: () => {
5769
+ handle: async () => {
5372
5770
  const current = pairing.current;
5373
- const activeJob = runner.activeJob();
5771
+ const { maxParallelJobs } = await limits();
5772
+ const activeJobs = runner.activeJobs();
5773
+ const first = activeJobs[0];
5774
+ const activeJob = first === undefined ? undefined : { jobId: first.jobId, ...first.op === undefined ? {} : { op: first.op } };
5374
5775
  const data = {
5375
5776
  extension: {
5376
5777
  paired: current !== null,
@@ -5378,9 +5779,11 @@ function cliRoutes(core) {
5378
5779
  version: current?.extensionVersion ?? null,
5379
5780
  browser: current?.browser ?? null,
5380
5781
  policy: extension.policy,
5381
- account: extension.account
5782
+ account: extension.account,
5783
+ maxTasks: extension.maxTasks
5382
5784
  },
5383
- ...activeJob === undefined ? {} : { activeJob },
5785
+ ...activeJob === undefined ? {} : { activeJob, activeJobs },
5786
+ maxJobs: jobCapacity(extension.maxTasks, maxParallelJobs),
5384
5787
  serverVersion: CLI_VERSION
5385
5788
  };
5386
5789
  return { status: 200, data };
@@ -5432,14 +5835,15 @@ function cliRoutes(core) {
5432
5835
  method: "POST",
5433
5836
  pattern: /^\/v1\/jobs$/,
5434
5837
  access: "cli",
5435
- handle: ({ body }) => {
5838
+ handle: async ({ body }) => {
5436
5839
  const checked = validateMessage("jobSubmitRequest", body);
5437
5840
  if (!checked.ok)
5438
5841
  throw failure("bad-request", "invalid job", { issues: checked.issues });
5439
5842
  const { plugin, command, input } = checked.value;
5440
5843
  if (plugin === CALL_PLUGIN)
5441
5844
  throw failure("bad-request", `use ${BRIDGE_PATHS.call} for single ops`);
5442
- const job = runner.submit(plugin, command, input);
5845
+ const { maxParallelJobs } = await limits();
5846
+ const job = runner.submit(plugin, command, input, maxParallelJobs === undefined ? {} : { maxParallelJobs });
5443
5847
  const data = { jobId: job.jobId };
5444
5848
  return { status: 202, data };
5445
5849
  }
@@ -5547,9 +5951,9 @@ async function readBlob(core, ctx) {
5547
5951
  const blob = core.blobs.get(ctx.params[0]);
5548
5952
  if (blob === undefined)
5549
5953
  throw failure("not-found", "no such blob");
5550
- const task = core.slot.current();
5954
+ const task = core.slot.taskOf(blob.jobId);
5551
5955
  const uploads = task?.op === "composer.attach" || task?.op === "project.upload";
5552
- if (task === undefined || task.jobId !== blob.jobId || !uploads) {
5956
+ if (task === undefined || !uploads) {
5553
5957
  throw failure("gone", "no upload of this blob is running");
5554
5958
  }
5555
5959
  const { offset, length } = checked.value;
@@ -5560,8 +5964,8 @@ async function readBlob(core, ctx) {
5560
5964
  return { status: 200, data: chunk };
5561
5965
  }
5562
5966
  function requireDownloadTask(core, jobId, taskId) {
5563
- const task = core.slot.current();
5564
- if (task?.op !== "files.download" || task.jobId !== jobId || task.taskId !== taskId) {
5967
+ const task = core.slot.taskOf(jobId);
5968
+ if (task?.op !== "files.download" || task.taskId !== taskId) {
5565
5969
  throw failure("gone", "no files.download of this job is running as that task");
5566
5970
  }
5567
5971
  }
@@ -5618,19 +6022,20 @@ async function poll(core, ctx) {
5618
6022
  core.extension.ops = request.capabilities.ops;
5619
6023
  core.extension.policy = request.policy;
5620
6024
  core.extension.account = request.account ?? null;
6025
+ core.extension.maxTasks = request.maxTasks ?? null;
5621
6026
  core.slot.notePoll();
5622
6027
  core.heldPoll?.();
5623
6028
  const token = bearer(ctx.req);
5624
6029
  const stillPaired = () => core.pairing.checkToken(token, ctx.origin) === "ok";
5625
- const view = {
6030
+ const view2 = {
5626
6031
  inflight: new Set(request.inflight.map((entry) => entry.taskId)),
5627
6032
  ops: core.extension.opNames(),
5628
6033
  policy: request.policy.state
5629
6034
  };
5630
- const immediate = core.slot.poll(view);
6035
+ const immediate = core.slot.poll(view2);
5631
6036
  if (immediate !== undefined)
5632
6037
  return { status: 200, data: immediate };
5633
- const held = await new Promise((resolve2) => {
6038
+ const held = await new Promise((resolve3) => {
5634
6039
  let finished = false;
5635
6040
  const finish = (response) => {
5636
6041
  if (finished)
@@ -5640,7 +6045,7 @@ async function poll(core, ctx) {
5640
6045
  unsubscribe();
5641
6046
  if (core.heldPoll === end)
5642
6047
  core.heldPoll = undefined;
5643
- resolve2(response);
6048
+ resolve3(response);
5644
6049
  };
5645
6050
  const end = () => finish(undefined);
5646
6051
  const timer = setTimeout(end, core.pollHoldMs);
@@ -5651,7 +6056,7 @@ async function poll(core, ctx) {
5651
6056
  end();
5652
6057
  return;
5653
6058
  }
5654
- const response = core.slot.poll(view);
6059
+ const response = core.slot.poll(view2);
5655
6060
  if (response !== undefined)
5656
6061
  finish(response);
5657
6062
  });
@@ -5827,7 +6232,8 @@ async function startBridgeServer(options) {
5827
6232
  isPaired: () => pairing.current !== null,
5828
6233
  log,
5829
6234
  rememberConversation: (entry) => recordLastConversation(options.paths, entry),
5830
- pluginState: (plugin) => pluginStateStore(options.paths, plugin)
6235
+ pluginState: (plugin) => pluginStateStore(options.paths, plugin),
6236
+ ...options.pageTurns === undefined ? {} : { pageTurns: options.pageTurns }
5831
6237
  });
5832
6238
  const core = {
5833
6239
  paths: options.paths,
@@ -5889,11 +6295,11 @@ async function startBridgeServer(options) {
5889
6295
  throw thrown;
5890
6296
  }
5891
6297
  }
5892
- await new Promise((resolve2, reject) => {
6298
+ await new Promise((resolve3, reject) => {
5893
6299
  server.once("error", reject);
5894
6300
  server.listen(options.port, BRIDGE_HOST, () => {
5895
6301
  server.off("error", reject);
5896
- resolve2();
6302
+ resolve3();
5897
6303
  });
5898
6304
  });
5899
6305
  boundPort = server.address().port;
@@ -5905,8 +6311,8 @@ async function startBridgeServer(options) {
5905
6311
  core.heldPoll?.();
5906
6312
  store.releaseWaiters();
5907
6313
  slot.close();
5908
- await new Promise((resolve2) => {
5909
- server.close(() => resolve2());
6314
+ await new Promise((resolve3) => {
6315
+ server.close(() => resolve3());
5910
6316
  server.closeAllConnections();
5911
6317
  });
5912
6318
  await runner.settled();
@@ -5955,7 +6361,7 @@ function tooLarge(limit) {
5955
6361
  // src/state/singleInstance.ts
5956
6362
  import { randomBytes as randomBytes6 } from "node:crypto";
5957
6363
  import { constants as constants4 } from "node:fs";
5958
- import { link as link2, open as open4, readFile as readFile7 } from "node:fs/promises";
6364
+ import { link as link2, open as open4, readFile as readFile8 } from "node:fs/promises";
5959
6365
  function isAlive(pid) {
5960
6366
  if (!Number.isInteger(pid) || pid <= 0)
5961
6367
  return false;
@@ -6007,7 +6413,7 @@ async function linkLock(paths) {
6007
6413
  }
6008
6414
  async function readLockPid(paths) {
6009
6415
  try {
6010
- const pid = Number((await readFile7(paths.lock, "utf8")).trim());
6416
+ const pid = Number((await readFile8(paths.lock, "utf8")).trim());
6011
6417
  return Number.isInteger(pid) ? pid : undefined;
6012
6418
  } catch (error) {
6013
6419
  if (isErrno(error, "ENOENT"))
@@ -6018,7 +6424,7 @@ async function readLockPid(paths) {
6018
6424
 
6019
6425
  // src/cli/commands/serve.ts
6020
6426
  async function serveCommand(paths, argv) {
6021
- const args = parse(argv, {
6427
+ const args = parse2(argv, {
6022
6428
  foreground: { type: "boolean", summary: "run in this terminal" },
6023
6429
  daemon: { type: "boolean", summary: "internal: run detached" },
6024
6430
  stop: { type: "boolean", summary: "stop the running serve" },
@@ -6085,9 +6491,9 @@ async function runServe(paths, daemon) {
6085
6491
  });
6086
6492
  if (!daemon)
6087
6493
  note(`voyager serve listening on 127.0.0.1:${server.port} (Ctrl-C to stop)`);
6088
- await new Promise((resolve2) => {
6089
- process.once("SIGINT", resolve2);
6090
- process.once("SIGTERM", resolve2);
6494
+ await new Promise((resolve3) => {
6495
+ process.once("SIGINT", resolve3);
6496
+ process.once("SIGTERM", resolve3);
6091
6497
  });
6092
6498
  log.info("serve stopping");
6093
6499
  await server.close();
@@ -6107,7 +6513,7 @@ async function stopServe(paths) {
6107
6513
  process.kill(pid, "SIGTERM");
6108
6514
  const deadline = Date.now() + 1e4;
6109
6515
  while (isAlive(pid) && Date.now() < deadline)
6110
- await new Promise((resolve2) => setTimeout(resolve2, 100));
6516
+ await new Promise((resolve3) => setTimeout(resolve3, 100));
6111
6517
  if (isAlive(pid)) {
6112
6518
  note(`voyager serve (pid ${pid}) did not stop`);
6113
6519
  return 1;
@@ -6117,7 +6523,7 @@ async function stopServe(paths) {
6117
6523
  }
6118
6524
  async function readLockPid2(paths) {
6119
6525
  try {
6120
- const pid = Number((await readFile8(paths.lock, "utf8")).trim());
6526
+ const pid = Number((await readFile9(paths.lock, "utf8")).trim());
6121
6527
  return Number.isInteger(pid) ? pid : undefined;
6122
6528
  } catch {
6123
6529
  return;
@@ -6150,7 +6556,7 @@ function defaultUpdateDeps(paths) {
6150
6556
  };
6151
6557
  }
6152
6558
  function runCommand(command, args, options) {
6153
- return new Promise((resolve2, reject) => {
6559
+ return new Promise((resolve3, reject) => {
6154
6560
  const child = spawn3(command, [...args], {
6155
6561
  stdio: options.capture ? ["ignore", "pipe", "ignore"] : ["ignore", 2, 2],
6156
6562
  shell: process.platform === "win32"
@@ -6160,7 +6566,7 @@ function runCommand(command, args, options) {
6160
6566
  stdout += chunk.toString();
6161
6567
  });
6162
6568
  child.once("error", reject);
6163
- child.once("close", (code) => resolve2({ code: code ?? 1, stdout }));
6569
+ child.once("close", (code) => resolve3({ code: code ?? 1, stdout }));
6164
6570
  });
6165
6571
  }
6166
6572
  function compareVersions(a, b) {
@@ -6405,7 +6811,7 @@ async function pairedExtensionVersion(deps) {
6405
6811
  }
6406
6812
  }
6407
6813
  async function updateCommand(paths, argv, deps = defaultUpdateDeps(paths)) {
6408
- const args = parse(argv, { json: { type: "boolean", summary: "JSON output" } });
6814
+ const args = parse2(argv, { json: { type: "boolean", summary: "JSON output" } });
6409
6815
  const cli = await updateCli(deps);
6410
6816
  const serve = await restartServe(deps, cli.serveTarget);
6411
6817
  const page = await refreshPageData(deps);
@@ -6452,6 +6858,8 @@ usage:
6452
6858
  voyager ask "<prompt>" [--json] new conversation, send, wait, save response.md
6453
6859
  and the files the reply offers under files/
6454
6860
  [--attach <file|folder>]... upload files; folders are zipped, credentials left out
6861
+ [--preset math] prepend the mathematical research opening
6862
+ [--preset <file>] prepend a UTF-8 text file, trimmed (up to 20,000 characters)
6455
6863
  [--dry-run] pack attachments and print the job input, send nothing
6456
6864
  [--chat <link|id> | --continue] ask in an existing conversation, or the last one used
6457
6865
  [--project <name|id>] ask in a new chat of a ChatGPT project (its files are there)
@@ -6464,7 +6872,7 @@ usage:
6464
6872
  skips one unchanged; a folder goes up as <folder>.zip
6465
6873
  | remove <project> <file> [--yes] deletes at once, no undo; asks, or needs --yes without a tty
6466
6874
  [--keep-tab] any project command: leave its ChatGPT tab open
6467
- voyager status [--json] serve, pairing and active job
6875
+ voyager status [--json] serve, pairing, active jobs and how many may run at once
6468
6876
  voyager commands [--json] plugin commands and extension ops
6469
6877
  voyager call <op> [--params JSON] run one extension op (debugging); files.download saves the file
6470
6878
  voyager job list | show <id> | wait <id> [--timeout 30m] | cancel <id>
@@ -6479,7 +6887,8 @@ each job opens its own ChatGPT tab and closes it when it ends, also after a fail
6479
6887
  unless --keep-tab; one you need to look at (sign-in, an unconfirmed send) stays open
6480
6888
  results name the ChatGPT plan the pages last showed (JSON: account; may trail a change)
6481
6889
  exit codes: 0 ok, 2 failed (data), 3 needs a person, 4 wait timed out (job continues),
6482
- 5 busy, 6 transient/detached/reply-stalled, 7 not paired or extension offline
6890
+ 5 busy (one job at a time, unless parallel jobs are on in the Voyager popup),
6891
+ 6 transient/detached/reply-stalled, 7 not paired or extension offline
6483
6892
  a reply saved with some files not downloaded is 0: see failedFiles (JSON) or stderr
6484
6893
  state: ~/.voyager (override with VOYAGER_HOME)`;
6485
6894
  async function main(argv) {