@moxt-ai/mobius 0.0.125 → 0.0.126

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.js CHANGED
@@ -23323,8 +23323,8 @@ function acpEndpointTerminationError(termination) {
23323
23323
  case "closed":
23324
23324
  return new AcpExecutionError("The ACP endpoint is closed", {}, "acp_endpoint_closed");
23325
23325
  default: {
23326
- const unreachable = termination;
23327
- return unreachable;
23326
+ const unreachable2 = termination;
23327
+ return unreachable2;
23328
23328
  }
23329
23329
  }
23330
23330
  }
@@ -31158,6 +31158,191 @@ var AcpProcessSession = class _AcpProcessSession {
31158
31158
  };
31159
31159
  };
31160
31160
 
31161
+ // src/acp/retained-sessions.ts
31162
+ var RetainedSessions = class {
31163
+ #entries = /* @__PURE__ */ new Map();
31164
+ #events;
31165
+ constructor(events) {
31166
+ this.#events = events;
31167
+ }
31168
+ #entry = (id) => this.#entries.get(id) ?? { lifecycle: { kind: "empty" }, users: 0 };
31169
+ #write = (id, entry) => {
31170
+ if (entry.lifecycle.kind === "empty" && entry.users === 0) {
31171
+ this.#entries.delete(id);
31172
+ } else {
31173
+ this.#entries.set(id, entry);
31174
+ }
31175
+ };
31176
+ #resource = (entry) => entry.lifecycle.kind === "preparing" ? entry.lifecycle.resource : entry.lifecycle;
31177
+ get = (id) => {
31178
+ const resource = this.#resource(this.#entry(id));
31179
+ return resource.kind === "ready" ? resource.runtime : void 0;
31180
+ };
31181
+ pending = (id) => {
31182
+ const state = this.#entry(id).lifecycle;
31183
+ return state.kind === "preparing" ? state.completion : void 0;
31184
+ };
31185
+ isBusy = (id) => {
31186
+ const entry = this.#entry(id);
31187
+ return entry.users > 0 || entry.lifecycle.kind === "preparing";
31188
+ };
31189
+ touch = (id, expected = this.get(id)) => {
31190
+ if (expected !== void 0 && this.get(id) === expected) {
31191
+ this.#events.touch(expected);
31192
+ }
31193
+ };
31194
+ #setResource = (id, resource) => {
31195
+ const entry = this.#entry(id);
31196
+ this.#write(id, {
31197
+ ...entry,
31198
+ lifecycle: entry.lifecycle.kind === "preparing" ? { ...entry.lifecycle, resource } : resource
31199
+ });
31200
+ };
31201
+ retain = (id, runtime) => {
31202
+ this.#setResource(id, { kind: "ready", runtime });
31203
+ };
31204
+ detach = (id, expected) => {
31205
+ if (this.get(id) !== expected) {
31206
+ return false;
31207
+ }
31208
+ this.#setResource(id, { kind: "empty" });
31209
+ return true;
31210
+ };
31211
+ entries = () => {
31212
+ const result = [];
31213
+ for (const id of this.#entries.keys()) {
31214
+ const runtime = this.get(id);
31215
+ if (runtime !== void 0) {
31216
+ result.push([id, runtime]);
31217
+ }
31218
+ }
31219
+ return result;
31220
+ };
31221
+ prepare = (id, start) => {
31222
+ const pending = this.pending(id);
31223
+ if (pending !== void 0) {
31224
+ return pending;
31225
+ }
31226
+ const completion = Promise.withResolvers();
31227
+ const entry = this.#entry(id);
31228
+ this.#write(id, {
31229
+ ...entry,
31230
+ lifecycle: {
31231
+ kind: "preparing",
31232
+ completion: completion.promise,
31233
+ resource: this.#resource(entry)
31234
+ }
31235
+ });
31236
+ const run = async () => {
31237
+ try {
31238
+ return await start();
31239
+ } finally {
31240
+ const current = this.#entry(id);
31241
+ if (current.lifecycle.kind === "preparing" && current.lifecycle.completion === completion.promise) {
31242
+ this.#write(id, { ...current, lifecycle: current.lifecycle.resource });
31243
+ }
31244
+ this.#events.changed();
31245
+ }
31246
+ };
31247
+ void run().then(completion.resolve, completion.reject);
31248
+ return completion.promise;
31249
+ };
31250
+ use = async (id, operation) => {
31251
+ const entry = this.#entry(id);
31252
+ this.#write(id, { ...entry, users: entry.users + 1 });
31253
+ this.touch(id);
31254
+ try {
31255
+ return await operation();
31256
+ } finally {
31257
+ const current = this.#entry(id);
31258
+ this.#write(id, { ...current, users: current.users - 1 });
31259
+ this.touch(id);
31260
+ this.#events.changed();
31261
+ }
31262
+ };
31263
+ };
31264
+
31265
+ // src/acp/session-executions.ts
31266
+ var unreachable = (state) => {
31267
+ throw new AcpExecutionError("Unexpected execution state", { cause: state });
31268
+ };
31269
+ var SessionExecutions = class {
31270
+ #entries = /* @__PURE__ */ new Map();
31271
+ command = (id) => {
31272
+ const state = this.#entries.get(id);
31273
+ switch (state?.kind) {
31274
+ case "running":
31275
+ case "cancelling":
31276
+ return state.commandId;
31277
+ case "cancelled-before-start":
31278
+ case void 0:
31279
+ return void 0;
31280
+ default:
31281
+ return unreachable(state);
31282
+ }
31283
+ };
31284
+ cancellation = (id) => {
31285
+ const state = this.#entries.get(id);
31286
+ switch (state?.kind) {
31287
+ case "cancelling":
31288
+ case "cancelled-before-start":
31289
+ return state.completion;
31290
+ case "running":
31291
+ case void 0:
31292
+ return void 0;
31293
+ default:
31294
+ return unreachable(state);
31295
+ }
31296
+ };
31297
+ isBusy = (id) => this.#entries.has(id);
31298
+ begin = (id, commandId) => {
31299
+ const state = this.#entries.get(id);
31300
+ switch (state?.kind) {
31301
+ case "running":
31302
+ case "cancelling":
31303
+ throw new AcpExecutionError(
31304
+ "Another prompt is already running in this conversation",
31305
+ {},
31306
+ "session_busy"
31307
+ );
31308
+ case "cancelled-before-start":
31309
+ case void 0:
31310
+ break;
31311
+ default:
31312
+ unreachable(state);
31313
+ }
31314
+ this.#entries.set(
31315
+ id,
31316
+ state?.kind === "cancelled-before-start" ? { kind: "cancelling", commandId, completion: state.completion } : { kind: "running", commandId }
31317
+ );
31318
+ };
31319
+ cancel = (id, stop) => {
31320
+ const state = this.#entries.get(id);
31321
+ switch (state?.kind) {
31322
+ case "cancelling":
31323
+ case "cancelled-before-start":
31324
+ return state.completion;
31325
+ case "running":
31326
+ case void 0:
31327
+ break;
31328
+ default:
31329
+ return unreachable(state);
31330
+ }
31331
+ const completion = Promise.withResolvers();
31332
+ this.#entries.set(
31333
+ id,
31334
+ state?.kind === "running" ? { kind: "cancelling", commandId: state.commandId, completion: completion.promise } : { kind: "cancelled-before-start", completion: completion.promise }
31335
+ );
31336
+ void (async () => await stop())().then(completion.resolve, completion.reject);
31337
+ return completion.promise;
31338
+ };
31339
+ finish = (id, commandId) => {
31340
+ if (this.command(id) === commandId) {
31341
+ this.#entries.delete(id);
31342
+ }
31343
+ };
31344
+ };
31345
+
31161
31346
  // src/native-sessions/chat-agent.ts
