@native-sdk/cli 0.5.2 → 0.5.4

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 (123) hide show
  1. package/build/app.zig +41 -1
  2. package/build.zig +264 -3
  3. package/native-sdk.d.ts +17 -1
  4. package/package.json +9 -9
  5. package/packages/core/package-lock.json +2 -2
  6. package/packages/core/package.json +1 -1
  7. package/packages/core/rt/rt.zig +138 -3
  8. package/packages/core/sdk/core.ts +225 -3
  9. package/packages/core/sdk/events.ts +39 -2
  10. package/packages/core/src/checker.ts +1 -1
  11. package/packages/core/src/emitter.ts +2625 -262
  12. package/packages/core/src/infer.ts +53 -7
  13. package/skill-data/automation/SKILL.md +7 -4
  14. package/skill-data/core/references/app-model-runtime.md +3 -0
  15. package/skill-data/native-ui/SKILL.md +4 -2
  16. package/skill-data/ts-core/SKILL.md +9 -1
  17. package/src/app_runner/root.zig +106 -1
  18. package/src/app_runner/ts_core_main.zig +4 -0
  19. package/src/automation/protocol.zig +20 -1
  20. package/src/embed/host.zig +27 -0
  21. package/src/embed/tests.zig +187 -0
  22. package/src/embed/ui_host.zig +4 -0
  23. package/src/platform/linux/gtk_host.c +112 -3
  24. package/src/platform/linux/root.zig +71 -0
  25. package/src/platform/macos/appkit_host.h +59 -2
  26. package/src/platform/macos/appkit_host.m +663 -34
  27. package/src/platform/macos/cef_host.mm +262 -10
  28. package/src/platform/macos/root.zig +126 -4
  29. package/src/platform/null_platform.zig +298 -5
  30. package/src/platform/null_platform_tests.zig +39 -0
  31. package/src/platform/root.zig +36 -0
  32. package/src/platform/types.zig +247 -8
  33. package/src/platform/windows/root.zig +134 -0
  34. package/src/platform/windows/webview2_host.cpp +152 -40
  35. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  36. package/src/primitives/app_manifest/root.zig +1 -0
  37. package/src/primitives/app_manifest/types.zig +32 -0
  38. package/src/primitives/canvas/chart.zig +7 -7
  39. package/src/primitives/canvas/chart_tests.zig +66 -0
  40. package/src/primitives/canvas/commands.zig +48 -0
  41. package/src/primitives/canvas/events.zig +161 -0
  42. package/src/primitives/canvas/font_ttf.zig +155 -8
  43. package/src/primitives/canvas/font_ttf_tests.zig +686 -0
  44. package/src/primitives/canvas/reference.zig +67 -8
  45. package/src/primitives/canvas/render_fingerprints.zig +8 -0
  46. package/src/primitives/canvas/root.zig +25 -0
  47. package/src/primitives/canvas/tests.zig +1 -0
  48. package/src/primitives/canvas/ui.zig +46 -9
  49. package/src/primitives/canvas/ui_markup.zig +82 -34
  50. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  51. package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
  52. package/src/primitives/canvas/ui_markup_compiled.zig +63 -11
  53. package/src/primitives/canvas/ui_markup_compiled_tests.zig +88 -0
  54. package/src/primitives/canvas/ui_markup_contract.zig +10 -1
  55. package/src/primitives/canvas/ui_markup_contract_tests.zig +50 -0
  56. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  57. package/src/primitives/canvas/ui_markup_tests.zig +55 -9
  58. package/src/primitives/canvas/ui_markup_view.zig +81 -14
  59. package/src/primitives/canvas/ui_markup_view_tests.zig +219 -7
  60. package/src/primitives/canvas/ui_schema.zig +29 -0
  61. package/src/primitives/canvas/ui_schema_tests.zig +11 -11
  62. package/src/primitives/canvas/ui_tests.zig +31 -0
  63. package/src/primitives/canvas/vector.zig +258 -141
  64. package/src/primitives/canvas/widget_access.zig +1 -1
  65. package/src/primitives/canvas/widget_builtin_tests.zig +129 -0
  66. package/src/primitives/canvas/widget_layout.zig +9 -4
  67. package/src/primitives/canvas/widget_render.zig +78 -40
  68. package/src/primitives/canvas/widget_render_controls.zig +15 -4
  69. package/src/primitives/canvas/widget_semantics.zig +6 -1
  70. package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
  71. package/src/primitives/canvas/widget_text_input.zig +118 -10
  72. package/src/primitives/canvas/widgets.zig +16 -0
  73. package/src/root.zig +12 -0
  74. package/src/runtime/api.zig +15 -0
  75. package/src/runtime/automation_commands.zig +33 -0
  76. package/src/runtime/automation_widget_dispatch.zig +64 -0
  77. package/src/runtime/bridge_payload.zig +7 -0
  78. package/src/runtime/bridge_responses.zig +5 -0
  79. package/src/runtime/builtin_bridge.zig +9 -4
  80. package/src/runtime/canvas_font_tests.zig +711 -5
  81. package/src/runtime/canvas_fonts.zig +120 -25
  82. package/src/runtime/canvas_frame_helpers.zig +15 -0
  83. package/src/runtime/canvas_image_tests.zig +221 -0
  84. package/src/runtime/canvas_images.zig +78 -19
  85. package/src/runtime/canvas_limits.zig +44 -8
  86. package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
  87. package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
  88. package/src/runtime/canvas_widget_events.zig +24 -4
  89. package/src/runtime/canvas_widget_runtime.zig +24 -7
  90. package/src/runtime/canvas_widget_state.zig +6 -1
  91. package/src/runtime/canvas_widget_text_tests.zig +277 -0
  92. package/src/runtime/core.zig +148 -9
  93. package/src/runtime/effects.zig +1664 -43
  94. package/src/runtime/effects_fetch_tests.zig +89 -1
  95. package/src/runtime/effects_file_tests.zig +41 -1
  96. package/src/runtime/effects_image_tests.zig +1304 -0
  97. package/src/runtime/effects_tests.zig +153 -0
  98. package/src/runtime/flow.zig +44 -0
  99. package/src/runtime/gpu_surface_events.zig +7 -0
  100. package/src/runtime/media_surface.zig +726 -0
  101. package/src/runtime/media_surface_tests.zig +1476 -0
  102. package/src/runtime/root.zig +20 -0
  103. package/src/runtime/session_blobs.zig +689 -0
  104. package/src/runtime/session_journal.zig +111 -20
  105. package/src/runtime/session_record.zig +27 -2
  106. package/src/runtime/session_replay.zig +168 -16
  107. package/src/runtime/session_tests.zig +2118 -4
  108. package/src/runtime/shell_layout.zig +7 -0
  109. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  110. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  111. package/src/runtime/tests.zig +3 -0
  112. package/src/runtime/ts_core_host.zig +439 -4
  113. package/src/runtime/ts_core_host_tests.zig +41 -0
  114. package/src/runtime/ts_ui_app.zig +63 -0
  115. package/src/runtime/ui_app.zig +169 -6
  116. package/src/runtime/ui_app_tests.zig +334 -0
  117. package/src/runtime/ui_app_window_tests.zig +468 -0
  118. package/src/runtime/window_command_bridge_tests.zig +117 -0
  119. package/src/runtime/window_storage.zig +32 -0
  120. package/src/runtime/window_views.zig +61 -1
  121. package/src/tooling/manifest.zig +75 -0
  122. package/src/tooling/raw_manifest.zig +2 -0
  123. package/src/tooling/templates.zig +219 -1
