@native-sdk/cli 0.5.4 → 0.6.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 (149) hide show
  1. package/build/app.zig +50 -1
  2. package/build.zig +588 -32
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +3 -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 +280 -8
  9. package/packages/core/sdk/core.ts +481 -2
  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 +599 -3
  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 +39 -2
  17. package/skill-data/ts-core/SKILL.md +4 -2
  18. package/src/app_runner/root.zig +32 -10
  19. package/src/automation/layout_fingerprint.zig +160 -0
  20. package/src/automation/protocol.zig +82 -33
  21. package/src/automation/root.zig +1 -0
  22. package/src/automation/server.zig +1 -1
  23. package/src/automation/snapshot.zig +68 -9
  24. package/src/automation/watcher.zig +1 -1
  25. package/src/embed/host.zig +5 -0
  26. package/src/embed/ui_host.zig +13 -0
  27. package/src/platform/linux/gtk_host.c +527 -13
  28. package/src/platform/linux/gtk_host.h +25 -0
  29. package/src/platform/linux/root.zig +280 -7
  30. package/src/platform/macos/appkit_host.h +104 -6
  31. package/src/platform/macos/appkit_host.m +1368 -74
  32. package/src/platform/macos/cef_host.mm +110 -1
  33. package/src/platform/macos/root.zig +287 -7
  34. package/src/platform/null_platform.zig +398 -4
  35. package/src/platform/null_platform_tests.zig +35 -0
  36. package/src/platform/root.zig +9 -0
  37. package/src/platform/types.zig +386 -14
  38. package/src/platform/windows/root.zig +298 -9
  39. package/src/platform/windows/webview2_host.cpp +330 -5
  40. package/src/primitives/app_manifest/validation.zig +18 -0
  41. package/src/primitives/canvas/a11y_audit.zig +33 -11
  42. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  43. package/src/primitives/canvas/drawing.zig +25 -0
  44. package/src/primitives/canvas/equality.zig +8 -0
  45. package/src/primitives/canvas/events.zig +103 -16
  46. package/src/primitives/canvas/frame.zig +125 -1
  47. package/src/primitives/canvas/layout_audit.zig +22 -5
  48. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  49. package/src/primitives/canvas/render_tests.zig +13 -10
  50. package/src/primitives/canvas/root.zig +48 -0
  51. package/src/primitives/canvas/terminal_box.zig +471 -0
  52. package/src/primitives/canvas/terminal_grid.zig +978 -0
  53. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  54. package/src/primitives/canvas/test_support.zig +1 -0
  55. package/src/primitives/canvas/tests.zig +1 -0
  56. package/src/primitives/canvas/text_tests.zig +39 -4
  57. package/src/primitives/canvas/tokens.zig +115 -12
  58. package/src/primitives/canvas/ui.zig +524 -5
  59. package/src/primitives/canvas/ui_markup.zig +195 -2
  60. package/src/primitives/canvas/ui_markup_compiled.zig +172 -1
  61. package/src/primitives/canvas/ui_markup_compiled_tests.zig +133 -8
  62. package/src/primitives/canvas/ui_markup_contract.zig +88 -5
  63. package/src/primitives/canvas/ui_markup_contract_tests.zig +39 -1
  64. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  65. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  66. package/src/primitives/canvas/ui_markup_tests.zig +149 -0
  67. package/src/primitives/canvas/ui_markup_view.zig +191 -0
  68. package/src/primitives/canvas/ui_markup_view_tests.zig +403 -19
  69. package/src/primitives/canvas/ui_schema.zig +97 -17
  70. package/src/primitives/canvas/ui_schema_tests.zig +18 -11
  71. package/src/primitives/canvas/ui_tests.zig +141 -9
  72. package/src/primitives/canvas/widget_access.zig +28 -2
  73. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  74. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  75. package/src/primitives/canvas/widget_layout.zig +59 -11
  76. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  77. package/src/primitives/canvas/widget_render.zig +272 -18
  78. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  79. package/src/primitives/canvas/widget_routing.zig +59 -7
  80. package/src/primitives/canvas/widget_runtime.zig +23 -0
  81. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  82. package/src/primitives/canvas/widget_semantics.zig +119 -11
  83. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  84. package/src/primitives/canvas/widget_tree.zig +17 -0
  85. package/src/primitives/canvas/widgets.zig +132 -0
  86. package/src/root.zig +16 -0
  87. package/src/runtime/api.zig +76 -6
  88. package/src/runtime/automation_commands.zig +20 -1
  89. package/src/runtime/automation_snapshot.zig +25 -1
  90. package/src/runtime/automation_widget_dispatch.zig +73 -8
  91. package/src/runtime/bridge_payload.zig +1 -0
  92. package/src/runtime/canvas_frame.zig +62 -7
  93. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  94. package/src/runtime/canvas_frame_helpers.zig +190 -1
  95. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  96. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  97. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  98. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  99. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  100. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  101. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  102. package/src/runtime/canvas_widget_display.zig +1 -1
  103. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  104. package/src/runtime/canvas_widget_events.zig +268 -26
  105. package/src/runtime/canvas_widget_runtime.zig +213 -29
  106. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  107. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  108. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  109. package/src/runtime/canvas_widget_state.zig +6 -0
  110. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  111. package/src/runtime/core.zig +73 -2
  112. package/src/runtime/effects.zig +6389 -54
  113. package/src/runtime/effects_channel_tests.zig +3519 -0
  114. package/src/runtime/effects_pty_tests.zig +1362 -0
  115. package/src/runtime/effects_tests.zig +14 -9
  116. package/src/runtime/effects_video_tests.zig +3609 -0
  117. package/src/runtime/flow.zig +25 -4
  118. package/src/runtime/gpu_surface_events.zig +475 -15
  119. package/src/runtime/media_surface.zig +116 -11
  120. package/src/runtime/media_surface_tests.zig +61 -0
  121. package/src/runtime/pty.zig +1252 -0
  122. package/src/runtime/pty_windows.zig +1780 -0
  123. package/src/runtime/root.zig +25 -0
  124. package/src/runtime/runtime_event_tests.zig +20 -1
  125. package/src/runtime/session_journal.zig +367 -61
  126. package/src/runtime/session_record.zig +16 -0
  127. package/src/runtime/session_replay.zig +395 -11
  128. package/src/runtime/session_tests.zig +114 -19
  129. package/src/runtime/terminal_session.zig +1569 -0
  130. package/src/runtime/terminal_session_tests.zig +450 -0
  131. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  132. package/src/runtime/terminal_vt_stub.zig +13 -0
  133. package/src/runtime/tests.zig +6 -0
  134. package/src/runtime/ts_core_host.zig +980 -140
  135. package/src/runtime/ts_core_host_tests.zig +1059 -1
  136. package/src/runtime/ts_ui_app.zig +16 -5
  137. package/src/runtime/ui_app.zig +2338 -67
  138. package/src/runtime/ui_app_tests.zig +3171 -744
  139. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  140. package/src/runtime/ui_app_window_tests.zig +268 -0
  141. package/src/runtime/view.zig +190 -2
  142. package/src/runtime/view_widget_scroll.zig +218 -68
  143. package/src/runtime/view_widget_text.zig +11 -2
  144. package/src/runtime/view_widget_tree.zig +111 -3
  145. package/src/runtime/window_storage.zig +45 -2
  146. package/src/runtime/window_view_runtime.zig +34 -2
  147. package/src/runtime/window_views.zig +1 -0
  148. package/src/terminal_session_tests_root.zig +15 -0
  149. package/src/tooling/templates.zig +25 -7
