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
//! v1.3.1 — pure model-level lockfile writer.
//!
//! This module mirrors the Lean theorem
//! `Grex.Lockfile.lockfile_branch_mirrors_manifest_ref` (B14, v1.3.1
//! Rule-8 gate) at the Rust impl boundary. It exposes a single
//! pure-data-transform function, [`write_entry`], that maps a parent
//! manifest's [`ChildRef`] into a freshly-shaped [`LockEntry`] whose
//! `branch` field carries `child.r#ref` (the manifest `ref:` value)
//! verbatim — the contract that v1.3.0 violated by emitting
//! `branch: ""` unconditionally.
//!
//! # Why a separate `writer` module
//!
//! The actual call-site that has historically been the bug is in
//! `crates/grex-core/src/sync.rs::upsert_lock_entry` (sister worker
//! W2+W4 owns the plumbing change that threads `manifest_ref` from the
//! walker down to that call site). Co-locating the pure data-transform
//! here keeps three goals decoupled:
//!
//! 1. **Lean fidelity.** `write_entry` is a pure function whose body
//! line-for-line matches `Grex.Lockfile.write_entry` in
//! `proof/Grex/Lockfile.lean`. Reviewers / theorem-prover bridges
//! can compare the two without traversing sync orchestration code.
//! 2. **Testability.** The fix's contract is testable with no
//! walker, no fixtures, no IO — just a `ChildRef` in, a
//! `LockEntry` out (see `crates/grex-core/tests/lockfile_branch_carry.rs`).
//! 3. **Worker scope.** The v1.3.1 W6 worker owns this file
//! exclusively; W2+W4 (sync-side plumbing) consumes the function
//! via the public re-export from
//! [`crate::lockfile::write_entry_from_child`].
//!
//! # Lean ↔ Rust correspondence
//!
//! | Lean (`Grex.Lockfile`) | Rust (`grex_core::lockfile::writer`) |
//! |------------------------------|--------------------------------------|
//! | `branchOf : Option String → String` | [`branch_of`] |
//! | `write_entry : ChildRef → LockEntryV131` | [`write_entry`] |
//! | `c.ref` | `child.r#ref` |
//! | `branchOf c.ref` | `branch_of(child.r#ref.as_deref())` |
//!
//! The Lean theorem (`lockfile_branch_mirrors_manifest_ref`) is a
//! pure-data-transform statement. The Rust impl realises it; no new
//! bridge axiom is required (see Lean module docstring).
use ;
use LockEntry;
use crateChildRef;
/// Lift `ChildRef.r#ref: Option<String>` into the `String` field of
/// `LockEntry.branch`. Mirrors the Lean `branchOf` function:
/// `None ↦ ""`, `Some s ↦ s`.
///
/// Centralising this rule (rather than inlining
/// `child.r#ref.clone().unwrap_or_default()` at every call site) keeps
/// the Lean ↔ Rust correspondence one symbol-pair wide, so future
/// changes to the empty-default convention land in exactly one place.
/// Build a [`LockEntry`] from a parent-manifest [`ChildRef`] plus the
/// resolved-state fields the walker / sync orchestrator hands in.
///
/// Implements the v1.3.1 fix for B14: the entry's `branch` field is
/// taken from `child.r#ref` (via [`branch_of`]). When the manifest
/// omits `ref:`, the empty string is recorded — matching the Lean
/// model's `branchOf : Option String → String` (`None ↦ ""`).
///
/// `id` and `path` are both seeded from the child's effective on-disk
/// directory name (the parent-relative POSIX path under which the
/// child's working tree lives). Callers that mount the same id under a
/// distinct path (rare, but legal under the v1.2.0 distributed
/// lockfile schema) should overwrite [`LockEntry::path`] after
/// construction — the field is `pub` and not `#[non_exhaustive]`-gated
/// from in-crate sites.
///
/// `synthetic` defaults to `false`. Plain-git-child synthesis (v1.1.1)
/// is plumbed through by sister worker W2+W4 at the sync call site,
/// not here — keeping this function a pure transform.
///
/// # Lean correspondence
///
/// ```text
/// def write_entry (c : ChildRef) : LockEntryV131 :=
/// { id := idOf c,
/// url := c.url,
/// branch := branchOf c.ref }
/// ```
///
/// The Rust impl carries the same three fields plus the resolved-state
/// columns (`sha`, `installed_at`, `actions_hash`, `schema_version`)
/// that the on-disk schema requires; the Lean model is intentionally
/// narrower since B14's contract is about the `branch` slot only.