@native-sdk/cli 0.9.0 → 0.9.1

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 (92) hide show
  1. package/build/app.zig +331 -24
  2. package/build.zig +179 -11
  3. package/native-sdk.d.ts +8 -0
  4. package/package.json +10 -10
  5. package/packages/core/compile-surface/core.ts +70 -1
  6. package/packages/core/package-lock.json +11 -11
  7. package/packages/core/package.json +2 -2
  8. package/packages/core/scripts/merge_static_archives.mjs +78 -0
  9. package/packages/core/scripts/run_external_core_compiler.mjs +52 -7
  10. package/packages/core/scripts/run_external_service_compiler.mjs +53 -13
  11. package/packages/core/scripts/stage_external_services.mjs +1 -1
  12. package/packages/core/sdk/core.d.ts +75 -0
  13. package/packages/core/sdk/core.ts +167 -12
  14. package/packages/core/sdk/events.d.ts +53 -0
  15. package/packages/core/sdk/events.ts +78 -7
  16. package/packages/core/src/checker.ts +470 -2
  17. package/packages/core/src/cli.ts +13 -0
  18. package/packages/core/src/contract.ts +3 -1
  19. package/packages/core/src/devhost.ts +30 -1
  20. package/packages/core/src/devhost_journal.mjs +10 -8
  21. package/packages/core/src/diagnostics.ts +99 -8
  22. package/packages/core/src/frontend.ts +6 -0
  23. package/packages/core/src/infer.ts +24 -0
  24. package/skill-data/automation/SKILL.md +1 -1
  25. package/skill-data/native-ui/SKILL.md +11 -5
  26. package/skill-data/ts-core/SKILL.md +53 -20
  27. package/skill-data/ts-services/SKILL.md +4 -4
  28. package/skill-data/ts-services/references/service-surface.md +2 -1
  29. package/skill-data/zig/SKILL.md +1 -1
  30. package/src/app_runner/root.zig +29 -0
  31. package/src/app_runner/ts_core_main.zig +5 -0
  32. package/src/app_runner/ts_core_mobile.zig +241 -0
  33. package/src/automation/protocol.zig +5 -1
  34. package/src/automation/snapshot.zig +8 -6
  35. package/src/embed/app_exports.zig +1 -0
  36. package/src/embed/root.zig +1 -0
  37. package/src/embed/tests.zig +28 -0
  38. package/src/embed/ui_host.zig +68 -2
  39. package/src/platform/linux/cef_host.cpp +7 -1
  40. package/src/platform/linux/gtk_host.c +200 -5
  41. package/src/platform/linux/gtk_host.h +2 -1
  42. package/src/platform/linux/root.zig +69 -4
  43. package/src/platform/macos/appkit_host.h +11 -7
  44. package/src/platform/macos/appkit_host.m +210 -88
  45. package/src/platform/macos/cef_host.mm +210 -88
  46. package/src/platform/macos/root.zig +77 -30
  47. package/src/platform/null_platform.zig +234 -51
  48. package/src/platform/null_platform_tests.zig +39 -0
  49. package/src/platform/root.zig +10 -0
  50. package/src/platform/types.zig +105 -16
  51. package/src/platform/windows/root.zig +20 -6
  52. package/src/platform/windows/webview2_host.cpp +41 -5
  53. package/src/root.zig +7 -0
  54. package/src/runtime/api.zig +8 -0
  55. package/src/runtime/automation_commands.zig +35 -2
  56. package/src/runtime/automation_snapshot.zig +32 -24
  57. package/src/runtime/bridge_permission_tests.zig +35 -1
  58. package/src/runtime/bridge_responses.zig +1 -1
  59. package/src/runtime/builtin_bridge.zig +11 -1
  60. package/src/runtime/core.zig +12 -6
  61. package/src/runtime/effects.zig +1323 -35
  62. package/src/runtime/effects_file_tests.zig +587 -0
  63. package/src/runtime/effects_notification_tests.zig +11 -0
  64. package/src/runtime/file_access.zig +400 -0
  65. package/src/runtime/file_effect_crash_helper.zig +28 -0
  66. package/src/runtime/file_effect_crash_tests.zig +37 -0
  67. package/src/runtime/flow.zig +21 -9
  68. package/src/runtime/native_command_event_tests.zig +44 -2
  69. package/src/runtime/pty.zig +5 -2
  70. package/src/runtime/root.zig +5 -0
  71. package/src/runtime/session_blobs.zig +4 -1
  72. package/src/runtime/session_journal.zig +56 -5
  73. package/src/runtime/session_record.zig +38 -0
  74. package/src/runtime/session_replay.zig +97 -7
  75. package/src/runtime/session_tests.zig +81 -0
  76. package/src/runtime/state.zig +10 -0
  77. package/src/runtime/system_services.zig +129 -36
  78. package/src/runtime/ts_core_host.zig +281 -11
  79. package/src/runtime/ts_core_host_tests.zig +95 -1
  80. package/src/runtime/ts_ui_app.zig +461 -8
  81. package/src/runtime/ui_app.zig +321 -109
  82. package/src/runtime/ui_app_tests.zig +132 -6
  83. package/src/runtime/ui_app_window_tests.zig +61 -7
  84. package/src/runtime/validation.zig +20 -0
  85. package/src/tooling/templates.zig +28 -0
  86. package/src/tooling/ts_core.zig +83 -0
  87. package/tools/corewire/emit.zig +3 -3
  88. package/tools/corewire/emit_facade.zig +65 -5
  89. package/tools/corewire/emit_profile.zig +2 -2
  90. package/tools/corewire/emit_service.zig +2 -2
  91. package/tools/corewire/service_contract.zig +4 -4
  92. package/tools/corewire/sidecar.zig +4 -4
