@native-sdk/cli 0.8.2 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/build/app.zig +50 -3
  2. package/build.zig +134 -360
  3. package/native-sdk.d.ts +4 -0
  4. package/package.json +9 -9
  5. package/packages/core/compile-surface/core.ts +90 -1
  6. package/packages/core/package-lock.json +2 -2
  7. package/packages/core/package.json +1 -1
  8. package/packages/core/sdk/core.d.ts +65 -2
  9. package/packages/core/sdk/core.ts +189 -33
  10. package/packages/core/sdk/events.d.ts +14 -0
  11. package/packages/core/sdk/events.ts +18 -0
  12. package/packages/core/src/checker.ts +61 -1
  13. package/packages/core/src/devhost.ts +3 -3
  14. package/packages/core/src/diagnostics.ts +4 -4
  15. package/skill-data/core/SKILL.md +4 -2
  16. package/skill-data/core/references/project-anatomy.md +2 -1
  17. package/skill-data/core/references/web-engines-packaging-debugging.md +3 -1
  18. package/skill-data/native-ui/SKILL.md +8 -8
  19. package/skill-data/ts-core/SKILL.md +16 -13
  20. package/src/app_runner/app_assets.zig +138 -0
  21. package/src/app_runner/root.zig +2 -0
  22. package/src/app_runner/ts_core_main.zig +27 -3
  23. package/src/embed/host.zig +6 -0
  24. package/src/embed/ui_host.zig +2 -0
  25. package/src/platform/linux/root.zig +1 -0
  26. package/src/platform/macos/appkit_host.h +10 -0
  27. package/src/platform/macos/appkit_host.m +515 -93
  28. package/src/platform/macos/capture_info_plist.c +25 -0
  29. package/src/platform/macos/cef_host.mm +22 -0
  30. package/src/platform/macos/root.zig +68 -0
  31. package/src/platform/null_platform.zig +58 -0
  32. package/src/platform/root.zig +7 -0
  33. package/src/platform/types.zig +100 -0
  34. package/src/platform/windows/root.zig +56 -0
  35. package/src/platform/windows/webview2_host.cpp +171 -0
  36. package/src/primitives/app_manifest/types.zig +2 -0
  37. package/src/primitives/canvas/code_tests.zig +9 -9
  38. package/src/primitives/canvas/events.zig +12 -11
  39. package/src/primitives/canvas/frame.zig +44 -9
  40. package/src/primitives/canvas/icons/mic.svg +1 -0
  41. package/src/primitives/canvas/icons.zig +2 -0
  42. package/src/primitives/canvas/root.zig +8 -0
  43. package/src/primitives/canvas/text_span_tests.zig +39 -0
  44. package/src/primitives/canvas/ui.zig +23 -4
  45. package/src/primitives/canvas/ui_markup.zig +15 -0
  46. package/src/primitives/canvas/ui_markup_compiled.zig +9 -0
  47. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1 -1
  48. package/src/primitives/canvas/ui_markup_tests.zig +5 -0
  49. package/src/primitives/canvas/ui_markup_view.zig +9 -0
  50. package/src/primitives/canvas/ui_markup_view_tests.zig +9 -6
  51. package/src/primitives/canvas/ui_schema.zig +19 -10
  52. package/src/primitives/canvas/ui_schema_tests.zig +5 -3
  53. package/src/primitives/canvas/ui_tests.zig +18 -4
  54. package/src/primitives/canvas/widget_invalidation.zig +9 -2
  55. package/src/primitives/canvas/widget_render.zig +321 -115
  56. package/src/primitives/canvas/widget_runtime_tests.zig +173 -0
  57. package/src/primitives/canvas/widget_text_input.zig +10 -10
  58. package/src/primitives/canvas/widget_tree.zig +1 -1
  59. package/src/primitives/canvas/widgets.zig +41 -5
  60. package/src/root.zig +10 -0
  61. package/src/runtime/automation_widget_dispatch.zig +1 -0
  62. package/src/runtime/bridge_payload.zig +2 -0
  63. package/src/runtime/canvas_frame.zig +13 -2
  64. package/src/runtime/canvas_frame_invalidation_tests.zig +35 -10
  65. package/src/runtime/canvas_widget_anchored_tests.zig +83 -0
  66. package/src/runtime/canvas_widget_event_tests.zig +1 -0
  67. package/src/runtime/canvas_widget_events.zig +69 -24
  68. package/src/runtime/canvas_widget_runtime.zig +101 -6
  69. package/src/runtime/canvas_widget_scroll_driver_tests.zig +47 -4
  70. package/src/runtime/canvas_widget_scroll_drivers.zig +3 -3
  71. package/src/runtime/canvas_widget_semantics_tests.zig +2 -0
  72. package/src/runtime/canvas_widget_split_tree_tests.zig +228 -0
  73. package/src/runtime/canvas_widget_state.zig +10 -1
  74. package/src/runtime/canvas_widget_text_tests.zig +112 -3
  75. package/src/runtime/effects.zig +313 -2
  76. package/src/runtime/effects_audio_capture_tests.zig +251 -0
  77. package/src/runtime/root.zig +3 -0
  78. package/src/runtime/tests.zig +1 -0
  79. package/src/runtime/ts_core_host.zig +401 -32
  80. package/src/runtime/ts_core_host_tests.zig +407 -0
  81. package/src/runtime/ts_ui_app.zig +39 -1
  82. package/src/runtime/ui_app.zig +18 -3
  83. package/src/runtime/ui_app_tests.zig +48 -0
  84. package/src/runtime/view.zig +2 -0
  85. package/src/runtime/view_widget_scroll.zig +56 -5
  86. package/src/runtime/view_widget_text.zig +24 -5
  87. package/src/runtime/view_widget_tree.zig +1 -1
  88. package/src/tooling/manifest.zig +1034 -0
  89. package/src/tooling/package.zig +841 -10
  90. package/src/tooling/raw_manifest.zig +35 -0
  91. package/src/tooling/templates.zig +55 -0
  92. package/tools/corewire/emit_facade.zig +36 -0
