@native-sdk/cli 0.8.3 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/build/app.zig +905 -61
  2. package/build.zig +674 -373
  3. package/build.zig.zon +1 -0
  4. package/native-sdk.d.ts +6 -0
  5. package/package.json +12 -11
  6. package/packages/core/compile-surface/core.ts +455 -3
  7. package/packages/core/package-lock.json +11 -11
  8. package/packages/core/package.json +2 -2
  9. package/packages/core/scripts/compiler_typecheck.mjs +116 -15
  10. package/packages/core/scripts/gen_service_surface.mjs +268 -0
  11. package/packages/core/scripts/npm_static_spike.mjs +64 -0
  12. package/packages/core/scripts/run_external_core_compiler.mjs +77 -6
  13. package/packages/core/scripts/run_external_service_compiler.mjs +240 -0
  14. package/packages/core/scripts/stage_external_core.mjs +9 -1
  15. package/packages/core/scripts/stage_external_services.mjs +172 -0
  16. package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
  17. package/packages/core/scripts/vendor_service_packages.mjs +189 -0
  18. package/packages/core/sdk/core.d.ts +214 -2
  19. package/packages/core/sdk/core.ts +715 -42
  20. package/packages/core/sdk/events.d.ts +50 -0
  21. package/packages/core/sdk/events.ts +75 -0
  22. package/packages/core/src/checker.ts +528 -15
  23. package/packages/core/src/cli.ts +143 -3
  24. package/packages/core/src/contract.ts +26 -5
  25. package/packages/core/src/devhost.ts +1849 -18
  26. package/packages/core/src/devhost_journal.mjs +181 -0
  27. package/packages/core/src/devhost_resolver.mjs +17 -1
  28. package/packages/core/src/devhost_service_worker.mjs +90 -0
  29. package/packages/core/src/diagnostics.ts +84 -18
  30. package/packages/core/src/frontend.ts +204 -34
  31. package/packages/core/src/modules.ts +129 -80
  32. package/packages/core/src/service_contract.ts +597 -0
  33. package/packages/core/src/sqlite_cli.ts +93 -0
  34. package/packages/core/src/sqlite_codegen.ts +806 -0
  35. package/packages/core/src/sqlite_runtime_policy.ts +364 -0
  36. package/packages/core/src/typed_ast.ts +36 -8
  37. package/skill-data/core/SKILL.md +4 -2
  38. package/skill-data/core/references/project-anatomy.md +3 -2
  39. package/skill-data/core/references/web-engines-packaging-debugging.md +3 -1
  40. package/skill-data/native-ui/SKILL.md +55 -8
  41. package/skill-data/ts-core/SKILL.md +92 -40
  42. package/skill-data/ts-services/SKILL.md +126 -0
  43. package/skill-data/ts-services/references/service-surface.md +603 -0
  44. package/skill-data/zig/SKILL.md +1 -1
  45. package/skills/native-sdk/SKILL.md +1 -1
  46. package/src/app_runner/no_migrations.zig +3 -0
  47. package/src/app_runner/no_services.zig +20 -0
  48. package/src/app_runner/root.zig +104 -4
  49. package/src/app_runner/ts_core_main.zig +268 -4
  50. package/src/automation/snapshot.zig +23 -5
  51. package/src/embed/app_exports.zig +11 -1
  52. package/src/embed/c_api.zig +48 -2
  53. package/src/embed/conversions.zig +4 -5
  54. package/src/embed/host.zig +115 -1
  55. package/src/embed/root.zig +7 -0
  56. package/src/embed/tests.zig +213 -0
  57. package/src/embed/types.zig +51 -0
  58. package/src/embed/ui_host.zig +121 -5
  59. package/src/platform/android/NativeSdkActivity.java +131 -0
  60. package/src/platform/android/android_host.c +182 -1
  61. package/src/platform/android/native_sdk_app.h +17 -0
  62. package/src/platform/ios/native_sdk_app.h +17 -0
  63. package/src/platform/ios/uikit_host.m +115 -0
  64. package/src/platform/linux/cef_host.cpp +1 -1
  65. package/src/platform/linux/gtk_host.c +89 -17
  66. package/src/platform/linux/gtk_host.h +3 -0
  67. package/src/platform/linux/root.zig +41 -18
  68. package/src/platform/macos/appkit_host.h +26 -3
  69. package/src/platform/macos/appkit_host.m +1051 -129
  70. package/src/platform/macos/capture_info_plist.c +25 -0
  71. package/src/platform/macos/cef_host.mm +270 -36
  72. package/src/platform/macos/root.zig +312 -34
  73. package/src/platform/null_platform.zig +281 -19
  74. package/src/platform/null_platform_tests.zig +74 -0
  75. package/src/platform/root.zig +16 -0
  76. package/src/platform/types.zig +279 -15
  77. package/src/platform/windows/root.zig +243 -21
  78. package/src/platform/windows/webview2_host.cpp +464 -46
  79. package/src/primitives/app_manifest/comptime_scene.zig +2 -0
  80. package/src/primitives/app_manifest/root.zig +4 -0
  81. package/src/primitives/app_manifest/tests.zig +3 -0
  82. package/src/primitives/app_manifest/types.zig +34 -0
  83. package/src/primitives/app_manifest/validation.zig +19 -1
  84. package/src/primitives/canvas/code_tests.zig +9 -9
  85. package/src/primitives/canvas/events.zig +12 -11
  86. package/src/primitives/canvas/frame.zig +44 -9
  87. package/src/primitives/canvas/icons/mic.svg +1 -0
  88. package/src/primitives/canvas/icons.zig +2 -0
  89. package/src/primitives/canvas/root.zig +8 -0
  90. package/src/primitives/canvas/text_span_tests.zig +39 -0
  91. package/src/primitives/canvas/ui.zig +23 -4
  92. package/src/primitives/canvas/ui_markup.zig +15 -0
  93. package/src/primitives/canvas/ui_markup_compiled.zig +9 -0
  94. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1 -1
  95. package/src/primitives/canvas/ui_markup_tests.zig +5 -0
  96. package/src/primitives/canvas/ui_markup_view.zig +9 -0
  97. package/src/primitives/canvas/ui_markup_view_tests.zig +9 -6
  98. package/src/primitives/canvas/ui_schema.zig +19 -10
  99. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  100. package/src/primitives/canvas/ui_tests.zig +18 -4
  101. package/src/primitives/canvas/widget_invalidation.zig +9 -2
  102. package/src/primitives/canvas/widget_render.zig +296 -114
  103. package/src/primitives/canvas/widget_runtime_tests.zig +133 -0
  104. package/src/primitives/canvas/widget_text_input.zig +10 -10
  105. package/src/primitives/canvas/widget_tree.zig +1 -1
  106. package/src/primitives/canvas/widgets.zig +29 -5
  107. package/src/root.zig +56 -0
  108. package/src/runtime/api.zig +11 -0
  109. package/src/runtime/automation_snapshot.zig +4 -0
  110. package/src/runtime/automation_widget_dispatch.zig +1 -0
  111. package/src/runtime/bridge_payload.zig +2 -0
  112. package/src/runtime/bridge_permission_tests.zig +23 -2
  113. package/src/runtime/bridge_responses.zig +3 -1
  114. package/src/runtime/canvas_frame.zig +31 -13
  115. package/src/runtime/canvas_frame_animation_tests.zig +4 -0
  116. package/src/runtime/canvas_frame_invalidation_tests.zig +35 -10
  117. package/src/runtime/canvas_widget_anchored_tests.zig +83 -0
  118. package/src/runtime/canvas_widget_events.zig +69 -24
  119. package/src/runtime/canvas_widget_runtime.zig +101 -6
  120. package/src/runtime/canvas_widget_scroll_driver_tests.zig +47 -4
  121. package/src/runtime/canvas_widget_scroll_drivers.zig +3 -3
  122. package/src/runtime/canvas_widget_semantics_tests.zig +2 -0
  123. package/src/runtime/canvas_widget_split_tree_tests.zig +228 -0
  124. package/src/runtime/canvas_widget_state.zig +6 -1
  125. package/src/runtime/canvas_widget_text_tests.zig +112 -3
  126. package/src/runtime/clock.zig +13 -1
  127. package/src/runtime/core.zig +133 -1
  128. package/src/runtime/credentials_store.zig +115 -0
  129. package/src/runtime/effects.zig +2568 -75
  130. package/src/runtime/effects_audio_capture_tests.zig +251 -0
  131. package/src/runtime/effects_credentials_tests.zig +332 -0
  132. package/src/runtime/effects_host_tests.zig +208 -1
  133. package/src/runtime/effects_relational_tests.zig +314 -0
  134. package/src/runtime/effects_store_tests.zig +211 -0
  135. package/src/runtime/flow.zig +14 -1
  136. package/src/runtime/gpu_surface_events.zig +6 -1
  137. package/src/runtime/native_command_event_tests.zig +42 -0
  138. package/src/runtime/persist_store.zig +666 -0
  139. package/src/runtime/record_store.zig +788 -0
  140. package/src/runtime/relational_store.zig +854 -0
  141. package/src/runtime/root.zig +52 -0
  142. package/src/runtime/runtime_event_tests.zig +70 -0
  143. package/src/runtime/service_host.zig +711 -0
  144. package/src/runtime/service_pool.zig +1314 -0
  145. package/src/runtime/session_blobs.zig +1 -1
  146. package/src/runtime/session_journal.zig +102 -0
  147. package/src/runtime/session_record.zig +177 -0
  148. package/src/runtime/session_replay.zig +109 -4
  149. package/src/runtime/shell_layout.zig +1 -0
  150. package/src/runtime/shell_layout_tests.zig +120 -0
  151. package/src/runtime/sqlite_engine.zig +474 -0
  152. package/src/runtime/state.zig +6 -0
  153. package/src/runtime/system_services.zig +49 -10
  154. package/src/runtime/tests.zig +5 -0
  155. package/src/runtime/ts_core_host.zig +1189 -118
  156. package/src/runtime/ts_core_host_tests.zig +638 -11
  157. package/src/runtime/ts_ui_app.zig +537 -1
  158. package/src/runtime/ui_app.zig +160 -19
  159. package/src/runtime/ui_app_tests.zig +203 -5
  160. package/src/runtime/validation.zig +22 -0
  161. package/src/runtime/view.zig +11 -0
  162. package/src/runtime/view_widget_scroll.zig +56 -5
  163. package/src/runtime/view_widget_text.zig +24 -5
  164. package/src/runtime/view_widget_tree.zig +1 -1
  165. package/src/runtime/window_storage.zig +1 -0
  166. package/src/runtime/window_view_runtime.zig +2 -3
  167. package/src/runtime/window_views.zig +19 -0
  168. package/src/tooling/android.zig +21 -16
  169. package/src/tooling/db.zig +160 -0
  170. package/src/tooling/ios.zig +7 -1
  171. package/src/tooling/manifest.zig +1248 -2
  172. package/src/tooling/package.zig +953 -6
  173. package/src/tooling/raw_manifest.zig +65 -0
  174. package/src/tooling/root.zig +1 -0
  175. package/src/tooling/templates.zig +216 -4
  176. package/src/tooling/ts_core.zig +283 -17
  177. package/src/tooling/verbs.zig +7 -0
  178. package/third_party/sqlite/README.md +13 -0
  179. package/third_party/sqlite/sqlite3.c +269649 -0
  180. package/third_party/sqlite/sqlite3.h +14349 -0
  181. package/tools/corewire/core_abi.zig +14 -2
  182. package/tools/corewire/emit.zig +98 -29
  183. package/tools/corewire/emit_facade.zig +414 -23
  184. package/tools/corewire/emit_profile.zig +6 -2
  185. package/tools/corewire/emit_service.zig +924 -0
  186. package/tools/corewire/main.zig +122 -0
  187. package/tools/corewire/service_contract.zig +250 -0
  188. package/tools/corewire/sidecar.zig +26 -15
