@niwa-labs/workflow 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +13 -2
- package/package.json +55 -4
- package/src/actions.ts +408 -0
- package/src/agent.ts +211 -0
- package/src/ask.ts +84 -0
- package/src/bridge.ts +710 -0
- package/src/catalogue.ts +74 -0
- package/src/check.ts +880 -0
- package/src/child.ts +1378 -0
- package/src/cockpit/actions.ts +128 -0
- package/src/cockpit/catalogue-actions.ts +97 -0
- package/src/cockpit/dashboard.tsx +134 -0
- package/src/cockpit/groups.ts +45 -0
- package/src/cockpit/index.tsx +50 -0
- package/src/cockpit/launch.tsx +136 -0
- package/src/cockpit/plan.ts +101 -0
- package/src/cockpit/run-commands.tsx +102 -0
- package/src/cockpit/run-page.tsx +140 -0
- package/src/cockpit/run-summary.ts +16 -0
- package/src/cockpit/runs.tsx +73 -0
- package/src/cockpit/statistics.tsx +176 -0
- package/src/cockpit/track.tsx +159 -0
- package/src/cockpit/workflows.tsx +383 -0
- package/src/describe-cache.ts +122 -0
- package/src/describe.ts +174 -0
- package/src/guide.ts +213 -0
- package/src/index.ts +2813 -0
- package/src/inspect.ts +235 -0
- package/src/installation.ts +90 -0
- package/src/language.ts +217 -0
- package/src/records-entry.ts +63 -0
- package/src/records.ts +574 -0
- package/src/registry.ts +250 -0
- package/src/revise.ts +89 -0
- package/src/runner.ts +339 -0
- package/src/script-types.ts +549 -0
- package/src/stats.ts +460 -0
- package/src/status.ts +512 -0
- package/src/store.ts +259 -0
- package/src/subscriptions.ts +102 -0
- package/src/waiting.ts +61 -0
- package/test/cockpit-actions.test.ts +20 -0
- package/test/cockpit-groups.test.ts +42 -0
- package/test/cockpit-plan.test.ts +118 -0
- package/test/fixtures/branches-host.ts +58 -0
- package/test/fixtures/branches-script.js +23 -0
- package/test/fixtures/bridge-host.ts +23 -0
- package/test/fixtures/bridge-keys-script.js +15 -0
- package/test/fixtures/bridge-wait-script.js +8 -0
- package/test/fixtures/cancel-host.ts +58 -0
- package/test/fixtures/cancel-script.js +12 -0
- package/test/fixtures/crash-host.ts +61 -0
- package/test/fixtures/crash-script.js +14 -0
- package/test/fixtures/ping-records.ts +20 -0
- package/test/fixtures/revision-host.ts +57 -0
- package/test/fixtures/revision-script.js +11 -0
- package/test/fixtures/running-host.ts +61 -0
- package/test/fixtures/running-script.js +12 -0
- package/test/fixtures/with-data/flow.ts +2 -0
- package/test/fixtures/with-data/table.json +1 -0
- package/test/git-repo.ts +45 -0
- package/test/git-worktree.test.ts +413 -0
- package/test/question-format.test.ts +279 -0
- package/test/workflow-agent-caps.test.ts +91 -0
- package/test/workflow-agent-lifetime.test.ts +93 -0
- package/test/workflow-agent-options.test.ts +216 -0
- package/test/workflow-agent-schema.test.ts +386 -0
- package/test/workflow-agent-typings.test.ts +241 -0
- package/test/workflow-ask-resume.test.ts +145 -0
- package/test/workflow-branch-keys.test.ts +133 -0
- package/test/workflow-bridge-inproc.test.ts +230 -0
- package/test/workflow-bridge.test.ts +141 -0
- package/test/workflow-builtin-candidate.test.ts +230 -0
- package/test/workflow-builtin.test.ts +197 -0
- package/test/workflow-cancel-agent.test.ts +144 -0
- package/test/workflow-cancel.test.ts +166 -0
- package/test/workflow-chain.test.ts +84 -0
- package/test/workflow-check-options.test.ts +144 -0
- package/test/workflow-check-uses.test.ts +148 -0
- package/test/workflow-check.test.ts +282 -0
- package/test/workflow-contract.test.ts +295 -0
- package/test/workflow-crash-resume.test.ts +141 -0
- package/test/workflow-deadline.test.ts +102 -0
- package/test/workflow-describe-cache.test.ts +240 -0
- package/test/workflow-directory.test.ts +73 -0
- package/test/workflow-emits.test.ts +261 -0
- package/test/workflow-group-keys.test.ts +72 -0
- package/test/workflow-harnesses.test.ts +262 -0
- package/test/workflow-inspect.test.ts +152 -0
- package/test/workflow-language-types.test.ts +148 -0
- package/test/workflow-manifest.test.ts +93 -0
- package/test/workflow-manual-pause.test.ts +116 -0
- package/test/workflow-owners.test.ts +154 -0
- package/test/workflow-pause.test.ts +200 -0
- package/test/workflow-phases.test.ts +445 -0
- package/test/workflow-plugin-entries.test.ts +59 -0
- package/test/workflow-record-refs.test.ts +310 -0
- package/test/workflow-replay.test.ts +206 -0
- package/test/workflow-resume-once.test.ts +188 -0
- package/test/workflow-revision.test.ts +216 -0
- package/test/workflow-scope.test.ts +505 -0
- package/test/workflow-script-defaults.test.ts +119 -0
- package/test/workflow-script-error.test.ts +228 -0
- package/test/workflow-script-libraries.test.ts +88 -0
- package/test/workflow-script-packages.test.ts +119 -0
- package/test/workflow-stdout.test.ts +89 -0
- package/test/workflow-stop.test.ts +299 -0
- package/test/workflow-store.test.ts +220 -0
- package/test/workflow-visible.test.ts +136 -0
- package/test/workflow-wait-version.test.ts +54 -0
- package/test/workflow-wait.test.ts +224 -0
- package/test/workflow-waiting-agent.test.ts +64 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// A script error pauses the run with a request, and the run waits on it.
|
|
2
|
+
//
|
|
3
|
+
// A failure pauses the run with a request. A script that throws (and no step failed) pauses its
|
|
4
|
+
// invocation too, so a run said to be paused always has something on the owner's list to answer.
|
|
5
|
+
//
|
|
6
|
+
// The scenario runs against a real host process (test/fixtures/revision-host.ts).
|
|
7
|
+
|
|
8
|
+
import { expect, test } from "bun:test";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
|
|
11
|
+
import type { ActionName, StoredRecord } from "@niwa-labs/core";
|
|
12
|
+
import { REQUEST_OPEN } from "@niwa-labs/requests/records";
|
|
13
|
+
import { connect, eventually, spawnHost, tempHome, watch, connectAsOwner } from "@niwa-labs/testing";
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/revision-host.ts", import.meta.url));
|
|
18
|
+
const RESOLVE = "niwa.invocation.resolve" as ActionName;
|
|
19
|
+
|
|
20
|
+
const RunOutput = z.object({ run: z.string(), finished: z.boolean() });
|
|
21
|
+
const Listed = z.object({ requests: z.array(z.record(z.string(), z.unknown())) });
|
|
22
|
+
const State = z.object({ invocation: z.string(), state: z.string(), choices: z.array(z.string()).optional() });
|
|
23
|
+
|
|
24
|
+
function states(seen: StoredRecord[]): Array<z.infer<typeof State>> {
|
|
25
|
+
return seen.flatMap((record) => {
|
|
26
|
+
if (record.type !== "niwa.invocation.state") return [];
|
|
27
|
+
const parsed = State.safeParse(record.data);
|
|
28
|
+
return parsed.success ? [parsed.data] : [];
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
test("a script error pauses the invocation with a request, and a retry goes on", async () => {
|
|
33
|
+
const home = await tempHome();
|
|
34
|
+
const homePath = home.path;
|
|
35
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
36
|
+
let observer: Awaited<ReturnType<typeof watch>> | undefined;
|
|
37
|
+
try {
|
|
38
|
+
host = spawnHost(HOST_SCRIPT, homePath);
|
|
39
|
+
await host.ready();
|
|
40
|
+
const client = await connectAsOwner(homePath);
|
|
41
|
+
observer = await watch(homePath);
|
|
42
|
+
const seen = observer.seen;
|
|
43
|
+
|
|
44
|
+
// The script makes a call that finishes, then throws unless the flag file exists.
|
|
45
|
+
const flag = `${homePath}/fixed`;
|
|
46
|
+
const script = `${homePath}/error-script.js`;
|
|
47
|
+
await Bun.write(
|
|
48
|
+
script,
|
|
49
|
+
[
|
|
50
|
+
'export default workflow({ uses: ["revision.agent"], output: z.json() }, async () => {',
|
|
51
|
+
' const first = await ask({ label: "work:first" }, "Study the core.");',
|
|
52
|
+
` if (!(await Bun.file(${JSON.stringify(flag)}).exists())) throw new Error("the script is broken");`,
|
|
53
|
+
" return first;",
|
|
54
|
+
'});',
|
|
55
|
+
"",
|
|
56
|
+
].join("\n"),
|
|
57
|
+
);
|
|
58
|
+
const started = RunOutput.parse(await client.call("workflow.start" as never, { file: script } as never));
|
|
59
|
+
|
|
60
|
+
await eventually(() => states(seen).some((s) => s.state === "paused"), 20_000, "the invocation should pause");
|
|
61
|
+
const pause = states(seen).find((s) => s.state === "paused");
|
|
62
|
+
if (pause === undefined) throw new Error("no pause");
|
|
63
|
+
// A failure of the script itself is on no step: there is no step to skip.
|
|
64
|
+
expect(pause.choices).toEqual(["retry", "cancel"]);
|
|
65
|
+
|
|
66
|
+
// The run waits on a request that the owner sees.
|
|
67
|
+
const open = Listed.parse(await client.call(REQUEST_OPEN, {})).requests.filter((entry) => entry.kind === "paused");
|
|
68
|
+
expect(open).toHaveLength(1);
|
|
69
|
+
expect(open[0]?.invocation).toBe(pause.invocation);
|
|
70
|
+
expect(open[0]?.choices).toEqual(["retry", "cancel"]);
|
|
71
|
+
// The invocation does not end as done after the pause.
|
|
72
|
+
expect(states(seen).filter((s) => s.invocation === pause.invocation).at(-1)?.state).toBe("paused");
|
|
73
|
+
expect(seen.filter((r) => r.type === "workflow.run.finished")).toHaveLength(0);
|
|
74
|
+
|
|
75
|
+
// The script is fixed and the owner retries: the run finishes.
|
|
76
|
+
await Bun.write(flag, "1");
|
|
77
|
+
await client.call(RESOLVE, { invocation: pause.invocation, choice: "retry" });
|
|
78
|
+
await eventually(
|
|
79
|
+
() => seen.some((r) => r.type === "workflow.run.finished"),
|
|
80
|
+
30_000,
|
|
81
|
+
"the run should finish after the retry",
|
|
82
|
+
);
|
|
83
|
+
const finished = seen.find((r) => r.type === "workflow.run.finished");
|
|
84
|
+
expect((finished?.data as { run?: string }).run).toBe(started.run);
|
|
85
|
+
expect((finished?.data as { outcome?: { result?: unknown } }).outcome?.result).toEqual({ answer: "answered: Study the core." });
|
|
86
|
+
await eventually(
|
|
87
|
+
() => states(seen).filter((s) => s.invocation === pause.invocation).at(-1)?.state === "done",
|
|
88
|
+
20_000,
|
|
89
|
+
"the invocation should end as done",
|
|
90
|
+
);
|
|
91
|
+
expect(Listed.parse(await client.call(REQUEST_OPEN, {})).requests.filter((e) => e.kind === "paused")).toHaveLength(0);
|
|
92
|
+
await client.close().catch(() => {});
|
|
93
|
+
} finally {
|
|
94
|
+
await observer?.stop();
|
|
95
|
+
await host?.kill().catch(() => {});
|
|
96
|
+
await home.cleanup();
|
|
97
|
+
}
|
|
98
|
+
}, 60_000);
|
|
99
|
+
|
|
100
|
+
test("a resume of a run paused by a script error resolves the pause and starts one run", async () => {
|
|
101
|
+
const home = await tempHome();
|
|
102
|
+
const homePath = home.path;
|
|
103
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
104
|
+
let observer: Awaited<ReturnType<typeof watch>> | undefined;
|
|
105
|
+
try {
|
|
106
|
+
host = spawnHost(HOST_SCRIPT, homePath);
|
|
107
|
+
await host.ready();
|
|
108
|
+
const client = await connectAsOwner(homePath);
|
|
109
|
+
observer = await watch(homePath);
|
|
110
|
+
const seen = observer.seen;
|
|
111
|
+
const flag = `${homePath}/fixed`;
|
|
112
|
+
const script = `${homePath}/error-script.js`;
|
|
113
|
+
await Bun.write(
|
|
114
|
+
script,
|
|
115
|
+
[
|
|
116
|
+
'export default workflow({ uses: ["revision.agent"], output: z.json() }, async () => {',
|
|
117
|
+
` if (!(await Bun.file(${JSON.stringify(flag)}).exists())) throw new Error("the script is broken");`,
|
|
118
|
+
' return await ask({ label: "work:first" }, "Study the core.");',
|
|
119
|
+
'});',
|
|
120
|
+
"",
|
|
121
|
+
].join("\n"),
|
|
122
|
+
);
|
|
123
|
+
const started = RunOutput.parse(await client.call("workflow.start" as never, { file: script } as never));
|
|
124
|
+
await eventually(() => states(seen).some((s) => s.state === "paused"), 20_000, "the invocation should pause");
|
|
125
|
+
await Bun.write(flag, "1");
|
|
126
|
+
await client.call("workflow.resume" as never, { run: started.run } as never);
|
|
127
|
+
await eventually(() => seen.some((r) => r.type === "workflow.run.finished"), 30_000, "the run should finish");
|
|
128
|
+
expect(seen.filter((r) => r.type === "workflow.run.finished")).toHaveLength(1);
|
|
129
|
+
expect(Listed.parse(await client.call(REQUEST_OPEN, {})).requests.filter((e) => e.kind === "paused")).toHaveLength(0);
|
|
130
|
+
await client.close().catch(() => {});
|
|
131
|
+
} finally {
|
|
132
|
+
await observer?.stop();
|
|
133
|
+
await host?.kill().catch(() => {});
|
|
134
|
+
await home.cleanup();
|
|
135
|
+
}
|
|
136
|
+
}, 60_000);
|
|
137
|
+
|
|
138
|
+
async function brokenRun(homePath: string, client: Awaited<ReturnType<typeof connect>>) {
|
|
139
|
+
const script = `${homePath}/broken.js`;
|
|
140
|
+
await Bun.write(
|
|
141
|
+
script,
|
|
142
|
+
[
|
|
143
|
+
'export default workflow({ uses: ["revision.agent"] }, async () => {',
|
|
144
|
+
' await ask({ label: "work:first" }, "Study the core.");',
|
|
145
|
+
' throw new Error("the script is broken");',
|
|
146
|
+
'});',
|
|
147
|
+
"",
|
|
148
|
+
].join("\n"),
|
|
149
|
+
);
|
|
150
|
+
return RunOutput.parse(await client.call("workflow.start" as never, { file: script } as never));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
test("a cancel of the pause ends the invocation, and a retry that fails again does not leave a second pause request", async () => {
|
|
154
|
+
const home = await tempHome();
|
|
155
|
+
const homePath = home.path;
|
|
156
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
157
|
+
let observer: Awaited<ReturnType<typeof watch>> | undefined;
|
|
158
|
+
try {
|
|
159
|
+
host = spawnHost(HOST_SCRIPT, homePath);
|
|
160
|
+
await host.ready();
|
|
161
|
+
const client = await connectAsOwner(homePath);
|
|
162
|
+
observer = await watch(homePath);
|
|
163
|
+
const seen = observer.seen;
|
|
164
|
+
await brokenRun(homePath, client);
|
|
165
|
+
await eventually(() => states(seen).some((s) => s.state === "paused"), 20_000, "the invocation should pause");
|
|
166
|
+
const pause = states(seen).find((s) => s.state === "paused");
|
|
167
|
+
if (pause === undefined) throw new Error("no pause");
|
|
168
|
+
// A skip of a failure of the script itself is not offered: there is no step to skip.
|
|
169
|
+
await expect(client.call(RESOLVE, { invocation: pause.invocation, choice: "skip" })).rejects.toThrow("does not offer skip");
|
|
170
|
+
// A retry reruns it and it pauses again: one request, not two.
|
|
171
|
+
await client.call(RESOLVE, { invocation: pause.invocation, choice: "retry" });
|
|
172
|
+
await eventually(
|
|
173
|
+
() => states(seen).filter((s) => s.invocation === pause.invocation && s.state === "paused").length >= 2,
|
|
174
|
+
20_000,
|
|
175
|
+
"the retry reruns the script, which pauses again",
|
|
176
|
+
);
|
|
177
|
+
const open = Listed.parse(await client.call(REQUEST_OPEN, {})).requests.filter((e) => e.kind === "paused");
|
|
178
|
+
expect(open).toHaveLength(1);
|
|
179
|
+
// RunPaused is not written twice for the same error.
|
|
180
|
+
expect(seen.filter((r) => r.type === "workflow.run.paused")).toHaveLength(1);
|
|
181
|
+
await client.call(RESOLVE, { invocation: pause.invocation, choice: "cancel" });
|
|
182
|
+
await eventually(
|
|
183
|
+
() => states(seen).filter((s) => s.invocation === pause.invocation).at(-1)?.state === "done",
|
|
184
|
+
20_000,
|
|
185
|
+
"the cancel should end the invocation",
|
|
186
|
+
);
|
|
187
|
+
expect(Listed.parse(await client.call(REQUEST_OPEN, {})).requests.filter((e) => e.kind === "paused")).toHaveLength(0);
|
|
188
|
+
await client.close().catch(() => {});
|
|
189
|
+
} finally {
|
|
190
|
+
await observer?.stop();
|
|
191
|
+
await host?.kill().catch(() => {});
|
|
192
|
+
await home.cleanup();
|
|
193
|
+
}
|
|
194
|
+
}, 60_000);
|
|
195
|
+
|
|
196
|
+
test("a restart while paused keeps one pause and one request, and no second call", async () => {
|
|
197
|
+
const home = await tempHome();
|
|
198
|
+
const homePath = home.path;
|
|
199
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
200
|
+
let observer: Awaited<ReturnType<typeof watch>> | undefined;
|
|
201
|
+
try {
|
|
202
|
+
host = spawnHost(HOST_SCRIPT, homePath);
|
|
203
|
+
await host.ready();
|
|
204
|
+
let client = await connectAsOwner(homePath);
|
|
205
|
+
observer = await watch(homePath);
|
|
206
|
+
const seen = observer.seen;
|
|
207
|
+
await brokenRun(homePath, client);
|
|
208
|
+
await eventually(() => states(seen).some((s) => s.state === "paused"), 20_000, "the invocation should pause");
|
|
209
|
+
const pause = states(seen).find((s) => s.state === "paused");
|
|
210
|
+
if (pause === undefined) throw new Error("no pause");
|
|
211
|
+
await client.close().catch(() => {});
|
|
212
|
+
await host.kill();
|
|
213
|
+
host = spawnHost(HOST_SCRIPT, homePath);
|
|
214
|
+
await host.ready();
|
|
215
|
+
client = await connectAsOwner(homePath);
|
|
216
|
+
const open = Listed.parse(await client.call(REQUEST_OPEN, {})).requests.filter((e) => e.kind === "paused");
|
|
217
|
+
expect(open).toHaveLength(1);
|
|
218
|
+
expect(open[0]?.invocation).toBe(pause.invocation);
|
|
219
|
+
await new Promise((resolve) => setTimeout(resolve, 1500));
|
|
220
|
+
expect(seen.filter((r) => r.type === "workflow.run.paused")).toHaveLength(1);
|
|
221
|
+
expect(seen.filter((r) => r.type === "workflow.run.finished")).toHaveLength(0);
|
|
222
|
+
await client.close().catch(() => {});
|
|
223
|
+
} finally {
|
|
224
|
+
await observer?.stop();
|
|
225
|
+
await host?.kill().catch(() => {});
|
|
226
|
+
await home.cleanup();
|
|
227
|
+
}
|
|
228
|
+
}, 60_000);
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// The script libraries ship with the workflow module: a script that imports `@niwa-labs/flow`,
|
|
2
|
+
// `@niwa-labs/flow-code` or `@niwa-labs/flow-eval` by name is checked, stored and run like one
|
|
3
|
+
// that imports zod. The copy of a registered version finds them through the link to the packages
|
|
4
|
+
// niwa runs with, its manifest names them with their versions, and a script process in a box may
|
|
5
|
+
// read them.
|
|
6
|
+
|
|
7
|
+
import { afterEach, expect, test } from "bun:test";
|
|
8
|
+
import { mkdtempSync, readFileSync, realpathSync } from "node:fs";
|
|
9
|
+
import { mkdir, rm, symlink, writeFile } from "node:fs/promises";
|
|
10
|
+
import { homedir } from "node:os";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
|
|
13
|
+
import { nono } from "@niwa-labs/sandbox";
|
|
14
|
+
import { createNiwa, live, requests, tempHome, timers } from "@niwa-labs/testing";
|
|
15
|
+
import type { Host } from "@niwa-labs/testing";
|
|
16
|
+
|
|
17
|
+
import { readableRoots } from "../src/installation.ts";
|
|
18
|
+
import { workflow, workflowRegister, workflowStart, workflowWait } from "../src/index.ts";
|
|
19
|
+
|
|
20
|
+
const LIBRARIES = ["flow", "flow-code", "flow-eval"] as const;
|
|
21
|
+
|
|
22
|
+
const homes: Array<{ cleanup(): Promise<void> }> = [];
|
|
23
|
+
const hosts: Host[] = [];
|
|
24
|
+
|
|
25
|
+
afterEach(async () => {
|
|
26
|
+
for (const host of hosts.splice(0)) await host.close().catch(() => {});
|
|
27
|
+
for (const home of homes.splice(0)) await home.cleanup();
|
|
28
|
+
}, 60_000);
|
|
29
|
+
|
|
30
|
+
const SCRIPT = [
|
|
31
|
+
'import { map } from "@niwa-labs/flow";',
|
|
32
|
+
'import { selfTestVerdict } from "@niwa-labs/flow-code";',
|
|
33
|
+
'import { blindSet } from "@niwa-labs/flow-eval";',
|
|
34
|
+
'export default workflow({ description: "Use each flow library", output: z.json() }, async (): Promise<any> => {',
|
|
35
|
+
' const doubled = await map("double", [1, 2, 3], { key: (n) => "n" + String(n) }, async (n) => n * 2);',
|
|
36
|
+
' const blind = await blindSet("blind", { one: 1, two: 2 });',
|
|
37
|
+
" return {",
|
|
38
|
+
" doubled: doubled.map((one) => (one.ok ? one.value : null)),",
|
|
39
|
+
' verdict: selfTestVerdict([{ status: "pass" }]),',
|
|
40
|
+
" revealed: Object.keys(blind.labeled).map((label) => blind.reveal(label)).sort(),",
|
|
41
|
+
" };",
|
|
42
|
+
"});",
|
|
43
|
+
].join("\n");
|
|
44
|
+
|
|
45
|
+
/** A home under the user's home, removed with the others. */
|
|
46
|
+
function userHome(): { path: string; cleanup(): Promise<void> } {
|
|
47
|
+
const path = mkdtempSync(join(homedir(), ".niwa-flow-home-"));
|
|
48
|
+
return { path, cleanup: () => rm(path, { recursive: true, force: true }) };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Register the script from a folder that finds packages where a script in niwa's tree does, and run it. */
|
|
52
|
+
async function registerAndRun(box: boolean): Promise<{ home: string; id: string; version: string; outcome: unknown }> {
|
|
53
|
+
// nono reads the temp area by default, so it cannot close the host's keys in a home there: a boxed home is under the user's.
|
|
54
|
+
const temp = box ? userHome() : await tempHome();
|
|
55
|
+
homes.push(temp);
|
|
56
|
+
const host = createNiwa({ home: temp.path });
|
|
57
|
+
host.use(requests(), timers([]), workflow(box ? { box: true, sandbox: "nono" } : {}));
|
|
58
|
+
await host.serve();
|
|
59
|
+
hosts.push(host);
|
|
60
|
+
const home = realpathSync(temp.path);
|
|
61
|
+
const src = join(home, "src");
|
|
62
|
+
await mkdir(src, { recursive: true });
|
|
63
|
+
await symlink(realpathSync(join(import.meta.dir, "..", "node_modules")), join(src, "node_modules"), "dir");
|
|
64
|
+
await writeFile(join(src, "libraries.ts"), SCRIPT);
|
|
65
|
+
const { id, version } = await host.call(workflowRegister, { file: join(src, "libraries.ts") });
|
|
66
|
+
const { run } = await host.call(workflowStart, { id });
|
|
67
|
+
const outcome = (await host.call(workflowWait, { run, timeoutMs: 30_000 })).outcome;
|
|
68
|
+
return { home, id, version, outcome };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
test("a stored script imports the script libraries by name, and its manifest names them with their versions", async () => {
|
|
72
|
+
const { home, id, version, outcome } = await registerAndRun(false);
|
|
73
|
+
expect(outcome).toEqual({ status: "done", result: { doubled: [2, 4, 6], verdict: "PASS", revealed: ["one", "two"] } });
|
|
74
|
+
const manifest = JSON.parse(readFileSync(join(home, "workflows", id, `${version}.json`), "utf8")) as { imports: Record<string, string | null> };
|
|
75
|
+
expect(manifest.imports).toEqual({ "@niwa-labs/flow": "0.0.0", "@niwa-labs/flow-code": "0.0.0", "@niwa-labs/flow-eval": "0.0.0" });
|
|
76
|
+
}, 60_000);
|
|
77
|
+
|
|
78
|
+
test("a script process in a box may read the script libraries", () => {
|
|
79
|
+
const roots = readableRoots();
|
|
80
|
+
for (const name of LIBRARIES) expect(roots).toContain(realpathSync(join(import.meta.dir, "..", "..", name)));
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const boxed = live && (await nono().available());
|
|
84
|
+
|
|
85
|
+
test.skipIf(!boxed)("a stored script that imports the script libraries runs in a nono box", async () => {
|
|
86
|
+
const { outcome } = await registerAndRun(true);
|
|
87
|
+
expect(outcome).toEqual({ status: "done", result: { doubled: [2, 4, 6], verdict: "PASS", revealed: ["one", "two"] } });
|
|
88
|
+
}, 60_000);
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// A plugin outside niwa may give scripts a package of its own (`@niwa-labs/forge/flow`): it offers the package to
|
|
2
|
+
// the workflow module as one scripts import, and a stored copy finds it, is checked against it and runs with it as a
|
|
3
|
+
// script in niwa's tree does. Its manifest names it with its version, and a script process in a box may read it. A
|
|
4
|
+
// package nobody offers is not found by a stored copy.
|
|
5
|
+
|
|
6
|
+
import { afterEach, expect, spyOn, test } from "bun:test";
|
|
7
|
+
import { mkdtempSync, readFileSync, realpathSync } from "node:fs";
|
|
8
|
+
import { mkdir, rm, writeFile } from "node:fs/promises";
|
|
9
|
+
import { homedir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
|
|
12
|
+
import { SCRIPT_PACKAGE } from "@niwa-labs/plugin";
|
|
13
|
+
import type { Plugin } from "@niwa-labs/plugin";
|
|
14
|
+
import { nono } from "@niwa-labs/sandbox";
|
|
15
|
+
import { createNiwa, live, plugin, requests, tempDir, tempHome, timers } from "@niwa-labs/testing";
|
|
16
|
+
import type { Host } from "@niwa-labs/testing";
|
|
17
|
+
|
|
18
|
+
import { workflow, workflowCheck, workflowRegister, workflowStart, workflowWait } from "../src/index.ts";
|
|
19
|
+
|
|
20
|
+
const homes: Array<{ cleanup(): Promise<void> }> = [];
|
|
21
|
+
const hosts: Host[] = [];
|
|
22
|
+
|
|
23
|
+
afterEach(async () => {
|
|
24
|
+
for (const host of hosts.splice(0)) await host.close().catch(() => {});
|
|
25
|
+
for (const home of homes.splice(0)) await home.cleanup();
|
|
26
|
+
}, 60_000);
|
|
27
|
+
|
|
28
|
+
const NAME = "@niwa-labs/shelf";
|
|
29
|
+
|
|
30
|
+
/** A folder under the user's home, removed with the homes: nono reads the temp area by default, so a box proves nothing there. */
|
|
31
|
+
function userDir(): { path: string; cleanup(): Promise<void> } {
|
|
32
|
+
const path = mkdtempSync(join(homedir(), ".niwa-script-package-"));
|
|
33
|
+
return { path, cleanup: () => rm(path, { recursive: true, force: true }) };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A package outside niwa's tree, as another repository's plugin would ship it, and a script folder that finds it. */
|
|
37
|
+
async function outside(at?: string): Promise<{ root: string; script: string }> {
|
|
38
|
+
const base = realpathSync(at ?? (await tempDir("script-package")));
|
|
39
|
+
const root = join(base, "src", "node_modules", "@niwa-labs", "shelf");
|
|
40
|
+
await mkdir(root, { recursive: true });
|
|
41
|
+
await writeFile(
|
|
42
|
+
join(root, "package.json"),
|
|
43
|
+
JSON.stringify({ name: NAME, version: "1.4.2", type: "module", exports: { ".": "./index.ts", "./flow": "./flow.ts" } }),
|
|
44
|
+
);
|
|
45
|
+
await writeFile(join(root, "index.ts"), "export const shelf = 'shelf';\n");
|
|
46
|
+
await writeFile(join(root, "flow.ts"), "export function label(n: number): string {\n return `shelf ${String(n)}`;\n}\n");
|
|
47
|
+
const script = join(base, "src", "shelf.ts");
|
|
48
|
+
await mkdir(join(base, "src"), { recursive: true });
|
|
49
|
+
await writeFile(
|
|
50
|
+
script,
|
|
51
|
+
[
|
|
52
|
+
'import { label } from "@niwa-labs/shelf/flow";',
|
|
53
|
+
'export default workflow({ description: "Label with a package from outside niwa", output: z.string() }, async () => label(3));',
|
|
54
|
+
].join("\n"),
|
|
55
|
+
);
|
|
56
|
+
return { root, script };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The plugin that ships the package and offers it to scripts. */
|
|
60
|
+
const shelf = (root: string): Plugin => plugin("shelf", (api) => api.offer(SCRIPT_PACKAGE, { name: NAME, root }));
|
|
61
|
+
|
|
62
|
+
async function served(extra: Plugin[], box = false): Promise<{ host: Host; home: string }> {
|
|
63
|
+
const temp = box ? userDir() : await tempHome();
|
|
64
|
+
homes.push(temp);
|
|
65
|
+
const host = createNiwa({ home: temp.path });
|
|
66
|
+
host.use(requests(), timers([]), ...extra, workflow(box ? { box: true, sandbox: "nono" } : {}));
|
|
67
|
+
await host.serve();
|
|
68
|
+
hosts.push(host);
|
|
69
|
+
return { host, home: realpathSync(temp.path) };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function registerAndRun(host: Host, file: string): Promise<{ id: string; version: string; outcome: unknown }> {
|
|
73
|
+
const { id, version } = await host.call(workflowRegister, { file });
|
|
74
|
+
const { run } = await host.call(workflowStart, { id });
|
|
75
|
+
const outcome = (await host.call(workflowWait, { run, timeoutMs: 30_000 })).outcome;
|
|
76
|
+
return { id, version, outcome };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
test("a stored script imports a package a plugin offers to scripts, and its manifest names it with its version", async () => {
|
|
80
|
+
const { root, script } = await outside();
|
|
81
|
+
const { host, home } = await served([shelf(root)]);
|
|
82
|
+
expect((await host.call(workflowCheck, { file: script })).errors).toEqual([]);
|
|
83
|
+
const { id, version, outcome } = await registerAndRun(host, script);
|
|
84
|
+
expect(outcome).toEqual({ status: "done", result: "shelf 3" });
|
|
85
|
+
const manifest = JSON.parse(readFileSync(join(home, "workflows", id, `${version}.json`), "utf8")) as { imports: Record<string, string | null> };
|
|
86
|
+
expect(manifest.imports).toEqual({ "@niwa-labs/shelf/flow": "1.4.2" });
|
|
87
|
+
}, 60_000);
|
|
88
|
+
|
|
89
|
+
test("a stored copy does not find a package no plugin offers to scripts", async () => {
|
|
90
|
+
const { script } = await outside();
|
|
91
|
+
const { host } = await served([]);
|
|
92
|
+
await expect(registerAndRun(host, script)).rejects.toThrow("Cannot find module '@niwa-labs/shelf/flow'");
|
|
93
|
+
}, 60_000);
|
|
94
|
+
|
|
95
|
+
test("an offer of a package that is not a niwa package at its folder is not given to scripts, and the host says why", async () => {
|
|
96
|
+
const { root, script } = await outside();
|
|
97
|
+
const said: string[] = [];
|
|
98
|
+
const error = spyOn(console, "error").mockImplementation((...args: unknown[]) => void said.push(args.map(String).join(" ")));
|
|
99
|
+
try {
|
|
100
|
+
const wrongName = plugin("wrong", (api) => api.offer(SCRIPT_PACKAGE, { name: "left-pad", root }));
|
|
101
|
+
const wrongRoot = plugin("elsewhere", (api) => api.offer(SCRIPT_PACKAGE, { name: "@niwa-labs/other", root }));
|
|
102
|
+
const { host } = await served([wrongName, wrongRoot]);
|
|
103
|
+
expect(said).toContain('niwa: the package "wrong" offers scripts is not given to them: "left-pad" is not a package of @niwa-labs');
|
|
104
|
+
expect(said).toContain(`niwa: the package "elsewhere" offers scripts is not given to them: ${root} is not the folder of @niwa-labs/other`);
|
|
105
|
+
await expect(registerAndRun(host, script)).rejects.toThrow("Cannot find module '@niwa-labs/shelf/flow'");
|
|
106
|
+
} finally {
|
|
107
|
+
error.mockRestore();
|
|
108
|
+
}
|
|
109
|
+
}, 60_000);
|
|
110
|
+
|
|
111
|
+
const boxed = live && (await nono().available());
|
|
112
|
+
|
|
113
|
+
test.skipIf(!boxed)("a stored script that imports an offered package runs in a nono box", async () => {
|
|
114
|
+
const place = userDir();
|
|
115
|
+
homes.push(place);
|
|
116
|
+
const { root, script } = await outside(place.path);
|
|
117
|
+
const { host } = await served([shelf(root)], true);
|
|
118
|
+
expect((await registerAndRun(host, script)).outcome).toEqual({ status: "done", result: "shelf 3" });
|
|
119
|
+
}, 60_000);
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// A script that writes to the stdout does not break the protocol.
|
|
2
|
+
//
|
|
3
|
+
// The script and the child share one stdout: a line of the protocol is told from an ordinary
|
|
4
|
+
// line by its prefix. A script that writes a line of its own (a `console.log`) is passed
|
|
5
|
+
// through as a log of the run, and the run goes on.
|
|
6
|
+
//
|
|
7
|
+
// The scenario runs against a real host process (test/fixtures/revision-host.ts).
|
|
8
|
+
|
|
9
|
+
import { expect, test } from "bun:test";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
|
|
12
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
13
|
+
import { counter, eventually, spawnHost, tempHome, watch, connectAsOwner } from "@niwa-labs/testing";
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/revision-host.ts", import.meta.url));
|
|
18
|
+
|
|
19
|
+
/** What the workflow actions answer. */
|
|
20
|
+
const RunOutput = z.object({ run: z.string(), finished: z.boolean() });
|
|
21
|
+
|
|
22
|
+
/** The data of the record of a log line of a run. */
|
|
23
|
+
const RunLog = z.object({ run: z.string(), text: z.string() });
|
|
24
|
+
|
|
25
|
+
/** The log lines of the runs, as the journal holds them. */
|
|
26
|
+
function logsOf(records: StoredRecord[]): string[] {
|
|
27
|
+
const texts: string[] = [];
|
|
28
|
+
for (const record of records) {
|
|
29
|
+
if (record.type !== "workflow.run.log") continue;
|
|
30
|
+
const data = RunLog.safeParse(record.data);
|
|
31
|
+
if (data.success) texts.push(data.data.text);
|
|
32
|
+
}
|
|
33
|
+
return texts;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
test("a script that writes to the stdout does not break the protocol", async () => {
|
|
37
|
+
const home = await tempHome();
|
|
38
|
+
const homePath = home.path;
|
|
39
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
40
|
+
|
|
41
|
+
try {
|
|
42
|
+
host = spawnHost(HOST_SCRIPT, homePath);
|
|
43
|
+
await host.ready();
|
|
44
|
+
const client = await connectAsOwner(homePath);
|
|
45
|
+
const observer = await watch(homePath);
|
|
46
|
+
const seen = observer.seen;
|
|
47
|
+
|
|
48
|
+
// The script writes a line of its own, then makes a call that finishes.
|
|
49
|
+
const script = `${homePath}/stdout-script.js`;
|
|
50
|
+
await Bun.write(
|
|
51
|
+
script,
|
|
52
|
+
[
|
|
53
|
+
'export default workflow({ uses: ["revision.agent"], output: z.json() }, async () => {',
|
|
54
|
+
' console.log("the script writes a line of its own");',
|
|
55
|
+
' const answer = await ask({ label: "work:first" }, "Study the core.");',
|
|
56
|
+
" return answer;",
|
|
57
|
+
'});',
|
|
58
|
+
"",
|
|
59
|
+
].join("\n"),
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
const started = RunOutput.parse(
|
|
63
|
+
await client.call("workflow.start" as never, { file: script } as never),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
// The run finished: the line the script wrote did not break the protocol.
|
|
67
|
+
await eventually(
|
|
68
|
+
() => seen.some((record) => record.type === "workflow.run.finished"),
|
|
69
|
+
30_000,
|
|
70
|
+
"the run should have finished",
|
|
71
|
+
);
|
|
72
|
+
const finished = seen.find((record) => record.type === "workflow.run.finished");
|
|
73
|
+
expect((finished?.data as { outcome?: { result?: unknown } }).outcome?.result).toEqual({
|
|
74
|
+
answer: "answered: Study the core.",
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// The line the script wrote was passed through as a log of the run.
|
|
78
|
+
expect(logsOf(seen)).toContain("the script writes a line of its own");
|
|
79
|
+
|
|
80
|
+
// The call was made once, and the run ended with the answer of it.
|
|
81
|
+
expect(counter(homePath, "revision.agent").count()).toBe(1);
|
|
82
|
+
|
|
83
|
+
await client.close().catch(() => {});
|
|
84
|
+
await observer.stop();
|
|
85
|
+
} finally {
|
|
86
|
+
await host?.kill().catch(() => {});
|
|
87
|
+
await home.cleanup();
|
|
88
|
+
}
|
|
89
|
+
}, 60_000);
|