"""Install the same deterministic ACP proof fixture in a disposable checkout.
Usage: python3 install-fixture.py CHECKOUT /absolute/path/mcp-proof.mjs
Only changes the replay agent; server implementation stays at the chosen commit.
"""
import json
import sys
from pathlib import Path
checkout, helper = map(Path, sys.argv[1:])
p = checkout / 'apps/server/scripts/dev-replay-agent.ts'
s = p.read_text()
needle = '        const turns = loadTurns(session.model);'
start = s.find('        const promptText = JSON.stringify(params.prompt ?? []);')
if start >= 0:
    end = s.index(needle, start)
    s = s[:start] + s[end:]
inject = '''        const promptText = JSON.stringify(params.prompt ?? []);
        if (promptText.includes("RUN_MCP_RECOVERY_PROOF")) {
          const { prove } = await import(HELPER);
          const result = await prove();
          send({ method: "session/update", params: { sessionId, update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: result } } } });
          return { stopReason: "end_turn" };
        }
        if (promptText.includes("Reply DONE")) {
          send({ method: "session/update", params: { sessionId, update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "DONE" } } } });
          return { stopReason: "end_turn" };
        }
'''.replace('HELPER', json.dumps(str(helper.resolve())))
assert needle in s
p.write_text(s.replace(needle, inject + needle))
