@coda-console/codex 1.14.1 → 1.14.2

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.
@@ -110,10 +110,30 @@ export function startAccountBridge({ source = new AccountSource(accountSource())
110
110
  });
111
111
  async function close() { if (closing) return; closing = true; reader.close(); source.close(); await server.close(); }
112
112
  reader.on('close', () => void close());
113
- void server.exited.then(() => reader.close());
114
- return { close, server, settled: () => queue };
113
+ // `exit` is how the app-server ended, `{ code, signal, error? }`, when nothing here closed it: its process died. When the
114
+ // bridge was closed first (its client went away, or it was told to stop), the server ending is the close, and it is null.
115
+ let settleExit;
116
+ const exit = new Promise(resolve => { settleExit = resolve; });
117
+ void server.exited.then(result => { settleExit(closing ? null : result ?? { code: 0, signal: null }); void close(); });
118
+ return { close, server, settled: () => queue, exit };
115
119
  }
116
- if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
117
- const bridge = startAccountBridge();
118
- for (const signal of ['SIGTERM', 'SIGINT']) process.on(signal, () => void bridge.close());
120
+
121
+ // The bridge as a process: it ends as the app-server under it ended, by the same signal or with the same exit code, so what
122
+ // runs it (codex-acp, and Coda's exit patch there) sees how the Codex died rather than a clean exit.
123
+ export function runAccountBridge(options) {
124
+ const bridge = startAccountBridge(options);
125
+ const signals = ['SIGTERM', 'SIGINT'], stop = () => void bridge.close();
126
+ for (const signal of signals) process.on(signal, stop);
127
+ void bridge.exit.then(async result => {
128
+ if (!result) return;
129
+ await bridge.close();
130
+ if (result.error) process.stderr.write(`Codex 无法启动:${result.error.code || result.error.message}\n`);
131
+ if (result.signal) {
132
+ for (const signal of signals) process.off(signal, stop);
133
+ process.exitCode = 1;
134
+ process.kill(process.pid, result.signal);
135
+ } else process.exit(result.code ?? 1);
136
+ });
137
+ return bridge;
119
138
  }
139
+ if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) runAccountBridge();
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "id": "codex",
4
4
  "displayName": "Codex",
5
5
  "description": "用 OpenAI Codex 跑会话",
6
- "version": "1.14.1",
6
+ "version": "1.14.2",
7
7
  "categories": [
8
8
  "Agent"
9
9
  ],
@@ -24,12 +24,15 @@ export class AppServer {
24
24
  if (message.error) waiter.reject(new Error(oneLine(message.error.message) || 'Codex 拒绝了请求'));
25
25
  else waiter.resolve(message.result);
26
26
  });
27
+ // Resolves how it ended: `{ code, signal }` as the process exited, or, if it could not be started at all (no such
28
+ // command), `{ code: 1, signal: null, error }`.
27
29
  this.exited = new Promise(resolve => {
28
- const done = () => {
30
+ const done = result => {
29
31
  for (const waiter of this.pending.values()) { clearTimeout(waiter.timer); waiter.reject(new Error('Codex 提前退出了')); }
30
- this.pending.clear(); resolve();
32
+ this.pending.clear(); resolve(result);
31
33
  };
32
- this.child.once('error', done); this.child.once('exit', done);
34
+ this.child.once('error', error => done({ code: 1, signal: null, error }));
35
+ this.child.once('exit', (code, signal) => done({ code, signal }));
33
36
  });
34
37
  }
35
38
  send(message) { this.child.stdin.write(`${JSON.stringify(message)}\n`); }