@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
@@ -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,11 +502,25 @@ export class SubsetChecker {
465
502
  this.checkCmdPurity();
466
503
  this.checkSubPurity();
467
504
  this.checkModelBindingSurface();
505
+ this.checkMigrationHook();
468
506
  this.checkThemePackHelper();
507
+ this.checkStatusItemHelper();
469
508
  this.checkViewUnbound();
470
509
  this.checkReservedContractConsts();
471
510
  this.checkValueRecordAliases();
472
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
+ }
473
524
  this.checkExceptions();
474
525
  return {
475
526
  diagnostics: this.diagnostics,
@@ -479,6 +530,72 @@ export class SubsetChecker {
479
530
  };
480
531
  }
481
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
+
482
599
  private report(id: RuleId, site: string, node: ts.Node): void {
483
600
  const file = node.getSourceFile();
484
601
  const { line, column } = lineColumn(file, node.getStart());
@@ -511,7 +628,7 @@ export class SubsetChecker {
511
628
  }
512
629
  if (bindings && ts.isNamespaceImport(bindings)) {
513
630
  // NS1039 (half 2): the intrinsic SDK surface is imported by name —
514
- // the purity rules (NS1017/NS1025) and the asciiBytes fold recognize
631
+ // the purity rules (NS1017/NS1025) and byte-text folds recognize
515
632
  // the factories by their imported names.
516
633
  this.report("NS1039", `\`import * as ${bindings.name.text}\` aliases the intrinsic SDK module.`, bindings);
517
634
  continue;
@@ -721,6 +838,162 @@ export class SubsetChecker {
721
838
  }
722
839
  }
723
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
+
724
997
  /// NS1032 — `export const viewUnbound = [...] as const`: the dead-state
725
998
  /// lint opt-out. Every entry must be a string literal naming a Model
726
999
  /// field, an exported model helper, or a Msg kind; the emitter routes the
@@ -1151,8 +1424,8 @@ export class SubsetChecker {
1151
1424
  /// the host-event channels from src/core.ts only. Imports may FEED those
1152
1425
  /// entry points, but the exports themselves live in the entry module.
1153
1426
  private static readonly entryOnlyExports = new Set([
1154
- "update", "initialModel", "subscriptions",
1155
- "commandMsg", "keyMsg", "frameMsg", "pinchMsg", "dropMsg", "appearanceMsg", "chromeMsg", "envMsgs", "themePack",
1427
+ "update", "initialModel", "subscriptions", "migrate",
1428
+ "commandMsg", "keyMsg", "frameMsg", "pinchMsg", "dropMsg", "appearanceMsg", "chromeMsg", "envMsgs", "themePack", "statusItem",
1156
1429
  "viewUnbound", "modelUnbound", "msgUnbound",
1157
1430
  ]);
1158
1431
 
@@ -1273,10 +1546,13 @@ export class SubsetChecker {
1273
1546
  /// module-level type-origin table and each union's payload-member table so
1274
1547
  /// a valid TypeScript name fails with a teaching instead of duplicate Zig.
1275
1548
  private checkGeneratedMetadataNames(): void {
1276
- const reportTypeOrigins = (name: ts.Identifier): void => {
1549
+ const reportGeneratedName = (name: ts.Identifier): void => {
1277
1550
  if (name.text === "type_origins") {
1278
1551
  this.report("NS1038", "`type_origins` collides with the generated contract type-origin metadata declaration.", name);
1279
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
+ }
1280
1556
  };
1281
1557
  for (const file of this.files) {
1282
1558
  for (const stmt of file.statements) {
@@ -1286,10 +1562,10 @@ export class SubsetChecker {
1286
1562
  ts.isClassDeclaration(stmt) ||
1287
1563
  ts.isFunctionDeclaration(stmt)
1288
1564
  ) {
1289
- if (stmt.name) reportTypeOrigins(stmt.name);
1565
+ if (stmt.name) reportGeneratedName(stmt.name);
1290
1566
  } else if (ts.isVariableStatement(stmt)) {
1291
1567
  for (const decl of stmt.declarationList.declarations) {
1292
- if (ts.isIdentifier(decl.name)) reportTypeOrigins(decl.name);
1568
+ if (ts.isIdentifier(decl.name)) reportGeneratedName(decl.name);
1293
1569
  }
1294
1570
  }
1295
1571
  }
@@ -1643,6 +1919,73 @@ export class SubsetChecker {
1643
1919
  return t.isUnion() && t.types.every((m) => m.isStringLiteral());
1644
1920
  }
1645
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
+
1646
1989
  private walk(root: ts.Node): void {
1647
1990
  const visit = (node: ts.Node): void => {
1648
1991
  // NS1002 — synchronous updates.
@@ -1811,6 +2154,23 @@ export class SubsetChecker {
1811
2154
  this.report("NS1018", "A tagged template runs a function over its strings at runtime.", node);
1812
2155
  }
1813
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
+
1814
2174
  // NS1045 — destructuring: record fields into const locals only.
1815
2175
  if (ts.isArrayBindingPattern(node)) {
1816
2176
  // `for (const [i, x] of xs.entries())` keeps its own tailored
@@ -2134,9 +2494,8 @@ export class SubsetChecker {
2134
2494
  }
2135
2495
  }
2136
2496
 
2137
- // NS1028 — persist compiles and stays on the wire, but no shipping
2138
- // host performs it yet; teach the writeFile path without stopping
2139
- // 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.
2140
2499
  if (
2141
2500
  ts.isCallExpression(node) &&
2142
2501
  ts.isPropertyAccessExpression(node.expression) &&
@@ -2145,7 +2504,101 @@ export class SubsetChecker {
2145
2504
  this.cmdNames.has(node.expression.expression.text) &&
2146
2505
  this.isSdkReference(node.expression.expression)
2147
2506
  ) {
2148
- 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);
2149
2602
  }
2150
2603
 
2151
2604
  // NS1001/NS1022/NS1051 — mutation stays inside local ownership: