@trama-dev/runtime 0.2.2 → 0.3.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/agent-backend.d.ts +24 -0
- package/dist/agent-backend.js +29 -0
- package/dist/agent.d.ts +12 -2
- package/dist/agent.js +23 -6
- package/dist/bridge-client.d.ts +22 -0
- package/dist/bridge-client.js +71 -0
- package/dist/child-process.d.ts +39 -6
- package/dist/child-process.js +132 -25
- package/dist/client.d.ts +1 -0
- package/dist/client.js +5 -48
- package/dist/context.d.ts +2 -0
- package/dist/context.js +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.js +61 -15
- package/dist/ipc-server.d.ts +6 -2
- package/dist/ipc-server.js +65 -8
- package/dist/pi-adapter.d.ts +7 -14
- package/dist/pi-adapter.js +20 -54
- package/dist/runner-types.d.ts +18 -7
- package/dist/runner.d.ts +22 -5
- package/dist/runner.js +251 -71
- package/dist/tools.d.ts +2 -2
- package/dist/tools.js +15 -12
- package/dist/watchdog.d.ts +19 -0
- package/dist/watchdog.js +167 -0
- package/package.json +1 -1
- package/dist/lifetime.d.ts +0 -8
- package/dist/lifetime.js +0 -70
package/dist/watchdog.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// What must not outlive this process: the process groups it started, and the
|
|
2
|
+
// temp directories it made.
|
|
3
|
+
//
|
|
4
|
+
// A program, and every shell command it runs, leads its own process group — so
|
|
5
|
+
// that a timeout can take down everything it started. The cost is that none of
|
|
6
|
+
// them die with the runner. Something has to stop them when the runner goes,
|
|
7
|
+
// and it cannot be the runner: a process that is killed outright, or runs out
|
|
8
|
+
// of memory, or crashes, runs no code on its way out.
|
|
9
|
+
//
|
|
10
|
+
// So it is a process of its own, the watchdog. The runner keeps a pipe to it
|
|
11
|
+
// open, and tells it what it has started and what has ended. When the pipe
|
|
12
|
+
// closes — the runner is gone, however it went — the watchdog stops whatever
|
|
13
|
+
// is still on its list and deletes what is left of the directories. One runner
|
|
14
|
+
// carries every run of a session, so one list covers the whole tree.
|
|
15
|
+
//
|
|
16
|
+
// Nothing here listens for a signal or for the process exiting: the process
|
|
17
|
+
// that hosts a runner keeps its own way of handling both.
|
|
18
|
+
import { spawn } from "child_process";
|
|
19
|
+
import { rmSync } from "fs";
|
|
20
|
+
import { STOP_TIMINGS, onGroupAskedToStop } from "./child-process.js";
|
|
21
|
+
// Run by the watchdog process. It applies the same policy as stopGroups (see
|
|
22
|
+
// child-process.ts): ask, wait out the grace period, kill what is left. A
|
|
23
|
+
// group the runner had already asked is not asked a second time.
|
|
24
|
+
const WATCHDOG = `
|
|
25
|
+
const { rmSync } = require("fs");
|
|
26
|
+
const { grace, settle, poll } = JSON.parse(process.argv[1]);
|
|
27
|
+
const lists = { g: new Set(), d: new Set(), asked: new Set() };
|
|
28
|
+
const alive = (pgid) => { try { process.kill(-pgid, 0); return true; } catch { return false; } };
|
|
29
|
+
const signal = (pgid, name) => { try { process.kill(-pgid, name); } catch {} };
|
|
30
|
+
|
|
31
|
+
let unread = "";
|
|
32
|
+
process.stdin.setEncoding("utf8");
|
|
33
|
+
process.stdin.on("data", (chunk) => {
|
|
34
|
+
unread += chunk;
|
|
35
|
+
for (let end; (end = unread.indexOf("\\n")) !== -1; unread = unread.slice(end + 1)) {
|
|
36
|
+
const [op, list, item] = JSON.parse(unread.slice(0, end));
|
|
37
|
+
lists[list][op === "+" ? "add" : "delete"](item);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// The pipe has closed: the runner is gone.
|
|
42
|
+
let leaving = false;
|
|
43
|
+
const takeOver = () => {
|
|
44
|
+
if (leaving) return;
|
|
45
|
+
leaving = true;
|
|
46
|
+
const finish = () => {
|
|
47
|
+
for (const dir of lists.d) { try { rmSync(dir, { recursive: true, force: true }); } catch {} }
|
|
48
|
+
process.exit(0);
|
|
49
|
+
};
|
|
50
|
+
let left = [...lists.g].filter(alive);
|
|
51
|
+
for (const pgid of left) { if (!lists.asked.has(pgid)) signal(pgid, "SIGTERM"); }
|
|
52
|
+
let deadline = performance.now() + grace;
|
|
53
|
+
let killed = false;
|
|
54
|
+
const check = () => {
|
|
55
|
+
// A group seen gone is never signalled again: its id may belong to another by now.
|
|
56
|
+
left = left.filter(alive);
|
|
57
|
+
if (left.length === 0) return finish();
|
|
58
|
+
if (performance.now() < deadline) return;
|
|
59
|
+
if (killed) return finish();
|
|
60
|
+
for (const pgid of left) signal(pgid, "SIGKILL");
|
|
61
|
+
killed = true;
|
|
62
|
+
deadline = performance.now() + settle;
|
|
63
|
+
};
|
|
64
|
+
check();
|
|
65
|
+
setInterval(check, poll);
|
|
66
|
+
};
|
|
67
|
+
process.stdin.on("end", takeOver);
|
|
68
|
+
process.stdin.on("close", takeOver);
|
|
69
|
+
process.stdin.on("error", takeOver);
|
|
70
|
+
`;
|
|
71
|
+
/** What this process has told the watchdog about and not yet taken back. */
|
|
72
|
+
const pending = { g: new Set(), d: new Set(), asked: new Set() };
|
|
73
|
+
let watchdog = null;
|
|
74
|
+
const tell = (to, op, list, item) => {
|
|
75
|
+
to.stdin?.write(JSON.stringify([op, list, item]) + "\n");
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* Start the watchdog if there is none, and return it — or null if it cannot be
|
|
79
|
+
* started. A watchdog that has itself died is replaced, and told again what is
|
|
80
|
+
* still pending.
|
|
81
|
+
*/
|
|
82
|
+
function startWatchdog() {
|
|
83
|
+
if (watchdog)
|
|
84
|
+
return watchdog;
|
|
85
|
+
let child;
|
|
86
|
+
try {
|
|
87
|
+
child = spawn(process.execPath, ["-e", WATCHDOG, JSON.stringify(STOP_TIMINGS)], {
|
|
88
|
+
detached: true, // a signal sent to this process's group must not reach it
|
|
89
|
+
stdio: ["pipe", "ignore", "ignore"],
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
const lost = () => { if (watchdog === child)
|
|
96
|
+
watchdog = null; };
|
|
97
|
+
child.on("error", lost);
|
|
98
|
+
child.on("exit", lost);
|
|
99
|
+
child.stdin?.on("error", lost);
|
|
100
|
+
if (child.pid === undefined)
|
|
101
|
+
return null; // a failed spawn has no pid from the start
|
|
102
|
+
// Neither the watchdog nor the pipe to it keeps this process alive.
|
|
103
|
+
child.unref();
|
|
104
|
+
child.stdin?.unref?.();
|
|
105
|
+
watchdog = child;
|
|
106
|
+
for (const list of ["g", "d", "asked"]) {
|
|
107
|
+
for (const item of pending[list])
|
|
108
|
+
tell(child, "+", list, item);
|
|
109
|
+
}
|
|
110
|
+
return child;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Make sure there is a watchdog before anything is started that would need
|
|
114
|
+
* one. Throws if one cannot be started: running without it would mean leaving
|
|
115
|
+
* processes behind with nobody to stop them.
|
|
116
|
+
*/
|
|
117
|
+
export function requireWatchdog() {
|
|
118
|
+
if (!startWatchdog()) {
|
|
119
|
+
throw new Error("Could not start trama's watchdog process, which stops what a run started if the runner dies.");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/** Keep `item` on the watchdog's list until the returned function is called. */
|
|
123
|
+
function watch(list, item) {
|
|
124
|
+
pending[list].add(item);
|
|
125
|
+
const to = startWatchdog();
|
|
126
|
+
if (to)
|
|
127
|
+
tell(to, "+", list, item);
|
|
128
|
+
return () => {
|
|
129
|
+
if (!pending[list].delete(item))
|
|
130
|
+
return;
|
|
131
|
+
if (watchdog)
|
|
132
|
+
tell(watchdog, "-", list, item);
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Register a process group this process started: if this process ends before
|
|
137
|
+
* the returned function is called, the group is stopped.
|
|
138
|
+
*/
|
|
139
|
+
export function stopWithThisProcess(pgid) {
|
|
140
|
+
const forget = watch("g", pgid);
|
|
141
|
+
return () => {
|
|
142
|
+
forget();
|
|
143
|
+
asked.get(pgid)?.();
|
|
144
|
+
asked.delete(pgid);
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/** Groups on the watchdog's list that this process has asked to stop, and how to take each off again. */
|
|
148
|
+
const asked = new Map();
|
|
149
|
+
onGroupAskedToStop((pgid) => {
|
|
150
|
+
if (pending.g.has(pgid) && !asked.has(pgid))
|
|
151
|
+
asked.set(pgid, watch("asked", pgid));
|
|
152
|
+
});
|
|
153
|
+
/**
|
|
154
|
+
* Register a temp directory this process created: if this process ends before
|
|
155
|
+
* the returned function is called, the directory is removed — once the groups
|
|
156
|
+
* that may be writing to it are gone.
|
|
157
|
+
*/
|
|
158
|
+
export function removeWithThisProcess(dir) {
|
|
159
|
+
return watch("d", dir);
|
|
160
|
+
}
|
|
161
|
+
/** Remove a temp directory. Best-effort: failing to clean up must never change a run's outcome. */
|
|
162
|
+
export function removeTempDir(dir) {
|
|
163
|
+
try {
|
|
164
|
+
rmSync(dir, { recursive: true, force: true });
|
|
165
|
+
}
|
|
166
|
+
catch { /* left for the OS to reap */ }
|
|
167
|
+
}
|
package/package.json
CHANGED
package/dist/lifetime.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Register something to undo if this process ends before `settle` is called.
|
|
3
|
-
* `undo` must be synchronous. Returns `settle`: call it once the thing has been
|
|
4
|
-
* dealt with in the normal course of the run.
|
|
5
|
-
*/
|
|
6
|
-
export declare function untilSettled(undo: () => void): () => void;
|
|
7
|
-
/** Remove a temp directory. Best-effort: failing to clean up must never change a run's outcome. */
|
|
8
|
-
export declare function removeTempDir(dir: string): void;
|
package/dist/lifetime.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
// What this process must undo if it ends while a run is still in flight:
|
|
2
|
-
// stop the processes it started, and delete what it left in the temp directory.
|
|
3
|
-
//
|
|
4
|
-
// A program, and every shell command it runs, leads its own process group — so
|
|
5
|
-
// that a timeout can take down everything it started. The cost is that none of
|
|
6
|
-
// them die with the runner: without this, Ctrl-C on `trama run` would leave the
|
|
7
|
-
// program running, and a program that coordinates others (a session) would
|
|
8
|
-
// leave them running when it ended.
|
|
9
|
-
import { rmSync } from "fs";
|
|
10
|
-
const SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
11
|
-
const pending = new Set();
|
|
12
|
-
let installed = false;
|
|
13
|
-
function runPending() {
|
|
14
|
-
for (const undo of [...pending]) {
|
|
15
|
-
try {
|
|
16
|
-
undo();
|
|
17
|
-
}
|
|
18
|
-
catch { /* best-effort: we are on the way out */ }
|
|
19
|
-
}
|
|
20
|
-
pending.clear();
|
|
21
|
-
}
|
|
22
|
-
function onSignal(signal) {
|
|
23
|
-
runPending();
|
|
24
|
-
// Having a handler at all cancelled the signal's default action. Step aside
|
|
25
|
-
// and send it again, so that what happens next is what would have happened
|
|
26
|
-
// had this handler never been installed: the process dies of it, or whoever
|
|
27
|
-
// else handles the signal decides. (Common libraries only act on a signal
|
|
28
|
-
// when they are its sole listener, so simply staying out of the way is not
|
|
29
|
-
// enough — the signal has to arrive once more without us.)
|
|
30
|
-
uninstall();
|
|
31
|
-
process.kill(process.pid, signal);
|
|
32
|
-
}
|
|
33
|
-
function install() {
|
|
34
|
-
if (installed)
|
|
35
|
-
return;
|
|
36
|
-
installed = true;
|
|
37
|
-
process.on("exit", runPending);
|
|
38
|
-
for (const signal of SIGNALS)
|
|
39
|
-
process.on(signal, onSignal);
|
|
40
|
-
}
|
|
41
|
-
/** Leave the host process's signal handling untouched whenever nothing is in flight. */
|
|
42
|
-
function uninstall() {
|
|
43
|
-
if (!installed)
|
|
44
|
-
return;
|
|
45
|
-
installed = false;
|
|
46
|
-
process.removeListener("exit", runPending);
|
|
47
|
-
for (const signal of SIGNALS)
|
|
48
|
-
process.removeListener(signal, onSignal);
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Register something to undo if this process ends before `settle` is called.
|
|
52
|
-
* `undo` must be synchronous. Returns `settle`: call it once the thing has been
|
|
53
|
-
* dealt with in the normal course of the run.
|
|
54
|
-
*/
|
|
55
|
-
export function untilSettled(undo) {
|
|
56
|
-
pending.add(undo);
|
|
57
|
-
install();
|
|
58
|
-
return () => {
|
|
59
|
-
pending.delete(undo);
|
|
60
|
-
if (pending.size === 0)
|
|
61
|
-
uninstall();
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
/** Remove a temp directory. Best-effort: failing to clean up must never change a run's outcome. */
|
|
65
|
-
export function removeTempDir(dir) {
|
|
66
|
-
try {
|
|
67
|
-
rmSync(dir, { recursive: true, force: true });
|
|
68
|
-
}
|
|
69
|
-
catch { /* left for the OS to reap */ }
|
|
70
|
-
}
|