agent-certify 0.5.0__tar.gz → 0.6.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. {agent_certify-0.5.0 → agent_certify-0.6.0}/CHANGELOG.md +55 -1
  2. {agent_certify-0.5.0 → agent_certify-0.6.0}/PKG-INFO +62 -4
  3. {agent_certify-0.5.0 → agent_certify-0.6.0}/README.md +61 -3
  4. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/__init__.py +1 -1
  5. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/anomaly.py +53 -10
  6. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/baseline.py +115 -0
  7. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/cli.py +204 -16
  8. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/graph.py +12 -2
  9. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/mcp_server.py +2 -0
  10. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/parser/claude_code.py +8 -2
  11. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/parser/cursor_trace.py +13 -3
  12. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/parser/mcp_logs.py +6 -0
  13. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/policy.py +5 -1
  14. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify.egg-info/PKG-INFO +62 -4
  15. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify.egg-info/SOURCES.txt +2 -0
  16. agent_certify-0.6.0/tests/test_baseline_overrides.py +414 -0
  17. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_cli_v05.py +2 -1
  18. agent_certify-0.6.0/tests/test_cli_v051.py +125 -0
  19. {agent_certify-0.5.0 → agent_certify-0.6.0}/CONTRIBUTING.md +0 -0
  20. {agent_certify-0.5.0 → agent_certify-0.6.0}/LICENSE +0 -0
  21. {agent_certify-0.5.0 → agent_certify-0.6.0}/MANIFEST.in +0 -0
  22. {agent_certify-0.5.0 → agent_certify-0.6.0}/SECURITY.md +0 -0
  23. {agent_certify-0.5.0 → agent_certify-0.6.0}/examples/scan_claude_session.py +0 -0
  24. {agent_certify-0.5.0 → agent_certify-0.6.0}/pyproject.toml +0 -0
  25. {agent_certify-0.5.0 → agent_certify-0.6.0}/scripts/demo.py +0 -0
  26. {agent_certify-0.5.0 → agent_certify-0.6.0}/setup.cfg +0 -0
  27. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/blast.py +0 -0
  28. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/certificate.py +0 -0
  29. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/otel.py +0 -0
  30. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/parser/__init__.py +0 -0
  31. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/parser/auto.py +0 -0
  32. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/parser/base.py +0 -0
  33. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/paths.py +0 -0
  34. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/scanner.py +0 -0
  35. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/viz.py +0 -0
  36. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify/watcher.py +0 -0
  37. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify.egg-info/dependency_links.txt +0 -0
  38. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify.egg-info/entry_points.txt +0 -0
  39. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify.egg-info/requires.txt +0 -0
  40. {agent_certify-0.5.0 → agent_certify-0.6.0}/src/agent_certify.egg-info/top_level.txt +0 -0
  41. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/conftest.py +0 -0
  42. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_anomaly.py +0 -0
  43. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_auto_parser.py +0 -0
  44. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_baseline.py +0 -0
  45. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_blast.py +0 -0
  46. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_cert_by_type.py +0 -0
  47. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_certificate.py +0 -0
  48. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_cli.py +0 -0
  49. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_cursor_parser.py +0 -0
  50. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_hardening.py +0 -0
  51. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_mcp_parser.py +0 -0
  52. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_mcp_server.py +0 -0
  53. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_otel.py +0 -0
  54. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_parser_claude.py +0 -0
  55. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_perspective.py +0 -0
  56. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_policy.py +0 -0
  57. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_policy_gen.py +0 -0
  58. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_scanner.py +0 -0
  59. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_security_cert.py +0 -0
  60. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_security_mcp.py +0 -0
  61. {agent_certify-0.5.0 → agent_certify-0.6.0}/tests/test_watcher.py +0 -0