31162
31347
  function supportsNativeChat(agentId) {
31163
31348
  return agentId === "codex" || agentId === "claude" || agentId === "opencode" || agentId === "pi";
@@ -31369,18 +31554,16 @@ var AcpProcessPool = class {
31369
31554
  var SESSION_CONTROL_TIMEOUT_MILLISECONDS = 3e4;
31370
31555
  var AcpSessionManager = class {
31371
31556
  #processes = new AcpProcessPool();
31372
- #activeCommandIds = /* @__PURE__ */ new Map();
31557
+ #sessions = new RetainedSessions({ touch: this.#processes.touch, changed: this.#processes.notifyChanged });
31558
+ #nativeBrowserSessions = new RetainedSessions({
31559
+ touch: this.#processes.touch,
31560
+ changed: this.#processes.notifyChanged
31561
+ });
31562
+ #executions = new SessionExecutions();
31373
31563
  #agentSettingsObserver = new NullAcpSessionObserver();
31374
31564
  #nativeOperations = /* @__PURE__ */ new Set();
31375
31565
  #nativeBrowserObserver = new NullAcpSessionObserver();
31376
- #nativeBrowserPreparing = /* @__PURE__ */ new Map();
31377
- #nativeBrowserSessions = /* @__PURE__ */ new Map();
31378
31566
  #nativeBrowserStore = new MemoryAcpSessionStore();
31379
- #nativeBrowserUseCounts = /* @__PURE__ */ new Map();
31380
- #cancellationRequests = /* @__PURE__ */ new Map();
31381
- #preparing = /* @__PURE__ */ new Map();
31382
- #sessionUseCounts = /* @__PURE__ */ new Map();
31383
- #sessions = /* @__PURE__ */ new Map();
31384
31567
  #store;
31385
31568
  #nativeHistoryReader;
31386
31569
  #sessionIdentity;
@@ -31394,60 +31577,6 @@ var AcpSessionManager = class {
31394
31577
  setObserver = (observer) => {
31395
31578
  this.#observer = observer;
31396
31579
  };
31397
- #touchSession(sessionId) {
31398
- const runtime = this.#sessions.get(sessionId);
31399
- if (runtime === void 0) {
31400
- return;
31401
- }
31402
- this.#processes.touch(runtime);
31403
- }
31404
- #touchExpectedSession(sessionId, expected) {
31405
- if (this.#sessions.get(sessionId) !== expected) {
31406
- return;
31407
- }
31408
- this.#processes.touch(expected);
31409
- }
31410
- #touchNativeBrowserSession(sessionId) {
31411
- const runtime = this.#nativeBrowserSessions.get(sessionId);
31412
- if (runtime === void 0) {
31413
- return;
31414
- }
31415
- this.#processes.touch(runtime);
31416
- }
31417
- #touchExpectedNativeBrowserSession(sessionId, expected) {
31418
- if (this.#nativeBrowserSessions.get(sessionId) !== expected) {
31419
- return;
31420
- }
31421
- this.#processes.touch(expected);
31422
- }
31423
- #beginSessionUse(sessionId) {
31424
- this.#sessionUseCounts.set(sessionId, (this.#sessionUseCounts.get(sessionId) ?? 0) + 1);
31425
- this.#touchSession(sessionId);
31426
- }
31427
- #endSessionUse(sessionId) {
31428
- const count = this.#sessionUseCounts.get(sessionId) ?? 0;
31429
- if (count <= 1) {
31430
- this.#sessionUseCounts.delete(sessionId);
31431
- } else {
31432
- this.#sessionUseCounts.set(sessionId, count - 1);
31433
- }
31434
- this.#touchSession(sessionId);
31435
- this.#processes.notifyChanged();
31436
- }
31437
- #beginNativeBrowserUse(sessionId) {
31438
- this.#nativeBrowserUseCounts.set(sessionId, (this.#nativeBrowserUseCounts.get(sessionId) ?? 0) + 1);
31439
- this.#touchNativeBrowserSession(sessionId);
31440
- }
31441
- #endNativeBrowserUse(sessionId) {
31442
- const count = this.#nativeBrowserUseCounts.get(sessionId) ?? 0;
31443
- if (count <= 1) {
31444
- this.#nativeBrowserUseCounts.delete(sessionId);
31445
- } else {
31446
- this.#nativeBrowserUseCounts.set(sessionId, count - 1);
31447
- }
31448
- this.#touchNativeBrowserSession(sessionId);
31449
- this.#processes.notifyChanged();
31450
- }
31451
31580
  #isNativeSessionOperationActive(sessionId) {
