@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/index.js CHANGED
@@ -22373,6 +22373,30 @@ function loadDaemonConfig() {
22373
22373
  );
22374
22374
  }
22375
22375
 
22376
+ // src/acp/acp-error.ts
22377
+ var AcpExecutionError = class extends Error {
22378
+ code;
22379
+ constructor(message, options = {}, code = "acp_execution_failed") {
22380
+ super(message, options);
22381
+ this.code = code;
22382
+ this.name = "AcpExecutionError";
22383
+ }
22384
+ };
22385
+ var AcpConfigurationValueUnavailableError = class extends AcpExecutionError {
22386
+ configId;
22387
+ offeredValueCount;
22388
+ offeredValues;
22389
+ requestedValue;
22390
+ constructor(configId, requestedValue, offeredValues) {
22391
+ super("The requested ACP configuration value is unavailable", {}, "invalid_session_configuration");
22392
+ this.name = "AcpConfigurationValueUnavailableError";
22393
+ this.configId = configId.slice(0, 128);
22394
+ this.requestedValue = requestedValue.slice(0, 128);
22395
+ this.offeredValueCount = offeredValues.length;
22396
+ this.offeredValues = Object.freeze(offeredValues.slice(0, 32).map((value) => value.slice(0, 128)));
22397
+ }
22398
+ };
22399
+
22376
22400
  // src/acp/process-session.ts
22377
22401
  import {
22378
22402
  methods as methods3,
@@ -22676,30 +22700,6 @@ async function replaceImageMarkers(text2, replace) {
22676
22700
  return `${rewritten}${text2.slice(cursor)}`;
22677
22701
  }
22678
22702
 
22679
- // src/acp/acp-error.ts
22680
- var AcpExecutionError = class extends Error {
22681
- code;
22682
- constructor(message, options = {}, code = "acp_execution_failed") {
22683
- super(message, options);
22684
- this.code = code;
22685
- this.name = "AcpExecutionError";
22686
- }
22687
- };
22688
- var AcpConfigurationValueUnavailableError = class extends AcpExecutionError {
22689
- configId;
22690
- offeredValueCount;
22691
- offeredValues;
22692
- requestedValue;
22693
- constructor(configId, requestedValue, offeredValues) {
22694
- super("The requested ACP configuration value is unavailable", {}, "invalid_session_configuration");
22695
- this.name = "AcpConfigurationValueUnavailableError";
22696
- this.configId = configId.slice(0, 128);
22697
- this.requestedValue = requestedValue.slice(0, 128);
22698
- this.offeredValueCount = offeredValues.length;
22699
- this.offeredValues = Object.freeze(offeredValues.slice(0, 32).map((value) => value.slice(0, 128)));
22700
- }
22701
- };
22702
-
22703
22703
  // src/acp/activity.ts
22704
22704
  var TRUNCATION_MARKER = "\n\u2026 output truncated by Mobius";
22705
22705
  var AcpActivityNarration = class {
@@ -23323,8 +23323,8 @@ function acpEndpointTerminationError(termination) {
23323
23323
  case "closed":
23324
23324
  return new AcpExecutionError("The ACP endpoint is closed", {}, "acp_endpoint_closed");
23325
23325
  default: {
23326
- const unreachable = termination;
23327
- return unreachable;
23326
+ const unreachable2 = termination;
23327
+ return unreachable2;
23328
23328
  }
23329
23329
  }
23330
23330
  }
@@ -31158,6 +31158,301 @@ var AcpProcessSession = class _AcpProcessSession {
31158
31158
  };
31159
31159
  };
31160
31160
 
