toiljs 0.0.59 → 0.0.61

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.
Files changed (158) hide show
  1. package/.github/workflows/ci.yml +31 -0
  2. package/CHANGELOG.md +15 -0
  3. package/build/cli/.tsbuildinfo +1 -1
  4. package/build/cli/index.js +311 -118
  5. package/build/client/.tsbuildinfo +1 -1
  6. package/build/client/index.d.ts +1 -1
  7. package/build/client/index.js +1 -1
  8. package/build/client/routing/mount.js +12 -1
  9. package/build/client/ssr/markers.d.ts +1 -0
  10. package/build/client/ssr/markers.js +3 -0
  11. package/build/compiler/.tsbuildinfo +1 -1
  12. package/build/compiler/config.d.ts +21 -0
  13. package/build/compiler/config.js +35 -0
  14. package/build/compiler/docs.d.ts +2 -1
  15. package/build/compiler/docs.js +33 -304
  16. package/build/compiler/index.d.ts +13 -0
  17. package/build/compiler/index.js +113 -21
  18. package/build/compiler/template-build.d.ts +21 -1
  19. package/build/compiler/template-build.js +110 -26
  20. package/build/compiler/toil-docs.generated.d.ts +1 -0
  21. package/build/compiler/toil-docs.generated.js +20 -0
  22. package/build/devserver/.tsbuildinfo +1 -1
  23. package/build/devserver/daemon/catalog.d.ts +26 -0
  24. package/build/devserver/daemon/catalog.js +48 -0
  25. package/build/devserver/daemon/cron.d.ts +4 -0
  26. package/build/devserver/daemon/cron.js +50 -0
  27. package/build/devserver/daemon/host.d.ts +37 -0
  28. package/build/devserver/daemon/host.js +94 -0
  29. package/build/devserver/daemon/index.d.ts +34 -0
  30. package/build/devserver/daemon/index.js +241 -0
  31. package/build/devserver/db/catalog.d.ts +2 -0
  32. package/build/devserver/db/catalog.js +80 -0
  33. package/build/devserver/db/database.d.ts +80 -0
  34. package/build/devserver/db/database.js +1032 -0
  35. package/build/devserver/db/index.d.ts +3 -0
  36. package/build/devserver/db/index.js +3 -0
  37. package/build/devserver/db/routeKinds.d.ts +8 -0
  38. package/build/devserver/db/routeKinds.js +139 -0
  39. package/build/devserver/db/types.d.ts +121 -0
  40. package/build/devserver/db/types.js +52 -0
  41. package/build/devserver/email/index.js +1 -1
  42. package/build/devserver/index.d.ts +19 -24
  43. package/build/devserver/index.js +11 -165
  44. package/build/devserver/mstore/store.d.ts +18 -0
  45. package/build/devserver/mstore/store.js +82 -0
  46. package/build/devserver/{host.d.ts → runtime/host.d.ts} +7 -1
  47. package/build/devserver/{host.js → runtime/host.js} +51 -7
  48. package/build/devserver/{module.d.ts → runtime/module.d.ts} +2 -1
  49. package/build/devserver/{module.js → runtime/module.js} +34 -1
  50. package/build/devserver/server.d.ts +23 -0
  51. package/build/devserver/server.js +223 -0
  52. package/build/devserver/ssr.d.ts +25 -0
  53. package/build/devserver/ssr.js +114 -0
  54. package/build/devserver/wasm/sections.d.ts +2 -0
  55. package/build/devserver/wasm/sections.js +42 -0
  56. package/build/devserver/wasm/surface.d.ts +18 -0
  57. package/build/devserver/wasm/surface.js +41 -0
  58. package/docs/README.md +4 -4
  59. package/docs/auth-todo.md +6 -6
  60. package/docs/caching.md +5 -5
  61. package/docs/cli.md +15 -0
  62. package/docs/client.md +40 -0
  63. package/docs/crypto.md +4 -4
  64. package/docs/data.md +6 -6
  65. package/docs/email.md +28 -28
  66. package/docs/environment.md +10 -10
  67. package/docs/index.md +26 -0
  68. package/docs/ratelimit.md +10 -10
  69. package/docs/routing.md +2 -2
  70. package/docs/server.md +61 -0
  71. package/docs/ssr.md +561 -113
  72. package/docs/styling.md +22 -0
  73. package/docs/time.md +3 -3
  74. package/eslint.config.js +10 -1
  75. package/examples/basic/client/components/Header.tsx +3 -0
  76. package/examples/basic/client/routes/features/actions.tsx +0 -2
  77. package/examples/basic/client/routes/hello.tsx +89 -19
  78. package/examples/basic/client/styles/main.css +48 -0
  79. package/examples/basic/server/SsrHelloRender.ts +97 -0
  80. package/examples/basic/server/main.ts +5 -0
  81. package/examples/basic/server/migrations/GuestEntry.migration.ts +39 -0
  82. package/examples/basic/server/streams/Echo.ts +49 -0
  83. package/package.json +12 -10
  84. package/scripts/gen-toil-docs.mjs +96 -0
  85. package/server/runtime/time.ts +3 -3
  86. package/src/cli/create.ts +40 -3
  87. package/src/cli/db.ts +158 -0
  88. package/src/cli/diagnostics.ts +19 -0
  89. package/src/cli/doctor.ts +20 -0
  90. package/src/cli/index.ts +10 -0
  91. package/src/cli/update.ts +58 -0
  92. package/src/client/index.ts +1 -1
  93. package/src/client/routing/mount.tsx +18 -2
  94. package/src/client/ssr/markers.tsx +22 -0
  95. package/src/compiler/config.ts +88 -2
  96. package/src/compiler/docs.ts +47 -308
  97. package/src/compiler/index.ts +236 -32
  98. package/src/compiler/ssr-codegen.ts +1 -1
  99. package/src/compiler/template-build.ts +247 -46
  100. package/src/compiler/toil-docs.generated.ts +26 -0
  101. package/src/devserver/daemon/catalog.ts +120 -0
  102. package/src/devserver/daemon/cron.ts +87 -0
  103. package/src/devserver/daemon/host.ts +224 -0
  104. package/src/devserver/daemon/index.ts +349 -0
  105. package/src/devserver/db/catalog.ts +108 -0
  106. package/src/devserver/db/database.ts +1633 -0
  107. package/src/devserver/db/index.ts +18 -0
  108. package/src/devserver/db/routeKinds.ts +147 -0
  109. package/src/devserver/db/types.ts +139 -0
  110. package/src/devserver/email/index.ts +1 -1
  111. package/src/devserver/index.ts +31 -287
  112. package/src/devserver/mstore/store.ts +121 -0
  113. package/src/devserver/{host.ts → runtime/host.ts} +98 -7
  114. package/src/devserver/{module.ts → runtime/module.ts} +47 -1
  115. package/src/devserver/server.ts +393 -0
  116. package/src/devserver/ssr.ts +166 -0
  117. package/src/devserver/wasm/sections.ts +59 -0
  118. package/src/devserver/wasm/surface.ts +88 -0
  119. package/test/daemon-build.test.ts +198 -0
  120. package/test/daemon-catalog.test.ts +265 -0
  121. package/test/daemon-emulation.test.ts +216 -0
  122. package/test/db.test.ts +0 -0
  123. package/test/devserver-database.test.ts +510 -14
  124. package/test/devserver-pqauth.test.ts +1 -1
  125. package/test/devserver-secrets.test.ts +5 -1
  126. package/test/doctor.test.ts +13 -0
  127. package/test/email-preview.test.ts +6 -1
  128. package/test/example-guestbook.test.ts +43 -1
  129. package/test/fixtures/daemon-app.ts +56 -0
  130. package/test/global-setup.ts +17 -0
  131. package/test/pqauth-e2e.test.ts +1 -1
  132. package/test/ssr-render.test.ts +94 -27
  133. package/test/ssr-template.test.tsx +44 -1
  134. package/vitest.config.ts +3 -0
  135. package/build/devserver/database.d.ts +0 -8
  136. package/build/devserver/database.js +0 -418
  137. package/src/devserver/database.ts +0 -618
  138. /package/build/devserver/{dotenv.d.ts → config/dotenv.d.ts} +0 -0
  139. /package/build/devserver/{dotenv.js → config/dotenv.js} +0 -0
  140. /package/build/devserver/{env.d.ts → config/env.d.ts} +0 -0
  141. /package/build/devserver/{env.js → config/env.js} +0 -0
  142. /package/build/devserver/{ratelimit.d.ts → config/ratelimit.d.ts} +0 -0
  143. /package/build/devserver/{ratelimit.js → config/ratelimit.js} +0 -0
  144. /package/build/devserver/{cache.d.ts → http/cache.d.ts} +0 -0
  145. /package/build/devserver/{cache.js → http/cache.js} +0 -0
  146. /package/build/devserver/{envelope.d.ts → http/envelope.d.ts} +0 -0
  147. /package/build/devserver/{envelope.js → http/envelope.js} +0 -0
  148. /package/build/devserver/{proxy.d.ts → http/proxy.d.ts} +0 -0
  149. /package/build/devserver/{proxy.js → http/proxy.js} +0 -0
  150. /package/build/devserver/{crypto.d.ts → runtime/crypto.d.ts} +0 -0
  151. /package/build/devserver/{crypto.js → runtime/crypto.js} +0 -0
  152. /package/src/devserver/{dotenv.ts → config/dotenv.ts} +0 -0
  153. /package/src/devserver/{env.ts → config/env.ts} +0 -0
  154. /package/src/devserver/{ratelimit.ts → config/ratelimit.ts} +0 -0
  155. /package/src/devserver/{cache.ts → http/cache.ts} +0 -0
  156. /package/src/devserver/{envelope.ts → http/envelope.ts} +0 -0
  157. /package/src/devserver/{proxy.ts → http/proxy.ts} +0 -0
  158. /package/src/devserver/{crypto.ts → runtime/crypto.ts} +0 -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
