@trama-dev/runtime 0.2.1 → 0.3.0
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/README.md +1 -1
- package/dist/agent-backend.d.ts +18 -0
- package/dist/agent-backend.js +9 -0
- package/dist/agent.d.ts +12 -2
- package/dist/agent.js +23 -6
- package/dist/bridge-client.d.ts +17 -0
- package/dist/bridge-client.js +52 -0
- package/dist/child-process.d.ts +40 -7
- package/dist/child-process.js +180 -25
- package/dist/client.js +3 -15
- package/dist/index.js +35 -33
- package/dist/ipc-server.d.ts +2 -0
- package/dist/ipc-server.js +21 -3
- package/dist/lifetime.d.ts +12 -0
- package/dist/lifetime.js +85 -0
- package/dist/pi-adapter.d.ts +6 -13
- package/dist/pi-adapter.js +9 -32
- package/dist/runner-types.d.ts +9 -7
- package/dist/runner.d.ts +44 -37
- package/dist/runner.js +305 -234
- package/dist/tools.d.ts +2 -4
- package/dist/tools.js +33 -18
- package/package.json +2 -2
package/dist/tools.d.ts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type { Tools } from "./types.js";
|
|
2
|
-
import { cappedBuffer } from "./child-process.js";
|
|
3
|
-
export { cappedBuffer };
|
|
4
2
|
export interface ToolsWithCleanup extends Tools {
|
|
5
|
-
/**
|
|
6
|
-
|
|
3
|
+
/** Stop every shell command still running, and anything they left in the background. Resolves when they are gone. */
|
|
4
|
+
stopActiveShells(): Promise<void>;
|
|
7
5
|
}
|
|
8
6
|
export declare function createTools(projectDir: string, signal?: AbortSignal): ToolsWithCleanup;
|
package/dist/tools.js
CHANGED
|
@@ -2,8 +2,8 @@ import { spawn } from "child_process";
|
|
|
2
2
|
import { readFileSync, writeFileSync, mkdirSync } from "fs";
|
|
3
3
|
import { resolve, dirname } from "path";
|
|
4
4
|
import { guardPath } from "./path-guard.js";
|
|
5
|
-
import {
|
|
6
|
-
|
|
5
|
+
import { signalGroup, stopGroups, superviseChild } from "./child-process.js";
|
|
6
|
+
import { stopWithThisProcess } from "./lifetime.js";
|
|
7
7
|
const MAX_FETCH_BODY = 10 * 1024 * 1024; // 10 MB
|
|
8
8
|
const DEFAULT_SHELL_TIMEOUT = 30_000;
|
|
9
9
|
const GROUP_SWEEP_MS = 250;
|
|
@@ -11,11 +11,22 @@ export function createTools(projectDir, signal) {
|
|
|
11
11
|
const normalizedProjectDir = resolve(projectDir);
|
|
12
12
|
// Each shell command leads its own process group, so background processes it
|
|
13
13
|
// starts stay killable after the shell itself exits. A group is tracked from
|
|
14
|
-
// spawn until it has no members left
|
|
15
|
-
// exited — only those are probed, since a group that is still starting
|
|
16
|
-
// may not be visible yet.
|
|
14
|
+
// spawn until it has no members left. `shellExited` marks groups whose shell
|
|
15
|
+
// has exited — only those are probed, since a group that is still starting
|
|
16
|
+
// up may not be visible yet. `forget` tells the process-lifetime guard the
|
|
17
|
+
// group no longer needs killing if this process dies.
|
|
17
18
|
const groups = new Map();
|
|
18
19
|
let sweeper = null;
|
|
20
|
+
const untrack = (pgid) => {
|
|
21
|
+
groups.get(pgid)?.forget();
|
|
22
|
+
groups.delete(pgid);
|
|
23
|
+
};
|
|
24
|
+
// Shell commands run here do not get the bridge's address or the init file
|
|
25
|
+
// (which holds its secret). A top-level runner has neither in its
|
|
26
|
+
// environment, but a runner inside a program — a session's creator —
|
|
27
|
+
// inherits both. (This covers tools.shell only: what the agent backend runs
|
|
28
|
+
// on its own takes its environment from this process.)
|
|
29
|
+
const { TRAMA_PORT: _port, TRAMA_INIT: _init, ...shellEnv } = process.env;
|
|
19
30
|
const stopSweeping = () => {
|
|
20
31
|
if (sweeper)
|
|
21
32
|
clearInterval(sweeper);
|
|
@@ -24,31 +35,33 @@ export function createTools(projectDir, signal) {
|
|
|
24
35
|
/** Forget groups that have fully exited, so a recycled pid is never signalled. */
|
|
25
36
|
const sweep = () => {
|
|
26
37
|
let pending = false;
|
|
27
|
-
for (const [pgid,
|
|
28
|
-
if (!shellExited)
|
|
38
|
+
for (const [pgid, group] of groups) {
|
|
39
|
+
if (!group.shellExited)
|
|
29
40
|
continue;
|
|
30
41
|
if (signalGroup(pgid, 0))
|
|
31
42
|
pending = true;
|
|
32
43
|
else
|
|
33
|
-
|
|
44
|
+
untrack(pgid);
|
|
34
45
|
}
|
|
35
46
|
if (!pending)
|
|
36
47
|
stopSweeping();
|
|
37
48
|
};
|
|
38
49
|
const markShellExited = (pgid) => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
50
|
+
const group = groups.get(pgid);
|
|
51
|
+
if (!group)
|
|
52
|
+
return; // already stopped by stopActiveShells
|
|
53
|
+
group.shellExited = true;
|
|
42
54
|
if (!sweeper) {
|
|
43
55
|
sweeper = setInterval(sweep, GROUP_SWEEP_MS);
|
|
44
56
|
sweeper.unref();
|
|
45
57
|
}
|
|
46
58
|
};
|
|
47
59
|
return {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
60
|
+
async stopActiveShells() {
|
|
61
|
+
const pgids = [...groups.keys()];
|
|
62
|
+
await stopGroups(pgids);
|
|
63
|
+
for (const pgid of pgids)
|
|
64
|
+
untrack(pgid);
|
|
52
65
|
stopSweeping();
|
|
53
66
|
},
|
|
54
67
|
async read(path) {
|
|
@@ -71,12 +84,14 @@ export function createTools(projectDir, signal) {
|
|
|
71
84
|
const cwd = options.cwd ? guardPath(normalizedProjectDir, options.cwd) : normalizedProjectDir;
|
|
72
85
|
const child = spawn("sh", ["-c", command], {
|
|
73
86
|
cwd,
|
|
87
|
+
env: shellEnv,
|
|
74
88
|
stdio: ["pipe", "pipe", "pipe"],
|
|
75
89
|
detached: true,
|
|
76
90
|
});
|
|
77
91
|
const pgid = child.pid;
|
|
78
|
-
if (pgid)
|
|
79
|
-
groups.set(pgid, false);
|
|
92
|
+
if (pgid) {
|
|
93
|
+
groups.set(pgid, { shellExited: false, forget: stopWithThisProcess(pgid) });
|
|
94
|
+
}
|
|
80
95
|
try {
|
|
81
96
|
const exit = await superviseChild(child, { timeout: options.timeout ?? DEFAULT_SHELL_TIMEOUT });
|
|
82
97
|
if (pgid)
|
|
@@ -90,7 +105,7 @@ export function createTools(projectDir, signal) {
|
|
|
90
105
|
catch (spawnError) {
|
|
91
106
|
// Failed to start (e.g. cwd does not exist) — a result, not an exception.
|
|
92
107
|
if (pgid)
|
|
93
|
-
|
|
108
|
+
untrack(pgid);
|
|
94
109
|
return { exitCode: 1, stdout: "", stderr: spawnError.message };
|
|
95
110
|
}
|
|
96
111
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trama-dev/runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -38,6 +38,6 @@
|
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@earendil-works/pi-coding-agent": "1.5.2",
|
|
41
|
-
"tsx": "
|
|
41
|
+
"tsx": "4.23.15"
|
|
42
42
|
}
|
|
43
43
|
}
|