Skip to main content

objects/transfer/
plan.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Shared repository transfer planning primitives.
3//!
4//! The wire protocol still carries the existing push/pull messages. This
5//! module gives local and hosted sync paths one Rust-native vocabulary for the
6//! Heddle object lane: content-addressed objects that can ride the native pack,
7//! and signed sidecars that must use the out-of-pack verification paths.
8
9use crate::{
10    error::Result,
11    object::StateId,
12    store::ObjectStore,
13    transfer::graph::{
14        ObjectInfo, ObjectType, ObjectTypeBucket, PlannedObject, StateClosureOptions,
15        enumerate_state_closure_plan_with_options,
16    },
17};
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
20pub enum GitLaneTransferIntent {
21    /// This transfer contains only Heddle content-addressed objects and
22    /// sidecars; no Git-lane work is expected.
23    #[default]
24    HeddleObjectsOnly,
25    /// Hosted Git-lane pack streaming remains on the current implementation.
26    /// The shared transfer plan records that fact without taking ownership of
27    /// reachable Git pack construction.
28    ExistingImplementation,
29    /// Placeholder for the future Sley facade boundary. Heddle should not grow
30    /// a second reachable-pack planner locally; once Sley exposes the needed
31    /// facade, this intent can become an executable Git-lane plan.
32    BlockedOnSleyReachablePackPlanning,
33}
34
35#[derive(Debug, Clone, PartialEq, Eq)]
36pub struct RepositoryTransferPlan<T = PlannedObject> {
37    pub partitions: TransferPartitions<T>,
38    pub stats: TransferPlanStats,
39    pub git_lane: GitLaneTransferIntent,
40}
41
42#[derive(Debug, Clone, PartialEq, Eq)]
43pub struct TransferPartitions<T = PlannedObject> {
44    pub packable_objects: Vec<T>,
45    pub sidecar_objects: Vec<T>,
46}
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
49pub struct TransferPlanStats {
50    pub total_objects: usize,
51    pub packable_objects: usize,
52    pub sidecar_objects: usize,
53    pub blobs: usize,
54    pub trees: usize,
55    pub states: usize,
56    pub actions: usize,
57    pub annotated_tags: usize,
58    pub redactions: usize,
59    pub purges: usize,
60    pub state_visibilities: usize,
61    pub state_attachments: usize,
62    pub key_bindings: usize,
63}
64
65impl RepositoryTransferPlan<PlannedObject> {
66    pub fn from_state_closure_plan(
67        store: &impl ObjectStore,
68        root: StateId,
69        options: StateClosureOptions,
70        git_lane: GitLaneTransferIntent,
71    ) -> Result<Self> {
72        let objects = enumerate_state_closure_plan_with_options(store, root, options)?;
73        Ok(Self::from_planned_objects(objects, git_lane))
74    }
75
76    pub fn from_planned_objects(
77        objects: impl IntoIterator<Item = PlannedObject>,
78        git_lane: GitLaneTransferIntent,
79    ) -> Self {
80        build_plan(objects, planned_object_type, git_lane)
81    }
82}
83
84impl RepositoryTransferPlan<ObjectInfo> {
85    pub fn from_object_infos(
86        objects: impl IntoIterator<Item = ObjectInfo>,
87        git_lane: GitLaneTransferIntent,
88    ) -> Self {
89        build_plan(objects, object_info_type, git_lane)
90    }
91}
92
93impl<T> RepositoryTransferPlan<T> {
94    pub fn requires_native_pack(&self, include_full_closure: bool) -> bool {
95        include_full_closure || self.stats.packable_objects > 0
96    }
97
98    pub fn is_heddle_only(&self) -> bool {
99        self.git_lane == GitLaneTransferIntent::HeddleObjectsOnly
100    }
101}
102
103impl<T> TransferPartitions<T> {
104    pub fn is_empty(&self) -> bool {
105        self.packable_objects.is_empty() && self.sidecar_objects.is_empty()
106    }
107
108    pub fn len(&self) -> usize {
109        self.packable_objects.len() + self.sidecar_objects.len()
110    }
111
112    pub fn iter(&self) -> impl Iterator<Item = &T> {
113        self.packable_objects
114            .iter()
115            .chain(self.sidecar_objects.iter())
116    }
117
118    pub fn is_sidecar_object_type(obj_type: ObjectType) -> bool {
119        !obj_type.packable()
120    }
121}
122
123impl<T> Default for TransferPartitions<T> {
124    fn default() -> Self {
125        Self {
126            packable_objects: Vec::new(),
127            sidecar_objects: Vec::new(),
128        }
129    }
130}
131
132impl TransferPlanStats {
133    fn record(&mut self, obj_type: ObjectType) {
134        self.total_objects += 1;
135        if TransferPartitions::<()>::is_sidecar_object_type(obj_type) {
136            self.sidecar_objects += 1;
137        } else {
138            self.packable_objects += 1;
139        }
140        match obj_type.bucket() {
141            ObjectTypeBucket::Blob => self.blobs += 1,
142            ObjectTypeBucket::Tree => self.trees += 1,
143            ObjectTypeBucket::State => self.states += 1,
144            ObjectTypeBucket::Action => self.actions += 1,
145            ObjectTypeBucket::AnnotatedTag => self.annotated_tags += 1,
146            ObjectTypeBucket::Redaction => self.redactions += 1,
147            ObjectTypeBucket::Purge => self.purges += 1,
148            ObjectTypeBucket::StateVisibility => self.state_visibilities += 1,
149            ObjectTypeBucket::StateAttachment => self.state_attachments += 1,
150            ObjectTypeBucket::KeyBinding => self.key_bindings += 1,
151        }
152    }
153}
154
155fn build_plan<T>(
156    objects: impl IntoIterator<Item = T>,
157    object_type: fn(&T) -> ObjectType,
158    git_lane: GitLaneTransferIntent,
159) -> RepositoryTransferPlan<T> {
160    let mut partitions = TransferPartitions::default();
161    let mut stats = TransferPlanStats::default();
162
163    for object in objects {
164        let obj_type = object_type(&object);
165        stats.record(obj_type);
166        if TransferPartitions::<T>::is_sidecar_object_type(obj_type) {
167            partitions.sidecar_objects.push(object);
168        } else {
169            partitions.packable_objects.push(object);
170        }
171    }
172
173    RepositoryTransferPlan {
174        partitions,
175        stats,
176        git_lane,
177    }
178}
179
180fn planned_object_type(object: &PlannedObject) -> ObjectType {
181    object.obj_type
182}
183
184fn object_info_type(object: &ObjectInfo) -> ObjectType {
185    object.obj_type
186}
187
188#[cfg(test)]
189mod tests {
190    use super::*;
191    use crate::{
192        object::{ContentHash, StateId},
193        transfer::graph::ObjectId,
194    };
195
196    fn hash(byte: u8) -> ContentHash {
197        ContentHash::from_bytes([byte; 32])
198    }
199
200    #[test]
201    fn partitions_split_native_pack_objects_from_sidecars() {
202        let state = StateId::from_bytes([9; 32]);
203        let plan = RepositoryTransferPlan::from_planned_objects(
204            vec![
205                PlannedObject {
206                    id: ObjectId::Hash(hash(1)),
207                    obj_type: ObjectType::Blob,
208                },
209                PlannedObject {
210                    id: ObjectId::Hash(hash(2)),
211                    obj_type: ObjectType::Tree,
212                },
213                PlannedObject {
214                    id: ObjectId::Hash(hash(1)),
215                    obj_type: ObjectType::Redaction,
216                },
217                PlannedObject {
218                    id: ObjectId::StateId(state),
219                    obj_type: ObjectType::StateVisibility,
220                },
221                PlannedObject {
222                    id: ObjectId::Hash(hash(3)),
223                    obj_type: ObjectType::KeyBinding,
224                },
225            ],
226            GitLaneTransferIntent::HeddleObjectsOnly,
227        );
228
229        assert_eq!(plan.partitions.packable_objects.len(), 2);
230        assert_eq!(plan.partitions.sidecar_objects.len(), 3);
231        assert_eq!(plan.stats.total_objects, 5);
232        assert_eq!(plan.stats.packable_objects, 2);
233        assert_eq!(plan.stats.sidecar_objects, 3);
234        assert_eq!(plan.stats.blobs, 1);
235        assert_eq!(plan.stats.trees, 1);
236        assert_eq!(plan.stats.redactions, 1);
237        assert_eq!(plan.stats.state_visibilities, 1);
238        assert_eq!(plan.stats.key_bindings, 1);
239        assert!(plan.requires_native_pack(false));
240    }
241
242    #[test]
243    fn sidecar_only_plan_does_not_require_native_pack() {
244        let state = StateId::from_bytes([3; 32]);
245        let plan = RepositoryTransferPlan::from_object_infos(
246            vec![ObjectInfo {
247                id: ObjectId::StateId(state),
248                obj_type: ObjectType::StateVisibility,
249                size: 128,
250                delta_base: None,
251            }],
252            GitLaneTransferIntent::HeddleObjectsOnly,
253        );
254
255        assert!(!plan.requires_native_pack(false));
256        assert!(plan.requires_native_pack(true));
257        assert_eq!(plan.stats.packable_objects, 0);
258        assert_eq!(plan.stats.sidecar_objects, 1);
259    }
260
261    #[test]
262    fn git_lane_intents_name_current_and_sley_gated_paths() {
263        let hosted = RepositoryTransferPlan::from_planned_objects(
264            Vec::<PlannedObject>::new(),
265            GitLaneTransferIntent::ExistingImplementation,
266        );
267        let sley_blocked = RepositoryTransferPlan::from_planned_objects(
268            Vec::<PlannedObject>::new(),
269            GitLaneTransferIntent::BlockedOnSleyReachablePackPlanning,
270        );
271
272        assert_eq!(
273            hosted.git_lane,
274            GitLaneTransferIntent::ExistingImplementation
275        );
276        assert_eq!(
277            sley_blocked.git_lane,
278            GitLaneTransferIntent::BlockedOnSleyReachablePackPlanning
279        );
280        assert!(!hosted.is_heddle_only());
281        assert!(!sley_blocked.is_heddle_only());
282    }
283}