@moxt-ai/mobius 0.0.125 → 0.0.126

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/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.126",
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,191 @@ 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/session-executions.ts
39587
+ var unreachable = (state) => {
39588
+ throw new AcpExecutionError("Unexpected execution state", { cause: state });
39589
+ };
39590
+ var SessionExecutions = class {
39591
+ #entries = /* @__PURE__ */ new Map();
39592
+ command = (id) => {
39593
+ const state = this.#entries.get(id);
39594
+ switch (state?.kind) {
39595
+ case "running":
39596
+ case "cancelling":
39597
+ return state.commandId;
39598
+ case "cancelled-before-start":
39599
+ case void 0:
39600
+ return void 0;
39601
+ default:
39602
+ return unreachable(state);
39603
+ }
39604
+ };
39605
+ cancellation = (id) => {
39606
+ const state = this.#entries.get(id);
39607
+ switch (state?.kind) {
39608
+ case "cancelling":
39609
+ case "cancelled-before-start":
39610
+ return state.completion;
39611
+ case "running":
39612
+ case void 0:
39613
+ return void 0;
39614
+ default:
39615
+ return unreachable(state);
39616
+ }
39617
+ };
39618
+ isBusy = (id) => this.#entries.has(id);
39619
+ begin = (id, commandId) => {
39620
+ const state = this.#entries.get(id);
39621
+ switch (state?.kind) {
39622
+ case "running":
39623
+ case "cancelling":
39624
+ throw new AcpExecutionError(
39625
+ "Another prompt is already running in this conversation",
39626
+ {},
39627
+ "session_busy"
39628
+ );
39629
+ case "cancelled-before-start":
39630
+ case void 0:
39631
+ break;
39632
+ default:
39633
+ unreachable(state);
39634
+ }
39635
+ this.#entries.set(
39636
+ id,
39637
+ state?.kind === "cancelled-before-start" ? { kind: "cancelling", commandId, completion: state.completion } : { kind: "running", commandId }
39638
+ );
39639
+ };
39640
+ cancel = (id, stop) => {
39641
+ const state = this.#entries.get(id);
39642
+ switch (state?.kind) {
39643
+ case "cancelling":
39644
+ case "cancelled-before-start":
39645
+ return state.completion;
39646
+ case "running":
39647
+ case void 0:
39648
+ break;
39649
+ default:
39650
+ return unreachable(state);
39651
+ }
39652
+ const completion = Promise.withResolvers();
39653
+ this.#entries.set(
39654
+ id,
39655
+ state?.kind === "running" ? { kind: "cancelling", commandId: state.commandId, completion: completion.promise } : { kind: "cancelled-before-start", completion: completion.promise }
39656
+ );
39657
+ void (async () => await stop())().then(completion.resolve, completion.reject);
39658
+ return completion.promise;
39659
+ };
39660
+ finish = (id, commandId) => {
39661
+ if (this.command(id) === commandId) {
39662
+ this.#entries.delete(id);
39663
+ }
39664
+ };
39665
+ };
39666
+
39482
39667
  // src/native-sessions/chat-agent.ts
