@moxt-ai/cli 0.10.0-beta.7 → 0.10.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -8728,9 +8728,11 @@ function createActiveAttempt(claim, durable, workingDirectory) {
8728
8728
  eventUploadQueue: Promise.resolve(),
8729
8729
  fatal,
8730
8730
  finished: new AbortController(),
8731
+ ignoreProcessLoss: false,
8731
8732
  interruptedCleanupStarted: false,
8732
8733
  leaseDeadlineMilliseconds: parseLeaseDeadline(claim.leaseExpiresAt),
8733
8734
  nativeThreadId: durable.snapshot().nativeThreadId ?? claim.nativeThreadId,
8735
+ processQuarantined: false,
8734
8736
  spoolQueue: Promise.resolve(),
8735
8737
  subagentThreadIds: /* @__PURE__ */ new Set(),
8736
8738
  terminal,
@@ -8820,6 +8822,24 @@ function appServerMessageThreadId(message) {
8820
8822
  }
8821
8823
  return optionalString2(safeRecord(params.thread).id);
8822
8824
  }
8825
+ function appServerMessageTurnId(message) {
8826
+ const params = safeRecord(message.params);
8827
+ const direct = optionalString2(params.turnId).value;
8828
+ if (direct !== void 0) {
8829
+ return { value: direct };
8830
+ }
8831
+ return optionalString2(safeRecord(params.turn).id);
8832
+ }
8833
+ function activeOwnsThread(active, threadId) {
8834
+ return active.nativeThreadId === threadId || active.subagentThreadIds.has(threadId) || active.initialThreadName?.generation?.threadId === threadId;
8835
+ }
8836
+ function singleActiveAttempt(attempts) {
8837
+ if (attempts.length !== 1) {
8838
+ return {};
8839
+ }
8840
+ const value = attempts[0];
8841
+ return value === void 0 ? {} : { value };
8842
+ }
8823
8843
  function normalizeThreadTitle(value) {
8824
8844
  const title = Array.from(value.trim().replace(/\s+/g, " ")).slice(0, maximumThreadTitleCharacters).join("");
8825
8845
  return title === "" ? {} : { value: title };
@@ -9120,6 +9140,16 @@ async function waitForAbort(signal, milliseconds) {
9120
9140
  signal.addEventListener("abort", complete, { once: true });
9121
9141
  });
9122
9142
  }