@@ -73,6 +73,15 @@
73
73
  //! ("truncated") rather than passing a silently cut
74
74
  //! body as ok. Wire timeout 0 means the engine
75
75
  //! default.
76
+ //! fetch_stream -> `fx.fetch` (`.stream`) through the STREAM table;
77
+ //! every complete response line routes the line arm,
78
+ //! and the ONE terminal retires the entry: `.ok`
79
+ //! routes the HTTP status through the one-number ok
80
+ //! arm, every other outcome routes the err arm with
81
+ //! its name. Wire timeout 0 and max-line 0 select the
82
+ //! engine defaults. `cancel` is loud for a live fetch
83
+ //! stream: it ends with err "cancelled", with no lines
84
+ //! after the cancel.
76
85
  //! clip_write -> `fx.writeClipboard` fire-and-forget (`on_result`
77
86
  //! null): a refused or over-bound write is dropped by
78
87
  //! design — there is no route to report on. Engine
@@ -357,6 +366,7 @@
357
366
 
358
367
  const std = @import("std");
359
368
  const runtime_effects = @import("effects.zig");
369
+ const platform = @import("../platform/root.zig");
360
370
 
361
371
  /// Engine-key namespace for bridge-issued host requests: `base + table
362
372
  /// index`. High bits spell "TSRQ" so a bridge key is recognizable in
@@ -512,7 +522,7 @@ pub fn TsCoreHost(comptime core: type) type {
512
522
  }
513
523
  };
514
524
 
515
- /// One live spawn STREAM — the non-retiring entry kind: line
525
+ /// One live spawn or fetch STREAM — the non-retiring entry kind: line
516
526
  /// results route through it repeatedly across dispatches, and
517
527
  /// only the exit terminal (or the engine's `.cancelled` end
518
528
  /// after a wire cancel) retires it. The table index IS the
@@ -522,11 +532,17 @@ pub fn TsCoreHost(comptime core: type) type {
522
532
  used: bool = false,
523
533
  key_len: usize = 0,
524
534
  key: [max_wire_key_bytes]u8 = undefined,
525
- /// `spawn_no_line_tag` = lines dispatch nothing.
535
+ /// `spawn_no_line_tag` = lines dispatch nothing (spawn only;
536
+ /// fetch streams always carry a line route).
526
537
  line_tag: u8 = spawn_no_line_tag,
527
538
  exit_tag: u8 = 0,
528
539
  err_tag: u8 = 0,
529
540
  collect: bool = false,
541
+ /// Fetch lines are data records, so a cut or dropped line makes
542
+ /// the eventual success terminal unusable. Spawn line mode keeps
543
+ /// its existing best-effort contract.
544
+ fetch: bool = false,
545
+ damaged: bool = false,
530
546
 
531
547
  fn wireKey(entry: *const StreamEntry) []const u8 {
532
548
  return entry.key[0..entry.key_len];
@@ -591,6 +607,20 @@ pub fn TsCoreHost(comptime core: type) type {
591
607
  event_tag: u8 = 0,
592
608
  };
593
609
 
610
+ /// One microphone/system capture stream. Capture uses the engine's
611
+ /// channel transport internally but keeps a distinct bridge table so
612
+ /// terminal events retain the requested source and output format.
613
+ /// Replacing a source may briefly leave its old entry closing while
614
+ /// the new key is already live, so this mirrors channel capacity.
615
+ const AudioCaptureEntry = struct {
616
+ used: bool = false,
617
+ key: u64 = 0,
618
+ event_tag: u8 = 0,
619
+ source: platform.AudioCaptureSource = .microphone,
620
+ sample_rate: u32 = 0,
621
+ channels: u8 = 0,
622
+ };
623
+
594
624
  /// One live pty session — the stream entries' non-retiring
595
625
  /// shape: "output" events route through it repeatedly across
596
626
  /// dispatches, and only the one "exit" terminal retires it. The
@@ -627,6 +657,7 @@ pub fn TsCoreHost(comptime core: type) type {
627
657
  var video_entry: VideoEntry = .{};
628
658
  var images: [runtime_effects.max_effects]ImageEntry = @splat(.{});
629
659
  var channels: [runtime_effects.max_effect_channels]ChannelEntry = @splat(.{});
660
+ var audio_captures: [runtime_effects.max_effect_channels]AudioCaptureEntry = @splat(.{});
630
661
  var ptys: [runtime_effects.max_effect_ptys]PtyEntry = @splat(.{});
631
662
  /// The platform caches directory for URL image sources, the
632
663
  /// audio cache dir's twin (`setImageCacheDir` / `TsUiApp`'s
@@ -701,6 +732,7 @@ pub fn TsCoreHost(comptime core: type) type {
701
732
  video_entry = .{};
702
733
  images = @splat(.{});
703
734
  channels = @splat(.{});
735
+ audio_captures = @splat(.{});
704
736
  ptys = @splat(.{});
705
737
  clip_write_counter = 0;
706
738
  audio_cache_dir_len = 0;
@@ -949,17 +981,27 @@ pub fn TsCoreHost(comptime core: type) type {
949
981
  headers[i] = .{ .name = name, .value = value };
950
982
  }
951
983
  const body = takeLongBytes(cmd, &at);
952
- fx.fetch(.{
953
- .key = effect_key_base + allocEffectEntry(fx, head),
954
- .method = method,
955
- .url = url,
956
- .headers = headers[0..header_count],
957
- .body = if (body.len > 0) body else null,
958
- // Wire 0 = "the engine's default" — the record
959
- // never bakes the default in.
960
- .timeout_ms = if (timeout_ms == 0) runtime_effects.default_effect_fetch_timeout_ms else timeout_ms,
961
- .on_response = fetchResultMsg,
962
- });
984
+ // Both Cmd.fetch overloads occupy one public key
985
+ // space. A buffered fetch must not start beside a
986
+ // live line stream under the same key: cancel would
987
+ // otherwise find this named op first and leave the
988
+ // stream running. The live stream owns the key, so
989
+ // reject the newcomer through its own err arm.
990
+ if (head.key.len > 0 and findStream(head.key) != null) {
991
+ fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "rejected"));
992
+ } else {
993
+ fx.fetch(.{
994
+ .key = effect_key_base + allocEffectEntry(fx, head),
995
+ .method = method,
996
+ .url = url,
997
+ .headers = headers[0..header_count],
998
+ .body = if (body.len > 0) body else null,
999
+ // Wire 0 = "the engine's default" — the record
1000
+ // never bakes the default in.
1001
+ .timeout_ms = if (timeout_ms == 0) runtime_effects.default_effect_fetch_timeout_ms else timeout_ms,
1002
+ .on_response = fetchResultMsg,
1003
+ });
1004
+ }
963
1005
  },
964
1006
  // clip_write [op][bytes_len u32 LE][bytes]
965
1007
  0x0A => {
@@ -1233,6 +1275,68 @@ pub fn TsCoreHost(comptime core: type) type {
1233
1275
  .body = body,
1234
1276
  });
1235
1277
  },
