@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
@@ -1053,6 +1053,94 @@ pub const EffectChannelEvent = struct {
1053
1053
  dropped_total: u32 = 0,
1054
1054
  };
1055
1055
 
1056
+ /// Capture stream states delivered through `Effects.audioCaptureMsg`.
1057
+ /// `.started`, `.data`, and `.failed` originate at the native source;
1058
+ /// `.stopped` is the channel's exactly-once close terminal and `.rejected`
1059
+ /// is a loop-side refusal before a source started.
1060
+ pub const EffectAudioCaptureEventKind = enum(u8) {
1061
+ started,
1062
+ data,
1063
+ failed,
1064
+ stopped,
1065
+ rejected,
1066
+ };
1067
+
1068
+ /// One microphone or system-output capture report. PCM is interleaved
1069
+ /// signed 16-bit little-endian drain scratch: copy what the model keeps.
1070
+ /// `key` is the caller's stream identity; it is authoritative on terminal
1071
+ /// events, whose format fields are zero/default because no packet exists.
1072
+ pub const EffectAudioCaptureEvent = struct {
1073
+ key: u64,
1074
+ kind: EffectAudioCaptureEventKind,
1075
+ source: platform.AudioCaptureSource = .microphone,
1076
+ sample_rate: u32 = 0,
1077
+ channels: u8 = 0,
1078
+ /// Capture timestamp in monotonic milliseconds. The platform packet
1079
+ /// keeps nanoseconds, but the public event uses milliseconds so a TS
1080
+ /// `number` carries it exactly across long-running sessions.
1081
+ timestamp_ms: u64 = 0,
1082
+ frames: u32 = 0,
1083
+ pcm_s16le: []const u8 = "",
1084
+ dropped_pending: u32 = 0,
1085
+ dropped_total: u32 = 0,
1086
+ };
1087
+
1088
+ const audio_capture_packet_header_bytes: usize = 24;
1089
+ const audio_capture_packet_magic = "AC01";
1090
+
1091
+ /// Convert the channel envelope used internally by capture into its public
1092
+ /// typed event. Also useful to native integrations that intentionally route
1093
+ /// capture through a generic `channelMsg` handler.
1094
+ pub fn decodeAudioCaptureChannelEvent(channel: EffectChannelEvent) EffectAudioCaptureEvent {
1095
+ if (channel.kind == .closed) return .{
1096
+ .key = channel.key,
1097
+ .kind = .stopped,
1098
+ .dropped_pending = channel.dropped_pending,
1099
+ .dropped_total = channel.dropped_total,
1100
+ };
1101
+ if (channel.kind == .rejected) return .{
1102
+ .key = channel.key,
1103
+ .kind = .rejected,
1104
+ .dropped_pending = channel.dropped_pending,
1105
+ .dropped_total = channel.dropped_total,
1106
+ };
1107
+ const bytes = channel.bytes;
1108
+ if (bytes.len < audio_capture_packet_header_bytes or !std.mem.eql(u8, bytes[0..4], audio_capture_packet_magic)) {
1109
+ return .{ .key = channel.key, .kind = .failed, .dropped_pending = channel.dropped_pending, .dropped_total = channel.dropped_total };
1110
+ }
1111
+ const native_kind = std.enums.fromInt(platform.AudioCaptureEventKind, bytes[4]) orelse
1112
+ return .{ .key = channel.key, .kind = .failed, .dropped_pending = channel.dropped_pending, .dropped_total = channel.dropped_total };
1113
+ const source = std.enums.fromInt(platform.AudioCaptureSource, bytes[5]) orelse
1114
+ return .{ .key = channel.key, .kind = .failed, .dropped_pending = channel.dropped_pending, .dropped_total = channel.dropped_total };
1115
+ const channels = bytes[6];
1116
+ const sample_rate = std.mem.readInt(u32, bytes[8..12], .little);
1117
+ const frames = std.mem.readInt(u32, bytes[12..16], .little);
1118
+ const timestamp_ns = std.mem.readInt(u64, bytes[16..24], .little);
1119
+ const pcm = bytes[audio_capture_packet_header_bytes..];
1120
+ const format: platform.AudioCaptureFormat = .{ .sample_rate = sample_rate, .channels = channels };
1121
+ const shape_valid = format.valid() and switch (native_kind) {
1122
+ .data => pcm.len == @as(usize, frames) * @as(usize, channels) * 2,
1123
+ .started, .failed => frames == 0 and pcm.len == 0,
1124
+ };
1125
+ if (!shape_valid) return .{ .key = channel.key, .kind = .failed, .dropped_pending = channel.dropped_pending, .dropped_total = channel.dropped_total };
1126
+ return .{
1127
+ .key = channel.key,
1128
+ .kind = switch (native_kind) {
1129
+ .started => .started,
1130
+ .data => .data,
1131
+ .failed => .failed,
1132
+ },
1133
+ .source = source,
1134
+ .sample_rate = sample_rate,
1135
+ .channels = channels,
1136
+ .timestamp_ms = timestamp_ns / std.time.ns_per_ms,
1137
+ .frames = frames,
1138
+ .pcm_s16le = pcm,
1139
+ .dropped_pending = channel.dropped_pending,
1140
+ .dropped_total = channel.dropped_total,
1141
+ };
1142
+ }
1143
+
1056
1144
  /// One channel's cross-thread staging FIFO: fixed entries, never
