@moxt-ai/mobius 0.0.79 → 0.0.80

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
@@ -1307,6 +1307,20 @@ var AcpConfigurationValueUnavailableError = class extends AcpExecutionError {
1307
1307
  }
1308
1308
  };
1309
1309
 
1310
+ // src/acp/in-process/acp-endpoint.ts
1311
+ function acpEndpointTerminationError(termination) {
1312
+ switch (termination.kind) {
1313
+ case "failed":
1314
+ return termination.error;
1315
+ case "closed":
1316
+ return new AcpExecutionError("The ACP endpoint is closed", {}, "acp_endpoint_closed");
1317
+ default: {
1318
+ const unreachable = termination;
1319
+ return unreachable;
1320
+ }
1321
+ }
1322
+ }
1323
+
1310
1324
  // src/acp/codex/app-server-client.ts
1311
1325
  import { spawn as spawn2 } from "node:child_process";
1312
1326
 
@@ -21201,11 +21215,9 @@ var threadSettingsUpdatedSchema = external_exports.object({
21201
21215
  // src/acp/codex/app-server-client.ts
21202
21216
  var INITIALIZE_TIMEOUT_MILLISECONDS = 3e4;
21203
21217
  var OUTPUT_LIMIT_BYTES = 32 * 1024 * 1024;
21204
- var CodexAppServerError = class extends Error {
21205
- code;
21218
+ var CodexAppServerError = class extends AcpExecutionError {
21206
21219
  constructor(code, message, options = {}) {
21207
- super(message, options);
21208
- this.code = code;
21220
+ super(message, options, code);
21209
21221
  this.name = "CodexAppServerError";
21210
21222
  }
21211
21223
  };
@@ -21213,12 +21225,14 @@ var CodexAppServerClient = class {
21213
21225
  #child;
21214
21226
  #handlers;
21215
21227
  #pending = /* @__PURE__ */ new Map();
21216
- #processExit;
21217
- #failure = null;
21228
+ #termination = Promise.withResolvers();
21229
+ terminated = this.#termination.promise;
21230
+ #lifecycle = {
21231
+ kind: "open"
21232
+ };
21218
21233
  #nextRequestId = 1;
21219
21234
  #pendingBuffer = "";
21220
21235
  #receivedBytes = 0;
21221
- #closed = false;
21222
21236
  constructor(options) {
21223
21237
  this.#handlers = options.handlers;
21224
21238
  this.#child = spawn2(options.command, ["app-server"], {
@@ -21243,21 +21257,56 @@ var CodexAppServerClient = class {
21243
21257
  stderr.on("data", (chunk) => {
21244
21258
  process.stderr.write(`[codex-app-server] ${chunk}`);
21245
21259
  });
21246
- this.#processExit = new Promise((resolve8) => {
21247
- this.#child.once("error", resolve8);
21248
- this.#child.once("exit", resolve8);
21260
+ this.#child.once("error", (cause) => {
21261
+ this.#fail(
21262
+ new CodexAppServerError("codex_app_server_exited", "The Codex app-server could not run", { cause })
21263
+ );
21264
+ });
21265
+ this.#child.once("exit", (code, signal) => {
21266
+ this.#fail(
21267
+ new CodexAppServerError("codex_app_server_exited", "The Codex app-server stopped", {
21268
+ cause: { code, signal }
21269
+ })
21270
+ );
21271
+ });
21272
+ for (const stream of [stdin, stdout, stderr]) {
21273
+ stream.on("error", (cause) => {
21274
+ this.#fail(
21275
+ new CodexAppServerError("codex_app_server_io_error", "The Codex app-server connection failed", {
21276
+ cause
21277
+ })
21278
+ );
21279
+ });
21280
+ }
21281
+ stdout.once("end", () => {
21282
+ this.#fail(new CodexAppServerError("codex_app_server_exited", "The Codex app-server output closed"));
21249
21283
  });
21250
21284
  }
21251
21285
  child = () => this.#child;
21252
21286
  #fail = (error62) => {
21253
- this.#failure ??= error62;
21287
+ this.#terminate({ kind: "failed", error: error62 });
21288
+ };
21289
+ #terminate = (result) => {
21290
+ if (this.#lifecycle.kind === "terminated") {
21291
+ return;
21292
+ }
21293
+ this.#lifecycle = { kind: "terminated", result };
21294
+ this.#termination.resolve(result);
21295
+ const error62 = acpEndpointTerminationError(result);
21254
21296
  for (const [id, pending] of this.#pending) {
21255
21297
  this.#pending.delete(id);
21256
21298
  pending.completion.reject(error62);
21257
21299
  }
