Compare commits
681
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34a884ffcc | ||
|
|
b889075e21 | ||
|
|
3fd2e58072 | ||
|
|
dc507ec9ff | ||
|
|
3c402d766c | ||
|
|
7de86ff471 | ||
|
|
e8924b9f5d | ||
|
|
2cd3f20cb2 | ||
|
|
e01c21f19e | ||
|
|
b68de8f186 | ||
|
|
05955b6349 | ||
|
|
6f4e85c656 | ||
|
|
f4cd0e89fd | ||
|
|
0b834bbca7 | ||
|
|
a0e86fc696 | ||
|
|
51a83ed3a8 | ||
|
|
eb4e964fbf | ||
|
|
121125aa50 | ||
|
|
947a68d274 | ||
|
|
0934292f47 | ||
|
|
1b706ca0b1 | ||
|
|
a8a0cfeb8f | ||
|
|
a7459c6e4f | ||
|
|
677c149022 | ||
|
|
144bfd5626 | ||
|
|
1ae83499ba | ||
|
|
f57ae96e55 | ||
|
|
30f274bcb0 | ||
|
|
c52907a346 | ||
|
|
8b91552f59 | ||
|
|
6ecadf294e | ||
|
|
ae8b0c65bc | ||
|
|
1568877f35 | ||
|
|
04eda6130d | ||
|
|
959e8d6d30 | ||
|
|
01543d9121 | ||
|
|
8c8b4367f1 | ||
|
|
c775aab29a | ||
|
|
df79ab8769 | ||
|
|
5b06dd7e43 | ||
|
|
c99ad77633 | ||
|
|
401289afac | ||
|
|
a6a6135f74 | ||
|
|
c91ab3acfa | ||
|
|
ba0330c205 | ||
|
|
15458aeb61 | ||
|
|
815be6bdce | ||
|
|
f85f639afe | ||
|
|
0400d7bbe7 | ||
|
|
3ba309c5e4 | ||
|
|
7f5f285b60 | ||
|
|
f0bf4578a9 | ||
|
|
40dc5fe11d | ||
|
|
82d5688477 | ||
|
|
57984e9b98 | ||
|
|
2f471edc0f | ||
|
|
6cde12bf2f | ||
|
|
e534f667da | ||
|
|
4f10af81b0 | ||
|
|
6d398264db | ||
|
|
e44a2d140d | ||
|
|
f9857ab712 | ||
|
|
0cafb62d0e | ||
|
|
d7dada57aa | ||
|
|
a1bc613702 | ||
|
|
abcbeed115 | ||
|
|
673d1db95a | ||
|
|
c7f54357ad | ||
|
|
a30852939c | ||
|
|
9cfda8d6c7 | ||
|
|
a943b6a7db | ||
|
|
d901c6c19d | ||
|
|
30d1527e6a | ||
|
|
f82338e8ff | ||
|
|
c28fd39d23 | ||
|
|
ac2cd3749c | ||
|
|
4bdcedd0f0 | ||
|
|
f85199f624 | ||
|
|
13d43a7de0 | ||
|
|
5dfb357333 | ||
|
|
83accdbb4e | ||
|
|
7c64c17e65 | ||
|
|
0a446c7572 | ||
|
|
e1521a2ecf | ||
|
|
3a94021766 | ||
|
|
282758456c | ||
|
|
0e9f916806 | ||
|
|
daa5d3ea5f | ||
|
|
be993cb2c5 | ||
|
|
cd3b8fe8e9 | ||
|
|
f0c21ca033 | ||
|
|
c6ec42bc2e | ||
|
|
f65d789052 | ||
|
|
62b8251b99 | ||
|
|
90c88fc7b5 | ||
|
|
740c218139 | ||
|
|
1481465ec7 | ||
|
|
d2b8f64c94 | ||
|
|
eed95cd63b | ||
|
|
84106a9338 | ||
|
|
9ed13cd9dc | ||
|
|
d76c3588e3 | ||
|
|
377d42f5ea | ||
|
|
9ade421e12 | ||
|
|
46f3dc8cd7 | ||
|
|
7da6dd20a1 | ||
|
|
c87cf1ace5 | ||
|
|
3f3f6789dd | ||
|
|
8b5cc8be8d | ||
|
|
7c6aed9d69 | ||
|
|
4f9f307eaf | ||
|
|
e87c6cd3b6 | ||
|
|
73c2286ec9 | ||
|
|
3be2f79921 | ||
|
|
cf5ea429a2 | ||
|
|
1419dcef7d | ||
|
|
8c96a02a01 | ||
|
|
03cc8eac90 | ||
|
|
f31d6c645d | ||
|
|
bbc9eb7c93 | ||
|
|
1310c28102 | ||
|
|
affffa9c23 | ||
|
|
94f3cde557 | ||
|
|
6962da2f98 | ||
|
|
0aee792af6 | ||
|
|
86367f65b2 | ||
|
|
932e5c4e4b | ||
|
|
930c0fb783 | ||
|
|
79bd15884d | ||
|
|
13a0277f8c | ||
|
|
fcdf21d615 | ||
|
|
d6b75616f5 | ||
|
|
096668a14a | ||
|
|
2b1bc1d302 | ||
|
|
a55241c30b | ||
|
|
e1ee303a1c | ||
|
|
c9f8e6e8cd | ||
|
|
0afca54a0f | ||
|
|
8bb73eb61f | ||
|
|
229c887999 | ||
|
|
cdf168706b | ||
|
|
ae78fb039f | ||
|
|
928de07c43 | ||
|
|
666be37916 | ||
|
|
ccab80d726 | ||
|
|
1168376930 | ||
|
|
2679399d60 | ||
|
|
ce30601b0c | ||
|
|
d2c1a24a24 | ||
|
|
88ff34e855 | ||
|
|
76195e421f | ||
|
|
59704456c3 | ||
|
|
4a99f7044e | ||
|
|
7d17441a9a | ||
|
|
c006538064 | ||
|
|
bfc877209f | ||
|
|
99f3e23492 | ||
|
|
e8204a9c27 | ||
|
|
89397466aa | ||
|
|
28f616e379 | ||
|
|
8ea8ef9768 | ||
|
|
fb24d384a2 | ||
|
|
5236c2aa72 | ||
|
|
5df0fb64f5 | ||
|
|
ed536102ac | ||
|
|
a98b6ba77e | ||
|
|
b1125c02cf | ||
|
|
a4bc3d95ed | ||
|
|
6ba73d4e2a | ||
|
|
53351dd30d | ||
|
|
32f1566cc8 | ||
|
|
0041b69d17 | ||
|
|
d1a15ced6c | ||
|
|
afa825f902 | ||
|
|
064ebc9b84 | ||
|
|
8ae4fabfbe | ||
|
|
aa0a472b29 | ||
|
|
9787223509 | ||
|
|
d15502f365 | ||
|
|
58664eee60 | ||
|
|
1ff904f101 | ||
|
|
2d592c63c1 | ||
|
|
87f8c30a09 | ||
|
|
e79392d666 | ||
|
|
5f372ac63a | ||
|
|
008e02afbd | ||
|
|
605c1b1ca0 | ||
|
|
7831f14a6e | ||
|
|
8927d68900 | ||
|
|
ec27f12524 | ||
|
|
c6844fc951 | ||
|
|
9116067e11 | ||
|
|
5ab8bb7e62 | ||
|
|
97bdb7d656 | ||
|
|
c0067e77fc | ||
|
|
8aad282e6c | ||
|
|
dd1a4e083c | ||
|
|
23256413de | ||
|
|
a357a85faa | ||
|
|
316039ca95 | ||
|
|
06d83912cd | ||
|
|
1bea6edcb8 | ||
|
|
2fb1e5c4ea | ||
|
|
a10bf45ed9 | ||
|
|
0fa4b5d8e7 | ||
|
|
3cb2b83a15 | ||
|
|
2b699d4046 | ||
|
|
117f933dac | ||
|
|
bb96fbbb3c | ||
|
|
75d98dd908 | ||
|
|
f6eb5d9efa | ||
|
|
f112011727 | ||
|
|
0aa5d6637c | ||
|
|
e271757db7 | ||
|
|
a6ce8dcbd6 | ||
|
|
c100e16ba0 | ||
|
|
b9abfde492 | ||
|
|
f7616c6a9b | ||
|
|
62c0eb90cb | ||
|
|
5795f4d230 | ||
|
|
a60728e6f0 | ||
|
|
792a79720b | ||
|
|
d638856768 | ||
|
|
d22a7f3a41 | ||
|
|
ec646f55ee | ||
|
|
49ab6c00ea | ||
|
|
45b0779b68 | ||
|
|
0ec8347786 | ||
|
|
52572698cb | ||
|
|
700b37890b | ||
|
|
ea4bb70a21 | ||
|
|
87ca34c92f | ||
|
|
836fcd81f6 | ||
|
|
8484ab0032 | ||
|
|
c29ef0d95e | ||
|
|
c0d666fdcf | ||
|
|
22bbb9c2c0 | ||
|
|
7b50628627 | ||
|
|
23a280a664 | ||
|
|
3bb8ace833 | ||
|
|
ab1eb0cb47 | ||
|
|
2a9827ce18 | ||
|
|
493c59c89b | ||
|
|
1cb1c84cc1 | ||
|
|
fdd33e1234 | ||
|
|
c41387f271 | ||
|
|
e034f73991 | ||
|
|
ee6d7c284d | ||
|
|
622b259145 | ||
|
|
b5c5594ea7 | ||
|
|
64fa2f5abf | ||
|
|
f1c04801e4 | ||
|
|
fff3b819d3 | ||
|
|
dad8672b73 | ||
|
|
b854f884e9 | ||
|
|
a57139dcf5 | ||
|
|
f7e59b876d | ||
|
|
b1da6344a8 | ||
|
|
0f796e6505 | ||
|
|
6d000647de | ||
|
|
cff1005422 | ||
|
|
0e9fb2cbe3 | ||
|
|
478551622d | ||
|
|
742303895b | ||
|
|
ce9f984363 | ||
|
|
c82b40660b | ||
|
|
e436c59430 | ||
|
|
fe56de746e | ||
|
|
8261c2c7e4 | ||
|
|
b276da8ba6 | ||
|
|
f18e24ce19 | ||
|
|
466e1ae320 | ||
|
|
cc21062250 | ||
|
|
f9dcbc2957 | ||
|
|
a196bf6323 | ||
|
|
94a0ebbedf | ||
|
|
e9cdf08841 | ||
|
|
0be81ae036 | ||
|
|
c5f6845386 | ||
|
|
2dc2c7e431 | ||
|
|
9e005317dd | ||
|
|
4226204e57 | ||
|
|
546b674d14 | ||
|
|
4caea7e624 | ||
|
|
636cb633ac | ||
|
|
6181d9afb3 | ||
|
|
89ad370485 | ||
|
|
333e3c21a5 | ||
|
|
8291502cbb | ||
|
|
8d2497b65e | ||
|
|
5b77eb18f8 | ||
|
|
aa10f070f2 | ||
|
|
9ee1cbe3f5 | ||
|
|
b52dd7ca44 | ||
|
|
bf60d6a8bb | ||
|
|
584fc94baa | ||
|
|
11f49905a1 | ||
|
|
51df346a23 | ||
|
|
c1008aeedf | ||
|
|
8d73b5ba1b | ||
|
|
48a9402ca0 | ||
|
|
ee56d56922 | ||
|
|
af0d58fc6f | ||
|
|
fea509d3f5 | ||
|
|
ff6a8928d6 | ||
|
|
9fe3f41ac0 | ||
|
|
48ad5ce947 | ||
|
|
f339ea5936 | ||
|
|
64669575c8 | ||
|
|
e4ef67adcd | ||
|
|
e723d498e7 | ||
|
|
a5e8bcca8a | ||
|
|
4f7c0ba534 | ||
|
|
00be4c5063 | ||
|
|
d708998a53 | ||
|
|
2d5358b8b9 | ||
|
|
741239a506 | ||
|
|
c235abcb11 | ||
|
|
dcaa229c2b | ||
|
|
0e3580895d | ||
|
|
8d6b7fbbc9 | ||
|
|
f8160922c9 | ||
|
|
904b8aedd4 | ||
|
|
6277067465 | ||
|
|
177115d6cd | ||
|
|
78b12be752 | ||
|
|
f17001d920 | ||
|
|
24778d24b6 | ||
|
|
247c422fbf | ||
|
|
ea87dd80bb | ||
|
|
0699741fab | ||
|
|
476fc0cc30 | ||
|
|
75514e05e2 | ||
|
|
15db34169e | ||
|
|
500c8c8cec | ||
|
|
6c2d6605a5 | ||
|
|
033ff2540b | ||
|
|
ebcc7761f1 | ||
|
|
bf7d040489 | ||
|
|
80ade3cacb | ||
|
|
3ebcb15a4b | ||
|
|
1ed0dca7df | ||
|
|
810254193c | ||
|
|
ba3fd46e82 | ||
|
|
dafa0691ad | ||
|
|
22a90d4267 | ||
|
|
739c0c23c6 | ||
|
|
a81f5ed0e3 | ||
|
|
c3e75e35e1 | ||
|
|
3a6a127c38 | ||
|
|
737cb95c47 | ||
|
|
d854f936f9 | ||
|
|
9e41f6380a | ||
|
|
4cd0b42d80 | ||
|
|
bd2997cf36 | ||
|
|
6d45f3ef58 | ||
|
|
968a0aac7a | ||
|
|
572a2b3a4b | ||
|
|
2dd02da76e | ||
|
|
0ebabf7174 | ||
|
|
7d755c8f08 | ||
|
|
b43c57f43b | ||
|
|
d9f2b7bcfa | ||
|
|
c6de76740f | ||
|
|
dbcba161d5 | ||
|
|
c8e321c8e1 | ||
|
|
b8e8be619d | ||
|
|
cc8a354f75 | ||
|
|
6a57bb7218 | ||
|
|
5d1456257c | ||
|
|
36d9d73984 | ||
|
|
0acad94ba5 | ||
|
|
056e4ea915 | ||
|
|
9cf5c1ddbd | ||
|
|
3b92e02862 | ||
|
|
a8131809b2 | ||
|
|
b8ce072c9f | ||
|
|
7b16e195ee | ||
|
|
baaf2e56dd | ||
|
|
4b749286bd | ||
|
|
ebaa4c9946 | ||
|
|
231975bce6 | ||
|
|
35fb415f7b | ||
|
|
14d1a919cc | ||
|
|
20e92dacd5 | ||
|
|
9e545658a5 | ||
|
|
4e10ab7b2e | ||
|
|
954e3afe56 | ||
|
|
605c583ab4 | ||
|
|
44cb7ad449 | ||
|
|
6cf98386e9 | ||
|
|
bd65b023bd | ||
|
|
31ce47d1cc | ||
|
|
6eccfc286a | ||
|
|
110c631b5f | ||
|
|
256a412590 | ||
|
|
e0428bd874 | ||
|
|
da5bbe0a17 | ||
|
|
b63e8ae58a | ||
|
|
3991211993 | ||
|
|
9cc62818f4 | ||
|
|
7278e509f4 | ||
|
|
0098960b5a | ||
|
|
c41a84ee95 | ||
|
|
ff07c3a668 | ||
|
|
7df879b367 | ||
|
|
095e23ecda | ||
|
|
2b604e0dad | ||
|
|
9770dab09e | ||
|
|
15094d6e72 | ||
|
|
5ca3876bea | ||
|
|
f29a9ad563 | ||
|
|
4179be7a56 | ||
|
|
e85cf002a4 | ||
|
|
8f3c810fc3 | ||
|
|
c854373840 | ||
|
|
49bace0073 | ||
|
|
1c03299721 | ||
|
|
e4c5108306 | ||
|
|
e995e40519 | ||
|
|
a9dfe77b1d | ||
|
|
caa350f350 | ||
|
|
673b9d075a | ||
|
|
47aaee122c | ||
|
|
22b50c88a9 | ||
|
|
265d3b83b2 | ||
|
|
72ccb75c1b | ||
|
|
790b5aac7d | ||
|
|
b15c511528 | ||
|
|
d4e29a07b2 | ||
|
|
d627400cf1 | ||
|
|
41567adee0 | ||
|
|
f5ce980808 | ||
|
|
0bd978f4ee | ||
|
|
af978cc559 | ||
|
|
6e343831c9 | ||
|
|
5b4b201558 | ||
|
|
f509c61b50 | ||
|
|
469763547c | ||
|
|
9aae6244d5 | ||
|
|
6a612c58cb | ||
|
|
d03ffa8038 | ||
|
|
f06dd01bfe | ||
|
|
c81915b039 | ||
|
|
839147b146 | ||
|
|
14699713b9 | ||
|
|
652493e2ac | ||
|
|
fc83f204f4 | ||
|
|
3eee45aa49 | ||
|
|
9f4d4eecba | ||
|
|
f0c7d468c3 | ||
|
|
18fd2a32bf | ||
|
|
7cc4a33cc5 | ||
|
|
8255640d00 | ||
|
|
3215e5eeb9 | ||
|
|
1cc67aa9d7 | ||
|
|
06ebd7e8cf | ||
|
|
cd24677756 | ||
|
|
05fcebf9c5 | ||
|
|
95f29d6c54 | ||
|
|
a587125047 | ||
|
|
ab5423f87f | ||
|
|
cbd4da3a11 | ||
|
|
b78e90c0c7 | ||
|
|
11d1be3ef8 | ||
|
|
9677f200f3 | ||
|
|
dd4449de61 | ||
|
|
846291069b | ||
|
|
d786477a0d | ||
|
|
b90d3a4556 | ||
|
|
9c1cb63c3b | ||
|
|
fa0f851f81 | ||
|
|
65570636b2 | ||
|
|
1af839fb72 | ||
|
|
ecf597adce | ||
|
|
811b59b454 | ||
|
|
8f4f70f088 | ||
|
|
ef60e5d09e | ||
|
|
26c82a507a | ||
|
|
76be864bff | ||
|
|
9e2311029a | ||
|
|
bea1c446ea | ||
|
|
1e55ea8e4e | ||
|
|
9f62e0a2ba | ||
|
|
579cd33cd5 | ||
|
|
d3286ad0eb | ||
|
|
a7b83b27f0 | ||
|
|
4839884d93 | ||
|
|
6d4ad2c249 | ||
|
|
6f7d070769 | ||
|
|
85023b3b52 | ||
|
|
8484d349aa | ||
|
|
1f1aa70580 | ||
|
|
46c125c07c | ||
|
|
435d1206e4 | ||
|
|
6fe1bba067 | ||
|
|
ed188268a0 | ||
|
|
bf6bb3d094 | ||
|
|
8ee28d08c3 | ||
|
|
6ac5c934d2 | ||
|
|
ee9c389888 | ||
|
|
59f9a00c64 | ||
|
|
badfe0d166 | ||
|
|
64ed69db9b | ||
|
|
c6cf8b64f1 | ||
|
|
32528aa58f | ||
|
|
00b95f1f68 | ||
|
|
a9fa004758 | ||
|
|
eb7a25622d | ||
|
|
8a6827dfe2 | ||
|
|
30a13c4ebb | ||
|
|
11daf2f289 | ||
|
|
3f25e5b4da | ||
|
|
cb5a8dd98e | ||
|
|
d3781fd840 | ||
|
|
cf383af441 | ||
|
|
bb1e18f490 | ||
|
|
663d65640d | ||
|
|
119e0a6f13 | ||
|
|
202c44c6e3 | ||
|
|
7a1e8d1036 | ||
|
|
299f43167c | ||
|
|
713b59633e | ||
|
|
86342c4399 | ||
|
|
bc2e5574ef | ||
|
|
a24f7809a6 | ||
|
|
6062feaa22 | ||
|
|
076f63f932 | ||
|
|
fc90e6c67b | ||
|
|
5c8f3b8351 | ||
|
|
405729127d | ||
|
|
ca02942a29 | ||
|
|
6acc747661 | ||
|
|
b6884796e7 | ||
|
|
fcff8cf35e | ||
|
|
a96fd44abb | ||
|
|
c2e017a4c1 | ||
|
|
bbd9f638a0 | ||
|
|
d64e9f3818 | ||
|
|
b9a224dc99 | ||
|
|
b8c3f5f09a | ||
|
|
e6aab55008 | ||
|
|
312d824e4f | ||
|
|
5daf3a333a | ||
|
|
a00429bc80 | ||
|
|
975f1aa8e8 | ||
|
|
0bf636826a | ||
|
|
9dfb2eae0e | ||
|
|
b471b5cf9e | ||
|
|
32655b95f6 | ||
|
|
b5aebbbe91 | ||
|
|
4ad5f40817 | ||
|
|
b7dab47e88 | ||
|
|
dfb682fa33 | ||
|
|
357967308f | ||
|
|
25bbd55539 | ||
|
|
cfad86394f | ||
|
|
c62650e4ff | ||
|
|
fe8c41928d | ||
|
|
50677b204b | ||
|
|
ae056a44dd | ||
|
|
ea74be9e2e | ||
|
|
19994c1817 | ||
|
|
3c0b4d29a8 | ||
|
|
7cc81512a3 | ||
|
|
462db1c257 | ||
|
|
4a3914ee1c | ||
|
|
ce3284fc8f | ||
|
|
ad6e813fb0 | ||
|
|
b373e34b29 | ||
|
|
067b9a47da | ||
|
|
bfa4a0851a | ||
|
|
b7f78d47c5 | ||
|
|
c4463ba47f | ||
|
|
c39503e322 | ||
|
|
6c61d9facf | ||
|
|
a39b72c377 | ||
|
|
e712cd227f | ||
|
|
12059ad3cc | ||
|
|
6a29e41175 | ||
|
|
1948da9cb4 | ||
|
|
b84a2f4dc1 | ||
|
|
9c42423d58 | ||
|
|
c12f1a16db | ||
|
|
09ea4a40bf | ||
|
|
6b8e7fc621 | ||
|
|
3c3fdd3446 | ||
|
|
06f28fbadb | ||
|
|
62644aa012 | ||
|
|
bdfd8db265 | ||
|
|
89ec9429db | ||
|
|
b9cfa4fe16 | ||
|
|
8b184d8322 | ||
|
|
02d57c4a53 | ||
|
|
5a7561fe1d | ||
|
|
5e537f851f | ||
|
|
0a6a5aba68 | ||
|
|
407fc6c9e6 | ||
|
|
6299b47f4f | ||
|
|
24bf59219e | ||
|
|
e3b4ecde8c | ||
|
|
fd9147087e | ||
|
|
a3d141e793 | ||
|
|
4fbe6e347f | ||
|
|
3d70bd3660 | ||
|
|
a4e5a8ffef | ||
|
|
13dea6b1aa | ||
|
|
6d2ce96630 | ||
|
|
a7dc4534d7 | ||
|
|
4387e77f28 | ||
|
|
443a340f06 | ||
|
|
15f81780b9 | ||
|
|
ea4dfaa5a2 | ||
|
|
7630e3cc9d | ||
|
|
994b25f540 | ||
|
|
503411e628 | ||
|
|
4e2a1a5a1f | ||
|
|
6a24e4d0c7 | ||
|
|
80186d0798 | ||
|
|
89cd8a2e8e | ||
|
|
4658e1cb1c | ||
|
|
6e6b325cf1 | ||
|
|
bbe107fa2a | ||
|
|
ac94f8338f | ||
|
|
a0459a8553 | ||
|
|
6cf7f67a1d | ||
|
|
08fbc51af2 | ||
|
|
1ef1b13560 | ||
|
|
7fd4ed5898 | ||
|
|
bc8cb3b153 | ||
|
|
9480557537 | ||
|
|
c88be88ccf | ||
|
|
53d173aa7d | ||
|
|
4becc7bd83 | ||
|
|
61737962fc | ||
|
|
b4192c525e | ||
|
|
d03b455ea6 | ||
|
|
1171b684d0 | ||
|
|
6547933b4d | ||
|
|
0cd706b044 | ||
|
|
908f3918de | ||
|
|
8b84ac7487 | ||
|
|
9698bcd934 | ||
|
|
21aa11aef1 | ||
|
|
145cf7e33a | ||
|
|
9a3356bd38 | ||
|
|
83732dcd53 | ||
|
|
adcddb40cc | ||
|
|
0c465fb949 | ||
|
|
bac7f503f0 | ||
|
|
80b8002a94 | ||
|
|
1c1b03c3a0 | ||
|
|
67d9ccb1d4 | ||
|
|
0f52013878 | ||
|
|
36c19694c9 | ||
|
|
77b30bd296 | ||
|
|
81287cb11d | ||
|
|
962e8547b1 | ||
|
|
ea25e4d589 | ||
|
|
422b22ab0e | ||
|
|
91edd0bfdb | ||
|
|
81874e4748 | ||
|
|
62fe1cc36d | ||
|
|
7bf15a3dc4 | ||
|
|
ba17347518 | ||
|
|
8c67c224c2 | ||
|
|
f2b332de73 | ||
|
|
980ade9338 | ||
|
|
7c0ce2fe21 | ||
|
|
be7d649d1f | ||
|
|
c8bcb2cdf5 | ||
|
|
608c4d4be6 | ||
|
|
da1356cd7c | ||
|
|
fa73674e22 | ||
|
|
1da83c29dd | ||
|
|
428ef8bbe5 | ||
|
|
0220dfa92b | ||
|
|
b6d85847f8 | ||
|
|
1d07778c88 | ||
|
|
ddb2ae9420 | ||
|
|
332d373114 |
+5
-4
@@ -20,6 +20,7 @@ dist/
|
||||
/checks/predefined/predefined
|
||||
/checks/readable/readable
|
||||
/checks/removedpackages/removedpackages
|
||||
/checks/requiredmodules/requiredmodules
|
||||
/checks/secrets/secrets
|
||||
/checks/sshdir/sshdir
|
||||
/checks/templates/templates
|
||||
@@ -42,7 +43,10 @@ dist/
|
||||
/modules/claude-code/claude-code
|
||||
/modules/claude-code/cmd/claude-code/claude-code
|
||||
/modules/claude-licence-manager/claude-licence-manager
|
||||
/modules/claude-licence-manager/cmd/claude-licence-manager/claude-licence-manager
|
||||
/modules/claude-licence-manager/cmd/manager/manager
|
||||
/modules/claude-licence-manager/cmd/tools/tools
|
||||
/modules/claude-licence-manager/manager
|
||||
/modules/claude-licence-manager/tools
|
||||
/modules/clipmenu/clipmenu-tools
|
||||
/modules/clipmenu/cmd/clipmenu-tools/clipmenu-tools
|
||||
/modules/cups/cmd/cups-tools/cups-tools
|
||||
@@ -111,9 +115,6 @@ dist/
|
||||
/modules/mailu/cmd/automx/automx
|
||||
/modules/memory-pressure/cmd/memory-pressure/memory-pressure
|
||||
/modules/memory-pressure/memory-pressure
|
||||
/modules/mesh-build-go/buildstep/cmd/mesh-pack/mesh-pack
|
||||
/modules/mesh-build-go/buildstep/cmd/seatgen/seatgen
|
||||
/modules/mesh-build-go/buildstep/mesh-pack
|
||||
/modules/mesh-build-go/cmd/mesh-build-go/mesh-build-go
|
||||
/modules/mesh-build-go/mesh-build-go
|
||||
/modules/mesh-build-image/cmd/mesh-build-image/mesh-build-image
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
// passing line in its place). A build module with no test step (mesh-build-image builds images and runs no tests) is
|
||||
// exempt from that, and said so. Every build module, test step or not, must also hold a test that calls
|
||||
// conformance.LeftRunning with its own start sweep, so a step a previous process left running is ended at its start
|
||||
// (issue 1890).
|
||||
// (issue 1890), and one that calls conformance.Passing, so a step whose network did not answer is said as a transport fault
|
||||
// (issue 1748), and one that calls conformance.StepEnds, so its steps can say a pipeline step's end (design 47 G §24). Its manifest asks the capability seat-settle-delay (ADR 0664 §5).
|
||||
package failurekit
|
||||
|
||||
import (
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
@@ -27,9 +29,15 @@ type manifest struct {
|
||||
Claims []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"claims"`
|
||||
Tools []json.RawMessage `json:"tools"`
|
||||
Tools []json.RawMessage `json:"tools"`
|
||||
Capabilities []string `json:"capabilities"`
|
||||
}
|
||||
|
||||
// settleDelay is the capability a tool runner states when it can settle a seat's ask with a delayed negative acknowledgement
|
||||
// (mesh-sdk CapSeatSettleDelay): a build module gives its ask again after a delay by it, so the delivery plan places a tool
|
||||
// runner that states it before the module (ADR 0664 §1, §5).
|
||||
const settleDelay = "seat-settle-delay"
|
||||
|
||||
// Judge reads every modules/<m>/module.json of the catalogue at root and judges each module that claims a node-build-*
|
||||
// seat.
|
||||
func Judge(root string) ([]Judged, error) {
|
||||
@@ -83,6 +91,41 @@ func Judge(root string) ([]Judged, error) {
|
||||
refused = append(refused, fmt.Sprintf("it claims %s, and no test of it calls conformance.LeftRunning "+
|
||||
"(mesh-build-go buildstep/conformance): run the kit's left-running case with the module's own start sweep (issue 1890)", seat))
|
||||
}
|
||||
// A-489b: every build module is asked pipeline steps, so every one runs the case that its steps can say a step's end: without a
|
||||
// SayStep every step ask is refused live ("this build module says no step's end"), as mesh-build-node's was.
|
||||
ends, err := runsTheKit(dir, "conformance.StepEnds(")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
counts, err := runsTheKit(dir, "conformance.RunsCounted(")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !counts {
|
||||
// A-489c
|
||||
refused = append(refused, fmt.Sprintf("it claims %s, and no test of it calls conformance.RunsCounted "+
|
||||
"(mesh-delivery-sdk steprun/conformance): run the kit's case that its puller counts the runs that started, not the bus's deliveries (A-489a)", seat))
|
||||
}
|
||||
if !ends {
|
||||
refused = append(refused, fmt.Sprintf("it claims %s, and no test of it calls conformance.StepEnds "+
|
||||
"(mesh-delivery-sdk steprun/conformance): run the kit's step-ends case with the module's own steps (design 47 G §24)", seat))
|
||||
}
|
||||
// Every build module meets the network (a fetch, a download, an install, a publish), so every one runs the passing case: its
|
||||
// steps say a network that did not answer as a transport fault the bus gives the ask again for (ADR 0664, issue 1748).
|
||||
says, err := runsTheKit(dir, "conformance.Passing(")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !says {
|
||||
refused = append(refused, fmt.Sprintf("it claims %s, and no test of it calls conformance.Passing "+
|
||||
"(mesh-build-go buildstep/conformance): run the kit's passing case with the module's own path from a failed run to its step's error (issue 1748)", seat))
|
||||
}
|
||||
// Every build module gives a step that met a transport fault again by the bus (ADR 0664 §5): it asks the tool runner's
|
||||
// capability in its manifest, so the delivery plan places a tool runner that states it first.
|
||||
if !slices.Contains(m.Capabilities, settleDelay) {
|
||||
refused = append(refused, fmt.Sprintf("it claims %s, and its manifest's capabilities do not ask %s: a step given again by the bus needs "+
|
||||
"a tool runner that can settle with a delay (ADR 0664 §5, issue 1748)", seat, settleDelay))
|
||||
}
|
||||
j.Refused = strings.Join(refused, "; ")
|
||||
out = append(out, j)
|
||||
}
|
||||
@@ -121,7 +164,7 @@ func runsTheKit(dir, call string) (bool, error) {
|
||||
return err
|
||||
}
|
||||
found = strings.Contains(string(raw), call) &&
|
||||
strings.Contains(string(raw), `"git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep/conformance"`)
|
||||
strings.Contains(string(raw), `"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun/conformance"`)
|
||||
return nil
|
||||
})
|
||||
return found, err
|
||||
|
||||
@@ -8,23 +8,32 @@ import (
|
||||
|
||||
// 1901/AC7 and 1890/AC8: a build module with a test step and no kit is refused, one that runs the kit passes, one with no
|
||||
// test step is exempt from the failure cases but not from the left-running case, a module that runs only the failure
|
||||
// cases is refused, and a module that claims no node-build-* seat is not judged.
|
||||
// cases is refused, a module that does not ask the capability seat-settle-delay is refused (ADR 0664 §5), and a module that
|
||||
// claims no node-build-* seat is not judged.
|
||||
func TestABuildModuleWithATestStepMustRunTheKit(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
const imp = "package main\n\nimport \"git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep/conformance\"\n\n"
|
||||
const sweep = "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\n"
|
||||
const imp = "package main\n\nimport \"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun/conformance\"\n\n"
|
||||
const sweep = "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\nfunc TestPassing(t *testing.T) { conformance.Passing(t, nil, nil) }\nfunc TestEnds(t *testing.T) { conformance.StepEnds(t, steprun.AskSteps{}) }\nfunc TestCounted(t *testing.T) { conformance.RunsCounted(t, nil) }\n"
|
||||
kit := imp + "func TestKit(t *testing.T) { conformance.Run(t, conformance.GoTest, nil) }\n" + sweep
|
||||
for path, body := range map[string]string{
|
||||
"modules/a/module.json": `{"claims":[{"name":"node-build-a"}],"tools":["build_a_fetch","build_a_test"]}`,
|
||||
"modules/a/cmd/a/check_test.go": "package main\n",
|
||||
"modules/b/module.json": `{"claims":[{"name":"node-build-b"}],"tools":[{"name":"build_b_test"}]}`,
|
||||
"modules/b/module.json": `{"claims":[{"name":"node-build-b"}],"capabilities":["seat-settle-delay"],"tools":[{"name":"build_b_test"}]}`,
|
||||
"modules/b/cmd/b/kit_test.go": kit,
|
||||
"modules/c/module.json": `{"claims":[{"name":"node-build-c"}],"tools":["build_c_build"]}`,
|
||||
"modules/c/module.json": `{"claims":[{"name":"node-build-c"}],"capabilities":["seat-settle-delay"],"tools":["build_c_build"]}`,
|
||||
"modules/c/cmd/c/kit_test.go": imp + sweep,
|
||||
"modules/f/module.json": `{"claims":[{"name":"node-build-f"}],"tools":["build_f_build"]}`,
|
||||
"modules/g/module.json": `{"claims":[{"name":"node-build-g"}],"tools":["build_g_test"]}`,
|
||||
"modules/g/cmd/g/kit_test.go": imp + "func TestKit(t *testing.T) { conformance.Run(t, conformance.GoTest, nil) }\n",
|
||||
"modules/d/module.json": `{"claims":[{"name":"other"}],"tools":["d_test"]}`,
|
||||
"modules/h/module.json": `{"claims":[{"name":"node-build-h"}],"tools":["build_h_build"]}`, // the sweep, and no passing case
|
||||
"modules/h/cmd/h/kit_test.go": imp + "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\n",
|
||||
"modules/i/module.json": `{"claims":[{"name":"node-build-i"}],"capabilities":["other"],"tools":["build_i_build"]}`, // the whole kit, and no seat-settle-delay
|
||||
"modules/i/cmd/i/kit_test.go": imp + sweep,
|
||||
"modules/j/module.json": `{"claims":[{"name":"node-build-j"}],"capabilities":["seat-settle-delay"],"tools":["build_j_build"]}`, // the rest of the kit, and no step-ends case
|
||||
"modules/j/cmd/j/kit_test.go": imp + "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\nfunc TestPassing(t *testing.T) { conformance.Passing(t, nil, nil) }\nfunc TestCounted(t *testing.T) { conformance.RunsCounted(t, nil) }\n",
|
||||
"modules/k/module.json": `{"claims":[{"name":"node-build-k"}],"capabilities":["seat-settle-delay"],"tools":["build_k_build"]}`, // no runs-counted case
|
||||
"modules/k/cmd/k/kit_test.go": imp + "func TestSweep(t *testing.T) { conformance.LeftRunning(t, nil) }\nfunc TestPassing(t *testing.T) { conformance.Passing(t, nil, nil) }\nfunc TestEnds(t *testing.T) { conformance.StepEnds(t, steprun.AskSteps{}) }\n",
|
||||
"modules/e/module.json": `{"claims":[{"name":"node-build-e"}],"tools":["build_e_test"]}`,
|
||||
"modules/e/cmd/e/kit.go": kit, // not a test file
|
||||
} {
|
||||
@@ -40,9 +49,9 @@ func TestABuildModuleWithATestStepMustRunTheKit(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := map[string]string{"a": "refused", "b": "passes", "c": "exempt", "e": "refused", "f": "refused", "g": "refused"}
|
||||
want := map[string]string{"a": "refused", "b": "passes", "c": "exempt", "e": "refused", "f": "refused", "g": "refused", "h": "refused", "i": "refused", "j": "refused", "k": "refused"}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("judged %+v, want a, b, c, e, f and g", got)
|
||||
t.Fatalf("judged %+v, want a, b, c, e, f, g, h, i, j and k", got)
|
||||
}
|
||||
for _, j := range got {
|
||||
is := "passes"
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
// go run . <catalogue root>
|
||||
//
|
||||
// Exits 1 on a finding. The packages that count are those the build module counts (mesh-build-node fetch.go
|
||||
// ownDependencies): the `dependencies` beyond the SDK, typescript and the @types packages.
|
||||
// ownDependencies): the `dependencies` beyond the mesh's own packages (@novox/mesh-*, the SDK among them), typescript and the
|
||||
// @types packages.
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -17,6 +18,10 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// meshPackages is the prefix of every package the mesh itself publishes: the build module's toolchain and the mesh's registry
|
||||
// give them, as they give the SDK, so a module naming only them needs no lock (design 47 G A-56).
|
||||
const meshPackages = "@novox/mesh-"
|
||||
|
||||
// Finding is one module refused: it names Packages beyond the SDK and has no lock file.
|
||||
type Finding struct {
|
||||
Module string
|
||||
@@ -70,7 +75,7 @@ func Check(root string) ([]Finding, error) {
|
||||
}
|
||||
var names []string
|
||||
for n := range pkg.Dependencies {
|
||||
if n != "@novox/mesh-sdk" && n != "typescript" && !strings.HasPrefix(n, "@types/") {
|
||||
if !strings.HasPrefix(n, meshPackages) && n != "typescript" && !strings.HasPrefix(n, "@types/") {
|
||||
names = append(names, n)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,6 +35,8 @@ func TestAModuleNamingOnlyTheSDKNeedsNoLock(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
module(t, root, "plain", "typescript", sdkOnly, false)
|
||||
module(t, root, "none", "typescript", "", false)
|
||||
// The mesh's own packages count as the SDK does (design 47 G A-56): a contracts package needs no lock.
|
||||
module(t, root, "contracts", "typescript", `{"dependencies":{"@novox/mesh-sdk":"^0.1.1","@novox/mesh-delivery-contracts-git":"^0.1.0"}}`, false)
|
||||
got, err := Check(root)
|
||||
if err != nil || len(got) != 0 {
|
||||
t.Fatalf("%v %v", got, err)
|
||||
|
||||
@@ -11,4 +11,4 @@ replace git.novox.be/novox/mesh-catalog/modules/disk-load/diskreadings => ../../
|
||||
|
||||
replace git.novox.be/novox/mesh-catalog/modules/restic/backupgood => ../../modules/restic/backupgood
|
||||
|
||||
require git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000 // indirect
|
||||
require git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000
|
||||
|
||||
+44
-23
@@ -31,6 +31,7 @@ import (
|
||||
|
||||
"git.novox.be/novox/mesh-catalog/modules/disk-load/diskreadings"
|
||||
"git.novox.be/novox/mesh-catalog/modules/restic/backupgood"
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
)
|
||||
|
||||
// Known are the constants of the owners' published packages an announcement may name a state by, by the selector the
|
||||
@@ -53,6 +54,7 @@ func Findings(root string) ([]string, error) {
|
||||
}
|
||||
dirs := map[string]string{}
|
||||
reads := map[string][]string{}
|
||||
holders := map[string][]string{} // seat -> the modules of the catalogue that claim it
|
||||
for _, path := range manifests {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
@@ -61,11 +63,17 @@ func Findings(root string) ([]string, error) {
|
||||
var m struct {
|
||||
Module string `json:"module"`
|
||||
Reads []json.RawMessage `json:"reads"`
|
||||
Claims []struct {
|
||||
Name string `json:"name"`
|
||||
} `json:"claims"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
dirs[m.Module] = filepath.Dir(path)
|
||||
for _, c := range m.Claims {
|
||||
holders[c.Name] = append(holders[c.Name], m.Module)
|
||||
}
|
||||
for _, r := range m.Reads {
|
||||
var s string
|
||||
if json.Unmarshal(r, &s) == nil {
|
||||
@@ -77,32 +85,45 @@ func Findings(root string) ([]string, error) {
|
||||
owners := map[string]Announced{}
|
||||
for reader, rs := range reads {
|
||||
for _, r := range rs {
|
||||
owner, state, ok := strings.Cut(r, ".")
|
||||
dir, inCatalogue := dirs[owner]
|
||||
if !ok || !inCatalogue || owner == reader {
|
||||
continue
|
||||
}
|
||||
a, seen := owners[owner]
|
||||
if !seen {
|
||||
var problems []string
|
||||
a, problems, err = AnnouncedBy(dir)
|
||||
// A read in the seat form `seat:<seat>.<state>` (design 47 G A-116, A-128) is of the holder of the seat, found where it
|
||||
// is read: each module of the catalogue claiming the seat is judged for the state, as a read of it by name is.
|
||||
var targets []string
|
||||
state := ""
|
||||
if seat, local, isSeat, err := catalogue.SeatRead(r); isSeat {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
continue // the manifest validator names a malformed seat read
|
||||
}
|
||||
owners[owner] = a
|
||||
for _, p := range problems {
|
||||
out = append(out, owner+": "+p)
|
||||
targets, state = holders[seat], local
|
||||
} else if owner, local, ok := strings.Cut(r, "."); ok {
|
||||
targets, state = []string{owner}, local
|
||||
}
|
||||
for _, owner := range targets {
|
||||
dir, inCatalogue := dirs[owner]
|
||||
if !inCatalogue || owner == reader {
|
||||
continue
|
||||
}
|
||||
a, seen := owners[owner]
|
||||
if !seen {
|
||||
var problems []string
|
||||
a, problems, err = AnnouncedBy(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
owners[owner] = a
|
||||
for _, p := range problems {
|
||||
out = append(out, owner+": "+p)
|
||||
}
|
||||
}
|
||||
if a == nil {
|
||||
continue // announces nothing from code: read as before
|
||||
}
|
||||
readable, keeps := a[state]
|
||||
switch {
|
||||
case !keeps:
|
||||
out = append(out, fmt.Sprintf("%s reads %s.%s, which %s's code does not announce: the read is refused on the mesh", reader, owner, state, owner))
|
||||
case !readable:
|
||||
out = append(out, fmt.Sprintf("%s reads %s.%s, which %s's code announces not readable: the read is refused on the mesh; announce it readable", reader, owner, state, owner))
|
||||
}
|
||||
}
|
||||
if a == nil {
|
||||
continue // announces nothing from code: read as before
|
||||
}
|
||||
readable, keeps := a[state]
|
||||
switch {
|
||||
case !keeps:
|
||||
out = append(out, fmt.Sprintf("%s reads %s.%s, which %s's code does not announce: the read is refused on the mesh", reader, owner, state, owner))
|
||||
case !readable:
|
||||
out = append(out, fmt.Sprintf("%s reads %s.%s, which %s's code announces not readable: the read is refused on the mesh; announce it readable", reader, owner, state, owner))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,15 @@ func TestFindings(t *testing.T) {
|
||||
"modules/r/module.json": `{"module":"r","reads":["o.bound"]}`,
|
||||
"modules/o/module.json": `{"module":"o"}`,
|
||||
"modules/o/index.ts": `await keeps([{ name: "bound", readable: true }]);`}, ""},
|
||||
{"seat form, the holder's state not readable", map[string]string{
|
||||
"modules/r/module.json": `{"module":"r","reads":["seat:the-seat.s"]}`,
|
||||
"modules/o/module.json": `{"module":"o","claims":[{"name":"the-seat","scope":"mesh"}]}`,
|
||||
"modules/o/cmd/o/main.go": goOwner + "var _ = announce.State{Name: \"s\"}\n"},
|
||||
"r reads o.s, which o's code announces not readable"},
|
||||
{"seat form, the holder's state readable, and a seat no module of the catalogue holds", map[string]string{
|
||||
"modules/r/module.json": `{"module":"r","reads":["seat:the-seat.s","seat:elsewhere.s"]}`,
|
||||
"modules/o/module.json": `{"module":"o","claims":[{"name":"the-seat","scope":"mesh"}]}`,
|
||||
"modules/o/cmd/o/main.go": goOwner + "var _ = announce.State{Name: \"s\", Readable: true}\n"}, ""},
|
||||
{"an owner that announces nothing, and one outside the catalogue", map[string]string{
|
||||
"modules/r/module.json": `{"module":"r","reads":["o.s","mesh-controller.lease"]}`,
|
||||
"modules/o/module.json": `{"module":"o"}`,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Command removedpackages refuses a Go source of the catalogue that imports what mesh-sdk gave up in the release that
|
||||
// narrowed it (novox/hq ADR 0593 §1): a package that left for its owner, and the build runner's names that left
|
||||
// mesh-sdk's buildstep for mesh-build-go's package.
|
||||
// mesh-sdk's buildstep for the build runner's package (now the delivery SDK's steprun).
|
||||
//
|
||||
// A module compiles against the mesh-sdk version it pins, so a source that still names a removed package builds until the
|
||||
// pin moves, and then breaks in a delivery of its own; and a later change could bring the import back with the old pin.
|
||||
@@ -8,8 +8,8 @@
|
||||
//
|
||||
// - an import of a package in Removed (or one under it);
|
||||
// - in a file importing mesh-sdk's buildstep (under any name), a selector naming anything that is not in
|
||||
// BuildstepContract: what is left of that package is what the delivery parts read, and the runner is
|
||||
// mesh-build-go's package (git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep).
|
||||
// BuildstepContract: what is left of that package is what the delivery parts read, and the runner is the
|
||||
// delivery SDK's package steprun (git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun).
|
||||
//
|
||||
// It reads the non-test and test Go files under modules/ and checks/ (testdata, vendor and node_modules left out).
|
||||
//
|
||||
@@ -35,8 +35,8 @@ const sdk = "git.novox.be/novox/mesh-sdk/go"
|
||||
|
||||
// Removed are the packages of mesh-sdk that left it, each with where its logic is now.
|
||||
var Removed = map[string]string{
|
||||
"buildcpu": "the build runner's package, git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep/buildcpu",
|
||||
"buildstep/conformance": "the build runner's package, git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep/conformance",
|
||||
"buildcpu": "the build runner's package, git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun/buildcpu",
|
||||
"buildstep/conformance": "the build runner's package, git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun/conformance",
|
||||
"delivery/buildrun": "the builder's package (mesh-delivery-builder), imported by its repository's modules",
|
||||
"compose": "the node-engine, which alone composes (ADR 0593 §6)",
|
||||
"compose/frombus": "the node-engine, which alone composes (ADR 0593 §6)",
|
||||
@@ -140,7 +140,7 @@ func fileFindings(path, rel string) ([]string, error) {
|
||||
return true
|
||||
}
|
||||
seen[sel.Sel.Name] = true
|
||||
out = append(out, fmt.Sprintf("%s: uses %s.%s, which is the build runner's, no longer mesh-sdk's buildstep; import git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep (novox/hq ADR 0593 §1)", rel, buildstep, sel.Sel.Name))
|
||||
out = append(out, fmt.Sprintf("%s: uses %s.%s, which is the build runner's, no longer mesh-sdk's buildstep; import git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun (novox/hq ADR 0593 §1)", rel, buildstep, sel.Sel.Name))
|
||||
return true
|
||||
})
|
||||
return out, nil
|
||||
|
||||
@@ -18,11 +18,11 @@ func TestFindingsRefuseWhatMeshSDKGaveUp(t *testing.T) {
|
||||
{"clean", "modules/a/x.go", head + "\t\"git.novox.be/novox/mesh-sdk/go/delivery\"\n)\n\nvar _ = delivery.StepsState\n", ""},
|
||||
{"a removed package", "modules/a/x.go", head + "\t\"git.novox.be/novox/mesh-sdk/go/compose\"\n)\n\nvar _ = compose.Resolve\n", "imports git.novox.be/novox/mesh-sdk/go/compose, which left mesh-sdk; it is the node-engine"},
|
||||
{"under a removed package", "modules/a/x_test.go", head + "\t\"git.novox.be/novox/mesh-sdk/go/compose/frombus\"\n)\n", "the node-engine"},
|
||||
{"buildcpu", "checks/c/x.go", head + "\t\"git.novox.be/novox/mesh-sdk/go/buildcpu\"\n)\n", "buildstep/buildcpu"},
|
||||
{"buildcpu", "checks/c/x.go", head + "\t\"git.novox.be/novox/mesh-sdk/go/buildcpu\"\n)\n", "steprun/buildcpu"},
|
||||
{"the runner through mesh-sdk's buildstep", "modules/a/x.go", head + "\t\"git.novox.be/novox/mesh-sdk/go/buildstep\"\n)\n\nvar _ = buildstep.RunAsk\n", "uses buildstep.RunAsk, which is the build runner's"},
|
||||
{"the runner through an alias", "modules/a/x.go", head + "\tsdkstep \"git.novox.be/novox/mesh-sdk/go/buildstep\"\n)\n\nvar _ = sdkstep.Puller{}\n", "uses sdkstep.Puller"},
|
||||
{"the contract of buildstep", "modules/a/x.go", head + "\t\"git.novox.be/novox/mesh-sdk/go/buildstep\"\n)\n\nvar _ = buildstep.NewID\nvar _ buildstep.Failure\n", ""},
|
||||
{"the runner's own package", "modules/a/x.go", head + "\t\"git.novox.be/novox/mesh-catalog/modules/mesh-build-go/buildstep\"\n)\n\nvar _ = buildstep.RunAsk\n", ""},
|
||||
{"the runner's own package", "modules/a/x.go", head + "\t\"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk/steprun\"\n)\n\nvar _ = steprun.RunAsk\n", ""},
|
||||
{"a copy in testdata", "modules/a/testdata/x.go", head + "\t\"git.novox.be/novox/mesh-sdk/go/compose\"\n)\n", ""},
|
||||
} {
|
||||
t.Run(row.name, func(t *testing.T) {
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
module git.novox.be/novox/mesh-catalog/checks/requiredmodules
|
||||
|
||||
go 1.22
|
||||
@@ -0,0 +1,204 @@
|
||||
// Command requiredmodules refuses a Go module that imports a package of a mesh module its go.mod does not require
|
||||
// (issue 1921, issue 1966; the second time a local workspace hid it: the candidate's build resolves only what go.mod says).
|
||||
//
|
||||
// A developer's go.work resolves every sibling module, so a source can import a package whose module the go.mod never
|
||||
// names and still build; the build seat builds against the go.mod alone and then fails with "finding module for
|
||||
// package". This check reads every go.mod under the roots it reports and every Go source beside it (test files and files
|
||||
// behind a build tag included, testdata, vendor and node_modules left out) and, for each import under
|
||||
// git.novox.be/novox/, finds the module that owns the package (the longest `module` line, among the modules of every
|
||||
// root it is given, that is a prefix of the import) and demands that module to be the go.mod's own or one it requires.
|
||||
// Where no known module owns the import, any own or required module that is a prefix of it satisfies it.
|
||||
//
|
||||
// go run . <root> [<root> ...]
|
||||
//
|
||||
// The first root is reported; the others (the repositories cloned beside, MESH_CHECK_BESIDE) only say which module owns
|
||||
// which path. With -all every root is reported. Exits 1 on a finding.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const prefix = "git.novox.be/novox/"
|
||||
|
||||
// Mod is a go.mod: where it is, its module path and the modules it requires.
|
||||
type Mod struct {
|
||||
Dir string
|
||||
Path string
|
||||
Requires map[string]bool
|
||||
}
|
||||
|
||||
// Modules reads every go.mod under root.
|
||||
func Modules(root string) ([]Mod, error) {
|
||||
var out []Mod
|
||||
err := filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.IsDir() {
|
||||
switch d.Name() {
|
||||
case "testdata", "vendor", "node_modules", ".git":
|
||||
return fs.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if d.Name() != "go.mod" {
|
||||
return nil
|
||||
}
|
||||
raw, err := os.ReadFile(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m := Mod{Dir: filepath.Dir(p), Requires: map[string]bool{}}
|
||||
in := false
|
||||
for _, line := range strings.Split(string(raw), "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if i := strings.Index(line, "//"); i >= 0 {
|
||||
line = strings.TrimSpace(line[:i])
|
||||
}
|
||||
f := strings.Fields(line)
|
||||
switch {
|
||||
case len(f) == 2 && f[0] == "module":
|
||||
m.Path = strings.Trim(f[1], `"`)
|
||||
case len(f) >= 1 && f[0] == "require" && len(f) >= 2 && f[1] == "(":
|
||||
in = true
|
||||
case line == ")":
|
||||
in = false
|
||||
case len(f) >= 3 && f[0] == "require":
|
||||
m.Requires[f[1]] = true
|
||||
case in && len(f) >= 2:
|
||||
m.Requires[f[0]] = true
|
||||
}
|
||||
}
|
||||
if m.Path == "" {
|
||||
return fmt.Errorf("%s names no module", p)
|
||||
}
|
||||
out = append(out, m)
|
||||
return nil
|
||||
})
|
||||
return out, err
|
||||
}
|
||||
|
||||
func within(path, module string) bool { return path == module || strings.HasPrefix(path, module+"/") }
|
||||
|
||||
// Findings reports, for every go.mod under the report roots, each imported package whose module the go.mod neither is nor
|
||||
// requires; known are the other roots that say which module owns a path.
|
||||
func Findings(report, known []string) ([]string, error) {
|
||||
var owners []string
|
||||
var reported []Mod
|
||||
for _, r := range report {
|
||||
ms, err := Modules(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reported = append(reported, ms...)
|
||||
}
|
||||
for _, r := range append(append([]string{}, report...), known...) {
|
||||
ms, err := Modules(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, m := range ms {
|
||||
owners = append(owners, m.Path)
|
||||
}
|
||||
}
|
||||
var out []string
|
||||
for _, m := range reported {
|
||||
seen := map[string]bool{}
|
||||
err := filepath.WalkDir(m.Dir, func(p string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.IsDir() {
|
||||
switch d.Name() {
|
||||
case "testdata", "vendor", "node_modules", ".git":
|
||||
return fs.SkipDir
|
||||
}
|
||||
if p != m.Dir {
|
||||
if _, err := os.Stat(filepath.Join(p, "go.mod")); err == nil {
|
||||
return fs.SkipDir // a nested module is judged by its own go.mod
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !strings.HasSuffix(p, ".go") {
|
||||
return nil
|
||||
}
|
||||
f, err := parser.ParseFile(token.NewFileSet(), p, nil, parser.ImportsOnly)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s: %w", p, err)
|
||||
}
|
||||
for _, im := range f.Imports {
|
||||
path, _ := strconv.Unquote(im.Path.Value)
|
||||
if !strings.HasPrefix(path, prefix) || seen[path] {
|
||||
continue
|
||||
}
|
||||
owner := ""
|
||||
for _, o := range owners {
|
||||
if within(path, o) && len(o) > len(owner) {
|
||||
owner = o
|
||||
}
|
||||
}
|
||||
ok := false
|
||||
if owner != "" {
|
||||
ok = owner == m.Path || m.Requires[owner]
|
||||
} else {
|
||||
ok = within(path, m.Path)
|
||||
for r := range m.Requires {
|
||||
ok = ok || within(path, r)
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
seen[path] = true
|
||||
if owner == "" {
|
||||
owner = "(its module)"
|
||||
}
|
||||
rel, _ := filepath.Rel(m.Dir, p)
|
||||
out = append(out, fmt.Sprintf("%s: %s imports %s (module %s), which its go.mod does not require", filepath.Join(m.Dir, "go.mod"), rel, path, owner))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
all := len(args) > 0 && args[0] == "-all"
|
||||
if all {
|
||||
args = args[1:]
|
||||
}
|
||||
if len(args) < 1 {
|
||||
fmt.Fprintln(os.Stderr, "usage: requiredmodules [-all] <root> [<root> ...]")
|
||||
os.Exit(2)
|
||||
}
|
||||
report, known := args[:1], args[1:]
|
||||
if all {
|
||||
report, known = args, nil
|
||||
}
|
||||
found, err := Findings(report, known)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(2)
|
||||
}
|
||||
for _, f := range found {
|
||||
fmt.Println(f)
|
||||
}
|
||||
if len(found) > 0 {
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("every module requires the mesh modules it imports")
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func tree(t *testing.T, files map[string]string) string {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
for p, c := range files {
|
||||
full := filepath.Join(root, p)
|
||||
if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(full, []byte(c), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
const aMod = "module git.novox.be/novox/r/a\n\ngo 1.22\n"
|
||||
const bMod = "module git.novox.be/novox/r/b/sub\n\ngo 1.22\n"
|
||||
|
||||
// A module that imports a package of a mesh module its go.mod does not require is refused, in a test file and behind a
|
||||
// build tag too; the module's own packages, a required module's and a nested module's (only when required) are not.
|
||||
func TestAModuleImportsOnlyWhatItRequires(t *testing.T) {
|
||||
src := func(imp string) string { return "package x\n\nimport _ \"" + imp + "\"\n" }
|
||||
for _, row := range []struct {
|
||||
name string
|
||||
files map[string]string
|
||||
want string
|
||||
}{
|
||||
{"its own package", map[string]string{"a/go.mod": aMod, "a/x.go": src("git.novox.be/novox/r/a/other")}, ""},
|
||||
{"a required module", map[string]string{"a/go.mod": aMod + "\nrequire git.novox.be/novox/r/b/sub v0.0.0\n", "a/x.go": src("git.novox.be/novox/r/b/sub/pkg"), "b/go.mod": bMod}, ""},
|
||||
{"a required module in a block", map[string]string{"a/go.mod": aMod + "\nrequire (\n\tgit.novox.be/novox/r/b/sub v0.0.0 // indirect\n)\n", "a/x.go": src("git.novox.be/novox/r/b/sub/pkg"), "b/go.mod": bMod}, ""},
|
||||
{"a module not required", map[string]string{"a/go.mod": aMod, "a/x.go": src("git.novox.be/novox/r/b/sub/pkg"), "b/go.mod": bMod}, "x.go imports git.novox.be/novox/r/b/sub/pkg"},
|
||||
{"a test file", map[string]string{"a/go.mod": aMod, "a/x_test.go": src("git.novox.be/novox/r/b/sub/pkg"), "b/go.mod": bMod}, "x_test.go imports"},
|
||||
{"an integration file", map[string]string{"a/go.mod": aMod, "a/x.go": "//go:build integration\n\n" + src("git.novox.be/novox/r/b/sub/pkg"), "b/go.mod": bMod}, "x.go imports"},
|
||||
{"a module the longest owner is not", map[string]string{"a/go.mod": aMod + "\nrequire git.novox.be/novox/r/b v0.0.0\n", "a/x.go": src("git.novox.be/novox/r/b/sub/pkg"), "b/go.mod": "module git.novox.be/novox/r/b\n", "b/sub/go.mod": bMod}, "does not require"},
|
||||
{"a testdata fixture", map[string]string{"a/go.mod": aMod, "a/testdata/x.go": src("git.novox.be/novox/r/b/sub/pkg"), "a/x.go": "package x\n"}, ""},
|
||||
{"an outside module", map[string]string{"a/go.mod": aMod, "a/x.go": src("github.com/x/y")}, ""},
|
||||
} {
|
||||
t.Run(row.name, func(t *testing.T) {
|
||||
got, err := Findings([]string{tree(t, row.files)}, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if all := strings.Join(got, "\n"); (row.want == "") != (all == "") || !strings.Contains(all, row.want) {
|
||||
t.Fatalf("findings = %q, want one containing %q", all, row.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// The script's run of this check over the real tree (novox/hq ADR 0529 clause 2): the repositories cloned beside it, when
|
||||
// the check says where, tell which module owns a path.
|
||||
func TestEveryCatalogueModuleRequiresWhatItImports(t *testing.T) {
|
||||
var known []string
|
||||
if beside := os.Getenv("MESH_CHECK_BESIDE"); beside != "" {
|
||||
es, _ := os.ReadDir(beside)
|
||||
for _, e := range es {
|
||||
if e.IsDir() && e.Name() != "mesh-catalog" {
|
||||
known = append(known, filepath.Join(beside, e.Name()))
|
||||
}
|
||||
}
|
||||
}
|
||||
got, err := Findings([]string{filepath.Join("..", "..")}, known)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, f := range got {
|
||||
t.Error(f)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
# What these tests read outside their package, as paths from the repository's root (a glob, or a directory read whole;
|
||||
# ? before a path whose absence is read). mesh-build-go hashes them into the package's digest, so its verdict is kept
|
||||
# until one of them changes (issue 1936). An empty list, or a line matching nothing, keeps nothing.
|
||||
# every go.mod and Go source of the modules and of the checks, whose imports and requires are read.
|
||||
modules
|
||||
checks
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"module": "adwaita",
|
||||
"version": "1",
|
||||
"version": "0.1",
|
||||
"moved": true,
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
|
||||
@@ -5,7 +5,7 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
sweep "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshartifactsweep"
|
||||
sweep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshartifactsweep"
|
||||
)
|
||||
|
||||
// The holders whose states this module reads, one constant each (a reader names its holder once; ADR 0634 §4).
|
||||
|
||||
@@ -20,21 +20,21 @@ import (
|
||||
"fmt"
|
||||
engineclient "git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient"
|
||||
nodeengine "git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
||||
catalogclient "git.novox.be/novox/mesh-delivery/contracts/go/clients/meshcatalogclient"
|
||||
catalog "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
|
||||
meshwants "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshwants"
|
||||
builds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
|
||||
catalogclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
|
||||
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
||||
meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
||||
artifactref "git.novox.be/novox/mesh-delivery/contracts/go/clients/containerimageregistryclient/reference"
|
||||
artifactref "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/containerimageregistryclient/reference"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/keys"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
)
|
||||
|
||||
@@ -45,8 +45,6 @@ const (
|
||||
HavesRead = "node-engine.have"
|
||||
// ManifestsRead is the indexer's manifests: the images a manifest pins by itself.
|
||||
ManifestsRead = indexerModule + "." + catalog.StateManifests
|
||||
// FindingsState is the holder's own findings (meshconditions.StateFindings): image-missing.
|
||||
FindingsState = meshconditions.StateFindings
|
||||
// Module is this module, the observer its findings name.
|
||||
Module = "artifact-store-tools"
|
||||
)
|
||||
@@ -83,8 +81,10 @@ type Door interface {
|
||||
|
||||
// Holder is the seat's holder.
|
||||
type Holder struct {
|
||||
Bus Bus
|
||||
Door func() (Door, error)
|
||||
Bus Bus
|
||||
// Findings is the module's finding writer: S25's and the sweep's findings go through it.
|
||||
Findings Findings
|
||||
Door func() (Door, error)
|
||||
// Listed is the store as its seats list it now (Store.View); nil reads none, and then nothing is said gone or collected.
|
||||
Listed func(ctx context.Context) (*View, error)
|
||||
Acts meshhandactsclient.Recorder
|
||||
@@ -95,6 +95,7 @@ type Holder struct {
|
||||
|
||||
sweep sync.Mutex // one sweep at a time
|
||||
mu sync.Mutex
|
||||
fmu sync.Mutex // one pass of Observe at a time
|
||||
ended map[string]time.Time // a done key's end, read once from its history
|
||||
}
|
||||
|
||||
@@ -852,16 +853,60 @@ func inStoreAt(image, address string) string {
|
||||
return r
|
||||
}
|
||||
|
||||
// Observe writes S25's findings (image-missing): each want whose build names an image the store does not hold, on the
|
||||
// want's machine, and deletes each finding that no longer holds. Nothing is said without a listing.
|
||||
// Observe says S25's findings (image-missing): each want whose build names an image the store does not hold, on the
|
||||
// want's machine, written once and cleared once it no longer holds. Nothing is said without a listing. What stands is what
|
||||
// the held writer holds (design 47 G A-32, A-38), so a restart neither drops nor rewrites it.
|
||||
func (h *Holder) Observe(ctx context.Context) error {
|
||||
j, err := h.judge(ctx)
|
||||
now, err := h.observed(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if j.view == nil {
|
||||
if now == nil {
|
||||
return nil
|
||||
}
|
||||
h.fmu.Lock()
|
||||
defer h.fmu.Unlock()
|
||||
said := map[string]meshconditions.Finding{}
|
||||
for _, f := range h.Findings.Held() {
|
||||
if f.Kind == ImageMissing {
|
||||
said[views.FindingKey(f.Scope, f.ID, f.Kind)] = f
|
||||
}
|
||||
}
|
||||
for k, was := range said {
|
||||
if _, still := now[k]; !still {
|
||||
if err := h.Findings.Clear(ctx, was.Scope, was.ID, was.Kind, "the store holds the image again"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
for k, f := range now {
|
||||
if was, ok := said[k]; ok && was.Summary == f.Summary {
|
||||
continue // written only when what it says changes
|
||||
}
|
||||
if err := h.Findings.Write(ctx, f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Findings is the module's held finding writer (views.HeldWriter): the holder writes and clears through it and reads what it
|
||||
// holds (design 47 G §689, A-29, A-38).
|
||||
type Findings interface {
|
||||
Write(ctx context.Context, finding meshconditions.Finding) error
|
||||
Clear(ctx context.Context, scope, id, kind, why string) error
|
||||
Held() []meshconditions.Finding
|
||||
}
|
||||
|
||||
// observed is S25's findings now by key, nil without a listing.
|
||||
func (h *Holder) observed(ctx context.Context) (map[string]meshconditions.Finding, error) {
|
||||
j, err := h.judge(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if j.view == nil {
|
||||
return nil, nil
|
||||
}
|
||||
address := ""
|
||||
if d, err := h.Door(); err == nil {
|
||||
address = d.Address()
|
||||
@@ -870,7 +915,7 @@ func (h *Holder) Observe(ctx context.Context) error {
|
||||
for _, b := range j.built {
|
||||
builds[b.Module+" "+b.FP] = b
|
||||
}
|
||||
now := map[string]meshconditions.FindingsByKeyValue{}
|
||||
now := map[string]meshconditions.Finding{}
|
||||
for _, w := range j.wants {
|
||||
b, ok := builds[w.Module+" "+w.FP]
|
||||
if !ok {
|
||||
@@ -895,63 +940,26 @@ func (h *Holder) Observe(ctx context.Context) error {
|
||||
continue
|
||||
}
|
||||
f := imageMissing(w.Node, w.Module, reference, h.now())
|
||||
now[f.Key] = f
|
||||
now[views.FindingKey(f.Scope, f.ID, f.Kind)] = f
|
||||
break
|
||||
}
|
||||
}
|
||||
keys, err := h.Bus.Keys(FindingsState)
|
||||
if err != nil {
|
||||
return fmt.Errorf("the findings: %w", err)
|
||||
}
|
||||
for _, k := range keys {
|
||||
if k == sweepFindingKey {
|
||||
continue // the sweep's own finding, which the sweep clears
|
||||
}
|
||||
if _, still := now[k]; !still {
|
||||
// A finding of a first view unanswered is the reader's (views.Reader), cleared by its answer, not by a sweep.
|
||||
if raw, found, err := h.Bus.Get(FindingsState, k); err == nil && found {
|
||||
if f, err := meshconditions.DecodeFindingsByKeyValue(raw); err == nil && f.Kind == views.UnansweredKind {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if err := h.Bus.Delete(FindingsState, k); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
for k, f := range now {
|
||||
raw, found, err := h.Bus.Get(FindingsState, k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if found {
|
||||
if was, err := meshconditions.DecodeFindingsByKeyValue(raw); err == nil && was.Summary == f.Summary {
|
||||
continue // written only when what it says changes
|
||||
}
|
||||
}
|
||||
if err := meshconditionsclient.CheckFinding(f); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := h.Bus.Put(FindingsState, k, f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
return now, nil
|
||||
}
|
||||
|
||||
// imageMissing is S25's finding, in the controller's words (cmd/mesh-controller/image_missing.go watchMissingImages).
|
||||
func imageMissing(node, module, image string, at time.Time) meshconditions.FindingsByKeyValue {
|
||||
id := module + "." + node
|
||||
return meshconditions.FindingsByKeyValue{V: meshconditionsclient.FindingVersion, Key: meshconditionsclient.Key(meshconditionsclient.ScopeModule, id, ImageMissing), Kind: ImageMissing,
|
||||
Subject: meshconditions.Subject{Scope: meshconditionsclient.ScopeModule, ID: id, Machine: node},
|
||||
Severity: meshconditionsclient.Warning, Resolver: meshconditionsclient.ResolverOperator,
|
||||
// imageMissing is S25's finding, in the controller's words (cmd/mesh-controller/image_missing.go watchMissingImages); its id
|
||||
// is the module's and the machine's, each escaped (design 47 G A-14).
|
||||
func imageMissing(node, module, image string, at time.Time) meshconditions.Finding {
|
||||
id := keys.EscapeName(module) + "." + keys.EscapeName(node)
|
||||
return meshconditions.Finding{Scope: "module", ID: id, Kind: ImageMissing,
|
||||
Subject: meshconditions.Subject{Scope: "module", ID: id, Machine: node},
|
||||
Severity: meshconditions.FindingSeverityWarning, Resolver: "operator",
|
||||
Summary: fmt.Sprintf("%s is left out of every send to %s: its declaration names an image that the artifact store does "+
|
||||
"not hold (the evidence names it); the running container is untouched", module, node),
|
||||
Headline: capital(module) + " is not updated on " + node,
|
||||
Explanation: fmt.Sprintf("The new version of %s names a container image that the mesh's store does not hold, so it "+
|
||||
"was not sent to %s. What runs there is untouched, and the rest of %s is sent as usual.", module, node, node),
|
||||
Needs: fmt.Sprintf("build %s again; the details name the image.", module),
|
||||
Resolved: "Resolved: the store holds the image of " + module + " again",
|
||||
Evidence: []meshconditions.Evidence{{At: at.UTC(), Said: "image " + image + " is not in the store"}},
|
||||
Source: "S25", Observer: Module, At: at.UTC()}
|
||||
}
|
||||
|
||||
@@ -198,7 +198,7 @@ func TestTheSeatSweepsAtABuildsEndOnARealBusWithNoLease(t *testing.T) {
|
||||
}
|
||||
return m.Data, nil
|
||||
}
|
||||
h := &Holder{Bus: findingsAt{realBus: bus}, Door: mem.Door, Listed: mem.Listed, Seats: ask, Now: time.Now, Logf: t.Logf}
|
||||
h := &Holder{Bus: findingsAt{realBus: bus}, Findings: findingsThrough{findingsAt{realBus: bus}}, Door: mem.Door, Listed: mem.Listed, Seats: ask, Now: time.Now, Logf: t.Logf}
|
||||
h.Acts = meshhandactsclient.Recorder{Ask: ask}
|
||||
|
||||
// A build's end heard from the builds bucket, as follow hands it over.
|
||||
|
||||
@@ -6,10 +6,10 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
nodeengine "git.novox.be/novox/mesh-contracts/go/seats/nodeengine"
|
||||
catalogclient "git.novox.be/novox/mesh-delivery/contracts/go/clients/meshcatalogclient"
|
||||
catalog "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
|
||||
meshwants "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshwants"
|
||||
builds "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"
|
||||
catalogclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
|
||||
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
meshwants "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -17,14 +17,14 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
||||
meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
|
||||
artifactref "git.novox.be/novox/mesh-delivery/contracts/go/clients/containerimageregistryclient/reference"
|
||||
reg "git.novox.be/novox/mesh-delivery/contracts/go/seats/containerimageregistry"
|
||||
ociseat "git.novox.be/novox/mesh-delivery/contracts/go/seats/ociartifactregistry"
|
||||
artifactref "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/containerimageregistryclient/reference"
|
||||
reg "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
|
||||
ociseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry"
|
||||
)
|
||||
|
||||
// memBus is the holder's bus in memory: states by name, each key's history, and what was published.
|
||||
@@ -229,7 +229,7 @@ func aMesh(t *testing.T) (*Holder, *memBus, *memDoor, *[]string) {
|
||||
want, _ := json.Marshal(meshwants.WantsByWantNodeModuleValue{V: 1, Fp: fp(1), Self: true})
|
||||
_ = b.Put(WantsRead, "want.ace.letta", json.RawMessage(want))
|
||||
var acts []string
|
||||
h := &Holder{Bus: b, Door: func() (Door, error) { return door, nil }, Seats: door.seat,
|
||||
h := &Holder{Bus: b, Findings: findingsThrough{b}, Door: func() (Door, error) { return door, nil }, Seats: door.seat,
|
||||
Listed: func(context.Context) (*View, error) { return &View{L: listing}, nil },
|
||||
Now: func() time.Time { return day.Add(48 * time.Hour) },
|
||||
Acts: meshhandactsclient.Recorder{Ask: func(key string, body any) (json.RawMessage, error) {
|
||||
@@ -370,7 +370,7 @@ func TestTheSeatKeepsTheFiveNewestAndTheWantedAndSweepsWithNoLease(t *testing.T)
|
||||
if c.letGo[0] == archiveRef("letta", 2) && !slices.Contains(door.calls, "seat:oci-artifact-registry.remove letta/files@"+digestOf(archiveRef("letta", 2))+" "+Retention) {
|
||||
t.Errorf("the seats were called %v", door.calls)
|
||||
}
|
||||
_, finding, _ := b.Get(FindingsState, sweepFindingKey)
|
||||
_, finding, _ := b.Get(FindingsState, views.FindingKey("module", Module, SweepRefused))
|
||||
if finding != (c.refuse != "") {
|
||||
t.Errorf("the sweep's finding is written: %v, want %v", finding, c.refuse != "")
|
||||
}
|
||||
@@ -379,7 +379,7 @@ func TestTheSeatKeepsTheFiveNewestAndTheWantedAndSweepsWithNoLease(t *testing.T)
|
||||
if _, err := h.Sweep(context.Background(), Sweeping{Most: MostPerSweep, By: "again"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, still, _ := b.Get(FindingsState, sweepFindingKey); still {
|
||||
if _, still, _ := b.Get(FindingsState, views.FindingKey("module", Module, SweepRefused)); still {
|
||||
t.Error("a clean sweep left the sweep's finding")
|
||||
}
|
||||
return
|
||||
@@ -410,13 +410,16 @@ func TestAWantedImageTheStoreLacksIsAFindingAndImagesNamesWantsAndHaves(t *testi
|
||||
if err := h.Observe(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key := meshconditionsclient.Key(meshconditionsclient.ScopeModule, "letta.ace", ImageMissing)
|
||||
key := views.FindingKey("module", "letta.ace", ImageMissing)
|
||||
raw, found, _ := b.Get(FindingsState, key)
|
||||
f, err := meshconditions.DecodeFindingsByKeyValue(raw)
|
||||
var f meshconditions.Finding
|
||||
err := json.Unmarshal(raw, &f)
|
||||
if !found || err != nil || f.Subject.Machine != "ace" || f.Source != "S25" || !strings.Contains(f.Evidence[0].Said, imageRef("letta", 1)) {
|
||||
t.Fatalf("the finding is %+v (%v, %v)", f, found, err)
|
||||
}
|
||||
view.L.Repos[repo].Revisions[digest] = true
|
||||
// A restart: a holder made afresh holds no finding of its own, and clears what the writer holds (design 47 G A-32, A-38).
|
||||
h = &Holder{Bus: h.Bus, Findings: h.Findings, Door: h.Door, Listed: h.Listed, Seats: h.Seats, Now: h.Now, Acts: h.Acts, Logf: h.Logf}
|
||||
if err := h.Observe(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -441,3 +444,41 @@ func TestAWantedImageTheStoreLacksIsAFindingAndImagesNamesWantsAndHaves(t *testi
|
||||
t.Fatalf("the images of ace are %+v: %v", images, err)
|
||||
}
|
||||
}
|
||||
|
||||
// FindingsState is the module's state `findings`, where the tests' writer keeps what the holder writes through it.
|
||||
const FindingsState = "findings"
|
||||
|
||||
// findingsThrough is the holder's finding writer in a test: it keeps a finding in its key and deletes it as the module's
|
||||
// writer does (views.Writer), without the bus.
|
||||
type findingsThrough struct {
|
||||
bus interface {
|
||||
Put(state, key string, value any) error
|
||||
Delete(state, key string) error
|
||||
}
|
||||
}
|
||||
|
||||
func (f findingsThrough) Write(_ context.Context, finding meshconditions.Finding) error {
|
||||
return f.bus.Put(FindingsState, views.FindingKey(finding.Scope, finding.ID, finding.Kind), finding)
|
||||
}
|
||||
|
||||
// Held is what the writer holds: the findings in the module's state.
|
||||
func (f findingsThrough) Held() []meshconditions.Finding {
|
||||
var out []meshconditions.Finding
|
||||
keys, _ := f.bus.(interface {
|
||||
Keys(string) ([]string, error)
|
||||
}).Keys(FindingsState)
|
||||
for _, k := range keys {
|
||||
raw, ok, _ := f.bus.(interface {
|
||||
Get(string, string) ([]byte, bool, error)
|
||||
}).Get(FindingsState, k)
|
||||
var m meshconditions.Finding
|
||||
if ok && json.Unmarshal(raw, &m) == nil {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (f findingsThrough) Clear(_ context.Context, scope, id, kind, _ string) error {
|
||||
return f.bus.Delete(FindingsState, views.FindingKey(scope, id, kind))
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
ociseat "git.novox.be/novox/mesh-delivery/contracts/go/seats/ociartifactregistry"
|
||||
ociseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry"
|
||||
|
||||
reg "git.novox.be/novox/mesh-delivery/contracts/go/seats/containerimageregistry"
|
||||
reg "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
|
||||
)
|
||||
|
||||
// Repo is one repository as the seats' list answers it.
|
||||
|
||||
@@ -13,8 +13,8 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient"
|
||||
meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
@@ -24,12 +24,29 @@ func logf(format string, a ...any) { fmt.Fprintf(os.Stderr, format+"\n", a...) }
|
||||
|
||||
func main() {
|
||||
s := &Store{Ask: stdio.Ask}
|
||||
v := &views.Reader{Module: "artifact-store-tools", Logf: logf, Findings: meshconditionsclient.FindingWriter{State: stdio.State("findings")}, Acts: meshhandactsclient.Recorder{Ask: stdio.Ask}}
|
||||
v := &views.Reader{Module: "artifact-store-tools", Logf: logf, Acts: meshhandactsclient.Recorder{Ask: stdio.Ask}}
|
||||
v.Follow(assignerModule, "wants", WantsTool)
|
||||
v.Follow(indexerModule, "manifests", ManifestsTool)
|
||||
h := &Holder{Bus: runtimeBus{views: v}, Door: DoorAt(os.Getenv("MESH_ARTIFACT_STORE_ADDRESS_FILE"), s), Listed: s.View,
|
||||
Seats: stdio.Ask, Now: time.Now, Logf: logf}
|
||||
h.Acts = meshhandactsclient.Recorder{Ask: stdio.Ask}
|
||||
// The module's one held writer (design 47 G §689, A-16, A-29, A-32): the holder's findings and the reader's go through it.
|
||||
w, err := views.NewStdioHeldWriter(views.WriterConfig[meshconditions.Finding, meshconditions.FindingCleared, meshconditions.FindingsStated, meshconditions.FindingsAskedEvent]{
|
||||
Asked: meshconditions.SubjectFindingsAsked, Module: Module, State: stdio.State("findings"), Kinds: []string{ImageMissing, SweepRefused}})
|
||||
if err != nil {
|
||||
logf("%v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
h.Findings = w
|
||||
v.Findings = w
|
||||
// the runtime answers the writer's subscription only once Serve has its channel
|
||||
go func() {
|
||||
<-stdio.Served()
|
||||
if err := w.Start(context.Background()); err != nil {
|
||||
logf("%v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
v.Start(context.Background())
|
||||
go follow(h, v)
|
||||
// An empty name serves as the module the runtime names (MESH_SERVED_MODULE): artifact-store-tools.
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
sweep "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshartifactsweep"
|
||||
sweep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshartifactsweep"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
reg "git.novox.be/novox/mesh-delivery/contracts/go/seats/containerimageregistry"
|
||||
sweep "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshartifactsweep"
|
||||
reg "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
|
||||
sweep "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshartifactsweep"
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
"git.novox.be/novox/mesh-sdk/go/seatlist/seatlisttest"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
@@ -474,14 +474,19 @@ func TestTheToolsServedAreTheToolsTheManifestNames(t *testing.T) {
|
||||
t.Errorf("invokes %v", m.Invokes)
|
||||
}
|
||||
// ADR 0615: the assigner's wants and the indexer's manifests are asked once and followed by their events, not read.
|
||||
if !reflect.DeepEqual(m.Consumes, []string{"mesh-delivery-assigner.views.wants", "mesh-delivery-indexer.views.manifests"}) ||
|
||||
if !reflect.DeepEqual(m.Consumes, []string{"mesh-delivery-assigner.views.wants", "mesh-delivery-indexer.views.manifests", "mesh-conditions.findings-asked"}) ||
|
||||
slices.Contains(m.Reads, WantsRead) || slices.Contains(m.Reads, ManifestsRead) {
|
||||
t.Errorf("consumes %v, reads %v", m.Consumes, m.Reads)
|
||||
}
|
||||
// The operator, 2026-11-04 (issue 1966): it reaches the store through the registry seats' verbs, and requires no
|
||||
// provision artifact-store.
|
||||
if len(m.Requires) != 0 {
|
||||
t.Errorf("requires %v, want nothing: the store is reached through the registry seats", m.Requires)
|
||||
// What it requires is its finding state and stream alone, in their template form (design 47 G §529).
|
||||
var required []string
|
||||
for _, r := range m.Requires {
|
||||
required = append(required, fmt.Sprint(r.(map[string]any)["name"]))
|
||||
}
|
||||
if !reflect.DeepEqual(required, []string{"findings", "finding-events"}) {
|
||||
t.Errorf("requires %v, want its findings and finding-events alone: the store is reached through the registry seats", m.Requires)
|
||||
}
|
||||
// Its address file is filled from the image seat holder's listen (ADR 0632 §1), never by the retired provision.
|
||||
address, err := os.ReadFile("../../files/store-address")
|
||||
|
||||
@@ -10,25 +10,23 @@ package main
|
||||
// sweep's finding, sweep-refused; the next sweep with no refusal clears it.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
||||
meshconditions "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
||||
artifactref "git.novox.be/novox/mesh-delivery/contracts/go/clients/containerimageregistryclient/reference"
|
||||
ociseat "git.novox.be/novox/mesh-delivery/contracts/go/seats/ociartifactregistry"
|
||||
artifactref "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/containerimageregistryclient/reference"
|
||||
ociseat "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry"
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
"git.novox.be/novox/mesh-sdk/go/keys"
|
||||
|
||||
reg "git.novox.be/novox/mesh-delivery/contracts/go/seats/containerimageregistry"
|
||||
reg "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry"
|
||||
)
|
||||
|
||||
// SweepRefused is the sweep's finding: a seat refused a hold or a remove the sweep asked.
|
||||
const SweepRefused = "sweep-refused"
|
||||
|
||||
// sweepFindingKey is the one key of the sweep's finding in the module's findings.
|
||||
var sweepFindingKey = meshconditionsclient.Key(meshconditionsclient.ScopeModule, Module, SweepRefused)
|
||||
|
||||
// canonical is a kept reference in the older form, `artifact-store://<path>`, whichever form it came in; another is as
|
||||
// it was.
|
||||
func canonical(reference string) string {
|
||||
@@ -115,26 +113,28 @@ func (h *Holder) removeOnSeat(reference, why string) error {
|
||||
// sweepFinding writes the sweep's finding: what the seat refused, and why.
|
||||
func (h *Holder) sweepFinding(what string, err error) {
|
||||
at := h.now().UTC()
|
||||
f := meshconditions.FindingsByKeyValue{V: meshconditionsclient.FindingVersion, Key: sweepFindingKey, Kind: SweepRefused,
|
||||
Subject: meshconditions.Subject{Scope: meshconditionsclient.ScopeModule, ID: Module},
|
||||
Severity: meshconditionsclient.Warning, Resolver: meshconditionsclient.ResolverOperator,
|
||||
f := meshconditions.Finding{Scope: "module", ID: keys.EscapeName(Module), Kind: SweepRefused,
|
||||
Subject: meshconditions.Subject{Scope: "module", ID: keys.EscapeName(Module)},
|
||||
Severity: meshconditions.FindingSeverityWarning, Resolver: "operator",
|
||||
Summary: fmt.Sprintf("the artifact sweep stopped: a registry seat refused %s (the evidence names why)", what),
|
||||
Headline: "The artifact sweep stopped",
|
||||
Explanation: "The sweep keeps what the mesh still needs and removes the rest through the registry seats. A seat " +
|
||||
"refused one of its asks, so it stopped before anything more was removed; what it kept stays kept.",
|
||||
Needs: "read the seat holder's journal for the refusal; the next sweep asks again.",
|
||||
Resolved: "Resolved: the artifact sweep ran through without a refusal",
|
||||
Evidence: []meshconditions.Evidence{{At: at.UTC(), Said: fmt.Sprintf("%s: %v", what, err)}},
|
||||
Source: "CAT-AS1", Observer: Module, At: at}
|
||||
if werr := h.Bus.Put(FindingsState, sweepFindingKey, f); werr != nil {
|
||||
if werr := h.Findings.Write(context.Background(), f); werr != nil {
|
||||
h.logf("the sweep's finding (%s: %v) could not be written: %v", what, err, werr)
|
||||
}
|
||||
}
|
||||
|
||||
// sweepClean clears the sweep's finding after a sweep no seat refused.
|
||||
// sweepClean clears the sweep's finding, when the held writer holds it, after a sweep no seat refused.
|
||||
func (h *Holder) sweepClean() {
|
||||
if _, found, err := h.Bus.Get(FindingsState, sweepFindingKey); err == nil && found {
|
||||
if err := h.Bus.Delete(FindingsState, sweepFindingKey); err != nil {
|
||||
for _, was := range h.Findings.Held() {
|
||||
if was.Kind != SweepRefused {
|
||||
continue
|
||||
}
|
||||
if err := h.Findings.Clear(context.Background(), was.Scope, was.ID, was.Kind, "a sweep ran through without a refusal"); err != nil {
|
||||
h.logf("the sweep's finding could not be cleared: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ func TestEveryFileTakesItsTextFromATemplate(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The builder's keys are read through its published package, taken by its series (ADR 0634 §1).
|
||||
if m.Packages["builds"] != "0.1" {
|
||||
if m.Packages["git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds"] != "0.1" {
|
||||
t.Errorf("packages %v: want builds of series 0.1", m.Packages)
|
||||
}
|
||||
for _, r := range m.Resources {
|
||||
|
||||
@@ -5,18 +5,17 @@ go 1.26.0
|
||||
require (
|
||||
git.novox.be/novox/mesh-contracts/go/clients/nodeengineclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/go/seats/nodeengine v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshhandacts v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/contracts/go/clients/containerimageregistryclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/contracts/go/clients/meshcatalogclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/contracts/go/seats/containerimageregistry v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/contracts/go/seats/meshartifactsweep v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/contracts/go/seats/meshwants v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/contracts/go/seats/ociartifactregistry v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/containerimageregistryclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/containerimageregistry v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshartifactsweep v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshwants v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/ociartifactregistry v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000
|
||||
github.com/nats-io/nats.go v1.54.0
|
||||
)
|
||||
@@ -35,4 +34,6 @@ require (
|
||||
golang.org/x/time v0.16.0 // indirect
|
||||
)
|
||||
|
||||
require git.novox.be/novox/mesh-delivery/sdk v0.0.0-00010101000000-000000000000 // indirect
|
||||
require git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk v0.0.0-00010101000000-000000000000 // indirect
|
||||
|
||||
require git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery v0.0.0-00010101000000-000000000000 // indirect
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"module": "artifact-store-tools",
|
||||
"version": "1",
|
||||
"version": "0.1",
|
||||
"packages": {
|
||||
"builds": "0.1"
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-builder/builds": "0.1"
|
||||
},
|
||||
"claims": [
|
||||
{
|
||||
@@ -39,10 +39,8 @@
|
||||
],
|
||||
"consumes": [
|
||||
"mesh-delivery-assigner.views.wants",
|
||||
"mesh-delivery-indexer.views.manifests"
|
||||
],
|
||||
"state": [
|
||||
"findings"
|
||||
"mesh-delivery-indexer.views.manifests",
|
||||
"mesh-conditions.findings-asked"
|
||||
],
|
||||
"tools": [
|
||||
"artifact_store_repositories",
|
||||
@@ -108,5 +106,26 @@
|
||||
"from": "files"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"requires": [
|
||||
{
|
||||
"name": "findings",
|
||||
"storage": "key-value",
|
||||
"template": "state"
|
||||
},
|
||||
{
|
||||
"name": "finding-events",
|
||||
"storage": "stream",
|
||||
"template": "events",
|
||||
"options": {
|
||||
"events": [
|
||||
"finding-written.*",
|
||||
"finding-cleared.*",
|
||||
"findings-stated.*"
|
||||
],
|
||||
"max-age-days": 7,
|
||||
"max-mib": 16
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"module": "asus-zephyrus-g14",
|
||||
"version": "1",
|
||||
"version": "0.1",
|
||||
"moved": true,
|
||||
"templates": "templates",
|
||||
"capabilities": [
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"module": "audit-logger",
|
||||
"version": "1",
|
||||
"version": "0.1",
|
||||
"slug": "audit",
|
||||
"consumes": [
|
||||
"**"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"module": "avahi",
|
||||
"version": "1",
|
||||
"version": "0.1",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"service-manager"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"module": "baserow",
|
||||
"version": "1",
|
||||
"version": "0.1",
|
||||
"capabilities": [
|
||||
"container-runtime"
|
||||
],
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"module": "blueman",
|
||||
"version": "1",
|
||||
"version": "0.1",
|
||||
"capabilities": [
|
||||
"package-manager"
|
||||
],
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"module": "bluetooth",
|
||||
"version": "1",
|
||||
"version": "0.1",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"service-manager"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"module": "brother",
|
||||
"version": "1",
|
||||
"version": "0.1",
|
||||
"requires": [
|
||||
{
|
||||
"name": "s3-bucket",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"module": "buildah",
|
||||
"version": "1",
|
||||
"version": "0.1",
|
||||
"capabilities": [
|
||||
"package-manager",
|
||||
"user-namespaces"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"module": "ca-trust",
|
||||
"version": "1",
|
||||
"version": "0.1",
|
||||
"slug": "catrust",
|
||||
"capabilities": [
|
||||
"service-manager"
|
||||
|
||||
@@ -28,8 +28,7 @@ And, for where the agent runs as an account of its own (hq ADR 0266):
|
||||
rule and can read no secret the mesh placed for another account. A way found is this module's condition,
|
||||
and the controller's self-check says it urgently.
|
||||
- `/usr/local/bin/claude-agent`, root's, `0755`: starts the agent as that account — `claude` itself when run
|
||||
as it, otherwise `sudo -iu <account> claude` through the operator's own sudo, carrying the guard's
|
||||
override only when it is set in the operator's shell.
|
||||
as it, otherwise `sudo -iu <account> claude` through the operator's own sudo.
|
||||
- `/etc/claude-code/agent-account`, root's, `0644`: the name of that account, the machine fact
|
||||
`agent-account`, for the shell to read — the controller fills no machine fact into shell code.
|
||||
- **`claude` in the operator's interactive zsh is `claude-agent`** where the machine names an agent account:
|
||||
@@ -48,7 +47,7 @@ whenever the node's tool runtime collects the module's tools:
|
||||
| `managed-mcp.json` | the tool servers every session loads: the mesh's console as `mesh` (with a `timeout` of 660000 ms, mesh_wait's longest wait and a minute, so the agent's one-minute default does not cut a wait, issue 1871), and the servers set in this module's `mcp_servers` setting. **Exclusive**: a server not listed here does not load — not one added with `claude mcp add`, not a project's `.mcp.json`, not a plugin's |
|
||||
| `managed-settings.json` | the keys set in this module's `managed_settings` setting — at the controller's terminal alone (hq issue 340) — under the mesh's own keys: the repositories' attribution convention, the claude.ai connectors kept beside the managed servers, the key-helper while the node holds an API-key licence, git's credential file while the node holds the forge's read credential (below), and the two that name the `nox-mesh` marketplace and enable its plugin, and `CLAUDE_CODE_MCP_TOOL_IDLE_TIMEOUT` (660000) in `env` beside the operator's own, the mesh's key winning |
|
||||
| `CLAUDE.md` | how a session on this mesh works, this node's name and role, the conventions, the **"instead of" table** — then the instruction sections registered for every node and for this one |
|
||||
| `marketplace/` | the `nox-mesh` plugin (hq ADR 0216): the skills, subagents, commands and output styles registered for every node and for this one, offered in a session as `nox-mesh:<name>` — and the **guard on the agent's shell** (`guard/`), the mesh's own hook and the plugin's only one (hq issue 340). Replaced whole, staged beside and swapped in |
|
||||
| `marketplace/` | the `nox-mesh` plugin (hq ADR 0216): the skills, subagents, commands and output styles registered for every node and for this one, offered in a session as `nox-mesh:<name>` — and this module's binary (`guard/guard`, its path kept so the home-fs helper's path does not move) run by the mesh's own SessionStart and Stop hooks, the plugin's only hooks (hq issue 340, ADR 0489). No PreToolUse hook: the guard on the agent's shell is removed (the operator, 2026-11-20). Replaced whole, staged beside and swapped in |
|
||||
|
||||
Under the operator's home: `~/.claude/.credentials.json`, only when the licence manager hands this node a
|
||||
subscription token; and what is registered at the **home** scope for this node — a skill, subagent,
|
||||
@@ -78,12 +77,12 @@ must see, a node that joins later included — kept, so it carries no secret eit
|
||||
|
||||
| what | how |
|
||||
|---|---|
|
||||
| what this node holds | the module's `holdings` state, one key for this node — the account, the kind, fingerprints and expiries, never a token — written at start and whenever the credentials file changes (hq ADR 0206) |
|
||||
| what this node holds | the module's `holdings` state, one key for this node — the account, the kind, fingerprints and expiries, never a token — written at start and whenever the credentials file changes (hq ADR 0206), and said whole as the event `holdings-changed` at start, on every change and again when the licence manager says `holdings.asked` (design 39 bus-and-switch §23) |
|
||||
| a person ran `/login` here | the credentials file gains a refresh token this module never writes; its next report shows it, and the licence manager asks `claude_code_grant` for it, giving its key — the one time a refresh token leaves the node, for the manager to adopt by refreshing it |
|
||||
| what this node should hold | the licence manager's `bindings` state, this node's key; on a newer generation this module asks `anthropic-licence-manager.current` for its token, sealed to the key it sends, and writes it access-token-only — so the agent here never refreshes. A node that was off reads its key when it is back |
|
||||
| what this node should hold | the licence manager's `binding.changed` naming this node, and at start (or a reconnect) its first view, `anthropic-licence-manager.bindings` with `consumer` this node; an event for another node is ignored. On a newer generation, at start, and on the vault's `secret-changed` for the licence it holds, this module asks `anthropic-licence-manager.current` which licence, and the vault's `get` for that licence's access token (`licence.<key>.access`), sealed to its own key, and writes it access-token-only — so the agent here never refreshes, and never receives a refresh token. A home holding no token is fetched again at the same generation (design 39 bus-and-switch §19, §22) |
|
||||
| an MCP server registered through this module before registering was refused (hq issue 340) | a key in the module's `servers` state — `all.<server>` for every node, `<node>.<server>` for one; every node watches it and renders what applies to it, a node's own entry over the one for every node. A node that joins later, or was off, reads the whole current set at start; unregistering is a delete. Nothing adds a key any more: a new tool server is the operator's, in the `mcp_servers` setting at the controller's terminal |
|
||||
| a proposed instruction section (hq ADR 0274) | a key in the module's `proposals` state per proposal, the ask published on `operator-channel` under this module's name, and the router's record of it read under that name alone; what a node approved is its own file, never the bus |
|
||||
| a session's question to the operator (hq ADR 0332) | a key `q-…` in the module's `questions` state per question, the ask published on `operator-channel` under this module's name, and the router's record of it read under that name alone; the router's word that it ended without a choice is kept on the question; two slot keys `slot-1`, `slot-2` name the questions open |
|
||||
| a session's question to the operator (hq ADR 0332) | a key `q-…` in the module's `questions` state per question, the ask published on `operator-channel` under this module's name, and the router's record of it read under that name alone; the router's word that it ended without a choice is kept on the question |
|
||||
| a project's memory (hq ADR 0343) | a key in the module's `memory` state per file — `<kind>.<project and path, base64url>`, `operator` for the operator's own homes and `agent` for an agent account's — holding the text, its digest, the digest of the version it replaced, the node and when it changed; a removal is a value marked removed, never a deleted key. Written at the revision read, so of two nodes writing one file only one wins. Every node of the same kind watches it. No event and no tool answer carries memory text |
|
||||
| the agent's configuration (hq ADR 0216) | a key in the module's `config` state per registration — `mesh.<kind>.<name>` for every node, `node.<node>.<kind>.<name>` for one, `home.<node>.<kind>.<name>` for one account's own directory — the item and its files in one value, at most 256 KiB. Every node watches it and renders what applies to it, a node item over a mesh item of the same kind and name. A hook, settings, or an item that would run something by itself is never taken, whenever it was put there (hq issue 340) |
|
||||
|
||||
@@ -126,32 +125,16 @@ The agent kept reaching for `ssh <machine> journalctl` while the service manager
|
||||
answered this is not placed) — at start, every ten minutes and on `claude_code_render`, keeps it in its
|
||||
state (`mesh-tools.json`), and renders it into `CLAUDE.md` as one row per seat or module: `<node>/<seat>.` and
|
||||
each verb with the commands it replaces. Generated, never written by hand: a verb that gains `replaces` is in
|
||||
the next render. Ordered by what the guard here refused most, then the machines' seats, the modules, the
|
||||
the next render. Ordered by the machines' seats, the modules, the
|
||||
mesh's seats; at most sixteen rows, the rest one `mesh_search` away.
|
||||
- **The guard.** A PreToolUse hook on every tool, judging `Bash`, `Edit`, `Write`, `MultiEdit` and `NotebookEdit` and letting the rest through (but see the account rule below): this module's own
|
||||
binary (`claude-code guard`), copied root's into the plugin with what it judges with (`guard.json`: the
|
||||
machines by name, domain and address, and the replaced commands), run by its path under `/etc/claude-code` so
|
||||
the session cannot change it. It refuses `ssh`, `scp`, `sftp`, `rsync`, `mosh` and `autossh` to a mesh machine
|
||||
(any `*.internal` name, a machine's name or a name under its domains, one of its addresses, as `ssh -G` reads
|
||||
the destination; a jump through one too), writing `/etc/hosts` or `/etc/resolv.conf`, and `HOSTALIASES` —
|
||||
naming the verb that does the job on that machine when one says it replaces the command, and otherwise that a
|
||||
missing tool is created in the module that owns it, never worked around. **Stated, not silent:** an ssh login
|
||||
as `git` is the forge's account, which runs nothing but git, and passes; a git remote is never an ssh command
|
||||
line anyway.
|
||||
- **The operator's override**: `MESH_GUARD_OVERRIDE=<why>`, exported in the operator's own shell before the
|
||||
session starts. It is read from the session's environment as the kernel kept it at exec
|
||||
(`/proc/<pid>/environ` of the agent's process), so nothing a session does — a command's `export`, a
|
||||
settings `env` key — can set it, and a command naming it is refused outright. Every override and every
|
||||
refusal is a line in the module's `guard.log`; an override that cannot be recorded is not honoured.
|
||||
`claude_code_guard` shows the rules, the data and the record.
|
||||
|
||||
It is a guard against the habit, not a sandbox: a command written to hide what it runs can hide it, and the
|
||||
record is how a habit that found a way round is seen.
|
||||
The rule under the table (never `ssh` to a mesh machine, never edit `/etc/hosts` or set `HOSTALIASES` for a
|
||||
mesh name) is the session's own: nothing on the agent's shell enforces it since the guard was removed (the
|
||||
operator, 2026-11-20).
|
||||
|
||||
## Tools
|
||||
|
||||
`claude_code_status`, `claude_code_render`, `claude_code_guard`, `claude_code_pull`, `claude_code_grant` (for the licence
|
||||
manager), `claude_code_mcp_list`, `claude_code_mcp_unregister` (this node by default; `nodes: "all"` or a list for
|
||||
`claude_code_status`, `claude_code_render`, `claude_code_pull` (asks the vault for this node's
|
||||
licence token now), `claude_code_grant` (for the licence manager), `claude_code_mcp_list`, `claude_code_mcp_unregister` (this node by default; `nodes: "all"` or a list for
|
||||
more).
|
||||
|
||||
**What a session runs or obeys is the operator's, at the controller's terminal** (hq issue 340). A hook, a
|
||||
@@ -233,11 +216,8 @@ key says waiting, chosen (the option, who answered, on which channel, when), end
|
||||
refused, cancelled, replaced), or not proven: the answer is read from the router's own record and held to the
|
||||
question as `questions` keeps it, so a question rewritten after it was asked, a warrant for another ask or a choice
|
||||
after expiry is never read as a choice. One call waits at most 60 s; the tool asks a session to read a question at
|
||||
most once a minute, and nothing enforces it. At most two questions wait at once, mesh-wide: each holds one of two
|
||||
slot keys in `questions`, taken by create or compare-and-set, so two nodes asking at once cannot both take the last,
|
||||
and a slot that cannot be read refuses the question. Questions alone never fill the router's three open asks, so one
|
||||
is left for a proposed instruction section while two are open. A question the router holds no record of, its refusal
|
||||
not heard here, keeps its slot and reads as waiting until it expires: the router may still take it from its queue,
|
||||
most once a minute, and nothing enforces it. No question is refused for how many wait: the router offers an asker at most three asks at once, holds the oldest back behind newer ones and offers it again when one ends (hq ADR 0666 §12). A question the router holds no record of, its refusal
|
||||
not heard here, reads as waiting until it expires: the router may still take it from its queue,
|
||||
and no cancel can overtake it there (the queue offers work again after an error, and a cancel of an ask not yet
|
||||
recorded is dropped); once expired, the router refuses it if taken late. A refused question keeps none of
|
||||
its words. An answer is the operator's word to the session and authorises nothing; a question never defaults.
|
||||
@@ -342,14 +322,6 @@ applied only while the agent's account holds it — and what this module placed
|
||||
goes: the access-token-only credentials file (one holding a refresh token is a login the person made, and
|
||||
stays, said), the home-scope items unless changed by hand, the key kept in the state.
|
||||
|
||||
**The guard there refuses every tool call of a session that does not run as the agent's account** —
|
||||
shell, file edits, reads and the mesh's tools alike — and no override lifts it: the operator's account may become root, and an
|
||||
agent started in it by habit must do nothing. Its record goes to the machine's journal
|
||||
(`journalctl -t mesh-claude-code-guard`), which the session's account may add to and not change;
|
||||
`claude_code_guard` reads it there, keeping only the lines the journal says the guard's own binary wrote. That
|
||||
is not proof: the account can run the guard's binary itself and add lines in the guard's words. What it
|
||||
cannot do is remove one.
|
||||
|
||||
**An API key is taken only from the agent's own home.** `claude_code_add_api_key` reads a file and removes it,
|
||||
and the runtime runs as the operator: a path anywhere would hand whoever calls it the runtime's credential or
|
||||
the operator's keys. So the file must lie under the home of the account the agent runs as, reached through no
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package main
|
||||
|
||||
// Where the agent runs as an account of its own (novox/hq ADR 0266): the home touched through the helper, the
|
||||
// licence that follows the account, what goes from the operator's home, and the guard that refuses a session
|
||||
// running as anyone else.
|
||||
// licence that follows the account, and what goes from the operator's home.
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@@ -11,7 +10,6 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -333,82 +331,6 @@ func TestNothingGoesWhereTheAgentRunsAsTheOperator(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// ---- the guard --------------------------------------------------------------------------------------------
|
||||
|
||||
func TestASessionNotRunningAsTheAgentsAccountDoesNothing(t *testing.T) {
|
||||
g := Guard{Data: GuardData{Node: "anchor", Agent: "agent"}}
|
||||
echo := HookInput{ToolName: "Bash", ToolInput: map[string]any{"command": "echo hello"}}
|
||||
v := g.JudgeAs("operator", echo)
|
||||
if !v.Refuse || v.Rule != "account" || v.Overridable || !strings.Contains(v.Message, "claude-agent") {
|
||||
t.Fatalf("a session as the operator is refused whatever it runs, with no override: %+v", v)
|
||||
}
|
||||
if v := g.JudgeAs("operator", HookInput{ToolName: "Write", ToolInput: map[string]any{"file_path": "/tmp/x"}}); !v.Refuse {
|
||||
t.Fatal("a file edit too")
|
||||
}
|
||||
if v := g.JudgeAs("agent", echo); v.Refuse {
|
||||
t.Fatalf("the agent's own account runs as before: %+v", v)
|
||||
}
|
||||
if v := (Guard{Data: GuardData{Node: "laptop"}}).JudgeAs("operator", echo); v.Refuse {
|
||||
t.Fatal("where agents run as the operator, the operator's session runs as before")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWhereTheAgentHasItsOwnAccountTheRecordIsTheJournal(t *testing.T) {
|
||||
p, _ := node(t, "anchor")
|
||||
if d := GuardDataOf(p, nil); d.Agent != "" || strings.HasPrefix(d.Log, JournalRecord) {
|
||||
t.Fatalf("as the operator: the module's own file; got %+v", d)
|
||||
}
|
||||
p.Agent, p.Operator = "agent", "operator"
|
||||
d := GuardDataOf(p, nil)
|
||||
if d.Agent != "agent" || d.Log != JournalRecord+GuardJournalID {
|
||||
t.Fatalf("as an account of its own: the journal, and the account named; got %+v", d)
|
||||
}
|
||||
|
||||
socket := filepath.Join(t.TempDir(), "journal")
|
||||
conn, err := net.ListenUnixgram("unixgram", &net.UnixAddr{Name: socket, Net: "unixgram"})
|
||||
if err != nil {
|
||||
t.Skipf("no unix datagram socket here: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
was := journalSocket
|
||||
journalSocket = socket
|
||||
t.Cleanup(func() { journalSocket = was })
|
||||
if err := RecordGuard(d.Log, GuardEntry{Decision: "refused", Rule: "account", Tool: "Bash", Command: "a\nb"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
buf := make([]byte, 4096)
|
||||
n, _, err := conn.ReadFrom(buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lines := strings.Split(strings.TrimSuffix(string(buf[:n]), "\n"), "\n")
|
||||
if len(lines) != 3 || lines[0] != "SYSLOG_IDENTIFIER="+GuardJournalID || !strings.HasPrefix(lines[2], "MESSAGE={") {
|
||||
t.Fatalf("one line per field, the entry as one line of JSON: %q", lines)
|
||||
}
|
||||
|
||||
readWas := readJournal
|
||||
t.Cleanup(func() { readJournal = readWas })
|
||||
readJournal = func(id string, last int) ([]byte, error) {
|
||||
if id != GuardJournalID {
|
||||
t.Fatalf("read %s", id)
|
||||
}
|
||||
msg, _ := json.Marshal(strings.TrimPrefix(lines[2], "MESSAGE="))
|
||||
forged, _ := json.Marshal(`{"decision":"overridden","rule":"ssh","tool":"Bash","command":"forged"}`)
|
||||
return []byte(`{"MESSAGE":` + string(msg) + `,"_EXE":"` + HelperPath() + `"}` + "\n" +
|
||||
`{"MESSAGE":` + string(forged) + `,"_EXE":"/usr/bin/logger"}` + "\n"), nil
|
||||
}
|
||||
if got := ReadGuardLog(d.Log, 10); len(got) != 1 || got[0].Rule != "account" || got[0].Command != "a\nb" {
|
||||
t.Fatalf("the record reads back from the journal: %+v", got)
|
||||
}
|
||||
if err := RecordGuard(JournalRecord+"x", GuardEntry{}); err != nil && !strings.Contains(err.Error(), "journal") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
journalSocket = filepath.Join(t.TempDir(), "absent")
|
||||
if err := RecordGuard(d.Log, GuardEntry{}); err == nil {
|
||||
t.Fatal("a journal that cannot be reached is an error, so an override is not honoured unrecorded")
|
||||
}
|
||||
}
|
||||
|
||||
// ---- an API key is taken only from the agent's own home ---------------------------------------------------
|
||||
|
||||
func TestAnAPIKeyFileOutsideTheAgentsHomeIsRefusedAndKept(t *testing.T) {
|
||||
@@ -439,31 +361,12 @@ func TestAnAPIKeyFileOutsideTheAgentsHomeIsRefusedAndKept(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheGuardIsAskedAboutEveryToolAndRefusesEachForAnotherAccount(t *testing.T) {
|
||||
if GuardMatcher != "*" {
|
||||
t.Fatalf("the guard is asked about every tool, got %q", GuardMatcher)
|
||||
}
|
||||
g := Guard{Data: GuardData{Node: "anchor", Agent: "agent"}}
|
||||
for _, tool := range []string{"Read", "Grep", "WebFetch", "mcp__mesh__mesh_call", "Task"} {
|
||||
if v := g.JudgeAs("operator", HookInput{ToolName: tool}); !v.Refuse {
|
||||
t.Fatalf("%s as another account is refused", tool)
|
||||
}
|
||||
if v := g.JudgeAs("agent", HookInput{ToolName: tool}); v.Refuse {
|
||||
t.Fatalf("%s as the agent's account passes", tool)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAMoveWritesTheAccountFileBesideTheToken(t *testing.T) {
|
||||
p, w := node(t, "anchor")
|
||||
writeFile(t, p.binding(), `{"licence":"personal","kind":"subscription","generation":4}`)
|
||||
ask := func(address string, args any) (json.RawMessage, error) {
|
||||
key := args.(map[string]any)["public_key"].(string)
|
||||
g, _ := json.Marshal(Grant{AccessToken: "at", ExpiresAt: now + 3_600_000})
|
||||
box, _ := Seal(string(g), key)
|
||||
return json.Marshal(Current{Licence: "personal", Kind: "subscription", Generation: 4, Sealed: &box,
|
||||
Identity: &Identity{AccountUUID: "u-1", EmailAddress: "operator@example.org"}})
|
||||
}
|
||||
var asked []string
|
||||
ask := vaulted(t, Current{Licence: "personal", Key: "personal", Kind: "subscription", Generation: 4,
|
||||
Identity: &Identity{AccountUUID: "u-1", EmailAddress: "operator@example.org"}}, "at", &asked)
|
||||
if _, err := OnBinding(p, &BindingState{Licence: "personal", Kind: "subscription", Generation: 4}, ask, writer(w)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
// claude-code's reading of which modules exist and of each placed module's manifest (design 47 G §196, §594, §614): the
|
||||
// catalog half, taken through meshcatalogclient's follower of the watched branches on the part's one views reader. A
|
||||
// module's manifest is the one its catalog graph entry carries whole (§443), never the indexer's state `modules` or
|
||||
// `manifests`.
|
||||
|
||||
import (
|
||||
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
)
|
||||
|
||||
// CatalogHalf is what this module reads of the catalog half.
|
||||
type CatalogHalf interface {
|
||||
// Entry is a module's entry at the trunk; an error while a watched branch has no first view yet, never "no module".
|
||||
Entry(module string) (catalog.CatalogEntry, bool, error)
|
||||
// Ready is nil once every watched branch has its first view (none-yet counting as one), else says what it waits for.
|
||||
Ready() error
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
|
||||
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
delivery "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
)
|
||||
|
||||
// wiringOf is a part's reader in miniature: it keeps the followers adopted, so a test starts them as the reader's Start does.
|
||||
type wiringOf struct {
|
||||
mu sync.Mutex
|
||||
fs []*views.Follower
|
||||
}
|
||||
|
||||
func (w *wiringOf) Adopt(f *views.Follower, _, _ string, _ func(context.Context, []byte) error) error {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
w.fs = append(w.fs, f)
|
||||
return nil
|
||||
}
|
||||
func (w *wiringOf) Drop(string, string) error { return nil }
|
||||
|
||||
// Design 47 G §196, §594: which modules exist, and the manifest of each, are the catalog half's; a branch with no first
|
||||
// view yet is an error naming it, never no module, and an entry's manifest is the one it carries whole.
|
||||
func TestCatalogHalfIsTheWatchedBranchesNewestRevision(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
answered := false
|
||||
var mu sync.Mutex
|
||||
call := func(key string, _ any) (json.RawMessage, error) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
switch key {
|
||||
case meshcatalogclient.RegistrationsVerb:
|
||||
return json.Marshal(catalog.RegistrationsAnswer{Registrations: []catalog.Registration{{Module: "m", Version: 1, Branches: []string{"main"}}}})
|
||||
case meshcatalogclient.SourceVerb:
|
||||
return json.Marshal(delivery.SourceAnswer{})
|
||||
case meshcatalogclient.RevisionVerb:
|
||||
if !answered {
|
||||
return nil, fmt.Errorf("the holder is away")
|
||||
}
|
||||
return json.Marshal(catalog.RevisionAnswer{Revision: &catalog.CatalogRevision{Revision: 1, Kind: meshcatalogclient.KindTrunk},
|
||||
Entries: []catalog.CatalogEntry{{Module: "pg", Manifest: map[string]any{"module": "pg"}}}})
|
||||
}
|
||||
return nil, fmt.Errorf("asked %s", key)
|
||||
}
|
||||
w := &wiringOf{}
|
||||
b, err := meshcatalogclient.NewBranches(w, call)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
h := b
|
||||
if _, _, err := h.Entry("pg"); err == nil || !strings.Contains(err.Error(), "no watched branch") {
|
||||
t.Fatalf("before the registrations: %v, want an error naming the missing branch", err)
|
||||
}
|
||||
go func() { _ = w.fs[0].Start(ctx) }() // the registrations
|
||||
for len(b.Followed()) == 0 {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
t.Fatal("the registrations' branch is never followed")
|
||||
case <-time.After(10 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
if _, _, err := h.Entry("pg"); err == nil || !strings.Contains(err.Error(), "no first view of main") {
|
||||
t.Fatalf("with no first view: %v, want an error naming the branch, never no module", err)
|
||||
}
|
||||
mu.Lock()
|
||||
answered = true
|
||||
mu.Unlock()
|
||||
c, _ := b.Catalog("main")
|
||||
if err := c.Start(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, ok, err := h.Entry("pg")
|
||||
if err != nil || !ok || got.Manifest["module"] != "pg" || h.Ready() != nil {
|
||||
t.Fatalf("with the first view: %v, %v, %v", got, ok, err)
|
||||
}
|
||||
if _, ok, err := h.Entry("absent"); ok || err != nil {
|
||||
t.Fatalf("a module the half does not list: %v, %v", ok, err)
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
)
|
||||
|
||||
var now = time.Now().UnixMilli()
|
||||
@@ -282,31 +287,52 @@ func TestTheGrantAnswersOnlyAWaitingLoginSealedToTheManagersKey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// seat answers `current` as the manager does: the grant sealed to the key the node sent.
|
||||
// seat answers `current` as the manager does, the licence and its key and never a token, and `get` as the vault does:
|
||||
// the licence's access secret sealed to the key the node sent (design 39 bus-and-switch §18, §19).
|
||||
func seat(t *testing.T, licence, token string, gen int64, asked *[]string) Ask {
|
||||
return vaulted(t, Current{Licence: licence, Key: licence, Kind: "subscription", Generation: gen}, token, asked)
|
||||
}
|
||||
|
||||
// vaulted answers `current` with c and the vault's `get` of c's access secret with token.
|
||||
func vaulted(t *testing.T, c Current, token string, asked *[]string) Ask {
|
||||
return func(address string, args any) (json.RawMessage, error) {
|
||||
*asked = append(*asked, address)
|
||||
key := args.(map[string]any)["public_key"].(string)
|
||||
g, _ := json.Marshal(Grant{AccessToken: token, ExpiresAt: now + 3_600_000})
|
||||
box, err := Seal(string(g), key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
a := args.(map[string]any)
|
||||
switch address {
|
||||
case SeatVerb("current"):
|
||||
if _, sent := a["public_key"]; sent || a["consumer"] == nil {
|
||||
t.Fatalf("current asked with %v", a)
|
||||
}
|
||||
return json.Marshal(c)
|
||||
case VaultGet:
|
||||
if a["name"] != AccessSecret(c.Key) {
|
||||
t.Fatalf("the vault asked for %v", a["name"])
|
||||
}
|
||||
// Each answer a little later, as each rotation hands a later token.
|
||||
g, _ := json.Marshal(Grant{AccessToken: token, ExpiresAt: now + 3_600_000 + int64(len(*asked))*1000})
|
||||
box, err := Seal(string(g), a["public_key"].(string))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return json.Marshal(map[string]any{"name": a["name"], "generation": 1, "sealed": box})
|
||||
}
|
||||
return json.Marshal(Current{Licence: licence, Kind: "subscription", Generation: gen, Sealed: &box})
|
||||
t.Fatalf("asked %s", address)
|
||||
return nil, nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestANewerGenerationFetchesTheTokenOnceByTheSeatsVerb(t *testing.T) {
|
||||
// design 39 bus-and-switch §19: a newer generation asks the seat which licence, and the vault for its token.
|
||||
func TestANewerGenerationFetchesTheTokenOnceFromTheVault(t *testing.T) {
|
||||
p, w := node(t, "laptop")
|
||||
var asked []string
|
||||
ask := seat(t, "personal", "at-1", 3, &asked)
|
||||
if _, err := OnBinding(p, &BindingState{Licence: "personal", Kind: "subscription", Generation: 3}, ask, writer(w)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(asked) != 1 || asked[0] != "seat:anthropic-licence-manager.current" || creds(t, p)["accessToken"] != "at-1" {
|
||||
if len(asked) != 2 || asked[0] != SeatVerb("current") || asked[1] != VaultGet || creds(t, p)["accessToken"] != "at-1" {
|
||||
t.Fatalf("asked %v, credentials %v", asked, creds(t, p))
|
||||
}
|
||||
if done, _ := OnBinding(p, &BindingState{Licence: "personal", Kind: "subscription", Generation: 3}, ask, writer(w)); done != "" || len(asked) != 1 {
|
||||
if done, _ := OnBinding(p, &BindingState{Licence: "personal", Kind: "subscription", Generation: 3}, ask, writer(w)); done != "" || len(asked) != 2 {
|
||||
t.Fatal("an equal generation asked again")
|
||||
}
|
||||
if HoldingsOf(p).Generation != 3 || w["managed-mcp.json"] == "" {
|
||||
@@ -333,18 +359,128 @@ func TestAnAppliedBindingSaysWhyAndWho(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheTokenANodeIsHandedReplacesALoginsGrantAndLeavesNoRefreshToken(t *testing.T) {
|
||||
p, w := node(t, "laptop")
|
||||
writeFile(t, p.credentials(), `{"claudeAiOauth":{"accessToken":"at-old","refreshToken":"rt-spent","expiresAt":`+
|
||||
strings.TrimSpace(string(mustJSON(now+7_200_000)))+`}}`)
|
||||
var asked []string
|
||||
out, err := Pull(p, seat(t, "personal", "at-new", 1, &asked), writer(w))
|
||||
if err != nil || out["applied"] != true {
|
||||
t.Fatalf("%v %v", out, err)
|
||||
}
|
||||
c := creds(t, p)
|
||||
if c["accessToken"] != "at-new" || c["refreshToken"] != nil || HoldingsOf(p).Refresh.Present {
|
||||
t.Fatalf("%v", c)
|
||||
// ADR 0206, ADR 0209, design 39 §6 "the latest login wins": a login in the file that was never handed to the manager
|
||||
// is newer than every lease, made while the tool runner was stopped (the start's first view) or while it runs (a
|
||||
// binding or the vault's change heard before the manager asked claude_code_grant). No path writes over it; once
|
||||
// claude_code_grant handed it, its successor replaces it and leaves no refresh token, and a file holding no login is
|
||||
// restored as before.
|
||||
func TestALoginNotHandedIsNeverWrittenOverAndItsSuccessorReplacesIt(t *testing.T) {
|
||||
login := `{"claudeAiOauth":{"accessToken":"at-login","refreshToken":"rt-login","expiresAt":` +
|
||||
strings.TrimSpace(string(mustJSON(now+7_200_000))) + `,"refreshTokenExpiresAt":9000},"other":1}`
|
||||
lease := `{"claudeAiOauth":{"accessToken":"at-lease","expiresAt":1},"other":1}`
|
||||
for _, c := range []struct {
|
||||
name, file string
|
||||
handed bool
|
||||
offeredRTE int64 // the refresh token expiry the grant offered carries, 0 for none
|
||||
offeredExp int64 // its access expiry, 0 for an hour from now
|
||||
heard func(p Paths, ask Ask, w WriteManaged) (string, error)
|
||||
want string // the access token the file holds after
|
||||
account string // the email address the agent's account file names, "" for none
|
||||
reason string // what the journal line says, "" for nothing
|
||||
}{
|
||||
{"the start's first view, a login made while stopped", login, false, 0, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return FirstBinding(p, ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"binding.changed heard while it runs, before the grant was asked", login, false, 0, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"the vault's change heard while it runs, before the grant was asked", login, false, 0, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnSecretChanged(p, []byte(`{"name":"licence.personal.access","generation":5}`), ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"a login handed, met by its refresh: same lineage, later access expiry (H-A-139)", login, true, 9000, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-new", "", ""},
|
||||
{"a login handed, met by a newer lineage (H-A-138)", login, true, 200_000_000, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-new", "", ""},
|
||||
{"a login handed, met by the same lineage with an earlier access expiry (H-A-139)", login, true, 9000, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"a login handed, met by the same lineage with an equal access expiry (H-A-139)", login, true, 9000, now + 7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"a login handed, met by an older lineage (H-A-138)", login, true, -100_000_000, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"a login handed, met by a lease with no lineage mark (H-A-138)", login, true, 0, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "", ""},
|
||||
{"not handed, the same account, a lease that succeeds it: the latest login wins (H-A-140)", login, false, 9000, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-new", "personal", "login superseded"},
|
||||
{"not handed, the same account, a lease older than it (H-A-140)", login, false, 9000, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "personal", ""},
|
||||
{"not handed, another account, a newer lease (H-A-140)", login, false, 200_000_000, now + 3*7_200_000, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}`), ask, w)
|
||||
}, "at-login", "someone@example.org", ""},
|
||||
{"a file holding the lease, older than the one handed", lease, false, 0, 0, func(p Paths, ask Ask, w WriteManaged) (string, error) {
|
||||
return FirstBinding(p, ask, w)
|
||||
}, "at-new", "", ""},
|
||||
} {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
p, w := node(t, "laptop")
|
||||
writeFile(t, p.binding(), `{"licence":"personal","key":"personal","kind":"subscription","generation":2}`+"\n")
|
||||
writeFile(t, p.credentials(), c.file)
|
||||
if c.account != "" {
|
||||
writeFile(t, p.account(), `{"oauthAccount":{"accountUuid":"u-1","emailAddress":"`+c.account+`"}}`)
|
||||
}
|
||||
if c.handed {
|
||||
manager, _ := GenerateKeyPair()
|
||||
if a, err := GrantFor(p, manager.PublicKey); err != nil || a.Sealed == nil {
|
||||
t.Fatalf("the grant: %+v %v", a, err)
|
||||
}
|
||||
}
|
||||
var asked []string
|
||||
current := seat(t, "personal", "at-new", 9, &asked)
|
||||
ask := func(address string, args any) (json.RawMessage, error) {
|
||||
if address == SeatVerb("bindings") {
|
||||
return json.RawMessage(`[{"consumer":"laptop","licence":"personal","key":"personal","kind":"subscription","generation":9}]`), nil
|
||||
}
|
||||
if address == VaultGet && (c.offeredRTE != 0 || c.offeredExp != 0) {
|
||||
exp := c.offeredExp
|
||||
if exp == 0 {
|
||||
exp = now + 3_600_000
|
||||
}
|
||||
g := Grant{AccessToken: "at-new", ExpiresAt: exp}
|
||||
if c.offeredRTE != 0 {
|
||||
g.RefreshTokenExpiresAt = &c.offeredRTE
|
||||
}
|
||||
gj, _ := json.Marshal(g)
|
||||
box, err := Seal(string(gj), args.(map[string]any)["public_key"].(string))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return json.Marshal(map[string]any{"name": args.(map[string]any)["name"], "generation": 1, "sealed": box})
|
||||
}
|
||||
return current(address, args)
|
||||
}
|
||||
before, _ := os.ReadFile(p.credentials())
|
||||
line, err := c.heard(p, ask, writer(w))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(line, c.reason) {
|
||||
t.Fatalf("the line %s lacks %q", line, c.reason)
|
||||
}
|
||||
got := creds(t, p)
|
||||
if got["accessToken"] != c.want {
|
||||
t.Fatalf("the file holds %v, want %s", got["accessToken"], c.want)
|
||||
}
|
||||
after, _ := os.ReadFile(p.credentials())
|
||||
if c.want == "at-login" {
|
||||
if string(after) != string(before) || !HoldingsOf(p).Refresh.Present {
|
||||
t.Fatalf("a login not handed was changed: %s", after)
|
||||
}
|
||||
return
|
||||
}
|
||||
// Restored: no refresh token survives (design 14, design 36 §5), the CLI's other keys stay.
|
||||
var whole map[string]any
|
||||
_ = json.Unmarshal(after, &whole)
|
||||
if got["refreshToken"] != nil || HoldingsOf(p).Refresh.Present || whole["other"] == nil {
|
||||
t.Fatalf("restored %s", after)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -497,30 +633,107 @@ func TestAnAPIKeyIsHandedOverSealedAndItsFileRemoved(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A manager whose store was rebuilt counts generations from one again (novox/hq issue 243): a binding with a
|
||||
// lower generation than the one applied is still a binding to apply, and only the one applied is skipped.
|
||||
func TestALowerGenerationAfterTheManagerWasRebuiltIsStillApplied(t *testing.T) {
|
||||
// design 39 bus-and-switch §19, B11: only a newer generation than the one applied is fetched; a lower one is not, and
|
||||
// the vault's secret-changed for the licence held asks its token again, for another secret nothing.
|
||||
func TestTheTokenIsAskedOnANewerGenerationAndOnTheVaultsChange(t *testing.T) {
|
||||
p, w := node(t, "laptop")
|
||||
var asked []string
|
||||
if _, err := OnBinding(p, &BindingState{Licence: "personal", Kind: "subscription", Generation: 16},
|
||||
seat(t, "personal", "at-old", 16, &asked), writer(w)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The rotation that came with it: a later token, as a refresh hands one over.
|
||||
later := func(address string, args any) (json.RawMessage, error) {
|
||||
asked = append(asked, address)
|
||||
g, _ := json.Marshal(Grant{AccessToken: "at-new", ExpiresAt: now + 7_200_000})
|
||||
box, err := Seal(string(g), args.(map[string]any)["public_key"].(string))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return json.Marshal(Current{Licence: "personal", Kind: "subscription", Generation: 3, Sealed: &box})
|
||||
if done, err := OnBinding(p, &BindingState{Licence: "personal", Kind: "subscription", Generation: 3},
|
||||
seat(t, "personal", "at-low", 3, &asked), writer(w)); err != nil || done != "" || len(asked) != 2 {
|
||||
t.Fatalf("a lower generation was fetched: %q %v, asked %v", done, err, asked)
|
||||
}
|
||||
if _, err := OnBinding(p, &BindingState{Licence: "personal", Kind: "subscription", Generation: 3},
|
||||
later, writer(w)); err != nil {
|
||||
rotated := seat(t, "personal", "at-new", 16, &asked)
|
||||
for _, other := range []string{`{"name":"licence.personal.grant","generation":5}`, `{"name":"licence.other.access","generation":5}`} {
|
||||
if done, err := OnSecretChanged(p, []byte(other), rotated, writer(w)); err != nil || done != "" || len(asked) != 2 {
|
||||
t.Fatalf("%s asked again: %q %v, asked %v", other, done, err, asked)
|
||||
}
|
||||
}
|
||||
if _, err := OnSecretChanged(p, []byte(`{"name":"licence.personal.access","generation":5,"by":"x","at":"2026-11-11T00:00:00Z"}`),
|
||||
rotated, writer(w)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(asked) != 2 || creds(t, p)["accessToken"] != "at-new" || HoldingsOf(p).Generation != 3 {
|
||||
t.Fatalf("asked %v, credentials %v: a lower generation was ignored", asked, creds(t, p))
|
||||
if len(asked) != 4 || asked[3] != VaultGet || creds(t, p)["accessToken"] != "at-new" || HoldingsOf(p).Generation != 16 {
|
||||
t.Fatalf("asked %v, credentials %v: the vault's change was not applied", asked, creds(t, p))
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §22/AC1, B22: binding.changed, decoded from a copy of the manager's form, is acted on only
|
||||
// when it names this node's consumer; an event for another consumer is ignored.
|
||||
func TestAnEventForAnotherConsumerIsIgnored(t *testing.T) {
|
||||
p, w := node(t, "server")
|
||||
var asked []string
|
||||
ask := seat(t, "api", "at-3", 3, &asked)
|
||||
said, err := os.ReadFile("testdata/binding-changed.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if done, err := OnBindingChanged(p, said, ask, writer(w)); err != nil || done != "" || len(asked) != 0 {
|
||||
t.Fatalf("an event for laptop was acted on by server: %q %v, asked %v", done, err, asked)
|
||||
}
|
||||
p, w = node(t, "laptop")
|
||||
if done, err := OnBindingChanged(p, said, ask, writer(w)); err != nil || !strings.Contains(done, `"generation":3`) || len(asked) != 2 {
|
||||
t.Fatalf("laptop's own event: %q %v, asked %v", done, err, asked)
|
||||
}
|
||||
if done, _ := OnBindingChanged(p, []byte(`{"consumer":"laptop","licence":"","generation":3,"from":"api"}`), ask, writer(w)); !strings.Contains(done, "released") {
|
||||
t.Fatalf("a release said %q", done)
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §22, B10: at its start the module asks the seat's bindings for its own consumer and applies
|
||||
// that first view; its manifest consumes binding.changed and no longer reads the manager's state.
|
||||
func TestTheFirstViewOfTheBindingIsApplied(t *testing.T) {
|
||||
p, w := node(t, "laptop")
|
||||
var asked []string
|
||||
current := vaulted(t, Current{Licence: "api", Key: "api", Kind: "subscription", Generation: 9}, "at-9", &asked)
|
||||
ask := func(address string, args any) (json.RawMessage, error) {
|
||||
if address == SeatVerb("bindings") {
|
||||
asked = append(asked, address)
|
||||
if args.(map[string]any)["consumer"] != "laptop" {
|
||||
t.Fatalf("bindings asked with %v", args)
|
||||
}
|
||||
return json.RawMessage(`[{"consumer":"laptop","licence":"api","key":"api","kind":"api-key","generation":9,"why":"carried over","by":"claude-licence-manager"}]`), nil
|
||||
}
|
||||
return current(address, args)
|
||||
}
|
||||
if done, err := FirstBinding(p, ask, writer(w)); err != nil || !strings.Contains(done, `"generation":9`) {
|
||||
t.Fatalf("the first view: %q %v, asked %v", done, err, asked)
|
||||
}
|
||||
if len(asked) != 3 || asked[0] != SeatVerb("bindings") || creds(t, p)["accessToken"] != "at-9" {
|
||||
t.Fatalf("asked %v, credentials %v", asked, creds(t, p))
|
||||
}
|
||||
// §19: at every start the vault is asked, though the binding is the one applied.
|
||||
if _, err := FirstBinding(p, ask, writer(w)); err != nil || len(asked) != 6 || asked[5] != VaultGet {
|
||||
t.Fatalf("a start asked %v", asked)
|
||||
}
|
||||
// A-134: the first view is the follower the module's views reader adopts on binding.changed.<consumer>; its answer
|
||||
// applies the binding as FirstBinding does.
|
||||
r := &views.Reader{Module: Module}
|
||||
f := bindingsFollower(p, ask, writer(w), func(done string, err error) {
|
||||
if err != nil || !strings.Contains(done, `"generation":9`) {
|
||||
t.Errorf("the adopted first view applied %q, %v", done, err)
|
||||
}
|
||||
})
|
||||
if err := r.Adopt(f, Manager, BindingChangedEvent, func(context.Context, []byte) error { return nil }); err != nil || !r.Follows(Manager+".bindings") {
|
||||
t.Fatalf("the bindings follower is not adopted: %v", err)
|
||||
}
|
||||
if err := f.Start(context.Background()); err != nil || len(asked) != 9 {
|
||||
t.Fatalf("the adopted first view: %v, asked %v", err, asked)
|
||||
}
|
||||
raw, _ := os.ReadFile("../../module.json")
|
||||
var m struct {
|
||||
Consumes, Invokes []string
|
||||
Reads []string
|
||||
}
|
||||
_ = json.Unmarshal(raw, &m)
|
||||
// The manager says each change as binding.changed.<consumer> (design 47 G A-134), and the bus consumer this module's
|
||||
// consumes is composed into hands it only what an entry names, so an entry naming binding.changed alone hears none.
|
||||
hears := slices.ContainsFunc(m.Consumes, func(c string) bool { return stdio.TopicMatches(c, BindingChanged+".g14") })
|
||||
if !hears || !slices.Contains(m.Consumes, SecretChanged) ||
|
||||
!slices.Contains(m.Invokes, SeatVerb("bindings")) || !slices.Contains(m.Invokes, VaultGet) || len(m.Reads) != 0 {
|
||||
t.Fatalf("the manifest consumes %v, invokes %v, reads %v", m.Consumes, m.Invokes, m.Reads)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -472,40 +472,27 @@ type PluginFile struct {
|
||||
From string `json:",omitempty"`
|
||||
}
|
||||
|
||||
// GuardDir is where the guard on the agent's shell lives in the plugin (novox/hq ADR 0245): its binary and
|
||||
// what it is given. Outside hooks/, so no registered hook's name can be it.
|
||||
// GuardDir is where this module's binary lives in the plugin, run by the SessionStart and Stop hooks and as the
|
||||
// home-fs helper (novox/hq ADR 0266). Outside hooks/, so no registered hook's name can be it. Its path is kept so the
|
||||
// helper's does not move; the guard on the agent's shell that once ran from it is removed (the operator,
|
||||
// 2026-11-20).
|
||||
const GuardDir = "guard"
|
||||
|
||||
// GuardFiles are the guard as the plugin carries it: the binary to copy, what it judges with, and — the same
|
||||
// binary again — the waits directory the SessionStart and Stop hooks are given (novox/hq ADR 0489, issues
|
||||
// 1744, 1743). Empty WaitsDir places no such hook.
|
||||
type GuardFiles struct {
|
||||
// HookFiles are what the plugin carries to run its hooks: the binary to copy and the waits directory the
|
||||
// SessionStart and Stop hooks are given (novox/hq ADR 0489, issues 1744, 1743). Empty WaitsDir places no hook.
|
||||
type HookFiles struct {
|
||||
Exe string
|
||||
Data GuardData
|
||||
WaitsDir string
|
||||
}
|
||||
|
||||
// GuardMatcher is what the guard is asked about: every tool. Its own rules judge the shell and the agent's file
|
||||
// edits and let every other tool through; the rule of the account agents run as (novox/hq ADR 0266) refuses
|
||||
// every tool of a session running as another, reading and the mesh's tools included, so that session can do
|
||||
// nothing at all.
|
||||
const GuardMatcher = "*"
|
||||
|
||||
// GuardCommand is the hook's command line: by its path under the managed directory, root's — never the
|
||||
// copy the agent may keep of a plugin, which the session could change.
|
||||
func GuardCommand() string {
|
||||
dir := filepath.Join(ManagedDir, MarketplaceDir, "plugins", Plugin, GuardDir)
|
||||
return `"` + filepath.Join(dir, "guard") + `" guard "` + filepath.Join(dir, "guard.json") + `"`
|
||||
}
|
||||
|
||||
// SessionStartCommand is the plugin's SessionStart hook: the guard's own binary, told the session's open
|
||||
// SessionStartCommand is the plugin's SessionStart hook: this module's binary, told the session's open
|
||||
// waits live under waitsDir (novox/hq ADR 0489 §3, issue 1744).
|
||||
func SessionStartCommand(waitsDir string) string {
|
||||
dir := filepath.Join(ManagedDir, MarketplaceDir, "plugins", Plugin, GuardDir)
|
||||
return `"` + filepath.Join(dir, "guard") + `" session-start "` + waitsDir + `"`
|
||||
}
|
||||
|
||||
// StopCommand is the plugin's Stop hook: the guard's own binary, refusing to end the turn while a wait under
|
||||
// StopCommand is the plugin's Stop hook: this module's binary, refusing to end the turn while a wait under
|
||||
// waitsDir is open and unwatched, or past its bound and untold (novox/hq ADR 0489 §3-§6, issue 1743).
|
||||
func StopCommand(waitsDir string) string {
|
||||
dir := filepath.Join(ManagedDir, MarketplaceDir, "plugins", Plugin, GuardDir)
|
||||
@@ -516,9 +503,9 @@ func StopCommand(waitsDir string) string {
|
||||
// over a mesh item of the same kind and name: the node's wins.
|
||||
func Marketplace(c Config) map[string]PluginFile { return MarketplaceWith(c, nil) }
|
||||
|
||||
// MarketplaceWith is the marketplace with the guard on the agent's shell, when there is one to place: its
|
||||
// hook first, before any registered one.
|
||||
func MarketplaceWith(c Config, guard *GuardFiles) map[string]PluginFile {
|
||||
// MarketplaceWith is the marketplace with the binary the plugin's hooks run, when there is one to place, and its
|
||||
// SessionStart and Stop hooks. No PreToolUse hook: the plugin judges no tool call.
|
||||
func MarketplaceWith(c Config, guard *HookFiles) map[string]PluginFile {
|
||||
root := "plugins/" + Plugin + "/"
|
||||
out := map[string]PluginFile{
|
||||
".claude-plugin/marketplace.json": {Content: jsonFile(map[string]any{
|
||||
@@ -547,9 +534,6 @@ func MarketplaceWith(c Config, guard *GuardFiles) map[string]PluginFile {
|
||||
hooks := map[string][]any{}
|
||||
if guard != nil && guard.Exe != "" {
|
||||
out[root+GuardDir+"/guard"] = PluginFile{From: guard.Exe, Executable: true}
|
||||
out[root+GuardDir+"/guard.json"] = PluginFile{Content: jsonFile(guard.Data)}
|
||||
hooks["PreToolUse"] = append(hooks["PreToolUse"], map[string]any{"matcher": GuardMatcher,
|
||||
"hooks": []any{map[string]any{"type": "command", "command": GuardCommand(), "timeout": 15}}})
|
||||
if guard.WaitsDir != "" {
|
||||
// A session's wait list (novox/hq ADR 0489): the SessionStart hook names the session's id and its open
|
||||
// waits (issue 1744); the Stop hook keeps the turn open while one is unwatched or untold (issue 1743).
|
||||
@@ -574,7 +558,7 @@ func MarketplaceWith(c Config, guard *GuardFiles) map[string]PluginFile {
|
||||
out[root+"output-styles/"+it.Name+".md"] = PluginFile{Content: it.Files[it.Name+".md"]}
|
||||
}
|
||||
// A hook is never rendered from a registration (novox/hq issue 340): the plugin's hooks are the mesh's own
|
||||
// guard alone, and any other is the operator's, in the managed settings set at the controller's terminal.
|
||||
// SessionStart and Stop hooks alone, and any other is the operator's, in the managed settings set at the controller's terminal.
|
||||
}
|
||||
if len(hooks) > 0 {
|
||||
out[root+"hooks/hooks.json"] = PluginFile{Content: jsonFile(map[string]any{"hooks": hooks})}
|
||||
|
||||
@@ -276,10 +276,12 @@ func TestTheManifestListsEveryToolServed(t *testing.T) {
|
||||
}
|
||||
_ = json.Unmarshal(raw, &m)
|
||||
// The hand-act seat's warranted and hand-act, the seat mesh-catalog's seat list (ADR 0635 §1), and the owners' read tools of the three followed states; no verb of
|
||||
// the controller, and no read of the indexer's or the assigner's state (ADR 0615 §1): their views are consumed.
|
||||
if !slices.Equal(m.Invokes, []string{"seat:mesh-hand-acts.warranted", "seat:mesh-hand-acts.hand-act", "seat:mesh-catalog.seat-definitions", ModulesTool, ManifestsTool, AssignmentsTool}) ||
|
||||
!slices.Equal(m.Reads, []string{"claude-licence-manager.bindings"}) ||
|
||||
!slices.Equal(m.Consumes, []string{"mesh-delivery-indexer.views.modules", "mesh-delivery-indexer.views.manifests", "mesh-delivery-assigner.views.assignments", "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed"}) {
|
||||
// the controller, and no read of the indexer's or the assigner's state (ADR 0615 §1): their views are consumed. The
|
||||
// licence binding is the seat's bindings and the manager's binding.changed, its token the vault's get and secret-changed (design 39 bus-and-switch §19, §22, B9); a
|
||||
// notice goes out on the operator channel's notify (design 47 G GR8).
|
||||
if !slices.Equal(m.Invokes, []string{"seat:mesh-hand-acts.warranted", "seat:mesh-hand-acts.hand-act", "seat:mesh-catalog.seat-definitions", RevisionVerb, RegistrationsVerb, SourceVerb, AssignmentsTool, SeatVerb("bindings"), SeatVerb("current"), VaultGet, "seat:operator-channel.notify"}) ||
|
||||
len(m.Reads) != 0 ||
|
||||
!slices.Equal(m.Consumes, []string{"mesh-catalog.catalog-revision", "mesh-catalog.registration-changed", "mesh-delivery.source-moved", "mesh-delivery-assigner.views.assignments", "mesh-catalog.seat-definition-changed", "mesh-catalog.seat-definition-removed", "mesh-conditions.findings-asked", HoldingsAsked, BindingChanged + ".*", SecretChanged}) {
|
||||
t.Errorf("invokes = %v, reads = %v, consumes = %v", m.Invokes, m.Reads, m.Consumes)
|
||||
}
|
||||
if src, _ := os.ReadFile("proposals.go"); !strings.Contains(string(src), `"seat:mesh-hand-acts.warranted"`) ||
|
||||
|
||||
@@ -122,6 +122,36 @@ func HoldsLogin(c Credentials) bool {
|
||||
return rt != ""
|
||||
}
|
||||
|
||||
// LoginKept is the reason a grant is not written over a login (H-A-138).
|
||||
const LoginKept = "login kept"
|
||||
|
||||
// Succeeds says the offered grant replaces the login the file holds (H-A-138, H-A-139): its lineage is newer
|
||||
// (its refresh token's expiry later than the login's, beyond one generation), or it is of the same lineage
|
||||
// (sameGeneration) and its access token expires later than the login's, which only the manager's refresh of
|
||||
// that login yields. No lineage mark, an older lineage, or the same lineage with no later access expiry is the
|
||||
// older lease.
|
||||
func Succeeds(login Credentials, offered Grant) bool {
|
||||
if offered.RefreshTokenExpiresAt == nil {
|
||||
return false
|
||||
}
|
||||
held := GrantOf(login)
|
||||
if held == nil || held.RefreshTokenExpiresAt == nil {
|
||||
return true
|
||||
}
|
||||
if sameGeneration(held.RefreshTokenExpiresAt, offered.RefreshTokenExpiresAt) {
|
||||
return offered.ExpiresAt > held.ExpiresAt
|
||||
}
|
||||
return *offered.RefreshTokenExpiresAt > *held.RefreshTokenExpiresAt
|
||||
}
|
||||
|
||||
// LoginNotHanded says the file holds a login that was never handed to the manager: handed is the fingerprint
|
||||
// of the refresh token claude_code_grant last sealed, "" when none was. Such a login is newer than anything the
|
||||
// manager holds, so no grant may overwrite it before the manager adopts it (ADR 0206, ADR 0209).
|
||||
func LoginNotHanded(c Credentials, handed string) bool {
|
||||
rt := RefreshTokenOf(c)
|
||||
return rt != "" && Fingerprint(rt) != handed
|
||||
}
|
||||
|
||||
// RefreshTokenOf is the refresh token a login left, or "".
|
||||
func RefreshTokenOf(c Credentials) string {
|
||||
rt, _ := c.oauth()["refreshToken"].(string)
|
||||
|
||||
@@ -1,811 +0,0 @@
|
||||
package main
|
||||
|
||||
// The guard on the agent's shell (novox/hq ADR 0245): a PreToolUse hook every session on a machine of the
|
||||
// mesh runs before a shell command or a file edit, delivered in the module's plugin.
|
||||
//
|
||||
// It refuses three work-arounds, each with the mesh tool that does the job when one says it replaces the
|
||||
// command, and otherwise with the rule that a missing tool is created, never worked around:
|
||||
//
|
||||
// 1. **ssh to a mesh machine** — `ssh`, `scp`, `sftp`, `rsync`, `mosh` or `autossh` to a machine's name, a
|
||||
// name under its domains, any `*.internal` name, or one of its addresses; a jump through one too. The
|
||||
// destination is read as ssh itself reads it (`ssh -G`), so an alias in ~/.ssh/config is no way round.
|
||||
// **Stated, not silent**: an ssh login as `git` is the forge's account, which runs git and nothing
|
||||
// else — so `git push` over ssh, and `ssh -T git@<forge>`, reach no machine and pass. A git remote is
|
||||
// never an ssh command line anyway.
|
||||
// 2. **writing /etc/hosts or /etc/resolv.conf** — by redirect, tee, sed -i, an editor, cp/mv/install onto
|
||||
// it, or the agent's own Edit and Write tools: a mesh name is the mesh's resolvers' (ADR 0194), and a
|
||||
// machine's hosts file is its node-hostname seat's.
|
||||
// 3. **HOSTALIASES**, named anywhere in a command line.
|
||||
//
|
||||
// And it refuses any command that names the operator's override, so the agent never sets it (guard_run.go).
|
||||
//
|
||||
// The matching is on the command line as a shell would split it — quotes, separators, `$(…)`, `bash -c` —
|
||||
// and wrappers such as sudo, env and timeout are looked through. It is a guard against the habit of reaching
|
||||
// for ssh, not a sandbox: a command built to hide what it runs can hide it, and the record of what was
|
||||
// refused (the module's `guard.log`, read by `claude_code_guard`) is how a habit that found a way round is seen.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GuardData is what the hook is given: this machine, the mesh's machines, what replaces what, and where
|
||||
// its record is kept. Written beside the hook on every render, root's, so the session cannot change it.
|
||||
type GuardData struct {
|
||||
Node string `json:"node"`
|
||||
Machines []Machine `json:"machines"`
|
||||
Replacements []Replacement `json:"replacements"`
|
||||
Log string `json:"log"`
|
||||
// Agent is the account every session here must run as, where the machine names an agent account of its
|
||||
// own (novox/hq ADR 0266); empty where agents run as the operator.
|
||||
Agent string `json:"agent,omitempty"`
|
||||
}
|
||||
|
||||
// HookInput is what the agent hands a PreToolUse hook.
|
||||
type HookInput struct {
|
||||
ToolName string `json:"tool_name"`
|
||||
ToolInput map[string]any `json:"tool_input"`
|
||||
Cwd string `json:"cwd"`
|
||||
SessionID string `json:"session_id"`
|
||||
}
|
||||
|
||||
// Verdict is the guard's answer.
|
||||
type Verdict struct {
|
||||
Refuse bool `json:"refuse"`
|
||||
Rule string `json:"rule,omitempty"` // ssh | hosts-file | hostaliases | override-named | account
|
||||
Machine string `json:"machine,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
// Overridable is false for a refusal no override lifts: the agent naming the override itself.
|
||||
Overridable bool `json:"-"`
|
||||
// Calls are the tools the refusal named, by address without a machine: what the instructions' table
|
||||
// puts first, the tools the agent most reached round.
|
||||
Calls []string `json:"calls,omitempty"`
|
||||
}
|
||||
|
||||
// OverrideVar is the operator's override: set in the operator's own shell before the session starts,
|
||||
// with why as its value. Read from the session's own environment as it was started, never from what a
|
||||
// command or a setting put there (guard_run.go).
|
||||
const OverrideVar = "MESH_GUARD_OVERRIDE"
|
||||
|
||||
// ForgeUser is the forge's ssh account: it runs git, never a shell (stated in ADR 0245).
|
||||
const ForgeUser = "git"
|
||||
|
||||
// protectedFiles are the files a session never writes for a mesh name.
|
||||
var protectedFiles = map[string]bool{"/etc/hosts": true, "/etc/resolv.conf": true}
|
||||
|
||||
// SSHView is how ssh itself reads a destination with these options: the host it connects to, the user,
|
||||
// and the jumps. Empty host when ssh could not say.
|
||||
type SSHView func(options []string, destination string) (host, user string, jumps []string)
|
||||
|
||||
// Guard judges what a session is about to do.
|
||||
type Guard struct {
|
||||
Data GuardData
|
||||
// SSH reads a destination as ssh does (`ssh -G`); nil reads it from the command line alone.
|
||||
SSH SSHView
|
||||
// Resolve is a name's addresses, for a name that is none of the mesh's but may point at a machine.
|
||||
Resolve func(string) []string
|
||||
}
|
||||
|
||||
// JudgeAs is the verdict on one tool call of a session running as an account. **On a machine whose agents run
|
||||
// as an account of their own, a session running as any other does nothing** (novox/hq ADR 0266): the
|
||||
// operator's account there may become root without a person, and an agent started in it by habit would carry
|
||||
// that into every command. Refused whatever the command, and no override lifts it — the operator's own shell is
|
||||
// how the operator acts as themselves.
|
||||
func (g Guard) JudgeAs(account string, in HookInput) Verdict {
|
||||
if g.Data.Agent != "" && account != g.Data.Agent {
|
||||
return Verdict{Refuse: true, Rule: "account", Message: fmt.Sprintf("Refused: on %s agents run as %s, an "+
|
||||
"account that cannot become root without a person, and this session runs as %s (novox/hq ADR 0266). "+
|
||||
"No tool it calls here is let through. End it, and start the agent with `claude-agent`, which runs it as "+
|
||||
"%s; what needs the operator's own rights is the operator's to run in their own shell.",
|
||||
orThisMachine(g.Data.Node), g.Data.Agent, account, g.Data.Agent)}
|
||||
}
|
||||
return g.Judge(in)
|
||||
}
|
||||
|
||||
func orThisMachine(node string) string {
|
||||
if node == "" {
|
||||
return "this machine"
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
// Judge is the verdict on one tool call.
|
||||
func (g Guard) Judge(in HookInput) Verdict {
|
||||
switch in.ToolName {
|
||||
case "Bash":
|
||||
command, _ := in.ToolInput["command"].(string)
|
||||
return g.judgeLine(command, 0)
|
||||
case "Edit", "Write", "MultiEdit", "NotebookEdit":
|
||||
for _, key := range []string{"file_path", "notebook_path"} {
|
||||
if p, _ := in.ToolInput[key].(string); p != "" && protectedFiles[filepath.Clean(p)] {
|
||||
return g.hostsFile(filepath.Clean(p), in.ToolName+" "+filepath.Clean(p))
|
||||
}
|
||||
}
|
||||
}
|
||||
return Verdict{}
|
||||
}
|
||||
|
||||
func (g Guard) judgeLine(line string, depth int) Verdict {
|
||||
if depth > 4 {
|
||||
return Verdict{}
|
||||
}
|
||||
if strings.Contains(line, OverrideVar) {
|
||||
return Verdict{Refuse: true, Rule: "override-named", Message: fmt.Sprintf(
|
||||
"Refused by the mesh's guard (novox/hq ADR 0245): this command names %s, the operator's override. "+
|
||||
"It is the operator's alone, set in their own shell before a session starts; a session never sets, "+
|
||||
"reads or passes it.", OverrideVar)}
|
||||
}
|
||||
if strings.Contains(line, "HOSTALIASES") {
|
||||
return g.refuseLocal("hostaliases", "HOSTALIASES", "setting HOSTALIASES")
|
||||
}
|
||||
for _, words := range simpleCommands(line) {
|
||||
if v := g.judgeCommand(words, depth); v.Refuse {
|
||||
return v
|
||||
}
|
||||
// What a word runs in its own shell: `$(…)` and backticks inside a quoted word.
|
||||
for _, w := range words {
|
||||
for _, inner := range substitutions(w) {
|
||||
if v := g.judgeLine(inner, depth+1); v.Refuse {
|
||||
return v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Verdict{}
|
||||
}
|
||||
|
||||
// wrappers run the command after them; each with the options that take a value.
|
||||
var wrappers = map[string]string{
|
||||
"sudo": "ugCDhpTrt", "doas": "uC", "env": "uSC", "timeout": "sk", "nohup": "", "nice": "n", "ionice": "cnpP",
|
||||
"command": "", "exec": "a", "time": "fo", "stdbuf": "ioe", "xargs": "aEdIiLlnPs", "setsid": "", "unbuffer": "",
|
||||
"chronic": "", "flock": "wE", "torsocks": "", "proxychains": "f", "proxychains4": "f", "caffeinate": "",
|
||||
}
|
||||
|
||||
// shells run their -c argument as a command line.
|
||||
var shells = map[string]bool{"sh": true, "bash": true, "zsh": true, "dash": true, "ksh": true, "fish": true, "eval": true}
|
||||
|
||||
func (g Guard) judgeCommand(words []string, depth int) Verdict {
|
||||
words = unwrap(words)
|
||||
if len(words) == 0 {
|
||||
return Verdict{}
|
||||
}
|
||||
name := path.Base(words[0])
|
||||
args := words[1:]
|
||||
if shells[name] {
|
||||
if name == "eval" {
|
||||
return g.judgeLine(strings.Join(args, " "), depth+1)
|
||||
}
|
||||
for i, a := range args {
|
||||
if strings.HasPrefix(a, "-") && !strings.HasPrefix(a, "--") && strings.Contains(a, "c") && i+1 < len(args) {
|
||||
return g.judgeLine(args[i+1], depth+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
switch name {
|
||||
case "ssh", "autossh", "mosh", "slogin":
|
||||
return g.judgeSSH(name, args, strings.Join(words, " "))
|
||||
case "scp", "sftp", "rsync":
|
||||
return g.judgeCopy(name, args, strings.Join(words, " "))
|
||||
}
|
||||
if target := writesProtected(name, args); target != "" {
|
||||
return g.hostsFile(target, strings.Join(words, " "))
|
||||
}
|
||||
return Verdict{}
|
||||
}
|
||||
|
||||
// unwrap takes off what only runs the command: assignments before it, and wrappers with their options.
|
||||
func unwrap(words []string) []string {
|
||||
for len(words) > 0 {
|
||||
w := words[0]
|
||||
switch {
|
||||
case w == "!" || w == "{" || w == "}" || w == "then" || w == "do" || w == "else" || w == "if" || w == "while" ||
|
||||
w == "until" || w == "elif":
|
||||
words = words[1:]
|
||||
case isAssignment(w):
|
||||
words = words[1:]
|
||||
default:
|
||||
opts, wrapper := wrappers[path.Base(w)]
|
||||
if !wrapper {
|
||||
return words
|
||||
}
|
||||
base := path.Base(w)
|
||||
words = words[1:]
|
||||
for len(words) > 0 {
|
||||
a := words[0]
|
||||
if a == "--" {
|
||||
words = words[1:]
|
||||
break
|
||||
}
|
||||
if base == "env" && isAssignment(a) {
|
||||
words = words[1:]
|
||||
continue
|
||||
}
|
||||
if !strings.HasPrefix(a, "-") || a == "-" {
|
||||
break
|
||||
}
|
||||
words = words[1:]
|
||||
if strings.HasPrefix(a, "--") {
|
||||
continue
|
||||
}
|
||||
if f := a[len(a)-1:]; strings.Contains(opts, f) && len(words) > 0 {
|
||||
words = words[1:]
|
||||
}
|
||||
}
|
||||
// timeout's duration, nice's adjustment as a word: a number before the command.
|
||||
if (base == "timeout") && len(words) > 0 {
|
||||
words = words[1:]
|
||||
}
|
||||
}
|
||||
}
|
||||
return words
|
||||
}
|
||||
|
||||
func isAssignment(w string) bool {
|
||||
i := strings.Index(w, "=")
|
||||
if i <= 0 {
|
||||
return false
|
||||
}
|
||||
for _, r := range w[:i] {
|
||||
if !(r == '_' || r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z' || r >= '0' && r <= '9') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// sshOptionsWithValue are ssh's single-letter options that take a value.
|
||||
const sshOptionsWithValue = "BbcDEeFIiJLlmOoPpQRSWw"
|
||||
|
||||
func (g Guard) judgeSSH(name string, args []string, line string) Verdict {
|
||||
var options, jumps []string
|
||||
user := ""
|
||||
dest, rest := "", []string(nil)
|
||||
for i := 0; i < len(args); i++ {
|
||||
a := args[i]
|
||||
if a == "--" {
|
||||
if i+1 < len(args) {
|
||||
dest, rest = args[i+1], args[i+2:]
|
||||
}
|
||||
break
|
||||
}
|
||||
if name == "autossh" && strings.HasPrefix(a, "-M") {
|
||||
if a == "-M" {
|
||||
i++ // autossh's own: the monitoring port
|
||||
}
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(a, "-") && len(a) > 1 {
|
||||
options = append(options, a)
|
||||
if name == "mosh" || strings.HasPrefix(a, "--") {
|
||||
continue // mosh's own options are long, each with its value after `=`
|
||||
}
|
||||
for j := 1; j < len(a); j++ {
|
||||
if !strings.ContainsRune(sshOptionsWithValue, rune(a[j])) {
|
||||
continue
|
||||
}
|
||||
value := a[j+1:]
|
||||
if value == "" && i+1 < len(args) {
|
||||
i++
|
||||
value = args[i]
|
||||
options = append(options, value)
|
||||
}
|
||||
switch a[j] {
|
||||
case 'J':
|
||||
jumps = append(jumps, strings.Split(value, ",")...)
|
||||
case 'l':
|
||||
user = value
|
||||
case 'o':
|
||||
k, v, _ := strings.Cut(value, "=")
|
||||
switch strings.ToLower(strings.TrimSpace(k)) {
|
||||
case "proxyjump":
|
||||
jumps = append(jumps, strings.Split(v, ",")...)
|
||||
case "user":
|
||||
user = strings.TrimSpace(v)
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
continue
|
||||
}
|
||||
dest, rest = a, args[i+1:]
|
||||
break
|
||||
}
|
||||
if dest == "" {
|
||||
return Verdict{}
|
||||
}
|
||||
host, destUser := hostOf(dest)
|
||||
if destUser != "" {
|
||||
user = destUser
|
||||
}
|
||||
if g.SSH != nil && name != "mosh" {
|
||||
if h, u, j := g.SSH(options, dest); h != "" {
|
||||
host, user = h, u
|
||||
jumps = append(jumps, j...)
|
||||
}
|
||||
}
|
||||
remote := strings.Join(rest, " ")
|
||||
for _, jump := range jumps {
|
||||
jh, ju := hostOf(jump)
|
||||
if machine, ok := g.meshHost(jh); ok && ju != ForgeUser {
|
||||
return g.refuseSSH(machine, jh, remote, line)
|
||||
}
|
||||
}
|
||||
if user == ForgeUser {
|
||||
return Verdict{} // the forge's account: git and nothing else (stated in ADR 0245)
|
||||
}
|
||||
if machine, ok := g.meshHost(host); ok {
|
||||
return g.refuseSSH(machine, host, remote, line)
|
||||
}
|
||||
return Verdict{}
|
||||
}
|
||||
|
||||
// judgeCopy reads scp's, sftp's and rsync's remote paths, `[user@]host:path` or `scp://[user@]host/…`.
|
||||
func (g Guard) judgeCopy(name string, args []string, line string) Verdict {
|
||||
for i := 0; i < len(args); i++ {
|
||||
a := args[i]
|
||||
if strings.HasPrefix(a, "-") {
|
||||
if name != "rsync" && len(a) == 2 && strings.ContainsRune("cFiJloPSXBRD", rune(a[1])) {
|
||||
i++
|
||||
}
|
||||
if name == "rsync" && (a == "-e" || a == "--rsh") {
|
||||
i++
|
||||
}
|
||||
continue
|
||||
}
|
||||
var host, user string
|
||||
if strings.Contains(a, "://") {
|
||||
host, user = hostOf(a)
|
||||
} else if colon := strings.Index(a, ":"); colon > 0 && !strings.Contains(a[:colon], "/") {
|
||||
host, user = hostOf(a[:colon])
|
||||
} else if name == "sftp" {
|
||||
host, user = hostOf(a)
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
if user == ForgeUser {
|
||||
continue
|
||||
}
|
||||
if g.SSH != nil {
|
||||
if h, u, _ := g.SSH(nil, host); h != "" {
|
||||
host = h
|
||||
if u == ForgeUser && user == "" {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
if machine, ok := g.meshHost(host); ok {
|
||||
return g.refuseSSH(machine, host, "", line)
|
||||
}
|
||||
}
|
||||
return Verdict{}
|
||||
}
|
||||
|
||||
// hostOf reads `[user@]host[:port]`, `ssh://[user@]host[:port]/…` or `[v6]`.
|
||||
func hostOf(dest string) (host, user string) {
|
||||
if strings.Contains(dest, "://") {
|
||||
if u, err := url.Parse(dest); err == nil {
|
||||
return strings.ToLower(u.Hostname()), u.User.Username()
|
||||
}
|
||||
}
|
||||
if at := strings.LastIndex(dest, "@"); at >= 0 {
|
||||
user, dest = dest[:at], dest[at+1:]
|
||||
}
|
||||
if strings.HasPrefix(dest, "[") {
|
||||
if end := strings.Index(dest, "]"); end > 0 {
|
||||
return strings.ToLower(dest[1:end]), user
|
||||
}
|
||||
}
|
||||
if h, _, err := net.SplitHostPort(dest); err == nil && strings.Count(dest, ":") == 1 {
|
||||
dest = h
|
||||
}
|
||||
return strings.TrimSuffix(strings.ToLower(dest), "."), user
|
||||
}
|
||||
|
||||
// meshHost says whether a host is one of the mesh's machines, and which when it can tell.
|
||||
func (g Guard) meshHost(host string) (string, bool) {
|
||||
host = strings.TrimSuffix(strings.ToLower(strings.Trim(host, "[]")), ".")
|
||||
if host == "" {
|
||||
return "", false
|
||||
}
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
for _, m := range g.Data.Machines {
|
||||
for _, a := range m.Addresses {
|
||||
if b := net.ParseIP(a); b != nil && b.Equal(ip) {
|
||||
return m.Name, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
for _, m := range g.Data.Machines {
|
||||
if host == strings.ToLower(m.Name) {
|
||||
return m.Name, true
|
||||
}
|
||||
for _, d := range m.Domains {
|
||||
if host == d || strings.HasSuffix(host, "."+d) {
|
||||
return m.Name, true
|
||||
}
|
||||
}
|
||||
}
|
||||
if strings.HasSuffix(host, InternalSuffix) {
|
||||
return "", true
|
||||
}
|
||||
if g.Resolve != nil && !strings.Contains(host, "/") {
|
||||
for _, a := range g.Resolve(host) {
|
||||
if m, ok := g.meshHost(a); ok && net.ParseIP(a) != nil {
|
||||
return m, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// writesProtected is the protected file a command writes, or "".
|
||||
func writesProtected(name string, args []string) string {
|
||||
protected := func(a string) string {
|
||||
a = strings.Trim(a, `"'`)
|
||||
if strings.HasPrefix(a, "of=") {
|
||||
a = a[3:]
|
||||
}
|
||||
if protectedFiles[filepath.Clean(a)] {
|
||||
return filepath.Clean(a)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
// A redirect onto it, from any command.
|
||||
for i, a := range args {
|
||||
if (a == ">" || a == ">>") && i+1 < len(args) {
|
||||
if p := protected(args[i+1]); p != "" {
|
||||
return p
|
||||
}
|
||||
}
|
||||
}
|
||||
operands := func() []string {
|
||||
var out []string
|
||||
for i := 0; i < len(args); i++ {
|
||||
if args[i] == ">" || args[i] == ">>" || args[i] == "<" {
|
||||
i++
|
||||
continue
|
||||
}
|
||||
out = append(out, args[i])
|
||||
}
|
||||
return out
|
||||
}()
|
||||
switch name {
|
||||
case "tee", "vi", "vim", "nvim", "nano", "emacs", "ed", "ex", "micro", "hx", "helix", "kak", "gedit", "code",
|
||||
"truncate", "chattr", "chmod", "chown", "rm", "unlink", "shred", "dd", "sponge", "visudo", "vipw":
|
||||
for _, a := range operands {
|
||||
if p := protected(a); p != "" {
|
||||
return p
|
||||
}
|
||||
}
|
||||
case "sed", "perl", "ruby", "awk", "gawk":
|
||||
inPlace := false
|
||||
for _, a := range operands {
|
||||
switch {
|
||||
case name == "awk" || name == "gawk":
|
||||
inPlace = inPlace || a == "inplace"
|
||||
case strings.HasPrefix(a, "--in-place"):
|
||||
inPlace = true
|
||||
case strings.HasPrefix(a, "-") && !strings.HasPrefix(a, "--") && strings.Contains(a, "i"):
|
||||
inPlace = true
|
||||
}
|
||||
}
|
||||
if inPlace {
|
||||
for _, a := range operands {
|
||||
if p := protected(a); p != "" {
|
||||
return p
|
||||
}
|
||||
}
|
||||
}
|
||||
case "cp", "mv", "install", "ln", "rsync":
|
||||
var files []string
|
||||
for _, a := range operands {
|
||||
if !strings.HasPrefix(a, "-") {
|
||||
files = append(files, a)
|
||||
}
|
||||
}
|
||||
if len(files) > 1 {
|
||||
if p := protected(files[len(files)-1]); p != "" {
|
||||
return p
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// replacementsFor are the mesh's tools that replace a command, the most specific first, each address once.
|
||||
func (g Guard) replacementsFor(command string) []string {
|
||||
have := commandWordsOf(command)
|
||||
type found struct {
|
||||
address string
|
||||
weight int
|
||||
replace string
|
||||
}
|
||||
var all []found
|
||||
for _, r := range g.Data.Replacements {
|
||||
best := 0
|
||||
which := ""
|
||||
for _, c := range r.Replaces {
|
||||
want := commandWordsOf(c)
|
||||
if inOrderWords(want, have) && len(want) > best {
|
||||
best, which = len(want), c
|
||||
}
|
||||
}
|
||||
if best > 0 {
|
||||
all = append(all, found{r.Address, best, which})
|
||||
}
|
||||
}
|
||||
sort.SliceStable(all, func(i, j int) bool { return all[i].weight > all[j].weight })
|
||||
var out []string
|
||||
for _, f := range all {
|
||||
out = append(out, f.address+"\x00"+f.replace)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// callFor is how an address is called for a machine.
|
||||
func (g Guard) callFor(address, machine string) string {
|
||||
for _, r := range g.Data.Replacements {
|
||||
if r.Address != address || !r.NodeScoped {
|
||||
continue
|
||||
}
|
||||
on := machine
|
||||
if !r.Seat && len(r.On) > 0 && !containsString(r.On, machine) {
|
||||
on = r.On[0]
|
||||
}
|
||||
if on == "" {
|
||||
on = "<node>"
|
||||
}
|
||||
return on + "/" + address
|
||||
}
|
||||
return address
|
||||
}
|
||||
|
||||
func containsString(xs []string, s string) bool {
|
||||
for _, x := range xs {
|
||||
if x == s {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// exact is the replacement that names this exact command, or "".
|
||||
func (g Guard) exact(command string) string {
|
||||
for _, r := range g.Data.Replacements {
|
||||
for _, c := range r.Replaces {
|
||||
if c == command {
|
||||
return r.Address
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
const missingTool = "No mesh tool says it replaces this. Do not work around it: a missing tool is created in the module " +
|
||||
"that owns the thing, on the seat it occupies — say what is missing to the operator. First make sure with " +
|
||||
"`mesh_search` and the command's own words."
|
||||
|
||||
func (g Guard) refuseSSH(machine, host, remote, line string) Verdict {
|
||||
who := machine
|
||||
if who == "" {
|
||||
who = host + " (a mesh name)"
|
||||
}
|
||||
var b strings.Builder
|
||||
var named []string
|
||||
fmt.Fprintf(&b, "Refused by the mesh's guard (novox/hq ADR 0245): `%s` reaches %s round the mesh's tools.\n", cut(line, 160), who)
|
||||
switch {
|
||||
case strings.TrimSpace(remote) == "":
|
||||
target := machine
|
||||
if target == "" {
|
||||
target = "<machine>"
|
||||
}
|
||||
fmt.Fprintf(&b, "What a machine serves is `mesh_machine %s`; `mesh_search` with what you meant to run finds its tool.\n", target)
|
||||
default:
|
||||
calls := g.replacementsFor(remote)
|
||||
if len(calls) == 0 {
|
||||
b.WriteString(missingTool + "\n")
|
||||
break
|
||||
}
|
||||
b.WriteString("Call instead, through `mesh_call`:\n")
|
||||
for i, c := range calls {
|
||||
if i == 3 {
|
||||
break
|
||||
}
|
||||
address, replaces, _ := strings.Cut(c, "\x00")
|
||||
named = append(named, address)
|
||||
fmt.Fprintf(&b, " %s — replaces `%s`\n", g.callFor(address, machine), replaces)
|
||||
}
|
||||
}
|
||||
return Verdict{Refuse: true, Rule: "ssh", Machine: machine, Message: strings.TrimRight(b.String(), "\n"), Overridable: true,
|
||||
Calls: named}
|
||||
}
|
||||
|
||||
func (g Guard) refuseLocal(rule, replaced, what string) Verdict {
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, "Refused by the mesh's guard (novox/hq ADR 0245): %s is a work-around for a mesh name. "+
|
||||
"A mesh name is the mesh's resolvers' to answer, and a machine's own lines in /etc/hosts are its node-hostname seat's.\n", what)
|
||||
var named []string
|
||||
if address := g.exact(replaced); address != "" {
|
||||
named = append(named, address)
|
||||
fmt.Fprintf(&b, "Call instead, through `mesh_call`: %s — replaces `%s`", g.callFor(address, g.Data.Node), replaced)
|
||||
} else {
|
||||
b.WriteString(missingTool)
|
||||
}
|
||||
return Verdict{Refuse: true, Rule: rule, Machine: g.Data.Node, Message: b.String(), Overridable: true, Calls: named}
|
||||
}
|
||||
|
||||
func (g Guard) hostsFile(file, line string) Verdict {
|
||||
replaced := "edit " + file
|
||||
return g.refuseLocal("hosts-file", replaced, fmt.Sprintf("writing %s (`%s`)", file, cut(line, 120)))
|
||||
}
|
||||
|
||||
func cut(s string, n int) string {
|
||||
s = strings.Join(strings.Fields(s), " ")
|
||||
if r := []rune(s); len(r) > n {
|
||||
return string(r[:n-1]) + "…"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// commandWordsOf are a command line's words as matched against a replaced command (the mesh MCP server's search rule):
|
||||
// lower-cased, a program by path reduced to its name, sudo left out.
|
||||
func commandWordsOf(line string) []string {
|
||||
var out []string
|
||||
for i, w := range strings.Fields(strings.ToLower(line)) {
|
||||
if i == 0 || strings.HasPrefix(w, "/") {
|
||||
w = path.Base(w)
|
||||
}
|
||||
if w == "sudo" {
|
||||
continue
|
||||
}
|
||||
out = append(out, w)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func inOrderWords(want, have []string) bool {
|
||||
i := 0
|
||||
for _, h := range have {
|
||||
if i < len(want) && h == want[i] {
|
||||
i++
|
||||
}
|
||||
}
|
||||
return len(want) > 0 && i == len(want)
|
||||
}
|
||||
|
||||
// ---- the shell's words ------------------------------------------------------------------------------
|
||||
|
||||
// simpleCommands splits a command line as a shell would into its simple commands' words: quotes
|
||||
// removed, `;`, `&`, `|`, `&&`, `||`, newlines, parentheses and backticks between commands, `$(` opening
|
||||
// one; `>` and `>>` kept as words of their own, before what they redirect to.
|
||||
func simpleCommands(line string) [][]string {
|
||||
var out [][]string
|
||||
var words []string
|
||||
var cur strings.Builder
|
||||
inWord := false
|
||||
flushWord := func() {
|
||||
if inWord {
|
||||
words = append(words, cur.String())
|
||||
cur.Reset()
|
||||
inWord = false
|
||||
}
|
||||
}
|
||||
flushCommand := func() {
|
||||
flushWord()
|
||||
if len(words) > 0 {
|
||||
out = append(out, words)
|
||||
}
|
||||
words = nil
|
||||
}
|
||||
r := []rune(line)
|
||||
for i := 0; i < len(r); i++ {
|
||||
c := r[i]
|
||||
switch {
|
||||
case c == '\\' && i+1 < len(r):
|
||||
i++
|
||||
if r[i] != '\n' {
|
||||
cur.WriteRune(r[i])
|
||||
inWord = true
|
||||
}
|
||||
case c == '\'':
|
||||
inWord = true
|
||||
for i++; i < len(r) && r[i] != '\''; i++ {
|
||||
cur.WriteRune(r[i])
|
||||
}
|
||||
case c == '"':
|
||||
inWord = true
|
||||
for i++; i < len(r) && r[i] != '"'; i++ {
|
||||
if r[i] == '\\' && i+1 < len(r) && strings.ContainsRune("\"\\$`", r[i+1]) {
|
||||
i++
|
||||
}
|
||||
cur.WriteRune(r[i])
|
||||
}
|
||||
case c == '$' && i+1 < len(r) && r[i+1] == '(':
|
||||
flushCommand()
|
||||
i++
|
||||
case c == ';' || c == '&' || c == '|' || c == '\n' || c == '(' || c == ')' || c == '`':
|
||||
flushCommand()
|
||||
case c == '>' || c == '<':
|
||||
// A file descriptor before it (`2>`) is not a word.
|
||||
if inWord && isDigits(cur.String()) {
|
||||
cur.Reset()
|
||||
inWord = false
|
||||
}
|
||||
flushWord()
|
||||
op := string(c)
|
||||
if c == '>' && i+1 < len(r) && r[i+1] == '>' {
|
||||
op = ">>"
|
||||
i++
|
||||
}
|
||||
if i+1 < len(r) && (r[i+1] == '&' || r[i+1] == '|') {
|
||||
i++
|
||||
}
|
||||
words = append(words, op)
|
||||
case c == ' ' || c == '\t':
|
||||
flushWord()
|
||||
case c == '#' && !inWord:
|
||||
for i < len(r) && r[i] != '\n' {
|
||||
i++
|
||||
}
|
||||
flushCommand()
|
||||
default:
|
||||
cur.WriteRune(c)
|
||||
inWord = true
|
||||
}
|
||||
}
|
||||
flushCommand()
|
||||
return out
|
||||
}
|
||||
|
||||
func isDigits(s string) bool {
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
for _, r := range s {
|
||||
if r < '0' || r > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// substitutions are the command lines a quoted word runs in a shell of its own: `$(…)` and `…`.
|
||||
func substitutions(word string) []string {
|
||||
var out []string
|
||||
for {
|
||||
i := strings.Index(word, "$(")
|
||||
if i < 0 {
|
||||
break
|
||||
}
|
||||
rest := word[i+2:]
|
||||
depth, end := 1, len(rest)
|
||||
for j, c := range rest {
|
||||
if c == '(' {
|
||||
depth++
|
||||
} else if c == ')' {
|
||||
depth--
|
||||
if depth == 0 {
|
||||
end = j
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
out = append(out, rest[:end])
|
||||
if end >= len(rest) {
|
||||
break
|
||||
}
|
||||
word = rest[end+1:]
|
||||
}
|
||||
parts := strings.Split(word, "`")
|
||||
for i := 1; i < len(parts); i += 2 {
|
||||
out = append(out, parts[i])
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -1,315 +0,0 @@
|
||||
package main
|
||||
|
||||
// The guard as the hook runs it (novox/hq ADR 0245): `claude-code guard <guard.json>`, the tool call on
|
||||
// standard input, exit 2 with the reason on standard error to refuse — the agent reads it — and 0 to let it be.
|
||||
//
|
||||
// **The operator's override.** `MESH_GUARD_OVERRIDE=<why>` lets one session through a refusal — never the
|
||||
// refusal of a command naming the override itself — and only:
|
||||
//
|
||||
// - **from the session's environment as it was started**: read from /proc/<pid>/environ of the agent's own
|
||||
// process, which is fixed at exec. A command's `export`, an `env` key in a settings file, a variable the
|
||||
// session sets for its tools — none of them reaches it. A nested session the agent starts with the
|
||||
// variable is refused before it starts: the command names the override;
|
||||
// - **recorded**: every override is a line in the module's guard.log — or, where the agent runs as an account
|
||||
// of its own, in the machine's journal (novox/hq ADR 0266) — with why, the command and the machine; an
|
||||
// override that cannot be recorded is not honoured. Refusals are recorded the same way.
|
||||
//
|
||||
// A guard that fails — a fault in this code — exits 1, which the agent shows and does not block on: a guard
|
||||
// that refused every command would stop the session the operator needs to repair it.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// procRoot and parentPID are where the session's process is found; a test points them at its own.
|
||||
var (
|
||||
procRoot = "/proc"
|
||||
parentPID = os.Getppid
|
||||
)
|
||||
|
||||
// GuardLogMax is the size the guard's record grows to before it is kept once as guard.log.1.
|
||||
const GuardLogMax = 1 << 20
|
||||
|
||||
// GuardEntry is one line of the guard's record.
|
||||
type GuardEntry struct {
|
||||
At string `json:"at"`
|
||||
Node string `json:"node,omitempty"`
|
||||
Decision string `json:"decision"` // refused | overridden
|
||||
Rule string `json:"rule"`
|
||||
Machine string `json:"machine,omitempty"`
|
||||
Tool string `json:"tool"`
|
||||
Command string `json:"command"`
|
||||
Why string `json:"why,omitempty"`
|
||||
// Tools are the tools the refusal named instead.
|
||||
Tools []string `json:"tools,omitempty"`
|
||||
Session string `json:"session,omitempty"`
|
||||
}
|
||||
|
||||
// JournalRecord is the guard's record where the agent runs as an account of its own (novox/hq ADR 0266): the
|
||||
// session's account cannot enter the module's state, and must not be able to rewrite what it was refused, so
|
||||
// each line goes to the machine's journal, which the account may add to and not change. The record's
|
||||
// location is then this prefix and the journal's identifier.
|
||||
const JournalRecord = "journal:"
|
||||
|
||||
// GuardJournalID is the identifier the guard's lines carry in the journal.
|
||||
const GuardJournalID = "mesh-claude-code-guard"
|
||||
|
||||
// journalSocket is where the journal takes a line; a test points it at its own.
|
||||
var journalSocket = "/run/systemd/journal/socket"
|
||||
|
||||
// RecordGuard appends one line to the record.
|
||||
func RecordGuard(path string, e GuardEntry) error {
|
||||
if path == "" {
|
||||
return fmt.Errorf("no record is kept here")
|
||||
}
|
||||
if id, ok := strings.CutPrefix(path, JournalRecord); ok {
|
||||
return recordInJournal(id, e)
|
||||
}
|
||||
if info, err := os.Stat(path); err == nil && info.Size() > GuardLogMax {
|
||||
_ = os.Rename(path, path+".1")
|
||||
}
|
||||
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
raw, _ := json.Marshal(e)
|
||||
if _, err := f.Write(append(raw, '\n')); err != nil {
|
||||
f.Close()
|
||||
return err
|
||||
}
|
||||
return f.Close()
|
||||
}
|
||||
|
||||
// recordInJournal says one line to the journal by its native protocol: the entry as JSON in MESSAGE, one line,
|
||||
// so nothing in a command can add a field of its own.
|
||||
func recordInJournal(id string, e GuardEntry) error {
|
||||
raw, err := json.Marshal(e)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conn, err := net.Dial("unixgram", journalSocket)
|
||||
if err != nil {
|
||||
return fmt.Errorf("the journal cannot be reached: %w", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
_, err = conn.Write([]byte("SYSLOG_IDENTIFIER=" + id + "\nPRIORITY=5\nMESSAGE=" + string(raw) + "\n"))
|
||||
return err
|
||||
}
|
||||
|
||||
// readJournal is the journal's last lines of the guard's record, read as root through the runtime account's
|
||||
// sudo where this process is not root: what other accounts said is not the runtime account's to read. As JSON,
|
||||
// with the field the journal itself sets for the program that wrote the line (_EXE), which no writer chooses —
|
||||
// and matched on it in the query itself, so lines another program wrote under the guard's identifier cannot
|
||||
// push the guard's own past the last ones read.
|
||||
var readJournal = func(id string, last int) ([]byte, error) {
|
||||
args := []string{"journalctl", "-q", "--no-pager", "-o", "json", "--output-fields=MESSAGE,_EXE", "-t", id,
|
||||
"_EXE=" + HelperPath(), "-n", strconv.Itoa(last)}
|
||||
if os.Geteuid() != 0 {
|
||||
args = append([]string{"sudo", "-n"}, args...)
|
||||
}
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
cmd.Env = append(os.Environ(), "LC_ALL=C", "LANG=C")
|
||||
return cmd.Output()
|
||||
}
|
||||
|
||||
// guardsOwnLines keeps, of the journal's JSON lines, the messages the guard's own binary wrote (novox/hq ADR
|
||||
// 0266). The agent's account may add to the journal under any identifier — `logger -t` — and the journal
|
||||
// says which program wrote each line; a line another program wrote under the guard's identifier is not the
|
||||
// guard's record. The agent can still run the guard's binary itself, so the record is the guard's words, not
|
||||
// proof that each refusal happened in a session: what it cannot do is remove one.
|
||||
func guardsOwnLines(raw []byte, exe string) []byte {
|
||||
var out []byte
|
||||
for _, line := range bytes.Split(raw, []byte("\n")) {
|
||||
var f struct {
|
||||
Message any `json:"MESSAGE"`
|
||||
Exe string `json:"_EXE"`
|
||||
}
|
||||
if json.Unmarshal(line, &f) != nil || f.Exe != exe {
|
||||
continue
|
||||
}
|
||||
if m, ok := f.Message.(string); ok {
|
||||
out = append(append(out, m...), '\n')
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ReadGuardLog is the record's last lines, newest last.
|
||||
func ReadGuardLog(path string, last int) []GuardEntry {
|
||||
read := os.ReadFile
|
||||
if id, ok := strings.CutPrefix(path, JournalRecord); ok {
|
||||
read = func(string) ([]byte, error) {
|
||||
raw, err := readJournal(id, last)
|
||||
return guardsOwnLines(raw, HelperPath()), err
|
||||
}
|
||||
}
|
||||
raw, err := read(path)
|
||||
if err != nil {
|
||||
return []GuardEntry{}
|
||||
}
|
||||
lines := strings.Split(strings.TrimSpace(string(raw)), "\n")
|
||||
if len(lines) > last {
|
||||
lines = lines[len(lines)-last:]
|
||||
}
|
||||
out := []GuardEntry{}
|
||||
for _, l := range lines {
|
||||
var e GuardEntry
|
||||
if json.Unmarshal([]byte(l), &e) == nil {
|
||||
out = append(out, e)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// sshDashG reads a destination as ssh does: `ssh -G` prints the configuration it would use and connects
|
||||
// to nothing, so an alias, a HostName and a ProxyJump in ~/.ssh/config are what is judged.
|
||||
func sshDashG(options []string, destination string) (host, user string, jumps []string) {
|
||||
if _, err := exec.LookPath("ssh"); err != nil {
|
||||
return "", "", nil
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
args := append([]string{"-G"}, options...)
|
||||
args = append(args, "--", destination)
|
||||
out, err := exec.CommandContext(ctx, "ssh", args...).Output()
|
||||
if err != nil {
|
||||
return "", "", nil
|
||||
}
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
k, v, ok := strings.Cut(strings.TrimSpace(line), " ")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
switch k {
|
||||
case "hostname":
|
||||
host = strings.ToLower(v)
|
||||
case "user":
|
||||
user = v
|
||||
case "proxyjump":
|
||||
if v != "none" {
|
||||
jumps = append(jumps, strings.Split(v, ",")...)
|
||||
}
|
||||
}
|
||||
}
|
||||
return host, user, jumps
|
||||
}
|
||||
|
||||
func quickLookup(name string) []string {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
addrs, _ := net.DefaultResolver.LookupHost(ctx, name)
|
||||
return addrs
|
||||
}
|
||||
|
||||
// sessionEnvironment is the environment the agent's own process was started with: the nearest ancestor
|
||||
// that is the agent, read from what the kernel kept at its exec.
|
||||
func sessionEnvironment(proc string, pid int) map[string]string {
|
||||
for i := 0; i < 32 && pid > 1; i++ {
|
||||
cmdline, _ := os.ReadFile(filepath.Join(proc, strconv.Itoa(pid), "cmdline"))
|
||||
if isAgent(cmdline) {
|
||||
raw, err := os.ReadFile(filepath.Join(proc, strconv.Itoa(pid), "environ"))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
env := map[string]string{}
|
||||
for _, kv := range bytes.Split(raw, []byte{0}) {
|
||||
if k, v, ok := strings.Cut(string(kv), "="); ok {
|
||||
env[k] = v
|
||||
}
|
||||
}
|
||||
return env
|
||||
}
|
||||
stat, err := os.ReadFile(filepath.Join(proc, strconv.Itoa(pid), "stat"))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
// The parent is the fourth field, after the command's name in parentheses (which may hold spaces).
|
||||
closing := bytes.LastIndexByte(stat, ')')
|
||||
fields := strings.Fields(string(stat[closing+1:]))
|
||||
if len(fields) < 2 {
|
||||
return nil
|
||||
}
|
||||
pid, _ = strconv.Atoi(fields[1])
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// isAgent says whether a process's command line is the agent's: its native binary, or its package run by node.
|
||||
func isAgent(cmdline []byte) bool {
|
||||
args := strings.Split(strings.TrimRight(string(cmdline), "\x00"), "\x00")
|
||||
if len(args) == 0 || args[0] == "" {
|
||||
return false
|
||||
}
|
||||
if filepath.Base(args[0]) == "claude" {
|
||||
return true
|
||||
}
|
||||
if b := filepath.Base(args[0]); (b == "node" || b == "bun") && len(args) > 1 {
|
||||
return filepath.Base(args[1]) == "claude" || strings.Contains(args[1], "@anthropic-ai/claude-code")
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// currentAccount is the account the session runs as; a test sets its own.
|
||||
var currentAccount = func() string {
|
||||
if u, err := user.Current(); err == nil {
|
||||
return u.Username
|
||||
}
|
||||
return "uid " + strconv.Itoa(os.Getuid())
|
||||
}
|
||||
|
||||
// runGuard is the hook: the tool call in, the verdict out as the exit code.
|
||||
func runGuard(dataPath string, stdin io.Reader, stderr io.Writer) (code int) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Fprintf(stderr, "the mesh's guard failed and judged nothing: %v\n", r)
|
||||
code = 1
|
||||
}
|
||||
}()
|
||||
var in HookInput
|
||||
if err := json.NewDecoder(stdin).Decode(&in); err != nil {
|
||||
fmt.Fprintf(stderr, "the mesh's guard could not read the tool call: %v\n", err)
|
||||
return 1
|
||||
}
|
||||
var data GuardData
|
||||
if raw, err := os.ReadFile(dataPath); err == nil {
|
||||
_ = json.Unmarshal(raw, &data)
|
||||
}
|
||||
g := Guard{Data: data, SSH: sshDashG, Resolve: quickLookup}
|
||||
v := g.JudgeAs(currentAccount(), in)
|
||||
if !v.Refuse {
|
||||
return 0
|
||||
}
|
||||
command, _ := in.ToolInput["command"].(string)
|
||||
if command == "" {
|
||||
command, _ = in.ToolInput["file_path"].(string)
|
||||
}
|
||||
entry := GuardEntry{At: time.Now().UTC().Format(time.RFC3339), Node: data.Node, Decision: "refused", Rule: v.Rule,
|
||||
Machine: v.Machine, Tool: in.ToolName, Command: cut(command, 400), Session: in.SessionID, Tools: v.Calls}
|
||||
if v.Overridable {
|
||||
if why := strings.TrimSpace(sessionEnvironment(procRoot, parentPID())[OverrideVar]); why != "" {
|
||||
entry.Decision, entry.Why = "overridden", cut(why, 200)
|
||||
if err := RecordGuard(data.Log, entry); err == nil {
|
||||
return 0
|
||||
} else {
|
||||
v.Message += fmt.Sprintf("\nThe operator's override is set, and could not be recorded (%v): an override "+
|
||||
"that is not recorded is not honoured.", err)
|
||||
entry.Decision, entry.Why = "refused", ""
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = RecordGuard(data.Log, entry)
|
||||
fmt.Fprintln(stderr, v.Message)
|
||||
return 2
|
||||
}
|
||||
@@ -1,328 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
||||
)
|
||||
|
||||
// meshForTheGuard is a mesh of two machines and the verbs that replace what an agent runs over ssh, as the
|
||||
// seats' contract and the modules' manifests say them.
|
||||
func meshForTheGuard() GuardData {
|
||||
defined := []seatdef.Definition{
|
||||
{Seat: "mesh-module-assigner", Scope: "mesh", Verbs: []seatdef.Verb{
|
||||
{Name: "assignments", Description: "Read assignments: one node's key.", Replaces: []string{"hostnamectl", "uptime"}}}},
|
||||
{Seat: "node-service-manager", Scope: "node", Verbs: []seatdef.Verb{
|
||||
{Name: "status", Description: "One unit as the service manager sees it now: its states.", Replaces: []string{"systemctl status", "systemctl is-active"}},
|
||||
{Name: "restart", Description: "Restart one unit.", Replaces: []string{"systemctl restart"}},
|
||||
{Name: "journal", Description: "The last lines of one unit's journal.", Replaces: []string{"journalctl"}}}},
|
||||
{Seat: "node-hostname", Scope: "node", Verbs: []seatdef.Verb{
|
||||
{Name: "add", Description: "Add one address and its names to the operator's lines of /etc/hosts.", Replaces: []string{"edit /etc/hosts", "HOSTALIASES"}}}},
|
||||
}
|
||||
modules := []ModuleTools{{Module: "docker", On: []string{"anchor"}, Replaces: map[string][]string{"docker_logs": {"docker logs"}, "docker_list": {"docker ps"}}},
|
||||
{Module: "systemd", On: []string{"anchor", "laptop"}}}
|
||||
return GuardData{Node: "laptop", Replacements: ReplacementsOf(defined, modules), Machines: []Machine{
|
||||
{Name: "anchor", Domains: []string{"anchor" + InternalSuffix, "anchor.example"}, Addresses: []string{"192.0.2.10", "10.10.0.1"}},
|
||||
{Name: "laptop", Domains: []string{"laptop" + InternalSuffix}, Addresses: []string{"10.10.0.4"}},
|
||||
}}
|
||||
}
|
||||
|
||||
// noSSH reads a destination from the command line alone; aliases reads one alias as ssh's configuration would.
|
||||
func aliases(options []string, destination string) (string, string, []string) {
|
||||
host, user := hostOf(destination)
|
||||
switch host {
|
||||
case "a":
|
||||
return "anchor.example", "operator", nil
|
||||
case "via-anchor":
|
||||
return "203.0.113.5", "operator", []string{"anchor"}
|
||||
case "forge":
|
||||
return "git.anchor" + InternalSuffix, "git", nil
|
||||
}
|
||||
if user == "" {
|
||||
user = "operator"
|
||||
}
|
||||
for i, o := range options {
|
||||
if o == "-l" && i+1 < len(options) {
|
||||
user = options[i+1]
|
||||
}
|
||||
}
|
||||
return host, user, nil
|
||||
}
|
||||
|
||||
func guard() Guard {
|
||||
return Guard{Data: meshForTheGuard(), SSH: aliases, Resolve: func(name string) []string {
|
||||
if name == "nas.lan" {
|
||||
return []string{"192.0.2.10"}
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
}
|
||||
|
||||
func bash(command string) HookInput {
|
||||
return HookInput{ToolName: "Bash", ToolInput: map[string]any{"command": command}}
|
||||
}
|
||||
|
||||
// **ssh to a mesh machine is refused**, by every name and address it has and however the shell spells it,
|
||||
// and the refusal names the verb that does the job on that machine (novox/hq ADR 0245).
|
||||
func TestSSHToAMeshMachineIsRefusedNamingTheTool(t *testing.T) {
|
||||
g := guard()
|
||||
for command, want := range map[string]string{
|
||||
"ssh anchor journalctl -u mesh-config -n 50": "anchor/node-service-manager.journal",
|
||||
"ssh anchor 'sudo journalctl -fu sshd'": "anchor/node-service-manager.journal",
|
||||
"ssh -p 22 root@anchor.example systemctl status nats": "anchor/node-service-manager.status",
|
||||
"ssh anchor.internal systemctl restart nats": "anchor/node-service-manager.restart",
|
||||
"ssh -o StrictHostKeyChecking=no 10.10.0.1 docker ps -a": "anchor/docker.docker_list",
|
||||
"ssh anchor docker logs --tail 100 nats": "anchor/docker.docker_logs",
|
||||
"ssh laptop docker logs x": "anchor/docker.docker_logs",
|
||||
"ssh a uptime": "mesh-module-assigner.assignments",
|
||||
"cd /tmp && ssh anchor journalctl": "anchor/node-service-manager.journal",
|
||||
"sudo -u root ssh anchor journalctl": "anchor/node-service-manager.journal",
|
||||
"timeout 10 ssh anchor journalctl": "anchor/node-service-manager.journal",
|
||||
"env LANG=C ssh anchor journalctl": "anchor/node-service-manager.journal",
|
||||
"bash -c 'ssh anchor journalctl -u x'": "anchor/node-service-manager.journal",
|
||||
`echo "$(ssh anchor journalctl -n 5)" | tail`: "anchor/node-service-manager.journal",
|
||||
"for n in anchor laptop; do ssh $n uptime; done; ssh anchor uptime": "mesh-module-assigner.assignments",
|
||||
"ssh -J anchor 203.0.113.9 journalctl": "anchor/node-service-manager.journal",
|
||||
"ssh via-anchor journalctl": "anchor/node-service-manager.journal",
|
||||
"ssh nas.lan journalctl": "anchor/node-service-manager.journal",
|
||||
"ssh ssh://anchor:2222 journalctl": "anchor/node-service-manager.journal",
|
||||
} {
|
||||
v := g.Judge(bash(command))
|
||||
if !v.Refuse || v.Rule != "ssh" {
|
||||
t.Errorf("%q was not refused: %+v", command, v)
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(v.Message, want) {
|
||||
t.Errorf("%q: the refusal does not name %s:\n%s", command, want, v.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// What no tool replaces is refused all the same, saying a tool is created, never worked around; a shell
|
||||
// on a machine is pointed at what the machine serves; copying a file off one is refused too.
|
||||
func TestSSHWithNoToolForItSaysCreateOne(t *testing.T) {
|
||||
g := guard()
|
||||
for command, want := range map[string]string{
|
||||
"ssh anchor cat /var/lib/thing/state.json": "a missing tool is created in the module",
|
||||
"ssh anchor": "mesh_machine anchor",
|
||||
"ssh build.internal ls": "a missing tool is created",
|
||||
"scp anchor:/etc/nats/nats.conf /tmp/": "mesh_machine anchor",
|
||||
"rsync -av root@10.10.0.4:/srv/ ./srv/": "mesh_machine laptop",
|
||||
"sftp anchor.example": "mesh_machine anchor",
|
||||
"mosh anchor": "mesh_machine anchor",
|
||||
"autossh -M 0 -N -L 5432:localhost:5432 anchor": "mesh_machine anchor",
|
||||
} {
|
||||
v := g.Judge(bash(command))
|
||||
if !v.Refuse {
|
||||
t.Errorf("%q was not refused", command)
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(v.Message, want) {
|
||||
t.Errorf("%q: want %q in:\n%s", command, want, v.Message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// **What is not a work-around passes**: git over ssh to the forge above all, and ssh beyond the mesh, and
|
||||
// every command that merely mentions ssh or a machine.
|
||||
func TestWhatIsNotAWorkAroundPasses(t *testing.T) {
|
||||
g := guard()
|
||||
for _, command := range []string{
|
||||
"git push -u origin feat/x",
|
||||
"git clone ssh://git@git.anchor.internal:222/novox/hq.git",
|
||||
"git fetch ssh://git@git.anchor.internal:222/novox/hq.git main",
|
||||
`GIT_SSH_COMMAND="ssh -i ~/.ssh/forge -o IdentitiesOnly=yes" git push origin HEAD`,
|
||||
"git -c core.sshCommand='ssh -p 222' pull",
|
||||
"ssh -T git@git.anchor.internal -p 222",
|
||||
"ssh forge",
|
||||
"scp git@git.anchor.internal:novox/hq.git .",
|
||||
"ssh github.com",
|
||||
"ssh -T git@github.com",
|
||||
"ssh user@203.0.113.7 uptime",
|
||||
"ssh -V",
|
||||
"ssh-keygen -t ed25519 -f ~/.ssh/x",
|
||||
"ssh-add -l",
|
||||
"man ssh",
|
||||
"grep -r 'ssh anchor' docs/",
|
||||
`git commit -m "Stop running ssh anchor journalctl"`,
|
||||
"echo anchor && journalctl --user -n 5",
|
||||
"cat /etc/hosts",
|
||||
"getent hosts anchor.internal",
|
||||
"cp /etc/hosts /tmp/hosts.copy",
|
||||
"sed -n 1,5p /etc/hosts",
|
||||
"curl -s http://anchor.internal:8080/health",
|
||||
"go test ./...",
|
||||
} {
|
||||
if v := g.Judge(bash(command)); v.Refuse {
|
||||
t.Errorf("%q was refused:\n%s", command, v.Message)
|
||||
}
|
||||
}
|
||||
if v := g.Judge(HookInput{ToolName: "Read", ToolInput: map[string]any{"file_path": "/etc/hosts"}}); v.Refuse {
|
||||
t.Errorf("reading /etc/hosts was refused")
|
||||
}
|
||||
if v := g.Judge(HookInput{ToolName: "Write", ToolInput: map[string]any{"file_path": "/tmp/hosts"}}); v.Refuse {
|
||||
t.Errorf("writing a file named hosts elsewhere was refused")
|
||||
}
|
||||
}
|
||||
|
||||
// **The local work-arounds for a mesh name are refused** with the machine's own hosts verb: writing
|
||||
// /etc/hosts by any means, the agent's own Edit and Write included, and HOSTALIASES.
|
||||
func TestTheLocalWorkAroundsForAMeshNameAreRefused(t *testing.T) {
|
||||
g := guard()
|
||||
for _, command := range []string{
|
||||
"echo '10.10.0.1 anchor' | sudo tee -a /etc/hosts",
|
||||
"sudo sh -c 'echo 10.10.0.1 anchor >> /etc/hosts'",
|
||||
"echo x >>/etc/hosts",
|
||||
"sudo sed -i 's/old/new/' /etc/hosts",
|
||||
"sudo sed -Ei.bak 's/a/b/' /etc/hosts",
|
||||
"sudo vim /etc/hosts",
|
||||
"sudo cp /tmp/hosts /etc/hosts",
|
||||
"sudo install -m 644 hosts /etc/hosts",
|
||||
"HOSTALIASES=~/.hosts curl http://anchor/",
|
||||
"export HOSTALIASES=/tmp/aliases",
|
||||
} {
|
||||
v := g.Judge(bash(command))
|
||||
if !v.Refuse {
|
||||
t.Errorf("%q was not refused", command)
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(v.Message, "laptop/node-hostname.add") {
|
||||
t.Errorf("%q: the refusal does not name this machine's hosts verb:\n%s", command, v.Message)
|
||||
}
|
||||
}
|
||||
for _, tool := range []string{"Edit", "Write", "MultiEdit"} {
|
||||
v := g.Judge(HookInput{ToolName: tool, ToolInput: map[string]any{"file_path": "/etc/../etc/hosts"}})
|
||||
if !v.Refuse || !strings.Contains(v.Message, "node-hostname.add") {
|
||||
t.Errorf("%s of /etc/hosts was not refused with the verb: %+v", tool, v)
|
||||
}
|
||||
}
|
||||
// The resolver file has no verb that replaces writing it: the refusal says to create one.
|
||||
v := g.Judge(bash("echo nameserver 192.0.2.53 | sudo tee /etc/resolv.conf"))
|
||||
if !v.Refuse || !strings.Contains(v.Message, "a missing tool is created") {
|
||||
t.Errorf("writing the resolver file: %+v", v)
|
||||
}
|
||||
}
|
||||
|
||||
// The agent never names the operator's override, and no override lifts that refusal.
|
||||
func TestACommandNamingTheOverrideIsRefused(t *testing.T) {
|
||||
g := guard()
|
||||
for _, command := range []string{
|
||||
"export " + OverrideVar + "=because",
|
||||
OverrideVar + "=x claude -p 'ssh anchor uptime'",
|
||||
"env " + OverrideVar + "=1 bash",
|
||||
} {
|
||||
v := g.Judge(bash(command))
|
||||
if !v.Refuse || v.Rule != "override-named" || v.Overridable {
|
||||
t.Errorf("%q: %+v", command, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Without the mesh's answer — a controller not asked yet — the mesh's own names are still refused.
|
||||
func TestWithoutTheMeshsAnswerItsNamesAreStillRefused(t *testing.T) {
|
||||
g := Guard{}
|
||||
if v := g.Judge(bash("ssh anchor.internal journalctl")); !v.Refuse || !strings.Contains(v.Message, "a missing tool is created") {
|
||||
t.Errorf("an internal name without data: %+v", v)
|
||||
}
|
||||
if v := g.Judge(bash("echo x | sudo tee -a /etc/hosts")); !v.Refuse {
|
||||
t.Errorf("the hosts file without data: %+v", v)
|
||||
}
|
||||
}
|
||||
|
||||
// fakeProc lays out a process tree: the agent started with env, a shell under it, the hook under that.
|
||||
func fakeProc(t *testing.T, agentEnv []string) (root string, hookParent int) {
|
||||
t.Helper()
|
||||
root = t.TempDir()
|
||||
write := func(pid, ppid int, cmdline []string, env []string) {
|
||||
dir := filepath.Join(root, strconv.Itoa(pid))
|
||||
_ = os.MkdirAll(dir, 0o755)
|
||||
_ = os.WriteFile(filepath.Join(dir, "cmdline"), []byte(strings.Join(cmdline, "\x00")+"\x00"), 0o644)
|
||||
_ = os.WriteFile(filepath.Join(dir, "environ"), []byte(strings.Join(env, "\x00")), 0o644)
|
||||
_ = os.WriteFile(filepath.Join(dir, "stat"), []byte(strconv.Itoa(pid)+" (some (odd) name) S "+strconv.Itoa(ppid)+" 1 1"), 0o644)
|
||||
}
|
||||
write(100, 1, []string{"/opt/claude-code/bin/claude", "--resume"}, agentEnv)
|
||||
write(200, 100, []string{"/bin/sh", "-c", "hook"}, []string{OverrideVar + "=set-by-the-session"})
|
||||
return root, 200
|
||||
}
|
||||
|
||||
// **The override is the operator's, from the session as it was started, and recorded** — and a value a
|
||||
// command or a setting put in the session's later environment is not it.
|
||||
func TestTheOverrideIsTheOperatorsAndRecorded(t *testing.T) {
|
||||
data := meshForTheGuard()
|
||||
data.Log = filepath.Join(t.TempDir(), "guard.log")
|
||||
path := filepath.Join(t.TempDir(), "guard.json")
|
||||
raw, _ := json.Marshal(data)
|
||||
_ = os.WriteFile(path, raw, 0o644)
|
||||
was, wasPID := procRoot, parentPID
|
||||
t.Cleanup(func() { procRoot, parentPID = was, wasPID })
|
||||
run := func(command string) (int, string) {
|
||||
var stderr bytes.Buffer
|
||||
in, _ := json.Marshal(bash(command))
|
||||
return runGuard(path, bytes.NewReader(in), &stderr), stderr.String()
|
||||
}
|
||||
|
||||
// Not set where the session started: the later shell's value is not the operator's.
|
||||
root, hook := fakeProc(t, []string{"HOME=/home/operator"})
|
||||
procRoot, parentPID = root, func() int { return hook }
|
||||
if code, says := run("ssh anchor journalctl"); code != 2 || !strings.Contains(says, "anchor/node-service-manager.journal") {
|
||||
t.Fatalf("refused with %d: %s", code, says)
|
||||
}
|
||||
// Set by the operator before the session: let through, with why on record.
|
||||
root, hook = fakeProc(t, []string{"HOME=/home/operator", OverrideVar + "=reading the broker's log by hand while the systemd module is down"})
|
||||
procRoot, parentPID = root, func() int { return hook }
|
||||
if code, says := run("ssh anchor journalctl -u nats"); code != 0 {
|
||||
t.Fatalf("the operator's override was not honoured: %d %s", code, says)
|
||||
}
|
||||
// Never for a command naming the override itself.
|
||||
if code, _ := run("export " + OverrideVar + "=x"); code != 2 {
|
||||
t.Fatalf("a command naming the override passed under it: %d", code)
|
||||
}
|
||||
record := ReadGuardLog(data.Log, 10)
|
||||
if len(record) != 3 || record[0].Decision != "refused" || record[1].Decision != "overridden" ||
|
||||
!strings.Contains(record[1].Why, "systemd module is down") || record[1].Machine != "anchor" || record[2].Rule != "override-named" {
|
||||
t.Fatalf("the record: %+v", record)
|
||||
}
|
||||
// An override that cannot be recorded is not honoured.
|
||||
data.Log = filepath.Join(t.TempDir(), "no", "such", "dir", "guard.log")
|
||||
raw, _ = json.Marshal(data)
|
||||
_ = os.WriteFile(path, raw, 0o644)
|
||||
if code, says := run("ssh anchor journalctl"); code != 2 || !strings.Contains(says, "could not be recorded") {
|
||||
t.Fatalf("an override not recorded was honoured: %d %s", code, says)
|
||||
}
|
||||
}
|
||||
|
||||
// What passes is not recorded, and a guard that cannot read the call says so and blocks nothing.
|
||||
func TestTheHookExitsAsTheAgentReadsIt(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "guard.json")
|
||||
_ = os.WriteFile(path, []byte(`{}`), 0o644)
|
||||
var stderr bytes.Buffer
|
||||
in, _ := json.Marshal(bash("git push"))
|
||||
if code := runGuard(path, bytes.NewReader(in), &stderr); code != 0 || stderr.Len() != 0 {
|
||||
t.Errorf("git push: %d %q", code, stderr.String())
|
||||
}
|
||||
if code := runGuard(path, strings.NewReader("not json"), &stderr); code != 1 {
|
||||
t.Errorf("an unreadable call: %d", code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheAgentsProcessIsKnown(t *testing.T) {
|
||||
for cmdline, want := range map[string]bool{
|
||||
"/opt/claude-code/bin/claude\x00--resume\x00": true,
|
||||
"claude\x00": true,
|
||||
"node\x00/usr/lib/node_modules/@anthropic-ai/claude-code/cli.js\x00": true,
|
||||
"/usr/bin/node\x00/home/x/.local/bin/claude\x00": true,
|
||||
"/bin/zsh\x00": false,
|
||||
"node\x00server.js\x00": false,
|
||||
"/usr/bin/claude-code-tools\x00": false,
|
||||
} {
|
||||
if isAgent([]byte(cmdline)) != want {
|
||||
t.Errorf("%q: want %v", cmdline, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
// What this node holds, said (design 39 bus-and-switch §23): kept in this module's own state `holdings` and said as
|
||||
// the event `holdings-changed`, the holdings report whole and never a token, at start, whenever it changes, and again
|
||||
// when the licence manager says `holdings.asked`.
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// HoldingsChanged is the event that says what a node holds.
|
||||
const HoldingsChanged = "holdings-changed"
|
||||
|
||||
// HoldingsAsked is the licence manager's event asking every node to say what it holds.
|
||||
const HoldingsAsked = Manager + ".holdings.asked"
|
||||
|
||||
// HoldingsSayer keeps and says what this node holds.
|
||||
type HoldingsSayer struct {
|
||||
Put func(Holdings) error
|
||||
Emit func(Holdings) error
|
||||
said string
|
||||
}
|
||||
|
||||
// Say keeps and says now when it differs from what was said last, or always when again: true when it said it. A
|
||||
// failure says nothing was said, so the next call tries again.
|
||||
func (s *HoldingsSayer) Say(now Holdings, again bool) (bool, error) {
|
||||
raw, _ := json.Marshal(now)
|
||||
if !again && string(raw) == s.said {
|
||||
return false, nil
|
||||
}
|
||||
if err := s.Put(now); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := s.Emit(now); err != nil {
|
||||
return false, err
|
||||
}
|
||||
s.said = string(raw)
|
||||
return true, nil
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// design 39 bus-and-switch §23: the holdings are said at start, on a change and when asked again, kept in the
|
||||
// module's own state each time, the report whole and never a token.
|
||||
func TestHoldingsAreSaidAtStartAndOnChange(t *testing.T) {
|
||||
var kept, said []string
|
||||
failing := false
|
||||
s := &HoldingsSayer{
|
||||
Put: func(h Holdings) error { raw, _ := json.Marshal(h); kept = append(kept, string(raw)); return nil },
|
||||
Emit: func(h Holdings) error {
|
||||
if failing {
|
||||
return errors.New("no responders")
|
||||
}
|
||||
raw, _ := json.Marshal(h)
|
||||
said = append(said, string(raw))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
fp := "sha256:0123456789abcdef"
|
||||
start := Holdings{Node: "laptop"}
|
||||
changed := Holdings{Node: "laptop"}
|
||||
changed.Refresh.Present, changed.Refresh.Fingerprint = true, &fp
|
||||
for i, step := range []struct {
|
||||
h Holdings
|
||||
again, out bool
|
||||
}{{start, false, true}, {start, false, false}, {changed, false, true}, {changed, true, true}} {
|
||||
if got, err := s.Say(step.h, step.again); err != nil || got != step.out {
|
||||
t.Fatalf("step %d: said %v (%v), want %v", i, got, err, step.out)
|
||||
}
|
||||
}
|
||||
if len(said) != 3 || len(kept) != 3 {
|
||||
t.Fatalf("said %d, kept %d, want 3 each", len(said), len(kept))
|
||||
}
|
||||
if !strings.Contains(said[1], fp) || !strings.Contains(said[1], `"node":"laptop"`) {
|
||||
t.Fatalf("the report is not said whole: %s", said[1])
|
||||
}
|
||||
failing = true
|
||||
if got, err := s.Say(start, false); err == nil || got {
|
||||
t.Fatal("a failed event counts as said")
|
||||
}
|
||||
failing = false
|
||||
if got, _ := s.Say(start, false); !got {
|
||||
t.Fatal("what failed to be said is not said at the next call")
|
||||
}
|
||||
}
|
||||
@@ -63,7 +63,7 @@ func FilesFor(p Paths) *Files {
|
||||
return &Files{Home: filepath.Clean(p.Home), Account: p.Agent, Helper: &Helper{Account: p.Agent, Exe: HelperPath()}}
|
||||
}
|
||||
|
||||
// HelperPath is the helper's binary: the guard's copy of this module's binary in the plugin, root's and
|
||||
// HelperPath is the helper's binary: the plugin's copy of this module's binary in the plugin, root's and
|
||||
// readable by every account, so the agent's account runs exactly what the mesh placed.
|
||||
func HelperPath() string {
|
||||
return filepath.Join(ManagedDir, MarketplaceDir, "plugins", Plugin, GuardDir, "guard")
|
||||
|
||||
@@ -4,12 +4,10 @@ package main
|
||||
// address, not this shell command — generated on every render from what each seat verb and module tool
|
||||
// says it replaces, so it cannot drift from the tools the mesh has.
|
||||
//
|
||||
// Short: a row per seat or module, its top what this machine's agent reached round most — the tools the
|
||||
// guard named most often in its record.
|
||||
// Short: a row per seat or module, the machines' seats first.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -31,17 +29,15 @@ func code(s string) string { return "`" + strings.ReplaceAll(s, "`", "'") + "`"
|
||||
|
||||
// insteadGroup is one row: a seat or a module, and its verbs that replace a command.
|
||||
type insteadGroup struct {
|
||||
prefix string // `<node>/node-service-manager.` or `mesh-controller.`
|
||||
verbs []Replacement
|
||||
refused int
|
||||
prefix string // `<node>/node-service-manager.` or `mesh-controller.`
|
||||
verbs []Replacement
|
||||
}
|
||||
|
||||
// InsteadOf is the section, or "" when the mesh said nothing replaces anything — an older controller,
|
||||
// or one not yet asked.
|
||||
//
|
||||
// One row per seat or module, every verb of it that replaces a command in the row, so a verb is never cut
|
||||
// for being late in its seat's list; the rows the guard here named most come first, then the machines'
|
||||
// seats, the modules' tools and the mesh's own seats in the records' order.
|
||||
// for being late in its seat's list; the machines' seats first, the modules' tools and the mesh's own seats in the records' order.
|
||||
func InsteadOf(m *MeshTools) string {
|
||||
if m == nil || len(m.Replacements) == 0 {
|
||||
return ""
|
||||
@@ -64,13 +60,8 @@ func InsteadOf(m *MeshTools) string {
|
||||
groups = append(groups, g)
|
||||
}
|
||||
g.verbs = append(g.verbs, r)
|
||||
g.refused += m.Refused[r.Address]
|
||||
}
|
||||
}
|
||||
sort.SliceStable(groups, func(i, j int) bool { return groups[i].refused > groups[j].refused })
|
||||
for _, g := range groups {
|
||||
sort.SliceStable(g.verbs, func(i, j int) bool { return m.Refused[g.verbs[i].Address] > m.Refused[g.verbs[j].Address] })
|
||||
}
|
||||
more := 0
|
||||
if len(groups) > InsteadOfRows {
|
||||
more = len(groups) - InsteadOfRows
|
||||
@@ -97,7 +88,7 @@ func InsteadOf(m *MeshTools) string {
|
||||
fmt.Fprintf(&b, "\n%d more seats and modules: `mesh_search` with the command finds the tool for it.\n", more)
|
||||
}
|
||||
b.WriteString("\n- **Never `ssh` to a mesh machine**, and never edit `/etc/hosts` or set `HOSTALIASES` for a mesh\n" +
|
||||
" name: the guard on this session's shell refuses it and names the tool. Where no tool covers what you\n" +
|
||||
" name: call the tool the table names instead. Where no tool covers what you\n" +
|
||||
" need, none is worked around: say so, and the tool is created in the module that owns it, on its seat.\n")
|
||||
return b.String()
|
||||
}
|
||||
|
||||
@@ -2,21 +2,20 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
assignerclient "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient"
|
||||
mesh "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
|
||||
catalogclient "git.novox.be/novox/mesh-delivery/contracts/go/clients/meshcatalogclient"
|
||||
catalog "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
assignerclient "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient"
|
||||
mesh "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
|
||||
catalog "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
||||
"git.novox.be/novox/mesh-sdk/go/seatlist"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
@@ -57,7 +56,7 @@ func someSeats() []seatdef.Definition {
|
||||
// saying what its tool replaces, as the seat mesh-module-assigner and the indexer keep them on the bus.
|
||||
func aMesh(t *testing.T) aBus {
|
||||
t.Helper()
|
||||
b := aBus{AssignmentsRead: {}, ModulesRead: {}, ManifestsRead: {}}
|
||||
b := aBus{AssignmentsRead: {}}
|
||||
for _, p := range []mesh.Assignments{
|
||||
{V: 1, Node: "anchor", Decided: mesh.NodeDecided{PublicDomain: "Anchor.Example"},
|
||||
Modules: []mesh.PlacedModule{{Module: "docker"}, {Module: "zsh"}}},
|
||||
@@ -71,33 +70,39 @@ func aMesh(t *testing.T) aBus {
|
||||
b[AssignmentsRead][k] = v
|
||||
}
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// fakeCatalog is the catalog half as the tests give it: docker and zsh, docker's manifest saying what its tool replaces.
|
||||
type fakeCatalog struct {
|
||||
entries map[string]catalog.CatalogEntry
|
||||
notYet error
|
||||
}
|
||||
|
||||
func (f fakeCatalog) Entry(module string) (catalog.CatalogEntry, bool, error) {
|
||||
e, ok := f.entries[module]
|
||||
return e, ok, f.notYet
|
||||
}
|
||||
|
||||
func (f fakeCatalog) Ready() error { return f.notYet }
|
||||
|
||||
func aCatalog() fakeCatalog {
|
||||
f := fakeCatalog{entries: map[string]catalog.CatalogEntry{}}
|
||||
for module, manifest := range map[string]string{
|
||||
"docker": `{"module":"docker","replaces":{"docker_logs":["docker logs"]}}`,
|
||||
"zsh": `{"module":"zsh"}`,
|
||||
} {
|
||||
digest := fmt.Sprintf("%x", sha256.Sum256([]byte(manifest)))
|
||||
mkey, _ := catalogclient.ManifestsKey(digest)
|
||||
raw, err := json.Marshal(catalog.ManifestsByDigestValue{V: 1, Module: module, Manifest: manifest})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b[ManifestsRead][mkey] = raw
|
||||
key, _ := catalogclient.ModulesKey(module)
|
||||
b[ModulesRead][key], _ = json.Marshal(catalog.ModulesByModuleValue{V: 1, None: "nothing is built of it", Manifest: digest, Repository: "novox/mesh-catalog", Commit: "c"})
|
||||
var whole map[string]any
|
||||
_ = json.Unmarshal([]byte(manifest), &whole)
|
||||
f.entries[module] = catalog.CatalogEntry{Module: module, Manifest: whole}
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func resolving(name string) []string {
|
||||
return map[string][]string{"anchor": {"10.10.0.1"}, "anchor.internal": {"10.10.0.1"}, "anchor.example": {"192.0.2.10"},
|
||||
"laptop": {"127.0.0.1", "10.10.0.4"}}[name]
|
||||
return f
|
||||
}
|
||||
|
||||
// Issue 1126 B13: what the mesh says of its tools and machines is read from the bus whole, never asked of the
|
||||
// controller: the seats' verbs, the placed modules' own tools, every machine with its domains and addresses — a
|
||||
// loopback address never one of them.
|
||||
// controller: the seats' verbs and the placed modules' own tools.
|
||||
func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) {
|
||||
m, err := ReadMeshToolsFrom(aMesh(t), someSeats(), resolving)
|
||||
m, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -108,10 +113,21 @@ func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) {
|
||||
if got := strings.Join(addresses, ","); got != "mesh-module-assigner.assignments,node-service-manager.status,node-service-manager.journal,docker.docker_logs" {
|
||||
t.Errorf("replacements: %s", got)
|
||||
}
|
||||
t.Run("the again tool answers the operator and the healers only", func(t *testing.T) {
|
||||
for caller, refused := range map[string]bool{"person.jochen": false, "shanks.mesh-healers": false, "shanks.mesh-config": true} {
|
||||
if err := seatlist.Refusal("claude_code_seat_list_again", seatListAgainCallers, caller); (err != nil) != refused {
|
||||
t.Errorf("%s: refused=%v, err %v", caller, refused, err)
|
||||
t.Run("the again tool answers every caller", func(t *testing.T) {
|
||||
// H-A-66 (design 47 H §752): no holder keeps a callers list; the seat list's tools answer every caller.
|
||||
files, _ := filepath.Glob("*.go")
|
||||
for _, f := range files {
|
||||
if strings.HasSuffix(f, "_test.go") {
|
||||
continue
|
||||
}
|
||||
raw, err := os.ReadFile(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, gone := range []string{"AnyPerson", "seatListAgainCallers", "seatlist.Refusal"} {
|
||||
if strings.Contains(string(raw), gone) {
|
||||
t.Errorf("%s still names %s", f, gone)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -132,7 +148,7 @@ func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) {
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatalf("the seat list was not read: %+v", seatList.Status())
|
||||
}
|
||||
got, err := ReadMeshToolsFrom(aMesh(t), definedSeats(), resolving)
|
||||
got, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), definedSeats())
|
||||
var addrs []string
|
||||
for _, r := range got.Replacements {
|
||||
addrs = append(addrs, r.Address)
|
||||
@@ -142,10 +158,6 @@ func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) {
|
||||
t.Fatalf("replacements from the bus: %v, %v", addrs, err)
|
||||
}
|
||||
})
|
||||
if len(m.Machines) != 2 || strings.Join(m.Machines[0].Domains, ",") != "anchor.internal,anchor.example" ||
|
||||
strings.Join(m.Machines[0].Addresses, ",") != "10.10.0.1,192.0.2.10" || strings.Join(m.Machines[1].Addresses, ",") != "10.10.0.4" {
|
||||
t.Errorf("machines: %+v", m.Machines)
|
||||
}
|
||||
if m.Replacements[3].Does != "logs" || !m.Replacements[2].NodeScoped || m.Replacements[0].NodeScoped {
|
||||
t.Errorf("the rows: %+v", m.Replacements)
|
||||
}
|
||||
@@ -154,7 +166,7 @@ func TestTheMeshsToolsAreReadFromTheBus(t *testing.T) {
|
||||
// **The table is generated, not written** (novox/hq ADR 0245): what the records say a verb replaces is a row,
|
||||
// the machines' seats first, and nothing is rendered where the records say nothing.
|
||||
func TestTheInsteadOfTableIsGeneratedFromTheRecords(t *testing.T) {
|
||||
m, _ := ReadMeshToolsFrom(aMesh(t), someSeats(), resolving)
|
||||
m, _ := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats())
|
||||
table := InsteadOf(&m)
|
||||
for _, want := range []string{
|
||||
"| `<node>/node-service-manager.` | `status` — `systemctl status`, `systemctl is-active` · `journal` — `journalctl` |",
|
||||
@@ -170,14 +182,11 @@ func TestTheInsteadOfTableIsGeneratedFromTheRecords(t *testing.T) {
|
||||
strings.Index(table, "docker.") > strings.Index(table, "`mesh-module-assigner.`") {
|
||||
t.Errorf("not the machines' seats, then modules, then the mesh's:\n%s", table)
|
||||
}
|
||||
// What the guard here named most comes first: its row, and in its row its verb.
|
||||
m.Refused = map[string]int{"node-service-manager.journal": 3, "mesh-module-assigner.assignments": 1}
|
||||
ranked := InsteadOf(&m)
|
||||
if !strings.Contains(ranked, "| `<node>/node-service-manager.` | `journal` — `journalctl` · `status`") ||
|
||||
strings.Index(ranked, "`mesh-module-assigner.`") > strings.Index(ranked, "`<node>/docker.`") {
|
||||
t.Errorf("not ranked by the guard's record:\n%s", ranked)
|
||||
// No guard on the session's shell: the rule is the session's own, and the text claims no guard (the operator,
|
||||
// 2026-11-20).
|
||||
if strings.Contains(table, "guard") || !strings.Contains(table, "call the tool the table names instead") {
|
||||
t.Errorf("the rule names a guard, or not the tool to call:\n%s", table)
|
||||
}
|
||||
m.Refused = nil
|
||||
if InsteadOf(nil) != "" || InsteadOf(&MeshTools{}) != "" {
|
||||
t.Error("a table from nothing")
|
||||
}
|
||||
@@ -204,21 +213,17 @@ func TestTheInsteadOfTableIsGeneratedFromTheRecords(t *testing.T) {
|
||||
// Asked again with the same answer, nothing is written and nothing renders.
|
||||
func TestTheMeshsToolsAreKeptOnlyWhenTheyChange(t *testing.T) {
|
||||
p := Paths{State: t.TempDir()}
|
||||
m, _ := ReadMeshToolsFrom(aMesh(t), someSeats(), resolving)
|
||||
m, _ := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats())
|
||||
if !KeepMeshTools(p, m) {
|
||||
t.Fatal("the first answer was not kept")
|
||||
}
|
||||
if KeepMeshTools(p, m) {
|
||||
t.Fatal("the same answer was kept again")
|
||||
}
|
||||
m.Machines = m.Machines[:1]
|
||||
if !KeepMeshTools(p, m) || len(ReadMeshTools(p).Machines) != 1 {
|
||||
m.Replacements = m.Replacements[:1]
|
||||
if !KeepMeshTools(p, m) || len(ReadMeshTools(p).Replacements) != 1 {
|
||||
t.Fatal("a changed answer was not kept")
|
||||
}
|
||||
d := GuardDataOf(Paths{State: p.State, Node: "laptop"}, ReadMeshTools(p))
|
||||
if d.Node != "laptop" || len(d.Replacements) != 4 || !strings.HasSuffix(d.Log, "guard.log") {
|
||||
t.Errorf("the guard's data: %+v", d)
|
||||
}
|
||||
}
|
||||
|
||||
// Where a module runs is read from the seat mesh-module-assigner's assignments (issue 1126 B13), never the controller's answer.
|
||||
@@ -241,7 +246,7 @@ func TestTheMachinesRunningAModuleAreReadFromAssignments(t *testing.T) {
|
||||
func TestTheMeshIsReadFromTheOwnersFirstViews(t *testing.T) {
|
||||
bus := aMesh(t)
|
||||
r := newMeshViews()
|
||||
for _, name := range []string{ModulesRead, ManifestsRead, AssignmentsRead} {
|
||||
for _, name := range []string{AssignmentsRead} {
|
||||
state := strings.TrimPrefix(name, strings.Split(name, ".")[0]+".")
|
||||
values := map[string]json.RawMessage{}
|
||||
for k, v := range bus[name] {
|
||||
@@ -255,16 +260,20 @@ func TestTheMeshIsReadFromTheOwnersFirstViews(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
want, err := ReadMeshToolsFrom(bus, someSeats(), resolving)
|
||||
want, err := ReadMeshToolsFrom(bus, aCatalog(), someSeats())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := ReadMeshToolsFrom(viewStates{r}, someSeats(), resolving)
|
||||
got, err := ReadMeshToolsFrom(viewStates{r}, aCatalog(), someSeats())
|
||||
if err != nil || !reflect.DeepEqual(got, want) || len(got.Replacements) == 0 {
|
||||
t.Fatalf("from the views: %+v, %v\nfrom the state: %+v", got, err, want)
|
||||
}
|
||||
// A reader not yet answered says so, and reads nothing.
|
||||
if _, err := ReadMeshToolsFrom(viewStates{newMeshViews()}, someSeats(), resolving); err == nil || !strings.Contains(err.Error(), "no first view") {
|
||||
// A catalog half with no first view says so, and reads no module as absent.
|
||||
if _, err := ReadMeshToolsFrom(bus, fakeCatalog{notYet: errors.New("the catalog half has no first view of main yet")}, someSeats()); err == nil || !strings.Contains(err.Error(), "no first view") {
|
||||
t.Fatalf("an unanswered catalog half: %v", err)
|
||||
}
|
||||
if _, err := ReadMeshToolsFrom(viewStates{newMeshViews()}, aCatalog(), someSeats()); err == nil || !strings.Contains(err.Error(), "no first view") {
|
||||
t.Fatalf("an unanswered first view: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -272,7 +281,7 @@ func TestTheMeshIsReadFromTheOwnersFirstViews(t *testing.T) {
|
||||
// Issue 1126 B13 AC1 (ADR 0590): over the seats the mesh defines, the rendered instructions, their table among them,
|
||||
// name no address of the controller, which is not placed: each job is named by the seat that owns it now.
|
||||
func TestTheRenderedInstructionsNameNoControllerAddress(t *testing.T) {
|
||||
m, err := ReadMeshToolsFrom(aMesh(t), someSeats(), resolving)
|
||||
m, err := ReadMeshToolsFrom(aMesh(t), aCatalog(), someSeats())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -9,5 +9,5 @@ func instructionsText(node, role string) string {
|
||||
node +
|
||||
"`\n- **Role:** " +
|
||||
role +
|
||||
"\n- The other nodes, their roles and what runs where: ask the seat `mesh-module-assigner` (`mesh-module-assigner.assignments`),\n or `mesh_overview` and `mesh_machine`. Nothing here lists them, because a copy drifts.\n\n## How a session on this mesh works\n\nThe console is the only way to the mesh: the MCP server named `mesh`. It offers five tools, and\neverything else is an address you find and call through them:\n\n- `mesh_search` — words in, matching addresses out. `mesh_describe` — one address's arguments.\n- `mesh_call` — call an address. A seat the mesh holds once is `<seat>.<verb>` (the mesh's own seats\n include `mesh-module-assigner`, `mesh-delivery`, `mesh-config` and `mesh-conditions`);\n a module on a machine is `<node>/<module>.<tool>`.\n- `mesh_overview` and `mesh_machine` — the mesh's seats and machines, and what one machine runs.\n\n- **Symptom first.** For an error, a failing service or anything unexpected, search the record with the\n literal text before forming a hypothesis: the records module's `records_search`, then\n `records_read`.\n- **Ask the mesh before changing it**, and change it through the seats' verbs or the catalog.\n- **A licence** through the `anthropic-licence-manager` seat's verbs. Never edit the agent's credentials\n file by hand, never print or ask for a token.\n\n## Hard rules\n\n- A file the mesh manages is changed through the verb or the catalog that owns it, never on disk. If\n unsure, `<node>/node-engine.composition` for the module says what the mesh writes there.\n- Never write to a store's database by hand; schema changes are numbered migrations.\n- Never push to a main branch: a branch, a pull request, and a human approval for every merge.\n- The mesh creates no symlinks, and nobody else does either.\n- A package is declared in a module, never installed by hand.\n\n## Conventions\n\n- Commit messages are concise, in the imperative, about why.\n- Test before pushing: nodes update unattended.\n- The playbooks in the record say how research, decisions, designs, issues and hand-offs are done.\n"
|
||||
"\n- The other nodes, their roles and what runs where: ask the seat `mesh-module-assigner` (`mesh-module-assigner.assignments`),\n or `mesh_overview` and `mesh_machine`. Nothing here lists them, because a copy drifts.\n\n## How a session on this mesh works\n\nThe console is the only way to the mesh: the MCP server named `mesh`. It offers five tools, and\neverything else is an address you find and call through them:\n\n- `mesh_search` — words in, matching addresses out. `mesh_describe` — one address's arguments.\n- `mesh_call` — call an address. A seat the mesh holds once is `<seat>.<verb>` (the mesh's own seats\n include `mesh-module-assigner`, `mesh-delivery`, `mesh-config` and `mesh-conditions`);\n a module on a machine is `<node>/<module>.<tool>`.\n- `mesh_overview` and `mesh_machine` — the mesh's seats and machines, and what one machine runs.\n\n- **Symptom first.** For an error, a failing service or anything unexpected, search the record with the\n literal text before forming a hypothesis: the records module's `records_search`, then\n `records_read`.\n- **Ask the mesh before changing it**, and change it through the seats' verbs or the catalog.\n- **A licence** through the `anthropic-licence-manager` seat's verbs. Never edit the agent's credentials\n file by hand, never print or ask for a token.\n\n## Hard rules\n\n- A file the mesh manages is changed through the verb or the catalog that owns it, never on disk. If\n unsure, `<node>/node-engine.composition` for the module says what the mesh writes there.\n- Never write to a store's database by hand; schema changes are numbered migrations.\n- Never push to a main branch: a branch and a pull request for every change.\n- The mesh creates no symlinks, and nobody else does either.\n- A package is declared in a module, never installed by hand.\n\n## Conventions\n\n- Commit messages are concise, in the imperative, about why.\n- Test before pushing: nodes update unattended.\n- The playbooks in the record say how research, decisions, designs, issues and hand-offs are done.\n"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The operator channel's notify, which notifyOperator calls, is in the manifest's invokes, which the grants part reads per
|
||||
// verb (design 47 G GR8): without it the call is refused live.
|
||||
func TestTheManifestInvokesTheNotifyTheModuleCalls(t *testing.T) {
|
||||
raw, err := os.ReadFile("../../module.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(raw), `"seat:operator-channel.notify"`) {
|
||||
t.Error("the manifest does not invoke seat:operator-channel.notify")
|
||||
}
|
||||
}
|
||||
@@ -20,11 +20,13 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient"
|
||||
condseat "git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions"
|
||||
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
||||
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
||||
catalogclient "git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
"git.novox.be/novox/mesh-sdk/go/seatlist"
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
@@ -217,12 +219,19 @@ func nodesRunningMe() ([]string, error) { return NodesRunning(viewStates{meshVie
|
||||
// holds them equal).
|
||||
const (
|
||||
assignerModule = "mesh-delivery-assigner"
|
||||
indexerModule = "mesh-delivery-indexer"
|
||||
)
|
||||
|
||||
// The seat verbs the follower of the watched branches asks (design 47 G §614), as the manifest's `invokes` names them.
|
||||
const (
|
||||
RevisionVerb = "seat:mesh-catalog.revision"
|
||||
RegistrationsVerb = "seat:mesh-catalog.registrations"
|
||||
SourceVerb = "seat:mesh-delivery.source"
|
||||
)
|
||||
|
||||
// meshCatalog is this module's reading of which modules exist: the catalog half, set where the reader is built.
|
||||
var meshCatalog CatalogHalf
|
||||
|
||||
const (
|
||||
ModulesTool = indexerModule + ".indexer_read_modules"
|
||||
ManifestsTool = indexerModule + ".indexer_read_manifests"
|
||||
AssignmentsTool = assignerModule + ".assigner_read_assignments"
|
||||
)
|
||||
|
||||
@@ -230,12 +239,22 @@ const (
|
||||
// once of the owner's read tool, then followed on the owner's views family. It reads none of their state.
|
||||
var meshViews *views.Reader
|
||||
|
||||
// newFindingsWriter is this module's finding writer (design 47 G §689, A-16): it states the findings of its views reader and
|
||||
// observes none of its own.
|
||||
func newFindingsWriter() (*views.Writer[condseat.Finding, condseat.FindingCleared, condseat.FindingsStated, condseat.FindingsAskedEvent], error) {
|
||||
return views.NewStdioWriter(views.WriterConfig[condseat.Finding, condseat.FindingCleared, condseat.FindingsStated, condseat.FindingsAskedEvent]{
|
||||
Asked: condseat.SubjectFindingsAsked, Module: "claude-code", State: stdio.State("findings"),
|
||||
Observe: func(context.Context) (views.Observed[condseat.Finding], error) {
|
||||
return views.Observed[condseat.Finding]{}, nil
|
||||
}})
|
||||
}
|
||||
|
||||
// meshChanged is told, without waiting, that a followed state changed: the instructions' table is read again.
|
||||
var meshChanged = make(chan struct{}, 1)
|
||||
|
||||
// newMeshViews builds the reader of those three states; Start asks them.
|
||||
func newMeshViews() *views.Reader {
|
||||
v := &views.Reader{Module: Module, Logf: func(f string, a ...any) { say(f, a...) }, Findings: meshconditionsclient.FindingWriter{State: stdio.State("findings")},
|
||||
v := &views.Reader{Module: Module, Logf: func(f string, a ...any) { say(f, a...) },
|
||||
Acts: handActs{},
|
||||
Changed: func(string, string, json.RawMessage, bool) {
|
||||
select {
|
||||
@@ -243,8 +262,6 @@ func newMeshViews() *views.Reader {
|
||||
default:
|
||||
}
|
||||
}}
|
||||
v.Follow(indexerModule, "modules", ModulesTool)
|
||||
v.Follow(indexerModule, "manifests", ManifestsTool)
|
||||
v.Follow(assignerModule, "assignments", AssignmentsTool)
|
||||
return v
|
||||
}
|
||||
@@ -322,10 +339,10 @@ func tools(p Paths, servers ServerState, view *ServerView, config ConfigState, c
|
||||
Description: "Claude Code on this machine as the mesh configured it: the licence it holds and when its token expires, what it reports holding, the managed files, the MCP servers registered here. Fingerprints only, never a token.",
|
||||
Run: func(map[string]any) (any, error) { return status(p), nil }},
|
||||
{Name: "claude_code_render",
|
||||
Description: "Write Claude Code's managed directory now, from the mesh's facts, this module's settings and the servers registered here — after reading the bus again for what each tool replaces and which machines the mesh has (the instructions' table and the shell's guard).",
|
||||
Description: "Write Claude Code's managed directory now, from the mesh's facts, this module's settings and the servers registered here — after reading the bus again for what each tool replaces (the instructions' table).",
|
||||
Run: func(map[string]any) (any, error) {
|
||||
answer := map[string]any{}
|
||||
if m, err := ReadMeshToolsFrom(viewStates{meshViews}, definedSeats(), lookup); err != nil {
|
||||
if m, err := ReadMeshToolsFrom(viewStates{meshViews}, meshCatalog, definedSeats()); err != nil {
|
||||
answer["asked"] = "the bus could not be read in full (" + err.Error() + "); what was kept is rendered"
|
||||
} else {
|
||||
KeepMeshTools(p, m)
|
||||
@@ -334,18 +351,8 @@ func tools(p Paths, servers ServerState, view *ServerView, config ConfigState, c
|
||||
answer["rendered"] = out
|
||||
return answer, err
|
||||
}},
|
||||
{Name: "claude_code_guard",
|
||||
Description: "The guard on the agent's shell on this machine (novox/hq ADR 0245): what it refuses — ssh to a mesh machine, writing /etc/hosts or /etc/resolv.conf, HOSTALIASES — the machines and the replaced commands it judges with, the \"instead of\" table rendered into the agent's instructions, and its record of what it refused and what the operator's override let through, newest last.",
|
||||
Input: map[string]any{"last": map[string]any{"type": "integer", "description": "how many lines of the record (default 50)"}},
|
||||
Run: func(a map[string]any) (any, error) {
|
||||
last := 50
|
||||
if n, ok := a["last"].(float64); ok && n > 0 {
|
||||
last = int(n)
|
||||
}
|
||||
return GuardStatus(p, last), nil
|
||||
}},
|
||||
{Name: "claude_code_pull",
|
||||
Description: "Ask the licence manager for this node's current token now and apply it, rather than waiting for its binding to change.",
|
||||
Description: "Ask the licence manager which licence this node holds and the vault for its access token now, and apply it, rather than waiting for its binding or the token to change.",
|
||||
Run: func(map[string]any) (any, error) { return Pull(p, ask, writeManaged) }},
|
||||
{Name: "claude_code_grant",
|
||||
Description: "For the licence manager (ADR 0206): the full grant in this node's credentials file — a login made here — sealed to the public key given, with the account it belongs to. Nothing when no login is waiting. Never answers a token in the clear.",
|
||||
@@ -435,10 +442,6 @@ func persist(what string, attempt func() error, done func(refusals int)) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
// The guard on the agent's shell (novox/hq ADR 0245): this binary, run by the agent's hook.
|
||||
if len(os.Args) == 3 && os.Args[1] == "guard" {
|
||||
os.Exit(runGuard(os.Args[2], os.Stdin, os.Stderr))
|
||||
}
|
||||
// The plugin's SessionStart hook (novox/hq ADR 0489 §1, issue 1744): names the session's id and its open waits.
|
||||
if len(os.Args) == 3 && os.Args[1] == "session-start" {
|
||||
os.Exit(runSessionStart(os.Args[2], os.Stdin, os.Stdout))
|
||||
@@ -459,6 +462,20 @@ func main() {
|
||||
}
|
||||
p, launched := PathsFrom(os.Getenv)
|
||||
meshViews = newMeshViews()
|
||||
branches, err := catalogclient.FollowBranches(meshViews, func(key string, body any) (json.RawMessage, error) { return stdio.Ask(key, body) })
|
||||
if err != nil {
|
||||
say("%v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
branches.Logf = say
|
||||
// A change in a branch's half is told as any followed state's: the instructions' table is read again.
|
||||
branches.Changed = func(string, string, json.RawMessage, bool, time.Time) {
|
||||
select {
|
||||
case meshChanged <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
meshCatalog = branches
|
||||
if launched {
|
||||
fsys = FilesFor(p)
|
||||
}
|
||||
@@ -488,6 +505,20 @@ func main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
findings, err := newFindingsWriter()
|
||||
if err != nil {
|
||||
say("%v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
meshViews.Findings = findings
|
||||
// the runtime answers the writer's subscription only once Serve has its channel
|
||||
go func() {
|
||||
<-stdio.Served()
|
||||
if err := findings.Start(context.Background()); err != nil {
|
||||
say("%v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
meshViews.Start(context.Background())
|
||||
go refreshMeshTools(p)
|
||||
servers := stateOf{stdio.State("servers")}
|
||||
@@ -567,15 +598,24 @@ func run(p Paths, view *ServerView, configView *ConfigView) {
|
||||
// every change of the credentials file, polled, because the file is replaced by rename and a watch on
|
||||
// the old inode would go quiet. Fingerprints and expiries only.
|
||||
holdings := stdio.State("holdings")
|
||||
reported := ""
|
||||
report := func() {
|
||||
var sayMu sync.Mutex
|
||||
sayer := &HoldingsSayer{
|
||||
Put: func(h Holdings) error { _, err := holdings.Put(p.Node, h); return err },
|
||||
Emit: func(h Holdings) error { return stdio.Emit(HoldingsChanged, h) },
|
||||
}
|
||||
sayHoldings := func(again bool) {
|
||||
sayMu.Lock()
|
||||
defer sayMu.Unlock()
|
||||
now := HoldingsOf(p)
|
||||
raw, _ := json.Marshal(now)
|
||||
if string(raw) == reported {
|
||||
return
|
||||
}
|
||||
persist("reporting what this node holds", func() error { _, err := holdings.Put(p.Node, now); return err }, func(int) {
|
||||
reported = string(raw)
|
||||
said := false
|
||||
persist("reporting what this node holds", func() error {
|
||||
var err error
|
||||
said, err = sayer.Say(now, again)
|
||||
return err
|
||||
}, func(int) {
|
||||
if !said {
|
||||
return
|
||||
}
|
||||
account := "no account"
|
||||
if now.Identity != nil && now.Identity.EmailAddress != "" {
|
||||
account = now.Identity.EmailAddress
|
||||
@@ -593,30 +633,43 @@ func run(p Paths, view *ServerView, configView *ConfigView) {
|
||||
say("%s", line)
|
||||
})
|
||||
}
|
||||
|
||||
// What this node should hold (ADR 0206): the manager's `bindings` key for this node; a newer
|
||||
// generation is fetched with the seat's `current`, sealed to this module's key.
|
||||
go persist("watching this node's licence binding", func() error {
|
||||
return stdio.State(Manager+".bindings").Watch(p.Node, func(c stdio.StateChange) error {
|
||||
if c.Key != p.Node {
|
||||
return nil
|
||||
}
|
||||
var b *BindingState
|
||||
if c.Op == "put" {
|
||||
b = &BindingState{}
|
||||
if err := json.Unmarshal(c.Value, b); err != nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if done, err := OnBinding(p, b, ask, writeManaged); err != nil {
|
||||
say("fetching this node's token failed: %v", err)
|
||||
} else if done != "" {
|
||||
say("%s", done)
|
||||
}
|
||||
go report()
|
||||
report := func() { sayHoldings(false) }
|
||||
// The licence manager asks every node to say what it holds again (§23): after its carry-over, at its start and on
|
||||
// its resync.
|
||||
go persist("hearing the licence manager ask what this node holds", func() error {
|
||||
return stdio.Subscribe(HoldingsAsked, func(stdio.Envelope) error {
|
||||
go sayHoldings(true)
|
||||
return nil
|
||||
})
|
||||
}, func(n int) { say("watching this node's licence binding%s", refusals(n)) })
|
||||
}, func(n int) { say("hearing %s%s", HoldingsAsked, refusals(n)) })
|
||||
|
||||
// What this node should hold (design 39 bus-and-switch §19, §22): the manager's binding.changed naming this node's
|
||||
// consumer, and its first view, the seat's bindings for this consumer, asked once each time the events are heard
|
||||
// anew (the start, or a reconnect); the token is asked from the vault at that first view and on a newer generation.
|
||||
applied := func(done string, err error) {
|
||||
if err != nil {
|
||||
say("fetching this node's token failed: %v", err)
|
||||
} else if done != "" {
|
||||
say("%s", done)
|
||||
}
|
||||
go report()
|
||||
}
|
||||
// Adopted by this module's views reader (design 47 G A-134): the reader hears binding.changed.<consumer> and asks the
|
||||
// first view once its subscription is taken, and raises and clears that view's findings through the module's writer.
|
||||
if err := meshViews.Adopt(bindingsFollower(p, ask, writeManaged, applied), Manager, BindingChangedEvent,
|
||||
func(_ context.Context, body []byte) error {
|
||||
applied(OnBindingChanged(p, body, ask, writeManaged))
|
||||
return nil
|
||||
}); err != nil {
|
||||
say("this node's licence binding is not followed: %v", err)
|
||||
}
|
||||
// The token of the licence this node holds, asked again when the vault says its access secret changed (§19).
|
||||
go persist("hearing the vault's changes of this node's licence token", func() error {
|
||||
return stdio.Subscribe(SecretChanged, func(e stdio.Envelope) error {
|
||||
applied(OnSecretChanged(p, e.Body, ask, writeManaged))
|
||||
return nil
|
||||
})
|
||||
}, func(n int) { say("hearing %s%s", SecretChanged, refusals(n)) })
|
||||
|
||||
report()
|
||||
var last string
|
||||
@@ -667,15 +720,12 @@ func refusals(n int) string {
|
||||
return fmt.Sprintf(" (after %d refusal(s))", n)
|
||||
}
|
||||
|
||||
// seatListAgainCallers may read the seat list again: every operator's session and the healers (ADR 0636 §7).
|
||||
var seatListAgainCallers = []string{stdio.AnyPerson, "mesh-healers"}
|
||||
|
||||
// seatListTools are the seat list reader's two tools: what the list holds and what it waits for, and a read now. The
|
||||
// reader starts once the runtime serves, after the tools are listed, so each tool finds it when it is called.
|
||||
// seatListTools are the seat list reader's two tools: what the list holds and what it waits for, and a read now, each
|
||||
// answering every caller (design 47 H §752, H-A-66). The reader starts once the runtime serves, after the tools are
|
||||
// listed, so each tool finds it when it is called.
|
||||
func seatListTools() []stdio.Tool {
|
||||
names := []string{"claude_code_seat_list", "claude_code_seat_list_again"}
|
||||
callers := seatListAgainCallers
|
||||
shape := seatlist.Tools(&seatlist.Reader{}, names[0], names[1], callers)
|
||||
shape := seatlist.Tools(&seatlist.Reader{}, names[0], names[1])
|
||||
for i := range shape {
|
||||
run := i
|
||||
shape[i].Run = func(args map[string]any) (any, error) {
|
||||
@@ -685,7 +735,7 @@ func seatListTools() []stdio.Tool {
|
||||
if r == nil {
|
||||
return nil, errors.New("the seat list reader has not started yet: the runtime has not served this module")
|
||||
}
|
||||
return seatlist.Tools(r, names[0], names[1], callers)[run].Run(args)
|
||||
return seatlist.Tools(r, names[0], names[1])[run].Run(args)
|
||||
}
|
||||
}
|
||||
return shape
|
||||
|
||||
@@ -1,26 +1,19 @@
|
||||
package main
|
||||
|
||||
// What the mesh says about its own tools and machines, as the agent's instructions and the guard on its
|
||||
// shell need it (novox/hq ADR 0245): every seat verb and module tool that says what shell command it
|
||||
// replaces, and the mesh's machines by every name and address a session could reach one by.
|
||||
// What the mesh says about its own tools, as the agent's instructions need it (novox/hq ADR 0245): every seat verb
|
||||
// and module tool that says what shell command it replaces.
|
||||
//
|
||||
// Read from the bus at start and every few minutes (issue 1126 B13; the controller that answered this before is
|
||||
// Read from the bus at start and whenever a followed state changes (issue 1126 B13; the controller that answered this before is
|
||||
// not placed, novox/hq ADR 0590): the seats' verbs from the seats' contract (mesh-contracts), the modules'
|
||||
// own tools from the manifests the indexer keeps (`mesh-delivery-indexer.modules` and `.manifests`), and the
|
||||
// machines and where each module runs from the seat mesh-module-assigner's `assignments`. Kept in the module's state so
|
||||
// own tools from the manifests the indexer keeps (`mesh-delivery-indexer.modules` and `.manifests`), and where
|
||||
// each module runs from the seat mesh-module-assigner's `assignments`. Kept in the module's state so
|
||||
// a render never waits for the bus, and rendered whenever it changes. Nothing here is written by hand: a verb
|
||||
// that gains a `replaces` is in the next render's table and the guard's next refusal.
|
||||
// that gains a `replaces` is in the next render's table.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
assignerclient "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient"
|
||||
mesh "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
|
||||
catalogclient "git.novox.be/novox/mesh-delivery/contracts/go/clients/meshcatalogclient"
|
||||
catalog "git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
@@ -28,6 +21,9 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
assignerclient "git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient"
|
||||
mesh "git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/catalogue"
|
||||
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
||||
"git.novox.be/novox/mesh-sdk/go/seatlist"
|
||||
@@ -46,39 +42,13 @@ type Replacement struct {
|
||||
On []string `json:"on,omitempty"`
|
||||
}
|
||||
|
||||
// Machine is one of the mesh's machines and the names and addresses it is reached by.
|
||||
type Machine struct {
|
||||
Name string `json:"name"`
|
||||
Domains []string `json:"domains,omitempty"`
|
||||
Addresses []string `json:"addresses,omitempty"`
|
||||
}
|
||||
|
||||
// MeshTools is what the bus said, as kept in the module's state.
|
||||
type MeshTools struct {
|
||||
Replacements []Replacement `json:"replacements"`
|
||||
Machines []Machine `json:"machines"`
|
||||
// Asked is when the bus was last read whole.
|
||||
Asked string `json:"asked,omitempty"`
|
||||
// Refused is how often the guard here named each tool instead of a work-around, from its record: the
|
||||
// table's order. Never kept — read from the record at each render.
|
||||
Refused map[string]int `json:"-"`
|
||||
}
|
||||
|
||||
// RefusedCounts are how often the guard's record named each tool, over its last lines.
|
||||
func RefusedCounts(log string) map[string]int {
|
||||
out := map[string]int{}
|
||||
for _, e := range ReadGuardLog(log, 2000) {
|
||||
for _, t := range e.Tools {
|
||||
out[t]++
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// InternalSuffix is the mesh's own names' domain: every machine and service is `<name>.internal`
|
||||
// (novox/hq ADR 0194), and a session reaches none of them by ssh.
|
||||
const InternalSuffix = ".internal"
|
||||
|
||||
func (p Paths) meshTools() string { return filepath.Join(p.State, "mesh-tools.json") }
|
||||
|
||||
// ReadMeshTools is what was kept, or nil.
|
||||
@@ -93,8 +63,6 @@ func ReadMeshTools(p Paths) *MeshTools {
|
||||
// The states read, each declared in the manifest's reads.
|
||||
const (
|
||||
AssignmentsRead = (assignerModule + "." + mesh.StateAssignments)
|
||||
ModulesRead = indexerModule + "." + catalog.StateModules
|
||||
ManifestsRead = indexerModule + "." + catalog.StateManifests
|
||||
)
|
||||
|
||||
// States reads the module states the manifest declares in its reads: a state's keys, and one key's value.
|
||||
@@ -152,44 +120,28 @@ func NodesRunning(st States, module string) ([]string, error) {
|
||||
return runsOn(placed)[module], nil
|
||||
}
|
||||
|
||||
// modulesTools is every placed module's own tools that replace a command, by module, from the manifest the indexer
|
||||
// keeps of it. A module whose manifest cannot be read is left out and said in the error.
|
||||
func modulesTools(st States, on map[string][]string) ([]ModuleTools, error) {
|
||||
// modulesTools is every placed module's own tools that replace a command, by module, from the manifest its catalog
|
||||
// graph entry carries. A module whose manifest cannot be read is left out and said in the error.
|
||||
func modulesTools(cat CatalogHalf, on map[string][]string) ([]ModuleTools, error) {
|
||||
var out []ModuleTools
|
||||
var problems []error
|
||||
if err := cat.Ready(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
modules := make([]string, 0, len(on))
|
||||
for m := range on {
|
||||
modules = append(modules, m)
|
||||
}
|
||||
sort.Strings(modules)
|
||||
for _, module := range modules {
|
||||
key, err := catalogclient.ModulesKey(module)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
raw, found, err := st.Get(ModulesRead, key)
|
||||
entry, found, err := cat.Entry(module)
|
||||
if err != nil || !found {
|
||||
if err != nil {
|
||||
problems = append(problems, fmt.Errorf("%s %s: %w", ModulesRead, module, err))
|
||||
problems = append(problems, fmt.Errorf("the catalog half's entry of %s: %w", module, err))
|
||||
}
|
||||
continue
|
||||
}
|
||||
m, err := catalogclient.DecodeModule(raw)
|
||||
if err != nil || m.Manifest == "" {
|
||||
continue
|
||||
}
|
||||
mkey, err := catalogclient.ManifestsKey(m.Manifest)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
raw, found, err = st.Get(ManifestsRead, mkey)
|
||||
if err != nil || !found {
|
||||
if err != nil {
|
||||
problems = append(problems, fmt.Errorf("%s of %s: %w", ManifestsRead, module, err))
|
||||
}
|
||||
continue
|
||||
}
|
||||
kept, err := catalogclient.DecodeManifest(raw)
|
||||
kept, err := json.Marshal(entry.Manifest)
|
||||
if err != nil {
|
||||
problems = append(problems, fmt.Errorf("the manifest of %s: %w", module, err))
|
||||
continue
|
||||
@@ -197,7 +149,7 @@ func modulesTools(st States, on map[string][]string) ([]ModuleTools, error) {
|
||||
var man struct {
|
||||
Replaces map[string][]string `json:"replaces"`
|
||||
}
|
||||
if json.Unmarshal([]byte(kept.Manifest), &man) != nil || len(man.Replaces) == 0 {
|
||||
if json.Unmarshal(kept, &man) != nil || len(man.Replaces) == 0 {
|
||||
continue
|
||||
}
|
||||
out = append(out, ModuleTools{Module: module, On: on[module], Replaces: man.Replaces})
|
||||
@@ -253,43 +205,16 @@ func ReplacementsOf(defined []seatdef.Definition, modules []ModuleTools) []Repla
|
||||
return out
|
||||
}
|
||||
|
||||
// lookup resolves one name, briefly: a name that does not resolve here is no address.
|
||||
func lookup(name string) []string {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
addrs, _ := net.DefaultResolver.LookupHost(ctx, name)
|
||||
return addrs
|
||||
}
|
||||
|
||||
// ReadMeshToolsFrom reads the bus now: the seats' verbs, the placed modules' own tools, and every node assignments names
|
||||
// with its domains and addresses. A part that cannot be read is left out and said in the error; what was read is kept.
|
||||
func ReadMeshToolsFrom(st States, defined []seatdef.Definition, resolve func(string) []string) (MeshTools, error) {
|
||||
// ReadMeshToolsFrom reads the bus now: the seats' verbs and the placed modules' own tools. A part that cannot be read is left out and said in the error; what was read is kept.
|
||||
func ReadMeshToolsFrom(st States, cat CatalogHalf, defined []seatdef.Definition) (MeshTools, error) {
|
||||
var m MeshTools
|
||||
placed, err := assignments(st)
|
||||
if err != nil {
|
||||
m.Replacements = ReplacementsOf(defined, nil)
|
||||
return m, err
|
||||
}
|
||||
modules, merr := modulesTools(st, runsOn(placed))
|
||||
modules, merr := modulesTools(cat, runsOn(placed))
|
||||
m.Replacements = ReplacementsOf(defined, modules)
|
||||
for node, p := range placed {
|
||||
machine := Machine{Name: node, Domains: []string{node + InternalSuffix}}
|
||||
if d := strings.ToLower(strings.TrimSpace(p.Decided.PublicDomain)); d != "" {
|
||||
machine.Domains = append(machine.Domains, d)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, name := range append([]string{node}, machine.Domains...) {
|
||||
for _, a := range resolve(name) {
|
||||
if !seen[a] && !net.ParseIP(a).IsLoopback() {
|
||||
seen[a] = true
|
||||
machine.Addresses = append(machine.Addresses, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(machine.Addresses)
|
||||
m.Machines = append(m.Machines, machine)
|
||||
}
|
||||
sort.Slice(m.Machines, func(i, j int) bool { return m.Machines[i].Name < m.Machines[j].Name })
|
||||
return m, merr
|
||||
}
|
||||
|
||||
@@ -314,22 +239,9 @@ func KeepMeshTools(p Paths, m MeshTools) bool {
|
||||
return os.Rename(tmp, p.meshTools()) == nil
|
||||
}
|
||||
|
||||
// GuardDataOf is what the guard judges with on this machine.
|
||||
func GuardDataOf(p Paths, m *MeshTools) GuardData {
|
||||
d := GuardData{Node: p.Node, Log: filepath.Join(p.State, "guard.log"), Machines: []Machine{}, Replacements: []Replacement{}}
|
||||
if p.Confined() {
|
||||
// The session's account cannot write the module's state, and must not rewrite its own record (ADR 0266).
|
||||
d.Agent, d.Log = p.Agent, JournalRecord+GuardJournalID
|
||||
}
|
||||
if m != nil {
|
||||
d.Machines, d.Replacements = m.Machines, m.Replacements
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// guardFiles is the guard to place: this module's own executable and what it judges with. None where the
|
||||
// hookFiles is what the plugin's hooks run: this module's own executable and the waits directory. None where the
|
||||
// executable cannot be named — then no hook is written, rather than one that cannot run.
|
||||
func guardFiles(p Paths, m *MeshTools) *GuardFiles {
|
||||
func hookFiles(p Paths) *HookFiles {
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil
|
||||
@@ -337,18 +249,13 @@ func guardFiles(p Paths, m *MeshTools) *GuardFiles {
|
||||
if resolved, err := filepath.EvalSymlinks(exe); err == nil {
|
||||
exe = resolved
|
||||
}
|
||||
return &GuardFiles{Exe: exe, Data: GuardDataOf(p, m), WaitsDir: WaitsDirPath(p)}
|
||||
return &HookFiles{Exe: exe, WaitsDir: WaitsDirPath(p)}
|
||||
}
|
||||
|
||||
// MeshToolsEvery is how often the machines' addresses are resolved again; the tools and the assignments are read again
|
||||
// when their followed states change (a views event), not on this timer.
|
||||
const MeshToolsEvery = 10 * time.Minute
|
||||
|
||||
// MeshToolsFirst is how long the first read waits for the first views to be answered.
|
||||
const MeshToolsFirst = 2 * time.Minute
|
||||
|
||||
// refreshMeshTools reads the pictures once the first views are answered, and again whenever one changes (and the
|
||||
// machines' addresses every few minutes), and renders when what it said changed.
|
||||
// refreshMeshTools reads the pictures once the first views are answered, and again whenever one changes, and renders when what it said changed.
|
||||
func refreshMeshTools(p Paths) {
|
||||
select {
|
||||
case <-meshViews.Ready():
|
||||
@@ -357,60 +264,26 @@ func refreshMeshTools(p Paths) {
|
||||
}
|
||||
startSeatList(seatlist.StdioBus())
|
||||
for {
|
||||
m, err := ReadMeshToolsFrom(viewStates{meshViews}, definedSeats(), lookup)
|
||||
m, err := ReadMeshToolsFrom(viewStates{meshViews}, meshCatalog, definedSeats())
|
||||
if err != nil {
|
||||
say("reading what each tool replaces and which machines the mesh has: %v", err)
|
||||
say("reading what each tool replaces: %v", err)
|
||||
}
|
||||
if (err == nil || len(m.Replacements) > 0) && KeepMeshTools(p, m) {
|
||||
if _, err := RenderNow(p, writeManaged); err != nil {
|
||||
say("rendering what the bus said of the tools: %v", err)
|
||||
} else {
|
||||
say("the mesh's tools changed: %d replace a command, %d machines; rendered", len(m.Replacements), len(m.Machines))
|
||||
say("the mesh's tools changed: %d replace a command; rendered", len(m.Replacements))
|
||||
}
|
||||
}
|
||||
<-meshChanged
|
||||
time.Sleep(2 * time.Second) // a burst of events is one read
|
||||
select {
|
||||
case <-meshChanged:
|
||||
time.Sleep(2 * time.Second) // a burst of events is one read
|
||||
select {
|
||||
case <-meshChanged:
|
||||
default:
|
||||
}
|
||||
case <-time.After(MeshToolsEvery):
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GuardStatus is what claude_code_guard answers.
|
||||
func GuardStatus(p Paths, last int) map[string]any {
|
||||
m := ReadMeshTools(p)
|
||||
d := GuardDataOf(p, m)
|
||||
asked := "never: the bus has not been read whole yet"
|
||||
if m != nil {
|
||||
asked = m.Asked
|
||||
}
|
||||
return map[string]any{
|
||||
"refuses": []string{
|
||||
"ssh, scp, sftp, rsync, mosh or autossh to a mesh machine: its name, a name under its domains, any *" + InternalSuffix +
|
||||
" name or one of its addresses, read as ssh reads it (ssh -G); a jump through one too. An ssh login as `" +
|
||||
ForgeUser + "` is the forge's account and passes",
|
||||
"writing /etc/hosts or /etc/resolv.conf, by the shell or the agent's Edit and Write",
|
||||
"HOSTALIASES, named anywhere in a command",
|
||||
"any command naming " + OverrideVar,
|
||||
"where the machine names an agent account of its own (novox/hq ADR 0266): every command and file edit of a " +
|
||||
"session that does not run as it, whatever it is, and no override lifts it",
|
||||
},
|
||||
"agent_account": map[bool]string{true: p.Agent, false: "none of its own: agents run as the operator's account"}[p.Confined()],
|
||||
"override": OverrideVar + "=<why>, set in the operator's own shell before the session starts; read from the session's " +
|
||||
"environment as it was started, recorded with why, and not honoured when it cannot be recorded",
|
||||
"hook": GuardCommand(),
|
||||
"asked": asked,
|
||||
"machines": d.Machines,
|
||||
"replacements": d.Replacements,
|
||||
"instead_of": InsteadOf(m),
|
||||
"record": ReadGuardLog(d.Log, last),
|
||||
}
|
||||
}
|
||||
|
||||
// seatList is the seat list this module holds: the definitions of the seats, read from the seat mesh-catalog and
|
||||
// followed on its events (novox/hq ADR 0635 §1, ADR 0636), the foundation's bootstrapped ones until it answers.
|
||||
var seatList *seatlist.Reader
|
||||
|
||||
@@ -10,17 +10,21 @@ package main
|
||||
// fingerprints and expiries — never a token. Written at start and on every change of the credentials
|
||||
// file, so the licence manager learns a login, or a node already logged in, from the state alone.
|
||||
// - The grant itself leaves only when the manager asks `claude_code_grant`, sealed to the key it gives.
|
||||
// - What this node should hold is the manager's `bindings` state; a newer generation for this node is
|
||||
// fetched with the seat's `current` verb, sealed to this module's key, and written access-token-only.
|
||||
// - What this node should hold is the manager's binding, heard as `binding.changed` and asked at start; the
|
||||
// seat's `current` says which licence, and its `access` secret is asked from the vault, sealed to this
|
||||
// module's key, at start, on a newer generation and on the vault's `secret-changed` for it, and written
|
||||
// access-token-only (design 39 bus-and-switch §19).
|
||||
// - An MCP server registered through this module is a key in its `servers` state — `all.<server>` for
|
||||
// every node, `<node>.<server>` for one — which every node watches.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@@ -28,14 +32,27 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/views"
|
||||
)
|
||||
|
||||
// Seat is the licence manager's role, and Manager the module whose `bindings` state this one reads.
|
||||
// Seat is the licence manager's role, and Manager the module whose events this one consumes.
|
||||
const (
|
||||
Seat = "anthropic-licence-manager"
|
||||
Manager = "claude-licence-manager"
|
||||
)
|
||||
|
||||
// VaultGet is the vault's verb a licence's access secret is asked by, and SecretChanged the vault's event that it
|
||||
// changed (design 39 bus-and-switch §19, B9).
|
||||
const (
|
||||
VaultGet = "seat:mesh-vault.get"
|
||||
SecretChanged = "mesh-vault.secret-changed"
|
||||
)
|
||||
|
||||
// AccessSecret is the name of a licence's access secret in the vault, by the licence's key as the manager says it
|
||||
// (§15, B7: the key is carried, never formed here).
|
||||
func AccessSecret(key string) string { return "licence." + key + ".access" }
|
||||
|
||||
// SeatVerb is a seat's verb as the runtime addresses it: a role, not a module.
|
||||
func SeatVerb(verb string) string { return "seat:" + Seat + "." + verb }
|
||||
|
||||
@@ -68,8 +85,8 @@ func PathsFrom(env func(string) string) (Paths, bool) {
|
||||
Agent: env("MESH_AGENT_ACCOUNT"), Operator: env("MESH_OPERATOR_ACCOUNT"),
|
||||
AgentHome: env("MESH_AGENT_HOME"), OperatorHome: env("MESH_OPERATOR_HOME")}
|
||||
if (p.Home == "") != (p.Agent == "") {
|
||||
// Half of the agent's account is not "the operator's": taken so, the agent's account rule of the guard
|
||||
// would be off and its home never written, and nothing would say so. Not launched, said (main).
|
||||
// Half of the agent's account is not "the operator's": taken so, the agent's
|
||||
// home would never be written, and nothing would say so. Not launched, said (main).
|
||||
return p, false
|
||||
}
|
||||
if p.Home == "" {
|
||||
@@ -127,6 +144,7 @@ func HomeScopeRefused(p Paths, target string) string {
|
||||
func (p Paths) credentials() string { return filepath.Join(p.Home, ".claude", ".credentials.json") }
|
||||
func (p Paths) account() string { return filepath.Join(p.Home, ".claude.json") }
|
||||
func (p Paths) binding() string { return filepath.Join(p.State, "licence.json") }
|
||||
func (p Paths) handed() string { return filepath.Join(p.State, "login-handed") }
|
||||
func (p Paths) apiKey() string { return filepath.Join(p.keyDir(), "api-key") }
|
||||
func (p Paths) helper() string { return filepath.Join(p.keyDir(), "api-key-helper") }
|
||||
|
||||
@@ -240,11 +258,8 @@ func RenderNow(p Paths, write WriteManaged) ([]string, error) {
|
||||
config.Approved = ApprovedHere(approved, p.Node)
|
||||
}
|
||||
mesh := ReadMeshTools(p)
|
||||
if mesh != nil {
|
||||
mesh.Refused = RefusedCounts(GuardDataOf(p, mesh).Log)
|
||||
}
|
||||
files := RenderWithMesh(facts, settings, binding, p.helperCommand(), Registered(p), config, mesh)
|
||||
tree, _ := json.Marshal(MarketplaceWith(config, guardFiles(p, mesh)))
|
||||
tree, _ := json.Marshal(MarketplaceWith(config, hookFiles(p)))
|
||||
files[MarketplaceDir+"/"] = string(tree)
|
||||
names := make([]string, 0, len(files))
|
||||
for n := range files {
|
||||
@@ -309,13 +324,14 @@ type BindingState struct {
|
||||
By string `json:"by,omitempty"`
|
||||
}
|
||||
|
||||
// Current is what the seat answers to `current`: the licence this node is bound to and its token, sealed.
|
||||
// Current is what the seat answers to `current`: the licence this node is bound to, its key and its account, never
|
||||
// its token (§18, B7).
|
||||
type Current struct {
|
||||
Licence string `json:"licence"`
|
||||
Kind string `json:"kind"`
|
||||
Generation int64 `json:"generation"`
|
||||
Sealed *SealedBox `json:"sealed"`
|
||||
Identity *Identity `json:"identity"`
|
||||
Licence string `json:"licence"`
|
||||
Key string `json:"key"`
|
||||
Kind string `json:"kind"`
|
||||
Generation int64 `json:"generation"`
|
||||
Identity *Identity `json:"identity"`
|
||||
}
|
||||
|
||||
// Holdings is what this node holds, as the `holdings` state carries it (ADR 0206): enough for the manager
|
||||
@@ -401,43 +417,70 @@ func GrantFor(p Paths, managerPublicKey string) (GrantAnswer, error) {
|
||||
if err != nil {
|
||||
return GrantAnswer{}, err
|
||||
}
|
||||
// Which login was handed: only its successor may replace it (ADR 0206, ADR 0209). The fingerprint, never
|
||||
// the token.
|
||||
if err := fsys.MkdirAll(p.State, 0o700); err != nil {
|
||||
return GrantAnswer{}, err
|
||||
}
|
||||
if err := fsys.WriteFile(p.handed(), []byte(Fingerprint(rt)+"\n"), 0o600); err != nil {
|
||||
return GrantAnswer{}, err
|
||||
}
|
||||
return GrantAnswer{Sealed: &box, Identity: ReadIdentity(p.account()), Fingerprint: Fingerprint(rt)}, nil
|
||||
}
|
||||
|
||||
// Pull asks the seat for this node's current token and applies it.
|
||||
// readHanded is the fingerprint of the login last handed to the manager, or "".
|
||||
func readHanded(p Paths) string {
|
||||
raw, err := fsys.ReadFile(p.handed())
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(string(raw))
|
||||
}
|
||||
|
||||
// Pull asks the seat which licence this node holds, then the vault for that licence's access secret, sealed to this
|
||||
// module's key, and applies it (§19).
|
||||
func Pull(p Paths, ask Ask, write WriteManaged) (map[string]any, error) {
|
||||
keys, err := Keypair(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
raw, err := ask(SeatVerb("current"), map[string]any{"consumer": p.Node, "public_key": keys.PublicKey})
|
||||
raw, err := ask(SeatVerb("current"), map[string]any{"consumer": p.Node})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var c Current
|
||||
if err := json.Unmarshal(raw, &c); err != nil || c.Sealed == nil {
|
||||
if err := json.Unmarshal(raw, &c); err != nil || c.Licence == "" || c.Key == "" {
|
||||
return map[string]any{"applied": false, "reason": "the seat holds no licence for this node"}, nil
|
||||
}
|
||||
return Apply(p, c, write)
|
||||
raw, err = ask(VaultGet, map[string]any{"name": AccessSecret(c.Key), "public_key": keys.PublicKey})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var got struct {
|
||||
Sealed *SealedBox `json:"sealed"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &got); err != nil || got.Sealed == nil {
|
||||
return nil, fmt.Errorf("the vault answered no sealed %s", AccessSecret(c.Key))
|
||||
}
|
||||
return Apply(p, c, *got.Sealed, write)
|
||||
}
|
||||
|
||||
// OnBinding takes a change to this node's key in the manager's `bindings` state (ADR 0206): the token is
|
||||
// fetched when the binding differs from the one applied. A released binding keeps the last token, which
|
||||
// lives hours, and says so.
|
||||
//
|
||||
// **Differs, not "is newer"** (novox/hq issue 243). The state keeps only the latest value per node, so
|
||||
// nothing older can arrive. A manager whose store was rebuilt counts generations from one again, and
|
||||
// a node that waited for a number above its own ignored every binding it was sent, its login and the
|
||||
// licence's rotations included, until the count caught up. Only the binding already applied is skipped.
|
||||
// OnBinding takes this node's binding as the manager said it: the token is fetched when its generation is newer than
|
||||
// the one applied (§19). A home that holds no token of the applied kind has nothing applied, so it is fetched too. A
|
||||
// released binding keeps the last token, which lives hours, and says so.
|
||||
func OnBinding(p Paths, b *BindingState, ask Ask, write WriteManaged) (string, error) {
|
||||
if b == nil {
|
||||
return "this node's binding was released; it keeps its last token until it expires", nil
|
||||
}
|
||||
var applied Binding
|
||||
if readJSON(p.binding(), &applied) && applied.Generation == b.Generation && applied.Licence == b.Licence &&
|
||||
holdsApplied(p, applied.Kind) {
|
||||
if readJSON(p.binding(), &applied) && b.Generation <= applied.Generation && holdsApplied(p, applied.Kind) {
|
||||
return "", nil
|
||||
}
|
||||
return fetched(p, b, ask, write)
|
||||
}
|
||||
|
||||
// fetched pulls this node's token for binding b and says the line naming it.
|
||||
func fetched(p Paths, b *BindingState, ask Ask, write WriteManaged) (string, error) {
|
||||
out, err := Pull(p, ask, write)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -455,14 +498,104 @@ func OnBinding(p Paths, b *BindingState, ask Ask, write WriteManaged) (string, e
|
||||
return string(raw), nil
|
||||
}
|
||||
|
||||
// Apply applies what the seat handed over. A switch replaces the grant whole and cleans up after the old
|
||||
// licence; whatever it is, the file is written without a refresh token, so the agent here never refreshes.
|
||||
func Apply(p Paths, c Current, write WriteManaged) (map[string]any, error) {
|
||||
// BindingChanged is the manager's event of a binding written or deleted (design 39 bus-and-switch §21), which this
|
||||
// module consumes (§22).
|
||||
const BindingChanged = Manager + "." + BindingChangedEvent
|
||||
|
||||
// BindingChangedEvent is that event's name as the manager says it, with the consumer as its further part
|
||||
// (`binding.changed.<consumer>`, design 47 G A-134).
|
||||
const BindingChangedEvent = "binding.changed"
|
||||
|
||||
// bindingsFollower is this node's first view of its licence binding, adopted by the module's views reader (A-134): its
|
||||
// ask is FirstBinding, whose outcome goes to applied; the picture keeps no binding, which the node's own files hold.
|
||||
func bindingsFollower(p Paths, ask Ask, write WriteManaged, applied func(string, error)) *views.Follower {
|
||||
return &views.Follower{Owner: Manager, Tool: SeatVerb("bindings"), State: "bindings",
|
||||
Ask: func(context.Context, string) (views.Answer, error) {
|
||||
done, err := FirstBinding(p, ask, write)
|
||||
if err != nil {
|
||||
return views.Answer{}, err
|
||||
}
|
||||
applied(done, nil)
|
||||
return views.Answer{Owner: Manager, State: "bindings", Values: map[string]json.RawMessage{}, At: time.Now()}, nil
|
||||
}}
|
||||
}
|
||||
|
||||
// bindingOf is a consumer's binding as the manager says it, in binding.changed and in the verb bindings' answer: an
|
||||
// empty licence is a release (B22: decoded from a copy of the writer's form, testdata/binding-changed.json).
|
||||
type bindingOf struct {
|
||||
Consumer string `json:"consumer"`
|
||||
BindingState
|
||||
}
|
||||
|
||||
func (b bindingOf) state() *BindingState {
|
||||
if b.Licence == "" {
|
||||
return nil
|
||||
}
|
||||
return &b.BindingState
|
||||
}
|
||||
|
||||
// OnBindingChanged takes one binding.changed: acted on only when it names this node's consumer (§22).
|
||||
func OnBindingChanged(p Paths, body []byte, ask Ask, write WriteManaged) (string, error) {
|
||||
var b bindingOf
|
||||
if err := json.Unmarshal(body, &b); err != nil {
|
||||
return "", fmt.Errorf("%s is not a binding's change: %w", BindingChanged, err)
|
||||
}
|
||||
if b.Consumer != p.Node {
|
||||
return "", nil
|
||||
}
|
||||
return OnBinding(p, b.state(), ask, write)
|
||||
}
|
||||
|
||||
// FirstBinding asks the seat's verb bindings for this node's consumer, its first view (§22, B10), and applies it: at
|
||||
// the module's start, and again whenever it hears the manager's events anew.
|
||||
func FirstBinding(p Paths, ask Ask, write WriteManaged) (string, error) {
|
||||
raw, err := ask(SeatVerb("bindings"), map[string]any{"consumer": p.Node})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var listed []bindingOf
|
||||
if err := json.Unmarshal(raw, &listed); err != nil {
|
||||
return "", fmt.Errorf("the seat's bindings answered what is not a list of bindings: %w", err)
|
||||
}
|
||||
for _, b := range listed {
|
||||
if b.Consumer == p.Node && b.state() != nil {
|
||||
// At its start the module asks the vault whatever it applied before (§19).
|
||||
return fetched(p, b.state(), ask, write)
|
||||
}
|
||||
}
|
||||
return OnBinding(p, nil, ask, write)
|
||||
}
|
||||
|
||||
// secretChanged is the vault's `secret-changed` as it says it: the secret's name and generation, never its value.
|
||||
type secretChanged struct {
|
||||
Name string `json:"name"`
|
||||
Generation int64 `json:"generation"`
|
||||
}
|
||||
|
||||
// OnSecretChanged takes one `secret-changed`: acted on only when it names the access secret of the licence this node
|
||||
// holds, which is then asked again (§19).
|
||||
func OnSecretChanged(p Paths, body []byte, ask Ask, write WriteManaged) (string, error) {
|
||||
var e secretChanged
|
||||
if err := json.Unmarshal(body, &e); err != nil {
|
||||
return "", fmt.Errorf("%s is not a secret's change: %w", SecretChanged, err)
|
||||
}
|
||||
var applied Binding
|
||||
if !readJSON(p.binding(), &applied) || applied.Key == "" || e.Name != AccessSecret(applied.Key) {
|
||||
return "", nil
|
||||
}
|
||||
return fetched(p, &BindingState{Licence: applied.Licence, Kind: applied.Kind, Generation: applied.Generation,
|
||||
Why: "the vault's " + e.Name + " changed"}, ask, write)
|
||||
}
|
||||
|
||||
// Apply applies the access secret the vault handed over for the licence the seat named. A switch replaces the grant
|
||||
// whole and cleans up after the old licence; whatever it is, the file is written without a refresh token, so the
|
||||
// agent here never refreshes.
|
||||
func Apply(p Paths, c Current, sealed SealedBox, write WriteManaged) (map[string]any, error) {
|
||||
keys, err := Keypair(p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
plain, err := Open(*c.Sealed, keys.PrivateKey)
|
||||
plain, err := Open(sealed, keys.PrivateKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -492,9 +625,37 @@ func Apply(p Paths, c Current, write WriteManaged) (map[string]any, error) {
|
||||
return nil, err
|
||||
}
|
||||
local := ReadCredentials(p.credentials())
|
||||
// A login waiting here was handed to the manager first (ADR 0206): what comes back is its successor,
|
||||
// and the refresh token in the file is the one the manager just spent.
|
||||
// A login made here that the manager has not adopted yet stays whole: the grant handed is the old
|
||||
// lineage's, and writing it would spend the person's login unseen (ADR 0206, ADR 0209). The manager
|
||||
// asks claude_code_grant for it on this node's report; its successor then comes as a newer generation.
|
||||
// This return skips the binding's generation below on purpose: later binding events pull again until
|
||||
// the manager adopts the login.
|
||||
// The latest login wins (design 39 §6): a lease of the same account that succeeds the login (H-A-139) —
|
||||
// the manager adopted a later login to that account from another node — replaces it, said as "login
|
||||
// superseded"; a login to another account than the lease's licence is always kept (H-A-140, hq
|
||||
// design/47-H1-answers-59 b75d1386).
|
||||
superseded := false
|
||||
if LoginNotHanded(local, readHanded(p)) {
|
||||
id := ReadIdentity(p.account())
|
||||
if id == nil || id.EmailAddress == "" || id.EmailAddress != c.Licence || !Succeeds(local, g) {
|
||||
log.Printf("login kept: the login in the credentials file was not handed to the manager yet (licence %s)", c.Licence)
|
||||
return map[string]any{"applied": false, "licence": c.Licence, "reason": LoginKept}, nil
|
||||
}
|
||||
superseded = true
|
||||
log.Printf("login superseded: a later login to the account of licence %s was adopted by the manager (generation %d)", c.Licence, c.Generation)
|
||||
}
|
||||
// A login handed to the manager is replaced only by a grant of a newer lineage (its refresh token's
|
||||
// expiry later than the login's, or the same lineage with a later access expiry, as the manager's refresh yields it): a lease older than the login is never
|
||||
// written, so the manager's failed refresh is a finding on the manager, never a lost login here
|
||||
// (H-A-138, hq design/47-H1-answers-59 175ed320; H-A-139, e359911e).
|
||||
if HoldsLogin(local) && !Succeeds(local, g) {
|
||||
log.Printf("login kept: the grant offered for licence %s does not succeed the login in the credentials file", c.Licence)
|
||||
return map[string]any{"applied": false, "licence": c.Licence, "reason": LoginKept}, nil
|
||||
}
|
||||
d := DecideApply(GrantOf(local), g, switched || HoldsLogin(local))
|
||||
if d.Apply && superseded {
|
||||
out["reason"] = "login superseded"
|
||||
}
|
||||
if d.Apply {
|
||||
next := WithGrant(local, g)
|
||||
if switched {
|
||||
@@ -522,7 +683,7 @@ func Apply(p Paths, c Current, write WriteManaged) (map[string]any, error) {
|
||||
if gen == 0 {
|
||||
gen = previous.Generation
|
||||
}
|
||||
raw, _ := json.Marshal(Binding{Licence: c.Licence, Kind: c.Kind, Generation: gen})
|
||||
raw, _ := json.Marshal(Binding{Licence: c.Licence, Key: c.Key, Kind: c.Kind, Generation: gen})
|
||||
if err := fsys.WriteFile(p.binding(), append(raw, '\n'), 0o600); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -906,10 +906,6 @@ func Propose(p Paths, in ProposeInput, store ProposalStore, bus AskBus, now time
|
||||
if why := approverReady(bus); why != "" {
|
||||
return refuse(why + "; " + AtTheTerminalInstead)
|
||||
}
|
||||
if waiting := waitingProposals(store, approved, now); waiting >= occ.MostOpen {
|
||||
return refuse(fmt.Sprintf("%d proposals already wait for the operator's answer, and the operator is asked at most %d "+
|
||||
"questions at once; claude_code_proposals lists them", waiting, occ.MostOpen))
|
||||
}
|
||||
if problem := ProposalProblem(prop); problem != "" {
|
||||
return refuse(problem)
|
||||
}
|
||||
@@ -956,26 +952,6 @@ func Propose(p Paths, in ProposeInput, store ProposalStore, bus AskBus, now time
|
||||
}, nil
|
||||
}
|
||||
|
||||
// waitingProposals counts the proposals still open: asked, not decided here, not expired.
|
||||
func waitingProposals(store ProposalStore, approved Approved, now time.Time) int {
|
||||
keys, err := store.Keys()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
n := 0
|
||||
for _, k := range keys {
|
||||
prop, ok := readProposal(store, k)
|
||||
if !ok || prop.Asked != "" {
|
||||
continue
|
||||
}
|
||||
if _, decided := approved.Decided[prop.ID]; decided || now.After(prop.At.Add(ProposalLasts)) {
|
||||
continue
|
||||
}
|
||||
n++
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func readProposal(store ProposalStore, key string) (Proposal, bool) {
|
||||
raw, found, err := store.Get(key)
|
||||
if err != nil || !found {
|
||||
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -38,26 +37,8 @@ const QuestionsState = "questions"
|
||||
// QuestionPrefix starts every question's key, so the router's word on one is never taken for a proposal's.
|
||||
const QuestionPrefix = "q-"
|
||||
|
||||
// MostQuestions is how many questions may wait at once, mesh-wide: the router allows an asker three open asks
|
||||
// (occ.MostOpen), and questions alone never fill them, so one is left for a proposed instruction section (ADR 0274)
|
||||
// while two questions are open. Each open question holds one slot key in the `questions` state (slotKey), taken by
|
||||
// create or by compare-and-set, so two nodes asking at once cannot both take the last one.
|
||||
const MostQuestions = occ.MostOpen - 1
|
||||
|
||||
// A question the router holds no record of, and whose refusal was not heard here, keeps its slot until it expires:
|
||||
// the router may still have it in its queue (down, or backlogged), and neither the queue's order nor a cancel can
|
||||
// take it back — the queue offers a piece of work again after an error, so a cancel may be taken before the ask it
|
||||
// cancels, and a cancel of an ask not yet recorded is dropped. Once the question has expired the router refuses it
|
||||
// when it is taken (oc.Ask.Check), so the slot is free and the question has ended.
|
||||
|
||||
// slotKey is the key of one of the MostQuestions slots: not a question's key, so no question is ever read from it.
|
||||
func slotKey(n int) string { return fmt.Sprintf("slot-%d", n) }
|
||||
|
||||
// slot is what a slot key holds: the question holding it.
|
||||
type slot struct {
|
||||
Question string `json:"question"`
|
||||
At time.Time `json:"at"`
|
||||
}
|
||||
// A question is never refused for how many wait: the router offers an asker at most three asks at once and holds the
|
||||
// oldest back behind newer ones, offered again when one ends (novox/hq ADR 0666 §12), so no question is hidden here.
|
||||
|
||||
// The bounds of a question's life (ADR 0332 §2): at least five minutes, at most a day less ten minutes (the
|
||||
// router's bound for an approval, with the margin a proposal keeps), an hour when none is given.
|
||||
@@ -372,19 +353,9 @@ func AskQuestion(p Paths, in QuestionInput, store QuestionStore, bus AskBus, now
|
||||
if err := occ.Check(ask, now); err != nil {
|
||||
return refuse(err.Error())
|
||||
}
|
||||
// The question is kept before its slot is taken, so a slot always names a question another node can judge.
|
||||
if err := store.Create(q.ID, q); err != nil {
|
||||
return nil, fmt.Errorf("the question could not be kept, so the operator was not asked: %w", err)
|
||||
}
|
||||
if open, err := takeSlot(store, bus, q, now); err != nil {
|
||||
_ = store.Put(q.ID, tombstone(q, "not asked: "+err.Error()))
|
||||
return refuse(err.Error())
|
||||
} else if open != nil {
|
||||
_ = store.Put(q.ID, tombstone(q, "not asked: two questions were open"))
|
||||
return refuse(fmt.Sprintf("%d questions already wait for the operator's answer (%s), and sessions hold at most %d "+
|
||||
"at once, so one of the router's three open asks is left for a proposed instruction section; read them with "+
|
||||
"claude_code_answer, or wait for one to end", len(open), strings.Join(open, ", "), MostQuestions))
|
||||
}
|
||||
if err := bus.Publish(oc.AcceptSubjectAsk(Module), ask, ask.ID); err != nil {
|
||||
_ = store.Put(q.ID, tombstone(q, "not asked: "+err.Error()))
|
||||
return nil, fmt.Errorf("the operator could not be asked: %v", err)
|
||||
@@ -420,75 +391,6 @@ func labelsOf(q Question) string {
|
||||
return strings.Join(out, ", ")
|
||||
}
|
||||
|
||||
// questionOpen says whether a question still waits: kept, asked, not ended, not expired, and open in the router's
|
||||
// record or not recorded yet. An unreadable record is open: a slot is never freed on a guess.
|
||||
func questionOpen(store QuestionStore, bus AskBus, id string, now time.Time) (bool, error) {
|
||||
q, ok, err := readQuestionErr(store, id)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
if !ok || q.Asked != "" || q.Ended != "" || !now.Before(q.Expires) {
|
||||
return false, nil
|
||||
}
|
||||
entry, err := bus.Record(q.ID)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
if entry == nil {
|
||||
return true, nil
|
||||
}
|
||||
var rec routerRecord
|
||||
if json.Unmarshal(entry.Value, &rec) != nil {
|
||||
return true, nil
|
||||
}
|
||||
return rec.State == "" || rec.State == "open", nil
|
||||
}
|
||||
|
||||
// takeSlot takes one of the MostQuestions slots for q: a slot no key holds, by create, or one whose question no longer
|
||||
// waits, by compare-and-set at the revision read. Of two nodes taking the same slot only one wins; the other tries
|
||||
// the next. It answers the open questions when every slot is held, and an error — refusing, never guessing — when a
|
||||
// slot or its question cannot be read.
|
||||
func takeSlot(store QuestionStore, bus AskBus, q Question, now time.Time) ([]string, error) {
|
||||
mine := slot{Question: q.ID, At: q.At}
|
||||
var open []string
|
||||
for n := 1; n <= MostQuestions; n++ {
|
||||
key := slotKey(n)
|
||||
raw, rev, found, err := store.Get(key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("the open questions could not be counted (%v), so none more is asked", err)
|
||||
}
|
||||
if !found {
|
||||
if err := store.Create(key, mine); err == nil {
|
||||
return nil, nil
|
||||
} else if !errors.Is(err, stdio.ErrChanged) {
|
||||
return nil, fmt.Errorf("the open questions could not be counted (%v), so none more is asked", err)
|
||||
}
|
||||
if raw, rev, found, err = store.Get(key); err != nil || !found {
|
||||
return nil, fmt.Errorf("the open questions could not be counted, so none more is asked")
|
||||
}
|
||||
}
|
||||
var held slot
|
||||
if json.Unmarshal(raw, &held) == nil && held.Question != "" {
|
||||
waits, err := questionOpen(store, bus, held.Question, now)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("the open questions could not be counted (%v), so none more is asked", err)
|
||||
}
|
||||
if waits {
|
||||
open = append(open, held.Question)
|
||||
continue
|
||||
}
|
||||
}
|
||||
if err := store.Update(key, mine, rev); err == nil {
|
||||
return nil, nil
|
||||
} else if !errors.Is(err, stdio.ErrChanged) {
|
||||
return nil, fmt.Errorf("the open questions could not be counted (%v), so none more is asked", err)
|
||||
}
|
||||
open = append(open, "one taken just now")
|
||||
}
|
||||
sort.Strings(open)
|
||||
return open, nil
|
||||
}
|
||||
|
||||
// tombstone is what is kept of a question that was not asked or that the router refused: its key, who asked, when, and
|
||||
// why it ended — none of its words, which the router judged unfit to leave the mesh or never saw.
|
||||
func tombstone(q Question, ended string) Question {
|
||||
@@ -608,7 +510,7 @@ func ReadAnswer(store QuestionStore, bus AskBus, key string, now time.Time) (map
|
||||
case entry == nil:
|
||||
out["state"] = AnswerWaiting
|
||||
out["words"] = "not yet recorded by the router: it may still be in the router's queue, or was refused without the " +
|
||||
"refusal being heard here. It holds its slot, and the operator may still be asked, until it expires at " +
|
||||
"refusal being heard here. The operator may still be asked until it expires at " +
|
||||
q.Expires.Format(time.RFC3339)
|
||||
case entry == nil, rec.State == "open", rec.Warrant == nil:
|
||||
out["state"] = AnswerWaiting
|
||||
|
||||
@@ -6,7 +6,6 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -182,22 +181,21 @@ func TestAQuestionIsRefusedOutsideItsBounds(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAtMostTwoQuestionsWaitSoAProposalFindsRoom(t *testing.T) {
|
||||
// ADR 0666 §12: a question is never refused for how many wait; the router holds the oldest back, offered again when one ends.
|
||||
func TestAThirdQuestionIsAskedNotRefusedAndAProposalIsAskedBesideThem(t *testing.T) {
|
||||
p, _ := node(t, "g14")
|
||||
store, r := newMemQuestions(), newRouter()
|
||||
first := askMergeTonight(t, p, store, r, t0)
|
||||
askMergeTonight(t, p, store, r, t0)
|
||||
if _, err := AskQuestion(p, mergeTonight(), store, r, t0); err == nil || !strings.Contains(err.Error(), "2 questions already wait") {
|
||||
t.Fatalf("a third question: %v", err)
|
||||
for i := 0; i < 4; i++ {
|
||||
if _, err := AskQuestion(p, mergeTonight(), store, r, t0); err != nil {
|
||||
t.Fatalf("question %d: %v", i+1, err)
|
||||
}
|
||||
}
|
||||
r.close(r.chose(first, "o1", t0.Add(time.Minute)))
|
||||
if _, err := AskQuestion(p, mergeTonight(), store, r, t0.Add(2*time.Minute)); err != nil {
|
||||
t.Fatalf("a question once one was answered: %v", err)
|
||||
if len(r.subjects) != 4 {
|
||||
t.Errorf("the router was asked %d times, want 4", len(r.subjects))
|
||||
}
|
||||
// A proposal is still asked beside two waiting questions.
|
||||
pstore := &memProposals{m: map[string]json.RawMessage{}}
|
||||
if _, err := Propose(p, ProposeInput{Scope: ScopeMesh, Name: "x", Text: "Text.\n"}, pstore, r, t0); err != nil {
|
||||
t.Errorf("a proposal beside two questions: %v", err)
|
||||
t.Errorf("a proposal beside the questions: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -234,9 +232,6 @@ func TestTheRoutersRefusalIsSaidAndKept(t *testing.T) {
|
||||
if err != nil || out["state"] != AnswerEnded || !strings.Contains(out["words"].(string), "refused") {
|
||||
t.Errorf("read after the refusal: %v %v", out, err)
|
||||
}
|
||||
if waits, err := questionOpen(store, refusing, refusing.id, t0.Add(time.Minute)); waits || err != nil {
|
||||
t.Errorf("a refused question still counts as waiting: %v %v", waits, err)
|
||||
}
|
||||
var kept Question
|
||||
_ = json.Unmarshal(store.m[refusing.id], &kept)
|
||||
if kept.Headline != "" || kept.Context != "" || len(kept.Options) != 0 || kept.Why != "" || kept.OnExpiry != "" {
|
||||
@@ -316,10 +311,6 @@ type memQuestions struct {
|
||||
m map[string]json.RawMessage
|
||||
rev map[string]uint64
|
||||
n uint64
|
||||
// failGet fails every read of a key with this prefix.
|
||||
failGet string
|
||||
// afterGet runs after a read, outside the lock: another node writing between a read and a write.
|
||||
afterGet func(key string)
|
||||
}
|
||||
|
||||
func newMemQuestions() *memQuestions {
|
||||
@@ -328,17 +319,9 @@ func newMemQuestions() *memQuestions {
|
||||
|
||||
func (s *memQuestions) Get(key string) (json.RawMessage, uint64, bool, error) {
|
||||
s.mu.Lock()
|
||||
if s.failGet != "" && strings.HasPrefix(key, s.failGet) {
|
||||
s.mu.Unlock()
|
||||
return nil, 0, false, errors.New("the bus did not answer")
|
||||
}
|
||||
defer s.mu.Unlock()
|
||||
raw, ok := s.m[key]
|
||||
rev, after := s.rev[key], s.afterGet
|
||||
s.mu.Unlock()
|
||||
if after != nil {
|
||||
after(key)
|
||||
}
|
||||
return raw, rev, ok, nil
|
||||
return raw, s.rev[key], ok, nil
|
||||
}
|
||||
func (s *memQuestions) put(key string, value any) error {
|
||||
raw, err := json.Marshal(value)
|
||||
@@ -397,121 +380,3 @@ func TestAWarrantForAnotherAskOrOptionIsNotProven(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Two nodes asking at the same moment take two slots at most: of three sessions racing, one is refused.
|
||||
func TestTheCapHoldsAcrossNodesAskingAtOnce(t *testing.T) {
|
||||
store, r := newMemQuestions(), &lockedRouter{router: newRouter()}
|
||||
nodes := []string{"g14", "shanks", "ace"}
|
||||
var wg sync.WaitGroup
|
||||
errs := make([]error, len(nodes))
|
||||
for i, name := range nodes {
|
||||
p, _ := node(t, name)
|
||||
wg.Add(1)
|
||||
go func(i int, p Paths) {
|
||||
defer wg.Done()
|
||||
in := mergeTonight()
|
||||
in.By = "fix-589"
|
||||
_, errs[i] = AskQuestion(p, in, store, r, t0)
|
||||
}(i, p)
|
||||
}
|
||||
wg.Wait()
|
||||
refused := 0
|
||||
for _, err := range errs {
|
||||
if err != nil {
|
||||
if !strings.Contains(err.Error(), "questions already wait") {
|
||||
t.Errorf("refused for another reason: %v", err)
|
||||
}
|
||||
refused++
|
||||
}
|
||||
}
|
||||
if refused != 1 {
|
||||
t.Errorf("%d of three questions asked at once were refused; one is", refused)
|
||||
}
|
||||
}
|
||||
|
||||
// A slot that cannot be read refuses the question: the cap fails closed.
|
||||
func TestTheCapFailsClosed(t *testing.T) {
|
||||
p, _ := node(t, "g14")
|
||||
store, r := newMemQuestions(), newRouter()
|
||||
store.failGet = "slot-"
|
||||
if _, err := AskQuestion(p, mergeTonight(), store, r, t0); err == nil || !strings.Contains(err.Error(), "could not be counted") {
|
||||
t.Fatalf("asked with the slots unreadable: %v", err)
|
||||
}
|
||||
if len(r.subjects) != 0 {
|
||||
t.Error("the operator was asked anyway")
|
||||
}
|
||||
}
|
||||
|
||||
// A question whose refusal was not heard here, and of which the router holds no record, keeps its slot and reads as
|
||||
// waiting until it expires — the router may still take it from its queue — and only then frees its slot and ends.
|
||||
func TestAQuestionTheRouterHasNotRecordedHoldsItsSlotUntilItExpires(t *testing.T) {
|
||||
p, _ := node(t, "g14")
|
||||
store, r := newMemQuestions(), newRouter()
|
||||
first := askMergeTonight(t, p, store, r, t0)
|
||||
askMergeTonight(t, p, store, r, t0)
|
||||
delete(r.records, first) // the router down or backlogged, or its refusal never heard here
|
||||
later := t0.Add(QuestionDefault - time.Minute)
|
||||
if _, err := AskQuestion(p, mergeTonight(), store, r, later); err == nil {
|
||||
t.Fatal("the slot of a question the router may still show was freed before it expired")
|
||||
}
|
||||
out, _ := ReadAnswer(store, r, first, later)
|
||||
if out["state"] != AnswerWaiting || !strings.Contains(out["words"].(string), "not yet recorded") {
|
||||
t.Errorf("before expiry read as %v", out)
|
||||
}
|
||||
expired := t0.Add(QuestionDefault)
|
||||
if out, _ := ReadAnswer(store, r, first, expired); out["state"] != AnswerEnded ||
|
||||
out["now"] != "do what you said: I leave the pull request open" {
|
||||
t.Errorf("at expiry read as %v", out)
|
||||
}
|
||||
if _, err := AskQuestion(p, mergeTonight(), store, r, expired); err != nil {
|
||||
t.Errorf("the slot of an expired question was not freed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A slot is taken only at the revision read: when another node takes both slots between this node's read and its
|
||||
// write, this node's writes are refused and the question is refused — a plain write would take a slot it lost.
|
||||
func TestASlotIsTakenOnlyAtTheRevisionRead(t *testing.T) {
|
||||
p, _ := node(t, "g14")
|
||||
store, r := newMemQuestions(), newRouter()
|
||||
// Two questions that ended: their slots are free to take again.
|
||||
for _, id := range []string{askMergeTonight(t, p, store, r, t0), askMergeTonight(t, p, store, r, t0)} {
|
||||
r.close(oc.Warrant{Ask: id, Outcome: occ.OutcomeExpired, At: occ.FormatTime(t0.Add(time.Minute))})
|
||||
}
|
||||
shanks, _ := node(t, "shanks")
|
||||
other := newRouter()
|
||||
store.afterGet = func(key string) {
|
||||
if !strings.HasPrefix(key, "slot-") {
|
||||
return
|
||||
}
|
||||
// Another node asks between this node's read of the slot and its write: its question waits.
|
||||
q := Question{ID: newQuestionID(), Headline: "Another", Context: "c", Why: "w", OnExpiry: "e", By: "other",
|
||||
From: shanks.Node, At: t0, Expires: t0.Add(time.Hour),
|
||||
Options: []QuestionOption{{ID: "o1", Label: "A", Means: "a", Recommended: true}, {ID: "o2", Label: "B", Means: "b"}}}
|
||||
_ = store.Put(q.ID, q)
|
||||
_ = other.Publish(oc.AcceptSubjectAsk(Module), q.Ask(), q.ID)
|
||||
r.records[q.ID] = other.records[q.ID]
|
||||
_ = store.Put(key, slot{Question: q.ID, At: t0})
|
||||
}
|
||||
_, err := AskQuestion(p, mergeTonight(), store, r, t0.Add(2*time.Minute))
|
||||
store.afterGet = nil
|
||||
if err == nil || !strings.Contains(err.Error(), "questions already wait") {
|
||||
t.Fatalf("a slot lost to another node was taken: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// lockedRouter is the router fake, safe for sessions asking at once.
|
||||
type lockedRouter struct {
|
||||
mu sync.Mutex
|
||||
*router
|
||||
}
|
||||
|
||||
func (l *lockedRouter) Publish(subject string, body any, id string) error {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
return l.router.Publish(subject, body, id)
|
||||
}
|
||||
func (l *lockedRouter) Record(id string) (*stdio.StateEntry, error) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
return l.router.Record(id)
|
||||
}
|
||||
|
||||
@@ -50,7 +50,9 @@ type Settings struct {
|
||||
|
||||
// Binding is the licence this node holds, as it was last applied.
|
||||
type Binding struct {
|
||||
Licence string `json:"licence"`
|
||||
Licence string `json:"licence"`
|
||||
// Key is the licence's key as the manager says it, which names its access secret in the vault (§19, B7).
|
||||
Key string `json:"key,omitempty"`
|
||||
Kind string `json:"kind"` // subscription | api-key
|
||||
Generation int64 `json:"generation,omitempty"`
|
||||
}
|
||||
|
||||
@@ -2,11 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
||||
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
||||
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
||||
)
|
||||
|
||||
// A section registered for the mesh before the controller's terminal took that over, or set at the terminal, is
|
||||
|
||||
@@ -114,7 +114,7 @@ func TestSessionStartRejectsAnUnsafeSessionID(t *testing.T) {
|
||||
}
|
||||
|
||||
// 1744/AC4 is checked together with 1743/AC7 in terminal_only_test.go's
|
||||
// TestThePluginsOnlyHooksAreTheGuardTheSessionStartHookAndTheStopHook.
|
||||
// TestThePluginsOnlyHooksAreTheSessionStartHookAndTheStopHook.
|
||||
|
||||
// ADR 0490 §2: every session start lists every name on this node with an open wait, from the node-wide
|
||||
// index, with the exact adopt call to make if it is yours — read directly, never by calling back into this
|
||||
|
||||
@@ -60,13 +60,12 @@ func waitBlocksStop(session string, w Wait, now time.Time) (bool, string) {
|
||||
"{\"op\":\"said\",\"session\":%q,\"by\":%q,\"id\":%q}.", w.ID, w.What, session, w.By, w.ID)
|
||||
}
|
||||
|
||||
// watcherAdvice is what a session is told to do to follow a wait (ADR 0489 §3, review round 1 item 3): a
|
||||
// family is a candidate for mesh_wait only when the tool runner is granted it (ADR 0367 §2), which this hook
|
||||
// cannot know, so it is offered conditionally; a watcher that rereads the read address always works.
|
||||
// watcherAdvice is what a session is told to do to follow a wait (ADR 0489 §3, design 47 E §20): a wait with an event is
|
||||
// followed by mesh_wait on it, rereading what it reads after every answer, or by a watcher that does; with no event, by a
|
||||
// watcher that rereads the read address. The advice puts no condition on a grant.
|
||||
func watcherAdvice(w Wait) string {
|
||||
if w.Family != "" {
|
||||
return fmt.Sprintf("mesh_wait on %s if this session's tool runner is granted it, otherwise start a watcher "+
|
||||
"that rereads %s", w.Family, w.Read.Address)
|
||||
return fmt.Sprintf("mesh_wait on %s and reread %s after every answer, or start a watcher that does", w.Family, w.Read.Address)
|
||||
}
|
||||
return fmt.Sprintf("start a watcher that rereads %s", w.Read.Address)
|
||||
}
|
||||
|
||||
@@ -300,15 +300,15 @@ func TestStopAllowsWhenTheRefusalCounterCannotBeWritten(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Review round 1, item 3 (1743/AC1, §3 with ADR 0367 §2): a family is offered only conditionally (the tool
|
||||
// runner may not be granted it); without one, the advice is only to start a watcher.
|
||||
func TestWaitBlocksStopAdvisesMeshWaitOnlyConditionallyOtherwiseAWatcher(t *testing.T) {
|
||||
// Design 47 E §20: a wait with an event is followed by mesh_wait on it and a reread after every answer, or a watcher
|
||||
// that does, with no condition on a grant; without an event, the advice is only to start a watcher.
|
||||
func TestWaitBlocksStopAdvisesMeshWaitOnItsEventOtherwiseAWatcher(t *testing.T) {
|
||||
withFamily := openWait("w-f", 10)
|
||||
withFamily.Read = WaitRead{Address: "mesh-delivery.recheck"}
|
||||
withFamily.Family = "mesh-delivery.decided"
|
||||
_, reason := waitBlocksStop("sess-f", withFamily, stopT0.Add(time.Minute))
|
||||
if !strings.Contains(reason, "mesh_wait on mesh-delivery.decided if this session's tool runner is granted it") ||
|
||||
!strings.Contains(reason, "otherwise start a watcher that rereads mesh-delivery.recheck") {
|
||||
if !strings.Contains(reason, "mesh_wait on mesh-delivery.decided and reread mesh-delivery.recheck after every answer, or start a watcher that does") ||
|
||||
strings.Contains(reason, "granted") {
|
||||
t.Fatalf("%s", reason)
|
||||
}
|
||||
|
||||
@@ -320,9 +320,9 @@ func TestWaitBlocksStopAdvisesMeshWaitOnlyConditionallyOtherwiseAWatcher(t *test
|
||||
}
|
||||
}
|
||||
|
||||
// 1743/AC7's rendering half (hooks.json holds the guard's PreToolUse entry and the Stop entry) is checked
|
||||
// 1743/AC7's rendering half (hooks.json holds the Stop entry) is checked
|
||||
// together with 1744/AC4 in terminal_only_test.go's
|
||||
// TestThePluginsOnlyHooksAreTheGuardTheSessionStartHookAndTheStopHook. claude_code_hook_register still
|
||||
// TestThePluginsOnlyHooksAreTheSessionStartHookAndTheStopHook. claude_code_hook_register still
|
||||
// refuses a Stop hook (or any other) at every scope.
|
||||
func TestHookRegisterStillRefusesEveryHook(t *testing.T) {
|
||||
p, _ := node(t, "laptop")
|
||||
|
||||
@@ -261,12 +261,13 @@ func TestTheOperatorsManagedSettingsAreWhatSessionsObey(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The plugin's only hooks are the guard, the session-start hook and the Stop hook (novox/hq ADR 0245, ADR
|
||||
// 0489) — never a hook a registration named, whatever the configuration given holds (novox/hq issue 340).
|
||||
func TestThePluginsOnlyHooksAreTheGuardTheSessionStartHookAndTheStopHook(t *testing.T) {
|
||||
// The plugin's only hooks are the session-start hook and the Stop hook (novox/hq ADR 0489) — no guard on the
|
||||
// shell (the operator, 2026-11-20), and never a hook a registration named, whatever the configuration given holds
|
||||
// (novox/hq issue 340).
|
||||
func TestThePluginsOnlyHooksAreTheSessionStartHookAndTheStopHook(t *testing.T) {
|
||||
c := Config{Mesh: []Item{{Kind: KindHook, Name: "planted", Scope: ScopeMesh, Event: "PreToolUse", Matcher: "Bash",
|
||||
Command: "${HOOK_DIR}/x.sh", Files: map[string]string{"x.sh": "#!/bin/sh\n"}}}}
|
||||
plugin := MarketplaceWith(c, &GuardFiles{Exe: "/bin/true", WaitsDir: "/home/agent/.claude/nox-mesh/waits"})
|
||||
plugin := MarketplaceWith(c, &HookFiles{Exe: "/bin/true", WaitsDir: "/home/agent/.claude/nox-mesh/waits"})
|
||||
root := "plugins/" + Plugin + "/"
|
||||
var hooks struct {
|
||||
Hooks map[string][]struct {
|
||||
@@ -279,9 +280,14 @@ func TestThePluginsOnlyHooksAreTheGuardTheSessionStartHookAndTheStopHook(t *test
|
||||
if err := json.Unmarshal([]byte(plugin[root+"hooks/hooks.json"].Content), &hooks); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(hooks.Hooks) != 3 || len(hooks.Hooks["PreToolUse"]) != 1 || hooks.Hooks["PreToolUse"][0].Matcher != GuardMatcher ||
|
||||
hooks.Hooks["PreToolUse"][0].Hooks[0].Command != GuardCommand() {
|
||||
t.Fatalf("the guard's hook: %s", plugin[root+"hooks/hooks.json"].Content)
|
||||
if len(hooks.Hooks) != 2 || hooks.Hooks["PreToolUse"] != nil {
|
||||
t.Fatalf("a hook beside the session-start and Stop hooks: %s", plugin[root+"hooks/hooks.json"].Content)
|
||||
}
|
||||
if plugin[root+GuardDir+"/guard"].From != "/bin/true" {
|
||||
t.Fatalf("the binary the hooks run is not placed: %+v", plugin[root+GuardDir+"/guard"])
|
||||
}
|
||||
if _, ok := plugin[root+GuardDir+"/guard.json"]; ok {
|
||||
t.Fatal("the guard's data is placed")
|
||||
}
|
||||
if len(hooks.Hooks["SessionStart"]) != 1 || hooks.Hooks["SessionStart"][0].Matcher != "startup|resume|clear|compact" ||
|
||||
hooks.Hooks["SessionStart"][0].Hooks[0].Command != SessionStartCommand("/home/agent/.claude/nox-mesh/waits") {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{"at":"2026-10-04T12:00:00Z","by":"g14.node-tools","consumer":"laptop","from":"acct-1@example.org","generation":3,"key":"api","kind":"api-key","licence":"api","why":"the switch verb"}
|
||||
@@ -32,11 +32,11 @@
|
||||
"withKey": {
|
||||
"managed-mcp.json": "{\n \"mcpServers\": {\n \"anton\": {\n \"type\": \"stdio\",\n \"command\": \"node\",\n \"args\": [\n \"/a/b.js\"\n ],\n \"env\": {}\n },\n \"docs\": {\n \"type\": \"stdio\",\n \"command\": \"docs-mcp\",\n \"args\": [\n \"--x\"\n ]\n },\n \"mesh\": {\n \"type\": \"http\",\n \"url\": \"http://127.0.0.1:4270/mcp\"\n },\n \"search\": {\n \"type\": \"http\",\n \"url\": \"https://s.example/mcp\"\n }\n }\n}\n",
|
||||
"managed-settings.json": "{\n \"attribution\": {\n \"commit\": \"\",\n \"pr\": \"\"\n },\n \"allowAllClaudeAiMcps\": true,\n \"apiKeyHelper\": \"/state/api-key-helper\"\n}\n",
|
||||
"CLAUDE.md": "# This machine is a node of a Novox mesh\n\nWritten by the mesh's `claude-code` module. Edit the module's settings or the catalog, never this file:\nit is rewritten whenever the module renders.\n\n## Who this node is\n\n- **Node:** `workstation`\n- **Role:** the laptop\n- The other nodes, their roles and what runs where: ask the seat `mesh-module-assigner` (`mesh-module-assigner.assignments`),\n or `mesh_overview` and `mesh_machine`. Nothing here lists them, because a copy drifts.\n\n## How a session on this mesh works\n\nThe console is the only way to the mesh: the MCP server named `mesh`. It offers five tools, and\neverything else is an address you find and call through them:\n\n- `mesh_search` — words in, matching addresses out. `mesh_describe` — one address's arguments.\n- `mesh_call` — call an address. A seat the mesh holds once is `<seat>.<verb>` (the mesh's own seats\n include `mesh-module-assigner`, `mesh-delivery`, `mesh-config` and `mesh-conditions`);\n a module on a machine is `<node>/<module>.<tool>`.\n- `mesh_overview` and `mesh_machine` — the mesh's seats and machines, and what one machine runs.\n\n- **Symptom first.** For an error, a failing service or anything unexpected, search the record with the\n literal text before forming a hypothesis: the records module's `records_search`, then\n `records_read`.\n- **Ask the mesh before changing it**, and change it through the seats' verbs or the catalog.\n- **A licence** through the `anthropic-licence-manager` seat's verbs. Never edit the agent's credentials\n file by hand, never print or ask for a token.\n\n## Hard rules\n\n- A file the mesh manages is changed through the verb or the catalog that owns it, never on disk. If\n unsure, `<node>/node-engine.composition` for the module says what the mesh writes there.\n- Never write to a store's database by hand; schema changes are numbered migrations.\n- Never push to a main branch: a branch, a pull request, and a human approval for every merge.\n- The mesh creates no symlinks, and nobody else does either.\n- A package is declared in a module, never installed by hand.\n\n## Conventions\n\n- Commit messages are concise, in the imperative, about why.\n- Test before pushing: nodes update unattended.\n- The playbooks in the record say how research, decisions, designs, issues and hand-offs are done.\n"
|
||||
"CLAUDE.md": "# This machine is a node of a Novox mesh\n\nWritten by the mesh's `claude-code` module. Edit the module's settings or the catalog, never this file:\nit is rewritten whenever the module renders.\n\n## Who this node is\n\n- **Node:** `workstation`\n- **Role:** the laptop\n- The other nodes, their roles and what runs where: ask the seat `mesh-module-assigner` (`mesh-module-assigner.assignments`),\n or `mesh_overview` and `mesh_machine`. Nothing here lists them, because a copy drifts.\n\n## How a session on this mesh works\n\nThe console is the only way to the mesh: the MCP server named `mesh`. It offers five tools, and\neverything else is an address you find and call through them:\n\n- `mesh_search` — words in, matching addresses out. `mesh_describe` — one address's arguments.\n- `mesh_call` — call an address. A seat the mesh holds once is `<seat>.<verb>` (the mesh's own seats\n include `mesh-module-assigner`, `mesh-delivery`, `mesh-config` and `mesh-conditions`);\n a module on a machine is `<node>/<module>.<tool>`.\n- `mesh_overview` and `mesh_machine` — the mesh's seats and machines, and what one machine runs.\n\n- **Symptom first.** For an error, a failing service or anything unexpected, search the record with the\n literal text before forming a hypothesis: the records module's `records_search`, then\n `records_read`.\n- **Ask the mesh before changing it**, and change it through the seats' verbs or the catalog.\n- **A licence** through the `anthropic-licence-manager` seat's verbs. Never edit the agent's credentials\n file by hand, never print or ask for a token.\n\n## Hard rules\n\n- A file the mesh manages is changed through the verb or the catalog that owns it, never on disk. If\n unsure, `<node>/node-engine.composition` for the module says what the mesh writes there.\n- Never write to a store's database by hand; schema changes are numbered migrations.\n- Never push to a main branch: a branch and a pull request for every change.\n- The mesh creates no symlinks, and nobody else does either.\n- A package is declared in a module, never installed by hand.\n\n## Conventions\n\n- Commit messages are concise, in the imperative, about why.\n- Test before pushing: nodes update unattended.\n- The playbooks in the record say how research, decisions, designs, issues and hand-offs are done.\n"
|
||||
},
|
||||
"plain": {
|
||||
"managed-mcp.json": "{\n \"mcpServers\": {\n \"mesh\": {\n \"type\": \"http\",\n \"url\": \"http://127.0.0.1:4270/mcp\"\n }\n }\n}\n",
|
||||
"managed-settings.json": "{\n \"attribution\": {\n \"commit\": \"\",\n \"pr\": \"\"\n },\n \"allowAllClaudeAiMcps\": true\n}\n",
|
||||
"CLAUDE.md": "# This machine is a node of a Novox mesh\n\nWritten by the mesh's `claude-code` module. Edit the module's settings or the catalog, never this file:\nit is rewritten whenever the module renders.\n\n## Who this node is\n\n- **Node:** `workstation`\n- **Role:** not stated — set it in this module's settings for the node\n- The other nodes, their roles and what runs where: ask the seat `mesh-module-assigner` (`mesh-module-assigner.assignments`),\n or `mesh_overview` and `mesh_machine`. Nothing here lists them, because a copy drifts.\n\n## How a session on this mesh works\n\nThe console is the only way to the mesh: the MCP server named `mesh`. It offers five tools, and\neverything else is an address you find and call through them:\n\n- `mesh_search` — words in, matching addresses out. `mesh_describe` — one address's arguments.\n- `mesh_call` — call an address. A seat the mesh holds once is `<seat>.<verb>` (the mesh's own seats\n include `mesh-module-assigner`, `mesh-delivery`, `mesh-config` and `mesh-conditions`);\n a module on a machine is `<node>/<module>.<tool>`.\n- `mesh_overview` and `mesh_machine` — the mesh's seats and machines, and what one machine runs.\n\n- **Symptom first.** For an error, a failing service or anything unexpected, search the record with the\n literal text before forming a hypothesis: the records module's `records_search`, then\n `records_read`.\n- **Ask the mesh before changing it**, and change it through the seats' verbs or the catalog.\n- **A licence** through the `anthropic-licence-manager` seat's verbs. Never edit the agent's credentials\n file by hand, never print or ask for a token.\n\n## Hard rules\n\n- A file the mesh manages is changed through the verb or the catalog that owns it, never on disk. If\n unsure, `<node>/node-engine.composition` for the module says what the mesh writes there.\n- Never write to a store's database by hand; schema changes are numbered migrations.\n- Never push to a main branch: a branch, a pull request, and a human approval for every merge.\n- The mesh creates no symlinks, and nobody else does either.\n- A package is declared in a module, never installed by hand.\n\n## Conventions\n\n- Commit messages are concise, in the imperative, about why.\n- Test before pushing: nodes update unattended.\n- The playbooks in the record say how research, decisions, designs, issues and hand-offs are done.\n"
|
||||
"CLAUDE.md": "# This machine is a node of a Novox mesh\n\nWritten by the mesh's `claude-code` module. Edit the module's settings or the catalog, never this file:\nit is rewritten whenever the module renders.\n\n## Who this node is\n\n- **Node:** `workstation`\n- **Role:** not stated — set it in this module's settings for the node\n- The other nodes, their roles and what runs where: ask the seat `mesh-module-assigner` (`mesh-module-assigner.assignments`),\n or `mesh_overview` and `mesh_machine`. Nothing here lists them, because a copy drifts.\n\n## How a session on this mesh works\n\nThe console is the only way to the mesh: the MCP server named `mesh`. It offers five tools, and\neverything else is an address you find and call through them:\n\n- `mesh_search` — words in, matching addresses out. `mesh_describe` — one address's arguments.\n- `mesh_call` — call an address. A seat the mesh holds once is `<seat>.<verb>` (the mesh's own seats\n include `mesh-module-assigner`, `mesh-delivery`, `mesh-config` and `mesh-conditions`);\n a module on a machine is `<node>/<module>.<tool>`.\n- `mesh_overview` and `mesh_machine` — the mesh's seats and machines, and what one machine runs.\n\n- **Symptom first.** For an error, a failing service or anything unexpected, search the record with the\n literal text before forming a hypothesis: the records module's `records_search`, then\n `records_read`.\n- **Ask the mesh before changing it**, and change it through the seats' verbs or the catalog.\n- **A licence** through the `anthropic-licence-manager` seat's verbs. Never edit the agent's credentials\n file by hand, never print or ask for a token.\n\n## Hard rules\n\n- A file the mesh manages is changed through the verb or the catalog that owns it, never on disk. If\n unsure, `<node>/node-engine.composition` for the module says what the mesh writes there.\n- Never write to a store's database by hand; schema changes are numbered migrations.\n- Never push to a main branch: a branch and a pull request for every change.\n- The mesh creates no symlinks, and nobody else does either.\n- A package is declared in a module, never installed by hand.\n\n## Conventions\n\n- Commit messages are concise, in the imperative, about why.\n- Test before pushing: nodes update unattended.\n- The playbooks in the record say how research, decisions, designs, issues and hand-offs are done.\n"
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ package main
|
||||
// Every file lives under the agent's home (the manifest's `agent-home` resource), which this process —
|
||||
// launched as the operator's account (novox/hq ADR 0193) — cannot itself write when the node names an agent
|
||||
// account of its own (novox/hq ADR 0266): every write below goes through `fsys` (homefs.go), which already
|
||||
// carries such a write through the sudo helper the guard's binary also serves, exactly as grant.go's
|
||||
// carries such a write through the sudo helper this module's binary serves, exactly as grant.go's
|
||||
// WriteCredentials does for the agent's own credentials file. Where the node has no agent account of its
|
||||
// own, fsys is the plain filesystem and the write reaches the operator's own home directly.
|
||||
|
||||
|
||||
@@ -5,9 +5,5 @@ agent='${machine:agent-account}'
|
||||
if [ "$(id -un)" = "$agent" ]; then
|
||||
exec claude "$@"
|
||||
fi
|
||||
# Another account, the operator's, becomes the agent's through its own sudo: the person is the authority. The
|
||||
# operator's override of the guard travels only when it is set in this shell, as it would reach claude.
|
||||
if [ -n "$MESH_GUARD_OVERRIDE" ]; then
|
||||
exec sudo -iu "$agent" env MESH_GUARD_OVERRIDE="$MESH_GUARD_OVERRIDE" claude "$@"
|
||||
fi
|
||||
# Another account, the operator's, becomes the agent's through its own sudo: the person is the authority.
|
||||
exec sudo -iu "$agent" claude "$@"
|
||||
|
||||
@@ -7,12 +7,11 @@ require git.novox.be/novox/mesh-sdk/go v0.0.0-00010101000000-000000000000
|
||||
require (
|
||||
git.novox.be/novox/mesh-contracts/go/clients/meshmoduleassignerclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/go/seats/meshmoduleassigner v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshconditionsclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/health-and-repair/go/clients/meshhandactsclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/contracts/go/clients/meshcatalogclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/contracts/go/seats/meshcatalog v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/clients/meshcatalogclient v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshcatalog v0.0.0-00010101000000-000000000000
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
@@ -26,6 +25,10 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions v0.0.0-00010101000000-000000000000 // indirect
|
||||
git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshconditions v0.0.0-00010101000000-000000000000
|
||||
git.novox.be/novox/mesh-contracts/health-and-repair/go/seats/meshhandacts v0.0.0-00010101000000-000000000000 // indirect
|
||||
)
|
||||
|
||||
require git.novox.be/novox/mesh-delivery/modules/mesh-delivery-contracts/go/seats/meshdelivery v0.0.0-00010101000000-000000000000
|
||||
|
||||
require git.novox.be/novox/mesh-delivery/modules/mesh-delivery-sdk v0.0.0-00010101000000-000000000000 // indirect
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"module": "claude-code",
|
||||
"version": "1",
|
||||
"version": "0.1",
|
||||
"moved": true,
|
||||
"slug": "agent",
|
||||
"capabilities": [
|
||||
@@ -11,6 +11,25 @@
|
||||
{
|
||||
"name": "gitea-read",
|
||||
"optional": true
|
||||
},
|
||||
{
|
||||
"name": "finding-events",
|
||||
"storage": "stream",
|
||||
"template": "events",
|
||||
"options": {
|
||||
"events": [
|
||||
"finding-written.*",
|
||||
"finding-cleared.*",
|
||||
"findings-stated.*"
|
||||
],
|
||||
"max-age-days": 7,
|
||||
"max-mib": 16
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "findings",
|
||||
"storage": "key-value",
|
||||
"template": "state"
|
||||
}
|
||||
],
|
||||
"binds": {
|
||||
@@ -26,16 +45,26 @@
|
||||
"seat:mesh-hand-acts.warranted",
|
||||
"seat:mesh-hand-acts.hand-act",
|
||||
"seat:mesh-catalog.seat-definitions",
|
||||
"mesh-delivery-indexer.indexer_read_modules",
|
||||
"mesh-delivery-indexer.indexer_read_manifests",
|
||||
"mesh-delivery-assigner.assigner_read_assignments"
|
||||
"seat:mesh-catalog.revision",
|
||||
"seat:mesh-catalog.registrations",
|
||||
"seat:mesh-delivery.source",
|
||||
"mesh-delivery-assigner.assigner_read_assignments",
|
||||
"seat:anthropic-licence-manager.bindings",
|
||||
"seat:anthropic-licence-manager.current",
|
||||
"seat:mesh-vault.get",
|
||||
"seat:operator-channel.notify"
|
||||
],
|
||||
"consumes": [
|
||||
"mesh-delivery-indexer.views.modules",
|
||||
"mesh-delivery-indexer.views.manifests",
|
||||
"mesh-catalog.catalog-revision",
|
||||
"mesh-catalog.registration-changed",
|
||||
"mesh-delivery.source-moved",
|
||||
"mesh-delivery-assigner.views.assignments",
|
||||
"mesh-catalog.seat-definition-changed",
|
||||
"mesh-catalog.seat-definition-removed"
|
||||
"mesh-catalog.seat-definition-removed",
|
||||
"mesh-conditions.findings-asked",
|
||||
"claude-licence-manager.holdings.asked",
|
||||
"claude-licence-manager.binding.changed.*",
|
||||
"mesh-vault.secret-changed"
|
||||
],
|
||||
"state": [
|
||||
"servers",
|
||||
@@ -44,19 +73,15 @@
|
||||
"proposals",
|
||||
"questions",
|
||||
"memory",
|
||||
"waits",
|
||||
"findings"
|
||||
"waits"
|
||||
],
|
||||
"emits": [
|
||||
"judged"
|
||||
],
|
||||
"reads": [
|
||||
"claude-licence-manager.bindings"
|
||||
"judged",
|
||||
"holdings-changed"
|
||||
],
|
||||
"tools": [
|
||||
"claude_code_status",
|
||||
"claude_code_render",
|
||||
"claude_code_guard",
|
||||
"claude_code_pull",
|
||||
"claude_code_grant",
|
||||
"claude_code_add_api_key",
|
||||
@@ -233,7 +258,16 @@
|
||||
"path": "${dir:state}/settings.json",
|
||||
"mode": "0600",
|
||||
"owner": "${machine:account}",
|
||||
"takes-config": ["role", "mcp_servers", "managed_settings", "instructions", "output_styles", "skills", "commands", "agents"],
|
||||
"takes-config": [
|
||||
"role",
|
||||
"mcp_servers",
|
||||
"managed_settings",
|
||||
"instructions",
|
||||
"output_styles",
|
||||
"skills",
|
||||
"commands",
|
||||
"agents"
|
||||
],
|
||||
"merge": "json",
|
||||
"template": "files/settings.json"
|
||||
}
|
||||
|
||||
@@ -1,58 +1,112 @@
|
||||
# claude-licence-manager
|
||||
|
||||
Holds the `anthropic-licence-manager` seat: every Anthropic licence the mesh has, kept alive by one
|
||||
rotation source, and handed to each consumer sealed (novox/hq ADR 0183, ADR 0206, design 39).
|
||||
rotation source, its consumers moved off a licence near its limit by the manager itself (novox/hq ADR 0183,
|
||||
ADR 0206, design 39 and its milestone bus-and-switch). Every grant and access token lives in the vault; this module
|
||||
keeps none.
|
||||
|
||||
It is one manifest of four parts (design 39 parts §1): the manager bundle (`manager`, `cmd/manager`), which runs long
|
||||
(the conditions judged at its start, the usage timer, the switch rule) and answers the seat's verbs and every act; the
|
||||
tools bundle (`tools`, `cmd/tools`), which answers the reading tools and writes nothing, so they answer while the
|
||||
manager fails; the settings file; and the module's account with its directory. Both bundles run as an account of its
|
||||
own (`runs-as claude-licence-manager`, design 39 bus-and-switch C1), in a runtime of its own, never in the machine's
|
||||
tool runner, so every state, log and secret below is issued to its own account. Its
|
||||
account is issued what its manifest names: the asks under `invokes` (the vault, the operator channel, mesh-config and
|
||||
a node's `claude_code_grant`), the events under `emits` and `consumes`.
|
||||
|
||||
## How a licence comes to exist
|
||||
|
||||
Nothing is configured. Every node running `claude-code` reports what it holds as that module's
|
||||
`holdings` state — the account, fingerprints and expiries, never a token. This module reads every report
|
||||
when it starts and watches them:
|
||||
Nothing is configured. Every node running `claude-code` says what it holds as the event
|
||||
`claude-code.holdings-changed` (the account, fingerprints and expiries, never a token), at its start, on every change,
|
||||
and again when this module says `holdings.asked` (at every start, after its conditions are judged, and on `licences_resync`):
|
||||
|
||||
1. A report with a refresh token it does not hold is a **candidate**.
|
||||
2. It asks that node's `claude_code_grant`, giving its public key, and receives the grant sealed to it.
|
||||
3. **It refreshes it.** If the vendor exchanges the token, the grant is this module's — encrypted at rest
|
||||
with the key the vault made for it — and from then on it is the only refresher. If not, the candidate
|
||||
is recorded dead and nothing is adopted.
|
||||
3. **It refreshes it**, under the account's lease. If the vendor exchanges the token, the licence is new, named by its
|
||||
account's address and kept under that name's key (`@` as `_at_`, any other character outside letters, digits, `-`
|
||||
and `.` as `_`); its grant and access token go to the vault (`licence-put`), and from then on this module is the
|
||||
only refresher. If not, the candidate is recorded dead and nothing is adopted. An adoption whose key another licence
|
||||
has is refused.
|
||||
4. Several nodes logged in to one account: newest login first; the rest are never exchanged.
|
||||
5. A node reporting that account and bound to nothing is bound to it.
|
||||
5. The node it came from is bound to it; a node reporting that account and bound to nothing is bound to it.
|
||||
|
||||
Each node is then handed an access token only, so the agent there never refreshes, and a refresh token
|
||||
appearing on a node later can only be a person's login — which wins if it refreshes.
|
||||
From its next reading a new subscription has a room and is a target of the switch. An API key enters through `adopt`,
|
||||
from a file on this module's node or sealed by a node's `claude_code_add_api_key`; it is never a target.
|
||||
|
||||
An API key enters through `adopt`, from a file on this module's node.
|
||||
## What it keeps on the bus
|
||||
|
||||
## What each consumer holds
|
||||
Each state asked for in `requires` by its template (ADR 0595), and the two logs declared in the `logs` section, the
|
||||
form the runtime issues today (design 39 bus-and-switch C3); each read by `licences_kept`:
|
||||
|
||||
This module's `bindings` state: one key per consumer (a node's name) with the licence, its kind and a
|
||||
generation that grows with every rotation and switch. `claude-code` watches its own key and, on a newer
|
||||
generation, asks `current` with its public key.
|
||||
| name | template | what |
|
||||
|---|---|---|
|
||||
| `licences` | state, history 1 | one key per licence: account, address, organisation, expiries, failures, adoption, the vault's generation, the latest reading; never a token |
|
||||
| `bindings` | state, history 8 | one key per consumer (a node's name): licence, kind, generation, since, why and by |
|
||||
| `leases` | state, a lifetime per key | `licence.<key>`, `account.<id>`: one refresh or adoption at a time, two minutes |
|
||||
| `offered` | state, a lifetime per key | each grant a node offered and its outcome, 90 days |
|
||||
| `usage` | log, 35 days, 256 MiB | every reading, one subject per licence key, with the vendor's answer whole |
|
||||
| `audit` | log, 64 MiB | every act, one subject per kind (`switched`, `carried`, `settings` among them) |
|
||||
| `carried` | state | `postgres`: the record of the carry-over from the store database, which ran once on 2026-11-12; nothing writes it again |
|
||||
| `conditions` | state | each condition said raised, and since when |
|
||||
|
||||
The carry-over from the store database is done, and its code has left the module with its requirement of the
|
||||
database and of the secret `grant-key` (design 39 parts §9, §10); at its start the manager waits for nothing and
|
||||
clears a `carry-over-failed` it finds kept.
|
||||
|
||||
## Switching on usage
|
||||
|
||||
Every `usage_minutes` the manager reads each subscription's usage with its access token from the vault: the session,
|
||||
weekly and sonnet windows, each with its reset time. A licence's room is 100 less its weekly percentage alone (the session
|
||||
window takes no part in room, the switch, the warning or the conditions: the vendor retired the session limit), known only when it was read within `usage_stale_minutes` and its refresh is healthy. When a reading puts
|
||||
a licence with consumers at or above `switch_at_pct` in its weekly window, every consumer bound to it moves, in one act, to the licence
|
||||
with the most room whose weekly window is below `room_below_pct`: one `binding.changed` per consumer, one
|
||||
`licence.switched`, and one `operator-channel.tell`. A consumer is never moved back by the rule.
|
||||
|
||||
Its conditions are said on the operator channel's condition subject, the router deciding where they go:
|
||||
`licence.<key>.no-room` (urgent; no licence has room), `licence.<key>.near-limit` (at `warn_at_pct`, once per
|
||||
crossing), `licence.<key>.usage-unread` and `settings.<setting>.invalid`.
|
||||
|
||||
## The seat's verbs
|
||||
|
||||
`licences`, `bindings`, `bind`, `switch`, `release`, `refresh`, `usage`, `adopt`, `current` — through
|
||||
the console as `anthropic-licence-manager.<verb>`. No answer carries a token.
|
||||
`licences`, `bindings` (with `consumer` for one), `bind`, `switch`, `release`, `refresh`, `usage` (`licence`,
|
||||
`since`, `limit` up to 2000, `raw`), `adopt`, `public-key`, `current` (the licence, its key, kind, generation and
|
||||
health, never the token) — through the console as `anthropic-licence-manager.<verb>`. No answer carries a token.
|
||||
|
||||
## Its tools
|
||||
|
||||
Read by the tools bundle: `licences_switches` (every switch and why), `licences_settings` (each value, its default
|
||||
and where it was set), `licences_kept` (every entry it keeps, how full against its cap) and `licences_conditions`.
|
||||
Acting, by the manager bundle: `licences_read_usage` (read now, `dry`), `licences_switch_now` (apply the rule now, even
|
||||
with `switch_auto` off) and `licences_resync` (say every binding, holdings.asked and each condition again, and hand
|
||||
each access token to the vault again).
|
||||
|
||||
## Settings
|
||||
|
||||
`settings.json` in the state directory: `cadence_minutes` (240), `floor_minutes` (60),
|
||||
`failures_to_notify` (3), `cooldown_hours` (24), `refresh_warn_days` (3).
|
||||
Through mesh-config, each `trusted`, read again at every reading: `cadence_minutes` (240), `floor_minutes` (60),
|
||||
`failures_to_notify` (3), `cooldown_hours` (24), `refresh_warn_days` (3), `switch_auto` (on), `switch_at_pct` (99,
|
||||
1 to 100), `room_below_pct` (90, 1 to 100), `warn_at_pct` (90, 1 to 100), `usage_minutes` (5, 1 to 60),
|
||||
`usage_stale_minutes` (15, 5 to 180). A value outside its bound keeps its default and raises its condition.
|
||||
|
||||
## Events
|
||||
|
||||
`licence.adopted`, `licence.refused`, `licence.failing`, `usage.read` — none carries a secret.
|
||||
`licence.adopted`, `licence.refused`, `licence.switched`, `licence.failing`, `usage.read`, `binding.changed`,
|
||||
`holdings.asked` — none carries a secret. It consumes `claude-code.holdings-changed`.
|
||||
|
||||
## Code and tests
|
||||
|
||||
Go, one binary (`cmd/claude-licence-manager`): the seat's verbs and the daemon in one launched bundle,
|
||||
`prepare` as the run-once preparation step. The sealed box is `claude-code`'s own format, byte for byte —
|
||||
the two modules carry the same `seal.go` — and a test opens one sealed by the TypeScript agent module the
|
||||
Go one replaced, so the format is the one already on the machines.
|
||||
Go, one Go module of two binaries: `cmd/manager` (`claude-licence-manager`: the seat's verbs, the acting tools and the
|
||||
daemon) and `cmd/tools` (`claude-licence-manager-tools`: the reading tools), sharing the package `internal/licences`
|
||||
(the layouts of the kept data and the log entries, the key form, the entries' names and the settings). The
|
||||
sealed box is `claude-code`'s own format, byte for byte — the two modules carry the same `seal.go` — and a test opens
|
||||
one sealed by the TypeScript agent module the Go one replaced, so the format is the one already on the machines.
|
||||
|
||||
go test ./...
|
||||
# the store against a real postgres:
|
||||
docker run -d --rm --name licmgr-pg -e POSTGRES_PASSWORD=t -p 15498:5432 postgres:16-alpine
|
||||
MESH_TEST_POSTGRES=postgres://postgres:t@127.0.0.1:15498/postgres go test ./...
|
||||
# the whole module on a real bus:
|
||||
go test -tags integration ./...
|
||||
|
||||
The whole tests (`cmd/manager/whole_integration_test.go`) start the built binaries as their runtime does, its kept data in a real
|
||||
nats-server. The vault, the agent modules, the
|
||||
operator channel and the vendor are played beside it; a condition is read through the operator channel's client.
|
||||
|
||||
## Files it does not know
|
||||
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
package main
|
||||
|
||||
// The grants at rest (novox/hq ADR 0183): encrypted with a key the vault made for this module, so the
|
||||
// store holds ciphertext and only this module, reading its own secret, can open a row. AES-256-GCM, the
|
||||
// key derived from the vault's secret by SHA-256 so a secret of any length serves.
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Crypt seals and opens what the store keeps.
|
||||
type Crypt struct{ key []byte }
|
||||
|
||||
// NewCrypt is the store's cipher from the vault's secret.
|
||||
func NewCrypt(secret string) (*Crypt, error) {
|
||||
secret = strings.TrimSpace(secret)
|
||||
if secret == "" {
|
||||
return nil, errors.New("the key the grants are encrypted with is empty: the vault has not delivered it yet")
|
||||
}
|
||||
sum := sha256.Sum256([]byte(secret))
|
||||
return &Crypt{key: sum[:]}, nil
|
||||
}
|
||||
|
||||
// CryptFromFile reads the vault's secret from the file the mesh delivered it to.
|
||||
func CryptFromFile(path string) (*Crypt, error) {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return NewCrypt(string(raw))
|
||||
}
|
||||
|
||||
// Seal encrypts a plaintext as `v1.<iv>.<ciphertext and tag>`.
|
||||
func (c *Crypt) Seal(plaintext string) string {
|
||||
gcm, _ := newGCM(c.key)
|
||||
iv := make([]byte, 12)
|
||||
_, _ = rand.Read(iv)
|
||||
b64 := base64.StdEncoding.EncodeToString
|
||||
return "v1." + b64(iv) + "." + b64(gcm.Seal(nil, iv, []byte(plaintext), nil))
|
||||
}
|
||||
|
||||
// Open decrypts what Seal made with the same key.
|
||||
func (c *Crypt) Open(sealed string) (string, error) {
|
||||
parts := strings.Split(sealed, ".")
|
||||
if len(parts) != 3 || parts[0] != "v1" {
|
||||
return "", errors.New("not a grant this module sealed")
|
||||
}
|
||||
iv, err1 := base64.StdEncoding.DecodeString(parts[1])
|
||||
ct, err2 := base64.StdEncoding.DecodeString(parts[2])
|
||||
if err := errors.Join(err1, err2); err != nil {
|
||||
return "", err
|
||||
}
|
||||
gcm, _ := newGCM(c.key)
|
||||
plain, err := gcm.Open(nil, iv, ct, nil)
|
||||
if err != nil {
|
||||
return "", errors.New("the grant does not open with this module's key")
|
||||
}
|
||||
return string(plain), nil
|
||||
}
|
||||
@@ -1,291 +0,0 @@
|
||||
package main
|
||||
|
||||
// The store on the mesh's postgres (novox/hq design 39 §1), the database the mesh provisioned for this
|
||||
// module. The schema is brought to this version's shape by the preparation step (ADR 0135), each
|
||||
// statement idempotent.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// Schema is what this version needs.
|
||||
var Schema = []string{
|
||||
`create table if not exists licence (
|
||||
name text primary key,
|
||||
kind text not null check (kind in ('subscription', 'api-key')),
|
||||
account_uuid text unique,
|
||||
email text,
|
||||
organization_uuid text,
|
||||
sealed text,
|
||||
refresh_fingerprint text,
|
||||
access_expires_at bigint,
|
||||
refresh_expires_at bigint,
|
||||
failures integer not null default 0,
|
||||
notified_at bigint,
|
||||
adopted_at bigint not null,
|
||||
rotated_at bigint)`,
|
||||
`create sequence if not exists binding_generation`,
|
||||
`create table if not exists binding (
|
||||
consumer text primary key,
|
||||
licence text not null references licence(name),
|
||||
generation bigint not null)`,
|
||||
`create table if not exists lease (
|
||||
key text primary key,
|
||||
holder text not null,
|
||||
until timestamptz not null)`,
|
||||
`create table if not exists offered (
|
||||
fingerprint text primary key,
|
||||
node text not null,
|
||||
account_uuid text,
|
||||
outcome text not null,
|
||||
why text not null,
|
||||
at timestamptz not null default now())`,
|
||||
`create table if not exists usage (
|
||||
licence text not null,
|
||||
at bigint not null,
|
||||
reading jsonb not null,
|
||||
raw jsonb not null)`,
|
||||
`create index if not exists usage_by_licence on usage (licence, at desc)`,
|
||||
`create table if not exists audit (
|
||||
at timestamptz not null default now(),
|
||||
what text not null,
|
||||
detail jsonb not null)`,
|
||||
}
|
||||
|
||||
// PgStore is the store on postgres.
|
||||
type PgStore struct{ pool *pgxpool.Pool }
|
||||
|
||||
// PgStoreFromEnv opens the store the mesh provisioned, its URL in the file DATABASE_URL_FILE names.
|
||||
func PgStoreFromEnv(ctx context.Context) (*PgStore, error) {
|
||||
file := os.Getenv("DATABASE_URL_FILE")
|
||||
if file == "" {
|
||||
return nil, errors.New("DATABASE_URL_FILE is not set: the manager's database is a requirement the mesh resolves")
|
||||
}
|
||||
raw, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return OpenPgStore(ctx, strings.TrimSpace(string(raw)))
|
||||
}
|
||||
|
||||
// OpenPgStore opens a store at a URL.
|
||||
func OpenPgStore(ctx context.Context, url string) (*PgStore, error) {
|
||||
pool, err := pgxpool.New(ctx, url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &PgStore{pool: pool}, nil
|
||||
}
|
||||
|
||||
// Migrate brings the schema to this version's shape.
|
||||
func (s *PgStore) Migrate(ctx context.Context) error {
|
||||
for _, q := range Schema {
|
||||
if _, err := s.pool.Exec(ctx, q); err != nil {
|
||||
return fmt.Errorf("%s: %w", strings.Fields(q)[0:6], err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
const licenceColumns = `name, kind, coalesce(account_uuid,''), coalesce(email,''), coalesce(organization_uuid,''),
|
||||
coalesce(sealed,''), coalesce(refresh_fingerprint,''), coalesce(access_expires_at,0), coalesce(refresh_expires_at,0),
|
||||
failures, coalesce(notified_at,0), adopted_at, coalesce(rotated_at,0)`
|
||||
|
||||
func scanLicence(row pgx.Row) (*Licence, error) {
|
||||
var l Licence
|
||||
err := row.Scan(&l.Name, &l.Kind, &l.AccountUUID, &l.Email, &l.OrganizationUUID, &l.Sealed, &l.RefreshFingerprint,
|
||||
&l.AccessExpiresAt, &l.RefreshExpiresAt, &l.Failures, &l.NotifiedAt, &l.AdoptedAt, &l.RotatedAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
return &l, err
|
||||
}
|
||||
|
||||
func (s *PgStore) Licences(ctx context.Context) ([]Licence, error) {
|
||||
rows, err := s.pool.Query(ctx, `select `+licenceColumns+` from licence order by name`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []Licence
|
||||
for rows.Next() {
|
||||
l, err := scanLicence(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, *l)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *PgStore) Licence(ctx context.Context, name string) (*Licence, error) {
|
||||
return scanLicence(s.pool.QueryRow(ctx, `select `+licenceColumns+` from licence where name = $1`, name))
|
||||
}
|
||||
|
||||
func (s *PgStore) LicenceForAccount(ctx context.Context, account string) (*Licence, error) {
|
||||
return scanLicence(s.pool.QueryRow(ctx, `select `+licenceColumns+` from licence where account_uuid = $1`, account))
|
||||
}
|
||||
|
||||
func nullable(s string) any {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func nullableInt(v int64) any {
|
||||
if v == 0 {
|
||||
return nil
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (s *PgStore) SaveLicence(ctx context.Context, l Licence) error {
|
||||
_, err := s.pool.Exec(ctx, `insert into licence (name, kind, account_uuid, email, organization_uuid, sealed, refresh_fingerprint,
|
||||
access_expires_at, refresh_expires_at, failures, notified_at, adopted_at, rotated_at)
|
||||
values ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)
|
||||
on conflict (name) do update set kind = excluded.kind, account_uuid = excluded.account_uuid, email = excluded.email,
|
||||
organization_uuid = excluded.organization_uuid, sealed = excluded.sealed, refresh_fingerprint = excluded.refresh_fingerprint,
|
||||
access_expires_at = excluded.access_expires_at, refresh_expires_at = excluded.refresh_expires_at,
|
||||
failures = excluded.failures, notified_at = excluded.notified_at, adopted_at = excluded.adopted_at,
|
||||
rotated_at = excluded.rotated_at`,
|
||||
l.Name, l.Kind, nullable(l.AccountUUID), nullable(l.Email), nullable(l.OrganizationUUID), nullable(l.Sealed),
|
||||
nullable(l.RefreshFingerprint), nullableInt(l.AccessExpiresAt), nullableInt(l.RefreshExpiresAt), l.Failures,
|
||||
nullableInt(l.NotifiedAt), l.AdoptedAt, nullableInt(l.RotatedAt))
|
||||
return err
|
||||
}
|
||||
|
||||
// Lease is taken in the store before a row is read (design 39 §3): a second run started together finds
|
||||
// it live and does nothing.
|
||||
func (s *PgStore) Lease(ctx context.Context, key, holder string, d time.Duration) (bool, error) {
|
||||
tag, err := s.pool.Exec(ctx, `insert into lease (key, holder, until) values ($1, $2, now() + make_interval(secs => $3))
|
||||
on conflict (key) do update set holder = excluded.holder, until = excluded.until
|
||||
where lease.until < now() or lease.holder = excluded.holder`, key, holder, d.Seconds())
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return tag.RowsAffected() == 1, nil
|
||||
}
|
||||
|
||||
func (s *PgStore) Unlease(ctx context.Context, key, holder string) error {
|
||||
_, err := s.pool.Exec(ctx, `delete from lease where key = $1 and holder = $2`, key, holder)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *PgStore) bindingsWhere(ctx context.Context, q string, args ...any) ([]Binding, error) {
|
||||
rows, err := s.pool.Query(ctx, q, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []Binding
|
||||
for rows.Next() {
|
||||
var b Binding
|
||||
if err := rows.Scan(&b.Consumer, &b.Licence, &b.Generation); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, b)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *PgStore) Bindings(ctx context.Context) ([]Binding, error) {
|
||||
return s.bindingsWhere(ctx, `select consumer, licence, generation from binding order by consumer`)
|
||||
}
|
||||
|
||||
func (s *PgStore) Binding(ctx context.Context, consumer string) (*Binding, error) {
|
||||
out, err := s.bindingsWhere(ctx, `select consumer, licence, generation from binding where consumer = $1`, consumer)
|
||||
if err != nil || len(out) == 0 {
|
||||
return nil, err
|
||||
}
|
||||
return &out[0], nil
|
||||
}
|
||||
|
||||
func (s *PgStore) Bind(ctx context.Context, consumer, licence string) (Binding, error) {
|
||||
out, err := s.bindingsWhere(ctx, `insert into binding (consumer, licence, generation) values ($1, $2, nextval('binding_generation'))
|
||||
on conflict (consumer) do update set licence = excluded.licence, generation = excluded.generation
|
||||
returning consumer, licence, generation`, consumer, licence)
|
||||
if err != nil {
|
||||
return Binding{}, err
|
||||
}
|
||||
return out[0], nil
|
||||
}
|
||||
|
||||
func (s *PgStore) Unbind(ctx context.Context, consumer string) (bool, error) {
|
||||
tag, err := s.pool.Exec(ctx, `delete from binding where consumer = $1`, consumer)
|
||||
return err == nil && tag.RowsAffected() == 1, err
|
||||
}
|
||||
|
||||
func (s *PgStore) GenerationsAbove(ctx context.Context, generation int64) error {
|
||||
if generation <= 0 {
|
||||
return nil
|
||||
}
|
||||
// setval with is_called, so the next nextval is generation+1; only ever forward.
|
||||
_, err := s.pool.Exec(ctx, `select setval('binding_generation', $1, true) from binding_generation
|
||||
where not is_called or last_value < $1`, generation)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *PgStore) Advance(ctx context.Context, licence string) ([]Binding, error) {
|
||||
return s.bindingsWhere(ctx, `update binding set generation = nextval('binding_generation') where licence = $1
|
||||
returning consumer, licence, generation`, licence)
|
||||
}
|
||||
|
||||
func (s *PgStore) Outcome(ctx context.Context, fp string) (Outcome, error) {
|
||||
var o string
|
||||
err := s.pool.QueryRow(ctx, `select outcome from offered where fingerprint = $1`, fp).Scan(&o)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "", nil
|
||||
}
|
||||
return Outcome(o), err
|
||||
}
|
||||
|
||||
func (s *PgStore) RecordOutcome(ctx context.Context, fp, node, account string, o Outcome, why string) error {
|
||||
_, err := s.pool.Exec(ctx, `insert into offered (fingerprint, node, account_uuid, outcome, why) values ($1,$2,$3,$4,$5)
|
||||
on conflict (fingerprint) do update set outcome = excluded.outcome, why = excluded.why, at = now()`,
|
||||
fp, node, nullable(account), string(o), why)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *PgStore) RecordUsage(ctx context.Context, licence string, at int64, r UsageReading, raw map[string]any) error {
|
||||
reading, _ := json.Marshal(r)
|
||||
rawJSON, _ := json.Marshal(raw)
|
||||
_, err := s.pool.Exec(ctx, `insert into usage (licence, at, reading, raw) values ($1,$2,$3,$4)`, licence, at, reading, rawJSON)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *PgStore) Usage(ctx context.Context, licence string, limit int) ([]UsageRow, error) {
|
||||
rows, err := s.pool.Query(ctx, `select licence, at, reading from usage where ($1 = '' or licence = $1) order by at desc limit $2`, licence, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []UsageRow
|
||||
for rows.Next() {
|
||||
var u UsageRow
|
||||
var reading []byte
|
||||
if err := rows.Scan(&u.Licence, &u.At, &reading); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = json.Unmarshal(reading, &u.Reading)
|
||||
out = append(out, u)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *PgStore) Audit(ctx context.Context, what string, detail map[string]any) error {
|
||||
raw, _ := json.Marshal(detail)
|
||||
_, err := s.pool.Exec(ctx, `insert into audit (what, detail) values ($1, $2)`, what, raw)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *PgStore) Close() { s.pool.Close() }
|
||||
@@ -1,43 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A box the agent module's TypeScript sealed opens here: the two implementations are one format.
|
||||
func TestABoxSealedInTypeScriptOpensInGo(t *testing.T) {
|
||||
raw, err := os.ReadFile("testdata/sealed-by-typescript.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var f struct {
|
||||
PrivateKey string `json:"privateKey"`
|
||||
Box SealedBox `json:"box"`
|
||||
Plaintext string `json:"plaintext"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &f); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := Open(f.Box, f.PrivateKey)
|
||||
if err != nil || got != f.Plaintext {
|
||||
t.Fatalf("opened %q, %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
// What Go seals opens with its own key and no other.
|
||||
func TestABoxOpensOnlyForItsRecipient(t *testing.T) {
|
||||
a, _ := GenerateKeyPair()
|
||||
b, _ := GenerateKeyPair()
|
||||
box, err := Seal("a token", a.PublicKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := Open(box, a.PrivateKey); err != nil || got != "a token" {
|
||||
t.Fatalf("opened %q, %v", got, err)
|
||||
}
|
||||
if _, err := Open(box, b.PrivateKey); err == nil {
|
||||
t.Fatal("a box opened for another key")
|
||||
}
|
||||
}
|
||||
@@ -1,272 +0,0 @@
|
||||
package main
|
||||
|
||||
// The manager's store (novox/hq ADR 0183, design 39 §1): licences with their grants encrypted, bindings
|
||||
// with a generation, what became of each login it was offered, usage readings, and the audit. One
|
||||
// interface, two implementations — postgres for the mesh (pgstore.go), memory for the tests — so every
|
||||
// rule is tested without a database, and the database is asked only to keep rows.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Licence is one licence: an account, or an API key.
|
||||
type Licence struct {
|
||||
Name string `json:"name"`
|
||||
Kind string `json:"kind"` // subscription | api-key
|
||||
AccountUUID string `json:"accountUuid,omitempty"`
|
||||
Email string `json:"email,omitempty"`
|
||||
OrganizationUUID string `json:"organizationUuid,omitempty"`
|
||||
Sealed string `json:"-"` // the grant or the key, encrypted at rest
|
||||
RefreshFingerprint string `json:"-"`
|
||||
AccessExpiresAt int64 `json:"accessExpiresAt,omitempty"`
|
||||
RefreshExpiresAt int64 `json:"refreshExpiresAt,omitempty"`
|
||||
Failures int `json:"failures"`
|
||||
NotifiedAt int64 `json:"-"`
|
||||
AdoptedAt int64 `json:"adoptedAt"`
|
||||
RotatedAt int64 `json:"rotatedAt,omitempty"`
|
||||
}
|
||||
|
||||
// Binding is one consumer's binding and the generation it was last given (ADR 0206).
|
||||
type Binding struct {
|
||||
Consumer string `json:"consumer"`
|
||||
Licence string `json:"licence"`
|
||||
Generation int64 `json:"generation"`
|
||||
}
|
||||
|
||||
// Outcome is what became of a login the manager was offered, by its refresh token's fingerprint.
|
||||
type Outcome string
|
||||
|
||||
const (
|
||||
Adopted Outcome = "adopted"
|
||||
Dead Outcome = "dead"
|
||||
Skipped Outcome = "skipped"
|
||||
Refused Outcome = "refused"
|
||||
Gone Outcome = "gone"
|
||||
)
|
||||
|
||||
// UsageRow is one usage reading.
|
||||
type UsageRow struct {
|
||||
Licence string `json:"licence"`
|
||||
At int64 `json:"at"`
|
||||
Reading UsageReading `json:"reading"`
|
||||
}
|
||||
|
||||
// Store is what the manager keeps.
|
||||
type Store interface {
|
||||
Licences(ctx context.Context) ([]Licence, error)
|
||||
Licence(ctx context.Context, name string) (*Licence, error)
|
||||
LicenceForAccount(ctx context.Context, account string) (*Licence, error)
|
||||
SaveLicence(ctx context.Context, l Licence) error
|
||||
// Lease takes a lease for d, or answers false while another holder's is live.
|
||||
Lease(ctx context.Context, key, holder string, d time.Duration) (bool, error)
|
||||
Unlease(ctx context.Context, key, holder string) error
|
||||
Bindings(ctx context.Context) ([]Binding, error)
|
||||
Binding(ctx context.Context, consumer string) (*Binding, error)
|
||||
// Bind binds (or switches) a consumer at the next generation.
|
||||
Bind(ctx context.Context, consumer, licence string) (Binding, error)
|
||||
Unbind(ctx context.Context, consumer string) (bool, error)
|
||||
// Advance gives every consumer of a licence a new generation: what a rotation is to them.
|
||||
Advance(ctx context.Context, licence string) ([]Binding, error)
|
||||
// GenerationsAbove makes every generation given from now on greater than this one.
|
||||
GenerationsAbove(ctx context.Context, generation int64) error
|
||||
Outcome(ctx context.Context, fingerprint string) (Outcome, error)
|
||||
RecordOutcome(ctx context.Context, fingerprint, node, account string, o Outcome, why string) error
|
||||
RecordUsage(ctx context.Context, licence string, at int64, r UsageReading, raw map[string]any) error
|
||||
Usage(ctx context.Context, licence string, limit int) ([]UsageRow, error)
|
||||
Audit(ctx context.Context, what string, detail map[string]any) error
|
||||
Close()
|
||||
}
|
||||
|
||||
// MemoryStore is the tests' store.
|
||||
type MemoryStore struct {
|
||||
mu sync.Mutex
|
||||
now func() time.Time
|
||||
rows map[string]Licence
|
||||
binds map[string]Binding
|
||||
leases map[string]struct {
|
||||
holder string
|
||||
until time.Time
|
||||
}
|
||||
outcomes map[string]Outcome
|
||||
usage []UsageRow
|
||||
Audits []map[string]any
|
||||
generation int64
|
||||
}
|
||||
|
||||
// NewMemoryStore is an empty store whose leases age by now.
|
||||
func NewMemoryStore(now func() time.Time) *MemoryStore {
|
||||
return &MemoryStore{now: now, rows: map[string]Licence{}, binds: map[string]Binding{},
|
||||
leases: map[string]struct {
|
||||
holder string
|
||||
until time.Time
|
||||
}{}, outcomes: map[string]Outcome{}}
|
||||
}
|
||||
|
||||
func (m *MemoryStore) Licences(context.Context) ([]Licence, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
out := make([]Licence, 0, len(m.rows))
|
||||
for _, l := range m.rows {
|
||||
out = append(out, l)
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) Licence(_ context.Context, name string) (*Licence, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if l, ok := m.rows[name]; ok {
|
||||
return &l, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) LicenceForAccount(_ context.Context, account string) (*Licence, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
for _, l := range m.rows {
|
||||
if l.AccountUUID == account {
|
||||
return &l, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) SaveLicence(_ context.Context, l Licence) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.rows[l.Name] = l
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) Lease(_ context.Context, key, holder string, d time.Duration) (bool, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if held, ok := m.leases[key]; ok && held.until.After(m.now()) && held.holder != holder {
|
||||
return false, nil
|
||||
}
|
||||
m.leases[key] = struct {
|
||||
holder string
|
||||
until time.Time
|
||||
}{holder, m.now().Add(d)}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) Unlease(_ context.Context, key, holder string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if m.leases[key].holder == holder {
|
||||
delete(m.leases, key)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) Bindings(context.Context) ([]Binding, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
out := make([]Binding, 0, len(m.binds))
|
||||
for _, b := range m.binds {
|
||||
out = append(out, b)
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Consumer < out[j].Consumer })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) Binding(_ context.Context, consumer string) (*Binding, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if b, ok := m.binds[consumer]; ok {
|
||||
return &b, nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) Bind(_ context.Context, consumer, licence string) (Binding, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.generation++
|
||||
b := Binding{Consumer: consumer, Licence: licence, Generation: m.generation}
|
||||
m.binds[consumer] = b
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) Unbind(_ context.Context, consumer string) (bool, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
_, ok := m.binds[consumer]
|
||||
delete(m.binds, consumer)
|
||||
return ok, nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) GenerationsAbove(_ context.Context, generation int64) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.generation = max(m.generation, generation)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) Advance(_ context.Context, licence string) ([]Binding, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
var out []Binding
|
||||
for c, b := range m.binds {
|
||||
if b.Licence != licence {
|
||||
continue
|
||||
}
|
||||
m.generation++
|
||||
b.Generation = m.generation
|
||||
m.binds[c] = b
|
||||
out = append(out, b)
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Consumer < out[j].Consumer })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) Outcome(_ context.Context, fp string) (Outcome, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return m.outcomes[fp], nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) RecordOutcome(_ context.Context, fp, _, _ string, o Outcome, _ string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.outcomes[fp] = o
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) RecordUsage(_ context.Context, licence string, at int64, r UsageReading, _ map[string]any) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
m.usage = append(m.usage, UsageRow{Licence: licence, At: at, Reading: r})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) Usage(_ context.Context, licence string, limit int) ([]UsageRow, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
var out []UsageRow
|
||||
for i := len(m.usage) - 1; i >= 0 && len(out) < limit; i-- {
|
||||
if licence == "" || m.usage[i].Licence == licence {
|
||||
out = append(out, m.usage[i])
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) Audit(_ context.Context, what string, detail map[string]any) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
d := map[string]any{"what": what}
|
||||
for k, v := range detail {
|
||||
d[k] = v
|
||||
}
|
||||
m.Audits = append(m.Audits, d)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MemoryStore) Close() {}
|
||||
@@ -1,126 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Against a real postgres, because the questions are the database's: does the schema apply twice, does a
|
||||
// lease refuse a second holder, does a generation only grow. Skipped unless one is named:
|
||||
//
|
||||
// docker run -d --rm --name licmgr-pg -e POSTGRES_PASSWORD=t -p 15498:5432 postgres:16-alpine
|
||||
// MESH_TEST_POSTGRES=postgres://postgres:t@127.0.0.1:15498/postgres go test ./...
|
||||
func TestTheStoreOnPostgres(t *testing.T) {
|
||||
url := os.Getenv("MESH_TEST_POSTGRES")
|
||||
if url == "" {
|
||||
t.Skip("MESH_TEST_POSTGRES unset")
|
||||
}
|
||||
ctx := context.Background()
|
||||
s, err := OpenPgStore(ctx, url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer s.Close()
|
||||
for _, table := range []string{"binding", "licence", "lease", "offered", "usage", "audit"} {
|
||||
_, _ = s.pool.Exec(ctx, "drop table if exists "+table+" cascade")
|
||||
}
|
||||
_, _ = s.pool.Exec(ctx, "drop sequence if exists binding_generation")
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := s.Migrate(ctx); err != nil {
|
||||
t.Fatalf("migration %d: %v", i+1, err)
|
||||
}
|
||||
}
|
||||
l := Licence{Name: "a@example.org", Kind: "subscription", AccountUUID: "u-1", Email: "a@example.org", Sealed: "v1.x.y",
|
||||
RefreshFingerprint: "sha256:1", AccessExpiresAt: 1, RefreshExpiresAt: 2, AdoptedAt: 3}
|
||||
if err := s.SaveLicence(ctx, l); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
l.Failures = 2
|
||||
_ = s.SaveLicence(ctx, l)
|
||||
if got, _ := s.LicenceForAccount(ctx, "u-1"); got == nil || got.Failures != 2 || got.OrganizationUUID != "" {
|
||||
t.Fatalf("%+v", got)
|
||||
}
|
||||
if none, err := s.Licence(ctx, "nobody"); none != nil || err != nil {
|
||||
t.Fatalf("%v %v", none, err)
|
||||
}
|
||||
lease := func(holder string) bool {
|
||||
ok, err := s.Lease(ctx, "licence:a", holder, time.Minute)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return ok
|
||||
}
|
||||
if !lease("one") || lease("two") || !lease("one") {
|
||||
t.Fatal("a lease did not refuse a second holder, or its own holder could not renew it")
|
||||
}
|
||||
_ = s.Unlease(ctx, "licence:a", "one")
|
||||
if !lease("two") {
|
||||
t.Fatal("a released lease was not taken")
|
||||
}
|
||||
b1, _ := s.Bind(ctx, "laptop", l.Name)
|
||||
adv, _ := s.Advance(ctx, l.Name)
|
||||
b3, _ := s.Bind(ctx, "laptop", l.Name)
|
||||
if len(adv) != 1 || !(b1.Generation < adv[0].Generation && adv[0].Generation < b3.Generation) {
|
||||
t.Fatalf("generations %d %v %d", b1.Generation, adv, b3.Generation)
|
||||
}
|
||||
_ = s.RecordOutcome(ctx, "sha256:x", "laptop", "u-1", Dead, "400")
|
||||
if o, _ := s.Outcome(ctx, "sha256:x"); o != Dead {
|
||||
t.Fatalf("outcome %q", o)
|
||||
}
|
||||
if o, _ := s.Outcome(ctx, "sha256:none"); o != "" {
|
||||
t.Fatalf("an unknown login is %q", o)
|
||||
}
|
||||
pct := 1.0
|
||||
_ = s.RecordUsage(ctx, l.Name, 5, UsageReading{SessionPct: &pct}, map[string]any{})
|
||||
if u, _ := s.Usage(ctx, "", 5); len(u) != 1 || *u[0].Reading.SessionPct != 1 {
|
||||
t.Fatalf("usage %v", u)
|
||||
}
|
||||
if err := s.Audit(ctx, "bound", map[string]any{"consumer": "laptop"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, _ := s.Unbind(ctx, "laptop"); !ok {
|
||||
t.Fatal("unbind")
|
||||
}
|
||||
all, _ := s.Licences(ctx)
|
||||
if len(all) != 1 || !strings.HasPrefix(all[0].Sealed, "v1.") {
|
||||
t.Fatalf("%v", all)
|
||||
}
|
||||
}
|
||||
|
||||
func TestARefreshThatDoesNotRotateKeepsTheRefreshToken(t *testing.T) {
|
||||
prev := FullGrant{AccessToken: "a", RefreshToken: "r", ExpiresAt: 1}
|
||||
in := int64(60)
|
||||
kept := NextGrant(prev, tokenResponse{AccessToken: "a2", ExpiresIn: &in}, 1000)
|
||||
if !kept.OK || kept.Grant.RefreshToken != "r" || kept.Grant.ExpiresAt != 61_000 {
|
||||
t.Fatalf("%+v", kept)
|
||||
}
|
||||
rotated := NextGrant(prev, tokenResponse{AccessToken: "a3", RefreshToken: "r2"}, 0)
|
||||
if rotated.Grant.RefreshToken != "r2" {
|
||||
t.Fatalf("%+v", rotated)
|
||||
}
|
||||
if NextGrant(prev, tokenResponse{}, 0).OK {
|
||||
t.Fatal("an answer with no access token was a grant")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAGrantAtRestOpensOnlyWithItsKey(t *testing.T) {
|
||||
a, _ := NewCrypt("one key")
|
||||
b, _ := NewCrypt("another key")
|
||||
sealed := a.Seal(`{"refreshToken":"rt"}`)
|
||||
// The plaintext's own words, not two letters a random ciphertext holds one time in about seventy.
|
||||
if strings.Contains(sealed, "refreshToken") {
|
||||
t.Fatal("stored in the clear")
|
||||
}
|
||||
if got, err := a.Open(sealed); err != nil || got != `{"refreshToken":"rt"}` {
|
||||
t.Fatalf("%q %v", got, err)
|
||||
}
|
||||
if _, err := b.Open(sealed); err == nil {
|
||||
t.Fatal("opened with another key")
|
||||
}
|
||||
if _, err := NewCrypt(" "); err == nil {
|
||||
t.Fatal("an empty key was accepted")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,321 @@
|
||||
package main
|
||||
|
||||
// The manager's kept data on the bus (design 39 bus-and-switch §2, §3, B11, B22): the state `licences`, one key per
|
||||
// licence by its key form (§10), and the state `bindings`, one key per consumer, its value the form the consumers
|
||||
// have always read. Written by the manager alone.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// BusStore keeps the manager's data in the bus's own storage.
|
||||
type BusStore struct {
|
||||
LicencesState State
|
||||
BindingsState State
|
||||
LeasesState State
|
||||
OfferedState State
|
||||
UsageLog Log
|
||||
AuditLog Log
|
||||
Now func() time.Time
|
||||
}
|
||||
|
||||
func (s *BusStore) get(st State, key string, into any) (bool, error) {
|
||||
e, err := st.Get(key)
|
||||
if err != nil || e == nil || len(e.Value) == 0 || string(e.Value) == "null" {
|
||||
return false, err
|
||||
}
|
||||
return true, json.Unmarshal(e.Value, into)
|
||||
}
|
||||
|
||||
func (s *BusStore) Licences(context.Context) ([]Licence, error) {
|
||||
keys, err := s.LicencesState.Keys()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := []Licence{}
|
||||
for _, k := range keys {
|
||||
var l Licence
|
||||
if ok, err := s.get(s.LicencesState, k, &l); err != nil {
|
||||
return nil, err
|
||||
} else if ok {
|
||||
out = append(out, l)
|
||||
}
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *BusStore) Licence(_ context.Context, name string) (*Licence, error) {
|
||||
var l Licence
|
||||
ok, err := s.get(s.LicencesState, KeyFor(name), &l)
|
||||
if err != nil || !ok || l.Name != name {
|
||||
return nil, err
|
||||
}
|
||||
return &l, nil
|
||||
}
|
||||
|
||||
func (s *BusStore) LicenceForAccount(ctx context.Context, account string) (*Licence, error) {
|
||||
all, err := s.Licences(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, l := range all {
|
||||
if l.AccountUUID == account {
|
||||
return &l, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *BusStore) SaveLicence(_ context.Context, l Licence) error {
|
||||
if l.Name == "" {
|
||||
return errors.New("a licence has a name")
|
||||
}
|
||||
_, err := s.LicencesState.Put(KeyFor(l.Name), l)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *BusStore) Bindings(context.Context) ([]Binding, error) {
|
||||
keys, err := s.BindingsState.Keys()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := []Binding{}
|
||||
for _, k := range keys {
|
||||
if b, err := s.binding(k); err != nil {
|
||||
return nil, err
|
||||
} else if b != nil {
|
||||
out = append(out, *b)
|
||||
}
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Consumer < out[j].Consumer })
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *BusStore) binding(consumer string) (*Binding, error) {
|
||||
var v BindingState
|
||||
ok, err := s.get(s.BindingsState, consumer, &v)
|
||||
if err != nil || !ok {
|
||||
return nil, err
|
||||
}
|
||||
return &Binding{Consumer: consumer, Licence: v.Licence, Kind: v.Kind, Generation: v.Generation, Since: v.Since,
|
||||
Why: v.Why, By: v.By}, nil
|
||||
}
|
||||
|
||||
func (s *BusStore) Binding(_ context.Context, consumer string) (*Binding, error) {
|
||||
return s.binding(consumer)
|
||||
}
|
||||
|
||||
// Bind writes a consumer's key at its next generation: one more than the higher of its own and the floor (B11).
|
||||
func (s *BusStore) Bind(_ context.Context, consumer, licence string, m Move) (Binding, error) {
|
||||
before, err := s.binding(consumer)
|
||||
if err != nil {
|
||||
return Binding{}, err
|
||||
}
|
||||
generation := m.floor(consumer)
|
||||
if before != nil {
|
||||
generation = max(generation, before.Generation)
|
||||
}
|
||||
b := Binding{Consumer: consumer, Licence: licence, Kind: m.Kind, Generation: generation + 1,
|
||||
Since: s.Now().UTC().Format(time.RFC3339), Why: m.Why, By: m.By}
|
||||
_, err = s.BindingsState.Put(consumer, BindingState{Licence: b.Licence, Kind: b.Kind, Generation: b.Generation,
|
||||
Since: b.Since, Why: b.Why, By: b.By})
|
||||
return b, err
|
||||
}
|
||||
|
||||
func (s *BusStore) Unbind(_ context.Context, consumer string) (bool, error) {
|
||||
before, err := s.binding(consumer)
|
||||
if err != nil || before == nil {
|
||||
return false, err
|
||||
}
|
||||
return true, s.BindingsState.Delete(consumer)
|
||||
}
|
||||
|
||||
// Advance gives every consumer bound to licence its next generation, keeping its licence and since.
|
||||
func (s *BusStore) Advance(ctx context.Context, licence string, m Move) ([]Binding, error) {
|
||||
all, err := s.Bindings(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []Binding
|
||||
for _, b := range all {
|
||||
if b.Licence != licence {
|
||||
continue
|
||||
}
|
||||
b.Generation = max(b.Generation, m.floor(b.Consumer)) + 1
|
||||
b.Kind, b.Why, b.By = m.Kind, m.Why, m.By
|
||||
if _, err := s.BindingsState.Put(b.Consumer, BindingState{Licence: b.Licence, Kind: b.Kind,
|
||||
Generation: b.Generation, Since: b.Since, Why: b.Why, By: b.By}); err != nil {
|
||||
return out, err
|
||||
}
|
||||
out = append(out, b)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ---- leases and offers (§4, §5) ---------------------------------------------------------------------------------
|
||||
|
||||
// Lease takes a lease by creating its key with a lifetime of d (§4): a create fails while another holds it, the bus's
|
||||
// own compare-and-set. A key its own holder holds is taken again.
|
||||
func (s *BusStore) Lease(_ context.Context, key, holder string, d time.Duration) (bool, error) {
|
||||
v := LeaseValue{Holder: holder, Until: s.Now().Add(d).UTC().Format(time.RFC3339)}
|
||||
_, err := s.LeasesState.CreateTTL(key, v, d)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if !errors.Is(err, stdio.ErrChanged) {
|
||||
return false, err
|
||||
}
|
||||
var held LeaseValue
|
||||
if ok, gerr := s.get(s.LeasesState, key, &held); gerr != nil || !ok || held.Holder != holder {
|
||||
return false, gerr
|
||||
}
|
||||
_, err = s.LeasesState.PutTTL(key, v, d)
|
||||
return err == nil, err
|
||||
}
|
||||
|
||||
// Unlease deletes a lease its holder holds; another's is left to it or its lifetime.
|
||||
func (s *BusStore) Unlease(_ context.Context, key, holder string) error {
|
||||
var held LeaseValue
|
||||
if ok, err := s.get(s.LeasesState, key, &held); err != nil || !ok || held.Holder != holder {
|
||||
return err
|
||||
}
|
||||
return s.LeasesState.Delete(key)
|
||||
}
|
||||
|
||||
// offeredFor is how long the outcome of an offered grant is kept (§5): longer than a refresh token lives.
|
||||
const offeredFor = 90 * 24 * time.Hour
|
||||
|
||||
// OfferedKey is a fingerprint's key in `offered`: through the licence key form (B13).
|
||||
func OfferedKey(fingerprint string) string { return KeyFor(fingerprint) }
|
||||
|
||||
func (s *BusStore) Outcome(_ context.Context, fingerprint string) (Outcome, error) {
|
||||
var v OfferedValue
|
||||
if ok, err := s.get(s.OfferedState, OfferedKey(fingerprint), &v); err != nil || !ok {
|
||||
return "", err
|
||||
}
|
||||
return v.Outcome, nil
|
||||
}
|
||||
|
||||
func (s *BusStore) RecordOutcome(_ context.Context, fingerprint, node, account string, o Outcome, why string) error {
|
||||
return s.putOffered(fingerprint, OfferedValue{Node: node, Account: account, Outcome: o, Why: why,
|
||||
At: s.Now().UTC().Format(time.RFC3339)}, offeredFor)
|
||||
}
|
||||
|
||||
func (s *BusStore) putOffered(fingerprint string, v OfferedValue, ttl time.Duration) error {
|
||||
_, err := s.OfferedState.PutTTL(OfferedKey(fingerprint), v, ttl)
|
||||
return err
|
||||
}
|
||||
|
||||
// ---- the logs (§6, §7) ------------------------------------------------------------------------------------------
|
||||
|
||||
func (s *BusStore) RecordUsage(_ context.Context, licence string, at int64, r UsageReading, raw map[string]any) error {
|
||||
_, err := s.UsageLog.Append(KeyFor(licence), UsageEntry{At: time.UnixMilli(at).UTC().Format(time.RFC3339Nano),
|
||||
Reading: r, Raw: raw})
|
||||
return err
|
||||
}
|
||||
|
||||
// Usage is the latest readings, newest first, of one licence or every one, none before since, the vendor's answer
|
||||
// with raw (§35).
|
||||
func (s *BusStore) Usage(ctx context.Context, licence string, since time.Time, limit int, raw bool) ([]UsageRow, error) {
|
||||
names := map[string]string{}
|
||||
all, err := s.Licences(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, l := range all {
|
||||
names[KeyFor(l.Name)] = l.Name
|
||||
}
|
||||
keys := ""
|
||||
if licence != "" {
|
||||
keys = KeyFor(licence)
|
||||
}
|
||||
entries, err := s.newest(s.UsageLog, keys, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := []UsageRow{}
|
||||
for _, e := range entries {
|
||||
var u UsageEntry
|
||||
if json.Unmarshal(e.Value, &u) != nil {
|
||||
continue
|
||||
}
|
||||
at, _ := time.Parse(time.RFC3339Nano, u.At)
|
||||
if at.Before(since) {
|
||||
continue
|
||||
}
|
||||
name := names[e.Key]
|
||||
if name == "" {
|
||||
name = e.Key
|
||||
}
|
||||
row := UsageRow{Licence: name, At: at.UnixMilli(), Reading: u.Reading}
|
||||
if raw {
|
||||
row.Raw = u.Raw
|
||||
}
|
||||
out = append(out, row)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// newest is up to limit entries of keys, newest first: read back from the log's last sequence, a window that doubles
|
||||
// until it holds limit entries or reaches the first.
|
||||
func (s *BusStore) newest(l Log, keys string, limit int) ([]stdio.LogEntry, error) {
|
||||
info, err := l.Info()
|
||||
if err != nil || info.Last == 0 {
|
||||
return nil, err
|
||||
}
|
||||
for span := uint64(max(limit, 1)); ; span *= 2 {
|
||||
from := uint64(1)
|
||||
if info.Last > span {
|
||||
from = info.Last - span + 1
|
||||
}
|
||||
var got []stdio.LogEntry
|
||||
for next := from; next != 0; {
|
||||
page, err := l.Read(next, keys, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
got = append(got, page.Entries...)
|
||||
next = page.Next
|
||||
}
|
||||
if len(got) >= limit || from <= max(info.First, 1) {
|
||||
if len(got) > limit {
|
||||
got = got[len(got)-limit:]
|
||||
}
|
||||
for i, j := 0, len(got)-1; i < j; i, j = i+1, j-1 {
|
||||
got[i], got[j] = got[j], got[i]
|
||||
}
|
||||
return got, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Audit appends one entry under its kind, with when; a log that does not take it fails the act (B20).
|
||||
func (s *BusStore) Audit(_ context.Context, what string, detail map[string]any) error {
|
||||
kind := "drift"
|
||||
for _, k := range AuditKinds {
|
||||
if k == what {
|
||||
kind = k
|
||||
}
|
||||
}
|
||||
entry := map[string]any{"at": s.Now().UTC().Format(time.RFC3339Nano)}
|
||||
if kind != what {
|
||||
entry["what"] = what
|
||||
}
|
||||
for k, v := range detail {
|
||||
entry[k] = v
|
||||
}
|
||||
if _, err := s.AuditLog.Append(kind, entry); err != nil {
|
||||
return fmt.Errorf("the audit did not take the %s entry, so the act is not done: %w", what, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,361 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// memState is a state of the bus as the runtime answers it, in memory: values, their past values, keys' lifetimes.
|
||||
type memState struct {
|
||||
mu sync.Mutex
|
||||
now func() time.Time
|
||||
values map[string]json.RawMessage
|
||||
until map[string]time.Time
|
||||
history map[string][]stdio.StateRevision
|
||||
rev uint64
|
||||
writes int
|
||||
}
|
||||
|
||||
func newMemState(now func() time.Time) *memState {
|
||||
return &memState{now: now, values: map[string]json.RawMessage{}, until: map[string]time.Time{},
|
||||
history: map[string][]stdio.StateRevision{}}
|
||||
}
|
||||
|
||||
func (s *memState) live(key string) bool {
|
||||
if u, ok := s.until[key]; ok && !s.now().Before(u) {
|
||||
delete(s.values, key)
|
||||
delete(s.until, key)
|
||||
}
|
||||
_, ok := s.values[key]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (s *memState) Get(key string) (*stdio.StateEntry, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if !s.live(key) {
|
||||
return nil, nil
|
||||
}
|
||||
return &stdio.StateEntry{Key: key, Value: s.values[key]}, nil
|
||||
}
|
||||
|
||||
func (s *memState) write(key string, value any, ttl time.Duration, create bool) (uint64, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
if create && s.live(key) {
|
||||
return 0, stdio.ErrChanged
|
||||
}
|
||||
raw, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
s.rev++
|
||||
s.writes++
|
||||
s.values[key] = raw
|
||||
delete(s.until, key)
|
||||
if ttl > 0 {
|
||||
s.until[key] = s.now().Add(ttl)
|
||||
}
|
||||
s.history[key] = append(s.history[key], stdio.StateRevision{Key: key, Value: raw, Revision: s.rev, Op: "put", At: s.now()})
|
||||
return s.rev, nil
|
||||
}
|
||||
|
||||
func (s *memState) Put(key string, value any) (uint64, error) { return s.write(key, value, 0, false) }
|
||||
func (s *memState) PutTTL(key string, value any, ttl time.Duration) (uint64, error) {
|
||||
return s.write(key, value, ttl, false)
|
||||
}
|
||||
func (s *memState) Create(key string, value any) (uint64, error) { return s.write(key, value, 0, true) }
|
||||
func (s *memState) CreateTTL(key string, value any, ttl time.Duration) (uint64, error) {
|
||||
return s.write(key, value, ttl, true)
|
||||
}
|
||||
|
||||
func (s *memState) Delete(key string) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.rev++
|
||||
s.writes++
|
||||
delete(s.values, key)
|
||||
delete(s.until, key)
|
||||
s.history[key] = append(s.history[key], stdio.StateRevision{Key: key, Revision: s.rev, Op: "delete", At: s.now()})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *memState) Keys() ([]string, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
out := []string{}
|
||||
for k := range s.values {
|
||||
if s.live(k) {
|
||||
out = append(out, k)
|
||||
}
|
||||
}
|
||||
sort.Strings(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *memState) History(key string) ([]stdio.StateRevision, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return append([]stdio.StateRevision(nil), s.history[key]...), nil
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §2: a rotation and a reading each write the licence's key, by its key form, and no field
|
||||
// holds a token.
|
||||
func TestARotationAndAReadingWriteTheLicenceKey(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
|
||||
key := KeyFor(licence1)
|
||||
before := len(mm.licences.history[key])
|
||||
if _, err := mm.m.Rotate(ctx, licence1, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := mm.m.ReadUsage(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := len(mm.licences.history[key]); got < before+2 {
|
||||
t.Fatalf("the key %s was written %d times by a rotation and a reading, want 2", key, got-before)
|
||||
}
|
||||
for _, r := range mm.licences.history[key] {
|
||||
for _, token := range []string{"rt-", "at-"} {
|
||||
if strings.Contains(string(r.Value), token) {
|
||||
t.Fatalf("a value of %s holds %s: %s", key, token, r.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
var l Licence
|
||||
_ = json.Unmarshal(mm.licences.values[key], &l)
|
||||
if l.Name != licence1 || l.AccountUUID != "acct-1" || !strings.Contains(string(mm.licences.values[key]), `"account":"acct-1"`) {
|
||||
t.Fatalf("the key holds %+v", l)
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §3, B11: bind, switch, release and a rotation each write or delete the consumer's key with
|
||||
// its why and by; each consumer counts its own generations, past what its node reported.
|
||||
func TestEachMoveWritesTheBindingWithWhyAndBy(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
|
||||
_ = mm.store.SaveLicence(ctx, Licence{Name: "api", Kind: "api-key", Generation: 1})
|
||||
mm.m.Reported = func(node string) int64 {
|
||||
if node == "server" {
|
||||
return 40
|
||||
}
|
||||
return 0
|
||||
}
|
||||
asked := WithCause(ctx, Cause{Why: "the switch verb", By: "jane"})
|
||||
if _, err := mm.m.Bind(asked, "server", "api"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b := mm.bound("server"); b.Licence != "api" || b.Generation != 41 || b.Why != "the switch verb" || b.By != "jane" ||
|
||||
b.Since == "" || b.Kind != "api-key" {
|
||||
t.Fatalf("server's binding %+v", b)
|
||||
}
|
||||
laptop := mm.bound("laptop").Generation
|
||||
if _, err := mm.m.Bind(asked, "laptop", "api"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b := mm.bound("laptop"); b.Generation != laptop+1 || b.Licence != "api" {
|
||||
t.Fatalf("laptop's switch %+v, from generation %d", b, laptop)
|
||||
}
|
||||
if _, err := mm.m.Release(WithCause(ctx, Cause{Why: "the release verb", By: "jane"}), "server"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := mm.bindings.values["server"]; ok {
|
||||
t.Fatal("release left the key")
|
||||
}
|
||||
if h, _ := mm.bindings.History("server"); len(h) != 2 || h[1].Op != "delete" {
|
||||
t.Fatalf("server's history %v", h)
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §4, §5, B13: a lease is a key created with a two-minute lifetime that another holder cannot
|
||||
// create; an offered grant's outcome is kept under its fingerprint in the key form for 90 days.
|
||||
func TestALeaseAndAnOfferAreKeptByTheirKeysAndLifetimes(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
key := "licence." + KeyFor(licence1)
|
||||
if ok, err := mm.store.Lease(ctx, key, "one", leaseFor); !ok || err != nil {
|
||||
t.Fatalf("the first lease: %v %v", ok, err)
|
||||
}
|
||||
if ok, _ := mm.store.Lease(ctx, key, "two", leaseFor); ok {
|
||||
t.Fatal("a second holder took a live lease")
|
||||
}
|
||||
if u := mm.leases.until[key]; !u.Equal(mm.now.Add(2 * time.Minute)) {
|
||||
t.Fatalf("the lease lives until %v", u)
|
||||
}
|
||||
mm.now = mm.now.Add(2 * time.Minute)
|
||||
if ok, _ := mm.store.Lease(ctx, key, "two", leaseFor); !ok {
|
||||
t.Fatal("a lease past its lifetime was not taken")
|
||||
}
|
||||
_ = mm.store.Unlease(ctx, key, "one")
|
||||
if _, held := mm.leases.values[key]; !held {
|
||||
t.Fatal("another holder's lease was ended")
|
||||
}
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-dead", false, t0)})
|
||||
fp := OfferedKey(Fingerprint("rt-dead"))
|
||||
if strings.ContainsAny(fp, ":") || !strings.HasPrefix(fp, "sha256_") {
|
||||
t.Fatalf("the offered key %q is not in the key form", fp)
|
||||
}
|
||||
var v OfferedValue
|
||||
_ = json.Unmarshal(mm.offered.values[fp], &v)
|
||||
if v.Outcome != Dead || v.Node != "laptop" || v.Account != "acct-1" || v.Why == "" || v.At == "" {
|
||||
t.Fatalf("offered %+v", v)
|
||||
}
|
||||
if u := mm.offered.until[fp]; !u.Equal(mm.now.Add(90 * 24 * time.Hour)) {
|
||||
t.Fatalf("the outcome lives until %v", u)
|
||||
}
|
||||
}
|
||||
|
||||
// memLog is a log of the bus as the runtime answers it, in memory; full, it refuses an entry.
|
||||
type memLog struct {
|
||||
mu sync.Mutex
|
||||
now func() time.Time
|
||||
entries []stdio.LogEntry
|
||||
full bool
|
||||
}
|
||||
|
||||
func newMemLog(now func() time.Time) *memLog { return &memLog{now: now} }
|
||||
|
||||
func (l *memLog) Append(key string, value any) (uint64, error) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
if l.full {
|
||||
return 0, errors.New("the log is full: maximum bytes exceeded")
|
||||
}
|
||||
raw, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
seq := uint64(len(l.entries) + 1)
|
||||
l.entries = append(l.entries, stdio.LogEntry{Sequence: seq, Key: key, Value: raw, At: l.now()})
|
||||
return seq, nil
|
||||
}
|
||||
|
||||
func (l *memLog) Read(from uint64, keys string, limit int) (*stdio.LogPage, error) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
p := &stdio.LogPage{Entries: []stdio.LogEntry{}, Last: uint64(len(l.entries))}
|
||||
for _, e := range l.entries {
|
||||
if e.Sequence >= from && (keys == "" || e.Key == keys) {
|
||||
if limit > 0 && len(p.Entries) == limit {
|
||||
p.Next = e.Sequence
|
||||
break
|
||||
}
|
||||
p.Entries = append(p.Entries, e)
|
||||
}
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (l *memLog) Last(key string) (*stdio.LogEntry, error) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
for i := len(l.entries) - 1; i >= 0; i-- {
|
||||
if l.entries[i].Key == key {
|
||||
e := l.entries[i]
|
||||
return &e, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (l *memLog) Info() (*stdio.LogInfo, error) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
i := &stdio.LogInfo{Entries: uint64(len(l.entries)), Last: uint64(len(l.entries))}
|
||||
if len(l.entries) > 0 {
|
||||
i.First = 1
|
||||
}
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func (l *memLog) Newest(keys string) ([]stdio.LogEntry, error) {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
last := map[string]int{}
|
||||
for i, e := range l.entries {
|
||||
if keys == "" || e.Key == keys {
|
||||
last[e.Key] = i
|
||||
}
|
||||
}
|
||||
out := []stdio.LogEntry{}
|
||||
for i, e := range l.entries {
|
||||
if j, ok := last[e.Key]; ok && j == i {
|
||||
out = append(out, e)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// under is every entry of a log's subject, decoded.
|
||||
func (l *memLog) under(key string) []map[string]any {
|
||||
l.mu.Lock()
|
||||
defer l.mu.Unlock()
|
||||
var out []map[string]any
|
||||
for _, e := range l.entries {
|
||||
if key == "" || e.Key == key {
|
||||
var v map[string]any
|
||||
_ = json.Unmarshal(e.Value, &v)
|
||||
v["what"] = e.Key
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §6: a reading appends one entry under its licence's key, the reading beside the raw answer.
|
||||
func TestAReadingAppendsOneUsageEntry(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
|
||||
if err := mm.m.ReadUsage(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := mm.usage.under(KeyFor(licence1))
|
||||
if len(got) != 1 || len(mm.usage.entries) != 1 {
|
||||
t.Fatalf("a reading appended %v", mm.usage.entries)
|
||||
}
|
||||
if got[0]["raw"] == nil || got[0]["reading"] == nil || got[0]["at"] == nil {
|
||||
t.Fatalf("the entry %v", got[0])
|
||||
}
|
||||
rows, _ := mm.store.Usage(ctx, licence1, time.Time{}, 20, false)
|
||||
if len(rows) != 1 || rows[0].Licence != licence1 || rows[0].Reading.SessionPct == nil {
|
||||
t.Fatalf("usage answers %+v", rows)
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §7, B20: each act appends one entry under its kind; a full log fails the act loudly.
|
||||
func TestEachActAppendsOneAuditEntry(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
|
||||
_, _ = mm.m.Rotate(ctx, licence1, true)
|
||||
_ = mm.store.SaveLicence(ctx, Licence{Name: "api", Kind: "api-key", Generation: 1})
|
||||
_, _ = mm.m.Bind(ctx, "laptop", "api")
|
||||
_, _ = mm.m.Release(ctx, "laptop")
|
||||
for kind, n := range map[string]int{"adopted": 1, "bound": 1, "rotated": 1, "switched": 1, "released": 1} {
|
||||
if got := len(mm.audit.under(kind)); got != n {
|
||||
t.Errorf("%s: %d entries, want %d", kind, got, n)
|
||||
}
|
||||
}
|
||||
for _, e := range mm.audit.under("") {
|
||||
if e["at"] == nil || e["by"] == nil && e["what"] != "adopted" {
|
||||
t.Errorf("the entry %v has no at or by", e)
|
||||
}
|
||||
}
|
||||
mm.audit.full = true
|
||||
if _, err := mm.m.Bind(ctx, "laptop", "api"); err == nil || !strings.Contains(err.Error(), "the audit did not take") {
|
||||
t.Fatalf("a bind with a full audit log: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
package main
|
||||
|
||||
// The manager's conditions (design 39 bus-and-switch §9, §11, §29 to §31, §33, B17, B37 to B39): each said on the
|
||||
// operator channel as any module says one (novox/hq ADR 0437 §9), the router alone deciding whether and where the
|
||||
// operator is told, and each it said raised kept in its state `conditions`, written before it is said and deleted when
|
||||
// it is said cleared. A raise or a clear is said again only when what it says changes (ADR 0289 §1); at its start the
|
||||
// manager judges every kind anew, says raised again each it still holds and cleared each key it no longer holds.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
||||
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// HeadlineLength is the operator channel's limit on a headline; a longer one is cut, ending in "…" (B39).
|
||||
const HeadlineLength = 60
|
||||
|
||||
// SayCondition publishes a condition on the operator channel's condition accept, under the manager's name, in the form
|
||||
// its client encodes (ADR 0437 §9).
|
||||
func SayCondition(c oc.Condition) error {
|
||||
subject, err := occ.ConditionSubject(Publisher)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
raw, err := occ.EncodeCondition(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return stdio.SeatPublish(subject, json.RawMessage(raw), "")
|
||||
}
|
||||
|
||||
// condition is a raised condition of the manager: its key, severity, words and the line said when it is over. An
|
||||
// empty needs is "Nothing for you to do."; the explanation is opened by its verdict here.
|
||||
func condition(key, severity, headline, summary, needs, explanation, resolved string) oc.Condition {
|
||||
verdict := occ.NothingToDo
|
||||
if needs != "" {
|
||||
verdict = occ.NeedsYou + needs + "."
|
||||
}
|
||||
if explanation != "" {
|
||||
verdict += " " + explanation
|
||||
}
|
||||
return oc.Condition{Key: key, Severity: severity, State: occ.StateRaised,
|
||||
Subject: oc.ConditionSubject{Scope: "module", ID: Publisher},
|
||||
Headline: cut(headline), Summary: summary, Needs: needs, Explanation: verdict, Resolved: resolved}
|
||||
}
|
||||
|
||||
// cut keeps a headline within the operator channel's limit, ending a longer one in "…" (B39).
|
||||
func cut(headline string) string {
|
||||
r := []rune(headline)
|
||||
if len(r) <= HeadlineLength {
|
||||
return headline
|
||||
}
|
||||
return string(r[:HeadlineLength-1]) + "…"
|
||||
}
|
||||
|
||||
// short is a licence's short name for a headline: its address before the "@", or its name (B39).
|
||||
func short(name string) string {
|
||||
if at := strings.Index(name, "@"); at > 0 {
|
||||
return name[:at]
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// machines is a count of consumers in a headline (B39).
|
||||
func machines(n int) string {
|
||||
if n == 1 {
|
||||
return "1 machine"
|
||||
}
|
||||
return fmt.Sprintf("%d machines", n)
|
||||
}
|
||||
|
||||
// Judge is every condition the manager holds now, judged anew from its own state and readings, by key.
|
||||
func (m *Manager) Judge(ctx context.Context) (map[string]oc.Condition, error) {
|
||||
held := map[string]oc.Condition{}
|
||||
s := m.settings()
|
||||
reasons := s.Reasons()
|
||||
for _, o := range s.OutOfBound {
|
||||
bound := fmt.Sprintf("at least %g", o.Min)
|
||||
if o.HasMax {
|
||||
bound = fmt.Sprintf("between %g and %g", o.Min, o.Max)
|
||||
}
|
||||
key := "settings." + o.Setting + ".invalid"
|
||||
held[key] = condition(key, occ.SeverityWarning, o.Setting+" is outside its bound",
|
||||
reasons[o.Setting],
|
||||
fmt.Sprintf("set %s %s", o.Setting, bound),
|
||||
fmt.Sprintf("%s is set to %g; the licence manager uses its default %g until it is fixed.", o.Setting,
|
||||
o.Value, o.Default), o.Setting+" is within its bound again")
|
||||
}
|
||||
for _, p := range s.Invalid() {
|
||||
key := "settings." + p.Setting + ".invalid"
|
||||
if _, ok := held[key]; ok {
|
||||
continue
|
||||
}
|
||||
held[key] = condition(key, occ.SeverityWarning, p.Setting+" is above switch_at_pct",
|
||||
reasons[p.Setting],
|
||||
fmt.Sprintf("set %s at or below switch_at_pct (%g)", p.Setting, p.SwitchAt),
|
||||
fmt.Sprintf("%s is %g, above switch_at_pct %g; the licence manager moves no consumer until it is fixed.",
|
||||
p.Setting, p.Value, p.SwitchAt), p.Setting+" is at or below switch_at_pct again")
|
||||
}
|
||||
licences, err := m.Store.Licences(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bindings, err := m.Store.Bindings(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, l := range licences {
|
||||
for key, c := range m.judgeLicence(l, licences, bindings, s) {
|
||||
held[key] = c
|
||||
}
|
||||
}
|
||||
return held, nil
|
||||
}
|
||||
|
||||
// judgeLicence is what licence l holds of no-room (§29), near-limit (§30) and usage-unread (§31).
|
||||
func (m *Manager) judgeLicence(l Licence, licences []Licence, bindings []Binding, s Settings) map[string]oc.Condition {
|
||||
held := map[string]oc.Condition{}
|
||||
key := "licence." + KeyFor(l.Name) + "."
|
||||
consumers := consumersOf(l.Name, bindings)
|
||||
list := strings.Join(consumers, ", ")
|
||||
if r := m.RuleFor(l, licences, bindings, s); r.NoTarget {
|
||||
o := r.Over
|
||||
var rooms []string
|
||||
for _, other := range licences {
|
||||
if other.Name != l.Name {
|
||||
rooms = append(rooms, other.Name+" "+m.roomWords(other, s))
|
||||
}
|
||||
}
|
||||
sort.Strings(rooms)
|
||||
others := "no other licence is held"
|
||||
if len(rooms) > 0 {
|
||||
others = strings.Join(rooms, "; ")
|
||||
}
|
||||
held[key+"no-room"] = condition(key+"no-room", occ.SeverityUrgent,
|
||||
"No licence has room for "+machines(len(consumers)),
|
||||
fmt.Sprintf("no licence has room for %s, bound to %s at %g %% of its %s window", list, l.Name, o.Pct, o.Window),
|
||||
fmt.Sprintf("add a licence, or wait for %s's %s window to reset at %s", l.Name, o.Window, o.ResetsAt),
|
||||
fmt.Sprintf("%s is at %g %% of its %s window, with %s bound to it; %s.", l.Name, o.Pct, o.Window, list, others),
|
||||
list+" have a licence with room again")
|
||||
}
|
||||
if window, pct, resetsAt, ok := windowAt(l.Usage, s.WarnAt); ok && len(consumers) > 0 {
|
||||
needs, plan := "", ""
|
||||
if to, _ := m.targetFor(l, licences, s); to != "" {
|
||||
plan = fmt.Sprintf("%s move to %s at %g %%. ", list, to, s.SwitchAt)
|
||||
} else {
|
||||
needs = "no other licence has room"
|
||||
}
|
||||
held[key+"near-limit"] = condition(key+"near-limit", occ.SeverityWarning,
|
||||
fmt.Sprintf("%s at %g%% %s", short(l.Name), pct, window),
|
||||
fmt.Sprintf("%s at %g %% of its %s window", l.Name, pct, window), needs,
|
||||
fmt.Sprintf("%s%s is at %g %% of its %s window, which resets at %s; %s bound to it.", plan, l.Name, pct,
|
||||
window, resetsAt, list), fmt.Sprintf("%s is below %g %% again", l.Name, s.WarnAt))
|
||||
}
|
||||
if since, stale := m.unread(l, s); stale {
|
||||
needs := ""
|
||||
if len(consumers) > 0 {
|
||||
needs = list + " are not moved off it while its usage is unknown"
|
||||
}
|
||||
why := l.Usage.ReadErrorWords()
|
||||
held[key+"usage-unread"] = condition(key+"usage-unread", occ.SeverityWarning,
|
||||
short(l.Name)+"'s usage cannot be read",
|
||||
fmt.Sprintf("the usage of %s cannot be read: %s", l.Name, why), needs,
|
||||
fmt.Sprintf("%s, older than %g minutes; the vendor answers: %s.", since, s.UsageStale.Minutes(), why),
|
||||
l.Name+"'s usage is read again")
|
||||
}
|
||||
return held
|
||||
}
|
||||
|
||||
// unread is whether subscription l's latest successful reading is older than usage_stale_minutes (§31), or, never
|
||||
// read successfully, its adoption is (B38); and the words naming since when.
|
||||
func (m *Manager) unread(l Licence, s Settings) (string, bool) {
|
||||
if l.Kind != "subscription" {
|
||||
return "", false
|
||||
}
|
||||
if l.Usage != nil && l.Usage.ReadAt != "" {
|
||||
read, err := time.Parse(time.RFC3339Nano, l.Usage.ReadAt)
|
||||
if err != nil {
|
||||
return "its latest successful reading has no time", true
|
||||
}
|
||||
return "its latest successful reading was at " + l.Usage.ReadAt, m.Now().Sub(read) > s.UsageStale
|
||||
}
|
||||
if l.AdoptedAt == 0 {
|
||||
return "", false
|
||||
}
|
||||
adopted := time.UnixMilli(l.AdoptedAt)
|
||||
return "no reading has succeeded since its adoption at " + adopted.UTC().Format(time.RFC3339),
|
||||
m.Now().Sub(adopted) > s.UsageStale
|
||||
}
|
||||
|
||||
// roomWords is another licence's room for the condition no-room, or why it is not known or not enough (§29).
|
||||
func (m *Manager) roomWords(o Licence, s Settings) string {
|
||||
room, known := Room(o, m.Now(), s)
|
||||
switch {
|
||||
case known && *o.Usage.WeeklyPct >= s.RoomBelow:
|
||||
return fmt.Sprintf("has %g %% room, its weekly window not below %g %%", room, s.RoomBelow)
|
||||
case known:
|
||||
return fmt.Sprintf("has %g %% room", room)
|
||||
case o.Kind != "subscription":
|
||||
return "is an API key, whose room is never known"
|
||||
case o.Usage == nil || o.Usage.ReadAt == "":
|
||||
return "has no reading"
|
||||
case o.Usage.WeeklyPct == nil:
|
||||
return "has no weekly window in its reading"
|
||||
case o.Failures >= s.FailuresToNotify || (o.RefreshExpiresAt != 0 && o.RefreshExpiresAt <= m.Now().UnixMilli()):
|
||||
return "does not refresh"
|
||||
}
|
||||
return fmt.Sprintf("was not read within %g minutes", s.UsageStale.Minutes())
|
||||
}
|
||||
|
||||
// SayConditions judges every kind anew and says what changed: each condition held whose words differ from those kept
|
||||
// is written in `conditions`, then said raised; each kept key no longer held is said cleared, then deleted. With
|
||||
// again (at the start, and by licences_resync), each held is said raised again whether or not it changed.
|
||||
func (m *Manager) SayConditions(ctx context.Context, again bool) error {
|
||||
if m.Conditions == nil || m.Say == nil {
|
||||
return nil
|
||||
}
|
||||
m.conditionsMu.Lock()
|
||||
defer m.conditionsMu.Unlock()
|
||||
held, err := m.Judge(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
keys, err := m.Conditions.Keys()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var errs []error
|
||||
for _, key := range keys {
|
||||
if _, ok := held[key]; ok {
|
||||
continue
|
||||
}
|
||||
var kept KeptCondition
|
||||
e, err := m.Conditions.Get(key)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
}
|
||||
if e != nil && len(e.Value) > 0 {
|
||||
_ = json.Unmarshal(e.Value, &kept)
|
||||
}
|
||||
cleared := kept.Body
|
||||
cleared.Key, cleared.State = key, occ.StateCleared
|
||||
cleared.Subject = oc.ConditionSubject{Scope: "module", ID: Publisher}
|
||||
if err := m.Say(cleared); err != nil {
|
||||
errs = append(errs, fmt.Errorf("the clearing of %s could not be said: %w", key, err))
|
||||
continue
|
||||
}
|
||||
if err := m.Conditions.Delete(key); err != nil {
|
||||
errs = append(errs, err)
|
||||
}
|
||||
}
|
||||
var order []string
|
||||
for key := range held {
|
||||
order = append(order, key)
|
||||
}
|
||||
sort.Strings(order)
|
||||
for _, key := range order {
|
||||
c := held[key]
|
||||
kept := KeptCondition{Body: c, Since: m.Now().UTC().Format(time.RFC3339)}
|
||||
if e, err := m.Conditions.Get(key); err == nil && e != nil && len(e.Value) > 0 {
|
||||
var was KeptCondition
|
||||
if json.Unmarshal(e.Value, &was) == nil {
|
||||
if !again && sameCondition(was.Body, c) {
|
||||
continue
|
||||
}
|
||||
if was.Since != "" {
|
||||
kept.Since = was.Since
|
||||
}
|
||||
}
|
||||
}
|
||||
if _, err := m.Conditions.Put(key, kept); err != nil {
|
||||
errs = append(errs, err)
|
||||
continue
|
||||
}
|
||||
if err := m.Say(c); err != nil {
|
||||
// Not said: the key goes again, so the next judgement says it.
|
||||
_ = m.Conditions.Delete(key)
|
||||
errs = append(errs, fmt.Errorf("%s could not be said: %w", key, err))
|
||||
}
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
return fmt.Errorf("conditions: %v", errs)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sameCondition is whether two raised conditions say the same.
|
||||
func sameCondition(a, b oc.Condition) bool {
|
||||
ra, _ := json.Marshal(a)
|
||||
rb, _ := json.Marshal(b)
|
||||
return string(ra) == string(rb)
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
occ "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/clients/operatorchannelclient"
|
||||
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// saying gives mm's manager a state `conditions` and records each condition it says, in the form the operator
|
||||
// channel's client encodes, refusing what it refuses.
|
||||
func saying(mm *miniMesh) (*memState, *[]oc.Condition) {
|
||||
kept := newMemState(func() time.Time { return mm.now })
|
||||
said := &[]oc.Condition{}
|
||||
mm.m.Conditions = kept
|
||||
mm.m.Say = func(c oc.Condition) error {
|
||||
if _, err := occ.EncodeCondition(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := occ.ConditionSubject(Publisher); err != nil {
|
||||
return err
|
||||
}
|
||||
*said = append(*said, c)
|
||||
return nil
|
||||
}
|
||||
return kept, said
|
||||
}
|
||||
|
||||
// of is what was said of key, as "state" per saying.
|
||||
func of(said []oc.Condition, key string) []string {
|
||||
var out []string
|
||||
for _, c := range said {
|
||||
if c.Key == key {
|
||||
out = append(out, c.State)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §9/AC1, §11, §33, B17, B37: each kind of condition the manager holds of itself is written in
|
||||
// `conditions` with its body and since, said once with its key, severity, scope and id, its words opened by their
|
||||
// verdict, and said cleared, its key deleted, once it no longer holds.
|
||||
func TestAConditionIsKeptThenSaidOnce(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
file := filepath.Join(t.TempDir(), "settings.json")
|
||||
for _, c := range []struct {
|
||||
name, key, severity string
|
||||
raise, fix func(mm *miniMesh)
|
||||
}{
|
||||
{"an invalid pair", "settings.room_below_pct.invalid", occ.SeverityWarning,
|
||||
func(mm *miniMesh) { mm.m.Settings.RoomBelow = 100 }, func(mm *miniMesh) { mm.m.Settings.RoomBelow = 90 }},
|
||||
{"a setting outside its bound", "settings.switch_at_pct.invalid", occ.SeverityWarning,
|
||||
func(mm *miniMesh) {
|
||||
_ = os.WriteFile(file, []byte(`{"switch_at_pct": 150}`), 0o600)
|
||||
mm.m.Settings = SettingsFrom(file)
|
||||
},
|
||||
func(mm *miniMesh) {
|
||||
_ = os.WriteFile(file, []byte(`{"switch_at_pct": 98}`), 0o600)
|
||||
mm.m.Settings = SettingsFrom(file)
|
||||
}},
|
||||
} {
|
||||
mm := newMesh(t)
|
||||
kept, said := saying(mm)
|
||||
c.raise(mm)
|
||||
for range 2 {
|
||||
if err := mm.m.SayConditions(ctx, false); err != nil {
|
||||
t.Fatalf("%s: %v", c.name, err)
|
||||
}
|
||||
}
|
||||
if got := of(*said, c.key); strings.Join(got, ",") != "raised" {
|
||||
t.Fatalf("%s: said %v of %s, all said %+v", c.name, got, c.key, *said)
|
||||
}
|
||||
s := (*said)[0]
|
||||
if s.Severity != c.severity || s.Subject.Scope != "module" || s.Subject.ID != Publisher || s.Headline == "" ||
|
||||
s.Summary == "" || s.Resolved == "" || !strings.HasPrefix(s.Explanation, occ.NeedsYou+s.Needs) || s.Needs == "" {
|
||||
t.Fatalf("%s: said %+v", c.name, s)
|
||||
}
|
||||
var k KeptCondition
|
||||
if e, _ := kept.Get(c.key); e == nil || json.Unmarshal(e.Value, &k) != nil || k.Body.Key != c.key || k.Since == "" {
|
||||
t.Fatalf("%s: kept %+v", c.name, k)
|
||||
}
|
||||
c.fix(mm)
|
||||
_ = mm.m.SayConditions(ctx, false)
|
||||
if got := of(*said, c.key); strings.Join(got, ",") != "raised,cleared" {
|
||||
t.Fatalf("%s: said %v", c.name, got)
|
||||
}
|
||||
if e, _ := kept.Get(c.key); e != nil {
|
||||
t.Fatalf("%s: the key is kept after its clearing", c.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §9/AC2, design 39 parts §11: at its start the manager says cleared each key of `conditions` it no longer holds,
|
||||
// and says raised again each it still holds, unchanged or not.
|
||||
func TestAStartClearsWhatItNoLongerHolds(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
mm := newMesh(t)
|
||||
kept, said := saying(mm)
|
||||
gone := condition("licence.gone.no-room", occ.SeverityUrgent, "No licence has room for 1 machine", "s", "add one",
|
||||
"", "x has a licence with room again")
|
||||
mm.m.Settings.WarnAt = 100
|
||||
_ = mm.m.SayConditions(ctx, false)
|
||||
_, _ = kept.Put(gone.Key, KeptCondition{Body: gone, Since: "2026-11-11T20:00:00Z"})
|
||||
// design 39 parts §11: a carry-over-failed key kept from before the carry-over left is cleared like any other.
|
||||
carry := condition("carry-over-failed", occ.SeverityUrgent, "The licence manager could not carry over its licences",
|
||||
"s", "", "", "The licence manager carried over its licences")
|
||||
_, _ = kept.Put(carry.Key, KeptCondition{Body: carry, Since: "2026-11-12T09:00:00Z"})
|
||||
*said = nil
|
||||
if err := mm.m.SayConditions(ctx, true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Join(of(*said, gone.Key), ",") != "cleared" || strings.Join(of(*said, carry.Key), ",") != "cleared" ||
|
||||
strings.Join(of(*said, "settings.warn_at_pct.invalid"), ",") != "raised" {
|
||||
t.Fatalf("a start said %+v", *said)
|
||||
}
|
||||
if (*said)[0].Resolved != carry.Resolved || (*said)[1].Resolved != gone.Resolved {
|
||||
t.Fatalf("the clearings carry %+v", *said)
|
||||
}
|
||||
for _, key := range []string{gone.Key, carry.Key} {
|
||||
if e, _ := kept.Get(key); e != nil {
|
||||
t.Fatalf("%s, no longer held, is kept", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §29/AC1, B39: with every licence at its threshold, no-room is said once, its headline a
|
||||
// count of machines, its words naming the licence, the window's reset and every other licence's room; a reading with
|
||||
// room clears it.
|
||||
func TestNoRoomIsSaidOnceAndClearedByRoom(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
mm := fourOnA(t)
|
||||
_, said := saying(mm)
|
||||
_ = mm.store.SaveLicence(ctx, sub("b", read(20, 95)))
|
||||
_ = mm.store.SaveLicence(ctx, sub("c", read(40, 99)))
|
||||
for range 2 {
|
||||
_ = mm.m.SayConditions(ctx, false)
|
||||
}
|
||||
key := "licence.a.no-room"
|
||||
if strings.Join(of(*said, key), ",") != "raised" {
|
||||
t.Fatalf("said %+v", *said)
|
||||
}
|
||||
var c oc.Condition
|
||||
for _, s := range *said {
|
||||
if s.Key == key {
|
||||
c = s
|
||||
}
|
||||
}
|
||||
if c.Severity != occ.SeverityUrgent || c.Headline != "No licence has room for 4 machines" ||
|
||||
c.Needs != "add a licence, or wait for a's weekly window to reset at 2026-11-14T09:00:00Z" ||
|
||||
!strings.Contains(c.Explanation, "a is at 99 % of its weekly window, with ace, g14, laptop, server bound to it") ||
|
||||
!strings.Contains(c.Explanation, "b has 5 % room, its weekly window not below 90 %") ||
|
||||
c.Resolved != "ace, g14, laptop, server have a licence with room again" {
|
||||
t.Fatalf("no-room %+v", c)
|
||||
}
|
||||
_ = mm.store.SaveLicence(ctx, sub("b", read(10, 20)))
|
||||
_ = mm.m.SayConditions(ctx, false)
|
||||
if strings.Join(of(*said, key), ",") != "raised,cleared" {
|
||||
t.Fatalf("said %v after a reading with room", of(*said, key))
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §30/AC1, B39: a rising series of readings says near-limit once at the crossing and again only
|
||||
// when its words change; with no other licence having room it needs the operator; a reading after the reset clears it.
|
||||
func TestNearLimitIsSaidOncePerCrossing(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
mm := fourOnA(t)
|
||||
_, said := saying(mm)
|
||||
key := "licence.a.near-limit"
|
||||
var last oc.Condition
|
||||
for _, w := range []float64{80, 89, 91, 91, 95} {
|
||||
_ = mm.store.SaveLicence(ctx, sub("a", read(95, w))) // a session at 95 % takes no part (C4)
|
||||
_ = mm.m.SayConditions(ctx, false)
|
||||
for _, s := range *said {
|
||||
if s.Key == key {
|
||||
last = s
|
||||
}
|
||||
}
|
||||
}
|
||||
if strings.Join(of(*said, key), ",") != "raised,raised" || last.Headline != "a at 95% weekly" || last.Needs != "" ||
|
||||
!strings.HasPrefix(last.Explanation, occ.NothingToDo+" ace, g14, laptop, server move to b at 99 %.") ||
|
||||
!strings.Contains(last.Explanation, "which resets at 2026-11-14T09:00:00Z") {
|
||||
t.Fatalf("said %v, last %+v", of(*said, key), last)
|
||||
}
|
||||
_ = mm.store.SaveLicence(ctx, sub("b", read(0, 95)))
|
||||
_ = mm.store.SaveLicence(ctx, sub("c", read(0, 95)))
|
||||
_ = mm.m.SayConditions(ctx, false)
|
||||
if c := (*said)[len(*said)-1]; c.Key != key || c.Needs != "no other licence has room" {
|
||||
t.Fatalf("with no target said %+v", c)
|
||||
}
|
||||
_ = mm.store.SaveLicence(ctx, sub("a", read(3, 1)))
|
||||
_ = mm.m.SayConditions(ctx, false)
|
||||
if got := of(*said, key); got[len(got)-1] != "cleared" || len(got) != 4 {
|
||||
t.Fatalf("after the reset said %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §31/AC1, B34, B38: three failed readings in a row, the latest success then older than
|
||||
// usage_stale_minutes, say usage-unread naming the vendor's reason, needing the operator for the consumers bound to it;
|
||||
// one success clears it. A subscription never read successfully is said once its adoption is that old.
|
||||
func TestUsageUnreadIsSaidAndCleared(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
mm := newMesh(t)
|
||||
_, said := saying(mm)
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
|
||||
if err := mm.m.ReadUsage(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key := "licence." + KeyFor(licence1) + ".usage-unread"
|
||||
mm.vendor.usageErr = errors.New("429 Too Many Requests: rate limited")
|
||||
for i := 1; i <= 3; i++ {
|
||||
mm.now = t0.Add(time.Duration(i) * 6 * time.Minute)
|
||||
if err := mm.m.ReadUsage(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := len(of(*said, key)); (i < 3 && got != 0) || (i == 3 && got != 1) {
|
||||
t.Fatalf("after %d failed readings said %v", i, of(*said, key))
|
||||
}
|
||||
}
|
||||
c := (*said)[len(*said)-1]
|
||||
if c.Severity != occ.SeverityWarning || c.Needs != "laptop are not moved off it while its usage is unknown" ||
|
||||
!strings.Contains(c.Explanation, "the vendor answers: 429 Too Many Requests: rate limited") {
|
||||
t.Fatalf("usage-unread %+v", c)
|
||||
}
|
||||
mm.vendor.usageErr = nil
|
||||
_ = mm.m.ReadUsage(ctx)
|
||||
if strings.Join(of(*said, key), ",") != "raised,cleared" {
|
||||
t.Fatalf("after a success said %v", of(*said, key))
|
||||
}
|
||||
// B38: never read successfully since its adoption.
|
||||
never := Licence{Name: "new@example.org", Kind: "subscription", AdoptedAt: mm.now.UnixMilli()}
|
||||
_ = mm.store.SaveLicence(ctx, never)
|
||||
_ = mm.m.SayConditions(ctx, false)
|
||||
newKey := "licence." + KeyFor(never.Name) + ".usage-unread"
|
||||
if len(of(*said, newKey)) != 0 {
|
||||
t.Fatal("said at its adoption")
|
||||
}
|
||||
mm.now = mm.now.Add(16 * time.Minute)
|
||||
_ = mm.m.SayConditions(ctx, false)
|
||||
if strings.Join(of(*said, newKey), ",") != "raised" {
|
||||
t.Fatalf("said %v of a licence never read", of(*said, newKey))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
// What every node holds, heard as events (design 39 bus-and-switch §23, §46, B12): the agent module says
|
||||
// `holdings-changed` with its holdings report whole, at its start, on every change and when asked; the manager says
|
||||
// `holdings.asked` at every start, after its conditions are judged, and on its resync, and takes each report it hears as that
|
||||
// node's latest.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// HoldingsChanged is the agent module's event saying what one node holds, which the manager consumes.
|
||||
const HoldingsChanged = "claude-code.holdings-changed"
|
||||
|
||||
// HoldingsAsked is the manager's event asking every node to say what it holds again.
|
||||
const HoldingsAsked = "holdings.asked"
|
||||
|
||||
// Heard is what every node last said it holds, by node.
|
||||
type Heard struct {
|
||||
mu sync.Mutex
|
||||
reports map[string]Holdings
|
||||
}
|
||||
|
||||
// Take keeps one `holdings-changed` as its node's latest report.
|
||||
func (h *Heard) Take(body []byte) (Holdings, error) {
|
||||
var r Holdings
|
||||
if err := json.Unmarshal(body, &r); err != nil {
|
||||
return r, fmt.Errorf("%s is not a holdings report: %w", HoldingsChanged, err)
|
||||
}
|
||||
if r.Node == "" {
|
||||
return r, fmt.Errorf("%s names no node", HoldingsChanged)
|
||||
}
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
if h.reports == nil {
|
||||
h.reports = map[string]Holdings{}
|
||||
}
|
||||
h.reports[r.Node] = r
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// All is every node's latest report, by node's name.
|
||||
func (h *Heard) All() []Holdings {
|
||||
h.mu.Lock()
|
||||
all := make([]Holdings, 0, len(h.reports))
|
||||
for _, r := range h.reports {
|
||||
all = append(all, r)
|
||||
}
|
||||
h.mu.Unlock()
|
||||
sort.Slice(all, func(i, j int) bool { return all[i].Node < all[j].Node })
|
||||
return all
|
||||
}
|
||||
|
||||
// Of is the generation node last reported applying.
|
||||
func (h *Heard) Of(node string) int64 {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
return h.reports[node].Generation
|
||||
}
|
||||
|
||||
// AskHoldings says `holdings.asked`, its time and who asks (§23).
|
||||
func (m *Manager) AskHoldings(by string) error {
|
||||
return m.Emit(HoldingsAsked, map[string]any{"at": m.Now().UTC().Format(time.RFC3339Nano), "by": by})
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
// A licence's key taken by another (design 39 bus-and-switch §10, B6); the key form itself is the shared package's.
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// keyTaken names the licence other than name whose key is name's, or "" when none is.
|
||||
func (m *Manager) keyTaken(ctx context.Context, name string) (string, error) {
|
||||
all, err := m.Store.Licences(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
key := KeyFor(name)
|
||||
for _, l := range all {
|
||||
if l.Name != name && KeyFor(l.Name) == key {
|
||||
return l.Name, nil
|
||||
}
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// design 39 bus-and-switch §10: an adoption whose key another licence has is refused, in the audit and as an event.
|
||||
func TestAnAdoptionWhoseKeyIsTakenIsRefused(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
t.Run("an API key", func(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
_ = mm.store.SaveLicence(ctx, Licence{Name: "Ops-Key", Kind: "api-key"})
|
||||
_, err := mm.m.adoptKey(ctx, "ops-key", "sk-ant-"+strings.Repeat("a", 40), "a test")
|
||||
if err == nil || !strings.Contains(err.Error(), "Ops-Key") {
|
||||
t.Fatalf("adopted a key whose key form is taken: %v", err)
|
||||
}
|
||||
if l, _ := mm.store.Licence(ctx, "ops-key"); l != nil {
|
||||
t.Fatal("the clashing licence was kept")
|
||||
}
|
||||
assertRefused(t, mm)
|
||||
})
|
||||
t.Run("a login", func(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
_ = mm.store.SaveLicence(ctx, Licence{Name: "ACCT-1@example.org", Kind: "api-key"})
|
||||
r := mm.login("laptop", "rt-laptop", true, t0.Add(-time.Minute))
|
||||
adopted, err := mm.m.Consider(ctx, []Holdings{r})
|
||||
if err != nil || len(adopted) != 0 {
|
||||
t.Fatalf("adopted %v (%v), want none", adopted, err)
|
||||
}
|
||||
if len(mm.vendor.exchanged) != 0 {
|
||||
t.Fatalf("a refused login was exchanged: %v", mm.vendor.exchanged)
|
||||
}
|
||||
if o, _ := mm.store.Outcome(ctx, Fingerprint("rt-laptop")); o != Refused {
|
||||
t.Fatalf("outcome %q, want refused", o)
|
||||
}
|
||||
assertRefused(t, mm)
|
||||
})
|
||||
}
|
||||
|
||||
func assertRefused(t *testing.T, mm *miniMesh) {
|
||||
t.Helper()
|
||||
said := false
|
||||
for _, e := range mm.events {
|
||||
said = said || strings.HasPrefix(e, "licence.refused ")
|
||||
}
|
||||
audited := false
|
||||
for _, a := range mm.audit.under("") {
|
||||
audited = audited || a["what"] == "refused"
|
||||
}
|
||||
if !said || !audited {
|
||||
t.Fatalf("refusal said %v, audited %v: %v", said, audited, mm.events)
|
||||
}
|
||||
}
|
||||
+161
-112
@@ -1,10 +1,10 @@
|
||||
// claude-licence-manager (novox/hq ADR 0183, ADR 0206, design 39): one binary, launched by the control
|
||||
// node's runtime and speaking MCP to it over stdio through the Go SDK (ADR 0193, ADR 0198). It serves the
|
||||
// `anthropic-licence-manager` seat's verbs and, beside them, runs long: it watches what every node reports
|
||||
// holding and adopts a login it does not hold, keeps every grant alive under a lease, and reads usage.
|
||||
//
|
||||
// `claude-licence-manager prepare` is the preparation step (ADR 0135): the host runs it once before the
|
||||
// version that needs it, with the module's words and no bus, and it brings the store's schema to shape.
|
||||
// claude-licence-manager (novox/hq ADR 0183, ADR 0206, design 39): the licence manager's manager bundle (design 39
|
||||
// parts §2), loaded by the module's runtime of its own under its own account (`runs-as claude-licence-manager`, design
|
||||
// 39 bus-and-switch C1), never by the machine's tool runner, and speaking MCP to it over stdio through the Go SDK (ADR
|
||||
// 0193, ADR 0198). It runs long (the conditions judged at its start, the usage timer, the switch rule): it watches what
|
||||
// every node reports holding and adopts a login it does not hold, keeps every grant alive under a lease, and reads
|
||||
// usage. It answers the seat's ten verbs and the module's three acting tools, and is the only part that writes the
|
||||
// module's kept data and logs; the reading tools are the tools bundle's (cmd/tools, parts §3, §4).
|
||||
//
|
||||
// stdout is the MCP channel; everything this module says, it says on stderr.
|
||||
package main
|
||||
@@ -16,12 +16,13 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
stdio "git.novox.be/novox/mesh-sdk/go"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// Seat is the role this module holds.
|
||||
@@ -32,14 +33,6 @@ func say(format string, args ...any) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "prepare" {
|
||||
if err := prepare(); err != nil {
|
||||
say("preparing the store failed: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
say("the store's schema is what this version needs")
|
||||
return
|
||||
}
|
||||
go daemon()
|
||||
if err := stdio.Serve("", tools()); err != nil {
|
||||
say("%v", err)
|
||||
@@ -47,17 +40,6 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func prepare() error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer cancel()
|
||||
store, err := PgStoreFromEnv(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer store.Close()
|
||||
return store.Migrate(ctx)
|
||||
}
|
||||
|
||||
// ---- what the binary is handed -----------------------------------------------------------------------
|
||||
|
||||
var (
|
||||
@@ -66,26 +48,18 @@ var (
|
||||
)
|
||||
|
||||
// manager builds the rules' dependencies once, from the module's words (ADR 0192): file paths, never
|
||||
// values. A failure is said and tried again on the next call, so a database that arrives late is not fatal.
|
||||
// values. A failure is said and tried again on the next call, so a key pair that cannot be made yet is not fatal.
|
||||
func manager() (*Manager, error) {
|
||||
buildMu.Lock()
|
||||
defer buildMu.Unlock()
|
||||
if built != nil {
|
||||
return built, nil
|
||||
}
|
||||
dir, keyFile := os.Getenv("MESH_LICENCE_STATE"), os.Getenv("MESH_LICENCE_KEY_FILE")
|
||||
if dir == "" || keyFile == "" {
|
||||
return nil, errors.New("MESH_LICENCE_STATE and MESH_LICENCE_KEY_FILE are not set: the mesh renders them for this module")
|
||||
dir := os.Getenv("MESH_LICENCE_STATE")
|
||||
if dir == "" {
|
||||
return nil, errors.New("MESH_LICENCE_STATE is not set: the mesh renders it for this module")
|
||||
}
|
||||
crypt, err := CryptFromFile(keyFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keys, err := keypair(dir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
store, err := PgStoreFromEnv(context.Background())
|
||||
keys, err := keypair(dir, say)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -93,11 +67,12 @@ func manager() (*Manager, error) {
|
||||
if n := os.Getenv("MESH_NODE"); n != "" {
|
||||
node = n
|
||||
}
|
||||
bindings := stdio.State("bindings")
|
||||
built = &Manager{
|
||||
Store: store,
|
||||
Store: &BusStore{LicencesState: stdio.State("licences"), BindingsState: stdio.State("bindings"),
|
||||
LeasesState: stdio.State("leases"), OfferedState: stdio.State("offered"), UsageLog: stdio.Log("usage"),
|
||||
AuditLog: stdio.Log("audit"), Now: time.Now},
|
||||
Vendor: LiveVendor(),
|
||||
Crypt: crypt,
|
||||
Grants: VaultGrants{Keys: keys},
|
||||
Keys: keys,
|
||||
AskGrant: func(_ context.Context, node, publicKey string) (GrantAnswer, error) {
|
||||
var a GrantAnswer
|
||||
@@ -107,50 +82,103 @@ func manager() (*Manager, error) {
|
||||
}
|
||||
return a, json.Unmarshal(raw, &a)
|
||||
},
|
||||
PutBinding: func(_ context.Context, consumer string, b BindingState) error {
|
||||
_, err := bindings.Put(consumer, b)
|
||||
Reported: heard.Of,
|
||||
Emit: func(event string, body map[string]any) error { return stdio.Emit(event, body) },
|
||||
Tell: func(_ context.Context, headline string, lines []string) error {
|
||||
_, err := stdio.Ask("seat:operator-channel.tell", map[string]any{"subject": Seat, "headline": headline,
|
||||
"lines": lines})
|
||||
return err
|
||||
},
|
||||
DeleteBinding: func(_ context.Context, consumer string) error { return bindings.Delete(consumer) },
|
||||
Emit: func(event string, body map[string]any) error { return stdio.Emit(event, body) },
|
||||
Now: time.Now,
|
||||
Log: say,
|
||||
Holder: fmt.Sprintf("%s:%d", node, os.Getpid()),
|
||||
Settings: SettingsFrom(os.Getenv("MESH_LICENCE_SETTINGS")),
|
||||
Now: time.Now,
|
||||
Log: say,
|
||||
Holder: fmt.Sprintf("%s:%d", node, os.Getpid()),
|
||||
Settings: SettingsFrom(os.Getenv("MESH_LICENCE_SETTINGS")),
|
||||
SettingsFile: os.Getenv("MESH_LICENCE_SETTINGS"),
|
||||
Conditions: stdio.State("conditions"),
|
||||
Say: SayCondition,
|
||||
}
|
||||
return built, nil
|
||||
}
|
||||
|
||||
// keypair is this module's own, made once in its state directory; the private half never leaves it.
|
||||
// Written whole, then linked into place, so a second process reads the first's key and never half of it.
|
||||
func keypair(dir string) (KeyPair, error) {
|
||||
// Written whole, then linked into place, so a second process reads the first's key and never half of it. A pair it
|
||||
// finds and cannot read (another account's, left before the module ran as its own) is made anew in its place and said
|
||||
// in its journal (design 39 bus-and-switch C2): every use passes the public key per ask, so nothing depends on the old.
|
||||
func keypair(dir string, log func(string, ...any)) (KeyPair, error) {
|
||||
file := filepath.Join(dir, "manager-key.json")
|
||||
if _, err := os.Stat(file); errors.Is(err, os.ErrNotExist) {
|
||||
k, err := GenerateKeyPair()
|
||||
if err != nil {
|
||||
if err := writeKeyPair(dir, file, false); err != nil {
|
||||
return KeyPair{}, err
|
||||
}
|
||||
raw, _ := json.Marshal(k)
|
||||
tmp := filepath.Join(dir, fmt.Sprintf(".manager-key.%d.json", os.Getpid()))
|
||||
if err := os.WriteFile(tmp, raw, 0o600); err != nil {
|
||||
return KeyPair{}, err
|
||||
}
|
||||
_ = os.Link(tmp, file) // fails when another made it first, which is right
|
||||
_ = os.Remove(tmp)
|
||||
}
|
||||
k, err := readKeyPair(file)
|
||||
if err == nil {
|
||||
return k, nil
|
||||
}
|
||||
log("the key pair %s cannot be read (%v); a new one is made in its place", file, err)
|
||||
if err := writeKeyPair(dir, file, true); err != nil {
|
||||
return KeyPair{}, err
|
||||
}
|
||||
return readKeyPair(file)
|
||||
}
|
||||
|
||||
func readKeyPair(file string) (KeyPair, error) {
|
||||
raw, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return KeyPair{}, err
|
||||
}
|
||||
var k KeyPair
|
||||
return k, json.Unmarshal(raw, &k)
|
||||
if err := json.Unmarshal(raw, &k); err != nil {
|
||||
return KeyPair{}, err
|
||||
}
|
||||
if k.PublicKey == "" || k.PrivateKey == "" {
|
||||
return KeyPair{}, errors.New("it holds no whole key pair")
|
||||
}
|
||||
return k, nil
|
||||
}
|
||||
|
||||
// writeKeyPair writes a new pair whole beside file, then links it into place, or, replacing, renames it over the one
|
||||
// that cannot be read.
|
||||
func writeKeyPair(dir, file string, replace bool) error {
|
||||
k, err := GenerateKeyPair()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
raw, _ := json.Marshal(k)
|
||||
tmp := filepath.Join(dir, fmt.Sprintf(".manager-key.%d.json", os.Getpid()))
|
||||
if err := os.WriteFile(tmp, raw, 0o600); err != nil {
|
||||
return err
|
||||
}
|
||||
if replace {
|
||||
return os.Rename(tmp, file)
|
||||
}
|
||||
_ = os.Link(tmp, file) // fails when another made it first, which is right
|
||||
_ = os.Remove(tmp)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ---- the long-running half ---------------------------------------------------------------------------
|
||||
|
||||
// heard is what every node last said it holds (ADR 0206, design 39 bus-and-switch §23), by node.
|
||||
var heard = &Heard{}
|
||||
|
||||
// judgedAtStart judges every condition anew at the start, before the manager asks what the nodes hold and starts its
|
||||
// timers: raised again each it holds, cleared each key of `conditions` it no longer holds, carry-over-failed among
|
||||
// them when present (design 39 parts §11, bus-and-switch §9). It waits for nothing to be carried over.
|
||||
func judgedAtStart() {
|
||||
<-stdio.Served()
|
||||
m, err := manager()
|
||||
if err != nil {
|
||||
say("the conditions cannot be judged at the start: %v", err)
|
||||
return
|
||||
}
|
||||
if err := m.SayConditions(context.Background(), true); err != nil {
|
||||
say("saying the conditions at the start: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func daemon() {
|
||||
var mu sync.Mutex
|
||||
reports := map[string]Holdings{}
|
||||
judgedAtStart()
|
||||
var passing sync.Mutex
|
||||
pass := func() {
|
||||
passing.Lock() // one pass at a time: each account is leased, and a pass is cheap
|
||||
@@ -160,14 +188,7 @@ func daemon() {
|
||||
say("not ready: %v", err)
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
all := make([]Holdings, 0, len(reports))
|
||||
for _, r := range reports {
|
||||
all = append(all, r)
|
||||
}
|
||||
mu.Unlock()
|
||||
sort.Slice(all, func(i, j int) bool { return all[i].Node < all[j].Node })
|
||||
adopted, err := m.Consider(context.Background(), all)
|
||||
adopted, err := m.Consider(context.Background(), heard.All())
|
||||
if err != nil {
|
||||
say("considering the reports failed: %v", err)
|
||||
}
|
||||
@@ -176,48 +197,45 @@ func daemon() {
|
||||
}
|
||||
}
|
||||
|
||||
// What every node holds (ADR 0206): the whole current set, then each change. Asked again until it
|
||||
// answers — the channel to the runtime opens as the bundle starts, and the agent module's state may
|
||||
// arrive after this one.
|
||||
// What every node holds (ADR 0206, design 39 bus-and-switch §23): each `holdings-changed` heard is that node's
|
||||
// latest report and starts a pass; once heard, every node is asked to say it again (B12: at every start, after the
|
||||
// conditions are judged). Asked again until it answers — the channel to the runtime opens as the bundle starts.
|
||||
go func() {
|
||||
waits := []time.Duration{2 * time.Second, 5 * time.Second, 10 * time.Second, 30 * time.Second}
|
||||
for attempt := 0; ; attempt++ {
|
||||
err := stdio.State("claude-code.holdings").Watch("", func(c stdio.StateChange) error {
|
||||
mu.Lock()
|
||||
if c.Op == "put" {
|
||||
var h Holdings
|
||||
if json.Unmarshal(c.Value, &h) == nil {
|
||||
reports[c.Key] = h
|
||||
}
|
||||
} else {
|
||||
delete(reports, c.Key)
|
||||
}
|
||||
mu.Unlock()
|
||||
// During the current values the pass waits for the whole set: newest login first needs all.
|
||||
if !c.Current {
|
||||
go pass()
|
||||
err := stdio.Subscribe(HoldingsChanged, func(e stdio.Envelope) error {
|
||||
if _, err := heard.Take(e.Body); err != nil {
|
||||
say("%v", err)
|
||||
return nil
|
||||
}
|
||||
go pass()
|
||||
return nil
|
||||
})
|
||||
if err == nil {
|
||||
mu.Lock()
|
||||
n := len(reports)
|
||||
mu.Unlock()
|
||||
say("watching what %d node(s) hold", n)
|
||||
pass()
|
||||
say("hearing what the nodes hold")
|
||||
if m, err := manager(); err != nil {
|
||||
say("not ready to ask what the nodes hold: %v", err)
|
||||
} else if err := m.AskHoldings(Itself); err != nil {
|
||||
say("asking what the nodes hold could not be said: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
pause := time.Minute
|
||||
if attempt < len(waits) {
|
||||
pause = waits[attempt]
|
||||
}
|
||||
say("what the nodes hold cannot be watched yet (%v); asking again in %s", err, pause)
|
||||
say("what the nodes hold cannot be heard yet (%v); asking again in %s", err, pause)
|
||||
time.Sleep(pause)
|
||||
}
|
||||
}()
|
||||
|
||||
refresh := time.NewTicker(time.Minute)
|
||||
usage := time.NewTicker(5 * time.Minute)
|
||||
// Every usage_minutes, read again at every reading (§24, §33): the manager's one timer of its own readings.
|
||||
every := Defaults.UsageEvery
|
||||
if m, err := manager(); err == nil {
|
||||
every = m.settings().UsageEvery
|
||||
}
|
||||
usage := time.NewTimer(every)
|
||||
for {
|
||||
select {
|
||||
case <-refresh.C:
|
||||
@@ -237,7 +255,9 @@ func daemon() {
|
||||
if err := m.ReadUsage(context.Background()); err != nil {
|
||||
say("reading usage failed: %v", err)
|
||||
}
|
||||
every = m.settings().UsageEvery
|
||||
}
|
||||
usage.Reset(every)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -266,6 +286,28 @@ func verb(name, description string, input map[string]any, run func(ctx context.C
|
||||
}}
|
||||
}
|
||||
|
||||
// sinceOf is the argument since, RFC 3339; absent is the zero time.
|
||||
func sinceOf(a map[string]any) (time.Time, error) {
|
||||
v, _ := a["since"].(string)
|
||||
if strings.TrimSpace(v) == "" {
|
||||
return time.Time{}, nil
|
||||
}
|
||||
t, err := time.Parse(time.RFC3339Nano, strings.TrimSpace(v))
|
||||
if err != nil {
|
||||
return time.Time{}, fmt.Errorf("since is not a time in RFC 3339: %w", err)
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// limitOf is the argument limit: def when absent or not positive, at most most.
|
||||
func limitOf(a map[string]any, def, most int) int {
|
||||
limit := def
|
||||
if v, ok := a["limit"].(float64); ok && v > 0 {
|
||||
limit = int(v)
|
||||
}
|
||||
return min(limit, most)
|
||||
}
|
||||
|
||||
func str(description string) map[string]any {
|
||||
return map[string]any{"type": "string", "description": description}
|
||||
}
|
||||
@@ -278,11 +320,15 @@ func two(a map[string]any, k1, k2 string) (string, string, error) {
|
||||
|
||||
func tools() []stdio.Tool {
|
||||
consumer := str("the node's name")
|
||||
return []stdio.Tool{
|
||||
verb("licences", "Every licence the manager holds — account, kind, when its token and its refresh token expire, failures in a row, which consumers are bound to it. Never a token.",
|
||||
return append([]stdio.Tool{
|
||||
verb("licences", "Every licence the manager holds — account, kind, when its token and its refresh token expire, failures in a row, which consumers are bound to it, its latest usage with its reset times, its room or why it is not known, whether it is where a full licence's consumers move next, when and from where it was adopted, and its open conditions. Never a token.",
|
||||
nil, func(ctx context.Context, m *Manager, _ map[string]any) (any, error) { return m.Licences(ctx) }),
|
||||
verb("bindings", "Which licence each consumer (a node's agent, by the node's name) is bound to, and the generation it was last given.",
|
||||
nil, func(ctx context.Context, m *Manager, _ map[string]any) (any, error) { return m.Store.Bindings(ctx) }),
|
||||
verb("bindings", "Which licence each consumer (a node's agent, by the node's name) is bound to, and the generation it was last given; with consumer, that consumer's alone.",
|
||||
map[string]any{"consumer": str("a consumer; absent for every one")},
|
||||
func(ctx context.Context, m *Manager, a map[string]any) (any, error) {
|
||||
c, _ := a["consumer"].(string)
|
||||
return m.BindingsOf(ctx, strings.TrimSpace(c))
|
||||
}),
|
||||
verb("bind", "Bind a consumer — a node's agent, by the node's name — to a licence. Its node fetches the licence's token at once.",
|
||||
map[string]any{"consumer": consumer, "licence": str("a licence, as `licences` names it")},
|
||||
func(ctx context.Context, m *Manager, a map[string]any) (any, error) {
|
||||
@@ -318,15 +364,18 @@ func tools() []stdio.Tool {
|
||||
}
|
||||
return m.RotateDue(ctx)
|
||||
}),
|
||||
verb("usage", "Usage readings, the latest first, for every licence or one.",
|
||||
map[string]any{"licence": str("a licence; absent for all"), "limit": map[string]any{"type": "number", "description": "how many readings (default 20)"}},
|
||||
verb("usage", "Usage readings, the latest first, for every licence or one: the flattened reading with its three reset times, none before since, at most limit (default 20, at most 2000), and with raw the vendor's answer whole.",
|
||||
map[string]any{"licence": str("a licence; absent for all"), "since": str("a time, RFC 3339: no reading before it"),
|
||||
"limit": map[string]any{"type": "number", "description": "how many readings (default 20, at most 2000)"},
|
||||
"raw": map[string]any{"type": "boolean", "description": "also the vendor's answer whole"}},
|
||||
func(ctx context.Context, m *Manager, a map[string]any) (any, error) {
|
||||
l, _ := a["licence"].(string)
|
||||
limit := 20
|
||||
if v, ok := a["limit"].(float64); ok && v > 0 {
|
||||
limit = int(v)
|
||||
since, err := sinceOf(a)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m.Store.Usage(ctx, l, limit)
|
||||
raw, _ := a["raw"].(bool)
|
||||
return m.Store.Usage(ctx, l, since, limitOf(a, 20, 2000), raw)
|
||||
}),
|
||||
verb("adopt", "Adopt an API key — never as an argument: from a file on the manager's node (`file`), or sealed to the manager's `public-key` by a node's claude-code (`sealed`; its `claude_code_add_api_key` does this). Subscriptions are adopted from the nodes' logins by themselves.",
|
||||
map[string]any{"name": str("the licence's name"), "file": str("a file on the manager's node holding the key"),
|
||||
@@ -356,14 +405,14 @@ func tools() []stdio.Tool {
|
||||
nil, func(_ context.Context, m *Manager, _ map[string]any) (any, error) {
|
||||
return map[string]any{"public_key": m.Keys.PublicKey}, nil
|
||||
}),
|
||||
verb("current", "For a consumer's agent module (ADR 0206): its token, sealed to the public key it sends, with the licence, kind and generation. Null when it is bound to nothing.",
|
||||
map[string]any{"consumer": consumer, "public_key": str("the consumer's public key, PEM")},
|
||||
verb("current", "For a consumer's agent module: which licence it is bound to, the licence's key, kind and generation, its health (ok, failing or expiring) and when its access token expires; never the token, which the vault hands. Null when it is bound to nothing.",
|
||||
map[string]any{"consumer": consumer},
|
||||
func(ctx context.Context, m *Manager, a map[string]any) (any, error) {
|
||||
c, k, err := two(a, "consumer", "public_key")
|
||||
c, err := text(a, "consumer")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m.Current(ctx, c, k)
|
||||
return m.Current(ctx, c)
|
||||
}),
|
||||
}
|
||||
}, moduleTools()...)
|
||||
}
|
||||
+398
-196
@@ -23,7 +23,12 @@ import (
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
oc "git.novox.be/novox/mesh-contracts/operator-and-conversation/go/seats/operatorchannel"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// Fingerprint names a token without being one: the agent module's own fingerprint, so the two compare.
|
||||
@@ -53,17 +58,6 @@ type Holdings struct {
|
||||
Generation int64 `json:"generation"`
|
||||
}
|
||||
|
||||
// BindingState is what `bindings` holds for one consumer: no secret, only what it should hold and which
|
||||
// generation.
|
||||
type BindingState struct {
|
||||
Licence string `json:"licence"`
|
||||
Kind string `json:"kind"`
|
||||
Generation int64 `json:"generation"`
|
||||
// Why and By are what moved it and who asked (novox/hq issue 625), so the node can say so when it applies it.
|
||||
Why string `json:"why,omitempty"`
|
||||
By string `json:"by,omitempty"`
|
||||
}
|
||||
|
||||
// Cause is why a consumer's binding changed and who or what asked for it (novox/hq issue 625): Why is the verb
|
||||
// called or the rule that fired, By the caller the runtime proved (stdio.CallerOf) or this module itself.
|
||||
type Cause struct {
|
||||
@@ -90,63 +84,52 @@ func CauseOf(ctx context.Context) Cause {
|
||||
return Cause{Why: "a rule of this module's own", By: Itself}
|
||||
}
|
||||
|
||||
// record says one change of what a consumer holds, the same fields in three places (novox/hq issue 625): a line
|
||||
// in the journal, a row in the audit table, and the event `licence.<what>` on the bus. Never a token.
|
||||
func (m *Manager) record(ctx context.Context, what, line string, detail map[string]any) {
|
||||
_ = m.Store.Audit(ctx, what, detail)
|
||||
// record says one change of what a consumer holds, the same fields in two places (novox/hq issue 625): a line in
|
||||
// the journal and an entry of the audit. The bus hears it as binding.changed (§21); `licence.bound`, `released` and
|
||||
// `rotated` are said no more (B16). Never a token.
|
||||
func (m *Manager) record(ctx context.Context, what, line string, detail map[string]any) error {
|
||||
if err := m.Store.Audit(ctx, what, detail); err != nil {
|
||||
m.Log("%s, but %v", line, err)
|
||||
return err
|
||||
}
|
||||
m.Log("%s", line)
|
||||
_ = m.Emit("licence."+what, detail)
|
||||
return nil
|
||||
}
|
||||
|
||||
// moved records a consumer bound or switched: from which licence to which, at which generation, why and by whom.
|
||||
func (m *Manager) moved(ctx context.Context, consumer, from, licence string, generation int64, c Cause) {
|
||||
what, line := "bound", fmt.Sprintf("bound %s to %s", consumer, licence)
|
||||
if from != "" {
|
||||
what, line = "switched", fmt.Sprintf("switched %s from %s to %s", consumer, from, licence)
|
||||
// BindingChanged is the event of a consumer's binding written or deleted, said as `binding.changed.<consumer>`: the consumer is
|
||||
// its further part, the one form of G's seat events (novox/hq design 47 G A-134), so a reader adopts it.
|
||||
const BindingChanged = "binding.changed"
|
||||
|
||||
// changed says that one consumer's key of `bindings` was written or deleted (§21, B7): its licence (empty on a
|
||||
// release) and that licence's key, kind, generation, the licence before, why, by and when. Never a token.
|
||||
func (m *Manager) changed(b Binding, from string) {
|
||||
key := ""
|
||||
if b.Licence != "" {
|
||||
key = KeyFor(b.Licence)
|
||||
}
|
||||
m.record(ctx, what, fmt.Sprintf("%s (generation %d): %s; asked by %s", line, generation, c.Why, c.By),
|
||||
map[string]any{"consumer": consumer, "licence": licence, "from": from, "generation": generation,
|
||||
"why": c.Why, "by": c.By})
|
||||
_ = m.Emit(BindingChanged+"."+b.Consumer, map[string]any{"consumer": b.Consumer, "licence": b.Licence, "key": key,
|
||||
"kind": b.Kind, "generation": b.Generation, "from": from, "why": b.Why, "by": b.By,
|
||||
"at": m.Now().UTC().Format(time.RFC3339Nano)})
|
||||
}
|
||||
|
||||
// Settings are the manager's own, declared with defaults (design 39 §8).
|
||||
type Settings struct {
|
||||
Cadence time.Duration // rotate a grant once older than this
|
||||
Floor time.Duration // refresh in any case with less than this left
|
||||
FailuresToNotify int
|
||||
Cooldown time.Duration // at most one notification per licence in this window
|
||||
RefreshWarn time.Duration // warn this long before a refresh token itself expires
|
||||
// switchedFields are a switch's fields, the same in the event licence.switched and the audit entry switched (§27).
|
||||
func switchedFields(from, to string, consumers []string, c Cause) map[string]any {
|
||||
return map[string]any{"from": from, "to": to, "consumers": consumers, "by": c.By, "why": c.Why}
|
||||
}
|
||||
|
||||
// Defaults are the settings a fresh mesh runs with.
|
||||
var Defaults = Settings{Cadence: 4 * time.Hour, Floor: time.Hour, FailuresToNotify: 3, Cooldown: 24 * time.Hour, RefreshWarn: 72 * time.Hour}
|
||||
|
||||
// SettingsFrom reads the settings file, keeping a default for anything absent or not positive.
|
||||
func SettingsFrom(path string) Settings {
|
||||
s := Defaults
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return s
|
||||
// moved records a consumer bound or switched, after its key is written: in the journal and the audit (a switch with
|
||||
// §27's fields), and as binding.changed.
|
||||
func (m *Manager) moved(ctx context.Context, b Binding, from string, c Cause) error {
|
||||
what, line := "bound", fmt.Sprintf("bound %s to %s", b.Consumer, b.Licence)
|
||||
detail := map[string]any{"consumer": b.Consumer, "licence": b.Licence, "from": from, "generation": b.Generation,
|
||||
"why": c.Why, "by": c.By}
|
||||
if from != "" && from != b.Licence {
|
||||
what, line = "switched", fmt.Sprintf("switched %s from %s to %s", b.Consumer, from, b.Licence)
|
||||
detail = switchedFields(from, b.Licence, []string{b.Consumer}, c)
|
||||
}
|
||||
var f map[string]float64
|
||||
if json.Unmarshal(raw, &f) != nil {
|
||||
return s
|
||||
}
|
||||
set := func(k string, unit time.Duration, into *time.Duration) {
|
||||
if v := f[k]; v > 0 {
|
||||
*into = time.Duration(v * float64(unit))
|
||||
}
|
||||
}
|
||||
set("cadence_minutes", time.Minute, &s.Cadence)
|
||||
set("floor_minutes", time.Minute, &s.Floor)
|
||||
set("cooldown_hours", time.Hour, &s.Cooldown)
|
||||
if v := f["refresh_warn_days"]; v > 0 {
|
||||
s.RefreshWarn = time.Duration(v * float64(24*time.Hour))
|
||||
}
|
||||
if v := f["failures_to_notify"]; v > 0 {
|
||||
s.FailuresToNotify = int(v)
|
||||
}
|
||||
return s
|
||||
err := m.record(ctx, what, fmt.Sprintf("%s (generation %d): %s; asked by %s", line, b.Generation, c.Why, c.By), detail)
|
||||
m.changed(b, from)
|
||||
return err
|
||||
}
|
||||
|
||||
// GrantAnswer is what a node's `claude_code_grant` answers: a login sealed to the key given, or nothing.
|
||||
@@ -160,20 +143,31 @@ type GrantAnswer struct {
|
||||
type Manager struct {
|
||||
Store Store
|
||||
Vendor Vendor
|
||||
Crypt *Crypt
|
||||
// Grants is the vault, which holds every licence's grant and access token (§15, §16).
|
||||
Grants Grants
|
||||
Keys KeyPair
|
||||
// AskGrant asks a node's agent module for the grant a login left there, sealed to publicKey. An error
|
||||
// is the node not answering; a nil Sealed is no login waiting.
|
||||
AskGrant func(ctx context.Context, node, publicKey string) (GrantAnswer, error)
|
||||
// PutBinding and DeleteBinding are this module's `bindings` state.
|
||||
PutBinding func(ctx context.Context, consumer string, b BindingState) error
|
||||
DeleteBinding func(ctx context.Context, consumer string) error
|
||||
Emit func(event string, body map[string]any) error
|
||||
Now func() time.Time
|
||||
Log func(format string, args ...any)
|
||||
// Reported is the binding generation a node last reported applying (B11), 0 when none is known.
|
||||
Reported func(node string) int64
|
||||
Emit func(event string, body map[string]any) error
|
||||
// Tell tells the operator one notice that needs no answer, through the seat operator-channel's tell (§28).
|
||||
Tell func(ctx context.Context, headline string, lines []string) error
|
||||
Now func() time.Time
|
||||
Log func(format string, args ...any)
|
||||
// Holder names this process in a lease, so a second run is told apart.
|
||||
Holder string
|
||||
Settings Settings
|
||||
Holder string
|
||||
// Settings are those in force, read again from SettingsFile at every reading (settings.go); an empty SettingsFile
|
||||
// keeps them.
|
||||
Settings Settings
|
||||
SettingsFile string
|
||||
settingsMu sync.RWMutex
|
||||
// Conditions is the state `conditions` (design 39 bus-and-switch §9), and Say says one condition on the operator
|
||||
// channel; either nil says none.
|
||||
Conditions State
|
||||
Say func(c oc.Condition) error
|
||||
conditionsMu sync.Mutex
|
||||
}
|
||||
|
||||
const leaseFor = 2 * time.Minute
|
||||
@@ -239,17 +233,9 @@ func (m *Manager) CandidatesIn(ctx context.Context, reports []Holdings) (map[str
|
||||
// newest first; the first that refreshes is adopted and the rest are settled as skipped without being
|
||||
// exchanged. Answers what was adopted.
|
||||
func (m *Manager) Consider(ctx context.Context, reports []Holdings) ([]string, error) {
|
||||
// **Never a generation a node has already passed** (novox/hq issue 243). A store rebuilt after a loss
|
||||
// counts from one again, while every node still holds the number it last applied. The nodes no longer
|
||||
// wait for a higher number, but a binding numbered exactly as the one a node applied would still be
|
||||
// skipped. So the count is moved past every number reported, before anything here binds.
|
||||
var highest int64
|
||||
for _, r := range reports {
|
||||
highest = max(highest, r.Generation)
|
||||
}
|
||||
if err := m.Store.GenerationsAbove(ctx, highest); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// **Never a generation a node has already passed** (novox/hq issue 243, B11): each consumer's count is its own,
|
||||
// and moves past the number its node reported applying.
|
||||
floor := m.floorIn(reports)
|
||||
byAccount, err := m.CandidatesIn(ctx, reports)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -262,19 +248,19 @@ func (m *Manager) Consider(ctx context.Context, reports []Holdings) ([]string, e
|
||||
// A node reporting an account the manager already holds, and bound to nothing, is bound to it
|
||||
// (ADR 0206 §7) — whenever its report arrives, not only when the licence is adopted: a node whose own
|
||||
// login is older than the one adopted is never a candidate, and would otherwise never be bound.
|
||||
if err := m.bindReporters(ctx, reports); err != nil {
|
||||
if err := m.bindReporters(ctx, reports, floor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var adopted []string
|
||||
for _, account := range accounts {
|
||||
list := byAccount[account]
|
||||
key := "account:" + account
|
||||
key := "account." + account
|
||||
ok, err := m.Store.Lease(ctx, key, m.Holder, leaseFor)
|
||||
if err != nil || !ok {
|
||||
continue
|
||||
}
|
||||
for i, c := range list {
|
||||
name, err := m.adoptOne(ctx, c, reports)
|
||||
name, err := m.adoptOne(ctx, c, reports, floor)
|
||||
if err != nil {
|
||||
m.Log("adopting %s's login failed: %v", c.Node, err)
|
||||
continue
|
||||
@@ -293,7 +279,7 @@ func (m *Manager) Consider(ctx context.Context, reports []Holdings) ([]string, e
|
||||
}
|
||||
|
||||
// bindReporters binds each reporting node that is bound to nothing to the licence its account already has.
|
||||
func (m *Manager) bindReporters(ctx context.Context, reports []Holdings) error {
|
||||
func (m *Manager) bindReporters(ctx context.Context, reports []Holdings, floor func(string) int64) error {
|
||||
for _, rep := range reports {
|
||||
if rep.Identity == nil || rep.Identity.AccountUUID == "" {
|
||||
continue
|
||||
@@ -311,21 +297,19 @@ func (m *Manager) bindReporters(ctx context.Context, reports []Holdings) error {
|
||||
if l == nil {
|
||||
continue
|
||||
}
|
||||
b, err := m.Store.Bind(ctx, rep.Node, l.Name)
|
||||
c := Cause{Why: rep.Node + " reported the account of " + l.Name + " and was bound to nothing", By: Itself}
|
||||
b, err := m.Store.Bind(ctx, rep.Node, l.Name, Move{Kind: l.Kind, Why: c.Why, By: c.By, Floor: floor})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c := Cause{Why: rep.Node + " reported the account of " + l.Name + " and was bound to nothing", By: Itself}
|
||||
m.moved(ctx, rep.Node, "", l.Name, b.Generation, c)
|
||||
if err := m.PutBinding(ctx, rep.Node, BindingState{Licence: l.Name, Kind: l.Kind, Generation: b.Generation,
|
||||
Why: c.Why, By: c.By}); err != nil {
|
||||
if err := m.moved(ctx, b, "", c); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) adoptOne(ctx context.Context, c Candidate, reports []Holdings) (string, error) {
|
||||
func (m *Manager) adoptOne(ctx context.Context, c Candidate, reports []Holdings, floor func(string) int64) (string, error) {
|
||||
answer, err := m.AskGrant(ctx, c.Node, m.Keys.PublicKey)
|
||||
if err != nil {
|
||||
// Not answering is not an answer: asked again on the next pass.
|
||||
@@ -348,12 +332,34 @@ func (m *Manager) adoptOne(ctx context.Context, c Candidate, reports []Holdings)
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// A new licence whose key another licence already has is refused before its grant is exchanged
|
||||
// (design 39 bus-and-switch §10).
|
||||
if held, err := m.Store.LicenceForAccount(ctx, c.Identity.AccountUUID); err != nil {
|
||||
return "", err
|
||||
} else if held == nil {
|
||||
taken, err := m.keyTaken(ctx, NameFor(c.Identity))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if taken != "" {
|
||||
why := "its key " + KeyFor(NameFor(c.Identity)) + " is the key of the licence " + taken
|
||||
_ = m.Store.RecordOutcome(ctx, c.Fingerprint, c.Node, c.Identity.AccountUUID, Refused, why)
|
||||
if err := m.Store.Audit(ctx, "refused", map[string]any{"node": c.Node, "licence": NameFor(c.Identity), "reason": why}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
_ = m.Emit("licence.refused", map[string]any{"node": c.Node, "account": NameFor(c.Identity), "reason": why})
|
||||
return "", nil
|
||||
}
|
||||
}
|
||||
|
||||
// Adopting is refreshing (ADR 0206 §4): the exchange is the check, and from here this module is the
|
||||
// only holder of a live refresh token for the account.
|
||||
r := m.Vendor.Refresh(ctx, offered)
|
||||
if !r.OK {
|
||||
_ = m.Store.RecordOutcome(ctx, c.Fingerprint, c.Node, c.Identity.AccountUUID, Dead, fmt.Sprintf("%d %s", r.Status, r.Reason))
|
||||
_ = m.Store.Audit(ctx, "refused", map[string]any{"node": c.Node, "account": c.Identity.AccountUUID, "status": r.Status, "reason": r.Reason})
|
||||
if err := m.Store.Audit(ctx, "refused", map[string]any{"node": c.Node, "account": c.Identity.AccountUUID, "status": r.Status, "reason": r.Reason}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
_ = m.Emit("licence.refused", map[string]any{"node": c.Node, "account": NameFor(c.Identity),
|
||||
"reason": fmt.Sprintf("the login's refresh token did not refresh (%d)", r.Status)})
|
||||
m.Log("%s's login for %s did not refresh: %d %s", c.Node, NameFor(c.Identity), r.Status, r.Reason)
|
||||
@@ -363,7 +369,9 @@ func (m *Manager) adoptOne(ctx context.Context, c Candidate, reports []Holdings)
|
||||
if r.Account != "" && r.Account != c.Identity.AccountUUID {
|
||||
_ = m.Store.RecordOutcome(ctx, c.Fingerprint, c.Node, c.Identity.AccountUUID, Refused,
|
||||
"the node says "+c.Identity.AccountUUID+", the vendor says "+r.Account)
|
||||
_ = m.Store.Audit(ctx, "refused", map[string]any{"node": c.Node, "reported": c.Identity.AccountUUID, "vendor": r.Account})
|
||||
if err := m.Store.Audit(ctx, "refused", map[string]any{"node": c.Node, "reported": c.Identity.AccountUUID, "vendor": r.Account}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
_ = m.Emit("licence.refused", map[string]any{"node": c.Node, "account": NameFor(c.Identity),
|
||||
"reason": "the account the node reported is not the one the vendor answered for"})
|
||||
return "", nil
|
||||
@@ -375,9 +383,10 @@ func (m *Manager) adoptOne(ctx context.Context, c Candidate, reports []Holdings)
|
||||
}
|
||||
now := m.Now().UnixMilli()
|
||||
l := Licence{Name: NameFor(c.Identity), Kind: "subscription", AccountUUID: c.Identity.AccountUUID,
|
||||
Email: c.Identity.EmailAddress, OrganizationUUID: c.Identity.OrganizationUUID}
|
||||
Email: c.Identity.EmailAddress, OrganizationUUID: c.Identity.OrganizationUUID, AdoptedFrom: c.Node}
|
||||
if held != nil {
|
||||
l.Name, l.NotifiedAt = held.Name, held.NotifiedAt
|
||||
// A newer login keeps what the key holds beside its grant: its latest reading (§2, §24).
|
||||
l.Name, l.NotifiedAt, l.Generation, l.Usage = held.Name, held.NotifiedAt, held.Generation, held.Usage
|
||||
if l.Email == "" {
|
||||
l.Email = held.Email
|
||||
}
|
||||
@@ -385,7 +394,9 @@ func (m *Manager) adoptOne(ctx context.Context, c Candidate, reports []Holdings)
|
||||
l.OrganizationUUID = held.OrganizationUUID
|
||||
}
|
||||
}
|
||||
m.keep(&l, r.Grant)
|
||||
if err := m.keep(ctx, &l, r.Grant); err != nil {
|
||||
return "", err
|
||||
}
|
||||
l.AdoptedAt = max(now, c.ChangedAt)
|
||||
if err := m.Store.SaveLicence(ctx, l); err != nil {
|
||||
return "", err
|
||||
@@ -395,18 +406,25 @@ func (m *Manager) adoptOne(ctx context.Context, c Candidate, reports []Holdings)
|
||||
why = "a newer login"
|
||||
}
|
||||
_ = m.Store.RecordOutcome(ctx, c.Fingerprint, c.Node, c.Identity.AccountUUID, Adopted, why)
|
||||
_ = m.Store.Audit(ctx, "adopted", map[string]any{"licence": l.Name, "node": c.Node, "account": c.Identity.AccountUUID,
|
||||
"vendorNamedAccount": r.Account != ""})
|
||||
if err := m.Store.Audit(ctx, "adopted", map[string]any{"licence": l.Name, "node": c.Node, "account": c.Identity.AccountUUID,
|
||||
"vendorNamedAccount": r.Account != ""}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// A login moves its node (ADR 0209): the node this login came from is bound to its licence —
|
||||
// switched, if it was bound to another. Then every node reporting this account and bound to nothing.
|
||||
// Who asked is the person who logged in there; the mesh knows them only as that login (novox/hq issue 625).
|
||||
cause := Cause{Why: "a login to " + l.Name + " on " + c.Node + " (ADR 0209)", By: Itself}
|
||||
ctx = WithCause(ctx, cause)
|
||||
// The consumers already bound to it take the new grant first, so each consumer this adoption moves is said once
|
||||
// (§21).
|
||||
if err := m.publishAdvance(ctx, l, floor); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if b, err := m.Store.Binding(ctx, c.Node); err != nil {
|
||||
return "", err
|
||||
} else if b == nil || b.Licence != l.Name {
|
||||
moved, err := m.Store.Bind(ctx, c.Node, l.Name)
|
||||
moved, err := m.Store.Bind(ctx, c.Node, l.Name, Move{Kind: l.Kind, Why: cause.Why, By: cause.By, Floor: floor})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -414,7 +432,9 @@ func (m *Manager) adoptOne(ctx context.Context, c Candidate, reports []Holdings)
|
||||
if b != nil {
|
||||
from = b.Licence
|
||||
}
|
||||
m.moved(ctx, c.Node, from, l.Name, moved.Generation, cause)
|
||||
if err := m.moved(ctx, moved, from, cause); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
for _, rep := range reports {
|
||||
if rep.Identity == nil || rep.Identity.AccountUUID != c.Identity.AccountUUID {
|
||||
@@ -423,23 +443,25 @@ func (m *Manager) adoptOne(ctx context.Context, c Candidate, reports []Holdings)
|
||||
if b, err := m.Store.Binding(ctx, rep.Node); err != nil || b != nil {
|
||||
continue
|
||||
}
|
||||
if b, err := m.Store.Bind(ctx, rep.Node, l.Name); err == nil {
|
||||
m.moved(ctx, rep.Node, "", l.Name, b.Generation,
|
||||
Cause{Why: rep.Node + " reports the account of " + l.Name + ", logged in on " + c.Node + ", and was bound to nothing", By: Itself})
|
||||
bc := Cause{Why: rep.Node + " reports the account of " + l.Name + ", logged in on " + c.Node + ", and was bound to nothing", By: Itself}
|
||||
if b, err := m.Store.Bind(ctx, rep.Node, l.Name, Move{Kind: l.Kind, Why: bc.Why, By: bc.By, Floor: floor}); err == nil {
|
||||
if err := m.moved(ctx, b, "", bc); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := m.publishAdvance(ctx, l); err != nil {
|
||||
return "", err
|
||||
}
|
||||
_ = m.Emit("licence.adopted", map[string]any{"licence": l.Name, "from": c.Node, "replaced": held != nil})
|
||||
m.Log("adopted %s from %s (%s)", l.Name, c.Node, why)
|
||||
return l.Name, nil
|
||||
}
|
||||
|
||||
// keep stores a grant on its licence: encrypted, fingerprinted, its expiries, fresh.
|
||||
func (m *Manager) keep(l *Licence, g FullGrant) {
|
||||
raw, _ := json.Marshal(g)
|
||||
l.Sealed = m.Crypt.Seal(string(raw))
|
||||
// keep hands a grant to the vault at the licence's next generation, the grant first and then its access (design 39
|
||||
// bus-and-switch §15), and keeps on the licence its fingerprint and expiries alone: no token once it returns.
|
||||
func (m *Manager) keep(ctx context.Context, l *Licence, g FullGrant) error {
|
||||
grant, _ := json.Marshal(g)
|
||||
if err := m.handOver(ctx, l, string(grant), accessOf(g)); err != nil {
|
||||
return err
|
||||
}
|
||||
l.RefreshFingerprint = Fingerprint(g.RefreshToken)
|
||||
l.AccessExpiresAt = g.ExpiresAt
|
||||
if g.RefreshTokenExpiresAt != nil {
|
||||
@@ -447,30 +469,73 @@ func (m *Manager) keep(l *Licence, g FullGrant) {
|
||||
}
|
||||
l.Failures = 0
|
||||
l.RotatedAt = m.Now().UnixMilli()
|
||||
return nil
|
||||
}
|
||||
|
||||
// publishAdvance gives every consumer of a licence a new generation, and tells each in the state with the
|
||||
// cause ctx carries, so the node says why it fetched again (novox/hq issue 625).
|
||||
func (m *Manager) publishAdvance(ctx context.Context, l Licence) error {
|
||||
bindings, err := m.Store.Advance(ctx, l.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c := CauseOf(ctx)
|
||||
for _, b := range bindings {
|
||||
if err := m.PutBinding(ctx, b.Consumer, BindingState{Licence: b.Licence, Kind: l.Kind, Generation: b.Generation,
|
||||
Why: c.Why, By: c.By}); err != nil {
|
||||
return err
|
||||
// accessOf is a subscription's `access` secret: its access token and expiry, never its refresh token (§15).
|
||||
func accessOf(g FullGrant) string {
|
||||
access, _ := json.Marshal(map[string]any{"accessToken": g.AccessToken, "expiresAt": g.ExpiresAt,
|
||||
"refreshTokenExpiresAt": g.RefreshTokenExpiresAt, "scopes": g.Scopes,
|
||||
"subscriptionType": g.SubscriptionType, "rateLimitTier": g.RateLimitTier})
|
||||
return string(access)
|
||||
}
|
||||
|
||||
// handOver puts a licence's two secrets in the vault at its next generation, the grant first (§15).
|
||||
func (m *Manager) handOver(ctx context.Context, l *Licence, grant, access string) error {
|
||||
generation := l.Generation + 1
|
||||
for _, part := range []struct{ name, value string }{{PartGrant, grant}, {PartAccess, access}} {
|
||||
if err := m.Grants.Put(ctx, KeyFor(l.Name), part.name, generation, part.value); err != nil {
|
||||
return fmt.Errorf("the vault did not take the %s of %s: %w", part.name, l.Name, err)
|
||||
}
|
||||
}
|
||||
l.Generation = generation
|
||||
return nil
|
||||
}
|
||||
|
||||
// grantOf reads a subscription's grant from the vault for one act (§16); the caller drops it when the act ends.
|
||||
func (m *Manager) grantOf(ctx context.Context, l Licence) (FullGrant, error) {
|
||||
var g FullGrant
|
||||
plain, err := m.Grants.Get(ctx, KeyFor(l.Name), PartGrant)
|
||||
if err != nil {
|
||||
return g, fmt.Errorf("the grant of %s cannot be read from the vault: %w", l.Name, err)
|
||||
}
|
||||
return g, json.Unmarshal([]byte(plain), &g)
|
||||
}
|
||||
|
||||
// publishAdvance gives every consumer of a licence a new generation, with the cause ctx carries, so the node says why
|
||||
// it fetched again (novox/hq issue 625).
|
||||
func (m *Manager) publishAdvance(ctx context.Context, l Licence, floor func(string) int64) error {
|
||||
c := CauseOf(ctx)
|
||||
advanced, err := m.Store.Advance(ctx, l.Name, Move{Kind: l.Kind, Why: c.Why, By: c.By, Floor: floor})
|
||||
for _, b := range advanced {
|
||||
m.changed(b, b.Licence)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// floorIn is the floor of each consumer's generation (B11): the higher of what its node reported in reports and what
|
||||
// Reported says it last applied. A worker's node is the part of its name before the first dot (ADR 0183 §19).
|
||||
func (m *Manager) floorIn(reports []Holdings) func(string) int64 {
|
||||
return func(consumer string) int64 {
|
||||
node, _, _ := strings.Cut(consumer, ".")
|
||||
var f int64
|
||||
for _, r := range reports {
|
||||
if r.Node == node {
|
||||
f = max(f, r.Generation)
|
||||
}
|
||||
}
|
||||
if m.Reported != nil {
|
||||
f = max(f, m.Reported(node))
|
||||
}
|
||||
return f
|
||||
}
|
||||
}
|
||||
|
||||
// ---- keeping grants alive ----------------------------------------------------------------------------
|
||||
|
||||
// Due says whether a licence needs a refresh now: near expiry, or older than the cadence.
|
||||
func Due(l Licence, now time.Time, s Settings) bool {
|
||||
if l.Kind != "subscription" || l.Sealed == "" {
|
||||
if l.Kind != "subscription" || l.Generation == 0 {
|
||||
return false
|
||||
}
|
||||
ms := now.UnixMilli()
|
||||
@@ -484,7 +549,7 @@ func Due(l Licence, now time.Time, s Settings) bool {
|
||||
// lease live and does nothing; one started just after finds a fresh grant and is not due. force refreshes
|
||||
// whatever the age — the seat's `refresh` verb.
|
||||
func (m *Manager) Rotate(ctx context.Context, name string, force bool) (map[string]any, error) {
|
||||
key := "licence:" + name
|
||||
key := "licence." + KeyFor(name)
|
||||
ok, err := m.Store.Lease(ctx, key, m.Holder, leaseFor)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -500,37 +565,38 @@ func (m *Manager) Rotate(ctx context.Context, name string, force bool) (map[stri
|
||||
if l == nil {
|
||||
return map[string]any{"licence": name, "refreshed": false, "reason": "no such licence"}, nil
|
||||
}
|
||||
if l.Kind != "subscription" || l.Sealed == "" {
|
||||
if l.Kind != "subscription" || l.Generation == 0 {
|
||||
return map[string]any{"licence": name, "refreshed": false, "reason": "an API key does not refresh"}, nil
|
||||
}
|
||||
now := m.Now()
|
||||
if !force && !Due(*l, now, m.Settings) {
|
||||
if !force && !Due(*l, now, m.settings()) {
|
||||
return map[string]any{"licence": name, "refreshed": false, "reason": "not due"}, nil
|
||||
}
|
||||
plain, err := m.Crypt.Open(l.Sealed)
|
||||
g, err := m.grantOf(ctx, *l)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var g FullGrant
|
||||
if err := json.Unmarshal([]byte(plain), &g); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r := m.Vendor.Refresh(ctx, g)
|
||||
g = FullGrant{}
|
||||
if !r.OK {
|
||||
l.Failures++
|
||||
m.Log("%s did not refresh (%d in a row): %d %s", name, l.Failures, r.Status, r.Reason)
|
||||
_ = m.Store.Audit(ctx, "failed", map[string]any{"licence": name, "status": r.Status, "reason": r.Reason, "failures": l.Failures})
|
||||
m.notify(l, fmt.Sprintf("refresh failed %d time(s) in a row: %d", l.Failures, r.Status), l.Failures >= m.Settings.FailuresToNotify)
|
||||
if err := m.Store.Audit(ctx, "failed", map[string]any{"licence": name, "status": r.Status, "reason": r.Reason, "failures": l.Failures}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m.notify(l, fmt.Sprintf("refresh failed %d time(s) in a row: %d", l.Failures, r.Status), l.Failures >= m.settings().FailuresToNotify)
|
||||
if err := m.Store.SaveLicence(ctx, *l); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]any{"licence": name, "refreshed": false, "reason": fmt.Sprintf("%d %s", r.Status, r.Reason), "failures": l.Failures}, nil
|
||||
}
|
||||
m.keep(l, r.Grant)
|
||||
if err := m.keep(ctx, l, r.Grant); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if l.RefreshExpiresAt != 0 {
|
||||
left := time.Duration(l.RefreshExpiresAt-now.UnixMilli()) * time.Millisecond
|
||||
m.notify(l, fmt.Sprintf("its refresh token expires in %.1f day(s): a person must log in again", left.Hours()/24),
|
||||
left < m.Settings.RefreshWarn)
|
||||
left < m.settings().RefreshWarn)
|
||||
}
|
||||
if err := m.Store.SaveLicence(ctx, *l); err != nil {
|
||||
return nil, err
|
||||
@@ -545,10 +611,12 @@ func (m *Manager) Rotate(ctx context.Context, name string, force bool) (map[stri
|
||||
}
|
||||
}
|
||||
ctx = WithCause(ctx, Cause{Why: "a rotation of " + name + " (" + c.Why + ")", By: c.By})
|
||||
m.record(ctx, "rotated", fmt.Sprintf("rotated %s, its token now expiring %s: %s; asked by %s", name,
|
||||
if err := m.record(ctx, "rotated", fmt.Sprintf("rotated %s, its token now expiring %s: %s; asked by %s", name,
|
||||
time.UnixMilli(l.AccessExpiresAt).UTC().Format(time.RFC3339), c.Why, c.By),
|
||||
map[string]any{"licence": name, "expiresAt": l.AccessExpiresAt, "why": c.Why, "by": c.By})
|
||||
if err := m.publishAdvance(ctx, *l); err != nil {
|
||||
map[string]any{"licence": name, "expiresAt": l.AccessExpiresAt, "why": c.Why, "by": c.By}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.publishAdvance(ctx, *l, m.floorIn(nil)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]any{"licence": name, "refreshed": true, "expiresAt": time.UnixMilli(l.AccessExpiresAt).UTC().Format(time.RFC3339)}, nil
|
||||
@@ -560,7 +628,7 @@ func (m *Manager) notify(l *Licence, why string, when bool) {
|
||||
return
|
||||
}
|
||||
now := m.Now().UnixMilli()
|
||||
if l.NotifiedAt != 0 && now-l.NotifiedAt < m.Settings.Cooldown.Milliseconds() {
|
||||
if l.NotifiedAt != 0 && now-l.NotifiedAt < m.settings().Cooldown.Milliseconds() {
|
||||
return
|
||||
}
|
||||
l.NotifiedAt = now
|
||||
@@ -575,7 +643,7 @@ func (m *Manager) RotateDue(ctx context.Context) ([]map[string]any, error) {
|
||||
}
|
||||
var out []map[string]any
|
||||
for _, l := range all {
|
||||
if Due(l, m.Now(), m.Settings) {
|
||||
if Due(l, m.Now(), m.settings()) {
|
||||
r, err := m.Rotate(ctx, l.Name, false)
|
||||
if err != nil {
|
||||
return out, err
|
||||
@@ -586,79 +654,165 @@ func (m *Manager) RotateDue(ctx context.Context) ([]map[string]any, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ReadUsage reads and records each subscription's usage (ADR 0054); the event names the licence and numbers.
|
||||
// ReadUsage reads every subscription's usage, on the manager's cadence (§24): each reading written, the rule applied
|
||||
// after it (§26), and the conditions said (§9).
|
||||
func (m *Manager) ReadUsage(ctx context.Context) error {
|
||||
_, err := m.ReadUsageOf(ctx, "", false)
|
||||
return err
|
||||
}
|
||||
|
||||
// Read is one reading as licences_read_usage answers it (§38): the licence, the reading or why it failed, and what §26
|
||||
// did or, with dry, would do.
|
||||
type Read struct {
|
||||
Licence string `json:"licence"`
|
||||
Reading *UsageReading `json:"reading,omitempty"`
|
||||
Error string `json:"readError,omitempty"`
|
||||
Switches []Switch `json:"switches"`
|
||||
}
|
||||
|
||||
// ReadUsageOf reads the usage of one subscription, or every one when licence is empty, as §24 does, and applies §26
|
||||
// after each reading unless dry; dry answers what §26 would do (§32, §38).
|
||||
func (m *Manager) ReadUsageOf(ctx context.Context, licence string, dry bool) ([]Read, error) {
|
||||
if err := m.ReadSettings(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
all, err := m.Store.Licences(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
out := []Read{}
|
||||
for _, l := range all {
|
||||
if l.Kind != "subscription" || l.Sealed == "" {
|
||||
if l.Kind != "subscription" || l.Generation == 0 || (licence != "" && l.Name != licence) {
|
||||
continue
|
||||
}
|
||||
plain, err := m.Crypt.Open(l.Sealed)
|
||||
// The reading takes the licence's grant under its lease, so it never reads a grant a refresh is replacing (§4,
|
||||
// §16); a grant the vault does not answer is a failed reading like a vendor's refusal (§31).
|
||||
key := "licence." + KeyFor(l.Name)
|
||||
leased, err := m.Store.Lease(ctx, key, m.Holder, leaseFor)
|
||||
if err != nil {
|
||||
return err
|
||||
return out, err
|
||||
}
|
||||
var g FullGrant
|
||||
_ = json.Unmarshal([]byte(plain), &g)
|
||||
raw, err := m.Vendor.Usage(ctx, g.AccessToken)
|
||||
if err != nil || raw == nil {
|
||||
if !leased {
|
||||
out = append(out, Read{Licence: l.Name, Error: "another run holds its lease", Switches: []Switch{}})
|
||||
continue
|
||||
}
|
||||
unlease := func() { _ = m.Store.Unlease(ctx, key, m.Holder) }
|
||||
if fresh, err := m.Store.Licence(ctx, l.Name); err == nil && fresh != nil {
|
||||
l = *fresh
|
||||
}
|
||||
var raw map[string]any
|
||||
g, err := m.grantOf(ctx, l)
|
||||
if err == nil {
|
||||
raw, err = m.Vendor.Usage(ctx, g.AccessToken)
|
||||
}
|
||||
g = FullGrant{}
|
||||
if err == nil && raw == nil {
|
||||
err = errors.New("the usage endpoint answered nothing")
|
||||
}
|
||||
if err != nil {
|
||||
// A failed reading keeps the last percentages and writes the vendor's status and reason (§24).
|
||||
kept := UsageReading{}
|
||||
if l.Usage != nil {
|
||||
kept = *l.Usage
|
||||
}
|
||||
kept.ReadError = err.Error()
|
||||
l.Usage = &kept
|
||||
err := m.Store.SaveLicence(ctx, l)
|
||||
unlease()
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
m.Log("reading the usage of %s failed: %v", l.Name, err)
|
||||
out = append(out, Read{Licence: l.Name, Error: kept.ReadError, Switches: []Switch{}})
|
||||
continue
|
||||
}
|
||||
reading := FlattenUsage(raw)
|
||||
if err := m.Store.RecordUsage(ctx, l.Name, m.Now().UnixMilli(), reading, raw); err != nil {
|
||||
return err
|
||||
unlease()
|
||||
return out, err
|
||||
}
|
||||
// The latest reading beside its licence, so licences answers from one key (§2).
|
||||
latest := reading
|
||||
latest.ReadAt = m.Now().UTC().Format(time.RFC3339Nano)
|
||||
l.Usage = &latest
|
||||
err = m.Store.SaveLicence(ctx, l)
|
||||
unlease()
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
_ = m.Emit("usage.read", map[string]any{"licence": l.Name, "sessionPct": reading.SessionPct,
|
||||
"weeklyPct": reading.WeeklyPct, "sonnetPct": reading.SonnetPct})
|
||||
"sessionResetsAt": reading.SessionResetsAt, "weeklyPct": reading.WeeklyPct,
|
||||
"weeklyResetsAt": reading.WeeklyResetsAt, "sonnetPct": reading.SonnetPct,
|
||||
"sonnetResetsAt": reading.SonnetResetsAt})
|
||||
// On each reading, a licence at its threshold moves every consumer bound to it (§26).
|
||||
acts, err := m.ApplyRule(ctx, l.Name, false, dry)
|
||||
if acts == nil {
|
||||
acts = []Switch{}
|
||||
}
|
||||
out = append(out, Read{Licence: l.Name, Reading: &latest, Switches: acts})
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
// What the readings and the settings now hold is said, each condition only when what it says changed (§9).
|
||||
return out, m.SayConditions(ctx, false)
|
||||
}
|
||||
|
||||
// ---- the seat's verbs --------------------------------------------------------------------------------
|
||||
|
||||
// Current is a consumer's token sealed to the key it sent (ADR 0206 §6): for a subscription the access
|
||||
// token and its expiries only — never the refresh token, which no node holds. Nil when it is bound to
|
||||
// Current is which licence a consumer is bound to and its health, never the token (design 39 bus-and-switch §18, B7,
|
||||
// B19): the licence, its key (the name of its secrets in the vault), kind, generation, health and accessExpiresAt, and
|
||||
// for a subscription the account's identity the agent module points its account file at. Nil when it is bound to
|
||||
// nothing.
|
||||
func (m *Manager) Current(ctx context.Context, consumer, publicKey string) (map[string]any, error) {
|
||||
if !strings.Contains(publicKey, "PUBLIC KEY") {
|
||||
return nil, errors.New("current seals to the consumer's public key, and none was given")
|
||||
}
|
||||
func (m *Manager) Current(ctx context.Context, consumer string) (map[string]any, error) {
|
||||
b, err := m.Store.Binding(ctx, consumer)
|
||||
if err != nil || b == nil {
|
||||
return nil, err
|
||||
}
|
||||
l, err := m.Store.Licence(ctx, b.Licence)
|
||||
if err != nil || l == nil || l.Sealed == "" {
|
||||
if err != nil || l == nil {
|
||||
return nil, err
|
||||
}
|
||||
plain, err := m.Crypt.Open(l.Sealed)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
out := map[string]any{"licence": l.Name, "key": KeyFor(l.Name), "kind": l.Kind, "generation": b.Generation,
|
||||
"health": Health(*l, m.Now(), m.settings())}
|
||||
if l.AccessExpiresAt != 0 {
|
||||
out["accessExpiresAt"] = time.UnixMilli(l.AccessExpiresAt).UTC().Format(time.RFC3339)
|
||||
}
|
||||
handed := plain
|
||||
if l.Kind == "subscription" {
|
||||
var g FullGrant
|
||||
if err := json.Unmarshal([]byte(plain), &g); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
access, _ := json.Marshal(map[string]any{"accessToken": g.AccessToken, "expiresAt": g.ExpiresAt,
|
||||
"refreshTokenExpiresAt": g.RefreshTokenExpiresAt, "scopes": g.Scopes,
|
||||
"subscriptionType": g.SubscriptionType, "rateLimitTier": g.RateLimitTier})
|
||||
handed = string(access)
|
||||
}
|
||||
box, err := Seal(handed, publicKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := map[string]any{"licence": l.Name, "kind": l.Kind, "generation": b.Generation, "sealed": box}
|
||||
if l.AccountUUID != "" {
|
||||
out["identity"] = Identity{AccountUUID: l.AccountUUID, EmailAddress: l.Email, OrganizationUUID: l.OrganizationUUID}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Health is a licence's health (B19): `failing` once its refresh failed failures_to_notify times in a row, `expiring`
|
||||
// when its refresh token expires within refresh_warn_days, else `ok`.
|
||||
func Health(l Licence, now time.Time, s Settings) string {
|
||||
switch {
|
||||
case l.Failures >= s.FailuresToNotify:
|
||||
return "failing"
|
||||
case l.RefreshExpiresAt != 0 && l.RefreshExpiresAt-now.UnixMilli() < s.RefreshWarn.Milliseconds():
|
||||
return "expiring"
|
||||
}
|
||||
return "ok"
|
||||
}
|
||||
|
||||
// BindingsOf is every binding, or the one of consumer when one is named (B10): a list either way, unpaged, each with
|
||||
// its licence's key beside the §3 fields (B25).
|
||||
func (m *Manager) BindingsOf(ctx context.Context, consumer string) ([]Binding, error) {
|
||||
all, err := m.Store.Bindings(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := []Binding{}
|
||||
for _, b := range all {
|
||||
if consumer == "" || b.Consumer == consumer {
|
||||
b.Key = KeyFor(b.Licence)
|
||||
out = append(out, b)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Bind binds or switches a consumer: a person's act (ADR 0183), told to the consumer as a new generation, and
|
||||
// recorded with the cause ctx carries — the verb and its proven caller (novox/hq issue 625).
|
||||
func (m *Manager) Bind(ctx context.Context, consumer, licence string) (map[string]any, error) {
|
||||
@@ -673,7 +827,8 @@ func (m *Manager) Bind(ctx context.Context, consumer, licence string) (map[strin
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b, err := m.Store.Bind(ctx, consumer, licence)
|
||||
c := CauseOf(ctx)
|
||||
b, err := m.Store.Bind(ctx, consumer, licence, Move{Kind: l.Kind, Why: c.Why, By: c.By, Floor: m.floorIn(nil)})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -681,12 +836,12 @@ func (m *Manager) Bind(ctx context.Context, consumer, licence string) (map[strin
|
||||
if before != nil {
|
||||
from = before.Licence
|
||||
}
|
||||
c := CauseOf(ctx)
|
||||
m.moved(ctx, consumer, from, licence, b.Generation, c)
|
||||
if err := m.PutBinding(ctx, consumer, BindingState{Licence: licence, Kind: l.Kind, Generation: b.Generation,
|
||||
Why: c.Why, By: c.By}); err != nil {
|
||||
if err := m.moved(ctx, b, from, c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if from != "" && from != licence {
|
||||
_ = m.Emit("licence.switched", switchedFields(from, licence, []string{consumer}, c)) // one per act (§27, §36)
|
||||
}
|
||||
return map[string]any{"consumer": consumer, "licence": licence, "generation": b.Generation, "from": from}, nil
|
||||
}
|
||||
|
||||
@@ -699,12 +854,12 @@ func (m *Manager) Release(ctx context.Context, consumer string) (map[string]any,
|
||||
if ok, err := m.Store.Unbind(ctx, consumer); err != nil || !ok {
|
||||
return map[string]any{"consumer": consumer, "released": false, "reason": "it was bound to nothing"}, err
|
||||
}
|
||||
if err := m.DeleteBinding(ctx, consumer); err != nil {
|
||||
c := CauseOf(ctx)
|
||||
if err := m.record(ctx, "released", fmt.Sprintf("released %s from %s: %s; asked by %s", consumer, was.Licence, c.Why, c.By),
|
||||
map[string]any{"consumer": consumer, "licence": was.Licence, "generation": was.Generation, "why": c.Why, "by": c.By}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c := CauseOf(ctx)
|
||||
m.record(ctx, "released", fmt.Sprintf("released %s from %s: %s; asked by %s", consumer, was.Licence, c.Why, c.By),
|
||||
map[string]any{"consumer": consumer, "licence": was.Licence, "generation": was.Generation, "why": c.Why, "by": c.By})
|
||||
m.changed(Binding{Consumer: consumer, Generation: was.Generation, Why: c.Why, By: c.By}, was.Licence)
|
||||
return map[string]any{"consumer": consumer, "released": true, "was": was.Licence}, nil
|
||||
}
|
||||
|
||||
@@ -751,22 +906,44 @@ func (m *Manager) adoptKey(ctx context.Context, name, key, from string) (map[str
|
||||
if held != nil && held.Kind != "api-key" {
|
||||
return nil, fmt.Errorf("%s is a subscription; an API key needs a name of its own", name)
|
||||
}
|
||||
l := Licence{Name: name, Kind: "api-key", Sealed: m.Crypt.Seal(key), AdoptedAt: m.Now().UnixMilli()}
|
||||
taken, err := m.keyTaken(ctx, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if taken != "" {
|
||||
why := "its key " + KeyFor(name) + " is the key of the licence " + taken
|
||||
if err := m.Store.Audit(ctx, "refused", map[string]any{"licence": name, "kind": "api-key", "from": from, "reason": why}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = m.Emit("licence.refused", map[string]any{"licence": name, "from": from, "reason": why})
|
||||
return nil, fmt.Errorf("%s is refused: %s", name, why)
|
||||
}
|
||||
l := Licence{Name: name, Kind: "api-key", AdoptedAt: m.Now().UnixMilli(), AdoptedFrom: from}
|
||||
if held != nil {
|
||||
l.Generation, l.Usage = held.Generation, held.Usage
|
||||
}
|
||||
if err := m.handOver(ctx, &l, key, key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.Store.SaveLicence(ctx, l); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c := CauseOf(ctx)
|
||||
_ = m.Store.Audit(ctx, "adopted", map[string]any{"licence": name, "kind": "api-key", "from": from, "why": c.Why, "by": c.By})
|
||||
if err := m.Store.Audit(ctx, "adopted", map[string]any{"licence": name, "kind": "api-key", "from": from, "why": c.Why, "by": c.By}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m.Log("adopted the API key %s from %s; asked by %s", name, from, c.By)
|
||||
ctx = WithCause(ctx, Cause{Why: "the API key " + name + " adopted from " + from, By: c.By})
|
||||
if err := m.publishAdvance(ctx, l); err != nil {
|
||||
if err := m.publishAdvance(ctx, l, m.floorIn(nil)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = m.Emit("licence.adopted", map[string]any{"licence": name, "from": from, "replaced": held != nil})
|
||||
return map[string]any{"licence": name, "kind": "api-key", "adopted": true, "fingerprint": Fingerprint(key)}, nil
|
||||
}
|
||||
|
||||
// Licences is each licence as a person reads it: health and who is bound, never a token.
|
||||
// Licences is each licence as a person reads it (design 39 bus-and-switch §34, §47, B19, B40): today's fields, its
|
||||
// latest usage with its reset times, its room or why it is not known, whether it is the target a full licence's
|
||||
// consumers move to next, when and from where it was adopted, and the conditions open about it. Never a token.
|
||||
func (m *Manager) Licences(ctx context.Context) ([]map[string]any, error) {
|
||||
all, err := m.Store.Licences(ctx)
|
||||
if err != nil {
|
||||
@@ -776,27 +953,52 @@ func (m *Manager) Licences(ctx context.Context) ([]map[string]any, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
open, err := KeptConditions(m.Conditions)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := m.settings()
|
||||
stamp := func(ms int64) any {
|
||||
if ms == 0 {
|
||||
return nil
|
||||
}
|
||||
return time.UnixMilli(ms).UTC().Format(time.RFC3339)
|
||||
}
|
||||
targets := map[string]bool{}
|
||||
for _, l := range all {
|
||||
if len(consumersOf(l.Name, bindings)) > 0 {
|
||||
if to, _ := m.targetFor(l, all, s); to != "" {
|
||||
targets[to] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
out := []map[string]any{}
|
||||
for _, l := range all {
|
||||
bound := []string{}
|
||||
for _, b := range bindings {
|
||||
if b.Licence == l.Name {
|
||||
bound = append(bound, b.Consumer)
|
||||
}
|
||||
bound := consumersOf(l.Name, bindings)
|
||||
if bound == nil {
|
||||
bound = []string{}
|
||||
}
|
||||
account := l.Email
|
||||
if account == "" {
|
||||
account = l.AccountUUID
|
||||
}
|
||||
var room any = m.roomWords(l, s)
|
||||
if r, known := Room(l, m.Now(), s); known {
|
||||
room = r
|
||||
}
|
||||
conditions := []map[string]any{}
|
||||
prefix := "licence." + KeyFor(l.Name) + "."
|
||||
for _, c := range open {
|
||||
if strings.HasPrefix(c.Body.Key, prefix) {
|
||||
conditions = append(conditions, map[string]any{"key": c.Body.Key, "severity": c.Body.Severity,
|
||||
"headline": c.Body.Headline, "since": c.Since})
|
||||
}
|
||||
}
|
||||
out = append(out, map[string]any{"name": l.Name, "kind": l.Kind, "account": account,
|
||||
"accessExpiresAt": stamp(l.AccessExpiresAt), "refreshExpiresAt": stamp(l.RefreshExpiresAt),
|
||||
"rotatedAt": stamp(l.RotatedAt), "failures": l.Failures, "bound": bound})
|
||||
"organisation": l.OrganizationUUID, "accessExpiresAt": stamp(l.AccessExpiresAt),
|
||||
"refreshExpiresAt": stamp(l.RefreshExpiresAt), "rotatedAt": stamp(l.RotatedAt), "failures": l.Failures,
|
||||
"bound": bound, "usage": l.Usage, "room": room, "target": targets[l.Name], "adoptedAt": stamp(l.AdoptedAt),
|
||||
"adoptedFrom": l.AdoptedFrom, "conditions": conditions})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
+249
-88
@@ -10,6 +10,8 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
var t0 = time.Date(2026, 10, 4, 12, 0, 0, 0, time.UTC)
|
||||
@@ -22,6 +24,9 @@ type stubVendor struct {
|
||||
issued int
|
||||
account string
|
||||
now func() time.Time
|
||||
// usage is what the usage endpoint answers, and usageErr its failure; nil answers a session window alone.
|
||||
usage map[string]any
|
||||
usageErr error
|
||||
}
|
||||
|
||||
func (v *stubVendor) Refresh(_ context.Context, g FullGrant) Refreshed {
|
||||
@@ -44,31 +49,89 @@ func (v *stubVendor) Refresh(_ context.Context, g FullGrant) Refreshed {
|
||||
}
|
||||
|
||||
func (v *stubVendor) Usage(context.Context, string) (map[string]any, error) {
|
||||
if v.usageErr != nil {
|
||||
return nil, v.usageErr
|
||||
}
|
||||
if v.usage != nil {
|
||||
return v.usage, nil
|
||||
}
|
||||
return map[string]any{"five_hour": map[string]any{"utilization": 12.0}}, nil
|
||||
}
|
||||
|
||||
// memGrants is the vault as the manager sees it: every licence's two secrets by name, the newest generation kept.
|
||||
type memGrants struct {
|
||||
mu sync.Mutex
|
||||
held map[string]heldGrant
|
||||
puts []string
|
||||
// refuseAccess is the vault refusing a licence's access token to the manager's machine, as it does for a licence no
|
||||
// binding there names (§17).
|
||||
refuseAccess bool
|
||||
}
|
||||
|
||||
type heldGrant struct {
|
||||
generation int64
|
||||
value string
|
||||
}
|
||||
|
||||
func (g *memGrants) Put(_ context.Context, key, part string, generation int64, value string) error {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
name := SecretName(key, part)
|
||||
if h, ok := g.held[name]; ok && h.generation == generation && h.value == value {
|
||||
return nil // the vault keeps what it holds, unchanged
|
||||
} else if ok && h.generation >= generation {
|
||||
return fmt.Errorf("%s is held at generation %d, and generation %d is not newer", name, h.generation, generation)
|
||||
}
|
||||
g.held[name] = heldGrant{generation, value}
|
||||
g.puts = append(g.puts, fmt.Sprintf("%s@%d", name, generation))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *memGrants) Get(_ context.Context, key, part string) (string, error) {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
h, ok := g.held[SecretName(key, part)]
|
||||
if g.refuseAccess && part == PartAccess {
|
||||
return "", fmt.Errorf("no consumer on this machine is bound to the licence %s", key)
|
||||
}
|
||||
if !ok {
|
||||
return "", fmt.Errorf("the vault holds no secret named %s", SecretName(key, part))
|
||||
}
|
||||
return h.value, nil
|
||||
}
|
||||
|
||||
type miniMesh struct {
|
||||
m *Manager
|
||||
store *MemoryStore
|
||||
vendor *stubVendor
|
||||
logins map[string]FullGrant // node → what its credentials file holds
|
||||
state map[string]BindingState
|
||||
events []string
|
||||
now time.Time
|
||||
keys KeyPair
|
||||
askDown bool
|
||||
grants *memGrants
|
||||
m *Manager
|
||||
store *BusStore
|
||||
usage *memLog
|
||||
audit *memLog
|
||||
licences *memState
|
||||
bindings *memState
|
||||
leases *memState
|
||||
offered *memState
|
||||
vendor *stubVendor
|
||||
logins map[string]FullGrant // node → what its credentials file holds
|
||||
events []string
|
||||
now time.Time
|
||||
keys KeyPair
|
||||
askDown bool
|
||||
}
|
||||
|
||||
func newMesh(t *testing.T) *miniMesh {
|
||||
t.Helper()
|
||||
mm := &miniMesh{logins: map[string]FullGrant{}, state: map[string]BindingState{}, now: t0}
|
||||
mm := &miniMesh{logins: map[string]FullGrant{}, now: t0,
|
||||
grants: &memGrants{held: map[string]heldGrant{}}}
|
||||
now := func() time.Time { return mm.now }
|
||||
mm.store = NewMemoryStore(now)
|
||||
mm.usage, mm.audit = newMemLog(now), newMemLog(now)
|
||||
mm.licences, mm.bindings = newMemState(now), newMemState(now)
|
||||
mm.leases, mm.offered = newMemState(now), newMemState(now)
|
||||
mm.store = &BusStore{LicencesState: mm.licences, BindingsState: mm.bindings, LeasesState: mm.leases,
|
||||
OfferedState: mm.offered, UsageLog: mm.usage, AuditLog: mm.audit, Now: now}
|
||||
mm.vendor = &stubVendor{live: map[string]bool{}, now: now}
|
||||
crypt, _ := NewCrypt("a key the vault made")
|
||||
mm.keys, _ = GenerateKeyPair()
|
||||
mm.m = &Manager{
|
||||
Store: mm.store, Vendor: mm.vendor, Crypt: crypt, Keys: mm.keys,
|
||||
Store: mm.store, Vendor: mm.vendor, Grants: mm.grants, Keys: mm.keys,
|
||||
AskGrant: func(_ context.Context, node, publicKey string) (GrantAnswer, error) {
|
||||
if mm.askDown {
|
||||
return GrantAnswer{}, fmt.Errorf("503 no responders")
|
||||
@@ -81,8 +144,6 @@ func newMesh(t *testing.T) *miniMesh {
|
||||
box, err := Seal(string(raw), publicKey)
|
||||
return GrantAnswer{Sealed: &box, Fingerprint: Fingerprint(g.RefreshToken)}, err
|
||||
},
|
||||
PutBinding: func(_ context.Context, c string, b BindingState) error { mm.state[c] = b; return nil },
|
||||
DeleteBinding: func(_ context.Context, c string) error { delete(mm.state, c); return nil },
|
||||
Emit: func(event string, body map[string]any) error {
|
||||
raw, _ := json.Marshal(body)
|
||||
mm.events = append(mm.events, event+" "+string(raw))
|
||||
@@ -93,6 +154,15 @@ func newMesh(t *testing.T) *miniMesh {
|
||||
return mm
|
||||
}
|
||||
|
||||
// bound is a consumer's value in the state bindings, as its readers decode it.
|
||||
func (mm *miniMesh) bound(consumer string) BindingState {
|
||||
var b BindingState
|
||||
if e, _ := mm.bindings.Get(consumer); e != nil {
|
||||
_ = json.Unmarshal(e.Value, &b)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (mm *miniMesh) report(node, rt string, at time.Time, account string) Holdings {
|
||||
h := Holdings{Node: node, Identity: &Identity{AccountUUID: account, EmailAddress: account + "@example.org"},
|
||||
Kind: "subscription", ChangedAt: at.Format(time.RFC3339Nano)}
|
||||
@@ -129,15 +199,14 @@ func TestAManagerWithNoLicenceAdoptsTheNewestLoginThatRefreshesAndNeverExchanges
|
||||
}
|
||||
// A first binding follows the login: both nodes reported this account and were bound to nothing.
|
||||
bs, _ := mm.store.Bindings(ctx)
|
||||
if len(bs) != 2 || mm.state["laptop"].Licence != licence1 || mm.state["server"].Licence != licence1 {
|
||||
t.Fatalf("bindings %v, state %v", bs, mm.state)
|
||||
if len(bs) != 2 || mm.bound("laptop").Licence != licence1 || mm.bound("server").Licence != licence1 {
|
||||
t.Fatalf("bindings %v, state %v", bs, mm.bindings.values)
|
||||
}
|
||||
// The adoption is said, and so is each binding it made (novox/hq issue 625).
|
||||
// The adoption is said, and so is each binding it made, once (novox/hq issue 625; design 39 bus-and-switch §21).
|
||||
events := strings.Join(mm.events, "|")
|
||||
for _, want := range []string{"licence.adopted ", `licence.bound {"by":"claude-licence-manager","consumer":"laptop"`,
|
||||
`licence.bound {"by":"claude-licence-manager","consumer":"server"`} {
|
||||
if !strings.Contains(events, want) {
|
||||
t.Fatalf("events lack %s: %v", want, mm.events)
|
||||
for _, want := range []string{"licence.adopted ", `"consumer":"laptop"`, `"consumer":"server"`} {
|
||||
if strings.Count(events, want) != 1 {
|
||||
t.Fatalf("events say %s other than once: %v", want, mm.events)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -169,6 +238,12 @@ func TestANewerLoginReplacesTheGrantHeldAndAnOlderOneDoesNot(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0.Add(-time.Minute))})
|
||||
before, _ := mm.store.Licence(ctx, licence1)
|
||||
// §2: a licence says where it was adopted from, and a newer login keeps the latest reading the key holds.
|
||||
pct := 42.0
|
||||
before.Usage = &UsageReading{SessionPct: &pct, ReadAt: t0.Format(time.RFC3339Nano)}
|
||||
if before.AdoptedFrom != "laptop" || mm.store.SaveLicence(ctx, *before) != nil {
|
||||
t.Fatalf("the adopted licence is %+v", before)
|
||||
}
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("server", "rt-old", true, t0.Add(-24*time.Hour))})
|
||||
if strings.Join(mm.vendor.exchanged, ",") != "rt-a" {
|
||||
t.Fatalf("an older login was exchanged: %v", mm.vendor.exchanged)
|
||||
@@ -179,6 +254,9 @@ func TestANewerLoginReplacesTheGrantHeldAndAnOlderOneDoesNot(t *testing.T) {
|
||||
if after.RefreshFingerprint == before.RefreshFingerprint || mm.vendor.exchanged[len(mm.vendor.exchanged)-1] != "rt-new" {
|
||||
t.Fatalf("a newer login did not win: %v", mm.vendor.exchanged)
|
||||
}
|
||||
if after.AdoptedFrom != "desktop" || after.Usage == nil || after.Usage.SessionPct == nil || *after.Usage.SessionPct != 42 {
|
||||
t.Fatalf("after a newer login the licence is %+v", after)
|
||||
}
|
||||
if ls, _ := mm.store.Licences(ctx); len(ls) != 1 {
|
||||
t.Fatalf("one account became %d licences", len(ls))
|
||||
}
|
||||
@@ -201,11 +279,12 @@ func TestTwoRefreshRunsStartedTogetherRotateAGrantOnce(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
|
||||
mm.now = t0.Add(5 * time.Hour)
|
||||
second := *mm.m
|
||||
second.Holder = "another run"
|
||||
second := &Manager{Store: mm.m.Store, Vendor: mm.m.Vendor, Grants: mm.m.Grants, Keys: mm.m.Keys,
|
||||
AskGrant: mm.m.AskGrant, Emit: mm.m.Emit, Now: mm.m.Now, Log: mm.m.Log, Holder: "another run",
|
||||
Settings: mm.m.Settings}
|
||||
var wg sync.WaitGroup
|
||||
results := make([]map[string]any, 2)
|
||||
for i, m := range []*Manager{mm.m, &second} {
|
||||
for i, m := range []*Manager{mm.m, second} {
|
||||
wg.Add(1)
|
||||
go func() { defer wg.Done(); results[i], _ = m.Rotate(ctx, licence1, false) }()
|
||||
}
|
||||
@@ -228,9 +307,9 @@ func TestARotationAndASwitchEachGiveANewerGeneration(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
|
||||
g0 := mm.state["laptop"].Generation
|
||||
g0 := mm.bound("laptop").Generation
|
||||
_, _ = mm.m.Rotate(ctx, licence1, true)
|
||||
g1 := mm.state["laptop"].Generation
|
||||
g1 := mm.bound("laptop").Generation
|
||||
if g1 <= g0 {
|
||||
t.Fatalf("a rotation went from %d to %d", g0, g1)
|
||||
}
|
||||
@@ -242,43 +321,54 @@ func TestARotationAndASwitchEachGiveANewerGeneration(t *testing.T) {
|
||||
if _, err := mm.m.Bind(ctx, "laptop", "api"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if mm.state["laptop"].Licence != "api" || mm.state["laptop"].Generation <= g1 {
|
||||
t.Fatalf("a switch to a licence rotated less often went backwards: %v", mm.state["laptop"])
|
||||
if mm.bound("laptop").Licence != "api" || mm.bound("laptop").Generation <= g1 {
|
||||
t.Fatalf("a switch to a licence rotated less often went backwards: %v", mm.bound("laptop"))
|
||||
}
|
||||
if _, err := mm.m.Release(ctx, "laptop"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, ok := mm.state["laptop"]; ok {
|
||||
if _, ok := mm.bindings.values["laptop"]; ok {
|
||||
t.Fatal("a released consumer still has a binding in the state")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurrentHandsAnAccessTokenOnlySealedToTheConsumersKey(t *testing.T) {
|
||||
// design 39 bus-and-switch §18, B7, B19: no answer of current holds a token; it names the licence's key and health.
|
||||
func TestCurrentHoldsNoToken(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
|
||||
node, _ := GenerateKeyPair()
|
||||
answer, err := mm.m.Current(ctx, "laptop", node.PublicKey)
|
||||
if err != nil || answer["kind"] != "subscription" {
|
||||
answer, err := mm.m.Current(ctx, "laptop")
|
||||
if err != nil || answer["kind"] != "subscription" || answer["key"] != KeyFor(licence1) || answer["health"] != "ok" ||
|
||||
answer["accessExpiresAt"] == nil || answer["identity"] == nil {
|
||||
t.Fatalf("%v %v", answer, err)
|
||||
}
|
||||
box := answer["sealed"].(SealedBox)
|
||||
plain, err := Open(box, node.PrivateKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
raw, _ := json.Marshal(answer)
|
||||
for _, token := range []string{"rt-", "at-", "sealed"} {
|
||||
if strings.Contains(string(raw), token) {
|
||||
t.Fatalf("current answered %s: %s", token, raw)
|
||||
}
|
||||
}
|
||||
var handed map[string]any
|
||||
_ = json.Unmarshal([]byte(plain), &handed)
|
||||
if !strings.HasPrefix(handed["accessToken"].(string), "at-") || handed["refreshToken"] != nil {
|
||||
t.Fatalf("handed %v", handed)
|
||||
l, _ := mm.store.Licence(ctx, licence1)
|
||||
for failures, health := range map[int]string{0: "ok", 3: "failing"} {
|
||||
l.Failures = failures
|
||||
if got := Health(*l, mm.now, mm.m.Settings); got != health {
|
||||
t.Errorf("%d failures: %s, want %s", failures, got, health)
|
||||
}
|
||||
}
|
||||
other, _ := GenerateKeyPair()
|
||||
if _, err := Open(box, other.PrivateKey); err == nil {
|
||||
t.Fatal("the hand-over opened with another key")
|
||||
l.Failures, l.RefreshExpiresAt = 0, mm.now.Add(48*time.Hour).UnixMilli()
|
||||
if got := Health(*l, mm.now, mm.m.Settings); got != "expiring" {
|
||||
t.Errorf("a refresh token expiring in two days: %s", got)
|
||||
}
|
||||
if a, _ := mm.m.Current(ctx, "nobody", node.PublicKey); a != nil {
|
||||
if a, _ := mm.m.Current(ctx, "nobody"); a != nil {
|
||||
t.Fatalf("a consumer bound to nothing was answered %v", a)
|
||||
}
|
||||
if bs, _ := mm.m.BindingsOf(ctx, "laptop"); len(bs) != 1 || bs[0].Consumer != "laptop" {
|
||||
t.Fatalf("bindings of laptop: %v", bs)
|
||||
}
|
||||
// design 39 bus-and-switch B25: the verb bindings answers each binding's licence key beside the §3 fields.
|
||||
if bs, _ := mm.m.BindingsOf(ctx, ""); len(bs) == 0 || bs[0].Key != KeyFor(bs[0].Licence) {
|
||||
t.Fatalf("bindings answered %+v", bs)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNothingTheManagerPublishesKeepsOrListsCarriesAToken(t *testing.T) {
|
||||
@@ -290,15 +380,15 @@ func TestNothingTheManagerPublishesKeepsOrListsCarriesAToken(t *testing.T) {
|
||||
_ = mm.m.ReadUsage(ctx)
|
||||
ls, _ := mm.m.Licences(ctx)
|
||||
bs, _ := mm.store.Bindings(ctx)
|
||||
everything, _ := json.Marshal([]any{mm.events, mm.state, ls, bs})
|
||||
everything, _ := json.Marshal([]any{mm.events, mm.bindings.values, mm.licences.values, ls, bs})
|
||||
for _, token := range []string{"rt-secret", "rt-1", "rt-2", "at-1", "at-2", "local-"} {
|
||||
if strings.Contains(string(everything), token) {
|
||||
t.Fatalf("%s was published: %s", token, everything)
|
||||
}
|
||||
}
|
||||
row, _ := mm.store.Licence(ctx, licence1)
|
||||
if strings.Contains(row.Sealed, "rt-") {
|
||||
t.Fatal("the grant is stored in the clear")
|
||||
if raw, _ := json.Marshal(row); strings.Contains(string(raw), "rt-") || strings.Contains(string(raw), "at-") {
|
||||
t.Fatalf("the licence keeps a token: %s", raw)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -324,22 +414,40 @@ func TestANodeThatDoesNotAnswerIsAskedAgainAndOneWithNoLoginIsSettled(t *testing
|
||||
}
|
||||
}
|
||||
|
||||
func TestAReportIsReadAsTheAgentModuleWritesIt(t *testing.T) {
|
||||
// The agent module's own report shape (claude-code's holdingsOf), parsed here.
|
||||
raw := `{"node":"laptop","identity":{"accountUuid":"u-1","emailAddress":"a@example.org"},"kind":"subscription",
|
||||
"refresh":{"present":true,"fingerprint":"sha256:0123456789abcdef","expiresAt":null},
|
||||
"access":{"fingerprint":"sha256:fedcba9876543210","expiresAt":1},"licence":null,"generation":0,
|
||||
"changedAt":"2026-10-04T11:00:00.000Z"}`
|
||||
var h Holdings
|
||||
if err := json.Unmarshal([]byte(raw), &h); err != nil {
|
||||
// design 39 bus-and-switch §23, §46, B12, B22: a holdings-changed, decoded from a copy of the agent module's form, is
|
||||
// kept as its node's latest report, and one carrying a fingerprint the manager does not hold is a candidate; the
|
||||
// manager asks every node with holdings.asked, its time and who asks.
|
||||
func TestAHoldingsChangedIsHeardAndMakesACandidate(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
raw, err := os.ReadFile("testdata/holdings-changed.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
heard := &Heard{}
|
||||
if _, err := heard.Take(raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
all := heard.All()
|
||||
if len(all) != 1 || heard.Of("laptop") != 4 {
|
||||
t.Fatalf("heard %+v", all)
|
||||
}
|
||||
h := all[0]
|
||||
if h.Node != "laptop" || h.Identity.AccountUUID != "u-1" || !h.Refresh.Present || h.Refresh.Fingerprint != "sha256:0123456789abcdef" {
|
||||
t.Fatalf("%+v", h)
|
||||
}
|
||||
if _, err := time.Parse(time.RFC3339Nano, h.ChangedAt); err != nil {
|
||||
t.Fatalf("the report's time does not parse: %v", err)
|
||||
}
|
||||
if c, err := mm.m.CandidatesIn(context.Background(), all); err != nil || len(c["u-1"]) != 1 {
|
||||
t.Fatalf("candidates %v %v", c, err)
|
||||
}
|
||||
if _, err := heard.Take([]byte(`{"kind":"subscription"}`)); err == nil {
|
||||
t.Fatal("a report naming no node was kept")
|
||||
}
|
||||
if err := mm.m.AskHoldings(Itself); err != nil || len(mm.events) != 1 ||
|
||||
mm.events[0] != HoldingsAsked+` {"at":"`+t0.UTC().Format(time.RFC3339Nano)+`","by":"claude-licence-manager"}` {
|
||||
t.Fatalf("said %v %v", mm.events, err)
|
||||
}
|
||||
}
|
||||
|
||||
// A node whose report arrives after its account was adopted — with an older login, so never a candidate —
|
||||
@@ -350,15 +458,15 @@ func TestANodeReportingAnAdoptedAccountLaterIsBoundToIt(t *testing.T) {
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("novox", "rt-new", true, t0)})
|
||||
late := mm.login("laptop", "rt-older", true, t0.Add(-24*time.Hour))
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{late})
|
||||
if mm.state["laptop"].Licence != licence1 {
|
||||
t.Fatalf("a node reporting the adopted account later was not bound: %v", mm.state)
|
||||
if mm.bound("laptop").Licence != licence1 {
|
||||
t.Fatalf("a node reporting the adopted account later was not bound: %v", mm.bindings.values)
|
||||
}
|
||||
if strings.Contains(strings.Join(mm.vendor.exchanged, ","), "rt-older") {
|
||||
t.Fatal("the older login was exchanged")
|
||||
}
|
||||
g := mm.state["laptop"].Generation
|
||||
g := mm.bound("laptop").Generation
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{late})
|
||||
if mm.state["laptop"].Generation != g {
|
||||
if mm.bound("laptop").Generation != g {
|
||||
t.Fatal("a node already bound was bound again")
|
||||
}
|
||||
}
|
||||
@@ -368,8 +476,8 @@ func TestALoginToAnotherAccountMovesItsNodeAndNoOther(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0), mm.report("server", "", t0, "acct-1")})
|
||||
if mm.state["laptop"].Licence != licence1 || mm.state["server"].Licence != licence1 {
|
||||
t.Fatalf("%v", mm.state)
|
||||
if mm.bound("laptop").Licence != licence1 || mm.bound("server").Licence != licence1 {
|
||||
t.Fatalf("%v", mm.bindings.values)
|
||||
}
|
||||
mm.now = t0.Add(time.Hour)
|
||||
mm.logins["laptop"] = FullGrant{AccessToken: "local", RefreshToken: "rt-b", ExpiresAt: 1}
|
||||
@@ -379,11 +487,11 @@ func TestALoginToAnotherAccountMovesItsNodeAndNoOther(t *testing.T) {
|
||||
if len(adopted) != 1 || adopted[0] != "acct-2@example.org" {
|
||||
t.Fatalf("adopted %v", adopted)
|
||||
}
|
||||
if mm.state["laptop"].Licence != "acct-2@example.org" {
|
||||
t.Fatalf("the node a login was made on stayed bound to %v", mm.state["laptop"])
|
||||
if mm.bound("laptop").Licence != "acct-2@example.org" {
|
||||
t.Fatalf("the node a login was made on stayed bound to %v", mm.bound("laptop"))
|
||||
}
|
||||
if mm.state["server"].Licence != licence1 {
|
||||
t.Fatalf("another node moved: %v", mm.state["server"])
|
||||
if mm.bound("server").Licence != licence1 {
|
||||
t.Fatalf("another node moved: %v", mm.bound("server"))
|
||||
}
|
||||
ls, _ := mm.store.Licences(ctx)
|
||||
if len(ls) != 2 {
|
||||
@@ -396,11 +504,11 @@ func TestALoginThatDoesNotRefreshMovesNothing(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
|
||||
g := mm.state["laptop"]
|
||||
g := mm.bound("laptop")
|
||||
mm.logins["laptop"] = FullGrant{AccessToken: "local", RefreshToken: "rt-dead", ExpiresAt: 1}
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.report("laptop", "rt-dead", t0.Add(time.Hour), "acct-2")})
|
||||
if mm.state["laptop"] != g {
|
||||
t.Fatalf("a dead login moved its node: %v", mm.state["laptop"])
|
||||
if mm.bound("laptop") != g {
|
||||
t.Fatalf("a dead login moved its node: %v", mm.bound("laptop"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -413,9 +521,10 @@ func TestAnAPIKeySealedByANodeIsAdopted(t *testing.T) {
|
||||
if err != nil || r["adopted"] != true {
|
||||
t.Fatalf("%v %v", r, err)
|
||||
}
|
||||
l, _ := mm.store.Licence(ctx, "api")
|
||||
if plain, _ := mm.m.Crypt.Open(l.Sealed); plain != "sk-ant-api03-test-0123456789abcdefghij" {
|
||||
t.Fatalf("stored %q", plain)
|
||||
for _, part := range []string{PartGrant, PartAccess} {
|
||||
if plain, _ := mm.grants.Get(ctx, "api", part); plain != "sk-ant-api03-test-0123456789abcdefghij" {
|
||||
t.Fatalf("the vault holds %q as the %s", plain, part)
|
||||
}
|
||||
}
|
||||
other, _ := GenerateKeyPair()
|
||||
wrong, _ := Seal("sk", other.PublicKey)
|
||||
@@ -439,7 +548,7 @@ func TestARebuiltStoreNeverGivesAGenerationANodeHasPassed(t *testing.T) {
|
||||
if _, err := mm.m.Consider(ctx, []Holdings{laptop}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if g := mm.state["laptop"].Generation; g <= 16 {
|
||||
if g := mm.bound("laptop").Generation; g <= 16 {
|
||||
t.Fatalf("the laptop applied generation 16 and was given %d", g)
|
||||
}
|
||||
}
|
||||
@@ -460,28 +569,68 @@ func TestWhatIsNotAnAPIKeyIsRefused(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// changedSince is the binding.changed events said from mark on, decoded.
|
||||
func (mm *miniMesh) changedSince(mark int) []map[string]any {
|
||||
var out []map[string]any
|
||||
for _, e := range mm.events[mark:] {
|
||||
if rest, ok := strings.CutPrefix(e, BindingChanged+"."); ok {
|
||||
// The consumer is the event's further part (A-134); one said under another consumer's name reads as none.
|
||||
consumer, body, _ := strings.Cut(rest, " ")
|
||||
var v map[string]any
|
||||
_ = json.Unmarshal([]byte(body), &v)
|
||||
if v["consumer"] != consumer {
|
||||
v["consumer"] = "said as binding.changed." + consumer
|
||||
}
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// A change of what a consumer holds says what, why and who in the journal, the audit and an event (novox/hq
|
||||
// issue 625): a login, a switch through the verb by a proven caller, a rotation and a release.
|
||||
func TestEveryMoveSaysWhatWhyAndWho(t *testing.T) {
|
||||
// issue 625): a login, a switch through the verb by a proven caller, a rotation and a release. design 39
|
||||
// bus-and-switch §21/AC1, §36/AC1, B7, B16: each says one binding.changed per consumer moved, with its licence's key,
|
||||
// after its key is written or deleted; a switch says one licence.switched with §27's fields.
|
||||
func TestEveryMoveSaysOneBindingChangedPerConsumer(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
var lines []string
|
||||
mm.m.Log = func(format string, args ...any) { lines = append(lines, fmt.Sprintf(format, args...)) }
|
||||
ctx := context.Background()
|
||||
once := func(mark int, consumer, licence, by string) map[string]any {
|
||||
t.Helper()
|
||||
got := mm.changedSince(mark)
|
||||
if len(got) != 1 || got[0]["consumer"] != consumer || got[0]["licence"] != licence || got[0]["by"] != by {
|
||||
t.Fatalf("binding.changed said %v, want one for %s to %q by %s", got, consumer, licence, by)
|
||||
}
|
||||
if licence != "" && got[0]["key"] != KeyFor(licence) {
|
||||
t.Fatalf("binding.changed names the key %v", got[0]["key"])
|
||||
}
|
||||
if mm.bound(consumer).Licence != licence {
|
||||
t.Fatalf("said before the key was written: %+v", mm.bound(consumer))
|
||||
}
|
||||
return got[0]
|
||||
}
|
||||
mark := len(mm.events)
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
|
||||
if s := mm.state["laptop"]; !strings.Contains(s.Why, "a login to "+licence1+" on laptop") || s.By != Itself {
|
||||
if s := mm.bound("laptop"); !strings.Contains(s.Why, "a login to "+licence1+" on laptop") || s.By != Itself {
|
||||
t.Fatalf("a login's binding said %+v", s)
|
||||
}
|
||||
once(mark, "laptop", licence1, Itself)
|
||||
file := filepath.Join(t.TempDir(), "key")
|
||||
_ = os.WriteFile(file, []byte("sk-ant-api03-key-0123456789abcdefghij\n"), 0o600)
|
||||
if _, err := mm.m.AdoptKey(ctx, "api", file); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
mark = len(mm.events)
|
||||
asked := WithCause(ctx, Cause{Why: "the switch verb", By: "g14.node-tools"})
|
||||
if _, err := mm.m.Bind(asked, "laptop", "api"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s := mm.state["laptop"]; s.Why != "the switch verb" || s.By != "g14.node-tools" {
|
||||
if e := once(mark, "laptop", "api", "g14.node-tools"); e["from"] != licence1 || e["kind"] != "api-key" || e["at"] == nil {
|
||||
t.Fatalf("the switch said %v", e)
|
||||
}
|
||||
if s := mm.bound("laptop"); s.Why != "the switch verb" || s.By != "g14.node-tools" {
|
||||
t.Fatalf("the node is told %+v", s)
|
||||
}
|
||||
said := strings.Join(lines, "\n")
|
||||
@@ -489,34 +638,46 @@ func TestEveryMoveSaysWhatWhyAndWho(t *testing.T) {
|
||||
!strings.Contains(said, "the switch verb; asked by g14.node-tools") {
|
||||
t.Fatalf("the journal said:\n%s", said)
|
||||
}
|
||||
var switched string
|
||||
var switched []string
|
||||
for _, e := range mm.events {
|
||||
if strings.HasPrefix(e, "licence.switched ") {
|
||||
switched = e
|
||||
switched = append(switched, e)
|
||||
}
|
||||
}
|
||||
for _, want := range []string{`"by":"g14.node-tools"`, `"from":"` + licence1 + `"`, `"licence":"api"`, `"consumer":"laptop"`, `"generation":`} {
|
||||
if !strings.Contains(switched, want) {
|
||||
t.Fatalf("the event %q lacks %s; events %v", switched, want, mm.events)
|
||||
if len(switched) != 1 {
|
||||
t.Fatalf("licence.switched said %d times: %v", len(switched), mm.events)
|
||||
}
|
||||
for _, want := range []string{`"by":"g14.node-tools"`, `"from":"` + licence1 + `"`, `"to":"api"`, `"consumers":["laptop"]`, `"why":"the switch verb"`} {
|
||||
if !strings.Contains(switched[0], want) {
|
||||
t.Fatalf("the event %q lacks %s", switched[0], want)
|
||||
}
|
||||
}
|
||||
last := mm.store.Audits[len(mm.store.Audits)-1]
|
||||
if raw, _ := json.Marshal(last); !strings.Contains(string(raw), `"by":"g14.node-tools"`) || !strings.Contains(string(raw), "switched") {
|
||||
all := mm.audit.under("switched")
|
||||
if raw, _ := json.Marshal(all[len(all)-1]); !strings.Contains(string(raw), `"by":"g14.node-tools"`) || !strings.Contains(string(raw), `"to":"api"`) {
|
||||
t.Fatalf("the audit kept %s", raw)
|
||||
}
|
||||
|
||||
_, _ = mm.m.Bind(ctx, "server", licence1)
|
||||
mark = len(mm.events)
|
||||
_, _ = mm.m.Rotate(WithCause(ctx, Cause{Why: "the refresh verb", By: "novox.node-tools"}), licence1, true)
|
||||
if !strings.Contains(strings.Join(mm.events, "|"), `licence.rotated {"by":"novox.node-tools"`) {
|
||||
t.Fatalf("a rotation asked for said no caller: %v", mm.events)
|
||||
}
|
||||
once(mark, "server", licence1, "novox.node-tools")
|
||||
|
||||
mark = len(mm.events)
|
||||
if _, err := mm.m.Release(WithCause(ctx, Cause{Why: "the release verb", By: "person.operator"}), "laptop"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if e := once(mark, "laptop", "", "person.operator"); e["from"] != "api" {
|
||||
t.Fatalf("the release said %v", e)
|
||||
}
|
||||
if said := strings.Join(lines, "\n"); !strings.Contains(said, "released laptop from api: the release verb; asked by person.operator") {
|
||||
t.Fatalf("the journal said:\n%s", said)
|
||||
}
|
||||
for _, e := range mm.events {
|
||||
for _, gone := range []string{"licence.bound ", "licence.released ", "licence.rotated "} {
|
||||
if strings.HasPrefix(e, gone) {
|
||||
t.Fatalf("an event no longer said was said: %s", e)
|
||||
}
|
||||
}
|
||||
if strings.Contains(e, "rt-a") || strings.Contains(e, "sk-ant-") || strings.Contains(e, "local-laptop") {
|
||||
t.Fatalf("an event carries a secret: %s", e)
|
||||
}
|
||||
@@ -0,0 +1,393 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// design 39 bus-and-switch §1: the module moves to the new form whole — no `reads`, no old `state`, no `prepare`
|
||||
// step — and asks for each state it keeps by template; its two logs are in the `logs` section with their bounds, the
|
||||
// form the runtime on main issues (C3).
|
||||
func TestTheManifestKeepsNoOldForm(t *testing.T) {
|
||||
raw, err := os.ReadFile(filepath.Join(moduleDir, "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var m map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, old := range []string{"reads", "state"} {
|
||||
if _, ok := m[old]; ok {
|
||||
t.Errorf("the manifest keeps the old section %q", old)
|
||||
}
|
||||
}
|
||||
var resources []struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
_ = json.Unmarshal(m["resources"], &resources)
|
||||
for _, r := range resources {
|
||||
if r.ID == "prepare" {
|
||||
t.Error("the manifest keeps the run-once step prepare")
|
||||
}
|
||||
}
|
||||
var requires []json.RawMessage
|
||||
_ = json.Unmarshal(m["requires"], &requires)
|
||||
kept := map[string]string{}
|
||||
for _, r := range requires {
|
||||
var e struct{ Name, Template string }
|
||||
if json.Unmarshal(r, &e) == nil {
|
||||
kept[e.Name] = e.Template
|
||||
}
|
||||
}
|
||||
for name, template := range map[string]string{"licences": "state", "bindings": "state", "leases": "state",
|
||||
"offered": "state", "usage": "", "audit": "", "carried": "state", "conditions": "state"} {
|
||||
if kept[name] != template {
|
||||
t.Errorf("bus-state entry %s is %q in requires, want %q", name, kept[name], template)
|
||||
}
|
||||
}
|
||||
var logs []json.RawMessage
|
||||
_ = json.Unmarshal(m["logs"], &logs)
|
||||
declared := logsSection(logs)
|
||||
for name, bounds := range map[string]map[string]float64{"usage": {"max-age-days": 35, "max-mib": 256},
|
||||
"audit": {"max-mib": 64}} {
|
||||
l, ok := declared[name]
|
||||
if !ok {
|
||||
t.Errorf("the log %s is not in the logs section", name)
|
||||
continue
|
||||
}
|
||||
for k, v := range bounds {
|
||||
if l[k] != v {
|
||||
t.Errorf("the log %s has %s %v, want %v", name, k, l[k], v)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// issued is what the manager's own runtime is issued from its manifest (design 39 bus-and-switch C1): its account, the
|
||||
// states it keeps (`requires` by template), the logs it keeps (the `logs` section, the form the runtime on main issues,
|
||||
// C3; a log asked for by template in `requires` is issued nothing), what it may ask (`invokes`) and say (`emits`).
|
||||
type issued struct {
|
||||
RunsAs, SecretsOwner string
|
||||
OwnSecrets map[string]string
|
||||
Resources []map[string]any
|
||||
States, Logs map[string]bool
|
||||
Invokes, Emits map[string]bool
|
||||
}
|
||||
|
||||
func manifestIssued(t testing.TB) issued {
|
||||
t.Helper()
|
||||
raw, err := os.ReadFile(filepath.Join(moduleDir, "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var m struct {
|
||||
RunsAs string `json:"runs-as"`
|
||||
SecretsOwner string `json:"secrets-owner"`
|
||||
OwnSecrets map[string]string `json:"own-secrets"`
|
||||
Requires []json.RawMessage `json:"requires"`
|
||||
Logs []json.RawMessage `json:"logs"`
|
||||
Resources []map[string]any `json:"resources"`
|
||||
Invokes []string `json:"invokes"`
|
||||
Emits []string `json:"emits"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
out := issued{RunsAs: m.RunsAs, SecretsOwner: m.SecretsOwner, OwnSecrets: m.OwnSecrets, Resources: m.Resources,
|
||||
States: map[string]bool{}, Logs: map[string]bool{}, Invokes: map[string]bool{}, Emits: map[string]bool{}}
|
||||
for _, r := range m.Requires {
|
||||
var e struct{ Name, Storage string }
|
||||
if json.Unmarshal(r, &e) != nil {
|
||||
continue
|
||||
}
|
||||
if e.Storage == "key-value" {
|
||||
out.States[e.Name] = true
|
||||
}
|
||||
}
|
||||
for name := range logsSection(m.Logs) {
|
||||
out.Logs[name] = true
|
||||
}
|
||||
for _, k := range m.Invokes {
|
||||
out.Invokes[k] = true
|
||||
}
|
||||
for _, e := range m.Emits {
|
||||
out.Emits[e] = true
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// logsSection is the manifest's `logs` section by name: each a name alone or {name, max-age-days, max-mib}.
|
||||
func logsSection(logs []json.RawMessage) map[string]map[string]any {
|
||||
out := map[string]map[string]any{}
|
||||
for _, l := range logs {
|
||||
var name string
|
||||
if json.Unmarshal(l, &name) == nil {
|
||||
out[name] = map[string]any{}
|
||||
continue
|
||||
}
|
||||
var e map[string]any
|
||||
if json.Unmarshal(l, &e) == nil {
|
||||
if n, _ := e["name"].(string); n != "" {
|
||||
out[n] = e
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// mayAsk is whether the manager's own account may ask key: a module tool asked on one node (`<module>.<tool>@<node>`)
|
||||
// is invoked as `<module>.<tool>`.
|
||||
// emits is whether the manifest names the event key's family: the key itself, or a family it extends by further parts,
|
||||
// as binding.changed.<consumer> extends binding.changed (design 47 G A-134).
|
||||
func (i issued) emits(key string) bool {
|
||||
for k := key; k != ""; {
|
||||
if i.Emits[k] {
|
||||
return true
|
||||
}
|
||||
dot := strings.LastIndex(k, ".")
|
||||
if dot < 0 {
|
||||
break
|
||||
}
|
||||
k = k[:dot]
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (i issued) mayAsk(key string) bool {
|
||||
k, _, _ := strings.Cut(key, "@")
|
||||
return i.Invokes[k]
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch C1: the manager runs as an account of its own, which it makes and which owns what it places
|
||||
// on its machine and its secrets, reaching the bus as itself; and that account is issued every ask its code makes,
|
||||
// since its runtime is not the machine's tool runner.
|
||||
func TestTheManagerRunsAsItsOwnAccount(t *testing.T) {
|
||||
m := manifestIssued(t)
|
||||
if m.RunsAs != Publisher {
|
||||
t.Fatalf("the manager runs as %q, not as its own account %s", m.RunsAs, Publisher)
|
||||
}
|
||||
if m.SecretsOwner != Publisher || m.OwnSecrets["broker"] == "" {
|
||||
t.Errorf("its secrets belong to %q and its own broker secret is %q: both are its account's", m.SecretsOwner,
|
||||
m.OwnSecrets["broker"])
|
||||
}
|
||||
made := false
|
||||
for _, r := range m.Resources {
|
||||
switch r["type"] {
|
||||
case "user":
|
||||
made = made || r["name"] == Publisher
|
||||
case "directory", "file":
|
||||
if r["owner"] != Publisher {
|
||||
t.Errorf("resource %v is owned by %v, not by the manager's account", r["id"], r["owner"])
|
||||
}
|
||||
}
|
||||
}
|
||||
if !made {
|
||||
t.Errorf("the manager does not make its account: no user resource named %s", Publisher)
|
||||
}
|
||||
files, _ := filepath.Glob("../*/*.go")
|
||||
shared, _ := filepath.Glob("../../internal/*/*.go")
|
||||
files = append(files, shared...)
|
||||
asked := regexp.MustCompile(`stdio\.Ask\("([^"]+)"`)
|
||||
for _, f := range files {
|
||||
if strings.HasSuffix(f, "_test.go") {
|
||||
continue
|
||||
}
|
||||
src, err := os.ReadFile(f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, k := range asked.FindAllStringSubmatch(string(src), -1) {
|
||||
if !m.mayAsk(k[1]) {
|
||||
t.Errorf("%s asks %s, which the manifest's invokes do not name", f, k[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 parts §9, §10: the carry-over has left the module whole. Its manifest requires neither the store database
|
||||
// nor the secret grant-key, contributes and binds nothing and has no secrets section; no word of its code names the
|
||||
// database's address or grant.key; its go.mod requires no postgres driver; the tool licences_carried is gone, and the
|
||||
// state carried stays as the carry-over's record.
|
||||
func TestTheCarryOverHasLeft(t *testing.T) {
|
||||
raw, err := os.ReadFile(filepath.Join(moduleDir, "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var m map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, gone := range []string{"contributes", "binds", "secrets"} {
|
||||
if _, ok := m[gone]; ok {
|
||||
t.Errorf("the manifest keeps %q", gone)
|
||||
}
|
||||
}
|
||||
var requires []json.RawMessage
|
||||
_ = json.Unmarshal(m["requires"], &requires)
|
||||
carried := false
|
||||
for _, r := range requires {
|
||||
var bare string
|
||||
if json.Unmarshal(r, &bare) == nil {
|
||||
t.Errorf("the manifest requires %q", bare)
|
||||
}
|
||||
var e struct{ Name string }
|
||||
if json.Unmarshal(r, &e) == nil && e.Name == "carried" {
|
||||
carried = true
|
||||
}
|
||||
}
|
||||
if !carried {
|
||||
t.Error("the state carried, the carry-over's record, is gone")
|
||||
}
|
||||
var tools []string
|
||||
_ = json.Unmarshal(m["tools"], &tools)
|
||||
if slices.Contains(tools, "licences_carried") {
|
||||
t.Error("the manifest keeps the tool licences_carried")
|
||||
}
|
||||
var resources []struct{ ID string }
|
||||
_ = json.Unmarshal(m["resources"], &resources)
|
||||
for _, r := range resources {
|
||||
if r.ID == "database-url" {
|
||||
t.Error("the manifest keeps the resource database-url")
|
||||
}
|
||||
}
|
||||
words := []string{"DATABASE_URL" + "_FILE", "MESH_LICENCE_KEY" + "_FILE"}
|
||||
err = filepath.WalkDir(moduleDir, func(path string, d os.DirEntry, err error) error {
|
||||
if err != nil || d.IsDir() {
|
||||
return err
|
||||
}
|
||||
switch filepath.Ext(path) {
|
||||
case ".go", ".json", ".mod", ".sum":
|
||||
default:
|
||||
if filepath.Base(filepath.Dir(path)) != "files" {
|
||||
return nil // a built binary is no code of the module
|
||||
}
|
||||
}
|
||||
src, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, w := range words {
|
||||
if strings.Contains(string(src), w) {
|
||||
t.Errorf("%s names %s", path, w)
|
||||
}
|
||||
}
|
||||
if filepath.Base(path) == "go.mod" && strings.Contains(string(src), "github.com/jackc/pgx") {
|
||||
t.Errorf("%s requires the postgres driver", path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 parts §1 to §4, §6 to §8: the module is four parts in one manifest. Its artifacts are exactly the manager
|
||||
// bundle, the tools bundle and the templates archive, with their words; its resources exactly the account, its
|
||||
// directory and the settings file, in that order; no process or container resource, no run-once step, no bus account
|
||||
// of its own and no runtime-health; its tools are the four reading ones and the three acting ones; files/ holds only
|
||||
// the settings template.
|
||||
func TestTheModuleIsFourParts(t *testing.T) {
|
||||
raw, err := os.ReadFile(filepath.Join(moduleDir, "module.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var m map[string]any
|
||||
if err := json.Unmarshal(raw, &m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
same := func(what string, got, want any) {
|
||||
t.Helper()
|
||||
g, _ := json.Marshal(got)
|
||||
w, _ := json.Marshal(want)
|
||||
if string(g) != string(w) {
|
||||
t.Errorf("%s is %s, want %s", what, g, w)
|
||||
}
|
||||
}
|
||||
build, _ := m["build"].(map[string]any)
|
||||
same("build.artifacts", build["artifacts"], []map[string]any{
|
||||
{"name": "manager", "kind": "bundle", "language": "go", "system": "arch", "from": "cmd/manager",
|
||||
"binary": "claude-licence-manager", "loads": []string{"claude-licence-manager"},
|
||||
"env": map[string]string{"MESH_LICENCE_STATE": "${dir:state}", "MESH_LICENCE_SETTINGS": "${dir:state}/settings.json"}},
|
||||
{"name": "tools", "kind": "bundle", "language": "go", "system": "arch", "from": "cmd/tools",
|
||||
"binary": "claude-licence-manager-tools", "loads": []string{"claude-licence-manager-tools"}},
|
||||
{"name": "files", "kind": "archive", "from": "files"},
|
||||
})
|
||||
same("resources", m["resources"], []map[string]any{
|
||||
{"id": "account", "type": "user", "name": Publisher, "shell": "/usr/bin/nologin", "home": "/var/lib/" + Publisher},
|
||||
{"id": "state", "type": "directory", "mode": "0700", "owner": Publisher, "place": ".", "unknown": "program"},
|
||||
{"id": "settings", "type": "file", "path": "${dir:state}/settings.json", "mode": "0600", "owner": Publisher,
|
||||
"takes-config": []string{"cadence_minutes", "floor_minutes", "failures_to_notify", "cooldown_hours",
|
||||
"refresh_warn_days", "switch_auto", "switch_at_pct", "room_below_pct", "warn_at_pct", "usage_minutes",
|
||||
"usage_stale_minutes"}, "merge": "json", "template": "files/settings.json"},
|
||||
})
|
||||
tools, _ := m["tools"].([]any)
|
||||
var names []string
|
||||
for _, n := range tools {
|
||||
names = append(names, fmt.Sprint(n))
|
||||
}
|
||||
sort.Strings(names)
|
||||
same("tools", names, []string{"licences_conditions", "licences_kept", "licences_read_usage", "licences_resync",
|
||||
"licences_settings", "licences_switch_now", "licences_switches"})
|
||||
same("runs-as", m["runs-as"], Publisher)
|
||||
same("secrets-owner", m["secrets-owner"], Publisher)
|
||||
same("own-secrets", m["own-secrets"], map[string]string{"broker": "${dir:state}/broker"})
|
||||
if _, ok := m["runtime-health"]; ok {
|
||||
t.Error("the manifest adds runtime-health: the node-engine's judgement that the runtime is alive is its health")
|
||||
}
|
||||
var walk func(path string, v any)
|
||||
walk = func(path string, v any) {
|
||||
switch v := v.(type) {
|
||||
case map[string]any:
|
||||
for k, e := range v {
|
||||
if k == "bus" || k == "run-once" {
|
||||
t.Errorf("the manifest says %s at %s", k, path)
|
||||
}
|
||||
if k == "type" && (e == "process" || e == "container") {
|
||||
t.Errorf("the manifest has a %v resource at %s", e, path)
|
||||
}
|
||||
walk(path+"."+k, e)
|
||||
}
|
||||
case []any:
|
||||
for i, e := range v {
|
||||
walk(fmt.Sprintf("%s[%d]", path, i), e)
|
||||
}
|
||||
}
|
||||
}
|
||||
walk("", m)
|
||||
files, _ := os.ReadDir(filepath.Join(moduleDir, "files"))
|
||||
var templates []string
|
||||
for _, f := range files {
|
||||
templates = append(templates, f.Name())
|
||||
}
|
||||
same("files/", templates, []string{"settings.json"})
|
||||
}
|
||||
|
||||
// design 39 parts §2, §4: the manager bundle registers the seat's ten verbs under the seat and the three acting tools
|
||||
// under the module, and nothing more.
|
||||
func TestTheManagerRegistersTheVerbsAndTheActs(t *testing.T) {
|
||||
var names []string
|
||||
for _, tool := range tools() {
|
||||
names = append(names, tool.Name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
var want []string
|
||||
for _, v := range []string{"licences", "bindings", "bind", "switch", "release", "refresh", "usage", "adopt",
|
||||
"current", "public-key"} {
|
||||
want = append(want, Seat+"."+v)
|
||||
}
|
||||
want = append(want, "licences_read_usage", "licences_resync", "licences_switch_now")
|
||||
sort.Strings(want)
|
||||
if !slices.Equal(names, want) {
|
||||
t.Fatalf("the manager bundle registers %v, not %v", names, want)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
// A licence's room (design 39 bus-and-switch §25, §48, C4): 100 less its weekly percentage in its latest reading, the
|
||||
// session window taking no part (the vendor retired the session limit), known only for a subscription read successfully within usage_stale_minutes, whose refresh has not
|
||||
// failed failures_to_notify times in a row and whose refresh token has not expired. An API key's room is never known,
|
||||
// so it is never a target.
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// Room is a licence's room at now, and whether it is known.
|
||||
func Room(l Licence, now time.Time, s Settings) (float64, bool) {
|
||||
if l.Kind != "subscription" || l.Usage == nil || l.Usage.WeeklyPct == nil {
|
||||
return 0, false
|
||||
}
|
||||
read, err := time.Parse(time.RFC3339Nano, l.Usage.ReadAt)
|
||||
if err != nil || now.Sub(read) > s.UsageStale {
|
||||
return 0, false
|
||||
}
|
||||
if l.Failures >= s.FailuresToNotify || (l.RefreshExpiresAt != 0 && l.RefreshExpiresAt <= now.UnixMilli()) {
|
||||
return 0, false
|
||||
}
|
||||
return 100 - *l.Usage.WeeklyPct, true
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// vendorUsage is the vendor's usage answer as it reads: three windows, each with its reset time.
|
||||
var vendorUsage = map[string]any{
|
||||
"five_hour": map[string]any{"utilization": 41.0, "resets_at": "2026-11-11T23:00:00Z"},
|
||||
"seven_day": map[string]any{"utilization": 97.0, "resets_at": "2026-11-14T09:00:00Z"},
|
||||
"seven_day_sonnet": map[string]any{"utilization": 12.0, "resets_at": "2026-11-15T09:00:00Z"},
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §24/AC1: a vendor answer is flattened with its three reset times, kept beside the licence
|
||||
// with when it was read, and said as usage.read with those fields.
|
||||
func TestAReadingIsFlattenedWithThreeResets(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
|
||||
mm.vendor.usage = vendorUsage
|
||||
mm.events = nil
|
||||
if err := mm.m.ReadUsage(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
l, _ := mm.store.Licence(ctx, licence1)
|
||||
u := l.Usage
|
||||
if u == nil || *u.SessionPct != 41 || u.SessionResetsAt != "2026-11-11T23:00:00Z" || *u.WeeklyPct != 97 ||
|
||||
u.WeeklyResetsAt != "2026-11-14T09:00:00Z" || *u.SonnetPct != 12 || u.SonnetResetsAt != "2026-11-15T09:00:00Z" ||
|
||||
u.ReadAt != t0.UTC().Format(time.RFC3339Nano) || u.ReadError != "" {
|
||||
t.Fatalf("kept %+v", u)
|
||||
}
|
||||
said := strings.Join(mm.events, "\n")
|
||||
for _, want := range []string{`"weeklyResetsAt":"2026-11-14T09:00:00Z"`, `"sonnetResetsAt":"2026-11-15T09:00:00Z"`,
|
||||
`"sessionResetsAt":"2026-11-11T23:00:00Z"`} {
|
||||
if !strings.Contains(said, "usage.read ") || !strings.Contains(said, want) {
|
||||
t.Fatalf("said %s, lacking %s", said, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §24/AC2: a failed answer keeps the last percentages and their time, writes the vendor's
|
||||
// status and reason, and appends no reading.
|
||||
func TestAFailedReadingKeepsThePercentages(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
|
||||
mm.vendor.usage = vendorUsage
|
||||
_ = mm.m.ReadUsage(ctx)
|
||||
mm.now = t0.Add(5 * time.Minute)
|
||||
mm.vendor.usageErr = errors.New(`the usage endpoint answered 429: {"error":"rate_limited"}`)
|
||||
if err := mm.m.ReadUsage(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
l, _ := mm.store.Licence(ctx, licence1)
|
||||
u := l.Usage
|
||||
if u == nil || *u.WeeklyPct != 97 || u.ReadAt != t0.UTC().Format(time.RFC3339Nano) ||
|
||||
u.ReadError != `the usage endpoint answered 429: {"error":"rate_limited"}` {
|
||||
t.Fatalf("kept %+v", u)
|
||||
}
|
||||
if n := len(mm.usage.entries); n != 1 {
|
||||
t.Fatalf("a failed reading appended: %d entries", n)
|
||||
}
|
||||
// §16, §31: a grant the vault does not answer is a failed reading, written and gone past, never the end of the round.
|
||||
mm.vendor.usageErr = nil
|
||||
mm.grants.mu.Lock()
|
||||
delete(mm.grants.held, SecretName(KeyFor(licence1), PartGrant))
|
||||
mm.grants.mu.Unlock()
|
||||
if err := mm.m.ReadUsage(ctx); err != nil {
|
||||
t.Fatalf("a grant the vault did not answer ended the round: %v", err)
|
||||
}
|
||||
l, _ = mm.store.Licence(ctx, licence1)
|
||||
if !strings.Contains(l.Usage.ReadError, "cannot be read from the vault") || *l.Usage.WeeklyPct != 97 {
|
||||
t.Fatalf("after the vault's refusal the reading is %+v", l.Usage)
|
||||
}
|
||||
// §4, §16: a reading is taken under the licence's lease; with another run holding it, nothing is read or written.
|
||||
if ok, _ := mm.store.Lease(ctx, "licence."+KeyFor(licence1), "another run", time.Minute); !ok {
|
||||
t.Fatal("the lease was not taken")
|
||||
}
|
||||
reads, err := mm.m.ReadUsageOf(ctx, licence1, true)
|
||||
if err != nil || len(reads) != 1 || reads[0].Error != "another run holds its lease" {
|
||||
t.Fatalf("a reading under another's lease answered %+v, %v", reads, err)
|
||||
}
|
||||
if again, _ := mm.store.Licence(ctx, licence1); again.Usage.ReadError != l.Usage.ReadError {
|
||||
t.Fatalf("a reading under another's lease wrote %+v", again.Usage)
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §25/AC1, §48, C4: a table of readings, ages and healths and the room each gives.
|
||||
func TestTheRoomOfEachLicence(t *testing.T) {
|
||||
now := t0
|
||||
pct := func(v float64) *float64 { return &v }
|
||||
read := func(session, weekly float64, ago time.Duration) *UsageReading {
|
||||
return &UsageReading{SessionPct: pct(session), WeeklyPct: pct(weekly), ReadAt: now.Add(-ago).Format(time.RFC3339Nano)}
|
||||
}
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
l Licence
|
||||
room float64
|
||||
known bool
|
||||
}{
|
||||
{"the weekly window counts", Licence{Kind: "subscription", Usage: read(41, 97, time.Minute)}, 3, true},
|
||||
{"the session takes no part (C4)", Licence{Kind: "subscription", Usage: read(80, 20, time.Minute)}, 80, true},
|
||||
{"no session window, weekly 33 (C4)", Licence{Kind: "subscription", Usage: &UsageReading{WeeklyPct: pct(33),
|
||||
ReadAt: now.Format(time.RFC3339Nano)}}, 67, true},
|
||||
{"read at the stale bound", Licence{Kind: "subscription", Usage: read(10, 10, 15*time.Minute)}, 90, true},
|
||||
{"read before the stale bound", Licence{Kind: "subscription", Usage: read(10, 10, 16*time.Minute)}, 0, false},
|
||||
{"a failure since keeps the room", Licence{Kind: "subscription", Usage: func() *UsageReading {
|
||||
u := read(10, 30, 5*time.Minute)
|
||||
u.ReadError = "the usage endpoint answered 500: x"
|
||||
return u
|
||||
}()}, 70, true},
|
||||
{"never read", Licence{Kind: "subscription"}, 0, false},
|
||||
{"no weekly window", Licence{Kind: "subscription", Usage: &UsageReading{SessionPct: pct(10),
|
||||
ReadAt: now.Format(time.RFC3339Nano)}}, 0, false},
|
||||
{"refresh failing", Licence{Kind: "subscription", Failures: 3, Usage: read(10, 10, time.Minute)}, 0, false},
|
||||
{"refresh failed twice", Licence{Kind: "subscription", Failures: 2, Usage: read(10, 10, time.Minute)}, 90, true},
|
||||
{"refresh token expired", Licence{Kind: "subscription", RefreshExpiresAt: now.UnixMilli(),
|
||||
Usage: read(10, 10, time.Minute)}, 0, false},
|
||||
{"an API key", Licence{Kind: "api-key", Usage: read(0, 0, 0)}, 0, false},
|
||||
} {
|
||||
room, known := Room(c.l, now, Defaults)
|
||||
if room != c.room || known != c.known {
|
||||
t.Errorf("%s: room %v known %v, want %v %v", c.name, room, known, c.room, c.known)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A box the agent module's TypeScript sealed opens here: the two implementations are one format.
|
||||
func TestABoxSealedInTypeScriptOpensInGo(t *testing.T) {
|
||||
raw, err := os.ReadFile("testdata/sealed-by-typescript.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var f struct {
|
||||
PrivateKey string `json:"privateKey"`
|
||||
Box SealedBox `json:"box"`
|
||||
Plaintext string `json:"plaintext"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &f); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := Open(f.Box, f.PrivateKey)
|
||||
if err != nil || got != f.Plaintext {
|
||||
t.Fatalf("opened %q, %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
// What Go seals opens with its own key and no other.
|
||||
func TestABoxOpensOnlyForItsRecipient(t *testing.T) {
|
||||
a, _ := GenerateKeyPair()
|
||||
b, _ := GenerateKeyPair()
|
||||
box, err := Seal("a token", a.PublicKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := Open(box, a.PrivateKey); err != nil || got != "a token" {
|
||||
t.Fatalf("opened %q, %v", got, err)
|
||||
}
|
||||
if _, err := Open(box, b.PrivateKey); err == nil {
|
||||
t.Fatal("a box opened for another key")
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch C2: a key pair the manager finds and cannot read (left by another account, here mode 000) is
|
||||
// replaced in its place by a new one, said in its journal; the new one is read back by the next start.
|
||||
func TestAKeyPairItCannotReadIsMadeAnew(t *testing.T) {
|
||||
if os.Geteuid() == 0 {
|
||||
t.Fatal("run as root, a file of mode 000 is still read: this test needs an ordinary account")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
file := filepath.Join(dir, "manager-key.json")
|
||||
if err := os.WriteFile(file, []byte(`{"publicKey":"old","privateKey":"old"}`), 0o000); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var said []string
|
||||
log := func(format string, a ...any) { said = append(said, fmt.Sprintf(format, a...)) }
|
||||
k, err := keypair(dir, log)
|
||||
if err != nil {
|
||||
t.Fatalf("a key pair it cannot read stops the manager: %v", err)
|
||||
}
|
||||
if k.PublicKey == "" || k.PublicKey == "old" || len(said) != 1 || !strings.Contains(said[0], "manager-key.json") {
|
||||
t.Fatalf("not made anew and said: key %q, journal %q", k.PublicKey, said)
|
||||
}
|
||||
again, err := keypair(dir, log)
|
||||
if err != nil || again != k || len(said) != 1 {
|
||||
t.Fatalf("the new pair is not the one read at the next start: %v, %v, %q", again == k, err, said)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
// The manager's settings (design 39 §8, design 39 bus-and-switch §33): today's five and the six of the switch, each
|
||||
// declared in the manifest's `config` with its default and bound, set through mesh-config and written into the
|
||||
// settings file. They are read again at every reading, so a change takes effect at the next one, and each change of a
|
||||
// value read is written in the audit under `settings`.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// settings are those in force.
|
||||
func (m *Manager) settings() Settings {
|
||||
m.settingsMu.RLock()
|
||||
defer m.settingsMu.RUnlock()
|
||||
return m.Settings
|
||||
}
|
||||
|
||||
// ReadSettings reads the settings file again, at every reading (§33): each value that changed is written in the audit
|
||||
// under `settings`, its value before and now, and the settings read are those in force from then on.
|
||||
func (m *Manager) ReadSettings(ctx context.Context) error {
|
||||
if m.SettingsFile == "" {
|
||||
return nil
|
||||
}
|
||||
next := SettingsFrom(m.SettingsFile)
|
||||
m.settingsMu.Lock()
|
||||
before := m.Settings
|
||||
m.Settings = next
|
||||
m.settingsMu.Unlock()
|
||||
was, now := before.Values(), next.Values()
|
||||
changed := map[string]any{}
|
||||
for k, v := range now {
|
||||
if !reflect.DeepEqual(was[k], v) {
|
||||
changed[k] = map[string]any{"from": was[k], "to": v}
|
||||
}
|
||||
}
|
||||
if len(changed) == 0 {
|
||||
return nil
|
||||
}
|
||||
return m.record(ctx, "settings", fmt.Sprintf("%d setting(s) changed: %v", len(changed), changed), changed)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// design 39 bus-and-switch §33: the settings are read again at every reading, and each change of a value read is
|
||||
// written once in the audit under settings.
|
||||
func TestASettingChangedIsReadAtTheNextReadingAndAudited(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
mm.m.SettingsFile = filepath.Join(t.TempDir(), "settings.json")
|
||||
_ = os.WriteFile(mm.m.SettingsFile, []byte(`{"switch_at_pct": 99}`), 0o600)
|
||||
if err := mm.m.ReadUsage(ctx); err != nil || len(mm.audit.under("settings")) != 0 {
|
||||
t.Fatalf("an unchanged file audited %v (%v)", mm.audit.under("settings"), err)
|
||||
}
|
||||
_ = os.WriteFile(mm.m.SettingsFile, []byte(`{"switch_at_pct": 95, "usage_minutes": 10}`), 0o600)
|
||||
_ = mm.m.ReadUsage(ctx)
|
||||
_ = mm.m.ReadUsage(ctx)
|
||||
if s := mm.m.settings(); s.SwitchAt != 95 || s.UsageEvery.Minutes() != 10 {
|
||||
t.Fatalf("in force %+v", s)
|
||||
}
|
||||
got := mm.audit.under("settings")
|
||||
if len(got) != 1 {
|
||||
t.Fatalf("audited %v", got)
|
||||
}
|
||||
if c, _ := got[0]["switch_at_pct"].(map[string]any); c["from"] != 99.0 || c["to"] != 95.0 {
|
||||
t.Fatalf("audited %v", got[0])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package main
|
||||
|
||||
// The manager's store (novox/hq ADR 0183, design 39 bus-and-switch §2 to §7): licences, bindings with a generation,
|
||||
// leases, what became of each login it was offered, usage readings and the audit, all in the bus's own storage
|
||||
// (busstore.go). Every rule is tested on the same store over a bus kept in memory.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// Binding is one consumer's binding (ADR 0206, §3): the licence, kind and generation it should hold, since when, and
|
||||
// why and by whom it was moved last.
|
||||
type Binding struct {
|
||||
Consumer string `json:"consumer"`
|
||||
Licence string `json:"licence"`
|
||||
// Key is the licence's key (§10), answered by the verb bindings (B25); the store keeps none.
|
||||
Key string `json:"key,omitempty"`
|
||||
Kind string `json:"kind,omitempty"`
|
||||
Generation int64 `json:"generation"`
|
||||
Since string `json:"since,omitempty"`
|
||||
Why string `json:"why,omitempty"`
|
||||
By string `json:"by,omitempty"`
|
||||
}
|
||||
|
||||
// Move is what moves a binding: the licence's kind, why and by whom, and the floor a consumer's generation must pass
|
||||
// (B11: the generation its node last reported); a nil Floor is none.
|
||||
type Move struct {
|
||||
Kind, Why, By string
|
||||
Floor func(consumer string) int64
|
||||
}
|
||||
|
||||
// floor is the floor m gives consumer.
|
||||
func (m Move) floor(consumer string) int64 {
|
||||
if m.Floor == nil {
|
||||
return 0
|
||||
}
|
||||
return m.Floor(consumer)
|
||||
}
|
||||
|
||||
// UsageRow is one usage reading.
|
||||
type UsageRow struct {
|
||||
Licence string `json:"licence"`
|
||||
At int64 `json:"at"`
|
||||
Reading UsageReading `json:"reading"`
|
||||
// Raw is the vendor's answer whole, answered only when asked (§35).
|
||||
Raw map[string]any `json:"raw,omitempty"`
|
||||
}
|
||||
|
||||
// Store is what the manager keeps.
|
||||
type Store interface {
|
||||
Licences(ctx context.Context) ([]Licence, error)
|
||||
Licence(ctx context.Context, name string) (*Licence, error)
|
||||
LicenceForAccount(ctx context.Context, account string) (*Licence, error)
|
||||
SaveLicence(ctx context.Context, l Licence) error
|
||||
Bindings(ctx context.Context) ([]Binding, error)
|
||||
Binding(ctx context.Context, consumer string) (*Binding, error)
|
||||
// Bind binds (or switches) a consumer at its next generation.
|
||||
Bind(ctx context.Context, consumer, licence string, m Move) (Binding, error)
|
||||
Unbind(ctx context.Context, consumer string) (bool, error)
|
||||
// Advance gives every consumer of a licence its next generation: what a rotation is to them.
|
||||
Advance(ctx context.Context, licence string, m Move) ([]Binding, error)
|
||||
// Lease takes a lease for d, or answers false while another holder's is live.
|
||||
Lease(ctx context.Context, key, holder string, d time.Duration) (bool, error)
|
||||
Unlease(ctx context.Context, key, holder string) error
|
||||
Outcome(ctx context.Context, fingerprint string) (Outcome, error)
|
||||
RecordOutcome(ctx context.Context, fingerprint, node, account string, o Outcome, why string) error
|
||||
RecordUsage(ctx context.Context, licence string, at int64, r UsageReading, raw map[string]any) error
|
||||
// Usage is the latest readings, newest first, of one licence or every one: at most limit, none before since (zero
|
||||
// is none), the vendor's answer with raw (§35).
|
||||
Usage(ctx context.Context, licence string, since time.Time, limit int, raw bool) ([]UsageRow, error)
|
||||
// Audit appends one entry for an act; an error is the act failing (B20).
|
||||
Audit(ctx context.Context, what string, detail map[string]any) error
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestARefreshThatDoesNotRotateKeepsTheRefreshToken(t *testing.T) {
|
||||
prev := FullGrant{AccessToken: "a", RefreshToken: "r", ExpiresAt: 1}
|
||||
in := int64(60)
|
||||
kept := NextGrant(prev, tokenResponse{AccessToken: "a2", ExpiresIn: &in}, 1000)
|
||||
if !kept.OK || kept.Grant.RefreshToken != "r" || kept.Grant.ExpiresAt != 61_000 {
|
||||
t.Fatalf("%+v", kept)
|
||||
}
|
||||
rotated := NextGrant(prev, tokenResponse{AccessToken: "a3", RefreshToken: "r2"}, 0)
|
||||
if rotated.Grant.RefreshToken != "r2" {
|
||||
t.Fatalf("%+v", rotated)
|
||||
}
|
||||
if NextGrant(prev, tokenResponse{}, 0).OK {
|
||||
t.Fatal("an answer with no access token was a grant")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
package main
|
||||
|
||||
// The switch rule (design 39 bus-and-switch §26, §27, §28, §32): after a reading of licence L, when switch_auto is on and
|
||||
// L, with one or more consumers bound to it, is at or above switch_at_pct in its weekly window (C4: the session window takes
|
||||
// no part), every consumer bound to L moves, in one act, to the other licence with known room whose weekly window is
|
||||
// below room_below_pct and that has the most room, the first by name on a tie. The act is said as one licence.switched, a
|
||||
// binding.changed per consumer, one audit entry switched, and told to the operator. A consumer is only ever moved off
|
||||
// a licence at its threshold, so none flaps between two.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// ByUsage is who moves a consumer by the rule (§26).
|
||||
const ByUsage = "usage"
|
||||
|
||||
// Switch is one act of the rule: from L to the target, the consumers it moves, the window of L that reached its
|
||||
// threshold, its percentage and reset, and the target's room.
|
||||
type Switch struct {
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
Consumers []string `json:"consumers"`
|
||||
Window string `json:"window"`
|
||||
Pct float64 `json:"pct"`
|
||||
ResetsAt string `json:"resetsAt"`
|
||||
Room float64 `json:"room"`
|
||||
}
|
||||
|
||||
// why is the cause a moved binding carries: L, the window, its percentage and its reset (§26).
|
||||
func (s Switch) why() string {
|
||||
return fmt.Sprintf("%s reached %g %% of its %s window, which resets at %s", s.From, s.Pct, s.Window, s.ResetsAt)
|
||||
}
|
||||
|
||||
// fields are the act's fields, the same in licence.switched and the audit entry switched (§27).
|
||||
func (s Switch) fields() map[string]any {
|
||||
f := switchedFields(s.From, s.To, s.Consumers, Cause{Why: s.why(), By: ByUsage})
|
||||
f["window"], f["pct"], f["resetsAt"], f["room"] = s.Window, s.Pct, s.ResetsAt, s.Room
|
||||
return f
|
||||
}
|
||||
|
||||
// Rule is what the rule finds for one licence at its threshold: the act, or none with NoTarget when no other licence
|
||||
// has room; Over is then the licence's consumers and the window at its threshold, for the condition no-room (§29).
|
||||
type Rule struct {
|
||||
Switch *Switch
|
||||
NoTarget bool
|
||||
Over *Switch
|
||||
}
|
||||
|
||||
// consumersOf are the consumers bound to licence, sorted.
|
||||
func consumersOf(licence string, bindings []Binding) []string {
|
||||
var consumers []string
|
||||
for _, b := range bindings {
|
||||
if b.Licence == licence {
|
||||
consumers = append(consumers, b.Consumer)
|
||||
}
|
||||
}
|
||||
sort.Strings(consumers)
|
||||
return consumers
|
||||
}
|
||||
|
||||
// windowAt is l's weekly window when it is at or above pct (C4: the session window takes no part): its name,
|
||||
// percentage and reset; ok false when it is not.
|
||||
func windowAt(u *UsageReading, pct float64) (window string, at float64, resetsAt string, ok bool) {
|
||||
if u == nil || u.WeeklyPct == nil || *u.WeeklyPct < pct {
|
||||
return "", 0, "", false
|
||||
}
|
||||
return "weekly", *u.WeeklyPct, u.WeeklyResetsAt, true
|
||||
}
|
||||
|
||||
// targetFor is the licence §26 moves l's consumers to: among the others with known room whose weekly window is below
|
||||
// room_below_pct (C4), the one with the most room, the first by name on a tie; empty when none has room.
|
||||
func (m *Manager) targetFor(l Licence, licences []Licence, s Settings) (string, float64) {
|
||||
others := append([]Licence(nil), licences...)
|
||||
sort.Slice(others, func(i, j int) bool { return others[i].Name < others[j].Name })
|
||||
to, best := "", -1.0
|
||||
for _, o := range others {
|
||||
if o.Name == l.Name {
|
||||
continue
|
||||
}
|
||||
room, known := Room(o, m.Now(), s)
|
||||
if !known || *o.Usage.WeeklyPct >= s.RoomBelow || room <= best {
|
||||
continue
|
||||
}
|
||||
best, to = room, o.Name
|
||||
}
|
||||
return to, best
|
||||
}
|
||||
|
||||
// RuleFor is what §26 gives for licence l among every licence and binding, under the settings s. A licence below its
|
||||
// threshold, or with no consumer, gives nothing.
|
||||
func (m *Manager) RuleFor(l Licence, licences []Licence, bindings []Binding, s Settings) Rule {
|
||||
consumers := consumersOf(l.Name, bindings)
|
||||
if len(consumers) == 0 {
|
||||
return Rule{}
|
||||
}
|
||||
sw := Switch{From: l.Name, Consumers: consumers}
|
||||
var ok bool
|
||||
if sw.Window, sw.Pct, sw.ResetsAt, ok = windowAt(l.Usage, s.SwitchAt); !ok {
|
||||
return Rule{}
|
||||
}
|
||||
if sw.To, sw.Room = m.targetFor(l, licences, s); sw.To == "" {
|
||||
sw.Room = 0
|
||||
return Rule{NoTarget: true, Over: &sw}
|
||||
}
|
||||
return Rule{Switch: &sw}
|
||||
}
|
||||
|
||||
// ApplyRule applies §26 to one licence, or to every one when licence is empty: after a reading, by switch_auto, or by
|
||||
// hand, even with switch_auto off (§32, §39). With dry it answers the moves and moves nothing. An invalid pair of
|
||||
// settings moves no consumer (§33).
|
||||
func (m *Manager) ApplyRule(ctx context.Context, licence string, byHand, dry bool) ([]Switch, error) {
|
||||
s := m.settings()
|
||||
if (!s.SwitchAuto && !byHand) || len(s.Invalid()) > 0 {
|
||||
return nil, nil
|
||||
}
|
||||
licences, err := m.Store.Licences(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bindings, err := m.Store.Bindings(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var acts []Switch
|
||||
for _, l := range licences {
|
||||
if licence != "" && l.Name != licence {
|
||||
continue
|
||||
}
|
||||
r := m.RuleFor(l, licences, bindings, s)
|
||||
if r.Switch == nil {
|
||||
continue
|
||||
}
|
||||
acts = append(acts, *r.Switch)
|
||||
if dry {
|
||||
continue
|
||||
}
|
||||
if err := m.act(ctx, *r.Switch, licences); err != nil {
|
||||
return acts, err
|
||||
}
|
||||
// What was moved is bound to the target now, for the next licence's rule.
|
||||
if bindings, err = m.Store.Bindings(ctx); err != nil {
|
||||
return acts, err
|
||||
}
|
||||
}
|
||||
return acts, nil
|
||||
}
|
||||
|
||||
// act moves every consumer of one act as the switch verb does, then says the act once and tells the operator (§26 to
|
||||
// §28).
|
||||
func (m *Manager) act(ctx context.Context, sw Switch, licences []Licence) error {
|
||||
kind := ""
|
||||
for _, l := range licences {
|
||||
if l.Name == sw.To {
|
||||
kind = l.Kind
|
||||
}
|
||||
}
|
||||
floor := m.floorIn(nil)
|
||||
for _, consumer := range sw.Consumers {
|
||||
b, err := m.Store.Bind(ctx, consumer, sw.To, Move{Kind: kind, Why: sw.why(), By: ByUsage, Floor: floor})
|
||||
if err != nil {
|
||||
return fmt.Errorf("moving %s to %s: %w", consumer, sw.To, err)
|
||||
}
|
||||
m.changed(b, sw.From)
|
||||
}
|
||||
f := sw.fields()
|
||||
if err := m.record(ctx, "switched", fmt.Sprintf("switched %s from %s to %s: %s; by %s",
|
||||
strings.Join(sw.Consumers, ", "), sw.From, sw.To, sw.why(), ByUsage), f); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = m.Emit("licence.switched", f)
|
||||
if m.Tell != nil {
|
||||
line := fmt.Sprintf("%s; %s has %g %% room", sw.why(), sw.To, sw.Room)
|
||||
if err := m.Tell(ctx, strings.Join(sw.Consumers, ", ")+" moved to "+sw.To, []string{line}); err != nil {
|
||||
m.Log("the switch of %s to %s could not be told: %v", strings.Join(sw.Consumers, ", "), sw.To, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
. "claude-licence-manager/internal/licences"
|
||||
)
|
||||
|
||||
// read is a licence's latest reading, read at t0.
|
||||
func read(session, weekly float64) *UsageReading {
|
||||
return &UsageReading{SessionPct: &session, SessionResetsAt: "2026-11-11T23:00:00Z", WeeklyPct: &weekly,
|
||||
WeeklyResetsAt: "2026-11-14T09:00:00Z", ReadAt: t0.UTC().Format(time.RFC3339Nano)}
|
||||
}
|
||||
|
||||
// weekly is a reading with no session window, as the vendor answers since the session limit was retired (C4).
|
||||
func weekly(pct float64) *UsageReading {
|
||||
u := read(0, pct)
|
||||
u.SessionPct, u.SessionResetsAt = nil, ""
|
||||
return u
|
||||
}
|
||||
|
||||
func sub(name string, u *UsageReading) Licence {
|
||||
return Licence{Name: name, Kind: "subscription", Generation: 1, Usage: u}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §26/AC1, §25, §48, C4: a table of licences, readings and bindings and the moves each gives.
|
||||
func TestTheRuleMovesEachTable(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
on := func(consumers ...string) []Binding {
|
||||
var out []Binding
|
||||
for _, c := range consumers {
|
||||
out = append(out, Binding{Consumer: c, Licence: "a"})
|
||||
}
|
||||
return out
|
||||
}
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
licences []Licence
|
||||
bindings []Binding
|
||||
to string
|
||||
window string
|
||||
noTarget bool
|
||||
consumers int
|
||||
}{
|
||||
{"below its threshold", []Licence{sub("a", read(50, 98)), sub("b", read(0, 0))}, on("x"), "", "", false, 0},
|
||||
{"no consumer", []Licence{sub("a", read(99, 99)), sub("b", read(0, 0))}, nil, "", "", false, 0},
|
||||
{"the weekly window at 99", []Licence{sub("a", read(40, 99)), sub("b", read(10, 30)), sub("c", read(5, 20))},
|
||||
on("x", "y", "z", "w"), "c", "weekly", false, 4},
|
||||
{"the session window at 100 moves nothing (C4)", []Licence{sub("a", read(100, 50)), sub("b", read(10, 30))},
|
||||
on("x"), "", "", false, 0},
|
||||
{"no session window, weekly 99, to weekly 33 (C4)", []Licence{sub("a", weekly(99)), sub("b", weekly(33))},
|
||||
on("x", "y"), "b", "weekly", false, 2},
|
||||
{"a tie goes to the first by name", []Licence{sub("a", read(0, 99)), sub("c", read(10, 10)), sub("b", read(10, 10))},
|
||||
on("x"), "b", "weekly", false, 1},
|
||||
{"a target's weekly below room_below_pct, its session not counted (C4)", []Licence{sub("a", read(0, 99)),
|
||||
sub("b", read(0, 90)), sub("c", read(95, 89))}, on("x"), "c", "weekly", false, 1},
|
||||
{"no other licence has room", []Licence{sub("a", read(0, 99)), sub("b", read(0, 95)),
|
||||
{Name: "k", Kind: "api-key", Generation: 1}}, on("x"), "", "", true, 0},
|
||||
{"an unread licence is no target", []Licence{sub("a", read(0, 99)), sub("b", nil)}, on("x"), "", "", true, 0},
|
||||
} {
|
||||
r := mm.m.RuleFor(c.licences[0], c.licences, c.bindings, Defaults)
|
||||
to, window, n := "", "", 0
|
||||
if r.Switch != nil {
|
||||
to, window, n = r.Switch.To, r.Switch.Window, len(r.Switch.Consumers)
|
||||
}
|
||||
if to != c.to || window != c.window || n != c.consumers || r.NoTarget != c.noTarget {
|
||||
t.Errorf("%s: to %q window %q consumers %d no target %v", c.name, to, window, n, r.NoTarget)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fourOnA is four consumers bound to a, at 99 % of its weekly window, b with 70 % room and c with 60 %.
|
||||
func fourOnA(t *testing.T) *miniMesh {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
for _, l := range []Licence{sub("a", read(40, 99)), sub("b", read(30, 30)), sub("c", read(40, 40))} {
|
||||
_ = mm.store.SaveLicence(ctx, l)
|
||||
}
|
||||
for _, c := range []string{"ace", "g14", "laptop", "server"} {
|
||||
_, _ = mm.store.Bind(ctx, c, "a", Move{Kind: "subscription", Why: "set", By: "test"})
|
||||
}
|
||||
mm.events = nil
|
||||
return mm
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §27/AC1, §26, §21: one act says one licence.switched with the consumers moved, the window,
|
||||
// its percentage, reset and the target's room; one binding.changed per consumer; one audit entry switched with the same
|
||||
// fields; each binding moved by usage, its why naming the licence, the window, the percentage and the reset.
|
||||
func TestOneSwitchedPerAct(t *testing.T) {
|
||||
mm := fourOnA(t)
|
||||
acts, err := mm.m.ApplyRule(context.Background(), "", false, false)
|
||||
if err != nil || len(acts) != 1 || acts[0].To != "b" || acts[0].Room != 70 {
|
||||
t.Fatalf("acts %+v %v", acts, err)
|
||||
}
|
||||
var switched []map[string]any
|
||||
changed := 0
|
||||
for _, e := range mm.events {
|
||||
name, body, _ := strings.Cut(e, " ")
|
||||
var f map[string]any
|
||||
_ = json.Unmarshal([]byte(body), &f)
|
||||
switch name {
|
||||
case "licence.switched":
|
||||
switched = append(switched, f)
|
||||
case "binding.changed." + fmt.Sprint(f["consumer"]):
|
||||
changed++
|
||||
}
|
||||
}
|
||||
if len(switched) != 1 || changed != 4 {
|
||||
t.Fatalf("said %v", mm.events)
|
||||
}
|
||||
s := switched[0]
|
||||
if s["from"] != "a" || s["to"] != "b" || len(s["consumers"].([]any)) != 4 || s["by"] != ByUsage ||
|
||||
s["window"] != "weekly" || s["pct"] != 99.0 || s["resetsAt"] != "2026-11-14T09:00:00Z" || s["room"] != 70.0 {
|
||||
t.Fatalf("licence.switched %v", s)
|
||||
}
|
||||
audit := mm.audit.under("switched")
|
||||
if len(audit) != 1 || audit[0]["window"] != "weekly" || len(audit[0]["consumers"].([]any)) != 4 {
|
||||
t.Fatalf("audit %v", audit)
|
||||
}
|
||||
b := mm.bound("laptop")
|
||||
if b.Licence != "b" || b.By != ByUsage || b.Why != "a reached 99 % of its weekly window, which resets at 2026-11-14T09:00:00Z" {
|
||||
t.Fatalf("laptop %+v", b)
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §28/AC1: the rule tells the operator once per act, and the switch verb never.
|
||||
func TestTheRuleTellsOnceAndTheVerbNever(t *testing.T) {
|
||||
mm := fourOnA(t)
|
||||
var told []string
|
||||
mm.m.Tell = func(_ context.Context, headline string, lines []string) error {
|
||||
told = append(told, headline+" | "+strings.Join(lines, " "))
|
||||
return nil
|
||||
}
|
||||
_, _ = mm.m.ApplyRule(context.Background(), "", false, false)
|
||||
if len(told) != 1 || told[0] != "ace, g14, laptop, server moved to b | a reached 99 % of its weekly window, "+
|
||||
"which resets at 2026-11-14T09:00:00Z; b has 70 % room" {
|
||||
t.Fatalf("told %v", told)
|
||||
}
|
||||
_, _ = mm.m.Bind(WithCause(context.Background(), Cause{Why: "the switch verb", By: "a person"}), "ace", "c")
|
||||
if len(told) != 1 {
|
||||
t.Fatalf("the switch verb told %v", told)
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §32/AC1, §33/AC2: dry answers the same moves as the act and moves nothing; an invalid pair
|
||||
// of settings, or switch_auto off, moves nothing after a reading, and the rule by hand runs with switch_auto off.
|
||||
func TestDryAnswersTheSameMovesAndMovesNothing(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
mm := fourOnA(t)
|
||||
dry, err := mm.m.ApplyRule(ctx, "a", true, true)
|
||||
if err != nil || len(dry) != 1 || mm.bound("ace").Licence != "a" || len(mm.events) != 0 {
|
||||
t.Fatalf("dry %+v %v, said %v", dry, err, mm.events)
|
||||
}
|
||||
acts, _ := mm.m.ApplyRule(ctx, "a", true, false)
|
||||
if len(acts) != 1 || acts[0].To != dry[0].To || strings.Join(acts[0].Consumers, ",") != strings.Join(dry[0].Consumers, ",") {
|
||||
t.Fatalf("dry %+v, act %+v", dry, acts)
|
||||
}
|
||||
for name, change := range map[string]func(*Settings){
|
||||
"an invalid pair": func(s *Settings) { s.RoomBelow = 100 },
|
||||
"switch_auto off": func(s *Settings) { s.SwitchAuto = false },
|
||||
} {
|
||||
mm := fourOnA(t)
|
||||
change(&mm.m.Settings)
|
||||
if acts, _ := mm.m.ApplyRule(ctx, "", false, false); len(acts) != 0 || mm.bound("ace").Licence != "a" {
|
||||
t.Errorf("%s moved %+v", name, acts)
|
||||
}
|
||||
}
|
||||
mm = fourOnA(t)
|
||||
mm.m.Settings.SwitchAuto = false
|
||||
if acts, _ := mm.m.ApplyRule(ctx, "", true, false); len(acts) != 1 || mm.bound("ace").Licence != "b" {
|
||||
t.Fatalf("by hand with switch_auto off moved %+v", acts)
|
||||
}
|
||||
}
|
||||
|
||||
// design 39 bus-and-switch §26: a reading at the threshold applies the rule at once.
|
||||
func TestAReadingAtTheThresholdApplies(t *testing.T) {
|
||||
mm := newMesh(t)
|
||||
ctx := context.Background()
|
||||
_, _ = mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)})
|
||||
_ = mm.store.SaveLicence(ctx, Licence{Name: "b@example.org", Kind: "subscription", Usage: read(10, 10)})
|
||||
mm.vendor.usage = map[string]any{"five_hour": map[string]any{"utilization": 30.0},
|
||||
"seven_day": map[string]any{"utilization": 99.0, "resets_at": "2026-11-14T09:00:00Z"}}
|
||||
if err := mm.m.ReadUsage(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if b := mm.bound("laptop"); b.Licence != "b@example.org" || b.By != ByUsage {
|
||||
t.Fatalf("laptop %+v", b)
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user