@native-sdk/cli 0.5.3 → 0.6.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 (172) hide show
  1. package/build/app.zig +91 -2
  2. package/build.zig +756 -5
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +19 -1
  5. package/package.json +9 -9
  6. package/packages/core/package-lock.json +2 -2
  7. package/packages/core/package.json +1 -1
  8. package/packages/core/rt/rt.zig +416 -9
  9. package/packages/core/sdk/core.ts +706 -5
  10. package/packages/core/sdk/events.ts +17 -8
  11. package/packages/core/src/checker.ts +305 -2
  12. package/packages/core/src/diagnostics.ts +12 -0
  13. package/packages/core/src/emitter.ts +3187 -264
  14. package/packages/core/src/typed_ast.ts +51 -4
  15. package/packages/core/src/types.ts +34 -1
  16. package/skill-data/native-ui/SKILL.md +42 -4
  17. package/skill-data/ts-core/SKILL.md +13 -3
  18. package/src/app_runner/root.zig +112 -10
  19. package/src/app_runner/ts_core_main.zig +4 -0
  20. package/src/automation/layout_fingerprint.zig +160 -0
  21. package/src/automation/protocol.zig +82 -33
  22. package/src/automation/root.zig +1 -0
  23. package/src/automation/server.zig +1 -1
  24. package/src/automation/snapshot.zig +68 -9
  25. package/src/automation/watcher.zig +1 -1
  26. package/src/embed/host.zig +5 -0
  27. package/src/embed/tests.zig +119 -1
  28. package/src/embed/ui_host.zig +13 -0
  29. package/src/platform/linux/gtk_host.c +639 -16
  30. package/src/platform/linux/gtk_host.h +25 -0
  31. package/src/platform/linux/root.zig +351 -7
  32. package/src/platform/macos/appkit_host.h +152 -8
  33. package/src/platform/macos/appkit_host.m +1816 -100
  34. package/src/platform/macos/cef_host.mm +372 -11
  35. package/src/platform/macos/root.zig +379 -11
  36. package/src/platform/null_platform.zig +696 -9
  37. package/src/platform/null_platform_tests.zig +35 -0
  38. package/src/platform/root.zig +42 -0
  39. package/src/platform/types.zig +524 -18
  40. package/src/platform/windows/root.zig +432 -9
  41. package/src/platform/windows/webview2_host.cpp +482 -45
  42. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  43. package/src/primitives/app_manifest/root.zig +1 -0
  44. package/src/primitives/app_manifest/types.zig +32 -0
  45. package/src/primitives/app_manifest/validation.zig +18 -0
  46. package/src/primitives/canvas/a11y_audit.zig +33 -11
  47. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  48. package/src/primitives/canvas/drawing.zig +25 -0
  49. package/src/primitives/canvas/equality.zig +8 -0
  50. package/src/primitives/canvas/events.zig +103 -16
  51. package/src/primitives/canvas/frame.zig +125 -1
  52. package/src/primitives/canvas/layout_audit.zig +22 -5
  53. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  54. package/src/primitives/canvas/render_tests.zig +13 -10
  55. package/src/primitives/canvas/root.zig +48 -0
  56. package/src/primitives/canvas/terminal_box.zig +471 -0
  57. package/src/primitives/canvas/terminal_grid.zig +978 -0
  58. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  59. package/src/primitives/canvas/test_support.zig +1 -0
  60. package/src/primitives/canvas/tests.zig +2 -0
  61. package/src/primitives/canvas/text_tests.zig +39 -4
  62. package/src/primitives/canvas/tokens.zig +115 -12
  63. package/src/primitives/canvas/ui.zig +535 -12
  64. package/src/primitives/canvas/ui_markup.zig +253 -36
  65. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  66. package/src/primitives/canvas/ui_markup_compiled.zig +195 -9
  67. package/src/primitives/canvas/ui_markup_compiled_tests.zig +188 -8
  68. package/src/primitives/canvas/ui_markup_contract.zig +89 -6
  69. package/src/primitives/canvas/ui_markup_contract_tests.zig +65 -1
  70. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  71. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  72. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  73. package/src/primitives/canvas/ui_markup_tests.zig +178 -9
  74. package/src/primitives/canvas/ui_markup_view.zig +220 -11
  75. package/src/primitives/canvas/ui_markup_view_tests.zig +553 -28
  76. package/src/primitives/canvas/ui_schema.zig +110 -17
  77. package/src/primitives/canvas/ui_schema_tests.zig +18 -10
  78. package/src/primitives/canvas/ui_tests.zig +141 -9
  79. package/src/primitives/canvas/widget_access.zig +28 -2
  80. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  81. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  82. package/src/primitives/canvas/widget_layout.zig +59 -11
  83. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  84. package/src/primitives/canvas/widget_render.zig +272 -18
  85. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  86. package/src/primitives/canvas/widget_routing.zig +59 -7
  87. package/src/primitives/canvas/widget_runtime.zig +23 -0
  88. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  89. package/src/primitives/canvas/widget_semantics.zig +119 -11
  90. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  91. package/src/primitives/canvas/widget_tree.zig +17 -0
  92. package/src/primitives/canvas/widgets.zig +132 -0
  93. package/src/root.zig +23 -0
  94. package/src/runtime/api.zig +76 -5
  95. package/src/runtime/automation_commands.zig +20 -1
  96. package/src/runtime/automation_snapshot.zig +25 -1
  97. package/src/runtime/automation_widget_dispatch.zig +73 -8
  98. package/src/runtime/bridge_payload.zig +8 -0
  99. package/src/runtime/bridge_responses.zig +5 -0
  100. package/src/runtime/builtin_bridge.zig +9 -4
  101. package/src/runtime/canvas_font_tests.zig +439 -2
  102. package/src/runtime/canvas_fonts.zig +61 -6
  103. package/src/runtime/canvas_frame.zig +62 -7
  104. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  105. package/src/runtime/canvas_frame_helpers.zig +190 -1
  106. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  107. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  108. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  109. package/src/runtime/canvas_image_tests.zig +195 -0
  110. package/src/runtime/canvas_images.zig +45 -12
  111. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  112. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  113. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  114. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  115. package/src/runtime/canvas_widget_display.zig +1 -1
  116. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  117. package/src/runtime/canvas_widget_events.zig +240 -24
  118. package/src/runtime/canvas_widget_runtime.zig +213 -29
  119. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  120. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  121. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  122. package/src/runtime/canvas_widget_state.zig +6 -0
  123. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  124. package/src/runtime/core.zig +131 -13
  125. package/src/runtime/effects.zig +8136 -180
  126. package/src/runtime/effects_channel_tests.zig +3519 -0
  127. package/src/runtime/effects_fetch_tests.zig +89 -1
  128. package/src/runtime/effects_file_tests.zig +41 -1
  129. package/src/runtime/effects_image_tests.zig +1304 -0
  130. package/src/runtime/effects_pty_tests.zig +1362 -0
  131. package/src/runtime/effects_tests.zig +167 -9
  132. package/src/runtime/effects_video_tests.zig +3609 -0
  133. package/src/runtime/flow.zig +42 -4
  134. package/src/runtime/gpu_surface_events.zig +473 -13
  135. package/src/runtime/media_surface.zig +116 -11
  136. package/src/runtime/media_surface_tests.zig +61 -0
  137. package/src/runtime/pty.zig +1252 -0
  138. package/src/runtime/pty_windows.zig +1780 -0
  139. package/src/runtime/root.zig +36 -0
  140. package/src/runtime/runtime_event_tests.zig +20 -1
  141. package/src/runtime/session_blobs.zig +689 -0
  142. package/src/runtime/session_journal.zig +432 -62
  143. package/src/runtime/session_record.zig +43 -2
  144. package/src/runtime/session_replay.zig +556 -20
  145. package/src/runtime/session_tests.zig +2087 -1
  146. package/src/runtime/shell_layout.zig +7 -0
  147. package/src/runtime/terminal_session.zig +1569 -0
  148. package/src/runtime/terminal_session_tests.zig +450 -0
  149. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  150. package/src/runtime/terminal_vt_stub.zig +13 -0
  151. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  152. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  153. package/src/runtime/tests.zig +8 -0
  154. package/src/runtime/ts_core_host.zig +1320 -45
  155. package/src/runtime/ts_core_host_tests.zig +1100 -1
  156. package/src/runtime/ts_ui_app.zig +23 -5
  157. package/src/runtime/ui_app.zig +2444 -69
  158. package/src/runtime/ui_app_tests.zig +3171 -744
  159. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  160. package/src/runtime/ui_app_window_tests.zig +736 -0
  161. package/src/runtime/view.zig +190 -2
  162. package/src/runtime/view_widget_scroll.zig +218 -68
  163. package/src/runtime/view_widget_text.zig +11 -2
  164. package/src/runtime/view_widget_tree.zig +111 -3
  165. package/src/runtime/window_command_bridge_tests.zig +117 -0
  166. package/src/runtime/window_storage.zig +77 -2
  167. package/src/runtime/window_view_runtime.zig +34 -2
  168. package/src/runtime/window_views.zig +62 -1
  169. package/src/terminal_session_tests_root.zig +15 -0
  170. package/src/tooling/manifest.zig +75 -0
  171. package/src/tooling/raw_manifest.zig +2 -0
  172. package/src/tooling/templates.zig +244 -8
