This carries #727's reviewed part forward, without auto-approve (that comes as its own PR). Permission behaviour is the same as 1.2. - Windows Git Bash detection, never the WSL System32 bash. It can be overridden in Settings and is passed per spawn only. - GBK / system code page decoding of raw process output. - Folded tool steps that don't wrap at ~600px. - Select-to-quote, and copy/edit under the user bubble. - Instant send with the echo replacing the bubble in place, and retry when a send fails. - `/app` heals itself after sleep/wake. `frame-ancestors` is limited to loopback and Magpie's window. - Grayscale timeline polish: one-line tool rows, a task-list pill, a reply footer and code blocks. - Test fixes: - The `initializing` push is awaited. - `afterEach` waits for the thread store's writes. - `runtimeInfo().starting` is exposed, and the steer test waits on it. - "idle recycling" waits for closed. Run on mote (macOS) at 65ab7c94: - `npm ci`, `npm run check` and `build` are ok; `verify:build` gives 5/5 OK. - Full `vitest run` x5: 4 green (427 passed). One red, "send while running → queued", is a race that is on main too. - Cause: `setStatus(idle)` runs before `queue.shift()`. - A 300ms pause injected there makes it red 3/3 on main and on this head. - Filed on #585 with the cause and the repro. - Focused send-latency + manager runs: 12/12. `e2e:v1`: 25/25, with no pageerror or console error. - Broken by hand: - With the old steer wait and the mock send held 400ms, the test is red 3/3; the new wait passes 3/3. - With `recycleIdle` made a no-op, "idle recycling" goes red. - Playwright render: - Chromium and WebKit × zh/en × light/dark × 1280/420. - No gradients, no coloured left borders, no `<select>`, no page errors. - At 420 the page scrolls sideways (scrollWidth 480), the same as main. Not tried: Windows (Git Bash detection, GBK decoding) and a real agent, rather than the mock.
189 lines
5.9 KiB
TypeScript
189 lines
5.9 KiB
TypeScript
// JSON-RPC over stdio client for `codex app-server` (§7.3.2).
|
|
// stdout is parsed line by line: {id,result|error} → response; {id,method} → server request; {method} → notification.
|
|
import { spawn, type ChildProcess } from "node:child_process";
|
|
import { log } from "../../log.js";
|
|
import { killTree, trackTree, type Argv } from "../../util/bin.js";
|
|
import { OutputDecoder } from "../../util/decode.js";
|
|
import { withAgentShell } from "../../util/agentShell.js";
|
|
|
|
export class CodexRpcError extends Error {
|
|
code?: number;
|
|
constructor(message: string, code?: number) {
|
|
super(message);
|
|
this.code = code;
|
|
}
|
|
}
|
|
|
|
export class CodexProcessExitedError extends CodexRpcError {
|
|
constructor() {
|
|
super("Codex 进程已退出");
|
|
}
|
|
}
|
|
|
|
type ServerRequestHandler = (method: string, params: Record<string, unknown>) => Promise<unknown>;
|
|
type NotificationHandler = (method: string, params: Record<string, unknown>) => void;
|
|
|
|
export class CodexRpc {
|
|
private proc: ChildProcess;
|
|
private nextId = 1;
|
|
private pending = new Map<number, { resolve: (v: unknown) => void; reject: (e: Error) => void }>();
|
|
private buf = "";
|
|
private exited = false;
|
|
onServerRequest?: ServerRequestHandler;
|
|
onNotification?: NotificationHandler;
|
|
onExit?: (code: number | null) => void;
|
|
|
|
constructor(
|
|
argv: Argv,
|
|
opts: { cwd?: string; env?: NodeJS.ProcessEnv } = {},
|
|
) {
|
|
this.proc = spawn(argv.cmd, argv.args, {
|
|
cwd: opts.cwd,
|
|
env: withAgentShell(opts.env ?? process.env),
|
|
stdio: ["pipe", "pipe", "pipe"],
|
|
windowsHide: true,
|
|
...(argv.windowsVerbatimArguments ? { windowsVerbatimArguments: true } : {}),
|
|
// Non-Windows: its own process group, so after a close() timeout killTree also ends the commands codex started
|
|
...(process.platform !== "win32" ? { detached: true } : {}),
|
|
});
|
|
trackTree(this.proc);
|
|
this.proc.stdout!.setEncoding("utf8");
|
|
this.proc.stdout!.on("data", (chunk: string) => this.onData(chunk));
|
|
const errDec = new OutputDecoder(); // 1.3: stderr may be in the Windows code page (GBK)
|
|
this.proc.stderr!.on("data", (chunk: Buffer) => {
|
|
this.stderrTail = (this.stderrTail + errDec.write(chunk)).slice(-4096);
|
|
});
|
|
this.proc.on("error", () => this.failAll());
|
|
this.proc.on("close", (code) => {
|
|
this.exited = true;
|
|
this.failAll();
|
|
this.onExit?.(code);
|
|
});
|
|
}
|
|
|
|
private stderrTail = "";
|
|
|
|
get stderr(): string {
|
|
return this.stderrTail;
|
|
}
|
|
|
|
get isExited(): boolean {
|
|
return this.exited;
|
|
}
|
|
|
|
private onData(chunk: string): void {
|
|
this.buf += chunk;
|
|
let i: number;
|
|
while ((i = this.buf.indexOf("\n")) >= 0) {
|
|
const line = this.buf.slice(0, i).trim();
|
|
this.buf = this.buf.slice(i + 1);
|
|
if (!line) continue;
|
|
let msg: Record<string, unknown>;
|
|
try {
|
|
msg = JSON.parse(line);
|
|
} catch {
|
|
log("warn", "codex rpc: 非 JSON 行", { line: line.slice(0, 200) });
|
|
continue;
|
|
}
|
|
void this.dispatch(msg);
|
|
}
|
|
}
|
|
|
|
private async dispatch(msg: Record<string, unknown>): Promise<void> {
|
|
const hasId = msg.id !== undefined && msg.id !== null;
|
|
if (hasId && ("result" in msg || "error" in msg)) {
|
|
const id = Number(msg.id);
|
|
const p = this.pending.get(id);
|
|
if (p) {
|
|
this.pending.delete(id);
|
|
if (msg.error) {
|
|
const e = msg.error as { code?: number; message?: string };
|
|
p.reject(new CodexRpcError(e.message ?? "RPC error", e.code));
|
|
} else {
|
|
p.resolve(msg.result);
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
if (hasId && msg.method) {
|
|
// Server request → needs a response
|
|
const id = Number(msg.id);
|
|
try {
|
|
const result = this.onServerRequest
|
|
? await this.onServerRequest(String(msg.method), (msg.params as Record<string, unknown>) ?? {})
|
|
: undefined;
|
|
this.send({ id, result: result ?? null });
|
|
} catch (e) {
|
|
this.send({ id, error: { code: -32603, message: String((e as Error)?.message ?? e) } });
|
|
}
|
|
return;
|
|
}
|
|
if (!hasId && msg.method) {
|
|
try {
|
|
this.onNotification?.(String(msg.method), (msg.params as Record<string, unknown>) ?? {});
|
|
} catch (e) {
|
|
log("warn", "codex rpc: 通知处理失败", { method: msg.method, err: String(e) });
|
|
}
|
|
}
|
|
}
|
|
|
|
private send(obj: Record<string, unknown>): void {
|
|
if (this.exited) return;
|
|
try {
|
|
this.proc.stdin!.write(JSON.stringify(obj) + "\n");
|
|
} catch {}
|
|
}
|
|
|
|
request<T = unknown>(method: string, params?: Record<string, unknown>, timeoutMs = 60000): Promise<T> {
|
|
if (this.exited) return Promise.reject(new CodexProcessExitedError());
|
|
const id = this.nextId++;
|
|
return new Promise<T>((resolvePromise, reject) => {
|
|
const timer = setTimeout(() => {
|
|
this.pending.delete(id);
|
|
reject(new CodexRpcError(`请求超时:${method}`));
|
|
}, timeoutMs);
|
|
this.pending.set(id, {
|
|
resolve: (v) => {
|
|
clearTimeout(timer);
|
|
resolvePromise(v as T);
|
|
},
|
|
reject: (e) => {
|
|
clearTimeout(timer);
|
|
reject(e);
|
|
},
|
|
});
|
|
this.send({ jsonrpc: "2.0", id, method, params: params ?? {} });
|
|
});
|
|
}
|
|
|
|
notify(method: string, params?: Record<string, unknown>): void {
|
|
this.send({ jsonrpc: "2.0", method, params: params ?? {} });
|
|
}
|
|
|
|
private failAll(): void {
|
|
const err = new CodexProcessExitedError();
|
|
for (const [, p] of this.pending) p.reject(err);
|
|
this.pending.clear();
|
|
}
|
|
|
|
async close(): Promise<void> {
|
|
try {
|
|
this.proc.stdin?.end();
|
|
} catch {}
|
|
await new Promise<void>((resolvePromise) => {
|
|
const t = setTimeout(() => {
|
|
void killTree(this.proc, { graceMs: 1500 });
|
|
resolvePromise();
|
|
}, 3000);
|
|
this.proc.on("close", () => {
|
|
clearTimeout(t);
|
|
resolvePromise();
|
|
});
|
|
if (this.exited) {
|
|
clearTimeout(t);
|
|
resolvePromise();
|
|
}
|
|
});
|
|
}
|
|
}
|