@mowaos/host-browser 0.0.0-stage → 0.5.1
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 +201 -0
- package/NOTICE +8 -0
- package/README.md +31 -2
- package/dist/host.d.ts +91 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +232 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/kernel.d.ts +33 -0
- package/dist/kernel.d.ts.map +1 -0
- package/dist/kernel.js +63 -0
- package/dist/kernel.js.map +1 -0
- package/dist/locks.d.ts +24 -0
- package/dist/locks.d.ts.map +1 -0
- package/dist/locks.js +46 -0
- package/dist/locks.js.map +1 -0
- package/dist/outbox.d.ts +68 -0
- package/dist/outbox.d.ts.map +1 -0
- package/dist/outbox.js +212 -0
- package/dist/outbox.js.map +1 -0
- package/dist/storage.d.ts +104 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +466 -0
- package/dist/storage.js.map +1 -0
- package/dist/types.d.ts +232 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +42 -0
- package/dist/types.js.map +1 -0
- package/dist/validation.d.ts +21 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +121 -0
- package/dist/validation.js.map +1 -0
- package/package.json +42 -4
package/dist/kernel.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WASM Kernel Bridge for MO·WA OS Browser Host.
|
|
3
|
+
* Bridges JavaScript calls to the Rust mowa-wasm module via JSON boundary.
|
|
4
|
+
*/
|
|
5
|
+
import { FlowHandleNotFoundError, } from "./types.js";
|
|
6
|
+
export class KernelExecutionError extends Error {
|
|
7
|
+
status;
|
|
8
|
+
details;
|
|
9
|
+
constructor(status, details) {
|
|
10
|
+
super(`Kernel execution failed with status '${status}': ${JSON.stringify(details)}`);
|
|
11
|
+
this.status = status;
|
|
12
|
+
this.details = details;
|
|
13
|
+
this.name = "KernelExecutionError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export class WasmKernelBridge {
|
|
17
|
+
wasm;
|
|
18
|
+
constructor(wasm) {
|
|
19
|
+
this.wasm = wasm;
|
|
20
|
+
}
|
|
21
|
+
getVersion() {
|
|
22
|
+
return this.wasm.kernelVersion();
|
|
23
|
+
}
|
|
24
|
+
loadFlow(flow) {
|
|
25
|
+
const raw = this.wasm.loadFlowJson(JSON.stringify(flow));
|
|
26
|
+
const res = JSON.parse(raw);
|
|
27
|
+
if (res.status !== "OK") {
|
|
28
|
+
throw new KernelExecutionError(res.status, res);
|
|
29
|
+
}
|
|
30
|
+
return { flowHandle: res.flowHandle, flowHash: res.flowHash };
|
|
31
|
+
}
|
|
32
|
+
dedupKey(op, sessionId, event) {
|
|
33
|
+
const raw = this.wasm.dedupKeyJson(JSON.stringify({ op, sessionId, event }));
|
|
34
|
+
const res = JSON.parse(raw);
|
|
35
|
+
if (res.status !== "OK") {
|
|
36
|
+
throw new KernelExecutionError(res.status, res);
|
|
37
|
+
}
|
|
38
|
+
return { dedupKey: res.dedupKey, intentHash: res.intentHash };
|
|
39
|
+
}
|
|
40
|
+
start(sessionId, event, flow, ctx) {
|
|
41
|
+
const raw = this.wasm.startJson(JSON.stringify({ sessionId, event, flow, ctx }));
|
|
42
|
+
const res = JSON.parse(raw);
|
|
43
|
+
if (res.status === "FLOW_HANDLE_NOT_FOUND") {
|
|
44
|
+
throw new FlowHandleNotFoundError(res.handle);
|
|
45
|
+
}
|
|
46
|
+
if (res.status !== "OK") {
|
|
47
|
+
throw new KernelExecutionError(res.status, res);
|
|
48
|
+
}
|
|
49
|
+
return res.commit;
|
|
50
|
+
}
|
|
51
|
+
advance(session, event, flow, facts, ctx) {
|
|
52
|
+
const raw = this.wasm.advanceJson(JSON.stringify({ session, event, flow, facts, ctx }));
|
|
53
|
+
const res = JSON.parse(raw);
|
|
54
|
+
if (res.status === "FLOW_HANDLE_NOT_FOUND") {
|
|
55
|
+
throw new FlowHandleNotFoundError(res.handle);
|
|
56
|
+
}
|
|
57
|
+
if (res.status !== "OK") {
|
|
58
|
+
throw new KernelExecutionError(res.status, res);
|
|
59
|
+
}
|
|
60
|
+
return res.commit;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=kernel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kernel.js","sourceRoot":"","sources":["../src/kernel.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,uBAAuB,GAUxB,MAAM,YAAY,CAAC;AAUpB,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAE3B;IACA;IAFlB,YACkB,MAAc,EACd,OAAgB;QAEhC,KAAK,CAAC,wCAAwC,MAAM,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAHrE,WAAM,GAAN,MAAM,CAAQ;QACd,YAAO,GAAP,OAAO,CAAS;QAGhC,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACrC,CAAC;CACF;AAED,MAAM,OAAO,gBAAgB;IACE;IAA7B,YAA6B,IAAsB;QAAtB,SAAI,GAAJ,IAAI,CAAkB;IAAG,CAAC;IAEvD,UAAU;QACR,OAAO,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,CAAC;IACnC,CAAC;IAED,QAAQ,CAAC,IAAoB;QAC3B,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;QACzD,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAqB,CAAC;QAEhD,IAAI,GAAG,CAAC,MAAM,KAAK,IAAI,EAAE,CAAC;YACxB,MAAM,IAAI,oBAAoB,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC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CAAC;IACpB,CAAC;IAED,OAAO,CACL,OAAwB,EACxB,KAAqB,EACrB,IAA6B,EAC7B,KAAoB,EACpB,GAAoB;QAEpB,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,CAC/B,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CACrD,CAAC;QACF,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAmB,CAAC;QAE9C,IAAI,GAAG,CAAC,MAAM,KAAK,uBAAuB,EAAE,CAAC;YAC3C,MAAM,IAAI,uBAAuB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QAChD,CAAC;QACD,IAAI,GAAG,CAAC,MAAM,KAAK,IAAI,EAAE,CAAC;YACxB,MAAM,IAAI,oBAAoB,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAClD,CAAC;QACD,OAAO,GAAG,CAAC,MAAM,CAAC;IACpB,CAAC;CACF"}
|
package/dist/locks.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Concurrency Mutex Subsystem for MO·WA OS Browser Host.
|
|
3
|
+
*
|
|
4
|
+
* Wraps the Web Locks API (`navigator.locks`) to serialize the complete
|
|
5
|
+
* read-compute-write cycle per session (`session:<id>`), eliminating CAS
|
|
6
|
+
* retries completely within a browser profile.
|
|
7
|
+
*/
|
|
8
|
+
export interface SessionLock {
|
|
9
|
+
acquire<T>(sessionId: string, fn: () => Promise<T>): Promise<T>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Browser-native session lock using Web Locks API (`navigator.locks`).
|
|
13
|
+
*/
|
|
14
|
+
export declare class WebLocksSessionLock implements SessionLock {
|
|
15
|
+
acquire<T>(sessionId: string, fn: () => Promise<T>): Promise<T>;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Deterministic in-memory FIFO queue lock for testing/Node environments.
|
|
19
|
+
*/
|
|
20
|
+
export declare class MemorySessionLock implements SessionLock {
|
|
21
|
+
private readonly queues;
|
|
22
|
+
acquire<T>(sessionId: string, fn: () => Promise<T>): Promise<T>;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=locks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"locks.d.ts","sourceRoot":"","sources":["../src/locks.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,WAAW,WAAW;IAC1B,OAAO,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CACjE;AAED;;GAEG;AACH,qBAAa,mBAAoB,YAAW,WAAW;IAC/C,OAAO,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAYtE;AAED;;GAEG;AACH,qBAAa,iBAAkB,YAAW,WAAW;IACnD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuC;IAExD,OAAO,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAoBtE"}
|
package/dist/locks.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Concurrency Mutex Subsystem for MO·WA OS Browser Host.
|
|
3
|
+
*
|
|
4
|
+
* Wraps the Web Locks API (`navigator.locks`) to serialize the complete
|
|
5
|
+
* read-compute-write cycle per session (`session:<id>`), eliminating CAS
|
|
6
|
+
* retries completely within a browser profile.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Browser-native session lock using Web Locks API (`navigator.locks`).
|
|
10
|
+
*/
|
|
11
|
+
export class WebLocksSessionLock {
|
|
12
|
+
async acquire(sessionId, fn) {
|
|
13
|
+
if (typeof navigator === "undefined" || !navigator.locks) {
|
|
14
|
+
throw new Error("Web Locks API (navigator.locks) is not available in the current environment");
|
|
15
|
+
}
|
|
16
|
+
const lockName = `session:${sessionId}`;
|
|
17
|
+
return navigator.locks.request(lockName, async () => {
|
|
18
|
+
return await fn();
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Deterministic in-memory FIFO queue lock for testing/Node environments.
|
|
24
|
+
*/
|
|
25
|
+
export class MemorySessionLock {
|
|
26
|
+
queues = new Map();
|
|
27
|
+
async acquire(sessionId, fn) {
|
|
28
|
+
const previous = this.queues.get(sessionId) ?? Promise.resolve();
|
|
29
|
+
let resolveNext;
|
|
30
|
+
const next = new Promise((resolve) => {
|
|
31
|
+
resolveNext = resolve;
|
|
32
|
+
});
|
|
33
|
+
this.queues.set(sessionId, next);
|
|
34
|
+
try {
|
|
35
|
+
await previous;
|
|
36
|
+
return await fn();
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
resolveNext();
|
|
40
|
+
if (this.queues.get(sessionId) === next) {
|
|
41
|
+
this.queues.delete(sessionId);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=locks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"locks.js","sourceRoot":"","sources":["../src/locks.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAMH;;GAEG;AACH,MAAM,OAAO,mBAAmB;IAC9B,KAAK,CAAC,OAAO,CAAI,SAAiB,EAAE,EAAoB;QACtD,IAAI,OAAO,SAAS,KAAK,WAAW,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;YACzD,MAAM,IAAI,KAAK,CACb,6EAA6E,CAC9E,CAAC;QACJ,CAAC;QAED,MAAM,QAAQ,GAAG,WAAW,SAAS,EAAE,CAAC;QACxC,OAAO,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,IAAI,EAAE;YAClD,OAAO,MAAM,EAAE,EAAE,CAAC;QACpB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAED;;GAEG;AACH,MAAM,OAAO,iBAAiB;IACX,MAAM,GAAG,IAAI,GAAG,EAA4B,CAAC;IAE9D,KAAK,CAAC,OAAO,CAAI,SAAiB,EAAE,EAAoB;QACtD,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;QAEjE,IAAI,WAAuB,CAAC;QAC5B,MAAM,IAAI,GAAG,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;YACzC,WAAW,GAAG,OAAO,CAAC;QACxB,CAAC,CAAC,CAAC;QAEH,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;QAEjC,IAAI,CAAC;YACH,MAAM,QAAQ,CAAC;YACf,OAAO,MAAM,EAAE,EAAE,CAAC;QACpB,CAAC;gBAAS,CAAC;YACT,WAAY,EAAE,CAAC;YACf,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,IAAI,EAAE,CAAC;gBACxC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;YAChC,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
package/dist/outbox.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Outbox Dispatcher Subsystem for MO·WA OS Browser Host.
|
|
3
|
+
*
|
|
4
|
+
* Runs asynchronously outside the session lock to dispatch side effects.
|
|
5
|
+
* Supports:
|
|
6
|
+
* - At-least-once delivery with deterministic effect.id deduplication.
|
|
7
|
+
* - True exponential backoff with jitter and Dead-Letter Queue (`FAILED_DLQ`).
|
|
8
|
+
* - "Pre-fill & Confirm" UI gating pattern with `AWAIT_CONFIRMATION` status segregation.
|
|
9
|
+
* - Grace period handling for unhandled effect kinds (prevents queue deadlock).
|
|
10
|
+
* - Operator recovery APIs: `requeueEffect`, `rejectEffect`, and `getNextDueAt`.
|
|
11
|
+
*/
|
|
12
|
+
import type { MowaStorage } from "./storage.js";
|
|
13
|
+
import type { OutboxRecord } from "./types.js";
|
|
14
|
+
export type EffectExecutionResult = "SUCCESS" | "AWAIT_CONFIRMATION" | "RETRY";
|
|
15
|
+
export type EffectHandler = (record: OutboxRecord) => Promise<EffectExecutionResult>;
|
|
16
|
+
export interface OutboxDispatcherOptions {
|
|
17
|
+
maxAttempts?: number;
|
|
18
|
+
batchSize?: number;
|
|
19
|
+
baseBackoffMs?: number;
|
|
20
|
+
maxBackoffMs?: number;
|
|
21
|
+
unhandledGraceIntervalMs?: number;
|
|
22
|
+
maxUnhandledGraceMs?: number;
|
|
23
|
+
}
|
|
24
|
+
export declare class OutboxDispatcher {
|
|
25
|
+
private readonly storage;
|
|
26
|
+
private readonly handlers;
|
|
27
|
+
private readonly maxAttempts;
|
|
28
|
+
private readonly batchSize;
|
|
29
|
+
private readonly baseBackoffMs;
|
|
30
|
+
private readonly maxBackoffMs;
|
|
31
|
+
private readonly unhandledGraceIntervalMs;
|
|
32
|
+
private readonly maxUnhandledGraceMs;
|
|
33
|
+
private isRunning;
|
|
34
|
+
constructor(storage: MowaStorage, options?: OutboxDispatcherOptions);
|
|
35
|
+
registerHandler(kind: string, handler: EffectHandler): void;
|
|
36
|
+
/**
|
|
37
|
+
* Calculates exponential backoff with ±10% jitter:
|
|
38
|
+
* delay = min(maxBackoff, base * 2^(attempts-1)) * (0.9 to 1.1)
|
|
39
|
+
*/
|
|
40
|
+
private calculateBackoff;
|
|
41
|
+
/**
|
|
42
|
+
* Returns earliest timestamp when a pending outbox record is due, or null if none pending.
|
|
43
|
+
* Host or extension can inspect this to schedule alarms (e.g. via chrome.alarms).
|
|
44
|
+
*/
|
|
45
|
+
getNextDueAt(): Promise<number | null>;
|
|
46
|
+
/**
|
|
47
|
+
* Called when a human operator reviews and confirms an action.
|
|
48
|
+
* Verifies the effect is in `AWAIT_CONFIRMATION` before marking `DISPATCHED`.
|
|
49
|
+
*/
|
|
50
|
+
confirmEffect(outboxId: string): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Called when an operator explicitly rejects an action awaiting confirmation.
|
|
53
|
+
*/
|
|
54
|
+
rejectEffect(outboxId: string, reason: string): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Operator recovery API: requeues an effect from `FAILED_DLQ` back to `PENDING`.
|
|
57
|
+
*/
|
|
58
|
+
requeueEffect(outboxId: string): Promise<void>;
|
|
59
|
+
/**
|
|
60
|
+
* Drain pending outbox records asynchronously outside the session lock.
|
|
61
|
+
*/
|
|
62
|
+
dispatchPending(): Promise<{
|
|
63
|
+
processed: number;
|
|
64
|
+
failed: number;
|
|
65
|
+
pending: number;
|
|
66
|
+
}>;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=outbox.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"outbox.d.ts","sourceRoot":"","sources":["../src/outbox.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,MAAM,MAAM,qBAAqB,GAC7B,SAAS,GACT,oBAAoB,GACpB,OAAO,CAAC;AAEZ,MAAM,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,YAAY,KAAK,OAAO,CAAC,qBAAqB,CAAC,CAAC;AAErF,MAAM,WAAW,uBAAuB;IACtC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM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@@
|
|
|
1
|
+
/**
|
|
2
|
+
* Outbox Dispatcher Subsystem for MO·WA OS Browser Host.
|
|
3
|
+
*
|
|
4
|
+
* Runs asynchronously outside the session lock to dispatch side effects.
|
|
5
|
+
* Supports:
|
|
6
|
+
* - At-least-once delivery with deterministic effect.id deduplication.
|
|
7
|
+
* - True exponential backoff with jitter and Dead-Letter Queue (`FAILED_DLQ`).
|
|
8
|
+
* - "Pre-fill & Confirm" UI gating pattern with `AWAIT_CONFIRMATION` status segregation.
|
|
9
|
+
* - Grace period handling for unhandled effect kinds (prevents queue deadlock).
|
|
10
|
+
* - Operator recovery APIs: `requeueEffect`, `rejectEffect`, and `getNextDueAt`.
|
|
11
|
+
*/
|
|
12
|
+
export class OutboxDispatcher {
|
|
13
|
+
storage;
|
|
14
|
+
handlers = new Map();
|
|
15
|
+
maxAttempts;
|
|
16
|
+
batchSize;
|
|
17
|
+
baseBackoffMs;
|
|
18
|
+
maxBackoffMs;
|
|
19
|
+
unhandledGraceIntervalMs;
|
|
20
|
+
maxUnhandledGraceMs;
|
|
21
|
+
isRunning = false;
|
|
22
|
+
constructor(storage, options = {}) {
|
|
23
|
+
this.storage = storage;
|
|
24
|
+
this.maxAttempts = options.maxAttempts ?? 5;
|
|
25
|
+
this.batchSize = options.batchSize ?? 20;
|
|
26
|
+
this.baseBackoffMs = options.baseBackoffMs ?? 1000;
|
|
27
|
+
this.maxBackoffMs = options.maxBackoffMs ?? 30000;
|
|
28
|
+
this.unhandledGraceIntervalMs = options.unhandledGraceIntervalMs ?? 5000;
|
|
29
|
+
this.maxUnhandledGraceMs = options.maxUnhandledGraceMs ?? 60000;
|
|
30
|
+
}
|
|
31
|
+
registerHandler(kind, handler) {
|
|
32
|
+
this.handlers.set(kind, handler);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Calculates exponential backoff with ±10% jitter:
|
|
36
|
+
* delay = min(maxBackoff, base * 2^(attempts-1)) * (0.9 to 1.1)
|
|
37
|
+
*/
|
|
38
|
+
calculateBackoff(attempts) {
|
|
39
|
+
const exponent = Math.max(0, attempts - 1);
|
|
40
|
+
const base = this.baseBackoffMs * Math.pow(2, exponent);
|
|
41
|
+
const capped = Math.min(this.maxBackoffMs, base);
|
|
42
|
+
const jitter = 0.9 + Math.random() * 0.2; // 0.9 to 1.1
|
|
43
|
+
return Math.round(capped * jitter);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns earliest timestamp when a pending outbox record is due, or null if none pending.
|
|
47
|
+
* Host or extension can inspect this to schedule alarms (e.g. via chrome.alarms).
|
|
48
|
+
*/
|
|
49
|
+
async getNextDueAt() {
|
|
50
|
+
return await this.storage.getNextDueAt();
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Called when a human operator reviews and confirms an action.
|
|
54
|
+
* Verifies the effect is in `AWAIT_CONFIRMATION` before marking `DISPATCHED`.
|
|
55
|
+
*/
|
|
56
|
+
async confirmEffect(outboxId) {
|
|
57
|
+
const record = await this.storage.getOutbox(outboxId);
|
|
58
|
+
if (!record) {
|
|
59
|
+
throw new Error(`Outbox effect '${outboxId}' not found.`);
|
|
60
|
+
}
|
|
61
|
+
if (record.status !== "AWAIT_CONFIRMATION") {
|
|
62
|
+
throw new Error(`Cannot confirm effect '${outboxId}': status is '${record.status}', expected 'AWAIT_CONFIRMATION'.`);
|
|
63
|
+
}
|
|
64
|
+
await this.storage.updateOutboxStatus(outboxId, "DISPATCHED", {
|
|
65
|
+
lastAttemptAt: Date.now(),
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Called when an operator explicitly rejects an action awaiting confirmation.
|
|
70
|
+
*/
|
|
71
|
+
async rejectEffect(outboxId, reason) {
|
|
72
|
+
const record = await this.storage.getOutbox(outboxId);
|
|
73
|
+
if (!record) {
|
|
74
|
+
throw new Error(`Outbox effect '${outboxId}' not found.`);
|
|
75
|
+
}
|
|
76
|
+
if (record.status !== "AWAIT_CONFIRMATION") {
|
|
77
|
+
throw new Error(`Cannot reject effect '${outboxId}': status is '${record.status}', expected 'AWAIT_CONFIRMATION'.`);
|
|
78
|
+
}
|
|
79
|
+
await this.storage.updateOutboxStatus(outboxId, "REJECTED", {
|
|
80
|
+
lastAttemptAt: Date.now(),
|
|
81
|
+
lastError: reason,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Operator recovery API: requeues an effect from `FAILED_DLQ` back to `PENDING`.
|
|
86
|
+
*/
|
|
87
|
+
async requeueEffect(outboxId) {
|
|
88
|
+
const record = await this.storage.getOutbox(outboxId);
|
|
89
|
+
if (!record) {
|
|
90
|
+
throw new Error(`Outbox effect '${outboxId}' not found.`);
|
|
91
|
+
}
|
|
92
|
+
if (record.status !== "FAILED_DLQ") {
|
|
93
|
+
throw new Error(`Cannot requeue effect '${outboxId}': status is '${record.status}', expected 'FAILED_DLQ'.`);
|
|
94
|
+
}
|
|
95
|
+
const now = Date.now();
|
|
96
|
+
await this.storage.updateOutboxStatus(outboxId, "PENDING", {
|
|
97
|
+
attempts: 0,
|
|
98
|
+
nextAttemptAt: now,
|
|
99
|
+
lastError: undefined,
|
|
100
|
+
unhandledSince: undefined,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Drain pending outbox records asynchronously outside the session lock.
|
|
105
|
+
*/
|
|
106
|
+
async dispatchPending() {
|
|
107
|
+
if (this.isRunning) {
|
|
108
|
+
return { processed: 0, failed: 0, pending: 0 };
|
|
109
|
+
}
|
|
110
|
+
this.isRunning = true;
|
|
111
|
+
let processed = 0;
|
|
112
|
+
let failed = 0;
|
|
113
|
+
let pending = 0;
|
|
114
|
+
try {
|
|
115
|
+
const now = Date.now();
|
|
116
|
+
const records = await this.storage.getPendingOutbox(this.batchSize, now);
|
|
117
|
+
for (const record of records) {
|
|
118
|
+
const handler = this.handlers.get(record.effect.kind);
|
|
119
|
+
if (!handler) {
|
|
120
|
+
// Effect kind has no handler registered.
|
|
121
|
+
// In MV3 service workers, handlers might register after initial wake-up.
|
|
122
|
+
// Push nextAttemptAt forward so it leaves the due window without blocking other records.
|
|
123
|
+
const unhandledSince = record.unhandledSince ?? now;
|
|
124
|
+
if (now - unhandledSince >= this.maxUnhandledGraceMs) {
|
|
125
|
+
await this.storage.updateOutboxStatus(record.outboxId, "FAILED_DLQ", {
|
|
126
|
+
lastAttemptAt: now,
|
|
127
|
+
lastError: `UNHANDLED_EFFECT_KIND: No handler registered for '${record.effect.kind}' within grace period`,
|
|
128
|
+
});
|
|
129
|
+
failed++;
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
await this.storage.updateOutboxStatus(record.outboxId, "PENDING", {
|
|
133
|
+
nextAttemptAt: now + this.unhandledGraceIntervalMs,
|
|
134
|
+
unhandledSince,
|
|
135
|
+
});
|
|
136
|
+
pending++;
|
|
137
|
+
}
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
// Attempt dispatch
|
|
142
|
+
const result = await handler(record);
|
|
143
|
+
if (result === "SUCCESS") {
|
|
144
|
+
// Success: mark DISPATCHED without incrementing retry attempts
|
|
145
|
+
await this.storage.updateOutboxStatus(record.outboxId, "DISPATCHED", {
|
|
146
|
+
lastAttemptAt: Date.now(),
|
|
147
|
+
});
|
|
148
|
+
processed++;
|
|
149
|
+
}
|
|
150
|
+
else if (result === "AWAIT_CONFIRMATION") {
|
|
151
|
+
// Awaiting operator approval: move to AWAIT_CONFIRMATION, do NOT increment attempts!
|
|
152
|
+
await this.storage.updateOutboxStatus(record.outboxId, "AWAIT_CONFIRMATION", {
|
|
153
|
+
lastAttemptAt: Date.now(),
|
|
154
|
+
});
|
|
155
|
+
pending++;
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
// Handler explicitly requested RETRY
|
|
159
|
+
const nextAttempts = record.attempts + 1;
|
|
160
|
+
if (nextAttempts >= this.maxAttempts) {
|
|
161
|
+
await this.storage.updateOutboxStatus(record.outboxId, "FAILED_DLQ", {
|
|
162
|
+
attempts: nextAttempts,
|
|
163
|
+
lastAttemptAt: Date.now(),
|
|
164
|
+
lastError: `Exceeded max retry attempts (${this.maxAttempts})`,
|
|
165
|
+
});
|
|
166
|
+
failed++;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
const delay = this.calculateBackoff(nextAttempts);
|
|
170
|
+
await this.storage.updateOutboxStatus(record.outboxId, "PENDING", {
|
|
171
|
+
attempts: nextAttempts,
|
|
172
|
+
nextAttemptAt: Date.now() + delay,
|
|
173
|
+
lastAttemptAt: Date.now(),
|
|
174
|
+
lastError: "Handler requested retry",
|
|
175
|
+
});
|
|
176
|
+
pending++;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
catch (err) {
|
|
181
|
+
// Handler threw exception during execution
|
|
182
|
+
const errMsg = err instanceof Error ? err.message : String(err);
|
|
183
|
+
const nextAttempts = record.attempts + 1;
|
|
184
|
+
const isDlq = nextAttempts >= this.maxAttempts;
|
|
185
|
+
if (isDlq) {
|
|
186
|
+
await this.storage.updateOutboxStatus(record.outboxId, "FAILED_DLQ", {
|
|
187
|
+
attempts: nextAttempts,
|
|
188
|
+
lastAttemptAt: Date.now(),
|
|
189
|
+
lastError: errMsg,
|
|
190
|
+
});
|
|
191
|
+
failed++;
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
const delay = this.calculateBackoff(nextAttempts);
|
|
195
|
+
await this.storage.updateOutboxStatus(record.outboxId, "PENDING", {
|
|
196
|
+
attempts: nextAttempts,
|
|
197
|
+
nextAttemptAt: Date.now() + delay,
|
|
198
|
+
lastAttemptAt: Date.now(),
|
|
199
|
+
lastError: errMsg,
|
|
200
|
+
});
|
|
201
|
+
pending++;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
finally {
|
|
207
|
+
this.isRunning = false;
|
|
208
|
+
}
|
|
209
|
+
return { processed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
|
2
|
+
* Storage Subsystem for MO·WA OS Browser Host.
|
|
3
|
+
*
|
|
4
|
+
* Implements the 6-store IndexedDB layout:
|
|
5
|
+
* 1. `sessions` - WorkflowSession snapshot (keyed by sessionId)
|
|
6
|
+
* 2. `flows` - FlowDefinition JSON (keyed by flowHash, written once on install)
|
|
7
|
+
* 3. `idempotency` - Idempotency records (keyed by dedupKey, verified by intentHash)
|
|
8
|
+
* 4. `audit_log` - TransitionAudit records (keyed by transitionId, chained via prevResultHash)
|
|
9
|
+
* 5. `outbox` - SideEffect outbox records (keyed by outboxId, compound index: [status, nextAttemptAt])
|
|
10
|
+
* 6. `steps` - Step inputs + commit (keyed by transitionId, index: [sessionId, version]),
|
|
11
|
+
* kept so every decision can be replayed (docs/host-contract.md, section 6)
|
|
12
|
+
*
|
|
13
|
+
* Provides:
|
|
14
|
+
* - `IndexedDbStorage`: Production-grade browser implementation.
|
|
15
|
+
* - `MemoryStorage`: Deterministic in-memory implementation for testing/Node.
|
|
16
|
+
*/
|
|
17
|
+
import type { Commit, CommitRecord, FlowDefinition, IdempotencyRecord, OutboxRecord, OutboxStatus, StepRecord, TransitionAudit, WorkflowSession } from "./types.js";
|
|
18
|
+
export interface MowaStorage {
|
|
19
|
+
getFlow(flowHash: string): Promise<FlowDefinition | null>;
|
|
20
|
+
saveFlow(flowHash: string, flow: FlowDefinition): Promise<void>;
|
|
21
|
+
getSession(sessionId: string): Promise<WorkflowSession | null>;
|
|
22
|
+
getIdempotency(dedupKey: string): Promise<IdempotencyRecord | null>;
|
|
23
|
+
getAudit(transitionId: string): Promise<TransitionAudit | null>;
|
|
24
|
+
getOutbox(outboxId: string): Promise<OutboxRecord | null>;
|
|
25
|
+
getPendingOutbox(limit?: number, now?: number): Promise<OutboxRecord[]>;
|
|
26
|
+
getOutboxByStatus(status: OutboxStatus, limit?: number): Promise<OutboxRecord[]>;
|
|
27
|
+
getNextDueAt(): Promise<number | null>;
|
|
28
|
+
updateOutboxStatus(outboxId: string, status: OutboxStatus, data?: {
|
|
29
|
+
attempts?: number;
|
|
30
|
+
nextAttemptAt?: number;
|
|
31
|
+
lastAttemptAt?: number;
|
|
32
|
+
unhandledSince?: number;
|
|
33
|
+
lastError?: string;
|
|
34
|
+
}): Promise<void>;
|
|
35
|
+
/** Every committed step of a session, in version order (0, 1, 2, …). */
|
|
36
|
+
getSteps(sessionId: string): Promise<StepRecord[]>;
|
|
37
|
+
/**
|
|
38
|
+
* Writes the session, audit record, duplicate record, outbox records and step record
|
|
39
|
+
* in one atomic transaction, only if the stored session version equals
|
|
40
|
+
* `expectedVersion` (`null` = the session must not exist yet).
|
|
41
|
+
*/
|
|
42
|
+
atomicCommit(sessionId: string, expectedVersion: number | null, commit: Commit, record: CommitRecord): Promise<void>;
|
|
43
|
+
}
|
|
44
|
+
export declare class CasConflictError extends Error {
|
|
45
|
+
constructor(sessionId: string, expected: number | null, actual: number | null);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* In-memory storage implementation for tests, simulation, and Node environments.
|
|
49
|
+
*/
|
|
50
|
+
export declare class MemoryStorage implements MowaStorage {
|
|
51
|
+
private readonly sessions;
|
|
52
|
+
private readonly flows;
|
|
53
|
+
private readonly idempotency;
|
|
54
|
+
private readonly auditLog;
|
|
55
|
+
private readonly outbox;
|
|
56
|
+
private readonly steps;
|
|
57
|
+
getSteps(sessionId: string): Promise<StepRecord[]>;
|
|
58
|
+
getFlow(flowHash: string): Promise<FlowDefinition | null>;
|
|
59
|
+
saveFlow(flowHash: string, flow: FlowDefinition): Promise<void>;
|
|
60
|
+
getSession(sessionId: string): Promise<WorkflowSession | null>;
|
|
61
|
+
getIdempotency(dedupKey: string): Promise<IdempotencyRecord | null>;
|
|
62
|
+
getAudit(transitionId: string): Promise<TransitionAudit | null>;
|
|
63
|
+
getOutbox(outboxId: string): Promise<OutboxRecord | null>;
|
|
64
|
+
getPendingOutbox(limit?: number, now?: number): Promise<OutboxRecord[]>;
|
|
65
|
+
getOutboxByStatus(status: OutboxStatus, limit?: number): Promise<OutboxRecord[]>;
|
|
66
|
+
getNextDueAt(): Promise<number | null>;
|
|
67
|
+
updateOutboxStatus(outboxId: string, status: OutboxStatus, data?: {
|
|
68
|
+
attempts?: number;
|
|
69
|
+
nextAttemptAt?: number;
|
|
70
|
+
lastAttemptAt?: number;
|
|
71
|
+
unhandledSince?: number;
|
|
72
|
+
lastError?: string;
|
|
73
|
+
}): Promise<void>;
|
|
74
|
+
atomicCommit(sessionId: string, expectedVersion: number | null, commit: Commit, record: CommitRecord): Promise<void>;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Production IndexedDB storage engine for Chrome MV3 extension.
|
|
78
|
+
*/
|
|
79
|
+
export declare class IndexedDbStorage implements MowaStorage {
|
|
80
|
+
private readonly dbName;
|
|
81
|
+
private readonly version;
|
|
82
|
+
private dbPromise;
|
|
83
|
+
constructor(dbName?: string, version?: number);
|
|
84
|
+
private getDb;
|
|
85
|
+
getFlow(flowHash: string): Promise<FlowDefinition | null>;
|
|
86
|
+
saveFlow(flowHash: string, flow: FlowDefinition): Promise<void>;
|
|
87
|
+
getSteps(sessionId: string): Promise<StepRecord[]>;
|
|
88
|
+
getSession(sessionId: string): Promise<WorkflowSession | null>;
|
|
89
|
+
getIdempotency(dedupKey: string): Promise<IdempotencyRecord | null>;
|
|
90
|
+
getAudit(transitionId: string): Promise<TransitionAudit | null>;
|
|
91
|
+
getOutbox(outboxId: string): Promise<OutboxRecord | null>;
|
|
92
|
+
getPendingOutbox(limit?: number, now?: number): Promise<OutboxRecord[]>;
|
|
93
|
+
getOutboxByStatus(status: OutboxStatus, limit?: number): Promise<OutboxRecord[]>;
|
|
94
|
+
getNextDueAt(): Promise<number | null>;
|
|
95
|
+
updateOutboxStatus(outboxId: string, status: OutboxStatus, data?: {
|
|
96
|
+
attempts?: number;
|
|
97
|
+
nextAttemptAt?: number;
|
|
98
|
+
lastAttemptAt?: number;
|
|
99
|
+
unhandledSince?: number;
|
|
100
|
+
lastError?: string;
|
|
101
|
+
}): Promise<void>;
|
|
102
|
+
atomicCommit(sessionId: string, expectedVersion: number | null, commit: Commit, record: CommitRecord): Promise<void>;
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=storage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EACV,MAAM,EACN,YAAY,EACZ,cAAc,EACd,iBAAiB,EACjB,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,eAAe,EACf,eAAe,EAChB,MAAM,YAAY,CAAC;AAEpB,MAAM,WAAW,WAAW;IAC1B,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,GAAG,IAAI,CAAC,CAAC;IAC1D,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEhE,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAAC;IAC/D,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,GAAG,IAAI,CAAC,CAAC;IACpE,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,CAAC;IAChE,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAAC;IAE1D,gBAAgB,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC;IACxE,iBAAiB,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC;IACjF,YAAY,IAA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