#!/usr/bin/env python3
"""Deterministic Copilot-shaped ACP fixture; no live model, no copied user data.
Early untagged metadata, then delayed untagged history on session/load.
Run as the local ACP executable named copilot so native task routing is used.
"""
import json, sys, time, threading
S='evidence-session'
lock=threading.Lock()
def out(v):
 with lock:
  print(json.dumps(dict(jsonrpc='2.0', **v)), flush=True)
def update(u): out(dict(method='session/update',params=dict(sessionId=S,update=u)))
def raw(t,d,a=None): out(dict(method='github.com/copilot/sessionEvent',params=dict(sessionId=S,type=t,data=d,**({'agentId':a} if a else {}))))
def text(t):
 raw('assistant.message_delta',dict(deltaContent=t))
 update(dict(sessionUpdate='agent_message_chunk',content=dict(type='text',text=t)))
def tasks(status):
 for i,title in [('a','Inspect alpha'),('b','Inspect beta')]:
  update(dict(sessionUpdate='tool_call',toolCallId='task-'+i,title=title,kind='other',status=status,rawInput=dict(description=title,prompt=title+'.',agent_type='explore',mode='sync'),**({'rawOutput':dict(content=title+' complete.')} if status=='completed' else {})))
  if status!='completed': raw('subagent.started',dict(toolCallId='task-'+i),'agent-'+i)
def complete():
 for i in ['a','b']:
  update(dict(sessionUpdate='tool_call_update',toolCallId='task-'+i,status='completed',rawOutput=dict(content='Inspection complete.')))
  raw('subagent.completed',dict(toolCallId='task-'+i),'agent-'+i)
config=[dict(id='model',name='Model',category='model',type='select',currentValue='auto',options=[dict(value='auto',name='Auto')])]
resumed=False
def handle(q):
 global resumed
 m=q.get('method'); r={}
 if m=='initialize': r=dict(protocolVersion=1,agentInfo=dict(name='Evidence ACP fixture',version='1'),agentCapabilities=dict(loadSession=True,promptCapabilities={}),authMethods=[])
 elif m=='session/new': r=dict(sessionId=S,configOptions=config)
 elif m=='session/load':
  resumed=True
  update(dict(sessionUpdate='available_commands_update',availableCommands=[dict(name='compact',description='Compact history')]))
  time.sleep(5)
  text('Historical answer: both inspections are complete.')
  tasks('in_progress')
  for _ in range(8):
   time.sleep(1)
   for i in ['a','b']:
    update(dict(sessionUpdate='tool_call_update',toolCallId='task-'+i,status='in_progress'))
  complete()
  r=dict(configOptions=config)
 elif m=='session/prompt':
  if not resumed:
   tasks('in_progress'); time.sleep(3.5); complete(); text('Historical answer: both inspections are complete.')
  else:
   text('New answer: resumed prompt only.'); time.sleep(12)
  r=dict(stopReason='end_turn')
 elif m=='session/set_config_option': r=dict(configOptions=config)
 elif m=='session/set_model': r={}
 elif m=='session/cancel': return
 if 'id' in q: out(dict(id=q['id'],result=r))
for line in sys.stdin:
 try: q=json.loads(line)
 except Exception: continue
 threading.Thread(target=handle,args=(q,),daemon=True).start()
