prikk-store 0.24.0

Prikk storage crate scaffold.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Read-only inverse planning for the supported patch-operation subset.
//!
//! PR-026 deliberately keeps inverse handling non-mutating. It validates a supported single-parent
//! patch chain while replaying it, derives the inverse operation sequence for the currently
//! supported operation subset, and exposes the unsigned inverse Patch payload as planning metadata.
//! Publishing, rollback refs, conflict witnesses, and full patch algebra remain later increments.

use std::collections::BTreeMap;

use prikk_error::{PrikkError, Result};
use prikk_object::{
    CanonicalEncode, ChangePerm, CreateFile, DeleteNode, DeleteNodePreimage, NodeId, NodeKind,
    ObjectEnvelope, ObjectId, ObjectType, Operation, OperationKind,
    PATCH_PARENT_IDS_RETIRED_SCHEMA, PatchPayload, PatchPurpose, ReplaceBinary,
};

use crate::layout::RepositoryLayout;
use crate::object_store::{ObjectReadSnapshot, ObjectReader};
use crate::patch_replay::decode::{
    DecodedDeletePreimage, DecodedOperationKind, DecodedPatchOperation, decode_patch_operations,
};
use crate::text_span;

mod read;

use read::{
    current_target_block, load_snapshot_files, read_blob_bytes_with_kind, read_block, read_patch,
    single_parent_chain,
};

/// Read-only inverse plan for the supported patch-operation subset.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PatchInversePlan {
    /// Ref used as the inverse planning target.
    pub ref_name: String,
    /// Target block ID whose supported patch chain was inspected.
    pub target_block_id: ObjectId,
    /// Number of blocks walked from the root side to the target.
    pub block_count: usize,
    /// Number of patch objects inspected.
    pub patch_count: usize,
    /// Number of original supported operations validated.
    pub original_operation_count: usize,
    /// Number of inverse operations generated.
    pub inverse_operation_count: usize,
    /// Unsigned inverse Patch object ID hint.
    ///
    /// This ID is only a deterministic planning hint for the unsigned payload. It is not a
    /// published object, and it is not sufficient authorization for rollback.
    pub inverse_patch_id_hint: ObjectId,
    /// Unsigned inverse Patch payload.
    pub inverse_payload: PatchPayload,
    /// Human-readable summary of inverse operations in application order.
    pub operations: Vec<PatchInverseOperationSummary>,
}

/// Summary of one inverse operation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PatchInverseOperationSummary {
    /// Operation sequence inside the inverse payload.
    pub op_seq: u32,
    /// Repository-relative path affected by the inverse operation.
    pub path: String,
    /// Inverse operation kind.
    pub kind: PatchInverseOperationKind,
}

/// Supported inverse operation kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PatchInverseOperationKind {
    /// Inverse operation creates a file.
    CreateFile,
    /// Inverse operation deletes a file.
    DeleteFile,
    /// Inverse operation replaces a binary blob.
    ReplaceBinary,
    /// Inverse operation performs a text edit.
    EditText,
    /// Inverse operation changes a node's mode bits (DC-73).
    ChangePerm,
}

impl PatchInverseOperationKind {
    /// Return a stable CLI label.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::CreateFile => "create-file",
            Self::DeleteFile => "delete-file",
            Self::ReplaceBinary => "replace-binary",
            Self::EditText => "edit-text",
            Self::ChangePerm => "change-perm",
        }
    }
}

