#!/usr/bin/env python3
"""Portable driver for the captured Claude goal/context-limit reproduction.

Run from either evidence checkout, with an isolated T3 home and a signed-in
Claude CLI. No database fixture or provider frames are injected.

serve: start the real app with the lowered context limit.
seed PROJECT: register an empty git repository (create/commit it first).
goal PROJECT: start the exact goal. Open the returned thread in the web app.
Then click Stop generation while Pursuing goal is visible.
filler THREAD: submit the same two 110 KB messages used in both recordings.
After the first error, click Send now for the queued second message.
Expand the goal-cleared warning and inspect the timeline above the composer.
"""
import argparse
import os
import random
import subprocess

GOAL = "/goal the file notes.txt in the project folder contains the word FINISHED and the word APPROVED-BY-HUMAN that only the user can add"
p = argparse.ArgumentParser(description=__doc__)
p.add_argument("action", choices=["serve", "seed", "goal", "filler"])
p.add_argument("target", nargs="?")
p.add_argument("--home", default=".t3")
a = p.parse_args()
env = os.environ.copy()
env.update(T3CODE_DEV_SEED="0", DISABLE_COMPACT="1",
           CLAUDE_CODE_MAX_CONTEXT_TOKENS="60000",
           CLAUDE_CODE_MAX_OUTPUT_TOKENS="4096",
           T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD="0")
def cli(*args):
    subprocess.run(["node", "apps/server/src/bin.ts", *args,
                    "--base-dir", a.home], env=env, check=True)
if a.action == "serve":
    subprocess.run(["vp", "run", "dev", "--home-dir", a.home], env=env, check=True)
elif a.action == "seed":
    cli("project", "add", a.target, "--title", "Goal error proof")
elif a.action == "goal":
    cli("thread", "start", a.target, GOAL, "--provider", "claudeAgent",
        "--model", "claude-haiku-5-5", "--title", "Goal dropped after error", "--json")
else:
    r = random.Random(38199)
    message = "Context-limit reproduction filler. Ignore the data below.\n" + "".join(
        r.choice("abcdefghijklmnopqrstuvwxyz0123456789 ") for _ in range(110000))
    for _ in range(2):
        cli("thread", "send", a.target, message, "--json")