@@ -213,6 +213,22 @@ pub const SessionRecorder = struct {
213
213
  journaled.image_blob_len = record.payload.len;
214
214
  journaled.payload = "";
215
215
  }
216
+ // Pty output batches take the same road: bytes into the
217
+ // content-addressed store, address into the record — stream
218
+ // payloads stay out of the journal and identical batches
219
+ // (prompts, repeated screens) share one blob.
220
+ if (record.kind == .pty and record.payload.len > 0) {
221
+ const blob_sink = self.blob_sink orelse {
222
+ return self.fail("a pty output result needs the session blob store, and none is bound - wire SessionRecorder.blob_sink (the app runner creates blobs/ beside the journal)");
223
+ };
224
+ const hash = session_blobs.hashBytes(record.payload);
225
+ blob_sink.write_fn(blob_sink.context, hash, record.payload) catch |err| {
226
+ return self.fail(@errorName(err));
227
+ };
228
+ journaled.pty_blob_hash = hash;
229
+ journaled.pty_blob_len = record.payload.len;
230
+ journaled.payload = "";
231
+ }
216
232
  const payload = journal.encodeEffect(journaled, &self.effect_buffer) catch {
217
233
  return self.fail("an effect result exceeded max_session_record_bytes");
218
234
  };
@@ -32,13 +32,14 @@ const canvas_limits = @import("canvas_limits.zig");
32
32
  const automation_protocol = @import("../automation/protocol.zig");
