Files
roboco/roboco/api/routes/system.py
T
Renn F 17ec52d1b7 feat(conventions): generalize defaults, backfill old projects, adopt the standard in-repo
Harden the architectural-conventions standard so it works out-of-the-box on
any project and resolves for projects that predate it, and make RoboCo pass
its own gate.

General defaults (apply to every project, not just one with a tuned file):
- The auto-scan excludes test and documentation trees (tests/, docs/) — those
  legitimately define fixtures and aren't enforced code.
- Helper placement seeds at warn, not block: `helper` matches any top-level
  function, too blunt a signal to hard-block a route file's small private glue.
  Misplaced model/route/component stay block; the body-level thin_routes check
  remains the real fat-handler guard.
- thin_routes no longer counts transaction-lifecycle calls (commit/flush/
  refresh) as data access — an explicit `db.commit()` after delegating to a
  service is a valid pattern.
- no_lint_suppressions exempts a small allowlist of structurally-unavoidable
  framework codes (ruff TC001-TC003, pydantic prop-decorator); bare or other
  suppressions still flag.
- CLAUDE.md rule-lifting skips bare common-word tokens that would match
  everywhere (e.g. "commit"), keeping only specific identifiers.
- The ambient prompt block lists only constrained modules and truncates at a
  line boundary with a "+N more" pointer instead of cutting mid-line.

Backfill: the standard previously read the committed file + repo scan from
project.workspace_path, a field only a manual API call set — so an older
project (or one whose workspace was cleared) showed an empty "missing" map no
matter what was pushed. The service now ensures a dedicated, default-branch
read clone on demand (WorkspaceService.ensure_read_clone) and resolves from
it, persisting the resolved path + real HEAD. The panel tab, the spawn-time
ambient block, and the per-task constraints all resolve the committed standard
with no manual setup.

Adopt in-repo: relocate the inline request/response models from the system and
*_live route modules into roboco/api/schemas/ so the codebase passes its own
placement gate, and ship a canonical .roboco/conventions.yml. no_models_in_routes
and modular_cohesion are now clean and enforced at block.

Docs updated across the user guide, the agent-facing RAG standard, the
developer and pr_reviewer role prompts, CLAUDE.md, and the changelog. New unit
tests cover the scan exclusions, helper-warn, the suppression allowlist, the
commit exemption, and the resolve/backfill path; the conventions + project
integration suites pass against Postgres.
2026-06-22 18:15:19 +02:00

66 lines
2.2 KiB
Python

"""System monitoring endpoints.
Provides read-only introspection into orchestrator-level state that is
useful for operators and the control panel but doesn't fit cleanly into
the per-resource routers (agents, tasks, etc.).
Currently exposed:
GET /api/system/rate-limits
Returns the current per-provider rate-limit state from Redis,
shaped for the control panel's rate-limit store.
"""
from __future__ import annotations
from datetime import datetime, timedelta
from fastapi import APIRouter
from roboco.api.schemas.system import RateLimitEntry, RateLimitListResponse
from roboco.services.gateway.rate_limit_tracker import RateLimitStateTracker
router = APIRouter()
def _resume_at(hit_at: str | None, retry_after: float | None) -> str | None:
"""Estimated lift time = hit_at + retry_after, ISO; falls back to hit_at."""
if not hit_at or retry_after is None:
return hit_at
try:
lifted = datetime.fromisoformat(hit_at) + timedelta(seconds=retry_after)
except (ValueError, TypeError):
return hit_at
return lifted.isoformat()
@router.get(
"/rate-limits",
summary="List per-provider rate-limit state",
response_model=RateLimitListResponse,
tags=["System"],
)
async def get_rate_limits() -> RateLimitListResponse:
"""Return rate-limit state for every currently rate-limited provider.
Backed by
:class:`~roboco.services.gateway.rate_limit_tracker.RateLimitStateTracker`.
Shaped as the panel's rate-limit store consumes it — a
``{ "entries": [...] }`` envelope where each entry is
``{provider, affectedAgents, hitAt, resumeAt, retryAfterSeconds}``.
``entries`` is empty when no provider is currently rate-limited.
"""
states = await RateLimitStateTracker.list_rate_limited_providers()
entries = [
RateLimitEntry(
provider=provider,
affected_agents=state.get("affected_agents", []),
hit_at=state.get("activated_at"),
resume_at=_resume_at(state.get("activated_at"), state.get("retry_after")),
retry_after_seconds=state.get("retry_after"),
)
for provider, state in states
]
return RateLimitListResponse(entries=entries)