prolly-map 0.2.0

Content-addressed versioned map storage primitives.
Documentation
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
# Cookbook: build applications with prolly trees

This cookbook shows how to use `prolly-map` as a storage layer for local-first apps, AI memory, retrieval metadata, derived indexes, and durable embedded state. Each recipe explains the problem, the prolly-tree design, the reference example, and future enhancements you can add in your own application.

The examples use Rust today. The storage patterns are byte-oriented and portable, so future Python or TypeScript ports should keep the same key ordering, value envelopes, root naming, and conflict semantics.

## Recipe index

- [Shared conventions]#shared-conventions
- [Git-like primitives for applications]#git-like-primitives-for-applications
- [Basic ordered map]#basic-ordered-map
- [Bulk import and tree statistics]#bulk-import-and-tree-statistics
- [Local-first application state]#local-first-application-state
- [Diff and merge for branches]#diff-and-merge-for-branches
- [Delete-aware merge resolvers]#delete-aware-merge-resolvers
- [Conflict-free custom merge]#conflict-free-custom-merge
- [Conversation memory]#conversation-memory
- [Agent event logs]#agent-event-logs
- [Background compaction and retention-aware GC]#background-compaction-and-retention-aware-gc
- [Deterministic RAG snapshots]#deterministic-rag-snapshots
- [Document chunk indexes]#document-chunk-indexes
- [Vector sidecars]#vector-sidecars
- [Provenance-rich values]#provenance-rich-values
- [Secondary indexes from diffs]#secondary-indexes-from-diffs
- [Materialized views from diffs]#materialized-views-from-diffs
- [Large values and file blob storage]#large-values-and-file-blob-storage
- [Filesystem snapshots with named roots]#filesystem-snapshots-with-named-roots
- [Durable SQLite setup]#durable-sqlite-setup
- [Object-store backed snapshots]#object-store-backed-snapshots
- [Browser and WASM storage]#browser-and-wasm-storage

## Shared conventions

Use these conventions before you choose a recipe. They keep roots, keys, and values understandable across features and future language ports.

### Build byte-stable keys

`prolly-map` orders keys with raw byte lexicographic ordering. Use `KeyBuilder` for tuple-like keys so numeric and timestamp segments sort correctly.

```rust
use prolly::KeyBuilder;

let key = KeyBuilder::new()
    .push_str("tenant")
    .push_str("t1")
    .push_str("conversation")
    .push_str("c42")
    .push_str("event")
    .push_timestamp_millis(1_783_036_805_000)
    .push_u64(5)
    .finish();
```

Use these key conventions:

- Put a stable domain first: `app`, `conversation`, `agent-log`, `rag`, `doc-index`, or `materialized-view`
- Put the tenant, workspace, corpus, conversation, or run ID near the front when you scan by it
- Use `push_u64`, `push_i64`, and timestamp helpers instead of decimal strings for ordered numbers
- Use `prefix_range(prefix)` for prefix scans instead of hand-writing an end key
- Add a unique suffix when a prefix can contain more than one record with the same logical time

### Store typed values

The tree stores bytes. Use `VersionedValue` when the value can outlive one release.

```rust
use prolly::{Error, VersionedValue};
use serde::{Deserialize, Serialize};

const MEMORY_SCHEMA: &str = "ai.memory.record";
const MEMORY_VERSION: u64 = 1;

#[derive(Serialize, Deserialize)]
struct MemoryRecord {
    subject: String,
    fact: String,
}

fn encode_memory(record: &MemoryRecord) -> Result<Vec<u8>, Error> {
    VersionedValue::json(MEMORY_SCHEMA, MEMORY_VERSION, record)?.to_bytes()
}
```

Use value envelopes to record:

- schema name and version
- parser or model versions
- source URI and source content IDs
- timestamps from the application domain
- references to large values or sidecar systems

### Publish roots by name

Raw `Tree` values are immutable snapshots. Named roots are mutable application pointers.

Map writes do not move those pointers. `put`, `delete`, `batch`, and `merge`
return a new `Tree`; the named root still points at the old tree until your
application publishes the new handle. Treat this as a two-step workflow:

```text
edit tree -> get new immutable tree
publish/CAS named root -> make that tree visible as the head
```

Recommended root patterns:

```text
app/<app-id>/root/main
app/<app-id>/device/<device-id>/head
conversation/<conversation-id>/root/main
agent-log/<run-id>/root/events/current
rag/corpus/<corpus-id>/root/index/current
materialized-view/root/source/current
```

Use `compare_and_swap_named_root` when more than one writer can publish the same root name.

```rust
let current = prolly.load_named_root(b"main")?;
let base = current.clone().unwrap_or_else(|| prolly.create());
let next = prolly.put(&base, b"key".to_vec(), b"value".to_vec())?;

let update = prolly.compare_and_swap_named_root(
    b"main",
    current.as_ref(),
    Some(&next),
)?;
```

If the update is not applied, another writer published first. Reload the named root, merge or reapply your change, then retry.

### Prefer structural APIs

The tree is designed to skip unchanged content-addressed subtrees. Prefer:

- `range` or `range_page` over full-tree reads
- `diff` over comparing decoded application objects
- `batch` over repeated single-key edits
- `merge` over last-writer-wins at the root pointer
- `plan_missing_nodes` and `copy_missing_nodes` over exporting full snapshots
- retention-aware garbage collection (GC) over deleting files directly

## Git-like primitives for applications

You can build Git-like behavior on top of `prolly-map`, but the boundaries are
different from Git:

- A `Tree` is the content-addressed snapshot of an ordered map.
- A named root is a mutable pointer to one `Tree`.
- A commit is an application-defined record if you need parents, messages,
  authors, signatures, policies, or a reflog.

The table below shows useful primitives and how to compose them.

| Primitive | Suggested shape | Prolly operations |
| --- | --- | --- |
| Head resolve | `resolve(name) -> Option<Tree>` | `load_named_root(name)` |
| Head update | `update_ref(name, expected, replacement)` | `compare_and_swap_named_root` |
| Snapshot write | `write_snapshot(base, changes) -> Tree` | `put`, `delete`, or `batch` |
| Branch create | `branch(new_name, from_name)` | Load source, CAS new name from `None` to source tree |
| Tag create | `tag(tag_name, tree)` | CAS tag name from `None` to tree |
| Checkpoint | `checkpoint(prefix, sequence, tree)` | `publish_named_root_at_millis` or CAS from `None` |
| Status | `status(head, candidate)` | Build a candidate tree, then `diff(head, candidate)` |
| Diff | `diff_names(left_name, right_name)` | Load both names, then `diff(left, right)` |
| Commit record | `Commit { tree, parents, message, author }` | Store outside the map or inside a separate metadata tree |
| Fast-forward | `ff(name, old_commit, new_commit)` | Verify ancestry in app metadata, then CAS the named root |
| Three-way merge | `merge(base, target, source)` | Load three trees, call `merge`, then CAS target head |
| Reset / rewind | `reset(name, expected, target_tree)` | CAS name from expected tree to target tree |
| Revert | `revert(base, bad, head)` | Compute inverse app-level patch, apply to head, then CAS |
| Cherry-pick | `cherry_pick(base, picked, head)` | Replay `diff(base, picked)` onto head, resolve conflicts, CAS |
| Checkout | `materialize(tree, destination)` | Range-read tree entries, write through a staging area |
| Fetch | `fetch(remote)` | Copy missing nodes/blobs, then update remote-tracking names |
| Push | `push(local, remote)` | Copy closure first, then CAS the remote named root |
| GC | `collect(retention)` | Retain branch, tag, checkpoint, and active cursor names |

Recommended root-name layout:

```text
refs/heads/<branch>
refs/tags/<tag>
refs/checkpoints/<scope>/<sequence>
refs/remotes/<remote>/<branch>
refs/worktrees/<id>/head
refs/sync/<peer>/cursor
```

For single-writer local tools, `publish_named_root` is often acceptable. For
multi-writer branches, sync jobs, remote pushes, or browser tabs sharing one
store, use CAS every time. A failed CAS has the same shape as a Git push race:
reload the current head, merge or reapply your changes, then retry.

Keep these records if users need history instead of only latest state:

- `CommitRecord`: tree, parent commit IDs, author, message, and created time.
- `RefLogEntry`: name, old tree or commit, new tree or commit, actor, reason,
  and timestamp.
- `RemoteRef`: remote name, local tracking name, last fetched tree or commit,
  and sync cursor.
- `RetentionPolicy`: which heads, tags, checkpoints, worktrees, and sync
  cursors protect nodes from GC.

## Basic ordered map

Use this recipe when you need a versioned ordered map with point lookups, deletes, and prefix scans. It maps to [`basic_map.rs`](../examples/basic_map.rs).

### Background

Application state usually starts as a map: users, documents, settings, tasks, or records. A mutable hash map works inside one process, but it cannot give you immutable snapshots, branchable state, or structural diffs.

### How prolly helps

`Prolly::put` and `Prolly::delete` return a new `Tree`. Old tree handles remain valid while their nodes remain in the store. `range` scans byte-ordered keys, so key prefixes become application namespaces.

### Reference design

Use a prefix per collection:

```text
user:<id>      -> user record
team:<id>      -> team record
setting:<name> -> setting value
```

For production schemas, prefer `KeyBuilder` segments over raw separators.

### Implementation sketch

The example creates a tree, inserts three users, deletes one user, then scans the `user:` range.

```rust
let tree = prolly.create();
let tree = prolly.put(&tree, b"user:001".to_vec(), b"Ada".to_vec())?;
let tree = prolly.put(&tree, b"user:002".to_vec(), b"Grace".to_vec())?;
let tree = prolly.delete(&tree, b"user:003")?;

let users = prolly
    .range(&tree, b"user:", Some(b"user;"))?
    .collect::<Result<Vec<_>, _>>()?;
```

### Future enhancements

- Add `VersionedValue` envelopes for typed records
- Publish the root as `main`
- Add secondary indexes with `diff`
- Switch from raw prefix strings to `KeyBuilder`
- Add range pagination for API responses

## Bulk import and tree statistics

Use this recipe when you need to load a large initial dataset. It maps to [`batch_build.rs`](../examples/batch_build.rs).

### Background