31161
+ // src/acp/retained-sessions.ts
31162
+ var RetainedSessions = class {
31163
+ #entries = /* @__PURE__ */ new Map();
31164
+ #events;
31165
+ constructor(events) {
31166
+ this.#events = events;
31167
+ }
31168
+ #entry = (id) => this.#entries.get(id) ?? { lifecycle: { kind: "empty" }, users: 0 };
31169
+ #write = (id, entry) => {
31170
+ if (entry.lifecycle.kind === "empty" && entry.users === 0) {
31171
+ this.#entries.delete(id);
31172
+ } else {
31173
+ this.#entries.set(id, entry);
31174
+ }
31175
+ };
31176
+ #resource = (entry) => entry.lifecycle.kind === "preparing" ? entry.lifecycle.resource : entry.lifecycle;
31177
+ get = (id) => {
31178
+ const resource = this.#resource(this.#entry(id));
31179
+ return resource.kind === "ready" ? resource.runtime : void 0;
31180
+ };
31181
+ pending = (id) => {
31182
+ const state = this.#entry(id).lifecycle;
31183
+ return state.kind === "preparing" ? state.completion : void 0;
31184
+ };
31185
+ isBusy = (id) => {
31186
+ const entry = this.#entry(id);
31187
+ return entry.users > 0 || entry.lifecycle.kind === "preparing";
31188
+ };
31189
+ touch = (id, expected = this.get(id)) => {
31190
+ if (expected !== void 0 && this.get(id) === expected) {
31191
+ this.#events.touch(expected);
31192
+ }
31193
+ };
31194
+ #setResource = (id, resource) => {
31195
+ const entry = this.#entry(id);
31196
+ this.#write(id, {
31197
+ ...entry,
31198
+ lifecycle: entry.lifecycle.kind === "preparing" ? { ...entry.lifecycle, resource } : resource
31199
+ });
31200
+ };
31201
+ retain = (id, runtime) => {
31202
+ this.#setResource(id, { kind: "ready", runtime });
31203
+ };
31204
+ detach = (id, expected) => {
31205
+ if (this.get(id) !== expected) {
31206
+ return false;
31207
+ }
31208
+ this.#setResource(id, { kind: "empty" });
31209
+ return true;
31210
+ };
31211
+ entries = () => {
31212
+ const result = [];
31213
+ for (const id of this.#entries.keys()) {
31214
+ const runtime = this.get(id);
31215
+ if (runtime !== void 0) {
31216
+ result.push([id, runtime]);
31217
+ }
31218
+ }
31219
+ return result;
31220
+ };
31221
+ prepare = (id, start) => {
31222
+ const pending = this.pending(id);
31223
+ if (pending !== void 0) {
31224
+ return pending;
31225
+ }
31226
+ const completion = Promise.withResolvers();
31227
+ const entry = this.#entry(id);
31228
+ this.#write(id, {
31229
+ ...entry,
31230
+ lifecycle: {
31231
+ kind: "preparing",
31232
+ completion: completion.promise,
31233
+ resource: this.#resource(entry)
31234
+ }
31235
+ });
31236
+ const run = async () => {
31237
+ try {
31238
+ return await start();
31239
+ } finally {
31240
+ const current = this.#entry(id);
31241
+ if (current.lifecycle.kind === "preparing" && current.lifecycle.completion === completion.promise) {
31242
+ this.#write(id, { ...current, lifecycle: current.lifecycle.resource });
31243
+ }
31244
+ this.#events.changed();
31245
+ }
31246
+ };
31247
+ void run().then(completion.resolve, completion.reject);
31248
+ return completion.promise;
31249
+ };
31250
+ use = async (id, operation) => {
31251
+ const entry = this.#entry(id);
31252
+ this.#write(id, { ...entry, users: entry.users + 1 });
31253
+ this.touch(id);
31254
+ try {
31255
+ return await operation();
31256
+ } finally {
31257
+ const current = this.#entry(id);
31258
+ this.#write(id, { ...current, users: current.users - 1 });
31259
+ this.touch(id);
31260
+ this.#events.changed();
31261
+ }
31262
+ };
31263
+ };
31264
+
31265
+ // src/acp/native-session-browsers.ts
31266
+ var NativeSessionBrowsers = class {
31267
+ #sessions;
31268
+ #processes;
31269
+ #observer = new NullAcpSessionObserver();
31270
+ #store = new MemoryAcpSessionStore();
31271
+ constructor(processes) {
31272
+ this.#processes = processes;
31273
+ this.#sessions = new RetainedSessions({ touch: processes.touch, changed: processes.notifyChanged });
31274
+ }
31275
+ retire = async (sessionId, runtime) => {
31276
+ const retained = this.#sessions.detach(sessionId, runtime);
31277
+ if (retained) {
31278
+ this.#store.remove(sessionId);
31279
+ }
31280
+ await this.#processes.retire(runtime, () => runtime.close());
31281
+ };
31282
+ async #startRetained(options) {
31283
+ const runtime = await this.#processes.startRetained(
31284
+ options.signal,
31285
+ () => AcpProcessSession.startNativeSessionBrowser(options, () => this.#observer, this.#store),
31286
+ {
31287
+ isBusy: () => this.#sessions.isBusy(options.sessionId),
31288
+ retire: (runtime2) => this.retire(options.sessionId, runtime2)
31289
+ }
31290
+ );
31291
+ this.#sessions.retain(options.sessionId, runtime);
31292
+ return runtime;
31293
+ }
31294
+ async #startPreparation(options) {
31295
+ return await this.#sessions.prepare(options.sessionId, async () => {
31296
+ return await this.#startRetained(options);
31297
+ });
31298
+ }
31299
+ async #replacePreparation(existing, options) {
31300
+ return await this.#sessions.prepare(options.sessionId, async () => {
31301
+ await this.retire(options.sessionId, existing);
31302
+ return await this.#startRetained(options);
31303
+ });
31304
+ }
31305
+ prepare = async (options) => {
31306
+ const pending = this.#sessions.pending(options.sessionId);
31307
+ if (pending !== void 0) {
31308
+ const runtime2 = await pending;
31309
+ if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31310
+ this.#sessions.touch(options.sessionId, runtime2);
31311
+ return runtime2.prepared();
31312
+ }
31313
+ return await this.prepare(options);
31314
+ }
31315
+ const existing = this.#sessions.get(options.sessionId);
31316
+ if (existing !== void 0) {
31317
+ if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31318
+ this.#sessions.touch(options.sessionId, existing);
31319
+ return existing.prepared();
31320
+ }
31321
+ if (!existing.canRebind()) {
31322
+ throw new AcpExecutionError(
31323
+ "The agent or workspace cannot change after a conversation has started",
31324
+ {},
31325
+ "session_identity_mismatch"
31326
+ );
31327
+ }
31328
+ const runtime2 = await this.#replacePreparation(existing, options);
31329
+ return runtime2.prepared();
31330
+ }
31331
+ const runtime = await this.#startPreparation(options);
31332
+ return runtime.prepared();
31333
+ };
31334
+ async #ensure(options) {
31335
+ const existing = this.#sessions.get(options.sessionId);
31336
+ if (existing !== void 0) {
31337
+ if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31338
+ throw new AcpExecutionError(
31339
+ "The agent or workspace cannot change after a conversation has started",
31340
+ {},
31341
+ "session_identity_mismatch"
31342
+ );
31343
+ }
31344
+ this.#sessions.touch(options.sessionId, existing);
31345
+ return existing;
31346
+ }
31347
+ await this.prepare(options);
31348
+ const started = this.#sessions.get(options.sessionId);
31349
+ if (started === void 0) {
31350
+ throw new AcpExecutionError("ACP native session browser did not start");
31351
+ }
31352
+ return started;
31353
+ }
31354
+ use = async (options, operation) => await this.#sessions.use(options.sessionId, async () => await operation(await this.#ensure(options)));
31355
+ preview = async (options, nativeSessionId) => {
31356
+ return await this.#processes.runTransient(
31357
+ options.signal,
31358
+ async () => await AcpProcessSession.startNativeSessionBrowser(
31359
+ options,
31360
+ () => this.#observer,
31361
+ new MemoryAcpSessionStore()
31362
+ ),
31363
+ async (runtime) => {
31364
+ runtime.selectNativeSession(nativeSessionId);
31365
+ return await runtime.loadHistory(options.signal);
31366
+ },
31367
+ async (runtime) => await runtime.closeProcess()
31368
+ );
31369
+ };
31370
+ close = async () => {
31371
+ await Promise.all(this.#sessions.entries().map(async ([id, runtime]) => await this.retire(id, runtime)));
31372
+ };
31373
+ };
31374
+
31375
+ // src/acp/session-executions.ts
31376
+ var unreachable = (state) => {
31377
+ throw new AcpExecutionError("Unexpected execution state", { cause: state });
31378
+ };
31379
+ var SessionExecutions = class {
31380
+ #entries = /* @__PURE__ */ new Map();
31381
+ command = (id) => {
31382
+ const state = this.#entries.get(id);
31383
+ switch (state?.kind) {
31384
+ case "running":
31385
+ case "cancelling":
31386
+ return state.commandId;
31387
+ case "cancelled-before-start":
31388
+ case void 0:
31389
+ return void 0;
31390
+ default:
31391
+ return unreachable(state);
31392
+ }
31393
+ };
31394
+ cancellation = (id) => {
31395
+ const state = this.#entries.get(id);
31396
+ switch (state?.kind) {
31397
+ case "cancelling":
31398
+ case "cancelled-before-start":
31399
+ return state.completion;
31400
+ case "running":
31401
+ case void 0:
31402
+ return void 0;
31403
+ default:
31404
+ return unreachable(state);
31405
+ }
31406
+ };
31407
+ isBusy = (id) => this.#entries.has(id);
31408
+ begin = (id, commandId) => {
31409
+ const state = this.#entries.get(id);
31410
+ switch (state?.kind) {
31411
+ case "running":
31412
+ case "cancelling":
31413
+ throw new AcpExecutionError(
31414
+ "Another prompt is already running in this conversation",
31415
+ {},
31416
+ "session_busy"
31417
+ );
31418
+ case "cancelled-before-start":
31419
+ case void 0:
31420
+ break;
31421
+ default:
31422
+ unreachable(state);
31423
+ }
31424
+ this.#entries.set(
31425
+ id,
31426
+ state?.kind === "cancelled-before-start" ? { kind: "cancelling", commandId, completion: state.completion } : { kind: "running", commandId }
31427
+ );
31428
+ };
31429
+ cancel = (id, stop) => {
31430
+ const state = this.#entries.get(id);
31431
+ switch (state?.kind) {
31432
+ case "cancelling":
31433
+ case "cancelled-before-start":
31434
+ return state.completion;
31435
+ case "running":
31436
+ case void 0:
31437
+ break;
31438
+ default:
31439
+ return unreachable(state);
31440
+ }
31441
+ const completion = Promise.withResolvers();
31442
+ this.#entries.set(
31443
+ id,
31444
+ state?.kind === "running" ? { kind: "cancelling", commandId: state.commandId, completion: completion.promise } : { kind: "cancelled-before-start", completion: completion.promise }
31445
+ );
31446
+ void (async () => await stop())().then(completion.resolve, completion.reject);
31447
+ return completion.promise;
31448
+ };
31449
+ finish = (id, commandId) => {
31450
+ if (this.command(id) === commandId) {
31451
+ this.#entries.delete(id);
31452
+ }
31453
+ };
31454
+ };
31455
+
31161
31456
  // src/native-sessions/chat-agent.ts
