@mowaos/host-browser 0.7.0 → 0.8.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/README.md +16 -0
- package/conformance/host-suite.d.ts +15 -0
- package/conformance/host-suite.mjs +910 -0
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -33,6 +33,22 @@ const host = new MowaBrowserHost(new WasmKernelBridge(wasm), {
|
|
|
33
33
|
});
|
|
34
34
|
```
|
|
35
35
|
|
|
36
|
+
**Conformance suite** (since 0.8.0): a storage adapter written elsewhere runs the same
|
|
37
|
+
tests as `MemoryStorage` and `SqliteStorage`, under `node --test`:
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
import * as lib from "@mowaos/host-browser";
|
|
41
|
+
import { hostSuite } from "@mowaos/host-browser/conformance";
|
|
42
|
+
import { loadKernel } from "@mowaos/kernel";
|
|
43
|
+
|
|
44
|
+
hostSuite("MyStorage", {
|
|
45
|
+
lib,
|
|
46
|
+
wasmPkg: await loadKernel(),
|
|
47
|
+
makeStorage: () => new MyStorage(),
|
|
48
|
+
clearSteps: (storage) => storage.deleteAllSteps(), // imitates a host from before 0.5.0
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
36
52
|
What every host must guarantee is specified in the
|
|
37
53
|
[host contract](../../docs/host-contract.md). Each release attaches the WASM build and its
|
|
38
54
|
SHA-256 checksum. Licensed under Apache-2.0.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { MowaStorage } from "../dist/index.js";
|
|
2
|
+
|
|
3
|
+
export interface HostSuiteOptions {
|
|
4
|
+
/** The @mowaos/host-browser module the storage was built against. */
|
|
5
|
+
lib: typeof import("../dist/index.js");
|
|
6
|
+
/** The kernel's bindings: startJson, advanceJson, loadFlowJson, dedupKeyJson, kernelVersion. */
|
|
7
|
+
wasmPkg: unknown;
|
|
8
|
+
/** A new, empty storage for each test. */
|
|
9
|
+
makeStorage: () => MowaStorage | Promise<MowaStorage>;
|
|
10
|
+
/** Deletes every step record from a storage, to imitate a host from before 0.5.0. */
|
|
11
|
+
clearSteps: (storage: MowaStorage) => void | Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Registers the host conformance tests (node:test) for one storage adapter. */
|
|
15
|
+
export function hostSuite(name: string, options: HostSuiteOptions): void;
|
|
@@ -0,0 +1,910 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host conformance suite: the behaviour every MO·WA host built on MowaBrowserHost must
|
|
3
|
+
* keep, whatever its storage (docs/host-contract.md, section 9). Published as
|
|
4
|
+
* `@mowaos/host-browser/conformance`, so a storage adapter outside this repository runs
|
|
5
|
+
* exactly the suite that MemoryStorage and SqliteStorage pass. It uses node:test, so it
|
|
6
|
+
* runs under Node's test runner (`node --test`).
|
|
7
|
+
*
|
|
8
|
+
* import * as lib from "@mowaos/host-browser";
|
|
9
|
+
* import { hostSuite } from "@mowaos/host-browser/conformance";
|
|
10
|
+
* import { loadKernel } from "@mowaos/kernel";
|
|
11
|
+
*
|
|
12
|
+
* hostSuite("MyStorage", {
|
|
13
|
+
* lib, // the module your storage was built against
|
|
14
|
+
* wasmPkg: await loadKernel(), // startJson, advanceJson, loadFlowJson, kernelVersion
|
|
15
|
+
* makeStorage: () => new MyStorage(), // a new, empty MowaStorage (may be async)
|
|
16
|
+
* clearSteps: (storage) => …, // delete every step record, to imitate an older host
|
|
17
|
+
* });
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import assert from "node:assert/strict";
|
|
21
|
+
import { describe, it } from "node:test";
|
|
22
|
+
|
|
23
|
+
/** Wraps a storage so every commit waits first, as IndexedDB does while it opens. */
|
|
24
|
+
function slowCommits(storage) {
|
|
25
|
+
return new Proxy(storage, {
|
|
26
|
+
get(target, key) {
|
|
27
|
+
const value = target[key];
|
|
28
|
+
if (key === "atomicCommit") {
|
|
29
|
+
return async (...args) => {
|
|
30
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
31
|
+
return value.apply(target, args);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function sampleFlow() {
|
|
40
|
+
return {
|
|
41
|
+
id: "APPROVAL_FLOW",
|
|
42
|
+
version: "1.0.0",
|
|
43
|
+
status: "ACTIVE",
|
|
44
|
+
startNode: "INTAKE",
|
|
45
|
+
startActors: ["SYSTEM", "OPERATOR"],
|
|
46
|
+
effectiveFrom: 0,
|
|
47
|
+
declaredFacts: {
|
|
48
|
+
"doc:invoice": ["MISSING", "VERIFIED", "CONFLICTED"],
|
|
49
|
+
},
|
|
50
|
+
nodes: {
|
|
51
|
+
INTAKE: {
|
|
52
|
+
id: "INTAKE",
|
|
53
|
+
type: "t",
|
|
54
|
+
tier: "T1",
|
|
55
|
+
onEnter: [{ kind: "EMIT_INTAKE_NOTIFICATION" }],
|
|
56
|
+
transitions: {
|
|
57
|
+
SUBMIT_DOCS: {
|
|
58
|
+
to: "APPROVED",
|
|
59
|
+
allowedActors: ["SYSTEM", "OPERATOR"],
|
|
60
|
+
guard: {
|
|
61
|
+
op: "status_is",
|
|
62
|
+
target: "doc:invoice",
|
|
63
|
+
status: "VERIFIED",
|
|
64
|
+
},
|
|
65
|
+
onGuardFail: "REVIEW",
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
REVIEW: {
|
|
70
|
+
id: "REVIEW",
|
|
71
|
+
type: "t",
|
|
72
|
+
tier: "T2",
|
|
73
|
+
onEnter: [{ kind: "REQUEST_REVIEW", payload: { reason: "Missing invoice" } }],
|
|
74
|
+
transitions: {
|
|
75
|
+
RESOLVE: {
|
|
76
|
+
to: "APPROVED",
|
|
77
|
+
allowedActors: ["OPERATOR"],
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
APPROVED: {
|
|
82
|
+
id: "APPROVED",
|
|
83
|
+
type: "t",
|
|
84
|
+
tier: "T1",
|
|
85
|
+
terminal: true,
|
|
86
|
+
onEnter: [{ kind: "EMIT_APPROVED" }],
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function hostSuite(name, { lib, wasmPkg, makeStorage, clearSteps }) {
|
|
93
|
+
const {
|
|
94
|
+
MowaBrowserHost,
|
|
95
|
+
WasmKernelBridge,
|
|
96
|
+
AnchorImportError,
|
|
97
|
+
MemorySessionLock,
|
|
98
|
+
OutboxDispatcher,
|
|
99
|
+
IdempotencyConflictError,
|
|
100
|
+
IncompleteStepHistoryError,
|
|
101
|
+
CasConflictError,
|
|
102
|
+
} = lib;
|
|
103
|
+
|
|
104
|
+
describe(`host conformance: ${name}`, () => {
|
|
105
|
+
const kernel = new WasmKernelBridge(wasmPkg);
|
|
106
|
+
|
|
107
|
+
it("1. installFlow validates, caches in WASM, and stores in storage", async () => {
|
|
108
|
+
const storage = await makeStorage();
|
|
109
|
+
const host = new MowaBrowserHost(kernel, { storage });
|
|
110
|
+
|
|
111
|
+
const flow = sampleFlow();
|
|
112
|
+
const { flowHash, flowHandle } = await host.installFlow(flow);
|
|
113
|
+
|
|
114
|
+
assert.equal(flowHash.length, 64);
|
|
115
|
+
assert.ok(flowHandle.startsWith(flowHash));
|
|
116
|
+
|
|
117
|
+
const stored = await storage.getFlow(flowHash);
|
|
118
|
+
assert.ok(stored);
|
|
119
|
+
assert.equal(stored.id, "APPROVAL_FLOW");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("2. startSession creates session version 0 and writes atomic 4-store commit", async () => {
|
|
123
|
+
const storage = await makeStorage();
|
|
124
|
+
const host = new MowaBrowserHost(kernel, { storage });
|
|
125
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
126
|
+
|
|
127
|
+
const sessionId = "SESSION_101";
|
|
128
|
+
const event = {
|
|
129
|
+
id: "EVT_1",
|
|
130
|
+
type: "INITIALIZE",
|
|
131
|
+
source: "chrome_extension_action",
|
|
132
|
+
caseId: "CASE_9921",
|
|
133
|
+
timestamp: 1000,
|
|
134
|
+
idempotencyKey: "idem_start_1",
|
|
135
|
+
actor: { type: "OPERATOR", id: "OPERATOR_ALICE" },
|
|
136
|
+
payload: {},
|
|
137
|
+
};
|
|
138
|
+
const ctx = { asOf: 1000, contextRefs: { manifestVersion: "2026.11" } };
|
|
139
|
+
|
|
140
|
+
const commit = await host.startSession({
|
|
141
|
+
sessionId,
|
|
142
|
+
event,
|
|
143
|
+
flowHash,
|
|
144
|
+
ctx,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
assert.equal(commit.newSession.id, sessionId);
|
|
148
|
+
assert.equal(commit.newSession.state, "INTAKE");
|
|
149
|
+
assert.equal(commit.newSession.version, 0);
|
|
150
|
+
assert.equal(commit.newSession.flowHash, flowHash);
|
|
151
|
+
assert.equal(commit.outbox.length, 1);
|
|
152
|
+
assert.equal(commit.outbox[0].kind, "EMIT_INTAKE_NOTIFICATION");
|
|
153
|
+
assert.equal(commit.audit.prevResultHash, null);
|
|
154
|
+
assert.equal(commit.newSession.lastResultHash, commit.audit.resultHash);
|
|
155
|
+
|
|
156
|
+
// Verify storage persistence
|
|
157
|
+
const savedSession = await storage.getSession(sessionId);
|
|
158
|
+
assert.ok(savedSession);
|
|
159
|
+
assert.equal(savedSession.version, 0);
|
|
160
|
+
|
|
161
|
+
const audit = await storage.getAudit(commit.audit.transitionId);
|
|
162
|
+
assert.ok(audit);
|
|
163
|
+
assert.equal(audit.toState, "INTAKE");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("3. idempotent start: duplicate start event returns cached commit immediately", async () => {
|
|
167
|
+
const storage = await makeStorage();
|
|
168
|
+
const host = new MowaBrowserHost(kernel, { storage });
|
|
169
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
170
|
+
|
|
171
|
+
const startParams = {
|
|
172
|
+
sessionId: "SESSION_IDEM_START",
|
|
173
|
+
event: {
|
|
174
|
+
id: "EVT_START_IDEM",
|
|
175
|
+
type: "INITIALIZE",
|
|
176
|
+
source: "action_card",
|
|
177
|
+
caseId: "CASE_IDEM",
|
|
178
|
+
timestamp: 1000,
|
|
179
|
+
idempotencyKey: "idem_key_unique_1",
|
|
180
|
+
actor: { type: "SYSTEM", id: "SYS_1" },
|
|
181
|
+
payload: {},
|
|
182
|
+
},
|
|
183
|
+
flowHash,
|
|
184
|
+
ctx: { asOf: 1000, contextRefs: {} },
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const first = await host.startSession(startParams);
|
|
188
|
+
const second = await host.startSession(startParams);
|
|
189
|
+
|
|
190
|
+
assert.equal(first.dedupKey, second.dedupKey);
|
|
191
|
+
assert.equal(first.audit.transitionId, second.audit.transitionId);
|
|
192
|
+
assert.equal(JSON.stringify(first), JSON.stringify(second));
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("4. advanceSession transitions state, increments version, and chains audit hashes", async () => {
|
|
196
|
+
const storage = await makeStorage();
|
|
197
|
+
const outbox = new OutboxDispatcher(storage);
|
|
198
|
+
const host = new MowaBrowserHost(kernel, { storage, outboxDispatcher: outbox });
|
|
199
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
200
|
+
|
|
201
|
+
const sessionId = "SESSION_ADV_1";
|
|
202
|
+
const startCommit = await host.startSession({
|
|
203
|
+
sessionId,
|
|
204
|
+
event: {
|
|
205
|
+
id: "EVT_INIT",
|
|
206
|
+
type: "INITIALIZE",
|
|
207
|
+
source: "action_card",
|
|
208
|
+
caseId: "CASE_ADV",
|
|
209
|
+
timestamp: 1000,
|
|
210
|
+
idempotencyKey: "idem_init",
|
|
211
|
+
actor: { type: "OPERATOR", id: "BOB" },
|
|
212
|
+
payload: {},
|
|
213
|
+
},
|
|
214
|
+
flowHash,
|
|
215
|
+
ctx: { asOf: 1000, contextRefs: {} },
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// Advance with verified invoice fact
|
|
219
|
+
const advanceCommit = await host.advanceSession({
|
|
220
|
+
sessionId,
|
|
221
|
+
event: {
|
|
222
|
+
id: "EVT_DOCS",
|
|
223
|
+
type: "SUBMIT_DOCS",
|
|
224
|
+
source: "action_card",
|
|
225
|
+
caseId: "CASE_ADV",
|
|
226
|
+
timestamp: 2000,
|
|
227
|
+
idempotencyKey: "idem_docs",
|
|
228
|
+
actor: { type: "OPERATOR", id: "BOB" },
|
|
229
|
+
payload: {},
|
|
230
|
+
},
|
|
231
|
+
facts: {
|
|
232
|
+
facts: { "doc:invoice": "VERIFIED" },
|
|
233
|
+
},
|
|
234
|
+
ctx: { asOf: 2000, contextRefs: {} },
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
assert.equal(advanceCommit.newSession.state, "APPROVED");
|
|
238
|
+
assert.equal(advanceCommit.newSession.version, 1);
|
|
239
|
+
assert.equal(advanceCommit.audit.fromState, "INTAKE");
|
|
240
|
+
assert.equal(advanceCommit.audit.toState, "APPROVED");
|
|
241
|
+
assert.equal(advanceCommit.outbox[0].kind, "EMIT_APPROVED");
|
|
242
|
+
assert.equal(advanceCommit.audit.prevResultHash, startCommit.audit.resultHash);
|
|
243
|
+
assert.equal(advanceCommit.newSession.lastResultHash, advanceCommit.audit.resultHash);
|
|
244
|
+
|
|
245
|
+
// Verify session updated in storage
|
|
246
|
+
const updated = await storage.getSession(sessionId);
|
|
247
|
+
assert.equal(updated?.version, 1);
|
|
248
|
+
assert.equal(updated?.state, "APPROVED");
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("5. guard failure routes deterministically to REVIEW with Pre-fill outbox", async () => {
|
|
252
|
+
const storage = await makeStorage();
|
|
253
|
+
const outbox = new OutboxDispatcher(storage);
|
|
254
|
+
const host = new MowaBrowserHost(kernel, { storage, outboxDispatcher: outbox });
|
|
255
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
256
|
+
|
|
257
|
+
const sessionId = "SESSION_REVIEW_1";
|
|
258
|
+
await host.startSession({
|
|
259
|
+
sessionId,
|
|
260
|
+
event: {
|
|
261
|
+
id: "EVT_0",
|
|
262
|
+
type: "INIT",
|
|
263
|
+
source: "action_card",
|
|
264
|
+
caseId: "CASE_REV",
|
|
265
|
+
timestamp: 1000,
|
|
266
|
+
idempotencyKey: "idem_rev_0",
|
|
267
|
+
actor: { type: "SYSTEM", id: "SYS" },
|
|
268
|
+
payload: {},
|
|
269
|
+
},
|
|
270
|
+
flowHash,
|
|
271
|
+
ctx: { asOf: 1000, contextRefs: {} },
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
// Advance with MISSING invoice fact -> guard fails, routes to REVIEW
|
|
275
|
+
const commit = await host.advanceSession({
|
|
276
|
+
sessionId,
|
|
277
|
+
event: {
|
|
278
|
+
id: "EVT_SUBMIT_MISSING",
|
|
279
|
+
type: "SUBMIT_DOCS",
|
|
280
|
+
source: "action_card",
|
|
281
|
+
caseId: "CASE_REV",
|
|
282
|
+
timestamp: 2000,
|
|
283
|
+
idempotencyKey: "idem_missing",
|
|
284
|
+
actor: { type: "SYSTEM", id: "SYS" },
|
|
285
|
+
payload: {},
|
|
286
|
+
},
|
|
287
|
+
facts: {
|
|
288
|
+
facts: { "doc:invoice": "MISSING" },
|
|
289
|
+
},
|
|
290
|
+
ctx: { asOf: 2000, contextRefs: {} },
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
assert.equal(commit.newSession.state, "REVIEW");
|
|
294
|
+
assert.equal(commit.audit.guardFailed, true);
|
|
295
|
+
assert.equal(commit.outbox[0].kind, "REQUEST_REVIEW");
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("6. outbox dispatcher handles AWAIT_CONFIRMATION without attempt increment, reject, and confirm", async () => {
|
|
299
|
+
const storage = await makeStorage();
|
|
300
|
+
const outbox = new OutboxDispatcher(storage, { maxAttempts: 3 });
|
|
301
|
+
const host = new MowaBrowserHost(kernel, { storage, outboxDispatcher: outbox });
|
|
302
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
303
|
+
|
|
304
|
+
let emailDrafted = false;
|
|
305
|
+
outbox.registerHandler("REQUEST_REVIEW", async (record) => {
|
|
306
|
+
emailDrafted = true;
|
|
307
|
+
// Pre-filled action card awaiting human confirmation
|
|
308
|
+
return "AWAIT_CONFIRMATION";
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
const sessionId = "SESSION_OUTBOX_TEST";
|
|
312
|
+
await host.startSession({
|
|
313
|
+
sessionId,
|
|
314
|
+
event: {
|
|
315
|
+
id: "E0",
|
|
316
|
+
type: "INIT",
|
|
317
|
+
source: "test",
|
|
318
|
+
caseId: "CASE_OB",
|
|
319
|
+
timestamp: 1,
|
|
320
|
+
idempotencyKey: "k0",
|
|
321
|
+
actor: { type: "SYSTEM", id: "S" },
|
|
322
|
+
payload: {},
|
|
323
|
+
},
|
|
324
|
+
flowHash,
|
|
325
|
+
ctx: { asOf: 1, contextRefs: {} },
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
const commit = await host.advanceSession({
|
|
329
|
+
sessionId,
|
|
330
|
+
event: {
|
|
331
|
+
id: "E1",
|
|
332
|
+
type: "SUBMIT_DOCS",
|
|
333
|
+
source: "test",
|
|
334
|
+
caseId: "CASE_OB",
|
|
335
|
+
timestamp: 2,
|
|
336
|
+
idempotencyKey: "k1",
|
|
337
|
+
actor: { type: "SYSTEM", id: "S" },
|
|
338
|
+
payload: {},
|
|
339
|
+
},
|
|
340
|
+
facts: { facts: { "doc:invoice": "MISSING" } },
|
|
341
|
+
ctx: { asOf: 2, contextRefs: {} },
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
const draftEffectId = commit.outbox[0].id;
|
|
345
|
+
|
|
346
|
+
// Run outbox dispatch: should transition to AWAIT_CONFIRMATION
|
|
347
|
+
await outbox.dispatchPending();
|
|
348
|
+
assert.equal(emailDrafted, true);
|
|
349
|
+
|
|
350
|
+
const record1 = await storage.getOutbox(draftEffectId);
|
|
351
|
+
assert.ok(record1);
|
|
352
|
+
assert.equal(record1.status, "AWAIT_CONFIRMATION");
|
|
353
|
+
assert.equal(record1.attempts, 0); // Must NOT increment attempts!
|
|
354
|
+
|
|
355
|
+
// Run 5 more unrelated dispatch passes: attempts MUST STAY 0 and NOT die in FAILED_DLQ!
|
|
356
|
+
for (let i = 0; i < 5; i++) {
|
|
357
|
+
await outbox.dispatchPending();
|
|
358
|
+
}
|
|
359
|
+
const recordAfterPasses = await storage.getOutbox(draftEffectId);
|
|
360
|
+
assert.equal(recordAfterPasses?.status, "AWAIT_CONFIRMATION");
|
|
361
|
+
assert.equal(recordAfterPasses?.attempts, 0);
|
|
362
|
+
|
|
363
|
+
// Confirm effect
|
|
364
|
+
await host.confirmEffect(draftEffectId);
|
|
365
|
+
const confirmedRecord = await storage.getOutbox(draftEffectId);
|
|
366
|
+
assert.equal(confirmedRecord?.status, "DISPATCHED");
|
|
367
|
+
|
|
368
|
+
// Cannot confirm an already dispatched effect
|
|
369
|
+
await assert.rejects(
|
|
370
|
+
() => host.confirmEffect(draftEffectId),
|
|
371
|
+
/expected 'AWAIT_CONFIRMATION'/
|
|
372
|
+
);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it("7. unhandled effect kind pushes backoff without blocking other effects", async () => {
|
|
376
|
+
const storage = await makeStorage();
|
|
377
|
+
const outbox = new OutboxDispatcher(storage, {
|
|
378
|
+
maxAttempts: 3,
|
|
379
|
+
unhandledGraceIntervalMs: 1000,
|
|
380
|
+
maxUnhandledGraceMs: 5000,
|
|
381
|
+
});
|
|
382
|
+
const host = new MowaBrowserHost(kernel, { storage, outboxDispatcher: outbox });
|
|
383
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
384
|
+
|
|
385
|
+
// Register handler for INTAKE effect but leave REQUEST_REVIEW unhandled
|
|
386
|
+
let intakeHandled = false;
|
|
387
|
+
outbox.registerHandler("EMIT_INTAKE_NOTIFICATION", async () => {
|
|
388
|
+
intakeHandled = true;
|
|
389
|
+
return "SUCCESS";
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
const sessionId = "SESSION_UNHANDLED_TEST";
|
|
393
|
+
await host.startSession({
|
|
394
|
+
sessionId,
|
|
395
|
+
event: {
|
|
396
|
+
id: "E0",
|
|
397
|
+
type: "INIT",
|
|
398
|
+
source: "test",
|
|
399
|
+
caseId: "CASE_UNH",
|
|
400
|
+
timestamp: 1,
|
|
401
|
+
idempotencyKey: "k_unh_0",
|
|
402
|
+
actor: { type: "SYSTEM", id: "S" },
|
|
403
|
+
payload: {},
|
|
404
|
+
},
|
|
405
|
+
flowHash,
|
|
406
|
+
ctx: { asOf: 1, contextRefs: {} },
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
// Advance creates REQUEST_REVIEW which has NO handler registered
|
|
410
|
+
await host.advanceSession({
|
|
411
|
+
sessionId,
|
|
412
|
+
event: {
|
|
413
|
+
id: "E1",
|
|
414
|
+
type: "SUBMIT_DOCS",
|
|
415
|
+
source: "test",
|
|
416
|
+
caseId: "CASE_UNH",
|
|
417
|
+
timestamp: 2,
|
|
418
|
+
idempotencyKey: "k_unh_1",
|
|
419
|
+
actor: { type: "SYSTEM", id: "S" },
|
|
420
|
+
payload: {},
|
|
421
|
+
},
|
|
422
|
+
facts: { facts: { "doc:invoice": "MISSING" } },
|
|
423
|
+
ctx: { asOf: 2, contextRefs: {} },
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
// Intake effect was already handled during startSession
|
|
427
|
+
assert.equal(intakeHandled, true);
|
|
428
|
+
|
|
429
|
+
// REQUEST_REVIEW was unhandled: verify it was deferred with grace interval
|
|
430
|
+
const pending = await storage.getPendingOutbox(10, Date.now() + 20000);
|
|
431
|
+
const unhandledRecord = pending.find((p) => p.effect.kind === "REQUEST_REVIEW");
|
|
432
|
+
assert.ok(unhandledRecord);
|
|
433
|
+
assert.equal(unhandledRecord.status, "PENDING");
|
|
434
|
+
assert.ok(unhandledRecord.nextAttemptAt > 0);
|
|
435
|
+
|
|
436
|
+
// Register handler now (simulating late registration in service worker)
|
|
437
|
+
let reviewHandled = false;
|
|
438
|
+
outbox.registerHandler("REQUEST_REVIEW", async () => {
|
|
439
|
+
reviewHandled = true;
|
|
440
|
+
return "SUCCESS";
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
// Make it immediately due and dispatch
|
|
444
|
+
await storage.updateOutboxStatus(unhandledRecord.outboxId, "PENDING", {
|
|
445
|
+
nextAttemptAt: Date.now() - 100,
|
|
446
|
+
});
|
|
447
|
+
const res = await outbox.dispatchPending();
|
|
448
|
+
assert.equal(res.processed, 1);
|
|
449
|
+
assert.equal(reviewHandled, true);
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
it("7b. requeueEffect clears unhandledSince and lastError, giving fresh grace period", async () => {
|
|
453
|
+
const storage = await makeStorage();
|
|
454
|
+
const outbox = new OutboxDispatcher(storage, {
|
|
455
|
+
maxAttempts: 3,
|
|
456
|
+
unhandledGraceIntervalMs: 500,
|
|
457
|
+
maxUnhandledGraceMs: 1000,
|
|
458
|
+
});
|
|
459
|
+
const host = new MowaBrowserHost(kernel, { storage, outboxDispatcher: outbox });
|
|
460
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
461
|
+
|
|
462
|
+
const sessionId = "SESSION_REQUEUE_GRACE";
|
|
463
|
+
await host.startSession({
|
|
464
|
+
sessionId,
|
|
465
|
+
event: {
|
|
466
|
+
id: "E0",
|
|
467
|
+
type: "INIT",
|
|
468
|
+
source: "test",
|
|
469
|
+
caseId: "CASE_REQ",
|
|
470
|
+
timestamp: 1,
|
|
471
|
+
idempotencyKey: "k_req_0",
|
|
472
|
+
actor: { type: "SYSTEM", id: "S" },
|
|
473
|
+
payload: {},
|
|
474
|
+
},
|
|
475
|
+
flowHash,
|
|
476
|
+
ctx: { asOf: 1, contextRefs: {} },
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
await host.advanceSession({
|
|
480
|
+
sessionId,
|
|
481
|
+
event: {
|
|
482
|
+
id: "E1",
|
|
483
|
+
type: "SUBMIT_DOCS",
|
|
484
|
+
source: "test",
|
|
485
|
+
caseId: "CASE_REQ",
|
|
486
|
+
timestamp: 2,
|
|
487
|
+
idempotencyKey: "k_req_1",
|
|
488
|
+
actor: { type: "SYSTEM", id: "S" },
|
|
489
|
+
payload: {},
|
|
490
|
+
},
|
|
491
|
+
facts: { facts: { "doc:invoice": "MISSING" } },
|
|
492
|
+
ctx: { asOf: 2, contextRefs: {} },
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
const pending = await storage.getPendingOutbox(10, Date.now() + 20000);
|
|
496
|
+
const unhandled = pending.find((p) => p.effect.kind === "REQUEST_REVIEW");
|
|
497
|
+
assert.ok(unhandled);
|
|
498
|
+
|
|
499
|
+
// Simulate grace period expiry (past 1000ms)
|
|
500
|
+
await storage.updateOutboxStatus(unhandled.outboxId, "PENDING", {
|
|
501
|
+
unhandledSince: Date.now() - 2000,
|
|
502
|
+
nextAttemptAt: Date.now() - 10,
|
|
503
|
+
});
|
|
504
|
+
await outbox.dispatchPending();
|
|
505
|
+
|
|
506
|
+
// Effect should now be in FAILED_DLQ with UNHANDLED_EFFECT_KIND
|
|
507
|
+
const dlqRecord = await storage.getOutbox(unhandled.outboxId);
|
|
508
|
+
assert.equal(dlqRecord?.status, "FAILED_DLQ");
|
|
509
|
+
assert.match(dlqRecord?.lastError ?? "", /UNHANDLED_EFFECT_KIND/);
|
|
510
|
+
|
|
511
|
+
// Requeue effect
|
|
512
|
+
await host.requeueEffect(unhandled.outboxId);
|
|
513
|
+
|
|
514
|
+
// Verify unhandledSince and lastError are cleared
|
|
515
|
+
const requeuedRecord = await storage.getOutbox(unhandled.outboxId);
|
|
516
|
+
assert.equal(requeuedRecord?.status, "PENDING");
|
|
517
|
+
assert.equal(requeuedRecord?.unhandledSince, undefined);
|
|
518
|
+
assert.equal(requeuedRecord?.lastError, undefined);
|
|
519
|
+
|
|
520
|
+
// Run dispatch again without registered handler: should be granted a FRESH grace period, NOT go straight back to DLQ!
|
|
521
|
+
await outbox.dispatchPending();
|
|
522
|
+
const freshRecord = await storage.getOutbox(unhandled.outboxId);
|
|
523
|
+
assert.equal(freshRecord?.status, "PENDING"); // Must still be pending under its new grace period!
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
it("8. idempotency conflict: reusing dedupKey with altered event intent throws IdempotencyConflictError", async () => {
|
|
527
|
+
const storage = await makeStorage();
|
|
528
|
+
const host = new MowaBrowserHost(kernel, { storage });
|
|
529
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
530
|
+
|
|
531
|
+
const sessionId = "SESSION_IDEM_CONFLICT";
|
|
532
|
+
const baseEvent = {
|
|
533
|
+
id: "EVT_BASE",
|
|
534
|
+
type: "INIT",
|
|
535
|
+
source: "client_app",
|
|
536
|
+
caseId: "CASE_IDEM_CONF",
|
|
537
|
+
timestamp: 1000,
|
|
538
|
+
idempotencyKey: "reused_idem_key",
|
|
539
|
+
actor: { type: "SYSTEM", id: "S1" },
|
|
540
|
+
payload: { value: 100 },
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
// First call succeeds
|
|
544
|
+
await host.startSession({
|
|
545
|
+
sessionId,
|
|
546
|
+
event: baseEvent,
|
|
547
|
+
flowHash,
|
|
548
|
+
ctx: { asOf: 1000, contextRefs: {} },
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
// Replay with exact same intent (even with different ephemeral event id) succeeds idempotently
|
|
552
|
+
const replayEvent = { ...baseEvent, id: "EVT_NEW_EPHEMERAL_ID", timestamp: 1050 };
|
|
553
|
+
const replayCommit = await host.startSession({
|
|
554
|
+
sessionId,
|
|
555
|
+
event: replayEvent,
|
|
556
|
+
flowHash,
|
|
557
|
+
ctx: { asOf: 1000, contextRefs: {} },
|
|
558
|
+
});
|
|
559
|
+
assert.equal(replayCommit.newSession.id, sessionId);
|
|
560
|
+
|
|
561
|
+
// Replay with CONFLICTING semantic intent (different payload value) throws IdempotencyConflictError!
|
|
562
|
+
const conflictingEvent = { ...baseEvent, payload: { value: 999 } };
|
|
563
|
+
await assert.rejects(
|
|
564
|
+
() =>
|
|
565
|
+
host.startSession({
|
|
566
|
+
sessionId,
|
|
567
|
+
event: conflictingEvent,
|
|
568
|
+
flowHash,
|
|
569
|
+
ctx: { asOf: 1000, contextRefs: {} },
|
|
570
|
+
}),
|
|
571
|
+
(err) => {
|
|
572
|
+
assert.ok(err instanceof IdempotencyConflictError);
|
|
573
|
+
assert.equal(err.dedupKey, replayCommit.dedupKey);
|
|
574
|
+
return true;
|
|
575
|
+
}
|
|
576
|
+
);
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
it("9. wasm cache eviction auto-reload: evicted flow reloads seamlessly from storage", async () => {
|
|
580
|
+
const storage = await makeStorage();
|
|
581
|
+
const host = new MowaBrowserHost(kernel, { storage });
|
|
582
|
+
|
|
583
|
+
// Install 35 distinct flows to exceed the 32-capacity WASM cache
|
|
584
|
+
const flows = [];
|
|
585
|
+
for (let i = 0; i < 35; i++) {
|
|
586
|
+
const f = {
|
|
587
|
+
...sampleFlow(),
|
|
588
|
+
id: `FLOW_${i}`,
|
|
589
|
+
};
|
|
590
|
+
const { flowHash } = await host.installFlow(f);
|
|
591
|
+
flows.push({ flowHash, id: `FLOW_${i}` });
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// The first installed flow (FLOW_0) has been evicted from the WASM cache
|
|
595
|
+
const oldest = flows[0];
|
|
596
|
+
|
|
597
|
+
// Starting a session with the evicted flow must auto-reload from storage without error!
|
|
598
|
+
const commit = await host.startSession({
|
|
599
|
+
sessionId: "SESSION_RELOAD_TEST",
|
|
600
|
+
event: {
|
|
601
|
+
id: "E0",
|
|
602
|
+
type: "INIT",
|
|
603
|
+
source: "test",
|
|
604
|
+
caseId: "CASE_RELOAD",
|
|
605
|
+
timestamp: 1,
|
|
606
|
+
idempotencyKey: "k_reload",
|
|
607
|
+
actor: { type: "SYSTEM", id: "S" },
|
|
608
|
+
payload: {},
|
|
609
|
+
},
|
|
610
|
+
flowHash: oldest.flowHash,
|
|
611
|
+
ctx: { asOf: 1, contextRefs: {} },
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
assert.equal(commit.newSession.workflowId, oldest.id);
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
it("10. concurrent calls on same session serialize with zero CAS conflicts", async () => {
|
|
618
|
+
const storage = await makeStorage();
|
|
619
|
+
const lock = new MemorySessionLock();
|
|
620
|
+
const host = new MowaBrowserHost(kernel, { storage, lock });
|
|
621
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
622
|
+
|
|
623
|
+
const sessionId = "SESSION_CONCURRENT";
|
|
624
|
+
await host.startSession({
|
|
625
|
+
sessionId,
|
|
626
|
+
event: {
|
|
627
|
+
id: "E_START",
|
|
628
|
+
type: "INIT",
|
|
629
|
+
source: "test",
|
|
630
|
+
caseId: "CASE_CONCUR",
|
|
631
|
+
timestamp: 1,
|
|
632
|
+
idempotencyKey: "k_concur_start",
|
|
633
|
+
actor: { type: "SYSTEM", id: "S" },
|
|
634
|
+
payload: {},
|
|
635
|
+
},
|
|
636
|
+
flowHash,
|
|
637
|
+
ctx: { asOf: 1, contextRefs: {} },
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
// Fire 5 identical requests concurrently
|
|
641
|
+
const promises = Array.from({ length: 5 }, (_, i) =>
|
|
642
|
+
host.advanceSession({
|
|
643
|
+
sessionId,
|
|
644
|
+
event: {
|
|
645
|
+
id: `EVT_CONCUR_${i}`,
|
|
646
|
+
type: "SUBMIT_DOCS",
|
|
647
|
+
source: "action_card",
|
|
648
|
+
caseId: "CASE_CONCUR",
|
|
649
|
+
timestamp: 10 + i,
|
|
650
|
+
idempotencyKey: `idem_concur_identical`, // Same idempotency key!
|
|
651
|
+
actor: { type: "SYSTEM", id: "S" },
|
|
652
|
+
payload: {},
|
|
653
|
+
},
|
|
654
|
+
facts: { facts: { "doc:invoice": "VERIFIED" } },
|
|
655
|
+
ctx: { asOf: 10 + i, contextRefs: {} },
|
|
656
|
+
})
|
|
657
|
+
);
|
|
658
|
+
|
|
659
|
+
const results = await Promise.all(promises);
|
|
660
|
+
|
|
661
|
+
// All 5 must succeed with the exact same commit and zero CAS errors!
|
|
662
|
+
const firstCommit = results[0];
|
|
663
|
+
for (const r of results) {
|
|
664
|
+
assert.equal(r.newSession.version, 1);
|
|
665
|
+
assert.equal(r.audit.transitionId, firstCommit.audit.transitionId);
|
|
666
|
+
}
|
|
667
|
+
});
|
|
668
|
+
|
|
669
|
+
it("11. getOutboxByStatus queries outbox records by status", async () => {
|
|
670
|
+
const storage = await makeStorage();
|
|
671
|
+
const outbox = new OutboxDispatcher(storage);
|
|
672
|
+
const host = new MowaBrowserHost(kernel, { storage, outboxDispatcher: outbox });
|
|
673
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
674
|
+
|
|
675
|
+
outbox.registerHandler("EMIT_INTAKE_NOTIFICATION", async () => "AWAIT_CONFIRMATION");
|
|
676
|
+
|
|
677
|
+
const sessionId = "SESSION_STATUS_QUERY";
|
|
678
|
+
const commit = await host.startSession({
|
|
679
|
+
sessionId,
|
|
680
|
+
event: {
|
|
681
|
+
id: "E0_STATUS",
|
|
682
|
+
type: "START",
|
|
683
|
+
source: "test",
|
|
684
|
+
caseId: "CASE_STATUS",
|
|
685
|
+
timestamp: 1000,
|
|
686
|
+
idempotencyKey: "k_status",
|
|
687
|
+
actor: { type: "SYSTEM", id: "S" },
|
|
688
|
+
payload: {},
|
|
689
|
+
},
|
|
690
|
+
flowHash,
|
|
691
|
+
ctx: { asOf: 1000, contextRefs: {} },
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
// Run outbox dispatch to move effect to AWAIT_CONFIRMATION
|
|
695
|
+
await outbox.dispatchPending();
|
|
696
|
+
|
|
697
|
+
const pendingConfirmation = await host.getOutboxByStatus("AWAIT_CONFIRMATION");
|
|
698
|
+
assert.equal(pendingConfirmation.length, 1);
|
|
699
|
+
assert.equal(pendingConfirmation[0].outboxId, commit.outbox[0].id);
|
|
700
|
+
assert.equal(pendingConfirmation[0].status, "AWAIT_CONFIRMATION");
|
|
701
|
+
|
|
702
|
+
const dispatched = await host.getOutboxByStatus("DISPATCHED");
|
|
703
|
+
assert.equal(dispatched.length, 0);
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
it("12. every step's inputs are kept, and an exported bundle replays to identical commits", async () => {
|
|
707
|
+
const storage = await makeStorage();
|
|
708
|
+
const host = new MowaBrowserHost(kernel, { storage, outboxDispatcher: new OutboxDispatcher(storage) });
|
|
709
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
710
|
+
const sessionId = "SESSION_REPLAY";
|
|
711
|
+
const ev = (id, type, actor, ts) => ({
|
|
712
|
+
id, type, source: "test", caseId: "CASE_REPLAY", timestamp: ts,
|
|
713
|
+
idempotencyKey: `k_${id}`, actor: { type: actor, id: "u1" }, payload: {},
|
|
714
|
+
});
|
|
715
|
+
|
|
716
|
+
await host.startSession({ sessionId, event: ev("E0", "INIT", "SYSTEM", 1000), flowHash, ctx: { asOf: 1000 } });
|
|
717
|
+
const facts = { facts: { "doc:invoice": "MISSING", "other:ignored": "X" } };
|
|
718
|
+
const submit = { sessionId, event: ev("E1", "SUBMIT_DOCS", "OPERATOR", 2000), facts, ctx: { asOf: 2000, contextRefs: { feed: "v1" } } };
|
|
719
|
+
await host.advanceSession(submit);
|
|
720
|
+
await host.advanceSession(submit); // duplicate delivery: returns the stored commit, adds no step
|
|
721
|
+
await host.advanceSession({ sessionId, event: ev("E2", "RESOLVE", "OPERATOR", 3000), ctx: { asOf: 3000 } });
|
|
722
|
+
|
|
723
|
+
const steps = await storage.getSteps(sessionId);
|
|
724
|
+
assert.deepEqual(steps.map((s) => [s.call, s.version]), [["start", 0], ["advance", 1], ["advance", 2]]);
|
|
725
|
+
assert.deepEqual(steps[1].facts, facts, "facts are kept exactly as supplied");
|
|
726
|
+
assert.deepEqual(steps[1].ctx, { asOf: 2000, contextRefs: { feed: "v1" } });
|
|
727
|
+
assert.equal(steps[0].facts, undefined, "start takes no facts");
|
|
728
|
+
|
|
729
|
+
const bundle = await host.exportVerificationBundle([sessionId], { feed: "reference data v1" });
|
|
730
|
+
assert.equal(bundle.format, "mowa.verification-bundle/1");
|
|
731
|
+
assert.equal(bundle.kernelVersion, wasmPkg.kernelVersion());
|
|
732
|
+
assert.deepEqual(Object.keys(bundle.flows), [flowHash]);
|
|
733
|
+
assert.deepEqual(bundle.contextData, { feed: "reference data v1" });
|
|
734
|
+
|
|
735
|
+
// Replay: a verifier with only the bundle and the open kernel reproduces every commit.
|
|
736
|
+
const replayed = JSON.parse(JSON.stringify(bundle)); // as if read from a file
|
|
737
|
+
let session = null;
|
|
738
|
+
for (const step of replayed.sessions[0].steps) {
|
|
739
|
+
const flow = replayed.flows[step.commit.newSession.flowHash];
|
|
740
|
+
const out = JSON.parse(
|
|
741
|
+
step.call === "start"
|
|
742
|
+
? wasmPkg.startJson(JSON.stringify({ sessionId: replayed.sessions[0].sessionId, event: step.event, flow, ctx: step.ctx }))
|
|
743
|
+
: wasmPkg.advanceJson(JSON.stringify({ session, event: step.event, flow, facts: step.facts, ctx: step.ctx }))
|
|
744
|
+
);
|
|
745
|
+
assert.equal(out.status, "OK");
|
|
746
|
+
assert.deepEqual(out.commit, step.commit, `${step.call} v${step.commit.newSession.version} replays identically`);
|
|
747
|
+
assert.equal(step.commit.audit.prevResultHash, session ? session.lastResultHash : null, "chain link");
|
|
748
|
+
session = out.commit.newSession;
|
|
749
|
+
}
|
|
750
|
+
});
|
|
751
|
+
|
|
752
|
+
it("13. export refuses a session without a complete step history", async () => {
|
|
753
|
+
const storage = await makeStorage();
|
|
754
|
+
const host = new MowaBrowserHost(kernel, { storage, outboxDispatcher: new OutboxDispatcher(storage) });
|
|
755
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
756
|
+
const commit = await host.startSession({
|
|
757
|
+
sessionId: "S_OLD",
|
|
758
|
+
event: { id: "E", type: "INIT", source: "t", caseId: "C", timestamp: 1, idempotencyKey: "k", actor: { type: "SYSTEM", id: "s" }, payload: {} },
|
|
759
|
+
flowHash,
|
|
760
|
+
ctx: { asOf: 1 },
|
|
761
|
+
});
|
|
762
|
+
// Simulate a session written by an older host: same session, no step records.
|
|
763
|
+
const legacy = await makeStorage();
|
|
764
|
+
await legacy.saveFlow(flowHash, sampleFlow());
|
|
765
|
+
await legacy.atomicCommit("S_OLD", null, commit, { intentHash: "x", inputs: { call: "start", event: {}, ctx: {} } });
|
|
766
|
+
await clearSteps(legacy);
|
|
767
|
+
const legacyHost = new MowaBrowserHost(kernel, { storage: legacy, outboxDispatcher: new OutboxDispatcher(legacy) });
|
|
768
|
+
await assert.rejects(legacyHost.exportVerificationBundle(["S_OLD"]), (err) => {
|
|
769
|
+
assert.ok(err instanceof IncompleteStepHistoryError);
|
|
770
|
+
assert.equal(err.name, "IncompleteStepHistoryError");
|
|
771
|
+
assert.equal(err.sessionId, "S_OLD");
|
|
772
|
+
assert.equal(err.stepCount, 0);
|
|
773
|
+
assert.equal(err.sessionVersion, 0);
|
|
774
|
+
assert.match(err.message, /incomplete step history/);
|
|
775
|
+
return true;
|
|
776
|
+
});
|
|
777
|
+
await assert.rejects(host.exportVerificationBundle(["NOPE"]), /not found/);
|
|
778
|
+
});
|
|
779
|
+
|
|
780
|
+
it("14. the host decides and stores a snapshot of its inputs, not the caller's live objects", async () => {
|
|
781
|
+
// Storage that waits before writing, as IndexedDB does while it opens the database.
|
|
782
|
+
const storage = slowCommits(await makeStorage());
|
|
783
|
+
const host = new MowaBrowserHost(kernel, { storage });
|
|
784
|
+
const flow = sampleFlow();
|
|
785
|
+
const { flowHash } = await host.installFlow(flow);
|
|
786
|
+
flow.nodes.INTAKE.type = "changed after install";
|
|
787
|
+
assert.equal((await storage.getFlow(flowHash)).nodes.INTAKE.type, "t", "installed flow is a snapshot");
|
|
788
|
+
|
|
789
|
+
// 1. The caller changes its event while the commit is waiting on storage.
|
|
790
|
+
const event = {
|
|
791
|
+
id: "EVT_SNAP", type: "INITIALIZE", source: "test", caseId: "CASE_SNAP", timestamp: 1000,
|
|
792
|
+
idempotencyKey: "idem_snap", actor: { type: "OPERATOR", id: "A" }, payload: { amount: 1 },
|
|
793
|
+
};
|
|
794
|
+
const ctx = { asOf: 1000, contextRefs: { ref: "r1" } };
|
|
795
|
+
const pending = host.startSession({ sessionId: "S_SNAP", event, flowHash, ctx });
|
|
796
|
+
setTimeout(() => { event.payload.amount = 999; ctx.contextRefs.ref = "r2"; }, 1);
|
|
797
|
+
const commit = await pending;
|
|
798
|
+
const [step] = await storage.getSteps("S_SNAP");
|
|
799
|
+
assert.deepEqual(step.event.payload, { amount: 1 });
|
|
800
|
+
assert.deepEqual(step.ctx.contextRefs, { ref: "r1" });
|
|
801
|
+
const replay = kernel.start("S_SNAP", step.event, sampleFlow(), step.ctx);
|
|
802
|
+
assert.equal(replay.audit.resultHash, commit.audit.resultHash, "stored inputs replay to the same commit");
|
|
803
|
+
|
|
804
|
+
// 2. A getter is read once, so the dedup key, the decision and the stored step agree.
|
|
805
|
+
let reads = 0;
|
|
806
|
+
const tricky = {
|
|
807
|
+
id: "EVT_GET", type: "INITIALIZE", source: "test", caseId: "CASE_GET", timestamp: 1000,
|
|
808
|
+
actor: { type: "OPERATOR", id: "A" }, payload: {},
|
|
809
|
+
};
|
|
810
|
+
Object.defineProperty(tricky, "idempotencyKey", { enumerable: true, get: () => `k${reads++}` });
|
|
811
|
+
const c2 = await host.startSession({ sessionId: "S_GET", event: tricky, flowHash, ctx: { asOf: 1000, contextRefs: {} } });
|
|
812
|
+
assert.equal(reads, 1);
|
|
813
|
+
const [step2] = await storage.getSteps("S_GET");
|
|
814
|
+
assert.equal(step2.event.idempotencyKey, "k0");
|
|
815
|
+
assert.equal(kernel.start("S_GET", step2.event, sampleFlow(), step2.ctx).audit.resultHash, c2.audit.resultHash);
|
|
816
|
+
|
|
817
|
+
// 3. Values JSON cannot carry are rejected at the boundary, not passed on.
|
|
818
|
+
const looped = { ...event, idempotencyKey: "idem_loop" };
|
|
819
|
+
looped.payload = { self: null };
|
|
820
|
+
looped.payload.self = looped.payload;
|
|
821
|
+
await assert.rejects(host.startSession({ sessionId: "S_LOOP", event: looped, flowHash, ctx }), /Validation failed at 'event': not JSON-serialisable/);
|
|
822
|
+
});
|
|
823
|
+
|
|
824
|
+
it("15. a commit decided on a stale session version is refused and writes nothing", async () => {
|
|
825
|
+
const storage = await makeStorage();
|
|
826
|
+
const host = new MowaBrowserHost(kernel, { storage });
|
|
827
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
828
|
+
const ev = (id, type, ts) => ({
|
|
829
|
+
id, type, source: "test", caseId: "CASE_CAS", timestamp: ts,
|
|
830
|
+
idempotencyKey: `k_${id}`, actor: { type: "SYSTEM", id: "S" }, payload: {},
|
|
831
|
+
});
|
|
832
|
+
const ctx = (asOf) => ({ asOf, contextRefs: {} });
|
|
833
|
+
const start = await host.startSession({ sessionId: "S_CAS", event: ev("E0", "INIT", 1), flowHash, ctx: ctx(1) });
|
|
834
|
+
await host.advanceSession({
|
|
835
|
+
sessionId: "S_CAS", event: ev("E1", "SUBMIT_DOCS", 2), facts: { facts: { "doc:invoice": "VERIFIED" } }, ctx: ctx(2),
|
|
836
|
+
});
|
|
837
|
+
|
|
838
|
+
// A second writer decided from version 0 and tries to commit after version 1 landed.
|
|
839
|
+
const event = ev("E2", "SUBMIT_DOCS", 3);
|
|
840
|
+
const facts = { facts: { "doc:invoice": "MISSING" } };
|
|
841
|
+
const stale = kernel.advance(start.newSession, event, sampleFlow(), facts, ctx(3));
|
|
842
|
+
assert.equal(stale.expectedSessionVersion, 0);
|
|
843
|
+
await assert.rejects(
|
|
844
|
+
storage.atomicCommit("S_CAS", 0, stale, { intentHash: "stale", inputs: { call: "advance", event, facts, ctx: ctx(3) } }),
|
|
845
|
+
(err) => err instanceof CasConflictError
|
|
846
|
+
);
|
|
847
|
+
|
|
848
|
+
assert.equal((await storage.getSession("S_CAS")).version, 1, "session unchanged");
|
|
849
|
+
assert.equal((await storage.getSession("S_CAS")).state, "APPROVED");
|
|
850
|
+
assert.equal(await storage.getAudit(stale.audit.transitionId), null, "no audit record");
|
|
851
|
+
assert.equal(await storage.getIdempotency(stale.dedupKey), null, "no duplicate record");
|
|
852
|
+
assert.equal((await storage.getSteps("S_CAS")).length, 2, "no step record");
|
|
853
|
+
for (const effect of stale.outbox) assert.equal(await storage.getOutbox(effect.id), null, "no outbox record");
|
|
854
|
+
});
|
|
855
|
+
|
|
856
|
+
it("16. anchors: imported only when they fit the records, exported only when present", async () => {
|
|
857
|
+
const storage = await makeStorage();
|
|
858
|
+
const host = new MowaBrowserHost(kernel, { storage });
|
|
859
|
+
const { flowHash } = await host.installFlow(sampleFlow());
|
|
860
|
+
const ev = (id, type, ts) => ({
|
|
861
|
+
id, type, source: "test", caseId: "CASE_ANCHOR", timestamp: ts,
|
|
862
|
+
idempotencyKey: `k_${id}`, actor: { type: "SYSTEM", id: "S" }, payload: {},
|
|
863
|
+
});
|
|
864
|
+
const start = await host.startSession({ sessionId: "S_ANC", event: ev("E0", "INIT", 1), flowHash, ctx: { asOf: 1, contextRefs: {} } });
|
|
865
|
+
const step1 = await host.advanceSession({
|
|
866
|
+
sessionId: "S_ANC", event: ev("E1", "SUBMIT_DOCS", 2), facts: { facts: { "doc:invoice": "VERIFIED" } }, ctx: { asOf: 2, contextRefs: {} },
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
// No anchors yet: the export has no anchors field at all.
|
|
870
|
+
const plain = await host.exportVerificationBundle(["S_ANC"]);
|
|
871
|
+
assert.equal("anchors" in plain, false);
|
|
872
|
+
|
|
873
|
+
// The host checks shape and the match with its own steps; hashing is the verifier's job.
|
|
874
|
+
const h = (c) => c.repeat(64);
|
|
875
|
+
const anchor = (version, resultHash, over = {}) => ({
|
|
876
|
+
type: "mowa.anchor/1", sessionId: "S_ANC", version, resultHash, leafIndex: 0, inclusionPath: [h("a")],
|
|
877
|
+
checkpoint: { type: "mowa.anchor-checkpoint/1", origin: "test/log", treeSize: 2, rootHash: h("b"), time: 1000 + version },
|
|
878
|
+
signatures: [{ keyId: h("c"), signature: "AAAA" }],
|
|
879
|
+
...over,
|
|
880
|
+
});
|
|
881
|
+
const a0 = anchor(0, start.audit.resultHash);
|
|
882
|
+
const a1 = anchor(1, step1.audit.resultHash);
|
|
883
|
+
|
|
884
|
+
const refuse = async (list, pattern) =>
|
|
885
|
+
assert.rejects(host.importAnchors(list), (err) => err instanceof AnchorImportError && pattern.test(err.message));
|
|
886
|
+
await refuse([a0, anchor(1, h("d"))], /different resultHash/);
|
|
887
|
+
await refuse([anchor(5, h("d"))], /has no step 5/);
|
|
888
|
+
await refuse([{ ...a0, sessionId: "S_NOPE" }], /no session 'S_NOPE'/);
|
|
889
|
+
await refuse([{ ...a0, type: "mowa.anchor/2" }], /type must be/);
|
|
890
|
+
await refuse([{ ...a0, inclusionPath: ["XYZ"] }], /inclusionPath/);
|
|
891
|
+
await refuse([{ ...a0, signatures: [] }], /signatures/);
|
|
892
|
+
assert.deepEqual(await storage.getAnchors("S_ANC"), [], "a refused batch stores nothing, not even its valid anchors");
|
|
893
|
+
|
|
894
|
+
assert.deepEqual(await host.importAnchors([a1, a0, a0]), { imported: 2, unchanged: 1 });
|
|
895
|
+
assert.deepEqual(await host.importAnchors([a0]), { imported: 0, unchanged: 1 }, "importing again is a no-op");
|
|
896
|
+
await refuse([{ ...a0, leafIndex: 1 }], /different anchor for the same session, step, log and tree/);
|
|
897
|
+
|
|
898
|
+
const bundle = await host.exportVerificationBundle(["S_ANC"]);
|
|
899
|
+
assert.deepEqual(bundle.anchors, [a0, a1], "exported in version order");
|
|
900
|
+
|
|
901
|
+
// Storage without anchor support exports none and refuses imports.
|
|
902
|
+
const bare = new Proxy(storage, {
|
|
903
|
+
get: (t, k) => (k === "saveAnchor" || k === "getAnchors" ? undefined : typeof t[k] === "function" ? t[k].bind(t) : t[k]),
|
|
904
|
+
});
|
|
905
|
+
const bareHost = new MowaBrowserHost(kernel, { storage: bare });
|
|
906
|
+
assert.equal("anchors" in (await bareHost.exportVerificationBundle(["S_ANC"])), false);
|
|
907
|
+
await assert.rejects(bareHost.importAnchors([a0]), /does not support anchors/);
|
|
908
|
+
});
|
|
909
|
+
});
|
|
910
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mowaos/host-browser",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "MO·WA OS Browser Extension Host Adapter (WASM + IndexedDB + Web Locks)",
|
|
6
6
|
"author": "Bruce Gardiner (ScrAtcH)",
|
|
@@ -11,10 +11,15 @@
|
|
|
11
11
|
".": {
|
|
12
12
|
"types": "./dist/index.d.ts",
|
|
13
13
|
"import": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./conformance": {
|
|
16
|
+
"types": "./conformance/host-suite.d.ts",
|
|
17
|
+
"import": "./conformance/host-suite.mjs"
|
|
14
18
|
}
|
|
15
19
|
},
|
|
16
20
|
"files": [
|
|
17
21
|
"dist",
|
|
22
|
+
"conformance",
|
|
18
23
|
"README.md",
|
|
19
24
|
"LICENSE",
|
|
20
25
|
"NOTICE"
|