@native-sdk/cli 0.8.3 → 0.9.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 (188) hide show
  1. package/build/app.zig +905 -61
  2. package/build.zig +674 -373
  3. package/build.zig.zon +1 -0
  4. package/native-sdk.d.ts +6 -0
  5. package/package.json +12 -11
  6. package/packages/core/compile-surface/core.ts +455 -3
  7. package/packages/core/package-lock.json +11 -11
  8. package/packages/core/package.json +2 -2
  9. package/packages/core/scripts/compiler_typecheck.mjs +116 -15
  10. package/packages/core/scripts/gen_service_surface.mjs +268 -0
  11. package/packages/core/scripts/npm_static_spike.mjs +64 -0
  12. package/packages/core/scripts/run_external_core_compiler.mjs +77 -6
  13. package/packages/core/scripts/run_external_service_compiler.mjs +240 -0
  14. package/packages/core/scripts/stage_external_core.mjs +9 -1
  15. package/packages/core/scripts/stage_external_services.mjs +172 -0
  16. package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
  17. package/packages/core/scripts/vendor_service_packages.mjs +189 -0
  18. package/packages/core/sdk/core.d.ts +214 -2
  19. package/packages/core/sdk/core.ts +715 -42
  20. package/packages/core/sdk/events.d.ts +50 -0
  21. package/packages/core/sdk/events.ts +75 -0
  22. package/packages/core/src/checker.ts +528 -15
  23. package/packages/core/src/cli.ts +143 -3
  24. package/packages/core/src/contract.ts +26 -5
  25. package/packages/core/src/devhost.ts +1849 -18
  26. package/packages/core/src/devhost_journal.mjs +181 -0
  27. package/packages/core/src/devhost_resolver.mjs +17 -1
  28. package/packages/core/src/devhost_service_worker.mjs +90 -0
  29. package/packages/core/src/diagnostics.ts +84 -18
  30. package/packages/core/src/frontend.ts +204 -34
  31. package/packages/core/src/modules.ts +129 -80
  32. package/packages/core/src/service_contract.ts +597 -0
  33. package/packages/core/src/sqlite_cli.ts +93 -0
  34. package/packages/core/src/sqlite_codegen.ts +806 -0
  35. package/packages/core/src/sqlite_runtime_policy.ts +364 -0
  36. package/packages/core/src/typed_ast.ts +36 -8
  37. package/skill-data/core/SKILL.md +4 -2
  38. package/skill-data/core/references/project-anatomy.md +3 -2
  39. package/skill-data/core/references/web-engines-packaging-debugging.md +3 -1
  40. package/skill-data/native-ui/SKILL.md +55 -8
  41. package/skill-data/ts-core/SKILL.md +92 -40
  42. package/skill-data/ts-services/SKILL.md +126 -0
  43. package/skill-data/ts-services/references/service-surface.md +603 -0
  44. package/skill-data/zig/SKILL.md +1 -1
  45. package/skills/native-sdk/SKILL.md +1 -1
  46. package/src/app_runner/no_migrations.zig +3 -0
  47. package/src/app_runner/no_services.zig +20 -0
  48. package/src/app_runner/root.zig +104 -4
  49. package/src/app_runner/ts_core_main.zig +268 -4
  50. package/src/automation/snapshot.zig +23 -5
  51. package/src/embed/app_exports.zig +11 -1
  52. package/src/embed/c_api.zig +48 -2
  53. package/src/embed/conversions.zig +4 -5
  54. package/src/embed/host.zig +115 -1
  55. package/src/embed/root.zig +7 -0
  56. package/src/embed/tests.zig +213 -0
  57. package/src/embed/types.zig +51 -0
  58. package/src/embed/ui_host.zig +121 -5
  59. package/src/platform/android/NativeSdkActivity.java +131 -0
  60. package/src/platform/android/android_host.c +182 -1
  61. package/src/platform/android/native_sdk_app.h +17 -0
  62. package/src/platform/ios/native_sdk_app.h +17 -0
  63. package/src/platform/ios/uikit_host.m +115 -0
  64. package/src/platform/linux/cef_host.cpp +1 -1
  65. package/src/platform/linux/gtk_host.c +89 -17
  66. package/src/platform/linux/gtk_host.h +3 -0
  67. package/src/platform/linux/root.zig +41 -18
  68. package/src/platform/macos/appkit_host.h +26 -3
  69. package/src/platform/macos/appkit_host.m +1051 -129
  70. package/src/platform/macos/capture_info_plist.c +25 -0
  71. package/src/platform/macos/cef_host.mm +270 -36
  72. package/src/platform/macos/root.zig +312 -34
  73. package/src/platform/null_platform.zig +281 -19
  74. package/src/platform/null_platform_tests.zig +74 -0
  75. package/src/platform/root.zig +16 -0
  76. package/src/platform/types.zig +279 -15
  77. package/src/platform/windows/root.zig +243 -21
  78. package/src/platform/windows/webview2_host.cpp +464 -46
  79. package/src/primitives/app_manifest/comptime_scene.zig +2 -0
  80. package/src/primitives/app_manifest/root.zig +4 -0
  81. package/src/primitives/app_manifest/tests.zig +3 -0
  82. package/src/primitives/app_manifest/types.zig +34 -0
  83. package/src/primitives/app_manifest/validation.zig +19 -1
  84. package/src/primitives/canvas/code_tests.zig +9 -9
  85. package/src/primitives/canvas/events.zig +12 -11
  86. package/src/primitives/canvas/frame.zig +44 -9
  87. package/src/primitives/canvas/icons/mic.svg +1 -0
  88. package/src/primitives/canvas/icons.zig +2 -0
  89. package/src/primitives/canvas/root.zig +8 -0
  90. package/src/primitives/canvas/text_span_tests.zig +39 -0
  91. package/src/primitives/canvas/ui.zig +23 -4
  92. package/src/primitives/canvas/ui_markup.zig +15 -0
  93. package/src/primitives/canvas/ui_markup_compiled.zig +9 -0
  94. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1 -1
  95. package/src/primitives/canvas/ui_markup_tests.zig +5 -0
  96. package/src/primitives/canvas/ui_markup_view.zig +9 -0
  97. package/src/primitives/canvas/ui_markup_view_tests.zig +9 -6
  98. package/src/primitives/canvas/ui_schema.zig +19 -10
  99. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  100. package/src/primitives/canvas/ui_tests.zig +18 -4
  101. package/src/primitives/canvas/widget_invalidation.zig +9 -2
  102. package/src/primitives/canvas/widget_render.zig +296 -114
  103. package/src/primitives/canvas/widget_runtime_tests.zig +133 -0
  104. package/src/primitives/canvas/widget_text_input.zig +10 -10
  105. package/src/primitives/canvas/widget_tree.zig +1 -1
  106. package/src/primitives/canvas/widgets.zig +29 -5
  107. package/src/root.zig +56 -0
  108. package/src/runtime/api.zig +11 -0
  109. package/src/runtime/automation_snapshot.zig +4 -0
  110. package/src/runtime/automation_widget_dispatch.zig +1 -0
  111. package/src/runtime/bridge_payload.zig +2 -0
  112. package/src/runtime/bridge_permission_tests.zig +23 -2
  113. package/src/runtime/bridge_responses.zig +3 -1
  114. package/src/runtime/canvas_frame.zig +31 -13
  115. package/src/runtime/canvas_frame_animation_tests.zig +4 -0
  116. package/src/runtime/canvas_frame_invalidation_tests.zig +35 -10
  117. package/src/runtime/canvas_widget_anchored_tests.zig +83 -0
  118. package/src/runtime/canvas_widget_events.zig +69 -24
  119. package/src/runtime/canvas_widget_runtime.zig +101 -6
  120. package/src/runtime/canvas_widget_scroll_driver_tests.zig +47 -4
  121. package/src/runtime/canvas_widget_scroll_drivers.zig +3 -3
  122. package/src/runtime/canvas_widget_semantics_tests.zig +2 -0
  123. package/src/runtime/canvas_widget_split_tree_tests.zig +228 -0
  124. package/src/runtime/canvas_widget_state.zig +6 -1
  125. package/src/runtime/canvas_widget_text_tests.zig +112 -3
  126. package/src/runtime/clock.zig +13 -1
  127. package/src/runtime/core.zig +133 -1
  128. package/src/runtime/credentials_store.zig +115 -0
  129. package/src/runtime/effects.zig +2568 -75
  130. package/src/runtime/effects_audio_capture_tests.zig +251 -0
  131. package/src/runtime/effects_credentials_tests.zig +332 -0
  132. package/src/runtime/effects_host_tests.zig +208 -1
  133. package/src/runtime/effects_relational_tests.zig +314 -0
  134. package/src/runtime/effects_store_tests.zig +211 -0
  135. package/src/runtime/flow.zig +14 -1
  136. package/src/runtime/gpu_surface_events.zig +6 -1
  137. package/src/runtime/native_command_event_tests.zig +42 -0
  138. package/src/runtime/persist_store.zig +666 -0
  139. package/src/runtime/record_store.zig +788 -0
  140. package/src/runtime/relational_store.zig +854 -0
  141. package/src/runtime/root.zig +52 -0
  142. package/src/runtime/runtime_event_tests.zig +70 -0
  143. package/src/runtime/service_host.zig +711 -0
  144. package/src/runtime/service_pool.zig +1314 -0
  145. package/src/runtime/session_blobs.zig +1 -1
  146. package/src/runtime/session_journal.zig +102 -0
  147. package/src/runtime/session_record.zig +177 -0
  148. package/src/runtime/session_replay.zig +109 -4
  149. package/src/runtime/shell_layout.zig +1 -0
  150. package/src/runtime/shell_layout_tests.zig +120 -0
  151. package/src/runtime/sqlite_engine.zig +474 -0
  152. package/src/runtime/state.zig +6 -0
  153. package/src/runtime/system_services.zig +49 -10
  154. package/src/runtime/tests.zig +5 -0
  155. package/src/runtime/ts_core_host.zig +1189 -118
  156. package/src/runtime/ts_core_host_tests.zig +638 -11
  157. package/src/runtime/ts_ui_app.zig +537 -1
  158. package/src/runtime/ui_app.zig +160 -19
  159. package/src/runtime/ui_app_tests.zig +203 -5
  160. package/src/runtime/validation.zig +22 -0
  161. package/src/runtime/view.zig +11 -0
  162. package/src/runtime/view_widget_scroll.zig +56 -5
  163. package/src/runtime/view_widget_text.zig +24 -5
  164. package/src/runtime/view_widget_tree.zig +1 -1
  165. package/src/runtime/window_storage.zig +1 -0
  166. package/src/runtime/window_view_runtime.zig +2 -3
  167. package/src/runtime/window_views.zig +19 -0
  168. package/src/tooling/android.zig +21 -16
  169. package/src/tooling/db.zig +160 -0
  170. package/src/tooling/ios.zig +7 -1
  171. package/src/tooling/manifest.zig +1248 -2
  172. package/src/tooling/package.zig +953 -6
  173. package/src/tooling/raw_manifest.zig +65 -0
  174. package/src/tooling/root.zig +1 -0
  175. package/src/tooling/templates.zig +216 -4
  176. package/src/tooling/ts_core.zig +283 -17
  177. package/src/tooling/verbs.zig +7 -0
  178. package/third_party/sqlite/README.md +13 -0
  179. package/third_party/sqlite/sqlite3.c +269649 -0
  180. package/third_party/sqlite/sqlite3.h +14349 -0
  181. package/tools/corewire/core_abi.zig +14 -2
  182. package/tools/corewire/emit.zig +98 -29
  183. package/tools/corewire/emit_facade.zig +414 -23
  184. package/tools/corewire/emit_profile.zig +6 -2
  185. package/tools/corewire/emit_service.zig +924 -0
  186. package/tools/corewire/main.zig +122 -0
  187. package/tools/corewire/service_contract.zig +250 -0
  188. package/tools/corewire/sidecar.zig +26 -15
