Commit bf71028
feat(069-A2): actor-owned usage aggregate + persistence (#560)
* feat(069-A2): actor-owned usage aggregate + persistence
Spec 069 Stream A2 (T005–T010): in-memory usage rollup owned by the
ActivityService goroutine, published to readers as a copy-on-write snapshot
via atomic pointer, persisted to BBolt with cold-start load-or-rebuild.
- UsageAggregate/ToolUsage/TimeBucket + incremental Apply (internal/runtime/usage_aggregate.go)
- UsageStore: atomic-pointer COW snapshot; lock-free, non-blocking reads
- ActivityService owns it: Apply on save in handleEvent, UsageSnapshot() reader,
periodic 30s flush + flush-on-shutdown, cold-start load-or-rebuild (one full scan)
- activity_stats BBolt bucket (versioned key) + byte-oriented persist/load + ScanAllActivities
- observability.usage_cache_ttl (5s) + usage_persist_interval (30s) config: defaults + hot-reload
- docs (configuration.md) + spec trace (tasks T005–T010, data-model A2 notes)
TDD: aggregate math, snapshot/reads-never-block, persistence round-trip,
cold-start rebuild vs load, and config defaults all covered. Full
internal/runtime -race suite green (approval-hash canary safe).
Related #745
Co-Authored-By: Paperclip <noreply@paperclip.ing>
* perf(069-A2): make UsageStore.Apply O(1) on the activity hot path
Apply previously cloned the entire usage aggregate (every tool + every
time bucket) on every activity write via publishLocked, making the
write hot path O(tools×buckets) instead of O(1) — violating spec 069
CN-002 ("aggregate update O(1) per activity write; must not block the
request hot path").
Decouple publish from write: Apply now mutates the working aggregate
under a short writer lock and only marks the snapshot stale (atomic
dirty flag, O(1), no clone). The clone is deferred to Snapshot
(publish-on-read): the first reader after a write burst materializes
one fresh snapshot off the hot path; reads with no pending writes stay
lock-free. The A3 endpoint and the 30s persist flush are the only
readers, so clones are rare relative to writes.
Test-first (ENG-1): TestUsageStore_ApplyDoesNotPublishPerWrite asserts
500 writes trigger zero publishes and exactly one clone on first read;
BenchmarkUsageStore_Apply shows 1 alloc/op with 1000 primed tools
(was O(tools) allocs/op). Existing snapshot/replace contract tests and
the full -race runtime suite stay green.
Related #560
Related MCP-835
* fix(069-A2): count blocked attempts + wire observability hot-reload
Addresses CodexReviewer findings #2 and #3 on PR #560 (the O(1) fix
cleared #1 once CI went green).
#2 — blocked tool attempts were missing from the usage aggregate. They
are persisted as blocked `policy_decision` records, but `Apply` dropped
all non-tool_call records and `handlePolicyDecision` never fed the
aggregate, so the contract's per-tool `blocked` field was permanently 0.
`Apply` now also folds blocked policy_decisions: a blocked attempt never
executed, so it increments only `Blocked` + `LastUsed` — not `Calls`,
latency, bytes, or the executed-call timeline. `handlePolicyDecision`
calls `usage.Apply` on save success so the live path matches a
cold-start rebuild-from-scan. Extracted a `tool()` get-or-create helper.
#3 — `observability.usage_persist_interval` claimed hot-reload but was
only read at construction. `DetectConfigChanges` now flags an
`observability` change and `ApplyConfig` pushes the new cadence into the
running ActivityService via `SetUsagePersistInterval` (the flush loop
already re-reads the interval each cycle).
Test-first (ENG-1): aggregate counts blocked-only (not Calls/latency/
timeline); live `handlePolicyDecision` folds blocked into the snapshot;
`DetectConfigChanges` detects observability as hot-reloadable; end-to-end
`ApplyConfig` applies the new interval to a running runtime. Repointed
the "ignores non-tool_calls" test to a non-blocked decision. Contract
documents `blocked` semantics. Full internal/runtime+config+storage
-race green; lint 0; personal+server builds.
Related #560
Related MCP-835
Co-Authored-By: Paperclip <noreply@paperclip.ing>
---------
Co-authored-by: Paperclip <noreply@paperclip.ing>1 parent ccbeb50 commit bf71028
21 files changed
Lines changed: 1406 additions & 14 deletions
File tree
- docs
- internal
- config
- runtime
- storage
- oas
- specs/069-observability-usage-graphs
- contracts
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
829 | 829 | | |
830 | 830 | | |
831 | 831 | | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
832 | 858 | | |
833 | 859 | | |
834 | 860 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
152 | 155 | | |
153 | 156 | | |
154 | 157 | | |
| |||
732 | 735 | | |
733 | 736 | | |
734 | 737 | | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
735 | 756 | | |
736 | 757 | | |
737 | 758 | | |
| |||
965 | 986 | | |
966 | 987 | | |
967 | 988 | | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
968 | 992 | | |
969 | 993 | | |
970 | 994 | | |
| |||
1351 | 1375 | | |
1352 | 1376 | | |
1353 | 1377 | | |
| 1378 | + | |
| 1379 | + | |
| 1380 | + | |
| 1381 | + | |
| 1382 | + | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
| 1389 | + | |
| 1390 | + | |
1354 | 1391 | | |
1355 | 1392 | | |
1356 | 1393 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
52 | 60 | | |
53 | 61 | | |
54 | 62 | | |
55 | 63 | | |
56 | | - | |
| 64 | + | |
57 | 65 | | |
58 | 66 | | |
59 | 67 | | |
| |||
62 | 70 | | |
63 | 71 | | |
64 | 72 | | |
| 73 | + | |
65 | 74 | | |
| 75 | + | |
| 76 | + | |
66 | 77 | | |
67 | 78 | | |
68 | 79 | | |
| |||
103 | 114 | | |
104 | 115 | | |
105 | 116 | | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
106 | 122 | | |
107 | 123 | | |
108 | 124 | | |
109 | 125 | | |
110 | 126 | | |
111 | 127 | | |
| 128 | + | |
| 129 | + | |
112 | 130 | | |
113 | 131 | | |
114 | 132 | | |
115 | 133 | | |
116 | 134 | | |
| 135 | + | |
117 | 136 | | |
118 | 137 | | |
119 | 138 | | |
| |||
303 | 322 | | |
304 | 323 | | |
305 | 324 | | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
306 | 332 | | |
307 | 333 | | |
308 | 334 | | |
| |||
336 | 362 | | |
337 | 363 | | |
338 | 364 | | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
339 | 374 | | |
340 | 375 | | |
341 | 376 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
107 | 108 | | |
108 | 109 | | |
109 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
110 | 146 | | |
111 | 147 | | |
112 | 148 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
136 | 142 | | |
137 | 143 | | |
138 | 144 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
12 | 38 | | |
13 | 39 | | |
14 | 40 | | |
| |||
49 | 75 | | |
50 | 76 | | |
51 | 77 | | |
52 | | - | |
| 78 | + | |
53 | 79 | | |
54 | 80 | | |
55 | 81 | | |
| |||
67 | 93 | | |
68 | 94 | | |
69 | 95 | | |
70 | | - | |
| 96 | + | |
71 | 97 | | |
72 | 98 | | |
73 | 99 | | |
| |||
85 | 111 | | |
86 | 112 | | |
87 | 113 | | |
88 | | - | |
| 114 | + | |
89 | 115 | | |
90 | 116 | | |
91 | 117 | | |
| |||
103 | 129 | | |
104 | 130 | | |
105 | 131 | | |
106 | | - | |
| 132 | + | |
107 | 133 | | |
108 | 134 | | |
109 | 135 | | |
| |||
124 | 150 | | |
125 | 151 | | |
126 | 152 | | |
127 | | - | |
| 153 | + | |
128 | 154 | | |
129 | 155 | | |
130 | 156 | | |
| |||
144 | 170 | | |
145 | 171 | | |
146 | 172 | | |
147 | | - | |
| 173 | + | |
148 | 174 | | |
149 | 175 | | |
150 | 176 | | |
| |||
0 commit comments