@lix-js/sdk 0.6.0-preview.1 → 0.6.0-preview.2

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 (191) hide show
  1. package/SKILL.md +305 -320
  2. package/dist/engine-wasm/wasm/lix_engine.d.ts +5 -0
  3. package/dist/engine-wasm/wasm/lix_engine.js +9 -13
  4. package/dist/engine-wasm/wasm/lix_engine.wasm +0 -0
  5. package/dist/engine-wasm/wasm/lix_engine.wasm.d.ts +1 -0
  6. package/dist/open-lix.d.ts +103 -14
  7. package/dist/open-lix.js +3 -0
  8. package/dist/sqlite/index.js +99 -22
  9. package/dist-engine-src/README.md +18 -0
  10. package/dist-engine-src/src/backend/kv.rs +358 -0
  11. package/dist-engine-src/src/backend/mod.rs +12 -0
  12. package/dist-engine-src/src/backend/testing.rs +658 -0
  13. package/dist-engine-src/src/backend/types.rs +96 -0
  14. package/dist-engine-src/src/binary_cas/chunking.rs +31 -0
  15. package/dist-engine-src/src/binary_cas/codec.rs +346 -0
  16. package/dist-engine-src/src/binary_cas/context.rs +139 -0
  17. package/dist-engine-src/src/binary_cas/kv.rs +1063 -0
  18. package/dist-engine-src/src/binary_cas/mod.rs +11 -0
  19. package/dist-engine-src/src/binary_cas/types.rs +127 -0
  20. package/dist-engine-src/src/cel/context.rs +86 -0
  21. package/dist-engine-src/src/cel/error.rs +19 -0
  22. package/dist-engine-src/src/cel/mod.rs +8 -0
  23. package/dist-engine-src/src/cel/provider.rs +9 -0
  24. package/dist-engine-src/src/cel/runtime.rs +167 -0
  25. package/dist-engine-src/src/cel/value.rs +50 -0
  26. package/dist-engine-src/src/changelog/codec.rs +321 -0
  27. package/dist-engine-src/src/changelog/context.rs +92 -0
  28. package/dist-engine-src/src/changelog/materialization.rs +121 -0
  29. package/dist-engine-src/src/changelog/mod.rs +13 -0
  30. package/dist-engine-src/src/changelog/reader.rs +20 -0
  31. package/dist-engine-src/src/changelog/storage.rs +220 -0
  32. package/dist-engine-src/src/changelog/types.rs +38 -0
  33. package/dist-engine-src/src/commit_graph/context.rs +1588 -0
  34. package/dist-engine-src/src/commit_graph/mod.rs +12 -0
  35. package/dist-engine-src/src/commit_graph/types.rs +145 -0
  36. package/dist-engine-src/src/commit_graph/walker.rs +780 -0
  37. package/dist-engine-src/src/common/error.rs +313 -0
  38. package/dist-engine-src/src/common/fingerprint.rs +3 -0
  39. package/dist-engine-src/src/common/fs_path.rs +1336 -0
  40. package/dist-engine-src/src/common/identity.rs +135 -0
  41. package/dist-engine-src/src/common/metadata.rs +35 -0
  42. package/dist-engine-src/src/common/mod.rs +23 -0
  43. package/dist-engine-src/src/common/types.rs +105 -0
  44. package/dist-engine-src/src/common/wire.rs +222 -0
  45. package/dist-engine-src/src/engine.rs +239 -0
  46. package/dist-engine-src/src/entity_identity.rs +285 -0
  47. package/dist-engine-src/src/functions/context.rs +327 -0
  48. package/dist-engine-src/src/functions/deterministic.rs +113 -0
  49. package/dist-engine-src/src/functions/mod.rs +18 -0
  50. package/dist-engine-src/src/functions/provider.rs +130 -0
  51. package/dist-engine-src/src/functions/state.rs +363 -0
  52. package/dist-engine-src/src/functions/types.rs +37 -0
  53. package/dist-engine-src/src/init.rs +505 -0
  54. package/dist-engine-src/src/json_store/compression.rs +77 -0
  55. package/dist-engine-src/src/json_store/context.rs +129 -0
  56. package/dist-engine-src/src/json_store/encoded.rs +15 -0
  57. package/dist-engine-src/src/json_store/mod.rs +9 -0
  58. package/dist-engine-src/src/json_store/store.rs +236 -0
  59. package/dist-engine-src/src/json_store/types.rs +52 -0
  60. package/dist-engine-src/src/lib.rs +61 -0
  61. package/dist-engine-src/src/live_state/context.rs +2241 -0
  62. package/dist-engine-src/src/live_state/mod.rs +15 -0
  63. package/dist-engine-src/src/live_state/overlay.rs +75 -0
  64. package/dist-engine-src/src/live_state/reader.rs +23 -0
  65. package/dist-engine-src/src/live_state/types.rs +239 -0
  66. package/dist-engine-src/src/live_state/visibility.rs +218 -0
  67. package/dist-engine-src/src/plugin/archive.rs +441 -0
  68. package/dist-engine-src/src/plugin/component.rs +183 -0
  69. package/dist-engine-src/src/plugin/install.rs +637 -0
  70. package/dist-engine-src/src/plugin/manifest.rs +516 -0
  71. package/dist-engine-src/src/plugin/materializer.rs +477 -0
  72. package/dist-engine-src/src/plugin/mod.rs +33 -0
  73. package/dist-engine-src/src/plugin/plugin_manifest.json +119 -0
  74. package/dist-engine-src/src/plugin/storage.rs +74 -0
  75. package/dist-engine-src/src/schema/annotations/defaults.rs +280 -0
  76. package/dist-engine-src/src/schema/annotations/mod.rs +1 -0
  77. package/dist-engine-src/src/schema/builtin/lix_account.json +22 -0
  78. package/dist-engine-src/src/schema/builtin/lix_active_account.json +30 -0
  79. package/dist-engine-src/src/schema/builtin/lix_binary_blob_ref.json +30 -0
  80. package/dist-engine-src/src/schema/builtin/lix_change.json +62 -0
  81. package/dist-engine-src/src/schema/builtin/lix_change_author.json +46 -0
  82. package/dist-engine-src/src/schema/builtin/lix_change_set.json +18 -0
  83. package/dist-engine-src/src/schema/builtin/lix_change_set_element.json +75 -0
  84. package/dist-engine-src/src/schema/builtin/lix_commit.json +62 -0
  85. package/dist-engine-src/src/schema/builtin/lix_commit_edge.json +46 -0
  86. package/dist-engine-src/src/schema/builtin/lix_directory_descriptor.json +53 -0
  87. package/dist-engine-src/src/schema/builtin/lix_entity_label.json +63 -0
  88. package/dist-engine-src/src/schema/builtin/lix_file_descriptor.json +53 -0
  89. package/dist-engine-src/src/schema/builtin/lix_key_value.json +41 -0
  90. package/dist-engine-src/src/schema/builtin/lix_label.json +22 -0
  91. package/dist-engine-src/src/schema/builtin/lix_registered_schema.json +31 -0
  92. package/dist-engine-src/src/schema/builtin/lix_version_descriptor.json +35 -0
  93. package/dist-engine-src/src/schema/builtin/lix_version_ref.json +49 -0
  94. package/dist-engine-src/src/schema/builtin/mod.rs +271 -0
  95. package/dist-engine-src/src/schema/definition.json +157 -0
  96. package/dist-engine-src/src/schema/definition.rs +636 -0
  97. package/dist-engine-src/src/schema/key.rs +206 -0
  98. package/dist-engine-src/src/schema/mod.rs +20 -0
  99. package/dist-engine-src/src/schema/seed.rs +14 -0
  100. package/dist-engine-src/src/schema/tests.rs +739 -0
  101. package/dist-engine-src/src/schema_registry.rs +294 -0
  102. package/dist-engine-src/src/session/context.rs +366 -0
  103. package/dist-engine-src/src/session/create_version.rs +80 -0
  104. package/dist-engine-src/src/session/execute.rs +447 -0
  105. package/dist-engine-src/src/session/merge/analysis.rs +102 -0
  106. package/dist-engine-src/src/session/merge/apply.rs +23 -0
  107. package/dist-engine-src/src/session/merge/conflicts.rs +62 -0
  108. package/dist-engine-src/src/session/merge/mod.rs +11 -0
  109. package/dist-engine-src/src/session/merge/stats.rs +65 -0
  110. package/dist-engine-src/src/session/merge/version.rs +437 -0
  111. package/dist-engine-src/src/session/mod.rs +25 -0
  112. package/dist-engine-src/src/session/switch_version.rs +121 -0
  113. package/dist-engine-src/src/sql2/change_provider.rs +337 -0
  114. package/dist-engine-src/src/sql2/classify.rs +147 -0
  115. package/dist-engine-src/src/sql2/commit_derived_provider.rs +591 -0
  116. package/dist-engine-src/src/sql2/context.rs +307 -0
  117. package/dist-engine-src/src/sql2/directory_history_provider.rs +623 -0
  118. package/dist-engine-src/src/sql2/directory_provider.rs +2405 -0
  119. package/dist-engine-src/src/sql2/dml.rs +148 -0
  120. package/dist-engine-src/src/sql2/entity_history_provider.rs +444 -0
  121. package/dist-engine-src/src/sql2/entity_provider.rs +2700 -0
  122. package/dist-engine-src/src/sql2/error.rs +196 -0
  123. package/dist-engine-src/src/sql2/execute.rs +3379 -0
  124. package/dist-engine-src/src/sql2/file_history_provider.rs +902 -0
  125. package/dist-engine-src/src/sql2/file_provider.rs +3254 -0
  126. package/dist-engine-src/src/sql2/filesystem_planner.rs +1526 -0
  127. package/dist-engine-src/src/sql2/filesystem_predicates.rs +159 -0
  128. package/dist-engine-src/src/sql2/filesystem_visibility.rs +369 -0
  129. package/dist-engine-src/src/sql2/history_projection.rs +80 -0
  130. package/dist-engine-src/src/sql2/history_provider.rs +418 -0
  131. package/dist-engine-src/src/sql2/history_route.rs +643 -0
  132. package/dist-engine-src/src/sql2/lix_state_provider.rs +2430 -0
  133. package/dist-engine-src/src/sql2/mod.rs +43 -0
  134. package/dist-engine-src/src/sql2/read_only.rs +65 -0
  135. package/dist-engine-src/src/sql2/record_batch.rs +17 -0
  136. package/dist-engine-src/src/sql2/result_metadata.rs +29 -0
  137. package/dist-engine-src/src/sql2/runtime.rs +60 -0
  138. package/dist-engine-src/src/sql2/session.rs +135 -0
  139. package/dist-engine-src/src/sql2/udfs/common.rs +295 -0
  140. package/dist-engine-src/src/sql2/udfs/lix_active_version_commit_id.rs +53 -0
  141. package/dist-engine-src/src/sql2/udfs/lix_empty_blob.rs +47 -0
  142. package/dist-engine-src/src/sql2/udfs/lix_json.rs +100 -0
  143. package/dist-engine-src/src/sql2/udfs/lix_json_get.rs +99 -0
  144. package/dist-engine-src/src/sql2/udfs/lix_json_get_text.rs +99 -0
  145. package/dist-engine-src/src/sql2/udfs/lix_text_decode.rs +82 -0
  146. package/dist-engine-src/src/sql2/udfs/lix_text_encode.rs +85 -0
  147. package/dist-engine-src/src/sql2/udfs/lix_uuid_v7.rs +76 -0
  148. package/dist-engine-src/src/sql2/udfs/mod.rs +82 -0
  149. package/dist-engine-src/src/sql2/version_provider.rs +1187 -0
  150. package/dist-engine-src/src/sql2/version_scope.rs +394 -0
  151. package/dist-engine-src/src/sql2/write_normalization.rs +345 -0
  152. package/dist-engine-src/src/storage/context.rs +356 -0
  153. package/dist-engine-src/src/storage/mod.rs +14 -0
  154. package/dist-engine-src/src/storage/read_scope.rs +88 -0
  155. package/dist-engine-src/src/storage/types.rs +501 -0
  156. package/dist-engine-src/src/storage_bench.rs +3406 -0
  157. package/dist-engine-src/src/test_support.rs +81 -0
  158. package/dist-engine-src/src/tracked_state/by_file_index.rs +102 -0
  159. package/dist-engine-src/src/tracked_state/codec.rs +747 -0
  160. package/dist-engine-src/src/tracked_state/context.rs +983 -0
  161. package/dist-engine-src/src/tracked_state/diff.rs +494 -0
  162. package/dist-engine-src/src/tracked_state/materialization.rs +141 -0
  163. package/dist-engine-src/src/tracked_state/merge.rs +474 -0
  164. package/dist-engine-src/src/tracked_state/mod.rs +31 -0
  165. package/dist-engine-src/src/tracked_state/rebuild.rs +771 -0
  166. package/dist-engine-src/src/tracked_state/storage.rs +243 -0
  167. package/dist-engine-src/src/tracked_state/tree.rs +2744 -0
  168. package/dist-engine-src/src/tracked_state/tree_types.rs +176 -0
  169. package/dist-engine-src/src/tracked_state/types.rs +61 -0
  170. package/dist-engine-src/src/transaction/commit.rs +1224 -0
  171. package/dist-engine-src/src/transaction/context.rs +1307 -0
  172. package/dist-engine-src/src/transaction/live_state_overlay.rs +34 -0
  173. package/dist-engine-src/src/transaction/mod.rs +11 -0
  174. package/dist-engine-src/src/transaction/normalization.rs +1026 -0
  175. package/dist-engine-src/src/transaction/schema_resolver.rs +127 -0
  176. package/dist-engine-src/src/transaction/staging.rs +1436 -0
  177. package/dist-engine-src/src/transaction/types.rs +351 -0
  178. package/dist-engine-src/src/transaction/validation.rs +4811 -0
  179. package/dist-engine-src/src/untracked_state/codec.rs +363 -0
  180. package/dist-engine-src/src/untracked_state/context.rs +82 -0
  181. package/dist-engine-src/src/untracked_state/materialization.rs +157 -0
  182. package/dist-engine-src/src/untracked_state/mod.rs +17 -0
  183. package/dist-engine-src/src/untracked_state/storage.rs +348 -0
  184. package/dist-engine-src/src/untracked_state/types.rs +96 -0
  185. package/dist-engine-src/src/version/context.rs +52 -0
  186. package/dist-engine-src/src/version/mod.rs +12 -0
  187. package/dist-engine-src/src/version/refs.rs +421 -0
  188. package/dist-engine-src/src/version/stage_rows.rs +71 -0
  189. package/dist-engine-src/src/version/types.rs +21 -0
  190. package/dist-engine-src/src/wasm/mod.rs +60 -0
  191. package/package.json +68 -64
