@native-sdk/cli 0.4.4 → 0.5.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 (83) hide show
  1. package/build/app.zig +187 -6
  2. package/build/ts_run.mjs +63 -0
  3. package/build.zig +213 -1
  4. package/build.zig.zon +1 -0
  5. package/package.json +15 -10
  6. package/packages/core/package-lock.json +43 -0
  7. package/packages/core/package.json +37 -0
  8. package/packages/core/rt/rt.zig +1579 -0
  9. package/packages/core/sdk/bytes_text_methods.d.ts +88 -0
  10. package/packages/core/sdk/core.ts +694 -0
  11. package/packages/core/sdk/events.ts +137 -0
  12. package/packages/core/sdk/text.ts +519 -0
  13. package/packages/core/src/checker.ts +1869 -0
  14. package/packages/core/src/cli.ts +68 -0
  15. package/packages/core/src/devhost.ts +291 -0
  16. package/packages/core/src/devhost_resolver.mjs +22 -0
  17. package/packages/core/src/diagnostics.ts +417 -0
  18. package/packages/core/src/emitter.ts +8815 -0
  19. package/packages/core/src/infer.ts +1515 -0
  20. package/packages/core/src/modules.ts +173 -0
  21. package/packages/core/src/ownership.ts +740 -0
  22. package/packages/core/src/text_polyfill.ts +337 -0
  23. package/packages/core/src/text_tables.ts +408 -0
  24. package/packages/core/src/transpile.ts +146 -0
  25. package/packages/core/src/typed_ast.ts +536 -0
  26. package/packages/core/src/types.ts +815 -0
  27. package/skill-data/native-ui/SKILL.md +14 -0
  28. package/skill-data/ts-core/SKILL.md +493 -0
  29. package/skill-data/zig/SKILL.md +10 -0
  30. package/src/app_runner/root.zig +24 -0
  31. package/src/app_runner/ts_core_main.zig +231 -0
  32. package/src/primitives/app_manifest/comptime_scene.zig +341 -0
  33. package/src/primitives/app_manifest/root.zig +5 -0
  34. package/src/primitives/canvas/commands.zig +14 -7
  35. package/src/primitives/canvas/lazy_tls.zig +73 -0
  36. package/src/primitives/canvas/render_generic_resources.zig +22 -15
  37. package/src/primitives/canvas/root.zig +6 -0
  38. package/src/primitives/canvas/tests.zig +1 -0
  39. package/src/primitives/canvas/text_atlas.zig +40 -32
  40. package/src/primitives/canvas/text_layout_cache.zig +29 -22
  41. package/src/primitives/canvas/text_measure_cache.zig +56 -41
  42. package/src/primitives/canvas/text_spans.zig +39 -26
  43. package/src/primitives/canvas/tokens.zig +54 -0
  44. package/src/primitives/canvas/ui.zig +125 -0
  45. package/src/primitives/canvas/ui_markup.zig +22 -3
  46. package/src/primitives/canvas/ui_markup_compiled.zig +97 -7
  47. package/src/primitives/canvas/ui_markup_compiled_tests.zig +275 -0
  48. package/src/primitives/canvas/ui_markup_contract.zig +68 -14
  49. package/src/primitives/canvas/ui_markup_contract_tests.zig +257 -2
  50. package/src/primitives/canvas/ui_markup_reflect.zig +214 -0
  51. package/src/primitives/canvas/ui_markup_view.zig +105 -1
  52. package/src/primitives/canvas/ui_markup_view_tests.zig +336 -0
  53. package/src/primitives/canvas/ui_schema.zig +8 -0
  54. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  55. package/src/primitives/canvas/widget_invalidation.zig +14 -7
  56. package/src/primitives/canvas/widget_render.zig +25 -15
  57. package/src/primitives/canvas/widget_render_controls.zig +15 -9
  58. package/src/primitives/canvas/widget_runtime_tests.zig +44 -0
  59. package/src/root.zig +7 -0
  60. package/src/runtime/canvas_frame.zig +57 -36
  61. package/src/runtime/canvas_images.zig +9 -3
  62. package/src/runtime/canvas_widget_control_tests.zig +5 -6
  63. package/src/runtime/canvas_widget_runtime.zig +7 -4
  64. package/src/runtime/effects.zig +490 -2
  65. package/src/runtime/effects_host_tests.zig +594 -0
  66. package/src/runtime/root.zig +18 -0
  67. package/src/runtime/session_replay.zig +7 -1
  68. package/src/runtime/tests.zig +2 -0
  69. package/src/runtime/ts_core_host.zig +1505 -0
  70. package/src/runtime/ts_core_host_tests.zig +1408 -0
  71. package/src/runtime/ts_ui_app.zig +469 -0
  72. package/src/runtime/ui_app.zig +58 -0
  73. package/src/runtime/view_canvas.zig +24 -17
  74. package/src/runtime/view_widget_tree.zig +2 -2
  75. package/src/tooling/codesign.zig +212 -105
  76. package/src/tooling/doctor.zig +78 -0
  77. package/src/tooling/manifest.zig +47 -0
  78. package/src/tooling/package.zig +124 -12
  79. package/src/tooling/raw_manifest.zig +15 -0
  80. package/src/tooling/root.zig +1 -0
  81. package/src/tooling/templates.zig +683 -28
  82. package/src/tooling/ts_core.zig +486 -0
  83. package/src/tooling/verbs.zig +17 -0