31162
31457
  function supportsNativeChat(agentId) {
31163
31458
  return agentId === "codex" || agentId === "claude" || agentId === "opencode" || agentId === "pi";
@@ -31369,18 +31664,11 @@ var AcpProcessPool = class {
31369
31664
  var SESSION_CONTROL_TIMEOUT_MILLISECONDS = 3e4;
31370
31665
  var AcpSessionManager = class {
31371
31666
  #processes = new AcpProcessPool();
31372
- #activeCommandIds = /* @__PURE__ */ new Map();
31667
+ #sessions = new RetainedSessions({ touch: this.#processes.touch, changed: this.#processes.notifyChanged });
31668
+ #browsers = new NativeSessionBrowsers(this.#processes);
31669
+ #executions = new SessionExecutions();
31373
31670
  #agentSettingsObserver = new NullAcpSessionObserver();
31374
31671
  #nativeOperations = /* @__PURE__ */ new Set();
31375
- #nativeBrowserObserver = new NullAcpSessionObserver();
31376
- #nativeBrowserPreparing = /* @__PURE__ */ new Map();
31377
- #nativeBrowserSessions = /* @__PURE__ */ new Map();
31378
- #nativeBrowserStore = new MemoryAcpSessionStore();
31379
- #nativeBrowserUseCounts = /* @__PURE__ */ new Map();
31380
- #cancellationRequests = /* @__PURE__ */ new Map();
31381
- #preparing = /* @__PURE__ */ new Map();
31382
- #sessionUseCounts = /* @__PURE__ */ new Map();
31383
- #sessions = /* @__PURE__ */ new Map();
31384
31672
  #store;
31385
31673
  #nativeHistoryReader;
31386
31674
  #sessionIdentity;
@@ -31394,81 +31682,18 @@ var AcpSessionManager = class {
31394
31682
  setObserver = (observer) => {
31395
31683
  this.#observer = observer;
31396
31684
  };
31397
- #touchSession(sessionId) {
31398
- const runtime = this.#sessions.get(sessionId);
31399
- if (runtime === void 0) {
31400
- return;
31401
- }
31402
- this.#processes.touch(runtime);
31403
- }
31404
- #touchExpectedSession(sessionId, expected) {
31405
- if (this.#sessions.get(sessionId) !== expected) {
31406
- return;
31407
- }
31408
- this.#processes.touch(expected);
31409
- }
31410
- #touchNativeBrowserSession(sessionId) {
31411
- const runtime = this.#nativeBrowserSessions.get(sessionId);
31412
- if (runtime === void 0) {
31413
- return;
31414
- }
31415
- this.#processes.touch(runtime);
31416
- }
31417
- #touchExpectedNativeBrowserSession(sessionId, expected) {
31418
- if (this.#nativeBrowserSessions.get(sessionId) !== expected) {
31419
- return;
31420
- }
31421
- this.#processes.touch(expected);
31422
- }
31423
- #beginSessionUse(sessionId) {
31424
- this.#sessionUseCounts.set(sessionId, (this.#sessionUseCounts.get(sessionId) ?? 0) + 1);
31425
- this.#touchSession(sessionId);
31426
- }
31427
- #endSessionUse(sessionId) {
31428
- const count = this.#sessionUseCounts.get(sessionId) ?? 0;
31429
- if (count <= 1) {
31430
- this.#sessionUseCounts.delete(sessionId);
31431
- } else {
31432
- this.#sessionUseCounts.set(sessionId, count - 1);
31433
- }
31434
- this.#touchSession(sessionId);
31435
- this.#processes.notifyChanged();
31436
- }
31437
- #beginNativeBrowserUse(sessionId) {
31438
- this.#nativeBrowserUseCounts.set(sessionId, (this.#nativeBrowserUseCounts.get(sessionId) ?? 0) + 1);
31439
- this.#touchNativeBrowserSession(sessionId);
31440
- }
31441
- #endNativeBrowserUse(sessionId) {
31442
- const count = this.#nativeBrowserUseCounts.get(sessionId) ?? 0;
31443
- if (count <= 1) {
31444
- this.#nativeBrowserUseCounts.delete(sessionId);
31445
- } else {
31446
- this.#nativeBrowserUseCounts.set(sessionId, count - 1);
31447
- }
31448
- this.#touchNativeBrowserSession(sessionId);
31449
- this.#processes.notifyChanged();
31450
- }
31451
31685
  #isNativeSessionOperationActive(sessionId) {
31452
31686
  const stored = this.#store.read(sessionId);
