@native-sdk/cli 0.5.4 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/build/app.zig +50 -1
  2. package/build.zig +534 -3
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +3 -1
  5. package/package.json +9 -9
  6. package/packages/core/package-lock.json +2 -2
  7. package/packages/core/package.json +1 -1
  8. package/packages/core/rt/rt.zig +280 -8
  9. package/packages/core/sdk/core.ts +481 -2
  10. package/packages/core/sdk/events.ts +17 -8
  11. package/packages/core/src/checker.ts +305 -2
  12. package/packages/core/src/diagnostics.ts +12 -0
  13. package/packages/core/src/emitter.ts +599 -3
  14. package/packages/core/src/typed_ast.ts +51 -4
  15. package/packages/core/src/types.ts +34 -1
  16. package/skill-data/native-ui/SKILL.md +39 -2
  17. package/skill-data/ts-core/SKILL.md +4 -2
  18. package/src/app_runner/root.zig +32 -10
  19. package/src/automation/layout_fingerprint.zig +160 -0
  20. package/src/automation/protocol.zig +82 -33
  21. package/src/automation/root.zig +1 -0
  22. package/src/automation/server.zig +1 -1
  23. package/src/automation/snapshot.zig +68 -9
  24. package/src/automation/watcher.zig +1 -1
  25. package/src/embed/host.zig +5 -0
  26. package/src/embed/ui_host.zig +13 -0
  27. package/src/platform/linux/gtk_host.c +527 -13
  28. package/src/platform/linux/gtk_host.h +25 -0
  29. package/src/platform/linux/root.zig +280 -7
  30. package/src/platform/macos/appkit_host.h +104 -6
  31. package/src/platform/macos/appkit_host.m +1283 -66
  32. package/src/platform/macos/cef_host.mm +110 -1
  33. package/src/platform/macos/root.zig +287 -7
  34. package/src/platform/null_platform.zig +398 -4
  35. package/src/platform/null_platform_tests.zig +35 -0
  36. package/src/platform/root.zig +9 -0
  37. package/src/platform/types.zig +386 -14
  38. package/src/platform/windows/root.zig +298 -9
  39. package/src/platform/windows/webview2_host.cpp +330 -5
  40. package/src/primitives/app_manifest/validation.zig +18 -0
  41. package/src/primitives/canvas/a11y_audit.zig +33 -11
  42. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  43. package/src/primitives/canvas/drawing.zig +25 -0
  44. package/src/primitives/canvas/equality.zig +8 -0
  45. package/src/primitives/canvas/events.zig +103 -16
  46. package/src/primitives/canvas/frame.zig +125 -1
  47. package/src/primitives/canvas/layout_audit.zig +22 -5
  48. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  49. package/src/primitives/canvas/render_tests.zig +13 -10
  50. package/src/primitives/canvas/root.zig +48 -0
  51. package/src/primitives/canvas/terminal_box.zig +471 -0
  52. package/src/primitives/canvas/terminal_grid.zig +978 -0
  53. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  54. package/src/primitives/canvas/test_support.zig +1 -0
  55. package/src/primitives/canvas/tests.zig +1 -0
  56. package/src/primitives/canvas/text_tests.zig +39 -4
  57. package/src/primitives/canvas/tokens.zig +115 -12
  58. package/src/primitives/canvas/ui.zig +524 -5
  59. package/src/primitives/canvas/ui_markup.zig +195 -2
  60. package/src/primitives/canvas/ui_markup_compiled.zig +172 -1
  61. package/src/primitives/canvas/ui_markup_compiled_tests.zig +133 -8
  62. package/src/primitives/canvas/ui_markup_contract.zig +88 -5
  63. package/src/primitives/canvas/ui_markup_contract_tests.zig +39 -1
  64. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  65. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  66. package/src/primitives/canvas/ui_markup_tests.zig +149 -0
  67. package/src/primitives/canvas/ui_markup_view.zig +191 -0
  68. package/src/primitives/canvas/ui_markup_view_tests.zig +403 -19
  69. package/src/primitives/canvas/ui_schema.zig +97 -17
  70. package/src/primitives/canvas/ui_schema_tests.zig +18 -11
  71. package/src/primitives/canvas/ui_tests.zig +141 -9
  72. package/src/primitives/canvas/widget_access.zig +28 -2
  73. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  74. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  75. package/src/primitives/canvas/widget_layout.zig +59 -11
  76. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  77. package/src/primitives/canvas/widget_render.zig +272 -18
  78. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  79. package/src/primitives/canvas/widget_routing.zig +59 -7
  80. package/src/primitives/canvas/widget_runtime.zig +23 -0
  81. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  82. package/src/primitives/canvas/widget_semantics.zig +119 -11
  83. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  84. package/src/primitives/canvas/widget_tree.zig +17 -0
  85. package/src/primitives/canvas/widgets.zig +132 -0
  86. package/src/root.zig +16 -0
  87. package/src/runtime/api.zig +70 -1
  88. package/src/runtime/automation_commands.zig +20 -1
  89. package/src/runtime/automation_snapshot.zig +25 -1
  90. package/src/runtime/automation_widget_dispatch.zig +73 -8
  91. package/src/runtime/bridge_payload.zig +1 -0
  92. package/src/runtime/canvas_frame.zig +62 -7
  93. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  94. package/src/runtime/canvas_frame_helpers.zig +190 -1
  95. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  96. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  97. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  98. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  99. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  100. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  101. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  102. package/src/runtime/canvas_widget_display.zig +1 -1
  103. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  104. package/src/runtime/canvas_widget_events.zig +240 -24
  105. package/src/runtime/canvas_widget_runtime.zig +213 -29
  106. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  107. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  108. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  109. package/src/runtime/canvas_widget_state.zig +6 -0
  110. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  111. package/src/runtime/core.zig +73 -2
  112. package/src/runtime/effects.zig +6389 -54
  113. package/src/runtime/effects_channel_tests.zig +3519 -0
  114. package/src/runtime/effects_pty_tests.zig +1362 -0
  115. package/src/runtime/effects_tests.zig +14 -9
  116. package/src/runtime/effects_video_tests.zig +3609 -0
  117. package/src/runtime/flow.zig +25 -4
  118. package/src/runtime/gpu_surface_events.zig +473 -13
  119. package/src/runtime/media_surface.zig +116 -11
  120. package/src/runtime/media_surface_tests.zig +61 -0
  121. package/src/runtime/pty.zig +1252 -0
  122. package/src/runtime/pty_windows.zig +1780 -0
  123. package/src/runtime/root.zig +25 -0
  124. package/src/runtime/runtime_event_tests.zig +20 -1
  125. package/src/runtime/session_journal.zig +367 -61
  126. package/src/runtime/session_record.zig +16 -0
  127. package/src/runtime/session_replay.zig +395 -11
  128. package/src/runtime/session_tests.zig +109 -15
  129. package/src/runtime/terminal_session.zig +1569 -0
  130. package/src/runtime/terminal_session_tests.zig +450 -0
  131. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  132. package/src/runtime/terminal_vt_stub.zig +13 -0
  133. package/src/runtime/tests.zig +6 -0
  134. package/src/runtime/ts_core_host.zig +980 -140
  135. package/src/runtime/ts_core_host_tests.zig +1059 -1
  136. package/src/runtime/ts_ui_app.zig +16 -5
  137. package/src/runtime/ui_app.zig +2333 -63
  138. package/src/runtime/ui_app_tests.zig +3171 -744
  139. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  140. package/src/runtime/ui_app_window_tests.zig +268 -0
  141. package/src/runtime/view.zig +190 -2
  142. package/src/runtime/view_widget_scroll.zig +218 -68
  143. package/src/runtime/view_widget_text.zig +11 -2
  144. package/src/runtime/view_widget_tree.zig +111 -3
  145. package/src/runtime/window_storage.zig +45 -2
  146. package/src/runtime/window_view_runtime.zig +34 -2
  147. package/src/runtime/window_views.zig +1 -0
  148. package/src/terminal_session_tests_root.zig +15 -0
  149. package/src/tooling/templates.zig +25 -7
