@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
@@ -4,7 +4,8 @@
4
4
  // checker adds the subset rules on the typed AST; the emitter re-derives every
5
5
  // rule during emission and turns any gap into a loud internal error.
6
6
 
7
- import { ts, TypedAst, lineColumn, hasExportModifier, exportListBindings, type ExportListBinding } from "./typed_ast.ts";
7
+ import { ts, TypedAst, lineColumn, hasExportModifier, exportListBindings, sdkCoreModulePath, type ExportListBinding } from "./typed_ast.ts";
8
+ import path from "node:path";
8
9
  import { makeDiagnostic, type SubsetDiagnostic, type RuleId } from "./diagnostics.ts";
9
10
  import type { TypeTable } from "./types.ts";
10
11
  import {
@@ -416,8 +417,8 @@ function thrownUnionOf(
416
417
 
417
418
  export interface CheckResult {
418
419
  readonly diagnostics: SubsetDiagnostic[];
419
- /// Teaching notices that do NOT stop the build (today: NS1028, the
420
- /// not-yet-host-backed persist op). Same shape as diagnostics, surfaced
420
+ /// Teaching notices that do NOT stop the build (today: capability and
421
+ /// persistence-contract lints). Same shape as diagnostics, surfaced
421
422
  /// as warnings by the CLI.
422
423
  readonly warnings: SubsetDiagnostic[];
423
424
  /// Local names bound to the SDK `Cmd` surface (import from
@@ -429,6 +430,15 @@ export interface CheckResult {
429
430
  readonly subNames: Set<string>;
430
431
  }
431
432
 
433
+ /// Manifest-owned boot routes for engine model persistence. The frontend
434
+ /// receives these as plain strings so it stays independent of app.zon's file
435
+ /// format while still checking the cross-file Msg contract.
436
+ export interface PersistRoutes {
437
+ readonly ok: string;
438
+ readonly none: string;
439
+ readonly err: string;
440
+ }
441
+
432
442
  export class SubsetChecker {
433
443
  private readonly diagnostics: SubsetDiagnostic[] = [];
434
444
  private readonly warnings: SubsetDiagnostic[] = [];
@@ -445,19 +455,46 @@ export class SubsetChecker {
445
455
  private readonly files: readonly ts.SourceFile[];
446
456
  private readonly entry: ts.SourceFile;
447
457
  private readonly fileSet: Set<ts.SourceFile>;
448
-
449
- constructor(tast: TypedAst, table: TypeTable, files: readonly ts.SourceFile[] | ts.SourceFile) {
458
+ /// Null means the app has no service registry. An empty set is distinct:
459
+ /// service files exist, but none exported a callable operation.
460
+ private readonly serviceOps: ReadonlySet<string> | null;
461
+ private readonly capabilities: Set<string>;
462
+ private readonly permissions: Set<string>;
463
+ private readonly persistRoutes: PersistRoutes | undefined;
464
+ private readonly sdkCorePath: string;
465
+ private usesPersist = false;
466
+ private usesStore = false;
467
+ private usesSqlite = false;
468
+ private usesCredentials = false;
469
+
470
+ constructor(
471
+ tast: TypedAst,
472
+ table: TypeTable,
473
+ files: readonly ts.SourceFile[] | ts.SourceFile,
474
+ serviceOps: ReadonlySet<string> | null = null,
475
+ capabilities: readonly string[] = [],
476
+ permissions: readonly string[] = [],
477
+ persistRoutes?: PersistRoutes,
478
+ sdkCorePath: string = sdkCoreModulePath,
479
+ ) {
450
480
  this.tast = tast;
451
481
  this.table = table;
452
482
  this.files = Array.isArray(files) ? files : [files as ts.SourceFile];
453
483
  this.entry = this.files[0];
454
484
  this.fileSet = new Set(this.files);
485
+ this.serviceOps = serviceOps;
486
+ this.capabilities = new Set(capabilities);
487
+ this.permissions = new Set(permissions);
488
+ this.persistRoutes = persistRoutes;
489
+ this.sdkCorePath = sdkCorePath;
455
490
  }
456
491
 
457
492
  check(): CheckResult {
458
493
  for (const file of this.files) this.findCmdNames(file);
494
+ this.checkServiceCalls();
459
495
  for (const file of this.files) this.checkModuleShape(file);
460
496
  this.checkEntryContract();
497
+ this.checkPersistRoutes();
461
498
  this.checkNameCollisions();
462
499
  this.checkGeneratedMetadataNames();
463
500
  this.checkModelHoldsData();
@@ -465,10 +502,25 @@ export class SubsetChecker {
465
502
  this.checkCmdPurity();
466
503
  this.checkSubPurity();
467
504
  this.checkModelBindingSurface();
505
+ this.checkMigrationHook();
506
+ this.checkThemePackHelper();
507
+ this.checkStatusItemHelper();
468
508
  this.checkViewUnbound();
469
509
  this.checkReservedContractConsts();
470
510
  this.checkValueRecordAliases();
471
511
  for (const file of this.files) this.walk(file);
512
+ if (this.capabilities.has("persist") && !this.usesPersist) {
513
+ this.warn("NS1028", "app.zon declares the `persist` capability, but this core has no `Cmd.persist()` call.", this.entry);
514
+ }
515
+ if (this.capabilities.has("store") && !this.usesStore) {
516
+ this.warn("NS1069", "app.zon declares the `store` capability, but this core has no `Cmd.store.*` call.", this.entry);
517
+ }
518
+ if (this.capabilities.has("sqlite") && !this.usesSqlite) {
519
+ this.warn("NS1070", "app.zon declares the `sqlite` capability, but this core has no raw or generated relational command/subscription.", this.entry);
520
+ }
521
+ if (this.capabilities.has("credentials") && !this.usesCredentials) {
522
+ this.warn("NS1071", "app.zon declares the `credentials` capability, but this core has no `Cmd.credentials.*` call.", this.entry);
523
+ }
472
524
  this.checkExceptions();
473
525
  return {
474
526
  diagnostics: this.diagnostics,
@@ -478,6 +530,72 @@ export class SubsetChecker {
478
530
  };
479
531
  }
480
532
 
533
+ /// NS1067 — once an app declares a service registry, every generic
534
+ /// Cmd.host/request literal must resolve either to that registry or to
535
+ /// the SDK-reserved native family. The service binding owns both command
536
+ /// channels, so an unknown fire-and-forget host call would otherwise be
537
+ /// dropped silently by ServiceHost.send.
538
+ private checkServiceCalls(): void {
539
+ if (this.serviceOps === null || this.cmdNames.size === 0) return;
540
+ const visit = (node: ts.Node): void => {
541
+ if (
542
+ ts.isCallExpression(node) &&
543
+ ts.isPropertyAccessExpression(node.expression) &&
544
+ (node.expression.name.text === "request" || node.expression.name.text === "host") &&
545
+ ts.isIdentifier(node.expression.expression) &&
546
+ this.cmdNames.has(node.expression.expression.text) &&
547
+ this.isSdkReference(node.expression.expression)
548
+ ) {
549
+ const name = node.arguments[0];
550
+ if (!name || !ts.isStringLiteral(name)) {
551
+ this.report("NS1067", "A service call name is not a string literal from the generated registry.", name ?? node);
552
+ } else if (!name.text.startsWith("native-sdk.") && !this.serviceOps.has(name.text)) {
553
+ this.report("NS1067", `\`${name.text}\` names no operation in services.contract.json.`, name);
554
+ }
555
+ }
556
+ ts.forEachChild(node, visit);
557
+ };
558
+ for (const file of this.files) visit(file);
559
+ }
560
+
561
+ /// app.zon owns these names, while the TypeScript core owns Msg. Validate
562
+ /// the seam in the frontend so `native check` and the node dev host fail at
563
+ /// the authoring boundary instead of waiting for generated Zig to compile.
564
+ private checkPersistRoutes(): void {
565
+ const routes = this.persistRoutes;
566
+ if (routes === undefined) return;
567
+ const msg = this.table.unions.get("Msg");
568
+ if (msg === undefined) return; // NS1062 owns the missing-root teaching.
569
+
570
+ const checks = [
571
+ { role: "ok", route: routes.ok, payload: "void" },
572
+ { role: "none", route: routes.none, payload: "void" },
573
+ { role: "err", route: routes.err, payload: "bytes" },
574
+ ] as const;
575
+ for (const check of checks) {
576
+ const arm = msg.arms.find((candidate) => candidate.tag === check.route);
577
+ if (arm === undefined) {
578
+ this.report(
579
+ "NS1033",
580
+ `app.zon persistence restore route \`${check.route}\` (${check.role}) names no Msg arm.`,
581
+ msg.decl.name,
582
+ );
583
+ continue;
584
+ }
585
+ const valid =
586
+ check.payload === "void"
587
+ ? arm.fields.length === 0
588
+ : arm.fields.length === 1 && arm.fields[0].type.k === "bytes";
589
+ if (!valid) {
590
+ this.report(
591
+ "NS1033",
592
+ `app.zon persistence restore route \`${check.route}\` (${check.role}) has the wrong Msg payload; ok/none must be void and err must carry one Uint8Array field.`,
593
+ arm.fields[0]?.decl ?? msg.decl.name,
594
+ );
595
+ }
596
+ }
597
+ }
598
+
481
599
  private report(id: RuleId, site: string, node: ts.Node): void {
482
600
  const file = node.getSourceFile();
483
601
  const { line, column } = lineColumn(file, node.getStart());
@@ -510,7 +628,7 @@ export class SubsetChecker {
510
628
  }
511
629
  if (bindings && ts.isNamespaceImport(bindings)) {
512
630
  // NS1039 (half 2): the intrinsic SDK surface is imported by name —
513
- // the purity rules (NS1017/NS1025) and the asciiBytes fold recognize
631
+ // the purity rules (NS1017/NS1025) and byte-text folds recognize
514
632
  // the factories by their imported names.
515
633
  this.report("NS1039", `\`import * as ${bindings.name.text}\` aliases the intrinsic SDK module.`, bindings);
516
634
  continue;
@@ -661,6 +779,221 @@ export class SubsetChecker {
661
779
  }
662
780
  }
663
781
 
782
+ /// `themePack(model)` is both an ordinary exported Model helper and a
783
+ /// generated-launcher convention. Keep its shape exact here so a typo
784
+ /// is taught in core.ts instead of failing inside the generated Zig
785
+ /// adapter (or, worse, becoming an inert markup-only helper).
786
+ private checkThemePackHelper(): void {
787
+ let decl: ts.FunctionDeclaration | null = null;
788
+ for (const stmt of this.entry.statements) {
789
+ if (
790
+ ts.isFunctionDeclaration(stmt) &&
791
+ stmt.name?.text === "themePack" &&
792
+ hasExportModifier(stmt)
793
+ ) {
794
+ decl = stmt;
795
+ break;
796
+ }
797
+ }
798
+ if (decl === null) {
799
+ for (const binding of exportListBindings(this.tast, this.entry)) {
800
+ if (
801
+ binding.exportedName === "themePack" &&
802
+ binding.target !== null &&
803
+ binding.target !== undefined &&
804
+ ts.isFunctionDeclaration(binding.target) &&
805
+ binding.target.getSourceFile() === this.entry
806
+ ) {
807
+ decl = binding.target;
808
+ break;
809
+ }
810
+ }
811
+ }
812
+ if (decl === null) return;
813
+
814
+ const helper = this.table.modelHelperDecls().find(
815
+ (candidate) => candidate.name === "themePack" && candidate.decl === decl,
816
+ );
817
+ if (helper === undefined || decl.type === undefined) {
818
+ this.report(
819
+ "NS1033",
820
+ "`themePack` is not a single-Model-parameter helper with an explicit return type.",
821
+ decl.name ?? decl,
822
+ );
823
+ return;
824
+ }
825
+
826
+ const returns = this.table.resolveTypeNode(decl.type);
827
+ if (
828
+ returns.k !== "enum" ||
829
+ returns.members.length !== 2 ||
830
+ !returns.members.includes("house") ||
831
+ !returns.members.includes("geist")
832
+ ) {
833
+ this.report(
834
+ "NS1033",
835
+ "`themePack` does not return exactly the built-in `\"house\" | \"geist\"` theme-pack union.",
836
+ decl.type,
837
+ );
838
+ }
839
+ }
840
+
841
+ /// Persistence migration is a pure entry hook, not a model helper. It
842
+ /// receives the previous canonical snapshot and monotonic schema version;
843
+ /// returning the current Model succeeds, while throwing closes as
844
+ /// `migrate_failed` at the host boundary.
845
+ private checkMigrationHook(): void {
846
+ let decl: ts.FunctionDeclaration | null = null;
847
+ for (const stmt of this.entry.statements) {
848
+ if (ts.isFunctionDeclaration(stmt) && stmt.name?.text === "migrate" && hasExportModifier(stmt)) {
849
+ decl = stmt;
850
+ break;
851
+ }
852
+ }
853
+ if (decl === null) {
854
+ for (const binding of exportListBindings(this.tast, this.entry)) {
855
+ if (
856
+ binding.exportedName === "migrate" &&
857
+ binding.target !== null &&
858
+ binding.target !== undefined &&
859
+ ts.isFunctionDeclaration(binding.target) &&
860
+ binding.target.getSourceFile() === this.entry
861
+ ) {
862
+ decl = binding.target;
863
+ break;
864
+ }
865
+ }
866
+ }
867
+ if (decl === null) return;
868
+ const params = decl.parameters;
869
+ const snapshot = params[0]?.type === undefined ? null : this.table.resolveTypeNode(params[0].type);
870
+ const version = params[1]?.type === undefined ? null : this.table.resolveTypeNode(params[1].type);
871
+ const returns = decl.type === undefined ? null : this.table.resolveTypeNode(decl.type);
872
+ if (params.length !== 2 || snapshot?.k !== "bytes" || version?.k !== "number" || returns?.k !== "struct" || returns.name !== "Model") {
873
+ this.report(
874
+ "NS1033",
875
+ "`migrate` must be declared exactly as `export function migrate(snapshot: Uint8Array, fromVersion: number): Model`; throw to report `migrate_failed`.",
876
+ decl.name ?? decl,
877
+ );
878
+ }
879
+ }
880
+
881
+ /// `statusItem(model)` is the TypeScript launcher's live menu-bar
882
+ /// declaration. Validate both records exactly here: the Zig adapter
883
+ /// intentionally accepts only this one canonical, projection-safe
884
+ /// shape, and a source diagnostic is much more useful than generated
885
+ /// Zig reflection failing later.
886
+ private checkStatusItemHelper(): void {
887
+ let decl: ts.FunctionDeclaration | null = null;
888
+ for (const stmt of this.entry.statements) {
889
+ if (ts.isFunctionDeclaration(stmt) && stmt.name?.text === "statusItem" && hasExportModifier(stmt)) {
890
+ decl = stmt;
891
+ break;
892
+ }
893
+ }
894
+ if (decl === null) {
895
+ for (const binding of exportListBindings(this.tast, this.entry)) {
896
+ if (
897
+ binding.exportedName === "statusItem" &&
898
+ binding.target !== null &&
899
+ binding.target !== undefined &&
900
+ ts.isFunctionDeclaration(binding.target) &&
901
+ binding.target.getSourceFile() === this.entry
902
+ ) {
903
+ decl = binding.target;
904
+ break;
905
+ }
906
+ }
907
+ }
908
+ if (decl === null) return;
909
+
910
+ const helper = this.table.modelHelperDecls().find(
911
+ (candidate) => candidate.name === "statusItem" && candidate.decl === decl,
912
+ );
913
+ if (helper === undefined || decl.type === undefined) {
914
+ this.report(
915
+ "NS1033",
916
+ "`statusItem` is not a single-Model-parameter helper with an explicit return type.",
917
+ decl.name ?? decl,
918
+ );
919
+ return;
920
+ }
921
+
922
+ const returns = this.table.resolveTypeNode(decl.type);
923
+ const state = returns.k === "struct" ? this.table.structs.get(returns.name) : undefined;
924
+ const stateNames = state?.fields.map((field) => field.tsName).sort() ?? [];
925
+ const iconPath = state?.fields.find((field) => field.tsName === "iconPath");
926
+ const tooltip = state?.fields.find((field) => field.tsName === "tooltip");
927
+ const activationCommand = state?.fields.find((field) => field.tsName === "activationCommand");
928
+ const alternateActivationCommand = state?.fields.find((field) => field.tsName === "alternateActivationCommand");
929
+ const openCommand = state?.fields.find((field) => field.tsName === "openCommand");
930
+ const presentationField = state?.fields.find((field) => field.tsName === "presentation");
931
+ const items = state?.fields.find((field) => field.tsName === "items");
932
+ const presentationType = presentationField?.type.k === "struct" ? presentationField.type : null;
933
+ const presentation = presentationType === null ? undefined : this.table.structs.get(presentationType.name);
934
+ const presentationNames = presentation?.fields.map((field) => field.tsName).sort() ?? [];
935
+ const title = presentation?.fields.find((field) => field.tsName === "title");
936
+ const width = presentation?.fields.find((field) => field.tsName === "width");
937
+ const tone = presentation?.fields.find((field) => field.tsName === "tone");
938
+ const iconOpacity = presentation?.fields.find((field) => field.tsName === "iconOpacity");
939
+ const monospaced = presentation?.fields.find((field) => field.tsName === "monospaced");
940
+ const itemType = items?.type.k === "slice" && items.type.elem.k === "struct" ? items.type.elem : null;
941
+ const item = itemType === null ? undefined : this.table.structs.get(itemType.name);
942
+ const itemNames = item?.fields.map((field) => field.tsName).sort() ?? [];
943
+ const id = item?.fields.find((field) => field.tsName === "id");
944
+ const label = item?.fields.find((field) => field.tsName === "label");
945
+ const command = item?.fields.find((field) => field.tsName === "command");
946
+ const separator = item?.fields.find((field) => field.tsName === "separator");
947
+ const enabled = item?.fields.find((field) => field.tsName === "enabled");
948
+ const detail = item?.fields.find((field) => field.tsName === "detail");
949
+ const role = item?.fields.find((field) => field.tsName === "role");
950
+ const key = item?.fields.find((field) => field.tsName === "key");
951
+ const modifiersField = item?.fields.find((field) => field.tsName === "modifiers");
952
+ const modifiersType = modifiersField?.type.k === "struct" ? modifiersField.type : null;
953
+ const modifiers = modifiersType === null ? undefined : this.table.structs.get(modifiersType.name);
954
+ const modifierNames = modifiers?.fields.map((field) => field.tsName).sort() ?? [];
955
+ const allByteFields = [iconPath, tooltip, activationCommand, alternateActivationCommand, openCommand].every(
956
+ (field) => field?.type.k === "bytes",
957
+ );
958
+ const enumMembersAre = (field: typeof tone, expected: readonly string[]): boolean => {
959
+ if (field?.type.k !== "enum") return false;
960
+ const found = this.table.enums.get(field.type.name)?.members.slice().sort() ?? [];
961
+ return found.join(",") === expected.slice().sort().join(",");
962
+ };
963
+ const boolModifierFields = modifiers !== undefined && modifiers.fields.every((field) => field.type.k === "bool");
964
+ const numericId = id !== undefined && ["number", "i64", "f64", "numAlias"].includes(id.type.k);
965
+ const numericWidth = width !== undefined && ["number", "i64", "f64", "numAlias"].includes(width.type.k);
966
+ const numericOpacity = iconOpacity !== undefined && ["number", "i64", "f64", "numAlias"].includes(iconOpacity.type.k);
967
+ const valid =
968
+ stateNames.join(",") === "activationCommand,alternateActivationCommand,iconPath,items,openCommand,presentation,tooltip" &&
969
+ allByteFields &&
970
+ presentationNames.join(",") === "iconOpacity,monospaced,title,tone,width" &&
971
+ title?.type.k === "bytes" &&
972
+ numericWidth &&
973
+ enumMembersAre(tone, ["normal", "warning", "critical"]) &&
974
+ numericOpacity &&
975
+ monospaced?.type.k === "bool" &&
976
+ item !== undefined &&
977
+ itemNames.join(",") === "command,detail,enabled,id,key,label,modifiers,role,separator" &&
978
+ numericId &&
979
+ label?.type.k === "bytes" &&
980
+ command?.type.k === "bytes" &&
981
+ separator?.type.k === "bool" &&
982
+ enabled?.type.k === "bool" &&
983
+ detail?.type.k === "bytes" &&
984
+ enumMembersAre(role, ["command", "info", "header", "hero", "agent", "context"]) &&
985
+ key?.type.k === "bytes" &&
986
+ modifierNames.join(",") === "command,control,option,primary,shift" &&
987
+ boolModifierFields;
988
+ if (!valid) {
989
+ this.report(
990
+ "NS1033",
991
+ "`statusItem` must return the exact canonical `StatusItemState` record (presentation, install-time shell fields, and rich menu rows); import it from `@native-sdk/core/events`.",
992
+ decl.type,
993
+ );
994
+ }
995
+ }
996
+
664
997
  /// NS1032 — `export const viewUnbound = [...] as const`: the dead-state
665
998
  /// lint opt-out. Every entry must be a string literal naming a Model
666
999
  /// field, an exported model helper, or a Msg kind; the emitter routes the
@@ -1091,8 +1424,8 @@ export class SubsetChecker {
1091
1424
  /// the host-event channels from src/core.ts only. Imports may FEED those
1092
1425
  /// entry points, but the exports themselves live in the entry module.
1093
1426
  private static readonly entryOnlyExports = new Set([
1094
- "update", "initialModel", "subscriptions",
1095
- "commandMsg", "keyMsg", "frameMsg", "pinchMsg", "dropMsg", "appearanceMsg", "chromeMsg", "envMsgs",
1427
+ "update", "initialModel", "subscriptions", "migrate",
1428
+ "commandMsg", "keyMsg", "frameMsg", "pinchMsg", "dropMsg", "appearanceMsg", "chromeMsg", "envMsgs", "themePack", "statusItem",
1096
1429
  "viewUnbound", "modelUnbound", "msgUnbound",
1097
1430
  ]);
1098
1431
 
@@ -1213,10 +1546,13 @@ export class SubsetChecker {
1213
1546
  /// module-level type-origin table and each union's payload-member table so
1214
1547
  /// a valid TypeScript name fails with a teaching instead of duplicate Zig.
1215
1548
  private checkGeneratedMetadataNames(): void {
1216
- const reportTypeOrigins = (name: ts.Identifier): void => {
1549
+ const reportGeneratedName = (name: ts.Identifier): void => {
1217
1550
  if (name.text === "type_origins") {
1218
1551
  this.report("NS1038", "`type_origins` collides with the generated contract type-origin metadata declaration.", name);
1219
1552
  }
1553
+ if (this.serviceOps !== null && name.text.startsWith("__nativeSdk")) {
1554
+ this.report("NS1067", "A core declaration imported by the service host uses the reserved `__nativeSdk` transport-lowering prefix.", name);
1555
+ }
1220
1556
  };
1221
1557
  for (const file of this.files) {
1222
1558
  for (const stmt of file.statements) {
@@ -1226,10 +1562,10 @@ export class SubsetChecker {
1226
1562
  ts.isClassDeclaration(stmt) ||
1227
1563
  ts.isFunctionDeclaration(stmt)
1228
1564
  ) {
1229
- if (stmt.name) reportTypeOrigins(stmt.name);
1565
+ if (stmt.name) reportGeneratedName(stmt.name);
1230
1566
  } else if (ts.isVariableStatement(stmt)) {
1231
1567
  for (const decl of stmt.declarationList.declarations) {
1232
- if (ts.isIdentifier(decl.name)) reportTypeOrigins(decl.name);
1568
+ if (ts.isIdentifier(decl.name)) reportGeneratedName(decl.name);
1233
1569
  }
1234
1570
  }
1235
1571
  }
@@ -1583,6 +1919,73 @@ export class SubsetChecker {
1583
1919
  return t.isUnion() && t.types.every((m) => m.isStringLiteral());
1584
1920
  }
1585
1921
 
1922
+ /// The canonical name of a function imported from the intrinsic SDK root,
1923
+ /// with local import renames resolved; null for app helpers and globals.
1924
+ private sdkRootFunctionName(expr: ts.Expression): string | null {
1925
+ if (!ts.isIdentifier(expr)) return null;
1926
+ const decl = this.tast.declarationOf(expr);
1927
+ if (!decl || !ts.isFunctionDeclaration(decl) || !decl.name) return null;
1928
+ if (path.resolve(decl.getSourceFile().fileName) !== path.resolve(this.sdkCorePath)) return null;
1929
+ return decl.name.text;
1930
+ }
1931
+
1932
+ /// Find the first non-ASCII character that an expression can contribute to
1933
+ /// an asciiBytes result. Syntax that merely helps choose a value (for
1934
+ /// example a Unicode literal in a conditional's comparison) is not output;
1935
+ /// string-literal union values in template holes are output and must all be
1936
+ /// ASCII-safe.
1937
+ private firstNonAsciiLiteral(expr: ts.Expression): string | null {
1938
+ let found: string | null = null;
1939
+ const inspectText = (value: string): void => {
1940
+ if (found !== null) return;
1941
+ for (const ch of value) {
1942
+ if ((ch.codePointAt(0) ?? 0) > 0x7f) {
1943
+ found = ch;
1944
+ return;
1945
+ }
1946
+ }
1947
+ };
1948
+ const inspectStringType = (value: ts.Expression): void => {
1949
+ if (found !== null) return;
1950
+ const type = this.tast.typeOf(value);
1951
+ if (type.isStringLiteral()) {
1952
+ inspectText(type.value);
1953
+ return;
1954
+ }
1955
+ if (type.isUnion()) {
1956
+ for (const member of type.types) {
1957
+ if (member.isStringLiteral()) inspectText(member.value);
1958
+ }
1959
+ }
1960
+ };
1961
+ const inspectOutput = (value: ts.Expression): void => {
1962
+ if (found !== null) return;
1963
+ if (ts.isStringLiteralLike(value)) {
1964
+ inspectText(value.text);
1965
+ } else if (ts.isTemplateExpression(value)) {
1966
+ inspectText(value.head.text);
1967
+ for (const span of value.templateSpans) {
1968
+ inspectOutput(span.expression);
1969
+ inspectText(span.literal.text);
1970
+ }
1971
+ } else if (ts.isConditionalExpression(value)) {
1972
+ inspectOutput(value.whenTrue);
1973
+ inspectOutput(value.whenFalse);
1974
+ } else if (
1975
+ ts.isParenthesizedExpression(value) ||
1976
+ ts.isAsExpression(value) ||
1977
+ ts.isTypeAssertionExpression(value) ||
1978
+ ts.isNonNullExpression(value)
1979
+ ) {
1980
+ inspectOutput(value.expression);
1981
+ } else {
1982
+ inspectStringType(value);
1983
+ }
1984
+ };
1985
+ inspectOutput(expr);
1986
+ return found;
1987
+ }
1988
+
1586
1989
  private walk(root: ts.Node): void {
1587
1990
  const visit = (node: ts.Node): void => {
1588
1991
  // NS1002 — synchronous updates.
@@ -1751,6 +2154,23 @@ export class SubsetChecker {
1751
2154
  this.report("NS1018", "A tagged template runs a function over its strings at runtime.", node);
1752
2155
  }
1753
2156
 
2157
+ // NS1064 — the narrow bridge fails closed on Unicode. The check is by
2158
+ // resolved SDK identity (import renames honored), never by call text.
2159
+ if (ts.isCallExpression(node) && this.sdkRootFunctionName(node.expression) === "asciiBytes") {
2160
+ const arg = node.arguments[0];
2161
+ if (arg) {
2162
+ const character = this.firstNonAsciiLiteral(arg);
2163
+ if (character !== null) {
2164
+ const codePoint = character.codePointAt(0) ?? 0;
2165
+ this.report(
2166
+ "NS1064",
2167
+ `\`asciiBytes(...)\` contains non-ASCII U+${codePoint.toString(16).toUpperCase().padStart(4, "0")}.`,
2168
+ arg,
2169
+ );
2170
+ }
2171
+ }
2172
+ }
2173
+
1754
2174
  // NS1045 — destructuring: record fields into const locals only.
1755
2175
  if (ts.isArrayBindingPattern(node)) {
1756
2176
  // `for (const [i, x] of xs.entries())` keeps its own tailored
@@ -2074,9 +2494,8 @@ export class SubsetChecker {
2074
2494
  }
2075
2495
  }
2076
2496
 
2077
- // NS1028 — persist compiles and stays on the wire, but no shipping
2078
- // host performs it yet; teach the writeFile path without stopping
2079
- // the build.
2497
+ // NS1028 — the reserved verb compiles either way, but a shipping app
2498
+ // binds its host service only when app.zon declares the capability.
2080
2499
  if (
2081
2500
  ts.isCallExpression(node) &&
2082
2501
  ts.isPropertyAccessExpression(node.expression) &&
@@ -2085,7 +2504,101 @@ export class SubsetChecker {
2085
2504
  this.cmdNames.has(node.expression.expression.text) &&
2086
2505
  this.isSdkReference(node.expression.expression)
2087
2506
  ) {
2088
- this.warn("NS1028", "`Cmd.persist()` asks for a host service no shipping host provides yet.", node);
2507
+ this.usesPersist = true;
2508
+ if (!this.capabilities.has("persist")) {
2509
+ this.warn("NS1028", "`Cmd.persist()` requires the `persist` capability in app.zon.", node);
2510
+ }
2511
+ }
2512
+
2513
+ // NS1069 — the nested record-store factories remain capability-bound;
2514
+ // recognizing the SDK Cmd symbol (rather than its spelling alone) keeps
2515
+ // local objects named Cmd out of this cross-file contract check.
2516
+ if (
2517
+ ts.isCallExpression(node) &&
2518
+ ts.isPropertyAccessExpression(node.expression) &&
2519
+ ts.isPropertyAccessExpression(node.expression.expression) &&
2520
+ node.expression.expression.name.text === "store" &&
2521
+ ts.isIdentifier(node.expression.expression.expression) &&
2522
+ this.cmdNames.has(node.expression.expression.expression.text) &&
2523
+ this.isSdkReference(node.expression.expression.expression)
2524
+ ) {
2525
+ this.usesStore = true;
2526
+ if (!this.capabilities.has("store")) {
2527
+ this.warn("NS1069", "`Cmd.store.*` requires the `store` capability in app.zon.", node);
2528
+ }
2529
+ }
2530
+
2531
+ // NS1070 — raw relational effects are a separate capability from the
2532
+ // record store even though both tiers share the vendored SQLite object.
2533
+ if (
2534
+ ts.isCallExpression(node) &&
2535
+ ts.isPropertyAccessExpression(node.expression) &&
2536
+ ts.isPropertyAccessExpression(node.expression.expression) &&
2537
+ node.expression.expression.name.text === "db" &&
2538
+ ts.isIdentifier(node.expression.expression.expression) &&
2539
+ this.cmdNames.has(node.expression.expression.expression.text) &&
2540
+ this.isSdkReference(node.expression.expression.expression)
2541
+ ) {
2542
+ this.usesSqlite = true;
2543
+ if (!this.capabilities.has("sqlite")) {
2544
+ this.warn("NS1070", "`Cmd.db.*` requires the `sqlite` capability in app.zon.", node);
2545
+ }
2546
+ if (node.expression.name.text === "query" && ts.isStringLiteralLike(node.arguments[0])) {
2547
+ this.warn("NS1420", "This raw Cmd.db.query uses a string literal that native check cannot name or generate.", node.arguments[0]);
2548
+ }
2549
+ }
2550
+
2551
+ // NS1071/NS1072 — keychain effects require both the build capability
2552
+ // and the runtime permission. Missing permission is a hard check error:
2553
+ // shipping a command guaranteed to receive `denied` is never useful.
2554
+ if (
2555
+ ts.isCallExpression(node) &&
2556
+ ts.isPropertyAccessExpression(node.expression) &&
2557
+ ts.isPropertyAccessExpression(node.expression.expression) &&
2558
+ node.expression.expression.name.text === "credentials" &&
2559
+ ts.isIdentifier(node.expression.expression.expression) &&
2560
+ this.cmdNames.has(node.expression.expression.expression.text) &&
2561
+ this.isSdkReference(node.expression.expression.expression)
2562
+ ) {
2563
+ this.usesCredentials = true;
2564
+ if (!this.capabilities.has("credentials")) {
2565
+ this.warn("NS1071", "`Cmd.credentials.*` requires the `credentials` capability in app.zon.", node);
2566
+ }
2567
+ if (!this.permissions.has("credentials")) {
2568
+ this.report("NS1072", "`Cmd.credentials.*` requires the `credentials` permission in app.zon.", node);
2569
+ }
2570
+ }
2571
+
2572
+ // NS1073 — the core credential wire names belong to the typed factory.
2573
+ // Raw Cmd.request payloads are app-controlled bytes and cannot state
2574
+ // the credential record contract safely at the authoring boundary.
2575
+ if (
2576
+ ts.isCallExpression(node) &&
2577
+ ts.isPropertyAccessExpression(node.expression) &&
2578
+ node.expression.name.text === "request" &&
2579
+ ts.isIdentifier(node.expression.expression) &&
2580
+ this.cmdNames.has(node.expression.expression.text) &&
2581
+ this.isSdkReference(node.expression.expression) &&
2582
+ node.arguments[0] !== undefined &&
2583
+ ts.isStringLiteral(node.arguments[0]) &&
2584
+ node.arguments[0].text.startsWith("core.credentials.")
2585
+ ) {
2586
+ this.usesCredentials = true;
2587
+ this.report("NS1073", "The `core.credentials.*` request namespace is reserved for `Cmd.credentials.*`.", node);
2588
+ }
2589
+
2590
+ // Generated declared-query constructors and live subscriptions are the
2591
+ // checked relational surface, and therefore count as sqlite use too.
2592
+ if (
2593
+ ts.isCallExpression(node) &&
2594
+ ts.isPropertyAccessExpression(node.expression) &&
2595
+ /^q[A-Z]/.test(node.expression.name.text) &&
2596
+ ts.isIdentifier(node.expression.expression) &&
2597
+ ((this.cmdNames.has(node.expression.expression.text) || this.subNames.has(node.expression.expression.text)) &&
2598
+ this.isSdkReference(node.expression.expression))
2599
+ ) {
2600
+ this.usesSqlite = true;
2601
+ if (!this.capabilities.has("sqlite")) this.warn("NS1070", "Generated `Cmd.q<Name>` and `Sub.q<Name>` require the `sqlite` capability in app.zon.", node);
2089
2602
  }
2090
2603
 
2091
2604
  // NS1001/NS1022/NS1051 — mutation stays inside local ownership: