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
//! The last build version this install reported under.
//!
//! An upgrade is invisible to every other signal here. `install_first_run`
//! fires once and never again, and `serve_started` carries the current
//! version but says nothing about what came before it — so an install that
//! upgraded and one that never moved look identical unless it happens to
//! serve. Recording the last-seen version turns that into an explicit
//! [`Event::InstallUpdated`](crate::Event::InstallUpdated).
//!
//! Comparing against stored state rather than hooking the updater is what
//! makes this cover every route a new binary can arrive by: `--auto-update`,
//! `mesh-llm update`, a re-run of `install.sh`, a package manager, or a
//! hand-swapped binary. The updater does not have to cooperate, and a process
//! that `exec`s itself away mid-update does not have to deliver anything: the
//! next run observes the change and reports it with a normal lifetime and a
//! normal flush.
//!
//! # Reporting exactly once
//!
//! The transition is a read-compare-write across processes, and an upgrade
//! must produce one `install_updated`, not none and not several. Three things
//! hold that together:
//!
//! - **A lock.** Several mesh-llm processes can start at once after an
//! upgrade. Without a lock each would read the old version and each would
//! report the same transition. The read and the write happen under one
//! exclusive lock on the state directory, and a process that cannot take it
//! reports nothing rather than racing.
//! - **An atomic replace.** The record is written to a temporary file and
//! renamed over the old one, so a crash mid-write leaves the previous
//! version intact instead of a truncated file that reads as no record.
//! - **Commit before report.** [`PendingVersion::commit`] returns whether the
//! record now actually reflects this build, and the caller only reports the
//! upgrade when it does. A write that failed leaves the old version in
//! place, so the next run sees the same transition again and can retry —
//! the opposite of the old behaviour, which reported a change it had failed
//! to persist and then reported it again on every subsequent run.
use ;
use Write;
use ;
/// File name inside the mesh-llm state directory.
pub const VERSION_FILE: &str = "analytics-version";
/// Lock held across the read-compare-write.
const VERSION_LOCK_FILE: &str = "analytics-version.lock";
/// Temporary file the new record is staged in before being renamed into place.
const VERSION_TEMP_FILE: &str = "analytics-version.tmp";
/// What changed about the build version since the last run.
/// A read transition that has not been written back yet.
///
/// Holds the state-directory lock until it is committed or dropped, so no
/// other process can observe the same transition in the meantime.
/// Read the recorded version, holding the state lock until the caller commits.
///
/// Best effort by design: a state directory that cannot be locked or read
/// yields an inert [`PendingVersion`], which reports nothing. The alternative
/// — treating an unreadable directory as a version change — would report an
/// upgrade on every single run.
///
/// The version is stored verbatim rather than sanitized. It is compared for
/// equality here and passed through
/// [`Label::sanitize_or_redact`](crate::Label::sanitize_or_redact) before it
/// reaches the wire, so a build environment that stamped something strange
/// into the version cannot smuggle it into a property.
/// Read the recorded version, or `None` when there is no record yet.
///
/// Separates "no record" (a missing or empty file) from "cannot read the
/// record" so the caller can decline to overwrite a record it could not read.
/// Replace the record by renaming a fully written temporary file over it.
///
/// A direct write truncates first, so a crash between truncate and write
/// leaves an empty record that reads as "no version ever seen" — which would
/// silently swallow the next upgrade. `rename` within one directory is atomic,
/// so a reader sees either the old record or the new one.
///
/// The fixed temporary name is safe because the caller holds the exclusive
/// lock for the whole read-commit window.