@native-sdk/cli 0.10.1 → 0.11.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 (462) hide show
  1. package/README.md +8 -6
  2. package/bin/native.js +2 -1
  3. package/build/app.zig +320 -60
  4. package/build/corewire.zig +53 -0
  5. package/build/test_shards.zig +49 -0
  6. package/build/ts_run.mjs +3 -3
  7. package/build.zig +1796 -83
  8. package/package.json +19 -14
  9. package/packages/core/compile-surface/core.ts +134 -72
  10. package/packages/core/package-lock.json +75 -357
  11. package/packages/core/package.json +25 -4
  12. package/packages/core/scripts/compiler_command.mjs +17 -12
  13. package/packages/core/scripts/compiler_typecheck.mjs +1 -0
  14. package/packages/core/scripts/gen_declarations.mjs +3 -2
  15. package/packages/core/scripts/gen_service_surface.mjs +22 -10
  16. package/packages/core/scripts/install_scriptc.mjs +182 -0
  17. package/packages/core/scripts/invoke_scriptc.mjs +18 -0
  18. package/packages/core/scripts/merge_static_archives.mjs +6 -1
  19. package/packages/core/scripts/npm_static_spike.mjs +2 -2
  20. package/packages/core/scripts/run_external_core_compiler.mjs +22 -10
  21. package/packages/core/scripts/run_external_service_compiler.mjs +15 -15
  22. package/packages/core/scripts/run_library_compiler.mjs +51 -0
  23. package/packages/core/scripts/run_native_tests.mjs +22 -0
  24. package/packages/core/scripts/scriptc-toolchain.json +101 -0
  25. package/packages/core/scripts/scriptc_toolchain.mjs +121 -0
  26. package/packages/core/scripts/stage_external_core.mjs +57 -1
  27. package/packages/core/scripts/surface_manifest_diff.mjs +19 -12
  28. package/packages/core/sdk/bytes.d.ts +2 -0
  29. package/packages/core/sdk/bytes.ts +86 -0
  30. package/packages/core/sdk/core.d.ts +308 -4
  31. package/packages/core/sdk/core.ts +320 -84
  32. package/packages/core/sdk/events.d.ts +247 -0
  33. package/packages/core/sdk/events.ts +230 -2
  34. package/packages/core/sdk/internal/image_identity.d.ts +7 -0
  35. package/packages/core/sdk/internal/image_identity.ts +71 -0
  36. package/packages/core/sdk/internal/markdown_policy.d.ts +5 -0
  37. package/packages/core/sdk/internal/markdown_policy.ts +878 -0
  38. package/packages/core/sdk/text.d.ts +27 -0
  39. package/packages/core/sdk/text.ts +206 -15
  40. package/packages/core/sdk/theme.d.ts +251 -0
  41. package/packages/core/sdk/theme.ts +267 -0
  42. package/packages/core/src/authorization.ts +137 -0
  43. package/packages/core/src/automation_commands.ts +201 -0
  44. package/packages/core/src/automation_interactions.ts +92 -0
  45. package/packages/core/src/bridge_descriptors.ts +104 -0
  46. package/packages/core/src/builtin_coordination.ts +172 -0
  47. package/packages/core/src/canvas_frame_coordination.ts +81 -0
  48. package/packages/core/src/canvas_presentation_lifecycle.ts +84 -0
  49. package/packages/core/src/canvas_scene_observation.ts +145 -0
  50. package/packages/core/src/chart_content.ts +161 -0
  51. package/packages/core/src/chart_plans.ts +432 -0
  52. package/packages/core/src/checker.ts +166 -19
  53. package/packages/core/src/code_content.ts +580 -0
  54. package/packages/core/src/command_coordination.ts +140 -0
  55. package/packages/core/src/component_composition.ts +94 -0
  56. package/packages/core/src/component_construction.ts +149 -0
  57. package/packages/core/src/contract.ts +25 -7
  58. package/packages/core/src/control_appearance.ts +232 -0
  59. package/packages/core/src/control_commands.ts +115 -0
  60. package/packages/core/src/control_content.ts +105 -0
  61. package/packages/core/src/control_geometry.ts +107 -0
  62. package/packages/core/src/control_payloads.ts +119 -0
  63. package/packages/core/src/control_primitives.ts +134 -0
  64. package/packages/core/src/devhost.ts +82 -7
  65. package/packages/core/src/devhost_journal.mjs +30 -7
  66. package/packages/core/src/effect_plans.ts +71 -0
  67. package/packages/core/src/flow_measurement.ts +121 -0
  68. package/packages/core/src/font_registry.ts +37 -0
  69. package/packages/core/src/glyph_atlas.ts +86 -0
  70. package/packages/core/src/gpu_packet_preparation.ts +130 -0
  71. package/packages/core/src/gpu_planning.ts +123 -0
  72. package/packages/core/src/gpu_presentation.ts +55 -0
  73. package/packages/core/src/host_flow.ts +89 -0
  74. package/packages/core/src/host_input_regions.ts +165 -0
  75. package/packages/core/src/image_registry.ts +66 -0
  76. package/packages/core/src/indicator_plans.ts +132 -0
  77. package/packages/core/src/infer.ts +150 -8
  78. package/packages/core/src/input_composition.ts +116 -0
  79. package/packages/core/src/intrinsic_measure.ts +100 -0
  80. package/packages/core/src/layout_coordination.ts +207 -0
  81. package/packages/core/src/leaf_plans.ts +134 -0
  82. package/packages/core/src/markup_development.ts +160 -0
  83. package/packages/core/src/measurement_coordination.ts +185 -0
  84. package/packages/core/src/modules.ts +1 -1
  85. package/packages/core/src/motion_sampling.ts +100 -0
  86. package/packages/core/src/paragraph_layout.ts +192 -0
  87. package/packages/core/src/portable_sources.json +74 -0
  88. package/packages/core/src/portable_sources.ts +17 -0
  89. package/packages/core/src/registered_font.ts +93 -0
  90. package/packages/core/src/render_animation_lifecycle.ts +128 -0
  91. package/packages/core/src/render_coordination.ts +170 -0
  92. package/packages/core/src/render_resources.ts +234 -0
  93. package/packages/core/src/runtime_policy.ts +3930 -0
  94. package/packages/core/src/scalar_text.ts +84 -0
  95. package/packages/core/src/service_contract.ts +2 -1
  96. package/packages/core/src/session_codecs.ts +309 -0
  97. package/packages/core/src/shell_admission.ts +126 -0
  98. package/packages/core/src/shell_coordination.ts +93 -0
  99. package/packages/core/src/sqlite_cli.ts +11 -0
  100. package/packages/core/src/status_validation.ts +232 -0
  101. package/packages/core/src/stream_policy.ts +63 -0
  102. package/packages/core/src/surface_recipes.ts +136 -0
  103. package/packages/core/src/system_admission.ts +60 -0
  104. package/packages/core/src/terminal_paint.ts +436 -0
  105. package/packages/core/src/text_document_width.ts +81 -0
  106. package/packages/core/src/text_measurement_cache.ts +69 -0
  107. package/packages/core/src/text_query_layout.ts +179 -0
  108. package/packages/core/src/text_run_layout.ts +410 -0
  109. package/packages/core/src/text_span_queries.ts +355 -0
  110. package/packages/core/src/typed_ast.ts +18 -1
  111. package/packages/core/src/types.ts +7 -5
  112. package/packages/core/src/vector_effects.ts +98 -0
  113. package/packages/core/src/vector_raster.ts +243 -0
  114. package/packages/core/src/view_components.ts +2415 -0
  115. package/packages/core/src/view_coordination.ts +198 -0
  116. package/packages/core/src/view_frontend.ts +980 -0
  117. package/packages/core/src/widget_audits.ts +202 -0
  118. package/packages/core/src/widget_changes.ts +258 -0
  119. package/packages/core/src/widget_metrics.ts +139 -0
  120. package/packages/core/src/widget_motion.ts +171 -0
  121. package/packages/core/src/widget_paint.ts +272 -0
  122. package/packages/core/src/widget_paint_walk.ts +203 -0
  123. package/packages/core/src/widget_presentation.ts +141 -0
  124. package/packages/core/src/widget_routing.ts +262 -0
  125. package/packages/core/src/window_lifecycle.ts +92 -0
  126. package/packages/core/testing/index.ts +430 -0
  127. package/schemas/app.schema.json +13 -0
  128. package/skill-data/core/SKILL.md +2 -2
  129. package/skill-data/native-ui/SKILL.md +76 -17
  130. package/skill-data/ts-core/SKILL.md +113 -9
  131. package/skill-data/ts-services/SKILL.md +1 -1
  132. package/skill-data/ts-services/references/service-surface.md +264 -144
  133. package/src/app_runner/root.zig +25 -12
  134. package/src/app_runner/ts_compiled_view.zig +1631 -0
  135. package/src/app_runner/ts_core_main.zig +143 -10
  136. package/src/app_runner/ts_core_mobile.zig +24 -12
  137. package/src/automation/server.zig +1 -3
  138. package/src/automation/snapshot.zig +9 -637
  139. package/src/automation/snapshot_reference.zig +656 -0
  140. package/src/automation/snapshot_serialization.zig +566 -0
  141. package/src/platform/linux/gtk_host.c +36 -24
  142. package/src/platform/linux/gtk_pixels.h +118 -0
  143. package/src/platform/linux/gtk_pixels_test.c +241 -0
  144. package/src/platform/linux/root.zig +2 -0
  145. package/src/platform/macos/appkit_host.m +188 -30
  146. package/src/platform/macos/pointer_motion_test.m +96 -0
  147. package/src/platform/macos/scroll_driver_test.m +105 -0
  148. package/src/platform/macos/spectrum_bins.h +34 -0
  149. package/src/platform/macos/spectrum_bins_test.c +56 -0
  150. package/src/platform/macos/text_keyboard_test.m +80 -0
  151. package/src/platform/macos/widget_focus_test.m +66 -0
  152. package/src/platform/null_platform.zig +26 -2
  153. package/src/platform/null_platform_tests.zig +13 -0
  154. package/src/platform/root.zig +1 -0
  155. package/src/platform/types.zig +13 -0
  156. package/src/platform/windows/root.zig +2 -0
  157. package/src/platform/windows/webview2_host.cpp +7 -2
  158. package/src/primitives/app_manifest/comptime_scene.zig +1 -1
  159. package/src/primitives/app_manifest/root.zig +2 -0
  160. package/src/primitives/app_manifest/tests.zig +4 -1
  161. package/src/primitives/app_manifest/validation.zig +2 -0
  162. package/src/primitives/canvas/a11y_audit.zig +3 -1
  163. package/src/primitives/canvas/canvas_frame_coordination_policy.zig +134 -0
  164. package/src/primitives/canvas/chart_content_policy.zig +75 -0
  165. package/src/primitives/canvas/chart_plan_policy.zig +196 -0
  166. package/src/primitives/canvas/code_content_policy.zig +162 -0
  167. package/src/primitives/canvas/commands.zig +29 -1
  168. package/src/primitives/canvas/component_composition.zig +125 -0
  169. package/src/primitives/canvas/component_composition_policy.zig +44 -0
  170. package/src/primitives/canvas/component_construction_policy.zig +187 -0
  171. package/src/primitives/canvas/container_layout_policy.zig +87 -0
  172. package/src/primitives/canvas/control_appearance_policy.zig +142 -0
  173. package/src/primitives/canvas/control_command_policy.zig +75 -0
  174. package/src/primitives/canvas/control_content_policy.zig +90 -0
  175. package/src/primitives/canvas/control_geometry_policy.zig +165 -0
  176. package/src/primitives/canvas/control_payload_policy.zig +48 -0
  177. package/src/primitives/canvas/control_primitive_policy.zig +129 -0
  178. package/src/primitives/canvas/effect_plan_policy.zig +96 -0
  179. package/src/primitives/canvas/events.zig +1027 -1
  180. package/src/primitives/canvas/flow_measurement_policy.zig +237 -0
  181. package/src/primitives/canvas/frame.zig +92 -23
  182. package/src/primitives/canvas/glyph_atlas_policy.zig +40 -0
  183. package/src/primitives/canvas/gpu.zig +10 -0
  184. package/src/primitives/canvas/gpu_packet_preparation_policy.zig +325 -0
  185. package/src/primitives/canvas/gpu_planning_policy.zig +364 -0
  186. package/src/primitives/canvas/gpu_presentation_policy.zig +74 -0
  187. package/src/primitives/canvas/grid_layout_policy.zig +51 -0
  188. package/src/primitives/canvas/indicator_plan_policy.zig +128 -0
  189. package/src/primitives/canvas/intrinsic_layout_policy.zig +87 -0
  190. package/src/primitives/canvas/intrinsic_measure_policy.zig +128 -0
  191. package/src/primitives/canvas/layout_audit.zig +5 -2
  192. package/src/primitives/canvas/layout_coordination_policy.zig +262 -0
  193. package/src/primitives/canvas/leaf_plan_policy.zig +117 -0
  194. package/src/primitives/canvas/markdown.zig +13 -3
  195. package/src/primitives/canvas/markdown_content_policy.zig +227 -0
  196. package/src/primitives/canvas/markdown_image_binding.zig +81 -0
  197. package/src/primitives/canvas/measurement_coordination_policy.zig +288 -0
  198. package/src/primitives/canvas/numeric_capabilities.zig +14 -0
  199. package/src/primitives/canvas/reference.zig +12 -6
  200. package/src/primitives/canvas/registered_font_policy.zig +152 -0
  201. package/src/primitives/canvas/render.zig +48 -0
  202. package/src/primitives/canvas/render_cache_policy.zig +184 -0
  203. package/src/primitives/canvas/render_coordination_policy.zig +163 -0
  204. package/src/primitives/canvas/render_damage_policy.zig +160 -0
  205. package/src/primitives/canvas/render_effects.zig +14 -0
  206. package/src/primitives/canvas/render_generic_resources.zig +14 -0
  207. package/src/primitives/canvas/render_images.zig +14 -0
  208. package/src/primitives/canvas/render_layers.zig +14 -0
  209. package/src/primitives/canvas/render_override_policy.zig +119 -0
  210. package/src/primitives/canvas/render_paths.zig +14 -0
  211. package/src/primitives/canvas/render_plan_policy.zig +199 -0
  212. package/src/primitives/canvas/render_resource_policy.zig +476 -0
  213. package/src/primitives/canvas/root.zig +100 -0
  214. package/src/primitives/canvas/semantic_tree_policy.zig +158 -0
  215. package/src/primitives/canvas/serialization.zig +8 -1
  216. package/src/primitives/canvas/surface_layout_policy.zig +36 -0
  217. package/src/primitives/canvas/surface_recipe_policy.zig +231 -0
  218. package/src/primitives/canvas/terminal_grid.zig +10 -696
  219. package/src/primitives/canvas/terminal_grid_reference.zig +709 -0
  220. package/src/primitives/canvas/terminal_paint_policy.zig +191 -0
  221. package/src/primitives/canvas/text.zig +1 -0
  222. package/src/primitives/canvas/text_atlas.zig +113 -0
  223. package/src/primitives/canvas/text_cache_policy.zig +94 -0
  224. package/src/primitives/canvas/text_document_policy.zig +120 -0
  225. package/src/primitives/canvas/text_layout.zig +51 -2
  226. package/src/primitives/canvas/text_layout_cache.zig +29 -0
  227. package/src/primitives/canvas/text_layout_hash.zig +1 -0
  228. package/src/primitives/canvas/text_layout_types.zig +3 -0
  229. package/src/primitives/canvas/text_measure_cache.zig +46 -0
  230. package/src/primitives/canvas/text_measurement_policy.zig +124 -0
  231. package/src/primitives/canvas/text_metrics.zig +23 -0
  232. package/src/primitives/canvas/text_query_policy.zig +156 -0
  233. package/src/primitives/canvas/text_run_policy.zig +252 -0
  234. package/src/primitives/canvas/text_scalar_policy.zig +156 -0
  235. package/src/primitives/canvas/text_span_query_policy.zig +232 -0
  236. package/src/primitives/canvas/text_spans.zig +280 -8
  237. package/src/primitives/canvas/text_tests.zig +10 -0
  238. package/src/primitives/canvas/tokens.zig +38 -0
  239. package/src/primitives/canvas/ui.zig +250 -83
  240. package/src/primitives/canvas/ui_markup.zig +139 -33
  241. package/src/primitives/canvas/ui_markup_compiled.zig +319 -59
  242. package/src/primitives/canvas/ui_markup_compiled_tests.zig +225 -0
  243. package/src/primitives/canvas/ui_markup_contract.zig +146 -14
  244. package/src/primitives/canvas/ui_markup_contract_tests.zig +77 -0
  245. package/src/primitives/canvas/ui_markup_reflect.zig +56 -0
  246. package/src/primitives/canvas/ui_markup_tests.zig +116 -0
  247. package/src/primitives/canvas/ui_markup_view.zig +258 -32
  248. package/src/primitives/canvas/ui_markup_view_tests.zig +142 -10
  249. package/src/primitives/canvas/ui_schema.zig +13 -1
  250. package/src/primitives/canvas/ui_schema_tests.zig +11 -7
  251. package/src/primitives/canvas/ui_tests.zig +1 -0
  252. package/src/primitives/canvas/vector.zig +36 -2
  253. package/src/primitives/canvas/vector_effect_policy.zig +236 -0
  254. package/src/primitives/canvas/vector_raster_policy.zig +190 -0
  255. package/src/primitives/canvas/virtual_extent_policy.zig +292 -0
  256. package/src/primitives/canvas/virtual_extents.zig +99 -3
  257. package/src/primitives/canvas/virtual_flow_policy.zig +108 -0
  258. package/src/primitives/canvas/widget_audit_policy.zig +293 -0
  259. package/src/primitives/canvas/widget_change_policy.zig +298 -0
  260. package/src/primitives/canvas/widget_invalidation.zig +113 -2
  261. package/src/primitives/canvas/widget_layout.zig +673 -128
  262. package/src/primitives/canvas/widget_layout_tests.zig +4 -0
  263. package/src/primitives/canvas/widget_metric_policy.zig +126 -0
  264. package/src/primitives/canvas/widget_metrics.zig +45 -6
  265. package/src/primitives/canvas/widget_paint_policy.zig +166 -0
  266. package/src/primitives/canvas/widget_paint_walk_policy.zig +252 -0
  267. package/src/primitives/canvas/widget_presentation_policy.zig +168 -0
  268. package/src/primitives/canvas/widget_render.zig +744 -74
  269. package/src/primitives/canvas/widget_render_controls.zig +1217 -133
  270. package/src/primitives/canvas/widget_render_scroll.zig +11 -3
  271. package/src/primitives/canvas/widget_render_style.zig +94 -0
  272. package/src/primitives/canvas/widget_render_surfaces.zig +61 -50
  273. package/src/primitives/canvas/widget_routing.zig +75 -0
  274. package/src/primitives/canvas/widget_routing_policy.zig +135 -0
  275. package/src/primitives/canvas/widget_runtime.zig +20 -1
  276. package/src/primitives/canvas/widget_semantics.zig +25 -0
  277. package/src/primitives/canvas/widget_text_input.zig +86 -23
  278. package/src/primitives/canvas/widget_text_select.zig +1 -0
  279. package/src/primitives/canvas/widget_tree.zig +8 -1
  280. package/src/primitives/canvas/widgets.zig +42 -12
  281. package/src/primitives/canvas/wrapped_layout_policy.zig +104 -0
  282. package/src/root.zig +21 -0
  283. package/src/runtime/accessibility_policy.zig +122 -0
  284. package/src/runtime/api.zig +28 -0
  285. package/src/runtime/app_dispatch_policy.zig +68 -0
  286. package/src/runtime/authorization_policy.zig +150 -0
  287. package/src/runtime/automation_command_policy.zig +241 -0
  288. package/src/runtime/automation_interaction_policy.zig +107 -0
  289. package/src/runtime/automation_snapshot.zig +52 -22
  290. package/src/runtime/automation_snapshot_reference.zig +290 -0
  291. package/src/runtime/automation_widget_dispatch.zig +267 -355
  292. package/src/runtime/automation_widget_dispatch_reference.zig +900 -0
  293. package/src/runtime/blob_policy.zig +58 -0
  294. package/src/runtime/blob_reference.zig +64 -0
  295. package/src/runtime/bridge_descriptor_policy.zig +206 -0
  296. package/src/runtime/builtin_bridge.zig +180 -44
  297. package/src/runtime/builtin_coordination_policy.zig +121 -0
  298. package/src/runtime/callback_policy_test_reference.zig +97 -0
  299. package/src/runtime/canvas_drag_policy.zig +94 -0
  300. package/src/runtime/canvas_fonts.zig +38 -10
  301. package/src/runtime/canvas_frame.zig +590 -134
  302. package/src/runtime/canvas_frame_helpers.zig +1 -0
  303. package/src/runtime/canvas_hover_policy.zig +131 -0
  304. package/src/runtime/canvas_images.zig +59 -30
  305. package/src/runtime/canvas_keyboard_focus_policy.zig +154 -0
  306. package/src/runtime/canvas_limits.zig +1 -1
  307. package/src/runtime/canvas_presentation_lifecycle_policy.zig +86 -0
  308. package/src/runtime/canvas_scene_observation_policy.zig +181 -0
  309. package/src/runtime/canvas_widget_context_menu.zig +4 -4
  310. package/src/runtime/canvas_widget_display.zig +110 -9
  311. package/src/runtime/canvas_widget_events.zig +718 -328
  312. package/src/runtime/canvas_widget_runtime.zig +437 -81
  313. package/src/runtime/canvas_widget_scroll_drivers.zig +39 -1
  314. package/src/runtime/canvas_widget_scroll_tests.zig +31 -0
  315. package/src/runtime/canvas_widget_state.zig +93 -56
  316. package/src/runtime/channel_sources.zig +80 -0
  317. package/src/runtime/command_policy.zig +161 -0
  318. package/src/runtime/component_policy.zig +83 -0
  319. package/src/runtime/core.zig +156 -6
  320. package/src/runtime/desktop_files.zig +189 -0
  321. package/src/runtime/effects.zig +466 -29
  322. package/src/runtime/effects_tests.zig +28 -0
  323. package/src/runtime/file_policy_test_reference.zig +113 -0
  324. package/src/runtime/flow.zig +418 -341
  325. package/src/runtime/flow_reference.zig +1426 -0
  326. package/src/runtime/font_registry_policy.zig +72 -0
  327. package/src/runtime/frame_profile.zig +24 -33
  328. package/src/runtime/gpu_surface_events.zig +97 -35
  329. package/src/runtime/host_event_pump.zig +303 -0
  330. package/src/runtime/host_event_pump_reference.zig +268 -0
  331. package/src/runtime/host_flow_policy.zig +155 -0
  332. package/src/runtime/host_input_regions_policy.zig +156 -0
  333. package/src/runtime/image_registry_policy.zig +94 -0
  334. package/src/runtime/input_composition_policy.zig +138 -0
  335. package/src/runtime/journal_policy.zig +66 -0
  336. package/src/runtime/journal_reference.zig +53 -0
  337. package/src/runtime/lifecycle_policy_test_reference.zig +97 -0
  338. package/src/runtime/markup_development_policy.zig +91 -0
  339. package/src/runtime/media_surface.zig +40 -1
  340. package/src/runtime/motion_sampling_policy.zig +159 -0
  341. package/src/runtime/recording_policy.zig +42 -0
  342. package/src/runtime/render_animation_policy.zig +206 -0
  343. package/src/runtime/replay_policy.zig +66 -0
  344. package/src/runtime/replay_policy_reference.zig +326 -0
  345. package/src/runtime/replay_progress_policy.zig +64 -0
  346. package/src/runtime/replay_progress_reference.zig +34 -0
  347. package/src/runtime/request_policy_test_reference.zig +188 -0
  348. package/src/runtime/root.zig +13 -0
  349. package/src/runtime/session_blobs.zig +157 -58
  350. package/src/runtime/session_blobs_reference.zig +692 -0
  351. package/src/runtime/session_codec.zig +361 -0
  352. package/src/runtime/session_codec_reference.zig +1027 -0
  353. package/src/runtime/session_journal.zig +246 -983
  354. package/src/runtime/session_record.zig +224 -45
  355. package/src/runtime/session_replay.zig +157 -415
  356. package/src/runtime/session_tests.zig +552 -0
  357. package/src/runtime/shell_admission_policy.zig +225 -0
  358. package/src/runtime/shell_coordination_policy.zig +138 -0
  359. package/src/runtime/shell_layout.zig +126 -1
  360. package/src/runtime/snapshot_policy.zig +60 -0
  361. package/src/runtime/snapshot_reference.zig +64 -0
  362. package/src/runtime/state.zig +16 -0
  363. package/src/runtime/status_validation.zig +190 -0
  364. package/src/runtime/system_admission_policy.zig +140 -0
  365. package/src/runtime/system_services.zig +46 -36
  366. package/src/runtime/tests.zig +1 -0
  367. package/src/runtime/ts_canvas_hooks.zig +261 -0
  368. package/src/runtime/ts_canvas_hooks_tests.zig +279 -0
  369. package/src/runtime/ts_canvas_icons.zig +234 -0
  370. package/src/runtime/ts_core_host.zig +2325 -411
  371. package/src/runtime/ts_core_host_tests.zig +1982 -0
  372. package/src/runtime/ts_test_host.zig +699 -0
  373. package/src/runtime/ts_ui_app.zig +1087 -45
  374. package/src/runtime/ui_app.zig +1117 -629
  375. package/src/runtime/ui_app_provenance.zig +147 -5
  376. package/src/runtime/ui_app_provenance_reference.zig +370 -0
  377. package/src/runtime/ui_app_tests.zig +619 -9
  378. package/src/runtime/ui_app_window_tests.zig +89 -0
  379. package/src/runtime/view.zig +25 -0
  380. package/src/runtime/view_canvas.zig +30 -6
  381. package/src/runtime/view_coordination_policy.zig +147 -0
  382. package/src/runtime/view_widget_control.zig +51 -14
  383. package/src/runtime/view_widget_scroll.zig +16 -5
  384. package/src/runtime/view_widget_text.zig +153 -68
  385. package/src/runtime/view_widget_tree.zig +460 -56
  386. package/src/runtime/webview_view_bridge_tests.zig +2 -0
  387. package/src/runtime/widget_motion_policy.zig +238 -0
  388. package/src/runtime/window_command_bridge_tests.zig +63 -0
  389. package/src/runtime/window_lifecycle_policy.zig +127 -0
  390. package/src/runtime/window_storage.zig +74 -8
  391. package/src/runtime/window_view_runtime.zig +148 -33
  392. package/src/runtime/window_views.zig +163 -10
  393. package/src/tooling/components/identity_tests.zig +62 -36
  394. package/src/tooling/components/stepper.native +24 -0
  395. package/src/tooling/components/stepper.zig +0 -19
  396. package/src/tooling/components/timeline-item.native +22 -0
  397. package/src/tooling/components/timeline.native +5 -3
  398. package/src/tooling/components/timeline_item.zig +0 -32
  399. package/src/tooling/eject_components.zig +76 -8
  400. package/src/tooling/ios.zig +66 -12
  401. package/src/tooling/manifest.zig +35 -0
  402. package/src/tooling/raw_manifest.zig +6 -0
  403. package/src/tooling/templates.zig +15 -3
  404. package/src/tooling/ts_core.zig +13 -3
  405. package/src/tooling/verbs.zig +22 -0
  406. package/tools/corewire/build_profile.mjs +54 -0
  407. package/tools/corewire/contract_intake.ts +303 -0
  408. package/tools/corewire/contract_intake.zig +82 -0
  409. package/tools/corewire/core.test.ts +166 -0
  410. package/tools/corewire/core_abi.zig +51 -0
  411. package/tools/corewire/core_cases.ts +233 -0
  412. package/tools/corewire/core_contract.ts +407 -0
  413. package/tools/corewire/core_emission.ts +136 -0
  414. package/tools/corewire/core_emission.zig +69 -0
  415. package/tools/corewire/core_goldens.json +3110 -0
  416. package/tools/corewire/core_policy.ts +14 -0
  417. package/tools/corewire/core_policy.zig +48 -0
  418. package/tools/corewire/core_projection.ts +360 -0
  419. package/tools/corewire/core_vocabulary.ts +6 -0
  420. package/tools/corewire/emit.zig +19 -1590
  421. package/tools/corewire/emit_facade.ts +714 -0
  422. package/tools/corewire/emit_facade.zig +10 -3752
  423. package/tools/corewire/emit_mirror.ts +290 -0
  424. package/tools/corewire/emit_profile.ts +50 -0
  425. package/tools/corewire/emit_service.ts +331 -0
  426. package/tools/corewire/emit_service.zig +23 -842
  427. package/tools/corewire/entry.test.ts +95 -0
  428. package/tools/corewire/entry_coordination.ts +127 -0
  429. package/tools/corewire/entry_coordination.zig +190 -0
  430. package/tools/corewire/facade_templates.ts +85 -0
  431. package/tools/corewire/filesystem.test.ts +166 -0
  432. package/tools/corewire/filesystem_coordination.ts +213 -0
  433. package/tools/corewire/filesystem_coordination.zig +245 -0
  434. package/tools/corewire/intake.test.ts +35 -0
  435. package/tools/corewire/intake_cases.ts +55 -0
  436. package/tools/corewire/intake_goldens.json +12237 -0
  437. package/tools/corewire/invocation.test.ts +24 -0
  438. package/tools/corewire/invocation.ts +175 -0
  439. package/tools/corewire/invocation.zig +136 -0
  440. package/tools/corewire/invocation_cases.ts +60 -0
  441. package/tools/corewire/invocation_fixture.ts +33 -0
  442. package/tools/corewire/invocation_goldens.json +1143 -0
  443. package/tools/corewire/invocation_tests.zig +111 -0
  444. package/tools/corewire/invocation_usage.ts +2 -0
  445. package/tools/corewire/lossless_projection.ts +96 -0
  446. package/tools/corewire/main.zig +7 -680
  447. package/tools/corewire/main_reference.zig +415 -0
  448. package/tools/corewire/mirror_templates.ts +55 -0
  449. package/tools/corewire/profile.test.ts +248 -0
  450. package/tools/corewire/profile.zig +56 -0
  451. package/tools/corewire/profile_emission.ts +202 -0
  452. package/tools/corewire/profile_library.json +29 -0
  453. package/tools/corewire/profile_tests.zig +136 -0
  454. package/tools/corewire/service.test.ts +75 -0
  455. package/tools/corewire/service_cases.ts +61 -0
  456. package/tools/corewire/service_contract.zig +40 -146
  457. package/tools/corewire/service_goldens.json +650 -0
  458. package/tools/corewire/service_projection.zig +52 -0
  459. package/tools/corewire/service_templates.ts +14 -0
  460. package/tools/corewire/shim_rt.zig +9 -0
  461. package/tools/corewire/sidecar.zig +267 -1438
  462. package/tools/corewire/emit_profile.zig +0 -521