@@ -6,6 +6,59 @@ project uses [Semantic Versioning](https://semver.org/).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.6.0] - 2026-11-03
10
+
11
+ Feedback from Reddit and LinkedIn: a baseline that can only be re-learned is a
12
+ baseline you cannot correct. Three decisions are now recorded in it, and
13
+ `learn` carries them forward instead of overwriting them.
14
+
15
+ ### Added
16
+ - `agent-certify baseline accept --tool NAME` — whitelist a tool without
17
+ re-learning. It stops being a `new_tool` anomaly and no statistic moves
18
+ - `agent-certify baseline reject --tool NAME` — a tool that must never become
19
+ normal. It fires **even after a re-learn that saw it in every session**, and
20
+ `suggest-policy` no longer drafts it as a `required_tool` rule
21
+ - `agent-certify baseline accept --link "agent:Build -> deploy_staging"` —
22
+ scoped acceptance: one caller, one target. A different agent calling the same
23
+ tool still fires, and the message names the callers no accepted link covers
24
+ (thanks to Rohan Ahmed on LinkedIn for the case)
25
+ - `agent-certify baseline show [--format json]` — the decisions on record
26
+ - `agent-certify learn --forget-overrides` — drop them deliberately
27
+ - `Baseline.accepted_tools`, `.rejected_tools`, `.accepted_links`,
28
+ `.has_overrides`, and `carry_overrides()`, `parse_link()`, `normalise_tool()`,
29
+ `normalise_endpoint()` in `agent_certify.baseline`
30
+
31
+ ### Changed
32
+ - `agent-certify learn` reads the baseline it is about to overwrite and carries
33
+ its accept/reject decisions onto the new one. A corrupt previous file loses
34
+ the decisions but does not block learning
35
+ - `--tool` and `--link` are repeatable; `--tool` takes `Bash` or `tool:Bash`
36
+ - accept and reject are last-write-wins on the same tool: "permanent" means it
37
+ survives a re-learn, not that it cannot be revised. Rejection beats a scoped
38
+ accepted link
39
+
40
+ ### Notes
41
+ - Baselines written by 0.5.1 and earlier load unchanged — the three fields are
42
+ defaulted and a pre-0.6 file simply has no decisions on record
43
+ - There is no `reject --link`: a link you never want is a policy FORBIDDEN rule,
44
+ where you write the message
45
+
46
+
47
+ ## [0.5.1] - 2026-10-27
48
+
49
+ ### Fixed
50
+ - A file with no readable records (empty, or nothing but malformed lines) now prints
51
+ `No readable records in <file>` and exits 1, instead of an empty report with FI 0.0000. A directory
52
+ scan leaves such files out and says how many; the MCP server reports it as a tool error
53
+ - Unreadable records produce a single warning per file,
54
+ `WARNING: <file>: N of M records unreadable, skipped` (the parser and the CLI each printed one);
55
+ M now counts every record read, sub-agent transcripts included
56
+
57
+ ### Changed
58
+ - `keygen` without an argument writes `agent-certify.key` (and `agent-certify.key.pub`)
59
+ - `verify` with its arguments reversed or missing prints
60
+ `Hint: usage is 'verify CERTIFICATE SESSION --public-key KEY'` and exits 2
61
+
9
62
  ## [0.5.0] - 2026-10-27
10
63
 
11
64
  Fixes for the security audit of 2026-10-27 (45 findings). Items marked
@@ -109,7 +162,8 @@ Fixes for the security audit of 2026-10-27 (45 findings). Items marked
109
162
  - HTML visualization with interactive graph
110
163
  - CLI: scan, blast, keygen, certify, verify
111
164
 
112
- [Unreleased]: https://github.com/zeekmartin/agent-certify/compare/v0.5.0...HEAD
165
+ [Unreleased]: https://github.com/zeekmartin/agent-certify/compare/v0.5.1...HEAD
166
+ [0.5.1]: https://github.com/zeekmartin/agent-certify/compare/v0.5.0...v0.5.1
113
167
  [0.5.0]: https://github.com/zeekmartin/agent-certify/compare/v0.4.0...v0.5.0
114
168
  [0.4.0]: https://github.com/zeekmartin/agent-certify/compare/v0.3.0...v0.4.0
115
169
  [0.3.0]: https://github.com/zeekmartin/agent-certify/compare/v0.2.0...v0.3.0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: agent-certify
3
- Version: 0.5.0
3
+ Version: 0.6.0
4
4
  Summary: Structural verification of multiagent systems
5
5
  Author-email: David Martin Venti <david.martin@cognitive-engineering.dev>
6
6
  License-Expression: Apache-2.0
@@ -46,7 +46,7 @@ Dynamic: license-file
46
46
  [![Downloads](https://img.shields.io/pypi/dm/agent-certify)](https://pypi.org/project/agent-certify/)
47
47
  [![License](https://img.shields.io/badge/license-Apache%202.0-blue)](LICENSE)
48
48
  [![Coverage](https://img.shields.io/badge/coverage-95%25-brightgreen)]()
49
- [![Tests](https://img.shields.io/badge/tests-279%20passed-brightgreen)]()
49
+ [![Tests](https://img.shields.io/badge/tests-306%20passed-brightgreen)]()
50
50
 
51
51
  Structural verification of multiagent systems.
52
52
 
@@ -161,6 +161,12 @@ agent-certify scan session.jsonl # compares against
161
161
  agent-certify scan session.jsonl --baseline path/to/baseline.json
162
162
  agent-certify scan session.jsonl --no-baseline
163
163
 
164
+ # Tell the baseline what you have decided (survives a re-learn)
165
+ agent-certify baseline accept --tool Bash # stop flagging it
166
+ agent-certify baseline reject --tool deploy_production # keep flagging it, forever
167
+ agent-certify baseline accept --link "agent:Build -> deploy_staging" # that agent only
168
+ agent-certify baseline show # what is on record
169
+
164
170
  # Policy: explicit rules, drafted from the baseline, edited by a human
165
171
  agent-certify suggest-policy # writes .agent-certify/policy.json
166
172
  agent-certify scan session.jsonl # also checks ./.agent-certify/policy.json if present
@@ -246,6 +252,8 @@ result.labels("compliance") # same result, other vocabulary
246
252
  error rate (errors / tool calls), plus FI's range and the most sub-agents seen
247
253
  - every tool seen, and the fraction of sessions it appears in
248
254
  - every invocation link seen (`source -> target`)
255
+ - the tools and links you have accepted or rejected by hand, which are decisions
256
+ rather than statistics and so survive a re-learn (see below)
249
257
 
250
258
  Sub-agent ids are unique per spawn, so links are recorded by role: a sub-agent
251
259
  counts as `agent:<type>` (`agent:Explore`, `agent:general-purpose`). Standard
@@ -269,12 +277,62 @@ report.has_critical
269
277
  print(report)
270
278
  ```
271
279
 
280
+ ### Deciding what counts as normal
281
+
282
+ Learning is a statistic. Whether you *accept* what it learned is a decision, and
283
+ a decision must not be erased by the next `learn`. Three commands record one:
284
+
285
+ ```bash
286
+ # A tool you know about. Stops being an anomaly, without re-learning anything.
287
+ agent-certify baseline accept --tool Bash
288
+
289
+ # A tool that must never become normal. It keeps firing even after a re-learn
290
+ # that saw it in every single session, and `suggest-policy` will never draft it
291
+ # as a REQUIRED tool.
292
+ agent-certify baseline reject --tool deploy_production
293
+
294
+ # Scoped acceptance: this agent may call this tool. Another agent calling the
295
+ # same tool still fires.
296
+ agent-certify baseline accept --link "agent:Build -> deploy_staging"
297
+
298
+ agent-certify baseline show # or --format json
299
+ ```
300
+
301
+ All three are stored in `baseline.json` and **carried forward by
302
+ `agent-certify learn`**, which recomputes every statistic and leaves the
303
+ decisions standing. `learn --forget-overrides` drops them on purpose.
304
+
305
+ Scoped acceptance is what makes `--link` worth having. Approving
306
+ `agent:Build -> deploy_staging` does not approve `deploy_staging`: if another
307
+ caller appears, the tool is flagged again and the message names the callers that
308
+ no accepted link covers.
309
+
310
+ ```
311
+ [CRITICAL] New tool never seen: deploy_staging
312
+ Baseline: not in 14 known tools
313
+ 2 call(s) by agent:Test:0 — not covered by an accepted link: agent:Test
314
+ ```
315
+
316
+ A tool can be accepted and then rejected, or the reverse: the newer decision
317
+ wins. "Permanent" means it survives re-learning, not that you cannot change your
318
+ mind. Rejection beats a scoped link, so a rejected tool fires whoever calls it.
319
+
320
+ A link endpoint written with a `kind:` prefix (`tool:Bash`, `agent:Explore`) is
321
+ taken as it stands. A bare word is resolved against the baseline — the
322
+ orchestrator's id is a bare word (`claude`) and so is a tool name — and falls
323
+ back to a tool, which is the usual case, since the reason to scope a link is
324
+ normally a tool that is new.
325
+
326
+ Rejection is deliberately asymmetric: there is no `reject --link`. A link you
327
+ never want is a policy FORBIDDEN rule, which `scan --policy` enforces with a
328
+ message you write yourself.
329
+
272
330
  ## Anomaly detection
273
331
 
274
332
  | Check | Flags | Severity |
275
333
  |---|---|---|
276
- | `new_tool` | a tool never seen in the baseline | critical |
277
- | `new_link` | an invocation path never seen (links to a new tool are not repeated) | warning |
334
+ | `new_tool` | a tool never seen in the baseline, or one you rejected | critical |
335
+ | `new_link` | an invocation path never seen or accepted (links to a flagged tool are not repeated) | warning |
278
336
  | `fi_deviation` | FI more than 2σ from the mean | warning; critical past 3σ when FI *rose* |
279
337
  | `error_rate` | error rate more than 2σ *above* the mean | warning; critical past 3σ |
280
338
  | `sub_agents` | more sub-agents than ever seen, or 2σ above the mean | warning; critical when above the max *and* past 3σ |
@@ -6,7 +6,7 @@
6
6
  [![Downloads](https://img.shields.io/pypi/dm/agent-certify)](https://pypi.org/project/agent-certify/)
7
7
  [![License](https://img.shields.io/badge/license-Apache%202.0-blue)](LICENSE)
8
8
  [![Coverage](https://img.shields.io/badge/coverage-95%25-brightgreen)]()
9
- [![Tests](https://img.shields.io/badge/tests-279%20passed-brightgreen)]()
9
+ [![Tests](https://img.shields.io/badge/tests-306%20passed-brightgreen)]()
10
10
 
11
11
  Structural verification of multiagent systems.
12
12
 
@@ -121,6 +121,12 @@ agent-certify scan session.jsonl # compares against
121
121
  agent-certify scan session.jsonl --baseline path/to/baseline.json
122
122
  agent-certify scan session.jsonl --no-baseline
123
123
 
124
+ # Tell the baseline what you have decided (survives a re-learn)
125
+ agent-certify baseline accept --tool Bash # stop flagging it
126
+ agent-certify baseline reject --tool deploy_production # keep flagging it, forever
127
+ agent-certify baseline accept --link "agent:Build -> deploy_staging" # that agent only
128
+ agent-certify baseline show # what is on record
129
+
124
130
  # Policy: explicit rules, drafted from the baseline, edited by a human
125
131
  agent-certify suggest-policy # writes .agent-certify/policy.json
126
132
  agent-certify scan session.jsonl # also checks ./.agent-certify/policy.json if present
@@ -206,6 +212,8 @@ result.labels("compliance") # same result, other vocabulary
206
212
  error rate (errors / tool calls), plus FI's range and the most sub-agents seen
207
213
  - every tool seen, and the fraction of sessions it appears in
208
214
  - every invocation link seen (`source -> target`)
215
+ - the tools and links you have accepted or rejected by hand, which are decisions
216
+ rather than statistics and so survive a re-learn (see below)
209
217
 
210
218
  Sub-agent ids are unique per spawn, so links are recorded by role: a sub-agent
211
219
  counts as `agent:<type>` (`agent:Explore`, `agent:general-purpose`). Standard
@@ -229,12 +237,62 @@ report.has_critical
229
237
  print(report)
230
238
  ```
231
239
 
240
+ ### Deciding what counts as normal
241
+
242
+ Learning is a statistic. Whether you *accept* what it learned is a decision, and
243
+ a decision must not be erased by the next `learn`. Three commands record one:
244
+
245
+ ```bash
246
+ # A tool you know about. Stops being an anomaly, without re-learning anything.
247
+ agent-certify baseline accept --tool Bash
248
+
249
+ # A tool that must never become normal. It keeps firing even after a re-learn
250
+ # that saw it in every single session, and `suggest-policy` will never draft it
251
+ # as a REQUIRED tool.
252
+ agent-certify baseline reject --tool deploy_production
253
+
254
+ # Scoped acceptance: this agent may call this tool. Another agent calling the
255
+ # same tool still fires.
256
+ agent-certify baseline accept --link "agent:Build -> deploy_staging"
257
+
258
+ agent-certify baseline show # or --format json
259
+ ```
260
+
261
+ All three are stored in `baseline.json` and **carried forward by
262
+ `agent-certify learn`**, which recomputes every statistic and leaves the
263
+ decisions standing. `learn --forget-overrides` drops them on purpose.
264
+
265
+ Scoped acceptance is what makes `--link` worth having. Approving
266
+ `agent:Build -> deploy_staging` does not approve `deploy_staging`: if another
267
+ caller appears, the tool is flagged again and the message names the callers that
268
+ no accepted link covers.
269
+
270
+ ```
271
+ [CRITICAL] New tool never seen: deploy_staging
272
+ Baseline: not in 14 known tools
273
+ 2 call(s) by agent:Test:0 — not covered by an accepted link: agent:Test
274
+ ```
275
+
276
+ A tool can be accepted and then rejected, or the reverse: the newer decision
277
+ wins. "Permanent" means it survives re-learning, not that you cannot change your
278
+ mind. Rejection beats a scoped link, so a rejected tool fires whoever calls it.
279
+
280
+ A link endpoint written with a `kind:` prefix (`tool:Bash`, `agent:Explore`) is
281
+ taken as it stands. A bare word is resolved against the baseline — the
282
+ orchestrator's id is a bare word (`claude`) and so is a tool name — and falls
283
+ back to a tool, which is the usual case, since the reason to scope a link is
284
+ normally a tool that is new.
285
+
286
+ Rejection is deliberately asymmetric: there is no `reject --link`. A link you
287
+ never want is a policy FORBIDDEN rule, which `scan --policy` enforces with a
288
+ message you write yourself.
289
+
232
290
  ## Anomaly detection
233
291
 
234
292
  | Check | Flags | Severity |
235
293
  |---|---|---|
236
- | `new_tool` | a tool never seen in the baseline | critical |
237
- | `new_link` | an invocation path never seen (links to a new tool are not repeated) | warning |
294
+ | `new_tool` | a tool never seen in the baseline, or one you rejected | critical |
295
+ | `new_link` | an invocation path never seen or accepted (links to a flagged tool are not repeated) | warning |
238
296
  | `fi_deviation` | FI more than 2σ from the mean | warning; critical past 3σ when FI *rose* |
239
297
  | `error_rate` | error rate more than 2σ *above* the mean | warning; critical past 3σ |
240
298
  | `sub_agents` | more sub-agents than ever seen, or 2σ above the mean | warning; critical when above the max *and* past 3σ |
@@ -2,7 +2,7 @@
2
2
  # Copyright 2026 Cognitive Engineering (Switzerland)
3
3
  # SPDX-License-Identifier: Apache-2.0
4
4
  """agent-certify: structural verification of multiagent systems."""
5
- __version__ = "0.5.0"
5
+ __version__ = "0.6.0"
6
6
 
7
7
  from agent_certify.anomaly import Anomaly, AnomalyDetector, AnomalyReport
8
8
  from agent_certify.baseline import Baseline, BaselineLearner, load_baseline
@@ -8,7 +8,8 @@ Each check compares one aspect of a session with the :class:`Baseline`:
8
8
  ============== ========================================= ======================
9
9
  check flags severity
10
10
  ============== ========================================= ======================
11
- new_tool a tool never seen in the baseline critical
11
+ new_tool a tool never seen in the baseline, or one critical
12
+ the operator rejected
12
13
  new_link an invocation path never seen (by role) warning
13
14
  fi_deviation FI more than 2 sigma from the mean warning; critical past
14
15
  3 sigma when FI *rose*
@@ -19,6 +20,14 @@ sub_agents more sub-agents than ever seen, or 2 sigma warning; critical wh
19
20
  node_count node count more than 2 sigma from mean info; warning past 3
20
21
  ============== ========================================= ======================
21
22
 
23
+ Three baseline overrides change what ``new_tool`` and ``new_link`` consider
24
+ normal, and they are decisions rather than statistics, so they win over what was
25
+ learned: ``accepted_tools`` silences a tool, ``rejected_tools`` makes one fire
26
+ however often it has been seen, and ``accepted_links`` silences one
27
+ (source role, target role) pair. Acceptance of a link is **scoped** — if a tool
28
+ is new and only some of its callers are covered by an accepted link, the
29
+ ``new_tool`` anomaly still fires and names the callers that are not.
30
+
22
31
  A falling FI means added redundancy (usually sub-agents sharing tools), a
23
32
  structural change worth knowing about but not a risk in itself, so it never
24
33
  goes past warning. When the baseline never varied on a metric (std 0), z is
@@ -132,30 +141,64 @@ class AnomalyDetector:
132
141
  baseline_source=self.source, learned_from=self.baseline.learned_from)
133
142
 
134
143
  # --------------------------------------------------------------- checks
144
+ def _uncovered_callers(self, graph: AgentGraph, tool_id: str) -> list[str]:
145
+ """Caller roles of ``tool_id`` that no accepted link covers.
146
+
147
+ This is what makes ``baseline accept --link`` scoped: approving
148
+ ``agent:Build -> tool:deploy_staging`` leaves every other caller of that
149
+ tool uncovered, so the tool still gets flagged and the message says who
150
+ is responsible.
151
+ """
152
+ accepted = self.baseline.accepted_links
153
+ callers = {node_role(graph, e.source) for e in graph.edges if e.target == tool_id}
154
+ return sorted(c for c in callers if (c, tool_id) not in accepted)
155
+
135
156
  def _new_tool_names(self, graph: AgentGraph) -> set[str]:
136
- return {n.name for n in graph.nodes.values() if n.kind == "tool"} - self.baseline.known_tools
157
+ """Tools that should be flagged, after the operator's decisions are applied.
158
+
159
+ A rejected tool is returned even when the baseline has learned it, which
160
+ is the whole point of rejecting: a re-learn that saw it in every session
161
+ must not make it normal.
162
+ """
163
+ b = self.baseline
164
+ present = {n.name for n in graph.nodes.values() if n.kind == "tool"}
165
+ return (present - b.known_tools - b.accepted_tools) | (present & b.rejected_tools)
137
166
 
138
167
  def _check_new_tools(self, graph: AgentGraph) -> list[Anomaly]:
139
- """Flag any tool not in baseline.known_tools. Severity: critical."""
168
+ """Flag any tool the baseline does not vouch for. Severity: critical."""
140
169
  out = []
141
170
  for name in sorted(self._new_tool_names(graph)):
142
171
  nid = next(i for i, n in graph.nodes.items() if n.kind == "tool" and n.name == name)
172
+ rejected = name in self.baseline.rejected_tools
173
+ uncovered = self._uncovered_callers(graph, nid)
174
+ if not rejected and not uncovered:
175
+ continue # every caller is covered by an accepted link
143
176
  callers = sorted({e.source for e in graph.edges if e.target == nid})
144
- out.append(Anomaly(
145
- CRITICAL, "new_tool", f"New tool never seen: {name}", name,
146
- f"not in {len(self.baseline.known_tools)} known tools", None,
147
- f"{graph.nodes[nid].call_count} call(s) by {', '.join(callers) or '-'}"))
177
+ detail = f"{graph.nodes[nid].call_count} call(s) by {', '.join(callers) or '-'}"
178
+ if rejected:
179
+ message = f"Rejected tool used: {name}"
180
+ reference = "explicitly rejected by the operator"
181
+ detail += " — rejected, so it stays an anomaly across re-learns"
182
+ else:
183
+ message = f"New tool never seen: {name}"
184
+ reference = f"not in {len(self.baseline.known_tools)} known tools"
185
+ if self.baseline.accepted_links:
186
+ detail += f" — not covered by an accepted link: {', '.join(uncovered)}"
187
+ out.append(Anomaly(CRITICAL, "new_tool", message, name, reference, None, detail))
148
188
  return out
149
189
 
150
190
  def _check_new_links(self, graph: AgentGraph) -> list[Anomaly]:
151
- """Flag any (source, target) link not in baseline.known_links. Severity: warning.
191
+ """Flag any (source, target) link the baseline does not allow. Severity: warning.
152
192
 
153
- Links to a new tool are covered by the new_tool anomaly and not repeated.
193
+ Allowed means learned (``known_links``) or accepted (``accepted_links``).
194
+ Links to a flagged tool are covered by the new_tool anomaly and not
195
+ repeated.
154
196
  """
155
197
  new_tools = {f"tool:{t}" for t in self._new_tool_names(graph)}
156
198
  links = {(node_role(graph, e.source), node_role(graph, e.target)) for e in graph.edges}
199
+ allowed = self.baseline.known_links | self.baseline.accepted_links
157
200
  out = []
158
- for s, t in sorted(links - self.baseline.known_links):
201
+ for s, t in sorted(links - allowed):
159
202
  if s in new_tools or t in new_tools:
160
203
  continue
161
204
  out.append(Anomaly(WARNING, "new_link", f"New communication path: {s} -> {t}", f"{s} -> {t}",
@@ -12,6 +12,25 @@ Sub-agent ids are unique per spawn, so links are recorded by *role*: an agent
12
12
  node becomes ``agent:<type>`` (``agent:Explore``, ``agent:general-purpose``).
13
13
  Tools and the orchestrator keep their ids. Standard deviations are population
14
14
  deviations, so a baseline learned from one session has std 0.
15
+
16
+ **Overrides.** What a baseline learned is a statistic; what a human decided
17
+ about it is not, and re-learning must not erase the decision. Three sets carry
18
+ that:
19
+
20
+ ``accepted_tools`` a tool the operator has blessed. It stops being a
21
+ ``new_tool`` anomaly without being averaged into any
22
+ statistic.
23
+ ``rejected_tools`` a tool that must never become normal. It fires every time,
24
+ *even after a re-learn that saw it in every session*, and
25
+ it is never promoted to a ``required_tool`` policy rule.
26
+ ``accepted_links`` a (source role, target role) pair. Scoped: approving
27
+ ``agent:Build -> tool:deploy_staging`` leaves
28
+ ``agent:Test -> tool:deploy_staging`` firing.
29
+
30
+ The sets are stored in the baseline file and copied forward by
31
+ :func:`carry_overrides`, which ``agent-certify learn`` calls. A later
32
+ ``accept``/``reject`` overrides an earlier one on the same tool: "permanent"
33
+ means it survives re-learning, not that it cannot be revised deliberately.
15
34
  """
16
35
  from __future__ import annotations
17
36
 
@@ -88,14 +107,26 @@ class Baseline:
88
107
  # Metadata
89
108
  created_at: str
90
109
  version: str = "0.2.0"
110
+ # Human decisions, not statistics. Carried across re-learns; see the module
111
+ # docstring. Defaulted, so a baseline written by <= 0.5.1 still loads.
112
+ accepted_tools: set[str] = field(default_factory=set)
113
+ rejected_tools: set[str] = field(default_factory=set)
114
+ accepted_links: set[tuple[str, str]] = field(default_factory=set)
91
115
  fi_min: float = 0.0
92
116
  fi_max: float = 0.0
93
117
  session_ids: list[str] = field(default_factory=list)
94
118
 
119
+ @property
120
+ def has_overrides(self) -> bool:
121
+ return bool(self.accepted_tools or self.rejected_tools or self.accepted_links)
122
+
95
123
  def to_dict(self) -> dict:
96
124
  d = asdict(self)
97
125
  d["known_tools"] = sorted(self.known_tools)
98
126
  d["known_links"] = sorted(list(link) for link in self.known_links)
127
+ d["accepted_tools"] = sorted(self.accepted_tools)
128
+ d["rejected_tools"] = sorted(self.rejected_tools)
129
+ d["accepted_links"] = sorted(list(link) for link in self.accepted_links)
99
130
  d["tool_frequency"] = dict(sorted(self.tool_frequency.items(), key=lambda kv: (-kv[1], kv[0])))
100
131
  return d
101
132
 
@@ -128,11 +159,24 @@ class Baseline:
128
159
  _check(isinstance(data["known_links"], list)
129
160
  and all(isinstance(link, list) and len(link) == 2 and all(isinstance(x, str) for x in link)
130
161
  for link in data["known_links"]))
162
+ for key in ("accepted_tools", "rejected_tools"):
163
+ if key in data:
164
+ _check(isinstance(data[key], list) and all(isinstance(t, str) for t in data[key]))
165
+ if "accepted_links" in data:
166
+ _check(isinstance(data["accepted_links"], list)
167
+ and all(isinstance(link, list) and len(link) == 2
168
+ and all(isinstance(x, str) for x in link)
169
+ for link in data["accepted_links"]))
131
170
  ids = data.get("session_ids", [])
132
171
  _check(isinstance(ids, list) and all(isinstance(x, str) for x in ids))
133
172
  data = dict(data)
134
173
  data["known_tools"] = set(data["known_tools"])
135
174
  data["known_links"] = {tuple(link) for link in data["known_links"]}
175
+ for key in ("accepted_tools", "rejected_tools"):
176
+ if key in data:
177
+ data[key] = set(data[key])
178
+ if "accepted_links" in data:
179
+ data["accepted_links"] = {tuple(link) for link in data["accepted_links"]}
136
180
  return cls(**data)
137
181
  except (TypeError, KeyError, AttributeError):
138
182
  raise ValueError("Invalid baseline format") from None
@@ -257,3 +301,74 @@ def _is_count(v) -> bool:
257
301
 
258
302
  def _is_number(v) -> bool:
259
303
  return isinstance(v, (int, float)) and not isinstance(v, bool) and math.isfinite(v)
304
+
305
+
306
+ # ------------------------------------------------------------------ overrides
307
+ _ARROW = "->"
308
+
309
+
310
+ def normalise_tool(name: str) -> str:
311
+ """A tool as ``known_tools`` spells it: the display name, no ``tool:`` prefix.
312
+
313
+ ``known_tools`` holds ``node.name``, while link endpoints hold ``node_id``
314
+ (``tool:Bash``). Both spellings reach the CLI, so both are accepted here and
315
+ the display name wins.
316
+ """
317
+ name = name.strip()
318
+ if not name:
319
+ raise ValueError("tool name is empty")
320
+ return name[len("tool:"):].strip() if name.lower().startswith("tool:") else name
321
+
322
+
323
+ def normalise_endpoint(endpoint: str, baseline: Baseline | None = None) -> str:
324
+ """One end of a link, as ``known_links`` spells it.
325
+
326
+ An endpoint carrying a ``kind:`` prefix is already a role
327
+ (``tool:Bash``, ``agent:Explore``) and is kept. A bare word is ambiguous:
328
+ the orchestrator's node id is a bare word (``claude``) and so is a tool
329
+ display name (``deploy_staging``). It is resolved against what the baseline
330
+ has seen, and falls back to a tool -- which is the common case, since the
331
+ reason to scope a link is usually a tool that is new.
332
+ """
333
+ endpoint = endpoint.strip()
334
+ if not endpoint:
335
+ raise ValueError("link endpoint is empty")
336
+ if ":" in endpoint:
337
+ return endpoint
338
+ if baseline is not None:
339
+ if endpoint in baseline.known_tools:
340
+ return f"tool:{endpoint}"
341
+ seen = {s for s, _ in baseline.known_links} | {t for _, t in baseline.known_links}
342
+ if endpoint in seen:
343
+ return endpoint
344
+ return f"tool:{endpoint}"
345
+
346
+
347
+ def parse_link(text: str, baseline: Baseline | None = None) -> tuple[str, str]:
348
+ """``"agent:Build -> deploy_staging"`` -> ``("agent:Build", "tool:deploy_staging")``.
349
+
350
+ Raises ``ValueError`` naming what was expected; a link the operator cannot
351
+ spell is worse than one they are told how to spell.
352
+ """
353
+ parts = str(text).split(_ARROW)
354
+ if len(parts) != 2:
355
+ raise ValueError(
356
+ f"link must be \"SOURCE {_ARROW} TARGET\", e.g. "
357
+ f"\"agent:Build {_ARROW} deploy_staging\"; got {text!r}")
358
+ return (normalise_endpoint(parts[0], baseline), normalise_endpoint(parts[1], baseline))
359
+
360
+
361
+ def carry_overrides(new: Baseline, old: Baseline | None) -> Baseline:
362
+ """Copy ``old``'s human decisions onto a freshly learned ``new`` baseline.
363
+
364
+ Re-learning recomputes every statistic, which is the point; it must not
365
+ silently undo an ``accept`` or a ``reject``, which would turn a rejected
366
+ tool normal the moment it appeared often enough. Returns ``new`` mutated in
367
+ place for the caller's convenience.
368
+ """
369
+ if old is None:
370
+ return new
371
+ new.accepted_tools = set(old.accepted_tools)
372
+ new.rejected_tools = set(old.rejected_tools)
373
+ new.accepted_links = set(old.accepted_links)
374
+ return new