|
| 1 | +# 8. Lockfile Data Model and Synchronization Refactor |
| 2 | + |
| 3 | +- **Status:** Proposed |
| 4 | +- **Deciders:** eka-devs |
| 5 | +- **Date:** 2025-10-13 |
| 6 | + |
| 7 | +## Context and Problem Statement |
| 8 | + |
| 9 | +The initial implementation of the ADR#7 manifest and lockfile format has revealed several areas of unnecessary complexity and inefficiency in the underlying data models. The primary issues are: |
| 10 | + |
| 11 | +1. **Redundant Identity Types:** The `lock.rs` module defines an `AtomDigest` struct to represent an atom's cryptographic ID, which is conceptually redundant with the `IdHash` struct defined in the canonical `id` module. This creates a confusing and error-prone translation layer. |
| 12 | +2. **Inefficient In-Memory Lockfile Structure:** The lockfile is currently deserialized into a `BTreeSet`, which requires inefficient linear scans for lookups and updates. This data structure will not scale and makes implementing future features, like transitive dependency resolution, overly complex. |
| 13 | +3. **Complex Synchronization Logic:** The existing synchronization logic is difficult to implement and maintain due to the inefficient data structures and redundant types. |
| 14 | + |
| 15 | +This ADR proposes a refactoring of the core data models to address these issues, creating a more elegant, efficient, and robust system for managing lockfiles. |
| 16 | + |
| 17 | +## Decision |
| 18 | + |
| 19 | +We will refactor the core identity types and the in-memory representation of the lockfile. The implementation will be conducted in three phases. |
| 20 | + |
| 21 | +### Phase 1: Refactor Core Identity Types |
| 22 | + |
| 23 | +The goal of this phase is to create a single, canonical representation for an atom's cryptographic ID. |
| 24 | + |
| 25 | +- **Step 1.1:** In `crates/atom/src/id/mod.rs`, the ephemeral `IdHash` struct will be renamed to `IdHashView` to clarify its purpose as a temporary, referenced view of a hash. |
| 26 | +- **Step 1.2:** A new public, storable struct, `pub struct Id([u8; 32]);`, will be created in `crates/atom/src/id/mod.rs`. This will become the canonical, owned type for an atom's cryptographic ID. |
| 27 | +- **Step 1.3:** The `AtomId::compute_hash()` method will be modified to return this new `id::Id` struct. |
| 28 | +- **Step 1.4:** The redundant `AtomDigest` struct will be completely removed from `crates/atom/src/lock.rs`. |
| 29 | +- **Step 1.5:** The `AtomDep` struct in `crates/atom/src/lock.rs` will be updated to use the new `id::Id` for its `id` field. |
| 30 | + |
| 31 | +### Phase 2: Redesign In-Memory Lockfile Structure |
| 32 | + |
| 33 | +This phase will optimize the in-memory data structure for the lockfile for efficient lookups. |
| 34 | + |
| 35 | +- **Step 2.1:** The `Lockfile` struct in `crates/atom/src/lock.rs` will be modified. The `DepMap` (a `BTreeSet`) will be replaced with separate `BTreeMap` collections for each major dependency type. |
| 36 | +- **Step 2.2:** The primary map will be `atoms: BTreeMap<id::Id, AtomDep>`, providing efficient O(log n) lookups. Similar maps will be added for Nix dependencies, keyed by their `Name`. |
| 37 | +- **Step 2.3:** Custom `Serialize` and `Deserialize` implementations for the `Lockfile` struct will be created to handle the conversion between the efficient in-memory `BTreeMap` representation and the readable on-disk flat list of `[[input]]` tables. |
| 38 | + |
| 39 | +### Phase 3: Implement the New Synchronization Algorithm |
| 40 | + |
| 41 | +With the new data models in place, we will implement a clear and robust synchronization algorithm. |
| 42 | + |
| 43 | +- **Step 3.1:** Implement the loading logic to parse `atom.toml` and deserialize `atom.lock` into the new `BTreeMap`-based `Lockfile` structure. |
| 44 | +- **Step 3.2:** Implement the reconciliation logic. This will iterate through manifest dependencies, compute the expected `id::Id` for each, and use the `BTreeMap` for efficient lookups to add, update, or verify entries against the loaded lockfile. |
| 45 | +- **Step 3.3:** Implement pruning logic to remove any stale entries from the lockfile that are no longer present in the manifest. |
| 46 | +- **Step 3.4:** Implement the final write logic that serializes the in-memory `Lockfile` back to the `atom.lock` on-disk format. |
| 47 | + |
| 48 | +## Consequences |
| 49 | + |
| 50 | +- **Positive:** |
| 51 | + |
| 52 | + - **Reduced Complexity:** Eliminates redundant types and simplifies the conceptual model. |
| 53 | + - **Improved Performance:** The `BTreeMap` structure provides efficient lookups, making the system more scalable. |
| 54 | + - **Increased Robustness:** A clearer data model and algorithm will be less prone to bugs and easier to maintain. |
| 55 | + - **Future-Proofing:** Provides a solid foundation for future features like transitive dependency resolution. |
| 56 | + |
| 57 | +- **Negative:** |
| 58 | + - **Implementation Effort:** This is a significant refactoring that will require careful implementation and testing. |
0 commit comments