"""The A2A-MetaTrace orchestrator and variant-based classes, offline. A scripted stage runner stands in for real agent servers, so these cover the generation contracts that must hold before any server exists: the orchestrator emits schema-valid records; the class label (not the variant) becomes ``task_class``; classes stay balanced across their variants; every message carries a variant tag that maps back to its class; and every variant stage has a serving capability. """ from __future__ import annotations from corpus.classes import CLASSES, all_variants, capabilities, class_of from corpus.orchestrator import REQUIRED_FIELDS, run_corpus, run_workflow def _scripted_runner(variant, instance, stage_idx, capability, client_id): """Discovery round-trip + request + class-dependent updates (stands in for a server).""" provider = f"agent-{abs(hash(capability)) % 9000 + 1000}" t = float(stage_idx) recs = [ {"step_type": "discovery_query", "direction": "c2s", "src": client_id, "dst": "registry", "t": t, "length": 40, "label_visible": True}, {"step_type": "discovery_result", "direction": "s2c", "src": "registry", "dst": client_id, "t": t + 0.01, "length": 60, "label_visible": False}, {"step_type": "request", "direction": "c2s", "src": client_id, "dst": provider, "t": t + 0.02, "length": 200, "label_visible": False}, ] n_updates = 1 + (len(capability) + stage_idx) % 4 for u in range(n_updates): recs.append({"step_type": "update", "direction": "s2c", "src": provider, "dst": client_id, "t": t + 0.03 + 0.01 * u, "length": 1500, "label_visible": False}) recs.append({"step_type": "response", "direction": "s2c", "src": provider, "dst": client_id, "t": t + 0.1, "length": 800, "label_visible": False}) return recs def test_records_schema_and_class_label() -> None: cls = CLASSES[0] variant = cls.variants[0] recs = run_workflow(variant, 0, task_class=cls.name, trace_id=0, client_id="client-001", stage_runner=_scripted_runner) assert recs for r in recs: assert REQUIRED_FIELDS <= r.keys() assert r["task_class"] == cls.name # the CLASS, not the variant assert r["variant"] == variant.name # variant rides along for grouping def test_corpus_balanced_across_classes() -> None: cap = run_corpus(CLASSES, runs_per_class=12, stage_runner=_scripted_runner) # one workflow per trace_id; its class is constant across the trace's messages class_of_trace = {m["trace_id"]: m["task_class"] for m in cap["messages"]} counts: dict[str, int] = {} for cls_name in class_of_trace.values(): counts[cls_name] = counts.get(cls_name, 0) + 1 assert set(counts) == {c.name for c in CLASSES} # each class gets exactly runs_per_class workflows (split across its variants) assert all(v == 12 for v in counts.values()) def test_variant_tag_present_for_every_message() -> None: cap = run_corpus(CLASSES, runs_per_class=6, stage_runner=_scripted_runner) variant_names = {v.name for _, v in all_variants()} assert all(m["variant"] in variant_names for m in cap["messages"]) # variant maps back to its class for m in cap["messages"]: assert class_of(m["variant"]) == m["task_class"] def test_capabilities_cover_all_variant_stages() -> None: caps = set(capabilities()) for _, v in all_variants(): assert set(v.stages) <= caps