"""English edition. Not part of the success count. For each condition-A trial, replace only the guessed host URL with the real one
and run again, to see whether the endpoint URL was the only thing the chat model could not know.
Usage: op run --env-file experiments/aib.env -- experiments/2_1_workspace/.venv/bin/python experiments_en/2_1_workspace/counterfactual_url.py
"""
import json, os, pathlib, shutil, subprocess, sys
HERE = pathlib.Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
from check import check
GUESSES = ['https://api.ai-builder.space/v1', 'https://api.ai-builder.run/v1']
REAL = 'https://space.ai-builders.com/backend/v1'
out = {}
for t in sorted((HERE / 'trials').glob('A*')):
    code = (t / 'run' / 'script.py').read_text(encoding='utf-8')
    found = [g for g in GUESSES if g in code]
    if not found:
        out[t.name] = 'guessed URL not found; skipped'
        continue
    d = t / 'counterfactual_url_fixed'
    if d.exists():
        shutil.rmtree(d)
    shutil.copytree(HERE / 'fixture' / 'memos', d / 'memos')
    (d / 'script.py').write_text(code.replace(found[0], REAL), encoding='utf-8')
    env = {k: v for k, v in os.environ.items() if k in ('PATH', 'HOME', 'LANG', 'AI_BUILDER_TOKEN', 'TMPDIR')}
    p = subprocess.run([str(HERE.parents[1] / 'experiments' / '2_1_workspace' / '.venv' / 'bin' / 'python'), 'script.py'], cwd=d, env=env, capture_output=True, text=True, timeout=600)
    (d / 'stdout.txt').write_text(p.stdout, encoding='utf-8'); (d / 'stderr.txt').write_text(p.stderr, encoding='utf-8')
    c = check(d)
    out[t.name] = {'guessed': found[0], 'exit_code': p.returncode, 'success': c['success'], 'n_ok': c['n_ok'],
                   'similarity': {k: v.get('similarity') for k, v in c['files'].items()}}
(HERE / 'counterfactual_url.json').write_text(json.dumps(out, ensure_ascii=False, indent=1), encoding='utf-8')
print(json.dumps(out, ensure_ascii=False, indent=1))
