@native-sdk/cli 0.9.3 → 0.9.4

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.
@@ -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,
@@ -59,6 +59,7 @@ pub fn main(init: std.process.Init) !void {
59
59
  var profile_path: ?[]const u8 = null;
60
60
  var effective_sidecar_path: ?[]const u8 = null;
61
61
  var check_only = false;
62
+ var optimization: ?[]const u8 = null;
62
63
  var f64_slots: std.ArrayListUnmanaged([]const u8) = .empty;
63
64
  var index: usize = 1;
64
65
  while (index < args.len) : (index += 1) {
@@ -83,6 +84,9 @@ pub fn main(init: std.process.Init) !void {
83
84
  try f64_slots.append(arena, args[index]);
84
85
  } else if (std.mem.eql(u8, arg, "--check")) {
85
86
  check_only = true;
87
+ } else if (std.mem.eql(u8, arg, "--optimization") and index + 1 < args.len) {
88
+ index += 1;
89
+ optimization = args[index];
86
90
  } else {
87
91
  try stderr.print("corewire: unknown argument \"{s}\"\n\n{s}", .{ arg, usage });
88
92
  try stderr.flush();
@@ -256,7 +260,12 @@ pub fn main(init: std.process.Init) !void {
256
260
  try stderr.flush();
257
261
  std.process.exit(2);
258
262
  }
259
- break :blk emit_profile_mod.emitProfile(arena, parsed, entry, &diags) catch |err| switch (err) {
263
+ if (optimization != null and !std.mem.eql(u8, optimization.?, "dev") and !std.mem.eql(u8, optimization.?, "release")) {
264
+ try stderr.print("corewire: --optimization must be dev or release\n", .{});
265
+ try stderr.flush();
266
+ std.process.exit(2);
267
+ }
268
+ break :blk emit_profile_mod.emitProfile(arena, parsed, entry, optimization, &diags) catch |err| switch (err) {
260
269
  error.Refused => {
261
270
  try diags.write(input, stderr);
262
271
  try stderr.flush();
@@ -373,6 +382,7 @@ fn serviceProjection(init: std.process.Init, args: []const []const u8, stderr: *
373
382
  var inproc_main_out: ?[]const u8 = null;
374
383
  var inproc_profile_out: ?[]const u8 = null;
375
384
  var saw_service_flag = false;
385
+ var optimization: ?[]const u8 = null;
376
386
  var index: usize = 1;
377
387
  while (index < args.len) : (index += 1) {
378
388
  const arg = args[index];
@@ -400,6 +410,10 @@ fn serviceProjection(init: std.process.Init, args: []const []const u8, stderr: *
400
410
  saw_service_flag = true;
401
411
  index += 1;
402
412
  inproc_profile_out = args[index];
413
+ } else if (saw_service_flag and std.mem.eql(u8, arg, "--optimization") and index + 1 < args.len) {
414
+ saw_service_flag = true;
415
+ index += 1;
416
+ optimization = args[index];
403
417
  } else if (saw_service_flag) {
404
418
  try stderr.print("corewire: unknown service projection argument \"{s}\"\n", .{arg});
405
419
  try stderr.flush();
@@ -407,6 +421,11 @@ fn serviceProjection(init: std.process.Init, args: []const []const u8, stderr: *
407
421
  }
408
422
  }
409
423
  if (!saw_service_flag) return false;
424
+ if (optimization != null and !std.mem.eql(u8, optimization.?, "dev") and !std.mem.eql(u8, optimization.?, "release")) {
425
+ try stderr.print("corewire: --optimization must be dev or release\n", .{});
426
+ try stderr.flush();
427
+ std.process.exit(2);
428
+ }
410
429
  const sidecar_path = input orelse {
411
430
  try stderr.print("usage: corewire --services-sidecar <services.contract.json> [--service-host-main <service_host_main.ts>] [--service-registry <services.zig>] [--service-client <services.gen.ts>] [--service-inproc-main <service_inproc_main.ts>] [--service-inproc-profile <service_profile.json>]\n", .{});
412
431
  try stderr.flush();
@@ -469,7 +488,7 @@ fn serviceProjection(init: std.process.Init, args: []const []const u8, stderr: *
469
488
  };
470
489
  }
471
490
  if (inproc_profile_out) |path| {
472
- const generated = try emit_service_mod.emitInprocProfile(arena);
491
+ const generated = try emit_service_mod.emitInprocProfile(arena, optimization);
473
492
  std.Io.Dir.cwd().writeFile(init.io, .{ .sub_path = path, .data = generated }) catch |err| {
474
493
  try stderr.print("corewire: cannot write {s}: {t}\n", .{ path, err });
475
494
  try stderr.flush();