@@ -61,6 +61,7 @@ const FileModel = struct {
61
61
  const FileMsg = union(enum) {
62
62
  save,
63
63
  load,
64
+ delete,
64
65
  stop,
65
66
  file_result: effects_mod.EffectFileResult,
66
67
  };
@@ -90,6 +91,11 @@ fn fileUpdate(model: *FileModel, msg: FileMsg, fx: *FileEffects) void {
90
91
  .path = test_path,
91
92
  .on_result = FileEffects.fileMsg(.file_result),
92
93
  }),
94
+ .delete => fx.deleteFile(.{
95
+ .key = file_key,
96
+ .path = test_path,
97
+ .on_result = FileEffects.fileMsg(.file_result),
98
+ }),
93
99
  .stop => fx.cancel(file_key),
94
100
  .file_result => |result| {
95
101
  model.record(result);
@@ -110,6 +116,7 @@ fn fileView(ui: *FileApp.Ui, model: *const FileModel) FileApp.Ui.Node {
110
116
  ui.text(.{}, ui.fmt("{d} results", .{model.result_count})),
111
117
  ui.button(.{ .on_press = .save }, "Save"),
112
118
  ui.button(.{ .on_press = .load }, "Load"),
119
+ ui.button(.{ .on_press = .delete }, "Delete"),
113
120
  ui.button(.{ .on_press = .stop }, "Stop"),
114
121
  });
115
122
  }
@@ -206,6 +213,42 @@ test "fake executor records file requests and feeds results back as msgs" {
206
213
  try std.testing.expectEqual(@as(usize, 0), h.app_state.model.bytes_len);
207
214
  }
208
215
 
