@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
@@ -67,6 +67,23 @@ const mini_core = struct {
67
67
  /// matches members by NAME, so the app's declaration order is free.
68
68
  pub const AudioState = enum { spectrum, loaded, completed, position, rejected, failed };
69
69
 
70
+ /// The channel event states, shuffled off the engine order for the
71
+ /// same by-NAME matching proof.
72
+ pub const ChannelState = enum { rejected, data, closed };
73
+
74
+ /// The image outcome states the mixed-rejection tests observe,
75
+ /// shuffled off the engine order like the other two.
76
+ pub const ImageState = enum { rejected, loaded };
77
+ /// The video event state union, scrambled against the engine's
78
+ /// `EffectVideoEventKind` for the same by-NAME pin.
79
+ pub const VideoState = enum { completed, loaded, rejected, position, failed };
80
+
81
+ /// The pty event states and exit reasons, both shuffled off the
82
+ /// engine order for the same by-NAME matching proof (the pty arm
83
+ /// carries TWO name-matched unions).
84
+ pub const PtyState = enum { exit, output };
85
+ pub const PtyReason = enum { cancelled, exited, rejected, spawn_failed, signaled };
86
+
70
87
  pub const Model = struct {
71
88
  polling: bool,
72
89
  fast: bool,
@@ -91,6 +108,43 @@ const mini_core = struct {
91
108
  buffering: bool,
92
109
  bands: []const u8,
93
110
  audio_events: i64,
111
+ // Channel event mirrors.
112
+ chan_state: ChannelState,
113
+ chan_key: f64,
114
+ chan_bytes: []const u8,
115
+ chan_dropped_pending: f64,
116
+ chan_dropped_total: f64,
117
+ chan_events: i64,
118
+ // Image event mirrors.
119
+ img_state: ImageState,
120
+ img_events: i64,
121
+ // Pty event mirrors.
122
+ pty_key: []const u8,
123
+ pty_state: PtyState,
124
+ pty_bytes: []const u8,
125
+ pty_code: i64,
126
+ pty_reason: PtyReason,
127
+ pty_signal: i64,
128
+ pty_dropped: f64,
129
+ pty_events: i64,
130
+ // Rejection delivery order probe: one mark per rejection Msg
131
+ // ('S' spawn, 'I' image, 'C' channel), in dispatch order.
132
+ order: []const u8,
133
+ // Video stream mirrors.
134
+ video_state: VideoState,
135
+ v_pos: f64,
136
+ v_dur: f64,
137
+ v_playing: bool,
138
+ v_buffering: bool,
139
+ v_w: f64,
140
+ v_h: f64,
141
+ video_events: i64,
142
+ // Second-arm mirrors (the replaced-load straggler probe).
143
+ video2_state: VideoState,
144
+ video2_events: i64,
145
+ // Unsigned-class mirrors (u64-classed arm routing).
146
+ ustamp_ms: u64,
147
+ ucode: u64,
94
148
  };
95
149
 
96
150
  pub const Msg = union(enum) {
@@ -145,6 +199,96 @@ const mini_core = struct {
145
199
  drop_save, // 40: cancel "save" (silent write_file drop)
146
200
  drop_get, // 41: cancel "get" (silent fetch drop)
147
201
  drop_paste, // 42: cancel "paste" (silent clip_read drop)
202
+ open_win, // 43: window_show "player" (the tray Open consequence)
203
+ quit_app, // 44: quit_app (the tray Quit consequence)
204
+ open_chan, // 45: channel_open key 41 -> chan_evt
205
+ close_chan, // 46: channel_close key 41
206
+ chan_evt: struct { // 47: the five-field channel event arm (the
207
+ // emitted shape — payload fields keep their TS names)
208
+ key: f64,
209
+ state: ChannelState,
210
+ bytes: []const u8,
211
+ droppedPending: f64,
212
+ droppedTotal: f64,
213
+ },
214
+ load_img, // 48: image_load id 7 -> img_evt
215
+ img_evt: struct { // 49: the five-field image result arm
216
+ id: f64,
217
+ state: ImageState,
218
+ width: f64,
219
+ height: f64,
220
+ status: f64,
221
+ },
222
+ mix_chan_then_img, // 50: batch [channel_open 41, image_load 7]
223
+ mix_img_then_chan, // 51: batch [image_load 7, channel_open 41]
224
+ mix_three, // 52: batch [channel_open 41, image_load 7, spawn "job"]
225
+ chan_evt2: struct { // 53: a second channel event arm so two
226
+ // refused opens in one batch stay distinguishable
227
+ key: f64,
228
+ state: ChannelState,
229
+ bytes: []const u8,
230
+ droppedPending: f64,
231
+ droppedTotal: f64,
232
+ },
233
+ mix_dup_chan_over_img, // 54: batch [channel_open 7 -> chan_evt,
234
+ // channel_open 7 -> chan_evt2] while image id 7 holds the raw key
235
+ mix_dup_then_engine, // 55: batch [channel_open 41 -> chan_evt2
236
+ // (bridge dup), channel_open 7 -> chan_evt (engine cross-family)]
237
+ vload, // 56: video_load "clip" (surface 5, local path, autoplay)
238
+ vload_url, // 57: video_load "clip" (url, autoplay off, loop + muted)
239
+ video_evt: struct { // 58: the seven-field video event arm (the
240
+ // emitted shape — payload fields keep their TS names)
241
+ state: VideoState,
242
+ positionMs: f64,
243
+ durationMs: f64,
244
+ playing: bool,
245
+ buffering: bool,
246
+ width: f64,
247
+ height: f64,
248
+ },
249
+ vplay_it, // 59: video_ctl play "clip"
250
+ vpause_it, // 60: video_ctl pause "clip"
251
+ vstop_it, // 61: video_ctl stop "clip"
252
+ vseek_it, // 62: video_ctl seek "clip" 45000ms
253
+ vvol_it, // 63: video_ctl volume "clip" 0.25
254
+ vmute_it, // 64: video_ctl muted "clip" 1
255
+ vloop_it, // 65: video_ctl loop "clip" 1
256
+ vctl_stray, // 66: video_ctl pause "other" (key gate no-op)
257
+ vload_bad, // 67: video_load "bad" with surface 0 (bridge-refused)
258
+ vload2, // 68: video_load "clip2" routed to video_evt2
259
+ video_evt2: struct { // 69: a second video event arm so a
260
+ // replaced load's straggling terminal stays distinguishable
261
+ state: VideoState,
262
+ positionMs: f64,
263
+ durationMs: f64,
264
+ playing: bool,
265
+ buffering: bool,
266
+ width: f64,
267
+ height: f64,
268
+ },
269
+ vseek_far, // 70: video_ctl seek "clip" 1e16ms (past the exact window)
270
+ vseek_inf, // 71: video_ctl seek "clip" Infinity (not an offset at all)
271
+ open_pty, // 72: pty_spawn key "shell" 120x30 xterm-256color -> pty_evt
272
+ open_pty_default, // 73: pty_spawn key "shell", wire term "" (engine default)
273
+ write_pty, // 74: pty_write "shell" "ls\n"
274
+ resize_pty, // 75: pty_resize "shell" 100x40
275
+ kill_pty, // 76: pty_kill "shell"
276
+ pty_evt: struct { // 77: the seven-field pty event arm (the emitted
277
+ // shape — payload fields keep their TS names)
278
+ key: []const u8,
279
+ state: PtyState,
280
+ bytes: []const u8,
281
+ code: f64,
282
+ reason: PtyReason,
283
+ signal: f64,
284
+ droppedWrites: f64,
285
+ },
286
+ dup_pty, // 78: two pty spawns under one key in one batch
287
+ ustamp, // 79: Cmd.now -> .ustamped (unsigned integer class)
288
+ ustamped: u64, // 80: now arm, u64-classed
289
+ uget, // 81: fetch "uget" -> ufetched/failed
290
+ ufetched: struct { status: u64, body: []const u8 }, // 82: fetch ok
291
+ // record with a u64-classed number field
148
292
  };
149
293
 
150
294
  pub const InitResult = struct { model: *const Model, cmd: []const u8 };
@@ -180,6 +324,35 @@ const mini_core = struct {
180
324
  .buffering = false,
181
325
  .bands = "",
182
326
  .audio_events = 0,
327
+ .chan_state = .closed,
328
+ .chan_key = -1,
329
+ .chan_bytes = "",
330
+ .chan_dropped_pending = -1,
331
+ .chan_dropped_total = -1,
332
+ .chan_events = 0,
333
+ .img_state = .loaded,
334
+ .img_events = 0,
335
+ .pty_key = "",
336
+ .pty_state = .output,
337
+ .pty_bytes = "",
338
+ .pty_code = -1,
339
+ .pty_reason = .exited,
340
+ .pty_signal = -1,
341
+ .pty_dropped = -1,
342
+ .pty_events = 0,
343
+ .order = "",
344
+ .video_state = .rejected,
345
+ .v_pos = -1,
346
+ .v_dur = -1,
347
+ .v_playing = false,
348
+ .v_buffering = false,
349
+ .v_w = -1,
350
+ .v_h = -1,
351
+ .video_events = 0,
352
+ .video2_state = .rejected,
353
+ .video2_events = 0,
354
+ .ustamp_ms = 0,
355
+ .ucode = 0,
183
356
  }),
184
357
  .cmd = cmdRequest("status.read", "status", 7, 8, "boot"),
185
358
  };
@@ -227,6 +400,7 @@ const mini_core = struct {
227
400
  const out = frameCreate(model.*);
228
401
  out.errs = model.errs + 1;
229
402
  out.last_err = why;
403
+ if (std.mem.eql(u8, why, "rejected")) out.order = appendOrder(model.order, 'S');
230
404
  return .{ .model = out, .cmd = "" };
231
405
  },
232
406
  .save_file => return .{ .model = model, .cmd = cmdWriteFile("save", 12, 8, "notes.bin", model.status) },
@@ -270,6 +444,19 @@ const mini_core = struct {
270
444
  out.stamp_ms = at;
271
445
  return .{ .model = out, .cmd = "" };
272
446
  },
447
+ .ustamp => return .{ .model = model, .cmd = cmdNow(80) },
448
+ .ustamped => |at| {
449
+ const out = frameCreate(model.*);
450
+ out.ustamp_ms = at;
451
+ return .{ .model = out, .cmd = "" };
452
+ },
453
+ .uget => return .{ .model = model, .cmd = cmdFetch("uget", 82, 8, 1, 0, "https://status.test/u", &.{}, "") },
454
+ .ufetched => |response| {
455
+ const out = frameCreate(model.*);
456
+ out.ucode = response.status;
457
+ out.status = response.body;
458
+ return .{ .model = out, .cmd = "" };
459
+ },
273
460
  .run_lines => return .{ .model = model, .cmd = cmdSpawn("job", 25, 26, 8, 0, &.{ "/bin/probe", "--fast" }, "feed me") },
274
461
  .run_quiet => return .{ .model = model, .cmd = cmdSpawn("job", 0xFF, 26, 8, 0, &.{"/bin/quiet"}, "") },
275
462
  .run_collect => return .{ .model = model, .cmd = cmdSpawn("job", 0xFF, 27, 8, 1, &.{ "/bin/ps", "-axo" }, "") },
@@ -322,9 +509,147 @@ const mini_core = struct {
322
509
  .drop_save => return .{ .model = model, .cmd = cmdCancel("save") },
323
510
  .drop_get => return .{ .model = model, .cmd = cmdCancel("get") },
324
511
  .drop_paste => return .{ .model = model, .cmd = cmdCancel("paste") },
512
+ .open_win => return .{ .model = model, .cmd = cmdWindowShow("player") },
513
+ .quit_app => return .{ .model = model, .cmd = cmdQuitApp() },
514
+ .open_chan => return .{ .model = model, .cmd = cmdChannelOpen(41, 47) },
515
+ .close_chan => return .{ .model = model, .cmd = cmdChannelClose(41) },
516
+ .chan_evt => |event| {
517
+ const out = frameCreate(model.*);
518
+ out.chan_state = event.state;
519
+ out.chan_key = event.key;
520
+ out.chan_bytes = event.bytes;
521
+ out.chan_dropped_pending = event.droppedPending;
522
+ out.chan_dropped_total = event.droppedTotal;
523
+ out.chan_events = model.chan_events + 1;
524
+ if (event.state == .rejected) out.order = appendOrder(model.order, 'C');
525
+ return .{ .model = out, .cmd = "" };
526
+ },
527
+ .load_img => return .{ .model = model, .cmd = cmdImageLoad(7, 49, "img/a.png", "", "", 0) },
528
+ .img_evt => |event| {
529
+ const out = frameCreate(model.*);
530
+ out.img_state = event.state;
531
+ out.img_events = model.img_events + 1;
532
+ if (event.state == .rejected) out.order = appendOrder(model.order, 'I');
533
+ return .{ .model = out, .cmd = "" };
534
+ },
535
+ .mix_chan_then_img => {
536
+ const first = cmdChannelOpen(41, 47);
537
+ const second = cmdImageLoad(7, 49, "img/a.png", "", "", 0);
538
+ const out = rt.frameAlloc(u8, first.len + second.len);
539
+ @memcpy(out[0..first.len], first);
540
+ @memcpy(out[first.len..], second);
541
+ return .{ .model = model, .cmd = out };
542
+ },
543
+ .mix_img_then_chan => {
544
+ const first = cmdImageLoad(7, 49, "img/a.png", "", "", 0);
545
+ const second = cmdChannelOpen(41, 47);
546
+ const out = rt.frameAlloc(u8, first.len + second.len);
547
+ @memcpy(out[0..first.len], first);
548
+ @memcpy(out[first.len..], second);
549
+ return .{ .model = model, .cmd = out };
550
+ },
551
+ .mix_three => {
552
+ const first = cmdChannelOpen(41, 47);
553
+ const second = cmdImageLoad(7, 49, "img/a.png", "", "", 0);
554
+ const third = cmdSpawn("job", 0xFF, 26, 8, 0, &.{"/bin/dup"}, "");
555
+ const out = rt.frameAlloc(u8, first.len + second.len + third.len);
556
+ @memcpy(out[0..first.len], first);
557
+ @memcpy(out[first.len..][0..second.len], second);
558
+ @memcpy(out[first.len + second.len ..], third);
559
+ return .{ .model = model, .cmd = out };
560
+ },
561
+ .chan_evt2 => |event| {
562
+ const out = frameCreate(model.*);
563
+ out.chan_events = model.chan_events + 1;
564
+ if (event.state == .rejected) out.order = appendOrder(model.order, 'D');
565
+ return .{ .model = out, .cmd = "" };
566
+ },
567
+ .mix_dup_chan_over_img => {
568
+ const first = cmdChannelOpen(7, 47);
569
+ const second = cmdChannelOpen(7, 53);
570
+ const out = rt.frameAlloc(u8, first.len + second.len);
571
+ @memcpy(out[0..first.len], first);
572
+ @memcpy(out[first.len..], second);
573
+ return .{ .model = model, .cmd = out };
574
+ },
575
+ .mix_dup_then_engine => {
576
+ const first = cmdChannelOpen(41, 53);
577
+ const second = cmdChannelOpen(7, 47);
578
+ const out = rt.frameAlloc(u8, first.len + second.len);
579
+ @memcpy(out[0..first.len], first);
580
+ @memcpy(out[first.len..], second);
581
+ return .{ .model = model, .cmd = out };
582
+ },
583
+ // flags bit0 = autoplay, bit1 = loop, bit2 = muted.
584
+ .vload => return .{ .model = model, .cmd = cmdVideoLoad("clip", 58, 5, "media/clip.mp4", "", 0b001) },
585
+ .vload_url => return .{ .model = model, .cmd = cmdVideoLoad("clip", 58, 5, "", "https://cdn.test/clip.mp4", 0b110) },
586
+ .video_evt => |event| {
587
+ const out = frameCreate(model.*);
588
+ out.video_state = event.state;
589
+ out.v_pos = event.positionMs;
590
+ out.v_dur = event.durationMs;
591
+ out.v_playing = event.playing;
592
+ out.v_buffering = event.buffering;
593
+ out.v_w = event.width;
594
+ out.v_h = event.height;
595
+ out.video_events = model.video_events + 1;
596
+ return .{ .model = out, .cmd = "" };
597
+ },
598
+ .vplay_it => return .{ .model = model, .cmd = cmdVideoCtl("clip", 0, 0) },
599
+ .vpause_it => return .{ .model = model, .cmd = cmdVideoCtl("clip", 1, 0) },
600
+ .vstop_it => return .{ .model = model, .cmd = cmdVideoCtl("clip", 2, 0) },
601
+ .vseek_it => return .{ .model = model, .cmd = cmdVideoCtl("clip", 3, 45_000) },
602
+ .vseek_far => return .{ .model = model, .cmd = cmdVideoCtl("clip", 3, 1e16) },
603
+ .vseek_inf => return .{ .model = model, .cmd = cmdVideoCtl("clip", 3, std.math.inf(f64)) },
604
+ .vvol_it => return .{ .model = model, .cmd = cmdVideoCtl("clip", 4, 0.25) },
605
+ .vmute_it => return .{ .model = model, .cmd = cmdVideoCtl("clip", 5, 1) },
606
+ .vloop_it => return .{ .model = model, .cmd = cmdVideoCtl("clip", 6, 1) },
607
+ .vctl_stray => return .{ .model = model, .cmd = cmdVideoCtl("other", 1, 0) },
608
+ // Surface 0 is the engine's own refusal class: the bridge
609
+ // must reject WITHOUT re-routing the single entry.
610
+ .vload_bad => return .{ .model = model, .cmd = cmdVideoLoad("bad", 58, 0, "media/x.mp4", "", 0b001) },
611
+ .vload2 => return .{ .model = model, .cmd = cmdVideoLoad("clip2", 69, 5, "media/clip2.mp4", "", 0b001) },
612
+ .video_evt2 => |event| {
613
+ const out = frameCreate(model.*);
614
+ out.video2_state = event.state;
615
+ out.video2_events = model.video2_events + 1;
616
+ return .{ .model = out, .cmd = "" };
617
+ },
618
+ .open_pty => return .{ .model = model, .cmd = cmdPtySpawn("shell", 77, 120, 30, "xterm-256color", &.{ "/bin/zsh", "-l" }) },
619
+ .open_pty_default => return .{ .model = model, .cmd = cmdPtySpawn("shell", 77, 80, 24, "", &.{"/bin/sh"}) },
620
+ .write_pty => return .{ .model = model, .cmd = cmdPtyWrite("shell", "ls\n") },
621
+ .resize_pty => return .{ .model = model, .cmd = cmdPtyResize("shell", 100, 40) },
622
+ .kill_pty => return .{ .model = model, .cmd = cmdPtyKill("shell") },
623
+ .pty_evt => |event| {
624
+ const out = frameCreate(model.*);
625
+ out.pty_key = event.key;
626
+ out.pty_state = event.state;
627
+ out.pty_bytes = event.bytes;
628
+ out.pty_code = @intFromFloat(event.code);
629
+ out.pty_reason = event.reason;
630
+ out.pty_signal = @intFromFloat(event.signal);
631
+ out.pty_dropped = event.droppedWrites;
632
+ out.pty_events = model.pty_events + 1;
633
+ return .{ .model = out, .cmd = "" };
634
+ },
635
+ .dup_pty => {
636
+ const first = cmdPtySpawn("shell", 77, 80, 24, "", &.{"/bin/one"});
637
+ const second = cmdPtySpawn("shell", 77, 80, 24, "", &.{"/bin/two"});
638
+ const out = rt.frameAlloc(u8, first.len + second.len);
639
+ @memcpy(out[0..first.len], first);
640
+ @memcpy(out[first.len..], second);
641
+ return .{ .model = model, .cmd = out };
642
+ },
325
643
  }
326
644
  }
