toiljs 0.0.60 → 0.0.62
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/.github/workflows/ci.yml +31 -0
- package/CHANGELOG.md +17 -0
- package/build/cli/.tsbuildinfo +1 -1
- package/build/cli/index.js +2 -2
- package/build/client/.tsbuildinfo +1 -1
- package/build/client/index.d.ts +1 -1
- package/build/client/index.js +1 -1
- package/build/client/routing/mount.js +11 -26
- package/build/client/ssr/markers.d.ts +1 -0
- package/build/client/ssr/markers.js +9 -2
- package/build/compiler/.tsbuildinfo +1 -1
- package/build/compiler/config.d.ts +21 -0
- package/build/compiler/config.js +35 -0
- package/build/compiler/docs.d.ts +2 -1
- package/build/compiler/docs.js +33 -304
- package/build/compiler/index.d.ts +13 -0
- package/build/compiler/index.js +113 -21
- package/build/compiler/template-build.d.ts +23 -3
- package/build/compiler/template-build.js +120 -30
- package/build/compiler/toil-docs.generated.d.ts +1 -0
- package/build/compiler/toil-docs.generated.js +20 -0
- package/build/devserver/.tsbuildinfo +1 -1
- package/build/devserver/daemon/catalog.d.ts +26 -0
- package/build/devserver/daemon/catalog.js +48 -0
- package/build/devserver/daemon/cron.d.ts +4 -0
- package/build/devserver/daemon/cron.js +50 -0
- package/build/devserver/daemon/host.d.ts +37 -0
- package/build/devserver/daemon/host.js +94 -0
- package/build/devserver/daemon/index.d.ts +34 -0
- package/build/devserver/daemon/index.js +241 -0
- package/build/devserver/db/catalog.d.ts +2 -1
- package/build/devserver/db/catalog.js +44 -44
- package/build/devserver/db/database.d.ts +27 -11
- package/build/devserver/db/database.js +539 -169
- package/build/devserver/db/index.d.ts +1 -1
- package/build/devserver/db/index.js +1 -1
- package/build/devserver/db/routeKinds.d.ts +8 -0
- package/build/devserver/db/routeKinds.js +139 -0
- package/build/devserver/db/types.d.ts +64 -1
- package/build/devserver/db/types.js +33 -1
- package/build/devserver/index.d.ts +10 -0
- package/build/devserver/index.js +7 -0
- package/build/devserver/mstore/store.d.ts +18 -0
- package/build/devserver/mstore/store.js +82 -0
- package/build/devserver/runtime/host.d.ts +6 -0
- package/build/devserver/runtime/host.js +45 -1
- package/build/devserver/runtime/module.d.ts +1 -0
- package/build/devserver/runtime/module.js +27 -1
- package/build/devserver/server.d.ts +6 -0
- package/build/devserver/server.js +59 -0
- package/build/devserver/ssr.d.ts +25 -0
- package/build/devserver/ssr.js +114 -0
- package/build/devserver/wasm/sections.d.ts +2 -0
- package/build/devserver/wasm/sections.js +42 -0
- package/build/devserver/wasm/surface.d.ts +18 -0
- package/build/devserver/wasm/surface.js +41 -0
- package/docs/README.md +4 -4
- package/docs/auth-todo.md +6 -6
- package/docs/caching.md +5 -5
- package/docs/cli.md +15 -0
- package/docs/client.md +40 -0
- package/docs/crypto.md +4 -4
- package/docs/data.md +6 -6
- package/docs/email.md +28 -28
- package/docs/environment.md +10 -10
- package/docs/index.md +26 -0
- package/docs/ratelimit.md +10 -10
- package/docs/routing.md +2 -2
- package/docs/server.md +61 -0
- package/docs/ssr.md +561 -113
- package/docs/styling.md +22 -0
- package/docs/time.md +1 -1
- package/eslint.config.js +10 -1
- package/examples/basic/client/components/Header.tsx +3 -0
- package/examples/basic/client/routes/features/actions.tsx +0 -2
- package/examples/basic/client/routes/hello.tsx +89 -19
- package/examples/basic/client/styles/main.css +48 -0
- package/examples/basic/server/SsrHelloRender.ts +97 -0
- package/examples/basic/server/main.ts +5 -0
- package/examples/basic/server/streams/Echo.ts +49 -0
- package/package.json +12 -10
- package/scripts/gen-toil-docs.mjs +96 -0
- package/src/cli/create.ts +2 -2
- package/src/client/index.ts +1 -1
- package/src/client/routing/mount.tsx +19 -31
- package/src/client/ssr/markers.tsx +33 -4
- package/src/compiler/config.ts +88 -2
- package/src/compiler/docs.ts +47 -308
- package/src/compiler/index.ts +236 -32
- package/src/compiler/ssr-codegen.ts +1 -1
- package/src/compiler/template-build.ts +271 -53
- package/src/compiler/toil-docs.generated.ts +26 -0
- package/src/devserver/daemon/catalog.ts +120 -0
- package/src/devserver/daemon/cron.ts +87 -0
- package/src/devserver/daemon/host.ts +224 -0
- package/src/devserver/daemon/index.ts +349 -0
- package/src/devserver/db/catalog.ts +61 -53
- package/src/devserver/db/database.ts +613 -149
- package/src/devserver/db/index.ts +1 -1
- package/src/devserver/db/routeKinds.ts +147 -0
- package/src/devserver/db/types.ts +65 -2
- package/src/devserver/index.ts +12 -0
- package/src/devserver/mstore/store.ts +121 -0
- package/src/devserver/runtime/host.ts +92 -1
- package/src/devserver/runtime/module.ts +35 -1
- package/src/devserver/server.ts +101 -0
- package/src/devserver/ssr.ts +166 -0
- package/src/devserver/wasm/sections.ts +59 -0
- package/src/devserver/wasm/surface.ts +88 -0
- package/test/daemon-build.test.ts +198 -0
- package/test/daemon-catalog.test.ts +265 -0
- package/test/daemon-emulation.test.ts +216 -0
- package/test/devserver-database.test.ts +396 -5
- package/test/email-preview.test.ts +6 -1
- package/test/fixtures/daemon-app.ts +56 -0
- package/test/global-setup.ts +17 -0
- package/test/ssr-hydration.test.tsx +107 -0
- package/test/ssr-render.test.ts +96 -27
- package/test/ssr-template.test.tsx +47 -2
- package/vitest.config.ts +3 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-import surface for the dev daemon (cold) box, mirroring the production
|
|
3
|
+
* edge's `daemon.*` + `mstore.*` imports. DAEMON path only (streams are Phase 4).
|
|
4
|
+
*
|
|
5
|
+
* Per RECONCILIATION:
|
|
6
|
+
* - Part 4 `daemon.*`: is_leader / current_epoch / yield / sleep_ms / task_count
|
|
7
|
+
* / next_fire_ms / http_call / remote_call. In a single dev process the leader
|
|
8
|
+
* stub is always true and the lease never expires (section 5.2). Fenced DB
|
|
9
|
+
* writes are TRANSPARENT (no `daemon.db_write_fenced` import).
|
|
10
|
+
* - Part 4 `mstore.*`: handleless, ttl_secs, inline drain (section 7.4).
|
|
11
|
+
* - Part 3 error bridge: a u16 subsystem code `c` is returned as `-(0x10000 + c)`;
|
|
12
|
+
* the buffer sentinels `-1` (TOO_SMALL) / `-2` (ABSENT) are unchanged.
|
|
13
|
+
*
|
|
14
|
+
* The cold box also imports the request-surface `env.*` MINUS the response/stream
|
|
15
|
+
* functions it must not have (no `set_status`/`set_header`/`respond_file`/
|
|
16
|
+
* `client_ip`/`ratelimit_check`); it keeps `@data`/crypto/env/`Date.now`/email so a
|
|
17
|
+
* daemon can read+write the DB and send mail. The two allow-lists live in
|
|
18
|
+
* `runtime/module.ts`.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import { buildDatabaseImports, type DbDevState, freshDbState } from '../db/index.js';
|
|
22
|
+
import { type DevMemoryStore } from '../mstore/store.js';
|
|
23
|
+
import { buildCryptoImports, type CryptoState, freshCryptoState } from '../runtime/crypto.js';
|
|
24
|
+
import {
|
|
25
|
+
buildEnvImports,
|
|
26
|
+
type MemoryRef,
|
|
27
|
+
readBytes,
|
|
28
|
+
writeBytesOut,
|
|
29
|
+
} from '../runtime/host.js';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Resolved daemon (L4) config the dev scheduler reads. Structurally identical to
|
|
33
|
+
* the compiler's `ResolvedDaemonConfig`; declared here so the devserver package has
|
|
34
|
+
* no source dependency on the compiler package (its tsconfig is isolated to
|
|
35
|
+
* `src/devserver`). The compiler's resolved config is passed in verbatim.
|
|
36
|
+
*/
|
|
37
|
+
export interface ResolvedDaemonConfig {
|
|
38
|
+
readonly region: string | null;
|
|
39
|
+
readonly standbyRegion: string | null;
|
|
40
|
+
readonly defaultIntervalMs: number;
|
|
41
|
+
readonly tickBudgetMs: number;
|
|
42
|
+
readonly gasTick: number;
|
|
43
|
+
readonly maxTasks: number;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** RECONCILIATION Part 3 u16 error registry (the subset the daemon/mstore use). */
|
|
47
|
+
export const enum AbiError {
|
|
48
|
+
MstoreNotFound = 0x0301,
|
|
49
|
+
MstoreNotANumber = 0x0306,
|
|
50
|
+
MstoreScanBusy = 0x0307,
|
|
51
|
+
MstoreConflict = 0x0304,
|
|
52
|
+
DaemonScheduleRejected = 0x0403,
|
|
53
|
+
DaemonCallFailed = 0x0405,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Encode a u16 subsystem error per the Part 3 negative-return bridge:
|
|
57
|
+
* `code = (-v) - 0x10000`, so `v = -(0x10000 + code)`. */
|
|
58
|
+
export function encodeAbiError(code: number): number {
|
|
59
|
+
return -(0x10000 + code);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The minimal scheduler/leader view the `daemon.*` imports read. Implemented by
|
|
63
|
+
* the resident `DaemonHost`. */
|
|
64
|
+
export interface DaemonRuntime {
|
|
65
|
+
isLeader(): boolean;
|
|
66
|
+
/** Monotonic fencing token; bumps on each (re)start. */
|
|
67
|
+
epoch(): bigint;
|
|
68
|
+
/** Number of registered `@scheduled` tasks. */
|
|
69
|
+
taskCount(): number;
|
|
70
|
+
/** Next computed fire time (epoch ms) for `taskId`, or `null` if unknown. */
|
|
71
|
+
nextFireMs(taskId: number): number | null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A host-import map: each value is a function over i32 args (i64 params are typed
|
|
75
|
+
* `number | bigint` individually, matching the existing db/crypto import maps). */
|
|
76
|
+
type HostFnMap = Record<string, (...args: number[]) => number | bigint>;
|
|
77
|
+
|
|
78
|
+
/** Build the `mstore.*` host imports, backed by `store`. Handleless; keys are read
|
|
79
|
+
* from guest memory and auto-scoped to the single dev host/region. */
|
|
80
|
+
export function buildMstoreImports(ref: MemoryRef, store: DevMemoryStore): HostFnMap {
|
|
81
|
+
const key = (p: number, l: number): string => readBytes(ref, p, l).toString('utf8');
|
|
82
|
+
return {
|
|
83
|
+
'mstore.get': (keyPtr: number, keyLen: number, outPtr: number, outCap: number): number => {
|
|
84
|
+
const v = store.get(key(keyPtr, keyLen));
|
|
85
|
+
if (v === null) return encodeAbiError(AbiError.MstoreNotFound);
|
|
86
|
+
return writeBytesOut(ref, v, outPtr, outCap);
|
|
87
|
+
},
|
|
88
|
+
'mstore.set': (
|
|
89
|
+
keyPtr: number,
|
|
90
|
+
keyLen: number,
|
|
91
|
+
valPtr: number,
|
|
92
|
+
valLen: number,
|
|
93
|
+
ttlSecs: number,
|
|
94
|
+
): number => {
|
|
95
|
+
store.set(key(keyPtr, keyLen), readBytes(ref, valPtr, valLen), ttlSecs);
|
|
96
|
+
return 0;
|
|
97
|
+
},
|
|
98
|
+
'mstore.delete': (keyPtr: number, keyLen: number): number =>
|
|
99
|
+
store.delete(key(keyPtr, keyLen)) ? 0 : encodeAbiError(AbiError.MstoreNotFound),
|
|
100
|
+
'mstore.incr': (
|
|
101
|
+
keyPtr: number,
|
|
102
|
+
keyLen: number,
|
|
103
|
+
delta: number | bigint,
|
|
104
|
+
ttlSecs: number,
|
|
105
|
+
): bigint => {
|
|
106
|
+
const d = typeof delta === 'bigint' ? delta : BigInt(delta);
|
|
107
|
+
const next = store.incr(key(keyPtr, keyLen), d, ttlSecs);
|
|
108
|
+
return next === null ? BigInt(encodeAbiError(AbiError.MstoreNotANumber)) : next;
|
|
109
|
+
},
|
|
110
|
+
'mstore.cas': (
|
|
111
|
+
keyPtr: number,
|
|
112
|
+
keyLen: number,
|
|
113
|
+
expectPtr: number,
|
|
114
|
+
expectLen: number,
|
|
115
|
+
newPtr: number,
|
|
116
|
+
newLen: number,
|
|
117
|
+
ttlSecs: number,
|
|
118
|
+
): number => {
|
|
119
|
+
const expect = expectLen === 0 ? null : readBytes(ref, expectPtr, expectLen);
|
|
120
|
+
const ok = store.cas(
|
|
121
|
+
key(keyPtr, keyLen),
|
|
122
|
+
expect,
|
|
123
|
+
readBytes(ref, newPtr, newLen),
|
|
124
|
+
ttlSecs,
|
|
125
|
+
);
|
|
126
|
+
return ok ? 0 : encodeAbiError(AbiError.MstoreConflict);
|
|
127
|
+
},
|
|
128
|
+
'mstore.expire': (keyPtr: number, keyLen: number, ttlSecs: number): number =>
|
|
129
|
+
store.expire(key(keyPtr, keyLen), ttlSecs)
|
|
130
|
+
? 0
|
|
131
|
+
: encodeAbiError(AbiError.MstoreNotFound),
|
|
132
|
+
'mstore.scan': (
|
|
133
|
+
prefixPtr: number,
|
|
134
|
+
prefixLen: number,
|
|
135
|
+
cursor: number | bigint,
|
|
136
|
+
outPtr: number,
|
|
137
|
+
outCap: number,
|
|
138
|
+
): bigint => {
|
|
139
|
+
const cur = typeof cursor === 'bigint' ? cursor : BigInt(cursor);
|
|
140
|
+
const res = store.scan(key(prefixPtr, prefixLen), cur);
|
|
141
|
+
if (res === null) return BigInt(encodeAbiError(AbiError.MstoreScanBusy));
|
|
142
|
+
// Frame: u32 count, then per key (u32 len + bytes).
|
|
143
|
+
let total = 4;
|
|
144
|
+
for (const k of res.keys) total += 4 + Buffer.byteLength(k, 'utf8');
|
|
145
|
+
const frame = Buffer.alloc(total);
|
|
146
|
+
let o = frame.writeUInt32LE(res.keys.length, 0);
|
|
147
|
+
for (const k of res.keys) {
|
|
148
|
+
const kb = Buffer.from(k, 'utf8');
|
|
149
|
+
o = frame.writeUInt32LE(kb.length, o);
|
|
150
|
+
kb.copy(frame, o);
|
|
151
|
+
o += kb.length;
|
|
152
|
+
}
|
|
153
|
+
const len = writeBytesOut(ref, frame, outPtr, outCap);
|
|
154
|
+
if (len < 0) return BigInt(len); // TOO_SMALL sentinel
|
|
155
|
+
return (res.next << 32n) | BigInt(len);
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Build the `daemon.*` host imports, closing over the resident `DaemonRuntime`.
|
|
161
|
+
* These imports do not read guest memory (they answer from the resident scheduler
|
|
162
|
+
* state), so they take no `MemoryRef`. */
|
|
163
|
+
export function buildDaemonNamespace(rt: DaemonRuntime): HostFnMap {
|
|
164
|
+
return {
|
|
165
|
+
'daemon.is_leader': (): number => (rt.isLeader() ? 1 : 0),
|
|
166
|
+
'daemon.current_epoch': (): bigint => rt.epoch(),
|
|
167
|
+
// The dev lease never expires, so yield/sleep never report LEASE_LOST.
|
|
168
|
+
'daemon.yield': (): number => 0,
|
|
169
|
+
'daemon.sleep_ms': (_ms: number | bigint): number => 0,
|
|
170
|
+
'daemon.task_count': (): number => rt.taskCount(),
|
|
171
|
+
'daemon.next_fire_ms': (taskId: number): bigint => {
|
|
172
|
+
const at = rt.nextFireMs(taskId);
|
|
173
|
+
return at === null ? BigInt(encodeAbiError(AbiError.DaemonScheduleRejected)) : BigInt(at);
|
|
174
|
+
},
|
|
175
|
+
// Outbound call stubs: dev returns a "call failed" sentinel rather than
|
|
176
|
+
// performing real network I/O from a synchronous wasm import (section 5.4).
|
|
177
|
+
'daemon.http_call': (
|
|
178
|
+
_reqPtr: number,
|
|
179
|
+
_reqLen: number,
|
|
180
|
+
_outPtr: number,
|
|
181
|
+
_outCap: number,
|
|
182
|
+
): bigint => BigInt(encodeAbiError(AbiError.DaemonCallFailed)),
|
|
183
|
+
'daemon.remote_call': (
|
|
184
|
+
_svcId: number,
|
|
185
|
+
_reqPtr: number,
|
|
186
|
+
_reqLen: number,
|
|
187
|
+
_outPtr: number,
|
|
188
|
+
_outCap: number,
|
|
189
|
+
): bigint => BigInt(encodeAbiError(AbiError.DaemonCallFailed)),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Per-cold-box host state (DB + crypto scratch), analogous to `DispatchState`. */
|
|
194
|
+
export interface DaemonState {
|
|
195
|
+
crypto: CryptoState;
|
|
196
|
+
db: DbDevState;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function freshDaemonState(): DaemonState {
|
|
200
|
+
return { crypto: freshCryptoState(), db: freshDbState() };
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* The full `env` import object for the cold daemon box: the request-surface env
|
|
205
|
+
* MINUS the response/stream functions (built by `buildEnvImports`), PLUS the
|
|
206
|
+
* `daemon.*` and `mstore.*` namespaces. The cold box has no `handle` entry and no
|
|
207
|
+
* response surface.
|
|
208
|
+
*/
|
|
209
|
+
export function buildDaemonImports(
|
|
210
|
+
ref: MemoryRef,
|
|
211
|
+
state: DaemonState,
|
|
212
|
+
rt: DaemonRuntime,
|
|
213
|
+
store: DevMemoryStore,
|
|
214
|
+
): WebAssembly.Imports {
|
|
215
|
+
return {
|
|
216
|
+
env: {
|
|
217
|
+
...buildEnvImports(ref, state),
|
|
218
|
+
...buildCryptoImports(ref, state.crypto),
|
|
219
|
+
...buildDatabaseImports(ref, state.db),
|
|
220
|
+
...buildDaemonNamespace(rt),
|
|
221
|
+
...buildMstoreImports(ref, store),
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
}
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dev DAEMON (L4) emulation. Loads `release-cold.wasm` ONCE into a single resident
|
|
3
|
+
* instance, calls the guest `daemon_start()` export (RECONCILIATION Part 2; runs
|
|
4
|
+
* once, `0` ok), registers the `@scheduled` tasks from `toildaemon.catalog`, and
|
|
5
|
+
* drives them: interval tasks via `setInterval`, cron tasks via a one-shot
|
|
6
|
+
* `setTimeout` armed at the next minute whose precomputed bitmasks all pass (F6;
|
|
7
|
+
* never a runtime cron-string parse).
|
|
8
|
+
*
|
|
9
|
+
* Single-process at-most-once: there is exactly one dev process, so the leader
|
|
10
|
+
* stub is always true and the lease never expires. The epoch is a fencing-token
|
|
11
|
+
* stub that bumps on each (re)start, so guest code that compares epochs behaves
|
|
12
|
+
* like the edge across a cold-artifact hot reload. A trapped tick does NOT tear
|
|
13
|
+
* down the long-lived daemon box (deliberate asymmetry with the stream box); the
|
|
14
|
+
* overlap guard (`ticking` set) prevents a slow tick from piling up
|
|
15
|
+
* (overlap_policy 0 = skip-if-running).
|
|
16
|
+
*
|
|
17
|
+
* DAEMON path only; stream + WebSocket dev emulation is Phase 4.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import fs from 'node:fs';
|
|
21
|
+
|
|
22
|
+
import pc from 'picocolors';
|
|
23
|
+
|
|
24
|
+
import { type MemoryRef } from '../runtime/host.js';
|
|
25
|
+
import { devMemoryStore } from '../mstore/store.js';
|
|
26
|
+
import { parseSurface } from '../wasm/surface.js';
|
|
27
|
+
import {
|
|
28
|
+
type CronMasks,
|
|
29
|
+
type DaemonCatalog,
|
|
30
|
+
parseDaemonCatalog,
|
|
31
|
+
type ScheduledTask,
|
|
32
|
+
} from './catalog.js';
|
|
33
|
+
import { cronMatches, cronNeverFires, nextCronFireMs } from './cron.js';
|
|
34
|
+
import {
|
|
35
|
+
buildDaemonImports,
|
|
36
|
+
type DaemonRuntime,
|
|
37
|
+
type DaemonState,
|
|
38
|
+
freshDaemonState,
|
|
39
|
+
type ResolvedDaemonConfig,
|
|
40
|
+
} from './host.js';
|
|
41
|
+
|
|
42
|
+
interface ColdExports {
|
|
43
|
+
readonly memory: WebAssembly.Memory;
|
|
44
|
+
readonly daemon_start: () => number;
|
|
45
|
+
readonly scheduled_tick: (taskId: number) => bigint;
|
|
46
|
+
/** OPTIONAL: the host calls it before `daemon_start` if exported (Part 2). */
|
|
47
|
+
readonly init?: () => number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** RECONCILIATION Part 3 decode of a negative packed-i64 return, for logging. */
|
|
51
|
+
function decodeAbiError(ret: bigint): string {
|
|
52
|
+
if (ret >= 0n) return 'ok';
|
|
53
|
+
if (ret === -1n) return 'STATUS_TOO_SMALL';
|
|
54
|
+
if (ret === -2n) return 'STATUS_ABSENT';
|
|
55
|
+
if (ret <= -0x10000n) return '0x' + ((-ret - 0x10000n) & 0xffffn).toString(16).padStart(4, '0');
|
|
56
|
+
if (ret <= -1000n) return 'DB(TDL ' + String(-ret - 1000n) + ')';
|
|
57
|
+
return String(ret);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Whether the daemon emulator should run for this dev process and artifact. Per
|
|
62
|
+
* doc 08 section 5.1: `nodeMode` is `daemon` or `all`, the cold artifact's
|
|
63
|
+
* `toil.surface` declares a daemon surface, AND `parseDaemonCatalog` returns
|
|
64
|
+
* non-null with a daemon present.
|
|
65
|
+
*/
|
|
66
|
+
export function daemonEmulationEnabled(nodeMode: string): boolean {
|
|
67
|
+
return nodeMode === 'daemon' || nodeMode === 'all';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export class DaemonHost implements DaemonRuntime {
|
|
71
|
+
private module: WebAssembly.Module | null = null;
|
|
72
|
+
private instance: WebAssembly.Instance | null = null;
|
|
73
|
+
private exports: ColdExports | null = null;
|
|
74
|
+
private state: DaemonState = freshDaemonState();
|
|
75
|
+
private catalog: DaemonCatalog | null = null;
|
|
76
|
+
private loadedMtimeMs = -1;
|
|
77
|
+
private running = false;
|
|
78
|
+
/** Fencing-token stub (00 D3); bumps on each (re)start. */
|
|
79
|
+
private epochValue = 0n;
|
|
80
|
+
/** task_index -> active interval/timeout handle. */
|
|
81
|
+
private timers = new Map<number, NodeJS.Timeout>();
|
|
82
|
+
/** task_index -> next computed fire time (epoch ms), for daemon.next_fire_ms. */
|
|
83
|
+
private nextFire = new Map<number, number>();
|
|
84
|
+
/** task_index of ticks currently executing (overlap guard). */
|
|
85
|
+
private ticking = new Set<number>();
|
|
86
|
+
|
|
87
|
+
constructor(
|
|
88
|
+
private readonly coldWasmPath: string,
|
|
89
|
+
private readonly cfg: ResolvedDaemonConfig,
|
|
90
|
+
private readonly nodeMode: string,
|
|
91
|
+
private readonly log: (s: string) => void = (s) => process.stdout.write(s),
|
|
92
|
+
) {}
|
|
93
|
+
|
|
94
|
+
/** Whether the daemon box is currently resident and started. */
|
|
95
|
+
get active(): boolean {
|
|
96
|
+
return this.running;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
get tasks(): readonly ScheduledTask[] {
|
|
100
|
+
return this.catalog?.tasks ?? [];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// --- DaemonRuntime (the daemon.* host imports read these) ---
|
|
104
|
+
isLeader(): boolean {
|
|
105
|
+
return true; // single dev process is always the leader
|
|
106
|
+
}
|
|
107
|
+
epoch(): bigint {
|
|
108
|
+
return this.epochValue;
|
|
109
|
+
}
|
|
110
|
+
taskCount(): number {
|
|
111
|
+
return this.catalog?.tasks.length ?? 0;
|
|
112
|
+
}
|
|
113
|
+
nextFireMs(taskId: number): number | null {
|
|
114
|
+
return this.nextFire.get(taskId) ?? null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* (Re)load on mtime change, mirroring `WasmServerModule.refresh`. A cold-artifact
|
|
119
|
+
* change PAUSES + RESTARTS the daemon with a bumped epoch (section 9.1). Returns
|
|
120
|
+
* `true` when a (re)load happened.
|
|
121
|
+
*/
|
|
122
|
+
refresh(): boolean {
|
|
123
|
+
if (!daemonEmulationEnabled(this.nodeMode)) return false;
|
|
124
|
+
let mtimeMs: number;
|
|
125
|
+
try {
|
|
126
|
+
mtimeMs = fs.statSync(this.coldWasmPath).mtimeMs;
|
|
127
|
+
} catch {
|
|
128
|
+
// Cold artifact gone -> stop a running daemon, stay idle.
|
|
129
|
+
if (this.running) this.stop();
|
|
130
|
+
this.module = null;
|
|
131
|
+
this.loadedMtimeMs = -1;
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
if (this.module !== null && mtimeMs === this.loadedMtimeMs) return false;
|
|
135
|
+
|
|
136
|
+
const bytes = fs.readFileSync(this.coldWasmPath);
|
|
137
|
+
|
|
138
|
+
// The cold artifact must declare a daemon surface and a non-null catalog,
|
|
139
|
+
// else the emulator stays off (fail-closed; section 3.3 / 5.1).
|
|
140
|
+
const surface = parseSurface(bytes);
|
|
141
|
+
if (surface === 'invalid') {
|
|
142
|
+
this.log(pc.red(' ✗ cold artifact toil.surface is corrupt; daemon not started') + '\n');
|
|
143
|
+
if (this.running) this.stop();
|
|
144
|
+
this.loadedMtimeMs = mtimeMs;
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
if (surface !== 'absent' && surface.targetMode !== 'cold')
|
|
148
|
+
this.log(
|
|
149
|
+
pc.yellow(' ! ') +
|
|
150
|
+
pc.dim('cold slot holds a hot-mode artifact; ignoring daemon emulator') +
|
|
151
|
+
'\n',
|
|
152
|
+
);
|
|
153
|
+
const catalog = parseDaemonCatalog(bytes);
|
|
154
|
+
const declaresDaemon =
|
|
155
|
+
(surface === 'absent' ? false : surface.flags.daemon) || (catalog?.hasDaemon ?? false);
|
|
156
|
+
|
|
157
|
+
// A restart: stop the old box (timers + instance), bump epoch, start fresh.
|
|
158
|
+
if (this.running) this.stop();
|
|
159
|
+
|
|
160
|
+
if (!declaresDaemon || catalog === null || !catalog.hasDaemon) {
|
|
161
|
+
// No daemon in this artifact: load nothing, stay idle.
|
|
162
|
+
this.module = null;
|
|
163
|
+
this.catalog = null;
|
|
164
|
+
this.loadedMtimeMs = mtimeMs;
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
this.module = new WebAssembly.Module(bytes);
|
|
169
|
+
this.catalog = catalog;
|
|
170
|
+
this.loadedMtimeMs = mtimeMs;
|
|
171
|
+
this.epochValue += 1n; // fencing token bumps on each (re)start
|
|
172
|
+
this.start();
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Instantiate the cold box, run daemon_start once, register the tasks. */
|
|
177
|
+
private start(): void {
|
|
178
|
+
if (this.module === null || this.catalog === null) return;
|
|
179
|
+
const ref: MemoryRef = { memory: null };
|
|
180
|
+
this.state = freshDaemonState();
|
|
181
|
+
const imports = buildDaemonImports(ref, this.state, this, devMemoryStore);
|
|
182
|
+
|
|
183
|
+
// Fail-closed up front, with names, when the cold box imports anything outside its allowed
|
|
184
|
+
// surface (the request env subset + crypto + @data + daemon.* + mstore.*; NO response/
|
|
185
|
+
// stream functions). Mirrors `WasmServerModule.assertImportSurface` (section 7.1).
|
|
186
|
+
const provided = new Set(Object.keys((imports as { env: Record<string, unknown> }).env));
|
|
187
|
+
const missing = WebAssembly.Module.imports(this.module)
|
|
188
|
+
.filter((i) => i.kind === 'function' && (i.module !== 'env' || !provided.has(i.name)))
|
|
189
|
+
.map((i) => `${i.module}.${i.name}`);
|
|
190
|
+
if (missing.length > 0) {
|
|
191
|
+
this.log(
|
|
192
|
+
pc.red(
|
|
193
|
+
` ✗ cold daemon wasm imports unsupported host functions: ${missing.join(', ')}`,
|
|
194
|
+
) + '\n',
|
|
195
|
+
);
|
|
196
|
+
this.module = null;
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
this.instance = new WebAssembly.Instance(this.module, imports);
|
|
201
|
+
this.exports = this.instance.exports as unknown as ColdExports;
|
|
202
|
+
ref.memory = this.exports.memory;
|
|
203
|
+
|
|
204
|
+
try {
|
|
205
|
+
if (typeof this.exports.init === 'function') this.exports.init();
|
|
206
|
+
const rc = this.exports.daemon_start();
|
|
207
|
+
if (rc !== 0) {
|
|
208
|
+
this.log(
|
|
209
|
+
pc.red(` ✗ daemon_start() returned ${String(rc)}; daemon not running`) + '\n',
|
|
210
|
+
);
|
|
211
|
+
this.instance = null;
|
|
212
|
+
this.exports = null;
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
} catch (e) {
|
|
216
|
+
// A trap in daemon_start leaves the daemon stopped; surface it (do not
|
|
217
|
+
// retry-loop in dev, mirroring the request-path error handling).
|
|
218
|
+
this.log(pc.red(` ✗ daemon_start() trapped: ${String(e)}`) + '\n');
|
|
219
|
+
this.instance = null;
|
|
220
|
+
this.exports = null;
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
this.running = true;
|
|
225
|
+
const limited = this.catalog.tasks.slice(0, this.cfg.maxTasks);
|
|
226
|
+
for (const task of limited) this.registerTask(task);
|
|
227
|
+
this.log(
|
|
228
|
+
pc.green(' ⏱ ') +
|
|
229
|
+
pc.dim(
|
|
230
|
+
`daemon started (epoch ${String(this.epochValue)}, ${String(limited.length)} task${
|
|
231
|
+
limited.length === 1 ? '' : 's'
|
|
232
|
+
})`,
|
|
233
|
+
) +
|
|
234
|
+
'\n',
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Clear timers, drop the resident instance. In-flight ticks finish on their own
|
|
239
|
+
* (the overlap guard prevents a NEW tick; a running one completes). */
|
|
240
|
+
private stop(): void {
|
|
241
|
+
for (const t of this.timers.values()) clearTimeout(t);
|
|
242
|
+
this.timers.clear();
|
|
243
|
+
this.nextFire.clear();
|
|
244
|
+
// Best-effort guest stop hook, if the artifact exports one.
|
|
245
|
+
const stop = (this.exports as unknown as { daemon_stop?: () => void } | null)?.daemon_stop;
|
|
246
|
+
if (typeof stop === 'function') {
|
|
247
|
+
try {
|
|
248
|
+
stop();
|
|
249
|
+
} catch {
|
|
250
|
+
/* ignore a trap in the optional stop hook */
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
this.instance = null;
|
|
254
|
+
this.exports = null;
|
|
255
|
+
this.running = false;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
private registerTask(task: ScheduledTask): void {
|
|
259
|
+
if (task.schedule.kind === 'interval') {
|
|
260
|
+
const ms = Math.max(1000, task.schedule.ms || this.cfg.defaultIntervalMs);
|
|
261
|
+
this.nextFire.set(task.taskIndex, Date.now() + ms);
|
|
262
|
+
const handle = setInterval(() => {
|
|
263
|
+
this.nextFire.set(task.taskIndex, Date.now() + ms);
|
|
264
|
+
this.runTick(task);
|
|
265
|
+
}, ms);
|
|
266
|
+
// Do not keep the event loop alive solely for the dev scheduler.
|
|
267
|
+
handle.unref?.();
|
|
268
|
+
this.timers.set(task.taskIndex, handle);
|
|
269
|
+
} else {
|
|
270
|
+
const masks = task.schedule.masks;
|
|
271
|
+
if (cronNeverFires(masks)) {
|
|
272
|
+
this.log(
|
|
273
|
+
pc.yellow(' ! ') +
|
|
274
|
+
pc.dim(
|
|
275
|
+
`@scheduled ${task.name} has an unsatisfiable cron mask; skipping (DAEMON_SCHEDULE_REJECTED)`,
|
|
276
|
+
) +
|
|
277
|
+
'\n',
|
|
278
|
+
);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
this.armCron(task, masks);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** Arm a one-shot timer to the next cron fire time, re-arming after each tick. */
|
|
286
|
+
private armCron(task: ScheduledTask, masks: CronMasks): void {
|
|
287
|
+
const next = nextCronFireMs(masks, Date.now());
|
|
288
|
+
if (next === null) {
|
|
289
|
+
this.nextFire.delete(task.taskIndex);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
this.nextFire.set(task.taskIndex, next);
|
|
293
|
+
const delay = Math.max(0, next - Date.now());
|
|
294
|
+
const handle = setTimeout(() => {
|
|
295
|
+
// Guard against a coarse-timer early fire: only run when the masks
|
|
296
|
+
// actually match the current minute (they should by construction).
|
|
297
|
+
if (cronMatches(masks, new Date())) this.runTick(task);
|
|
298
|
+
if (this.running) this.armCron(task, masks);
|
|
299
|
+
}, delay);
|
|
300
|
+
handle.unref?.();
|
|
301
|
+
this.timers.set(task.taskIndex, handle);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Fire one `@scheduled` task via `scheduled_tick(task_id)` (Part 2). */
|
|
305
|
+
private runTick(task: ScheduledTask): void {
|
|
306
|
+
if (!this.running || this.exports === null) return;
|
|
307
|
+
if (this.ticking.has(task.taskIndex)) {
|
|
308
|
+
// overlap_policy 0 = skip-if-running: a slow tick must not pile up.
|
|
309
|
+
this.log(
|
|
310
|
+
pc.dim(` ⏱ @scheduled ${task.name} overran its interval; skipping a tick`) + '\n',
|
|
311
|
+
);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
if (!this.isLeader()) return; // always true in dev; kept for parity
|
|
315
|
+
this.ticking.add(task.taskIndex);
|
|
316
|
+
const startedAt = Date.now();
|
|
317
|
+
try {
|
|
318
|
+
const ret = this.exports.scheduled_tick(task.taskIndex); // packed-i64
|
|
319
|
+
if (ret < 0n)
|
|
320
|
+
this.log(
|
|
321
|
+
pc.yellow(
|
|
322
|
+
` ⏱ @scheduled ${task.name} returned error ${decodeAbiError(ret)}`,
|
|
323
|
+
) + '\n',
|
|
324
|
+
);
|
|
325
|
+
} catch (e) {
|
|
326
|
+
// A trapped tick does NOT tear down the long-lived daemon box (unlike a
|
|
327
|
+
// stream box); the next tick runs normally on the same memory.
|
|
328
|
+
this.log(pc.red(` ✗ @scheduled ${task.name} trapped: ${String(e)}`) + '\n');
|
|
329
|
+
} finally {
|
|
330
|
+
const took = Date.now() - startedAt;
|
|
331
|
+
if (took > this.cfg.tickBudgetMs)
|
|
332
|
+
this.log(
|
|
333
|
+
pc.yellow(
|
|
334
|
+
` ⏱ @scheduled ${task.name} took ${String(took)}ms (> tickBudgetMs ${String(
|
|
335
|
+
this.cfg.tickBudgetMs,
|
|
336
|
+
)})`,
|
|
337
|
+
) + '\n',
|
|
338
|
+
);
|
|
339
|
+
this.ticking.delete(task.taskIndex);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** Tear the daemon down for good (dev-server shutdown). */
|
|
344
|
+
close(): void {
|
|
345
|
+
if (this.running) this.stop();
|
|
346
|
+
this.module = null;
|
|
347
|
+
this.catalog = null;
|
|
348
|
+
}
|
|
349
|
+
}
|