/// Prepare an unsigned inverse Patch payload for the supported patch-operation subset.
pub fn prepare_patch_inverse_plan(
    layout: &RepositoryLayout,
    ref_name: &str,
) -> Result<PatchInversePlan> {
    let object_store = ObjectReadSnapshot::open(layout)?;
    let target_block_id = current_target_block(layout, &object_store, ref_name)?;
    let block_ids = single_parent_chain(&object_store, target_block_id)?;
    let mut files = BTreeMap::new();
    let mut live_nodes = BTreeMap::new();
    let mut inverse_operations = Vec::new();
    let mut patch_count = 0_usize;
    let mut original_operation_count = 0_usize;

    for block_id in &block_ids {
        let block = read_block(&object_store, *block_id)?;
        if let Some(snapshot_blob_ref) = block.snapshot_blob_ref {
            files = load_snapshot_files(&object_store, snapshot_blob_ref)?;
            live_nodes.clear();
            inverse_operations.clear();
            patch_count = 0;
            original_operation_count = 0;
        }
        for patch_id in block.patch_ids {
            let patch = read_patch(&object_store, patch_id)?;
            let operations =
                decode_patch_operations(&patch.canonical_payload, patch.schema_version)?;
            for operation in operations {
                let inverse = derive_inverse_operation(
                    &object_store,
                    &mut files,
                    &mut live_nodes,
                    operation,
                )?;
                inverse_operations.push(inverse);
                original_operation_count += 1;
            }
            patch_count += 1;
        }
    }

    inverse_operations.reverse();
    renumber_operations(&mut inverse_operations)?;
    let summaries = summarize_operations(&inverse_operations);
    let inverse_payload = PatchPayload {
        operations: inverse_operations,
        intent: None,
        preconditions: Vec::new(),
        purpose: PatchPurpose::Normal,
    };
    let inverse_payload_bytes = inverse_payload.to_canonical_bytes()?;
    let inverse_patch_id_hint = ObjectEnvelope::unsigned(
        ObjectType::Patch,
        PATCH_PARENT_IDS_RETIRED_SCHEMA,
        inverse_payload_bytes,
    )
    .object_id();

    Ok(PatchInversePlan {
        ref_name: ref_name.to_string(),
        target_block_id,
        block_count: block_ids.len(),
        patch_count,
        original_operation_count,
        inverse_operation_count: inverse_payload.operations.len(),
        inverse_patch_id_hint,
        inverse_payload,
        operations: summaries,
    })
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct InverseLiveNode {
    path: String,
    kind: NodeKind,
}

fn derive_inverse_operation(
    object_store: &impl ObjectReader,
    files: &mut BTreeMap<String, Vec<u8>>,
    live_nodes: &mut BTreeMap<NodeId, InverseLiveNode>,
    operation: DecodedPatchOperation,
) -> Result<Operation> {
    match operation.kind {
        DecodedOperationKind::CreateFile {
            path,
            node_id,
            blob_id,
            mode,
        } => {
            if files.contains_key(&path) {
                return Err(PrikkError::Integrity(format!(
                    "CreateFile would overwrite existing path {path}"
                )));
            }
            if live_nodes.contains_key(&node_id) {
                return Err(PrikkError::Integrity(
                    "CreateFile would introduce an already-live node_id".to_string(),
                ));
            }
            let (old_node_kind, bytes) = read_blob_bytes_with_kind(object_store, blob_id)?;
            files.insert(path.clone(), bytes);
            live_nodes.insert(
                node_id,
                InverseLiveNode {
                    path: path.clone(),
                    kind: old_node_kind,
                },
            );
            Ok(Operation {
                op_seq: 0,
                op_id: Some(format!("inverse-delete-{path}")),
                preconditions: Vec::new(),
                kind: OperationKind::DeleteNode(DeleteNode {
                    path,
                    node_id,
                    old_node_kind,
                    preimage: DeleteNodePreimage::File {
                        old_blob_id: blob_id,
                        old_mode: mode,
                    },
                }),
            })
        }
        DecodedOperationKind::DeleteNode {
            path,
            node_id,
            preimage:
                DecodedDeletePreimage::File {
                    old_node_kind,
                    old_blob_id,
                    old_mode,
                },
        } => {
            let old_bytes = files.get(&path).ok_or_else(|| {
                PrikkError::Integrity(format!("DeleteNode path is absent: {path}"))
            })?;
            crate::blob_access::ensure_blob_matches_node_kind(
                old_bytes,
                old_blob_id,
                old_node_kind,
            )?;
            files.remove(&path);
            if let Some(live) = live_nodes.remove(&node_id) {
                if live.path != path {
                    return Err(PrikkError::Integrity(format!(
                        "DeleteNode path {path} does not match live node path {}",
                        live.path
                    )));
                }
                if live.kind != old_node_kind {
                    return Err(PrikkError::Integrity(
                        "DeleteNode old_node_kind does not match live node kind".to_string(),
                    ));
                }
            }
            Ok(Operation {
                op_seq: 0,
                op_id: Some(format!("inverse-create-{path}")),
                preconditions: Vec::new(),
                kind: OperationKind::CreateFile(CreateFile {
                    path,
                    node_id,
                    blob_id: old_blob_id,
                    mode: old_mode,
                }),
            })
        }
        DecodedOperationKind::EditText {
            node_id,
            span_id,
            old_span_hash,
            left_anchor_hash,
            right_anchor_hash,
            replacement_text,
            old_span_text,
        } => {
            let live = live_nodes.get(&node_id).ok_or_else(|| {
                PrikkError::Integrity("EditText inverse target node is not live".to_string())
            })?;
            if live.kind != NodeKind::TextFile {
                return Err(PrikkError::Integrity(
                    "EditText inverse target node is not TextFile".to_string(),
                ));
            }
            let pre_text = files.get(&live.path).ok_or_else(|| {
                PrikkError::Integrity(format!(
                    "EditText inverse target path {} is absent",
                    live.path
                ))
            })?;
            let (inverse, post_text) = text_span::derive_inverse_edit_text(
                pre_text,
                node_id,
                &span_id,
                &old_span_hash,
                &left_anchor_hash,
                &right_anchor_hash,
                &replacement_text,
                &old_span_text,
            )?;
            files.insert(live.path.clone(), post_text);
            Ok(Operation {
                op_seq: 0,
                op_id: Some(format!("inverse-edit-text-{}", live.path)),
                preconditions: Vec::new(),
                kind: OperationKind::EditText(inverse),
            })
        }
        DecodedOperationKind::DeleteNode {
            preimage: DecodedDeletePreimage::Symlink { .. },
            ..
        } => Err(PrikkError::UnsupportedObjectType(
            "inverse planning for symlink DeleteNode is deferred".to_string(),
        )),
        DecodedOperationKind::ReplaceBinary {
            node_id,
            old_blob_id,
            new_blob_id,
        } => {
            let live = live_nodes.get(&node_id).ok_or_else(|| {
                PrikkError::Integrity("ReplaceBinary inverse target node is not live".to_string())
            })?;
            if live.kind != NodeKind::BinaryFile {
                return Err(PrikkError::Integrity(
                    "ReplaceBinary inverse target node is not BinaryFile".to_string(),
                ));
            }
            let path = live.path.clone();
            // `files` holds forward (original-history) state at this point in the walk — the
            // bytes *before* this original ReplaceBinary took effect, which must match its
            // `old_blob_id`, not `new_blob_id`.
            let current_bytes = files.get(&path).ok_or_else(|| {
                PrikkError::Integrity(format!(
                    "ReplaceBinary inverse target path {path} is absent"
                ))
            })?;
            crate::blob_access::ensure_blob_matches_node_kind(
                current_bytes,
                old_blob_id,
                live.kind,
            )?;
            let (new_kind, new_bytes) = read_blob_bytes_with_kind(object_store, new_blob_id)?;
            if new_kind != NodeKind::BinaryFile {
                return Err(PrikkError::Integrity(format!(
                    "ReplaceBinary new blob {new_blob_id} is not a binary-file blob"
                )));
            }
            // Advance `files` to this original operation's forward result, so a later operation
            // in the same walk sees correct "current" state.
            files.insert(path.clone(), new_bytes);
            Ok(Operation {
                op_seq: 0,
                op_id: Some(format!("inverse-replace-binary-{path}")),
                preconditions: Vec::new(),
                kind: OperationKind::ReplaceBinary(ReplaceBinary {
                    node_id,
                    old_blob_id: new_blob_id,
                    new_blob_id: old_blob_id,
                }),
            })
        }
        DecodedOperationKind::ChangePerm {
            node_id,
            old_mode,
            new_mode,
        } => {
            let live = live_nodes.get(&node_id).ok_or_else(|| {
                PrikkError::Integrity("ChangePerm inverse target node is not live".to_string())
            })?;
            let path = live.path.clone();
            Ok(Operation {
                op_seq: 0,
                op_id: Some(format!("inverse-change-perm-{path}")),
                preconditions: Vec::new(),
                kind: OperationKind::ChangePerm(ChangePerm {
                    node_id,
                    old_mode: new_mode,
                    new_mode: old_mode,
                }),
            })
        }
        // DC-73: unreachable in practice — nothing authors either kind (renames become
        // delete+create; symlink authoring is refused), so inverse stays deferred pending an
        // authoring path, not the node model.
        DecodedOperationKind::RenamePath { .. } => Err(PrikkError::UnsupportedObjectType(
            "inverse planning for RenamePath awaits a rename authoring path".to_string(),
        )),
        DecodedOperationKind::CreateSymlink { .. } => Err(PrikkError::UnsupportedObjectType(
            "inverse planning for CreateSymlink awaits a symlink authoring path".to_string(),
        )),
    }
}

fn renumber_operations(operations: &mut [Operation]) -> Result<()> {
    for (index, operation) in operations.iter_mut().enumerate() {
        let next = index
            .checked_add(1)
            .ok_or_else(|| PrikkError::CanonicalEncoding("operation count overflow".to_string()))?;
        operation.op_seq = u32::try_from(next).map_err(|_| {
            PrikkError::CanonicalEncoding("operation count exceeds u32".to_string())
        })?;
    }
    Ok(())
}

fn summarize_operations(operations: &[Operation]) -> Vec<PatchInverseOperationSummary> {
    operations
        .iter()
        .map(|operation| {
            let (kind, path) = match &operation.kind {
                OperationKind::CreateFile(value) => {
                    (PatchInverseOperationKind::CreateFile, value.path.clone())
                }
                OperationKind::DeleteNode(value) => {
                    (PatchInverseOperationKind::DeleteFile, value.path.clone())
                }
                OperationKind::EditText(_) => (
                    PatchInverseOperationKind::EditText,
                    operation
                        .op_id
                        .as_deref()
                        .and_then(|value| value.strip_prefix("inverse-edit-text-"))
                        .unwrap_or("<unknown>")
                        .to_string(),
                ),
                OperationKind::ReplaceBinary(_) => (
                    PatchInverseOperationKind::ReplaceBinary,
                    operation
                        .op_id
                        .as_deref()
                        .and_then(|value| value.strip_prefix("inverse-replace-binary-"))
                        .unwrap_or("<unknown>")
                        .to_string(),
                ),
                OperationKind::ChangePerm(_) => (
                    PatchInverseOperationKind::ChangePerm,
                    operation
                        .op_id
                        .as_deref()
                        .and_then(|value| value.strip_prefix("inverse-change-perm-"))
                        .unwrap_or("<unknown>")
                        .to_string(),
                ),
                OperationKind::RenamePath(_) | OperationKind::CreateSymlink(_) => {
                    unreachable!("inverse plan contains unsupported operation kind")
                }
            };
            PatchInverseOperationSummary {
                op_seq: operation.op_seq,
                path,
                kind,
            }
        })
        .collect()
}

#[cfg(test)]
mod tests;