327
645
 
646
+ fn appendOrder(prev: []const u8, mark: u8) []const u8 {
647
+ const out = rt.frameAlloc(u8, prev.len + 1);
648
+ @memcpy(out[0..prev.len], prev);
649
+ out[prev.len] = mark;
650
+ return out;
651
+ }
652
+
328
653
  pub fn subscriptions(model: *const Model) []const u8 {
329
654
  if (!model.polling) return "";
330
655
  return subTimer("tick", if (model.fast) 40 else 100, 9);
@@ -339,6 +664,9 @@ const mini_core = struct {
339
664
  out[0].last_line = commitBytes(next.last_line);
340
665
  out[0].output = commitBytes(next.output);
341
666
  out[0].bands = commitBytes(next.bands);
667
+ out[0].chan_bytes = commitBytes(next.chan_bytes);
668
+ out[0].pty_bytes = commitBytes(next.pty_bytes);
669
+ out[0].order = commitBytes(next.order);
342
670
  return &out[0];
343
671
  }
344
672
 
@@ -524,6 +852,121 @@ const mini_core = struct {
524
852
  return out;
525
853
  }
526
854
 
855
+ fn cmdVideoLoad(key: []const u8, event_tag: u8, surface: f64, video_path: []const u8, url: []const u8, flags: u8) []const u8 {
856
+ const out = rt.frameAlloc(u8, 2 + key.len + 1 + 8 + 4 + video_path.len + 4 + url.len + 1);
857
+ out[0] = 0x17;
858
+ out[1] = @intCast(key.len);
859
+ @memcpy(out[2..][0..key.len], key);
860
+ var off: usize = 2 + key.len;
861
+ out[off] = event_tag;
862
+ std.mem.writeInt(u64, out[off + 1 ..][0..8], @bitCast(surface), .little);
863
+ off += 9;
864
+ off = writeLongBytes(out, off, video_path);
865
+ off = writeLongBytes(out, off, url);
866
+ out[off] = flags;
867
+ return out;
868
+ }
869
+
870
+ fn cmdVideoCtl(key: []const u8, verb: u8, value: f64) []const u8 {
871
+ const out = rt.frameAlloc(u8, 2 + key.len + 1 + 8);
872
+ out[0] = 0x18;
873
+ out[1] = @intCast(key.len);
874
+ @memcpy(out[2..][0..key.len], key);
875
+ out[2 + key.len] = verb;
876
+ std.mem.writeInt(u64, out[2 + key.len + 1 ..][0..8], @bitCast(value), .little);
877
+ return out;
878
+ }
879
+
880
+ fn cmdWindowShow(label: []const u8) []const u8 {
881
+ const out = rt.frameAlloc(u8, 2 + label.len);
882
+ out[0] = 0x10;
883
+ out[1] = @intCast(label.len);
884
+ @memcpy(out[2..][0..label.len], label);
885
+ return out;
886
+ }
887
+
888
+ fn cmdQuitApp() []const u8 {
889
+ const out = rt.frameAlloc(u8, 1);
890
+ out[0] = 0x11;
891
+ return out;
892
+ }
893
+
894
+ fn cmdImageLoad(id: f64, event_tag: u8, image_path: []const u8, url: []const u8, cache_path: []const u8, expected: f64) []const u8 {
895
+ const out = rt.frameAlloc(u8, 1 + 8 + 1 + 4 + image_path.len + 4 + url.len + 4 + cache_path.len + 8);
896
+ out[0] = 0x12;
897
+ std.mem.writeInt(u64, out[1..][0..8], @bitCast(id), .little);
898
+ out[9] = event_tag;
899
+ var off: usize = 10;
900
+ off = writeLongBytes(out, off, image_path);
901
+ off = writeLongBytes(out, off, url);
902
+ off = writeLongBytes(out, off, cache_path);
903
+ std.mem.writeInt(u64, out[off..][0..8], @bitCast(expected), .little);
904
+ return out;
905
+ }
906
+
907
+ fn cmdChannelOpen(key: f64, event_tag: u8) []const u8 {
908
+ const out = rt.frameAlloc(u8, 1 + 8 + 1);
909
+ out[0] = 0x15;
910
+ std.mem.writeInt(u64, out[1..][0..8], @bitCast(key), .little);
911
+ out[9] = event_tag;
912
+ return out;
913
+ }
914
+
915
+ fn cmdChannelClose(key: f64) []const u8 {
916
+ const out = rt.frameAlloc(u8, 1 + 8);
917
+ out[0] = 0x16;
918
+ std.mem.writeInt(u64, out[1..][0..8], @bitCast(key), .little);
919
+ return out;
920
+ }
921
+
922
+ fn cmdPtySpawn(key: []const u8, event_tag: u8, cols: f64, rows: f64, term: []const u8, argv: []const []const u8) []const u8 {
923
+ var argv_bytes: usize = 0;
924
+ for (argv) |arg| argv_bytes += 4 + arg.len;
925
+ const out = rt.frameAlloc(u8, 2 + key.len + 1 + 8 + 8 + 1 + term.len + 1 + argv_bytes);
926
+ out[0] = 0x19;
927
+ out[1] = @intCast(key.len);
928
+ @memcpy(out[2..][0..key.len], key);
929
+ var off: usize = 2 + key.len;
930
+ out[off] = event_tag;
931
+ std.mem.writeInt(u64, out[off + 1 ..][0..8], @bitCast(cols), .little);
932
+ std.mem.writeInt(u64, out[off + 9 ..][0..8], @bitCast(rows), .little);
933
+ off += 17;
934
+ out[off] = @intCast(term.len);
935
+ @memcpy(out[off + 1 ..][0..term.len], term);
936
+ off += 1 + term.len;
937
+ out[off] = @intCast(argv.len);
938
+ off += 1;
939
+ for (argv) |arg| off = writeLongBytes(out, off, arg);
940
+ return out;
941
+ }
942
+
943
+ fn cmdPtyWrite(key: []const u8, bytes: []const u8) []const u8 {
944
+ const out = rt.frameAlloc(u8, 2 + key.len + 4 + bytes.len);
945
+ out[0] = 0x1A;
946
+ out[1] = @intCast(key.len);
947
+ @memcpy(out[2..][0..key.len], key);
948
+ _ = writeLongBytes(out, 2 + key.len, bytes);
949
+ return out;
950
+ }
951
+
952
+ fn cmdPtyResize(key: []const u8, cols: f64, rows: f64) []const u8 {
953
+ const out = rt.frameAlloc(u8, 2 + key.len + 8 + 8);
954
+ out[0] = 0x1B;
955
+ out[1] = @intCast(key.len);
956
+ @memcpy(out[2..][0..key.len], key);
957
+ std.mem.writeInt(u64, out[2 + key.len ..][0..8], @bitCast(cols), .little);
958
+ std.mem.writeInt(u64, out[2 + key.len + 8 ..][0..8], @bitCast(rows), .little);
959
+ return out;
960
+ }
961
+
962
+ fn cmdPtyKill(key: []const u8) []const u8 {
963
+ const out = rt.frameAlloc(u8, 2 + key.len);
964
+ out[0] = 0x1C;
965
+ out[1] = @intCast(key.len);
966
+ @memcpy(out[2..][0..key.len], key);
967
+ return out;
968
+ }
969
+
527
970
  fn subTimer(key: []const u8, every_ms: f64, msg_tag: u8) []const u8 {
528
971
  const out = rt.frameAlloc(u8, 2 + key.len + 8 + 1);
529
972
  out[0] = 0x01;
@@ -732,6 +1175,28 @@ test "Cmd.now dispatches synchronously with the journaled clock" {
732
1175
  try std.testing.expectEqual(effects_mod.EffectResultKind.clock, Capture.kinds[0]);
733
1176
  }
734
1177
 
1178
+ test "u64-classed arms route the number and number_bytes dispatch paths" {
1179
+ const fx = freshChannel();
1180
+ defer fx.deinit();
1181
+ var clock = runtime_clock.TestClock{};
1182
+ clock.setWallMs(1_234);
1183
+ fx.clock = clock.clock();
1184
+ Host.init(fx);
1185
+
1186
+ // Cmd.now into a u64-classed arm: the fire time narrows into the
1187
+ // unsigned class the way an i64-classed arm narrows.
1188
+ Host.dispatch(fx, .ustamp);
1189
+ try std.testing.expectEqual(@as(u64, 1_234), Host.model().ustamp_ms);
1190
+
1191
+ // A fetch ok record whose number field is u64-classed still matches
1192
+ // the { number, bytes } shape by type and routes whole.
1193
+ Host.dispatch(fx, .uget);
1194
+ try fx.feedResponse(load_effect_key, 404, "missing");
1195
+ Host.drain(fx);
1196
+ try std.testing.expectEqual(@as(u64, 404), Host.model().ucode);
1197
+ try std.testing.expectEqualStrings("missing", Host.model().status);
1198
+ }
1199
+
735
1200
  test "fire-and-forget records ride the host-call binding in wire order" {
736
1201
  const fx = freshChannel();
737
1202
  defer fx.deinit();
@@ -1225,9 +1690,12 @@ test "a duplicate spawn key rejects the new spawn through its err arm (the one e
1225
1690
 
1226
1691
  // A running subprocess is never killed implicitly: unlike the named
1227
1692
  // ops, a live spawn key REJECTS the new spawn — cancel it first.
1693
+ // The rejection Msg stages into the engine's pending order and
1694
+ // delivers at the next drain (the one rejection stream).
1228
1695
  Host.dispatch(fx, .dup_job);
1229
1696
  try std.testing.expectEqual(@as(usize, 1), fx.pendingSpawnCount());
1230
1697
  try std.testing.expectEqualStrings("/bin/one", fx.pendingSpawnAt(0).?.argv[0]);
1698
+ Host.drain(fx);
1231
1699
  try std.testing.expectEqual(@as(i64, 1), Host.model().errs);
1232
1700
  try std.testing.expectEqualStrings("rejected", Host.model().last_err);
1233
1701
 
@@ -1300,13 +1768,21 @@ test "audio_play decodes whole and events route the six-field arm by name" {
1300
1768
  try std.testing.expectEqual(@as(usize, 32), Host.model().bands.len);
1301
1769
  try std.testing.expectEqualSlices(u8, full[0..32], Host.model().bands);
1302
1770
 
1771
+ // Audio scalars clamp into the exact-integer delivery window at the
1772
+ // feed boundary (the video clamp's twin): whatever a host reports,
1773
+ // integer-classed Msg fields and the mirrors only ever see values
1774
+ // below 2^53.
1775
+ try fx.feedAudioEvent(.position, std.math.maxInt(u64), 183_000, true);
1776
+ Host.drain(fx);
1777
+ try std.testing.expectEqual(@as(f64, 9007199254740991), Host.model().position_ms);
1778
+
1303
1779
  // completed does NOT close the stream (apps start the next track
1304
1780
  // from it); the entry keeps routing.
1305
1781
  try fx.feedAudioEvent(.completed, 183_000, 183_000, false);
1306
1782
  Host.drain(fx);
1307
1783
  try std.testing.expectEqual(mini_core.AudioState.completed, Host.model().audio_state);
1308
1784
  try std.testing.expect(!Host.model().playing);
1309
- try std.testing.expectEqual(@as(i64, 5), Host.model().audio_events);
1785
+ try std.testing.expectEqual(@as(i64, 6), Host.model().audio_events);
1310
1786
  }
1311
1787
 
1312
1788
  test "audio_ctl verbs drive the engine channel, gated by the wire key" {
@@ -1362,6 +1838,254 @@ test "a replacing audio_play re-keys the stream and the url source decodes whole
1362
1838
  try std.testing.expect(!fx.audioSnapshot().playing);
1363
1839
  }
1364
1840
 
1841
+ // ------------------------------------------------------- video stream
1842
+
1843
+ test "video_load decodes whole and events route the seven-field arm by name" {
1844
+ const fx = freshChannel();
1845
+ defer fx.deinit();
1846
+ Host.init(fx);
1847
+
1848
+ Host.dispatch(fx, .vload);
1849
+ const request = fx.pendingVideo().?;
1850
+ // The engine key is the video namespace with the load's event tag
1851
+ // (video_evt = 58) in the low byte — the per-load routing stamp.
1852
+ try std.testing.expectEqual(ts_core_host.videoKeyForTag(58), request.key);
1853
+ try std.testing.expectEqual(@as(u64, 5), request.surface);
1854
+ try std.testing.expectEqualStrings("media/clip.mp4", request.path);
1855
+ try std.testing.expectEqualStrings("", request.url);
1856
+ try std.testing.expect(request.playing); // autoplay flag
1857
+ try std.testing.expect(!request.looping);
1858
+ try std.testing.expect(!request.muted);
1859
+
1860
+ // The loaded acknowledgment routes the event arm with the decoded
1861
+ // dimensions; the state member is matched by NAME (the mini core
1862
+ // scrambles its declaration order on purpose).
1863
+ try fx.feedVideoEvent(.loaded, 0, 12_000, true, false, 1920, 1080);
1864
+ Host.drain(fx);
1865
+ try std.testing.expectEqual(mini_core.VideoState.loaded, Host.model().video_state);
1866
+ try std.testing.expectEqual(@as(f64, 12_000), Host.model().v_dur);
1867
+ try std.testing.expectEqual(@as(f64, 1920), Host.model().v_w);
1868
+ try std.testing.expectEqual(@as(f64, 1080), Host.model().v_h);
1869
+ try std.testing.expect(Host.model().v_playing);
1870
+ try std.testing.expectEqual(@as(i64, 1), Host.model().video_events);
1871
+
1872
+ // Position ticks keep flowing through the same non-retiring entry,
1873
+ // the buffering flag riding along.
1874
+ try fx.feedVideoEvent(.position, 1_500, 12_000, true, false, 0, 0);
1875
+ try fx.feedVideoEvent(.position, 2_000, 12_000, true, true, 0, 0);
1876
+ Host.drain(fx);
1877
+ try std.testing.expectEqual(mini_core.VideoState.position, Host.model().video_state);
1878
+ try std.testing.expectEqual(@as(f64, 2_000), Host.model().v_pos);
1879
+ try std.testing.expect(Host.model().v_buffering);
1880
+ try std.testing.expectEqual(@as(i64, 3), Host.model().video_events);
1881
+
1882
+ // completed does NOT close the stream (apps start the next clip
1883
+ // from it); the entry keeps routing.
1884
+ try fx.feedVideoEvent(.completed, 12_000, 12_000, false, false, 0, 0);
1885
+ Host.drain(fx);
1886
+ try std.testing.expectEqual(mini_core.VideoState.completed, Host.model().video_state);
1887
+ try std.testing.expect(!Host.model().v_playing);
1888
+ try std.testing.expectEqual(@as(i64, 4), Host.model().video_events);
1889
+
1890
+ // A replacing video_load re-keys the stream in place and the url
1891
+ // record's option flags decode whole (autoplay off, loop + muted).
1892
+ Host.dispatch(fx, .vload_url);
1893
+ const replaced = fx.pendingVideo().?;
1894
+ try std.testing.expectEqualStrings("", replaced.path);
1895
+ try std.testing.expectEqualStrings("https://cdn.test/clip.mp4", replaced.url);
1896
+ try std.testing.expect(!replaced.playing);
1897
+ try std.testing.expect(replaced.looping);
1898
+ try std.testing.expect(replaced.muted);
1899
+ }
1900
+
1901
+ test "a stale wire key's verbs never touch a playback the bridge did not load" {
1902
+ const fx = freshChannel();
1903
+ defer fx.deinit();
1904
+ Host.init(fx);
1905
+
1906
+ // The bridge opens a stream, then a load the bridge never issued
1907
+ // (a declarative element's) replaces the single engine player.
1908
+ Host.dispatch(fx, .vload);
1909
+ fx.loadVideo(.{
1910
+ .key = 424_242,
1911
+ .surface = 5,
1912
+ .path = "media/declared.mp4",
1913
+ });
1914
+ try std.testing.expect(fx.videoSnapshot().playing);
1915
+ try std.testing.expectEqual(@as(u64, 424_242), fx.videoSnapshot().key);
1916
+
1917
+ // The stale entry's transport verbs no-op: pausing, seeking, or
1918
+ // muting through the old wire key must not mutate someone else's
1919
+ // playback.
1920
+ Host.dispatch(fx, .vpause_it);
1921
+ try std.testing.expect(fx.videoSnapshot().playing);
1922
+ Host.dispatch(fx, .vseek_it);
1923
+ try std.testing.expectEqual(@as(u64, 0), fx.videoSnapshot().position_ms);
1924
+ Host.dispatch(fx, .vmute_it);
1925
+ try std.testing.expect(!fx.videoSnapshot().muted);
1926
+
1927
+ // Volume is gated too while a FOREIGN playback is live: its
1928
+ // volume is not this key's to move.
1929
+ Host.dispatch(fx, .vvol_it);
1930
+ try std.testing.expectEqual(@as(f32, 1.0), fx.videoSnapshot().volume);
1931
+
1932
+ // Stop retires the entry and cancels the entry's OWN stream, but
1933
+ // the playback on the channel — someone else's — survives.
1934
+ Host.dispatch(fx, .vstop_it);
1935
+ try std.testing.expect(fx.videoSnapshot().active);
1936
+ try std.testing.expect(fx.videoSnapshot().playing);
1937
+ }
1938
+
1939
+ test "volume set after a failed load is remembered for the retry" {
1940
+ const fx = freshChannel();
1941
+ defer fx.deinit();
1942
+ Host.init(fx);
1943
+
1944
+ // The load fails and resets the channel; the handler sets the
1945
+ // volume before retrying. Volume is a remembered preference the
1946
+ // next load re-applies, and with the channel idle there is no
1947
+ // other caller's playback to protect — the command must land.
1948
+ Host.dispatch(fx, .vload);
1949
+ try fx.feedVideoEvent(.failed, 0, 0, false, false, 0, 0);
1950
+ Host.drain(fx);
1951
+ try std.testing.expect(!fx.videoSnapshot().active);
1952
+ Host.dispatch(fx, .vvol_it);
1953
+ try std.testing.expectEqual(@as(f32, 0.25), fx.videoSnapshot().volume);
1954
+ Host.dispatch(fx, .vload);
1955
+ try std.testing.expectEqual(@as(f32, 0.25), fx.pendingVideo().?.volume);
1956
+ }
1957
+
1958
+ test "stop cancels the stream: staged terminals never outlive it" {
1959
+ const fx = freshChannel();
1960
+ defer fx.deinit();
1961
+ Host.init(fx);
1962
+
1963
+ // The batch shape: a load whose terminal stages synchronously,
1964
+ // then stop closes the stream in the same cycle. `Cmd.videoStop`'s
1965
+ // wire contract is the stream's CANCEL — no events for the key
1966
+ // after this — so the stop drops the staged `.failed` inside the
1967
+ // engine with the player, and the next drain has nothing to say.
1968
+ Host.dispatch(fx, .vload);
1969
+ try fx.feedVideoEvent(.failed, 0, 0, false, false, 0, 0);
1970
+ Host.dispatch(fx, .vstop_it);
1971
+ try std.testing.expect(!fx.videoSnapshot().active);
1972
+
1973
+ // A load reusing the SAME event tag before the drain: the stopped
1974
+ // stream's cancelled terminal must not resurface through the
1975
+ // reopened tag — the cancel already removed it at the stop.
1976
+ Host.dispatch(fx, .vload);
1977
+ Host.drain(fx);
1978
+ try std.testing.expectEqual(@as(@TypeOf(Host.model().video_events), 0), Host.model().video_events);
1979
+
1980
+ // The fresh load's own events flow.
1981
+ try fx.feedVideoEvent(.loaded, 0, 12_000, true, false, 1920, 1080);
1982
+ Host.drain(fx);
1983
+ try std.testing.expectEqual(mini_core.VideoState.loaded, Host.model().video_state);
1984
+ try std.testing.expectEqual(@as(@TypeOf(Host.model().video_events), 1), Host.model().video_events);
1985
+ }
1986
+
1987
+ test "a rejected video_load keeps the live stream's routing and key gate" {
1988
+ const fx = freshChannel();
1989
+ defer fx.deinit();
1990
+ Host.init(fx);
1991
+
1992
+ Host.dispatch(fx, .vload);
1993
+ try std.testing.expect(fx.videoSnapshot().playing);
1994
+ try fx.feedVideoEvent(.loaded, 0, 12_000, true, false, 1920, 1080);
1995
+ Host.drain(fx);
1996
+ try std.testing.expectEqual(mini_core.VideoState.loaded, Host.model().video_state);
1997
+
1998
+ // An invalid replacement is refused before it can re-route the
1999
+ // single entry: the rejection reaches the app through the refused
2000
+ // record's own event arm...
2001
+ Host.dispatch(fx, .vload_bad);
2002
+ Host.drain(fx);
2003
+ try std.testing.expectEqual(mini_core.VideoState.rejected, Host.model().video_state);
2004
+ // ...while the surviving playback keeps its stream, its routing,
2005
+ // and its wire-key gate: events still deliver, and the ORIGINAL
2006
+ // key still drives the transport (a re-keyed entry would answer
2007
+ // to the refused key instead).
2008
+ try std.testing.expect(fx.videoSnapshot().playing);
2009
+ try fx.feedVideoEvent(.position, 1_000, 12_000, true, false, 0, 0);
2010
+ Host.drain(fx);
2011
+ try std.testing.expectEqual(mini_core.VideoState.position, Host.model().video_state);
2012
+ Host.dispatch(fx, .vpause_it);
2013
+ try std.testing.expect(!fx.videoSnapshot().playing);
2014
+ }
2015
+
2016
+ test "a replaced load's straggling terminal routes its own arm, not the replacement's" {
2017
+ const fx = freshChannel();
2018
+ defer fx.deinit();
2019
+ Host.init(fx);
2020
+
2021
+ // Open stream A and let its terminal stage: the fed `.failed`
2022
+ // carries A's key (the arm tag rides the low byte), and it is
2023
+ // still awaiting its drain when the replacing load re-keys the
2024
+ // bridge entry.
2025
+ Host.dispatch(fx, .vload);
2026
+ try fx.feedVideoEvent(.failed, 0, 0, false, false, 0, 0);
2027
+ Host.dispatch(fx, .vload2);
2028
+
2029
+ // The drain delivers A's terminal to A's arm (video_evt) and B's
2030
+ // stream keeps its own arm (video_evt2) — routing by the mutable
2031
+ // entry's tag would have handed A's failure to B.
2032
+ Host.drain(fx);
2033
+ try std.testing.expectEqual(mini_core.VideoState.failed, Host.model().video_state);
2034
+ try std.testing.expectEqual(@as(@TypeOf(Host.model().video2_events), 0), Host.model().video2_events);
2035
+
2036
+ try fx.feedVideoEvent(.loaded, 0, 8_000, true, false, 640, 360);
2037
+ Host.drain(fx);
2038
+ try std.testing.expectEqual(mini_core.VideoState.loaded, Host.model().video2_state);
2039
+ try std.testing.expectEqual(@as(@TypeOf(Host.model().video2_events), 1), Host.model().video2_events);
2040
+ }
2041
+
2042
+ test "video_ctl verbs drive the engine channel, gated by the wire key" {
2043
+ const fx = freshChannel();
2044
+ defer fx.deinit();
2045
+ Host.init(fx);
2046
+
2047
+ Host.dispatch(fx, .vload);
2048
+ try std.testing.expect(fx.videoSnapshot().playing);
2049
+
2050
+ // A verb aimed at a key that is not the open stream is a no-op.
2051
+ Host.dispatch(fx, .vctl_stray);
2052
+ try std.testing.expect(fx.videoSnapshot().playing);
2053
+
2054
+ Host.dispatch(fx, .vpause_it);
2055
+ try std.testing.expect(!fx.videoSnapshot().playing);
2056
+ Host.dispatch(fx, .vplay_it);
2057
+ try std.testing.expect(fx.videoSnapshot().playing);
2058
+
2059
+ Host.dispatch(fx, .vseek_it);
2060
+ try std.testing.expectEqual(@as(u64, 45_000), fx.videoSnapshot().position_ms);
2061
+
2062
+ // A finite offset past the exact-integer window is still a forward
2063
+ // seek: it saturates just below the window (the engine clamps it
2064
+ // to the duration once one is known) - never a rewind to zero.
2065
+ Host.dispatch(fx, .vseek_far);
2066
+ try std.testing.expectEqual(effects_mod.max_effect_video_scalar_exclusive - 1, fx.videoSnapshot().position_ms);
2067
+
2068
+ // Infinity is not a millisecond offset at all (the literal
2069
+ // validation rejects non-finite offsets): it seeks to 0 like NaN
2070
+ // and negatives, never to the end.
2071
+ Host.dispatch(fx, .vseek_inf);
2072
+ try std.testing.expectEqual(@as(u64, 0), fx.videoSnapshot().position_ms);
2073
+
2074
+ Host.dispatch(fx, .vvol_it);
2075
+ try std.testing.expectEqual(@as(f32, 0.25), fx.pendingVideo().?.volume);
2076
+
2077
+ Host.dispatch(fx, .vmute_it);
2078
+ try std.testing.expect(fx.videoSnapshot().muted);
2079
+ Host.dispatch(fx, .vloop_it);
2080
+ try std.testing.expect(fx.videoSnapshot().looping);
2081
+
2082
+ // stop closes the stream: the channel idles, the entry retires,
2083
+ // and a straggler feed finds nothing.
2084
+ Host.dispatch(fx, .vstop_it);
2085
+ try std.testing.expect(!fx.videoSnapshot().active);
2086
+ try std.testing.expectError(error.EffectNotFound, fx.feedVideoEvent(.position, 5_000, 12_000, true, false, 0, 0));
2087
+ }
2088
+
1365
2089
  test "boot commits the model before any effects and performBoot fires the boot command once" {
1366
2090
  const fx = freshChannel();
1367
2091
  defer fx.deinit();
@@ -1406,3 +2130,378 @@ test "a URL audio_play with no cache path derives the content-addressed path whe
1406
2130
  Host.dispatch(fx, .play_stream);
1407
2131
  try std.testing.expectEqualStrings("cache/a.mp3", fx.pendingAudio().?.cache_path);
1408
2132
  }
2133
+
2134
+ test "window verbs bridge to the effects channel's label-addressed verbs" {
2135
+ const fx = freshChannel();
2136
+ defer fx.deinit();
2137
+ Host.init(fx);
2138
+ const boot_pending = fx.pendingHostCount();
2139
+
2140
+ // window_show decodes onto fx.showWindow: under the fake executor
2141
+ // the mirror records the request — count and label — exactly the
2142
+ // Zig tier's contract, so replay and hermetic tests see the same
2143
+ // observable.
2144
+ Host.dispatch(fx, .open_win);
2145
+ try std.testing.expectEqual(@as(u32, 1), fx.windowActionState().show_count);
2146
+ try std.testing.expectEqualStrings("player", fx.windowActionState().lastLabel());
2147
+
2148
+ // quit_app decodes onto fx.quitApp — the graceful terminate request.
2149
+ Host.dispatch(fx, .quit_app);
2150
+ try std.testing.expectEqual(@as(u32, 1), fx.windowActionState().quit_count);
2151
+
2152
+ // Fire-and-forget: neither verb parked a keyed effect or dispatched
2153
+ // a result Msg of its own — only init's boot request is pending.
2154
+ try std.testing.expectEqual(boot_pending, fx.pendingHostCount());
2155
+ }
2156
+
2157
+ test "a channel opens, posts route the five-field arm by name, and close retires the key" {
2158
+ const fx = freshChannel();
2159
+ defer fx.deinit();
2160
+ Host.init(fx);
2161
+
2162
+ // channel_open decodes onto fx.openChannel under the raw numeric
2163
+ // key; the native side resolves the thread-safe posting handle —
2164
+ // exactly what an embedder does.
2165
+ Host.dispatch(fx, .open_chan);
2166
+ const handle = fx.channelHandle(41) orelse return error.TestExpectedHandle;
2167
+ try std.testing.expectEqual(effects_mod.ChannelHandle.PostResult.accepted, handle.post("cpu 42%"));
2168
+ Host.drain(fx);
2169
+ try std.testing.expectEqual(@as(i64, 1), Host.model().chan_events);
2170
+ try std.testing.expectEqual(mini_core.ChannelState.data, Host.model().chan_state);
2171
+ try std.testing.expectEqual(@as(f64, 41), Host.model().chan_key);
2172
+ try std.testing.expectEqualStrings("cpu 42%", Host.model().chan_bytes);
2173
+ try std.testing.expectEqual(@as(f64, 0), Host.model().chan_dropped_total);
2174
+
2175
+ // A duplicate LIVE key rejects the new open — the rejection Msg
2176
+ // stages into the engine's pending order and delivers at the next
2177
+ // drain, echoing the refused key; the live channel is untouched.
2178
+ Host.dispatch(fx, .open_chan);
2179
+ Host.drain(fx);
2180
+ try std.testing.expectEqual(@as(i64, 2), Host.model().chan_events);
2181
+ try std.testing.expectEqual(mini_core.ChannelState.rejected, Host.model().chan_state);
2182
+ try std.testing.expectEqual(@as(f64, 41), Host.model().chan_key);
2183
+ try std.testing.expect(fx.channelHandle(41) != null);
2184
+
2185
+ // close: the staged post flushes ahead of the one closed terminal,
2186
+ // which retires the entry and kills the handle.
2187
+ try std.testing.expectEqual(effects_mod.ChannelHandle.PostResult.accepted, handle.post("last reading"));
2188
+ Host.dispatch(fx, .close_chan);
2189
+ Host.drain(fx);
2190
+ try std.testing.expectEqual(@as(i64, 4), Host.model().chan_events);
2191
+ try std.testing.expectEqual(mini_core.ChannelState.closed, Host.model().chan_state);
2192
+ try std.testing.expectEqual(effects_mod.ChannelHandle.PostResult.closed, handle.post("too late"));
2193
+ try std.testing.expect(fx.channelHandle(41) == null);
2194
+
2195
+ // The key is free again: a fresh open lands with no rejection
2196
+ // (the drain delivers nothing new).
2197
+ Host.dispatch(fx, .open_chan);
2198
+ Host.drain(fx);
2199
+ try std.testing.expectEqual(@as(i64, 4), Host.model().chan_events);
2200
+ try std.testing.expect(fx.channelHandle(41) != null);
2201
+ }
2202
+
2203
+ test "a mixed refused batch dispatches its rejections in command-stream order" {
2204
+ const fx = freshChannel();
2205
+ defer fx.deinit();
2206
+ Host.init(fx);
2207
+
2208
+ // Park a live channel under key 41 and a live load under id 7 —
2209
+ // accepted issues dispatch no rejection Msg, so the probe is empty.
2210
+ Host.dispatch(fx, .open_chan);
2211
+ Host.dispatch(fx, .load_img);
2212
+ try std.testing.expectEqualStrings("", Host.model().order);
2213
+
2214
+ // Both records in one batch are refused (duplicate LIVE key/id).
2215
+ // Cmd.batch's contract: performed in order — the channel rejection
2216
+ // reaches update FIRST because its record came first. Rejections
2217
+ // deliver at the next drain, in the engine's one pending order.
2218
+ Host.dispatch(fx, .mix_chan_then_img);
2219
+ Host.drain(fx);
2220
+ try std.testing.expectEqualStrings("CI", Host.model().order);
2221
+ }
2222
+
2223
+ test "the reverse mixed refused batch keeps command-stream order (image first)" {
2224
+ const fx = freshChannel();
2225
+ defer fx.deinit();
2226
+ Host.init(fx);
2227
+
2228
+ Host.dispatch(fx, .open_chan);
2229
+ Host.dispatch(fx, .load_img);
2230
+
2231
+ Host.dispatch(fx, .mix_img_then_chan);
2232
+ Host.drain(fx);
2233
+ try std.testing.expectEqualStrings("IC", Host.model().order);
2234
+ }
2235
+
2236
+ test "an engine-refused open followed by a bridge-refused open delivers in command order" {
2237
+ const fx = freshChannel();
2238
+ defer fx.deinit();
2239
+ Host.init(fx);
2240
+
2241
+ // Image id 7 holds raw engine key 7 (the raw id IS the engine key).
2242
+ Host.dispatch(fx, .load_img);
2243
+ try std.testing.expectEqualStrings("", Host.model().order);
2244
+
2245
+ // Batch [channel_open 7 -> chan_evt ('C'), channel_open 7 ->
2246
+ // chan_evt2 ('D')]. The FIRST open passes the bridge (its table
2247
+ // tracks channel entries, not the cross-family raw key) and the
2248
+ // ENGINE refuses it — occupied by the image family. The SECOND is
2249
+ // refused by the bridge (duplicate in-flight channel key). Cmd.batch
2250
+ // is performed in order, so the rejection Msgs must land 'C' then
2251
+ // 'D' — the first open's refusal first, regardless of which layer
2252
+ // refused it.
2253
+ Host.dispatch(fx, .mix_dup_chan_over_img);
2254
+ Host.drain(fx);
2255
+ try std.testing.expectEqualStrings("CD", Host.model().order);
2256
+
2257
+ // The image occupant was never disturbed.
2258
+ try std.testing.expectEqual(@as(i64, 0), Host.model().img_events);
2259
+ }
2260
+
2261
+ test "a bridge-refused open followed by an engine-refused open delivers in command order" {
2262
+ const fx = freshChannel();
2263
+ defer fx.deinit();
2264
+ Host.init(fx);
2265
+
2266
+ // Channel 41 live in the bridge table, image id 7 on raw key 7.
2267
+ Host.dispatch(fx, .open_chan);
2268
+ Host.dispatch(fx, .load_img);
2269
+ try std.testing.expectEqualStrings("", Host.model().order);
2270
+
2271
+ // Batch [channel_open 41 -> chan_evt2 ('D'): bridge dup-key refusal,
2272
+ // channel_open 7 -> chan_evt ('C'): engine cross-family refusal].
2273
+ // Command order: 'D' then 'C'.
2274
+ Host.dispatch(fx, .mix_dup_then_engine);
2275
+ Host.drain(fx);
2276
+ try std.testing.expectEqualStrings("DC", Host.model().order);
2277
+
2278
+ // The live channel under 41 survived its dup refusal.
2279
+ try std.testing.expect(fx.channelHandle(41) != null);
2280
+ }
2281
+
2282
+ test "bridge-staged rejections journal nothing while the engine's journal marked regenerable" {
2283
+ const fx = freshChannel();
2284
+ defer fx.deinit();
2285
+
2286
+ const Capture = struct {
2287
+ var channel_records: usize = 0;
2288
+ var regenerable_channel_records: usize = 0;
2289
+ var last_channel_key: u64 = 0;
2290
+ fn record(context: *anyopaque, record_value: effects_mod.EffectResultRecord) void {
2291
+ _ = context;
2292
+ if (record_value.kind != .channel) return;
2293
+ channel_records += 1;
2294
+ last_channel_key = record_value.key;
2295
+ if (record_value.exit_reason == .rejected) regenerable_channel_records += 1;
2296
+ }
2297
+ };
2298
+ Capture.channel_records = 0;
2299
+ Capture.regenerable_channel_records = 0;
2300
+ var context: u8 = 0;
2301
+ fx.bindJournal(.{ .context = &context, .record_fn = Capture.record });
2302
+
2303
+ Host.init(fx);
2304
+ Host.dispatch(fx, .load_img);
2305
+
2306
+ // The mixed batch again: the first open engine-refused (image id 7
2307
+ // holds raw key 7), the second bridge-refused (duplicate in-flight
2308
+ // channel key). Exactly ONE channel record reaches the journal —
2309
+ // the ENGINE's, marked regenerable (`exit_reason` `.rejected`, the
2310
+ // channel records' provenance convention) so replay skips it and
2311
+ // the re-run open re-derives it. The bridge's rejection is a
2312
+ // caller-staged Msg (`stageLoopMsg`): never journaled, regenerated
2313
+ // by the replayed command walk itself — the journal carries
2314
+ // NEITHER as executor truth.
2315
+ Host.dispatch(fx, .mix_dup_chan_over_img);
2316
+ Host.drain(fx);
2317
+ try std.testing.expectEqualStrings("CD", Host.model().order);
2318
+ try std.testing.expectEqual(@as(usize, 1), Capture.channel_records);
2319
+ try std.testing.expectEqual(@as(usize, 1), Capture.regenerable_channel_records);
2320
+ try std.testing.expectEqual(@as(u64, 7), Capture.last_channel_key);
2321
+ }
2322
+
2323
+ test "a three-family refused batch pins full stream order across channel, image, and spawn" {
2324
+ const fx = freshChannel();
2325
+ defer fx.deinit();
2326
+ Host.init(fx);
2327
+
2328
+ // One live occupant per family: channel 41, image 7, spawn "job".
2329
+ Host.dispatch(fx, .open_chan);
2330
+ Host.dispatch(fx, .load_img);
2331
+ Host.dispatch(fx, .run_quiet);
2332
+ try std.testing.expectEqualStrings("", Host.model().order);
2333
+
2334
+ // channel_open ++ image_load ++ spawn, all refused: stream order.
2335
+ Host.dispatch(fx, .mix_three);
2336
+ Host.drain(fx);
2337
+ try std.testing.expectEqualStrings("CIS", Host.model().order);
2338
+ }
2339
+
2340
+ // -------------------------------------------------------- pty sessions
2341
+
2342
+ const shell_pty_key: u64 = ts_core_host.pty_key_base + 0;
2343
+
2344
+ test "a pty session decodes whole, routes output batches, and retires on the exit" {
2345
+ const fx = freshChannel();
2346
+ defer fx.deinit();
2347
+ Host.init(fx);
2348
+
2349
+ Host.dispatch(fx, .open_pty);
2350
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingPtyCount());
2351
+ const request = fx.pendingPtyAt(0).?;
2352
+ try std.testing.expectEqual(shell_pty_key, request.key);
2353
+ try std.testing.expectEqual(@as(usize, 2), request.argv.len);
2354
+ try std.testing.expectEqualStrings("/bin/zsh", request.argv[0]);
2355
+ try std.testing.expectEqualStrings("-l", request.argv[1]);
2356
+ try std.testing.expectEqual(@as(u16, 120), request.cols);
2357
+ try std.testing.expectEqual(@as(u16, 30), request.rows);
2358
+ try std.testing.expectEqualStrings("xterm-256color", request.term);
2359
+
2360
+ // The NON-RETIRING contract: output batches route the event arm
2361
+ // across separate drains (state matched by member NAME — the mini
2362
+ // core scrambles both unions' declaration order on purpose) and
2363
+ // the entry stays live between them.
2364
+ try fx.feedPtyOutput(shell_pty_key, "prompt% ");
2365
+ Host.drain(fx);
2366
+ try std.testing.expectEqual(@as(i64, 1), Host.model().pty_events);
2367
+ try std.testing.expectEqual(mini_core.PtyState.output, Host.model().pty_state);
2368
+ try std.testing.expectEqualStrings("prompt% ", Host.model().pty_bytes);
2369
+ // The arm carries the app's own session key, never the engine key —
2370
+ // two sessions on one arm are told apart by this field.
2371
+ try std.testing.expectEqualStrings("shell", Host.model().pty_key);
2372
+ try fx.feedPtyOutput(shell_pty_key, "ls\r\n");
2373
+ Host.drain(fx);
2374
+ try std.testing.expectEqual(@as(i64, 2), Host.model().pty_events);
2375
+ try std.testing.expectEqualStrings("ls\r\n", Host.model().pty_bytes);
2376
+
2377
+ // Exactly one terminal retires the entry: the exit routes the same
2378
+ // arm with the code/reason/drop counters aboard, and the key is
2379
+ // dead to further feeds.
2380
+ try fx.feedPtyExit(shell_pty_key, 0, 0, .exited, 2);
2381
+ Host.drain(fx);
2382
+ try std.testing.expectEqual(@as(i64, 3), Host.model().pty_events);
2383
+ try std.testing.expectEqual(mini_core.PtyState.exit, Host.model().pty_state);
2384
+ try std.testing.expectEqual(mini_core.PtyReason.exited, Host.model().pty_reason);
2385
+ try std.testing.expectEqual(@as(i64, 0), Host.model().pty_code);
2386
+ try std.testing.expectEqual(@as(f64, 2), Host.model().pty_dropped);
2387
+ try std.testing.expectError(error.EffectNotFound, fx.feedPtyOutput(shell_pty_key, "late"));
2388
+
2389
+ // The wire key is free again for a fresh session in the same slot.
2390
+ Host.dispatch(fx, .open_pty);
2391
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingPtyCount());
2392
+ try std.testing.expectEqual(shell_pty_key, fx.pendingPtyAt(0).?.key);
2393
+ }
2394
+
2395
+ test "an empty wire TERM opens with the engine default" {
2396
+ const fx = freshChannel();
2397
+ defer fx.deinit();
2398
+ Host.init(fx);
2399
+
2400
+ // Wire "" = "the engine's default TERM" — the record never bakes
2401
+ // the default in, the fetch-timeout convention.
2402
+ Host.dispatch(fx, .open_pty_default);
2403
+ const request = fx.pendingPtyAt(0).?;
2404
+ try std.testing.expectEqualStrings(@import("pty.zig").default_term, request.term);
2405
+ try std.testing.expectEqual(@as(u16, 80), request.cols);
2406
+ try std.testing.expectEqual(@as(u16, 24), request.rows);
2407
+ }
2408
+
2409
+ test "pty_write reaches the session and pty_resize mirrors the declared grid" {
2410
+ const fx = freshChannel();
2411
+ defer fx.deinit();
2412
+ Host.init(fx);
2413
+
2414
+ Host.dispatch(fx, .open_pty);
2415
+ Host.dispatch(fx, .write_pty);
2416
+ try std.testing.expectEqualStrings("ls\n", fx.ptyWrittenBytes(shell_pty_key));
2417
+
2418
+ Host.dispatch(fx, .resize_pty);
2419
+ const size = fx.ptySize(shell_pty_key).?;
2420
+ try std.testing.expectEqual(@as(u16, 100), size.cols);
2421
+ try std.testing.expectEqual(@as(u16, 40), size.rows);
2422
+
2423
+ // Both verbs are fire-and-forget: nothing queued, nothing routed.
2424
+ try std.testing.expect(fx.takeMsg() == null);
2425
+ try std.testing.expectEqual(@as(i64, 0), Host.model().pty_events);
2426
+
2427
+ // Aimed at a key with no open session they no-op: retire the
2428
+ // session, then re-issue both — the engine sees nothing.
2429
+ try fx.feedPtyExit(shell_pty_key, 0, 0, .exited, 0);
2430
+ Host.drain(fx);
2431
+ Host.dispatch(fx, .write_pty);
2432
+ Host.dispatch(fx, .resize_pty);
2433
+ try std.testing.expect(fx.takeMsg() == null);
2434
+ }
2435
+
2436
+ test "pty_kill records the kill and the cancelled exit routes the event arm loudly" {
2437
+ const fx = freshChannel();
2438
+ defer fx.deinit();
2439
+ Host.init(fx);
2440
+
2441
+ Host.dispatch(fx, .open_pty);
2442
+ Host.dispatch(fx, .kill_pty);
2443
+ // The fake pty mirrors the kill; the test answers it by feeding
2444
+ // the exit the real transport would deliver — reason `.cancelled`,
2445
+ // the spawn cancel convention, LOUD through the event arm. A kill is
2446
+ // a cancellation, not a signaled death, so it carries no signal and
2447
+ // the -1 code sentinel; the feed boundary clamps any stray signal to
2448
+ // 0 to match the live io loop's own contract.
2449
+ try std.testing.expect(fx.ptyKillRequested(shell_pty_key));
2450
+ try fx.feedPtyExit(shell_pty_key, -1, 9, .cancelled, 0);
2451
+ Host.drain(fx);
2452
+ try std.testing.expectEqual(mini_core.PtyState.exit, Host.model().pty_state);
2453
+ try std.testing.expectEqual(mini_core.PtyReason.cancelled, Host.model().pty_reason);
2454
+ try std.testing.expectEqual(@as(i64, 0), Host.model().pty_signal);
2455
+ try std.testing.expectEqual(@as(i64, -1), Host.model().pty_code);
2456
+
2457
+ // The entry retired and the key is free for a fresh session.
2458
+ Host.dispatch(fx, .open_pty);
2459
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingPtyCount());
2460
+
2461
+ // A kill aimed at a key with no open session no-ops.
2462
+ try fx.feedPtyExit(shell_pty_key, 0, 0, .exited, 0);
2463
+ Host.drain(fx);
2464
+ Host.dispatch(fx, .kill_pty);
2465
+ try std.testing.expect(fx.takeMsg() == null);
2466
+ }
2467
+
2468
+ test "a duplicate pty key rejects the new spawn through its event arm (the spawn exception)" {
2469
+ const fx = freshChannel();
2470
+ defer fx.deinit();
2471
+ Host.init(fx);
2472
+
2473
+ // A running terminal's child is a running subprocess: a live wire
2474
+ // key REJECTS the new spawn — kill it first. The rejection Msg
2475
+ // stages into the engine's pending order and delivers at the next
2476
+ // drain (the one rejection stream).
2477
+ Host.dispatch(fx, .dup_pty);
2478
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingPtyCount());
2479
+ try std.testing.expectEqualStrings("/bin/one", fx.pendingPtyAt(0).?.argv[0]);
2480
+ Host.drain(fx);
2481
+ try std.testing.expectEqual(@as(i64, 1), Host.model().pty_events);
2482
+ try std.testing.expectEqual(mini_core.PtyState.exit, Host.model().pty_state);
2483
+ try std.testing.expectEqual(mini_core.PtyReason.rejected, Host.model().pty_reason);
2484
+ // A STAGED rejection is delivered a frame after it is issued, past a
2485
+ // frame-arena reset, yet still carries the app's requested key — from
2486
+ // the durable reject ring, never a dangling frame-arena copy — so the
2487
+ // refusal is correlated with the "shell" spawn that caused it.
2488
+ try std.testing.expectEqualStrings("shell", Host.model().pty_key);
2489
+
2490
+ // The surviving session still delivers normally.
2491
+ try fx.feedPtyExit(shell_pty_key, 0, 0, .exited, 0);
2492
+ Host.drain(fx);
2493
+ try std.testing.expectEqual(mini_core.PtyReason.exited, Host.model().pty_reason);
2494
+ }
2495
+
2496
+ test "a spawn_failed transport end routes the event arm with the reason" {
2497
+ const fx = freshChannel();
2498
+ defer fx.deinit();
2499
+ Host.init(fx);
2500
+
2501
+ Host.dispatch(fx, .open_pty);
2502
+ try fx.feedPtyExit(shell_pty_key, -1, 0, .spawn_failed, 0);
2503
+ Host.drain(fx);
2504
+ try std.testing.expectEqual(mini_core.PtyState.exit, Host.model().pty_state);
2505
+ try std.testing.expectEqual(mini_core.PtyReason.spawn_failed, Host.model().pty_reason);
2506
+ try std.testing.expectEqual(@as(i64, -1), Host.model().pty_code);
2507
+ }