@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
|
@@ -2,8 +2,10 @@ import { dirname, join } from "node:path";
|
|
|
2
2
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
3
3
|
import { type CodemodeRuntimeAssetEnvironment, requireCodemodeRuntimeAsset } from "../shared/runtime-asset.ts";
|
|
4
4
|
import { createInlineWorker, type WorkerLike } from "./inline-worker.ts";
|
|
5
|
+
import { retireWorker } from "./interrupt-bounds.ts";
|
|
5
6
|
import { resolveKernelToolNameSource } from "./kernel-contract.ts";
|
|
6
7
|
import { type JavaScriptKernelOptions, localBridgeConnection } from "./local-module-loader.ts";
|
|
8
|
+
import { resolveJsProcessEntryUrl, spawnProcessWorker } from "./process-worker.ts";
|
|
7
9
|
import { spawnNodeWorker, WorkerStartupCancelledError, waitForReady } from "./worker-host.ts";
|
|
8
10
|
|
|
9
11
|
export interface JavaScriptWorkerEntryUrlOptions extends CodemodeRuntimeAssetEnvironment {
|
|
@@ -15,6 +17,21 @@ export function resolveJsWorkerEntryUrl(options: JavaScriptWorkerEntryUrlOptions
|
|
|
15
17
|
return pathToFileURL(requireCodemodeRuntimeAsset(localPath, join("kernels", "js", "worker-entry.js"), options));
|
|
16
18
|
}
|
|
17
19
|
|
|
20
|
+
/**
|
|
21
|
+
* How long a process-mode kernel child gets to report ready. A child that a starved host never schedules would
|
|
22
|
+
* otherwise hold every queued cell forever: no cell's own timeout is armed until the kernel is up.
|
|
23
|
+
*/
|
|
24
|
+
export const PROCESS_STARTUP_DEADLINE_MS = 30_000;
|
|
25
|
+
|
|
26
|
+
export class ProcessKernelStartupTimeoutError extends Error {
|
|
27
|
+
constructor(deadlineMs: number) {
|
|
28
|
+
super(
|
|
29
|
+
`JavaScript kernel process did not become ready within ${deadlineMs / 1000}s; it was stopped, and the next cell starts a fresh one`,
|
|
30
|
+
);
|
|
31
|
+
this.name = "ProcessKernelStartupTimeoutError";
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
18
35
|
export interface WorkerStartupHooks {
|
|
19
36
|
readonly options: JavaScriptKernelOptions;
|
|
20
37
|
readonly kernelGeneration: number;
|
|
@@ -36,6 +53,13 @@ export async function startWorkerWithInlineFallback(hooks: WorkerStartupHooks, s
|
|
|
36
53
|
await worker.terminate();
|
|
37
54
|
throw new WorkerStartupCancelledError();
|
|
38
55
|
}
|
|
56
|
+
// Process mode never falls back: the user asked for isolation, so a failed start stays visible. The child that
|
|
57
|
+
// failed to start is stopped (bounded), so a stuck one is not left behind.
|
|
58
|
+
if (worker.mode === "process") {
|
|
59
|
+
hooks.retire(worker);
|
|
60
|
+
await retireWorker(worker);
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
39
63
|
if (worker.mode === "inline") throw error;
|
|
40
64
|
hooks.retire(worker);
|
|
41
65
|
await worker.terminate();
|
|
@@ -47,6 +71,15 @@ export async function startWorkerWithInlineFallback(hooks: WorkerStartupHooks, s
|
|
|
47
71
|
}
|
|
48
72
|
|
|
49
73
|
function spawnWorker(options: JavaScriptKernelOptions): WorkerLike {
|
|
74
|
+
if (options.isolation === "process") {
|
|
75
|
+
const url = resolveJsProcessEntryUrl();
|
|
76
|
+
return spawnProcessWorker(url, {
|
|
77
|
+
cwd: options.cwd,
|
|
78
|
+
parallelPoolWidth: options.parallelPoolWidth,
|
|
79
|
+
...(options.processCommandPath === undefined ? {} : { searchPath: options.processCommandPath }),
|
|
80
|
+
...(options.processExecPath === undefined ? {} : { execPath: options.processExecPath }),
|
|
81
|
+
});
|
|
82
|
+
}
|
|
50
83
|
try {
|
|
51
84
|
const url = options.workerEntryUrl ?? resolveJsWorkerEntryUrl();
|
|
52
85
|
return spawnNodeWorker(url, options.cwd, options.parallelPoolWidth);
|
|
@@ -57,7 +90,10 @@ function spawnWorker(options: JavaScriptKernelOptions): WorkerLike {
|
|
|
57
90
|
}
|
|
58
91
|
|
|
59
92
|
async function initializeWorker(worker: WorkerLike, hooks: WorkerStartupHooks, signal: AbortSignal): Promise<void> {
|
|
60
|
-
const ready =
|
|
93
|
+
const ready =
|
|
94
|
+
worker.mode === "process"
|
|
95
|
+
? withStartupDeadline(waitForReady(worker, signal), hooks.options)
|
|
96
|
+
: waitForReady(worker, signal);
|
|
61
97
|
const options = hooks.options;
|
|
62
98
|
worker.postMessage({
|
|
63
99
|
type: "init",
|
|
@@ -68,6 +104,18 @@ async function initializeWorker(worker: WorkerLike, hooks: WorkerStartupHooks, s
|
|
|
68
104
|
foreignLanguageNames: resolveKernelToolNameSource(options.foreignLanguageNames),
|
|
69
105
|
...(options.sessionEnv === undefined ? {} : { sessionEnv: options.sessionEnv }),
|
|
70
106
|
...(options.memory === undefined ? {} : { memory: options.memory }),
|
|
107
|
+
...(options.kernelToolsEnabled === false ? { kernelToolsDisabled: true } : {}),
|
|
71
108
|
});
|
|
72
109
|
await ready;
|
|
73
110
|
}
|
|
111
|
+
|
|
112
|
+
function withStartupDeadline(ready: Promise<void>, options: JavaScriptKernelOptions): Promise<void> {
|
|
113
|
+
const deadlineMs = options.processStartupDeadlineMs ?? PROCESS_STARTUP_DEADLINE_MS;
|
|
114
|
+
let timer: NodeJS.Timeout | undefined;
|
|
115
|
+
const expired = new Promise<never>((_resolve, reject) => {
|
|
116
|
+
timer = setTimeout(() => reject(new ProcessKernelStartupTimeoutError(deadlineMs)), deadlineMs);
|
|
117
|
+
});
|
|
118
|
+
// When the deadline wins, the stopped child's readiness promise rejects later; that outcome is already reported.
|
|
119
|
+
ready.catch(() => {});
|
|
120
|
+
return Promise.race([ready, expired]).finally(() => clearTimeout(timer));
|
|
121
|
+
}
|
|
@@ -5,6 +5,7 @@ import { DELIVER_EVENT, WebViewPortClient, webViewError } from "./worker-webview
|
|
|
5
5
|
// `Bun` parameter bound to a stand-in whose `WebView` forwards Chrome-backed views to the
|
|
6
6
|
// main-thread service over a private port; every other `Bun.*` member is the real one.
|
|
7
7
|
const KERNEL_BUN = Symbol.for("senpi.kernel.bun");
|
|
8
|
+
const KERNEL_PROCESS_MODE = Symbol.for("senpi.kernel.processMode");
|
|
8
9
|
|
|
9
10
|
export function bindKernelBun(source) {
|
|
10
11
|
if (!globalThis[KERNEL_BUN]) return source;
|
|
@@ -17,6 +18,10 @@ export function installKernelWebView(requestPort) {
|
|
|
17
18
|
globalThis[KERNEL_BUN] = shadowBun(bun, createKernelWebView(bun.WebView, requestPort));
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
export function markKernelProcessMode() {
|
|
22
|
+
globalThis[KERNEL_PROCESS_MODE] = true;
|
|
23
|
+
}
|
|
24
|
+
|
|
20
25
|
function shadowBun(real, WebView) {
|
|
21
26
|
return new Proxy(
|
|
22
27
|
{},
|
|
@@ -35,7 +40,10 @@ function shadowBun(real, WebView) {
|
|
|
35
40
|
}
|
|
36
41
|
|
|
37
42
|
// The macOS default (WebKit) stays a native worker view: its host process serves any thread.
|
|
43
|
+
// A process-mode kernel already runs its cells on the child's main thread, so the native
|
|
44
|
+
// WebView constructs there directly and no MessagePort proxy is ever needed.
|
|
38
45
|
function needsMainThread(options) {
|
|
46
|
+
if (globalThis[KERNEL_PROCESS_MODE]) return false;
|
|
39
47
|
const backend = options?.backend;
|
|
40
48
|
if (backend === undefined) return process.platform !== "darwin";
|
|
41
49
|
return backend === "chrome" || (typeof backend === "object" && backend !== null && backend.type === "chrome");
|
|
@@ -1,42 +1,19 @@
|
|
|
1
1
|
import type { KernelToHostMessage } from "../../bridge/protocol.ts";
|
|
2
2
|
import type { HostCellExecutor } from "../../tool/types.ts";
|
|
3
|
-
|
|
4
|
-
type HostCellOutcome = Awaited<ReturnType<HostCellExecutor>>;
|
|
5
|
-
|
|
3
|
+
import { runHostCell as runSharedHostCell } from "../shared/host-cell.ts";
|
|
6
4
|
import type { PendingRun, ResultMessage } from "./kernel-contract.ts";
|
|
7
|
-
import { failedPythonResult } from "./transport.ts";
|
|
8
5
|
|
|
9
6
|
export function runHostCell(
|
|
10
7
|
pending: PendingRun,
|
|
11
8
|
host: HostCellExecutor,
|
|
12
9
|
io: { readonly emit: (message: KernelToHostMessage) => void; readonly settle: (result: ResultMessage) => void },
|
|
13
10
|
): void {
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const emit = (message: KernelToHostMessage): void => {
|
|
20
|
-
if (!finished && !abort.signal.aborted) io.emit(message);
|
|
21
|
-
};
|
|
22
|
-
// Calling the executor inside a promise keeps a synchronous throw in this cell instead of failing the queue.
|
|
23
|
-
const done = new Promise<HostCellOutcome>((resolve) => resolve(host({ signal: abort.signal, emit }))).then(
|
|
24
|
-
(outcome): ResultMessage =>
|
|
25
|
-
outcome.ok
|
|
26
|
-
? {
|
|
27
|
-
type: "result",
|
|
28
|
-
cellId,
|
|
29
|
-
ok: true,
|
|
30
|
-
durationMs: 0,
|
|
31
|
-
...(outcome.valueRepr === undefined ? {} : { valueRepr: outcome.valueRepr }),
|
|
32
|
-
}
|
|
33
|
-
: { type: "result", cellId, ok: false, error: outcome.error, durationMs: 0 },
|
|
34
|
-
(error: unknown): ResultMessage =>
|
|
35
|
-
failedPythonResult(cellId, error instanceof Error ? error.message : String(error)),
|
|
36
|
-
);
|
|
37
|
-
pending.hostDone = done;
|
|
38
|
-
void done.then((result) => {
|
|
39
|
-
finished = true;
|
|
40
|
-
if (!abort.signal.aborted) io.settle(result);
|
|
11
|
+
const run = runSharedHostCell(pending.input.cellId, host, {
|
|
12
|
+
emit: io.emit,
|
|
13
|
+
settle: io.settle,
|
|
14
|
+
durationMs: () => 0,
|
|
15
|
+
settleAfterAbort: false,
|
|
41
16
|
});
|
|
17
|
+
pending.hostAbort = run.abort;
|
|
18
|
+
pending.hostDone = run.done;
|
|
42
19
|
}
|
|
@@ -704,7 +704,8 @@ class _Entry:
|
|
|
704
704
|
|
|
705
705
|
|
|
706
706
|
class Registry:
|
|
707
|
-
def __init__(self, on_change: Callable[[list[str]], None]) -> None:
|
|
707
|
+
def __init__(self, on_change: Callable[[list[str]], None], *, disabled: bool = False) -> None:
|
|
708
|
+
self._disabled = disabled
|
|
708
709
|
self._lock = threading.Lock()
|
|
709
710
|
self._entries: dict[str, _Entry] = {}
|
|
710
711
|
# Revisions outlive undefine: a redefined name never reuses a revision an old descriptor carries.
|
|
@@ -713,6 +714,8 @@ class Registry:
|
|
|
713
714
|
self.generation = 1
|
|
714
715
|
|
|
715
716
|
def define(self, fn: Any, *, name: Any = None, description: Any = None, schema: Any = None) -> Any:
|
|
717
|
+
if self._disabled:
|
|
718
|
+
raise KernelToolError("tools_unavailable", "kernel tools are turned off for this project (kernelTools.enabled is false)")
|
|
716
719
|
inferred = infer_tool(fn, name=name, description=description, schema=schema)
|
|
717
720
|
key = tool_key(inferred["name"])
|
|
718
721
|
with self._lock:
|
|
@@ -983,7 +986,12 @@ async def _awaited(awaitable: Any) -> Any:
|
|
|
983
986
|
|
|
984
987
|
KERNEL_TOOL_TOKEN = OwnerToken()
|
|
985
988
|
KERNEL_TOOL_STREAMS = (ThreadRoutedStream(stderr=False), ThreadRoutedStream(stderr=True))
|
|
986
|
-
|
|
989
|
+
# kernelTools.enabled is read once, while the prelude loads and before any cell runs, then removed from the
|
|
990
|
+
# environment: rewriting os.environ cannot turn kernel tools back on, and cell subprocesses never see the switch.
|
|
991
|
+
KERNEL_TOOL_REGISTRY = Registry(
|
|
992
|
+
lambda names: emit({"type": "kernel-tools-defined", "names": names}),
|
|
993
|
+
disabled=os.environ.pop("SENPI_CODEMODE_KERNEL_TOOLS", None) == "0",
|
|
994
|
+
)
|
|
987
995
|
KERNEL_TOOL_RUNNER = KernelToolRunner(emit, KERNEL_TOOL_TOKEN, KERNEL_TOOL_REGISTRY, KERNEL_TOOL_STREAMS)
|
|
988
996
|
|
|
989
997
|
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { isAbsolute } from "node:path";
|
|
2
4
|
import type { Readable, Writable } from "node:stream";
|
|
3
5
|
|
|
4
6
|
export interface KernelChild {
|
|
@@ -53,6 +55,8 @@ export function defaultSpawn(options: KernelSpawnOptions): KernelChild {
|
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
export function splitCommand(commandLine: string): { readonly command: string; readonly args: readonly string[] } {
|
|
58
|
+
// A configured interpreter path is one executable even when it contains spaces.
|
|
59
|
+
if (isAbsolute(commandLine) && existsSync(commandLine)) return { command: commandLine, args: [] };
|
|
56
60
|
const [command, ...args] = commandLine.split(" ").filter(Boolean);
|
|
57
61
|
if (!command) throw new Error("Python interpreter path is empty");
|
|
58
62
|
return { command, args };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { ResolvedSandbox } from "../../config/feature-settings.ts";
|
|
2
|
+
import { marshalToolResult } from "../../tool/image.ts";
|
|
3
|
+
import type { ExecuteTool, HostCellExecutor } from "../../tool/types.ts";
|
|
4
|
+
import type { CodemodeSandbox } from "./vendor/pi-codemode/runtime/host.ts";
|
|
5
|
+
import type { CodemodeError, CodemodeOutputFrame, CodemodeTool } from "./vendor/pi-codemode/types.ts";
|
|
6
|
+
|
|
7
|
+
export interface SandboxCellOptions {
|
|
8
|
+
readonly sandbox: ResolvedSandbox;
|
|
9
|
+
readonly executeTool: ExecuteTool;
|
|
10
|
+
readonly toolNames: () => readonly string[];
|
|
11
|
+
readonly describeTool?: (name: string) => string | undefined;
|
|
12
|
+
/** Where the QuickJS wasm lives; by default the installed `quickjs-wasi` package's file. */
|
|
13
|
+
readonly wasmPath?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// On the script's first line so reported line numbers still match the cell; globals, so a cell's own
|
|
17
|
+
// `const print` shadows them instead of failing as a redeclaration.
|
|
18
|
+
const OUTPUT_ALIASES =
|
|
19
|
+
'globalThis.print = (...values) => text(values.map((value) => (typeof value === "string" ? value : JSON.stringify(value))).join(" ")); globalThis.display = (value) => ((typeof value === "string" && value.startsWith("data:image/")) || (value !== null && typeof value === "object" && ("image_url" in value || value.type === "image")) ? image(value) : text(typeof value === "string" ? value : JSON.stringify(value))); globalThis.tool = tools; ';
|
|
20
|
+
|
|
21
|
+
const ERROR_CODES: Partial<Record<CodemodeError["kind"], string>> = {
|
|
22
|
+
timeout: "eval_isolate_timeout",
|
|
23
|
+
aborted: "eval_isolate_aborted",
|
|
24
|
+
sandbox: "eval_isolate_unavailable",
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function codeFor(error: CodemodeError): string | undefined {
|
|
28
|
+
if (error.kind === "script" && error.reason === "memory") return "eval_isolate_memory_limit";
|
|
29
|
+
if (error.kind === "script" && error.name === "CodemodeStoreDisabledError") return "eval_isolate_no_state";
|
|
30
|
+
if (error.kind === "script" && error.reason === "unresolved") return "eval_isolate_unresolved_promise";
|
|
31
|
+
return ERROR_CODES[error.kind];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The persistent kernel never sees an isolated cell; its tools go through executeTool so permission hooks still apply. */
|
|
35
|
+
export function sandboxCellExecutor(code: string, options: SandboxCellOptions): HostCellExecutor {
|
|
36
|
+
return async ({ signal, emit }) => {
|
|
37
|
+
let sandbox: CodemodeSandbox;
|
|
38
|
+
try {
|
|
39
|
+
// Lazy: the vendored QuickJS runtime loads only when a session runs its first isolated cell. The wasm is loaded
|
|
40
|
+
// here, before any of the cell runs, so a missing runtime is reported as unavailable instead of failing inside
|
|
41
|
+
// the script.
|
|
42
|
+
const runtime = await import("./vendor/pi-codemode/runtime/host.ts");
|
|
43
|
+
const { loadQuickJSWasm } = await import("./vendor/pi-codemode/wasm.ts");
|
|
44
|
+
const wasm = await loadQuickJSWasm(options.wasmPath);
|
|
45
|
+
const items = new Map<number, string[]>();
|
|
46
|
+
const tools: CodemodeTool[] = options
|
|
47
|
+
.toolNames()
|
|
48
|
+
.filter((name) => name !== "eval")
|
|
49
|
+
.map((name) => ({
|
|
50
|
+
name,
|
|
51
|
+
description: options.describeTool?.(name) ?? "",
|
|
52
|
+
execute: async (args: unknown, context: { signal: AbortSignal }) =>
|
|
53
|
+
marshalToolResult(await options.executeTool(name, args, { signal: context.signal })),
|
|
54
|
+
}));
|
|
55
|
+
sandbox = new runtime.CodemodeSandbox({
|
|
56
|
+
wasm,
|
|
57
|
+
tools,
|
|
58
|
+
timeoutMs: options.sandbox.timeoutSeconds * 1_000,
|
|
59
|
+
memoryLimitBytes: options.sandbox.memoryMb * 1024 * 1024,
|
|
60
|
+
output: "stream",
|
|
61
|
+
builtins: { store: "reject" },
|
|
62
|
+
onOutputFrame: (frame: CodemodeOutputFrame) => {
|
|
63
|
+
const parts = items.get(frame.itemId) ?? [];
|
|
64
|
+
parts.push(frame.chunk);
|
|
65
|
+
if (!frame.final) {
|
|
66
|
+
items.set(frame.itemId, parts);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
items.delete(frame.itemId);
|
|
70
|
+
const payload = parts.join("");
|
|
71
|
+
if (frame.type === "image") {
|
|
72
|
+
emit({ type: "display", mimeType: frame.mimeType ?? "image/png", dataBase64: payload });
|
|
73
|
+
} else {
|
|
74
|
+
emit({ type: "text", stream: "stdout", data: payload.endsWith("\n") ? payload : `${payload}\n` });
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
} catch (error) {
|
|
79
|
+
// The full error, paths included, goes to the host's own log so a maintainer can diagnose it; the cell gets
|
|
80
|
+
// only what failed and the error's code. This covers every setup step: the runtime import, the tool list,
|
|
81
|
+
// the wasm load and the sandbox itself.
|
|
82
|
+
console.error("[senpi-codemode] isolated cell setup failed:", error);
|
|
83
|
+
const code =
|
|
84
|
+
error instanceof Error && "code" in error && typeof error.code === "string" ? ` (${error.code})` : "";
|
|
85
|
+
return {
|
|
86
|
+
ok: false,
|
|
87
|
+
error: {
|
|
88
|
+
name: "SandboxUnavailableError",
|
|
89
|
+
message: `eval_isolate_unavailable: the QuickJS runtime for isolated cells could not be loaded${code}; nothing in the cell ran`,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const result = await sandbox.execute(OUTPUT_ALIASES + code, { signal });
|
|
95
|
+
if (result.ok) {
|
|
96
|
+
return result.value === undefined
|
|
97
|
+
? { ok: true }
|
|
98
|
+
: {
|
|
99
|
+
ok: true,
|
|
100
|
+
valueRepr: typeof result.value === "string" ? result.value : JSON.stringify(result.value),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
const errorCode = codeFor(result.error);
|
|
104
|
+
return {
|
|
105
|
+
ok: false,
|
|
106
|
+
error: {
|
|
107
|
+
...(result.error.name === undefined ? {} : { name: result.error.name }),
|
|
108
|
+
message: errorCode === undefined ? result.error.message : `${errorCode}: ${result.error.message}`,
|
|
109
|
+
...(result.error.stack === undefined ? {} : { stack: result.error.stack }),
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
} finally {
|
|
113
|
+
await sandbox.close();
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
@@ -40,3 +40,4 @@ A sync must re-apply both kinds: dropping a marked line (for example `"output-fr
|
|
|
40
40
|
- A normal finish settles only after every frame is consumed. An abort or timeout settles at once and drops frames still queued. The timer and the abort signal stay armed while a finished run's last frames drain, so a late abort, timeout or `close()` abandons them too (the run settles with that error): a consumer that never returns cannot keep a run alive.
|
|
41
41
|
- `frameBytes` and `windowBytes` must be at least 4, so a frame can always hold a whole surrogate pair.
|
|
42
42
|
2. **Builtin store policy** (`builtins: { store: "collect" | "reject" }`; `types.ts`, `runtime/protocol.ts`, `runtime/worker.ts`, `runtime/host.ts`, `runtime/prelude-source.ts`). With `"reject"`, `load()` returns `undefined` and `store()` throws `CodemodeStoreDisabledError`. The names stay defined, so scripts written for upstream fail with a clear message instead of a ReferenceError.
|
|
43
|
+
3. **Runtime-set error reasons** (`types.ts` `CodemodeError.reason`, `runtime/prelude-source.ts`, `runtime/worker.ts`). The prelude captures the engine's own `InternalError` before user code runs and tags a failure `reason: "memory"` only for an instance of it whose message is exactly "out of memory"; its stalled-promise failure is tagged `reason: "unresolved"`; the worker tags an engine out-of-memory from `evalCode` the same way; and `text()` rethrows the engine's own error instead of replacing it with a `TypeError`. The host maps the isolate error codes from `reason` only, never from message text. Upstream has no `reason`, so its behaviour is unchanged.
|
|
@@ -46,6 +46,10 @@ export const PRELUDE_SOURCE: string = `(function (bridge, toolsJson, globalsJson
|
|
|
46
46
|
const options = optionsJson === undefined ? {} : JSON.parse(optionsJson);
|
|
47
47
|
const streamOutput = options.streamOutput === true;
|
|
48
48
|
const rejectStore = options.store === "reject";
|
|
49
|
+
// The engine's own InternalError, captured before user code runs: a failure is tagged by identity, never by its
|
|
50
|
+
// text, so a cell's own error that merely mentions memory keeps its own code. A script can still construct this
|
|
51
|
+
// constructor itself and so label its own failure as the memory limit; that mislabels only its own cell.
|
|
52
|
+
const EngineInternalError = typeof InternalError === "function" ? InternalError : undefined;
|
|
49
53
|
// senpi-change end
|
|
50
54
|
"use strict";
|
|
51
55
|
const stringify = JSON.stringify;
|
|
@@ -94,7 +98,13 @@ export const PRELUDE_SOURCE: string = `(function (bridge, toolsJson, globalsJson
|
|
|
94
98
|
|
|
95
99
|
function describeError(error) {
|
|
96
100
|
if (error instanceof ErrorCtor) {
|
|
97
|
-
|
|
101
|
+
// senpi-change begin: the engine's out-of-memory error is reported by identity, not by matching its message
|
|
102
|
+
const reason =
|
|
103
|
+
EngineInternalError !== undefined && error instanceof EngineInternalError && error.message === "out of memory"
|
|
104
|
+
? "memory"
|
|
105
|
+
: undefined;
|
|
106
|
+
return stringify({ name: error.name, message: error.message, stack: errorText(error), reason });
|
|
107
|
+
// senpi-change end
|
|
98
108
|
}
|
|
99
109
|
return stringify({ message: format(error) });
|
|
100
110
|
}
|
|
@@ -285,6 +295,8 @@ export const PRELUDE_SOURCE: string = `(function (bridge, toolsJson, globalsJson
|
|
|
285
295
|
try {
|
|
286
296
|
rendered = outputText(value);
|
|
287
297
|
} catch (error) {
|
|
298
|
+
// The engine's own out-of-memory error keeps its identity, so it is still reported as the memory limit.
|
|
299
|
+
if (EngineInternalError !== undefined && error instanceof EngineInternalError) throw error; // senpi-change
|
|
288
300
|
throw new TypeErrorCtor(error instanceof ErrorCtor ? error.message : String(error));
|
|
289
301
|
}
|
|
290
302
|
output("text", rendered);
|
|
@@ -423,6 +435,7 @@ export const PRELUDE_SOURCE: string = `(function (bridge, toolsJson, globalsJson
|
|
|
423
435
|
name: "Error",
|
|
424
436
|
message:
|
|
425
437
|
"The script is waiting on a promise that can never settle: no tool call is pending, and timers do not exist here.",
|
|
438
|
+
reason: "unresolved", // senpi-change: tagged by the runtime, so a script's own error text never matches it
|
|
426
439
|
}),
|
|
427
440
|
);
|
|
428
441
|
return true;
|
|
@@ -48,7 +48,10 @@ function discardOutput(memory: { readonly buffer: ArrayBufferLike }) {
|
|
|
48
48
|
function describeException(error: JSException): string {
|
|
49
49
|
const head = error.message ? `${error.name}: ${error.message}` : error.name;
|
|
50
50
|
const stack = error.stack?.trimEnd();
|
|
51
|
-
|
|
51
|
+
// senpi-change begin: evalCode fails before any script runs, so an InternalError here is the engine's own
|
|
52
|
+
const reason = error.name === "InternalError" && error.message === "out of memory" ? "memory" : undefined;
|
|
53
|
+
return JSON.stringify({ name: error.name, message: error.message, stack: stack ? `${head}\n${stack}` : head, reason });
|
|
54
|
+
// senpi-change end
|
|
52
55
|
}
|
|
53
56
|
// senpi-change begin: output streaming
|
|
54
57
|
/**
|
|
@@ -71,6 +71,8 @@ export interface CodemodeError {
|
|
|
71
71
|
name?: string;
|
|
72
72
|
message: string;
|
|
73
73
|
stack?: string;
|
|
74
|
+
/** senpi-change: set by the runtime itself (never from script text) for its own memory-limit and stalled failures. */
|
|
75
|
+
reason?: "memory" | "unresolved";
|
|
74
76
|
}
|
|
75
77
|
|
|
76
78
|
/** Keys the script changed with `store()`. Only successful executions report writes. */
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { KernelToHostMessage } from "../../bridge/protocol.ts";
|
|
2
|
+
import type { HostCellExecutor } from "../../tool/types.ts";
|
|
3
|
+
|
|
4
|
+
type HostCellOutcome = Awaited<ReturnType<HostCellExecutor>>;
|
|
5
|
+
type HostResult = Extract<KernelToHostMessage, { type: "result" }>;
|
|
6
|
+
|
|
7
|
+
export interface HostCellRun {
|
|
8
|
+
readonly cellId: string;
|
|
9
|
+
readonly abort: AbortController;
|
|
10
|
+
readonly done: Promise<HostResult>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Runs a host entry (a cell the host executes, such as an install) for one queue slot: output after the outcome or after
|
|
15
|
+
* an abort belongs to no cell, a synchronous throw stays in this cell, and `settle` receives the cell's result.
|
|
16
|
+
* `settleAfterAbort` says whether an aborted entry still settles here or whoever aborted it settles the cell.
|
|
17
|
+
*/
|
|
18
|
+
export function runHostCell(
|
|
19
|
+
cellId: string,
|
|
20
|
+
host: HostCellExecutor,
|
|
21
|
+
io: {
|
|
22
|
+
readonly emit: (message: KernelToHostMessage) => void;
|
|
23
|
+
readonly settle: (result: HostResult) => void;
|
|
24
|
+
readonly durationMs: () => number;
|
|
25
|
+
readonly settleAfterAbort: boolean;
|
|
26
|
+
},
|
|
27
|
+
): HostCellRun {
|
|
28
|
+
const abort = new AbortController();
|
|
29
|
+
let finished = false;
|
|
30
|
+
const emit = (message: KernelToHostMessage): void => {
|
|
31
|
+
if (!finished && !abort.signal.aborted) io.emit(message);
|
|
32
|
+
};
|
|
33
|
+
const done = new Promise<HostCellOutcome>((resolve) => resolve(host({ signal: abort.signal, emit }))).then(
|
|
34
|
+
(outcome): HostResult =>
|
|
35
|
+
outcome.ok
|
|
36
|
+
? {
|
|
37
|
+
type: "result",
|
|
38
|
+
cellId,
|
|
39
|
+
ok: true,
|
|
40
|
+
durationMs: io.durationMs(),
|
|
41
|
+
...(outcome.valueRepr === undefined ? {} : { valueRepr: outcome.valueRepr }),
|
|
42
|
+
}
|
|
43
|
+
: { type: "result", cellId, ok: false, error: outcome.error, durationMs: 0 },
|
|
44
|
+
(error: unknown): HostResult => ({
|
|
45
|
+
type: "result",
|
|
46
|
+
cellId,
|
|
47
|
+
ok: false,
|
|
48
|
+
error: { message: error instanceof Error ? error.message : String(error) },
|
|
49
|
+
durationMs: 0,
|
|
50
|
+
}),
|
|
51
|
+
);
|
|
52
|
+
void done.then((result) => {
|
|
53
|
+
finished = true;
|
|
54
|
+
if (io.settleAfterAbort || !abort.signal.aborted) io.settle(result);
|
|
55
|
+
});
|
|
56
|
+
return { cellId, abort, done };
|
|
57
|
+
}
|
|
@@ -151,6 +151,11 @@ export class SubprocessProcess {
|
|
|
151
151
|
}
|
|
152
152
|
|
|
153
153
|
private kill(signal: NodeJS.Signals): void {
|
|
154
|
+
if (globalThis.process.platform === "win32") {
|
|
155
|
+
if (this.child.pid !== undefined) killProcessTree(this.child.pid);
|
|
156
|
+
else this.child.kill(signal);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
154
159
|
if (this.child.pid !== undefined) {
|
|
155
160
|
try {
|
|
156
161
|
globalThis.process.kill(-this.child.pid, signal);
|
|
@@ -163,6 +168,15 @@ export class SubprocessProcess {
|
|
|
163
168
|
}
|
|
164
169
|
}
|
|
165
170
|
|
|
171
|
+
// Windows has no process-group signal: taskkill /T /F walks the tree rooted at the child.
|
|
172
|
+
function killProcessTree(pid: number): void {
|
|
173
|
+
try {
|
|
174
|
+
nodeSpawn("taskkill", ["/PID", String(pid), "/T", "/F"], { stdio: "ignore" });
|
|
175
|
+
} catch (error) {
|
|
176
|
+
if (!(error instanceof Error)) throw error;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
166
180
|
export function spawnSubprocess(spawn: SubprocessSpawn | undefined, request: SubprocessSpawnRequest): SubprocessLike {
|
|
167
181
|
if (spawn) return spawn(request.command, request.args, { cwd: request.cwd, env: request.env });
|
|
168
182
|
const child = nodeSpawn(request.command, [...request.args], {
|
|
@@ -18,6 +18,8 @@ export interface EvalPromptParts {
|
|
|
18
18
|
|
|
19
19
|
export interface EvalPromptOptions {
|
|
20
20
|
readonly spawns: boolean;
|
|
21
|
+
/** `prompt.advertiseHelpers`: true appends one pointer line to the helper documentation entry. */
|
|
22
|
+
readonly advertiseHelpers?: boolean;
|
|
21
23
|
/** Whether the session registry exposes the monitor tool through eval. */
|
|
22
24
|
readonly monitor?: boolean;
|
|
23
25
|
readonly spawnDefaultAgent?: string;
|
|
@@ -74,6 +76,10 @@ export function evalEmphasisStyle(modelId: string | undefined): EvalEmphasisStyl
|
|
|
74
76
|
type ContextValue = string | boolean;
|
|
75
77
|
type Context = Readonly<Record<string, ContextValue>>;
|
|
76
78
|
|
|
79
|
+
/** The single line `prompt.advertiseHelpers: true` adds; off by default, so the default description is unchanged. */
|
|
80
|
+
export const ADVERTISED_HELPERS_LINE =
|
|
81
|
+
"Advanced cell helpers (handle controls, wait(), kernel tools in workpools) are documented on demand: tool_schema('eval:helpers').";
|
|
82
|
+
|
|
77
83
|
export function buildEvalPrompt(
|
|
78
84
|
enabled: EnabledLanguages,
|
|
79
85
|
options: EvalPromptOptions = { spawns: false },
|
|
@@ -104,9 +110,10 @@ export function buildEvalPrompt(
|
|
|
104
110
|
maxDetachedCells: String(options.maxDetachedCells ?? DEFAULT_MAX_DETACHED_CELLS),
|
|
105
111
|
kernelPreludeDocs: (options.kernelPreludes ?? []).map((prelude) => prelude.documentation).join("\n"),
|
|
106
112
|
};
|
|
107
|
-
const
|
|
113
|
+
const rendered = renderTemplate(EVAL_PROMPT_TEMPLATE, context)
|
|
108
114
|
.replace(/\n{3,}/g, "\n\n")
|
|
109
115
|
.trim();
|
|
116
|
+
const description = options.advertiseHelpers === true ? `${rendered}\n\n${ADVERTISED_HELPERS_LINE}` : rendered;
|
|
110
117
|
return {
|
|
111
118
|
description,
|
|
112
119
|
promptSnippet: "Run one incremental code cell in a persistent language kernel.",
|
package/src/tool/AGENTS.md
CHANGED
|
@@ -17,6 +17,7 @@ anchor most suites).
|
|
|
17
17
|
| Code preview layout | `code-preview.ts` -> `display-code.ts` (JS: `display-js*.ts` Bun-only masked `Bun.Transpiler`; Python: `display-python*.ts` user interpreter, background + repaint) |
|
|
18
18
|
| Status events, execution events | `status-events.ts`, `eval-execution-event.ts` |
|
|
19
19
|
| Interrupt, capture | `interrupt-note.ts`, `call-capture.ts` |
|
|
20
|
+
| Magic cells | `magic-cells.ts` (parsing), `magic-cell-host.ts` (`%pip`, `%bun`/`%npm`, `%environment`), `load-cell.ts` (`%load`); installs live in `../environments/` |
|
|
20
21
|
|
|
21
22
|
## CONVENTIONS
|
|
22
23
|
|
package/src/tool/cell-handler.ts
CHANGED
|
@@ -235,22 +235,20 @@ export class CellHandler {
|
|
|
235
235
|
this.#kernel.deliverToolReply({ type: "tool-reply", callId: message.callId, ok: true, value: reply.value });
|
|
236
236
|
} catch (error) {
|
|
237
237
|
if (!this.#state.active) return;
|
|
238
|
-
const
|
|
239
|
-
error instanceof Error ? error.
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
238
|
+
const code =
|
|
239
|
+
error instanceof Error && "code" in error && typeof error.code === "string" ? error.code : undefined;
|
|
240
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
241
|
+
// A blocked call (a permission denial or another hook's veto) is not an argument problem.
|
|
242
|
+
const text =
|
|
243
|
+
code === "blocked"
|
|
244
|
+
? errorMessage
|
|
245
|
+
: appendSchemaHint(errorMessage, message.toolName, this.#toolParameters(message.toolName));
|
|
243
246
|
recordToolCall(this.#state.toolCalls, false, capture, undefined, text);
|
|
244
247
|
this.#kernel.deliverToolReply({
|
|
245
248
|
type: "tool-reply",
|
|
246
249
|
callId: message.callId,
|
|
247
250
|
ok: false,
|
|
248
|
-
error: {
|
|
249
|
-
message: text,
|
|
250
|
-
...(error instanceof Error && "code" in error && typeof error.code === "string"
|
|
251
|
-
? { code: error.code }
|
|
252
|
-
: {}),
|
|
253
|
-
},
|
|
251
|
+
error: { message: text, ...(code === undefined ? {} : { code }) },
|
|
254
252
|
});
|
|
255
253
|
}
|
|
256
254
|
this.#resultBuilder.emitUpdate(false);
|
package/src/tool/eval-request.ts
CHANGED
|
@@ -17,9 +17,19 @@ export function normalizeEvalSummary(value: unknown): string | undefined {
|
|
|
17
17
|
return normalized.length === 0 ? undefined : normalized;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
export class EvalIsolateInvalidError extends TypeError {
|
|
21
|
+
readonly name = "EvalIsolateInvalidError";
|
|
22
|
+
readonly code = "eval_isolate_invalid";
|
|
23
|
+
|
|
24
|
+
constructor(reason: string) {
|
|
25
|
+
super(`eval_isolate_invalid: ${reason}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
20
29
|
export function parseEvalRequest(
|
|
21
30
|
params: unknown,
|
|
22
31
|
enabledLanguages: readonly EvalLanguage[] = evalLanguageOrder,
|
|
32
|
+
options: { readonly sandbox?: boolean } = {},
|
|
23
33
|
): EvalToolRequest {
|
|
24
34
|
if (!isRecord(params)) throw new TypeError("eval parameters must be an object");
|
|
25
35
|
if (params.action === "list") return { action: "list" };
|
|
@@ -30,7 +40,9 @@ export function parseEvalRequest(
|
|
|
30
40
|
}
|
|
31
41
|
if (params.action !== undefined && params.action !== "run")
|
|
32
42
|
throw new TypeError(`Unknown eval action "${String(params.action)}"`);
|
|
33
|
-
if (
|
|
43
|
+
if (params.language === undefined) throw new TypeError(evalRunRequiresLanguageMessage(enabledLanguages));
|
|
44
|
+
if (!isEvalLanguage(params.language))
|
|
45
|
+
throw new TypeError(`eval run language must be one of: ${enabledLanguages.join(", ")}`);
|
|
34
46
|
if (typeof params.code !== "string")
|
|
35
47
|
throw new TypeError("eval run requires code — the cell body to execute, verbatim");
|
|
36
48
|
const summary = normalizeEvalSummary(params.summary);
|
|
@@ -40,6 +52,15 @@ export function parseEvalRequest(
|
|
|
40
52
|
);
|
|
41
53
|
if (params.on_timeout !== undefined && params.on_timeout !== "detach" && params.on_timeout !== "error")
|
|
42
54
|
throw new TypeError(`Unknown eval on_timeout value "${String(params.on_timeout)}"`);
|
|
55
|
+
if (params.isolate !== undefined && params.isolate !== false) {
|
|
56
|
+
if (params.isolate !== true) throw new EvalIsolateInvalidError("isolate must be true or false");
|
|
57
|
+
if (options.sandbox !== true) {
|
|
58
|
+
throw new EvalIsolateInvalidError("sandbox cells are turned off (set sandbox.enabled in codemode settings)");
|
|
59
|
+
}
|
|
60
|
+
if (params.language !== "js")
|
|
61
|
+
throw new EvalIsolateInvalidError('sandbox cells run JavaScript only (language: "js")');
|
|
62
|
+
if (params.reset === true) throw new EvalIsolateInvalidError("a sandbox cell starts fresh; reset does not apply");
|
|
63
|
+
}
|
|
43
64
|
return {
|
|
44
65
|
language: params.language,
|
|
45
66
|
code: params.code,
|
|
@@ -48,6 +69,7 @@ export function parseEvalRequest(
|
|
|
48
69
|
...(typeof params.timeout === "number" ? { timeout: params.timeout } : {}),
|
|
49
70
|
...(params.on_timeout === "detach" || params.on_timeout === "error" ? { on_timeout: params.on_timeout } : {}),
|
|
50
71
|
...(typeof params.reset === "boolean" ? { reset: params.reset } : {}),
|
|
72
|
+
...(params.isolate === true ? { isolate: true } : {}),
|
|
51
73
|
};
|
|
52
74
|
}
|
|
53
75
|
|
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
import type { EvalSchemaToolInfo } from "../bridges/schema-bridge.ts";
|
|
9
9
|
import type { CompletionRequest, CompletionResult } from "../completion/handler.ts";
|
|
10
10
|
import type { ResolvedCodemodeSettings } from "../config/settings.ts";
|
|
11
|
+
import type { JsEnvironments } from "../environments/js-environments.ts";
|
|
11
12
|
import type { PythonEnvironments } from "../environments/python-environments.ts";
|
|
12
13
|
import type { EvalExecutionTracker } from "../extension/session-manager.ts";
|
|
13
14
|
import type { HandleRegistry } from "../handles/handle-registry.ts";
|
|
@@ -72,6 +73,8 @@ export interface CreateEvalToolOptions {
|
|
|
72
73
|
readonly kernelPreludes?: () => readonly KernelPreludeContribution[];
|
|
73
74
|
/** The session's Python environments: `%pip` / `%environment` cells and the import root of every Python cell. */
|
|
74
75
|
readonly pythonEnvironments?: PythonEnvironments;
|
|
76
|
+
/** The session's managed JavaScript packages: `%bun add` / `%npm add` cells and the bare-import fallback root. */
|
|
77
|
+
readonly jsEnvironments?: JsEnvironments;
|
|
75
78
|
/** Contributions whose documentation lines the description lists; snapshot taken when the tool is (re)registered. */
|
|
76
79
|
readonly promptKernelPreludes?: readonly KernelPreludeContribution[];
|
|
77
80
|
}
|