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
//! The rows a cache hit needs, published into the store.
//!
//! # Why this exists, and what D43 got wrong
//!
//! D43 says the SQLite index "is rebuildable from the store, which it can
//! be because everything in it derives from content hashes". Held against
//! `cache.rs`, that is **false**. A step's key is a hash of its
//! ingredients — code, parameters, input hashes, tool versions — and no
//! object in the store carries any of them, so a bag of objects cannot say
//! which key produced which. `remotes` is keyed by a locator, which is not
//! a hash of anything either.
//!
//! The consequence is the whole of this module. A clean machine pointed at
//! a shared bucket computes exactly the right step key, finds the output
//! sitting in the bucket under exactly the right name, and still has no
//! way to know they belong together. So the rows are published too: each
//! one an object, named for the key it answers to.
//!
//! # These are claims, and that is the one thing to be clear about
//!
//! An object's name is the hash of its contents, so an object's name is a
//! claim that can be checked, and [`BlobStore::checked_get`] checks it. A
//! row is different. It says "key K produced object O", and nothing short
//! of running the step can check that. A row is therefore **the one object
//! class whose name is not the hash of its contents**, and it gets the
//! verification it can have rather than the one it cannot:
//!
//! - the body restates the table and the key, and a body that restates the
//! wrong ones is refused, so a row cannot be served in answer to a
//! question it was not written for;
//! - the object it points at is still verified byte for byte on the way in
//! ([`super::tiered`]), so a shared store can never hand over corrupt
//! data.
//!
//! What remains is that **a shared store is as trustworthy as write access
//! to it**. Someone who can write your bucket can claim that step K
//! produced some other intact object, and you would believe them. That is
//! the same trust boundary a Nix binary cache has — Nix answers it with
//! signatures, which v0.2 does not have — and it is written down here,
//! in the architecture page and in D43 rather than left to be discovered.
//!
//! The practical reading: point Ligase at a bucket your lab writes, not at
//! one the internet writes.
use crateContentHash;
use ;
/// Separates a row's name from every other hash in the engine.
const DOMAIN: &str = "ligase.published-row";
/// The format of a row's body.
///
/// A row written by another version is *ignored* rather than read, exactly
/// as a cache entry from another key format is: it describes something
/// that was put together differently, and the cost of ignoring it is one
/// recomputation.
///
/// Version 2 (M18) adds the output's canonical hash to a `steps` row, so
/// that a worker whose publish was dropped can tell at no cost whether the
/// machine that got there first *means* the same thing it does (D52).
///
/// This number is in [`key_of`]'s hash, and that is not decoration — see
/// the note there. It was not, until the first bump needed it.
const ROW_VERSION: u32 = 2;
/// Step entries: which key produced which output.
pub const STEPS: &str = "steps";
/// Pinned remotes: which locator resolved to which objects.
pub const REMOTES: &str = "remotes";
/// Where a row lives in the store.
///
/// Not the hash of the row's contents — the hash of the *question* it
/// answers, because the question is all a cold machine has. The `.row`
/// suffix keeps the two classes apart in any listing, and the domain
/// string keeps a row from ever colliding with an object.
///
/// # Why [`ROW_VERSION`] is in here
///
/// Because objects are **write-once**, and a format version that is not in
/// the name of the thing it versions can only be discovered by the reader
/// — which in a write-once store is already too late.
///
/// Until M18 this hashed the domain, the table and the key alone, so every
/// format competed for one address. A bucket holding a version 1 row would
/// have refused every version 2 row written to that key: the `put` is
/// dropped because something is already there, the read finds a version
/// this build will not parse, [`fetch`] answers `None`, and that step key
/// becomes **permanently unpublishable** by anyone, for as long as the
/// bucket exists. Not one recomputation — all of them, for ever.
///
/// The bug was latent in M14 rather than introduced here: it is
/// unreachable until the first format bump, and M18 is the first format
/// bump. With the version in the address each format addresses its own
/// rows, old rows are never read instead of being read wrongly, and the
/// next bump costs one recomputation as the doc comment above promises.
/// What the store holds for a row once it has been published.
///
/// Write-once means a `put` cannot say whether it wrote: an object already
/// held is kept, and the call succeeds either way. So publishing reads
/// back, and this is what it found.
///
/// The distinction exists for M18. A step key fixes its ingredients, so two
/// machines publishing one key have done the same work — but not
/// necessarily to the same bytes, and in the cases D52 names not even to
/// the same *meaning*. Which of those it is has to be decided by somebody,
/// and the store is the right somebody: it is the only layer that knows
/// what is actually there.
/// Write a row, so a machine with an empty index can learn it.
///
/// Write-once like every other object: a row already published is left
/// alone. That is right rather than merely convenient — a step key fixes
/// its ingredients, so two rows under one key would have to agree.
///
/// Says nothing about what was already there. Use [`publish`] where that
/// matters, and note what it says about *which* store to ask.
///
/// # Errors
///
/// If the row cannot be written.
/// Write a row and say what the store holds for it afterwards.
///
/// # Ask the right store
///
/// The read-back is only worth anything against the store **every machine
/// sees** ([`super::TieredStore::shared`]). Asked of a tiered store, it
/// reads this machine's own local tier, finds this machine's own row, and
/// answers [`Publication::Ours`] every time — which is how M18's first
/// implementation managed to be blind to every divergence while looking
/// correct.
///
/// # Errors
///
/// If the row cannot be written, or cannot be read back.
/// Read a row back, refusing one that answers a different question.
///
/// `None` for a row that is not there, and for one written by a version
/// this build does not read.
///
/// # Errors
///
/// [`StoreError::Impostor`] if the row restates a table or key other than
/// the one it was found under — which means the store holds a row someone
/// wrote by hand or by accident, and serving it would be worse than
/// recomputing. Or an I/O failure.
/// Tabs, newlines and backslashes, so no field can shift a column.