+ }
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Parse a compiled server wasm's `toildb.catalog` custom section into structured
3
+ * dev capability metadata keyed by `"<Db>/<collection>"` (the resolve key the
4
+ * guest passes to `data.resolve_collection`).
5
+ *
6
+ * The dev DB uses this to STAMP each write with the value type's current schema
7
+ * version. When the developer evolves a `@data` type and rebuilds, the catalog
8
+ * version changes; data already on disk keeps its OLD stamp, so a read surfaces
9
+ * that old version and the guest's woven `decodeInto` runs the `@migrate` - the
10
+ * dev-side equivalent of the edge binding the cap's schema_version into the row.
11
+ * The parsed family is also used to reject wrong-family imports locally.
12
+ *
13
+ * Wire format mirrors `toildb::catalog` / the backend `db_catalog` decoder and the
14
+ * compiler's `buildToilDbCatalog` emitter (all little-endian).
15
+ */
16
+
17
+ import { DataReader } from 'toiljs/io';
18
+ import { customSection } from '../wasm/sections.js';
19
+ import {
20
+ CollectionFamily,
21
+ type DbCatalogState,
22
+ DEFAULT_FILL_WAIT_MS,
23
+ type DevCollectionHandle,
24
+ isCollectionFamily,
25
+ MAX_FILL_WAIT_MS,
26
+ } from './types.js';
27
+
28
+ function validReplication(value: number): boolean {
29
+ return value >= 0 && value <= 5;
30
+ }
31
+
32
+ function validPlacement(value: number): boolean {
33
+ return value === 0 || value === 1;
34
+ }
35
+
36
+ /** Decode the devserver's catalog state. A missing section stays distinct from a
37
+ * present-but-bad section so `resolve_collection` can match the edge's
38
+ * Present/Malformed/NoSection admission behavior. */
39
+ export function parseCatalog(wasm: Buffer): DbCatalogState {
40
+ const collections = new Map<string, DevCollectionHandle>();
41
+ let sec: Buffer | null;
42
+ try {
43
+ sec = customSection(wasm, 'toildb.catalog');
44
+ } catch {
45
+ return { kind: 'no-section' }; // garbage section table (mid-rebuild) -> no catalog
46
+ }
47
+ if (sec === null) return { kind: 'no-section' };
48
+
49
+ const r = new DataReader(sec);
50
+ const version = r.readU16();
51
+ if (!r.ok || (version !== 1 && version !== 2)) return { kind: 'malformed' };
52
+ const ndb = r.readU16();
53
+ for (let d = 0; d < ndb && r.ok; d++) {
54
+ const db = r.readString();
55
+ const nc = r.readU16();
56
+ for (let c = 0; c < nc && r.ok; c++) {
57
+ const name = r.readString();
58
+ const family = r.readU8();
59
+ r.readString(); // keyType
60
+ r.readString(); // valueType
61
+ r.readU32(); // valueDataId
62
+ const schemaVersion = r.readU32();
63
+ r.readU32(); // generation
64
+ const replication = r.readU8(); // emitter order: replication then placement
65
+ const placement = r.readU8();
66
+ let fillMaxWaitMs = DEFAULT_FILL_WAIT_MS;
67
+ let fillAllowStale = true;
68
+ if (version >= 2) {
69
+ fillMaxWaitMs = r.readU32();
70
+ const fillAllowStaleByte = r.readU8();
71
+ if (
72
+ fillMaxWaitMs > MAX_FILL_WAIT_MS ||
73
+ (fillAllowStaleByte !== 0 && fillAllowStaleByte !== 1)
74
+ )
75
+ return { kind: 'malformed' };
76
+ fillAllowStale = fillAllowStaleByte === 1;
77
+ }
78
+ const nFields = r.readU16();
79
+ for (let f = 0; f < nFields; f++) {
80
+ r.readString(); // field name
81
+ r.readString(); // field type
82
+ r.readU8(); // isArray
83
+ }
84
+ const nMig = r.readU16();
85
+ for (let m = 0; m < nMig; m++) r.readU32(); // migratableFrom versions
86
+ if (
87
+ !isCollectionFamily(family) ||
88
+ !validReplication(replication) ||
89
+ !validPlacement(placement)
90
+ )
91
+ return { kind: 'malformed' };
92
+ const key = db + '/' + name;
93
+ if (collections.has(key)) return { kind: 'malformed' };
94
+ if (r.ok)
95
+ collections.set(key, {
96
+ name: key,
97
+ family: family as CollectionFamily,
98
+ schemaVersion: schemaVersion >>> 0,
99
+ replication,
100
+ placement,
101
+ fillMaxWaitMs,
102
+ fillAllowStale,
103
+ });
104
+ }
105
+ }
106
+ if (!r.ok || r.remaining() !== 0) return { kind: 'malformed' };
107
+ return { kind: 'present', collections };
108
+ }