@trama-dev/runtime 0.4.0 → 0.6.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 -3
- package/dist/abort.d.ts +12 -0
- package/dist/abort.js +31 -0
- package/dist/agent-backend.d.ts +127 -7
- package/dist/agent-backend.js +38 -19
- package/dist/agent-host.d.ts +50 -0
- package/dist/agent-host.js +97 -0
- package/dist/agent-hosts.d.ts +14 -0
- package/dist/agent-hosts.js +321 -0
- package/dist/agent.d.ts +26 -3
- package/dist/agent.js +165 -43
- package/dist/bridge-client.d.ts +6 -0
- package/dist/bridge-client.js +69 -16
- package/dist/budget.d.ts +52 -0
- package/dist/budget.js +118 -0
- package/dist/child-process.d.ts +18 -0
- package/dist/child-process.js +38 -9
- package/dist/client.d.ts +2 -1
- package/dist/client.js +9 -19
- package/dist/context.d.ts +36 -7
- package/dist/context.js +83 -92
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +2 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +254 -88
- package/dist/ipc-server.d.ts +14 -5
- package/dist/ipc-server.js +174 -144
- package/dist/journal-view.d.ts +2 -0
- package/dist/journal-view.js +52 -7
- package/dist/journal.d.ts +27 -9
- package/dist/journal.js +216 -79
- package/dist/meta.d.ts +18 -0
- package/dist/meta.js +29 -0
- package/dist/model-relay.d.ts +130 -0
- package/dist/model-relay.js +339 -0
- package/dist/path-guard.d.ts +128 -19
- package/dist/path-guard.js +384 -68
- package/dist/pi-adapter.d.ts +35 -9
- package/dist/pi-adapter.js +865 -66
- package/dist/replace-file.d.ts +31 -0
- package/dist/replace-file.js +75 -0
- package/dist/runner-types.d.ts +35 -0
- package/dist/runner.d.ts +202 -30
- package/dist/runner.js +949 -273
- package/dist/serializable.d.ts +10 -4
- package/dist/serializable.js +56 -31
- package/dist/stopping.d.ts +37 -3
- package/dist/stopping.js +167 -21
- package/dist/tools.d.ts +2 -1
- package/dist/tools.js +90 -21
- package/dist/tree.d.ts +241 -0
- package/dist/tree.js +1431 -0
- package/dist/types.d.ts +106 -15
- package/dist/workspace.d.ts +7 -0
- package/dist/workspace.js +93 -104
- package/package.json +1 -1
package/dist/path-guard.js
CHANGED
|
@@ -1,107 +1,423 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { lstatSync, mkdirSync, readlinkSync, statSync } from "fs";
|
|
2
|
+
import { dirname, isAbsolute, join, parse, resolve, sep } from "path";
|
|
3
3
|
/**
|
|
4
4
|
* Keep file access inside a bounded root directory (a project directory or a
|
|
5
5
|
* session workspace) — rejecting escapes via ".." segments or symlinks.
|
|
6
6
|
*
|
|
7
|
+
* A path is read the way the system reads it: one step at a time, each link
|
|
8
|
+
* followed where it stands, ".." taken from where the walk has got to. It is
|
|
9
|
+
* never tidied up as text first — "dir/../file" is "file" only if "dir" is a
|
|
10
|
+
* directory that is not a link, and the text cannot know.
|
|
11
|
+
*
|
|
7
12
|
* Not a security sandbox — just a footgun guard against malicious shared
|
|
8
13
|
* projects where files like state.json or .gitignore are symlinks pointing
|
|
9
14
|
* outside the project directory.
|
|
10
15
|
*/
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
/**
|
|
17
|
+
* The most links followed on the way to one path. More than this is a loop —
|
|
18
|
+
* to the strictest of the systems this runs on (macOS stops at 32, Linux at 40).
|
|
19
|
+
*/
|
|
20
|
+
const MAX_LINKS = 32;
|
|
21
|
+
const tooManyLinks = (path) => new Error(`Too many levels of symbolic links: ${path}`);
|
|
22
|
+
/**
|
|
23
|
+
* A step that is not a name: what the walk has come to must be a directory.
|
|
24
|
+
* A path says so by ending in a slash. (It is not the same as a last step of
|
|
25
|
+
* ".", which is looked up in the directory and so needs leave to search it.)
|
|
26
|
+
*/
|
|
27
|
+
const A_DIRECTORY = sep;
|
|
28
|
+
/** The steps of a path, one name at a time. */
|
|
29
|
+
function stepsOf(path) {
|
|
30
|
+
const steps = path.split(sep).filter(Boolean);
|
|
31
|
+
if (steps.length > 0 && path.endsWith(sep))
|
|
32
|
+
steps.push(A_DIRECTORY);
|
|
33
|
+
return steps;
|
|
14
34
|
}
|
|
35
|
+
const UTF8 = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true });
|
|
15
36
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* Catches ".." traversal only — use guardPath to also catch symlink escapes.
|
|
37
|
+
* A byte buffer as UTF-8 text — undefined if its bytes are not valid UTF-8.
|
|
38
|
+
* Leading BOM bytes are preserved as text rather than stripped.
|
|
19
39
|
*/
|
|
20
|
-
export function
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (!isInside(resolved, normalizedDir)) {
|
|
24
|
-
throw new Error(`Path escapes project directory: ${relativePath}`);
|
|
40
|
+
export function utf8Text(raw) {
|
|
41
|
+
try {
|
|
42
|
+
return UTF8.decode(raw);
|
|
25
43
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
function verifyRealPath(realPath, realRoot, originalPath) {
|
|
29
|
-
if (!isInside(realPath, realRoot)) {
|
|
30
|
-
throw new Error(`Path escapes project directory via symlink: ${originalPath}`);
|
|
44
|
+
catch {
|
|
45
|
+
return undefined;
|
|
31
46
|
}
|
|
32
47
|
}
|
|
33
48
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
49
|
+
* What a link says, as text — undefined if it says nothing a path can be made
|
|
50
|
+
* of: a name that is not valid UTF-8, or nothing at all (macOS lets a link to
|
|
51
|
+
* nothing be made, and then refuses every use of it).
|
|
37
52
|
*/
|
|
38
|
-
function
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const parent = dirname(cur);
|
|
42
|
-
if (parent === cur)
|
|
43
|
-
return; // reached filesystem root — nothing to verify
|
|
44
|
-
cur = parent;
|
|
45
|
-
}
|
|
46
|
-
verifyRealPath(realpathSync(cur), realRoot, originalPath);
|
|
53
|
+
export function linkText(link) {
|
|
54
|
+
const text = utf8Text(readlinkSync(link, "buffer"));
|
|
55
|
+
return text !== "" ? text : undefined;
|
|
47
56
|
}
|
|
57
|
+
/** The top of the file system an absolute path starts from: where every walk begins. */
|
|
58
|
+
const topOf = (path) => ({ at: parse(path).root, links: 0, missing: false });
|
|
48
59
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
60
|
+
* Where a path leads once every symlink on the way is followed. The path is
|
|
61
|
+
* taken as it is written, and may say "..", "." or end in a slash. One that
|
|
62
|
+
* starts at the top of the file system is walked from there; any other, on
|
|
63
|
+
* from `from` — so a path with no steps at all leads to where it started.
|
|
64
|
+
*
|
|
65
|
+
* Like realpath, but through what does not exist yet: a link whose
|
|
66
|
+
* destination is missing is still followed to where it points — and from
|
|
67
|
+
* there on, if that is a link too — and once nothing is there, the rest is
|
|
68
|
+
* taken as it is named. This is where a write to the path lands, and where a
|
|
69
|
+
* mkdir on the way to it creates directories.
|
|
70
|
+
*
|
|
71
|
+
* `passing` is told each place the walk comes to, the first and the last
|
|
72
|
+
* among them, with the steps still to be taken from there, and may give the
|
|
73
|
+
* place another name to go on by. `link` is asked about each link the walk
|
|
74
|
+
* comes to — where it is, and whether the path ends there — and may say where
|
|
75
|
+
* the walk is to be instead of following it. (A link so taken counts as one
|
|
76
|
+
* followed, however many it stands for: the count is then only what stops a
|
|
77
|
+
* walk that goes round. Whoever has links taken so, and needs to know whether
|
|
78
|
+
* the system would follow the words, asks the system.)
|
|
51
79
|
*/
|
|
52
|
-
function
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const
|
|
61
|
-
|
|
80
|
+
function follow(path, from, watch = {}) {
|
|
81
|
+
const fromTop = isAbsolute(path);
|
|
82
|
+
let { at, links, missing } = fromTop ? topOf(path) : from;
|
|
83
|
+
let steps = stepsOf(fromTop ? path.slice(at.length) : path);
|
|
84
|
+
for (;;) {
|
|
85
|
+
at = watch.passing?.(at, steps) ?? at;
|
|
86
|
+
if (steps.length === 0)
|
|
87
|
+
break;
|
|
88
|
+
const step = steps.shift();
|
|
89
|
+
if (step === "." || step === ".." || step === A_DIRECTORY) {
|
|
90
|
+
// "Here", "up" and "this is a directory" are said of a directory. Of
|
|
91
|
+
// something that is not there they lead nowhere the system would go —
|
|
92
|
+
// it cannot be created as a file and be a directory too. (Of a file
|
|
93
|
+
// they are refused further down, where the file is come to.)
|
|
94
|
+
if (missing) {
|
|
95
|
+
throw new Error(`Path cannot be followed: ${path} (it leads on from what does not exist)`);
|
|
96
|
+
}
|
|
97
|
+
if (step === A_DIRECTORY)
|
|
98
|
+
continue;
|
|
99
|
+
// Asked of the system rather than assumed: it takes a directory, and leave to search it.
|
|
100
|
+
lstatSync(at + sep + step);
|
|
101
|
+
if (step === "..")
|
|
102
|
+
at = dirname(at);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const next = join(at, step);
|
|
106
|
+
if (missing) {
|
|
107
|
+
at = next;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
let stat;
|
|
111
|
+
try {
|
|
112
|
+
stat = lstatSync(next);
|
|
113
|
+
}
|
|
114
|
+
catch (e) {
|
|
115
|
+
if (e.code !== "ENOENT")
|
|
116
|
+
throw e;
|
|
117
|
+
missing = true;
|
|
118
|
+
at = next;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (!stat.isSymbolicLink()) {
|
|
122
|
+
// Nothing lies beyond a file — not a name, and not "." or ".." either.
|
|
123
|
+
// The system refuses to go on, and so does this.
|
|
124
|
+
if (!stat.isDirectory() && steps.length > 0) {
|
|
125
|
+
throw Object.assign(new Error(`ENOTDIR: not a directory, ${next} (on the way to ${path})`), { code: "ENOTDIR" });
|
|
126
|
+
}
|
|
127
|
+
at = next;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const instead = watch.link?.(next, steps.length === 0);
|
|
131
|
+
if (instead) {
|
|
132
|
+
if (++links > MAX_LINKS)
|
|
133
|
+
throw tooManyLinks(path);
|
|
134
|
+
({ at, missing } = instead);
|
|
135
|
+
// As where a file is come to by a step: nothing lies beyond one.
|
|
136
|
+
if (steps.length > 0 && !missing && !lstatSync(at).isDirectory()) {
|
|
137
|
+
throw Object.assign(new Error(`ENOTDIR: not a directory, ${at} (on the way to ${path})`), { code: "ENOTDIR" });
|
|
138
|
+
}
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (++links > MAX_LINKS)
|
|
142
|
+
throw tooManyLinks(path);
|
|
143
|
+
const target = linkText(next);
|
|
144
|
+
if (target === undefined) {
|
|
145
|
+
throw new Error(`Path cannot be followed: ${next} (it links to nothing, or to what is not readable as text)`);
|
|
146
|
+
}
|
|
147
|
+
if (isAbsolute(target))
|
|
148
|
+
at = parse(target).root;
|
|
149
|
+
steps = [...stepsOf(target), ...steps];
|
|
62
150
|
}
|
|
151
|
+
return { at, links, missing };
|
|
63
152
|
}
|
|
64
|
-
/**
|
|
153
|
+
/**
|
|
154
|
+
* The real (symlink-resolved) location of a root directory, whether or not it
|
|
155
|
+
* exists yet. A root is named by whoever runs the program, not by the
|
|
156
|
+
* program, and is read as the rest of Node reads a directory it is given.
|
|
157
|
+
*/
|
|
65
158
|
export function realRootOf(rootDir) {
|
|
66
|
-
const
|
|
159
|
+
const root = resolve(rootDir);
|
|
160
|
+
return follow(root, topOf(root)).at;
|
|
161
|
+
}
|
|
162
|
+
/** Whether one normalized path is another or lies under it — as text, which is all this can say. */
|
|
163
|
+
const within = (path, root) => path === root || path.startsWith(root.endsWith(sep) ? root : root + sep);
|
|
164
|
+
export const identityIn = (stat) => `${stat.dev}:${stat.ino}`;
|
|
165
|
+
/**
|
|
166
|
+
* What tells one file or directory from every other — undefined if nothing is
|
|
167
|
+
* there to be told apart. Two paths with one identity are one place, however
|
|
168
|
+
* each is spelled; a link is itself, not what it points at.
|
|
169
|
+
*
|
|
170
|
+
* This is how the runtime asks whether two paths are the same. Their text
|
|
171
|
+
* cannot say: a file system that takes "Data" and "data" for one name has
|
|
172
|
+
* many names for every directory.
|
|
173
|
+
*
|
|
174
|
+
* Undefined says that nothing is there, and only that. What is there and may
|
|
175
|
+
* not be looked at is not nothing: it throws.
|
|
176
|
+
*/
|
|
177
|
+
export function identityOf(path) {
|
|
178
|
+
const stat = lstatSync(path, { bigint: true, throwIfNoEntry: false });
|
|
179
|
+
return stat && identityIn(stat);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* A destination by the root's own name for it, if it is the root or lies
|
|
183
|
+
* under it — undefined if it does not.
|
|
184
|
+
*
|
|
185
|
+
* Under it by name, or by being there: a file system that takes "Data" and
|
|
186
|
+
* "data" for one name has many names for every directory, and a link may use
|
|
187
|
+
* any of them. So where the names do not say, each directory above the
|
|
188
|
+
* destination is asked whether it is the root.
|
|
189
|
+
*/
|
|
190
|
+
function underRoot(destination, realRoot) {
|
|
191
|
+
if (within(destination, realRoot))
|
|
192
|
+
return destination;
|
|
193
|
+
const root = identityOf(realRoot);
|
|
194
|
+
if (!root)
|
|
195
|
+
return undefined;
|
|
196
|
+
for (let at = destination;; at = dirname(at)) {
|
|
197
|
+
if (identityOf(at) === root)
|
|
198
|
+
return join(realRoot, destination.slice(at.length));
|
|
199
|
+
if (at === dirname(at))
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* A reader of the links in `root` (named without links), for one who means
|
|
205
|
+
* to keep a copy of the root.
|
|
206
|
+
*
|
|
207
|
+
* It reads as a copy of the root would have to be read. A link of the root
|
|
208
|
+
* that a way passes through is not followed step by step but taken for the
|
|
209
|
+
* place it names: a copy has that link too, saying whatever it must to name
|
|
210
|
+
* the same place there, and how it says so is no part of the way. A link
|
|
211
|
+
* outside the root is nobody's copy, and is followed like any other step —
|
|
212
|
+
* to the end, also where the words end at it: what matters of it is where it
|
|
213
|
+
* leads.
|
|
214
|
+
*
|
|
215
|
+
* Whether a place is in the root is asked of the place (see underRoot), not
|
|
216
|
+
* read off its name: a way that has left the root may come back to it by any
|
|
217
|
+
* of the names the file system has for it.
|
|
218
|
+
*/
|
|
219
|
+
export function linksOf(root) {
|
|
220
|
+
/** What each link of the root that a way has passed through names — null while that is being worked out. */
|
|
221
|
+
const named = new Map();
|
|
222
|
+
let depth = 0;
|
|
223
|
+
const nameOf = (link) => {
|
|
224
|
+
const known = named.get(link);
|
|
225
|
+
if (known)
|
|
226
|
+
return known;
|
|
227
|
+
// (A link that is being worked out leads back to itself. And one link's words may lead through another's, and
|
|
228
|
+
// those through a third: the system follows so many in one way and no more, and neither is this to go deeper.)
|
|
229
|
+
if (known === null || depth >= MAX_LINKS)
|
|
230
|
+
throw tooManyLinks(link);
|
|
231
|
+
named.set(link, null);
|
|
232
|
+
depth += 1;
|
|
233
|
+
try {
|
|
234
|
+
const text = linkText(link);
|
|
235
|
+
if (text === undefined)
|
|
236
|
+
throw new Error(`Path cannot be followed: ${link} (it links to nothing, or to what is not readable as text)`);
|
|
237
|
+
const { place } = lead(dirname(link), text);
|
|
238
|
+
named.set(link, place);
|
|
239
|
+
return place;
|
|
240
|
+
}
|
|
241
|
+
catch (err) {
|
|
242
|
+
named.delete(link);
|
|
243
|
+
throw err;
|
|
244
|
+
}
|
|
245
|
+
finally {
|
|
246
|
+
depth -= 1;
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
const isLink = (place) => !place.missing && lstatSync(place.at, { throwIfNoEntry: false })?.isSymbolicLink() === true;
|
|
250
|
+
const lead = (dir, text, seen = {}) => {
|
|
251
|
+
let inside = !isAbsolute(text) && underRoot(dir, root) !== undefined;
|
|
252
|
+
let strays = false;
|
|
253
|
+
const place = follow(text, { at: dir, links: 0, missing: false }, {
|
|
254
|
+
passing(at, ahead) {
|
|
255
|
+
const own = underRoot(at, root);
|
|
256
|
+
if ((own !== undefined) !== inside) {
|
|
257
|
+
strays = true;
|
|
258
|
+
// A step into the root arrives at its top, with the rest still to be said.
|
|
259
|
+
seen.stayed = own === root;
|
|
260
|
+
if (own === root)
|
|
261
|
+
seen.sinceRoot = ahead.length === 0 ? "" : sep + ahead.map((step) => (step === A_DIRECTORY ? "" : step)).join(sep);
|
|
262
|
+
}
|
|
263
|
+
inside = own !== undefined;
|
|
264
|
+
return own;
|
|
265
|
+
},
|
|
266
|
+
link(link, last) {
|
|
267
|
+
if (!within(link, root))
|
|
268
|
+
return undefined;
|
|
269
|
+
if (last)
|
|
270
|
+
return { at: link, links: 0, missing: false };
|
|
271
|
+
let place = nameOf(link);
|
|
272
|
+
for (let hops = 1; within(place.at, root) && isLink(place); hops++) {
|
|
273
|
+
if (hops === MAX_LINKS)
|
|
274
|
+
throw tooManyLinks(link);
|
|
275
|
+
place = nameOf(place.at);
|
|
276
|
+
}
|
|
277
|
+
inside = within(place.at, root); // arrived by a link of the root, not by a step of this way
|
|
278
|
+
return place;
|
|
279
|
+
},
|
|
280
|
+
});
|
|
281
|
+
return { place, strays };
|
|
282
|
+
};
|
|
283
|
+
return {
|
|
284
|
+
names(dir, text) {
|
|
285
|
+
const seen = {};
|
|
286
|
+
const { place, strays } = lead(dir, text, seen);
|
|
287
|
+
return { to: place.at, strays, home: seen.stayed ? seen.sinceRoot : undefined };
|
|
288
|
+
},
|
|
289
|
+
sinceRoot(dir, text) {
|
|
290
|
+
const seen = {};
|
|
291
|
+
try {
|
|
292
|
+
lead(dir, text, seen);
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
// The way cannot be followed to its end. How far it came is known all the same, and is all that is asked.
|
|
296
|
+
}
|
|
297
|
+
return seen.sinceRoot;
|
|
298
|
+
},
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* The places under a root that make a path lead where it does: every place
|
|
303
|
+
* the way comes to there — each name it steps to, each link it follows, each
|
|
304
|
+
* directory it goes through (also one it leaves again with ".."), and the
|
|
305
|
+
* place it ends at. With any of them another thing, or gone, the path leads
|
|
306
|
+
* somewhere else, or nowhere. Told as far as the way can be followed, each
|
|
307
|
+
* as the way names it — which need not be how its directory lists it.
|
|
308
|
+
*/
|
|
309
|
+
export function wayTo(rootDir, path) {
|
|
310
|
+
const root = realRootOf(rootDir);
|
|
311
|
+
const places = new Set();
|
|
312
|
+
const cameTo = (at) => {
|
|
313
|
+
const own = underRoot(at, root);
|
|
314
|
+
if (own !== undefined && own !== root)
|
|
315
|
+
places.add(own);
|
|
316
|
+
};
|
|
67
317
|
try {
|
|
68
|
-
|
|
318
|
+
follow(path, { at: root, links: 0, missing: false }, {
|
|
319
|
+
passing(at) { cameTo(at); return undefined; },
|
|
320
|
+
link(link) { cameTo(link); return undefined; },
|
|
321
|
+
});
|
|
69
322
|
}
|
|
70
323
|
catch {
|
|
71
|
-
|
|
324
|
+
// The way cannot be followed to its end. How far it came is all that is known of it.
|
|
72
325
|
}
|
|
326
|
+
return [...places];
|
|
73
327
|
}
|
|
74
328
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
329
|
+
* Where a project is — which is a directory that is there. Nothing in the
|
|
330
|
+
* runtime makes one for a project that is gone: what it makes for a project,
|
|
331
|
+
* it makes in it. (Making the directories on the way to a file would make a
|
|
332
|
+
* project of that file, at the path of one that somebody has moved away.)
|
|
333
|
+
*/
|
|
334
|
+
export function projectAt(projectDir) {
|
|
335
|
+
const project = realRootOf(projectDir);
|
|
336
|
+
if (!statSync(project, { throwIfNoEntry: false })?.isDirectory())
|
|
337
|
+
throw new Error(`There is no project at ${project}.`);
|
|
338
|
+
return project;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* A question to ask later: whether what is at a project's path is still the
|
|
342
|
+
* directory that is there now. A project is known by what it is, and what is
|
|
343
|
+
* done over some time — generate a program, ask for a fix, try it, install it,
|
|
344
|
+
* write it down — is done to the project it was started on: not to another
|
|
345
|
+
* directory that somebody has put at the path meanwhile. (Gone is not another:
|
|
346
|
+
* what is to be done where no project is, each step says for itself.)
|
|
347
|
+
*/
|
|
348
|
+
export function sameProjectAs(projectDir) {
|
|
349
|
+
const start = lstatSync(realRootOf(projectDir), { bigint: true, throwIfNoEntry: false });
|
|
350
|
+
const wasId = start?.isDirectory() ? identityIn(start) : undefined;
|
|
351
|
+
return () => {
|
|
352
|
+
const root = realRootOf(projectDir);
|
|
353
|
+
const now = lstatSync(root, { bigint: true, throwIfNoEntry: false });
|
|
354
|
+
if (wasId !== undefined && now && !(now.isDirectory() && identityIn(now) === wasId)) {
|
|
355
|
+
throw new Error(`What is at ${root} is not the project this was started on: nothing more is done there.`);
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Make a directory under a root, with those on the way to it — in the root,
|
|
361
|
+
* which must be there: no root is made for the sake of what goes into it.
|
|
362
|
+
* This is for a root that is something in itself — a project, which
|
|
363
|
+
* somebody may have moved away since it was named: making the directory
|
|
364
|
+
* anew at its path would make another, and put into it what was meant for
|
|
365
|
+
* the first. (A workspace is nothing but its path, and is made as needed,
|
|
366
|
+
* with the way to it.)
|
|
367
|
+
* `dir` is a place the guard has passed.
|
|
368
|
+
*/
|
|
369
|
+
export function makeDirsIn(rootDir, dir) {
|
|
370
|
+
const root = realRootOf(rootDir);
|
|
371
|
+
if (!statSync(root, { throwIfNoEntry: false })?.isDirectory()) {
|
|
372
|
+
throw Object.assign(new Error(`There is no directory at ${root}.`), { code: "ENOENT" });
|
|
373
|
+
}
|
|
374
|
+
mkdirSync(dir, { recursive: true });
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* The guard for reading or writing a path under a bounded root: where the
|
|
378
|
+
* path leads, every link on the way followed — refused unless that is inside
|
|
379
|
+
* the root, and a file, a directory, or nothing yet. What comes back has no
|
|
380
|
+
* link left in it, so using it goes where this looked.
|
|
78
381
|
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
382
|
+
* A path that does not exist yet is followed as far as it does, so a caller
|
|
383
|
+
* can safely mkdir and write afterwards: a write lands on what a link points
|
|
384
|
+
* at, not on a link on the way.
|
|
81
385
|
*/
|
|
82
|
-
export function guardPath(rootDir,
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
386
|
+
export function guardPath(rootDir, path) {
|
|
387
|
+
const root = resolve(rootDir);
|
|
388
|
+
// The root is walked first, and the path on from where that arrives: the
|
|
389
|
+
// way the system reads the two written one after the other.
|
|
390
|
+
const realRoot = follow(root, topOf(root));
|
|
391
|
+
const destination = underRoot(follow(path, realRoot).at, realRoot.at);
|
|
392
|
+
// The same, as the system is asked about it below.
|
|
393
|
+
const given = isAbsolute(path) ? path : root + sep + path;
|
|
394
|
+
if (destination === undefined) {
|
|
395
|
+
// Said for whoever reads the refusal: if the text alone stays inside, it is the links that lead out.
|
|
396
|
+
throw new Error(`Path escapes project directory${within(resolve(given), root) ? " via symlink" : ""}: ${path}`);
|
|
397
|
+
}
|
|
398
|
+
// The walk above is this guard's own reading of the path. The system has
|
|
399
|
+
// the last word on it: where it can follow the path, it must arrive where
|
|
400
|
+
// the walk did, and where it cannot, that must be because nothing is there
|
|
401
|
+
// yet — not because it will not go (too many links, a name too long, no
|
|
402
|
+
// leave to pass), which the walk may not have noticed. A path the two read
|
|
403
|
+
// differently is one a write would put somewhere a read does not look.
|
|
404
|
+
let found;
|
|
89
405
|
try {
|
|
90
|
-
|
|
406
|
+
found = statSync(given, { bigint: true });
|
|
91
407
|
}
|
|
92
408
|
catch (e) {
|
|
93
409
|
if (e.code !== "ENOENT")
|
|
94
410
|
throw e;
|
|
95
|
-
// Path truly doesn't exist (not even as a dangling symlink)
|
|
96
|
-
// — verify nearest ancestor is inside the root
|
|
97
|
-
verifyNearestAncestor(resolved, realRoot, relativePath);
|
|
98
|
-
return resolved;
|
|
99
411
|
}
|
|
100
|
-
|
|
101
|
-
|
|
412
|
+
const there = lstatSync(destination, { bigint: true, throwIfNoEntry: false });
|
|
413
|
+
if (found ? !(there && identityIn(there) === identityIn(found)) : there !== undefined) {
|
|
414
|
+
throw new Error(`Path cannot be followed: ${path} (the system does not take it where its links say)`);
|
|
102
415
|
}
|
|
103
|
-
|
|
104
|
-
|
|
416
|
+
// What is there is content, or a place for it. A pipe, a socket or a device
|
|
417
|
+
// is the end of something else: reading one, or writing to one, can wait
|
|
418
|
+
// for ever on whoever is at the other end — and with it this whole process.
|
|
419
|
+
if (there && !there.isFile() && !there.isDirectory()) {
|
|
420
|
+
throw new Error(`Not a file or a directory: ${path}`);
|
|
105
421
|
}
|
|
106
|
-
return
|
|
422
|
+
return destination;
|
|
107
423
|
}
|
package/dist/pi-adapter.d.ts
CHANGED
|
@@ -2,23 +2,49 @@ import { type AgentBackend, type AgentCall } from "./agent-backend.js";
|
|
|
2
2
|
import type { ModelConfig } from "./runner-types.js";
|
|
3
3
|
/** The pi version this runtime is built against (recorded in each project's meta.json). */
|
|
4
4
|
export declare const piVersion: () => Promise<string>;
|
|
5
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* The pi coding agent: a fresh in-memory session for every call — in this
|
|
7
|
+
* process, or in a process of its own (see agent-host.ts). A call that has
|
|
8
|
+
* extensions is served in one: an extension is somebody's code, and where it
|
|
9
|
+
* runs it can hold up, or bring down, everything else that runs there. And
|
|
10
|
+
* so is every call of an adapter that was given variables (`env`: a run's):
|
|
11
|
+
* what an agent finds of pi, which key it has for a provider, and what the
|
|
12
|
+
* commands it runs are given, are a process's — and this process is the
|
|
13
|
+
* runner's, whose own are not a run's to change.
|
|
14
|
+
*/
|
|
6
15
|
export declare class PiAdapter implements AgentBackend {
|
|
7
16
|
private config;
|
|
8
|
-
|
|
17
|
+
/** Whether every call is served in a process of its own: where those processes are given variables this one has not. */
|
|
18
|
+
private apart;
|
|
19
|
+
/** The processes that serve this adapter's calls. */
|
|
20
|
+
private hosts;
|
|
21
|
+
constructor(config: ModelConfig, env?: Record<string, string>);
|
|
22
|
+
/** Have a call served: here, or by a host. */
|
|
23
|
+
ask(prompt: string, call: AgentCall): Promise<string>;
|
|
24
|
+
/** End the processes that served calls: what this adapter was for is over. (It can be asked again: it starts them anew.) */
|
|
25
|
+
close(): Promise<void>;
|
|
9
26
|
private createSession;
|
|
27
|
+
/** What is kept of each session that has extensions. */
|
|
28
|
+
private extended;
|
|
29
|
+
/** Where trama stands when each session's model is asked. */
|
|
30
|
+
private asking;
|
|
31
|
+
/** Be done with a session: its extensions, where it has any, told first that it is over. */
|
|
32
|
+
private letGoOf;
|
|
10
33
|
/**
|
|
11
34
|
* The configured provider/model is not in pi's catalog, so pi picked a model
|
|
12
35
|
* on its own. Say so once — otherwise a typo silently runs a different model.
|
|
13
36
|
*/
|
|
14
37
|
private warnUnknownModel;
|
|
15
|
-
private extractText;
|
|
16
38
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
39
|
+
* Serve a call in this process: run one prompt in a fresh session and
|
|
40
|
+
* return the assistant's text reply. (What a host does; and what `ask`
|
|
41
|
+
* does itself for a call without extensions.)
|
|
42
|
+
*
|
|
43
|
+
* Cancellation during `createSession` is handled step-by-step inside it; once
|
|
44
|
+
* `session.prompt()` is running, a cancelled call has pi abort the run — which
|
|
45
|
+
* settles the prompt (pi 1.x resolves it rather than rejecting, so
|
|
46
|
+
* `signal.aborted` is checked again when `prompt()` returns) and leaves on
|
|
47
|
+
* the session's record what the request that was cut off had taken by then.
|
|
22
48
|
*/
|
|
23
|
-
|
|
49
|
+
serve(prompt: string, call: AgentCall): Promise<string>;
|
|
24
50
|
}
|