@@ -0,0 +1,477 @@
1
+ use std::collections::BTreeSet;
2
+ use std::sync::{Arc, RwLock};
3
+
4
+ use async_trait::async_trait;
5
+
6
+ use crate::common::LixError;
7
+ use crate::live_state::{list_installed_plugin_archive_refs, PluginArchiveRef};
8
+ use crate::Backend;
9
+
10
+ use super::component::{apply_changes_with_plugin, PluginComponentHost};
11
+ use super::{
12
+ load_installed_plugin_from_archive_bytes, plugin_key_from_archive_path, PluginContentType,
13
+ PluginRuntime,
14
+ };
15
+
16
+ #[derive(Debug, Clone, PartialEq, Eq)]
17
+ pub struct InstalledPlugin {
18
+ pub key: String,
19
+ pub runtime: PluginRuntime,
20
+ pub api_version: String,
21
+ pub path_glob: String,
22
+ pub content_type: Option<PluginContentType>,
23
+ pub entry: String,
24
+ pub manifest_json: String,
25
+ pub wasm: Vec<u8>,
26
+ }
27
+
28
+ #[async_trait(?Send)]
29
+ pub trait FilesystemPluginMaterializer {
30
+ async fn load_installed_plugins(&self) -> Result<Vec<InstalledPlugin>, LixError>;
31
+
32
+ async fn apply_plugin_changes(
33
+ &self,
34
+ plugin: &InstalledPlugin,
35
+ payload: &[u8],
36
+ ) -> Result<Vec<u8>, LixError>;
37
+ }
38
+
39
+ pub(crate) trait PluginMaterializationHost: PluginComponentHost {
40
+ fn plugin_backend(&self) -> &Arc<dyn Backend + Send + Sync>;
41
+
42
+ fn installed_plugins_cache(&self) -> &RwLock<Option<Vec<InstalledPlugin>>>;
43
+ }
44
+
45
+ pub(crate) async fn load_installed_plugins_with_runtime_cache(
46
+ host: &impl PluginMaterializationHost,
47
+ ) -> Result<Vec<InstalledPlugin>, LixError> {
48
+ if let Some(cached) = host
49
+ .installed_plugins_cache()
50
+ .read()
51
+ .map_err(|_| LixError {
52
+ code: "LIX_ERROR_UNKNOWN".to_string(),
53
+ message: "installed plugin cache lock poisoned".to_string(),
54
+ hint: None,
55
+ details: None,
56
+ })?
57
+ .clone()
58
+ {
59
+ return Ok(cached);
60
+ }
61
+
62
+ let plugins = load_installed_plugins_from_backend(host).await?;
63
+ let mut guard = host
64
+ .installed_plugins_cache()
65
+ .write()
66
+ .map_err(|_| LixError {
67
+ code: "LIX_ERROR_UNKNOWN".to_string(),
68
+ message: "installed plugin cache lock poisoned".to_string(),
69
+ hint: None,
70
+ details: None,
71
+ })?;
72
+ *guard = Some(plugins.clone());
73
+ Ok(plugins)
74
+ }
75
+
76
+ pub(crate) async fn load_installed_plugins_from_backend(
77
+ host: &impl PluginMaterializationHost,
78
+ ) -> Result<Vec<InstalledPlugin>, LixError> {
79
+ load_installed_plugins_from_backend_state(host.plugin_backend().as_ref()).await
80
+ }
81
+
82
+ pub(crate) async fn load_installed_plugins_from_backend_state(
83
+ backend: &dyn Backend,
84
+ ) -> Result<Vec<InstalledPlugin>, LixError> {
85
+ let archive_refs = list_installed_plugin_archive_refs(backend).await?;
86
+ let mut plugins = Vec::with_capacity(archive_refs.len());
87
+ for archive_ref in archive_refs {
88
+ plugins.push(
89
+ load_installed_plugin_from_archive_ref_with_backend(backend, &archive_ref).await?,
90
+ );
91
+ }
92
+ Ok(plugins)
93
+ }
94
+
95
+ pub(crate) async fn load_installed_plugin_from_archive_ref_with_backend(
96
+ backend: &dyn Backend,
97
+ archive_ref: &PluginArchiveRef,
98
+ ) -> Result<InstalledPlugin, LixError> {
99
+ let Some(plugin_key) = plugin_key_from_archive_path(&archive_ref.path) else {
100
+ return Err(LixError {
101
+ code: "LIX_ERROR_UNKNOWN".to_string(),
102
+ message: format!(
103
+ "plugin materialization: unsupported plugin archive path '{}'",
104
+ archive_ref.path
105
+ ),
106
+ hint: None,
107
+ details: None,
108
+ });
109
+ };
110
+ let binary_cas = crate::binary_cas::BinaryCasContext::new();
111
+ let mut reader = binary_cas.reader(backend);
112
+ let archive_hash = crate::binary_cas::BlobHash::from_hex(&archive_ref.blob_hash)?;
113
+ let archive_bytes = reader
114
+ .load_bytes_many(&[archive_hash])
115
+ .await?
116
+ .into_vec()
117
+ .into_iter()
118
+ .next()
119
+ .flatten()
120
+ .ok_or_else(|| LixError {
121
+ code: "LIX_ERROR_UNKNOWN".to_string(),
122
+ message: format!(
123
+ "plugin materialization: missing plugin archive blob '{}' for file '{}' ({})",
124
+ archive_ref.blob_hash, archive_ref.path, archive_ref.file_id
125
+ ),
126
+ hint: None,
127
+ details: None,
128
+ })?;
129
+ if archive_bytes.is_empty() {
130
+ return Err(LixError {
131
+ code: "LIX_ERROR_UNKNOWN".to_string(),
132
+ message: format!(
133
+ "plugin materialization: archive '{}' is empty",
134
+ archive_ref.path
135
+ ),
136
+ hint: None,
137
+ details: None,
138
+ });
139
+ }
140
+ load_installed_plugin_from_archive_bytes(&plugin_key, &archive_ref.path, &archive_bytes)
141
+ }
142
+
143
+ pub(crate) async fn list_installed_plugin_manifest_keys(
144
+ backend: &dyn Backend,
145
+ ) -> Result<BTreeSet<String>, LixError> {
146
+ Ok(load_installed_plugins_from_backend_state(backend)
147
+ .await?
148
+ .into_iter()
149
+ .map(|plugin| plugin.key)
150
+ .collect())
151
+ }
152
+
153
+ #[allow(dead_code)]
154
+ pub(crate) async fn installed_plugin_manifest_key_exists(
155
+ backend: &dyn Backend,
156
+ plugin_key: &str,
157
+ ) -> Result<bool, LixError> {
158
+ Ok(list_installed_plugin_manifest_keys(backend)
159
+ .await?
160
+ .contains(plugin_key))
161
+ }
162
+
163
+ pub(crate) fn invalidate_installed_plugins_cache(
164
+ host: &impl PluginMaterializationHost,
165
+ ) -> Result<(), LixError> {
166
+ let mut guard = host
167
+ .installed_plugins_cache()
168
+ .write()
169
+ .map_err(|_| LixError {
170
+ code: "LIX_ERROR_UNKNOWN".to_string(),
171
+ message: "installed plugin cache lock poisoned".to_string(),
172
+ hint: None,
173
+ details: None,
174
+ })?;
175
+ *guard = None;
176
+ let mut component_guard = host.plugin_component_cache().lock().map_err(|_| LixError {
177
+ code: "LIX_ERROR_UNKNOWN".to_string(),
178
+ message: "plugin component cache lock poisoned".to_string(),
179
+ hint: None,
180
+ details: None,
181
+ })?;
182
+ component_guard.clear();
183
+ Ok(())
184
+ }
185
+
186
+ #[async_trait(?Send)]
187
+ impl<T> FilesystemPluginMaterializer for T
188
+ where
189
+ T: PluginMaterializationHost,
190
+ {
191
+ async fn load_installed_plugins(&self) -> Result<Vec<InstalledPlugin>, LixError> {
192
+ load_installed_plugins_with_runtime_cache(self).await
193
+ }
194
+
195
+ async fn apply_plugin_changes(
196
+ &self,
197
+ plugin: &InstalledPlugin,
198
+ payload: &[u8],
199
+ ) -> Result<Vec<u8>, LixError> {
200
+ apply_changes_with_plugin(self, plugin, payload).await
201
+ }
202
+ }
203
+
204
+ #[cfg(test)]
205
+ mod tests {
206
+ use super::*;
207
+ use crate::binary_cas::codec::{
208
+ binary_blob_hash_bytes, encode_binary_cas_chunk, encode_binary_cas_manifest,
209
+ encode_binary_cas_manifest_chunk, BinaryCasManifest, BinaryChunkCodec,
210
+ };
211
+ use crate::binary_cas::kv::{
212
+ BINARY_CAS_CHUNK_NAMESPACE, BINARY_CAS_MANIFEST_CHUNK_NAMESPACE,
213
+ BINARY_CAS_MANIFEST_NAMESPACE,
214
+ };
215
+ use crate::{
216
+ BackendKvEntryPage, BackendKvExistsBatch, BackendKvExistsGroup,
217
+ BackendKvGetRequest, BackendKvKeyPage, BackendKvScanRange, BackendKvScanRequest,
218
+ BackendKvValueBatch, BackendKvValueGroup, BackendKvValuePage, BackendKvWriteBatch,
219
+ BackendKvWriteStats, BackendReadTransaction, BackendWriteTransaction, BytePageBuilder,
220
+ };
221
+ use async_trait::async_trait;
222
+ use std::io::{Cursor, Write};
223
+ use zip::write::SimpleFileOptions;
224
+ use zip::{CompressionMethod, ZipWriter};
225
+
226
+ struct InstalledPluginLookupBackend {
227
+ archive_bytes: Vec<u8>,
228
+ }
229
+
230
+ struct PluginLookupTransaction {
231
+ archive_bytes: Vec<u8>,
232
+ }
233
+
234
+ #[async_trait]
235
+ impl Backend for InstalledPluginLookupBackend {
236
+ async fn begin_read_transaction(
237
+ &self,
238
+ ) -> Result<Box<dyn BackendReadTransaction + Send + Sync + 'static>, LixError> {
239
+ Ok(Box::new(PluginLookupTransaction {
240
+ archive_bytes: self.archive_bytes.clone(),
241
+ }))
242
+ }
243
+
244
+ async fn begin_write_transaction(
245
+ &self,
246
+ ) -> Result<Box<dyn BackendWriteTransaction + Send + Sync + 'static>, LixError> {
247
+ Ok(Box::new(PluginLookupTransaction {
248
+ archive_bytes: self.archive_bytes.clone(),
249
+ }))
250
+ }
251
+ }
252
+
253
+ #[async_trait]
254
+ impl BackendReadTransaction for PluginLookupTransaction {
255
+ async fn get_values(
256
+ &mut self,
257
+ request: BackendKvGetRequest,
258
+ ) -> Result<BackendKvValueBatch, LixError> {
259
+ let mut groups = Vec::with_capacity(request.groups.len());
260
+ for group in request.groups {
261
+ let namespace = group.namespace.clone();
262
+ let mut values = BytePageBuilder::with_capacity(group.keys.len(), 0);
263
+ let mut present = Vec::with_capacity(group.keys.len());
264
+ for key in group.keys {
265
+ if let Some(value) = test_kv_get(&self.archive_bytes, &group.namespace, &key)? {
266
+ values.push(value);
267
+ present.push(true);
268
+ } else {
269
+ values.push([]);
270
+ present.push(false);
271
+ }
272
+ }
273
+ groups.push(BackendKvValueGroup::new(namespace, values.finish(), present));
274
+ }
275
+ Ok(BackendKvValueBatch { groups })
276
+ }
277
+
278
+ async fn exists_many(
279
+ &mut self,
280
+ request: BackendKvGetRequest,
281
+ ) -> Result<BackendKvExistsBatch, LixError> {
282
+ let mut groups = Vec::with_capacity(request.groups.len());
283
+ for group in request.groups {
284
+ let namespace = group.namespace.clone();
285
+ let exists = group
286
+ .keys
287
+ .iter()
288
+ .map(|key| test_kv_get(&self.archive_bytes, &group.namespace, key))
289
+ .collect::<Result<Vec<_>, LixError>>()?
290
+ .into_iter()
291
+ .map(|value| value.is_some())
292
+ .collect();
293
+ groups.push(BackendKvExistsGroup {
294
+ namespace,
295
+ exists,
296
+ });
297
+ }
298
+ Ok(BackendKvExistsBatch { groups })
299
+ }
300
+
301
+ async fn scan_keys(
302
+ &mut self,
303
+ request: BackendKvScanRequest,
304
+ ) -> Result<BackendKvKeyPage, LixError> {
305
+ let entries = test_kv_scan(&self.archive_bytes, request)?;
306
+ Ok(BackendKvKeyPage {
307
+ keys: entries.keys,
308
+ resume_after: entries.resume_after,
309
+ })
310
+ }
311
+
312
+ async fn scan_values(
313
+ &mut self,
314
+ request: BackendKvScanRequest,
315
+ ) -> Result<BackendKvValuePage, LixError> {
316
+ let entries = test_kv_scan(&self.archive_bytes, request)?;
317
+ Ok(BackendKvValuePage {
318
+ values: entries.values,
319
+ resume_after: entries.resume_after,
320
+ })
321
+ }
322
+
323
+ async fn scan_entries(
324
+ &mut self,
325
+ request: BackendKvScanRequest,
326
+ ) -> Result<BackendKvEntryPage, LixError> {
327
+ test_kv_scan(&self.archive_bytes, request)
328
+ }
329
+
330
+ async fn rollback(self: Box<Self>) -> Result<(), LixError> {
331
+ Ok(())
332
+ }
333
+ }
334
+
335
+ #[async_trait]
336
+ impl BackendWriteTransaction for PluginLookupTransaction {
337
+ async fn write_kv_batch(&mut self, _batch: BackendKvWriteBatch) -> Result<BackendKvWriteStats, LixError> {
338
+ Err(LixError::new(
339
+ "LIX_ERROR_UNKNOWN",
340
+ "plugin lookup test backend is read-only",
341
+ ))
342
+ }
343
+
344
+ async fn commit(self: Box<Self>) -> Result<(), LixError> {
345
+ Ok(())
346
+ }
347
+ }
348
+
349
+ fn test_kv_get(
350
+ archive_bytes: &[u8],
351
+ namespace: &str,
352
+ key: &[u8],
353
+ ) -> Result<Option<Vec<u8>>, LixError> {
354
+ match (namespace, key) {
355
+ (BINARY_CAS_MANIFEST_NAMESPACE, key)
356
+ if key == binary_blob_hash_bytes(archive_bytes).as_slice() =>
357
+ {
358
+ Ok(Some(encode_binary_cas_manifest(
359
+ &BinaryCasManifest::Chunked {
360
+ size_bytes: archive_bytes.len() as u64,
361
+ chunk_count: 1,
362
+ },
363
+ )))
364
+ }
365
+ (BINARY_CAS_CHUNK_NAMESPACE, key)
366
+ if key == binary_blob_hash_bytes(archive_bytes).as_slice() =>
367
+ {
368
+ Ok(Some(encode_binary_cas_chunk(
369
+ BinaryChunkCodec::Raw,
370
+ archive_bytes.len() as u64,
371
+ archive_bytes,
372
+ )))
373
+ }
374
+ _ => Ok(None),
375
+ }
376
+ }
377
+
378
+ fn test_kv_scan(
379
+ archive_bytes: &[u8],
380
+ request: BackendKvScanRequest,
381
+ ) -> Result<BackendKvEntryPage, LixError> {
382
+ if request.namespace != BINARY_CAS_MANIFEST_CHUNK_NAMESPACE {
383
+ return Ok(BackendKvEntryPage {
384
+ keys: BytePageBuilder::new().finish(),
385
+ values: BytePageBuilder::new().finish(),
386
+ resume_after: None,
387
+ });
388
+ }
389
+ let blob_hash = binary_blob_hash_bytes(archive_bytes);
390
+ let chunk_hash = binary_blob_hash_bytes(archive_bytes);
391
+ let mut key = blob_hash.to_vec();
392
+ key.extend_from_slice(&0u64.to_be_bytes());
393
+ let include = match request.range {
394
+ BackendKvScanRange::Prefix(prefix) => key.starts_with(&prefix),
395
+ BackendKvScanRange::Range { start, end } => key >= start && key < end,
396
+ };
397
+ if !include || request.after.as_deref().is_some_and(|after| key.as_slice() <= after) {
398
+ return Ok(BackendKvEntryPage {
399
+ keys: BytePageBuilder::new().finish(),
400
+ values: BytePageBuilder::new().finish(),
401
+ resume_after: None,
402
+ });
403
+ }
404
+ let value = encode_binary_cas_manifest_chunk(&chunk_hash, archive_bytes.len() as u64);
405
+ let mut keys = BytePageBuilder::with_capacity(1, key.len());
406
+ let mut values = BytePageBuilder::with_capacity(1, value.len());
407
+ let mut resume_after = None;
408
+ if request.limit > 0 {
409
+ resume_after = Some(key.clone());
410
+ keys.push(&key);
411
+ values.push(&value);
412
+ }
413
+ let resume_after = (request.limit == 0).then_some(resume_after).flatten();
414
+ Ok(BackendKvEntryPage {
415
+ keys: keys.finish(),
416
+ values: values.finish(),
417
+ resume_after,
418
+ })
419
+ }
420
+
421
+ fn build_archive(entries: &[(&str, &[u8])]) -> Vec<u8> {
422
+ let options = SimpleFileOptions::default().compression_method(CompressionMethod::Stored);
423
+ let cursor = Cursor::new(Vec::new());
424
+ let mut writer = ZipWriter::new(cursor);
425
+ for (path, bytes) in entries {
426
+ writer
427
+ .start_file(*path, options)
428
+ .expect("archive entry start should succeed");
429
+ writer
430
+ .write_all(bytes)
431
+ .expect("archive entry write should succeed");
432
+ }
433
+ writer
434
+ .finish()
435
+ .expect("archive finish should succeed")
436
+ .into_inner()
437
+ }
438
+
439
+ fn build_plugin_archive(manifest_json: &str) -> Vec<u8> {
440
+ let wasm = [0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00];
441
+ build_archive(&[
442
+ ("manifest.json", manifest_json.as_bytes()),
443
+ ("plugin.wasm", &wasm),
444
+ ])
445
+ }
446
+
447
+ fn plugin_manifest_json(key: &str) -> String {
448
+ format!(
449
+ r#"{{
450
+ "key":"{key}",
451
+ "runtime":"wasm-component-v1",
452
+ "api_version":"0.1.0",
453
+ "match":{{"path_glob":"*.json"}},
454
+ "entry":"plugin.wasm",
455
+ "schemas":["schema/plugin_json_schema.json"]
456
+ }}"#
457
+ )
458
+ }
459
+
460
+ #[tokio::test]
461
+ async fn installed_plugin_manifest_key_exists_reads_installed_manifest_keys() {
462
+ let backend = InstalledPluginLookupBackend {
463
+ archive_bytes: build_plugin_archive(&plugin_manifest_json("plugin_json")),
464
+ };
465
+
466
+ assert!(
467
+ installed_plugin_manifest_key_exists(&backend, "plugin_json")
468
+ .await
469
+ .expect("installed manifest key lookup should succeed")
470
+ );
471
+ assert!(
472
+ !installed_plugin_manifest_key_exists(&backend, "missing_plugin")
473
+ .await
474
+ .expect("missing manifest key lookup should succeed")
475
+ );
476
+ }
477
+ }
@@ -0,0 +1,33 @@
1
+ //! Plugin subsystem root.
2
+ //!
3
+ //! Phase 1 establishes `crate::plugin::*` as the owner path for plugin-domain
4
+ //! code under concrete plugin-owned modules instead of legacy ownership-neutral
5
+ //! buckets.
6
+
7
+ mod archive;
8
+ pub(crate) mod component;
9
+ mod manifest;
10
+ mod materializer;
11
+ mod storage;
12
+
13
+ pub(crate) use archive::{
14
+ load_installed_plugin_from_archive_bytes, parse_plugin_archive_for_install, ParsedPluginArchive,
15
+ };
16
+ #[allow(unused_imports)]
17
+ pub(crate) use manifest::{
18
+ glob_matches_path, parse_plugin_manifest_json, select_best_glob_match, DetectChangesConfig,
19
+ DetectStateContextConfig, PluginContentType, PluginManifest, PluginMatch, PluginRuntime,
20
+ StateContextColumn, ValidatedPluginManifest,
21
+ };
22
+ #[allow(unused_imports)]
23
+ pub(crate) use materializer::{
24
+ installed_plugin_manifest_key_exists, invalidate_installed_plugins_cache,
25
+ list_installed_plugin_manifest_keys, load_installed_plugins_from_backend_state,
26
+ load_installed_plugins_with_runtime_cache, FilesystemPluginMaterializer, InstalledPlugin,
27
+ PluginMaterializationHost,
28
+ };
29
+ #[allow(unused_imports)]
30
+ pub(crate) use storage::{
31
+ plugin_key_from_archive_path, plugin_storage_archive_file_id, plugin_storage_archive_path,
32
+ PLUGIN_ARCHIVE_FILE_EXTENSION, PLUGIN_STORAGE_ROOT_DIRECTORY_PATH,
33
+ };
@@ -0,0 +1,119 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "type": "object",
4
+ "additionalProperties": false,
5
+ "required": [
6
+ "key",
7
+ "runtime",
8
+ "api_version",
9
+ "match",
10
+ "entry",
11
+ "schemas"
12
+ ],
13
+ "properties": {
14
+ "key": {
15
+ "type": "string",
16
+ "minLength": 1,
17
+ "maxLength": 128,
18
+ "pattern": "^[a-z][a-z0-9_-]*$"
19
+ },
20
+ "runtime": {
21
+ "type": "string",
22
+ "enum": [
23
+ "wasm-component-v1"
24
+ ]
25
+ },
26
+ "api_version": {
27
+ "type": "string",
28
+ "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"
29
+ },
30
+ "match": {
31
+ "type": "object",
32
+ "additionalProperties": false,
33
+ "required": ["path_glob"],
34
+ "properties": {
35
+ "path_glob": {
36
+ "type": "string",
37
+ "minLength": 1
38
+ },
39
+ "content_type": {
40
+ "type": "string",
41
+ "enum": ["text", "binary"]
42
+ }
43
+ }
44
+ },
45
+ "detect_changes": {
46
+ "type": "object",
47
+ "additionalProperties": false,
48
+ "properties": {
49
+ "state_context": {
50
+ "type": "object",
51
+ "additionalProperties": false,
52
+ "properties": {
53
+ "include_active_state": {
54
+ "type": "boolean"
55
+ },
56
+ "columns": {
57
+ "type": "array",
58
+ "minItems": 1,
59
+ "uniqueItems": true,
60
+ "items": {
61
+ "type": "string",
62
+ "enum": [
63
+ "entity_id",
64
+ "schema_key",
65
+ "schema_version",
66
+ "snapshot_content",
67
+ "file_id",
68
+ "plugin_key",
69
+ "version_id",
70
+ "change_id",
71
+ "metadata",
72
+ "created_at",
73
+ "updated_at"
74
+ ]
75
+ },
76
+ "contains": {
77
+ "const": "entity_id"
78
+ }
79
+ }
80
+ },
81
+ "allOf": [
82
+ {
83
+ "if": {
84
+ "properties": {
85
+ "include_active_state": {
86
+ "const": true
87
+ }
88
+ },
89
+ "required": [
90
+ "include_active_state"
91
+ ]
92
+ },
93
+ "then": {},
94
+ "else": {
95
+ "not": {
96
+ "required": [
97
+ "columns"
98
+ ]
99
+ }
100
+ }
101
+ }
102
+ ]
103
+ }
104
+ }
105
+ },
106
+ "entry": {
107
+ "type": "string",
108
+ "minLength": 1
109
+ },
110
+ "schemas": {
111
+ "type": "array",
112
+ "minItems": 1,
113
+ "items": {
114
+ "type": "string",
115
+ "minLength": 1
116
+ }
117
+ }
118
+ }
119
+ }
@@ -0,0 +1,74 @@
1
+ use crate::LixError;
2
+
3
+ pub const PLUGIN_STORAGE_ROOT_DIRECTORY_PATH: &str = "/.lix/plugins/";
4
+ pub const PLUGIN_ARCHIVE_FILE_EXTENSION: &str = ".lixplugin";
5
+
6
+ pub fn plugin_storage_archive_file_id(plugin_key: &str) -> String {
7
+ format!("lix_plugin_archive::{plugin_key}")
8
+ }
9
+
10
+ pub fn plugin_storage_archive_path(plugin_key: &str) -> Result<String, LixError> {
11
+ validate_plugin_key_segment(plugin_key)?;
12
+ Ok(format!(
13
+ "{PLUGIN_STORAGE_ROOT_DIRECTORY_PATH}{plugin_key}{PLUGIN_ARCHIVE_FILE_EXTENSION}"
14
+ ))
15
+ }
16
+
17
+ pub fn plugin_key_from_archive_path(path: &str) -> Option<String> {
18
+ let file_name = path.strip_prefix(PLUGIN_STORAGE_ROOT_DIRECTORY_PATH)?;
19
+ let plugin_key = file_name.strip_suffix(PLUGIN_ARCHIVE_FILE_EXTENSION)?;
20
+ if plugin_key.is_empty()
21
+ || plugin_key == "."
22
+ || plugin_key == ".."
23
+ || plugin_key.contains('/')
24
+ || plugin_key.contains('\\')
25
+ {
26
+ return None;
27
+ }
28
+ Some(plugin_key.to_string())
29
+ }
30
+
31
+ fn validate_plugin_key_segment(plugin_key: &str) -> Result<(), LixError> {
32
+ if plugin_key.is_empty()
33
+ || plugin_key == "."
34
+ || plugin_key == ".."
35
+ || plugin_key.contains('/')
36
+ || plugin_key.contains('\\')
37
+ {
38
+ return Err(LixError {
39
+ code: "LIX_ERROR_UNKNOWN".to_string(),
40
+ message: format!(
41
+ "plugin key '{}' must be a single relative path segment",
42
+ plugin_key
43
+ ),
44
+ hint: None,
45
+ details: None,
46
+ });
47
+ }
48
+ Ok(())
49
+ }
50
+
51
+ #[cfg(test)]
52
+ mod tests {
53
+ use super::{plugin_key_from_archive_path, plugin_storage_archive_path};
54
+
55
+ #[test]
56
+ fn computes_storage_archive_paths() {
57
+ assert_eq!(
58
+ plugin_storage_archive_path("plugin_json").expect("path should build"),
59
+ "/.lix/plugins/plugin_json.lixplugin"
60
+ );
61
+ }
62
+
63
+ #[test]
64
+ fn extracts_plugin_key_from_storage_path() {
65
+ assert_eq!(
66
+ plugin_key_from_archive_path("/.lix/plugins/plugin_json.lixplugin"),
67
+ Some("plugin_json".to_string())
68
+ );
69
+ assert_eq!(
70
+ plugin_key_from_archive_path("/.lix/plugins/nested/plugin.lixplugin"),
71
+ None
72
+ );
73
+ }
74
+ }