@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/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
- /** Kill every shell command still running, and anything they left in the background. */
6
- killActiveShells(): void;
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 { cappedBuffer, signalGroup, superviseChild } from "./child-process.js";
6
- export { cappedBuffer };
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; `true` marks groups whose shell has
15
- // exited — only those are probed, since a group that is still starting up
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, shellExited] of groups) {
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
- groups.delete(pgid);
44
+ untrack(pgid);
34
45
  }
35
46
  if (!pending)
36
47
  stopSweeping();
37
48
  };
38
49
  const markShellExited = (pgid) => {
39
- if (!groups.has(pgid))
40
- return; // already killed by killActiveShells
41
- groups.set(pgid, true);
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
- killActiveShells() {
49
- for (const pgid of groups.keys())
50
- signalGroup(pgid, "SIGKILL");
51
- groups.clear();
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
- groups.delete(pgid);
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.2.1",
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": "^4.23.15"
41
+ "tsx": "4.23.15"
42
42
  }
43
43
  }