@@ -39,7 +39,7 @@ pub const hash_len: usize = runtime_effects.effect_image_blob_hash_len;
39
39
 
40
40
  /// The largest payload one blob may hold — the largest journaled effect
41
41
  /// payload that goes out of line (an image load's encoded source).
42
- pub const max_blob_bytes: usize = runtime_effects.max_effect_image_bytes;
42
+ pub const max_blob_bytes: usize = @max(runtime_effects.max_effect_image_bytes, runtime_effects.max_effect_persist_snapshot_bytes);
43
43
 
44
44
  pub const BlobHash = [hash_len]u8;
45
45
 
@@ -184,6 +184,7 @@ fn formatLayoutDescription(comptime epoch: u32) []const u8 {
184
184
  "shortcut=" ++ layout_fingerprint.describe(platform.ShortcutEvent) ++ "\n" ++
185
185
  "native_command=" ++ layout_fingerprint.describe(platform.NativeCommandEvent) ++ "\n" ++
186
186
  "menu_command=" ++ layout_fingerprint.describe(platform.MenuCommandEvent) ++ "\n" ++
187
+ "tray_command=" ++ layout_fingerprint.describe(platform.TrayCommandEvent) ++ "\n" ++
187
188
  "timer=" ++ layout_fingerprint.describe(platform.TimerEvent) ++ "\n" ++
188
189
  "audio=" ++ layout_fingerprint.describe(platform.AudioEvent) ++ "\n" ++
189
190
  "video=" ++ layout_fingerprint.describe(platform.VideoEvent) ++ "\n" ++
@@ -464,6 +465,7 @@ const EventTag = enum(u8) {
464
465
  audio = 24,
465
466
  video = 25,
466
467
  view_focused = 26,
468
+ tray_command = 27,
467
469
  };
468
470
 
469
471
  // The bit assignments below are hand-written wire layout: they are
@@ -605,6 +607,11 @@ pub fn encodeEvent(event: platform.Event, buffer: []u8) JournalError![]const u8
605
607
  try cursor.writeStr(command.name);
606
608
  try cursor.writeInt(u64, command.window_id);
607
609
  },
