@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
157 lines • 6.54 kB
JavaScript
import { AsyncLocalStorage } from "node:async_hooks";
import net from "node:net";
import { fileURLToPath } from "node:url";
export const handleOwnerStore = new AsyncLocalStorage();
/**
* `at fn (/abs/file.ts:1:2)`, `at /abs/file.ts:1:2`, or Node's ESM form `at fn (file:///abs/file.mjs:1:2)`;
* `node:` and `native` frames never match.
*/
const FRAME_PATH = /(?:^|[\s(])((?:file:\/\/)?(?:\/|[A-Za-z]:[\\/])[^()]*?):\d+:\d+\)?$/;
/**
* A file extension Bun loads through senpi's importer names its frames by module id,
* `senpi-extension:<generation>/<encodeURIComponent(filename)>` (`bun-extension-registry.ts`).
* Not imported from there: the registry is Bun-only and loads lazily.
*/
const EXTENSION_MODULE_FRAME = /(?:^|[\s(])senpi-extension:[^/\s()]+\/([^\s()]+?):\d+:\d+\)?$/;
const CREATOR_STACK_DEPTH = 16;
/**
* `creatorPath`, `creatingOwner` and the patched function sit above the creating call. Skipped by count, not
* by file: in a bundled build every senpi module shares one file, and skipping by file would skip senpi code
* that created the handle and blame the extension frame below it.
*/
const TRACKING_FRAMES = 3;
function creatorPath() {
const limit = Error.stackTraceLimit;
Error.stackTraceLimit = CREATOR_STACK_DEPTH;
const stack = new Error().stack ?? "";
Error.stackTraceLimit = limit;
for (const line of stack.split("\n").slice(1 + TRACKING_FRAMES)) {
const frame = line.trim();
const moduleFile = EXTENSION_MODULE_FRAME.exec(frame)?.[1];
if (moduleFile !== undefined)
return decodeURIComponent(moduleFile);
const location = FRAME_PATH.exec(frame)?.[1];
if (location === undefined)
continue;
return location.startsWith("file://") ? fileURLToPath(location) : location;
}
return undefined;
}
function creatingOwner() {
const owner = handleOwnerStore.getStore();
if (owner === undefined)
return undefined;
const path = creatorPath();
return path !== undefined && owner.ownsCreatorFrame(path) ? owner : undefined;
}
/** Keeps `util.promisify(setTimeout)` and friends working on the patched globals. */
function replaceGlobal(target, name, patched) {
const native = Reflect.get(target, name);
if (typeof native === "function") {
for (const key of Reflect.ownKeys(native)) {
if (key === "length" || key === "name" || key === "prototype")
continue;
const descriptor = Reflect.getOwnPropertyDescriptor(native, key);
if (descriptor)
Reflect.defineProperty(patched, key, descriptor);
}
}
Reflect.set(target, name, patched);
}
const forgetByTimer = new WeakMap();
function patchTimer(name, clearName) {
const native = Reflect.get(globalThis, name);
const clear = Reflect.get(globalThis, clearName);
if (typeof native !== "function" || typeof clear !== "function")
return;
const oneShot = name === "setTimeout";
const patched = function (callback, ...rest) {
const owner = typeof callback === "function" ? creatingOwner() : undefined;
if (owner === undefined || typeof callback !== "function")
return Reflect.apply(native, this, [callback, ...rest]);
let forget = () => { };
const run = oneShot
? function (...args) {
forget();
return Reflect.apply(callback, this, args);
}
: callback;
const handle = Reflect.apply(native, this, [run, ...rest]);
forget = owner.track("timer", () => Reflect.apply(clear, globalThis, [handle]));
if (typeof handle === "object" && handle !== null)
forgetByTimer.set(handle, forget);
return handle;
};
replaceGlobal(globalThis, name, patched);
}
function patchClear(name) {
const native = Reflect.get(globalThis, name);
if (typeof native !== "function")
return;
const patched = function (handle) {
if (typeof handle === "object" && handle !== null) {
forgetByTimer.get(handle)?.();
forgetByTimer.delete(handle);
}
return Reflect.apply(native, this, [handle]);
};
replaceGlobal(globalThis, name, patched);
}
function isStoppable(value) {
return typeof value === "object" && value !== null && "stop" in value && typeof value.stop === "function";
}
/** `Bun.serve` (which also backs Bun's `node:http` servers) and `Bun.listen`. */
function patchBunFactory(name) {
const bun = Reflect.get(globalThis, "Bun");
if (typeof bun !== "object" || bun === null)
return;
const native = Reflect.get(bun, name);
if (typeof native !== "function")
return;
const patched = function (...args) {
const value = Reflect.apply(native, this, args);
const owner = isStoppable(value) ? creatingOwner() : undefined;
if (owner === undefined || !isStoppable(value))
return value;
const stop = value.stop;
const forget = owner.track("server", () => Reflect.apply(stop, value, [true]));
value.stop = function (...stopArgs) {
forget();
return Reflect.apply(stop, this, stopArgs);
};
return value;
};
replaceGlobal(bun, name, patched);
}
/** `node:net` servers (Node's `node:http` included) and client sockets. */
function patchNet() {
const listen = net.Server.prototype.listen;
Reflect.set(net.Server.prototype, "listen", function (...args) {
const owner = creatingOwner();
if (owner !== undefined)
this.once("close", owner.track("server", () => this.close()));
return Reflect.apply(listen, this, args);
});
const connect = net.Socket.prototype.connect;
Reflect.set(net.Socket.prototype, "connect", function (...args) {
const owner = creatingOwner();
if (owner !== undefined)
this.once("close", owner.track("socket", () => this.destroy()));
return Reflect.apply(connect, this, args);
});
}
let installed = false;
/** Installs the hooks once per process; a no-op for every call after the first. */
export function installHandleTracking() {
if (installed)
return;
installed = true;
patchTimer("setTimeout", "clearTimeout");
patchTimer("setInterval", "clearInterval");
patchClear("clearTimeout");
patchClear("clearInterval");
patchBunFactory("serve");
patchBunFactory("listen");
patchNet();
}
//# sourceMappingURL=extension-handle-tracking.js.map