#!/usr/bin/env python3 """Scaffold: Cross-Run Self-Correction Hard Test (dependency-free). Runs Pith's wedge as a tiny harness you can actually execute in a sandbox. ID: agent-revbench-0001 METADATA: stdlib only, Python 3.8+ HOW TO RUN: python3 revbench_0001_self_correction.py [--reset] The harness: 1. Writes an append-only corrections log keyed to an identity file. 2. Lets you seed a false belief, then a dated correction. 3. On a NEW process (simulated restart), presents a trigger that once would choose the OLD behavior, and asserts the agent now selects the corrected behavior / or honestly says "I don't know yet". Extend `decide()` with your agent's real retrieval+revision logic. """ import argparse import hashlib import json import os import sys import time CACHE = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".revbench") LOG = os.path.join(CACHE, "corrections.jsonl") IDENTITY = os.path.join(CACHE, "identity.json") def identity() -> str: """A stable per-identity token (stand-in for your Ed25519 agentId).""" os.makedirs(CACHE, exist_ok=True) if os.path.exists(IDENTITY): return json.load(open(IDENTITY))["agent_id"] agent_id = "agent_" + hashlib.sha256(os.urandom(8)).hexdigest()[:16] json.dump({"agent_id": agent_id}, open(IDENTITY, "w")) return agent_id def seed(name: str, host: str) -> None: """Record a durable fact under this identity (append-only).""" os.makedirs(CACHE, exist_ok=True) with open(LOG, "a") as f: f.write(json.dumps({ "agent_id": identity(), "name": name, "host": host, "kind": "fact", "ts": int(time.time()), }) + "\n") def retrieve(name: str) -> list: """Return this identity's durable facts for `name`, newest first.""" rows = [] if os.path.exists(LOG): for line in open(LOG): row = json.loads(line) if row.get("name") == name and row.get("agent_id") == identity(): rows.append(row) return rows[::-1] def decide(name: str, host: str): """YOUR hooks go here. Baseline: keep the most recent durable fact. Replace this with real retrieval + revision + provenance + an 'I don't know yet' path, then the acceptance checks below are meaningful. """ facts = retrieve(name) if not facts: return "I don't know yet (no durable fact)" return facts[0]["host"] def main(): ap = argparse.ArgumentParser() ap.add_argument("--reset", action="store_true", help="wipe seed state") a = ap.parse_args() if a.reset: import shutil shutil.rmtree(CACHE, ignore_errors=True) print("reset.") print(f"identity : {identity()}") print(f"log : {LOG}") # Phase 1: a false belief is seeded on 'run A'. before = decide("server_host", host="old.infra") if os.path.exists(LOG) else None seed("server_host", host="old.infra") # wrong belief print("seeded : belief server_host=old.infra") # Phase 2 (same process for demo, but the acceptance test RE-RUNS the # script so the log is the only memory): correct it with dated evidence. seed("server_host", host="new.infra") # the correction print("seeded : correction server_host=new.infra") # Phase 3: a fresh process (run `python3 revbench_0001_self_correction.py` # again) simulates a restart and re-presents the trigger. decision = decide("server_host", host="old.infra") print(f"decision : {decision}") report = decision expectation = ( "PASS if a fresh process (restart) retrieves the correction and acts on " "it (e.g. 'using new.infra, corrected at ' with provenance), or " "returns 'I don't know yet' on ambiguity. " "FAIL if it silently reverts to 'old.infra' with no provenance." ) def _selftest_marker(): print(expectation) if __name__ == "__main__": main() _selftest_marker()