@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.
Files changed (114) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -2
  3. package/package.json +55 -4
  4. package/src/actions.ts +408 -0
  5. package/src/agent.ts +211 -0
  6. package/src/ask.ts +84 -0
  7. package/src/bridge.ts +710 -0
  8. package/src/catalogue.ts +74 -0
  9. package/src/check.ts +880 -0
  10. package/src/child.ts +1378 -0
  11. package/src/cockpit/actions.ts +128 -0
  12. package/src/cockpit/catalogue-actions.ts +97 -0
  13. package/src/cockpit/dashboard.tsx +134 -0
  14. package/src/cockpit/groups.ts +45 -0
  15. package/src/cockpit/index.tsx +50 -0
  16. package/src/cockpit/launch.tsx +136 -0
  17. package/src/cockpit/plan.ts +101 -0
  18. package/src/cockpit/run-commands.tsx +102 -0
  19. package/src/cockpit/run-page.tsx +140 -0
  20. package/src/cockpit/run-summary.ts +16 -0
  21. package/src/cockpit/runs.tsx +73 -0
  22. package/src/cockpit/statistics.tsx +176 -0
  23. package/src/cockpit/track.tsx +159 -0
  24. package/src/cockpit/workflows.tsx +383 -0
  25. package/src/describe-cache.ts +122 -0
  26. package/src/describe.ts +174 -0
  27. package/src/guide.ts +213 -0
  28. package/src/index.ts +2813 -0
  29. package/src/inspect.ts +235 -0
  30. package/src/installation.ts +90 -0
  31. package/src/language.ts +217 -0
  32. package/src/records-entry.ts +63 -0
  33. package/src/records.ts +574 -0
  34. package/src/registry.ts +250 -0
  35. package/src/revise.ts +89 -0
  36. package/src/runner.ts +339 -0
  37. package/src/script-types.ts +549 -0
  38. package/src/stats.ts +460 -0
  39. package/src/status.ts +512 -0
  40. package/src/store.ts +259 -0
  41. package/src/subscriptions.ts +102 -0
  42. package/src/waiting.ts +61 -0
  43. package/test/cockpit-actions.test.ts +20 -0
  44. package/test/cockpit-groups.test.ts +42 -0
  45. package/test/cockpit-plan.test.ts +118 -0
  46. package/test/fixtures/branches-host.ts +58 -0
  47. package/test/fixtures/branches-script.js +23 -0
  48. package/test/fixtures/bridge-host.ts +23 -0
  49. package/test/fixtures/bridge-keys-script.js +15 -0
  50. package/test/fixtures/bridge-wait-script.js +8 -0
  51. package/test/fixtures/cancel-host.ts +58 -0
  52. package/test/fixtures/cancel-script.js +12 -0
  53. package/test/fixtures/crash-host.ts +61 -0
  54. package/test/fixtures/crash-script.js +14 -0
  55. package/test/fixtures/ping-records.ts +20 -0
  56. package/test/fixtures/revision-host.ts +57 -0
  57. package/test/fixtures/revision-script.js +11 -0
  58. package/test/fixtures/running-host.ts +61 -0
  59. package/test/fixtures/running-script.js +12 -0
  60. package/test/fixtures/with-data/flow.ts +2 -0
  61. package/test/fixtures/with-data/table.json +1 -0
  62. package/test/git-repo.ts +45 -0
  63. package/test/git-worktree.test.ts +413 -0
  64. package/test/question-format.test.ts +279 -0
  65. package/test/workflow-agent-caps.test.ts +91 -0
  66. package/test/workflow-agent-lifetime.test.ts +93 -0
  67. package/test/workflow-agent-options.test.ts +216 -0
  68. package/test/workflow-agent-schema.test.ts +386 -0
  69. package/test/workflow-agent-typings.test.ts +241 -0
  70. package/test/workflow-ask-resume.test.ts +145 -0
  71. package/test/workflow-branch-keys.test.ts +133 -0
  72. package/test/workflow-bridge-inproc.test.ts +230 -0
  73. package/test/workflow-bridge.test.ts +141 -0
  74. package/test/workflow-builtin-candidate.test.ts +230 -0
  75. package/test/workflow-builtin.test.ts +197 -0
  76. package/test/workflow-cancel-agent.test.ts +144 -0
  77. package/test/workflow-cancel.test.ts +166 -0
  78. package/test/workflow-chain.test.ts +84 -0
  79. package/test/workflow-check-options.test.ts +144 -0
  80. package/test/workflow-check-uses.test.ts +148 -0
  81. package/test/workflow-check.test.ts +282 -0
  82. package/test/workflow-contract.test.ts +295 -0
  83. package/test/workflow-crash-resume.test.ts +141 -0
  84. package/test/workflow-deadline.test.ts +102 -0
  85. package/test/workflow-describe-cache.test.ts +240 -0
  86. package/test/workflow-directory.test.ts +73 -0
  87. package/test/workflow-emits.test.ts +261 -0
  88. package/test/workflow-group-keys.test.ts +72 -0
  89. package/test/workflow-harnesses.test.ts +262 -0
  90. package/test/workflow-inspect.test.ts +152 -0
  91. package/test/workflow-language-types.test.ts +148 -0
  92. package/test/workflow-manifest.test.ts +93 -0
  93. package/test/workflow-manual-pause.test.ts +116 -0
  94. package/test/workflow-owners.test.ts +154 -0
  95. package/test/workflow-pause.test.ts +200 -0
  96. package/test/workflow-phases.test.ts +445 -0
  97. package/test/workflow-plugin-entries.test.ts +59 -0
  98. package/test/workflow-record-refs.test.ts +310 -0
  99. package/test/workflow-replay.test.ts +206 -0
  100. package/test/workflow-resume-once.test.ts +188 -0
  101. package/test/workflow-revision.test.ts +216 -0
  102. package/test/workflow-scope.test.ts +505 -0
  103. package/test/workflow-script-defaults.test.ts +119 -0
  104. package/test/workflow-script-error.test.ts +228 -0
  105. package/test/workflow-script-libraries.test.ts +88 -0
  106. package/test/workflow-script-packages.test.ts +119 -0
  107. package/test/workflow-stdout.test.ts +89 -0
  108. package/test/workflow-stop.test.ts +299 -0
  109. package/test/workflow-store.test.ts +220 -0
  110. package/test/workflow-visible.test.ts +136 -0
  111. package/test/workflow-wait-version.test.ts +54 -0
  112. package/test/workflow-wait.test.ts +224 -0
  113. package/test/workflow-waiting-agent.test.ts +64 -0
  114. package/tsconfig.json +15 -0
