@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,295 @@
|
|
|
1
|
+
// The typed contract of a script: `workflow({ input, output, ... }, fn)`. The schemas are read by
|
|
2
|
+
// loading the module (check, install), held at the start (input) and at the end (output) of a run, kept
|
|
3
|
+
// as a hash with the run, and checked by the type checker against the function.
|
|
4
|
+
|
|
5
|
+
import { afterEach, expect, test } from "bun:test";
|
|
6
|
+
import { mkdir, writeFile } from "node:fs/promises";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
|
|
9
|
+
import { record, seq } from "@niwa-labs/core";
|
|
10
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
11
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
12
|
+
import { createNiwa, requests, eventually, tempHome, timers } from "@niwa-labs/testing";
|
|
13
|
+
import type { Host } from "@niwa-labs/testing";
|
|
14
|
+
import { InputInvalid, workflow, workflowCancel, workflowCheck, workflowInstall, workflowInstallationGet, workflowInstallationList, workflowResume, workflowStart, workflowWait } from "../src/index.ts";
|
|
15
|
+
import { z } from "zod";
|
|
16
|
+
import type { RunStatus } from "../src/status.ts";
|
|
17
|
+
|
|
18
|
+
const homes: Array<{ cleanup(): Promise<void> }> = [];
|
|
19
|
+
const hosts: Host[] = [];
|
|
20
|
+
|
|
21
|
+
afterEach(async () => {
|
|
22
|
+
for (const host of hosts.splice(0)) await host.close().catch(() => {});
|
|
23
|
+
for (const home of homes.splice(0)) await home.cleanup();
|
|
24
|
+
}, 60_000);
|
|
25
|
+
|
|
26
|
+
const Ping = record("ping.sent", z.object({ id: z.string(), size: z.number() }));
|
|
27
|
+
|
|
28
|
+
async function served(): Promise<{ host: Host; home: string }> {
|
|
29
|
+
const home = await tempHome();
|
|
30
|
+
homes.push(home);
|
|
31
|
+
const host = createNiwa({ home: home.path });
|
|
32
|
+
host.use(requests(), timers([]), plugin("ping", (api) => api.declare(Ping)), workflow({ triggers: [Ping] }));
|
|
33
|
+
await host.serve();
|
|
34
|
+
hosts.push(host);
|
|
35
|
+
return { host, home: home.path };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function scriptIn(home: string, name: string, lines: readonly string[]): Promise<string> {
|
|
39
|
+
await mkdir(home, { recursive: true });
|
|
40
|
+
const file = join(home, name);
|
|
41
|
+
await writeFile(file, lines.join("\n"));
|
|
42
|
+
return file;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const ofType = (records: readonly StoredRecord[], type: string): StoredRecord[] => records.filter((row) => row.type === type);
|
|
46
|
+
const recordsOf = async (host: Host): Promise<StoredRecord[]> => host.records.read();
|
|
47
|
+
|
|
48
|
+
/** The status of a run once it is paused with a reason that says this. */
|
|
49
|
+
async function untilPaused(host: Host, run: string, reason: string): Promise<RunStatus> {
|
|
50
|
+
let seen: RunStatus | undefined;
|
|
51
|
+
await eventually(async () => {
|
|
52
|
+
seen = await host.call(workflowWait, { run, timeoutMs: 300 });
|
|
53
|
+
return seen.state === "paused" && seen.reason?.includes(reason) === true;
|
|
54
|
+
}, 30_000, `the run should pause with "${reason}"`);
|
|
55
|
+
if (seen === undefined) throw new Error("no status");
|
|
56
|
+
return seen;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const Repo = [
|
|
60
|
+
"export default workflow({",
|
|
61
|
+
' description: "Count the findings of a repository",',
|
|
62
|
+
" input: z.object({ repo: z.string(), depth: z.number().int().default(2) }),",
|
|
63
|
+
" output: z.object({ repo: z.string(), findings: z.array(z.string()) }),",
|
|
64
|
+
"}, async ({ repo, depth }) => {",
|
|
65
|
+
" return { repo, findings: Array.from({ length: depth }, (_, at) => `finding ${at}`) };",
|
|
66
|
+
"});",
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
test("check shows the description, the schemas as JSON Schema and a contract hash; install keeps them for the installation", async () => {
|
|
70
|
+
const { host, home } = await served();
|
|
71
|
+
const file = await scriptIn(home, "repo.ts", Repo);
|
|
72
|
+
const report = await host.call(workflowCheck, { file });
|
|
73
|
+
expect(report).toMatchObject({ ok: true, errors: [], note: "described, the script was not run", description: "Count the findings of a repository" });
|
|
74
|
+
expect(report.input).toMatchObject({ type: "object", properties: { repo: { type: "string" }, depth: { type: "integer", default: 2 } }, required: ["repo"] });
|
|
75
|
+
expect(report.output).toMatchObject({ type: "object", required: ["repo", "findings"] });
|
|
76
|
+
expect(report.contract).toMatch(/^[0-9a-f]{64}$/);
|
|
77
|
+
|
|
78
|
+
const on = await scriptIn(home, "on.ts", [
|
|
79
|
+
"export default workflow({",
|
|
80
|
+
' description: "Look at a ping",',
|
|
81
|
+
' on: [{ type: "ping.sent", filter: { id: "a" } }],',
|
|
82
|
+
" output: z.string(),",
|
|
83
|
+
"}, async (trigger) => {",
|
|
84
|
+
" return trigger.data.id;",
|
|
85
|
+
"});",
|
|
86
|
+
]);
|
|
87
|
+
const { id } = await host.call(workflowInstall, { file: on, description: "pinged" });
|
|
88
|
+
const { items } = await host.call(workflowInstallationList, {});
|
|
89
|
+
expect(items).toEqual([expect.objectContaining({ id, description: "pinged" })]);
|
|
90
|
+
const installed = await host.call(workflowInstallationGet, { id });
|
|
91
|
+
expect(installed).toMatchObject({ id, description: "pinged", output: { type: "string" } });
|
|
92
|
+
expect(installed.input).toBeUndefined();
|
|
93
|
+
expect(installed.contract).toMatch(/^[0-9a-f]{64}$/);
|
|
94
|
+
}, 60_000);
|
|
95
|
+
|
|
96
|
+
test("the module is loaded for its schemas, so imports at the top work and the function does not run", async () => {
|
|
97
|
+
const { host, home } = await served();
|
|
98
|
+
const marker = join(home, "ran");
|
|
99
|
+
await writeFile(join(home, "shapes.ts"), "export const Shape = z.object({ size: z.number() });\n");
|
|
100
|
+
const file = await scriptIn(home, "imports.ts", [
|
|
101
|
+
'import { Shape } from "./shapes.ts";',
|
|
102
|
+
`console.log("a line the module writes at its top level");`,
|
|
103
|
+
"export default workflow({ input: Shape, output: Shape }, async ({ size }) => {",
|
|
104
|
+
` await Bun.write(${JSON.stringify(marker)}, "ran");`,
|
|
105
|
+
" return { size };",
|
|
106
|
+
"});",
|
|
107
|
+
]);
|
|
108
|
+
const report = await host.call(workflowCheck, { file });
|
|
109
|
+
expect(report).toMatchObject({ ok: true, input: { type: "object", properties: { size: { type: "number" } } } });
|
|
110
|
+
expect(await Bun.file(marker).exists()).toBe(false);
|
|
111
|
+
}, 60_000);
|
|
112
|
+
|
|
113
|
+
test("a schema that JSON Schema cannot say, an input that is not an object, and a call of niwa at the top are errors of the check", async () => {
|
|
114
|
+
const { host, home } = await served();
|
|
115
|
+
const bad = await scriptIn(home, "bad.js", [
|
|
116
|
+
"export default workflow({",
|
|
117
|
+
" input: z.object({ when: z.date() }),",
|
|
118
|
+
" output: z.object({ anything: z.unknown() }),",
|
|
119
|
+
"}, async () => ({ anything: 1 }));",
|
|
120
|
+
]);
|
|
121
|
+
const report = await host.call(workflowCheck, { file: bad });
|
|
122
|
+
expect(report.ok).toBe(false);
|
|
123
|
+
expect(report.errors.map((error) => [error.code, error.line])).toEqual([["schema", 2], ["schema", 3]]);
|
|
124
|
+
expect(report.errors[0]?.message).toContain("Date cannot be represented");
|
|
125
|
+
expect(report.errors[1]?.message).toContain("says nothing");
|
|
126
|
+
|
|
127
|
+
const text = await scriptIn(home, "text.js", ["export default workflow({ input: z.string() }, async () => {});"]);
|
|
128
|
+
expect((await host.call(workflowCheck, { file: text })).errors[0]).toMatchObject({ code: "schema", message: expect.stringContaining("z.object") });
|
|
129
|
+
|
|
130
|
+
const top = await scriptIn(home, "top.js", ["await niwa.task.create({});", "export default workflow({}, async () => {});"]);
|
|
131
|
+
expect((await host.call(workflowCheck, { file: top })).errors[0]).toMatchObject({ code: "describe", message: expect.stringContaining("not available while the module is read") });
|
|
132
|
+
|
|
133
|
+
const legacy = await scriptIn(home, "legacy.js", ["export default async function () {}"]);
|
|
134
|
+
expect((await host.call(workflowCheck, { file: legacy })).errors[0]).toMatchObject({ code: "workflow" });
|
|
135
|
+
|
|
136
|
+
const both = await scriptIn(home, "both.js", ['export default workflow({ input: z.object({}), on: ["ping.sent"] }, async () => {});']);
|
|
137
|
+
expect((await host.call(workflowCheck, { file: both })).errors[0]).toMatchObject({ code: "workflow", message: expect.stringContaining("not both") });
|
|
138
|
+
}, 60_000);
|
|
139
|
+
|
|
140
|
+
test("a start whose arguments do not pass the input is refused with the fields, and no run exists", async () => {
|
|
141
|
+
const { host, home } = await served();
|
|
142
|
+
const file = await scriptIn(home, "repo.ts", Repo);
|
|
143
|
+
const refused = await host.call(workflowStart, { file, args: { repo: 7, depth: "deep" } }).catch((error: unknown) => error);
|
|
144
|
+
expect(refused).toBeInstanceOf(Error);
|
|
145
|
+
expect((refused as Error).message).toContain("input of the script");
|
|
146
|
+
expect((refused as Error).message).toContain("repo");
|
|
147
|
+
expect((refused as Error).message).toContain("depth");
|
|
148
|
+
expect(ofType(await recordsOf(host), "workflow.run.requested")).toHaveLength(0);
|
|
149
|
+
|
|
150
|
+
const nothing = await host.call(workflowStart, { file }).catch((error: unknown) => error);
|
|
151
|
+
expect((nothing as Error).message).toContain("repo");
|
|
152
|
+
|
|
153
|
+
const bare = await scriptIn(home, "bare.ts", ["export default workflow({ output: z.number() }, async () => 1);"]);
|
|
154
|
+
const stray = await host.call(workflowStart, { file: bare, args: { x: 1 } }).catch((error: unknown) => error);
|
|
155
|
+
expect((stray as Error).message).toContain("takes no input");
|
|
156
|
+
expect(ofType(await recordsOf(host), "workflow.run.requested")).toHaveLength(0);
|
|
157
|
+
expect(new InputInvalid([{ path: "a", message: "m" }]).code).toBe("input-invalid");
|
|
158
|
+
|
|
159
|
+
// Good arguments run, the defaults go in, and the result is the outcome of the run.
|
|
160
|
+
const started = await host.call(workflowStart, { file, args: { repo: "niwa" }, wait: true, timeoutMs: 20_000 });
|
|
161
|
+
expect(started).toMatchObject({ state: "finished", outcome: { status: "done", result: { repo: "niwa", findings: ["finding 0", "finding 1"] } } });
|
|
162
|
+
}, 60_000);
|
|
163
|
+
|
|
164
|
+
test("a result that does not pass the output ends the run as failed with the fields; a result with no output declared is refused too", async () => {
|
|
165
|
+
const { host, home } = await served();
|
|
166
|
+
const wrong = await scriptIn(home, "wrong.js", [
|
|
167
|
+
"export default workflow({ output: z.object({ count: z.number(), tags: z.array(z.string()) }) }, async () => ({ count: 'many', tags: [1] }));",
|
|
168
|
+
]);
|
|
169
|
+
const failed = await host.call(workflowStart, { file: wrong, wait: true, timeoutMs: 20_000 });
|
|
170
|
+
expect(failed.state).toBe("finished");
|
|
171
|
+
expect(failed.outcome).toMatchObject({ status: "failed", error: { code: "output-invalid", issues: expect.arrayContaining([expect.objectContaining({ path: "count" }), expect.objectContaining({ path: "tags.0" })]) } });
|
|
172
|
+
expect(await host.call(workflowWait, { run: failed.run })).toMatchObject({ state: "finished", outcome: { status: "failed" } });
|
|
173
|
+
expect(ofType(await recordsOf(host), "workflow.run.finished")).toHaveLength(1);
|
|
174
|
+
|
|
175
|
+
const silent = await scriptIn(home, "silent.js", ["export default workflow({}, async () => 5);"]);
|
|
176
|
+
const stray = await host.call(workflowStart, { file: silent, wait: true, timeoutMs: 20_000 });
|
|
177
|
+
expect(stray.outcome).toMatchObject({ status: "failed", error: { code: "output-invalid", message: expect.stringContaining("declares no output") } });
|
|
178
|
+
}, 60_000);
|
|
179
|
+
|
|
180
|
+
test("the outcome of a run is done, failed or cancelled", async () => {
|
|
181
|
+
const { host, home } = await served();
|
|
182
|
+
const done = await scriptIn(home, "done.js", ["export default workflow({ output: z.string() }, async () => 'ok');"]);
|
|
183
|
+
const nothing = await scriptIn(home, "nothing.js", ["export default workflow({}, async () => {});"]);
|
|
184
|
+
const sleeper = await scriptIn(home, "sleeper.js", ["export default workflow({}, async () => { await sleep('1h'); });"]);
|
|
185
|
+
expect(await host.call(workflowStart, { file: done, wait: true, timeoutMs: 20_000 })).toMatchObject({ outcome: { status: "done", result: "ok" } });
|
|
186
|
+
expect(await host.call(workflowStart, { file: nothing, wait: true, timeoutMs: 20_000 })).toMatchObject({ outcome: { status: "done", result: null } });
|
|
187
|
+
const { run: sleeping } = await host.call(workflowStart, { file: sleeper });
|
|
188
|
+
await eventually(async () => ofType(await recordsOf(host), "workflow.run.started").length === 3, 20_000);
|
|
189
|
+
await host.call(workflowCancel, { run: sleeping });
|
|
190
|
+
expect(await host.call(workflowWait, { run: sleeping })).toMatchObject({ state: "finished", outcome: { status: "cancelled" } });
|
|
191
|
+
expect(ofType(await recordsOf(host), "workflow.run.finished").map((row) => (row.data as { outcome: { status: string } }).outcome.status).sort()).toEqual(["cancelled", "done", "done"]);
|
|
192
|
+
}, 30_000);
|
|
193
|
+
|
|
194
|
+
test("a changed contract stops a run whose recorded arguments no longer pass it, and lets one whose arguments still pass go on", async () => {
|
|
195
|
+
const { host, home } = await served();
|
|
196
|
+
const body = (shape: string): string[] => [
|
|
197
|
+
`export default workflow({ input: z.object(${shape}), output: z.number() }, async () => {`,
|
|
198
|
+
' throw new Error("first start fails");',
|
|
199
|
+
"});",
|
|
200
|
+
];
|
|
201
|
+
const stops = await scriptIn(home, "stops.js", body("{ repo: z.string() }"));
|
|
202
|
+
const { run: stopping } = await host.call(workflowStart, { file: stops, args: { repo: "niwa" } });
|
|
203
|
+
expect((await host.call(workflowWait, { run: stopping, timeoutMs: 20_000 })).state).toBe("paused");
|
|
204
|
+
const before = (ofType(await recordsOf(host), "workflow.run.started")[0]?.data as { contract: string }).contract;
|
|
205
|
+
|
|
206
|
+
await writeFile(stops, body("{ repo: z.string(), branch: z.string() }").join("\n"));
|
|
207
|
+
await host.call(workflowResume, { run: stopping });
|
|
208
|
+
const status = await untilPaused(host, stopping, "contract-changed");
|
|
209
|
+
expect(status).toMatchObject({ state: "paused", choices: ["cancel"] });
|
|
210
|
+
expect(status.reason).toContain("branch");
|
|
211
|
+
// The run keeps the contract it began under: a resume meets the same change.
|
|
212
|
+
const starts = ofType(await recordsOf(host), "workflow.run.started");
|
|
213
|
+
expect((starts.at(-1)?.data as { contract: string }).contract).toBe(before);
|
|
214
|
+
|
|
215
|
+
const goes = await scriptIn(home, "goes.js", body("{ repo: z.string() }"));
|
|
216
|
+
const { run: going } = await host.call(workflowStart, { file: goes, args: { repo: "niwa" } });
|
|
217
|
+
expect((await host.call(workflowWait, { run: going, timeoutMs: 20_000 })).state).toBe("paused");
|
|
218
|
+
// A field with a default: the recorded arguments still pass, so the run goes on under the new contract.
|
|
219
|
+
await writeFile(goes, ["export default workflow({ input: z.object({ repo: z.string(), branch: z.string().default('main') }), output: z.number() }, async () => 7);"].join("\n"));
|
|
220
|
+
await host.call(workflowResume, { run: going });
|
|
221
|
+
await eventually(async () => (await host.call(workflowWait, { run: going, timeoutMs: 300 })).state === "finished", 20_000);
|
|
222
|
+
const after = await host.call(workflowWait, { run: going });
|
|
223
|
+
expect(after.outcome).toMatchObject({ status: "done", result: 7 });
|
|
224
|
+
const goesStarts = ofType(await recordsOf(host), "workflow.run.started").filter((row) => (row.data as { run: string }).run === going);
|
|
225
|
+
expect(new Set(goesStarts.map((row) => (row.data as { contract: string }).contract)).size).toBe(2);
|
|
226
|
+
}, 60_000);
|
|
227
|
+
|
|
228
|
+
test("a schema that the script imports changes the contract without changing the file of the script", async () => {
|
|
229
|
+
const { host, home } = await served();
|
|
230
|
+
const shape = (extra: string): string => `export const Input = z.object({ repo: z.string()${extra} });\n`;
|
|
231
|
+
await writeFile(join(home, "input.ts"), shape(""));
|
|
232
|
+
const file = await scriptIn(home, "imported.js", [
|
|
233
|
+
'import { Input } from "./input.ts";',
|
|
234
|
+
"export default workflow({ input: Input }, async () => { throw new Error('stop'); });",
|
|
235
|
+
]);
|
|
236
|
+
const { run: imported } = await host.call(workflowStart, { file, args: { repo: "niwa" } });
|
|
237
|
+
expect((await host.call(workflowWait, { run: imported, timeoutMs: 20_000 })).state).toBe("paused");
|
|
238
|
+
await writeFile(join(home, "input.ts"), shape(", branch: z.string()"));
|
|
239
|
+
await host.call(workflowResume, { run: imported });
|
|
240
|
+
expect(await untilPaused(host, imported, "contract-changed")).toMatchObject({ state: "paused", choices: ["cancel"] });
|
|
241
|
+
}, 60_000);
|
|
242
|
+
|
|
243
|
+
test("the typing rejects a wrong field of the input, a wrong result, a result with no output, and an argument for a script with none", async () => {
|
|
244
|
+
const { host, home } = await served();
|
|
245
|
+
const rejected = async (name: string, lines: string[]): Promise<{ line: number; code: string }[]> => {
|
|
246
|
+
const file = await scriptIn(home, name, lines);
|
|
247
|
+
const report = await host.call(workflowCheck, { file });
|
|
248
|
+
expect(report.ok).toBe(false);
|
|
249
|
+
return report.errors.map((error) => ({ line: error.line, code: error.code }));
|
|
250
|
+
};
|
|
251
|
+
const ok = await scriptIn(home, "ok.ts", Repo);
|
|
252
|
+
expect((await host.call(workflowCheck, { file: ok })).ok).toBe(true);
|
|
253
|
+
|
|
254
|
+
expect(await rejected("input.ts", [
|
|
255
|
+
"export default workflow({ input: z.object({ repo: z.string() }), output: z.number() }, async ({ repository }) => {",
|
|
256
|
+
" return 1;",
|
|
257
|
+
"});",
|
|
258
|
+
])).toEqual([{ line: 1, code: "TS2339" }]);
|
|
259
|
+
|
|
260
|
+
expect(await rejected("output.ts", [
|
|
261
|
+
"export default workflow({ output: z.object({ count: z.number() }) }, async () => {",
|
|
262
|
+
' return { count: "many" };',
|
|
263
|
+
"});",
|
|
264
|
+
])).toEqual([{ line: 1, code: "TS2345" }]);
|
|
265
|
+
|
|
266
|
+
expect(await rejected("none.ts", [
|
|
267
|
+
"export default workflow({}, async () => {",
|
|
268
|
+
" return 1;",
|
|
269
|
+
"});",
|
|
270
|
+
])).toEqual([{ line: 1, code: "TS2345" }]);
|
|
271
|
+
|
|
272
|
+
expect(await rejected("noinput.ts", [
|
|
273
|
+
"export default workflow({ output: z.number() }, async (input) => {",
|
|
274
|
+
" return 1;",
|
|
275
|
+
"});",
|
|
276
|
+
])).toEqual([{ line: 1, code: "TS2345" }]);
|
|
277
|
+
|
|
278
|
+
expect(await rejected("trigger.ts", [
|
|
279
|
+
'export default workflow({ on: [{ type: "ping.sent" }], output: z.number() }, async (trigger) => {',
|
|
280
|
+
" return trigger.data.nothing;",
|
|
281
|
+
"});",
|
|
282
|
+
])).toEqual([{ line: 2, code: "TS2339" }]);
|
|
283
|
+
}, 60_000);
|
|
284
|
+
|
|
285
|
+
test("the filter of a subscription is held to the schema of its type: an unknown path and a literal the field cannot hold are errors", async () => {
|
|
286
|
+
const { host, home } = await served();
|
|
287
|
+
const on = (filter: string): string[] => [`export default workflow({ on: [{ type: "ping.sent", filter: ${filter} }] }, async () => {});`];
|
|
288
|
+
const typo = await scriptIn(home, "typo.ts", on('{ idd: "a" }'));
|
|
289
|
+
expect((await host.call(workflowCheck, { file: typo })).errors).toMatchObject([{ code: "on", message: expect.stringContaining('names "idd"') }]);
|
|
290
|
+
const kind = await scriptIn(home, "kind.ts", on("{ size: 'big' }"));
|
|
291
|
+
expect((await host.call(workflowCheck, { file: kind })).errors).toMatchObject([{ code: "on", message: expect.stringContaining("cannot hold") }]);
|
|
292
|
+
const fine = await scriptIn(home, "fine.ts", on('{ id: "a", size: 3 }'));
|
|
293
|
+
expect((await host.call(workflowCheck, { file: fine })).ok).toBe(true);
|
|
294
|
+
await expect(host.call(workflowInstall, { file: typo, description: "typo" })).rejects.toThrow("does not pass its check");
|
|
295
|
+
}, 60_000);
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Workflow resume: a script runs agents in parallel in two harnesses and the host is killed midway.
|
|
2
|
+
// After the resume the finished calls are not repeated and the result is complete.
|
|
3
|
+
//
|
|
4
|
+
// The scenario runs against a real host process (test/fixtures/crash-host.ts):
|
|
5
|
+
//
|
|
6
|
+
// 1. spawn the host, which installs the workflow plugin with a stub agent; start the script
|
|
7
|
+
// 2. the two branches of the script reach the gated agent call; the host is killed there
|
|
8
|
+
// 3. spawn the host again, open the gate: the run is resumed
|
|
9
|
+
// 4. the calls that already finished are not repeated, and the result of the run is complete
|
|
10
|
+
//
|
|
11
|
+
// Only the public API is used: the entries of the packages, and of testing.
|
|
12
|
+
|
|
13
|
+
import { expect, test } from "bun:test";
|
|
14
|
+
import { fileURLToPath } from "node:url";
|
|
15
|
+
|
|
16
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
17
|
+
import { counter, eventually, gate, spawnHost, tempHome, connectAsOwner } from "@niwa-labs/testing";
|
|
18
|
+
import { z } from "zod";
|
|
19
|
+
|
|
20
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/crash-host.ts", import.meta.url));
|
|
21
|
+
const SCRIPT = fileURLToPath(new URL("./fixtures/crash-script.js", import.meta.url));
|
|
22
|
+
|
|
23
|
+
/** What the workflow actions answer. */
|
|
24
|
+
const RunOutput = z.object({
|
|
25
|
+
run: z.string(),
|
|
26
|
+
finished: z.boolean(),
|
|
27
|
+
result: z.unknown().optional(),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
/** The result the script returns: the two reports. */
|
|
31
|
+
const ScriptResult = z.object({
|
|
32
|
+
reports: z.array(z.object({ answer: z.string() })),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
/** The data of a record of the type that says a run finished. */
|
|
36
|
+
function finishedResult(record: StoredRecord): unknown {
|
|
37
|
+
if (record.type !== "workflow.run.finished") return undefined;
|
|
38
|
+
return (record.data as { outcome?: { result?: unknown } }).outcome?.result;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
test("Workflow resume: the finished calls are not repeated and the result is complete", async () => {
|
|
42
|
+
const home = await tempHome();
|
|
43
|
+
const homePath = home.path;
|
|
44
|
+
let first: ReturnType<typeof spawnHost> | undefined;
|
|
45
|
+
let second: ReturnType<typeof spawnHost> | undefined;
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
// 1. The host, and the script that runs two agents in parallel.
|
|
49
|
+
first = spawnHost(HOST_SCRIPT, homePath);
|
|
50
|
+
await first.ready();
|
|
51
|
+
const client = await connectAsOwner(homePath);
|
|
52
|
+
|
|
53
|
+
const started = RunOutput.parse(
|
|
54
|
+
await client.call("workflow.start" as never, { file: SCRIPT } as never),
|
|
55
|
+
);
|
|
56
|
+
expect(started.run).toBeTruthy();
|
|
57
|
+
|
|
58
|
+
// 2. Both branches reach the gated call: they are in flight at the same time.
|
|
59
|
+
await eventually(
|
|
60
|
+
() => counter(homePath, "crash.agent").count() === 2,
|
|
61
|
+
20_000,
|
|
62
|
+
"both branches should have reached the agent call",
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
// One branch finishes: its gate is opened and its answer is committed to the journal. The
|
|
66
|
+
// counter only says the action returned, so the journal is what is waited for.
|
|
67
|
+
const early = await connectAsOwner(homePath);
|
|
68
|
+
const outcomes: StoredRecord[] = [];
|
|
69
|
+
const reading = (async () => {
|
|
70
|
+
for await (const record of early.records.since()) outcomes.push(record);
|
|
71
|
+
})().catch(() => undefined);
|
|
72
|
+
gate(homePath, "crash.gate.crash.one").open();
|
|
73
|
+
await eventually(
|
|
74
|
+
() =>
|
|
75
|
+
outcomes.some(
|
|
76
|
+
(record) =>
|
|
77
|
+
record.type === "niwa.step.outcome" &&
|
|
78
|
+
// The hold of the grant on a call is a step of its own: it is not the call.
|
|
79
|
+
!(record.data as { key: string }).key.endsWith("~auth") &&
|
|
80
|
+
(record.data as { outcome?: { status?: string } }).outcome?.status === "completed",
|
|
81
|
+
),
|
|
82
|
+
20_000,
|
|
83
|
+
"the first branch should have finished and be in the journal",
|
|
84
|
+
);
|
|
85
|
+
await early.close().catch(() => {});
|
|
86
|
+
await reading;
|
|
87
|
+
|
|
88
|
+
// The host is killed in the middle: a real crash, with the other branch still in flight.
|
|
89
|
+
await first.kill();
|
|
90
|
+
first = undefined;
|
|
91
|
+
await client.close().catch(() => {});
|
|
92
|
+
|
|
93
|
+
// 3. The host comes back and the gate of the second branch opens: the run is resumed.
|
|
94
|
+
second = spawnHost(HOST_SCRIPT, homePath);
|
|
95
|
+
gate(homePath, "crash.gate.crash.two").open();
|
|
96
|
+
await second.ready();
|
|
97
|
+
|
|
98
|
+
const watcher = await connectAsOwner(homePath);
|
|
99
|
+
const records: StoredRecord[] = [];
|
|
100
|
+
const watch = (async () => {
|
|
101
|
+
for await (const record of watcher.records.since()) {
|
|
102
|
+
records.push(record);
|
|
103
|
+
}
|
|
104
|
+
})().catch(() => undefined);
|
|
105
|
+
|
|
106
|
+
// The run ends with its result.
|
|
107
|
+
await eventually(
|
|
108
|
+
() => records.some((record) => finishedResult(record) !== undefined),
|
|
109
|
+
30_000,
|
|
110
|
+
"the resumed run should have finished",
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
// 4. The result is complete: both branches answered.
|
|
114
|
+
const finished = records.find((record) => finishedResult(record) !== undefined);
|
|
115
|
+
const result = ScriptResult.parse(finishedResult(finished as StoredRecord));
|
|
116
|
+
expect(result.reports.length).toBe(2);
|
|
117
|
+
// The results are in the order of the branches, whichever finished first.
|
|
118
|
+
expect(result.reports.map((report) => report.answer)).toEqual([
|
|
119
|
+
"answered: Study the core.",
|
|
120
|
+
"answered: Study the state.",
|
|
121
|
+
]);
|
|
122
|
+
expect(records.filter((record) => finishedResult(record) !== undefined).length).toBe(1);
|
|
123
|
+
|
|
124
|
+
// The finished call is not repeated: it was called once before the crash and the resumed
|
|
125
|
+
// run took its answer from the journal. Only the branch that was in flight ran again.
|
|
126
|
+
expect(counter(homePath, "crash.crash.one").count()).toBe(1);
|
|
127
|
+
expect(counter(homePath, "crash.crash.two").count()).toBe(1);
|
|
128
|
+
expect(counter(homePath, "crash.agent").count()).toBe(3);
|
|
129
|
+
|
|
130
|
+
await watcher.close().catch(() => {});
|
|
131
|
+
await watch;
|
|
132
|
+
|
|
133
|
+
// No child of the script is left behind, neither the one orphaned by the crash nor the new one.
|
|
134
|
+
const left = Bun.spawnSync(["pgrep", "-f", "child.ts.*crash-script.js"]);
|
|
135
|
+
expect(left.stdout.toString().trim()).toBe("");
|
|
136
|
+
} finally {
|
|
137
|
+
await first?.kill().catch(() => {});
|
|
138
|
+
await second?.kill().catch(() => {});
|
|
139
|
+
await home.cleanup();
|
|
140
|
+
}
|
|
141
|
+
}, 90_000);
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// The deadline of a run: `limits.deadline` bounds the whole script. When it comes the run pauses
|
|
2
|
+
// with a choice of extend or cancel; nothing new is entered until the owner extends it, and a run
|
|
3
|
+
// it starts takes no later deadline than its own.
|
|
4
|
+
|
|
5
|
+
import { afterEach, expect, test } from "bun:test";
|
|
6
|
+
import { writeFile } from "node:fs/promises";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
|
|
9
|
+
import type { ActionName, StoredRecord } from "@niwa-labs/core";
|
|
10
|
+
import { createNiwa, requests, eventually, tempHome, testClock, timers, WfNote, stub } from "@niwa-labs/testing";
|
|
11
|
+
import type { Host, TestClock } from "@niwa-labs/testing";
|
|
12
|
+
import { workflow } from "../src/index.ts";
|
|
13
|
+
|
|
14
|
+
const START = 1_700_000_000_000;
|
|
15
|
+
const MINUTE = 60_000;
|
|
16
|
+
const NOTE = 'const Note = record("note", z.object({ text: z.string() })); ';
|
|
17
|
+
|
|
18
|
+
const homes: Array<{ cleanup(): Promise<void> }> = [];
|
|
19
|
+
const hosts: Host[] = [];
|
|
20
|
+
afterEach(async () => {
|
|
21
|
+
for (const host of hosts.splice(0)) await host.close().catch(() => {});
|
|
22
|
+
for (const home of homes.splice(0)) await home.cleanup();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
async function served(): Promise<{ niwa: Host; clock: TestClock; home: string }> {
|
|
26
|
+
const home = await tempHome();
|
|
27
|
+
homes.push(home);
|
|
28
|
+
const clock = testClock(START);
|
|
29
|
+
const niwa = createNiwa({ home: home.path, clock });
|
|
30
|
+
niwa.use(requests());
|
|
31
|
+
niwa.use(timers([]));
|
|
32
|
+
niwa.use(stub(home.path));
|
|
33
|
+
niwa.use(workflow({ emits: [WfNote] }));
|
|
34
|
+
await niwa.serve();
|
|
35
|
+
hosts.push(niwa);
|
|
36
|
+
return { niwa, clock, home: home.path };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const call = <O>(niwa: Host, name: string, input: unknown): Promise<O> => niwa.call(name as ActionName, input as never) as Promise<O>;
|
|
40
|
+
const ofType = async (niwa: Host, type: string): Promise<StoredRecord[]> => (await niwa.records.read()).filter((row) => row.type === type);
|
|
41
|
+
const notes = async (niwa: Host): Promise<string[]> => (await ofType(niwa, "wf.demo.note")).map((row) => (row.data as { text: string }).text);
|
|
42
|
+
|
|
43
|
+
const SCRIPT = `${NOTE}export default workflow({ name: "demo", emits: [Note], output: z.json() }, async () => {
|
|
44
|
+
await emit(Note, { text: "first" });
|
|
45
|
+
await sleep("10m");
|
|
46
|
+
await emit(Note, { text: "second" });
|
|
47
|
+
return "done";
|
|
48
|
+
});`;
|
|
49
|
+
|
|
50
|
+
async function started(niwa: Host, home: string, limits?: unknown): Promise<string> {
|
|
51
|
+
const file = join(home, "script.js");
|
|
52
|
+
await writeFile(file, SCRIPT);
|
|
53
|
+
const { run } = await call<{ run: string }>(niwa, "workflow.start", { file, ...(limits === undefined ? {} : { limits }) });
|
|
54
|
+
await eventually(async () => (await ofType(niwa, "timer.sleep.requested")).length === 1, 10_000, "the sleep should be armed");
|
|
55
|
+
return run;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type Status = { state: string; reason?: string; choices?: string[]; manualPause?: { kind?: string } };
|
|
59
|
+
|
|
60
|
+
test("a run that reaches its deadline pauses with extend or cancel, enters nothing new, and goes on once extended", async () => {
|
|
61
|
+
const { niwa, clock, home } = await served();
|
|
62
|
+
const run = await started(niwa, home, { deadline: 5 * MINUTE });
|
|
63
|
+
|
|
64
|
+
await clock.advance(5 * MINUTE);
|
|
65
|
+
const paused = await call<Status>(niwa, "workflow.wait", { run, timeoutMs: 1 });
|
|
66
|
+
expect(paused).toMatchObject({ state: "paused", choices: ["extend", "cancel"], manualPause: { kind: "deadline" } });
|
|
67
|
+
expect(paused.reason).toContain("deadline");
|
|
68
|
+
// The list of runs says how the pause is answered.
|
|
69
|
+
const { runs } = await call<{ runs: string[] }>(niwa, "workflow.runs", {});
|
|
70
|
+
expect(runs.find((line) => line.startsWith(run.slice(0, 8)) || line.includes("paused"))).toContain("niwa workflow.extend --run");
|
|
71
|
+
// A plain continue does not get past a deadline: only more time does.
|
|
72
|
+
const pause = (await ofType(niwa, "workflow.run.pause-requested")).at(-1)!;
|
|
73
|
+
await expect(call(niwa, "workflow.continue", { run, pause: String(pause.id) })).rejects.toThrow(/workflow.extend/);
|
|
74
|
+
|
|
75
|
+
// The sleep ends, and the script is held before its next step.
|
|
76
|
+
await clock.advance(5 * MINUTE);
|
|
77
|
+
await Bun.sleep(300);
|
|
78
|
+
expect(await notes(niwa)).toEqual(["first"]);
|
|
79
|
+
|
|
80
|
+
await call(niwa, "workflow.extend", { run, by: 10 * MINUTE });
|
|
81
|
+
await eventually(async () => (await ofType(niwa, "workflow.run.finished")).length === 1, 10_000, "the run should finish once extended");
|
|
82
|
+
expect(await notes(niwa)).toEqual(["first", "second"]);
|
|
83
|
+
expect((await ofType(niwa, "workflow.run.deadline-extended")).map((row) => row.data)).toEqual([{ run, deadline: START + 20 * MINUTE }]);
|
|
84
|
+
}, 30_000);
|
|
85
|
+
|
|
86
|
+
test("a run with no deadline does not pause on the clock", async () => {
|
|
87
|
+
const { niwa, clock, home } = await served();
|
|
88
|
+
const run = await started(niwa, home);
|
|
89
|
+
await clock.advance(10 * MINUTE);
|
|
90
|
+
await eventually(async () => (await ofType(niwa, "workflow.run.finished")).length === 1, 10_000, "the run should finish");
|
|
91
|
+
expect(await ofType(niwa, "workflow.run.pause-requested")).toEqual([]);
|
|
92
|
+
expect((await call<Status>(niwa, "workflow.wait", { run, timeoutMs: 1 })).state).toBe("finished");
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("a run that finished before its deadline is not paused when the deadline comes", async () => {
|
|
96
|
+
const { niwa, clock, home } = await served();
|
|
97
|
+
await started(niwa, home, { deadline: 30 * MINUTE });
|
|
98
|
+
await clock.advance(10 * MINUTE);
|
|
99
|
+
await eventually(async () => (await ofType(niwa, "workflow.run.finished")).length === 1, 10_000, "the run should finish");
|
|
100
|
+
await clock.advance(30 * MINUTE);
|
|
101
|
+
expect(await ofType(niwa, "workflow.run.pause-requested")).toEqual([]);
|
|
102
|
+
});
|