216
+ test "fake executor records delete requests and feeds the standard result callback" {
217
+ const Capture = struct {
218
+ var record: ?effects_mod.EffectResultRecord = null;
219
+
220
+ fn note(_: *anyopaque, value: effects_mod.EffectResultRecord) void {
221
+ record = value;
222
+ }
223
+ };
224
+
225
+ var h = try Harness.create();
226
+ defer h.destroy();
227
+ const fx = &h.app_state.effects;
228
+ fx.executor = .fake;
229
+ Capture.record = null;
230
+ var journal_context: u8 = 0;
231
+ fx.bindJournal(.{ .context = &journal_context, .record_fn = Capture.note });
232
+
233
+ test_path = "sessions/obsolete.json";
234
+ try h.app_state.dispatch(&h.harness.runtime, 1, .delete);
235
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingFileCount());
236
+ const request = fx.pendingFileAt(0).?;
237
+ try std.testing.expectEqual(file_key, request.key);
238
+ try std.testing.expectEqual(effects_mod.EffectFileOp.delete, request.op);
239
+ try std.testing.expectEqualStrings("sessions/obsolete.json", request.path);
240
+ try std.testing.expectEqualStrings("", request.bytes);
241
+
242
+ try fx.feedFileResult(file_key, .ok, "ignored");
243
+ try h.harness.runtime.dispatchPlatformEvent(h.app, .wake);
244
+ try std.testing.expectEqual(effects_mod.EffectFileOp.delete, h.app_state.model.last_op.?);
245
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, h.app_state.model.last_outcome.?);
246
+ try std.testing.expectEqual(@as(usize, 0), h.app_state.model.bytes_len);
247
+ try std.testing.expect(Capture.record != null);
248
+ try std.testing.expectEqual(effects_mod.EffectFileOp.delete, Capture.record.?.file_op);
249
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, Capture.record.?.file_outcome);
250
+ }
251
+
209
252
  test "fake reads over the file bound arrive cut with outcome truncated" {
210
253
  var h = try Harness.create();
211
254
  defer h.destroy();
@@ -413,6 +456,130 @@ test "real executor reports missing files as not_found" {
413
456
  try std.testing.expectEqual(@as(usize, 0), h.app_state.model.bytes_len);
414
457
  }
415
458
 
459
+ test "real executor deletes a file and reports a later delete as not_found" {
460
+ const io = std.testing.io;
461
+ var tmp = std.testing.tmpDir(.{});
462
+ defer tmp.cleanup();
463
+ try tmp.dir.writeFile(io, .{ .sub_path = "obsolete.bin", .data = "remove me" });
464
+
465
+ var h = try Harness.create();
466
+ defer h.destroy();
467
+ var path_buffer: [256]u8 = undefined;
468
+ test_path = try std.fmt.bufPrint(&path_buffer, ".zig-cache/tmp/{s}/obsolete.bin", .{tmp.sub_path[0..]});
469
+
470
+ try h.app_state.dispatch(&h.harness.runtime, 1, .delete);
471
+ try waitForRealResult(&h, 1);
472
+ try std.testing.expectEqual(effects_mod.EffectFileOp.delete, h.app_state.model.last_op.?);
473
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, h.app_state.model.last_outcome.?);
474
+ try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "obsolete.bin", .{}));
475
+
476
+ try h.app_state.dispatch(&h.harness.runtime, 1, .delete);
477
+ try waitForRealResult(&h, 2);
478
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.not_found, h.app_state.model.last_outcome.?);
479
+ }
480
+
481
+ test "delete unlinks a final symlink without deleting its target" {
482
+ if (@import("builtin").os.tag == .windows) return error.SkipZigTest;
483
+ const io = std.testing.io;
484
+ var tmp = std.testing.tmpDir(.{});
485
+ defer tmp.cleanup();
486
+ try tmp.dir.createDirPath(io, "app-data");
487
+ try tmp.dir.writeFile(io, .{ .sub_path = "app-data/target.bin", .data = "keep me" });
488
+ try tmp.dir.symLink(io, "target.bin", "app-data/alias.bin", .{});
489
+
490
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
491
+ const Fx = effects_mod.Effects(TestMsg);
492
+ var fx = Fx.init(std.testing.allocator);
493
+ defer fx.deinit();
494
+
495
+ var root_buffer: [256]u8 = undefined;
496
+ var alias_buffer: [256]u8 = undefined;
497
+ const root = try std.fmt.bufPrint(&root_buffer, ".zig-cache/tmp/{s}/app-data", .{tmp.sub_path[0..]});
498
+ const alias = try std.fmt.bufPrint(&alias_buffer, "{s}/alias.bin", .{root});
499
+ fx.bindFileAccess(.{ .roots = &.{root}, .permitted = false, .enforce = true });
500
+ fx.deleteFile(.{ .key = 1, .path = alias, .on_result = Fx.fileMsg(.result) });
501
+ const result = while (true) {
502
+ if (fx.takeMsg()) |msg| break msg.result;
503
+ try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
504
+ };
505
+
506
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, result.outcome);
507
+ try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "app-data/alias.bin", .{ .follow_symlinks = false }));
508
+ const target = try tmp.dir.readFileAlloc(io, "app-data/target.bin", std.testing.allocator, .limited(32));
509
+ defer std.testing.allocator.free(target);
510
+ try std.testing.expectEqualStrings("keep me", target);
511
+
512
+ try tmp.dir.symLink(io, "missing.bin", "app-data/dangling.bin", .{});
513
+ var dangling_buffer: [256]u8 = undefined;
514
+ const dangling = try std.fmt.bufPrint(&dangling_buffer, "{s}/dangling.bin", .{root});
515
+ fx.deleteFile(.{ .key = 2, .path = dangling, .on_result = Fx.fileMsg(.result) });
516
+ const dangling_result = while (true) {
517
+ if (fx.takeMsg()) |msg| break msg.result;
518
+ try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
519
+ };
520
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, dangling_result.outcome);
521
+ try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "app-data/dangling.bin", .{ .follow_symlinks = false }));
522
+ }
523
+
524
+ test "filesystem permission deletes an external symlink entry without following it" {
525
+ if (@import("builtin").os.tag == .windows) return error.SkipZigTest;
526
+ const io = std.testing.io;
527
+ var tmp = std.testing.tmpDir(.{});
528
+ defer tmp.cleanup();
529
+ try tmp.dir.writeFile(io, .{ .sub_path = "target.bin", .data = "keep me too" });
530
+ try tmp.dir.symLink(io, "target.bin", "alias.bin", .{});
531
+
532
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
533
+ const Fx = effects_mod.Effects(TestMsg);
534
+ var fx = Fx.init(std.testing.allocator);
535
+ defer fx.deinit();
536
+ fx.bindFileAccess(.{ .roots = &.{}, .permitted = true, .enforce = true });
537
+
538
+ var alias_buffer: [256]u8 = undefined;
539
+ const alias = try std.fmt.bufPrint(&alias_buffer, ".zig-cache/tmp/{s}/alias.bin", .{tmp.sub_path[0..]});
540
+ fx.deleteFile(.{ .key = 1, .path = alias, .on_result = Fx.fileMsg(.result) });
541
+ const result = while (true) {
542
+ if (fx.takeMsg()) |msg| break msg.result;
543
+ try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
544
+ };
545
+
546
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, result.outcome);
547
+ try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, "alias.bin", .{ .follow_symlinks = false }));
548
+ const target = try tmp.dir.readFileAlloc(io, "target.bin", std.testing.allocator, .limited(32));
549
+ defer std.testing.allocator.free(target);
550
+ try std.testing.expectEqualStrings("keep me too", target);
551
+ }
552
+
553
+ test "an app-root symlink to an external target still requires filesystem permission" {
554
+ if (@import("builtin").os.tag == .windows) return error.SkipZigTest;
555
+ const io = std.testing.io;
556
+ var tmp = std.testing.tmpDir(.{});
557
+ defer tmp.cleanup();
558
+ try tmp.dir.createDirPath(io, "app-data");
559
+ try tmp.dir.createDirPath(io, "outside");
560
+ try tmp.dir.writeFile(io, .{ .sub_path = "outside/target.bin", .data = "protected" });
561
+ try tmp.dir.symLink(io, "../outside/target.bin", "app-data/alias.bin", .{});
562
+
563
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
564
+ const Fx = effects_mod.Effects(TestMsg);
565
+ var fx = Fx.init(std.testing.allocator);
566
+ defer fx.deinit();
567
+
568
+ var root_buffer: [256]u8 = undefined;
569
+ var alias_buffer: [256]u8 = undefined;
570
+ const root = try std.fmt.bufPrint(&root_buffer, ".zig-cache/tmp/{s}/app-data", .{tmp.sub_path[0..]});
571
+ const alias = try std.fmt.bufPrint(&alias_buffer, "{s}/alias.bin", .{root});
572
+ fx.bindFileAccess(.{ .roots = &.{root}, .permitted = false, .enforce = true });
573
+ fx.deleteFile(.{ .key = 1, .path = alias, .on_result = Fx.fileMsg(.result) });
574
+
575
+ const result = fx.takeMsg().?.result;
576
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.rejected, result.outcome);
577
+ _ = try tmp.dir.statFile(io, "app-data/alias.bin", .{ .follow_symlinks = false });
578
+ const target = try tmp.dir.readFileAlloc(io, "outside/target.bin", std.testing.allocator, .limited(32));
579
+ defer std.testing.allocator.free(target);
580
+ try std.testing.expectEqualStrings("protected", target);
581
+ }
582
+
416
583
  test "real executor cuts over-bound reads with outcome truncated" {
417
584
  const io = std.testing.io;
418
585
  var tmp = std.testing.tmpDir(.{});
@@ -437,6 +604,426 @@ test "real executor cuts over-bound reads with outcome truncated" {
437
604
  );
438
605
  }