39483
39668
  function supportsNativeChat(agentId) {
39484
39669
  return agentId === "codex" || agentId === "claude" || agentId === "opencode" || agentId === "pi";
@@ -39690,18 +39875,16 @@ var AcpProcessPool = class {
39690
39875
  var SESSION_CONTROL_TIMEOUT_MILLISECONDS = 3e4;
39691
39876
  var AcpSessionManager = class {
39692
39877
  #processes = new AcpProcessPool();
39693
- #activeCommandIds = /* @__PURE__ */ new Map();
39878
+ #sessions = new RetainedSessions({ touch: this.#processes.touch, changed: this.#processes.notifyChanged });
39879
+ #nativeBrowserSessions = new RetainedSessions({
39880
+ touch: this.#processes.touch,
39881
+ changed: this.#processes.notifyChanged
39882
+ });
39883
+ #executions = new SessionExecutions();
39694
39884
  #agentSettingsObserver = new NullAcpSessionObserver();
39695
39885
  #nativeOperations = /* @__PURE__ */ new Set();
39696
39886
  #nativeBrowserObserver = new NullAcpSessionObserver();
39697
- #nativeBrowserPreparing = /* @__PURE__ */ new Map();
39698
- #nativeBrowserSessions = /* @__PURE__ */ new Map();
39699
39887
  #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
39888
  #store;
39706
39889
  #nativeHistoryReader;
39707
39890
  #sessionIdentity;
@@ -39715,60 +39898,6 @@ var AcpSessionManager = class {
39715
39898
  setObserver = (observer) => {
39716
39899
  this.#observer = observer;
39717
39900
  };
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
39901
  #isNativeSessionOperationActive(sessionId) {
39773
39902
  const stored = this.#store.read(sessionId);
39774
39903
  return stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`);
@@ -39776,20 +39905,17 @@ var AcpSessionManager = class {
39776
39905
  #retention(sessionId, kind) {
39777
39906
  if (kind === "execution") {
39778
39907
  return {
39779
- isBusy: () => this.#activeCommandIds.has(sessionId) || this.#cancellationRequests.has(sessionId) || this.#preparing.has(sessionId) || (this.#sessionUseCounts.get(sessionId) ?? 0) > 0 || this.#isNativeSessionOperationActive(sessionId),
39908
+ isBusy: () => this.#executions.isBusy(sessionId) || this.#sessions.isBusy(sessionId) || this.#isNativeSessionOperationActive(sessionId),
39780
39909
  retire: (runtime) => this.#retireSessionProcess(sessionId, runtime, () => runtime.close())
39781
39910
  };
39782
39911
  }
39783
39912
  return {
39784
- isBusy: () => this.#nativeBrowserPreparing.has(sessionId) || (this.#nativeBrowserUseCounts.get(sessionId) ?? 0) > 0,
39913
+ isBusy: () => this.#nativeBrowserSessions.isBusy(sessionId),
39785
39914
  retire: (runtime) => this.#retireNativeBrowserProcess(sessionId, runtime, () => runtime.close())
39786
39915
  };
39787
39916
  }
39788
39917
  async #retireSessionProcess(sessionId, runtime, close) {
39789
- const retained = this.#sessions.get(sessionId) === runtime;
39790
- if (retained) {
39791
- this.#sessions.delete(sessionId);
39792
- }
39918
+ const retained = this.#sessions.detach(sessionId, runtime);
39793
39919
  try {
39794
39920
  await this.#processes.retire(runtime, close);
39795
39921
  } finally {
@@ -39802,9 +39928,8 @@ var AcpSessionManager = class {
39802
39928
  }
39803
39929
  }
39804
39930
  async #retireNativeBrowserProcess(sessionId, runtime, close) {
39805
- const retained = this.#nativeBrowserSessions.get(sessionId) === runtime;
39931
+ const retained = this.#nativeBrowserSessions.detach(sessionId, runtime);
39806
39932
  if (retained) {
39807
- this.#nativeBrowserSessions.delete(sessionId);
39808
39933
  this.#nativeBrowserStore.remove(sessionId);
39809
39934
  }
39810
39935
  await this.#processes.retire(runtime, close);
@@ -39815,7 +39940,7 @@ var AcpSessionManager = class {
39815
39940
  start,
39816
39941
  this.#retention(options.sessionId, "execution")
39817
39942
  );
39818
- this.#sessions.set(options.sessionId, runtime);
39943
+ this.#sessions.retain(options.sessionId, runtime);
39819
39944
  return runtime;
39820
39945
  }
39821
39946
  async #startRetainedNativeBrowserProcess(options, start) {
@@ -39824,140 +39949,105 @@ var AcpSessionManager = class {
39824
39949
  start,
39825
39950
  this.#retention(options.sessionId, "browser")
39826
39951
  );
39827
- this.#nativeBrowserSessions.set(options.sessionId, runtime);
39952
+ this.#nativeBrowserSessions.retain(options.sessionId, runtime);
39828
39953
  return runtime;
39829
39954
  }
39830
39955
  async #startNativeBrowserPreparation(options) {
39831
- const operation = (async () => {
39832
- try {
39833
- return await this.#startRetainedNativeBrowserProcess(
39956
+ return await this.#nativeBrowserSessions.prepare(options.sessionId, async () => {
39957
+ return await this.#startRetainedNativeBrowserProcess(
39958
+ options,
39959
+ async () => await AcpProcessSession.startNativeSessionBrowser(
39834
39960
  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;
39961
+ () => this.#nativeBrowserObserver,
39962
+ this.#nativeBrowserStore
39963
+ )
39964
+ );
39965
+ });
39848
39966
  }
39849
39967
  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(
39968
+ return await this.#nativeBrowserSessions.prepare(options.sessionId, async () => {
39969
+ await this.#retireNativeBrowserProcess(options.sessionId, existing, async () => await existing.close());
39970
+ return await this.#startRetainedNativeBrowserProcess(
39971
+ options,
39972
+ async () => await AcpProcessSession.startNativeSessionBrowser(
39854
39973
  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;
39974
+ () => this.#nativeBrowserObserver,
39975
+ this.#nativeBrowserStore
39976
+ )
39977
+ );
39978
+ });
39868
39979
  }
39869
39980
  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;
39981
+ return await this.#sessions.prepare(options.sessionId, async () => {
39982
+ return await this.#startRetainedSessionProcess(
39983
+ options,
39984
+ async () => await AcpProcessSession.start(
39985
+ 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,
39986
+ () => this.#observer,
39987
+ this.#store,
39988
+ metrics
39989
+ )
39990
+ );
39991
+ });
39888
39992
  }
39889
39993
  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;
39994
+ return await this.#sessions.prepare(options.sessionId, async () => {
39995
+ await this.#retireSessionProcess(
39996
+ options.sessionId,
39997
+ existing,
39998
+ async () => await metrics.measure("runtime-replacement-close", async () => await existing.close())
39999
+ );
40000
+ this.#store.remove(options.sessionId);
40001
+ return await this.#startRetainedSessionProcess(
40002
+ options,
40003
+ async () => await AcpProcessSession.start(options, () => this.#observer, this.#store, metrics)
40004
+ );
40005
+ });
39909
40006
  }
39910
40007
  async #replaceExecutionPreparation(existing, options) {
39911
40008
  const desiredConfigOptions = existing.prepared().configOptions;
39912
- const operation = (async () => {
40009
+ return await this.#sessions.prepare(options.sessionId, async () => {
40010
+ await this.#retireSessionProcess(options.sessionId, existing, async () => await existing.close());
40011
+ const runtime = await this.#startRetainedSessionProcess(
40012
+ options,
40013
+ async () => await AcpProcessSession.start(options, () => this.#observer, this.#store)
40014
+ );
39913
40015
  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)) {
40016
+ for (const desired of desiredConfigOptions) {
40017
+ const current = runtime.prepared().configOptions.find((option) => option.id === desired.id);
40018
+ if (current === void 0 || current.type !== desired.type) {
40019
+ continue;
40020
+ }
40021
+ if (desired.type === "boolean") {
40022
+ if (current.booleanValue !== desired.booleanValue) {
39939
40023
  await runtime.configure({
39940
40024
  ...options,
39941
- booleanValue: false,
40025
+ booleanValue: desired.booleanValue,
39942
40026
  configId: desired.id,
39943
40027
  requestId: `execution-prepare-${desired.id}`,
39944
- value: desired.currentValue,
39945
- valueType: "select"
40028
+ value: "",
40029
+ valueType: "boolean"
39946
40030
  });
39947
40031
  }
40032
+ continue;
40033
+ }
40034
+ if (current.currentValue !== desired.currentValue && current.values.some((value) => value.value === desired.currentValue)) {
40035
+ await runtime.configure({
40036
+ ...options,
40037
+ booleanValue: false,
40038
+ configId: desired.id,
40039
+ requestId: `execution-prepare-${desired.id}`,
40040
+ value: desired.currentValue,
40041
+ valueType: "select"
40042
+ });
39948
40043
  }
39949
- } catch (error62) {
39950
- await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
39951
- throw error62;
39952
40044
  }
39953
- return runtime;
39954
- } finally {
39955
- this.#preparing.delete(options.sessionId);
39956
- this.#processes.notifyChanged();
40045
+ } catch (error62) {
40046
+ await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
40047
+ throw error62;
39957
40048
  }
39958
- })();
39959
- this.#preparing.set(options.sessionId, operation);
39960
- return await operation;
40049
+ return runtime;
40050
+ });
39961
40051
  }
39962
40052
  prepareSession = async (options, metrics = unmeasuredOperationMetrics) => {
39963
40053
  const stored = metrics.measureSync("session-state-read", () => this.#store.read(options.sessionId));
@@ -39971,11 +40061,11 @@ var AcpSessionManager = class {
39971
40061
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
39972
40062
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
39973
40063
  }
39974
- const pending = this.#preparing.get(options.sessionId);
40064
+ const pending = this.#sessions.pending(options.sessionId);
39975
40065
  if (pending !== void 0) {
39976
40066
  const runtime2 = await metrics.measure("session-preparation-wait", async () => await pending);
39977
40067
  if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
39978
- this.#touchExpectedSession(options.sessionId, runtime2);
40068
+ this.#sessions.touch(options.sessionId, runtime2);
39979
40069
  return runtime2.prepared();
39980
40070
  }
39981
40071
  return await this.prepareSession(options, metrics);
@@ -39983,7 +40073,7 @@ var AcpSessionManager = class {
39983
40073
  const existing = this.#sessions.get(options.sessionId);
39984
40074
  if (existing !== void 0) {
39985
40075
  if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
39986
- this.#touchExpectedSession(options.sessionId, existing);
40076
+ this.#sessions.touch(options.sessionId, existing);
39987
40077
  return existing.prepared();
39988
40078
  }
39989
40079
  if (!existing.canRebind()) {
@@ -40037,11 +40127,10 @@ var AcpSessionManager = class {
40037
40127
  * Use inspectAgentSettings for Agent-scoped settings discovery.
40038
40128
  */
40039
40129
  inspectSession = async (options, metrics = unmeasuredOperationMetrics) => {
40040
- if (this.#preparing.has(options.sessionId) || this.#sessions.has(options.sessionId)) {
40130
+ if (this.#sessions.pending(options.sessionId) !== void 0 || this.#sessions.get(options.sessionId) !== void 0) {
40041
40131
  throw new AcpExecutionError("The configuration inspection session is already in use", {}, "session_busy");
40042
40132
  }
40043
- this.#beginSessionUse(options.sessionId);
40044
- try {
40133
+ return await this.#sessions.use(options.sessionId, async () => {
40045
40134
  const runtime = await this.#startPreparation(options, metrics);
40046
40135
  try {
40047
40136
  return runtime.prepared();
@@ -40052,12 +40141,10 @@ var AcpSessionManager = class {
40052
40141
  this.#store.remove(options.sessionId);
40053
40142
  }
40054
40143
  }
40055
- } finally {
40056
- this.#endSessionUse(options.sessionId);
40057
- }
40144
+ });
40058
40145
  };
40059
40146
  prepareExecutionSession = async (options) => {
40060
- const pending = this.#preparing.get(options.sessionId);
40147
+ const pending = this.#sessions.pending(options.sessionId);
40061
40148
  if (pending !== void 0) {
40062
40149
  await pending;
40063
40150
  return await this.prepareExecutionSession(options);
@@ -40074,7 +40161,7 @@ var AcpSessionManager = class {
40074
40161
  );
40075
40162
  }
40076
40163
  if (existing.usesEnvironment(options.agent.environment)) {
40077
- this.#touchExpectedSession(options.sessionId, existing);
40164
+ this.#sessions.touch(options.sessionId, existing);
40078
40165
  return existing.prepared();
40079
40166
  }
40080
40167
  if (existing.hasRunningTurn()) {
@@ -40087,11 +40174,11 @@ var AcpSessionManager = class {
40087
40174
  return (await this.#replaceExecutionPreparation(existing, options)).prepared();
40088
40175
  };
40089
40176
  prepareNativeSessionBrowser = async (options) => {
40090
- const pending = this.#nativeBrowserPreparing.get(options.sessionId);
40177
+ const pending = this.#nativeBrowserSessions.pending(options.sessionId);
40091
40178
  if (pending !== void 0) {
40092
40179
  const runtime2 = await pending;
40093
40180
  if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
40094
- this.#touchExpectedNativeBrowserSession(options.sessionId, runtime2);
40181
+ this.#nativeBrowserSessions.touch(options.sessionId, runtime2);
40095
40182
  return runtime2.prepared();
40096
40183
  }
40097
40184
  return await this.prepareNativeSessionBrowser(options);
@@ -40099,7 +40186,7 @@ var AcpSessionManager = class {
40099
40186
  const existing = this.#nativeBrowserSessions.get(options.sessionId);
40100
40187
  if (existing !== void 0) {
40101
40188
  if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
40102
- this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
40189
+ this.#nativeBrowserSessions.touch(options.sessionId, existing);
40103
40190
  return existing.prepared();
40104
40191
  }
40105
40192
  if (!existing.canRebind()) {
@@ -40116,45 +40203,34 @@ var AcpSessionManager = class {
40116
40203
  return runtime.prepared();
40117
40204
  };
40118
40205
  configureSession = async (options) => {
40119
- this.#beginSessionUse(options.sessionId);
40120
- try {
40206
+ return await this.#sessions.use(options.sessionId, async () => {
40121
40207
  const runtime = await this.ensureSession(options);
40122
40208
  return await runtime.configure(options);
40123
- } finally {
40124
- this.#endSessionUse(options.sessionId);
40125
- }
40209
+ });
40126
40210
  };
40127
40211
  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 () => {
40212
+ await this.#executions.cancel(sessionId, async () => {
40134
40213
  const runtime = this.#sessions.get(sessionId);
40135
40214
  if (runtime !== void 0) {
40136
40215
  try {
40137
40216
  await runtime.cancel();
40138
40217
  } finally {
40139
- if (this.#sessions.get(sessionId) === runtime) {
40140
- this.#sessions.delete(sessionId);
40218
+ if (this.#sessions.detach(sessionId, runtime)) {
40141
40219
  this.#processes.releaseClosed(runtime);
40142
40220
  }
40143
40221
  }
40144
40222
  }
40145
- })();
40146
- this.#cancellationRequests.set(sessionId, cancellation);
40147
- await cancellation;
40223
+ });
40148
40224
  };
40149
40225
  steerPrompt = async (options) => {
40150
- if (this.#activeCommandIds.get(options.sessionId) !== options.targetCommandId) {
40226
+ if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
40151
40227
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
40152
40228
  }
40153
- const pending = this.#preparing.get(options.sessionId);
40229
+ const pending = this.#sessions.pending(options.sessionId);
40154
40230
  if (pending !== void 0) {
40155
40231
  await pending;
40156
40232
  }
40157
- if (this.#activeCommandIds.get(options.sessionId) !== options.targetCommandId) {
40233
+ if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
40158
40234
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
40159
40235
  }
40160
40236
  const runtime = this.#sessions.get(options.sessionId);
@@ -40192,27 +40268,24 @@ var AcpSessionManager = class {
40192
40268
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
40193
40269
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
40194
40270
  }
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);
40271
+ this.#executions.begin(options.sessionId, options.commandId);
40199
40272
  try {
40200
- if (this.#cancellationRequests.has(options.sessionId)) {
40273
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
40201
40274
  return { stopReason: "cancelled" };
40202
40275
  }
40203
40276
  const runtime = await this.ensureSession(options);
40204
40277
  if (this.#nativeOperations.has(`${options.agent.id}:${runtime.nativeSessionId()}`)) {
40205
40278
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
40206
40279
  }
40207
- if (this.#cancellationRequests.has(options.sessionId)) {
40280
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
40208
40281
  return { stopReason: "cancelled" };
40209
40282
  }
40210
40283
  try {
40211
40284
  const result = await runtime.prompt(options);
40212
- return this.#cancellationRequests.has(options.sessionId) ? { stopReason: "cancelled" } : result;
40285
+ return this.#executions.cancellation(options.sessionId) !== void 0 ? { stopReason: "cancelled" } : result;
40213
40286
  } catch (error62) {
40214
- if (this.#cancellationRequests.has(options.sessionId)) {
40215
- await this.#cancellationRequests.get(options.sessionId);
40287
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
40288
+ await this.#executions.cancellation(options.sessionId);
40216
40289
  return { stopReason: "cancelled" };
40217
40290
  }
40218
40291
  throw error62;
@@ -40226,7 +40299,7 @@ var AcpSessionManager = class {
40226
40299
  async () => await failedRuntime.close()
40227
40300
  );
40228
40301
  }
40229
- if (this.#cancellationRequests.has(options.sessionId)) {
40302
+ if (this.#executions.cancellation(options.sessionId) !== void 0) {
40230
40303
  return { stopReason: "cancelled" };
40231
40304
  }
40232
40305
  if (error62 instanceof AcpExecutionError) {
@@ -40237,13 +40310,10 @@ var AcpSessionManager = class {
40237
40310
  });
40238
40311
  } finally {
40239
40312
  try {
40240
- await this.#cancellationRequests.get(options.sessionId);
40313
+ await this.#executions.cancellation(options.sessionId);
40241
40314
  } 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);
40315
+ this.#executions.finish(options.sessionId, options.commandId);
40316
+ this.#sessions.touch(options.sessionId);
40247
40317
  this.#processes.notifyChanged();
40248
40318
  }
40249
40319
  }
@@ -40283,38 +40353,38 @@ var AcpSessionManager = class {
40283
40353
  createNativeSession = async (options) => {
40284
40354
  const temporarySessionId = `new-${crypto.randomUUID()}`;
40285
40355
  const temporaryOptions = { ...options, sessionId: temporarySessionId };
40286
- this.#beginSessionUse(temporarySessionId);
40287
- try {
40288
- const runtime = await this.#startPreparation(temporaryOptions);
40289
- let adopted = false;
40356
+ return await this.#sessions.use(temporarySessionId, async () => {
40290
40357
  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
- );
40358
+ const runtime = await this.#startPreparation(temporaryOptions);
40359
+ let adopted = false;
40360
+ try {
40361
+ const nativeSessionId = runtime.nativeSessionId();
40362
+ if (!isNativeChatSessionId(options.agent.id, nativeSessionId) || this.#sessions.get(nativeSessionId) !== void 0 || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
40363
+ throw new AcpExecutionError(
40364
+ "The Agent returned an invalid new Session ID",
40365
+ {},
40366
+ "invalid_native_session_response"
40367
+ );
40368
+ }
40369
+ runtime.adoptNativeSessionId(nativeSessionId);
40370
+ this.#sessions.detach(temporarySessionId, runtime);
40371
+ this.#sessions.retain(nativeSessionId, runtime);
40372
+ this.#processes.retain(runtime, this.#retention(nativeSessionId, "execution"));
40373
+ adopted = true;
40374
+ return nativeSessionId;
40375
+ } finally {
40376
+ if (!adopted) {
40377
+ await this.#retireSessionProcess(
40378
+ temporarySessionId,
40379
+ runtime,
40380
+ async () => await runtime.closeProcess()
40381
+ );
40382
+ }
40298
40383
  }
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
40384
  } finally {
40306
- if (!adopted) {
40307
- await this.#retireSessionProcess(
40308
- temporarySessionId,
40309
- runtime,
40310
- async () => await runtime.closeProcess()
40311
- );
40312
- }
40385
+ this.#store.remove(temporarySessionId);
40313
40386
  }
40314
- } finally {
40315
- this.#store.remove(temporarySessionId);
40316
- this.#endSessionUse(temporarySessionId);
40317
- }
40387
+ });
40318
40388
  };
40319
40389
  ensureSession = async (options) => {
40320
40390
  const existing = this.#sessions.get(options.sessionId);
@@ -40326,7 +40396,7 @@ var AcpSessionManager = class {
40326
40396
  "session_identity_mismatch"
40327
40397
  );
40328
40398
  }
40329
- this.#touchExpectedSession(options.sessionId, existing);
40399
+ this.#sessions.touch(options.sessionId, existing);
40330
40400
  return existing;
40331
40401
  }
40332
40402
  await this.prepareSession(options);
@@ -40346,7 +40416,7 @@ var AcpSessionManager = class {
40346
40416
  "session_identity_mismatch"
40347
40417
  );
40348
40418
  }
40349
- this.#touchExpectedNativeBrowserSession(options.sessionId, existing);
40419
+ this.#nativeBrowserSessions.touch(options.sessionId, existing);
40350
40420
  return existing;
40351
40421
  }
40352
40422
  await this.prepareNativeSessionBrowser(options);
@@ -40357,17 +40427,16 @@ var AcpSessionManager = class {
40357
40427
  return started;
40358
40428
  }
40359
40429
  listNativeSessions = async (options, cursor) => {
40360
- this.#beginNativeBrowserUse(options.sessionId);
40361
- try {
40430
+ return await this.#nativeBrowserSessions.use(options.sessionId, async () => {
40362
40431
  return await (await this.#ensureNativeSessionBrowser(options)).listNativeSessions(cursor, options.signal);
40363
- } finally {
40364
- this.#endNativeBrowserUse(options.sessionId);
40365
- }
40432
+ });
40366
40433
  };
40367
40434
  previewNativeSession = async (options, nativeSessionId) => {
40368
40435
  const key = `${options.agent.id}:${nativeSessionId}`;
40369
40436
  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))) {
40437
+ if (this.#nativeOperations.has(key) || linked.some(
40438
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
40439
+ )) {
40371
40440
  throw new AcpExecutionError(
40372
40441
  "Wait for the native session's running operation to finish",
40373
40442
  {},
@@ -40394,8 +40463,7 @@ var AcpSessionManager = class {
40394
40463
  }
40395
40464
  };
40396
40465
  loadSessionHistory = async (options) => {
40397
- this.#beginSessionUse(options.sessionId);
40398
- try {
40466
+ return await this.#sessions.use(options.sessionId, async () => {
40399
40467
  const runtime = await this.ensureSession(options);
40400
40468
  const stored = this.#store.read(options.sessionId);
40401
40469
  if (stored instanceof FoundAcpStoredSession && this.#nativeHistoryReader !== null) {
@@ -40409,9 +40477,7 @@ var AcpSessionManager = class {
40409
40477
  }
40410
40478
  }
40411
40479
  return await runtime.loadHistory(options.signal);
40412
- } finally {
40413
- this.#endSessionUse(options.sessionId);
40414
- }
40480
+ });
40415
40481
  };
40416
40482
  openNativeSession = async (options, origin) => {
40417
40483
  const key = `${options.agent.id}:${origin.nativeSessionId}`;
@@ -40419,7 +40485,9 @@ var AcpSessionManager = class {
40419
40485
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
40420
40486
  }
40421
40487
  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))) {
40488
+ if (linked.some(
40489
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
40490
+ )) {
40423
40491
  throw new AcpExecutionError(
40424
40492
  "Wait for the native session's running operation to finish",
40425
40493
  {},
@@ -40428,13 +40496,12 @@ var AcpSessionManager = class {
40428
40496
  }
40429
40497
  const existing = linked[0];
40430
40498
  if (origin.kind === "import" && existing !== void 0) {
40431
- this.#beginSessionUse(existing.sessionId);
40432
- try {
40499
+ return await this.#sessions.use(existing.sessionId, async () => {
40433
40500
  const runtime = await this.ensureSession({
40434
40501
  ...options,
40435
40502
  sessionId: existing.sessionId
40436
40503
  });
40437
- if (this.#activeCommandIds.has(existing.sessionId) || this.#nativeOperations.has(key)) {
40504
+ if (this.#executions.command(existing.sessionId) !== void 0 || this.#nativeOperations.has(key)) {
40438
40505
  throw new AcpExecutionError(
40439
40506
  "Wait for the native session's running operation to finish",
40440
40507
  {},
@@ -40451,48 +40518,49 @@ var AcpSessionManager = class {
40451
40518
  this.#nativeOperations.delete(key);
40452
40519
  this.#processes.notifyChanged();
40453
40520
  }
40454
- } finally {
40455
- this.#endSessionUse(existing.sessionId);
40456
- }
40521
+ });
40457
40522
  }
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) {
40523
+ 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
40524
  throw new AcpExecutionError("The destination conversation already exists", {}, "session_identity_mismatch");
40460
40525
  }
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);
40526
+ return await this.#sessions.use(options.sessionId, async () => {
40527
+ this.#nativeOperations.add(key);
40467
40528
  try {
40468
- return {
40469
- sessionId: options.sessionId,
40470
- prepared: runtime.prepared()
40471
- };
40529
+ const runtime = await this.#startPreparation({ ...options, origin });
40530
+ const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
40531
+ this.#nativeOperations.add(openedKey);
40532
+ try {
40533
+ return {
40534
+ sessionId: options.sessionId,
40535
+ prepared: runtime.prepared()
40536
+ };
40537
+ } finally {
40538
+ this.#nativeOperations.delete(openedKey);
40539
+ this.#processes.notifyChanged();
40540
+ }
40541
+ } catch (error62) {
40542
+ this.#store.remove(options.sessionId);
40543
+ const runtime = this.#sessions.get(options.sessionId);
40544
+ if (runtime !== void 0) {
40545
+ await this.#retireSessionProcess(
40546
+ options.sessionId,
40547
+ runtime,
40548
+ async () => await runtime.closeProcess()
40549
+ );
40550
+ }
40551
+ throw error62;
40472
40552
  } 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());
40553
+ this.#nativeOperations.delete(key);
40481
40554
  }
40482
- throw error62;
40483
- } finally {
40484
- this.#nativeOperations.delete(key);
40485
- this.#endSessionUse(options.sessionId);
40486
- }
40555
+ });
40487
40556
  };
40488
40557
  deleteNativeSession = async (options, nativeSessionId) => {
40489
- this.#beginNativeBrowserUse(options.sessionId);
40490
- try {
40558
+ return await this.#nativeBrowserSessions.use(options.sessionId, async () => {
40491
40559
  const runtime = await this.#ensureNativeSessionBrowser(options);
40492
40560
  const key = `${options.agent.id}:${nativeSessionId}`;
40493
40561
  const linked = this.#store.findNative(options.agent.id, nativeSessionId);
40494
40562
  if (this.#nativeOperations.has(key) || linked.some(
40495
- (entry) => this.#activeCommandIds.has(entry.sessionId) || this.#preparing.has(entry.sessionId)
40563
+ (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#sessions.pending(entry.sessionId) !== void 0
40496
40564
  )) {
40497
40565
  throw new AcpExecutionError(
40498
40566
  "Wait for the native session's running operation to finish",
@@ -40523,9 +40591,7 @@ var AcpSessionManager = class {
40523
40591
  this.#nativeOperations.delete(key);
40524
40592
  this.#processes.notifyChanged();
40525
40593
  }
40526
- } finally {
40527
- this.#endNativeBrowserUse(options.sessionId);
40528
- }
40594
+ });
40529
40595
  };
40530
40596
  };
40531
40597