@native-sdk/cli 0.5.3 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/build/app.zig +91 -2
  2. package/build.zig +756 -5
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +19 -1
  5. package/package.json +9 -9
  6. package/packages/core/package-lock.json +2 -2
  7. package/packages/core/package.json +1 -1
  8. package/packages/core/rt/rt.zig +416 -9
  9. package/packages/core/sdk/core.ts +706 -5
  10. package/packages/core/sdk/events.ts +17 -8
  11. package/packages/core/src/checker.ts +305 -2
  12. package/packages/core/src/diagnostics.ts +12 -0
  13. package/packages/core/src/emitter.ts +3187 -264
  14. package/packages/core/src/typed_ast.ts +51 -4
  15. package/packages/core/src/types.ts +34 -1
  16. package/skill-data/native-ui/SKILL.md +42 -4
  17. package/skill-data/ts-core/SKILL.md +13 -3
  18. package/src/app_runner/root.zig +112 -10
  19. package/src/app_runner/ts_core_main.zig +4 -0
  20. package/src/automation/layout_fingerprint.zig +160 -0
  21. package/src/automation/protocol.zig +82 -33
  22. package/src/automation/root.zig +1 -0
  23. package/src/automation/server.zig +1 -1
  24. package/src/automation/snapshot.zig +68 -9
  25. package/src/automation/watcher.zig +1 -1
  26. package/src/embed/host.zig +5 -0
  27. package/src/embed/tests.zig +119 -1
  28. package/src/embed/ui_host.zig +13 -0
  29. package/src/platform/linux/gtk_host.c +639 -16
  30. package/src/platform/linux/gtk_host.h +25 -0
  31. package/src/platform/linux/root.zig +351 -7
  32. package/src/platform/macos/appkit_host.h +152 -8
  33. package/src/platform/macos/appkit_host.m +1816 -100
  34. package/src/platform/macos/cef_host.mm +372 -11
  35. package/src/platform/macos/root.zig +379 -11
  36. package/src/platform/null_platform.zig +696 -9
  37. package/src/platform/null_platform_tests.zig +35 -0
  38. package/src/platform/root.zig +42 -0
  39. package/src/platform/types.zig +524 -18
  40. package/src/platform/windows/root.zig +432 -9
  41. package/src/platform/windows/webview2_host.cpp +482 -45
  42. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  43. package/src/primitives/app_manifest/root.zig +1 -0
  44. package/src/primitives/app_manifest/types.zig +32 -0
  45. package/src/primitives/app_manifest/validation.zig +18 -0
  46. package/src/primitives/canvas/a11y_audit.zig +33 -11
  47. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  48. package/src/primitives/canvas/drawing.zig +25 -0
  49. package/src/primitives/canvas/equality.zig +8 -0
  50. package/src/primitives/canvas/events.zig +103 -16
  51. package/src/primitives/canvas/frame.zig +125 -1
  52. package/src/primitives/canvas/layout_audit.zig +22 -5
  53. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  54. package/src/primitives/canvas/render_tests.zig +13 -10
  55. package/src/primitives/canvas/root.zig +48 -0
  56. package/src/primitives/canvas/terminal_box.zig +471 -0
  57. package/src/primitives/canvas/terminal_grid.zig +978 -0
  58. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  59. package/src/primitives/canvas/test_support.zig +1 -0
  60. package/src/primitives/canvas/tests.zig +2 -0
  61. package/src/primitives/canvas/text_tests.zig +39 -4
  62. package/src/primitives/canvas/tokens.zig +115 -12
  63. package/src/primitives/canvas/ui.zig +535 -12
  64. package/src/primitives/canvas/ui_markup.zig +253 -36
  65. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  66. package/src/primitives/canvas/ui_markup_compiled.zig +195 -9
  67. package/src/primitives/canvas/ui_markup_compiled_tests.zig +188 -8
  68. package/src/primitives/canvas/ui_markup_contract.zig +89 -6
  69. package/src/primitives/canvas/ui_markup_contract_tests.zig +65 -1
  70. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  71. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  72. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  73. package/src/primitives/canvas/ui_markup_tests.zig +178 -9
  74. package/src/primitives/canvas/ui_markup_view.zig +220 -11
  75. package/src/primitives/canvas/ui_markup_view_tests.zig +553 -28
  76. package/src/primitives/canvas/ui_schema.zig +110 -17
  77. package/src/primitives/canvas/ui_schema_tests.zig +18 -10
  78. package/src/primitives/canvas/ui_tests.zig +141 -9
  79. package/src/primitives/canvas/widget_access.zig +28 -2
  80. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  81. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  82. package/src/primitives/canvas/widget_layout.zig +59 -11
  83. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  84. package/src/primitives/canvas/widget_render.zig +272 -18
  85. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  86. package/src/primitives/canvas/widget_routing.zig +59 -7
  87. package/src/primitives/canvas/widget_runtime.zig +23 -0
  88. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  89. package/src/primitives/canvas/widget_semantics.zig +119 -11
  90. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  91. package/src/primitives/canvas/widget_tree.zig +17 -0
  92. package/src/primitives/canvas/widgets.zig +132 -0
  93. package/src/root.zig +23 -0
  94. package/src/runtime/api.zig +76 -5
  95. package/src/runtime/automation_commands.zig +20 -1
  96. package/src/runtime/automation_snapshot.zig +25 -1
  97. package/src/runtime/automation_widget_dispatch.zig +73 -8
  98. package/src/runtime/bridge_payload.zig +8 -0
  99. package/src/runtime/bridge_responses.zig +5 -0
  100. package/src/runtime/builtin_bridge.zig +9 -4
  101. package/src/runtime/canvas_font_tests.zig +439 -2
  102. package/src/runtime/canvas_fonts.zig +61 -6
  103. package/src/runtime/canvas_frame.zig +62 -7
  104. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  105. package/src/runtime/canvas_frame_helpers.zig +190 -1
  106. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  107. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  108. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  109. package/src/runtime/canvas_image_tests.zig +195 -0
  110. package/src/runtime/canvas_images.zig +45 -12
  111. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  112. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  113. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  114. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  115. package/src/runtime/canvas_widget_display.zig +1 -1
  116. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  117. package/src/runtime/canvas_widget_events.zig +240 -24
  118. package/src/runtime/canvas_widget_runtime.zig +213 -29
  119. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  120. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  121. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  122. package/src/runtime/canvas_widget_state.zig +6 -0
  123. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  124. package/src/runtime/core.zig +131 -13
  125. package/src/runtime/effects.zig +8136 -180
  126. package/src/runtime/effects_channel_tests.zig +3519 -0
  127. package/src/runtime/effects_fetch_tests.zig +89 -1
  128. package/src/runtime/effects_file_tests.zig +41 -1
  129. package/src/runtime/effects_image_tests.zig +1304 -0
  130. package/src/runtime/effects_pty_tests.zig +1362 -0
  131. package/src/runtime/effects_tests.zig +167 -9
  132. package/src/runtime/effects_video_tests.zig +3609 -0
  133. package/src/runtime/flow.zig +42 -4
  134. package/src/runtime/gpu_surface_events.zig +473 -13
  135. package/src/runtime/media_surface.zig +116 -11
  136. package/src/runtime/media_surface_tests.zig +61 -0
  137. package/src/runtime/pty.zig +1252 -0
  138. package/src/runtime/pty_windows.zig +1780 -0
  139. package/src/runtime/root.zig +36 -0
  140. package/src/runtime/runtime_event_tests.zig +20 -1
  141. package/src/runtime/session_blobs.zig +689 -0
  142. package/src/runtime/session_journal.zig +432 -62
  143. package/src/runtime/session_record.zig +43 -2
  144. package/src/runtime/session_replay.zig +556 -20
  145. package/src/runtime/session_tests.zig +2087 -1
  146. package/src/runtime/shell_layout.zig +7 -0
  147. package/src/runtime/terminal_session.zig +1569 -0
  148. package/src/runtime/terminal_session_tests.zig +450 -0
  149. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  150. package/src/runtime/terminal_vt_stub.zig +13 -0
  151. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  152. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  153. package/src/runtime/tests.zig +8 -0
  154. package/src/runtime/ts_core_host.zig +1320 -45
  155. package/src/runtime/ts_core_host_tests.zig +1100 -1
  156. package/src/runtime/ts_ui_app.zig +23 -5
  157. package/src/runtime/ui_app.zig +2444 -69
  158. package/src/runtime/ui_app_tests.zig +3171 -744
  159. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  160. package/src/runtime/ui_app_window_tests.zig +736 -0
  161. package/src/runtime/view.zig +190 -2
  162. package/src/runtime/view_widget_scroll.zig +218 -68
  163. package/src/runtime/view_widget_text.zig +11 -2
  164. package/src/runtime/view_widget_tree.zig +111 -3
  165. package/src/runtime/window_command_bridge_tests.zig +117 -0
  166. package/src/runtime/window_storage.zig +77 -2
  167. package/src/runtime/window_view_runtime.zig +34 -2
  168. package/src/runtime/window_views.zig +62 -1
  169. package/src/terminal_session_tests_root.zig +15 -0
  170. package/src/tooling/manifest.zig +75 -0
  171. package/src/tooling/raw_manifest.zig +2 -0
  172. package/src/tooling/templates.zig +244 -8
@@ -1,6 +1,6 @@
1
1
  //! The native host consumer for transpiled app cores: bridges the
2
2
  //! versioned command/subscription wire format a transpiled core emits
3
- //! (`packages/core/rt/rt.zig`, `cmd_format_version` 2) onto the
3
+ //! (`packages/core/rt/rt.zig`, `cmd_format_version` 3) onto the
4
4
  //! real effect engine (`effects.zig`). The transpiler's output is a
5
5
  //! pure Model/Msg/update core whose effects are INERT BYTES — this
6
6
  //! module is the one place those bytes become engine calls, so the
@@ -127,6 +127,122 @@
127
127
  //! the entry: the platform may still speak (soundboard
128
128
  //! starts the next track from `completed`), and stop
129
129
  //! is the explicit close.
