@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/runtime.js CHANGED
@@ -22846,8 +22846,8 @@ function acpEndpointTerminationError(termination) {
22846
22846
  case "closed":
22847
22847
  return new AcpExecutionError("The ACP endpoint is closed", {}, "acp_endpoint_closed");
22848
22848
  default: {
22849
- const unreachable = termination;
22850
- return unreachable;
22849
+ const unreachable2 = termination;
22850
+ return unreachable2;
22851
22851
  }
22852
22852
  }
22853
22853
  }
@@ -35160,6 +35160,191 @@ var AcpProcessSession = class _AcpProcessSession {
35160
35160
  };
35161
35161
  };
35162
35162
 
35163
+ // src/acp/retained-sessions.ts
35164
+ var RetainedSessions = class {
35165
+ #entries = /* @__PURE__ */ new Map();
35166
+ #events;
35167
+ constructor(events) {
35168
+ this.#events = events;
35169
+ }
35170
+ #entry = (id) => this.#entries.get(id) ?? { lifecycle: { kind: "empty" }, users: 0 };
35171
+ #write = (id, entry) => {
35172
+ if (entry.lifecycle.kind === "empty" && entry.users === 0) {
35173
+ this.#entries.delete(id);
35174
+ } else {
35175
+ this.#entries.set(id, entry);
35176
+ }
35177
+ };
35178
+ #resource = (entry) => entry.lifecycle.kind === "preparing" ? entry.lifecycle.resource : entry.lifecycle;
35179
+ get = (id) => {
35180
+ const resource = this.#resource(this.#entry(id));
35181
+ return resource.kind === "ready" ? resource.runtime : void 0;
35182
+ };
35183
+ pending = (id) => {
35184
+ const state = this.#entry(id).lifecycle;
35185
+ return state.kind === "preparing" ? state.completion : void 0;
35186
+ };
35187
+ isBusy = (id) => {
35188
+ const entry = this.#entry(id);
35189
+ return entry.users > 0 || entry.lifecycle.kind === "preparing";
35190
+ };
35191
+ touch = (id, expected = this.get(id)) => {
35192
+ if (expected !== void 0 && this.get(id) === expected) {
35193
+ this.#events.touch(expected);
35194
+ }
35195
+ };
35196
+ #setResource = (id, resource) => {
35197
+ const entry = this.#entry(id);
35198
+ this.#write(id, {
35199
+ ...entry,
35200
+ lifecycle: entry.lifecycle.kind === "preparing" ? { ...entry.lifecycle, resource } : resource
35201
+ });
35202
+ };
35203
+ retain = (id, runtime) => {
35204
+ this.#setResource(id, { kind: "ready", runtime });
35205
+ };
35206
+ detach = (id, expected) => {
35207
+ if (this.get(id) !== expected) {
35208
+ return false;
35209
+ }
35210
+ this.#setResource(id, { kind: "empty" });
35211
+ return true;
35212
+ };
35213
+ entries = () => {
35214
+ const result = [];
35215
+ for (const id of this.#entries.keys()) {
35216
+ const runtime = this.get(id);
35217
+ if (runtime !== void 0) {
35218
+ result.push([id, runtime]);
35219
+ }
35220
+ }
35221
+ return result;
35222
+ };
35223
+ prepare = (id, start) => {
35224
+ const pending = this.pending(id);
35225
+ if (pending !== void 0) {
35226
+ return pending;
35227
+ }
35228
+ const completion = Promise.withResolvers();
35229
+ const entry = this.#entry(id);
35230
+ this.#write(id, {
35231
+ ...entry,
35232
+ lifecycle: {
35233
+ kind: "preparing",
35234
+ completion: completion.promise,
35235
+ resource: this.#resource(entry)
35236
+ }
35237
+ });
35238
+ const run = async () => {
35239
+ try {
35240
+ return await start();
35241
+ } finally {
35242
+ const current = this.#entry(id);
35243
+ if (current.lifecycle.kind === "preparing" && current.lifecycle.completion === completion.promise) {
35244
+ this.#write(id, { ...current, lifecycle: current.lifecycle.resource });
35245
+ }
35246
+ this.#events.changed();
35247
+ }
35248
+ };
35249
+ void run().then(completion.resolve, completion.reject);
35250
+ return completion.promise;
35251
+ };
35252
+ use = async (id, operation) => {
35253
+ const entry = this.#entry(id);
35254
+ this.#write(id, { ...entry, users: entry.users + 1 });
35255
+ this.touch(id);
35256
+ try {
35257
+ return await operation();
35258
+ } finally {
35259
+ const current = this.#entry(id);
35260
+ this.#write(id, { ...current, users: current.users - 1 });
35261
+ this.touch(id);
35262
+ this.#events.changed();
35263
+ }
35264
+ };
35265
+ };
35266
+
35267
+ // src/acp/session-executions.ts
35268
+ var unreachable = (state) => {
35269
+ throw new AcpExecutionError("Unexpected execution state", { cause: state });
35270
+ };
35271
+ var SessionExecutions = class {
35272
+ #entries = /* @__PURE__ */ new Map();
35273
+ command = (id) => {
35274
+ const state = this.#entries.get(id);
35275
+ switch (state?.kind) {
35276
+ case "running":
35277
+ case "cancelling":
35278
+ return state.commandId;
35279
+ case "cancelled-before-start":
35280
+ case void 0:
35281
+ return void 0;
35282
+ default:
35283
+ return unreachable(state);
35284
+ }
35285
+ };
35286
+ cancellation = (id) => {
35287
+ const state = this.#entries.get(id);
35288
+ switch (state?.kind) {
35289
+ case "cancelling":
35290
+ case "cancelled-before-start":
35291
+ return state.completion;
35292
+ case "running":
35293
+ case void 0:
35294
+ return void 0;
35295
+ default:
35296
+ return unreachable(state);
35297
+ }
35298
+ };
35299
+ isBusy = (id) => this.#entries.has(id);
35300
+ begin = (id, commandId) => {
35301
+ const state = this.#entries.get(id);
35302
+ switch (state?.kind) {
35303
+ case "running":
35304
+ case "cancelling":
35305
+ throw new AcpExecutionError(
35306
+ "Another prompt is already running in this conversation",
35307
+ {},
35308
+ "session_busy"
35309
+ );
35310
+ case "cancelled-before-start":
35311
+ case void 0:
35312
+ break;
35313
+ default:
35314
+ unreachable(state);
35315
+ }
35316
+ this.#entries.set(
35317
+ id,
35318
+ state?.kind === "cancelled-before-start" ? { kind: "cancelling", commandId, completion: state.completion } : { kind: "running", commandId }
35319
+ );
35320
+ };
35321
+ cancel = (id, stop) => {
35322
+ const state = this.#entries.get(id);
35323
+ switch (state?.kind) {
35324
+ case "cancelling":
35325
+ case "cancelled-before-start":
35326
+ return state.completion;
35327
+ case "running":
35328
+ case void 0:
35329
+ break;
35330
+ default:
35331
+ return unreachable(state);
35332
+ }
35333
+ const completion = Promise.withResolvers();
35334
+ this.#entries.set(
35335
+ id,
35336
+ state?.kind === "running" ? { kind: "cancelling", commandId: state.commandId, completion: completion.promise } : { kind: "cancelled-before-start", completion: completion.promise }
35337
+ );
35338
+ void (async () => await stop())().then(completion.resolve, completion.reject);
35339
+ return completion.promise;
35340
+ };
35341
+ finish = (id, commandId) => {
35342
+ if (this.command(id) === commandId) {
35343
+ this.#entries.delete(id);
35344
+ }
35345
+ };
35346
+ };
35347
+
35163
35348
  // src/native-sessions/chat-agent.ts
