@lix-js/sdk 0.6.0-preview.4 → 0.6.0-preview.5

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 (234) hide show
  1. package/README.md +1 -1
  2. package/SKILL.md +65 -64
  3. package/dist/engine-wasm/index.js +4 -4
  4. package/dist/engine-wasm/wasm/lix_engine.d.ts +5 -5
  5. package/dist/engine-wasm/wasm/lix_engine.js +130 -118
  6. package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
  7. package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +9 -8
  8. package/dist/generated/builtin-schemas.d.ts +69 -69
  9. package/dist/generated/builtin-schemas.js +94 -94
  10. package/dist/open-lix.d.ts +33 -26
  11. package/dist/open-lix.js +10 -10
  12. package/dist/sqlite/index.js +86 -30
  13. package/dist-engine-src/README.md +3 -3
  14. package/dist-engine-src/src/backend/capabilities.rs +67 -0
  15. package/dist-engine-src/src/backend/conformance/baseline.rs +1127 -0
  16. package/dist-engine-src/src/backend/conformance/factory.rs +93 -0
  17. package/dist-engine-src/src/backend/conformance/failure_tests.rs +608 -0
  18. package/dist-engine-src/src/backend/conformance/fixtures.rs +26 -0
  19. package/dist-engine-src/src/backend/conformance/mod.rs +75 -0
  20. package/dist-engine-src/src/backend/conformance/model.rs +28 -0
  21. package/dist-engine-src/src/backend/conformance/model_based.rs +257 -0
  22. package/dist-engine-src/src/backend/conformance/persistence.rs +204 -0
  23. package/dist-engine-src/src/backend/conformance/projection.rs +21 -0
  24. package/dist-engine-src/src/backend/conformance/pushdown.rs +24 -0
  25. package/dist-engine-src/src/backend/conformance/runner.rs +90 -0
  26. package/dist-engine-src/src/backend/conformance/scan.rs +24 -0
  27. package/dist-engine-src/src/backend/conformance/write.rs +16 -0
  28. package/dist-engine-src/src/backend/error.rs +94 -0
  29. package/dist-engine-src/src/backend/in_memory.rs +670 -0
  30. package/dist-engine-src/src/backend/mod.rs +36 -9
  31. package/dist-engine-src/src/backend/predicate.rs +80 -0
  32. package/dist-engine-src/src/backend/traits.rs +260 -0
  33. package/dist-engine-src/src/backend/types.rs +224 -81
  34. package/dist-engine-src/src/binary_cas/context.rs +8 -8
  35. package/dist-engine-src/src/binary_cas/kv.rs +234 -259
  36. package/dist-engine-src/src/{version → branch}/context.rs +12 -12
  37. package/dist-engine-src/src/branch/lifecycle.rs +221 -0
  38. package/dist-engine-src/src/branch/mod.rs +13 -0
  39. package/dist-engine-src/src/branch/refs.rs +321 -0
  40. package/dist-engine-src/src/branch/stage_rows.rs +67 -0
  41. package/dist-engine-src/src/branch/types.rs +21 -0
  42. package/dist-engine-src/src/catalog/context.rs +18 -18
  43. package/dist-engine-src/src/catalog/snapshot.rs +8 -8
  44. package/dist-engine-src/src/changelog/bench_support.rs +785 -0
  45. package/dist-engine-src/src/changelog/change.rs +1 -0
  46. package/dist-engine-src/src/changelog/codec.rs +497 -0
  47. package/dist-engine-src/src/changelog/commit.rs +1 -0
  48. package/dist-engine-src/src/changelog/context.rs +1614 -0
  49. package/dist-engine-src/src/changelog/mod.rs +29 -0
  50. package/dist-engine-src/src/changelog/store.rs +163 -0
  51. package/dist-engine-src/src/changelog/test_support.rs +54 -0
  52. package/dist-engine-src/src/changelog/types.rs +213 -0
  53. package/dist-engine-src/src/commit_graph/context.rs +317 -274
  54. package/dist-engine-src/src/commit_graph/mod.rs +2 -4
  55. package/dist-engine-src/src/commit_graph/types.rs +22 -42
  56. package/dist-engine-src/src/commit_graph/walker.rs +133 -103
  57. package/dist-engine-src/src/common/error.rs +52 -18
  58. package/dist-engine-src/src/common/identity.rs +2 -2
  59. package/dist-engine-src/src/common/mod.rs +1 -1
  60. package/dist-engine-src/src/domain.rs +42 -46
  61. package/dist-engine-src/src/engine.rs +74 -96
  62. package/dist-engine-src/src/{entity_identity.rs → entity_pk.rs} +89 -92
  63. package/dist-engine-src/src/functions/context.rs +56 -52
  64. package/dist-engine-src/src/functions/state.rs +51 -52
  65. package/dist-engine-src/src/init.rs +288 -154
  66. package/dist-engine-src/src/json_store/context.rs +15 -266
  67. package/dist-engine-src/src/json_store/mod.rs +26 -0
  68. package/dist-engine-src/src/json_store/store.rs +103 -718
  69. package/dist-engine-src/src/json_store/types.rs +4 -9
  70. package/dist-engine-src/src/lib.rs +49 -19
  71. package/dist-engine-src/src/live_state/context.rs +654 -790
  72. package/dist-engine-src/src/live_state/mod.rs +9 -3
  73. package/dist-engine-src/src/live_state/overlay.rs +4 -4
  74. package/dist-engine-src/src/live_state/types.rs +30 -21
  75. package/dist-engine-src/src/live_state/visibility.rs +514 -71
  76. package/dist-engine-src/src/plugin/install.rs +48 -48
  77. package/dist-engine-src/src/plugin/manifest.rs +7 -7
  78. package/dist-engine-src/src/plugin/materializer.rs +0 -275
  79. package/dist-engine-src/src/plugin/plugin_manifest.json +4 -3
  80. package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +2 -2
  81. package/dist-engine-src/src/schema/builtin/lix_branch_descriptor.json +34 -0
  82. package/dist-engine-src/src/schema/builtin/lix_branch_ref.json +48 -0
  83. package/dist-engine-src/src/schema/builtin/lix_change.json +3 -3
  84. package/dist-engine-src/src/schema/builtin/lix_commit.json +1 -1
  85. package/dist-engine-src/src/schema/builtin/lix_label_assignment.json +6 -6
  86. package/dist-engine-src/src/schema/builtin/mod.rs +18 -20
  87. package/dist-engine-src/src/schema/compatibility.rs +11 -11
  88. package/dist-engine-src/src/schema/definition.json +2 -2
  89. package/dist-engine-src/src/schema/definition.rs +5 -5
  90. package/dist-engine-src/src/schema/key.rs +3 -3
  91. package/dist-engine-src/src/schema/mod.rs +1 -1
  92. package/dist-engine-src/src/schema/tests.rs +18 -18
  93. package/dist-engine-src/src/session/context.rs +803 -148
  94. package/dist-engine-src/src/session/create_branch.rs +94 -0
  95. package/dist-engine-src/src/session/execute.rs +223 -83
  96. package/dist-engine-src/src/session/merge/analysis.rs +9 -3
  97. package/dist-engine-src/src/session/merge/{version.rs → branch.rs} +119 -129
  98. package/dist-engine-src/src/session/merge/conflicts.rs +2 -2
  99. package/dist-engine-src/src/session/merge/mod.rs +5 -6
  100. package/dist-engine-src/src/session/merge/stats.rs +7 -11
  101. package/dist-engine-src/src/session/mod.rs +15 -12
  102. package/dist-engine-src/src/session/switch_branch.rs +113 -0
  103. package/dist-engine-src/src/session/transaction.rs +495 -14
  104. package/dist-engine-src/src/sql2/{classify.rs → bind/classify.rs} +3 -75
  105. package/dist-engine-src/src/sql2/bind/error.rs +5 -0
  106. package/dist-engine-src/src/sql2/bind/expr.rs +29 -0
  107. package/dist-engine-src/src/sql2/bind/mod.rs +12 -0
  108. package/dist-engine-src/src/sql2/{udfs/public_call.rs → bind/public_udf.rs} +71 -3
  109. package/dist-engine-src/src/sql2/bind/read.rs +65 -0
  110. package/dist-engine-src/src/sql2/bind/statement.rs +2236 -0
  111. package/dist-engine-src/src/sql2/bind/table.rs +273 -0
  112. package/dist-engine-src/src/sql2/bind/write.rs +86 -0
  113. package/dist-engine-src/src/sql2/branch_scope.rs +436 -0
  114. package/dist-engine-src/src/sql2/catalog/capability.rs +20 -0
  115. package/dist-engine-src/src/sql2/catalog/entity_surface.rs +296 -0
  116. package/dist-engine-src/src/sql2/catalog/mod.rs +15 -0
  117. package/dist-engine-src/src/sql2/catalog/registry.rs +556 -0
  118. package/dist-engine-src/src/sql2/catalog/schema.rs +88 -0
  119. package/dist-engine-src/src/sql2/catalog/surface.rs +41 -0
  120. package/dist-engine-src/src/sql2/change_materialization.rs +122 -0
  121. package/dist-engine-src/src/sql2/context.rs +36 -30
  122. package/dist-engine-src/src/sql2/error.rs +1 -1
  123. package/dist-engine-src/src/sql2/exec/bound_public_write.rs +1593 -0
  124. package/dist-engine-src/src/sql2/exec/datafusion.rs +5266 -0
  125. package/dist-engine-src/src/sql2/exec/fast_write.rs +82 -0
  126. package/dist-engine-src/src/sql2/exec/mod.rs +24 -0
  127. package/dist-engine-src/src/sql2/exec/write.rs +661 -0
  128. package/dist-engine-src/src/sql2/filesystem_planner.rs +72 -77
  129. package/dist-engine-src/src/sql2/filesystem_visibility.rs +21 -21
  130. package/dist-engine-src/src/sql2/history_projection.rs +8 -8
  131. package/dist-engine-src/src/sql2/history_route.rs +35 -31
  132. package/dist-engine-src/src/sql2/mod.rs +28 -23
  133. package/dist-engine-src/src/sql2/optimize/datafusion.rs +1 -0
  134. package/dist-engine-src/src/sql2/optimize/mod.rs +2 -0
  135. package/dist-engine-src/src/sql2/optimize/simple_write.rs +116 -0
  136. package/dist-engine-src/src/sql2/parse/mod.rs +69 -0
  137. package/dist-engine-src/src/sql2/parse/normalize.rs +1 -0
  138. package/dist-engine-src/src/sql2/plan/branch_scope.rs +24 -0
  139. package/dist-engine-src/src/sql2/plan/mod.rs +5 -0
  140. package/dist-engine-src/src/sql2/plan/predicate.rs +22 -0
  141. package/dist-engine-src/src/sql2/plan/write.rs +147 -0
  142. package/dist-engine-src/src/sql2/predicate_typecheck.rs +258 -0
  143. package/dist-engine-src/src/sql2/{version_provider.rs → providers/branch.rs} +218 -214
  144. package/dist-engine-src/src/sql2/{change_provider.rs → providers/change.rs} +156 -42
  145. package/dist-engine-src/src/sql2/{directory_provider.rs → providers/directory.rs} +291 -322
  146. package/dist-engine-src/src/sql2/{directory_history_provider.rs → providers/directory_history.rs} +56 -42
  147. package/dist-engine-src/src/sql2/providers/entity.rs +1484 -0
  148. package/dist-engine-src/src/sql2/{entity_history_provider.rs → providers/entity_history.rs} +43 -31
  149. package/dist-engine-src/src/sql2/{file_provider.rs → providers/file.rs} +323 -316
  150. package/dist-engine-src/src/sql2/{file_history_provider.rs → providers/file_history.rs} +60 -46
  151. package/dist-engine-src/src/sql2/{history_provider.rs → providers/history.rs} +46 -32
  152. package/dist-engine-src/src/sql2/{lix_state_provider.rs → providers/lix_state.rs} +359 -329
  153. package/dist-engine-src/src/sql2/providers/mod.rs +508 -0
  154. package/dist-engine-src/src/sql2/read_only.rs +2 -2
  155. package/dist-engine-src/src/sql2/session.rs +47 -96
  156. package/dist-engine-src/src/sql2/storage/constraints.rs +1 -0
  157. package/dist-engine-src/src/sql2/storage/mod.rs +1 -0
  158. package/dist-engine-src/src/sql2/test_support/differential.rs +712 -0
  159. package/dist-engine-src/src/sql2/test_support/generators.rs +354 -0
  160. package/dist-engine-src/src/sql2/test_support/mod.rs +2 -0
  161. package/dist-engine-src/src/sql2/udfs/{lix_active_version_commit_id.rs → lix_active_branch_commit_id.rs} +7 -7
  162. package/dist-engine-src/src/sql2/udfs/mod.rs +3 -6
  163. package/dist-engine-src/src/sql2/write_normalization.rs +45 -22
  164. package/dist-engine-src/src/storage/conformance.rs +399 -0
  165. package/dist-engine-src/src/storage/context.rs +552 -288
  166. package/dist-engine-src/src/storage/mod.rs +48 -10
  167. package/dist-engine-src/src/storage/point.rs +440 -0
  168. package/dist-engine-src/src/storage/read_scope.rs +43 -64
  169. package/dist-engine-src/src/storage/reader.rs +867 -0
  170. package/dist-engine-src/src/storage/scan.rs +784 -0
  171. package/dist-engine-src/src/storage/spaces.rs +236 -0
  172. package/dist-engine-src/src/storage/stats.rs +80 -0
  173. package/dist-engine-src/src/storage/write_set.rs +962 -0
  174. package/dist-engine-src/src/storage_bench.rs +136 -4828
  175. package/dist-engine-src/src/test_support.rs +360 -138
  176. package/dist-engine-src/src/tracked_state/bench_support.rs +394 -0
  177. package/dist-engine-src/src/tracked_state/codec.rs +155 -1057
  178. package/dist-engine-src/src/tracked_state/commit_root_rebuild.rs +358 -0
  179. package/dist-engine-src/src/tracked_state/context.rs +1927 -993
  180. package/dist-engine-src/src/tracked_state/diff.rs +1715 -261
  181. package/dist-engine-src/src/tracked_state/merge.rs +74 -88
  182. package/dist-engine-src/src/tracked_state/mod.rs +19 -16
  183. package/dist-engine-src/src/tracked_state/{materialization.rs → row_materialization.rs} +50 -178
  184. package/dist-engine-src/src/tracked_state/storage.rs +243 -191
  185. package/dist-engine-src/src/tracked_state/tree.rs +247 -371
  186. package/dist-engine-src/src/tracked_state/types.rs +49 -42
  187. package/dist-engine-src/src/transaction/bench_support.rs +407 -0
  188. package/dist-engine-src/src/transaction/commit.rs +821 -713
  189. package/dist-engine-src/src/transaction/context.rs +705 -600
  190. package/dist-engine-src/src/transaction/mod.rs +13 -2
  191. package/dist-engine-src/src/transaction/normalization.rs +63 -76
  192. package/dist-engine-src/src/transaction/prep.rs +13 -13
  193. package/dist-engine-src/src/transaction/schema_resolver.rs +19 -5
  194. package/dist-engine-src/src/transaction/staging.rs +228 -434
  195. package/dist-engine-src/src/transaction/types.rs +41 -98
  196. package/dist-engine-src/src/transaction/validation.rs +382 -446
  197. package/dist-engine-src/src/untracked_state/codec.rs +337 -29
  198. package/dist-engine-src/src/untracked_state/context.rs +7 -7
  199. package/dist-engine-src/src/untracked_state/materialization.rs +2 -2
  200. package/dist-engine-src/src/untracked_state/mod.rs +1 -1
  201. package/dist-engine-src/src/untracked_state/storage.rs +659 -157
  202. package/dist-engine-src/src/untracked_state/types.rs +21 -21
  203. package/package.json +71 -68
  204. package/dist-engine-src/src/backend/kv.rs +0 -358
  205. package/dist-engine-src/src/backend/testing.rs +0 -658
  206. package/dist-engine-src/src/commit_store/codec.rs +0 -887
  207. package/dist-engine-src/src/commit_store/context.rs +0 -944
  208. package/dist-engine-src/src/commit_store/materialization.rs +0 -84
  209. package/dist-engine-src/src/commit_store/mod.rs +0 -16
  210. package/dist-engine-src/src/commit_store/storage.rs +0 -600
  211. package/dist-engine-src/src/commit_store/types.rs +0 -215
  212. package/dist-engine-src/src/schema/builtin/lix_version_descriptor.json +0 -34
  213. package/dist-engine-src/src/schema/builtin/lix_version_ref.json +0 -48
  214. package/dist-engine-src/src/session/create_version.rs +0 -88
  215. package/dist-engine-src/src/session/merge/apply.rs +0 -23
  216. package/dist-engine-src/src/session/optimization9_sql2_bench.rs +0 -100
  217. package/dist-engine-src/src/session/switch_version.rs +0 -110
  218. package/dist-engine-src/src/sql2/entity_provider.rs +0 -3211
  219. package/dist-engine-src/src/sql2/execute.rs +0 -3533
  220. package/dist-engine-src/src/sql2/public_bind/assignment.rs +0 -46
  221. package/dist-engine-src/src/sql2/public_bind/capability.rs +0 -41
  222. package/dist-engine-src/src/sql2/public_bind/dml.rs +0 -172
  223. package/dist-engine-src/src/sql2/public_bind/mod.rs +0 -26
  224. package/dist-engine-src/src/sql2/public_bind/table.rs +0 -168
  225. package/dist-engine-src/src/sql2/version_scope.rs +0 -394
  226. package/dist-engine-src/src/storage/types.rs +0 -501
  227. package/dist-engine-src/src/tracked_state/by_file_index.rs +0 -98
  228. package/dist-engine-src/src/tracked_state/materializer.rs +0 -488
  229. package/dist-engine-src/src/transaction/live_state_overlay.rs +0 -35
  230. package/dist-engine-src/src/version/lifecycle.rs +0 -221
  231. package/dist-engine-src/src/version/mod.rs +0 -13
  232. package/dist-engine-src/src/version/refs.rs +0 -330
  233. package/dist-engine-src/src/version/stage_rows.rs +0 -67
  234. package/dist-engine-src/src/version/types.rs +0 -21
