@trama-dev/runtime 0.3.0 → 0.4.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/agent-backend.d.ts +6 -0
- package/dist/agent-backend.js +21 -1
- package/dist/bridge-client.d.ts +11 -6
- package/dist/bridge-client.js +43 -24
- package/dist/child-process.d.ts +2 -21
- package/dist/child-process.js +2 -125
- package/dist/client.d.ts +1 -0
- package/dist/client.js +4 -10
- package/dist/context.d.ts +2 -0
- package/dist/context.js +1 -1
- package/dist/index.d.ts +14 -2
- package/dist/index.js +60 -6
- package/dist/ipc-server.d.ts +12 -4
- package/dist/ipc-server.js +87 -27
- package/dist/journal-view.d.ts +9 -0
- package/dist/journal-view.js +131 -0
- package/dist/journal.d.ts +60 -0
- package/dist/journal.js +322 -0
- package/dist/pi-adapter.d.ts +2 -2
- package/dist/pi-adapter.js +11 -22
- package/dist/runner-types.d.ts +11 -0
- package/dist/runner.d.ts +29 -5
- package/dist/runner.js +325 -92
- package/dist/stopping.d.ts +31 -0
- package/dist/stopping.js +230 -0
- package/dist/tools.js +10 -8
- package/package.json +2 -2
- package/dist/lifetime.d.ts +0 -12
- package/dist/lifetime.js +0 -85
package/dist/agent-backend.d.ts
CHANGED
|
@@ -16,3 +16,9 @@ export interface AgentBackend {
|
|
|
16
16
|
*/
|
|
17
17
|
ask(prompt: string, call: AgentCall): Promise<string>;
|
|
18
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Hold a piece of work to the promise above: reject as soon as `signal` is
|
|
21
|
+
* aborted, whether or not the work itself notices. What the work goes on to do
|
|
22
|
+
* after that is no longer waited for.
|
|
23
|
+
*/
|
|
24
|
+
export declare function rejectOnAbort<T>(work: Promise<T>, signal?: AbortSignal): Promise<T>;
|
package/dist/agent-backend.js
CHANGED
|
@@ -6,4 +6,24 @@
|
|
|
6
6
|
// request; what to ask for, and what to make of the reply, is decided by the
|
|
7
7
|
// caller. Nothing outside the backend's own file knows which agent, harness or
|
|
8
8
|
// model is behind it.
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Hold a piece of work to the promise above: reject as soon as `signal` is
|
|
11
|
+
* aborted, whether or not the work itself notices. What the work goes on to do
|
|
12
|
+
* after that is no longer waited for.
|
|
13
|
+
*/
|
|
14
|
+
export function rejectOnAbort(work, signal) {
|
|
15
|
+
if (!signal)
|
|
16
|
+
return work;
|
|
17
|
+
return new Promise((resolve, reject) => {
|
|
18
|
+
const onAbort = () => reject(new Error("Aborted"));
|
|
19
|
+
if (signal.aborted)
|
|
20
|
+
onAbort();
|
|
21
|
+
else
|
|
22
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
23
|
+
const settle = (pass) => (value) => {
|
|
24
|
+
signal.removeEventListener("abort", onAbort);
|
|
25
|
+
pass(value);
|
|
26
|
+
};
|
|
27
|
+
work.then(settle(resolve), settle(reject));
|
|
28
|
+
});
|
|
29
|
+
}
|
package/dist/bridge-client.d.ts
CHANGED
|
@@ -1,17 +1,22 @@
|
|
|
1
|
+
import type { Workspace } from "./types.js";
|
|
1
2
|
export interface Bridge {
|
|
2
3
|
/** What the runner handed the program when it started it — the bridge secret among it. */
|
|
3
4
|
init: Record<string, any>;
|
|
4
5
|
/** POST one call to the runner and return its result. */
|
|
5
6
|
call(endpoint: string, body: unknown): Promise<any>;
|
|
6
|
-
/**
|
|
7
|
-
* Open a call that lasts until the returned function is called — or until
|
|
8
|
-
* this process ends, however it ends. It has no result; it is the connection
|
|
9
|
-
* itself the runner goes by.
|
|
10
|
-
*/
|
|
11
|
-
hold(endpoint: string): () => void;
|
|
12
7
|
}
|
|
13
8
|
/**
|
|
14
9
|
* @param port - the runner's loopback port (TRAMA_PORT)
|
|
15
10
|
* @param initPath - the file the runner wrote for this program (TRAMA_INIT)
|
|
16
11
|
*/
|
|
17
12
|
export declare function openBridge(port: string, initPath: string): Bridge;
|
|
13
|
+
/**
|
|
14
|
+
* The bridge to the runner that started this process as a program — or null
|
|
15
|
+
* when this process is not a program, and so is itself where runs happen.
|
|
16
|
+
*/
|
|
17
|
+
export declare function parentBridge(): Bridge | null;
|
|
18
|
+
/**
|
|
19
|
+
* A workspace reached over a bridge: the directory `dir`, or — without one —
|
|
20
|
+
* the workspace of the session the calling program is running in.
|
|
21
|
+
*/
|
|
22
|
+
export declare function remoteWorkspace(call: Bridge["call"], dir?: string): Workspace;
|
package/dist/bridge-client.js
CHANGED
|
@@ -8,24 +8,22 @@ import { request } from "http";
|
|
|
8
8
|
*/
|
|
9
9
|
export function openBridge(port, initPath) {
|
|
10
10
|
const init = JSON.parse(readFileSync(initPath, "utf-8"));
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
path: endpoint,
|
|
15
|
-
method: "POST",
|
|
16
|
-
headers: {
|
|
17
|
-
"Content-Type": "application/json",
|
|
18
|
-
"Content-Length": Buffer.byteLength(payload),
|
|
19
|
-
"Authorization": `Bearer ${init.token}`,
|
|
20
|
-
},
|
|
21
|
-
});
|
|
22
|
-
// A call lasts as long as the work behind it — an agent run, or a wait for
|
|
23
|
-
// another program — so this uses node:http, which sets no deadline for the
|
|
24
|
-
// response. (fetch gives up after five minutes without response headers.)
|
|
11
|
+
// A call lasts as long as the work behind it — an agent run, or the whole
|
|
12
|
+
// run of another program — so this uses node:http, which sets no deadline
|
|
13
|
+
// for the response. (fetch gives up after five minutes without response headers.)
|
|
25
14
|
const call = (endpoint, body) => new Promise((resolve, reject) => {
|
|
26
15
|
const payload = JSON.stringify(body);
|
|
27
|
-
const req =
|
|
28
|
-
|
|
16
|
+
const req = request({
|
|
17
|
+
host: "127.0.0.1",
|
|
18
|
+
port,
|
|
19
|
+
path: endpoint,
|
|
20
|
+
method: "POST",
|
|
21
|
+
headers: {
|
|
22
|
+
"Content-Type": "application/json",
|
|
23
|
+
"Content-Length": Buffer.byteLength(payload),
|
|
24
|
+
"Authorization": `Bearer ${init.token}`,
|
|
25
|
+
},
|
|
26
|
+
}, (res) => {
|
|
29
27
|
const chunks = [];
|
|
30
28
|
res.on("data", (chunk) => chunks.push(chunk));
|
|
31
29
|
res.on("error", reject);
|
|
@@ -40,13 +38,34 @@ export function openBridge(port, initPath) {
|
|
|
40
38
|
req.on("error", reject);
|
|
41
39
|
req.end(payload);
|
|
42
40
|
});
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
41
|
+
return { init, call };
|
|
42
|
+
}
|
|
43
|
+
let parent;
|
|
44
|
+
/**
|
|
45
|
+
* The bridge to the runner that started this process as a program — or null
|
|
46
|
+
* when this process is not a program, and so is itself where runs happen.
|
|
47
|
+
*/
|
|
48
|
+
export function parentBridge() {
|
|
49
|
+
if (parent === undefined) {
|
|
50
|
+
const { TRAMA_PORT: port, TRAMA_INIT: initPath } = process.env;
|
|
51
|
+
try {
|
|
52
|
+
parent = port && initPath ? openBridge(port, initPath) : null;
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
parent = null; // a stale environment: there is no runner to ask
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return parent;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* A workspace reached over a bridge: the directory `dir`, or — without one —
|
|
62
|
+
* the workspace of the session the calling program is running in.
|
|
63
|
+
*/
|
|
64
|
+
export function remoteWorkspace(call, dir) {
|
|
65
|
+
return {
|
|
66
|
+
read: (path) => call("/workspace/read", { dir, path }),
|
|
67
|
+
write: (path, content) => call("/workspace/write", { dir, path, content }),
|
|
68
|
+
list: (pattern) => call("/workspace/list", { dir, pattern }),
|
|
69
|
+
observe: (pattern, options) => call("/workspace/observe", { dir, pattern, ...options }),
|
|
50
70
|
};
|
|
51
|
-
return { init, call, hold };
|
|
52
71
|
}
|
package/dist/child-process.d.ts
CHANGED
|
@@ -1,28 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ChildProcess } from "child_process";
|
|
2
2
|
/** A string accumulator that stops growing (and says so) once it passes 10MB. */
|
|
3
3
|
export declare function cappedBuffer(): {
|
|
4
4
|
append(text: string): void;
|
|
5
5
|
readonly value: string;
|
|
6
6
|
};
|
|
7
|
-
/**
|
|
8
|
-
* Send a signal to a whole process group. Children spawned with `detached: true`
|
|
9
|
-
* lead their own group, so this also reaches whatever they started in the background.
|
|
10
|
-
* Signal 0 only probes. Returns false if the group no longer exists.
|
|
11
|
-
*/
|
|
12
|
-
export declare function signalGroup(pgid: number, signal: NodeJS.Signals | 0): boolean;
|
|
13
|
-
/**
|
|
14
|
-
* Stop process groups for good: SIGTERM now, SIGKILL after the grace period to
|
|
15
|
-
* whichever are still there. Resolves when none is left — so that what follows
|
|
16
|
-
* (merging a directory they were writing to, deleting it) finds them gone.
|
|
17
|
-
*/
|
|
18
|
-
export declare function stopGroups(pgids: number[]): Promise<void>;
|
|
19
|
-
/**
|
|
20
|
-
* The same as stopGroups, for a process that is itself on its way out and can
|
|
21
|
-
* neither wait nor keep a timer: synchronous, with the grace period seen
|
|
22
|
-
* through by a small detached process. Once the groups are gone it also
|
|
23
|
-
* removes `dirs` — temp directories they may have been writing to until then.
|
|
24
|
-
*/
|
|
25
|
-
export declare function stopGroupsOnExit(pgids: number[], dirs: string[]): void;
|
|
26
7
|
/**
|
|
27
8
|
* A time limit that only runs down while nothing is holding it.
|
|
28
9
|
*
|
|
@@ -67,7 +48,7 @@ export interface ChildExit {
|
|
|
67
48
|
/**
|
|
68
49
|
* Collect a child's output and wait for it to exit, enforcing a time limit
|
|
69
50
|
* and an optional stop request. Either one stops the child's whole process
|
|
70
|
-
* group (see
|
|
51
|
+
* group (see stopping.ts).
|
|
71
52
|
* Resolves on exit however it happened; rejects only if the child failed to spawn.
|
|
72
53
|
*/
|
|
73
54
|
export declare function superviseChild(child: ChildProcess, options: SuperviseOptions): Promise<ChildExit>;
|
package/dist/child-process.js
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
import { spawn } from "child_process";
|
|
2
1
|
import { StringDecoder } from "string_decoder";
|
|
2
|
+
import { stopGroups } from "./stopping.js";
|
|
3
3
|
const MAX_OUTPUT = 10 * 1024 * 1024; // 10 MB
|
|
4
|
-
/** How long a process group gets to exit after SIGTERM before it is sent SIGKILL. */
|
|
5
|
-
const KILL_GRACE_MS = 5000;
|
|
6
|
-
const STOP_POLL_MS = 25;
|
|
7
|
-
/** How long a group is waited for after SIGKILL before it is given up on. */
|
|
8
|
-
const KILL_SETTLE_MS = 1000;
|
|
9
4
|
/** How long a child's output is waited for once the child itself has exited. */
|
|
10
5
|
const DRAIN_MS = 1000;
|
|
11
6
|
/** The longest delay a timer can be given; a longer one fires at once. */
|
|
@@ -27,124 +22,6 @@ export function cappedBuffer() {
|
|
|
27
22
|
get value() { return buf; },
|
|
28
23
|
};
|
|
29
24
|
}
|
|
30
|
-
/**
|
|
31
|
-
* Send a signal to a whole process group. Children spawned with `detached: true`
|
|
32
|
-
* lead their own group, so this also reaches whatever they started in the background.
|
|
33
|
-
* Signal 0 only probes. Returns false if the group no longer exists.
|
|
34
|
-
*/
|
|
35
|
-
export function signalGroup(pgid, signal) {
|
|
36
|
-
try {
|
|
37
|
-
process.kill(-pgid, signal);
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
catch {
|
|
41
|
-
return false;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
// --- Stopping ---
|
|
45
|
-
//
|
|
46
|
-
// Never SIGKILL first. A process group may hold a runner of its own — a program
|
|
47
|
-
// that coordinates others, a shell command that runs trama — and that runner
|
|
48
|
-
// has started process groups this one cannot see. SIGTERM is its chance to stop
|
|
49
|
-
// them; killed outright, it leaves them running. So a group is asked to stop,
|
|
50
|
-
// and killed only if it is still there when the grace period is over.
|
|
51
|
-
//
|
|
52
|
-
// There are two ways to see that through, depending on whether the process
|
|
53
|
-
// asking will be around to do it: stopGroups (it will) and stopGroupsOnExit
|
|
54
|
-
// (it will not).
|
|
55
|
-
/** Groups this process has asked to stop and is still watching. */
|
|
56
|
-
const stopping = new Map();
|
|
57
|
-
function stopGroup(pgid) {
|
|
58
|
-
// Asked once is enough: to some programs a second SIGTERM means "now".
|
|
59
|
-
const watched = stopping.get(pgid);
|
|
60
|
-
if (watched)
|
|
61
|
-
return watched;
|
|
62
|
-
if (!signalGroup(pgid, "SIGTERM"))
|
|
63
|
-
return Promise.resolve();
|
|
64
|
-
const gone = new Promise((resolve) => {
|
|
65
|
-
let deadline = performance.now() + KILL_GRACE_MS;
|
|
66
|
-
let killed = false;
|
|
67
|
-
const poll = setInterval(() => {
|
|
68
|
-
const alive = signalGroup(pgid, 0);
|
|
69
|
-
if (alive && performance.now() < deadline)
|
|
70
|
-
return;
|
|
71
|
-
if (alive && !killed) {
|
|
72
|
-
// Out of patience. A killed group is gone in a moment; allow it that
|
|
73
|
-
// moment, but do not wait on one that for some reason never goes.
|
|
74
|
-
signalGroup(pgid, "SIGKILL");
|
|
75
|
-
killed = true;
|
|
76
|
-
deadline = performance.now() + KILL_SETTLE_MS;
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
clearInterval(poll);
|
|
80
|
-
stopping.delete(pgid);
|
|
81
|
-
resolve();
|
|
82
|
-
}, STOP_POLL_MS);
|
|
83
|
-
});
|
|
84
|
-
stopping.set(pgid, gone);
|
|
85
|
-
return gone;
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* Stop process groups for good: SIGTERM now, SIGKILL after the grace period to
|
|
89
|
-
* whichever are still there. Resolves when none is left — so that what follows
|
|
90
|
-
* (merging a directory they were writing to, deleting it) finds them gone.
|
|
91
|
-
*/
|
|
92
|
-
export async function stopGroups(pgids) {
|
|
93
|
-
await Promise.all(pgids.map(stopGroup));
|
|
94
|
-
}
|
|
95
|
-
// What stopGroupsOnExit leaves behind to finish the job: a process of its own
|
|
96
|
-
// that waits for the groups to go, kills whichever are still there when the
|
|
97
|
-
// grace period is over, and then deletes the directories they were using.
|
|
98
|
-
const REAPER = `
|
|
99
|
-
const { rmSync } = require("fs");
|
|
100
|
-
const { grace, settle, poll, groups, dirs } = JSON.parse(process.argv[1]);
|
|
101
|
-
let left = groups;
|
|
102
|
-
const alive = (pgid) => { try { process.kill(-pgid, 0); return true; } catch { return false; } };
|
|
103
|
-
const finish = () => {
|
|
104
|
-
for (const dir of dirs) { try { rmSync(dir, { recursive: true, force: true }); } catch {} }
|
|
105
|
-
process.exit(0);
|
|
106
|
-
};
|
|
107
|
-
let deadline = performance.now() + grace;
|
|
108
|
-
let killed = false;
|
|
109
|
-
setInterval(() => {
|
|
110
|
-
// A group seen gone is never signalled again: its id may belong to another by now.
|
|
111
|
-
left = left.filter(alive);
|
|
112
|
-
if (left.length === 0) return finish();
|
|
113
|
-
if (performance.now() < deadline) return;
|
|
114
|
-
if (killed) return finish();
|
|
115
|
-
for (const pgid of left) { try { process.kill(-pgid, "SIGKILL"); } catch {} }
|
|
116
|
-
killed = true;
|
|
117
|
-
deadline = performance.now() + settle;
|
|
118
|
-
}, poll);
|
|
119
|
-
`;
|
|
120
|
-
/**
|
|
121
|
-
* The same as stopGroups, for a process that is itself on its way out and can
|
|
122
|
-
* neither wait nor keep a timer: synchronous, with the grace period seen
|
|
123
|
-
* through by a small detached process. Once the groups are gone it also
|
|
124
|
-
* removes `dirs` — temp directories they may have been writing to until then.
|
|
125
|
-
*/
|
|
126
|
-
export function stopGroupsOnExit(pgids, dirs) {
|
|
127
|
-
// Groups already asked to stop are not asked again, but still need seeing through.
|
|
128
|
-
const groups = pgids.filter(pgid => stopping.has(pgid) ? signalGroup(pgid, 0) : signalGroup(pgid, "SIGTERM"));
|
|
129
|
-
if (groups.length === 0)
|
|
130
|
-
return;
|
|
131
|
-
// Without a reaper, do not leave them to chance.
|
|
132
|
-
const killNow = () => { for (const pgid of groups)
|
|
133
|
-
signalGroup(pgid, "SIGKILL"); };
|
|
134
|
-
try {
|
|
135
|
-
const job = JSON.stringify({ grace: KILL_GRACE_MS, settle: KILL_SETTLE_MS, poll: STOP_POLL_MS, groups, dirs });
|
|
136
|
-
const reaper = spawn(process.execPath, ["-e", REAPER, job], { detached: true, stdio: "ignore" });
|
|
137
|
-
// A spawn that fails says so later, in an event this process will not be
|
|
138
|
-
// around for — but it has no pid from the start.
|
|
139
|
-
if (reaper.pid === undefined)
|
|
140
|
-
killNow();
|
|
141
|
-
reaper.on("error", killNow);
|
|
142
|
-
reaper.unref();
|
|
143
|
-
}
|
|
144
|
-
catch {
|
|
145
|
-
killNow();
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
25
|
export function createAllowance(ms) {
|
|
149
26
|
let remaining = ms;
|
|
150
27
|
let holds = 0;
|
|
@@ -194,7 +71,7 @@ export function createAllowance(ms) {
|
|
|
194
71
|
/**
|
|
195
72
|
* Collect a child's output and wait for it to exit, enforcing a time limit
|
|
196
73
|
* and an optional stop request. Either one stops the child's whole process
|
|
197
|
-
* group (see
|
|
74
|
+
* group (see stopping.ts).
|
|
198
75
|
* Resolves on exit however it happened; rejects only if the child failed to spawn.
|
|
199
76
|
*/
|
|
200
77
|
export function superviseChild(child, options) {
|
package/dist/client.d.ts
CHANGED
|
@@ -2,4 +2,5 @@ import type { Ctx, Agent, Tools, Workspace } from "./types.js";
|
|
|
2
2
|
export declare const ctx: Ctx;
|
|
3
3
|
export declare const agent: Agent;
|
|
4
4
|
export declare const tools: Tools;
|
|
5
|
+
/** workspace.* exists only inside a session — everywhere else it fails clearly, without a round trip. */
|
|
5
6
|
export declare const workspace: Workspace;
|
package/dist/client.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @trama-dev/runtime client — this is what program.ts imports.
|
|
2
2
|
// A thin wrapper that forwards every call to the runner's IPC server
|
|
3
3
|
// (ipc-server.ts), which does the real work.
|
|
4
|
-
import { openBridge } from "./bridge-client.js";
|
|
4
|
+
import { openBridge, remoteWorkspace } from "./bridge-client.js";
|
|
5
5
|
import { assertSerializable } from "./serializable.js";
|
|
6
6
|
const PORT = process.env.TRAMA_PORT;
|
|
7
7
|
if (!PORT) {
|
|
@@ -68,15 +68,9 @@ export const tools = {
|
|
|
68
68
|
fetch: (url, opts) => call("/tools/fetch", { url, ...opts }),
|
|
69
69
|
};
|
|
70
70
|
/** workspace.* exists only inside a session — everywhere else it fails clearly, without a round trip. */
|
|
71
|
-
|
|
71
|
+
export const workspace = remoteWorkspace((endpoint, body) => {
|
|
72
72
|
if (!initData.inSession) {
|
|
73
|
-
|
|
73
|
+
return Promise.reject(new Error("workspace is only available when running in a session"));
|
|
74
74
|
}
|
|
75
75
|
return call(endpoint, body);
|
|
76
|
-
}
|
|
77
|
-
export const workspace = {
|
|
78
|
-
read: (path) => workspaceCall("/workspace/read", { path }),
|
|
79
|
-
write: (path, content) => workspaceCall("/workspace/write", { path, content }),
|
|
80
|
-
list: (pattern) => workspaceCall("/workspace/list", { pattern }),
|
|
81
|
-
observe: (pattern, options) => workspaceCall("/workspace/observe", { pattern, ...options }),
|
|
82
|
-
};
|
|
76
|
+
});
|
package/dist/context.d.ts
CHANGED
|
@@ -14,4 +14,6 @@ export declare function createContext(projectDir: string, initialState: Record<s
|
|
|
14
14
|
maxIterations?: number;
|
|
15
15
|
argsOverride?: Record<string, unknown>;
|
|
16
16
|
onReady?: (data?: Record<string, unknown>) => void;
|
|
17
|
+
/** Where the run's log lines are shown as they are written. Defaults to the console. */
|
|
18
|
+
print?: (line: string) => void;
|
|
17
19
|
}): Ctx;
|
package/dist/context.js
CHANGED
|
@@ -51,7 +51,7 @@ export function createContext(projectDir, initialState, options) {
|
|
|
51
51
|
}
|
|
52
52
|
const entry = { ts: Date.now(), message, data: safeData };
|
|
53
53
|
appendFileSync(logsPath, JSON.stringify(entry) + "\n");
|
|
54
|
-
console.log(`[trama] ${message}`);
|
|
54
|
+
(options?.print ?? console.log)(`[trama] ${message}`);
|
|
55
55
|
},
|
|
56
56
|
async ready(data) {
|
|
57
57
|
if (readyCalled)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
export { runProgram } from "./runner.js";
|
|
2
|
-
export { createWorkspace } from "./workspace.js";
|
|
3
1
|
export type { ProgramResult, RunOptions } from "./runner-types.js";
|
|
2
|
+
import type { ProgramResult, RunOptions } from "./runner-types.js";
|
|
4
3
|
export type { Workspace, ObserveResult } from "./types.js";
|
|
4
|
+
import type { Workspace } from "./types.js";
|
|
5
|
+
/** Run a project to an outcome: done, yielded, or failed. */
|
|
6
|
+
export declare function runProgram(options: RunOptions): Promise<ProgramResult>;
|
|
7
|
+
/** The shared workspace in a directory, created if it does not exist: read, write, list, observe. */
|
|
8
|
+
export declare function createWorkspace(dir: string): Workspace;
|
|
5
9
|
export declare function validateProjectName(name: string): void;
|
|
6
10
|
export declare function resolveProject(name: string): string;
|
|
7
11
|
export declare function createProject(name: string, prompt: string, options?: {
|
|
@@ -11,3 +15,11 @@ export declare function createProject(name: string, prompt: string, options?: {
|
|
|
11
15
|
export declare function updateProject(name: string, prompt: string): Promise<void>;
|
|
12
16
|
export declare function listProjects(): Promise<void>;
|
|
13
17
|
export declare function showLogs(name: string): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Show what a run of a project did, from its journal: the latest run, or the
|
|
20
|
+
* one named. With `list`, show which runs have a journal instead.
|
|
21
|
+
*/
|
|
22
|
+
export declare function showJournal(name: string, options?: {
|
|
23
|
+
run?: string;
|
|
24
|
+
list?: boolean;
|
|
25
|
+
}): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,59 @@
|
|
|
1
1
|
// @trama-dev/runtime runner surface — exported via "@trama-dev/runtime/runner"
|
|
2
|
+
import { randomUUID } from "crypto";
|
|
2
3
|
import { readFileSync, writeFileSync, mkdirSync, readdirSync, existsSync, statSync, rmSync } from "fs";
|
|
3
|
-
import { join } from "path";
|
|
4
|
+
import { join, resolve } from "path";
|
|
4
5
|
import { homedir } from "os";
|
|
5
|
-
import {
|
|
6
|
+
import { piVersion } from "./pi-adapter.js";
|
|
6
7
|
import { stripCodeFences } from "./agent.js";
|
|
7
|
-
import { loadConfig, loadProjectConfig, loadState, createBackend, validateProgram, installValidatedProgram, withTempDir, ensureProjectScaffold, RUNTIME_TYPES, } from "./runner.js";
|
|
8
|
+
import { loadConfig, loadProjectConfig, loadState, createBackend, validateProgram, installValidatedProgram, withTempDir, ensureProjectScaffold, RUNTIME_TYPES, runProgram as runHere, } from "./runner.js";
|
|
9
|
+
import { createWorkspace as workspaceHere } from "./workspace.js";
|
|
10
|
+
import { parentBridge, remoteWorkspace } from "./bridge-client.js";
|
|
8
11
|
import { guardPath } from "./path-guard.js";
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
import { printJournals } from "./journal-view.js";
|
|
13
|
+
// --- Running a project, and reaching a workspace ---
|
|
14
|
+
//
|
|
15
|
+
// A program never runs another program: it asks its runner to. So these two do
|
|
16
|
+
// their work in this process only when this process is not itself a program.
|
|
17
|
+
// Inside one they are calls on the bridge, carried out by the one runner that
|
|
18
|
+
// owns every run in the tree — which is how it can time them, stop them, and
|
|
19
|
+
// see them.
|
|
20
|
+
/** Run a project to an outcome: done, yielded, or failed. */
|
|
21
|
+
export async function runProgram(options) {
|
|
22
|
+
const bridge = parentBridge();
|
|
23
|
+
if (!bridge)
|
|
24
|
+
return runHere(options);
|
|
25
|
+
// A signal cannot cross the bridge, but what it says can: the runner is told
|
|
26
|
+
// to stop the run, and this call still waits for the run to be over — the
|
|
27
|
+
// same as outside a program.
|
|
28
|
+
const { signal, ...request } = options;
|
|
29
|
+
const id = randomUUID();
|
|
30
|
+
const stop = () => { bridge.call("/run/stop", { id }).catch(() => { }); };
|
|
31
|
+
if (signal?.aborted)
|
|
32
|
+
stop();
|
|
33
|
+
else
|
|
34
|
+
signal?.addEventListener("abort", stop, { once: true });
|
|
35
|
+
try {
|
|
36
|
+
return await bridge.call("/run/program", {
|
|
37
|
+
...request,
|
|
38
|
+
id,
|
|
39
|
+
// The runner does not share this program's working directory.
|
|
40
|
+
projectDir: resolve(request.projectDir),
|
|
41
|
+
workspaceDir: request.workspaceDir && resolve(request.workspaceDir),
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
signal?.removeEventListener("abort", stop);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** The shared workspace in a directory, created if it does not exist: read, write, list, observe. */
|
|
49
|
+
export function createWorkspace(dir) {
|
|
50
|
+
const bridge = parentBridge();
|
|
51
|
+
if (!bridge)
|
|
52
|
+
return workspaceHere(dir);
|
|
53
|
+
const absolute = resolve(dir);
|
|
54
|
+
mkdirSync(absolute, { recursive: true }); // it exists from here on, as it does outside a program
|
|
55
|
+
return remoteWorkspace(bridge.call, absolute);
|
|
56
|
+
}
|
|
11
57
|
// --- Example programs (included in system prompt during create/update) ---
|
|
12
58
|
const EXAMPLE_PROGRAMS = `// Example A: Simple — one-shot action, no LLM needed.
|
|
13
59
|
import { ctx, tools } from "@trama-dev/runtime";
|
|
@@ -122,6 +168,7 @@ const validationOptions = (config) => ({
|
|
|
122
168
|
timeout: config.defaultTimeout,
|
|
123
169
|
maxRepairAttempts: config.maxRepairAttempts,
|
|
124
170
|
maxIterations: config.defaultMaxIterations,
|
|
171
|
+
journalRuns: config.journalRuns,
|
|
125
172
|
});
|
|
126
173
|
// --- Public API ---
|
|
127
174
|
export async function createProject(name, prompt, options) {
|
|
@@ -140,7 +187,7 @@ export async function createProject(name, prompt, options) {
|
|
|
140
187
|
const meta = {
|
|
141
188
|
input: { prompt, args: options?.args ?? {} },
|
|
142
189
|
createdAt: new Date().toISOString(),
|
|
143
|
-
piVersion:
|
|
190
|
+
piVersion: await piVersion(),
|
|
144
191
|
model: config.model,
|
|
145
192
|
provider: config.provider,
|
|
146
193
|
};
|
|
@@ -255,3 +302,10 @@ export async function showLogs(name) {
|
|
|
255
302
|
}
|
|
256
303
|
}
|
|
257
304
|
}
|
|
305
|
+
/**
|
|
306
|
+
* Show what a run of a project did, from its journal: the latest run, or the
|
|
307
|
+
* one named. With `list`, show which runs have a journal instead.
|
|
308
|
+
*/
|
|
309
|
+
export async function showJournal(name, options = {}) {
|
|
310
|
+
printJournals(resolveProject(name), name, options);
|
|
311
|
+
}
|
package/dist/ipc-server.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type Server } from "http";
|
|
2
2
|
import type { Ctx, Agent, Tools, Workspace } from "./types.js";
|
|
3
|
-
import type { ProgramResult } from "./runner-types.js";
|
|
3
|
+
import type { ProgramResult, RunRequest } from "./runner-types.js";
|
|
4
|
+
import type { Journal } from "./journal.js";
|
|
4
5
|
export interface IpcBackends {
|
|
5
6
|
/**
|
|
6
7
|
* The secret only this run's program knows. Every request must present it:
|
|
@@ -11,12 +12,19 @@ export interface IpcBackends {
|
|
|
11
12
|
ctx: Ctx;
|
|
12
13
|
agent: Agent;
|
|
13
14
|
tools: Tools;
|
|
14
|
-
/**
|
|
15
|
+
/** The workspace of the session the program is running in — null when it is in none. */
|
|
15
16
|
workspace: Workspace | null;
|
|
17
|
+
/** A workspace by directory, for a program that created a session rather than joined one. */
|
|
18
|
+
workspaceAt(dir: string): Workspace;
|
|
19
|
+
/**
|
|
20
|
+
* Run another project on the program's behalf, until it ends or `stop` says
|
|
21
|
+
* to stop it. `seq` is the position of the call that asked for it.
|
|
22
|
+
*/
|
|
23
|
+
run(request: RunRequest, stop: AbortSignal, seq: number): Promise<ProgramResult>;
|
|
24
|
+
/** Where every call, and how it ended, is written down. */
|
|
25
|
+
journal: Journal;
|
|
16
26
|
/** Called once ctx.done() or ctx.yield() has been persisted for this run. */
|
|
17
27
|
onTerminalSignal(result: ProgramResult): void;
|
|
18
|
-
/** Stop the program's clock until the returned function is called (see Allowance). */
|
|
19
|
-
holdClock(): () => void;
|
|
20
28
|
}
|
|
21
29
|
export declare function createIPCServer(backends: IpcBackends): Server;
|
|
22
30
|
/** Start listening on an ephemeral loopback port and return it. */
|