35164
35349
  function supportsNativeChat(agentId) {
35165
35350
  return agentId === "codex" || agentId === "claude" || agentId === "opencode" || agentId === "pi";
@@ -35371,18 +35556,16 @@ var AcpProcessPool = class {
35371
35556
  var SESSION_CONTROL_TIMEOUT_MILLISECONDS = 3e4;
35372
35557
  var AcpSessionManager = class {
35373
35558
  #processes = new AcpProcessPool();
35374
- #activeCommandIds = /* @__PURE__ */ new Map();
35559
+ #sessions = new RetainedSessions({ touch: this.#processes.touch, changed: this.#processes.notifyChanged });
35560
+ #nativeBrowserSessions = new RetainedSessions({
35561
+ touch: this.#processes.touch,
35562
+ changed: this.#processes.notifyChanged
35563
+ });
35564
+ #executions = new SessionExecutions();
35375
35565
  #agentSettingsObserver = new NullAcpSessionObserver();
35376
35566
  #nativeOperations = /* @__PURE__ */ new Set();
35377
35567
  #nativeBrowserObserver = new NullAcpSessionObserver();
35378
- #nativeBrowserPreparing = /* @__PURE__ */ new Map();
35379
- #nativeBrowserSessions = /* @__PURE__ */ new Map();
35380
35568
  #nativeBrowserStore = new MemoryAcpSessionStore();
35381
- #nativeBrowserUseCounts = /* @__PURE__ */ new Map();
35382
- #cancellationRequests = /* @__PURE__ */ new Map();
35383
- #preparing = /* @__PURE__ */ new Map();
35384
- #sessionUseCounts = /* @__PURE__ */ new Map();
35385
- #sessions = /* @__PURE__ */ new Map();
35386
35569
  #store;
35387
35570
  #nativeHistoryReader;
35388
35571
  #sessionIdentity;
@@ -35396,60 +35579,6 @@ var AcpSessionManager = class {
35396
35579
  setObserver = (observer) => {
35397
35580
  this.#observer = observer;
35398
35581
  };
35399
- #touchSession(sessionId) {
35400
- const runtime = this.#sessions.get(sessionId);
35401
- if (runtime === void 0) {
35402
- return;
35403
- }
35404
- this.#processes.touch(runtime);
35405
- }
35406
- #touchExpectedSession(sessionId, expected) {
35407
- if (this.#sessions.get(sessionId) !== expected) {
35408
- return;
35409
- }
35410
- this.#processes.touch(expected);
35411
- }
35412
- #touchNativeBrowserSession(sessionId) {
35413
- const runtime = this.#nativeBrowserSessions.get(sessionId);
35414
- if (runtime === void 0) {
35415
- return;
35416
- }
35417
- this.#processes.touch(runtime);
35418
- }
35419
- #touchExpectedNativeBrowserSession(sessionId, expected) {
35420
- if (this.#nativeBrowserSessions.get(sessionId) !== expected) {
35421
- return;
35422
- }
35423
- this.#processes.touch(expected);
35424
- }
35425
- #beginSessionUse(sessionId) {
35426
- this.#sessionUseCounts.set(sessionId, (this.#sessionUseCounts.get(sessionId) ?? 0) + 1);
35427
- this.#touchSession(sessionId);
35428
- }
35429
- #endSessionUse(sessionId) {
35430
- const count = this.#sessionUseCounts.get(sessionId) ?? 0;
35431
- if (count <= 1) {
35432
- this.#sessionUseCounts.delete(sessionId);
35433
- } else {
35434
- this.#sessionUseCounts.set(sessionId, count - 1);
35435
- }
35436
- this.#touchSession(sessionId);
35437
- this.#processes.notifyChanged();
35438
- }
35439
- #beginNativeBrowserUse(sessionId) {
35440
- this.#nativeBrowserUseCounts.set(sessionId, (this.#nativeBrowserUseCounts.get(sessionId) ?? 0) + 1);
35441
- this.#touchNativeBrowserSession(sessionId);
35442
- }
35443
- #endNativeBrowserUse(sessionId) {
35444
- const count = this.#nativeBrowserUseCounts.get(sessionId) ?? 0;
35445
- if (count <= 1) {
35446
- this.#nativeBrowserUseCounts.delete(sessionId);
35447
- } else {
35448
- this.#nativeBrowserUseCounts.set(sessionId, count - 1);
35449
- }
35450
- this.#touchNativeBrowserSession(sessionId);
35451
- this.#processes.notifyChanged();
35452
- }
35453
35582
  #isNativeSessionOperationActive(sessionId) {
