@moxt-ai/mobius 0.0.131 → 0.0.133

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/runtime.js CHANGED
@@ -23984,6 +23984,7 @@ var CodexAcpAgent = class {
23984
23984
  #settings;
23985
23985
  #nativeSessionId = "";
23986
23986
  #streamedReasoningItemIds = /* @__PURE__ */ new Set();
23987
+ #streamedAgentMessageItemIds = /* @__PURE__ */ new Set();
23987
23988
  #turn = null;
23988
23989
  #nativeTurnId = null;
23989
23990
  #updateTail = Promise.resolve();
@@ -24293,6 +24294,20 @@ var CodexAcpAgent = class {
24293
24294
  }
24294
24295
  return;
24295
24296
  }
24297
+ if (method === "item/agentMessage/delta") {
24298
+ const decoded = reasoningTextDeltaParamsSchema.safeParse(params);
24299
+ if (decoded.success && decoded.data.threadId === this.#nativeSessionId && this.#turn !== null && decoded.data.turnId === this.#turn.turn.id && decoded.data.delta.length > 0) {
24300
+ this.#streamedAgentMessageItemIds.add(decoded.data.itemId);
24301
+ await this.#enqueue(async () => {
24302
+ await this.#emit({
24303
+ content: { text: decoded.data.delta, type: "text" },
24304
+ messageId: decoded.data.itemId,
24305
+ sessionUpdate: "agent_message_chunk"
24306
+ });
24307
+ });
24308
+ }
24309
+ return;
24310
+ }
24296
24311
  if (method !== "item/started" && method !== "item/completed") {
24297
24312
  return;
24298
24313
  }
@@ -24301,7 +24316,7 @@ var CodexAcpAgent = class {
24301
24316
  return;
24302
24317
  }
