fix(internal_endpoints): drive session busy<->ready oscillation from daemon callbacks

Two call sites added:
  * After _claim_next_queued_run flips a queued run to running, call
    refresh_session_status_from_runs so a session that was 'ready'
    transitions to 'busy'. Idempotent for already-busy sessions.
  * After daemon_append_event finalizes a terminal event (run_finished /
    run_cancelled / run_error), call refresh_session_status_from_runs so
    a session with no remaining active runs transitions back to 'ready'.

Without these hooks, session.status stayed permanently 'busy' after the
first run started — clients polling GET /v2/sessions/{id} always saw
'busy' regardless of run state.

Greptile P1 (review #PRR_kwDOKALCgc78u9En).
This commit is contained in:
Ishaan Jaffer 2026-05-06 15:51:24 -07:00
parent f68869071b
commit f3101be1a1
No known key found for this signature in database

View file

@ -33,6 +33,9 @@ from litellm.proxy.agent_session_endpoints.constants import (
SESSION_STATUS_PROVISIONING,
SESSION_STATUS_READY,
)
from litellm.proxy.agent_session_endpoints.session_status import (
refresh_session_status_from_runs,
)
from litellm.proxy.agent_session_endpoints.schemas import (
DaemonHeartbeatRequest,
DaemonRegisterRequest,
@ -185,6 +188,11 @@ async def daemon_next_run(
while True:
claimed = await _claim_next_queued_run(prisma_client, session_id)
if claimed is not None:
# Run just transitioned queued -> running; if the session is
# still ``ready``, flip it to ``busy``. Idempotent — if the
# session is already ``busy`` (e.g. concurrent runs), this
# is a no-op via ``derive_session_status_from_runs``.
await refresh_session_status_from_runs(prisma_client, session_id)
return NextRunResponse(
run_id=claimed.id,
prompt=claimed.prompt or {},
@ -282,6 +290,10 @@ async def daemon_append_event(
),
},
)
# Drive the session ``busy`` -> ``ready`` flip. Without this,
# the parent session stays permanently ``busy`` after the first
# run terminates — Greptile P1.
await refresh_session_status_from_runs(prisma_client, session_id)
return {
"run_id": run_id,