439
606
 
607
+ test "append and stat are bounded one-shot file effects" {
608
+ const io = std.testing.io;
609
+ var tmp = std.testing.tmpDir(.{});
610
+ defer tmp.cleanup();
611
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
612
+ const Fx = effects_mod.Effects(TestMsg);
613
+ var fx = Fx.init(std.testing.allocator);
614
+ defer fx.deinit();
615
+ var path_buffer: [256]u8 = undefined;
616
+ const path = try std.fmt.bufPrint(&path_buffer, ".zig-cache/tmp/{s}/log/events.log", .{tmp.sub_path[0..]});
617
+ fx.appendFile(.{ .key = 1, .path = path, .bytes = "one", .on_result = Fx.fileMsg(.result) });
618
+ const first = while (true) {
619
+ if (fx.takeMsg()) |msg| break msg.result;
620
+ try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
621
+ };
622
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, first.outcome);
623
+ fx.appendFile(.{ .key = 2, .path = path, .bytes = "-two", .on_result = Fx.fileMsg(.result) });
624
+ const second = while (true) {
625
+ if (fx.takeMsg()) |msg| break msg.result;
626
+ try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
627
+ };
628
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, second.outcome);
629
+ fx.statFile(.{ .key = 3, .path = path, .on_result = Fx.fileMsg(.result) });
630
+ var stat_result: ?effects_mod.EffectFileResult = null;
631
+ while (stat_result == null) {
632
+ if (fx.takeMsg()) |msg| stat_result = msg.result else try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
633
+ }
634
+ try std.testing.expect(stat_result.?.exists);
635
+ try std.testing.expectEqual(@as(u64, 7), stat_result.?.total);
636
+ const bytes = try tmp.dir.readFileAlloc(io, "log/events.log", std.testing.allocator, .limited(32));
637
+ defer std.testing.allocator.free(bytes);
638
+ try std.testing.expectEqualStrings("one-two", bytes);
639
+ }
640
+
641
+ test "streaming file round-trip has no total-size cliff and finalizes atomically" {
642
+ const io = std.testing.io;
643
+ var tmp = std.testing.tmpDir(.{});
644
+ defer tmp.cleanup();
645
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
646
+ const Fx = effects_mod.Effects(TestMsg);
647
+ var fx = Fx.init(std.testing.allocator);
648
+ defer fx.deinit();
649
+ var path_buffer: [256]u8 = undefined;
650
+ const path = try std.fmt.bufPrint(&path_buffer, ".zig-cache/tmp/{s}/export.bin", .{tmp.sub_path[0..]});
651
+ const chunk = try std.testing.allocator.alloc(u8, effects_mod.effect_file_stream_chunk_bytes);
652
+ defer std.testing.allocator.free(chunk);
653
+ for (chunk, 0..) |*byte, index| byte.* = @truncate(index);
654
+
655
+ fx.writeFileStream(.{ .key = 44, .path = path, .on_result = Fx.fileMsg(.result) });
656
+ var write_result: ?effects_mod.EffectFileResult = null;
657
+ while (write_result == null) {
658
+ if (fx.takeMsg()) |msg| write_result = msg.result else try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
659
+ }
660
+ try std.testing.expectEqual(@as(u64, 0), write_result.?.total);
661
+ for (0..5) |_| {
662
+ fx.writeFileChunk(.{ .key = 44, .bytes = chunk, .on_result = Fx.fileMsg(.result) });
663
+ write_result = null;
664
+ while (write_result == null) {
665
+ if (fx.takeMsg()) |msg| write_result = msg.result else try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
666
+ }
667
+ try std.testing.expectEqual(@as(u64, 0), write_result.?.total);
668
+ }
669
+ fx.writeFileClose(.{ .key = 44, .on_result = Fx.fileMsg(.result) });
670
+ write_result = null;
671
+ while (write_result == null) {
672
+ if (fx.takeMsg()) |msg| write_result = msg.result else try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
673
+ }
674
+ try std.testing.expectEqual(@as(u64, 0), write_result.?.total);
675
+
676
+ fx.readFileStream(.{ .key = 45, .path = path, .on_result = Fx.fileMsg(.result) });
677
+ var chunks: usize = 0;
678
+ var total: u64 = 0;
679
+ while (true) {
680
+ const msg = fx.takeMsg() orelse {
681
+ try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
682
+ continue;
683
+ };
684
+ if (msg.result.event == .chunk) chunks += 1 else {
685
+ try std.testing.expectEqual(effects_mod.EffectFileEvent.done, msg.result.event);
686
+ total = msg.result.total;
687
+ break;
688
+ }
689
+ }
690
+ try std.testing.expectEqual(@as(usize, 5), chunks);
691
+ try std.testing.expectEqual(@as(u64, chunk.len * 5), total);
692
+ }
693
+
694
+ test "a rejected write-stream chunk can retry without losing its atomic sink" {
695
+ const io = std.testing.io;
696
+ var tmp = std.testing.tmpDir(.{});
697
+ defer tmp.cleanup();
698
+ try tmp.dir.writeFile(io, .{ .sub_path = "export.bin", .data = "previous generation" });
699
+
700
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
701
+ const Fx = effects_mod.Effects(TestMsg);
702
+ var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{});
703
+ var fx = Fx.init(failing.allocator());
704
+ defer fx.deinit();
705
+ var path_buffer: [256]u8 = undefined;
706
+ const path = try std.fmt.bufPrint(&path_buffer, ".zig-cache/tmp/{s}/export.bin", .{tmp.sub_path[0..]});
707
+
708
+ fx.writeFileStream(.{ .key = 55, .path = path, .on_result = Fx.fileMsg(.result) });
709
+ var result: ?effects_mod.EffectFileResult = null;
710
+ while (result == null) {
711
+ if (fx.takeMsg()) |msg| result = msg.result else try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
712
+ }
713
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, result.?.outcome);
714
+
715
+ // Reject the engine-owned chunk copy after the atomic sink is open.
716
+ failing.fail_index = failing.alloc_index;
717
+ fx.writeFileChunk(.{ .key = 55, .bytes = "replacement", .on_result = Fx.fileMsg(.result) });
718
+ result = null;
719
+ while (result == null) {
720
+ if (fx.takeMsg()) |msg| result = msg.result else try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
721
+ }
722
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.rejected, result.?.outcome);
723
+
724
+ const still_visible = try tmp.dir.readFileAlloc(io, "export.bin", std.testing.allocator, .limited(64));
725
+ defer std.testing.allocator.free(still_visible);
726
+ try std.testing.expectEqualStrings("previous generation", still_visible);
727
+
728
+ failing.fail_index = std.math.maxInt(usize);
729
+ fx.writeFileChunk(.{ .key = 55, .bytes = "replacement", .on_result = Fx.fileMsg(.result) });
730
+ result = null;
731
+ while (result == null) {
732
+ if (fx.takeMsg()) |msg| result = msg.result else try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
733
+ }
734
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, result.?.outcome);
735
+ fx.writeFileClose(.{ .key = 55, .on_result = Fx.fileMsg(.result) });
736
+ result = null;
737
+ while (result == null) {
738
+ if (fx.takeMsg()) |msg| result = msg.result else try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
739
+ }
740
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, result.?.outcome);
741
+
742
+ const visible = try tmp.dir.readFileAlloc(io, "export.bin", std.testing.allocator, .limited(64));
743
+ defer std.testing.allocator.free(visible);
744
+ try std.testing.expectEqualStrings("replacement", visible);
745
+ }
746
+
747
+ test "an unclosed write stream never exposes a partial destination" {
748
+ const io = std.testing.io;
749
+ var tmp = std.testing.tmpDir(.{});
750
+ defer tmp.cleanup();
751
+ try tmp.dir.writeFile(io, .{ .sub_path = "export.bin", .data = "previous generation" });
752
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
753
+ const Fx = effects_mod.Effects(TestMsg);
754
+ var path_buffer: [256]u8 = undefined;
755
+ const path = try std.fmt.bufPrint(&path_buffer, ".zig-cache/tmp/{s}/export.bin", .{tmp.sub_path[0..]});
756
+
757
+ var fx = Fx.init(std.testing.allocator);
758
+ fx.writeFileStream(.{ .key = 54, .path = path, .on_result = Fx.fileMsg(.result) });
759
+ while (fx.takeMsg() == null) try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
760
+ fx.writeFileChunk(.{ .key = 54, .bytes = "partial replacement", .on_result = Fx.fileMsg(.result) });
761
+ while (fx.takeMsg() == null) try std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake);
762
+ fx.deinit();
763
+
764
+ const visible = try tmp.dir.readFileAlloc(io, "export.bin", std.testing.allocator, .limited(64));
765
+ defer std.testing.allocator.free(visible);
766
+ try std.testing.expectEqualStrings("previous generation", visible);
767
+ }
768
+
769
+ test "stream sinks reject duplicate and out-of-order operations loudly" {
770
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
771
+ const Fx = effects_mod.Effects(TestMsg);
772
+ var fx = Fx.init(std.testing.allocator);
773
+ defer fx.deinit();
774
+ fx.executor = .fake;
775
+ fx.writeFileStream(.{ .key = 9, .path = "out.bin", .on_result = Fx.fileMsg(.result) });
776
+ fx.writeFileStream(.{ .key = 9, .path = "other.bin", .on_result = Fx.fileMsg(.result) });
777
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.rejected, fx.takeMsg().?.result.outcome);
778
+ try fx.acknowledgeFakeFileStreamOpen(9);
779
+ try fx.feedFileResultDetailed(.{ .key = 9, .op = .write_stream_open, .outcome = .ok });
780
+ _ = fx.takeMsg().?;
781
+ fx.writeFileChunk(.{ .key = 9, .bytes = "one", .on_result = Fx.fileMsg(.result) });
782
+ fx.writeFileChunk(.{ .key = 9, .bytes = "two", .on_result = Fx.fileMsg(.result) });
783
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.out_of_order, fx.takeMsg().?.result.outcome);
784
+ }
785
+
786
+ test "loop-side stream protocol results are journaled as replay-regenerated" {
787
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
788
+ const Fx = effects_mod.Effects(TestMsg);
789
+ const Capture = struct {
790
+ var records: [8]effects_mod.EffectResultRecord = undefined;
791
+ var count: usize = 0;
792
+
793
+ fn note(_: *anyopaque, record: effects_mod.EffectResultRecord) void {
794
+ records[count] = record;
795
+ count += 1;
796
+ }
797
+ };
798
+
799
+ Capture.count = 0;
800
+ var context: u8 = 0;
801
+ var fx = Fx.init(std.testing.allocator);
802
+ defer fx.deinit();
803
+ fx.executor = .fake;
804
+ fx.bindJournal(.{ .context = &context, .record_fn = Capture.note });
805
+
806
+ fx.writeFileChunk(.{ .key = 40, .bytes = "orphan", .on_result = Fx.fileMsg(.result) });
807
+ _ = fx.takeMsg().?;
808
+
809
+ fx.writeFileStream(.{ .key = 41, .path = "sink.bin", .on_result = Fx.fileMsg(.result) });
810
+ try fx.acknowledgeFakeFileStreamOpen(41);
811
+ try fx.feedFileResultDetailed(.{ .key = 41, .op = .write_stream_open, .outcome = .ok });
812
+ _ = fx.takeMsg().?;
813
+
814
+ fx.writeFileChunk(.{ .key = 41, .bytes = "one", .on_result = Fx.fileMsg(.result) });
815
+ fx.writeFileChunk(.{ .key = 41, .bytes = "two", .on_result = Fx.fileMsg(.result) });
816
+ _ = fx.takeMsg().?;
817
+ try fx.feedFileResultDetailed(.{ .key = 41, .op = .write_stream_chunk, .outcome = .ok, .total = 3 });
818
+ _ = fx.takeMsg().?;
819
+
820
+ const oversized = try std.testing.allocator.alloc(u8, effects_mod.max_effect_file_bytes + 1);
821
+ defer std.testing.allocator.free(oversized);
822
+ fx.writeFileChunk(.{ .key = 41, .bytes = oversized, .on_result = Fx.fileMsg(.result) });
823
+ _ = fx.takeMsg().?;
824
+
825
+ fx.writeFileClose(.{ .key = 42, .on_result = Fx.fileMsg(.result) });
826
+ _ = fx.takeMsg().?;
827
+
828
+ try std.testing.expectEqual(@as(usize, 6), Capture.count);
829
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.sink_missing, Capture.records[0].file_outcome);
830
+ try std.testing.expect(Capture.records[0].file_rejected_admission);
831
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, Capture.records[1].file_outcome);
832
+ try std.testing.expect(!Capture.records[1].file_rejected_admission);
833
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.out_of_order, Capture.records[2].file_outcome);
834
+ try std.testing.expect(Capture.records[2].file_rejected_admission);
835
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, Capture.records[3].file_outcome);
836
+ try std.testing.expect(!Capture.records[3].file_rejected_admission);
837
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.rejected, Capture.records[4].file_outcome);
838
+ try std.testing.expect(Capture.records[4].file_rejected_admission);
839
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.sink_missing, Capture.records[5].file_outcome);
840
+ try std.testing.expect(Capture.records[5].file_rejected_admission);
841
+ }
842
+
843
+ test "path-policy file rejections are journaled as executor truth" {
844
+ var tmp = std.testing.tmpDir(.{});
845
+ defer tmp.cleanup();
846
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
847
+ const Fx = effects_mod.Effects(TestMsg);
848
+ const Capture = struct {
849
+ var record: ?effects_mod.EffectResultRecord = null;
850
+
851
+ fn note(_: *anyopaque, value: effects_mod.EffectResultRecord) void {
852
+ record = value;
853
+ }
854
+ };
855
+
856
+ Capture.record = null;
857
+ var context: u8 = 0;
858
+ var fx = Fx.init(std.testing.allocator);
859
+ defer fx.deinit();
860
+ fx.executor = .fake;
861
+ fx.bindJournal(.{ .context = &context, .record_fn = Capture.note });
862
+ fx.bindFileAccess(.{ .roots = &.{}, .permitted = false, .enforce = true });
863
+
864
+ var path_buffer: [256]u8 = undefined;
865
+ const path = try std.fmt.bufPrint(&path_buffer, ".zig-cache/tmp/{s}/denied.bin", .{tmp.sub_path[0..]});
866
+ fx.readFile(.{ .key = 61, .path = path, .on_result = Fx.fileMsg(.result) });
867
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.rejected, fx.takeMsg().?.result.outcome);
868
+ try std.testing.expect(Capture.record != null);
869
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.rejected, Capture.record.?.file_outcome);
870
+ try std.testing.expect(!Capture.record.?.file_rejected_admission);
871
+ }
872
+
873
+ test "read streams replace and cancel silently while sinks cancel loudly" {
874
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
875
+ const Fx = effects_mod.Effects(TestMsg);
876
+ var fx = Fx.init(std.testing.allocator);
877
+ defer fx.deinit();
878
+ fx.executor = .fake;
879
+
880
+ fx.readFileStream(.{ .key = 20, .path = "first.bin", .on_result = Fx.fileMsg(.result) });
881
+ fx.readFileStream(.{ .key = 20, .path = "replacement.bin", .on_result = Fx.fileMsg(.result) });
882
+ try fx.feedFileResultDetailed(.{ .key = 20, .op = .read_stream, .event = .chunk, .outcome = .ok, .bytes = "replacement", .total = 11 });
883
+ try std.testing.expectEqualStrings("replacement", fx.takeMsg().?.result.bytes);
884
+ fx.cancel(20);
885
+ try std.testing.expectEqual(@as(?TestMsg, null), fx.takeMsg());
886
+ try std.testing.expectError(error.EffectNotFound, fx.feedFileResultDetailed(.{ .key = 20, .op = .read_stream, .event = .done, .outcome = .ok, .total = 11 }));
887
+
888
+ fx.writeFileStream(.{ .key = 21, .path = "sink.bin", .on_result = Fx.fileMsg(.result) });
889
+ fx.cancel(21);
890
+ const cancelled = fx.takeMsg().?.result;
891
+ try std.testing.expectEqual(effects_mod.EffectFileOp.write_stream_open, cancelled.op);
892
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.cancelled, cancelled.outcome);
893
+ }
894
+
895
+ test "replay cancellation keeps a fake sink parked for its recorded terminal" {
896
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
897
+ const Fx = effects_mod.Effects(TestMsg);
898
+ var fx = Fx.init(std.testing.allocator);
899
+ defer fx.deinit();
900
+ fx.armReplay();
901
+
902
+ fx.writeFileStream(.{ .key = 22, .path = "sink.bin", .on_result = Fx.fileMsg(.result) });
903
+ fx.cancel(22);
904
+ try std.testing.expectEqual(@as(?TestMsg, null), fx.takeMsg());
905
+ try fx.feedFileResultDetailed(.{ .key = 22, .op = .write_stream_open, .outcome = .cancelled });
906
+ const cancelled = fx.takeMsg().?.result;
907
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.cancelled, cancelled.outcome);
908
+ }
909
+
910
+ test "disk capacity errors are closed and enum-named across whole and streaming writes" {
911
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
912
+ const Fx = effects_mod.Effects(TestMsg);
913
+ var fx = Fx.init(std.testing.allocator);
914
+ defer fx.deinit();
915
+ fx.executor = .fake;
916
+
917
+ fx.failNextFileOperationForTest(error.NoSpaceLeft);
918
+ fx.writeFile(.{ .key = 31, .path = "whole.bin", .bytes = "x", .on_result = Fx.fileMsg(.result) });
919
+ var result = fx.takeMsg().?.result;
920
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.disk_full, result.outcome);
921
+ try std.testing.expectEqualStrings("disk_full", @tagName(result.outcome));
922
+
923
+ fx.failNextFileOperationForTest(error.DiskQuota);
924
+ fx.writeFileStream(.{ .key = 32, .path = "stream.bin", .on_result = Fx.fileMsg(.result) });
925
+ result = fx.takeMsg().?.result;
926
+ try std.testing.expectEqual(effects_mod.EffectFileOp.write_stream_open, result.op);
927
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.disk_full, result.outcome);
928
+
929
+ fx.writeFileStream(.{ .key = 33, .path = "stream-chunk.bin", .on_result = Fx.fileMsg(.result) });
930
+ try fx.acknowledgeFakeFileStreamOpen(33);
931
+ try fx.feedFileResultDetailed(.{ .key = 33, .op = .write_stream_open, .outcome = .ok });
932
+ _ = fx.takeMsg().?;
933
+ fx.failNextFileOperationForTest(error.NoSpaceLeft);
934
+ fx.writeFileChunk(.{ .key = 33, .bytes = "chunk", .on_result = Fx.fileMsg(.result) });
935
+ result = fx.takeMsg().?.result;
936
+ try std.testing.expectEqual(effects_mod.EffectFileOp.write_stream_chunk, result.op);
937
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.disk_full, result.outcome);
938
+ }
939
+
940
+ test "file access gating covers whole, append, stat, delete, and stream verbs without consuming slots" {
941
+ var tmp = std.testing.tmpDir(.{});
942
+ defer tmp.cleanup();
943
+ try tmp.dir.createDirPath(std.testing.io, "app-data");
944
+
945
+ const TestMsg = union(enum) { result: effects_mod.EffectFileResult };
946
+ const Fx = effects_mod.Effects(TestMsg);
947
+ var fx = Fx.init(std.testing.allocator);
948
+ defer fx.deinit();
949
+ fx.executor = .fake;
950
+
951
+ var root_buffer: [256]u8 = undefined;
952
+ var inside_buffer: [256]u8 = undefined;
953
+ var outside_buffer: [256]u8 = undefined;
954
+ const root = try std.fmt.bufPrint(&root_buffer, ".zig-cache/tmp/{s}/app-data", .{tmp.sub_path[0..]});
955
+ const inside = try std.fmt.bufPrint(&inside_buffer, "{s}/owned.bin", .{root});
956
+ const outside = try std.fmt.bufPrint(&outside_buffer, ".zig-cache/tmp/{s}/outside.bin", .{tmp.sub_path[0..]});
957
+ fx.bindFileAccess(.{ .roots = &.{root}, .permitted = false, .enforce = true });
958
+
959
+ fx.writeFile(.{ .key = 1, .path = inside, .bytes = "ok", .on_result = Fx.fileMsg(.result) });
960
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingFileCount());
961
+ fx.cancel(1);
962
+ _ = fx.takeMsg().?;
963
+
964
+ fx.readFile(.{ .key = 2, .path = outside, .on_result = Fx.fileMsg(.result) });
965
+ fx.writeFile(.{ .key = 3, .path = outside, .bytes = "x", .on_result = Fx.fileMsg(.result) });
966
+ fx.appendFile(.{ .key = 4, .path = outside, .bytes = "x", .on_result = Fx.fileMsg(.result) });
967
+ fx.statFile(.{ .key = 5, .path = outside, .on_result = Fx.fileMsg(.result) });
968
+ fx.deleteFile(.{ .key = 6, .path = outside, .on_result = Fx.fileMsg(.result) });
969
+ fx.readFileStream(.{ .key = 7, .path = outside, .on_result = Fx.fileMsg(.result) });
970
+ fx.writeFileStream(.{ .key = 8, .path = outside, .on_result = Fx.fileMsg(.result) });
971
+ const refused_ops = [_]effects_mod.EffectFileOp{ .read, .write, .append, .stat, .delete, .read_stream, .write_stream_open };
972
+ for (refused_ops) |expected_op| {
973
+ const refused = fx.takeMsg().?.result;
974
+ try std.testing.expectEqual(expected_op, refused.op);
975
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.rejected, refused.outcome);
976
+ try std.testing.expectEqualStrings("rejected", @tagName(refused.outcome));
977
+ }
978
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingFileCount());
979
+
980
+ var granted = Fx.init(std.testing.allocator);
981
+ defer granted.deinit();
982
+ granted.executor = .fake;
983
+ granted.bindFileAccess(.{ .roots = &.{}, .permitted = true, .enforce = true });
984
+ granted.statFile(.{ .key = 9, .path = outside, .on_result = Fx.fileMsg(.result) });
985
+ try std.testing.expectEqual(@as(usize, 1), granted.pendingFileCount());
986
+ granted.readFile(.{ .key = 10, .path = inside, .on_result = Fx.fileMsg(.result) });
987
+ granted.readFileStream(.{ .key = 11, .path = inside, .on_result = Fx.fileMsg(.result) });
988
+ try std.testing.expectEqual(@as(usize, 2), granted.pendingFileCount());
989
+
990
+ // Full matrix: both whole-file and stream families are admitted inside
991
+ // app roots regardless of the grant, and outside only with the grant.
992
+ var inside_ungranted = Fx.init(std.testing.allocator);
993
+ defer inside_ungranted.deinit();
994
+ inside_ungranted.executor = .fake;
995
+ inside_ungranted.bindFileAccess(.{ .roots = &.{root}, .permitted = false, .enforce = true });
996
+ inside_ungranted.readFile(.{ .key = 40, .path = inside, .on_result = Fx.fileMsg(.result) });
997
+ inside_ungranted.readFileStream(.{ .key = 41, .path = inside, .on_result = Fx.fileMsg(.result) });
998
+ try std.testing.expectEqual(@as(usize, 1), inside_ungranted.pendingFileCount());
999
+ try inside_ungranted.feedFileResultDetailed(.{ .key = 41, .op = .read_stream, .event = .done, .outcome = .ok });
1000
+ _ = inside_ungranted.takeMsg().?;
1001
+
1002
+ granted.readFileStream(.{ .key = 42, .path = outside, .on_result = Fx.fileMsg(.result) });
1003
+ try granted.feedFileResultDetailed(.{ .key = 42, .op = .read_stream, .event = .done, .outcome = .ok });
1004
+ try std.testing.expectEqual(effects_mod.EffectFileOutcome.ok, granted.takeMsg().?.result.outcome);
1005
+
1006
+ var warn_only = Fx.init(std.testing.allocator);
1007
+ defer warn_only.deinit();
1008
+ warn_only.executor = .fake;
1009
+ warn_only.bindFileAccess(.{ .roots = &.{root}, .permitted = false, .enforce = false });
1010
+ warn_only.statFile(.{ .key = 43, .path = outside, .on_result = Fx.fileMsg(.result) });
1011
+ try std.testing.expectEqual(@as(usize, 1), warn_only.pendingFileCount());
1012
+ }
1013
+
1014
+ test "runtime binding defaults support one warn-only release before enforcement" {
1015
+ const binding_warn: effects_mod.FileAccessBinding = .{ .roots = &.{}, .permitted = false, .enforce = false };
1016
+ const binding_enforce: effects_mod.FileAccessBinding = .{ .roots = &.{}, .permitted = false, .enforce = true };
1017
+ try std.testing.expectEqual(
1018
+ @import("file_access.zig").Decision.warn,
1019
+ @import("file_access.zig").decide(std.testing.allocator, std.testing.io, binding_warn, ".zig-cache/outside-file"),
1020
+ );
1021
+ try std.testing.expectEqual(
1022
+ @import("file_access.zig").Decision.reject,
1023
+ @import("file_access.zig").decide(std.testing.allocator, std.testing.io, binding_enforce, ".zig-cache/outside-file"),
1024
+ );
1025
+ }
1026
+
440
1027
  // --------------------------------------------------- bounded teardown