Repeated `put` calls work for small data, but imports and index rebuilds benefit from building leaves and internal nodes in bulk. Bulk building also gives you a clean place to inspect tree shape before publishing.

### How prolly helps

`BatchBuilder` accepts unsorted entries and builds a tree with the configured chunking rules. `SortedBatchBuilder` fits already sorted exports. `collect_stats` reports node count, entries, height, fill factor, and serialized size.

### Reference design

Use bulk builders for:

- first import from another database
- rebuilding a materialized view
- rebuilding a retrieval index
- compacting append-only events into a smaller tree
- loading fixtures for conformance tests

### Implementation sketch

```rust
let mut builder = BatchBuilder::new(store.clone(), config.clone());
for index in (0..1_000).rev() {
    builder.add(
        format!("event:{index:04}").into_bytes(),
        format!("payload-{index}").into_bytes(),
    );
}

let tree = builder.build()?;
let stats = prolly.collect_stats(&tree)?;
```

### Future enhancements

- Use `SortedBatchBuilder` for sorted source data
- Write import progress by named root checkpoints
- Compare stats across configs before choosing production chunk sizes
- Add a conformance fixture that records expected root CIDs

## Local-first application state

Use this recipe when local devices or background workers need independent branches that merge into a canonical root.

### Background

Local-first apps need optimistic writes, offline branches, and sync. A single mutable "current state" row makes branch reconciliation hard because you cannot diff, replay, or copy only missing state.

### How prolly helps

Each replica can publish its own named root. The canonical root advances with compare-and-swap. Merge compares `base`, `left`, and `right`, so a sync job can preserve both local and remote changes when they touch different keys.

### Reference design

Root names:

```text
app/<app-id>/root/main
app/<app-id>/device/<device-id>/head
app/<app-id>/sync/<peer-id>/last-seen
app/<app-id>/checkpoint/<timestamp>
```

Key layout:

```text
entity/<collection>/<id>                 -> entity value
index/<collection>/<field>/<value>/<id>  -> empty marker or pointer
meta/migration                           -> schema version
```

### Implementation sketch

Use one batch for source and index changes:

```rust
let next = prolly.batch(
    &base,
    vec![
        Mutation::Upsert {
            key: b"entity/user/001".to_vec(),
            val: br#"{"name":"Ada"}"#.to_vec(),
        },
        Mutation::Upsert {
            key: b"index/user/name/Ada/001".to_vec(),
            val: Vec::new(),
        },
    ],
)?;
```

Publish with CAS:

```rust
let update = prolly.compare_and_swap_named_root(
    b"app/demo/root/main",
    current.as_ref(),
    Some(&next),
)?;
```

### Operations

- Keep device roots until peers confirm sync
- Use `plan_missing_nodes` to copy only unknown CIDs
- Use `range_page` for background indexing work
- Use `debug_compare_trees` when a sync job publishes an unexpected root
- Run GC after retention roots are loaded and complete

### Future enhancements

- Add per-key merge policy rules
- Store tombstone values for deletes that must replicate before compaction
- Add remote object storage with `AsyncStore`
- Add root manifests for every materialized view derived from main

## Diff and merge for branches

Use this recipe when two branches edit the same base snapshot. It maps to [`diff_merge.rs`](../examples/diff_merge.rs).

### Background

Branching is useful only if you can understand and combine the changes. Prolly trees make this cheap because unchanged subtrees keep the same CID.

### How prolly helps

`diff(base, other)` returns added, removed, and changed keys. `merge(base, left, right, None)` applies disjoint changes without a resolver.

### Reference design

Use this flow for branch reconciliation:

1. Record the base root when a branch starts
2. Let each branch write a new tree
3. Preview `diff(base, branch)` for review or audit
4. Merge `base`, `left`, and `right`
5. CAS-publish the merged root

### Implementation sketch

```rust
let base = prolly.put(&prolly.create(), b"doc:title".to_vec(), b"Draft".to_vec())?;
let left = prolly.put(&base, b"doc:body".to_vec(), b"Hello".to_vec())?;
let right = prolly.put(&base, b"doc:tags".to_vec(), b"example".to_vec())?;

let changes = prolly.diff(&base, &left)?;
let merged = prolly.merge(&base, &left, &right, None)?;
```

### Future enhancements

- Add `merge_explain` output to audit logs
- Use `stream_conflicts` before applying a merge
- Add range-limited merge for partitioned keyspaces
- Add UI copy for conflict summaries

## Delete-aware merge resolvers

Use this recipe when concurrent edits can conflict. It maps to [`resolver.rs`](../examples/resolver.rs).

### Background

Delete/update conflicts need explicit absence. If a resolver treats missing values as empty bytes, it can resurrect deleted records or delete valid empty values.

### How prolly helps

Merge conflicts preserve all sides as optional values:

```rust
pub struct Conflict {
    pub key: Vec<u8>,
    pub base: Option<Vec<u8>>,
    pub left: Option<Vec<u8>>,
    pub right: Option<Vec<u8>>,
}
```

`None` means absent. `Some(Vec::new())` means the key is present with an empty value.

### Reference design

Choose policies by key family:

```text
settings/          update-wins
permissions/       delete-wins
documents/body/    domain merge
billing/           unresolved
```