31453
31687
  return stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`);
31454
31688
  }
31455
- #retention(sessionId, kind) {
31456
- if (kind === "execution") {
31457
- return {
31458
- isBusy: () => this.#activeCommandIds.has(sessionId) || this.#cancellationRequests.has(sessionId) || this.#preparing.has(sessionId) || (this.#sessionUseCounts.get(sessionId) ?? 0) > 0 || this.#isNativeSessionOperationActive(sessionId),
31459
- retire: (runtime) => this.#retireSessionProcess(sessionId, runtime, () => runtime.close())
31460
- };
31461
- }
31689
+ #retention(sessionId) {
31462
31690
  return {
31463
- isBusy: () => this.#nativeBrowserPreparing.has(sessionId) || (this.#nativeBrowserUseCounts.get(sessionId) ?? 0) > 0,
31464
- retire: (runtime) => this.#retireNativeBrowserProcess(sessionId, runtime, () => runtime.close())
31691
+ isBusy: () => this.#executions.isBusy(sessionId) || this.#sessions.isBusy(sessionId) || this.#isNativeSessionOperationActive(sessionId),
31692
+ retire: (runtime) => this.#retireSessionProcess(sessionId, runtime, () => runtime.close())
31465
31693
  };
31466
31694
  }
31467
31695
  async #retireSessionProcess(sessionId, runtime, close) {
31468
- const retained = this.#sessions.get(sessionId) === runtime;
31469
- if (retained) {
31470
- this.#sessions.delete(sessionId);
31471
- }
31696
+ const retained = this.#sessions.detach(sessionId, runtime);
31472
31697
  try {
31473
31698
  await this.#processes.retire(runtime, close);
31474
31699
  } finally {
@@ -31480,163 +31705,82 @@ var AcpSessionManager = class {
31480
31705
  }
31481
31706
  }
31482
31707
  }
31483
- async #retireNativeBrowserProcess(sessionId, runtime, close) {
31484
- const retained = this.#nativeBrowserSessions.get(sessionId) === runtime;
31485
- if (retained) {
31486
- this.#nativeBrowserSessions.delete(sessionId);
31487
- this.#nativeBrowserStore.remove(sessionId);
31488
- }
31489
- await this.#processes.retire(runtime, close);
31490
- }
31491
31708
  async #startRetainedSessionProcess(options, start) {
31492
- const runtime = await this.#processes.startRetained(
31493
- options.signal,
31494
- start,
31495
- this.#retention(options.sessionId, "execution")
31496
- );
31497
- this.#sessions.set(options.sessionId, runtime);
31498
- return runtime;
31499
- }
31500
- async #startRetainedNativeBrowserProcess(options, start) {
31501
- const runtime = await this.#processes.startRetained(
31502
- options.signal,
31503
- start,
31504
- this.#retention(options.sessionId, "browser")
31505
- );
31506
- this.#nativeBrowserSessions.set(options.sessionId, runtime);
31709
+ const runtime = await this.#processes.startRetained(options.signal, start, this.#retention(options.sessionId));
31710
+ this.#sessions.retain(options.sessionId, runtime);
31507
31711
  return runtime;
31508
31712
  }
31509
- async #startNativeBrowserPreparation(options) {
31510
- const operation = (async () => {
31511
- try {
31512
- return await this.#startRetainedNativeBrowserProcess(
31513
- options,
31514
- async () => await AcpProcessSession.startNativeSessionBrowser(
31515
- options,
31516
- () => this.#nativeBrowserObserver,
31517
- this.#nativeBrowserStore
31518
- )
31519
- );
31520
- } finally {
31521
- this.#nativeBrowserPreparing.delete(options.sessionId);
31522
- this.#processes.notifyChanged();
31523
- }
31524
- })();
31525
- this.#nativeBrowserPreparing.set(options.sessionId, operation);
31526
- return await operation;
31527
- }
31528
- async #replaceNativeBrowserPreparation(existing, options) {
31529
- const operation = (async () => {
31530
- try {
31531
- await this.#retireNativeBrowserProcess(options.sessionId, existing, async () => await existing.close());
31532
- return await this.#startRetainedNativeBrowserProcess(
31533
- options,
31534
- async () => await AcpProcessSession.startNativeSessionBrowser(
31535
- options,
31536
- () => this.#nativeBrowserObserver,
31537
- this.#nativeBrowserStore
31538
- )
31539
- );
31540
- } finally {
31541
- this.#nativeBrowserPreparing.delete(options.sessionId);
31542
- this.#processes.notifyChanged();
31543
- }
31544
- })();
31545
- this.#nativeBrowserPreparing.set(options.sessionId, operation);
31546
- return await operation;
31547
- }
31548
31713
  async #startPreparation(options, metrics = unmeasuredOperationMetrics) {
31549
- const operation = (async () => {
31550
- try {
31551
- return await this.#startRetainedSessionProcess(
31552
- options,
31553
- async () => await AcpProcessSession.start(
31554
- options.origin === void 0 && this.#sessionIdentity === "native" && isNativeChatSessionId(options.agent.id, options.sessionId) && this.#store.read(options.sessionId) instanceof MissingAcpStoredSession ? { ...options, origin: { kind: "import", nativeSessionId: options.sessionId } } : options,
31555
- () => this.#observer,
31556
- this.#store,
31557
- metrics
31558
- )
31559
- );
31560
- } finally {
31561
- this.#preparing.delete(options.sessionId);
31562
- this.#processes.notifyChanged();
31563
- }
31564
- })();
31565
- this.#preparing.set(options.sessionId, operation);
31566
- return await operation;
31714
+ return await this.#sessions.prepare(options.sessionId, async () => {
31715
+ return await this.#startRetainedSessionProcess(
31716
+ options,
31717
+ async () => await AcpProcessSession.start(
31718
+ 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,
31719
+ () => this.#observer,
31720
+ this.#store,
31721
+ metrics
31722
+ )
31723
+ );
31724
+ });
31567
31725
  }
31568
31726
  async #replacePreparation(existing, options, metrics) {
31569
- const operation = (async () => {
31570
- try {
31571
- await this.#retireSessionProcess(
31572
- options.sessionId,
31573
- existing,
31574
- async () => await metrics.measure("runtime-replacement-close", async () => await existing.close())
31575
- );
31576
- this.#store.remove(options.sessionId);
31577
- return await this.#startRetainedSessionProcess(
31578
- options,
31579
- async () => await AcpProcessSession.start(options, () => this.#observer, this.#store, metrics)
31580
- );
31581
- } finally {
31582
- this.#preparing.delete(options.sessionId);
31583
- this.#processes.notifyChanged();
31584
- }
31585
- })();
31586
- this.#preparing.set(options.sessionId, operation);
31587
- return await operation;
31727
+ return await this.#sessions.prepare(options.sessionId, async () => {
31728
+ await this.#retireSessionProcess(
31729
+ options.sessionId,
31730
+ existing,
31731
+ async () => await metrics.measure("runtime-replacement-close", async () => await existing.close())
31732
+ );
31733
+ this.#store.remove(options.sessionId);
31734
+ return await this.#startRetainedSessionProcess(
31735
+ options,
31736
+ async () => await AcpProcessSession.start(options, () => this.#observer, this.#store, metrics)
31737
+ );
31738
+ });
31588
31739
  }
31589
31740
  async #replaceExecutionPreparation(existing, options) {
31590
31741
  const desiredConfigOptions = existing.prepared().configOptions;
31591
- const operation = (async () => {
31742
+ return await this.#sessions.prepare(options.sessionId, async () => {
31743
+ await this.#retireSessionProcess(options.sessionId, existing, async () => await existing.close());
31744
+ const runtime = await this.#startRetainedSessionProcess(
31745
+ options,
31746
+ async () => await AcpProcessSession.start(options, () => this.#observer, this.#store)
31747
+ );
31592
31748
  try {
31593
- await this.#retireSessionProcess(options.sessionId, existing, async () => await existing.close());
31594
- const runtime = await this.#startRetainedSessionProcess(
31595
- options,
31596
- async () => await AcpProcessSession.start(options, () => this.#observer, this.#store)
31597
- );
31598
- try {
31599
- for (const desired of desiredConfigOptions) {
31600
- const current = runtime.prepared().configOptions.find((option) => option.id === desired.id);
31601
- if (current === void 0 || current.type !== desired.type) {
31602
- continue;
31603
- }
31604
- if (desired.type === "boolean") {
31605
- if (current.booleanValue !== desired.booleanValue) {
31606
- await runtime.configure({
31607
- ...options,
31608
- booleanValue: desired.booleanValue,
31609
- configId: desired.id,
31610
- requestId: `execution-prepare-${desired.id}`,
31611
- value: "",
31612
- valueType: "boolean"
31613
- });
31614
- }
31615
- continue;
31616
- }
31617
- if (current.currentValue !== desired.currentValue && current.values.some((value) => value.value === desired.currentValue)) {
31749
+ for (const desired of desiredConfigOptions) {
31750
+ const current = runtime.prepared().configOptions.find((option) => option.id === desired.id);
31751
+ if (current === void 0 || current.type !== desired.type) {
31752
+ continue;
31753
+ }
31754
+ if (desired.type === "boolean") {
31755
+ if (current.booleanValue !== desired.booleanValue) {
31618
31756
  await runtime.configure({
31619
31757
  ...options,
31620
- booleanValue: false,
31758
+ booleanValue: desired.booleanValue,
31621
31759
  configId: desired.id,
31622
31760
  requestId: `execution-prepare-${desired.id}`,
31623
- value: desired.currentValue,
31624
- valueType: "select"
31761
+ value: "",
31762
+ valueType: "boolean"
31625
31763
  });
31626
31764
  }
31765
+ continue;
31766
+ }
31767
+ if (current.currentValue !== desired.currentValue && current.values.some((value) => value.value === desired.currentValue)) {
31768
+ await runtime.configure({
31769
+ ...options,
31770
+ booleanValue: false,
31771
+ configId: desired.id,
31772
+ requestId: `execution-prepare-${desired.id}`,
31773
+ value: desired.currentValue,
31774
+ valueType: "select"
31775
+ });
31627
31776
  }
31628
- } catch (error62) {
31629
- await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
31630
- throw error62;
31631
31777
  }
31632
- return runtime;
31633
- } finally {
31634
- this.#preparing.delete(options.sessionId);
31635
- this.#processes.notifyChanged();
31778
+ } catch (error62) {
31779
+ await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
31780
+ throw error62;
31636
31781
  }
31637
- })();
31638
- this.#preparing.set(options.sessionId, operation);
31639
- return await operation;
31782
+ return runtime;
31783
+ });
31640
31784
  }
31641
31785
  prepareSession = async (options, metrics = unmeasuredOperationMetrics) => {
31642
31786
  const stored = metrics.measureSync("session-state-read", () => this.#store.read(options.sessionId));
@@ -31650,11 +31794,11 @@ var AcpSessionManager = class {
31650
31794
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
31651
31795
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
31652
31796
  }
31653
- const pending = this.#preparing.get(options.sessionId);
31797
+ const pending = this.#sessions.pending(options.sessionId);
31654
31798
  if (pending !== void 0) {
31655
31799
  const runtime2 = await metrics.measure("session-preparation-wait", async () => await pending);
31656
31800
  if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31657
- this.#touchExpectedSession(options.sessionId, runtime2);
31801
+ this.#sessions.touch(options.sessionId, runtime2);
31658
31802
  return runtime2.prepared();
31659
31803
  }
31660
31804
  return await this.prepareSession(options, metrics);
@@ -31662,7 +31806,7 @@ var AcpSessionManager = class {
31662
31806
  const existing = this.#sessions.get(options.sessionId);
31663
31807
  if (existing !== void 0) {
31664
31808
  if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31665
- this.#touchExpectedSession(options.sessionId, existing);
31809
+ this.#sessions.touch(options.sessionId, existing);
31666
31810
  return existing.prepared();
31667
31811
  }
31668
31812
  if (!existing.canRebind()) {
@@ -31716,11 +31860,10 @@ var AcpSessionManager = class {
31716
31860
  * Use inspectAgentSettings for Agent-scoped settings discovery.
31717
31861
  */
31718
31862
  inspectSession = async (options, metrics = unmeasuredOperationMetrics) => {
31719
- if (this.#preparing.has(options.sessionId) || this.#sessions.has(options.sessionId)) {
31863
+ if (this.#sessions.pending(options.sessionId) !== void 0 || this.#sessions.get(options.sessionId) !== void 0) {
31720
31864
  throw new AcpExecutionError("The configuration inspection session is already in use", {}, "session_busy");
31721
31865
  }
31722
- this.#beginSessionUse(options.sessionId);
31723
- try {
31866
+ return await this.#sessions.use(options.sessionId, async () => {
31724
31867
  const runtime = await this.#startPreparation(options, metrics);
31725
31868
  try {
31726
31869
  return runtime.prepared();
@@ -31731,12 +31874,10 @@ var AcpSessionManager = class {
31731
31874
  this.#store.remove(options.sessionId);
31732
31875
  }
31733
31876
  }
31734
- } finally {
31735
- this.#endSessionUse(options.sessionId);
31736
- }
31877
+ });
31737
31878
  };
31738
31879
  prepareExecutionSession = async (options) => {
31739
- const pending = this.#preparing.get(options.sessionId);
31880
+ const pending = this.#sessions.pending(options.sessionId);
31740
31881
  if (pending !== void 0) {
31741
31882
  await pending;
31742
31883
  return await this.prepareExecutionSession(options);
@@ -31753,7 +31894,7 @@ var AcpSessionManager = class {
31753
31894
  );
31754
31895
  }
31755
31896
  if (existing.usesEnvironment(options.agent.environment)) {
31756
- this.#touchExpectedSession(options.sessionId, existing);
31897
+ this.#sessions.touch(options.sessionId, existing);
31757
31898
  return existing.prepared();
31758
31899
  }
31759
31900
  if (existing.hasRunningTurn()) {
@@ -31765,75 +31906,36 @@ var AcpSessionManager = class {
31765
31906
  }
31766
31907
  return (await this.#replaceExecutionPreparation(existing, options)).prepared();
31767
31908
  };
31768
- prepareNativeSessionBrowser = async (options) => {
31769
- const pending = this.#nativeBrowserPreparing.get(options.sessionId);
31770
- if (pending !== void 0) {
31771
- const runtime2 = await pending;
31772
- if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31773
- this.#touchExpectedNativeBrowserSession(options.sessionId, runtime2);
31774
- return runtime2.prepared();
31775
- }
31776
- return await this.prepareNativeSessionBrowser(options);
31777
- }
31778
- const existing = this.#nativeBrowserSessions.get(options.sessionId);
31779
- if (existing !== void 0) {
31780
- if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31781
- this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
31782
- return existing.prepared();
31783
- }
31784
- if (!existing.canRebind()) {
31785
- throw new AcpExecutionError(
31786
- "The agent or workspace cannot change after a conversation has started",
31787
- {},
31788
- "session_identity_mismatch"
31789
- );
31790
- }
31791
- const runtime2 = await this.#replaceNativeBrowserPreparation(existing, options);
31792
- return runtime2.prepared();
31793
- }
31794
- const runtime = await this.#startNativeBrowserPreparation(options);
31795
- return runtime.prepared();
31796
- };
31909
+ prepareNativeSessionBrowser = (options) => this.#browsers.prepare(options);
31797
31910
  configureSession = async (options) => {
31798
- this.#beginSessionUse(options.sessionId);
31799
- try {
31911
+ return await this.#sessions.use(options.sessionId, async () => {
31800
31912
  const runtime = await this.ensureSession(options);
31801
31913
  return await runtime.configure(options);
31802
- } finally {
31803
- this.#endSessionUse(options.sessionId);
31804
- }
31914
+ });
31805
31915
  };
31806
31916
  cancelPrompt = async (sessionId) => {
31807
- const existing = this.#cancellationRequests.get(sessionId);
31808
- if (existing !== void 0) {
31809
- await existing;
31810
- return;
31811
- }
31812
- const cancellation = (async () => {
31917
+ await this.#executions.cancel(sessionId, async () => {
31813
31918
  const runtime = this.#sessions.get(sessionId);
31814
31919
  if (runtime !== void 0) {
31815
31920
  try {
31816
31921
  await runtime.cancel();
31817
31922
  } finally {
31818
- if (this.#sessions.get(sessionId) === runtime) {
31819
- this.#sessions.delete(sessionId);
31923
+ if (this.#sessions.detach(sessionId, runtime)) {
31820
31924
  this.#processes.releaseClosed(runtime);
31821
31925
  }
31822
31926
  }
31823
31927
  }
31824
- })();
31825
- this.#cancellationRequests.set(sessionId, cancellation);
31826
- await cancellation;
31928
+ });
31827
31929
  };
31828
31930
  steerPrompt = async (options) => {
31829
- if (this.#activeCommandIds.get(options.sessionId) !== options.targetCommandId) {
31931
+ if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
31830
31932
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
31831
31933
  }
31832
- const pending = this.#preparing.get(options.sessionId);
31934
+ const pending = this.#sessions.pending(options.sessionId);
31833
31935
  if (pending !== void 0) {
31834
31936
  await pending;
31835
31937
  }
31836
- if (this.#activeCommandIds.get(options.sessionId) !== options.targetCommandId) {
31938
+ if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
31837
31939
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
31838
31940
  }
31839
31941
  const runtime = this.#sessions.get(options.sessionId);
@@ -31871,27 +31973,24 @@ var AcpSessionManager = class {
31871
31973
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
31872
31974
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
31873
31975
  }
31874
- if (this.#activeCommandIds.has(options.sessionId)) {
31875
- throw new AcpExecutionError("Another prompt is already running in this conversation", {}, "session_busy");
31876
- }
31877
- this.#activeCommandIds.set(options.sessionId, options.commandId);
31976
+ this.#executions.begin(options.sessionId, options.commandId);
31878
31977
  try {
31879
- if (this.#cancellationRequests.has(options.sessionId)) {
31978
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
31880
31979
  return { stopReason: "cancelled" };
31881
31980
  }
31882
31981
  const runtime = await this.ensureSession(options);
31883
31982
  if (this.#nativeOperations.has(`${options.agent.id}:${runtime.nativeSessionId()}`)) {
31884
31983
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
31885
31984
  }
31886
- if (this.#cancellationRequests.has(options.sessionId)) {
31985
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
31887
31986
  return { stopReason: "cancelled" };
31888
31987
  }
31889
31988
  try {
31890
31989
  const result = await runtime.prompt(options);
31891
- return this.#cancellationRequests.has(options.sessionId) ? { stopReason: "cancelled" } : result;
31990
+ return this.#executions.cancellation(options.sessionId) !== void 0 ? { stopReason: "cancelled" } : result;
31892
31991
  } catch (error62) {
31893
- if (this.#cancellationRequests.has(options.sessionId)) {
31894
- await this.#cancellationRequests.get(options.sessionId);
31992
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
31993
+ await this.#executions.cancellation(options.sessionId);
31895
31994
  return { stopReason: "cancelled" };
31896
31995
  }
31897
31996
  throw error62;
@@ -31905,7 +32004,7 @@ var AcpSessionManager = class {
31905
32004
  async () => await failedRuntime.close()
31906
32005
  );
31907
32006
  }
31908
- if (this.#cancellationRequests.has(options.sessionId)) {
32007
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
31909
32008
  return { stopReason: "cancelled" };
31910
32009
  }
31911
32010
  if (error62 instanceof AcpExecutionError) {
@@ -31916,13 +32015,10 @@ var AcpSessionManager = class {
31916
32015
  });
31917
32016
  } finally {
31918
32017
  try {
31919
- await this.#cancellationRequests.get(options.sessionId);
32018
+ await this.#executions.cancellation(options.sessionId);
31920
32019
  } finally {
31921
- if (this.#activeCommandIds.get(options.sessionId) === options.commandId) {
31922
- this.#activeCommandIds.delete(options.sessionId);
31923
- }
31924
- this.#cancellationRequests.delete(options.sessionId);
31925
- this.#touchSession(options.sessionId);
32020
+ this.#executions.finish(options.sessionId, options.commandId);
32021
+ this.#sessions.touch(options.sessionId);
31926
32022
  this.#processes.notifyChanged();
31927
32023
  }
31928
32024
  }
@@ -31943,14 +32039,11 @@ var AcpSessionManager = class {
31943
32039
  };
31944
32040
  close = async () => {
31945
32041
  const sessions = [...this.#sessions.entries()];
31946
- const nativeBrowserSessions = [...this.#nativeBrowserSessions.entries()];
31947
32042
  await Promise.all([
31948
32043
  ...sessions.map(
31949
32044
  async ([sessionId, runtime]) => await this.#retireSessionProcess(sessionId, runtime, async () => await runtime.close())
31950
32045
  ),
31951
- ...nativeBrowserSessions.map(
31952
- async ([sessionId, runtime]) => await this.#retireNativeBrowserProcess(sessionId, runtime, async () => await runtime.close())
31953
- )
32046
+ this.#browsers.close()
31954
32047
  ]);
31955
32048
  };
31956
32049
  findSessions = (sessionIds) => sessionIds.flatMap((sessionId) => {
@@ -31962,38 +32055,38 @@ var AcpSessionManager = class {
31962
32055
  createNativeSession = async (options) => {
31963
32056
  const temporarySessionId = `new-${crypto.randomUUID()}`;
31964
32057
  const temporaryOptions = { ...options, sessionId: temporarySessionId };
31965
- this.#beginSessionUse(temporarySessionId);
31966
- try {
31967
- const runtime = await this.#startPreparation(temporaryOptions);
31968
- let adopted = false;
32058
+ return await this.#sessions.use(temporarySessionId, async () => {
31969
32059
  try {
31970
- const nativeSessionId = runtime.nativeSessionId();
31971
- if (!isNativeChatSessionId(options.agent.id, nativeSessionId) || this.#sessions.has(nativeSessionId) || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
31972
- throw new AcpExecutionError(
31973
- "The Agent returned an invalid new Session ID",
31974
- {},
31975
- "invalid_native_session_response"
31976
- );
32060
+ const runtime = await this.#startPreparation(temporaryOptions);
32061
+ let adopted = false;
32062
+ try {
32063
+ const nativeSessionId = runtime.nativeSessionId();
32064
+ if (!isNativeChatSessionId(options.agent.id, nativeSessionId) || this.#sessions.get(nativeSessionId) !== void 0 || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
32065
+ throw new AcpExecutionError(
32066
+ "The Agent returned an invalid new Session ID",
32067
+ {},
32068
+ "invalid_native_session_response"
32069
+ );
32070
+ }
32071
+ runtime.adoptNativeSessionId(nativeSessionId);
32072
+ this.#sessions.detach(temporarySessionId, runtime);
32073
+ this.#sessions.retain(nativeSessionId, runtime);
32074
+ this.#processes.retain(runtime, this.#retention(nativeSessionId));
32075
+ adopted = true;
32076
+ return nativeSessionId;
32077
+ } finally {
32078
+ if (!adopted) {
32079
+ await this.#retireSessionProcess(
32080
+ temporarySessionId,
32081
+ runtime,
32082
+ async () => await runtime.closeProcess()
32083
+ );
32084
+ }
31977
32085
  }
31978
- runtime.adoptNativeSessionId(nativeSessionId);
31979
- this.#sessions.delete(temporarySessionId);
31980
- this.#sessions.set(nativeSessionId, runtime);
31981
- this.#processes.retain(runtime, this.#retention(nativeSessionId, "execution"));
31982
- adopted = true;
31983
- return nativeSessionId;
31984
32086
  } finally {
31985
- if (!adopted) {
31986
- await this.#retireSessionProcess(
31987
- temporarySessionId,
31988
- runtime,
31989
- async () => await runtime.closeProcess()
31990
- );
31991
- }
32087
+ this.#store.remove(temporarySessionId);
31992
32088
  }
31993
- } finally {
31994
- this.#store.remove(temporarySessionId);
31995
- this.#endSessionUse(temporarySessionId);
31996
- }
32089
+ });
31997
32090
  };
31998
32091
  ensureSession = async (options) => {
31999
32092
  const existing = this.#sessions.get(options.sessionId);
@@ -32005,7 +32098,7 @@ var AcpSessionManager = class {
32005
32098
  "session_identity_mismatch"
32006
32099
  );
32007
32100
  }
32008
- this.#touchExpectedSession(options.sessionId, existing);
32101
+ this.#sessions.touch(options.sessionId, existing);
32009
32102
  return existing;
32010
32103
  }
32011
32104
  await this.prepareSession(options);
@@ -32015,38 +32108,18 @@ var AcpSessionManager = class {
32015
32108
  }
32016
32109
  return started;
32017
32110
  };
32018
- async #ensureNativeSessionBrowser(options) {
32019
- const existing = this.#nativeBrowserSessions.get(options.sessionId);
32020
- if (existing !== void 0) {
32021
- if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
32022
- throw new AcpExecutionError(
32023
- "The agent or workspace cannot change after a conversation has started",
32024
- {},
32025
- "session_identity_mismatch"
32026
- );
32027
- }
32028
- this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
32029
- return existing;
32030
- }
32031
- await this.prepareNativeSessionBrowser(options);
32032
- const started = this.#nativeBrowserSessions.get(options.sessionId);
32033
- if (started === void 0) {
32034
- throw new AcpExecutionError("ACP native session browser did not start");
32035
- }
32036
- return started;
32037
- }
32038
32111
  listNativeSessions = async (options, cursor) => {
32039
- this.#beginNativeBrowserUse(options.sessionId);
32040
- try {
32041
- return await (await this.#ensureNativeSessionBrowser(options)).listNativeSessions(cursor, options.signal);
32042
- } finally {
32043
- this.#endNativeBrowserUse(options.sessionId);
32044
- }
32112
+ return await this.#browsers.use(
32113
+ options,
32114
+ async (runtime) => await runtime.listNativeSessions(cursor, options.signal)
32115
+ );
32045
32116
  };
32046
32117
  previewNativeSession = async (options, nativeSessionId) => {
32047
32118
  const key = `${options.agent.id}:${nativeSessionId}`;
32048
32119
  const linked = this.#store.findNative(options.agent.id, nativeSessionId);
32049
- if (this.#nativeOperations.has(key) || linked.some((entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId))) {
32120
+ if (this.#nativeOperations.has(key) || linked.some(
32121
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
32122
+ )) {
32050
32123
  throw new AcpExecutionError(
32051
32124
  "Wait for the native session's running operation to finish",
32052
32125
  {},
@@ -32055,26 +32128,13 @@ var AcpSessionManager = class {
32055
32128
  }
32056
32129
  this.#nativeOperations.add(key);
32057
32130
  try {
32058
- return await this.#processes.runTransient(
32059
- options.signal,
32060
- async () => await AcpProcessSession.startNativeSessionBrowser(
32061
- options,
32062
- () => this.#nativeBrowserObserver,
32063
- new MemoryAcpSessionStore()
32064
- ),
32065
- async (runtime) => {
32066
- runtime.selectNativeSession(nativeSessionId);
32067
- return await runtime.loadHistory(options.signal);
32068
- },
32069
- async (runtime) => await runtime.closeProcess()
32070
- );
32131
+ return await this.#browsers.preview(options, nativeSessionId);
32071
32132
  } finally {
32072
32133
  this.#nativeOperations.delete(key);
32073
32134
  }
32074
32135
  };
32075
32136
  loadSessionHistory = async (options) => {
32076
- this.#beginSessionUse(options.sessionId);
32077
- try {
32137
+ return await this.#sessions.use(options.sessionId, async () => {
32078
32138
  const runtime = await this.ensureSession(options);
32079
32139
  const stored = this.#store.read(options.sessionId);
32080
32140
  if (stored instanceof FoundAcpStoredSession && this.#nativeHistoryReader !== null) {
@@ -32088,9 +32148,7 @@ var AcpSessionManager = class {
32088
32148
  }
32089
32149
  }
32090
32150
  return await runtime.loadHistory(options.signal);
32091
- } finally {
32092
- this.#endSessionUse(options.sessionId);
32093
- }
32151
+ });
32094
32152
  };
32095
32153
  openNativeSession = async (options, origin) => {
32096
32154
  const key = `${options.agent.id}:${origin.nativeSessionId}`;
@@ -32098,7 +32156,9 @@ var AcpSessionManager = class {
32098
32156
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
32099
32157
  }
32100
32158
  const linked = this.#store.findNative(options.agent.id, origin.nativeSessionId);
32101
- if (linked.some((entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId))) {
32159
+ if (linked.some(
32160
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
32161
+ )) {
32102
32162
  throw new AcpExecutionError(
32103
32163
  "Wait for the native session's running operation to finish",
32104
32164
  {},
@@ -32107,13 +32167,12 @@ var AcpSessionManager = class {
32107
32167
  }
32108
32168
  const existing = linked[0];
32109
32169
  if (origin.kind === "import" && existing !== void 0) {
32110
- this.#beginSessionUse(existing.sessionId);
32111
- try {
32170
+ return await this.#sessions.use(existing.sessionId, async () => {
32112
32171
  const runtime = await this.ensureSession({
32113
32172
  ...options,
32114
32173
  sessionId: existing.sessionId
32115
32174
  });
32116
- if (this.#activeCommandIds.has(existing.sessionId) || this.#nativeOperations.has(key)) {
32175
+ if (this.#executions.command(existing.sessionId) !== void 0 || this.#nativeOperations.has(key)) {
32117
32176
  throw new AcpExecutionError(
32118
32177
  "Wait for the native session's running operation to finish",
32119
32178
  {},
@@ -32130,48 +32189,48 @@ var AcpSessionManager = class {
32130
32189
  this.#nativeOperations.delete(key);
32131
32190
  this.#processes.notifyChanged();
32132
32191
  }
32133
- } finally {
32134
- this.#endSessionUse(existing.sessionId);
32135
- }
32192
+ });
32136
32193
  }
32137
- if (this.#sessions.has(options.sessionId) || this.#preparing.has(options.sessionId) || this.#store.read(options.sessionId) instanceof FoundAcpStoredSession || this.#store.read(options.sessionId) instanceof DeletedAcpStoredSession) {
32194
+ if (this.#sessions.get(options.sessionId) !== void 0 || this.#sessions.pending(options.sessionId) !== void 0 || this.#store.read(options.sessionId) instanceof FoundAcpStoredSession || this.#store.read(options.sessionId) instanceof DeletedAcpStoredSession) {
32138
32195
  throw new AcpExecutionError("The destination conversation already exists", {}, "session_identity_mismatch");
32139
32196
  }
32140
- this.#beginSessionUse(options.sessionId);
32141
- this.#nativeOperations.add(key);
32142
- try {
32143
- const runtime = await this.#startPreparation({ ...options, origin });
32144
- const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
32145
- this.#nativeOperations.add(openedKey);
32197
+ return await this.#sessions.use(options.sessionId, async () => {
32198
+ this.#nativeOperations.add(key);
32146
32199
  try {
32147
- return {
32148
- sessionId: options.sessionId,
32149
- prepared: runtime.prepared()
32150
- };
32200
+ const runtime = await this.#startPreparation({ ...options, origin });
32201
+ const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
32202
+ this.#nativeOperations.add(openedKey);
32203
+ try {
32204
+ return {
32205
+ sessionId: options.sessionId,
32206
+ prepared: runtime.prepared()
32207
+ };
32208
+ } finally {
32209
+ this.#nativeOperations.delete(openedKey);
32210
+ this.#processes.notifyChanged();
32211
+ }
32212
+ } catch (error62) {
32213
+ this.#store.remove(options.sessionId);
32214
+ const runtime = this.#sessions.get(options.sessionId);
32215
+ if (runtime !== void 0) {
32216
+ await this.#retireSessionProcess(
32217
+ options.sessionId,
32218
+ runtime,
32219
+ async () => await runtime.closeProcess()
32220
+ );
32221
+ }
32222
+ throw error62;
32151
32223
  } finally {
32152
- this.#nativeOperations.delete(openedKey);
32153
- this.#processes.notifyChanged();
32154
- }
32155
- } catch (error62) {
32156
- this.#store.remove(options.sessionId);
32157
- const runtime = this.#sessions.get(options.sessionId);
32158
- if (runtime !== void 0) {
32159
- await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.closeProcess());
32224
+ this.#nativeOperations.delete(key);
32160
32225
  }
32161
- throw error62;
32162
- } finally {
32163
- this.#nativeOperations.delete(key);
32164
- this.#endSessionUse(options.sessionId);
32165
- }
32226
+ });
32166
32227
  };
32167
32228
  deleteNativeSession = async (options, nativeSessionId) => {
32168
- this.#beginNativeBrowserUse(options.sessionId);
32169
- try {
32170
- const runtime = await this.#ensureNativeSessionBrowser(options);
32229
+ return await this.#browsers.use(options, async (runtime) => {
32171
32230
  const key = `${options.agent.id}:${nativeSessionId}`;
32172
32231
  const linked = this.#store.findNative(options.agent.id, nativeSessionId);
32173
32232
  if (this.#nativeOperations.has(key) || linked.some(
32174
- (entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId)
32233
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
32175
32234
  )) {
32176
32235
  throw new AcpExecutionError(
32177
32236
  "Wait for the native session's running operation to finish",
@@ -32191,20 +32250,14 @@ var AcpSessionManager = class {
32191
32250
  await runtime.deleteNativeSession(nativeSessionId, options.signal);
32192
32251
  const deleted = this.#store.markDeleted(options.agent.id, nativeSessionId);
32193
32252
  if (runtime.nativeSessionId() === nativeSessionId) {
32194
- await this.#retireNativeBrowserProcess(
32195
- options.sessionId,
32196
- runtime,
32197
- async () => await runtime.close()
32198
- );
32253
+ await this.#browsers.retire(options.sessionId, runtime);
32199
32254
  }
32200
32255
  return deleted;
32201
32256
  } finally {
32202
32257
  this.#nativeOperations.delete(key);
32203
32258
  this.#processes.notifyChanged();
32204
32259
  }
32205
- } finally {
32206
- this.#endNativeBrowserUse(options.sessionId);
32207
- }
32260
+ });
32208
32261
  };
32209
32262
  };
32210
32263