@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
174 lines • 8.65 kB
JavaScript
import { existsSync } from "node:fs";
import { dirname, isAbsolute } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { loadInTurn } from "./bun-extension-load-turns.js";
import { publishRuntimeMetadata } from "./extension-runtime-module.js";
// esbuild reads an import() only when its second argument is fully static, and these
// attributes are whatever the extension wrote. The call stays dynamic either way, so
// the importer is built at runtime and the bundler never has to read it.
const importModule = new Function("specifier", "options", "return import(specifier, options)");
export const extensionNamespace = "senpi-extension";
const RUNTIME_SPECIFIER = "runtime";
// Inside a `bun build --compile` binary the shim has no on-disk path (import.meta.url is a
// $bunfs URL), so the file route is only available when the file is really there; the compiled
// binary keeps the virtual module, which it never had a problem with.
export const runtimeModulePath = resolveRuntimeModulePath();
function resolveRuntimeModulePath() {
try {
const candidate = fileURLToPath(new URL("./extension-runtime-module.js", import.meta.url));
return existsSync(candidate) ? candidate : undefined;
}
catch {
return undefined;
}
}
// An id is `<generation>/<encodeURIComponent(filename)>`. The encoded half never contains a
// literal "/" (Windows separators arrive as %5C), so the FIRST slash is always the generation
// separator — but only when one exists. A bare id with no slash used to slice to nonsense and
// surface as `Cannot find package '<generation>'` (omo#8427).
function splitModuleId(id) {
const slash = id.indexOf("/");
if (slash <= 0 || slash === id.length - 1)
return undefined;
return { generation: id.slice(0, slash), filename: decodeURIComponent(id.slice(slash + 1)) };
}
const graphs = new Map();
const collected = new FinalizationRegistry((generation) => graphs.delete(generation));
// Bun serves an import()'s modules one loop turn at a time (senpi#6199) and its loader keeps no reference to the graph
// serving them: a graph whose importer was already dropped was collected between two loads and failed the import as
// disposed (senpi#6208). A graph is held strongly while any import it serves is in flight.
const importing = new Map();
let nextGeneration = 0;
let installed = false;
let pluginRegistrations = 0;
const registeredHosts = new Set();
export class ExtensionModuleIdError extends Error {
constructor(id) {
super(`Extension module id is not "<generation>/<encoded filename>": ${id}`);
this.name = "ExtensionModuleIdError";
}
}
export class ExtensionGenerationDisposedError extends Error {
constructor(generation) {
super(`Extension generation ${generation} has been disposed`);
this.name = "ExtensionGenerationDisposedError";
this.generation = generation;
}
}
function graphFor(generation) {
const graph = graphs.get(generation)?.deref();
if (!graph)
throw new ExtensionGenerationDisposedError(generation);
return graph;
}
// Module-registry exports must never close over a graph: Bun retains modules.
// Only the importer, returned factory wrappers, and live runtimes own graphs.
function metadata(generation, filename) {
const resolvePath = (specifier) => {
const id = graphFor(generation).resolve(specifier, filename);
if (id.startsWith(`${extensionNamespace}:`))
return decodeURIComponent(id.slice(id.indexOf("/") + 1));
// Files Bun loads natively (JSON, assets) resolve to their real path, not a graph id.
return isAbsolute(id) ? id : undefined;
};
// CommonJS code calls `require.resolve` for sibling files (jsdom locates its XHR sync
// worker that way), so `require` is a function object carrying Node's resolver shape.
const require = Object.assign((specifier) => graphFor(generation).require(specifier, filename), {
resolve: (specifier) => resolvePath(specifier) ?? graphFor(generation).resolve(specifier, filename),
});
return {
url: pathToFileURL(filename).href,
path: filename,
dir: dirname(filename),
// Node (>= 20.11) and Bun both expose these; dependencies read them to find bundled assets (#4766).
filename,
dirname: dirname(filename),
require,
commonJs: (body) => graphFor(generation).evaluateCommonJs(filename, body),
import: async (specifier, options) => importModule(graphFor(generation).resolve(specifier, filename), options),
resolve: (specifier) => {
const path = resolvePath(specifier);
return path === undefined ? graphFor(generation).resolve(specifier, filename) : pathToFileURL(path).href;
},
};
}
// A separate activation prevents permanent hooks from sharing a closure
// environment with registerExtensionGraph's disposable graph reference.
function installRegistry(virtualModules) {
const hosts = Object.entries(virtualModules).filter(([name]) => !registeredHosts.has(name));
if (!installed || hosts.length > 0) {
pluginRegistrations++;
Bun.plugin({
name: extensionNamespace,
setup(builder) {
for (const [name, exports] of hosts) {
builder.module(name, () => ({ exports, loader: "object" }));
registeredHosts.add(name);
}
if (installed)
return;
// "runtime" resolves to a real file, never a Bun.plugin virtual module. A
// builder.module() registration is intermittently invisible to the resolver under
// `bun test --parallel` on a loaded Windows shard: onResolve still fires and returns
// the namespace, the registration is still listed, and Bun answers
// `Cannot find package 'runtime'` anyway — generation 1 served 40 resolves and
// refused the 41st in the same worker (omo#8427, run 35329245740). A file on disk
// has no registration window to lose.
publishRuntimeMetadata(metadata);
builder.module(`${extensionNamespace}:${RUNTIME_SPECIFIER}`, () => ({
exports: { metadata },
loader: "object",
}));
builder.onResolve({ filter: /.*/, namespace: extensionNamespace }, ({ path }) => {
if (path === RUNTIME_SPECIFIER)
return runtimeModulePath === undefined
? { path, namespace: extensionNamespace }
: { path: runtimeModulePath, namespace: "file" };
const parsed = splitModuleId(path);
if (parsed === undefined)
throw new ExtensionModuleIdError(path);
graphFor(parsed.generation);
return { path, namespace: extensionNamespace };
});
builder.onLoad({ filter: /.*/, namespace: extensionNamespace }, ({ path }) => {
const parsed = splitModuleId(path);
if (parsed === undefined)
throw new ExtensionModuleIdError(path);
return loadInTurn(() => graphFor(parsed.generation).load(parsed.filename));
});
installed = true;
},
});
}
}
export function registerExtensionGraph(graph, virtualModules) {
installRegistry(virtualModules);
const generation = String(nextGeneration++);
graphs.set(generation, new WeakRef(graph));
collected.register(graph, generation, graph);
return {
generation,
async holdDuring(pending) {
importing.set(graph, (importing.get(graph) ?? 0) + 1);
try {
return await pending;
}
finally {
const remaining = (importing.get(graph) ?? 1) - 1;
if (remaining === 0)
importing.delete(graph);
else
importing.set(graph, remaining);
}
},
dispose() {
graphs.delete(generation);
collected.unregister(graph);
},
};
}
/** Internal lifecycle diagnostics, shared by regression and compiled probes. */
export function bunExtensionImporterStats() {
return { generations: [...graphs.values()].filter((entry) => entry.deref()).length, plugins: pluginRegistrations };
}
//# sourceMappingURL=bun-extension-registry.js.map