Files

102 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""The committed mirror-replay fixtures (``scripts/ab/mirror_fixtures``) through
BOTH the sidecar's legacy mirror and this plugin's own (loaded by
``harness_loader`` with ``SIDECAR_HARNESS_BUILTINS=0``), every replay mode
(oneshot / stepwise / chunked / restart), frozen exactly as
``scripts/ab/_mirror_worker.py`` freezes them. Prints JSON:
``{mode: {"same": bool, "files": n, "lines": n, "plugin": files?, "legacy": files?}}``.
python replay_parity.py <plugin dir> [--sidecar DIR]
"""
from __future__ import annotations
import argparse
import json
import os
import pathlib
import random
import shutil
import sys
import tempfile
MODES = ("oneshot", "stepwise", "chunked", "restart")
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("plugin", type=pathlib.Path)
ap.add_argument("--sidecar", type=pathlib.Path, default=pathlib.Path(
os.environ.get("AI_AGENT_SIDECAR") or pathlib.Path.home() / "projects/ai-agent/sidecar"))
args = ap.parse_args()
plugin = args.plugin.resolve()
sidecar = args.sidecar.resolve()
ab = sidecar.parent / "scripts" / "ab"
mf = json.loads((plugin / "plugin.json").read_text())
harness = mf["harness"]["harnessId"]
sys.path.insert(0, str(ab))
import _mirror_worker as mw
from mirror_fixtures import agy as FX_AGY
from mirror_fixtures import opencode as FX_OC
# _mirror_worker.freeze()'s environment: relative dirs resolved in each
# replay's own directory (the mirrors' uuid5 of a path is then stable).
os.environ["SIDECAR_AGY_DIR"] = "runner"
os.environ["SIDECAR_AGY_ACCOUNTS"] = f"{FX_AGY.ACCOUNT}=home"
os.environ["SIDECAR_LOG_DIR"] = "logs"
os.environ["SIDECAR_HARNESS_BUILTINS"] = "0"
os.environ["SIDECAR_USAGE_BUILTINS"] = "0"
live = pathlib.Path(tempfile.mkdtemp(prefix="replay-live-"))
shutil.copytree(plugin / "sidecar", live / mf["id"] / "sidecar",
ignore=shutil.ignore_patterns("__pycache__"))
shutil.copy2(plugin / "plugin.json", live / mf["id"] / "plugin.json")
os.environ["SIDECAR_LIVE_HARNESSES_DIR"] = str(live)
sys.path.insert(0, str(sidecar))
import harness_loader
import pids
out = harness_loader.LOADER.reload(start=False)
assert out["ok"], out
pkg = "sidecar_ext.harness." + mf["id"].replace("-", "_")
ours = sys.modules[f"{pkg}.mirror"]
legacy = __import__("opencode_mirror" if harness == "opencode" else "agy_mirror")
assert ours is not legacy
for mod in (legacy, ours):
mod.time = mw._fake_time()
mod.datetime = mw._fake_datetime()
mod._git_branch = lambda cwd: "replay-branch"
work = pathlib.Path(tempfile.mkdtemp(prefix="replay-parity-"))
res: dict = {}
try:
if harness == "opencode":
ops, name, key = FX_OC.OPS, "fixture", "opencode:fixture"
else:
ops, name, key = FX_AGY.OPS, "fixture", "agy:fixture"
for mode in MODES:
got = {}
for side, mod in (("legacy", legacy), ("plugin", ours)):
rng = random.Random(f"1:{key}:{mode}")
cuts = mw.cuts_for(mode, len(ops), 8, rng)
if harness == "opencode":
r = mw.oc_replay(mod, work, f"{name}-{side}", FX_OC.ACCOUNT, ops, mode, cuts)
else:
r = mw.agy_replay(mod, pids, work, f"{name}-{side}", ops, mode, cuts, rng)
got[side] = r["files"]
same = got["legacy"] == got["plugin"]
res[mode] = {"same": same, "files": len(got["plugin"]),
"lines": sum(len(v) for v in got["plugin"].values())}
if not same:
res[mode].update(got)
finally:
os.chdir("/")
shutil.rmtree(work, ignore_errors=True)
shutil.rmtree(live, ignore_errors=True)
json.dump(res, sys.stdout)
return 0
if __name__ == "__main__":
sys.exit(main())