Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/coding-agent/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Fixed

- Fixed stdout/stderr write-stream failures from embedded terminal hosts, including Node's `errno -122` case, crashing Caveman Code with an unhandled `WriteStream` error.
- Added missing `@sinclair/typebox` runtime dependency. Fixes `ERR_MODULE_NOT_FOUND` on `caveman-code` startup when installed globally ([#6](https://github.com/JuliusBrussee/caveman-code/issues/6)).
- Fixed broken extensions-migration links in the hooks-folder warning — now point to `JuliusBrussee/caveman-code` instead of the renamed-away `caveman-cli` repo ([#8](https://github.com/JuliusBrussee/caveman-code/issues/8)).
- Self-update check now queries the correct GitHub repo (`caveman-code`) for releases.
Expand Down
36 changes: 36 additions & 0 deletions packages/coding-agent/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,42 @@ process.emitWarning = (() => {}) as typeof process.emitWarning;
import { EnvHttpProxyAgent, setGlobalDispatcher } from "undici";
import { main } from "./main.js";

type StdioWriteError = Error & {
code?: string;
errno?: number | string;
syscall?: string;
};

function isClosedStdioWrite(error: StdioWriteError): boolean {
if (error.syscall !== "write") return false;
if (error.code === "EPIPE" || error.code === "ERR_STREAM_DESTROYED") return true;
// Some embedded PTY hosts surface a failed output stream as libuv EDQUOT
// (errno -122) instead of a more specific stream error on recent Node versions.
return error.errno === -122 || error.errno === "-122" || error.code === "Unknown system error -122";
}

function getStdioWriteExitCode(error: StdioWriteError): number {
return error.errno === -122 || error.errno === "-122" || error.code === "Unknown system error -122" ? 1 : 0;
}

function installStdioWriteErrorHandlers(): void {
let exiting = false;
const handleError = (error: Error) => {
if (!isClosedStdioWrite(error)) {
throw error;
}
if (exiting) return;
exiting = true;
const exitCode = getStdioWriteExitCode(error);
process.exitCode = exitCode;
setImmediate(() => process.exit(exitCode));
};

process.stdout.on("error", handleError);
process.stderr.on("error", handleError);
}

setGlobalDispatcher(new EnvHttpProxyAgent());
installStdioWriteErrorHandlers();

main(process.argv.slice(2));
Loading