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
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
//! 13a (v0.17): coordination records, `message`, `task`, `claim` and
//! `presence` (`docs/design/13a-coordination.md`). Step 1 is the plumbing:
//! the kinds are stored as ordinary records in the shared scope with the
//! sentinel vector, and kept out of recall, the keyword index, the ANN and
//! `about` (see `is_coordination`). Each kind's API comes in its own step.
use super::*;
impl Mnemo {
/// Write a coordination record of `kind` (one of
/// [`crate::memory::COORDINATION_KINDS`]) holding `content` and
/// `metadata` (whose `_mnemo.*` keys the caller of this internal path
/// sets): shared scope, the sentinel vector, never embedded. The
/// handle's author is stamped like any new record's.
// The test hook's; the APIs use `put_coordination_with_id`.
#[cfg_attr(not(feature = "dev-hooks"), allow(dead_code))]
pub(super) fn put_coordination(
&mut self,
kind: &str,
content: String,
metadata: Map<String, Value>,
) -> Result<Ulid> {
self.put_coordination_with_id(Ulid::new(), kind, content, metadata)
}
/// [`Mnemo::put_coordination`] with the record's id chosen first (a
/// message's id names its conversation).
pub(super) fn put_coordination_with_id(
&mut self,
id: Ulid,
kind: &str,
content: String,
mut metadata: Map<String, Value>,
) -> Result<Ulid> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
if !crate::memory::COORDINATION_KINDS.contains(&kind) {
return Err(MnemoError::Invalid(format!(
"{kind:?} isn't a coordination kind"
)));
}
metadata.insert(MNEMO_META_KIND.into(), Value::String(kind.to_string()));
let mut m = Memory::new(content, MemoryType::Episodic, self.sentinel_vector())
.with_scope(Scope::Shared);
m.id = id;
m.metadata = metadata;
self.put(m)
}
}
/// 13a (13.1): one message between agents, as [`Mnemo::inbox`] returns it.
#[derive(Debug, Clone, PartialEq)]
pub struct Message {
/// The record's id.
pub id: Ulid,
/// An agent id, `role:<role>`, or `all`.
pub to: String,
/// The sender as it said, unproven until 13b.
pub from: String,
/// One line.
pub subject: String,
/// The body (may be empty).
pub body: String,
/// Record ids, a PR number, a ledger entry title.
pub refs: Vec<String>,
/// The conversation it belongs to.
pub conversation: String,
/// Its hop count (13.1b's hop cap).
pub hops: u64,
/// `unread` or `acked`.
pub status: String,
/// `sent`, `delivered`, `seen` or `done`.
pub delivery: String,
/// When it was posted, in unix seconds.
pub created_at: i64,
/// When it was acked, if it was.
pub acked_at: Option<i64>,
}
impl Message {
/// The message a record holds, if it's one.
pub fn from_memory(m: &Memory) -> Option<Message> {
if m.metadata.get(MNEMO_META_KIND).and_then(Value::as_str)
!= Some(crate::memory::MNEMO_META_KIND_MESSAGE)
{
return None;
}
let s = |k: &str| {
m.metadata
.get(k)
.and_then(Value::as_str)
.unwrap_or("")
.to_string()
};
let (subject, body) = match m.content.split_once("\n\n") {
Some((a, b)) => (a.to_string(), b.to_string()),
None => (m.content.clone(), String::new()),
};
Some(Message {
id: m.id,
to: s(MNEMO_META_TO),
from: s(MNEMO_META_FROM),
subject,
body,
refs: m
.metadata
.get(MNEMO_META_REFS)
.and_then(Value::as_array)
.map(|a| {
a.iter()
.filter_map(|v| v.as_str().map(str::to_string))
.collect()
})
.unwrap_or_default(),
conversation: s(MNEMO_META_CONVERSATION),
hops: m
.metadata
.get(MNEMO_META_HOPS)
.and_then(Value::as_u64)
.unwrap_or(0),
status: s(MNEMO_META_STATUS),
delivery: s(MNEMO_META_DELIVERY),
created_at: m.created_at,
acked_at: m.metadata.get(MNEMO_META_ACKED_AT).and_then(Value::as_i64),
})
}
/// Whether it's addressed to `agent` (by id), to `role`, or to everyone,
/// and isn't from `agent` itself.
pub fn is_for(&self, agent: Option<&str>, role: Option<&str>) -> bool {
if agent.is_some_and(|a| a == self.from) {
return false;
}
self.to == "all"
|| agent.is_some_and(|a| a == self.to)
|| role.is_some_and(|r| self.to.strip_prefix("role:") == Some(r))
}
}
/// Whether `to` is an address: an agent id, `role:<role>`, or `all`.
fn valid_address(to: &str) -> bool {
to == "all"
|| crate::memory::valid_agent_id(to) && !to.starts_with("role:")
|| to
.strip_prefix("role:")
.is_some_and(crate::memory::valid_agent_id)
}
/// 13a (RB-012.6 item 4): the agents a broadcast message or a role's task
/// has been shown to (`_mnemo.seen_by`).
fn seen_by(m: &Memory) -> Vec<String> {
m.metadata
.get(crate::memory::MNEMO_META_SEEN_BY)
.and_then(Value::as_array)
.map(|a| {
a.iter()
.filter_map(|v| v.as_str().map(str::to_string))
.collect()
})
.unwrap_or_default()
}
/// COORD-LEGACY-SCAN: whether a stored (fp16) vector is the sentinel every
/// coordination record carries: all its values equal, at 1/sqrt(dims).
fn is_sentinel(h: &[u16]) -> bool {
let Some(&first) = h.first() else {
return false;
};
let want = 1.0 / (h.len() as f32).sqrt();
h.iter().all(|&x| x == first) && (crate::veccol::f16_to_f32(first) - want).abs() < 1e-3
}
/// READY-NOTICE: the agents a `ready` task was shown to as theirs to
/// close (`_mnemo.ready_seen_by`).
fn ready_seen_by(m: &Memory) -> Vec<String> {
m.metadata
.get(crate::memory::MNEMO_META_READY_SEEN_BY)
.and_then(Value::as_array)
.map(|a| {
a.iter()
.filter_map(|v| v.as_str().map(str::to_string))
.collect()
})
.unwrap_or_default()
}
/// Add `agent` to a record's `_mnemo.seen_by`; `false` when it was there.
fn add_seen_by(m: &mut Memory, agent: &str) -> bool {
let mut v = seen_by(m);
if v.iter().any(|a| a == agent) {
return false;
}
v.push(agent.to_string());
m.metadata.insert(
crate::memory::MNEMO_META_SEEN_BY.into(),
Value::Array(v.into_iter().map(Value::String).collect()),
);
true
}
impl Mnemo {
/// Every live coordination record of `kind`, in catalog order; with a
/// `locator` (a claim's topic, a presence record's agent), only that
/// one's, found through its row's hash without decrypting the rest.
fn coordination_records(&mut self, kind: &str, locator: Option<&str>) -> Result<Vec<Memory>> {
let want = locator.map(|l| locator_hash_of(kind, l));
let legacy = self.legacy_coordination_ids()?;
let entries: Vec<CatalogEntry> = self
.catalog
.iter()
.filter(|e| !e.deleted)
.filter(|e| {
legacy.contains(&e.id)
|| e.v9.is_none_or(|r| {
r.kind == ROW_KIND_COORDINATION && want.is_none_or(|h| r.thread_hash == h)
})
})
.cloned()
.collect();
let mut out = Vec::new();
for e in entries {
let m = self.read_memory(&e)?;
if m.metadata.get(MNEMO_META_KIND).and_then(Value::as_str) != Some(kind) {
continue;
}
if let Some(l) = locator {
if locator_of(&m) != Some(l) {
continue;
}
}
out.push(m);
}
Ok(out)
}
/// RB-013.1: the coordination records an older build re-tagged as facts
/// (its `compact` writes every row again, and it computes "fact" for a
/// kind it doesn't know). Found once per handle: such a record keeps
/// its `episodic` type in the row and its sentinel vector in the column,
/// so only `episodic` fact rows whose vector is the sentinel are
/// decrypted (COORD-LEGACY-SCAN, v0.17.2); a row whose vector can't be
/// read is decrypted, as before. A write of one re-tags its row.
fn legacy_coordination_ids(&mut self) -> Result<std::collections::HashSet<u128>> {
if let Some(ids) = &self.legacy_coordination {
return Ok(ids.clone());
}
let episodic = super::records::memory_type_code(MemoryType::Episodic);
let candidates: Vec<CatalogEntry> = self
.catalog
.iter()
.filter(|e| !e.deleted)
.filter(|e| {
e.v9.is_some_and(|r| {
r.kind == super::records::ROW_KIND_FACT && r.memory_type == episodic
})
})
.cloned()
.collect();
let mut ids = std::collections::HashSet::new();
// Each vector page read once, for every candidate row on it.
let mut pages: std::collections::HashMap<(u64, u64), Option<Vec<u8>>> =
std::collections::HashMap::new();
for e in candidates {
let vector = match e.v9 {
Some(r) if r.vec_page != 0 && !self.has_hot_vector(&e) => {
let page = pages
.entry((r.vec_page, r.vec_gen))
.or_insert_with(|| self.pager.read_page(r.vec_page, r.vec_gen).ok());
page.as_ref().and_then(|p| {
crate::veccol::decode_entry(p, self.dimensions, r.vec_slot as usize).ok()
})
}
_ => self.column_entry(&e).ok(),
};
if matches!(vector, Some((h, _)) if !is_sentinel(&h)) {
continue;
}
if is_coordination(&self.read_memory(&e)?) {
ids.insert(e.id);
}
}
self.legacy_coordination = Some(ids.clone());
Ok(ids)
}
/// RB-013.1: how many coordination records are tagged as facts in their
/// rows (an older mnemo rewrote this brain); `mnemo compact` re-tags
/// them. They're found and used all the same.
pub fn mistagged_coordination(&mut self) -> Result<usize> {
let ids = self.legacy_coordination_ids()?;
Ok(self
.catalog
.iter()
.filter(|e| !e.deleted && ids.contains(&e.id))
.filter(|e| {
e.v9.is_some_and(|r| r.kind == super::records::ROW_KIND_FACT)
})
.count())
}
/// Every live message, oldest first.
fn messages(&mut self) -> Result<Vec<Memory>> {
let mut out = self.coordination_records(crate::memory::MNEMO_META_KIND_MESSAGE, None)?;
// Stable: within one second (`created_at`'s grain) the catalog's
// order, which is the order they were written.
out.sort_by_key(|m| m.created_at);
Ok(out)
}
/// 13a (13.1): post a message to `to` (an agent id, `role:<role>`, or
/// `all`) with a one-line `subject` and an optional `body` (at most
/// [`crate::memory::MESSAGE_BODY_MAX`] bytes), from `from` (else the
/// handle's author, else `anonymous`), in `from_role` when given.
///
/// A reply names its `conversation`, and its hop count is one more than
/// the conversation's highest. A new message between the same two
/// parties within [`crate::memory::MESSAGE_PAIR_CARRY_SECS`] starts one
/// above the highest count between them in that time, so agents trading
/// fresh messages still reach the hop cap. Its delivery is `sent` and its status `unread`.
// Each is a message's own field; a request struct would only rename them.
#[allow(clippy::too_many_arguments)]
pub fn post(
&mut self,
to: &str,
subject: &str,
body: &str,
refs: &[String],
conversation: Option<&str>,
from: Option<&str>,
from_role: Option<&str>,
) -> Result<Message> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
if !valid_address(to) {
return Err(MnemoError::Invalid(format!(
"{to:?} isn't an address: an agent id, role:<role>, or all"
)));
}
let subject = subject.trim();
if subject.is_empty() || subject.contains('\n') {
return Err(MnemoError::Invalid(
"a message's subject is one line, and not empty".into(),
));
}
if subject.len() > crate::memory::MESSAGE_SUBJECT_MAX {
return Err(MnemoError::Invalid(format!(
"a message's subject is at most {} bytes",
crate::memory::MESSAGE_SUBJECT_MAX
)));
}
if body.len() > crate::memory::MESSAGE_BODY_MAX {
return Err(MnemoError::Invalid(format!(
"a message's body is at most {} bytes; put a longer one in a fact and send its id \
in refs",
crate::memory::MESSAGE_BODY_MAX
)));
}
let from = from
.map(str::to_string)
.or_else(|| self.author.clone())
.unwrap_or_else(|| "anonymous".to_string());
let now = memory::now_secs();
let earlier: Vec<Message> = self
.messages()?
.iter()
.filter_map(Message::from_memory)
.collect();
let mut hops = 0u64;
if let Some(c) = conversation {
if let Some(h) = earlier
.iter()
.filter(|m| m.conversation == c)
.map(|m| m.hops)
.max()
{
hops = h + 1;
}
}
// The pair carry: one more than the highest count between these two,
// either way, in the window.
if let Some(h) = earlier
.iter()
.filter(|m| now - m.created_at <= crate::memory::MESSAGE_PAIR_CARRY_SECS)
.filter(|m| (m.from == from && m.to == to) || (m.from == to && m.to == from))
.map(|m| m.hops)
.max()
{
hops = hops.max(h + 1);
}
let id = Ulid::new();
let conversation = conversation
.map(str::to_string)
.unwrap_or_else(|| id.to_string());
let mut meta = Map::new();
meta.insert(MNEMO_META_TO.into(), Value::String(to.into()));
meta.insert(MNEMO_META_FROM.into(), Value::String(from));
if let Some(r) = from_role {
meta.insert("_mnemo.from_role".into(), Value::String(r.to_string()));
}
meta.insert(MNEMO_META_CONVERSATION.into(), Value::String(conversation));
meta.insert(MNEMO_META_HOPS.into(), Value::from(hops));
meta.insert(
MNEMO_META_REFS.into(),
Value::Array(refs.iter().map(|r| Value::String(r.clone())).collect()),
);
meta.insert(
MNEMO_META_STATUS.into(),
Value::String(crate::memory::MESSAGE_UNREAD.into()),
);
meta.insert(
MNEMO_META_DELIVERY.into(),
Value::String(crate::memory::delivery::SENT.into()),
);
let content = if body.is_empty() {
subject.to_string()
} else {
format!("{subject}\n\n{body}")
};
let id = self.put_coordination_with_id(
id,
crate::memory::MNEMO_META_KIND_MESSAGE,
content,
meta,
)?;
let m = self.get(&id)?;
Ok(Message::from_memory(&m).expect("just written"))
}
/// 13a (13.1): the messages for `agent` (by id), its `role`, or
/// everyone, oldest first, leaving out its own. With `unread_only`,
/// only those not yet acked. Reads only; [`Mnemo::mark_seen`] records
/// that they were shown.
pub fn inbox(
&mut self,
agent: Option<&str>,
role: Option<&str>,
unread_only: bool,
) -> Result<Vec<Message>> {
self.inbox_in(
agent,
role,
unread_only,
crate::memory::VisibilitySet::DEFAULT,
)
}
/// 13a (13.5): [`Mnemo::inbox`] with the visibility levels it shows
/// (`inbox` itself shows public and team).
pub fn inbox_in(
&mut self,
agent: Option<&str>,
role: Option<&str>,
unread_only: bool,
visibility: crate::memory::VisibilitySet,
) -> Result<Vec<Message>> {
Ok(self
.messages()?
.iter()
.filter(|m| visibility.shows(m))
.filter_map(Message::from_memory)
.filter(|m| m.is_for(agent, role))
.filter(|m| !unread_only || m.status == crate::memory::MESSAGE_UNREAD)
.collect())
}
/// 13a (13.1b): mark these messages `seen` (from `sent` or `delivered`),
/// in place. Returns how many changed.
pub fn mark_seen(&mut self, ids: &[Ulid]) -> Result<usize> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let mut changed = 0;
for id in ids {
let mut m = self.get(id)?;
let Some(msg) = Message::from_memory(&m) else {
continue;
};
let mut dirty = false;
if msg.delivery == crate::memory::delivery::SENT
|| msg.delivery == crate::memory::delivery::DELIVERED
{
m.metadata.insert(
MNEMO_META_DELIVERY.into(),
Value::String(crate::memory::delivery::SEEN.into()),
);
dirty = true;
}
// RB-012.6 item 4: a message to a role or everyone is seen per
// agent (this handle's author).
if msg.to == "all" || msg.to.starts_with("role:") {
if let Some(a) = self.author.clone() {
dirty |= add_seen_by(&mut m, &a);
}
}
if dirty {
self.put(m)?;
changed += 1;
}
}
Ok(changed)
}
/// 13a (13.1): ack a message, in place: `unread → acked`, its delivery
/// `done`, and a `status_history` entry with `by` (else the handle's
/// author) and the optional `note`. `Unchanged` when it's already
/// acked; an id that isn't a message is refused.
pub fn ack(&mut self, id: &Ulid, note: Option<&str>, by: Option<&str>) -> Result<WriteOutcome> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let mut m = self.get(id)?;
let Some(msg) = Message::from_memory(&m) else {
return Err(MnemoError::Invalid(format!("{id} isn't a message")));
};
if msg.status == crate::memory::MESSAGE_ACKED {
return Ok(WriteOutcome::Unchanged);
}
let now = memory::now_secs();
let by = by
.map(str::to_string)
.or_else(|| self.author.clone())
.unwrap_or_else(|| "anonymous".to_string());
let mut entry = Map::new();
entry.insert("from".into(), Value::String(msg.status.clone()));
entry.insert(
"to".into(),
Value::String(crate::memory::MESSAGE_ACKED.into()),
);
entry.insert("at".into(), Value::from(now));
entry.insert("by".into(), Value::String(by));
if let Some(n) = note {
entry.insert("note".into(), Value::String(n.to_string()));
}
let mut history = m
.metadata
.get(MNEMO_META_STATUS_HISTORY)
.and_then(Value::as_array)
.cloned()
.unwrap_or_default();
history.push(Value::Object(entry));
m.metadata
.insert(MNEMO_META_STATUS_HISTORY.into(), Value::Array(history));
m.metadata.insert(
MNEMO_META_STATUS.into(),
Value::String(crate::memory::MESSAGE_ACKED.into()),
);
m.metadata.insert(
MNEMO_META_DELIVERY.into(),
Value::String(crate::memory::delivery::DONE.into()),
);
m.metadata
.insert(MNEMO_META_ACKED_AT.into(), Value::from(now));
self.put(m)?;
Ok(WriteOutcome::Updated)
}
/// 13a (R-010.8): delete messages acked more than `older_than_secs` ago
/// ([`crate::memory::MESSAGE_ACKED_KEEP_SECS`] by default in the CLI).
/// Returns how many went. `compact` drops the same ones.
pub fn prune_messages(&mut self, older_than_secs: i64) -> Result<usize> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let now = memory::now_secs();
let old: Vec<Ulid> = self
.messages()?
.iter()
.filter_map(Message::from_memory)
.filter(|m| prunable(m, now, older_than_secs))
.map(|m| m.id)
.collect();
for id in &old {
self.delete(id)?;
}
Ok(old.len())
}
}
/// Whether a message is acked and older than `keep` seconds.
fn prunable(m: &Message, now: i64, keep: i64) -> bool {
m.status == crate::memory::MESSAGE_ACKED && m.acked_at.is_some_and(|t| now - t > keep)
}
/// 13a (R-010.8): whether `compact` leaves this record out: a message acked
/// more than [`crate::memory::MESSAGE_ACKED_KEEP_SECS`] ago.
pub(super) fn compact_drops(m: &Memory, now: i64) -> bool {
Message::from_memory(m)
.is_some_and(|msg| prunable(&msg, now, crate::memory::MESSAGE_ACKED_KEEP_SECS))
}
// --- 13a step 3: claims and presence (13.3) ---------------------------------
use crate::memory::{
CLAIM_LAPSE_GRACE_SECS, CLAIM_TOPIC_MAX, CLAIM_TTL_DEFAULT, CLAIM_TTL_MAX, CLAIM_TTL_MIN,
MNEMO_META_AGENT, MNEMO_META_CLAIM_TOKEN, MNEMO_META_DEAD_SEEN_AT, MNEMO_META_EXPIRES_AT,
MNEMO_META_HOLDER, MNEMO_META_HOLDER_LOCK, MNEMO_META_KIND_CLAIM, MNEMO_META_KIND_PRESENCE,
MNEMO_META_LAST_ACTIVE, MNEMO_META_LEASE, MNEMO_META_PRESENCE_LOCK, MNEMO_META_TOOL,
MNEMO_META_TOPIC, PRESENCE_EVERY_SECS,
};
/// A claim's or presence record's locator: its topic or its agent.
fn locator_of(m: &Memory) -> Option<&str> {
let key = match m.metadata.get(MNEMO_META_KIND).and_then(Value::as_str)? {
MNEMO_META_KIND_CLAIM => MNEMO_META_TOPIC,
MNEMO_META_KIND_PRESENCE => MNEMO_META_AGENT,
crate::memory::MNEMO_META_KIND_TASK => crate::memory::MNEMO_META_SLUG,
_ => return None,
};
m.metadata.get(key).and_then(Value::as_str)
}
fn locator_hash_of(kind: &str, locator: &str) -> u32 {
super::records::fnv1a32(&[b"_mnemo.coordination", kind.as_bytes(), locator.as_bytes()])
}
/// The hash a coordination record's v9 row carries in place of a thread
/// hash: of its kind and topic (a claim) or agent (presence); `0` for the
/// other kinds.
pub(super) fn locator_hash(m: &Memory) -> u32 {
match (
m.metadata.get(MNEMO_META_KIND).and_then(Value::as_str),
locator_of(m),
) {
(Some(kind), Some(l)) => locator_hash_of(kind, l),
_ => 0,
}
}
/// 13a (13.3): what [`Mnemo::claim`] got.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ClaimOutcome {
/// The claim is this agent's. Keep the token: `renew`, `release` and
/// `check_claim` need it.
Claimed {
/// The claim's token.
token: String,
/// When it lapses unless renewed, in unix seconds.
expires_at: i64,
},
/// Another agent (or this one, in another process or session) holds it.
HeldBy {
/// The holder.
agent: String,
/// When it lapses unless renewed.
expires_at: i64,
},
}
/// 13a (13.3): what `renew`, `release` and `check_claim` found.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClaimStatus {
/// The claim is still the token's, until `expires_at`.
Held {
/// When it lapses unless renewed, in unix seconds.
expires_at: i64,
},
/// Released.
Released,
/// The claim lapsed or another agent holds it now: whatever it guarded
/// may have been done by someone else. Check before an irreversible
/// step.
Lost,
}
/// 13a (13.3): a live claim, as [`Mnemo::claims`] lists it (no token).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Claim {
/// What's claimed.
pub topic: String,
/// Who holds it.
pub holder: String,
/// When it lapses unless renewed.
pub expires_at: i64,
/// Its lease, in seconds.
pub lease: i64,
}
/// 13a (13.3): whether an agent's process is running, by its presence lock.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Alive {
/// Its long-lived process holds its presence lock.
Yes,
/// It registered a lock, and the lock is free: its process is gone.
No,
/// It has no long-lived process (the CLI, a session without the MCP
/// server), or the probe couldn't tell.
Unknown,
}
/// 13a (13.3): one agent, as [`Mnemo::who`] lists it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Presence {
/// The agent id.
pub agent: String,
/// Its role, if it gave one.
pub role: Option<String>,
/// The tool it last wrote through (`mcp`, `cli`, ...).
pub tool: Option<String>,
/// When it last wrote, in unix seconds (updated at most once a minute).
pub last_active: i64,
/// Whether its process is running.
pub alive: Alive,
/// The topics it holds live claims on.
pub claims: Vec<String>,
}
fn meta_i64(m: &Memory, k: &str) -> Option<i64> {
m.metadata.get(k).and_then(Value::as_i64)
}
fn meta_string(m: &Memory, k: &str) -> Option<String> {
m.metadata
.get(k)
.and_then(Value::as_str)
.map(str::to_string)
}
/// A claim record's expiry, bounded by one lease from `now`: a clock that
/// stepped back can't stretch a claim past its lease.
fn effective_expiry(m: &Memory, now: i64) -> Option<i64> {
let token = m
.metadata
.get(MNEMO_META_CLAIM_TOKEN)
.and_then(Value::as_str)?;
if token.is_empty() {
return None;
}
let expires = meta_i64(m, MNEMO_META_EXPIRES_AT)?;
let lease = meta_i64(m, MNEMO_META_LEASE).unwrap_or(CLAIM_TTL_DEFAULT);
Some(expires.min(now.saturating_add(lease)))
}
/// Whether a claim record is held at `now`.
fn is_live(m: &Memory, now: i64) -> bool {
effective_expiry(m, now).is_some_and(|e| now < e)
}
fn new_token() -> String {
format!(
"{:016x}{:016x}",
crate::crypto::random_u64(),
crate::crypto::random_u64()
)
}
fn check_topic(topic: &str) -> Result<()> {
if topic.trim().is_empty() || topic.contains('\n') || topic.len() > CLAIM_TOPIC_MAX {
return Err(MnemoError::Invalid(format!(
"a claim's topic is one line, not empty, at most {CLAIM_TOPIC_MAX} bytes"
)));
}
Ok(())
}
fn check_ttl(ttl: Option<i64>) -> Result<i64> {
let t = ttl.unwrap_or(CLAIM_TTL_DEFAULT);
if !(CLAIM_TTL_MIN..=CLAIM_TTL_MAX).contains(&t) {
return Err(MnemoError::Invalid(format!(
"a claim's ttl is {CLAIM_TTL_MIN} s to {CLAIM_TTL_MAX} s (24 h); got {t}"
)));
}
Ok(t)
}
impl Mnemo {
/// 13a: the clock claims, leases and presence run on, in unix seconds:
/// the wall clock (dev-hooks builds let tests shift it).
pub fn coordination_now(&self) -> i64 {
#[cfg(feature = "dev-hooks")]
let env: i64 = std::env::var("MNEMO_DEV_CLOCK_OFFSET")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(0);
#[cfg(not(feature = "dev-hooks"))]
let env: i64 = 0;
memory::now_secs() + self.clock_offset + env
}
/// **Not a stable API** (dev-hooks builds only): shift this handle's
/// coordination clock by `secs`.
#[cfg(feature = "dev-hooks")]
#[doc(hidden)]
pub fn __set_clock_offset_for_testing(&mut self, secs: i64) {
self.clock_offset = secs;
}
fn claim_agent(&self) -> Result<String> {
self.author.clone().ok_or_else(|| {
MnemoError::Invalid(
"a claim needs an agent: set MNEMO_AGENT (or --as), or call set_author".into(),
)
})
}
fn find_claim(&mut self, topic: &str) -> Result<Option<Memory>> {
Ok(self
.coordination_records(MNEMO_META_KIND_CLAIM, Some(topic))?
.into_iter()
.next())
}
/// 13a (13.3): claim `topic` for this handle's author for `ttl` seconds
/// ([`crate::memory::CLAIM_TTL_DEFAULT`], 15 minutes, by default; 30 s
/// to 24 h).
///
/// A claim is a record, not a lock, and it's advisory: nothing refuses
/// a write because of one. In a shared handle's write window it's a
/// compare-and-set: two processes can't both win. The topic is free
/// when nobody holds it, its claim lapsed at `expires_at`, or its
/// holder registered a presence lock when it claimed and has been seen
/// not alive twice, [`crate::memory::CLAIM_LAPSE_GRACE_SECS`] apart
/// (each look is recorded on the claim). A topic this agent already
/// holds is [`ClaimOutcome::HeldBy`] too: a token can't be recovered.
/// One record per topic, changed in place: a new holder gets a new
/// token.
pub fn claim(&mut self, topic: &str, ttl: Option<i64>) -> Result<ClaimOutcome> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
check_topic(topic)?;
let ttl = check_ttl(ttl)?;
let agent = self.claim_agent()?;
let now = self.coordination_now();
let existing = self.find_claim(topic)?;
if let Some(mut m) = existing.clone() {
if is_live(&m, now) {
let holder = meta_string(&m, MNEMO_META_HOLDER).unwrap_or_default();
let expires = effective_expiry(&m, now).unwrap_or(now);
let mut changed = false;
let mut lapse = false;
let had_lock = m
.metadata
.get(MNEMO_META_HOLDER_LOCK)
.and_then(Value::as_bool)
.unwrap_or(false);
if had_lock && holder != agent {
match crate::shared::probe_presence_once(&self.path, &holder) {
Some(false) => match meta_i64(&m, MNEMO_META_DEAD_SEEN_AT) {
Some(t) if now - t >= CLAIM_LAPSE_GRACE_SECS => lapse = true,
Some(_) => {}
None => {
m.metadata
.insert(MNEMO_META_DEAD_SEEN_AT.into(), Value::from(now));
changed = true;
}
},
_ => {
changed |= m.metadata.remove(MNEMO_META_DEAD_SEEN_AT).is_some();
}
}
}
if !lapse {
// A clock that stepped back: bring the stored expiry
// within one lease.
if meta_i64(&m, MNEMO_META_EXPIRES_AT) != Some(expires) {
m.metadata
.insert(MNEMO_META_EXPIRES_AT.into(), Value::from(expires));
changed = true;
}
if changed {
self.put(m)?;
}
return Ok(ClaimOutcome::HeldBy {
agent: holder,
expires_at: expires,
});
}
}
}
let token = new_token();
let expires_at = now + ttl;
let mut meta = existing
.as_ref()
.map(|m| m.metadata.clone())
.unwrap_or_default();
meta.remove(MNEMO_META_DEAD_SEEN_AT);
meta.insert(MNEMO_META_TOPIC.into(), Value::String(topic.to_string()));
meta.insert(MNEMO_META_HOLDER.into(), Value::String(agent));
meta.insert(MNEMO_META_CLAIM_TOKEN.into(), Value::String(token.clone()));
meta.insert(MNEMO_META_EXPIRES_AT.into(), Value::from(expires_at));
meta.insert(MNEMO_META_LEASE.into(), Value::from(ttl));
meta.insert(
MNEMO_META_HOLDER_LOCK.into(),
Value::Bool(self.presence_held),
);
match existing {
Some(mut m) => {
m.metadata = meta;
self.put(m)?;
}
None => {
self.put_coordination_with_id(
Ulid::new(),
MNEMO_META_KIND_CLAIM,
format!("claim {topic}"),
meta,
)?;
}
}
Ok(ClaimOutcome::Claimed { token, expires_at })
}
/// 13a (13.3): renew a claim for `ttl` seconds from now (its lease by
/// default). [`ClaimStatus::Lost`] when it lapsed or isn't the token's.
pub fn renew(&mut self, topic: &str, token: &str, ttl: Option<i64>) -> Result<ClaimStatus> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let now = self.coordination_now();
let Some(mut m) = self.find_claim(topic)? else {
return Ok(ClaimStatus::Lost);
};
if !is_live(&m, now) || meta_string(&m, MNEMO_META_CLAIM_TOKEN).as_deref() != Some(token) {
return Ok(ClaimStatus::Lost);
}
let lease = match ttl {
Some(t) => check_ttl(Some(t))?,
None => meta_i64(&m, MNEMO_META_LEASE).unwrap_or(CLAIM_TTL_DEFAULT),
};
let expires_at = now + lease;
m.metadata
.insert(MNEMO_META_EXPIRES_AT.into(), Value::from(expires_at));
m.metadata
.insert(MNEMO_META_LEASE.into(), Value::from(lease));
m.metadata.remove(MNEMO_META_DEAD_SEEN_AT);
self.put(m)?;
Ok(ClaimStatus::Held { expires_at })
}
/// 13a (13.3): release a claim. [`ClaimStatus::Lost`] when it already
/// lapsed or isn't the token's (then nothing changes).
pub fn release(&mut self, topic: &str, token: &str) -> Result<ClaimStatus> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let now = self.coordination_now();
let Some(mut m) = self.find_claim(topic)? else {
return Ok(ClaimStatus::Lost);
};
if !is_live(&m, now) || meta_string(&m, MNEMO_META_CLAIM_TOKEN).as_deref() != Some(token) {
return Ok(ClaimStatus::Lost);
}
m.metadata.remove(MNEMO_META_CLAIM_TOKEN);
m.metadata.remove(MNEMO_META_DEAD_SEEN_AT);
m.metadata
.insert(MNEMO_META_EXPIRES_AT.into(), Value::from(now));
self.put(m)?;
Ok(ClaimStatus::Released)
}
/// 13a (13.3): whether `token` still holds `topic`. Call it before an
/// irreversible step. Reads only.
pub fn check_claim(&mut self, topic: &str, token: &str) -> Result<ClaimStatus> {
let now = self.coordination_now();
let Some(m) = self.find_claim(topic)? else {
return Ok(ClaimStatus::Lost);
};
if meta_string(&m, MNEMO_META_CLAIM_TOKEN).as_deref() != Some(token) {
return Ok(ClaimStatus::Lost);
}
match effective_expiry(&m, now) {
Some(e) if now < e => Ok(ClaimStatus::Held { expires_at: e }),
_ => Ok(ClaimStatus::Lost),
}
}
/// 13a (13.3): the live claims, by topic. Reads only.
pub fn claims(&mut self) -> Result<Vec<Claim>> {
let now = self.coordination_now();
let mut out: Vec<Claim> = self
.coordination_records(MNEMO_META_KIND_CLAIM, None)?
.iter()
.filter(|m| is_live(m, now))
.map(|m| Claim {
topic: meta_string(m, MNEMO_META_TOPIC).unwrap_or_default(),
holder: meta_string(m, MNEMO_META_HOLDER).unwrap_or_default(),
expires_at: effective_expiry(m, now).unwrap_or(now),
lease: meta_i64(m, MNEMO_META_LEASE).unwrap_or(CLAIM_TTL_DEFAULT),
})
.collect();
out.sort_by(|a, b| a.topic.cmp(&b.topic));
Ok(out)
}
/// 13a (13.3): record that this handle's author is active, with its
/// `role` and `tool`: its presence record's `last_active`, changed in
/// place, at most once a minute (sooner when the role, the tool or the
/// presence lock changed). Call it before a write the agent makes
/// anyway, so it rides that commit. `false` when nothing was written
/// (too soon, or no author).
pub fn touch_presence(&mut self, role: Option<&str>, tool: Option<&str>) -> Result<bool> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let Some(agent) = self.author.clone() else {
return Ok(false);
};
let now = self.coordination_now();
let existing = self
.coordination_records(MNEMO_META_KIND_PRESENCE, Some(&agent))?
.into_iter()
.next();
let role_v = role.map(|r| Value::String(r.to_string()));
let tool_v = tool.map(|t| Value::String(t.to_string()));
// RB-012.4: only a process holding the presence lock writes the
// lock flag and the tool; another (the CLI, beside a server that
// holds it) moves `last_active` alone.
let defer = !self.presence_held
&& existing.as_ref().is_some_and(|m| {
m.metadata
.get(MNEMO_META_PRESENCE_LOCK)
.and_then(Value::as_bool)
.unwrap_or(false)
});
if let Some(m) = &existing {
let fresh = meta_i64(m, MNEMO_META_LAST_ACTIVE)
.is_some_and(|t| now >= t && now - t < PRESENCE_EVERY_SECS);
let same = defer
|| (m.metadata.get(crate::memory::MNEMO_META_ROLE) == role_v.as_ref()
&& m.metadata.get(MNEMO_META_TOOL) == tool_v.as_ref()
&& m.metadata
.get(MNEMO_META_PRESENCE_LOCK)
.and_then(Value::as_bool)
== Some(self.presence_held));
if fresh && same {
return Ok(false);
}
}
if defer {
let mut m = existing.expect("checked");
m.metadata
.insert(MNEMO_META_LAST_ACTIVE.into(), Value::from(now));
self.put(m)?;
return Ok(true);
}
let mut meta = existing
.as_ref()
.map(|m| m.metadata.clone())
.unwrap_or_default();
meta.insert(MNEMO_META_AGENT.into(), Value::String(agent.clone()));
for (k, v) in [
(crate::memory::MNEMO_META_ROLE, role_v),
(MNEMO_META_TOOL, tool_v),
] {
match v {
Some(v) => {
meta.insert(k.into(), v);
}
None => {
meta.remove(k);
}
}
}
meta.insert(MNEMO_META_LAST_ACTIVE.into(), Value::from(now));
meta.insert(
MNEMO_META_PRESENCE_LOCK.into(),
Value::Bool(self.presence_held),
);
match existing {
Some(mut m) => {
m.metadata = meta;
self.put(m)?;
}
None => {
self.put_coordination_with_id(
Ulid::new(),
MNEMO_META_KIND_PRESENCE,
format!("presence {agent}"),
meta,
)?;
}
}
Ok(true)
}
/// 13a (13.3): every agent with a presence record, most recently active
/// first: its role, tool, `last_active`, whether its process is alive
/// (by its presence lock; [`Alive::Unknown`] for an agent with no
/// long-lived process), and the topics it holds. Reads only.
pub fn who(&mut self) -> Result<Vec<Presence>> {
let claims = self.claims()?;
let records = self.coordination_records(MNEMO_META_KIND_PRESENCE, None)?;
let mut out: Vec<Presence> = records
.iter()
.map(|m| {
let agent = meta_string(m, MNEMO_META_AGENT).unwrap_or_default();
let locked = m
.metadata
.get(MNEMO_META_PRESENCE_LOCK)
.and_then(Value::as_bool)
.unwrap_or(false);
let alive = if !locked {
Alive::Unknown
} else {
match crate::shared::probe_presence(&self.path, &agent) {
Some(true) => Alive::Yes,
Some(false) => Alive::No,
None => Alive::Unknown,
}
};
Presence {
claims: claims
.iter()
.filter(|c| c.holder == agent)
.map(|c| c.topic.clone())
.collect(),
role: meta_string(m, crate::memory::MNEMO_META_ROLE),
tool: meta_string(m, MNEMO_META_TOOL),
last_active: meta_i64(m, MNEMO_META_LAST_ACTIVE).unwrap_or(0),
alive,
agent,
}
})
.collect();
out.sort_by(|a, b| {
b.last_active
.cmp(&a.last_active)
.then(a.agent.cmp(&b.agent))
});
Ok(out)
}
}
// --- 13a step 4: tasks (13.1) ------------------------------------------------
use crate::memory::{
task_status, MNEMO_META_AFTER, MNEMO_META_CREATOR, MNEMO_META_KIND_TASK, MNEMO_META_OWNER,
MNEMO_META_PRE_APPROVED, MNEMO_META_SLUG, TASK_CLAIM_TTL, TASK_SLUG_MAX, TASK_TRANSITIONS,
};
/// 13a (13.1): a task to add ([`Mnemo::task_add`]).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TaskSpec {
/// One line, at most 200 bytes.
pub title: String,
/// At most 8 KB; put more in a fact and name it.
pub body: String,
/// The role that may take it (`worker`); `None`, any.
pub role: Option<String>,
/// The slugs of the tasks it waits for; each must exist.
pub after: Vec<String>,
/// Its owner may go from `ready` to `done` without the creator.
pub pre_approved: bool,
/// Its name; made from the title when left out.
pub slug: Option<String>,
}
/// 13a (13.1): one task.
#[derive(Debug, Clone, PartialEq)]
pub struct Task {
/// The record's id.
pub id: Ulid,
/// Its name.
pub slug: String,
/// One line.
pub title: String,
/// The body (may be empty).
pub body: String,
/// The role that may take it.
pub role: Option<String>,
/// `queued`, `active`, `ready`, `done` or `blocked`.
pub status: String,
/// The agent that took it.
pub owner: Option<String>,
/// The agent that added it.
pub creator: String,
/// The slugs it waits for.
pub after: Vec<String>,
/// Whether its owner may set `done`.
pub pre_approved: bool,
/// When it was added, in unix seconds.
pub created_at: i64,
/// Its status changes and notes, oldest first (`from`, `to`, `at`,
/// `by`, and `note` or `reclaimed_from` when there's one).
pub history: Vec<Value>,
}
impl Task {
/// The task a record holds, if it's one.
pub fn from_memory(m: &Memory) -> Option<Task> {
if m.metadata.get(MNEMO_META_KIND).and_then(Value::as_str) != Some(MNEMO_META_KIND_TASK) {
return None;
}
let (title, body) = match m.content.split_once("\n\n") {
Some((a, b)) => (a.to_string(), b.to_string()),
None => (m.content.clone(), String::new()),
};
Some(Task {
id: m.id,
slug: meta_string(m, MNEMO_META_SLUG).unwrap_or_default(),
title,
body,
role: meta_string(m, crate::memory::MNEMO_META_ROLE),
status: meta_string(m, MNEMO_META_STATUS).unwrap_or_default(),
owner: meta_string(m, MNEMO_META_OWNER),
creator: meta_string(m, MNEMO_META_CREATOR).unwrap_or_default(),
after: m
.metadata
.get(MNEMO_META_AFTER)
.and_then(Value::as_array)
.map(|a| {
a.iter()
.filter_map(|v| v.as_str().map(str::to_string))
.collect()
})
.unwrap_or_default(),
pre_approved: m
.metadata
.get(MNEMO_META_PRE_APPROVED)
.and_then(Value::as_bool)
.unwrap_or(false),
created_at: m.created_at,
history: m
.metadata
.get(MNEMO_META_STATUS_HISTORY)
.and_then(Value::as_array)
.cloned()
.unwrap_or_default(),
})
}
/// Whether an agent of `role` may take it (a task with no role is any
/// role's).
fn for_role(&self, role: Option<&str>) -> bool {
self.role.is_none() || self.role.as_deref() == role
}
}
/// 13a (13.1): a task [`Mnemo::task_next`] took, with its claim's token.
#[derive(Debug, Clone, PartialEq)]
pub struct TaskTaken {
/// The task, now `active` and this agent's.
pub task: Task,
/// The `task:<slug>` claim's token (give it to `task_update` for the
/// strict check).
pub token: String,
/// When the claim lapses unless an update renews it.
pub expires_at: i64,
/// The owner whose claim lapsed, when this took an abandoned task back.
pub reclaimed_from: Option<String>,
}
/// 13a (13.1): the claim an owner holds after a [`Mnemo::task_update`]
/// (RB-012.1), so it always has the current token.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TaskClaim {
/// The `task:<slug>` claim's token.
pub token: String,
/// When it lapses unless an update renews it.
pub expires_at: i64,
/// The claim had lapsed and nobody took the task, so the owner took
/// the claim again (with a new token).
pub retaken: bool,
}
/// 13a (13.1): why a [`Mnemo::task_update`] changed nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TaskLost {
/// Another agent took the task back after the caller's claim lapsed.
TakenBack,
/// The token isn't the task's claim's (another session of the same
/// agent may hold it).
OtherToken,
}
/// 13a (13.1): what [`Mnemo::task_update`] did.
#[derive(Debug, Clone, PartialEq)]
pub enum TaskOutcome {
/// Changed (or a note added). For the owner, the claim it now holds:
/// renewed, or taken again after a lapse; `None` when the change
/// released it (`ready`, `done`, `queued`) or wasn't the owner's.
Updated(Box<Task>, Option<TaskClaim>),
/// Nothing changed: don't carry on with it.
Lost(TaskLost),
}
/// The slug a title gives: lower case, runs of anything but `a-z0-9`
/// as one `-`, at most [`TASK_SLUG_MAX`] bytes.
pub fn slug_of(title: &str) -> String {
let mut out = String::new();
for c in title.chars() {
if c.is_ascii_alphanumeric() {
out.push(c.to_ascii_lowercase());
} else if !out.ends_with('-') && !out.is_empty() {
out.push('-');
}
if out.len() >= TASK_SLUG_MAX {
break;
}
}
out.trim_end_matches('-').to_string()
}
fn valid_slug(s: &str) -> bool {
!s.is_empty()
&& s.len() <= TASK_SLUG_MAX
&& s.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"._-".contains(&b))
}
fn task_topic(slug: &str) -> String {
format!("task:{slug}")
}
impl Mnemo {
fn task_records(&mut self, slug: Option<&str>) -> Result<Vec<Memory>> {
let mut v = self.coordination_records(MNEMO_META_KIND_TASK, slug)?;
v.sort_by_key(|m| m.created_at);
Ok(v)
}
fn task_record(&mut self, slug: &str) -> Result<Option<Memory>> {
Ok(self.task_records(Some(slug))?.into_iter().next())
}
/// 13a (13.1): one task by slug. Reads only.
pub fn task(&mut self, slug: &str) -> Result<Option<Task>> {
Ok(self.task_record(slug)?.as_ref().and_then(Task::from_memory))
}
/// 13a (13.1): the tasks, oldest first, with `status` and for `role`
/// when given (a task with no role matches any). Reads only.
pub fn tasks(&mut self, status: Option<&str>, role: Option<&str>) -> Result<Vec<Task>> {
Ok(self
.task_records(None)?
.iter()
.filter_map(Task::from_memory)
.filter(|t| status.is_none_or(|s| t.status == s))
.filter(|t| role.is_none() || t.for_role(role))
.collect())
}
/// 13a (13.1): the `queued` tasks an agent of `role` may take now (all
/// their `after` tasks `done`), oldest first. Reads only.
pub fn ready_tasks(&mut self, role: Option<&str>) -> Result<Vec<Task>> {
let all: Vec<Task> = self
.task_records(None)?
.iter()
.filter_map(Task::from_memory)
.collect();
let done: std::collections::HashSet<&str> = all
.iter()
.filter(|t| t.status == task_status::DONE)
.map(|t| t.slug.as_str())
.collect();
Ok(all
.iter()
.filter(|t| t.status == task_status::QUEUED && t.for_role(role))
.filter(|t| t.after.iter().all(|a| done.contains(a.as_str())))
.cloned()
.collect())
}
/// 13a (13.1): add a task, `queued`, created by this handle's author.
/// A slug that already names a task updates its title, body, role,
/// `after` and `pre_approved` in place (its status, owner and history
/// stay); `Unchanged` when nothing differs. `after` must name existing
/// tasks, never itself.
pub fn task_add(&mut self, spec: &TaskSpec) -> Result<(Task, WriteOutcome)> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let title = spec.title.trim();
if title.is_empty()
|| title.contains('\n')
|| title.len() > crate::memory::MESSAGE_SUBJECT_MAX
{
return Err(MnemoError::Invalid(format!(
"a task's title is one line, not empty, at most {} bytes",
crate::memory::MESSAGE_SUBJECT_MAX
)));
}
if spec.body.len() > crate::memory::MESSAGE_BODY_MAX {
return Err(MnemoError::Invalid(format!(
"a task's body is at most {} bytes; put more in a fact and name it",
crate::memory::MESSAGE_BODY_MAX
)));
}
let slug = match &spec.slug {
Some(s) => s.clone(),
None => slug_of(title),
};
if !valid_slug(&slug) {
return Err(MnemoError::Invalid(format!(
"{slug:?} isn't a slug: 1 to {TASK_SLUG_MAX} of a-z, 0-9, '.', '_' and '-' \
(give one with --slug)"
)));
}
if let Some(r) = &spec.role {
if !crate::memory::valid_agent_id(r) {
return Err(MnemoError::Invalid(format!("{r:?} isn't a role")));
}
}
for a in &spec.after {
if a == &slug {
return Err(MnemoError::Invalid("a task can't wait for itself".into()));
}
if self.task_record(a)?.is_none() {
return Err(MnemoError::Invalid(format!("after: no task {a:?}")));
}
}
let content = if spec.body.is_empty() {
title.to_string()
} else {
format!("{title}\n\n{}", spec.body)
};
let set = |meta: &mut Map<String, Value>| {
match &spec.role {
Some(r) => {
meta.insert(
crate::memory::MNEMO_META_ROLE.into(),
Value::String(r.clone()),
);
}
None => {
meta.remove(crate::memory::MNEMO_META_ROLE);
}
}
meta.insert(
MNEMO_META_AFTER.into(),
Value::Array(
spec.after
.iter()
.map(|a| Value::String(a.clone()))
.collect(),
),
);
meta.insert(
MNEMO_META_PRE_APPROVED.into(),
Value::Bool(spec.pre_approved),
);
};
if let Some(mut m) = self.task_record(&slug)? {
let t = Task::from_memory(&m).expect("a task");
let mut meta = m.metadata.clone();
set(&mut meta);
if meta == m.metadata && m.content == content {
return Ok((t, WriteOutcome::Unchanged));
}
// RB-012.2: a finished task is never reopened by a re-add, and
// only its creator changes what a task is.
if t.status == task_status::DONE {
let mut n = 2;
while self.task_record(&format!("{slug}-{n}"))?.is_some() {
n += 1;
}
return Err(MnemoError::Invalid(format!(
"{slug} is done; give a new slug ({slug}-{n}?)"
)));
}
let caller = self.author.clone().unwrap_or_else(|| "anonymous".into());
if caller != t.creator {
return Err(MnemoError::Invalid(format!(
"that slug is {}'s task; it wasn't changed",
t.creator
)));
}
// RB-012.6 item 5: an `after` that closes a cycle.
if self.closes_cycle(&slug, &spec.after)? {
return Err(MnemoError::Invalid(format!(
"after: that would make {slug} wait for itself, through the tasks it waits for"
)));
}
m.metadata = meta;
m.content = content;
let id = m.id;
self.put(m)?;
let t = Task::from_memory(&self.get(&id)?).expect("a task");
return Ok((t, WriteOutcome::Updated));
}
let mut meta = Map::new();
meta.insert(MNEMO_META_SLUG.into(), Value::String(slug));
meta.insert(
MNEMO_META_STATUS.into(),
Value::String(task_status::QUEUED.into()),
);
meta.insert(
MNEMO_META_CREATOR.into(),
Value::String(self.author.clone().unwrap_or_else(|| "anonymous".into())),
);
meta.insert(
MNEMO_META_DELIVERY.into(),
Value::String(crate::memory::delivery::SENT.into()),
);
set(&mut meta);
let id = self.put_coordination_with_id(Ulid::new(), MNEMO_META_KIND_TASK, content, meta)?;
let t = Task::from_memory(&self.get(&id)?).expect("a task");
Ok((t, WriteOutcome::Added))
}
/// Whether `slug` waiting for `after` closes a cycle: a task in `after`
/// that waits (through its own `after`, transitively) for `slug`.
fn closes_cycle(&mut self, slug: &str, after: &[String]) -> Result<bool> {
let all: std::collections::HashMap<String, Vec<String>> = self
.task_records(None)?
.iter()
.filter_map(Task::from_memory)
.map(|t| (t.slug, t.after))
.collect();
let mut stack: Vec<&str> = after.iter().map(String::as_str).collect();
let mut seen = std::collections::HashSet::new();
while let Some(s) = stack.pop() {
if s == slug {
return Ok(true);
}
if seen.insert(s.to_string()) {
if let Some(next) = all.get(s) {
stack.extend(next.iter().map(String::as_str));
}
}
}
Ok(false)
}
/// Append a `status_history` entry.
fn task_history(
m: &mut Memory,
from: &str,
to: &str,
by: &str,
at: i64,
extra: &[(&str, &str)],
) {
let mut e = Map::new();
e.insert("from".into(), Value::String(from.into()));
e.insert("to".into(), Value::String(to.into()));
e.insert("at".into(), Value::from(at));
e.insert("by".into(), Value::String(by.into()));
for (k, v) in extra {
e.insert((*k).into(), Value::String((*v).into()));
}
let mut h = m
.metadata
.get(MNEMO_META_STATUS_HISTORY)
.and_then(Value::as_array)
.cloned()
.unwrap_or_default();
h.push(Value::Object(e));
m.metadata
.insert(MNEMO_META_STATUS_HISTORY.into(), Value::Array(h));
}
/// 13a (13.1): take the next task for `role`, for this handle's author,
/// all in one exclusive window in a shared handle: the oldest `queued`
/// task for the role whose `after` tasks are all `done`; failing that
/// (R-010.7), an `active` one whose `task:<slug>` claim has lapsed,
/// taken back from its owner with a `status_history` entry naming it.
/// It claims `task:<slug>` for [`crate::memory::TASK_CLAIM_TTL`] and
/// sets `active` and the owner. `None` when there's nothing to take.
pub fn task_next(&mut self, role: Option<&str>) -> Result<Option<TaskTaken>> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let agent = self.claim_agent()?;
let now = self.coordination_now();
let ready: Vec<Ulid> = self.ready_tasks(role)?.iter().map(|t| t.id).collect();
// Taken tasks (`active`, or `blocked`: RB-012.3) for the role. The
// caller's own come first (RB-012.1): a worker whose claim lapsed
// gets its own task back before anything new. `claim()` decides
// which are free.
let taken: Vec<Task> = self
.task_records(None)?
.iter()
.filter_map(Task::from_memory)
.filter(|t| t.status == task_status::ACTIVE || t.status == task_status::BLOCKED)
.filter(|t| t.for_role(role))
.collect();
let (own, others): (Vec<&Task>, Vec<&Task>) = taken
.iter()
.partition(|t| t.owner.as_deref() == Some(agent.as_str()));
for (id, back) in own
.iter()
.map(|t| (t.id, true))
.chain(ready.into_iter().map(|i| (i, false)))
.chain(others.iter().map(|t| (t.id, true)))
{
let mut m = self.get(&id)?;
let t = Task::from_memory(&m).expect("a task");
let (token, expires_at) =
match self.claim(&task_topic(&t.slug), Some(TASK_CLAIM_TTL))? {
ClaimOutcome::Claimed { token, expires_at } => (token, expires_at),
ClaimOutcome::HeldBy { .. } => continue,
};
// The claim's write may have changed the catalog: read the task
// again before changing it.
m = self.get(&id)?;
let old = t.owner.clone();
let own = old.as_deref() == Some(agent.as_str());
if back && own {
Self::task_history(
&mut m,
&t.status,
task_status::ACTIVE,
&agent,
now,
&[("note", "taken again after its claim lapsed")],
);
} else if back {
let from = old.clone().unwrap_or_default();
Self::task_history(
&mut m,
&t.status,
task_status::ACTIVE,
&agent,
now,
&[("reclaimed_from", from.as_str())],
);
} else {
Self::task_history(
&mut m,
task_status::QUEUED,
task_status::ACTIVE,
&agent,
now,
&[],
);
}
m.metadata.insert(
MNEMO_META_STATUS.into(),
Value::String(task_status::ACTIVE.into()),
);
m.metadata
.insert(MNEMO_META_OWNER.into(), Value::String(agent.clone()));
self.put(m)?;
let task = Task::from_memory(&self.get(&id)?).expect("a task");
return Ok(Some(TaskTaken {
task,
token,
expires_at,
reclaimed_from: if back && !own { old } else { None },
}));
}
Ok(None)
}
/// 13a (13.1): change a task's status (along [`TASK_TRANSITIONS`]), or
/// add a `note` with none, as this handle's author.
/// - **Its owner** moves it from `active` to `ready`, `blocked` or
/// `queued` (handing it back) and from `blocked` to `active`, and may
/// add notes; each renews its `task:<slug>` claim. With a `token`,
/// the claim must be that token's. An owner whose task was taken back
/// (or whose token isn't the claim's) gets [`TaskOutcome::Lost`].
/// - **`ready → done`** is its creator's, or its owner's when
/// `pre_approved`.
/// - **`active → queued`** is anyone's once the owner's claim has
/// lapsed (an abandoned task).
/// - `queued → active` happens only through [`Mnemo::task_next`].
///
/// `ready`, `done` and `queued` release the claim.
pub fn task_update(
&mut self,
slug: &str,
status: Option<&str>,
note: Option<&str>,
token: Option<&str>,
) -> Result<TaskOutcome> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let agent = self.claim_agent()?;
let now = self.coordination_now();
let Some(m) = self.task_record(slug)? else {
return Err(MnemoError::Invalid(format!("no task {slug:?}")));
};
let t = Task::from_memory(&m).expect("a task");
let to = status.unwrap_or(&t.status).to_string();
if status.is_none() && note.is_none() {
return Err(MnemoError::Invalid("give a status or a note".into()));
}
// A token says the caller thinks the task is its own: when it isn't
// any more, that's the answer, whatever the change asked for.
if token.is_some() && t.owner.as_deref() != Some(agent.as_str()) {
return Ok(TaskOutcome::Lost(TaskLost::TakenBack));
}
let changing = to != t.status;
if changing && !TASK_TRANSITIONS.contains(&(t.status.as_str(), to.as_str())) {
return Err(MnemoError::Invalid(format!(
"a task can't go from {} to {to}",
t.status
)));
}
if changing && t.status == task_status::QUEUED {
return Err(MnemoError::Invalid(
"a queued task is taken with `task next`".into(),
));
}
let topic = task_topic(slug);
let claim = self.find_claim(&topic)?;
let live = claim.as_ref().filter(|c| is_live(c, now));
let claim_token = claim
.as_ref()
.and_then(|c| meta_string(c, MNEMO_META_CLAIM_TOKEN));
let held_by_owner = live.is_some_and(|c| meta_string(c, MNEMO_META_HOLDER) == t.owner);
let is_owner = t.owner.as_deref() == Some(agent.as_str());
let is_creator = t.creator == agent;
// Who may do this.
let by_owner = if changing && t.status == task_status::READY {
// ready -> done
if !(is_creator || (is_owner && t.pre_approved)) {
return Err(MnemoError::Invalid(format!(
"{slug} waits for its creator ({}) to set done",
t.creator
)));
}
false
} else if changing && to == task_status::QUEUED && !is_owner {
// Requeueing an abandoned task: anyone's, once its claim lapsed.
if let Some(c) = live {
return Err(MnemoError::Invalid(format!(
"{slug} is {}'s, and its claim is live until {}; only its owner can hand \
it back before then",
t.owner.as_deref().unwrap_or("its owner"),
crate::memory::date_time_of(effective_expiry(c, now).unwrap_or(now))
)));
}
false
} else if t.status == task_status::DONE || (!changing && t.status == task_status::READY) {
// A note on a finished task: its creator's or owner's.
if !(is_creator || is_owner) {
return Err(MnemoError::Invalid(format!("{slug} isn't yours")));
}
false
} else if !changing && is_creator && !is_owner {
false // its creator's note, at any status
} else if t.status == task_status::QUEUED {
// A note on a queued task: its creator's.
if !is_creator {
return Err(MnemoError::Invalid(format!("{slug} isn't yours")));
}
false
} else {
if !is_owner {
return Ok(TaskOutcome::Lost(TaskLost::TakenBack));
}
true
};
// The owner's claim (RB-012.1):
// - live and its own: a given token must be the claim's;
// - live and someone else's: taken back;
// - lapsed: a given token must be the lapsed claim's (the record
// keeps it), else another session took it in between; then the
// owner carries on and takes the claim again.
let mut renew_token = None;
if by_owner {
let token_ok = token.is_none_or(|tk| claim_token.as_deref() == Some(tk));
if live.is_some() && !held_by_owner {
return Ok(TaskOutcome::Lost(TaskLost::TakenBack));
}
if !token_ok {
return Ok(TaskOutcome::Lost(TaskLost::OtherToken));
}
if held_by_owner {
renew_token = claim_token.clone();
}
}
let mut m = m;
let mut extra: Vec<(&str, &str)> = Vec::new();
if let Some(n) = note {
extra.push(("note", n));
}
Self::task_history(&mut m, &t.status, &to, &agent, now, &extra);
m.metadata
.insert(MNEMO_META_STATUS.into(), Value::String(to.clone()));
if to == task_status::QUEUED {
m.metadata.remove(MNEMO_META_OWNER);
}
let id = m.id;
self.put(m)?;
let releases =
[task_status::READY, task_status::DONE, task_status::QUEUED].contains(&to.as_str());
let mut held: Option<TaskClaim> = None;
match (releases, by_owner, renew_token) {
(true, _, Some(tk)) => {
self.release(&topic, &tk)?;
}
(true, _, None) => {
// A lapsed or foreign-free claim: let it lapse.
if let Some(mut c) = self.find_claim(&topic)? {
if is_live(&c, now)
&& meta_string(&c, MNEMO_META_HOLDER) == t.owner
&& to == task_status::DONE
{
c.metadata.remove(MNEMO_META_CLAIM_TOKEN);
c.metadata
.insert(MNEMO_META_EXPIRES_AT.into(), Value::from(now));
self.put(c)?;
}
}
}
(false, true, Some(tk)) => {
if let ClaimStatus::Held { expires_at } =
self.renew(&topic, &tk, Some(TASK_CLAIM_TTL))?
{
held = Some(TaskClaim {
token: tk,
expires_at,
retaken: false,
});
}
}
(false, true, None) => {
// Lapsed, and nobody else took it: the owner takes it again,
// and gets the new token.
if let ClaimOutcome::Claimed { token, expires_at } =
self.claim(&topic, Some(TASK_CLAIM_TTL))?
{
held = Some(TaskClaim {
token,
expires_at,
retaken: true,
});
}
}
(false, false, _) => {}
}
Ok(TaskOutcome::Updated(
Box::new(Task::from_memory(&self.get(&id)?).expect("a task")),
held,
))
}
}
// --- 13a step 5: what's new for an agent, and the wake-up guardrails ---------
use crate::memory::{
HOP_CAP, MNEMO_META_WAKES, MNEMO_META_WAKE_PAUSED, MNEMO_META_WARNED_EXPIRY, PAIR_BURST,
PAIR_BURST_SECS, TASK_CLAIM_WARN_SECS, WAKE_COOLDOWN_SECS, WAKE_PER_HOUR,
};
/// 13a (13.1b): what's new for an agent since it was last shown
/// ([`Mnemo::new_work`]), for the per-turn hook.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct NewWork {
/// Unread messages to it, its role or everyone, not yet `seen`.
pub messages: Vec<Message>,
/// Tasks ready for its role, not yet `seen`.
pub tasks: Vec<Task>,
/// Its own task claims lapsing within
/// [`crate::memory::TASK_CLAIM_WARN_SECS`], not yet warned about at this
/// expiry.
pub lapsing: Vec<Claim>,
/// READY-NOTICE (v0.17.2): tasks it created that are `ready`, waiting
/// for its `done`, not yet shown to it as such. They wait for a
/// review, so they never wake a session.
pub ready_for_done: Vec<Task>,
/// The agent it was read for: [`Mnemo::mark_shown`] records this agent
/// as having seen the broadcasts and the role's tasks.
pub agent: Option<String>,
}
impl NewWork {
/// Whether there's nothing.
pub fn is_empty(&self) -> bool {
self.messages.is_empty()
&& self.tasks.is_empty()
&& self.lapsing.is_empty()
&& self.ready_for_done.is_empty()
}
}
/// 13a (13.1b): what [`Mnemo::wake`] decided.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Wake {
/// Wake the agent for these, now marked `delivered`: how many
/// pre-approved tasks and messages addressed to it by id.
Now {
/// Pre-approved tasks ready for its role.
tasks: usize,
/// Messages to it by id.
messages: usize,
},
/// Nothing that may wake it.
Nothing,
/// Something waits, but a guardrail holds it (its items stay `sent`,
/// and are seen at the next turn or session start).
Held(&'static str),
}
impl Mnemo {
/// 13a (13.1b): what's new for `agent` (and `role`) since it was last
/// shown. Reads only; [`Mnemo::mark_shown`] records that it was.
pub fn new_work(&mut self, agent: Option<&str>, role: Option<&str>) -> Result<NewWork> {
let now = self.coordination_now();
let unseen =
|d: &str| d == crate::memory::delivery::SENT || d == crate::memory::delivery::DELIVERED;
// RB-012.6 item 4: a message to the agent by id is new until it's
// seen; one to a role or everyone, and a role's task, until this
// agent has been shown it (`_mnemo.seen_by`).
let mut messages = Vec::new();
for msg in self.inbox(agent, role, true)? {
let broadcast = msg.to == "all" || msg.to.starts_with("role:");
let new = match (broadcast, agent) {
(true, Some(a)) => !seen_by(&self.get(&msg.id)?).iter().any(|s| s == a),
_ => unseen(&msg.delivery),
};
if new {
messages.push(msg);
}
}
let mut tasks = Vec::new();
for t in self.ready_tasks(role)? {
let m = self.get(&t.id)?;
let new = match agent {
Some(a) => !seen_by(&m).iter().any(|s| s == a),
None => meta_string(&m, MNEMO_META_DELIVERY).is_none_or(|d| unseen(&d)),
};
if new {
tasks.push(t);
}
}
// READY-NOTICE: its own tasks waiting for its `done`, not yet
// shown to it as such.
let mut ready_for_done = Vec::new();
if let Some(a) = agent {
for t in self.tasks(Some(crate::memory::task_status::READY), None)? {
if t.creator == a && !ready_seen_by(&self.get(&t.id)?).iter().any(|s| s == a) {
ready_for_done.push(t);
}
}
}
let mut lapsing = Vec::new();
if let Some(a) = agent {
for m in self.coordination_records(MNEMO_META_KIND_CLAIM, None)? {
let topic = meta_string(&m, MNEMO_META_TOPIC).unwrap_or_default();
if !topic.starts_with("task:")
|| meta_string(&m, MNEMO_META_HOLDER).as_deref() != Some(a)
|| !is_live(&m, now)
{
continue;
}
let e = effective_expiry(&m, now).unwrap_or(now);
if e - now <= TASK_CLAIM_WARN_SECS
&& meta_i64(&m, MNEMO_META_WARNED_EXPIRY) != Some(e)
{
lapsing.push(Claim {
topic,
holder: a.to_string(),
expires_at: e,
lease: meta_i64(&m, MNEMO_META_LEASE).unwrap_or(CLAIM_TTL_DEFAULT),
});
}
}
}
Ok(NewWork {
messages,
tasks,
lapsing,
ready_for_done,
agent: agent.map(str::to_string),
})
}
/// 13a (13.1b): record that `work` was shown: its messages and tasks
/// `seen`, its tasks waiting for a `done` shown to their creator
/// (READY-NOTICE), its lapsing claims warned about at their expiry. In
/// place.
pub fn mark_shown(&mut self, work: &NewWork) -> Result<()> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let ids: Vec<Ulid> = work.messages.iter().map(|m| m.id).collect();
// Seen by the agent the work was read for.
let who = work.agent.clone().or_else(|| self.author.clone());
let author = std::mem::replace(&mut self.author, who.clone());
let r = self.mark_seen(&ids);
self.author = author;
r?;
for t in &work.tasks {
let mut m = self.get(&t.id)?;
match who.clone() {
// Seen by this agent; the task stays new to the others.
Some(a) => {
if add_seen_by(&mut m, &a) {
self.put(m)?;
}
}
None => {
m.metadata.insert(
MNEMO_META_DELIVERY.into(),
Value::String(crate::memory::delivery::SEEN.into()),
);
self.put(m)?;
}
}
}
// READY-NOTICE: shown to its creator as waiting for its `done`.
if let Some(a) = &who {
for t in &work.ready_for_done {
let mut m = self.get(&t.id)?;
let mut v = ready_seen_by(&m);
if v.iter().any(|s| s == a) {
continue;
}
v.push(a.clone());
m.metadata.insert(
crate::memory::MNEMO_META_READY_SEEN_BY.into(),
Value::Array(v.into_iter().map(Value::String).collect()),
);
self.put(m)?;
}
}
for c in &work.lapsing {
if let Some(mut m) = self.find_claim(&c.topic)? {
m.metadata
.insert(MNEMO_META_WARNED_EXPIRY.into(), Value::from(c.expires_at));
self.put(m)?;
}
}
Ok(())
}
/// 13a (13.1b): whether wake-ups are paused (the kill switch): who
/// paused them and when.
pub fn wake_paused(&mut self) -> Result<Option<(String, i64)>> {
let Some(m) = self
.about_in(crate::memory::VisibilitySet::ALL)?
.into_iter()
.find(is_manifest)
else {
return Ok(None);
};
Ok(m.metadata.get(MNEMO_META_WAKE_PAUSED).map(|v| {
(
v.get("by")
.and_then(Value::as_str)
.unwrap_or("")
.to_string(),
v.get("at").and_then(Value::as_i64).unwrap_or(0),
)
}))
}
/// 13a (13.1b): pause (`true`) or resume wake-ups for every session
/// of this brain, on the manifest. Paused, nothing wakes anyone: items
/// stay `sent`, and the per-turn hook and the brief still show them.
pub fn set_wake_paused(&mut self, paused: bool) -> Result<WriteOutcome> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let Some(mut m) = self
.about_in(crate::memory::VisibilitySet::ALL)?
.into_iter()
.find(is_manifest)
else {
return Err(MnemoError::Invalid(
"this brain has no manifest to record the pause on".into(),
));
};
if m.metadata.contains_key(MNEMO_META_WAKE_PAUSED) == paused {
return Ok(WriteOutcome::Unchanged);
}
if paused {
let mut v = Map::new();
v.insert(
"by".into(),
Value::String(self.author.clone().unwrap_or_else(|| "anonymous".into())),
);
v.insert("at".into(), Value::from(self.coordination_now()));
m.metadata
.insert(MNEMO_META_WAKE_PAUSED.into(), Value::Object(v));
} else {
m.metadata.remove(MNEMO_META_WAKE_PAUSED);
}
self.put(m)?;
Ok(WriteOutcome::Updated)
}
/// 13a (13.1b): the wake-up guardrails, in the one place that marks an
/// item `delivered`. For `agent` (and `role`), the items that may wake
/// it: pre-approved tasks ready for its role, and messages addressed to
/// it by id (never to its role or `all`), still `sent`. Then:
/// - **paused** (the kill switch): [`Wake::Held`];
/// - **an explicit reply past the hop cap** ([`crate::memory::HOP_CAP`])
/// or **a pair in a burst** ([`crate::memory::PAIR_BURST`] messages
/// either way in [`crate::memory::PAIR_BURST_SECS`]) wakes nobody;
/// - **the recipient's rate** ([`crate::memory::WAKE_PER_HOUR`]) and
/// **cooldown** ([`crate::memory::WAKE_COOLDOWN_SECS`]), from its
/// presence record's `_mnemo.wakes`.
///
/// On [`Wake::Now`] the items are marked `delivered` and the wake-up is
/// recorded on the recipient's presence record, in place.
pub fn wake(&mut self, agent: &str, role: Option<&str>) -> Result<Wake> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let now = self.coordination_now();
let sent = crate::memory::delivery::SENT;
let mut tasks = Vec::new();
for t in self.ready_tasks(role)? {
let m = self.get(&t.id)?;
if t.pre_approved
&& meta_string(&m, MNEMO_META_DELIVERY).as_deref() == Some(sent)
&& !seen_by(&m).iter().any(|s| s == agent)
{
tasks.push(t.id);
}
}
let all_msgs: Vec<Message> = self
.messages()?
.iter()
.filter_map(Message::from_memory)
.collect();
let candidates: Vec<&Message> = all_msgs
.iter()
.filter(|m| m.to == agent && m.from != agent)
.filter(|m| m.status == crate::memory::MESSAGE_UNREAD && m.delivery == sent)
.collect();
if tasks.is_empty() && candidates.is_empty() {
return Ok(Wake::Nothing);
}
if self.wake_paused()?.is_some() {
return Ok(Wake::Held("paused"));
}
let burst = |a: &str, b: &str| {
all_msgs
.iter()
.filter(|m| now - m.created_at <= PAIR_BURST_SECS)
.filter(|m| (m.from == a && m.to == b) || (m.from == b && m.to == a))
.count()
>= PAIR_BURST
};
let mut reason = None;
let messages: Vec<Ulid> = candidates
.iter()
.filter(|m| {
let reply = m.conversation != m.id.to_string();
if reply && m.hops > HOP_CAP {
reason = Some("the thread reached its hop cap");
return false;
}
if burst(&m.from, &m.to) {
reason = Some("a burst of messages between two agents");
return false;
}
true
})
.map(|m| m.id)
.collect();
if tasks.is_empty() && messages.is_empty() {
return Ok(Wake::Held(reason.unwrap_or("a guardrail")));
}
// The recipient's rate and cooldown, from its presence record.
let presence = self
.coordination_records(MNEMO_META_KIND_PRESENCE, Some(agent))?
.into_iter()
.next();
let mut wakes: Vec<i64> = presence
.as_ref()
.and_then(|m| m.metadata.get(MNEMO_META_WAKES))
.and_then(Value::as_array)
.map(|a| a.iter().filter_map(Value::as_i64).collect())
.unwrap_or_default();
wakes.retain(|t| now - t < 3600 && *t <= now);
if wakes.len() >= WAKE_PER_HOUR {
return Ok(Wake::Held("the hourly limit of wake-ups"));
}
if wakes.iter().any(|t| now - t < WAKE_COOLDOWN_SECS) {
return Ok(Wake::Held("the cooldown between wake-ups"));
}
wakes.push(now);
let delivered = Value::String(crate::memory::delivery::DELIVERED.into());
for id in tasks.iter().chain(messages.iter()) {
let mut m = self.get(id)?;
m.metadata
.insert(MNEMO_META_DELIVERY.into(), delivered.clone());
self.put(m)?;
}
let w = Value::Array(wakes.into_iter().map(Value::from).collect());
match presence {
Some(mut m) => {
m.metadata.insert(MNEMO_META_WAKES.into(), w);
self.put(m)?;
}
None => {
let mut meta = Map::new();
meta.insert(MNEMO_META_AGENT.into(), Value::String(agent.to_string()));
meta.insert(MNEMO_META_LAST_ACTIVE.into(), Value::from(0));
meta.insert(MNEMO_META_PRESENCE_LOCK.into(), Value::Bool(false));
meta.insert(MNEMO_META_WAKES.into(), w);
self.put_coordination_with_id(
Ulid::new(),
MNEMO_META_KIND_PRESENCE,
format!("presence {agent}"),
meta,
)?;
}
}
Ok(Wake::Now {
tasks: tasks.len(),
messages: messages.len(),
})
}
}
// --- 13a step 7: freshness (13.6) --------------------------------------------
/// 13a (13.6): one stale record ([`Mnemo::stale`]).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Stale {
/// The record's id.
pub id: Ulid,
/// What it is: `essence state`, `essence next`, `essence open`, `task
/// active`, `task blocked`, `message unread`, or `fact` (past its
/// `review_by`).
pub what: String,
/// When it went stale, in unix seconds.
pub since: i64,
/// Its first line (a task's title, a message's subject).
pub text: String,
/// Whose it is: a task's owner, a message's recipient, else its author.
pub whose: Option<String>,
/// Its visibility level (`public`, `team` or `private`).
pub visibility: String,
}
impl Mnemo {
/// 13a (13.6): every record that's stale now
/// ([`crate::memory::stale_since`]), oldest first: past its `review_by`,
/// or older than its kind's default (essence `state` and `next` 14
/// days, `open` 30, an `active` task with no update 3, a `blocked` one
/// 7, an unread message 3). Reads only the rows that can be (essence,
/// coordination, and those with a `review_by`).
pub fn stale(&mut self) -> Result<Vec<Stale>> {
use super::records::{ROW_KIND_ESSENCE, ROW_REVIEW};
let now = self.coordination_now();
let entries: Vec<CatalogEntry> = self
.catalog
.iter()
.filter(|e| !e.deleted)
.filter(|e| {
e.v9.is_none_or(|r| {
r.kind == ROW_KIND_ESSENCE
|| r.kind == ROW_KIND_COORDINATION
|| r.flags & ROW_REVIEW != 0
})
})
.cloned()
.collect();
let mut out = Vec::new();
for e in entries {
let m = self.read_memory(&e)?;
let Some(since) = crate::memory::stale_since(&m, now) else {
continue;
};
let s = |k: &str| meta_string(&m, k);
let kind = s(MNEMO_META_KIND);
let what = match kind.as_deref() {
Some("essence") => format!(
"essence {}",
s(crate::memory::MNEMO_META_SECTION).unwrap_or_default()
),
Some(k @ ("task" | "message")) => {
format!("{k} {}", s(MNEMO_META_STATUS).unwrap_or_default())
}
Some(k) => k.to_string(),
None => "fact".to_string(),
};
let whose = match kind.as_deref() {
Some("task") => s(MNEMO_META_OWNER),
Some("message") => s(MNEMO_META_TO),
_ => s(crate::memory::MNEMO_META_AUTHOR),
};
out.push(Stale {
id: m.id,
what,
since,
text: m.content.lines().next().unwrap_or("").to_string(),
whose,
visibility: crate::memory::visibility_of(&m).to_string(),
});
}
out.sort_by_key(|x| (x.since, x.id.0));
Ok(out)
}
}
/// 13a (ABOUT-COUNT): the live records by what they are.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct KindCounts {
/// Facts and onboarding records (what recall and `about` read).
pub facts: usize,
/// Thread turns and thread links.
pub turns: usize,
/// Essence items.
pub essence: usize,
/// Messages, tasks, claims and presence records.
pub coordination: usize,
}
impl Mnemo {
/// 13a (ABOUT-COUNT): how many live records of each kind, from the
/// catalog's rows (a v8 file's records have none and count as facts).
/// Superseded versions count too, as in [`Mnemo::len`].
pub fn kind_counts(&self) -> KindCounts {
use super::records::{ROW_KIND_ESSENCE, ROW_KIND_THREAD_LINK, ROW_KIND_TURN};
let mut c = KindCounts::default();
for e in self.catalog.iter().filter(|e| !e.deleted) {
match e.v9.map(|r| r.kind) {
Some(ROW_KIND_TURN | ROW_KIND_THREAD_LINK) => c.turns += 1,
Some(ROW_KIND_ESSENCE) => c.essence += 1,
Some(ROW_KIND_COORDINATION) => c.coordination += 1,
_ => c.facts += 1,
}
}
c
}
}
#[cfg(test)]
mod legacy_scan_tests {
use super::is_sentinel;
/// COORD-LEGACY-SCAN: the sentinel, quantised as the column stores it,
/// is recognised at every width; an ordinary vector isn't.
#[test]
fn the_sentinel_is_told_from_an_ordinary_vector() {
for dims in [4, 32, 384, 768] {
let s = vec![1.0 / (dims as f32).sqrt(); dims];
let (h, _) = crate::veccol::quantize(&s);
assert!(is_sentinel(&h), "{dims}");
let mut v = s.clone();
v[dims / 2] += 0.05;
assert!(!is_sentinel(&crate::veccol::quantize(&v).0), "{dims}");
let flat = vec![0.5; dims];
assert_eq!(
is_sentinel(&crate::veccol::quantize(&flat).0),
dims == 4,
"{dims}"
);
}
assert!(!is_sentinel(&[]));
}
}