@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/types.d.ts
CHANGED
|
@@ -6,13 +6,13 @@ export interface Ctx {
|
|
|
6
6
|
};
|
|
7
7
|
/**
|
|
8
8
|
* Persistent state. Must be strictly JSON-serializable.
|
|
9
|
-
* Local working copy — only checkpoint() and done() persist to disk.
|
|
9
|
+
* Local working copy — only checkpoint(), yield() and done() persist to disk.
|
|
10
10
|
* Reserved keys: any key starting with `__trama_` is reserved.
|
|
11
11
|
*/
|
|
12
12
|
state: Record<string, unknown>;
|
|
13
13
|
/**
|
|
14
14
|
* Current iteration counter (starts at 0).
|
|
15
|
-
* Advances only when checkpoint() or done() is called.
|
|
15
|
+
* Advances only when checkpoint(), yield() or done() is called.
|
|
16
16
|
*/
|
|
17
17
|
iteration: number;
|
|
18
18
|
/** Max iterations hint. Not enforced by runtime. */
|
|
@@ -30,7 +30,10 @@ export interface Ctx {
|
|
|
30
30
|
* Idempotent: second call is a no-op.
|
|
31
31
|
*/
|
|
32
32
|
ready(data?: Record<string, unknown>): Promise<void>;
|
|
33
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* Persist current ctx.state to disk.
|
|
35
|
+
* Throws once done() or yield() has been called: the run has ended, and its state is settled.
|
|
36
|
+
*/
|
|
34
37
|
checkpoint(): Promise<void>;
|
|
35
38
|
/**
|
|
36
39
|
* Signal program completion. Persists state (logging is best-effort).
|
|
@@ -49,24 +52,105 @@ export interface Ctx {
|
|
|
49
52
|
*/
|
|
50
53
|
yield(reason: string): Promise<never>;
|
|
51
54
|
}
|
|
55
|
+
/** How much a model is to think before it answers. */
|
|
56
|
+
export type Thinking = "off" | "minimal" | "low" | "medium" | "high" | "xhigh" | "max";
|
|
57
|
+
/**
|
|
58
|
+
* What a call may say about how it is served. Each is honoured or the call
|
|
59
|
+
* fails: nothing asked for here is quietly left out. (But by an extension
|
|
60
|
+
* the call asked for itself: what one does to a session is its to do.)
|
|
61
|
+
*/
|
|
62
|
+
export interface AgentOptions {
|
|
63
|
+
/** Instructions for this call, on top of the agent's own. */
|
|
64
|
+
system?: string;
|
|
65
|
+
/** The model that serves this call, as "provider/model" — in place of the project's. */
|
|
66
|
+
model?: string;
|
|
67
|
+
/** How much the model is to think. A level pi does not have for the model is an error. */
|
|
68
|
+
thinking?: Thinking;
|
|
69
|
+
/**
|
|
70
|
+
* The agent's tools for this call: exactly the ones named. `[]` for none —
|
|
71
|
+
* the answer is then made from the prompt alone, and nothing is read, run
|
|
72
|
+
* or written. Without it the agent has its own choice of them (read, bash,
|
|
73
|
+
* edit, write); pi has more than it chooses (grep, find, ls). A name the
|
|
74
|
+
* agent has no tool for is an error.
|
|
75
|
+
*/
|
|
76
|
+
tools?: string[];
|
|
77
|
+
/**
|
|
78
|
+
* The skills the agent has for this call: `true` for all that pi finds
|
|
79
|
+
* for the user and for the project (its `.pi/skills` among them; of two
|
|
80
|
+
* by one name, the project's) — or the ones named. Without it, none. A
|
|
81
|
+
* name pi has no skill by is an error, and so is a call whose agent could
|
|
82
|
+
* not read them: pi tells a model of its skills only where it has `read`
|
|
83
|
+
* or `bash`. A prompt of `instruct` that begins with "/skill:name " is
|
|
84
|
+
* then that skill's text, and after it what followed the space. (A prompt
|
|
85
|
+
* of `generate` is text, whatever it begins with.)
|
|
86
|
+
*/
|
|
87
|
+
skills?: boolean | string[];
|
|
88
|
+
/**
|
|
89
|
+
* The prompt templates the call has: `true` for all that pi finds for the
|
|
90
|
+
* user and for the project (its `.pi/prompts`; of two by one name, the
|
|
91
|
+
* project's) — or the ones named. A prompt that begins with "/name" is
|
|
92
|
+
* then the template of that name, with what follows as its arguments:
|
|
93
|
+
* split as a shell splits words — quotes taken off, a line break or a run
|
|
94
|
+
* of spaces become one space. A name pi has no template by is an error.
|
|
95
|
+
* (For `instruct` only: what tells the model how a typed answer is to be
|
|
96
|
+
* written follows the prompt, and a template is free to leave that out.)
|
|
97
|
+
*/
|
|
98
|
+
templates?: boolean | string[];
|
|
99
|
+
/**
|
|
100
|
+
* Whether the agent has pi's extensions: `true` for the user's — those of
|
|
101
|
+
* the user's pi directory and settings, and the ones pi has built in, its
|
|
102
|
+
* MCP servers among them — or "project" for the project's own as well (its
|
|
103
|
+
* `.pi/extensions`, the MCP servers it names). With them come the tools
|
|
104
|
+
* they add, the commands a prompt of `instruct` can then begin with
|
|
105
|
+
* ("/name"), and what they do to a session, the skills and templates they
|
|
106
|
+
* bring among it. One pi has something against is an error. A call with
|
|
107
|
+
* extensions is served in a process of its own: what an extension gets
|
|
108
|
+
* wrong ends that call, and nothing else of the run. Without any of
|
|
109
|
+
* `skills`, `templates` and `extensions`, a prompt is the text it is,
|
|
110
|
+
* whatever it begins with.
|
|
111
|
+
*/
|
|
112
|
+
extensions?: boolean | "project";
|
|
113
|
+
/**
|
|
114
|
+
* What to make of an answer that was cut off at the model's output limit.
|
|
115
|
+
* "fail", as it is without this: the call fails — the answer is not whole.
|
|
116
|
+
* "keep": the call returns what there was of it, as if it were the answer
|
|
117
|
+
* (and still fails where there was none: a reply cut off before its first
|
|
118
|
+
* word) — for a call whose work is what the agent did with its tools, and
|
|
119
|
+
* whose closing words matter little. (The agent goes no further than the
|
|
120
|
+
* reply that was cut off: where that was leading up to more work, the work
|
|
121
|
+
* was not done. A typed answer that was cut off is seldom whole.)
|
|
122
|
+
*/
|
|
123
|
+
cutOff?: "fail" | "keep";
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The shape of a typed answer, field by field. A field is "string", "number"
|
|
127
|
+
* or "boolean" — what follows a ":" describes it to the model — or an object
|
|
128
|
+
* of such fields, or a list, written as the shape of its items in brackets:
|
|
129
|
+
*
|
|
130
|
+
* { summary: "string", passed: "boolean: did every test pass",
|
|
131
|
+
* failures: [{ test: "string", line: "number" }], tags: ["string"] }
|
|
132
|
+
*/
|
|
133
|
+
export type Schema = {
|
|
134
|
+
readonly [field: string]: SchemaField;
|
|
135
|
+
};
|
|
136
|
+
/** (A list has one element: the shape of every item.) */
|
|
137
|
+
export type SchemaField = string | Schema | readonly SchemaField[];
|
|
52
138
|
export interface Agent {
|
|
53
139
|
/**
|
|
54
140
|
* Instruct the pi-coding-agent to perform a task. Returns plain text.
|
|
55
|
-
* The underlying agent may autonomously use its
|
|
56
|
-
*
|
|
141
|
+
* The underlying agent may autonomously use its tools to fulfill the
|
|
142
|
+
* instruction.
|
|
57
143
|
*/
|
|
58
|
-
instruct(prompt: string, options?:
|
|
59
|
-
system?: string;
|
|
60
|
-
}): Promise<string>;
|
|
144
|
+
instruct(prompt: string, options?: AgentOptions): Promise<string>;
|
|
61
145
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
146
|
+
* The same, with the answer as a typed object matching the schema: the
|
|
147
|
+
* agent may use its tools to find out what it is asked, and answers in
|
|
148
|
+
* that shape: the fields of the schema and no others, each as declared.
|
|
149
|
+
* Asks once more on a parse/validation failure.
|
|
65
150
|
*/
|
|
66
|
-
generate<T>(input: {
|
|
151
|
+
generate<T>(input: Omit<AgentOptions, "templates"> & {
|
|
67
152
|
prompt: string;
|
|
68
|
-
schema:
|
|
69
|
-
system?: string;
|
|
153
|
+
schema: Schema;
|
|
70
154
|
}): Promise<T>;
|
|
71
155
|
}
|
|
72
156
|
export interface ShellResult {
|
|
@@ -79,10 +163,17 @@ export interface Tools {
|
|
|
79
163
|
read(path: string): Promise<string>;
|
|
80
164
|
/** Write a file. Path is relative to project dir. */
|
|
81
165
|
write(path: string, content: string): Promise<void>;
|
|
82
|
-
/**
|
|
166
|
+
/**
|
|
167
|
+
* Execute a shell command. Returns structured result.
|
|
168
|
+
* `env` is variables for the command, over those it has anyway: the
|
|
169
|
+
* runner's, and those the run was started with. (What a program sets in
|
|
170
|
+
* its own process.env is the program's, and is not handed on unless it is
|
|
171
|
+
* given here: `env: process.env` hands on all of it.)
|
|
172
|
+
*/
|
|
83
173
|
shell(command: string, options?: {
|
|
84
174
|
cwd?: string;
|
|
85
175
|
timeout?: number;
|
|
176
|
+
env?: Record<string, string | undefined>;
|
|
86
177
|
}): Promise<ShellResult>;
|
|
87
178
|
/** HTTP fetch. Returns response body as string. */
|
|
88
179
|
fetch(url: string, options?: {
|
package/dist/workspace.d.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
import type { Workspace } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Make sure a workspace directory exists where `workspaceDir` leads, and
|
|
4
|
+
* return its resolved path. Unlike a project, a workspace is nothing but its
|
|
5
|
+
* path and is made as needed — also when `workspaceDir` is a link whose target
|
|
6
|
+
* is not there yet.
|
|
7
|
+
*/
|
|
8
|
+
export declare function ensureWorkspaceDir(workspaceDir: string): string;
|
|
2
9
|
export declare function createWorkspace(workspaceDir: string, signal?: AbortSignal): Workspace;
|
package/dist/workspace.js
CHANGED
|
@@ -1,27 +1,24 @@
|
|
|
1
|
-
import { readFileSync,
|
|
2
|
-
import { resolve,
|
|
3
|
-
import {
|
|
1
|
+
import { readFileSync, mkdirSync, readdirSync, statSync } from "fs";
|
|
2
|
+
import { resolve, relative, sep } from "path";
|
|
3
|
+
import { onAbort } from "./abort.js";
|
|
4
|
+
import { guardPath, realRootOf } from "./path-guard.js";
|
|
5
|
+
import { isStagedName, stageFile } from "./replace-file.js";
|
|
4
6
|
const OBSERVE_POLL_MS = 250;
|
|
5
7
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
8
|
+
* Compile a simple glob pattern for matching relative paths: `*` is any run
|
|
9
|
+
* of characters within a segment, `?` one of them, `**` any run of characters
|
|
10
|
+
* at all, and `**` with its slash any number of whole segments, none included
|
|
11
|
+
* (so `**` + `/*.md` matches at the root too). Every other character is itself.
|
|
12
|
+
*
|
|
13
|
+
* The pattern is taken apart at its operators and each piece between them
|
|
14
|
+
* escaped whole — nothing is put into the pattern on the way, so there is no
|
|
15
|
+
* text a pattern could hold that is mistaken for an operator.
|
|
9
16
|
*/
|
|
10
|
-
function
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
// Escape regex special chars except * and ?
|
|
16
|
-
regex = regex
|
|
17
|
-
.replace(/[.+^${}()|[\]\\]/g, "\\$&")
|
|
18
|
-
.replace(/\*/g, "[^/]*")
|
|
19
|
-
.replace(/\?/g, "[^/]");
|
|
20
|
-
// Restore placeholders
|
|
21
|
-
regex = regex
|
|
22
|
-
.replace(/\x00GLOBSTARSLASH\x00/g, "(?:.+/)?")
|
|
23
|
-
.replace(/\x00GLOBSTAR\x00/g, ".*");
|
|
24
|
-
return new RegExp(`^${regex}$`).test(filePath);
|
|
17
|
+
function compileGlob(pattern) {
|
|
18
|
+
const literal = (text) => text.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
19
|
+
const within = (segment) => segment.split("?").map((part) => part.split("*").map(literal).join("[^/]*")).join("[^/]");
|
|
20
|
+
const regex = pattern.split("**/").map((part) => part.split("**").map(within).join(".*")).join("(?:.+/)?");
|
|
21
|
+
return new RegExp(`^${regex}$`);
|
|
25
22
|
}
|
|
26
23
|
/**
|
|
27
24
|
* A pattern with no glob operators names exactly one artifact.
|
|
@@ -31,81 +28,89 @@ function isExactPath(pattern) {
|
|
|
31
28
|
return !/[*?]/.test(pattern);
|
|
32
29
|
}
|
|
33
30
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
31
|
+
* List the files under a directory of a workspace, as forward-slash paths
|
|
32
|
+
* relative to `root`, into `found`. Links to files are included, and links
|
|
33
|
+
* to directories followed, but only those the guard lets through: what is
|
|
34
|
+
* listed is what can be read.
|
|
37
35
|
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
36
|
+
* A link to a directory is an alias: what is in that directory is listed
|
|
37
|
+
* under the link's name too. It is followed where it really is — in a
|
|
38
|
+
* directory reached by no link — and not again where it is come upon by way
|
|
39
|
+
* of another link (`byLink`). So every alias of a shared directory is listed,
|
|
40
|
+
* a loop is gone round once, and the work is bounded by the links there are
|
|
41
|
+
* times what they lead to; following links found through links, it is
|
|
42
|
+
* bounded by nothing but the length of a path. What this leaves unlisted,
|
|
43
|
+
* though it can be read: what lies beyond a second link to a directory. (A
|
|
44
|
+
* link to a file costs nothing to list, wherever it is come upon.)
|
|
43
45
|
*/
|
|
44
|
-
function
|
|
45
|
-
const results = [];
|
|
46
|
+
function listFiles(dir, root, found, byLink = false) {
|
|
46
47
|
let entries;
|
|
47
48
|
try {
|
|
48
49
|
entries = readdirSync(dir, { withFileTypes: true });
|
|
49
50
|
}
|
|
50
51
|
catch {
|
|
51
|
-
return
|
|
52
|
+
return;
|
|
52
53
|
}
|
|
53
|
-
|
|
54
|
+
// A temp file left by a write that was cut off is not an artifact.
|
|
55
|
+
const addFile = (full) => { if (!isStagedName(full))
|
|
56
|
+
found.push(relative(root, full).split(sep).join("/")); };
|
|
54
57
|
for (const entry of entries) {
|
|
55
58
|
const full = resolve(dir, entry.name);
|
|
56
59
|
if (entry.isSymbolicLink()) {
|
|
57
|
-
let
|
|
60
|
+
let stat;
|
|
58
61
|
try {
|
|
59
|
-
|
|
62
|
+
guardPath(root, full); // leads out of the workspace, or nowhere: not listed
|
|
63
|
+
stat = statSync(full);
|
|
60
64
|
}
|
|
61
65
|
catch {
|
|
62
|
-
continue;
|
|
66
|
+
continue;
|
|
63
67
|
}
|
|
64
|
-
if (
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (stat.isDirectory()) {
|
|
69
|
-
if (visited.has(real))
|
|
70
|
-
continue;
|
|
71
|
-
visited.add(real);
|
|
72
|
-
results.push(...listFilesRecursive(full, root, realRoot, visited));
|
|
73
|
-
}
|
|
74
|
-
else if (stat.isFile()) {
|
|
75
|
-
addFile(full);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
catch { /* stat failed */ }
|
|
68
|
+
if (stat.isFile())
|
|
69
|
+
addFile(full);
|
|
70
|
+
else if (stat.isDirectory() && !byLink)
|
|
71
|
+
listFiles(full, root, found, true);
|
|
79
72
|
}
|
|
80
73
|
else if (entry.isDirectory()) {
|
|
81
|
-
|
|
74
|
+
listFiles(full, root, found, byLink);
|
|
82
75
|
}
|
|
83
76
|
else if (entry.isFile()) {
|
|
84
77
|
addFile(full);
|
|
85
78
|
}
|
|
86
79
|
}
|
|
87
|
-
return results;
|
|
88
80
|
}
|
|
89
81
|
/** Resolve after `ms`, or reject as soon as `signal` aborts. */
|
|
90
82
|
function sleep(ms, signal) {
|
|
91
83
|
return new Promise((resolve, reject) => {
|
|
92
|
-
const onAbort = () => {
|
|
93
|
-
clearTimeout(timer);
|
|
94
|
-
reject(new Error("workspace.observe() aborted"));
|
|
95
|
-
};
|
|
96
84
|
const timer = setTimeout(() => {
|
|
97
|
-
|
|
85
|
+
offAbort();
|
|
98
86
|
resolve();
|
|
99
87
|
}, ms);
|
|
100
|
-
|
|
88
|
+
const offAbort = onAbort(signal, () => {
|
|
89
|
+
clearTimeout(timer);
|
|
90
|
+
reject(new Error("workspace.observe() aborted"));
|
|
91
|
+
});
|
|
101
92
|
});
|
|
102
93
|
}
|
|
103
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Make sure a workspace directory exists where `workspaceDir` leads, and
|
|
96
|
+
* return its resolved path. Unlike a project, a workspace is nothing but its
|
|
97
|
+
* path and is made as needed — also when `workspaceDir` is a link whose target
|
|
98
|
+
* is not there yet.
|
|
99
|
+
*/
|
|
100
|
+
export function ensureWorkspaceDir(workspaceDir) {
|
|
104
101
|
const root = resolve(workspaceDir);
|
|
105
|
-
mkdirSync(root, { recursive: true });
|
|
106
|
-
|
|
102
|
+
mkdirSync(realRootOf(root), { recursive: true });
|
|
103
|
+
return root;
|
|
104
|
+
}
|
|
105
|
+
export function createWorkspace(workspaceDir, signal) {
|
|
106
|
+
const root = ensureWorkspaceDir(workspaceDir);
|
|
107
107
|
/** All artifact paths matching a glob, sorted. */
|
|
108
|
-
const listMatching = (pattern) =>
|
|
108
|
+
const listMatching = (pattern) => {
|
|
109
|
+
const re = compileGlob(pattern);
|
|
110
|
+
const found = [];
|
|
111
|
+
listFiles(root, root, found);
|
|
112
|
+
return found.filter(f => re.test(f)).sort();
|
|
113
|
+
};
|
|
109
114
|
/** Read an artifact, or null if it is missing, unreadable, or escapes the workspace. */
|
|
110
115
|
const readIfPresent = (path) => {
|
|
111
116
|
try {
|
|
@@ -128,42 +133,9 @@ export function createWorkspace(workspaceDir, signal) {
|
|
|
128
133
|
}
|
|
129
134
|
},
|
|
130
135
|
async write(path, content) {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
// replacing the link (matches tools.write). guardPath has already
|
|
135
|
-
// verified the destination stays inside the workspace.
|
|
136
|
-
let writePath = target;
|
|
137
|
-
try {
|
|
138
|
-
if (lstatSync(target).isSymbolicLink()) {
|
|
139
|
-
try {
|
|
140
|
-
writePath = realpathSync(target);
|
|
141
|
-
}
|
|
142
|
-
catch (e) {
|
|
143
|
-
if (e.code !== "ENOENT")
|
|
144
|
-
throw e;
|
|
145
|
-
// Dangling symlink — resolve the link destination manually
|
|
146
|
-
writePath = resolve(dirname(target), readlinkSync(target));
|
|
147
|
-
mkdirSync(dirname(writePath), { recursive: true });
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
catch (e) {
|
|
152
|
-
if (e.code !== "ENOENT")
|
|
153
|
-
throw e;
|
|
154
|
-
}
|
|
155
|
-
// Atomic write: temp file in the same directory, fsync, rename.
|
|
156
|
-
// Readers see either the old content or the new — never a partial file.
|
|
157
|
-
const tmpFile = writePath + `.trama-tmp-${process.pid}-${Date.now()}`;
|
|
158
|
-
const fd = openSync(tmpFile, "w");
|
|
159
|
-
try {
|
|
160
|
-
writeFileSync(fd, content);
|
|
161
|
-
fdatasyncSync(fd);
|
|
162
|
-
}
|
|
163
|
-
finally {
|
|
164
|
-
closeSync(fd);
|
|
165
|
-
}
|
|
166
|
-
renameSync(tmpFile, writePath);
|
|
136
|
+
// One move, so that readers see either the old content or the new, never
|
|
137
|
+
// a partial file. A link is written through rather than replaced.
|
|
138
|
+
stageFile(root, path, content).commit();
|
|
167
139
|
},
|
|
168
140
|
async list(pattern) {
|
|
169
141
|
return listMatching(pattern);
|
|
@@ -177,10 +149,24 @@ export function createWorkspace(workspaceDir, signal) {
|
|
|
177
149
|
throw new Error(`workspace.observe(): timeout must be a non-negative number (got ${timeout})`);
|
|
178
150
|
}
|
|
179
151
|
const needed = expect ?? 1;
|
|
152
|
+
const exact = isExactPath(pattern);
|
|
180
153
|
// One exact path, one artifact wanted → the caller gets its content as a string.
|
|
181
|
-
const single =
|
|
182
|
-
|
|
183
|
-
|
|
154
|
+
const single = exact && needed === 1;
|
|
155
|
+
// A path that will never be read is refused now, rather than waited for: one that leads out of the
|
|
156
|
+
// workspace, or cannot be followed as things stand, whatever is asked of it. (What comes to be in its way
|
|
157
|
+
// later, while it waits — a link, a file where a directory is to be — is waited out like anything that is
|
|
158
|
+
// not there yet.)
|
|
159
|
+
if (exact)
|
|
160
|
+
guardPath(root, pattern);
|
|
161
|
+
// And one path, wanted as one text, is none while a directory is there: that is said as soon as it is seen.
|
|
162
|
+
const isDirectory = () => {
|
|
163
|
+
try {
|
|
164
|
+
return statSync(guardPath(root, pattern)).isDirectory();
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
184
170
|
const collectMatches = () => {
|
|
185
171
|
const matches = [];
|
|
186
172
|
for (const path of single ? [pattern] : listMatching(pattern)) {
|
|
@@ -191,14 +177,17 @@ export function createWorkspace(workspaceDir, signal) {
|
|
|
191
177
|
return matches;
|
|
192
178
|
};
|
|
193
179
|
// Level-triggered: artifacts that already exist count immediately.
|
|
194
|
-
const startTime =
|
|
180
|
+
const startTime = performance.now();
|
|
195
181
|
while (true) {
|
|
196
182
|
if (signal?.aborted)
|
|
197
183
|
throw new Error("workspace.observe() aborted");
|
|
184
|
+
if (single && isDirectory()) {
|
|
185
|
+
throw Object.assign(new Error(`EISDIR: illegal operation on a directory, read '${pattern}'`), { code: "EISDIR" });
|
|
186
|
+
}
|
|
198
187
|
const matches = collectMatches();
|
|
199
188
|
if (matches.length >= needed)
|
|
200
189
|
return single ? matches[0].content : matches;
|
|
201
|
-
if (timeout !== undefined && (
|
|
190
|
+
if (timeout !== undefined && (performance.now() - startTime) >= timeout) {
|
|
202
191
|
throw new Error(`workspace.observe("${pattern}") timed out after ${timeout}ms. ` +
|
|
203
192
|
`Found ${matches.length}/${needed} matching artifacts.`);
|
|
204
193
|
}
|