@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/index.js CHANGED
@@ -23593,33 +23593,6 @@ var AcpActivityTracker = class {
23593
23593
  // src/acp/agent-turn.ts
23594
23594
  var SESSION_CONTROL_COMMAND_ID = "session-control";
23595
23595
  var AcpTurnState = class {
23596
- #bufferedText = [];
23597
- bufferText = (text2, messageId) => {
23598
- const previous = this.#bufferedText.at(-1);
23599
- if (previous?.messageId === messageId) {
23600
- previous.chunks.push(text2);
23601
- } else {
23602
- this.#bufferedText.push({ chunks: [text2], messageId });
23603
- }
23604
- };
23605
- flushText = async () => {
23606
- const buffered = this.#bufferedText;
23607
- this.#bufferedText = [];
23608
- for (const message of buffered) {
23609
- for (const chunk of message.chunks) {
23610
- await this.onText(chunk, message.messageId);
23611
- }
23612
- }
23613
- };
23614
- recordTextAsProgress = async () => {
23615
- const buffered = this.#bufferedText;
23616
- this.#bufferedText = [];
23617
- for (const message of buffered) {
23618
- for (const chunk of message.chunks) {
23619
- await this.activities.recordNarrative("progress", message.messageId, chunk);
23620
- }
23621
- }
23622
- };
23623
23596
  };
23624
23597
  var IdleAcpTurn = class extends AcpTurnState {
23625
23598
  commandId = SESSION_CONTROL_COMMAND_ID;
@@ -30991,7 +30964,6 @@ var AgentSession = class _AgentSession {
30991
30964
  options.signal
30992
30965
  );
30993
30966
  await this.#updates.drained();
30994
- await this.#turn.flushText();
30995
30967
  await running.activities.finish();
30996
30968
  let result;
30997
30969
  try {
@@ -31113,11 +31085,10 @@ var AgentSession = class _AgentSession {
31113
31085
  );
31114
31086
  } else {
31115
31087
  await this.#turn.activities.finishNarrative();
31116
- this.#turn.bufferText(update.content.text, truncate2(update.messageId ?? "", 255));
31088
+ await this.#turn.onText(update.content.text, truncate2(update.messageId ?? "", 255));
31117
31089
  }
31118
31090
  } else {
31119
31091
  await this.#turn.activities.finishNarrative();
31120
- await this.#turn.flushText();
31121
31092
  await this.emitAgentContent(
31122
31093
  normalizeContentBlock(update.content),
31123
31094
  truncate2(update.messageId ?? "", 255)
@@ -31136,7 +31107,6 @@ var AgentSession = class _AgentSession {
31136
31107
  return;
31137
31108
  }
31138
31109
  if (update.sessionUpdate === "tool_call") {
31139
- await this.#turn.recordTextAsProgress();
31140
31110
  await this.#turn.activities.recordToolCall(update);
31141
31111
  return;
31142
31112
  }
@@ -31368,6 +31338,48 @@ var RetainedAgentProcesses = class {
31368
31338
  };
31369
31339
  };
31370
31340
 
31341
+ // src/acp/runtime-operations.ts
31342
+ var RuntimeOperations = class {
31343
+ #shutdown = new AbortController();
31344
+ #pending = /* @__PURE__ */ new Set();
31345
+ #closing = null;
31346
+ run = async (options, operation) => {
31347
+ const signal = AbortSignal.any([options.signal, this.#shutdown.signal]);
31348
+ signal.throwIfAborted();
31349
+ const pending = Promise.resolve().then(() => operation({ ...options, signal }));
31350
+ this.#pending.add(pending);
31351
+ try {
31352
+ const result = await pending;
31353
+ signal.throwIfAborted();
31354
+ return result;
31355
+ } finally {
31356
+ this.#pending.delete(pending);
31357
+ }
31358
+ };
31359
+ close = (release) => {
31360
+ if (this.#closing !== null) {
31361
+ return this.#closing;
31362
+ }
31363
+ this.#shutdown.abort();
31364
+ this.#closing = (async () => {
31365
+ await Promise.allSettled([...this.#pending]);
31366
+ await release();
31367
+ })();
31368
+ return this.#closing;
31369
+ };
31370
+ };
31371
+ async function waitForPreparation(pending, signal) {
31372
+ signal.throwIfAborted();
31373
+ const cancelled = Promise.withResolvers();
31374
+ const abort = () => cancelled.reject(signal.reason);
31375
+ signal.addEventListener("abort", abort, { once: true });
31376
+ try {
31377
+ return await Promise.race([pending, cancelled.promise]);
31378
+ } finally {
31379
+ signal.removeEventListener("abort", abort);
31380
+ }
31381
+ }
31382
+
31371
31383
  // src/acp/agent-query-processes.ts
31372
31384
  var AgentQueryProcesses = class {
31373
31385
  #processesByQuery;
@@ -31414,7 +31426,7 @@ var AgentQueryProcesses = class {
31414
31426
  prepare = async (options) => {
31415
31427
  const pending = this.#processesByQuery.pending(options.sessionId);
31416
31428
  if (pending !== void 0) {
31417
- const runtime2 = await pending;
31429
+ const runtime2 = await waitForPreparation(pending, options.signal);
31418
31430
  if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31419
31431
  this.#processesByQuery.touch(options.sessionId, runtime2);
31420
31432
  return runtime2.prepared();
@@ -31693,6 +31705,7 @@ var AgentProcessPool = class {
31693
31705
  // src/acp/agent-runtime-manager.ts
31694
31706
  var SESSION_CONTROL_TIMEOUT_MILLISECONDS = 3e4;
31695
31707
  var AgentRuntimeManager = class {
31708
+ #operations = new RuntimeOperations();
31696
31709
  #processes = new AgentProcessPool();
31697
31710
  #executionProcesses = new RetainedAgentProcesses({
31698
31711
  touch: this.#processes.touch,
@@ -31815,8 +31828,8 @@ var AgentRuntimeManager = class {
31815
31828
  return runtime;
31816
31829
  });
31817
31830
  }
31818
- prepareSession = async (options, metrics = unmeasuredOperationMetrics) => {
31819
- const stored = metrics.measureSync("session-state-read", () => this.#store.read(options.sessionId));
31831
+ prepareSession = async (options, metrics = unmeasuredOperationMetrics) => this.#operations.run(options, async (options2) => {
31832
+ const stored = metrics.measureSync("session-state-read", () => this.#store.read(options2.sessionId));
31820
31833
  if (stored instanceof DeletedAcpStoredSession) {
31821
31834
  throw new AcpExecutionError(
31822
31835
  "The native Agent session was deleted. Start a new chat.",
@@ -31827,19 +31840,22 @@ var AgentRuntimeManager = class {
31827
31840
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
31828
31841
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
31829
31842
  }
31830
- const pending = this.#executionProcesses.pending(options.sessionId);
31843
+ const pending = this.#executionProcesses.pending(options2.sessionId);
31831
31844
  if (pending !== void 0) {
31832
- const runtime2 = await metrics.measure("session-preparation-wait", async () => await pending);
31833
- if (runtime2.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31834
- this.#executionProcesses.touch(options.sessionId, runtime2);
31845
+ const runtime2 = await metrics.measure(
31846
+ "session-preparation-wait",
31847
+ async () => await waitForPreparation(pending, options2.signal)
31848
+ );
31849
+ if (runtime2.belongsTo(options2.agent.id, options2.cwd, options2.additionalDirectories)) {
31850
+ this.#executionProcesses.touch(options2.sessionId, runtime2);
31835
31851
  return runtime2.prepared();
31836
31852
  }
31837
- return await this.prepareSession(options, metrics);
31853
+ return await this.prepareSession(options2, metrics);
31838
31854
  }
31839
- const existing = this.#executionProcesses.get(options.sessionId);
31855
+ const existing = this.#executionProcesses.get(options2.sessionId);
31840
31856
  if (existing !== void 0) {
31841
- if (existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31842
- this.#executionProcesses.touch(options.sessionId, existing);
31857
+ if (existing.belongsTo(options2.agent.id, options2.cwd, options2.additionalDirectories)) {
31858
+ this.#executionProcesses.touch(options2.sessionId, existing);
31843
31859
  return existing.prepared();
31844
31860
  }
31845
31861
  if (!existing.canRebind()) {
@@ -31849,85 +31865,91 @@ var AgentRuntimeManager = class {
31849
31865
  "session_identity_mismatch"
31850
31866
  );
31851
31867
  }
31852
- const runtime2 = await this.#replacePreparation(existing, options, metrics);
31868
+ const runtime2 = await this.#replacePreparation(existing, options2, metrics);
31853
31869
  return runtime2.prepared();
31854
31870
  }
31855
- const runtime = await this.#startPreparation(options, metrics);
31871
+ const runtime = await this.#startPreparation(options2, metrics);
31856
31872
  return runtime.prepared();
31857
- };
31873
+ });
31858
31874
  /**
31859
31875
  * Opens a short-lived native ACP session only to read the local Agent's
31860
31876
  * authoritative settings. Its identity and store are private to this
31861
31877
  * operation and never become a Mobius Session.
31862
31878
  */
31863
- inspectAgentSettings = async (options) => await this.#processes.runTransient(
31864
- options.signal,
31865
- async () => await AgentSession.start(
31866
- {
31867
- ...options,
31868
- sessionId: `agent-settings-${crypto.randomUUID()}`
31879
+ inspectAgentSettings = async (options) => this.#operations.run(options, async (options2) => {
31880
+ return await this.#processes.runTransient(
31881
+ options2.signal,
31882
+ async () => await AgentSession.start(
31883
+ {
31884
+ ...options2,
31885
+ sessionId: `agent-settings-${crypto.randomUUID()}`
31886
+ },
31887
+ () => this.#agentSettingsObserver,
31888
+ new MemoryAcpSessionStore()
31889
+ ),
31890
+ async (runtime) => {
31891
+ const prepared = runtime.prepared();
31892
+ const model = options2.model;
31893
+ const configOptions = model === void 0 ? prepared.configOptions : await runtime.configure({
31894
+ ...options2,
31895
+ booleanValue: false,
31896
+ configId: prepared.configOptions.find(
31897
+ (option) => option.id === "model" || option.category === "model"
31898
+ )?.id ?? "model",
31899
+ requestId: "agent-settings-model",
31900
+ sessionId: `agent-settings-${crypto.randomUUID()}`,
31901
+ value: model,
31902
+ valueType: "select"
31903
+ });
31904
+ return new AcpAgentSettingsSnapshot(prepared.agentName, prepared.agentVersion, configOptions);
31869
31905
  },
31870
- () => this.#agentSettingsObserver,
31871
- new MemoryAcpSessionStore()
31872
- ),
31873
- async (runtime) => {
31874
- const prepared = runtime.prepared();
31875
- const model = options.model;
31876
- const configOptions = model === void 0 ? prepared.configOptions : await runtime.configure({
31877
- ...options,
31878
- booleanValue: false,
31879
- configId: prepared.configOptions.find(
31880
- (option) => option.id === "model" || option.category === "model"
31881
- )?.id ?? "model",
31882
- requestId: "agent-settings-model",
31883
- sessionId: `agent-settings-${crypto.randomUUID()}`,
31884
- value: model,
31885
- valueType: "select"
31886
- });
31887
- return new AcpAgentSettingsSnapshot(prepared.agentName, prepared.agentVersion, configOptions);
31888
- },
31889
- async (runtime) => await runtime.close()
31890
- );
31906
+ async (runtime) => await runtime.close()
31907
+ );
31908
+ });
31891
31909
  /**
31892
31910
  * Retained for Agents that expose a separate persisted configuration channel.
31893
31911
  * Use inspectAgentSettings for Agent-scoped settings discovery.
31894
31912
  */
31895
- inspectSession = async (options, metrics = unmeasuredOperationMetrics) => {
31896
- if (this.#executionProcesses.pending(options.sessionId) !== void 0 || this.#executionProcesses.get(options.sessionId) !== void 0) {
31897
- throw new AcpExecutionError("The configuration inspection session is already in use", {}, "session_busy");
31913
+ inspectSession = async (options, metrics = unmeasuredOperationMetrics) => this.#operations.run(options, async (options2) => {
31914
+ if (this.#executionProcesses.pending(options2.sessionId) !== void 0 || this.#executionProcesses.get(options2.sessionId) !== void 0) {
31915
+ throw new AcpExecutionError(
31916
+ "The configuration inspection session is already in use",
31917
+ {},
31918
+ "session_busy"
31919
+ );
31898
31920
  }
31899
- return await this.#executionProcesses.use(options.sessionId, async () => {
31900
- const runtime = await this.#startPreparation(options, metrics);
31921
+ return await this.#executionProcesses.use(options2.sessionId, async () => {
31922
+ const runtime = await this.#startPreparation(options2, metrics);
31901
31923
  try {
31902
31924
  return runtime.prepared();
31903
31925
  } finally {
31904
31926
  try {
31905
- await this.#retireSessionProcess(options.sessionId, runtime, async () => await runtime.close());
31927
+ await this.#retireSessionProcess(options2.sessionId, runtime, async () => await runtime.close());
31906
31928
  } finally {
31907
- this.#store.remove(options.sessionId);
31929
+ this.#store.remove(options2.sessionId);
31908
31930
  }
31909
31931
  }
31910
31932
  });
31911
- };
31912
- prepareExecutionSession = async (options) => {
31913
- const pending = this.#executionProcesses.pending(options.sessionId);
31933
+ });
31934
+ prepareExecutionSession = async (options) => this.#operations.run(options, async (options2) => {
31935
+ const pending = this.#executionProcesses.pending(options2.sessionId);
31914
31936
  if (pending !== void 0) {
31915
- await pending;
31916
- return await this.prepareExecutionSession(options);
31937
+ await waitForPreparation(pending, options2.signal);
31938
+ return await this.prepareExecutionSession(options2);
31917
31939
  }
31918
- const existing = this.#executionProcesses.get(options.sessionId);
31940
+ const existing = this.#executionProcesses.get(options2.sessionId);
31919
31941
  if (existing === void 0) {
31920
- return (await this.#startPreparation(options)).prepared();
31942
+ return (await this.#startPreparation(options2)).prepared();
31921
31943
  }
31922
- if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31944
+ if (!existing.belongsTo(options2.agent.id, options2.cwd, options2.additionalDirectories)) {
31923
31945
  throw new AcpExecutionError(
31924
31946
  "The agent or workspace cannot change after a conversation has started",
31925
31947
  {},
31926
31948
  "session_identity_mismatch"
31927
31949
  );
31928
31950
  }
31929
- if (existing.usesEnvironment(options.agent.environment)) {
31930
- this.#executionProcesses.touch(options.sessionId, existing);
31951
+ if (existing.usesEnvironment(options2.agent.environment)) {
31952
+ this.#executionProcesses.touch(options2.sessionId, existing);
31931
31953
  return existing.prepared();
31932
31954
  }
31933
31955
  if (existing.hasRunningTurn()) {
@@ -31937,15 +31959,15 @@ var AgentRuntimeManager = class {
31937
31959
  "session_busy"
31938
31960
  );
31939
31961
  }
31940
- return (await this.#replaceExecutionPreparation(existing, options)).prepared();
31941
- };
31942
- prepareNativeSessionBrowser = (options) => this.#queryProcesses.prepare(options);
31943
- configureSession = async (options) => {
31944
- return await this.#executionProcesses.use(options.sessionId, async () => {
31945
- const runtime = await this.ensureSession(options);
31946
- return await runtime.configure(options);
31962
+ return (await this.#replaceExecutionPreparation(existing, options2)).prepared();
31963
+ });
31964
+ prepareNativeSessionBrowser = (options) => this.#operations.run(options, (scoped) => this.#queryProcesses.prepare(scoped));
31965
+ configureSession = async (options) => this.#operations.run(options, async (options2) => {
31966
+ return await this.#executionProcesses.use(options2.sessionId, async () => {
31967
+ const runtime = await this.ensureSession(options2);
31968
+ return await runtime.configure(options2);
31947
31969
  });
31948
- };
31970
+ });
31949
31971
  cancelPrompt = async (sessionId) => {
31950
31972
  await this.#executions.cancel(sessionId, async () => {
31951
31973
  const runtime = this.#executionProcesses.get(sessionId);
@@ -31960,19 +31982,19 @@ var AgentRuntimeManager = class {
31960
31982
  }
31961
31983
  });
31962
31984
  };
31963
- steerPrompt = async (options) => {
31964
- if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
31985
+ steerPrompt = async (options) => this.#operations.run(options, async (options2) => {
31986
+ if (this.#executions.command(options2.sessionId) !== options2.targetCommandId) {
31965
31987
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
31966
31988
  }
31967
- const pending = this.#executionProcesses.pending(options.sessionId);
31989
+ const pending = this.#executionProcesses.pending(options2.sessionId);
31968
31990
  if (pending !== void 0) {
31969
- await pending;
31991
+ await waitForPreparation(pending, options2.signal);
31970
31992
  }
31971
- if (this.#executions.command(options.sessionId) !== options.targetCommandId) {
31993
+ if (this.#executions.command(options2.sessionId) !== options2.targetCommandId) {
31972
31994
  throw new AcpExecutionError("The target turn is no longer active", {}, "steering_target_unavailable");
31973
31995
  }
31974
- const runtime = this.#executionProcesses.get(options.sessionId);
31975
- if (runtime === void 0 || !runtime.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
31996
+ const runtime = this.#executionProcesses.get(options2.sessionId);
31997
+ if (runtime === void 0 || !runtime.belongsTo(options2.agent.id, options2.cwd, options2.additionalDirectories)) {
31976
31998
  throw new AcpExecutionError(
31977
31999
  "The target turn execution context is unavailable",
31978
32000
  {},
@@ -31989,8 +32011,8 @@ var AgentRuntimeManager = class {
31989
32011
  deadline.unref();
31990
32012
  try {
31991
32013
  return await runtime.steer({
31992
- ...options,
31993
- signal: AbortSignal.any([options.signal, deadlineController.signal])
32014
+ ...options2,
32015
+ signal: AbortSignal.any([options2.signal, deadlineController.signal])
31994
32016
  });
31995
32017
  } catch (error62) {
31996
32018
  if (error62 instanceof AcpExecutionError) {
@@ -32000,44 +32022,44 @@ var AgentRuntimeManager = class {
32000
32022
  } finally {
32001
32023
  clearTimeout(deadline);
32002
32024
  }
32003
- };
32004
- runPrompt = async (options) => {
32005
- const stored = this.#store.read(options.sessionId);
32025
+ });
32026
+ runPrompt = async (options) => this.#operations.run(options, async (options2) => {
32027
+ const stored = this.#store.read(options2.sessionId);
32006
32028
  if (stored instanceof FoundAcpStoredSession && this.#nativeOperations.has(`${stored.session.agentId}:${stored.session.nativeSessionId}`)) {
32007
32029
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
32008
32030
  }
32009
- this.#executions.begin(options.sessionId, options.commandId);
32031
+ this.#executions.begin(options2.sessionId, options2.commandId);
32010
32032
  try {
32011
- if (this.#executions.cancellation(options.sessionId) !== void 0) {
32033
+ if (this.#executions.cancellation(options2.sessionId) !== void 0) {
32012
32034
  return { stopReason: "cancelled" };
32013
32035
  }
32014
- const runtime = await this.ensureSession(options);
32015
- if (this.#nativeOperations.has(`${options.agent.id}:${runtime.nativeSessionId()}`)) {
32036
+ const runtime = await this.ensureSession(options2);
32037
+ if (this.#nativeOperations.has(`${options2.agent.id}:${runtime.nativeSessionId()}`)) {
32016
32038
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
32017
32039
  }
32018
- if (this.#executions.cancellation(options.sessionId) !== void 0) {
32040
+ if (this.#executions.cancellation(options2.sessionId) !== void 0) {
32019
32041
  return { stopReason: "cancelled" };
32020
32042
  }
32021
32043
  try {
32022
- const result = await runtime.prompt(options);
32023
- return this.#executions.cancellation(options.sessionId) !== void 0 ? { stopReason: "cancelled" } : result;
32044
+ const result = await runtime.prompt(options2);
32045
+ return this.#executions.cancellation(options2.sessionId) !== void 0 ? { stopReason: "cancelled" } : result;
32024
32046
  } catch (error62) {
32025
- if (this.#executions.cancellation(options.sessionId) !== void 0) {
32026
- await this.#executions.cancellation(options.sessionId);
32047
+ if (this.#executions.cancellation(options2.sessionId) !== void 0) {
32048
+ await this.#executions.cancellation(options2.sessionId);
32027
32049
  return { stopReason: "cancelled" };
32028
32050
  }
32029
32051
  throw error62;
32030
32052
  }
32031
32053
  } catch (error62) {
32032
- const failedRuntime = this.#executionProcesses.get(options.sessionId);
32054
+ const failedRuntime = this.#executionProcesses.get(options2.sessionId);
32033
32055
  if (failedRuntime !== void 0) {
32034
32056
  await this.#retireSessionProcess(
32035
- options.sessionId,
32057
+ options2.sessionId,
32036
32058
  failedRuntime,
32037
32059
  async () => await failedRuntime.close()
32038
32060
  );
32039
32061
  }
32040
- if (this.#executions.cancellation(options.sessionId) !== void 0) {
32062
+ if (this.#executions.cancellation(options2.sessionId) !== void 0) {
32041
32063
  return { stopReason: "cancelled" };
32042
32064
  }
32043
32065
  if (error62 instanceof AcpExecutionError) {
@@ -32048,14 +32070,14 @@ var AgentRuntimeManager = class {
32048
32070
  });
32049
32071
  } finally {
32050
32072
  try {
32051
- await this.#executions.cancellation(options.sessionId);
32073
+ await this.#executions.cancellation(options2.sessionId);
32052
32074
  } finally {
32053
- this.#executions.finish(options.sessionId, options.commandId);
32054
- this.#executionProcesses.touch(options.sessionId);
32075
+ this.#executions.finish(options2.sessionId, options2.commandId);
32076
+ this.#executionProcesses.touch(options2.sessionId);
32055
32077
  this.#processes.notifyChanged();
32056
32078
  }
32057
32079
  }
32058
- };
32080
+ });
32059
32081
  respondPermission = async (options) => {
32060
32082
  const runtime = this.#executionProcesses.get(options.sessionId);
32061
32083
  if (runtime === void 0) {
@@ -32070,7 +32092,7 @@ var AgentRuntimeManager = class {
32070
32092
  }
32071
32093
  await runtime.respondElicitation(options);
32072
32094
  };
32073
- close = async () => {
32095
+ close = async () => this.#operations.close(async () => {
32074
32096
  const sessions = [...this.#executionProcesses.entries()];
32075
32097
  await Promise.all([
32076
32098
  ...sessions.map(
@@ -32078,23 +32100,23 @@ var AgentRuntimeManager = class {
32078
32100
  ),
32079
32101
  this.#queryProcesses.close()
32080
32102
  ]);
32081
- };
32103
+ });
32082
32104
  findSessions = (sessionIds) => sessionIds.flatMap((sessionId) => {
32083
32105
  const stored = this.#store.read(sessionId);
32084
32106
  return stored instanceof FoundAcpStoredSession ? [stored.session] : [];
32085
32107
  });
32086
32108
  findLinkedSessions = (agentId, nativeSessionId) => this.#store.findNative(agentId, nativeSessionId);
32087
32109
  hasPreparedSession = (sessionId, agentId, cwd, additionalDirectories) => this.#executionProcesses.get(sessionId)?.belongsTo(agentId, cwd, additionalDirectories) === true;
32088
- createNativeSession = async (options) => {
32110
+ createNativeSession = async (options) => this.#operations.run(options, async (options2) => {
32089
32111
  const temporarySessionId = `new-${crypto.randomUUID()}`;
32090
- const temporaryOptions = { ...options, sessionId: temporarySessionId };
32112
+ const temporaryOptions = { ...options2, sessionId: temporarySessionId };
32091
32113
  return await this.#executionProcesses.use(temporarySessionId, async () => {
32092
32114
  try {
32093
32115
  const runtime = await this.#startPreparation(temporaryOptions);
32094
32116
  let adopted = false;
32095
32117
  try {
32096
32118
  const nativeSessionId = runtime.nativeSessionId();
32097
- if (!isNativeChatSessionId(options.agent.id, nativeSessionId) || this.#executionProcesses.get(nativeSessionId) !== void 0 || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
32119
+ if (!isNativeChatSessionId(options2.agent.id, nativeSessionId) || this.#executionProcesses.get(nativeSessionId) !== void 0 || !(this.#store.read(nativeSessionId) instanceof MissingAcpStoredSession)) {
32098
32120
  throw new AcpExecutionError(
32099
32121
  "The Agent returned an invalid new Session ID",
32100
32122
  {},
@@ -32120,36 +32142,36 @@ var AgentRuntimeManager = class {
32120
32142
  this.#store.remove(temporarySessionId);
32121
32143
  }
32122
32144
  });
32123
- };
32124
- ensureSession = async (options) => {
32125
- const existing = this.#executionProcesses.get(options.sessionId);
32145
+ });
32146
+ ensureSession = async (options) => this.#operations.run(options, async (options2) => {
32147
+ const existing = this.#executionProcesses.get(options2.sessionId);
32126
32148
  if (existing !== void 0) {
32127
- if (!existing.belongsTo(options.agent.id, options.cwd, options.additionalDirectories)) {
32149
+ if (!existing.belongsTo(options2.agent.id, options2.cwd, options2.additionalDirectories)) {
32128
32150
  throw new AcpExecutionError(
32129
32151
  "The agent or workspace cannot change after a conversation has started",
32130
32152
  {},
32131
32153
  "session_identity_mismatch"
32132
32154
  );
32133
32155
  }
32134
- this.#executionProcesses.touch(options.sessionId, existing);
32156
+ this.#executionProcesses.touch(options2.sessionId, existing);
32135
32157
  return existing;
32136
32158
  }
32137
- await this.prepareSession(options);
32138
- const started = this.#executionProcesses.get(options.sessionId);
32159
+ await this.prepareSession(options2);
32160
+ const started = this.#executionProcesses.get(options2.sessionId);
32139
32161
  if (started === void 0) {
32140
32162
  throw new AcpExecutionError("ACP session did not start");
32141
32163
  }
32142
32164
  return started;
32143
- };
32144
- listNativeSessions = async (options, cursor) => {
32165
+ });
32166
+ listNativeSessions = async (options, cursor) => this.#operations.run(options, async (options2) => {
32145
32167
  return await this.#queryProcesses.use(
32146
- options,
32147
- async (runtime) => await runtime.listNativeSessions(cursor, options.signal)
32168
+ options2,
32169
+ async (runtime) => await runtime.listNativeSessions(cursor, options2.signal)
32148
32170
  );
32149
- };
32150
- previewNativeSession = async (options, nativeSessionId) => {
32151
- const key = `${options.agent.id}:${nativeSessionId}`;
32152
- const linked = this.#store.findNative(options.agent.id, nativeSessionId);
32171
+ });
32172
+ previewNativeSession = async (options, nativeSessionId) => this.#operations.run(options, async (options2) => {
32173
+ const key = `${options2.agent.id}:${nativeSessionId}`;
32174
+ const linked = this.#store.findNative(options2.agent.id, nativeSessionId);
32153
32175
  if (this.#nativeOperations.has(key) || linked.some(
32154
32176
  (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#executionProcesses.pending(entry.sessionId) !== void 0
32155
32177
  )) {
@@ -32161,18 +32183,18 @@ var AgentRuntimeManager = class {
32161
32183
  }
32162
32184
  this.#nativeOperations.add(key);
32163
32185
  try {
32164
- return await this.#queryProcesses.preview(options, nativeSessionId);
32186
+ return await this.#queryProcesses.preview(options2, nativeSessionId);
32165
32187
  } finally {
32166
32188
  this.#nativeOperations.delete(key);
32167
32189
  }
32168
- };
32169
- loadSessionHistory = async (options) => {
32170
- return await this.#executionProcesses.use(options.sessionId, async () => {
32171
- const runtime = await this.ensureSession(options);
32172
- const stored = this.#store.read(options.sessionId);
32190
+ });
32191
+ loadSessionHistory = async (options) => this.#operations.run(options, async (options2) => {
32192
+ return await this.#executionProcesses.use(options2.sessionId, async () => {
32193
+ const runtime = await this.ensureSession(options2);
32194
+ const stored = this.#store.read(options2.sessionId);
32173
32195
  if (stored instanceof FoundAcpStoredSession && this.#nativeHistoryReader !== null) {
32174
32196
  const history = await this.#nativeHistoryReader.read(
32175
- options,
32197
+ options2,
32176
32198
  stored.session.nativeSessionId,
32177
32199
  runtime.nativeHistoryTurn
32178
32200
  );
@@ -32180,15 +32202,15 @@ var AgentRuntimeManager = class {
32180
32202
  return history;
32181
32203
  }
32182
32204
  }
32183
- return await runtime.loadHistory(options.signal);
32205
+ return await runtime.loadHistory(options2.signal);
32184
32206
  });
32185
- };
32186
- openNativeSession = async (options, origin) => {
32187
- const key = `${options.agent.id}:${origin.nativeSessionId}`;
32207
+ });
32208
+ openNativeSession = async (options, origin) => this.#operations.run(options, async (options2) => {
32209
+ const key = `${options2.agent.id}:${origin.nativeSessionId}`;
32188
32210
  if (this.#nativeOperations.has(key)) {
32189
32211
  throw new AcpExecutionError("The native session is being managed", {}, "session_busy");
32190
32212
  }
32191
- const linked = this.#store.findNative(options.agent.id, origin.nativeSessionId);
32213
+ const linked = this.#store.findNative(options2.agent.id, origin.nativeSessionId);
32192
32214
  if (linked.some(
32193
32215
  (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#executionProcesses.pending(entry.sessionId) !== void 0
32194
32216
  )) {
@@ -32202,7 +32224,7 @@ var AgentRuntimeManager = class {
32202
32224
  if (origin.kind === "import" && existing !== void 0) {
32203
32225
  return await this.#executionProcesses.use(existing.sessionId, async () => {
32204
32226
  const runtime = await this.ensureSession({
32205
- ...options,
32227
+ ...options2,
32206
32228
  sessionId: existing.sessionId
32207
32229
  });
32208
32230
  if (this.#executions.command(existing.sessionId) !== void 0 || this.#nativeOperations.has(key)) {
@@ -32224,18 +32246,22 @@ var AgentRuntimeManager = class {
32224
32246
  }
32225
32247
  });
32226
32248
  }
32227
- 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) {
32228
- throw new AcpExecutionError("The destination conversation already exists", {}, "session_identity_mismatch");
32249
+ 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) {
32250
+ throw new AcpExecutionError(
32251
+ "The destination conversation already exists",
32252
+ {},
32253
+ "session_identity_mismatch"
32254
+ );
32229
32255
  }
32230
- return await this.#executionProcesses.use(options.sessionId, async () => {
32256
+ return await this.#executionProcesses.use(options2.sessionId, async () => {
32231
32257
  this.#nativeOperations.add(key);
32232
32258
  try {
32233
- const runtime = await this.#startPreparation({ ...options, origin });
32234
- const openedKey = `${options.agent.id}:${runtime.nativeSessionId()}`;
32259
+ const runtime = await this.#startPreparation({ ...options2, origin });
32260
+ const openedKey = `${options2.agent.id}:${runtime.nativeSessionId()}`;
32235
32261
  this.#nativeOperations.add(openedKey);
32236
32262
  try {
32237
32263
  return {
32238
- sessionId: options.sessionId,
32264
+ sessionId: options2.sessionId,
32239
32265
  prepared: runtime.prepared()
32240
32266
  };
32241
32267
  } finally {
@@ -32243,11 +32269,11 @@ var AgentRuntimeManager = class {
32243
32269
  this.#processes.notifyChanged();
32244
32270
  }
32245
32271
  } catch (error62) {
32246
- this.#store.remove(options.sessionId);
32247
- const runtime = this.#executionProcesses.get(options.sessionId);
32272
+ this.#store.remove(options2.sessionId);
32273
+ const runtime = this.#executionProcesses.get(options2.sessionId);
32248
32274
  if (runtime !== void 0) {
32249
32275
  await this.#retireSessionProcess(
32250
- options.sessionId,
32276
+ options2.sessionId,
32251
32277
  runtime,
32252
32278
  async () => await runtime.closeProcess()
32253
32279
  );
@@ -32257,11 +32283,11 @@ var AgentRuntimeManager = class {
32257
32283
  this.#nativeOperations.delete(key);
32258
32284
  }
32259
32285
  });
32260
- };
32261
- deleteNativeSession = async (options, nativeSessionId) => {
32262
- return await this.#queryProcesses.use(options, async (runtime) => {
32263
- const key = `${options.agent.id}:${nativeSessionId}`;
32264
- const linked = this.#store.findNative(options.agent.id, nativeSessionId);
32286
+ });
32287
+ deleteNativeSession = async (options, nativeSessionId) => this.#operations.run(options, async (options2) => {
32288
+ return await this.#queryProcesses.use(options2, async (runtime) => {
32289
+ const key = `${options2.agent.id}:${nativeSessionId}`;
32290
+ const linked = this.#store.findNative(options2.agent.id, nativeSessionId);
32265
32291
  if (this.#nativeOperations.has(key) || linked.some(
32266
32292
  (entry) => this.#executions.command(entry.sessionId) !== void 0 || this.#executionProcesses.pending(entry.sessionId) !== void 0
32267
32293
  )) {
@@ -32279,11 +32305,11 @@ var AgentRuntimeManager = class {
32279
32305
  await this.#retireSessionProcess(entry.sessionId, active, async () => await active.close());
32280
32306
  }
32281
32307
  }
32282
- options.signal.throwIfAborted();
32283
- await runtime.deleteNativeSession(nativeSessionId, options.signal);
32284
- const deleted = this.#store.markDeleted(options.agent.id, nativeSessionId);
32308
+ options2.signal.throwIfAborted();
32309
+ await runtime.deleteNativeSession(nativeSessionId, options2.signal);
32310
+ const deleted = this.#store.markDeleted(options2.agent.id, nativeSessionId);
32285
32311
  if (runtime.nativeSessionId() === nativeSessionId) {
32286
- await this.#queryProcesses.retire(options.sessionId, runtime);
32312
+ await this.#queryProcesses.retire(options2.sessionId, runtime);
32287
32313
  }
32288
32314
  return deleted;
32289
32315
  } finally {
@@ -32291,7 +32317,7 @@ var AgentRuntimeManager = class {
32291
32317
  this.#processes.notifyChanged();
32292
32318
  }
32293
32319
  });