@@ -28,15 +28,18 @@
28
28
 
29
29
  const std = @import("std");
30
30
  const canvas = @import("canvas");
31
+ const canvas_limits = @import("canvas_limits.zig");
31
32
  const automation_protocol = @import("../automation/protocol.zig");
32
33
  const core = @import("core.zig");
33
34
  const journal = @import("session_journal.zig");
35
+ const runtime_effects = @import("effects.zig");
36
+ const session_blobs = @import("session_blobs.zig");
34
37
 
35
38
  pub const ReplayError = error{
36
39
  /// The journal was recorded on a different platform; v1 replay is
37
40
  /// same-platform only (font metrics and scale behavior differ).
38
41
  ReplayPlatformMismatch,
39
- /// The journal's automation protocol version differs from this
42
+ /// The journal's automation protocol fingerprint differs from this
40
43
  /// build's — the recording binary and this one are skewed.
41
44
  ReplayProtocolMismatch,
42
45
  /// A journaled effect result found no parked request with its key:
@@ -46,6 +49,22 @@ pub const ReplayError = error{
46
49
  /// The app registered no replay hook (`App.replay_fn`), but the
47
50
  /// journal carries effect results that need one.
48
51
  ReplayUnsupportedApp,
52
+ /// The journal references a session blob (an image record's source
53
+ /// bytes) but no blob source was provided, the blob is missing, or
54
+ /// its bytes fail their content hash — the journal directory was
55
+ /// moved without its `blobs/`, or the store was damaged.
56
+ ReplayMissingBlob,
57
+ /// A record's fields contradict each other in a way the recorder
58
+ /// can never produce (an image record claiming `.loaded` with a
59
+ /// zero-length blob: the recorder journals `.loaded` only after the
60
+ /// bytes decoded and registered, and empty bytes cannot decode; or
61
+ /// decoded dimensions the canvas registry would never have accepted
62
+ /// — zero or over-budget on `.loaded`, nonzero on any other
63
+ /// outcome) — the journal is damaged or hand-edited.
64
+ /// `JournalCorrupt` is the structural sibling (payloads that fail
65
+ /// to decode at all); this class is for records that decode fine
66
+ /// but lie.
67
+ ReplayDamagedRecord,
49
68
  };
50
69
 
51
70
  /// Bounded mismatch detail (first N are kept; the count keeps counting).
@@ -68,10 +87,15 @@ pub const ReplayOptions = struct {
68
87
  /// Refuse a journal recorded on another platform (the v1 bar).
69
88
  /// Tests recording under the null platform disable this.
70
89
  require_same_platform: bool = true,
90
+ /// Where journal records' out-of-line payloads resolve from (the
91
+ /// `blobs/` directory beside the journal — see session_blobs.zig).
92
+ /// Only consulted when a record references a blob; a journal
93
+ /// without image records replays fine with none.
94
+ blobs: ?session_blobs.SessionBlobSource = null,
71
95
  };
72
96
 
73
97
  pub const ReplayReport = struct {
74
- protocol_version: u32 = 0,
98
+ protocol_fingerprint: u64 = 0,
75
99
  events_replayed: u64 = 0,
76
100
  effects_fed: u64 = 0,
77
101
  effects_skipped: u64 = 0,
@@ -124,11 +148,11 @@ pub fn replaySession(
124
148
  while (try reader.next()) |record| {
125
149
  switch (record) {
126
150
  .header => |header| {
127
- report.protocol_version = header.protocol_version;
128
- if (header.protocol_version != automation_protocol.version) {
151
+ report.protocol_fingerprint = header.protocol_fingerprint;
152
+ if (header.protocol_fingerprint != automation_protocol.fingerprint) {
129
153
  std.debug.print(
130
- "replay refused: the journal was recorded at automation protocol v{d} but this build speaks v{d} - re-record with this build\n",
131
- .{ header.protocol_version, automation_protocol.version },
154
+ "replay refused: the journal was recorded by a build whose automation protocol differs from this build's (journal 0x{x:0>16}, this build 0x{x:0>16}) - re-record with this build\n",
155
+ .{ header.protocol_fingerprint, automation_protocol.fingerprint },
132
156
  );
133
157
  return error.ReplayProtocolMismatch;
134
158
  }
@@ -152,28 +176,219 @@ pub fn replaySession(
152
176
  try runtime.dispatchPlatformEvent(app, event);
153
177
  report.events_replayed += 1;
154
178
  },
155
- .effect => |effect| {
179
+ .effect => |effect_record| {
180
+ var effect = effect_record;
181
+ // A `.loaded` image record ALWAYS names source bytes:
182
+ // the recorder journals `.loaded` only after those
183
+ // exact bytes decoded and registered (a failed decode
184
+ // rewrites the outcome before it journals, and empty
185
+ // bytes cannot decode), so a zero-length blob here is
186
+ // journal damage, not a session shape. Refuse before
187
+ // any skip/feed decision — resolving blobs only for
188
+ // records that claim bytes would otherwise let the
189
+ // damaged record sail past the blob-integrity gate and
190
+ // deliver a pixel-less "loaded" the recording never
191
+ // produced.
192
+ if (effect.kind == .image and effect.image_outcome == .loaded and effect.image_blob_len == 0) {
193
+ std.debug.print(
194
+ "replay refused after event {d}: image record for id {d} claims .loaded with a zero-length blob - a recorded .loaded always carries its source bytes, so the journal is damaged or hand-edited; re-record the session\n",
195
+ .{ report.events_replayed, effect.key },
196
+ );
197
+ return error.ReplayDamagedRecord;
198
+ }
199
+ // Decoded dimensions obey the recorder the same way: a
200
+ // live `.loaded` journals only after the canvas registry
201
+ // accepted the pixels (nonzero width and height, and
202
+ // width * height * 4 bytes within
203
+ // `max_registered_canvas_image_pixel_bytes` — the same
204
+ // bound `registerCanvasImage` enforces), and every other
205
+ // outcome journals 0x0. Refuse out-of-bound dims HERE:
206
+ // the fed values flow verbatim into the app's Msg — and,
207
+ // on the TS core host, through `@intCast` into
208
+ // i64-classed arm fields, a safety panic on absurd
209
+ // values — before any decode could disprove them.
210
+ // `status` needs no twin gate: it is u16 at the journal
211
+ // codec, in the completion entry, and in
212
+ // `EffectImageResult`, so every downstream cast
213
+ // (i64/f64 arm fields) holds by type.
214
+ // Channel records obey the recorder the same way: a
215
+ // post can never exceed `max_effect_channel_bytes`
216
+ // (`ChannelHandle.post` refuses the bound before
217
+ // staging), and only `.data` events carry bytes at all
218
+ // — `.closed` and `.rejected` are payload-free
219
+ // terminals. A record that decodes fine but claims
220
+ // otherwise is damaged or hand-edited, and the fed
221
+ // bytes would flow verbatim into the app's Msg (and
222
+ // into a fixed-size feed buffer) before anything could
223
+ // disprove them — refuse HERE, before the feed.
224
+ if (effect.kind == .channel and channelRecordDamaged(effect)) {
225
+ std.debug.print(
226
+ "replay refused after event {d}: channel record for key {d} claims .{s} with {d} payload bytes - a recorded post is bounded at {d} bytes and only .data events carry bytes, so the journal is damaged or hand-edited; re-record the session\n",
227
+ .{ report.events_replayed, effect.key, @tagName(effect.channel_kind), effect.payload.len, runtime_effects.max_effect_channel_bytes },
228
+ );
229
+ return error.ReplayDamagedRecord;
230
+ }
231
+ // Provenance consistency, gated BEFORE the regeneration
232
+ // skip below: a `.data` or `.closed` channel record
233
+ // stamped with `.rejected` provenance would be skipped
234
+ // there and its event silently omitted from the Msg
235
+ // stream (see `channelRecordProvenanceDamaged` for the
236
+ // recorder-truth analysis of exactly which pairs a
237
+ // recording can produce).
238
+ if (effect.kind == .channel and channelRecordProvenanceDamaged(effect)) {
239
+ std.debug.print(
240
+ "replay refused after event {d}: channel record for key {d} claims a .{s} event stamped with .{s} provenance - the recorder stamps .rejected only on regenerating .rejected admission refusals and every other channel record keeps .exited, so the journal is damaged or hand-edited; re-record the session\n",
241
+ .{ report.events_replayed, effect.key, @tagName(effect.channel_kind), @tagName(effect.exit_reason) },
242
+ );
243
+ return error.ReplayDamagedRecord;
244
+ }
245
+ if (effect.kind == .image and imageDimsDamaged(effect)) {
246
+ std.debug.print(
247
+ "replay refused after event {d}: image record for id {d} claims .{s} with dimensions {d}x{d} - a recorded .loaded always carries nonzero decoded dimensions within the registered-image pixel budget and every other outcome records 0x0, so the journal is damaged or hand-edited; re-record the session\n",
248
+ .{ report.events_replayed, effect.key, @tagName(effect.image_outcome), effect.image_width, effect.image_height },
249
+ );
250
+ return error.ReplayDamagedRecord;
251
+ }
252
+ if (effect.kind == .video and videoScalarsDamaged(effect)) {
253
+ std.debug.print(
254
+ "replay refused after event {d}: video record for key {d} carries a millisecond or dimension value at or past 2^53 - recorded playback scalars ride the exact-integer window every delivery tier can carry, so the journal is damaged or hand-edited; re-record the session\n",
255
+ .{ report.events_replayed, effect.key },
256
+ );
257
+ return error.ReplayDamagedRecord;
258
+ }
259
+ // Audio scalars obey the recorder the same way (the
260
+ // delivery boundary clamps them into the exact-integer
261
+ // window before anything journals), so an out-of-window
262
+ // value is journal damage — refuse it rather than
263
+ // silently reshaping the recorded stream at the feed.
264
+ if (effect.kind == .audio and audioScalarsDamaged(effect)) {
265
+ std.debug.print(
266
+ "replay refused after event {d}: audio record for key {d} carries a millisecond value at or past 2^53 - recorded playback scalars ride the exact-integer window every delivery tier can carry, so the journal is damaged or hand-edited; re-record the session\n",
267
+ .{ report.events_replayed, effect.key },
268
+ );
269
+ return error.ReplayDamagedRecord;
270
+ }
271
+ // Provenance consistency, gated BEFORE the regeneration
272
+ // skip below: a `.rejected` stamped onto a delivered
273
+ // record (nonzero token) would be skipped there and its
274
+ // handler Msg silently omitted from the stream.
275
+ if (effect.kind == .video and videoRecordProvenanceDamaged(effect)) {
276
+ std.debug.print(
277
+ "replay refused after event {d}: video record for key {d} claims .{s} with load token {d} - the recorder stamps token 0 exactly on loop-side rejections and a minted token on every delivery, so the journal is damaged or hand-edited; re-record the session\n",
278
+ .{ report.events_replayed, effect.key, @tagName(effect.video_kind), effect.video_token },
279
+ );
280
+ return error.ReplayDamagedRecord;
281
+ }
282
+ if (effect.kind == .video and videoRecordShapeDamaged(effect)) {
283
+ std.debug.print(
284
+ "replay refused after event {d}: video record for key {d} claims .{s} with motion or geometry the recorder never writes on that kind - terminals deliver with playing and buffering false and no dimensions, and a completion pins position to the duration, so the journal is damaged or hand-edited; re-record the session\n",
285
+ .{ report.events_replayed, effect.key, @tagName(effect.video_kind) },
286
+ );
287
+ return error.ReplayDamagedRecord;
288
+ }
289
+ if (effect.kind == .video_load and videoLoadOutcomeDamaged(effect)) {
290
+ std.debug.print(
291
+ "replay refused after event {d}: video load record for key {d} claims outcome .{s} - the recorder stamps .loaded on a resolved cascade and .failed on a refusal, nothing else, so the journal is damaged or hand-edited; re-record the session\n",
292
+ .{ report.events_replayed, effect.key, @tagName(effect.video_kind) },
293
+ );
294
+ return error.ReplayDamagedRecord;
295
+ }
296
+ // Pty records obey the recorder the same way: output
297
+ // batches journal their bytes OUT OF LINE (an output
298
+ // record's payload is always empty on disk, its blob
299
+ // bounded by the chunk size), and exits carry neither
300
+ // payload nor blob. Refuse contradictions before the
301
+ // feed, like the channel gate above.
302
+ if (effect.kind == .pty and ptyRecordDamaged(effect)) {
303
+ std.debug.print(
304
+ "replay refused after event {d}: pty record for key {d} claims .{s} with {d} payload bytes and a {d}-byte blob - a recorded output batch rides the blob store bounded at {d} bytes and an exit carries neither, so the journal is damaged or hand-edited; re-record the session\n",
305
+ .{ report.events_replayed, effect.key, @tagName(effect.pty_kind), effect.payload.len, effect.pty_blob_len, runtime_effects.max_effect_pty_chunk_bytes },
306
+ );
307
+ return error.ReplayDamagedRecord;
308
+ }
309
+ // Provenance, gated before the regeneration skip (the
310
+ // channel argument): the recorder stamps `.rejected`
311
+ // only on regenerating admission refusals, which are
312
+ // always `.exit` events; an output record wearing exit
313
+ // provenance would be silently omitted below.
314
+ if (effect.kind == .pty and ptyRecordProvenanceDamaged(effect)) {
315
+ std.debug.print(
316
+ "replay refused after event {d}: pty record for key {d} claims a .{s} event stamped with .{s} provenance - only .exit records carry a non-.exited reason, so the journal is damaged or hand-edited; re-record the session\n",
317
+ .{ report.events_replayed, effect.key, @tagName(effect.pty_kind), @tagName(effect.exit_reason) },
318
+ );
319
+ return error.ReplayDamagedRecord;
320
+ }
156
321
  if (effectRegeneratesUnderReplay(effect)) {
157
322
  report.effects_skipped += 1;
158
323
  continue;
159
324
  }
160
- app.replayControl(.{ .feed = effect }) catch |err| switch (err) {
161
- error.EffectNotFound => {
325
+ // Resolve out-of-line payloads: an image record's
326
+ // source bytes come from the blob store, verified
327
+ // against the journaled address and length, and feed
328
+ // as `payload` — the recorded bytes, byte-identical,
329
+ // no network. The scratch lives until the feed below
330
+ // returns (the fed bytes are copied into the stub
331
+ // executor's slot buffer).
332
+ var blob_scratch: ?[]u8 = null;
333
+ defer if (blob_scratch) |scratch| std.heap.page_allocator.free(scratch);
334
+ 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| {
162
336
  std.debug.print(
163
- "replay diverged after event {d}: journaled {s} result for effect key {d} has no matching pending request - the replayed updates issued different effects than the recording (nondeterminism outside the effect boundary?)\n",
164
- .{ report.events_replayed, @tagName(effect.kind), effect.key },
337
+ "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
+ .{ report.events_replayed, effect.key, session_blobs.hexName(effect.pty_blob_hash), effect.pty_blob_len, @errorName(err) },
165
339
  );
166
- return error.ReplayEffectDivergence;
167
- },
168
- error.ReplayUnsupported => {
340
+ return error.ReplayMissingBlob;
341
+ };
342
+ effect.payload = bytes;
343
+ }
344
+ 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| {
169
346
  std.debug.print(
170
- "replay refused: the journal carries effect results but this app registered no replay hook (App.replay_fn - UiApp wires it automatically)\n",
171
- .{},
347
+ "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
+ .{ report.events_replayed, effect.key, session_blobs.hexName(effect.image_blob_hash), effect.image_blob_len, @errorName(err) },
172
349
  );
173
- return error.ReplayUnsupportedApp;
174
- },
175
- else => return err,
176
- };
350
+ return error.ReplayMissingBlob;
351
+ };
352
+ effect.payload = bytes;
353
+ }
354
+ // Feed with back-pressure: results journal in delivery
355
+ // order, so one recorded drain pass can carry more
356
+ // results than the completion queue holds (a live
357
+ // recording's workers keep refilling the queue while
358
+ // the loop drains it). A feed that reports the queue
359
+ // full drains the loop through the same `.wake`
360
+ // dispatch the platform delivers live — the parked
361
+ // request keeps its bytes, and delivery stays
362
+ // queue-ordered — then feeds once more. That one drain
363
+ // empties the whole queue, so a second refusal is a
364
+ // real fault and propagates.
365
+ var drained_for_room = false;
366
+ feed: while (true) {
367
+ app.replayControl(.{ .feed = effect }) catch |err| switch (err) {
368
+ error.EffectQueueFull => {
369
+ if (drained_for_room) return err;
370
+ drained_for_room = true;
371
+ try runtime.dispatchPlatformEvent(app, .wake);
372
+ continue :feed;
373
+ },
374
+ error.EffectNotFound => {
375
+ std.debug.print(
376
+ "replay diverged after event {d}: journaled {s} result for effect key {d} has no matching pending request - the replayed updates issued different effects than the recording (nondeterminism outside the effect boundary?)\n",
377
+ .{ report.events_replayed, @tagName(effect.kind), effect.key },
378
+ );
379
+ return error.ReplayEffectDivergence;
380
+ },
381
+ error.ReplayUnsupported => {
382
+ std.debug.print(
383
+ "replay refused: the journal carries effect results but this app registered no replay hook (App.replay_fn - UiApp wires it automatically)\n",
384
+ .{},
385
+ );
386
+ return error.ReplayUnsupportedApp;
387
+ },
388
+ else => return err,
389
+ };
390
+ break :feed;
391
+ }
177
392
  report.effects_fed += 1;
178
393
  },
179
394
  .checkpoint => |checkpoint| {
@@ -206,9 +421,229 @@ pub fn replaySession(
206
421
  .end => {},
207
422
  }
208
423
  }
424
+ // End-of-journal consistency: every fed-but-queued record must have
425
+ // been consumed by the load it named, and nothing the replayed
426
+ // timeline did may have latched a divergence
427
+ // (`Effects.finishReplay`). Unconditional — a recording with ZERO
428
+ // effect records still fails here when the replayed updates issued
429
+ // a load the recording never journaled. Apps without a replay hook
430
+ // have no armed channel to check and answer ReplayUnsupported,
431
+ // which is fine: nothing was fed and nothing could have latched.
432
+ app.replayControl(.finish) catch |err| switch (err) {
433
+ error.ReplayUnsupported => {},
434
+ else => {
435
+ std.debug.print(
436
+ "replay diverged at the journal's end: {s} - the replayed updates issued different effects than the recording (nondeterminism outside the effect boundary?)\n",
437
+ .{@errorName(err)},
438
+ );
439
+ return error.ReplayEffectDivergence;
440
+ },
441
+ };
209
442
  return report;
210
443
  }
211
444
 
445
+ /// Read one journal-referenced blob into fresh scratch (handed to the
446
+ /// caller through `scratch_out` for freeing) and verify it against the
447
+ /// record: present, exact length, and hashing to its address — the
448
+ /// same hostile-input honesty the journal reader keeps.
449
+ fn resolveBlob(
450
+ blob_hash: [runtime_effects.effect_image_blob_hash_len]u8,
451
+ blob_len_field: u64,
452
+ blobs: ?session_blobs.SessionBlobSource,
453
+ scratch_out: *?[]u8,
454
+ ) anyerror![]const u8 {
455
+ const blob_source = blobs orelse return error.BlobMissing;
456
+ if (blob_len_field > session_blobs.max_blob_bytes) return error.BlobOverBudget;
457
+ const blob_len: usize = @intCast(blob_len_field);
458
+ // One spare byte proves the stored blob is not LONGER than the
459
+ // record claims (the source reads at most the buffer).
460
+ const scratch = try std.heap.page_allocator.alloc(u8, blob_len + 1);
461
+ scratch_out.* = scratch;
462
+ const bytes = try blob_source.read_fn(blob_source.context, blob_hash, scratch);
463
+ if (bytes.len != blob_len) return error.BlobCorrupt;
464
+ return bytes;
465
+ }
466
+
467
+ /// Whether an image record's journaled dimensions contradict what the
468
+ /// recorder can produce (see the gate in `replaySession`): `.loaded`
469
+ /// carries nonzero width and height whose RGBA8 byte size — computed
470
+ /// overflow-checked, a hand-edited product that wraps u64 is damage,
471
+ /// not a small image — fits `registerCanvasImage`'s per-image slot
472
+ /// bound; every other outcome carries 0x0.
473
+ /// Whether a channel record's journaled shape contradicts what the
474
+ /// recorder can produce (see the gate in `replaySession`): `.data`
475
+ /// payloads stay within the post bound; `.closed` and `.rejected`
476
+ /// carry no bytes.
477
+ /// Whether a pty record's journaled shape contradicts what the
478
+ /// recorder can produce: output batches journal with an EMPTY payload
479
+ /// (the bytes moved into the blob store, bounded by the chunk size);
480
+ /// exits carry neither payload nor blob.
481
+ fn ptyRecordDamaged(record: journal.EffectResultRecord) bool {
482
+ if (record.payload.len > 0) return true;
483
+ if (record.pty_blob_len > runtime_effects.max_effect_pty_chunk_bytes) return true;
484
+ switch (record.pty_kind) {
485
+ // The recorder journals output ONLY for a non-empty batch, so
486
+ // its blob is always non-empty — a zero-length output blob is
487
+ // damage that would otherwise replay a synthetic empty output
488
+ // event and diverge the fingerprint. The scalar fields are
489
+ // canonical (-1 code sentinel, no signal, no drops): replay
490
+ // delivers those exact defaults regardless, so a record claiming
491
+ // anything else would be silently rewritten — refuse it instead.
492
+ .output => {
493
+ if (record.pty_blob_len == 0) return true;
494
+ if (record.code != runtime_effects.effect_error_exit_code) return true;
495
+ if (record.pty_signal != 0 or record.pty_dropped_writes != 0) return true;
496
+ },
497
+ // Exits carry neither payload nor blob, and their code/signal
498
+ // must obey the delivered contract (`signal != 0` iff the reason
499
+ // is `.signaled`; `code == -1` for every reason but `.exited`).
500
+ // A hand-edited `.cancelled` with code 0 or signal 9 would
501
+ // otherwise dispatch an event violating that contract. The
502
+ // values are also RANGE-gated to what the transport can produce:
503
+ // `waitpid`'s status word yields exit codes 0..255 (plus the
504
+ // documented -1 for a child reaped outside the toolkit) and
505
+ // signals 1..127 — anything else is hand-editing, refused
506
+ // rather than replayed into an event no live run can emit.
507
+ .exit => {
508
+ if (record.pty_blob_len > 0) return true;
509
+ const signaled = record.exit_reason == .signaled;
510
+ if (signaled != (record.pty_signal != 0)) return true;
511
+ if (record.exit_reason != .exited and record.code != runtime_effects.effect_error_exit_code) return true;
512
+ if (record.exit_reason == .exited and
513
+ record.code != runtime_effects.effect_error_exit_code and
514
+ (record.code < 0 or record.code > 255)) return true;
515
+ if (signaled and (record.pty_signal < 1 or record.pty_signal > 127)) return true;
516
+ },
517
+ // A write-admission verdict carries only the accepted bit in
518
+ // `code` (1/0) — no blob, no signal, no drop count.
519
+ .write => {
520
+ if (record.pty_blob_len > 0) return true;
521
+ if (record.code != 0 and record.code != 1) return true;
522
+ if (record.pty_signal != 0 or record.pty_dropped_writes != 0) return true;
523
+ },
524
+ }
525
+ return false;
526
+ }
527
+
528
+ /// Recorder truth for pty provenance: output records always carry
529
+ /// `.exited` (the live drain never touches the exit reason on an
530
+ /// output); every reason is legal on an `.exit` record (`.rejected` =
531
+ /// regenerating admission refusal, `.spawn_failed` = executor-truth
532
+ /// start failure, the rest = real endings).
533
+ fn ptyRecordProvenanceDamaged(record: journal.EffectResultRecord) bool {
534
+ // Output and write-verdict records always carry `.exited` (neither
535
+ // journal site ever touches the reason).
536
+ if (record.pty_kind != .exit and record.exit_reason != .exited) return true;
537
+ // The regenerating-provenance bit only ever rides an admission
538
+ // refusal — an `.exit` record whose reason is `.rejected`.
539
+ if (record.truncated and (record.pty_kind != .exit or record.exit_reason != .rejected)) return true;
540
+ return false;
541
+ }
542
+
543
+ fn channelRecordDamaged(record: journal.EffectResultRecord) bool {
544
+ if (record.payload.len > runtime_effects.max_effect_channel_bytes) return true;
545
+ return record.channel_kind != .data and record.payload.len > 0;
546
+ }
547
+
548
+ /// Whether a channel record's provenance stamp contradicts its event
549
+ /// kind — RECORDER TRUTH: channel records journal from exactly two
550
+ /// sites. The live drain (staged posts and the close marker) journals
551
+ /// `.data` and `.closed` events and never touches `exit_reason`, so
552
+ /// they always carry `.exited`; the pending-terminal ring journals only
553
+ /// `.rejected` events, stamped `.rejected` when the refusal is
554
+ /// regenerating loop-side admission validation and left `.exited` when
555
+ /// it is executor truth (an open that could not stage its channel). The
556
+ /// legal pairs are therefore exactly (.data, .exited),
557
+ /// (.closed, .exited), (.rejected, .exited), (.rejected, .rejected).
558
+ /// The forward mismatch is the dangerous one: a `.data` or `.closed`
559
+ /// record stamped `.rejected` sails into the regeneration skip and is
560
+ /// silently OMITTED — with verification disabled, replay succeeds with
561
+ /// a different Msg stream. The reverse direction needs no twin gate
562
+ /// beyond the range check: `.rejected` with `.exited` is exactly the
563
+ /// executor-truth rejection and must feed; and no recorder site can
564
+ /// write any other exit reason on a channel record, so a decoded
565
+ /// `.signaled`/`.cancelled`/`.spawn_failed` (valid members for SPAWN
566
+ /// records) is hand-editing.
567
+ fn channelRecordProvenanceDamaged(record: journal.EffectResultRecord) bool {
568
+ if (record.exit_reason == .rejected) return record.channel_kind != .rejected;
569
+ return record.exit_reason != .exited;
570
+ }
571
+
572
+ /// A structurally valid u64 in a video record is not automatically an
573
+ /// HONEST one: the engine clamps every video scalar into the
574
+ /// exact-integer delivery window at the delivery boundary
575
+ /// (`Effects.takeVideoMsg`, `max_effect_video_scalar_exclusive`), so no
576
+ /// recorder can write a position, duration, or dimension at or past
577
+ /// 2^53 whatever a host or embedder reports — and the TS delivery tier
578
+ /// carries these scalars through the subset's exact-integer number
579
+ /// window, where feeding a larger value would trap in the bridge's
580
+ /// numeric widening instead of refusing the hostile file at the gate.
581
+ fn videoScalarsDamaged(record: journal.EffectResultRecord) bool {
582
+ const max_exact: u64 = runtime_effects.max_effect_video_scalar_exclusive;
583
+ return record.video_position_ms >= max_exact or
584
+ record.video_duration_ms >= max_exact or
585
+ record.video_width >= max_exact or
586
+ record.video_height >= max_exact;
587
+ }
588
+
589
+ /// The audio twin: positions and durations clamp into the exact-integer
590
+ /// window at every delivery entry before they journal, so a recorded
591
+ /// value at or past 2^53 can only be a damaged or hand-edited journal.
592
+ fn audioScalarsDamaged(record: journal.EffectResultRecord) bool {
593
+ const max_exact: u64 = runtime_effects.max_effect_video_scalar_exclusive;
594
+ return record.audio_position_ms >= max_exact or
595
+ record.audio_duration_ms >= max_exact;
596
+ }
597
+
598
+ /// A `.video_load` record's `video_kind` is the load's OUTCOME, and the
599
+ /// recorder writes exactly two values: `.loaded` on a resolved cascade
600
+ /// and `.failed` on a synchronous refusal. Any other kind steers
601
+ /// nothing honestly (a `.position` would leave the replayed fake load
602
+ /// active where nothing is known), so it refuses at the gate.
603
+ /// A `.video` record's kind and token are recorder-coupled: loop-side
604
+ /// rejections never minted a token (they stamp 0) and every DELIVERY —
605
+ /// position ticks, terminals, acknowledgments — carries the minted
606
+ /// token of the load that produced it. A `.rejected` with a nonzero
607
+ /// token would silently skip a real delivery through the regeneration
608
+ /// gate; a delivery with token 0 could never have routed. Both are
609
+ /// damage.
610
+ fn videoRecordProvenanceDamaged(record: journal.EffectResultRecord) bool {
611
+ if (record.video_kind == .rejected) return record.video_token != 0;
612
+ return record.video_token == 0;
613
+ }
614
+
615
+ /// Recorder truth for a `.video` record's payload SHAPE, per kind:
616
+ /// `.failed` and `.rejected` deliver with playing and buffering false
617
+ /// (the terminal resolution forces the flags) and no dimensions (no
618
+ /// failure path ever measured a frame); `.completed` pins position to
619
+ /// the duration with the flags false. A record violating these can
620
+ /// only be hand-edited — and a synchronously failed load's record has
621
+ /// no platform event behind it to cross-check at the pairing, so the
622
+ /// gate is where the impossible payload refuses.
623
+ fn videoRecordShapeDamaged(record: journal.EffectResultRecord) bool {
624
+ return switch (record.video_kind) {
625
+ .failed, .rejected => record.video_playing or record.video_buffering or
626
+ record.video_width != 0 or record.video_height != 0,
627
+ .completed => record.video_playing or record.video_buffering or
628
+ record.video_position_ms != record.video_duration_ms,
629
+ .loaded, .position => false,
630
+ };
631
+ }
632
+
633
+ fn videoLoadOutcomeDamaged(record: journal.EffectResultRecord) bool {
634
+ return record.video_kind != .loaded and record.video_kind != .failed;
635
+ }
636
+
637
+ fn imageDimsDamaged(record: journal.EffectResultRecord) bool {
638
+ if (record.image_outcome != .loaded) {
639
+ return record.image_width != 0 or record.image_height != 0;
640
+ }
641
+ if (record.image_width == 0 or record.image_height == 0) return true;
642
+ const pixels = std.math.mul(u64, record.image_width, record.image_height) catch return true;
643
+ const pixel_bytes = std.math.mul(u64, pixels, 4) catch return true;
644
+ return pixel_bytes > canvas_limits.max_registered_canvas_image_pixel_bytes;
645
+ }
646
+
212
647
  /// Journaled results that regenerate deterministically from the
213
648
  /// replayed updates themselves — feeding them would double-deliver:
214
649
  /// rejections (the same over-capacity/duplicate-key validation refuses
@@ -218,6 +653,13 @@ fn effectRegeneratesUnderReplay(record: journal.EffectResultRecord) bool {
218
653
  return switch (record.kind) {
219
654
  .timer => true,
220
655
  .exit => record.exit_reason == .rejected,
656
+ // Only DETERMINISTIC admission refusals regenerate — marked by
657
+ // the provenance bit the recorder set (`truncated`, unused for
658
+ // pty otherwise). Executor-truth terminals — start failures,
659
+ // the platform-unsupported rejection, output, and real exits —
660
+ // are external inputs and must be fed, even when their reason
661
+ // is `.rejected`.
662
+ .pty => record.pty_kind == .exit and record.truncated,
221
663
  .response => record.fetch_outcome == .rejected,
222
664
  .file => record.file_outcome == .rejected,
223
665
  .clipboard => record.clipboard_outcome == .rejected,
@@ -226,9 +668,42 @@ fn effectRegeneratesUnderReplay(record: journal.EffectResultRecord) bool {
226
668
  // position ticks, completions, platform failures — is an
227
669
  // external input and must be fed.
228
670
  .audio => record.audio_kind == .rejected,
671
+ // Video rejections are the same loop-side validation (source
672
+ // bounds, scheme, surface-id shape) and regenerate; failures —
673
+ // including a claim or platform load the recording host
674
+ // refused — are executor truth the replayed fake never
675
+ // reproduces, so they feed like every other kind.
676
+ .video => record.video_kind == .rejected,
677
+ // The cascade resolution is the recording host's filesystem
678
+ // truth — the replayed fake load cannot re-run the local
679
+ // probe, so the record must feed.
680
+ .video_load => false,
229
681
  // Host-request rejections mark themselves with the exit reason
230
682
  // (the `.host` record encoding); host answers must be fed.
231
683
  .host => record.exit_reason == .rejected,
684
+ // Image `.rejected` terminals journal from BOTH sides of the
685
+ // executor seam, so the outcome alone is not provenance: only
686
+ // loop-side validation refusals — which the replayed
687
+ // `loadImage` regenerates — mark themselves with the exit
688
+ // reason (the `.host` records' convention, above). Worker-side
689
+ // rejections (a URL that passes the loop's scheme check but
690
+ // cannot become a request, an executor that could not start a
691
+ // cancelable load) keep `.exited`: the fake executor parks
692
+ // those requests, so the journaled record is the ONLY terminal
693
+ // and must be fed like every other worker truth.
694
+ .image => record.exit_reason == .rejected,
695
+ // Channel `.rejected` terminals follow the image convention
696
+ // exactly: admission refusals (occupied key, full channel
697
+ // table) mark themselves with the exit reason and regenerate —
698
+ // the replayed `openChannel` re-runs the same deterministic
699
+ // gates against re-derived occupancy windows and stages its
700
+ // own. An executor-truth rejection (the open that could not
701
+ // stage its channel) keeps `.exited` and FEEDS, retiring the
702
+ // slot the replayed open parked. `.data` and `.closed` are
703
+ // executor truth by definition — the source thread never
704
+ // re-runs at replay, so the journaled events are the ONLY
705
+ // delivery.
706
+ .channel => record.exit_reason == .rejected,
232
707
  // Launch-env deliveries are exactly what must NOT regenerate:
233
708
  // the recorded values feed the replayed envMsgs dispatch so the
234
709
  // replay launch's environment is never consulted.
@@ -265,6 +740,67 @@ fn renderScreenshotHash(runtime: *core.Runtime, view_label: []const u8, scale: f
265
740
  return std.hash.Wyhash.hash(0, writer.written());
266
741
  }
267
742
 
743
+ test "exit records outside the transport's producible ranges are damaged" {
744
+ // Every code waitpid can produce passes: 0..255, plus the
745
+ // documented -1 sentinel for a child reaped outside the toolkit.
746
+ var record: journal.EffectResultRecord = .{
747
+ .kind = .pty,
748
+ .key = 1,
749
+ .pty_kind = .exit,
750
+ .exit_reason = .exited,
751
+ .code = 0,
752
+ };
753
+ try std.testing.expect(!ptyRecordDamaged(record));
754
+ record.code = 255;
755
+ try std.testing.expect(!ptyRecordDamaged(record));
756
+ record.code = runtime_effects.effect_error_exit_code;
757
+ try std.testing.expect(!ptyRecordDamaged(record));
758
+
759
+ // Hand-edited codes no status word can carry are refused.
760
+ record.code = 256;
761
+ try std.testing.expect(ptyRecordDamaged(record));
762
+ record.code = -2;
763
+ try std.testing.expect(ptyRecordDamaged(record));
764
+
765
+ // Signals: waitpid's status word carries 1..127.
766
+ record = .{
767
+ .kind = .pty,
768
+ .key = 1,
769
+ .pty_kind = .exit,
770
+ .exit_reason = .signaled,
771
+ .code = runtime_effects.effect_error_exit_code,
772
+ .pty_signal = 9,
773
+ };
774
+ try std.testing.expect(!ptyRecordDamaged(record));
775
+ record.pty_signal = 127;
776
+ try std.testing.expect(!ptyRecordDamaged(record));
777
+ record.pty_signal = 128;
778
+ try std.testing.expect(ptyRecordDamaged(record));
779
+ record.pty_signal = -9;
780
+ try std.testing.expect(ptyRecordDamaged(record));
781
+ }
782
+
783
+ test "audio records outside the exact-integer scalar window are damaged" {
784
+ // The delivery boundary clamps positions and durations below 2^53
785
+ // before anything journals, so every in-window value passes and
786
+ // every out-of-window value can only be journal damage.
787
+ var record: journal.EffectResultRecord = .{
788
+ .kind = .audio,
789
+ .key = 1,
790
+ .audio_kind = .position,
791
+ .audio_position_ms = 0,
792
+ .audio_duration_ms = 183_000,
793
+ };
794
+ try std.testing.expect(!audioScalarsDamaged(record));
795
+ record.audio_position_ms = runtime_effects.max_effect_video_scalar_exclusive - 1;
796
+ try std.testing.expect(!audioScalarsDamaged(record));
797
+ record.audio_position_ms = runtime_effects.max_effect_video_scalar_exclusive;
798
+ try std.testing.expect(audioScalarsDamaged(record));
799
+ record.audio_position_ms = 0;
800
+ record.audio_duration_ms = std.math.maxInt(u64);
801
+ try std.testing.expect(audioScalarsDamaged(record));
802
+ }
803
+
268
804
  /// Debug aid: `NATIVE_SDK_SESSION_REPLAY_DUMP=<dir>` writes each
269
805
  /// replay-rendered screenshot PNG so a mismatching pixel mark can be
270
806
  /// diffed against the recording's artifact.