akseljoonas HF Staff commited on
Commit
94e0915
·
1 Parent(s): 1d590c5

feat: cleanup sandbox spaces on session end and increase cookie TTL

Browse files

Delete sandbox HF Spaces when sessions end or get deleted.
Increase auth cookie TTL from 24h to 7 days.
Add token map entries for new models.

agent/core/session.py CHANGED
@@ -19,12 +19,14 @@ logger = logging.getLogger(__name__)
19
  # on network calls for certain providers (known litellm issue).
20
  _MAX_TOKENS_MAP: dict[str, int] = {
21
  # Anthropic
 
22
  "anthropic/claude-opus-4-5-20251101": 200_000,
23
  "anthropic/claude-sonnet-4-5-20250929": 200_000,
24
  "anthropic/claude-sonnet-4-20250514": 200_000,
25
  "anthropic/claude-haiku-3-5-20241022": 200_000,
26
  "anthropic/claude-3-5-sonnet-20241022": 200_000,
27
  "anthropic/claude-3-opus-20240229": 200_000,
 
28
  "huggingface/novita/minimax/minimax-m2.1": 196_608,
29
  "huggingface/novita/moonshotai/kimi-k2.5": 262_144,
30
  "huggingface/novita/zai-org/glm-5": 200_000,
 
19
  # on network calls for certain providers (known litellm issue).
20
  _MAX_TOKENS_MAP: dict[str, int] = {
21
  # Anthropic
22
+ "anthropic/claude-opus-4-6": 200_000,
23
  "anthropic/claude-opus-4-5-20251101": 200_000,
24
  "anthropic/claude-sonnet-4-5-20250929": 200_000,
25
  "anthropic/claude-sonnet-4-20250514": 200_000,
26
  "anthropic/claude-haiku-3-5-20241022": 200_000,
27
  "anthropic/claude-3-5-sonnet-20241022": 200_000,
28
  "anthropic/claude-3-opus-20240229": 200_000,
29
+ "huggingface/fireworks-ai/MiniMaxAI/MiniMax-M2.5": 200_000,
30
  "huggingface/novita/minimax/minimax-m2.1": 196_608,
31
  "huggingface/novita/moonshotai/kimi-k2.5": 262_144,
32
  "huggingface/novita/zai-org/glm-5": 200_000,
backend/routes/auth.py CHANGED
@@ -142,7 +142,7 @@ async def oauth_callback(
142
  httponly=True,
143
  secure=is_production, # Secure flag only in production (HTTPS)
144
  samesite="lax",
145
- max_age=3600 * 24, # 24 hours
146
  path="/",
147
  )
148
  return response
 
142
  httponly=True,
143
  secure=is_production, # Secure flag only in production (HTTPS)
144
  samesite="lax",
145
+ max_age=3600 * 24 * 7, # 7 days
146
  path="/",
147
  )
148
  return response
backend/session_manager.py CHANGED
@@ -164,6 +164,17 @@ class SessionManager:
164
  logger.info(f"Created session {session_id} for user {user_id}")
165
  return session_id
166
 
 
 
 
 
 
 
 
 
 
 
 
167
  async def _run_session(
168
  self,
169
  session_id: str,
@@ -218,6 +229,8 @@ class SessionManager:
218
  except asyncio.CancelledError:
219
  pass
220
 
 
 
221
  async with self._lock:
222
  if session_id in self.sessions:
223
  self.sessions[session_id].is_active = False
@@ -307,6 +320,9 @@ class SessionManager:
307
  if not agent_session:
308
  return False
309
 
 
 
 
310
  # Cancel the task if running
311
  if agent_session.task and not agent_session.task.done():
312
  agent_session.task.cancel()
 
164
  logger.info(f"Created session {session_id} for user {user_id}")
165
  return session_id
166
 
167
+ @staticmethod
168
+ async def _cleanup_sandbox(session: Session) -> None:
169
+ """Delete the sandbox Space if one was created for this session."""
170
+ sandbox = getattr(session, "sandbox", None)
171
+ if sandbox and getattr(sandbox, "_owns_space", False):
172
+ try:
173
+ logger.info(f"Deleting sandbox {sandbox.space_id}...")
174
+ await asyncio.to_thread(sandbox.delete)
175
+ except Exception as e:
176
+ logger.warning(f"Failed to delete sandbox {sandbox.space_id}: {e}")
177
+
178
  async def _run_session(
179
  self,
180
  session_id: str,
 
229
  except asyncio.CancelledError:
230
  pass
231
 
232
+ await self._cleanup_sandbox(session)
233
+
234
  async with self._lock:
235
  if session_id in self.sessions:
236
  self.sessions[session_id].is_active = False
 
320
  if not agent_session:
321
  return False
322
 
323
+ # Clean up sandbox Space before cancelling the task
324
+ await self._cleanup_sandbox(agent_session.session)
325
+
326
  # Cancel the task if running
327
  if agent_session.task and not agent_session.task.done():
328
  agent_session.task.cancel()