@trama-dev/runtime 0.1.1 → 0.1.3
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 +25 -6
- package/dist/agent.js +28 -12
- package/dist/client.js +51 -16
- package/dist/context.d.ts +2 -0
- package/dist/context.js +47 -17
- package/dist/index.d.ts +1 -0
- package/dist/index.js +53 -25
- package/dist/path-guard.d.ts +37 -0
- package/dist/path-guard.js +102 -0
- package/dist/pi-adapter.d.ts +2 -0
- package/dist/pi-adapter.js +21 -7
- package/dist/runner.d.ts +15 -1
- package/dist/runner.js +407 -128
- package/dist/serializable.js +2 -1
- package/dist/tools.js +44 -75
- package/dist/types.d.ts +8 -0
- package/package.json +5 -4
package/dist/serializable.js
CHANGED
|
@@ -29,7 +29,8 @@ export function assertSerializable(value, path, ancestors = new Set()) {
|
|
|
29
29
|
ancestors.delete(value);
|
|
30
30
|
return;
|
|
31
31
|
}
|
|
32
|
-
|
|
32
|
+
const proto = Object.getPrototypeOf(value);
|
|
33
|
+
if (proto !== Object.prototype && proto !== null) {
|
|
33
34
|
const name = value.constructor?.name ?? "unknown";
|
|
34
35
|
throw new TypeError(`${path} is not a plain object (${name})`);
|
|
35
36
|
}
|
package/dist/tools.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { spawn
|
|
2
|
-
import { readFileSync, writeFileSync, mkdirSync, realpathSync,
|
|
3
|
-
import { resolve, dirname
|
|
1
|
+
import { spawn } from "child_process";
|
|
2
|
+
import { readFileSync, writeFileSync, mkdirSync, realpathSync, lstatSync } from "fs";
|
|
3
|
+
import { resolve, dirname } from "path";
|
|
4
|
+
import { StringDecoder } from "string_decoder";
|
|
5
|
+
import { resolveBounded, verifyRealPath, verifyNearestAncestor, verifySymlink } from "./path-guard.js";
|
|
4
6
|
const MAX_OUTPUT = 10 * 1024 * 1024; // 10 MB
|
|
5
7
|
export function cappedBuffer() {
|
|
6
8
|
let buf = "";
|
|
@@ -37,39 +39,23 @@ export function createTools(projectDir, signal) {
|
|
|
37
39
|
sessionReapers.delete(sessionId);
|
|
38
40
|
}
|
|
39
41
|
};
|
|
40
|
-
const listSessionPids = (sessionId) => {
|
|
41
|
-
const result = spawnSync("ps", ["-o", "pid=", "-s", String(sessionId)], {
|
|
42
|
-
encoding: "utf-8",
|
|
43
|
-
});
|
|
44
|
-
if (result.error)
|
|
45
|
-
return [];
|
|
46
|
-
return result.stdout
|
|
47
|
-
.split("\n")
|
|
48
|
-
.map(line => parseInt(line.trim(), 10))
|
|
49
|
-
.filter(pid => Number.isInteger(pid) && pid > 0);
|
|
50
|
-
};
|
|
51
42
|
const killSession = (sessionId, signal) => {
|
|
52
|
-
const pids = listSessionPids(sessionId);
|
|
53
|
-
if (pids.length > 0) {
|
|
54
|
-
for (const pid of pids) {
|
|
55
|
-
try {
|
|
56
|
-
process.kill(pid, signal);
|
|
57
|
-
}
|
|
58
|
-
catch { /* already exited */ }
|
|
59
|
-
}
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
43
|
try {
|
|
63
44
|
process.kill(-sessionId, signal);
|
|
64
45
|
}
|
|
65
46
|
catch { /* already exited */ }
|
|
66
47
|
};
|
|
48
|
+
/** Poll until the process group is gone, then remove from tracking. */
|
|
67
49
|
const reapSessionWhenGone = (sessionId) => {
|
|
68
50
|
stopReapingSession(sessionId);
|
|
69
51
|
const timer = setTimeout(() => {
|
|
70
52
|
if (!activeSessions.has(sessionId))
|
|
71
53
|
return;
|
|
72
|
-
|
|
54
|
+
try {
|
|
55
|
+
process.kill(-sessionId, 0); // signal 0: check if process group still exists
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// ESRCH — process group gone
|
|
73
59
|
activeSessions.delete(sessionId);
|
|
74
60
|
sessionReapers.delete(sessionId);
|
|
75
61
|
return;
|
|
@@ -79,39 +65,9 @@ export function createTools(projectDir, signal) {
|
|
|
79
65
|
timer.unref?.();
|
|
80
66
|
sessionReapers.set(sessionId, timer);
|
|
81
67
|
};
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
* Not a security sandbox — just a footgun guard.
|
|
86
|
-
*/
|
|
87
|
-
const resolvePath = (p) => {
|
|
88
|
-
const resolved = resolve(normalizedProjectDir, p);
|
|
89
|
-
if (resolved !== normalizedProjectDir && !resolved.startsWith(normalizedProjectDir + sep)) {
|
|
90
|
-
throw new Error(`Path escapes project directory: ${p}`);
|
|
91
|
-
}
|
|
92
|
-
return resolved;
|
|
93
|
-
};
|
|
94
|
-
/** Verify that an existing path's real location is inside the project dir. */
|
|
95
|
-
const verifyRealPath = (real, original) => {
|
|
96
|
-
if (real !== realProjectDir && !real.startsWith(realProjectDir + sep)) {
|
|
97
|
-
throw new Error(`Path escapes project directory via symlink: ${original}`);
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
/**
|
|
101
|
-
* Walk up from `resolved` to find the deepest existing ancestor,
|
|
102
|
-
* then verify its real path is inside the project dir.
|
|
103
|
-
* This catches symlink escapes BEFORE any mkdir side effects.
|
|
104
|
-
*/
|
|
105
|
-
const verifyNearestAncestor = (resolved, original) => {
|
|
106
|
-
let cur = resolved;
|
|
107
|
-
while (!existsSync(cur)) {
|
|
108
|
-
const parent = dirname(cur);
|
|
109
|
-
if (parent === cur)
|
|
110
|
-
return; // reached filesystem root — nothing to verify
|
|
111
|
-
cur = parent;
|
|
112
|
-
}
|
|
113
|
-
verifyRealPath(realpathSync(cur), original);
|
|
114
|
-
};
|
|
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);
|
|
115
71
|
return {
|
|
116
72
|
killActiveShells() {
|
|
117
73
|
for (const proc of activeShells) {
|
|
@@ -131,24 +87,26 @@ export function createTools(projectDir, signal) {
|
|
|
131
87
|
},
|
|
132
88
|
async read(path) {
|
|
133
89
|
const target = resolvePath(path);
|
|
134
|
-
|
|
90
|
+
let real;
|
|
91
|
+
try {
|
|
92
|
+
real = realpathSync(target);
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
if (e.code === "ENOENT") {
|
|
96
|
+
throw new Error(`File not found: ${path}`);
|
|
97
|
+
}
|
|
98
|
+
throw e;
|
|
99
|
+
}
|
|
100
|
+
checkRealPath(real, path);
|
|
135
101
|
return readFileSync(target, "utf-8");
|
|
136
102
|
},
|
|
137
103
|
async write(path, content) {
|
|
138
104
|
const target = resolvePath(path);
|
|
139
|
-
|
|
105
|
+
checkNearestAncestor(target, path); // before mkdir — prevents side effects outside project
|
|
140
106
|
mkdirSync(dirname(target), { recursive: true });
|
|
141
107
|
try {
|
|
142
108
|
if (lstatSync(target).isSymbolicLink()) {
|
|
143
|
-
|
|
144
|
-
verifyRealPath(realpathSync(target), path);
|
|
145
|
-
}
|
|
146
|
-
catch (e) {
|
|
147
|
-
if (e.code !== "ENOENT")
|
|
148
|
-
throw e;
|
|
149
|
-
// Dangling symlink — resolve what we can via nearest existing ancestor
|
|
150
|
-
verifyNearestAncestor(resolve(dirname(target), readlinkSync(target)), path);
|
|
151
|
-
}
|
|
109
|
+
verifySymlink(target, realProjectDir, path);
|
|
152
110
|
}
|
|
153
111
|
}
|
|
154
112
|
catch (e) {
|
|
@@ -162,7 +120,7 @@ export function createTools(projectDir, signal) {
|
|
|
162
120
|
const cwd = options.cwd ? resolvePath(options.cwd) : normalizedProjectDir;
|
|
163
121
|
try {
|
|
164
122
|
if (options.cwd)
|
|
165
|
-
|
|
123
|
+
checkRealPath(realpathSync(cwd), options.cwd);
|
|
166
124
|
}
|
|
167
125
|
catch (err) {
|
|
168
126
|
if (err.code !== "ENOENT")
|
|
@@ -180,11 +138,13 @@ export function createTools(projectDir, signal) {
|
|
|
180
138
|
activeSessions.add(child.pid);
|
|
181
139
|
const out = cappedBuffer();
|
|
182
140
|
const err = cappedBuffer();
|
|
141
|
+
const outDecoder = new StringDecoder("utf-8");
|
|
142
|
+
const errDecoder = new StringDecoder("utf-8");
|
|
183
143
|
let timedOut = false;
|
|
184
144
|
let exited = false;
|
|
185
145
|
let killFallbackTimer = null;
|
|
186
|
-
child.stdout.on("data", (chunk) => { out.append(
|
|
187
|
-
child.stderr.on("data", (chunk) => { err.append(
|
|
146
|
+
child.stdout.on("data", (chunk) => { out.append(outDecoder.write(chunk)); });
|
|
147
|
+
child.stderr.on("data", (chunk) => { err.append(errDecoder.write(chunk)); });
|
|
188
148
|
const timer = setTimeout(() => {
|
|
189
149
|
timedOut = true;
|
|
190
150
|
if (child.pid) {
|
|
@@ -197,7 +157,7 @@ export function createTools(projectDir, signal) {
|
|
|
197
157
|
}, 5000);
|
|
198
158
|
killFallbackTimer.unref?.();
|
|
199
159
|
}, timeout);
|
|
200
|
-
child.on("error", (
|
|
160
|
+
child.on("error", (spawnError) => {
|
|
201
161
|
clearTimeout(timer);
|
|
202
162
|
if (killFallbackTimer)
|
|
203
163
|
clearTimeout(killFallbackTimer);
|
|
@@ -206,7 +166,7 @@ export function createTools(projectDir, signal) {
|
|
|
206
166
|
activeSessions.delete(child.pid);
|
|
207
167
|
stopReapingSession(child.pid);
|
|
208
168
|
}
|
|
209
|
-
resolve({ exitCode: 1, stdout: out.value, stderr:
|
|
169
|
+
resolve({ exitCode: 1, stdout: out.value, stderr: spawnError.message });
|
|
210
170
|
});
|
|
211
171
|
child.on("close", (code) => {
|
|
212
172
|
exited = true;
|
|
@@ -216,6 +176,12 @@ export function createTools(projectDir, signal) {
|
|
|
216
176
|
clearTimeout(killFallbackTimer);
|
|
217
177
|
if (child.pid)
|
|
218
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);
|
|
219
185
|
resolve({
|
|
220
186
|
exitCode: timedOut ? 1 : (code ?? 1),
|
|
221
187
|
stdout: out.value,
|
|
@@ -231,7 +197,10 @@ export function createTools(projectDir, signal) {
|
|
|
231
197
|
body: options.body,
|
|
232
198
|
signal,
|
|
233
199
|
});
|
|
234
|
-
const
|
|
200
|
+
const raw = await response.text();
|
|
201
|
+
const body = raw.length > MAX_OUTPUT
|
|
202
|
+
? raw.slice(0, MAX_OUTPUT) + "\n[trama] response body truncated at 10MB"
|
|
203
|
+
: raw;
|
|
235
204
|
const headers = {};
|
|
236
205
|
response.headers.forEach((v, k) => { headers[k] = v; });
|
|
237
206
|
return { status: response.status, body, headers };
|
package/dist/types.d.ts
CHANGED
|
@@ -19,6 +19,13 @@ export interface Ctx {
|
|
|
19
19
|
maxIterations: number;
|
|
20
20
|
/** Write a structured log entry */
|
|
21
21
|
log(message: string, data?: Record<string, unknown>): Promise<void>;
|
|
22
|
+
/**
|
|
23
|
+
* Signal that a long-running program has finished startup and is now serving.
|
|
24
|
+
* Used by smoke validation to distinguish "started successfully" from "completed".
|
|
25
|
+
* This marks startup success only; it is not a guarantee about long-term health.
|
|
26
|
+
* Idempotent: second call is a no-op.
|
|
27
|
+
*/
|
|
28
|
+
ready(data?: Record<string, unknown>): Promise<void>;
|
|
22
29
|
/** Persist current ctx.state to disk */
|
|
23
30
|
checkpoint(): Promise<void>;
|
|
24
31
|
/**
|
|
@@ -81,6 +88,7 @@ export interface RunOptions {
|
|
|
81
88
|
projectDir: string;
|
|
82
89
|
maxRepairAttempts?: number;
|
|
83
90
|
timeout?: number;
|
|
91
|
+
args?: Record<string, unknown>;
|
|
84
92
|
}
|
|
85
93
|
export interface RepairInput {
|
|
86
94
|
programSource: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trama-dev/runtime",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -32,9 +32,10 @@
|
|
|
32
32
|
"README.md",
|
|
33
33
|
"LICENSE"
|
|
34
34
|
],
|
|
35
|
+
"os": ["darwin", "linux"],
|
|
35
36
|
"dependencies": {
|
|
36
|
-
"@mariozechner/pi-coding-agent": "0.
|
|
37
|
-
"@mariozechner/pi-ai": "0.
|
|
38
|
-
"tsx": "^4.
|
|
37
|
+
"@mariozechner/pi-coding-agent": "0.64.0",
|
|
38
|
+
"@mariozechner/pi-ai": "0.64.0",
|
|
39
|
+
"tsx": "^4.21.0"
|
|
39
40
|
}
|
|
40
41
|
}
|