@native-sdk/cli 0.9.3 → 0.9.5

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 (84) hide show
  1. package/README.md +3 -3
  2. package/build/app.zig +323 -76
  3. package/build/ts_run.mjs +15 -18
  4. package/build.zig +83 -8
  5. package/package.json +14 -10
  6. package/packages/core/compile-surface/core.ts +1 -0
  7. package/packages/core/package-lock.json +16 -13
  8. package/packages/core/package.json +5 -2
  9. package/packages/core/scripts/compiler_command.mjs +71 -0
  10. package/packages/core/scripts/compiler_typecheck.mjs +4 -4
  11. package/packages/core/scripts/embed_markup_c.mjs +25 -0
  12. package/packages/core/scripts/npm_static_spike.mjs +3 -3
  13. package/packages/core/scripts/run_external_core_compiler.mjs +21 -10
  14. package/packages/core/scripts/run_external_service_compiler.mjs +17 -7
  15. package/packages/core/scripts/vendor_service_packages.mjs +121 -8
  16. package/packages/core/sdk/core.ts +1 -0
  17. package/packages/core/sdk/events.d.ts +36 -4
  18. package/packages/core/sdk/events.ts +49 -6
  19. package/packages/core/src/checker.ts +106 -10
  20. package/packages/core/src/modules.ts +6 -2
  21. package/packages/core/src/sqlite_cli.ts +22 -2
  22. package/packages/core/src/typed_ast.ts +4 -2
  23. package/packages/core/src/types.ts +8 -8
  24. package/schemas/app.schema.json +435 -0
  25. package/skill-data/core/SKILL.md +53 -15
  26. package/skill-data/core/references/project-anatomy.md +6 -6
  27. package/skill-data/native-ui/SKILL.md +28 -6
  28. package/skill-data/ts-core/SKILL.md +9 -6
  29. package/skill-data/ts-services/references/service-surface.md +1 -1
  30. package/skills/native-sdk/SKILL.md +2 -2
  31. package/src/app_runner/ts_core_main.zig +12 -4
  32. package/src/app_runner/ts_core_mobile.zig +6 -2
  33. package/src/automation/snapshot.zig +91 -3
  34. package/src/platform/macos/appkit_host.h +41 -2
  35. package/src/platform/macos/appkit_host.m +198 -9
  36. package/src/platform/macos/cef_host.mm +191 -9
  37. package/src/platform/macos/root.zig +110 -3
  38. package/src/platform/null_platform.zig +50 -1
  39. package/src/platform/null_platform_tests.zig +53 -0
  40. package/src/platform/root.zig +9 -0
  41. package/src/platform/types.zig +58 -0
  42. package/src/platform/windows/root.zig +88 -15
  43. package/src/primitives/canvas/reference.zig +37 -15
  44. package/src/primitives/canvas/reference_tests.zig +63 -0
  45. package/src/primitives/canvas/ui.zig +9 -0
  46. package/src/primitives/canvas/ui_markup.zig +29 -0
  47. package/src/primitives/canvas/ui_markup_compiled.zig +32 -0
  48. package/src/primitives/canvas/ui_markup_compiled_tests.zig +4 -2
  49. package/src/primitives/canvas/ui_markup_contract.zig +7 -0
  50. package/src/primitives/canvas/ui_markup_contract_tests.zig +21 -0
  51. package/src/primitives/canvas/ui_markup_tests.zig +6 -0
  52. package/src/primitives/canvas/ui_markup_view.zig +28 -0
  53. package/src/primitives/canvas/ui_markup_view_tests.zig +32 -3
  54. package/src/primitives/canvas/ui_schema.zig +9 -0
  55. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  56. package/src/primitives/canvas/ui_tests.zig +4 -2
  57. package/src/primitives/canvas/widget_render.zig +7 -2
  58. package/src/primitives/canvas/widget_semantics_tests.zig +3 -1
  59. package/src/root.zig +4 -0
  60. package/src/runtime/automation_snapshot.zig +3 -0
  61. package/src/runtime/flow.zig +1 -1
  62. package/src/runtime/state.zig +14 -0
  63. package/src/runtime/system_services.zig +62 -0
  64. package/src/runtime/ts_ui_app.zig +181 -39
  65. package/src/runtime/ui_app.zig +40 -1
  66. package/src/runtime/ui_app_tests.zig +63 -0
  67. package/src/runtime/ui_app_window_tests.zig +23 -0
  68. package/src/runtime/validation.zig +87 -1
  69. package/src/tooling/android.zig +5 -4
  70. package/src/tooling/buildgraph.zig +24 -8
  71. package/src/tooling/db.zig +1 -1
  72. package/src/tooling/doctor.zig +1 -1
  73. package/src/tooling/ios.zig +5 -4
  74. package/src/tooling/json_to_zon.zig +136 -0
  75. package/src/tooling/manifest.zig +120 -33
  76. package/src/tooling/raw_manifest.zig +4 -0
  77. package/src/tooling/root.zig +1 -0
  78. package/src/tooling/templates.zig +171 -118
  79. package/src/tooling/ts_core.zig +12 -8
  80. package/src/tooling/verbs.zig +233 -5
  81. package/src/tooling/web_engine.zig +17 -1
  82. package/tools/corewire/emit_profile.zig +20 -7
  83. package/tools/corewire/emit_service.zig +49 -24
  84. package/tools/corewire/main.zig +21 -2