### Implementation sketch

```rust
let update_wins = prolly.merge(
    &base,
    &left,
    &right,
    Some(Box::new(resolver::update_wins)),
)?;

let delete_wins = prolly.merge(
    &base,
    &left,
    &right,
    Some(Box::new(resolver::delete_wins)),
)?;
```

Use a policy registry when one resolver needs multiple rules:

```rust
let policies = MergePolicyRegistry::with_default(|_| Resolution::unresolved())
    .add_prefix(b"settings/".to_vec(), resolver::update_wins)
    .add_prefix(b"permissions/".to_vec(), resolver::delete_wins);

let merged = prolly.merge(&base, &left, &right, Some(policies.into_resolver()))?;
```

### Future enhancements

- Decode structured values inside resolver callbacks
- Persist unresolved conflicts as records for review
- Add key-family policy tests
- Use `merge_explain` to show which policy resolved each conflict

## Conflict-free custom merge

Use this recipe when your application requires merge to always produce a tree. It maps to [`crdt_merge.rs`](../examples/crdt_merge.rs).

### Background

Some systems cannot pause for manual conflict review. Counters, presence records, tag sets, and generated alternatives often need deterministic conflict-free merge behavior.

### How prolly helps

`crdt_merge` uses `CrdtConfig`. Built-in strategies include last-writer-wins and multi-value. Custom strategies receive the same `Conflict` shape and return `CrdtResolution::Value` or `CrdtResolution::Delete`.

### Reference design

Use CRDT-style merge for:

- additive counters
- multi-value alternatives
- timestamped records
- generated artifacts where all alternatives should be retained
- delete/update policies that should never return `Error::Conflict`

### Implementation sketch

```rust
let sum_values = CrdtConfig::custom(|conflict| {
    let left = conflict.left.as_deref().and_then(parse_u64).unwrap_or_default();
    let right = conflict.right.as_deref().and_then(parse_u64).unwrap_or_default();
    CrdtResolution::value((left + right).to_string().into_bytes())
});

let merged = prolly.crdt_merge(&base, &left, &right, &sum_values)?;
```

### Future enhancements

- Add schema-aware CRDT value types
- Add a bounded multi-value strategy for generated candidates
- Store causal metadata in values when your domain needs it
- Add property tests for commutativity and determinism

## Conversation memory

Use this recipe when agents extract durable memories from conversations. It maps to [`conversation_memory.rs`](../examples/conversation_memory.rs).

### Background

Agent memory needs reviewable branches. An extraction agent may propose memories, while the canonical conversation state can change at the same time.

### How prolly helps

Attempt roots let agents write speculative memories without publishing them. The canonical memory root advances through CAS. Merge accepts an attempt branch against the base it started from.

### Reference design

Root names:

```text
conversation/<conversation-id>/root/main
conversation/<conversation-id>/attempt/<actor>/<attempt>
```

Key layout:

```text
conversation/<conversation-id>/memory/<memory-id> -> MemoryRecord
```

Value fields:

- `subject`
- `fact`
- `source`
- `confidence`

### Implementation sketch

The example writes an initial memory, publishes `main`, writes an agent attempt, handles a concurrent canonical update, then merges the attempt back.

```rust
let canonical_before_accept = prolly
    .load_named_root(&main_root)?
    .expect("main exists");

let merged = prolly.merge(
    &base,
    &canonical_before_accept,
    &agent_attempt,
    None,
)?;
```

### Operations

- Store the base root for every attempt
- Use `VersionedValue` for memory schemas
- Keep attempt roots until accepted, rejected, or expired
- Use delete-aware resolvers for user-deleted memories
- Record accepted root CIDs in audit logs

### Future enhancements

- Add confidence thresholds before accepting attempt roots
- Add user review queues for unresolved memory conflicts
- Add retention rules for stale attempts
- Add provenance links to the source event log

## Agent event logs

Use this recipe when you need an ordered, replayable record of agent activity. It maps to [`agent_event_log.rs`](../examples/agent_event_log.rs).

### Background

Agents produce more than messages. Tool calls, tool results, memory writes, checkpoints, and summaries all matter for audit and recovery. A plain append log can record events, but it cannot branch or attach immutable memory roots cleanly.

### How prolly helps

Prolly keys give ordered event ranges. Values can contain typed event variants. Checkpoint events can embed a `Tree` handle, so the log records the exact memory root accepted at that step.

### Reference design

Root names:

```text
agent-log/<run-id>/root/events/current
agent-log/<run-id>/root/memory/current
```

Event keys:

```text
agent-log/<run-id>/event/<timestamp>/<sequence>
```

Event kinds:

- user message
- assistant message
- tool call
- tool result
- memory write
- checkpoint
- summary compaction

### Implementation sketch

Use a batch append for a group of events:

```rust
let mutations = events
    .iter()
    .map(|event| {
        Ok(Mutation::Upsert {
            key: event_key(event),
            val: encode_event(event)?,
        })
    })
    .collect::<Result<Vec<_>, Error>>()?;

let log = prolly.batch(&base, mutations)?;
```

### Operations

