@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,197 @@
1
+ // Built-in workflows: a plugin offers a script it ships, and the workflow module
2
+ // registers and installs it when the host starts. A restart finds the same ids and makes nothing again, a changed file
3
+ // is a new version of the same workflow, a built-in no longer offered is uninstalled, and the owner neither registers a
4
+ // file under a built-in nor uninstalls one. A plugin offers the timers it needs, and a point nobody reads refuses to serve.
5
+
6
+ import { afterEach, expect, test } from "bun:test";
7
+ import { writeFileSync } from "node:fs";
8
+ import { join } from "node:path";
9
+
10
+ import { record, seq } from "@niwa-labs/core";
11
+ import type { StoredRecord } from "@niwa-labs/core";
12
+ import { BUILTIN_WORKFLOW, TIMER_OFFER, plugin } from "@niwa-labs/plugin";
13
+ import { createNiwa, timer, timerList, timers, eventually, tempDir, tempHome } from "@niwa-labs/testing";
14
+ import type { Host } from "@niwa-labs/testing";
15
+ import { workflow, workflowGet, workflowInstallationGet, workflowInstallationList, workflowList, workflowRegister, workflowUninstall } from "../src/index.ts";
16
+ import { z } from "zod";
17
+
18
+ const Go = record("shipper.go", z.object({ n: z.number() }));
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
+ });
27
+
28
+ /** A plugin that ships the built-in `answer` from a file, or nothing. */
29
+ function shipper(file: string | undefined) {
30
+ return plugin("shipper", (api) => {
31
+ api.declare(Go);
32
+ if (file !== undefined) api.offer(BUILTIN_WORKFLOW, { name: "answer", file, description: "Answer every go", triggers: [Go] });
33
+ });
34
+ }
35
+
36
+ /** The script of the built-in, answering `text`. */
37
+ function script(file: string, text: string): void {
38
+ writeFileSync(file, `export default workflow({ on: ["shipper.go"], output: z.string() }, async () => ${JSON.stringify(text)});\n`);
39
+ }
40
+
41
+ async function served(home: string, file: string | undefined): Promise<Host> {
42
+ const host = createNiwa({ home });
43
+ host.use(shipper(file), workflow());
44
+ await host.serve();
45
+ hosts.push(host);
46
+ return host;
47
+ }
48
+
49
+ async function stop(host: Host): Promise<void> {
50
+ await host.close();
51
+ hosts.splice(hosts.indexOf(host), 1);
52
+ }
53
+
54
+ const ofType = async (host: Host, type: string): Promise<StoredRecord[]> => (await host.records.read()).filter((row) => row.type === type);
55
+
56
+ /** Wait until the built-ins stand as asked: one installation of the built-in, or none. */
57
+ async function settled(host: Host, installed: number): Promise<void> {
58
+ await eventually(async () => (await host.call(workflowInstallationList, {})).items.length === installed, 20_000, "the built-ins did not settle");
59
+ }
60
+
61
+ async function results(host: Host): Promise<unknown[]> {
62
+ return (await ofType(host, "workflow.run.finished")).map((row) => (row.data as { outcome: { result?: unknown } }).outcome.result);
63
+ }
64
+
65
+ test("a built-in is registered and installed at start, marked with its plugin, and runs on its records", async () => {
66
+ const home = await tempHome();
67
+ homes.push(home);
68
+ const file = join((await tempDir("builtin")), "answer.ts");
69
+ script(file, "v1");
70
+ const host = await served(home.path, file);
71
+ await settled(host, 1);
72
+
73
+ const [listed] = (await host.call(workflowList, {})).items;
74
+ expect(listed?.builtin).toEqual({ plugin: "shipper", name: "answer" });
75
+ expect(String(listed?.description)).toBe("Answer every go");
76
+ const got = await host.call(workflowGet, { id: listed?.id ?? "" });
77
+ expect(got.builtin).toEqual({ plugin: "shipper", name: "answer" });
78
+ // The code is kept under the home: the run is of the copy, not of the file the plugin ships.
79
+ expect(got.versions[0]?.stored).toContain(`/workflows/${String(listed?.id)}/`);
80
+ const [installation] = (await host.call(workflowInstallationList, {})).items;
81
+ expect(installation?.builtin).toEqual({ plugin: "shipper", name: "answer" });
82
+ expect(installation?.workflow).toBe(listed?.id);
83
+ expect((await host.call(workflowInstallationGet, { id: installation?.id ?? "" })).builtin).toEqual({ plugin: "shipper", name: "answer" });
84
+ expect(installation?.file).not.toBe(file);
85
+
86
+ await host.emit(Go, { n: 1 });
87
+ await eventually(async () => (await results(host)).length === 1, 20_000, "the built-in did not run");
88
+ expect(await results(host)).toEqual(["v1"]);
89
+ }, 60_000);
90
+
91
+ test("a restart finds the same ids and writes nothing; a changed file is a new version of the same workflow; one no longer offered is uninstalled", async () => {
92
+ const home = await tempHome();
93
+ homes.push(home);
94
+ const file = join((await tempDir("builtin")), "answer.ts");
95
+ script(file, "v1");
96
+
97
+ const first = await served(home.path, file);
98
+ await settled(first, 1);
99
+ const [wf] = (await first.call(workflowList, {})).items;
100
+ const [sub] = (await first.call(workflowInstallationList, {})).items;
101
+ await stop(first);
102
+
103
+ // The same file: the same workflow, the same installation, and not a record more.
104
+ const again = await served(home.path, file);
105
+ await Bun.sleep(300);
106
+ expect((await again.call(workflowList, {})).items.map((one) => String(one.id))).toEqual([String(wf?.id)]);
107
+ expect((await again.call(workflowInstallationList, {})).items.map((one) => String(one.id))).toEqual([String(sub?.id)]);
108
+ expect(await ofType(again, "workflow.builtins.wanted")).toHaveLength(1);
109
+ expect(await ofType(again, "workflow.registered")).toHaveLength(1);
110
+ expect(await ofType(again, "workflow.subscription.installed")).toHaveLength(1);
111
+ await stop(again);
112
+
113
+ // A new build ships another script: a new version of the same workflow, installed in place of the old one.
114
+ script(file, "v2");
115
+ const changed = await served(home.path, file);
116
+ await eventually(async () => (await ofType(changed, "workflow.subscription.installed")).length === 2, 20_000, "the new version was not installed");
117
+ await settled(changed, 1);
118
+ const [newWf] = (await changed.call(workflowList, {})).items;
119
+ expect(newWf?.id).toBe(wf?.id);
120
+ expect(newWf?.versions).toBe(2);
121
+ expect(newWf?.version).not.toBe(wf?.version);
122
+ const [newSub] = (await changed.call(workflowInstallationList, {})).items;
123
+ expect(newSub?.id).not.toBe(sub?.id);
124
+ expect(newSub?.builtin).toEqual({ plugin: "shipper", name: "answer" });
125
+ await changed.emit(Go, { n: 1 });
126
+ await eventually(async () => (await results(changed)).length === 1, 20_000, "the new version did not run");
127
+ expect(await results(changed)).toEqual(["v2"]);
128
+ await stop(changed);
129
+
130
+ // The plugin offers it no more: it is uninstalled, and its registration stays with its runs.
131
+ const removed = await served(home.path, undefined);
132
+ await settled(removed, 0);
133
+ expect((await removed.call(workflowList, {})).items.map((one) => String(one.id))).toEqual([String(wf?.id)]);
134
+ const ends = await ofType(removed, "workflow.subscription.uninstalled");
135
+ expect(ends.at(-1)?.data).toMatchObject({ installation: newSub?.id, reason: "its plugin no longer offers it" });
136
+ }, 90_000);
137
+
138
+ test("a file is not registered under a built-in, and a built-in is not uninstalled by hand", async () => {
139
+ const home = await tempHome();
140
+ homes.push(home);
141
+ const file = join((await tempDir("builtin")), "answer.ts");
142
+ script(file, "v1");
143
+ const host = await served(home.path, file);
144
+ await settled(host, 1);
145
+ const [wf] = (await host.call(workflowList, {})).items;
146
+ const [sub] = (await host.call(workflowInstallationList, {})).items;
147
+
148
+ const other = join((await tempDir("mine")), "mine.ts");
149
+ writeFileSync(other, `export default workflow({ description: "Mine, not the plugin's", output: z.string() }, async () => "mine");\n`);
150
+ await expect(host.call(workflowRegister, { file: other, id: wf?.id ?? "" })).rejects.toThrow(/built-in "answer" of the plugin "shipper"/);
151
+ await expect(host.call(workflowUninstall, { id: sub?.id ?? "" })).rejects.toThrow(/Turn it off with the option of the plugin/);
152
+ // The same file as a workflow of its own is the owner's.
153
+ const own = await host.call(workflowRegister, { file: other });
154
+ expect(own.id).not.toBe(wf?.id);
155
+ expect((await host.call(workflowGet, { id: own.id })).builtin).toBeUndefined();
156
+ }, 60_000);
157
+
158
+ test("the sync is the workflow module's own: a caller cannot install a built-in", async () => {
159
+ const home = await tempHome();
160
+ homes.push(home);
161
+ const host = await served(home.path, undefined);
162
+ await expect(
163
+ host.call("workflow.builtin.sync" as never, { plugin: "shipper", name: "forged", file: "/nowhere.ts", description: "Forged", version: "0".repeat(64) } as never),
164
+ ).rejects.toThrow(/only the workflow module/);
165
+ }, 30_000);
166
+
167
+ test("a plugin installed after the workflow module that offers a built-in is refused: it would never be installed", () => {
168
+ const host = createNiwa({ home: "/tmp/never-served" });
169
+ host.use(workflow());
170
+ expect(() => host.use(shipper("/nowhere.ts"))).toThrow(/install "shipper" before "workflow"/);
171
+ });
172
+
173
+ test("a built-in offered on a host without the workflow module refuses to serve", async () => {
174
+ const home = await tempHome();
175
+ homes.push(home);
176
+ const host = createNiwa({ home: home.path });
177
+ host.use(shipper("/nowhere.ts"));
178
+ await expect(host.serve()).rejects.toMatchObject({ code: "MissingReader", plugin: "shipper", point: "workflow.builtin", reader: "workflow" });
179
+ await host.close();
180
+ }, 30_000);
181
+
182
+ test("a plugin offers the timers it needs; the timers schedule them beside the listed ones, in any order", async () => {
183
+ const home = await tempHome();
184
+ homes.push(home);
185
+ const host = createNiwa({ home: home.path });
186
+ host.use(
187
+ timers([timer.daily("21:00", { id: "listed" })]),
188
+ plugin("needs-a-timer", (api) => api.offer(TIMER_OFFER, timer.every(60_000, { id: "offered", description: "Offered by a plugin" }))),
189
+ );
190
+ await host.serve();
191
+ hosts.push(host);
192
+ const listed = (await host.call(timerList, {})).timers.map((one) => [String(one.name), String(one.description)]);
193
+ expect(listed).toEqual([
194
+ ["listed", "daily 21:00"],
195
+ ["offered", "Offered by a plugin"],
196
+ ]);
197
+ }, 30_000);
@@ -0,0 +1,144 @@
1
+ // A cancelled run leaves nothing for the owner to decide.
2
+ //
3
+ // `workflow.cancel` interrupts the sessions of the run, so the agent turn in flight fails. That
4
+ // failure is a consequence of the cancel: the invocation ends, and no retry/skip/cancel question
5
+ // is put to the owner for a run the owner has just cancelled.
6
+
7
+ import { afterEach, expect, test } from "bun:test";
8
+ import { writeFile } from "node:fs/promises";
9
+ import { join } from "node:path";
10
+
11
+ import { seq } from "@niwa-labs/core";
12
+ import type { StoredRecord } from "@niwa-labs/core";
13
+ import { harnessId, harnessKind, machineId, sessionId } from "@niwa-labs/harness";
14
+ import type { Harness, SessionControl, SessionId } from "@niwa-labs/harness";
15
+ import { requestOpen } from "@niwa-labs/requests/records";
16
+ import { createNiwa, requests, eventually, tempHome, sessions } from "@niwa-labs/testing";
17
+ import type { Host } from "@niwa-labs/testing";
18
+ import { workflow, workflowCancel, workflowStart } from "../src/index.ts";
19
+
20
+ const HARNESS = harnessId("fake-harness");
21
+
22
+ /** A control side whose turn lasts until it is interrupted, and then fails. */
23
+ class InterruptibleControl implements SessionControl {
24
+ asking = false;
25
+ private fail: ((error: Error) => void) | undefined;
26
+
27
+ async start(): Promise<{ session: SessionId }> {
28
+ return { session: sessionId(HARNESS, "session-1") };
29
+ }
30
+
31
+ async resume(): Promise<void> {
32
+ throw new Error("this session cannot be resumed");
33
+ }
34
+
35
+ async send() {
36
+ return { status: "started" as const, turn: 1 };
37
+ }
38
+
39
+ async interrupt(): Promise<void> {
40
+ this.fail?.(new Error("the turn was interrupted"));
41
+ }
42
+
43
+ async ask<T>(): Promise<{ session: SessionId; value: T; at: never }> {
44
+ this.asking = true;
45
+ return await new Promise((_, reject) => {
46
+ this.fail = reject;
47
+ });
48
+ }
49
+
50
+ async *life(): AsyncIterable<never> {
51
+ // The session stays alive until the test ends.
52
+ await new Promise<never>(() => {});
53
+ }
54
+
55
+ async state(): Promise<"running" | "idle" | "unknown"> {
56
+ return "unknown";
57
+ }
58
+ }
59
+
60
+ /** The harness the fake side serves; one that cannot answer natively asks through a request to the session. */
61
+ const instanceOf = (structuredOutput: boolean): Harness => ({
62
+ id: HARNESS,
63
+ kind: harnessKind("fake-kind"),
64
+ machine: machineId("machine-1"),
65
+ home: "/tmp/fake-home",
66
+ sides: {
67
+ reading: { list: true, read: true, watch: false },
68
+ control: { start: true, resume: false, send: true, interrupt: true, ask: true, structuredOutput, launch: { model: false, effort: [], modes: [] } },
69
+ },
70
+ });
71
+
72
+ const homes: Array<{ cleanup(): Promise<void> }> = [];
73
+ const hosts: Host[] = [];
74
+
75
+ afterEach(async () => {
76
+ for (const host of hosts.splice(0)) await host.close().catch(() => {});
77
+ for (const home of homes.splice(0)) await home.cleanup();
78
+ });
79
+
80
+ const recordsOf = async (niwa: Host): Promise<StoredRecord[]> => niwa.records.read();
81
+
82
+ /** Cancel a run in the middle of an agent turn, and see what is left for the owner. */
83
+ async function cancelledMidTurn(structuredOutput: boolean): Promise<void> {
84
+ const home = await tempHome();
85
+ homes.push(home);
86
+ const control = new InterruptibleControl();
87
+ const niwa = createNiwa({ home: home.path });
88
+ niwa.use(requests());
89
+ niwa.use(sessions({ harnesses: [{ instance: instanceOf(structuredOutput), control }], niwaMcp: { command: "niwa", args: ["mcp"] } }));
90
+ niwa.use(workflow({ harnesses: [HARNESS] }));
91
+ await niwa.serve();
92
+ hosts.push(niwa);
93
+
94
+ const file = join(home.path, "script.js");
95
+ await writeFile(file, `export default workflow({ output: z.json() }, async () => { return await ask({ harness: "fake-harness" }, "Work."); });`);
96
+ const { run } = await niwa.call(workflowStart, { file });
97
+ // A harness that answers natively is in its turn; one that does not has asked the session through a request.
98
+ await eventually(
99
+ async () => (structuredOutput ? control.asking : (await niwa.call(requestOpen, {})).requests.length === 1),
100
+ 10_000,
101
+ "the agent turn should be in flight",
102
+ );
103
+
104
+ await niwa.call(workflowCancel, { run });
105
+ // The turn fails because of the cancel; give its failure the time to reach the invocation.
106
+ await eventually(
107
+ async () => (await recordsOf(niwa)).some((row) => row.type === "workflow.run.finished"),
108
+ 10_000,
109
+ "the cancelled run should finish",
110
+ );
111
+ await Bun.sleep(250);
112
+
113
+ const records = await recordsOf(niwa);
114
+ const finished = records.filter((row) => row.type === "workflow.run.finished");
115
+ expect(finished.map((row) => (row.data as { outcome: { status: string } }).outcome.status)).toEqual(["cancelled"]);
116
+ expect(records.filter((row) => row.type === "workflow.run.paused")).toEqual([]);
117
+ const states = records.filter((row) => row.type === "niwa.invocation.state").map((row) => (row.data as { state: string }).state);
118
+ expect(states).not.toContain("paused");
119
+ expect((await niwa.call(requestOpen, {})).requests).toEqual([]);
120
+ // The session of the agent call goes with the run.
121
+ expect(records.filter((row) => row.type === "session.ended").map((row) => (row.data as { reason: string }).reason)).toEqual([`the run ${run} that started it was cancelled`]);
122
+ }
123
+
124
+ test("cancelling a run in the middle of an agent turn leaves no pause and no question", () => cancelledMidTurn(true), 30_000);
125
+
126
+ test("cancelling a run that waits for a session to answer through a request leaves no pause and no question", () => cancelledMidTurn(false), 30_000);
127
+
128
+ test("cancelling a run that asks the owner a question withdraws the question", async () => {
129
+ const home = await tempHome();
130
+ homes.push(home);
131
+ const niwa = createNiwa({ home: home.path });
132
+ niwa.use(requests());
133
+ niwa.use(workflow());
134
+ await niwa.serve();
135
+ hosts.push(niwa);
136
+
137
+ const file = join(home.path, "ask.js");
138
+ await writeFile(file, `export default workflow({ output: z.json() }, async () => { return await ask.confirm("Go on?"); });`);
139
+ const { run } = await niwa.call(workflowStart, { file });
140
+ await eventually(async () => (await niwa.call(requestOpen, {})).requests.length === 1, 10_000, "the owner should be asked");
141
+
142
+ await niwa.call(workflowCancel, { run });
143
+ expect((await niwa.call(requestOpen, {})).requests).toEqual([]);
144
+ }, 30_000);
@@ -0,0 +1,166 @@
1
+ // A cancelled run stops its child process.
2
+ //
3
+ // A cancel stops the child process of the run, and the run ends there: the invocation ends
4
+ // cancelled, and no call of the script is made after the cancel.
5
+ //
6
+ // The scenario runs against a real host process (test/fixtures/cancel-host.ts):
7
+ //
8
+ // 1. the script makes a long call: the run is parked inside it
9
+ // 2. the owner cancels the run: the child process of the run is gone
10
+ // 3. the invocation ends as done, with the reason "cancelled"
11
+ // 4. no further call of the script was made
12
+ //
13
+ // Only the public API is used: the entries of the packages, and of testing.
14
+
15
+ import { expect, test } from "bun:test";
16
+ import { fileURLToPath } from "node:url";
17
+
18
+ import type { ActionName, StoredRecord } from "@niwa-labs/core";
19
+ import { counter, eventually, spawnHost, tempHome, watch, connectAsOwner } from "@niwa-labs/testing";
20
+ import { z } from "zod";
21
+
22
+
23
+ const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/cancel-host.ts", import.meta.url));
24
+ const SCRIPT = fileURLToPath(new URL("./fixtures/cancel-script.js", import.meta.url));
25
+
26
+ /** What the workflow actions answer. */
27
+ const RunOutput = z.object({ run: z.string(), finished: z.boolean() });
28
+
29
+ /** The state of an invocation, as the journal holds it. */
30
+ const InvocationState = z.object({
31
+ invocation: z.string(),
32
+ state: z.string(),
33
+ reason: z.string().optional(),
34
+ choices: z.array(z.string()).optional(),
35
+ key: z.string().optional(),
36
+ });
37
+
38
+ /** The states of the invocations, in the order they happened. */
39
+ function statesOf(seen: StoredRecord[]): Array<z.infer<typeof InvocationState>> {
40
+ const states: Array<z.infer<typeof InvocationState>> = [];
41
+ for (const record of seen) {
42
+ if (record.type !== "niwa.invocation.state") continue;
43
+ const parsed = InvocationState.safeParse(record.data);
44
+ if (parsed.success) states.push(parsed.data);
45
+ }
46
+ return states;
47
+ }
48
+
49
+ /** The last state of an invocation. */
50
+ function lastStateOf(
51
+ seen: StoredRecord[],
52
+ invocation: string,
53
+ ): z.infer<typeof InvocationState> | undefined {
54
+ let last: z.infer<typeof InvocationState> | undefined;
55
+ for (const state of statesOf(seen)) {
56
+ if (state.invocation === invocation) last = state;
57
+ }
58
+ return last;
59
+ }
60
+
61
+ /** The records of one type, as the observer saw them. */
62
+ function ofType(seen: StoredRecord[], type: string): StoredRecord[] {
63
+ return seen.filter((record) => record.type === type);
64
+ }
65
+
66
+ /** Is a process with this pid still there? */
67
+ function alive(pid: number): boolean {
68
+ try {
69
+ process.kill(pid, 0);
70
+ return true;
71
+ } catch {
72
+ return false;
73
+ }
74
+ }
75
+
76
+ /** The child processes of a process, by their parent pid. */
77
+ function childPids(parentPid: number): number[] {
78
+ const pids: number[] = [];
79
+ const listing = Bun.spawnSync(["ps", "ax", "-o", "pid=,ppid="]).stdout.toString();
80
+ for (const line of listing.split("\n")) {
81
+ const [pid, ppid] = line.trim().split(/\s+/);
82
+ if (ppid !== String(parentPid)) continue;
83
+ const parsed = Number(pid);
84
+ if (Number.isInteger(parsed) && parsed > 0) pids.push(parsed);
85
+ }
86
+ return pids;
87
+ }
88
+
89
+ test("a cancelled run stops its child process, and no call is made after it", async () => {
90
+ const home = await tempHome();
91
+ const homePath = home.path;
92
+ let host: ReturnType<typeof spawnHost> | undefined;
93
+ let observed: Awaited<ReturnType<typeof watch>> | undefined;
94
+
95
+ try {
96
+ host = spawnHost(HOST_SCRIPT, homePath);
97
+ await host.ready();
98
+ const client = await connectAsOwner(homePath);
99
+ observed = await watch(homePath);
100
+ const seen = observed.seen;
101
+
102
+ // 1. The script runs: the long call is made, and the run waits for its answer.
103
+ const started = RunOutput.parse(
104
+ await client.call("workflow.start" as ActionName, { file: SCRIPT } as never),
105
+ );
106
+
107
+ await eventually(
108
+ () => counter(homePath, "cancel.agent").count() === 1,
109
+ 20_000,
110
+ "the long call should have been made",
111
+ );
112
+
113
+ // The child process of the run is there: the script is running in it.
114
+ const children = childPids(host.pid);
115
+ expect(children.length).toBeGreaterThan(0);
116
+ const childPid = children[0] as number;
117
+ expect(alive(childPid)).toBe(true);
118
+
119
+ // 2. The owner cancels the run.
120
+ await client.call("workflow.cancel" as ActionName, { run: started.run } as never);
121
+
122
+ // 3. The child process of the run is gone: the cancel stopped it.
123
+ await eventually(
124
+ () => !alive(childPid),
125
+ 20_000,
126
+ "the child process of the cancelled run should be gone",
127
+ );
128
+
129
+ // 4. The run ended as cancelled, and the invocation of the run ended with it.
130
+ const finished = ofType(seen, "workflow.run.finished")[0];
131
+ expect((finished?.data as { run?: string; cancelled?: boolean }).run).toBe(started.run);
132
+ expect((finished?.data as { outcome?: { status?: string } }).outcome?.status).toBe("cancelled");
133
+
134
+ await eventually(
135
+ () => {
136
+ const invocation = (seen.find((record) => record.type === "niwa.invocation.admitted")?.data as
137
+ | { invocation?: string }
138
+ | undefined)?.invocation;
139
+ if (invocation === undefined) return false;
140
+ return lastStateOf(seen, invocation)?.state === "done";
141
+ },
142
+ 20_000,
143
+ "the invocation of the cancelled run should have ended",
144
+ );
145
+ const invocation = (seen.find((record) => record.type === "niwa.invocation.admitted")?.data as
146
+ | { invocation?: string }
147
+ | undefined)?.invocation;
148
+ // The invocation of the run ended: it is done, and it did not pause on the call that was cut
149
+ // off.
150
+ expect(lastStateOf(seen, invocation ?? "")?.state).toBe("done");
151
+ expect(statesOf(seen).some((state) => state.state === "paused")).toBe(false);
152
+
153
+ // 5. No call was made after the cancel: the second call of the script never happened.
154
+ expect(counter(homePath, "cancel.agent").count()).toBe(1);
155
+ const intents = ofType(seen, "niwa.step.intent").filter(
156
+ (record) => (record.data as { action?: string }).action === "cancel.agent",
157
+ );
158
+ expect(intents.length).toBe(1);
159
+
160
+ await client.close().catch(() => {});
161
+ } finally {
162
+ await observed?.stop();
163
+ await host?.kill().catch(() => {});
164
+ await home.cleanup();
165
+ }
166
+ }, 60_000);
@@ -0,0 +1,84 @@
1
+ // Chaining: a script starts on what another script emits (`wf.*`), with no composition setup, and
2
+ // `triggers` takes a type name as well as a type.
3
+
4
+ import { afterEach, expect, test } from "bun:test";
5
+ import { mkdir, writeFile } from "node:fs/promises";
6
+ import { join } from "node:path";
7
+
8
+ import { actionName, seq } from "@niwa-labs/core";
9
+ import type { StoredRecord } from "@niwa-labs/core";
10
+ import { createNiwa, requests, eventually, tempHome, stub } from "@niwa-labs/testing";
11
+ import type { Host } from "@niwa-labs/testing";
12
+ import { workflow } from "../src/index.ts";
13
+
14
+
15
+ const INSTALL = actionName("workflow.install");
16
+ const START = actionName("workflow.start");
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
+ });
25
+
26
+ async function served(triggers?: ReadonlyArray<string>): Promise<{ host: Host; home: string }> {
27
+ const home = await tempHome();
28
+ homes.push(home);
29
+ const host = createNiwa({ home: home.path });
30
+ host.use(requests(), stub(home.path), workflow(triggers === undefined ? {} : { triggers }));
31
+ await host.serve();
32
+ hosts.push(host);
33
+ return { host, home: home.path };
34
+ }
35
+
36
+ async function scriptIn(home: string, name: string, lines: readonly string[]): Promise<string> {
37
+ await mkdir(home, { recursive: true });
38
+ const file = join(home, name);
39
+ await writeFile(file, lines.join("\n"));
40
+ return file;
41
+ }
42
+
43
+ const finished = async (host: Host): Promise<StoredRecord[]> =>
44
+ (await host.records.read()).filter((row) => row.type === "workflow.run.finished");
45
+
46
+ test("a script installed on a wf.* type starts on what another script emits", async () => {
47
+ const { host, home } = await served();
48
+ const ticker = await scriptIn(home, "ticker.js", [
49
+ 'export default workflow({ on: [{ type: "wf.long.tick", filter: { n: 2 } }], output: z.json() }, async (trigger) => { return { got: trigger.data.n }; });',
50
+ ]);
51
+ await host.call(INSTALL, { file: ticker, description: "ticker" });
52
+ const long = await scriptIn(home, "long.js", [
53
+ 'const Tick = record("tick", z.object({ n: z.number() }));',
54
+ 'export default workflow({ name: "long", emits: [Tick], output: z.string() }, async () => { await emit(Tick, { n: 1 }); await emit(Tick, { n: 2 }); return "emitted"; });',
55
+ ]);
56
+ await host.call(START, { file: long });
57
+ await eventually(async () => (await finished(host)).length === 2, 20_000, "the second tick should start the ticker");
58
+ const results = (await finished(host)).map((row) => (row.data as { outcome: { result: unknown } }).outcome.result);
59
+ expect(results).toContainEqual({ got: 2 });
60
+ expect(results).toContain("emitted");
61
+ expect(results).toHaveLength(2);
62
+ }, 30_000);
63
+
64
+ test("triggers takes names: a wf type needs no declaration, a declared type is found, an unknown one is named", async () => {
65
+ const { host } = await served(["wf.long.tick", "stub.finished"]);
66
+ expect(host).toBeDefined();
67
+ const home = await tempHome();
68
+ homes.push(home);
69
+ expect(() => createNiwa({ home: home.path }).use(workflow({ triggers: ["no.such.type"] }))).toThrow(/no\.such\.type/);
70
+ expect(() => createNiwa({ home: home.path }).use(workflow({ triggers: ["no.such.type"] }))).toThrow(/workflow/);
71
+ }, 30_000);
72
+
73
+ test("a script that emits the type it starts on does not start itself again, however many scripts are in the way", async () => {
74
+ const { host, home } = await served();
75
+ const loop = await scriptIn(home, "loop.js", [
76
+ 'const Tick = record("tick", z.object({ n: z.number() }));',
77
+ 'export default workflow({ name: "loop", on: [{ type: "wf.loop.tick" }], emits: [Tick], output: z.json() }, async (trigger) => { await emit(Tick, { n: (trigger?.data?.n ?? 0) + 1 }); return \"x\"; });',
78
+ ]);
79
+ await host.call(INSTALL, { file: loop, description: "loop" });
80
+ await host.call(START, { file: loop });
81
+ await eventually(async () => (await finished(host)).length >= 2, 20_000, "the first tick starts the installation once");
82
+ await Bun.sleep(1500);
83
+ expect(await finished(host)).toHaveLength(2);
84
+ }, 30_000);