1278
+ // audio_capture_start [op][key f64 LE][source u8]
1279
+ // [sample_rate u32 LE][channels u8]
1280
+ // [event_tag u8]
1281
+ 0x1E => {
1282
+ const key_bits = takeBytes(cmd, &at, 8);
1283
+ const key_value: f64 = @bitCast(std.mem.readInt(u64, key_bits[0..8], .little));
1284
+ const source_wire = takeByte(cmd, &at);
1285
+ const source = std.enums.fromInt(platform.AudioCaptureSource, source_wire) orelse
1286
+ @panic("ts core host: unknown audio capture source wire value - the core and this runtime disagree on cmd_format_version");
1287
+ const sample_rate = std.mem.readInt(u32, takeBytes(cmd, &at, 4)[0..4], .little);
1288
+ const capture_channels = takeByte(cmd, &at);
1289
+ const event_tag = takeByte(cmd, &at);
1290
+ issueAudioCaptureStart(fx, key_value, source, sample_rate, capture_channels, event_tag);
1291
+ },
1292
+ // audio_capture_stop [op][key f64 LE]
1293
+ 0x1F => {
1294
+ const key_bits = takeBytes(cmd, &at, 8);
1295
+ const key_value: f64 = @bitCast(std.mem.readInt(u64, key_bits[0..8], .little));
1296
+ runAudioCaptureStop(fx, key_value);
1297
+ },
1298
+ // fetch_stream [op][key_len][key][line][ok][err]
1299
+ // [method u8][timeout u32 LE]
1300
+ // [max_line_bytes u32 LE]
1301
+ // [url_len u32 LE][url][header_count u8]
1302
+ // ([name_len u8][name][value_len u32 LE][value])*
1303
+ // [body_len u32 LE][body]
1304
+ 0x20 => {
1305
+ const key = takeShortBytes(cmd, &at);
1306
+ const line_tag = takeByte(cmd, &at);
1307
+ const ok_tag = takeByte(cmd, &at);
1308
+ const err_tag = takeByte(cmd, &at);
1309
+ const method = fetchMethod(takeByte(cmd, &at));
1310
+ const timeout_bytes = takeBytes(cmd, &at, 4);
1311
+ const timeout_ms = std.mem.readInt(u32, timeout_bytes[0..4], .little);
1312
+ const max_line_bytes_wire = takeBytes(cmd, &at, 4);
1313
+ const max_line_bytes = std.mem.readInt(u32, max_line_bytes_wire[0..4], .little);
1314
+ const url = takeLongBytes(cmd, &at);
1315
+ const header_count: usize = takeByte(cmd, &at);
1316
+ if (header_count > runtime_effects.max_effect_fetch_headers) {
1317
+ @panic("ts core host: a streaming fetch wire record carries more headers than the engine accepts - the frontend's own bound should have stopped this build");
1318
+ }
1319
+ var headers: [runtime_effects.max_effect_fetch_headers]std.http.Header = undefined;
1320
+ for (0..header_count) |i| {
1321
+ const name = takeShortBytes(cmd, &at);
1322
+ const value = takeLongBytes(cmd, &at);
1323
+ headers[i] = .{ .name = name, .value = value };
1324
+ }
1325
+ const body = takeLongBytes(cmd, &at);
1326
+ issueFetchStream(fx, .{
1327
+ .key = key,
1328
+ .line_tag = line_tag,
1329
+ .exit_tag = ok_tag,
1330
+ .err_tag = err_tag,
1331
+ }, .{
1332
+ .method = method,
1333
+ .url = url,
1334
+ .headers = headers[0..header_count],
1335
+ .body = if (body.len > 0) body else null,
1336
+ .timeout_ms = if (timeout_ms == 0) runtime_effects.default_effect_fetch_timeout_ms else timeout_ms,
1337
+ .max_line_bytes = if (max_line_bytes == 0) runtime_effects.max_effect_line_bytes else max_line_bytes,
1338
+ });
1339
+ },
1236
1340
  else => @panic("ts core host: unknown command wire record - the core and this runtime disagree on cmd_format_version"),
1237
1341
  }
1238
1342
  }
@@ -1347,7 +1451,7 @@ pub fn TsCoreHost(comptime core: type) type {
1347
1451
  return null;
1348
1452
  }
1349
1453
 
1350
- // ------------------------------------------------ spawn streams
1454
+ // ------------------------------------------- spawn / fetch streams
1351
1455
 
1352
1456
  const SpawnHead = struct { key: []const u8, line_tag: u8, exit_tag: u8, err_tag: u8 };
1353
1457
 
@@ -1370,8 +1474,14 @@ pub fn TsCoreHost(comptime core: type) type {
1370
1474
  fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "rejected"));
1371
1475
  return;
1372
1476
  }
1373
- const index = freeStreamIndex() orelse
1374
- @panic("ts core host: more than 16 spawn streams in flight - the stream table mirrors the engine's max_effects slots");
1477
+ const index = freeStreamIndex() orelse {
1478
+ // Resource refusal is an ordinary stream terminal. The
1479
+ // engine would report the same rejection if it owned one
1480
+ // more routing slot; the bridge table must not turn that
1481
+ // public outcome into a process panic.
1482
+ fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "rejected"));
1483
+ return;
1484
+ };
1375
1485
  const entry = &streams[index];
1376
1486
  entry.used = true;
1377
1487
  entry.key_len = head.key.len;
