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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (223) hide show
  1. package/README.md +76 -4
  2. package/dist/errors.d.ts +7 -0
  3. package/dist/errors.js +19 -0
  4. package/dist/index.d.ts +4 -5
  5. package/dist/index.js +3 -3
  6. package/dist/native.d.ts +1 -0
  7. package/dist/native.js +47 -0
  8. package/dist/open-lix.d.ts +39 -201
  9. package/dist/open-lix.js +59 -284
  10. package/dist/result.d.ts +18 -0
  11. package/dist/result.js +48 -0
  12. package/dist/types.d.ts +114 -1
  13. package/dist/value.d.ts +28 -0
  14. package/dist/value.js +245 -0
  15. package/package.json +20 -50
  16. package/SKILL.md +0 -506
  17. package/dist/builtin-schemas.d.ts +0 -1
  18. package/dist/builtin-schemas.js +0 -1
  19. package/dist/engine-wasm/index.d.ts +0 -87
  20. package/dist/engine-wasm/index.js +0 -339
  21. package/dist/engine-wasm/wasm/lix_engine.d.ts +0 -79
  22. package/dist/engine-wasm/wasm/lix_engine.js +0 -821
  23. package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
  24. package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +0 -26
  25. package/dist/generated/builtin-schemas.d.ts +0 -427
  26. package/dist/generated/builtin-schemas.js +0 -643
  27. package/dist/sqlite/index.d.ts +0 -12
  28. package/dist/sqlite/index.js +0 -303
  29. package/dist-engine-src/README.md +0 -18
  30. package/dist-engine-src/src/backend/kv.rs +0 -358
  31. package/dist-engine-src/src/backend/mod.rs +0 -12
  32. package/dist-engine-src/src/backend/testing.rs +0 -658
  33. package/dist-engine-src/src/backend/types.rs +0 -96
  34. package/dist-engine-src/src/binary_cas/chunking.rs +0 -31
  35. package/dist-engine-src/src/binary_cas/codec.rs +0 -346
  36. package/dist-engine-src/src/binary_cas/context.rs +0 -139
  37. package/dist-engine-src/src/binary_cas/kv.rs +0 -1063
  38. package/dist-engine-src/src/binary_cas/mod.rs +0 -11
  39. package/dist-engine-src/src/binary_cas/types.rs +0 -121
  40. package/dist-engine-src/src/catalog/context.rs +0 -412
  41. package/dist-engine-src/src/catalog/mod.rs +0 -10
  42. package/dist-engine-src/src/catalog/schema.rs +0 -4
  43. package/dist-engine-src/src/catalog/snapshot.rs +0 -1114
  44. package/dist-engine-src/src/cel/context.rs +0 -86
  45. package/dist-engine-src/src/cel/error.rs +0 -19
  46. package/dist-engine-src/src/cel/mod.rs +0 -8
  47. package/dist-engine-src/src/cel/provider.rs +0 -9
  48. package/dist-engine-src/src/cel/runtime.rs +0 -167
  49. package/dist-engine-src/src/cel/value.rs +0 -50
  50. package/dist-engine-src/src/commit_graph/context.rs +0 -901
  51. package/dist-engine-src/src/commit_graph/mod.rs +0 -11
  52. package/dist-engine-src/src/commit_graph/types.rs +0 -109
  53. package/dist-engine-src/src/commit_graph/walker.rs +0 -756
  54. package/dist-engine-src/src/commit_store/codec.rs +0 -887
  55. package/dist-engine-src/src/commit_store/context.rs +0 -944
  56. package/dist-engine-src/src/commit_store/materialization.rs +0 -84
  57. package/dist-engine-src/src/commit_store/mod.rs +0 -16
  58. package/dist-engine-src/src/commit_store/storage.rs +0 -600
  59. package/dist-engine-src/src/commit_store/types.rs +0 -215
  60. package/dist-engine-src/src/common/error.rs +0 -313
  61. package/dist-engine-src/src/common/fingerprint.rs +0 -3
  62. package/dist-engine-src/src/common/fs_path.rs +0 -1336
  63. package/dist-engine-src/src/common/identity.rs +0 -145
  64. package/dist-engine-src/src/common/json_pointer.rs +0 -67
  65. package/dist-engine-src/src/common/metadata.rs +0 -40
  66. package/dist-engine-src/src/common/mod.rs +0 -23
  67. package/dist-engine-src/src/common/types.rs +0 -105
  68. package/dist-engine-src/src/common/wire.rs +0 -222
  69. package/dist-engine-src/src/domain.rs +0 -324
  70. package/dist-engine-src/src/engine.rs +0 -225
  71. package/dist-engine-src/src/entity_identity.rs +0 -405
  72. package/dist-engine-src/src/functions/context.rs +0 -292
  73. package/dist-engine-src/src/functions/deterministic.rs +0 -113
  74. package/dist-engine-src/src/functions/mod.rs +0 -18
  75. package/dist-engine-src/src/functions/provider.rs +0 -130
  76. package/dist-engine-src/src/functions/state.rs +0 -336
  77. package/dist-engine-src/src/functions/types.rs +0 -37
  78. package/dist-engine-src/src/init.rs +0 -558
  79. package/dist-engine-src/src/json_store/compression.rs +0 -77
  80. package/dist-engine-src/src/json_store/context.rs +0 -423
  81. package/dist-engine-src/src/json_store/encoded.rs +0 -15
  82. package/dist-engine-src/src/json_store/mod.rs +0 -12
  83. package/dist-engine-src/src/json_store/store.rs +0 -1109
  84. package/dist-engine-src/src/json_store/types.rs +0 -217
  85. package/dist-engine-src/src/lib.rs +0 -62
  86. package/dist-engine-src/src/live_state/context.rs +0 -2019
  87. package/dist-engine-src/src/live_state/mod.rs +0 -15
  88. package/dist-engine-src/src/live_state/overlay.rs +0 -75
  89. package/dist-engine-src/src/live_state/reader.rs +0 -23
  90. package/dist-engine-src/src/live_state/types.rs +0 -222
  91. package/dist-engine-src/src/live_state/visibility.rs +0 -223
  92. package/dist-engine-src/src/plugin/archive.rs +0 -438
  93. package/dist-engine-src/src/plugin/component.rs +0 -183
  94. package/dist-engine-src/src/plugin/install.rs +0 -619
  95. package/dist-engine-src/src/plugin/manifest.rs +0 -516
  96. package/dist-engine-src/src/plugin/materializer.rs +0 -477
  97. package/dist-engine-src/src/plugin/mod.rs +0 -33
  98. package/dist-engine-src/src/plugin/plugin_manifest.json +0 -118
  99. package/dist-engine-src/src/plugin/storage.rs +0 -74
  100. package/dist-engine-src/src/schema/annotations/defaults.rs +0 -275
  101. package/dist-engine-src/src/schema/annotations/mod.rs +0 -1
  102. package/dist-engine-src/src/schema/builtin/lix_account.json +0 -21
  103. package/dist-engine-src/src/schema/builtin/lix_active_account.json +0 -29
  104. package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +0 -29
  105. package/dist-engine-src/src/schema/builtin/lix_change.json +0 -63
  106. package/dist-engine-src/src/schema/builtin/lix_change_author.json +0 -45
  107. package/dist-engine-src/src/schema/builtin/lix_commit.json +0 -24
  108. package/dist-engine-src/src/schema/builtin/lix_commit_edge.json +0 -53
  109. package/dist-engine-src/src/schema/builtin/lix_directory_descriptor.json +0 -52
  110. package/dist-engine-src/src/schema/builtin/lix_file_descriptor.json +0 -52
  111. package/dist-engine-src/src/schema/builtin/lix_key_value.json +0 -40
  112. package/dist-engine-src/src/schema/builtin/lix_label.json +0 -29
  113. package/dist-engine-src/src/schema/builtin/lix_label_assignment.json +0 -74
  114. package/dist-engine-src/src/schema/builtin/lix_registered_schema.json +0 -25
  115. package/dist-engine-src/src/schema/builtin/lix_version_descriptor.json +0 -34
  116. package/dist-engine-src/src/schema/builtin/lix_version_ref.json +0 -48
  117. package/dist-engine-src/src/schema/builtin/mod.rs +0 -222
  118. package/dist-engine-src/src/schema/compatibility.rs +0 -787
  119. package/dist-engine-src/src/schema/definition.json +0 -187
  120. package/dist-engine-src/src/schema/definition.rs +0 -742
  121. package/dist-engine-src/src/schema/key.rs +0 -138
  122. package/dist-engine-src/src/schema/mod.rs +0 -20
  123. package/dist-engine-src/src/schema/seed.rs +0 -14
  124. package/dist-engine-src/src/schema/tests.rs +0 -780
  125. package/dist-engine-src/src/session/context.rs +0 -404
  126. package/dist-engine-src/src/session/create_version.rs +0 -88
  127. package/dist-engine-src/src/session/execute.rs +0 -541
  128. package/dist-engine-src/src/session/merge/analysis.rs +0 -102
  129. package/dist-engine-src/src/session/merge/apply.rs +0 -23
  130. package/dist-engine-src/src/session/merge/conflicts.rs +0 -63
  131. package/dist-engine-src/src/session/merge/mod.rs +0 -11
  132. package/dist-engine-src/src/session/merge/stats.rs +0 -65
  133. package/dist-engine-src/src/session/merge/version.rs +0 -427
  134. package/dist-engine-src/src/session/mod.rs +0 -27
  135. package/dist-engine-src/src/session/optimization9_sql2_bench.rs +0 -100
  136. package/dist-engine-src/src/session/switch_version.rs +0 -110
  137. package/dist-engine-src/src/session/transaction.rs +0 -76
  138. package/dist-engine-src/src/sql2/change_provider.rs +0 -331
  139. package/dist-engine-src/src/sql2/classify.rs +0 -174
  140. package/dist-engine-src/src/sql2/context.rs +0 -311
  141. package/dist-engine-src/src/sql2/directory_history_provider.rs +0 -631
  142. package/dist-engine-src/src/sql2/directory_provider.rs +0 -2453
  143. package/dist-engine-src/src/sql2/dml.rs +0 -148
  144. package/dist-engine-src/src/sql2/entity_history_provider.rs +0 -440
  145. package/dist-engine-src/src/sql2/entity_provider.rs +0 -3211
  146. package/dist-engine-src/src/sql2/error.rs +0 -215
  147. package/dist-engine-src/src/sql2/execute.rs +0 -3533
  148. package/dist-engine-src/src/sql2/file_history_provider.rs +0 -910
  149. package/dist-engine-src/src/sql2/file_provider.rs +0 -3679
  150. package/dist-engine-src/src/sql2/filesystem_planner.rs +0 -1490
  151. package/dist-engine-src/src/sql2/filesystem_predicates.rs +0 -159
  152. package/dist-engine-src/src/sql2/filesystem_visibility.rs +0 -383
  153. package/dist-engine-src/src/sql2/history_projection.rs +0 -56
  154. package/dist-engine-src/src/sql2/history_provider.rs +0 -412
  155. package/dist-engine-src/src/sql2/history_route.rs +0 -657
  156. package/dist-engine-src/src/sql2/lix_state_provider.rs +0 -2512
  157. package/dist-engine-src/src/sql2/mod.rs +0 -47
  158. package/dist-engine-src/src/sql2/predicate_typecheck.rs +0 -246
  159. package/dist-engine-src/src/sql2/public_bind/assignment.rs +0 -46
  160. package/dist-engine-src/src/sql2/public_bind/capability.rs +0 -41
  161. package/dist-engine-src/src/sql2/public_bind/dml.rs +0 -172
  162. package/dist-engine-src/src/sql2/public_bind/mod.rs +0 -26
  163. package/dist-engine-src/src/sql2/public_bind/table.rs +0 -168
  164. package/dist-engine-src/src/sql2/read_only.rs +0 -63
  165. package/dist-engine-src/src/sql2/record_batch.rs +0 -17
  166. package/dist-engine-src/src/sql2/result_metadata.rs +0 -29
  167. package/dist-engine-src/src/sql2/runtime.rs +0 -60
  168. package/dist-engine-src/src/sql2/session.rs +0 -132
  169. package/dist-engine-src/src/sql2/udfs/common.rs +0 -295
  170. package/dist-engine-src/src/sql2/udfs/lix_active_version_commit_id.rs +0 -53
  171. package/dist-engine-src/src/sql2/udfs/lix_empty_blob.rs +0 -47
  172. package/dist-engine-src/src/sql2/udfs/lix_json.rs +0 -100
  173. package/dist-engine-src/src/sql2/udfs/lix_json_get.rs +0 -99
  174. package/dist-engine-src/src/sql2/udfs/lix_json_get_text.rs +0 -99
  175. package/dist-engine-src/src/sql2/udfs/lix_text_decode.rs +0 -82
  176. package/dist-engine-src/src/sql2/udfs/lix_text_encode.rs +0 -85
  177. package/dist-engine-src/src/sql2/udfs/lix_timestamp.rs +0 -76
  178. package/dist-engine-src/src/sql2/udfs/lix_uuid_v7.rs +0 -76
  179. package/dist-engine-src/src/sql2/udfs/mod.rs +0 -89
  180. package/dist-engine-src/src/sql2/udfs/public_call.rs +0 -238
  181. package/dist-engine-src/src/sql2/version_provider.rs +0 -1202
  182. package/dist-engine-src/src/sql2/version_scope.rs +0 -394
  183. package/dist-engine-src/src/sql2/write_normalization.rs +0 -345
  184. package/dist-engine-src/src/storage/context.rs +0 -356
  185. package/dist-engine-src/src/storage/mod.rs +0 -14
  186. package/dist-engine-src/src/storage/read_scope.rs +0 -88
  187. package/dist-engine-src/src/storage/types.rs +0 -501
  188. package/dist-engine-src/src/storage_bench.rs +0 -4863
  189. package/dist-engine-src/src/test_support.rs +0 -228
  190. package/dist-engine-src/src/tracked_state/by_file_index.rs +0 -98
  191. package/dist-engine-src/src/tracked_state/codec.rs +0 -2085
  192. package/dist-engine-src/src/tracked_state/context.rs +0 -1867
  193. package/dist-engine-src/src/tracked_state/diff.rs +0 -686
  194. package/dist-engine-src/src/tracked_state/materialization.rs +0 -403
  195. package/dist-engine-src/src/tracked_state/materializer.rs +0 -488
  196. package/dist-engine-src/src/tracked_state/merge.rs +0 -492
  197. package/dist-engine-src/src/tracked_state/mod.rs +0 -32
  198. package/dist-engine-src/src/tracked_state/storage.rs +0 -375
  199. package/dist-engine-src/src/tracked_state/tree.rs +0 -3187
  200. package/dist-engine-src/src/tracked_state/types.rs +0 -231
  201. package/dist-engine-src/src/transaction/commit.rs +0 -1484
  202. package/dist-engine-src/src/transaction/context.rs +0 -1548
  203. package/dist-engine-src/src/transaction/live_state_overlay.rs +0 -35
  204. package/dist-engine-src/src/transaction/mod.rs +0 -13
  205. package/dist-engine-src/src/transaction/normalization.rs +0 -890
  206. package/dist-engine-src/src/transaction/prep.rs +0 -37
  207. package/dist-engine-src/src/transaction/schema_resolver.rs +0 -149
  208. package/dist-engine-src/src/transaction/staging.rs +0 -1731
  209. package/dist-engine-src/src/transaction/types.rs +0 -460
  210. package/dist-engine-src/src/transaction/validation.rs +0 -5830
  211. package/dist-engine-src/src/untracked_state/codec.rs +0 -307
  212. package/dist-engine-src/src/untracked_state/context.rs +0 -98
  213. package/dist-engine-src/src/untracked_state/materialization.rs +0 -63
  214. package/dist-engine-src/src/untracked_state/mod.rs +0 -15
  215. package/dist-engine-src/src/untracked_state/storage.rs +0 -396
  216. package/dist-engine-src/src/untracked_state/types.rs +0 -146
  217. package/dist-engine-src/src/version/context.rs +0 -40
  218. package/dist-engine-src/src/version/lifecycle.rs +0 -221
  219. package/dist-engine-src/src/version/mod.rs +0 -13
  220. package/dist-engine-src/src/version/refs.rs +0 -330
  221. package/dist-engine-src/src/version/stage_rows.rs +0 -67
  222. package/dist-engine-src/src/version/types.rs +0 -21
  223. package/dist-engine-src/src/wasm/mod.rs +0 -60
@@ -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
- }