@niwa-labs/host 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2 -2
- package/package.json +16 -4
- package/src/access-actions.ts +193 -0
- package/src/access-control.ts +333 -0
- package/src/call-operations.ts +176 -0
- package/src/cockpit-catalog.ts +99 -0
- package/src/describe.ts +55 -0
- package/src/doors.ts +272 -0
- package/src/host-types.ts +43 -0
- package/src/host.ts +3346 -0
- package/src/index.ts +10 -0
- package/src/plugin-install.ts +249 -0
- package/src/state.ts +261 -0
- package/src/stores.ts +131 -0
- package/test/abandon.test.ts +58 -0
- package/test/action-refusal-restart.test.ts +46 -0
- package/test/action-step.test.ts +363 -0
- package/test/action-wait-restart.test.ts +96 -0
- package/test/builtin-actions.test.ts +21 -0
- package/test/cancel-steps.test.ts +150 -0
- package/test/catch-up.test.ts +378 -0
- package/test/causes.test.ts +138 -0
- package/test/client.test.ts +190 -0
- package/test/cockpit-catalog.test.ts +118 -0
- package/test/crash-between-steps.test.ts +88 -0
- package/test/declared-values.test.ts +175 -0
- package/test/deep-nested-pause.test.ts +147 -0
- package/test/fixtures/action-step-host.ts +166 -0
- package/test/fixtures/action-wait-restart-host.ts +62 -0
- package/test/fixtures/catch-up-host.ts +91 -0
- package/test/fixtures/causes-host.ts +78 -0
- package/test/fixtures/client-host.ts +52 -0
- package/test/fixtures/crash-between-steps-host.ts +71 -0
- package/test/fixtures/declared-values-host.ts +110 -0
- package/test/fixtures/intercept-order-host.ts +112 -0
- package/test/fixtures/intercept-step-host.ts +106 -0
- package/test/fixtures/live-frames-host.ts +81 -0
- package/test/fixtures/long-wait-host.ts +78 -0
- package/test/fixtures/nested-wait-host.ts +120 -0
- package/test/fixtures/no-repeat-crash-host.ts +79 -0
- package/test/fixtures/plugin-replace-host.ts +176 -0
- package/test/fixtures/record-refusal-crash.ts +50 -0
- package/test/fixtures/recovery-contracts-host.ts +79 -0
- package/test/fixtures/repeated-write-host.ts +39 -0
- package/test/fixtures/replay-mismatch-host.ts +66 -0
- package/test/fixtures/single-writer-host.ts +113 -0
- package/test/fixtures/state-host.ts +118 -0
- package/test/fixtures/wait-restart-host.ts +82 -0
- package/test/guards.test.ts +448 -0
- package/test/handler-emit.test.ts +116 -0
- package/test/host.test.ts +114 -0
- package/test/intercept-adjust.test.ts +318 -0
- package/test/intercept-order.test.ts +276 -0
- package/test/intercept-step.test.ts +144 -0
- package/test/large-outcome.test.ts +60 -0
- package/test/live-frames.test.ts +150 -0
- package/test/live-ready.test.ts +61 -0
- package/test/long-wait.test.ts +168 -0
- package/test/nested-outcome.test.ts +75 -0
- package/test/nested-wait.test.ts +111 -0
- package/test/no-repeat-crash.test.ts +132 -0
- package/test/offer-replace.test.ts +39 -0
- package/test/plugin-async-setup.test.ts +22 -0
- package/test/plugin-copies.test.ts +87 -0
- package/test/plugin-emit.test.ts +43 -0
- package/test/plugin-install-failure.test.ts +121 -0
- package/test/plugin-install-hash.test.ts +57 -0
- package/test/plugin-needs.test.ts +80 -0
- package/test/plugin-replace.test.ts +225 -0
- package/test/plugin-uninstall.test.ts +70 -0
- package/test/plugin-version-copies.test.ts +53 -0
- package/test/plugin-version.test.ts +180 -0
- package/test/read-filter.test.ts +136 -0
- package/test/read-query.test.ts +92 -0
- package/test/record-admission.test.ts +294 -0
- package/test/recovery-contracts.test.ts +107 -0
- package/test/repeated-write.test.ts +119 -0
- package/test/replay-mismatch.test.ts +79 -0
- package/test/replay.test.ts +135 -0
- package/test/single-writer.test.ts +267 -0
- package/test/state.test.ts +246 -0
- package/test/step-by-name.test.ts +122 -0
- package/test/stores.test.ts +102 -0
- package/test/stream.test.ts +154 -0
- package/test/subscriber-stream.test.ts +71 -0
- package/test/subscribers.test.ts +101 -0
- package/test/subscriptions.test.ts +632 -0
- package/test/trace-call.test.ts +124 -0
- package/test/trigger-filter.test.ts +42 -0
- package/test/use-many.test.ts +151 -0
- package/test/wait-query.test.ts +117 -0
- package/test/wait-restart.test.ts +187 -0
- package/test/wire-read-access.test.ts +85 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// A handler that gives up its work: `step.abandon()` says the work is
|
|
2
|
+
// no longer wanted, so a step that does not complete after it pauses nothing and the invocation
|
|
3
|
+
// ends as done. A step that fails without it still pauses.
|
|
4
|
+
|
|
5
|
+
import { expect, test } from "bun:test";
|
|
6
|
+
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
|
|
9
|
+
import { action, actionName, record, seq } from "@niwa-labs/core";
|
|
10
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
11
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
12
|
+
import type { ActionName, StoredRecord } from "@niwa-labs/core";
|
|
13
|
+
import { eventually, tempHome } from "@niwa-labs/testing";
|
|
14
|
+
|
|
15
|
+
const Start = record("abandon.start", z.object({ abandon: z.boolean() }));
|
|
16
|
+
const FAIL: ActionName = actionName("abandon.fail");
|
|
17
|
+
const NEST: ActionName = actionName("abandon.nest");
|
|
18
|
+
|
|
19
|
+
const io = { input: z.object({}), output: z.object({}), recovery: "do-not-repeat" as const, effect: "write" as const };
|
|
20
|
+
|
|
21
|
+
/** The states the invocations were written in, in order. */
|
|
22
|
+
async function run(abandon: boolean, call: ActionName): Promise<string[]> {
|
|
23
|
+
const home = await tempHome();
|
|
24
|
+
const niwa = createNiwa({ home: home.path });
|
|
25
|
+
niwa.use(
|
|
26
|
+
plugin("abandon", (api) => {
|
|
27
|
+
api.declare(Start);
|
|
28
|
+
api.action(action(FAIL, { ...io, description: "fails", run: async () => { throw new Error("no"); } }));
|
|
29
|
+
api.action(action(NEST, { ...io, description: "calls a call that fails", run: async (_input, ctx) => ctx.step(FAIL, {}) }));
|
|
30
|
+
api.on(Start, async (data, step) => {
|
|
31
|
+
if (data.abandon) step.abandon();
|
|
32
|
+
await step(call, {});
|
|
33
|
+
});
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
await niwa.serve();
|
|
37
|
+
const records: StoredRecord[] = [];
|
|
38
|
+
void (async () => {
|
|
39
|
+
for await (const stored of niwa.records.since()) records.push(stored);
|
|
40
|
+
})();
|
|
41
|
+
try {
|
|
42
|
+
await niwa.emit(Start, { abandon });
|
|
43
|
+
const states = () =>
|
|
44
|
+
records.filter((stored) => stored.type === "niwa.invocation.state").map((stored) => (stored.data as { state: string }).state);
|
|
45
|
+
await eventually(() => states().some((state) => state !== "running"), 10_000, "the invocation did not end");
|
|
46
|
+
return states();
|
|
47
|
+
} finally {
|
|
48
|
+
await niwa.close();
|
|
49
|
+
await home.cleanup();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
test("a failed step pauses the invocation, and after abandon it does not, whether it is the handler's or a nested one", async () => {
|
|
54
|
+
expect(await run(false, FAIL)).toEqual(["running", "paused"]);
|
|
55
|
+
expect(await run(true, FAIL)).toEqual(["running", "done"]);
|
|
56
|
+
expect(await run(false, NEST)).toEqual(["running", "paused"]);
|
|
57
|
+
expect(await run(true, NEST)).toEqual(["running", "done"]);
|
|
58
|
+
}, 30_000);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import {expect, test} from "bun:test";
|
|
2
|
+
import {fileURLToPath} from "node:url";
|
|
3
|
+
import {CORE_TYPES} from "@niwa-labs/core";
|
|
4
|
+
import {connectAsOwner, counter, eventually, spawnHost, tempHome} from "@niwa-labs/testing";
|
|
5
|
+
import {Start, Wake, Caught, Done} from "./fixtures/record-refusal-crash.ts";
|
|
6
|
+
|
|
7
|
+
const script = fileURLToPath(new URL("./fixtures/record-refusal-crash.ts", import.meta.url));
|
|
8
|
+
|
|
9
|
+
for (const mode of ["escaped", "claim", "handled"] as const) {
|
|
10
|
+
test(`restart after a guarded action refusal: ${mode}`, async () => {
|
|
11
|
+
const home = await tempHome();
|
|
12
|
+
let host = spawnHost(script, home.path);
|
|
13
|
+
let client;
|
|
14
|
+
try {
|
|
15
|
+
await host.ready();
|
|
16
|
+
client = await connectAsOwner(home.path);
|
|
17
|
+
await client.emit(Start, {mode});
|
|
18
|
+
const before = client;
|
|
19
|
+
await eventually(async () => (await before.records.read({types: [
|
|
20
|
+
mode === "escaped" ? Caught : CORE_TYPES.recordRefused,
|
|
21
|
+
]})).length === 1);
|
|
22
|
+
if (mode === "escaped") await eventually(async () => (await before.records.read({types: [CORE_TYPES.invocationState]}))
|
|
23
|
+
.some(row => (row.data as {state: string}).state === "waiting"));
|
|
24
|
+
const [audit] = await client.records.read({types: [CORE_TYPES.recordRefused]});
|
|
25
|
+
if (mode !== "escaped") expect(await client.records.read({types: [CORE_TYPES.stepOutcome]})).toHaveLength(0);
|
|
26
|
+
await client.close();
|
|
27
|
+
await host.kill();
|
|
28
|
+
host = spawnHost(script, home.path);
|
|
29
|
+
await host.ready();
|
|
30
|
+
client = await connectAsOwner(home.path);
|
|
31
|
+
const after = client;
|
|
32
|
+
if (mode === "handled") {
|
|
33
|
+
await eventually(async () => (await after.records.read({types: [CORE_TYPES.invocationState]}))
|
|
34
|
+
.some(row => (row.data as {state: string}).state === "paused"));
|
|
35
|
+
expect(await client.records.read({types: [Caught]})).toHaveLength(0);
|
|
36
|
+
} else {
|
|
37
|
+
await client.emit(Wake, {});
|
|
38
|
+
await eventually(async () => (await after.records.read({types: [Done]})).length === 1);
|
|
39
|
+
expect((await client.records.read({types: [Caught]})).map(row => row.data)).toEqual([{audit: audit!.id}]);
|
|
40
|
+
expect((await client.records.read({types: [CORE_TYPES.stepOutcome]})).map(row => row.data))
|
|
41
|
+
.toContainEqual(expect.objectContaining({key: "call", outcome: {status: "refused", audit: audit!.id}}));
|
|
42
|
+
}
|
|
43
|
+
expect(counter(home.path, "calls").count()).toBe(1);
|
|
44
|
+
} finally { await client?.close(); await host.kill(); await home.cleanup(); }
|
|
45
|
+
}, 20_000);
|
|
46
|
+
}
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
// An action may call another action as a step: waiting is a step too.
|
|
2
|
+
//
|
|
3
|
+
// A handler and an action both make durable calls this way. An action that only waits is
|
|
4
|
+
// recorded under the key `<stepKey>.wait-N` and resumes as waiting after a restart (action-wait-restart.test.ts).
|
|
5
|
+
//
|
|
6
|
+
// The scenario runs against a real host process (fixtures/action-step-host.ts). The plugin
|
|
7
|
+
// declares one record type, two actions and one handler:
|
|
8
|
+
//
|
|
9
|
+
// on Start { n } — the handler calls the action "outer"
|
|
10
|
+
// "outer" — calls the action "inner" through ctx.step, by name
|
|
11
|
+
// "inner" — an effect that counts its calls
|
|
12
|
+
//
|
|
13
|
+
// The checks:
|
|
14
|
+
//
|
|
15
|
+
// 1. a handler calls "outer", "outer" calls "inner" by name, and the answer comes back
|
|
16
|
+
// 2. the host is killed after the nested call's outcome is recorded and before "outer"
|
|
17
|
+
// finishes: after the restart "inner" is not called again, and the invocation ends as done
|
|
18
|
+
// 3. "inner" is cut off mid-call and says "do-not-repeat": the nested step is unknown and the
|
|
19
|
+
// invocation pauses, as a top-level step does
|
|
20
|
+
// 4. a direct client call of "outer" works, and "inner" runs once
|
|
21
|
+
// 5. a nested call of a name nobody serves pauses the invocation on the nested step, even
|
|
22
|
+
// when the code of the action catches the error
|
|
23
|
+
// 6. the outcome of a nested call of a name nobody serves is "failed", and the nested step
|
|
24
|
+
// still takes its key
|
|
25
|
+
//
|
|
26
|
+
// Only the public API is used: the entries of the packages (types) and "@niwa-labs/testing".
|
|
27
|
+
|
|
28
|
+
import { expect, test } from "bun:test";
|
|
29
|
+
import { fileURLToPath } from "node:url";
|
|
30
|
+
|
|
31
|
+
import { actionName, CORE_TYPES, seq } from "@niwa-labs/core";
|
|
32
|
+
import { connect } from "@niwa-labs/client";
|
|
33
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
34
|
+
import { counter, eventually, gate, spawnHost, tempHome, invocationOf, lastState, watch, connectAsOwner } from "@niwa-labs/testing";
|
|
35
|
+
|
|
36
|
+
import { Answered, Start } from "./fixtures/action-step-host.ts";
|
|
37
|
+
|
|
38
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/action-step-host.ts", import.meta.url));
|
|
39
|
+
|
|
40
|
+
/** The type the core writes for the intent of a step. */
|
|
41
|
+
const INTENT_TYPE = CORE_TYPES.stepIntent;
|
|
42
|
+
|
|
43
|
+
/** The type the core writes for the outcome of a step. */
|
|
44
|
+
const OUTCOME_TYPE = CORE_TYPES.stepOutcome;
|
|
45
|
+
|
|
46
|
+
/** The type the core writes for every change of an invocation's state. */
|
|
47
|
+
const STATE_TYPE = CORE_TYPES.invocationState;
|
|
48
|
+
|
|
49
|
+
/** The action a direct call reaches. */
|
|
50
|
+
const OUTER = actionName("outer");
|
|
51
|
+
|
|
52
|
+
/** The action that calls a name nobody serves. */
|
|
53
|
+
const MISSING = actionName("missing");
|
|
54
|
+
|
|
55
|
+
/** The action that asks for the outcome of a name nobody serves. */
|
|
56
|
+
const OUTCOME = actionName("outcome");
|
|
57
|
+
|
|
58
|
+
/** The key a step was recorded under. */
|
|
59
|
+
function keyOf(record: StoredRecord): string {
|
|
60
|
+
return (record.data as { key?: string }).key ?? "";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** The step records of one kind, in the order the journal holds them. */
|
|
64
|
+
async function stepsOf(
|
|
65
|
+
client: Awaited<ReturnType<typeof connect>>,
|
|
66
|
+
type: typeof INTENT_TYPE,
|
|
67
|
+
): Promise<StoredRecord[]> {
|
|
68
|
+
return client.records.read({ types: [type] });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
test("an action that calls another action as a step: the answer comes back and the call is recorded", async () => {
|
|
72
|
+
const home = await tempHome();
|
|
73
|
+
const homePath = home.path;
|
|
74
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
75
|
+
let watcher: Awaited<ReturnType<typeof watch>> | undefined;
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
host = spawnHost(HOST_SCRIPT, homePath, ["repeat"]);
|
|
79
|
+
await host.ready();
|
|
80
|
+
watcher = await watch(homePath);
|
|
81
|
+
const client = await connectAsOwner(homePath);
|
|
82
|
+
|
|
83
|
+
// The gates are opened, so the nested call finishes and the answer comes back.
|
|
84
|
+
gate(homePath, "g-inner").open();
|
|
85
|
+
gate(homePath, "g-outer").open();
|
|
86
|
+
await client.emit(Start, { n: 7 });
|
|
87
|
+
|
|
88
|
+
await eventually(
|
|
89
|
+
async () => (await client.records.read({ types: [Answered.name] })).length === 1,
|
|
90
|
+
15_000,
|
|
91
|
+
"the handler should have written the answer",
|
|
92
|
+
);
|
|
93
|
+
const answers = await client.records.read({ types: [Answered.name] });
|
|
94
|
+
expect(answers[0]?.data).toEqual({ n: 7 });
|
|
95
|
+
|
|
96
|
+
// The nested call is a step of the same invocation: its key belongs to the step that
|
|
97
|
+
// made it, and the intent and the outcome are recorded like those of any other step.
|
|
98
|
+
const intents = await stepsOf(client, INTENT_TYPE);
|
|
99
|
+
const keys = intents.map(keyOf);
|
|
100
|
+
expect(keys).toContain("1");
|
|
101
|
+
expect(keys).toContain("1.step-1");
|
|
102
|
+
const outcomes = await stepsOf(client, OUTCOME_TYPE);
|
|
103
|
+
expect(outcomes.map(keyOf)).toContain("1.step-1");
|
|
104
|
+
|
|
105
|
+
// The inner action ran once.
|
|
106
|
+
expect(counter(homePath, "inner").count()).toBe(1);
|
|
107
|
+
|
|
108
|
+
await client.close().catch(() => {});
|
|
109
|
+
} finally {
|
|
110
|
+
await watcher?.stop();
|
|
111
|
+
await host?.kill().catch(() => {});
|
|
112
|
+
await home.cleanup();
|
|
113
|
+
}
|
|
114
|
+
}, 40_000);
|
|
115
|
+
|
|
116
|
+
test("a crash after the nested call's outcome: the nested call is not made again and the invocation ends as done", async () => {
|
|
117
|
+
const home = await tempHome();
|
|
118
|
+
const homePath = home.path;
|
|
119
|
+
let first: ReturnType<typeof spawnHost> | undefined;
|
|
120
|
+
let second: ReturnType<typeof spawnHost> | undefined;
|
|
121
|
+
let watcher: Awaited<ReturnType<typeof watch>> | undefined;
|
|
122
|
+
|
|
123
|
+
try {
|
|
124
|
+
first = spawnHost(HOST_SCRIPT, homePath, ["repeat"]);
|
|
125
|
+
await first.ready();
|
|
126
|
+
watcher = await watch(homePath);
|
|
127
|
+
const client = await connectAsOwner(homePath);
|
|
128
|
+
|
|
129
|
+
// The inner gate is open, so the nested call finishes and its outcome is recorded. The
|
|
130
|
+
// outer gate stays closed: "outer" does not finish before the crash.
|
|
131
|
+
gate(homePath, "g-inner").open();
|
|
132
|
+
await client.emit(Start, { n: 3 });
|
|
133
|
+
|
|
134
|
+
// The nested call has run: its outcome is recorded, and "outer" has not finished, because
|
|
135
|
+
// the answer the handler writes comes after it.
|
|
136
|
+
await eventually(
|
|
137
|
+
async () =>
|
|
138
|
+
(await client.records.read({ types: [OUTCOME_TYPE] })).some(
|
|
139
|
+
(record) => keyOf(record) === "1.step-1",
|
|
140
|
+
),
|
|
141
|
+
15_000,
|
|
142
|
+
"the outcome of the nested call should be recorded",
|
|
143
|
+
);
|
|
144
|
+
expect(counter(homePath, "inner").count()).toBe(1);
|
|
145
|
+
|
|
146
|
+
// A real crash: SIGKILL, no clean shutdown.
|
|
147
|
+
await first.kill();
|
|
148
|
+
await client.close().catch(() => {});
|
|
149
|
+
|
|
150
|
+
second = spawnHost(HOST_SCRIPT, homePath, ["repeat"]);
|
|
151
|
+
gate(homePath, "g-outer").open();
|
|
152
|
+
await second.ready();
|
|
153
|
+
|
|
154
|
+
// The invocation resumes: the recorded outcome of the nested call is its answer, so the
|
|
155
|
+
// inner action is not called again, and the invocation ends as done.
|
|
156
|
+
await eventually(
|
|
157
|
+
() => lastState(watcher!.seen)?.state === "done",
|
|
158
|
+
15_000,
|
|
159
|
+
"the invocation should have ended as done",
|
|
160
|
+
);
|
|
161
|
+
expect(counter(homePath, "inner").count()).toBe(1);
|
|
162
|
+
expect(lastState(watcher!.seen)?.state).not.toBe("paused");
|
|
163
|
+
} finally {
|
|
164
|
+
await watcher?.stop();
|
|
165
|
+
await first?.kill().catch(() => {});
|
|
166
|
+
await second?.kill().catch(() => {});
|
|
167
|
+
await home.cleanup();
|
|
168
|
+
}
|
|
169
|
+
}, 60_000);
|
|
170
|
+
|
|
171
|
+
test("a nested call that is cut off and says do-not-repeat: the step is unknown and the invocation pauses", async () => {
|
|
172
|
+
const home = await tempHome();
|
|
173
|
+
const homePath = home.path;
|
|
174
|
+
let first: ReturnType<typeof spawnHost> | undefined;
|
|
175
|
+
let second: ReturnType<typeof spawnHost> | undefined;
|
|
176
|
+
let watcher: Awaited<ReturnType<typeof watch>> | undefined;
|
|
177
|
+
|
|
178
|
+
try {
|
|
179
|
+
first = spawnHost(HOST_SCRIPT, homePath, ["do-not-repeat"]);
|
|
180
|
+
await first.ready();
|
|
181
|
+
watcher = await watch(homePath);
|
|
182
|
+
const client = await connectAsOwner(homePath);
|
|
183
|
+
|
|
184
|
+
await client.emit(Start, { n: 5 });
|
|
185
|
+
|
|
186
|
+
// The nested call was cut off: its intent is recorded and its outcome is not.
|
|
187
|
+
await eventually(
|
|
188
|
+
() => counter(homePath, "inner").count() >= 1,
|
|
189
|
+
15_000,
|
|
190
|
+
"the inner action should have been called",
|
|
191
|
+
);
|
|
192
|
+
await eventually(
|
|
193
|
+
async () =>
|
|
194
|
+
(await client.records.read({ types: [INTENT_TYPE] })).some(
|
|
195
|
+
(record) => keyOf(record) === "1.step-1",
|
|
196
|
+
),
|
|
197
|
+
15_000,
|
|
198
|
+
"the intent of the nested call should be recorded",
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
// A real crash in the middle of the nested call.
|
|
202
|
+
await first.kill();
|
|
203
|
+
await client.close().catch(() => {});
|
|
204
|
+
|
|
205
|
+
second = spawnHost(HOST_SCRIPT, homePath, ["do-not-repeat"]);
|
|
206
|
+
await second.ready();
|
|
207
|
+
|
|
208
|
+
// The recovery contract decides: the effect may have happened, so the step is unknown and
|
|
209
|
+
// the invocation pauses, exactly as a top-level step does.
|
|
210
|
+
await eventually(
|
|
211
|
+
() => lastState(watcher!.seen)?.state === "paused",
|
|
212
|
+
15_000,
|
|
213
|
+
"the invocation should have paused",
|
|
214
|
+
);
|
|
215
|
+
// The inner action was not called again: the contract says it may not be.
|
|
216
|
+
expect(counter(homePath, "inner").count()).toBe(1);
|
|
217
|
+
} finally {
|
|
218
|
+
await watcher?.stop();
|
|
219
|
+
await first?.kill().catch(() => {});
|
|
220
|
+
await second?.kill().catch(() => {});
|
|
221
|
+
await home.cleanup();
|
|
222
|
+
}
|
|
223
|
+
}, 60_000);
|
|
224
|
+
|
|
225
|
+
test("a direct call of an action that calls another action: it works and the inner action runs once", async () => {
|
|
226
|
+
const home = await tempHome();
|
|
227
|
+
const homePath = home.path;
|
|
228
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
229
|
+
|
|
230
|
+
try {
|
|
231
|
+
host = spawnHost(HOST_SCRIPT, homePath, ["repeat"]);
|
|
232
|
+
await host.ready();
|
|
233
|
+
const client = await connectAsOwner(homePath);
|
|
234
|
+
|
|
235
|
+
// The call is made outside any invocation: it is not a step and not journaled, and the
|
|
236
|
+
// nested call goes through the same point with the same caller.
|
|
237
|
+
gate(homePath, "g-inner").open();
|
|
238
|
+
gate(homePath, "g-outer").open();
|
|
239
|
+
const answer = await client.call(OUTER, { n: 11 });
|
|
240
|
+
expect(answer).toEqual({ n: 11 });
|
|
241
|
+
expect(counter(homePath, "inner").count()).toBe(1);
|
|
242
|
+
|
|
243
|
+
await client.close().catch(() => {});
|
|
244
|
+
} finally {
|
|
245
|
+
await host?.kill().catch(() => {});
|
|
246
|
+
await home.cleanup();
|
|
247
|
+
}
|
|
248
|
+
}, 40_000);
|
|
249
|
+
|
|
250
|
+
test("a nested call of a name nobody serves: the invocation pauses on the nested step", async () => {
|
|
251
|
+
const home = await tempHome();
|
|
252
|
+
const homePath = home.path;
|
|
253
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
254
|
+
let watcher: Awaited<ReturnType<typeof watch>> | undefined;
|
|
255
|
+
|
|
256
|
+
try {
|
|
257
|
+
host = spawnHost(HOST_SCRIPT, homePath, ["repeat"]);
|
|
258
|
+
await host.ready();
|
|
259
|
+
watcher = await watch(homePath);
|
|
260
|
+
const client = await connectAsOwner(homePath);
|
|
261
|
+
|
|
262
|
+
gate(homePath, "g-inner").open();
|
|
263
|
+
gate(homePath, "g-outer").open();
|
|
264
|
+
await client.emit(Start, { n: 2, mode: "missing" });
|
|
265
|
+
|
|
266
|
+
// The code of the action caught the error, but the invocation still pauses: the nested
|
|
267
|
+
// step is a step of the invocation, and nobody serves that name.
|
|
268
|
+
await eventually(
|
|
269
|
+
() => lastState(watcher!.seen)?.state === "paused",
|
|
270
|
+
15_000,
|
|
271
|
+
"the invocation should have paused on the nested step",
|
|
272
|
+
);
|
|
273
|
+
// The pause names the nested step, not the step of the handler.
|
|
274
|
+
const paused = watcher.seen.filter(
|
|
275
|
+
(record) => record.type === STATE_TYPE && (record.data as { state?: string }).state === "paused",
|
|
276
|
+
);
|
|
277
|
+
expect(JSON.stringify(paused[0]?.data)).toContain("1.step-1");
|
|
278
|
+
} finally {
|
|
279
|
+
await watcher?.stop();
|
|
280
|
+
await host?.kill().catch(() => {});
|
|
281
|
+
await home.cleanup();
|
|
282
|
+
}
|
|
283
|
+
}, 40_000);
|
|
284
|
+
|
|
285
|
+
test("the outcome of a nested call of a name nobody serves is failed, and the nested key is taken", async () => {
|
|
286
|
+
const home = await tempHome();
|
|
287
|
+
const homePath = home.path;
|
|
288
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
289
|
+
let watcher: Awaited<ReturnType<typeof watch>> | undefined;
|
|
290
|
+
|
|
291
|
+
try {
|
|
292
|
+
host = spawnHost(HOST_SCRIPT, homePath, ["repeat"]);
|
|
293
|
+
await host.ready();
|
|
294
|
+
watcher = await watch(homePath);
|
|
295
|
+
const client = await connectAsOwner(homePath);
|
|
296
|
+
|
|
297
|
+
gate(homePath, "g-inner").open();
|
|
298
|
+
gate(homePath, "g-outer").open();
|
|
299
|
+
await client.emit(Start, { n: 4, mode: "outcome" });
|
|
300
|
+
|
|
301
|
+
// The outcome is "failed", so the action goes on and the invocation ends as done.
|
|
302
|
+
await eventually(
|
|
303
|
+
() => lastState(watcher!.seen)?.state === "done",
|
|
304
|
+
15_000,
|
|
305
|
+
"the invocation should have ended as done",
|
|
306
|
+
);
|
|
307
|
+
// The nested step took its key: the call after it keeps its own.
|
|
308
|
+
const intents = await stepsOf(client, INTENT_TYPE);
|
|
309
|
+
expect(intents.map(keyOf)).toEqual(["1"]);
|
|
310
|
+
} finally {
|
|
311
|
+
await watcher?.stop();
|
|
312
|
+
await host?.kill().catch(() => {});
|
|
313
|
+
await home.cleanup();
|
|
314
|
+
}
|
|
315
|
+
}, 40_000);
|
|
316
|
+
|
|
317
|
+
test("a nested step that failed inside an action that must not be repeated: the answer is about the nested step", async () => {
|
|
318
|
+
const home = await tempHome();
|
|
319
|
+
const homePath = home.path;
|
|
320
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
321
|
+
let watcher: Awaited<ReturnType<typeof watch>> | undefined;
|
|
322
|
+
|
|
323
|
+
try {
|
|
324
|
+
host = spawnHost(HOST_SCRIPT, homePath, ["repeat"]);
|
|
325
|
+
await host.ready();
|
|
326
|
+
watcher = await watch(homePath);
|
|
327
|
+
const client = await connectAsOwner(homePath);
|
|
328
|
+
|
|
329
|
+
await client.emit(Start, { n: 1, mode: "careful" });
|
|
330
|
+
await eventually(
|
|
331
|
+
() => lastState(watcher!.seen)?.state === "paused",
|
|
332
|
+
15_000,
|
|
333
|
+
"the invocation should have paused on the nested step",
|
|
334
|
+
);
|
|
335
|
+
expect(lastState(watcher.seen)?.key).toBe("1.step-1");
|
|
336
|
+
expect(counter(homePath, "boom").count()).toBe(1);
|
|
337
|
+
|
|
338
|
+
// The owner says retry about the nested step. The action that made the call was not cut off:
|
|
339
|
+
// it paused, so it is entered again and the nested step is called again, and the invocation
|
|
340
|
+
// pauses on the nested step again - not on the action, whose contract is about a crash.
|
|
341
|
+
const before = watcher.seen.length;
|
|
342
|
+
await client.call(actionName("niwa.invocation.resolve"), {
|
|
343
|
+
invocation: invocationOf(watcher.seen),
|
|
344
|
+
choice: "retry",
|
|
345
|
+
});
|
|
346
|
+
await eventually(
|
|
347
|
+
() => counter(homePath, "boom").count() === 2,
|
|
348
|
+
15_000,
|
|
349
|
+
"the nested step should have been called again",
|
|
350
|
+
);
|
|
351
|
+
await eventually(
|
|
352
|
+
() => watcher!.seen.length > before + 2 && lastState(watcher!.seen)?.state === "paused",
|
|
353
|
+
15_000,
|
|
354
|
+
"the invocation should have paused again",
|
|
355
|
+
);
|
|
356
|
+
expect(lastState(watcher.seen)?.key).toBe("1.step-1");
|
|
357
|
+
await client.close().catch(() => {});
|
|
358
|
+
} finally {
|
|
359
|
+
await watcher?.stop();
|
|
360
|
+
await host?.kill().catch(() => {});
|
|
361
|
+
await home.cleanup();
|
|
362
|
+
}
|
|
363
|
+
}, 40_000);
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// An action that waits inside itself is waiting after a restart, not cut off.
|
|
2
|
+
//
|
|
3
|
+
// 1. spawn the host, emit Start { n }: the handler calls "waiter", a do-not-repeat action that
|
|
4
|
+
// waits inside itself; the invocation becomes "waiting"
|
|
5
|
+
// 2. kill the host with SIGKILL while it waits
|
|
6
|
+
// 3. spawn a second host: the invocation is waiting again, not paused by the recovery contract
|
|
7
|
+
// 4. emit Answer { n }: the action returns, the invocation ends as "done", never "paused"
|
|
8
|
+
|
|
9
|
+
import { expect, test } from "bun:test";
|
|
10
|
+
|
|
11
|
+
import { seq } from "@niwa-labs/core";
|
|
12
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
13
|
+
import { eventually, spawnHost, tempHome, connectAsOwner } from "@niwa-labs/testing";
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
|
|
16
|
+
import { Answer, Start } from "./fixtures/action-wait-restart-host.ts";
|
|
17
|
+
import { fileURLToPath } from "node:url";
|
|
18
|
+
|
|
19
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/action-wait-restart-host.ts", import.meta.url));
|
|
20
|
+
|
|
21
|
+
/** The type the core writes for every change of an invocation's state. */
|
|
22
|
+
const STATE_TYPE = "niwa.invocation.state";
|
|
23
|
+
|
|
24
|
+
const InvocationState = z.object({ invocation: z.string(), state: z.string() });
|
|
25
|
+
|
|
26
|
+
/** The states of the invocation, in the order the journal holds them. */
|
|
27
|
+
function statesOf(records: StoredRecord[]): string[] {
|
|
28
|
+
const states: string[] = [];
|
|
29
|
+
for (const record of records) {
|
|
30
|
+
if (record.type !== STATE_TYPE) continue;
|
|
31
|
+
const parsed = InvocationState.safeParse(record.data);
|
|
32
|
+
if (parsed.success) states.push(parsed.data.state);
|
|
33
|
+
}
|
|
34
|
+
return states;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
test("a do-not-repeat action that waits inside itself keeps waiting across a restart and completes", async () => {
|
|
38
|
+
const home = await tempHome();
|
|
39
|
+
const homePath = home.path;
|
|
40
|
+
const clients: Array<{ close(): Promise<void> }> = [];
|
|
41
|
+
let first: ReturnType<typeof spawnHost> | undefined;
|
|
42
|
+
let second: ReturnType<typeof spawnHost> | undefined;
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
first = spawnHost(HOST_SCRIPT, homePath);
|
|
46
|
+
await first.ready();
|
|
47
|
+
const client = await connectAsOwner(homePath);
|
|
48
|
+
clients.push(client);
|
|
49
|
+
const watcher = await connectAsOwner(homePath);
|
|
50
|
+
clients.push(watcher);
|
|
51
|
+
const seen: StoredRecord[] = [];
|
|
52
|
+
void (async () => {
|
|
53
|
+
for await (const record of watcher.records.since()) seen.push(record);
|
|
54
|
+
})().catch(() => {});
|
|
55
|
+
|
|
56
|
+
await client.emit(Start, { n: 1 });
|
|
57
|
+
await eventually(() => statesOf(seen).includes("waiting"), 10_000, "the action should be waiting");
|
|
58
|
+
|
|
59
|
+
await first.kill();
|
|
60
|
+
first = undefined;
|
|
61
|
+
|
|
62
|
+
second = spawnHost(HOST_SCRIPT, homePath);
|
|
63
|
+
await second.ready();
|
|
64
|
+
const observer = await connectAsOwner(homePath);
|
|
65
|
+
clients.push(observer);
|
|
66
|
+
const history: StoredRecord[] = [];
|
|
67
|
+
void (async () => {
|
|
68
|
+
for await (const record of observer.records.since()) history.push(record);
|
|
69
|
+
})().catch(() => {});
|
|
70
|
+
|
|
71
|
+
// The restarted host takes the step up again as waiting: a second "waiting" after the crash,
|
|
72
|
+
// and no pause.
|
|
73
|
+
await eventually(
|
|
74
|
+
() => statesOf(history).filter((state) => state === "waiting").length >= 2,
|
|
75
|
+
15_000,
|
|
76
|
+
"the restarted host should resume the invocation as waiting",
|
|
77
|
+
);
|
|
78
|
+
expect(statesOf(history)).not.toContain("paused");
|
|
79
|
+
|
|
80
|
+
const answerer = await connectAsOwner(homePath);
|
|
81
|
+
clients.push(answerer);
|
|
82
|
+
await answerer.emit(Answer, { n: 1 });
|
|
83
|
+
|
|
84
|
+
await eventually(
|
|
85
|
+
() => statesOf(history).includes("done"),
|
|
86
|
+
15_000,
|
|
87
|
+
"the invocation should end as done once the answer arrives",
|
|
88
|
+
);
|
|
89
|
+
expect(statesOf(history)).not.toContain("paused");
|
|
90
|
+
} finally {
|
|
91
|
+
for (const client of clients) await client.close().catch(() => {});
|
|
92
|
+
await first?.kill().catch(() => {});
|
|
93
|
+
await second?.kill().catch(() => {});
|
|
94
|
+
await home.cleanup();
|
|
95
|
+
}
|
|
96
|
+
}, 60_000);
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// The actions the host registers for itself are there from the moment the host is created: what
|
|
2
|
+
// niwa describes does not wait for the first plugin.
|
|
3
|
+
|
|
4
|
+
import { expect, test } from "bun:test";
|
|
5
|
+
|
|
6
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
7
|
+
import { tempHome } from "@niwa-labs/testing";
|
|
8
|
+
|
|
9
|
+
test("a host with no plugin describes its own actions", async () => {
|
|
10
|
+
const home = await tempHome();
|
|
11
|
+
const niwa = createNiwa({ home: home.path });
|
|
12
|
+
try {
|
|
13
|
+
const names = (await niwa.describe()).actions.map((entry) => entry.name);
|
|
14
|
+
expect(names).toContain("niwa.records.read");
|
|
15
|
+
expect(names).toContain("plugin.install");
|
|
16
|
+
expect(names).toContain("niwa.describe");
|
|
17
|
+
} finally {
|
|
18
|
+
await niwa.close();
|
|
19
|
+
await home.cleanup();
|
|
20
|
+
}
|
|
21
|
+
});
|