anil988 commited on
Commit
1fe9dc5
·
1 Parent(s): 949e05c

Refactor check_approval to hang the connection

Browse files
Files changed (1) hide show
  1. main.py +18 -10
main.py CHANGED
@@ -912,20 +912,28 @@ APPROVALS = {
912
  @app.post("/api/agent/approval-request")
913
  async def check_approval(req: ApprovalRequest):
914
  """
915
- Maestro polls this endpoint.
916
- If approved, returns True (Maestro loop breaks and continues).
917
- If not approved, broadcasts the 'paused' event and returns False.
918
  """
919
  gate = req.gate.lower()
920
- is_approved = APPROVALS.get(gate, False)
921
 
922
- # If not approved yet, broadcast the pause event to the React dashboard (only once every few seconds)
923
- if not is_approved:
924
- agent_idx = 1 if gate == "triage" else 7
925
- agent_name = "Gate 1: Triage" if gate == "triage" else "Gate 2: BSA Officer"
926
- await broadcaster.broadcast("agent_pause", {"agent": agent_idx, "name": agent_name})
 
 
 
 
 
 
 
 
 
927
 
928
- return {"status": "success", "approved": is_approved}
929
 
930
 
931
  @app.get("/approve", response_class=HTMLResponse)
 
912
  @app.post("/api/agent/approval-request")
913
  async def check_approval(req: ApprovalRequest):
914
  """
915
+ Maestro fires a single HTTP POST here.
916
+ We hang (wait) internally until the human clicks approve in our UI,
917
+ forcing Maestro to pause on this step naturally.
918
  """
919
  gate = req.gate.lower()
 
920
 
921
+ # Broadcast the pause event to the React dashboard
922
+ agent_idx = 1 if gate == "triage" else 7
923
+ agent_name = "Gate 1: Triage" if gate == "triage" else "Gate 2: BSA Officer"
924
+ await broadcaster.broadcast("agent_pause", {"agent": agent_idx, "name": agent_name})
925
+
926
+ # Loop indefinitely until human approves (up to 120 seconds for safety)
927
+ timeout = 120
928
+ elapsed = 0
929
+ while not APPROVALS.get(gate, False) and elapsed < timeout:
930
+ await asyncio.sleep(2)
931
+ elapsed += 2
932
+
933
+ # Reset for next run
934
+ APPROVALS[gate] = False
935
 
936
+ return {"status": "success", "approved": True}
937
 
938
 
939
  @app.get("/approve", response_class=HTMLResponse)