1057
1145
  /// evicted (see `max_effect_channel_pending`). Allocated from
1058
1146
  /// `process_allocator` per open occupancy and freed when the `.closed`
@@ -2546,6 +2634,18 @@ pub fn Effects(comptime Msg: type) type {
2546
2634
  }.make;
2547
2635
  }
2548
2636
 
2637
+ /// Comptime Msg constructor for microphone/system capture. Capture
2638
+ /// reuses the external-channel transport internally, while this
2639
+ /// constructor decodes its bounded packet into
2640
+ /// `native_sdk.EffectAudioCaptureEvent` for app code.
2641
+ pub fn audioCaptureMsg(comptime tag: std.meta.Tag(Msg)) ChannelMsgFn {
2642
+ return struct {
2643
+ fn make(event: EffectChannelEvent) Msg {
2644
+ return @unionInit(Msg, @tagName(tag), decodeAudioCaptureChannelEvent(event));
2645
+ }
2646
+ }.make;
2647
+ }
2648
+
2549
2649
  /// Comptime Msg constructor for `on_event` of pty effects:
2550
2650
  /// `ptyMsg(.shell)` builds `Msg{ .shell = event }` — the
2551
2651
  /// variant's payload type must be `native_sdk.EffectPtyEvent`.
@@ -2893,6 +2993,18 @@ pub fn Effects(comptime Msg: type) type {
2893
2993
  max_pending: u32 = max_effect_channel_pending,
2894
2994
  };
2895
2995
 