- Include the event sequence in the key to break timestamp ties
- Store tool arguments and results as values, not logs outside the tree
- Publish `events/current` after successful batch writes
- Page long logs with `range_page`
- Use summaries to reduce long-context replay cost

### Future enhancements

- Use `append_batch` for append-heavy runs
- Add checksum fields for large tool outputs stored in blobs
- Add signed event envelopes for audit
- Add automatic compaction windows by token count or event count

## Background compaction and retention-aware GC

Use this recipe when logs or memory trees need compaction without losing auditability. It maps to [`background_compaction.rs`](../examples/background_compaction.rs).

### Background

Long-running agents can accumulate large event histories. You may want to replace old event ranges with summaries while retaining enough roots for rollback and audit.

### How prolly helps

Because every root is immutable, compaction creates a new tree instead of rewriting the old log. Named root retention decides which historical roots survive GC. `plan_store_gc_for_retention` reports reclaimable nodes before sweep.

### Reference design

Root names:

```text
compaction/run/<run-id>/root/events/0001
compaction/run/<run-id>/root/events/current
compaction/run/<run-id>/root/summary-index/current
```

Compaction flow:

1. Load the current event log
2. Delete the compacted source event keys
3. Insert one `SummaryCompaction` event
4. Rebuild a summary index
5. Publish `events/current` and `summary-index/current`
6. Plan GC using retained named roots
7. Sweep only after the plan looks correct

### Implementation sketch

The example deletes an event window and inserts a summary event in one batch:

```rust
let mut mutations = (first_sequence..=last_sequence)
    .map(|sequence| Mutation::Delete {
        key: event_key(run_id, sequence),
    })
    .collect::<Vec<_>>();

mutations.push(Mutation::Upsert {
    key: event_key(run_id, compacted_sequence),
    val: encode_event(&summary_event)?,
});
```

It then retains selected named roots:

```rust
let retention = NamedRootRetention::exact(vec![
    root_name(run_id, "events/0001"),
    root_name(run_id, "events/current"),
    root_name(run_id, "summary-index/current"),
]);

let plan = prolly.plan_store_gc_for_retention(&retention)?;
```

### Future enhancements

- Add retention classes for legal hold, audit, and cache-only roots
- Compact by token budget instead of sequence range
- Store summaries with source root CIDs and model metadata
- Run compaction in an async worker against remote storage

## Deterministic RAG snapshots

Use this recipe when generated answers must be replayable against the exact retrieval metadata used at answer time. It maps to [`deterministic_rag_snapshot.rs`](../examples/deterministic_rag_snapshot.rs).

### Background

Retrieval-augmented generation (RAG) systems change over time. Documents are re-parsed, chunks are re-embedded, and indexes are republished. If you store only answer text and citations, you cannot prove which index state produced them.

### How prolly helps

A `Tree` snapshot pins the chunk metadata used for retrieval. Answer records can store `index_snapshot: Tree`, so replay uses the old root even after the current index changes.

### Reference design

Root names:

```text
rag/corpus/<corpus-id>/root/index/current
rag/corpus/<corpus-id>/root/answers
```

Keys:

```text
rag/corpus/<corpus-id>/chunk/<doc-id>/<chunk-id> -> DocumentChunk
rag/answer/<query-id>                           -> AnswerRecord
```

Answer values should include:

- query text or query ID
- answer text
- index snapshot
- citations
- parser version
- source URI

### Implementation sketch

Store the answer with the root used for retrieval:

```rust
let index_snapshot = prolly
    .load_named_root(&current_index_name)?
    .expect("current index exists");

let answer = answer_from_snapshot(&prolly, &index_snapshot, corpus_id, query)?;
```

Replay later:

```rust
let replayed = answer_from_snapshot(
    &prolly,
    &stored_answer.index_snapshot,
    corpus_id,
    &stored_answer.query,
)?;
```

### Future enhancements

- Store vector model and embedding dimensions with each answer
- Store prompt and model IDs for answer generation
- Add answer-root retention policies
- Add fixtures that prove replay returns identical citations

## Document chunk indexes

Use this recipe when you need ordered document and chunk metadata, with large text kept outside leaf nodes. It maps to [`document_chunk_index.rs`](../examples/document_chunk_index.rs).

### Background

Document indexes need metadata lookups, ordered chunk scans, and access to large chunk text. Storing long text in every leaf can make nodes heavy and reduce cache efficiency.

### How prolly helps

The tree stores small metadata records and `ValueRef` entries for large text. `BlobStore` holds chunk text. A vector sidecar can hold embeddings keyed by a stable vector ID.

### Reference design

Keys:

```text
doc-index/<corpus>/document/<doc-id>/meta
doc-index/<corpus>/parser/<parser>/document/<doc-id>/chunk/<start-byte>
doc-index/<corpus>/text/<parser>/<doc-id>/<chunk-id>
```

Chunk metadata fields:

- document ID
- chunk ID
- parser version
- byte range
- text key
- vector ID

### Implementation sketch

Store chunk text through `put_large_value`:

```rust
let policy = LargeValueConfig::new(32);
tree = prolly.put_large_value(
    blobs,
    &tree,
    text_key.clone(),
    chunk.text.into_bytes(),
    policy,
)?;
```

Store metadata inline:

