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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
//! The store facade's value types (store-api.md §Public surface): the
//! shapes put/get/stat/sweep return, the pin lifecycle vocabulary, and
//! the registered-liveness vocabulary. Engine-blind — nothing here
//! knows which engines a store runs (ADR-007/008/010); these types are
//! the contract the facade behavior implements and every consumer
//! programs against.
//!
//! **The pin types ([`Pin`], [`Batch`]) carry their registry
//! back-reference.** Each guard holds an `Arc` to the store instance's
//! GC registry core ([`crate::gc`]), so a guard's release reaches the
//! per-key refcounts under the arbitration lock wherever the guard
//! goes — the wave-1 shape carried only the key. The guard's drop
//! decrements its refcount; a [`Batch`] holds its members' keys and
//! releases every one of them in **one** ordered step under the same
//! lock on commit or drop (no early single-pin release). The guard
//! types never expose the registry consumer-side: the Arc is private,
//! minting happens on the store core only.
//!
//! **The registered-liveness vocabulary is consumer-owned shared
//! state, never a snapshot.** [`LivenessSource`] and
//! [`ProtectCallback`] are the two consumer-side halves of ADR-005's
//! three liveness sources (the pins are the third); both are
//! registered through the facade and are consulted *live* — at mark
//! time and at delete-time arbitration — never copied at registration
//! (POC #1 finding 7's install-implies-copy failure is the pinning
//! reason). A [`Registration`] is the deregistration token the
//! correction flows of ADR-012 §6.4 need.
use fmt;
use Arc;
use SystemTime;
use crateReadCursor;
use crateRegistryCore;
use crateAlgorithm;
use crateKey;
use crateAnyCursor;
/// The `get` seam's public stream (`(len, stream)` — store-api.md §Read
/// path): a bounded, re-entrant **windowed reader** over a stored entry,
/// uniform across both tiers with the crate-internal read cursor
/// (ADR-010 §1) staying behind it. The kv tier's windows slice
/// materialized content (bounded by tier policy); the large tier's read
/// window-by-window over the committed file — streaming without ever
/// materializing the whole value. `read_at` is a pread-style bounded
/// window (over-reads clamp at the entry's end; a read at EOF is empty,
/// never an error); `read_range` is the same window plus the ADR-006
/// slice digest — plain SHA-256 over exactly the returned bytes, fixed
/// by convention.
///
/// Constructed by the store core only (via `get`); engine cursors never
/// leak onto this surface. A `Blob` stays valid across concurrent pool
/// activity because committed entries are immutable. No verification
/// rides the windows themselves: canonical verification fires at get
/// time on the kv tier's materialized reads (and at put time), per the
/// store core's recorded verification-on-get posture (ADR-006).
/// The cheap stat probe's result (store-api.md §Read path): the entry's
/// length and its hash algorithm — the "type" at this layer is the hash
/// algorithm, there is no other (ADR-002). `stat` rides the Backend
/// trait's `size` method (ADR-008): an indexed-column probe on kv
/// engines, a file stat on `local`, a companion-row probe on pg-lo —
/// never a value/content load.
/// The RAII put-path pin guard (ADR-005 §Marks 2; store-api.md §Write
/// path).
///
/// **Lifecycle contract (ADR-005; the machinery is wired):**
///
/// - Acquired **before the entry becomes visible** — pin-before-publish:
/// an entry cannot appear in a sweep's `list()` without already
/// carrying its pin (or its converted registration, ADR-005's put-side
/// invariant).
/// - **Back-reference shape (named design point):** the guard holds an
/// `Arc` to the store's GC registry core (`RegistryCore`,
/// crate-internal) alongside
/// its key, so the drop reaches the per-key refcounts under the
/// arbitration lock the sweep consults — wherever the guard travels.
/// The registry core outlives the `Store` instance when guards still
/// reference it; a late release against a detached registry is
/// harmless (the refcount bookkeeping is self-contained).
/// - The guard holds a per-key refcount under the same arbitration lock
/// the sweep consults, until **dropped or converted into a registered
/// consumer reference** (a registered liveness source or a root tag).
/// Conversion goes through the registry's ordered-step primitive
/// (`RegistryCore::convert`, crate-internal): the pin's release and
/// the replacement's registration into a liveness source are one
/// ordered step under the same lock — a consumer may not release a
/// pin before its registered source observes the replacement (the
/// local analog of the ops-layer pin-token contract).
/// - A dedup put's guard protects exactly as a fresh put's does: a
/// re-put acquires no new pool entry but refcounts the existing one
/// under the same arbitration lock (ADR-005).
/// - Fleet correspondence (ADR-010 §4, ADR-012 §2): in fleet mode the
/// joint tx commits the expiry-carrying pin row alongside the entry,
/// and **the guard carries the row's renewal duty while it lives** —
/// the store's TTL/3 schedule (ADR-008 §2; the landed
/// `store-fleet-put` design point: a per-guard tokio task, aborted on
/// the guard's drop/conversion/batch contribution, silent on a failed
/// renewal, ended by the never-resurrect `Ok(false)` — the row's
/// expiry is the once-protection after the guard leaves). A guard
/// that outlives its store instance keeps renewing through the
/// registry-adjacent engine handle it carries (same detachment as the
/// refcount bookkeeping); in non-fleet modes the ops layer's token is
/// this guard's in-process handle — nothing expires outside fleet
/// mode.
/// The fleet pin row's renewal duty — the guard's TTL/3 renewal task's
/// handle (store-fleet-put's named design point: a **per-guard tokio
/// task**; the shared-renewal-sweep keyed off a guard registry was the
/// rejected alternative — it needs the same lifecycle notifications an
/// abort handle gives for free). The task moves the row's expiry
/// forward every TTL/3 while it lives; wherever the guard's life or
/// ownership ends (drop, batch contribution, conversion), the handle
/// aborts it — no leaked tasks, no renewal past the guard's life.
/// Crate-internal, minted by the store core's fleet put path only.
pub
/// Batch-scoped pin group (ADR-005 §Marks 2; store-api.md): multi-put
/// writes — manifest writes are exactly this — group under one
/// batch-scoped pin.
///
/// **Lifecycle contract (ADR-005; the machinery is wired):**
///
/// - Created by the store core's batching put seam; each put within the
/// batch contributes its pin (batch-internal puts publish
/// pin-before-publish exactly as lone puts do).
/// - **A batch's pins drop together on batch commit or batch drop**, in
/// one ordered step under the same arbitration lock the sweep
/// consults. No early single-pin release: protection spans the whole
/// batch or none of it. The batch holds its members' keys; the
/// release takes the arbitration lock once and decrements every
/// member refcount inside that one section.
/// - Batch drop is the failure path (stream error, caller abandonment):
/// every member pin releases together; member liveness never outlives
/// the batch.
/// A sweep's abort cause — report data only, never an error (ADR-012 §1:
/// sweeps are never errors; an aborted sweep returns `Ok(SweepReport)`
/// with `aborted: Some(cause)`, and there is no sweep-abort error
/// variant).
///
/// Exactly three variants, ADR-pinned; new causes are new ADRs.
/// `#[non_exhaustive]` makes the ADR-012 §1 ruling — callers match
/// non-exhaustively — structural for downstream crates.
/// A key's pin classification (ADR-010 §2): the engine's only vocabulary
/// for pins — the engine never decides deletion, it only classifies, and
/// the store core receives the answer and applies the decision.
/// A sweep's outcome record — always data, never an error carrier
/// (ADR-012 §1). Counts are exact; the test gate asserts sweep outcomes
/// against these fields (store-api.md invariants 4/7).
/// A registered liveness source — the live-shared GC seam (ADR-005
/// §Marks 1; store-api.md §Lifecycle, `register_liveness_source`).
///
/// **Named design point — the source shape: two sync verbs, live-shared
/// consults, none blocking.** The source is consumer-owned state shared
/// by handle (`Arc<dyn LivenessSource>`); the store consults the
/// *current* contents every time and never snapshots at registration —
/// POC #1 finding 7's install-implies-copy failure (roots registered
/// after install were invisible to GC) is the pinning reason. The two
/// verbs split by protocol phase:
///
/// - [`live_keys`](Self::live_keys) — mark-time enumeration, consulted
/// outside the arbitration lock (whole live-set computation);
/// - [`contains`](Self::contains) — delete-time arbitration probe,
/// consulted **under the in-process arbitration lock** (the pin/
/// liveness lock), so it must answer from an indexed structure and
/// never block or await; the provided default derives it from
/// [`live_keys`](Self::live_keys) (correct but linear — indexed
/// sources override).
///
/// The verbs are sync (non-async) by the same lock discipline: pin
/// drops are sync RAII, and arbitration consults run under a
/// short-held `parking_lot` lock (store-api.md §Concurrency posture;
/// AGENTS convention 3). A source whose set changes concurrently with
/// a sweep is the consumer's coordination duty — the delete-time
/// re-observation composes the sources' live-shared answers, and the
/// ADR-005 Consequences assign pin/liveness misuse to the consumer
/// contract (store-api.md's invariants document it).
/// A registered protect callback — ADR-005 §Marks 3's protection
/// source, consulted pre-sweep and at direct-delete arbitration.
///
/// **Named design point — the callback shape: one sync verb returning
/// the protection set or a failure to answer.** `None` means the
/// protection source could not answer, and every consumer of that
/// answer takes the safe side (iroh's `ProtectOutcome::Abort`
/// conclusion, adopted): a sweep aborts with
/// [`GcAbortCause::ProtectFailed`], nothing deleted (ADR-012 §1:
/// abort is report data); a direct delete refuses conservatively with
/// `Error::GcRefuse` — nothing deleted either way. The verb is sync by
/// the same discipline as [`LivenessSource`] (direct-delete
/// arbitration runs it under the arbitration lock — quick, never
/// blocking); mark-time consults run outside the lock and may be
/// slower.
/// A registration token — the opaque handle the store's
/// `unregister_liveness_source`/`unregister_protect_callback` verbs
/// consume to remove a registered [`LivenessSource`], a
/// [`ProtectCallback`], or a fleet liveness table registration (the
/// [`LivenessRegistration`] verb's fleet form). ADR-012 §6.4's
/// embedder correction flow ("deregisters/re-registers around the
/// delete") is exactly this: the token makes deregistration
/// expressible without exposing the registry internals.
///
/// Copyable, but each token deregisters at most once (the second call
/// returns `false`).
/// The registration record's kind — crate-internal bookkeeping behind
/// [`Registration`], routing the deregistration verbs (a source token
/// never deregisters a protect callback, and so on).
pub
/// The liveness registration's **argument form** — ADR-012 §6.6's
/// fleet-shaped argument riding the same verb
/// (`register_liveness_source`): the two legal forms of registering a
/// liveness source, single-node and fleet.
///
/// **Named design points (recorded):**
///
/// - **The verb's shape and error surface.** The fleet form is not a
/// sibling method: ADR-012 §6.6's "same verb" wording pins the
/// argument-form reading, so this type IS the verb's argument and
/// the verb returns `Result<Registration, Error>` — the pre-fleet
/// signature's infallibility was an accident of the in-process form
/// alone (registration bookkeeping cannot fail there); the fleet
/// form validates the table reference (malformed references error
/// at registration, never interpolate unquoted) and forwards to the
/// shared engine, so the unified signature carries the fleet form's
/// fallibility. The in-process form never errors in practice.
/// - **In-process sources remain legal under `fleet: true`**
/// (allow-and-document): an instance-local registration's
/// protection is instance-local only — it covers *this* instance's
/// sweeps (mark-time enumeration) and *this* instance's direct
/// deletes (the in-process domain's probes); it is not a *fleet*
/// liveness form (ADR-008 §2: the SQL protocol sees tables, not
/// callbacks), so the embedder's fleet-wide protection must
/// materialize into its liveness table — or pin rows — before the
/// fleet's other instances sweep. The two domains compose: the
/// in-process registrations protect locally, the registered table
/// protects fleet-wide.