130
+ //! image_load -> `fx.loadImage` keyed by the app's own numeric
131
+ //! ImageId (the engine registers the pixels under it,
132
+ //! so the bridge holds a small id->tag table rather
133
+ //! than minting an engine key). One terminal per load
134
+ //! routes the event arm — a five-field record built by
135
+ //! field NAME (id/state/width/height/status; `state`'s
136
+ //! enum members are matched by member name, and `id`
137
+ //! echoes the requested ImageId so concurrent loads
138
+ //! sharing one arm stay distinguishable) — and the
139
+ //! entry retires on it. A URL record carrying no cache
140
+ //! path loads under the engine's conventional
141
+ //! content-addressed image cache path when the wiring
142
+ //! configured a caches directory (`setImageCacheDir` /
143
+ //! `TsUiApp`'s `image_cache_dir`), derived bridge-side
144
+ //! from the URL alone like the audio cache. A
145
+ //! duplicate LIVE id rejects the new load (the spawn
146
+ //! discipline: one load per id, never replaced
147
+ //! implicitly), dispatching state "rejected" at the
148
+ //! next drain, in command-stream order with the
149
+ //! engine's own refusals, the refused id echoed; ids
150
+ //! the wire cannot carry exactly (0, non-integers,
151
+ //! 2^53 and past — the SDK contract is BELOW 2^53)
152
+ //! reject the same way echoing id 0, and so does a
153
+ //! 17th in-flight load (a full bridge table).
154
+ //! Image loads are not the string-keyed cancel's to
155
+ //! end (they are keyed by numeric id, not a wire key)
156
+ //! — `image_cancel` is their cancel.
157
+ //! image_cancel-> `fx.cancel(id)` on the live load under the id, if
158
+ //! any: the engine's `.cancelled` terminal routes the
159
+ //! load's own event arm (state "cancelled") and
160
+ //! retires the entry, freeing the id for a fresh load
161
+ //! — LOUD, the spawn cancel discipline. An id naming
162
+ //! no live load (or one the wire cannot carry exactly)
163
+ //! is a no-op, audio_ctl's idle rule.
164
+ //! image_unregister
165
+ //! -> `fx.unregisterImage(id)`: free the registry slot
166
+ //! under the id — registration's synchronous
167
+ //! discipline in reverse, direct registry surgery
168
+ //! with no terminal and no Msg. An id with no
169
+ //! registration (or one the wire cannot carry
170
+ //! exactly) is a no-op, image_cancel's idle rule. It
171
+ //! never touches the load table: a load in flight
172
+ //! under the id keeps running and its terminal still
173
+ //! registers the pixels — cancel the load first to
174
+ //! keep the slot free.
175
+ //! channel_open-> `fx.openChannel` keyed by the app's own numeric key
176
+ //! (the image_load id convention: the raw key IS the
177
+ //! engine key, and the bridge holds a key->tag table).
178
+ //! The entry is non-retiring the spawn way: every
179
+ //! channel event routes the event arm — a five-field
180
+ //! record built by field NAME (key/state/bytes/
181
+ //! droppedPending/droppedTotal; `state`'s enum members
182
+ //! are matched by member name, exactly the
183
+ //! data/closed/rejected set) — until the one `closed`
184
+ //! (or a refused open's `rejected`) terminal retires
185
+ //! it. POSTING is not a TS-tier verb: transpiled cores
186
+ //! are single-threaded, so the thread-safe posting
187
+ //! handle is native-side API (`Effects.channelHandle`)
188
+ //! for embedders and platform-services extensions —
189
+ //! the TS tier opens, closes, and receives. A
190
+ //! duplicate LIVE key rejects the new open (one
191
+ //! channel per key, never replaced implicitly),
192
+ //! dispatching state "rejected" at the next drain, in
193
+ //! command-stream order with the engine's own
194
+ //! refusals, the refused key echoed; keys the wire
195
+ //! cannot carry exactly reject the same way echoing
196
+ //! key 0, and so does a full bridge table.
197
+ //! channel_close-> `fx.closeChannel(key)` on the live channel under
198
+ //! the key, if any: staged posts flush and the
199
+ //! engine's one `.closed` terminal routes the entry's
200
+ //! event arm and retires it. A key naming no live
201
+ //! channel (or one the wire cannot carry exactly) is
202
+ //! a no-op, audio_ctl's idle rule. Channels are keyed
203
+ //! numerically, so the string-keyed cancel never
204
+ //! touches them — this is their close.
205
+ //! pty_spawn -> `fx.ptySpawn` through the PTY table (`pty_key_base`
206
+ //! + index) — non-retiring the spawn way: coalesced
207
+ //! "output" batches route the event arm across
208
+ //! dispatches — a six-field record built by field NAME
209
+ //! (state/bytes/code/reason/signal/droppedWrites;
210
+ //! `state`'s and `reason`'s enum members are matched
211
+ //! by member name, so the app's declaration order is
212
+ //! free) — and the exactly-one "exit" terminal retires
213
+ //! the entry. An empty wire TERM opens with the
214
+ //! engine's default (the fetch-timeout convention);
215
+ //! grid dimensions ride the wire as f64 subset numbers
216
+ //! and a value the u16 transport cannot carry exactly
217
+ //! spawns with dimension 0, which the engine answers
218
+ //! with one "rejected" exit — loud, never a guess. A
219
+ //! duplicate LIVE wire key rejects the new spawn (the
220
+ //! spawn discipline: a running terminal's child is a
221
+ //! running subprocess, never killed implicitly),
222
+ //! dispatching one "exit" with reason "rejected" at
223
+ //! the next drain, in command-stream order with the
224
+ //! engine's own refusals; a full bridge table rejects
225
+ //! the same way.
226
+ //! pty_write -> `fx.ptyWrite` on the live session under the wire
227
+ //! key — fire-and-forget (keystrokes): a key naming no
228
+ //! open session is a no-op (the exit was already on
229
+ //! its way), and refused payloads count into the exit
230
+ //! event's droppedWrites, never silence.
231
+ //! pty_resize -> `fx.ptyResize` on the live session under the wire
232
+ //! key — fire-and-forget like pty_write; a key naming
233
+ //! no open session is a no-op, and a grid value the
234
+ //! u16 transport cannot carry exactly resizes nothing
235
+ //! (the no-op rule: there is no honest grid to push).
236
+ //! pty_kill -> `fx.ptyKill` on the live session under the wire key
237
+ //! — LOUD, the spawn cancel discipline: the engine
238
+ //! ends the child and the session's one "exit"
239
+ //! terminal routes its own event arm with reason
240
+ //! "cancelled", retiring the entry. A key naming no
241
+ //! open session is a no-op. Sessions are keyed by
242
+ //! their own family's verbs, so the string-keyed
243
+ //! `cancel` record never touches them — pty_kill is
244
+ //! their kill, the way audio_ctl stop is audio's
245
+ //! close.
130
246
  //! audio_ctl -> the engine's control verbs (`fx.pauseAudio`/
131
247
  //! `resumeAudio`/`stopAudio`/`seekAudio`/
132
248
  //! `setAudioVolume`), gated by the wire key: a verb
@@ -134,6 +250,43 @@
134
250
  //! (the playback it aimed at is gone). `stop` also
135
251
  //! retires the bridge entry — no events for that key
136
252
  //! after it.
253
+ //! video_load -> `fx.loadVideo` on the bridge's single video entry.
254
+ //! The engine key is `videoKeyForTag` (the
255
+ //! `video_key_base` namespace with the load's event
256
+ //! tag in the low byte; the engine has ONE player, so
257
+ //! the bridge holds one stream), non-retiring the
258
+ //! audio way, gated by the wire key for verbs: every
259
+ //! `EffectVideo` event (loaded/position/completed/
260
+ //! failed/rejected) routes the arm of the load whose
261
+ //! key it echoes —
262
+ //! a seven-field record built by field NAME (state/
263
+ //! positionMs/durationMs/playing/buffering/width/
264
+ //! height; `state`'s enum members are matched by
265
+ //! member name, so the app's declaration order is
266
+ //! free) — until video_ctl `stop` (or a replacing
267
+ //! video_load, which re-keys and re-routes the same
268
+ //! entry) closes it. The record's surface is the
269
+ //! app's own media-surface id (decoded frames flow
270
+ //! platform-side into that texture channel, never
271
+ //! through this bridge); an id the wire cannot carry
272
+ //! exactly reaches the engine as 0, which it refuses
273
+ //! with one `.rejected` event — never silent.
274
+ //! `completed`/`failed`/`rejected` do NOT retire the
275
+ //! entry: stop is the explicit close, audio's rule.
276
+ //! video_ctl -> the engine's control verbs (`fx.playVideo`/
277
+ //! `pauseVideo`/`stopVideo`/`seekVideo`/
278
+ //! `setVideoVolume`/`setVideoMuted`/`setVideoLoop`),
279
+ //! gated by the wire key: a verb whose key does not
280
+ //! name the open stream is a no-op (the playback it
281
+ //! aimed at is gone). `stop` also retires the bridge
282
+ //! entry — no events for that key after it.
283
+ //! window_show -> `fx.showWindow(label)` — fire-and-forget, label-
284
+ //! addressed like the Zig tier's verb: un-hide +
285
+ //! activate (the tray "Open" consequence of the
286
+ //! menu-bar-app loop). No result Msg; the window's own
287
+ //! frame event carries the state.
288
+ //! quit_app -> `fx.quitApp()` — the graceful terminate through the
289
+ //! same shutdown path a last-window close takes.
137
290
  //! cancel -> by wire key, first match wins in this order: the
138
291
  //! request table (`fx.cancelHostRequest`, silent), the
139
292
  //! named-op table (the entry is marked dropped and
@@ -144,8 +297,8 @@
144
297
  //! the err arm with "cancelled"; killing a process IS
145
298
  //! an observable event, so spawn's cancel stays loud),
146
299
  //! the delay table (`fx.cancelTimer`, silent). The
147
- //! audio stream is not cancel's to end — audio_ctl
148
- //! `stop` is its close.
300
+ //! audio and video streams are not cancel's to end —
301
+ //! their ctl `stop` records are their closes.
149
302
  //!
150
303
  //! THE KEYED-EFFECT DISCIPLINE is ONE rule across request, read_file,
151
304
  //! write_file, fetch, clip_read, and delay: a keyed effect REPLACES its
@@ -155,12 +308,12 @@
155
308
  //! superseded entry dropped and cancelling its engine call; the
156
309
  //! engine's `.cancelled` terminal retires the entry and its Msg is
157
310
  //! swallowed before dispatch. The ONE exception is a live `Cmd.spawn`
158
- //! key: a duplicate REJECTS the new spawn — its err arm dispatches with
159
- //! "rejected" right after the issuing cycle (the same post-cycle
160
- //! boundary `now` dispatches use) — because a running subprocess is
161
- //! never killed implicitly; cancel it first. And spawn's explicit
162
- //! cancel stays loud (err arm "cancelled"), because killing a process
163
- //! is an observable event.
311
+ //! key: a duplicate REJECTS the new spawn — its err arm dispatches
312
+ //! with "rejected" on the next drain, at the refusal's command-stream
313
+ //! position (see the rejection-ordering paragraph below) — because a
314
+ //! running subprocess is never killed implicitly; cancel it first. And
315
+ //! spawn's explicit cancel stays loud (err arm "cancelled"), because
316
+ //! killing a process is an observable event.
164
317
  //! Sub timer -> `fx.startTimer` (repeating) with a fixed slot table
165
318
  //! reconciled by wire key: a new key arms the first
166
319
  //! free slot (slot order — deterministic, never hash
@@ -176,7 +329,18 @@
176
329
  //! `.effects_wake` and presented-frame drains), exactly like Zig-core
177
330
  //! fx results. The bridge adds no scheduler of its own; the one
178
331
  //! deliberate exception is `now` (above), which is synchronous the way
179
- //! `fx.wallMs` is.
332
+ //! `fx.wallMs` is. Bridge-refused rejections (spawn/image/channel)
333
+ //! stage into the ENGINE's loop-side pending order at refusal time
334
+ //! (`Effects.stageLoopMsg`) and dispatch at the next drain — the same
335
+ //! delivery moment as the engine's own refusals, so a `Cmd.batch`
336
+ //! whose records are refused by DIFFERENT layers (the bridge's table
337
+ //! validation, the engine's cross-family key gate) still dispatches
338
+ //! its rejections in COMMAND-STREAM order: one seq-ordered stream is
339
+ //! the ordering authority for every rejection, `Cmd.batch`'s
340
+ //! performed-in-order contract extended to refusals. A second
341
+ //! delivery moment is exactly how families fall out of stream order,
342
+ //! so a new refusing family joins by staging, never by dispatching
343
+ //! from its own boundary.
180
344
  //!
181
345
  //! Result payload lifetime: the engine's result bytes are drain
182
346
  //! scratch, but a routed result's bytes become a Msg payload the core
@@ -227,6 +391,25 @@ pub const spawn_key_base: u64 = 0x5453_5350_0000_0000;
227
391
  /// is the whole surface, so one constant key is the honest shape.
228
392
  pub const audio_key_base: u64 = 0x5453_4155_0000_0000;
229
393
 
394
+ /// The engine-key namespace of the bridge's single video playback
395
+ /// channel ("TSVI") — the audio key's twin, except the low byte
396
+ /// carries the issuing load's event-arm tag: every `EffectVideo` event
397
+ /// echoes the key of the playback that produced it, so a staged
398
+ /// synchronous `.failed` that delivers AFTER a replacing load re-keyed
399
+ /// the entry still routes the arm of the load it answers (the mutable
400
+ /// entry's tag would misroute it to the replacement's arm).
401
+ pub const video_key_base: u64 = 0x5453_5649_0000_0000;
402
+
403
+ /// The engine key for a video load routed to `event_tag`'s arm.
404
+ pub fn videoKeyForTag(event_tag: u8) u64 {
405
+ return video_key_base | event_tag;
406
+ }
407
+
408
+ /// Engine-key namespace for pty sessions ("TSPT"): `base + table
409
+ /// index`, deterministic in issue order. Ptys share the keyed
410
+ /// families' one engine key space without ever colliding on a key.
411
+ pub const pty_key_base: u64 = 0x5453_5054_0000_0000;
412
+
230
413
  /// The spawn wire record's "no line routing" tag sentinel (mirrors