441
1028
 
442
1029
  /// Create a FIFO at `path` (POSIX-only; callers gate on the platform).
@@ -18,15 +18,21 @@ test "notification effect delivers validated fields to the platform service" {
18
18
  fx.bindServices(&host.services);
19
19
 
20
20
  fx.showNotification(.{
21
+ .id = "build-status",
21
22
  .title = "Build finished",
22
23
  .subtitle = "native-sdk",
23
24
  .body = "All checks passed.",
25
+ .action_label = "Open",
26
+ .action_command = "build.open",
24
27
  });
25
28
 
26
29
  try std.testing.expectEqual(@as(usize, 1), null_platform.notificationCount());
27
30
  try std.testing.expectEqualStrings("Build finished", null_platform.lastNotificationTitle());
28
31
  try std.testing.expectEqualStrings("native-sdk", null_platform.lastNotificationSubtitle());
29
32
  try std.testing.expectEqualStrings("All checks passed.", null_platform.lastNotificationBody());
33
+ try std.testing.expectEqualStrings("build-status", null_platform.lastNotificationId());
34
+ try std.testing.expectEqualStrings("Open", null_platform.lastNotificationActionLabel());
35
+ try std.testing.expectEqualStrings("build.open", null_platform.lastNotificationActionCommand());
30
36
  }
31
37
 
32
38
  test "notification effect rejects malformed and over-bound requests" {
@@ -37,9 +43,14 @@ test "notification effect rejects malformed and over-bound requests" {
37
43
  fx.bindServices(&host.services);
38
44
 
39
45
  const long_title = [_]u8{'x'} ** (platform.max_notification_title_bytes + 1);
46
+ const long_id = [_]u8{'x'} ** (platform.max_notification_id_bytes + 1);
40
47
  fx.showNotification(.{ .title = "" });
41
48
  fx.showNotification(.{ .title = "bad\x00title" });
42
49
  fx.showNotification(.{ .title = &long_title });
50
+ fx.showNotification(.{ .id = &long_id, .title = "Bad id" });
51
+ fx.showNotification(.{ .title = "Missing command", .action_label = "Open" });
52
+ fx.showNotification(.{ .title = "Missing label", .action_command = "build.open" });
53
+ fx.showNotification(.{ .title = "Invalid command", .action_label = "Open", .action_command = "../open" });
43
54
 
44
55
  try std.testing.expectEqual(@as(usize, 0), null_platform.notificationCount());
45
56
  }