Gate background tasks behind foreground activity

This commit is contained in:
pewdiepie-archdaemon
2026-06-29 13:52:52 +00:00
parent 783ea99bd0
commit 840e59cd05
5 changed files with 174 additions and 3 deletions
+30 -3
View File
@@ -735,11 +735,21 @@ class TaskScheduler:
try:
if bypass_model_slot or not self._task_needs_model_slot(task_id):
await self._execute_task_locked(task_id, run_id, release_executing=release_executing)
await self._execute_task_locked(
task_id,
run_id,
release_executing=release_executing,
gate_foreground=not bypass_model_slot,
)
return
async with self._run_semaphore:
await self._execute_task_locked(task_id, run_id, release_executing=release_executing)
await self._execute_task_locked(
task_id,
run_id,
release_executing=release_executing,
gate_foreground=True,
)
except asyncio.CancelledError:
# If cancellation happens while queued behind the semaphore,
# _execute_task_locked never runs and cannot update the Activity row.
@@ -753,7 +763,14 @@ class TaskScheduler:
async with self._executing_lock:
self._executing.discard(task_id)
async def _execute_task_locked(self, task_id: str, run_id: str, *, release_executing: bool = True):
async def _execute_task_locked(
self,
task_id: str,
run_id: str,
*,
release_executing: bool = True,
gate_foreground: bool = True,
):
from core.database import SessionLocal, ScheduledTask, TaskRun
db = SessionLocal()
@@ -770,6 +787,14 @@ class TaskScheduler:
db.commit()
return
if gate_foreground:
waiting = db.query(TaskRun).filter(TaskRun.id == run_id).first()
if waiting and waiting.status == "queued":
waiting.result = "Queued — waiting for Odysseus to be idle…"
db.commit()
from src.interactive_gate import wait_for_interactive_quiet
await wait_for_interactive_quiet(f"scheduled task {task.name}")
# Flip the run from queued → running. Reset started_at to the
# actual execution start so queue wait time is visible from
# created_at vs started_at if we ever surface that.
@@ -1764,6 +1789,8 @@ class TaskScheduler:
# behind the primary endpoint so a downed primary won't silently yield
# `(no output)`.
try:
from src.interactive_gate import wait_for_interactive_quiet
await wait_for_interactive_quiet(f"agent task {task.name}")
from src.task_endpoint import resolve_task_candidates
_task_fallbacks = resolve_task_candidates(
fallback_url=endpoint_url,