Files

203 lines
8.9 KiB
Nim

## G7 head-to-head + no-regression floor (CHIMERA M6). LIVE, Java required.
##
## Three binaries built from the SAME tree:
## * inert — `-d:chimeraInert` == the M1 exact copy (all CHIMERA off)
## * full — default (probes + fusion + influence)
## * noprobe — `-d:chimeraNoProbes` (fusion + influence, no probes)
##
## Head-to-head: full vs inert mirror 1v1, `H2HN` matches x `H2HRounds`.
## Bar: head-to-head win-rate >= 50% (exact, reported).
##
## Suite no-regression floor: each arm vs the 8 primary sample bots,
## `Rounds` rounds each. Bar: full >= inert on EVERY opponent (win rate), with
## survival and score margin reported; the full-minus-probes arm isolates the
## probe cost.
##
## A full < inert anywhere is reported plainly as a falsification, never hidden.
## Run: `nim c -r --hints:off -d:release tests/bench_g7.nim`
import std/[os, osproc, strutils, sequtils, math, tables, algorithm, times]
import bench_common
const
Rounds = 5
H2HN = 10 # head-to-head mirror matches
H2HRounds = 5 # rounds per head-to-head match
type
Outcome = object
rounds, wins: int
ourScore, oppScore: float64
ourSurv, oppSurv: int
Arm = object
tag, name: string
defines: seq[string]
const Arms = [
Arm(tag: "inert", name: "ChimeraInert", defines: @["-d:chimeraInert"]),
Arm(tag: "full", name: "ChimeraFull", defines: @[]),
Arm(tag: "noprobe", name: "ChimeraNoProbe", defines: @["-d:chimeraNoProbes"]),
]
proc parseOutcome(log, ourName: string): Outcome =
## Aggregate a battle's JSONL runner output. A failed battle (no round_ended)
## yields rounds == 0.
for ln in log.splitLines():
if ln.len == 0 or ln.find("\"event\":\"round_ended\"") < 0: continue
inc result.rounds
let ri = ln.find("\"results\":[")
if ri < 0: continue
let oi = ln.find('[', ri)
let ci = ln.find(']', ri)
if oi < 0 or ci < 0 or ci <= oi: continue
let seg = ln[oi + 1 ..< ci]
var j = 0
var ourScoreV = -1.0
var theirScoreV = -1.0
var ourSurvF, theirSurvF = false
while j < seg.len:
let s = seg.find('{', j)
if s < 0: break
let e = seg.find('}', s)
if e < 0: break
let obj = seg[s + 1 ..< e]
let (found, name) = extract(obj, "name")
let (sf, score) = extract(obj, "score")
let (vf, surv) = extract(obj, "survived")
if found and sf and vf:
if name == ourName:
ourScoreV = score.parseFloat()
ourSurvF = surv == "true"
else:
theirScoreV = score.parseFloat()
theirSurvF = surv == "true"
j = e + 1
if ourScoreV >= 0.0 and theirScoreV >= 0.0:
result.ourScore += ourScoreV
result.oppScore += theirScoreV
if ourScoreV > theirScoreV: inc result.wins
if ourSurvF: inc result.ourSurv
if theirSurvF: inc result.oppSurv
proc runBattle(url: string, botDirs: seq[string], rounds: int,
ourName, tag: string): Outcome =
## Launch one battle, route telemetry to a throwaway dir, parse and return.
putEnv("CHIMERA_TELEMETRY_DIR", getTempDir() / ("chimera_g7_tel_" & tag))
let (ok, logPath) = runBattleHardLog(url, botDirs, rounds,
logName = "chimera_g7_" & tag)
if not ok: return Outcome()
parseOutcome(readFile(logPath), ourName)
proc winRate(o: Outcome): float64 =
if o.rounds == 0: 0.0 else: 100.0 * float(o.wins) / float(o.rounds)
proc margin(o: Outcome): float64 = o.ourScore - o.oppScore
proc main() =
guardSkip()
echo "[G7] staging 3 arms: " & Arms.mapIt(it.tag &
"=" & (if it.defines.len == 0: "(default)" else: it.defines.join(","))).join(" ")
var bots = initTable[string, string]()
for a in Arms:
bots[a.tag] = stageBot("g7_" & a.tag, a.defines, a.name)
var lines: seq[string]
lines.add("# G7 head-to-head + no-regression floor (M6)\n")
lines.add("Three arms from the SAME tree: **inert** (`-d:chimeraInert`, == M1 " &
"exact copy), **full** (default: probes + fusion + influence), " &
"**noprobe** (`-d:chimeraNoProbes`: fusion + influence). Head-to-head: " &
"full vs inert mirror 1v1, " & $H2HN & " matches x " & $H2HRounds &
" rounds. Suite: " & $Rounds & " rounds per arm per opponent vs the 8 " &
"primary sample bots. Generated by `tests/bench_g7.nim`.\n")
# ── head-to-head ──────────────────────────────────────────────────────────
restartServer()
var url = serverUrl()
var h2h: Outcome
echo "\n[G7] head-to-head full vs inert, ", H2HN, " matches x ", H2HRounds, " rounds"
for m in 1 .. H2HN:
var o: Outcome
for attempt in 1 .. MaxAttempts:
removeDir(getTempDir() / "chimera_g7_h2h_tel"); sleep(300)
o = runBattle(url, @[bots["full"], bots["inert"]], H2HRounds,
"ChimeraFull", "h2h")
if o.rounds > 0: break
echo " h2h match ", m, " attempt ", attempt, ": retry"
killStaleBots(); restartServer(); url = serverUrl()
h2h.wins += o.wins; h2h.rounds += o.rounds
h2h.ourScore += o.ourScore; h2h.oppScore += o.oppScore
h2h.ourSurv += o.ourSurv; h2h.oppSurv += o.oppSurv
echo " match ", m, " -> ", o.rounds, " rounds, full wins ", o.wins
let h2hWin = winRate(h2h)
echo "\n[G7] head-to-head: full win ", formatFloat(h2hWin, ffDecimal, 1),
"% over ", h2h.rounds, " rounds, margin ", formatFloat(margin(h2h), ffDecimal, 1),
", survival full ", h2h.ourSurv, " vs inert ", h2h.oppSurv
echo " G7 head-to-head bar (>=50%): ", (if h2hWin >= 50.0: "PASS" else: "FAIL")
lines.add "## Head-to-head: full vs inert\n"
lines.add "| metric | value |"
lines.add "|---|---:|"
lines.add "| matches | " & $H2HN & " x " & $H2HRounds & " rounds |"
lines.add "| full win-rate | " & formatFloat(h2hWin, ffDecimal, 1) & "% |"
lines.add "| full survival | " & $h2h.ourSurv & " / inert " & $h2h.oppSurv & " |"
lines.add "| score margin (full-inert) | " & formatFloat(margin(h2h), ffDecimal, 1) & " |"
lines.add "| **bar (>=50%)** | " & (if h2hWin >= 50.0: "**PASS**" else: "**FAIL**") & " |"
# ── suite no-regression ───────────────────────────────────────────────────
let opps = sorted(toSeq(Expected.keys))
var suite = initTable[string, Table[string, Outcome]]() # arm -> opp -> out
for arm in Arms:
var t = initTable[string, Outcome]()
for opp in opps:
restartServer()
putEnv("CHIMERA_OPPONENT", opp)
let o = runBattle(serverUrl(), @[bots[arm.tag], SampleRoot / opp], Rounds,
arm.name, arm.tag & "_" & opp)
t[opp] = o
echo " [", arm.tag, "] vs ", alignLeft(opp, 10), " rounds=", o.rounds,
" winRate=", formatFloat(winRate(o), ffDecimal, 0), "%",
" margin=", formatFloat(margin(o), ffDecimal, 1)
suite[arm.tag] = t
echo "\n=== G7 suite no-regression (", Rounds, " rounds/arm/opponent) ==="
echo "Opponent fullW% fullM fullS inertW% inertM inertS noprW% noprM"
lines.add "\n## Suite no-regression floor (" & $Rounds & " rounds per arm)\n"
lines.add "| opponent | full W% | full margin | full surv | inert W% | inert margin | inert surv | noProbe W% | noProbe margin |"
lines.add "|---|---:|---:|---:|---:|---:|---:|---:|---:|"
var allOk = true
var failedOpps: seq[string]
for opp in opps:
let f = suite["full"].getOrDefault(opp)
let i = suite["inert"].getOrDefault(opp)
let n = suite["noprobe"].getOrDefault(opp)
let okRow = (f.rounds > 0 and i.rounds > 0) and (f.wins >= i.wins)
if not okRow: allOk = false; failedOpps.add opp
echo " ", alignLeft(opp, 10),
align(formatFloat(winRate(f), ffDecimal, 0), 7),
align(formatFloat(margin(f), ffDecimal, 1), 6),
align($f.ourSurv, 6),
align(formatFloat(winRate(i), ffDecimal, 0), 8),
align(formatFloat(margin(i), ffDecimal, 1), 6),
align($i.ourSurv, 6),
align(formatFloat(winRate(n), ffDecimal, 0), 7),
" ", formatFloat(margin(n), ffDecimal, 1)
lines.add "| " & opp & " | " & formatFloat(winRate(f), ffDecimal, 1) & " | " &
formatFloat(margin(f), ffDecimal, 1) & " | " & $f.ourSurv & " | " &
formatFloat(winRate(i), ffDecimal, 1) & " | " &
formatFloat(margin(i), ffDecimal, 1) & " | " & $i.ourSurv & " | " &
formatFloat(winRate(n), ffDecimal, 1) & " | " &
formatFloat(margin(n), ffDecimal, 1) & " |"
lines.add "\n**No-regression (full >= inert on every opponent): " &
(if allOk: "PASS" else: "**FAIL** on " & failedOpps.join(", ")) & "**. "
lines.add "Head-to-head win rate " & formatFloat(h2hWin, ffDecimal, 1) &
"% (bar >=50%: " & (if h2hWin >= 50.0: "PASS" else: "FAIL") & ")."
let art = currentSourcePath().parentDir.parentDir / "benchmarks" / "m6_g7.md"
createDir(art.parentDir)
writeFile(art, lines.join("\n") & "\n")
echo "\nArtifact: ", art
when isMainModule:
main()