Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions autobot-backend/llc/services/controls_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ControlsService:
All methods are synchronous from the caller's perspective: the state change
is committed before the method returns. Redis flags are best-effort (failures
are logged, not raised) because the DB state is authoritative.
A flag follows the activity write, so a failed write leaves no flag behind (#20150).
"""

def __init__(self, activity_log: Optional[LLCActivityLogService] = None) -> None:
Expand Down Expand Up @@ -120,8 +121,6 @@ async def pause_agent(
},
)

await self._set_redis_flag(_AGENT_PAUSED_KEY.format(agent_id=agent_id), "1")

await self._activity_log.record(
session=session,
company_id=company_id,
Expand All @@ -134,6 +133,8 @@ async def pause_agent(
after={"status": LLCAgentStatus.PAUSED.value, "pause_reason": reason},
)

await self._set_redis_flag(_AGENT_PAUSED_KEY.format(agent_id=agent_id), "1")

return {"agent_id": agent_id, "status": LLCAgentStatus.PAUSED.value, "pause_reason": reason}

async def resume_agent(
Expand Down Expand Up @@ -169,8 +170,6 @@ async def resume_agent(
},
)

await self._del_redis_flag(_AGENT_PAUSED_KEY.format(agent_id=agent_id))

await self._activity_log.record(
session=session,
company_id=company_id,
Expand All @@ -183,6 +182,8 @@ async def resume_agent(
after={"status": restore_status},
)

await self._del_redis_flag(_AGENT_PAUSED_KEY.format(agent_id=agent_id))

return {"agent_id": agent_id, "status": restore_status}

async def terminate_agent(
Expand Down Expand Up @@ -214,9 +215,6 @@ async def terminate_agent(
},
)

# Terminated agents also get the Redis pause flag so heartbeat skips them
await self._set_redis_flag(_AGENT_PAUSED_KEY.format(agent_id=agent_id), "terminated")

await self._activity_log.record(
session=session,
company_id=company_id,
Expand All @@ -229,6 +227,9 @@ async def terminate_agent(
after={"status": LLCAgentStatus.TERMINATED.value, "reason": reason},
)

# Terminated agents also get the Redis pause flag so heartbeat skips them
await self._set_redis_flag(_AGENT_PAUSED_KEY.format(agent_id=agent_id), "terminated")

return {"agent_id": agent_id, "status": LLCAgentStatus.TERMINATED.value}

async def set_agent_heartbeat(
Expand Down Expand Up @@ -353,8 +354,6 @@ async def pause_company(
"""Set company-wide Redis pause flag — blocks all new heartbeat runs."""
if await self._get_company_row(session, company_id) is None:
raise CompanyNotFoundError(company_id)
await self._set_redis_flag(_COMPANY_PAUSED_KEY.format(company_id=company_id), reason or "1")

await self._activity_log.record(
session=session,
company_id=company_id,
Expand All @@ -366,6 +365,8 @@ async def pause_company(
after={"reason": reason},
)

await self._set_redis_flag(_COMPANY_PAUSED_KEY.format(company_id=company_id), reason or "1")

return {"company_id": company_id, "paused": True}

async def resume_company(
Expand All @@ -377,8 +378,6 @@ async def resume_company(
"""Lift company-wide pause flag."""
if await self._get_company_row(session, company_id) is None:
raise CompanyNotFoundError(company_id)
await self._del_redis_flag(_COMPANY_PAUSED_KEY.format(company_id=company_id))

await self._activity_log.record(
session=session,
company_id=company_id,
Expand All @@ -390,6 +389,8 @@ async def resume_company(
after={"paused": False},
)

await self._del_redis_flag(_COMPANY_PAUSED_KEY.format(company_id=company_id))

return {"company_id": company_id, "paused": False}

# ------------------------------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions autobot-backend/llc/tests/test_controls.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,18 @@ async def test_resume_company_raises_company_not_found():

with pytest.raises(CompanyNotFoundError):
await svc.resume_company(session, company_id, actor_id)


@pytest.mark.asyncio
@pytest.mark.parametrize("control", ["pause_agent", "terminate_agent", "resume_agent"])
async def test_a_failed_activity_write_changes_no_redis_flag_20150(control):
log = _activity_log_mock()
log.record = AsyncMock(side_effect=RuntimeError("activity write failed"))
svc = ControlsService(activity_log=log)
session = _mock_session(rows=[("paused", "available")])
redis = AsyncMock()
with patch("llc.services.controls_service.get_async_redis_client", AsyncMock(return_value=redis)):
with pytest.raises(RuntimeError):
await getattr(svc, control)(session, str(uuid.uuid4()), "agent-1", str(uuid.uuid4()))
redis.set.assert_not_awaited()
redis.delete.assert_not_awaited()