610
+ .tray_command => |command| {
611
+ try cursor.writeEnum(EventTag.tray_command);
612
+ try cursor.writeStr(command.name);
613
+ try cursor.writeInt(u64, command.window_id);
614
+ },
608
615
  .timer => |timer| {
609
616
  try cursor.writeEnum(EventTag.timer);
610
617
  try cursor.writeInt(u64, timer.id);
@@ -830,6 +837,13 @@ pub fn decodeEvent(bytes: []const u8, storage: *EventDecodeStorage) JournalError
830
837
  .window_id = try cursor.readInt(u64),
831
838
  } };
832
839
  },
840
+ .tray_command => blk: {
841
+ const name = try cursor.readStr();
842
+ break :blk .{ .tray_command = .{
843
+ .name = name,
844
+ .window_id = try cursor.readInt(u64),
845
+ } };
846
+ },
833
847
  .timer => blk: {
834
848
  const id = try cursor.readInt(u64);
835
849
  break :blk .{ .timer = .{
@@ -1068,6 +1082,25 @@ pub fn encodeEffect(record: EffectResultRecord, buffer: []u8) JournalError![]con
1068
1082
  try cursor.writeInt(u32, record.pty_dropped_writes);
1069
1083
  try cursor.writeBytes(&record.pty_blob_hash);
1070
1084
  try cursor.writeInt(u64, record.pty_blob_len);
1085
+ // Model restore is the first kind-local suffix. Keeping it conditional
1086
+ // preserves every established effect record's byte layout (tests and
1087
+ // tooling deliberately patch those records at known offsets).
1088
+ if (record.kind == .persist) {
1089
+ try cursor.writeEnum(record.persist_outcome);
1090
+ try cursor.writeBytes(&record.persist_blob_hash);
1091
+ try cursor.writeInt(u64, record.persist_blob_len);
1092
+ }
1093
+ if (record.kind == .db) {
1094
+ try cursor.writeBytes(&record.db_blob_hash);
1095
+ try cursor.writeInt(u64, record.db_blob_len);
1096
+ }
1097
+ if (record.kind == .credentials) {
1098
+ try cursor.writeEnum(record.credentials_operation);
1099
+ try cursor.writeEnum(record.credentials_outcome);
1100
+ try cursor.writeInt(u64, record.credentials_secret_len);
1101
+ try cursor.writeBytes(&record.credentials_salt);
1102
+ try cursor.writeBytes(&record.credentials_digest);
1103
+ }
1071
1104
  return buffer[0..cursor.len];
1072
1105
  }
1073
1106
 
@@ -1126,6 +1159,22 @@ pub fn decodeEffect(bytes: []const u8) JournalError!EffectResultRecord {
1126
1159
  record.pty_dropped_writes = try cursor.readInt(u32);
1127
1160
  @memcpy(&record.pty_blob_hash, try cursor.readBytes(record.pty_blob_hash.len));
1128
1161
  record.pty_blob_len = try cursor.readInt(u64);
1162
+ if (record.kind == .persist) {
1163
+ record.persist_outcome = try cursor.readEnum(runtime_effects.EffectPersistOutcome);
1164
+ @memcpy(&record.persist_blob_hash, try cursor.readBytes(record.persist_blob_hash.len));
1165
+ record.persist_blob_len = try cursor.readInt(u64);
1166
+ }
1167
+ if (record.kind == .db) {
1168
+ @memcpy(&record.db_blob_hash, try cursor.readBytes(record.db_blob_hash.len));
1169
+ record.db_blob_len = try cursor.readInt(u64);
1170
+ }
1171
+ if (record.kind == .credentials) {
1172
+ record.credentials_operation = try cursor.readEnum(runtime_effects.EffectCredentialsOperation);
1173
+ record.credentials_outcome = try cursor.readEnum(runtime_effects.EffectCredentialsOutcome);
1174
+ record.credentials_secret_len = try cursor.readInt(u64);
1175
+ @memcpy(&record.credentials_salt, try cursor.readBytes(record.credentials_salt.len));
1176
+ @memcpy(&record.credentials_digest, try cursor.readBytes(record.credentials_digest.len));
1177
+ }
1129
1178
  if (!cursor.done()) return error.JournalCorrupt;
1130
1179
  return record;
1131
1180
  }
@@ -1581,6 +1630,11 @@ test "event codec round-trips every payload variant" {
1581
1630
  const decoded = try roundTripEvent(.{ .menu_command = .{ .name = "app.about", .window_id = 1 } });
1582
1631
  try testing.expectEqualStrings("app.about", decoded.menu_command.name);
1583
1632
  }
1633
+ {
1634
+ const decoded = try roundTripEvent(.{ .tray_command = .{ .name = "app.refresh", .window_id = 3 } });
1635
+ try testing.expectEqualStrings("app.refresh", decoded.tray_command.name);
1636
+ try testing.expectEqual(@as(platform.WindowId, 3), decoded.tray_command.window_id);
1637
+ }
1584
1638
  {
1585
1639
  const decoded = try roundTripEvent(.{ .tray_action = 9 });
1586
1640
  try testing.expectEqual(@as(platform.TrayItemId, 9), decoded.tray_action);
@@ -1724,6 +1778,54 @@ test "effect codec round-trips payloads and outcomes" {
1724
1778
  try testing.expect(video_decoded.video_buffering);
1725
1779
  try testing.expectEqual(@as(u64, 1280), video_decoded.video_width);
1726
1780
  try testing.expectEqual(@as(u64, 720), video_decoded.video_height);
1781
+
1782
+ // Persistence is a kind-local suffix: successful restore bytes live in
1783
+ // the blob store, while the journal carries its closed outcome/address.
1784
+ const persist_hash: [runtime_effects.effect_image_blob_hash_len]u8 = @splat(0xa5);
1785
+ const persist_encoded = try encodeEffect(.{
1786
+ .kind = .persist,
1787
+ .key = 0,
1788
+ .persist_outcome = .ok,
1789
+ .persist_blob_hash = persist_hash,
1790
+ .persist_blob_len = 4096,
1791
+ }, &buffer);
1792
+ const persist_decoded = try decodeEffect(persist_encoded);
1793
+ try testing.expectEqual(runtime_effects.EffectResultKind.persist, persist_decoded.kind);
1794
+ try testing.expectEqual(runtime_effects.EffectPersistOutcome.ok, persist_decoded.persist_outcome);
1795
+ try testing.expectEqualSlices(u8, &persist_hash, &persist_decoded.persist_blob_hash);
1796
+ try testing.expectEqual(@as(u64, 4096), persist_decoded.persist_blob_len);
1797
+
1798
+ const db_hash: [runtime_effects.effect_image_blob_hash_len]u8 = @splat(0x5a);
1799
+ const db_encoded = try encodeEffect(.{
1800
+ .kind = .db,
1801
+ .key = 73,
1802
+ .code = runtime_effects.dbJournalCode(.page, .ok),
1803
+ .db_blob_hash = db_hash,
1804
+ .db_blob_len = 96 * 1024,
1805
+ }, &buffer);
1806
+ const db_decoded = try decodeEffect(db_encoded);
1807
+ try testing.expectEqual(runtime_effects.EffectResultKind.db, db_decoded.kind);
1808
+ try testing.expectEqualSlices(u8, &db_hash, &db_decoded.db_blob_hash);
1809
+ try testing.expectEqual(@as(u64, 96 * 1024), db_decoded.db_blob_len);
1810
+
1811
+ const credential_digest: [32]u8 = @splat(0x39);
1812
+ const credential_salt: [16]u8 = @splat(0x17);
1813
+ const credential_encoded = try encodeEffect(.{
1814
+ .kind = .credentials,
1815
+ .key = 81,
1816
+ .credentials_operation = .get,
1817
+ .credentials_outcome = .ok,
1818
+ .credentials_secret_len = 27,
1819
+ .credentials_salt = credential_salt,
1820
+ .credentials_digest = credential_digest,
1821
+ }, &buffer);
1822
+ const credential_decoded = try decodeEffect(credential_encoded);
1823
+ try testing.expectEqual(runtime_effects.EffectResultKind.credentials, credential_decoded.kind);
1824
+ try testing.expectEqual(runtime_effects.EffectCredentialsOperation.get, credential_decoded.credentials_operation);
1825
+ try testing.expectEqual(runtime_effects.EffectCredentialsOutcome.ok, credential_decoded.credentials_outcome);
1826
+ try testing.expectEqual(@as(u64, 27), credential_decoded.credentials_secret_len);
1827
+ try testing.expectEqualSlices(u8, &credential_salt, &credential_decoded.credentials_salt);
1828
+ try testing.expectEqualSlices(u8, &credential_digest, &credential_decoded.credentials_digest);
1727
1829
  }
1728
1830
 
1729
1831
  test "header, checkpoint, screenshot, and end codecs round-trip" {
@@ -27,6 +27,11 @@ const runtime_effects = @import("effects.zig");
27
27
  const journal = @import("session_journal.zig");
28
28
  const session_blobs = @import("session_blobs.zig");
29
29
 
30
+ /// Keep ordinary query pages self-contained while spilling pages large
31
+ /// enough to dominate the journal. The relational page's own hard ceiling
32
+ /// remains `max_effect_db_page_bytes`.
33
+ pub const max_inline_db_page_bytes: usize = 64 * 1024;
34
+
30
35
  pub const Header = journal.Header;
31
36
 
32
37
  /// Where journal bytes go. The app runner backs this with a file opened
@@ -53,6 +58,11 @@ pub const SessionRecorder = struct {
53
58
  effect_count: u64 = 0,
54
59
  checkpoint_count: u64 = 0,
55
60
  screenshot_count: u64 = 0,
61
+ /// Per-session salt for credential replay-placeholder digests. The digest
62
+ /// is deliberately independent of the secret, so a shareable journal is
63
+ /// not an offline guessing oracle. Successful secret bytes are never
64
+ /// written to either the journal or its blob store.
65
+ credentials_salt: [16]u8 = @splat(0),
56
66
  /// Frame index of the last recorded checkpoint, so exactly one
57
67
  /// checkpoint follows each published frame.
58
68
  last_checkpoint_frame: u64 = 0,
@@ -79,6 +89,20 @@ pub const SessionRecorder = struct {
79
89
  pub fn begin(self: *SessionRecorder, header: Header) void {
80
90
  if (self.began or self.failed) return;
81
91
  self.began = true;
92
+ // SessionRecorder deliberately has no ambient-I/O handle. Derive a
93
+ // per-session salt from the already-journaled header identity and
94
+ // timestamp. The resulting placeholder digest is metadata only and
95
+ // never hashes credential bytes.
96
+ var salt_hasher = std.crypto.hash.sha2.Sha256.init(.{});
97
+ salt_hasher.update(header.platform_name);
98
+ salt_hasher.update(header.app_name);
99
+ var salt_scalars: [16]u8 = undefined;
100
+ std.mem.writeInt(u64, salt_scalars[0..8], header.protocol_fingerprint, .little);
101
+ std.mem.writeInt(i64, salt_scalars[8..16], header.recorded_at_wall_ms, .little);
102
+ salt_hasher.update(&salt_scalars);
103
+ var salt_digest: [32]u8 = undefined;
104
+ salt_hasher.final(&salt_digest);
105
+ @memcpy(&self.credentials_salt, salt_digest[0..self.credentials_salt.len]);
82
106
  var preamble_buffer: [journal.preamble_len]u8 = undefined;
83
107
  self.write(journal.writePreamble(&preamble_buffer));
84
108
  const payload = journal.encodeHeader(header, &self.small_buffer) catch {
@@ -201,6 +225,22 @@ pub const SessionRecorder = struct {
201
225
  pub fn recordEffect(self: *SessionRecorder, record: runtime_effects.EffectResultRecord) void {
202
226
  if (!self.began or self.failed or self.finished) return;
203
227
  var journaled = record;
228
+ if (record.kind == .credentials) {
229
+ // Error bytes duplicate the closed outcome and successful get
230
+ // bytes are secrets. Neither belongs in a shareable artifact.
231
+ journaled.payload = "";
232
+ if (record.credentials_operation == .get and record.credentials_outcome == .ok) {
233
+ var hasher = std.crypto.hash.sha2.Sha256.init(.{});
234
+ hasher.update(&self.credentials_salt);
235
+ var ordinal: [8]u8 = undefined;
236
+ std.mem.writeInt(u64, ordinal[0..], self.effect_count, .little);
237
+ hasher.update(&ordinal);
238
+ hasher.update(@tagName(record.credentials_operation));
239
+ hasher.final(&journaled.credentials_digest);
240
+ journaled.credentials_salt = self.credentials_salt;
241
+ journaled.credentials_secret_len = record.payload.len;
242
+ }
243
+ }
204
244
  if (record.kind == .image and record.payload.len > 0) {
205
245
  const blob_sink = self.blob_sink orelse {
206
246
  return self.fail("an image effect result needs the session blob store, and none is bound - wire SessionRecorder.blob_sink (the app runner creates blobs/ beside the journal)");
@@ -229,6 +269,30 @@ pub const SessionRecorder = struct {
229
269
  journaled.pty_blob_len = record.payload.len;
230
270
  journaled.payload = "";
231
271
  }
272
+ if (record.kind == .persist and record.payload.len > 0) {
273
+ const blob_sink = self.blob_sink orelse {
274
+ return self.fail("a model restore needs the session blob store, and none is bound - wire SessionRecorder.blob_sink (the app runner creates blobs/ beside the journal)");
275
+ };
276
+ const hash = session_blobs.hashBytes(record.payload);
277
+ blob_sink.write_fn(blob_sink.context, hash, record.payload) catch |err| {
278
+ return self.fail(@errorName(err));
279
+ };
280
+ journaled.persist_blob_hash = hash;
281
+ journaled.persist_blob_len = record.payload.len;
282
+ journaled.payload = "";
283
+ }
284
+ if (record.kind == .db and record.payload.len > max_inline_db_page_bytes) {
285
+ const blob_sink = self.blob_sink orelse {
286
+ return self.fail("a large relational page needs the session blob store, and none is bound - wire SessionRecorder.blob_sink (the app runner creates blobs/ beside the journal)");
287
+ };
288
+ const hash = session_blobs.hashBytes(record.payload);
289
+ blob_sink.write_fn(blob_sink.context, hash, record.payload) catch |err| {
290
+ return self.fail(@errorName(err));
291
+ };
292
+ journaled.db_blob_hash = hash;
293
+ journaled.db_blob_len = record.payload.len;
294
+ journaled.payload = "";
295
+ }
232
296
  const payload = journal.encodeEffect(journaled, &self.effect_buffer) catch {
233
297
  return self.fail("an effect result exceeded max_session_record_bytes");
234
298
  };
@@ -369,6 +433,119 @@ test "recorder orders effect results before their consuming event" {
369
433
  try testing.expectEqual(@as(u64, 1), end.end.effect_count);
370
434
  }
371
435
 
436
+ test "recorder moves model restore snapshots into the session blob store" {
437
+ var buffer_sink = BufferSink{};
438
+ var blobs = session_blobs.MemoryBlobStore.init(testing.allocator);
439
+ defer blobs.deinit();
440
+ const recorder = try testing.allocator.create(SessionRecorder);
441
+ defer testing.allocator.destroy(recorder);
442
+ recorder.* = SessionRecorder.init(buffer_sink.sink());
443
+ recorder.blob_sink = blobs.sink();
444
+
445
+ recorder.begin(.{ .platform_name = "test", .app_name = "persisted" });
446
+ recorder.recordEffect(.{ .kind = .persist, .key = 0, .payload = "canonical model", .persist_outcome = .ok });
447
+ recorder.finish();
448
+ try testing.expect(!recorder.failed);
449
+
450
+ var reader = try journal.Reader.init(buffer_sink.bytes());
451
+ _ = (try reader.next()).?;
452
+ const effect = (try reader.next()).?.effect;
453
+ try testing.expectEqual(runtime_effects.EffectResultKind.persist, effect.kind);
454
+ try testing.expectEqual(@as(usize, 0), effect.payload.len);
455
+ try testing.expectEqual(@as(u64, "canonical model".len), effect.persist_blob_len);
456
+ var scratch: [64]u8 = undefined;
457
+ const restored = try blobs.read(effect.persist_blob_hash, &scratch);
458
+ try testing.expectEqualStrings("canonical model", restored);
459
+ }
460
+
461
+ test "recorder redacts credential bytes from journal and blob storage" {
462
+ const secret = "storage-tier-four-live-token";
463
+ const set_secret = "storage-tier-four-set-token";
464
+ var buffer_sink = BufferSink{};
465
+ var blobs = session_blobs.MemoryBlobStore.init(testing.allocator);
466
+ defer blobs.deinit();
467
+ const recorder = try testing.allocator.create(SessionRecorder);
468
+ defer testing.allocator.destroy(recorder);
469
+ recorder.* = SessionRecorder.init(buffer_sink.sink());
470
+ recorder.blob_sink = blobs.sink();
471
+
472
+ recorder.begin(.{
473
+ .platform_name = "test",
474
+ .app_name = "credentials",
475
+ .recorded_at_wall_ms = 1_723_000_000_000,
476
+ });
477
+ recorder.recordEffect(.{
478
+ .kind = .credentials,
479
+ .key = 44,
480
+ .payload = secret,
481
+ .credentials_operation = .get,
482
+ .credentials_outcome = .ok,
483
+ });
484
+ // Set requests are not journaled at all, but keep the result encoder
485
+ // defensive: if a caller ever supplies request bytes here, credential
486
+ // records elide them for every operation, not only successful gets.
487
+ recorder.recordEffect(.{
488
+ .kind = .credentials,
489
+ .key = 45,
490
+ .payload = set_secret,
491
+ .credentials_operation = .set,
492
+ .credentials_outcome = .ok,
493
+ });
494
+ recorder.finish();
495
+ try testing.expect(!recorder.failed);
496
+ try testing.expect(std.mem.indexOf(u8, buffer_sink.bytes(), secret) == null);
497
+ try testing.expect(std.mem.indexOf(u8, buffer_sink.bytes(), set_secret) == null);
498
+ try testing.expectEqual(@as(usize, 0), blobs.count);
499
+
500
+ var reader = try journal.Reader.init(buffer_sink.bytes());
501
+ _ = (try reader.next()).?;
502
+ const effect = (try reader.next()).?.effect;
503
+ try testing.expectEqual(runtime_effects.EffectResultKind.credentials, effect.kind);
504
+ try testing.expectEqual(runtime_effects.EffectCredentialsOperation.get, effect.credentials_operation);
505
+ try testing.expectEqual(runtime_effects.EffectCredentialsOutcome.ok, effect.credentials_outcome);
506
+ try testing.expectEqual(@as(u64, secret.len), effect.credentials_secret_len);
507
+ try testing.expectEqual(@as(usize, 0), effect.payload.len);
508
+ try testing.expect(!std.mem.allEqual(u8, &effect.credentials_salt, 0));
509
+ try testing.expect(!std.mem.allEqual(u8, &effect.credentials_digest, 0));
510
+ const set_effect = (try reader.next()).?.effect;
511
+ try testing.expectEqual(runtime_effects.EffectCredentialsOperation.set, set_effect.credentials_operation);
512
+ try testing.expectEqual(@as(usize, 0), set_effect.payload.len);
513
+ try testing.expectEqual(@as(u64, 0), set_effect.credentials_secret_len);
514
+ }
515
+
516
+ test "recorder spills large relational pages into the session blob store" {
517
+ var buffer_sink = BufferSink{};
518
+ var blobs = session_blobs.MemoryBlobStore.init(testing.allocator);
519
+ defer blobs.deinit();
520
+ const recorder = try testing.allocator.create(SessionRecorder);
521
+ defer testing.allocator.destroy(recorder);
522
+ recorder.* = SessionRecorder.init(buffer_sink.sink());
523
+ recorder.blob_sink = blobs.sink();
524
+
525
+ const page = try testing.allocator.alloc(u8, max_inline_db_page_bytes + 1);
526
+ defer testing.allocator.free(page);
527
+ @memset(page, 0x5a);
528
+ recorder.begin(.{ .platform_name = "test", .app_name = "relational" });
529
+ recorder.recordEffect(.{
530
+ .kind = .db,
531
+ .key = 73,
532
+ .payload = page,
533
+ .code = runtime_effects.dbJournalCode(.page, .ok),
534
+ });
535
+ recorder.finish();
536
+ try testing.expect(!recorder.failed);
537
+
538
+ var reader = try journal.Reader.init(buffer_sink.bytes());
539
+ _ = (try reader.next()).?;
540
+ const effect = (try reader.next()).?.effect;
541
+ try testing.expectEqual(@as(usize, 0), effect.payload.len);
542
+ try testing.expectEqual(@as(u64, page.len), effect.db_blob_len);
543
+ const scratch = try testing.allocator.alloc(u8, page.len);
544
+ defer testing.allocator.free(scratch);
545
+ const restored = try blobs.read(effect.db_blob_hash, scratch);
546
+ try testing.expectEqualSlices(u8, page, restored);
547
+ }
548
+
372
549
  test "recorder commits nested events innermost-first" {
373
550
  var buffer_sink = BufferSink{};
374
551
  const recorder = try testing.allocator.create(SessionRecorder);
@@ -318,6 +318,27 @@ pub fn replaySession(
318
318
  );
319
319
  return error.ReplayDamagedRecord;
320
320
  }
321
+ if (effect.kind == .persist and persistRecordDamaged(effect)) {
322
+ std.debug.print(
323
+ "replay refused after event {d}: model-restore record claims .{s} with {d} inline bytes and a {d}-byte blob - only .ok may carry canonical snapshot bytes, always out of line and bounded at {d}; re-record the session\n",
324
+ .{ report.events_replayed, @tagName(effect.persist_outcome), effect.payload.len, effect.persist_blob_len, runtime_effects.max_effect_persist_snapshot_bytes },
325
+ );
326
+ return error.ReplayDamagedRecord;
327
+ }
328
+ if (effect.kind == .db and dbRecordDamaged(effect)) {
329
+ std.debug.print(
330
+ "replay refused after event {d}: relational record for key {d} has a kind, outcome, payload, blob, or provenance shape the recorder never writes - the journal is damaged or hand-edited; re-record the session\n",
331
+ .{ report.events_replayed, effect.key },
332
+ );
333
+ return error.ReplayDamagedRecord;
334
+ }
335
+ if (effect.kind == .credentials and credentialsRecordDamaged(effect)) {
336
+ std.debug.print(
337
+ "replay refused after event {d}: credential record for key {d} has a payload or redaction shape the recorder never writes - the journal is damaged or hand-edited; re-record the session\n",
338
+ .{ report.events_replayed, effect.key },
339
+ );
340
+ return error.ReplayDamagedRecord;
341
+ }
321
342
  if (effectRegeneratesUnderReplay(effect)) {
322
343
  report.effects_skipped += 1;
323
344
  continue;
@@ -332,7 +353,7 @@ pub fn replaySession(
332
353
  var blob_scratch: ?[]u8 = null;
333
354
  defer if (blob_scratch) |scratch| std.heap.page_allocator.free(scratch);
334
355
  if (effect.kind == .pty and effect.pty_blob_len > 0) {
335
- const bytes = resolveBlob(effect.pty_blob_hash, effect.pty_blob_len, options.blobs, &blob_scratch) catch |err| {
356
+ const bytes = resolveBlob(effect.pty_blob_hash, effect.pty_blob_len, runtime_effects.max_effect_pty_chunk_bytes, options.blobs, &blob_scratch) catch |err| {
336
357
  std.debug.print(
337
358
  "replay refused after event {d}: pty record for key {d} references blob {s} ({d} bytes) that could not be resolved ({s}) - replay needs the journal's blobs/ directory beside it\n",
338
359
  .{ report.events_replayed, effect.key, session_blobs.hexName(effect.pty_blob_hash), effect.pty_blob_len, @errorName(err) },
@@ -342,7 +363,7 @@ pub fn replaySession(
342
363
  effect.payload = bytes;
343
364
  }
344
365
  if (effect.kind == .image and effect.image_blob_len > 0) {
345
- const bytes = resolveBlob(effect.image_blob_hash, effect.image_blob_len, options.blobs, &blob_scratch) catch |err| {
366
+ const bytes = resolveBlob(effect.image_blob_hash, effect.image_blob_len, runtime_effects.max_effect_image_bytes, options.blobs, &blob_scratch) catch |err| {
346
367
  std.debug.print(
347
368
  "replay refused after event {d}: image record for id {d} references blob {s} ({d} bytes) that could not be resolved ({s}) - replay needs the journal's blobs/ directory beside it\n",
348
369
  .{ report.events_replayed, effect.key, session_blobs.hexName(effect.image_blob_hash), effect.image_blob_len, @errorName(err) },
@@ -351,6 +372,26 @@ pub fn replaySession(
351
372
  };
352
373
  effect.payload = bytes;
353
374
  }
375
+ if (effect.kind == .persist and effect.persist_blob_len > 0) {
376
+ const bytes = resolveBlob(effect.persist_blob_hash, effect.persist_blob_len, runtime_effects.max_effect_persist_snapshot_bytes, options.blobs, &blob_scratch) catch |err| {
377
+ std.debug.print(
378
+ "replay refused after event {d}: model restore references blob {s} ({d} bytes) that could not be resolved ({s}) - replay needs the journal's blobs/ directory beside it\n",
379
+ .{ report.events_replayed, session_blobs.hexName(effect.persist_blob_hash), effect.persist_blob_len, @errorName(err) },
380
+ );
381
+ return error.ReplayMissingBlob;
382
+ };
383
+ effect.payload = bytes;
384
+ }
385
+ if (effect.kind == .db and effect.db_blob_len > 0) {
386
+ const bytes = resolveBlob(effect.db_blob_hash, effect.db_blob_len, runtime_effects.max_effect_db_page_bytes, options.blobs, &blob_scratch) catch |err| {
387
+ std.debug.print(
388
+ "replay refused after event {d}: relational page for key {d} references blob {s} ({d} bytes) that could not be resolved ({s}) - replay needs the journal's blobs/ directory beside it\n",
389
+ .{ report.events_replayed, effect.key, session_blobs.hexName(effect.db_blob_hash), effect.db_blob_len, @errorName(err) },
390
+ );
391
+ return error.ReplayMissingBlob;
392
+ };
393
+ effect.payload = bytes;
394
+ }
354
395
  // Feed with back-pressure: results journal in delivery
355
396
  // order, so one recorded drain pass can carry more
356
397
  // results than the completion queue holds (a live
@@ -449,11 +490,12 @@ pub fn replaySession(
449
490
  fn resolveBlob(
450
491
  blob_hash: [runtime_effects.effect_image_blob_hash_len]u8,
451
492
  blob_len_field: u64,
493
+ max_bytes: usize,
452
494
  blobs: ?session_blobs.SessionBlobSource,
453
495
  scratch_out: *?[]u8,
454
496
  ) anyerror![]const u8 {
455
497
  const blob_source = blobs orelse return error.BlobMissing;
456
- if (blob_len_field > session_blobs.max_blob_bytes) return error.BlobOverBudget;
498
+ if (blob_len_field > max_bytes) return error.BlobOverBudget;
457
499
  const blob_len: usize = @intCast(blob_len_field);
458
500
  // One spare byte proves the stored blob is not LONGER than the
459
501
  // record claims (the source reads at most the buffer).
@@ -525,6 +567,42 @@ fn ptyRecordDamaged(record: journal.EffectResultRecord) bool {
525
567
  return false;
526
568
  }
527
569
 
570
+ fn persistRecordDamaged(record: journal.EffectResultRecord) bool {
571
+ if (record.payload.len > 0) return true;
572
+ if (record.persist_blob_len > runtime_effects.max_effect_persist_snapshot_bytes) return true;
573
+ return record.persist_outcome != .ok and record.persist_blob_len > 0;
574
+ }
575
+
576
+ fn dbRecordDamaged(record: journal.EffectResultRecord) bool {
577
+ const kind = runtime_effects.dbKindFromJournalCode(record.code) orelse return true;
578
+ const outcome = runtime_effects.dbOutcomeFromJournalCode(record.code) orelse return true;
579
+ if (record.code != runtime_effects.dbJournalCode(kind, outcome)) return true;
580
+ // `.rejected` provenance identifies an admission refusal that replay
581
+ // regenerates. An executor may also return the closed `.rejected`
582
+ // outcome (for example when a result crosses its total bound); that
583
+ // terminal is external truth and correctly keeps `.exited` provenance.
584
+ if (record.exit_reason != .exited and record.exit_reason != .rejected) return true;
585
+ if (record.exit_reason == .rejected and outcome != .rejected) return true;
586
+ if (record.db_blob_len > runtime_effects.max_effect_db_page_bytes) return true;
587
+ return switch (kind) {
588
+ .page => outcome != .ok or
589
+ (record.payload.len == 0) == (record.db_blob_len == 0) or
590
+ record.payload.len > runtime_effects.max_effect_db_page_bytes,
591
+ .done, .exec => record.payload.len > 0 or record.db_blob_len > 0,
592
+ };
593
+ }
594
+
595
+ fn credentialsRecordDamaged(record: journal.EffectResultRecord) bool {
596
+ if (record.payload.len != 0 or record.exit_reason != .exited) return true;
597
+ const redacted_get = record.credentials_operation == .get and record.credentials_outcome == .ok;
598
+ if (redacted_get) return record.credentials_secret_len > runtime_effects.max_effect_credentials_secret_bytes or
599
+ std.mem.allEqual(u8, &record.credentials_salt, 0) or
600
+ std.mem.allEqual(u8, &record.credentials_digest, 0);
601
+ if (record.credentials_secret_len != 0) return true;
602
+ return !std.mem.allEqual(u8, &record.credentials_salt, 0) or
603
+ !std.mem.allEqual(u8, &record.credentials_digest, 0);
604
+ }
605
+
528
606
  /// Recorder truth for pty provenance: output records always carry
529
607
  /// `.exited` (the live drain never touches the exit reason on an
530
608
  /// output); every reason is legal on an `.exit` record (`.rejected` =
@@ -681,6 +759,8 @@ fn effectRegeneratesUnderReplay(record: journal.EffectResultRecord) bool {
681
759
  // Host-request rejections mark themselves with the exit reason
682
760
  // (the `.host` record encoding); host answers must be fed.
683
761
  .host => record.exit_reason == .rejected,
762
+ .db => record.exit_reason == .rejected,
763
+ .credentials => false,
684
764
  // Image `.rejected` terminals journal from BOTH sides of the
685
765
  // executor seam, so the outcome alone is not provenance: only
686
766
  // loop-side validation refusals — which the replayed
@@ -707,7 +787,7 @@ fn effectRegeneratesUnderReplay(record: journal.EffectResultRecord) bool {
707
787
  // Launch-env deliveries are exactly what must NOT regenerate:
708
788
  // the recorded values feed the replayed envMsgs dispatch so the
709
789
  // replay launch's environment is never consulted.
710
- .line, .clock, .env => false,
790
+ .line, .clock, .env, .persist => false,
711
791
  };
712
792
  }
713
793
 
@@ -801,6 +881,31 @@ test "audio records outside the exact-integer scalar window are damaged" {
801
881
  try std.testing.expect(audioScalarsDamaged(record));
802
882
  }
803
883
 
884
+ test "relational rejection provenance distinguishes admission from executor truth" {
885
+ var record: journal.EffectResultRecord = .{
886
+ .kind = .db,
887
+ .key = 1,
888
+ .code = runtime_effects.dbJournalCode(.done, .rejected),
889
+ .exit_reason = .exited,
890
+ };
891
+
892
+ // A bounded executor result is a real terminal which replay must feed.
893
+ try std.testing.expect(!dbRecordDamaged(record));
894
+
895
+ // A deterministic admission refusal regenerates instead and uses the
896
+ // same outcome with explicit rejected provenance.
897
+ record.exit_reason = .rejected;
898
+ try std.testing.expect(!dbRecordDamaged(record));
899
+
900
+ // Rejected provenance cannot decorate a successful result, and DB
901
+ // records never carry process-only exit reasons.
902
+ record.code = runtime_effects.dbJournalCode(.done, .ok);
903
+ try std.testing.expect(dbRecordDamaged(record));
904
+ record.code = runtime_effects.dbJournalCode(.done, .rejected);
905
+ record.exit_reason = .cancelled;
906
+ try std.testing.expect(dbRecordDamaged(record));
907
+ }
908
+
804
909
  /// Debug aid: `NATIVE_SDK_SESSION_REPLAY_DUMP=<dir>` writes each
805
910
  /// replay-rendered screenshot PNG so a mismatching pixel mark can be
806
911
  /// diffed against the recording's artifact.
@@ -233,6 +233,7 @@ pub fn shellClosePolicy(policy: app_manifest.WindowClosePolicy) platform.WindowC
233
233
  /// user never sees a blank window while the first frame renders. Webview
234
234
  /// windows keep immediate visibility — their engine owns first paint.
235
235
  pub fn shellWindowShowMode(shell_window: app_manifest.ShellWindow) platform.WindowShowMode {
236
+ if (shell_window.initially_hidden) return .hidden;
236
237
  for (shell_window.views) |view| {
237
238
  if (view.kind == .gpu_surface) return .on_first_present;
238
239
  }