@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/cli.js CHANGED
@@ -3381,7 +3381,7 @@ var program = new Command();
3381
3381
  // package.json
3382
3382
  var package_default = {
3383
3383
  name: "@moxt-ai/mobius",
3384
- version: "0.0.125",
3384
+ version: "0.0.127",
3385
3385
  description: "Mobius CLI for pairing and running local AI agent services.",
3386
3386
  type: "module",
3387
3387
  bin: {
@@ -4818,8 +4818,8 @@ function acpEndpointTerminationError(termination) {
4818
4818
  case "closed":
4819
4819
  return new AcpExecutionError("The ACP endpoint is closed", {}, "acp_endpoint_closed");
4820
4820
  default: {
4821
- const unreachable = termination;
4822
- return unreachable;
4821
+ const unreachable2 = termination;
4822
+ return unreachable2;
4823
4823
  }
4824
4824
  }
4825
4825
  }
@@ -39479,6 +39479,301 @@ var AcpProcessSession = class _AcpProcessSession {
39479
39479
  };
39480
39480
  };
39481
39481
 
39482
+ // src/acp/retained-sessions.ts
39483
+ var RetainedSessions = class {
39484
+ #entries = /* @__PURE__ */ new Map();
39485
+ #events;
39486
+ constructor(events) {
39487
+ this.#events = events;
39488
+ }
39489
+ #entry = (id) => this.#entries.get(id) ?? { lifecycle: { kind: "empty" }, users: 0 };
39490
+ #write = (id, entry) => {
39491
+ if (entry.lifecycle.kind === "empty" && entry.users === 0) {
39492
+ this.#entries.delete(id);
39493
+ } else {
39494
+ this.#entries.set(id, entry);
39495
+ }
39496
+ };
39497
+ #resource = (entry) => entry.lifecycle.kind === "preparing" ? entry.lifecycle.resource : entry.lifecycle;
39498
+ get = (id) => {
39499
+ const resource = this.#resource(this.#entry(id));
39500
+ return resource.kind === "ready" ? resource.runtime : void 0;
39501
+ };
39502
+ pending = (id) => {
39503
+ const state = this.#entry(id).lifecycle;
39504
+ return state.kind === "preparing" ? state.completion : void 0;
39505
+ };
39506
+ isBusy = (id) => {
39507
+ const entry = this.#entry(id);
39508
+ return entry.users > 0 || entry.lifecycle.kind === "preparing";
39509
+ };
39510
+ touch = (id, expected = this.get(id)) => {
39511
+ if (expected !== void 0 && this.get(id) === expected) {
39512
+ this.#events.touch(expected);
39513
+ }
39514
+ };
39515
+ #setResource = (id, resource) => {
39516
+ const entry = this.#entry(id);
39517
+ this.#write(id, {
39518
+ ...entry,
39519
+ lifecycle: entry.lifecycle.kind === "preparing" ? { ...entry.lifecycle, resource } : resource
39520
+ });
39521
+ };
39522
+ retain = (id, runtime) => {
39523
+ this.#setResource(id, { kind: "ready", runtime });
39524
+ };
39525
+ detach = (id, expected) => {
39526
+ if (this.get(id) !== expected) {
39527
+ return false;
39528
+ }
39529
+ this.#setResource(id, { kind: "empty" });
39530
+ return true;
39531
+ };
39532
+ entries = () => {
39533
+ const result = [];
39534
+ for (const id of this.#entries.keys()) {
39535
+ const runtime = this.get(id);
39536
+ if (runtime !== void 0) {
39537
+ result.push([id, runtime]);
39538
+ }
39539
+ }
39540
+ return result;
39541
+ };
39542
+ prepare = (id, start) => {
39543
+ const pending = this.pending(id);
39544
+ if (pending !== void 0) {
39545
+ return pending;
39546
+ }
39547
+ const completion = Promise.withResolvers();
39548
+ const entry = this.#entry(id);
39549
+ this.#write(id, {
39550
+ ...entry,
39551
+ lifecycle: {
39552
+ kind: "preparing",
39553
+ completion: completion.promise,
39554
+ resource: this.#resource(entry)
39555
+ }
39556
+ });
39557
+ const run = async () => {
39558
+ try {
39559
+ return await start();
39560
+ } finally {
39561
+ const current = this.#entry(id);
39562
+ if (current.lifecycle.kind === "preparing" && current.lifecycle.completion === completion.promise) {
39563
+ this.#write(id, { ...current, lifecycle: current.lifecycle.resource });
39564
+ }
39565
+ this.#events.changed();
39566
+ }
39567
+ };
39568
+ void run().then(completion.resolve, completion.reject);
39569
+ return completion.promise;
39570
+ };
39571
+ use = async (id, operation) => {
39572
+ const entry = this.#entry(id);
39573
+ this.#write(id, { ...entry, users: entry.users + 1 });
39574
+ this.touch(id);
39575
+ try {
39576
+ return await operation();
39577
+ } finally {
39578
+ const current = this.#entry(id);
39579
+ this.#write(id, { ...current, users: current.users - 1 });
39580
+ this.touch(id);
39581
+ this.#events.changed();
39582
+ }
39583
+ };
39584
+ };
39585
+
39586
+ // src/acp/native-session-browsers.ts
39587
+ var NativeSessionBrowsers = class {
39588
+ #sessions;
39589
+ #processes;
39590
+ #observer = new NullAcpSessionObserver();
39591
+ #store = new MemoryAcpSessionStore();
39592
+ constructor(processes) {
39593
+ this.#processes = processes;
39594
+ this.#sessions = new RetainedSessions({ touch: processes.touch, changed: processes.notifyChanged });
39595
+ }
39596
+ retire = async (sessionId, runtime) => {
39597
+ const retained = this.#sessions.detach(sessionId, runtime);
39598
+ if (retained) {
39599
+ this.#store.remove(sessionId);
39600
+ }
39601
+ await this.#processes.retire(runtime, () => runtime.close());
39602
+ };
39603
+ async #startRetained(options) {
39604
+ const runtime = await this.#processes.startRetained(
39605
+ options.signal,
39606
+ () => AcpProcessSession.startNativeSessionBrowser(options, () => this.#observer, this.#store),
39607
+ {
39608
+ isBusy: () => this.#sessions.isBusy(options.sessionId),
39609
+ retire: (runtime2) => this.retire(options.sessionId, runtime2)
39610
+ }
39611
+ );
39612
+ this.#sessions.retain(options.sessionId, runtime);
39613
+ return runtime;
39614
+ }
39615
+ async #startPreparation(options) {
39616
+ return await this.#sessions.prepare(options.sessionId, async () => {
39617
+ return await this.#startRetained(options);
39618
+ });
39619
+ }
39620
+ async #replacePreparation(existing, options) {
39621
+ return await this.#sessions.prepare(options.sessionId, async () => {
39622
+ await this.retire(options.sessionId, existing);
39623
+ return await this.#startRetained(options);
39624
+ });
39625
+ }
39626
+ prepare = async (options) => {
39627
+ const pending = this.#sessions.pending(options.sessionId);
39628
+ if (pending !== void 0) {
39629
+ const runtime2 = await pending;
39630
+ if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
39631
+ this.#sessions.touch(options.sessionId, runtime2);
39632
+ return runtime2.prepared();
39633
+ }
39634
+ return await this.prepare(options);
39635
+ }
39636
+ const existing = this.#sessions.get(options.sessionId);
39637
+ if (existing !== void 0) {
39638
+ if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
39639
+ this.#sessions.touch(options.sessionId, existing);
39640
+ return existing.prepared();
39641
+ }
39642
+ if (!existing.canRebind()) {
39643
+ throw new AcpExecutionError(
39644
+ "The agent or workspace cannot change after a conversation has started",
39645
+ {},
39646
+ "session_identity_mismatch"
39647
+ );
39648
+ }
39649
+ const runtime2 = await this.#replacePreparation(existing, options);
39650
+ return runtime2.prepared();
39651
+ }
39652
+ const runtime = await this.#startPreparation(options);
39653
+ return runtime.prepared();
39654
+ };
39655
+ async #ensure(options) {
39656
+ const existing = this.#sessions.get(options.sessionId);
39657
+ if (existing !== void 0) {
39658
+ if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
39659
+ throw new AcpExecutionError(
39660
+ "The agent or workspace cannot change after a conversation has started",
39661
+ {},
39662
+ "session_identity_mismatch"
39663
+ );
39664
+ }
39665
+ this.#sessions.touch(options.sessionId, existing);
39666
+ return existing;
39667
+ }
39668
+ await this.prepare(options);
39669
+ const started = this.#sessions.get(options.sessionId);
39670
+ if (started === void 0) {
39671
+ throw new AcpExecutionError("ACP native session browser did not start");
39672
+ }
39673
+ return started;
39674
+ }
39675
+ use = async (options, operation) => await this.#sessions.use(options.sessionId, async () => await operation(await this.#ensure(options)));
39676
+ preview = async (options, nativeSessionId) => {
39677
+ return await this.#processes.runTransient(
39678
+ options.signal,
39679
+ async () => await AcpProcessSession.startNativeSessionBrowser(
39680
+ options,
39681
+ () => this.#observer,
39682
+ new MemoryAcpSessionStore()
39683
+ ),
39684
+ async (runtime) => {
39685
+ runtime.selectNativeSession(nativeSessionId);
39686
+ return await runtime.loadHistory(options.signal);
39687
+ },
39688
+ async (runtime) => await runtime.closeProcess()
39689
+ );
39690
+ };
39691
+ close = async () => {
39692
+ await Promise.all(this.#sessions.entries().map(async ([id, runtime]) => await this.retire(id, runtime)));
39693
+ };
39694
+ };
39695
+
39696
+ // src/acp/session-executions.ts
39697
+ var unreachable = (state) => {
39698
+ throw new AcpExecutionError("Unexpected execution state", { cause: state });
39699
+ };
39700
+ var SessionExecutions = class {
39701
+ #entries = /* @__PURE__ */ new Map();
39702
+ command = (id) => {
39703
+ const state = this.#entries.get(id);
39704
+ switch (state?.kind) {
39705
+ case "running":
39706
+ case "cancelling":
39707
+ return state.commandId;
39708
+ case "cancelled-before-start":
39709
+ case void 0:
39710
+ return void 0;
39711
+ default:
39712
+ return unreachable(state);
39713
+ }
39714
+ };
39715
+ cancellation = (id) => {
39716
+ const state = this.#entries.get(id);
39717
+ switch (state?.kind) {
39718
+ case "cancelling":
39719
+ case "cancelled-before-start":
39720
+ return state.completion;
39721
+ case "running":
39722
+ case void 0:
39723
+ return void 0;
39724
+ default:
39725
+ return unreachable(state);
39726
+ }
39727
+ };
39728
+ isBusy = (id) => this.#entries.has(id);
39729
+ begin = (id, commandId) => {
39730
+ const state = this.#entries.get(id);
39731
+ switch (state?.kind) {
39732
+ case "running":
39733
+ case "cancelling":
39734
+ throw new AcpExecutionError(
39735
+ "Another prompt is already running in this conversation",
39736
+ {},
39737
+ "session_busy"
39738
+ );
39739
+ case "cancelled-before-start":
39740
+ case void 0:
39741
+ break;
39742
+ default:
39743
+ unreachable(state);
39744
+ }
39745
+ this.#entries.set(
39746
+ id,
39747
+ state?.kind === "cancelled-before-start" ? { kind: "cancelling", commandId, completion: state.completion } : { kind: "running", commandId }
39748
+ );
39749
+ };
39750
+ cancel = (id, stop) => {
39751
+ const state = this.#entries.get(id);
39752
+ switch (state?.kind) {
39753
+ case "cancelling":
39754
+ case "cancelled-before-start":
39755
+ return state.completion;
39756
+ case "running":
39757
+ case void 0:
39758
+ break;
39759
+ default:
39760
+ return unreachable(state);
39761
+ }
39762
+ const completion = Promise.withResolvers();
39763
+ this.#entries.set(
39764
+ id,
39765
+ state?.kind === "running" ? { kind: "cancelling", commandId: state.commandId, completion: completion.promise } : { kind: "cancelled-before-start", completion: completion.promise }
39766
+ );
39767
+ void (async () => await stop())().then(completion.resolve, completion.reject);
39768
+ return completion.promise;
39769
+ };
39770
+ finish = (id, commandId) => {
39771
+ if (this.command(id) === commandId) {
39772
+ this.#entries.delete(id);
39773
+ }
39774
+ };
39775
+ };
39776
+
39482
39777
  // src/native-sessions/chat-agent.ts
