ohmyapi commited on
Commit
7db8c45
·
1 Parent(s): d3a42f2

fix: cookie SameSite for HF proxy, Jobs truncation, Integrations timeout

Browse files
emergent2api/backends/integrations.py CHANGED
@@ -43,7 +43,7 @@ class IntegrationsBackend(EmergentBackend):
43
  try:
44
  async with httpx.AsyncClient(
45
  transport=transport,
46
- timeout=httpx.Timeout(120.0, connect=15.0),
47
  ) as client:
48
  if stream:
49
  async with client.stream(
 
43
  try:
44
  async with httpx.AsyncClient(
45
  transport=transport,
46
+ timeout=httpx.Timeout(180.0, connect=30.0),
47
  ) as client:
48
  if stream:
49
  async with client.stream(
emergent2api/backends/jobs.py CHANGED
@@ -63,9 +63,10 @@ class JobsBackend(EmergentBackend):
63
  consecutive_unchanged = 0
64
  confirmed_end = False
65
  has_received_anything = False
66
- max_wait_cycles = 400
 
 
67
 
68
- # Initial wait to let the container spin up
69
  await asyncio.sleep(8.0)
70
 
71
  for _ in range(max_wait_cycles):
@@ -77,7 +78,12 @@ class JobsBackend(EmergentBackend):
77
 
78
  data_items = result.get("data", [])
79
 
80
- # Find the longest text across all items (main content)
 
 
 
 
 
81
  current_full_text = ""
82
  current_thinking = ""
83
  for item in data_items:
@@ -89,14 +95,12 @@ class JobsBackend(EmergentBackend):
89
  if reasoning and len(reasoning) > len(current_thinking):
90
  current_thinking = reasoning
91
 
92
- # Yield thinking delta
93
  if current_thinking and len(current_thinking) > len(full_thinking):
94
  delta = current_thinking[len(full_thinking):]
95
  full_thinking = current_thinking
96
  if stream and delta:
97
  yield {"type": "thinking", "content": delta}
98
 
99
- # Yield text delta
100
  if len(current_full_text) > len(full_text):
101
  delta = current_full_text[len(full_text):]
102
  full_text = current_full_text
@@ -108,17 +112,26 @@ class JobsBackend(EmergentBackend):
108
  else:
109
  consecutive_unchanged += 1
110
 
111
- if has_received_anything and full_text and consecutive_unchanged >= 6:
 
 
 
 
 
 
112
  if not confirmed_end:
113
  logger.debug("Content stable, confirming end...")
114
  confirmed_end = True
115
- await asyncio.sleep(5.0)
116
  continue
117
  logger.info("Stream completed (double-confirmed stable)")
118
  break
119
 
120
  await self._dynamic_sleep(consecutive_unchanged)
121
 
 
 
 
122
  if not has_received_anything:
123
  logger.warning("Stream finished with NO content")
124
 
 
63
  consecutive_unchanged = 0
64
  confirmed_end = False
65
  has_received_anything = False
66
+ job_completed = False
67
+ max_wait_cycles = 600
68
+ start_time = time.time()
69
 
 
70
  await asyncio.sleep(8.0)
71
 
72
  for _ in range(max_wait_cycles):
 
78
 
79
  data_items = result.get("data", [])
80
 
81
+ # Check for explicit completion signal in trajectory items
82
+ for item in data_items:
83
+ status = (item.get("traj_payload") or {}).get("status", "")
84
+ if status in ("completed", "done", "finished"):
85
+ job_completed = True
86
+
87
  current_full_text = ""
88
  current_thinking = ""
89
  for item in data_items:
 
95
  if reasoning and len(reasoning) > len(current_thinking):
96
  current_thinking = reasoning
97
 
 
98
  if current_thinking and len(current_thinking) > len(full_thinking):
99
  delta = current_thinking[len(full_thinking):]
100
  full_thinking = current_thinking
101
  if stream and delta:
102
  yield {"type": "thinking", "content": delta}
103
 
 
104
  if len(current_full_text) > len(full_text):
105
  delta = current_full_text[len(full_text):]
106
  full_text = current_full_text
 
112
  else:
113
  consecutive_unchanged += 1
114
 
115
+ if job_completed and has_received_anything:
116
+ logger.info("Stream completed (job status=completed)")
117
+ break
118
+
119
+ # Require more stability cycles and longer confirmation for complex queries
120
+ stable_threshold = 15 if len(full_text) < 500 else 10
121
+ if has_received_anything and full_text and consecutive_unchanged >= stable_threshold:
122
  if not confirmed_end:
123
  logger.debug("Content stable, confirming end...")
124
  confirmed_end = True
125
+ await asyncio.sleep(8.0)
126
  continue
127
  logger.info("Stream completed (double-confirmed stable)")
128
  break
129
 
130
  await self._dynamic_sleep(consecutive_unchanged)
131
 
132
+ elapsed = time.time() - start_time
133
+ logger.info(f"Job finished: {len(full_text)} chars in {elapsed:.1f}s")
134
+
135
  if not has_received_anything:
136
  logger.warning("Stream finished with NO content")
137
 
emergent2api/routes/admin.py CHANGED
@@ -50,7 +50,10 @@ async def login(request: Request):
50
  token = secrets.token_urlsafe(32)
51
  _sessions.add(token)
52
  resp = JSONResponse(content={"ok": True})
53
- resp.set_cookie(COOKIE_NAME, token, httponly=True, samesite="lax", max_age=86400 * 7)
 
 
 
54
  return resp
55
 
56
 
 
50
  token = secrets.token_urlsafe(32)
51
  _sessions.add(token)
52
  resp = JSONResponse(content={"ok": True})
53
+ resp.set_cookie(
54
+ COOKIE_NAME, token, httponly=True, samesite="none",
55
+ secure=True, max_age=86400 * 7, path="/",
56
+ )
57
  return resp
58
 
59