231
414
  /// rt.zig's `spawn_no_line_tag`).
232
415
  pub const spawn_no_line_tag: u8 = 0xFF;
@@ -361,6 +544,67 @@ pub fn TsCoreHost(comptime core: type) type {
361
544
  }
362
545
  };
363
546
 
547
+ /// The single video stream entry — the audio entry's exact
548
+ /// shape (one player is the whole engine surface). Non-retiring:
549
+ /// video_ctl `stop` closes it, a new video_load re-keys and
550
+ /// re-routes it in place.
551
+ const VideoEntry = struct {
552
+ used: bool = false,
553
+ key_len: usize = 0,
554
+ key: [max_wire_key_bytes]u8 = undefined,
555
+ event_tag: u8 = 0,
556
+ /// The load identity this entry's own accepted load minted
557
+ /// (`Effects.videoMintedToken`): the proof that the
558
+ /// playback on the single engine channel is still THIS
559
+ /// stream — a load the bridge never issued (a declarative
560
+ /// element's) may have replaced it, and the entry's verbs
561
+ /// must not mutate or cancel someone else's playback.
562
+ token: u64 = 0,
563
+
564
+ fn wireKey(entry: *const VideoEntry) []const u8 {
565
+ return entry.key[0..entry.key_len];
566
+ }
567
+ };
568
+
569
+ /// One in-flight image load, keyed by the app's own numeric
570
+ /// ImageId (which IS the engine key — the registry id and the
571
+ /// effect key are one value by design). Retires on its one
572
+ /// terminal.
573
+ const ImageEntry = struct {
574
+ used: bool = false,
575
+ id: u64 = 0,
576
+ event_tag: u8 = 0,
577
+ };
578
+
579
+ /// One open external-source channel, keyed by the app's own
580
+ /// numeric key (which IS the engine key, the image
581
+ /// convention). Non-retiring the spawn way: `data` events flow
582
+ /// through it across dispatches; the one `closed` (or a
583
+ /// refused open's `rejected`) terminal retires it. Sized to
584
+ /// the engine's channel table.
585
+ const ChannelEntry = struct {
586
+ used: bool = false,
587
+ key: u64 = 0,
588
+ event_tag: u8 = 0,
589
+ };
590
+
591
+ /// One live pty session — the stream entries' non-retiring
592
+ /// shape: "output" events route through it repeatedly across
593
+ /// dispatches, and only the one "exit" terminal retires it. The
594
+ /// table index IS the engine key (minus `pty_key_base`),
595
+ /// deterministic in issue order; sized to the engine's pty
596
+ /// table.
597
+ const PtyEntry = struct {
598
+ used: bool = false,
599
+ key_len: usize = 0,
600
+ key: [max_wire_key_bytes]u8 = undefined,
601
+ event_tag: u8 = 0,
602
+
603
+ fn wireKey(entry: *const PtyEntry) []const u8 {
604
+ return entry.key[0..entry.key_len];
605
+ }
606
+ };
607
+
364
608
  var model_root: *const Model = undefined;
365
609
  /// The platform caches directory for URL audio sources, set by
366
610
  /// the wiring (`TsUiApp`'s `audio_cache_dir`, or `setAudioCacheDir`