39483
39778
  function supportsNativeChat(agentId) {
39484
39779
  return agentId === "codex" || agentId === "claude" || agentId === "opencode" || agentId === "pi";
@@ -39690,18 +39985,11 @@ var AcpProcessPool = class {
39690
39985
  var SESSION_CONTROL_TIMEOUT_MILLISECONDS = 3e4;
39691
39986
  var AcpSessionManager = class {
39692
39987
  #processes = new AcpProcessPool();
39693
- #activeCommandIds = /* @__PURE__ */ new Map();
39988
+ #sessions = new RetainedSessions({ touch: this.#processes.touch, changed: this.#processes.notifyChanged });
39989
+ #browsers = new NativeSessionBrowsers(this.#processes);
39990
+ #executions = new SessionExecutions();
39694
39991
  #agentSettingsObserver = new NullAcpSessionObserver();
39695
39992
  #nativeOperations = /* @__PURE__ */ new Set();
39696
- #nativeBrowserObserver = new NullAcpSessionObserver();
39697
- #nativeBrowserPreparing = /* @__PURE__ */ new Map();
39698
- #nativeBrowserSessions = /* @__PURE__ */ new Map();
39699
- #nativeBrowserStore = new MemoryAcpSessionStore();
39700
- #nativeBrowserUseCounts = /* @__PURE__ */ new Map();
39701
- #cancellationRequests = /* @__PURE__ */ new Map();
39702
- #preparing = /* @__PURE__ */ new Map();
39703
- #sessionUseCounts = /* @__PURE__ */ new Map();
39704
- #sessions = /* @__PURE__ */ new Map();
39705
39993
  #store;
39706
39994
  #nativeHistoryReader;
39707
39995
  #sessionIdentity;
@@ -39715,81 +40003,18 @@ var AcpSessionManager = class {
39715
40003
  setObserver = (observer) => {
39716
40004
  this.#observer = observer;
39717
40005
  };
39718
- #touchSession(sessionId) {
39719
- const runtime = this.#sessions.get(sessionId);
39720
- if (runtime === void 0) {
39721
- return;
39722
- }
39723
- this.#processes.touch(runtime);
39724
- }
39725
- #touchExpectedSession(sessionId, expected) {
39726
- if (this.#sessions.get(sessionId) !== expected) {
39727
- return;
39728
- }
39729
- this.#processes.touch(expected);
39730
- }
39731
- #touchNativeBrowserSession(sessionId) {
39732
- const runtime = this.#nativeBrowserSessions.get(sessionId);
39733
- if (runtime === void 0) {
39734
- return;
39735
- }
39736
- this.#processes.touch(runtime);
39737
- }
39738
- #touchExpectedNativeBrowserSession(sessionId, expected) {
39739
- if (this.#nativeBrowserSessions.get(sessionId) !== expected) {
39740
- return;
39741
- }
39742
- this.#processes.touch(expected);
39743
- }
39744
- #beginSessionUse(sessionId) {
39745
- this.#sessionUseCounts.set(sessionId, (this.#sessionUseCounts.get(sessionId) ?? 0) + 1);
39746
- this.#touchSession(sessionId);
39747
- }
39748
- #endSessionUse(sessionId) {
39749
- const count = this.#sessionUseCounts.get(sessionId) ?? 0;
39750
- if (count <= 1) {
39751
- this.#sessionUseCounts.delete(sessionId);
39752
- } else {
39753
- this.#sessionUseCounts.set(sessionId, count - 1);
39754
- }
39755
- this.#touchSession(sessionId);
39756
- this.#processes.notifyChanged();
39757
- }
39758
- #beginNativeBrowserUse(sessionId) {
39759
- this.#nativeBrowserUseCounts.set(sessionId, (this.#nativeBrowserUseCounts.get(sessionId) ?? 0) + 1);
39760
- this.#touchNativeBrowserSession(sessionId);
39761
- }
39762
- #endNativeBrowserUse(sessionId) {
39763
- const count = this.#nativeBrowserUseCounts.get(sessionId) ?? 0;
39764
- if (count <= 1) {
39765
- this.#nativeBrowserUseCounts.delete(sessionId);
39766
- } else {
39767
- this.#nativeBrowserUseCounts.set(sessionId, count - 1);
39768
- }
39769
- this.#touchNativeBrowserSession(sessionId);
39770
- this.#processes.notifyChanged();
39771
- }
39772
40006
  #isNativeSessionOperationActive(sessionId) {