```rust
tree = prolly.put(
    &tree,
    chunk_metadata_key(corpus_id, parser_version, doc_id, start_byte),
    encode_chunk(&metadata)?,
)?;
```

### Future enhancements

- Add parser-version roots for side-by-side re-ingestion
- Use file or object blob stores for chunk text
- Add diff-based re-embedding for changed chunks
- Store checksums for blob payload validation

## Vector sidecars

Use this recipe when a vector database scores embeddings while prolly stores reproducible metadata. It maps to [`vector_sidecar.rs`](../examples/vector_sidecar.rs).

### Background

Approximate nearest neighbor (ANN) systems are optimized for vector search, not immutable application snapshots. If a vector index changes, old answers can become hard to replay.

### How prolly helps

Prolly stores the metadata set and vector IDs for a snapshot. The sidecar stores embeddings. Retrieval filters sidecar hits to vector IDs present in the chosen prolly root.

### Reference design

Keys:

```text
vector-sidecar/corpus/<corpus-id>/chunk/<doc-id>/<chunk-id>
vector-sidecar/answer/<answer-id>
```

Chunk metadata should include:

- corpus ID
- document ID
- chunk ID
- source URI
- parser version
- vector ID
- embedding model
- embedding dimensions

### Implementation sketch

Build the allowed vector set from the snapshot:

```rust
let metadata = metadata_by_vector_id(prolly, index_snapshot, corpus_id)?;
let allowed = metadata.keys().cloned().collect::<HashSet<_>>();
let hits = sidecar.search_filtered(query_embedding, &allowed, limit);
```

Store answers with the metadata root:

```rust
let answer = AnswerRecord {
    query: query.to_string(),
    embedding_model: EMBEDDING_MODEL.to_string(),
    index_snapshot: index_snapshot.clone(),
    citations,
    answer,
};
```

### Future enhancements

- Add sidecar garbage collection from retained prolly roots
- Store vector checksums or version IDs
- Support multiple embedding models per corpus
- Add replay tests for old answers after sidecar updates

## Provenance-rich values

Use this recipe when every generated claim needs source, parser, model, and parent-record provenance. It maps to [`provenance_values.rs`](../examples/provenance_values.rs).

### Background

AI applications need to answer "where did this come from?" Claims derived from documents should carry enough context to audit the source and the pipeline.

### How prolly helps

Values can store source CIDs, chunk CIDs, parent keys, and pipeline metadata. The tree root pins the set of claims. Range scans by source file retrieve all derived claims for review.

### Reference design

Keys:

```text
provenance/chunk/<file-id>/<chunk-id>
provenance/claim/<file-id>/<claim-id>
```

Value records:

- `SourceRef`: source URI, file ID, source CID
- `PipelineRef`: parser, embedding model, dimensions, summarizer model, prompt version
- `ChunkRecord`: byte range, chunk text, chunk CID, pipeline
- `DerivedClaim`: claim text, confidence, parent chunk key, source CIDs, pipeline

### Implementation sketch

Calculate content IDs for source bytes and chunks:

```rust
let source = SourceRef {
    source_uri,
    file_id,
    source_cid: Cid::from_bytes(source_bytes),
};

let chunk_cid = Cid::from_bytes(chunk_text.as_bytes());
```

Scan claims for one source:

```rust
let (start, end) = prefix_range(claim_prefix(&source.file_id));
let claims = prolly
    .range(&tree, &start, end.as_deref())?
    .collect::<Result<Vec<_>, _>>()?;
```

### Future enhancements

- Add signatures for pipeline outputs
- Store prompt templates as content-addressed records
- Link claims to answer records
- Add provenance graph export for audits

## Secondary indexes from diffs

Use this recipe when a source tree needs a derived lookup structure. It maps to [`secondary_index.rs`](../examples/secondary_index.rs).

### Background

An ordered map gives one primary key order. Applications need alternate access paths: users by status, documents by tag, jobs by priority, or memories by subject.

### How prolly helps

Build the secondary index as another tree. Use `diff(source_v1, source_v2)` to update the index incrementally, then compare the result to a full rebuild in tests.

### Reference design

Source key:

```text
source/tenant/<tenant-id>/user/<user-id>
```

Index key:

```text
index/user-by-status/tenant/<tenant-id>/status/<status>/<user-id>
```

Index values can be empty marker bytes or compact pointers to source keys.

### Implementation sketch

Map source diffs to index mutations:

```rust
match diff {
    Diff::Added { val, .. } => upsert_index(decode_user(val)?),
    Diff::Removed { val, .. } => delete_index(decode_user(val)?),
    Diff::Changed { old, new, .. } => update_if_index_key_changed(old, new)?,
}
```

Apply the derived mutations in one batch:

```rust
let index_v2 = prolly.batch(&index_v1, mutations)?;
```

### Operations

- Publish source and index roots together through a view manifest or adjacent named roots
- Store the source root used to build each index root
- Rebuild from source periodically to detect index drift
- Treat index roots as disposable if source roots are retained

### Future enhancements

- Add helper APIs for source/index manifests
- Add drift-check tooling
- Add multi-index update batches
- Add async background indexing with `AsyncStore`

## Materialized views from diffs

