@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
@@ -70,6 +70,8 @@ const mini_core = struct {
70
70
  /// The channel event states, shuffled off the engine order for the
71
71
  /// same by-NAME matching proof.
72
72
  pub const ChannelState = enum { rejected, data, closed };
73
+ pub const CaptureState = enum { data, stopped, started, rejected, failed };
74
+ pub const CaptureSource = enum { system, microphone };
73
75
 
74
76
  /// The image outcome states the mixed-rejection tests observe,
75
77
  /// shuffled off the engine order like the other two.
@@ -115,6 +117,18 @@ const mini_core = struct {
115
117
  chan_dropped_pending: f64,
116
118
  chan_dropped_total: f64,
117
119
  chan_events: i64,
120
+ // Native audio-capture stream mirrors.
121
+ capture_state: CaptureState,
122
+ capture_source: CaptureSource,
123
+ capture_key: f64,
124
+ capture_rate: f64,
125
+ capture_channels: f64,
126
+ capture_timestamp_ms: f64,
127
+ capture_frames: f64,
128
+ capture_pcm: []const u8,
129
+ capture_dropped_pending: f64,
130
+ capture_dropped_total: f64,
131
+ capture_events: i64,
118
132
  // Image event mirrors.
119
133
  img_state: ImageState,
120
134
  img_events: i64,
@@ -289,6 +303,36 @@ const mini_core = struct {
289
303
  uget, // 81: fetch "uget" -> ufetched/failed
290
304
  ufetched: struct { status: u64, body: []const u8 }, // 82: fetch ok
291
305
  // record with a u64-classed number field
306
+ start_capture, // 83: microphone capture key 91 -> capture_evt
307
+ stop_capture, // 84: stop capture key 91
308
+ capture_evt: struct { // 85: ten-field capture event arm
309
+ key: f64,
310
+ state: CaptureState,
311
+ source: CaptureSource,
312
+ sampleRate: f64,
313
+ channels: f64,
314
+ timestampMs: f64,
315
+ frames: f64,
316
+ pcm: []const u8,
317
+ droppedPending: f64,
318
+ droppedTotal: f64,
319
+ },
320
+ stream_get, // 86: streaming fetch "events" -> stream_line/stream_done/failed
321
+ stream_line: []const u8, // 87: one complete response line
322
+ stream_done: i64, // 88: terminal HTTP status
323
+ stop_stream, // 89: cancel "events" (loud -> failed "cancelled")
324
+ dup_stream, // 90: a second live "events" stream is rejected
325
+ stream_over_get, // 91: streaming fetch collides with buffered fetch "get"
326
+ get_over_stream, // 92: buffered fetch collides with streaming fetch "events"
327
+ fill_streams, // 93: seventeen distinct streams exceed the bridge table
328
+ };
329
+
330
+ const stream_fill_keys = [_][]const u8{
331
+ "fill-00", "fill-01", "fill-02", "fill-03",
332
+ "fill-04", "fill-05", "fill-06", "fill-07",
333
+ "fill-08", "fill-09", "fill-10", "fill-11",
334
+ "fill-12", "fill-13", "fill-14", "fill-15",
335
+ "fill-16",
292
336
  };
293
337
 
294
338
  pub const InitResult = struct { model: *const Model, cmd: []const u8 };
@@ -330,6 +374,17 @@ const mini_core = struct {
330
374
  .chan_dropped_pending = -1,
331
375
  .chan_dropped_total = -1,
332
376
  .chan_events = 0,
377
+ .capture_state = .stopped,
378
+ .capture_source = .microphone,
379
+ .capture_key = -1,
380
+ .capture_rate = 0,
381
+ .capture_channels = 0,
382
+ .capture_timestamp_ms = 0,
383
+ .capture_frames = 0,
384
+ .capture_pcm = "",
385
+ .capture_dropped_pending = 0,
386
+ .capture_dropped_total = 0,
387
+ .capture_events = 0,
333
388
  .img_state = .loaded,
334
389
  .img_events = 0,
335
390
  .pty_key = "",
@@ -457,6 +512,93 @@ const mini_core = struct {
457
512
  out.status = response.body;
458
513
  return .{ .model = out, .cmd = "" };
459
514
  },
515
+ .stream_get => {
516
+ const headers = [_]FetchHeader{.{ .name = "accept", .value = "text/event-stream" }};
517
+ return .{ .model = model, .cmd = cmdFetchStream(
518
+ "events",
519
+ @intFromEnum(@as(std.meta.Tag(Msg), .stream_line)),
520
+ @intFromEnum(@as(std.meta.Tag(Msg), .stream_done)),
521
+ @intFromEnum(@as(std.meta.Tag(Msg), .failed)),
522
+ 1,
523
+ 60_000,
524
+ 65_536,
525
+ "https://status.test/events",
526
+ &headers,
527
+ "ask",
528
+ ) };
529
+ },
530
+ .stream_line => |line| {
531
+ const out = frameCreate(model.*);
532
+ out.line_count = model.line_count + 1;
533
+ out.last_line = line;
534
+ return .{ .model = out, .cmd = "" };
535
+ },
536
+ .stream_done => |status| {
537
+ const out = frameCreate(model.*);
538
+ out.code = status;
539
+ return .{ .model = out, .cmd = "" };
540
+ },
541
+ .stop_stream => return .{ .model = model, .cmd = cmdCancel("events") },
542
+ .dup_stream => return .{ .model = model, .cmd = cmdFetchStream(
543
+ "events",
544
+ @intFromEnum(@as(std.meta.Tag(Msg), .stream_line)),
545
+ @intFromEnum(@as(std.meta.Tag(Msg), .stream_done)),
546
+ @intFromEnum(@as(std.meta.Tag(Msg), .failed)),
547
+ 0,
548
+ 0,
549
+ 0,
550
+ "https://status.test/other",
551
+ &.{},
552
+ "",
553
+ ) },
554
+ .stream_over_get => return .{ .model = model, .cmd = cmdFetchStream(
555
+ "get",
556
+ @intFromEnum(@as(std.meta.Tag(Msg), .stream_line)),
557
+ @intFromEnum(@as(std.meta.Tag(Msg), .stream_done)),
558
+ @intFromEnum(@as(std.meta.Tag(Msg), .failed)),
559
+ 0,
560
+ 0,
561
+ 0,
562
+ "https://status.test/collision",
563
+ &.{},
564
+ "",
565
+ ) },
566
+ .get_over_stream => return .{ .model = model, .cmd = cmdFetch(
567
+ "events",
568
+ @intFromEnum(@as(std.meta.Tag(Msg), .fetched)),
569
+ @intFromEnum(@as(std.meta.Tag(Msg), .failed)),
570
+ 0,
571
+ 0,
572
+ "https://status.test/collision",
573
+ &.{},
574
+ "",
575
+ ) },
576
+ .fill_streams => {
577
+ var commands: [stream_fill_keys.len][]const u8 = undefined;
578
+ var total: usize = 0;
579
+ for (&commands, stream_fill_keys) |*command, key| {
580
+ command.* = cmdFetchStream(
581
+ key,
582
+ @intFromEnum(@as(std.meta.Tag(Msg), .stream_line)),
583
+ @intFromEnum(@as(std.meta.Tag(Msg), .stream_done)),
584
+ @intFromEnum(@as(std.meta.Tag(Msg), .failed)),
585
+ 0,
586
+ 0,
587
+ 0,
588
+ "https://status.test/fill",
589
+ &.{},
590
+ "",
591
+ );
592
+ total += command.len;
593
+ }
594
+ const out = rt.frameAlloc(u8, total);
595
+ var off: usize = 0;
596
+ for (commands) |command| {
597
+ @memcpy(out[off..][0..command.len], command);
598
+ off += command.len;
599
+ }
600
+ return .{ .model = model, .cmd = out };
601
+ },
460
602
  .run_lines => return .{ .model = model, .cmd = cmdSpawn("job", 25, 26, 8, 0, &.{ "/bin/probe", "--fast" }, "feed me") },
461
603
  .run_quiet => return .{ .model = model, .cmd = cmdSpawn("job", 0xFF, 26, 8, 0, &.{"/bin/quiet"}, "") },
462
604
  .run_collect => return .{ .model = model, .cmd = cmdSpawn("job", 0xFF, 27, 8, 1, &.{ "/bin/ps", "-axo" }, "") },
@@ -640,6 +782,23 @@ const mini_core = struct {
640
782
  @memcpy(out[first.len..], second);
641
783
  return .{ .model = model, .cmd = out };
642
784
  },
785
+ .start_capture => return .{ .model = model, .cmd = cmdAudioCaptureStart(91, 0, 16_000, 1, @intFromEnum(@as(std.meta.Tag(Msg), .capture_evt))) },
786
+ .stop_capture => return .{ .model = model, .cmd = cmdAudioCaptureStop(91) },
787
+ .capture_evt => |event| {
788
+ const out = frameCreate(model.*);
789
+ out.capture_state = event.state;
790
+ out.capture_source = event.source;
791
+ out.capture_key = event.key;
792
+ out.capture_rate = event.sampleRate;
793
+ out.capture_channels = event.channels;
794
+ out.capture_timestamp_ms = event.timestampMs;
795
+ out.capture_frames = event.frames;
796
+ out.capture_pcm = event.pcm;
797
+ out.capture_dropped_pending = event.droppedPending;
798
+ out.capture_dropped_total = event.droppedTotal;
799
+ out.capture_events = model.capture_events + 1;
800
+ return .{ .model = out, .cmd = "" };
801
+ },
643
802
  }
644
803
  }
645
804
 
@@ -665,6 +824,7 @@ const mini_core = struct {
665
824
  out[0].output = commitBytes(next.output);
666
825
  out[0].bands = commitBytes(next.bands);
667
826
  out[0].chan_bytes = commitBytes(next.chan_bytes);
827
+ out[0].capture_pcm = commitBytes(next.capture_pcm);
668
828
  out[0].pty_bytes = commitBytes(next.pty_bytes);
669
829
  out[0].order = commitBytes(next.order);
670
830
  return &out[0];
@@ -785,6 +945,34 @@ const mini_core = struct {
785
945
  return out;
786
946
  }
787
947
 
948
+ fn cmdFetchStream(key: []const u8, line_tag: u8, ok_tag: u8, err_tag: u8, method: u8, timeout_ms: u32, max_line_bytes: u32, url: []const u8, headers: []const FetchHeader, body: []const u8) []const u8 {
949
+ var header_bytes: usize = 0;
950
+ for (headers) |h| header_bytes += 1 + h.name.len + 4 + h.value.len;
951
+ const out = rt.frameAlloc(u8, 2 + key.len + 3 + 1 + 4 + 4 + 4 + url.len + 1 + header_bytes + 4 + body.len);
952
+ out[0] = 0x20;
953
+ out[1] = @intCast(key.len);
954
+ @memcpy(out[2..][0..key.len], key);
955
+ var off: usize = 2 + key.len;
956
+ out[off] = line_tag;
957
+ out[off + 1] = ok_tag;
958
+ out[off + 2] = err_tag;
959
+ out[off + 3] = method;
960
+ std.mem.writeInt(u32, out[off + 4 ..][0..4], timeout_ms, .little);
961
+ std.mem.writeInt(u32, out[off + 8 ..][0..4], max_line_bytes, .little);
962
+ off += 12;
963
+ off = writeLongBytes(out, off, url);
964
+ out[off] = @intCast(headers.len);
965
+ off += 1;
966
+ for (headers) |h| {
967
+ out[off] = @intCast(h.name.len);
968
+ @memcpy(out[off + 1 ..][0..h.name.len], h.name);
969
+ off += 1 + h.name.len;
970
+ off = writeLongBytes(out, off, h.value);
971
+ }
972
+ _ = writeLongBytes(out, off, body);
973
+ return out;
974
+ }
975
+
788
976
  fn cmdClipWrite(bytes: []const u8) []const u8 {
789
977
  const out = rt.frameAlloc(u8, 1 + 4 + bytes.len);
790
978
  out[0] = 0x0A;
@@ -919,6 +1107,24 @@ const mini_core = struct {
919
1107
  return out;
920
1108
  }
921
1109
 
1110
+ fn cmdAudioCaptureStart(key: f64, source: u8, sample_rate: u32, channels: u8, event_tag: u8) []const u8 {
1111
+ const out = rt.frameAlloc(u8, 16);
1112
+ out[0] = 0x1E;
1113
+ std.mem.writeInt(u64, out[1..9], @bitCast(key), .little);
1114
+ out[9] = source;
1115
+ std.mem.writeInt(u32, out[10..14], sample_rate, .little);
1116
+ out[14] = channels;
1117
+ out[15] = event_tag;
1118
+ return out;
1119
+ }
1120
+
1121
+ fn cmdAudioCaptureStop(key: f64) []const u8 {
1122
+ const out = rt.frameAlloc(u8, 9);
1123
+ out[0] = 0x1F;
1124
+ std.mem.writeInt(u64, out[1..9], @bitCast(key), .little);
1125
+ return out;
1126
+ }
1127
+
922
1128
  fn cmdPtySpawn(key: []const u8, event_tag: u8, cols: f64, rows: f64, term: []const u8, argv: []const []const u8) []const u8 {
923
1129
  var argv_bytes: usize = 0;
924
1130
  for (argv) |arg| argv_bytes += 4 + arg.len;
@@ -1552,6 +1758,174 @@ test "cancel is silent for every named-op family - write_file, fetch, clip_read"
1552
1758
  try std.testing.expectEqualStrings("", Host.model().status);
1553
1759
  }
1554
1760
 
1761
+ // ------------------------------------------------------ fetch streams
1762
+
1763
+ const event_fetch_key: u64 = ts_core_host.spawn_key_base + 0;
1764
+
1765
+ test "a streaming fetch decodes whole, routes lines repeatedly, and terminates with the HTTP status" {
1766
+ const fx = freshChannel();
1767
+ defer fx.deinit();
1768
+ Host.init(fx);
1769
+
1770
+ Host.dispatch(fx, .stream_get);
1771
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingFetchCount());
1772
+ const request = fx.pendingFetchAt(0).?;
1773
+ try std.testing.expectEqual(event_fetch_key, request.key);
1774
+ try std.testing.expectEqual(std.http.Method.POST, request.method);
1775
+ try std.testing.expectEqual(effects_mod.FetchResponseMode.stream, request.response);
1776
+ try std.testing.expectEqual(@as(usize, 65_536), request.max_line_bytes);
1777
+ try std.testing.expectEqualStrings("https://status.test/events", request.url);
1778
+ try std.testing.expectEqual(@as(usize, 1), request.headers.len);
1779
+ try std.testing.expectEqualStrings("accept", request.headers[0].name);
1780
+ try std.testing.expectEqualStrings("text/event-stream", request.headers[0].value);
1781
+ try std.testing.expectEqualStrings("ask", request.body);
1782
+
1783
+ try fx.feedLine(event_fetch_key, "data: one");
1784
+ Host.drain(fx);
1785
+ try std.testing.expectEqual(@as(i64, 1), Host.model().line_count);
1786
+ try std.testing.expectEqualStrings("data: one", Host.model().last_line);
1787
+
1788
+ try fx.feedLine(event_fetch_key, "data: two");
1789
+ try fx.feedLine(event_fetch_key, "");
1790
+ Host.drain(fx);
1791
+ try std.testing.expectEqual(@as(i64, 3), Host.model().line_count);
1792
+ try std.testing.expectEqualStrings("", Host.model().last_line);
1793
+
1794
+ // A non-2xx status is still a delivered response and therefore the
1795
+ // successful terminal. Stream terminals carry no body.
1796
+ try fx.feedResponse(event_fetch_key, 429, "ignored");
1797
+ Host.drain(fx);
1798
+ try std.testing.expectEqual(@as(i64, 429), Host.model().code);
1799
+ try std.testing.expectError(error.EffectNotFound, fx.feedLine(event_fetch_key, "late"));
1800
+ try std.testing.expectEqual(@as(i64, 0), Host.model().errs);
1801
+ }
1802
+
1803
+ test "streaming fetch failures and cancellation are loud terminals" {
1804
+ const fx = freshChannel();
1805
+ defer fx.deinit();
1806
+ Host.init(fx);
1807
+
1808
+ Host.dispatch(fx, .stream_get);
1809
+ try fx.feedResponseOutcome(event_fetch_key, .timed_out, 0, "");
1810
+ Host.drain(fx);
1811
+ try std.testing.expectEqual(@as(i64, 1), Host.model().errs);
1812
+ try std.testing.expectEqualStrings("timed_out", Host.model().last_err);
1813
+
1814
+ Host.dispatch(fx, .stream_get);
1815
+ try fx.feedLine(event_fetch_key, "queued before cancel");
1816
+ Host.dispatch(fx, .stop_stream);
1817
+ Host.drain(fx);
1818
+ try std.testing.expectEqual(@as(i64, 0), Host.model().line_count);
1819
+ try std.testing.expectEqual(@as(i64, 2), Host.model().errs);
1820
+ try std.testing.expectEqualStrings("cancelled", Host.model().last_err);
1821
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingFetchCount());
1822
+ }
1823
+
1824
+ test "a lossy streaming fetch terminates as truncated instead of success" {
1825
+ const fx = freshChannel();
1826
+ defer fx.deinit();
1827
+ Host.init(fx);
1828
+
1829
+ // A later delivered line reports earlier queue loss. The line still
1830
+ // routes, but even a normal HTTP terminal cannot certify the response as
1831
+ // complete afterward.
1832
+ Host.dispatch(fx, .stream_get);
1833
+ try fx.feedLineWithMetadata(event_fetch_key, "data: [DONE]", false, 2);
1834
+ Host.drain(fx);
1835
+ try std.testing.expectEqual(@as(i64, 1), Host.model().line_count);
1836
+ try fx.feedResponse(event_fetch_key, 200, "");
1837
+ Host.drain(fx);
1838
+ try std.testing.expectEqual(@as(i64, 1), Host.model().errs);
1839
+ try std.testing.expectEqualStrings("truncated", Host.model().last_err);
1840
+ try std.testing.expectEqual(@as(i64, -1), Host.model().code);
1841
+
1842
+ // Loss with no later line rides the response terminal itself. Cover both
1843
+ // terminal metadata fields: either one must suppress the ok arm.
1844
+ Host.dispatch(fx, .stream_get);
1845
+ try fx.feedResponseOutcomeWithMetadata(event_fetch_key, .ok, 204, "", true, 0);
1846
+ Host.drain(fx);
1847
+ try std.testing.expectEqual(@as(i64, 2), Host.model().errs);
1848
+ try std.testing.expectEqualStrings("truncated", Host.model().last_err);
1849
+ try std.testing.expectEqual(@as(i64, -1), Host.model().code);
1850
+
1851
+ Host.dispatch(fx, .stream_get);
1852
+ try fx.feedResponseOutcomeWithMetadata(event_fetch_key, .ok, 206, "", false, 3);
1853
+ Host.drain(fx);
1854
+ try std.testing.expectEqual(@as(i64, 3), Host.model().errs);
1855
+ try std.testing.expectEqualStrings("truncated", Host.model().last_err);
1856
+ try std.testing.expectEqual(@as(i64, -1), Host.model().code);
1857
+ }
1858
+
1859
+ test "a duplicate live streaming fetch key is rejected without replacing the stream" {
1860
+ const fx = freshChannel();
1861
+ defer fx.deinit();
1862
+ Host.init(fx);
1863
+
1864
+ Host.dispatch(fx, .stream_get);
1865
+ Host.dispatch(fx, .dup_stream);
1866
+ Host.drain(fx);
1867
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingFetchCount());
1868
+ try std.testing.expectEqual(@as(i64, 1), Host.model().errs);
1869
+ try std.testing.expectEqualStrings("rejected", Host.model().last_err);
1870
+
1871
+ try fx.feedLine(event_fetch_key, "original still live");
1872
+ try fx.feedResponse(event_fetch_key, 204, "");
1873
+ Host.drain(fx);
1874
+ try std.testing.expectEqual(@as(i64, 1), Host.model().line_count);
1875
+ try std.testing.expectEqualStrings("original still live", Host.model().last_line);
1876
+ try std.testing.expectEqual(@as(i64, 204), Host.model().code);
1877
+ }
1878
+
1879
+ test "buffered and streaming fetch modes cannot share a live public key" {
1880
+ const fx = freshChannel();
1881
+ defer fx.deinit();
1882
+ Host.init(fx);
1883
+
1884
+ // A buffered fetch owns "get", so a streaming fetch cannot make
1885
+ // cancel ambiguous by claiming the same public key beside it.
1886
+ Host.dispatch(fx, .get);
1887
+ Host.dispatch(fx, .stream_over_get);
1888
+ Host.drain(fx);
1889
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingFetchCount());
1890
+ try std.testing.expectEqual(@as(i64, 1), Host.model().errs);
1891
+ try std.testing.expectEqualStrings("rejected", Host.model().last_err);
1892
+
1893
+ Host.dispatch(fx, .drop_get);
1894
+ Host.drain(fx);
1895
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingFetchCount());
1896
+
1897
+ // The same shared namespace applies in the opposite order. The
1898
+ // rejected buffered fetch must not hide the live stream from cancel.
1899
+ Host.dispatch(fx, .stream_get);
1900
+ Host.dispatch(fx, .get_over_stream);
1901
+ Host.drain(fx);
1902
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingFetchCount());
1903
+ try std.testing.expectEqual(@as(i64, 2), Host.model().errs);
1904
+ try std.testing.expectEqualStrings("rejected", Host.model().last_err);
1905
+
1906
+ Host.dispatch(fx, .stop_stream);
1907
+ Host.drain(fx);
1908
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingFetchCount());
1909
+ try std.testing.expectEqual(@as(i64, 3), Host.model().errs);
1910
+ try std.testing.expectEqualStrings("cancelled", Host.model().last_err);
1911
+ }
1912
+
1913
+ test "a seventeenth live stream is rejected instead of panicking" {
1914
+ const fx = freshChannel();
1915
+ defer fx.deinit();
1916
+ Host.init(fx);
1917
+ // Retire init's host request so all sixteen shared engine effect
1918
+ // slots are available to the streams this test is isolating.
1919
+ try fx.feedHostResult(boot_request_key, true, "ready");
1920
+ Host.drain(fx);
1921
+
1922
+ Host.dispatch(fx, .fill_streams);
1923
+ Host.drain(fx);
1924
+ try std.testing.expectEqual(@as(usize, 16), fx.pendingFetchCount());
1925
+ try std.testing.expectEqual(@as(i64, 1), Host.model().errs);
1926
+ try std.testing.expectEqualStrings("rejected", Host.model().last_err);
1927
+ }
1928
+
1555
1929
  // ------------------------------------------------------ spawn streams
