akseljoonas HF Staff commited on
Commit
89a00bb
·
1 Parent(s): c99339e

Fix Ctrl+C at idle prompt: no false interrupt, no stuck state

Browse files
Files changed (1) hide show
  1. agent/main.py +8 -6
agent/main.py CHANGED
@@ -779,6 +779,7 @@ async def main():
779
 
780
  submission_id = [0]
781
  last_interrupt_time = 0.0
 
782
 
783
  try:
784
  while True:
@@ -788,6 +789,7 @@ async def main():
788
  except asyncio.CancelledError:
789
  break
790
  turn_complete_event.clear()
 
791
 
792
  # Get user input
793
  try:
@@ -797,16 +799,14 @@ async def main():
797
  except KeyboardInterrupt:
798
  now = time.monotonic()
799
  if now - last_interrupt_time < 3.0:
800
- print("\nDouble Ctrl+C, exiting...")
801
  break
802
  last_interrupt_time = now
803
- # If agent is busy, cancel it
804
  session = session_holder[0]
805
- if session and not turn_complete_event.is_set():
806
  session.cancel()
807
- print("\nInterrupting agent...")
808
  else:
809
- print("\n(Ctrl+C again within 3s to exit)")
810
  turn_complete_event.set()
811
  continue
812
 
@@ -829,6 +829,7 @@ async def main():
829
  turn_complete_event.set()
830
  continue
831
  else:
 
832
  await submission_queue.put(sub)
833
  continue
834
 
@@ -840,10 +841,11 @@ async def main():
840
  op_type=OpType.USER_INPUT, data={"text": user_input}
841
  ),
842
  )
 
843
  await submission_queue.put(submission)
844
 
845
  except KeyboardInterrupt:
846
- print("\n\nInterrupted by user")
847
 
848
  # Shutdown
849
  shutdown_submission = Submission(
 
779
 
780
  submission_id = [0]
781
  last_interrupt_time = 0.0
782
+ agent_busy = False # True only while the agent is processing a submission
783
 
784
  try:
785
  while True:
 
789
  except asyncio.CancelledError:
790
  break
791
  turn_complete_event.clear()
792
+ agent_busy = False
793
 
794
  # Get user input
795
  try:
 
799
  except KeyboardInterrupt:
800
  now = time.monotonic()
801
  if now - last_interrupt_time < 3.0:
 
802
  break
803
  last_interrupt_time = now
804
+ # If agent is actually working, cancel it
805
  session = session_holder[0]
806
+ if agent_busy and session:
807
  session.cancel()
 
808
  else:
809
+ get_console().print("[dim]Ctrl+C again to exit[/dim]")
810
  turn_complete_event.set()
811
  continue
812
 
 
829
  turn_complete_event.set()
830
  continue
831
  else:
832
+ agent_busy = True
833
  await submission_queue.put(sub)
834
  continue
835
 
 
841
  op_type=OpType.USER_INPUT, data={"text": user_input}
842
  ),
843
  )
844
+ agent_busy = True
845
  await submission_queue.put(submission)
846
 
847
  except KeyboardInterrupt:
848
+ pass
849
 
850
  # Shutdown
851
  shutdown_submission = Submission(