32294
- };
32320
+ });
32295
32321
  };
32296
32322
 
32297
32323
  // src/acp/run-prompt.ts
@@ -34025,6 +34051,7 @@ var CodexAcpAgent = class {
34025
34051
  #settings;
34026
34052
  #nativeSessionId = "";
34027
34053
  #streamedReasoningItemIds = /* @__PURE__ */ new Set();
34054
+ #streamedAgentMessageItemIds = /* @__PURE__ */ new Set();
34028
34055
  #turn = null;
34029
34056
  #nativeTurnId = null;
34030
34057
  #updateTail = Promise.resolve();
@@ -34334,6 +34361,20 @@ var CodexAcpAgent = class {
34334
34361
  }
34335
34362
  return;
34336
34363
  }
34364
+ if (method === "item/agentMessage/delta") {
34365
+ const decoded = reasoningTextDeltaParamsSchema.safeParse(params);
34366
+ if (decoded.success && decoded.data.threadId === this.#nativeSessionId && this.#turn !== null && decoded.data.turnId === this.#turn.turn.id && decoded.data.delta.length > 0) {
34367
+ this.#streamedAgentMessageItemIds.add(decoded.data.itemId);
34368
+ await this.#enqueue(async () => {
34369
+ await this.#emit({
34370
+ content: { text: decoded.data.delta, type: "text" },
34371
+ messageId: decoded.data.itemId,
34372
+ sessionUpdate: "agent_message_chunk"
34373
+ });
34374
+ });
34375
+ }
34376
+ return;
34377
+ }
34337
34378
  if (method !== "item/started" && method !== "item/completed") {
34338
34379
  return;
34339
34380
  }
