@trama-dev/runtime 0.1.5 → 0.2.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 +4 -4
- package/dist/agent.js +3 -3
- package/dist/child-process.d.ts +40 -0
- package/dist/child-process.js +108 -0
- package/dist/client.js +46 -99
- package/dist/context.d.ts +11 -0
- package/dist/context.js +48 -62
- package/dist/index.d.ts +3 -1
- package/dist/index.js +36 -59
- package/dist/ipc-server.d.ts +22 -0
- package/dist/ipc-server.js +168 -0
- package/dist/path-guard.d.ts +16 -26
- package/dist/path-guard.js +42 -37
- package/dist/pi-adapter.d.ts +11 -2
- package/dist/pi-adapter.js +49 -6
- package/dist/runner-types.d.ts +29 -0
- package/dist/runner-types.js +3 -0
- package/dist/runner.d.ts +46 -35
- package/dist/runner.js +369 -897
- package/dist/tools.d.ts +3 -5
- package/dist/tools.js +70 -166
- package/dist/types.d.ts +2 -43
- package/dist/types.js +4 -1
- package/dist/workspace.js +76 -124
- package/package.json +6 -4
package/dist/tools.d.ts
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import type { Tools } from "./types.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
readonly value: string;
|
|
5
|
-
};
|
|
2
|
+
import { cappedBuffer } from "./child-process.js";
|
|
3
|
+
export { cappedBuffer };
|
|
6
4
|
export interface ToolsWithCleanup extends Tools {
|
|
7
|
-
/** Kill
|
|
5
|
+
/** Kill every shell command still running, and anything they left in the background. */
|
|
8
6
|
killActiveShells(): void;
|
|
9
7
|
}
|
|
10
8
|
export declare function createTools(projectDir: string, signal?: AbortSignal): ToolsWithCleanup;
|
package/dist/tools.js
CHANGED
|
@@ -1,194 +1,98 @@
|
|
|
1
1
|
import { spawn } from "child_process";
|
|
2
|
-
import { readFileSync, writeFileSync, mkdirSync
|
|
2
|
+
import { readFileSync, writeFileSync, mkdirSync } from "fs";
|
|
3
3
|
import { resolve, dirname } from "path";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return {
|
|
11
|
-
append(text) {
|
|
12
|
-
if (capped)
|
|
13
|
-
return;
|
|
14
|
-
buf += text;
|
|
15
|
-
if (buf.length > MAX_OUTPUT) {
|
|
16
|
-
buf = buf.slice(0, MAX_OUTPUT) + "\n[trama] output truncated at 10MB";
|
|
17
|
-
capped = true;
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
|
-
get value() { return buf; },
|
|
21
|
-
};
|
|
22
|
-
}
|
|
4
|
+
import { guardPath } from "./path-guard.js";
|
|
5
|
+
import { cappedBuffer, signalGroup, superviseChild } from "./child-process.js";
|
|
6
|
+
export { cappedBuffer };
|
|
7
|
+
const MAX_FETCH_BODY = 10 * 1024 * 1024; // 10 MB
|
|
8
|
+
const DEFAULT_SHELL_TIMEOUT = 30_000;
|
|
9
|
+
const GROUP_SWEEP_MS = 250;
|
|
23
10
|
export function createTools(projectDir, signal) {
|
|
24
11
|
const normalizedProjectDir = resolve(projectDir);
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const timer = sessionReapers.get(sessionId);
|
|
37
|
-
if (timer) {
|
|
38
|
-
clearTimeout(timer);
|
|
39
|
-
sessionReapers.delete(sessionId);
|
|
40
|
-
}
|
|
12
|
+
// Each shell command leads its own process group, so background processes it
|
|
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.
|
|
17
|
+
const groups = new Map();
|
|
18
|
+
let sweeper = null;
|
|
19
|
+
const stopSweeping = () => {
|
|
20
|
+
if (sweeper)
|
|
21
|
+
clearInterval(sweeper);
|
|
22
|
+
sweeper = null;
|
|
41
23
|
};
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
24
|
+
/** Forget groups that have fully exited, so a recycled pid is never signalled. */
|
|
25
|
+
const sweep = () => {
|
|
26
|
+
let pending = false;
|
|
27
|
+
for (const [pgid, shellExited] of groups) {
|
|
28
|
+
if (!shellExited)
|
|
29
|
+
continue;
|
|
30
|
+
if (signalGroup(pgid, 0))
|
|
31
|
+
pending = true;
|
|
32
|
+
else
|
|
33
|
+
groups.delete(pgid);
|
|
45
34
|
}
|
|
46
|
-
|
|
35
|
+
if (!pending)
|
|
36
|
+
stopSweeping();
|
|
47
37
|
};
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
catch {
|
|
58
|
-
// ESRCH — process group gone
|
|
59
|
-
activeSessions.delete(sessionId);
|
|
60
|
-
sessionReapers.delete(sessionId);
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
reapSessionWhenGone(sessionId);
|
|
64
|
-
}, 250);
|
|
65
|
-
timer.unref?.();
|
|
66
|
-
sessionReapers.set(sessionId, timer);
|
|
38
|
+
const markShellExited = (pgid) => {
|
|
39
|
+
if (!groups.has(pgid))
|
|
40
|
+
return; // already killed by killActiveShells
|
|
41
|
+
groups.set(pgid, true);
|
|
42
|
+
if (!sweeper) {
|
|
43
|
+
sweeper = setInterval(sweep, GROUP_SWEEP_MS);
|
|
44
|
+
sweeper.unref();
|
|
45
|
+
}
|
|
67
46
|
};
|
|
68
|
-
const resolvePath = (p) => resolveBounded(normalizedProjectDir, p);
|
|
69
|
-
const checkRealPath = (real, original) => verifyRealPath(real, realProjectDir, original);
|
|
70
|
-
const checkNearestAncestor = (resolved, original) => verifyNearestAncestor(resolved, realProjectDir, original);
|
|
71
47
|
return {
|
|
72
48
|
killActiveShells() {
|
|
73
|
-
for (const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
catch { /* already exited */ }
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
for (const sessionId of activeSessions) {
|
|
82
|
-
killSession(sessionId, "SIGKILL");
|
|
83
|
-
stopReapingSession(sessionId);
|
|
84
|
-
}
|
|
85
|
-
activeShells.clear();
|
|
86
|
-
activeSessions.clear();
|
|
49
|
+
for (const pgid of groups.keys())
|
|
50
|
+
signalGroup(pgid, "SIGKILL");
|
|
51
|
+
groups.clear();
|
|
52
|
+
stopSweeping();
|
|
87
53
|
},
|
|
88
54
|
async read(path) {
|
|
89
|
-
const target =
|
|
90
|
-
let real;
|
|
55
|
+
const target = guardPath(normalizedProjectDir, path);
|
|
91
56
|
try {
|
|
92
|
-
|
|
57
|
+
return readFileSync(target, "utf-8");
|
|
93
58
|
}
|
|
94
59
|
catch (e) {
|
|
95
|
-
if (e.code === "ENOENT")
|
|
60
|
+
if (e.code === "ENOENT")
|
|
96
61
|
throw new Error(`File not found: ${path}`);
|
|
97
|
-
}
|
|
98
62
|
throw e;
|
|
99
63
|
}
|
|
100
|
-
checkRealPath(real, path);
|
|
101
|
-
return readFileSync(target, "utf-8");
|
|
102
64
|
},
|
|
103
65
|
async write(path, content) {
|
|
104
|
-
const target =
|
|
105
|
-
checkNearestAncestor(target, path); // before mkdir — prevents side effects outside project
|
|
66
|
+
const target = guardPath(normalizedProjectDir, path); // before mkdir — no side effects outside the project
|
|
106
67
|
mkdirSync(dirname(target), { recursive: true });
|
|
107
|
-
try {
|
|
108
|
-
if (lstatSync(target).isSymbolicLink()) {
|
|
109
|
-
verifySymlink(target, realProjectDir, path);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
catch (e) {
|
|
113
|
-
if (e.code !== "ENOENT")
|
|
114
|
-
throw e;
|
|
115
|
-
}
|
|
116
68
|
writeFileSync(target, content);
|
|
117
69
|
},
|
|
118
70
|
async shell(command, options = {}) {
|
|
119
|
-
const
|
|
120
|
-
const
|
|
71
|
+
const cwd = options.cwd ? guardPath(normalizedProjectDir, options.cwd) : normalizedProjectDir;
|
|
72
|
+
const child = spawn("sh", ["-c", command], {
|
|
73
|
+
cwd,
|
|
74
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
75
|
+
detached: true,
|
|
76
|
+
});
|
|
77
|
+
const pgid = child.pid;
|
|
78
|
+
if (pgid)
|
|
79
|
+
groups.set(pgid, false);
|
|
121
80
|
try {
|
|
122
|
-
|
|
123
|
-
|
|
81
|
+
const exit = await superviseChild(child, { timeout: options.timeout ?? DEFAULT_SHELL_TIMEOUT });
|
|
82
|
+
if (pgid)
|
|
83
|
+
markShellExited(pgid);
|
|
84
|
+
return {
|
|
85
|
+
exitCode: exit.timedOut ? 1 : (exit.code ?? 1),
|
|
86
|
+
stdout: exit.stdout,
|
|
87
|
+
stderr: exit.timedOut ? exit.stderr + "\ntimeout" : exit.stderr,
|
|
88
|
+
};
|
|
124
89
|
}
|
|
125
|
-
catch (
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
90
|
+
catch (spawnError) {
|
|
91
|
+
// Failed to start (e.g. cwd does not exist) — a result, not an exception.
|
|
92
|
+
if (pgid)
|
|
93
|
+
groups.delete(pgid);
|
|
94
|
+
return { exitCode: 1, stdout: "", stderr: spawnError.message };
|
|
129
95
|
}
|
|
130
|
-
return new Promise((resolve) => {
|
|
131
|
-
const child = spawn("sh", ["-c", command], {
|
|
132
|
-
cwd,
|
|
133
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
134
|
-
detached: true,
|
|
135
|
-
});
|
|
136
|
-
activeShells.add(child);
|
|
137
|
-
if (child.pid)
|
|
138
|
-
activeSessions.add(child.pid);
|
|
139
|
-
const out = cappedBuffer();
|
|
140
|
-
const err = cappedBuffer();
|
|
141
|
-
const outDecoder = new StringDecoder("utf-8");
|
|
142
|
-
const errDecoder = new StringDecoder("utf-8");
|
|
143
|
-
let timedOut = false;
|
|
144
|
-
let exited = false;
|
|
145
|
-
let killFallbackTimer = null;
|
|
146
|
-
child.stdout.on("data", (chunk) => { out.append(outDecoder.write(chunk)); });
|
|
147
|
-
child.stderr.on("data", (chunk) => { err.append(errDecoder.write(chunk)); });
|
|
148
|
-
const timer = setTimeout(() => {
|
|
149
|
-
timedOut = true;
|
|
150
|
-
if (child.pid) {
|
|
151
|
-
killSession(child.pid, "SIGTERM");
|
|
152
|
-
}
|
|
153
|
-
killFallbackTimer = setTimeout(() => {
|
|
154
|
-
if (!exited && child.pid) {
|
|
155
|
-
killSession(child.pid, "SIGKILL");
|
|
156
|
-
}
|
|
157
|
-
}, 5000);
|
|
158
|
-
killFallbackTimer.unref?.();
|
|
159
|
-
}, timeout);
|
|
160
|
-
child.on("error", (spawnError) => {
|
|
161
|
-
clearTimeout(timer);
|
|
162
|
-
if (killFallbackTimer)
|
|
163
|
-
clearTimeout(killFallbackTimer);
|
|
164
|
-
activeShells.delete(child);
|
|
165
|
-
if (child.pid) {
|
|
166
|
-
activeSessions.delete(child.pid);
|
|
167
|
-
stopReapingSession(child.pid);
|
|
168
|
-
}
|
|
169
|
-
resolve({ exitCode: 1, stdout: out.value, stderr: spawnError.message });
|
|
170
|
-
});
|
|
171
|
-
child.on("close", (code) => {
|
|
172
|
-
exited = true;
|
|
173
|
-
activeShells.delete(child);
|
|
174
|
-
clearTimeout(timer);
|
|
175
|
-
if (killFallbackTimer)
|
|
176
|
-
clearTimeout(killFallbackTimer);
|
|
177
|
-
if (child.pid)
|
|
178
|
-
reapSessionWhenGone(child.pid);
|
|
179
|
-
const outFlush = outDecoder.end();
|
|
180
|
-
const errFlush = errDecoder.end();
|
|
181
|
-
if (outFlush)
|
|
182
|
-
out.append(outFlush);
|
|
183
|
-
if (errFlush)
|
|
184
|
-
err.append(errFlush);
|
|
185
|
-
resolve({
|
|
186
|
-
exitCode: timedOut ? 1 : (code ?? 1),
|
|
187
|
-
stdout: out.value,
|
|
188
|
-
stderr: timedOut ? err.value + "\ntimeout" : err.value,
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
96
|
},
|
|
193
97
|
async fetch(url, options = {}) {
|
|
194
98
|
const response = await globalThis.fetch(url, {
|
|
@@ -198,8 +102,8 @@ export function createTools(projectDir, signal) {
|
|
|
198
102
|
signal,
|
|
199
103
|
});
|
|
200
104
|
const raw = await response.text();
|
|
201
|
-
const body = raw.length >
|
|
202
|
-
? raw.slice(0,
|
|
105
|
+
const body = raw.length > MAX_FETCH_BODY
|
|
106
|
+
? raw.slice(0, MAX_FETCH_BODY) + "\n[trama] response body truncated at 10MB"
|
|
203
107
|
: raw;
|
|
204
108
|
const headers = {};
|
|
205
109
|
response.headers.forEach((v, k) => { headers[k] = v; });
|
package/dist/types.d.ts
CHANGED
|
@@ -37,18 +37,15 @@ export interface Ctx {
|
|
|
37
37
|
* Does NOT terminate execution — program should return naturally.
|
|
38
38
|
* Idempotent: second call is a no-op.
|
|
39
39
|
* Mutually exclusive with yield() — throws if yield() was already called.
|
|
40
|
-
* Terminal choice is irreversible: once done() is attempted (even if it fails),
|
|
41
|
-
* yield() is permanently blocked. Fix the issue and retry done() instead.
|
|
42
40
|
*/
|
|
43
41
|
done(result?: Record<string, unknown>): Promise<void>;
|
|
44
42
|
/**
|
|
45
43
|
* Cooperative suspension. Persists ctx.state with a yield marker and exits (logging is best-effort).
|
|
46
44
|
* The session layer (or manual `trama run`) re-runs the program when ready.
|
|
47
45
|
* The program resumes by reading its own ctx.state.
|
|
48
|
-
* Idempotent: second call is a no-op.
|
|
49
46
|
* Mutually exclusive with done() — throws if done() was already called.
|
|
50
|
-
*
|
|
51
|
-
*
|
|
47
|
+
* A done() or yield() that fails (e.g. on unserializable state) settles nothing:
|
|
48
|
+
* fix the problem and call either one.
|
|
52
49
|
*/
|
|
53
50
|
yield(reason: string): Promise<never>;
|
|
54
51
|
}
|
|
@@ -61,10 +58,6 @@ export interface Agent {
|
|
|
61
58
|
instruct(prompt: string, options?: {
|
|
62
59
|
system?: string;
|
|
63
60
|
}): Promise<string>;
|
|
64
|
-
/** @deprecated Use instruct() instead. Alias kept for backwards compatibility. */
|
|
65
|
-
ask(prompt: string, options?: {
|
|
66
|
-
system?: string;
|
|
67
|
-
}): Promise<string>;
|
|
68
61
|
/**
|
|
69
62
|
* Structured LLM call. Returns typed object matching the provided schema.
|
|
70
63
|
* Supported schema value prefixes: "string", "number", "boolean".
|
|
@@ -123,37 +116,3 @@ export interface Workspace {
|
|
|
123
116
|
timeout?: number;
|
|
124
117
|
}): Promise<ObserveResult>;
|
|
125
118
|
}
|
|
126
|
-
export interface ProgramResult {
|
|
127
|
-
status: "done" | "yielded" | "failed";
|
|
128
|
-
/** Yield reason, or error summary for failed. */
|
|
129
|
-
reason?: string;
|
|
130
|
-
/** From ctx.done(result), if provided. */
|
|
131
|
-
result?: unknown;
|
|
132
|
-
}
|
|
133
|
-
export interface PiAdapterConfig {
|
|
134
|
-
provider: string;
|
|
135
|
-
model: string;
|
|
136
|
-
}
|
|
137
|
-
export interface RunOptions {
|
|
138
|
-
projectDir: string;
|
|
139
|
-
maxRepairAttempts?: number;
|
|
140
|
-
timeout?: number;
|
|
141
|
-
args?: Record<string, unknown>;
|
|
142
|
-
/** Shared workspace directory. When set, workspace.* API is enabled for the program. */
|
|
143
|
-
workspaceDir?: string;
|
|
144
|
-
}
|
|
145
|
-
export interface RepairInput {
|
|
146
|
-
programSource: string;
|
|
147
|
-
error: string;
|
|
148
|
-
runtimeTypes: string;
|
|
149
|
-
}
|
|
150
|
-
export interface ChildResult {
|
|
151
|
-
exitCode: number;
|
|
152
|
-
stdout: string;
|
|
153
|
-
stderr: string;
|
|
154
|
-
}
|
|
155
|
-
export interface TramaConfig extends PiAdapterConfig {
|
|
156
|
-
maxRepairAttempts: number;
|
|
157
|
-
defaultTimeout: number;
|
|
158
|
-
defaultMaxIterations: number;
|
|
159
|
-
}
|
package/dist/types.js
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
// @trama-dev/runtime
|
|
1
|
+
// The program-facing API: everything a program.ts can import from "@trama-dev/runtime".
|
|
2
|
+
// This file is the single source of truth for that API — its generated .d.ts is also
|
|
3
|
+
// what the LLM is shown when it writes or repairs a program, so the doc comments here
|
|
4
|
+
// are part of the prompt. Runner-side types live in runner-types.ts.
|
|
2
5
|
export {};
|