@@ -1,887 +0,0 @@
1
- use crate::commit_store::{
2
- Change, ChangeLocator, ChangeLocatorRef, ChangeRef, Commit, StoredCommitRef,
3
- };
4
- use crate::entity_identity::EntityIdentity;
5
- use crate::json_store::JsonRef;
6
- use crate::LixError;
7
-
8
- const COMMIT_MAGIC: &[u8; 5] = b"LXCM1";
9
- const CHANGE_MAGIC: &[u8; 5] = b"LXCH2";
10
- const CHANGE_PACK_MAGIC: &[u8; 5] = b"LXCP3";
11
- const MEMBERSHIP_PACK_MAGIC: &[u8; 5] = b"LXMP1";
12
- const CHANGE_ID_FULL: u8 = 0;
13
- const CHANGE_ID_COMMIT_SUFFIX: u8 = 1;
14
-
15
- pub(crate) fn encode_commit_ref(commit: StoredCommitRef<'_>) -> Result<Vec<u8>, LixError> {
16
- let mut bytes = Vec::new();
17
- bytes.extend_from_slice(COMMIT_MAGIC);
18
- write_str(&mut bytes, commit.id)?;
19
- write_str(&mut bytes, commit.change_id)?;
20
- write_strs(&mut bytes, commit.parent_ids.iter().map(String::as_str))?;
21
- write_strs(
22
- &mut bytes,
23
- commit.author_account_ids.iter().map(String::as_str),
24
- )?;
25
- write_str(&mut bytes, commit.created_at)?;
26
- bytes.extend_from_slice(&commit.change_pack_count.to_le_bytes());
27
- bytes.extend_from_slice(&commit.membership_pack_count.to_le_bytes());
28
- Ok(bytes)
29
- }
30
-
31
- pub(crate) fn decode_commit(bytes: &[u8]) -> Result<Commit, LixError> {
32
- let mut cursor = ByteCursor::new(bytes);
33
- cursor.expect_magic(COMMIT_MAGIC, "commit")?;
34
- let id = cursor.read_string("id")?;
35
- let change_id = cursor.read_string("change_id")?;
36
- let parent_ids = cursor.read_strings("parent_ids")?;
37
- let author_account_ids = cursor.read_strings("author_account_ids")?;
38
- let created_at = cursor.read_string("created_at")?;
39
- let change_pack_count = cursor.read_u32("change_pack_count")?;
40
- let membership_pack_count = cursor.read_u32("membership_pack_count")?;
41
- cursor.expect_end("commit")?;
42
- Ok(Commit {
43
- id,
44
- change_id,
45
- parent_ids,
46
- author_account_ids,
47
- created_at,
48
- change_pack_count,
49
- membership_pack_count,
50
- })
51
- }
52
-
53
- pub(crate) fn encode_change_ref(change: ChangeRef<'_>) -> Result<Vec<u8>, LixError> {
54
- let mut bytes = Vec::new();
55
- write_change_ref(&mut bytes, change)?;
56
- Ok(bytes)
57
- }
58
-
59
- fn write_change_ref(bytes: &mut Vec<u8>, change: ChangeRef<'_>) -> Result<(), LixError> {
60
- let entity_id = change.entity_id.as_json_array_text().map_err(|error| {
61
- LixError::unknown(format!(
62
- "failed to encode commit-store change entity identity: {error}"
63
- ))
64
- })?;
65
-
66
- bytes.extend_from_slice(CHANGE_MAGIC);
67
- write_str(bytes, change.id)?;
68
- write_str(bytes, &entity_id)?;
69
- write_str(bytes, change.schema_key)?;
70
- write_optional_str(bytes, change.file_id)?;
71
- write_optional_json_ref(bytes, change.snapshot_ref);
72
- write_optional_json_ref(bytes, change.metadata_ref);
73
- write_str(bytes, change.created_at)
74
- }
75
-
76
- pub(crate) fn decode_change(bytes: &[u8]) -> Result<Change, LixError> {
77
- let mut cursor = ByteCursor::new(bytes);
78
- cursor.expect_magic(CHANGE_MAGIC, "change")?;
79
- let id = cursor.read_string("id")?;
80
- let entity_id = cursor.read_string("entity_id")?;
81
- let entity_id = EntityIdentity::from_json_array_text(&entity_id).map_err(|error| {
82
- LixError::unknown(format!(
83
- "failed to decode commit-store change entity identity: {error}"
84
- ))
85
- })?;
86
- let schema_key = cursor.read_string("schema_key")?;
87
- let file_id = cursor.read_optional_string("file_id")?;
88
- let snapshot_ref = cursor.read_optional_json_ref("snapshot_ref")?;
89
- let metadata_ref = cursor.read_optional_json_ref("metadata_ref")?;
90
- let created_at = cursor.read_string("created_at")?;
91
- cursor.expect_end("change")?;
92
- Ok(Change {
93
- id,
94
- entity_id,
95
- schema_key,
96
- file_id,
97
- snapshot_ref,
98
- metadata_ref,
99
- created_at,
100
- })
101
- }
102
-
103
- pub(crate) fn encode_change_pack(
104
- commit_id: &str,
105
- pack_id: u32,
106
- changes: &[ChangeRef<'_>],
107
- ) -> Result<Vec<u8>, LixError> {
108
- let mut bytes = Vec::new();
109
- bytes.extend_from_slice(CHANGE_PACK_MAGIC);
110
- write_var_str(&mut bytes, commit_id, "change pack commit_id")?;
111
- bytes.extend_from_slice(&pack_id.to_le_bytes());
112
- let (shapes, change_shape_indexes) = change_shapes(changes);
113
- write_var_len(&mut bytes, shapes.len(), "change pack shapes")?;
114
- for shape in &shapes {
115
- write_var_str(&mut bytes, shape.schema_key, "schema_key")?;
116
- write_optional_var_str(&mut bytes, shape.file_id, "file_id")?;
117
- }
118
- write_var_len(&mut bytes, changes.len(), "change pack changes")?;
119
- for (change, shape_index) in changes.iter().copied().zip(change_shape_indexes) {
120
- write_var_change_id(&mut bytes, commit_id, change.id)?;
121
- write_var_entity_identity(&mut bytes, change.entity_id)?;
122
- write_var_len(&mut bytes, shape_index, "change shape index")?;
123
- write_optional_json_ref(&mut bytes, change.snapshot_ref);
124
- write_optional_json_ref(&mut bytes, change.metadata_ref);
125
- write_var_str(&mut bytes, change.created_at, "created_at")?;
126
- }
127
- Ok(bytes)
128
- }
129
-
130
- pub(crate) fn decode_change_pack(bytes: &[u8]) -> Result<(String, u32, Vec<Change>), LixError> {
131
- let mut cursor = ByteCursor::new(bytes);
132
- cursor.expect_magic(CHANGE_PACK_MAGIC, "change pack")?;
133
- let commit_id = cursor.read_var_string("commit_id")?;
134
- let pack_id = cursor.read_u32("pack_id")?;
135
- let shape_count = cursor.read_var_usize("shape_count")?;
136
- let mut shapes = Vec::with_capacity(shape_count);
137
- for _ in 0..shape_count {
138
- shapes.push(ChangeShape {
139
- schema_key: cursor.read_var_string("schema_key")?,
140
- file_id: cursor.read_optional_var_string("file_id")?,
141
- });
142
- }
143
- let change_count = cursor.read_var_usize("change_count")?;
144
- let mut changes = Vec::with_capacity(change_count);
145
- for _ in 0..change_count {
146
- let id = cursor.read_var_change_id(&commit_id)?;
147
- let entity_id = cursor.read_var_entity_identity()?;
148
- let shape_index = cursor.read_var_usize("shape_index")?;
149
- let shape = shapes.get(shape_index).ok_or_else(|| {
150
- LixError::new(
151
- LixError::CODE_INTERNAL_ERROR,
152
- format!("failed to decode commit-store change pack: shape index {shape_index} is out of bounds"),
153
- )
154
- })?;
155
- let snapshot_ref = cursor.read_optional_json_ref("snapshot_ref")?;
156
- let metadata_ref = cursor.read_optional_json_ref("metadata_ref")?;
157
- let created_at = cursor.read_var_string("created_at")?;
158
- changes.push(Change {
159
- id,
160
- entity_id,
161
- schema_key: shape.schema_key.clone(),
162
- file_id: shape.file_id.clone(),
163
- snapshot_ref,
164
- metadata_ref,
165
- created_at,
166
- });
167
- }
168
- cursor.expect_end("change pack")?;
169
- Ok((commit_id, pack_id, changes))
170
- }
171
-
172
- #[derive(Debug, Clone, Copy, PartialEq, Eq)]
173
- struct ChangeShapeRef<'a> {
174
- schema_key: &'a str,
175
- file_id: Option<&'a str>,
176
- }
177
-
178
- #[derive(Debug, Clone, PartialEq, Eq)]
179
- struct ChangeShape {
180
- schema_key: String,
181
- file_id: Option<String>,
182
- }
183
-
184
- fn change_shapes<'a>(changes: &'a [ChangeRef<'a>]) -> (Vec<ChangeShapeRef<'a>>, Vec<usize>) {
185
- let mut shapes = Vec::new();
186
- let mut shape_indexes = Vec::with_capacity(changes.len());
187
- for change in changes {
188
- let shape = ChangeShapeRef {
189
- schema_key: change.schema_key,
190
- file_id: change.file_id,
191
- };
192
- let shape_index = match shapes.iter().position(|candidate| *candidate == shape) {
193
- Some(shape_index) => shape_index,
194
- None => {
195
- let shape_index = shapes.len();
196
- shapes.push(shape);
197
- shape_index
198
- }
199
- };
200
- shape_indexes.push(shape_index);
201
- }
202
- (shapes, shape_indexes)
203
- }
204
-
205
- pub(crate) fn encode_membership_pack<'a>(
206
- commit_id: &str,
207
- pack_id: u32,
208
- members: impl IntoIterator<Item = ChangeLocatorRef<'a>>,
209
- ) -> Result<Vec<u8>, LixError> {
210
- let members = members.into_iter().collect::<Vec<_>>();
211
- let mut bytes = Vec::new();
212
- bytes.extend_from_slice(MEMBERSHIP_PACK_MAGIC);
213
- write_str(&mut bytes, commit_id)?;
214
- bytes.extend_from_slice(&pack_id.to_le_bytes());
215
- write_len(&mut bytes, members.len(), "membership pack members")?;
216
- for member in members {
217
- encode_locator(&mut bytes, member)?;
218
- }
219
- Ok(bytes)
220
- }
221
-
222
- pub(crate) fn decode_membership_pack(
223
- bytes: &[u8],
224
- ) -> Result<(String, u32, Vec<ChangeLocator>), LixError> {
225
- let mut cursor = ByteCursor::new(bytes);
226
- cursor.expect_magic(MEMBERSHIP_PACK_MAGIC, "membership pack")?;
227
- let commit_id = cursor.read_string("commit_id")?;
228
- let pack_id = cursor.read_u32("pack_id")?;
229
- let member_count = cursor.read_u32("member_count")? as usize;
230
- let mut members = Vec::with_capacity(member_count);
231
- for _ in 0..member_count {
232
- members.push(decode_locator(&mut cursor)?);
233
- }
234
- cursor.expect_end("membership pack")?;
235
- Ok((commit_id, pack_id, members))
236
- }
237
-
238
- fn encode_locator(bytes: &mut Vec<u8>, locator: ChangeLocatorRef<'_>) -> Result<(), LixError> {
239
- write_str(bytes, locator.source_commit_id)?;
240
- bytes.extend_from_slice(&locator.source_pack_id.to_le_bytes());
241
- bytes.extend_from_slice(&locator.source_ordinal.to_le_bytes());
242
- write_str(bytes, locator.change_id)
243
- }
244
-
245
- fn decode_locator(cursor: &mut ByteCursor<'_>) -> Result<ChangeLocator, LixError> {
246
- Ok(ChangeLocator {
247
- source_commit_id: cursor.read_string("source_commit_id")?,
248
- source_pack_id: cursor.read_u32("source_pack_id")?,
249
- source_ordinal: cursor.read_u32("source_ordinal")?,
250
- change_id: cursor.read_string("change_id")?,
251
- })
252
- }
253
-
254
- fn write_str(bytes: &mut Vec<u8>, value: &str) -> Result<(), LixError> {
255
- let len = u32::try_from(value.len()).map_err(|_| {
256
- LixError::new(
257
- LixError::CODE_INTERNAL_ERROR,
258
- "commit-store string field exceeds u32 length",
259
- )
260
- })?;
261
- bytes.extend_from_slice(&len.to_le_bytes());
262
- bytes.extend_from_slice(value.as_bytes());
263
- Ok(())
264
- }
265
-
266
- fn write_optional_str(bytes: &mut Vec<u8>, value: Option<&str>) -> Result<(), LixError> {
267
- match value {
268
- Some(value) => {
269
- bytes.push(1);
270
- write_str(bytes, value)?;
271
- }
272
- None => bytes.push(0),
273
- }
274
- Ok(())
275
- }
276
-
277
- fn write_optional_json_ref(bytes: &mut Vec<u8>, value: Option<&JsonRef>) {
278
- match value {
279
- Some(value) => {
280
- bytes.push(1);
281
- bytes.extend_from_slice(value.as_hash_bytes());
282
- }
283
- None => bytes.push(0),
284
- }
285
- }
286
-
287
- fn write_len(bytes: &mut Vec<u8>, len: usize, field: &str) -> Result<(), LixError> {
288
- let len = u32::try_from(len).map_err(|_| {
289
- LixError::new(
290
- LixError::CODE_INTERNAL_ERROR,
291
- format!("commit-store {field} exceeds u32 length"),
292
- )
293
- })?;
294
- bytes.extend_from_slice(&len.to_le_bytes());
295
- Ok(())
296
- }
297
-
298
- fn write_var_len(bytes: &mut Vec<u8>, len: usize, field: &str) -> Result<(), LixError> {
299
- let mut value = u32::try_from(len).map_err(|_| {
300
- LixError::new(
301
- LixError::CODE_INTERNAL_ERROR,
302
- format!("commit-store {field} exceeds u32 length"),
303
- )
304
- })?;
305
- while value >= 0x80 {
306
- bytes.push((value as u8 & 0x7f) | 0x80);
307
- value >>= 7;
308
- }
309
- bytes.push(value as u8);
310
- Ok(())
311
- }
312
-
313
- fn write_var_str(bytes: &mut Vec<u8>, value: &str, field: &str) -> Result<(), LixError> {
314
- write_var_len(bytes, value.len(), field)?;
315
- bytes.extend_from_slice(value.as_bytes());
316
- Ok(())
317
- }
318
-
319
- fn write_optional_var_str(
320
- bytes: &mut Vec<u8>,
321
- value: Option<&str>,
322
- field: &str,
323
- ) -> Result<(), LixError> {
324
- match value {
325
- Some(value) => {
326
- bytes.push(1);
327
- write_var_str(bytes, value, field)?;
328
- }
329
- None => bytes.push(0),
330
- }
331
- Ok(())
332
- }
333
-
334
- fn write_change_id(bytes: &mut Vec<u8>, commit_id: &str, change_id: &str) -> Result<(), LixError> {
335
- if let Some(suffix) = change_id.strip_prefix(commit_id) {
336
- bytes.push(CHANGE_ID_COMMIT_SUFFIX);
337
- write_str(bytes, suffix)
338
- } else {
339
- bytes.push(CHANGE_ID_FULL);
340
- write_str(bytes, change_id)
341
- }
342
- }
343
-
344
- fn write_var_change_id(
345
- bytes: &mut Vec<u8>,
346
- commit_id: &str,
347
- change_id: &str,
348
- ) -> Result<(), LixError> {
349
- if let Some(suffix) = change_id.strip_prefix(commit_id) {
350
- bytes.push(CHANGE_ID_COMMIT_SUFFIX);
351
- write_var_str(bytes, suffix, "change_id")
352
- } else {
353
- bytes.push(CHANGE_ID_FULL);
354
- write_var_str(bytes, change_id, "change_id")
355
- }
356
- }
357
-
358
- fn write_entity_identity(bytes: &mut Vec<u8>, identity: &EntityIdentity) -> Result<(), LixError> {
359
- write_len(
360
- bytes,
361
- identity.parts.len(),
362
- "commit-store entity identity parts",
363
- )?;
364
- for part in &identity.parts {
365
- write_str(bytes, part)?;
366
- }
367
- Ok(())
368
- }
369
-
370
- fn write_var_entity_identity(
371
- bytes: &mut Vec<u8>,
372
- identity: &EntityIdentity,
373
- ) -> Result<(), LixError> {
374
- write_var_len(
375
- bytes,
376
- identity.parts.len(),
377
- "commit-store entity identity parts",
378
- )?;
379
- for part in &identity.parts {
380
- write_var_str(bytes, part, "entity identity part")?;
381
- }
382
- Ok(())
383
- }
384
-
385
- fn write_strs<'a>(
386
- bytes: &mut Vec<u8>,
387
- values: impl IntoIterator<Item = &'a str>,
388
- ) -> Result<(), LixError> {
389
- let values = values.into_iter().collect::<Vec<_>>();
390
- let len = u32::try_from(values.len()).map_err(|_| {
391
- LixError::new(
392
- LixError::CODE_INTERNAL_ERROR,
393
- "commit-store string vector field exceeds u32 length",
394
- )
395
- })?;
396
- bytes.extend_from_slice(&len.to_le_bytes());
397
- for value in values {
398
- write_str(bytes, value)?;
399
- }
400
- Ok(())
401
- }
402
-
403
- struct ByteCursor<'a> {
404
- bytes: &'a [u8],
405
- offset: usize,
406
- }
407
-
408
- impl<'a> ByteCursor<'a> {
409
- fn new(bytes: &'a [u8]) -> Self {
410
- Self { bytes, offset: 0 }
411
- }
412
-
413
- fn expect_magic(&mut self, magic: &[u8], label: &str) -> Result<(), LixError> {
414
- if self.bytes.len() < magic.len() || &self.bytes[..magic.len()] != magic {
415
- return Err(LixError::new(
416
- LixError::CODE_INTERNAL_ERROR,
417
- format!("failed to decode commit-store {label}: invalid magic"),
418
- ));
419
- }
420
- self.offset = magic.len();
421
- Ok(())
422
- }
423
-
424
- fn read_string(&mut self, field: &str) -> Result<String, LixError> {
425
- let len = self.read_u32(field)? as usize;
426
- let end = self.offset.checked_add(len).ok_or_else(|| {
427
- LixError::new(
428
- LixError::CODE_INTERNAL_ERROR,
429
- format!("failed to decode commit-store field `{field}`: length overflow"),
430
- )
431
- })?;
432
- let bytes = self.bytes.get(self.offset..end).ok_or_else(|| {
433
- LixError::new(
434
- LixError::CODE_INTERNAL_ERROR,
435
- format!("failed to decode commit-store field `{field}`: truncated string"),
436
- )
437
- })?;
438
- self.offset = end;
439
- String::from_utf8(bytes.to_vec()).map_err(|error| {
440
- LixError::new(
441
- LixError::CODE_INTERNAL_ERROR,
442
- format!("failed to decode commit-store field `{field}` as UTF-8: {error}"),
443
- )
444
- })
445
- }
446
-
447
- fn read_strings(&mut self, field: &str) -> Result<Vec<String>, LixError> {
448
- let count = self.read_u32(field)? as usize;
449
- let mut values = Vec::with_capacity(count);
450
- for _ in 0..count {
451
- values.push(self.read_string(field)?);
452
- }
453
- Ok(values)
454
- }
455
-
456
- fn read_optional_string(&mut self, field: &str) -> Result<Option<String>, LixError> {
457
- match self.read_u8(field)? {
458
- 0 => Ok(None),
459
- 1 => self.read_string(field).map(Some),
460
- tag => Err(LixError::new(
461
- LixError::CODE_INTERNAL_ERROR,
462
- format!("failed to decode commit-store field `{field}`: invalid option tag {tag}"),
463
- )),
464
- }
465
- }
466
-
467
- fn read_optional_json_ref(&mut self, field: &str) -> Result<Option<JsonRef>, LixError> {
468
- match self.read_u8(field)? {
469
- 0 => Ok(None),
470
- 1 => {
471
- let end = self.offset.checked_add(32).ok_or_else(|| {
472
- LixError::new(
473
- LixError::CODE_INTERNAL_ERROR,
474
- format!("failed to decode commit-store field `{field}`: offset overflow"),
475
- )
476
- })?;
477
- let bytes = self.bytes.get(self.offset..end).ok_or_else(|| {
478
- LixError::new(
479
- LixError::CODE_INTERNAL_ERROR,
480
- format!("failed to decode commit-store field `{field}`: truncated ref"),
481
- )
482
- })?;
483
- self.offset = end;
484
- let hash = <[u8; 32]>::try_from(bytes).expect("json ref length was checked");
485
- Ok(Some(JsonRef::from_hash_bytes(hash)))
486
- }
487
- tag => Err(LixError::new(
488
- LixError::CODE_INTERNAL_ERROR,
489
- format!("failed to decode commit-store field `{field}`: invalid option tag {tag}"),
490
- )),
491
- }
492
- }
493
-
494
- fn read_u8(&mut self, field: &str) -> Result<u8, LixError> {
495
- let byte = self.bytes.get(self.offset).copied().ok_or_else(|| {
496
- LixError::new(
497
- LixError::CODE_INTERNAL_ERROR,
498
- format!("failed to decode commit-store field `{field}`: truncated u8"),
499
- )
500
- })?;
501
- self.offset += 1;
502
- Ok(byte)
503
- }
504
-
505
- fn read_u32(&mut self, field: &str) -> Result<u32, LixError> {
506
- let end = self.offset.checked_add(4).ok_or_else(|| {
507
- LixError::new(
508
- LixError::CODE_INTERNAL_ERROR,
509
- format!("failed to decode commit-store field `{field}`: offset overflow"),
510
- )
511
- })?;
512
- let bytes = self.bytes.get(self.offset..end).ok_or_else(|| {
513
- LixError::new(
514
- LixError::CODE_INTERNAL_ERROR,
515
- format!("failed to decode commit-store field `{field}`: truncated u32"),
516
- )
517
- })?;
518
- self.offset = end;
519
- Ok(u32::from_le_bytes(
520
- bytes
521
- .try_into()
522
- .expect("slice length was checked before u32 decode"),
523
- ))
524
- }
525
-
526
- fn read_var_usize(&mut self, field: &str) -> Result<usize, LixError> {
527
- let mut value = 0u32;
528
- let mut shift = 0u32;
529
- for byte_index in 0..5 {
530
- let byte = self.read_u8(field)?;
531
- if shift == 28 && (byte & 0x80 != 0 || byte & 0x70 != 0) {
532
- return Err(LixError::new(
533
- LixError::CODE_INTERNAL_ERROR,
534
- format!("failed to decode commit-store field `{field}`: varint exceeds u32"),
535
- ));
536
- }
537
- if byte_index > 0 && byte & 0x80 == 0 && byte == 0 {
538
- return Err(LixError::new(
539
- LixError::CODE_INTERNAL_ERROR,
540
- format!("failed to decode commit-store field `{field}`: non-canonical varint"),
541
- ));
542
- }
543
- value |= ((byte & 0x7f) as u32) << shift;
544
- if byte & 0x80 == 0 {
545
- return Ok(value as usize);
546
- }
547
- shift += 7;
548
- }
549
- Err(LixError::new(
550
- LixError::CODE_INTERNAL_ERROR,
551
- format!("failed to decode commit-store field `{field}`: varint exceeds u32"),
552
- ))
553
- }
554
-
555
- fn read_var_string(&mut self, field: &str) -> Result<String, LixError> {
556
- let len = self.read_var_usize(field)?;
557
- let end = self.offset.checked_add(len).ok_or_else(|| {
558
- LixError::new(
559
- LixError::CODE_INTERNAL_ERROR,
560
- format!("failed to decode commit-store field `{field}`: length overflow"),
561
- )
562
- })?;
563
- let bytes = self.bytes.get(self.offset..end).ok_or_else(|| {
564
- LixError::new(
565
- LixError::CODE_INTERNAL_ERROR,
566
- format!("failed to decode commit-store field `{field}`: truncated string"),
567
- )
568
- })?;
569
- self.offset = end;
570
- String::from_utf8(bytes.to_vec()).map_err(|error| {
571
- LixError::new(
572
- LixError::CODE_INTERNAL_ERROR,
573
- format!("failed to decode commit-store field `{field}` as UTF-8: {error}"),
574
- )
575
- })
576
- }
577
-
578
- fn read_optional_var_string(&mut self, field: &str) -> Result<Option<String>, LixError> {
579
- match self.read_u8(field)? {
580
- 0 => Ok(None),
581
- 1 => self.read_var_string(field).map(Some),
582
- tag => Err(LixError::new(
583
- LixError::CODE_INTERNAL_ERROR,
584
- format!("failed to decode commit-store field `{field}`: invalid option tag {tag}"),
585
- )),
586
- }
587
- }
588
-
589
- fn read_change_id(&mut self, commit_id: &str) -> Result<String, LixError> {
590
- let tag = self.read_u8("change_id tag")?;
591
- let value = self.read_string("change_id")?;
592
- match tag {
593
- CHANGE_ID_FULL => Ok(value),
594
- CHANGE_ID_COMMIT_SUFFIX => Ok(format!("{commit_id}{value}")),
595
- tag => Err(LixError::new(
596
- LixError::CODE_INTERNAL_ERROR,
597
- format!("failed to decode commit-store field `change_id`: invalid tag {tag}"),
598
- )),
599
- }
600
- }
601
-
602
- fn read_var_change_id(&mut self, commit_id: &str) -> Result<String, LixError> {
603
- let tag = self.read_u8("change_id tag")?;
604
- let value = self.read_var_string("change_id")?;
605
- match tag {
606
- CHANGE_ID_FULL => Ok(value),
607
- CHANGE_ID_COMMIT_SUFFIX => Ok(format!("{commit_id}{value}")),
608
- tag => Err(LixError::new(
609
- LixError::CODE_INTERNAL_ERROR,
610
- format!("failed to decode commit-store field `change_id`: invalid tag {tag}"),
611
- )),
612
- }
613
- }
614
-
615
- fn read_entity_identity(&mut self) -> Result<EntityIdentity, LixError> {
616
- let count = self.read_u32("entity identity part count")? as usize;
617
- let mut parts = Vec::with_capacity(count);
618
- for _ in 0..count {
619
- parts.push(self.read_string("entity identity part")?);
620
- }
621
- if parts.is_empty() {
622
- return Err(LixError::new(
623
- LixError::CODE_INTERNAL_ERROR,
624
- "failed to decode commit-store entity identity: empty identity",
625
- ));
626
- }
627
- Ok(EntityIdentity { parts })
628
- }
629
-
630
- fn read_var_entity_identity(&mut self) -> Result<EntityIdentity, LixError> {
631
- let count = self.read_var_usize("entity identity part count")?;
632
- let mut parts = Vec::with_capacity(count);
633
- for _ in 0..count {
634
- parts.push(self.read_var_string("entity identity part")?);
635
- }
636
- if parts.is_empty() {
637
- return Err(LixError::new(
638
- LixError::CODE_INTERNAL_ERROR,
639
- "failed to decode commit-store entity identity: empty identity",
640
- ));
641
- }
642
- Ok(EntityIdentity { parts })
643
- }
644
-
645
- fn expect_end(&self, label: &str) -> Result<(), LixError> {
646
- if self.offset != self.bytes.len() {
647
- return Err(LixError::new(
648
- LixError::CODE_INTERNAL_ERROR,
649
- format!("failed to decode commit-store {label}: trailing bytes"),
650
- ));
651
- }
652
- Ok(())
653
- }
654
- }
655
-
656
- #[cfg(test)]
657
- mod tests {
658
- use super::*;
659
-
660
- #[test]
661
- fn commit_codec_roundtrips() {
662
- let commit = Commit {
663
- id: "commit-1".to_string(),
664
- change_id: "commit-change-1".to_string(),
665
- parent_ids: vec!["parent-1".to_string(), "parent-2".to_string()],
666
- author_account_ids: vec!["author-1".to_string()],
667
- created_at: "2026-01-01T00:00:00Z".to_string(),
668
- change_pack_count: 2,
669
- membership_pack_count: 1,
670
- };
671
-
672
- let encoded = encode_commit_ref(commit.as_ref()).expect("commit should encode");
673
- let decoded = decode_commit(&encoded).expect("commit should decode");
674
-
675
- assert_eq!(decoded, commit);
676
- }
677
-
678
- #[test]
679
- fn change_codec_roundtrips() {
680
- let change = Change {
681
- id: "change-1".to_string(),
682
- entity_id: EntityIdentity::single("entity-1"),
683
- schema_key: "test_schema".to_string(),
684
- file_id: Some("file-1".to_string()),
685
- snapshot_ref: Some(JsonRef::from_hash_bytes([1; 32])),
686
- metadata_ref: Some(JsonRef::from_hash_bytes([2; 32])),
687
- created_at: "2026-01-01T00:00:00Z".to_string(),
688
- };
689
-
690
- let encoded = encode_change_ref(change.as_ref()).expect("change should encode");
691
- let decoded = decode_change(&encoded).expect("change should decode");
692
-
693
- assert_eq!(decoded, change);
694
- }
695
-
696
- #[test]
697
- fn change_codec_roundtrips_empty_optionals() {
698
- let change = Change {
699
- id: "change-1".to_string(),
700
- entity_id: EntityIdentity::single("entity-1"),
701
- schema_key: "test_schema".to_string(),
702
- file_id: None,
703
- snapshot_ref: None,
704
- metadata_ref: None,
705
- created_at: "2026-01-01T00:00:00Z".to_string(),
706
- };
707
-
708
- let encoded = encode_change_ref(change.as_ref()).expect("change should encode");
709
- let decoded = decode_change(&encoded).expect("change should decode");
710
-
711
- assert_eq!(decoded, change);
712
- }
713
-
714
- #[test]
715
- fn change_pack_compacts_shared_shape_and_commit_id_prefix() {
716
- let changes = [
717
- Change {
718
- id: "commit-1:change-1".to_string(),
719
- entity_id: EntityIdentity::single("entity-1"),
720
- schema_key: "test_schema".to_string(),
721
- file_id: Some("file-1".to_string()),
722
- snapshot_ref: Some(JsonRef::from_hash_bytes([1; 32])),
723
- metadata_ref: None,
724
- created_at: "2026-01-01T00:00:00Z".to_string(),
725
- },
726
- Change {
727
- id: "external-change".to_string(),
728
- entity_id: EntityIdentity::single("entity-2"),
729
- schema_key: "test_schema".to_string(),
730
- file_id: Some("file-1".to_string()),
731
- snapshot_ref: None,
732
- metadata_ref: Some(JsonRef::from_hash_bytes([2; 32])),
733
- created_at: "2026-01-02T00:00:00Z".to_string(),
734
- },
735
- ];
736
-
737
- let encoded = encode_change_pack(
738
- "commit-1",
739
- 7,
740
- &changes.iter().map(Change::as_ref).collect::<Vec<_>>(),
741
- )
742
- .expect("pack should encode");
743
- let (commit_id, pack_id, decoded) =
744
- decode_change_pack(&encoded).expect("pack should decode");
745
-
746
- assert_eq!(commit_id, "commit-1");
747
- assert_eq!(pack_id, 7);
748
- assert_eq!(decoded, changes);
749
-
750
- let mut cursor = ByteCursor::new(&encoded);
751
- cursor
752
- .expect_magic(CHANGE_PACK_MAGIC, "change pack")
753
- .unwrap();
754
- assert_eq!(cursor.read_var_string("commit_id").unwrap(), "commit-1");
755
- assert_eq!(cursor.read_u32("pack_id").unwrap(), 7);
756
- assert_eq!(cursor.read_var_usize("shape_count").unwrap(), 1);
757
- assert_eq!(cursor.read_var_string("schema_key").unwrap(), "test_schema");
758
- assert_eq!(
759
- cursor
760
- .read_optional_var_string("file_id")
761
- .unwrap()
762
- .as_deref(),
763
- Some("file-1")
764
- );
765
- assert_eq!(cursor.read_var_usize("change_count").unwrap(), 2);
766
- assert_eq!(
767
- cursor.read_u8("change_id tag").unwrap(),
768
- CHANGE_ID_COMMIT_SUFFIX
769
- );
770
- assert_eq!(cursor.read_var_string("change_id").unwrap(), ":change-1");
771
- }
772
-
773
- #[test]
774
- fn change_pack_rejects_overlong_varint() {
775
- let mut encoded = Vec::new();
776
- encoded.extend_from_slice(CHANGE_PACK_MAGIC);
777
- encoded.extend_from_slice(&[0x80, 0x80, 0x80, 0x80, 0x80]);
778
-
779
- let error = decode_change_pack(&encoded).expect_err("overlong varint should reject");
780
- assert!(
781
- error.to_string().contains("varint exceeds u32"),
782
- "error should mention overlong varint: {error}"
783
- );
784
- }
785
-
786
- #[test]
787
- fn change_pack_rejects_varint_above_u32() {
788
- let mut encoded = Vec::new();
789
- encoded.extend_from_slice(CHANGE_PACK_MAGIC);
790
- encoded.extend_from_slice(&[0xff, 0xff, 0xff, 0xff, 0x1f]);
791
-
792
- let error = decode_change_pack(&encoded).expect_err("too-large varint should reject");
793
- assert!(
794
- error.to_string().contains("varint exceeds u32"),
795
- "error should mention oversized varint: {error}"
796
- );
797
- }
798
-
799
- #[test]
800
- fn change_pack_rejects_non_canonical_varint() {
801
- let mut encoded = Vec::new();
802
- encoded.extend_from_slice(CHANGE_PACK_MAGIC);
803
- encoded.extend_from_slice(&[0x80, 0x00]);
804
-
805
- let error = decode_change_pack(&encoded).expect_err("non-canonical varint should reject");
806
- assert!(
807
- error.to_string().contains("non-canonical varint"),
808
- "error should mention non-canonical varint: {error}"
809
- );
810
- }
811
-
812
- #[test]
813
- fn change_codec_rejects_invalid_optional_tag() {
814
- let change = Change {
815
- id: "change-1".to_string(),
816
- entity_id: EntityIdentity::single("entity-1"),
817
- schema_key: "test_schema".to_string(),
818
- file_id: None,
819
- snapshot_ref: None,
820
- metadata_ref: None,
821
- created_at: "2026-01-01T00:00:00Z".to_string(),
822
- };
823
- let mut encoded = encode_change_ref(change.as_ref()).expect("change should encode");
824
- let mut cursor = ByteCursor::new(&encoded);
825
- cursor.expect_magic(CHANGE_MAGIC, "change").unwrap();
826
- cursor.read_string("id").unwrap();
827
- cursor.read_string("entity_id").unwrap();
828
- cursor.read_string("schema_key").unwrap();
829
- let file_tag_offset = cursor.offset;
830
- encoded[file_tag_offset] = 2;
831
-
832
- let error = decode_change(&encoded).expect_err("invalid optional tag should fail");
833
- assert!(
834
- error.to_string().contains("invalid option tag"),
835
- "error should mention invalid tag: {error}"
836
- );
837
- }
838
-
839
- #[test]
840
- fn change_codec_rejects_truncated_json_ref() {
841
- let change = Change {
842
- id: "change-1".to_string(),
843
- entity_id: EntityIdentity::single("entity-1"),
844
- schema_key: "test_schema".to_string(),
845
- file_id: None,
846
- snapshot_ref: Some(JsonRef::from_hash_bytes([1; 32])),
847
- metadata_ref: None,
848
- created_at: "2026-01-01T00:00:00Z".to_string(),
849
- };
850
- let mut encoded = encode_change_ref(change.as_ref()).expect("change should encode");
851
- let mut cursor = ByteCursor::new(&encoded);
852
- cursor.expect_magic(CHANGE_MAGIC, "change").unwrap();
853
- cursor.read_string("id").unwrap();
854
- cursor.read_string("entity_id").unwrap();
855
- cursor.read_string("schema_key").unwrap();
856
- cursor.read_optional_string("file_id").unwrap();
857
- cursor.read_u8("snapshot_ref").unwrap();
858
- encoded.truncate(cursor.offset + 16);
859
-
860
- let error = decode_change(&encoded).expect_err("truncated ref should fail");
861
- assert!(
862
- error.to_string().contains("truncated ref"),
863
- "error should mention truncation: {error}"
864
- );
865
- }
866
-
867
- #[test]
868
- fn change_codec_rejects_trailing_bytes() {
869
- let change = Change {
870
- id: "change-1".to_string(),
871
- entity_id: EntityIdentity::single("entity-1"),
872
- schema_key: "test_schema".to_string(),
873
- file_id: None,
874
- snapshot_ref: None,
875
- metadata_ref: None,
876
- created_at: "2026-01-01T00:00:00Z".to_string(),
877
- };
878
- let mut encoded = encode_change_ref(change.as_ref()).expect("change should encode");
879
- encoded.push(0);
880
-
881
- let error = decode_change(&encoded).expect_err("trailing bytes should fail");
882
- assert!(
883
- error.to_string().contains("trailing bytes"),
884
- "error should mention trailing bytes: {error}"
885
- );
886
- }
887
- }