@@ -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,50 @@
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
+ //! post-cycle boundary with the refused id echoed; ids
149
+ //! the wire cannot carry exactly (0, non-integers,
150
+ //! 2^53 and past — the SDK contract is BELOW 2^53)
151
+ //! reject the same way echoing id 0, and so does a
152
+ //! 17th in-flight load (a full bridge table).
153
+ //! Image loads are not the string-keyed cancel's to
154
+ //! end (they are keyed by numeric id, not a wire key)
155
+ //! — `image_cancel` is their cancel.
156
+ //! image_cancel-> `fx.cancel(id)` on the live load under the id, if
157
+ //! any: the engine's `.cancelled` terminal routes the
158
+ //! load's own event arm (state "cancelled") and
159
+ //! retires the entry, freeing the id for a fresh load
160
+ //! — LOUD, the spawn cancel discipline. An id naming
161
+ //! no live load (or one the wire cannot carry exactly)
162
+ //! is a no-op, audio_ctl's idle rule.
163
+ //! image_unregister
164
+ //! -> `fx.unregisterImage(id)`: free the registry slot
165
+ //! under the id — registration's synchronous
166
+ //! discipline in reverse, direct registry surgery
167
+ //! with no terminal and no Msg. An id with no
168
+ //! registration (or one the wire cannot carry
169
+ //! exactly) is a no-op, image_cancel's idle rule. It
170
+ //! never touches the load table: a load in flight
171
+ //! under the id keeps running and its terminal still
172
+ //! registers the pixels — cancel the load first to
173
+ //! keep the slot free.
130
174
  //! audio_ctl -> the engine's control verbs (`fx.pauseAudio`/
131
175
  //! `resumeAudio`/`stopAudio`/`seekAudio`/
132
176
  //! `setAudioVolume`), gated by the wire key: a verb
@@ -134,6 +178,13 @@
134
178
  //! (the playback it aimed at is gone). `stop` also
135
179
  //! retires the bridge entry — no events for that key
136
180
  //! after it.
181
+ //! window_show -> `fx.showWindow(label)` — fire-and-forget, label-
182
+ //! addressed like the Zig tier's verb: un-hide +
183
+ //! activate (the tray "Open" consequence of the
184
+ //! menu-bar-app loop). No result Msg; the window's own
185
+ //! frame event carries the state.
186
+ //! quit_app -> `fx.quitApp()` — the graceful terminate through the
187
+ //! same shutdown path a last-window close takes.
137
188
  //! cancel -> by wire key, first match wins in this order: the
