@voyager-crew/cli 0.4.0 → 0.5.0

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
@@ -4,6 +4,9 @@
4
4
  import { basename as basename3 } from "node:path";
5
5
 
6
6
  // src/plugins/api.ts
7
+ function conversationConflictKey(conversationId) {
8
+ return `conversation ${conversationId}`;
9
+ }
7
10
  function definePlugin(plugin) {
8
11
  return plugin;
9
12
  }
@@ -272,6 +275,7 @@ var POLL_HOLD_MS = 25000;
272
275
  var TASK_ACK_DEADLINE_MS = 1e4;
273
276
  var EXTENSION_OFFLINE_MS = 90000;
274
277
  var MAX_JSON_BODY_BYTES = 1024 * 1024;
278
+ var MAX_INFLIGHT_TASKS = 16;
275
279
  var MAX_BLOB_CHUNK_BYTES = 4 * 1024 * 1024;
276
280
  var EXTENSION_ORIGIN_PATTERNS = [
277
281
  /^chrome-extension:\/\/[a-p]{32}$/,
@@ -948,6 +952,11 @@ function envelopeReader(data) {
948
952
  };
949
953
  }
950
954
  var anyData = (raw) => raw;
955
+ function readMaxTasks(raw) {
956
+ if (typeof raw !== "number" || !Number.isSafeInteger(raw) || raw < 1)
957
+ return 1;
958
+ return Math.min(raw, MAX_INFLIGHT_TASKS);
959
+ }
951
960
  var readOpCapability = (raw, path, issues) => {
952
961
  const rec = readObject(raw, path, issues);
953
962
  return {
@@ -1027,12 +1036,13 @@ var READERS = {
1027
1036
  taskId: requireString(entry, "taskId", itemPath, list, ID),
1028
1037
  state: requireLiteral(entry, "state", itemPath, list, ["acked", "running"])
1029
1038
  };
1030
- }, { max: 8 }),
1039
+ }, { max: MAX_INFLIGHT_TASKS }),
1031
1040
  policy: {
1032
1041
  revision: requireInt(policy, "revision", policyPath, issues),
1033
1042
  state: requireLiteral(policy, "state", policyPath, issues, POLICY_STATES)
1034
1043
  },
1035
- ...rec.account === undefined ? {} : { account: readAccountPlan(rec.account) }
1044
+ ...rec.account === undefined ? {} : { account: readAccountPlan(rec.account) },
1045
+ ...rec.maxTasks === undefined ? {} : { maxTasks: readMaxTasks(rec.maxTasks) }
1036
1046
  };
1037
1047
  },