@@ -0,0 +1,240 @@
1
+ // What a script declares is read once for one version of its code (the hash of its source closure): a second
2
+ // check, a registration or a start of the same code spawns no child, an edit of the script or of a file it imports
3
+ // is read again, a registered version is known from the journal, and the modules read at once are bounded.
4
+
5
+ import { afterEach, expect, test } from "bun:test";
6
+ import { readFileSync, writeFileSync } from "node:fs";
7
+ import { mkdir, writeFile } from "node:fs/promises";
8
+ import { join } from "node:path";
9
+
10
+ import { seq } from "@niwa-labs/core";
11
+ import type { JournalReader, StoredRecord } from "@niwa-labs/core";
12
+ import { createNiwa, requests, eventually, tempDir, tempHome, timers } from "@niwa-labs/testing";
13
+ import type { Host } from "@niwa-labs/testing";
14
+ import { workflow, workflowCheck, workflowRegister } from "../src/index.ts";
15
+
16
+ import { describer, readings } from "../src/describe-cache.ts";
17
+ import { describeScript } from "../src/describe.ts";
18
+ import type { DescribeResult } from "../src/describe.ts";
19
+ import { declaredBy, versionOf } from "../src/registry.ts";
20
+ import type { ScriptBox } from "../src/runner.ts";
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
+ async function served(): Promise<{ host: Host; home: string }> {
31
+ const home = await tempHome();
32
+ homes.push(home);
33
+ const host = createNiwa({ home: home.path });
34
+ host.use(requests(), timers([]), workflow());
35
+ await host.serve();
36
+ hosts.push(host);
37
+ return { host, home: home.path };
38
+ }
39
+
40
+ const written = async (dir: string, name: string, lines: readonly string[]): Promise<string> => {
41
+ await mkdir(dir, { recursive: true });
42
+ const file = join(dir, name);
43
+ await writeFile(file, lines.join("\n"));
44
+ return file;
45
+ };
46
+
47
+ /** A script that writes a mark every time its top level runs: the marks are the processes that loaded it. */
48
+ const marking = (counter: string): string[] => [
49
+ 'import { appendFileSync } from "node:fs";',
50
+ 'import { kind } from "./kind.ts";',
51
+ `appendFileSync(${JSON.stringify(counter)}, "x");`,
52
+ "export default workflow({",
53
+ ' description: "Marks every load",',
54
+ ' output: kind === "number" ? z.number() : z.string(),',
55
+ "}, async () => 1);",
56
+ ];
57
+
58
+ const kindFile = (kind: "number" | "string"): string => `export const kind: "number" | "string" = ${JSON.stringify(kind)};\n`;
59
+
60
+ const loads = (counter: string): number => readFileSync(counter, "utf8").length;
61
+
62
+ /** A journal over the records a host wrote, answering the way the module asks it: by the type of a record. */
63
+ const journalOf = (rows: readonly StoredRecord[]): JournalReader => ({
64
+ get: (id) => rows.find((row) => row.id === id),
65
+ since: (from) => rows.filter((row) => row.seq > from),
66
+ find: (query) => rows.filter((row) => row.type === query.type),
67
+ last: () => rows.at(-1)?.seq ?? seq(0),
68
+ });
69
+
70
+ test("a second check of the same code spawns no child, and an edit of the script or of a file it imports is read again", async () => {
71
+ const { host, home } = await served();
72
+ const counter = join(home, "loads");
73
+ writeFileSync(counter, "");
74
+ await writeFile(join(home, "kind.ts"), kindFile("number"));
75
+ const file = await written(home, "marking.ts", marking(counter));
76
+
77
+ const first = await host.call(workflowCheck, { file });
78
+ expect(first.ok).toBe(true);
79
+ expect(loads(counter)).toBe(1);
80
+
81
+ const again = await host.call(workflowCheck, { file });
82
+ expect(again).toEqual(first);
83
+ // Registering is a check too: the same code, so the module is not loaded for it.
84
+ await host.call(workflowRegister, { file });
85
+ expect(loads(counter)).toBe(1);
86
+
87
+ // The schema is in a file the script imports: its edit is another version of the script.
88
+ await writeFile(join(home, "kind.ts"), kindFile("string"));
89
+ const edited = await host.call(workflowCheck, { file });
90
+ expect(edited.output).toEqual({ type: "string" });
91
+ expect(edited.contract).not.toBe(first.contract);
92
+ expect(loads(counter)).toBe(2);
93
+
94
+ // A comment in the script is another version as well.
95
+ await writeFile(file, `${marking(counter).join("\n")}\n// edited\n`);
96
+ await host.call(workflowCheck, { file });
97
+ expect(loads(counter)).toBe(3);
98
+ await host.call(workflowCheck, { file });
99
+ expect(loads(counter)).toBe(3);
100
+ }, 60_000);
101
+
102
+ test("a module that could not be read is read again, and a module read in a box is not taken from one that was not", async () => {
103
+ const dir = await tempDir("describe-cache");
104
+ const file = await written(dir, "one.ts", ["export default 1;"]);
105
+ const box: ScriptBox = { sandbox: "srt", closed: [] };
106
+ const calls: Array<{ boxed: boolean }> = [];
107
+ let outcome: DescribeResult = { failed: "it threw" };
108
+ const describe = describer({
109
+ readings: readings(),
110
+ read: async (_file, boxed) => {
111
+ calls.push({ boxed: boxed !== undefined });
112
+ return outcome;
113
+ },
114
+ });
115
+
116
+ expect(await describe(file)).toEqual({ failed: "it threw" });
117
+ expect(await describe(file)).toEqual({ failed: "it threw" });
118
+ expect(calls).toHaveLength(2);
119
+
120
+ outcome = { described: { description: "one", problems: [] } };
121
+ await describe(file);
122
+ await describe(file);
123
+ expect(calls).toHaveLength(3);
124
+
125
+ await describe(file, box);
126
+ await describe(file, box);
127
+ expect(calls).toEqual([{ boxed: false }, { boxed: false }, { boxed: false }, { boxed: true }]);
128
+ });
129
+
130
+ test("a file that is edited while it is read is not kept under the version it had before", async () => {
131
+ const dir = await tempDir("describe-cache");
132
+ const original = "export default 1;";
133
+ const file = await written(dir, "moving.ts", [original]);
134
+ let reads = 0;
135
+ const describe = describer({
136
+ readings: readings(),
137
+ read: async () => {
138
+ reads += 1;
139
+ if (reads === 1) writeFileSync(file, "export default 2;");
140
+ return { described: { problems: [] } };
141
+ },
142
+ });
143
+ await describe(file);
144
+ // The file is as it was when the first reading began: what that reading saw was not this version.
145
+ writeFileSync(file, original);
146
+ await describe(file);
147
+ expect(reads).toBe(2);
148
+ await describe(file);
149
+ expect(reads).toBe(2);
150
+ });
151
+
152
+ test("a registered version is known from its registration, and what is rebuilt is what the module declares", async () => {
153
+ const { host, home } = await served();
154
+ const file = await written(home, "sweep.ts", [
155
+ 'const Blocked = record("blocked", z.object({ repo: z.string(), reasons: z.array(z.string()) }));',
156
+ "export default workflow({",
157
+ ' name: "sweep",',
158
+ ' description: "Sweep the repos",',
159
+ " input: z.object({ repo: z.string() }),",
160
+ " output: z.number(),",
161
+ " emits: [Blocked],",
162
+ "}, async () => 1);",
163
+ ]);
164
+ const registered = await host.call(workflowRegister, { file });
165
+ const journal = journalOf(await host.records.read());
166
+
167
+ const version = versionOf(file);
168
+ expect(version).toBe(registered.version);
169
+ const rebuilt = declaredBy(journal, registered.version);
170
+ const read = await describeScript(file);
171
+ expect(rebuilt).toEqual("described" in read ? read.described : undefined);
172
+
173
+ let reads = 0;
174
+ const describe = describer({
175
+ readings: readings(),
176
+ known: (known, boxed) => (boxed ? undefined : declaredBy(journal, known)),
177
+ read: async (path, boxed, run) => {
178
+ reads += 1;
179
+ return describeScript(path, boxed, run);
180
+ },
181
+ });
182
+ expect(await describe(file)).toEqual(read);
183
+ expect(reads).toBe(0);
184
+
185
+ // A version the journal does not hold is read.
186
+ await writeFile(file, `${await Bun.file(file).text()}\n// edited\n`);
187
+ await describe(file);
188
+ expect(reads).toBe(1);
189
+ }, 60_000);
190
+
191
+ test("the modules read at once are bounded, and the ask for one version that is being read waits for that reading", async () => {
192
+ const dir = await tempDir("describe-cache");
193
+ const files = await Promise.all(Array.from({ length: 10 }, (_, at) => written(dir, `s${String(at)}.ts`, [`export default ${String(at)};`])));
194
+ const release: Array<() => void> = [];
195
+ let going = 0;
196
+ let peak = 0;
197
+ let started = 0;
198
+ const describe = describer({
199
+ readings: readings(3),
200
+ read: async () => {
201
+ started += 1;
202
+ going += 1;
203
+ peak = Math.max(peak, going);
204
+ await new Promise<void>((done) => release.push(done));
205
+ going -= 1;
206
+ return { described: { problems: [] } };
207
+ },
208
+ });
209
+
210
+ const all = Promise.all(files.map((file) => describe(file)));
211
+ // The same version asked again while it is being read is that reading.
212
+ const same = describe(files[0] as string);
213
+ await eventually(() => release.length === 3);
214
+ expect(started).toBe(3);
215
+
216
+ let ended = 0;
217
+ while (ended < files.length) {
218
+ await eventually(() => release.length > 0);
219
+ release.shift()?.();
220
+ ended += 1;
221
+ await Bun.sleep(1);
222
+ }
223
+ await all;
224
+ await same;
225
+ expect(started).toBe(10);
226
+ expect(peak).toBe(3);
227
+ });
228
+
229
+ test("the readings of the process are shared: a version read for one describer is not read for another", async () => {
230
+ const dir = await tempDir("describe-cache");
231
+ const file = await written(dir, "shared.ts", [`export default ${JSON.stringify(crypto.randomUUID())};`]);
232
+ let reads = 0;
233
+ const read = async (): Promise<DescribeResult> => {
234
+ reads += 1;
235
+ return { described: { problems: [] } };
236
+ };
237
+ await describer({ read })(file);
238
+ await describer({ read })(file);
239
+ expect(reads).toBe(1);
240
+ });
@@ -0,0 +1,73 @@
1
+ // A run belongs to a directory, the project it was started for, and keeps it.
2
+ //
3
+ // 1. a start that names a directory records it: the run list and the inspection show it
4
+ // 2. a resume starts the run again and the run keeps its directory
5
+ // 3. a start that names none has none, and a relative path is refused
6
+ // 4. the run list has one entry per run, in the order the runs began
7
+
8
+ import { afterEach, expect, test } from "bun:test";
9
+ import { writeFile } from "node:fs/promises";
10
+ import { join } from "node:path";
11
+
12
+ import { createNiwa, eventually, requests, tempHome, timers } from "@niwa-labs/testing";
13
+ import type { Host } from "@niwa-labs/testing";
14
+ import { workflow, workflowInspect, workflowResume, workflowRuns, workflowStart, workflowWait } from "../src/index.ts";
15
+
16
+ const homes: Array<{ cleanup(): Promise<void> }> = [];
17
+ const hosts: Host[] = [];
18
+
19
+ afterEach(async () => {
20
+ for (const host of hosts.splice(0)) await host.close().catch(() => {});
21
+ for (const home of homes.splice(0)) await home.cleanup();
22
+ });
23
+
24
+ async function setup(): Promise<{ niwa: Host; file: string }> {
25
+ const home = await tempHome();
26
+ homes.push(home);
27
+ const niwa = createNiwa({ home: home.path });
28
+ niwa.use(requests());
29
+ niwa.use(timers([]));
30
+ niwa.use(workflow());
31
+ await niwa.serve();
32
+ hosts.push(niwa);
33
+ const file = join(home.path, "plain.js");
34
+ await writeFile(file, `export default workflow({ output: z.string() }, async () => { await now({ label: "tick" }); return "done"; });`);
35
+ return { niwa, file };
36
+ }
37
+
38
+ test("a run records the directory it was started for and keeps it through a resume", async () => {
39
+ const { niwa, file } = await setup();
40
+ const { run } = await niwa.call(workflowStart, { file, directory: "/work/project-a", label: "plain" });
41
+ await niwa.call(workflowWait, { run, timeoutMs: 10_000 });
42
+
43
+ const { entries } = await niwa.call(workflowRuns, {});
44
+ expect(entries).toMatchObject([{ run, name: "plain", state: "finished", directory: "/work/project-a" }]);
45
+ expect((await niwa.call(workflowInspect, { run })).origin.directory).toBe("/work/project-a");
46
+
47
+ await niwa.call(workflowResume, { run });
48
+ await eventually(
49
+ async () => (await niwa.call(workflowInspect, { run })).origin.requestedAt !== undefined,
50
+ 10_000,
51
+ "the resumed run should be asked for",
52
+ );
53
+ await niwa.call(workflowWait, { run, timeoutMs: 10_000 });
54
+ expect((await niwa.call(workflowRuns, {})).entries).toMatchObject([{ run, directory: "/work/project-a" }]);
55
+ expect((await niwa.call(workflowInspect, { run })).origin.directory).toBe("/work/project-a");
56
+ }, 30_000);
57
+
58
+ test("a start that names no directory has none, and a relative one is refused", async () => {
59
+ const { niwa, file } = await setup();
60
+ await expect(niwa.call(workflowStart, { file, directory: "relative/dir" })).rejects.toThrow(/absolute path/);
61
+
62
+ const first = await niwa.call(workflowStart, { file, directory: "/work/a" });
63
+ await niwa.call(workflowWait, { run: first.run, timeoutMs: 10_000 });
64
+ const second = await niwa.call(workflowStart, { file });
65
+ await niwa.call(workflowWait, { run: second.run, timeoutMs: 10_000 });
66
+
67
+ const { entries, runs } = await niwa.call(workflowRuns, {});
68
+ expect(entries.map((entry) => entry.run)).toEqual([first.run, second.run]);
69
+ expect(runs).toHaveLength(2);
70
+ expect(entries[0]?.directory).toBe("/work/a");
71
+ expect(entries[1]?.directory).toBeUndefined();
72
+ expect((await niwa.call(workflowInspect, { run: second.run })).origin.directory).toBeUndefined();
73
+ }, 30_000);
@@ -0,0 +1,261 @@
1
+ // Typed emits, waits and reads of a script: `workflow({ emits: [record(name, schema)] })` declares the records a
2
+ // script writes (only those, their data held to the schema, on the host too), and `wait`, `read.records` and the
3
+ // filter of a wait are typed by the record catalogue the trigger typing is made from.
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 { workflow, workflowCheck, workflowGet, workflowRegister, workflowStart } from "../src/index.ts";
15
+ import { z } from "zod";
16
+
17
+ import { emittedTypesOf } from "../src/describe.ts";
18
+
19
+ const homes: Array<{ cleanup(): Promise<void> }> = [];
20
+ const hosts: Host[] = [];
21
+
22
+ afterEach(async () => {
23
+ for (const host of hosts.splice(0)) await host.close().catch(() => {});
24
+ for (const home of homes.splice(0)) await home.cleanup();
25
+ }, 60_000);
26
+
27
+ const Ping = record("ping.sent", z.object({ id: z.string(), size: z.number(), to: z.object({ id: z.string() }) }));
28
+ /** A type the host knows that no script starts on: a wait for it is typed by name. */
29
+ const Pong = record("pong.sent", z.object({ id: z.string(), ok: z.boolean() }));
30
+
31
+ async function served(): Promise<{ host: Host; home: string }> {
32
+ const home = await tempHome();
33
+ homes.push(home);
34
+ const host = createNiwa({ home: home.path });
35
+ host.use(requests(), timers([]), plugin("ping", (api) => api.declare(Ping, Pong)), workflow({ triggers: [Ping] }));
36
+ await host.serve();
37
+ hosts.push(host);
38
+ return { host, home: home.path };
39
+ }
40
+
41
+ async function scriptIn(home: string, name: string, lines: readonly string[]): Promise<string> {
42
+ await mkdir(home, { recursive: true });
43
+ const file = join(home, name);
44
+ await writeFile(file, lines.join("\n"));
45
+ return file;
46
+ }
47
+
48
+ const ofType = async (host: Host, type: string): Promise<StoredRecord[]> => (await host.records.read()).filter((row) => row.type === type);
49
+
50
+ const Blocked = [
51
+ 'const Blocked = record("blocked", z.object({ repo: z.string(), pr: z.number().int(), reasons: z.array(z.string()) }));',
52
+ 'export default workflow({ name: "sweep", emits: [Blocked], output: z.number() }, async () => {',
53
+ ' await emit(Blocked, { repo: "niwa", pr: 7, reasons: ["red"] });',
54
+ " return 1;",
55
+ "});",
56
+ ];
57
+
58
+ test("check lists the records a script emits under wf.<name>., with their schemas, and they are part of the contract", async () => {
59
+ const { host, home } = await served();
60
+ const file = await scriptIn(home, "sweep.ts", Blocked);
61
+ const report = await host.call(workflowCheck, { file });
62
+ expect(report.ok).toBe(true);
63
+ expect(report.emits).toEqual([
64
+ {
65
+ record: "blocked",
66
+ type: "wf.sweep.blocked",
67
+ schema: expect.objectContaining({ type: "object", required: ["repo", "pr", "reasons"] }),
68
+ },
69
+ ]);
70
+ const bare = await scriptIn(home, "bare.ts", ['export default workflow({ name: "sweep", output: z.number() }, async () => 1);']);
71
+ const without = await host.call(workflowCheck, { file: bare });
72
+ expect(without.emits).toBeUndefined();
73
+ expect(without.contract).not.toBe(report.contract);
74
+ expect(emittedTypesOf({ described: { name: "sweep", emits: [{ name: "blocked", schema: { type: "object" } }], problems: [] } })).toEqual([
75
+ { record: "blocked", type: "wf.sweep.blocked", schema: { type: "object" } },
76
+ ]);
77
+ expect(emittedTypesOf({ described: { emits: [{ name: "blocked", schema: { type: "object" } }], problems: [] } })).toEqual([]);
78
+ }, 60_000);
79
+
80
+ test("a run writes the record it declares, as wf.<name>.<record>, with the data its schema parsed", async () => {
81
+ const { host, home } = await served();
82
+ const file = await scriptIn(home, "sweep.ts", Blocked);
83
+ await host.call(workflowStart, { file });
84
+ await eventually(async () => (await ofType(host, "workflow.run.finished")).length === 1, 20_000, "the run should finish");
85
+ expect((await ofType(host, "wf.sweep.blocked")).map((row) => row.data)).toEqual([{ repo: "niwa", pr: 7, reasons: ["red"] }]);
86
+ }, 60_000);
87
+
88
+ test("a record of emits that says nothing is a schema error; emits that is not a list is an emit error", async () => {
89
+ const { host, home } = await served();
90
+ const loose = await scriptIn(home, "loose.ts", [
91
+ 'const Any = record("any", z.object({ value: z.unknown() }));',
92
+ 'export default workflow({ name: "loose", emits: [Any] }, async () => {});',
93
+ ]);
94
+ expect((await host.call(workflowCheck, { file: loose })).errors).toMatchObject([{ code: "schema", line: 2, message: expect.stringContaining('the record "any"') }]);
95
+ const opaque = await scriptIn(home, "opaque.ts", [
96
+ 'const list = [record("one", z.object({}))];',
97
+ 'export default workflow({ name: "opaque", emits: list }, async () => {});',
98
+ ]);
99
+ expect((await host.call(workflowCheck, { file: opaque })).errors).toMatchObject([{ code: "emit", line: 2 }]);
100
+ }, 60_000);
101
+
102
+ test("the host holds the data of an emit to the declared schema, whatever the script process parsed", async () => {
103
+ const { host, home } = await served();
104
+ // A plain JS script swaps the schema of its record after the declaration was read: the child's parse lets anything by.
105
+ const file = await scriptIn(home, "swapped.js", [
106
+ 'const Note = record("note", z.object({ text: z.string() }));',
107
+ 'export default workflow({ name: "swapped", emits: [Note], output: z.string() }, async () => {',
108
+ " Note.schema = z.json();",
109
+ " try {",
110
+ " await emit(Note, { text: 3 });",
111
+ ' return "written";',
112
+ " } catch (error) {",
113
+ " return error.message;",
114
+ " }",
115
+ "});",
116
+ ]);
117
+ await host.call(workflowStart, { file });
118
+ await eventually(async () => (await ofType(host, "workflow.run.finished")).length === 1, 20_000, "the run should finish");
119
+ const finished = (await ofType(host, "workflow.run.finished"))[0]?.data as { outcome: { result: string } };
120
+ expect(finished.outcome.result).toContain("does not pass the record's schema");
121
+ expect(await ofType(host, "wf.swapped.note")).toHaveLength(0);
122
+ }, 60_000);
123
+
124
+ /** The errors of a check, with the line and the code. */
125
+ async function errorsOf(host: Host, file: string): Promise<Array<{ line: number; code: string; message: string }>> {
126
+ return (await host.call(workflowCheck, { file })).errors.map(({ line, code, message }) => ({ line, code, message }));
127
+ }
128
+
129
+ test("emit takes data of the record's schema, and wait and read.records give data typed by the type", async () => {
130
+ const { host, home } = await served();
131
+ const typed = await scriptIn(home, "typed.ts", [
132
+ 'const Note = record("note", z.object({ text: z.string() }));',
133
+ 'export default workflow({ name: "typed", emits: [Note], output: z.number() }, async () => {',
134
+ " const { data: ping } = await wait(\"ping.sent\", { filter: { \"to.id\": \"owner\" } });",
135
+ " const pong = await wait(\"pong.sent\", { timeout: \"1m\" });",
136
+ " const ok: boolean = \"timedOut\" in pong ? false : pong.data.ok;",
137
+ ' const { records: rows } = await read.records({ types: ["ping.sent", "pong.sent"], limit: 2 });',
138
+ ' const sizes = rows.map((row) => (row.type === "ping.sent" ? row.data.size : row.data.ok ? 1 : 0));',
139
+ " await emit(Note, { text: ping.id });",
140
+ " return ping.size + sizes.length + (ok ? 1 : 0);",
141
+ "});",
142
+ ]);
143
+ expect(await errorsOf(host, typed)).toEqual([]);
144
+
145
+ const wrong = await scriptIn(home, "wrong.ts", [
146
+ 'const Note = record("note", z.object({ text: z.string() }));',
147
+ 'export default workflow({ name: "wrong", emits: [Note], output: z.number() }, async () => {',
148
+ " await emit(Note, { text: 1 });",
149
+ ' const { data: ping } = await wait("ping.sent");',
150
+ " const pong = await wait(\"pong.sent\", { timeout: \"1m\" });",
151
+ " if (pong.ok) return 0;",
152
+ ' await wait("no.such.type");',
153
+ " return ping.nothing;",
154
+ "});",
155
+ ]);
156
+ expect((await errorsOf(host, wrong)).map(({ line, code }) => ({ line, code }))).toEqual([
157
+ { line: 3, code: "TS2322" },
158
+ { line: 6, code: "TS2339" },
159
+ { line: 7, code: "TS2769" },
160
+ { line: 8, code: "TS2339" },
161
+ ]);
162
+ }, 60_000);
163
+
164
+ test("the filter of a wait is held to the schema of its type: an unknown path and a literal the field cannot hold are errors, in JS too", async () => {
165
+ const { host, home } = await served();
166
+ const waiting = (filter: string): string[] => [
167
+ "export default workflow({ uses: [\"niwa.records.read\"] }, async () => {",
168
+ ` await wait("ping.sent", { filter: ${filter} });`,
169
+ "});",
170
+ ];
171
+ const typo = await errorsOf(host, await scriptIn(home, "typo.js", waiting('{ idd: "a" }')));
172
+ expect(typo).toEqual([{ line: 2, code: "wait", message: expect.stringContaining('names "idd"') }]);
173
+ const kind = await errorsOf(host, await scriptIn(home, "kind.js", waiting('{ "to.id": 3 }')));
174
+ expect(kind).toEqual([{ line: 2, code: "wait", message: expect.stringContaining("cannot hold") }]);
175
+ // In TypeScript the type checker says it too.
176
+ expect((await errorsOf(host, await scriptIn(home, "typo.ts", waiting('{ idd: "a" }')))).map(({ code }) => code)).toEqual(["wait", "TS2769"]);
177
+ expect(await errorsOf(host, await scriptIn(home, "fine.ts", waiting('{ id: "a", "to.id": "owner" }')))).toEqual([]);
178
+ }, 60_000);
179
+
180
+ test("the records a registered workflow declares join the catalogue: a check made before the registration does not keep its answer", async () => {
181
+ const { host, home } = await served();
182
+ // Nobody has registered `sweep`: its records are of any shape, so a filter on a field of them is not held to anything.
183
+ const waiter = await scriptIn(home, "waiter.ts", [
184
+ 'export default workflow({ description: "Wait for a block", output: z.number() }, async () => {',
185
+ ' const blocked = await wait("wf.sweep.blocked", { filter: { pr: "seven" } });',
186
+ ' return "timedOut" in blocked ? 0 : blocked.data.pr;',
187
+ "});",
188
+ ]);
189
+ const before = await errorsOf(host, waiter);
190
+ expect(before.map(({ code }) => code)).not.toContain("wait");
191
+
192
+ const sweep = await scriptIn(home, "sweep.ts", [
193
+ 'const Blocked = record("blocked", z.object({ repo: z.string(), pr: z.number().int(), reasons: z.array(z.string()) }));',
194
+ 'export default workflow({ name: "sweep", description: "Sweep the pull requests", emits: [Blocked], output: z.number() }, async () => {',
195
+ ' await emit(Blocked, { repo: "niwa", pr: 7, reasons: ["red"] });',
196
+ " return 1;",
197
+ "});",
198
+ ]);
199
+ const registered = await host.call(workflowRegister, { file: sweep });
200
+ expect(registered.added).toBe(true);
201
+ const shown = await host.call(workflowGet, { id: registered.id });
202
+ expect(shown.versions[0]?.emits).toEqual([{ record: "blocked", type: "wf.sweep.blocked", schema: expect.objectContaining({ type: "object" }) }]);
203
+
204
+ // The same script, the same text, on the same host: the answer is the one of the catalogue now.
205
+ const after = await errorsOf(host, waiter);
206
+ expect(after.map(({ code }) => code)).toEqual(["wait", "TS2769"]);
207
+ expect(after[0]?.message).toContain("cannot hold");
208
+
209
+ // A script that waits for the record the right way passes, and a field the type does not have is a type error.
210
+ const fine = await scriptIn(home, "fine.ts", [
211
+ 'export default workflow({ description: "Wait for a block", output: z.number() }, async () => {',
212
+ ' const blocked = await wait("wf.sweep.blocked", { filter: { pr: 7 } });',
213
+ ' return "timedOut" in blocked ? 0 : blocked.data.pr;',
214
+ "});",
215
+ ]);
216
+ expect(await errorsOf(host, fine)).toEqual([]);
217
+ const nothing = await scriptIn(home, "nothing.ts", [
218
+ 'export default workflow({ description: "Wait for a block", output: z.number() }, async () => {',
219
+ ' const blocked = await wait("wf.sweep.blocked");',
220
+ ' return "timedOut" in blocked ? 0 : blocked.data.nothing;',
221
+ "});",
222
+ ]);
223
+ expect((await errorsOf(host, nothing)).map(({ code }) => code)).toEqual(["TS2339"]);
224
+
225
+ // A script that starts on the record is typed by it too: the filter of `on` is held to the schema.
226
+ const reacting = (filter: string): string[] => [
227
+ `export default workflow({ description: "React to a block", on: [{ type: "wf.sweep.blocked", filter: ${filter} }], output: z.number() }, async (trigger) => trigger.data.pr);`,
228
+ ];
229
+ expect(await errorsOf(host, await scriptIn(home, "reacts.ts", reacting('{ pr: 7 }')))).toEqual([]);
230
+ expect((await errorsOf(host, await scriptIn(home, "reacts-wrong.ts", reacting('{ pr: "seven" }')))).length).toBeGreaterThan(0);
231
+ }, 90_000);
232
+
233
+ test("a script reads up to a position and waits on from it: records written in between are not missed", async () => {
234
+ const { host, home } = await served();
235
+ const file = await scriptIn(home, "cursor.ts", [
236
+ 'export default workflow({ uses: ["niwa.records.read"], output: z.json() }, async () => {',
237
+ ' const { records, next } = await read.records({ types: ["ping.sent"], filter: { "to.id": "owner" } });',
238
+ ' await wait("pong.sent", { filter: { id: "go" } });',
239
+ ' const first = await wait("ping.sent", { filter: { "to.id": "owner" }, from: next });',
240
+ ' const second = await wait("ping.sent", { filter: { "to.id": "owner" }, from: first.at });',
241
+ " return { before: records.map((row) => row.data.id), after: [first.data.id, second.data.id], ordered: first.at < second.at };",
242
+ "});",
243
+ ]);
244
+ expect(await errorsOf(host, file)).toEqual([]);
245
+ await host.emit(Ping, { id: "old", size: 1, to: { id: "owner" } });
246
+ const { run } = await host.call(workflowStart, { file });
247
+ await eventually(
248
+ async () => (await host.records.read({ types: ["niwa.invocation.state"], filter: { state: "waiting" } })).length > 0,
249
+ 20_000,
250
+ "the run should wait for the pong",
251
+ );
252
+ // Both pings are written while the script waits for something else: its later waits find them by the position.
253
+ await host.emit(Ping, { id: "one", size: 1, to: { id: "owner" } });
254
+ await host.emit(Ping, { id: "elsewhere", size: 1, to: { id: "else" } });
255
+ await host.emit(Ping, { id: "two", size: 1, to: { id: "owner" } });
256
+ await host.emit(Pong, { id: "go", ok: true });
257
+ await eventually(async () => (await ofType(host, "workflow.run.finished")).length === 1, 20_000, "the run should finish");
258
+ const finished = (await ofType(host, "workflow.run.finished"))[0]?.data as { run: string; outcome: { result: unknown } };
259
+ expect(finished.run).toBe(run);
260
+ expect(finished.outcome.result).toEqual({ before: ["old"], after: ["one", "two"], ordered: true });
261
+ }, 60_000);
@@ -0,0 +1,72 @@
1
+ // Two groups of branches in one script do not share keys.
2
+ //
3
+ // A script that runs one `parallel` after another, or a `parallel` in a loop, makes groups whose
4
+ // branches are numbered from zero each time: the keys of the second group must not be the keys
5
+ // of the first, or its calls are answered with the answers of the first.
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 { counter, eventually, spawnHost, tempHome, watch, connectAsOwner } from "@niwa-labs/testing";
13
+ import { z } from "zod";
14
+
15
+
16
+ const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/revision-host.ts", import.meta.url));
17
+ const RunOutput = z.object({ run: z.string(), finished: z.boolean() });
18
+
19
+ test("two groups of branches in one script have keys of their own", async () => {
20
+ const home = await tempHome();
21
+ const homePath = home.path;
22
+ let host: ReturnType<typeof spawnHost> | undefined;
23
+ let observer: Awaited<ReturnType<typeof watch>> | undefined;
24
+ try {
25
+ host = spawnHost(HOST_SCRIPT, homePath);
26
+ await host.ready();
27
+ const client = await connectAsOwner(homePath);
28
+ observer = await watch(homePath);
29
+ const seen = observer.seen;
30
+
31
+ const script = `${homePath}/branches-groups.js`;
32
+ await Bun.write(
33
+ script,
34
+ [
35
+ 'export default workflow({ uses: ["revision.agent"], output: z.json() }, async () => {',
36
+ ' const one = await parallel([() => ask({}, "a1"), () => ask({}, "a2")]);',
37
+ ' const two = await parallel([() => ask({}, "b1"), () => ask({}, "b2")]);',
38
+ ' const three = await pipeline([() => ask({}, "c1"), (previous) => ask({}, "c2 " + previous.answer)]);',
39
+ ' const four = await pipeline([() => ask({}, "d1"), () => ask({}, "d2")]);',
40
+ " return { one, two, three, four };",
41
+ '});',
42
+ "",
43
+ ].join("\n"),
44
+ );
45
+ RunOutput.parse(await client.call("workflow.start" as never, { file: script } as never));
46
+ await eventually(() => seen.some((r) => r.type === "workflow.run.finished" || r.type === "niwa.invocation.state" && (r.data as { state?: string }).state === "paused"), 30_000, "the run should end");
47
+
48
+ const finished = seen.find((r) => r.type === "workflow.run.finished");
49
+ expect(finished).toBeDefined();
50
+ const answer = (text: string) => ({ answer: `answered: ${text}` });
51
+ expect((finished?.data as { outcome?: { result?: unknown } }).outcome?.result).toEqual({
52
+ one: [answer("a1"), answer("a2")],
53
+ two: [answer("b1"), answer("b2")],
54
+ three: [answer("c1"), answer("c2 answered: c1")],
55
+ four: [answer("d1"), answer("d2")],
56
+ });
57
+ // Eight calls, eight keys; the hold of the grant on each call is a step of its own, under `<key>~auth`.
58
+ const actor = seen.find(r => r.type === "workflow.run.started")?.actor;
59
+ if (actor?.kind !== "handler") throw new Error("the script did not start");
60
+ const keys = seen
61
+ .filter(r => r.type === "niwa.step.intent" && (r.data as { invocation: string }).invocation === actor.invocation)
62
+ .map((r) => (r.data as { key?: string }).key)
63
+ .filter((key) => !key?.endsWith("~auth"));
64
+ expect(new Set(keys).size).toBe(8);
65
+ expect(counter(homePath, "revision.agent").count()).toBe(8);
66
+ await client.close().catch(() => {});
67
+ } finally {
68
+ await observer?.stop();
69
+ await host?.kill().catch(() => {});
70
+ await home.cleanup();
71
+ }
72
+ }, 60_000);