fix(calendar): /agent/status idempotent + 409 on bad transition
Same-state transition was 500 (transition_to_busy asserts current=IDLE). Now: short-circuit identical target → 200 no_change=true. Any other state-machine violation surfaces as 409 with the actual error message instead of generic 500. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -52,6 +52,7 @@ from app.schemas.calendar import (
|
|||||||
)
|
)
|
||||||
from app.services.agent_heartbeat import get_pending_slots_for_agent
|
from app.services.agent_heartbeat import get_pending_slots_for_agent
|
||||||
from app.services.agent_status import (
|
from app.services.agent_status import (
|
||||||
|
AgentStatusError,
|
||||||
record_heartbeat,
|
record_heartbeat,
|
||||||
transition_to_busy,
|
transition_to_busy,
|
||||||
transition_to_idle,
|
transition_to_idle,
|
||||||
@@ -594,6 +595,13 @@ def update_agent_status(
|
|||||||
):
|
):
|
||||||
agent = _require_agent(db, payload.agent_id, payload.claw_identifier)
|
agent = _require_agent(db, payload.agent_id, payload.claw_identifier)
|
||||||
target = (payload.status or '').lower().strip()
|
target = (payload.status or '').lower().strip()
|
||||||
|
# Idempotent same-state transition: a 'busy → busy' request is a
|
||||||
|
# no-op rather than a 500. Lets plugin status gates / cli `--set`
|
||||||
|
# be safe to fire-and-forget without first reading current state.
|
||||||
|
current = agent.status.value if hasattr(agent.status, 'value') else str(agent.status)
|
||||||
|
if current == target:
|
||||||
|
return {"ok": True, "agent_id": agent.agent_id, "status": current, "no_change": True}
|
||||||
|
try:
|
||||||
if target == AgentStatus.IDLE.value:
|
if target == AgentStatus.IDLE.value:
|
||||||
transition_to_idle(db, agent)
|
transition_to_idle(db, agent)
|
||||||
elif target == AgentStatus.BUSY.value:
|
elif target == AgentStatus.BUSY.value:
|
||||||
@@ -607,6 +615,11 @@ def update_agent_status(
|
|||||||
transition_to_exhausted(db, agent, reason=reason, recovery_at=payload.recovery_at)
|
transition_to_exhausted(db, agent, reason=reason, recovery_at=payload.recovery_at)
|
||||||
else:
|
else:
|
||||||
raise HTTPException(status_code=400, detail="Unsupported agent status")
|
raise HTTPException(status_code=400, detail="Unsupported agent status")
|
||||||
|
except AgentStatusError as e:
|
||||||
|
# State-machine violation (e.g. busy → busy via wrong precondition)
|
||||||
|
# → 409 with the rejected transition explained, instead of a 500.
|
||||||
|
db.rollback()
|
||||||
|
raise HTTPException(status_code=409, detail=str(e))
|
||||||
db.commit()
|
db.commit()
|
||||||
return {"ok": True, "agent_id": agent.agent_id, "status": agent.status.value if hasattr(agent.status, 'value') else str(agent.status)}
|
return {"ok": True, "agent_id": agent.agent_id, "status": agent.status.value if hasattr(agent.status, 'value') else str(agent.status)}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user