1038
1048
  pollResponse: (raw, path, issues) => {
@@ -2281,7 +2291,13 @@ async function loadConfig(paths) {
2281
2291
  const port = isRecord2(raw) ? raw.port : undefined;
2282
2292
  const valid = typeof port === "number" && Number.isInteger(port) && port > 0 && port < 65536;
2283
2293
  const driveRemote = isRecord2(raw) && typeof raw.driveRemote === "string" ? raw.driveRemote.trim() : "";
2284
- return { port: valid ? port : DEFAULT_PORT, ...driveRemote === "" ? {} : { driveRemote } };
2294
+ const maxJobs = isRecord2(raw) ? raw.maxParallelJobs : undefined;
2295
+ const usable2 = typeof maxJobs === "number" && Number.isSafeInteger(maxJobs) && maxJobs >= 1;
2296
+ return {
2297
+ port: valid ? port : DEFAULT_PORT,
2298
+ ...driveRemote === "" ? {} : { driveRemote },
2299
+ ...maxJobs === undefined ? {} : usable2 ? { maxParallelJobs: maxJobs } : { maxParallelJobs: 1, invalidMaxParallelJobs: JSON.stringify(maxJobs) }
2300
+ };
2285
2301
  }
2286
2302
 
2287
2303
  // src/state/paths.ts
@@ -2645,14 +2661,12 @@ async function run2(ctx, input) {
2645
2661
  const shown = await callProjectOp(ctx, "project.open", { projectId, view: "chat" });
2646
2662
  await ctx.checkpoint("project", { projectId, url: shown.url });
2647
2663
  }
2648
- const model = await chooseModel(ctx, input.model ?? DEFAULT_ASK_MODEL);
2649
2664
  const result = await askInTab(ctx, input, {
2650
2665
  startedAt,
2651
2666
  blobs,
2652
2667
  attachments,
2653
2668
  existing,
2654
- opened,
2655
- model
2669
+ opened
2656
2670
  });
2657
2671
  if (input.repo !== undefined)
2658
2672
  checkRepoReach(input.repo, result);
@@ -2664,25 +2678,28 @@ async function askInTab(ctx, input, {
2664
2678
  blobs,
2665
2679
  attachments,
2666
2680
  existing,
2667
- opened,
2668
- model
2681
+ opened
2669
2682
  }) {
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}
2683
+ const { model, sent } = await ctx.ops.exclusive(async () => {
2684
+ const chosen = await chooseModel(ctx, input.model ?? DEFAULT_ASK_MODEL);
2685
+ if (blobs.length > 0) {
2686
+ const bytes = blobs.reduce((sum, blob) => sum + blob.size, 0);
2687
+ const attached = await ctx.ops.call("composer.attach", { blobs }, { timeoutMs: attachTimeoutMs(bytes) });
2688
+ await ctx.checkpoint("attached", {
2689
+ names: attached.attached.map((entry) => entry.name),
2690
+ bytes
2691
+ });
2692
+ }
2693
+ const preamble = attachmentPreamble(attachments);
2694
+ const text = preamble === "" ? input.prompt : `${input.prompt}
2680
2695
 
2681
2696
  ${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 } }
2697
+ const confirmed = await sendOnce(ctx, {
2698
+ text,
2699
+ ...blobs.length > 0 ? { expectAttachments: blobs.map((blob) => blob.name) } : {},
2700
+ ...existing === undefined ? {} : { expect: { conversationId: existing } }
2701
+ });
2702
+ return { model: chosen, sent: confirmed };
2686
2703
  });
2687
2704
  const sentAt = Date.now();
2688
2705
  if (existing !== undefined && sent.conversationId !== existing) {
@@ -2804,6 +2821,7 @@ var askPlugin = definePlugin({
2804
2821
  }
2805
2822
  },
2806
2823
  prepare,
2824
+ conflictKey: (input) => input.conversationId === undefined ? undefined : conversationConflictKey(input.conversationId),
2807
2825
  run: run2,
2808
2826
  present: presentResponse
2809
2827
  }
@@ -2824,12 +2842,13 @@ function readFingerprint(raw) {
2824
2842
  };
2825
2843
  }
2826
2844
  async function load(state) {
2827
- let raw;
2828
2845
  try {
2829
- raw = await state.read();
2846
+ return parse(await state.read());
2830
2847
  } catch {
2831
2848
  return {};
2832
2849
  }
2850
+ }
2851
+ function parse(raw) {
2833
2852
  const projects = {};
2834
2853
  if (!isRecord2(raw) || !isRecord2(raw.uploads))
2835
2854
  return projects;
@@ -2851,14 +2870,16 @@ async function fingerprintOf(state, projectId, name) {
2851
2870
  return Object.hasOwn(projects, projectId) && Object.hasOwn(projects[projectId], name) ? projects[projectId][name] : undefined;
2852
2871
  }
2853
2872
  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 });
2873
+ await state.update((raw) => {
2874
+ const projects = parse(raw);
2875
+ const files = { ...Object.hasOwn(projects, projectId) ? projects[projectId] : {} };
2876
+ if (fingerprint)
2877
+ files[name] = fingerprint;
2878
+ else
2879
+ delete files[name];
2880
+ projects[projectId] = files;
2881
+ return { version: 1, uploads: projects };
2882
+ });
2862
2883
  }
2863
2884
 
2864
2885
  // plugins/project/index.ts
@@ -3071,6 +3092,7 @@ var projectPlugin = definePlugin({
3071
3092
  "keep-tab": { type: "boolean", summary: "leave the bridge tab open afterwards" }
3072
3093
  },
3073
3094
  prepare: prepare2,
3095
+ conflictKey: (input) => input.action === "create" || input.action === "upload" || input.action === "remove" ? "project changes" : undefined,
3074
3096
  run: run3,
3075
3097
  present
3076
3098
  }
@@ -3211,7 +3233,7 @@ class PluginRegistry {
3211
3233
  // package.json
3212
3234
  var package_default = {
3213
3235
  name: "@voyager-crew/cli",
3214
- version: "0.4.0",
3236
+ version: "0.5.0",
3215
3237
  description: "Ask ChatGPT from your terminal or your coding agent, through the Voyager browser extension",
3216
3238
  keywords: [
3217
3239
  "chatgpt",
@@ -3258,7 +3280,7 @@ var package_default = {
3258
3280
  },
3259
3281
  devDependencies: {
3260
3282
  "@types/node": "^24.9.0",
3261
- "@voyager-crew/bridge-protocol": "^0.4.0",
3283
+ "@voyager-crew/bridge-protocol": "^0.5.0",
3262
3284
  fflate: "0.8.2",
3263
3285
  oxfmt: "^0.65.0",
3264
3286
  oxlint: "^1.80.0",
@@ -3526,7 +3548,7 @@ function parseDuration(input) {
3526
3548
  }
3527
3549
 
3528
3550
  // src/cli/args.ts
3529
- function parse(argv, defs) {
3551
+ function parse2(argv, defs) {
3530
3552
  const options = {};
3531
3553
  for (const [name, def] of Object.entries(defs)) {
3532
3554
  options[name] = {
@@ -3568,7 +3590,7 @@ function waitTimeout(args) {
3568
3590
 
3569
3591
  // src/cli/commands/info.ts
3570
3592
  async function statusCommand(paths, argv) {
3571
- const args = parse(argv, { json: { type: "boolean", summary: "JSON output" } });
3593
+ const args = parse2(argv, { json: { type: "boolean", summary: "JSON output" } });
3572
3594
  const client = await ensureServe(paths);
3573
3595
  const status = await client.request("GET", BRIDGE_PATHS.status);
3574
3596
  if (args.options.json === true) {
@@ -3589,11 +3611,46 @@ async function statusCommand(paths, argv) {
3589
3611
  const age = seenAt === undefined ? "" : `, seen ${Math.max(0, Math.round((Date.now() - seenAt) / 60000))} min ago`;
3590
3612
  printLine(`${describeAccount(ext.account)}${ext.account ? age : ""}`);
3591
3613
  }
3592
- printLine(status.activeJob === undefined ? "active job: none" : `active job: ${status.activeJob.jobId}${status.activeJob.op ? ` (${status.activeJob.op})` : ""}`);
3614
+ for (const line of statusJobLines(status))
3615
+ printLine(line);
3593
3616
  return 0;
3594
3617
  }
3618
+ function statusJobLines(status, now = Date.now()) {
3619
+ const active = status.activeJobs ?? (status.activeJob === undefined ? [] : [status.activeJob]);
3620
+ const turns = active.length > 1;
3621
+ const named = active.map((job) => {
3622
+ const turn = !turns ? undefined : job.pageTurn === "holding" ? "has the page turn" : job.pageTurn === "waiting" ? "waiting for the page turn" : undefined;
3623
+ const notes = [job.op, turn].filter((part) => part !== undefined);
3624
+ return `${job.jobId}${notes.length > 0 ? ` (${notes.join(", ")})` : ""}`;
3625
+ });
3626
+ const label = named.length > 1 ? "active jobs" : "active job";
3627
+ const lines = [`${label}: ${named.length === 0 ? "none" : named.join(", ")}`];
3628
+ const parallel = parallelLine(status, now);
3629
+ if (parallel !== undefined)
3630
+ lines.push(parallel);
3631
+ return lines;
3632
+ }
3633
+ function parallelLine(status, now) {
3634
+ const ext = status.extension;
3635
+ const last = ext.lastPollAt;
3636
+ if (!ext.paired || last === null || now - last > EXTENSION_OFFLINE_MS)
3637
+ return;
3638
+ const offered = ext.maxTasks;
3639
+ const maxJobs = status.maxJobs ?? 1;
3640
+ if (offered === undefined) {
3641
+ return maxJobs > 1 ? `parallel jobs: up to ${maxJobs} at once` : undefined;
3642
+ }
3643
+ if (offered === null) {
3644
+ return `parallel jobs: not in Voyager ${ext.version ?? "?"} (one job at a time; a newer Voyager runs several)`;
3645
+ }
3646
+ if (offered <= 1)
3647
+ return "parallel jobs: off (one job at a time; turn it on in the Voyager popup)";
3648
+ if (maxJobs >= offered)
3649
+ return `parallel jobs: up to ${maxJobs} at once`;
3650
+ return `parallel jobs: up to ${maxJobs} at once (maxParallelJobs in config.json; the Voyager popup allows ${offered})`;
3651
+ }
3595
3652
  async function commandsCommand(paths, argv) {
3596
- const args = parse(argv, { json: { type: "boolean", summary: "JSON output" } });
3653
+ const args = parse2(argv, { json: { type: "boolean", summary: "JSON output" } });
3597
3654
  const client = await ensureServe(paths);
3598
3655
  const listed2 = await client.request("GET", BRIDGE_PATHS.commands);
3599
3656
  if (args.options.json === true) {
@@ -3749,11 +3806,10 @@ async function submitAndWait(client, registry, args, submit) {
3749
3806
  ({ jobId } = await submit());
3750
3807
  } catch (error) {
3751
3808
  if (error instanceof BridgeFailure && error.code === "busy") {
3752
- const active = String(error.error.details?.activeJobId ?? "?");
3753
3809
  if (args.json)
3754
3810
  printJson({ ok: false, error: error.error });
3755
3811
  else
3756
- note(`busy: job ${active} is running. Wait with voyager job wait ${active}; jobs are not queued.`);
3812
+ note(busyNote(error.error));
3757
3813
  return EXIT.busy;
3758
3814
  }
3759
3815
  throw error;
@@ -3769,6 +3825,17 @@ async function submitAndWait(client, registry, args, submit) {
3769
3825
  note(`job ${jobId}`);
3770
3826
  return waitAndPresent(client, registry, jobId, { ...args, account: account2 });
3771
3827
  }
3828
+ function busyNote(error) {
3829
+ const active = String(error.details?.activeJobId ?? "?");
3830
+ const maxJobs = error.details?.maxJobs;
3831
+ if (typeof maxJobs !== "number" || maxJobs <= 1) {
3832
+ return `busy: job ${active} is running. Wait with voyager job wait ${active}; jobs are not queued.`;
3833
+ }
3834
+ const others = error.details?.activeJobIds;
3835
+ const several = error.details?.conflict === undefined && Array.isArray(others) && others.length > 1;
3836
+ const which = several ? ` (running: ${others.join(", ")})` : "";
3837
+ return `busy: ${error.message}. Wait with voyager job wait ${active}${which}.`;
3838
+ }
3772
3839
  async function waitAndPresent(client, registry, jobId, args) {
3773
3840
  const outcome = await waitForJob(client, jobId, args.timeoutMs, (event) => {
3774
3841
  const line = args.json ? undefined : describeEvent(event);
@@ -3790,7 +3857,7 @@ async function waitAndPresent(client, registry, jobId, args) {
3790
3857
  return presentJob(outcome.job, registry, args.json);
3791
3858
  }
3792
3859
  async function callCommand(paths, registry, argv) {
3793
- const args = parse(argv, {
3860
+ const args = parse2(argv, {
3794
3861
  ...WAIT_OPTIONS,
3795
3862
  params: { type: "string", summary: "op params as JSON" }
3796
3863
  });
@@ -3813,7 +3880,7 @@ async function callCommand(paths, registry, argv) {
3813
3880
  }
3814
3881
  async function jobCommand(paths, registry, argv) {
3815
3882
  const [sub, ...rest] = argv;
3816
- const args = parse(rest, WAIT_OPTIONS);
3883
+ const args = parse2(rest, WAIT_OPTIONS);
3817
3884
  const json = args.options.json === true;
3818
3885
  const jobId = args.positionals[0];
3819
3886
  const needId = () => {
@@ -3863,7 +3930,7 @@ var CLI_LOCAL_PATHS = {
3863
3930
 
3864
3931
  // src/cli/commands/pair.ts
3865
3932
  async function pairCommand(paths, argv) {
3866
- const args = parse(argv, {
3933
+ const args = parse2(argv, {
3867
3934
  revoke: { type: "boolean", summary: "forget the paired extension" },
3868
3935
  json: { type: "boolean", summary: "JSON lines on stdout" }
3869
3936
  });
@@ -3994,7 +4061,7 @@ function terminalConfirm() {
3994
4061
  };
3995
4062
  }
3996
4063
  async function pluginCommand(paths, registry, found, argv) {
3997
- const args = parse(argv, { ...WAIT_OPTIONS, ...found.def.options });
4064
+ const args = parse2(argv, { ...WAIT_OPTIONS, ...found.def.options });
3998
4065
  const confirm = terminalConfirm();
3999
4066
  const fg = {
4000
4067
  cwd: process.cwd(),
@@ -4291,7 +4358,7 @@ class IncomingBlobStore {
4291
4358
  }
4292
4359
 
4293
4360
  // src/jobs/jobRunner.ts
4294
- import { join as join11 } from "node:path";
4361
+ import { join as join12 } from "node:path";
4295
4362
 
4296
4363
  // src/server/extensionState.ts
4297
4364
  function policyFailure(state, op) {
@@ -4311,6 +4378,7 @@ class ExtensionState {
4311
4378
  ops = null;
4312
4379
  policy = null;
4313
4380
  account = null;
4381
+ maxTasks = null;
4314
4382
  opNames() {
4315
4383
  return this.ops === null ? null : new Set(this.ops.map((op) => op.name));
4316
4384
  }
@@ -4319,12 +4387,78 @@ class ExtensionState {
4319
4387
  this.ops = null;
4320
4388
  this.policy = null;
4321
4389
  this.account = null;
4390
+ this.maxTasks = null;
4322
4391
  }
4323
4392
  }
4324
4393
 
4325
- // src/jobs/jobStore.ts
4326
- import { appendFile, mkdir as mkdir6, readdir as readdir5, readFile as readFile6, rm as rm4 } from "node:fs/promises";
4394
+ // src/state/pluginState.ts
4395
+ import { mkdir as mkdir6 } from "node:fs/promises";
4327
4396
  import { join as join10 } from "node:path";
4397
+ var PLUGIN_NAME = /^[a-z][a-z0-9-]{0,63}$/;
4398
+ function pluginStateStore(paths, plugin) {
4399
+ if (!PLUGIN_NAME.test(plugin))
4400
+ throw new Error(`not a plugin name: ${plugin}`);
4401
+ const file = join10(paths.pluginState, `${plugin}.json`);
4402
+ return {
4403
+ read: () => readJsonFile(file),
4404
+ write: async (value) => {
4405
+ await mkdir6(paths.pluginState, { recursive: true, mode: 448 });
4406
+ await writeJsonAtomic(file, value);
4407
+ }
4408
+ };
4409
+ }
4410
+ function withSerialUpdates(base, last) {
4411
+ return {
4412
+ read: () => base.read(),
4413
+ write: (value) => base.write(value),
4414
+ update: (change) => {
4415
+ const next = last.current.then(async () => {
4416
+ const current = await base.read().catch(() => {
4417
+ return;
4418
+ });
4419
+ await base.write(change(current));
4420
+ });
4421
+ last.current = next.catch(() => {
4422
+ return;
4423
+ });
4424
+ return next;
4425
+ }
4426
+ };
4427
+ }
4428
+
4429
+ // src/jobs/admission.ts
4430
+ function jobCapacity(extensionMaxTasks, configured) {
4431
+ const extension = extensionMaxTasks ?? 1;
4432
+ return configured === undefined ? extension : Math.max(1, Math.min(extension, configured));
4433
+ }
4434
+ function admissionFailure(running, next, capacity) {
4435
+ const first = running[0];
4436
+ if (first === undefined)
4437
+ return;
4438
+ const details = {
4439
+ activeJobId: first.jobId,
4440
+ activeJobIds: running.map((job) => job.jobId),
4441
+ maxJobs: capacity
4442
+ };
4443
+ if (capacity <= 1)
4444
+ return failure("busy", "another job is running; it is not queued", details);
4445
+ if (next.alone || running.some((job) => job.alone)) {
4446
+ return failure("busy", "voyager call runs only while no other job does; it is not queued", details);
4447
+ }
4448
+ if (running.length >= capacity) {
4449
+ return failure("busy", `${running.length} jobs are running, as many as may run at once; it is not queued`, details);
4450
+ }
4451
+ const key = next.conflictKey;
4452
+ const clash = key === undefined ? undefined : running.find((job) => job.conflictKeys.includes(key));
4453
+ if (clash !== undefined) {
4454
+ return failure("busy", `job ${clash.jobId} is working on ${next.conflictKey}; it is not queued`, { ...details, activeJobId: clash.jobId, conflict: next.conflictKey });
4455
+ }
4456
+ return;
4457
+ }
4458
+
4459
+ // src/jobs/jobStore.ts
4460
+ import { appendFile, mkdir as mkdir7, readdir as readdir5, readFile as readFile6, rm as rm4 } from "node:fs/promises";
4461
+ import { join as join11 } from "node:path";
4328
4462
  var TERMINAL_STATUSES = new Set([
4329
4463
  "succeeded",
4330
4464
  "failed",
@@ -4355,7 +4489,7 @@ class JobStore {
4355
4489
  if (job === undefined || isTerminal(job.status))
4356
4490
  continue;
4357
4491
  await this.loadEvents(jobId);
4358
- await rm4(join10(this.jobDir(jobId), "incoming"), { recursive: true, force: true });
4492
+ await rm4(join11(this.jobDir(jobId), "incoming"), { recursive: true, force: true });
4359
4493
  if (sendInterrupted(job.checkpoints)) {
4360
4494
  const error = sendUnverified("serve stopped before the send was confirmed; the prompt may have been sent", { reason: "serve restarted" });
4361
4495
  this.update(job, { status: "failed", error });
@@ -4385,7 +4519,7 @@ class JobStore {
4385
4519
  this.jobs.set(job.jobId, job);
4386
4520
  this.events.set(job.jobId, []);
4387
4521
  this.enqueue(job.jobId, async () => {
4388
- await mkdir6(this.outputDir(job.jobId), { recursive: true, mode: 448 });
4522
+ await mkdir7(this.outputDir(job.jobId), { recursive: true, mode: 448 });
4389
4523
  });
4390
4524
  this.persist(job);
4391
4525
  return job;
@@ -4402,7 +4536,7 @@ class JobStore {
4402
4536
  this.events.set(jobId, list);
4403
4537
  const event = { seq: (list.at(-1)?.seq ?? 0) + 1, at: this.now(), type, data };
4404
4538
  list.push(event);
4405
- this.enqueue(jobId, () => appendFile(join10(this.jobDir(jobId), "events.jsonl"), `${JSON.stringify(event)}
4539
+ this.enqueue(jobId, () => appendFile(join11(this.jobDir(jobId), "events.jsonl"), `${JSON.stringify(event)}
4406
4540
  `, {
4407
4541
  mode: 384
4408
4542
  }));
@@ -4451,10 +4585,10 @@ class JobStore {
4451
4585
  };
4452
4586
  }
4453
4587
  jobDir(jobId) {
4454
- return join10(this.paths.jobs, jobId);
4588
+ return join11(this.paths.jobs, jobId);
4455
4589
  }
4456
4590
  outputDir(jobId) {
4457
- return join10(this.jobDir(jobId), "output");
4591
+ return join11(this.jobDir(jobId), "output");
4458
4592
  }
4459
4593
  async flush(jobId) {
4460
4594
  const ids = jobId === undefined ? [...this.writes.keys()] : [jobId];
@@ -4476,10 +4610,10 @@ class JobStore {
4476
4610
  }
4477
4611
  persist(job) {
4478
4612
  const snapshot = JSON.parse(JSON.stringify(job));
4479
- this.enqueue(job.jobId, () => writeJsonAtomic(join10(this.jobDir(job.jobId), "job.json"), snapshot));
4613
+ this.enqueue(job.jobId, () => writeJsonAtomic(join11(this.jobDir(job.jobId), "job.json"), snapshot));
4480
4614
  }
4481
4615
  enqueue(jobId, write) {
4482
- const previous = this.writes.get(jobId) ?? mkdir6(this.jobDir(jobId), { recursive: true, mode: 448 }).then(() => {
4616
+ const previous = this.writes.get(jobId) ?? mkdir7(this.jobDir(jobId), { recursive: true, mode: 448 }).then(() => {
4483
4617
  return;
4484
4618
  });
4485
4619
  const next = previous.then(write).then(() => {
@@ -4505,7 +4639,7 @@ class JobStore {
4505
4639
  async load(jobId) {
4506
4640
  if (!/^[A-Za-z0-9_-]{1,128}$/.test(jobId))
4507
4641
  return;
4508
- const raw = await readJsonFile(join10(this.jobDir(jobId), "job.json"));
4642
+ const raw = await readJsonFile(join11(this.jobDir(jobId), "job.json"));
4509
4643
  if (!isRecord2(raw) || raw.jobId !== jobId || typeof raw.status !== "string")
4510
4644
  return;
4511
4645
  const job = raw;
@@ -4519,7 +4653,7 @@ class JobStore {
4519
4653
  return cached;
4520
4654
  let text = "";
4521
4655
  try {
4522
- text = await readFile6(join10(this.jobDir(jobId), "events.jsonl"), "utf8");
4656
+ text = await readFile6(join11(this.jobDir(jobId), "events.jsonl"), "utf8");
4523
4657
  } catch (error) {
4524
4658
  if (!isErrno(error, "ENOENT"))
4525
4659
  throw error;
@@ -4548,6 +4682,154 @@ function summarize(job) {
4548
4682
  };
4549
4683
  }
4550
4684
 
4685
+ // src/jobs/pageTurns.ts
4686
+ var PASSIVE_OPS = new Set([
4687
+ "model.inspect",
4688
+ "turns.waitReply",
4689
+ "turns.read",
4690
+ "files.download",
4691
+ "project.list",
4692
+ "catalog.refresh"
4693
+ ]);
4694
+ function takesPageTurn(op) {
4695
+ return !PASSIVE_OPS.has(op);
4696
+ }
4697
+ var PAGE_TURN_GRACE_MS = 60000;
4698
+ var CLEANUP_TURN_WAIT_MS = 60000;
4699
+
4700
+ class PageTurns {
4701
+ holder;
4702
+ depth = 0;
4703
+ queue = [];
4704
+ holderDue = 0;
4705
+ holderOp;
4706
+ overdueTimer;
4707
+ graceMs;
4708
+ cleanupWaitMs;
4709
+ constructor(options = {}) {
4710
+ this.graceMs = options.graceMs ?? PAGE_TURN_GRACE_MS;
4711
+ this.cleanupWaitMs = options.cleanupWaitMs ?? CLEANUP_TURN_WAIT_MS;
4712
+ }
4713
+ async take(jobId, options = {}) {
4714
+ const { signal } = options;
4715
+ if (signal?.aborted)
4716
+ throw failure("cancelled", "job was cancelled");
4717
+ if (this.holder === undefined || this.holder === jobId)
4718
+ this.grant(jobId);
4719
+ else
4720
+ await this.wait(jobId, signal, options.cleanup === true);
4721
+ let given = false;
4722
+ return () => {
4723
+ if (given)
4724
+ return;
4725
+ given = true;
4726
+ this.giveBack();
4727
+ };
4728
+ }
4729
+ opStarted(jobId, op, deadlineMs) {
4730
+ if (this.holder !== jobId)
4731
+ return;
4732
+ this.holderOp = op;
4733
+ this.due(deadlineMs + this.graceMs);
4734
+ }
4735
+ opEnded(jobId) {
4736
+ if (this.holder !== jobId)
4737
+ return;
4738
+ this.holderOp = undefined;
4739
+ this.due(this.graceMs);
4740
+ }
4741
+ current() {
4742
+ return this.holder;
4743
+ }
4744
+ waiting() {
4745
+ return this.queue.map((waiter) => waiter.jobId);
4746
+ }
4747
+ wait(jobId, signal, cleanup) {
4748
+ if (Date.now() >= this.holderDue)
4749
+ return Promise.reject(this.overdueFailure());
4750
+ return new Promise((resolve2, reject) => {
4751
+ const leave = () => {
4752
+ clearTimeout(cleanupTimer);
4753
+ signal?.removeEventListener("abort", onAbort);
4754
+ const index = this.queue.indexOf(waiter);
4755
+ if (index < 0)
4756
+ return false;
4757
+ this.queue.splice(index, 1);
4758
+ return true;
4759
+ };
4760
+ const waiter = {
4761
+ jobId,
4762
+ grant: () => {
4763
+ leave();
4764
+ resolve2();
4765
+ },
4766
+ fail: (error) => {
4767
+ if (leave())
4768
+ reject(error);
4769
+ }
4770
+ };
4771
+ const onAbort = () => waiter.fail(failure("cancelled", "job was cancelled"));
4772
+ const cleanupTimer = cleanup ? setTimeout(() => waiter.fail(this.overdueFailure(this.cleanupWaitMs)), this.cleanupWaitMs) : undefined;
4773
+ this.queue.push(waiter);
4774
+ signal?.addEventListener("abort", onAbort, { once: true });
4775
+ this.armOverdue();
4776
+ });
4777
+ }
4778
+ due(fromNowMs) {
4779
+ this.holderDue = Date.now() + fromNowMs;
4780
+ this.armOverdue();
4781
+ }
4782
+ armOverdue() {
4783
+ clearTimeout(this.overdueTimer);
4784
+ this.overdueTimer = undefined;
4785
+ if (this.holder === undefined || this.queue.length === 0)
4786
+ return;
4787
+ this.overdueTimer = setTimeout(() => {
4788
+ if (Date.now() < this.holderDue) {
4789
+ this.armOverdue();
4790
+ return;
4791
+ }
4792
+ const error = this.overdueFailure();
4793
+ for (const waiter of this.queue.slice())
4794
+ waiter.fail(error);
4795
+ }, Math.max(0, this.holderDue - Date.now()));
4796
+ }
4797
+ overdueFailure(cleanupWaitMs) {
4798
+ const holder = this.holder ?? "?";
4799
+ const step = this.holderOp === undefined ? "between its steps" : `in ${this.holderOp}`;
4800
+ 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`;
4801
+ return failure("busy", `${why}; nothing was sent for this step`, {
4802
+ activeJobId: this.holder,
4803
+ pageTurn: true,
4804
+ ...this.holderOp === undefined ? {} : { holderOp: this.holderOp }
4805
+ });
4806
+ }
4807
+ grant(jobId) {
4808
+ if (this.holder !== jobId) {
4809
+ this.holder = jobId;
4810
+ this.holderOp = undefined;
4811
+ this.due(this.graceMs);
4812
+ }
4813
+ this.depth += 1;
4814
+ }
4815
+ giveBack() {
4816
+ this.depth -= 1;
4817
+ if (this.depth > 0)
4818
+ return;
4819
+ this.holder = undefined;
4820
+ this.holderOp = undefined;
4821
+ const next = this.queue[0];
4822
+ if (next === undefined) {
4823
+ clearTimeout(this.overdueTimer);
4824
+ this.overdueTimer = undefined;
4825
+ return;
4826
+ }
4827
+ this.grant(next.jobId);
4828
+ next.grant();
4829
+ this.armOverdue();
4830
+ }
4831
+ }
4832
+
4551
4833
  // src/jobs/opsClient.ts
4552
4834
  var DEFAULT_OP_TIMEOUT_MS = {
4553
4835
  "conversation.open": 60000,
@@ -4577,7 +4859,18 @@ function defaultOpTimeout(op, params) {
4577
4859
  return DEFAULT_OP_TIMEOUT_MS[op];
4578
4860
  }
4579
4861
  function createOpsClient(hooks) {
4862
+ const { jobId, signal, slot, turns } = hooks;
4863
+ const inTurn = async (work, cleanup = false) => {
4864
+ const giveBack = await turns.take(jobId, { signal, cleanup });
4865
+ try {
4866
+ return await work();
4867
+ } finally {
4868
+ await slot.idle(jobId);
4869
+ giveBack();
4870
+ }
4871
+ };
4580
4872
  return {
4873
+ exclusive: (work) => inTurn(work),
4581
4874
  async call(op, params, options = {}) {
4582
4875
  if (hooks.signal.aborted)
4583
4876
  throw failure("cancelled", "job was cancelled");
@@ -4594,20 +4887,26 @@ function createOpsClient(hooks) {
4594
4887
  if (!checked.ok) {
4595
4888
  throw failure("bad-request", `invalid params for ${op}`, { issues: checked.issues });
4596
4889
  }
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
- }
4890
+ const deadlineMs = options.timeoutMs ?? defaultOpTimeout(op, checked.value);
4891
+ const run5 = async () => {
4892
+ hooks.onStart(op);
4893
+ turns.opStarted(jobId, op, deadlineMs);
4894
+ try {
4895
+ const result = await slot.run({
4896
+ jobId,
4897
+ op,
4898
+ params: checked.value,
4899
+ deadlineMs,
4900
+ signal,
4901
+ onProgress: (event) => hooks.onProgress(op, event)
4902
+ });
4903
+ return result;
4904
+ } finally {
4905
+ turns.opEnded(jobId);
4906
+ hooks.onEnd(op);
4907
+ }
4908
+ };
4909
+ return takesPageTurn(op) ? inTurn(run5, op === "conversation.close") : run5();
4611
4910
  }
4612
4911
  };
4613
4912
  }
@@ -4615,18 +4914,27 @@ function createOpsClient(hooks) {
4615
4914
  // src/jobs/jobRunner.ts
4616
4915
  class JobRunner {
4617
4916
  deps;
4618
- active;
4917
+ active = new Map;
4619
4918
  memoryState = new Map;
4919
+ turns;
4920
+ stateUpdates = new Map;
4620
4921
  constructor(deps) {
4621
4922
  this.deps = deps;
4923
+ this.turns = new PageTurns(deps.pageTurns);
4622
4924
  }
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
- }
4925
+ submit(plugin, command, input, options = {}) {
4629
4926
  const def = this.deps.registry.command(plugin, command);
4927
+ const running = [...this.active.values()].map((entry) => ({
4928
+ jobId: entry.job.jobId,
4929
+ alone: entry.alone,
4930
+ conflictKeys: [...entry.conflictKeys]
4931
+ }));
4932
+ const alone = plugin === CALL_PLUGIN;
4933
+ const conflictKey = conflictKeyOf(def, input);
4934
+ const capacity = jobCapacity(this.deps.extension.maxTasks, options.maxParallelJobs);
4935
+ const busy = admissionFailure(running, { alone, conflictKey }, capacity);
4936
+ if (busy !== undefined)
4937
+ throw busy;
4630
4938
  if (def === undefined)
4631
4939
  throw failure("bad-request", `unknown command ${plugin} ${command}`);
4632
4940
  if (!this.deps.isPaired()) {
@@ -4645,31 +4953,40 @@ class JobRunner {
4645
4953
  const job = this.deps.store.create(plugin, command, input);
4646
4954
  const controller = new AbortController;
4647
4955
  this.deps.store.appendEvent(job.jobId, "status", { status: "running" });
4956
+ const conflictKeys = new Set(conflictKey === undefined ? [] : [conflictKey]);
4648
4957
  const done = this.execute(job, def, controller);
4649
- this.active = { job, controller, done };
4958
+ this.active.set(job.jobId, { job, controller, done, alone, conflictKeys });
4650
4959
  return job;
4651
4960
  }
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 };
4961
+ activeJobs() {
4962
+ const holder = this.turns.current();
4963
+ const waiting = new Set(this.turns.waiting());
4964
+ return [...this.active.keys()].map((jobId) => {
4965
+ const op = this.deps.slot.taskOf(jobId)?.op;
4966
+ const pageTurn = holder === jobId ? "holding" : waiting.has(jobId) ? "waiting" : undefined;
4967
+ return {
4968
+ jobId,
4969
+ ...op === undefined ? {} : { op },
4970
+ ...pageTurn === undefined ? {} : { pageTurn }
4971
+ };
4972
+ });
4657
4973
  }
4658
4974
  async cancel(jobId) {
4659
4975
  const job = await this.deps.store.get(jobId);
4660
4976
  if (job === undefined)
4661
4977
  throw failure("not-found", `no job ${jobId}`);
4662
- if (this.active?.job !== job) {
4978
+ const active = this.active.get(jobId);
4979
+ if (active?.job !== job) {
4663
4980
  throw failure("gone", `job ${jobId} has already ended (${job.status})`);
4664
4981
  }
4665
4982
  if (job.status !== "cancelling") {
4666
4983
  this.setStatus(job, "cancelling");
4667
- this.active.controller.abort();
4984
+ active.controller.abort();
4668
4985
  }
4669
4986
  return job;
4670
4987
  }
4671
4988
  async settled() {
4672
- await this.active?.done;
4989
+ await Promise.all([...this.active.values()].map((entry) => entry.done));
4673
4990
  }
4674
4991
  async execute(job, def, controller) {
4675
4992
  await Promise.resolve();
@@ -4689,7 +5006,7 @@ class JobRunner {
4689
5006
  }
4690
5007
  status = statusForError(error, job.checkpoints);
4691
5008
  }
4692
- await this.deps.slot.idle();
5009
+ await this.deps.slot.idle(job.jobId);
4693
5010
  this.deps.blobs.forgetJob(job.jobId);
4694
5011
  await this.deps.incoming.forgetJob(job.jobId).catch((error2) => {
4695
5012
  this.deps.log.warn("could not delete unsaved downloads", {
@@ -4711,7 +5028,7 @@ class JobRunner {
4711
5028
  ...error === undefined ? {} : { error }
4712
5029
  });
4713
5030
  this.deps.log.info("job finished", { jobId: job.jobId, status, code: error?.code });
4714
- this.active = undefined;
5031
+ this.active.delete(job.jobId);
4715
5032
  }
4716
5033
  context(job, controller) {
4717
5034
  const { store, slot, extension, log } = this.deps;
@@ -4719,6 +5036,7 @@ class JobRunner {
4719
5036
  jobId: job.jobId,
4720
5037
  signal: controller.signal,
4721
5038
  slot,
5039
+ turns: this.turns,
4722
5040
  available: () => extension.opNames(),
4723
5041
  policy: () => extension.policy?.state,
4724
5042
  onStart: (op) => {
@@ -4738,10 +5056,13 @@ class JobRunner {
4738
5056
  ops: ops2,
4739
5057
  blobs: {
4740
5058
  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)
5059
+ save: (blob, name) => this.deps.incoming.take(job.jobId, blob, join12(store.outputDir(job.jobId), "files"), name)
4742
5060
  },
4743
5061
  progress: (event) => store.appendEvent(job.jobId, "progress", event),
4744
5062
  checkpoint: async (name, data) => {
5063
+ if (typeof data.conversationId === "string") {
5064
+ this.active.get(job.jobId)?.conflictKeys.add(conversationConflictKey(data.conversationId));
5065
+ }
4745
5066
  job.checkpoints[name] = data;
4746
5067
  store.update(job, {});
4747
5068
  store.appendEvent(job.jobId, "checkpoint", { name, data });
@@ -4753,10 +5074,7 @@ class JobRunner {
4753
5074
  });
4754
5075
  }
4755
5076
  },
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
- },
5077
+ state: this.pluginState(job.plugin),
4760
5078
  log: {
4761
5079
  info: (message, fields) => {
4762
5080
  store.appendEvent(job.jobId, "log", { level: "info", message, ...fields });
@@ -4769,6 +5087,18 @@ class JobRunner {
4769
5087
  }
4770
5088
  };
4771
5089
  }
5090
+ pluginState(plugin) {
5091
+ const base = this.deps.pluginState?.(plugin) ?? {
5092
+ read: async () => this.memoryState.get(plugin),
5093
+ write: async (value) => void this.memoryState.set(plugin, value)
5094
+ };
5095
+ let last = this.stateUpdates.get(plugin);
5096
+ if (last === undefined) {
5097
+ last = { current: Promise.resolve() };
5098
+ this.stateUpdates.set(plugin, last);
5099
+ }
5100
+ return withSerialUpdates(base, last);
5101
+ }
4772
5102
  async remember(job, result) {
4773
5103
  const conversationId = result?.conversationId;
4774
5104
  if (this.deps.rememberConversation === undefined || typeof conversationId !== "string")
@@ -4796,6 +5126,13 @@ class JobRunner {
4796
5126
  this.deps.store.appendEvent(job.jobId, "status", { status });
4797
5127
  }
4798
5128
  }
5129
+ function conflictKeyOf(def, input) {
5130
+ try {
5131
+ return def?.conflictKey?.(input);
5132
+ } catch {
5133
+ return;
5134
+ }
5135
+ }
4799
5136
  function statusForError(error, checkpoints) {
4800
5137
  if (error.code === "cancelled")
4801
5138
  return "cancelled";
@@ -4812,10 +5149,12 @@ function statusForError(error, checkpoints) {
4812
5149
  var FINISHED_MEMORY = 64;
4813
5150
 
4814
5151
  class TaskSlot {
4815
- entry;
5152
+ entries = new Map;
4816
5153
  finished = new Map;
4817
5154
  idleWaiters = [];
4818
5155
  listeners = new Set;
5156
+ polling = false;
5157
+ changePending = false;
4819
5158
  offlineTimer;
4820
5159
  now;
4821
5160
  ackDeadlineMs;
@@ -4830,10 +5169,10 @@ class TaskSlot {
4830
5169
  this.abortResendMs = options.abortResendMs ?? 2000;
4831
5170
  }
4832
5171
  async run(input) {
4833
- await this.idle();
5172
+ await this.idle(input.jobId);
4834
5173
  if (input.signal?.aborted)
4835
5174
  throw failure("cancelled", "job was cancelled");
4836
- if (this.entry !== undefined)
5175
+ if (this.taskOf(input.jobId) !== undefined)
4837
5176
  return this.run(input);
4838
5177
  return new Promise((resolve2, reject) => {
4839
5178
  const entry = {
@@ -4856,7 +5195,7 @@ class TaskSlot {
4856
5195
  reject,
4857
5196
  ...input.onProgress === undefined ? {} : { onProgress: input.onProgress }
4858
5197
  };
4859
- this.entry = entry;
5198
+ this.entries.set(entry.task.taskId, entry);
4860
5199
  this.armOffline();
4861
5200
  input.signal?.addEventListener("abort", () => this.abandon(entry, "cancelled"), {
4862
5201
  once: true
@@ -4864,76 +5203,87 @@ class TaskSlot {
4864
5203
  this.changed();
4865
5204
  });
4866
5205
  }
4867
- current() {
4868
- const entry = this.entry;
4869
- return entry && { taskId: entry.task.taskId, jobId: entry.task.jobId, op: entry.task.op };
5206
+ tasks() {
5207
+ return [...this.entries.values()].map(view);
4870
5208
  }
4871
- idle() {
4872
- if (this.entry === undefined)
5209
+ taskOf(jobId) {
5210
+ const entry = [...this.entries.values()].find((item) => item.task.jobId === jobId);
5211
+ return entry && view(entry);
5212
+ }
5213
+ idle(jobId) {
5214
+ if (this.isIdle(jobId))
4873
5215
  return Promise.resolve();
4874
- return new Promise((resolve2) => this.idleWaiters.push(resolve2));
5216
+ return new Promise((wake) => this.idleWaiters.push({ jobId, wake }));
4875
5217
  }
4876
5218
  onChange(listener) {
4877
5219
  this.listeners.add(listener);
4878
5220
  return () => this.listeners.delete(listener);
4879
5221
  }
4880
5222
  notePoll() {
4881
- if (this.entry !== undefined)
5223
+ if (this.entries.size > 0)
4882
5224
  this.armOffline();
4883
5225
  }
4884
5226
  poll(view) {
4885
- const entry = this.entry;
4886
- if (entry === undefined)
4887
- return;
5227
+ this.polling = true;
5228
+ try {
5229
+ const waiting = [...this.entries.values()].filter((entry) => this.settleByPoll(entry, view));
5230
+ const abort = waiting.find((entry) => entry.abandoned !== undefined);
5231
+ if (abort !== undefined)
5232
+ return this.sendAbort(abort);
5233
+ const next = waiting.find((entry) => entry.state === "queued" || entry.state === "dispatched");
5234
+ return next === undefined ? undefined : this.dispatch(next);
5235
+ } finally {
5236
+ this.polling = false;
5237
+ if (this.changePending) {
5238
+ this.changePending = false;
5239
+ queueMicrotask(() => this.changed());
5240
+ }
5241
+ }
5242
+ }
5243
+ settleByPoll(entry, view) {
4888
5244
  const { taskId } = entry.task;
4889
5245
  const listed2 = view.inflight.has(taskId);
4890
5246
  if (entry.abandoned !== undefined) {
4891
- if (!entry.delivered) {
5247
+ if (!entry.delivered || entry.abortDelivered && !listed2) {
4892
5248
  this.release(entry);
4893
- return;
4894
- }
4895
- if (entry.abortDelivered && !listed2) {
4896
- this.release(entry);
4897
- return;
4898
- }
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 };
5249
+ return false;
4905
5250
  }
4906
- return;
5251
+ return !entry.abortDelivered || this.now() - entry.abortSentAt >= this.abortResendMs;
4907
5252
  }
4908
5253
  if ((entry.state === "acked" || entry.state === "running") && !listed2) {
4909
5254
  this.settle(entry, failure("extension-reloaded", `extension lost task ${taskId} (${entry.task.op})`));
4910
5255
  this.release(entry);
4911
- return;
5256
+ return false;
4912
5257
  }
4913
5258
  if (entry.state === "dispatched" && listed2) {
4914
5259
  entry.state = "acked";
4915
5260
  clearTimeout(entry.ackTimer);
4916
- return;
5261
+ return false;
4917
5262
  }
4918
5263
  if (entry.state === "queued") {
4919
5264
  const refused = policyFailure(view.policy, entry.task.op);
4920
5265
  if (refused !== undefined) {
4921
5266
  this.settle(entry, refused);
4922
5267
  this.release(entry);
4923
- return;
5268
+ return false;
4924
5269
  }
4925
5270
  if (view.ops !== null && !view.ops.has(entry.task.op)) {
4926
5271
  this.settle(entry, failure("unsupported-op", `the extension does not offer ${entry.task.op}`, {
4927
5272
  available: [...view.ops]
4928
5273
  }));
4929
5274
  this.release(entry);
4930
- return;
5275
+ return false;
4931
5276
  }
4932
- return this.dispatch(entry);
5277
+ return true;
4933
5278
  }
4934
- if (entry.state === "dispatched")
4935
- return this.dispatch(entry);
4936
- return;
5279
+ return entry.state === "dispatched";
5280
+ }
5281
+ sendAbort(entry) {
5282
+ entry.abortDelivered = true;
5283
+ entry.abortSentAt = this.now();
5284
+ clearTimeout(entry.resendTimer);
5285
+ entry.resendTimer = setTimeout(() => this.changed(), this.abortResendMs);
5286
+ return { kind: "abort", taskId: entry.task.taskId, reason: entry.abandoned ?? "cancelled" };
4937
5287
  }
4938
5288
  ack(taskId) {
4939
5289
  const entry = this.match(taskId);
@@ -4992,13 +5342,8 @@ class TaskSlot {
4992
5342
  this.release(entry);
4993
5343
  return reply;
4994
5344
  }
4995
- abandonCurrent(reason) {
4996
- if (this.entry !== undefined)
4997
- this.abandon(this.entry, reason);
4998
- }
4999
5345
  close() {
5000
- const entry = this.entry;
5001
- if (entry !== undefined) {
5346
+ for (const entry of this.entries.values()) {
5002
5347
  this.settle(entry, failure("server-unreachable", "voyager serve stopped"));
5003
5348
  this.release(entry);
5004
5349
  }
@@ -5012,7 +5357,7 @@ class TaskSlot {
5012
5357
  entry.state = "dispatched";
5013
5358
  clearTimeout(entry.ackTimer);
5014
5359
  entry.ackTimer = setTimeout(() => {
5015
- if (this.entry === entry && entry.state === "dispatched" && entry.abandoned === undefined) {
5360
+ if (this.holds(entry) && entry.state === "dispatched" && entry.abandoned === undefined) {
5016
5361
  entry.state = "queued";
5017
5362
  this.changed();
5018
5363
  }
@@ -5020,7 +5365,7 @@ class TaskSlot {
5020
5365
  return { kind: "task", task: { ...entry.task } };
5021
5366
  }
5022
5367
  abandon(entry, reason) {
5023
- if (this.entry !== entry || entry.abandoned !== undefined)
5368
+ if (!this.holds(entry) || entry.abandoned !== undefined)
5024
5369
  return;
5025
5370
  const message = reason === "timeout" ? `${entry.task.op} timed out` : "job was cancelled";
5026
5371
  this.settle(entry, failure(reason, message));
@@ -5036,11 +5381,10 @@ class TaskSlot {
5036
5381
  armOffline() {
5037
5382
  clearTimeout(this.offlineTimer);
5038
5383
  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);
5384
+ for (const entry of this.entries.values()) {
5385
+ this.settle(entry, failure("extension-offline", `no poll from the extension for ${this.extensionOfflineMs / 1000}s`));
5386
+ this.release(entry);
5387
+ }
5044
5388
  }, this.extensionOfflineMs);
5045
5389
  }
5046
5390
  settle(entry, error, value) {
@@ -5053,13 +5397,16 @@ class TaskSlot {
5053
5397
  entry.resolve(value);
5054
5398
  }
5055
5399
  release(entry) {
5056
- if (this.entry !== entry)
5400
+ if (!this.holds(entry))
5057
5401
  return;
5058
5402
  clearTimeout(entry.ackTimer);
5059
5403
  clearTimeout(entry.deadlineTimer);
5060
5404
  clearTimeout(entry.resendTimer);
5061
- clearTimeout(this.offlineTimer);
5062
- this.offlineTimer = undefined;
5405
+ this.entries.delete(entry.task.taskId);
5406
+ if (this.entries.size === 0) {
5407
+ clearTimeout(this.offlineTimer);
5408
+ this.offlineTimer = undefined;
5409
+ }
5063
5410
  this.finished.set(entry.task.taskId, entry.abandoned === undefined ? "done" : "abandoned");
5064
5411
  while (this.finished.size > FINISHED_MEMORY) {
5065
5412
  const oldest = this.finished.keys().next().value;
@@ -5067,38 +5414,37 @@ class TaskSlot {
5067
5414
  break;
5068
5415
  this.finished.delete(oldest);
5069
5416
  }
5070
- this.entry = undefined;
5071
- for (const wake of this.idleWaiters.splice(0))
5072
- wake();
5417
+ for (const waiter of this.idleWaiters.filter((item) => this.isIdle(item.jobId))) {
5418
+ this.idleWaiters.splice(this.idleWaiters.indexOf(waiter), 1);
5419
+ waiter.wake();
5420
+ }
5073
5421
  this.changed();
5074
5422
  }
5423
+ holds(entry) {
5424
+ return this.entries.get(entry.task.taskId) === entry;
5425
+ }
5426
+ isIdle(jobId) {
5427
+ if (jobId === undefined)
5428
+ return this.entries.size === 0;
5429
+ return [...this.entries.values()].every((entry) => entry.task.jobId !== jobId);
5430
+ }
5075
5431
  match(taskId) {
5076
- return this.entry?.task.taskId === taskId ? this.entry : undefined;
5432
+ return this.entries.get(taskId);
5077
5433
  }
5078
5434
  unknown(taskId) {
5079
5435
  return this.finished.has(taskId) ? { kind: "gone" } : { kind: "not-found" };
5080
5436
  }
5081
5437
  changed() {
5438
+ if (this.polling) {
5439
+ this.changePending = true;
5440
+ return;
5441
+ }
5082
5442
  for (const listener of this.listeners)
5083
5443
  listener();
5084
5444
  }
5085
5445
  }
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
- };
5446
+ function view(entry) {
5447
+ return { taskId: entry.task.taskId, jobId: entry.task.jobId, op: entry.task.op };
5102
5448
  }
5103
5449
 
5104
5450
  // src/util/log.ts
@@ -5357,6 +5703,19 @@ var JOB = "([A-Za-z0-9_-]{1,128})";
5357
5703
  var MAX_EVENTS_WAIT_S = 25;
5358
5704
  function cliRoutes(core) {
5359
5705
  const { pairing, extension, runner, store, registry } = core;
5706
+ let warnedLimit;
5707
+ const limits = async () => {
5708
+ const config = await loadConfig(core.paths).catch((error) => {
5709
+ core.log.warn("config.json could not be read", { error: String(error) });
5710
+ return { maxParallelJobs: undefined, invalidMaxParallelJobs: undefined };
5711
+ });
5712
+ const invalid = config.invalidMaxParallelJobs;
5713
+ if (invalid !== undefined && invalid !== warnedLimit) {
5714
+ core.log.warn("maxParallelJobs in config.json is not a whole number of 1 or more; running one job at a time", { maxParallelJobs: invalid });
5715
+ }
5716
+ warnedLimit = invalid;
5717
+ return config.maxParallelJobs === undefined ? {} : { maxParallelJobs: config.maxParallelJobs };
5718
+ };
5360
5719
  const requireJob = async (jobId) => {
5361
5720
  const job = await store.get(jobId);
5362
5721
  if (job === undefined)
@@ -5368,9 +5727,12 @@ function cliRoutes(core) {
5368
5727
  method: "GET",
5369
5728
  pattern: /^\/v1\/status$/,
5370
5729
  access: "cli",
5371
- handle: () => {
5730
+ handle: async () => {
5372
5731
  const current = pairing.current;
5373
- const activeJob = runner.activeJob();
5732
+ const { maxParallelJobs } = await limits();
5733
+ const activeJobs = runner.activeJobs();
5734
+ const first = activeJobs[0];
5735
+ const activeJob = first === undefined ? undefined : { jobId: first.jobId, ...first.op === undefined ? {} : { op: first.op } };
5374
5736
  const data = {
5375
5737
  extension: {
5376
5738
  paired: current !== null,
@@ -5378,9 +5740,11 @@ function cliRoutes(core) {
5378
5740
  version: current?.extensionVersion ?? null,
5379
5741
  browser: current?.browser ?? null,
5380
5742
  policy: extension.policy,
5381
- account: extension.account
5743
+ account: extension.account,
5744
+ maxTasks: extension.maxTasks
5382
5745
  },
5383
- ...activeJob === undefined ? {} : { activeJob },
5746
+ ...activeJob === undefined ? {} : { activeJob, activeJobs },
5747
+ maxJobs: jobCapacity(extension.maxTasks, maxParallelJobs),
5384
5748
  serverVersion: CLI_VERSION
5385
5749
  };
5386
5750
  return { status: 200, data };
@@ -5432,14 +5796,15 @@ function cliRoutes(core) {
5432
5796
  method: "POST",
5433
5797
  pattern: /^\/v1\/jobs$/,
5434
5798
  access: "cli",
5435
- handle: ({ body }) => {
5799
+ handle: async ({ body }) => {
5436
5800
  const checked = validateMessage("jobSubmitRequest", body);
5437
5801
  if (!checked.ok)
5438
5802
  throw failure("bad-request", "invalid job", { issues: checked.issues });
5439
5803
  const { plugin, command, input } = checked.value;
5440
5804
  if (plugin === CALL_PLUGIN)
5441
5805
  throw failure("bad-request", `use ${BRIDGE_PATHS.call} for single ops`);
5442
- const job = runner.submit(plugin, command, input);
5806
+ const { maxParallelJobs } = await limits();
5807
+ const job = runner.submit(plugin, command, input, maxParallelJobs === undefined ? {} : { maxParallelJobs });
5443
5808
  const data = { jobId: job.jobId };
5444
5809
  return { status: 202, data };
5445
5810
  }
@@ -5547,9 +5912,9 @@ async function readBlob(core, ctx) {
5547
5912
  const blob = core.blobs.get(ctx.params[0]);
5548
5913
  if (blob === undefined)
5549
5914
  throw failure("not-found", "no such blob");
5550
- const task = core.slot.current();
5915
+ const task = core.slot.taskOf(blob.jobId);
5551
5916
  const uploads = task?.op === "composer.attach" || task?.op === "project.upload";
5552
- if (task === undefined || task.jobId !== blob.jobId || !uploads) {
5917
+ if (task === undefined || !uploads) {
5553
5918
  throw failure("gone", "no upload of this blob is running");
5554
5919
  }
5555
5920
  const { offset, length } = checked.value;
@@ -5560,8 +5925,8 @@ async function readBlob(core, ctx) {
5560
5925
  return { status: 200, data: chunk };
5561
5926
  }
5562
5927
  function requireDownloadTask(core, jobId, taskId) {
5563
- const task = core.slot.current();
5564
- if (task?.op !== "files.download" || task.jobId !== jobId || task.taskId !== taskId) {
5928
+ const task = core.slot.taskOf(jobId);
5929
+ if (task?.op !== "files.download" || task.taskId !== taskId) {
5565
5930
  throw failure("gone", "no files.download of this job is running as that task");
5566
5931
  }
5567
5932
  }
@@ -5618,16 +5983,17 @@ async function poll(core, ctx) {
5618
5983
  core.extension.ops = request.capabilities.ops;
5619
5984
  core.extension.policy = request.policy;
5620
5985
  core.extension.account = request.account ?? null;
5986
+ core.extension.maxTasks = request.maxTasks ?? null;
5621
5987
  core.slot.notePoll();
5622
5988
  core.heldPoll?.();
5623
5989
  const token = bearer(ctx.req);
5624
5990
  const stillPaired = () => core.pairing.checkToken(token, ctx.origin) === "ok";
5625
- const view = {
5991
+ const view2 = {
5626
5992
  inflight: new Set(request.inflight.map((entry) => entry.taskId)),
5627
5993
  ops: core.extension.opNames(),
5628
5994
  policy: request.policy.state
5629
5995
  };
5630
- const immediate = core.slot.poll(view);
5996
+ const immediate = core.slot.poll(view2);
5631
5997
  if (immediate !== undefined)
5632
5998
  return { status: 200, data: immediate };
5633
5999
  const held = await new Promise((resolve2) => {
@@ -5651,7 +6017,7 @@ async function poll(core, ctx) {
5651
6017
  end();
5652
6018
  return;
5653
6019
  }
5654
- const response = core.slot.poll(view);
6020
+ const response = core.slot.poll(view2);
5655
6021
  if (response !== undefined)
5656
6022
  finish(response);
5657
6023
  });
@@ -5827,7 +6193,8 @@ async function startBridgeServer(options) {
5827
6193
  isPaired: () => pairing.current !== null,
5828
6194
  log,
5829
6195
  rememberConversation: (entry) => recordLastConversation(options.paths, entry),
5830
- pluginState: (plugin) => pluginStateStore(options.paths, plugin)
6196
+ pluginState: (plugin) => pluginStateStore(options.paths, plugin),
6197
+ ...options.pageTurns === undefined ? {} : { pageTurns: options.pageTurns }
5831
6198
  });
5832
6199
  const core = {
5833
6200
  paths: options.paths,
@@ -6018,7 +6385,7 @@ async function readLockPid(paths) {
6018
6385
 
6019
6386
  // src/cli/commands/serve.ts
6020
6387
  async function serveCommand(paths, argv) {
6021
- const args = parse(argv, {
6388
+ const args = parse2(argv, {
6022
6389
  foreground: { type: "boolean", summary: "run in this terminal" },
6023
6390
  daemon: { type: "boolean", summary: "internal: run detached" },
6024
6391
  stop: { type: "boolean", summary: "stop the running serve" },
@@ -6405,7 +6772,7 @@ async function pairedExtensionVersion(deps) {
6405
6772
  }
6406
6773
  }
6407
6774
  async function updateCommand(paths, argv, deps = defaultUpdateDeps(paths)) {
6408
- const args = parse(argv, { json: { type: "boolean", summary: "JSON output" } });
6775
+ const args = parse2(argv, { json: { type: "boolean", summary: "JSON output" } });
6409
6776
  const cli = await updateCli(deps);
6410
6777
  const serve = await restartServe(deps, cli.serveTarget);
6411
6778
  const page = await refreshPageData(deps);
@@ -6464,7 +6831,7 @@ usage:
6464
6831
  skips one unchanged; a folder goes up as <folder>.zip
6465
6832
  | remove <project> <file> [--yes] deletes at once, no undo; asks, or needs --yes without a tty
6466
6833
  [--keep-tab] any project command: leave its ChatGPT tab open
6467
- voyager status [--json] serve, pairing and active job
6834
+ voyager status [--json] serve, pairing, active jobs and how many may run at once
6468
6835
  voyager commands [--json] plugin commands and extension ops
6469
6836
  voyager call <op> [--params JSON] run one extension op (debugging); files.download saves the file
6470
6837
  voyager job list | show <id> | wait <id> [--timeout 30m] | cancel <id>
@@ -6479,7 +6846,8 @@ each job opens its own ChatGPT tab and closes it when it ends, also after a fail
6479
6846
  unless --keep-tab; one you need to look at (sign-in, an unconfirmed send) stays open
6480
6847
  results name the ChatGPT plan the pages last showed (JSON: account; may trail a change)
6481
6848
  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
6849
+ 5 busy (one job at a time, unless parallel jobs are on in the Voyager popup),
6850
+ 6 transient/detached/reply-stalled, 7 not paired or extension offline
6483
6851
  a reply saved with some files not downloaded is 0: see failedFiles (JSON) or stderr
6484
6852
  state: ~/.voyager (override with VOYAGER_HOME)`;
6485
6853
  async function main(argv) {