39773
40007
  const stored = this.#store.read(sessionId);
39774
40008
  return stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`);
39775
40009
  }
39776
- #retention(sessionId, kind) {
39777
- if (kind === "execution") {
39778
- return {
39779
- isBusy: () => this.#activeCommandIds.has(sessionId) || this.#cancellationRequests.has(sessionId) || this.#preparing.has(sessionId) || (this.#sessionUseCounts.get(sessionId) ?? 0) > 0 || this.#isNativeSessionOperationActive(sessionId),
39780
- retire: (runtime) => this.#retireSessionProcess(sessionId, runtime, () => runtime.close())
39781
- };
39782
- }
40010
+ #retention(sessionId) {
39783
40011
  return {
39784
- isBusy: () => this.#nativeBrowserPreparing.has(sessionId) || (this.#nativeBrowserUseCounts.get(sessionId) ?? 0) > 0,
39785
- retire: (runtime) => this.#retireNativeBrowserProcess(sessionId, runtime, () => runtime.close())
40012
+ isBusy: () => this.#executions.isBusy(sessionId) || this.#sessions.isBusy(sessionId) || this.#isNativeSessionOperationActive(sessionId),
40013
+ retire: (runtime) => this.#retireSessionProcess(sessionId, runtime, () => runtime.close())
39786
40014
  };
39787
40015
  }
39788
40016
  async #retireSessionProcess(sessionId, runtime, close) {
39789
- const retained = this.#sessions.get(sessionId) === runtime;
39790
- if (retained) {
39791
- this.#sessions.delete(sessionId);
39792
- }
40017
+ const retained = this.#sessions.detach(sessionId, runtime);
39793
40018
  try {
39794
40019
  await this.#processes.retire(runtime, close);
39795
40020
  } finally {
@@ -39801,163 +40026,82 @@ var AcpSessionManager = class {
39801
40026
  }
39802
40027
  }
39803
40028
  }
39804
- async #retireNativeBrowserProcess(sessionId, runtime, close) {
39805
- const retained = this.#nativeBrowserSessions.get(sessionId) === runtime;
39806
- if (retained) {
39807
- this.#nativeBrowserSessions.delete(sessionId);
39808
- this.#nativeBrowserStore.remove(sessionId);
39809
- }
39810
- await this.#processes.retire(runtime, close);
39811
- }
39812
40029
  async #startRetainedSessionProcess(options, start) {
39813
- const runtime = await this.#processes.startRetained(
39814
- options.signal,
39815
- start,
39816
- this.#retention(options.sessionId, "execution")
39817
- );
39818
- this.#sessions.set(options.sessionId, runtime);
40030
+ const runtime = await this.#processes.startRetained(options.signal, start, this.#retention(options.sessionId));
40031
+ this.#sessions.retain(options.sessionId, runtime);
39819
40032
  return runtime;
39820
40033
  }
39821
- async #startRetainedNativeBrowserProcess(options, start) {
39822
- const runtime = await this.#processes.startRetained(
39823
- options.signal,
39824
- start,
39825
- this.#retention(options.sessionId, "browser")
39826
- );
39827
- this.#nativeBrowserSessions.set(options.sessionId, runtime);
39828
- return runtime;
39829
- }
39830
- async #startNativeBrowserPreparation(options) {
39831
- const operation = (async () => {
39832
- try {
39833
- return await this.#startRetainedNativeBrowserProcess(
39834
- options,
39835
- async () => await AcpProcessSession.startNativeSessionBrowser(
39836
- options,
39837
- () => this.#nativeBrowserObserver,
39838
- this.#nativeBrowserStore
39839
- )
39840
- );
39841
- } finally {
39842
- this.#nativeBrowserPreparing.delete(options.sessionId);
39843
- this.#processes.notifyChanged();
39844
- }
39845
- })();
39846
- this.#nativeBrowserPreparing.set(options.sessionId, operation);
39847
- return await operation;
39848
- }
39849
- async #replaceNativeBrowserPreparation(existing, options) {
39850
- const operation = (async () => {
39851
- try {
39852
- await this.#retireNativeBrowserProcess(options.sessionId, existing, async () => await existing.close());
39853
- return await this.#startRetainedNativeBrowserProcess(
39854
- options,
39855
- async () => await AcpProcessSession.startNativeSessionBrowser(
39856
- options,
39857
- () => this.#nativeBrowserObserver,
39858
- this.#nativeBrowserStore
39859
- )
39860
- );
39861
- } finally {
39862
- this.#nativeBrowserPreparing.delete(options.sessionId);
39863
- this.#processes.notifyChanged();
39864
- }
39865
- })();
39866
- this.#nativeBrowserPreparing.set(options.sessionId, operation);
39867
- return await operation;
39868
- }
39869
40034
  async #startPreparation(options, metrics = unmeasuredOperationMetrics) {
39870
- const operation = (async () => {
39871
- try {
39872
- return await this.#startRetainedSessionProcess(
39873
- options,
39874
- async () => await AcpProcessSession.start(
39875
- 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,
39876
- () => this.#observer,
39877
- this.#store,
39878
- metrics
39879
- )
39880
- );
39881
- } finally {
39882
- this.#preparing.delete(options.sessionId);
39883
- this.#processes.notifyChanged();
39884
- }
39885
- })();
39886
- this.#preparing.set(options.sessionId, operation);
39887
- return await operation;
40035
+ return await this.#sessions.prepare(options.sessionId, async () => {
40036
+ return await this.#startRetainedSessionProcess(
40037
+ options,
40038
+ async () => await AcpProcessSession.start(
40039
+ 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,
40040
+ () => this.#observer,
40041
+ this.#store,
40042
+ metrics
40043
+ )
40044
+ );
40045
+ });
39888
40046
  }
39889
40047
  async #replacePreparation(existing, options, metrics) {
39890
- const operation = (async () => {
39891
- try {
39892
- await this.#retireSessionProcess(
39893
- options.sessionId,
39894
- existing,
39895
- async () => await metrics.measure("runtime-replacement-close", async () => await existing.close())
39896
- );
39897
- this.#store.remove(options.sessionId);
39898
- return await this.#startRetainedSessionProcess(
39899
- options,
39900
- async () => await AcpProcessSession.start(options, () => this.#observer, this.#store, metrics)
39901
- );
39902
- } finally {
39903
- this.#preparing.delete(options.sessionId);
39904
- this.#processes.notifyChanged();
39905
- }
39906
- })();
39907
- this.#preparing.set(options.sessionId, operation);
39908
- return await operation;
40048
+ return await this.#sessions.prepare(options.sessionId, async () => {
40049
+ await this.#retireSessionProcess(
40050
+ options.sessionId,
40051
+ existing,
40052
+ async () => await metrics.measure("runtime-replacement-close", async () => await existing.close())
40053
+ );
40054
+ this.#store.remove(options.sessionId);
40055
+ return await this.#startRetainedSessionProcess(
40056
+ options,
40057
+ async () => await AcpProcessSession.start(options, () => this.#observer, this.#store, metrics)
40058
+ );
40059
+ });
39909
40060
  }
39910
40061
  async #replaceExecutionPreparation(existing, options) {
39911
40062
  const desiredConfigOptions = existing.prepared().configOptions;
39912
- const operation = (async () => {
40063
+ return await this.#sessions.prepare(options.sessionId, async () => {
40064
+ await this.#retireSessionProcess(options.sessionId, existing, async () => await existing.close());
40065
+ const runtime = await this.#startRetainedSessionProcess(
40066
+ options,
40067
+ async () => await AcpProcessSession.start(options, () => this.#observer, this.#store)
40068
+ );
39913
40069
  try {
39914
- await this.#retireSessionProcess(options.sessionId, existing, async () => await existing.close());
39915
- const runtime = await this.#startRetainedSessionProcess(
39916
- options,
39917
- async () => await AcpProcessSession.start(options, () => this.#observer, this.#store)
39918
- );
39919
- try {
39920
- for (const desired of desiredConfigOptions) {
39921
- const current = runtime.prepared().configOptions.find((option) => option.id === desired.id);
39922
- if (current === void 0 || current.type !== desired.type) {
39923
- continue;
39924
- }
39925
- if (desired.type === "boolean") {
39926
- if (current.booleanValue !== desired.booleanValue) {
39927
- await runtime.configure({
39928
- ...options,
39929
- booleanValue: desired.booleanValue,
39930
- configId: desired.id,
39931
- requestId: `execution-prepare-${desired.id}`,
39932
- value: "",
39933
- valueType: "boolean"
39934
- });
39935
- }
39936
- continue;
39937
- }
39938
- if (current.currentValue !== desired.currentValue && current.values.some((value) => value.value === desired.currentValue)) {
40070
+ for (const desired of desiredConfigOptions) {
40071
+ const current = runtime.prepared().configOptions.find((option) => option.id === desired.id);
40072
+ if (current === void 0 || current.type !== desired.type) {
40073
+ continue;
40074
+ }
40075
+ if (desired.type === "boolean") {
40076
+ if (current.booleanValue !== desired.booleanValue) {
39939
40077
  await runtime.configure({
39940
40078
  ...options,
39941
- booleanValue: false,
40079
+ booleanValue: desired.booleanValue,
39942
40080
  configId: desired.id,
39943
40081
  requestId: `execution-prepare-${desired.id}`,
39944
- value: desired.currentValue,
39945
- valueType: "select"
40082
+ value: "",
40083
+ valueType: "boolean"
39946
40084
  });
39947
40085
  }
40086
+ continue;
40087
+ }
40088
+ if (current.currentValue !== desired.currentValue && current.values.some((value) => value.value === desired.currentValue)) {
40089
+ await runtime.configure({
40090
+ ...options,
40091
+ booleanValue: false,
40092
+ configId: desired.id,
40093
+ requestId: `execution-prepare-${desired.id}`,
40094
+ value: desired.currentValue,
40095
+ valueType: "select"
40096
+ });
39948
40097
  }
39949
- } catch (error62) {
39950
- await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
39951
- throw error62;
39952
40098
  }
39953
- return runtime;
39954
- } finally {
39955
- this.#preparing.delete(options.sessionId);
39956
- this.#processes.notifyChanged();
40099
+ } catch (error62) {
40100
+ await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
40101
+ throw error62;
39957
40102
  }
39958
- })();
39959
- this.#preparing.set(options.sessionId, operation);
39960
- return await operation;
40103
+ return runtime;
40104
+ });
39961
40105
  }
39962
40106
  prepareSession = async (options, metrics = unmeasuredOperationMetrics) => {
39963
40107
  const stored = metrics.measureSync("session-state-read", () => this.#store.read(options.sessionId));
@@ -39971,11 +40115,11 @@ var AcpSessionManager = class {
39971
40115
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
39972
40116
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
39973
40117
  }
39974
- const pending = this.#preparing.get(options.sessionId);
40118
+ const pending = this.#sessions.pending(options.sessionId);
39975
40119
  if (pending !== void 0) {
39976
40120
  const runtime2 = await metrics.measure("session-preparation-wait", async () => await pending);
39977
40121
  if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
39978
- this.#touchExpectedSession(options.sessionId, runtime2);
40122
+ this.#sessions.touch(options.sessionId, runtime2);
39979
40123
  return runtime2.prepared();
39980
40124
  }
39981
40125
  return await this.prepareSession(options, metrics);
@@ -39983,7 +40127,7 @@ var AcpSessionManager = class {
39983
40127
  const existing = this.#sessions.get(options.sessionId);
39984
40128
  if (existing !== void 0) {
39985
40129
  if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
39986
- this.#touchExpectedSession(options.sessionId, existing);
40130
+ this.#sessions.touch(options.sessionId, existing);
39987
40131
  return existing.prepared();
39988
40132
  }
39989
40133
  if (!existing.canRebind()) {
@@ -40037,11 +40181,10 @@ var AcpSessionManager = class {
40037
40181
  * Use inspectAgentSettings for Agent-scoped settings discovery.
40038
40182
  */
40039
40183
  inspectSession = async (options, metrics = unmeasuredOperationMetrics) => {
40040
- if (this.#preparing.has(options.sessionId) || this.#sessions.has(options.sessionId)) {
40184
+ if (this.#sessions.pending(options.sessionId) !== void 0 || this.#sessions.get(options.sessionId) !== void 0) {
40041
40185
  throw new AcpExecutionError("The configuration inspection session is already in use", {}, "session_busy");
40042
40186
  }
40043
- this.#beginSessionUse(options.sessionId);
40044
- try {
40187
+ return await this.#sessions.use(options.sessionId, async () => {
40045
40188
  const runtime = await this.#startPreparation(options, metrics);
40046
40189
  try {
40047
40190
  return runtime.prepared();
@@ -40052,12 +40195,10 @@ var AcpSessionManager = class {
40052
40195
  this.#store.remove(options.sessionId);
40053
40196
  }
40054
40197
  }
40055
- } finally {
40056
- this.#endSessionUse(options.sessionId);
40057
- }
40198
+ });
40058
40199
  };
40059
40200
  prepareExecutionSession = async (options) => {
40060
- const pending = this.#preparing.get(options.sessionId);
40201
+ const pending = this.#sessions.pending(options.sessionId);
40061
40202
  if (pending !== void 0) {
40062
40203
  await pending;
40063
40204
  return await this.prepareExecutionSession(options);
@@ -40074,7 +40215,7 @@ var AcpSessionManager = class {
40074
40215
  );
40075
40216
  }
40076
40217
  if (existing.usesEnvironment(options.agent.environment)) {
40077
- this.#touchExpectedSession(options.sessionId, existing);
40218
+ this.#sessions.touch(options.sessionId, existing);
40078
40219
  return existing.prepared();
40079
40220
  }
40080
40221
  if (existing.hasRunningTurn()) {
@@ -40086,75 +40227,36 @@ var AcpSessionManager = class {
40086
40227
  }
40087
40228
  return (await this.#replaceExecutionPreparation(existing, options)).prepared();
40088
40229
  };
40089
- prepareNativeSessionBrowser = async (options) => {
40090
- const pending = this.#nativeBrowserPreparing.get(options.sessionId);
40091
- if (pending !== void 0) {
40092
- const runtime2 = await pending;
40093
- if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
40094
- this.#touchExpectedNativeBrowserSession(options.sessionId, runtime2);
40095
- return runtime2.prepared();
40096
- }
40097
- return await this.prepareNativeSessionBrowser(options);
40098
- }
40099
- const existing = this.#nativeBrowserSessions.get(options.sessionId);
40100
- if (existing !== void 0) {
40101
- if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
40102
- this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
40103
- return existing.prepared();
40104
- }
40105
- if (!existing.canRebind()) {
40106
- throw new AcpExecutionError(
40107
- "The agent or workspace cannot change after a conversation has started",
40108
- {},
40109
- "session_identity_mismatch"
40110
- );
40111
- }
40112
- const runtime2 = await this.#replaceNativeBrowserPreparation(existing, options);
40113
- return runtime2.prepared();
40114
- }
40115
- const runtime = await this.#startNativeBrowserPreparation(options);
40116
- return runtime.prepared();
40117
- };
40230
+ prepareNativeSessionBrowser = (options) => this.#browsers.prepare(options);
40118
40231
  configureSession = async (options) => {
40119
- this.#beginSessionUse(options.sessionId);
40120
- try {
40232
+ return await this.#sessions.use(options.sessionId, async () => {
40121
40233
  const runtime = await this.ensureSession(options);
40122
40234
  return await runtime.configure(options);
40123
- } finally {
40124
- this.#endSessionUse(options.sessionId);
40125
- }
40235
+ });
40126
40236
  };
40127
40237
  cancelPrompt = async (sessionId) => {
40128
- const existing = this.#cancellationRequests.get(sessionId);
40129
- if (existing !== void 0) {
40130
- await existing;
40131
- return;
40132
- }
40133
- const cancellation = (async () => {
40238
+ await this.#executions.cancel(sessionId, async () => {
40134
40239
  const runtime = this.#sessions.get(sessionId);
40135
40240
  if (runtime !== void 0) {
40136
40241
  try {
40137
40242
  await runtime.cancel();
40138
40243
  } finally {
40139
- if (this.#sessions.get(sessionId) === runtime) {
40140
- this.#sessions.delete(sessionId);
40244
+ if (this.#sessions.detach(sessionId, runtime)) {
40141
40245
  this.#processes.releaseClosed(runtime);
40142
40246
  }
40143
40247
  }
40144
40248
  }
40145
- })();
40146
- this.#cancellationRequests.set(sessionId, cancellation);
40147
- await cancellation;
40249
+ });
40148
40250
  };
40149
40251
  steerPrompt = async (options) => {
40150
- if (this.#activeCommandIds.get(options.sessionId) !== options.targetCommandId) {
40252
+ if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
40151
40253
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
40152
40254
  }
40153
- const pending = this.#preparing.get(options.sessionId);
40255
+ const pending = this.#sessions.pending(options.sessionId);
40154
40256
  if (pending !== void 0) {
40155
40257
  await pending;
40156
40258
  }
40157
- if (this.#activeCommandIds.get(options.sessionId) !== options.targetCommandId) {
40259
+ if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
40158
40260
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
40159
40261
  }
40160
40262
  const runtime = this.#sessions.get(options.sessionId);
@@ -40192,27 +40294,24 @@ var AcpSessionManager = class {
40192
40294
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
40193
40295
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
40194
40296
  }
40195
- if (this.#activeCommandIds.has(options.sessionId)) {
40196
- throw new AcpExecutionError("Another prompt is already running in this conversation", {}, "session_busy");
40197
- }
40198
- this.#activeCommandIds.set(options.sessionId, options.commandId);
40297
+ this.#executions.begin(options.sessionId, options.commandId);
40199
40298
  try {
40200
- if (this.#cancellationRequests.has(options.sessionId)) {
40299
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
40201
40300
  return { stopReason: "cancelled" };
40202
40301
  }
40203
40302
  const runtime = await this.ensureSession(options);
40204
40303
  if (this.#nativeOperations.has(`${options.agent.id}:${runtime.nativeSessionId()}`)) {
40205
40304
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
40206
40305
  }
40207
- if (this.#cancellationRequests.has(options.sessionId)) {
40306
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
40208
40307
  return { stopReason: "cancelled" };
40209
40308
  }
40210
40309
  try {
40211
40310
  const result = await runtime.prompt(options);
40212
- return this.#cancellationRequests.has(options.sessionId) ? { stopReason: "cancelled" } : result;
40311
+ return this.#executions.cancellation(options.sessionId) !== void 0 ? { stopReason: "cancelled" } : result;
40213
40312
  } catch (error62) {
40214
- if (this.#cancellationRequests.has(options.sessionId)) {
40215
- await this.#cancellationRequests.get(options.sessionId);
40313
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
40314
+ await this.#executions.cancellation(options.sessionId);
40216
40315
  return { stopReason: "cancelled" };
40217
40316
  }
40218
40317
  throw error62;
@@ -40226,7 +40325,7 @@ var AcpSessionManager = class {
40226
40325
  async () => await failedRuntime.close()
40227
40326
  );
40228
40327
  }
40229
- if (this.#cancellationRequests.has(options.sessionId)) {
40328
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
40230
40329
  return { stopReason: "cancelled" };
40231
40330
  }
40232
40331
  if (error62 instanceof AcpExecutionError) {
@@ -40237,13 +40336,10 @@ var AcpSessionManager = class {
40237
40336
  });
40238
40337
  } finally {
40239
40338
  try {
40240
- await this.#cancellationRequests.get(options.sessionId);
40339
+ await this.#executions.cancellation(options.sessionId);
40241
40340
  } finally {
40242
- if (this.#activeCommandIds.get(options.sessionId) === options.commandId) {
40243
- this.#activeCommandIds.delete(options.sessionId);
40244
- }
40245
- this.#cancellationRequests.delete(options.sessionId);
40246
- this.#touchSession(options.sessionId);
40341
+ this.#executions.finish(options.sessionId, options.commandId);
40342
+ this.#sessions.touch(options.sessionId);
40247
40343
  this.#processes.notifyChanged();
40248
40344
  }
40249
40345
  }
@@ -40264,14 +40360,11 @@ var AcpSessionManager = class {
40264
40360
  };
40265
40361
  close = async () => {
40266
40362
  const sessions = [...this.#sessions.entries()];
40267
- const nativeBrowserSessions = [...this.#nativeBrowserSessions.entries()];
40268
40363
  await Promise.all([
40269
40364
  ...sessions.map(
40270
40365
  async ([sessionId, runtime]) => await this.#retireSessionProcess(sessionId, runtime, async () => await runtime.close())
40271
40366
  ),
40272
- ...nativeBrowserSessions.map(
40273
- async ([sessionId, runtime]) => await this.#retireNativeBrowserProcess(sessionId, runtime, async () => await runtime.close())
40274
- )
40367
+ this.#browsers.close()
40275
40368
  ]);
40276
40369
  };
40277
40370
  findSessions = (sessionIds) => sessionIds.flatMap((sessionId) => {
@@ -40283,38 +40376,38 @@ var AcpSessionManager = class {
40283
40376
  createNativeSession = async (options) => {
40284
40377
  const temporarySessionId = `new-${crypto.randomUUID()}`;
40285
40378
  const temporaryOptions = { ...options, sessionId: temporarySessionId };
40286
- this.#beginSessionUse(temporarySessionId);
40287
- try {
40288
- const runtime = await this.#startPreparation(temporaryOptions);
40289
- let adopted = false;
40379
+ return await this.#sessions.use(temporarySessionId, async () => {
40290
40380
  try {
40291
- const nativeSessionId = runtime.nativeSessionId();
40292
- if (!isNativeChatSessionId(options.agent.id, nativeSessionId) || this.#sessions.has(nativeSessionId) || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
40293
- throw new AcpExecutionError(
40294
- "The Agent returned an invalid new Session ID",
40295
- {},
40296
- "invalid_native_session_response"
40297
- );
40381
+ const runtime = await this.#startPreparation(temporaryOptions);
40382
+ let adopted = false;
40383
+ try {
40384
+ const nativeSessionId = runtime.nativeSessionId();
40385
+ if (!isNativeChatSessionId(options.agent.id, nativeSessionId) || this.#sessions.get(nativeSessionId) !== void 0 || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
40386
+ throw new AcpExecutionError(
40387
+ "The Agent returned an invalid new Session ID",
40388
+ {},
40389
+ "invalid_native_session_response"
40390
+ );
40391
+ }
40392
+ runtime.adoptNativeSessionId(nativeSessionId);
40393
+ this.#sessions.detach(temporarySessionId, runtime);
40394
+ this.#sessions.retain(nativeSessionId, runtime);
40395
+ this.#processes.retain(runtime, this.#retention(nativeSessionId));
40396
+ adopted = true;
40397
+ return nativeSessionId;
40398
+ } finally {
40399
+ if (!adopted) {
40400
+ await this.#retireSessionProcess(
40401
+ temporarySessionId,
40402
+ runtime,
40403
+ async () => await runtime.closeProcess()
40404
+ );
40405
+ }
40298
40406
  }
40299
- runtime.adoptNativeSessionId(nativeSessionId);
40300
- this.#sessions.delete(temporarySessionId);
40301
- this.#sessions.set(nativeSessionId, runtime);
40302
- this.#processes.retain(runtime, this.#retention(nativeSessionId, "execution"));
40303
- adopted = true;
40304
- return nativeSessionId;
40305
40407
  } finally {
40306
- if (!adopted) {
40307
- await this.#retireSessionProcess(
40308
- temporarySessionId,
40309
- runtime,
40310
- async () => await runtime.closeProcess()
40311
- );
40312
- }
40408
+ this.#store.remove(temporarySessionId);
40313
40409
  }
40314
- } finally {
40315
- this.#store.remove(temporarySessionId);
40316
- this.#endSessionUse(temporarySessionId);
40317
- }
40410
+ });
40318
40411
  };
40319
40412
  ensureSession = async (options) => {
40320
40413
  const existing = this.#sessions.get(options.sessionId);
@@ -40326,7 +40419,7 @@ var AcpSessionManager = class {
40326
40419
  "session_identity_mismatch"
40327
40420
  );
40328
40421
  }
40329
- this.#touchExpectedSession(options.sessionId, existing);
40422
+ this.#sessions.touch(options.sessionId, existing);
40330
40423
  return existing;
40331
40424
  }
40332
40425
  await this.prepareSession(options);
@@ -40336,38 +40429,18 @@ var AcpSessionManager = class {
40336
40429
  }
40337
40430
  return started;
40338
40431
  };
40339
- async #ensureNativeSessionBrowser(options) {
40340
- const existing = this.#nativeBrowserSessions.get(options.sessionId);
40341
- if (existing !== void 0) {
40342
- if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
40343
- throw new AcpExecutionError(
40344
- "The agent or workspace cannot change after a conversation has started",
40345
- {},
40346
- "session_identity_mismatch"
40347
- );
40348
- }
40349
- this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
40350
- return existing;
40351
- }
40352
- await this.prepareNativeSessionBrowser(options);
40353
- const started = this.#nativeBrowserSessions.get(options.sessionId);
40354
- if (started === void 0) {
40355
- throw new AcpExecutionError("ACP native session browser did not start");
40356
- }
40357
- return started;
40358
- }
40359
40432
  listNativeSessions = async (options, cursor) => {
40360
- this.#beginNativeBrowserUse(options.sessionId);
40361
- try {
40362
- return await (await this.#ensureNativeSessionBrowser(options)).listNativeSessions(cursor, options.signal);
40363
- } finally {
40364
- this.#endNativeBrowserUse(options.sessionId);
40365
- }
40433
+ return await this.#browsers.use(
40434
+ options,
40435
+ async (runtime) => await runtime.listNativeSessions(cursor, options.signal)
40436
+ );
40366
40437
  };
40367
40438
  previewNativeSession = async (options, nativeSessionId) => {
40368
40439
  const key = `${options.agent.id}:${nativeSessionId}`;
40369
40440
  const linked = this.#store.findNative(options.agent.id, nativeSessionId);
40370
- if (this.#nativeOperations.has(key) || linked.some((entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId))) {
40441
+ if (this.#nativeOperations.has(key) || linked.some(
40442
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
40443
+ )) {
40371
40444
  throw new AcpExecutionError(
40372
40445
  "Wait for the native session's running operation to finish",
40373
40446
  {},
@@ -40376,26 +40449,13 @@ var AcpSessionManager = class {
40376
40449
  }
40377
40450
  this.#nativeOperations.add(key);
40378
40451
  try {
40379
- return await this.#processes.runTransient(
40380
- options.signal,
40381
- async () => await AcpProcessSession.startNativeSessionBrowser(
40382
- options,
40383
- () => this.#nativeBrowserObserver,
40384
- new MemoryAcpSessionStore()
40385
- ),
40386
- async (runtime) => {
40387
- runtime.selectNativeSession(nativeSessionId);
40388
- return await runtime.loadHistory(options.signal);
40389
- },
40390
- async (runtime) => await runtime.closeProcess()
40391
- );
40452
+ return await this.#browsers.preview(options, nativeSessionId);
40392
40453
  } finally {
40393
40454
  this.#nativeOperations.delete(key);
40394
40455
  }
40395
40456
  };
40396
40457
  loadSessionHistory = async (options) => {
40397
- this.#beginSessionUse(options.sessionId);
40398
- try {
40458
+ return await this.#sessions.use(options.sessionId, async () => {
40399
40459
  const runtime = await this.ensureSession(options);
40400
40460
  const stored = this.#store.read(options.sessionId);
40401
40461
  if (stored instanceof FoundAcpStoredSession && this.#nativeHistoryReader !== null) {
@@ -40409,9 +40469,7 @@ var AcpSessionManager = class {
40409
40469
  }
40410
40470
  }
40411
40471
  return await runtime.loadHistory(options.signal);
40412
- } finally {
40413
- this.#endSessionUse(options.sessionId);
40414
- }
40472
+ });
40415
40473
  };
40416
40474
  openNativeSession = async (options, origin) => {
40417
40475
  const key = `${options.agent.id}:${origin.nativeSessionId}`;
@@ -40419,7 +40477,9 @@ var AcpSessionManager = class {
40419
40477
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
40420
40478
  }
40421
40479
  const linked = this.#store.findNative(options.agent.id, origin.nativeSessionId);
40422
- if (linked.some((entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId))) {
40480
+ if (linked.some(
40481
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
40482
+ )) {
40423
40483
  throw new AcpExecutionError(
40424
40484
  "Wait for the native session's running operation to finish",
40425
40485
  {},
@@ -40428,13 +40488,12 @@ var AcpSessionManager = class {
40428
40488
  }
40429
40489
  const existing = linked[0];
40430
40490
  if (origin.kind === "import" && existing !== void 0) {
40431
- this.#beginSessionUse(existing.sessionId);
40432
- try {
40491
+ return await this.#sessions.use(existing.sessionId, async () => {
40433
40492
  const runtime = await this.ensureSession({
40434
40493
  ...options,
40435
40494
  sessionId: existing.sessionId
40436
40495
  });
40437
- if (this.#activeCommandIds.has(existing.sessionId) || this.#nativeOperations.has(key)) {
40496
+ if (this.#executions.command(existing.sessionId) !== void 0 || this.#nativeOperations.has(key)) {
40438
40497
  throw new AcpExecutionError(
40439
40498
  "Wait for the native session's running operation to finish",
40440
40499
  {},
@@ -40451,48 +40510,48 @@ var AcpSessionManager = class {
40451
40510
  this.#nativeOperations.delete(key);
40452
40511
  this.#processes.notifyChanged();
40453
40512
  }
40454
- } finally {
40455
- this.#endSessionUse(existing.sessionId);
40456
- }
40513
+ });
40457
40514
  }
40458
- 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) {
40515
+ 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) {
40459
40516
  throw new AcpExecutionError("The destination conversation already exists", {}, "session_identity_mismatch");
40460
40517
  }
40461
- this.#beginSessionUse(options.sessionId);
40462
- this.#nativeOperations.add(key);
40463
- try {
40464
- const runtime = await this.#startPreparation({ ...options, origin });
40465
- const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
40466
- this.#nativeOperations.add(openedKey);
40518
+ return await this.#sessions.use(options.sessionId, async () => {
40519
+ this.#nativeOperations.add(key);
40467
40520
  try {
40468
- return {
40469
- sessionId: options.sessionId,
40470
- prepared: runtime.prepared()
40471
- };
40521
+ const runtime = await this.#startPreparation({ ...options, origin });
40522
+ const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
40523
+ this.#nativeOperations.add(openedKey);
40524
+ try {
40525
+ return {
40526
+ sessionId: options.sessionId,
40527
+ prepared: runtime.prepared()
40528
+ };
40529
+ } finally {
40530
+ this.#nativeOperations.delete(openedKey);
40531
+ this.#processes.notifyChanged();
40532
+ }
40533
+ } catch (error62) {
40534
+ this.#store.remove(options.sessionId);
40535
+ const runtime = this.#sessions.get(options.sessionId);
40536
+ if (runtime !== void 0) {
40537
+ await this.#retireSessionProcess(
40538
+ options.sessionId,
40539
+ runtime,
40540
+ async () => await runtime.closeProcess()
40541
+ );
40542
+ }
40543
+ throw error62;
40472
40544
  } finally {
40473
- this.#nativeOperations.delete(openedKey);
40474
- this.#processes.notifyChanged();
40475
- }
40476
- } catch (error62) {
40477
- this.#store.remove(options.sessionId);
40478
- const runtime = this.#sessions.get(options.sessionId);
40479
- if (runtime !== void 0) {
40480
- await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.closeProcess());
40545
+ this.#nativeOperations.delete(key);
40481
40546
  }
40482
- throw error62;
40483
- } finally {
40484
- this.#nativeOperations.delete(key);
40485
- this.#endSessionUse(options.sessionId);
40486
- }
40547
+ });
40487
40548
  };
40488
40549
  deleteNativeSession = async (options, nativeSessionId) => {
40489
- this.#beginNativeBrowserUse(options.sessionId);
40490
- try {
40491
- const runtime = await this.#ensureNativeSessionBrowser(options);
40550
+ return await this.#browsers.use(options, async (runtime) => {
40492
40551
  const key = `${options.agent.id}:${nativeSessionId}`;
40493
40552
  const linked = this.#store.findNative(options.agent.id, nativeSessionId);
40494
40553
  if (this.#nativeOperations.has(key) || linked.some(
40495
- (entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId)
40554
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
40496
40555
  )) {
40497
40556
  throw new AcpExecutionError(
40498
40557
  "Wait for the native session's running operation to finish",
@@ -40512,20 +40571,14 @@ var AcpSessionManager = class {
40512
40571
  await runtime.deleteNativeSession(nativeSessionId, options.signal);
40513
40572
  const deleted = this.#store.markDeleted(options.agent.id, nativeSessionId);
40514
40573
  if (runtime.nativeSessionId() === nativeSessionId) {
40515
- await this.#retireNativeBrowserProcess(
40516
- options.sessionId,
40517
- runtime,
40518
- async () => await runtime.close()
40519
- );
40574
+ await this.#browsers.retire(options.sessionId, runtime);
40520
40575
  }
40521
40576
  return deleted;
40522
40577
  } finally {
40523
40578
  this.#nativeOperations.delete(key);
40524
40579
  this.#processes.notifyChanged();
40525
40580
  }
40526
- } finally {
40527
- this.#endNativeBrowserUse(options.sessionId);
40528
- }
40581
+ });
40529
40582
  };
40530
40583
  };
40531
40584