Use this recipe when a derived tree stores aggregates rather than alternate keys. It maps to [`materialized_view.rs`](../examples/materialized_view.rs).

### Background

Dashboards and repeated queries need precomputed aggregates. Recomputing from source for every request wastes work, but mutable aggregate tables can drift from source if updates fail halfway.

### How prolly helps

A materialized view is another immutable tree. Diff the source, translate changes into aggregate deltas, build a new view root, and publish a manifest that records the source and view snapshots together.

### Reference design

Source key:

```text
orders/source/tenant/<tenant-id>/order/<order-id>
```

View key:

```text
orders/view/by-status/tenant/<tenant-id>/status/<status>
```

Manifest value:

```text
view_name
source_snapshot
view_snapshot
source_diff_count
```

### Implementation sketch

The example computes per-status revenue. Each source diff updates aggregate deltas:

```rust
match diff {
    Diff::Added { val, .. } => record_delta(decode_order(val)?, 1),
    Diff::Removed { val, .. } => record_delta(decode_order(val)?, -1),
    Diff::Changed { old, new, .. } => {
        record_delta(decode_order(old)?, -1);
        record_delta(decode_order(new)?, 1);
    }
}
```

After applying deltas, publish the view and manifest roots.

### Future enhancements

- Add transaction-like publish helpers for source, view, and manifest roots
- Add incremental rebuild checkpoints
- Add view invalidation when schema versions change
- Add range-limited views for tenant partitions

## Large values and file blob storage

Use this recipe when values are too large to keep inside tree nodes. It maps to [`file_blob_store.rs`](../examples/file_blob_store.rs).

### Background

Large documents, tool outputs, media, and chunk text can make leaf nodes expensive to read and cache. The ordered tree should keep metadata and references; payload bytes can live elsewhere.

### How prolly helps

`put_large_value` stores small values inline and large values in a `BlobStore` according to `LargeValueConfig`. The tree stores a `ValueRef`, so GC can trace retained roots and remove unreferenced blobs.

### Reference design

Store:

```text
node store: content-addressed tree nodes
blob store: large payload bytes
tree value: ValueRef::Inline or ValueRef::Blob
```

Use this for:

- document text
- large tool outputs
- transcript chunks
- generated artifacts
- binary attachments

### Implementation sketch

```rust
let blobs = FileBlobStore::open(&blob_dir)?;
let policy = LargeValueConfig::new(1024);

let tree = prolly.put_large_value(
    &blobs,
    &tree,
    b"doc/body".to_vec(),
    vec![7; 4096],
    policy,
)?;
```

Sweep unreachable blobs after choosing retained roots:

```rust
let plan = prolly.plan_blob_store_gc(&blobs, std::slice::from_ref(&tree))?;
let sweep = prolly.sweep_blob_store_gc(&blobs, std::slice::from_ref(&tree))?;
```

### Future enhancements

- Use object storage for blobs
- Store checksums and MIME types in metadata values
- Add blob replication before publishing roots
- Add retention classes for attachments

## Filesystem snapshots with named roots

Use this recipe when you want Git-like filesystem snapshots where file contents live in blobs and a named root points at the current tree. It maps to [`filesystem_snapshot.rs`](../examples/filesystem_snapshot.rs).

### Background

A filesystem snapshot is a map from relative path to file state. The expensive bytes are file contents; the state you want to branch, diff, merge, and publish is the ordered path map.

### How prolly helps

`BatchBuilder` can build the initial path map in bulk. `BlobStore` keeps file contents content-addressed. `publish_named_root` moves a durable name such as `refs/heads/main` only after the tree nodes and blob bytes have been written, so a crash before publish leaves the previous snapshot visible.

For later snapshots, load the named root, apply changed path mutations, and use `compare_and_swap_named_root` to move the branch head only if it still points at the tree you edited from.

### Reference design

Root names:

```text
refs/heads/main
refs/heads/<branch>
checkpoint/<timestamp-or-sequence>
```

Tree keys:

```text
path/README.md   -> ValueRef::Blob(file-content-cid)
path/src/lib.rs  -> ValueRef::Blob(file-content-cid)
```

For production, store a typed file-entry envelope instead of a bare `ValueRef`. Include mode, executable bit, size, content CID, optional MIME type, and any platform metadata you need to preserve.

### Implementation sketch

Build the initial snapshot from file blobs:

```rust
let mut builder = BatchBuilder::new(store.clone(), config.clone());

for path in ["README.md", "src/lib.rs"] {
    let bytes = std::fs::read(path)?;
    let blob_ref = blobs.put_blob(&bytes)?;

    builder.add(
        format!("path/{path}").into_bytes(),
        ValueRef::Blob(blob_ref).to_bytes(),
    );
}

let snapshot = builder.build()?;
prolly.publish_named_root(b"refs/heads/main", &snapshot)?;
```

Resolve a snapshot and read file contents:

```rust
let snapshot = prolly
    .load_named_root(b"refs/heads/main")?
    .expect("branch exists");

let readme = prolly.get_large_value(&blobs, &snapshot, b"path/README.md")?;
```

Publish an incremental snapshot with CAS:

```rust
let base = prolly
    .load_named_root(b"refs/heads/main")?
    .unwrap_or_else(|| prolly.create());

let bytes = std::fs::read("README.md")?;
let blob_ref = blobs.put_blob(&bytes)?;
let next = prolly.put(
    &base,
    b"path/README.md".to_vec(),
    ValueRef::Blob(blob_ref).to_bytes(),
)?;

let update = prolly.compare_and_swap_named_root(
    b"refs/heads/main",
    Some(&base),
    Some(&next),
)?;
```

If the update conflicts, another writer moved the branch first. Reload the named root, reapply or merge your path changes, then retry.

### Future enhancements

- Encode `FileEntry` metadata with `VersionedValue`
- Use `batch` for incremental snapshots with many changed, deleted, or renamed paths
- Store directory entries or derive them from path prefixes
- Add ignore rules and path normalization before building keys
- Run blob and node GC from retained branch, tag, and checkpoint roots

## Durable SQLite setup

Use this recipe when a desktop app, local-first app, command-line tool, or agent worker needs a durable embedded store.

### Background

`MemStore` is right for tests and examples. Applications need durability, backups, concurrency settings, and GC discipline.

### How prolly helps

`SqliteStore` persists content-addressed nodes and manifests in one SQLite database. Named roots become durable branch heads. SQLite write transactions give `batch` and manifest updates a strong local backend.

### Reference design

Cargo dependencies:

```toml
[dependencies]
prolly-map = "0.2"
prolly-store-sqlite = "0.1"
```

Setup:

```rust
use std::sync::Arc;
use prolly::{Config, Prolly};
use prolly_store_sqlite::{SqliteStore, SqliteStoreConfig};

let store = Arc::new(SqliteStore::open_with_config(
    "app.prolly.sqlite",
    SqliteStoreConfig {
        busy_timeout_ms: 5_000,
        enable_wal: true,
        synchronous_normal: true,
    },
)?);

let config = Config::builder()
    .node_cache_max_nodes(50_000)
    .node_cache_max_bytes(256 * 1024 * 1024)
    .build();

let prolly = Prolly::new(store, config);
```

### Operations

- Keep SQLite `-wal` and `-shm` files with the database during backup
- Use WAL mode for app-style read/write concurrency
- Use non-zero `busy_timeout_ms` for contended local writes
- Publish every durable head as a named root
- Run `plan_store_gc_for_retention` before sweeping
- Use `TokioBlockingStore` in async Tokio apps that call a sync SQLite backend

### Future enhancements

- Add app-level migrations stored under a metadata key
- Add backup and restore examples
- Add store health checks before startup
- Add CLI inspection for SQLite-backed stores

## Object-store backed snapshots

Use this recipe when tree nodes and blobs should live in S3, R2, or another object store.

### Background

Object stores have high latency and cheap durable bytes. They fit immutable content-addressed nodes, but root updates need conditional writes and reads should be concurrent.

### How prolly helps

`AsyncStore` lets object-store reads overlap. Content-addressed node writes are idempotent because the key is the CID. Missing-node planning can upload only content that the destination lacks.

### Reference design

Object layout:

```text
nodes/<cid>       -> serialized node bytes
blobs/<digest>    -> large value bytes
roots/<name>      -> root manifest
hints/<namespace> -> optional performance hints
```

Store behavior:

- `put` is idempotent
- `batch_get_ordered` uses native multi-get or concurrent point reads
- `delete` is reserved for GC
- root manifest updates use conditional writes
- hot internal nodes are cached near the application

### Implementation sketch

Enable async support:

```toml
[dependencies]
prolly-map = { version = "0.2", features = ["async-store"] }
```

Implement `AsyncStore` over your object client. Override `read_parallelism` or `batch_get_ordered` so tree traversal can overlap network reads.

### Future enhancements

- Add an official object-store backend
- Add upload manifests for multi-object publish
- Add background prefetch for hot subtrees
- Add signed root manifests for peer sync

## Browser and WASM storage

Use this recipe when a web app needs local branches, offline reads, and eventual sync.

### Background

Browser storage APIs are async. IndexedDB, Origin Private File System (OPFS), Cache Storage, and remote APIs do not fit a blocking `Store` trait.

### How prolly helps

`AsyncStore` is single-thread friendly and does not require Tokio. A browser store can expose async point reads and writes while `AsyncProlly` handles tree operations.

### Reference design

Storage choices:

- IndexedDB for node bytes and root manifests
- OPFS for larger local blobs
- remote API for peer sync
- in-memory cache for hot nodes

Root names:

```text
browser/<installation-id>/root/main
browser/<installation-id>/root/outbox
browser/<installation-id>/root/synced/<peer-id>
```

### Implementation sketch

Use async APIs in browser-facing code:

```rust
let tree = prolly.create();
let tree = prolly.put(&tree, b"k".to_vec(), b"v".to_vec()).await?;
let value = prolly.get(&tree, b"k").await?;
```

Sync flow:

1. Load local and remote named roots
2. Plan missing local nodes for upload
3. Plan missing remote nodes for download
4. Merge roots if both sides changed
5. CAS-publish the merged root locally and remotely

### Future enhancements

- Add a WASM example store
- Add range-page UI examples
- Add background sync with retry state in named roots
- Add quota-aware blob retention