fix(cli): bound diagnostics and add swap management

This commit is contained in:
Codex
2026-05-17 08:07:32 +00:00
parent c8e291f5fd
commit 57402f28c0
13 changed files with 618 additions and 50 deletions
+21 -5
View File
@@ -1,5 +1,5 @@
import { spawn, spawnSync } from "node:child_process";
import { createWriteStream, existsSync, readFileSync } from "node:fs";
import { closeSync, createWriteStream, existsSync, openSync, readSync, statSync } from "node:fs";
export interface CommandResult {
command: string[];
@@ -7,20 +7,26 @@ export interface CommandResult {
exitCode: number | null;
stdout: string;
stderr: string;
signal: NodeJS.Signals | null;
timedOut: boolean;
}
export function runCommand(command: string[], cwd: string): CommandResult {
export function runCommand(command: string[], cwd: string, options: { timeoutMs?: number } = {}): CommandResult {
const result = spawnSync(command[0], command.slice(1), {
cwd,
encoding: "utf8",
maxBuffer: 1024 * 1024 * 8,
timeout: options.timeoutMs,
});
const error = result.error as (Error & { code?: string }) | undefined;
return {
command,
cwd,
exitCode: result.status,
stdout: result.stdout ?? "",
stderr: result.stderr ?? result.error?.message ?? "",
stderr: result.stderr ?? error?.message ?? "",
signal: result.signal,
timedOut: error?.code === "ETIMEDOUT",
};
}
@@ -50,6 +56,16 @@ export async function runCommandToFiles(command: string[], cwd: string, stdoutFi
export function tailFile(path: string, maxBytes = 8192): string {
if (!existsSync(path)) return "";
const content = readFileSync(path);
return content.subarray(Math.max(0, content.length - maxBytes)).toString("utf8");
const safeMaxBytes = Math.max(0, Math.floor(maxBytes));
if (safeMaxBytes === 0) return "";
const size = statSync(path).size;
const bytesToRead = Math.min(size, safeMaxBytes);
const buffer = Buffer.alloc(bytesToRead);
const fd = openSync(path, "r");
try {
readSync(fd, buffer, 0, bytesToRead, size - bytesToRead);
} finally {
closeSync(fd);
}
return buffer.toString("utf8");
}