24303
24318
  if (item.type === "agentMessage") {
24304
- if (method === "item/started") {
24319
+ if (method === "item/started" || this.#streamedAgentMessageItemIds.delete(item.id)) {
24305
24320
  return;
24306
24321
  }
24307
24322
  await this.#enqueue(async () => {
@@ -24464,6 +24479,7 @@ var CodexAcpAgent = class {
24464
24479
  };
24465
24480
  signal.addEventListener("abort", aborted2, { once: true });
24466
24481
  const turn = { completion, turn: { id: "" } };
24482
+ this.#streamedAgentMessageItemIds.clear();
24467
24483
  this.#streamedReasoningItemIds.clear();
24468
24484
  this.#turn = turn;
24469
24485
  this.#nativeTurnId = null;
@@ -24488,6 +24504,7 @@ var CodexAcpAgent = class {
24488
24504
  signal.removeEventListener("abort", aborted2);
24489
24505
  if (this.#turn === turn) {
24490
24506
  this.#turn = null;
24507
+ this.#streamedAgentMessageItemIds.clear();
24491
24508
  this.#streamedReasoningItemIds.clear();
24492
24509
  }
24493
24510
  }
@@ -27668,33 +27685,6 @@ var AcpActivityTracker = class {
27668
27685
  // src/acp/agent-turn.ts
27669
27686
  var SESSION_CONTROL_COMMAND_ID = "session-control";
27670
27687
  var AcpTurnState = class {
27671
- #bufferedText = [];
27672
- bufferText = (text2, messageId) => {
27673
- const previous = this.#bufferedText.at(-1);
27674
- if (previous?.messageId === messageId) {
27675
- previous.chunks.push(text2);
27676
- } else {
27677
- this.#bufferedText.push({ chunks: [text2], messageId });
27678
- }
27679
- };
27680
- flushText = async () => {
27681
- const buffered = this.#bufferedText;
27682
- this.#bufferedText = [];
27683
- for (const message of buffered) {
27684
- for (const chunk of message.chunks) {
27685
- await this.onText(chunk, message.messageId);
27686
- }
27687
- }
27688
- };
27689
- recordTextAsProgress = async () => {
27690
- const buffered = this.#bufferedText;
27691
- this.#bufferedText = [];
27692
- for (const message of buffered) {
27693
- for (const chunk of message.chunks) {
27694
- await this.activities.recordNarrative("progress", message.messageId, chunk);
27695
- }
27696
- }
27697
- };
27698
27688
  };
27699
27689
  var IdleAcpTurn = class extends AcpTurnState {
27700
27690
  commandId = SESSION_CONTROL_COMMAND_ID;
@@ -35066,7 +35056,6 @@ var AgentSession = class _AgentSession {
35066
35056
  options.signal
35067
35057
  );
35068
35058
  await this.#updates.drained();
35069
- await this.#turn.flushText();
35070
35059
  await running.activities.finish();
35071
35060
  let result;
35072
35061
  try {
@@ -35188,11 +35177,10 @@ var AgentSession = class _AgentSession {
35188
35177
  );
35189
35178
  } else {
35190
35179
  await this.#turn.activities.finishNarrative();
35191
- this.#turn.bufferText(update.content.text, truncate3(update.messageId ?? "", 255));
35180
+ await this.#turn.onText(update.content.text, truncate3(update.messageId ?? "", 255));
35192
35181
  }
35193
35182
  } else {
35194
35183
  await this.#turn.activities.finishNarrative();
35195
- await this.#turn.flushText();
35196
35184
  await this.emitAgentContent(
35197
35185
  normalizeContentBlock(update.content),
35198
35186
  truncate3(update.messageId ?? "", 255)
@@ -35211,7 +35199,6 @@ var AgentSession = class _AgentSession {
35211
35199
  return;
35212
35200
  }
35213
35201
  if (update.sessionUpdate === "tool_call") {
35214
- await this.#turn.recordTextAsProgress();
35215
35202
  await this.#turn.activities.recordToolCall(update);
35216
35203
  return;
35217
35204
  }
@@ -35443,6 +35430,48 @@ var RetainedAgentProcesses = class {
35443
35430
  };
35444
35431
  };
35445
35432
 
35433
+ // src/acp/runtime-operations.ts
35434
+ var RuntimeOperations = class {
35435
+ #shutdown = new AbortController();
35436
+ #pending = /* @__PURE__ */ new Set();
35437
+ #closing = null;
35438
+ run = async (options, operation) => {
35439
+ const signal = AbortSignal.any([options.signal, this.#shutdown.signal]);
35440
+ signal.throwIfAborted();
35441
+ const pending = Promise.resolve().then(() => operation({ ...options, signal }));
35442
+ this.#pending.add(pending);
35443
+ try {
35444
+ const result = await pending;
35445
+ signal.throwIfAborted();
35446
+ return result;
35447
+ } finally {
35448
+ this.#pending.delete(pending);
35449
+ }
35450
+ };
35451
+ close = (release) => {
35452
+ if (this.#closing !== null) {
35453
+ return this.#closing;
35454
+ }
35455
+ this.#shutdown.abort();
35456
+ this.#closing = (async () => {
35457
+ await Promise.allSettled([...this.#pending]);
35458
+ await release();
35459
+ })();
35460
+ return this.#closing;
35461
+ };
35462
+ };
35463
+ async function waitForPreparation(pending, signal) {
35464
+ signal.throwIfAborted();
35465
+ const cancelled = Promise.withResolvers();
35466
+ const abort = () => cancelled.reject(signal.reason);
35467
+ signal.addEventListener("abort", abort, { once: true });
35468
+ try {
35469
+ return await Promise.race([pending, cancelled.promise]);
35470
+ } finally {
35471
+ signal.removeEventListener("abort", abort);
35472
+ }
35473
+ }
35474
+
35446
35475
  // src/acp/agent-query-processes.ts
35447
35476
  var AgentQueryProcesses = class {
35448
35477
  #processesByQuery;
@@ -35489,7 +35518,7 @@ var AgentQueryProcesses = class {
35489
35518
  prepare = async (options) => {
35490
35519
  const pending = this.#processesByQuery.pending(options.sessionId);
35491
35520
  if (pending !== void 0) {
35492
- const runtime2 = await pending;
35521
+ const runtime2 = await waitForPreparation(pending, options.signal);
35493
35522
  if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35494
35523
  this.#processesByQuery.touch(options.sessionId, runtime2);
35495
35524
  return runtime2.prepared();
@@ -35768,6 +35797,7 @@ var AgentProcessPool = class {
35768
35797
  // src/acp/agent-runtime-manager.ts
35769
35798
  var SESSION_CONTROL_TIMEOUT_MILLISECONDS = 3e4;
35770
35799
  var AgentRuntimeManager = class {
35800
+ #operations = new RuntimeOperations();
35771
35801
  #processes = new AgentProcessPool();
35772
35802
  #executionProcesses = new RetainedAgentProcesses({
35773
35803
  touch: this.#processes.touch,
@@ -35890,8 +35920,8 @@ var AgentRuntimeManager = class {
35890
35920
  return runtime;
35891
35921
  });
35892
35922
  }
35893
- prepareSession = async (options, metrics = unmeasuredOperationMetrics) => {
35894
- const stored = metrics.measureSync("session-state-read", () => this.#store.read(options.sessionId));
35923
+ prepareSession = async (options, metrics = unmeasuredOperationMetrics) => this.#operations.run(options, async (options2) => {
35924
+ const stored = metrics.measureSync("session-state-read", () => this.#store.read(options2.sessionId));
35895
35925
  if (stored instanceof DeletedAcpStoredSession) {
35896
35926
  throw new AcpExecutionError(
35897
35927
  "The native Agent session was deleted. Start a new chat.",
@@ -35902,19 +35932,22 @@ var AgentRuntimeManager = class {
35902
35932
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
35903
35933
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
35904
35934
  }
35905
- const pending = this.#executionProcesses.pending(options.sessionId);
35935
+ const pending = this.#executionProcesses.pending(options2.sessionId);
35906
35936
  if (pending !== void 0) {
35907
- const runtime2 = await metrics.measure("session-preparation-wait", async () => await pending);
35908
- if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35909
- this.#executionProcesses.touch(options.sessionId, runtime2);
35937
+ const runtime2 = await metrics.measure(
35938
+ "session-preparation-wait",
35939
+ async () => await waitForPreparation(pending, options2.signal)
35940
+ );
35941
+ if (runtime2.belongsTo(options2.agent.id, options2.cwd, options2.additionalDirectories)) {
35942
+ this.#executionProcesses.touch(options2.sessionId, runtime2);
35910
35943
  return runtime2.prepared();
35911
35944
  }
35912
- return await this.prepareSession(options, metrics);
35945
+ return await this.prepareSession(options2, metrics);
35913
35946
  }
35914
- const existing = this.#executionProcesses.get(options.sessionId);
35947
+ const existing = this.#executionProcesses.get(options2.sessionId);
35915
35948
  if (existing !== void 0) {
35916
- if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
35917
- this.#executionProcesses.touch(options.sessionId, existing);
35949
+ if (existing.belongsTo(options2.agent.id, options2.cwd, options2.additionalDirectories)) {
35950
+ this.#executionProcesses.touch(options2.sessionId, existing);
35918
35951
  return existing.prepared();
35919
35952
  }
35920
35953
  if (!existing.canRebind()) {
@@ -35924,85 +35957,91 @@ var AgentRuntimeManager = class {
35924
35957
  "session_identity_mismatch"
35925
35958
  );
35926
35959
  }
35927
- const runtime2 = await this.#replacePreparation(existing, options, metrics);
35960
+ const runtime2 = await this.#replacePreparation(existing, options2, metrics);
35928
35961
  return runtime2.prepared();
35929
35962
  }
35930
- const runtime = await this.#startPreparation(options, metrics);
35963
+ const runtime = await this.#startPreparation(options2, metrics);
35931
35964
  return runtime.prepared();
35932
- };
35965
+ });
35933
35966
  /**
35934
35967
  * Opens a short-lived native ACP session only to read the local Agent's
35935
35968
  * authoritative settings. Its identity and store are private to this
35936
35969
  * operation and never become a Mobius Session.
35937
35970
  */
35938
- inspectAgentSettings = async (options) => await this.#processes.runTransient(
35939
- options.signal,
35940
- async () => await AgentSession.start(
35941
- {
35942
- ...options,
35943
- sessionId: `agent-settings-${crypto.randomUUID()}`
35971
+ inspectAgentSettings = async (options) => this.#operations.run(options, async (options2) => {
35972
+ return await this.#processes.runTransient(
35973
+ options2.signal,
35974
+ async () => await AgentSession.start(
35975
+ {
35976
+ ...options2,
35977
+ sessionId: `agent-settings-${crypto.randomUUID()}`
35978
+ },
35979
+ () => this.#agentSettingsObserver,
35980
+ new MemoryAcpSessionStore()
35981
+ ),
35982
+ async (runtime) => {
35983
+ const prepared = runtime.prepared();
35984
+ const model = options2.model;
35985
+ const configOptions = model === void 0 ? prepared.configOptions : await runtime.configure({
35986
+ ...options2,
35987
+ booleanValue: false,
35988
+ configId: prepared.configOptions.find(
35989
+ (option) => option.id === "model" || option.category === "model"
35990
+ )?.id ?? "model",
35991
+ requestId: "agent-settings-model",
35992
+ sessionId: `agent-settings-${crypto.randomUUID()}`,
35993
+ value: model,
35994
+ valueType: "select"
35995
+ });
35996
+ return new AcpAgentSettingsSnapshot(prepared.agentName, prepared.agentVersion, configOptions);
35944
35997
  },
35945
- () => this.#agentSettingsObserver,
35946
- new MemoryAcpSessionStore()
35947
- ),
35948
- async (runtime) => {
35949
- const prepared = runtime.prepared();
35950
- const model = options.model;
35951
- const configOptions = model === void 0 ? prepared.configOptions : await runtime.configure({
35952
- ...options,
35953
- booleanValue: false,
35954
- configId: prepared.configOptions.find(
35955
- (option) => option.id === "model" || option.category === "model"
35956
- )?.id ?? "model",
35957
- requestId: "agent-settings-model",
35958
- sessionId: `agent-settings-${crypto.randomUUID()}`,
35959
- value: model,
35960
- valueType: "select"
35961
- });
35962
- return new AcpAgentSettingsSnapshot(prepared.agentName, prepared.agentVersion, configOptions);
35963
- },
35964
- async (runtime) => await runtime.close()
35965
- );
35998
+ async (runtime) => await runtime.close()
35999
+ );
36000
+ });
35966
36001
  /**
35967
36002
  * Retained for Agents that expose a separate persisted configuration channel.
35968
36003
  * Use inspectAgentSettings for Agent-scoped settings discovery.
35969
36004
  */
35970
- inspectSession = async (options, metrics = unmeasuredOperationMetrics) => {
35971
- if (this.#executionProcesses.pending(options.sessionId) !== void 0 || this.#executionProcesses.get(options.sessionId) !== void 0) {
35972
- throw new AcpExecutionError("The configuration inspection session is already in use", {}, "session_busy");
36005
+ inspectSession = async (options, metrics = unmeasuredOperationMetrics) => this.#operations.run(options, async (options2) => {
36006
+ if (this.#executionProcesses.pending(options2.sessionId) !== void 0 || this.#executionProcesses.get(options2.sessionId) !== void 0) {
36007
+ throw new AcpExecutionError(
36008
+ "The configuration inspection session is already in use",
36009
+ {},
36010
+ "session_busy"
36011
+ );
35973
36012
  }
35974
- return await this.#executionProcesses.use(options.sessionId, async () => {
35975
- const runtime = await this.#startPreparation(options, metrics);
36013
+ return await this.#executionProcesses.use(options2.sessionId, async () => {
36014
+ const runtime = await this.#startPreparation(options2, metrics);
35976
36015
  try {
35977
36016
  return runtime.prepared();
35978
36017
  } finally {
35979
36018
  try {
35980
- await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
36019
+ await this.#retireSessionProcess(options2.sessionId, runtime, async () => await runtime.close());
35981
36020
  } finally {
35982
- this.#store.remove(options.sessionId);
36021
+ this.#store.remove(options2.sessionId);
35983
36022
  }
35984
36023
  }
35985
36024
  });
35986
- };
35987
- prepareExecutionSession = async (options) => {
35988
- const pending = this.#executionProcesses.pending(options.sessionId);
36025
+ });
36026
+ prepareExecutionSession = async (options) => this.#operations.run(options, async (options2) => {
36027
+ const pending = this.#executionProcesses.pending(options2.sessionId);
35989
36028
  if (pending !== void 0) {
35990
- await pending;
35991
- return await this.prepareExecutionSession(options);
36029
+ await waitForPreparation(pending, options2.signal);
36030
+ return await this.prepareExecutionSession(options2);
35992
36031
  }
35993
- const existing = this.#executionProcesses.get(options.sessionId);
36032
+ const existing = this.#executionProcesses.get(options2.sessionId);
35994
36033
  if (existing === void 0) {
35995
- return (await this.#startPreparation(options)).prepared();
36034
+ return (await this.#startPreparation(options2)).prepared();
35996
36035
  }
35997
- if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
36036
+ if (!existing.belongsTo(options2.agent.id, options2.cwd, options2.additionalDirectories)) {
35998
36037
  throw new AcpExecutionError(
35999
36038
  "The agent or workspace cannot change after a conversation has started",
36000
36039
  {},
36001
36040
  "session_identity_mismatch"
36002
36041
  );
36003
36042
  }
36004
- if (existing.usesEnvironment(options.agent.environment)) {
36005
- this.#executionProcesses.touch(options.sessionId, existing);
36043
+ if (existing.usesEnvironment(options2.agent.environment)) {
36044
+ this.#executionProcesses.touch(options2.sessionId, existing);
36006
36045
  return existing.prepared();
36007
36046
  }
36008
36047
  if (existing.hasRunningTurn()) {
@@ -36012,15 +36051,15 @@ var AgentRuntimeManager = class {
36012
36051
  "session_busy"
36013
36052
  );
36014
36053
  }
36015
- return (await this.#replaceExecutionPreparation(existing, options)).prepared();
36016
- };
36017
- prepareNativeSessionBrowser = (options) => this.#queryProcesses.prepare(options);
36018
- configureSession = async (options) => {
36019
- return await this.#executionProcesses.use(options.sessionId, async () => {
36020
- const runtime = await this.ensureSession(options);
36021
- return await runtime.configure(options);
36054
+ return (await this.#replaceExecutionPreparation(existing, options2)).prepared();
36055
+ });
36056
+ prepareNativeSessionBrowser = (options) => this.#operations.run(options, (scoped) => this.#queryProcesses.prepare(scoped));
36057
+ configureSession = async (options) => this.#operations.run(options, async (options2) => {
36058
+ return await this.#executionProcesses.use(options2.sessionId, async () => {
36059
+ const runtime = await this.ensureSession(options2);
36060
+ return await runtime.configure(options2);
36022
36061
  });
36023
- };
36062
+ });
36024
36063
  cancelPrompt = async (sessionId) => {
36025
36064
  await this.#executions.cancel(sessionId, async () => {
36026
36065
  const runtime = this.#executionProcesses.get(sessionId);
@@ -36035,19 +36074,19 @@ var AgentRuntimeManager = class {
36035
36074
  }
36036
36075
  });
36037
36076
  };
36038
- steerPrompt = async (options) => {
36039
- if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
36077
+ steerPrompt = async (options) => this.#operations.run(options, async (options2) => {
36078
+ if (this.#executions.command(options2.sessionId) !== options2.targetCommandId) {
36040
36079
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
36041
36080
  }
36042
- const pending = this.#executionProcesses.pending(options.sessionId);
36081
+ const pending = this.#executionProcesses.pending(options2.sessionId);
36043
36082
  if (pending !== void 0) {
36044
- await pending;
36083
+ await waitForPreparation(pending, options2.signal);
36045
36084
  }
36046
- if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
36085
+ if (this.#executions.command(options2.sessionId) !== options2.targetCommandId) {
36047
36086
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
36048
36087
  }
36049
- const runtime = this.#executionProcesses.get(options.sessionId);
36050
- if (runtime === void 0 || !runtime.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
36088
+ const runtime = this.#executionProcesses.get(options2.sessionId);
36089
+ if (runtime === void 0 || !runtime.belongsTo(options2.agent.id, options2.cwd, options2.additionalDirectories)) {
36051
36090
  throw new AcpExecutionError(
36052
36091
  "The target turn execution context is unavailable",
36053
36092
  {},
@@ -36064,8 +36103,8 @@ var AgentRuntimeManager = class {
36064
36103
  deadline.unref();
36065
36104
  try {
36066
36105
  return await runtime.steer({
36067
- ...options,
36068
- signal: AbortSignal.any([options.signal, deadlineController.signal])
36106
+ ...options2,
36107
+ signal: AbortSignal.any([options2.signal, deadlineController.signal])
36069
36108
  });
36070
36109
  } catch (error62) {
36071
36110
  if (error62 instanceof AcpExecutionError) {
@@ -36075,44 +36114,44 @@ var AgentRuntimeManager = class {
36075
36114
  } finally {
36076
36115
  clearTimeout(deadline);
36077
36116
  }
36078
- };
36079
- runPrompt = async (options) => {
36080
- const stored = this.#store.read(options.sessionId);
36117
+ });
36118
+ runPrompt = async (options) => this.#operations.run(options, async (options2) => {
36119
+ const stored = this.#store.read(options2.sessionId);
36081
36120
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
36082
36121
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
36083
36122
  }
36084
- this.#executions.begin(options.sessionId, options.commandId);
36123
+ this.#executions.begin(options2.sessionId, options2.commandId);
36085
36124
  try {
36086
- if (this.#executions.cancellation(options.sessionId) !== void 0) {
36125
+ if (this.#executions.cancellation(options2.sessionId) !== void 0) {
36087
36126
  return { stopReason: "cancelled" };
36088
36127
  }
36089
- const runtime = await this.ensureSession(options);
36090
- if (this.#nativeOperations.has(`${options.agent.id}:${runtime.nativeSessionId()}`)) {
36128
+ const runtime = await this.ensureSession(options2);
36129
+ if (this.#nativeOperations.has(`${options2.agent.id}:${runtime.nativeSessionId()}`)) {
36091
36130
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
36092
36131
  }
36093
- if (this.#executions.cancellation(options.sessionId) !== void 0) {
36132
+ if (this.#executions.cancellation(options2.sessionId) !== void 0) {
36094
36133
  return { stopReason: "cancelled" };
36095
36134
  }
36096
36135
  try {
36097
- const result = await runtime.prompt(options);
36098
- return this.#executions.cancellation(options.sessionId) !== void 0 ? { stopReason: "cancelled" } : result;
36136
+ const result = await runtime.prompt(options2);
36137
+ return this.#executions.cancellation(options2.sessionId) !== void 0 ? { stopReason: "cancelled" } : result;
36099
36138
  } catch (error62) {
36100
- if (this.#executions.cancellation(options.sessionId) !== void 0) {
36101
- await this.#executions.cancellation(options.sessionId);
36139
+ if (this.#executions.cancellation(options2.sessionId) !== void 0) {
36140
+ await this.#executions.cancellation(options2.sessionId);
36102
36141
  return { stopReason: "cancelled" };
36103
36142
  }
36104
36143
  throw error62;
36105
36144
  }
36106
36145
  } catch (error62) {
36107
- const failedRuntime = this.#executionProcesses.get(options.sessionId);
36146
+ const failedRuntime = this.#executionProcesses.get(options2.sessionId);
36108
36147
  if (failedRuntime !== void 0) {
36109
36148
  await this.#retireSessionProcess(
36110
- options.sessionId,
36149
+ options2.sessionId,
36111
36150
  failedRuntime,
36112
36151
  async () => await failedRuntime.close()
36113
36152
  );
36114
36153
  }
36115
- if (this.#executions.cancellation(options.sessionId) !== void 0) {
36154
+ if (this.#executions.cancellation(options2.sessionId) !== void 0) {
36116
36155
  return { stopReason: "cancelled" };
36117
36156
  }
36118
36157
  if (error62 instanceof AcpExecutionError) {
@@ -36123,14 +36162,14 @@ var AgentRuntimeManager = class {
36123
36162
  });
36124
36163
  } finally {
36125
36164
  try {
36126
- await this.#executions.cancellation(options.sessionId);
36165
+ await this.#executions.cancellation(options2.sessionId);
36127
36166
  } finally {
36128
- this.#executions.finish(options.sessionId, options.commandId);
36129
- this.#executionProcesses.touch(options.sessionId);
36167
+ this.#executions.finish(options2.sessionId, options2.commandId);
36168
+ this.#executionProcesses.touch(options2.sessionId);
36130
36169
  this.#processes.notifyChanged();
36131
36170
  }
36132
36171
  }
36133
- };
36172
+ });
36134
36173
  respondPermission = async (options) => {
36135
36174
  const runtime = this.#executionProcesses.get(options.sessionId);
36136
36175
  if (runtime === void 0) {
@@ -36145,7 +36184,7 @@ var AgentRuntimeManager = class {
36145
36184
  }
36146
36185
  await runtime.respondElicitation(options);
36147
36186
  };
36148
- close = async () => {
36187
+ close = async () => this.#operations.close(async () => {
36149
36188
  const sessions = [...this.#executionProcesses.entries()];
36150
36189
  await Promise.all([
36151
36190
  ...sessions.map(
@@ -36153,23 +36192,23 @@ var AgentRuntimeManager = class {
36153
36192
  ),
36154
36193
  this.#queryProcesses.close()
36155
36194
  ]);
36156
- };
36195
+ });
36157
36196
  findSessions = (sessionIds) => sessionIds.flatMap((sessionId) => {
36158
36197
  const stored = this.#store.read(sessionId);
36159
36198
  return stored instanceof FoundAcpStoredSession ? [stored.session] : [];
36160
36199
  });
36161
36200
  findLinkedSessions = (agentId, nativeSessionId) => this.#store.findNative(agentId, nativeSessionId);
36162
36201
  hasPreparedSession = (sessionId, agentId, cwd, additionalDirectories) => this.#executionProcesses.get(sessionId)?.belongsTo(agentId, cwd, additionalDirectories) === true;
36163
- createNativeSession = async (options) => {
36202
+ createNativeSession = async (options) => this.#operations.run(options, async (options2) => {
36164
36203
  const temporarySessionId = `new-${crypto.randomUUID()}`;
36165
- const temporaryOptions = { ...options, sessionId: temporarySessionId };
36204
+ const temporaryOptions = { ...options2, sessionId: temporarySessionId };
36166
36205
  return await this.#executionProcesses.use(temporarySessionId, async () => {
36167
36206
  try {
36168
36207
  const runtime = await this.#startPreparation(temporaryOptions);
36169
36208
  let adopted = false;
36170
36209
  try {
36171
36210
  const nativeSessionId = runtime.nativeSessionId();
36172
- if (!isNativeChatSessionId(options.agent.id, nativeSessionId) || this.#executionProcesses.get(nativeSessionId) !== void 0 || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
36211
+ if (!isNativeChatSessionId(options2.agent.id, nativeSessionId) || this.#executionProcesses.get(nativeSessionId) !== void 0 || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
36173
36212
  throw new AcpExecutionError(
36174
36213
  "The Agent returned an invalid new Session ID",
36175
36214
  {},
@@ -36195,36 +36234,36 @@ var AgentRuntimeManager = class {
36195
36234
  this.#store.remove(temporarySessionId);
36196
36235
  }
36197
36236
  });
36198
- };
36199
- ensureSession = async (options) => {
36200
- const existing = this.#executionProcesses.get(options.sessionId);
36237
+ });
36238
+ ensureSession = async (options) => this.#operations.run(options, async (options2) => {
36239
+ const existing = this.#executionProcesses.get(options2.sessionId);
36201
36240
  if (existing !== void 0) {
36202
- if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
36241
+ if (!existing.belongsTo(options2.agent.id, options2.cwd, options2.additionalDirectories)) {
36203
36242
  throw new AcpExecutionError(
36204
36243
  "The agent or workspace cannot change after a conversation has started",
36205
36244
  {},
36206
36245
  "session_identity_mismatch"
36207
36246
  );
36208
36247
  }
36209
- this.#executionProcesses.touch(options.sessionId, existing);
36248
+ this.#executionProcesses.touch(options2.sessionId, existing);
36210
36249
  return existing;
36211
36250
  }
36212
- await this.prepareSession(options);
36213
- const started = this.#executionProcesses.get(options.sessionId);
36251
+ await this.prepareSession(options2);
36252
+ const started = this.#executionProcesses.get(options2.sessionId);
36214
36253
  if (started === void 0) {
36215
36254
  throw new AcpExecutionError("ACP session did not start");
36216
36255
  }
36217
36256
  return started;
36218
- };
36219
- listNativeSessions = async (options, cursor) => {
36257
+ });
36258
+ listNativeSessions = async (options, cursor) => this.#operations.run(options, async (options2) => {
36220
36259
  return await this.#queryProcesses.use(
36221
- options,
36222
- async (runtime) => await runtime.listNativeSessions(cursor, options.signal)
36260
+ options2,
36261
+ async (runtime) => await runtime.listNativeSessions(cursor, options2.signal)
36223
36262
  );
36224
- };
36225
- previewNativeSession = async (options, nativeSessionId) => {
36226
- const key = `${options.agent.id}:${nativeSessionId}`;
36227
- const linked = this.#store.findNative(options.agent.id, nativeSessionId);
36263
+ });
36264
+ previewNativeSession = async (options, nativeSessionId) => this.#operations.run(options, async (options2) => {
36265
+ const key = `${options2.agent.id}:${nativeSessionId}`;
36266
+ const linked = this.#store.findNative(options2.agent.id, nativeSessionId);
36228
36267
  if (this.#nativeOperations.has(key) || linked.some(
36229
36268
  (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#executionProcesses.pending(entry.sessionId) !== void 0
36230
36269
  )) {
@@ -36236,18 +36275,18 @@ var AgentRuntimeManager = class {
36236
36275
  }
36237
36276
  this.#nativeOperations.add(key);
36238
36277
  try {
36239
- return await this.#queryProcesses.preview(options, nativeSessionId);
36278
+ return await this.#queryProcesses.preview(options2, nativeSessionId);
36240
36279
  } finally {
36241
36280
  this.#nativeOperations.delete(key);
36242
36281
  }
36243
- };
36244
- loadSessionHistory = async (options) => {
36245
- return await this.#executionProcesses.use(options.sessionId, async () => {
36246
- const runtime = await this.ensureSession(options);
36247
- const stored = this.#store.read(options.sessionId);
36282
+ });
36283
+ loadSessionHistory = async (options) => this.#operations.run(options, async (options2) => {
36284
+ return await this.#executionProcesses.use(options2.sessionId, async () => {
36285
+ const runtime = await this.ensureSession(options2);
36286
+ const stored = this.#store.read(options2.sessionId);
36248
36287
  if (stored instanceof FoundAcpStoredSession && this.#nativeHistoryReader !== null) {
36249
36288
  const history = await this.#nativeHistoryReader.read(
36250
- options,
36289
+ options2,
36251
36290
  stored.session.nativeSessionId,
36252
36291
  runtime.nativeHistoryTurn
36253
36292
  );
@@ -36255,15 +36294,15 @@ var AgentRuntimeManager = class {
36255
36294
  return history;
36256
36295
  }
36257
36296
  }
36258
- return await runtime.loadHistory(options.signal);
36297
+ return await runtime.loadHistory(options2.signal);
36259
36298
  });
36260
- };
36261
- openNativeSession = async (options, origin) => {
36262
- const key = `${options.agent.id}:${origin.nativeSessionId}`;
36299
+ });
36300
+ openNativeSession = async (options, origin) => this.#operations.run(options, async (options2) => {
36301
+ const key = `${options2.agent.id}:${origin.nativeSessionId}`;
36263
36302
  if (this.#nativeOperations.has(key)) {
36264
36303
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
36265
36304
  }
