@native-sdk/cli 0.8.4 → 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 (146) hide show
  1. package/build/app.zig +859 -62
  2. package/build.zig +543 -13
  3. package/build.zig.zon +1 -0
  4. package/native-sdk.d.ts +2 -0
  5. package/package.json +12 -11
  6. package/packages/core/compile-surface/core.ts +365 -2
  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 +149 -0
  19. package/packages/core/sdk/core.ts +529 -12
  20. package/packages/core/sdk/events.d.ts +36 -0
  21. package/packages/core/sdk/events.ts +57 -0
  22. package/packages/core/src/checker.ts +468 -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 +1847 -16
  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 +82 -16
  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/references/project-anatomy.md +1 -1
  38. package/skill-data/native-ui/SKILL.md +47 -0
  39. package/skill-data/ts-core/SKILL.md +80 -31
  40. package/skill-data/ts-services/SKILL.md +126 -0
  41. package/skill-data/ts-services/references/service-surface.md +603 -0
  42. package/skill-data/zig/SKILL.md +1 -1
  43. package/skills/native-sdk/SKILL.md +1 -1
  44. package/src/app_runner/no_migrations.zig +3 -0
  45. package/src/app_runner/no_services.zig +20 -0
  46. package/src/app_runner/root.zig +104 -4
  47. package/src/app_runner/ts_core_main.zig +249 -8
  48. package/src/automation/snapshot.zig +23 -5
  49. package/src/embed/app_exports.zig +11 -1
  50. package/src/embed/c_api.zig +48 -2
  51. package/src/embed/conversions.zig +4 -5
  52. package/src/embed/host.zig +109 -1
  53. package/src/embed/root.zig +7 -0
  54. package/src/embed/tests.zig +213 -0
  55. package/src/embed/types.zig +51 -0
  56. package/src/embed/ui_host.zig +119 -5
  57. package/src/platform/android/NativeSdkActivity.java +131 -0
  58. package/src/platform/android/android_host.c +182 -1
  59. package/src/platform/android/native_sdk_app.h +17 -0
  60. package/src/platform/ios/native_sdk_app.h +17 -0
  61. package/src/platform/ios/uikit_host.m +115 -0
  62. package/src/platform/linux/cef_host.cpp +1 -1
  63. package/src/platform/linux/gtk_host.c +89 -17
  64. package/src/platform/linux/gtk_host.h +3 -0
  65. package/src/platform/linux/root.zig +40 -18
  66. package/src/platform/macos/appkit_host.h +16 -3
  67. package/src/platform/macos/appkit_host.m +536 -36
  68. package/src/platform/macos/cef_host.mm +248 -36
  69. package/src/platform/macos/root.zig +244 -34
  70. package/src/platform/null_platform.zig +223 -19
  71. package/src/platform/null_platform_tests.zig +74 -0
  72. package/src/platform/root.zig +9 -0
  73. package/src/platform/types.zig +179 -15
  74. package/src/platform/windows/root.zig +187 -21
  75. package/src/platform/windows/webview2_host.cpp +293 -46
  76. package/src/primitives/app_manifest/comptime_scene.zig +2 -0
  77. package/src/primitives/app_manifest/root.zig +4 -0
  78. package/src/primitives/app_manifest/tests.zig +3 -0
  79. package/src/primitives/app_manifest/types.zig +32 -0
  80. package/src/primitives/app_manifest/validation.zig +19 -1
  81. package/src/root.zig +46 -0
  82. package/src/runtime/api.zig +11 -0
  83. package/src/runtime/automation_snapshot.zig +4 -0
  84. package/src/runtime/bridge_permission_tests.zig +23 -2
  85. package/src/runtime/bridge_responses.zig +3 -1
  86. package/src/runtime/canvas_frame.zig +18 -11
  87. package/src/runtime/canvas_frame_animation_tests.zig +4 -0
  88. package/src/runtime/clock.zig +13 -1
  89. package/src/runtime/core.zig +133 -1
  90. package/src/runtime/credentials_store.zig +115 -0
  91. package/src/runtime/effects.zig +2295 -113
  92. package/src/runtime/effects_credentials_tests.zig +332 -0
  93. package/src/runtime/effects_host_tests.zig +208 -1
  94. package/src/runtime/effects_relational_tests.zig +314 -0
  95. package/src/runtime/effects_store_tests.zig +211 -0
  96. package/src/runtime/flow.zig +14 -1
  97. package/src/runtime/gpu_surface_events.zig +6 -1
  98. package/src/runtime/native_command_event_tests.zig +42 -0
  99. package/src/runtime/persist_store.zig +666 -0
  100. package/src/runtime/record_store.zig +788 -0
  101. package/src/runtime/relational_store.zig +854 -0
  102. package/src/runtime/root.zig +49 -0
  103. package/src/runtime/runtime_event_tests.zig +70 -0
  104. package/src/runtime/service_host.zig +711 -0
  105. package/src/runtime/service_pool.zig +1314 -0
  106. package/src/runtime/session_blobs.zig +1 -1
  107. package/src/runtime/session_journal.zig +102 -0
  108. package/src/runtime/session_record.zig +177 -0
  109. package/src/runtime/session_replay.zig +109 -4
  110. package/src/runtime/shell_layout.zig +1 -0
  111. package/src/runtime/shell_layout_tests.zig +120 -0
  112. package/src/runtime/sqlite_engine.zig +474 -0
  113. package/src/runtime/state.zig +6 -0
  114. package/src/runtime/system_services.zig +49 -10
  115. package/src/runtime/tests.zig +4 -0
  116. package/src/runtime/ts_core_host.zig +788 -86
  117. package/src/runtime/ts_core_host_tests.zig +231 -11
  118. package/src/runtime/ts_ui_app.zig +499 -1
  119. package/src/runtime/ui_app.zig +142 -16
  120. package/src/runtime/ui_app_tests.zig +155 -5
  121. package/src/runtime/validation.zig +22 -0
  122. package/src/runtime/view.zig +9 -0
  123. package/src/runtime/window_storage.zig +1 -0
  124. package/src/runtime/window_view_runtime.zig +2 -3
  125. package/src/runtime/window_views.zig +19 -0
  126. package/src/tooling/android.zig +21 -16
  127. package/src/tooling/db.zig +160 -0
  128. package/src/tooling/ios.zig +7 -1
  129. package/src/tooling/manifest.zig +214 -2
  130. package/src/tooling/package.zig +116 -0
  131. package/src/tooling/raw_manifest.zig +30 -0
  132. package/src/tooling/root.zig +1 -0
  133. package/src/tooling/templates.zig +161 -4
  134. package/src/tooling/ts_core.zig +283 -17
  135. package/src/tooling/verbs.zig +7 -0
  136. package/third_party/sqlite/README.md +13 -0
  137. package/third_party/sqlite/sqlite3.c +269649 -0
  138. package/third_party/sqlite/sqlite3.h +14349 -0
  139. package/tools/corewire/core_abi.zig +14 -2
  140. package/tools/corewire/emit.zig +98 -29
  141. package/tools/corewire/emit_facade.zig +378 -23
  142. package/tools/corewire/emit_profile.zig +6 -2
  143. package/tools/corewire/emit_service.zig +924 -0
  144. package/tools/corewire/main.zig +122 -0
  145. package/tools/corewire/service_contract.zig +250 -0
  146. package/tools/corewire/sidecar.zig +26 -15
