"""Queen aggregation (agent_swarm#8/#12, M2). Verifies the Queen's best-of-N selection: among the fan-out agents' candidates, the one whose impl passes the most tests wins; selection is deterministic; honest fail-closed (unscored != zero). """ import os import sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) os.environ.setdefault("REDIS_FAKE", "1") os.environ.setdefault("ALLOW_MEMORY_STORE", "1") os.environ.setdefault("OPENAI_API_KEY", "test-key") import asyncio from orchestrator.queen import Candidate, select_best, should_bounce, _auth_url, promote_to_main _failures = [] def check(name, cond): print(("PASS" if cond else "FAIL") + " - " + name) if not cond: _failures.append(name) F = ["impl.py"] # non-empty impl marker # highest pass_rate wins (best-of-N) check("highest score wins", select_best([Candidate("t1", "a1", F, score=40.0), Candidate("t2", "a2", F, score=90.0)]).task_id == "t2") # scored ranks above unscored check("scored beats unscored", select_best([Candidate("t1", "a1", F), Candidate("t2", "a2", F, score=10.0)]).task_id == "t2") # no impl anywhere → None (nothing to deliver) check("no impl → None", select_best([Candidate("t1", "a1", [])]) is None) # all unscored → deterministic fallback to first with impl check("all unscored → first with impl", select_best([Candidate("t1", "a1", F), Candidate("t2", "a2", F)]).task_id == "t1") # tie on score → stable (first input order wins) check("score tie → stable first", select_best([Candidate("t1", "a1", F, score=100.0), Candidate("t2", "a2", F, score=100.0)]).task_id == "t1") # tie on score, more passed wins check("score tie → more passed wins", select_best([Candidate("t1", "a1", F, score=100.0, passed=2, total=2), Candidate("t2", "a2", F, score=100.0, passed=5, total=5)]).task_id == "t2") # --- should_bounce (M3/SC-9 quality gate decision) --- def _summary(score): return {"winner": {"task_id": "t1", "score": score}, "candidates": [{"task_id": "t1"}]} # below threshold + under cap → bounce check("below bar under cap → bounce", should_bounce(_summary(40.0), 80.0, cycles=0, max_cycles=2) is True) # meets bar → accept check("meets bar → no bounce", should_bounce(_summary(90.0), 80.0, cycles=0, max_cycles=2) is False) # cap reached → accept best-so-far check("cap reached → no bounce", should_bounce(_summary(40.0), 80.0, cycles=2, max_cycles=2) is False) # no threshold (disabled) → never bounce check("no threshold → no bounce", should_bounce(_summary(0.0), None, cycles=0, max_cycles=2) is False) # unscored → honest, don't bounce check("unscored → no bounce", should_bounce(_summary(None), 80.0, cycles=0, max_cycles=2) is False) # --- SC-7 promote_to_main --- # _auth_url embeds + URL-encodes creds; passthrough when missing check("auth url embeds + encodes creds", _auth_url("http://h/r.git", "u", "p@ss") == "http://u:p%40ss@h/r.git") check("auth url passthrough w/o creds", _auth_url("http://h/r.git", None, None) == "http://h/r.git") class _RunNoGrant: def __init__(self): self.metadata = {} self.swarm_id = "s1" # promote is a no-op (not an error) when the run has no git grant _promo = asyncio.run(promote_to_main(_RunNoGrant(), [], "t1")) check("promote without grant → not promoted", _promo.get("promoted") is False and _promo.get("reason") == "no_git_grant") if _failures: print(f"\nFAILED: {len(_failures)} check(s): {_failures}") raise SystemExit(1) print("\nALL PASSED")