@botiverse/oar 0.80.0 → 0.80.1
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/dist/contracts/records.d.ts +2 -2
- package/dist/contracts/records.d.ts.map +1 -1
- package/dist/runtimes/claude/context-breakdown.js +2 -2
- package/dist/runtimes/claude/context-breakdown.js.map +1 -1
- package/dist/runtimes/qoder/daemon.js +1 -1
- package/dist/runtimes/qoder/daemon.js.map +1 -1
- package/dist/shared/acp/input.d.ts +8 -0
- package/dist/shared/acp/input.d.ts.map +1 -0
- package/dist/shared/acp/input.js +76 -0
- package/dist/shared/acp/input.js.map +1 -0
- package/dist/shared/acp/process.d.ts.map +1 -1
- package/dist/shared/acp/process.js +19 -2
- package/dist/shared/acp/process.js.map +1 -1
- package/dist/shared/executable/ownership.d.ts +4 -0
- package/dist/shared/executable/ownership.d.ts.map +1 -1
- package/dist/shared/executable/ownership.js +35 -13
- package/dist/shared/executable/ownership.js.map +1 -1
- package/dist/shared/executable/process-cleanup-diagnostics.d.ts +21 -0
- package/dist/shared/executable/process-cleanup-diagnostics.d.ts.map +1 -0
- package/dist/shared/executable/process-cleanup-diagnostics.js +22 -0
- package/dist/shared/executable/process-cleanup-diagnostics.js.map +1 -0
- package/dist/shared/executable/process-cleanup.d.ts +8 -24
- package/dist/shared/executable/process-cleanup.d.ts.map +1 -1
- package/dist/shared/executable/process-cleanup.js +56 -37
- package/dist/shared/executable/process-cleanup.js.map +1 -1
- package/dist/shared/executable/process-shutdown.d.ts +17 -0
- package/dist/shared/executable/process-shutdown.d.ts.map +1 -0
- package/dist/shared/executable/process-shutdown.js +115 -0
- package/dist/shared/executable/process-shutdown.js.map +1 -0
- package/dist/shared/executable/process-signals.d.ts +43 -0
- package/dist/shared/executable/process-signals.d.ts.map +1 -0
- package/dist/shared/executable/process-signals.js +74 -0
- package/dist/shared/executable/process-signals.js.map +1 -0
- package/dist/shared/executable/process-table-async.d.ts +10 -0
- package/dist/shared/executable/process-table-async.d.ts.map +1 -0
- package/dist/shared/executable/process-table-async.js +104 -0
- package/dist/shared/executable/process-table-async.js.map +1 -0
- package/dist/shared/executable/process-table-command.d.ts +4 -0
- package/dist/shared/executable/process-table-command.d.ts.map +1 -0
- package/dist/shared/executable/process-table-command.js +69 -0
- package/dist/shared/executable/process-table-command.js.map +1 -0
- package/dist/shared/executable/process-tree.d.ts +10 -2
- package/dist/shared/executable/process-tree.d.ts.map +1 -1
- package/dist/shared/executable/process-tree.js +41 -28
- package/dist/shared/executable/process-tree.js.map +1 -1
- package/dist/shared/executable/process.d.ts +6 -44
- package/dist/shared/executable/process.d.ts.map +1 -1
- package/dist/shared/executable/process.js +27 -139
- package/dist/shared/executable/process.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,60 +1,59 @@
|
|
|
1
|
-
import { descendantsOf, killEntries,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
return undefined;
|
|
5
|
-
}
|
|
6
|
-
const reasons = cleanup.observations.filter((entry) => !entry.ok).map((entry) => `${entry.phase} ${entry.source}: ${entry.reason} (${entry.elapsedMs.toFixed(0)} ms${entry.code === undefined ? "" : `, ${entry.code}`}${entry.exitCode === undefined ? "" : `, exit ${String(entry.exitCode)}`}${entry.signal === undefined ? "" : `, ${entry.signal}`})`);
|
|
7
|
-
return `Descendant cleanup could not be verified: ${reasons.join("; ")}`;
|
|
8
|
-
}
|
|
9
|
-
/** Keep a startup error's native class, code and cause when cleanup also failed. */
|
|
10
|
-
export function appendCleanupFailure(error, reason) {
|
|
11
|
-
if (reason === undefined) {
|
|
12
|
-
return error;
|
|
13
|
-
}
|
|
14
|
-
const failure = error instanceof Error ? error : new Error(String(error));
|
|
15
|
-
if (!failure.message.includes(reason)) {
|
|
16
|
-
failure.message += `; ${reason}`;
|
|
17
|
-
if (failure.stack !== undefined) {
|
|
18
|
-
failure.stack += `\nCleanup: ${reason}`;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return failure;
|
|
22
|
-
}
|
|
1
|
+
import { descendantsOf, killEntries, processTableFailure } from "./process-tree.js";
|
|
2
|
+
import { observeProcessTableAsync, processTableSource } from "./process-table-async.js";
|
|
3
|
+
export { appendCleanupFailure, cleanupFailureReason } from "./process-cleanup-diagnostics.js";
|
|
23
4
|
/** Capture before EOF, then revalidate identities before signalling descendants. */
|
|
24
|
-
export function processTreeCleanup(pid) {
|
|
5
|
+
export function processTreeCleanup(pid, alive = () => true) {
|
|
25
6
|
const observations = [];
|
|
26
7
|
let unverified = false;
|
|
8
|
+
let captured = false;
|
|
27
9
|
let descendants = [];
|
|
28
10
|
let root = undefined;
|
|
29
|
-
const
|
|
30
|
-
|
|
11
|
+
const diagnostics = () => ({ status: unverified ? "unverified" : "observed", observations: [...observations] });
|
|
12
|
+
const failed = (error, phase) => {
|
|
13
|
+
unverified = true;
|
|
14
|
+
observations.push({ ...processTableFailure(error, processTableSource(), 0), phase });
|
|
15
|
+
};
|
|
16
|
+
const accept = (result, phase) => {
|
|
31
17
|
if (!result.ok) {
|
|
32
18
|
observations.push({ ...result, phase });
|
|
33
19
|
return undefined;
|
|
34
20
|
}
|
|
35
|
-
if (phase === "before_eof" && !result.table.has(pid)) {
|
|
21
|
+
if (phase === "before_eof" && (!alive() || !result.table.has(pid))) {
|
|
36
22
|
observations.push({ ok: false, phase, source: result.source, elapsedMs: result.elapsedMs, reason: "missing_root" });
|
|
37
23
|
return undefined;
|
|
38
24
|
}
|
|
39
25
|
observations.push({ ok: true, phase, source: result.source, elapsedMs: result.elapsedMs, skipped: result.skipped });
|
|
40
26
|
return result.table;
|
|
41
27
|
};
|
|
28
|
+
const read = async (phase) => {
|
|
29
|
+
try {
|
|
30
|
+
return accept(await observeProcessTableAsync(), phase);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
failed(error, phase);
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
42
37
|
return {
|
|
43
|
-
capture() {
|
|
44
|
-
//
|
|
45
|
-
//
|
|
46
|
-
|
|
38
|
+
async capture() {
|
|
39
|
+
// At most two reads (one retry), both before EOF. A root that exited
|
|
40
|
+
// during a read cannot lend its possibly reused PID to a retry.
|
|
41
|
+
let table = await read("before_eof");
|
|
42
|
+
if (table === undefined && alive()) {
|
|
43
|
+
table = await read("before_eof");
|
|
44
|
+
}
|
|
47
45
|
if (table === undefined) {
|
|
48
46
|
unverified = true;
|
|
49
47
|
return;
|
|
50
48
|
}
|
|
49
|
+
captured = true;
|
|
51
50
|
root = table.get(pid);
|
|
52
51
|
descendants = descendantsOf(table, pid);
|
|
53
52
|
},
|
|
54
|
-
force(stopRoot) {
|
|
55
|
-
const table = read("before_force");
|
|
56
|
-
//
|
|
57
|
-
//
|
|
53
|
+
async force(stopRoot) {
|
|
54
|
+
const table = await read("before_force");
|
|
55
|
+
// Read failure still permits stopping the live owned root, but never
|
|
56
|
+
// signalling a remembered descendant without matching its identity.
|
|
58
57
|
stopRoot();
|
|
59
58
|
if (table === undefined) {
|
|
60
59
|
unverified = true;
|
|
@@ -65,11 +64,11 @@ export function processTreeCleanup(pid) {
|
|
|
65
64
|
killEntries([...descendants, ...now], table);
|
|
66
65
|
descendants = [];
|
|
67
66
|
},
|
|
68
|
-
finish() {
|
|
67
|
+
async finish() {
|
|
69
68
|
if (descendants.length === 0) {
|
|
70
69
|
return;
|
|
71
70
|
}
|
|
72
|
-
const table = read("after_exit");
|
|
71
|
+
const table = await read("after_exit");
|
|
73
72
|
if (table === undefined) {
|
|
74
73
|
unverified = true;
|
|
75
74
|
}
|
|
@@ -78,7 +77,27 @@ export function processTreeCleanup(pid) {
|
|
|
78
77
|
}
|
|
79
78
|
descendants = [];
|
|
80
79
|
},
|
|
81
|
-
|
|
80
|
+
finishSync(readNow) {
|
|
81
|
+
// The host may exit after child.close while an async observation is
|
|
82
|
+
// pending. Revalidate known identities synchronously, never its cache.
|
|
83
|
+
const phase = alive() ? "before_force" : "after_exit";
|
|
84
|
+
const table = accept(readNow(), phase);
|
|
85
|
+
if (table === undefined) {
|
|
86
|
+
unverified = true;
|
|
87
|
+
return diagnostics();
|
|
88
|
+
}
|
|
89
|
+
if (!captured && !alive()) {
|
|
90
|
+
unverified = true;
|
|
91
|
+
observations.push({ ok: false, phase: "before_eof", source: processTableSource(), elapsedMs: 0, reason: "missing_root" });
|
|
92
|
+
}
|
|
93
|
+
const current = table.get(pid);
|
|
94
|
+
const now = current !== undefined && (root === undefined ? alive() : current.start === root.start) ? descendantsOf(table, pid) : [];
|
|
95
|
+
killEntries([...descendants, ...now], table);
|
|
96
|
+
descendants = [];
|
|
97
|
+
return diagnostics();
|
|
98
|
+
},
|
|
99
|
+
failed,
|
|
100
|
+
diagnostics,
|
|
82
101
|
};
|
|
83
102
|
}
|
|
84
103
|
//# sourceMappingURL=process-cleanup.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"process-cleanup.js","sources":["../../../src/shared/executable/process-cleanup.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,mBAAmB,EAAsE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"process-cleanup.js","sources":["../../../src/shared/executable/process-cleanup.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,mBAAmB,EAAsE,MAAM,mBAAmB,CAAC;AACxJ,OAAO,EAAE,wBAAwB,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAGxF,OAAO,EAAE,oBAAoB,EAAE,oBAAoB,EAAkC,MAAM,kCAAkC,CAAC;AAE9H,oFAAoF;AACpF,MAAM,UAAU,kBAAkB,CAAC,GAAW,EAAE,KAAK,GAAkB,GAAG,EAAE,CAAC,IAAI;IAQ/E,MAAM,YAAY,GAAkB,EAAE,CAAC;IACvC,IAAI,UAAU,GAAG,KAAK,CAAC;IACvB,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,IAAI,WAAW,GAA4B,EAAE,CAA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AM,OAAO,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YAC/B,MAAM,GAAG,GAAG,OAAO,KAAK,SAAS,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;YACpI,WAAW,CAAC,CAAC,GAAG,WAAW,EAAE,GAAG,GAAG,CAAC,EAAE,KAAK,CAAC,CAAC;YAC7C,WAAW,GAAG,EAAE,CAAC;YACjB,OAAO,WAAW,EAAE,CAAC;QACvB,CAAC;QACD,MAAM;QACN,WAAW;KACZ,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ChildProcess } from "node:child_process";
|
|
2
|
+
import { type ProcessCleanupDiagnostics } from "./process-cleanup.js";
|
|
3
|
+
interface ShutdownOptions {
|
|
4
|
+
readonly killTree?: boolean;
|
|
5
|
+
readonly shutdown?: "stdin";
|
|
6
|
+
}
|
|
7
|
+
/** The root's exit and asynchronous cleanup form one owned, awaited lifetime. */
|
|
8
|
+
export declare function processShutdown(child: ChildProcess, options: ShutdownOptions): {
|
|
9
|
+
readonly finished: Promise<void>;
|
|
10
|
+
stop(): void;
|
|
11
|
+
nativeExited(): void;
|
|
12
|
+
stopping(): boolean;
|
|
13
|
+
diagnostics(): ProcessCleanupDiagnostics | undefined;
|
|
14
|
+
reason(): string | undefined;
|
|
15
|
+
};
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=process-shutdown.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-shutdown.d.ts","sources":["../../../src/shared/executable/process-shutdown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAA4C,KAAK,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AAIhH,UAAU,eAAe;IAAG,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;CAAE;AAEvF,iFAAiF;AACjF,wBAAgB,eAAe,CAAC,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,eAAe,GAAG;IAC9E,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACjC,IAAI,IAAI,IAAI,CAAC;IACb,YAAY,IAAI,IAAI,CAAC;IACrB,QAAQ,IAAI,OAAO,CAAC;IACpB,WAAW,IAAI,yBAAyB,GAAG,SAAS,CAAC;IACrD,MAAM,IAAI,MAAM,GAAG,SAAS,CAAC;CAC9B,CAqEA"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { processTreeCleanup, cleanupFailureReason } from "./process-cleanup.js";
|
|
2
|
+
import { trackPendingCleanup } from "./ownership.js";
|
|
3
|
+
import { killGraceMs, killProcessTree, signalProcessGroup } from "./process-signals.js";
|
|
4
|
+
/** The root's exit and asynchronous cleanup form one owned, awaited lifetime. */
|
|
5
|
+
export function processShutdown(child, options) {
|
|
6
|
+
let stopping = false;
|
|
7
|
+
let ended = false;
|
|
8
|
+
let phase = Promise.resolve();
|
|
9
|
+
let escalation = undefined;
|
|
10
|
+
let cleanup = undefined;
|
|
11
|
+
let releaseCleanup = undefined;
|
|
12
|
+
const nativeExit = Promise.withResolvers();
|
|
13
|
+
const alive = () => !ended && child.exitCode === null && child.signalCode === null;
|
|
14
|
+
const stopRoot = () => {
|
|
15
|
+
if (!alive()) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (process.platform === "win32" && options.killTree === true) {
|
|
19
|
+
killProcessTree(child);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
signalProcessGroup(child, "SIGKILL");
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const force = async () => {
|
|
26
|
+
try {
|
|
27
|
+
if (cleanup === undefined) {
|
|
28
|
+
stopRoot();
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
await cleanup.force(stopRoot);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
cleanup?.failed(error, "before_force");
|
|
36
|
+
if (alive()) {
|
|
37
|
+
try {
|
|
38
|
+
child.kill("SIGKILL");
|
|
39
|
+
}
|
|
40
|
+
catch { /* Native exit stays authoritative. */ }
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const close = () => {
|
|
45
|
+
if (!alive()) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
child.stdin?.end();
|
|
49
|
+
// Pi reenters shutdown on EOF plus SIGTERM and skips its native hooks.
|
|
50
|
+
if (options.shutdown !== "stdin") {
|
|
51
|
+
signalProcessGroup(child, "SIGTERM");
|
|
52
|
+
}
|
|
53
|
+
escalation = setTimeout(() => { phase = force(); }, killGraceMs());
|
|
54
|
+
};
|
|
55
|
+
const captureAndClose = async () => {
|
|
56
|
+
try {
|
|
57
|
+
await cleanup?.capture();
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
cleanup?.failed(error, "before_eof");
|
|
61
|
+
}
|
|
62
|
+
try {
|
|
63
|
+
close();
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
cleanup?.failed(error, "before_force");
|
|
67
|
+
await force();
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const finish = async () => {
|
|
71
|
+
await nativeExit.promise;
|
|
72
|
+
// Native exit can arrive during capture or force. Finish after that phase,
|
|
73
|
+
// not concurrently with a mutation of the remembered identity set.
|
|
74
|
+
await phase;
|
|
75
|
+
try {
|
|
76
|
+
await cleanup?.finish();
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
cleanup?.failed(error, "after_exit");
|
|
80
|
+
}
|
|
81
|
+
releaseCleanup?.();
|
|
82
|
+
};
|
|
83
|
+
const finished = finish();
|
|
84
|
+
return {
|
|
85
|
+
finished,
|
|
86
|
+
stopping: () => stopping,
|
|
87
|
+
diagnostics: () => cleanup?.diagnostics(),
|
|
88
|
+
reason: () => cleanupFailureReason(cleanup?.diagnostics()),
|
|
89
|
+
nativeExited() { ended = true; clearTimeout(escalation); nativeExit.resolve(); },
|
|
90
|
+
stop() {
|
|
91
|
+
if (!alive() || stopping) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
stopping = true;
|
|
95
|
+
if (options.shutdown !== "stdin" && options.killTree === true && process.platform === "win32") {
|
|
96
|
+
child.stdin?.end();
|
|
97
|
+
killProcessTree(child);
|
|
98
|
+
escalation = setTimeout(() => { if (alive()) {
|
|
99
|
+
child.kill("SIGKILL");
|
|
100
|
+
} }, killGraceMs());
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (options.killTree === true && child.pid !== undefined) {
|
|
104
|
+
cleanup = processTreeCleanup(child.pid, alive);
|
|
105
|
+
const ownedCleanup = cleanup;
|
|
106
|
+
releaseCleanup = trackPendingCleanup((read) => ownedCleanup.finishSync(read));
|
|
107
|
+
phase = captureAndClose();
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
close();
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=process-shutdown.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-shutdown.js","sources":["../../../src/shared/executable/process-shutdown.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAkC,MAAM,sBAAsB,CAAC;AAChH,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,sBA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,OAAO,CAAC;gBAC7B,cAAc,GAAG,mBAAmB,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,YAAY,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC;gBAC9E,KAAK,GAAG,eAAe,EAAE,CAAC;YAC5B,CAAC;iBAAM,CAAC;gBAAC,KAAK,EAAE,CAAC;YAAC,CAAC;QACrB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type ChildProcess } from "node:child_process";
|
|
2
|
+
/**
|
|
3
|
+
* How long a process asked to stop (SIGTERM) gets before it is killed outright
|
|
4
|
+
* (SIGKILL): the bound that keeps `kill()` followed by `await exited` from
|
|
5
|
+
* hanging on a process that ignores the request. Generous on purpose: claude
|
|
6
|
+
* answers SIGTERM with a shutdown of its own that runs its SessionEnd hooks
|
|
7
|
+
* and force-exits after max(5 s, hook budget + 5 s), 15 s while writes are
|
|
8
|
+
* still pending (2.1.283; the hook budget defaults to 1.5 s), and cutting that
|
|
9
|
+
* short kills the hooks with it. Observed exits take 0.6-2.3 s.
|
|
10
|
+
*/
|
|
11
|
+
export declare const KILL_GRACE_MS = 10000;
|
|
12
|
+
/**
|
|
13
|
+
* The grace period in force: `OAR_KILL_GRACE_MS` when it holds a nonnegative
|
|
14
|
+
* number of milliseconds (a host whose runtimes need longer, e.g. claude with
|
|
15
|
+
* slow SessionEnd hooks, or that wants a faster teardown), else `KILL_GRACE_MS`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function killGraceMs(env?: NodeJS.ProcessEnv): number;
|
|
18
|
+
/**
|
|
19
|
+
* POSIX children are spawned detached, which makes each the leader of its own
|
|
20
|
+
* process group, so one signal to the negative pid reaches every process it
|
|
21
|
+
* started (shells, dev servers, MCP servers), not only the child itself.
|
|
22
|
+
* Detaching changes neither stdio nor exit tracking: the pipes stay connected
|
|
23
|
+
* and the child is never unref'd, so the host still observes its exit. It does
|
|
24
|
+
* take the child out of the terminal's job control: Ctrl-C (SIGINT) and SIGHUP
|
|
25
|
+
* reach the host only, so a host that wants them to stop its runtimes
|
|
26
|
+
* disposes its sessions on them. Windows has no process groups, and a
|
|
27
|
+
* detached child there gets its own console, so it stays attached.
|
|
28
|
+
*/
|
|
29
|
+
export declare const OWN_PROCESS_GROUP: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Send `signal` to a child spawned with `detached: OWN_PROCESS_GROUP` and to
|
|
32
|
+
* everything in its process group. Falls back to the child alone where there
|
|
33
|
+
* are no groups (Windows) or the group is gone (the child left it; ESRCH).
|
|
34
|
+
* A reaped child's pid can be reused, so call this only while the child, or a
|
|
35
|
+
* process it started, can still be alive.
|
|
36
|
+
*/
|
|
37
|
+
export declare function signalProcessGroup(child: ChildProcess, signal: NodeJS.Signals): void;
|
|
38
|
+
/**
|
|
39
|
+
* Windows taskkill /T /F ends the child and its descendants, falling back to
|
|
40
|
+
* the child alone when the walk fails. An exited child's pid may already be reused.
|
|
41
|
+
*/
|
|
42
|
+
export declare function killProcessTree(child: ChildProcess): void;
|
|
43
|
+
//# sourceMappingURL=process-signals.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-signals.d.ts","sources":["../../../src/shared/executable/process-signals.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAGjE;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa,QAAS,CAAC;AACpC;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,GAAG,GAAE,MAAM,CAAC,UAAwB,GAAG,MAAM,CAIxE;AACD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,iBAAiB,SAA+B,CAAC;AAC9D;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,CAAC,OAAO,GAAG,IAAI,CAWpF;AACD;;;GAGG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,YAAY,GAAG,IAAI,CAYzD"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { trackOwnedProcess } from "./ownership.js";
|
|
3
|
+
/**
|
|
4
|
+
* How long a process asked to stop (SIGTERM) gets before it is killed outright
|
|
5
|
+
* (SIGKILL): the bound that keeps `kill()` followed by `await exited` from
|
|
6
|
+
* hanging on a process that ignores the request. Generous on purpose: claude
|
|
7
|
+
* answers SIGTERM with a shutdown of its own that runs its SessionEnd hooks
|
|
8
|
+
* and force-exits after max(5 s, hook budget + 5 s), 15 s while writes are
|
|
9
|
+
* still pending (2.1.283; the hook budget defaults to 1.5 s), and cutting that
|
|
10
|
+
* short kills the hooks with it. Observed exits take 0.6-2.3 s.
|
|
11
|
+
*/
|
|
12
|
+
export const KILL_GRACE_MS = 10_000;
|
|
13
|
+
/**
|
|
14
|
+
* The grace period in force: `OAR_KILL_GRACE_MS` when it holds a nonnegative
|
|
15
|
+
* number of milliseconds (a host whose runtimes need longer, e.g. claude with
|
|
16
|
+
* slow SessionEnd hooks, or that wants a faster teardown), else `KILL_GRACE_MS`.
|
|
17
|
+
*/
|
|
18
|
+
export function killGraceMs(env = process.env) {
|
|
19
|
+
const configured = env.OAR_KILL_GRACE_MS?.trim() ?? "";
|
|
20
|
+
const value = Number(configured);
|
|
21
|
+
return configured.length > 0 && Number.isFinite(value) && value >= 0 ? value : KILL_GRACE_MS;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* POSIX children are spawned detached, which makes each the leader of its own
|
|
25
|
+
* process group, so one signal to the negative pid reaches every process it
|
|
26
|
+
* started (shells, dev servers, MCP servers), not only the child itself.
|
|
27
|
+
* Detaching changes neither stdio nor exit tracking: the pipes stay connected
|
|
28
|
+
* and the child is never unref'd, so the host still observes its exit. It does
|
|
29
|
+
* take the child out of the terminal's job control: Ctrl-C (SIGINT) and SIGHUP
|
|
30
|
+
* reach the host only, so a host that wants them to stop its runtimes
|
|
31
|
+
* disposes its sessions on them. Windows has no process groups, and a
|
|
32
|
+
* detached child there gets its own console, so it stays attached.
|
|
33
|
+
*/
|
|
34
|
+
export const OWN_PROCESS_GROUP = process.platform !== "win32";
|
|
35
|
+
/**
|
|
36
|
+
* Send `signal` to a child spawned with `detached: OWN_PROCESS_GROUP` and to
|
|
37
|
+
* everything in its process group. Falls back to the child alone where there
|
|
38
|
+
* are no groups (Windows) or the group is gone (the child left it; ESRCH).
|
|
39
|
+
* A reaped child's pid can be reused, so call this only while the child, or a
|
|
40
|
+
* process it started, can still be alive.
|
|
41
|
+
*/
|
|
42
|
+
export function signalProcessGroup(child, signal) {
|
|
43
|
+
if (OWN_PROCESS_GROUP && child.pid !== undefined) {
|
|
44
|
+
try {
|
|
45
|
+
process.kill(-child.pid, signal);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// Nobody is left in the group; the child itself may still be alive in
|
|
50
|
+
// a group of its own making, so it is signalled directly below.
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
child.kill(signal);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Windows taskkill /T /F ends the child and its descendants, falling back to
|
|
57
|
+
* the child alone when the walk fails. An exited child's pid may already be reused.
|
|
58
|
+
*/
|
|
59
|
+
export function killProcessTree(child) {
|
|
60
|
+
if (child.exitCode !== null || child.signalCode !== null) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (child.pid === undefined) {
|
|
64
|
+
child.kill();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const killer = execFile("taskkill", ["/pid", String(child.pid), "/T", "/F"], { windowsHide: true }, (error) => {
|
|
68
|
+
if (error !== null) {
|
|
69
|
+
child.kill();
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
trackOwnedProcess(killer);
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=process-signals.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"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I,EAAE,CAAC;QAAC,OAAO;IAAC,CAAC;IACrE,IAAI,KAAK,CAAC,GAAG,KAAK,SAAS,EAAE,CAAC;QAC5B,KAAK,CAAC,IAAI,EAAE,CAAC;QACb,OAAO;IACT,CAAC;IACD,MAAM,MAAM,GAAG,QAAQ,CAAC,UAAU,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,WAAW,EAAE,IAAI,EAAE,EAAE,CAAC,KAAK,EAAE,EAAE;QAC5G,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACnB,KAAK,CAAC,IAAI,EAAE,CAAC;QACf,CAAC;IACH,CAAC,CAAC,CAAC;IACH,iBAAiB,CAAC,MAAM,CAAC,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type ProcessTableObservation, type ProcessTableSource } from "./process-tree.js";
|
|
2
|
+
export declare const processTableSource: () => ProcessTableSource;
|
|
3
|
+
/** One actual read, distinct from the callers waiting to join a new batch. */
|
|
4
|
+
export declare function observeProcessTableOnce(source: ProcessTableSource): Promise<ProcessTableObservation>;
|
|
5
|
+
type Reader = (source: ProcessTableSource) => Promise<ProcessTableObservation>;
|
|
6
|
+
/** Only requests made before a read starts may use it. No result is cached. */
|
|
7
|
+
export declare function batchProcessTableReads(read: Reader): Reader;
|
|
8
|
+
export declare function observeProcessTableAsync(source?: ProcessTableSource): Promise<ProcessTableObservation>;
|
|
9
|
+
export {};
|
|
10
|
+
//# sourceMappingURL=process-table-async.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-table-async.d.ts","sources":["../../../src/shared/executable/process-table-async.ts"],"names":[],"mappings":"AAEA,OAAO,EAA2H,KAAK,uBAAuB,EAAyB,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAI1O,eAAO,MAAM,kBAAkB,QAAO,kBAAiH,CAAC;AAuBxJ,8EAA8E;AAC9E,wBAAsB,uBAAuB,CAAC,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAc1G;AAED,KAAK,MAAM,GAAG,CAAC,MAAM,EAAE,kBAAkB,KAAK,OAAO,CAAC,uBAAuB,CAAC,CAAC;AAM/E,+EAA+E;AAC/E,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAuB3D;AAGD,wBAAsB,wBAAwB,CAAC,MAAM,GAAE,kBAAyC,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAGlI"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
2
|
+
import { asRecord } from "../json.js";
|
|
3
|
+
import { InvalidProcessTableError, parseProcessPs, parseProcessStat, parseProcessWindows, processTableFailure } from "./process-tree.js";
|
|
4
|
+
import { processTableCommand } from "./process-table-command.js";
|
|
5
|
+
const BATCH = 64;
|
|
6
|
+
export const processTableSource = () => process.platform === "linux" ? "procfs" : (process.platform === "win32" ? "windows" : "ps");
|
|
7
|
+
async function readProcfs() {
|
|
8
|
+
const names = await readdir("/proc");
|
|
9
|
+
const listed = names.filter((name) => /^\d+$/u.test(name));
|
|
10
|
+
const table = new Map();
|
|
11
|
+
let skipped = 0;
|
|
12
|
+
const read = async (name) => {
|
|
13
|
+
try {
|
|
14
|
+
return parseProcessStat(Number(name), await readFile(`/proc/${name}/stat`, "utf8"));
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
const code = asRecord(error)?.code;
|
|
18
|
+
if (code === "ENOENT" || code === "ESRCH") {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
if (code === "EACCES" || code === "EPERM") {
|
|
22
|
+
skipped += 1;
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
for (let offset = 0; offset < listed.length; offset += BATCH) {
|
|
29
|
+
const entries = await Promise.all(listed.slice(offset, offset + BATCH).map(async (name) => { const entry = await read(name); return entry; }));
|
|
30
|
+
for (const entry of entries) {
|
|
31
|
+
if (entry !== null) {
|
|
32
|
+
table.set(entry.pid, entry);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return { table, skipped };
|
|
37
|
+
}
|
|
38
|
+
/** One actual read, distinct from the callers waiting to join a new batch. */
|
|
39
|
+
export async function observeProcessTableOnce(source) {
|
|
40
|
+
const started = performance.now();
|
|
41
|
+
try {
|
|
42
|
+
let result = { table: new Map(), skipped: 0 };
|
|
43
|
+
if (source === "procfs") {
|
|
44
|
+
result = await readProcfs();
|
|
45
|
+
}
|
|
46
|
+
else if (source === "ps") {
|
|
47
|
+
result = parseProcessPs(await processTableCommand("ps", ["-A", "-o", "pid=,ppid=,pgid=,lstart="], { ...process.env, LC_ALL: "C" }));
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
result = parseProcessWindows(await processTableCommand("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command",
|
|
51
|
+
"$ErrorActionPreference='Stop'; Get-CimInstance Win32_Process | Select-Object ProcessId,ParentProcessId,@{Name='Started';Expression={if ($null -ne $_.CreationDate) { $_.CreationDate.ToUniversalTime().ToString('o') }}} | ConvertTo-Json -Compress"]));
|
|
52
|
+
}
|
|
53
|
+
if (!result.table.has(process.pid)) {
|
|
54
|
+
throw new InvalidProcessTableError("missing_host");
|
|
55
|
+
}
|
|
56
|
+
return { ok: true, source, elapsedMs: performance.now() - started, ...result };
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
return processTableFailure(error, source, performance.now() - started);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/** Only requests made before a read starts may use it. No result is cached. */
|
|
63
|
+
export function batchProcessTableReads(read) {
|
|
64
|
+
const batches = new Map();
|
|
65
|
+
return async (source) => {
|
|
66
|
+
const previous = batches.get(source);
|
|
67
|
+
if (previous !== undefined && !previous.started) {
|
|
68
|
+
return previous.result;
|
|
69
|
+
}
|
|
70
|
+
const observe = async () => {
|
|
71
|
+
// A pending generation waits for its predecessor, then admits every
|
|
72
|
+
// caller that arrived before this query actually begins.
|
|
73
|
+
if (previous !== undefined) {
|
|
74
|
+
await previous.result;
|
|
75
|
+
}
|
|
76
|
+
await Promise.resolve();
|
|
77
|
+
batch.started = true;
|
|
78
|
+
const started = performance.now();
|
|
79
|
+
try {
|
|
80
|
+
return await read(source);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
return processTableFailure(error, source, performance.now() - started);
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
if (batches.get(source) === batch) {
|
|
87
|
+
batches.delete(source);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
// The observed promise is the same one callers await, with every read
|
|
92
|
+
// failure converted into metadata. There is no detached worker promise.
|
|
93
|
+
const batch = { started: false, result: observe() };
|
|
94
|
+
batches.set(source, batch);
|
|
95
|
+
const result = await batch.result;
|
|
96
|
+
return result;
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
const observeBatched = batchProcessTableReads(observeProcessTableOnce);
|
|
100
|
+
export async function observeProcessTableAsync(source = processTableSource()) {
|
|
101
|
+
const result = await observeBatched(source);
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=process-table-async.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-table-async.js","sources":["../../../src/shared/executable/process-table-async.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,wBAAwB,EAAE,cAAc,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,mBAAmB,EAAmG,MAAM,mBAAmB,CAAC;AAC1O,O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,CAAC;YAClC,IAAI,CAAC;gBAAC,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,CAAC;YAAC,CAAC;YAClC,OAAO,KAAK,EAAE,CAAC;gBAAC,OAAO,mBAAmB,CAAC,KAAK,EAAE,MAAM,EAAE,WAAW,CAAC,GAAG,EAAE,GAAG,OAAO,CAAC,CAAC;YAAC,CAAC;oBACjF,CAAC;gBAAC,IAAI,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,KAAK,EAAE,CAAC;oBAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;gBAAC,CAAC;YAAC,CAAC;QAC5E,CAAC,CAAC;QACF,sEAAsE;QACtE,wEAAwE;QACxE,MAAM,KAAK,GAAiB,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,CAAC;QAClE,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QAC3B,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,MAAM,CAAC;QAClC,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,cAAc,GAAG,sBAAsB,CAAC,uBAAuB,CAAC,CAAC;AACvE,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAAC,MAAM,GAAuB,kBAAkB,EAAE;IAC9F,MAAM,MAAM,GAAG,MAAM,cAAc,CAAC,MAAM,CAAC,CAAC;IAC5C,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const PROCESS_TABLE_TIMEOUT_MS = 5000;
|
|
2
|
+
/** A query deadline includes pipe drain; a dead root cannot leave this read waiting on close. */
|
|
3
|
+
export declare function processTableCommand(command: string, args: readonly string[], env?: NodeJS.ProcessEnv): Promise<string>;
|
|
4
|
+
//# sourceMappingURL=process-table-command.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-table-command.d.ts","sources":["../../../src/shared/executable/process-table-command.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,wBAAwB,OAAO,CAAC;AAG7C,iGAAiG;AACjG,wBAAsB,mBAAmB,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,MAAM,EAAE,EAAE,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CA8C5H"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { trackOwnedProcess } from "./ownership.js";
|
|
3
|
+
export const PROCESS_TABLE_TIMEOUT_MS = 5000;
|
|
4
|
+
const MAX_OUTPUT = 16 * 1024 * 1024;
|
|
5
|
+
/** A query deadline includes pipe drain; a dead root cannot leave this read waiting on close. */
|
|
6
|
+
export async function processTableCommand(command, args, env) {
|
|
7
|
+
const output = await new Promise((resolve, reject) => {
|
|
8
|
+
// Node's piped-stdio overload excludes the missing pipes of a failed spawn.
|
|
9
|
+
const child = spawn(command, [...args], { env, windowsHide: true, stdio: ["ignore", "pipe", "ignore"] });
|
|
10
|
+
trackOwnedProcess(child);
|
|
11
|
+
const chunks = [];
|
|
12
|
+
let size = 0;
|
|
13
|
+
let settled = false;
|
|
14
|
+
const deadline = {};
|
|
15
|
+
const finish = (error) => {
|
|
16
|
+
if (settled) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
settled = true;
|
|
20
|
+
clearTimeout(deadline.timer);
|
|
21
|
+
if (error !== undefined) {
|
|
22
|
+
// Never wait for close after the deadline. Ownership retains the query
|
|
23
|
+
// until close, including when the OS cannot terminate it immediately.
|
|
24
|
+
if (child.exitCode === null && child.signalCode === null) {
|
|
25
|
+
try {
|
|
26
|
+
child.kill("SIGKILL");
|
|
27
|
+
}
|
|
28
|
+
catch { /* Keep the original read failure. */ }
|
|
29
|
+
}
|
|
30
|
+
child.stdout?.destroy();
|
|
31
|
+
reject(error instanceof Error ? error : new Error("Process table query failed"));
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
resolve(Buffer.concat(chunks).toString("utf8"));
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
// Failed spawn (including EMFILE/ENFILE) may return no stdout. Register
|
|
38
|
+
// the native error before touching pipes or arming a deadline.
|
|
39
|
+
child.once("error", finish);
|
|
40
|
+
child.once("close", (code, signal) => {
|
|
41
|
+
if (code === 0 && signal === null) {
|
|
42
|
+
finish();
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
finish(Object.assign(new Error("Process table command failed"), { status: code, signal }));
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
const stdout = child.stdout;
|
|
49
|
+
if (!stdout) {
|
|
50
|
+
// The native spawn error is queued first; retain its metadata when it
|
|
51
|
+
// arrives, while also settling a malformed spawn with no error event.
|
|
52
|
+
setImmediate(() => { finish(new Error("Process table query has no stdout")); });
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
stdout.on("data", (chunk) => {
|
|
56
|
+
size += chunk.length;
|
|
57
|
+
if (size > MAX_OUTPUT) {
|
|
58
|
+
finish(Object.assign(new Error("Process table output exceeded limit"), { code: "ENOBUFS" }));
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
chunks.push(chunk);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
stdout.once("error", finish);
|
|
65
|
+
deadline.timer = setTimeout(() => { finish(Object.assign(new Error("Process table query timed out"), { code: "ETIMEDOUT" })); }, PROCESS_TABLE_TIMEOUT_MS);
|
|
66
|
+
});
|
|
67
|
+
return output;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=process-table-command.js.map
|