1556
1930
 
1557
1931
  const job_spawn_key: u64 = ts_core_host.spawn_key_base + 0;
@@ -2200,6 +2574,39 @@ test "a channel opens, posts route the five-field arm by name, and close retires
2200
2574
  try std.testing.expect(fx.channelHandle(41) != null);
2201
2575
  }
2202
2576
 
2577
+ test "audio capture wire records route canonical PCM through the ten-field arm" {
2578
+ const fx = freshChannel();
2579
+ defer fx.deinit();
2580
+ Host.init(fx);
2581
+
2582
+ Host.dispatch(fx, .start_capture);
2583
+ Host.drain(fx);
2584
+ try std.testing.expectEqual(@as(i64, 1), Host.model().capture_events);
2585
+ try std.testing.expectEqual(mini_core.CaptureState.started, Host.model().capture_state);
2586
+ try std.testing.expectEqual(mini_core.CaptureSource.microphone, Host.model().capture_source);
2587
+ try std.testing.expectEqual(@as(f64, 91), Host.model().capture_key);
2588
+ try std.testing.expectEqual(@as(f64, 16_000), Host.model().capture_rate);
2589
+ try std.testing.expectEqual(@as(f64, 1), Host.model().capture_channels);
2590
+
2591
+ const pcm = [_]u8{ 1, 0, 2, 0 };
2592
+ try fx.feedAudioCapture(91, 9_876_543, &pcm);
2593
+ Host.drain(fx);
2594
+ try std.testing.expectEqual(@as(i64, 2), Host.model().capture_events);
2595
+ try std.testing.expectEqual(mini_core.CaptureState.data, Host.model().capture_state);
2596
+ try std.testing.expectEqual(@as(f64, 9), Host.model().capture_timestamp_ms);
2597
+ try std.testing.expectEqual(@as(f64, 2), Host.model().capture_frames);
2598
+ try std.testing.expectEqualStrings(&pcm, Host.model().capture_pcm);
2599
+
2600
+ Host.dispatch(fx, .stop_capture);
2601
+ Host.drain(fx);
2602
+ try std.testing.expectEqual(@as(i64, 3), Host.model().capture_events);
2603
+ try std.testing.expectEqual(mini_core.CaptureState.stopped, Host.model().capture_state);
2604
+ // Terminal channel envelopes carry no PCM packet; the bridge retains
2605
+ // the requested source/format so every event arm stays self-describing.
2606
+ try std.testing.expectEqual(mini_core.CaptureSource.microphone, Host.model().capture_source);
2607
+ try std.testing.expectEqual(@as(f64, 16_000), Host.model().capture_rate);
2608
+ }
2609
+
2203
2610
  test "a mixed refused batch dispatches its rejections in command-stream order" {
2204
2611
  const fx = freshChannel();
2205
2612
  defer fx.deinit();
@@ -41,7 +41,10 @@
41
41
  //! Everything else on `UiApp.Options` is the wiring's, unchanged: view
42
42
  //! or markup, scene, `on_command` maps command ids through the core's
43
43
  //! `commandMsg`, `tokens_fn`/`windows_fn`/`status_item_fn` derive from
44
- //! the committed model. The two seams the core owns — `update_fx` and
44
+ //! the committed model. One model-helper convention joins that wiring:
45
+ //! an exported `themePack(model): "house" | "geist"` helper selects the
46
+ //! stock pack live through `theme_fn`, without taking ownership of the
47
+ //! system appearance axes. The two seams the core owns — `update_fx` and
45
48
  //! `init_fx` — are stamped by this adapter and must be left null.
46
49
  //!
47
50
  //! `Options.sync` is deliberately unsupported: it mutates the model in
@@ -183,6 +186,19 @@ pub fn TsUiApp(comptime core: type) type {
183
186
  var stamped = options;
184
187
  stamped.init_fx = initFx;
185
188
  stamped.update_fx = updateFx;
189
+ // A TypeScript core can select a built-in pack from ordinary
190
+ // model state without ejecting the generated launcher. The
191
+ // exported single-model helper emits as a Model method, so it
192
+ // is equally visible on direct compiler output and the
193
+ // external-core mirror. The app owns only the pack; UiApp's
194
+ // stock-token path keeps following the OS appearance.
195
+ if (comptime @hasDecl(Model, "themePack")) {
196
+ if (options.theme_fn != null) {
197
+ @panic("TsUiApp wires theme_fn from the core's themePack helper - remove the wiring's theme_fn");
198
+ }
199
+ comptime validateThemePackHelper();
200
+ stamped.theme_fn = themePackAdapter;
201
+ }
186
202
  // The core's host-event channels, comptime-detected from its
187
203
  // exports (export exists -> wired; every shape mismatch is a
188
204
  // teaching compile error in the adapter below). A wiring that
@@ -227,6 +243,28 @@ pub fn TsUiApp(comptime core: type) type {
227
243
  return stamped;
228
244
  }
229
245
 
246
+ fn themePackAdapter(model: *const Model) canvas.ThemePack {
247
+ const pack = model.themePack();
248
+ return canvas.ThemePack.fromName(@tagName(pack)).?;
249
+ }
250
+
251
+ fn validateThemePackHelper() void {
252
+ const teaching = "TsUiApp: themePack must be exported from core.ts as themePack(model: Model): ThemePack, where ThemePack is exactly \"house\" | \"geist\"";
253
+ const helper_info = @typeInfo(@TypeOf(Model.themePack));
254
+ if (helper_info != .@"fn") @compileError(teaching);
255
+ const function = helper_info.@"fn";
256
+ if (function.params.len != 1 or function.params[0].type == null or function.params[0].type.? != *const Model) {
257
+ @compileError(teaching);
258
+ }
259
+ const Pack = function.return_type orelse @compileError(teaching);
260
+ const pack_info = @typeInfo(Pack);
261
+ if (pack_info != .@"enum" or pack_info.@"enum".fields.len != 2 or
262
+ !@hasField(Pack, "house") or !@hasField(Pack, "geist"))
263
+ {
264
+ @compileError(teaching);
265
+ }
266
+ }
267
+
230
268
  /// `Options.init_fx`: register the wiring's boot images, perform
231
269
  /// the boot command and initial subscriptions on the installing
232
270
  /// frame (the boot model itself committed in `init`), dispatch
@@ -418,6 +418,14 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
418
418
  /// The scaffold wires this to app.zon's `theme` field
419
419
  /// through `app_runner.manifestThemePack()`.
420
420
  theme: canvas.ThemePack = .house,
421
+ /// Model-derived built-in theme pack. When set, this is
422
+ /// consulted on every rebuild instead of the static `theme`
423
+ /// field, while the runtime continues to own the live system
424
+ /// appearance axes (scheme, contrast, reduced motion), the
425
+ /// surface scale, and `theme_accent`. Explicit `tokens` or
426
+ /// `tokens_fn` still take precedence because those paths own
427
+ /// the complete token register.
428
+ theme_fn: ?*const fn (model: *const ModelT) canvas.ThemePack = null,
421
429
  /// The app's ONE-accent brand statement over the stock
422
430
  /// tokens: when set (and the app claims neither `tokens`
423
431
  /// nor `tokens_fn` — apps that own their tokens own their
@@ -1428,13 +1436,20 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
1428
1436
  switch (control) {
1429
1437
  .arm => self.effects.armReplay(),
1430
1438
  .feed => |record| switch (record.kind) {
1431
- .line => try self.effects.feedLine(record.key, record.payload),
1439
+ .line => try self.effects.feedLineWithMetadata(record.key, record.payload, record.truncated, record.dropped),
1432
1440
  .exit => {
1433
1441
  if (record.payload.len > 0) try self.effects.feedOutput(record.key, record.payload);
1434
1442
  if (record.stderr_tail.len > 0) try self.effects.feedStderr(record.key, record.stderr_tail);
1435
1443
  try self.effects.feedExitReason(record.key, record.code, record.exit_reason);
1436
1444
  },
1437
- .response => try self.effects.feedResponseOutcome(record.key, record.fetch_outcome, record.status, record.payload),
1445
+ .response => try self.effects.feedResponseOutcomeWithMetadata(
1446
+ record.key,
1447
+ record.fetch_outcome,
1448
+ record.status,
1449
+ record.payload,
1450
+ record.truncated,
1451
+ record.dropped,
1452
+ ),
1438
1453
  .file => try self.effects.feedFileResult(record.key, record.file_outcome, record.payload),
1439
1454
  .clipboard => try self.effects.feedClipboardResult(record.key, record.clipboard_outcome, record.payload),
1440
1455
  // `.host` records ride the route in `code` (0 ok / 1
@@ -1732,7 +1747,7 @@ pub fn UiAppWithFeatures(comptime ModelT: type, comptime MsgT: type, comptime fe
1732
1747
  },
1733
1748
  .contrast = if (self.system_appearance.high_contrast) .high else .standard,
1734
1749
  .reduce_motion = self.system_appearance.reduce_motion,
1735
- .pack = self.options.theme,
1750
+ .pack = if (self.options.theme_fn) |theme_fn| theme_fn(&self.model) else self.options.theme,
1736
1751
  });
1737
1752
  if (self.options.theme_accent) |accent| {
1738
1753
  // The manifest accent layers over the resolved pack —
@@ -43,6 +43,10 @@ fn counterCommand(name: []const u8) ?CounterMsg {
43
43
  return null;
44
44
  }
45
45
 
46
+ fn counterThemePack(model: *const CounterModel) canvas.ThemePack {
47
+ return if (model.count == 0) .house else .geist;
48
+ }
49
+
46
50
  const counter_views = [_]app_manifest.ShellView{
47
51
  .{ .label = canvas_label, .kind = .gpu_surface, .fill = true, .gpu_backend = .metal },
48
52
  };
@@ -1679,6 +1683,50 @@ test "unthemed apps follow the system appearance live; explicit tokens opt out"
1679
1683
  try std.testing.expectEqualDeep(light.colors.background, fixed_state.effectiveTokens().colors.background);
1680
1684
  }
1681
1685
 
1686
+ test "model-derived theme packs retain live appearance, accent, and surface scale" {
1687
+ var options = counterOptions();
1688
+ options.theme = .geist; // The model-derived pack wins this fallback.
1689
+ options.theme_fn = counterThemePack;
1690
+ options.theme_accent = canvas.Color.rgb8(0xdf, 0x26, 0x70);
1691
+
1692
+ const app_state = try std.testing.allocator.create(CounterApp);
1693
+ defer std.testing.allocator.destroy(app_state);
1694
+ app_state.* = CounterApp.init(std.heap.page_allocator, .{}, options);
1695
+ defer app_state.deinit();
1696
+ app_state.system_appearance = .{ .color_scheme = .dark, .reduce_motion = true };
1697
+ app_state.pixel_snap_scale = 2;
1698
+
1699
+ var house = canvas.DesignTokens.theme(.{
1700
+ .color_scheme = .dark,
1701
+ .reduce_motion = true,
1702
+ .pack = .house,
1703
+ });
1704
+ house = house.withOverrides(canvas.accentOverrides(options.theme_accent.?, .dark));
1705
+ var actual = app_state.effectiveTokens();
1706
+ try std.testing.expectEqualDeep(house.colors.background, actual.colors.background);
1707
+ try std.testing.expectEqualDeep(house.colors.accent, actual.colors.accent);
1708
+ try std.testing.expectEqualDeep(house.colors.focus_ring, actual.colors.focus_ring);
1709
+ try std.testing.expectEqual(house.metrics.control_height, actual.metrics.control_height);
1710
+ try std.testing.expectEqual(@as(u32, 0), actual.motion.normal_ms);
1711
+ try std.testing.expectEqual(@as(f32, 2), actual.pixel_snap.scale);
1712
+
1713
+ app_state.model.count = 1;
1714
+ var geist = canvas.DesignTokens.theme(.{
1715
+ .color_scheme = .dark,
1716
+ .reduce_motion = true,
1717
+ .pack = .geist,
1718
+ });
1719
+ geist = geist.withOverrides(canvas.accentOverrides(options.theme_accent.?, .dark));
1720
+ actual = app_state.effectiveTokens();
1721
+ try std.testing.expectEqualDeep(geist.colors.background, actual.colors.background);
1722
+ try std.testing.expectEqualDeep(geist.colors.accent, actual.colors.accent);
1723
+ try std.testing.expectEqualDeep(geist.colors.focus_ring, actual.colors.focus_ring);
1724
+ try std.testing.expectEqual(geist.metrics.control_height, actual.metrics.control_height);
1725
+ try std.testing.expect(house.metrics.control_height != geist.metrics.control_height);
1726
+ try std.testing.expectEqual(@as(u32, 0), actual.motion.normal_ms);
1727
+ try std.testing.expectEqual(@as(f32, 2), actual.pixel_snap.scale);
1728
+ }
1729
+
1682
1730
  test "static tokens carry the surface scale and re-snap on a scale change" {
1683
1731
  const harness = try core.TestHarness().create(std.testing.allocator, .{ .size = geometry.SizeF.init(400, 300) });
1684
1732
  defer harness.destroy(std.testing.allocator);
@@ -836,6 +836,7 @@ pub const RuntimeView = struct {
836
836
  pub const pruneCanvasWidgetTextHistory = CanvasWidgetTextMethods.pruneCanvasWidgetTextHistory;
837
837
  pub const clearCanvasWidgetTextVerticalGoal = CanvasWidgetTextMethods.clearCanvasWidgetTextVerticalGoal;
838
838
  pub const canEditCanvasWidgetText = CanvasWidgetTextMethods.canEditCanvasWidgetText;
839
+ pub const ensureCanvasWidgetFocusedTextCaret = CanvasWidgetTextMethods.ensureCanvasWidgetFocusedTextCaret;
839
840
  pub const applyCanvasWidgetTextPointer = CanvasWidgetTextMethods.applyCanvasWidgetTextPointer;
840
841
  pub const clearCanvasWidgetStaticTextSelection = CanvasWidgetTextMethods.clearCanvasWidgetStaticTextSelection;
841
842
  pub const canvasWidgetCopyText = CanvasWidgetTextMethods.canvasWidgetCopyText;
@@ -856,6 +857,7 @@ pub const RuntimeView = struct {
856
857
  pub const applyCanvasWidgetTextareaScroll = CanvasWidgetScrollMethods.applyCanvasWidgetTextareaScroll;
857
858
  pub const applyCanvasWidgetScrollDriverOffset = CanvasWidgetScrollMethods.applyCanvasWidgetScrollDriverOffset;
858
859
  pub const applyCanvasWidgetScrollKeyboardTarget = CanvasWidgetScrollMethods.applyCanvasWidgetScrollKeyboardTarget;
860
+ pub const scrollCanvasWidgetIntoView = CanvasWidgetScrollMethods.scrollCanvasWidgetIntoView;
859
861
  pub const stepCanvasWidgetKineticScroll = CanvasWidgetScrollMethods.stepCanvasWidgetKineticScroll;
860
862
  pub const canvasWidgetScrollContentExtent = CanvasWidgetScrollMethods.canvasWidgetScrollContentExtent;
861
863
  pub const canvasWidgetScrollContentExtentX = CanvasWidgetScrollMethods.canvasWidgetScrollContentExtentX;