@@ -3273,3 +3273,1985 @@ test "a spawn_failed transport end routes the event arm with the reason" {
3273
3273
  try std.testing.expectEqual(mini_core.PtyReason.spawn_failed, Host.model().pty_reason);
3274
3274
  try std.testing.expectEqual(@as(i64, -1), Host.model().pty_code);
3275
3275
  }
3276
+
3277
+ // A deliberately redirected policy proves the host consumes the copied plan
3278
+ // rather than repeating slot, interval, route or retirement decisions itself.
3279
+ const timer_policy_probe_core = struct {
3280
+ pub const rt = mini_core.rt;
3281
+ pub const Model = mini_core.Model;
3282
+ pub const Msg = mini_core.Msg;
3283
+ pub const initialModel = mini_core.initialModel;
3284
+ pub const bootCommand = mini_core.bootCommand;
3285
+ pub const update = mini_core.update;
3286
+ pub const subscriptions = mini_core.subscriptions;
3287
+ pub const commitModelRoot = mini_core.commitModelRoot;
3288
+ var retire: bool = false;
3289
+ var calls: usize = 0;
3290
+
3291
+ pub fn nativeTimerPolicy(request: []const u8, output: []u8) usize {
3292
+ calls += 1;
3293
+ if (request[0] == 7) {
3294
+ const size = @import("callback_policy_test_reference.zig").plan(request, output);
3295
+ output[1] = 10;
3296
+ std.mem.writeInt(u64, output[8..16], @bitCast(@as(f64, 1234.5)), .little);
3297
+ return size;
3298
+ }
3299
+ if (request[0] == 1) {
3300
+ std.mem.writeInt(u16, output[0..2], if (retire) std.mem.readInt(u16, request[1..3], .little) else 0, .little);
3301
+ return 2;
3302
+ }
3303
+ @memset(output[0..14], 0);
3304
+ output[0] = 5;
3305
+ output[1] = 1;
3306
+ output[2] = 10; // Route to stamped instead of tick.
3307
+ std.mem.writeInt(u64, output[4..12], @bitCast(@as(f64, 7)), .little);
3308
+ std.mem.writeInt(u16, output[12..14], 1 << 5, .little);
3309
+ return 14;
3310
+ }
3311
+ };
3312
+
3313
+ test "timer host applies policy slot interval route and retirement without resetting the cycle" {
3314
+ const Probe = ts_core_host.TsCoreHost(timer_policy_probe_core);
3315
+ const fx = freshChannel();
3316
+ defer fx.deinit();
3317
+ timer_policy_probe_core.calls = 0;
3318
+ timer_policy_probe_core.retire = false;
3319
+ Probe.init(fx);
3320
+ Probe.dispatch(fx, .toggle);
3321
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingTimerCount());
3322
+ const redirected = ts_core_host.timer_key_base + 5;
3323
+ try std.testing.expectEqual(redirected, fx.pendingTimerAt(0).?.key);
3324
+ try std.testing.expectEqual(@as(u64, 7), fx.pendingTimerAt(0).?.interval_ms);
3325
+ Probe.dispatch(fx, .arm_db_live);
3326
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingDbCount());
3327
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingTimerCount());
3328
+ try fx.fireTimer(redirected);
3329
+ Probe.drain(fx);
3330
+ try std.testing.expectEqual(@as(i64, 0), Probe.model().ticks);
3331
+ try std.testing.expectEqual(@as(f64, 1234.5), Probe.model().stamp_ms);
3332
+ Probe.dispatch(fx, .toggle);
3333
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingTimerCount());
3334
+ timer_policy_probe_core.retire = true;
3335
+ Probe.dispatch(fx, .speed);
3336
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingTimerCount());
3337
+ try std.testing.expect(timer_policy_probe_core.calls >= 6);
3338
+ }
3339
+
3340
+ // Redirecting every delay operation distinguishes policy ownership from a
3341
+ // native fallback: a nonmatching cancellation can be suppressed or routed,
3342
+ // and fire routing can differ from the stored declaration tag.
3343
+ const delay_policy_probe_core = struct {
3344
+ pub const rt = mini_core.rt;
3345
+ pub const Model = mini_core.Model;
3346
+ pub const Msg = mini_core.Msg;
3347
+ pub const initialModel = mini_core.initialModel;
3348
+ pub const bootCommand = mini_core.bootCommand;
3349
+ pub fn update(model: *const Model, msg: Msg) mini_core.UpdateResult {
3350
+ if (msg == .open_save_sink) return .{ .model = model, .cmd = mini_core.cmdWriteFileStream("boom", 12, 8, "notes.bin") };
3351
+ return mini_core.update(model, msg);
3352
+ }
3353
+ pub const commitModelRoot = mini_core.commitModelRoot;
3354
+ var lookup_enabled: bool = false;
3355
+ var calls = [_]usize{0} ** 8;
3356
+
3357
+ pub fn nativeTimerPolicy(request: []const u8, output: []u8) usize {
3358
+ calls[request[0]] += 1;
3359
+ switch (request[0]) {
3360
+ 2 => {
3361
+ const value_at = 2 + @as(usize, request[1]);
3362
+ output[0] = if (request[value_at + 9] == 1) 255 else 6;
3363
+ output[1] = 10;
3364
+ std.mem.writeInt(u64, output[2..10], @bitCast(@as(f64, 17)), .little);
3365
+ return 10;
3366
+ },
3367
+ 3 => {
3368
+ var at: usize = 2 + @as(usize, request[1]);
3369
+ for (0..16) |slot| {
3370
+ if (slot == 6) output[0] = if (lookup_enabled and request[at] == 1) 6 else 255;
3371
+ at += 3 + @as(usize, request[at + 1]);
3372
+ }
3373
+ return 1;
3374
+ },
3375
+ 7 => {
3376
+ const size = @import("callback_policy_test_reference.zig").plan(request, output);
3377
+ output[1] = 9;
3378
+ return size;
3379
+ },
3380
+ 4 => {
3381
+ output[0] = request[1];
3382
+ output[1] = 9; // tick instead of the stored stamped arm.
3383
+ return 2;
3384
+ },
3385
+ else => unreachable,
3386
+ }
3387
+ }
3388
+ };
3389
+
3390
+ test "delay host consumes compiled allocation lookup fire route and retirement" {
3391
+ const Probe = ts_core_host.TsCoreHost(delay_policy_probe_core);
3392
+ const fx = freshChannel();
3393
+ defer fx.deinit();
3394
+ delay_policy_probe_core.lookup_enabled = false;
3395
+ delay_policy_probe_core.calls = @splat(0);
3396
+ Probe.init(fx);
3397
+ Probe.dispatch(fx, .arm_delay);
3398
+ const redirected = ts_core_host.delay_key_base + 6;
3399
+ try std.testing.expectEqual(redirected, fx.pendingTimerAt(0).?.key);
3400
+ try std.testing.expectEqual(@as(u64, 17), fx.pendingTimerAt(0).?.interval_ms);
3401
+ Probe.dispatch(fx, .halt);
3402
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingTimerCount());
3403
+ delay_policy_probe_core.lookup_enabled = true;
3404
+ Probe.dispatch(fx, .halt);
3405
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingTimerCount());
3406
+ Probe.dispatch(fx, .arm_delay);
3407
+ Probe.dispatch(fx, .arm_delay);
3408
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingTimerCount());
3409
+ try fx.fireTimer(redirected);
3410
+ Probe.drain(fx);
3411
+ try std.testing.expectEqual(@as(i64, 1), Probe.model().ticks);
3412
+ try std.testing.expectEqual(@as(f64, -1), Probe.model().stamp_ms);
3413
+ Probe.dispatch(fx, .halt); // The fire retired the bridge slot too.
3414
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingTimerCount());
3415
+ try std.testing.expectEqual(@as(usize, 3), delay_policy_probe_core.calls[2]);
3416
+ try std.testing.expectEqual(@as(usize, 3), delay_policy_probe_core.calls[3]);
3417
+ try std.testing.expectEqual(@as(usize, 1), delay_policy_probe_core.calls[7]);
3418
+ }
3419
+
3420
+ test "delay admission supplies file stream occupancy and preserves cancellation priority" {
3421
+ const Probe = ts_core_host.TsCoreHost(delay_policy_probe_core);
3422
+ const fx = freshChannel();
3423
+ defer fx.deinit();
3424
+ delay_policy_probe_core.lookup_enabled = true;
3425
+ delay_policy_probe_core.calls = @splat(0);
3426
+ Probe.init(fx);
3427
+ Probe.dispatch(fx, .open_save_sink);
3428
+ Probe.dispatch(fx, .arm_delay);
3429
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingTimerCount());
3430
+ try std.testing.expectEqual(@as(usize, 1), delay_policy_probe_core.calls[2]);
3431
+ Probe.dispatch(fx, .halt);
3432
+ Probe.drain(fx);
3433
+ try std.testing.expectEqual(@as(i64, 1), Probe.model().errs);
3434
+ try std.testing.expectEqualStrings("cancelled", Probe.model().last_err);
3435
+ // Conversely a live delay occupies its key for file-stream admission.
3436
+ Probe.dispatch(fx, .arm_delay);
3437
+ Probe.dispatch(fx, .open_save_sink);
3438
+ Probe.drain(fx);
3439
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingTimerCount());
3440
+ try std.testing.expectEqual(@as(i64, 2), Probe.model().errs);
3441
+ try std.testing.expectEqualStrings("rejected", Probe.model().last_err);
3442
+ Probe.dispatch(fx, .halt);
3443
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingTimerCount());
3444
+ }
3445
+
3446
+ const db_policy_probe_core = struct {
3447
+ pub const rt = mini_core.rt;
3448
+ pub const Model = mini_core.Model;
3449
+ pub const Msg = mini_core.Msg;
3450
+ pub const initialModel = mini_core.initialModel;
3451
+ pub const bootCommand = mini_core.bootCommand;
3452
+ pub const update = mini_core.update;
3453
+ pub const subscriptions = mini_core.subscriptions;
3454
+ pub const commitModelRoot = mini_core.commitModelRoot;
3455
+ var retain: bool = true;
3456
+ var rearm: bool = true;
3457
+ var lookup: bool = true;
3458
+ var calls = [_]usize{0} ** 3;
3459
+
3460
+ pub fn nativeDbPolicy(request: []const u8, output: []u8) usize {
3461
+ calls[request[0]] += 1;
3462
+ @memset(rt.frameAlloc(u8, 37), 255);
3463
+ if (request[0] == 1) {
3464
+ std.mem.writeInt(u16, output[0..2], if (retain) 1 << 6 else 0, .little);
3465
+ return 2;
3466
+ }
3467
+ const table = if (request[0] == 0) 2 + @as(usize, request[1]) else 15 + @as(usize, request[3]);
3468
+ var at: usize = table;
3469
+ for (0..6) |_| at += 11 + @as(usize, request[at + 2]);
3470
+ const used = request[at] == 1;
3471
+ if (request[0] == 0) {
3472
+ output[0] = if (lookup and used) 6 else 255;
3473
+ return 1;
3474
+ }
3475
+ output[0] = 6;
3476
+ output[1] = @intFromBool(!used or rearm);
3477
+ output[2] = @intFromBool(used and rearm);
3478
+ output[3] = 8; // Route pages to failed instead of loaded.
3479
+ output[4] = 12;
3480
+ output[5] = 7; // Route errors to loaded instead of failed.
3481
+ std.mem.writeInt(u16, output[6..8], 1 << 6, .little);
3482
+ return 8;
3483
+ }
3484
+ };
3485
+
3486
+ test "database host consumes policy slot routes restart retention and copied cycle facts" {
3487
+ const Probe = ts_core_host.TsCoreHost(db_policy_probe_core);
3488
+ const fx = freshChannel();
3489
+ defer fx.deinit();
3490
+ db_policy_probe_core.retain = true;
3491
+ db_policy_probe_core.rearm = true;
3492
+ db_policy_probe_core.lookup = true;
3493
+ db_policy_probe_core.calls = @splat(0);
3494
+ Probe.init(fx);
3495
+ Probe.dispatch(fx, .toggle); // Native timer and database policies share a cycle.
3496
+ Probe.dispatch(fx, .arm_db_live);
3497
+ const redirected = ts_core_host.db_key_base + 6;
3498
+ try std.testing.expectEqual(redirected, fx.pendingDbAt(0).?.key);
3499
+ try std.testing.expectEqualStrings("SELECT id FROM item", fx.pendingDbAt(0).?.sql);
3500
+ const generation = fx.pendingDbAt(0).?.generation;
3501
+ db_policy_probe_core.rearm = false;
3502
+ Probe.dispatch(fx, .arm_db_live);
3503
+ try std.testing.expectEqual(generation, fx.pendingDbAt(0).?.generation);
3504
+ db_policy_probe_core.rearm = true;
3505
+ Probe.dispatch(fx, .arm_db_live);
3506
+ try std.testing.expect(generation != fx.pendingDbAt(0).?.generation);
3507
+ try fx.feedDbResult(redirected, .page, .ok, &(@as([8]u8, @splat(0))));
3508
+ Probe.drain(fx);
3509
+ try std.testing.expectEqual(@as(i64, 1), Probe.model().errs);
3510
+ try std.testing.expectEqual(@as(usize, 8), Probe.model().last_err.len);
3511
+ try fx.feedDbResult(redirected, .done, .busy, "");
3512
+ Probe.drain(fx);
3513
+ try std.testing.expectEqualStrings("busy", Probe.model().status);
3514
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingDbCount());
3515
+ Probe.dispatch(fx, .stop_db_live_with_cancel);
3516
+ // The policy deliberately retains an absent declaration; Cmd.cancel is
3517
+ // still unable to strand a live query. Only retention retires that slot.
3518
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingDbCount());
3519
+ db_policy_probe_core.retain = false;
3520
+ Probe.dispatch(fx, .stamp);
3521
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingDbCount());
3522
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingTimerCount());
3523
+ try std.testing.expect(db_policy_probe_core.calls[1] >= 6);
3524
+ try std.testing.expect(db_policy_probe_core.calls[2] >= 3);
3525
+ }
3526
+
3527
+ test "database key lookup is owned by the compiled policy and preserves live command refusal" {
3528
+ const Probe = ts_core_host.TsCoreHost(db_policy_probe_core);
3529
+ const fx = freshChannel();
3530
+ defer fx.deinit();
3531
+ db_policy_probe_core.retain = true;
3532
+ db_policy_probe_core.rearm = false;
3533
+ db_policy_probe_core.lookup = true;
3534
+ db_policy_probe_core.calls = @splat(0);
3535
+ Probe.init(fx);
3536
+ Probe.dispatch(fx, .arm_db_live);
3537
+ Probe.dispatch(fx, .query_over_db_live);
3538
+ Probe.drain(fx);
3539
+ try std.testing.expectEqual(@as(i64, 1), Probe.model().errs);
3540
+ try std.testing.expectEqualStrings("rejected", Probe.model().last_err);
3541
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingDbCount());
3542
+ db_policy_probe_core.lookup = false;
3543
+ Probe.dispatch(fx, .query_over_db_live);
3544
+ try std.testing.expectEqual(@as(usize, 2), fx.pendingDbCount());
3545
+ try fx.feedDbResult(ts_core_host.db_key_base, .done, .ok, "");
3546
+ Probe.drain(fx);
3547
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingDbCount());
3548
+ try std.testing.expect(db_policy_probe_core.calls[0] >= 2);
3549
+ }
3550
+
3551
+ // Redirected slots and routes demonstrate that the native consumer applies the
3552
+ // policy's copied decisions rather than independently recomputing them.
3553
+ // Independent native behavior for unrelated forced-policy probes. New probes
3554
+ // override these bytes explicitly to prove that the consumer applies the plan.
3555
+ fn referenceCoordinationPolicy(request: []const u8, output: []u8) usize {
3556
+ if (request[0] == 17) return @import("callback_policy_test_reference.zig").plan(request, output);
3557
+ if (request[0] >= 15) return @import("file_policy_test_reference.zig").plan(request, output);
3558
+ if (request[0] >= 11) return @import("request_policy_test_reference.zig").plan(request, output);
3559
+ if (request[0] == 10) {
3560
+ output[0] = if (request[1] == 1) request[4] else request[3];
3561
+ return 1;
3562
+ }
3563
+ if (request[0] == 8) {
3564
+ @memset(output[0..24], 0);
3565
+ output[1] = request[2];
3566
+ output[2] = request[3] & 7;
3567
+ const value: f64 = @bitCast(std.mem.readInt(u64, request[8..16], .little));
3568
+ if (request[1] == 0) {
3569
+ output[0] = 1;
3570
+ std.mem.writeInt(u64, output[16..24], if (value >= 1 and value <= 9007199254740992.0) @intFromFloat(value) else 0, .little);
3571
+ } else {
3572
+ const surface: u64 = if (value >= 1 and value < 9007199254740992.0 and @floor(value) == value) @intFromFloat(value) else 0;
3573
+ std.mem.writeInt(u64, output[8..16], surface, .little);
3574
+ const path_len = std.mem.readInt(u32, request[16..20], .little);
3575
+ const url_len = std.mem.readInt(u32, request[20..24], .little);
3576
+ // These probes only issue short paths. Full size/URI parity is
3577
+ // compared directly against videoLoadRejected in the compiled suite.
3578
+ const path = "fixture.mp4"[0..@min(path_len, "fixture.mp4".len)];
3579
+ output[0] = @intFromBool(url_len <= 1024 and path_len <= 1024 and !Fx.videoLoadRejected(.{
3580
+ .key = 0,
3581
+ .surface = surface,
3582
+ .path = path,
3583
+ .url = request[24..],
3584
+ }));
3585
+ }
3586
+ return 24;
3587
+ }
3588
+ std.debug.assert(request[0] == 9);
3589
+ @memset(output[0..16], 0);
3590
+ output[0] = 255;
3591
+ output[1] = request[5];
3592
+ const cols: f64 = @bitCast(std.mem.readInt(u64, request[16..24], .little));
3593
+ const rows: f64 = @bitCast(std.mem.readInt(u64, request[24..32], .little));
3594
+ std.mem.writeInt(u16, output[4..6], if (cols >= 1 and cols <= 65535 and @floor(cols) == cols) @intFromFloat(cols) else 0, .little);
3595
+ std.mem.writeInt(u16, output[6..8], if (rows >= 1 and rows <= 65535 and @floor(rows) == rows) @intFromFloat(rows) else 0, .little);
3596
+ const name = request[32..][0..request[6]];
3597
+ var at: usize = 32 + name.len;
3598
+ var positions: [4]usize = undefined;
3599
+ var live: ?usize = null;
3600
+ var bound: ?usize = null;
3601
+ var free: ?usize = null;
3602
+ var binding: ?usize = null;
3603
+ for (0..4) |slot| {
3604
+ positions[slot] = at;
3605
+ const used = request[at] == 1;
3606
+ const retained = request[at + 1] == 1;
3607
+ const equal = std.mem.eql(u8, name, request[at + 4 ..][0..request[at + 3]]);
3608
+ if (name.len > 0 and equal and used and live == null) live = slot;
3609
+ if (equal and retained and !used and bound == null) bound = slot;
3610
+ if (!used and !retained and free == null) free = slot;
3611
+ if (name.len > 0 and equal and (used or retained) and binding == null) binding = slot;
3612
+ at += 4 + request[at + 3];
3613
+ }
3614
+ const selected: ?usize = switch (request[1]) {
3615
+ 0 => if (name.len > 0 and (live != null or request[3] == 1)) null else bound orelse free,
3616
+ 1 => live,
3617
+ 2 => if (std.mem.readInt(u16, output[4..6], .little) != 0 and std.mem.readInt(u16, output[6..8], .little) != 0) live else null,
3618
+ 3 => binding,
3619
+ 4 => @intCast(std.mem.readInt(u64, request[8..16], .little) - ts_core_host.pty_key_base),
3620
+ else => unreachable,
3621
+ };
3622
+ if (selected) |slot| {
3623
+ output[0] = @intCast(slot);
3624
+ if (request[1] != 0) output[1] = request[positions[slot] + 2];
3625
+ output[2] = @intFromBool(request[1] == 4 and request[4] == 1);
3626
+ output[3] = @intFromBool(request[1] == 3);
3627
+ std.mem.writeInt(u64, output[8..16], ts_core_host.pty_key_base + slot, .little);
3628
+ }
3629
+ return 16;
3630
+ }
3631
+
3632
+ const effect_policy_probe_core = struct {
3633
+ pub const rt = mini_core.rt;
3634
+ pub const Model = mini_core.Model;
3635
+ pub const Msg = mini_core.Msg;
3636
+ pub const initialModel = mini_core.initialModel;
3637
+ pub const bootCommand = mini_core.bootCommand;
3638
+ pub const update = mini_core.update;
3639
+ pub const subscriptions = mini_core.subscriptions;
3640
+ pub const commitModelRoot = mini_core.commitModelRoot;
3641
+ var lookup_enabled: bool = true;
3642
+ var calls = [_]usize{0} ** 18;
3643
+
3644
+ pub fn nativeEffectPolicy(request: []const u8, output: []u8) usize {
3645
+ calls[request[0]] += 1;
3646
+ if (request[0] == 17) {
3647
+ const size = referenceCoordinationPolicy(request, output);
3648
+ output[1] = 7; // Redirect the complete buffered route to loaded.
3649
+ if (output[2] != 0 and output[2] != 5) output[2] = 2;
3650
+ return size;
3651
+ }
3652
+ if (request[0] >= 8) {
3653
+ const size = referenceCoordinationPolicy(request, output);
3654
+ if (request[0] == 12 and !lookup_enabled and output[0] == 1) {
3655
+ output[0] = 255;
3656
+ output[1] = 255;
3657
+ }
3658
+ return size;
3659
+ }
3660
+ if (request[0] == 2) {
3661
+ const at = 5 + @as(usize, request[1]) * 4;
3662
+ output[0] = request[1];
3663
+ output[1] = 7; // The loaded bytes arm, including an error outcome.
3664
+ output[2] = if (request[at + 1] == 1) 0 else if (request[3] == 1) 2 else 5;
3665
+ output[3] = if (output[2] == 5) 1 else 0;
3666
+ return 4;
3667
+ }
3668
+ const length: usize = request[1];
3669
+ var at: usize = 2 + length + @as(usize, if (request[0] == 0) 3 else 0);
3670
+ var matching: u8 = 255;
3671
+ var used: [16]bool = undefined;
3672
+ for (0..16) |slot| {
3673
+ used[slot] = request[at] == 1;
3674
+ const key_len: usize = request[at + 2];
3675
+ if (used[slot] and request[at + 1] == 0 and matching == 255 and std.mem.eql(u8, request[at + 3 ..][0..key_len], request[2..][0..length])) matching = @intCast(slot);
3676
+ at += 5 + key_len;
3677
+ }
3678
+ if (request[0] == 1) {
3679
+ output[0] = if (lookup_enabled) matching else 255;
3680
+ return 1;
3681
+ }
3682
+ const blocked = request[2 + length] == 1;
3683
+ var free: u8 = 255;
3684
+ for (0..16) |offset| {
3685
+ const slot = (offset + 5) % 16;
3686
+ if (!used[slot]) {
3687
+ free = @intCast(slot);
3688
+ break;
3689
+ }
3690
+ }
3691
+ @memcpy(output[0..5], &@as([5]u8, .{ @intFromBool(!blocked), if (blocked) 255 else free, if (blocked or length == 0) 255 else matching, 8, 7 }));
3692
+ return 5;
3693
+ }
3694
+ };
3695
+
3696
+ test "named effect host applies policy admission slots cancellation routes and dropped terminal retirement" {
3697
+ const Probe = ts_core_host.TsCoreHost(effect_policy_probe_core);
3698
+ const fx = freshChannel();
3699
+ defer fx.deinit();
3700
+ effect_policy_probe_core.lookup_enabled = false;
3701
+ effect_policy_probe_core.calls = @splat(0);
3702
+ Probe.init(fx);
3703
+ Probe.dispatch(fx, .load_file);
3704
+ const first = ts_core_host.effect_key_base + 5;
3705
+ try std.testing.expectEqual(first, fx.pendingFileAt(0).?.key);
3706
+ Probe.dispatch(fx, .drop_load); // Compiled cancellation deliberately suppresses it.
3707
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingFileCount());
3708
+ effect_policy_probe_core.lookup_enabled = true;
3709
+ Probe.dispatch(fx, .drop_load);
3710
+ Probe.drain(fx);
3711
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingFileCount());
3712
+ try std.testing.expectEqual(@as(i64, 0), Probe.model().errs);
3713
+ Probe.dispatch(fx, .load_file); // Dropped completion retired the owned slot.
3714
+ try std.testing.expectEqual(first, fx.pendingFileAt(0).?.key);
3715
+ try fx.feedFileResult(first, .io_failed, "");
3716
+ Probe.drain(fx);
3717
+ try std.testing.expectEqualStrings("io_failed", Probe.model().status);
3718
+ try std.testing.expectEqual(@as(i64, 0), Probe.model().errs);
3719
+ Probe.dispatch(fx, .dup_load);
3720
+ try std.testing.expectEqual(ts_core_host.effect_key_base + 6, fx.pendingFileAt(0).?.key);
3721
+ Probe.drain(fx); // First terminal is swallowed; replacement still owns slot 6.
3722
+ try std.testing.expectEqualStrings("io_failed", Probe.model().status);
3723
+ try fx.feedFileResult(ts_core_host.effect_key_base + 6, .ok, "replacement");
3724
+ Probe.drain(fx);
3725
+ try std.testing.expectEqualStrings("replacement", Probe.model().status);
3726
+ Probe.dispatch(fx, .load_file);
3727
+ try std.testing.expectEqual(first, fx.pendingFileAt(0).?.key);
3728
+ try std.testing.expect(effect_policy_probe_core.calls[0] >= 5);
3729
+ try std.testing.expectEqual(@as(usize, 1), effect_policy_probe_core.calls[1]);
3730
+ try std.testing.expectEqual(@as(usize, 2), effect_policy_probe_core.calls[12]);
3731
+ try std.testing.expectEqual(@as(usize, 4), effect_policy_probe_core.calls[17]);
3732
+ }
3733
+
3734
+ test "named effect admission consumes native file stream occupancy and keeps cancel priority" {
3735
+ const Probe = ts_core_host.TsCoreHost(effect_policy_probe_core);
3736
+ const fx = freshChannel();
3737
+ defer fx.deinit();
3738
+ effect_policy_probe_core.lookup_enabled = true;
3739
+ effect_policy_probe_core.calls = @splat(0);
3740
+ Probe.init(fx);
3741
+ Probe.dispatch(fx, .open_save_sink);
3742
+ Probe.dispatch(fx, .save_file);
3743
+ Probe.drain(fx);
3744
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingFileCount());
3745
+ // The policy redirects the refusal's error route to loaded.
3746
+ try std.testing.expectEqualStrings("rejected", Probe.model().status);
3747
+ try std.testing.expectEqual(@as(i64, 0), Probe.model().errs);
3748
+ Probe.dispatch(fx, .drop_save);
3749
+ Probe.drain(fx);
3750
+ try std.testing.expectEqualStrings("cancelled", Probe.model().last_err);
3751
+ try std.testing.expectEqual(@as(usize, 1), effect_policy_probe_core.calls[0]);
3752
+ }
3753
+
3754
+ const stream_policy_probe_core = struct {
3755
+ pub const rt = mini_core.rt;
3756
+ pub const Model = mini_core.Model;
3757
+ pub const Msg = mini_core.Msg;
3758
+ pub const initialModel = mini_core.initialModel;
3759
+ pub const bootCommand = mini_core.bootCommand;
3760
+ pub const update = mini_core.update;
3761
+ pub const commitModelRoot = mini_core.commitModelRoot;
3762
+ var admit = false;
3763
+ var lookup = false;
3764
+ var calls = [_]usize{0} ** 5;
3765
+ pub fn nativeStreamPolicy(request: []const u8, output: []u8) usize {
3766
+ calls[request[0]] += 1;
3767
+ @memset(rt.frameAlloc(u8, 37), 255);
3768
+ switch (request[0]) {
3769
+ 0 => {
3770
+ output[0] = if (admit) 6 else 255;
3771
+ return 1;
3772
+ },
3773
+ 1 => {
3774
+ output[0] = if (lookup) 6 else 255;
3775
+ return 1;
3776
+ },
3777
+ 2 => {
3778
+ output[0..5].* = .{ 8, 0, 1, 0, 0 }; // failed instead of got_line
3779
+ return 5;
3780
+ },
3781
+ 3 => {
3782
+ output[0..5].* = .{ 26, 1, 0, 1, 0 }; // number even for collect
3783
+ return 5;
3784
+ },
3785
+ 4 => {
3786
+ output[0..5].* = .{ 8, 0, 1, 1, 1 }; // truncated even for success
3787
+ return 5;
3788
+ },
3789
+ else => unreachable,
3790
+ }
3791
+ }
3792
+ };
3793
+
3794
+ test "stream host consumes compiled admission lookup line payload route and retirement" {
3795
+ const Probe = ts_core_host.TsCoreHost(stream_policy_probe_core);
3796
+ const fx = freshChannel();
3797
+ defer fx.deinit();
3798
+ stream_policy_probe_core.admit = false;
3799
+ stream_policy_probe_core.lookup = false;
3800
+ stream_policy_probe_core.calls = @splat(0);
3801
+ Probe.init(fx);
3802
+ Probe.dispatch(fx, .run_lines);
3803
+ Probe.drain(fx);
3804
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingSpawnCount());
3805
+ try std.testing.expectEqualStrings("rejected", Probe.model().last_err);
3806
+ stream_policy_probe_core.admit = true;
3807
+ Probe.dispatch(fx, .run_lines);
3808
+ const key = ts_core_host.spawn_key_base + 6;
3809
+ try std.testing.expectEqual(key, fx.pendingSpawnAt(0).?.key);
3810
+ try fx.feedLine(key, "redirected line");
3811
+ Probe.drain(fx);
3812
+ try std.testing.expectEqualStrings("redirected line", Probe.model().last_err);
3813
+ try std.testing.expectEqual(@as(i64, 0), Probe.model().line_count);
3814
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingSpawnCount());
3815
+ Probe.dispatch(fx, .stop_job); // Lookup refuses the real stored key.
3816
+ Probe.drain(fx);
3817
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingSpawnCount());
3818
+ stream_policy_probe_core.lookup = true;
3819
+ Probe.dispatch(fx, .stop_job);
3820
+ Probe.drain(fx);
3821
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingSpawnCount());
3822
+ Probe.dispatch(fx, .run_collect); // Retired slot may be reused.
3823
+ try fx.feedOutput(key, "collected bytes");
3824
+ try fx.feedExit(key, 7);
3825
+ Probe.drain(fx);
3826
+ try std.testing.expectEqual(@as(i64, 7), Probe.model().exit_code);
3827
+ try std.testing.expectEqualStrings("", Probe.model().output);
3828
+ Probe.dispatch(fx, .stream_get);
3829
+ try std.testing.expectEqual(key, fx.pendingFetchAt(0).?.key);
3830
+ try fx.feedResponse(key, 200, "");
3831
+ Probe.drain(fx);
3832
+ try std.testing.expectEqualStrings("truncated", Probe.model().last_err);
3833
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingFetchCount());
3834
+ try std.testing.expectEqualSlices(usize, &.{ 4, 2, 1, 2, 1 }, &stream_policy_probe_core.calls);
3835
+ }
3836
+
3837
+ // Observe cross-family facts while redirecting native slots through both callbacks.
3838
+ const mixed_effect_stream_probe_core = struct {
3839
+ pub const rt = mini_core.rt;
3840
+ pub const Model = mini_core.Model;
3841
+ pub const Msg = mini_core.Msg;
3842
+ pub const initialModel = mini_core.initialModel;
3843
+ pub const bootCommand = mini_core.bootCommand;
3844
+ pub const update = mini_core.update;
3845
+ pub const subscriptions = mini_core.subscriptions;
3846
+ pub const commitModelRoot = mini_core.commitModelRoot;
3847
+ pub const nativeEffectPolicy = effect_policy_probe_core.nativeEffectPolicy;
3848
+ var buffered_blocked: [2]bool = undefined;
3849
+ var admissions: usize = 0;
3850
+ pub fn nativeStreamPolicy(request: []const u8, output: []u8) usize {
3851
+ if (request[0] == 1) {
3852
+ stream_policy_probe_core.calls[1] += 1;
3853
+ var at: usize = 2 + @as(usize, request[1]);
3854
+ for (0..6) |_| at += 2 + @as(usize, request[at + 1]);
3855
+ output[0] = if (request[at] == 1) 6 else 255;
3856
+ return 1;
3857
+ }
3858
+ if (request[0] == 4) {
3859
+ stream_policy_probe_core.calls[4] += 1;
3860
+ output[0..5].* = .{ 8, 0, 0, 1, 0 }; // Cancelled response follows the error route.
3861
+ return 5;
3862
+ }
3863
+ if (request[0] == 0) {
3864
+ buffered_blocked[admissions] = request[3] == 1;
3865
+ admissions += 1;
3866
+ output[0] = if (request[2] == 1 or request[3] == 1) 255 else 6;
3867
+ return 1;
3868
+ }
3869
+ return stream_policy_probe_core.nativeStreamPolicy(request, output);
3870
+ }
3871
+ };
3872
+
3873
+ test "compiled named and streaming policies share key occupancy and preserve cancellation priority" {
3874
+ const Probe = ts_core_host.TsCoreHost(mixed_effect_stream_probe_core);
3875
+ const fx = freshChannel();
3876
+ defer fx.deinit();
3877
+ effect_policy_probe_core.lookup_enabled = true;
3878
+ effect_policy_probe_core.calls = @splat(0);
3879
+ stream_policy_probe_core.lookup = true;
3880
+ stream_policy_probe_core.calls = @splat(0);
3881
+ mixed_effect_stream_probe_core.admissions = 0;
3882
+ Probe.init(fx);
3883
+ Probe.dispatch(fx, .get);
3884
+ try std.testing.expectEqual(ts_core_host.effect_key_base + 5, fx.pendingFetchAt(0).?.key);
3885
+ Probe.dispatch(fx, .stream_over_get);
3886
+ Probe.drain(fx);
3887
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingFetchCount());
3888
+ try std.testing.expectEqualStrings("rejected", Probe.model().last_err);
3889
+ Probe.dispatch(fx, .drop_get);
3890
+ Probe.drain(fx); // Cancelled buffered predecessor retires silently.
3891
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingFetchCount());
3892
+ try std.testing.expectEqual(@as(i64, 1), Probe.model().errs);
3893
+ Probe.dispatch(fx, .stream_get);
3894
+ try std.testing.expectEqual(ts_core_host.spawn_key_base + 6, fx.pendingFetchAt(0).?.key);
3895
+ Probe.dispatch(fx, .get_over_stream);
3896
+ Probe.drain(fx);
3897
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingFetchCount());
3898
+ try std.testing.expectEqual(@as(i64, 2), Probe.model().errs);
3899
+ Probe.dispatch(fx, .stop_stream);
3900
+ Probe.drain(fx);
3901
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingFetchCount());
3902
+ try std.testing.expectEqualStrings("cancelled", Probe.model().last_err);
3903
+ try std.testing.expectEqualSlices(bool, &.{ true, false }, &mixed_effect_stream_probe_core.buffered_blocked);
3904
+ try std.testing.expect(effect_policy_probe_core.calls[0] == 1 and effect_policy_probe_core.calls[1] >= 3 and effect_policy_probe_core.calls[17] == 1);
3905
+ try std.testing.expect(stream_policy_probe_core.calls[1] > 0 and stream_policy_probe_core.calls[4] == 1);
3906
+ }
3907
+
3908
+ // Complete capability records deliberately shuffle enum declaration order.
3909
+ const complete_core = struct {
3910
+ pub const rt = mini_core.rt;
3911
+ const Line = struct { key: []const u8, line: []const u8, truncated: bool, droppedBefore: f64 };
3912
+ const Exit = struct { key: []const u8, code: f64, reason: enum { rejected, signaled, cancelled, spawn_failed, exited }, droppedLines: f64, output: []const u8, outputTruncated: bool, stderrTail: []const u8, stderrTruncated: bool };
3913
+ const Clipboard = struct { key: []const u8, operation: enum { write, read }, outcome: enum { cancelled, rejected, failed, ok }, text: []const u8, droppedBefore: f64 };
3914
+ const Snapshot = struct { key: []const u8, active: bool, surface: f64, playing: bool, buffering: bool, completed: bool, looping: bool, muted: bool, source: enum { stream, local }, positionMs: f64, durationMs: f64, width: f64, height: f64, volume: f64 };
3915
+ pub const Model = struct { line: ?Line = null, exit: ?Exit = null, clipboard: ?Clipboard = null, snapshot: ?Snapshot = null, replies: usize = 0 };
3916
+ pub const Msg = union(enum) {
3917
+ commands: []const u8,
3918
+ line: Line,
3919
+ exit: Exit,
3920
+ clipboard: Clipboard,
3921
+ snapshot: Snapshot,
3922
+ video: struct { state: enum { failed, rejected, completed, loaded, position }, positionMs: f64, durationMs: f64, playing: bool, buffering: bool, width: f64, height: f64 },
3923
+ };
3924
+ pub const UpdateResult = struct { model: *const Model, cmd: []const u8 };
3925
+ var model: Model = .{};
3926
+ var owned: [6][8192]u8 = undefined;
3927
+ fn bytes(value: []const u8, slot: usize) []const u8 {
3928
+ std.debug.assert(value.len <= owned[slot].len);
3929
+ @memcpy(owned[slot][0..value.len], value);
3930
+ return owned[slot][0..value.len];
3931
+ }
3932
+ pub fn initialModel() *const Model {
3933
+ model = .{};
3934
+ return &model;
3935
+ }
3936
+ pub fn commitModelRoot(value: *const Model) *const Model {
3937
+ return value;
3938
+ }
3939
+ pub fn update(value: *const Model, msg: Msg) UpdateResult {
3940
+ switch (msg) {
3941
+ .commands => |wire| return .{ .model = value, .cmd = wire },
3942
+ .line => |line| {
3943
+ model.line = line;
3944
+ model.line.?.key = bytes(line.key, 0);
3945
+ model.line.?.line = bytes(line.line, 1);
3946
+ },
3947
+ .exit => |exit| {
3948
+ model.exit = exit;
3949
+ model.exit.?.key = bytes(exit.key, 2);
3950
+ model.exit.?.output = bytes(exit.output, 3);
3951
+ model.exit.?.stderrTail = bytes(exit.stderrTail, 4);
3952
+ },
3953
+ .clipboard => |clip| {
3954
+ model.clipboard = clip;
3955
+ model.clipboard.?.key = bytes(clip.key, 5);
3956
+ },
3957
+ .snapshot => |snapshot| {
3958
+ model.snapshot = snapshot;
3959
+ model.snapshot.?.key = bytes(snapshot.key, 5);
3960
+ },
3961
+ .video => {},
3962
+ }
3963
+ model.replies += 1;
3964
+ return .{ .model = &model, .cmd = "" };
3965
+ }
3966
+ fn pair(a: []const u8, b: []const u8) []const u8 {
3967
+ const out = rt.frameAlloc(u8, a.len + b.len);
3968
+ @memcpy(out[0..a.len], a);
3969
+ @memcpy(out[a.len..], b);
3970
+ return out;
3971
+ }
3972
+ fn spawn(key: []const u8, collect: bool) []const u8 {
3973
+ const argv = [_][]const u8{ "worker", "argument" };
3974
+ const out = rt.frameAlloc(u8, 6 + key.len + 4 + 6 + 4 + 8 + 4);
3975
+ out[0] = 0x33;
3976
+ out[1] = @intCast(key.len);
3977
+ @memcpy(out[2..][0..key.len], key);
3978
+ var at = 2 + key.len;
3979
+ out[at] = 1;
3980
+ out[at + 1] = 2;
3981
+ out[at + 2] = @intFromBool(collect);
3982
+ out[at + 3] = argv.len;
3983
+ at += 4;
3984
+ for (argv) |arg| at = mini_core.writeLongBytes(out, at, arg);
3985
+ _ = mini_core.writeLongBytes(out, at, "");
3986
+ return out;
3987
+ }
3988
+ fn clipboard(key: []const u8) []const u8 {
3989
+ const out = rt.frameAlloc(u8, 7 + key.len + 4);
3990
+ out[0] = 0x34;
3991
+ out[1] = @intCast(key.len);
3992
+ @memcpy(out[2..][0..key.len], key);
3993
+ out[2 + key.len] = 3;
3994
+ _ = mini_core.writeLongBytes(out, 3 + key.len, "copy");
3995
+ return out;
3996
+ }
3997
+ };
3998
+ const CompleteHost = ts_core_host.TsCoreHost(complete_core);
3999
+ var complete_channel: CompleteHost.Fx = undefined;
4000
+ fn freshComplete() *CompleteHost.Fx {
4001
+ complete_channel = CompleteHost.Fx.init(std.testing.allocator);
4002
+ complete_channel.executor = .fake;
4003
+ CompleteHost.init(&complete_channel);
4004
+ return &complete_channel;
4005
+ }
4006
+ test "complete subprocess records retain all metadata, rejection keys, collected output and stderr" {
4007
+ const fx = freshComplete();
4008
+ defer fx.deinit();
4009
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.spawn("lines", false) });
4010
+ const key = fx.pendingSpawnAt(0).?.key;
4011
+ try fx.feedLineWithMetadata(key, "Caf\xc3\xa9\x00tail", true, 17);
4012
+ CompleteHost.drain(fx);
4013
+ try std.testing.expectEqualDeep(complete_core.Line{ .key = "lines", .line = "Caf\xc3\xa9\x00tail", .truncated = true, .droppedBefore = 17 }, CompleteHost.model().line.?);
4014
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.spawn("lines", false) });
4015
+ CompleteHost.drain(fx);
4016
+ try std.testing.expectEqualStrings("lines", CompleteHost.model().exit.?.key);
4017
+ try std.testing.expectEqual(.rejected, CompleteHost.model().exit.?.reason);
4018
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingSpawnCount());
4019
+ try fx.feedExitWithMetadata(key, -9, .signaled, .{ .dropped_lines = 31, .output_truncated = true, .stderr_truncated = true });
4020
+ CompleteHost.drain(fx);
4021
+ try std.testing.expectEqualDeep(complete_core.Exit{ .key = "lines", .code = -9, .reason = .signaled, .droppedLines = 31, .output = "", .outputTruncated = true, .stderrTail = "", .stderrTruncated = true }, CompleteHost.model().exit.?);
4022
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.spawn("lines", true) });
4023
+ const collected = fx.pendingSpawnAt(0).?.key;
4024
+ try fx.feedOutput(collected, "output\x00owned");
4025
+ try fx.feedStderr(collected, "stderr\xc3\xa9");
4026
+ try fx.feedExit(collected, 7);
4027
+ CompleteHost.drain(fx);
4028
+ try std.testing.expectEqualStrings("output\x00owned", CompleteHost.model().exit.?.output);
4029
+ try std.testing.expectEqualStrings("stderr\xc3\xa9", CompleteHost.model().exit.?.stderrTail);
4030
+ try std.testing.expectEqual(@as(f64, 7), CompleteHost.model().exit.?.code);
4031
+ // Starting a new cycle resets transient command bytes; retained result
4032
+ // bytes must still match the entire prior completion.
4033
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.spawn("next", false) });
4034
+ try std.testing.expectEqualStrings("output\x00owned", CompleteHost.model().exit.?.output);
4035
+ CompleteHost.dispatch(fx, .{ .commands = mini_core.cmdCancel("next") });
4036
+ CompleteHost.drain(fx);
4037
+ try std.testing.expectEqual(.cancelled, CompleteHost.model().exit.?.reason);
4038
+ }
4039
+ test "routed clipboard duplicates reject without stealing the original completion and cancel retires the key" {
4040
+ const fx = freshComplete();
4041
+ defer fx.deinit();
4042
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.clipboard("copy-key") });
4043
+ const key = fx.pendingClipboardAt(0).?.key;
4044
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.clipboard("copy-key") });
4045
+ CompleteHost.drain(fx);
4046
+ try std.testing.expectEqualDeep(complete_core.Clipboard{ .key = "copy-key", .operation = .write, .outcome = .rejected, .text = "", .droppedBefore = 0 }, CompleteHost.model().clipboard.?);
4047
+ try std.testing.expectEqual(key, fx.pendingClipboardAt(0).?.key);
4048
+ try fx.feedClipboardResult(key, .ok, "ignored");
4049
+ CompleteHost.drain(fx);
4050
+ try std.testing.expectEqual(.ok, CompleteHost.model().clipboard.?.outcome);
4051
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.clipboard("copy-key") });
4052
+ CompleteHost.dispatch(fx, .{ .commands = mini_core.cmdCancel("copy-key") });
4053
+ CompleteHost.drain(fx);
4054
+ try std.testing.expectEqual(.cancelled, CompleteHost.model().clipboard.?.outcome);
4055
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.clipboard("copy-key") });
4056
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingClipboardCount());
4057
+ }
4058
+ test "playback snapshot captures command-order state and restart retains ownership without commandeering a foreign load" {
4059
+ const fx = freshComplete();
4060
+ defer fx.deinit();
4061
+ CompleteHost.dispatch(fx, .{ .commands = mini_core.cmdVideoLoad("owned", 5, 0x7601, "clip.mp4", "", 7) });
4062
+ try fx.feedVideoEvent(.loaded, 1234, 60000, true, true, 1920, 1080);
4063
+ CompleteHost.drain(fx);
4064
+ const pause = mini_core.cmdVideoCtl("owned", 1, 0);
4065
+ const command = complete_core.pair(&.{ 0x35, 4 }, pause);
4066
+ CompleteHost.dispatch(fx, .{ .commands = command });
4067
+ const snapshot = CompleteHost.model().snapshot.?;
4068
+ try std.testing.expectEqualDeep(complete_core.Snapshot{ .key = "owned", .active = true, .surface = 0x7601, .playing = true, .buffering = true, .completed = false, .looping = true, .muted = true, .source = .local, .positionMs = 1234, .durationMs = 60000, .width = 1920, .height = 1080, .volume = 1 }, snapshot);
4069
+ try std.testing.expect(!fx.videoSnapshot().playing);
4070
+ try fx.feedVideoEvent(.completed, 60000, 60000, false, false, 1920, 1080);
4071
+ CompleteHost.drain(fx);
4072
+ const token = fx.videoOwnerToken();
4073
+ CompleteHost.dispatch(fx, .{ .commands = mini_core.cmdVideoCtl("owned", 7, 0) });
4074
+ try std.testing.expect(fx.videoOwnerToken() != token);
4075
+ CompleteHost.dispatch(fx, .{ .commands = mini_core.cmdVideoCtl("owned", 1, 0) });
4076
+ try std.testing.expect(!fx.videoSnapshot().playing);
4077
+ fx.loadVideo(.{ .key = 77, .surface = 99, .path = "foreign.mp4" });
4078
+ const foreign = fx.videoSnapshot();
4079
+ CompleteHost.dispatch(fx, .{ .commands = mini_core.cmdVideoCtl("owned", 7, 0) });
4080
+ try std.testing.expectEqualDeep(foreign, fx.videoSnapshot());
4081
+ CompleteHost.dispatch(fx, .{ .commands = &.{ 0x35, 4 } });
4082
+ try std.testing.expectEqualStrings("", CompleteHost.model().snapshot.?.key);
4083
+ }
4084
+
4085
+ test "routed clipboard keys reject cross-family collisions and every full-capacity terminal returns its original key" {
4086
+ const fx = freshComplete();
4087
+ defer fx.deinit();
4088
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.spawn("shared", false) });
4089
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.clipboard("shared") });
4090
+ CompleteHost.drain(fx);
4091
+ try std.testing.expectEqual(.rejected, CompleteHost.model().clipboard.?.outcome);
4092
+ try std.testing.expectEqualStrings("shared", CompleteHost.model().clipboard.?.key);
4093
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingClipboardCount());
4094
+ CompleteHost.dispatch(fx, .{ .commands = mini_core.cmdCancel("shared") });
4095
+ CompleteHost.drain(fx);
4096
+ for (0..effects_mod.max_effects) |i| {
4097
+ var key: [16]u8 = undefined;
4098
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.clipboard(try std.fmt.bufPrint(&key, "copy-{d}", .{i})) });
4099
+ }
4100
+ try std.testing.expectEqual(effects_mod.max_effects, fx.pendingClipboardCount());
4101
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.clipboard("overflow") });
4102
+ CompleteHost.drain(fx);
4103
+ try std.testing.expectEqualStrings("overflow", CompleteHost.model().clipboard.?.key);
4104
+ try std.testing.expectEqual(.rejected, CompleteHost.model().clipboard.?.outcome);
4105
+ // A new stream cannot steal a pending clipboard key either.
4106
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.spawn("copy-0", false) });
4107
+ CompleteHost.drain(fx);
4108
+ try std.testing.expectEqualStrings("copy-0", CompleteHost.model().exit.?.key);
4109
+ try std.testing.expectEqual(.rejected, CompleteHost.model().exit.?.reason);
4110
+ for (0..effects_mod.max_effects) |_| {
4111
+ const request = fx.pendingClipboardAt(0).?;
4112
+ try fx.feedClipboardResult(request.key, .ok, "");
4113
+ CompleteHost.drain(fx);
4114
+ }
4115
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingClipboardCount());
4116
+ CompleteHost.dispatch(fx, .{ .commands = complete_core.clipboard("reused") });
4117
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingClipboardCount());
4118
+ }
4119
+
4120
+ const complete_storage_core = struct {
4121
+ pub const rt = mini_core.rt;
4122
+ const File = struct {
4123
+ key: []const u8,
4124
+ operation: effects_mod.EffectFileOp,
4125
+ event: enum { done, chunk, terminal },
4126
+ outcome: effects_mod.EffectFileOutcome,
4127
+ bytes: []const u8,
4128
+ totalBytes: []const u8,
4129
+ mtimeMs: []const u8,
4130
+ exists: bool,
4131
+ droppedBefore: f64,
4132
+ };
4133
+ const Timer = struct { key: []const u8, timestampNs: []const u8, outcome: enum { rejected, fired } };
4134
+ pub const Model = struct { file: ?File = null, timer: ?Timer = null, stamp: []const u8 = "", replies: usize = 0 };
4135
+ pub const Msg = union(enum) { commands: []const u8, file: File, timer: Timer, stamp: []const u8 };
4136
+ pub const UpdateResult = struct { model: *const Model, cmd: []const u8 };
4137
+ var model: Model = .{};
4138
+ var owned: [7][8192]u8 = undefined;
4139
+ fn bytes(value: []const u8, slot: usize) []const u8 {
4140
+ std.debug.assert(value.len <= owned[slot].len);
4141
+ @memcpy(owned[slot][0..value.len], value);
4142
+ return owned[slot][0..value.len];
4143
+ }
4144
+ pub fn initialModel() *const Model {
4145
+ model = .{};
4146
+ return &model;
4147
+ }
4148
+ pub fn commitModelRoot(value: *const Model) *const Model {
4149
+ return value;
4150
+ }
4151
+ pub fn update(value: *const Model, msg: Msg) UpdateResult {
4152
+ switch (msg) {
4153
+ .commands => |wire| return .{ .model = value, .cmd = wire },
4154
+ .file => |file| {
4155
+ model.file = file;
4156
+ model.file.?.key = bytes(file.key, 0);
4157
+ model.file.?.bytes = bytes(file.bytes, 1);
4158
+ model.file.?.totalBytes = bytes(file.totalBytes, 2);
4159
+ model.file.?.mtimeMs = bytes(file.mtimeMs, 3);
4160
+ },
4161
+ .timer => |timer| {
4162
+ model.timer = timer;
4163
+ model.timer.?.key = bytes(timer.key, 4);
4164
+ model.timer.?.timestampNs = bytes(timer.timestampNs, 5);
4165
+ },
4166
+ .stamp => |stamp| model.stamp = bytes(stamp, 6),
4167
+ }
4168
+ model.replies += 1;
4169
+ return .{ .model = &model, .cmd = "" };
4170
+ }
4171
+ fn fileCommand(key: []const u8, write: bool) []const u8 {
4172
+ const path = "notes.bin";
4173
+ const content = "body\x00\xff";
4174
+ const out = rt.frameAlloc(u8, 3 + key.len + 4 + path.len + (if (write) @as(usize, 4 + content.len) else 0));
4175
+ out[0] = if (write) 0x38 else 0x37;
4176
+ out[1] = @intCast(key.len);
4177
+ @memcpy(out[2..][0..key.len], key);
4178
+ out[2 + key.len] = @intFromEnum(std.meta.Tag(Msg).file);
4179
+ const at = mini_core.writeLongBytes(out, 3 + key.len, path);
4180
+ if (write) _ = mini_core.writeLongBytes(out, at, content);
4181
+ return out;
4182
+ }
4183
+ fn timerCommand(key: []const u8, repeating: bool, ms: f64) []const u8 {
4184
+ const out = rt.frameAlloc(u8, 12 + key.len);
4185
+ out[0] = 0x3A;
4186
+ out[1] = @intCast(key.len);
4187
+ @memcpy(out[2..][0..key.len], key);
4188
+ out[2 + key.len] = @intFromBool(repeating);
4189
+ std.mem.writeInt(u64, out[3 + key.len ..][0..8], @bitCast(ms), .little);
4190
+ out[11 + key.len] = @intFromEnum(std.meta.Tag(Msg).timer);
4191
+ return out;
4192
+ }
4193
+ };
4194
+ const StorageHost = ts_core_host.TsCoreHost(complete_storage_core);
4195
+
4196
+ test "complete storage records own binary keys bodies and exact signed clock and file metadata" {
4197
+ var fx = StorageHost.Fx.init(std.testing.allocator);
4198
+ defer fx.deinit();
4199
+ fx.executor = .fake;
4200
+ var clock: runtime_clock.TestClock = .{};
4201
+ fx.clock = clock.clock();
4202
+ StorageHost.init(&fx);
4203
+ for ([_]i64{ std.math.minInt(i64), -9007199254740993, -1, 0, 9007199254740993, std.math.maxInt(i64) }) |value| {
4204
+ clock.setWallMs(value);
4205
+ StorageHost.dispatch(&fx, .{ .commands = &.{ 0x39, @intFromEnum(std.meta.Tag(complete_storage_core.Msg).stamp) } });
4206
+ var buffer: [21]u8 = undefined;
4207
+ try std.testing.expectEqualStrings(try std.fmt.bufPrint(&buffer, "{d}", .{value}), StorageHost.model().stamp);
4208
+ complete_storage_core.rt.frameReset();
4209
+ @memset(complete_storage_core.rt.frameAlloc(u8, 512), 0xA5);
4210
+ try std.testing.expectEqualStrings(try std.fmt.bufPrint(&buffer, "{d}", .{value}), StorageHost.model().stamp);
4211
+ }
4212
+ inline for (std.meta.tags(effects_mod.EffectFileOutcome)) |outcome| {
4213
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.fileCommand("file\x00\xff", false) });
4214
+ const pending = fx.pendingFileAt(0).?;
4215
+ try std.testing.expectEqualStrings("notes.bin", pending.path);
4216
+ try fx.feedFileResultDetailed(.{ .key = pending.key, .op = .read, .outcome = outcome, .bytes = "body\x00\xff", .total = std.math.maxInt(u64), .mtime_ms = std.math.minInt(i64), .exists = true });
4217
+ StorageHost.drain(&fx);
4218
+ try std.testing.expectEqualDeep(complete_storage_core.File{
4219
+ .key = "file\x00\xff",
4220
+ .operation = .read,
4221
+ .event = .terminal,
4222
+ .outcome = outcome,
4223
+ .bytes = if (outcome == .ok or outcome == .truncated) "body\x00\xff" else "",
4224
+ .totalBytes = "18446744073709551615",
4225
+ .mtimeMs = "-9223372036854775808",
4226
+ .exists = true,
4227
+ .droppedBefore = 0,
4228
+ }, StorageHost.model().file.?);
4229
+ complete_storage_core.rt.frameReset();
4230
+ @memset(complete_storage_core.rt.frameAlloc(u8, 512), 0xA5);
4231
+ try std.testing.expectEqualStrings("file\x00\xff", StorageHost.model().file.?.key);
4232
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.fileCommand("write", true) });
4233
+ const write = fx.pendingFileAt(0).?;
4234
+ try std.testing.expectEqualStrings("body\x00\xff", write.bytes);
4235
+ try fx.feedFileResult(write.key, outcome, "ignored");
4236
+ StorageHost.drain(&fx);
4237
+ try std.testing.expectEqual(outcome, StorageHost.model().file.?.outcome);
4238
+ try std.testing.expectEqual(.write, StorageHost.model().file.?.operation);
4239
+ try std.testing.expectEqualStrings("", StorageHost.model().file.?.bytes);
4240
+ }
4241
+ }
4242
+
4243
+ test "complete storage admission preserves key ownership cancellation replacement and capacity" {
4244
+ var fx = StorageHost.Fx.init(std.testing.allocator);
4245
+ defer fx.deinit();
4246
+ fx.executor = .fake;
4247
+ StorageHost.init(&fx);
4248
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.timerCommand("timer", true, 30) });
4249
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.fileCommand("timer", false) });
4250
+ complete_storage_core.rt.frameReset();
4251
+ @memset(complete_storage_core.rt.frameAlloc(u8, 512), 0xA5);
4252
+ StorageHost.drain(&fx);
4253
+ try std.testing.expectEqual(.rejected, StorageHost.model().file.?.outcome);
4254
+ try std.testing.expectEqualStrings("timer", StorageHost.model().file.?.key);
4255
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingTimerCount());
4256
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.fileCommand("replace", false) });
4257
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.fileCommand("replace", true) });
4258
+ StorageHost.drain(&fx);
4259
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingFileCount());
4260
+ try std.testing.expectEqual(.write, fx.pendingFileAt(0).?.op);
4261
+ const count = StorageHost.model().replies;
4262
+ StorageHost.dispatch(&fx, .{ .commands = mini_core.cmdCancel("replace") });
4263
+ StorageHost.drain(&fx);
4264
+ try std.testing.expectEqual(count, StorageHost.model().replies);
4265
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingFileCount());
4266
+ for (0..16) |i| {
4267
+ var key: [16]u8 = undefined;
4268
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.fileCommand(try std.fmt.bufPrint(&key, "file{d}", .{i}), false) });
4269
+ }
4270
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.fileCommand("overflow", true) });
4271
+ StorageHost.drain(&fx);
4272
+ try std.testing.expectEqualDeep(complete_storage_core.File{ .key = "overflow", .operation = .write, .event = .terminal, .outcome = .rejected, .bytes = "", .totalBytes = "0", .mtimeMs = "0", .exists = false, .droppedBefore = 0 }, StorageHost.model().file.?);
4273
+ try std.testing.expectEqual(@as(usize, 16), fx.pendingFileCount());
4274
+ }
4275
+
4276
+ test "complete timers preserve modes rearming explicit refusals cross-family ownership and cancellation" {
4277
+ var fx = StorageHost.Fx.init(std.testing.allocator);
4278
+ defer fx.deinit();
4279
+ fx.executor = .fake;
4280
+ StorageHost.init(&fx);
4281
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.timerCommand("pulse", true, 30.5) });
4282
+ const key = fx.pendingTimerAt(0).?.key;
4283
+ try std.testing.expectEqual(@as(u64, 31), fx.pendingTimerAt(0).?.interval_ms);
4284
+ for (0..2) |_| {
4285
+ try fx.fireTimer(key);
4286
+ StorageHost.drain(&fx);
4287
+ try std.testing.expectEqualDeep(complete_storage_core.Timer{ .key = "pulse", .timestampNs = "0", .outcome = .fired }, StorageHost.model().timer.?);
4288
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingTimerCount());
4289
+ }
4290
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.timerCommand("pulse", false, 40) });
4291
+ try std.testing.expectEqual(key, fx.pendingTimerAt(0).?.key);
4292
+ try fx.fireTimer(key);
4293
+ StorageHost.drain(&fx);
4294
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingTimerCount());
4295
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.fileCommand("file", false) });
4296
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.timerCommand("file", false, 40) });
4297
+ StorageHost.drain(&fx);
4298
+ try std.testing.expectEqual(.rejected, StorageHost.model().timer.?.outcome);
4299
+ try std.testing.expectEqualStrings("file", StorageHost.model().timer.?.key);
4300
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingFileCount());
4301
+ for ([_]f64{ 0, -1, std.math.nan(f64), std.math.inf(f64), 31536000001 }) |invalid| {
4302
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.timerCommand("invalid", false, invalid) });
4303
+ StorageHost.drain(&fx);
4304
+ try std.testing.expectEqual(.rejected, StorageHost.model().timer.?.outcome);
4305
+ try std.testing.expectEqualStrings("invalid", StorageHost.model().timer.?.key);
4306
+ }
4307
+ for (0..16) |i| {
4308
+ var name: [16]u8 = undefined;
4309
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.timerCommand(try std.fmt.bufPrint(&name, "timer{d}", .{i}), true, 1) });
4310
+ }
4311
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.timerCommand("overflow", false, 1) });
4312
+ complete_storage_core.rt.frameReset();
4313
+ @memset(complete_storage_core.rt.frameAlloc(u8, 512), 0xA5);
4314
+ StorageHost.drain(&fx);
4315
+ try std.testing.expectEqual(.rejected, StorageHost.model().timer.?.outcome);
4316
+ try std.testing.expectEqualStrings("overflow", StorageHost.model().timer.?.key);
4317
+ try std.testing.expectEqual(@as(usize, 16), fx.pendingTimerCount());
4318
+ StorageHost.dispatch(&fx, .{ .commands = mini_core.cmdCancel("timer0") });
4319
+ StorageHost.drain(&fx);
4320
+ try std.testing.expectEqual(@as(usize, 15), fx.pendingTimerCount());
4321
+ }
4322
+
4323
+ test "complete storage owners refuse later basic commands and retain same-family replacement" {
4324
+ var fx = StorageHost.Fx.init(std.testing.allocator);
4325
+ defer fx.deinit();
4326
+ fx.executor = .fake;
4327
+ StorageHost.init(&fx);
4328
+ const reply_tag = @intFromEnum(std.meta.Tag(complete_storage_core.Msg).stamp);
4329
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.timerCommand("owned", true, 50) });
4330
+ StorageHost.dispatch(&fx, .{ .commands = mini_core.cmdReadFile("owned", reply_tag, reply_tag, "notes.bin") });
4331
+ StorageHost.drain(&fx);
4332
+ try std.testing.expectEqualStrings("rejected", StorageHost.model().stamp);
4333
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingFileCount());
4334
+ StorageHost.dispatch(&fx, .{ .commands = mini_core.cmdRequest("echo", "owned", reply_tag, reply_tag, "body") });
4335
+ StorageHost.drain(&fx);
4336
+ try std.testing.expectEqualStrings("rejected", StorageHost.model().stamp);
4337
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingHostCount());
4338
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingTimerCount());
4339
+ StorageHost.dispatch(&fx, .{ .commands = mini_core.cmdDelay("owned", 100, reply_tag) });
4340
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingTimerCount());
4341
+ try std.testing.expectEqual(@as(u32, 100), fx.pendingTimerAt(0).?.interval_ms);
4342
+ StorageHost.dispatch(&fx, .{ .commands = mini_core.cmdCancel("owned") });
4343
+ StorageHost.dispatch(&fx, .{ .commands = complete_storage_core.fileCommand("owned", false) });
4344
+ StorageHost.dispatch(&fx, .{ .commands = mini_core.cmdDelay("owned", 20, reply_tag) });
4345
+ StorageHost.dispatch(&fx, .{ .commands = mini_core.cmdRequest("echo", "owned", reply_tag, reply_tag, "body") });
4346
+ StorageHost.drain(&fx);
4347
+ try std.testing.expectEqualStrings("rejected", StorageHost.model().stamp);
4348
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingTimerCount());
4349
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingHostCount());
4350
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingFileCount());
4351
+ StorageHost.dispatch(&fx, .{ .commands = mini_core.cmdCancel("owned") });
4352
+ StorageHost.drain(&fx);
4353
+ StorageHost.dispatch(&fx, .{ .commands = mini_core.cmdDelay("owned", 20, reply_tag) });
4354
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingTimerCount());
4355
+ }
4356
+
4357
+ // Deliberately redirect portable decisions. OS requests and owned table facts
4358
+ // expose whether the consumer obeys the returned plan instead of recomputing it.
4359
+ const media_policy_probe_core = struct {
4360
+ pub const rt = mini_core.rt;
4361
+ pub const Model = mini_core.Model;
4362
+ pub const Msg = mini_core.Msg;
4363
+ pub const initialModel = mini_core.initialModel;
4364
+ pub const bootCommand = mini_core.bootCommand;
4365
+ pub const update = mini_core.update;
4366
+ pub const subscriptions = mini_core.subscriptions;
4367
+ pub const commitModelRoot = mini_core.commitModelRoot;
4368
+ var lookup = true;
4369
+ var retire = true;
4370
+ var transport = true;
4371
+ var calls = [_]usize{0} ** 8;
4372
+ var owned_after_restart = false;
4373
+
4374
+ pub fn nativeEffectPolicy(request: []const u8, output: []u8) usize {
4375
+ if (request[0] >= 8) return referenceCoordinationPolicy(request, output);
4376
+ calls[request[0]] += 1;
4377
+ if (request[0] == 1) {
4378
+ output[0] = 255;
4379
+ return 1;
4380
+ }
4381
+ // Allocate without resetting the borrowed command or event arena.
4382
+ @memset(rt.frameAlloc(u8, 37), 255);
4383
+ if (request[0] == 6) {
4384
+ @memset(output[0..12], 0);
4385
+ output[0] = 255;
4386
+ if (!transport) return 12;
4387
+ const video = request[1] == 1;
4388
+ const verb = request[4];
4389
+ output[0] = if (verb == 2) 2 else if (video and verb == 1) 7 else if (video and verb == 0) 1 else 3;
4390
+ output[1] = @intFromBool(verb == 2 and retire);
4391
+ output[2] = @intFromBool(output[0] == 7);
4392
+ std.mem.writeInt(u64, output[4..12], @bitCast(@as(f64, 321)), .little);
4393
+ if (video and verb == 0) owned_after_restart = request[3] == 1;
4394
+ return 12;
4395
+ }
4396
+ @memset(output[0..28], 0);
4397
+ output[0] = 1;
4398
+ output[1] = 255;
4399
+ output[2] = if (request[1] == 1) 53 else request[4];
4400
+ output[4] = request[5];
4401
+ output[5] = request[6];
4402
+ std.mem.writeInt(u32, output[24..28], std.mem.readInt(u32, request[16..20], .little), .little);
4403
+ const numeric = request[0] == 3 or request[0] == 4;
4404
+ const key: u64 = if (numeric) @as(u64, @intFromFloat(@as(f64, @bitCast(std.mem.readInt(u64, request[8..16], .little))))) else std.mem.readInt(u64, request[8..16], .little);
4405
+ std.mem.writeInt(u64, output[8..16], key, .little);
4406
+ std.mem.writeInt(u64, output[16..24], 987, .little);
4407
+ var matching: u8 = 255;
4408
+ for (0..request[2]) |slot| {
4409
+ const at = 28 + slot * 16;
4410
+ if (request[at] == 1 and std.mem.readInt(u64, request[at + 1 ..][0..8], .little) == key) matching = @intCast(slot);
4411
+ }
4412
+ if (request[0] == 3) {
4413
+ if (matching == 255) {
4414
+ for (0..request[2]) |offset| {
4415
+ const slot = (offset + 5) % request[2];
4416
+ if (request[28 + slot * 16] == 0) {
4417
+ output[1] = @intCast(slot);
4418
+ break;
4419
+ }
4420
+ }
4421
+ }
4422
+ } else if (lookup) output[1] = matching;
4423
+ if (request[0] == 5 and matching != 255) {
4424
+ const at = 28 + @as(usize, matching) * 16;
4425
+ // An accepted channel route was selected by the admission policy.
4426
+ std.debug.assert(request[1] != 1 or request[at + 9] == 53);
4427
+ output[2] = if (request[1] == 1) 47 else request[at + 9];
4428
+ output[3] = @intFromBool(retire and (request[1] == 0 or request[1] == 1 and request[3] != 0 or request[1] == 2 and request[3] >= 3));
4429
+ if (request[1] == 2) {
4430
+ output[4] = 1;
4431
+ output[5] = 2;
4432
+ std.mem.writeInt(u32, output[24..28], 24_000, .little);
4433
+ }
4434
+ }
4435
+ return 28;
4436
+ }
4437
+ };
4438
+
4439
+ const MediaProbe = ts_core_host.TsCoreHost(media_policy_probe_core);
4440
+ fn freshMediaProbe() *MediaProbe.Fx {
4441
+ media_policy_probe_core.lookup = true;
4442
+ media_policy_probe_core.retire = true;
4443
+ media_policy_probe_core.transport = true;
4444
+ media_policy_probe_core.owned_after_restart = false;
4445
+ media_policy_probe_core.calls = @splat(0);
4446
+ const fx = freshChannel();
4447
+ MediaProbe.init(fx);
4448
+ return fx;
4449
+ }
4450
+
4451
+ test "media host consumes redirected admission slots routes sizes lookup and terminal retirement" {
4452
+ const fx = freshMediaProbe();
4453
+ defer fx.deinit();
4454
+ MediaProbe.dispatch(fx, .load_img);
4455
+ try std.testing.expectEqual(@as(u64, 987), fx.pendingImageLoadAt(0).?.expected_bytes);
4456
+ MediaProbe.dispatch(fx, .open_chan);
4457
+ const handle = fx.channelHandle(41).?;
4458
+ try std.testing.expectEqual(effects_mod.ChannelHandle.PostResult.accepted, handle.post("owned\x00\xff"));
4459
+ MediaProbe.drain(fx);
4460
+ try std.testing.expectEqualStrings("owned\x00\xff", MediaProbe.model().chan_bytes);
4461
+ media_policy_probe_core.lookup = false;
4462
+ MediaProbe.dispatch(fx, .close_chan);
4463
+ try std.testing.expect(fx.channelHandle(41) != null);
4464
+ media_policy_probe_core.lookup = true;
4465
+ MediaProbe.dispatch(fx, .close_chan);
4466
+ MediaProbe.drain(fx);
4467
+ try std.testing.expect(fx.channelHandle(41) == null);
4468
+ MediaProbe.dispatch(fx, .open_chan);
4469
+ try std.testing.expect(fx.channelHandle(41) != null);
4470
+ media_policy_probe_core.retire = false;
4471
+ try fx.feedImageResult(7, .loaded, 2, 3, 200, "");
4472
+ MediaProbe.drain(fx);
4473
+ try std.testing.expectEqual(@as(i64, 1), MediaProbe.model().img_events);
4474
+ MediaProbe.dispatch(fx, .load_img);
4475
+ MediaProbe.drain(fx);
4476
+ try std.testing.expectEqual(mini_core.ImageState.rejected, MediaProbe.model().img_state);
4477
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingImageLoadCount());
4478
+ try std.testing.expect(media_policy_probe_core.calls[3] >= 4);
4479
+ try std.testing.expect(media_policy_probe_core.calls[4] >= 2);
4480
+ try std.testing.expect(media_policy_probe_core.calls[5] >= 3);
4481
+ }
4482
+
4483
+ test "media host consumes capture restoration data and terminal retirement" {
4484
+ const fx = freshMediaProbe();
4485
+ defer fx.deinit();
4486
+ MediaProbe.dispatch(fx, .start_capture);
4487
+ MediaProbe.drain(fx);
4488
+ try std.testing.expectEqual(mini_core.CaptureSource.system, MediaProbe.model().capture_source);
4489
+ try std.testing.expectEqual(@as(f64, 24_000), MediaProbe.model().capture_rate);
4490
+ try std.testing.expectEqual(@as(f64, 2), MediaProbe.model().capture_channels);
4491
+ try fx.feedAudioCapture(91, 9_876_543, &.{ 1, 0, 2, 0 });
4492
+ MediaProbe.drain(fx);
4493
+ try std.testing.expectEqualStrings("\x01\x00\x02\x00", MediaProbe.model().capture_pcm);
4494
+ MediaProbe.dispatch(fx, .stop_capture);
4495
+ MediaProbe.drain(fx);
4496
+ try std.testing.expectEqual(mini_core.CaptureState.stopped, MediaProbe.model().capture_state);
4497
+ try std.testing.expectEqual(@as(f64, 24_000), MediaProbe.model().capture_rate);
4498
+ MediaProbe.dispatch(fx, .start_capture);
4499
+ MediaProbe.drain(fx);
4500
+ try std.testing.expectEqual(mini_core.CaptureState.started, MediaProbe.model().capture_state);
4501
+ try std.testing.expectEqual(@as(usize, 4), media_policy_probe_core.calls[5]);
4502
+ }
4503
+
4504
+ test "media host consumes transport suppression scalar restart rekey and scoped foreign cancellation" {
4505
+ const fx = freshMediaProbe();
4506
+ defer fx.deinit();
4507
+ MediaProbe.dispatch(fx, .play);
4508
+ media_policy_probe_core.transport = false;
4509
+ const before = fx.audioSnapshot();
4510
+ MediaProbe.dispatch(fx, .pause_it);
4511
+ try std.testing.expectEqualDeep(before, fx.audioSnapshot());
4512
+ media_policy_probe_core.transport = true;
4513
+ MediaProbe.dispatch(fx, .seek_it);
4514
+ try std.testing.expectEqual(@as(u64, 321), fx.audioSnapshot().position_ms);
4515
+ MediaProbe.dispatch(fx, .vload);
4516
+ const token = fx.videoOwnerToken();
4517
+ MediaProbe.dispatch(fx, .vpause_it); // Policy requests restart instead.
4518
+ try std.testing.expect(fx.videoOwnerToken() != token);
4519
+ MediaProbe.dispatch(fx, .vplay_it);
4520
+ try std.testing.expect(media_policy_probe_core.owned_after_restart);
4521
+ try std.testing.expect(!fx.videoSnapshot().playing);
4522
+ MediaProbe.dispatch(fx, .vseek_it);
4523
+ try std.testing.expectEqual(@as(u64, 321), fx.videoSnapshot().position_ms);
4524
+ fx.loadVideo(.{ .key = 77, .surface = 99, .path = "foreign.mp4" });
4525
+ const foreign = fx.videoSnapshot();
4526
+ MediaProbe.dispatch(fx, .vstop_it);
4527
+ try std.testing.expectEqualDeep(foreign, fx.videoSnapshot());
4528
+ try std.testing.expect(media_policy_probe_core.calls[6] >= 6);
4529
+ }
4530
+
4531
+ const coordination_policy_probe_core = struct {
4532
+ pub const rt = mini_core.rt;
4533
+ pub const Model = mini_core.Model;
4534
+ pub const Msg = mini_core.Msg;
4535
+ pub const initialModel = mini_core.initialModel;
4536
+ pub const bootCommand = mini_core.bootCommand;
4537
+ pub const update = mini_core.update;
4538
+ pub const subscriptions = mini_core.subscriptions;
4539
+ pub const commitModelRoot = mini_core.commitModelRoot;
4540
+ var admit = true;
4541
+ var lookup = true;
4542
+ var bind = true;
4543
+ var retire = true;
4544
+ var calls = [_]usize{0} ** 11;
4545
+ pub fn nativeEffectPolicy(request: []const u8, output: []u8) usize {
4546
+ if (request[0] >= 11) return referenceCoordinationPolicy(request, output);
4547
+ calls[request[0]] += 1;
4548
+ @memset(rt.frameAlloc(u8, 19), 173);
4549
+ if (request[0] < 8) return media_policy_probe_core.nativeEffectPolicy(request, output);
4550
+ const size = referenceCoordinationPolicy(request, output);
4551
+ if (request[0] == 8) {
4552
+ if (request[1] == 0) std.mem.writeInt(u64, output[16..24], 1234, .little) else {
4553
+ output[0] = @intFromBool(admit);
4554
+ output[1] = 69;
4555
+ output[2] = 6;
4556
+ std.mem.writeInt(u64, output[8..16], 17, .little);
4557
+ }
4558
+ } else if (request[0] == 10) {
4559
+ if (request[1] == 1) output[0] = 58;
4560
+ } else {
4561
+ switch (request[1]) {
4562
+ 0 => {
4563
+ output[0] = if (admit) 3 else 255;
4564
+ std.mem.writeInt(u64, output[8..16], if (admit) ts_core_host.pty_key_base + 3 else 0, .little);
4565
+ },
4566
+ 1 => if (!lookup) {
4567
+ output[0] = 255;
4568
+ @memset(output[8..16], 0);
4569
+ },
4570
+ 2 => {
4571
+ std.mem.writeInt(u16, output[4..6], 31, .little);
4572
+ std.mem.writeInt(u16, output[6..8], 9, .little);
4573
+ },
4574
+ 3 => output[3] = @intFromBool(bind),
4575
+ 4 => output[2] = @intFromBool(retire and request[4] == 1),
4576
+ else => unreachable,
4577
+ }
4578
+ }
4579
+ return size;
4580
+ }
4581
+ };
4582
+ const CoordinationProbe = ts_core_host.TsCoreHost(coordination_policy_probe_core);
4583
+ fn freshCoordinationProbe() *CoordinationProbe.Fx {
4584
+ coordination_policy_probe_core.admit = true;
4585
+ coordination_policy_probe_core.lookup = true;
4586
+ coordination_policy_probe_core.bind = true;
4587
+ coordination_policy_probe_core.retire = true;
4588
+ coordination_policy_probe_core.calls = @splat(0);
4589
+ const fx = freshMediaProbe();
4590
+ CoordinationProbe.init(fx);
4591
+ return fx;
4592
+ }
4593
+
4594
+ test "media host consumes playback preparation route flags refusal and borrowed complete events" {
4595
+ const fx = freshCoordinationProbe();
4596
+ defer fx.deinit();
4597
+ CoordinationProbe.dispatch(fx, .play_stream);
4598
+ try std.testing.expectEqual(@as(u64, 1234), fx.pendingAudio().?.expected_bytes);
4599
+ CoordinationProbe.dispatch(fx, .vload);
4600
+ const original = fx.videoSnapshot();
4601
+ try std.testing.expectEqual(@as(u64, 17), original.surface);
4602
+ try std.testing.expect(!original.playing and original.looping and original.muted);
4603
+ const token = fx.videoOwnerToken();
4604
+ coordination_policy_probe_core.admit = false;
4605
+ CoordinationProbe.dispatch(fx, .vload2);
4606
+ CoordinationProbe.drain(fx);
4607
+ try std.testing.expectEqualDeep(original, fx.videoSnapshot());
4608
+ try std.testing.expectEqual(token, fx.videoOwnerToken());
4609
+ try std.testing.expectEqual(@as(i64, 1), CoordinationProbe.model().video2_events);
4610
+ try fx.feedVideoEvent(.position, 123, 456, true, true, 1920, 1080);
4611
+ CoordinationProbe.drain(fx);
4612
+ try std.testing.expectEqual(@as(f64, 123), CoordinationProbe.model().v_pos);
4613
+ try std.testing.expectEqual(@as(f64, 456), CoordinationProbe.model().v_dur);
4614
+ try std.testing.expectEqual(@as(f64, 1920), CoordinationProbe.model().v_w);
4615
+ try std.testing.expectEqual(@as(f64, 1080), CoordinationProbe.model().v_h);
4616
+ try std.testing.expect(CoordinationProbe.model().v_playing and CoordinationProbe.model().v_buffering);
4617
+ try std.testing.expect(coordination_policy_probe_core.calls[8] >= 3);
4618
+ try std.testing.expect(coordination_policy_probe_core.calls[10] >= 1);
4619
+ }
4620
+
4621
+ test "media host consumes PTY slots dimensions lookup bindings complete payload and retirement" {
4622
+ const fx = freshCoordinationProbe();
4623
+ defer fx.deinit();
4624
+ const engine = ts_core_host.pty_key_base + 3;
4625
+ CoordinationProbe.dispatch(fx, .open_pty_default);
4626
+ try std.testing.expectEqual(engine, fx.pendingPtyAt(0).?.key);
4627
+ try std.testing.expectEqualStrings(@import("pty.zig").default_term, fx.pendingPtyAt(0).?.term);
4628
+ coordination_policy_probe_core.lookup = false;
4629
+ CoordinationProbe.dispatch(fx, .write_pty);
4630
+ try std.testing.expectEqualStrings("", fx.ptyWrittenBytes(engine));
4631
+ coordination_policy_probe_core.lookup = true;
4632
+ CoordinationProbe.dispatch(fx, .write_pty);
4633
+ try std.testing.expectEqualStrings("ls\n", fx.ptyWrittenBytes(engine));
4634
+ CoordinationProbe.dispatch(fx, .resize_pty);
4635
+ try std.testing.expectEqual(@as(u16, 31), fx.ptySize(engine).?.cols);
4636
+ try std.testing.expectEqual(@as(u16, 9), fx.ptySize(engine).?.rows);
4637
+ try std.testing.expectEqual(engine, CoordinationProbe.resolvePtyKey("shell"));
4638
+ var bytes = [_]u8{ 255, 0, 27, 91, 109 };
4639
+ try fx.feedPtyOutput(engine, &bytes);
4640
+ CoordinationProbe.drain(fx);
4641
+ @memset(&bytes, 0);
4642
+ try std.testing.expectEqualSlices(u8, &.{ 255, 0, 27, 91, 109 }, CoordinationProbe.model().pty_bytes);
4643
+ try std.testing.expectEqualStrings("shell", CoordinationProbe.model().pty_key);
4644
+ coordination_policy_probe_core.retire = false;
4645
+ try fx.feedPtyExit(engine, 7, 0, .exited, 13);
4646
+ CoordinationProbe.drain(fx);
4647
+ try std.testing.expectEqual(@as(i64, 7), CoordinationProbe.model().pty_code);
4648
+ try std.testing.expectEqual(@as(f64, 13), CoordinationProbe.model().pty_dropped);
4649
+ try std.testing.expectEqual(engine, CoordinationProbe.resolvePtyKey("shell"));
4650
+ // A live retained entry blocks another authored spawn; forced refusal is staged.
4651
+ coordination_policy_probe_core.admit = false;
4652
+ CoordinationProbe.dispatch(fx, .open_pty_default);
4653
+ CoordinationProbe.drain(fx);
4654
+ try std.testing.expectEqualStrings("shell", CoordinationProbe.model().pty_key);
4655
+ try std.testing.expectEqual(mini_core.PtyReason.rejected, CoordinationProbe.model().pty_reason);
4656
+ try std.testing.expect(coordination_policy_probe_core.calls[9] >= 8);
4657
+ }
4658
+
4659
+ test "media host consumes PTY retirement while named bindings retain ended grids" {
4660
+ for ([_]bool{ false, true }) |bound| {
4661
+ const fx = freshCoordinationProbe();
4662
+ defer fx.deinit();
4663
+ const engine = ts_core_host.pty_key_base + 3;
4664
+ CoordinationProbe.dispatch(fx, .open_pty_default);
4665
+ coordination_policy_probe_core.bind = bound;
4666
+ try std.testing.expectEqual(engine, CoordinationProbe.resolvePtyKey("shell"));
4667
+ try fx.feedPtyExit(engine, 0, 0, .exited, 0);
4668
+ CoordinationProbe.drain(fx);
4669
+ try std.testing.expectEqual(if (bound) engine else @as(u64, 0), CoordinationProbe.resolvePtyKey("shell"));
4670
+ CoordinationProbe.dispatch(fx, .write_pty);
4671
+ try std.testing.expectEqualStrings("", fx.ptyWrittenBytes(engine));
4672
+ CoordinationProbe.dispatch(fx, .open_pty_default);
4673
+ try std.testing.expectEqual(engine, fx.pendingPtyAt(0).?.key);
4674
+ CoordinationProbe.dispatch(fx, .kill_pty);
4675
+ try std.testing.expect(fx.ptyKillRequested(engine));
4676
+ }
4677
+ }
4678
+
4679
+ const request_policy_probe_core = struct {
4680
+ pub const rt = mini_core.rt;
4681
+ pub const Model = struct { bytes: []const u8 = "", error_bytes: []const u8 = "", done: usize = 0 };
4682
+ pub const Msg = union(enum) { commands: []const u8, loaded: []const u8, failed: []const u8, done };
4683
+ pub const UpdateResult = struct { model: *const Model, cmd: []const u8 };
4684
+ var model: Model = .{};
4685
+ var owned: [2][1024]u8 = undefined;
4686
+ var admit = true;
4687
+ var route_error = false;
4688
+ var retire = true;
4689
+ var cancel = true;
4690
+ var credential_suffix = false;
4691
+ var calls: [17]usize = @splat(0);
4692
+ pub fn initialModel() *const Model {
4693
+ model = .{};
4694
+ return &model;
4695
+ }
4696
+ pub fn commitModelRoot(value: *const Model) *const Model {
4697
+ return value;
4698
+ }
4699
+ pub fn update(value: *const Model, msg: Msg) UpdateResult {
4700
+ switch (msg) {
4701
+ .commands => |wire| return .{ .model = value, .cmd = wire },
4702
+ .loaded => |bytes| {
4703
+ @memcpy(owned[0][0..bytes.len], bytes);
4704
+ model.bytes = owned[0][0..bytes.len];
4705
+ },
4706
+ .failed => |bytes| {
4707
+ @memcpy(owned[1][0..bytes.len], bytes);
4708
+ model.error_bytes = owned[1][0..bytes.len];
4709
+ },
4710
+ .done => model.done += 1,
4711
+ }
4712
+ return .{ .model = &model, .cmd = "" };
4713
+ }
4714
+ pub fn nativeEffectPolicy(request: []const u8, output: []u8) usize {
4715
+ if (request[0] < 8) return media_policy_probe_core.nativeEffectPolicy(request, output);
4716
+ calls[request[0]] += 1;
4717
+ @memset(rt.frameAlloc(u8, 23), 173);
4718
+ const size = referenceCoordinationPolicy(request, output);
4719
+ if (request[0] == 11 and request[1] == 0) {
4720
+ if (!admit) output[0] = 255 else if (request[2] == 0 and output[0] == 0) output[0] = 15;
4721
+ } else if (request[0] == 11 and request[1] == 2) {
4722
+ if (route_error) output[2] = 2;
4723
+ output[4] = @intFromBool(retire);
4724
+ } else if (request[0] == 12 and !cancel) {
4725
+ output[0] = 255;
4726
+ output[1] = 255;
4727
+ } else if (request[0] == 13 and credential_suffix and output[0] == 1) {
4728
+ const offset = std.mem.readInt(u32, output[4..8], .little);
4729
+ const length = std.mem.readInt(u32, output[8..12], .little);
4730
+ std.mem.writeInt(u32, output[4..8], offset + 1, .little);
4731
+ std.mem.writeInt(u32, output[8..12], length - 1, .little);
4732
+ } else if (request[0] == 14) {
4733
+ if (request[1] == 0 and output[0] == 0) output[0] = 15;
4734
+ if (request[1] == 1) {
4735
+ if (route_error) output[4] = 2;
4736
+ output[6] = @intFromBool(retire and output[6] == 1);
4737
+ }
4738
+ }
4739
+ return size;
4740
+ }
4741
+ };
4742
+ const RequestProbe = ts_core_host.TsCoreHost(request_policy_probe_core);
4743
+ fn freshRequestProbe(fx: *RequestProbe.Fx) void {
4744
+ request_policy_probe_core.admit = true;
4745
+ request_policy_probe_core.route_error = false;
4746
+ request_policy_probe_core.retire = true;
4747
+ request_policy_probe_core.cancel = true;
4748
+ request_policy_probe_core.credential_suffix = false;
4749
+ request_policy_probe_core.calls = @splat(0);
4750
+ fx.* = RequestProbe.Fx.init(std.testing.allocator);
4751
+ fx.executor = .fake;
4752
+ RequestProbe.init(fx);
4753
+ }
4754
+
4755
+ test "request host consumes compiled admission routes retirement cancellation and retains all result bytes" {
4756
+ var fx: RequestProbe.Fx = undefined;
4757
+ freshRequestProbe(&fx);
4758
+ defer fx.deinit();
4759
+ RequestProbe.dispatch(&fx, .{ .commands = mini_core.cmdRequest("echo", "owned", 1, 2, "body\x00\xff") });
4760
+ const first = fx.pendingHostAt(0).?;
4761
+ try std.testing.expectEqual(ts_core_host.request_key_base + 15, first.key);
4762
+ try std.testing.expectEqualStrings("body\x00\xff", first.payload);
4763
+ request_policy_probe_core.cancel = false;
4764
+ RequestProbe.dispatch(&fx, .{ .commands = mini_core.cmdCancel("owned") });
4765
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingHostCount());
4766
+ request_policy_probe_core.route_error = true;
4767
+ request_policy_probe_core.retire = false;
4768
+ try fx.feedHostResult(first.key, true, "answer\x00\xff");
4769
+ RequestProbe.drain(&fx);
4770
+ try std.testing.expectEqualStrings("answer\x00\xff", RequestProbe.model().error_bytes);
4771
+ mini_core.rt.frameReset();
4772
+ @memset(mini_core.rt.frameAlloc(u8, 2048), 165);
4773
+ try std.testing.expectEqualStrings("answer\x00\xff", RequestProbe.model().error_bytes);
4774
+ // Keeping the route affects duplicate rejection after the engine terminal.
4775
+ const duplicate = mini_core.cmdRequest("echo", "owned", 1, 2, "next");
4776
+ @constCast(duplicate)[2 + 4 + 1 + 5 + 2] = 1;
4777
+ RequestProbe.dispatch(&fx, .{ .commands = duplicate });
4778
+ RequestProbe.drain(&fx);
4779
+ try std.testing.expectEqualStrings("rejected", RequestProbe.model().error_bytes);
4780
+ request_policy_probe_core.cancel = true;
4781
+ RequestProbe.dispatch(&fx, .{ .commands = mini_core.cmdCancel("owned") });
4782
+ RequestProbe.dispatch(&fx, .{ .commands = mini_core.cmdRequest("echo", "owned", 1, 2, "new") });
4783
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingHostCount());
4784
+ request_policy_probe_core.admit = false;
4785
+ RequestProbe.dispatch(&fx, .{ .commands = mini_core.cmdRequest("echo", "other", 1, 2, "deny") });
4786
+ RequestProbe.drain(&fx);
4787
+ try std.testing.expectEqualStrings("rejected", RequestProbe.model().error_bytes);
4788
+ try std.testing.expect(request_policy_probe_core.calls[11] >= 5);
4789
+ try std.testing.expect(request_policy_probe_core.calls[12] >= 2);
4790
+ }
4791
+
4792
+ test "request host consumes compiled credential offsets without retaining borrowed packets" {
4793
+ var fx: RequestProbe.Fx = undefined;
4794
+ freshRequestProbe(&fx);
4795
+ defer fx.deinit();
4796
+ var backing = platform.NullPlatform.init(.{});
4797
+ defer backing.deinit();
4798
+ var services = backing.platform();
4799
+ fx.bindCredentialsStore(.{ .services = &services.services, .service = "dev.native-sdk.request-test", .permitted = true });
4800
+ request_policy_probe_core.credential_suffix = true;
4801
+ const payload = [_]u8{ 4, 0, 0, 0, 'x', 'a', 'b', 'k', 3, 0, 0, 0, 's', 0, 255 };
4802
+ RequestProbe.dispatch(&fx, .{ .commands = mini_core.cmdRequest("core.credentials.set", "credential", 3, 2, &payload) });
4803
+ const pending = fx.pendingHostAt(0).?;
4804
+ try std.testing.expectEqualSlices(u8, &.{ 3, 0, 0, 0, 'a', 'b', 'k', 3, 0, 0, 0, 's', 0, 255 }, pending.payload);
4805
+ mini_core.rt.frameReset();
4806
+ @memset(mini_core.rt.frameAlloc(u8, 2048), 165);
4807
+ try std.testing.expectEqualSlices(u8, &.{ 3, 0, 0, 0, 'a', 'b', 'k', 3, 0, 0, 0, 's', 0, 255 }, pending.payload);
4808
+ try fx.feedHostResult(pending.key, true, "");
4809
+ RequestProbe.drain(&fx);
4810
+ try std.testing.expectEqual(@as(usize, 1), RequestProbe.model().done);
4811
+ RequestProbe.dispatch(&fx, .{ .commands = mini_core.cmdRequest("core.credentials.set", "credential", 3, 2, payload[0..8]) });
4812
+ RequestProbe.drain(&fx);
4813
+ try std.testing.expectEqualStrings("rejected", RequestProbe.model().error_bytes);
4814
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingHostCount());
4815
+ try std.testing.expect(request_policy_probe_core.calls[13] == 2);
4816
+ }
4817
+
4818
+ test "request host consumes compiled database admission complete routing and retirement" {
4819
+ var fx: RequestProbe.Fx = undefined;
4820
+ freshRequestProbe(&fx);
4821
+ defer fx.deinit();
4822
+ RequestProbe.dispatch(&fx, .{ .commands = mini_core.cmdDbQuery("query", 1, 3, 2, "SELECT 1") });
4823
+ const pending = fx.pendingDbAt(0).?;
4824
+ try std.testing.expectEqual(ts_core_host.db_key_base + 15, pending.key);
4825
+ request_policy_probe_core.route_error = true;
4826
+ const page = "\x01\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00blob\x04\x06\x00\x00\x00rows\x00\xff";
4827
+ try fx.feedDbResult(pending.key, .page, .ok, page);
4828
+ RequestProbe.drain(&fx);
4829
+ try std.testing.expectEqualStrings(page, RequestProbe.model().error_bytes);
4830
+ request_policy_probe_core.route_error = false;
4831
+ request_policy_probe_core.retire = false;
4832
+ try fx.feedDbResult(pending.key, .done, .ok, "");
4833
+ RequestProbe.drain(&fx);
4834
+ try std.testing.expectEqual(@as(usize, 1), RequestProbe.model().done);
4835
+ // The retained query route still owns the key and refuses an exec collision.
4836
+ const exec = mini_core.rt.frameAlloc(u8, 4 + 5 + 4);
4837
+ exec[0..2].* = .{ 0x2A, 5 };
4838
+ @memcpy(exec[2..7], "query");
4839
+ exec[7..9].* = .{ 3, 2 };
4840
+ @memset(exec[9..], 0);
4841
+ RequestProbe.dispatch(&fx, .{ .commands = exec });
4842
+ RequestProbe.drain(&fx);
4843
+ try std.testing.expectEqualStrings("rejected", RequestProbe.model().error_bytes);
4844
+ RequestProbe.dispatch(&fx, .{ .commands = mini_core.cmdCancel("query") });
4845
+ RequestProbe.dispatch(&fx, .{ .commands = mini_core.cmdDbQuery("query", 1, 3, 2, "SELECT 2") });
4846
+ try std.testing.expectEqual(@as(usize, 1), fx.pendingDbCount());
4847
+ try std.testing.expect(request_policy_probe_core.calls[14] >= 5);
4848
+ }
4849
+
4850
+ const file_policy_probe_core = struct {
4851
+ pub const rt = mini_core.rt;
4852
+ pub const Model = mini_core.Model;
4853
+ pub const Msg = mini_core.Msg;
4854
+ pub const UpdateResult = mini_core.UpdateResult;
4855
+ pub const initialModel = mini_core.initialModel;
4856
+ pub const commitModelRoot = mini_core.commitModelRoot;
4857
+ pub fn update(model: *const Model, msg: Msg) UpdateResult {
4858
+ if (msg == .note) {
4859
+ const key = "read";
4860
+ const out = rt.frameAlloc(u8, 5 + key.len + 4 + 9);
4861
+ out[0..2].* = .{ 0x2D, key.len };
4862
+ @memcpy(out[2..][0..key.len], key);
4863
+ out[2 + key.len ..][0..3].* = .{ 25, 9, 8 };
4864
+ _ = mini_core.writeLongBytes(out, 5 + key.len, "notes.bin");
4865
+ return .{ .model = model, .cmd = out };
4866
+ }
4867
+ return mini_core.update(model, msg);
4868
+ }
4869
+ pub const bootCommand = mini_core.bootCommand;
4870
+ pub const subscriptions = mini_core.subscriptions;
4871
+ var calls: [6]usize = @splat(0);
4872
+ var retain: bool = false;
4873
+ var clear_busy: bool = true;
4874
+ pub fn nativeEffectPolicy(request: []const u8, output: []u8) usize {
4875
+ if (request[0] < 8) return media_policy_probe_core.nativeEffectPolicy(request, output);
4876
+ const size = referenceCoordinationPolicy(request, output);
4877
+ if (request[0] != 15) return size;
4878
+ calls[request[1]] += 1;
4879
+ if ((request[1] == 1 or request[1] == 2) and output[0] != 255) output[0] = 3;
4880
+ if (request[1] == 5) {
4881
+ if (retain) output[4] = 0;
4882
+ if (!clear_busy) output[5] = 0;
4883
+ if (output[3] == 0) output[2] = 19; // alternate void arm
4884
+ if (output[3] == 1 or output[3] == 3) output[2] = 7; // alternate bytes arm
4885
+ if (output[3] == 2) output[2] = 10; // alternate number arm
4886
+ }
4887
+ return size;
4888
+ }
4889
+ };
4890
+
4891
+ test "file host consumes redirected slots routes busy mutations and retained terminal ownership" {
4892
+ const Probe = ts_core_host.TsCoreHost(file_policy_probe_core);
4893
+ const fx = freshChannel();
4894
+ defer fx.deinit();
4895
+ file_policy_probe_core.calls = @splat(0);
4896
+ file_policy_probe_core.retain = false;
4897
+ file_policy_probe_core.clear_busy = false;
4898
+ Probe.init(fx);
4899
+ Probe.dispatch(fx, .open_save_sink);
4900
+ const key = ts_core_host.file_stream_key_base + 3;
4901
+ try fx.acknowledgeFakeFileStreamOpen(key);
4902
+ try fx.feedFileResultDetailed(.{ .key = key, .op = .write_stream_open, .outcome = .ok });
4903
+ Probe.drain(fx);
4904
+ try std.testing.expect(!Probe.model().saved); // .wrote was redirected
4905
+ Probe.dispatch(fx, .write_save_chunk);
4906
+ Probe.drain(fx);
4907
+ try std.testing.expectEqualStrings("out_of_order", Probe.model().last_err);
4908
+ file_policy_probe_core.clear_busy = true;
4909
+ try fx.feedFileResultDetailed(.{ .key = key, .op = .write_stream_open, .outcome = .ok });
4910
+ Probe.drain(fx);
4911
+ Probe.dispatch(fx, .write_save_chunk);
4912
+ try fx.feedFileResultDetailed(.{ .key = key, .op = .write_stream_chunk, .outcome = .rejected });
4913
+ Probe.drain(fx);
4914
+ try std.testing.expectEqualStrings("rejected", Probe.model().status); // bytes redirected to loaded
4915
+ file_policy_probe_core.retain = true;
4916
+ Probe.dispatch(fx, .close_save_sink);
4917
+ try fx.feedFileResultDetailed(.{ .key = key, .op = .write_stream_close, .outcome = .ok });
4918
+ Probe.drain(fx);
4919
+ Probe.dispatch(fx, .open_save_sink);
4920
+ Probe.drain(fx);
4921
+ try std.testing.expectEqualStrings("rejected", Probe.model().last_err);
4922
+ try std.testing.expect(file_policy_probe_core.calls[2] == 2 and file_policy_probe_core.calls[3] == 2 and file_policy_probe_core.calls[4] == 1 and file_policy_probe_core.calls[5] == 4);
4923
+ }
4924
+
4925
+ const clipboard_policy_probe_core = struct {
4926
+ pub const rt = complete_core.rt;
4927
+ pub const Model = complete_core.Model;
4928
+ pub const Msg = union(enum) { commands: []const u8, line: complete_core.Line, exit: complete_core.Exit, clipboard: complete_core.Clipboard, alternate: complete_core.Clipboard };
4929
+ pub const UpdateResult = complete_core.UpdateResult;
4930
+ pub const initialModel = complete_core.initialModel;
4931
+ pub const commitModelRoot = complete_core.commitModelRoot;
4932
+ var alternate_route: bool = false;
4933
+ pub fn update(model: *const Model, msg: Msg) UpdateResult {
4934
+ return switch (msg) {
4935
+ .commands => |wire| complete_core.update(model, .{ .commands = wire }),
4936
+ .line => |value| complete_core.update(model, .{ .line = value }),
4937
+ .exit => |value| complete_core.update(model, .{ .exit = value }),
4938
+ .clipboard => |value| complete_core.update(model, .{ .clipboard = value }),
4939
+ .alternate => |value| blk: {
4940
+ alternate_route = true;
4941
+ break :blk complete_core.update(model, .{ .clipboard = value });
4942
+ },
4943
+ };
4944
+ }
4945
+ var retain: bool = false;
4946
+ var calls: [3]usize = @splat(0);
4947
+ pub fn nativeEffectPolicy(request: []const u8, output: []u8) usize {
4948
+ if (request[0] < 8) return media_policy_probe_core.nativeEffectPolicy(request, output);
4949
+ if (request[0] != 16) return referenceCoordinationPolicy(request, output);
4950
+ const size = @import("file_policy_test_reference.zig").plan(request, output);
4951
+ calls[request[1]] += 1;
4952
+ if (request[1] == 1 and output[0] != 255) output[0] = 15;
4953
+ if (request[1] == 2) {
4954
+ output[1] = 4;
4955
+ output[2] = @intFromBool(!retain);
4956
+ }
4957
+ return size;
4958
+ }
4959
+ };
4960
+
4961
+ var clipboard_probe_channel: ts_core_host.TsCoreHost(clipboard_policy_probe_core).Fx = undefined;
4962
+
4963
+ test "file host consumes clipboard admission and complete borrowed records before retirement" {
4964
+ const Probe = ts_core_host.TsCoreHost(clipboard_policy_probe_core);
4965
+ clipboard_probe_channel = Probe.Fx.init(std.testing.allocator);
4966
+ clipboard_probe_channel.executor = .fake;
4967
+ const fx = &clipboard_probe_channel;
4968
+ defer fx.deinit();
4969
+ clipboard_policy_probe_core.calls = @splat(0);
4970
+ clipboard_policy_probe_core.retain = true;
4971
+ clipboard_policy_probe_core.alternate_route = false;
4972
+ Probe.init(fx);
4973
+ const name = "copy\x00\xff";
4974
+ Probe.dispatch(fx, .{ .commands = complete_core.clipboard(name) });
4975
+ const key: u64 = 0x5453_4352_0000_0000 + 15;
4976
+ try std.testing.expectEqual(key, fx.pendingClipboardAt(0).?.key);
4977
+ try fx.feedClipboardResult(key, .ok, "ignored");
4978
+ Probe.drain(fx);
4979
+ try std.testing.expect(clipboard_policy_probe_core.alternate_route);
4980
+ try std.testing.expectEqualDeep(complete_core.Clipboard{ .key = name, .operation = .write, .outcome = .ok, .text = "", .droppedBefore = 0 }, Probe.model().clipboard.?);
4981
+ Probe.dispatch(fx, .{ .commands = complete_core.clipboard(name) });
4982
+ Probe.drain(fx);
4983
+ try std.testing.expectEqual(.rejected, Probe.model().clipboard.?.outcome);
4984
+ clipboard_policy_probe_core.retain = false;
4985
+ // Reset the deliberately retained probe owner after the OS op has ended.
4986
+ Probe.init(fx);
4987
+ Probe.dispatch(fx, .{ .commands = complete_core.clipboard(name) });
4988
+ try fx.feedClipboardResult(key, .ok, "ignored");
4989
+ Probe.drain(fx);
4990
+ Probe.dispatch(fx, .{ .commands = complete_core.clipboard(name) });
4991
+ try std.testing.expectEqual(key, fx.pendingClipboardAt(0).?.key);
4992
+ try std.testing.expect(clipboard_policy_probe_core.calls[0] > 0 and clipboard_policy_probe_core.calls[1] == 4 and clipboard_policy_probe_core.calls[2] == 2);
4993
+ }
4994
+
4995
+ test "file host consumes read replacement complete bytes and numeric terminal routes" {
4996
+ const Probe = ts_core_host.TsCoreHost(file_policy_probe_core);
4997
+ const fx = freshChannel();
4998
+ defer fx.deinit();
4999
+ file_policy_probe_core.calls = @splat(0);
5000
+ file_policy_probe_core.retain = false;
5001
+ file_policy_probe_core.clear_busy = true;
5002
+ Probe.init(fx);
5003
+ Probe.dispatch(fx, .note);
5004
+ Probe.dispatch(fx, .note); // Replaces the same read generation in slot three.
5005
+ const key = ts_core_host.file_stream_key_base + 3;
5006
+ try fx.feedFileResultDetailed(.{ .key = key, .op = .read_stream, .event = .chunk, .outcome = .ok, .bytes = "raw\x00\xff" });
5007
+ Probe.drain(fx);
5008
+ try std.testing.expectEqualStrings("raw\x00\xff", Probe.model().status);
5009
+ try std.testing.expectEqual(@as(i64, 0), Probe.model().line_count);
5010
+ try fx.feedFileResultDetailed(.{ .key = key, .op = .read_stream, .event = .done, .outcome = .ok, .total = 4294967302 });
5011
+ Probe.drain(fx);
5012
+ try std.testing.expectEqual(@as(f64, 4294967302), Probe.model().stamp_ms);
5013
+ try std.testing.expectEqual(@as(i64, 0), Probe.model().ticks);
5014
+ Probe.dispatch(fx, .note);
5015
+ try fx.feedFileResultDetailed(.{ .key = key, .op = .read_stream, .outcome = .io_failed });
5016
+ Probe.drain(fx);
5017
+ try std.testing.expectEqualStrings("io_failed", Probe.model().status);
5018
+ try std.testing.expectEqual(@as(usize, 3), file_policy_probe_core.calls[1]);
5019
+ try std.testing.expectEqual(@as(usize, 3), file_policy_probe_core.calls[5]);
5020
+ }
5021
+
5022
+ test "file host consumes fresh bridge ownership after effects teardown and reinitialization" {
5023
+ const Probe = ts_core_host.TsCoreHost(file_policy_probe_core);
5024
+ file_policy_probe_core.retain = false;
5025
+ file_policy_probe_core.clear_busy = true;
5026
+ for (0..2) |_| {
5027
+ const fx = freshChannel();
5028
+ Probe.init(fx);
5029
+ Probe.dispatch(fx, .open_save_sink);
5030
+ try fx.acknowledgeFakeFileStreamOpen(ts_core_host.file_stream_key_base + 3);
5031
+ fx.deinit(); // The next host installation must own an empty bridge table.
5032
+ }
5033
+ for (0..2) |_| {
5034
+ const fx = freshChannel();
5035
+ Host.init(fx);
5036
+ Host.dispatch(fx, .open_save_sink);
5037
+ try fx.acknowledgeFakeFileStreamOpen(ts_core_host.file_stream_key_base);
5038
+ fx.deinit();
5039
+ }
5040
+ }
5041
+
5042
+ const callback_complete_probe = struct {
5043
+ pub const rt = complete_storage_core.rt;
5044
+ pub const Model = complete_storage_core.Model;
5045
+ pub const Msg = complete_storage_core.Msg;
5046
+ pub const UpdateResult = complete_storage_core.UpdateResult;
5047
+ pub const initialModel = complete_storage_core.initialModel;
5048
+ pub const commitModelRoot = complete_storage_core.commitModelRoot;
5049
+ pub const update = complete_storage_core.update;
5050
+ var retain: bool = false;
5051
+ var complete_calls: usize = 0;
5052
+ var completion_saw_live: bool = false;
5053
+ var suppress_file: bool = false;
5054
+ pub fn nativeEffectPolicy(request: []const u8, output: []u8) usize {
5055
+ if (request[0] >= 8 and request[0] != 17) return referenceCoordinationPolicy(request, output);
5056
+ const size = @import("callback_policy_test_reference.zig").plan(request, output);
5057
+ if (request[0] == 17) {
5058
+ output[4] = @intFromBool(!retain);
5059
+ output[5] = @intFromBool(suppress_file);
5060
+ }
5061
+ return size;
5062
+ }
5063
+ pub fn nativeTimerPolicy(request: []const u8, output: []u8) usize {
5064
+ if (request[0] == 7) {
5065
+ complete_calls += 1;
5066
+ const size = @import("callback_policy_test_reference.zig").plan(request, output);
5067
+ completion_saw_live = request[32 + @as(usize, output[0]) * 4] == 1;
5068
+ output[3] = @intFromBool(!retain);
5069
+ const timestamp = "18446744073709551615";
5070
+ output[4] = timestamp.len;
5071
+ @memcpy(output[16..36], timestamp);
5072
+ return size;
5073
+ }
5074
+ if (request[0] == 3) {
5075
+ // The forced owner is visible only while the copied retirement
5076
+ // decision has left it live, regardless of the engine timer.
5077
+ var at: usize = 2 + request[1];
5078
+ output[0] = 255;
5079
+ for (0..16) |slot| {
5080
+ if (slot == 4 and request[at] == 1) output[0] = 4;
5081
+ at += 3 + request[at + 1];
5082
+ }
5083
+ return 1;
5084
+ }
5085
+ std.debug.assert(request[0] == 6);
5086
+ @memset(output[0..10], 0);
5087
+ output[0] = 4;
5088
+ output[1] = @intFromEnum(std.meta.Tag(Msg).timer);
5089
+ std.mem.writeInt(u64, output[2..10], @bitCast(@as(f64, 1)), .little);
5090
+ return 10;
5091
+ }
5092
+ };
5093
+
5094
+ test "callback host consumes complete timer decimal bytes retirement and retained owner before dispatch reset" {
5095
+ const Probe = ts_core_host.TsCoreHost(callback_complete_probe);
5096
+ var timer_channel = Probe.Fx.init(std.testing.allocator);
5097
+ timer_channel.executor = .fake;
5098
+ defer timer_channel.deinit();
5099
+ callback_complete_probe.retain = true;
5100
+ callback_complete_probe.complete_calls = 0;
5101
+ callback_complete_probe.completion_saw_live = false;
5102
+ Probe.init(&timer_channel);
5103
+ const name = "clock\x00\xff";
5104
+ Probe.dispatch(&timer_channel, .{ .commands = complete_storage_core.timerCommand(name, true, 1) });
5105
+ const key = ts_core_host.delay_key_base + 4;
5106
+ try timer_channel.fireTimer(key);
5107
+ Probe.drain(&timer_channel);
5108
+ try std.testing.expect(callback_complete_probe.completion_saw_live);
5109
+ try std.testing.expectEqualDeep(complete_storage_core.Timer{ .key = name, .timestampNs = "18446744073709551615", .outcome = .fired }, Probe.model().timer.?);
5110
+ // The policy keeps the repeating bridge entry alive for another fire.
5111
+ callback_complete_probe.retain = false;
5112
+ try timer_channel.fireTimer(key);
5113
+ Probe.drain(&timer_channel);
5114
+ try std.testing.expectEqual(@as(usize, 2), callback_complete_probe.complete_calls);
5115
+ try std.testing.expectEqualStrings("18446744073709551615", Probe.model().timer.?.timestampNs);
5116
+ // The engine still owns a repeating timer; the copied plan retired only
5117
+ // the bridge. Cancel cannot rediscover that retired owner.
5118
+ Probe.dispatch(&timer_channel, .{ .commands = mini_core.cmdCancel(name) });
5119
+ try std.testing.expectEqual(@as(usize, 1), timer_channel.pendingTimerCount());
5120
+ }
5121
+
5122
+ test "callback host consumes full file retirement and silent delivery while retaining complete owned metadata" {
5123
+ const Probe = ts_core_host.TsCoreHost(callback_complete_probe);
5124
+ var file_channel = Probe.Fx.init(std.testing.allocator);
5125
+ file_channel.executor = .fake;
5126
+ defer file_channel.deinit();
5127
+ callback_complete_probe.retain = true;
5128
+ callback_complete_probe.suppress_file = false;
5129
+ Probe.init(&file_channel);
5130
+ const name = "file\x00\xff";
5131
+ Probe.dispatch(&file_channel, .{ .commands = complete_storage_core.fileCommand(name, false) });
5132
+ const first = file_channel.pendingFileAt(0).?.key;
5133
+ try file_channel.feedFileResultDetailed(.{ .key = first, .op = .read, .outcome = .ok, .bytes = "body\x00\xff", .total = std.math.maxInt(u64), .mtime_ms = -12345, .exists = true });
5134
+ Probe.drain(&file_channel);
5135
+ try std.testing.expectEqualDeep(complete_storage_core.File{ .key = name, .operation = .read, .event = .terminal, .outcome = .ok, .bytes = "body\x00\xff", .totalBytes = "18446744073709551615", .mtimeMs = "-12345", .exists = true, .droppedBefore = 0 }, Probe.model().file.?);
5136
+ Probe.dispatch(&file_channel, .{ .commands = complete_storage_core.fileCommand(name, false) });
5137
+ try std.testing.expectEqual(first + 1, file_channel.pendingFileAt(0).?.key);
5138
+ callback_complete_probe.retain = false;
5139
+ callback_complete_probe.suppress_file = true;
5140
+ const replies = Probe.model().replies;
5141
+ try file_channel.feedFileResultDetailed(.{ .key = first + 1, .op = .read, .outcome = .disk_full });
5142
+ Probe.drain(&file_channel);
5143
+ try std.testing.expectEqual(replies, Probe.model().replies);
5144
+ try std.testing.expectEqualStrings("body\x00\xff", Probe.model().file.?.bytes);
5145
+ try std.testing.expectEqualStrings("18446744073709551615", Probe.model().file.?.totalBytes);
5146
+ callback_complete_probe.suppress_file = false;
5147
+ }
5148
+
5149
+ const word_image_core = struct {
5150
+ pub const rt = mini_core.rt;
5151
+ const Image = struct { imageLower: f64, imageUpper: f64, state: enum { loaded, rejected, cancelled, decode_failed }, width: i64, height: i64, status: i64 };
5152
+ pub const Model = struct { image: ?Image = null, image_replies: usize = 0, file_replies: usize = 0, file_errors: usize = 0 };
5153
+ pub const Msg = union(enum) { commands: []const u8, image: Image, file: []const u8, failed: []const u8 };
5154
+ pub const UpdateResult = struct { model: *const Model, cmd: []const u8 };
5155
+ var stored: Model = .{};
5156
+ pub fn initialModel() *const Model {
5157
+ stored = .{};
5158
+ return &stored;
5159
+ }
5160
+ pub fn commitModelRoot(value: *const Model) *const Model { return value; }
5161
+ pub fn update(value: *const Model, msg: Msg) UpdateResult {
5162
+ switch (msg) {
5163
+ .commands => |wire| return .{ .model = value, .cmd = wire },
5164
+ .image => |image| { stored.image = image; stored.image_replies += 1; },
5165
+ .file => stored.file_replies += 1,
5166
+ .failed => stored.file_errors += 1,
5167
+ }
5168
+ return .{ .model = &stored, .cmd = "" };
5169
+ }
5170
+ fn imageCommand(buffer: []u8, op: u8, id: u64) []const u8 {
5171
+ buffer[0] = op;
5172
+ std.mem.writeInt(u64, buffer[1..9], id, .little);
5173
+ if (op != 0x3b) return buffer[0..9];
5174
+ buffer[9] = 1;
5175
+ std.mem.writeInt(u32, buffer[10..14], 5, .little);
5176
+ @memcpy(buffer[14..19], "a.png");
5177
+ @memset(buffer[19..35], 0); // empty URL/cache, unspecified expected bytes
5178
+ return buffer[0..35];
5179
+ }
5180
+ };
5181
+
5182
+ test "word image ABI preserves exact bounds and complete terminals after the command bytes expire" {
5183
+ const WordHost = ts_core_host.TsCoreHost(word_image_core);
5184
+ var fx = WordHost.Fx.init(std.testing.allocator);
5185
+ defer fx.deinit();
5186
+ fx.executor = .fake;
5187
+ WordHost.init(&fx);
5188
+ var command: [64]u8 = undefined;
5189
+ for ([_]u64{ 1, 9007199254740993, 0x7fff_ffff_ffff_ffff }) |id| {
5190
+ WordHost.dispatch(&fx, .{ .commands = word_image_core.imageCommand(&command, 0x3b, id) });
5191
+ @memset(&command, 255);
5192
+ try std.testing.expectEqual(id, fx.pendingImageLoadAt(0).?.id);
5193
+ try std.testing.expectEqualStrings("a.png", fx.pendingImageLoadAt(0).?.path);
5194
+ try fx.feedImageResult(id, .decode_failed, 19, 23, 503, "");
5195
+ WordHost.drain(&fx);
5196
+ const result = WordHost.model().image.?;
5197
+ try std.testing.expectEqual(@as(f64, @floatFromInt(@as(u32, @truncate(id)))), result.imageLower);
5198
+ try std.testing.expectEqual(@as(f64, @floatFromInt(@as(u32, @truncate(id >> 32)))), result.imageUpper);
5199
+ try std.testing.expectEqual(@as(i64, 19), result.width);
5200
+ try std.testing.expectEqual(@as(i64, 23), result.height);
5201
+ try std.testing.expectEqual(@as(i64, 503), result.status);
5202
+ try std.testing.expectEqual(.decode_failed, result.state);
5203
+ }
5204
+ for ([_]u64{ 0, 0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff }) |id| {
5205
+ WordHost.dispatch(&fx, .{ .commands = word_image_core.imageCommand(&command, 0x3b, id) });
5206
+ WordHost.drain(&fx);
5207
+ try std.testing.expectEqual(@as(usize, 0), fx.pendingImageLoadCount());
5208
+ try std.testing.expectEqual(.rejected, WordHost.model().image.?.state);
5209
+ try std.testing.expectEqual(@as(f64, @floatFromInt(@as(u32, @truncate(id >> 32)))), WordHost.model().image.?.imageUpper);
5210
+ }
5211
+ }
5212
+
5213
+ test "image and named cancellation preserve the other family's incumbent and queued terminal" {
5214
+ const WordHost = ts_core_host.TsCoreHost(word_image_core);
5215
+ const key = ts_core_host.effect_key_base;
5216
+ const read = [_]u8{ 0x07, 1, 'k', 2, 3, 1, 0, 0, 0, 'p' };
5217
+ const cancel = [_]u8{ 0x06, 1, 'k' };
5218
+ var command: [64]u8 = undefined;
5219
+ var batch: [96]u8 = undefined;
5220
+ for ([_]bool{ false, true }) |queued| {
5221
+ var fx = WordHost.Fx.init(std.testing.allocator);
5222
+ defer fx.deinit();
5223
+ fx.executor = .fake;
5224
+ WordHost.init(&fx);
5225
+ WordHost.dispatch(&fx, .{ .commands = &read });
5226
+ if (queued) try fx.feedFileResult(key, .ok, "owned");
5227
+ const load = word_image_core.imageCommand(&command, 0x3b, key);
5228
+ @memcpy(batch[0..load.len], load);
5229
+ const stop = word_image_core.imageCommand(&command, 0x3c, key);
5230
+ @memcpy(batch[load.len..][0..stop.len], stop);
5231
+ WordHost.dispatch(&fx, .{ .commands = batch[0 .. load.len + stop.len] });
5232
+ WordHost.drain(&fx);
5233
+ try std.testing.expectEqual(.rejected, WordHost.model().image.?.state);
5234
+ if (!queued) try fx.feedFileResult(key, .ok, "owned");
5235
+ WordHost.drain(&fx);
5236
+ try std.testing.expectEqual(@as(usize, 1), WordHost.model().file_replies);
5237
+ }
5238
+ for ([_]bool{ false, true }) |queued| {
5239
+ var fx = WordHost.Fx.init(std.testing.allocator);
5240
+ defer fx.deinit();
5241
+ fx.executor = .fake;
5242
+ WordHost.init(&fx);
5243
+ WordHost.dispatch(&fx, .{ .commands = word_image_core.imageCommand(&command, 0x3b, key) });
5244
+ if (queued) try fx.feedImageResult(key, .decode_failed, 0, 0, 0, "");
5245
+ @memcpy(batch[0..read.len], &read);
5246
+ @memcpy(batch[read.len..][0..cancel.len], &cancel);
5247
+ WordHost.dispatch(&fx, .{ .commands = batch[0 .. read.len + cancel.len] });
5248
+ WordHost.drain(&fx);
5249
+ // Named cancellation silently drops its own refused file terminal,
5250
+ // while the image incumbent still owns its pending completion.
5251
+ try std.testing.expectEqual(@as(usize, 0), WordHost.model().file_errors);
5252
+ if (!queued) try fx.feedImageResult(key, .decode_failed, 0, 0, 0, "");
5253
+ WordHost.drain(&fx);
5254
+ try std.testing.expectEqual(.decode_failed, WordHost.model().image.?.state);
5255
+ try std.testing.expectEqual(@as(usize, 1), WordHost.model().image_replies);
5256
+ }
5257
+ }