import argparse,json,pathlib,subprocess
p=argparse.ArgumentParser();p.add_argument('checkout');p.add_argument('home');a=p.parse_args()
root=pathlib.Path(a.checkout); home=pathlib.Path(a.home)
agent=root/'apps/server/scripts/dev-replay-agent.ts'
agent.write_text(agent.read_text().replace('"usage_update",','"usage_update", "compaction_update",'))
frames=[{'type':'expect_outbound','frame':{'kind':'request','method':'session/prompt','params':{}}}]
def update(u): frames.append({'type':'emit_inbound','afterMs':100,'frame':{'kind':'notification','method':'session/update','params':{'sessionId':'synthetic-session','update':u}}})
for i in range(1,4):
 update({'sessionUpdate':'tool_call','toolCallId':f'read-{i}','kind':'read','title':f'Read phase-{i}.txt','status':'completed','rawInput':{'path':f'phase-{i}.txt'},'rawOutput':f'Phase {i} verified.'})
 if i<3:
  update({'sessionUpdate':'usage_update','used':100000 if i==1 else 90000,'size':200000})
  update({'sessionUpdate':'compaction_update','compactionId':f'compact-{i}','status':'in_progress'})
  update({'sessionUpdate':'compaction_update','compactionId':f'compact-{i}','status':'completed','summary':[{'type':'text','text':f'Phase {i} context retained.'}]})
update({'sessionUpdate':'agent_message_chunk','content':{'type':'text','text':'Review complete. Three files checked; context compacted twice during this turn.'}})
frames.append({'type':'emit_inbound','frame':{'kind':'response','method':'session/prompt','result':{'stopReason':'end_turn'}}})
(root/'apps/server/scripts/dev-replay/showcase_transcript.ndjson').write_text('\n'.join(map(json.dumps,frames))+'\n')
workspace=home/'workspaces'/'Compaction demo';workspace.mkdir(parents=True,exist_ok=True)
for i in range(1,4): (workspace/f'phase-{i}.txt').write_text(f'Phase {i} synthetic review material.\n')
def run(cmd,cwd=root):
 r=subprocess.run(cmd,cwd=cwd,capture_output=True,text=True)
 if r.returncode: raise RuntimeError(r.stderr)
 return r.stdout
run(['git','init','--quiet','--initial-branch=main'],workspace);run(['git','add','.'],workspace);run(['git','-c','user.name=Scenario','-c','user.email=scenario@example.invalid','commit','--allow-empty','-qm','Initial sample'],workspace)
def cli(args): return run(['node','apps/server/src/bin.ts',*args,'--base-dir',str(home)])
cli(['project','add',str(workspace),'--title','Compaction demo'])
result=cli(['thread','start',str(workspace),'Review the three phases, retaining context between phases.','--title','Two compactions in one turn','--provider','devReplay','--model','showcase','--wait','--json'])
(home/'seed-result.json').write_text(result)
print('Synthetic thread ready')
