|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""External fixture conformance: chenhz01-001 — availability certification (#11). |
| 3 | +
|
| 4 | +Source: AgentMeasure issue #11 (Clarify Result Consumption). The two-state |
| 5 | +ontology (availability vs influence) and the compaction boundary are |
| 6 | +@gunjanjaswal's; the trace-side hash certification is @chenhz01's; the |
| 7 | +canonicalized-content + field-path + four-value axis is @roy-tong's. This |
| 8 | +fixture is the first availability vector: one synthetic trace whose four tool |
| 9 | +results each meet a different certification outcome, so a compaction-aware |
| 10 | +implementation can be tested against all four at once. |
| 11 | +
|
| 12 | +Certification axis (per result, orthogonal to the State-B influence axis): |
| 13 | + verbatim the result's canonicalized content is embedded in the next |
| 14 | + model request -> State A certified (observability: TRUE); |
| 15 | + summary-embed only a lossy projection reached the request -> weak influence |
| 16 | + evidence, NOT availability, its own bucket (FALSE for A); |
| 17 | + absent a request left, the result is nowhere in it (FALSE); |
| 18 | + unreachable no request left the framework -> UNOBSERVABLE, excluded from |
| 19 | + the denominator, never counted FALSE (invariant 17). |
| 20 | +
|
| 21 | +Guards: |
| 22 | + 1. schema — every event validates under FMT-002; |
| 23 | + 2. core — aggregate() reproduces the naive boolean consumption_rate |
| 24 | + (1/4): the collapse the four-value axis corrects; |
| 25 | + 3. cert — certification recomputed from the raw trace_context (canonical |
| 26 | + field-walk) reproduces every event's x_availability and the |
| 27 | + expected four-bucket breakdown; |
| 28 | + 4. reserial — a raw byte/substring certifier MISSES op1 (re-serialized JSON |
| 29 | + string) where the canonical field-walk certifies it verbatim; |
| 30 | + 5. boundary — the forbid list holds: summary-embed is not State A and not |
| 31 | + absent; unreachable is excluded, never collapsed to FALSE; |
| 32 | + 6. collapse — mislabeling unreachable as absent (UNOBSERVABLE -> FALSE) |
| 33 | + changes the denominator; the vector must make that detectable. |
| 34 | +
|
| 35 | +Claim boundary: synthetic conformance fixture authored for #11 — not an |
| 36 | +endorsement, an external reproduction, or a real runtime observation. |
| 37 | +
|
| 38 | +Exit 0 = all guards pass. |
| 39 | +""" |
| 40 | +from __future__ import annotations |
| 41 | + |
| 42 | +import hashlib |
| 43 | +import json |
| 44 | +import sys |
| 45 | +from pathlib import Path |
| 46 | + |
| 47 | +ROOT = Path(__file__).resolve().parents[2] |
| 48 | +sys.path.insert(0, str(ROOT / "lab")) |
| 49 | +from agentmeasure_lab.analysis import aggregate # noqa: E402 |
| 50 | +from agentmeasure_lab.schemas import SchemaError, validate # noqa: E402 |
| 51 | + |
| 52 | +VEC = ROOT / "conformance" / "vectors" / "external" / "chenhz01-001" |
| 53 | +EVENTS_FILE = VEC / "agentmeasure_chenhz01_fixture_001.events.jsonl" |
| 54 | +EXPECTED_FILE = VEC / "agentmeasure_chenhz01_fixture_001.expected.json" |
| 55 | +CONTEXT_FILE = VEC / "agentmeasure_chenhz01_fixture_001.trace_context.json" |
| 56 | +SCHEMA_FILE = ROOT / "lab" / "schemas" / "funnel-event.schema.json" |
| 57 | + |
| 58 | +fails: list = [] |
| 59 | + |
| 60 | + |
| 61 | +def check(name: str, cond: bool, detail: str = "") -> None: |
| 62 | + print(f" {'✓' if cond else '✗'} {name}" + (f" — {detail}" if detail and not cond else "")) |
| 63 | + if not cond: |
| 64 | + fails.append(name) |
| 65 | + |
| 66 | + |
| 67 | +def canon(o) -> str: |
| 68 | + return json.dumps(o, sort_keys=True, separators=(",", ":"), ensure_ascii=False) |
| 69 | + |
| 70 | + |
| 71 | +def hcontent(o) -> str: |
| 72 | + return "sha256:" + hashlib.sha256(canon(o).encode("utf-8")).hexdigest() |
| 73 | + |
| 74 | + |
| 75 | +def certify_canonical(op: str, ctx: dict): |
| 76 | + """Recompute certification from raw inputs: canonicalize each field value, |
| 77 | + compare content hashes, record the field path. Independent of x_availability.""" |
| 78 | + results = ctx["results"] |
| 79 | + req = ctx["next_request"][op] |
| 80 | + if not req["emitted"]: |
| 81 | + return "unreachable", None |
| 82 | + target = hcontent(results[op]["content"]) |
| 83 | + for i, m in enumerate(req["messages"] or []): |
| 84 | + val = m.get("content") |
| 85 | + if isinstance(val, str): |
| 86 | + try: |
| 87 | + parsed = json.loads(val) |
| 88 | + except Exception: |
| 89 | + parsed = None |
| 90 | + if parsed is not None and hcontent(parsed) == target: |
| 91 | + return "verbatim", f"messages[{i}].content" |
| 92 | + if ctx["embed_form"][op] == "summary": |
| 93 | + return "summary-embed", ctx["embedded_at"][op] |
| 94 | + return "absent", None |
| 95 | + |
| 96 | + |
| 97 | +def certify_raw_substring(op: str, ctx: dict) -> str: |
| 98 | + """Naive certifier: substring of the canonical result in the re-serialized |
| 99 | + request blob. Breaks on re-serialization — present only to be caught.""" |
| 100 | + results = ctx["results"] |
| 101 | + req = ctx["next_request"][op] |
| 102 | + if not req["emitted"]: |
| 103 | + return "unreachable" |
| 104 | + blob = canon(req) |
| 105 | + if canon(results[op]["content"]) in blob: |
| 106 | + return "verbatim" |
| 107 | + return "summary-embed" if ctx["embed_form"][op] == "summary" else "absent" |
| 108 | + |
| 109 | + |
| 110 | +def main() -> int: |
| 111 | + events = [json.loads(l) for l in EVENTS_FILE.read_text().splitlines() if l.strip()] |
| 112 | + expected = json.loads(EXPECTED_FILE.read_text()) |
| 113 | + ctx = json.loads(CONTEXT_FILE.read_text()) |
| 114 | + schema = json.loads(SCHEMA_FILE.read_text()) |
| 115 | + |
| 116 | + print("chenhz01-001 guard 1: schema validation (FMT-002)") |
| 117 | + # Fail loudly on an empty events file, so the per-event loop below can't |
| 118 | + # pass vacuously (chenhz01, #32 review). |
| 119 | + check("events file is non-empty", len(events) > 0, f"{len(events)} events") |
| 120 | + for i, ev in enumerate(events, 1): |
| 121 | + try: |
| 122 | + validate(ev, schema) |
| 123 | + except SchemaError as e: |
| 124 | + check(f"event {i} valid", False, str(e)) |
| 125 | + check(f"{len(events)} events valid under FMT-002", len(events) > 0) |
| 126 | + |
| 127 | + print("chenhz01-001 guard 2: core boolean consumption_rate (the collapse)") |
| 128 | + cell = aggregate(events)[("availability-cert",)] |
| 129 | + ce = expected["core_expected_metrics"] |
| 130 | + check(f"operations == {ce['operations']}", cell["operations"] == ce["operations"], str(cell["operations"])) |
| 131 | + cr = cell["consumption_rate"] |
| 132 | + check(f"consumption_rate == {ce['consumption_rate']['value']} (= {ce['consumption_rate']['numerator']}/{ce['consumption_rate']['denominator']})", |
| 133 | + cr["value"] == ce["consumption_rate"]["value"] |
| 134 | + and cr["numerator"] == ce["consumption_rate"]["numerator"] |
| 135 | + and cr["denominator"] == ce["consumption_rate"]["denominator"], |
| 136 | + f"got {cr['numerator']}/{cr['denominator']}={cr['value']}") |
| 137 | + check("operation reconciliation passed", cell["operation_reconciliation"]["status"] == "passed", |
| 138 | + cell["operation_reconciliation"]["status"]) |
| 139 | + |
| 140 | + print("chenhz01-001 guard 3: certification recomputed from raw trace_context") |
| 141 | + consumptions = {e["operation_index"]: e for e in events if e["event"] == "consumption"} |
| 142 | + buckets = {"verbatim": 0, "summary-embed": 0, "absent": 0, "unreachable": 0} |
| 143 | + for op_str in ("1", "2", "3", "4"): |
| 144 | + op = int(op_str) |
| 145 | + cert, path = certify_canonical(op_str, ctx) |
| 146 | + buckets[cert] += 1 |
| 147 | + xa = consumptions[op]["x_availability"] |
| 148 | + check(f"op{op} certification == x_availability ({cert})", xa["certification"] == cert, xa["certification"]) |
| 149 | + check(f"op{op} field_path == {path}", xa["embedded_field_path"] == path, str(xa["embedded_field_path"])) |
| 150 | + check(f"op{op} content hash matches", xa["result_content_hash"] == hcontent(ctx["results"][op_str]["content"])) |
| 151 | + check(f"op{op} consumed boolean == state_a", consumptions[op]["consumed"] == (cert == "verbatim")) |
| 152 | + exp_bd = expected["availability_certification"]["breakdown"] |
| 153 | + for k, v in buckets.items(): |
| 154 | + check(f"breakdown[{k}] == {v}", exp_bd[k.replace('-', '_')] == v, str(exp_bd[k.replace('-', '_')])) |
| 155 | + |
| 156 | + print("chenhz01-001 guard 4: re-serialization false negative on op1") |
| 157 | + raw1 = certify_raw_substring("1", ctx) |
| 158 | + good1, _ = certify_canonical("1", ctx) |
| 159 | + check("raw substring misses op1 (absent)", raw1 == "absent", raw1) |
| 160 | + check("canonical field-walk certifies op1 (verbatim)", good1 == "verbatim", good1) |
| 161 | + |
| 162 | + print("chenhz01-001 guard 5: bucket boundary (forbid list)") |
| 163 | + sa = expected["availability_certification"]["state_a_rate"] |
| 164 | + judgeable = buckets["verbatim"] + buckets["summary-embed"] + buckets["absent"] |
| 165 | + check("State-A denominator excludes unreachable", sa["denominator"] == judgeable and sa["denominator"] == 3, |
| 166 | + str(sa["denominator"])) |
| 167 | + check("State-A numerator counts verbatim only", sa["numerator"] == buckets["verbatim"] == 1) |
| 168 | + check("summary-embed is not State A", consumptions[2]["x_availability"]["state_a_certified"] is False |
| 169 | + and consumptions[2]["consumed"] is False) |
| 170 | + check("summary-embed is its own bucket, not absent", |
| 171 | + consumptions[2]["x_availability"]["certification"] == "summary-embed") |
| 172 | + check("unreachable maps to UNOBSERVABLE, not FALSE", |
| 173 | + consumptions[4]["x_availability"]["observability_state"] == "UNOBSERVABLE") |
| 174 | + |
| 175 | + print("chenhz01-001 guard 6: collapse is detectable (UNOBSERVABLE -> FALSE)") |
| 176 | + # Mislabel the unreachable result as absent: invariant 17 violation. The |
| 177 | + # judgeable denominator would wrongly grow from 3 to 4 — the vector must |
| 178 | + # make that change observable rather than silent. |
| 179 | + collapsed = dict(buckets) |
| 180 | + collapsed["unreachable"] -= 1 |
| 181 | + collapsed["absent"] += 1 |
| 182 | + collapsed_den = collapsed["verbatim"] + collapsed["summary-embed"] + collapsed["absent"] |
| 183 | + check("collapsing unreachable->absent changes the denominator (3 -> 4)", |
| 184 | + collapsed_den == 4 and collapsed_den != judgeable, str(collapsed_den)) |
| 185 | + |
| 186 | + if fails: |
| 187 | + print(f"\nCHENHZ01-001 CONFORMANCE FAIL: {len(fails)} guard(s): {', '.join(fails)}") |
| 188 | + return 1 |
| 189 | + print("\nCHENHZ01-001 CONFORMANCE PASS: schema + core collapse + cert + re-serial + boundary + collapse-detectable") |
| 190 | + return 0 |
| 191 | + |
| 192 | + |
| 193 | +if __name__ == "__main__": |
| 194 | + sys.exit(main()) |
0 commit comments