chore: stabilize Zhinian pilot delivery

This commit is contained in:
inman
2026-05-12 19:44:44 +08:00
parent 45389855e1
commit 20b5aff4ad
174 changed files with 41428 additions and 784 deletions

View File

@@ -43,6 +43,54 @@ let logDir: string | null = null;
const RING_BUFFER_SIZE = 500;
const recentLogs: string[] = [];
// ── Console safety ───────────────────────────────────────────────
type ConsoleMethod = 'debug' | 'info' | 'warn' | 'error' | 'log';
const consoleMethods: ConsoleMethod[] = ['debug', 'info', 'warn', 'error', 'log'];
let consolePatched = false;
let consoleOutputDisabled = false;
function isBrokenConsoleOutput(error: unknown): boolean {
const code = (error as NodeJS.ErrnoException | undefined)?.code;
return code === 'EIO' || code === 'EPIPE' || code === 'ERR_STREAM_DESTROYED';
}
function patchConsoleForBrokenOutput(): void {
if (consolePatched) return;
consolePatched = true;
const mutableConsole = console as unknown as Record<ConsoleMethod, (...args: unknown[]) => void>;
for (const method of consoleMethods) {
const original = mutableConsole[method].bind(console);
mutableConsole[method] = (...args: unknown[]) => {
if (consoleOutputDisabled) return;
try {
original(...args);
} catch (error) {
if (isBrokenConsoleOutput(error)) {
consoleOutputDisabled = true;
return;
}
throw error;
}
};
}
process.stdout?.on('error', (error) => {
if (isBrokenConsoleOutput(error)) {
consoleOutputDisabled = true;
}
});
process.stderr?.on('error', (error) => {
if (isBrokenConsoleOutput(error)) {
consoleOutputDisabled = true;
}
});
}
patchConsoleForBrokenOutput();
// ── Async write buffer ───────────────────────────────────────────
/** Pending log lines waiting to be flushed to disk. */