@@ -34342,7 +34383,7 @@ var CodexAcpAgent = class {
34342
34383
  return;
34343
34384
  }
34344
34385
  if (item.type === "agentMessage") {
34345
- if (method === "item/started") {
34386
+ if (method === "item/started" || this.#streamedAgentMessageItemIds.delete(item.id)) {
34346
34387
  return;
34347
34388
  }
34348
34389
  await this.#enqueue(async () => {
@@ -34505,6 +34546,7 @@ var CodexAcpAgent = class {
34505
34546
  };
34506
34547
  signal.addEventListener("abort", aborted2, { once: true });
34507
34548
  const turn = { completion, turn: { id: "" } };
34549
+ this.#streamedAgentMessageItemIds.clear();
34508
34550
  this.#streamedReasoningItemIds.clear();
34509
34551
  this.#turn = turn;
34510
34552
  this.#nativeTurnId = null;
@@ -34529,6 +34571,7 @@ var CodexAcpAgent = class {
34529
34571
  signal.removeEventListener("abort", aborted2);
34530
34572
  if (this.#turn === turn) {
34531
34573
  this.#turn = null;
34574
+ this.#streamedAgentMessageItemIds.clear();
34532
34575
  this.#streamedReasoningItemIds.clear();
34533
34576
  }
34534
34577
  }