@code-yeongyu/senpi-codemode 2026.10.9 → 2026.10.10-3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +54 -0
- package/README.md +131 -5
- package/package.json +6 -5
- package/src/bridge/protocol.ts +8 -3
- package/src/config/executable-settings.json +5 -0
- package/src/config/project-trust.ts +51 -0
- package/src/config/settings-overrides.ts +2 -0
- package/src/environments/js-environments.ts +208 -0
- package/src/environments/js-installer.ts +268 -0
- package/src/environments/js-revision-files.ts +91 -0
- package/src/environments/no-links.ts +132 -0
- package/src/environments/project-resolves.ts +54 -0
- package/src/environments/revision-store.ts +20 -4
- package/src/extension/kernel-registration.ts +7 -2
- package/src/extension/runtime-factory.ts +40 -3
- package/src/extension/runtime-info.ts +6 -2
- package/src/extension/session-manager.ts +16 -2
- package/src/index.ts +1 -0
- package/src/interpreters/detect.ts +34 -1
- package/src/kernels/AGENTS.md +2 -0
- package/src/kernels/js/cell-dispatch.ts +44 -0
- package/src/kernels/js/context-manager.ts +46 -51
- package/src/kernels/js/control-frames.ts +24 -0
- package/src/kernels/js/host-entries.ts +62 -0
- package/src/kernels/js/kernel-contract.ts +16 -1
- package/src/kernels/js/kernel-memory-bridge.ts +1 -1
- package/src/kernels/js/kernel-memory.ts +79 -0
- package/src/kernels/js/kernel-tools-registry.js +1 -0
- package/src/kernels/js/local-module-loader.ts +17 -6
- package/src/kernels/js/process-drain-scan.d.ts +8 -0
- package/src/kernels/js/process-drain-scan.js +28 -0
- package/src/kernels/js/process-entry.js +462 -0
- package/src/kernels/js/process-memory-host.ts +83 -0
- package/src/kernels/js/process-worker.ts +298 -0
- package/src/kernels/js/run-queue.ts +7 -0
- package/src/kernels/js/worker-core.js +7 -2
- package/src/kernels/js/worker-cwd.js +5 -2
- package/src/kernels/js/worker-host.ts +9 -2
- package/src/kernels/js/worker-package-resolve.js +153 -0
- package/src/kernels/js/worker-runtime.js +1 -0
- package/src/kernels/js/worker-slot.ts +9 -0
- package/src/kernels/js/worker-startup.ts +49 -1
- package/src/kernels/js/worker-webview.js +8 -0
- package/src/kernels/py/host-cell.ts +8 -31
- package/src/kernels/py/prelude.py +10 -2
- package/src/kernels/py/process.ts +4 -0
- package/src/kernels/sandbox/sandbox-cell.ts +116 -0
- package/src/kernels/sandbox/vendor/pi-codemode/VENDORED.md +1 -0
- package/src/kernels/sandbox/vendor/pi-codemode/runtime/prelude-source.ts +14 -1
- package/src/kernels/sandbox/vendor/pi-codemode/runtime/worker.ts +4 -1
- package/src/kernels/sandbox/vendor/pi-codemode/types.ts +2 -0
- package/src/kernels/shared/host-cell.ts +57 -0
- package/src/kernels/shared/subprocess-process.ts +14 -0
- package/src/prompt/eval-prompt.ts +8 -1
- package/src/tool/AGENTS.md +1 -0
- package/src/tool/cell-handler.ts +9 -11
- package/src/tool/eval-request.ts +23 -1
- package/src/tool/eval-tool-options.ts +3 -0
- package/src/tool/eval-tool.ts +17 -4
- package/src/tool/load-cell.ts +18 -3
- package/src/tool/magic-cell-host.ts +60 -2
- package/src/tool/magic-cells.ts +12 -2
- package/src/tool/run-eval-cell.ts +30 -3
- package/src/tool/runtime-label.ts +2 -1
- package/src/tool/types.ts +23 -1
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { KernelMemoryThresholds } from "../../bridge/memory-protocol.ts";
|
|
2
|
+
import { restartNotice } from "../shared/kernel-death.ts";
|
|
3
|
+
import { KernelMemoryPolicy } from "../shared/kernel-memory.ts";
|
|
4
|
+
import type { ResultMessage } from "./kernel-contract.ts";
|
|
5
|
+
|
|
6
|
+
export interface JavaScriptProcessMemoryReading {
|
|
7
|
+
readonly liveBytes: number;
|
|
8
|
+
readonly measure: "footprint";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type JavaScriptProcessFootprintReader = (pid: number) => { readonly bytes: number } | undefined;
|
|
12
|
+
|
|
13
|
+
// Host-side memory for a process-mode js kernel: the in-heap worker reading cannot cross a process
|
|
14
|
+
// boundary, so the host reads the child's footprint (the py/rb/jl pattern) and the shared policy
|
|
15
|
+
// applies the notice and the ceiling. A crashed child re-announces itself through the restart notice
|
|
16
|
+
// on the next result, mirroring ReplaceableKernel's restarted tag.
|
|
17
|
+
export class JavaScriptProcessMemoryHost {
|
|
18
|
+
readonly #policy: KernelMemoryPolicy;
|
|
19
|
+
readonly #readFootprint: JavaScriptProcessFootprintReader | undefined;
|
|
20
|
+
#lastLiveBytes: number | undefined;
|
|
21
|
+
#restarted: string | null = null;
|
|
22
|
+
|
|
23
|
+
constructor(thresholds: KernelMemoryThresholds, readFootprint?: JavaScriptProcessFootprintReader) {
|
|
24
|
+
this.#readFootprint = readFootprint;
|
|
25
|
+
this.#policy = new KernelMemoryPolicy("js", thresholds, { collects: readFootprint === undefined });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
get thresholds(): KernelMemoryThresholds {
|
|
29
|
+
return this.#policy.thresholds;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
lastLiveBytes(pid: () => number | undefined): number | undefined {
|
|
33
|
+
const reading = this.#read(pid);
|
|
34
|
+
if (reading !== undefined) this.#lastLiveBytes = reading.liveBytes;
|
|
35
|
+
return this.#lastLiveBytes;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
query(pid: () => number | undefined): JavaScriptProcessMemoryReading | undefined {
|
|
39
|
+
const reading = this.#read(pid);
|
|
40
|
+
if (reading !== undefined) this.#lastLiveBytes = reading.liveBytes;
|
|
41
|
+
return reading;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
settled(message: ResultMessage, pid: () => number | undefined): ResultMessage {
|
|
45
|
+
const reading = this.#read(pid);
|
|
46
|
+
if (reading !== undefined) this.#lastLiveBytes = reading.liveBytes;
|
|
47
|
+
let result: ResultMessage = message;
|
|
48
|
+
if (reading !== undefined) {
|
|
49
|
+
const report = { liveBytes: reading.liveBytes, measure: "footprint" as const };
|
|
50
|
+
result = { ...result, memory: this.#policy.annotate(report) };
|
|
51
|
+
}
|
|
52
|
+
if (this.#restarted !== null) {
|
|
53
|
+
const notice = this.#restarted;
|
|
54
|
+
this.#restarted = null;
|
|
55
|
+
result = { ...result, notice, kernelState: "restarted" };
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
claimRecycle(idle: boolean): boolean {
|
|
61
|
+
if (!idle || !this.#policy.recyclePending) return false;
|
|
62
|
+
this.#policy.recycleStarted();
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
markRestarted(error: Error): void {
|
|
67
|
+
this.#restarted = restartNotice("js", error.message);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
workerLost(): void {
|
|
71
|
+
this.#policy.kernelRetired();
|
|
72
|
+
this.#lastLiveBytes = undefined;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
#read(pid: () => number | undefined): JavaScriptProcessMemoryReading | undefined {
|
|
76
|
+
if (this.#readFootprint === undefined) return undefined;
|
|
77
|
+
const current = pid();
|
|
78
|
+
if (current === undefined) return undefined;
|
|
79
|
+
const footprint = this.#readFootprint(current);
|
|
80
|
+
if (footprint === undefined) return undefined;
|
|
81
|
+
return { liveBytes: Math.round(footprint.bytes), measure: "footprint" };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { randomBytes } from "node:crypto";
|
|
3
|
+
import { accessSync, constants } from "node:fs";
|
|
4
|
+
import { basename, dirname, join } from "node:path";
|
|
5
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
6
|
+
import type { KernelToHostMessage } from "../../bridge/protocol.ts";
|
|
7
|
+
import { isKernelToHostMessage, parseBridgeJsonLine, validateBridgeMessage } from "../../bridge/protocol.ts";
|
|
8
|
+
import type { EvalRuntimeInfo } from "../../tool/types.ts";
|
|
9
|
+
import { type CodemodeRuntimeAssetEnvironment, requireCodemodeRuntimeAsset } from "../shared/runtime-asset.ts";
|
|
10
|
+
import { type SubprocessLike, SubprocessProcess, spawnSubprocess } from "../shared/subprocess-process.ts";
|
|
11
|
+
import type { WorkerLike } from "./inline-worker.ts";
|
|
12
|
+
import { JavaScriptWorkerExitedError } from "./worker-host.ts";
|
|
13
|
+
|
|
14
|
+
const PROCESS_CLOSE_GRACE_MS = 2_000;
|
|
15
|
+
|
|
16
|
+
export interface JavaScriptProcessEntryUrlOptions extends CodemodeRuntimeAssetEnvironment {
|
|
17
|
+
readonly localPath?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function resolveJsProcessEntryUrl(options: JavaScriptProcessEntryUrlOptions = {}): URL {
|
|
21
|
+
const localPath = options.localPath ?? join(dirname(fileURLToPath(import.meta.url)), "process-entry.js");
|
|
22
|
+
return pathToFileURL(requireCodemodeRuntimeAsset(localPath, join("kernels", "js", "process-entry.js"), options));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class JavaScriptProcessRuntimeUnavailableError extends Error {
|
|
26
|
+
readonly name = "JavaScriptProcessRuntimeUnavailableError";
|
|
27
|
+
readonly runtime: string;
|
|
28
|
+
readonly searchPath: string;
|
|
29
|
+
|
|
30
|
+
constructor(runtime: string, searchPath: string) {
|
|
31
|
+
super(
|
|
32
|
+
`JavaScript runtime is unavailable: no ${runtime} executable found on PATH${searchPath.length === 0 ? " (empty)" : ""}. Install bun or node, or use isolation.js: "worker".`,
|
|
33
|
+
);
|
|
34
|
+
this.runtime = runtime;
|
|
35
|
+
this.searchPath = searchPath;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface JavaScriptProcessWorkerOptions {
|
|
40
|
+
readonly cwd: string;
|
|
41
|
+
readonly parallelPoolWidth: number;
|
|
42
|
+
readonly searchPath?: string;
|
|
43
|
+
readonly execPath?: string;
|
|
44
|
+
readonly env?: NodeJS.ProcessEnv;
|
|
45
|
+
readonly spawn?: (
|
|
46
|
+
command: string,
|
|
47
|
+
args: readonly string[],
|
|
48
|
+
options: { cwd?: string; env?: NodeJS.ProcessEnv },
|
|
49
|
+
) => SubprocessLike;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface JavaScriptProcessWorker extends WorkerLike {
|
|
53
|
+
readonly pid?: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* The runtime that runs the kernel child: the host's own runtime (bun when senpi runs on Bun, node otherwise), taken
|
|
58
|
+
* from the host's executable when that is the runtime itself, else found on PATH, the host's runtime first.
|
|
59
|
+
*/
|
|
60
|
+
export function resolveJavaScriptProcessCommand(
|
|
61
|
+
searchPath: string | undefined,
|
|
62
|
+
platform: NodeJS.Platform = process.platform,
|
|
63
|
+
hostRuntime: string = process.versions.bun === undefined ? "node" : "bun",
|
|
64
|
+
execPath: string = process.execPath,
|
|
65
|
+
): string {
|
|
66
|
+
const candidates = hostRuntime === "bun" ? ["bun", "node"] : ["node", "bun"];
|
|
67
|
+
const extensions = platform === "win32" ? [".exe", ".cmd", ".bat", ""] : [""];
|
|
68
|
+
if (runtimeNameOf(execPath) !== undefined && isExecutable(execPath)) return execPath;
|
|
69
|
+
const pathValue = searchPath ?? process.env.PATH ?? "";
|
|
70
|
+
if (pathValue.trim().length === 0) throw new JavaScriptProcessRuntimeUnavailableError(hostRuntime, "");
|
|
71
|
+
const directories = pathValue.split(platform === "win32" ? ";" : ":").filter((directory) => directory.length > 0);
|
|
72
|
+
for (const name of candidates) {
|
|
73
|
+
for (const directory of directories) {
|
|
74
|
+
for (const extension of extensions) {
|
|
75
|
+
const candidate = join(directory, `${name}${extension}`);
|
|
76
|
+
if (isExecutable(candidate)) return candidate;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
throw new JavaScriptProcessRuntimeUnavailableError(hostRuntime, pathValue);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The badge identity of the child a process-mode kernel runs: the host's own identity when the child is the host's
|
|
85
|
+
* executable, else the resolved runtime with the version it reports. Undefined when no runtime resolves (the kernel
|
|
86
|
+
* then reports the capability gap on its first cell).
|
|
87
|
+
*/
|
|
88
|
+
export function processRuntimeInfo(searchPath: string | undefined): EvalRuntimeInfo | undefined {
|
|
89
|
+
let command: string;
|
|
90
|
+
try {
|
|
91
|
+
command = resolveJavaScriptProcessCommand(searchPath);
|
|
92
|
+
} catch (error) {
|
|
93
|
+
if (error instanceof JavaScriptProcessRuntimeUnavailableError) return undefined;
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
const name = runtimeNameOf(command);
|
|
97
|
+
if (command === process.execPath || name === undefined) return undefined;
|
|
98
|
+
const version = execFileSync(command, ["--version"], { encoding: "utf8", timeout: 5_000 }).trim().replace(/^v/, "");
|
|
99
|
+
return { name, version, path: command, isolation: "process" };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** "bun" or "node" from an executable path; undefined for anything else (a compiled senpi binary, for one). */
|
|
103
|
+
export function runtimeNameOf(command: string): "bun" | "node" | undefined {
|
|
104
|
+
const name = basename(command).replace(/\.(exe|cmd|bat)$/i, "");
|
|
105
|
+
return name === "bun" || name === "node" ? name : undefined;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function isExecutable(path: string): boolean {
|
|
109
|
+
try {
|
|
110
|
+
accessSync(path, constants.X_OK);
|
|
111
|
+
return true;
|
|
112
|
+
} catch (error) {
|
|
113
|
+
if (
|
|
114
|
+
error instanceof Error &&
|
|
115
|
+
"code" in error &&
|
|
116
|
+
(error.code === "ENOENT" || error.code === "EACCES" || error.code === "ENOTDIR")
|
|
117
|
+
) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function spawnProcessWorker(url: URL, options: JavaScriptProcessWorkerOptions): JavaScriptProcessWorker {
|
|
125
|
+
const command = resolveJavaScriptProcessCommand(options.searchPath, process.platform, undefined, options.execPath);
|
|
126
|
+
const isBun = runtimeNameOf(command) === "bun";
|
|
127
|
+
const args = isBun ? [fileURLToPath(url)] : ["--experimental-strip-types", fileURLToPath(url)];
|
|
128
|
+
const env: NodeJS.ProcessEnv = {
|
|
129
|
+
...process.env,
|
|
130
|
+
...options.env,
|
|
131
|
+
SENPI_CODEMODE_PROCESS_CWD: options.cwd,
|
|
132
|
+
SENPI_CODEMODE_PROCESS_POOL_WIDTH: String(options.parallelPoolWidth),
|
|
133
|
+
};
|
|
134
|
+
const child =
|
|
135
|
+
options.spawn === undefined
|
|
136
|
+
? spawnSubprocess(undefined, { command, args, cwd: options.cwd, env })
|
|
137
|
+
: options.spawn(command, args, { cwd: options.cwd, env });
|
|
138
|
+
const messageHandlers = new Set<(message: KernelToHostMessage) => void>();
|
|
139
|
+
const errorHandlers = new Set<(error: Error) => void>();
|
|
140
|
+
const frameToken = randomBytes(16).toString("hex");
|
|
141
|
+
const framePrefix = `${frameToken} `;
|
|
142
|
+
let crashCause: Error | undefined;
|
|
143
|
+
let stderrTail = "";
|
|
144
|
+
// A line that fails to parse may be the end of a frame a dying child was writing; it is reported only once the
|
|
145
|
+
// channel carries on, so the crash cause or the exit itself, not "invalid frame", settles the cell.
|
|
146
|
+
let pendingInvalidFrame: Error | undefined;
|
|
147
|
+
const reportPendingInvalidFrame = () => {
|
|
148
|
+
const error = pendingInvalidFrame;
|
|
149
|
+
pendingInvalidFrame = undefined;
|
|
150
|
+
if (error !== undefined) for (const handler of [...errorHandlers]) handler(error);
|
|
151
|
+
};
|
|
152
|
+
// Attached directly, not through SubprocessProcess, which stops reading stderr as soon as the exit is observed.
|
|
153
|
+
child.stderr.on("data", (chunk: string | Buffer) => {
|
|
154
|
+
stderrTail = `${stderrTail}${String(chunk)}`.slice(-STDERR_TAIL_CHARS);
|
|
155
|
+
const crash = crashCauseFrom(stderrTail, frameToken);
|
|
156
|
+
if (crash !== undefined) crashCause = crash;
|
|
157
|
+
});
|
|
158
|
+
const subprocess = new SubprocessProcess(child, {
|
|
159
|
+
onLine: (_process, line) => {
|
|
160
|
+
// Only a line carrying this process's token is a frame; any other line is output that
|
|
161
|
+
// reached the channel (a raw fd write, a child process) and is delivered as text.
|
|
162
|
+
if (!line.startsWith(framePrefix)) {
|
|
163
|
+
if (line.trim().length === 0) return;
|
|
164
|
+
reportPendingInvalidFrame();
|
|
165
|
+
for (const handler of [...messageHandlers]) handler({ type: "text", stream: "stdout", data: line });
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
const parsed = decodeProcessFrame(line.slice(framePrefix.length), frameToken);
|
|
169
|
+
if (!parsed.ok) {
|
|
170
|
+
reportPendingInvalidFrame();
|
|
171
|
+
pendingInvalidFrame = new Error(
|
|
172
|
+
`JavaScript kernel process emitted an invalid frame: ${parsed.error.message}`,
|
|
173
|
+
);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
reportPendingInvalidFrame();
|
|
177
|
+
const message = parsed.message;
|
|
178
|
+
if (!isKernelToHostMessage(message)) return;
|
|
179
|
+
for (const handler of [...messageHandlers]) handler(message);
|
|
180
|
+
},
|
|
181
|
+
// Crash causes are read by the listener below, which stays attached until stderr ends.
|
|
182
|
+
onStderr: () => {},
|
|
183
|
+
onExit: (_process, code, signal) => {
|
|
184
|
+
pendingInvalidFrame = undefined;
|
|
185
|
+
// A dead child's last stderr bytes can still be in flight when its exit is observed: settle once stderr has
|
|
186
|
+
// ended (bounded), so the cause it reported is not replaced by the bare exit.
|
|
187
|
+
void stderrEnded(child.stderr, STDERR_DRAIN_GRACE_MS).then(() => {
|
|
188
|
+
// The child's own report of what killed it, as worker mode reports a thread's error; else the exit itself.
|
|
189
|
+
const error = crashCause ?? new JavaScriptWorkerExitedError(code ?? -1, signal);
|
|
190
|
+
for (const handler of [...errorHandlers]) handler(error);
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
onError: (_process, error) => {
|
|
194
|
+
for (const handler of [...errorHandlers]) handler(error);
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
// The token is the first line the entry reads, before any cell can run.
|
|
198
|
+
subprocess.send(`${frameToken}\n`);
|
|
199
|
+
return {
|
|
200
|
+
mode: "process",
|
|
201
|
+
get pid() {
|
|
202
|
+
return child.pid;
|
|
203
|
+
},
|
|
204
|
+
postMessage(message) {
|
|
205
|
+
subprocess.send(`${JSON.stringify(message)}\n`);
|
|
206
|
+
},
|
|
207
|
+
onMessage(handler) {
|
|
208
|
+
messageHandlers.add(handler);
|
|
209
|
+
return () => {
|
|
210
|
+
messageHandlers.delete(handler);
|
|
211
|
+
};
|
|
212
|
+
},
|
|
213
|
+
onError(handler) {
|
|
214
|
+
errorHandlers.add(handler);
|
|
215
|
+
return () => {
|
|
216
|
+
errorHandlers.delete(handler);
|
|
217
|
+
};
|
|
218
|
+
},
|
|
219
|
+
async terminate() {
|
|
220
|
+
const exited = await subprocess.shutdown(`${JSON.stringify({ type: "close" })}\n`);
|
|
221
|
+
if (!exited) await subprocess.terminate("SIGKILL", PROCESS_CLOSE_GRACE_MS);
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const STDERR_TAIL_CHARS = 16 * 1024;
|
|
227
|
+
const STDERR_DRAIN_GRACE_MS = 1_000;
|
|
228
|
+
|
|
229
|
+
/** Resolves when the stream has ended or closed, or after the grace period, whichever comes first. */
|
|
230
|
+
function stderrEnded(stream: NodeJS.ReadableStream, graceMs: number): Promise<void> {
|
|
231
|
+
if (("readableEnded" in stream && stream.readableEnded === true) || ("closed" in stream && stream.closed === true)) {
|
|
232
|
+
return Promise.resolve();
|
|
233
|
+
}
|
|
234
|
+
return new Promise((resolve) => {
|
|
235
|
+
const done = () => {
|
|
236
|
+
clearTimeout(timer);
|
|
237
|
+
stream.removeListener("end", done);
|
|
238
|
+
stream.removeListener("close", done);
|
|
239
|
+
resolve();
|
|
240
|
+
};
|
|
241
|
+
const timer = setTimeout(done, graceMs);
|
|
242
|
+
stream.once("end", done);
|
|
243
|
+
stream.once("close", done);
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
const BIGINT_MARKER = "\u0000senpi:bigint:";
|
|
247
|
+
const UNDEFINED_MARKER = "\u0000senpi:undefined:";
|
|
248
|
+
const BIGINT_DIGITS = /^-?\d+$/;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* A frame's `BigInt` and `undefined` values travel as markers (see process-entry.js `replacer`). They are revived
|
|
252
|
+
* before the frame is checked against the bridge schema: an optional field the child left `undefined` (a cell whose
|
|
253
|
+
* value is `undefined` has no `valueRepr`) arrives as a marker, and checked as-is it would fail the schema.
|
|
254
|
+
*/
|
|
255
|
+
function decodeProcessFrame(line: string, token: string): ReturnType<typeof validateBridgeMessage> {
|
|
256
|
+
const parsed = parseBridgeJsonLine(line);
|
|
257
|
+
if (!parsed.ok) return parsed;
|
|
258
|
+
const value = parsed.value;
|
|
259
|
+
if (typeof value === "object" && value !== null) reviveFrameValues(value, token);
|
|
260
|
+
return validateBridgeMessage(value);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** The cause the child reported on stderr just before exiting (see process-entry.js `reportCrash`). */
|
|
264
|
+
function crashCauseFrom(stderr: string, token: string): Error | undefined {
|
|
265
|
+
const prefix = `senpi-kernel-crash ${token} `;
|
|
266
|
+
const line = stderr.split("\n").find((candidate) => candidate.startsWith(prefix));
|
|
267
|
+
if (line === undefined) return undefined;
|
|
268
|
+
try {
|
|
269
|
+
const cause: unknown = JSON.parse(line.slice(prefix.length));
|
|
270
|
+
if (typeof cause !== "object" || cause === null || !("message" in cause) || typeof cause.message !== "string") {
|
|
271
|
+
return undefined;
|
|
272
|
+
}
|
|
273
|
+
const error = new Error(cause.message);
|
|
274
|
+
if ("name" in cause && typeof cause.name === "string") error.name = cause.name;
|
|
275
|
+
return error;
|
|
276
|
+
} catch {
|
|
277
|
+
return undefined;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Turns the child's markers back, in place, into the values JSON cannot carry (a BigInt, an `undefined` property or
|
|
283
|
+
* element), so a frame matches what worker mode's structured clone delivers. A marker carries this child's frame
|
|
284
|
+
* token, so a cell's own data that merely looks like one is delivered as it is.
|
|
285
|
+
*/
|
|
286
|
+
function reviveFrameValues(container: object, token: string): void {
|
|
287
|
+
const bigintKey = `${BIGINT_MARKER}${token}`;
|
|
288
|
+
const undefinedKey = `${UNDEFINED_MARKER}${token}`;
|
|
289
|
+
for (const key of Object.keys(container)) {
|
|
290
|
+
const value: unknown = Reflect.get(container, key);
|
|
291
|
+
if (typeof value !== "object" || value === null) continue;
|
|
292
|
+
const keys = Object.keys(value);
|
|
293
|
+
const digits: unknown = keys.length === 1 && keys[0] === bigintKey ? Reflect.get(value, bigintKey) : undefined;
|
|
294
|
+
if (typeof digits === "string" && BIGINT_DIGITS.test(digits)) Reflect.set(container, key, BigInt(digits));
|
|
295
|
+
else if (keys.length === 1 && keys[0] === undefinedKey) Reflect.set(container, key, undefined);
|
|
296
|
+
else reviveFrameValues(value, token);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { EvalStatusEvent, KernelToHostMessage } from "../../bridge/protocol.ts";
|
|
2
2
|
import type { JavaScriptRunInput } from "./kernel-contract.ts";
|
|
3
|
+
import { crashedResult } from "./worker-host.ts";
|
|
3
4
|
|
|
4
5
|
type ResultMessage = Extract<KernelToHostMessage, { type: "result" }>;
|
|
5
6
|
|
|
@@ -83,6 +84,12 @@ export class JavaScriptRunQueue {
|
|
|
83
84
|
return Math.max(0, Math.round(finishedAtMs - run.startedAtMs));
|
|
84
85
|
}
|
|
85
86
|
|
|
87
|
+
/** Settles `run` as crashed with `error`: the worker died under it. */
|
|
88
|
+
settleCrashed(run: PendingJavaScriptRun, error: Error): void {
|
|
89
|
+
this.releaseActive(run);
|
|
90
|
+
this.settle(run, crashedResult(run.input.cellId, error, this.durationMs(run, performance.now())));
|
|
91
|
+
}
|
|
92
|
+
|
|
86
93
|
releaseActive(run: PendingJavaScriptRun): boolean {
|
|
87
94
|
if (this.#active !== run) return false;
|
|
88
95
|
this.#active = null;
|
|
@@ -3,6 +3,7 @@ import { kernelToolError } from "./kernel-tools-errors.js";
|
|
|
3
3
|
import { createKernelToolPump } from "./kernel-tools-pump.js";
|
|
4
4
|
import { hostDeniedError, hostToolRefusal } from "./kernel-tools-scope.js";
|
|
5
5
|
import { installSessionCwd } from "./worker-cwd.js";
|
|
6
|
+
import { installPackageResolver } from "./worker-package-resolve.js";
|
|
6
7
|
import { createHeapProbe } from "./worker-heap.js";
|
|
7
8
|
import { createWorkerMemory } from "./worker-memory.js";
|
|
8
9
|
import { JsWorkerRuntime } from "./worker-runtime.js";
|
|
@@ -127,8 +128,9 @@ export function createWorkerCore(transport, options) {
|
|
|
127
128
|
}
|
|
128
129
|
if (message.type === "init") {
|
|
129
130
|
applySessionEnvironment(message.sessionEnv);
|
|
130
|
-
installSessionCwd(options.cwd);
|
|
131
|
+
installSessionCwd(options.cwd, options.cwdInstallOptions);
|
|
131
132
|
installKernelWebView(requestWebViewPort);
|
|
133
|
+
installPackageResolver();
|
|
132
134
|
runtime = new JsWorkerRuntime({
|
|
133
135
|
cwd: options.cwd,
|
|
134
136
|
parallelPoolWidth: options.parallelPoolWidth,
|
|
@@ -137,12 +139,15 @@ export function createWorkerCore(transport, options) {
|
|
|
137
139
|
kernelGeneration: message.kernelGeneration ?? 1,
|
|
138
140
|
hostToolNames: message.hostToolNames ?? [],
|
|
139
141
|
foreignLanguageNames: message.foreignLanguageNames ?? [],
|
|
142
|
+
kernelToolsDisabled: message.kernelToolsDisabled === true,
|
|
140
143
|
onChildEvent: (event) => emit({ type: "status", event: { op: CHILD_LIFECYCLE_OP, ...event } }),
|
|
141
144
|
onShellWaitChange: () => {
|
|
142
145
|
if (activeCell?.interruption) acknowledgeInterrupt();
|
|
143
146
|
},
|
|
144
147
|
});
|
|
145
|
-
|
|
148
|
+
// A process-mode kernel measures its memory host-side as a footprint; the in-heap
|
|
149
|
+
// worker reading would contradict it, so the child keeps memory collection off.
|
|
150
|
+
if (message.memory && !options.processModeMemory) {
|
|
146
151
|
memory = createWorkerMemory(message.memory, (report) => emit({ type: "status", event: { op: MEMORY_COLLECTED_OP, ...report } }));
|
|
147
152
|
memory.captureBaseline();
|
|
148
153
|
}
|
|
@@ -52,8 +52,11 @@ const FS_PATH_ARGS = {
|
|
|
52
52
|
const CHILD_PROCESS_FILE_ARGS = ["spawn", "spawnSync", "execFile", "execFileSync", "fork"];
|
|
53
53
|
const CHILD_PROCESS_COMMAND_ARGS = ["exec", "execSync"];
|
|
54
54
|
|
|
55
|
-
export function installSessionCwd(cwd) {
|
|
56
|
-
|
|
55
|
+
export function installSessionCwd(cwd, options) {
|
|
56
|
+
// A process-mode kernel child IS the main thread; the worker guard relaxes only for that entry.
|
|
57
|
+
if (isMainThread && options?.allowMainThread !== true) {
|
|
58
|
+
throw new Error("installSessionCwd must only run inside a kernel worker thread");
|
|
59
|
+
}
|
|
57
60
|
const root = resolve(cwd);
|
|
58
61
|
const rootBytes = Buffer.from(root.endsWith(sep) ? root : `${root}${sep}`);
|
|
59
62
|
// fs also takes Buffer/Uint8Array paths; a relative one gets the session root prepended as bytes,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { EventEmitter } from "node:events";
|
|
2
2
|
import { Worker } from "node:worker_threads";
|
|
3
3
|
import type { KernelToHostMessage } from "../../bridge/protocol.ts";
|
|
4
|
+
import { describeExit } from "../shared/kernel-death.ts";
|
|
4
5
|
import type { WorkerLike } from "./inline-worker.ts";
|
|
5
6
|
import type { JavaScriptKernelMode } from "./kernel-contract.ts";
|
|
6
7
|
|
|
@@ -22,10 +23,16 @@ export class WorkerStartupCancelledError extends Error {
|
|
|
22
23
|
export class JavaScriptWorkerExitedError extends Error {
|
|
23
24
|
readonly name = "JavaScriptWorkerExitedError";
|
|
24
25
|
readonly exitCode: number;
|
|
26
|
+
readonly exitSignal: string | null;
|
|
25
27
|
|
|
26
|
-
constructor(exitCode: number) {
|
|
27
|
-
super(
|
|
28
|
+
constructor(exitCode: number, exitSignal: string | null = null) {
|
|
29
|
+
super(
|
|
30
|
+
exitSignal === null
|
|
31
|
+
? `JavaScript worker exited with code ${exitCode}`
|
|
32
|
+
: `JavaScript worker exited with ${describeExit(null, exitSignal)} (code ${exitCode})`,
|
|
33
|
+
);
|
|
28
34
|
this.exitCode = exitCode;
|
|
35
|
+
this.exitSignal = exitSignal;
|
|
29
36
|
}
|
|
30
37
|
}
|
|
31
38
|
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { readFileSync, realpathSync, statSync } from "node:fs";
|
|
2
|
+
import { isBuiltin } from "node:module";
|
|
3
|
+
import { dirname, join, sep } from "node:path";
|
|
4
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
5
|
+
|
|
6
|
+
// A cell's bare import runs `import()` from this worker's own module, so the runtime resolves it from Senpi's install
|
|
7
|
+
// tree, not the session's project or its managed package revision. This resolver walks `node_modules` from each root
|
|
8
|
+
// the cell names, in order, and resolves the package entry the way ESM does (the `exports` field with import
|
|
9
|
+
// conditions), so ESM-only packages load and the first root that has the package wins.
|
|
10
|
+
const RESOLVE_PACKAGE = Symbol.for("senpi.kernel.resolvePackage");
|
|
11
|
+
const CONDITIONS = typeof globalThis.Bun === "object" ? ["bun", "node", "import", "default"] : ["node", "import", "default"];
|
|
12
|
+
|
|
13
|
+
function isFile(path) {
|
|
14
|
+
try {
|
|
15
|
+
return statSync(path).isFile();
|
|
16
|
+
} catch {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function isDirectory(path) {
|
|
22
|
+
try {
|
|
23
|
+
return statSync(path).isDirectory();
|
|
24
|
+
} catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function splitSpecifier(specifier) {
|
|
30
|
+
const parts = specifier.split("/");
|
|
31
|
+
const nameParts = specifier.startsWith("@") ? 2 : 1;
|
|
32
|
+
if (parts.length < nameParts || parts.slice(0, nameParts).some((part) => part === "" || part === "." || part === ".."))
|
|
33
|
+
return undefined;
|
|
34
|
+
const rest = parts.slice(nameParts).join("/");
|
|
35
|
+
return { name: parts.slice(0, nameParts).join("/"), subpath: rest === "" ? "." : `./${rest}` };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function conditionalTarget(target) {
|
|
39
|
+
if (typeof target === "string") return target;
|
|
40
|
+
if (Array.isArray(target)) {
|
|
41
|
+
for (const candidate of target) {
|
|
42
|
+
const resolved = conditionalTarget(candidate);
|
|
43
|
+
if (resolved !== undefined) return resolved;
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
if (target === null || typeof target !== "object") return undefined;
|
|
48
|
+
for (const [condition, value] of Object.entries(target)) {
|
|
49
|
+
if (condition !== "default" && !CONDITIONS.includes(condition)) continue;
|
|
50
|
+
const resolved = conditionalTarget(value);
|
|
51
|
+
if (resolved !== undefined) return resolved;
|
|
52
|
+
}
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function exportsTarget(exportsField, subpath) {
|
|
57
|
+
const subpathKeyed =
|
|
58
|
+
exportsField !== null &&
|
|
59
|
+
typeof exportsField === "object" &&
|
|
60
|
+
!Array.isArray(exportsField) &&
|
|
61
|
+
Object.keys(exportsField).some((key) => key.startsWith("."));
|
|
62
|
+
if (!subpathKeyed) return subpath === "." ? conditionalTarget(exportsField) : undefined;
|
|
63
|
+
if (Object.hasOwn(exportsField, subpath)) return conditionalTarget(exportsField[subpath]);
|
|
64
|
+
let best;
|
|
65
|
+
for (const key of Object.keys(exportsField)) {
|
|
66
|
+
const star = key.indexOf("*");
|
|
67
|
+
if (star < 0 || key.includes("*", star + 1)) continue;
|
|
68
|
+
const prefix = key.slice(0, star);
|
|
69
|
+
const suffix = key.slice(star + 1);
|
|
70
|
+
if (!subpath.startsWith(prefix) || !subpath.endsWith(suffix) || subpath.length < prefix.length + suffix.length)
|
|
71
|
+
continue;
|
|
72
|
+
if (best === undefined || prefix.length > best.prefix.length) {
|
|
73
|
+
best = { key, prefix, match: subpath.slice(prefix.length, subpath.length - suffix.length) };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (best === undefined || hasInvalidSegment(best.match)) return undefined;
|
|
77
|
+
const target = conditionalTarget(exportsField[best.key]);
|
|
78
|
+
return target === undefined ? undefined : target.replaceAll("*", best.match);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Node refuses a pattern match or target with "." / ".." / "node_modules" segments (ERR_INVALID_MODULE_SPECIFIER).
|
|
82
|
+
function hasInvalidSegment(path) {
|
|
83
|
+
return path.split(/[\\/]/).some((segment) => segment === "." || segment === ".." || segment === "node_modules");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function legacyFile(path) {
|
|
87
|
+
for (const candidate of [path, `${path}.js`, `${path}.mjs`, `${path}.cjs`, join(path, "index.js")]) {
|
|
88
|
+
if (isFile(candidate)) return candidate;
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function packageEntry(packageDir, subpath, specifier) {
|
|
94
|
+
let manifest;
|
|
95
|
+
try {
|
|
96
|
+
manifest = JSON.parse(readFileSync(join(packageDir, "package.json"), "utf8"));
|
|
97
|
+
} catch {
|
|
98
|
+
return legacyFile(join(packageDir, subpath === "." ? "index.js" : subpath));
|
|
99
|
+
}
|
|
100
|
+
if (manifest.exports !== undefined && manifest.exports !== null) {
|
|
101
|
+
const target = exportsTarget(manifest.exports, subpath);
|
|
102
|
+
if (typeof target !== "string" || !target.startsWith("./") || hasInvalidSegment(target.slice(2))) {
|
|
103
|
+
const error = new Error(`Package subpath '${subpath}' is not exported for import by '${specifier}'`);
|
|
104
|
+
error.code = "ERR_PACKAGE_PATH_NOT_EXPORTED";
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
return join(packageDir, target);
|
|
108
|
+
}
|
|
109
|
+
if (subpath !== ".") return legacyFile(join(packageDir, subpath));
|
|
110
|
+
const moduleEntry = typeof globalThis.Bun === "object" && typeof manifest.module === "string" ? manifest.module : undefined;
|
|
111
|
+
const main = moduleEntry ?? (typeof manifest.main === "string" ? manifest.main : "index.js");
|
|
112
|
+
return legacyFile(join(packageDir, main));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function findPackageDir(name, rootUrl) {
|
|
116
|
+
let directory = fileURLToPath(rootUrl);
|
|
117
|
+
for (;;) {
|
|
118
|
+
const candidate = join(directory, "node_modules", name);
|
|
119
|
+
if (isDirectory(candidate)) return candidate;
|
|
120
|
+
const parent = dirname(directory);
|
|
121
|
+
if (parent === directory) return undefined;
|
|
122
|
+
directory = parent;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* The file URL the bare `specifier` resolves to from the first of `rootUrls` whose `node_modules` chain has its
|
|
128
|
+
* package, or undefined when it names a builtin or no root has the package (the caller then imports it natively).
|
|
129
|
+
*/
|
|
130
|
+
export function resolvePackage(specifier, rootUrls) {
|
|
131
|
+
if (specifier.includes(":") || isBuiltin(specifier)) return undefined;
|
|
132
|
+
const parts = splitSpecifier(specifier);
|
|
133
|
+
if (parts === undefined) return undefined;
|
|
134
|
+
for (const rootUrl of rootUrls) {
|
|
135
|
+
if (typeof rootUrl !== "string") continue;
|
|
136
|
+
const packageDir = findPackageDir(parts.name, rootUrl);
|
|
137
|
+
if (packageDir === undefined) continue;
|
|
138
|
+
const entry = packageEntry(packageDir, parts.subpath, specifier);
|
|
139
|
+
if (entry === undefined) continue;
|
|
140
|
+
let real = entry;
|
|
141
|
+
try {
|
|
142
|
+
real = realpathSync(entry);
|
|
143
|
+
} catch {
|
|
144
|
+
// A missing target fails in the import itself, with the runtime's own error.
|
|
145
|
+
}
|
|
146
|
+
return pathToFileURL(real.endsWith(sep) ? real.slice(0, -1) : real).href;
|
|
147
|
+
}
|
|
148
|
+
return undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function installPackageResolver() {
|
|
152
|
+
globalThis[RESOLVE_PACKAGE] = resolvePackage;
|
|
153
|
+
}
|
|
@@ -47,6 +47,7 @@ export class JsWorkerRuntime {
|
|
|
47
47
|
generation: options.kernelGeneration ?? 1,
|
|
48
48
|
hostToolNames: options.hostToolNames ?? [],
|
|
49
49
|
foreignLanguageNames: options.foreignLanguageNames ?? [],
|
|
50
|
+
disabled: options.kernelToolsDisabled === true,
|
|
50
51
|
});
|
|
51
52
|
this.#installGlobals();
|
|
52
53
|
}
|
|
@@ -4,6 +4,7 @@ import type { WorkerLike } from "./inline-worker.ts";
|
|
|
4
4
|
import { retireWorker, type WorkerRetirement } from "./interrupt-bounds.ts";
|
|
5
5
|
import type { JavaScriptKernelMode } from "./kernel-contract.ts";
|
|
6
6
|
import type { JavaScriptKernelOptions } from "./local-module-loader.ts";
|
|
7
|
+
import type { JavaScriptProcessWorker } from "./process-worker.ts";
|
|
7
8
|
import { KernelWebViewClients } from "./webview-host.ts";
|
|
8
9
|
import { WorkerStartupCancelledError } from "./worker-host.ts";
|
|
9
10
|
import { startWorkerWithInlineFallback } from "./worker-startup.ts";
|
|
@@ -19,6 +20,7 @@ export class WorkerSlot {
|
|
|
19
20
|
readonly #options: JavaScriptKernelOptions;
|
|
20
21
|
readonly #listeners: WorkerSlotListeners;
|
|
21
22
|
#worker: WorkerLike | null = null;
|
|
23
|
+
#lastProcessPid: number | undefined;
|
|
22
24
|
#webViews: KernelWebViewClients | null = null;
|
|
23
25
|
#mode: JavaScriptKernelMode = "worker";
|
|
24
26
|
#generation = 0;
|
|
@@ -34,6 +36,12 @@ export class WorkerSlot {
|
|
|
34
36
|
return this.#mode;
|
|
35
37
|
}
|
|
36
38
|
|
|
39
|
+
get processPid(): number | undefined {
|
|
40
|
+
const worker = this.#worker;
|
|
41
|
+
if (worker?.mode === "process") return (worker as JavaScriptProcessWorker).pid;
|
|
42
|
+
return this.#lastProcessPid;
|
|
43
|
+
}
|
|
44
|
+
|
|
37
45
|
get present(): boolean {
|
|
38
46
|
return this.#worker !== null;
|
|
39
47
|
}
|
|
@@ -105,6 +113,7 @@ export class WorkerSlot {
|
|
|
105
113
|
#publish(worker: WorkerLike, generation: number): void {
|
|
106
114
|
if (!this.#listeners.isOpen() || generation !== this.#generation) throw new WorkerStartupCancelledError();
|
|
107
115
|
this.#worker = worker;
|
|
116
|
+
if (worker.mode === "process") this.#lastProcessPid = (worker as JavaScriptProcessWorker).pid;
|
|
108
117
|
const webViews = new KernelWebViewClients((message, transfer) => worker.postMessage(message, transfer));
|
|
109
118
|
this.#webViews = webViews;
|
|
110
119
|
worker.onMessage((message) => {
|