@@ -41,7 +41,15 @@
41
41
  //! Everything else on `UiApp.Options` is the wiring's, unchanged: view
42
42
  //! or markup, scene, `on_command` maps command ids through the core's
43
43
  //! `commandMsg`, `tokens_fn`/`windows_fn`/`status_item_fn` derive from
44
- //! the committed model. The two seams the core owns — `update_fx` and
44
+ //! the committed model. One model-helper convention joins that wiring:
45
+ //! an exported `themePack(model): "house" | "geist"` helper selects the
46
+ //! stock pack live through `theme_fn`, without taking ownership of the
47
+ //! system appearance axes. An exported
48
+ //! `statusItem(model): StatusItemState` helper similarly owns the complete
49
+ //! menu-bar item through `status_item_fn`: install-time icon/tooltip/click
50
+ //! hooks plus the live presentation and menu. In hand wiring, empty
51
+ //! install-time fields inherit the static `status_item` options. The two
52
+ //! seams the core owns — `update_fx` and
45
53
  //! `init_fx` — are stamped by this adapter and must be left null.
46
54
  //!
47
55
  //! `Options.sync` is deliberately unsupported: it mutates the model in
@@ -60,6 +68,9 @@
60
68
  const std = @import("std");
61
69
  const canvas = @import("canvas");