@@ -74,7 +74,7 @@ fn nodeMissing() Error {
74
74
  std.debug.print(
75
75
  \\TypeScript app cores need node on PATH (the @native-sdk/core frontend and the
76
76
  \\core dev-harness run under it; the binary you ship carries no JS runtime).
77
- \\Install Node.js 22.15+ (on the 23 line: 23.5+) - https://nodejs.org or
77
+ \\Install Node.js 24+ - https://nodejs.org or
78
78
  \\`brew install node` - and re-run.
79
79
  \\
80
80
  , .{});
@@ -480,13 +480,17 @@ pub fn generateSqliteSurface(
480
480
  errdefer allocator.free(sdk_out);
481
481
  var child = std.process.spawn(io, .{
482
482
  .argv = &.{
483
- "node", runner, sqlite_cli,
484
- "--src", "src", "--sdk-in",
485
- sdk_in, "--static-in", static_in,
486
- "--sdk-out", sdk_out, "--dts-out",
487
- ".native/cache/sqlite/core.d.ts", "--static-out", ".native/cache/sqlite/core_static.ts",
488
- "--zig-out", ".native/cache/sqlite/migrations.zig", "--metadata-out",
489
- ".native/cache/sqlite/metadata.json", "--state", "src/schema/migrations.lock.json",
483
+ "node", runner, sqlite_cli,
484
+ "--src", "src", "--sdk-in",
485
+ sdk_in, "--static-in", static_in,
486
+ "--sdk-out", sdk_out, "--dts-out",
487
+ ".native/cache/sqlite/core.d.ts", "--static-out", ".native/cache/sqlite/core_static.ts",
488
+ "--sdk-events-out", ".native/cache/sqlite/events.ts", "--sdk-text-out",
489
+ ".native/cache/sqlite/text.ts", "--sdk-bytes-text-methods-out", ".native/cache/sqlite/bytes_text_methods.d.ts",
490
+ "--sdk-events-dts-out", ".native/cache/sqlite/events.d.ts", "--sdk-text-dts-out",
491
+ ".native/cache/sqlite/text.d.ts", "--zig-out", ".native/cache/sqlite/migrations.zig",
492
+ "--metadata-out", ".native/cache/sqlite/metadata.json", "--state",
493
+ "src/schema/migrations.lock.json",
490
494
  },
491
495
  .stdin = .ignore,
492
496
  .stdout = .inherit,
@@ -1,7 +1,7 @@
1
1
  //! The canonical app verbs: `native dev|build|test` work in any app
2
2
  //! directory. If the app owns a build.zig (ejected, and every example),
3
3
  //! the verbs drive it through plain `zig build` — zero behavior change.
4
- //! Otherwise (app.zon + src/ only) the CLI synthesizes the build graph
4
+ //! Otherwise (app.json/app.zon + src/ only) the CLI synthesizes the build graph
5
5
  //! into `<app>/.native/build/` (see buildgraph.zig) and drives that.
6
6
  //!
7
7
  //! Callers are expected to have chdir'd into the app directory: every
@@ -38,18 +38,25 @@ pub const Options = struct {
38
38
  url_override: ?[]const u8 = null,
39
39
  command_override: ?[]const []const u8 = null,
40
40
  timeout_ms: ?u32 = null,
41
+ /// Explain authored-input changes and the content-gated downstream graph.
42
+ explain_rebuild: bool = false,
41
43
  };
42
44
 
43
45
  pub fn run(allocator: std.mem.Allocator, io: std.Io, verb: Verb, options: Options) !void {
44
- if (!buildgraph.fileExists(io, "app.zon")) {
46
+ const manifest_path = manifest_tool.defaultPath(io) orelse {
45
47
  std.debug.print(
46
- \\no app.zon here — `native {s}` runs inside an app directory
48
+ \\no app.json or app.zon here — `native {s}` runs inside an app directory
47
49
  \\(or pass one: `native {s} path/to/app`). Start one with `native init`.
48
50
  \\
49
51
  , .{ @tagName(verb), @tagName(verb) });
50
52
  return error.MissingManifest;
51
- }
52
- const metadata = try manifest_tool.readMetadata(allocator, io, "app.zon");
53
+ };
54
+ const metadata = try manifest_tool.readMetadata(allocator, io, manifest_path);
55
+ const rebuild_snapshot = if (options.explain_rebuild)
56
+ try explainRebuild(allocator, io)
57
+ else
58
+ null;
59
+ defer if (rebuild_snapshot) |snapshot| allocator.free(snapshot);
53
60
 
54
61
  // Tree detection happens here too (the build graph re-derives it), so
55
62
  // a both-cores tree fails with one clean teaching message before any
@@ -105,6 +112,7 @@ pub fn run(allocator: std.mem.Allocator, io: std.Io, verb: Verb, options: Option
105
112
  build_file = try buildgraph.ensureGeneratedBuild(allocator, io, ".", .{
106
113
  .app_name = metadata.name,
107
114
  .framework_root = framework_root,
115
+ .manifest_name = manifest_path,
108
116
  });
109
117
  // Keep artifacts where users expect them: the generated build root
110
118
  // is .native/build/, so without a prefix the binary would hide in
@@ -122,6 +130,12 @@ pub fn run(allocator: std.mem.Allocator, io: std.Io, verb: Verb, options: Option
122
130
  switch (verb) {
123
131
  .dev => {
124
132
  if (!wants_frontend_dev) try argv.append(allocator, "run");
133
+ // Zig's full summary reports each named build step's duration,
134
+ // cache status, and peak RSS. Native's generated graph names the
135
+ // frontend, corewire, staging, scriptc, and final Zig phases, so
136
+ // slow iterative builds are attributable without reproducing
137
+ // them under an external profiler.
138
+ try argv.appendSlice(allocator, &.{ "--summary", "all" });
125
139
  if (!hasOptimizeFlag(options.forwarded_args)) {
126
140
  // The dev loop is a Debug loop: the markup hot-reload
127
141
  // watcher and the teaching diagnostics are compiled in only
@@ -133,6 +147,8 @@ pub fn run(allocator: std.mem.Allocator, io: std.Io, verb: Verb, options: Option
133
147
  }
134
148
  },
135
149
  .build => {
150
+ try argv.appendSlice(allocator, &.{ "--summary", "all" });
151
+ if (options.explain_rebuild) try argv.append(allocator, "-Dbuild-trace=true");
136
152
  if (!hasOptimizeFlag(options.forwarded_args)) {
137
153
  // Both build shapes register -Doptimize (addApp and the
138
154
  // expanded template each b.option it by hand).
@@ -157,6 +173,7 @@ pub fn run(allocator: std.mem.Allocator, io: std.Io, verb: Verb, options: Option
157
173
  }
158
174
 
159
175
  try runZig(io, verb, argv.items);
176
+ if (rebuild_snapshot) |snapshot| try persistRebuildSnapshot(io, snapshot);
160
177
 
161
178
  if (wants_frontend_dev) {
162
179
  // WebView apps with a dev server config: the binary is built, now
@@ -219,6 +236,168 @@ fn hasOptimizeFlag(args: []const []const u8) bool {
219
236
  return false;
220
237
  }
221
238
 
239
+ const rebuild_snapshot_path = ".native/cache/rebuild-inputs.tsv";
240
+
241
+ const RebuildInput = struct {
242
+ path: []const u8,
243
+ hash: [std.crypto.hash.sha2.Sha256.digest_length * 2]u8,
244
+ };
245
+
246
+ fn appendRebuildInput(allocator: std.mem.Allocator, io: std.Io, inputs: *std.ArrayList(RebuildInput), path: []const u8) !void {
247
+ const owned_path = try allocator.dupe(u8, path);
248
+ errdefer allocator.free(owned_path);
249
+ var file = try std.Io.Dir.cwd().openFile(io, path, .{});
250
+ defer file.close(io);
251
+ var read_buffer: [64 * 1024]u8 = undefined;
252
+ var reader = file.reader(io, &read_buffer);
253
+ var hasher = std.crypto.hash.sha2.Sha256.init(.{});
254
+ var chunk: [64 * 1024]u8 = undefined;
255
+ while (true) {
256
+ const len = try reader.interface.readSliceShort(&chunk);
257
+ hasher.update(chunk[0..len]);
258
+ if (len < chunk.len) break;
259
+ }
260
+ const digest = hasher.finalResult();
261
+ try inputs.append(allocator, .{ .path = owned_path, .hash = std.fmt.bytesToHex(digest, .lower) });
262
+ }
263
+
264
+ fn rebuildPathIgnored(path: []const u8) bool {
265
+ var components = std.mem.splitScalar(u8, path, '/');
266
+ while (components.next()) |component| {
267
+ if (std.mem.eql(u8, component, ".git") or
268
+ std.mem.eql(u8, component, ".native") or
269
+ std.mem.eql(u8, component, ".zig-cache") or
270
+ std.mem.eql(u8, component, "zig-out") or
271
+ std.mem.eql(u8, component, "node_modules")) return true;
272
+ }
273
+ return false;
274
+ }
275
+
276
+ fn rebuildInputs(allocator: std.mem.Allocator, io: std.Io) ![]RebuildInput {
277
+ var inputs: std.ArrayList(RebuildInput) = .empty;
278
+ errdefer {
279
+ for (inputs.items) |input| allocator.free(input.path);
280
+ inputs.deinit(allocator);
281
+ }
282
+ // Ejected builds can consume any authored file, and even the standard
283
+ // graph has non-extension inputs: Zig sources, migration lock JSON,
284
+ // vendored service packages, assets, and frontend configuration. Hash the
285
+ // app tree instead of maintaining an inevitably incomplete suffix list;
286
+ // exclude only directories owned by VCS, dependency installs, and build
287
+ // outputs (including this snapshot itself under .native/).
288
+ var root = try std.Io.Dir.cwd().openDir(io, ".", .{ .iterate = true });
289
+ defer root.close(io);
290
+ var walker = try root.walkSelectively(allocator);
291
+ defer walker.deinit();
292
+ while (try walker.next(io)) |entry| {
293
+ const path = try allocator.dupe(u8, entry.path);
294
+ defer allocator.free(path);
295
+ for (path) |*char| if (char.* == '\\') {
296
+ char.* = '/';
297
+ };
298
+ if (entry.kind == .directory) {
299
+ if (!rebuildPathIgnored(path)) try walker.enter(io, entry);
300
+ continue;
301
+ }
302
+ if (entry.kind != .file) continue;
303
+ if (rebuildPathIgnored(path)) continue;
304
+ try appendRebuildInput(allocator, io, &inputs, path);
305
+ }
306
+ std.mem.sort(RebuildInput, inputs.items, {}, struct {
307
+ fn less(_: void, a: RebuildInput, b: RebuildInput) bool {
308
+ return std.mem.order(u8, a.path, b.path) == .lt;
309
+ }
310
+ }.less);
311
+ return inputs.toOwnedSlice(allocator);
312
+ }
313
+
314
+ fn explainRebuild(allocator: std.mem.Allocator, io: std.Io) ![]u8 {
315
+ const inputs = try rebuildInputs(allocator, io);
316
+ defer {
317
+ for (inputs) |input| allocator.free(input.path);
318
+ allocator.free(inputs);
319
+ }
320
+ const prior = std.Io.Dir.cwd().readFileAlloc(io, rebuild_snapshot_path, allocator, .limited(16 * 1024 * 1024)) catch null;
321
+ defer if (prior) |bytes| allocator.free(bytes);
322
+
323
+ var snapshot = std.Io.Writer.Allocating.init(allocator);
324
+ errdefer snapshot.deinit();
325
+ var changed: usize = 0;
326
+ for (inputs) |input| {
327
+ try snapshot.writer.print("{s}\t{s}\n", .{ &input.hash, input.path });
328
+ const old_hash: ?[]const u8 = if (prior) |bytes| old: {
329
+ var lines = std.mem.splitScalar(u8, bytes, '\n');
330
+ while (lines.next()) |line| {
331
+ const tab = std.mem.indexOfScalar(u8, line, '\t') orelse continue;
332
+ if (std.mem.eql(u8, line[tab + 1 ..], input.path)) break :old line[0..tab];
333
+ }
334
+ break :old null;
335
+ } else null;
336
+ if (old_hash != null and std.mem.eql(u8, old_hash.?, &input.hash)) continue;
337
+ changed += 1;
338
+ std.debug.print("native rebuild: {s} {s} -> {s}\n", .{ input.path, old_hash orelse "<new>", &input.hash });
339
+ if (std.mem.eql(u8, input.path, "app.json") or std.mem.eql(u8, input.path, "app.zon")) {
340
+ std.debug.print(" invalidates manifest-derived configuration -> affected contracts, app-code/platform link, and final artifact\n", .{});
341
+ } else if (std.mem.eql(u8, input.path, "build.zig") or std.mem.eql(u8, input.path, "build.zig.zon")) {
342
+ std.debug.print(" invalidates the owned build graph -> graph-selected compilation and final artifact\n", .{});
343
+ } else if (std.mem.eql(u8, input.path, "src/app.native")) {
344
+ std.debug.print(" invalidates markup data object -> final app link; app-code/core scriptc remain content-gated\n", .{});
345
+ } else if (std.mem.endsWith(u8, input.path, ".native")) {
346
+ std.debug.print(" invalidates staged markup source set -> app-code object -> final link; core scriptc remains content-gated\n", .{});
347
+ } else if (std.mem.startsWith(u8, input.path, "src/services/")) {
348
+ std.debug.print(" invalidates TS frontend -> service contract/source hash -> service host compile; API client/registry/core scriptc change only if API shape changes\n", .{});
349
+ } else if (std.mem.endsWith(u8, input.path, ".ts")) {
350
+ std.debug.print(" invalidates TS frontend -> core contract/facade/stage -> core scriptc -> app-code object -> final link\n", .{});
351
+ } else if (std.mem.endsWith(u8, input.path, ".zig")) {
352
+ std.debug.print(" invalidates Zig build/app code -> affected compile and final link\n", .{});
353
+ } else if (std.mem.endsWith(u8, input.path, ".sql") or std.mem.endsWith(u8, input.path, "/migrations.lock.json")) {
354
+ std.debug.print(" invalidates SQLite schema/migration generation -> app-code object -> final link\n", .{});
355
+ } else if (std.mem.startsWith(u8, input.path, "frontend/")) {
356
+ std.debug.print(" invalidates frontend build inputs -> bundled web assets and final package\n", .{});
357
+ } else if (std.mem.startsWith(u8, input.path, "assets/")) {
358
+ std.debug.print(" invalidates authored assets -> affected runtime/package artifact\n", .{});
359
+ } else {
360
+ std.debug.print(" changes an authored app input -> the build graph determines affected downstream steps\n", .{});
361
+ }
362
+ }
363
+ if (prior) |bytes| {
364
+ var lines = std.mem.splitScalar(u8, bytes, '\n');
365
+ while (lines.next()) |line| {
366
+ const tab = std.mem.indexOfScalar(u8, line, '\t') orelse continue;
367
+ const old_hash = line[0..tab];
368
+ const old_path = line[tab + 1 ..];
369
+ const present = for (inputs) |input| {
370
+ if (std.mem.eql(u8, input.path, old_path)) break true;
371
+ } else false;
372
+ if (present) continue;
373
+ changed += 1;
374
+ std.debug.print("native rebuild: {s} {s} -> <deleted>\n", .{ old_path, old_hash });
375
+ if (std.mem.startsWith(u8, old_path, "src/services/")) {
376
+ std.debug.print(" invalidates TS frontend -> service API/source set -> service host; core scriptc changes if the removed module changed API shape\n", .{});
377
+ } else if (std.mem.eql(u8, old_path, "src/app.native")) {
378
+ std.debug.print(" invalidates primary markup data object -> final app link\n", .{});
379
+ } else if (std.mem.endsWith(u8, old_path, ".native")) {
380
+ std.debug.print(" invalidates markup source set -> app-code object -> final link\n", .{});
381
+ } else if (std.mem.endsWith(u8, old_path, ".zig")) {
382
+ std.debug.print(" invalidates Zig build/app code -> affected compile and final link\n", .{});
383
+ } else {
384
+ std.debug.print(" removes an authored app input -> the build graph determines affected downstream steps\n", .{});
385
+ }
386
+ }
387
+ }
388
+ if (prior == null) std.debug.print("native rebuild: no prior successful snapshot; current inputs are the baseline\n", .{});
389
+ if (prior != null and changed == 0) std.debug.print("native rebuild: no authored input hashes changed\n", .{});
390
+ return snapshot.toOwnedSlice();
391
+ }
392
+
393
+ fn persistRebuildSnapshot(io: std.Io, snapshot: []const u8) !void {
394
+ try std.Io.Dir.cwd().createDirPath(io, ".native/cache");
395
+ var atomic = try std.Io.Dir.cwd().createFileAtomic(io, rebuild_snapshot_path, .{ .make_path = true, .replace = true });
396
+ defer atomic.deinit(io);
397
+ try atomic.file.writeStreamingAll(io, snapshot);
398
+ try atomic.replace(io);
399
+ }
400
+
222
401
  fn runZig(io: std.Io, verb: Verb, argv: []const []const u8) !void {
223
402
  // The dev verb owns a whole process TREE: `zig build run` spawns the
224
403
  // app as its child, and a `native dev` that dies (Ctrl-C, a driver
@@ -318,3 +497,52 @@ test "optimize flags are detected among forwarded args" {
318
497
  try std.testing.expect(!hasOptimizeFlag(&.{"-Dautomation=true"}));
319
498
  try std.testing.expect(!hasOptimizeFlag(&.{}));
320
499
  }
500
+
501
+ test "rebuild explanations persist manifest and source hashes" {
502
+ const allocator = std.testing.allocator;
503
+ const io = std.testing.io;
504
+ const root = ".zig-cache/test-rebuild-explain";
505
+ var cwd = std.Io.Dir.cwd();
506
+ cwd.deleteTree(io, root) catch {};
507
+ defer cwd.deleteTree(io, root) catch {};
508
+ try cwd.createDirPath(io, root ++ "/src/services");
509
+ try cwd.createDirPath(io, root ++ "/src/schema");
510
+ try cwd.createDirPath(io, root ++ "/assets");
511
+ try cwd.createDirPath(io, root ++ "/.native/cache");
512
+ try cwd.createDirPath(io, root ++ "/node_modules/package");
513
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/app.zon", .data = ".{ .name = \"rebuild-test\" }\n" });
514
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/build.zig", .data = "pub fn build() void {}\n" });
515
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/src/core.ts", .data = "export const core = 1;\n" });
516
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/src/main.zig", .data = "pub fn main() void {}\n" });
517
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/src/app.native", .data = "<column/>\n" });
518
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/src/services/work.ts", .data = "export function work(): number { return 1; }\n" });
519
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/src/schema/migrations.lock.json", .data = "{}\n" });
520
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/assets/icon.bin", .data = "authored asset\n" });
521
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/.native/cache/generated", .data = "generated\n" });
522
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/node_modules/package/index.js", .data = "dependency\n" });
523
+ const original = try std.process.currentPathAlloc(io, allocator);
524
+ defer allocator.free(original);
525
+ try std.process.setCurrentPath(io, root);
526
+ defer std.process.setCurrentPath(io, original) catch {};
527
+ const first = try explainRebuild(allocator, io);
528
+ defer allocator.free(first);
529
+ try std.testing.expect(std.mem.indexOf(u8, first, "\tapp.zon\n") != null);
530
+ try std.testing.expect(std.mem.indexOf(u8, first, "\tbuild.zig\n") != null);
531
+ try std.testing.expect(std.mem.indexOf(u8, first, "\tsrc/main.zig\n") != null);
532
+ try std.testing.expect(std.mem.indexOf(u8, first, "\tsrc/schema/migrations.lock.json\n") != null);
533
+ try std.testing.expect(std.mem.indexOf(u8, first, "\tassets/icon.bin\n") != null);
534
+ try std.testing.expect(std.mem.indexOf(u8, first, "\t.native/") == null);
535
+ try std.testing.expect(std.mem.indexOf(u8, first, "\tnode_modules/") == null);
536
+ try persistRebuildSnapshot(io, first);
537
+ const second = try explainRebuild(allocator, io);
538
+ defer allocator.free(second);
539
+ try std.testing.expectEqualStrings(first, second);
540
+ try cwd.writeFile(io, .{ .sub_path = "src/services/work.ts", .data = "// comment\nexport function work(): number { return 1; }\n" });
541
+ const changed = try explainRebuild(allocator, io);
542
+ defer allocator.free(changed);
543
+ try std.testing.expect(!std.mem.eql(u8, first, changed));
544
+ try cwd.writeFile(io, .{ .sub_path = "app.zon", .data = ".{ .name = \"rebuild-test\", .capabilities = .{\"store\"} }\n" });
545
+ const manifest_changed = try explainRebuild(allocator, io);
546
+ defer allocator.free(manifest_changed);
547
+ try std.testing.expect(!std.mem.eql(u8, changed, manifest_changed));
548
+ }
@@ -1,4 +1,5 @@
1
1
  const std = @import("std");
2
+ const json_to_zon = @import("json_to_zon.zig");
2
3
  const raw_manifest = @import("raw_manifest.zig");
3
4
 
4
5
  pub const default_engine: Engine = .system;
@@ -72,7 +73,10 @@ pub fn resolve(manifest: ManifestConfig, overrides: Overrides) Error!Resolved {
72
73
  pub fn readManifestConfig(allocator: std.mem.Allocator, io: std.Io, path: []const u8) !ManifestConfig {
73
74
  const source = try std.Io.Dir.cwd().readFileAlloc(io, path, allocator, .limited(1024 * 1024));
74
75
  defer allocator.free(source);
75
- return parseManifestConfig(allocator, source);
76
+ return if (json_to_zon.isJsonPath(path))
77
+ parseJsonManifestConfig(allocator, source)
78
+ else
79
+ parseManifestConfig(allocator, source);
76
80
  }
77
81
 
78
82
  pub fn parseManifestConfig(allocator: std.mem.Allocator, source: []const u8) !ManifestConfig {
@@ -82,6 +86,18 @@ pub fn parseManifestConfig(allocator: std.mem.Allocator, source: []const u8) !Ma
82
86
  const source_z = try scratch.dupeZ(u8, source);
83
87
  @setEvalBranchQuota(4000);
84
88
  const raw = try std.zon.parse.fromSliceAlloc(raw_manifest.RawManifest, scratch, source_z, null, .{});
89
+ return duplicateManifestConfig(allocator, raw);
90
+ }
91
+
92
+ pub fn parseJsonManifestConfig(allocator: std.mem.Allocator, source: []const u8) !ManifestConfig {
93
+ try json_to_zon.validateSource(allocator, source);
94
+ var arena = std.heap.ArenaAllocator.init(allocator);
95
+ defer arena.deinit();
96
+ const raw = try std.json.parseFromSliceLeaky(raw_manifest.RawManifest, arena.allocator(), source, .{ .ignore_unknown_fields = false });
97
+ return duplicateManifestConfig(allocator, raw);
98
+ }
99
+
100
+ fn duplicateManifestConfig(allocator: std.mem.Allocator, raw: raw_manifest.RawManifest) !ManifestConfig {
85
101
  return .{
86
102
  .web_engine = try allocator.dupe(u8, raw.web_engine),
87
103
  .cef = .{
@@ -153,7 +153,7 @@ const determinism_remediations = [_]Rider{
153
153
  .{ .code = "SC4017", .text = "restart the app process: a trapped core is poisoned and never resumes in place, and the recorded session replays deterministically." },
154
154
  };
155
155
 
156
- pub fn emitProfile(arena: std.mem.Allocator, sidecar: Sidecar, entry: []const u8, diags: *sidecar_mod.Diagnostics) Error![]const u8 {
156
+ pub fn emitProfile(arena: std.mem.Allocator, sidecar: Sidecar, entry: []const u8, optimization: ?[]const u8, diags: *sidecar_mod.Diagnostics) Error![]const u8 {
157
157
  // The profile is ENFORCEMENT data: a compile that succeeds under its
158
158
  // determinism fences attests deterministic, and the compilation mode
159
159
  // is structurally async-free — so a contract attesting false for
@@ -168,7 +168,7 @@ pub fn emitProfile(arena: std.mem.Allocator, sidecar: Sidecar, entry: []const u8
168
168
  diags.flag("async_free", "the contract attests async_free: false, but this profile's compilation mode is structurally async-free — a core reaching async surface refuses under it, so this contract cannot have come from the profile it asks for; remove the async surface and regenerate the contract", .{});
169
169
  }
170
170
  if (diags.hasErrors()) return error.Refused;
171
- var emitter = ProfileEmitter{ .arena = arena, .sidecar = sidecar, .out = .empty };
171
+ var emitter = ProfileEmitter{ .arena = arena, .sidecar = sidecar, .optimization = optimization, .out = .empty };
172
172
  try emitter.run(entry);
173
173
  return emitter.out.items;
174
174
  }
@@ -176,6 +176,7 @@ pub fn emitProfile(arena: std.mem.Allocator, sidecar: Sidecar, entry: []const u8
176
176
  const ProfileEmitter = struct {
177
177
  arena: std.mem.Allocator,
178
178
  sidecar: Sidecar,
179
+ optimization: ?[]const u8,
179
180
  out: std.ArrayListUnmanaged(u8),
180
181
 
181
182
  fn print(self: *ProfileEmitter, comptime fmt: []const u8, args: anytype) Error!void {
@@ -195,6 +196,14 @@ const ProfileEmitter = struct {
195
196
  \\ "name": "native-sdk-core",
196
197
  \\ "entry": {s},
197
198
  \\ "emission": "llvm",
199
+ , .{try self.jsonString(entry)});
200
+ if (self.optimization) |optimization| {
201
+ try self.print(
202
+ \\ "optimization": {s},
203
+ , .{try self.jsonString(optimization)});
204
+ try self.raw("\n");
205
+ }
206
+ try self.print(
198
207
  \\ "abi": {{
199
208
  \\ "prefix": {s},
200
209
  \\ "init_symbol": {s},
@@ -205,7 +214,6 @@ const ProfileEmitter = struct {
205
214
  \\ "exports": [
206
215
  \\
207
216
  , .{
208
- try self.jsonString(entry),
209
217
  try self.jsonString(prefix),
210
218
  try self.symbol(prefix, "init"),
211
219
  try self.symbol(prefix, "set_panic_sink"),
@@ -361,7 +369,7 @@ const testing = std.testing;
361
369
  fn profileFromJson(arena: std.mem.Allocator, json: []const u8, entry: []const u8) ![]const u8 {
362
370
  var diags = sidecar_mod.Diagnostics{ .arena = arena };
363
371
  const parsed = try sidecar_mod.read(arena, json, &diags);
364
- return emitProfile(arena, parsed, entry, &diags);
372
+ return emitProfile(arena, parsed, entry, "release", &diags);
365
373
  }
366
374
 
367
375
  test "profile emission is deterministic and carries the library-mode surface" {
@@ -373,6 +381,11 @@ test "profile emission is deterministic and carries the library-mode surface" {
373
381
  try testing.expectEqualStrings(first, second);
374
382
  try testing.expect(std.mem.indexOf(u8, first, "\"profile_format\": 1") != null);
375
383
  try testing.expect(std.mem.indexOf(u8, first, "\"entry\": \"core_facade.ts\"") != null);
384
+ try testing.expect(std.mem.indexOf(u8, first, "\"optimization\": \"release\"") != null);
385
+ var dev_diags = sidecar_mod.Diagnostics{ .arena = arena };
386
+ const parsed_for_dev = try sidecar_mod.read(arena, sidecar_mod.minimal_valid_json, &dev_diags);
387
+ const dev = try emitProfile(arena, parsed_for_dev, default_entry, "dev", &dev_diags);
388
+ try testing.expect(std.mem.indexOf(u8, dev, "\"optimization\": \"dev\"") != null);
376
389
  // Mode symbols ride the abi block under the contract's prefix.
377
390
  try testing.expect(std.mem.indexOf(u8, first, "\"init_symbol\": \"nsc_core_init\"") != null);
378
391
  try testing.expect(std.mem.indexOf(u8, first, "\"sink_register_symbol\": \"nsc_core_set_panic_sink\"") != null);
@@ -459,7 +472,7 @@ test "a non-UTF-8 entry spelling refuses instead of corrupting the JSON" {
459
472
  const arena = arena_state.allocator();
460
473
  var diags = sidecar_mod.Diagnostics{ .arena = arena };
461
474
  const parsed = try sidecar_mod.read(arena, sidecar_mod.minimal_valid_json, &diags);
462
- try testing.expectError(error.Refused, emitProfile(arena, parsed, "core_\xfffacade.ts", &diags));
475
+ try testing.expectError(error.Refused, emitProfile(arena, parsed, "core_\xfffacade.ts", "release", &diags));
463
476
  }
464
477
 
465
478
  test "a contract attesting false for an enforced posture refuses" {
@@ -473,7 +486,7 @@ test "a contract attesting false for an enforced posture refuses" {
473
486
  const source = try std.mem.replaceOwned(u8, arena, sidecar_mod.minimal_valid_json, case.needle, case.replacement);
474
487
  var diags = sidecar_mod.Diagnostics{ .arena = arena };
475
488
  const parsed = try sidecar_mod.read(arena, source, &diags);
476
- try testing.expectError(error.Refused, emitProfile(arena, parsed, default_entry, &diags));
489
+ try testing.expectError(error.Refused, emitProfile(arena, parsed, default_entry, "release", &diags));
477
490
  var found = false;
478
491
  for (diags.list.items) |item| {
479
492
  if (item.severity == .@"error" and std.mem.indexOf(u8, item.message, case.fragment) != null) found = true;
@@ -489,7 +502,7 @@ test "the emitted profile parses as JSON with the expected top-level keys" {
489
502
  const generated = try profileFromJson(arena, sidecar_mod.minimal_valid_json, default_entry);
490
503
  const parsed = try std.json.parseFromSliceLeaky(std.json.Value, arena, generated, .{});
491
504
  const top = parsed.object;
492
- for ([_][]const u8{ "profile_format", "name", "entry", "emission", "abi", "exports", "sidecar", "determinism" }) |key| {
505
+ for ([_][]const u8{ "profile_format", "name", "entry", "emission", "optimization", "abi", "exports", "sidecar", "determinism" }) |key| {
493
506
  try testing.expect(top.contains(key));
494
507
  }
495
508
  const determinism = top.get("determinism").?.object;
@@ -29,29 +29,24 @@ fn fingerprintType(hasher: *std.crypto.hash.sha2.Sha256, ref: service.TypeRef) v
29
29
  if (ref.name) |name| fingerprintField(hasher, name);
30
30
  }
31
31
 
32
- /// Identity of every fact that can change generated dispatch semantics. The
33
- /// same digest is embedded in the service executable and the Zig registry so
34
- /// a stale or hand-supplied sibling is refused before numeric indices are used.
32
+ /// Identity of the service ABI facts both carriers put on the wire. The same
33
+ /// digest is embedded in the service executable and the Zig registry so a
34
+ /// stale or hand-supplied sibling is refused before numeric indices are used.
35
+ ///
36
+ /// Deliberately excluded: compiler/package identities, implementation module
37
+ /// paths and exports, and `source_hash`. Those facts rebuild the service
38
+ /// executable, but they do not change operation indices or encoded values and
39
+ /// therefore must not rewrite the app runner or recompile the app core.
35
40
  fn contractFingerprint(contract: service.Contract) [fingerprint_len]u8 {
36
41
  var hasher = std.crypto.hash.sha2.Sha256.init(.{});
37
- hasher.update("native-sdk.services.contract.v3\x00");
42
+ hasher.update("native-sdk.services.abi.v3\x00");
38
43
  var scalar: [8]u8 = undefined;
39
44
  std.mem.writeInt(i64, &scalar, contract.format, .little);
40
45
  hasher.update(&scalar);
41
46
  std.mem.writeInt(i64, &scalar, contract.protocol_version, .little);
42
47
  hasher.update(&scalar);
43
- hasher.update(&.{@intFromBool(contract.deterministic)});
44
- fingerprintField(&hasher, contract.compiler_version);
45
- std.mem.writeInt(u64, &scalar, @intCast(contract.packages.len), .little);
46
- hasher.update(&scalar);
47
- for (contract.packages) |package_entry| {
48
- fingerprintField(&hasher, package_entry.name);
49
- fingerprintField(&hasher, package_entry.version);
50
- fingerprintField(&hasher, package_entry.content_hash);
51
- }
52
48
  for (contract.types.records) |record| {
53
49
  fingerprintField(&hasher, record.name);
54
- fingerprintField(&hasher, record.origin);
55
50
  for (record.fields) |field| {
56
51
  fingerprintField(&hasher, field.name);
57
52
  fingerprintType(&hasher, field.type);
@@ -59,12 +54,10 @@ fn contractFingerprint(contract: service.Contract) [fingerprint_len]u8 {
59
54
  }
60
55
  for (contract.types.enums) |enum_type| {
61
56
  fingerprintField(&hasher, enum_type.name);
62
- fingerprintField(&hasher, enum_type.origin);
63
57
  for (enum_type.members) |member| fingerprintField(&hasher, member);
64
58
  }
65
59
  for (contract.types.unions) |union_type| {
66
60
  fingerprintField(&hasher, union_type.name);
67
- fingerprintField(&hasher, union_type.origin);
68
61
  for (union_type.arms) |arm| {
69
62
  fingerprintField(&hasher, arm.name);
70
63
  for (arm.fields) |field| {
@@ -77,9 +70,6 @@ fn contractFingerprint(contract: service.Contract) [fingerprint_len]u8 {
77
70
  hasher.update(&scalar);
78
71
  for (contract.operations) |op| {
79
72
  fingerprintField(&hasher, op.name);
80
- fingerprintField(&hasher, op.client);
81
- fingerprintField(&hasher, op.module);
82
- fingerprintField(&hasher, op.@"export");
83
73
  fingerprintType(&hasher, op.request);
84
74
  fingerprintType(&hasher, op.result);
85
75
  std.mem.writeInt(i64, &scalar, op.deadline_ms orelse 0, .little);
@@ -91,7 +81,6 @@ fn contractFingerprint(contract: service.Contract) [fingerprint_len]u8 {
91
81
  std.mem.writeInt(i64, &scalar, stream.in_flight, .little);
92
82
  hasher.update(&scalar);
93
83
  } else hasher.update(&.{0});
94
- fingerprintField(&hasher, op.source_hash);
95
84
  }
96
85
  return hasher.finalResult();
97
86
  }
@@ -601,15 +590,23 @@ pub fn emitInprocMain(arena: std.mem.Allocator, contract: service.Contract) ![]c
601
590
  /// and linked by one build graph, and the facade's fingerprint export is
602
591
  /// the pairing check. No determinism fences — services run with ambient
603
592
  /// authority by contract.
604
- pub fn emitInprocProfile(arena: std.mem.Allocator) ![]const u8 {
593
+ pub fn emitInprocProfile(arena: std.mem.Allocator, optimization: ?[]const u8) ![]const u8 {
605
594
  var out = std.Io.Writer.Allocating.init(arena);
606
595
  const w = &out.writer;
607
- try w.print(
608
- \\{{
596
+ try w.writeAll(
597
+ \\{
609
598
  \\ "profile_format": 1,
610
599
  \\ "name": "native-sdk-services",
611
600
  \\ "entry": "service_inproc_main.ts",
612
601
  \\ "emission": "llvm",
602
+ );
603
+ if (optimization) |value| {
604
+ try w.print(
605
+ \\ "optimization": "{s}",
606
+ , .{value});
607
+ try w.writeByte('\n');
608
+ }
609
+ try w.print(
613
610
  \\ "abi": {{
614
611
  \\ "prefix": "{s}",
615
612
  \\ "init_symbol": "{s}init",
@@ -887,12 +884,13 @@ test "service projection derives host dispatch and registry from one contract" {
887
884
  try std.testing.expect(std.mem.indexOf(u8, inproc, "writeHello") == null);
888
885
  try std.testing.expect(std.mem.indexOf(u8, inproc, "requestId") == null);
889
886
 
890
- const profile = try emitInprocProfile(std.testing.allocator);
887
+ const profile = try emitInprocProfile(std.testing.allocator, "release");
891
888
  defer std.testing.allocator.free(profile);
892
889
  try std.testing.expect(std.mem.indexOf(u8, profile, "\"prefix\": \"nsc_svc_\"") != null);
893
890
  try std.testing.expect(std.mem.indexOf(u8, profile, "\"localize_runtime\": true") != null);
894
891
  try std.testing.expect(std.mem.indexOf(u8, profile, "\"instance_per_thread\": true") != null);
895
892
  try std.testing.expect(std.mem.indexOf(u8, profile, "\"entry\": \"service_inproc_main.ts\"") != null);
893
+ try std.testing.expect(std.mem.indexOf(u8, profile, "\"optimization\": \"release\"") != null);
896
894
  try std.testing.expect(std.mem.indexOf(u8, profile, "\"symbol\": \"nsc_svc_dispatch\"") != null);
897
895
 
898
896
  const registry = try emitRegistry(std.testing.allocator, contract);
@@ -910,6 +908,33 @@ test "service projection derives host dispatch and registry from one contract" {
910
908
  try std.testing.expect(std.mem.indexOf(u8, client, "ServiceRoute<Msg, readonly (Uint8Array | null)[]>") != null);
911
909
 
912
910
  const original_fingerprint = contractFingerprint(contract);
911
+ // An implementation-only fingerprint input change must leave the ABI
912
+ // digest alone: source/package/compiler facts rebuild only the service.
913
+ const implementation_only_fingerprint = contractFingerprint(.{
914
+ .format = contract.format,
915
+ .protocol_version = contract.protocol_version,
916
+ .compiler_version = "9.9.9",
917
+ .deterministic = contract.deterministic,
918
+ .packages = &.{.{ .name = "different-package", .version = "4.5.6", .content_hash = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" }},
919
+ .types = contract.types,
920
+ .operations = &.{
921
+ .{
922
+ .name = operations[0].name,
923
+ .client = "renamedInternalClient",
924
+ .module = "src/services/moved.ts",
925
+ .@"export" = "moved",
926
+ .request = operations[0].request,
927
+ .result = operations[0].result,
928
+ .deadline_ms = operations[0].deadline_ms,
929
+ .cancellable = operations[0].cancellable,
930
+ .stream = operations[0].stream,
931
+ .source_hash = "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
932
+ },
933
+ operations[1],
934
+ operations[2],
935
+ },
936
+ });
937
+ try std.testing.expectEqualSlices(u8, &original_fingerprint, &implementation_only_fingerprint);
913
938
  const reordered_operations = [_]service.Operation{ operations[1], operations[0] };
914
939
  const reordered_fingerprint = contractFingerprint(.{
915
940
  .format = contract.format,