@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,116 @@
1
+ import { expect, test } from "bun:test";
2
+ import { writeFile } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+
6
+ import { RequestCreated, requestAnswer } from "@niwa-labs/requests/records";
7
+ import { counter, eventually, gate, spawnHost, tempHome, connectAsOwner, watch } from "@niwa-labs/testing";
8
+ import { workflowContinue, workflowInspect, workflowPause, workflowStart, workflowWait } from "../src/index.ts";
9
+
10
+
11
+
12
+ test("manual pause drains admitted parallel operations and survives restart without replaying side effects", async () => {
13
+ const home = await tempHome();
14
+ const hostScript = fileURLToPath(new URL("./fixtures/bridge-host.ts", import.meta.url));
15
+ let host = spawnHost(hostScript, home.path);
16
+ let observed: Awaited<ReturnType<typeof watch>> | undefined;
17
+ let client: Awaited<ReturnType<typeof connectAsOwner>> | undefined;
18
+ try {
19
+ await host.ready();
20
+ const owner = client = await connectAsOwner(home.path);
21
+ observed = await watch(home.path);
22
+ const { seen } = observed;
23
+ const { run } = await owner.call(workflowStart, { file: fileURLToPath(new URL("./fixtures/bridge-keys-script.js", import.meta.url)) });
24
+ await eventually(
25
+ () => ["a", "b"].every((name) => counter(home.path, `w1.hold.${name}`).count() === 1),
26
+ 20_000,
27
+ "both calls were admitted",
28
+ );
29
+ const requests = await Promise.all([owner.call(workflowPause, { run }), owner.call(workflowPause, { run })]);
30
+ const pause = requests[0].manualPause?.id;
31
+ expect(pause).toBeDefined();
32
+ expect(requests.map((status) => status.manualPause)).toEqual([
33
+ { id: pause!, state: "requested" },
34
+ { id: pause!, state: "requested" },
35
+ ]);
36
+
37
+ gate(home.path, "w1.hold.b").open();
38
+ const count = (key: string) => seen.filter((row) => row.type === "niwa.step.intent" &&
39
+ (row.data as { key: string }).key.startsWith(key)).length;
40
+ await eventually(() => count("manual-pause:") === 1, 10_000, "run reaches the boundary");
41
+ expect((await owner.call(workflowInspect, { run })).status.manualPause?.state).toBe("requested");
42
+ expect(count("p1.1.call-2")).toBe(0);
43
+
44
+ gate(home.path, "w1.hold.a").open();
45
+ expect((await owner.call(workflowWait, { run, timeoutMs: 5_000 })).manualPause).toEqual({ id: pause!, state: "paused" });
46
+ expect(count("p1.0.call-2")).toBe(0);
47
+ expect(count("p1.1.call-2")).toBe(0);
48
+
49
+ await host.kill();
50
+ await owner.close().catch(() => {});
51
+ host = spawnHost(hostScript, home.path);
52
+ await host.ready();
53
+ const again = client = await connectAsOwner(home.path);
54
+ expect((await again.call(workflowInspect, { run })).status.manualPause?.id).toBe(pause);
55
+ expect((await again.call(workflowContinue, { run, pause: "stale" })).manualPause?.id).toBe(pause);
56
+ const continued = await again.call(workflowContinue, { run, pause: pause! });
57
+ expect(continued.manualPause).toBeUndefined();
58
+
59
+ expect((await again.call(workflowWait, { run, timeoutMs: 10_000 })).outcome?.status).toBe("done");
60
+ expect(["a", "b"].map((name) => counter(home.path, `w1.hold.${name}`).count())).toEqual([1, 1]);
61
+ expect(count("manual-pause:")).toBe(1);
62
+ } finally {
63
+ await observed?.stop();
64
+ await client?.close().catch(() => {});
65
+ await host.kill().catch(() => {});
66
+ await home.cleanup();
67
+ }
68
+ }, 60_000);
69
+
70
+ test("an answered question stays recorded while its workflow is manually paused", async () => {
71
+ const home = await tempHome();
72
+ const host = spawnHost(fileURLToPath(new URL("./fixtures/bridge-host.ts", import.meta.url)), home.path);
73
+ let observed: Awaited<ReturnType<typeof watch>> | undefined;
74
+ let owner: Awaited<ReturnType<typeof connectAsOwner>> | undefined;
75
+ try {
76
+ await host.ready();
77
+ owner = await connectAsOwner(home.path);
78
+ observed = await watch(home.path);
79
+ const { seen } = observed;
80
+ const file = join(home.path, "ask.js");
81
+ await writeFile(file, `export default workflow({ output: z.boolean() }, async () => await ask.confirm("Go on?"));`);
82
+ const { run } = await owner.call(workflowStart, { file });
83
+ await eventually(
84
+ () => seen.some((row) => row.type === "niwa.invocation.state" &&
85
+ (row.data as { state: string }).state === "waiting"),
86
+ 15_000,
87
+ "the run should park on its question",
88
+ );
89
+ const asked = RequestCreated.schema.parse(seen.find((row) => row.type === "request.created")?.data);
90
+ const status = await owner.call(workflowPause, { run });
91
+ expect(status.requests).toEqual([asked.id]);
92
+ expect(status.state).toBe("paused");
93
+
94
+ await owner.call(requestAnswer, { id: asked.id, answer: { ids: ["yes"] } });
95
+ await eventually(
96
+ () => seen.some((row) => row.type === "niwa.step.intent" &&
97
+ (row.data as { key: string }).key.startsWith("manual-pause:")),
98
+ 10_000,
99
+ "the answer should wake the invocation, which should stop at the pause boundary",
100
+ );
101
+ expect(seen.some((row) => row.type === "workflow.run.finished")).toBe(false);
102
+ expect((await owner.call(workflowInspect, { run })).status.requests).toEqual([]);
103
+
104
+ await owner.call(workflowContinue, { run, pause: status.manualPause!.id });
105
+ expect((await owner.call(workflowWait, { run, timeoutMs: 10_000 })).outcome).toEqual({
106
+ status: "done",
107
+ result: true,
108
+ });
109
+ expect(seen.filter((row) => row.type === "request.created")).toHaveLength(1);
110
+ } finally {
111
+ await observed?.stop();
112
+ await owner?.close().catch(() => {});
113
+ await host.kill().catch(() => {});
114
+ await home.cleanup();
115
+ }
116
+ }, 45_000);
@@ -0,0 +1,154 @@
1
+ // Runs and installations have owners: niwa issues the id of a run,
2
+ // and resume, revise and cancel of a run, and uninstall of an installation, are
3
+ // for whoever started or installed it, and for the owner.
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 { actionName, grant, principalOf, seq } from "@niwa-labs/core";
10
+ import type { RuleSpec, StoredRecord } from "@niwa-labs/core";
11
+ import { connect, createNiwa, requests, timers, eventually, tempHome, connectAsOwner, StubFinished, WfNote, stub } from "@niwa-labs/testing";
12
+ import type { Host, NiwaApi } from "@niwa-labs/testing";
13
+ import { workflow } from "../src/index.ts";
14
+
15
+
16
+ const START = actionName("workflow.start");
17
+ const RESUME = actionName("workflow.resume");
18
+ const REVISE = actionName("workflow.revise");
19
+ const CANCEL = actionName("workflow.cancel");
20
+ const INSTALL = actionName("workflow.install");
21
+ const UNINSTALL = actionName("workflow.uninstall");
22
+ const INSTALLED = actionName("workflow.installation.list");
23
+ const FINISH = actionName("stub.finish");
24
+
25
+ const NOT_YOURS = "whoever made it";
26
+
27
+ const homes: Array<{ cleanup(): Promise<void> }> = [];
28
+ const hosts: Host[] = [];
29
+ const clients: NiwaApi[] = [];
30
+
31
+ afterEach(async () => {
32
+ for (const client of clients.splice(0)) await client.close().catch(() => {});
33
+ for (const host of hosts.splice(0)) await host.close().catch(() => {});
34
+ for (const home of homes.splice(0)) await home.cleanup();
35
+ });
36
+
37
+ async function served(): Promise<{ host: Host; home: string }> {
38
+ const home = await tempHome();
39
+ homes.push(home);
40
+ const host = createNiwa({ home: home.path });
41
+ host.use(requests(), timers([]), stub(home.path));
42
+ host.use(workflow({ triggers: [StubFinished], emits: [WfNote] }));
43
+ await host.serve();
44
+ hosts.push(host);
45
+ return { host, home: home.path };
46
+ }
47
+
48
+ async function holding(host: Host, home: string, name: string, rules: readonly RuleSpec[]): Promise<NiwaApi> {
49
+ const secret = host.issueToken(principalOf("session", name), { grant: grant(rules) });
50
+ const client = await connect({ home, id: `session:${name}`, token: secret });
51
+ clients.push(client);
52
+ return client;
53
+ }
54
+
55
+ async function ownerOf(home: string): Promise<NiwaApi> {
56
+ const client = await connectAsOwner(home);
57
+ clients.push(client);
58
+ return client;
59
+ }
60
+
61
+ async function scriptIn(home: string, name: string, lines: readonly string[]): Promise<string> {
62
+ const file = join(home, name);
63
+ await mkdir(home, { recursive: true });
64
+ await writeFile(file, lines.join("\n"));
65
+ return file;
66
+ }
67
+
68
+ const ofType = (records: readonly StoredRecord[], type: string): StoredRecord[] => records.filter((row) => row.type === type);
69
+ const recordsOf = (host: Host): Promise<StoredRecord[]> => host.records.read();
70
+ const finished = async (host: Host): Promise<StoredRecord[]> => ofType(await recordsOf(host), "workflow.run.finished");
71
+
72
+ /** How many runs wait for a record now. */
73
+ async function waiting(host: Host): Promise<number> {
74
+ const latest = new Map<string, string>();
75
+ for (const row of ofType(await recordsOf(host), "niwa.invocation.state")) {
76
+ const data = row.data as { invocation: string; state: string };
77
+ if (data.invocation.startsWith("inv:workflow/workflow.run.requested")) latest.set(data.invocation, data.state);
78
+ }
79
+ return [...latest.values()].filter((state) => state === "waiting").length;
80
+ }
81
+
82
+ const FOREVER = [
83
+ 'export default workflow({ uses: ["niwa.records.read"], output: z.json() }, async () => { return await wait("never.happens"); });',
84
+ ];
85
+ const WORK = ["workflow.start", "workflow.resume", "workflow.revise", "workflow.cancel", "niwa.records.read"] as const;
86
+
87
+ test("niwa issues the id of every run: two starts of one script are two runs run-…", async () => {
88
+ const { host, home } = await served();
89
+ const starter = await holding(host, home, "starter", WORK);
90
+ const file = await scriptIn(home, "forever.js", FOREVER);
91
+ const first = (await starter.call(START, { file })) as { run: string };
92
+ const second = (await starter.call(START, { file })) as { run: string };
93
+ expect(first.run).toMatch(/^run-[0-9a-z]{6,8}$/);
94
+ expect(second.run).toMatch(/^run-[0-9a-z]{6,8}$/);
95
+ expect(second.run).not.toBe(first.run);
96
+ await eventually(async () => (await waiting(host)) === 2, 20_000);
97
+ for (const run of [first.run, second.run]) await starter.call(CANCEL, { run });
98
+ expect(ofType(await recordsOf(host), "workflow.run.requested")).toHaveLength(2);
99
+ }, 30_000);
100
+
101
+ test("a run is cancelled, resumed and revised by whoever started it and by the owner, and by no other client", async () => {
102
+ const { host, home } = await served();
103
+ const starter = await holding(host, home, "starter", WORK);
104
+ const other = await holding(host, home, "other", WORK);
105
+ const owner = await ownerOf(home);
106
+ const file = await scriptIn(home, "forever.js", FOREVER);
107
+ const { run } = (await starter.call(START, { file })) as { run: string };
108
+ await eventually(async () => (await waiting(host)) === 1, 20_000);
109
+
110
+ await expect(other.call(CANCEL, { run })).rejects.toThrow(NOT_YOURS);
111
+ await expect(other.call(RESUME, { run })).rejects.toThrow(NOT_YOURS);
112
+ await expect(other.call(REVISE, { run, file })).rejects.toThrow(NOT_YOURS);
113
+ expect(await finished(host)).toHaveLength(0);
114
+
115
+ // The ones who may act get as far as the run itself: it is still going, so it is not started again.
116
+ await expect(starter.call(RESUME, { run })).rejects.toThrow("still going");
117
+ await expect(owner.call(REVISE, { run, file })).rejects.toThrow("still going");
118
+
119
+ await owner.call(CANCEL, { run });
120
+ expect(await finished(host)).toHaveLength(1);
121
+
122
+ const second = (await starter.call(START, { file })) as { run: string };
123
+ await eventually(async () => (await waiting(host)) === 2, 20_000);
124
+ await starter.call(CANCEL, { run: second.run });
125
+ expect(await finished(host)).toHaveLength(2);
126
+ }, 30_000);
127
+
128
+ test("an installation is uninstalled by whoever installed it and by the owner, and by no other client; its runs are for the installer", async () => {
129
+ const { host, home } = await served();
130
+ const installer = await holding(host, home, "installer", ["workflow.install", "workflow.uninstall", "workflow.cancel", "niwa.records.read"]);
131
+ const other = await holding(host, home, "other", ["workflow.uninstall", "workflow.cancel", "workflow.installation.list"]);
132
+ const owner = await ownerOf(home);
133
+ const file = await scriptIn(home, "installed.js", [
134
+ 'export default workflow({ on: ["stub.finished"], uses: ["niwa.records.read"], output: z.json() }, async () => { return await wait("never.happens"); });',
135
+ ]);
136
+ const { id } = (await installer.call(INSTALL, { file, description: "mine" })) as { id: string };
137
+ await owner.call(FINISH, { id: "go" });
138
+ await eventually(async () => (await waiting(host)) === 1, 20_000);
139
+ const { run } = (ofType(await recordsOf(host), "workflow.run.started")[0]?.data ?? {}) as { run: string };
140
+
141
+ // The run of an installation is its installer's, as the run a client started is the client's.
142
+ await expect(other.call(CANCEL, { run })).rejects.toThrow(NOT_YOURS);
143
+ await installer.call(CANCEL, { run });
144
+ expect(await finished(host)).toHaveLength(1);
145
+
146
+ await expect(other.call(UNINSTALL, { id })).rejects.toThrow(NOT_YOURS);
147
+ expect(((await other.call(INSTALLED, {})) as { items: unknown[] }).items).toHaveLength(1);
148
+ await installer.call(UNINSTALL, { id });
149
+
150
+ const again = (await installer.call(INSTALL, { file, description: "mine" })) as { id: string };
151
+ expect(again.id).not.toBe(id);
152
+ await owner.call(UNINSTALL, { id: again.id });
153
+ expect(((await owner.call(INSTALLED, {})) as { items: unknown[] }).items).toEqual([]);
154
+ }, 30_000);
@@ -0,0 +1,200 @@
1
+ // A failure pauses the run with a request, and the owner
2
+ // answers that request with one of the choices the pause offers.
3
+ //
4
+ // The scenario runs against a real host process (test/fixtures/revision-host.ts), which installs the
5
+ // requests module next to the workflow:
6
+ //
7
+ // 1. the script makes one call that finishes and one that fails: the run pauses
8
+ // 2. the pause is on the owner's list of open requests, with the choices retry, skip, cancel
9
+ // 3. the owner answers "skip": the run goes on without the failed call, and finishes
10
+ // 4. the owner answers "cancel": the run ends there, and the failed call is never made
11
+ //
12
+ // Only the public API is used: the entries of the packages, and of testing.
13
+
14
+ import { expect, test } from "bun:test";
15
+ import { fileURLToPath } from "node:url";
16
+
17
+ import type { ActionName, StoredRecord } from "@niwa-labs/core";
18
+ import { REQUEST_OPEN } from "@niwa-labs/requests/records";
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/revision-host.ts", import.meta.url));
24
+ const SCRIPT = fileURLToPath(new URL("./fixtures/revision-script.js", import.meta.url));
25
+ const RESOLVE = "niwa.invocation.resolve" as ActionName;
26
+
27
+ /** What the workflow actions answer. */
28
+ const RunOutput = z.object({ run: z.string(), finished: z.boolean() });
29
+
30
+ /** The list of what waits: the open requests and the invocations paused for a decision. */
31
+ const Listed = z.object({ requests: z.array(z.record(z.string(), z.unknown())) });
32
+
33
+ /** The state of an invocation, as the journal holds it. */
34
+ const InvocationState = z.object({
35
+ invocation: z.string(),
36
+ state: z.string(),
37
+ reason: z.string().optional(),
38
+ choices: z.array(z.string()).optional(),
39
+ key: z.string().optional(),
40
+ });
41
+
42
+ /** The states of the invocations, in the order they happened. */
43
+ function statesOf(seen: StoredRecord[]): Array<z.infer<typeof InvocationState>> {
44
+ const states: Array<z.infer<typeof InvocationState>> = [];
45
+ for (const record of seen) {
46
+ if (record.type !== "niwa.invocation.state") continue;
47
+ const parsed = InvocationState.safeParse(record.data);
48
+ if (parsed.success) states.push(parsed.data);
49
+ }
50
+ return states;
51
+ }
52
+
53
+ /** The last state of an invocation. */
54
+ function lastStateOf(
55
+ seen: StoredRecord[],
56
+ invocation: string,
57
+ ): z.infer<typeof InvocationState> | undefined {
58
+ let last: z.infer<typeof InvocationState> | undefined;
59
+ for (const state of statesOf(seen)) {
60
+ if (state.invocation === invocation) last = state;
61
+ }
62
+ return last;
63
+ }
64
+
65
+ /** The paused invocations on the owner's list of what waits. */
66
+ function pausedEntries(listed: z.infer<typeof Listed>): Array<Record<string, unknown>> {
67
+ return listed.requests.filter((entry) => entry.kind === "paused");
68
+ }
69
+
70
+ /** The records of one type, as the observer saw them. */
71
+ function ofType(seen: StoredRecord[], type: string): StoredRecord[] {
72
+ return seen.filter((record) => record.type === type);
73
+ }
74
+
75
+ test("rq-4: a failed call pauses the run, and the pause is a request the owner answers", async () => {
76
+ const home = await tempHome();
77
+ const homePath = home.path;
78
+ let host: ReturnType<typeof spawnHost> | undefined;
79
+ let observed: Awaited<ReturnType<typeof watch>> | undefined;
80
+
81
+ try {
82
+ host = spawnHost(HOST_SCRIPT, homePath);
83
+ await host.ready();
84
+ const client = await connectAsOwner(homePath);
85
+ observed = await watch(homePath);
86
+ const seen = observed.seen;
87
+
88
+ // 1. The script runs: the first call finishes, the second fails and the run pauses.
89
+ const started = RunOutput.parse(
90
+ await client.call("workflow.start" as never, { file: SCRIPT } as never),
91
+ );
92
+
93
+ await eventually(
94
+ () => statesOf(seen).some((state) => state.state === "paused"),
95
+ 20_000,
96
+ "the run should have paused",
97
+ );
98
+ const pause = statesOf(seen).find((state) => state.state === "paused");
99
+ if (pause === undefined) throw new Error("the run did not pause");
100
+
101
+ // The pause offers the choices of the core.
102
+ expect(pause.choices).toEqual(["retry", "skip", "cancel"]);
103
+ // It paused on the step of the call that failed.
104
+ expect(pause.key).toBe("label:work:second");
105
+ // The first call finished; the second failed.
106
+ expect(counter(homePath, "revision.agent").count()).toBe(2);
107
+
108
+ // The run says it paused, and it is the run that was started.
109
+ const runPaused = ofType(seen, "workflow.run.paused")[0];
110
+ expect((runPaused?.data as { run?: string }).run).toBe(started.run);
111
+
112
+ // 2. The pause is on the owner's list of open requests, and it is the only thing there.
113
+ const listed = Listed.parse(await client.call(REQUEST_OPEN, {}));
114
+ const paused = pausedEntries(listed);
115
+ expect(paused).toHaveLength(1);
116
+ expect(paused[0]?.invocation).toBe(pause.invocation);
117
+ expect(paused[0]?.choices).toEqual(["retry", "skip", "cancel"]);
118
+
119
+ // 3. The owner answers "skip": the run goes on without the failed call.
120
+ await client.call(RESOLVE, { invocation: pause.invocation, choice: "skip" });
121
+
122
+ await eventually(
123
+ () => ofType(seen, "workflow.run.finished").length > 0,
124
+ 30_000,
125
+ "the run should have finished after the skip",
126
+ );
127
+ const finished = ofType(seen, "workflow.run.finished")[0];
128
+ expect((finished?.data as { run?: string }).run).toBe(started.run);
129
+ // The first call has its answer; the skipped one has none, and the script goes on.
130
+ expect((finished?.data as { outcome?: { result?: unknown } }).outcome?.result).toEqual({
131
+ first: { answer: "answered: Study the core." },
132
+ second: null,
133
+ });
134
+
135
+ // The invocation ends as done after its handler returns, so its state follows the record of
136
+ // the finished run. The test waits for that state instead of reading it at once.
137
+ await eventually(
138
+ () => lastStateOf(seen, pause.invocation)?.state === "done",
139
+ 20_000,
140
+ "the invocation should have ended as done after the skip",
141
+ );
142
+
143
+ // The failed call was not made again, and the run is not paused any more.
144
+ expect(counter(homePath, "revision.agent").count()).toBe(2);
145
+ expect(pausedEntries(Listed.parse(await client.call(REQUEST_OPEN, {})))).toHaveLength(0);
146
+
147
+ await client.close().catch(() => {});
148
+ } finally {
149
+ await observed?.stop();
150
+ await host?.kill().catch(() => {});
151
+ await home.cleanup();
152
+ }
153
+ }, 60_000);
154
+
155
+ test("rq-4: the owner cancels the paused run, and it ends there", async () => {
156
+ const home = await tempHome();
157
+ const homePath = home.path;
158
+ let host: ReturnType<typeof spawnHost> | undefined;
159
+ let observed: Awaited<ReturnType<typeof watch>> | undefined;
160
+
161
+ try {
162
+ host = spawnHost(HOST_SCRIPT, homePath);
163
+ await host.ready();
164
+ const client = await connectAsOwner(homePath);
165
+ observed = await watch(homePath);
166
+ const seen = observed.seen;
167
+
168
+ // The script runs and pauses on the call that failed.
169
+ await client.call("workflow.start" as never, { file: SCRIPT } as never);
170
+ await eventually(
171
+ () => statesOf(seen).some((state) => state.state === "paused"),
172
+ 20_000,
173
+ "the run should have paused",
174
+ );
175
+ const pause = statesOf(seen).find((state) => state.state === "paused");
176
+ if (pause === undefined) throw new Error("the run did not pause");
177
+ expect(counter(homePath, "revision.agent").count()).toBe(2);
178
+
179
+ // The owner answers "cancel": the run ends, and the failed call is never made.
180
+ await client.call(RESOLVE, { invocation: pause.invocation, choice: "cancel" });
181
+
182
+ await eventually(
183
+ () => lastStateOf(seen, pause.invocation)?.state === "done",
184
+ 20_000,
185
+ "the run should have ended after the cancel",
186
+ );
187
+ expect(lastStateOf(seen, pause.invocation)?.reason).toBe("cancelled");
188
+ expect(ofType(seen, "workflow.run.finished")).toHaveLength(0);
189
+ expect(counter(homePath, "revision.agent").count()).toBe(2);
190
+
191
+ // The pause is not on the list of what waits any more.
192
+ expect(pausedEntries(Listed.parse(await client.call(REQUEST_OPEN, {})))).toHaveLength(0);
193
+
194
+ await client.close().catch(() => {});
195
+ } finally {
196
+ await observed?.stop();
197
+ await host?.kill().catch(() => {});
198
+ await home.cleanup();
199
+ }
200
+ }, 60_000);