36266
- const linked = this.#store.findNative(options.agent.id, origin.nativeSessionId);
36305
+ const linked = this.#store.findNative(options2.agent.id, origin.nativeSessionId);
36267
36306
  if (linked.some(
36268
36307
  (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#executionProcesses.pending(entry.sessionId) !== void 0
36269
36308
  )) {
@@ -36277,7 +36316,7 @@ var AgentRuntimeManager = class {
36277
36316
  if (origin.kind === "import" && existing !== void 0) {
36278
36317
  return await this.#executionProcesses.use(existing.sessionId, async () => {
36279
36318
  const runtime = await this.ensureSession({
36280
- ...options,
36319
+ ...options2,
36281
36320
  sessionId: existing.sessionId
36282
36321
  });
36283
36322
  if (this.#executions.command(existing.sessionId) !== void 0 || this.#nativeOperations.has(key)) {
@@ -36299,18 +36338,22 @@ var AgentRuntimeManager = class {
36299
36338
  }
36300
36339
  });
36301
36340
  }
36302
- if (this.#executionProcesses.get(options.sessionId) !== void 0 || this.#executionProcesses.pending(options.sessionId) !== void 0 || this.#store.read(options.sessionId) instanceof FoundAcpStoredSession || this.#store.read(options.sessionId) instanceof DeletedAcpStoredSession) {
36303
- throw new AcpExecutionError("The destination conversation already exists", {}, "session_identity_mismatch");
36341
+ if (this.#executionProcesses.get(options2.sessionId) !== void 0 || this.#executionProcesses.pending(options2.sessionId) !== void 0 || this.#store.read(options2.sessionId) instanceof FoundAcpStoredSession || this.#store.read(options2.sessionId) instanceof DeletedAcpStoredSession) {
36342
+ throw new AcpExecutionError(
36343
+ "The destination conversation already exists",
36344
+ {},
36345
+ "session_identity_mismatch"
36346
+ );
36304
36347
  }
36305
- return await this.#executionProcesses.use(options.sessionId, async () => {
36348
+ return await this.#executionProcesses.use(options2.sessionId, async () => {
36306
36349
  this.#nativeOperations.add(key);
36307
36350
  try {
36308
- const runtime = await this.#startPreparation({ ...options, origin });
36309
- const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
36351
+ const runtime = await this.#startPreparation({ ...options2, origin });
36352
+ const openedKey = `${options2.agent.id}:${runtime.nativeSessionId()}`;
36310
36353
  this.#nativeOperations.add(openedKey);
36311
36354
  try {
36312
36355
  return {
36313
- sessionId: options.sessionId,
36356
+ sessionId: options2.sessionId,
36314
36357
  prepared: runtime.prepared()
36315
36358
  };
36316
36359
  } finally {
@@ -36318,11 +36361,11 @@ var AgentRuntimeManager = class {
36318
36361
  this.#processes.notifyChanged();
36319
36362
  }
36320
36363
  } catch (error62) {
36321
- this.#store.remove(options.sessionId);
36322
- const runtime = this.#executionProcesses.get(options.sessionId);
36364
+ this.#store.remove(options2.sessionId);
36365
+ const runtime = this.#executionProcesses.get(options2.sessionId);
36323
36366
  if (runtime !== void 0) {
36324
36367
  await this.#retireSessionProcess(
36325
- options.sessionId,
36368
+ options2.sessionId,
36326
36369
  runtime,
36327
36370
  async () => await runtime.closeProcess()
36328
36371
  );
@@ -36332,11 +36375,11 @@ var AgentRuntimeManager = class {
36332
36375
  this.#nativeOperations.delete(key);
36333
36376
  }
36334
36377
  });
36335
- };
36336
- deleteNativeSession = async (options, nativeSessionId) => {
36337
- return await this.#queryProcesses.use(options, async (runtime) => {
36338
- const key = `${options.agent.id}:${nativeSessionId}`;
36339
- const linked = this.#store.findNative(options.agent.id, nativeSessionId);
36378
+ });
36379
+ deleteNativeSession = async (options, nativeSessionId) => this.#operations.run(options, async (options2) => {
36380
+ return await this.#queryProcesses.use(options2, async (runtime) => {
36381
+ const key = `${options2.agent.id}:${nativeSessionId}`;
36382
+ const linked = this.#store.findNative(options2.agent.id, nativeSessionId);
36340
36383
  if (this.#nativeOperations.has(key) || linked.some(
36341
36384
  (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#executionProcesses.pending(entry.sessionId) !== void 0
36342
36385
  )) {
@@ -36354,11 +36397,11 @@ var AgentRuntimeManager = class {
36354
36397
  await this.#retireSessionProcess(entry.sessionId, active, async () => await active.close());
36355
36398
  }
36356
36399
  }
36357
- options.signal.throwIfAborted();
36358
- await runtime.deleteNativeSession(nativeSessionId, options.signal);
36359
- const deleted = this.#store.markDeleted(options.agent.id, nativeSessionId);
36400
+ options2.signal.throwIfAborted();
36401
+ await runtime.deleteNativeSession(nativeSessionId, options2.signal);
36402
+ const deleted = this.#store.markDeleted(options2.agent.id, nativeSessionId);
36360
36403
  if (runtime.nativeSessionId() === nativeSessionId) {
36361
- await this.#queryProcesses.retire(options.sessionId, runtime);
36404
+ await this.#queryProcesses.retire(options2.sessionId, runtime);
36362
36405
  }
36363
36406
  return deleted;
36364
36407
  } finally {
@@ -36366,7 +36409,7 @@ var AgentRuntimeManager = class {
36366
36409
  this.#processes.notifyChanged();
36367
36410
  }
36368
36411
  });
36369
- };
36412
+ });
36370
36413
  };
36371
36414
 
36372
36415
  // src/agent-catalog/publish-agent-catalog.ts