@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
96 lines • 4.76 kB
JavaScript
import { close as closeInspector, url as inspectorUrl } from "node:inspector";
import { hasModeCrashHandler } from "./core/bootstrap-crash-listeners.js";
import { envValue } from "./core/brand.js";
import { isStaleExtensionContextError } from "./core/extensions/builtin/goal/stale-context.js";
const VM_DYNAMIC_IMPORT_CALLBACK_MISSING = "ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING";
const INSPECTOR_IMPORT_FRAME = "at importModuleDynamicallyCallback (node:internal/modules/esm/";
const INSPECTOR_TIMEOUT_FRAME = /\bat Timeout\._onTimeout \(<anonymous>:\d+:\d+\)/;
const RECOVER_INSPECTOR_VM_IMPORT = envValue("RECOVER_INSPECTOR_VM_IMPORT") === "1";
export const INSPECTOR_VM_IMPORT_WARNING = "Node Inspector dynamic import is unsupported; use require() or a target-side loader. Senpi kept running.";
/**
* Report whether this process inherited an Inspector option, either as an exec argument or
* through NODE_OPTIONS. Such a run owns a debugger socket that has to be handed to the process
* running the agent, which is what makes the child spawn in `cli.ts` load-bearing there.
*/
export function hasInheritedInspectorOption() {
return (process.execArgv.some((argument) => argument.startsWith("--inspect")) ||
process.env.NODE_OPTIONS?.includes("--inspect") === true);
}
export function releaseInheritedInspectorForChild() {
if (inspectorUrl() !== undefined && hasInheritedInspectorOption()) {
closeInspector();
}
}
export function isRecoverableInspectorVmImportError(error, origin) {
if (!RECOVER_INSPECTOR_VM_IMPORT || origin !== "unhandledRejection") {
return false;
}
// A hostile rejection value can throw from a `has` trap or a `code`/`stack` getter. An
// exception raised inside an uncaughtException handler terminates the process before any
// terminal restoration can run, so crash-policy inspection must stay non-throwing and
// classify such values as non-recoverable.
try {
if (inspectorUrl() === undefined) {
return false;
}
if (typeof error !== "object" || error === null || !("code" in error) || !("stack" in error)) {
return false;
}
return (error.code === VM_DYNAMIC_IMPORT_CALLBACK_MISSING &&
typeof error.stack === "string" &&
error.stack.includes(INSPECTOR_IMPORT_FRAME) &&
INSPECTOR_TIMEOUT_FRAME.test(error.stack));
}
catch {
return false;
}
}
let earlyRecoveryCount = 0;
let earlyRecoveryInstalled = false;
/**
* Install the process-level recovery seam before any asynchronous bootstrap work.
*
* With `--inspect-brk` the debugger is usable at the first line of the user script, so the
* reported delayed Inspector `import()` rejection can fire during `main()` bootstrap, before
* interactive mode registers its terminal-restoring uncaughtException handler. This listener
* exists only under the explicit `SENPI_RECOVER_INSPECTOR_VM_IMPORT=1` opt-in: it swallows
* exactly the recoverable Inspector rejection (counting it for a deferred TUI warning) and
* re-creates Node's default fatal outcome for everything else so bootstrap crashes stay loud.
*/
export function installEarlyInspectorVmImportRecovery() {
if (!RECOVER_INSPECTOR_VM_IMPORT || earlyRecoveryInstalled) {
return;
}
earlyRecoveryInstalled = true;
const listener = (error, origin) => {
// Interactive mode prepends its own crash handler, which runs first and owns both the
// recovery warning and the fatal terminal-restoration path. Once a mode handler is
// registered, this early seam must neither double-count recoveries nor exit itself. Other
// bootstrap listeners do not count: two of them deferring to each other would leave a real
// crash running.
const otherHandlerActive = hasModeCrashHandler("uncaughtException");
if (isRecoverableInspectorVmImportError(error, origin)) {
if (!otherHandlerActive) {
earlyRecoveryCount++;
}
return;
}
// A retired extension ctx is contained by the stale-ctx seam installed right after this one.
if (otherHandlerActive || isStaleExtensionContextError(error)) {
return;
}
console.error(error);
process.exit(1);
};
process.on("uncaughtException", listener);
}
/**
* Return the number of Inspector rejections recovered before the TUI existed and clear the
* pending count, so interactive mode can surface one deferred warning after startup.
*/
export function consumeEarlyInspectorVmImportRecoveries() {
const count = earlyRecoveryCount;
earlyRecoveryCount = 0;
return count;
}
//# sourceMappingURL=inspector-policy.js.map