@moxt-ai/mobius 0.0.125 → 0.0.127

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,301 @@ 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/native-session-browsers.ts
35268
+ var NativeSessionBrowsers = class {
35269
+ #sessions;
35270
+ #processes;
35271
+ #observer = new NullAcpSessionObserver();
35272
+ #store = new MemoryAcpSessionStore();
35273
+ constructor(processes) {
35274
+ this.#processes = processes;
35275
+ this.#sessions = new RetainedSessions({ touch: processes.touch, changed: processes.notifyChanged });
35276
+ }
35277
+ retire = async (sessionId, runtime) => {
35278
+ const retained = this.#sessions.detach(sessionId, runtime);
35279
+ if (retained) {
35280
+ this.#store.remove(sessionId);
35281
+ }
35282
+ await this.#processes.retire(runtime, () => runtime.close());
35283
+ };
35284
+ async #startRetained(options) {
35285
+ const runtime = await this.#processes.startRetained(
35286
+ options.signal,
35287
+ () => AcpProcessSession.startNativeSessionBrowser(options, () => this.#observer, this.#store),
35288
+ {
35289
+ isBusy: () => this.#sessions.isBusy(options.sessionId),
35290
+ retire: (runtime2) => this.retire(options.sessionId, runtime2)
35291
+ }
35292
+ );
35293
+ this.#sessions.retain(options.sessionId, runtime);
35294
+ return runtime;
35295
+ }
35296
+ async #startPreparation(options) {
35297
+ return await this.#sessions.prepare(options.sessionId, async () => {
35298
+ return await this.#startRetained(options);
35299
+ });
35300
+ }
35301
+ async #replacePreparation(existing, options) {
35302
+ return await this.#sessions.prepare(options.sessionId, async () => {
35303
+ await this.retire(options.sessionId, existing);
35304
+ return await this.#startRetained(options);
35305
+ });
35306
+ }
35307
+ prepare = async (options) => {
35308
+ const pending = this.#sessions.pending(options.sessionId);
35309
+ if (pending !== void 0) {
35310
+ const runtime2 = await pending;
35311
+ if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35312
+ this.#sessions.touch(options.sessionId, runtime2);
35313
+ return runtime2.prepared();
35314
+ }
35315
+ return await this.prepare(options);
35316
+ }
35317
+ const existing = this.#sessions.get(options.sessionId);
35318
+ if (existing !== void 0) {
35319
+ if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35320
+ this.#sessions.touch(options.sessionId, existing);
35321
+ return existing.prepared();
35322
+ }
35323
+ if (!existing.canRebind()) {
35324
+ throw new AcpExecutionError(
35325
+ "The agent or workspace cannot change after a conversation has started",
35326
+ {},
35327
+ "session_identity_mismatch"
35328
+ );
35329
+ }
35330
+ const runtime2 = await this.#replacePreparation(existing, options);
35331
+ return runtime2.prepared();
35332
+ }
35333
+ const runtime = await this.#startPreparation(options);
35334
+ return runtime.prepared();
35335
+ };
35336
+ async #ensure(options) {
35337
+ const existing = this.#sessions.get(options.sessionId);
35338
+ if (existing !== void 0) {
35339
+ if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35340
+ throw new AcpExecutionError(
35341
+ "The agent or workspace cannot change after a conversation has started",
35342
+ {},
35343
+ "session_identity_mismatch"
35344
+ );
35345
+ }
35346
+ this.#sessions.touch(options.sessionId, existing);
35347
+ return existing;
35348
+ }
35349
+ await this.prepare(options);
35350
+ const started = this.#sessions.get(options.sessionId);
35351
+ if (started === void 0) {
35352
+ throw new AcpExecutionError("ACP native session browser did not start");
35353
+ }
35354
+ return started;
35355
+ }
35356
+ use = async (options, operation) => await this.#sessions.use(options.sessionId, async () => await operation(await this.#ensure(options)));
35357
+ preview = async (options, nativeSessionId) => {
35358
+ return await this.#processes.runTransient(
35359
+ options.signal,
35360
+ async () => await AcpProcessSession.startNativeSessionBrowser(
35361
+ options,
35362
+ () => this.#observer,
35363
+ new MemoryAcpSessionStore()
35364
+ ),
35365
+ async (runtime) => {
35366
+ runtime.selectNativeSession(nativeSessionId);
35367
+ return await runtime.loadHistory(options.signal);
35368
+ },
35369
+ async (runtime) => await runtime.closeProcess()
35370
+ );
35371
+ };
35372
+ close = async () => {
35373
+ await Promise.all(this.#sessions.entries().map(async ([id, runtime]) => await this.retire(id, runtime)));
35374
+ };
35375
+ };
35376
+
35377
+ // src/acp/session-executions.ts
35378
+ var unreachable = (state) => {
35379
+ throw new AcpExecutionError("Unexpected execution state", { cause: state });
35380
+ };
35381
+ var SessionExecutions = class {
35382
+ #entries = /* @__PURE__ */ new Map();
35383
+ command = (id) => {
35384
+ const state = this.#entries.get(id);
35385
+ switch (state?.kind) {
35386
+ case "running":
35387
+ case "cancelling":
35388
+ return state.commandId;
35389
+ case "cancelled-before-start":
35390
+ case void 0:
35391
+ return void 0;
35392
+ default:
35393
+ return unreachable(state);
35394
+ }
35395
+ };
35396
+ cancellation = (id) => {
35397
+ const state = this.#entries.get(id);
35398
+ switch (state?.kind) {
35399
+ case "cancelling":
35400
+ case "cancelled-before-start":
35401
+ return state.completion;
35402
+ case "running":
35403
+ case void 0:
35404
+ return void 0;
35405
+ default:
35406
+ return unreachable(state);
35407
+ }
35408
+ };
35409
+ isBusy = (id) => this.#entries.has(id);
35410
+ begin = (id, commandId) => {
35411
+ const state = this.#entries.get(id);
35412
+ switch (state?.kind) {
35413
+ case "running":
35414
+ case "cancelling":
35415
+ throw new AcpExecutionError(
35416
+ "Another prompt is already running in this conversation",
35417
+ {},
35418
+ "session_busy"
35419
+ );
35420
+ case "cancelled-before-start":
35421
+ case void 0:
35422
+ break;
35423
+ default:
35424
+ unreachable(state);
35425
+ }
35426
+ this.#entries.set(
35427
+ id,
35428
+ state?.kind === "cancelled-before-start" ? { kind: "cancelling", commandId, completion: state.completion } : { kind: "running", commandId }
35429
+ );
35430
+ };
35431
+ cancel = (id, stop) => {
35432
+ const state = this.#entries.get(id);
35433
+ switch (state?.kind) {
35434
+ case "cancelling":
35435
+ case "cancelled-before-start":
35436
+ return state.completion;
35437
+ case "running":
35438
+ case void 0:
35439
+ break;
35440
+ default:
35441
+ return unreachable(state);
35442
+ }
35443
+ const completion = Promise.withResolvers();
35444
+ this.#entries.set(
35445
+ id,
35446
+ state?.kind === "running" ? { kind: "cancelling", commandId: state.commandId, completion: completion.promise } : { kind: "cancelled-before-start", completion: completion.promise }
35447
+ );
35448
+ void (async () => await stop())().then(completion.resolve, completion.reject);
35449
+ return completion.promise;
35450
+ };
35451
+ finish = (id, commandId) => {
35452
+ if (this.command(id) === commandId) {
35453
+ this.#entries.delete(id);
35454
+ }
35455
+ };
35456
+ };
35457
+
35163
35458
  // src/native-sessions/chat-agent.ts