@@ -218,6 +218,12 @@ const MAX_SPAWN_ARGV_BYTES = 2048;
218
218
  const MAX_SPAWN_STDIN_BYTES = 4096;
219
219
  const MAX_AUDIO_PATH_BYTES = 1024;
220
220
  const MAX_IMAGE_PATH_BYTES = 1024;
221
+ const MAX_VIDEO_PATH_BYTES = 1024;
222
+ const MAX_PTY_TERM_BYTES = 32;
223
+ const MAX_PTY_WRITE_BYTES = 4096;
224
+ /// Terminal grids ride the wire as f64 subset numbers; the engine's
225
+ /// transport is u16 — the largest grid a pty can declare.
226
+ const MAX_PTY_DIMENSION = 65535;
221
227
 
222
228
  /// The closed `Cmd.fetch` verb set, wire value = position.
223
229
  const FETCH_METHODS = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"];
@@ -227,6 +233,11 @@ const FETCH_METHODS = ["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"];
227
233
  /// the app's own).
228
234
  const AUDIO_STATES = ["loaded", "position", "completed", "failed", "rejected", "spectrum"];
229
235
 
236
+ /// The video event states an event arm's `state` union must carry — the
237
+ /// engine's event vocabulary, matched by member NAME (declaration order is
238
+ /// the app's own).
239
+ const VIDEO_STATES = ["loaded", "position", "completed", "failed", "rejected"];
240
+
230
241
  /// The image load result states an event arm's `state` union must carry —
231
242
  /// the engine's outcome vocabulary, matched by member NAME (declaration
232
243
  /// order is the app's own).
@@ -236,6 +247,21 @@ const IMAGE_STATES = [
236
247
  "unsupported", "decode_failed", "registry_full", "alloc_failed",
237
248
  ];
238
249
 
250
+ /// The external-source channel event states an event arm's `state`
251
+ /// union must carry — the engine's vocabulary, matched by member NAME
252
+ /// (declaration order is the app's own).
253
+ const CHANNEL_STATES = ["data", "closed", "rejected"];
254
+
255
+ /// The pty session event states an event arm's `state` union must carry
256
+ /// — the engine's vocabulary, matched by member NAME (declaration order
257
+ /// is the app's own).
258
+ const PTY_STATES = ["output", "exit"];
259
+
260
+ /// The pty exit reasons an event arm's `reason` union must carry — the
261
+ /// spawn exit vocabulary, matched by member NAME (declaration order is
262
+ /// the app's own).
263
+ const PTY_EXIT_REASONS = ["exited", "signaled", "cancelled", "rejected", "spawn_failed"];
264
+
239
265
  /// Names the emitted module's own fixtures occupy (header helpers + commit
240
266
  /// machinery): module-level claims and function locals unique around them.
