import sys,json,uuid,threading,subprocess

children=[]
def sleep_command():
 child=subprocess.Popen(["sleep","400"]); children.append(child); child.wait()
def send(m):
 print(json.dumps({'jsonrpc':'2.0',**m}),flush=True)
def handle(m):
 p=m.get('params',{}); method=m.get('method'); result={}
 if method=='initialize': result={'protocolVersion':1,'agentCapabilities':{'loadSession':True,'promptCapabilities':{}},'authMethods':[],'agentInfo':{'name':'restart-proof-replay','version':'1'}}
 elif method in ['session/new','session/load']:
  result={'models':{'currentModelId':'sleep','availableModels':[{'modelId':'sleep','name':'Unfinished sleep turn'}]}}
  if method=='session/new': result['sessionId']=str(uuid.uuid4())
 elif method=='session/prompt':
  sid=p['sessionId']; tool=str(uuid.uuid4())
  def update(u): send({'method':'session/update','params':{'sessionId':sid,'update':u}})
  update({'sessionUpdate':'agent_message_chunk','content':{'type':'text','text':'Holding this unfinished turn: running sleep 400 in the foreground.\n'}})
  update({'sessionUpdate':'tool_call','toolCallId':tool,'title':'sleep 400','kind':'execute','status':'in_progress','rawInput':{'command':'sleep 400'}})
  sleep_command()
  # Keep the turn unfinished even after the first harmless command.
  while True: sleep_command()
 if 'id' in m: send({'id':m['id'],'result':result})
for line in sys.stdin:
 try: m=json.loads(line)
 except: continue
 threading.Thread(target=handle,args=(m,),daemon=True).start()

for child in children:
 if child.poll() is None: child.terminate(); child.wait()