@@ -1380,16 +1490,66 @@ pub fn TsCoreHost(comptime core: type) type {
1380
1490
  entry.exit_tag = head.exit_tag;
1381
1491
  entry.err_tag = head.err_tag;
1382
1492
  entry.collect = collect;
1493
+ entry.fetch = false;
1494
+ entry.damaged = false;
1383
1495
  fx.spawn(.{
1384
1496
  .key = spawn_key_base + index,
1385
1497
  .argv = argv,
1386
1498
  .stdin = if (stdin.len > 0) stdin else null,
1387
1499
  .output = if (collect) .collect else .lines,
1388
- .on_line = if (head.line_tag != spawn_no_line_tag) spawnLineMsg else null,
1500
+ .on_line = if (head.line_tag != spawn_no_line_tag) streamLineMsg else null,
1389
1501
  .on_exit = spawnExitMsg,
1390
1502
  });
1391
1503
  }
1392
1504
 
1505
+ const FetchStreamOptions = struct {
1506
+ method: std.http.Method,
1507
+ url: []const u8,
1508
+ headers: []const std.http.Header,
1509
+ body: ?[]const u8,
1510
+ timeout_ms: u32,
1511
+ max_line_bytes: usize,
1512
+ };
1513
+
1514
+ /// Open a line-streamed fetch in the shared stream table. Like a
1515
+ /// spawn, a duplicate live wire key is rejected rather than replaced:
1516
+ /// replacing a source that has already delivered lines would splice
1517
+ /// two HTTP responses into one app-owned stream.
1518
+ fn issueFetchStream(fx: *Fx, head: SpawnHead, options: FetchStreamOptions) void {
1519
+ if (head.key.len > 0 and
1520
+ (findStream(head.key) != null or findEffect(head.key) != null))
1521
+ {
1522
+ fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "rejected"));
1523
+ return;
1524
+ }
1525
+ const index = freeStreamIndex() orelse {
1526
+ fx.stageLoopMsg(msgFromTagStaticBytes(head.err_tag, "rejected"));
1527
+ return;
1528
+ };
1529
+ const entry = &streams[index];
1530
+ entry.used = true;
1531
+ entry.key_len = head.key.len;
1532
+ @memcpy(entry.key[0..head.key.len], head.key);
1533
+ entry.line_tag = head.line_tag;
1534
+ entry.exit_tag = head.exit_tag;
1535
+ entry.err_tag = head.err_tag;
1536
+ entry.collect = false;
1537
+ entry.fetch = true;
1538
+ entry.damaged = false;
1539
+ fx.fetch(.{
1540
+ .key = spawn_key_base + index,
1541
+ .method = options.method,
1542
+ .url = options.url,
1543
+ .headers = options.headers,
1544
+ .body = options.body,
1545
+ .timeout_ms = options.timeout_ms,
1546
+ .response = .stream,
1547
+ .on_line = streamLineMsg,
1548
+ .max_line_bytes = options.max_line_bytes,
1549
+ .on_response = fetchStreamResultMsg,
1550
+ });
1551
+ }
1552
+
1393
1553
  fn findStream(key: []const u8) ?usize {
1394
1554
  for (&streams, 0..) |*entry, index| {
1395
1555
  if (entry.used and std.mem.eql(u8, entry.wireKey(), key)) return index;
@@ -1404,26 +1564,30 @@ pub fn TsCoreHost(comptime core: type) type {
1404
1564
  return null;
1405
1565
  }
1406
1566
 
1407
- /// The stream entry an engine spawn result names — looked up
1567
+ /// The stream entry an engine spawn/fetch result names — looked up
1408
1568
  /// WITHOUT retiring (lines flow through it repeatedly; only the
1409
- /// exit terminal retires, in `spawnExitMsg`).
1569
+ /// terminal retires it).
1410
1570
  fn streamAt(key: u64) *StreamEntry {
1411
1571
  if (key < spawn_key_base) {
1412
- @panic("ts core host: a spawn result arrived outside the bridge's stream key namespace");
1572
+ @panic("ts core host: a stream result arrived outside the bridge's stream key namespace");
1413
1573
  }
1414
1574
  const index = key - spawn_key_base;
1415
1575
  if (index >= streams.len or !streams[index].used) {
1416
- @panic("ts core host: a spawn result arrived for a stream the bridge is not tracking");
1576
+ @panic("ts core host: a result arrived for a stream the bridge is not tracking");
1417
1577
  }
1418
1578
  return &streams[index];
1419
1579
  }
1420
1580
 
1421
- /// `LineMsgFn` for spawn streams: every stdout line routes the
1422
- /// entry's line arm with the line bytes; the entry stays live.
1423
- /// Only set when the wire carries a line arm, so this never
1424
- /// sees `spawn_no_line_tag`.
1425
- fn spawnLineMsg(line: runtime_effects.EffectLine) Msg {
1581
+ /// Shared `LineMsgFn` for spawn and fetch streams: every delivered
1582
+ /// source line routes the entry's line arm; the entry stays live.
1583
+ /// Fetch streams additionally remember any cut line or preceding
1584
+ /// queue loss so their terminal cannot later claim the response was
1585
+ /// complete.
1586
+ fn streamLineMsg(line: runtime_effects.EffectLine) Msg {
1426
1587
  const entry = streamAt(line.key);
1588
+ if (entry.fetch and (line.truncated or line.dropped_before != 0)) {
1589
+ entry.damaged = true;
1590
+ }
1427
1591
  return msgFromTagBytes(entry.line_tag, line.line);
1428
1592
  }
1429
1593
 
@@ -1449,6 +1613,23 @@ pub fn TsCoreHost(comptime core: type) type {
1449
1613
  return msgFromTagBytes(entry.err_tag, reason);
1450
1614
  }
1451
1615
 
1616
+ /// The streamed fetch's ONE terminal. A delivered, lossless response
1617
+ /// — any HTTP status, including non-2xx — routes the status through
1618
+ /// the ok arm. A cut/dropped line routes `truncated`; transport
1619
+ /// failure, timeout, rejection, and cancellation route their
1620
+ /// machine-readable outcome through err. The terminal retires the
1621
+ /// shared stream entry, so no later line can route for the key.
1622
+ fn fetchStreamResultMsg(response: runtime_effects.EffectResponse) Msg {
1623
+ const entry = streamAt(response.key);
1624
+ const damaged = entry.damaged or response.truncated or response.dropped_before != 0;
1625
+ entry.used = false;
1626
+ if (response.outcome == .ok) {
1627
+ if (damaged) return msgFromTagStaticBytes(entry.err_tag, "truncated");
1628
+ return msgFromTagNumber(entry.exit_tag, @floatFromInt(response.status));
1629
+ }
1630
+ return msgFromTagBytes(entry.err_tag, @tagName(response.outcome));
1631
+ }
1632
+
1452
1633
  // ------------------------------------------------- audio stream
1453
1634
 
1454
1635
  /// The audio_ctl record: drive the single playback channel,
@@ -1817,6 +1998,110 @@ pub fn TsCoreHost(comptime core: type) type {
1817
1998
  return msgFromTagChannel(entry.event_tag, event);
1818
1999
  }
1819
2000
 
2001
+ // --------------------------------------------- audio capture streams
2002
+
2003
+ fn issueAudioCaptureStart(
2004
+ fx: *Fx,
2005
+ key_value: f64,
2006
+ source: platform.AudioCaptureSource,
2007
+ sample_rate: u32,
2008
+ capture_channels: u8,
2009
+ event_tag: u8,
2010
+ ) void {
2011
+ const representable = std.math.isFinite(key_value) and
2012
+ key_value >= 1 and key_value < 9007199254740992.0 and
2013
+ @floor(key_value) == key_value;
2014
+ const format: platform.AudioCaptureFormat = .{
2015
+ .sample_rate = sample_rate,
2016
+ .channels = capture_channels,
2017
+ };
2018
+ if (!representable or !format.valid()) {
2019
+ fx.stageLoopMsg(msgFromTagAudioCapture(event_tag, .{
2020
+ .key = if (representable) @intFromFloat(key_value) else 0,
2021
+ .kind = .rejected,
2022
+ .source = source,
2023
+ .sample_rate = sample_rate,
2024
+ .channels = capture_channels,
2025
+ }));
2026
+ return;
2027
+ }
2028
+ const key: u64 = @intFromFloat(key_value);
2029
+ if (findAudioCapture(key) != null) {
2030
+ fx.stageLoopMsg(msgFromTagAudioCapture(event_tag, .{
2031
+ .key = key,
2032
+ .kind = .rejected,
2033
+ .source = source,
2034
+ .sample_rate = sample_rate,
2035
+ .channels = capture_channels,
2036
+ }));
2037
+ return;
2038
+ }
2039
+ const index = freeAudioCaptureIndex() orelse {
2040
+ fx.stageLoopMsg(msgFromTagAudioCapture(event_tag, .{
2041
+ .key = key,
2042
+ .kind = .rejected,
2043
+ .source = source,
2044
+ .sample_rate = sample_rate,
2045
+ .channels = capture_channels,
2046
+ }));
2047
+ return;
2048
+ };
2049
+ audio_captures[index] = .{
2050
+ .used = true,
2051
+ .key = key,
2052
+ .event_tag = event_tag,
2053
+ .source = source,
2054
+ .sample_rate = sample_rate,
2055
+ .channels = capture_channels,
2056
+ };
2057
+ fx.startAudioCapture(.{
2058
+ .key = key,
2059
+ .source = source,
2060
+ .sample_rate = sample_rate,
2061
+ .channels = capture_channels,
2062
+ .on_event = audioCaptureEventMsg,
2063
+ });
2064
+ }
2065
+
2066
+ fn findAudioCapture(key: u64) ?usize {
2067
+ for (&audio_captures, 0..) |*entry, index| {
2068
+ if (entry.used and entry.key == key) return index;
2069
+ }
2070
+ return null;
2071
+ }
2072
+
2073
+ fn freeAudioCaptureIndex() ?usize {
2074
+ for (&audio_captures, 0..) |*entry, index| {
2075
+ if (!entry.used) return index;
2076
+ }
2077
+ return null;
2078
+ }
2079
+
2080
+ fn runAudioCaptureStop(fx: *Fx, key_value: f64) void {
2081
+ const representable = std.math.isFinite(key_value) and
2082
+ key_value >= 1 and key_value < 9007199254740992.0 and
2083
+ @floor(key_value) == key_value;
2084
+ if (!representable) return;
2085
+ const key: u64 = @intFromFloat(key_value);
2086
+ if (findAudioCapture(key) == null) return;
2087
+ fx.stopAudioCapture(key);
2088
+ }
2089
+
2090
+ fn audioCaptureEventMsg(channel_event: runtime_effects.EffectChannelEvent) Msg {
2091
+ const index = findAudioCapture(channel_event.key) orelse
2092
+ @panic("ts core host: an audio capture event arrived with no open bridge entry");
2093
+ const entry = &audio_captures[index];
2094
+ var event = runtime_effects.decodeAudioCaptureChannelEvent(channel_event);
2095
+ // The channel's terminal envelope carries no capture packet;
2096
+ // restore the requested identity/format from the bridge entry.
2097
+ event.source = entry.source;
2098
+ if (event.sample_rate == 0) event.sample_rate = entry.sample_rate;
2099
+ if (event.channels == 0) event.channels = entry.channels;
2100
+ const event_tag = entry.event_tag;
2101
+ if (event.kind == .stopped or event.kind == .rejected) entry.used = false;
2102
+ return msgFromTagAudioCapture(event_tag, event);
2103
+ }
2104
+
1820
2105
  // -------------------------------------------------- pty sessions
1821
2106
 
1822
2107
  /// Open a pty session: claim a non-retiring entry (the spawn
@@ -1947,10 +2232,10 @@ pub fn TsCoreHost(comptime core: type) type {
1947
2232
  /// keyed tables — requests (silent drop), named engine ops
1948
2233
  /// (silent drop: the entry is marked dropped, the engine's
1949
2234
  /// `.cancelled` terminal retires it, and its Msg is swallowed —
1950
- /// no arm dispatches), spawn streams (the child dies and its
1951
- /// exit routes the err arm with "cancelled" — killing a process
1952
- /// IS an observable event, so spawn's cancel stays loud), then
1953
- /// delays (silent — a cancelled delay just never fires).
2235
+ /// no arm dispatches), spawn/fetch streams (their terminal routes
2236
+ /// the err arm with "cancelled" — ending a stream is observable,
2237
+ /// so stream cancellation stays loud), then delays (silent — a
2238
+ /// cancelled delay just never fires).
1954
2239
  /// Unknown keys are a no-op; the audio stream is not cancel's
1955
2240
  /// to end (audio_ctl `stop` closes it).
1956
2241
  fn cancelWireKey(fx: *Fx, key: []const u8) void {
@@ -1965,8 +2250,8 @@ pub fn TsCoreHost(comptime core: type) type {
1965
2250
  return;
1966
2251
  }
1967
2252
  if (findStream(key)) |index| {
1968
- // Same shape: the engine ends the child and the
1969
- // `.cancelled` exit retires the entry in spawnExitMsg.
2253
+ // The engine's `.cancelled` terminal retires the entry in
2254
+ // spawnExitMsg or fetchStreamResultMsg.
1970
2255
  fx.cancel(spawn_key_base + index);
1971
2256
  return;
1972
2257
  }
@@ -2625,6 +2910,90 @@ pub fn TsCoreHost(comptime core: type) type {
2625
2910
  @panic("ts core host: a channel event names a Msg tag outside the union");
2626
2911
  }
2627
2912
 
2913
+ fn audioCaptureArmShape(comptime T: type) bool {
2914
+ const info = @typeInfo(T);
2915
+ if (info != .@"struct") return false;
2916
+ const fields = info.@"struct".fields;
2917
+ if (fields.len != 10) return false;
2918
+ var ok = true;
2919
+ for (fields) |f| {
2920
+ if (std.mem.eql(u8, f.name, "state") or std.mem.eql(u8, f.name, "source")) {
2921
+ if (@typeInfo(f.type) != .@"enum") ok = false;
2922
+ } else if (std.mem.eql(u8, f.name, "pcm")) {
2923
+ if (f.type != []const u8) ok = false;
2924
+ } else if (std.mem.eql(u8, f.name, "key") or
2925
+ std.mem.eql(u8, f.name, "sampleRate") or
2926
+ std.mem.eql(u8, f.name, "channels") or
2927
+ std.mem.eql(u8, f.name, "timestampMs") or
2928
+ std.mem.eql(u8, f.name, "frames") or
2929
+ std.mem.eql(u8, f.name, "droppedPending") or
2930
+ std.mem.eql(u8, f.name, "droppedTotal"))
2931
+ {
2932
+ if (f.type != i64 and f.type != u64 and f.type != f64) ok = false;
2933
+ } else {
2934
+ ok = false;
2935
+ }
2936
+ }
2937
+ return ok;
2938
+ }
2939
+
2940
+ fn audioCaptureStateValue(comptime E: type, kind: runtime_effects.EffectAudioCaptureEventKind) E {
2941
+ const name = @tagName(kind);
2942
+ inline for (@typeInfo(E).@"enum".fields) |f| {
2943
+ if (std.mem.eql(u8, f.name, name)) return @enumFromInt(f.value);
2944
+ }
2945
+ @panic("ts core host: an audio capture event kind has no member in the event arm's state union - the frontend's own shape check should have stopped this build");
2946
+ }
2947
+
2948
+ fn audioCaptureSourceValue(comptime E: type, source: platform.AudioCaptureSource) E {
2949
+ const name = @tagName(source);
2950
+ inline for (@typeInfo(E).@"enum".fields) |f| {
2951
+ if (std.mem.eql(u8, f.name, name)) return @enumFromInt(f.value);
2952
+ }
2953
+ @panic("ts core host: an audio capture source has no member in the event arm's source union - the frontend's own shape check should have stopped this build");
2954
+ }
2955
+
2956
+ fn msgFromTagAudioCapture(tag: u8, event: runtime_effects.EffectAudioCaptureEvent) Msg {
2957
+ inline for (msg_arms, 0..) |arm, index| {
2958
+ if (tag == index) {
2959
+ if (comptime audioCaptureArmShape(arm.type)) {
2960
+ const fields = @typeInfo(arm.type).@"struct".fields;
2961
+ var payload: arm.type = undefined;
2962
+ inline for (fields) |f| {
2963
+ if (comptime std.mem.eql(u8, f.name, "state")) {
2964
+ @field(payload, f.name) = audioCaptureStateValue(f.type, event.kind);
2965
+ } else if (comptime std.mem.eql(u8, f.name, "source")) {
2966
+ @field(payload, f.name) = audioCaptureSourceValue(f.type, event.source);
2967
+ } else if (comptime std.mem.eql(u8, f.name, "key")) {
2968
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.key) else @intCast(event.key);
2969
+ } else if (comptime std.mem.eql(u8, f.name, "sampleRate")) {
2970
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.sample_rate) else @intCast(event.sample_rate);
2971
+ } else if (comptime std.mem.eql(u8, f.name, "channels")) {
2972
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.channels) else @intCast(event.channels);
2973
+ } else if (comptime std.mem.eql(u8, f.name, "timestampMs")) {
2974
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.timestamp_ms) else @intCast(event.timestamp_ms);
2975
+ } else if (comptime std.mem.eql(u8, f.name, "frames")) {
2976
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.frames) else @intCast(event.frames);
2977
+ } else if (comptime std.mem.eql(u8, f.name, "droppedPending")) {
2978
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.dropped_pending) else @intCast(event.dropped_pending);
2979
+ } else if (comptime std.mem.eql(u8, f.name, "droppedTotal")) {
2980
+ @field(payload, f.name) = if (comptime f.type == f64) @floatFromInt(event.dropped_total) else @intCast(event.dropped_total);
2981
+ } else if (event.pcm_s16le.len == 0) {
2982
+ @field(payload, f.name) = "";
2983
+ } else {
2984
+ const copy = core.rt.frameAlloc(u8, event.pcm_s16le.len);
2985
+ @memcpy(copy, event.pcm_s16le);
2986
+ @field(payload, f.name) = copy;
2987
+ }
2988
+ }
2989
+ return @unionInit(Msg, arm.name, payload);
2990
+ }
2991
+ @panic("ts core host: an audio capture event targets Msg arm '" ++ arm.name ++ "', which is not the ten-field audio capture event record");
2992
+ }
2993
+ }
2994
+ @panic("ts core host: an audio capture event names a Msg tag outside the union");
2995
+ }
2996
+
2628
2997
  /// The six-field pty event record, matched by field name —
2629
2998
  /// `channelArmShape`'s twin: `state` and `reason` (any enums;
2630
2999
  /// members matched by name at delivery), `bytes` (bytes),