33
33
  const core = @import("core.zig");
34
34
  const journal = @import("session_journal.zig");
35
+ const runtime_effects = @import("effects.zig");
35
36
  const session_blobs = @import("session_blobs.zig");
36
37
 
37
38
  pub const ReplayError = error{
38
39
  /// The journal was recorded on a different platform; v1 replay is
39
40
  /// same-platform only (font metrics and scale behavior differ).
40
41
  ReplayPlatformMismatch,
41
- /// The journal's automation protocol version differs from this
42
+ /// The journal's automation protocol fingerprint differs from this
42
43
  /// build's — the recording binary and this one are skewed.
43
44
  ReplayProtocolMismatch,
44
45
  /// A journaled effect result found no parked request with its key:
@@ -94,7 +95,7 @@ pub const ReplayOptions = struct {
94
95
  };
95
96
 
96
97
  pub const ReplayReport = struct {
97
- protocol_version: u32 = 0,
98
+ protocol_fingerprint: u64 = 0,
98
99
  events_replayed: u64 = 0,
99
100
  effects_fed: u64 = 0,
100
101
  effects_skipped: u64 = 0,
@@ -147,11 +148,11 @@ pub fn replaySession(
147
148
  while (try reader.next()) |record| {
148
149
  switch (record) {
149
150
  .header => |header| {
150
- report.protocol_version = header.protocol_version;
151
- if (header.protocol_version != automation_protocol.version) {
151
+ report.protocol_fingerprint = header.protocol_fingerprint;
152
+ if (header.protocol_fingerprint != automation_protocol.fingerprint) {
152
153
  std.debug.print(
153
- "replay refused: the journal was recorded at automation protocol v{d} but this build speaks v{d} - re-record with this build\n",
154
- .{ 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 },
155
156
  );
156
157
  return error.ReplayProtocolMismatch;
157
158
  }
@@ -210,6 +211,37 @@ pub fn replaySession(
210
211
  // codec, in the completion entry, and in
211
212
  // `EffectImageResult`, so every downstream cast
212
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
+ }
213
245
  if (effect.kind == .image and imageDimsDamaged(effect)) {
214
246
  std.debug.print(
215
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",
@@ -217,6 +249,75 @@ pub fn replaySession(
217
249
  );
218
250
  return error.ReplayDamagedRecord;
219
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
+ }
220
321
  if (effectRegeneratesUnderReplay(effect)) {
221
322
  report.effects_skipped += 1;
222
323
  continue;
@@ -230,8 +331,18 @@ pub fn replaySession(
230
331
  // executor's slot buffer).
231
332
  var blob_scratch: ?[]u8 = null;
232
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| {
336
+ std.debug.print(
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) },
339
+ );
340
+ return error.ReplayMissingBlob;
341
+ };
342
+ effect.payload = bytes;
343
+ }
233
344
  if (effect.kind == .image and effect.image_blob_len > 0) {
234
- const bytes = resolveBlob(effect, options.blobs, &blob_scratch) catch |err| {
345
+ const bytes = resolveBlob(effect.image_blob_hash, effect.image_blob_len, options.blobs, &blob_scratch) catch |err| {
235
346
  std.debug.print(
236
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",
237
348
  .{ report.events_replayed, effect.key, session_blobs.hexName(effect.image_blob_hash), effect.image_blob_len, @errorName(err) },
@@ -310,6 +421,24 @@ pub fn replaySession(
310
421
  .end => {},
311
422
  }
312
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
+ };
313
442
  return report;
314
443
  }
315
444
 
@@ -318,18 +447,19 @@ pub fn replaySession(
318
447
  /// record: present, exact length, and hashing to its address — the
319
448
  /// same hostile-input honesty the journal reader keeps.
320
449
  fn resolveBlob(
321
- record: journal.EffectResultRecord,
450
+ blob_hash: [runtime_effects.effect_image_blob_hash_len]u8,
451
+ blob_len_field: u64,
322
452
  blobs: ?session_blobs.SessionBlobSource,
323
453
  scratch_out: *?[]u8,
324
454
  ) anyerror![]const u8 {
325
455
  const blob_source = blobs orelse return error.BlobMissing;
326
- if (record.image_blob_len > session_blobs.max_blob_bytes) return error.BlobOverBudget;
327
- const blob_len: usize = @intCast(record.image_blob_len);
456
+ if (blob_len_field > session_blobs.max_blob_bytes) return error.BlobOverBudget;
457
+ const blob_len: usize = @intCast(blob_len_field);
328
458
  // One spare byte proves the stored blob is not LONGER than the
329
459
  // record claims (the source reads at most the buffer).
330
460
  const scratch = try std.heap.page_allocator.alloc(u8, blob_len + 1);
331
461
  scratch_out.* = scratch;
332
- const bytes = try blob_source.read_fn(blob_source.context, record.image_blob_hash, scratch);
462
+ const bytes = try blob_source.read_fn(blob_source.context, blob_hash, scratch);
333
463
  if (bytes.len != blob_len) return error.BlobCorrupt;
334
464
  return bytes;
335
465
  }
@@ -340,6 +470,170 @@ fn resolveBlob(
340
470
  /// overflow-checked, a hand-edited product that wraps u64 is damage,
341
471
  /// not a small image — fits `registerCanvasImage`'s per-image slot
342
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
+
343
637
  fn imageDimsDamaged(record: journal.EffectResultRecord) bool {
344
638
  if (record.image_outcome != .loaded) {
345
639
  return record.image_width != 0 or record.image_height != 0;
@@ -359,6 +653,13 @@ fn effectRegeneratesUnderReplay(record: journal.EffectResultRecord) bool {
359
653
  return switch (record.kind) {
360
654
  .timer => true,
361
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,
362
663
  .response => record.fetch_outcome == .rejected,
363
664
  .file => record.file_outcome == .rejected,
364
665
  .clipboard => record.clipboard_outcome == .rejected,
@@ -367,6 +668,16 @@ fn effectRegeneratesUnderReplay(record: journal.EffectResultRecord) bool {
367
668
  // position ticks, completions, platform failures — is an
368
669
  // external input and must be fed.
369
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,
370
681
  // Host-request rejections mark themselves with the exit reason
371
682
  // (the `.host` record encoding); host answers must be fed.
372
683
  .host => record.exit_reason == .rejected,
@@ -381,6 +692,18 @@ fn effectRegeneratesUnderReplay(record: journal.EffectResultRecord) bool {
381
692
  // those requests, so the journaled record is the ONLY terminal
382
693
  // and must be fed like every other worker truth.
383
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,
384
707
  // Launch-env deliveries are exactly what must NOT regenerate:
385
708
  // the recorded values feed the replayed envMsgs dispatch so the
386
709
  // replay launch's environment is never consulted.
@@ -417,6 +740,67 @@ fn renderScreenshotHash(runtime: *core.Runtime, view_label: []const u8, scale: f
417
740
  return std.hash.Wyhash.hash(0, writer.written());
418
741
  }
419
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
+
420
804
  /// Debug aid: `NATIVE_SDK_SESSION_REPLAY_DUMP=<dir>` writes each
421
805
  /// replay-rendered screenshot PNG so a mismatching pixel mark can be
422
806
  /// diffed against the recording's artifact.