@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
@@ -0,0 +1,692 @@
1
+ //! Session blob store: content-addressed payload storage beside the
2
+ //! session journal.
3
+ //!
4
+ //! Some effect results are too big — and too binary — to inline in
5
+ //! journal records: an image load's ENCODED source bytes are the whole
6
+ //! effect result, and replaying them byte-identically is the point. So
7
+ //! the journal record carries only a content address (the first 16
8
+ //! bytes of the payload's SHA-256, the `audioCachePath` hashing
9
+ //! convention) plus the length, and the bytes live as one file per
10
+ //! distinct payload under `blobs/` next to the journal file:
11
+ //!
12
+ //! <session_dir>/session.journal
13
+ //! <session_dir>/blobs/<32-hex-chars>
14
+ //!
15
+ //! Content addressing gives deduplication for free — recording the same
16
+ //! bytes twice (a cache hit replaying the same image, two loads of one
17
+ //! asset) writes one blob — and makes the store verifiable at BOTH
18
+ //! ends: replay re-hashes what it reads and refuses a store whose bytes
19
+ //! do not match their name, and recording's dedup probe reads the
20
+ //! existing file back before trusting it, repairing a mismatch in place
21
+ //! through the same atomic install a fresh write uses. A damaged blob
22
+ //! is a repairable state while the true bytes are in hand — recording
23
+ //! self-heals it instead of sealing a journal that replay must refuse.
24
+ //!
25
+ //! Two type-erased seams keep the recorder and replayer storage-
26
+ //! agnostic: `SessionBlobSink` (recording) and `SessionBlobSource`
27
+ //! (replay). `DirBlobStore` backs them with a directory over `std.Io`
28
+ //! (the app runner's wiring); `MemoryBlobStore` backs them in memory
29
+ //! for tests.
30
+
31
+ const std = @import("std");
32
+ const runtime_effects = @import("effects.zig");
33
+
34
+ const blob_log = std.log.scoped(.zero_session_blobs);
35
+
36
+ /// Bytes of a blob's content address (SHA-256 prefix); hex-encoded it
37
+ /// is the blob's file name.
38
+ pub const hash_len: usize = runtime_effects.effect_image_blob_hash_len;
39
+
40
+ /// The largest payload one blob may hold — the largest journaled effect
41
+ /// payload that goes out of line (an image load's encoded source).
42
+ pub const max_blob_bytes: usize = @max(
43
+ runtime_effects.effect_file_stream_chunk_bytes,
44
+ @max(runtime_effects.max_effect_image_bytes, runtime_effects.max_effect_persist_snapshot_bytes),
45
+ );
46
+
47
+ pub const BlobHash = [hash_len]u8;
48
+
49
+ /// Content address of `bytes`.
50
+ pub fn hashBytes(bytes: []const u8) BlobHash {
51
+ var digest: [32]u8 = undefined;
52
+ std.crypto.hash.sha2.Sha256.hash(bytes, &digest, .{});
53
+ return digest[0..hash_len].*;
54
+ }
55
+
56
+ /// A blob's file name: the address in lowercase hex, no extension (the
57
+ /// bytes are an opaque payload; decoders sniff content, not names).
58
+ pub fn hexName(hash: BlobHash) [hash_len * 2]u8 {
59
+ return std.fmt.bytesToHex(hash, .lower);
60
+ }
61
+
62
+ /// The temp path one blob write stages before its atomic rename into
63
+ /// the addressed name. `token` is the write's own random draw, and it
64
+ /// carries the WHOLE writer-uniqueness: the directory is shared between
65
+ /// recorders and the name is the content hash, so every other component
66
+ /// is identical when two recorders write the same blob — a temp built
67
+ /// without the token is one file both writers fight over. The name
68
+ /// stays recognizable install debris (`<hash>.<token>.partial` beside
69
+ /// its blob) for a hard crash to leave behind. A pure formatter: the
70
+ /// entropy lives with the caller.
71
+ fn blobPartialPath(buffer: []u8, dir_path: []const u8, name: []const u8, token: u64) ![]const u8 {
72
+ return std.fmt.bufPrint(buffer, "{s}/{s}.{x:0>16}.partial", .{ dir_path, name, token });
73
+ }
74
+
75
+ pub const BlobError = error{
76
+ /// The store has no blob under this address — the journal names
77
+ /// bytes the store never received (a moved journal without its
78
+ /// blobs directory, or a damaged store).
79
+ BlobMissing,
80
+ /// The stored bytes do not hash to their address, or their length
81
+ /// disagrees with the journal record: the store was damaged or
82
+ /// hand-edited.
83
+ BlobCorrupt,
84
+ /// The blob claims a payload beyond `max_blob_bytes`.
85
+ BlobOverBudget,
86
+ /// The store could not be written (recording) or read (replay).
87
+ BlobIoFailed,
88
+ };
89
+
90
+ /// Where recorded blobs go. `write_fn` must be idempotent per address —
91
+ /// content addressing means a repeat write of the same bytes is a no-op
92
+ /// by construction, and implementations should skip the copy AFTER
93
+ /// verifying the stored bytes really are these bytes (a damaged blob is
94
+ /// repaired from the bytes in hand, never trusted by name).
95
+ pub const SessionBlobSink = struct {
96
+ context: *anyopaque,
97
+ write_fn: *const fn (context: *anyopaque, hash: BlobHash, bytes: []const u8) BlobError!void,
98
+ };
99
+
100
+ /// Where replayed blobs come from. `read_fn` fills `buffer` and returns
101
+ /// the blob's bytes (a prefix of `buffer`); implementations verify the
102
+ /// content hash before answering.
103
+ pub const SessionBlobSource = struct {
104
+ context: *anyopaque,
105
+ read_fn: *const fn (context: *anyopaque, hash: BlobHash, buffer: []u8) BlobError![]const u8,
106
+ };
107
+
108
+ /// A directory-backed blob store over `std.Io` — the app runner's
109
+ /// implementation for both recording and replay. Writes are atomic
110
+ /// (beside-then-rename, the cache-install discipline) and deduplicated
111
+ /// by a VERIFYING probe — an existing file under the address is read
112
+ /// back and must hold exactly the incoming bytes, or the write repairs
113
+ /// it in place; reads re-hash and refuse mismatches.
114
+ ///
115
+ /// The directory is a SHARED medium: two recorders (two app processes,
116
+ /// or two stores in one process) may write the same blob into one
117
+ /// blobs/ directory concurrently. Two disciplines keep that safe:
118
+ /// every write stages through a WRITER-unique temp (a random token in
119
+ /// the name — a temp named by the hash alone would be one file both
120
+ /// writers truncate and rename out from under each other), and a
121
+ /// rename that still fails gets the content-addressed grace — when the
122
+ /// addressed name already holds exactly the bytes being written, some
123
+ /// writer won with THIS content, so the write succeeded (only a
124
+ /// verified mismatch or absence stays `BlobIoFailed`).
125
+ pub const DirBlobStore = struct {
126
+ io: std.Io,
127
+ dir_storage: [max_dir_bytes]u8 = undefined,
128
+ dir_len: usize = 0,
129
+
130
+ pub const max_dir_bytes: usize = 1024;
131
+
132
+ pub fn init(io: std.Io, dir_path: []const u8) error{BlobDirTooLong}!DirBlobStore {
133
+ if (dir_path.len == 0 or dir_path.len > max_dir_bytes) return error.BlobDirTooLong;
134
+ var store: DirBlobStore = .{ .io = io };
135
+ @memcpy(store.dir_storage[0..dir_path.len], dir_path);
136
+ store.dir_len = dir_path.len;
137
+ return store;
138
+ }
139
+
140
+ pub fn dir(self: *const DirBlobStore) []const u8 {
141
+ return self.dir_storage[0..self.dir_len];
142
+ }
143
+
144
+ pub fn sink(self: *DirBlobStore) SessionBlobSink {
145
+ return .{ .context = self, .write_fn = writeErased };
146
+ }
147
+
148
+ pub fn source(self: *DirBlobStore) SessionBlobSource {
149
+ return .{ .context = self, .read_fn = readErased };
150
+ }
151
+
152
+ pub fn write(self: *DirBlobStore, hash: BlobHash, bytes: []const u8) BlobError!void {
153
+ if (bytes.len > max_blob_bytes) return error.BlobOverBudget;
154
+ const cwd = std.Io.Dir.cwd();
155
+ var path_buffer: [max_dir_bytes + hash_len * 2 + 16]u8 = undefined;
156
+ const name = hexName(hash);
157
+ const blob_path = std.fmt.bufPrint(&path_buffer, "{s}/{s}", .{ self.dir(), name }) catch return error.BlobIoFailed;
158
+ // Content addressing dedups by name, but recording never TRUSTS
159
+ // the name alone: replay verifies what it reads, so a damaged
160
+ // blob accepted here would seal a journal replay must refuse.
161
+ // The probe reads the existing file back (bounded by the bytes
162
+ // in hand, themselves under max_blob_bytes) and skips the write
163
+ // only on an exact match; anything else — damage, truncation,
164
+ // an unreadable file — is a repairable state while the true
165
+ // bytes are right here, so it falls through to the same atomic
166
+ // partial+rename a fresh write uses. Cost: a dedup hit is one
167
+ // bounded read instead of one existence probe.
168
+ switch (self.probeExisting(blob_path, bytes)) {
169
+ .matches => return,
170
+ .absent => {},
171
+ .mismatch => blob_log.debug("blob {s} does not hold its addressed bytes; repairing in place", .{name}),
172
+ }
173
+ cwd.createDirPath(self.io, self.dir()) catch return error.BlobIoFailed;
174
+ // The temp name must be WRITER-unique, not merely blob-unique:
175
+ // two recorders sharing one blobs/ directory write the same
176
+ // blob under the same hash, and a temp named by the hash alone
177
+ // is ONE file both writers truncate mid-write and rename out
178
+ // from under each other (the loser's rename then fails a
179
+ // correct recording). The token is the write's own random draw
180
+ // from the store's io (the CSPRNG seam the executor's cache
181
+ // installs already use), unique across stores and processes
182
+ // alike.
183
+ var token_bytes: [8]u8 = undefined;
184
+ self.io.random(&token_bytes);
185
+ const token = std.mem.readInt(u64, &token_bytes, .little);
186
+ var partial_buffer: [max_dir_bytes + hash_len * 2 + 48]u8 = undefined;
187
+ const partial_path = blobPartialPath(&partial_buffer, self.dir(), &name, token) catch return error.BlobIoFailed;
188
+ cwd.writeFile(self.io, .{ .sub_path = partial_path, .data = bytes }) catch {
189
+ cwd.deleteFile(self.io, partial_path) catch {};
190
+ return error.BlobIoFailed;
191
+ };
192
+ return self.installPartial(partial_path, blob_path, bytes);
193
+ }
194
+
195
+ /// The atomic tail of `write`: rename the writer-unique temp into
196
+ /// the blob's addressed name. A failed rename is not yet a failed
197
+ /// WRITE — the store is content-addressed, so if the addressed name
198
+ /// already holds exactly these bytes, a concurrent writer won the
199
+ /// install with the same content and this write SUCCEEDED (its
200
+ /// bytes are durable under their address; only who renamed them
201
+ /// there differs). Re-running the verifying probe decides: a match
202
+ /// returns cleanly, a verified mismatch or absence is the honest
203
+ /// `BlobIoFailed`. The loser's temp is deleted on every failure
204
+ /// path so no `.partial` debris outlives the write.
205
+ fn installPartial(self: *DirBlobStore, partial_path: []const u8, blob_path: []const u8, bytes: []const u8) BlobError!void {
206
+ const cwd = std.Io.Dir.cwd();
207
+ cwd.rename(partial_path, cwd, blob_path, self.io) catch {
208
+ defer cwd.deleteFile(self.io, partial_path) catch {};
209
+ switch (self.probeExisting(blob_path, bytes)) {
210
+ .matches => return,
211
+ .absent, .mismatch => return error.BlobIoFailed,
212
+ }
213
+ };
214
+ }
215
+
216
+ const ProbeResult = enum { absent, matches, mismatch };
217
+
218
+ /// The verifying half of the dedup probe: whether the file at
219
+ /// `blob_path` holds exactly `bytes`. Compared chunk-wise against
220
+ /// the incoming bytes (equality against the caller's bytes IS the
221
+ /// hash check — their address is the file's name), so no
222
+ /// blob-sized buffer is ever staged. A read failure mid-probe
223
+ /// counts as a mismatch: the repair path rewrites the file whole.
224
+ fn probeExisting(self: *DirBlobStore, blob_path: []const u8, bytes: []const u8) ProbeResult {
225
+ var file = std.Io.Dir.cwd().openFile(self.io, blob_path, .{}) catch return .absent;
226
+ defer file.close(self.io);
227
+ var chunk: [4096]u8 = undefined;
228
+ var at: usize = 0;
229
+ while (true) {
230
+ const len = file.readPositionalAll(self.io, &chunk, at) catch return .mismatch;
231
+ if (len == 0) break;
232
+ if (at + len > bytes.len) return .mismatch;
233
+ if (!std.mem.eql(u8, chunk[0..len], bytes[at .. at + len])) return .mismatch;
234
+ at += len;
235
+ if (len < chunk.len) break;
236
+ }
237
+ return if (at == bytes.len) .matches else .mismatch;
238
+ }
239
+
240
+ pub fn read(self: *DirBlobStore, hash: BlobHash, buffer: []u8) BlobError![]const u8 {
241
+ const cwd = std.Io.Dir.cwd();
242
+ var path_buffer: [max_dir_bytes + hash_len * 2 + 16]u8 = undefined;
243
+ const name = hexName(hash);
244
+ const blob_path = std.fmt.bufPrint(&path_buffer, "{s}/{s}", .{ self.dir(), name }) catch return error.BlobIoFailed;
245
+ var file = cwd.openFile(self.io, blob_path, .{}) catch return error.BlobMissing;
246
+ defer file.close(self.io);
247
+ const len = file.readPositionalAll(self.io, buffer, 0) catch return error.BlobIoFailed;
248
+ if (len == buffer.len) {
249
+ // A full buffer is ambiguous: `readPositionalAll` stops at
250
+ // `buffer.len`, so the blob is either exactly buffer-sized
251
+ // (the designed case — callers size the buffer from the
252
+ // journal record's byte length) or larger. Probe one byte
253
+ // past the end to tell them apart — the effects layer's
254
+ // one-spare-byte idiom, and a probe rather than a stat so
255
+ // no size/read race can disagree with what was just read.
256
+ var spare: [1]u8 = undefined;
257
+ const extra = file.readPositionalAll(self.io, &spare, buffer.len) catch return error.BlobIoFailed;
258
+ if (extra != 0) return error.BlobOverBudget;
259
+ }
260
+ const bytes = buffer[0..len];
261
+ if (!std.mem.eql(u8, &hashBytes(bytes), &hash)) return error.BlobCorrupt;
262
+ return bytes;
263
+ }
264
+
265
+ fn writeErased(context: *anyopaque, hash: BlobHash, bytes: []const u8) BlobError!void {
266
+ const self: *DirBlobStore = @ptrCast(@alignCast(context));
267
+ return self.write(hash, bytes);
268
+ }
269
+
270
+ fn readErased(context: *anyopaque, hash: BlobHash, buffer: []u8) BlobError![]const u8 {
271
+ const self: *DirBlobStore = @ptrCast(@alignCast(context));
272
+ return self.read(hash, buffer);
273
+ }
274
+ };
275
+
276
+ /// An in-memory blob store for tests: allocator-backed, bounded, and
277
+ /// honest about capacity. Write dedups by address with the directory
278
+ /// store's verifying probe (an entry that no longer holds its
279
+ /// addressed bytes is repaired in place); read verifies the hash like
280
+ /// the directory store, so a test store misbehaves exactly as loudly
281
+ /// as the real one. The directory store's two-writer race has no
282
+ /// analogue here: this store's medium is its own `entries` array, not
283
+ /// a shared directory — two recorders can never address one
284
+ /// MemoryBlobStore from two processes, and within one process each
285
+ /// recorder's sink drives its own store instance on its own thread —
286
+ /// so there is no shared temp to fight over and no rename to lose.
287
+ pub const MemoryBlobStore = struct {
288
+ allocator: std.mem.Allocator,
289
+ entries: [max_entries]Entry = undefined,
290
+ count: usize = 0,
291
+ /// How many writes found their address already present — the dedup
292
+ /// evidence tests assert on.
293
+ dedup_hits: usize = 0,
294
+
295
+ pub const max_entries: usize = 32;
296
+
297
+ const Entry = struct {
298
+ hash: BlobHash,
299
+ bytes: []u8,
300
+ };
301
+
302
+ pub fn init(allocator: std.mem.Allocator) MemoryBlobStore {
303
+ return .{ .allocator = allocator };
304
+ }
305
+
306
+ pub fn deinit(self: *MemoryBlobStore) void {
307
+ for (self.entries[0..self.count]) |entry| self.allocator.free(entry.bytes);
308
+ self.count = 0;
309
+ }
310
+
311
+ pub fn sink(self: *MemoryBlobStore) SessionBlobSink {
312
+ return .{ .context = self, .write_fn = writeErased };
313
+ }
314
+
315
+ pub fn source(self: *MemoryBlobStore) SessionBlobSource {
316
+ return .{ .context = self, .read_fn = readErased };
317
+ }
318
+
319
+ pub fn write(self: *MemoryBlobStore, hash: BlobHash, bytes: []const u8) BlobError!void {
320
+ if (bytes.len > max_blob_bytes) return error.BlobOverBudget;
321
+ if (self.find(hash)) |entry| {
322
+ // The dir store's verifying dedup, in memory: an entry that
323
+ // still holds its addressed bytes is the dedup hit; one
324
+ // that no longer does (a tampering test — the dir store's
325
+ // damaged-file case) is repaired in place rather than
326
+ // trusted or refused.
327
+ if (std.mem.eql(u8, entry.bytes, bytes)) {
328
+ self.dedup_hits += 1;
329
+ return;
330
+ }
331
+ const copy = self.allocator.dupe(u8, bytes) catch return error.BlobIoFailed;
332
+ self.allocator.free(entry.bytes);
333
+ entry.bytes = copy;
334
+ return;
335
+ }
336
+ if (self.count == max_entries) return error.BlobIoFailed;
337
+ const copy = self.allocator.dupe(u8, bytes) catch return error.BlobIoFailed;
338
+ self.entries[self.count] = .{ .hash = hash, .bytes = copy };
339
+ self.count += 1;
340
+ }
341
+
342
+ pub fn read(self: *MemoryBlobStore, hash: BlobHash, buffer: []u8) BlobError![]const u8 {
343
+ const entry = self.find(hash) orelse return error.BlobMissing;
344
+ if (entry.bytes.len > buffer.len) return error.BlobOverBudget;
345
+ @memcpy(buffer[0..entry.bytes.len], entry.bytes);
346
+ const bytes = buffer[0..entry.bytes.len];
347
+ if (!std.mem.eql(u8, &hashBytes(bytes), &hash)) return error.BlobCorrupt;
348
+ return bytes;
349
+ }
350
+
351
+ fn find(self: *MemoryBlobStore, hash: BlobHash) ?*Entry {
352
+ for (self.entries[0..self.count]) |*entry| {
353
+ if (std.mem.eql(u8, &entry.hash, &hash)) return entry;
354
+ }
355
+ return null;
356
+ }
357
+
358
+ fn writeErased(context: *anyopaque, hash: BlobHash, bytes: []const u8) BlobError!void {
359
+ const self: *MemoryBlobStore = @ptrCast(@alignCast(context));
360
+ return self.write(hash, bytes);
361
+ }
362
+
363
+ fn readErased(context: *anyopaque, hash: BlobHash, buffer: []u8) BlobError![]const u8 {
364
+ const self: *MemoryBlobStore = @ptrCast(@alignCast(context));
365
+ return self.read(hash, buffer);
366
+ }
367
+ };
368
+
369
+ // -------------------------------------------------------------- tests
370
+
371
+ const testing = std.testing;
372
+
373
+ test "content addresses are stable and hex names filesystem-safe" {
374
+ const hash = hashBytes("the same bytes");
375
+ try testing.expectEqualSlices(u8, &hash, &hashBytes("the same bytes"));
376
+ try testing.expect(!std.mem.eql(u8, &hash, &hashBytes("different bytes")));
377
+ const name = hexName(hash);
378
+ for (name) |char| {
379
+ try testing.expect((char >= '0' and char <= '9') or (char >= 'a' and char <= 'f'));
380
+ }
381
+ }
382
+
383
+ test "memory store round-trips, dedups identical bytes, and refuses damage" {
384
+ var store = MemoryBlobStore.init(testing.allocator);
385
+ defer store.deinit();
386
+
387
+ const bytes = "png bytes, say";
388
+ const hash = hashBytes(bytes);
389
+ try store.write(hash, bytes);
390
+ try store.write(hash, bytes);
391
+ try testing.expectEqual(@as(usize, 1), store.count);
392
+ try testing.expectEqual(@as(usize, 1), store.dedup_hits);
393
+
394
+ var buffer: [64]u8 = undefined;
395
+ const read_back = try store.read(hash, &buffer);
396
+ try testing.expectEqualStrings(bytes, read_back);
397
+
398
+ try testing.expectError(error.BlobMissing, store.read(hashBytes("never written"), &buffer));
399
+ // A tampered entry no longer hashes to its address.
400
+ store.entries[0].bytes[0] ^= 0x40;
401
+ try testing.expectError(error.BlobCorrupt, store.read(hash, &buffer));
402
+ }
403
+
404
+ test "memory store repairs a tampered entry on the next same-bytes write" {
405
+ var store = MemoryBlobStore.init(testing.allocator);
406
+ defer store.deinit();
407
+
408
+ const bytes = "encoded image bytes";
409
+ const hash = hashBytes(bytes);
410
+ try store.write(hash, bytes);
411
+ store.entries[0].bytes[0] ^= 0x40;
412
+ var buffer: [64]u8 = undefined;
413
+ try testing.expectError(error.BlobCorrupt, store.read(hash, &buffer));
414
+
415
+ // Recording the same bytes again finds the address occupied but
416
+ // VERIFIES before trusting it: the damaged entry heals in place
417
+ // (no dedup hit — a repair is not a dedup), and replay succeeds.
418
+ try store.write(hash, bytes);
419
+ try testing.expectEqual(@as(usize, 0), store.dedup_hits);
420
+ try testing.expectEqual(@as(usize, 1), store.count);
421
+ const read_back = try store.read(hash, &buffer);
422
+ try testing.expectEqualStrings(bytes, read_back);
423
+ }
424
+
425
+ test "dir store writes atomically, dedups by existence, and verifies on read" {
426
+ var tmp = testing.tmpDir(.{});
427
+ defer tmp.cleanup();
428
+ const io = testing.io;
429
+
430
+ var dir_buffer: [256]u8 = undefined;
431
+ const dir_path = try std.fmt.bufPrint(&dir_buffer, ".zig-cache/tmp/{s}/blobs", .{tmp.sub_path[0..]});
432
+ var store = try DirBlobStore.init(io, dir_path);
433
+
434
+ const bytes = "encoded image bytes";
435
+ const hash = hashBytes(bytes);
436
+ try store.write(hash, bytes);
437
+ try store.write(hash, bytes);
438
+
439
+ var read_buffer: [4096]u8 = undefined;
440
+ const read_back = try store.read(hash, &read_buffer);
441
+ try testing.expectEqualStrings(bytes, read_back);
442
+
443
+ // Exactly one blob file, no .partial debris.
444
+ var blob_dir = try tmp.dir.openDir(io, "blobs", .{ .iterate = true });
445
+ defer blob_dir.close(io);
446
+ var iterator = blob_dir.iterate();
447
+ var file_count: usize = 0;
448
+ while (try iterator.next(io)) |entry| {
449
+ try testing.expect(!std.mem.endsWith(u8, entry.name, ".partial"));
450
+ file_count += 1;
451
+ }
452
+ try testing.expectEqual(@as(usize, 1), file_count);
453
+
454
+ try testing.expectError(error.BlobMissing, store.read(hashBytes("absent"), &read_buffer));
455
+
456
+ // Damaged bytes are refused, never returned as the payload.
457
+ const name = hexName(hash);
458
+ var damaged: [64]u8 = undefined;
459
+ @memcpy(damaged[0..bytes.len], bytes);
460
+ damaged[0] ^= 0x40;
461
+ var path_buffer: [300]u8 = undefined;
462
+ const rel = try std.fmt.bufPrint(&path_buffer, "blobs/{s}", .{name});
463
+ try tmp.dir.writeFile(io, .{ .sub_path = rel, .data = damaged[0..bytes.len] });
464
+ try testing.expectError(error.BlobCorrupt, store.read(hash, &read_buffer));
465
+ }
466
+
467
+ test "both stores accept an exact-fit buffer and refuse one byte under" {
468
+ // The SessionBlobSource contract lets callers size the buffer from
469
+ // the journal record's byte length — an exact-fit read is the
470
+ // designed case, not an over-budget one. Over-budget is a buffer
471
+ // the blob does NOT fit in, even by one byte.
472
+ var tmp = testing.tmpDir(.{});
473
+ defer tmp.cleanup();
474
+ const io = testing.io;
475
+
476
+ var dir_buffer: [256]u8 = undefined;
477
+ const dir_path = try std.fmt.bufPrint(&dir_buffer, ".zig-cache/tmp/{s}/blobs", .{tmp.sub_path[0..]});
478
+ var dir_store = try DirBlobStore.init(io, dir_path);
479
+
480
+ const bytes = "encoded image bytes";
481
+ const hash = hashBytes(bytes);
482
+ try dir_store.write(hash, bytes);
483
+
484
+ var buffer: [bytes.len]u8 = undefined;
485
+ try testing.expectEqualStrings(bytes, try dir_store.read(hash, &buffer));
486
+ try testing.expectError(error.BlobOverBudget, dir_store.read(hash, buffer[0 .. bytes.len - 1]));
487
+
488
+ // Store parity: the memory store answers the same three sizes the
489
+ // same way, so a test against it exercises the real contract.
490
+ var memory_store = MemoryBlobStore.init(testing.allocator);
491
+ defer memory_store.deinit();
492
+ try memory_store.write(hash, bytes);
493
+ try testing.expectEqualStrings(bytes, try memory_store.read(hash, &buffer));
494
+ try testing.expectError(error.BlobOverBudget, memory_store.read(hash, buffer[0 .. bytes.len - 1]));
495
+ }
496
+
497
+ test "an exact-fit read of damaged bytes is BlobCorrupt, not BlobOverBudget" {
498
+ // The budget check must not mask corruption detection: a blob that
499
+ // fills the buffer exactly still gets re-hashed, and damage is
500
+ // reported as damage.
501
+ var tmp = testing.tmpDir(.{});
502
+ defer tmp.cleanup();
503
+ const io = testing.io;
504
+
505
+ var dir_buffer: [256]u8 = undefined;
506
+ const dir_path = try std.fmt.bufPrint(&dir_buffer, ".zig-cache/tmp/{s}/blobs", .{tmp.sub_path[0..]});
507
+ var dir_store = try DirBlobStore.init(io, dir_path);
508
+
509
+ const bytes = "encoded image bytes";
510
+ const hash = hashBytes(bytes);
511
+ try dir_store.write(hash, bytes);
512
+ var damaged: [bytes.len]u8 = undefined;
513
+ @memcpy(&damaged, bytes);
514
+ damaged[0] ^= 0x40;
515
+ const name = hexName(hash);
516
+ var path_buffer: [300]u8 = undefined;
517
+ const rel = try std.fmt.bufPrint(&path_buffer, "blobs/{s}", .{name});
518
+ try tmp.dir.writeFile(io, .{ .sub_path = rel, .data = &damaged });
519
+
520
+ var buffer: [bytes.len]u8 = undefined;
521
+ try testing.expectError(error.BlobCorrupt, dir_store.read(hash, &buffer));
522
+
523
+ var memory_store = MemoryBlobStore.init(testing.allocator);
524
+ defer memory_store.deinit();
525
+ try memory_store.write(hash, bytes);
526
+ memory_store.entries[0].bytes[0] ^= 0x40;
527
+ try testing.expectError(error.BlobCorrupt, memory_store.read(hash, &buffer));
528
+ }
529
+
530
+ test "two recorders sharing one blobs directory both record the same blob successfully" {
531
+ var tmp = testing.tmpDir(.{});
532
+ defer tmp.cleanup();
533
+ const io = testing.io;
534
+
535
+ var dir_buffer: [256]u8 = undefined;
536
+ const dir_path = try std.fmt.bufPrint(&dir_buffer, ".zig-cache/tmp/{s}/blobs", .{tmp.sub_path[0..]});
537
+
538
+ // Two DirBlobStore INSTANCES over one directory — the designed
539
+ // shared layout when two recorders journal into one session
540
+ // directory. Both write the same bytes (the same image loaded in
541
+ // both recordings addresses the same blob) and BOTH writes must
542
+ // succeed: a failure here invalidates a correct recording.
543
+ var first_recorder = try DirBlobStore.init(io, dir_path);
544
+ var second_recorder = try DirBlobStore.init(io, dir_path);
545
+
546
+ const bytes = "encoded image bytes both sessions loaded";
547
+ const hash = hashBytes(bytes);
548
+ try first_recorder.write(hash, bytes);
549
+ try second_recorder.write(hash, bytes);
550
+
551
+ // One intact blob, readable through either store, zero temp debris.
552
+ var read_buffer: [4096]u8 = undefined;
553
+ try testing.expectEqualStrings(bytes, try first_recorder.read(hash, &read_buffer));
554
+ try testing.expectEqualStrings(bytes, try second_recorder.read(hash, &read_buffer));
555
+ var blob_dir = try tmp.dir.openDir(io, "blobs", .{ .iterate = true });
556
+ defer blob_dir.close(io);
557
+ var iterator = blob_dir.iterate();
558
+ var file_count: usize = 0;
559
+ while (try iterator.next(io)) |entry| {
560
+ try testing.expect(!std.mem.endsWith(u8, entry.name, ".partial"));
561
+ file_count += 1;
562
+ }
563
+ try testing.expectEqual(@as(usize, 1), file_count);
564
+ }
565
+
566
+ test "blob write temps are writer-unique, so concurrent recorders never share one" {
567
+ // Two writes of the same blob into the same directory agree on
568
+ // every name component except the token: dir (shared layout), name
569
+ // (the content hash). A temp built without the token would be ONE
570
+ // file both writers truncate mid-write and rename out from under
571
+ // each other — the loser's rename fails and a correct recording is
572
+ // refused. The token carries the whole uniqueness, so this pin
573
+ // fails if it ever leaves the name.
574
+ var first_buffer: [256]u8 = undefined;
575
+ var second_buffer: [256]u8 = undefined;
576
+ const name = "aabbccddeeff00112233445566778899";
577
+ const first = try blobPartialPath(&first_buffer, "session/blobs", name, 0x1111);
578
+ const second = try blobPartialPath(&second_buffer, "session/blobs", name, 0x2222);
579
+ try testing.expect(!std.mem.eql(u8, first, second));
580
+ // Both stay recognizable install debris beside their blob.
581
+ try testing.expect(std.mem.startsWith(u8, first, "session/blobs/aabbccddeeff00112233445566778899."));
582
+ try testing.expect(std.mem.endsWith(u8, first, ".partial"));
583
+ try testing.expect(std.mem.endsWith(u8, second, ".partial"));
584
+ }
585
+
586
+ test "a losing writer's failed rename succeeds through the content-addressed grace" {
587
+ var tmp = testing.tmpDir(.{});
588
+ defer tmp.cleanup();
589
+ const io = testing.io;
590
+
591
+ var dir_buffer: [256]u8 = undefined;
592
+ const dir_path = try std.fmt.bufPrint(&dir_buffer, ".zig-cache/tmp/{s}/blobs", .{tmp.sub_path[0..]});
593
+ var winner = try DirBlobStore.init(io, dir_path);
594
+ var loser = try DirBlobStore.init(io, dir_path);
595
+
596
+ const bytes = "encoded image bytes";
597
+ const hash = hashBytes(bytes);
598
+ const name = hexName(hash);
599
+ var blob_path_buffer: [512]u8 = undefined;
600
+ const blob_path = try std.fmt.bufPrint(&blob_path_buffer, "{s}/{s}", .{ dir_path, name });
601
+ var partial_buffer: [512]u8 = undefined;
602
+ const partial_path = try blobPartialPath(&partial_buffer, dir_path, &name, 0xdead);
603
+
604
+ // The loser's exact state at the rename boundary, staged directly
605
+ // (one `write` call cannot be paused mid-window, so the window is
606
+ // pinned at the install seam): its probe saw the address ABSENT,
607
+ // the winner then installed the addressed bytes, and the loser's
608
+ // rename fails (here: the temp is gone — the shared-name theft this
609
+ // fix's unique tokens prevent, and the one rename failure POSIX
610
+ // makes deterministic). The bytes ARE durable under their address,
611
+ // so the write must report success, not refuse a correct recording.
612
+ try winner.write(hash, bytes);
613
+ try loser.installPartial(partial_path, blob_path, bytes);
614
+ var read_buffer: [4096]u8 = undefined;
615
+ try testing.expectEqualStrings(bytes, try loser.read(hash, &read_buffer));
616
+
617
+ // The grace VERIFIES: an address occupied by different bytes means
618
+ // the write's bytes are NOT durable — that stays BlobIoFailed
619
+ // (write's earlier repair path handles mismatches while the rename
620
+ // can still deliver; here it cannot).
621
+ const other = "different bytes at the same staged path";
622
+ try testing.expectError(error.BlobIoFailed, loser.installPartial(partial_path, blob_path, other));
623
+
624
+ // ...and an absent address after a failed rename is a plain failed
625
+ // write.
626
+ var absent_path_buffer: [512]u8 = undefined;
627
+ const absent_name = hexName(hashBytes(other));
628
+ const absent_path = try std.fmt.bufPrint(&absent_path_buffer, "{s}/{s}", .{ dir_path, absent_name });
629
+ var absent_partial_buffer: [512]u8 = undefined;
630
+ const absent_partial = try blobPartialPath(&absent_partial_buffer, dir_path, &absent_name, 0xbeef);
631
+ try testing.expectError(error.BlobIoFailed, loser.installPartial(absent_partial, absent_path, other));
632
+
633
+ // No temp debris outlives any of it.
634
+ var blob_dir = try tmp.dir.openDir(io, "blobs", .{ .iterate = true });
635
+ defer blob_dir.close(io);
636
+ var iterator = blob_dir.iterate();
637
+ var file_count: usize = 0;
638
+ while (try iterator.next(io)) |entry| {
639
+ try testing.expect(!std.mem.endsWith(u8, entry.name, ".partial"));
640
+ file_count += 1;
641
+ }
642
+ try testing.expectEqual(@as(usize, 1), file_count);
643
+ }
644
+
645
+ test "dir store verifies the dedup probe and repairs a damaged blob in place" {
646
+ var tmp = testing.tmpDir(.{});
647
+ defer tmp.cleanup();
648
+ const io = testing.io;
649
+
650
+ var dir_buffer: [256]u8 = undefined;
651
+ const dir_path = try std.fmt.bufPrint(&dir_buffer, ".zig-cache/tmp/{s}/blobs", .{tmp.sub_path[0..]});
652
+ var store = try DirBlobStore.init(io, dir_path);
653
+
654
+ const bytes = "encoded image bytes";
655
+ const hash = hashBytes(bytes);
656
+ const name = hexName(hash);
657
+ var path_buffer: [300]u8 = undefined;
658
+ const rel = try std.fmt.bufPrint(&path_buffer, "blobs/{s}", .{name});
659
+ try store.write(hash, bytes);
660
+
661
+ // Flip one byte on disk: the blob's name lies about its content.
662
+ // Without the true bytes in hand this is replay's BlobCorrupt; the
663
+ // next same-bytes RECORDING has them, so the dedup probe detects
664
+ // the mismatch and repairs in place instead of sealing a journal
665
+ // replay must refuse.
666
+ var damaged: [64]u8 = undefined;
667
+ @memcpy(damaged[0..bytes.len], bytes);
668
+ damaged[3] ^= 0x40;
669
+ try tmp.dir.writeFile(io, .{ .sub_path = rel, .data = damaged[0..bytes.len] });
670
+ var read_buffer: [4096]u8 = undefined;
671
+ try testing.expectError(error.BlobCorrupt, store.read(hash, &read_buffer));
672
+ try store.write(hash, bytes);
673
+ try testing.expectEqualStrings(bytes, try store.read(hash, &read_buffer));
674
+
675
+ // A truncated blob (a different length, not just different bytes)
676
+ // repairs the same way.
677
+ try tmp.dir.writeFile(io, .{ .sub_path = rel, .data = bytes[0 .. bytes.len - 3] });
678
+ try store.write(hash, bytes);
679
+ try testing.expectEqualStrings(bytes, try store.read(hash, &read_buffer));
680
+
681
+ // The repair went through the atomic partial+rename path: exactly
682
+ // one blob file, no .partial debris.
683
+ var blob_dir = try tmp.dir.openDir(io, "blobs", .{ .iterate = true });
684
+ defer blob_dir.close(io);
685
+ var iterator = blob_dir.iterate();
686
+ var file_count: usize = 0;
687
+ while (try iterator.next(io)) |entry| {
688
+ try testing.expect(!std.mem.endsWith(u8, entry.name, ".partial"));
689
+ file_count += 1;
690
+ }
691
+ try testing.expectEqual(@as(usize, 1), file_count);
692
+ }