|
| 1 | +"""Comparison primitives, plus the two gates that keep a comparison honest. |
| 2 | +
|
| 3 | +Every engine's diff reduces to four shapes: a map of name to digest (components, |
| 4 | +instruction files, policy files), a set of names (tools, MCP servers), a scalar |
| 5 | +(model, permission mode), and a rollup hash. What differs between engines is which |
| 6 | +categories exist and what they are called, so those stay with the engine and the |
| 7 | +shapes live here. |
| 8 | +
|
| 9 | +Two gates matter more than the shapes. |
| 10 | +
|
| 11 | +**Observed gating.** A snapshot records which categories it actually measured. A |
| 12 | +shell hook cannot enumerate a live tool roster, so comparing a hook snapshot |
| 13 | +against a richer baseline would report the baseline's tools as removed. Only |
| 14 | +categories that BOTH sides measured are compared. |
| 15 | +
|
| 16 | +**Scope gating.** When an engine widens what a fingerprint covers, old fingerprints |
| 17 | +become incomparable. Without handling, an upgrade reports every affected component |
| 18 | +as changed. That is an alarm the user knows is false, which is worse than no alarm |
| 19 | +because it teaches them to dismiss the next one. So a scope mismatch is reported |
| 20 | +once, as a re-approval prompt, and the affected categories are dropped from the |
| 21 | +comparison rather than compared wrongly. |
| 22 | +""" |
| 23 | + |
| 24 | +from __future__ import annotations |
| 25 | + |
| 26 | +from collections.abc import Iterable, Mapping, Sequence |
| 27 | + |
| 28 | +__all__ = [ |
| 29 | + "Change", |
| 30 | + "diff_hash", |
| 31 | + "diff_maps", |
| 32 | + "diff_scalar", |
| 33 | + "diff_sets", |
| 34 | + "observed_categories", |
| 35 | + "scope_change", |
| 36 | +] |
| 37 | + |
| 38 | +#: A single finding. ``change`` is one of added, removed, changed. |
| 39 | +Change = dict |
| 40 | + |
| 41 | + |
| 42 | +def _change(change: str, what: str, detail: str) -> Change: |
| 43 | + return {"change": change, "what": what, "detail": detail} |
| 44 | + |
| 45 | + |
| 46 | +def diff_maps(base: Mapping[str, str], current: Mapping[str, str], what: str) -> list[Change]: |
| 47 | + """Compare two name-to-digest maps. Names are reported, digests are not. |
| 48 | +
|
| 49 | + A digest in a report tells the reader nothing they can act on; the name of the |
| 50 | + component that moved does. |
| 51 | + """ |
| 52 | + out: list[Change] = [] |
| 53 | + for name in sorted(set(current) - set(base)): |
| 54 | + out.append(_change("added", what, name)) |
| 55 | + for name in sorted(set(base) - set(current)): |
| 56 | + out.append(_change("removed", what, name)) |
| 57 | + for name in sorted(set(base) & set(current)): |
| 58 | + if base[name] != current[name]: |
| 59 | + out.append(_change("changed", what, name)) |
| 60 | + return out |
| 61 | + |
| 62 | + |
| 63 | +def diff_sets(base: Iterable[str], current: Iterable[str], what: str) -> list[Change]: |
| 64 | + """Compare two name sets, for categories with no per-item digest.""" |
| 65 | + before, after = set(base), set(current) |
| 66 | + out: list[Change] = [] |
| 67 | + for name in sorted(after - before): |
| 68 | + out.append(_change("added", what, name)) |
| 69 | + for name in sorted(before - after): |
| 70 | + out.append(_change("removed", what, name)) |
| 71 | + return out |
| 72 | + |
| 73 | + |
| 74 | +def diff_scalar(before: object, after: object, what: str, *, unknown: str = "unknown") -> list[Change]: |
| 75 | + """Compare a single value, reporting the transition rather than just the fact.""" |
| 76 | + if before == after: |
| 77 | + return [] |
| 78 | + return [_change("changed", what, "%s -> %s" % (before or unknown, after or unknown))] |
| 79 | + |
| 80 | + |
| 81 | +def diff_hash(before: str | None, after: str | None, what: str, detail: str) -> list[Change]: |
| 82 | + """Compare a rollup hash, where only the fact of change is available.""" |
| 83 | + if before == after: |
| 84 | + return [] |
| 85 | + return [_change("changed", what, detail)] |
| 86 | + |
| 87 | + |
| 88 | +def observed_categories( |
| 89 | + base: Mapping[str, object], |
| 90 | + current: Mapping[str, object], |
| 91 | + default: Sequence[str] = (), |
| 92 | +) -> set[str]: |
| 93 | + """Categories both snapshots measured, and therefore may be compared.""" |
| 94 | + return set(base.get("observed", list(default))) & set(current.get("observed", list(default))) |
| 95 | + |
| 96 | + |
| 97 | +def scope_change( |
| 98 | + base: Mapping[str, object], |
| 99 | + current_scope: int, |
| 100 | + *, |
| 101 | + affected: Sequence[str], |
| 102 | + reason: str, |
| 103 | +) -> Change | None: |
| 104 | + """Report a widened measurement scope, or None when the scopes agree. |
| 105 | +
|
| 106 | + ``affected`` names the categories the caller must drop from its comparison, |
| 107 | + and is included in the message so the reader knows what was not checked rather |
| 108 | + than assuming everything was. |
| 109 | + """ |
| 110 | + base_scope = base.get("scope", 1) |
| 111 | + if base_scope == current_scope: |
| 112 | + return None |
| 113 | + dropped = ", ".join(affected) if affected else "none" |
| 114 | + return _change( |
| 115 | + "changed", |
| 116 | + "measurement scope", |
| 117 | + "widened from %s to %s; %s Not compared this run: %s. Re-approve once to " |
| 118 | + "compare on the new scope." % (base_scope, current_scope, reason, dropped), |
| 119 | + ) |
0 commit comments