35454
35583
  const stored = this.#store.read(sessionId);
35455
35584
  return stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`);
@@ -35457,20 +35586,17 @@ var AcpSessionManager = class {
35457
35586
  #retention(sessionId, kind) {
35458
35587
  if (kind === "execution") {
35459
35588
  return {
35460
- isBusy: () => this.#activeCommandIds.has(sessionId) || this.#cancellationRequests.has(sessionId) || this.#preparing.has(sessionId) || (this.#sessionUseCounts.get(sessionId) ?? 0) > 0 || this.#isNativeSessionOperationActive(sessionId),
35589
+ isBusy: () => this.#executions.isBusy(sessionId) || this.#sessions.isBusy(sessionId) || this.#isNativeSessionOperationActive(sessionId),
35461
35590
  retire: (runtime) => this.#retireSessionProcess(sessionId, runtime, () => runtime.close())
35462
35591
  };
35463
35592
  }
35464
35593
  return {
35465
- isBusy: () => this.#nativeBrowserPreparing.has(sessionId) || (this.#nativeBrowserUseCounts.get(sessionId) ?? 0) > 0,
35594
+ isBusy: () => this.#nativeBrowserSessions.isBusy(sessionId),
35466
35595
  retire: (runtime) => this.#retireNativeBrowserProcess(sessionId, runtime, () => runtime.close())
35467
35596
  };
35468
35597
  }
35469
35598
  async #retireSessionProcess(sessionId, runtime, close) {
35470
- const retained = this.#sessions.get(sessionId) === runtime;
35471
- if (retained) {
35472
- this.#sessions.delete(sessionId);
35473
- }
35599
+ const retained = this.#sessions.detach(sessionId, runtime);
35474
35600
  try {
35475
35601
  await this.#processes.retire(runtime, close);
35476
35602
  } finally {
@@ -35483,9 +35609,8 @@ var AcpSessionManager = class {
35483
35609
  }
35484
35610
  }
35485
35611
  async #retireNativeBrowserProcess(sessionId, runtime, close) {
35486
- const retained = this.#nativeBrowserSessions.get(sessionId) === runtime;
35612
+ const retained = this.#nativeBrowserSessions.detach(sessionId, runtime);
35487
35613
  if (retained) {
35488
- this.#nativeBrowserSessions.delete(sessionId);
35489
35614
  this.#nativeBrowserStore.remove(sessionId);
35490
35615
  }
35491
35616
  await this.#processes.retire(runtime, close);
@@ -35496,7 +35621,7 @@ var AcpSessionManager = class {
35496
35621
  start,
35497
35622
  this.#retention(options.sessionId, "execution")
35498
35623
  );
35499
- this.#sessions.set(options.sessionId, runtime);
35624
+ this.#sessions.retain(options.sessionId, runtime);
35500
35625
  return runtime;
35501
35626
  }
35502
35627
  async #startRetainedNativeBrowserProcess(options, start) {
@@ -35505,140 +35630,105 @@ var AcpSessionManager = class {
35505
35630
  start,
35506
35631
  this.#retention(options.sessionId, "browser")
35507
35632
  );
35508
- this.#nativeBrowserSessions.set(options.sessionId, runtime);
35633
+ this.#nativeBrowserSessions.retain(options.sessionId, runtime);
35509
35634
  return runtime;
35510
35635
  }
35511
35636
  async #startNativeBrowserPreparation(options) {
35512
- const operation = (async () => {
35513
- try {
35514
- return await this.#startRetainedNativeBrowserProcess(
35637
+ return await this.#nativeBrowserSessions.prepare(options.sessionId, async () => {
35638
+ return await this.#startRetainedNativeBrowserProcess(
35639
+ options,
35640
+ async () => await AcpProcessSession.startNativeSessionBrowser(
35515
35641
  options,
35516
- async () => await AcpProcessSession.startNativeSessionBrowser(
35517
- options,
35518
- () => this.#nativeBrowserObserver,
35519
- this.#nativeBrowserStore
35520
- )
35521
- );
35522
- } finally {
35523
- this.#nativeBrowserPreparing.delete(options.sessionId);
35524
- this.#processes.notifyChanged();
35525
- }
35526
- })();
35527
- this.#nativeBrowserPreparing.set(options.sessionId, operation);
35528
- return await operation;
35642
+ () => this.#nativeBrowserObserver,
35643
+ this.#nativeBrowserStore
35644
+ )
35645
+ );
35646
+ });
35529
35647
  }
35530
35648
  async #replaceNativeBrowserPreparation(existing, options) {
35531
- const operation = (async () => {
35532
- try {
35533
- await this.#retireNativeBrowserProcess(options.sessionId, existing, async () => await existing.close());
35534
- return await this.#startRetainedNativeBrowserProcess(
35649
+ return await this.#nativeBrowserSessions.prepare(options.sessionId, async () => {
35650
+ await this.#retireNativeBrowserProcess(options.sessionId, existing, async () => await existing.close());
35651
+ return await this.#startRetainedNativeBrowserProcess(
35652
+ options,
35653
+ async () => await AcpProcessSession.startNativeSessionBrowser(
35535
35654
  options,
35536
- async () => await AcpProcessSession.startNativeSessionBrowser(
35537
- options,
35538
- () => this.#nativeBrowserObserver,
35539
- this.#nativeBrowserStore
35540
- )
35541
- );
35542
- } finally {
35543
- this.#nativeBrowserPreparing.delete(options.sessionId);
35544
- this.#processes.notifyChanged();
35545
- }
35546
- })();
35547
- this.#nativeBrowserPreparing.set(options.sessionId, operation);
35548
- return await operation;
35655
+ () => this.#nativeBrowserObserver,
35656
+ this.#nativeBrowserStore
35657
+ )
35658
+ );
35659
+ });
35549
35660
  }
35550
35661
  async #startPreparation(options, metrics = unmeasuredOperationMetrics) {
35551
- const operation = (async () => {
35552
- try {
35553
- return await this.#startRetainedSessionProcess(
35554
- options,
35555
- async () => await AcpProcessSession.start(
35556
- 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,
35557
- () => this.#observer,
35558
- this.#store,
35559
- metrics
35560
- )
35561
- );
35562
- } finally {
35563
- this.#preparing.delete(options.sessionId);
35564
- this.#processes.notifyChanged();
35565
- }
35566
- })();
35567
- this.#preparing.set(options.sessionId, operation);
35568
- return await operation;
35662
+ return await this.#sessions.prepare(options.sessionId, async () => {
35663
+ return await this.#startRetainedSessionProcess(
35664
+ options,
35665
+ async () => await AcpProcessSession.start(
35666
+ 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,
35667
+ () => this.#observer,
35668
+ this.#store,
35669
+ metrics
35670
+ )
35671
+ );
35672
+ });
35569
35673
  }
35570
35674
  async #replacePreparation(existing, options, metrics) {
35571
- const operation = (async () => {
35572
- try {
35573
- await this.#retireSessionProcess(
35574
- options.sessionId,
35575
- existing,
35576
- async () => await metrics.measure("runtime-replacement-close", async () => await existing.close())
35577
- );
35578
- this.#store.remove(options.sessionId);
35579
- return await this.#startRetainedSessionProcess(
35580
- options,
35581
- async () => await AcpProcessSession.start(options, () => this.#observer, this.#store, metrics)
35582
- );
35583
- } finally {
35584
- this.#preparing.delete(options.sessionId);
35585
- this.#processes.notifyChanged();
35586
- }
35587
- })();
35588
- this.#preparing.set(options.sessionId, operation);
35589
- return await operation;
35675
+ return await this.#sessions.prepare(options.sessionId, async () => {
35676
+ await this.#retireSessionProcess(
35677
+ options.sessionId,
35678
+ existing,
35679
+ async () => await metrics.measure("runtime-replacement-close", async () => await existing.close())
35680
+ );
35681
+ this.#store.remove(options.sessionId);
35682
+ return await this.#startRetainedSessionProcess(
35683
+ options,
35684
+ async () => await AcpProcessSession.start(options, () => this.#observer, this.#store, metrics)
35685
+ );
35686
+ });
35590
35687
  }
35591
35688
  async #replaceExecutionPreparation(existing, options) {
35592
35689
  const desiredConfigOptions = existing.prepared().configOptions;
35593
- const operation = (async () => {
35690
+ return await this.#sessions.prepare(options.sessionId, async () => {
35691
+ await this.#retireSessionProcess(options.sessionId, existing, async () => await existing.close());
35692
+ const runtime = await this.#startRetainedSessionProcess(
35693
+ options,
35694
+ async () => await AcpProcessSession.start(options, () => this.#observer, this.#store)
35695
+ );
35594
35696
  try {
35595
- await this.#retireSessionProcess(options.sessionId, existing, async () => await existing.close());
35596
- const runtime = await this.#startRetainedSessionProcess(
35597
- options,
35598
- async () => await AcpProcessSession.start(options, () => this.#observer, this.#store)
35599
- );
35600
- try {
35601
- for (const desired of desiredConfigOptions) {
35602
- const current = runtime.prepared().configOptions.find((option) => option.id === desired.id);
35603
- if (current === void 0 || current.type !== desired.type) {
35604
- continue;
35605
- }
35606
- if (desired.type === "boolean") {
35607
- if (current.booleanValue !== desired.booleanValue) {
35608
- await runtime.configure({
35609
- ...options,
35610
- booleanValue: desired.booleanValue,
35611
- configId: desired.id,
35612
- requestId: `execution-prepare-${desired.id}`,
35613
- value: "",
35614
- valueType: "boolean"
35615
- });
35616
- }
35617
- continue;
35618
- }
35619
- if (current.currentValue !== desired.currentValue && current.values.some((value) => value.value === desired.currentValue)) {
35697
+ for (const desired of desiredConfigOptions) {
35698
+ const current = runtime.prepared().configOptions.find((option) => option.id === desired.id);
35699
+ if (current === void 0 || current.type !== desired.type) {
35700
+ continue;
35701
+ }
35702
+ if (desired.type === "boolean") {
35703
+ if (current.booleanValue !== desired.booleanValue) {
35620
35704
  await runtime.configure({
35621
35705
  ...options,
35622
- booleanValue: false,
35706
+ booleanValue: desired.booleanValue,
35623
35707
  configId: desired.id,
35624
35708
  requestId: `execution-prepare-${desired.id}`,
35625
- value: desired.currentValue,
35626
- valueType: "select"
35709
+ value: "",
35710
+ valueType: "boolean"
35627
35711
  });
35628
35712
  }
35713
+ continue;
35714
+ }
35715
+ if (current.currentValue !== desired.currentValue && current.values.some((value) => value.value === desired.currentValue)) {
35716
+ await runtime.configure({
35717
+ ...options,
35718
+ booleanValue: false,
35719
+ configId: desired.id,
35720
+ requestId: `execution-prepare-${desired.id}`,
35721
+ value: desired.currentValue,
35722
+ valueType: "select"
35723
+ });
35629
35724
  }
35630
- } catch (error62) {
35631
- await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
35632
- throw error62;
35633
35725
  }
35634
- return runtime;
35635
- } finally {
35636
- this.#preparing.delete(options.sessionId);
35637
- this.#processes.notifyChanged();
35726
+ } catch (error62) {
35727
+ await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
35728
+ throw error62;
35638
35729
  }
35639
- })();
35640
- this.#preparing.set(options.sessionId, operation);
35641
- return await operation;
35730
+ return runtime;
35731
+ });
35642
35732
  }
35643
35733
  prepareSession = async (options, metrics = unmeasuredOperationMetrics) => {
35644
35734
  const stored = metrics.measureSync("session-state-read", () => this.#store.read(options.sessionId));
@@ -35652,11 +35742,11 @@ var AcpSessionManager = class {
35652
35742
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
35653
35743
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
35654
35744
  }
35655
- const pending = this.#preparing.get(options.sessionId);
35745
+ const pending = this.#sessions.pending(options.sessionId);
35656
35746
  if (pending !== void 0) {
35657
35747
  const runtime2 = await metrics.measure("session-preparation-wait", async () => await pending);
35658
35748
  if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35659
- this.#touchExpectedSession(options.sessionId, runtime2);
35749
+ this.#sessions.touch(options.sessionId, runtime2);
35660
35750
  return runtime2.prepared();
35661
35751
  }
35662
35752
  return await this.prepareSession(options, metrics);
@@ -35664,7 +35754,7 @@ var AcpSessionManager = class {
35664
35754
  const existing = this.#sessions.get(options.sessionId);
35665
35755
  if (existing !== void 0) {
35666
35756
  if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35667
- this.#touchExpectedSession(options.sessionId, existing);
35757
+ this.#sessions.touch(options.sessionId, existing);
35668
35758
  return existing.prepared();
35669
35759
  }
35670
35760
  if (!existing.canRebind()) {
@@ -35718,11 +35808,10 @@ var AcpSessionManager = class {
35718
35808
  * Use inspectAgentSettings for Agent-scoped settings discovery.
35719
35809
  */
35720
35810
  inspectSession = async (options, metrics = unmeasuredOperationMetrics) => {
35721
- if (this.#preparing.has(options.sessionId) || this.#sessions.has(options.sessionId)) {
35811
+ if (this.#sessions.pending(options.sessionId) !== void 0 || this.#sessions.get(options.sessionId) !== void 0) {
35722
35812
  throw new AcpExecutionError("The configuration inspection session is already in use", {}, "session_busy");
35723
35813
  }
35724
- this.#beginSessionUse(options.sessionId);
35725
- try {
35814
+ return await this.#sessions.use(options.sessionId, async () => {
35726
35815
  const runtime = await this.#startPreparation(options, metrics);
35727
35816
  try {
35728
35817
  return runtime.prepared();
@@ -35733,12 +35822,10 @@ var AcpSessionManager = class {
35733
35822
  this.#store.remove(options.sessionId);
35734
35823
  }
35735
35824
  }
35736
- } finally {
35737
- this.#endSessionUse(options.sessionId);
35738
- }
35825
+ });
35739
35826
  };
35740
35827
  prepareExecutionSession = async (options) => {
35741
- const pending = this.#preparing.get(options.sessionId);
35828
+ const pending = this.#sessions.pending(options.sessionId);
35742
35829
  if (pending !== void 0) {
35743
35830
  await pending;
35744
35831
  return await this.prepareExecutionSession(options);
@@ -35755,7 +35842,7 @@ var AcpSessionManager = class {
35755
35842
  );
35756
35843
  }
35757
35844
  if (existing.usesEnvironment(options.agent.environment)) {
35758
- this.#touchExpectedSession(options.sessionId, existing);
35845
+ this.#sessions.touch(options.sessionId, existing);
35759
35846
  return existing.prepared();
35760
35847
  }
35761
35848
  if (existing.hasRunningTurn()) {
@@ -35768,11 +35855,11 @@ var AcpSessionManager = class {
35768
35855
  return (await this.#replaceExecutionPreparation(existing, options)).prepared();
35769
35856
  };
35770
35857
  prepareNativeSessionBrowser = async (options) => {
35771
- const pending = this.#nativeBrowserPreparing.get(options.sessionId);
35858
+ const pending = this.#nativeBrowserSessions.pending(options.sessionId);
35772
35859
  if (pending !== void 0) {
35773
35860
  const runtime2 = await pending;
35774
35861
  if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35775
- this.#touchExpectedNativeBrowserSession(options.sessionId, runtime2);
35862
+ this.#nativeBrowserSessions.touch(options.sessionId, runtime2);
35776
35863
  return runtime2.prepared();
35777
35864
  }
35778
35865
  return await this.prepareNativeSessionBrowser(options);
@@ -35780,7 +35867,7 @@ var AcpSessionManager = class {
35780
35867
  const existing = this.#nativeBrowserSessions.get(options.sessionId);
35781
35868
  if (existing !== void 0) {
35782
35869
  if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35783
- this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
35870
+ this.#nativeBrowserSessions.touch(options.sessionId, existing);
35784
35871
  return existing.prepared();
35785
35872
  }
35786
35873
  if (!existing.canRebind()) {
@@ -35797,45 +35884,34 @@ var AcpSessionManager = class {
35797
35884
  return runtime.prepared();
35798
35885
  };
35799
35886
  configureSession = async (options) => {
35800
- this.#beginSessionUse(options.sessionId);
35801
- try {
35887
+ return await this.#sessions.use(options.sessionId, async () => {
35802
35888
  const runtime = await this.ensureSession(options);
35803
35889
  return await runtime.configure(options);
35804
- } finally {
35805
- this.#endSessionUse(options.sessionId);
35806
- }
35890
+ });
35807
35891
  };
35808
35892
  cancelPrompt = async (sessionId) => {
35809
- const existing = this.#cancellationRequests.get(sessionId);
35810
- if (existing !== void 0) {
35811
- await existing;
35812
- return;
35813
- }
35814
- const cancellation = (async () => {
35893
+ await this.#executions.cancel(sessionId, async () => {
35815
35894
  const runtime = this.#sessions.get(sessionId);
35816
35895
  if (runtime !== void 0) {
35817
35896
  try {
35818
35897
  await runtime.cancel();
35819
35898
  } finally {
35820
- if (this.#sessions.get(sessionId) === runtime) {
35821
- this.#sessions.delete(sessionId);
35899
+ if (this.#sessions.detach(sessionId, runtime)) {
35822
35900
  this.#processes.releaseClosed(runtime);
35823
35901
  }
35824
35902
  }
35825
35903
  }
35826
- })();
35827
- this.#cancellationRequests.set(sessionId, cancellation);
35828
- await cancellation;
35904
+ });
35829
35905
  };
35830
35906
  steerPrompt = async (options) => {
35831
- if (this.#activeCommandIds.get(options.sessionId) !== options.targetCommandId) {
35907
+ if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
35832
35908
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
35833
35909
  }
35834
- const pending = this.#preparing.get(options.sessionId);
35910
+ const pending = this.#sessions.pending(options.sessionId);
35835
35911
  if (pending !== void 0) {
35836
35912
  await pending;
35837
35913
  }
35838
- if (this.#activeCommandIds.get(options.sessionId) !== options.targetCommandId) {
35914
+ if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
35839
35915
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
35840
35916
  }
35841
35917
  const runtime = this.#sessions.get(options.sessionId);
@@ -35873,27 +35949,24 @@ var AcpSessionManager = class {
35873
35949
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
35874
35950
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
35875
35951
  }
35876
- if (this.#activeCommandIds.has(options.sessionId)) {
35877
- throw new AcpExecutionError("Another prompt is already running in this conversation", {}, "session_busy");
35878
- }
35879
- this.#activeCommandIds.set(options.sessionId, options.commandId);
35952
+ this.#executions.begin(options.sessionId, options.commandId);
35880
35953
  try {
35881
- if (this.#cancellationRequests.has(options.sessionId)) {
35954
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
35882
35955
  return { stopReason: "cancelled" };
35883
35956
  }
35884
35957
  const runtime = await this.ensureSession(options);
35885
35958
  if (this.#nativeOperations.has(`${options.agent.id}:${runtime.nativeSessionId()}`)) {
35886
35959
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
35887
35960
  }
35888
- if (this.#cancellationRequests.has(options.sessionId)) {
35961
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
35889
35962
  return { stopReason: "cancelled" };
35890
35963
  }
35891
35964
  try {
35892
35965
  const result = await runtime.prompt(options);
35893
- return this.#cancellationRequests.has(options.sessionId) ? { stopReason: "cancelled" } : result;
35966
+ return this.#executions.cancellation(options.sessionId) !== void 0 ? { stopReason: "cancelled" } : result;
35894
35967
  } catch (error62) {
35895
- if (this.#cancellationRequests.has(options.sessionId)) {
35896
- await this.#cancellationRequests.get(options.sessionId);
35968
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
35969
+ await this.#executions.cancellation(options.sessionId);
35897
35970
  return { stopReason: "cancelled" };
35898
35971
  }
35899
35972
  throw error62;
@@ -35907,7 +35980,7 @@ var AcpSessionManager = class {
35907
35980
  async () => await failedRuntime.close()
35908
35981
  );
35909
35982
  }
35910
- if (this.#cancellationRequests.has(options.sessionId)) {
35983
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
35911
35984
  return { stopReason: "cancelled" };
35912
35985
  }
35913
35986
  if (error62 instanceof AcpExecutionError) {
@@ -35918,13 +35991,10 @@ var AcpSessionManager = class {
35918
35991
  });
35919
35992
  } finally {
35920
35993
  try {
35921
- await this.#cancellationRequests.get(options.sessionId);
35994
+ await this.#executions.cancellation(options.sessionId);
35922
35995
  } finally {
35923
- if (this.#activeCommandIds.get(options.sessionId) === options.commandId) {
35924
- this.#activeCommandIds.delete(options.sessionId);
35925
- }
35926
- this.#cancellationRequests.delete(options.sessionId);
35927
- this.#touchSession(options.sessionId);
35996
+ this.#executions.finish(options.sessionId, options.commandId);
35997
+ this.#sessions.touch(options.sessionId);
35928
35998
  this.#processes.notifyChanged();
35929
35999
  }
35930
36000
  }
@@ -35964,38 +36034,38 @@ var AcpSessionManager = class {
35964
36034
  createNativeSession = async (options) => {
35965
36035
  const temporarySessionId = `new-${crypto.randomUUID()}`;
35966
36036
  const temporaryOptions = { ...options, sessionId: temporarySessionId };
35967
- this.#beginSessionUse(temporarySessionId);
35968
- try {
35969
- const runtime = await this.#startPreparation(temporaryOptions);
35970
- let adopted = false;
36037
+ return await this.#sessions.use(temporarySessionId, async () => {
35971
36038
  try {
35972
- const nativeSessionId = runtime.nativeSessionId();
35973
- if (!isNativeChatSessionId(options.agent.id, nativeSessionId) || this.#sessions.has(nativeSessionId) || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
35974
- throw new AcpExecutionError(
35975
- "The Agent returned an invalid new Session ID",
35976
- {},
35977
- "invalid_native_session_response"
35978
- );
36039
+ const runtime = await this.#startPreparation(temporaryOptions);
36040
+ let adopted = false;
36041
+ try {
36042
+ const nativeSessionId = runtime.nativeSessionId();
36043
+ if (!isNativeChatSessionId(options.agent.id, nativeSessionId) || this.#sessions.get(nativeSessionId) !== void 0 || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
36044
+ throw new AcpExecutionError(
36045
+ "The Agent returned an invalid new Session ID",
36046
+ {},
36047
+ "invalid_native_session_response"
36048
+ );
36049
+ }
36050
+ runtime.adoptNativeSessionId(nativeSessionId);
36051
+ this.#sessions.detach(temporarySessionId, runtime);
36052
+ this.#sessions.retain(nativeSessionId, runtime);
36053
+ this.#processes.retain(runtime, this.#retention(nativeSessionId, "execution"));
36054
+ adopted = true;
36055
+ return nativeSessionId;
36056
+ } finally {
36057
+ if (!adopted) {
36058
+ await this.#retireSessionProcess(
36059
+ temporarySessionId,
36060
+ runtime,
36061
+ async () => await runtime.closeProcess()
36062
+ );
36063
+ }
35979
36064
  }
35980
- runtime.adoptNativeSessionId(nativeSessionId);
35981
- this.#sessions.delete(temporarySessionId);
35982
- this.#sessions.set(nativeSessionId, runtime);
35983
- this.#processes.retain(runtime, this.#retention(nativeSessionId, "execution"));
35984
- adopted = true;
35985
- return nativeSessionId;
35986
36065
  } finally {
35987
- if (!adopted) {
35988
- await this.#retireSessionProcess(
35989
- temporarySessionId,
35990
- runtime,
35991
- async () => await runtime.closeProcess()
35992
- );
35993
- }
36066
+ this.#store.remove(temporarySessionId);
35994
36067
  }
35995
- } finally {
35996
- this.#store.remove(temporarySessionId);
35997
- this.#endSessionUse(temporarySessionId);
35998
- }
36068
+ });
35999
36069
  };
36000
36070
  ensureSession = async (options) => {
36001
36071
  const existing = this.#sessions.get(options.sessionId);
@@ -36007,7 +36077,7 @@ var AcpSessionManager = class {
36007
36077
  "session_identity_mismatch"
36008
36078
  );
36009
36079
  }
36010
- this.#touchExpectedSession(options.sessionId, existing);
36080
+ this.#sessions.touch(options.sessionId, existing);
36011
36081
  return existing;
36012
36082
  }
36013
36083
  await this.prepareSession(options);
@@ -36027,7 +36097,7 @@ var AcpSessionManager = class {
36027
36097
  "session_identity_mismatch"
36028
36098
  );
36029
36099
  }
36030
- this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
36100
+ this.#nativeBrowserSessions.touch(options.sessionId, existing);
36031
36101
  return existing;
36032
36102
  }
36033
36103
  await this.prepareNativeSessionBrowser(options);
@@ -36038,17 +36108,16 @@ var AcpSessionManager = class {
36038
36108
  return started;
36039
36109
  }
36040
36110
  listNativeSessions = async (options, cursor) => {
36041
- this.#beginNativeBrowserUse(options.sessionId);
36042
- try {
36111
+ return await this.#nativeBrowserSessions.use(options.sessionId, async () => {
36043
36112
  return await (await this.#ensureNativeSessionBrowser(options)).listNativeSessions(cursor, options.signal);
36044
- } finally {
36045
- this.#endNativeBrowserUse(options.sessionId);
36046
- }
36113
+ });
36047
36114
  };
36048
36115
  previewNativeSession = async (options, nativeSessionId) => {
36049
36116
  const key = `${options.agent.id}:${nativeSessionId}`;
36050
36117
  const linked = this.#store.findNative(options.agent.id, nativeSessionId);
36051
- if (this.#nativeOperations.has(key) || linked.some((entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId))) {
36118
+ if (this.#nativeOperations.has(key) || linked.some(
36119
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
36120
+ )) {
36052
36121
  throw new AcpExecutionError(
36053
36122
  "Wait for the native session's running operation to finish",
36054
36123
  {},
@@ -36075,8 +36144,7 @@ var AcpSessionManager = class {
36075
36144
  }
36076
36145
  };
36077
36146
  loadSessionHistory = async (options) => {
36078
- this.#beginSessionUse(options.sessionId);
36079
- try {
36147
+ return await this.#sessions.use(options.sessionId, async () => {
36080
36148
  const runtime = await this.ensureSession(options);
36081
36149
  const stored = this.#store.read(options.sessionId);
36082
36150
  if (stored instanceof FoundAcpStoredSession && this.#nativeHistoryReader !== null) {
@@ -36090,9 +36158,7 @@ var AcpSessionManager = class {
36090
36158
  }
36091
36159
  }
36092
36160
  return await runtime.loadHistory(options.signal);
36093
- } finally {
36094
- this.#endSessionUse(options.sessionId);
36095
- }
36161
+ });
36096
36162
  };
36097
36163
  openNativeSession = async (options, origin) => {
36098
36164
  const key = `${options.agent.id}:${origin.nativeSessionId}`;
@@ -36100,7 +36166,9 @@ var AcpSessionManager = class {
36100
36166
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
36101
36167
  }
36102
36168
  const linked = this.#store.findNative(options.agent.id, origin.nativeSessionId);
36103
- if (linked.some((entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId))) {
36169
+ if (linked.some(
36170
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
36171
+ )) {
36104
36172
  throw new AcpExecutionError(
36105
36173
  "Wait for the native session's running operation to finish",
36106
36174
  {},
@@ -36109,13 +36177,12 @@ var AcpSessionManager = class {
36109
36177
  }
36110
36178
  const existing = linked[0];
36111
36179
  if (origin.kind === "import" && existing !== void 0) {
36112
- this.#beginSessionUse(existing.sessionId);
36113
- try {
36180
+ return await this.#sessions.use(existing.sessionId, async () => {
36114
36181
  const runtime = await this.ensureSession({
36115
36182
  ...options,
36116
36183
  sessionId: existing.sessionId
36117
36184
  });
36118
- if (this.#activeCommandIds.has(existing.sessionId) || this.#nativeOperations.has(key)) {
36185
+ if (this.#executions.command(existing.sessionId) !== void 0 || this.#nativeOperations.has(key)) {
36119
36186
  throw new AcpExecutionError(
36120
36187
  "Wait for the native session's running operation to finish",
36121
36188
  {},
@@ -36132,48 +36199,49 @@ var AcpSessionManager = class {
36132
36199
  this.#nativeOperations.delete(key);
36133
36200
  this.#processes.notifyChanged();
36134
36201
  }
36135
- } finally {
36136
- this.#endSessionUse(existing.sessionId);
36137
- }
36202
+ });
36138
36203
  }
36139
- 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) {
36204
+ 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) {
36140
36205
  throw new AcpExecutionError("The destination conversation already exists", {}, "session_identity_mismatch");
36141
36206
  }
36142
- this.#beginSessionUse(options.sessionId);
36143
- this.#nativeOperations.add(key);
36144
- try {
36145
- const runtime = await this.#startPreparation({ ...options, origin });
36146
- const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
36147
- this.#nativeOperations.add(openedKey);
36207
+ return await this.#sessions.use(options.sessionId, async () => {
36208
+ this.#nativeOperations.add(key);
36148
36209
  try {
36149
- return {
36150
- sessionId: options.sessionId,
36151
- prepared: runtime.prepared()
36152
- };
36210
+ const runtime = await this.#startPreparation({ ...options, origin });
36211
+ const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
36212
+ this.#nativeOperations.add(openedKey);
36213
+ try {
36214
+ return {
36215
+ sessionId: options.sessionId,
36216
+ prepared: runtime.prepared()
36217
+ };
36218
+ } finally {
36219
+ this.#nativeOperations.delete(openedKey);
36220
+ this.#processes.notifyChanged();
36221
+ }
36222
+ } catch (error62) {
36223
+ this.#store.remove(options.sessionId);
36224
+ const runtime = this.#sessions.get(options.sessionId);
36225
+ if (runtime !== void 0) {
36226
+ await this.#retireSessionProcess(
36227
+ options.sessionId,
36228
+ runtime,
36229
+ async () => await runtime.closeProcess()
36230
+ );
36231
+ }
36232
+ throw error62;
36153
36233
  } finally {
36154
- this.#nativeOperations.delete(openedKey);
36155
- this.#processes.notifyChanged();
36156
- }
36157
- } catch (error62) {
36158
- this.#store.remove(options.sessionId);
36159
- const runtime = this.#sessions.get(options.sessionId);
36160
- if (runtime !== void 0) {
36161
- await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.closeProcess());
36234
+ this.#nativeOperations.delete(key);
36162
36235
  }
36163
- throw error62;
36164
- } finally {
36165
- this.#nativeOperations.delete(key);
36166
- this.#endSessionUse(options.sessionId);
36167
- }
36236
+ });
36168
36237
  };
36169
36238
  deleteNativeSession = async (options, nativeSessionId) => {
36170
- this.#beginNativeBrowserUse(options.sessionId);
36171
- try {
36239
+ return await this.#nativeBrowserSessions.use(options.sessionId, async () => {
36172
36240
  const runtime = await this.#ensureNativeSessionBrowser(options);
36173
36241
  const key = `${options.agent.id}:${nativeSessionId}`;
36174
36242
  const linked = this.#store.findNative(options.agent.id, nativeSessionId);
36175
36243
  if (this.#nativeOperations.has(key) || linked.some(
36176
- (entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId)
36244
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
36177
36245
  )) {
36178
36246
  throw new AcpExecutionError(
36179
36247
  "Wait for the native session's running operation to finish",
@@ -36204,9 +36272,7 @@ var AcpSessionManager = class {
36204
36272
  this.#nativeOperations.delete(key);
36205
36273
  this.#processes.notifyChanged();
36206
36274
  }
36207
- } finally {
36208
- this.#endNativeBrowserUse(options.sessionId);
36209
- }
36275
+ });
36210
36276
  };
36211
36277
  };
36212
36278