2996
+ pub const StartAudioCaptureOptions = struct {
2997
+ /// Caller-chosen stream identity. Capture uses the external
2998
+ /// channel table and its shared effect-key namespace.
2999
+ key: u64,
3000
+ source: platform.AudioCaptureSource,
3001
+ sample_rate: u32 = 48_000,
3002
+ channels: u8 = 1,
3003
+ /// Use `Effects.audioCaptureMsg(.tag)` for a typed payload.
3004
+ on_event: ChannelMsgFn,
3005
+ max_pending: u32 = max_effect_channel_pending,
3006
+ };
3007
+
2896
3008
  pub const PtySpawnOptions = struct {
2897
3009
  /// Caller-chosen identity, stored in the model. Shares the
2898
3010
  /// keyed families' one key space, occupied from spawn until
@@ -3086,6 +3198,14 @@ pub fn Effects(comptime Msg: type) type {
3086
3198
  park_state: ParkOrderState = .none,
3087
3199
  };
3088
3200
 
3201
+ const AudioCaptureSlot = struct {
3202
+ active: bool = false,
3203
+ platform_started: bool = false,
3204
+ key: u64 = 0,
3205
+ source: platform.AudioCaptureSource = .microphone,
3206
+ format: platform.AudioCaptureFormat = .{},
3207
+ };
3208
+
3089
3209
  /// The single audio playback channel — one player is the whole
3090
3210
  /// platform surface, so the effects layer holds exactly one.
3091
3211
  /// Like timers it is a platform service arm, not a worker-thread
@@ -4270,6 +4390,10 @@ pub fn Effects(comptime Msg: type) type {
4270
4390
  /// the effect slots. Loop-thread only — the thread-shared half
4271
4391
  /// of each slot lives behind its `ChannelSlot.shared` header.
4272
4392
  channel_slots: [max_effect_channels]ChannelSlot = [_]ChannelSlot{.{}} ** max_effect_channels,
4393
+ /// One independently-running capture per source. PCM itself rides the
4394
+ /// channel table; this tiny loop-side mirror exists to quiesce native
4395
+ /// audio callbacks before closing/reusing their channel occupancy.
4396
+ audio_capture_slots: [2]AudioCaptureSlot = [_]AudioCaptureSlot{.{}} ** 2,
4273
4397
  /// Monotonic post-order stamp shared by every channel's staging
4274
4398
  /// FIFO and the close markers: the cross-channel delivery order
4275
4399
  /// and the drain boundary's causality cut (posts stamped at or
@@ -4485,6 +4609,18 @@ pub fn Effects(comptime Msg: type) type {
4485
4609
  }
4486
4610
  timer_slot.* = .{};
4487
4611
  }
4612
+ // Quiesce every native capture BEFORE the channel sweep closes
4613
+ // its posting target and frees its staging. The platform stop
4614
+ // contract fences callbacks synchronously, so after this loop no capture
4615
+ // source can still enter its channel while teardown dismantles
4616
+ // staging below. Fake/replay occupancies have no platform source
4617
+ // to stop, but their mirrors are cleared by the same sweep.
4618
+ for (&self.audio_capture_slots) |*capture| {
4619
+ if (capture.active and capture.platform_started) {
4620
+ if (self.services) |services| services.audioCaptureStop(capture.source) catch {};
4621
+ }
4622
+ capture.* = .{};
4623
+ }
4488
4624
  // Silence the platform audio player (best effort) and clear
4489
4625
  // the channel.
4490
4626
  if (self.audio.active and !self.audio.fake) {
@@ -6392,6 +6528,103 @@ pub fn Effects(comptime Msg: type) type {
6392
6528
  return null;
6393
6529
  }
6394
6530
 
6531
+ /// Start (or replace) the native capture for one source. Capture is
6532
+ /// a typed specialization of `openChannel`: bounded staging, drop
6533
+ /// accounting, loop wakeups, and session replay are inherited
6534
+ /// verbatim. The platform normalizes to the requested signed-16 LE
6535
+ /// format and reports `.started` before its first `.data` packet.
6536
+ pub fn startAudioCapture(self: *Self, options: StartAudioCaptureOptions) void {
6537
+ const format: platform.AudioCaptureFormat = .{
6538
+ .sample_rate = options.sample_rate,
6539
+ .channels = options.channels,
6540
+ };
6541
+ if (!format.valid()) {
6542
+ self.rejectChannel(options.key, options.on_event, true);
6543
+ return;
6544
+ }
6545
+ const capture_index: usize = @intFromEnum(options.source);
6546
+ const capture = &self.audio_capture_slots[capture_index];
6547
+
6548
+ // Admit the replacement BEFORE stopping the source it would
6549
+ // supersede. A duplicate/occupied key, a full channel table, or
6550
+ // an executor allocation failure must reject the new request
6551
+ // without destroying a healthy recording. A same-key restart is
6552
+ // therefore only a rejection; the existing stream keeps running
6553
+ // until its owner explicitly stops it and receives `.stopped`.
6554
+ if (self.keyOccupiedUntilDelivery(options.key)) {
6555
+ self.rejectChannel(options.key, options.on_event, true);
6556
+ return;
6557
+ }
6558
+
6559
+ const handle = self.openChannel(.{
6560
+ .key = options.key,
6561
+ .on_event = options.on_event,
6562
+ .max_pending = options.max_pending,
6563
+ });
6564
+ // A refused open has no slot. Replay parks one but deliberately
6565
+ // returns an inert handle; retain the tiny mirror so the replayed
6566
+ // stop verb closes that parked occupancy without touching a host.
6567
+ if (self.findChannelSlot(options.key) == null) return;
6568
+ if (capture.active) self.closeChannel(capture.key);
6569
+ capture.* = .{
6570
+ .active = true,
6571
+ .platform_started = false,
6572
+ .key = options.key,
6573
+ .source = options.source,
6574
+ .format = format,
6575
+ };
6576
+ if (!handle.live()) return;
6577
+
6578
+ const sink: platform.AudioCaptureSink = .{
6579
+ .context = handle.shared,
6580
+ .generation = handle.generation,
6581
+ .push_fn = pushAudioCaptureEvent,
6582
+ };
6583
+ if (self.executor == .fake) {
6584
+ _ = sink.push(.{ .kind = .started, .source = options.source, .format = format });
6585
+ return;
6586
+ }
6587
+ const services = self.services orelse {
6588
+ _ = sink.push(.{ .kind = .failed, .source = options.source, .format = format });
6589
+ self.closeChannel(options.key);
6590
+ return;
6591
+ };
6592
+ services.audioCaptureStart(options.source, format, sink) catch {
6593
+ _ = sink.push(.{ .kind = .failed, .source = options.source, .format = format });
6594
+ self.closeChannel(options.key);
6595
+ return;
6596
+ };
6597
+ capture.platform_started = true;
6598
+ }
6599
+
6600
+ /// Stop a keyed capture. Accepted PCM already staged drains first,
6601
+ /// then exactly one `.stopped` terminal delivers through the channel.
6602
+ pub fn stopAudioCapture(self: *Self, key: u64) void {
6603
+ if (self.findAudioCaptureByKey(key) == null) return;
6604
+ self.closeChannel(key);
6605
+ }
6606
+
6607
+ /// Deterministic fake-executor seam: push one canonical PCM chunk
6608
+ /// through the same packet/channel path a native callback uses.
6609
+ pub fn feedAudioCapture(self: *Self, key: u64, timestamp_ns: u64, pcm_s16le: []const u8) error{ EffectNotFound, InvalidAudioCaptureData, EffectQueueFull }!void {
6610
+ const capture = self.findAudioCaptureByKey(key) orelse return error.EffectNotFound;
6611
+ if (pcm_s16le.len % (@as(usize, capture.format.channels) * 2) != 0 or
6612
+ pcm_s16le.len > platform.max_audio_capture_pcm_bytes)
6613
+ {
6614
+ return error.InvalidAudioCaptureData;
6615
+ }
6616
+ const handle = self.channelHandle(key) orelse return error.EffectNotFound;
6617
+ const result = pushAudioCaptureEvent(handle.shared, handle.generation, .{
6618
+ .kind = .data,
6619
+ .source = capture.source,
6620
+ .format = capture.format,
6621
+ .timestamp_ns = timestamp_ns,
6622
+ .frames = @intCast(pcm_s16le.len / (@as(usize, capture.format.channels) * 2)),
6623
+ .pcm_s16le = pcm_s16le,
6624
+ });
6625
+ if (result != .accepted) return error.EffectQueueFull;
6626
+ }
6627
+
6395
6628
  /// Close the open channel with `key`: posts stop landing
6396
6629
  /// immediately (the handle answers `.closed`), the staged
6397
6630
  /// backlog flushes in post order, and exactly one terminal
@@ -6401,6 +6634,13 @@ pub fn Effects(comptime Msg: type) type {
6401
6634
  /// is NOT a channel's close (channels end the way audio does:
6402
6635
  /// through their own verb).
6403
6636
  pub fn closeChannel(self: *Self, key: u64) void {
6637
+ if (self.findAudioCaptureByKey(key)) |capture| {
6638
+ if (capture.platform_started) {
6639
+ if (self.services) |services| services.audioCaptureStop(capture.source) catch {};
6640
+ }
6641
+ capture.active = false;
6642
+ capture.platform_started = false;
6643
+ }
6404
6644
  const slot = self.findChannelSlot(key) orelse return;
6405
6645
  if (slot.state != .open) return;
6406
6646
  slot.state = .closing;
@@ -10054,6 +10294,33 @@ pub fn Effects(comptime Msg: type) type {
10054
10294
  }
10055
10295
  }
10056
10296
 
10297
+ /// Feed one synthetic line-mode result with the same loss metadata a
10298
+ /// real executor can attach. This is the exact fake/replay seam for
10299
+ /// testing consumers that must reject cut or dropped stream data;
10300
+ /// collect-mode spawns have no `EffectLine` results and are refused.
10301
+ pub fn feedLineWithMetadata(
10302
+ self: *Self,
10303
+ key: u64,
10304
+ bytes: []const u8,
10305
+ truncated: bool,
10306
+ dropped_before: u32,
10307
+ ) error{ EffectNotFound, EffectQueueFull }!void {
10308
+ const slot_index = self.findActiveFakeSlot(key, .spawn) orelse blk: {
10309
+ const fetch_index = self.findActiveFakeSlot(key, .fetch) orelse return error.EffectNotFound;
10310
+ if (self.slots[fetch_index].fetch_response_mode != .stream) return error.EffectNotFound;
10311
+ break :blk fetch_index;
10312
+ };
10313
+ const slot = &self.slots[slot_index];
10314
+ if (slot.kind == .spawn and slot.output_mode == .collect) return error.EffectNotFound;
10315
+
10316
+ const prior_dropped = slot.dropped_pending;
10317
+ slot.dropped_pending +|= dropped_before;
10318
+ if (!self.produceLine(slot, @intCast(slot_index), slot.generation, bytes, truncated, true)) {
10319
+ slot.dropped_pending = prior_dropped;
10320
+ return error.EffectQueueFull;
10321
+ }
10322
+ }
10323
+
10057
10324
  /// Feed synthetic stderr bytes to the fake `.collect` effect with
10058
10325
  /// `key`. Mirrors the real tail: only the last
10059
10326
  /// `max_effect_stderr_tail_bytes` are kept and earlier bytes are
@@ -10148,6 +10415,21 @@ pub fn Effects(comptime Msg: type) type {
10148
10415
  /// `.connect_failed`, ...). Non-`.ok` outcomes carry no body,
10149
10416
  /// mirroring the real executor.
10150
10417
  pub fn feedResponseOutcome(self: *Self, key: u64, outcome: EffectFetchOutcome, status: u16, body: []const u8) error{EffectNotFound}!void {
10418
+ return self.feedResponseOutcomeWithMetadata(key, outcome, status, body, false, 0);
10419
+ }
10420
+
10421
+ /// Exact synthetic response feed, including loss metadata recorded on
10422
+ /// a real `EffectResponse`. The ordinary helper above derives body
10423
+ /// truncation and supplies zero additional loss.
10424
+ pub fn feedResponseOutcomeWithMetadata(
10425
+ self: *Self,
10426
+ key: u64,
10427
+ outcome: EffectFetchOutcome,
10428
+ status: u16,
10429
+ body: []const u8,
10430
+ truncated: bool,
10431
+ dropped_before: u32,
10432
+ ) error{EffectNotFound}!void {
10151
10433
  const slot_index = self.findActiveFakeSlot(key, .fetch) orelse return error.EffectNotFound;
10152
10434
  const slot = &self.slots[slot_index];
10153
10435
  const buffer = slot.fetch_buffer orelse return error.EffectNotFound;
@@ -10169,8 +10451,8 @@ pub fn Effects(comptime Msg: type) type {
10169
10451
  .generation = slot.generation,
10170
10452
  .key = slot.key,
10171
10453
  .line_len = @intCast(slot.body_len),
10172
- .truncated = slot.fetch_truncated,
10173
- .dropped_before = slot.dropped_pending,
10454
+ .truncated = slot.fetch_truncated or truncated,
10455
+ .dropped_before = slot.dropped_pending +| dropped_before,
10174
10456
  .status = status,
10175
10457
  .outcome = outcome,
10176
10458
  .response_fn = slot.on_response,
@@ -10726,6 +11008,35 @@ pub fn Effects(comptime Msg: type) type {
10726
11008
  });
10727
11009
  }
10728
11010
 
11011
+ fn findAudioCaptureByKey(self: *Self, key: u64) ?*AudioCaptureSlot {
11012
+ for (&self.audio_capture_slots) |*capture| {
11013
+ if (capture.active and capture.key == key) return capture;
11014
+ }
11015
+ return null;
11016
+ }
11017
+
11018
+ fn pushAudioCaptureEvent(context: ?*anyopaque, generation: u64, event: platform.AudioCaptureEvent) platform.AudioCapturePushResult {
11019
+ const shared: *ChannelShared = @ptrCast(@alignCast(context orelse return .closed));
11020
+ var packet: [max_effect_channel_bytes]u8 = undefined;
11021
+ const total = audio_capture_packet_header_bytes + event.pcm_s16le.len;
11022
+ if (total > packet.len) return .dropped_oversized;
11023
+ @memcpy(packet[0..4], audio_capture_packet_magic);
11024
+ packet[4] = @intFromEnum(event.kind);
11025
+ packet[5] = @intFromEnum(event.source);
11026
+ packet[6] = event.format.channels;
11027
+ packet[7] = 0;
11028
+ std.mem.writeInt(u32, packet[8..12], event.format.sample_rate, .little);
11029
+ std.mem.writeInt(u32, packet[12..16], event.frames, .little);
11030
+ std.mem.writeInt(u64, packet[16..24], event.timestamp_ns, .little);
11031
+ @memcpy(packet[audio_capture_packet_header_bytes..total], event.pcm_s16le);
11032
+ return switch ((ChannelHandle{ .shared = shared, .generation = generation }).post(packet[0..total])) {
11033
+ .accepted => .accepted,
11034
+ .dropped_full => .dropped_full,
11035
+ .dropped_oversized => .dropped_oversized,
11036
+ .closed => .closed,
11037
+ };
11038
+ }
11039
+
10729
11040
  /// The next channel occupancy generation — channel-owned,
10730
11041
  /// u64, monotonic (see `channel_generation`). The zero skip
10731
11042
  /// mirrors the shared counter's: 0 is the never-opened /
@@ -0,0 +1,251 @@
1
+ //! Microphone/system capture coverage over the fake executor. The native
2
+ //! hosts have platform compile/smoke coverage; these tests pin the shared
3
+ //! typed stream, canonical PCM shape, bounded back-pressure, and lifecycle.
4
+
5
+ const std = @import("std");
6
+ const effects_mod = @import("effects.zig");
7
+ const platform = @import("../platform/root.zig");
8
+
9
+ const testing = std.testing;
10
+
11
+ const Msg = union(enum) {
12
+ capture: effects_mod.EffectAudioCaptureEvent,
13
+ channel: effects_mod.EffectChannelEvent,
14
+ };
15
+
16
+ const Fx = effects_mod.Effects(Msg);
17
+
18
+ fn takeCapture(fx: *Fx, kind: effects_mod.EffectAudioCaptureEventKind) !effects_mod.EffectAudioCaptureEvent {
19
+ const msg = fx.takeMsg() orelse return error.TestExpectedMsg;
20
+ try testing.expect(msg == .capture);
21
+ try testing.expectEqual(kind, msg.capture.kind);
22
+ return msg.capture;
23
+ }
24
+
25
+ test "audio capture fake lifecycle delivers canonical PCM and a stopped terminal" {
26
+ var fx = Fx.init(testing.allocator);
27
+ defer fx.deinit();
28
+ fx.executor = .fake;
29
+
30
+ fx.startAudioCapture(.{
31
+ .key = 71,
32
+ .source = .microphone,
33
+ .sample_rate = 16_000,
34
+ .channels = 1,
35
+ .on_event = Fx.audioCaptureMsg(.capture),
36
+ });
37
+ const started = try takeCapture(&fx, .started);
38
+ try testing.expectEqual(@as(u64, 71), started.key);
39
+ try testing.expectEqual(platform.AudioCaptureSource.microphone, started.source);
40
+ try testing.expectEqual(@as(u32, 16_000), started.sample_rate);
41
+ try testing.expectEqual(@as(u8, 1), started.channels);
42
+
43
+ const pcm = [_]u8{ 0x01, 0x00, 0xff, 0x7f, 0x00, 0x80 };
44
+ try fx.feedAudioCapture(71, 12_345_678, &pcm);
45
+ const data = try takeCapture(&fx, .data);
46
+ try testing.expectEqual(@as(u64, 12), data.timestamp_ms);
47
+ try testing.expectEqual(@as(u32, 3), data.frames);
48
+ try testing.expectEqualSlices(u8, &pcm, data.pcm_s16le);
49
+
50
+ fx.stopAudioCapture(71);
51
+ const stopped = try takeCapture(&fx, .stopped);
52
+ try testing.expectEqual(@as(u64, 71), stopped.key);
53
+ try testing.expectEqual(@as(?Msg, null), fx.takeMsg());
54
+ try testing.expectError(error.EffectNotFound, fx.feedAudioCapture(71, 0, &pcm));
55
+ }
56
+
57
+ test "microphone and system capture run concurrently and source replacement is ordered" {
58
+ var fx = Fx.init(testing.allocator);
59
+ defer fx.deinit();
60
+ fx.executor = .fake;
61
+
62
+ fx.startAudioCapture(.{ .key = 1, .source = .microphone, .on_event = Fx.audioCaptureMsg(.capture) });
63
+ fx.startAudioCapture(.{ .key = 2, .source = .system, .channels = 2, .on_event = Fx.audioCaptureMsg(.capture) });
64
+ try testing.expectEqual(platform.AudioCaptureSource.microphone, (try takeCapture(&fx, .started)).source);
65
+ try testing.expectEqual(platform.AudioCaptureSource.system, (try takeCapture(&fx, .started)).source);
66
+
67
+ // Starting another microphone capture closes key 1 before key 3 starts.
68
+ fx.startAudioCapture(.{ .key = 3, .source = .microphone, .on_event = Fx.audioCaptureMsg(.capture) });
69
+ try testing.expectEqual(@as(u64, 1), (try takeCapture(&fx, .stopped)).key);
70
+ try testing.expectEqual(@as(u64, 3), (try takeCapture(&fx, .started)).key);
71
+
72
+ fx.stopAudioCapture(2);
73
+ fx.stopAudioCapture(3);
74
+ _ = try takeCapture(&fx, .stopped);
75
+ _ = try takeCapture(&fx, .stopped);
76
+ }
77
+
78
+ test "audio capture refuses a key owned by the other source without corrupting it" {
79
+ var fx = Fx.init(testing.allocator);
80
+ defer fx.deinit();
81
+ fx.executor = .fake;
82
+
83
+ fx.startAudioCapture(.{ .key = 33, .source = .system, .on_event = Fx.audioCaptureMsg(.capture) });
84
+ try testing.expectEqual(platform.AudioCaptureSource.system, (try takeCapture(&fx, .started)).source);
85
+
86
+ fx.startAudioCapture(.{ .key = 33, .source = .microphone, .on_event = Fx.audioCaptureMsg(.capture) });
87
+ try testing.expectEqual(@as(u64, 33), (try takeCapture(&fx, .rejected)).key);
88
+
89
+ const pcm = [_]u8{ 1, 0 };
90
+ try fx.feedAudioCapture(33, 1_000_000, &pcm);
91
+ try testing.expectEqual(platform.AudioCaptureSource.system, (try takeCapture(&fx, .data)).source);
92
+ fx.stopAudioCapture(33);
93
+ _ = try takeCapture(&fx, .stopped);
94
+ }
95
+
96
+ test "a refused same-source replacement leaves the current capture running" {
97
+ var fx = Fx.init(testing.allocator);
98
+ defer fx.deinit();
99
+ fx.executor = .fake;
100
+
101
+ fx.startAudioCapture(.{ .key = 35, .source = .microphone, .on_event = Fx.audioCaptureMsg(.capture) });
102
+ _ = try takeCapture(&fx, .started);
103
+
104
+ const occupied = fx.openChannel(.{ .key = 36, .on_event = Fx.channelMsg(.channel) });
105
+ try testing.expect(occupied.live());
106
+
107
+ // The replacement cannot claim key 36. Refusing it must not stop key 35.
108
+ fx.startAudioCapture(.{ .key = 36, .source = .microphone, .on_event = Fx.audioCaptureMsg(.capture) });
109
+ try testing.expectEqual(@as(u64, 36), (try takeCapture(&fx, .rejected)).key);
110
+
111
+ const pcm = [_]u8{ 1, 0 };
112
+ try fx.feedAudioCapture(35, 1_000_000, &pcm);
113
+ try testing.expectEqual(@as(u64, 35), (try takeCapture(&fx, .data)).key);
114
+ try testing.expect(occupied.live());
115
+
116
+ // A same-key duplicate is a refusal too, never an implicit stop.
117
+ fx.startAudioCapture(.{ .key = 35, .source = .microphone, .on_event = Fx.audioCaptureMsg(.capture) });
118
+ try testing.expectEqual(@as(u64, 35), (try takeCapture(&fx, .rejected)).key);
119
+ try fx.feedAudioCapture(35, 2_000_000, &pcm);
120
+ try testing.expectEqual(@as(u64, 35), (try takeCapture(&fx, .data)).key);
121
+
122
+ fx.stopAudioCapture(35);
123
+ _ = try takeCapture(&fx, .stopped);
124
+ fx.closeChannel(36);
125
+ const closed = fx.takeMsg() orelse return error.TestExpectedMsg;
126
+ try testing.expect(closed == .channel);
127
+ try testing.expectEqual(effects_mod.EffectChannelEventKind.closed, closed.channel.kind);
128
+ }
129
+
130
+ test "stopping an unknown audio key leaves an ordinary channel open" {
131
+ var fx = Fx.init(testing.allocator);
132
+ defer fx.deinit();
133
+ fx.executor = .fake;
134
+
135
+ const handle = fx.openChannel(.{ .key = 34, .on_event = Fx.channelMsg(.channel) });
136
+ try testing.expect(handle.live());
137
+
138
+ fx.stopAudioCapture(34);
139
+ try testing.expect(handle.live());
140
+ try testing.expectEqual(effects_mod.ChannelHandle.PostResult.accepted, handle.post("still open"));
141
+
142
+ const data = fx.takeMsg() orelse return error.TestExpectedMsg;
143
+ try testing.expect(data == .channel);
144
+ try testing.expectEqual(effects_mod.EffectChannelEventKind.data, data.channel.kind);
145
+ try testing.expectEqualStrings("still open", data.channel.bytes);
146
+
147
+ fx.closeChannel(34);
148
+ const closed = fx.takeMsg() orelse return error.TestExpectedMsg;
149
+ try testing.expect(closed == .channel);
150
+ try testing.expectEqual(effects_mod.EffectChannelEventKind.closed, closed.channel.kind);
151
+ }
152
+
153
+ test "audio capture validates format and reports bounded queue drops" {
154
+ var fx = Fx.init(testing.allocator);
155
+ defer fx.deinit();
156
+ fx.executor = .fake;
157
+
158
+ fx.startAudioCapture(.{
159
+ .key = 8,
160
+ .source = .system,
161
+ .sample_rate = 44_100,
162
+ .on_event = Fx.audioCaptureMsg(.capture),
163
+ });
164
+ try testing.expectEqual(@as(u64, 8), (try takeCapture(&fx, .rejected)).key);
165
+
166
+ fx.startAudioCapture(.{
167
+ .key = 9,
168
+ .source = .system,
169
+ .max_pending = 1,
170
+ .on_event = Fx.audioCaptureMsg(.capture),
171
+ });
172
+ _ = try takeCapture(&fx, .started);
173
+ const frame = [_]u8{ 0, 0 };
174
+ try fx.feedAudioCapture(9, 1_000_000, &frame);
175
+ try testing.expectError(error.EffectQueueFull, fx.feedAudioCapture(9, 2_000_000, &frame));
176
+ const with_drop = try takeCapture(&fx, .data);
177
+ try testing.expectEqual(@as(u32, 1), with_drop.dropped_pending);
178
+ try testing.expectEqual(@as(u32, 1), with_drop.dropped_total);
179
+ try fx.feedAudioCapture(9, 3_000_000, &frame);
180
+ const after_drop = try takeCapture(&fx, .data);
181
+ try testing.expectEqual(@as(u32, 0), after_drop.dropped_pending);
182
+ try testing.expectEqual(@as(u32, 1), after_drop.dropped_total);
183
+ fx.stopAudioCapture(9);
184
+ _ = try takeCapture(&fx, .stopped);
185
+ }
186
+
187
+ test "audio capture real-executor path binds and quiesces platform services" {
188
+ var host = platform.NullPlatform.init(.{});
189
+ defer host.deinit();
190
+ var host_platform = host.platform();
191
+ var fx = Fx.init(testing.allocator);
192
+ defer fx.deinit();
193
+ fx.bindServices(&host_platform.services);
194
+
195
+ fx.startAudioCapture(.{
196
+ .key = 44,
197
+ .source = .system,
198
+ .sample_rate = 24_000,
199
+ .channels = 2,
200
+ .on_event = Fx.audioCaptureMsg(.capture),
201
+ });
202
+ try testing.expectEqual(platform.AudioCaptureSource.system, (try takeCapture(&fx, .started)).source);
203
+ try testing.expect(host.audio_captures[@intFromEnum(platform.AudioCaptureSource.system)].active);
204
+
205
+ const pcm = [_]u8{ 1, 0, 2, 0 };
206
+ try testing.expectEqual(platform.AudioCapturePushResult.accepted, host.pushAudioCapture(.system, 5_000_000, &pcm));
207
+ const data = try takeCapture(&fx, .data);
208
+ try testing.expectEqual(@as(u32, 1), data.frames);
209
+ try testing.expectEqual(@as(u64, 5), data.timestamp_ms);
210
+
211
+ fx.stopAudioCapture(44);
212
+ _ = try takeCapture(&fx, .stopped);
213
+ const capture = &host.audio_captures[@intFromEnum(platform.AudioCaptureSource.system)];
214
+ try testing.expect(!capture.active);
215
+ try testing.expectEqual(@as(usize, 1), capture.stop_count);
216
+ }
217
+
218
+ test "audio capture teardown quiesces every platform source exactly once" {
219
+ var host = platform.NullPlatform.init(.{});
220
+ defer host.deinit();
221
+ var host_platform = host.platform();
222
+ var fx = Fx.init(testing.allocator);
223
+ defer fx.deinit();
224
+ fx.bindServices(&host_platform.services);
225
+
226
+ fx.startAudioCapture(.{
227
+ .key = 51,
228
+ .source = .microphone,
229
+ .on_event = Fx.audioCaptureMsg(.capture),
230
+ });
231
+ fx.startAudioCapture(.{
232
+ .key = 52,
233
+ .source = .system,
234
+ .on_event = Fx.audioCaptureMsg(.capture),
235
+ });
236
+ try testing.expect(host.audio_captures[@intFromEnum(platform.AudioCaptureSource.microphone)].active);
237
+ try testing.expect(host.audio_captures[@intFromEnum(platform.AudioCaptureSource.system)].active);
238
+
239
+ fx.deinit();
240
+ for (&host.audio_captures) |*capture| {
241
+ try testing.expect(!capture.active);
242
+ try testing.expectEqual(@as(usize, 1), capture.stop_count);
243
+ }
244
+
245
+ // Effects teardown is idempotent: the post-platform owner defer must
246
+ // never call back into services or stop either source a second time.
247
+ fx.deinit();
248
+ for (&host.audio_captures) |*capture| {
249
+ try testing.expectEqual(@as(usize, 1), capture.stop_count);
250
+ }
251
+ }
@@ -110,6 +110,9 @@ pub const max_effect_image_bytes = runtime_effects.max_effect_image_bytes;
110
110
  pub const effect_image_blob_hash_len = runtime_effects.effect_image_blob_hash_len;
111
111
  pub const EffectChannelEvent = runtime_effects.EffectChannelEvent;
112
112
  pub const EffectChannelEventKind = runtime_effects.EffectChannelEventKind;
113
+ pub const EffectAudioCaptureEvent = runtime_effects.EffectAudioCaptureEvent;
114
+ pub const EffectAudioCaptureEventKind = runtime_effects.EffectAudioCaptureEventKind;
115
+ pub const decodeAudioCaptureChannelEvent = runtime_effects.decodeAudioCaptureChannelEvent;
113
116
  pub const ChannelHandle = runtime_effects.ChannelHandle;
114
117
  pub const max_effect_channels = runtime_effects.max_effect_channels;
115
118
  pub const max_effect_channel_bytes = runtime_effects.max_effect_channel_bytes;
@@ -22,6 +22,7 @@ test {
22
22
  _ = @import("effects_clipboard_tests.zig");
23
23
  _ = @import("effects_notification_tests.zig");
24
24
  _ = @import("effects_audio_tests.zig");
25
+ _ = @import("effects_audio_capture_tests.zig");
25
26
  _ = @import("effects_video_tests.zig");
26
27
  _ = @import("effects_image_tests.zig");
27
28
  _ = @import("effects_channel_tests.zig");