@code-yeongyu/senpi-codemode 2026.10.10-10 → 2026.10.10-12
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 +46 -0
- package/package.json +4 -4
- package/src/bridge/memory-protocol.ts +8 -1
- package/src/bridge/protocol.ts +1 -1
- package/src/bridges/eval-environment-schemas.ts +5 -1
- package/src/environments/editable-check.ts +1 -1
- package/src/extension/session-cwd.ts +25 -9
- package/src/extension/session-manager.ts +4 -4
- package/src/kernels/jl/runner.jl +11 -22
- package/src/kernels/js/group-signal-notice.js +117 -0
- package/src/kernels/js/inline-worker.ts +2 -0
- package/src/kernels/js/local-module-loader.ts +8 -0
- package/src/kernels/js/process-tree-host.ts +4 -8
- package/src/kernels/js/process-tree.d.ts +11 -0
- package/src/kernels/js/process-tree.js +55 -10
- package/src/kernels/js/windows-tree-kill-host.ts +117 -0
- package/src/kernels/js/windows-tree-kill.d.ts +19 -0
- package/src/kernels/js/windows-tree-kill.js +100 -0
- package/src/kernels/js/worker-cwd.js +41 -10
- package/src/kernels/js/worker-runtime.js +23 -4
- package/src/kernels/js/worker-shell-capture.js +4 -0
- package/src/kernels/js/worker-slot.ts +2 -3
- package/src/kernels/py/prelude.py +36 -25
- package/src/kernels/py/transport.ts +4 -0
- package/src/kernels/rb/runner.rb +9 -19
- package/src/kernels/sandbox/vendor/pi-codemode/runtime/host.ts +2 -2
- package/src/kernels/shared/bun-ffi.ts +24 -0
- package/src/kernels/shared/kernel-memory-host.ts +30 -7
- package/src/kernels/shared/process-cpu.ts +7 -3
- package/src/kernels/shared/process-group-cpu.ts +6 -3
- package/src/kernels/shared/subprocess-kernel.ts +29 -3
- package/src/kernels/shared/subprocess-memory-globals.ts +101 -0
- package/src/kernels/shared/subprocess-process.ts +20 -12
- package/src/kernels/shared/subprocess-queue.ts +1 -0
- package/src/tool/eval-tool.ts +2 -1
- package/src/tool/render-agent.ts +110 -0
- package/src/tool/render-blocks.ts +252 -0
- package/src/tool/render-call.ts +130 -0
- package/src/tool/render-cell.ts +165 -0
- package/src/tool/render-detail.ts +152 -0
- package/src/tool/render-live.ts +288 -0
- package/src/tool/render-result.ts +202 -0
- package/src/tool/render-status.ts +166 -0
- package/src/tool/render-tail.ts +139 -0
- package/src/tool/render.ts +3 -1128
- package/src/tool/run-eval-cell.ts +7 -12
- package/src/tool/types.ts +11 -2
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
// Host-side twin of windows-tree-kill.js (the worker imports that one; .ts files may not import .js). `taskkill /T`
|
|
4
|
+
// adopts every process whose recorded ParentProcessId equals the root's pid, and Windows never rewrites that
|
|
5
|
+
// field when a parent exits and reuses pids (senpi#2999). The tree is computed from one process listing instead: a process
|
|
6
|
+
// belongs to it only when it started at or after the parent it names, and each pid is killed by name.
|
|
7
|
+
|
|
8
|
+
const LISTING_TIMEOUT_MS = 5_000;
|
|
9
|
+
const LISTING_MAX_BUFFER = 16 * 1024 * 1024;
|
|
10
|
+
|
|
11
|
+
const ROWS_SCRIPT = `Get-CimInstance Win32_Process | Where-Object { $_.ProcessId -ne $PID } | ForEach-Object {
|
|
12
|
+
$created = if ($_.CreationDate) { $_.CreationDate.ToFileTimeUtc() } else { 0 }
|
|
13
|
+
"{0} {1} {2} {3}" -f $_.ProcessId, $_.ParentProcessId, $created, $_.Name }`;
|
|
14
|
+
|
|
15
|
+
const PROTECTED_IMAGES = new Set([
|
|
16
|
+
"system",
|
|
17
|
+
"smss.exe",
|
|
18
|
+
"csrss.exe",
|
|
19
|
+
"wininit.exe",
|
|
20
|
+
"winlogon.exe",
|
|
21
|
+
"services.exe",
|
|
22
|
+
"lsass.exe",
|
|
23
|
+
"lsaiso.exe",
|
|
24
|
+
"svchost.exe",
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
export interface WindowsProcessRow {
|
|
28
|
+
readonly pid: number;
|
|
29
|
+
readonly parentPid: number;
|
|
30
|
+
readonly createdAt: bigint;
|
|
31
|
+
readonly name: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function parseWindowsRows(stdout: string): WindowsProcessRow[] {
|
|
35
|
+
const rows: WindowsProcessRow[] = [];
|
|
36
|
+
for (const line of stdout.split(/\r?\n/u)) {
|
|
37
|
+
const [pidText, parentText, createdText, ...nameParts] = line.trim().split(/\s+/u);
|
|
38
|
+
const pid = Number(pidText);
|
|
39
|
+
const parentPid = Number(parentText);
|
|
40
|
+
if (!Number.isInteger(pid) || pid <= 0 || !Number.isInteger(parentPid) || !/^\d+$/u.test(createdText ?? ""))
|
|
41
|
+
continue;
|
|
42
|
+
rows.push({ pid, parentPid, createdAt: BigInt(createdText ?? "0"), name: nameParts.join(" ") });
|
|
43
|
+
}
|
|
44
|
+
return rows;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The root and its genuine descendants; any protected image or ancestor of `selfPid` in it shrinks the kill to the root. */
|
|
48
|
+
export function windowsTreeKillPids(rows: readonly WindowsProcessRow[], rootPid: number, selfPid: number): number[] {
|
|
49
|
+
const byPid = new Map(rows.map((row) => [row.pid, row]));
|
|
50
|
+
if (!byPid.has(rootPid)) return [];
|
|
51
|
+
const tree = new Set<number>([rootPid]);
|
|
52
|
+
let grew = true;
|
|
53
|
+
while (grew) {
|
|
54
|
+
grew = false;
|
|
55
|
+
for (const row of rows) {
|
|
56
|
+
if (tree.has(row.pid) || !tree.has(row.parentPid)) continue;
|
|
57
|
+
const parent = byPid.get(row.parentPid);
|
|
58
|
+
if (parent === undefined || row.createdAt < parent.createdAt) continue;
|
|
59
|
+
tree.add(row.pid);
|
|
60
|
+
grew = true;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const ancestors = new Set<number>([selfPid]);
|
|
64
|
+
for (let current = byPid.get(selfPid); current !== undefined; ) {
|
|
65
|
+
const parent = byPid.get(current.parentPid);
|
|
66
|
+
if (parent === undefined || ancestors.has(parent.pid) || parent.createdAt > current.createdAt) break;
|
|
67
|
+
ancestors.add(parent.pid);
|
|
68
|
+
current = parent;
|
|
69
|
+
}
|
|
70
|
+
const refused = [...tree].some(
|
|
71
|
+
(pid) => PROTECTED_IMAGES.has((byPid.get(pid)?.name ?? "").toLowerCase()) || ancestors.has(pid),
|
|
72
|
+
);
|
|
73
|
+
if (!refused) return [...tree];
|
|
74
|
+
const rootRefused = PROTECTED_IMAGES.has((byPid.get(rootPid)?.name ?? "").toLowerCase()) || ancestors.has(rootPid);
|
|
75
|
+
return rootRefused ? [] : [rootPid];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** `taskkill` arguments: the checked tree pid by pid, or `/T` on the root when no listing could be read. */
|
|
79
|
+
export function windowsTreeKillArgs(
|
|
80
|
+
rootPid: number,
|
|
81
|
+
rows: readonly WindowsProcessRow[] | undefined,
|
|
82
|
+
selfPid: number = process.pid,
|
|
83
|
+
): string[] {
|
|
84
|
+
if (rows === undefined) return ["/F", "/T", "/PID", String(rootPid)];
|
|
85
|
+
const pids = windowsTreeKillPids(rows, rootPid, selfPid);
|
|
86
|
+
if (pids.length === 0) return [];
|
|
87
|
+
return ["/F", ...pids.flatMap((pid) => ["/PID", String(pid)])];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function listWindowsRows(): Promise<WindowsProcessRow[] | undefined> {
|
|
91
|
+
return new Promise((resolve) => {
|
|
92
|
+
execFile(
|
|
93
|
+
"powershell.exe",
|
|
94
|
+
["-NoProfile", "-NonInteractive", "-Command", ROWS_SCRIPT],
|
|
95
|
+
{ timeout: LISTING_TIMEOUT_MS, maxBuffer: LISTING_MAX_BUFFER, windowsHide: true },
|
|
96
|
+
(error, stdout) => {
|
|
97
|
+
const rows = error ? [] : parseWindowsRows(String(stdout ?? ""));
|
|
98
|
+
resolve(rows.length > 0 ? rows : undefined);
|
|
99
|
+
},
|
|
100
|
+
);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Kills the checked process tree rooted at `pid`; resolves once taskkill has run (or nothing needed killing).
|
|
106
|
+
* A batch passes one shared `listing` so every tree is planned from the same snapshot.
|
|
107
|
+
*/
|
|
108
|
+
export async function killWindowsTree(
|
|
109
|
+
pid: number,
|
|
110
|
+
listing: Promise<readonly WindowsProcessRow[] | undefined> = listWindowsRows(),
|
|
111
|
+
): Promise<void> {
|
|
112
|
+
const args = windowsTreeKillArgs(pid, await listing);
|
|
113
|
+
if (args.length === 0) return;
|
|
114
|
+
await new Promise<void>((resolve) => {
|
|
115
|
+
execFile("taskkill", args, { timeout: LISTING_TIMEOUT_MS, windowsHide: true }, () => resolve());
|
|
116
|
+
});
|
|
117
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface WindowsProcessRow {
|
|
2
|
+
readonly pid: number;
|
|
3
|
+
readonly parentPid: number;
|
|
4
|
+
readonly createdAt: bigint;
|
|
5
|
+
readonly name: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function parseWindowsRows(stdout: string): WindowsProcessRow[];
|
|
9
|
+
export function windowsTreeKillPids(rows: readonly WindowsProcessRow[], rootPid: number, selfPid: number): number[];
|
|
10
|
+
export function windowsTreeKillArgs(
|
|
11
|
+
rootPid: number,
|
|
12
|
+
rows: readonly WindowsProcessRow[] | undefined,
|
|
13
|
+
selfPid?: number,
|
|
14
|
+
): string[];
|
|
15
|
+
export function listWindowsRows(): Promise<WindowsProcessRow[] | undefined>;
|
|
16
|
+
export function killWindowsTree(
|
|
17
|
+
pid: number,
|
|
18
|
+
listing?: Promise<readonly WindowsProcessRow[] | undefined>,
|
|
19
|
+
): Promise<void>;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
// Plain JS (with windows-tree-kill.d.ts) because the worker runtime imports it. `taskkill /T`
|
|
4
|
+
// adopts every process whose recorded ParentProcessId equals the root's pid, and Windows never rewrites that
|
|
5
|
+
// field when a parent exits and reuses pids (senpi#2999). The tree is computed from one process listing instead: a process
|
|
6
|
+
// belongs to it only when it started at or after the parent it names, and each pid is killed by name.
|
|
7
|
+
|
|
8
|
+
const LISTING_TIMEOUT_MS = 5_000;
|
|
9
|
+
const LISTING_MAX_BUFFER = 16 * 1024 * 1024;
|
|
10
|
+
|
|
11
|
+
const ROWS_SCRIPT = `Get-CimInstance Win32_Process | Where-Object { $_.ProcessId -ne $PID } | ForEach-Object {
|
|
12
|
+
$created = if ($_.CreationDate) { $_.CreationDate.ToFileTimeUtc() } else { 0 }
|
|
13
|
+
"{0} {1} {2} {3}" -f $_.ProcessId, $_.ParentProcessId, $created, $_.Name }`;
|
|
14
|
+
|
|
15
|
+
const PROTECTED_IMAGES = new Set([
|
|
16
|
+
"system",
|
|
17
|
+
"smss.exe",
|
|
18
|
+
"csrss.exe",
|
|
19
|
+
"wininit.exe",
|
|
20
|
+
"winlogon.exe",
|
|
21
|
+
"services.exe",
|
|
22
|
+
"lsass.exe",
|
|
23
|
+
"lsaiso.exe",
|
|
24
|
+
"svchost.exe",
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
export function parseWindowsRows(stdout) {
|
|
28
|
+
const rows = [];
|
|
29
|
+
for (const line of stdout.split(/\r?\n/u)) {
|
|
30
|
+
const [pidText, parentText, createdText, ...nameParts] = line.trim().split(/\s+/u);
|
|
31
|
+
const pid = Number(pidText);
|
|
32
|
+
const parentPid = Number(parentText);
|
|
33
|
+
if (!Number.isInteger(pid) || pid <= 0 || !Number.isInteger(parentPid) || !/^\d+$/u.test(createdText ?? "")) continue;
|
|
34
|
+
rows.push({ pid, parentPid, createdAt: BigInt(createdText ?? "0"), name: nameParts.join(" ") });
|
|
35
|
+
}
|
|
36
|
+
return rows;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The root and its genuine descendants; any protected image or ancestor of `selfPid` in it shrinks the kill to the root. */
|
|
40
|
+
export function windowsTreeKillPids(rows, rootPid, selfPid) {
|
|
41
|
+
const byPid = new Map(rows.map((row) => [row.pid, row]));
|
|
42
|
+
if (!byPid.has(rootPid)) return [];
|
|
43
|
+
const tree = new Set([rootPid]);
|
|
44
|
+
let grew = true;
|
|
45
|
+
while (grew) {
|
|
46
|
+
grew = false;
|
|
47
|
+
for (const row of rows) {
|
|
48
|
+
if (tree.has(row.pid) || !tree.has(row.parentPid)) continue;
|
|
49
|
+
const parent = byPid.get(row.parentPid);
|
|
50
|
+
if (parent === undefined || row.createdAt < parent.createdAt) continue;
|
|
51
|
+
tree.add(row.pid);
|
|
52
|
+
grew = true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const ancestors = new Set([selfPid]);
|
|
56
|
+
for (let current = byPid.get(selfPid); current !== undefined; ) {
|
|
57
|
+
const parent = byPid.get(current.parentPid);
|
|
58
|
+
if (parent === undefined || ancestors.has(parent.pid) || parent.createdAt > current.createdAt) break;
|
|
59
|
+
ancestors.add(parent.pid);
|
|
60
|
+
current = parent;
|
|
61
|
+
}
|
|
62
|
+
const refused = [...tree].some((pid) => PROTECTED_IMAGES.has((byPid.get(pid)?.name ?? "").toLowerCase()) || ancestors.has(pid));
|
|
63
|
+
if (!refused) return [...tree];
|
|
64
|
+
const rootRefused = PROTECTED_IMAGES.has(byPid.get(rootPid).name.toLowerCase()) || ancestors.has(rootPid);
|
|
65
|
+
return rootRefused ? [] : [rootPid];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** `taskkill` arguments: the checked tree pid by pid, or `/T` on the root when no listing could be read. */
|
|
69
|
+
export function windowsTreeKillArgs(rootPid, rows, selfPid = process.pid) {
|
|
70
|
+
if (rows === undefined) return ["/F", "/T", "/PID", String(rootPid)];
|
|
71
|
+
const pids = windowsTreeKillPids(rows, rootPid, selfPid);
|
|
72
|
+
if (pids.length === 0) return [];
|
|
73
|
+
return ["/F", ...pids.flatMap((pid) => ["/PID", String(pid)])];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function listWindowsRows() {
|
|
77
|
+
return new Promise((resolve) => {
|
|
78
|
+
execFile(
|
|
79
|
+
"powershell.exe",
|
|
80
|
+
["-NoProfile", "-NonInteractive", "-Command", ROWS_SCRIPT],
|
|
81
|
+
{ timeout: LISTING_TIMEOUT_MS, maxBuffer: LISTING_MAX_BUFFER, windowsHide: true },
|
|
82
|
+
(error, stdout) => {
|
|
83
|
+
const rows = error ? [] : parseWindowsRows(String(stdout ?? ""));
|
|
84
|
+
resolve(rows.length > 0 ? rows : undefined);
|
|
85
|
+
},
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Kills the checked process tree rooted at `pid`; resolves once taskkill has run (or nothing needed killing).
|
|
92
|
+
* A batch passes one shared `listing` so every tree is planned from the same snapshot.
|
|
93
|
+
*/
|
|
94
|
+
export async function killWindowsTree(pid, listing = listWindowsRows()) {
|
|
95
|
+
const args = windowsTreeKillArgs(pid, await listing);
|
|
96
|
+
if (args.length === 0) return;
|
|
97
|
+
await new Promise((resolve) => {
|
|
98
|
+
execFile("taskkill", args, { timeout: LISTING_TIMEOUT_MS, windowsHide: true }, () => resolve());
|
|
99
|
+
});
|
|
100
|
+
}
|
|
@@ -51,6 +51,7 @@ const FS_PATH_ARGS = {
|
|
|
51
51
|
|
|
52
52
|
const CHILD_PROCESS_FILE_ARGS = ["spawn", "spawnSync", "execFile", "execFileSync", "fork"];
|
|
53
53
|
const CHILD_PROCESS_COMMAND_ARGS = ["exec", "execSync"];
|
|
54
|
+
const ASYNC_GROUP_SPAWNERS = new Set(["spawn", "fork"]);
|
|
54
55
|
|
|
55
56
|
export function installSessionCwd(cwd, options) {
|
|
56
57
|
// A process-mode kernel child IS the main thread; the worker guard relaxes only for that entry.
|
|
@@ -95,7 +96,9 @@ function patchFs(fs, promises, at, root) {
|
|
|
95
96
|
function patchChildProcess(childProcess, at, root) {
|
|
96
97
|
for (const name of CHILD_PROCESS_FILE_ARGS) {
|
|
97
98
|
replace(childProcess, name, (fn) => {
|
|
98
|
-
|
|
99
|
+
// Only asynchronous spawners start a new group: a synchronous call keeps the terminal (ssh/sudo/git prompts),
|
|
100
|
+
// and Bun ignores `detached` for execFile. Those calls get a notice instead (group-signal-notice.js).
|
|
101
|
+
const withCwd = withOptionsCwd(fn, fileOptionsIndex, root, ASYNC_GROUP_SPAWNERS.has(name));
|
|
99
102
|
return name === "fork" ? resolvingArgs(withCwd, [0], at) : withCwd;
|
|
100
103
|
});
|
|
101
104
|
}
|
|
@@ -107,7 +110,7 @@ function patchBun(bun, at, root) {
|
|
|
107
110
|
replace(bun, "file", (fn) => resolvingArgs(fn, [0], at));
|
|
108
111
|
replace(bun, "write", (fn) => resolvingArgs(fn, [0], at));
|
|
109
112
|
replace(bun, "spawn", (fn) => withBunSpawnCwd(fn, root));
|
|
110
|
-
replace(bun, "spawnSync", (fn) => withBunSpawnCwd(fn, root));
|
|
113
|
+
replace(bun, "spawnSync", (fn) => withBunSpawnCwd(fn, root, false));
|
|
111
114
|
if (typeof bun.$?.cwd === "function") bun.$.cwd(root);
|
|
112
115
|
const glob = bun.Glob?.prototype;
|
|
113
116
|
if (glob) {
|
|
@@ -149,32 +152,60 @@ function fileOptionsIndex(args) {
|
|
|
149
152
|
return Array.isArray(args[1]) || (args[1] == null && args.length > 2) ? 2 : 1;
|
|
150
153
|
}
|
|
151
154
|
|
|
152
|
-
function withOptionsCwd(fn, optionsIndex, root) {
|
|
155
|
+
function withOptionsCwd(fn, optionsIndex, root, inOwnGroup = false) {
|
|
156
|
+
const group = inOwnGroup ? ownGroup : (options) => options;
|
|
153
157
|
return function withSessionCwd(...args) {
|
|
154
158
|
const index = optionsIndex(args);
|
|
155
159
|
const options = args[index];
|
|
156
|
-
if (options === undefined || options === null) args[index] = { cwd: root };
|
|
157
|
-
else if (typeof options === "function") args.splice(index, 0, { cwd: root });
|
|
158
|
-
else if (typeof options === "object") args[index] = { ...options, cwd: cwdFrom(options.cwd, root) };
|
|
159
|
-
|
|
160
|
+
if (options === undefined || options === null) args[index] = group({ cwd: root });
|
|
161
|
+
else if (typeof options === "function") args.splice(index, 0, group({ cwd: root }));
|
|
162
|
+
else if (typeof options === "object") args[index] = group({ ...options, cwd: cwdFrom(options.cwd, root) });
|
|
163
|
+
if (!inOwnGroup || process.platform === "win32" || (isPlainOptions(options) && options.detached !== undefined)) {
|
|
164
|
+
return fn.apply(this, args);
|
|
165
|
+
}
|
|
166
|
+
// Bun's child_process spawns through Bun.spawn, whose child tracking skips `detached` children as ones the
|
|
167
|
+
// cell keeps on purpose. While this call runs, `detached` is ours, so the child is still retired with the cell.
|
|
168
|
+
const depth = globalThis[INJECTED_GROUP] ?? 0;
|
|
169
|
+
globalThis[INJECTED_GROUP] = depth + 1;
|
|
170
|
+
try {
|
|
171
|
+
return fn.apply(this, args);
|
|
172
|
+
} finally {
|
|
173
|
+
globalThis[INJECTED_GROUP] = depth;
|
|
174
|
+
}
|
|
160
175
|
};
|
|
161
176
|
}
|
|
162
177
|
|
|
163
|
-
function withBunSpawnCwd(fn, root) {
|
|
178
|
+
function withBunSpawnCwd(fn, root, inOwnGroup = true) {
|
|
179
|
+
const group = inOwnGroup ? ownGroup : (options) => options;
|
|
164
180
|
return function spawnInSessionCwd(...args) {
|
|
165
181
|
const [first, second] = args;
|
|
166
182
|
if (Array.isArray(first)) {
|
|
167
183
|
const options = second === undefined || second === null ? {} : second;
|
|
168
184
|
if (typeof options !== "object") return fn.apply(this, args);
|
|
169
|
-
return fn.call(this, first, { ...options, cwd: cwdFrom(options.cwd, root) }, ...args.slice(2));
|
|
185
|
+
return fn.call(this, first, group({ ...options, cwd: cwdFrom(options.cwd, root) }), ...args.slice(2));
|
|
170
186
|
}
|
|
171
187
|
if (first !== null && typeof first === "object") {
|
|
172
|
-
return fn.call(this, { ...first, cwd: cwdFrom(first.cwd, root) }, ...args.slice(1));
|
|
188
|
+
return fn.call(this, group({ ...first, cwd: cwdFrom(first.cwd, root) }), ...args.slice(1));
|
|
173
189
|
}
|
|
174
190
|
return fn.apply(this, args);
|
|
175
191
|
};
|
|
176
192
|
}
|
|
177
193
|
|
|
194
|
+
// senpi#2995: a JS kernel runs inside the agent process, so a child spawned from a cell would join the agent's
|
|
195
|
+
// process group, and a cell that later signals that group (`kill -TERM -- -$PGID`) stops the agent itself. On
|
|
196
|
+
// POSIX every cell child starts its own group instead; kernel teardown still retires it by pid and descendants.
|
|
197
|
+
// A cell that sets `detached` itself keeps its choice. Windows is left alone: there `detached` opens a console.
|
|
198
|
+
export const INJECTED_GROUP = Symbol.for("senpi.kernel.injectedProcessGroup");
|
|
199
|
+
|
|
200
|
+
function isPlainOptions(value) {
|
|
201
|
+
return value !== null && typeof value === "object";
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function ownGroup(options) {
|
|
205
|
+
if (process.platform === "win32" || options.detached !== undefined) return options;
|
|
206
|
+
return { ...options, detached: true };
|
|
207
|
+
}
|
|
208
|
+
|
|
178
209
|
function globScanOptions(options, root) {
|
|
179
210
|
if (options === undefined || options === null) return { cwd: root };
|
|
180
211
|
if (typeof options === "string") return cwdFrom(options, root);
|
|
@@ -5,8 +5,9 @@ import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
|
5
5
|
import { dirname, isAbsolute, join, normalize, resolve, sep } from "node:path";
|
|
6
6
|
import { inspect } from "node:util";
|
|
7
7
|
import { encodeDisplayImage, resolveDisplayOps } from "./display-image.js";
|
|
8
|
-
import { terminateProcessTrees } from "./process-tree.js";
|
|
8
|
+
import { terminateProcessGroups, terminateProcessTrees } from "./process-tree.js";
|
|
9
9
|
import { awaitMaybePromise, indirectEval, wrapUserCode } from "./worker-indirect-eval.js";
|
|
10
|
+
import { INJECTED_GROUP } from "./worker-cwd.js";
|
|
10
11
|
import { installShellCapture } from "./worker-shell-capture.js";
|
|
11
12
|
import { bindKernelBun } from "./worker-webview.js";
|
|
12
13
|
import { createWorkpool } from "./workpool.js";
|
|
@@ -33,6 +34,9 @@ export class JsWorkerRuntime {
|
|
|
33
34
|
#hooks = null;
|
|
34
35
|
#pendingDisplays = [];
|
|
35
36
|
#children = new Set();
|
|
37
|
+
// Groups the worker created for cell children (senpi#2995). A group outlives its leader, so a grandchild re-parented
|
|
38
|
+
// to init is still found through it at retirement (senpi#3020).
|
|
39
|
+
#ownedGroups = new Map();
|
|
36
40
|
#childrenStopping;
|
|
37
41
|
#shellWaits = new Set();
|
|
38
42
|
#onChildEvent;
|
|
@@ -111,9 +115,14 @@ export class JsWorkerRuntime {
|
|
|
111
115
|
#trackChild(child, spawnOptions) {
|
|
112
116
|
if (child === null || typeof child !== "object" || typeof child.kill !== "function") return;
|
|
113
117
|
// `detached: true` is the cell saying it wants the process to outlive it.
|
|
114
|
-
|
|
118
|
+
const groupInjected = (globalThis[INJECTED_GROUP] ?? 0) > 0;
|
|
119
|
+
if (isPlainObject(spawnOptions) && spawnOptions.detached === true && !groupInjected) return;
|
|
115
120
|
this.#children.add(child);
|
|
116
121
|
const pid = Number.isInteger(child.pid) && child.pid > 0 ? child.pid : null;
|
|
122
|
+
// Bun.spawn children get their group from worker-cwd.js without the injection marker, so every tracked child is
|
|
123
|
+
// recorded; a child that is not a group leader names no group, and signalling it later finds nothing.
|
|
124
|
+
const ownGroupLeader = !(isPlainObject(spawnOptions) && spawnOptions.detached === false);
|
|
125
|
+
if (pid !== null && process.platform !== "win32" && ownGroupLeader) this.#ownedGroups.set(pid, child);
|
|
117
126
|
// The host keeps its own copy of live pids: if this worker is terminated
|
|
118
127
|
// while blocked, only the host can still retire them.
|
|
119
128
|
if (pid !== null) this.#onChildEvent?.({ pid, state: "spawned" });
|
|
@@ -127,10 +136,20 @@ export class JsWorkerRuntime {
|
|
|
127
136
|
#terminateChildren() {
|
|
128
137
|
const children = [...this.#children].filter(child => child.exitCode === null && child.signalCode === null);
|
|
129
138
|
this.#children.clear();
|
|
130
|
-
|
|
139
|
+
// A leader that already exited leaves its group id reserved only while the group has members; if that pid now
|
|
140
|
+
// names a live process, it was reused by someone else and the group is not ours to signal (senpi#3020 review).
|
|
141
|
+
const groups = [...this.#ownedGroups].map(([pgid, child]) => ({
|
|
142
|
+
pgid,
|
|
143
|
+
leaderExited: child.exitCode !== null || child.signalCode !== null,
|
|
144
|
+
}));
|
|
145
|
+
this.#ownedGroups.clear();
|
|
146
|
+
if (children.length === 0 && groups.length === 0) return undefined;
|
|
131
147
|
const roots = children.map(child => child.pid).filter(pid => Number.isInteger(pid) && pid > 0);
|
|
132
148
|
const settled = Promise.allSettled(children.map(child => (child.exited instanceof Promise ? child.exited : Promise.resolve())));
|
|
133
|
-
return
|
|
149
|
+
return Promise.all([
|
|
150
|
+
roots.length > 0 ? terminateProcessTrees(roots, { graceMs: CHILD_TERMINATION_GRACE_MS, settled }) : undefined,
|
|
151
|
+
terminateProcessGroups(groups, { graceMs: CHILD_TERMINATION_GRACE_MS }),
|
|
152
|
+
]);
|
|
134
153
|
}
|
|
135
154
|
|
|
136
155
|
async #drainPendingDisplays() {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { assertCellLive } from "./cell-run-context.js";
|
|
2
|
+
import { groupSignalNotice, noticeChildProcessGroupSignals, shellCommandText, signalsProcessGroup } from "./group-signal-notice.js";
|
|
2
3
|
|
|
3
4
|
const SHELL_CONFIG_METHODS = ["env", "cwd", "nothrow", "throws"];
|
|
4
5
|
const SHELL_READ_METHODS = ["text", "json", "lines", "arrayBuffer", "bytes", "blob"];
|
|
@@ -28,7 +29,9 @@ export function installShellCapture(options) {
|
|
|
28
29
|
bun.$ = capturedShell(originalShell, options);
|
|
29
30
|
bun.spawn = capturedSpawn(originalSpawn, options, pinEnv);
|
|
30
31
|
if (originalSpawnSync !== null) bun.spawnSync = capturedSpawnSync(originalSpawnSync, pinEnv);
|
|
32
|
+
const restoreChildProcessNotice = noticeChildProcessGroupSignals(options.emitText, options.isActive);
|
|
31
33
|
return () => {
|
|
34
|
+
restoreChildProcessNotice();
|
|
32
35
|
bun.$ = originalShell;
|
|
33
36
|
bun.spawn = originalSpawn;
|
|
34
37
|
if (originalSpawnSync !== null) bun.spawnSync = originalSpawnSync;
|
|
@@ -43,6 +46,7 @@ function capturedShell(originalShell, options) {
|
|
|
43
46
|
const shell = (strings, ...expressions) => {
|
|
44
47
|
assertCellLive();
|
|
45
48
|
if (!options.isActive()) return originalShell(strings, ...expressions);
|
|
49
|
+
if (signalsProcessGroup(shellCommandText(strings, expressions))) options.emitText("stderr", groupSignalNotice("Bun.$"));
|
|
46
50
|
const promise = originalShell(isolateStdin(strings), ...expressions);
|
|
47
51
|
return captureShellPromise(promise, options);
|
|
48
52
|
};
|
|
@@ -4,7 +4,6 @@ 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";
|
|
8
7
|
import { KernelWebViewClients } from "./webview-host.ts";
|
|
9
8
|
import { WorkerStartupCancelledError } from "./worker-host.ts";
|
|
10
9
|
import { startWorkerWithInlineFallback } from "./worker-startup.ts";
|
|
@@ -38,7 +37,7 @@ export class WorkerSlot {
|
|
|
38
37
|
|
|
39
38
|
get processPid(): number | undefined {
|
|
40
39
|
const worker = this.#worker;
|
|
41
|
-
if (worker?.mode === "process") return
|
|
40
|
+
if (worker?.mode === "process") return worker.pid;
|
|
42
41
|
return this.#lastProcessPid;
|
|
43
42
|
}
|
|
44
43
|
|
|
@@ -113,7 +112,7 @@ export class WorkerSlot {
|
|
|
113
112
|
#publish(worker: WorkerLike, generation: number): void {
|
|
114
113
|
if (!this.#listeners.isOpen() || generation !== this.#generation) throw new WorkerStartupCancelledError();
|
|
115
114
|
this.#worker = worker;
|
|
116
|
-
if (worker.mode === "process") this.#lastProcessPid =
|
|
115
|
+
if (worker.mode === "process") this.#lastProcessPid = worker.pid;
|
|
117
116
|
const webViews = new KernelWebViewClients((message, transfer) => worker.postMessage(message, transfer));
|
|
118
117
|
this.#webViews = webViews;
|
|
119
118
|
worker.onMessage((message) => {
|
|
@@ -661,25 +661,6 @@ class ThreadRoutedStream(io.TextIOBase):
|
|
|
661
661
|
)
|
|
662
662
|
self.cell_target: io.StringIO | None = None
|
|
663
663
|
|
|
664
|
-
@contextlib.contextmanager
|
|
665
|
-
def capture(self, buffer: io.StringIO, *, cell: bool) -> Iterator[None]:
|
|
666
|
-
me = threading.get_ident()
|
|
667
|
-
previous = self._targets.get(me)
|
|
668
|
-
self._targets[me] = buffer
|
|
669
|
-
inherited = self._inherited.set(buffer)
|
|
670
|
-
if cell:
|
|
671
|
-
self.cell_target = buffer
|
|
672
|
-
try:
|
|
673
|
-
yield
|
|
674
|
-
finally:
|
|
675
|
-
self._inherited.reset(inherited)
|
|
676
|
-
if previous is None:
|
|
677
|
-
self._targets.pop(me, None)
|
|
678
|
-
else:
|
|
679
|
-
self._targets[me] = previous
|
|
680
|
-
if cell:
|
|
681
|
-
self.cell_target = None
|
|
682
|
-
|
|
683
664
|
def write(self, text: str) -> int:
|
|
684
665
|
target = self._targets.get(threading.get_ident()) or self._inherited.get() or self.cell_target
|
|
685
666
|
if target is None:
|
|
@@ -694,6 +675,38 @@ class ThreadRoutedStream(io.TextIOBase):
|
|
|
694
675
|
return None
|
|
695
676
|
|
|
696
677
|
|
|
678
|
+
@contextlib.contextmanager
|
|
679
|
+
def capture_streams(
|
|
680
|
+
streams: tuple[ThreadRoutedStream, ThreadRoutedStream],
|
|
681
|
+
buffers: tuple[io.StringIO, io.StringIO],
|
|
682
|
+
*,
|
|
683
|
+
cell: bool,
|
|
684
|
+
) -> Iterator[None]:
|
|
685
|
+
stdout, stderr = streams
|
|
686
|
+
out, err = buffers
|
|
687
|
+
me = threading.get_ident()
|
|
688
|
+
previous_out, previous_err = stdout._targets.get(me), stderr._targets.get(me)
|
|
689
|
+
stdout._targets[me], stderr._targets[me] = out, err
|
|
690
|
+
inherited_out, inherited_err = stdout._inherited.set(out), stderr._inherited.set(err)
|
|
691
|
+
if cell:
|
|
692
|
+
stdout.cell_target, stderr.cell_target = out, err
|
|
693
|
+
try:
|
|
694
|
+
yield
|
|
695
|
+
finally:
|
|
696
|
+
stderr._inherited.reset(inherited_err)
|
|
697
|
+
stdout._inherited.reset(inherited_out)
|
|
698
|
+
if previous_err is None:
|
|
699
|
+
stderr._targets.pop(me, None)
|
|
700
|
+
else:
|
|
701
|
+
stderr._targets[me] = previous_err
|
|
702
|
+
if previous_out is None:
|
|
703
|
+
stdout._targets.pop(me, None)
|
|
704
|
+
else:
|
|
705
|
+
stdout._targets[me] = previous_out
|
|
706
|
+
if cell:
|
|
707
|
+
stdout.cell_target, stderr.cell_target = None, None
|
|
708
|
+
|
|
709
|
+
|
|
697
710
|
@dataclass
|
|
698
711
|
class _Entry:
|
|
699
712
|
name: str
|
|
@@ -905,8 +918,7 @@ class KernelToolRunner:
|
|
|
905
918
|
if invocation.cancelled.is_set():
|
|
906
919
|
raise Cancelled(CANCELLED_BEFORE_START)
|
|
907
920
|
CURRENT.set(invocation)
|
|
908
|
-
|
|
909
|
-
with stdout.capture(output, cell=False), stderr.capture(output, cell=False):
|
|
921
|
+
with capture_streams(self._streams, (output, output), cell=False):
|
|
910
922
|
value = self._call(invocation, entry.fn, args)
|
|
911
923
|
finally:
|
|
912
924
|
CURRENT.set(None)
|
|
@@ -2218,8 +2230,7 @@ def run_cell(
|
|
|
2218
2230
|
cell_scope = CURRENT_CELL_TOKEN.set(cell_token)
|
|
2219
2231
|
try:
|
|
2220
2232
|
KERNEL_TOOL_TOKEN.acquire()
|
|
2221
|
-
|
|
2222
|
-
with cell_stdout.capture(stdout, cell=True), cell_stderr.capture(stderr, cell=True):
|
|
2233
|
+
with capture_streams(KERNEL_TOOL_STREAMS, (stdout, stderr), cell=True):
|
|
2223
2234
|
apply_preludes(preludes)
|
|
2224
2235
|
leave_source_file = _enter_source_file(source_file)
|
|
2225
2236
|
body, expression = compile_cell(code, source_file or "<cell>")
|
|
@@ -2373,7 +2384,7 @@ def _start_parent_watch() -> None:
|
|
|
2373
2384
|
).start()
|
|
2374
2385
|
|
|
2375
2386
|
|
|
2376
|
-
def _read_control(commands: queue.
|
|
2387
|
+
def _read_control(commands: queue.SimpleQueue[tuple[str, Any]]) -> None:
|
|
2377
2388
|
# Kernel-tool frames are served here, so a callback can be admitted while the main thread is inside a cell.
|
|
2378
2389
|
for raw in sys.stdin:
|
|
2379
2390
|
try:
|
|
@@ -2392,7 +2403,7 @@ def main() -> None:
|
|
|
2392
2403
|
signal.signal(signal.SIGINT, signal.SIG_IGN)
|
|
2393
2404
|
_start_parent_watch()
|
|
2394
2405
|
sys.stdout, sys.stderr = KERNEL_TOOL_STREAMS
|
|
2395
|
-
commands: queue.
|
|
2406
|
+
commands: queue.SimpleQueue[tuple[str, Any]] = queue.SimpleQueue()
|
|
2396
2407
|
threading.Thread(target=_read_control, args=(commands,), name="senpi-control-reader", daemon=True).start()
|
|
2397
2408
|
host_closed = False
|
|
2398
2409
|
while True:
|
|
@@ -244,6 +244,10 @@ export class PythonKernelTransport {
|
|
|
244
244
|
this.#child.stderr.on("data", onStderr);
|
|
245
245
|
this.#child.on("error", onError);
|
|
246
246
|
this.#child.on("exit", onExit);
|
|
247
|
+
// senpi#3016: a frame written while the interpreter is dying fails on the stdin stream (EPIPE), not on the child.
|
|
248
|
+
// #onError already ignores it once the child has exited or the transport is inactive, and the listener is never
|
|
249
|
+
// detached, so a late write failure is never unhandled.
|
|
250
|
+
this.#child.stdin.on("error", onError);
|
|
247
251
|
const { sessionId, connection, memory, kernelGeneration } = this.#options;
|
|
248
252
|
this.#write({
|
|
249
253
|
type: "init",
|
package/src/kernels/rb/runner.rb
CHANGED
|
@@ -9,6 +9,7 @@ $stdout.flush
|
|
|
9
9
|
$__senpi_binding = TOPLEVEL_BINDING
|
|
10
10
|
$__senpi_frame_mutex = Mutex.new
|
|
11
11
|
$__senpi_current_cell = nil
|
|
12
|
+
$__senpi_memory_cell = nil
|
|
12
13
|
$__senpi_capture_cell = nil
|
|
13
14
|
$__senpi_connection = nil
|
|
14
15
|
$__senpi_frame_io = STDOUT.dup
|
|
@@ -123,8 +124,6 @@ rescue JSON::GeneratorError
|
|
|
123
124
|
value.inspect
|
|
124
125
|
end
|
|
125
126
|
|
|
126
|
-
$__senpi_memory = nil
|
|
127
|
-
|
|
128
127
|
SENPI_SIZER_SAMPLE = 1_000
|
|
129
128
|
SENPI_SIZER_NODE_BUDGET = 5_000
|
|
130
129
|
SENPI_SIZER_MAX_DEPTH = 64
|
|
@@ -239,19 +238,6 @@ rescue StandardError
|
|
|
239
238
|
[]
|
|
240
239
|
end
|
|
241
240
|
|
|
242
|
-
# The host reads the interpreter's footprint and decides on the notice; the runner only names its
|
|
243
|
-
# largest globals. Nothing here spawns a process, so the user's `$?` is left alone.
|
|
244
|
-
def __senpi_memory_report
|
|
245
|
-
return nil if $__senpi_memory.nil?
|
|
246
|
-
named = __senpi_largest_globals(5)
|
|
247
|
-
# The host replaces liveBytes with the interpreter footprint it reads itself; only globals is used.
|
|
248
|
-
report = { "liveBytes" => 0, "measure" => "footprint" }
|
|
249
|
-
report["globals"] = named unless named.empty?
|
|
250
|
-
report
|
|
251
|
-
rescue StandardError
|
|
252
|
-
nil
|
|
253
|
-
end
|
|
254
|
-
|
|
255
241
|
SENPI_NON_DISPLAY_NODES = %i[
|
|
256
242
|
LASGN IASGN GASGN CVASGN DASGN OP_ASGN OP_CDECL CDECL MASGN CASGN
|
|
257
243
|
DEFN DEFS CLASS MODULE SCLASS ALIAS UNDEF
|
|
@@ -305,8 +291,6 @@ def __senpi_run_cell(message)
|
|
|
305
291
|
"durationMs" => ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000).round,
|
|
306
292
|
}
|
|
307
293
|
frame["valueRepr"] = __senpi_value_repr(value) if !value.nil? && __senpi_should_display_result?(source)
|
|
308
|
-
memory = __senpi_memory_report
|
|
309
|
-
frame["memory"] = memory unless memory.nil?
|
|
310
294
|
__senpi_emit(frame)
|
|
311
295
|
rescue Exception => error
|
|
312
296
|
__senpi_emit({
|
|
@@ -319,6 +303,7 @@ def __senpi_run_cell(message)
|
|
|
319
303
|
ensure
|
|
320
304
|
$__senpi_capture_cell = nil
|
|
321
305
|
$__senpi_current_cell = nil
|
|
306
|
+
$__senpi_memory_cell = cell_id
|
|
322
307
|
end
|
|
323
308
|
end
|
|
324
309
|
|
|
@@ -332,10 +317,15 @@ $__senpi_protocol_stdin.each_line do |line|
|
|
|
332
317
|
when "init"
|
|
333
318
|
__senpi_emit({ "type" => "status", "event" => { "op" => "kernel-startup", "stage" => "host-init" } })
|
|
334
319
|
$__senpi_connection = message["connection"]
|
|
335
|
-
|
|
336
|
-
__senpi_emit({ "type" => "ready" })
|
|
320
|
+
__senpi_emit({ "type" => "ready", "memoryGlobals" => true })
|
|
337
321
|
when "run"
|
|
322
|
+
$__senpi_memory_cell = nil
|
|
338
323
|
__senpi_run_cell(message)
|
|
324
|
+
when "memory-globals"
|
|
325
|
+
if message["cellId"] == $__senpi_memory_cell && $__senpi_current_cell.nil?
|
|
326
|
+
__senpi_emit({ "type" => "memory-globals-result", "cellId" => message["cellId"], "globals" => __senpi_largest_globals(5) })
|
|
327
|
+
$__senpi_memory_cell = nil
|
|
328
|
+
end
|
|
339
329
|
when "close"
|
|
340
330
|
__senpi_emit({ "type" => "closed" })
|
|
341
331
|
exit 0
|
|
@@ -115,7 +115,7 @@ class Execution {
|
|
|
115
115
|
private finished = false;
|
|
116
116
|
// senpi-change begin: output streaming
|
|
117
117
|
private readonly stream: StreamOptions | undefined;
|
|
118
|
-
private readonly credits: Int32Array | undefined;
|
|
118
|
+
private readonly credits: Int32Array<SharedArrayBuffer> | undefined;
|
|
119
119
|
private frameChain: Promise<void> = Promise.resolve();
|
|
120
120
|
private frames = 0;
|
|
121
121
|
private inFlightBytes = 0;
|
|
@@ -188,7 +188,7 @@ class Execution {
|
|
|
188
188
|
// senpi-change begin: output streaming and builtin policy
|
|
189
189
|
...(this.credits === undefined || this.stream === undefined
|
|
190
190
|
? {}
|
|
191
|
-
: { stream: { credits: this.credits.buffer
|
|
191
|
+
: { stream: { credits: this.credits.buffer, frameBytes: this.stream.frameBytes } }),
|
|
192
192
|
storePolicy: options.storePolicy,
|
|
193
193
|
// senpi-change end
|
|
194
194
|
};
|