@@ -157,6 +157,22 @@ pub const max_effect_file_bytes: usize = 1024 * 1024;
157
157
  /// must never pass for the whole one).
158
158
  pub const max_effect_clipboard_bytes: usize = platform.max_clipboard_data_bytes;
159
159
 
160
+ /// Maximum bytes of a host request's (or fire-and-forget host send's)
161
+ /// name. Mirrors the transpiled-core command wire format, whose name
162
+ /// field carries a one-byte length.
163
+ pub const max_effect_host_name_bytes: usize = 255;
164
+
165
+ /// Maximum bytes of a host request's outbound payload (mirrors
166
+ /// `max_effect_fetch_payload_bytes`: a command argument, not a bulk
167
+ /// transfer).
168
+ pub const max_effect_host_payload_bytes: usize = 64 * 1024;
169
+
170
+ /// Maximum bytes of a host request's result. Mirrors the fetch body
171
+ /// bound: the result lands in `update` as one bytes payload, sized for
172
+ /// API-shaped answers. An over-bound feed delivers the err route with a
173
+ /// teaching message, never a silent cut.
174
+ pub const max_effect_host_result_bytes: usize = 256 * 1024;
175
+
160
176
  /// The exit `code` reported for every non-`.exited` reason.
161
177
  pub const effect_error_exit_code: i32 = -1;
162
178
 
@@ -190,6 +206,29 @@ pub const WindowActionBinding = struct {
190
206
  minimize_fn: *const fn (context: *anyopaque, window_label: []const u8) bool,
191
207
  };
192
208
 
209
+ /// Type-erased handle to the embedding host's named-command services,
210
+ /// bound onto the effects channel (`bindHostCalls`). This is the seam
211
+ /// behind `hostRequest`/`hostSend` — the generic named host call a
212
+ /// transpiled app core's command channel (`host`/`host_bytes`/`request`
213
+ /// wire records) rides into the host. Both callbacks run on the loop
214
+ /// thread during dispatch; `name` and `payload` are valid only for the
215
+ /// duration of the call (copy what outlives it). A host answers a
216
+ /// request by calling `Effects.feedHostResult(key, ok, bytes)` on the
217
+ /// loop thread — synchronously from `request_fn`, or later from an
218
+ /// event the host marshals back.
219
+ pub const HostCallBinding = struct {
220
+ context: *anyopaque,
221
+ /// Fire-and-forget named host command: no key, no result, no Msg.
222
+ send_fn: *const fn (context: *anyopaque, name: []const u8, payload: []const u8) void,
223
+ /// Keyed routed host command: perform `name` and answer through
224
+ /// `feedHostResult` with this `key`.
225
+ request_fn: *const fn (context: *anyopaque, name: []const u8, key: u64, payload: []const u8) void,
226
+ /// Optional abort notice: the request with `key` was replaced or
227
+ /// cancelled — any late `feedHostResult` for it reports
228
+ /// `error.EffectNotFound` and delivers nothing.
229
+ cancel_fn: ?*const fn (context: *anyopaque, key: u64) void = null,
230
+ };
231
+
193
232
  /// Window-action label capacity (`Effects.closeWindow`/`minimizeWindow`):
194
233
  /// the mirror copies the last requested label so tests can pin it.
195
234
  pub const max_window_action_label = 64;
@@ -436,6 +475,26 @@ pub const EffectClipboardResult = struct {
436
475
  dropped_before: u32 = 0,
437
476
  };
438
477
 
478
+ /// Payload for `on_result` Msg constructors of host requests
479
+ /// (`hostRequest` — the generic named host call behind transpiled
480
+ /// cores' `request` wire records). Exactly one terminal per request,
481
+ /// routed by `ok`: true is the success route with the result bytes,
482
+ /// false the error route with the error bytes. Unlike the other keyed
483
+ /// effects there is no outcome enum and no cancelled terminal — the
484
+ /// request contract is REPLACE on key reuse and SILENT drop on cancel,
485
+ /// so the only deliveries are the host's answer and the loud rejection
486
+ /// (`ok = false`, bytes `"rejected"`) for a request the channel refused
487
+ /// (over-bound payload, key collision with another effect kind, no
488
+ /// slot, no bound host service).
489
+ pub const EffectHostResult = struct {
490
+ key: u64,
491
+ /// True routes the request's ok arm; false its err arm.
492
+ ok: bool = true,
493
+ /// Result (or error) bytes; drain scratch — copy what the model
494
+ /// keeps.
495
+ bytes: []const u8 = "",
496
+ };
497
+
439
498
  /// Maximum concurrently armed fx timers per app. Timers are their own
440
499
  /// fixed table: they do NOT consume `max_effects` slots or worker
441
500
  /// threads (a timer is a platform service arm, not a blocking effect),
@@ -579,6 +638,22 @@ pub const EffectResultKind = enum(u8) {
579
638
  timer = 6,
580
639
  clock = 7,
581
640
  audio = 8,
641
+ /// A host-request terminal (`EffectHostResult`). Additive over the
642
+ /// v3 journal layout — no new record fields: the ok/err route rides
643
+ /// `code` (0 ok, 1 err) and a channel-refused rejection is marked
644
+ /// by `exit_reason == .rejected` (rejections regenerate from the
645
+ /// same deterministic validation under replay and are never fed).
646
+ host = 9,
647
+ /// One launch-environment delivery (the TS adapter's `envMsgs`
648
+ /// channel): journaled as the adapter dispatches each launch value,
649
+ /// so replay feeds the RECORDED values instead of re-reading the
650
+ /// replay launch's environment. Additive over the v3 journal layout
651
+ /// — no new record fields: the value rides `payload`, the target
652
+ /// Msg arm name rides `stderr_tail`, and `key` is the dispatch
653
+ /// index. Journals without env records (older recordings, or
654
+ /// launches with no variables set) re-derive from the launch
655
+ /// configuration exactly as before.
656
+ env = 10,
582
657
  };