21258
21300
  };
21259
- #exitedFailure = (method) => this.#failure ?? new CodexAppServerError("codex_app_server_exited", `The Codex app-server stopped during ${method}`);
21301
+ #requireOpen = () => {
21302
+ if (this.#lifecycle.kind === "terminated") {
21303
+ throw acpEndpointTerminationError(this.#lifecycle.result);
21304
+ }
21305
+ };
21260
21306
  #consume = (chunk) => {
21307
+ if (this.#lifecycle.kind === "terminated") {
21308
+ return;
21309
+ }
21261
21310
  try {
21262
21311
  this.#receivedBytes += Buffer.byteLength(chunk);
21263
21312
  if (this.#receivedBytes > OUTPUT_LIMIT_BYTES) {
@@ -21286,7 +21335,6 @@ var CodexAppServerClient = class {
21286
21335
  { cause: error62 }
21287
21336
  )
21288
21337
  );
21289
- this.#child.kill("SIGKILL");
21290
21338
  }
21291
21339
  };
21292
21340
  // Handler failures are recorded as the app-server's terminal failure: an ACP client that cannot
@@ -21336,9 +21384,7 @@ var CodexAppServerClient = class {
21336
21384
  /** Sends a request and waits for its response, bounded by the caller's cancellation signal. */
21337
21385
  request = async (method, params, signal) => {
21338
21386
  signal.throwIfAborted();
21339
- if (this.#closed) {
21340
- throw new CodexAppServerError("codex_app_server_closed", "The Codex app-server is closed");
21341
- }
21387
+ this.#requireOpen();
21342
21388
  const id = this.#nextRequestId;
21343
21389
  this.#nextRequestId += 1;
21344
21390
  const completion = Promise.withResolvers();
@@ -21351,7 +21397,7 @@ var CodexAppServerClient = class {
21351
21397
  signal.addEventListener("abort", abort, { once: true });
21352
21398
  try {
21353
21399
  this.#write({ id, method, params });
21354
- return await Promise.race([completion.promise, this.#processExit.then(() => this.#exitedFailure(method))]);
21400
+ return await completion.promise;
21355
21401
  } finally {
21356
21402
  signal.removeEventListener("abort", abort);
21357
21403
  this.#pending.delete(id);
@@ -21367,12 +21413,12 @@ var CodexAppServerClient = class {
21367
21413
  this.#write({ error: { code, message }, id });
21368
21414
  };
21369
21415
  #write = (message) => {
21370
- if (this.#closed) {
21371
- return;
21372
- }
21416
+ this.#requireOpen();
21373
21417
  const stdin = this.#child.stdin;
21374
21418
  if (stdin === null || stdin.destroyed) {
21375
- throw new CodexAppServerError("codex_app_server_closed", "The Codex app-server input is closed");
21419
+ const error62 = new CodexAppServerError("codex_app_server_io_error", "The Codex app-server input is closed");
21420
+ this.#fail(error62);
21421
+ throw error62;
21376
21422
  }
21377
21423
  stdin.write(`${JSON.stringify(message)}
21378
21424
  `);
@@ -21395,15 +21441,7 @@ var CodexAppServerClient = class {
21395
21441
  * app-server and every tool subprocess it started.
21396
21442
  */
21397
21443
  detach = () => {
21398
- if (this.#closed) {
21399
- return;
21400
- }
21401
- this.#closed = true;
21402
- this.#fail(new CodexAppServerError("codex_app_server_closed", "The Codex app-server is closed"));
21403
- const stdin = this.#child.stdin;
21404
- if (stdin !== null && !stdin.destroyed) {
21405
- stdin.end();
21406
- }
21444
+ this.#terminate({ kind: "closed" });
21407
21445
  };
21408
21446
  };
21409
21447
 
@@ -21745,6 +21783,8 @@ function userInputSchema(questions) {
21745
21783
  var CodexAcpAgent = class {
21746
21784
  #app;
21747
21785
  #appServer;
21786
+ terminated;
21787
+ #closePromise = null;
21748
21788
  #connection = null;
21749
21789
  #models = [];
21750
21790
  #nativeSessionId = "";
@@ -21782,6 +21822,15 @@ var CodexAcpAgent = class {
21782
21822
  }
21783
21823
  }
21784
21824
  });
21825
+ this.terminated = this.#appServer.terminated;
21826
+ void this.terminated.then((termination) => {
21827
+ this.#turn?.completion.reject(acpEndpointTerminationError(termination));
21828
+ if (termination.kind === "failed") {
21829
+ void this.close().catch((cause) => {
21830
+ console.error("The failed Codex endpoint could not release its process tree", cause);
21831
+ });
21832
+ }
21833
+ });
21785
21834
  this.#app = createAgentApp({ name: "mobius-codex" }).onConnect((connection) => {
21786
21835
  this.#connection = connection;
21787
21836
  }).onRequest(methods.agent.initialize, async () => ({
@@ -21893,11 +21942,20 @@ var CodexAcpAgent = class {
21893
21942
  * Releases the app-server and every process it started. Mirrors the adapter path's teardown so a
21894
21943
  * cancelled or closed session cannot leak the tool subprocesses running under the app-server.
21895
21944
  */
21896
- close = async () => {
21897
- const child = this.#appServer.child();
21898
- const captured = await captureProcessTree(child);
21899
- this.#appServer.detach();
21900
- await stopProcessTree(child, captured);
21945
+ close = () => {
21946
+ if (this.#closePromise === null) {
21947
+ this.#appServer.detach();
21948
+ this.#closePromise = (async () => {
21949
+ const child = this.#appServer.child();
21950
+ try {
21951
+ const captured = await captureProcessTree(child);
21952
+ await stopProcessTree(child, captured);
21953
+ } finally {
21954
+ this.#connection?.close();
21955
+ }
21956
+ })();
21957
+ }
21958
+ return this.#closePromise;
21901
21959
  };
21902
21960
  /**
21903
21961
  * Reads the model catalog once per app-server. A failure leaves the catalog empty, which costs
@@ -22141,7 +22199,7 @@ var CodexAcpAgent = class {
22141
22199
  if (method === "turn/started") {
22142
22200
  const decoded = turnNotificationSchema.safeParse(params);
22143
22201
  if (decoded.success && decoded.data.threadId === this.#nativeSessionId && this.#turn !== null) {
22144
- this.#turn = { completion: this.#turn.completion, turn: { id: decoded.data.turn.id } };
22202
+ this.#turn.turn.id = decoded.data.turn.id;
22145
22203
  }
22146
22204
  return;
22147
22205
  }
@@ -22330,10 +22388,20 @@ var CodexAcpAgent = class {
22330
22388
  this.#appServer.respond(request.id, { action: response.action, content: content3 });
22331
22389
  };
22332
22390
  #runTurn = async (input2, signal) => {
22391
+ signal.throwIfAborted();
22333
22392
  if (this.#turn !== null) {
22334
22393
  throw new AcpExecutionError("Another prompt is already running in this conversation", {}, "session_busy");
22335
22394
  }
22336
22395
  const completion = Promise.withResolvers();
22396
+ void completion.promise.catch(() => {
22397
+ });
22398
+ const aborted2 = () => {
22399
+ completion.reject(signal.reason);
22400
+ void this.close().catch((cause) => {
22401
+ console.error("The cancelled Codex endpoint could not release its process tree", cause);
22402
+ });
22403
+ };
22404
+ signal.addEventListener("abort", aborted2, { once: true });
22337
22405
  const turn = { completion, turn: { id: "" } };
22338
22406
  this.#turn = turn;
22339
22407
  try {
@@ -22379,6 +22447,7 @@ var CodexAcpAgent = class {
22379
22447
  }
22380
22448
  return await completion.promise;
22381
22449
  } finally {
22450
+ signal.removeEventListener("abort", aborted2);
22382
22451
  if (this.#turn === turn) {
22383
22452
  this.#turn = null;
22384
22453
  }
@@ -22407,7 +22476,6 @@ var CodexAcpAgent = class {
22407
22476
  if (active === null) {
22408
22477
  return;
22409
22478
  }
22410
- this.#turn = null;
22411
22479
  await this.#updateTail;
22412
22480
  if (turn.status === "failed") {
22413
22481
  active.completion.reject(
@@ -23529,24 +23597,40 @@ async function probeInProcessReadiness(agent, environment, defaultWorkspace, con
23529
23597
  environment: { ...filterEnvironment(environment), ...agent.environment }
23530
23598
  });
23531
23599
  try {
23532
- await inProcess.initialize(controller.signal);
23533
23600
  const connection = createAcpClient(new DiscoveryAcpHandler()).connect(inProcess.app());
23534
- const initialized = await connection.agent.request(
23535
- methods6.agent.initialize,
23536
- {
23537
- clientCapabilities: {},
23538
- clientInfo: { name: "mobius", version: "0.0.3" },
23539
- protocolVersion: PROTOCOL_VERSION2
23540
- },
23541
- { cancellationSignal: controller.signal }
23542
- );
23543
- if (initialized.protocolVersion !== PROTOCOL_VERSION2) {
23544
- throw new AgentProbeError("acp_incompatible", "ACP agent selected an unsupported protocol version");
23601
+ try {
23602
+ return await Promise.race([
23603
+ (async () => {
23604
+ await inProcess.initialize(controller.signal);
23605
+ const initialized = await connection.agent.request(
23606
+ methods6.agent.initialize,
23607
+ {
23608
+ clientCapabilities: {},
23609
+ clientInfo: { name: "mobius", version: "0.0.3" },
23610
+ protocolVersion: PROTOCOL_VERSION2
23611
+ },
23612
+ { cancellationSignal: controller.signal }
23613
+ );
23614
+ if (initialized.protocolVersion !== PROTOCOL_VERSION2) {
23615
+ throw new AgentProbeError(
23616
+ "acp_incompatible",
23617
+ "ACP agent selected an unsupported protocol version"
23618
+ );
23619
+ }
23620
+ return new VerifiedAcpReadiness(
23621
+ normalizeVersionOutput(initialized.agentInfo?.version ?? ""),
23622
+ normalizeAcpCapabilities(initialized)
23623
+ );
23624
+ })(),
23625
+ inProcess.terminated.then((termination) => {
23626
+ throw new AgentProbeError("acp_incompatible", "The ACP endpoint stopped during initialization", {
23627
+ cause: acpEndpointTerminationError(termination)
23628
+ });
23629
+ })
23630
+ ]);
23631
+ } finally {
23632
+ connection.close();
23545
23633
  }
23546
- return new VerifiedAcpReadiness(
23547
- normalizeVersionOutput(initialized.agentInfo?.version ?? ""),
23548
- normalizeAcpCapabilities(initialized)
23549
- );
23550
23634
  } finally {
23551
23635
  await inProcess.close();
23552
23636
  }
@@ -35496,8 +35580,8 @@ var AcpProcessSession = class _AcpProcessSession {
35496
35580
  #pendingElicitations = /* @__PURE__ */ new Map();
35497
35581
  #pendingPermissions = /* @__PURE__ */ new Map();
35498
35582
  #preSessionUpdates = [];
35499
- // The adapter-process topology only. An in-process agent owns the app-server child itself.
35500
- #processExit;
35583
+ // Both topologies report a terminal outcome, even after a prompt's initial RPC is acknowledged.
35584
+ #termination;
35501
35585
  #runtimeChild;
35502
35586
  #metrics;
35503
35587
  #sessionConfigProtocol;
@@ -35541,8 +35625,7 @@ var AcpProcessSession = class _AcpProcessSession {
35541
35625
  });
35542
35626
  this.#inProcessAgent = inProcess;
35543
35627
  this.#runtimeChild = null;
35544
- this.#processExit = new Promise(() => {
35545
- });
35628
+ this.#termination = inProcess.terminated;
35546
35629
  this.#connection = createAcpClient(this).connect(inProcess.app());
35547
35630
  return;
35548
35631
  }
@@ -35566,9 +35649,16 @@ var AcpProcessSession = class _AcpProcessSession {
35566
35649
  stderr.on("data", (chunk) => {
35567
35650
  process.stderr.write(`[${agent.id}-acp] ${chunk}`);
35568
35651
  });
35569
- this.#processExit = new Promise((resolve8) => {
35570
- child.once("error", resolve8);
35571
- child.once("exit", resolve8);
35652
+ this.#termination = new Promise((resolve8) => {
35653
+ child.once("error", (cause) => {
35654
+ resolve8({ kind: "failed", error: new AcpExecutionError("The ACP process could not run", { cause }) });
35655
+ });
35656
+ child.once("exit", (code, signal) => {
35657
+ resolve8({
35658
+ kind: "failed",
35659
+ error: new AcpExecutionError("The ACP process stopped", { cause: { code, signal } })
35660
+ });
35661
+ });
35572
35662
  });
35573
35663
  const stream = ndJsonStream2(createWritableAcpStream(stdin), readableWebStream(stdout));
35574
35664
  this.#connection = createAcpClient(this).connect(stream);
@@ -36710,8 +36800,9 @@ var AcpProcessSession = class _AcpProcessSession {
36710
36800
  try {
36711
36801
  return await Promise.race([
36712
36802
  operation,
36713
- this.#processExit.then(() => {
36714
- throw new AcpExecutionError(exitMessage);
36803
+ this.#termination.then((termination) => {
36804
+ const cause = acpEndpointTerminationError(termination);
36805
+ throw new AcpExecutionError(exitMessage, { cause }, cause.code);
36715
36806
  }),
36716
36807
  aborted2
36717
36808
  ]);