62
70
  const platform = @import("../platform/root.zig");
71
+ const runtime_core = @import("core.zig");
72
+ const runtime_effects = @import("effects.zig");
73
+ const persist_store = @import("persist_store.zig");
63
74
  const ui_app = @import("ui_app.zig");
64
75
  const ts_core_host = @import("ts_core_host.zig");
65
76
 
@@ -76,6 +87,10 @@ pub fn TsUiApp(comptime core: type) type {
76
87
  pub const Effects = App.Effects;
77
88
  pub const Ui = App.Ui;
78
89
 
90
+ /// Internal keyed-channel namespace for persistence write failures
91
+ /// ("TSPR"). It never shares an app-authored TS bridge index.
92
+ const persist_outcome_channel_key: u64 = 0x5453_5052_0000_0001;
93
+
79
94
  /// One boot-registered image: the wiring reads the encoded bytes
80
95
  /// (app.zon's `.assets.images` paths) and the adapter registers
81
96
  /// them on the installing frame — `fx.registerImageBytes`, the
@@ -99,6 +114,132 @@ pub fn TsUiApp(comptime core: type) type {
99
114
  value: []const u8,
100
115
  };
101
116
 
117
+ pub const PersistRoutes = struct {
118
+ ok: []const u8,
119
+ none: []const u8,
120
+ err: []const u8,
121
+ };
122
+
123
+ pub const PersistRestore = struct {
124
+ outcome: persist_store.Outcome,
125
+ bytes: []const u8 = "",
126
+ migration_from_version: ?u64 = null,
127
+ };
128
+
129
+ pub const PersistOptions = struct {
130
+ binding: runtime_effects.HostCallBinding,
131
+ routes: PersistRoutes,
132
+ restore: PersistRestore,
133
+ /// Runner-owned slot populated during install. The store worker
134
+ /// posts failed writes through it, gaining ordinary effect wake,
135
+ /// ordering, journaling, and replay semantics.
136
+ outcome_handle: ?*runtime_effects.ChannelHandle = null,
137
+ };
138
+
139
+ /// One effects host binding must carry both app services and the
140
+ /// framework-owned persistence verbs when an app enables both. The
141
+ /// reserved persistence names route to that binding; every other
142
+ /// command and the worker completion lifecycle stay with the app
143
+ /// service carrier.
144
+ const HostCallMux = struct {
145
+ primary: runtime_effects.HostCallBinding,
146
+ persist: runtime_effects.HostCallBinding,
147
+
148
+ fn binding(self: *HostCallMux) runtime_effects.HostCallBinding {
149
+ return .{
150
+ .context = self,
151
+ .send_fn = send,
152
+ .request_fn = request,
153
+ .cancel_fn = cancel,
154
+ .reject_duplicate_keys = self.primary.reject_duplicate_keys,
155
+ .poll_fn = poll,
156
+ .pending_fn = pending,
157
+ .bind_services_fn = bindServices,
158
+ .bind_channels_fn = bindChannels,
159
+ .shutdown_fn = shutdown,
160
+ };
161
+ }
162
+
163
+ fn persistenceName(name: []const u8) bool {
164
+ return std.mem.eql(u8, name, "core.persist") or std.mem.eql(u8, name, "core.persist.flush");
165
+ }
166
+
167
+ fn send(context: *anyopaque, name: []const u8, payload: []const u8) void {
168
+ const self: *HostCallMux = @ptrCast(@alignCast(context));
169
+ const target = if (persistenceName(name)) self.persist else self.primary;
170
+ target.send_fn(target.context, name, payload);
171
+ }
172
+
173
+ fn request(context: *anyopaque, name: []const u8, key: u64, payload: []const u8) void {
174
+ const self: *HostCallMux = @ptrCast(@alignCast(context));
175
+ const target = if (persistenceName(name)) self.persist else self.primary;
176
+ target.request_fn(target.context, name, key, payload);
177
+ }
178
+
179
+ fn cancel(context: *anyopaque, key: u64) void {
180
+ const self: *HostCallMux = @ptrCast(@alignCast(context));
181
+ if (self.primary.cancel_fn) |cancel_fn| cancel_fn(self.primary.context, key);
182
+ }
183
+
184
+ fn poll(context: *anyopaque) ?runtime_effects.HostCallCompletion {
185
+ const self: *HostCallMux = @ptrCast(@alignCast(context));
186
+ const poll_fn = self.primary.poll_fn orelse return null;
187
+ return poll_fn(self.primary.context);
188
+ }
189
+
190
+ fn pending(context: *anyopaque) bool {
191
+ const self: *HostCallMux = @ptrCast(@alignCast(context));
192
+ const pending_fn = self.primary.pending_fn orelse return false;
193
+ return pending_fn(self.primary.context);
194
+ }
195
+
196
+ fn bindServices(context: *anyopaque, services: *const platform.PlatformServices) void {
197
+ const self: *HostCallMux = @ptrCast(@alignCast(context));
198
+ if (self.primary.bind_services_fn) |bind_fn| bind_fn(self.primary.context, services);
199
+ if (self.persist.bind_services_fn) |bind_fn| bind_fn(self.persist.context, services);
200
+ }
201
+
202
+ fn bindChannels(context: *anyopaque, channels: runtime_effects.HostChannelBinding) void {
203
+ const self: *HostCallMux = @ptrCast(@alignCast(context));
204
+ if (self.primary.bind_channels_fn) |bind_fn| bind_fn(self.primary.context, channels);
205
+ if (self.persist.bind_channels_fn) |bind_fn| bind_fn(self.persist.context, channels);
206
+ }
207
+
208
+ fn shutdown(context: *anyopaque) void {
209
+ const self: *HostCallMux = @ptrCast(@alignCast(context));
210
+ if (self.primary.shutdown_fn) |shutdown_fn| shutdown_fn(self.primary.context);
211
+ if (self.persist.shutdown_fn) |shutdown_fn| shutdown_fn(self.persist.context);
212
+ }
213
+ };
214
+
215
+ /// Build-time manifest/wire fence for the three persistence routes.
216
+ /// The generated runner calls this with app.zon's comptime strings so
217
+ /// a typo or payload mismatch fails during `native build`, before a
218
+ /// first boot can reach the dynamic dispatch path below.
219
+ pub fn validatePersistRoutes(comptime routes: PersistRoutes) void {
220
+ validatePersistRoute(routes.ok, void, "ok");
221
+ validatePersistRoute(routes.none, void, "none");
222
+ validatePersistRoute(routes.err, []const u8, "err");
223
+ }
224
+
225
+ fn validatePersistRoute(comptime route: []const u8, comptime Payload: type, comptime role: []const u8) void {
226
+ inline for (@typeInfo(Msg).@"union".fields) |arm| {
227
+ if (comptime std.mem.eql(u8, arm.name, route)) {
228
+ if (arm.type != Payload) {
229
+ @compileError(std.fmt.comptimePrint(
230
+ "persistence restore route `{s}` ({s}) has the wrong Msg payload; ok/none must be void and err must carry one Uint8Array field",
231
+ .{ route, role },
232
+ ));
233
+ }
234
+ return;
235
+ }
236
+ }
237
+ @compileError(std.fmt.comptimePrint(
238
+ "persistence restore route `{s}` ({s}) names no Msg arm",
239
+ .{ route, role },
240
+ ));
241
+ }
242
+
102
243
  /// Adapter-owned configuration — the knobs that exist because
103
244
  /// the core is TypeScript, kept separate from `App.Options` so
104
245
  /// the wiring surface reads as ordinary UiApp wiring.
@@ -125,6 +266,14 @@ pub fn TsUiApp(comptime core: type) type {
125
266
  /// meaningful for cores exporting `envMsgs`. The slices must
126
267
  /// outlive install.
127
268
  env_values: []const EnvValue = &.{},
269
+ /// Generated service registry/carrier for Cmd.host/request. Null
270
+ /// preserves the existing embedding-host behavior.
271
+ host_calls: ?runtime_effects.HostCallBinding = null,
272
+ /// Typed service result projection generated from the same
273
+ /// sidecar as `host_calls`. Null keeps raw Cmd.request byte
274
+ /// routing for embedders and apps without services.
275
+ service_results: ?Host.ServiceResultBinding = null,
276
+ persist: ?PersistOptions = null,
128
277
  };
129
278
 
130
279
  /// Construct the UiApp over the committed TS model. `options`
@@ -159,15 +308,30 @@ pub fn TsUiApp(comptime core: type) type {
159
308
  /// installing frame.
160
309
  var boot_images_store: []const BootImage = &.{};
161
310
  var env_values_store: []const EnvValue = &.{};
311
+ var host_calls_store: ?runtime_effects.HostCallBinding = null;
312
+ var persist_options_store: ?PersistOptions = null;
313
+ var host_call_mux_store: ?HostCallMux = null;
314
+ var lifecycle_store: ?*const fn (event: runtime_core.LifecycleEvent) ?Msg = null;
315
+ var lifecycle_flush_after_update = false;
162
316
 
163
317
  fn applyCoreOptions(core_options: CoreOptions) void {
164
318
  Host.setAudioCacheDir(core_options.audio_cache_dir);
165
319
  Host.setImageCacheDir(core_options.image_cache_dir);
320
+ Host.bindServiceResults(core_options.service_results);
166
321
  boot_images_store = core_options.boot_images;
167
322
  env_values_store = core_options.env_values;
323
+ host_calls_store = core_options.host_calls;
324
+ persist_options_store = core_options.persist;
325
+ host_call_mux_store = if (core_options.host_calls != null and core_options.persist != null) .{
326
+ .primary = core_options.host_calls.?,
327
+ .persist = core_options.persist.?.binding,
328
+ } else null;
168
329
  if (core_options.env_values.len > 0 and comptime !@hasDecl(core, "envMsgs")) {
169
330
  @panic("TsUiApp received env_values but the core exports no envMsgs channel - declare `export const envMsgs = [{ env: \"NAME\", msg: \"<arm>\" }] as const` in core.ts");
170
331
  }
332
+ if (core_options.persist != null and comptime !@hasDecl(core, "restoreModel")) {
333
+ @panic("TsUiApp received persistence wiring but the generated core exposes no restoreModel entry - rebuild with core ABI version 2");
334
+ }
171
335
  }
172
336
 
173
337
  fn stampOptions(options: Options) Options {
@@ -181,8 +345,36 @@ pub fn TsUiApp(comptime core: type) type {
181
345
  @panic("TsUiApp does not support Options.sync: a committed model cannot be mutated in place - echo widget state through on-change/on-scroll Msgs instead");
182
346
  }
183
347
  var stamped = options;
348
+ lifecycle_store = stamped.on_lifecycle;
349
+ lifecycle_flush_after_update = false;
350
+ stamped.on_lifecycle = lifecycleAdapter;
184
351
  stamped.init_fx = initFx;
185
352
  stamped.update_fx = updateFx;
353
+ // A TypeScript core can select a built-in pack from ordinary
354
+ // model state without ejecting the generated launcher. The
355
+ // exported single-model helper emits as a Model method, so it
356
+ // is equally visible on direct compiler output and the
357
+ // external-core mirror. The app owns only the pack; UiApp's
358
+ // stock-token path keeps following the OS appearance.
359
+ if (comptime @hasDecl(Model, "themePack")) {
360
+ if (options.theme_fn != null) {
361
+ @panic("TsUiApp wires theme_fn from the core's themePack helper - remove the wiring's theme_fn");
362
+ }
363
+ comptime validateThemePackHelper();
364
+ stamped.theme_fn = themePackAdapter;
365
+ }
366
+ // A statusItem helper is the TS app's model-derived shell
367
+ // declaration. UiApp installs it on the first frame and
368
+ // independently patches presentation/menu after each
369
+ // committed rebuild; empty install fields inherit custom
370
+ // static status_item options in hand wiring.
371
+ if (comptime @hasDecl(Model, "statusItem")) {
372
+ if (options.status_item_fn != null) {
373
+ @panic("TsUiApp wires status_item_fn from the core's statusItem helper - remove the wiring's status_item_fn");
374
+ }
375
+ comptime validateStatusItemHelper();
376
+ stamped.status_item_fn = statusItemAdapter;
377
+ }
186
378
  // The core's host-event channels, comptime-detected from its
187
379
  // exports (export exists -> wired; every shape mismatch is a
188
380
  // teaching compile error in the adapter below). A wiring that
@@ -227,23 +419,363 @@ pub fn TsUiApp(comptime core: type) type {
227
419
  return stamped;
228
420
  }
229
421
 
422
+ fn lifecycleAdapter(event: runtime_core.LifecycleEvent) ?Msg {
423
+ const mapped = if (lifecycle_store) |map| map(event) else null;
424
+ if (event != .deactivate and event != .stop) return mapped;
425
+ if (mapped != null) {
426
+ // UiApp dispatches the mapped Msg after this callback returns.
427
+ // Delay the flush until updateFx has committed that cycle, so
428
+ // a Cmd.persist issued by the lifecycle update is included.
429
+ lifecycle_flush_after_update = true;
430
+ } else {
431
+ flushPersistence();
432
+ }
433
+ return mapped;
434
+ }
435
+
436
+ fn flushPersistence() void {
437
+ if (persist_options_store) |persist| {
438
+ persist.binding.send_fn(persist.binding.context, "core.persist.flush", "");
439
+ }
440
+ }
441
+
442
+ fn themePackAdapter(model: *const Model) canvas.ThemePack {
443
+ const pack = model.themePack();
444
+ return canvas.ThemePack.fromName(@tagName(pack)).?;
445
+ }
446
+
447
+ fn validateThemePackHelper() void {
448
+ const teaching = "TsUiApp: themePack must be exported from core.ts as themePack(model: Model): ThemePack, where ThemePack is exactly \"house\" | \"geist\"";
449
+ const helper_info = @typeInfo(@TypeOf(Model.themePack));
450
+ if (helper_info != .@"fn") @compileError(teaching);
451
+ const function = helper_info.@"fn";
452
+ if (function.params.len != 1 or function.params[0].type == null or function.params[0].type.? != *const Model) {
453
+ @compileError(teaching);
454
+ }
455
+ const Pack = function.return_type orelse @compileError(teaching);
456
+ const pack_info = @typeInfo(Pack);
457
+ if (pack_info != .@"enum" or pack_info.@"enum".fields.len != 2 or
458
+ !@hasField(Pack, "house") or !@hasField(Pack, "geist"))
459
+ {
460
+ @compileError(teaching);
461
+ }
462
+ }
463
+
464
+ /// Convert the compiled core's canonical status-item records into
465
+ /// the platform rows UiApp already knows how to validate, copy,
466
+ /// hash, install, and patch. Interface records cross the core ABI
467
+ /// by pointer; value records are accepted too so hand-assembled
468
+ /// compiler fixtures exercise the same seam.
469
+ fn statusItemAdapter(model: *const Model, scratch: *App.StatusItemScratch) App.StatusItemState {
470
+ const params = @typeInfo(@TypeOf(Model.statusItem)).@"fn".params;
471
+ const raw_state = if (comptime params.len == 1)
472
+ model.statusItem()
473
+ else
474
+ model.statusItem(core.rt.frameAllocator());
475
+ const state = if (comptime @typeInfo(@TypeOf(raw_state)) == .pointer) raw_state.* else raw_state;
476
+ if (state.items.len > scratch.items.len) {
477
+ // The callback cannot return an error. Feed UiApp one
478
+ // deliberately invalid actionable row so the ordinary
479
+ // tray validator rejects the over-capacity declaration
480
+ // loudly instead of silently truncating it.
481
+ scratch.items[0] = .{ .id = 0, .label = "status item has more than 32 rows", .command = "status-item-overflow" };
482
+ return statusItemState(state, scratch.items[0..1]);
483
+ }
484
+ for (state.items, 0..) |raw_item, index| {
485
+ const item = if (comptime @typeInfo(@TypeOf(raw_item)) == .pointer) raw_item.* else raw_item;
486
+ scratch.items[index] = .{
487
+ .id = statusItemId(item.id),
488
+ .label = item.label,
489
+ .command = item.command,
490
+ .separator = item.separator,
491
+ .enabled = item.enabled,
492
+ .detail = item.detail,
493
+ .role = statusItemRole(item.role),
494
+ .key = item.key,
495
+ .modifiers = .{
496
+ .primary = item.modifiers.primary,
497
+ .command = item.modifiers.command,
498
+ .control = item.modifiers.control,
499
+ .option = item.modifiers.option,
500
+ .shift = item.modifiers.shift,
501
+ },
502
+ };
503
+ }
504
+ return statusItemState(state, scratch.items[0..state.items.len]);
505
+ }
506
+
507
+ fn statusItemState(state: anytype, items: []const platform.TrayMenuItem) App.StatusItemState {
508
+ const presentation = if (comptime @typeInfo(@TypeOf(state.presentation)) == .pointer) state.presentation.* else state.presentation;
509
+ return .{
510
+ .presentation = .{
511
+ .title = presentation.title,
512
+ .width = statusItemFloat(presentation.width),
513
+ .tone = statusItemTone(presentation.tone),
514
+ .icon_opacity = statusItemFloat(presentation.iconOpacity),
515
+ .monospaced = presentation.monospaced,
516
+ },
517
+ .icon_path = state.iconPath,
518
+ .tooltip = state.tooltip,
519
+ .activation_command = state.activationCommand,
520
+ .alternate_activation_command = state.alternateActivationCommand,
521
+ .open_command = state.openCommand,
522
+ .items = items,
523
+ };
524
+ }
525
+
526
+ /// JavaScript `number` slots may infer as integer or float in the
527
+ /// compiled core. Tray ids are u32; a negative, fractional,
528
+ /// non-finite, or overflowing value maps to zero so the runtime's
529
+ /// existing actionable-row validation produces the rejection.
530
+ fn statusItemId(value: anytype) u32 {
531
+ const number: f64 = switch (@typeInfo(@TypeOf(value))) {
532
+ .int => @floatFromInt(value),
533
+ .float => @floatCast(value),
534
+ else => unreachable,
535
+ };
536
+ if (!std.math.isFinite(number) or number < 0 or number > @as(f64, @floatFromInt(std.math.maxInt(u32))) or @floor(number) != number) return 0;
537
+ return @intFromFloat(number);
538
+ }
539
+
540
+ fn statusItemFloat(value: anytype) f32 {
541
+ return switch (@typeInfo(@TypeOf(value))) {
542
+ .int => @floatFromInt(value),
543
+ .float => @floatCast(value),
544
+ else => unreachable,
545
+ };
546
+ }
547
+
548
+ fn statusItemTone(value: anytype) platform.TrayTone {
549
+ const name = @tagName(value);
550
+ inline for (std.meta.fields(platform.TrayTone)) |field| {
551
+ if (std.mem.eql(u8, name, field.name)) return @enumFromInt(field.value);
552
+ }
553
+ unreachable;
554
+ }
555
+
556
+ fn statusItemRole(value: anytype) platform.TrayItemRole {
557
+ const name = @tagName(value);
558
+ inline for (std.meta.fields(platform.TrayItemRole)) |field| {
559
+ if (std.mem.eql(u8, name, field.name)) return @enumFromInt(field.value);
560
+ }
561
+ unreachable;
562
+ }
563
+
564
+ /// Teaching re-derivation of frontend NS1033 for hand-assembled
565
+ /// cores. The nested records stay flat and exact so their ABI
566
+ /// projection is deterministic and arrays need no tagged union.
567
+ fn validateStatusItemHelper() void {
568
+ const teaching = "TsUiApp: statusItem must be exported from core.ts as statusItem(model: Model): StatusItemState; import StatusItemState from @native-sdk/core/events";
569
+ const helper_info = @typeInfo(@TypeOf(Model.statusItem));
570
+ if (helper_info != .@"fn") @compileError(teaching);
571
+ const function = helper_info.@"fn";
572
+ if ((function.params.len != 1 and function.params.len != 2) or function.params[0].type == null or function.params[0].type.? != *const Model) {
573
+ @compileError(teaching);
574
+ }
575
+ if (function.params.len == 2) {
576
+ if (function.params[1].type == null or function.params[1].type.? != std.mem.Allocator or
577
+ !@hasDecl(core, "rt") or !@hasDecl(core.rt, "frameAllocator"))
578
+ {
579
+ @compileError(teaching);
580
+ }
581
+ }
582
+ const RawState = function.return_type orelse @compileError(teaching);
583
+ const State = statusItemRecordType(RawState, teaching);
584
+ const state_info = @typeInfo(State).@"struct";
585
+ if (state_info.fields.len != 7 or !@hasField(State, "iconPath") or !@hasField(State, "tooltip") or
586
+ !@hasField(State, "activationCommand") or !@hasField(State, "alternateActivationCommand") or
587
+ !@hasField(State, "openCommand") or !@hasField(State, "presentation") or !@hasField(State, "items"))
588
+ {
589
+ @compileError(teaching);
590
+ }
591
+ if (@FieldType(State, "iconPath") != []const u8 or @FieldType(State, "tooltip") != []const u8 or
592
+ @FieldType(State, "activationCommand") != []const u8 or @FieldType(State, "alternateActivationCommand") != []const u8 or
593
+ @FieldType(State, "openCommand") != []const u8)
594
+ {
595
+ @compileError(teaching);
596
+ }
597
+ const Presentation = statusItemRecordType(@FieldType(State, "presentation"), teaching);
598
+ const presentation_info = @typeInfo(Presentation).@"struct";
599
+ if (presentation_info.fields.len != 5 or !@hasField(Presentation, "title") or !@hasField(Presentation, "width") or
600
+ !@hasField(Presentation, "tone") or !@hasField(Presentation, "iconOpacity") or !@hasField(Presentation, "monospaced"))
601
+ {
602
+ @compileError(teaching);
603
+ }
604
+ if (@FieldType(Presentation, "title") != []const u8 or !statusItemNumericType(@FieldType(Presentation, "width")) or
605
+ !statusItemEnumType(@FieldType(Presentation, "tone"), &.{ "normal", "warning", "critical" }) or
606
+ !statusItemNumericType(@FieldType(Presentation, "iconOpacity")) or @FieldType(Presentation, "monospaced") != bool)
607
+ {
608
+ @compileError(teaching);
609
+ }
610
+ const items_info = @typeInfo(@FieldType(State, "items"));
611
+ if (items_info != .pointer or items_info.pointer.size != .slice or !items_info.pointer.is_const) @compileError(teaching);
612
+ const Item = statusItemRecordType(items_info.pointer.child, teaching);
613
+ const item_info = @typeInfo(Item).@"struct";
614
+ if (item_info.fields.len != 9 or !@hasField(Item, "id") or !@hasField(Item, "label") or
615
+ !@hasField(Item, "command") or !@hasField(Item, "separator") or !@hasField(Item, "enabled") or
616
+ !@hasField(Item, "detail") or !@hasField(Item, "role") or !@hasField(Item, "key") or !@hasField(Item, "modifiers"))
617
+ {
618
+ @compileError(teaching);
619
+ }
620
+ if (!statusItemNumericType(@FieldType(Item, "id"))) @compileError(teaching);
621
+ if (@FieldType(Item, "label") != []const u8 or @FieldType(Item, "command") != []const u8 or
622
+ @FieldType(Item, "separator") != bool or @FieldType(Item, "enabled") != bool or
623
+ @FieldType(Item, "detail") != []const u8 or
624
+ !statusItemEnumType(@FieldType(Item, "role"), &.{ "command", "info", "header", "hero", "agent", "context" }) or
625
+ @FieldType(Item, "key") != []const u8)
626
+ {
627
+ @compileError(teaching);
628
+ }
629
+ const Modifiers = statusItemRecordType(@FieldType(Item, "modifiers"), teaching);
630
+ const modifiers_info = @typeInfo(Modifiers).@"struct";
631
+ if (modifiers_info.fields.len != 5 or !@hasField(Modifiers, "primary") or !@hasField(Modifiers, "command") or
632
+ !@hasField(Modifiers, "control") or !@hasField(Modifiers, "option") or !@hasField(Modifiers, "shift") or
633
+ @FieldType(Modifiers, "primary") != bool or @FieldType(Modifiers, "command") != bool or
634
+ @FieldType(Modifiers, "control") != bool or @FieldType(Modifiers, "option") != bool or @FieldType(Modifiers, "shift") != bool)
635
+ {
636
+ @compileError(teaching);
637
+ }
638
+ }
639
+
640
+ fn statusItemNumericType(comptime T: type) bool {
641
+ return @typeInfo(T) == .int or @typeInfo(T) == .float;
642
+ }
643
+
644
+ fn statusItemEnumType(comptime T: type, comptime expected: []const []const u8) bool {
645
+ const info = @typeInfo(T);
646
+ if (info != .@"enum" or info.@"enum".fields.len != expected.len) return false;
647
+ inline for (expected) |name| {
648
+ var found = false;
649
+ inline for (info.@"enum".fields) |field| {
650
+ if (std.mem.eql(u8, name, field.name)) found = true;
651
+ }
652
+ if (!found) return false;
653
+ }
654
+ return true;
655
+ }
656
+
657
+ fn statusItemRecordType(comptime Raw: type, comptime teaching: []const u8) type {
658
+ const Record = switch (@typeInfo(Raw)) {
659
+ .pointer => |pointer| if (pointer.size == .one and pointer.is_const) pointer.child else @compileError(teaching),
660
+ else => Raw,
661
+ };
662
+ if (@typeInfo(Record) != .@"struct") @compileError(teaching);
663
+ return Record;
664
+ }
665
+
230
666
  /// `Options.init_fx`: register the wiring's boot images, perform
231
667
  /// the boot command and initial subscriptions on the installing
232
668
  /// frame (the boot model itself committed in `init`), dispatch
233
669
  /// the launch environment overrides as ordinary journaled Msgs,
234
670
  /// then refresh the app-held root.
235
671
  fn initFx(model: *Model, fx: *Effects) void {
672
+ if (host_call_mux_store) |*mux| {
673
+ fx.bindHostCalls(mux.binding());
674
+ } else if (host_calls_store) |binding| {
675
+ fx.bindHostCalls(binding);
676
+ } else if (persist_options_store) |persist| {
677
+ fx.bindHostCalls(persist.binding);
678
+ }
236
679
  for (boot_images_store) |image| {
237
680
  // Registration is synchronous; a failed decode leaves the
238
681
  // views on their fallback (avatar initials) — a bad asset
239
682
  // never breaks presentation (the Zig apps' convention).
240
683
  _ = fx.registerImageBytes(image.id, image.bytes) catch continue;
241
684
  }
685
+ if (persist_options_store) |persist| {
686
+ if (persist.outcome_handle) |handle| {
687
+ handle.* = fx.openChannel(.{
688
+ .key = persist_outcome_channel_key,
689
+ .on_event = persistOutcomeMsg,
690
+ .max_pending = 8,
691
+ });
692
+ }
693
+ }
694
+ const persist_outcome = preparePersistRestore(fx);
242
695
  Host.performBoot(fx);
696
+ if (persist_outcome) |outcome| dispatchPersistOutcome(fx, outcome);
243
697
  dispatchEnvValues(fx);
244
698
  model.* = Host.model().*;
245
699
  }
246
700
 
701
+ /// Resolve the boot snapshot entirely at the effect boundary. Live
702
+ /// launches journal the runner-provided result; replay consumes the
703
+ /// recorded result and never consults the current app-data directory.
704
+ fn preparePersistRestore(fx: *Effects) ?persist_store.Outcome {
705
+ const persist = persist_options_store orelse return null;
706
+ const restore = if (fx.replay) fx.takeReplayPersist() orelse return null else blk: {
707
+ if (persist.restore.migration_from_version) |from_version| {
708
+ if (Host.migrateSnapshot(persist.restore.bytes, from_version, std.heap.page_allocator)) |migrated| {
709
+ defer std.heap.page_allocator.free(migrated);
710
+ if (migrated.len > persist_store.max_snapshot_bytes) {
711
+ fx.journalPersistRestore(.rejected, "");
712
+ return .rejected;
713
+ }
714
+ Host.restoreSnapshot(migrated);
715
+ persist.binding.send_fn(persist.binding.context, "core.persist", migrated);
716
+ fx.journalPersistRestore(.ok, migrated);
717
+ return .ok;
718
+ }
719
+ fx.journalPersistRestore(.migrate_failed, "");
720
+ return .migrate_failed;
721
+ }
722
+ fx.journalPersistRestore(persist.restore.outcome, persist.restore.bytes);
723
+ break :blk runtime_effects.ReplayPersistEntry{ .outcome = persist.restore.outcome, .bytes = persist.restore.bytes };
724
+ };
725
+ if (restore.outcome == .ok) Host.restoreSnapshot(restore.bytes);
726
+ return restore.outcome;
727
+ }
728
+
729
+ fn dispatchPersistOutcome(fx: *Effects, outcome: persist_store.Outcome) void {
730
+ const routes = persist_options_store.?.routes;
731
+ switch (outcome) {
732
+ .ok => dispatchPersistVoid(fx, routes.ok),
733
+ .none => dispatchPersistVoid(fx, routes.none),
734
+ .corrupt, .version_unknown, .migrate_failed, .io_failed, .rejected => dispatchPersistError(fx, routes.err, @tagName(outcome)),
735
+ }
736
+ }
737
+
738
+ fn persistOutcomeMsg(event: runtime_effects.EffectChannelEvent) Msg {
739
+ const reason: []const u8 = switch (event.kind) {
740
+ .data => event.bytes,
741
+ .rejected => "rejected",
742
+ .closed => "io_failed",
743
+ };
744
+ const route = persist_options_store.?.routes.err;
745
+ inline for (@typeInfo(Msg).@"union".fields) |arm| {
746
+ if (comptime arm.type == []const u8) {
747
+ if (std.mem.eql(u8, arm.name, route)) return @unionInit(Msg, arm.name, reason);
748
+ }
749
+ }
750
+ @panic("TsUiApp persistence err route does not name a one-Uint8Array-field Msg arm");
751
+ }
752
+
753
+ fn dispatchPersistVoid(fx: *Effects, route: []const u8) void {
754
+ inline for (@typeInfo(Msg).@"union".fields) |arm| {
755
+ if (comptime arm.type == void) {
756
+ if (std.mem.eql(u8, arm.name, route)) {
757
+ Host.dispatch(fx, @unionInit(Msg, arm.name, {}));
758
+ return;
759
+ }
760
+ }
761
+ }
762
+ @panic("TsUiApp persistence route does not name a void Msg arm");
763
+ }
764
+
765
+ fn dispatchPersistError(fx: *Effects, route: []const u8, reason: []const u8) void {
766
+ inline for (@typeInfo(Msg).@"union".fields) |arm| {
767
+ if (comptime arm.type == []const u8) {
768
+ if (std.mem.eql(u8, arm.name, route)) {
769
+ const copy = core.rt.frameAlloc(u8, reason.len);
770
+ @memcpy(copy, reason);
771
+ Host.dispatch(fx, @unionInit(Msg, arm.name, copy));
772
+ return;
773
+ }
774
+ }
775
+ }
776
+ @panic("TsUiApp persistence err route does not name a one-Uint8Array-field Msg arm");
777
+ }
778
+
247
779
  /// The `envMsgs` channel's delivery: each launch-resolved value
248
780
  /// dispatches its named one-bytes-field arm — a full core cycle
249
781
  /// per value, in declaration order, right after the boot command.
@@ -591,6 +1123,10 @@ pub fn TsUiApp(comptime core: type) type {
591
1123
  fn updateFx(model: *Model, msg: Msg, fx: *Effects) void {
592
1124
  Host.dispatch(fx, msg);
593
1125
  model.* = Host.model().*;
1126
+ if (lifecycle_flush_after_update) {
1127
+ lifecycle_flush_after_update = false;
1128
+ flushPersistence();
1129
+ }
594
1130
  }
595
1131
  };
596
1132
  }