31452
31581
  const stored = this.#store.read(sessionId);
31453
31582
  return stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`);
@@ -31455,20 +31584,17 @@ var AcpSessionManager = class {
31455
31584
  #retention(sessionId, kind) {
31456
31585
  if (kind === "execution") {
31457
31586
  return {
31458
- isBusy: () => this.#activeCommandIds.has(sessionId) || this.#cancellationRequests.has(sessionId) || this.#preparing.has(sessionId) || (this.#sessionUseCounts.get(sessionId) ?? 0) > 0 || this.#isNativeSessionOperationActive(sessionId),
31587
+ isBusy: () => this.#executions.isBusy(sessionId) || this.#sessions.isBusy(sessionId) || this.#isNativeSessionOperationActive(sessionId),
31459
31588
  retire: (runtime) => this.#retireSessionProcess(sessionId, runtime, () => runtime.close())
31460
31589
  };
31461
31590
  }
31462
31591
  return {
31463
- isBusy: () => this.#nativeBrowserPreparing.has(sessionId) || (this.#nativeBrowserUseCounts.get(sessionId) ?? 0) > 0,
31592
+ isBusy: () => this.#nativeBrowserSessions.isBusy(sessionId),
31464
31593
  retire: (runtime) => this.#retireNativeBrowserProcess(sessionId, runtime, () => runtime.close())
31465
31594
  };
31466
31595
  }
31467
31596
  async #retireSessionProcess(sessionId, runtime, close) {
31468
- const retained = this.#sessions.get(sessionId) === runtime;
31469
- if (retained) {
31470
- this.#sessions.delete(sessionId);
31471
- }
31597
+ const retained = this.#sessions.detach(sessionId, runtime);
31472
31598
  try {
31473
31599
  await this.#processes.retire(runtime, close);
31474
31600
  } finally {
@@ -31481,9 +31607,8 @@ var AcpSessionManager = class {
31481
31607
  }
31482
31608
  }
31483
31609
  async #retireNativeBrowserProcess(sessionId, runtime, close) {
31484
- const retained = this.#nativeBrowserSessions.get(sessionId) === runtime;
31610
+ const retained = this.#nativeBrowserSessions.detach(sessionId, runtime);
31485
31611
  if (retained) {
31486
- this.#nativeBrowserSessions.delete(sessionId);
31487
31612
  this.#nativeBrowserStore.remove(sessionId);
31488
31613
  }
31489
31614
  await this.#processes.retire(runtime, close);
@@ -31494,7 +31619,7 @@ var AcpSessionManager = class {
31494
31619
  start,
31495
31620
  this.#retention(options.sessionId, "execution")
31496
31621
  );
31497
- this.#sessions.set(options.sessionId, runtime);
31622
+ this.#sessions.retain(options.sessionId, runtime);
31498
31623
  return runtime;
31499
31624
  }
31500
31625
  async #startRetainedNativeBrowserProcess(options, start) {
@@ -31503,140 +31628,105 @@ var AcpSessionManager = class {
31503
31628
  start,
31504
31629
  this.#retention(options.sessionId, "browser")
31505
31630
  );
31506
- this.#nativeBrowserSessions.set(options.sessionId, runtime);
31631
+ this.#nativeBrowserSessions.retain(options.sessionId, runtime);
31507
31632
  return runtime;
31508
31633
  }
31509
31634
  async #startNativeBrowserPreparation(options) {
31510
- const operation = (async () => {
31511
- try {
31512
- return await this.#startRetainedNativeBrowserProcess(
31635
+ return await this.#nativeBrowserSessions.prepare(options.sessionId, async () => {
31636
+ return await this.#startRetainedNativeBrowserProcess(
31637
+ options,
31638
+ async () => await AcpProcessSession.startNativeSessionBrowser(
31513
31639
  options,
31514
- async () => await AcpProcessSession.startNativeSessionBrowser(
31515
- options,
31516
- () => this.#nativeBrowserObserver,
31517
- this.#nativeBrowserStore
31518
- )
31519
- );
31520
- } finally {
31521
- this.#nativeBrowserPreparing.delete(options.sessionId);
31522
- this.#processes.notifyChanged();
31523
- }
31524
- })();
31525
- this.#nativeBrowserPreparing.set(options.sessionId, operation);
31526
- return await operation;
31640
+ () => this.#nativeBrowserObserver,
31641
+ this.#nativeBrowserStore
31642
+ )
31643
+ );
31644
+ });
31527
31645
  }
31528
31646
  async #replaceNativeBrowserPreparation(existing, options) {
31529
- const operation = (async () => {
31530
- try {
31531
- await this.#retireNativeBrowserProcess(options.sessionId, existing, async () => await existing.close());
31532
- return await this.#startRetainedNativeBrowserProcess(
31647
+ return await this.#nativeBrowserSessions.prepare(options.sessionId, async () => {
31648
+ await this.#retireNativeBrowserProcess(options.sessionId, existing, async () => await existing.close());
31649
+ return await this.#startRetainedNativeBrowserProcess(
31650
+ options,
31651
+ async () => await AcpProcessSession.startNativeSessionBrowser(
31533
31652
  options,
31534
- async () => await AcpProcessSession.startNativeSessionBrowser(
31535
- options,
31536
- () => this.#nativeBrowserObserver,
31537
- this.#nativeBrowserStore
31538
- )
31539
- );
31540
- } finally {
31541
- this.#nativeBrowserPreparing.delete(options.sessionId);
31542
- this.#processes.notifyChanged();
31543
- }
31544
- })();
31545
- this.#nativeBrowserPreparing.set(options.sessionId, operation);
31546
- return await operation;
31653
+ () => this.#nativeBrowserObserver,
31654
+ this.#nativeBrowserStore
31655
+ )
31656
+ );
31657
+ });
31547
31658
  }
31548
31659
  async #startPreparation(options, metrics = unmeasuredOperationMetrics) {
31549
- const operation = (async () => {
31550
- try {
31551
- return await this.#startRetainedSessionProcess(
31552
- options,
31553
- async () => await AcpProcessSession.start(
31554
- options.origin === void 0 && this.#sessionIdentity === "native" && isNativeChatSessionId(options.agent.id, options.sessionId) && this.#store.read(options.sessionId) instanceof MissingAcpStoredSession ? { ...options, origin: { kind: "import", nativeSessionId: options.sessionId } } : options,
31555
- () => this.#observer,
31556
- this.#store,
31557
- metrics
31558
- )
31559
- );
31560
- } finally {
31561
- this.#preparing.delete(options.sessionId);
31562
- this.#processes.notifyChanged();
31563
- }
31564
- })();
31565
- this.#preparing.set(options.sessionId, operation);
31566
- return await operation;
31660
+ return await this.#sessions.prepare(options.sessionId, async () => {
31661
+ return await this.#startRetainedSessionProcess(
31662
+ options,
31663
+ async () => await AcpProcessSession.start(
31664
+ options.origin === void 0 && this.#sessionIdentity === "native" && isNativeChatSessionId(options.agent.id, options.sessionId) && this.#store.read(options.sessionId) instanceof MissingAcpStoredSession ? { ...options, origin: { kind: "import", nativeSessionId: options.sessionId } } : options,
31665
+ () => this.#observer,
31666
+ this.#store,
31667
+ metrics
31668
+ )
31669
+ );
31670
+ });
31567
31671
  }
31568
31672
  async #replacePreparation(existing, options, metrics) {
31569
- const operation = (async () => {
31570
- try {
31571
- await this.#retireSessionProcess(
31572
- options.sessionId,
31573
- existing,
31574
- async () => await metrics.measure("runtime-replacement-close", async () => await existing.close())
31575
- );
31576
- this.#store.remove(options.sessionId);
31577
- return await this.#startRetainedSessionProcess(
31578
- options,
31579
- async () => await AcpProcessSession.start(options, () => this.#observer, this.#store, metrics)
31580
- );
31581
- } finally {
31582
- this.#preparing.delete(options.sessionId);
31583
- this.#processes.notifyChanged();
31584
- }
31585
- })();
31586
- this.#preparing.set(options.sessionId, operation);
31587
- return await operation;
31673
+ return await this.#sessions.prepare(options.sessionId, async () => {
31674
+ await this.#retireSessionProcess(
31675
+ options.sessionId,
31676
+ existing,
31677
+ async () => await metrics.measure("runtime-replacement-close", async () => await existing.close())
31678
+ );
31679
+ this.#store.remove(options.sessionId);
31680
+ return await this.#startRetainedSessionProcess(
31681
+ options,
31682
+ async () => await AcpProcessSession.start(options, () => this.#observer, this.#store, metrics)
31683
+ );
31684
+ });
31588
31685
  }
31589
31686
  async #replaceExecutionPreparation(existing, options) {
31590
31687
  const desiredConfigOptions = existing.prepared().configOptions;
31591
- const operation = (async () => {
31688
+ return await this.#sessions.prepare(options.sessionId, async () => {
31689
+ await this.#retireSessionProcess(options.sessionId, existing, async () => await existing.close());
31690
+ const runtime = await this.#startRetainedSessionProcess(
31691
+ options,
31692
+ async () => await AcpProcessSession.start(options, () => this.#observer, this.#store)
31693
+ );
31592
31694
  try {
31593
- await this.#retireSessionProcess(options.sessionId, existing, async () => await existing.close());
31594
- const runtime = await this.#startRetainedSessionProcess(
31595
- options,
31596
- async () => await AcpProcessSession.start(options, () => this.#observer, this.#store)
31597
- );
31598
- try {
31599
- for (const desired of desiredConfigOptions) {
31600
- const current = runtime.prepared().configOptions.find((option) => option.id === desired.id);
31601
- if (current === void 0 || current.type !== desired.type) {
31602
- continue;
31603
- }
31604
- if (desired.type === "boolean") {
31605
- if (current.booleanValue !== desired.booleanValue) {
31606
- await runtime.configure({
31607
- ...options,
31608
- booleanValue: desired.booleanValue,
31609
- configId: desired.id,
31610
- requestId: `execution-prepare-${desired.id}`,
31611
- value: "",
31612
- valueType: "boolean"
31613
- });
31614
- }
31615
- continue;
31616
- }
31617
- if (current.currentValue !== desired.currentValue && current.values.some((value) => value.value === desired.currentValue)) {
31695
+ for (const desired of desiredConfigOptions) {
31696
+ const current = runtime.prepared().configOptions.find((option) => option.id === desired.id);
31697
+ if (current === void 0 || current.type !== desired.type) {
31698
+ continue;
31699
+ }
31700
+ if (desired.type === "boolean") {
31701
+ if (current.booleanValue !== desired.booleanValue) {
31618
31702
  await runtime.configure({
31619
31703
  ...options,
31620
- booleanValue: false,
31704
+ booleanValue: desired.booleanValue,
31621
31705
  configId: desired.id,
31622
31706
  requestId: `execution-prepare-${desired.id}`,
31623
- value: desired.currentValue,
31624
- valueType: "select"
31707
+ value: "",
31708
+ valueType: "boolean"
31625
31709
  });
31626
31710
  }
31711
+ continue;
31712
+ }
31713
+ if (current.currentValue !== desired.currentValue && current.values.some((value) => value.value === desired.currentValue)) {
31714
+ await runtime.configure({
31715
+ ...options,
31716
+ booleanValue: false,
31717
+ configId: desired.id,
31718
+ requestId: `execution-prepare-${desired.id}`,
31719
+ value: desired.currentValue,
31720
+ valueType: "select"
31721
+ });
31627
31722
  }
31628
- } catch (error62) {
31629
- await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
31630
- throw error62;
31631
31723
  }
31632
- return runtime;
31633
- } finally {
31634
- this.#preparing.delete(options.sessionId);
31635
- this.#processes.notifyChanged();
31724
+ } catch (error62) {
31725
+ await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
31726
+ throw error62;
31636
31727
  }
31637
- })();
31638
- this.#preparing.set(options.sessionId, operation);
31639
- return await operation;
31728
+ return runtime;
31729
+ });
31640
31730
  }
31641
31731
  prepareSession = async (options, metrics = unmeasuredOperationMetrics) => {
31642
31732
  const stored = metrics.measureSync("session-state-read", () => this.#store.read(options.sessionId));
@@ -31650,11 +31740,11 @@ var AcpSessionManager = class {
31650
31740
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
31651
31741
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
31652
31742
  }
31653
- const pending = this.#preparing.get(options.sessionId);
31743
+ const pending = this.#sessions.pending(options.sessionId);
31654
31744
  if (pending !== void 0) {
31655
31745
  const runtime2 = await metrics.measure("session-preparation-wait", async () => await pending);
31656
31746
  if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31657
- this.#touchExpectedSession(options.sessionId, runtime2);
31747
+ this.#sessions.touch(options.sessionId, runtime2);
31658
31748
  return runtime2.prepared();
31659
31749
  }
31660
31750
  return await this.prepareSession(options, metrics);
@@ -31662,7 +31752,7 @@ var AcpSessionManager = class {
31662
31752
  const existing = this.#sessions.get(options.sessionId);
31663
31753
  if (existing !== void 0) {
31664
31754
  if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31665
- this.#touchExpectedSession(options.sessionId, existing);
31755
+ this.#sessions.touch(options.sessionId, existing);
31666
31756
  return existing.prepared();
31667
31757
  }
31668
31758
  if (!existing.canRebind()) {
@@ -31716,11 +31806,10 @@ var AcpSessionManager = class {
31716
31806
  * Use inspectAgentSettings for Agent-scoped settings discovery.
31717
31807
  */
31718
31808
  inspectSession = async (options, metrics = unmeasuredOperationMetrics) => {
31719
- if (this.#preparing.has(options.sessionId) || this.#sessions.has(options.sessionId)) {
31809
+ if (this.#sessions.pending(options.sessionId) !== void 0 || this.#sessions.get(options.sessionId) !== void 0) {
31720
31810
  throw new AcpExecutionError("The configuration inspection session is already in use", {}, "session_busy");
31721
31811
  }
31722
- this.#beginSessionUse(options.sessionId);
31723
- try {
31812
+ return await this.#sessions.use(options.sessionId, async () => {
31724
31813
  const runtime = await this.#startPreparation(options, metrics);
31725
31814
  try {
31726
31815
  return runtime.prepared();
@@ -31731,12 +31820,10 @@ var AcpSessionManager = class {
31731
31820
  this.#store.remove(options.sessionId);
31732
31821
  }
31733
31822
  }
31734
- } finally {
31735
- this.#endSessionUse(options.sessionId);
31736
- }
31823
+ });
31737
31824
  };
31738
31825
  prepareExecutionSession = async (options) => {
31739
- const pending = this.#preparing.get(options.sessionId);
31826
+ const pending = this.#sessions.pending(options.sessionId);
31740
31827
  if (pending !== void 0) {
31741
31828
  await pending;
31742
31829
  return await this.prepareExecutionSession(options);
@@ -31753,7 +31840,7 @@ var AcpSessionManager = class {
31753
31840
  );
31754
31841
  }
31755
31842
  if (existing.usesEnvironment(options.agent.environment)) {
31756
- this.#touchExpectedSession(options.sessionId, existing);
31843
+ this.#sessions.touch(options.sessionId, existing);
31757
31844
  return existing.prepared();
31758
31845
  }
31759
31846
  if (existing.hasRunningTurn()) {
@@ -31766,11 +31853,11 @@ var AcpSessionManager = class {
31766
31853
  return (await this.#replaceExecutionPreparation(existing, options)).prepared();
31767
31854
  };
31768
31855
  prepareNativeSessionBrowser = async (options) => {
31769
- const pending = this.#nativeBrowserPreparing.get(options.sessionId);
31856
+ const pending = this.#nativeBrowserSessions.pending(options.sessionId);
31770
31857
  if (pending !== void 0) {
31771
31858
  const runtime2 = await pending;
31772
31859
  if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31773
- this.#touchExpectedNativeBrowserSession(options.sessionId, runtime2);
31860
+ this.#nativeBrowserSessions.touch(options.sessionId, runtime2);
31774
31861
  return runtime2.prepared();
31775
31862
  }
31776
31863
  return await this.prepareNativeSessionBrowser(options);
@@ -31778,7 +31865,7 @@ var AcpSessionManager = class {
31778
31865
  const existing = this.#nativeBrowserSessions.get(options.sessionId);
31779
31866
  if (existing !== void 0) {
31780
31867
  if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31781
- this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
31868
+ this.#nativeBrowserSessions.touch(options.sessionId, existing);
31782
31869
  return existing.prepared();
31783
31870
  }
31784
31871
  if (!existing.canRebind()) {
@@ -31795,45 +31882,34 @@ var AcpSessionManager = class {
31795
31882
  return runtime.prepared();
31796
31883
  };
31797
31884
  configureSession = async (options) => {
31798
- this.#beginSessionUse(options.sessionId);
31799
- try {
31885
+ return await this.#sessions.use(options.sessionId, async () => {
31800
31886
  const runtime = await this.ensureSession(options);
31801
31887
  return await runtime.configure(options);
31802
- } finally {
31803
- this.#endSessionUse(options.sessionId);
31804
- }
31888
+ });
31805
31889
  };
31806
31890
  cancelPrompt = async (sessionId) => {
31807
- const existing = this.#cancellationRequests.get(sessionId);
31808
- if (existing !== void 0) {
31809
- await existing;
31810
- return;
31811
- }
31812
- const cancellation = (async () => {
31891
+ await this.#executions.cancel(sessionId, async () => {
31813
31892
  const runtime = this.#sessions.get(sessionId);
31814
31893
  if (runtime !== void 0) {
31815
31894
  try {
31816
31895
  await runtime.cancel();
31817
31896
  } finally {
31818
- if (this.#sessions.get(sessionId) === runtime) {
31819
- this.#sessions.delete(sessionId);
31897
+ if (this.#sessions.detach(sessionId, runtime)) {
31820
31898
  this.#processes.releaseClosed(runtime);
31821
31899
  }
31822
31900
  }
31823
31901
  }
31824
- })();
31825
- this.#cancellationRequests.set(sessionId, cancellation);
31826
- await cancellation;
31902
+ });
31827
31903
  };
31828
31904
  steerPrompt = async (options) => {
31829
- if (this.#activeCommandIds.get(options.sessionId) !== options.targetCommandId) {
31905
+ if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
31830
31906
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
31831
31907
  }
31832
- const pending = this.#preparing.get(options.sessionId);
31908
+ const pending = this.#sessions.pending(options.sessionId);
31833
31909
  if (pending !== void 0) {
31834
31910
  await pending;
31835
31911
  }
31836
- if (this.#activeCommandIds.get(options.sessionId) !== options.targetCommandId) {
31912
+ if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
31837
31913
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
31838
31914
  }
31839
31915
  const runtime = this.#sessions.get(options.sessionId);
@@ -31871,27 +31947,24 @@ var AcpSessionManager = class {
31871
31947
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
31872
31948
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
31873
31949
  }
31874
- if (this.#activeCommandIds.has(options.sessionId)) {
31875
- throw new AcpExecutionError("Another prompt is already running in this conversation", {}, "session_busy");
31876
- }
31877
- this.#activeCommandIds.set(options.sessionId, options.commandId);
31950
+ this.#executions.begin(options.sessionId, options.commandId);
31878
31951
  try {
31879
- if (this.#cancellationRequests.has(options.sessionId)) {
31952
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
31880
31953
  return { stopReason: "cancelled" };
31881
31954
  }
31882
31955
  const runtime = await this.ensureSession(options);
31883
31956
  if (this.#nativeOperations.has(`${options.agent.id}:${runtime.nativeSessionId()}`)) {
31884
31957
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
31885
31958
  }
31886
- if (this.#cancellationRequests.has(options.sessionId)) {
31959
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
31887
31960
  return { stopReason: "cancelled" };
31888
31961
  }
31889
31962
  try {
31890
31963
  const result = await runtime.prompt(options);
31891
- return this.#cancellationRequests.has(options.sessionId) ? { stopReason: "cancelled" } : result;
31964
+ return this.#executions.cancellation(options.sessionId) !== void 0 ? { stopReason: "cancelled" } : result;
31892
31965
  } catch (error62) {
31893
- if (this.#cancellationRequests.has(options.sessionId)) {
31894
- await this.#cancellationRequests.get(options.sessionId);
31966
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
31967
+ await this.#executions.cancellation(options.sessionId);
31895
31968
  return { stopReason: "cancelled" };
31896
31969
  }
31897
31970
  throw error62;
@@ -31905,7 +31978,7 @@ var AcpSessionManager = class {
31905
31978
  async () => await failedRuntime.close()
31906
31979
  );
31907
31980
  }
31908
- if (this.#cancellationRequests.has(options.sessionId)) {
31981
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
31909
31982
  return { stopReason: "cancelled" };
31910
31983
  }
31911
31984
  if (error62 instanceof AcpExecutionError) {
@@ -31916,13 +31989,10 @@ var AcpSessionManager = class {
31916
31989
  });
31917
31990
  } finally {
31918
31991
  try {
31919
- await this.#cancellationRequests.get(options.sessionId);
31992
+ await this.#executions.cancellation(options.sessionId);
31920
31993
  } finally {
31921
- if (this.#activeCommandIds.get(options.sessionId) === options.commandId) {
31922
- this.#activeCommandIds.delete(options.sessionId);
31923
- }
31924
- this.#cancellationRequests.delete(options.sessionId);
31925
- this.#touchSession(options.sessionId);
31994
+ this.#executions.finish(options.sessionId, options.commandId);
31995
+ this.#sessions.touch(options.sessionId);
31926
31996
  this.#processes.notifyChanged();
31927
31997
  }
31928
31998
  }
@@ -31962,38 +32032,38 @@ var AcpSessionManager = class {
31962
32032
  createNativeSession = async (options) => {
31963
32033
  const temporarySessionId = `new-${crypto.randomUUID()}`;
31964
32034
  const temporaryOptions = { ...options, sessionId: temporarySessionId };
31965
- this.#beginSessionUse(temporarySessionId);
31966
- try {
31967
- const runtime = await this.#startPreparation(temporaryOptions);
31968
- let adopted = false;
32035
+ return await this.#sessions.use(temporarySessionId, async () => {
31969
32036
  try {
31970
- const nativeSessionId = runtime.nativeSessionId();
31971
- if (!isNativeChatSessionId(options.agent.id, nativeSessionId) || this.#sessions.has(nativeSessionId) || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
31972
- throw new AcpExecutionError(
31973
- "The Agent returned an invalid new Session ID",
31974
- {},
31975
- "invalid_native_session_response"
31976
- );
32037
+ const runtime = await this.#startPreparation(temporaryOptions);
32038
+ let adopted = false;
32039
+ try {
32040
+ const nativeSessionId = runtime.nativeSessionId();
32041
+ if (!isNativeChatSessionId(options.agent.id, nativeSessionId) || this.#sessions.get(nativeSessionId) !== void 0 || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
32042
+ throw new AcpExecutionError(
32043
+ "The Agent returned an invalid new Session ID",
32044
+ {},
32045
+ "invalid_native_session_response"
32046
+ );
32047
+ }
32048
+ runtime.adoptNativeSessionId(nativeSessionId);
32049
+ this.#sessions.detach(temporarySessionId, runtime);
32050
+ this.#sessions.retain(nativeSessionId, runtime);
32051
+ this.#processes.retain(runtime, this.#retention(nativeSessionId, "execution"));
32052
+ adopted = true;
32053
+ return nativeSessionId;
32054
+ } finally {
32055
+ if (!adopted) {
32056
+ await this.#retireSessionProcess(
32057
+ temporarySessionId,
32058
+ runtime,
32059
+ async () => await runtime.closeProcess()
32060
+ );
32061
+ }
31977
32062
  }
31978
- runtime.adoptNativeSessionId(nativeSessionId);
31979
- this.#sessions.delete(temporarySessionId);
31980
- this.#sessions.set(nativeSessionId, runtime);
31981
- this.#processes.retain(runtime, this.#retention(nativeSessionId, "execution"));
31982
- adopted = true;
31983
- return nativeSessionId;
31984
32063
  } finally {
31985
- if (!adopted) {
31986
- await this.#retireSessionProcess(
31987
- temporarySessionId,
31988
- runtime,
31989
- async () => await runtime.closeProcess()
31990
- );
31991
- }
32064
+ this.#store.remove(temporarySessionId);
31992
32065
  }
31993
- } finally {
31994
- this.#store.remove(temporarySessionId);
31995
- this.#endSessionUse(temporarySessionId);
31996
- }
32066
+ });
31997
32067
  };
31998
32068
  ensureSession = async (options) => {
31999
32069
  const existing = this.#sessions.get(options.sessionId);
@@ -32005,7 +32075,7 @@ var AcpSessionManager = class {
32005
32075
  "session_identity_mismatch"
32006
32076
  );
32007
32077
  }
32008
- this.#touchExpectedSession(options.sessionId, existing);
32078
+ this.#sessions.touch(options.sessionId, existing);
32009
32079
  return existing;
32010
32080
  }
32011
32081
  await this.prepareSession(options);
@@ -32025,7 +32095,7 @@ var AcpSessionManager = class {
32025
32095
  "session_identity_mismatch"
32026
32096
  );
32027
32097
  }
32028
- this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
32098
+ this.#nativeBrowserSessions.touch(options.sessionId, existing);
32029
32099
  return existing;
32030
32100
  }
32031
32101
  await this.prepareNativeSessionBrowser(options);
@@ -32036,17 +32106,16 @@ var AcpSessionManager = class {
32036
32106
  return started;
32037
32107
  }
32038
32108
  listNativeSessions = async (options, cursor) => {
32039
- this.#beginNativeBrowserUse(options.sessionId);
32040
- try {
32109
+ return await this.#nativeBrowserSessions.use(options.sessionId, async () => {
32041
32110
  return await (await this.#ensureNativeSessionBrowser(options)).listNativeSessions(cursor, options.signal);
32042
- } finally {
32043
- this.#endNativeBrowserUse(options.sessionId);
32044
- }
32111
+ });
32045
32112
  };
32046
32113
  previewNativeSession = async (options, nativeSessionId) => {
32047
32114
  const key = `${options.agent.id}:${nativeSessionId}`;
32048
32115
  const linked = this.#store.findNative(options.agent.id, nativeSessionId);
32049
- if (this.#nativeOperations.has(key) || linked.some((entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId))) {
32116
+ if (this.#nativeOperations.has(key) || linked.some(
32117
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
32118
+ )) {
32050
32119
  throw new AcpExecutionError(
32051
32120
  "Wait for the native session's running operation to finish",
32052
32121
  {},
@@ -32073,8 +32142,7 @@ var AcpSessionManager = class {
32073
32142
  }
32074
32143
  };
32075
32144
  loadSessionHistory = async (options) => {
32076
- this.#beginSessionUse(options.sessionId);
32077
- try {
32145
+ return await this.#sessions.use(options.sessionId, async () => {
32078
32146
  const runtime = await this.ensureSession(options);
32079
32147
  const stored = this.#store.read(options.sessionId);
32080
32148
  if (stored instanceof FoundAcpStoredSession && this.#nativeHistoryReader !== null) {
@@ -32088,9 +32156,7 @@ var AcpSessionManager = class {
32088
32156
  }
32089
32157
  }
32090
32158
  return await runtime.loadHistory(options.signal);
32091
- } finally {
32092
- this.#endSessionUse(options.sessionId);
32093
- }
32159
+ });
32094
32160
  };
32095
32161
  openNativeSession = async (options, origin) => {
32096
32162
  const key = `${options.agent.id}:${origin.nativeSessionId}`;
@@ -32098,7 +32164,9 @@ var AcpSessionManager = class {
32098
32164
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
32099
32165
  }
32100
32166
  const linked = this.#store.findNative(options.agent.id, origin.nativeSessionId);
32101
- if (linked.some((entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId))) {
32167
+ if (linked.some(
32168
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
32169
+ )) {
32102
32170
  throw new AcpExecutionError(
32103
32171
  "Wait for the native session's running operation to finish",
32104
32172
  {},
@@ -32107,13 +32175,12 @@ var AcpSessionManager = class {
32107
32175
  }
32108
32176
  const existing = linked[0];
32109
32177
  if (origin.kind === "import" && existing !== void 0) {
32110
- this.#beginSessionUse(existing.sessionId);
32111
- try {
32178
+ return await this.#sessions.use(existing.sessionId, async () => {
32112
32179
  const runtime = await this.ensureSession({
32113
32180
  ...options,
32114
32181
  sessionId: existing.sessionId
32115
32182
  });
32116
- if (this.#activeCommandIds.has(existing.sessionId) || this.#nativeOperations.has(key)) {
32183
+ if (this.#executions.command(existing.sessionId) !== void 0 || this.#nativeOperations.has(key)) {
32117
32184
  throw new AcpExecutionError(
32118
32185
  "Wait for the native session's running operation to finish",
32119
32186
  {},
@@ -32130,48 +32197,49 @@ var AcpSessionManager = class {
32130
32197
  this.#nativeOperations.delete(key);
32131
32198
  this.#processes.notifyChanged();
32132
32199
  }
32133
- } finally {
32134
- this.#endSessionUse(existing.sessionId);
32135
- }
32200
+ });
32136
32201
  }
32137
- if (this.#sessions.has(options.sessionId) || this.#preparing.has(options.sessionId) || this.#store.read(options.sessionId) instanceof FoundAcpStoredSession || this.#store.read(options.sessionId) instanceof DeletedAcpStoredSession) {
32202
+ if (this.#sessions.get(options.sessionId) !== void 0 || this.#sessions.pending(options.sessionId) !== void 0 || this.#store.read(options.sessionId) instanceof FoundAcpStoredSession || this.#store.read(options.sessionId) instanceof DeletedAcpStoredSession) {
32138
32203
  throw new AcpExecutionError("The destination conversation already exists", {}, "session_identity_mismatch");
32139
32204
  }
32140
- this.#beginSessionUse(options.sessionId);
32141
- this.#nativeOperations.add(key);
32142
- try {
32143
- const runtime = await this.#startPreparation({ ...options, origin });
32144
- const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
32145
- this.#nativeOperations.add(openedKey);
32205
+ return await this.#sessions.use(options.sessionId, async () => {
32206
+ this.#nativeOperations.add(key);
32146
32207
  try {
32147
- return {
32148
- sessionId: options.sessionId,
32149
- prepared: runtime.prepared()
32150
- };
32208
+ const runtime = await this.#startPreparation({ ...options, origin });
32209
+ const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
32210
+ this.#nativeOperations.add(openedKey);
32211
+ try {
32212
+ return {
32213
+ sessionId: options.sessionId,
32214
+ prepared: runtime.prepared()
32215
+ };
32216
+ } finally {
32217
+ this.#nativeOperations.delete(openedKey);
32218
+ this.#processes.notifyChanged();
32219
+ }
32220
+ } catch (error62) {
32221
+ this.#store.remove(options.sessionId);
32222
+ const runtime = this.#sessions.get(options.sessionId);
32223
+ if (runtime !== void 0) {
32224
+ await this.#retireSessionProcess(
32225
+ options.sessionId,
32226
+ runtime,
32227
+ async () => await runtime.closeProcess()
32228
+ );
32229
+ }
32230
+ throw error62;
32151
32231
  } finally {
32152
- this.#nativeOperations.delete(openedKey);
32153
- this.#processes.notifyChanged();
32154
- }
32155
- } catch (error62) {
32156
- this.#store.remove(options.sessionId);
32157
- const runtime = this.#sessions.get(options.sessionId);
32158
- if (runtime !== void 0) {
32159
- await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.closeProcess());
32232
+ this.#nativeOperations.delete(key);
32160
32233
  }
32161
- throw error62;
32162
- } finally {
32163
- this.#nativeOperations.delete(key);
32164
- this.#endSessionUse(options.sessionId);
32165
- }
32234
+ });
32166
32235
  };
32167
32236
  deleteNativeSession = async (options, nativeSessionId) => {
32168
- this.#beginNativeBrowserUse(options.sessionId);
32169
- try {
32237
+ return await this.#nativeBrowserSessions.use(options.sessionId, async () => {
32170
32238
  const runtime = await this.#ensureNativeSessionBrowser(options);
32171
32239
  const key = `${options.agent.id}:${nativeSessionId}`;
32172
32240
  const linked = this.#store.findNative(options.agent.id, nativeSessionId);
32173
32241
  if (this.#nativeOperations.has(key) || linked.some(
32174
- (entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId)
32242
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
32175
32243
  )) {
32176
32244
  throw new AcpExecutionError(
32177
32245
  "Wait for the native session's running operation to finish",
@@ -32202,9 +32270,7 @@ var AcpSessionManager = class {
32202
32270
  this.#nativeOperations.delete(key);
32203
32271
  this.#processes.notifyChanged();
32204
32272
  }
32205
- } finally {
32206
- this.#endNativeBrowserUse(options.sessionId);
32207
- }
32273
+ });
32208
32274
  };
32209
32275
  };
32210
32276