"""A scripted agent fixes a streak bug. The harness, not the agent, decides what counts as done."""
BUGGY = "def streak(days):\n    return sum(days)\n"
HALF = "def streak(days):\n    return sum(days[-7:])\n"
FIXED = "def streak(days):\n    n = 0\n    for done in reversed(days):\n        if not done:\n            break\n        n += 1\n    return n\n"

# Each scripted turn is (code the agent writes, what the agent claims).
TURNS = [(BUGGY, "I reviewed the code. Done."), (HALF, "Fixed and done."), (FIXED, "Done.")]

# 1 = habit completed, 0 = missed day. The last value is today.
CASES = [([1, 1, 1], 3), ([1, 1, 0, 1], 1), ([1, 1, 0], 0)]


def check(code):
    scope = {}
    exec(code, scope)
    return [(d, want, scope["streak"](d)) for d, want in CASES if scope["streak"](d) != want]


def run(gate, max_turns=5):
    for turn, (code, claim) in enumerate(TURNS[:max_turns], 1):
        failures = check(code)
        print(f"turn {turn}: agent says {claim!r} | failed cases: {len(failures)}")
        if not gate or not failures:
            return "accepted", turn, len(failures)
    return "stopped", turn, len(failures)


print("No gate: ", run(gate=False))
print("With gate:", run(gate=True))
print("Gate, 2-turn limit:", run(gate=True, max_turns=2))