583
658
 
584
659
  /// Journaled wall-clock reads buffered for replay (`Effects.wallMs`).
@@ -586,6 +661,19 @@ pub const EffectResultKind = enum(u8) {
586
661
  /// is a runaway loop, not a session shape.
587
662
  pub const max_effect_replay_clock_entries: usize = 64;
588
663
 
664
+ /// Journaled launch-env deliveries buffered for replay (the `.env`
665
+ /// record feed). Bounded like the clock queue: more launch variables
666
+ /// than this is a wiring bug, not a session shape.
667
+ pub const max_effect_replay_env_entries: usize = 32;
668
+
669
+ /// One queued launch-env delivery: the target Msg arm name and the
670
+ /// recorded value bytes (both reference the journal, which outlives the
671
+ /// replay).
672
+ pub const ReplayEnvEntry = struct {
673
+ msg: []const u8,
674
+ value: []const u8,
675
+ };
676
+
589
677
  /// One drained effect result, flattened for the session journal: the
590
678
  /// exact payload `update` received, Msg-type-erased so the recorder and
591
679
  /// replayer need no knowledge of the app's Msg union. Only the fields
@@ -732,6 +820,7 @@ pub fn Effects(comptime Msg: type) type {
732
820
  pub const ClipboardMsgFn = *const fn (result: EffectClipboardResult) Msg;
733
821
  pub const TimerMsgFn = *const fn (timer: EffectTimer) Msg;
734
822
  pub const AudioMsgFn = *const fn (event: EffectAudio) Msg;
823
+ pub const HostMsgFn = *const fn (result: EffectHostResult) Msg;
735
824
 
736
825
  /// Comptime Msg constructor for `on_line`, following
737
826
  /// `canvas.Ui(Msg).inputMsg`: `lineMsg(.agent_line)` builds
@@ -816,6 +905,18 @@ pub fn Effects(comptime Msg: type) type {
816
905
  }.make;
817
906
  }
818
907
 
908
+ /// Comptime Msg constructor for `on_result` of host requests:
909
+ /// `hostMsg(.store_answered)` builds
910
+ /// `Msg{ .store_answered = result }` — the variant's payload
911
+ /// type must be `native_sdk.EffectHostResult`.
912
+ pub fn hostMsg(comptime tag: std.meta.Tag(Msg)) HostMsgFn {
913
+ return struct {
914
+ fn make(result: EffectHostResult) Msg {
915
+ return @unionInit(Msg, @tagName(tag), result);
916
+ }
917
+ }.make;
918
+ }
919
+
819
920
  pub const SpawnOptions = struct {
820
921
  /// Caller-chosen identity, stored in the model. Must not
821
922
  /// collide with another still-running effect.
@@ -974,6 +1075,32 @@ pub fn Effects(comptime Msg: type) type {
974
1075
  text: []const u8 = "",
975
1076
  };
976
1077
 
1078
+ pub const HostRequestOptions = struct {
1079
+ /// Caller-chosen identity. Same key space and slots as
1080
+ /// spawn/fetch/file/clipboard, but with the request key
1081
+ /// discipline: issuing a key whose HOST request is still in
1082
+ /// flight REPLACES it (the old result is dropped, silently),
1083
+ /// while a collision with another effect kind rejects.
1084
+ key: u64,
1085
+ /// Host service name (what the host dispatches on), at most
1086
+ /// `max_effect_host_name_bytes`; copied into slot storage.
1087
+ name: []const u8,
1088
+ /// Outbound payload, at most
1089
+ /// `max_effect_host_payload_bytes`; copied into the slot's
1090
+ /// buffer at call time.
1091
+ payload: []const u8 = "",
1092
+ on_result: ?HostMsgFn = null,
1093
+ };
1094
+
1095
+ /// A recorded host request, exposed by the fake executor for
1096
+ /// test assertions. Slices point into slot storage and stay
1097
+ /// valid until the request retires.
1098
+ pub const HostRequest = struct {
1099
+ key: u64,
1100
+ name: []const u8,
1101
+ payload: []const u8 = "",
1102
+ };
1103
+
977
1104
  pub const StartTimerOptions = struct {
978
1105
  /// Caller-chosen identity, stored in the model. Timer keys
979
1106
  /// are their own namespace: they never collide with (and are
@@ -1139,9 +1266,9 @@ pub fn Effects(comptime Msg: type) type {
1139
1266
  /// thereby retires) it.
1140
1267
  const SlotState = enum(u8) { idle, running, done, draining };
1141
1268
 
1142
- const SlotKind = enum(u8) { spawn, fetch, file, clipboard };
1269
+ const SlotKind = enum(u8) { spawn, fetch, file, clipboard, host };
1143
1270
 
1144
- const EntryKind = enum(u8) { line, exit, response, file, clipboard };
1271
+ const EntryKind = enum(u8) { line, exit, response, file, clipboard, host };
1145
1272
 
1146
1273
  const Entry = struct {
1147
1274
  kind: EntryKind = .line,
@@ -1171,6 +1298,11 @@ pub fn Effects(comptime Msg: type) type {
1171
1298
  /// `line_len`.
1172
1299
  clipboard_op: EffectClipboardOp = .write,
1173
1300
  clipboard_outcome: EffectClipboardOutcome = .ok,
1301
+ /// `.host` entries: which route the result takes (true = the
1302
+ /// ok arm). The bytes stay in the slot's heap buffer (taken
1303
+ /// at drain, like a fetch body) with their length in
1304
+ /// `line_len`.
1305
+ host_ok: bool = true,
1174
1306
  /// `.exit` entries of `.collect` spawns: the collected stdout
1175
1307
  /// stays in the slot's heap buffer (taken at drain, like a
1176
1308
  /// fetch body); the stderr tail rides in `line_bytes` with
@@ -1184,6 +1316,7 @@ pub fn Effects(comptime Msg: type) type {
1184
1316
  response_fn: ?ResponseMsgFn = null,
1185
1317
  file_fn: ?FileMsgFn = null,
1186
1318
  clipboard_fn: ?ClipboardMsgFn = null,
1319
+ host_fn: ?HostMsgFn = null,
1187
1320
  /// `.line` entries whose payload exceeds the inline buffer
1188
1321
  /// (a raised `max_line_bytes` bound): the bytes ride in this
1189
1322
  /// heap allocation instead of `line_bytes`. Owned by the
@@ -1202,6 +1335,11 @@ pub fn Effects(comptime Msg: type) type {
1202
1335
  file: struct { result: EffectFileResult, file_fn: ?FileMsgFn },
1203
1336
  clipboard: struct { result: EffectClipboardResult, clipboard_fn: ?ClipboardMsgFn },
1204
1337
  timer: struct { timer: EffectTimer, timer_fn: ?TimerMsgFn },
1338
+ /// `.host` terminals produced on the loop thread: rejections
1339
+ /// (`rejected = true`, static bytes, regenerated under
1340
+ /// replay) and feed fallbacks. Bytes here are never a host
1341
+ /// answer's — those ride the queue with their slot buffer.
1342
+ host: struct { result: EffectHostResult, host_fn: ?HostMsgFn, rejected: bool },
1205
1343
  /// `resolve`: a fed audio event (fake executor / replay)
1206
1344
  /// whose key and handler come from the live channel at
1207
1345
  /// delivery time — exactly how a platform event resolves in
@@ -1221,6 +1359,9 @@ pub fn Effects(comptime Msg: type) type {
1221
1359
  // EffectAudio carries no drop counter either; the
1222
1360
  // next position tick supersedes a lost one.
1223
1361
  .audio => {},
1362
+ // EffectHostResult carries none: its terminals are
1363
+ // one-per-request by construction.
1364
+ .host => {},
1224
1365
  }
1225
1366
  }
1226
1367
 
@@ -1232,6 +1373,7 @@ pub fn Effects(comptime Msg: type) type {
1232
1373
  .clipboard => |entry| entry.result.dropped_before,
1233
1374
  .timer => 0,
1234
1375
  .audio => 0,
1376
+ .host => 0,
1235
1377
  };
1236
1378
  }
1237
1379
  };
@@ -1247,6 +1389,7 @@ pub fn Effects(comptime Msg: type) type {
1247
1389
  on_response: ?ResponseMsgFn = null,
1248
1390
  on_file: ?FileMsgFn = null,
1249
1391
  on_clipboard: ?ClipboardMsgFn = null,
1392
+ on_host: ?HostMsgFn = null,
1250
1393
  /// Set by `cancel` before any kill attempt; read by the
1251
1394
  /// worker so a cancel that lands before the process spawns
1252
1395
  /// still kills it.
@@ -1357,6 +1500,12 @@ pub fn Effects(comptime Msg: type) type {
1357
1500
  fn filePath(slot: *const Slot) []const u8 {
1358
1501
  return slot.url_storage[0..slot.url_len];
1359
1502
  }
1503
+
1504
+ /// A host request's service name (they share the fetch URL
1505
+ /// storage — a slot is one occupancy at a time).
1506
+ fn hostName(slot: *const Slot) []const u8 {
1507
+ return slot.url_storage[0..slot.url_len];
1508
+ }
1360
1509
  };
1361
1510
 
1362
1511
  allocator: std.mem.Allocator,
@@ -1386,6 +1535,14 @@ pub fn Effects(comptime Msg: type) type {
1386
1535
  /// divergence signal (the replayed update read the clock more
1387
1536
  /// often than the recorded one). Reported loudly once.
1388
1537
  replay_clock_underflows: u64 = 0,
1538
+ /// Journaled launch-env deliveries queued for the replay-mode
1539
+ /// envMsgs dispatch (FIFO; fed from `.env` records before the
1540
+ /// installing event dispatches). Empty under replay means the
1541
+ /// journal carried none — the adapter re-derives from the
1542
+ /// launch configuration, the pre-`.env`-record behavior.
1543
+ replay_env: [max_effect_replay_env_entries]ReplayEnvEntry = undefined,
1544
+ replay_env_head: usize = 0,
1545
+ replay_env_len: usize = 0,
1389
1546
  /// Set once from the loop thread before the first dispatch;
1390
1547
  /// workers call `services.wake()` through it (the one
1391
1548
  /// thread-safe PlatformServices entry).
@@ -1398,6 +1555,12 @@ pub fn Effects(comptime Msg: type) type {
1398
1555
  /// by `UiApp` alongside the services — the seam behind
1399
1556
  /// app-drawn window controls (loop-thread only).
1400
1557
  window_actions: ?WindowActionBinding = null,
1558
+ /// The embedding host's named-command services (`hostSend` /
1559
+ /// `hostRequest`), bound by whoever hosts a transpiled app core
1560
+ /// (loop-thread only). Null means no host services: sends drop,
1561
+ /// requests reject loudly in real mode, and the fake executor
1562
+ /// parks requests for `feedHostResult` regardless.
1563
+ host_calls: ?HostCallBinding = null,
1401
1564
  /// Window-action mirror: counts and the last requested label,
1402
1565
  /// observable in tests (`windowActionState`).
1403
1566
  window_action_state: WindowActionState = .{},
@@ -1482,6 +1645,16 @@ pub fn Effects(comptime Msg: type) type {
1482
1645
  if (self.services) |services| services.audioStop() catch {};
1483
1646
  }
1484
1647
  self.audio = .{};
1648
+ // Host requests park on the loop thread with no worker to
1649
+ // wait for: retire them here so the worker wait below never
1650
+ // stalls on one, and any late host answer reports
1651
+ // EffectNotFound instead of touching a freed buffer.
1652
+ for (&self.slots) |*slot| {
1653
+ if (slot.kind == .host and slot.state.load(.acquire) == .running) {
1654
+ self.releaseFetchSlot(slot);
1655
+ slot.generation = 0;
1656
+ }
1657
+ }
1485
1658
  for (&self.slots) |*slot| {
1486
1659
  if (slot.state.load(.acquire) == .running and !slot.fake) {
1487
1660
  slot.cancel_requested.store(true, .release);
@@ -1545,6 +1718,9 @@ pub fn Effects(comptime Msg: type) type {
1545
1718
  // transport command inert through its existing no-services
1546
1719
  // paths instead of dereferencing freed memory.
1547
1720
  self.services = null;
1721
+ // Sever the host-call binding for the same reason: its
1722
+ // context belongs to the embedding host.
1723
+ self.host_calls = null;
1548
1724
  }
1549
1725
 
1550
1726
  /// Discard every queued completion, freeing heap line payloads
@@ -1603,6 +1779,13 @@ pub fn Effects(comptime Msg: type) type {
1603
1779
  if (self.window_actions == null) self.window_actions = binding;
1604
1780
  }
1605
1781
 
1782
+ /// Point named host commands at the embedding host's services
1783
+ /// (see `HostCallBinding`). Loop-thread only; the first bind
1784
+ /// sticks.
1785
+ pub fn bindHostCalls(self: *Self, binding: HostCallBinding) void {
1786
+ if (self.host_calls == null) self.host_calls = binding;
1787
+ }
1788
+
1606
1789
  /// Switch this channel into session-replay mode: the fake
1607
1790
  /// executor (no processes, no network, no file or pasteboard
1608
1791
  /// I/O — requests park in their slots) with journaled results as
@@ -1660,6 +1843,36 @@ pub fn Effects(comptime Msg: type) type {
1660
1843
  self.replay_clock_len += 1;
1661
1844
  }
1662
1845
 
1846
+ /// Journal one launch-env delivery (an `.env` record): the TS
1847
+ /// adapter reports each `envMsgs` value as it dispatches it, so
1848
+ /// replay carries the recorded values instead of re-reading the
1849
+ /// environment. No-op when no session is being recorded.
1850
+ pub fn journalEnvValue(self: *Self, index: u64, msg: []const u8, value: []const u8) void {
1851
+ self.journalNote(.{ .kind = .env, .key = index, .payload = value, .stderr_tail = msg });
1852
+ }
1853
+
1854
+ /// Queue one journaled launch-env delivery for the replay-mode
1855
+ /// envMsgs dispatch (the `.env` record feed). The slices must
1856
+ /// outlive the replay (journal bytes do).
1857
+ pub fn pushReplayEnv(self: *Self, msg: []const u8, value: []const u8) error{EffectNotFound}!void {
1858
+ if (self.replay_env_len >= max_effect_replay_env_entries) return error.EffectNotFound;
1859
+ const index = (self.replay_env_head + self.replay_env_len) % max_effect_replay_env_entries;
1860
+ self.replay_env[index] = .{ .msg = msg, .value = value };
1861
+ self.replay_env_len += 1;
1862
+ }
1863
+
1864
+ /// Pop the next journaled launch-env delivery (FIFO), or null
1865
+ /// when none remain. An empty queue at the first pop means the
1866
+ /// journal carried no `.env` records — the caller re-derives
1867
+ /// from the launch configuration (older recordings).
1868
+ pub fn takeReplayEnv(self: *Self) ?ReplayEnvEntry {
1869
+ if (self.replay_env_len == 0) return null;
1870
+ const entry = self.replay_env[self.replay_env_head];
1871
+ self.replay_env_head = (self.replay_env_head + 1) % max_effect_replay_env_entries;
1872
+ self.replay_env_len -= 1;
1873
+ return entry;
1874
+ }
1875
+
1663
1876
  // ------------------------------------------------------------- API
1664
1877
 
1665
1878
  /// Register (or replace) decoded straight-alpha RGBA8 pixels
@@ -2110,6 +2323,157 @@ pub fn Effects(comptime Msg: type) type {
2110
2323
  self.wakeHost();
2111
2324
  }
2112
2325
 
2326
+ /// Fire-and-forget named host command: hand `name` + `payload`
2327
+ /// to the bound host services and move on — no key, no result,
2328
+ /// no Msg. This is the delivery path of a transpiled core's
2329
+ /// `host`/`host_bytes` wire records. A channel without bound
2330
+ /// host services drops the send (there is no result route to
2331
+ /// reject into — the honest degrade, documented rather than
2332
+ /// silent-by-accident); session replay never calls the host
2333
+ /// (the send's observable consequences replay through whatever
2334
+ /// results it caused, which ARE journaled).
2335
+ pub fn hostSend(self: *Self, name: []const u8, payload: []const u8) void {
2336
+ if (self.replay) return;
2337
+ if (name.len == 0 or name.len > max_effect_host_name_bytes) return;
2338
+ const binding = self.host_calls orelse return;
2339
+ binding.send_fn(binding.context, name, payload);
2340
+ }
2341
+
2342
+ /// A keyed, routed host command — the generic named host call
2343
+ /// behind a transpiled core's `request` wire records: the host
2344
+ /// performs `name` with `payload` and answers with exactly one
2345
+ /// `feedHostResult(key, ok, bytes)`, delivered as one
2346
+ /// `on_result` Msg (`EffectHostResult`) on the next drain. Key
2347
+ /// discipline differs from spawn/fetch/file/clipboard on
2348
+ /// purpose (the wire contract): issuing a key whose host
2349
+ /// request is still in flight REPLACES it — the old request's
2350
+ /// result is dropped, silently — and `cancelHostRequest` drops
2351
+ /// one without dispatching anything. Rejection is never silent:
2352
+ /// an over-bound name/payload, a key colliding with another
2353
+ /// effect kind, a full slot table, or a real-mode channel with
2354
+ /// no bound host services delivers exactly one err-route Msg
2355
+ /// (`ok = false`, bytes `"rejected"`). Under the fake executor
2356
+ /// the request parks in its slot (inspect with `pendingHostAt`,
2357
+ /// answer with `feedHostResult`).
2358
+ pub fn hostRequest(self: *Self, options: HostRequestOptions) void {
2359
+ self.reclaimSlots();
2360
+ const fake = self.executor == .fake;
2361
+ if (options.name.len == 0 or options.name.len > max_effect_host_name_bytes or
2362
+ options.payload.len > max_effect_host_payload_bytes)
2363
+ {
2364
+ return self.rejectHost(options.key, options.on_result);
2365
+ }
2366
+ if (!fake and self.host_calls == null) return self.rejectHost(options.key, options.on_result);
2367
+ const slot_index = blk: {
2368
+ // In flight = running (no answer yet) OR draining with
2369
+ // an undelivered answer: both are replaced, dropping
2370
+ // the old result silently (its queued entry dies by
2371
+ // generation mismatch at drain). A running occupancy of
2372
+ // another kind is a key collision and rejects; a
2373
+ // draining one is already terminal — its key is free.
2374
+ var replaced: ?usize = null;
2375
+ for (&self.slots, 0..) |*slot, index| {
2376
+ const state = slot.state.load(.acquire);
2377
+ if (state != .running and state != .draining) continue;
2378
+ if (slot.key != options.key) continue;
2379
+ if (slot.kind != .host) {
2380
+ if (state == .running) return self.rejectHost(options.key, options.on_result);
2381
+ continue;
2382
+ }
2383
+ // Tell the host first: a late answer for the old
2384
+ // occupancy must find nothing.
2385
+ if (state == .running and !slot.fake) self.notifyHostCancel(options.key);
2386
+ self.releaseFetchSlot(slot);
2387
+ slot.generation = 0;
2388
+ replaced = index;
2389
+ }
2390
+ break :blk replaced orelse
2391
+ (self.findIdleSlot() orelse return self.rejectHost(options.key, options.on_result));
2392
+ };
2393
+
2394
+ const slot = &self.slots[slot_index];
2395
+ // The buffer holds the payload copy, then the result space.
2396
+ const buffer = self.allocator.alloc(u8, options.payload.len + max_effect_host_result_bytes) catch {
2397
+ return self.rejectHost(options.key, options.on_result);
2398
+ };
2399
+ slot.generation = self.next_generation;
2400
+ self.next_generation +%= 1;
2401
+ if (self.next_generation == 0) self.next_generation = 1;
2402
+ slot.key = options.key;
2403
+ slot.kind = .host;
2404
+ slot.on_line = null;
2405
+ slot.on_exit = null;
2406
+ slot.on_response = null;
2407
+ slot.on_file = null;
2408
+ slot.on_clipboard = null;
2409
+ slot.on_host = options.on_result;
2410
+ slot.cancel_requested.store(false, .release);
2411
+ // `cancelled_generation` stays sticky, exactly as in `spawn`.
2412
+ slot.child_id = null;
2413
+ slot.reaping = false;
2414
+ slot.dropped_pending = 0;
2415
+ slot.dropped_total = 0;
2416
+ @memcpy(slot.url_storage[0..options.name.len], options.name);
2417
+ slot.url_len = options.name.len;
2418
+ if (slot.line_buffer) |old| {
2419
+ self.allocator.free(old);
2420
+ slot.line_buffer = null;
2421
+ }
2422
+ if (slot.fetch_buffer) |old| self.allocator.free(old);
2423
+ slot.fetch_buffer = buffer;
2424
+ @memcpy(buffer[0..options.payload.len], options.payload);
2425
+ slot.payload_len = options.payload.len;
2426
+ slot.body_len = 0;
2427
+ slot.fake = fake;
2428
+ slot.state.store(.running, .release);
2429
+
2430
+ // Fake mode (tests and session replay) parks here: the feed
2431
+ // is the only terminal source.
2432
+ if (fake) return;
2433
+ const binding = self.host_calls.?;
2434
+ binding.request_fn(binding.context, slot.hostName(), options.key, slot.fetchPayload());
2435
+ }
2436
+
2437
+ /// Drop the in-flight host request with `key` without
2438
+ /// dispatching either route — the wire contract's `cancel`
2439
+ /// record. Silent by design (unlike `cancel`, which delivers a
2440
+ /// `.cancelled` terminal): the request's own issuer asked for
2441
+ /// the drop, so there is nothing to report. Also drops a result
2442
+ /// already completed but not yet drained. Unknown keys (and
2443
+ /// keys naming a non-host effect) are a no-op.
2444
+ pub fn cancelHostRequest(self: *Self, key: u64) void {
2445
+ for (&self.slots) |*slot| {
2446
+ const state = slot.state.load(.acquire);
2447
+ if (state != .running and state != .draining) continue;
2448
+ if (slot.kind != .host or slot.key != key) continue;
2449
+ if (state == .running and !slot.fake) self.notifyHostCancel(key);
2450
+ self.releaseFetchSlot(slot);
2451
+ // A queued result entry (fed, undrained) dies by
2452
+ // generation mismatch; zero marks "no occupancy".
2453
+ slot.generation = 0;
2454
+ }
2455
+ }
2456
+
2457
+ /// Tell the bound host services a request key is dead so the
2458
+ /// host can abort work; best effort, real executor only.
2459
+ fn notifyHostCancel(self: *Self, key: u64) void {
2460
+ const binding = self.host_calls orelse return;
2461
+ const cancel_fn = binding.cancel_fn orelse return;
2462
+ cancel_fn(binding.context, key);
2463
+ }
2464
+
2465
+ fn rejectHost(self: *Self, key: u64, host_fn: ?HostMsgFn) void {
2466
+ self.deliverLoopHost(.{ .key = key, .ok = false, .bytes = "rejected" }, host_fn, true);
2467
+ }
2468
+
2469
+ /// Queue a host terminal produced on the loop thread
2470
+ /// (rejections and feed fallbacks) for the next drain. Bytes
2471
+ /// here are always static.
2472
+ fn deliverLoopHost(self: *Self, result: EffectHostResult, host_fn: ?HostMsgFn, rejected: bool) void {
2473
+ if (host_fn == null) return;
2474
+ self.deliverPending(.{ .host = .{ .result = result, .host_fn = host_fn, .rejected = rejected } });
2475
+ }
2476
+
2113
2477
  /// Cancel a running effect by key. After this returns, no
2114
2478
  /// further `on_line` Msgs for that spawn are dispatched; one
2115
2479
  /// `on_exit` Msg with reason `.cancelled` follows once the
@@ -2117,6 +2481,8 @@ pub fn Effects(comptime Msg: type) type {
2117
2481
  /// (worker finished, completions still queued) keeps the same
2118
2482
  /// promise: queued lines are discarded and the queued exit is
2119
2483
  /// reported as `.cancelled`. Unknown keys are a no-op.
2484
+ /// Host-request slots keep their own contract instead: a cancel
2485
+ /// aimed at one drops it silently (`cancelHostRequest`).
2120
2486
  pub fn cancel(self: *Self, key: u64) void {
2121
2487
  const slot_index = self.findActiveSlot(key) orelse {
2122
2488
  // The worker may have finished with its exit still in
@@ -2129,6 +2495,7 @@ pub fn Effects(comptime Msg: type) type {
2129
2495
  return;
2130
2496
  };
2131
2497
  const slot = &self.slots[slot_index];
2498
+ if (slot.kind == .host) return self.cancelHostRequest(key);
2132
2499
  slot.cancelled_generation = slot.generation;
2133
2500
  slot.cancel_requested.store(true, .release);
2134
2501
  if (slot.fake) {
@@ -2638,6 +3005,20 @@ pub fn Effects(comptime Msg: type) type {
2638
3005
  });
2639
3006
  return timer_fn(entry.timer);
2640
3007
  },
3008
+ .host => |entry| {
3009
+ const host_fn = entry.host_fn orelse continue;
3010
+ self.journalNote(.{
3011
+ .kind = .host,
3012
+ .key = entry.result.key,
3013
+ .payload = entry.result.bytes,
3014
+ // `.host` journal encoding (no new record
3015
+ // fields): the route rides `code`, and
3016
+ // rejections are marked regenerable.
3017
+ .code = @intFromBool(!entry.result.ok),
3018
+ .exit_reason = if (entry.rejected) .rejected else .exited,
3019
+ });
3020
+ return host_fn(entry.result);
3021
+ },
2641
3022
  .audio => |entry| {
2642
3023
  var event = entry.event;
2643
3024
  var audio_fn = entry.audio_fn;
@@ -2865,6 +3246,41 @@ pub fn Effects(comptime Msg: type) type {
2865
3246
  });
2866
3247
  return clipboard_fn(result);
2867
3248
  },
3249
+ .host => {
3250
+ // One terminal per host occupancy, mirroring
3251
+ // `.response`: a mismatched generation means the
3252
+ // occupant was already retired (replaced or
3253
+ // cancelled — its result drops silently, per the
3254
+ // request contract).
3255
+ if (entry.generation != slot.generation) continue;
3256
+ // Take buffer ownership so the slot can be
3257
+ // reused while `update` still reads the bytes.
3258
+ if (self.drain_fetch_body) |old| self.allocator.free(old);
3259
+ self.drain_fetch_body = slot.fetch_buffer;
3260
+ slot.fetch_buffer = null;
3261
+ const payload_len = slot.payload_len;
3262
+ // A cancel that raced the feed (the entry was
3263
+ // already queued) still drops silently.
3264
+ if (cancelled) continue;
3265
+ const host_fn = entry.host_fn orelse continue;
3266
+ const bytes: []const u8 = if (self.drain_fetch_body) |buffer|
3267
+ buffer[payload_len .. payload_len + entry.line_len]
3268
+ else
3269
+ "";
3270
+ const result: EffectHostResult = .{
3271
+ .key = entry.key,
3272
+ .ok = entry.host_ok,
3273
+ .bytes = bytes,
3274
+ };
3275
+ self.journalNote(.{
3276
+ .kind = .host,
3277
+ .key = result.key,
3278
+ .payload = result.bytes,
3279
+ // `.host` journal encoding: route in `code`.
3280
+ .code = @intFromBool(!result.ok),
3281
+ });
3282
+ return host_fn(result);
3283
+ },
2868
3284
  }
2869
3285
  }
2870
3286
  }
@@ -3241,6 +3657,78 @@ pub fn Effects(comptime Msg: type) type {
3241
3657
  self.wakeHost();
3242
3658
  }
3243
3659
 
3660
+ /// Number of parked (still-active) fake host requests.
3661
+ pub fn pendingHostCount(self: *Self) usize {
3662
+ var count: usize = 0;
3663
+ for (&self.slots) |*slot| {
3664
+ if (slot.fake and slot.kind == .host and slot.state.load(.acquire) == .running) count += 1;
3665
+ }
3666
+ return count;
3667
+ }
3668
+
3669
+ /// The `index`-th parked fake host request (slot order).
3670
+ pub fn pendingHostAt(self: *Self, index: usize) ?HostRequest {
3671
+ var seen: usize = 0;
3672
+ for (&self.slots) |*slot| {
3673
+ if (!(slot.fake and slot.kind == .host and slot.state.load(.acquire) == .running)) continue;
3674
+ if (seen == index) {
3675
+ return .{
3676
+ .key = slot.key,
3677
+ .name = slot.hostName(),
3678
+ .payload = slot.fetchPayload(),
3679
+ };
3680
+ }
3681
+ seen += 1;
3682
+ }
3683
+ return null;
3684
+ }
3685
+
3686
+ /// The host's (or the test's, or session replay's) answer to
3687
+ /// the in-flight host request with `key`, retiring its slot:
3688
+ /// one terminal Msg through `on_result`, `ok` choosing the
3689
+ /// route and `bytes` riding along. Works for fake-parked AND
3690
+ /// real-bound requests — this is the completion path
3691
+ /// `HostCallBinding.request_fn` answers through. A result over
3692
+ /// `max_effect_host_result_bytes` delivers the err route with a
3693
+ /// teaching message (an opaque payload must never pass for the
3694
+ /// whole one); if the completion queue is somehow full, the
3695
+ /// terminal still lands through the pending ring as an err —
3696
+ /// never a silent ok with lost bytes.
3697
+ pub fn feedHostResult(self: *Self, key: u64, ok: bool, bytes: []const u8) error{EffectNotFound}!void {
3698
+ const slot_index = blk: {
3699
+ const index = self.findActiveSlot(key) orelse return error.EffectNotFound;
3700
+ if (self.slots[index].kind != .host) return error.EffectNotFound;
3701
+ break :blk index;
3702
+ };
3703
+ const slot = &self.slots[slot_index];
3704
+ const buffer = slot.fetch_buffer orelse return error.EffectNotFound;
3705
+ const capacity = buffer.len - slot.payload_len;
3706
+ var delivered_ok = ok;
3707
+ var delivered: []const u8 = bytes;
3708
+ if (bytes.len > capacity) {
3709
+ delivered_ok = false;
3710
+ delivered = "host result over budget";
3711
+ }
3712
+ @memcpy(buffer[slot.payload_len..][0..delivered.len], delivered);
3713
+ slot.body_len = delivered.len;
3714
+ var entry: Entry = .{
3715
+ .kind = .host,
3716
+ .slot_index = @intCast(slot_index),
3717
+ .generation = slot.generation,
3718
+ .key = slot.key,
3719
+ .line_len = @intCast(delivered.len),
3720
+ .host_ok = delivered_ok,
3721
+ .host_fn = slot.on_host,
3722
+ };
3723
+ slot.state.store(.draining, .release);
3724
+ if (!self.enqueue(&entry)) {
3725
+ const host_fn = slot.on_host;
3726
+ self.releaseFetchSlot(slot);
3727
+ self.deliverLoopHost(.{ .key = entry.key, .ok = false }, host_fn, false);
3728
+ }
3729
+ self.wakeHost();
3730
+ }
3731
+
3244
3732
  /// Number of recorded (still-armed) fake fx timers.
3245
3733
  pub fn pendingTimerCount(self: *Self) usize {
3246
3734
  var count: usize = 0;