@coda-console/codex 1.14.0 → 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.
- package/agent/accounts.mjs +25 -5
- package/agent/entry.mjs +11 -2
- package/agent/process-exit.mjs +26 -6
- package/agent/waits.mjs +15 -1
- package/package.json +1 -1
- package/scripts/app-server.mjs +6 -3
package/agent/accounts.mjs
CHANGED
|
@@ -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
|
-
|
|
114
|
-
|
|
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
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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/agent/entry.mjs
CHANGED
|
@@ -16,7 +16,7 @@ import { accountSource } from './account-source.mjs';
|
|
|
16
16
|
import { readLines } from './lines.mjs';
|
|
17
17
|
import { orderedCodexTitles } from './titles.mjs';
|
|
18
18
|
import { continuedCodexSubagents } from './subagents.mjs';
|
|
19
|
-
import { followCodexProcessExit } from './process-exit.mjs';
|
|
19
|
+
import { describeExit, followCodexProcessExit, listenExit } from './process-exit.mjs';
|
|
20
20
|
import { networkedCodexDevelopment } from './network.mjs';
|
|
21
21
|
import { codexOptionsAsShown } from './options.mjs';
|
|
22
22
|
import { instructedCodexThreads } from './instructions.mjs';
|
|
@@ -67,13 +67,22 @@ const pass = ({ agent, coda }) => { for (const each of agent) toCodex(each); for
|
|
|
67
67
|
listen(line => {
|
|
68
68
|
for (const message of waits.fromAgent(parse(line))) for (const told of unrecognized.fromAgent(message)) for (const each of reviews.fromAgent(told)) pass(questions.fromAgent(each));
|
|
69
69
|
});
|
|
70
|
+
// The Codex process ended on its own, not as Coda's input closing stops it: the log and the turns waiting on it say how
|
|
71
|
+
// (process-exit.mjs).
|
|
72
|
+
let stopping = false;
|
|
73
|
+
listenExit(exit => {
|
|
74
|
+
if (stopping) return;
|
|
75
|
+
const message = describeExit(exit);
|
|
76
|
+
process.stderr.write(`${message}\n`);
|
|
77
|
+
for (const each of waits.fail(message)) say(each);
|
|
78
|
+
});
|
|
70
79
|
listenUnrecognized(report => { for (const each of unrecognized.report({ ...report, sessionId: report.sessionId ?? waits.root })) say(each); });
|
|
71
80
|
readLines(process.stdin).on('line', line => {
|
|
72
81
|
const message = parse(line);
|
|
73
82
|
if (!message || typeof message !== 'object') return;
|
|
74
83
|
unrecognized.fromCoda(message);
|
|
75
84
|
pass(questions.fromCoda(message));
|
|
76
|
-
}).on('close', () => input.end());
|
|
85
|
+
}).on('close', () => { stopping = true; input.end(); });
|
|
77
86
|
|
|
78
87
|
// codex-acp as Node finds it from here (its package names dist/index.js as its main): the plugin's own dependency.
|
|
79
88
|
const adapter = new URL(import.meta.resolve('@agentclientprotocol/codex-acp'));
|
package/agent/process-exit.mjs
CHANGED
|
@@ -1,17 +1,37 @@
|
|
|
1
1
|
// codex-acp 1.12.0 outlives its Codex app-server. When that child exits on its own, the adapter only
|
|
2
2
|
// fails background tasks: a started turn keeps waiting for a completion that never comes, and every
|
|
3
3
|
// later request fails against the dead child. Follow the child out instead, so the session host reports
|
|
4
|
-
// an agent exit
|
|
5
|
-
//
|
|
6
|
-
|
|
4
|
+
// an agent exit. A deliberate provider restart moves the generation first, and stays unaffected. How the child exited (code or
|
|
5
|
+
// signal, and the end of its stderr) is told to the entry first, which logs it and answers the turns still waiting with it, so
|
|
6
|
+
// the backend settles them with that error. Fails loudly once the adapter changes, `getRecentStderr` included, which the patch calls
|
|
7
|
+
// from the exit callback.
|
|
8
|
+
const before = `.once("exit", () => {
|
|
9
|
+
if (generation !== this.codexProcessGeneration) return;
|
|
7
10
|
void this.finishAllAsyncTasks("failed", "after the Codex process exited");`;
|
|
8
11
|
// Exit only after stdout has flushed the failed background tasks.
|
|
9
|
-
const after =
|
|
12
|
+
const after = `.once("exit", (code, signal) => {
|
|
13
|
+
if (generation !== this.codexProcessGeneration) return;
|
|
14
|
+
codaProcessExit.exited({ code, signal, stderr: this.getRecentStderr() });
|
|
10
15
|
void this.finishAllAsyncTasks("failed", "after the Codex process exited").finally(() => {
|
|
11
16
|
logger.log("Codex process exited; ending the adapter with it");
|
|
12
17
|
process.stdout.write("", () => process.exit(1));
|
|
13
18
|
});`;
|
|
14
19
|
export function followCodexProcessExit(source) {
|
|
15
|
-
if (source.split(before).length !== 2) throw new Error('codex-acp process exit patch no longer matches the locked adapter');
|
|
16
|
-
return source.replace(before, after)
|
|
20
|
+
if (source.split(before).length !== 2 || !source.includes('this.getRecentStderr = ')) throw new Error('codex-acp process exit patch no longer matches the locked adapter');
|
|
21
|
+
return `import * as codaProcessExit from ${JSON.stringify(import.meta.url)};\n${source.replace(/^#![^\n]*\n/, '').replace(before, after)}`;
|
|
17
22
|
}
|
|
23
|
+
|
|
24
|
+
let heard = () => {};
|
|
25
|
+
export const listenExit = listener => { heard = listener; };
|
|
26
|
+
export const exited = exit => heard(exit);
|
|
27
|
+
// What Coda's turn error and the log say of it: how the Codex process ended, and the last lines it wrote to stderr.
|
|
28
|
+
export function describeExit({ code, signal, stderr }) {
|
|
29
|
+
const tail = stderr.trim().split('\n').slice(-5).join('\n').slice(-1000);
|
|
30
|
+
return `Codex 进程意外退出(${signal ? `被信号 ${signal} 终止` : `退出码 ${code}`})${tail ? `,stderr 末尾:\n${tail}` : ''}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// The error that ends a prompt as the process ends (docs/agent-protocol.md「原生会话已结束」): the category tells Coda the native
|
|
34
|
+
// session is over, not that the service failed. `input` is not written: nothing the
|
|
35
|
+
// process said proves this prompt was read, and Coda then resumes by checking first, with the whole original input.
|
|
36
|
+
export const endedError = message => ({ code: -32603, message,
|
|
37
|
+
data: { _meta: { coda: { failure: { kind: 'session_ended' } } } } });
|
package/agent/waits.mjs
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// here and handed to the patched codex-acp in the request's `_meta` (instructions.mjs).
|
|
6
6
|
import { INSTRUCTIONS, sessionInstructions } from './instructions.mjs';
|
|
7
7
|
import { sleepTitle } from './sleep.mjs';
|
|
8
|
+
import { endedError } from './process-exit.mjs';
|
|
8
9
|
|
|
9
10
|
const record = value => value && typeof value === 'object' && !Array.isArray(value);
|
|
10
11
|
const string = value => typeof value === 'string' && value.length > 0;
|
|
@@ -51,16 +52,29 @@ export class CodexWaits {
|
|
|
51
52
|
if (wait?.stoppable) return { agent: [{ jsonrpc: '2.0', method: 'session/cancel', params: { sessionId: wait.subagentSessionId } }], coda: [{ jsonrpc: '2.0', id, result: {} }] };
|
|
52
53
|
return { agent: [], coda: [{ jsonrpc: '2.0', id, error: { code: -32602, message: '没有可以停止的等待项' } }] };
|
|
53
54
|
}
|
|
55
|
+
// The Codex process is gone: answer every prompt still waiting with the error `message`, as a session that ended. A later answer of codex-acp's own to one of them (its pending requests failing
|
|
56
|
+
// as the connection closes) is dropped.
|
|
57
|
+
fail(message) {
|
|
58
|
+
const answers = [];
|
|
59
|
+
for (const [id, asked] of this.asked) if (asked?.prompt) {
|
|
60
|
+
answers.push(...this.finishSleeps(base(asked.prompt), 'failed'), { jsonrpc: '2.0', id, error: endedError(message) });
|
|
61
|
+
this.asked.set(id, 'failed');
|
|
62
|
+
}
|
|
63
|
+
return answers;
|
|
64
|
+
}
|
|
54
65
|
// What codex-acp says, as said to Coda.
|
|
55
66
|
fromAgent(message) {
|
|
56
67
|
if (!record(message)) return [];
|
|
57
68
|
if ('id' in message && !('method' in message)) {
|
|
69
|
+
if (this.asked.get(message.id) === 'failed') { this.asked.delete(message.id); return []; }
|
|
58
70
|
const sessionId = this.asked.get(message.id)?.prompt;
|
|
59
71
|
// Cancelling or failing a turn can skip the sleep's completed item. End its waits before ending the prompt.
|
|
60
72
|
const finished = sessionId ? this.finishSleeps(base(sessionId), message.error ? 'failed' : message.result?.stopReason === 'cancelled' ? 'cancelled' : 'done') : [];
|
|
61
73
|
return [...finished, this.answer(message)];
|
|
62
74
|
}
|
|
63
|
-
if (record(message.params) && string(message.params.sessionId))
|
|
75
|
+
if (record(message.params) && string(message.params.sessionId)) {
|
|
76
|
+
message = { ...message, params: { ...message.params, sessionId: base(message.params.sessionId) } };
|
|
77
|
+
}
|
|
64
78
|
if (message.method !== 'session/update' || !record(message.params?.update)) return [message];
|
|
65
79
|
const { sessionId, update: u } = message.params;
|
|
66
80
|
const said = params => [{ jsonrpc: '2.0', method: '_coda/wait', params: { sessionId, ...params } }];
|
package/package.json
CHANGED
package/scripts/app-server.mjs
CHANGED
|
@@ -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
|
|
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`); }
|