35164
35459
  function supportsNativeChat(agentId) {
35165
35460
  return agentId === "codex" || agentId === "claude" || agentId === "opencode" || agentId === "pi";
@@ -35371,18 +35666,11 @@ var AcpProcessPool = class {
35371
35666
  var SESSION_CONTROL_TIMEOUT_MILLISECONDS = 3e4;
35372
35667
  var AcpSessionManager = class {
35373
35668
  #processes = new AcpProcessPool();
35374
- #activeCommandIds = /* @__PURE__ */ new Map();
35669
+ #sessions = new RetainedSessions({ touch: this.#processes.touch, changed: this.#processes.notifyChanged });
35670
+ #browsers = new NativeSessionBrowsers(this.#processes);
35671
+ #executions = new SessionExecutions();
35375
35672
  #agentSettingsObserver = new NullAcpSessionObserver();
35376
35673
  #nativeOperations = /* @__PURE__ */ new Set();
35377
- #nativeBrowserObserver = new NullAcpSessionObserver();
35378
- #nativeBrowserPreparing = /* @__PURE__ */ new Map();
35379
- #nativeBrowserSessions = /* @__PURE__ */ new Map();
35380
- #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
35674
  #store;
35387
35675
  #nativeHistoryReader;
35388
35676
  #sessionIdentity;
@@ -35396,81 +35684,18 @@ var AcpSessionManager = class {
35396
35684
  setObserver = (observer) => {
35397
35685
  this.#observer = observer;
35398
35686
  };
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
35687
  #isNativeSessionOperationActive(sessionId) {
35454
35688
  const stored = this.#store.read(sessionId);
35455
35689
  return stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`);
35456
35690
  }
35457
- #retention(sessionId, kind) {
35458
- if (kind === "execution") {
35459
- return {
35460
- isBusy: () => this.#activeCommandIds.has(sessionId) || this.#cancellationRequests.has(sessionId) || this.#preparing.has(sessionId) || (this.#sessionUseCounts.get(sessionId) ?? 0) > 0 || this.#isNativeSessionOperationActive(sessionId),
35461
- retire: (runtime) => this.#retireSessionProcess(sessionId, runtime, () => runtime.close())
35462
- };
35463
- }
35691
+ #retention(sessionId) {
35464
35692
  return {
35465
- isBusy: () => this.#nativeBrowserPreparing.has(sessionId) || (this.#nativeBrowserUseCounts.get(sessionId) ?? 0) > 0,
35466
- retire: (runtime) => this.#retireNativeBrowserProcess(sessionId, runtime, () => runtime.close())
35693
+ isBusy: () => this.#executions.isBusy(sessionId) || this.#sessions.isBusy(sessionId) || this.#isNativeSessionOperationActive(sessionId),
35694
+ retire: (runtime) => this.#retireSessionProcess(sessionId, runtime, () => runtime.close())
35467
35695
  };
35468
35696
  }
35469
35697
  async #retireSessionProcess(sessionId, runtime, close) {
35470
- const retained = this.#sessions.get(sessionId) === runtime;
35471
- if (retained) {
35472
- this.#sessions.delete(sessionId);
35473
- }
35698
+ const retained = this.#sessions.detach(sessionId, runtime);
35474
35699
  try {
35475
35700
  await this.#processes.retire(runtime, close);
35476
35701
  } finally {
@@ -35482,163 +35707,82 @@ var AcpSessionManager = class {
35482
35707
  }
35483
35708
  }
35484
35709
  }
35485
- async #retireNativeBrowserProcess(sessionId, runtime, close) {
35486
- const retained = this.#nativeBrowserSessions.get(sessionId) === runtime;
35487
- if (retained) {
35488
- this.#nativeBrowserSessions.delete(sessionId);
35489
- this.#nativeBrowserStore.remove(sessionId);
35490
- }
35491
- await this.#processes.retire(runtime, close);
35492
- }
35493
35710
  async #startRetainedSessionProcess(options, start) {
35494
- const runtime = await this.#processes.startRetained(
35495
- options.signal,
35496
- start,
35497
- this.#retention(options.sessionId, "execution")
35498
- );
35499
- this.#sessions.set(options.sessionId, runtime);
35500
- return runtime;
35501
- }
35502
- async #startRetainedNativeBrowserProcess(options, start) {
35503
- const runtime = await this.#processes.startRetained(
35504
- options.signal,
35505
- start,
35506
- this.#retention(options.sessionId, "browser")
35507
- );
35508
- this.#nativeBrowserSessions.set(options.sessionId, runtime);
35711
+ const runtime = await this.#processes.startRetained(options.signal, start, this.#retention(options.sessionId));
35712
+ this.#sessions.retain(options.sessionId, runtime);
35509
35713
  return runtime;
35510
35714
  }
35511
- async #startNativeBrowserPreparation(options) {
35512
- const operation = (async () => {
35513
- try {
35514
- return await this.#startRetainedNativeBrowserProcess(
35515
- 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;
35529
- }
35530
- 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(
35535
- 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;
35549
- }
35550
35715
  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;
35716
+ return await this.#sessions.prepare(options.sessionId, async () => {
35717
+ return await this.#startRetainedSessionProcess(
35718
+ options,
35719
+ async () => await AcpProcessSession.start(
35720
+ 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,
35721
+ () => this.#observer,
35722
+ this.#store,
35723
+ metrics
35724
+ )
35725
+ );
35726
+ });
35569
35727
  }
35570
35728
  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;
35729
+ return await this.#sessions.prepare(options.sessionId, async () => {
35730
+ await this.#retireSessionProcess(
35731
+ options.sessionId,
35732
+ existing,
35733
+ async () => await metrics.measure("runtime-replacement-close", async () => await existing.close())
35734
+ );
35735
+ this.#store.remove(options.sessionId);
35736
+ return await this.#startRetainedSessionProcess(
35737
+ options,
35738
+ async () => await AcpProcessSession.start(options, () => this.#observer, this.#store, metrics)
35739
+ );
35740
+ });
35590
35741
  }
35591
35742
  async #replaceExecutionPreparation(existing, options) {
35592
35743
  const desiredConfigOptions = existing.prepared().configOptions;
35593
- const operation = (async () => {
35744
+ return await this.#sessions.prepare(options.sessionId, async () => {
35745
+ await this.#retireSessionProcess(options.sessionId, existing, async () => await existing.close());
35746
+ const runtime = await this.#startRetainedSessionProcess(
35747
+ options,
35748
+ async () => await AcpProcessSession.start(options, () => this.#observer, this.#store)
35749
+ );
35594
35750
  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)) {
35751
+ for (const desired of desiredConfigOptions) {
35752
+ const current = runtime.prepared().configOptions.find((option) => option.id === desired.id);
35753
+ if (current === void 0 || current.type !== desired.type) {
35754
+ continue;
35755
+ }
35756
+ if (desired.type === "boolean") {
35757
+ if (current.booleanValue !== desired.booleanValue) {
35620
35758
  await runtime.configure({
35621
35759
  ...options,
35622
- booleanValue: false,
35760
+ booleanValue: desired.booleanValue,
35623
35761
  configId: desired.id,
35624
35762
  requestId: `execution-prepare-${desired.id}`,
35625
- value: desired.currentValue,
35626
- valueType: "select"
35763
+ value: "",
35764
+ valueType: "boolean"
35627
35765
  });
35628
35766
  }
35767
+ continue;
35768
+ }
35769
+ if (current.currentValue !== desired.currentValue && current.values.some((value) => value.value === desired.currentValue)) {
35770
+ await runtime.configure({
35771
+ ...options,
35772
+ booleanValue: false,
35773
+ configId: desired.id,
35774
+ requestId: `execution-prepare-${desired.id}`,
35775
+ value: desired.currentValue,
35776
+ valueType: "select"
35777
+ });
35629
35778
  }
35630
- } catch (error62) {
35631
- await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
35632
- throw error62;
35633
35779
  }
35634
- return runtime;
35635
- } finally {
35636
- this.#preparing.delete(options.sessionId);
35637
- this.#processes.notifyChanged();
35780
+ } catch (error62) {
35781
+ await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
35782
+ throw error62;
35638
35783
  }
35639
- })();
35640
- this.#preparing.set(options.sessionId, operation);
35641
- return await operation;
35784
+ return runtime;
35785
+ });
35642
35786
  }
35643
35787
  prepareSession = async (options, metrics = unmeasuredOperationMetrics) => {
35644
35788
  const stored = metrics.measureSync("session-state-read", () => this.#store.read(options.sessionId));
@@ -35652,11 +35796,11 @@ var AcpSessionManager = class {
35652
35796
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
35653
35797
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
35654
35798
  }
35655
- const pending = this.#preparing.get(options.sessionId);
35799
+ const pending = this.#sessions.pending(options.sessionId);
35656
35800
  if (pending !== void 0) {
35657
35801
  const runtime2 = await metrics.measure("session-preparation-wait", async () => await pending);
35658
35802
  if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35659
- this.#touchExpectedSession(options.sessionId, runtime2);
35803
+ this.#sessions.touch(options.sessionId, runtime2);
35660
35804
  return runtime2.prepared();
35661
35805
  }
35662
35806
  return await this.prepareSession(options, metrics);
@@ -35664,7 +35808,7 @@ var AcpSessionManager = class {
35664
35808
  const existing = this.#sessions.get(options.sessionId);
35665
35809
  if (existing !== void 0) {
35666
35810
  if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35667
- this.#touchExpectedSession(options.sessionId, existing);
35811
+ this.#sessions.touch(options.sessionId, existing);
35668
35812
  return existing.prepared();
35669
35813
  }
35670
35814
  if (!existing.canRebind()) {
@@ -35718,11 +35862,10 @@ var AcpSessionManager = class {
35718
35862
  * Use inspectAgentSettings for Agent-scoped settings discovery.
35719
35863
  */
35720
35864
  inspectSession = async (options, metrics = unmeasuredOperationMetrics) => {
35721
- if (this.#preparing.has(options.sessionId) || this.#sessions.has(options.sessionId)) {
35865
+ if (this.#sessions.pending(options.sessionId) !== void 0 || this.#sessions.get(options.sessionId) !== void 0) {
35722
35866
  throw new AcpExecutionError("The configuration inspection session is already in use", {}, "session_busy");
35723
35867
  }
35724
- this.#beginSessionUse(options.sessionId);
35725
- try {
35868
+ return await this.#sessions.use(options.sessionId, async () => {
35726
35869
  const runtime = await this.#startPreparation(options, metrics);
35727
35870
  try {
35728
35871
  return runtime.prepared();
@@ -35733,12 +35876,10 @@ var AcpSessionManager = class {
35733
35876
  this.#store.remove(options.sessionId);
35734
35877
  }
35735
35878
  }
35736
- } finally {
35737
- this.#endSessionUse(options.sessionId);
35738
- }
35879
+ });
35739
35880
  };
35740
35881
  prepareExecutionSession = async (options) => {
35741
- const pending = this.#preparing.get(options.sessionId);
35882
+ const pending = this.#sessions.pending(options.sessionId);
35742
35883
  if (pending !== void 0) {
35743
35884
  await pending;
35744
35885
  return await this.prepareExecutionSession(options);
@@ -35755,7 +35896,7 @@ var AcpSessionManager = class {
35755
35896
  );
35756
35897
  }
35757
35898
  if (existing.usesEnvironment(options.agent.environment)) {
35758
- this.#touchExpectedSession(options.sessionId, existing);
35899
+ this.#sessions.touch(options.sessionId, existing);
35759
35900
  return existing.prepared();
35760
35901
  }
35761
35902
  if (existing.hasRunningTurn()) {
@@ -35767,75 +35908,36 @@ var AcpSessionManager = class {
35767
35908
  }
35768
35909
  return (await this.#replaceExecutionPreparation(existing, options)).prepared();
35769
35910
  };
35770
- prepareNativeSessionBrowser = async (options) => {
35771
- const pending = this.#nativeBrowserPreparing.get(options.sessionId);
35772
- if (pending !== void 0) {
35773
- const runtime2 = await pending;
35774
- if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35775
- this.#touchExpectedNativeBrowserSession(options.sessionId, runtime2);
35776
- return runtime2.prepared();
35777
- }
35778
- return await this.prepareNativeSessionBrowser(options);
35779
- }
35780
- const existing = this.#nativeBrowserSessions.get(options.sessionId);
35781
- if (existing !== void 0) {
35782
- if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35783
- this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
35784
- return existing.prepared();
35785
- }
35786
- if (!existing.canRebind()) {
35787
- throw new AcpExecutionError(
35788
- "The agent or workspace cannot change after a conversation has started",
35789
- {},
35790
- "session_identity_mismatch"
35791
- );
35792
- }
35793
- const runtime2 = await this.#replaceNativeBrowserPreparation(existing, options);
35794
- return runtime2.prepared();
35795
- }
35796
- const runtime = await this.#startNativeBrowserPreparation(options);
35797
- return runtime.prepared();
35798
- };
35911
+ prepareNativeSessionBrowser = (options) => this.#browsers.prepare(options);
35799
35912
  configureSession = async (options) => {
35800
- this.#beginSessionUse(options.sessionId);
35801
- try {
35913
+ return await this.#sessions.use(options.sessionId, async () => {
35802
35914
  const runtime = await this.ensureSession(options);
35803
35915
  return await runtime.configure(options);
35804
- } finally {
35805
- this.#endSessionUse(options.sessionId);
35806
- }
35916
+ });
35807
35917
  };
35808
35918
  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 () => {
35919
+ await this.#executions.cancel(sessionId, async () => {
35815
35920
  const runtime = this.#sessions.get(sessionId);
35816
35921
  if (runtime !== void 0) {
35817
35922
  try {
35818
35923
  await runtime.cancel();
35819
35924
  } finally {
35820
- if (this.#sessions.get(sessionId) === runtime) {
35821
- this.#sessions.delete(sessionId);
35925
+ if (this.#sessions.detach(sessionId, runtime)) {
35822
35926
  this.#processes.releaseClosed(runtime);
35823
35927
  }
35824
35928
  }
35825
35929
  }
35826
- })();
35827
- this.#cancellationRequests.set(sessionId, cancellation);
35828
- await cancellation;
35930
+ });
35829
35931
  };
35830
35932
  steerPrompt = async (options) => {
35831
- if (this.#activeCommandIds.get(options.sessionId) !== options.targetCommandId) {
35933
+ if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
35832
35934
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
35833
35935
  }
35834
- const pending = this.#preparing.get(options.sessionId);
35936
+ const pending = this.#sessions.pending(options.sessionId);
35835
35937
  if (pending !== void 0) {
35836
35938
  await pending;
35837
35939
  }
35838
- if (this.#activeCommandIds.get(options.sessionId) !== options.targetCommandId) {
35940
+ if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
35839
35941
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
35840
35942
  }
35841
35943
  const runtime = this.#sessions.get(options.sessionId);
@@ -35873,27 +35975,24 @@ var AcpSessionManager = class {
35873
35975
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
35874
35976
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
35875
35977
  }
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);
35978
+ this.#executions.begin(options.sessionId, options.commandId);
35880
35979
  try {
35881
- if (this.#cancellationRequests.has(options.sessionId)) {
35980
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
35882
35981
  return { stopReason: "cancelled" };
35883
35982
  }
35884
35983
  const runtime = await this.ensureSession(options);
35885
35984
  if (this.#nativeOperations.has(`${options.agent.id}:${runtime.nativeSessionId()}`)) {
35886
35985
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
35887
35986
  }
35888
- if (this.#cancellationRequests.has(options.sessionId)) {
35987
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
35889
35988
  return { stopReason: "cancelled" };
35890
35989
  }
35891
35990
  try {
35892
35991
  const result = await runtime.prompt(options);
35893
- return this.#cancellationRequests.has(options.sessionId) ? { stopReason: "cancelled" } : result;
35992
+ return this.#executions.cancellation(options.sessionId) !== void 0 ? { stopReason: "cancelled" } : result;
35894
35993
  } catch (error62) {
35895
- if (this.#cancellationRequests.has(options.sessionId)) {
35896
- await this.#cancellationRequests.get(options.sessionId);
35994
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
35995
+ await this.#executions.cancellation(options.sessionId);
35897
35996
  return { stopReason: "cancelled" };
35898
35997
  }
35899
35998
  throw error62;
@@ -35907,7 +36006,7 @@ var AcpSessionManager = class {
35907
36006
  async () => await failedRuntime.close()
35908
36007
  );
35909
36008
  }
35910
- if (this.#cancellationRequests.has(options.sessionId)) {
36009
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
35911
36010
  return { stopReason: "cancelled" };
35912
36011
  }
35913
36012
  if (error62 instanceof AcpExecutionError) {
@@ -35918,13 +36017,10 @@ var AcpSessionManager = class {
35918
36017
  });
35919
36018
  } finally {
35920
36019
  try {
35921
- await this.#cancellationRequests.get(options.sessionId);
36020
+ await this.#executions.cancellation(options.sessionId);
35922
36021
  } 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);
36022
+ this.#executions.finish(options.sessionId, options.commandId);
36023
+ this.#sessions.touch(options.sessionId);
35928
36024
  this.#processes.notifyChanged();
35929
36025
  }
35930
36026
  }
@@ -35945,14 +36041,11 @@ var AcpSessionManager = class {
35945
36041
  };
35946
36042
  close = async () => {
35947
36043
  const sessions = [...this.#sessions.entries()];
35948
- const nativeBrowserSessions = [...this.#nativeBrowserSessions.entries()];
35949
36044
  await Promise.all([
35950
36045
  ...sessions.map(
35951
36046
  async ([sessionId, runtime]) => await this.#retireSessionProcess(sessionId, runtime, async () => await runtime.close())
35952
36047
  ),
35953
- ...nativeBrowserSessions.map(
35954
- async ([sessionId, runtime]) => await this.#retireNativeBrowserProcess(sessionId, runtime, async () => await runtime.close())
35955
- )
36048
+ this.#browsers.close()
35956
36049
  ]);
35957
36050
  };
35958
36051
  findSessions = (sessionIds) => sessionIds.flatMap((sessionId) => {
@@ -35964,38 +36057,38 @@ var AcpSessionManager = class {
35964
36057
  createNativeSession = async (options) => {
35965
36058
  const temporarySessionId = `new-${crypto.randomUUID()}`;
35966
36059
  const temporaryOptions = { ...options, sessionId: temporarySessionId };
35967
- this.#beginSessionUse(temporarySessionId);
35968
- try {
35969
- const runtime = await this.#startPreparation(temporaryOptions);
35970
- let adopted = false;
36060
+ return await this.#sessions.use(temporarySessionId, async () => {
35971
36061
  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
- );
36062
+ const runtime = await this.#startPreparation(temporaryOptions);
36063
+ let adopted = false;
36064
+ try {
36065
+ const nativeSessionId = runtime.nativeSessionId();
36066
+ if (!isNativeChatSessionId(options.agent.id, nativeSessionId) || this.#sessions.get(nativeSessionId) !== void 0 || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
36067
+ throw new AcpExecutionError(
36068
+ "The Agent returned an invalid new Session ID",
36069
+ {},
36070
+ "invalid_native_session_response"
36071
+ );
36072
+ }
36073
+ runtime.adoptNativeSessionId(nativeSessionId);
36074
+ this.#sessions.detach(temporarySessionId, runtime);
36075
+ this.#sessions.retain(nativeSessionId, runtime);
36076
+ this.#processes.retain(runtime, this.#retention(nativeSessionId));
36077
+ adopted = true;
36078
+ return nativeSessionId;
36079
+ } finally {
36080
+ if (!adopted) {
36081
+ await this.#retireSessionProcess(
36082
+ temporarySessionId,
36083
+ runtime,
36084
+ async () => await runtime.closeProcess()
36085
+ );
36086
+ }
35979
36087
  }
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
36088
  } finally {
35987
- if (!adopted) {
35988
- await this.#retireSessionProcess(
35989
- temporarySessionId,
35990
- runtime,
35991
- async () => await runtime.closeProcess()
35992
- );
35993
- }
36089
+ this.#store.remove(temporarySessionId);
35994
36090
  }
35995
- } finally {
35996
- this.#store.remove(temporarySessionId);
35997
- this.#endSessionUse(temporarySessionId);
35998
- }
36091
+ });
35999
36092
  };
36000
36093
  ensureSession = async (options) => {
36001
36094
  const existing = this.#sessions.get(options.sessionId);
@@ -36007,7 +36100,7 @@ var AcpSessionManager = class {
36007
36100
  "session_identity_mismatch"
36008
36101
  );
36009
36102
  }
36010
- this.#touchExpectedSession(options.sessionId, existing);
36103
+ this.#sessions.touch(options.sessionId, existing);
36011
36104
  return existing;
36012
36105
  }
36013
36106
  await this.prepareSession(options);
@@ -36017,38 +36110,18 @@ var AcpSessionManager = class {
36017
36110
  }
36018
36111
  return started;
36019
36112
  };
36020
- async #ensureNativeSessionBrowser(options) {
36021
- const existing = this.#nativeBrowserSessions.get(options.sessionId);
36022
- if (existing !== void 0) {
36023
- if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
36024
- throw new AcpExecutionError(
36025
- "The agent or workspace cannot change after a conversation has started",
36026
- {},
36027
- "session_identity_mismatch"
36028
- );
36029
- }
36030
- this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
36031
- return existing;
36032
- }
36033
- await this.prepareNativeSessionBrowser(options);
36034
- const started = this.#nativeBrowserSessions.get(options.sessionId);
36035
- if (started === void 0) {
36036
- throw new AcpExecutionError("ACP native session browser did not start");
36037
- }
36038
- return started;
36039
- }
36040
36113
  listNativeSessions = async (options, cursor) => {
36041
- this.#beginNativeBrowserUse(options.sessionId);
36042
- try {
36043
- return await (await this.#ensureNativeSessionBrowser(options)).listNativeSessions(cursor, options.signal);
36044
- } finally {
36045
- this.#endNativeBrowserUse(options.sessionId);
36046
- }
36114
+ return await this.#browsers.use(
36115
+ options,
36116
+ async (runtime) => await runtime.listNativeSessions(cursor, options.signal)
36117
+ );
36047
36118
  };
36048
36119
  previewNativeSession = async (options, nativeSessionId) => {
36049
36120
  const key = `${options.agent.id}:${nativeSessionId}`;
36050
36121
  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))) {
36122
+ if (this.#nativeOperations.has(key) || linked.some(
36123
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
36124
+ )) {
36052
36125
  throw new AcpExecutionError(
36053
36126
  "Wait for the native session's running operation to finish",
36054
36127
  {},
@@ -36057,26 +36130,13 @@ var AcpSessionManager = class {
36057
36130
  }
36058
36131
  this.#nativeOperations.add(key);
36059
36132
  try {
36060
- return await this.#processes.runTransient(
36061
- options.signal,
36062
- async () => await AcpProcessSession.startNativeSessionBrowser(
36063
- options,
36064
- () => this.#nativeBrowserObserver,
36065
- new MemoryAcpSessionStore()
36066
- ),
36067
- async (runtime) => {
36068
- runtime.selectNativeSession(nativeSessionId);
36069
- return await runtime.loadHistory(options.signal);
36070
- },
36071
- async (runtime) => await runtime.closeProcess()
36072
- );
36133
+ return await this.#browsers.preview(options, nativeSessionId);
36073
36134
  } finally {
36074
36135
  this.#nativeOperations.delete(key);
36075
36136
  }
36076
36137
  };
36077
36138
  loadSessionHistory = async (options) => {
36078
- this.#beginSessionUse(options.sessionId);
36079
- try {
36139
+ return await this.#sessions.use(options.sessionId, async () => {
36080
36140
  const runtime = await this.ensureSession(options);
36081
36141
  const stored = this.#store.read(options.sessionId);
36082
36142
  if (stored instanceof FoundAcpStoredSession && this.#nativeHistoryReader !== null) {
@@ -36090,9 +36150,7 @@ var AcpSessionManager = class {
36090
36150
  }
36091
36151
  }
36092
36152
  return await runtime.loadHistory(options.signal);
36093
- } finally {
36094
- this.#endSessionUse(options.sessionId);
36095
- }
36153
+ });
36096
36154
  };
36097
36155
  openNativeSession = async (options, origin) => {
36098
36156
  const key = `${options.agent.id}:${origin.nativeSessionId}`;
@@ -36100,7 +36158,9 @@ var AcpSessionManager = class {
36100
36158
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
36101
36159
  }
36102
36160
  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))) {
36161
+ if (linked.some(
36162
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
36163
+ )) {
36104
36164
  throw new AcpExecutionError(
36105
36165
  "Wait for the native session's running operation to finish",
36106
36166
  {},
@@ -36109,13 +36169,12 @@ var AcpSessionManager = class {
36109
36169
  }
36110
36170
  const existing = linked[0];
36111
36171
  if (origin.kind === "import" && existing !== void 0) {
36112
- this.#beginSessionUse(existing.sessionId);
36113
- try {
36172
+ return await this.#sessions.use(existing.sessionId, async () => {
36114
36173
  const runtime = await this.ensureSession({
36115
36174
  ...options,
36116
36175
  sessionId: existing.sessionId
36117
36176
  });
36118
- if (this.#activeCommandIds.has(existing.sessionId) || this.#nativeOperations.has(key)) {
36177
+ if (this.#executions.command(existing.sessionId) !== void 0 || this.#nativeOperations.has(key)) {
36119
36178
  throw new AcpExecutionError(
36120
36179
  "Wait for the native session's running operation to finish",
36121
36180
  {},
@@ -36132,48 +36191,48 @@ var AcpSessionManager = class {
36132
36191
  this.#nativeOperations.delete(key);
36133
36192
  this.#processes.notifyChanged();
36134
36193
  }
36135
- } finally {
36136
- this.#endSessionUse(existing.sessionId);
36137
- }
36194
+ });
36138
36195
  }
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) {
36196
+ 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
36197
  throw new AcpExecutionError("The destination conversation already exists", {}, "session_identity_mismatch");
36141
36198
  }
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);
36199
+ return await this.#sessions.use(options.sessionId, async () => {
36200
+ this.#nativeOperations.add(key);
36148
36201
  try {
36149
- return {
36150
- sessionId: options.sessionId,
36151
- prepared: runtime.prepared()
36152
- };
36202
+ const runtime = await this.#startPreparation({ ...options, origin });
36203
+ const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
36204
+ this.#nativeOperations.add(openedKey);
36205
+ try {
36206
+ return {
36207
+ sessionId: options.sessionId,
36208
+ prepared: runtime.prepared()
36209
+ };
36210
+ } finally {
36211
+ this.#nativeOperations.delete(openedKey);
36212
+ this.#processes.notifyChanged();
36213
+ }
36214
+ } catch (error62) {
36215
+ this.#store.remove(options.sessionId);
36216
+ const runtime = this.#sessions.get(options.sessionId);
36217
+ if (runtime !== void 0) {
36218
+ await this.#retireSessionProcess(
36219
+ options.sessionId,
36220
+ runtime,
36221
+ async () => await runtime.closeProcess()
36222
+ );
36223
+ }
36224
+ throw error62;
36153
36225
  } 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());
36226
+ this.#nativeOperations.delete(key);
36162
36227
  }
36163
- throw error62;
36164
- } finally {
36165
- this.#nativeOperations.delete(key);
36166
- this.#endSessionUse(options.sessionId);
36167
- }
36228
+ });
36168
36229
  };
36169
36230
  deleteNativeSession = async (options, nativeSessionId) => {
36170
- this.#beginNativeBrowserUse(options.sessionId);
36171
- try {
36172
- const runtime = await this.#ensureNativeSessionBrowser(options);
36231
+ return await this.#browsers.use(options, async (runtime) => {
36173
36232
  const key = `${options.agent.id}:${nativeSessionId}`;
36174
36233
  const linked = this.#store.findNative(options.agent.id, nativeSessionId);
36175
36234
  if (this.#nativeOperations.has(key) || linked.some(
36176
- (entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId)
36235
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
36177
36236
  )) {
36178
36237
  throw new AcpExecutionError(
36179
36238
  "Wait for the native session's running operation to finish",
@@ -36193,20 +36252,14 @@ var AcpSessionManager = class {
36193
36252
  await runtime.deleteNativeSession(nativeSessionId, options.signal);
36194
36253
  const deleted = this.#store.markDeleted(options.agent.id, nativeSessionId);
36195
36254
  if (runtime.nativeSessionId() === nativeSessionId) {
36196
- await this.#retireNativeBrowserProcess(
36197
- options.sessionId,
36198
- runtime,
36199
- async () => await runtime.close()
36200
- );
36255
+ await this.#browsers.retire(options.sessionId, runtime);
36201
36256
  }
36202
36257
  return deleted;
36203
36258
  } finally {
36204
36259
  this.#nativeOperations.delete(key);
36205
36260
  this.#processes.notifyChanged();
36206
36261
  }
36207
- } finally {
36208
- this.#endNativeBrowserUse(options.sessionId);
36209
- }
36262
+ });
36210
36263
  };
36211
36264
  };
36212
36265