@@ -377,6 +621,17 @@ pub fn TsCoreHost(comptime core: type) type {
377
621
  var delays: [runtime_effects.max_effect_timers]DelayEntry = @splat(.{});
378
622
  var streams: [runtime_effects.max_effects]StreamEntry = @splat(.{});
379
623
  var audio_entry: AudioEntry = .{};
624
+ var video_entry: VideoEntry = .{};
625
+ var images: [runtime_effects.max_effects]ImageEntry = @splat(.{});
626
+ var channels: [runtime_effects.max_effect_channels]ChannelEntry = @splat(.{});
627
+ var ptys: [runtime_effects.max_effect_ptys]PtyEntry = @splat(.{});
628
+ /// The platform caches directory for URL image sources, the
629
+ /// audio cache dir's twin (`setImageCacheDir` / `TsUiApp`'s
630
+ /// `image_cache_dir`): bridge-side derivation of the
631
+ /// content-addressed cache path, never an env read in update.
632
+ var image_cache_dir_len: usize = 0;
633
+ var image_cache_dir_buf: [runtime_effects.max_effect_image_path_bytes]u8 = undefined;
634
+ var image_cache_path_buf: [runtime_effects.max_effect_image_path_bytes]u8 = undefined;
380
635
  var clip_write_counter: u32 = 0;
381
636
  /// Set by a result callback whose entry was dropped (replaced
382
637
  /// or wire-cancelled): the terminal is journal-visible but must
@@ -392,12 +647,25 @@ pub fn TsCoreHost(comptime core: type) type {
392
647
  /// after the issuing cycle's frame reset.
393
648
  const PendingNow = struct { tag: u8, ms: i64 };
394
649
 
395
- /// A spawn issue the bridge itself refused (duplicate live wire
396
- /// key — the one reject in the keyed-effect discipline),
397
- /// dispatched as the spawn's err arm with "rejected" after the
398
- /// issuing cycle's frame reset, at the same boundary `now`
399
- /// dispatches use.
400
- const max_rejects_per_cmd: usize = runtime_effects.max_effects;
650
+ // Bridge-refused dispatches (a spawn under a live wire key, an
651
+ // image load or channel open under a duplicate live id/key, an
652
+ // unrepresentable one, or a full bridge table) do NOT dispatch
653
+ // from the bridge's own boundary: each refusal builds its
654
+ // record's err/event arm Msg ("rejected", echoing the
655
+ // requested ImageId / channel key where the family has one —
656
+ // 0 when the wire value is not an exactly-carried positive
657
+ // integer, since there is no honest integer to echo) and
658
+ // stages it into the ENGINE's loop-side pending order at
659
+ // refusal time (`Effects.stageLoopMsg`). One seq-ordered
660
+ // stream is the ordering authority for every rejection, so a
661
+ // batch mixing bridge-refused and engine-refused records
662
+ // dispatches all of them in command-stream order at the next
663
+ // drain — see the result-ordering contract in the module doc.
664
+ // The staged Msgs are self-contained (static reason bytes and
665
+ // scalar echoes, never frame-arena slices) and regenerate
666
+ // deterministically under replay: the walk re-runs against the
667
+ // same table state, so record and replay stage identical
668
+ // sequences — `stageLoopMsg`'s two caller contracts.
401
669
 
402
670
  /// Install the core: reset the kernel and the bridge tables
403
671
  /// (deterministic re-init — the seam session replay relies on),
@@ -427,8 +695,13 @@ pub fn TsCoreHost(comptime core: type) type {
427
695
  delays = @splat(.{});
428
696
  streams = @splat(.{});
429
697
  audio_entry = .{};
698
+ video_entry = .{};
699
+ images = @splat(.{});
700
+ channels = @splat(.{});
701
+ ptys = @splat(.{});
430
702
  clip_write_counter = 0;
431
703
  audio_cache_dir_len = 0;
704
+ image_cache_dir_len = 0;
432
705
  swallow_next_dispatch = false;
433
706
  const initial = core.initialModel();
434
707
  model_root = core.commitModelRoot(if (comptime init_returns_cmd) initial.model else initial);
@@ -486,6 +759,33 @@ pub fn TsCoreHost(comptime core: type) type {
486
759
  return runtime_effects.audioCachePath(&audio_cache_path_buf, audioCacheDir(), url) catch "";
487
760
  }
488
761
 
762
+ /// Configure the caches directory image-cache derivation uses —
763
+ /// `setAudioCacheDir`'s twin, same wiring-time contract.
764
+ pub fn setImageCacheDir(dir: []const u8) void {
765
+ if (dir.len > image_cache_dir_buf.len) {
766
+ @panic("ts core host: the image cache directory path is longer than the engine's image path bound");
767
+ }
768
+ @memcpy(image_cache_dir_buf[0..dir.len], dir);
769
+ image_cache_dir_len = dir.len;
770
+ }
771
+
772
+ fn imageCacheDir() []const u8 {
773
+ return image_cache_dir_buf[0..image_cache_dir_len];
774
+ }
775
+
776
+ /// The cache path an image_load record loads under: the
777
+ /// record's own when it carries one; otherwise, for URL sources
778
+ /// with a configured caches directory, the engine's
779
+ /// conventional content-addressed path
780
+ /// (`<cache_dir>/images/<sha256[..16]>.<ext>` via
781
+ /// `imageCachePath`) — `effectiveAudioCachePath`'s twin, a
782
+ /// fixed function of the wire bytes so replay re-derives
783
+ /// identically.
784
+ fn effectiveImageCachePath(cache_path: []const u8, url: []const u8) []const u8 {
785
+ if (cache_path.len > 0 or url.len == 0 or image_cache_dir_len == 0) return cache_path;
786
+ return runtime_effects.imageCachePath(&image_cache_path_buf, imageCacheDir(), url) catch "";
787
+ }
788
+
489
789
  /// One full dispatch cycle for `msg`. The `TsUiApp` adapter
490
790
  /// wires this to `UiApp.Options.update_fx` (refreshing the
491
791
  /// app-held root from `model()` afterwards) so host events and
@@ -504,8 +804,14 @@ pub fn TsCoreHost(comptime core: type) type {
504
804
  /// Drain every queued effect completion into the core — the
505
805
  /// bridge-shaped mirror of `UiApp.drainEffects`' loop. Hosts
506
806
  /// that embed the core without a UiApp call this on wake/frame.
807
+ /// Bounded to the completions that existed at entry, exactly
808
+ /// like `UiApp.drainEffects`: a completion produced by a
809
+ /// dispatch inside this pass waits for the wake its producer
810
+ /// already nudged, keeping the session journal's event
811
+ /// boundaries causal (see `Effects.DrainBoundary`).
507
812
  pub fn drain(fx: *Fx) void {
508
- while (fx.takeMsg()) |msg| dispatch(fx, msg);
813
+ var boundary = fx.drainBoundary();
814
+ while (fx.takeMsgWithin(&boundary)) |msg| dispatch(fx, msg);
509
815
  }
510
816
 
511
817
  fn dispatchDepth(fx: *Fx, msg: Msg, depth: usize) void {
@@ -522,39 +828,33 @@ pub fn TsCoreHost(comptime core: type) type {
522
828
  }
523
829
  }
524
830
 
525
- /// The tail of every cycle: walk the command bytes, reconcile
831
+ /// The tail of every cycle: walk the command bytes (staging any
832
+ /// bridge-refused rejections into the engine's pending order as
833
+ /// it goes — see the rejection note above `init`), reconcile
526
834
  /// subscriptions against the NEW committed model, reset the
527
- /// frame arena, then run the cycle's `now` dispatches and
528
- /// bridge-refused spawn rejections (each a full cycle of its
529
- /// own, on the fresh frame — nows first, rejections after, in
530
- /// record order; deterministic under record and replay alike).
835
+ /// frame arena, then run the cycle's `now` dispatches (each a
836
+ /// full cycle of its own, on the fresh frame, in record order;
837
+ /// deterministic under record and replay alike).
531
838
  fn finishCycle(fx: *Fx, cmd: []const u8, depth: usize) void {
532
839
  var nows: [max_nows_per_cmd]PendingNow = undefined;
533
840
  var now_count: usize = 0;
534
- var rejects: [max_rejects_per_cmd]u8 = undefined;
535
- var reject_count: usize = 0;
536
- runCmd(fx, cmd, &nows, &now_count, &rejects, &reject_count);
841
+ runCmd(fx, cmd, &nows, &now_count);
537
842
  reconcileTimers(fx);
538
843
  core.rt.frameReset();
539
844
  for (nows[0..now_count]) |pending| {
540
845
  dispatchDepth(fx, msgFromTagNumber(pending.tag, @floatFromInt(pending.ms)), depth + 1);
541
846
  }
542
- for (rejects[0..reject_count]) |err_tag| {
543
- dispatchDepth(fx, msgFromTagBytes(err_tag, "rejected"), depth + 1);
544
- }
545
847
  }
546
848
 
547
849
  // ------------------------------------------------- command walk
548
850
 
549
- /// Walk one command value (v2 wire format; batch is plain
851
+ /// Walk one command value (v3 wire format; batch is plain
550
852
  /// concatenation, the empty slice is `Cmd.none`).
551
853
  fn runCmd(
552
854
  fx: *Fx,
553
855
  cmd: []const u8,
554
856
  nows: *[max_nows_per_cmd]PendingNow,
555
857
  now_count: *usize,
556
- rejects: *[max_rejects_per_cmd]u8,
557
- reject_count: *usize,
558
858
  ) void {
559
859
  var at: usize = 0;
560
860
  while (at < cmd.len) {
@@ -711,7 +1011,7 @@ pub fn TsCoreHost(comptime core: type) type {
711
1011
  var argv: [runtime_effects.max_effect_argv][]const u8 = undefined;
712
1012
  for (0..argc) |i| argv[i] = takeLongBytes(cmd, &at);
713
1013
  const stdin = takeLongBytes(cmd, &at);
714
- issueSpawn(fx, .{ .key = key, .line_tag = line_tag, .exit_tag = exit_tag, .err_tag = err_tag }, mode == 1, argv[0..argc], stdin, rejects, reject_count);
1014
+ issueSpawn(fx, .{ .key = key, .line_tag = line_tag, .exit_tag = exit_tag, .err_tag = err_tag }, mode == 1, argv[0..argc], stdin);
715
1015
  },
716
1016
  // audio_play [op][key_len][key][event_tag]
717
1017
  // [path_len u32 LE][path][url_len u32 LE][url]
@@ -754,6 +1054,169 @@ pub fn TsCoreHost(comptime core: type) type {
754
1054
  const value: f64 = @bitCast(std.mem.readInt(u64, value_bits[0..8], .little));
755
1055
  runAudioCtl(fx, key, verb, value);
756
1056
  },
1057
+ // window_show [op][label_len][label]
1058
+ 0x10 => {
1059
+ const label = takeShortBytes(cmd, &at);
1060
+ fx.showWindow(label);
1061
+ },
1062
+ // quit_app [op]
1063
+ 0x11 => fx.quitApp(),
1064
+ // image_load [op][id f64 LE][event_tag]
1065
+ // [path_len u32 LE][path][url_len u32 LE][url]
1066
+ // [cache_len u32 LE][cache][expected f64 LE]
1067
+ 0x12 => {
1068
+ const id_bits = takeBytes(cmd, &at, 8);
1069
+ const id_value: f64 = @bitCast(std.mem.readInt(u64, id_bits[0..8], .little));
1070
+ const event_tag = takeByte(cmd, &at);
1071
+ const image_path = takeLongBytes(cmd, &at);
1072
+ const url = takeLongBytes(cmd, &at);
1073
+ const cache_path = takeLongBytes(cmd, &at);
1074
+ const expected_bits = takeBytes(cmd, &at, 8);
1075
+ const expected: f64 = @bitCast(std.mem.readInt(u64, expected_bits[0..8], .little));
1076
+ issueImageLoad(fx, id_value, event_tag, image_path, url, cache_path, expected);
1077
+ },
1078
+ // image_cancel [op][id f64 LE]
1079
+ 0x13 => {
1080
+ const id_bits = takeBytes(cmd, &at, 8);
1081
+ const id_value: f64 = @bitCast(std.mem.readInt(u64, id_bits[0..8], .little));
1082
+ runImageCancel(fx, id_value);
1083
+ },
1084
+ // image_unregister [op][id f64 LE]
1085
+ 0x14 => {
1086
+ const id_bits = takeBytes(cmd, &at, 8);
1087
+ const id_value: f64 = @bitCast(std.mem.readInt(u64, id_bits[0..8], .little));
1088
+ runImageUnregister(fx, id_value);
1089
+ },
1090
+ // channel_open [op][key f64 LE][event_tag]
1091
+ 0x15 => {
1092
+ const key_bits = takeBytes(cmd, &at, 8);
1093
+ const key_value: f64 = @bitCast(std.mem.readInt(u64, key_bits[0..8], .little));
1094
+ const event_tag = takeByte(cmd, &at);
1095
+ issueChannelOpen(fx, key_value, event_tag);
1096
+ },
1097
+ // channel_close [op][key f64 LE]
1098
+ 0x16 => {
1099
+ const key_bits = takeBytes(cmd, &at, 8);
1100
+ const key_value: f64 = @bitCast(std.mem.readInt(u64, key_bits[0..8], .little));
1101
+ runChannelClose(fx, key_value);
1102
+ },
1103
+ // video_load [op][key_len][key][event_tag][surface f64 LE]
1104
+ // [path_len u32 LE][path][url_len u32 LE][url]
1105
+ // [flags u8]
1106
+ 0x17 => {
1107
+ const key = takeShortBytes(cmd, &at);
1108
+ const event_tag = takeByte(cmd, &at);
1109
+ const surface_bits = takeBytes(cmd, &at, 8);
1110
+ const surface: f64 = @bitCast(std.mem.readInt(u64, surface_bits[0..8], .little));
1111
+ const video_path = takeLongBytes(cmd, &at);
1112
+ const url = takeLongBytes(cmd, &at);
1113
+ const flags = takeByte(cmd, &at);
1114
+ const options: Fx.LoadVideoOptions = .{
1115
+ // The tag rides the key's low byte so every
1116
+ // event routes the arm of the load that
1117
+ // produced it (see `videoKeyForTag`).
1118
+ .key = videoKeyForTag(event_tag),
1119
+ // The wire carries the app's number; a surface
1120
+ // that is not an exactly-carried positive
1121
+ // integer (0, negatives, fractions, 2^53 and
1122
+ // past — the image id bound) reaches the engine
1123
+ // as 0, which the validation refuses with one
1124
+ // `.rejected` event — never silent.
1125
+ .surface = if (surface >= 1 and surface < 9007199254740992.0 and @floor(surface) == surface)
1126
+ @intFromFloat(surface)
1127
+ else
1128
+ 0,
1129
+ .path = video_path,
1130
+ .url = url,
1131
+ .autoplay = (flags & 0x01) != 0,
1132
+ .loop = (flags & 0x02) != 0,
1133
+ .muted = (flags & 0x04) != 0,
1134
+ .on_event = videoEventMsg,
1135
+ };
1136
+ // A load the engine's own deterministic gates
1137
+ // would refuse must not commit the routing
1138
+ // entry: the engine keeps the CURRENT playback
1139
+ // on a rejected load, so re-keying first would
1140
+ // route the surviving stream's events and verbs
1141
+ // through the refused load's key and arm. Stage
1142
+ // the rejection to the refused arm directly
1143
+ // (the channel-admission precedent) and leave
1144
+ // the entry — and the engine — untouched.
1145
+ if (Fx.videoLoadRejected(options)) {
1146
+ fx.stageLoopMsg(msgFromTagVideo(event_tag, .{
1147
+ .key = videoKeyForTag(event_tag),
1148
+ .kind = .rejected,
1149
+ }));
1150
+ } else {
1151
+ // One player is the whole surface: an
1152
+ // accepted load re-keys and re-routes the
1153
+ // single entry in place, exactly as the
1154
+ // engine replaces its channel.
1155
+ video_entry.used = true;
1156
+ video_entry.key_len = key.len;
1157
+ @memcpy(video_entry.key[0..key.len], key);
1158
+ video_entry.event_tag = event_tag;
1159
+ fx.loadVideo(options);
1160
+ // The identity this load minted (valid even
1161
+ // when a synchronous refusal already reset
1162
+ // the channel): the entry's verbs prove
1163
+ // ownership against it.
1164
+ video_entry.token = fx.videoMintedToken();
1165
+ }
1166
+ },
1167
+ // video_ctl [op][key_len][key][verb u8][value f64 LE]
1168
+ 0x18 => {
1169
+ const key = takeShortBytes(cmd, &at);
1170
+ const verb = takeByte(cmd, &at);
1171
+ const value_bits = takeBytes(cmd, &at, 8);
1172
+ const value: f64 = @bitCast(std.mem.readInt(u64, value_bits[0..8], .little));
1173
+ runVideoCtl(fx, key, verb, value);
1174
+ },
1175
+ // pty_spawn [op][key_len][key][event_tag]
1176
+ // [cols f64 LE][rows f64 LE][term_len][term]
1177
+ // [argc u8]([arg_len u32 LE][arg])*
1178
+ 0x19 => {
1179
+ const key = takeShortBytes(cmd, &at);
1180
+ const event_tag = takeByte(cmd, &at);
1181
+ const cols_bits = takeBytes(cmd, &at, 8);
1182
+ const cols: f64 = @bitCast(std.mem.readInt(u64, cols_bits[0..8], .little));
1183
+ const rows_bits = takeBytes(cmd, &at, 8);
1184
+ const rows: f64 = @bitCast(std.mem.readInt(u64, rows_bits[0..8], .little));
1185
+ const term = takeShortBytes(cmd, &at);
1186
+ const argc: usize = takeByte(cmd, &at);
1187
+ if (argc == 0 or argc > runtime_effects.max_effect_argv) {
1188
+ @panic("ts core host: a pty_spawn wire record carries more argv elements than the engine accepts - the transpiler's own bound should have stopped this build");
1189
+ }
1190
+ var argv: [runtime_effects.max_effect_argv][]const u8 = undefined;
1191
+ for (0..argc) |i| argv[i] = takeLongBytes(cmd, &at);
1192
+ issuePtySpawn(fx, key, event_tag, cols, rows, term, argv[0..argc]);
1193
+ },
1194
+ // pty_write [op][key_len][key][bytes_len u32 LE][bytes]
1195
+ 0x1A => {
1196
+ const key = takeShortBytes(cmd, &at);
1197
+ const bytes = takeLongBytes(cmd, &at);
1198
+ // TS `Cmd.ptyWrite` is fire-and-forget: a refusal
1199
+ // counts into the exit's dropped_writes, so the
1200
+ // acceptance result is ignored here.
1201
+ if (findPty(key)) |index| _ = fx.ptyWrite(pty_key_base + index, bytes);
1202
+ },
1203
+ // pty_resize [op][key_len][key][cols f64 LE][rows f64 LE]
1204
+ 0x1B => {
1205
+ const key = takeShortBytes(cmd, &at);
1206
+ const cols_bits = takeBytes(cmd, &at, 8);
1207
+ const cols: f64 = @bitCast(std.mem.readInt(u64, cols_bits[0..8], .little));
1208
+ const rows_bits = takeBytes(cmd, &at, 8);
1209
+ const rows: f64 = @bitCast(std.mem.readInt(u64, rows_bits[0..8], .little));
1210
+ runPtyResize(fx, key, cols, rows);
1211
+ },
1212
+ // pty_kill [op][key_len][key]
1213
+ 0x1C => {
1214
+ const key = takeShortBytes(cmd, &at);
1215
+ // LOUD: the engine ends the child and the session's
1216
+ // one "cancelled" exit routes its own event arm,
1217
+ // retiring the entry in ptyEventMsg.
1218
+ if (findPty(key)) |index| fx.ptyKill(pty_key_base + index);
1219
+ },
757
1220
  else => @panic("ts core host: unknown command wire record - the core and this runtime disagree on cmd_format_version"),
758
1221
  }
759
1222
  }
@@ -886,15 +1349,9 @@ pub fn TsCoreHost(comptime core: type) type {
886
1349
  collect: bool,
887
1350
  argv: []const []const u8,
888
1351
  stdin: []const u8,
889
- rejects: *[max_rejects_per_cmd]u8,
890
- reject_count: *usize,
891
1352
  ) void {
892
1353
  if (head.key.len > 0 and findStream(head.key) != null) {
893
- if (reject_count.* >= max_rejects_per_cmd) {
894
- @panic("ts core host: one command value carries more rejected named ops than the effect table holds");
895
- }
896
- rejects[reject_count.*] = head.err_tag;
897
- reject_count.* += 1;
1354
+ fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "rejected"));
898
1355
  return;
899
1356
  }
900
1357
  const index = freeStreamIndex() orelse
@@ -1016,6 +1473,460 @@ pub fn TsCoreHost(comptime core: type) type {
1016
1473
  return msgFromTagAudio(audio_entry.event_tag, event);
1017
1474
  }
1018
1475
 
1476
+ // ------------------------------------------------- video stream
1477
+
1478
+ /// The video_ctl record: drive the single playback channel,
1479
+ /// gated by the wire key — a verb aimed at a key that is not
1480
+ /// the open stream no-ops (its playback is already gone), the
1481
+ /// same idle no-op the engine's own verbs keep. `stop` closes
1482
+ /// the stream: the entry retires and later platform stragglers
1483
+ /// are the engine's to swallow.
1484
+ fn runVideoCtl(fx: *Fx, key: []const u8, verb: u8, value: f64) void {
1485
+ if (!video_entry.used or !std.mem.eql(u8, video_entry.wireKey(), key)) return;
1486
+ if (verb == 2) {
1487
+ // `Cmd.videoStop` is the stream's CANCEL: the engine
1488
+ // drops THIS stream's staged answers (token-scoped —
1489
+ // even the synchronous terminal of a batch that
1490
+ // loaded, failed, and stopped in one dispatch, and
1491
+ // even when a later load reuses the same event tag),
1492
+ // and stops the player only while the channel still
1493
+ // plays this stream — a playback some other caller
1494
+ // loaded since (a declarative element) survives
1495
+ // untouched. A REPLACED predecessor sharing this arm
1496
+ // keeps its own owed terminal; only stop cancels.
1497
+ const token = video_entry.token;
1498
+ video_entry.used = false;
1499
+ fx.stopVideoCancel(token);
1500
+ return;
1501
+ }
1502
+ // The wire key names the bridge's entry, but the single
1503
+ // engine player may since have been replaced by a load the
1504
+ // bridge never issued (a declarative <video>): transport
1505
+ // verbs act only while the entry's own stream is the
1506
+ // playback on the channel — anything else is a stale key
1507
+ // and no-ops, the idle rule. VOLUME is the one exception
1508
+ // with the channel IDLE: it is a remembered preference the
1509
+ // next load re-applies (a failed load's handler routinely
1510
+ // sets it before retrying), and with nobody's playback on
1511
+ // the channel there is nothing to protect — a FOREIGN live
1512
+ // playback still gates it, its volume is not this key's to
1513
+ // move.
1514
+ if (fx.videoOwnerToken() != video_entry.token) {
1515
+ if (verb == 4 and fx.videoOwnerToken() == 0) {
1516
+ fx.setVideoVolume(@floatCast(value));
1517
+ }
1518
+ return;
1519
+ }
1520
+ switch (verb) {
1521
+ 0 => fx.playVideo(),
1522
+ 1 => fx.pauseVideo(),
1523
+ // The wire carries the app's f64. In-window offsets pass
1524
+ // through (the engine clamps to the duration itself); a
1525
+ // FINITE offset PAST the exact-integer window saturates
1526
+ // just below it — still beyond every real duration, so
1527
+ // the engine's clamp lands it at the end, exactly what
1528
+ // an oversized forward seek asks for. Non-finite values
1529
+ // and negatives are not millisecond offsets at all (the
1530
+ // literal validation rejects them) and seek to 0.
1531
+ 3 => fx.seekVideo(if (value >= 0 and value <= 9007199254740992.0)
1532
+ @intFromFloat(value)
1533
+ else if (value > 9007199254740992.0 and std.math.isFinite(value))
1534
+ runtime_effects.max_effect_video_scalar_exclusive - 1
1535
+ else
1536
+ 0),
1537
+ // The engine clamps volume to 0..1 (NaN clamps to the
1538
+ // bound arithmetic's result deterministically).
1539
+ 4 => fx.setVideoVolume(@floatCast(value)),
1540
+ 5 => fx.setVideoMuted(value != 0),
1541
+ 6 => fx.setVideoLoop(value != 0),
1542
+ else => @panic("ts core host: unknown video_ctl verb wire value - the core and this runtime disagree on cmd_format_version"),
1543
+ }
1544
+ }
1545
+
1546
+ /// `VideoMsgFn` for the video stream: every playback event
1547
+ /// routes by the TAG its own key carries (`videoKeyForTag`) —
1548
+ /// never the mutable entry's tag, which a replacing load in the
1549
+ /// same batch may already have re-pointed at another arm while
1550
+ /// the replaced playback's staged terminal was still awaiting
1551
+ /// its drain (a REPLACED stream still speaks its terminal).
1552
+ /// Stopped streams never reach here at all: video_ctl `stop`
1553
+ /// cancels the key's staged answers inside the engine
1554
+ /// (`stopVideoCancel`) and the engine swallows its own
1555
+ /// post-stop stragglers, so every event arriving carries a
1556
+ /// live stream's tag. The entry never retires here —
1557
+ /// `completed`/`failed` streams may still speak (the app often
1558
+ /// starts the next clip from `completed`), and video_ctl
1559
+ /// `stop` is the explicit close.
1560
+ fn videoEventMsg(event: runtime_effects.EffectVideo) Msg {
1561
+ return msgFromTagVideo(@intCast(event.key & 0xFF), event);
1562
+ }
1563
+
1564
+ /// Issue one image load. The keyed-effect discipline here is
1565
+ /// the SPAWN exception, by the same reasoning: one load per id
1566
+ /// at a time, never replaced implicitly — a duplicate LIVE id
1567
+ /// rejects the new load (event arm, state "rejected", staged
1568
+ /// into the engine's pending order and delivered at the next
1569
+ /// drain), and so do an id the f64 wire cannot
1570
+ /// honestly carry into the u64 registry (0, negatives,
1571
+ /// fractions, 2^53 and past) and a 17th in-flight load (the table
1572
+ /// mirrors the engine's max_effects slots, whose own exhaustion
1573
+ /// answer is the same rejected result). Everything else the
1574
+ /// engine refuses dynamically (a full registry, a bad source)
1575
+ /// comes back through the entry's own event arm — never silent.
1576
+ fn issueImageLoad(
1577
+ fx: *Fx,
1578
+ id_value: f64,
1579
+ event_tag: u8,
1580
+ image_path: []const u8,
1581
+ url: []const u8,
1582
+ cache_path: []const u8,
1583
+ expected: f64,
1584
+ ) void {
1585
+ // Strictly BELOW 2^53 (the SDK contract): at 2^53 the f64
1586
+ // grid steps by 2, so 2^53 is the first value that aliases
1587
+ // a neighbor (2^53 + 1) on the wire — the bridge rejects it
1588
+ // rather than guess which integer the app meant. 2^53 - 1
1589
+ // is the last id every tier carries exactly.
1590
+ const representable = std.math.isFinite(id_value) and
1591
+ id_value >= 1 and id_value < 9007199254740992.0 and
1592
+ @floor(id_value) == id_value;
1593
+ if (!representable) {
1594
+ fx.stageLoopMsg(msgFromTagImage(event_tag, .{ .id = 0, .outcome = .rejected }));
1595
+ return;
1596
+ }
1597
+ const id: u64 = @intFromFloat(id_value);
1598
+ if (findImage(id) != null) {
1599
+ fx.stageLoopMsg(msgFromTagImage(event_tag, .{ .id = id, .outcome = .rejected }));
1600
+ return;
1601
+ }
1602
+ const index = freeImageIndex() orelse {
1603
+ // All 16 entries hold live loads — one gallery screen's
1604
+ // Cmd.batch reaches this. The engine answers its own
1605
+ // slot exhaustion with a dynamic `.rejected` result, and
1606
+ // the audio channel's exhaustion story is a quiet
1607
+ // in-place replace; a full bridge table speaks the same
1608
+ // vocabulary: exactly one rejected result through the
1609
+ // event arm, never a crash — however many loads one
1610
+ // batch stages against it.
1611
+ fx.stageLoopMsg(msgFromTagImage(event_tag, .{ .id = id, .outcome = .rejected }));
1612
+ return;
1613
+ };
1614
+ const entry = &images[index];
1615
+ entry.used = true;
1616
+ entry.id = id;
1617
+ entry.event_tag = event_tag;
1618
+ fx.loadImage(.{
1619
+ .id = id,
1620
+ .path = image_path,
1621
+ .url = url,
1622
+ .cache_path = effectiveImageCachePath(cache_path, url),
1623
+ // The wire carries the app's number; anything that is
1624
+ // not a representable WHOLE byte count — fractional,
1625
+ // out of range, NaN — means "unknown size" (0), the
1626
+ // engine's own default. The integer clause matters:
1627
+ // @intFromFloat would truncate 1.5 to 1, and the cache
1628
+ // would then verify downloads against a size the app
1629
+ // never declared — re-fetching on every launch. The
1630
+ // bound is strictly BELOW 2^53, the id gate's: 2^53 is
1631
+ // the first f64 that aliases a neighbor (2^53 + 1
1632
+ // arrives as the same wire value), so there is no one
1633
+ // honest count to install — it maps to "unknown" with
1634
+ // the fractionals rather than becoming a verification
1635
+ // size every real download misses. 0 is the honest
1636
+ // mapping; the emitter already stops the literal
1637
+ // spellings (NS1030).
1638
+ .expected_bytes = if (expected >= 1 and expected < 9007199254740992.0 and @floor(expected) == expected)
1639
+ @intFromFloat(expected)
1640
+ else
1641
+ 0,
1642
+ .on_result = imageResultMsg,
1643
+ });
1644
+ }
1645
+
1646
+ fn findImage(id: u64) ?usize {
1647
+ for (&images, 0..) |*entry, index| {
1648
+ if (entry.used and entry.id == id) return index;
1649
+ }
1650
+ return null;
1651
+ }
1652
+
1653
+ fn freeImageIndex() ?usize {
1654
+ for (&images, 0..) |*entry, index| {
1655
+ if (!entry.used) return index;
1656
+ }
1657
+ return null;
1658
+ }
1659
+
1660
+ /// The image_cancel record: end the in-flight load under the
1661
+ /// id, if any, LOUDLY — the engine delivers the load's one
1662
+ /// terminal as `.cancelled`, which routes the entry's own event
1663
+ /// arm through `imageResultMsg` and retires the entry (freeing
1664
+ /// the id for a fresh load), the spawn cancel discipline. An id
1665
+ /// naming no live entry — or one the wire cannot carry exactly,
1666
+ /// which no load could ever park under — is a no-op, the same
1667
+ /// idle no-op audio_ctl keeps: whatever it aimed at is already
1668
+ /// gone. The raw id IS the engine key (image loads never mint a
1669
+ /// bridge namespace), and every bridge key base sits above 2^53,
1670
+ /// so the cancel can never reach another table's slot.
1671
+ fn runImageCancel(fx: *Fx, id_value: f64) void {
1672
+ const representable = std.math.isFinite(id_value) and
1673
+ id_value >= 1 and id_value < 9007199254740992.0 and
1674
+ @floor(id_value) == id_value;
1675
+ if (!representable) return;
1676
+ const id: u64 = @intFromFloat(id_value);
1677
+ if (findImage(id) == null) return;
1678
+ fx.cancel(id);
1679
+ }
1680
+
1681
+ /// The image_unregister record: free the registry slot under
1682
+ /// the id — direct registry surgery, registration's synchronous
1683
+ /// discipline in reverse (no terminal, no Msg; the engine call
1684
+ /// answers a bool the wire has no channel to carry, and a miss
1685
+ /// means whatever it aimed at is already gone — image_cancel's
1686
+ /// idle no-op). The bridge's load table is deliberately NOT
1687
+ /// consulted: unregister targets the REGISTRY, and a load in
1688
+ /// flight under the id is not a registry occupant — its
1689
+ /// terminal registers as usual, re-occupying the id, so an app
1690
+ /// that wants the slot to STAY free cancels the load first. An
1691
+ /// id the wire cannot carry exactly could never have been
1692
+ /// registered through this bridge, so it no-ops the same way.
1693
+ fn runImageUnregister(fx: *Fx, id_value: f64) void {
1694
+ const representable = std.math.isFinite(id_value) and
1695
+ id_value >= 1 and id_value < 9007199254740992.0 and
1696
+ @floor(id_value) == id_value;
1697
+ if (!representable) return;
1698
+ const id: u64 = @intFromFloat(id_value);
1699
+ _ = fx.unregisterImage(id);
1700
+ }
1701
+
1702
+ /// `ImageMsgFn` for image loads: the ONE terminal routes the
1703
+ /// entry's event arm and retires the entry.
1704
+ fn imageResultMsg(result: runtime_effects.EffectImageResult) Msg {
1705
+ const index = findImage(result.id) orelse
1706
+ @panic("ts core host: an image result arrived with no open bridge entry");
1707
+ const entry = &images[index];
1708
+ entry.used = false;
1709
+ return msgFromTagImage(entry.event_tag, result);
1710
+ }
1711
+
1712
+ /// Open one external-source channel. The keyed-effect
1713
+ /// discipline here is the spawn/image exception, by the same
1714
+ /// reasoning: one channel per key at a time, never replaced
1715
+ /// implicitly — a duplicate LIVE key rejects the new open
1716
+ /// (event arm, state "rejected", staged into the engine's
1717
+ /// pending order and delivered at the next drain),
1718
+ /// and so do a key the f64 wire cannot honestly carry into the
1719
+ /// u64 engine (0, negatives, fractions, 2^53 and past — the
1720
+ /// image id gate) and a bridge table already holding
1721
+ /// `max_effect_channels` live channels. Everything else the
1722
+ /// engine refuses dynamically (a key occupied by another
1723
+ /// family, the engine's own table) comes back through the
1724
+ /// entry's event arm as its `.rejected` terminal — never
1725
+ /// silent. Posting is native-side API: embedders resolve
1726
+ /// `Effects.channelHandle(key)` and feed from their own
1727
+ /// threads; this bridge only opens, closes, and routes.
1728
+ fn issueChannelOpen(
1729
+ fx: *Fx,
1730
+ key_value: f64,
1731
+ event_tag: u8,
1732
+ ) void {
1733
+ const representable = std.math.isFinite(key_value) and
1734
+ key_value >= 1 and key_value < 9007199254740992.0 and
1735
+ @floor(key_value) == key_value;
1736
+ if (!representable) {
1737
+ fx.stageLoopMsg(msgFromTagChannel(event_tag, .{ .key = 0, .kind = .rejected }));
1738
+ return;
1739
+ }
1740
+ const key: u64 = @intFromFloat(key_value);
1741
+ if (findChannel(key) != null) {
1742
+ fx.stageLoopMsg(msgFromTagChannel(event_tag, .{ .key = key, .kind = .rejected }));
1743
+ return;
1744
+ }
1745
+ const index = freeChannelIndex() orelse {
1746
+ fx.stageLoopMsg(msgFromTagChannel(event_tag, .{ .key = key, .kind = .rejected }));
1747
+ return;
1748
+ };
1749
+ const entry = &channels[index];
1750
+ entry.used = true;
1751
+ entry.key = key;
1752
+ entry.event_tag = event_tag;
1753
+ _ = fx.openChannel(.{
1754
+ .key = key,
1755
+ .on_event = channelEventMsg,
1756
+ });
1757
+ }
1758
+
1759
+ fn findChannel(key: u64) ?usize {
1760
+ for (&channels, 0..) |*entry, index| {
1761
+ if (entry.used and entry.key == key) return index;
1762
+ }
1763
+ return null;
1764
+ }
1765
+
1766
+ fn freeChannelIndex() ?usize {
1767
+ for (&channels, 0..) |*entry, index| {
1768
+ if (!entry.used) return index;
1769
+ }
1770
+ return null;
1771
+ }
1772
+
1773
+ /// The channel_close record: close the live channel under the
1774
+ /// key, if any — the engine flushes staged posts, delivers the
1775
+ /// one `.closed` terminal through `channelEventMsg` (which
1776
+ /// retires the entry), and frees the key. A key naming no live
1777
+ /// entry — or one the wire cannot carry exactly, which no
1778
+ /// channel could ever open under — is a no-op, audio_ctl's
1779
+ /// idle rule. The raw key IS the engine key (the image
1780
+ /// convention; every bridge key base sits above 2^53, so this
1781
+ /// can never reach another table's slot).
1782
+ fn runChannelClose(fx: *Fx, key_value: f64) void {
1783
+ const representable = std.math.isFinite(key_value) and
1784
+ key_value >= 1 and key_value < 9007199254740992.0 and
1785
+ @floor(key_value) == key_value;
1786
+ if (!representable) return;
1787
+ const key: u64 = @intFromFloat(key_value);
1788
+ if (findChannel(key) == null) return;
1789
+ fx.closeChannel(key);
1790
+ }
1791
+
1792
+ /// `ChannelMsgFn` for external-source channels: every event
1793
+ /// routes the entry's event arm; the `closed` and `rejected`
1794
+ /// terminals retire the entry (freeing the key for a fresh
1795
+ /// open), `data` events keep it live — the spawn stream shape.
1796
+ fn channelEventMsg(event: runtime_effects.EffectChannelEvent) Msg {
1797
+ const index = findChannel(event.key) orelse
1798
+ @panic("ts core host: a channel event arrived with no open bridge entry");
1799
+ const entry = &channels[index];
1800
+ if (event.kind != .data) entry.used = false;
1801
+ return msgFromTagChannel(entry.event_tag, event);
1802
+ }
1803
+
1804
+ // -------------------------------------------------- pty sessions
1805
+
1806
+ /// Open a pty session: claim a non-retiring entry (the spawn
1807
+ /// exception, by the same reasoning — a live wire key REJECTS
1808
+ /// the new spawn, because a running terminal's child is a
1809
+ /// running subprocess and is never killed implicitly; kill it
1810
+ /// first) and hand the request to the engine. Everything
1811
+ /// dynamic the engine refuses (argv over the block bound, a
1812
+ /// zero grid, a full engine table) comes back as one
1813
+ /// "rejected" exit through the entry's own event arm — never
1814
+ /// silent — and a transport that could not start as one
1815
+ /// "spawn_failed".
1816
+ fn issuePtySpawn(
1817
+ fx: *Fx,
1818
+ key: []const u8,
1819
+ event_tag: u8,
1820
+ cols: f64,
1821
+ rows: f64,
1822
+ term: []const u8,
1823
+ argv: []const []const u8,
1824
+ ) void {
1825
+ if (key.len > 0 and findPty(key) != null) {
1826
+ // The rejection is STAGED (delivered a later frame), so its
1827
+ // key must be self-contained: the wire key points into this
1828
+ // dispatch's command buffer, gone by delivery, so intern
1829
+ // it in the engine's instance-lived staged-key store and
1830
+ // reference that. The app's key rides the refusal,
1831
+ // correlating it with its command — and the interned
1832
+ // slice stays valid even committed into the model.
1833
+ fx.stageLoopMsg(msgFromTagPty(event_tag, fx.stageLoopKey(key), true, .{ .key = 0, .kind = .exit, .reason = .rejected }));
1834
+ return;
1835
+ }
1836
+ const index = freePtyIndex() orelse {
1837
+ // The bridge table mirrors the engine's pty table, whose
1838
+ // own exhaustion answer is the same rejected exit — one
1839
+ // vocabulary for every refusal, never a crash. Staged, so
1840
+ // the requested key rides the engine's interned
1841
+ // instance-lived staged-key store, not the frame arena.
1842
+ fx.stageLoopMsg(msgFromTagPty(event_tag, fx.stageLoopKey(key), true, .{ .key = 0, .kind = .exit, .reason = .rejected }));
1843
+ return;
1844
+ };
1845
+ const entry = &ptys[index];
1846
+ entry.used = true;
1847
+ entry.key_len = key.len;
1848
+ @memcpy(entry.key[0..key.len], key);
1849
+ entry.event_tag = event_tag;
1850
+ if (term.len == 0) {
1851
+ // Wire "" = "the engine's default TERM" — the record
1852
+ // never bakes the default in (the fetch-timeout rule).
1853
+ fx.ptySpawn(.{
1854
+ .key = pty_key_base + index,
1855
+ .argv = argv,
1856
+ .cols = ptyDimension(cols),
1857
+ .rows = ptyDimension(rows),
1858
+ .on_event = ptyEventMsg,
1859
+ });
1860
+ } else {
1861
+ fx.ptySpawn(.{
1862
+ .key = pty_key_base + index,
1863
+ .argv = argv,
1864
+ .cols = ptyDimension(cols),
1865
+ .rows = ptyDimension(rows),
1866
+ .term = term,
1867
+ .on_event = ptyEventMsg,
1868
+ });
1869
+ }
1870
+ }
1871
+
1872
+ /// The wire carries the app's f64; the transport's grid is u16.
1873
+ /// Anything that is not a whole dimension in 1..65535 maps to
1874
+ /// 0, which `ptySpawn` answers with one deterministic
1875
+ /// "rejected" exit — loud, never a truncated guess (the emitter
1876
+ /// already stops the literal spellings, NS1030).
1877
+ fn ptyDimension(value: f64) u16 {
1878
+ if (!(std.math.isFinite(value) and value >= 1 and value <= 65535 and @floor(value) == value)) return 0;
1879
+ return @intFromFloat(value);
1880
+ }
1881
+
1882
+ /// The pty_resize record: push a new grid to the live session
1883
+ /// under the wire key, if any — fire-and-forget. A key naming
1884
+ /// no session, or a grid value the u16 transport cannot carry
1885
+ /// exactly, is a no-op (there is no honest grid to push, and
1886
+ /// the engine's clamp would otherwise shrink the child to a
1887
+ /// 1x1 the app never asked for).
1888
+ fn runPtyResize(fx: *Fx, key: []const u8, cols: f64, rows: f64) void {
1889
+ const index = findPty(key) orelse return;
1890
+ const c = ptyDimension(cols);
1891
+ const r = ptyDimension(rows);
1892
+ if (c == 0 or r == 0) return;
1893
+ fx.ptyResize(pty_key_base + index, c, r);
1894
+ }
1895
+
1896
+ fn findPty(key: []const u8) ?usize {
1897
+ if (key.len == 0) return null;
1898
+ for (&ptys, 0..) |*entry, index| {
1899
+ if (entry.used and std.mem.eql(u8, entry.wireKey(), key)) return index;
1900
+ }
1901
+ return null;
1902
+ }
1903
+
1904
+ fn freePtyIndex() ?usize {
1905
+ for (&ptys, 0..) |*entry, index| {
1906
+ if (!entry.used) return index;
1907
+ }
1908
+ return null;
1909
+ }
1910
+
1911
+ /// `PtyMsgFn` for pty sessions: every event routes the entry's
1912
+ /// event arm; the one "exit" terminal retires the entry
1913
+ /// (freeing the wire key for a fresh session), "output"
1914
+ /// batches keep it live — the spawn stream shape.
1915
+ fn ptyEventMsg(event: runtime_effects.EffectPtyEvent) Msg {
1916
+ if (event.key < pty_key_base) {
1917
+ @panic("ts core host: a pty event arrived outside the bridge's pty key namespace");
1918
+ }
1919
+ const index = event.key - pty_key_base;
1920
+ if (index >= ptys.len or !ptys[index].used) {
1921
+ @panic("ts core host: a pty event arrived for a session the bridge is not tracking");
1922
+ }
1923
+ const entry = &ptys[index];
1924
+ const wire_key = entry.wireKey();
1925
+ if (event.kind == .exit) entry.used = false;
1926
+ // Live delivery is same-frame, so the key rides the frame arena.
1927
+ return msgFromTagPty(entry.event_tag, wire_key, false, event);
1928
+ }
1929
+
1019
1930
  /// The wire `cancel` record: first match wins across the four
1020
1931
  /// keyed tables — requests (silent drop), named engine ops
1021
1932
  /// (silent drop: the entry is marked dropped, the engine's
@@ -1322,6 +2233,25 @@ pub fn TsCoreHost(comptime core: type) type {
1322
2233
  @panic("ts core host: a routed result names a Msg tag outside the union");
1323
2234
  }
1324
2235
 
2236
+ /// `msgFromTagBytes` for STATIC payloads (the staged rejection
2237
+ /// Msgs' "rejected"): no frame-arena copy, because a staged
2238
+ /// Msg outlives the issuing cycle's frame reset — it is held
2239
+ /// in the engine's pending order until the next drain, so its
2240
+ /// payload must be self-contained (`stageLoopMsg`'s contract).
2241
+ /// The commit walkers keep non-frame pointers as-is, and a
2242
+ /// static string's lifetime is the program's.
2243
+ fn msgFromTagStaticBytes(tag: u8, comptime bytes: []const u8) Msg {
2244
+ inline for (msg_arms, 0..) |arm, index| {
2245
+ if (tag == index) {
2246
+ if (comptime arm.type == []const u8) {
2247
+ return @unionInit(Msg, arm.name, bytes);
2248
+ }
2249
+ @panic("ts core host: a routed result targets Msg arm '" ++ arm.name ++ "', whose payload is not bytes");
2250
+ }
2251
+ }
2252
+ @panic("ts core host: a routed result names a Msg tag outside the union");
2253
+ }
2254
+
1325
2255
  /// Build the payload-less Msg arm at index `tag` (write_file's
1326
2256
  /// ok route — success carries nothing).
1327
2257
  fn msgFromTagVoid(tag: u8) Msg {
@@ -1343,7 +2273,7 @@ pub fn TsCoreHost(comptime core: type) type {
1343
2273
  /// transpiler validates the shape, so field names stay the
1344
2274
  /// app's). The bytes copy into the core's frame arena like
1345
2275
  /// every routed payload; the number widens into its field the
1346
- /// way the subset's number model classes it (i64 or f64).
2276
+ /// way the subset's number model classes it (i64, u64, or f64).
1347
2277
  fn msgFromTagNumberBytes(comptime what: []const u8, comptime shape: []const u8, tag: u8, number: anytype, bytes: []const u8) Msg {
1348
2278
  inline for (msg_arms, 0..) |arm, index| {
1349
2279
  if (tag == index) {
@@ -1356,7 +2286,7 @@ pub fn TsCoreHost(comptime core: type) type {
1356
2286
  var number_fields = 0;
1357
2287
  for (fields) |f| {
1358
2288
  if (f.type == []const u8) bytes_fields += 1;
1359
- if (f.type == i64 or f.type == f64) number_fields += 1;
2289
+ if (f.type == i64 or f.type == u64 or f.type == f64) number_fields += 1;
1360
2290
  }
1361
2291
  break :blk bytes_fields == 1 and number_fields == 1;
1362
2292
  };
@@ -1370,6 +2300,17 @@ pub fn TsCoreHost(comptime core: type) type {
1370
2300
  } else if (comptime f.type == f64) {
1371
2301
  @field(payload, f.name) = @floatFromInt(number);
1372
2302
  } else {
2303
+ // Exit codes carry -1 sentinels by
2304
+ // contract: a negative host number
2305
+ // into an unsigned field has no
2306
+ // honest value, so the crossing
2307
+ // teaches instead of faulting in
2308
+ // the cast.
2309
+ if (comptime f.type == u64) {
2310
+ if (number < 0) {
2311
+ @panic("ts core host: a negative number reached the u64-classed field '" ++ f.name ++ "' of Msg arm '" ++ arm.name ++ "' — the unsigned class cannot carry it; declare the field i64 or f64");
2312
+ }
2313
+ }
1373
2314
  @field(payload, f.name) = @intCast(number);
1374
2315
  }
1375
2316
  }
@@ -1397,7 +2338,7 @@ pub fn TsCoreHost(comptime core: type) type {
1397
2338
  if (std.mem.eql(u8, f.name, "state")) {
1398
2339
  if (@typeInfo(f.type) != .@"enum") ok = false;
1399
2340
  } else if (std.mem.eql(u8, f.name, "positionMs") or std.mem.eql(u8, f.name, "durationMs")) {
1400
- if (f.type != i64 and f.type != f64) ok = false;
2341
+ if (f.type != i64 and f.type != u64 and f.type != f64) ok = false;
1401
2342
  } else if (std.mem.eql(u8, f.name, "playing") or std.mem.eql(u8, f.name, "buffering")) {
1402
2343
  if (f.type != bool) ok = false;
1403
2344
  } else if (std.mem.eql(u8, f.name, "bands")) {
@@ -1424,7 +2365,7 @@ pub fn TsCoreHost(comptime core: type) type {
1424
2365
  /// engine event, by field name. The band bytes copy into the
1425
2366
  /// core's frame arena like every routed bytes payload; the
1426
2367
  /// millisecond fields widen the way the subset's number model
1427
- /// classes them (i64 or f64).
2368
+ /// classes them (i64, u64, or f64).
1428
2369
  fn msgFromTagAudio(tag: u8, event: runtime_effects.EffectAudio) Msg {
1429
2370
  inline for (msg_arms, 0..) |arm, index| {
1430
2371
  if (tag == index) {
@@ -1456,9 +2397,333 @@ pub fn TsCoreHost(comptime core: type) type {
1456
2397
  @panic("ts core host: an audio event names a Msg tag outside the union");
1457
2398
  }
1458
2399
 
2400
+ /// Whether an arm payload struct is the video event record: the
2401
+ /// seven SDK-fixed fields, matched by NAME — `state` (any enum;
2402
+ /// its members are matched by member name at delivery),
2403
+ /// `positionMs`/`durationMs` (numbers), `playing`/`buffering`
2404
+ /// (booleans), `width`/`height` (numbers).
2405
+ fn videoArmShape(comptime T: type) bool {
2406
+ const info = @typeInfo(T);
2407
+ if (info != .@"struct") return false;
2408
+ const fields = info.@"struct".fields;
2409
+ if (fields.len != 7) return false;
2410
+ var ok = true;
2411
+ for (fields) |f| {
2412
+ if (std.mem.eql(u8, f.name, "state")) {
2413
+ if (@typeInfo(f.type) != .@"enum") ok = false;
2414
+ } else if (std.mem.eql(u8, f.name, "positionMs") or std.mem.eql(u8, f.name, "durationMs") or
2415
+ std.mem.eql(u8, f.name, "width") or std.mem.eql(u8, f.name, "height"))
2416
+ {
2417
+ if (f.type != i64 and f.type != u64 and f.type != f64) ok = false;
2418
+ } else if (std.mem.eql(u8, f.name, "playing") or std.mem.eql(u8, f.name, "buffering")) {
2419
+ if (f.type != bool) ok = false;
2420
+ } else {
2421
+ ok = false;
2422
+ }
2423
+ }
2424
+ return ok;
2425
+ }
2426
+
2427
+ /// The arm's `state` member for an engine event kind, matched
2428
+ /// by member NAME — `audioStateValue`'s twin.
2429
+ fn videoStateValue(comptime E: type, kind: runtime_effects.EffectVideoEventKind) E {
2430
+ const name = @tagName(kind);
2431
+ inline for (@typeInfo(E).@"enum".fields) |f| {
2432
+ if (std.mem.eql(u8, f.name, name)) return @enumFromInt(f.value);
2433
+ }
2434
+ @panic("ts core host: a video event kind has no member in the event arm's state union - the transpiler's own shape check should have stopped this build");
2435
+ }
2436
+
2437
+ /// Build the seven-field video event arm at index `tag` from an
2438
+ /// engine event, by field name. The millisecond and dimension
2439
+ /// fields widen the way the subset's number model classes them
2440
+ /// (i64, u64, or f64).
2441
+ fn msgFromTagVideo(tag: u8, event: runtime_effects.EffectVideo) Msg {
2442
+ inline for (msg_arms, 0..) |arm, index| {
2443
+ if (tag == index) {
2444
+ if (comptime videoArmShape(arm.type)) {
2445
+ const fields = @typeInfo(arm.type).@"struct".fields;
2446
+ var payload: arm.type = undefined;
2447
+ inline for (fields) |f| {
2448
+ if (comptime std.mem.eql(u8, f.name, "state")) {
2449
+ @field(payload, f.name) = videoStateValue(f.type, event.kind);
2450
+ } else if (comptime std.mem.eql(u8, f.name, "positionMs")) {
2451
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.position_ms) else @intCast(event.position_ms);
2452
+ } else if (comptime std.mem.eql(u8, f.name, "durationMs")) {
2453
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.duration_ms) else @intCast(event.duration_ms);
2454
+ } else if (comptime std.mem.eql(u8, f.name, "playing")) {
2455
+ @field(payload, f.name) = event.playing;
2456
+ } else if (comptime std.mem.eql(u8, f.name, "buffering")) {
2457
+ @field(payload, f.name) = event.buffering;
2458
+ } else if (comptime std.mem.eql(u8, f.name, "width")) {
2459
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.width) else @intCast(event.width);
2460
+ } else {
2461
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.height) else @intCast(event.height);
2462
+ }
2463
+ }
2464
+ return @unionInit(Msg, arm.name, payload);
2465
+ }
2466
+ @panic("ts core host: a video event targets Msg arm '" ++ arm.name ++ "', which is not the seven-field video event record");
2467
+ }
2468
+ }
2469
+ @panic("ts core host: a video event names a Msg tag outside the union");
2470
+ }
2471
+
2472
+ /// The five-field image result record, matched by field name —
2473
+ /// `audioArmShape`'s twin.
2474
+ fn imageArmShape(comptime T: type) bool {
2475
+ const info = @typeInfo(T);
2476
+ if (info != .@"struct") return false;
2477
+ const fields = info.@"struct".fields;
2478
+ if (fields.len != 5) return false;
2479
+ var ok = true;
2480
+ for (fields) |f| {
2481
+ if (std.mem.eql(u8, f.name, "state")) {
2482
+ if (@typeInfo(f.type) != .@"enum") ok = false;
2483
+ } else if (std.mem.eql(u8, f.name, "id") or std.mem.eql(u8, f.name, "width") or std.mem.eql(u8, f.name, "height") or std.mem.eql(u8, f.name, "status")) {
2484
+ if (f.type != i64 and f.type != u64 and f.type != f64) ok = false;
2485
+ } else {
2486
+ ok = false;
2487
+ }
2488
+ }
2489
+ return ok;
2490
+ }
2491
+
2492
+ /// The arm's `state` member for an engine image outcome,
2493
+ /// matched by member NAME — `audioStateValue`'s twin.
2494
+ fn imageStateValue(comptime E: type, outcome: runtime_effects.EffectImageOutcome) E {
2495
+ const name = @tagName(outcome);
2496
+ inline for (@typeInfo(E).@"enum".fields) |f| {
2497
+ if (std.mem.eql(u8, f.name, name)) return @enumFromInt(f.value);
2498
+ }
2499
+ @panic("ts core host: an image outcome has no member in the result arm's state union - the transpiler's own shape check should have stopped this build");
2500
+ }
2501
+
2502
+ /// Build the five-field image result arm at index `tag` from an
2503
+ /// engine result, by field name. `id` is the requested ImageId
2504
+ /// echoed verbatim (always below 2^53 — the bridge refused
2505
+ /// anything wider — so both number classes carry it exactly).
2506
+ fn msgFromTagImage(tag: u8, result: runtime_effects.EffectImageResult) Msg {
2507
+ inline for (msg_arms, 0..) |arm, index| {
2508
+ if (tag == index) {
2509
+ if (comptime imageArmShape(arm.type)) {
2510
+ const fields = @typeInfo(arm.type).@"struct".fields;
2511
+ var payload: arm.type = undefined;
2512
+ inline for (fields) |f| {
2513
+ if (comptime std.mem.eql(u8, f.name, "state")) {
2514
+ @field(payload, f.name) = imageStateValue(f.type, result.outcome);
2515
+ } else if (comptime std.mem.eql(u8, f.name, "id")) {
2516
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(result.id) else @intCast(result.id);
2517
+ } else if (comptime std.mem.eql(u8, f.name, "width")) {
2518
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(result.width) else @intCast(result.width);
2519
+ } else if (comptime std.mem.eql(u8, f.name, "height")) {
2520
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(result.height) else @intCast(result.height);
2521
+ } else {
2522
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(result.status) else @intCast(result.status);
2523
+ }
2524
+ }
2525
+ return @unionInit(Msg, arm.name, payload);
2526
+ }
2527
+ @panic("ts core host: an image result targets Msg arm '" ++ arm.name ++ "', which is not the five-field image result record");
2528
+ }
2529
+ }
2530
+ @panic("ts core host: an image result names a Msg tag outside the union");
2531
+ }
2532
+
2533
+ /// The five-field channel event record, matched by field name —
2534
+ /// `imageArmShape`'s twin: `key` (number), `state` (any enum;
2535
+ /// members matched by name at delivery), `bytes` (bytes),
2536
+ /// `droppedPending`/`droppedTotal` (numbers).
2537
+ fn channelArmShape(comptime T: type) bool {
2538
+ const info = @typeInfo(T);
2539
+ if (info != .@"struct") return false;
2540
+ const fields = info.@"struct".fields;
2541
+ if (fields.len != 5) return false;
2542
+ var ok = true;
2543
+ for (fields) |f| {
2544
+ if (std.mem.eql(u8, f.name, "state")) {
2545
+ if (@typeInfo(f.type) != .@"enum") ok = false;
2546
+ } else if (std.mem.eql(u8, f.name, "bytes")) {
2547
+ if (f.type != []const u8) ok = false;
2548
+ } else if (std.mem.eql(u8, f.name, "key") or std.mem.eql(u8, f.name, "droppedPending") or std.mem.eql(u8, f.name, "droppedTotal")) {
2549
+ if (f.type != i64 and f.type != u64 and f.type != f64) ok = false;
2550
+ } else {
2551
+ ok = false;
2552
+ }
2553
+ }
2554
+ return ok;
2555
+ }
2556
+
2557
+ /// The arm's `state` member for an engine channel event kind,
2558
+ /// matched by member NAME — `imageStateValue`'s twin.
2559
+ fn channelStateValue(comptime E: type, kind: runtime_effects.EffectChannelEventKind) E {
2560
+ const name = @tagName(kind);
2561
+ inline for (@typeInfo(E).@"enum".fields) |f| {
2562
+ if (std.mem.eql(u8, f.name, name)) return @enumFromInt(f.value);
2563
+ }
2564
+ @panic("ts core host: a channel event kind has no member in the event arm's state union - the transpiler's own shape check should have stopped this build");
2565
+ }
2566
+
2567
+ /// Build the five-field channel event arm at index `tag` from
2568
+ /// an engine event, by field name. The bytes copy into the
2569
+ /// core's frame arena like every routed payload (the engine's
2570
+ /// slice is drain scratch); `key` is the channel key echoed
2571
+ /// verbatim (always below 2^53 — the bridge refused anything
2572
+ /// wider — so both number classes carry it exactly), and the
2573
+ /// drop counters widen the way the subset's number model
2574
+ /// classes them.
2575
+ fn msgFromTagChannel(tag: u8, event: runtime_effects.EffectChannelEvent) Msg {
2576
+ inline for (msg_arms, 0..) |arm, index| {
2577
+ if (tag == index) {
2578
+ if (comptime channelArmShape(arm.type)) {
2579
+ const fields = @typeInfo(arm.type).@"struct".fields;
2580
+ var payload: arm.type = undefined;
2581
+ inline for (fields) |f| {
2582
+ if (comptime std.mem.eql(u8, f.name, "state")) {
2583
+ @field(payload, f.name) = channelStateValue(f.type, event.kind);
2584
+ } else if (comptime std.mem.eql(u8, f.name, "key")) {
2585
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.key) else @intCast(event.key);
2586
+ } else if (comptime std.mem.eql(u8, f.name, "droppedPending")) {
2587
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.dropped_pending) else @intCast(event.dropped_pending);
2588
+ } else if (comptime std.mem.eql(u8, f.name, "droppedTotal")) {
2589
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.dropped_total) else @intCast(event.dropped_total);
2590
+ } else if (event.bytes.len == 0) {
2591
+ // Payload-free events (rejected/closed
2592
+ // terminals, and the staged rejection
2593
+ // Msgs that must be self-contained
2594
+ // across the frame reset) carry the
2595
+ // static empty slice, never a
2596
+ // zero-length frame pointer.
2597
+ @field(payload, f.name) = "";
2598
+ } else {
2599
+ const copy = core.rt.frameAlloc(u8, event.bytes.len);
2600
+ @memcpy(copy, event.bytes);
2601
+ @field(payload, f.name) = copy;
2602
+ }
2603
+ }
2604
+ return @unionInit(Msg, arm.name, payload);
2605
+ }
2606
+ @panic("ts core host: a channel event targets Msg arm '" ++ arm.name ++ "', which is not the five-field channel event record");
2607
+ }
2608
+ }
2609
+ @panic("ts core host: a channel event names a Msg tag outside the union");
2610
+ }
2611
+
2612
+ /// The six-field pty event record, matched by field name —
2613
+ /// `channelArmShape`'s twin: `state` and `reason` (any enums;
2614
+ /// members matched by name at delivery), `bytes` (bytes),
2615
+ /// `code`/`signal`/`droppedWrites` (numbers).
2616
+ fn ptyArmShape(comptime T: type) bool {
2617
+ const info = @typeInfo(T);
2618
+ if (info != .@"struct") return false;
2619
+ const fields = info.@"struct".fields;
2620
+ if (fields.len != 7) return false;
2621
+ var ok = true;
2622
+ for (fields) |f| {
2623
+ if (std.mem.eql(u8, f.name, "state") or std.mem.eql(u8, f.name, "reason")) {
2624
+ if (@typeInfo(f.type) != .@"enum") ok = false;
2625
+ } else if (std.mem.eql(u8, f.name, "bytes") or std.mem.eql(u8, f.name, "key")) {
2626
+ if (f.type != []const u8) ok = false;
2627
+ } else if (std.mem.eql(u8, f.name, "code")) {
2628
+ // Non-exited terminals deliver the -1 code sentinel:
2629
+ // signed by contract, so the unsigned class cannot
2630
+ // carry it. `signal` stays zero-or-positive (the
2631
+ // fatal signal number, else 0) and takes any class.
2632
+ if (f.type != i64 and f.type != f64) ok = false;
2633
+ } else if (std.mem.eql(u8, f.name, "signal") or std.mem.eql(u8, f.name, "droppedWrites")) {
2634
+ if (f.type != i64 and f.type != u64 and f.type != f64) ok = false;
2635
+ } else {
2636
+ ok = false;
2637
+ }
2638
+ }
2639
+ return ok;
2640
+ }
2641
+
2642
+ /// The arm's `state` member for an engine pty event kind,
2643
+ /// matched by member NAME — `channelStateValue`'s twin.
2644
+ fn ptyStateValue(comptime E: type, kind: runtime_effects.EffectPtyEventKind) E {
2645
+ const name = @tagName(kind);
2646
+ inline for (@typeInfo(E).@"enum".fields) |f| {
2647
+ if (std.mem.eql(u8, f.name, name)) return @enumFromInt(f.value);
2648
+ }
2649
+ @panic("ts core host: a pty event kind has no member in the event arm's state union - the transpiler's own shape check should have stopped this build");
2650
+ }
2651
+
2652
+ /// The arm's `reason` member for an engine exit reason, matched
2653
+ /// by member NAME — the state member's twin.
2654
+ fn ptyReasonValue(comptime E: type, reason: runtime_effects.EffectExitReason) E {
2655
+ const name = @tagName(reason);
2656
+ inline for (@typeInfo(E).@"enum".fields) |f| {
2657
+ if (std.mem.eql(u8, f.name, name)) return @enumFromInt(f.value);
2658
+ }
2659
+ @panic("ts core host: a pty exit reason has no member in the event arm's reason union - the transpiler's own shape check should have stopped this build");
2660
+ }
2661
+
2662
+ /// Build the six-field pty event arm at index `tag` from an
2663
+ /// engine event, by field name. The output bytes copy into the
2664
+ /// core's frame arena like every routed payload (the engine's
2665
+ /// slice is drain scratch); payload-free events — exits, and
2666
+ /// the staged rejection Msgs that must be self-contained across
2667
+ /// the frame reset — carry the static empty slice, the channel
2668
+ /// record's rule.
2669
+ fn msgFromTagPty(tag: u8, wire_key: []const u8, key_durable: bool, event: runtime_effects.EffectPtyEvent) Msg {
2670
+ inline for (msg_arms, 0..) |arm, index| {
2671
+ if (tag == index) {
2672
+ if (comptime ptyArmShape(arm.type)) {
2673
+ const fields = @typeInfo(arm.type).@"struct".fields;
2674
+ var payload: arm.type = undefined;
2675
+ inline for (fields) |f| {
2676
+ if (comptime std.mem.eql(u8, f.name, "state")) {
2677
+ @field(payload, f.name) = ptyStateValue(f.type, event.kind);
2678
+ } else if (comptime std.mem.eql(u8, f.name, "reason")) {
2679
+ @field(payload, f.name) = ptyReasonValue(f.type, event.reason);
2680
+ } else if (comptime std.mem.eql(u8, f.name, "code")) {
2681
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.code) else @intCast(event.code);
2682
+ } else if (comptime std.mem.eql(u8, f.name, "signal")) {
2683
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.signal) else @intCast(event.signal);
2684
+ } else if (comptime std.mem.eql(u8, f.name, "droppedWrites")) {
2685
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.dropped_writes) else @intCast(event.dropped_writes);
2686
+ } else if (comptime std.mem.eql(u8, f.name, "key")) {
2687
+ // The app's own session key, so two sessions
2688
+ // sharing one event arm are told apart by this
2689
+ // field (never the engine key). A LIVE event
2690
+ // routes and delivers within one frame, so its
2691
+ // key copies into the frame arena like every
2692
+ // routed payload. A STAGED rejection (issued
2693
+ // now, delivered a later frame, past a frame
2694
+ // reset) passes `key_durable = true` with a key
2695
+ // already in the durable reject ring, so it is
2696
+ // referenced directly — a frame-arena copy there
2697
+ // would dangle by delivery.
2698
+ if (wire_key.len == 0) {
2699
+ @field(payload, f.name) = "";
2700
+ } else if (key_durable) {
2701
+ @field(payload, f.name) = wire_key;
2702
+ } else {
2703
+ const copy = core.rt.frameAlloc(u8, wire_key.len);
2704
+ @memcpy(copy, wire_key);
2705
+ @field(payload, f.name) = copy;
2706
+ }
2707
+ } else if (event.bytes.len == 0) {
2708
+ @field(payload, f.name) = "";
2709
+ } else {
2710
+ const copy = core.rt.frameAlloc(u8, event.bytes.len);
2711
+ @memcpy(copy, event.bytes);
2712
+ @field(payload, f.name) = copy;
2713
+ }
2714
+ }
2715
+ return @unionInit(Msg, arm.name, payload);
2716
+ }
2717
+ @panic("ts core host: a pty event targets Msg arm '" ++ arm.name ++ "', which is not the seven-field pty event record");
2718
+ }
2719
+ }
2720
+ @panic("ts core host: a pty event names a Msg tag outside the union");
2721
+ }
2722
+
1459
2723
  /// Build the Msg arm at index `tag` carrying one number (`now`
1460
- /// timestamps and timer fires; an i64-classed arm truncates the
1461
- /// way the subset's number model does at index sites).
2724
+ /// timestamps and timer fires; an integer-classed arm — i64 or
2725
+ /// its unsigned twin — truncates the way the subset's number
2726
+ /// model does at index sites).
1462
2727
  fn msgFromTagNumber(tag: u8, value: f64) Msg {
1463
2728
  inline for (msg_arms, 0..) |arm, index| {
1464
2729
  if (tag == index) {
@@ -1466,6 +2731,16 @@ pub fn TsCoreHost(comptime core: type) type {
1466
2731
  return @unionInit(Msg, arm.name, value);
1467
2732
  } else if (comptime arm.type == i64) {
1468
2733
  return @unionInit(Msg, arm.name, @intFromFloat(value));
2734
+ } else if (comptime arm.type == u64) {
2735
+ // Clocks are signed by contract (a pre-epoch or
2736
+ // skewed wall clock is a legal reading): when
2737
+ // one reaches an unsigned arm there is no
2738
+ // honest value, so the crossing teaches instead
2739
+ // of faulting in the cast.
2740
+ if (value < 0) {
2741
+ @panic("ts core host: a negative number reached the u64-classed Msg arm '" ++ arm.name ++ "' — the unsigned class cannot carry it; declare the arm i64 or f64");
2742
+ }
2743
+ return @unionInit(Msg, arm.name, @intFromFloat(value));
1469
2744
  }
1470
2745
  @panic("ts core host: a timestamp targets Msg arm '" ++ arm.name ++ "', whose payload is not a number");
1471
2746
  }