241
267
  const emittedFixtureNames = [
@@ -256,6 +282,17 @@ const AUDIO_VERBS: Record<string, string> = {
256
282
  audioSetVolume: ".volume",
257
283
  };
258
284
 
285
+ /// The `Cmd.video*` control verbs and their zig enum literals.
286
+ const VIDEO_VERBS: Record<string, string> = {
287
+ videoPlay: ".play",
288
+ videoPause: ".pause",
289
+ videoStop: ".stop",
290
+ videoSeek: ".seek",
291
+ videoSetVolume: ".volume",
292
+ videoSetMuted: ".muted",
293
+ videoSetLoop: ".loop",
294
+ };
295
+
259
296
  export class Emitter {
260
297
  private readonly out: string[] = [];
261
298
 
@@ -1137,8 +1174,47 @@ export class Emitter {
1137
1174
  const name = this.moduleNameOf(decl) ?? decl.name.text;
1138
1175
  // The dead-state opt-out list is transpiler config, not module data:
1139
1176
  // already validated and routed into the Model/Msg `view_unbound`
1140
- // declarations (viewUnboundNames), nothing to emit here.
1177
+ // declarations (viewUnboundNames), nothing to emit here. The split
1178
+ // modelUnbound/msgUnbound pair spells the same facts for contract
1179
+ // consumers that read declarations per side; this lane reads
1180
+ // viewUnbound only, so the pair passes through unemitted — but the
1181
+ // names stay RESERVED contract vocabulary: a data const under either
1182
+ // spelling would emit no declaration while its references still
1183
+ // emit, so anything but the list shape refuses here.
1141
1184
  if (name === "viewUnbound") continue;
1185
+ if (name === "modelUnbound" || name === "msgUnbound") {
1186
+ let init = decl.initializer;
1187
+ while (ts.isParenthesizedExpression(init) || ts.isAsExpression(init) || ts.isSatisfiesExpression(init)) init = init.expression;
1188
+ if (!ts.isArrayLiteralExpression(init) || init.elements.some((el) => !ts.isStringLiteral(el))) {
1189
+ this.fail(decl, `\`${name}\` is the contract's unbound-list vocabulary and must be a const array of string literals; rename the constant to keep it as module data`, "NS1032");
1190
+ }
1191
+ // The split pair RESTATES viewUnbound's facts for consumers
1192
+ // that read declarations per side, so its entries must resolve
1193
+ // and the resolved sets must be one set — two spellings of one
1194
+ // fact can never disagree.
1195
+ const entries = init.elements.map((el) => (el as ts.StringLiteral).text);
1196
+ const resolved = name === "modelUnbound" ? this.unbound.model : this.unbound.msg;
1197
+ const side = name === "modelUnbound" ? "Model field or exported model helper" : "Msg kind";
1198
+ for (const entry of entries) {
1199
+ if (!resolved.includes(entry)) {
1200
+ this.fail(
1201
+ decl,
1202
+ `\`${name}\` names \`"${entry}"\`, which \`viewUnbound\` does not resolve as a ${side} — the split pair restates viewUnbound's facts, so declare the name there (and as a real ${side}) first`,
1203
+ "NS1032",
1204
+ );
1205
+ }
1206
+ }
1207
+ for (const entry of resolved) {
1208
+ if (!entries.includes(entry)) {
1209
+ this.fail(
1210
+ decl,
1211
+ `\`${name}\` is missing \`"${entry}"\`, which \`viewUnbound\` declares on this side — the split pair restates viewUnbound's facts, never a subset of them`,
1212
+ "NS1032",
1213
+ );
1214
+ }
1215
+ }
1216
+ continue;
1217
+ }
1142
1218
  // The env override channel: a declarative table the generated wiring
1143
1219
  // walks at comptime (read the named variables once at launch,
1144
1220
  // dispatch each present value through its bytes arm).
@@ -1557,7 +1633,7 @@ export class Emitter {
1557
1633
 
1558
1634
  // ------------------------------------------------------------------ types
1559
1635
 
1560
- private emitStruct(decl: ts.InterfaceDeclaration | ts.ClassDeclaration): void {
1636
+ private emitStruct(decl: ts.InterfaceDeclaration | ts.ClassDeclaration | ts.TypeAliasDeclaration): void {
1561
1637
  if (!decl.name) return;
1562
1638
  const info = this.table.structs.get(decl.name.text);
1563
1639
  if (!info) return;
@@ -1885,6 +1961,13 @@ export class Emitter {
1885
1961
  this.out.push(`};`);
1886
1962
  return;
1887
1963
  }
1964
+ const st = this.table.structs.get(name);
1965
+ if (st && st.decl === decl) {
1966
+ // An object-literal alias is a struct like an interface (the alias
1967
+ // form is a contract projection's value-storage spelling).
1968
+ this.emitStruct(decl);
1969
+ return;
1970
+ }
1888
1971
  const target = this.table.plainAliases.get(name);
1889
1972
  if (target) {
1890
1973
  this.out.push(`${pub}const ${name} = ${target};`);
@@ -2295,9 +2378,65 @@ export class Emitter {
2295
2378
  const id = this.emitExpr(idArg, ctx, { k: "f64" }).code;
2296
2379
  return `rt.cmdImageUnregister(${id})`;
2297
2380
  }
2381
+ if (method === "channelOpen") {
2382
+ return this.emitChannelOpenCmd(e, ctx);
2383
+ }
2384
+ if (method === "channelClose") {
2385
+ const keyArg = e.arguments[0];
2386
+ if (!keyArg) this.fail(e, "`Cmd.channelClose` key (the app's numeric channel key)", "NS1027");
2387
+ // The same literal gate as imageCancel: a key no channel could
2388
+ // ever open under has nothing to close — stop the build instead
2389
+ // of shipping a certain runtime no-op. Dynamic keys stay the
2390
+ // host's (an unknown key is the documented no-op).
2391
+ const keyLiteral = this.numberLiteralValue(keyArg);
2392
+ if (keyLiteral !== null && !(Number.isSafeInteger(keyLiteral) && keyLiteral >= 1)) {
2393
+ this.fail(keyArg, `\`Cmd.channelClose\` key ${keyLiteral} is not a positive integer channel key below 2^53`, "NS1030");
2394
+ }
2395
+ const key = this.emitExpr(keyArg, ctx, { k: "f64" }).code;
2396
+ return `rt.cmdChannelClose(${key})`;
2397
+ }
2398
+ if (method === "ptySpawn") {
2399
+ return this.emitPtySpawnCmd(e, ctx);
2400
+ }
2401
+ if (method === "ptyWrite") {
2402
+ const keyArg = e.arguments[0];
2403
+ if (!keyArg || !ts.isStringLiteral(keyArg)) {
2404
+ this.fail(e, `\`Cmd.ptyWrite\` takes its key as a string literal`, "NS1027");
2405
+ }
2406
+ if (utf8ByteLength(keyArg.text) > 255) this.fail(keyArg, "Cmd.ptyWrite key over 255 bytes");
2407
+ // Keystrokes and pastes, not bulk transfers: a compile-time-known
2408
+ // payload over the engine's per-write bound stops the build;
2409
+ // dynamic bytes stay the engine's, counted into droppedWrites.
2410
+ const bytes = this.effectBytesArg(e, e.arguments[1], "Cmd.ptyWrite bytes", MAX_PTY_WRITE_BYTES, ctx);
2411
+ return `rt.cmdPtyWrite("${escapeZigString(keyArg.text)}", ${bytes})`;
2412
+ }
2413
+ if (method === "ptyResize") {
2414
+ const keyArg = e.arguments[0];
2415
+ if (!keyArg || !ts.isStringLiteral(keyArg)) {
2416
+ this.fail(e, `\`Cmd.ptyResize\` takes its key as a string literal`, "NS1027");
2417
+ }
2418
+ if (utf8ByteLength(keyArg.text) > 255) this.fail(keyArg, "Cmd.ptyResize key over 255 bytes");
2419
+ const cols = this.ptyDimensionArg(e, e.arguments[1], "Cmd.ptyResize", "cols", ctx);
2420
+ const rows = this.ptyDimensionArg(e, e.arguments[2], "Cmd.ptyResize", "rows", ctx);
2421
+ return `rt.cmdPtyResize("${escapeZigString(keyArg.text)}", ${cols}, ${rows})`;
2422
+ }
2423
+ if (method === "ptyKill") {
2424
+ const keyArg = e.arguments[0];
2425
+ if (!keyArg || !ts.isStringLiteral(keyArg)) {
2426
+ this.fail(e, `\`Cmd.ptyKill\` takes its key as a string literal`, "NS1027");
2427
+ }
2428
+ if (utf8ByteLength(keyArg.text) > 255) this.fail(keyArg, "Cmd.ptyKill key over 255 bytes");
2429
+ return `rt.cmdPtyKill("${escapeZigString(keyArg.text)}")`;
2430
+ }
2298
2431
  if (method in AUDIO_VERBS) {
2299
2432
  return this.emitAudioCtlCmd(e, method, ctx);
2300
2433
  }
2434
+ if (method === "videoLoad") {
2435
+ return this.emitVideoLoadCmd(e, ctx);
2436
+ }
2437
+ if (method in VIDEO_VERBS) {
2438
+ return this.emitVideoCtlCmd(e, method, ctx);
2439
+ }
2301
2440
  if (method === "showWindow") {
2302
2441
  // Window labels are declarations (app.zon / the scene), so the
2303
2442
  // verb takes the label as a string literal — the same discipline
@@ -2322,7 +2461,7 @@ export class Emitter {
2322
2461
  }
2323
2462
  this.fail(
2324
2463
  e,
2325
- `Cmd.${method} (the v3 command set is none, persist, now, host, request, cancel, readFile, writeFile, fetch, clipboardWrite, clipboardRead, delay, spawn, audioPlay, audioPause, audioResume, audioStop, audioSeek, audioSetVolume, showWindow, quitApp, imageLoad, imageCancel, imageUnregister, batch)`,
2464
+ `Cmd.${method} (the v3 command set is none, persist, now, host, request, cancel, readFile, writeFile, fetch, clipboardWrite, clipboardRead, delay, spawn, audioPlay, audioPause, audioResume, audioStop, audioSeek, audioSetVolume, videoLoad, videoPlay, videoPause, videoStop, videoSeek, videoSetVolume, videoSetMuted, videoSetLoop, showWindow, quitApp, imageLoad, imageCancel, imageUnregister, channelOpen, channelClose, ptySpawn, ptyWrite, ptyResize, ptyKill, batch)`,
2326
2465
  );
2327
2466
  }
2328
2467
  this.fail(expr, "command expression (Cmd values are built inline from the Cmd.* factories)");
@@ -2737,6 +2876,263 @@ export class Emitter {
2737
2876
  return `@intFromEnum(std.meta.Tag(${unionName}).${zigId(arg.text)})`;
2738
2877
  }
2739
2878
 
2879
+ /// `Cmd.channelOpen(key, route)`: the app's numeric channel key (any
2880
+ /// number expression — keys are model data) and the `{ event }` routing
2881
+ /// whose arm carries the five SDK-fixed channel event fields.
2882
+ private emitChannelOpenCmd(e: ts.CallExpression, ctx: Ctx): string {
2883
+ const keyArg = e.arguments[0];
2884
+ if (!keyArg) this.fail(e, "`Cmd.channelOpen` key (the app's numeric channel key)", "NS1027");
2885
+ // The image-id literal gate: a compile-time-known key the host is
2886
+ // certain to refuse stops the build; dynamic keys stay the host's
2887
+ // to validate through the "rejected" event state. Strictly BELOW
2888
+ // 2^53 — the first f64 that aliases a neighbor on the wire.
2889
+ const keyLiteral = this.numberLiteralValue(keyArg);
2890
+ if (keyLiteral !== null && !(Number.isSafeInteger(keyLiteral) && keyLiteral >= 1)) {
2891
+ this.fail(keyArg, `\`Cmd.channelOpen\` key ${keyLiteral} is not a positive integer channel key below 2^53`, "NS1030");
2892
+ }
2893
+ const key = this.emitExpr(keyArg, ctx, { k: "f64" }).code;
2894
+
2895
+ let route = e.arguments[1];
2896
+ while (route && (ts.isParenthesizedExpression(route) || ts.isAsExpression(route) || ts.isSatisfiesExpression(route))) route = route.expression;
2897
+ if (!route || !ts.isObjectLiteralExpression(route)) {
2898
+ this.fail(e.arguments[1] ?? e, `\`Cmd.channelOpen\` routing is an inline \`{ event }\` object`, "NS1027");
2899
+ }
2900
+ let event: ts.StringLiteral | null = null;
2901
+ for (const p of route.properties) {
2902
+ if (!ts.isPropertyAssignment(p) || !ts.isIdentifier(p.name)) {
2903
+ this.fail(p, `\`Cmd.channelOpen\` routing member \`${p.getText()}\` is not a plain property`, "NS1027");
2904
+ }
2905
+ let v: ts.Expression = p.initializer;
2906
+ while (ts.isParenthesizedExpression(v) || ts.isAsExpression(v) || ts.isSatisfiesExpression(v)) v = v.expression;
2907
+ if (p.name.text !== "event") {
2908
+ this.fail(p, `\`Cmd.channelOpen\` routing member \`${p.name.text}\``, "NS1027");
2909
+ }
2910
+ if (!ts.isStringLiteral(v)) {
2911
+ this.fail(p.initializer, `\`Cmd.channelOpen\` event arm is not a string literal`, "NS1027");
2912
+ }
2913
+ event = v;
2914
+ }
2915
+ if (!event) this.fail(route, `\`Cmd.channelOpen\` routing without an \`event\` arm`, "NS1027");
2916
+ const tag = this.channelEventArmTag(event, ctx);
2917
+ return `rt.cmdChannelOpen(${key}, ${tag})`;
2918
+ }
2919
+
2920
+ /// Resolve the channel event arm: exactly the five SDK-fixed fields,
2921
+ /// matched by NAME — key (a number: the channel key echoed verbatim),
2922
+ /// state (a named literal-union alias carrying exactly the three
2923
+ /// channel states, any order), bytes (the post payload),
2924
+ /// droppedPending/droppedTotal (numbers).
2925
+ private channelEventArmTag(arg: ts.StringLiteral, ctx: Ctx): string {
2926
+ const unionName = ctx.cmdReturn!.msgUnion;
2927
+ const info = this.table.unions.get(unionName);
2928
+ if (!info) this.fail(arg, `unknown union ${unionName}`);
2929
+ const arm = info.arms.find((a) => a.tag === arg.text);
2930
+ if (!arm) {
2931
+ this.fail(arg, `routing target \`${arg.text}\` is not an arm of ${unionName}`, "NS1027");
2932
+ }
2933
+ const shape =
2934
+ "the five channel event fields — key: number (the echoed channel key), state (a named alias of exactly " +
2935
+ CHANNEL_STATES.map((s) => `"${s}"`).join(" | ") +
2936
+ "), bytes: Uint8Array, droppedPending: number, droppedTotal: number";
2937
+ const fieldsByName = new Map(arm.fields.map((f) => [f.tsName, f]));
2938
+ const isNumber = (k: string): boolean => k === "number" || k === "i64" || k === "f64";
2939
+ const key = fieldsByName.get("key");
2940
+ const state = fieldsByName.get("state");
2941
+ const bytes = fieldsByName.get("bytes");
2942
+ const droppedPending = fieldsByName.get("droppedPending");
2943
+ const droppedTotal = fieldsByName.get("droppedTotal");
2944
+ const stateOk =
2945
+ state !== undefined &&
2946
+ state.type.k === "enum" &&
2947
+ state.type.members.length === CHANNEL_STATES.length &&
2948
+ CHANNEL_STATES.every((s) => state.type.k === "enum" && state.type.members.includes(s));
2949
+ const matches =
2950
+ arm.fields.length === 5 &&
2951
+ stateOk &&
2952
+ key !== undefined &&
2953
+ isNumber(key.type.k) &&
2954
+ bytes !== undefined &&
2955
+ bytes.type.k === "bytes" &&
2956
+ droppedPending !== undefined &&
2957
+ isNumber(droppedPending.type.k) &&
2958
+ droppedTotal !== undefined &&
2959
+ isNumber(droppedTotal.type.k);
2960
+ if (!matches) {
2961
+ this.fail(arg, `routing target \`${arg.text}\` does not carry ${shape}`, "NS1027");
2962
+ }
2963
+ return `@intFromEnum(std.meta.Tag(${unionName}).${zigId(arg.text)})`;
2964
+ }
2965
+
2966
+ /// `Cmd.ptySpawn(argv, route)`: an inline argv array of bytes elements
2967
+ /// (the spawn gates verbatim — same engine budgets, different transport)
2968
+ /// and a `{ key?, cols?, rows?, term?, event }` routing object whose arm
2969
+ /// carries the six SDK-fixed pty event fields.
2970
+ private emitPtySpawnCmd(e: ts.CallExpression, ctx: Ctx): string {
2971
+ let argvArg = e.arguments[0];
2972
+ while (argvArg && (ts.isParenthesizedExpression(argvArg) || ts.isAsExpression(argvArg) || ts.isSatisfiesExpression(argvArg))) {
2973
+ argvArg = argvArg.expression;
2974
+ }
2975
+ if (!argvArg || !ts.isArrayLiteralExpression(argvArg) || argvArg.elements.some((el) => ts.isSpreadElement(el))) {
2976
+ this.fail(
2977
+ e.arguments[0] ?? e,
2978
+ `\`Cmd.ptySpawn\` argv is an inline array literal of bytes elements ([asciiBytes("/bin/zsh"), ...])`,
2979
+ "NS1029",
2980
+ );
2981
+ }
2982
+ if (argvArg.elements.length === 0) {
2983
+ this.fail(argvArg, `\`Cmd.ptySpawn\` argv is empty; the engine needs at least the program name`, "NS1030");
2984
+ }
2985
+ if (argvArg.elements.length > MAX_SPAWN_ARGV) {
2986
+ this.fail(
2987
+ argvArg,
2988
+ `\`Cmd.ptySpawn\` argv carries ${argvArg.elements.length} elements; the engine bound is ${MAX_SPAWN_ARGV}`,
2989
+ "NS1030",
2990
+ );
2991
+ }
2992
+ const args = argvArg.elements.map((el) => this.effectBytesArg(e, el, "Cmd.ptySpawn argv element", null, ctx));
2993
+ // The 2 KiB bound is on the WHOLE argv block; stop the build only when
2994
+ // every element's byte length is knowable (dynamic elements stay the
2995
+ // engine's to validate through the event arm's "rejected" exit).
2996
+ const literalLens = argvArg.elements.map((el) => this.literalBytesLength(el));
2997
+ if (literalLens.every((len) => len !== null)) {
2998
+ const total = literalLens.reduce((n: number, len) => n + (len ?? 0), 0);
2999
+ if (total > MAX_SPAWN_ARGV_BYTES) {
3000
+ this.fail(argvArg, `\`Cmd.ptySpawn\` argv is ${total} bytes; the engine bound is ${MAX_SPAWN_ARGV_BYTES}`, "NS1030");
3001
+ }
3002
+ }
3003
+
3004
+ let route = e.arguments[1];
3005
+ while (route && (ts.isParenthesizedExpression(route) || ts.isAsExpression(route) || ts.isSatisfiesExpression(route))) route = route.expression;
3006
+ if (!route || !ts.isObjectLiteralExpression(route)) {
3007
+ this.fail(
3008
+ e.arguments[1] ?? e,
3009
+ `\`Cmd.ptySpawn\` routing is an inline \`{ key?, cols?, rows?, term?, event }\` object`,
3010
+ "NS1027",
3011
+ );
3012
+ }
3013
+ let key = "";
3014
+ let cols = "80";
3015
+ let rows = "24";
3016
+ let term = "";
3017
+ let event: ts.StringLiteral | null = null;
3018
+ for (const p of route.properties) {
3019
+ if (!ts.isPropertyAssignment(p) || !ts.isIdentifier(p.name)) {
3020
+ this.fail(p, `\`Cmd.ptySpawn\` routing member \`${p.getText()}\` is not a plain property`, "NS1027");
3021
+ }
3022
+ const name = p.name.text;
3023
+ let v: ts.Expression = p.initializer;
3024
+ while (ts.isParenthesizedExpression(v) || ts.isAsExpression(v) || ts.isSatisfiesExpression(v)) v = v.expression;
3025
+ if (name === "cols") {
3026
+ cols = this.ptyDimensionArg(e, p.initializer, "Cmd.ptySpawn", "cols", ctx);
3027
+ continue;
3028
+ }
3029
+ if (name === "rows") {
3030
+ rows = this.ptyDimensionArg(e, p.initializer, "Cmd.ptySpawn", "rows", ctx);
3031
+ continue;
3032
+ }
3033
+ if (!ts.isStringLiteral(v)) {
3034
+ this.fail(
3035
+ p.initializer,
3036
+ `\`Cmd.ptySpawn\` routing value \`${p.initializer.getText()}\` is not a string literal`,
3037
+ "NS1027",
3038
+ );
3039
+ }
3040
+ if (name === "key") {
3041
+ if (utf8ByteLength(v.text) > 255) this.fail(v, "Cmd.ptySpawn key over 255 bytes");
3042
+ key = v.text;
3043
+ } else if (name === "term") {
3044
+ // TERM values are declarations like effect keys; an empty one
3045
+ // has nothing to name and the engine bound is small.
3046
+ if (v.text.length === 0) this.fail(v, `\`Cmd.ptySpawn\` term is empty (omit it for the engine default)`, "NS1030");
3047
+ if (utf8ByteLength(v.text) > MAX_PTY_TERM_BYTES) {
3048
+ this.fail(v, `\`Cmd.ptySpawn\` term is over ${MAX_PTY_TERM_BYTES} bytes`, "NS1030");
3049
+ }
3050
+ term = v.text;
3051
+ } else if (name === "event") event = v;
3052
+ else this.fail(p, `\`Cmd.ptySpawn\` routing member \`${name}\``, "NS1027");
3053
+ }
3054
+ if (!event) this.fail(route, `\`Cmd.ptySpawn\` routing without an \`event\` arm`, "NS1027");
3055
+ const tag = this.ptyEventArmTag(event, ctx);
3056
+ return (
3057
+ `rt.cmdPtySpawn("${escapeZigString(key)}", ${tag}, ${cols}, ${rows}, ` +
3058
+ `"${escapeZigString(term)}", &.{ ${args.join(", ")} })`
3059
+ );
3060
+ }
3061
+
3062
+ /// A terminal grid dimension (ptySpawn cols/rows, ptyResize): a
3063
+ /// compile-time-known value outside the transport's 1..65535 integer
3064
+ /// range stops the build; dynamic values stay the host's (an
3065
+ /// unrepresentable spawn grid is one "rejected" exit, an
3066
+ /// unrepresentable resize a documented no-op).
3067
+ private ptyDimensionArg(call: ts.CallExpression, arg: ts.Expression | undefined, factory: string, field: string, ctx: Ctx): string {
3068
+ if (!arg) this.fail(call, `${factory} ${field} (a terminal dimension)`, "NS1029");
3069
+ const literal = this.numberLiteralValue(arg);
3070
+ if (literal !== null && !(Number.isInteger(literal) && literal >= 1 && literal <= MAX_PTY_DIMENSION)) {
3071
+ this.fail(arg, `\`${factory}\` ${field} ${literal} is not a terminal dimension (an integer in 1..${MAX_PTY_DIMENSION})`, "NS1030");
3072
+ }
3073
+ return this.emitExpr(arg, ctx, { k: "f64" }).code;
3074
+ }
3075
+
3076
+ /// Resolve the pty event arm: exactly the seven SDK-fixed fields,
3077
+ /// matched by NAME — key (the app's own session key, so two sessions
3078
+ /// on one arm are distinguishable), state (a named literal-union alias
3079
+ /// carrying exactly the two pty states, any order), bytes (the output
3080
+ /// batch), code (number), reason (a named alias carrying exactly the
3081
+ /// five spawn exit reasons, any order), signal and droppedWrites
3082
+ /// (numbers).
3083
+ private ptyEventArmTag(arg: ts.StringLiteral, ctx: Ctx): string {
3084
+ const unionName = ctx.cmdReturn!.msgUnion;
3085
+ const info = this.table.unions.get(unionName);
3086
+ if (!info) this.fail(arg, `unknown union ${unionName}`);
3087
+ const arm = info.arms.find((a) => a.tag === arg.text);
3088
+ if (!arm) {
3089
+ this.fail(arg, `routing target \`${arg.text}\` is not an arm of ${unionName}`, "NS1027");
3090
+ }
3091
+ const shape =
3092
+ "the seven pty event fields — key: string, state (a named alias of exactly " +
3093
+ PTY_STATES.map((s) => `"${s}"`).join(" | ") +
3094
+ "), bytes: Uint8Array, code: number, reason (a named alias of exactly " +
3095
+ PTY_EXIT_REASONS.map((s) => `"${s}"`).join(" | ") +
3096
+ "), signal: number, droppedWrites: number";
3097
+ const fieldsByName = new Map(arm.fields.map((f) => [f.tsName, f]));
3098
+ const isNumber = (k: string): boolean => k === "number" || k === "i64" || k === "f64";
3099
+ const key = fieldsByName.get("key");
3100
+ const state = fieldsByName.get("state");
3101
+ const bytes = fieldsByName.get("bytes");
3102
+ const code = fieldsByName.get("code");
3103
+ const reason = fieldsByName.get("reason");
3104
+ const signal = fieldsByName.get("signal");
3105
+ const droppedWrites = fieldsByName.get("droppedWrites");
3106
+ const stateOk =
3107
+ state !== undefined &&
3108
+ state.type.k === "enum" &&
3109
+ state.type.members.length === PTY_STATES.length &&
3110
+ PTY_STATES.every((s) => state.type.k === "enum" && state.type.members.includes(s));
3111
+ const reasonOk =
3112
+ reason !== undefined &&
3113
+ reason.type.k === "enum" &&
3114
+ reason.type.members.length === PTY_EXIT_REASONS.length &&
3115
+ PTY_EXIT_REASONS.every((s) => reason.type.k === "enum" && reason.type.members.includes(s));
3116
+ const matches =
3117
+ arm.fields.length === 7 &&
3118
+ key !== undefined &&
3119
+ key.type.k === "string" &&
3120
+ stateOk &&
3121
+ reasonOk &&
3122
+ bytes !== undefined &&
3123
+ bytes.type.k === "bytes" &&
3124
+ code !== undefined &&
3125
+ isNumber(code.type.k) &&
3126
+ signal !== undefined &&
3127
+ isNumber(signal.type.k) &&
3128
+ droppedWrites !== undefined &&
3129
+ isNumber(droppedWrites.type.k);
3130
+ if (!matches) {
3131
+ this.fail(arg, `routing target \`${arg.text}\` does not carry ${shape}`, "NS1027");
3132
+ }
3133
+ return `@intFromEnum(std.meta.Tag(${unionName}).${zigId(arg.text)})`;
3134
+ }
3135
+
2740
3136
  /// Resolve the audio event arm: exactly the six SDK-fixed fields, matched
2741
3137
  /// by NAME — state (a named literal-union alias carrying exactly the six
2742
3138
  /// audio states, any order), positionMs/durationMs (numbers), playing/
@@ -2815,6 +3211,192 @@ export class Emitter {
2815
3211
  return `rt.cmdAudioCtl("${escapeZigString(keyArg.text)}", ${AUDIO_VERBS[method]}, ${value})`;
2816
3212
  }
2817
3213
 
3214
+ /// `Cmd.videoLoad(key, source, route)`: a string-literal key, an inline
3215
+ /// `{ surface, path?, url?, autoplay?, loop?, muted? }` source (at least
3216
+ /// one of path/url; the option booleans are literals — they encode into
3217
+ /// the wire record's flags byte at build time), and `{ event }` routing
3218
+ /// onto the seven-field video event arm.
3219
+ private emitVideoLoadCmd(e: ts.CallExpression, ctx: Ctx): string {
3220
+ const keyArg = e.arguments[0];
3221
+ if (!keyArg || !ts.isStringLiteral(keyArg)) {
3222
+ this.fail(e, `\`Cmd.videoLoad\` takes its key as a string literal`, "NS1027");
3223
+ }
3224
+ if (utf8ByteLength(keyArg.text) > 255) this.fail(keyArg, "Cmd.videoLoad key over 255 bytes");
3225
+
3226
+ let source = e.arguments[1];
3227
+ while (source && (ts.isParenthesizedExpression(source) || ts.isAsExpression(source) || ts.isSatisfiesExpression(source))) source = source.expression;
3228
+ if (!source || !ts.isObjectLiteralExpression(source)) {
3229
+ this.fail(
3230
+ e.arguments[1] ?? e,
3231
+ `\`Cmd.videoLoad\` takes an inline \`{ surface, path?, url?, autoplay?, loop?, muted? }\` source object`,
3232
+ "NS1029",
3233
+ );
3234
+ }
3235
+ let surface: string | null = null;
3236
+ let video_path = '""';
3237
+ let url = '""';
3238
+ // Wire flags: bit0 = autoplay (default on), bit1 = loop, bit2 = muted.
3239
+ let flags = 1;
3240
+ let has_source = false;
3241
+ for (const p of source.properties) {
3242
+ if (!ts.isPropertyAssignment(p) || !ts.isIdentifier(p.name)) {
3243
+ this.fail(p, `\`Cmd.videoLoad\` source member \`${p.getText()}\` is not a plain property`, "NS1029");
3244
+ }
3245
+ const name = p.name.text;
3246
+ if (name === "surface") {
3247
+ // A compile-time-known surface id the engine is certain to
3248
+ // refuse stops the build (the imageLoad id gate: 0 is the
3249
+ // no-surface sentinel, and past 2^53 the f64 wire cannot carry
3250
+ // the id exactly). Dynamic ids stay the host's to validate
3251
+ // through the "rejected" event state.
3252
+ const literal = this.numberLiteralValue(p.initializer);
3253
+ if (literal !== null && !(Number.isSafeInteger(literal) && literal >= 1)) {
3254
+ this.fail(p.initializer, `\`Cmd.videoLoad\` surface ${literal} is not a positive integer media-surface id below 2^53`, "NS1030");
3255
+ }
3256
+ surface = this.emitExpr(p.initializer, ctx, { k: "f64" }).code;
3257
+ } else if (name === "path") {
3258
+ video_path = this.effectBytesArg(e, p.initializer, "Cmd.videoLoad path", MAX_VIDEO_PATH_BYTES, ctx);
3259
+ has_source = true;
3260
+ } else if (name === "url") {
3261
+ url = this.effectBytesArg(e, p.initializer, "Cmd.videoLoad url", MAX_VIDEO_PATH_BYTES, ctx);
3262
+ has_source = true;
3263
+ } else if (name === "autoplay" || name === "loop" || name === "muted") {
3264
+ // The option booleans encode into the wire record's flags byte
3265
+ // at build time, so they are literals — a dynamic switch
3266
+ // belongs on the control verbs (videoSetMuted/videoSetLoop
3267
+ // drive the open stream).
3268
+ let v: ts.Expression = p.initializer;
3269
+ while (ts.isParenthesizedExpression(v) || ts.isAsExpression(v) || ts.isSatisfiesExpression(v)) v = v.expression;
3270
+ if (v.kind !== ts.SyntaxKind.TrueKeyword && v.kind !== ts.SyntaxKind.FalseKeyword) {
3271
+ this.fail(p.initializer, `\`Cmd.videoLoad\` ${name} must be the literal \`true\` or \`false\``, "NS1030");
3272
+ }
3273
+ const bit = name === "autoplay" ? 1 : name === "loop" ? 2 : 4;
3274
+ flags = v.kind === ts.SyntaxKind.TrueKeyword ? flags | bit : flags & ~bit;
3275
+ } else {
3276
+ this.fail(p, `\`Cmd.videoLoad\` source member \`${name}\``, "NS1029");
3277
+ }
3278
+ }
3279
+ if (surface === null) {
3280
+ this.fail(source, `\`Cmd.videoLoad\` source without a \`surface\` — the media-surface id the decoded frames feed`, "NS1029");
3281
+ }
3282
+ if (!has_source) {
3283
+ this.fail(source, `\`Cmd.videoLoad\` source without a \`path\` or a \`url\` — nothing could play`, "NS1029");
3284
+ }
3285
+
3286
+ let route = e.arguments[2];
3287
+ while (route && (ts.isParenthesizedExpression(route) || ts.isAsExpression(route) || ts.isSatisfiesExpression(route))) route = route.expression;
3288
+ if (!route || !ts.isObjectLiteralExpression(route)) {
3289
+ this.fail(e.arguments[2] ?? e, `\`Cmd.videoLoad\` routing is an inline \`{ event }\` object`, "NS1027");
3290
+ }
3291
+ let event: ts.StringLiteral | null = null;
3292
+ for (const p of route.properties) {
3293
+ if (!ts.isPropertyAssignment(p) || !ts.isIdentifier(p.name)) {
3294
+ this.fail(p, `\`Cmd.videoLoad\` routing member \`${p.getText()}\` is not a plain property`, "NS1027");
3295
+ }
3296
+ let v: ts.Expression = p.initializer;
3297
+ while (ts.isParenthesizedExpression(v) || ts.isAsExpression(v) || ts.isSatisfiesExpression(v)) v = v.expression;
3298
+ if (p.name.text !== "event") {
3299
+ this.fail(p, `\`Cmd.videoLoad\` routing member \`${p.name.text}\``, "NS1027");
3300
+ }
3301
+ if (!ts.isStringLiteral(v)) {
3302
+ this.fail(p.initializer, `\`Cmd.videoLoad\` event arm is not a string literal`, "NS1027");
3303
+ }
3304
+ event = v;
3305
+ }
3306
+ if (!event) this.fail(route, `\`Cmd.videoLoad\` routing without an \`event\` arm`, "NS1027");
3307
+ const tag = this.videoEventArmTag(event, ctx);
3308
+ return `rt.cmdVideoLoad("${escapeZigString(keyArg.text)}", ${tag}, ${surface}, ${video_path}, ${url}, ${flags})`;
3309
+ }
3310
+
3311
+ /// Resolve the video event arm: exactly the seven SDK-fixed fields,
3312
+ /// matched by NAME — state (a named literal-union alias carrying exactly
3313
+ /// the five video states, any order), positionMs/durationMs (numbers),
3314
+ /// playing/buffering (booleans), width/height (numbers).
3315
+ private videoEventArmTag(arg: ts.StringLiteral, ctx: Ctx): string {
3316
+ const unionName = ctx.cmdReturn!.msgUnion;
3317
+ const info = this.table.unions.get(unionName);
3318
+ if (!info) this.fail(arg, `unknown union ${unionName}`);
3319
+ const arm = info.arms.find((a) => a.tag === arg.text);
3320
+ if (!arm) {
3321
+ this.fail(arg, `routing target \`${arg.text}\` is not an arm of ${unionName}`, "NS1027");
3322
+ }
3323
+ const shape =
3324
+ "the seven video event fields — state (a named alias of exactly " +
3325
+ VIDEO_STATES.map((s) => `"${s}"`).join(" | ") +
3326
+ "), positionMs: number, durationMs: number, playing: boolean, buffering: boolean, width: number, height: number";
3327
+ const fieldsByName = new Map(arm.fields.map((f) => [f.tsName, f]));
3328
+ const isNumber = (k: string): boolean => k === "number" || k === "i64" || k === "f64";
3329
+ const state = fieldsByName.get("state");
3330
+ const position = fieldsByName.get("positionMs");
3331
+ const duration = fieldsByName.get("durationMs");
3332
+ const playing = fieldsByName.get("playing");
3333
+ const buffering = fieldsByName.get("buffering");
3334
+ const width = fieldsByName.get("width");
3335
+ const height = fieldsByName.get("height");
3336
+ const stateOk =
3337
+ state !== undefined &&
3338
+ state.type.k === "enum" &&
3339
+ state.type.members.length === VIDEO_STATES.length &&
3340
+ VIDEO_STATES.every((s) => state.type.k === "enum" && state.type.members.includes(s));
3341
+ const matches =
3342
+ arm.fields.length === 7 &&
3343
+ stateOk &&
3344
+ position !== undefined &&
3345
+ isNumber(position.type.k) &&
3346
+ duration !== undefined &&
3347
+ isNumber(duration.type.k) &&
3348
+ playing !== undefined &&
3349
+ playing.type.k === "bool" &&
3350
+ buffering !== undefined &&
3351
+ buffering.type.k === "bool" &&
3352
+ width !== undefined &&
3353
+ isNumber(width.type.k) &&
3354
+ height !== undefined &&
3355
+ isNumber(height.type.k);
3356
+ if (!matches) {
3357
+ this.fail(arg, `routing target \`${arg.text}\` does not carry ${shape}`, "NS1027");
3358
+ }
3359
+ return `@intFromEnum(std.meta.Tag(${unionName}).${zigId(arg.text)})`;
3360
+ }
3361
+
3362
+ /// The `Cmd.videoPlay/Pause/Stop/Seek/SetVolume/SetMuted/SetLoop`
3363
+ /// control verbs: a string-literal key plus the seek position / volume /
3364
+ /// switch value where the verb takes one.
3365
+ private emitVideoCtlCmd(e: ts.CallExpression, method: string, ctx: Ctx): string {
3366
+ const keyArg = e.arguments[0];
3367
+ if (!keyArg || !ts.isStringLiteral(keyArg)) {
3368
+ this.fail(e, `\`Cmd.${method}\` takes its key as a string literal`, "NS1027");
3369
+ }
3370
+ if (utf8ByteLength(keyArg.text) > 255) this.fail(keyArg, `Cmd.${method} key over 255 bytes`);
3371
+ let value = "0";
3372
+ if (method === "videoSeek") {
3373
+ const msArg = e.arguments[1];
3374
+ if (!msArg) this.fail(e, "Cmd.videoSeek position (milliseconds)");
3375
+ const literal = this.numberLiteralValue(msArg);
3376
+ if (literal !== null && !(literal >= 0 && Number.isFinite(literal))) {
3377
+ this.fail(msArg, `\`Cmd.videoSeek\` position ${literal} is not a millisecond offset`, "NS1030");
3378
+ }
3379
+ value = this.emitExpr(msArg, ctx, { k: "f64" }).code;
3380
+ } else if (method === "videoSetVolume") {
3381
+ const volArg = e.arguments[1];
3382
+ if (!volArg) this.fail(e, "Cmd.videoSetVolume volume (0..1)");
3383
+ const literal = this.numberLiteralValue(volArg);
3384
+ if (literal !== null && !(literal >= 0 && literal <= 1)) {
3385
+ this.fail(volArg, `\`Cmd.videoSetVolume\` volume ${literal} is outside 0..1`, "NS1030");
3386
+ }
3387
+ value = this.emitExpr(volArg, ctx, { k: "f64" }).code;
3388
+ } else if (method === "videoSetMuted" || method === "videoSetLoop") {
3389
+ // The switch verbs take any boolean expression — applying runtime
3390
+ // state to the open stream is what they exist for (the load-time
3391
+ // literals cover the start-muted/start-looping cases). It rides
3392
+ // the value field as 0/1, the audioSeek dynamic-value discipline.
3393
+ const boolArg = e.arguments[1];
3394
+ if (!boolArg) this.fail(e, `Cmd.${method} switch (a boolean)`);
3395
+ value = `@floatFromInt(@intFromBool(${this.emitExpr(boolArg, ctx, { k: "bool" }).code}))`;
3396
+ }
3397
+ return `rt.cmdVideoCtl("${escapeZigString(keyArg.text)}", ${VIDEO_VERBS[method]}, ${value})`;
3398
+ }
3399
+
2818
3400
  /// Resolve a `Cmd.now`/`Cmd.delay`/`Sub.timer` target arm to its tag,
2819
3401
  /// requiring exactly one number payload field.
2820
3402
  private numberArmTag(arg: ts.StringLiteral, unionName: string, factory: string): string {
@@ -8365,6 +8947,20 @@ export class Emitter {
8365
8947
  // names — the overlap of the two string sets, resolved at compile time.
8366
8948
  const lt = unwrapOptional(this.zTypeOfExpr(expr.left, ctx));
8367
8949
  const rt = unwrapOptional(this.zTypeOfExpr(expr.right, ctx));
8950
+ // Layer-3 re-derivation of NS1061: identity over a by-value record
8951
+ // has no reference to compare (a pointer-stored record's `==` IS
8952
+ // its identity). This is the emission-time authority, so operands
8953
+ // that reached here through generic instantiation or erased
8954
+ // assertions stop the same way the checker teaches directly.
8955
+ for (const t of [lt, rt]) {
8956
+ if (t.k === "struct" && !this.table.isPointerStruct(t.name)) {
8957
+ this.fail(
8958
+ expr,
8959
+ `\`${op === ts.SyntaxKind.EqualsEqualsEqualsToken ? "===" : "!=="}\` compares \`${t.name}\` values by identity, which value storage does not carry — compare fields, or store the record by reference for identity`,
8960
+ "NS1061",
8961
+ );
8962
+ }
8963
+ }
8368
8964
  if (lt.k === "enum" && rt.k === "enum" && lt.name !== rt.name) {
8369
8965
  const l = this.emitExpr(expr.left, ctx).code;
8370
8966
  const r = this.emitExpr(expr.right, ctx).code;