@@ -44,7 +44,12 @@
44
44
  //! the committed model. One model-helper convention joins that wiring:
45
45
  //! an exported `themePack(model): "house" | "geist"` helper selects the
46
46
  //! stock pack live through `theme_fn`, without taking ownership of the
47
- //! system appearance axes. The two seams the core owns — `update_fx` and
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
48
53
  //! `init_fx` — are stamped by this adapter and must be left null.
49
54
  //!
50
55
  //! `Options.sync` is deliberately unsupported: it mutates the model in
@@ -63,6 +68,9 @@
63
68
  const std = @import("std");
64
69
  const canvas = @import("canvas");
65
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");
66
74
  const ui_app = @import("ui_app.zig");
67
75
  const ts_core_host = @import("ts_core_host.zig");
68
76
 
@@ -79,6 +87,10 @@ pub fn TsUiApp(comptime core: type) type {
79
87
  pub const Effects = App.Effects;
80
88
  pub const Ui = App.Ui;
81
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
+
82
94
  /// One boot-registered image: the wiring reads the encoded bytes
83
95
  /// (app.zon's `.assets.images` paths) and the adapter registers
84
96
  /// them on the installing frame — `fx.registerImageBytes`, the
@@ -102,6 +114,132 @@ pub fn TsUiApp(comptime core: type) type {
102
114
  value: []const u8,
103
115
  };
104
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
+
105
243
  /// Adapter-owned configuration — the knobs that exist because
106
244
  /// the core is TypeScript, kept separate from `App.Options` so
107
245
  /// the wiring surface reads as ordinary UiApp wiring.
@@ -128,6 +266,14 @@ pub fn TsUiApp(comptime core: type) type {
128
266
  /// meaningful for cores exporting `envMsgs`. The slices must
129
267
  /// outlive install.
130
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,
131
277
  };
132
278
 
133
279
  /// Construct the UiApp over the committed TS model. `options`
@@ -162,15 +308,30 @@ pub fn TsUiApp(comptime core: type) type {
162
308
  /// installing frame.
163
309
  var boot_images_store: []const BootImage = &.{};
164
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;
165
316
 
166
317
  fn applyCoreOptions(core_options: CoreOptions) void {
167
318
  Host.setAudioCacheDir(core_options.audio_cache_dir);
168
319
  Host.setImageCacheDir(core_options.image_cache_dir);
320
+ Host.bindServiceResults(core_options.service_results);
169
321
  boot_images_store = core_options.boot_images;
170
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;
171
329
  if (core_options.env_values.len > 0 and comptime !@hasDecl(core, "envMsgs")) {
172
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");
173
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
+ }
174
335
  }
175
336
 
176
337
  fn stampOptions(options: Options) Options {
@@ -184,6 +345,9 @@ pub fn TsUiApp(comptime core: type) type {
184
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");
185
346
  }
186
347
  var stamped = options;
348
+ lifecycle_store = stamped.on_lifecycle;
349
+ lifecycle_flush_after_update = false;
350
+ stamped.on_lifecycle = lifecycleAdapter;
187
351
  stamped.init_fx = initFx;
188
352
  stamped.update_fx = updateFx;
189
353
  // A TypeScript core can select a built-in pack from ordinary
@@ -199,6 +363,18 @@ pub fn TsUiApp(comptime core: type) type {
199
363
  comptime validateThemePackHelper();
200
364
  stamped.theme_fn = themePackAdapter;
201
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
+ }
202
378
  // The core's host-event channels, comptime-detected from its
203
379
  // exports (export exists -> wired; every shape mismatch is a
204
380
  // teaching compile error in the adapter below). A wiring that
@@ -243,6 +419,26 @@ pub fn TsUiApp(comptime core: type) type {
243
419
  return stamped;
244
420
  }
245
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
+
246
442
  fn themePackAdapter(model: *const Model) canvas.ThemePack {
247
443
  const pack = model.themePack();
248
444
  return canvas.ThemePack.fromName(@tagName(pack)).?;
@@ -265,23 +461,321 @@ pub fn TsUiApp(comptime core: type) type {
265
461
  }
266
462
  }
267
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
+
268
666
  /// `Options.init_fx`: register the wiring's boot images, perform
269
667
  /// the boot command and initial subscriptions on the installing
270
668
  /// frame (the boot model itself committed in `init`), dispatch
271
669
  /// the launch environment overrides as ordinary journaled Msgs,
272
670
  /// then refresh the app-held root.
273
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
+ }
274
679
  for (boot_images_store) |image| {
275
680
  // Registration is synchronous; a failed decode leaves the
276
681
  // views on their fallback (avatar initials) — a bad asset
277
682
  // never breaks presentation (the Zig apps' convention).
278
683
  _ = fx.registerImageBytes(image.id, image.bytes) catch continue;
279
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);
280
695
  Host.performBoot(fx);
696
+ if (persist_outcome) |outcome| dispatchPersistOutcome(fx, outcome);
281
697
  dispatchEnvValues(fx);
282
698
  model.* = Host.model().*;
283
699
  }
284
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
+
285
779
  /// The `envMsgs` channel's delivery: each launch-resolved value
286
780
  /// dispatches its named one-bytes-field arm — a full core cycle
287
781
  /// per value, in declaration order, right after the boot command.
@@ -629,6 +1123,10 @@ pub fn TsUiApp(comptime core: type) type {
629
1123
  fn updateFx(model: *Model, msg: Msg, fx: *Effects) void {
630
1124
  Host.dispatch(fx, msg);
631
1125
  model.* = Host.model().*;
1126
+ if (lifecycle_flush_after_update) {
1127
+ lifecycle_flush_after_update = false;
1128
+ flushPersistence();
1129
+ }
632
1130
  }
633
1131
  };
634
1132
  }