9143
+ function rejectWhenAborted(signal) {
9144
+ return new Promise((_resolve, reject) => {
9145
+ const rejectStopped = () => reject(new WorkerStoppedError());
9146
+ if (signal.aborted) {
9147
+ rejectStopped();
9148
+ return;
9149
+ }
9150
+ signal.addEventListener("abort", rejectStopped, { once: true });
9151
+ });
9152
+ }
9123
9153
  function parseLeaseDeadline(value) {
9124
9154
  const parsed = Date.parse(value);
9125
9155
  if (!Number.isFinite(parsed)) {
@@ -9127,7 +9157,7 @@ function parseLeaseDeadline(value) {
9127
9157
  }
9128
9158
  return parsed;
9129
9159
  }
9130
- var import_node_child_process4, import_promises13, import_node_os7, import_node_path11, heartbeatIntervalMilliseconds, claimRetryMilliseconds, claimWaitSeconds, discoveryWaitSeconds, discoveryRetryMilliseconds, maximumDiscoveryDirectories, maximumDiscoveryModels, maximumModelListPages, eventUploadCoalesceMilliseconds, eventUploadRetryMilliseconds, maximumEventUploadCount, preferredEventUploadRequestBytes, leaseRenewIntervalMilliseconds, leaseRenewRetryMilliseconds, interactionTimeoutMilliseconds, executableProbeTimeoutMilliseconds, recoveryRetryMilliseconds, initialThreadNameRetryMilliseconds, maximumThreadNameRetryMilliseconds, maximumInitialThreadNameAttempts, maximumThreadTitleCharacters, maximumThreadTitleSourceCharacters, threadTitleGenerationTimeoutMilliseconds, interruptedExecutionCleanupTimeoutMilliseconds, threadTitleGenerationInstructions, threadTitleGenerationOutputSchema, ThreadUnavailableError, LeaseLostError, SpoolPersistenceError, UnknownTurnStartResultError, LocalCodexRuntimeConnectionWorker;
9160
+ var import_node_child_process4, import_promises13, import_node_os7, import_node_path11, heartbeatIntervalMilliseconds, claimRetryMilliseconds, claimWaitSeconds, discoveryWaitSeconds, discoveryRetryMilliseconds, maximumDiscoveryDirectories, maximumDiscoveryModels, maximumModelListPages, eventUploadCoalesceMilliseconds, eventUploadRetryMilliseconds, maximumEventUploadCount, preferredEventUploadRequestBytes, leaseRenewIntervalMilliseconds, leaseRenewRetryMilliseconds, interactionTimeoutMilliseconds, executableProbeTimeoutMilliseconds, recoveryRetryMilliseconds, initialThreadNameRetryMilliseconds, maximumThreadNameRetryMilliseconds, maximumInitialThreadNameAttempts, maximumThreadTitleCharacters, maximumThreadTitleSourceCharacters, threadTitleGenerationTimeoutMilliseconds, interruptedExecutionCleanupTimeoutMilliseconds, threadTitleGenerationInstructions, threadTitleGenerationOutputSchema, ThreadUnavailableError, LeaseLostError, SpoolPersistenceError, UnknownTurnStartResultError, WorkerStoppedError, LocalCodexRuntimeConnectionWorker;
9131
9161
  var init_runtime_worker = __esm({
9132
9162
  "../machine-connector/dist/runtime/local-codex-runtime/runtime-worker.js"() {
9133
9163
  "use strict";
@@ -9203,6 +9233,12 @@ var init_runtime_worker = __esm({
9203
9233
  this.name = "UnknownTurnStartResultError";
9204
9234
  }
9205
9235
  };
9236
+ WorkerStoppedError = class extends Error {
9237
+ constructor() {
9238
+ super("Local Codex Runtime worker stopped");
9239
+ this.name = "WorkerStoppedError";
9240
+ }
9241
+ };
9206
9242
  LocalCodexRuntimeConnectionWorker = class {
9207
9243
  connection;
9208
9244
  relay;
@@ -9211,9 +9247,10 @@ var init_runtime_worker = __esm({
9211
9247
  environment;
9212
9248
  connectorInstanceId;
9213
9249
  logger;
9214
- active = {};
9250
+ activeAttempts = /* @__PURE__ */ new Map();
9215
9251
  configured = false;
9216
9252
  enabled = false;
9253
+ executionTasks = /* @__PURE__ */ new Map();
9217
9254
  lastCapabilityStartError;
9218
9255
  lifetimeAbort = new AbortController();
9219
9256
  registered = true;
@@ -9242,13 +9279,55 @@ var init_runtime_worker = __esm({
9242
9279
  }
9243
9280
  revoke() {
9244
9281
  this.lifetimeAbort.abort();
9245
- this.active.value?.finished.abort();
9282
+ for (const active of this.activeAttempts.values()) {
9283
+ active.finished.abort();
9284
+ }
9246
9285
  }
9247
9286
  status() {
9248
9287
  return this.processPool.status();
9249
9288
  }
9289
+ ownsMessage(message) {
9290
+ return this.activeAttemptForMessage(message).value !== void 0;
9291
+ }
9292
+ activeAttemptForMessage(message) {
9293
+ const threadId = appServerMessageThreadId(message).value;
9294
+ const turnId = appServerMessageTurnId(message).value;
9295
+ if (threadId === void 0 && turnId === void 0) {
9296
+ return {};
9297
+ }
9298
+ if (turnId !== void 0) {
9299
+ const exactTurnMatches = [...this.activeAttempts.values()].filter((active) => {
9300
+ if (active.finished.signal.aborted) {
9301
+ return false;
9302
+ }
9303
+ const generation = active.initialThreadName?.generation;
9304
+ const ownsTurn = active.nativeTurnId === turnId || generation?.turnId === turnId;
9305
+ return ownsTurn && (threadId === void 0 || activeOwnsThread(active, threadId));
9306
+ });
9307
+ if (exactTurnMatches.length > 0) {
9308
+ return singleActiveAttempt(exactTurnMatches);
9309
+ }
9310
+ }
9311
+ if (threadId === void 0) {
9312
+ return {};
9313
+ }
9314
+ const threadMatches = [...this.activeAttempts.values()].filter((active) => {
9315
+ if (active.finished.signal.aborted) {
9316
+ return false;
9317
+ }
9318
+ const generation = active.initialThreadName?.generation;
9319
+ const ownsVisibleThread = active.nativeThreadId === threadId;
9320
+ const ownsSubagentThread = active.subagentThreadIds.has(threadId);
9321
+ const ownsTitleThread = generation?.threadId === threadId;
9322
+ if (turnId === void 0) {
9323
+ return ownsVisibleThread || ownsSubagentThread || ownsTitleThread;
9324
+ }
9325
+ return ownsSubagentThread || ownsVisibleThread && active.nativeTurnId === void 0 || ownsTitleThread && generation?.turnId === void 0;
9326
+ });
9327
+ return singleActiveAttempt(threadMatches);
9328
+ }
9250
9329
  async onNotification(message) {
9251
- const active = this.active.value;
9330
+ const active = this.activeAttemptForMessage(message).value;
9252
9331
  if (active === void 0 || active.finished.signal.aborted) {
9253
9332
  return;
9254
9333
  }
@@ -9290,7 +9369,13 @@ var init_runtime_worker = __esm({
9290
9369
  initialThreadName.terminalObserved = true;
9291
9370
  }
9292
9371
  if (normalized.terminalStatus === LocalCodexTurnStatusWire.Interrupted) {
9293
- await this.stopInterruptedExecution(active, normalized);
9372
+ if (active.interruptedCleanupTask === void 0) {
9373
+ const task = this.stopInterruptedExecution(active, normalized).catch((error) => {
9374
+ this.signalFatal(active, error instanceof Error ? error : new Error(String(error)));
9375
+ });
9376
+ active.interruptedCleanupTask = task;
9377
+ void task;
9378
+ }
9294
9379
  return;
9295
9380
  }
9296
9381
  if (normalized.terminalStatus !== void 0 && initialThreadName !== void 0 && !initialThreadName.completed) {
@@ -9317,46 +9402,60 @@ var init_runtime_worker = __esm({
9317
9402
  void this.enqueueEventFlush(active).catch(() => void 0);
9318
9403
  }
9319
9404
  async stopInterruptedExecution(active, normalized) {
9405
+ if (active.interruptedCleanupStarted) {
9406
+ return;
9407
+ }
9320
9408
  active.interruptedCleanupStarted = true;
9409
+ active.ignoreProcessLoss = true;
9321
9410
  active.initialThreadName?.generation?.resolve();
9322
9411
  const client = this.processPool.currentClient().value;
9412
+ let cleanupFailed = false;
9323
9413
  if (client !== void 0 && active.nativeThreadId !== void 0) {
9324
9414
  await Promise.all([active.nativeThreadId, ...active.subagentThreadIds].map(async (threadId) => {
9325
9415
  try {
9326
9416
  await client.requestWithin("thread/backgroundTerminals/clean", { threadId }, interruptedExecutionCleanupTimeoutMilliseconds);
9327
9417
  } catch (error) {
9328
- await this.logger.error(`Failed to clean background terminals for interrupted Local Codex thread ${threadId}; stopping its App Server process tree`, error);
9418
+ cleanupFailed = true;
9419
+ await this.logger.error(`Failed to clean background terminals for interrupted Local Codex thread ${threadId}; quarantining the shared App Server`, error);
9329
9420
  }
9330
9421
  }));
9331
9422
  }
9332
- try {
9333
- await this.processPool.quarantineCurrentProcess(active.processTreeCapture);
9334
- } catch (error) {
9335
- const failure = error instanceof Error ? error : new AppServerProcessLostError(String(error));
9336
- await this.enqueueSpool(active, async () => {
9337
- const terminal = await active.durable.appendEvent("turn_failed", {
9338
- message: failure.message,
9339
- status: "runtime_lost"
9340
- }, {
9341
- nativeThreadId: active.nativeThreadId,
9342
- nativeTurnId: active.nativeTurnId
9423
+ const quarantineProcess = cleanupFailed || !this.processPool.hasOtherActiveExecution();
9424
+ if (quarantineProcess) {
9425
+ active.processQuarantined = true;
9426
+ try {
9427
+ if (client === void 0) {
9428
+ await this.processPool.quarantineCurrentProcess(active.processTreeCapture);
9429
+ } else {
9430
+ await this.processPool.quarantineProcess(client, active.processTreeCapture);
9431
+ }
9432
+ } catch (error) {
9433
+ const failure = error instanceof Error ? error : new AppServerProcessLostError(String(error));
9434
+ await this.enqueueSpool(active, async () => {
9435
+ const terminal = await active.durable.appendEvent("turn_failed", {
9436
+ message: failure.message,
9437
+ status: "runtime_lost"
9438
+ }, {
9439
+ nativeThreadId: active.nativeThreadId,
9440
+ nativeTurnId: active.nativeTurnId
9441
+ });
9442
+ const result = {
9443
+ errorCode: "runtime_lost",
9444
+ errorMessage: failure.message,
9445
+ finalSequence: terminal.sequence,
9446
+ status: LocalCodexTurnStatusWire.RuntimeLost
9447
+ };
9448
+ await active.durable.markTerminal(result);
9449
+ this.settleTerminal(active, result);
9343
9450
  });
9344
- const result = {
9345
- errorCode: "runtime_lost",
9346
- errorMessage: failure.message,
9347
- finalSequence: terminal.sequence,
9348
- status: LocalCodexTurnStatusWire.RuntimeLost
9349
- };
9350
- await active.durable.markTerminal(result);
9351
- this.settleTerminal(active, result);
9352
- });
9353
- return;
9451
+ return;
9452
+ }
9354
9453
  }
9355
9454
  this.completeInitialThreadName(active);
9356
9455
  this.persistNormalizedNotification(active, normalized);
9357
9456
  }
9358
9457
  async onServerRequest(message) {
9359
- const active = this.active.value;
9458
+ const active = this.activeAttemptForMessage(message).value;
9360
9459
  const client = this.processPool.currentClient().value;
9361
9460
  const requestId = rpcIdentifier2(message.id).value;
9362
9461
  if (active === void 0 || client === void 0 || requestId === void 0 || message.method === void 0) {
@@ -9398,29 +9497,27 @@ var init_runtime_worker = __esm({
9398
9497
  await this.enqueueEventFlush(active);
9399
9498
  }
9400
9499
  async onProcessExit(error) {
9401
- const active = this.active.value;
9402
- if (active === void 0 || !active.acceptedByAppServer) {
9403
- return;
9404
- }
9405
- active.initialThreadName?.generation?.resolve();
9406
- await this.enqueueSpool(active, async () => {
9407
- await active.durable.appendEvent("runtime_error", { message: error.message }, {
9408
- nativeThreadId: active.nativeThreadId,
9409
- nativeTurnId: active.nativeTurnId
9410
- });
9411
- const terminal = await active.durable.appendEvent("turn_failed", { status: "runtime_lost" }, {
9412
- nativeThreadId: active.nativeThreadId,
9413
- nativeTurnId: active.nativeTurnId
9500
+ await Promise.all([...this.activeAttempts.values()].filter((active) => active.acceptedByAppServer && !active.finished.signal.aborted && !active.ignoreProcessLoss && active.terminalValue === void 0).map(async (active) => {
9501
+ active.initialThreadName?.generation?.resolve();
9502
+ await this.enqueueSpool(active, async () => {
9503
+ await active.durable.appendEvent("runtime_error", { message: error.message }, {
9504
+ nativeThreadId: active.nativeThreadId,
9505
+ nativeTurnId: active.nativeTurnId
9506
+ });
9507
+ const terminal = await active.durable.appendEvent("turn_failed", { status: "runtime_lost" }, {
9508
+ nativeThreadId: active.nativeThreadId,
9509
+ nativeTurnId: active.nativeTurnId
9510
+ });
9511
+ const result = {
9512
+ errorCode: "runtime_lost",
9513
+ errorMessage: error.message,
9514
+ finalSequence: terminal.sequence,
9515
+ status: LocalCodexTurnStatusWire.RuntimeLost
9516
+ };
9517
+ await active.durable.markTerminal(result);
9518
+ this.settleTerminal(active, result);
9414
9519
  });
9415
- const result = {
9416
- errorCode: "runtime_lost",
9417
- errorMessage: error.message,
9418
- finalSequence: terminal.sequence,
9419
- status: LocalCodexTurnStatusWire.RuntimeLost
9420
- };
9421
- await active.durable.markTerminal(result);
9422
- this.settleTerminal(active, result);
9423
- });
9520
+ }));
9424
9521
  }
9425
9522
  async runLoops() {
9426
9523
  try {
@@ -9431,13 +9528,16 @@ var init_runtime_worker = __esm({
9431
9528
  this.runRecoveryLoop()
9432
9529
  ]);
9433
9530
  } finally {
9434
- this.active.value?.finished.abort();
9531
+ for (const active of this.activeAttempts.values()) {
9532
+ active.finished.abort();
9533
+ }
9534
+ await Promise.all(this.executionTasks.values());
9435
9535
  this.unregisterRoute();
9436
9536
  }
9437
9537
  }
9438
9538
  async runHeartbeatLoop() {
9439
9539
  while (!this.lifetimeAbort.signal.aborted && !this.relay.isRevoked()) {
9440
- if (this.active.value?.interruptedCleanupStarted) {
9540
+ if ([...this.activeAttempts.values()].some((active) => active.interruptedCleanupStarted)) {
9441
9541
  await waitForAbort(this.lifetimeAbort.signal, claimRetryMilliseconds);
9442
9542
  continue;
9443
9543
  }
@@ -9459,10 +9559,12 @@ var init_runtime_worker = __esm({
9459
9559
  await waitForAbort(this.lifetimeAbort.signal, claimRetryMilliseconds);
9460
9560
  continue;
9461
9561
  }
9462
- if (!this.processPool.tryAcquire(this.connection.connectionId)) {
9562
+ const slot = this.processPool.tryAcquireExecutionSlot(this.connection.connectionId).value;
9563
+ if (slot === void 0) {
9463
9564
  await waitForAbort(this.lifetimeAbort.signal, claimRetryMilliseconds);
9464
9565
  continue;
9465
9566
  }
9567
+ let executionScheduled = false;
9466
9568
  try {
9467
9569
  const capability = this.processPool.status();
9468
9570
  const response = await this.relay.claimCodexRuntime({
@@ -9473,21 +9575,33 @@ var init_runtime_worker = __esm({
9473
9575
  waitSeconds: claimWaitSeconds
9474
9576
  }, this.lifetimeAbort.signal);
9475
9577
  if (response.attempt === void 0) {
9476
- this.processPool.release(this.connection.connectionId);
9477
9578
  continue;
9478
9579
  }
9479
- await this.executeAttempt(response.attempt);
9580
+ this.scheduleExecution(slot, response.attempt);
9581
+ executionScheduled = true;
9480
9582
  } catch (error) {
9481
9583
  if (!this.lifetimeAbort.signal.aborted) {
9482
9584
  await this.logger.error(`Local Codex Runtime claim loop failed for connection ${this.connection.connectionId}`, error);
9483
9585
  await waitForAbort(this.lifetimeAbort.signal, eventUploadRetryMilliseconds);
9484
9586
  }
9485
9587
  } finally {
9486
- this.active = {};
9487
- this.processPool.release(this.connection.connectionId);
9588
+ if (!executionScheduled) {
9589
+ this.processPool.releaseExecutionSlot(slot);
9590
+ }
9488
9591
  }
9489
9592
  }
9490
9593
  }
9594
+ scheduleExecution(slot, claim) {
9595
+ if (this.executionTasks.has(claim.attemptId)) {
9596
+ throw new Error(`Local Codex attempt ${claim.attemptId} is already executing`);
9597
+ }
9598
+ this.processPool.markExecutionStarted(slot);
9599
+ const task = this.executeAttempt(claim).catch(async (error) => await this.logger.error(`Local Codex attempt ${claim.attemptId} execution crashed`, error).catch(() => void 0)).finally(() => {
9600
+ this.executionTasks.delete(claim.attemptId);
9601
+ this.processPool.releaseExecutionSlot(slot);
9602
+ });
9603
+ this.executionTasks.set(claim.attemptId, task);
9604
+ }
9491
9605
  async runDiscoveryLoop() {
9492
9606
  while (!this.lifetimeAbort.signal.aborted && !this.relay.isRevoked()) {
9493
9607
  try {
@@ -9590,7 +9704,10 @@ var init_runtime_worker = __esm({
9590
9704
  async executeAttempt(claim) {
9591
9705
  const durable = await DurableLocalCodexAttempt.create(this.store, this.connection.connectionId, this.connectorInstanceId, claim);
9592
9706
  const active = createActiveAttempt(claim, durable, this.connection.rootPath);
9593
- this.active = { value: active };
9707
+ if (this.activeAttempts.has(claim.attemptId)) {
9708
+ throw new Error(`Local Codex attempt ${claim.attemptId} is already active`);
9709
+ }
9710
+ this.activeAttempts.set(claim.attemptId, active);
9594
9711
  const leaseLoop = this.runLeaseLoop(active);
9595
9712
  const controlLoop = this.runControlLoop(active);
9596
9713
  try {
@@ -9667,7 +9784,8 @@ var init_runtime_worker = __esm({
9667
9784
  active.terminal,
9668
9785
  active.fatal.then((error) => {
9669
9786
  throw error;
9670
- })
9787
+ }),
9788
+ rejectWhenAborted(active.finished.signal)
9671
9789
  ]);
9672
9790
  await active.spoolQueue;
9673
9791
  await this.flushAllEvents(active);
@@ -9681,12 +9799,13 @@ var init_runtime_worker = __esm({
9681
9799
  });
9682
9800
  await durable.markUploaded();
9683
9801
  await this.store.deleteLocalCodexAttempt(claim.attemptId);
9684
- if (active.interruptedCleanupStarted) {
9802
+ if (active.processQuarantined) {
9685
9803
  active.finished.abort();
9686
9804
  await this.restoreAppServerAfterInterruption(active);
9687
9805
  }
9688
9806
  } catch (error) {
9689
- if (error instanceof LeaseLostError) {
9807
+ if (error instanceof WorkerStoppedError) {
9808
+ } else if (error instanceof LeaseLostError) {
9690
9809
  await this.logger.error(`Local Codex attempt ${claim.attemptId} stopped after losing its execution lease`, error);
9691
9810
  } else if (error instanceof SpoolPersistenceError) {
9692
9811
  try {
@@ -9701,17 +9820,22 @@ var init_runtime_worker = __esm({
9701
9820
  await this.failAttempt(active, error);
9702
9821
  }
9703
9822
  } finally {
9704
- this.completeInitialThreadName(active);
9705
- active.finished.abort();
9706
- await active.initialThreadName?.task;
9707
- const titleGenerationTurnId = active.initialThreadName?.generation?.turnId;
9708
- if (titleGenerationTurnId !== void 0) {
9709
- this.processPool.retireTurn(titleGenerationTurnId);
9710
- }
9711
- if (active.acceptedByAppServer && active.nativeTurnId !== void 0) {
9712
- this.processPool.retireTurn(active.nativeTurnId);
9823
+ try {
9824
+ this.completeInitialThreadName(active);
9825
+ active.finished.abort();
9826
+ await active.interruptedCleanupTask;
9827
+ await active.initialThreadName?.task;
9828
+ const titleGenerationTurnId = active.initialThreadName?.generation?.turnId;
9829
+ if (titleGenerationTurnId !== void 0) {
9830
+ this.processPool.retireTurn(titleGenerationTurnId);
9831
+ }
9832
+ if (active.acceptedByAppServer && active.nativeTurnId !== void 0) {
9833
+ this.processPool.retireTurn(active.nativeTurnId);
9834
+ }
9835
+ await Promise.all([leaseLoop, controlLoop]);
9836
+ } finally {
9837
+ this.activeAttempts.delete(claim.attemptId);
9713
9838
  }
9714
- await Promise.all([leaseLoop, controlLoop]);
9715
9839
  }
9716
9840
  }
9717
9841
  async prepareThread(client, active) {
@@ -9938,6 +10062,7 @@ var init_runtime_worker = __esm({
9938
10062
  async loseLease(active, message) {
9939
10063
  const error = new LeaseLostError(message);
9940
10064
  this.signalFatal(active, error);
10065
+ active.ignoreProcessLoss = true;
9941
10066
  await this.processPool.quarantineCurrentProcess();
9942
10067
  }
9943
10068
  async runControlLoop(active) {
@@ -10024,7 +10149,9 @@ var init_runtime_worker = __esm({
10024
10149
  if (!active.abortRequested || active.abortSent || active.nativeThreadId === void 0 || active.nativeTurnId === void 0) {
10025
10150
  return;
10026
10151
  }
10027
- active.processTreeCapture = (await this.processPool.captureCurrentProcessTree()).value;
10152
+ if (!this.processPool.hasOtherActiveExecution()) {
10153
+ active.processTreeCapture = (await this.processPool.captureCurrentProcessTree()).value;
10154
+ }
10028
10155
  await client.request("turn/interrupt", {
10029
10156
  threadId: active.nativeThreadId,
10030
10157
  turnId: active.nativeTurnId
@@ -10388,7 +10515,6 @@ ${titleInput}`,
10388
10515
  if (!this.registered) {
10389
10516
  return;
10390
10517
  }
10391
- this.processPool.release(this.connection.connectionId);
10392
10518
  this.processPool.unregister(this.connection.connectionId);
10393
10519
  this.registered = false;
10394
10520
  }
@@ -10407,18 +10533,20 @@ function localCodexProcessPoolKey(environment, nativeStoreIdentitySeed) {
10407
10533
  "connector-owned-app-server-stdio-v1"
10408
10534
  ].join("\0")).digest("hex");
10409
10535
  }
10410
- var import_node_crypto8, LocalCodexProcessPool;
10536
+ var import_node_crypto8, maximumConcurrentTurns, LocalCodexProcessPool;
10411
10537
  var init_process_pool = __esm({
10412
10538
  "../machine-connector/dist/runtime/local-codex-runtime/process-pool.js"() {
10413
10539
  "use strict";
10414
10540
  init_cjs_shims();
10415
10541
  import_node_crypto8 = require("crypto");
10416
10542
  init_app_server_client();
10543
+ maximumConcurrentTurns = 1e3;
10417
10544
  LocalCodexProcessPool = class {
10418
10545
  poolKey;
10419
10546
  logger;
10420
- activeOwner = {};
10547
+ activeSlots = /* @__PURE__ */ new Map();
10421
10548
  loadedThreadIds = /* @__PURE__ */ new Set();
10549
+ nextSlotId = 1;
10422
10550
  routes = /* @__PURE__ */ new Map();
10423
10551
  retiredTurnIds = /* @__PURE__ */ new Set();
10424
10552
  supervisor;
@@ -10428,7 +10556,7 @@ var init_process_pool = __esm({
10428
10556
  this.logger = logger;
10429
10557
  this.supervisor = new LocalCodexAppServerSupervisor(environment.HOME ?? process.cwd(), environment, nativeStoreIdentitySeed, {
10430
10558
  onNotification: async (message) => {
10431
- await this.activeRoute().value?.onNotification(message);
10559
+ await this.routeNotification(message);
10432
10560
  },
10433
10561
  onProcessExit: async (error, processGeneration) => {
10434
10562
  if (!this.supervisor.markProcessLost(processGeneration)) {
@@ -10436,18 +10564,10 @@ var init_process_pool = __esm({
10436
10564
  }
10437
10565
  this.loadedThreadIds.clear();
10438
10566
  await this.logger.error(`Shared Codex App Server generation ${processGeneration} exited`, error);
10439
- await this.activeRoute().value?.onProcessExit(error);
10567
+ await this.broadcastProcessExit(error);
10440
10568
  },
10441
10569
  onServerRequest: async (message) => {
10442
- const route = this.activeRoute().value;
10443
- if (route !== void 0) {
10444
- await route.onServerRequest(message);
10445
- return;
10446
- }
10447
- const client = this.supervisor.currentClient().value;
10448
- if (client !== void 0 && message.id !== void 0) {
10449
- await client.respondWithError(message.id, -32e3, "No owning Local Codex Runtime turn");
10450
- }
10570
+ await this.routeServerRequest(message);
10451
10571
  },
10452
10572
  onStderr: async (text3) => {
10453
10573
  await this.logger.info("Codex App Server stderr", {
@@ -10465,7 +10585,7 @@ var init_process_pool = __esm({
10465
10585
  this.routes.set(ownerId, route);
10466
10586
  }
10467
10587
  unregister(ownerId) {
10468
- if (this.activeOwner.value === ownerId) {
10588
+ if ([...this.activeSlots.values()].some((slot) => slot.ownerId === ownerId)) {
10469
10589
  throw new Error(`Local Codex route ${ownerId} still owns capacity`);
10470
10590
  }
10471
10591
  this.routes.delete(ownerId);
@@ -10474,27 +10594,35 @@ var init_process_pool = __esm({
10474
10594
  this.waitingOwners.splice(index2, 1);
10475
10595
  }
10476
10596
  }
10477
- tryAcquire(ownerId) {
10597
+ tryAcquireExecutionSlot(ownerId) {
10478
10598
  if (!this.routes.has(ownerId)) {
10479
10599
  throw new Error(`Local Codex route ${ownerId} is not registered`);
10480
10600
  }
10481
- if (this.activeOwner.value === ownerId) {
10482
- return true;
10483
- }
10484
10601
  if (!this.waitingOwners.includes(ownerId)) {
10485
10602
  this.waitingOwners.push(ownerId);
10486
10603
  }
10487
- if (this.activeOwner.value !== void 0 || this.waitingOwners[0] !== ownerId) {
10488
- return false;
10604
+ if (this.activeSlots.size >= maximumConcurrentTurns || this.waitingOwners[0] !== ownerId) {
10605
+ return {};
10489
10606
  }
10490
10607
  this.waitingOwners.shift();
10491
- this.activeOwner = { value: ownerId };
10492
- return true;
10608
+ const value = { ownerId, slotId: this.nextSlotId };
10609
+ this.nextSlotId += 1;
10610
+ this.activeSlots.set(value.slotId, { executing: false, ownerId });
10611
+ return { value };
10493
10612
  }
10494
- release(ownerId) {
10495
- if (this.activeOwner.value === ownerId) {
10496
- this.activeOwner = {};
10613
+ markExecutionStarted(slot) {
10614
+ const active = this.requireActiveSlot(slot);
10615
+ if (active.executing) {
10616
+ throw new Error(`Local Codex capacity slot ${slot.slotId} is already executing`);
10497
10617
  }
10618
+ active.executing = true;
10619
+ }
10620
+ releaseExecutionSlot(slot) {
10621
+ this.requireActiveSlot(slot);
10622
+ this.activeSlots.delete(slot.slotId);
10623
+ }
10624
+ hasOtherActiveExecution() {
10625
+ return [...this.activeSlots.values()].filter((slot) => slot.executing).length > 1;
10498
10626
  }
10499
10627
  retireTurn(nativeTurnId) {
10500
10628
  this.retiredTurnIds.add(nativeTurnId);
@@ -10539,20 +10667,82 @@ var init_process_pool = __esm({
10539
10667
  return client === void 0 ? {} : { value: await client.captureProcessTree() };
10540
10668
  }
10541
10669
  async quarantineCurrentProcess(capture) {
10670
+ const processGeneration = this.supervisor.currentClient().value?.processGeneration;
10671
+ this.loadedThreadIds.clear();
10672
+ try {
10673
+ await this.supervisor.quarantineCurrentProcess(capture);
10674
+ } finally {
10675
+ if (processGeneration !== void 0) {
10676
+ await this.broadcastProcessExit(new AppServerProcessLostError(`Shared Codex App Server generation ${processGeneration} was quarantined`));
10677
+ }
10678
+ }
10679
+ }
10680
+ async quarantineProcess(client, capture) {
10681
+ if (this.supervisor.currentClient().value === client) {
10682
+ await this.quarantineCurrentProcess(capture);
10683
+ return;
10684
+ }
10542
10685
  this.loadedThreadIds.clear();
10543
- await this.supervisor.quarantineCurrentProcess(capture);
10686
+ await client.stop(capture);
10544
10687
  }
10545
10688
  async stop() {
10546
10689
  this.loadedThreadIds.clear();
10547
10690
  await this.supervisor.stop();
10548
10691
  }
10549
- activeRoute() {
10550
- const owner = this.activeOwner.value;
10551
- if (owner === void 0) {
10552
- return {};
10692
+ matchingRoutes(message) {
10693
+ return [...this.routes.values()].filter((route) => route.ownsMessage(message));
10694
+ }
10695
+ async routeNotification(message) {
10696
+ const routes = this.matchingRoutes(message);
10697
+ if (routes.length === 0) {
10698
+ return;
10699
+ }
10700
+ if (routes.length > 1) {
10701
+ await this.logger.error("Local Codex App Server notification matched multiple runtime routes", new Error(message.method ?? "Unknown App Server notification"));
10702
+ return;
10703
+ }
10704
+ const route = routes[0];
10705
+ if (route === void 0) {
10706
+ throw new Error("Expected one Local Codex notification route");
10707
+ }
10708
+ await route.onNotification(message);
10709
+ }
10710
+ async routeServerRequest(message) {
10711
+ const routes = this.matchingRoutes(message);
10712
+ if (routes.length === 1) {
10713
+ const route = routes[0];
10714
+ if (route === void 0) {
10715
+ throw new Error("Expected one Local Codex request route");
10716
+ }
10717
+ await route.onServerRequest(message);
10718
+ return;
10719
+ }
10720
+ if (routes.length > 1) {
10721
+ await this.logger.error("Local Codex App Server request matched multiple runtime routes", new Error(message.method ?? "Unknown App Server request"));
10722
+ }
10723
+ const client = this.supervisor.currentClient().value;
10724
+ if (client !== void 0 && message.id !== void 0) {
10725
+ await client.respondWithError(message.id, -32e3, routes.length === 0 ? "No owning Local Codex Runtime turn" : "Ambiguous Local Codex Runtime turn ownership");
10726
+ }
10727
+ }
10728
+ async broadcastProcessExit(error) {
10729
+ await Promise.all([...this.routes.values()].map(async (route) => {
10730
+ try {
10731
+ await route.onProcessExit(error);
10732
+ } catch (routeError) {
10733
+ await this.logger.error("Failed to settle a Local Codex route after the shared App Server exited", routeError).catch(() => void 0);
10734
+ }
10735
+ }));
10736
+ }
10737
+ requireActiveSlot(slot) {
10738
+ const active = this.activeSlots.get(slot.slotId);
10739
+ if (active === void 0) {
10740
+ throw new Error(`Local Codex capacity slot ${slot.slotId} is not active`);
10741
+ }
10742
+ if (active.ownerId !== slot.ownerId) {
10743
+ throw new Error(`Local Codex capacity slot ${slot.slotId} belongs to another route`);
10553
10744
  }
10554
- const value = this.routes.get(owner);
10555
- return value === void 0 ? {} : { value };
10745
+ return active;
10556
10746
  }
10557
10747
  };
10558
10748
  }
@@ -14374,7 +14564,7 @@ function getApiKeyOrUndefined() {
14374
14564
 
14375
14565
  // src/utils/http.ts
14376
14566
  function getUserAgent() {
14377
- return `moxt-cli/${"0.10.0-beta.7"} (${(0, import_os.platform)()}/${(0, import_os.arch)()}) node/${process.versions.node}`;
14567
+ return `moxt-cli/${"0.10.0-beta.8"} (${(0, import_os.platform)()}/${(0, import_os.arch)()}) node/${process.versions.node}`;
14378
14568
  }
14379
14569
  async function sendRequest(method, path4, init) {
14380
14570
  return await fetch(`${getApiBaseUrl()}${path4}`, {
@@ -28796,7 +28986,7 @@ var FLUSH_TIMEOUT_MS = 3e3;
28796
28986
  var MAX_BATCH_SIZE = 100;
28797
28987
  function commonLabels() {
28798
28988
  return {
28799
- cli_version: "0.10.0-beta.7",
28989
+ cli_version: "0.10.0-beta.8",
28800
28990
  node_version: process.versions.node,
28801
28991
  os: (0, import_node_os11.platform)(),
28802
28992
  arch: (0, import_node_os11.arch)(),
@@ -28842,7 +29032,7 @@ async function flush() {
28842
29032
  }
28843
29033
  const batch = buffer.splice(0, MAX_BATCH_SIZE);
28844
29034
  const payload = {
28845
- release_id: "0.10.0-beta.7",
29035
+ release_id: "0.10.0-beta.8",
28846
29036
  client_type: "cli",
28847
29037
  metric_data_list: batch.map((m) => ({
28848
29038
  profile: "cli",
@@ -29225,9 +29415,9 @@ function commandPath3(command) {
29225
29415
  }
29226
29416
 
29227
29417
  // src/index.ts
29228
- var program2 = createProgram("0.10.0-beta.7");
29229
- installDaemonReconcileHook(program2, "0.10.0-beta.7");
29230
- installUpdateHook(program2, "0.10.0-beta.7");
29418
+ var program2 = createProgram("0.10.0-beta.8");
29419
+ installDaemonReconcileHook(program2, "0.10.0-beta.8");
29420
+ installUpdateHook(program2, "0.10.0-beta.8");
29231
29421
  instrumentProgram(program2);
29232
29422
  installExitHandler();
29233
29423
  program2.parseAsync(process.argv).then(async () => {