@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,276 @@
|
|
|
1
|
+
// Intercept order.
|
|
2
|
+
//
|
|
3
|
+
// The scenario runs against a real host process (fixtures/intercept-order-host.ts):
|
|
4
|
+
//
|
|
5
|
+
// 1. spawn the host, connect a client, watch the journal from the beginning
|
|
6
|
+
// 2. write Start: the handler calls the action with the text "requested"
|
|
7
|
+
// 3. the adjust changes it to "adjusted", the guard sees "adjusted" and allows
|
|
8
|
+
// it, and the action is called with "adjusted"
|
|
9
|
+
// 4. the journal holds the decision: the requested input and the final one
|
|
10
|
+
// 5. the handler calls the action with the text "forbidden": the guard refuses
|
|
11
|
+
// it, the decision is recorded with the reason and with both inputs, and the
|
|
12
|
+
// action is not called
|
|
13
|
+
// 6. the invocation pauses on the refused step, and the owner answers "retry"
|
|
14
|
+
// 7. on the retry the guard is asked again, and this time it allows the call:
|
|
15
|
+
// the action runs with "forbidden"
|
|
16
|
+
//
|
|
17
|
+
// Only the public API is used: the entries of the packages and "@niwa-labs/testing".
|
|
18
|
+
|
|
19
|
+
import { expect, test } from "bun:test";
|
|
20
|
+
import { fileURLToPath } from "node:url";
|
|
21
|
+
|
|
22
|
+
import { action, deny, seq } from "@niwa-labs/core";
|
|
23
|
+
import { connect } from "@niwa-labs/client";
|
|
24
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
25
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
26
|
+
import type { StoredRecord } from "@niwa-labs/core";
|
|
27
|
+
import { z } from "zod";
|
|
28
|
+
import { eventually, spawnHost, tempHome, connectAsOwner } from "@niwa-labs/testing";
|
|
29
|
+
|
|
30
|
+
import {
|
|
31
|
+
ADJUSTED,
|
|
32
|
+
CALL_LOG,
|
|
33
|
+
FORBIDDEN,
|
|
34
|
+
GUARD_LOG,
|
|
35
|
+
REQUESTED,
|
|
36
|
+
Start,
|
|
37
|
+
} from "./fixtures/intercept-order-host.ts";
|
|
38
|
+
|
|
39
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/intercept-order-host.ts", import.meta.url));
|
|
40
|
+
|
|
41
|
+
/** The record type the core writes for what the intercepts decided. */
|
|
42
|
+
const DECISION_TYPE = "niwa.step.decision";
|
|
43
|
+
|
|
44
|
+
/** The record type the core writes for the intent of a step. */
|
|
45
|
+
const INTENT_TYPE = "niwa.step.intent";
|
|
46
|
+
|
|
47
|
+
/** The record type the core writes for the outcome of a step. */
|
|
48
|
+
const OUTCOME_TYPE = "niwa.step.outcome";
|
|
49
|
+
|
|
50
|
+
/** The action the owner calls to answer a paused invocation. */
|
|
51
|
+
const RESOLVE_ACTION = "niwa.invocation.resolve";
|
|
52
|
+
|
|
53
|
+
/** What a decision record holds. */
|
|
54
|
+
type DecisionData = {
|
|
55
|
+
invocation: string;
|
|
56
|
+
key: string;
|
|
57
|
+
attempt: number;
|
|
58
|
+
requested: { text: string };
|
|
59
|
+
final: { text: string };
|
|
60
|
+
denied: boolean;
|
|
61
|
+
reason?: string;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** What an outcome record holds. */
|
|
65
|
+
type OutcomeData = {
|
|
66
|
+
invocation: string;
|
|
67
|
+
key: string;
|
|
68
|
+
outcome: { status: string; reason?: string };
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** The lines of a file the host wrote, or nothing when it wrote none. */
|
|
72
|
+
async function linesOf(path: string): Promise<string[]> {
|
|
73
|
+
const file = Bun.file(path);
|
|
74
|
+
if (!(await file.exists())) return [];
|
|
75
|
+
const text = await file.text();
|
|
76
|
+
return text.split("\n").filter((line) => line.trim() !== "");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
test("intercept order: the adjust changes the input, the guard sees the changed one, the denial is recorded and the action does not run, and a retry asks the guard again", async () => {
|
|
80
|
+
const home = await tempHome();
|
|
81
|
+
const homePath = home.path;
|
|
82
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
// 1. The host, a client, and a watch on the journal from the beginning.
|
|
86
|
+
host = spawnHost(HOST_SCRIPT, homePath);
|
|
87
|
+
await host.ready();
|
|
88
|
+
const client = await connectAsOwner(homePath);
|
|
89
|
+
|
|
90
|
+
const seen: StoredRecord[] = [];
|
|
91
|
+
const watcher = await connectAsOwner(homePath);
|
|
92
|
+
const watch = (async () => {
|
|
93
|
+
for await (const record of watcher.records.since()) {
|
|
94
|
+
seen.push(record);
|
|
95
|
+
}
|
|
96
|
+
})().catch(() => {});
|
|
97
|
+
|
|
98
|
+
const decisions = () =>
|
|
99
|
+
seen
|
|
100
|
+
.filter((record) => record.type === DECISION_TYPE)
|
|
101
|
+
.map((record) => record.data as DecisionData);
|
|
102
|
+
const outcomes = () =>
|
|
103
|
+
seen
|
|
104
|
+
.filter((record) => record.type === OUTCOME_TYPE)
|
|
105
|
+
.map((record) => record.data as OutcomeData);
|
|
106
|
+
const states = () =>
|
|
107
|
+
seen
|
|
108
|
+
.filter((record) => record.type === "niwa.invocation.state")
|
|
109
|
+
.map((record) => record.data as { invocation: string; state: string; reason?: string });
|
|
110
|
+
|
|
111
|
+
// 2. The handler wakes and calls the action with the text "requested".
|
|
112
|
+
await client.emit(Start, {});
|
|
113
|
+
|
|
114
|
+
// 3. The adjust changed the input, the guard saw the changed one and allowed
|
|
115
|
+
// it, and the action was called with the changed input.
|
|
116
|
+
await eventually(
|
|
117
|
+
async () => (await linesOf(`${homePath}/${CALL_LOG}`)).length === 1,
|
|
118
|
+
10_000,
|
|
119
|
+
"the action should have been called once",
|
|
120
|
+
);
|
|
121
|
+
const calls = await linesOf(`${homePath}/${CALL_LOG}`);
|
|
122
|
+
expect(calls[0]?.startsWith(`${ADJUSTED}:`)).toBe(true);
|
|
123
|
+
|
|
124
|
+
// The guard was asked for the first step, and it saw the adjusted input, not
|
|
125
|
+
// the requested one.
|
|
126
|
+
const guards = await linesOf(`${homePath}/${GUARD_LOG}`);
|
|
127
|
+
expect(guards.length).toBeGreaterThanOrEqual(1);
|
|
128
|
+
expect(guards[0]?.startsWith(`${ADJUSTED}:`)).toBe(true);
|
|
129
|
+
expect(guards[0]).not.toContain(REQUESTED);
|
|
130
|
+
|
|
131
|
+
// 4. The decision is recorded with both inputs: the requested one and the
|
|
132
|
+
// final one.
|
|
133
|
+
await eventually(() => decisions().length >= 1, 10_000, "the decision should be recorded");
|
|
134
|
+
const first = decisions()[0] as DecisionData;
|
|
135
|
+
expect(first.requested).toEqual({ text: REQUESTED });
|
|
136
|
+
expect(first.final).toEqual({ text: ADJUSTED });
|
|
137
|
+
expect(first.denied).toBe(false);
|
|
138
|
+
expect(first.reason).toBeUndefined();
|
|
139
|
+
|
|
140
|
+
// 5. The handler calls the action with the text "forbidden": the guard refuses
|
|
141
|
+
// it, and the action is not called.
|
|
142
|
+
await eventually(
|
|
143
|
+
() => decisions().length >= 2,
|
|
144
|
+
10_000,
|
|
145
|
+
"the second decision should be recorded",
|
|
146
|
+
);
|
|
147
|
+
const denied = decisions().find((decision) => decision.denied);
|
|
148
|
+
expect(denied).toBeDefined();
|
|
149
|
+
expect(denied?.requested).toEqual({ text: FORBIDDEN });
|
|
150
|
+
expect(denied?.final).toEqual({ text: FORBIDDEN });
|
|
151
|
+
expect(denied?.reason).toBe("not tonight");
|
|
152
|
+
|
|
153
|
+
// The action was not called for the refused text.
|
|
154
|
+
const afterDenial = await linesOf(`${homePath}/${CALL_LOG}`);
|
|
155
|
+
expect(afterDenial.filter((line) => line.startsWith(`${FORBIDDEN}:`))).toHaveLength(0);
|
|
156
|
+
|
|
157
|
+
// The order of the records: the decision comes before the intent, the intent holds the final
|
|
158
|
+
// input, and the outcome comes last.
|
|
159
|
+
const at = (type: string, text: string) =>
|
|
160
|
+
seen.find(
|
|
161
|
+
(record) =>
|
|
162
|
+
record.type === type &&
|
|
163
|
+
(record.data as { requested?: { text: string }; input?: { text: string } })[
|
|
164
|
+
type === DECISION_TYPE ? "requested" : "input"
|
|
165
|
+
]?.text === text,
|
|
166
|
+
);
|
|
167
|
+
const deniedDecision = at(DECISION_TYPE, FORBIDDEN);
|
|
168
|
+
const deniedIntent = at(INTENT_TYPE, FORBIDDEN);
|
|
169
|
+
expect(deniedDecision).toBeDefined();
|
|
170
|
+
expect(deniedIntent).toBeDefined();
|
|
171
|
+
expect((deniedDecision?.seq ?? 0) < (deniedIntent?.seq ?? 0)).toBe(true);
|
|
172
|
+
const adjustedIntent = seen.find(
|
|
173
|
+
(record) =>
|
|
174
|
+
record.type === INTENT_TYPE && (record.data as { input: { text: string } }).input.text === ADJUSTED,
|
|
175
|
+
);
|
|
176
|
+
expect(adjustedIntent).toBeDefined();
|
|
177
|
+
expect((at(DECISION_TYPE, REQUESTED)?.seq ?? 0) < (adjustedIntent?.seq ?? 0)).toBe(true);
|
|
178
|
+
|
|
179
|
+
// The step ended as denied, with the reason of the guard.
|
|
180
|
+
await eventually(
|
|
181
|
+
() => outcomes().some((outcome) => outcome.outcome.status === "denied"),
|
|
182
|
+
10_000,
|
|
183
|
+
"the refused step should have ended as denied",
|
|
184
|
+
);
|
|
185
|
+
const deniedOutcome = outcomes().find((outcome) => outcome.outcome.status === "denied");
|
|
186
|
+
expect(deniedOutcome?.outcome.reason).toBe("not tonight");
|
|
187
|
+
|
|
188
|
+
// 6. The invocation paused on the refused step, and the owner answers "retry".
|
|
189
|
+
await eventually(
|
|
190
|
+
() => states().some((state) => state.state === "paused"),
|
|
191
|
+
10_000,
|
|
192
|
+
"the invocation should have paused",
|
|
193
|
+
);
|
|
194
|
+
const invocation = states().find((state) => state.state === "paused")?.invocation ?? "";
|
|
195
|
+
expect(invocation).not.toBe("");
|
|
196
|
+
|
|
197
|
+
await client.call(RESOLVE_ACTION as never, {
|
|
198
|
+
invocation,
|
|
199
|
+
choice: "retry",
|
|
200
|
+
} as never);
|
|
201
|
+
|
|
202
|
+
// 7. On the retry the guard was asked again, and this time it allowed the call:
|
|
203
|
+
// the action ran with "forbidden".
|
|
204
|
+
await eventually(
|
|
205
|
+
async () =>
|
|
206
|
+
(await linesOf(`${homePath}/${CALL_LOG}`)).filter((line) =>
|
|
207
|
+
line.startsWith(`${FORBIDDEN}:`),
|
|
208
|
+
).length === 1,
|
|
209
|
+
10_000,
|
|
210
|
+
"the action should have been called after the retry",
|
|
211
|
+
);
|
|
212
|
+
const guardsAfter = await linesOf(`${homePath}/${GUARD_LOG}`);
|
|
213
|
+
expect(guardsAfter.filter((line) => line.startsWith(`${FORBIDDEN}:`)).length).toBe(2);
|
|
214
|
+
|
|
215
|
+
// The retry is a new attempt, and it has its own decision: the first one denied
|
|
216
|
+
// it, the second one allowed it.
|
|
217
|
+
const attempts = decisions()
|
|
218
|
+
.filter((decision) => decision.requested.text === FORBIDDEN)
|
|
219
|
+
.map((decision) => decision.attempt);
|
|
220
|
+
expect(attempts).toEqual([0, 1]);
|
|
221
|
+
const allowed = decisions().find(
|
|
222
|
+
(decision) => decision.requested.text === FORBIDDEN && !decision.denied,
|
|
223
|
+
);
|
|
224
|
+
expect(allowed?.final).toEqual({ text: FORBIDDEN });
|
|
225
|
+
|
|
226
|
+
await client.close().catch(() => {});
|
|
227
|
+
await watcher.close().catch(() => {});
|
|
228
|
+
await watch;
|
|
229
|
+
} finally {
|
|
230
|
+
await host?.kill().catch(() => {});
|
|
231
|
+
await home.cleanup();
|
|
232
|
+
}
|
|
233
|
+
}, 30_000);
|
|
234
|
+
|
|
235
|
+
test("intercept cycle: an intercept that calls its own action is refused, and a denied outside call does not run", async () => {
|
|
236
|
+
const home = await tempHome();
|
|
237
|
+
const host = createNiwa({ home: home.path });
|
|
238
|
+
let ran = 0;
|
|
239
|
+
const Ping = action("c11cycle.ping", {
|
|
240
|
+
description: "Counts its runs.",
|
|
241
|
+
input: z.object({ n: z.number() }),
|
|
242
|
+
output: z.object({ n: z.number() }),
|
|
243
|
+
recovery: "repeat",
|
|
244
|
+
effect: "write",
|
|
245
|
+
run: (input) => {
|
|
246
|
+
ran += 1;
|
|
247
|
+
return input;
|
|
248
|
+
},
|
|
249
|
+
});
|
|
250
|
+
let inner: unknown;
|
|
251
|
+
host.use(
|
|
252
|
+
plugin("c11cycle", (api) => {
|
|
253
|
+
api.action(Ping);
|
|
254
|
+
api.adjust(Ping, async (input) => {
|
|
255
|
+
inner = await host.call(Ping, { n: input.n }).catch((error: unknown) => error);
|
|
256
|
+
return undefined;
|
|
257
|
+
});
|
|
258
|
+
api.guard(Ping, (input) => (input.n === 0 ? deny("zero") : undefined));
|
|
259
|
+
}),
|
|
260
|
+
);
|
|
261
|
+
await host.serve();
|
|
262
|
+
try {
|
|
263
|
+
// The adjust calls the action it is registered on: the inner call is refused, not looped.
|
|
264
|
+
await expect(host.call(Ping, { n: 1 })).resolves.toEqual({ n: 1 });
|
|
265
|
+
expect((inner as { code?: string }).code).toBe("Denied");
|
|
266
|
+
expect((inner as Error).message).toContain("intercept cycle");
|
|
267
|
+
expect(ran).toBe(1);
|
|
268
|
+
|
|
269
|
+
// A guard's denial of an outside call: the error says why and the action did not run.
|
|
270
|
+
await expect(host.call(Ping, { n: 0 })).rejects.toMatchObject({ code: "Denied", reason: "zero" });
|
|
271
|
+
expect(ran).toBe(1);
|
|
272
|
+
} finally {
|
|
273
|
+
await host.close();
|
|
274
|
+
await home.cleanup();
|
|
275
|
+
}
|
|
276
|
+
});
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
// The intercept that needs a call to the outside.
|
|
2
|
+
//
|
|
3
|
+
// An intercept that itself needs a call to the outside (asking a model which lessons apply)
|
|
4
|
+
// makes it as a step of the invocation that is calling the action. No transaction is held
|
|
5
|
+
// while it waits.
|
|
6
|
+
//
|
|
7
|
+
// The scenario runs against a real host process (fixtures/intercept-step-host.ts):
|
|
8
|
+
//
|
|
9
|
+
// 1. the guard of the action "send" calls the action "look" through `ctx.step`
|
|
10
|
+
// 2. the call to the outside is recorded as a step of the invocation, under a key of its own
|
|
11
|
+
// 3. the host is killed with SIGKILL, in the middle of the call to "send"
|
|
12
|
+
// 4. the host comes back: the invocation runs again, the guard is asked again, and the
|
|
13
|
+
// recorded step answers — the stub "look" is not called a second time
|
|
14
|
+
// 5. outside an invocation there is no step: `ctx.step` is absent
|
|
15
|
+
//
|
|
16
|
+
// Only the public API is used: the entries of the packages and "@niwa-labs/testing".
|
|
17
|
+
|
|
18
|
+
import { expect, test } from "bun:test";
|
|
19
|
+
import { fileURLToPath } from "node:url";
|
|
20
|
+
|
|
21
|
+
import { connect } from "@niwa-labs/client";
|
|
22
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
23
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
24
|
+
import { seq } from "@niwa-labs/core";
|
|
25
|
+
import { counter, eventually, gate, spawnHost, tempHome, connectAsOwner } from "@niwa-labs/testing";
|
|
26
|
+
|
|
27
|
+
import { GUARD_LOG, Start, actionsOf } from "./fixtures/intercept-step-host.ts";
|
|
28
|
+
|
|
29
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/intercept-step-host.ts", import.meta.url));
|
|
30
|
+
|
|
31
|
+
/** The lines of a file the host wrote, or nothing when it wrote none. */
|
|
32
|
+
async function linesOf(path: string): Promise<string[]> {
|
|
33
|
+
const file = Bun.file(path);
|
|
34
|
+
if (!(await file.exists())) return [];
|
|
35
|
+
const text = await file.text();
|
|
36
|
+
return text.split("\n").filter((line) => line.trim() !== "");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
test("an intercept that needs a call to the outside makes it a step: after a crash the stub is called once", async () => {
|
|
40
|
+
const home = await tempHome();
|
|
41
|
+
const homePath = home.path;
|
|
42
|
+
const hosts: Array<ReturnType<typeof spawnHost>> = [];
|
|
43
|
+
let client: Awaited<ReturnType<typeof connect>> | undefined;
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
const first = spawnHost(HOST_SCRIPT, homePath);
|
|
47
|
+
hosts.push(first);
|
|
48
|
+
await first.ready();
|
|
49
|
+
|
|
50
|
+
client = await connectAsOwner(homePath);
|
|
51
|
+
await client.emit(Start, {});
|
|
52
|
+
|
|
53
|
+
// The guard ran inside the invocation, so it had a step: the call to the outside was made.
|
|
54
|
+
await eventually(
|
|
55
|
+
() => counter(homePath, "look").count() >= 1,
|
|
56
|
+
10_000,
|
|
57
|
+
"the guard should have called the outside once",
|
|
58
|
+
);
|
|
59
|
+
expect((await linesOf(`${homePath}/${GUARD_LOG}`))[0]).toBe("inside");
|
|
60
|
+
|
|
61
|
+
// The call to "send" is blocked on the gate: the host can be killed in the middle of it.
|
|
62
|
+
await eventually(
|
|
63
|
+
() => counter(homePath, "send").count() >= 1,
|
|
64
|
+
10_000,
|
|
65
|
+
"the action should have been called",
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
// A real crash: SIGKILL, no clean shutdown.
|
|
69
|
+
await first.kill();
|
|
70
|
+
await client.close().catch(() => {});
|
|
71
|
+
client = undefined;
|
|
72
|
+
|
|
73
|
+
// The host comes back on the same home, and the gate is opened right away.
|
|
74
|
+
const second = spawnHost(HOST_SCRIPT, homePath);
|
|
75
|
+
hosts.push(second);
|
|
76
|
+
gate(homePath, "g1").open();
|
|
77
|
+
await second.ready();
|
|
78
|
+
|
|
79
|
+
// The invocation runs again: the guard is asked again, and its step is answered by the
|
|
80
|
+
// record — the stub is not called a second time.
|
|
81
|
+
await eventually(
|
|
82
|
+
() => counter(homePath, "send").count() >= 2,
|
|
83
|
+
15_000,
|
|
84
|
+
"the action should have been called again after the restart",
|
|
85
|
+
);
|
|
86
|
+
await eventually(
|
|
87
|
+
async () => (await linesOf(`${homePath}/${GUARD_LOG}`)).filter((line) => line === "inside").length >= 2,
|
|
88
|
+
15_000,
|
|
89
|
+
"the guard should have been asked again after the restart",
|
|
90
|
+
);
|
|
91
|
+
// Give the replay a moment to make a second call, if it were going to.
|
|
92
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
93
|
+
expect(counter(homePath, "look").count()).toBe(1);
|
|
94
|
+
|
|
95
|
+
// The call to the outside was recorded as a step of the invocation, under a key of its own.
|
|
96
|
+
const watcher = await connectAsOwner(homePath);
|
|
97
|
+
const intents: string[] = [];
|
|
98
|
+
try {
|
|
99
|
+
const records = await watcher.records.read({ types: ["niwa.step.intent" as never] });
|
|
100
|
+
for (const record of records) {
|
|
101
|
+
const data = record.data as { key?: string; action?: string };
|
|
102
|
+
if (data.action === "look" && typeof data.key === "string") intents.push(data.key);
|
|
103
|
+
}
|
|
104
|
+
} finally {
|
|
105
|
+
await watcher.close().catch(() => {});
|
|
106
|
+
}
|
|
107
|
+
expect(intents.length).toBeGreaterThan(0);
|
|
108
|
+
// The key is namespaced under the intercepted step: it is not the step of the handler.
|
|
109
|
+
expect(intents.every((key) => key.startsWith("k~guard~"))).toBe(true);
|
|
110
|
+
} finally {
|
|
111
|
+
await client?.close().catch(() => {});
|
|
112
|
+
for (const host of hosts) await host.kill().catch(() => {});
|
|
113
|
+
await home.cleanup();
|
|
114
|
+
}
|
|
115
|
+
}, 40_000);
|
|
116
|
+
|
|
117
|
+
test("outside an invocation there is no step to make a call as", async () => {
|
|
118
|
+
const home = await tempHome();
|
|
119
|
+
const homePath = home.path;
|
|
120
|
+
const { send } = actionsOf(homePath);
|
|
121
|
+
|
|
122
|
+
// The action waits on the gate; it is opened, so the call ends.
|
|
123
|
+
gate(homePath, "g1").open();
|
|
124
|
+
|
|
125
|
+
// The same guard, run by a host: a call from outside an invocation has no step.
|
|
126
|
+
const host = createNiwa({ home: homePath });
|
|
127
|
+
let sawStep: unknown = "unset";
|
|
128
|
+
host.use(
|
|
129
|
+
plugin("outside", (api) => {
|
|
130
|
+
api.action(send);
|
|
131
|
+
api.guard(send, async (_input, ctx) => {
|
|
132
|
+
sawStep = ctx.step;
|
|
133
|
+
});
|
|
134
|
+
}),
|
|
135
|
+
);
|
|
136
|
+
await host.serve();
|
|
137
|
+
try {
|
|
138
|
+
await host.call(send, { text: "hello" });
|
|
139
|
+
expect(sawStep).toBeUndefined();
|
|
140
|
+
} finally {
|
|
141
|
+
await host.close();
|
|
142
|
+
await home.cleanup();
|
|
143
|
+
}
|
|
144
|
+
}, 30_000);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// A step whose value is too large for a record: the host keeps the value as an artifact, the
|
|
2
|
+
// record holds its reference, and a run that goes on after a wait reads the value back.
|
|
3
|
+
|
|
4
|
+
import { expect, test } from "bun:test";
|
|
5
|
+
|
|
6
|
+
import { ARTIFACT_THRESHOLD, CORE_TYPES, action, record } from "@niwa-labs/core";
|
|
7
|
+
import type { ArtifactRef } from "@niwa-labs/core";
|
|
8
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
9
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
10
|
+
import { eventually, tempHome } from "@niwa-labs/testing";
|
|
11
|
+
import { z } from "zod";
|
|
12
|
+
|
|
13
|
+
const Start = record("large.start", z.object({}));
|
|
14
|
+
const Go = record("large.go", z.object({}));
|
|
15
|
+
const Seen = record("large.seen", z.object({ length: z.number() }));
|
|
16
|
+
|
|
17
|
+
test("a large step value is an artifact of the host, and the step that is replayed after a wait gets it whole", async () => {
|
|
18
|
+
const home = await tempHome();
|
|
19
|
+
const niwa = createNiwa({ home: home.path });
|
|
20
|
+
const text = "z".repeat(ARTIFACT_THRESHOLD * 2);
|
|
21
|
+
let calls = 0;
|
|
22
|
+
const Big = action("large.big", {
|
|
23
|
+
description: "Answers with a long text.",
|
|
24
|
+
input: z.object({}),
|
|
25
|
+
output: z.object({ text: z.string() }),
|
|
26
|
+
recovery: "do-not-repeat",
|
|
27
|
+
effect: "read",
|
|
28
|
+
run: async () => {
|
|
29
|
+
calls += 1;
|
|
30
|
+
return { text };
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
niwa.use(
|
|
34
|
+
plugin("large", (api) => {
|
|
35
|
+
api.declare(Start, Go, Seen);
|
|
36
|
+
api.action(Big);
|
|
37
|
+
api.on(Start, async (_data, step) => {
|
|
38
|
+
const got = await step(Big, {}, { key: "big" });
|
|
39
|
+
await step.wait(Go);
|
|
40
|
+
await step.emit(Seen, { length: got.text.length }, { key: "seen" });
|
|
41
|
+
});
|
|
42
|
+
}),
|
|
43
|
+
);
|
|
44
|
+
await niwa.serve();
|
|
45
|
+
try {
|
|
46
|
+
await niwa.emit(Start, {});
|
|
47
|
+
await eventually(async () => (await niwa.records.read()).some((row) => row.type === CORE_TYPES.stepOutcome), 5_000, "the step should end");
|
|
48
|
+
await niwa.emit(Go, {});
|
|
49
|
+
await eventually(async () => (await niwa.records.read()).some((row) => row.type === Seen.name), 5_000, "the run should go on");
|
|
50
|
+
const rows = await niwa.records.read();
|
|
51
|
+
const outcome = (rows.find((row) => row.type === CORE_TYPES.stepOutcome)?.data as { outcome: { artifact?: ArtifactRef; value?: unknown } }).outcome;
|
|
52
|
+
expect(outcome.value).toBeUndefined();
|
|
53
|
+
expect(JSON.parse(new TextDecoder().decode(niwa.artifacts.read(outcome.artifact!)))).toEqual({ text });
|
|
54
|
+
expect(rows.find((row) => row.type === Seen.name)?.data).toEqual({ length: text.length });
|
|
55
|
+
expect(calls).toBe(1);
|
|
56
|
+
} finally {
|
|
57
|
+
await niwa.close();
|
|
58
|
+
await home.cleanup();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// Live frames.
|
|
2
|
+
//
|
|
3
|
+
// The scenario runs against a real host process (fixtures/live-frames-host.ts):
|
|
4
|
+
//
|
|
5
|
+
// 1. a subscriber connects to the channel "chat"
|
|
6
|
+
// 2. a message is written: the handler sends a live frame, and the subscriber receives it
|
|
7
|
+
// 3. the journal holds the record and no frame: a frame is not a record
|
|
8
|
+
// 4. a subscriber that connected late received none of the frames, and still receives
|
|
9
|
+
// the complete record
|
|
10
|
+
//
|
|
11
|
+
// Only the public API is used: the entries of the packages and "@niwa-labs/testing".
|
|
12
|
+
|
|
13
|
+
import { expect, test } from "bun:test";
|
|
14
|
+
|
|
15
|
+
import type { LiveFrame, StoredRecord } from "@niwa-labs/core";
|
|
16
|
+
import { connect } from "@niwa-labs/client";
|
|
17
|
+
import { seq } from "@niwa-labs/core";
|
|
18
|
+
import { eventually, spawnHost, tempHome, connectAsOwner } from "@niwa-labs/testing";
|
|
19
|
+
|
|
20
|
+
import { CHANNEL, Message, sendFrame } from "./fixtures/live-frames-host.ts";
|
|
21
|
+
import { fileURLToPath } from "node:url";
|
|
22
|
+
|
|
23
|
+
const HOST_SCRIPT = fileURLToPath(new URL("./fixtures/live-frames-host.ts", import.meta.url));
|
|
24
|
+
|
|
25
|
+
/** From the beginning of the journal. */
|
|
26
|
+
const FROM_START = seq(0);
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Collect the records through the public subscription.
|
|
30
|
+
*
|
|
31
|
+
* A subscription never ends by itself, so the collection stops itself: it waits for the
|
|
32
|
+
* journal to settle and then closes the subscription.
|
|
33
|
+
*/
|
|
34
|
+
async function readJournal(
|
|
35
|
+
client: Awaited<ReturnType<typeof connect>>,
|
|
36
|
+
settleMs = 500,
|
|
37
|
+
): Promise<StoredRecord[]> {
|
|
38
|
+
const records: StoredRecord[] = [];
|
|
39
|
+
const reading = (async () => {
|
|
40
|
+
for await (const record of client.records.since()) {
|
|
41
|
+
records.push(record);
|
|
42
|
+
}
|
|
43
|
+
})().catch(() => {});
|
|
44
|
+
await Bun.sleep(settleMs);
|
|
45
|
+
await client.close().catch(() => {});
|
|
46
|
+
await reading;
|
|
47
|
+
return records;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Listen to a channel until it has heard as many frames as it should.
|
|
52
|
+
*
|
|
53
|
+
* The frames are collected in the background; the caller waits for the count and then stops
|
|
54
|
+
* the subscription by closing the client.
|
|
55
|
+
*/
|
|
56
|
+
function listen(
|
|
57
|
+
client: Awaited<ReturnType<typeof connect>>,
|
|
58
|
+
channel: string,
|
|
59
|
+
): { frames: LiveFrame[]; done: Promise<void>; ready: Promise<void> } {
|
|
60
|
+
const frames: LiveFrame[] = [];
|
|
61
|
+
const subscription = client.live.subscribe(channel);
|
|
62
|
+
const done = (async () => {
|
|
63
|
+
for await (const frame of subscription) {
|
|
64
|
+
frames.push(frame);
|
|
65
|
+
}
|
|
66
|
+
})().catch(() => {});
|
|
67
|
+
return { frames, done, ready: subscription.ready };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
test("live frames: a subscriber receives the frames, the journal holds none of them, and a late subscriber still receives the complete record", async () => {
|
|
71
|
+
const home = await tempHome();
|
|
72
|
+
const homePath = home.path;
|
|
73
|
+
let host: ReturnType<typeof spawnHost> | undefined;
|
|
74
|
+
let client: Awaited<ReturnType<typeof connect>> | undefined;
|
|
75
|
+
let late: Awaited<ReturnType<typeof connect>> | undefined;
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
host = spawnHost(HOST_SCRIPT, homePath);
|
|
79
|
+
await host.ready();
|
|
80
|
+
client = await connectAsOwner(homePath);
|
|
81
|
+
|
|
82
|
+
// 1. A subscriber connects to the channel.
|
|
83
|
+
const subscriber = listen(client, CHANNEL);
|
|
84
|
+
await subscriber.ready;
|
|
85
|
+
|
|
86
|
+
// 2. A message is written. The handler sends a live frame, and the
|
|
87
|
+
// subscriber receives it.
|
|
88
|
+
const written = await client.emit(Message, { text: "hello" });
|
|
89
|
+
await eventually(
|
|
90
|
+
() => subscriber.frames.length === 1,
|
|
91
|
+
10_000,
|
|
92
|
+
"the subscriber should receive the live frame",
|
|
93
|
+
);
|
|
94
|
+
expect(subscriber.frames[0]?.channel).toBe(CHANNEL);
|
|
95
|
+
expect(subscriber.frames[0]?.data).toEqual({ partial: "hello" });
|
|
96
|
+
|
|
97
|
+
// 3. The journal holds the record and no frame. The frame is not a record:
|
|
98
|
+
// it was never written.
|
|
99
|
+
const records = await readJournal(await connectAsOwner(homePath));
|
|
100
|
+
expect(records.some((entry) => entry.id === written.id)).toBe(true);
|
|
101
|
+
expect(records.some((entry) => entry.type === CHANNEL)).toBe(false);
|
|
102
|
+
expect(JSON.stringify(records)).not.toContain("partial");
|
|
103
|
+
const recordsBefore = records.length;
|
|
104
|
+
|
|
105
|
+
// 4. A subscriber that connected late received none of the frames, and
|
|
106
|
+
// still receives the complete record.
|
|
107
|
+
late = await connectAsOwner(homePath);
|
|
108
|
+
const lateSubscriber = listen(late, CHANNEL);
|
|
109
|
+
await lateSubscriber.ready;
|
|
110
|
+
|
|
111
|
+
// The frames sent before the late subscriber connected are gone: they were
|
|
112
|
+
// not received by it, and they are not in the journal either.
|
|
113
|
+
expect(lateSubscriber.frames).toHaveLength(0);
|
|
114
|
+
|
|
115
|
+
// A frame sent now reaches it.
|
|
116
|
+
await client.call(sendFrame, { text: "later" });
|
|
117
|
+
await eventually(
|
|
118
|
+
() => lateSubscriber.frames.length === 1,
|
|
119
|
+
10_000,
|
|
120
|
+
"the late subscriber should receive the frames sent after it connected",
|
|
121
|
+
);
|
|
122
|
+
expect(lateSubscriber.frames[0]?.data).toEqual({ partial: "later" });
|
|
123
|
+
|
|
124
|
+
// A frame sent by a client goes the same way: heard, and written nowhere.
|
|
125
|
+
await client.live.send(CHANNEL, { channel: CHANNEL, data: { partial: "from a client" } });
|
|
126
|
+
await eventually(
|
|
127
|
+
() => lateSubscriber.frames.length === 2,
|
|
128
|
+
10_000,
|
|
129
|
+
"the late subscriber should receive a frame sent by a client",
|
|
130
|
+
);
|
|
131
|
+
expect(lateSubscriber.frames[1]?.data).toEqual({ partial: "from a client" });
|
|
132
|
+
|
|
133
|
+
// And it still receives the complete record — the frames are not the
|
|
134
|
+
// message, the record is.
|
|
135
|
+
const lateRecords = await readJournal(await connectAsOwner(homePath));
|
|
136
|
+
const found = lateRecords.find((entry) => entry.id === written.id);
|
|
137
|
+
expect(found).toBeDefined();
|
|
138
|
+
expect(found?.type).toBe(Message.name);
|
|
139
|
+
expect(found?.data).toEqual({ text: "hello" });
|
|
140
|
+
|
|
141
|
+
// Three frames were sent since the first read, and the journal did not grow by one record.
|
|
142
|
+
expect(lateRecords).toHaveLength(recordsBefore);
|
|
143
|
+
expect(JSON.stringify(lateRecords)).not.toContain("from a client");
|
|
144
|
+
} finally {
|
|
145
|
+
await client?.close().catch(() => {});
|
|
146
|
+
await late?.close().catch(() => {});
|
|
147
|
+
await host?.kill().catch(() => {});
|
|
148
|
+
await home.cleanup();
|
|
149
|
+
}
|
|
150
|
+
}, 30_000);
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// A live subscription says when the host is listening for it: a frame sent right after that is received.
|
|
2
|
+
|
|
3
|
+
import { afterEach, expect, test } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import { connect } from "@niwa-labs/client";
|
|
6
|
+
import type { NiwaApi } from "@niwa-labs/client";
|
|
7
|
+
import { createNiwa } from "@niwa-labs/host";
|
|
8
|
+
import type { Host } from "@niwa-labs/host";
|
|
9
|
+
import { connectAsOwner, tempHome } from "@niwa-labs/testing";
|
|
10
|
+
|
|
11
|
+
const homes: Array<{ cleanup(): Promise<void> }> = [];
|
|
12
|
+
const hosts: Host[] = [];
|
|
13
|
+
const clients: NiwaApi[] = [];
|
|
14
|
+
|
|
15
|
+
afterEach(async () => {
|
|
16
|
+
for (const client of clients.splice(0)) await client.close().catch(() => {});
|
|
17
|
+
for (const host of hosts.splice(0)) await host.close().catch(() => {});
|
|
18
|
+
for (const home of homes.splice(0)) await home.cleanup();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
async function served() {
|
|
22
|
+
const home = await tempHome();
|
|
23
|
+
homes.push(home);
|
|
24
|
+
const host = createNiwa({ home: home.path });
|
|
25
|
+
await host.serve();
|
|
26
|
+
hosts.push(host);
|
|
27
|
+
const client = await connectAsOwner(home.path);
|
|
28
|
+
clients.push(client);
|
|
29
|
+
return { home: home.path, client };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
test("a frame sent right after ready is received, with no pause between", async () => {
|
|
33
|
+
const { client } = await served();
|
|
34
|
+
for (let round = 0; round < 20; round += 1) {
|
|
35
|
+
const channel = `chat-${round}`;
|
|
36
|
+
const frames = client.live.subscribe(channel);
|
|
37
|
+
await frames.ready;
|
|
38
|
+
await client.live.send(channel, { channel, data: round });
|
|
39
|
+
const first = await Promise.race([frames.next(), Bun.sleep(2_000).then(() => "lost" as const)]);
|
|
40
|
+
expect(first).toEqual({ done: false, value: { channel, data: round } });
|
|
41
|
+
await frames.return();
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("ready is refused when the subscription ends before the host answered", async () => {
|
|
46
|
+
const { client } = await served();
|
|
47
|
+
const frames = client.live.subscribe("chat");
|
|
48
|
+
await frames.return();
|
|
49
|
+
await expect(frames.ready).rejects.toThrow();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test("a plain client's subscription is ready and hears a frame another client sent", async () => {
|
|
53
|
+
const { home, client } = await served();
|
|
54
|
+
const other = await connect({ home });
|
|
55
|
+
clients.push(other);
|
|
56
|
+
const frames = other.live.subscribe("quiet");
|
|
57
|
+
await frames.ready;
|
|
58
|
+
await client.live.send("quiet", { channel: "quiet", data: 1 });
|
|
59
|
+
expect((await frames.next()).value).toEqual({ channel: "quiet", data: 1 });
|
|
60
|
+
await frames.return();
|
|
61
|
+
});
|