138
189
  //! request table (`fx.cancelHostRequest`, silent), the
139
190
  //! named-op table (the entry is marked dropped and
@@ -361,6 +412,16 @@ pub fn TsCoreHost(comptime core: type) type {
361
412
  }
362
413
  };
363
414
 
415
+ /// One in-flight image load, keyed by the app's own numeric
416
+ /// ImageId (which IS the engine key — the registry id and the
417
+ /// effect key are one value by design). Retires on its one
418
+ /// terminal.
419
+ const ImageEntry = struct {
420
+ used: bool = false,
421
+ id: u64 = 0,
422
+ event_tag: u8 = 0,
423
+ };
424
+
364
425
  var model_root: *const Model = undefined;
365
426
  /// The platform caches directory for URL audio sources, set by
366
427
  /// the wiring (`TsUiApp`'s `audio_cache_dir`, or `setAudioCacheDir`
@@ -377,6 +438,14 @@ pub fn TsCoreHost(comptime core: type) type {
377
438
  var delays: [runtime_effects.max_effect_timers]DelayEntry = @splat(.{});
378
439
  var streams: [runtime_effects.max_effects]StreamEntry = @splat(.{});
379
440
  var audio_entry: AudioEntry = .{};
441
+ var images: [runtime_effects.max_effects]ImageEntry = @splat(.{});
442
+ /// The platform caches directory for URL image sources, the
443
+ /// audio cache dir's twin (`setImageCacheDir` / `TsUiApp`'s
444
+ /// `image_cache_dir`): bridge-side derivation of the
445
+ /// content-addressed cache path, never an env read in update.
446
+ var image_cache_dir_len: usize = 0;
447
+ var image_cache_dir_buf: [runtime_effects.max_effect_image_path_bytes]u8 = undefined;
448
+ var image_cache_path_buf: [runtime_effects.max_effect_image_path_bytes]u8 = undefined;
380
449
  var clip_write_counter: u32 = 0;
381
450
  /// Set by a result callback whose entry was dropped (replaced
382
451
  /// or wire-cancelled): the terminal is journal-visible but must
@@ -399,6 +468,84 @@ pub fn TsCoreHost(comptime core: type) type {
399
468
  /// dispatches use.
400
469
  const max_rejects_per_cmd: usize = runtime_effects.max_effects;
401
470
 
471
+ /// An image load the bridge itself refused (duplicate live id,
472
+ /// an unrepresentable id, a full image table), dispatched as the
473
+ /// load's event arm with state "rejected" at the same post-cycle
474
+ /// boundary. `id` echoes the requested ImageId so concurrent
475
+ /// rejections stay distinguishable — 0 when the wire value is
476
+ /// not an exactly-carried positive integer, because there is no
477
+ /// honest integer to echo for one.
478
+ const ImageReject = struct { tag: u8, id: u64 };
479
+
480
+ /// One cycle's staging for bridge-refused image loads. Unlike
481
+ /// the spawn-reject buffer, the count here is the APP's to
482
+ /// choose — a `Cmd.batch` of N loads against a full table must
483
+ /// yield N rejected results, never a crash — so the stage never
484
+ /// caps at the table size: the inline buffer covers everyday
485
+ /// command values without touching an allocator, and the first
486
+ /// overflow spills once into an engine-allocator block sized by
487
+ /// the wire's own arithmetic bound. Staging exists because the
488
+ /// command walk cannot dispatch (the wire bytes are frame-arena
489
+ /// resident, and a nested cycle's frame reset would free them
490
+ /// mid-walk): rejects are captured during the walk and
491
+ /// delivered after the issuing cycle's frame reset, at the same
492
+ /// boundary `now` dispatches use — stack and heap storage both
493
+ /// survive the nested delivery cycles that boundary runs.
494
+ const ImageRejectStage = struct {
495
+ /// Every image_load record occupies at least this many wire
496
+ /// bytes ([op][id f64][event_tag], three empty long-bytes
497
+ /// fields, [expected f64]), so a command value of L bytes
498
+ /// carries at most L / 30 of them — the spill bound needs
499
+ /// no second record parser to stay in sync with.
500
+ const min_image_load_record_bytes: usize = 1 + 8 + 1 + 3 * 4 + 8;
501
+
502
+ inline_buf: [max_rejects_per_cmd]ImageReject = undefined,
503
+ spill: []ImageReject = &.{},
504
+ count: usize = 0,
505
+ /// The arithmetic ceiling on this cycle's image_load record
506
+ /// count (and therefore on its rejects).
507
+ bound: usize,
508
+ allocator: std.mem.Allocator,
509
+
510
+ fn open(fx: *Fx, cmd_len: usize) ImageRejectStage {
511
+ return .{
512
+ .bound = cmd_len / min_image_load_record_bytes,
513
+ .allocator = fx.allocator,
514
+ };
515
+ }
516
+
517
+ fn push(self: *ImageRejectStage, tag: u8, id: u64) void {
518
+ if (self.count < self.inline_buf.len) {
519
+ self.inline_buf[self.count] = .{ .tag = tag, .id = id };
520
+ self.count += 1;
521
+ return;
522
+ }
523
+ if (self.spill.len == 0) {
524
+ // First overflow: size by the wire bound once and
525
+ // move the inline prefix over — one allocation for
526
+ // the whole cycle, and only on cycles that earn it.
527
+ self.spill = self.allocator.alloc(ImageReject, self.bound) catch
528
+ @panic("ts core host: out of memory staging rejected image loads");
529
+ @memcpy(self.spill[0..self.count], &self.inline_buf);
530
+ }
531
+ // The wire arithmetic sized the spill: rejects <= loads
532
+ // <= bound, so the store below can never run past it.
533
+ std.debug.assert(self.count < self.spill.len);
534
+ self.spill[self.count] = .{ .tag = tag, .id = id };
535
+ self.count += 1;
536
+ }
537
+
538
+ fn staged(self: *const ImageRejectStage) []const ImageReject {
539
+ if (self.spill.len > 0) return self.spill[0..self.count];
540
+ return self.inline_buf[0..self.count];
541
+ }
542
+
543
+ fn close(self: *ImageRejectStage) void {
544
+ if (self.spill.len > 0) self.allocator.free(self.spill);
545
+ self.spill = &.{};
546
+ }
547
+ };
548
+
402
549
  /// Install the core: reset the kernel and the bridge tables
403
550
  /// (deterministic re-init — the seam session replay relies on),
404
551
  /// run the core's `initialModel`, commit it, and perform the
@@ -427,8 +574,10 @@ pub fn TsCoreHost(comptime core: type) type {
427
574
  delays = @splat(.{});
428
575
  streams = @splat(.{});
429
576
  audio_entry = .{};
577
+ images = @splat(.{});
430
578
  clip_write_counter = 0;
431
579
  audio_cache_dir_len = 0;
580
+ image_cache_dir_len = 0;
432
581
  swallow_next_dispatch = false;
433
582
  const initial = core.initialModel();
434
583
  model_root = core.commitModelRoot(if (comptime init_returns_cmd) initial.model else initial);
@@ -486,6 +635,33 @@ pub fn TsCoreHost(comptime core: type) type {
486
635
  return runtime_effects.audioCachePath(&audio_cache_path_buf, audioCacheDir(), url) catch "";
487
636
  }
488
637
 
638
+ /// Configure the caches directory image-cache derivation uses —
639
+ /// `setAudioCacheDir`'s twin, same wiring-time contract.
640
+ pub fn setImageCacheDir(dir: []const u8) void {
641
+ if (dir.len > image_cache_dir_buf.len) {
642
+ @panic("ts core host: the image cache directory path is longer than the engine's image path bound");
643
+ }
644
+ @memcpy(image_cache_dir_buf[0..dir.len], dir);
645
+ image_cache_dir_len = dir.len;
646
+ }
647
+
648
+ fn imageCacheDir() []const u8 {
649
+ return image_cache_dir_buf[0..image_cache_dir_len];
650
+ }
651
+
652
+ /// The cache path an image_load record loads under: the
653
+ /// record's own when it carries one; otherwise, for URL sources
654
+ /// with a configured caches directory, the engine's
655
+ /// conventional content-addressed path
656
+ /// (`<cache_dir>/images/<sha256[..16]>.<ext>` via
657
+ /// `imageCachePath`) — `effectiveAudioCachePath`'s twin, a
658
+ /// fixed function of the wire bytes so replay re-derives
659
+ /// identically.
660
+ fn effectiveImageCachePath(cache_path: []const u8, url: []const u8) []const u8 {
661
+ if (cache_path.len > 0 or url.len == 0 or image_cache_dir_len == 0) return cache_path;
662
+ return runtime_effects.imageCachePath(&image_cache_path_buf, imageCacheDir(), url) catch "";
663
+ }
664
+
489
665
  /// One full dispatch cycle for `msg`. The `TsUiApp` adapter
490
666
  /// wires this to `UiApp.Options.update_fx` (refreshing the
491
667
  /// app-held root from `model()` afterwards) so host events and
@@ -504,8 +680,14 @@ pub fn TsCoreHost(comptime core: type) type {
504
680
  /// Drain every queued effect completion into the core — the
505
681
  /// bridge-shaped mirror of `UiApp.drainEffects`' loop. Hosts
506
682
  /// that embed the core without a UiApp call this on wake/frame.
683
+ /// Bounded to the completions that existed at entry, exactly
684
+ /// like `UiApp.drainEffects`: a completion produced by a
685
+ /// dispatch inside this pass waits for the wake its producer
686
+ /// already nudged, keeping the session journal's event
687
+ /// boundaries causal (see `Effects.DrainBoundary`).
507
688
  pub fn drain(fx: *Fx) void {
508
- while (fx.takeMsg()) |msg| dispatch(fx, msg);
689
+ var boundary = fx.drainBoundary();
690
+ while (fx.takeMsgWithin(&boundary)) |msg| dispatch(fx, msg);
509
691
  }
510
692
 
511
693
  fn dispatchDepth(fx: *Fx, msg: Msg, depth: usize) void {
@@ -533,7 +715,9 @@ pub fn TsCoreHost(comptime core: type) type {
533
715
  var now_count: usize = 0;
534
716
  var rejects: [max_rejects_per_cmd]u8 = undefined;
535
717
  var reject_count: usize = 0;
536
- runCmd(fx, cmd, &nows, &now_count, &rejects, &reject_count);
718
+ var image_rejects = ImageRejectStage.open(fx, cmd.len);
719
+ defer image_rejects.close();
720
+ runCmd(fx, cmd, &nows, &now_count, &rejects, &reject_count, &image_rejects);
537
721
  reconcileTimers(fx);
538
722
  core.rt.frameReset();
539
723
  for (nows[0..now_count]) |pending| {
@@ -542,11 +726,19 @@ pub fn TsCoreHost(comptime core: type) type {
542
726
  for (rejects[0..reject_count]) |err_tag| {
543
727
  dispatchDepth(fx, msgFromTagBytes(err_tag, "rejected"), depth + 1);
544
728
  }
729
+ // Bridge-refused image loads (duplicate live id, an
730
+ // unrepresentable id, a full image table): the event arm's
731
+ // "rejected" state echoing the refused id, regenerated
732
+ // deterministically under replay like the spawn rejections
733
+ // above.
734
+ for (image_rejects.staged()) |reject| {
735
+ dispatchDepth(fx, msgFromTagImage(reject.tag, .{ .id = reject.id, .outcome = .rejected }), depth + 1);
736
+ }
545
737
  }
546
738
 
547
739
  // ------------------------------------------------- command walk
548
740
 
549
- /// Walk one command value (v2 wire format; batch is plain
741
+ /// Walk one command value (v3 wire format; batch is plain
550
742
  /// concatenation, the empty slice is `Cmd.none`).
551
743
  fn runCmd(
552
744
  fx: *Fx,
@@ -555,6 +747,7 @@ pub fn TsCoreHost(comptime core: type) type {
555
747
  now_count: *usize,
556
748
  rejects: *[max_rejects_per_cmd]u8,
557
749
  reject_count: *usize,
750
+ image_rejects: *ImageRejectStage,
558
751
  ) void {
559
752
  var at: usize = 0;
560
753
  while (at < cmd.len) {
@@ -754,6 +947,39 @@ pub fn TsCoreHost(comptime core: type) type {
754
947
  const value: f64 = @bitCast(std.mem.readInt(u64, value_bits[0..8], .little));
755
948
  runAudioCtl(fx, key, verb, value);
756
949
  },
950
+ // window_show [op][label_len][label]
951
+ 0x10 => {
952
+ const label = takeShortBytes(cmd, &at);
953
+ fx.showWindow(label);
954
+ },
955
+ // quit_app [op]
956
+ 0x11 => fx.quitApp(),
957
+ // image_load [op][id f64 LE][event_tag]
958
+ // [path_len u32 LE][path][url_len u32 LE][url]
959
+ // [cache_len u32 LE][cache][expected f64 LE]
960
+ 0x12 => {
961
+ const id_bits = takeBytes(cmd, &at, 8);
962
+ const id_value: f64 = @bitCast(std.mem.readInt(u64, id_bits[0..8], .little));
963
+ const event_tag = takeByte(cmd, &at);
964
+ const image_path = takeLongBytes(cmd, &at);
965
+ const url = takeLongBytes(cmd, &at);
966
+ const cache_path = takeLongBytes(cmd, &at);
967
+ const expected_bits = takeBytes(cmd, &at, 8);
968
+ const expected: f64 = @bitCast(std.mem.readInt(u64, expected_bits[0..8], .little));
969
+ issueImageLoad(fx, id_value, event_tag, image_path, url, cache_path, expected, image_rejects);
970
+ },
971
+ // image_cancel [op][id f64 LE]
972
+ 0x13 => {
973
+ const id_bits = takeBytes(cmd, &at, 8);
974
+ const id_value: f64 = @bitCast(std.mem.readInt(u64, id_bits[0..8], .little));
975
+ runImageCancel(fx, id_value);
976
+ },
977
+ // image_unregister [op][id f64 LE]
978
+ 0x14 => {
979
+ const id_bits = takeBytes(cmd, &at, 8);
980
+ const id_value: f64 = @bitCast(std.mem.readInt(u64, id_bits[0..8], .little));
981
+ runImageUnregister(fx, id_value);
982
+ },
757
983
  else => @panic("ts core host: unknown command wire record - the core and this runtime disagree on cmd_format_version"),
758
984
  }
759
985
  }
@@ -1016,6 +1242,154 @@ pub fn TsCoreHost(comptime core: type) type {
1016
1242
  return msgFromTagAudio(audio_entry.event_tag, event);
1017
1243
  }
1018
1244
 
1245
+ /// Issue one image load. The keyed-effect discipline here is
1246
+ /// the SPAWN exception, by the same reasoning: one load per id
1247
+ /// at a time, never replaced implicitly — a duplicate LIVE id
1248
+ /// rejects the new load (event arm, state "rejected", at the
1249
+ /// post-cycle boundary), and so do an id the f64 wire cannot
1250
+ /// honestly carry into the u64 registry (0, negatives,
1251
+ /// fractions, 2^53 and past) and a 17th in-flight load (the table
1252
+ /// mirrors the engine's max_effects slots, whose own exhaustion
1253
+ /// answer is the same rejected result). Everything else the
1254
+ /// engine refuses dynamically (a full registry, a bad source)
1255
+ /// comes back through the entry's own event arm — never silent.
1256
+ fn issueImageLoad(
1257
+ fx: *Fx,
1258
+ id_value: f64,
1259
+ event_tag: u8,
1260
+ image_path: []const u8,
1261
+ url: []const u8,
1262
+ cache_path: []const u8,
1263
+ expected: f64,
1264
+ image_rejects: *ImageRejectStage,
1265
+ ) void {
1266
+ // Strictly BELOW 2^53 (the SDK contract): at 2^53 the f64
1267
+ // grid steps by 2, so 2^53 is the first value that aliases
1268
+ // a neighbor (2^53 + 1) on the wire — the bridge rejects it
1269
+ // rather than guess which integer the app meant. 2^53 - 1
1270
+ // is the last id every tier carries exactly.
1271
+ const representable = std.math.isFinite(id_value) and
1272
+ id_value >= 1 and id_value < 9007199254740992.0 and
1273
+ @floor(id_value) == id_value;
1274
+ if (!representable) {
1275
+ image_rejects.push(event_tag, 0);
1276
+ return;
1277
+ }
1278
+ const id: u64 = @intFromFloat(id_value);
1279
+ if (findImage(id) != null) {
1280
+ image_rejects.push(event_tag, id);
1281
+ return;
1282
+ }
1283
+ const index = freeImageIndex() orelse {
1284
+ // All 16 entries hold live loads — one gallery screen's
1285
+ // Cmd.batch reaches this. The engine answers its own
1286
+ // slot exhaustion with a dynamic `.rejected` result, and
1287
+ // the audio channel's exhaustion story is a quiet
1288
+ // in-place replace; a full bridge table speaks the same
1289
+ // vocabulary: exactly one rejected result through the
1290
+ // event arm, never a crash — however many loads one
1291
+ // batch stages against it.
1292
+ image_rejects.push(event_tag, id);
1293
+ return;
1294
+ };
1295
+ const entry = &images[index];
1296
+ entry.used = true;
1297
+ entry.id = id;
1298
+ entry.event_tag = event_tag;
1299
+ fx.loadImage(.{
1300
+ .id = id,
1301
+ .path = image_path,
1302
+ .url = url,
1303
+ .cache_path = effectiveImageCachePath(cache_path, url),
1304
+ // The wire carries the app's number; anything that is
1305
+ // not a representable WHOLE byte count — fractional,
1306
+ // out of range, NaN — means "unknown size" (0), the
1307
+ // engine's own default. The integer clause matters:
1308
+ // @intFromFloat would truncate 1.5 to 1, and the cache
1309
+ // would then verify downloads against a size the app
1310
+ // never declared — re-fetching on every launch. The
1311
+ // bound is strictly BELOW 2^53, the id gate's: 2^53 is
1312
+ // the first f64 that aliases a neighbor (2^53 + 1
1313
+ // arrives as the same wire value), so there is no one
1314
+ // honest count to install — it maps to "unknown" with
1315
+ // the fractionals rather than becoming a verification
1316
+ // size every real download misses. 0 is the honest
1317
+ // mapping; the emitter already stops the literal
1318
+ // spellings (NS1030).
1319
+ .expected_bytes = if (expected >= 1 and expected < 9007199254740992.0 and @floor(expected) == expected)
1320
+ @intFromFloat(expected)
1321
+ else
1322
+ 0,
1323
+ .on_result = imageResultMsg,
1324
+ });
1325
+ }
1326
+
1327
+ fn findImage(id: u64) ?usize {
1328
+ for (&images, 0..) |*entry, index| {
1329
+ if (entry.used and entry.id == id) return index;
1330
+ }
1331
+ return null;
1332
+ }
1333
+
1334
+ fn freeImageIndex() ?usize {
1335
+ for (&images, 0..) |*entry, index| {
1336
+ if (!entry.used) return index;
1337
+ }
1338
+ return null;
1339
+ }
1340
+
1341
+ /// The image_cancel record: end the in-flight load under the
1342
+ /// id, if any, LOUDLY — the engine delivers the load's one
1343
+ /// terminal as `.cancelled`, which routes the entry's own event
1344
+ /// arm through `imageResultMsg` and retires the entry (freeing
1345
+ /// the id for a fresh load), the spawn cancel discipline. An id
1346
+ /// naming no live entry — or one the wire cannot carry exactly,
1347
+ /// which no load could ever park under — is a no-op, the same
1348
+ /// idle no-op audio_ctl keeps: whatever it aimed at is already
1349
+ /// gone. The raw id IS the engine key (image loads never mint a
1350
+ /// bridge namespace), and every bridge key base sits above 2^53,
1351
+ /// so the cancel can never reach another table's slot.
1352
+ fn runImageCancel(fx: *Fx, id_value: f64) void {
1353
+ const representable = std.math.isFinite(id_value) and
1354
+ id_value >= 1 and id_value < 9007199254740992.0 and
1355
+ @floor(id_value) == id_value;
1356
+ if (!representable) return;
1357
+ const id: u64 = @intFromFloat(id_value);
1358
+ if (findImage(id) == null) return;
1359
+ fx.cancel(id);
1360
+ }
1361
+
1362
+ /// The image_unregister record: free the registry slot under
1363
+ /// the id — direct registry surgery, registration's synchronous
1364
+ /// discipline in reverse (no terminal, no Msg; the engine call
1365
+ /// answers a bool the wire has no channel to carry, and a miss
1366
+ /// means whatever it aimed at is already gone — image_cancel's
1367
+ /// idle no-op). The bridge's load table is deliberately NOT
1368
+ /// consulted: unregister targets the REGISTRY, and a load in
1369
+ /// flight under the id is not a registry occupant — its
1370
+ /// terminal registers as usual, re-occupying the id, so an app
1371
+ /// that wants the slot to STAY free cancels the load first. An
1372
+ /// id the wire cannot carry exactly could never have been
1373
+ /// registered through this bridge, so it no-ops the same way.
1374
+ fn runImageUnregister(fx: *Fx, id_value: f64) void {
1375
+ const representable = std.math.isFinite(id_value) and
1376
+ id_value >= 1 and id_value < 9007199254740992.0 and
1377
+ @floor(id_value) == id_value;
1378
+ if (!representable) return;
1379
+ const id: u64 = @intFromFloat(id_value);
1380
+ _ = fx.unregisterImage(id);
1381
+ }
1382
+
1383
+ /// `ImageMsgFn` for image loads: the ONE terminal routes the
1384
+ /// entry's event arm and retires the entry.
1385
+ fn imageResultMsg(result: runtime_effects.EffectImageResult) Msg {
1386
+ const index = findImage(result.id) orelse
1387
+ @panic("ts core host: an image result arrived with no open bridge entry");
1388
+ const entry = &images[index];
1389
+ entry.used = false;
1390
+ return msgFromTagImage(entry.event_tag, result);
1391
+ }
1392
+
1019
1393
  /// The wire `cancel` record: first match wins across the four
1020
1394
  /// keyed tables — requests (silent drop), named engine ops
1021
1395
  /// (silent drop: the entry is marked dropped, the engine's
@@ -1456,6 +1830,67 @@ pub fn TsCoreHost(comptime core: type) type {
1456
1830
  @panic("ts core host: an audio event names a Msg tag outside the union");
1457
1831
  }
1458
1832
 
1833
+ /// The five-field image result record, matched by field name —
1834
+ /// `audioArmShape`'s twin.
1835
+ fn imageArmShape(comptime T: type) bool {
1836
+ const info = @typeInfo(T);
1837
+ if (info != .@"struct") return false;
1838
+ const fields = info.@"struct".fields;
1839
+ if (fields.len != 5) return false;
1840
+ var ok = true;
1841
+ for (fields) |f| {
1842
+ if (std.mem.eql(u8, f.name, "state")) {
1843
+ if (@typeInfo(f.type) != .@"enum") ok = false;
1844
+ } 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")) {
1845
+ if (f.type != i64 and f.type != f64) ok = false;
1846
+ } else {
1847
+ ok = false;
1848
+ }
1849
+ }
1850
+ return ok;
1851
+ }
1852
+
1853
+ /// The arm's `state` member for an engine image outcome,
1854
+ /// matched by member NAME — `audioStateValue`'s twin.
1855
+ fn imageStateValue(comptime E: type, outcome: runtime_effects.EffectImageOutcome) E {
1856
+ const name = @tagName(outcome);
1857
+ inline for (@typeInfo(E).@"enum".fields) |f| {
1858
+ if (std.mem.eql(u8, f.name, name)) return @enumFromInt(f.value);
1859
+ }
1860
+ @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");
1861
+ }
1862
+
1863
+ /// Build the five-field image result arm at index `tag` from an
1864
+ /// engine result, by field name. `id` is the requested ImageId
1865
+ /// echoed verbatim (always below 2^53 — the bridge refused
1866
+ /// anything wider — so both number classes carry it exactly).
1867
+ fn msgFromTagImage(tag: u8, result: runtime_effects.EffectImageResult) Msg {
1868
+ inline for (msg_arms, 0..) |arm, index| {
1869
+ if (tag == index) {
1870
+ if (comptime imageArmShape(arm.type)) {
1871
+ const fields = @typeInfo(arm.type).@"struct".fields;
1872
+ var payload: arm.type = undefined;
1873
+ inline for (fields) |f| {
1874
+ if (comptime std.mem.eql(u8, f.name, "state")) {
1875
+ @field(payload, f.name) = imageStateValue(f.type, result.outcome);
1876
+ } else if (comptime std.mem.eql(u8, f.name, "id")) {
1877
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(result.id) else @intCast(result.id);
1878
+ } else if (comptime std.mem.eql(u8, f.name, "width")) {
1879
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(result.width) else @intCast(result.width);
1880
+ } else if (comptime std.mem.eql(u8, f.name, "height")) {
1881
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(result.height) else @intCast(result.height);
1882
+ } else {
1883
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(result.status) else @intCast(result.status);
1884
+ }
1885
+ }
1886
+ return @unionInit(Msg, arm.name, payload);
1887
+ }
1888
+ @panic("ts core host: an image result targets Msg arm '" ++ arm.name ++ "', which is not the five-field image result record");
1889
+ }
1890
+ }
1891
+ @panic("ts core host: an image result names a Msg tag outside the union");
1892
+ }
1893
+
1459
1894
  /// Build the Msg arm at index `tag` carrying one number (`now`
1460
1895
  /// timestamps and timer fires; an i64-classed arm truncates the
1461
1896
  /// way the subset's number model does at index sites).
@@ -145,6 +145,8 @@ const mini_core = struct {
145
145
  drop_save, // 40: cancel "save" (silent write_file drop)
146
146
  drop_get, // 41: cancel "get" (silent fetch drop)
147
147
  drop_paste, // 42: cancel "paste" (silent clip_read drop)
148
+ open_win, // 43: window_show "player" (the tray Open consequence)
149
+ quit_app, // 44: quit_app (the tray Quit consequence)
148
150
  };
149
151
 
150
152
  pub const InitResult = struct { model: *const Model, cmd: []const u8 };
@@ -322,6 +324,8 @@ const mini_core = struct {
322
324
  .drop_save => return .{ .model = model, .cmd = cmdCancel("save") },
323
325
  .drop_get => return .{ .model = model, .cmd = cmdCancel("get") },
324
326
  .drop_paste => return .{ .model = model, .cmd = cmdCancel("paste") },
327
+ .open_win => return .{ .model = model, .cmd = cmdWindowShow("player") },
328
+ .quit_app => return .{ .model = model, .cmd = cmdQuitApp() },
325
329
  }
326
330
  }
327
331
 
@@ -524,6 +528,20 @@ const mini_core = struct {
524
528
  return out;
525
529
  }
526
530
 
531
+ fn cmdWindowShow(label: []const u8) []const u8 {
532
+ const out = rt.frameAlloc(u8, 2 + label.len);
533
+ out[0] = 0x10;
534
+ out[1] = @intCast(label.len);
535
+ @memcpy(out[2..][0..label.len], label);
536
+ return out;
537
+ }
538
+
539
+ fn cmdQuitApp() []const u8 {
540
+ const out = rt.frameAlloc(u8, 1);
541
+ out[0] = 0x11;
542
+ return out;
543
+ }
544
+
527
545
  fn subTimer(key: []const u8, every_ms: f64, msg_tag: u8) []const u8 {
528
546
  const out = rt.frameAlloc(u8, 2 + key.len + 8 + 1);
529
547
  out[0] = 0x01;
@@ -1406,3 +1424,26 @@ test "a URL audio_play with no cache path derives the content-addressed path whe
1406
1424
  Host.dispatch(fx, .play_stream);
1407
1425
  try std.testing.expectEqualStrings("cache/a.mp3", fx.pendingAudio().?.cache_path);
1408
1426
  }
1427
+
1428
+ test "window verbs bridge to the effects channel's label-addressed verbs" {
1429
+ const fx = freshChannel();
1430
+ defer fx.deinit();
1431
+ Host.init(fx);
1432
+ const boot_pending = fx.pendingHostCount();
1433
+
1434
+ // window_show decodes onto fx.showWindow: under the fake executor
1435
+ // the mirror records the request — count and label — exactly the
1436
+ // Zig tier's contract, so replay and hermetic tests see the same
1437
+ // observable.
1438
+ Host.dispatch(fx, .open_win);
1439
+ try std.testing.expectEqual(@as(u32, 1), fx.windowActionState().show_count);
1440
+ try std.testing.expectEqualStrings("player", fx.windowActionState().lastLabel());
1441
+
1442
+ // quit_app decodes onto fx.quitApp — the graceful terminate request.
1443
+ Host.dispatch(fx, .quit_app);
1444
+ try std.testing.expectEqual(@as(u32, 1), fx.windowActionState().quit_count);
1445
+
1446
+ // Fire-and-forget: neither verb parked a keyed effect or dispatched
1447
+ // a result Msg of its own — only init's boot request is pending.
1448
+ try std.testing.expectEqual(boot_pending, fx.pendingHostCount());
1449
+ }