agnes-the-ai-analyst/cli/commands/diagnose.py
ZdenekSrotyr 1563b05f2e refactor(cli): hard-cutover env vars + config dir to AGNES_*
Task 0.5 of clean-analyst-bootstrap. Greenfield rewrite — no fallback,
no aliases. Existing dev environments lose their cached PAT and must
re-authenticate.

Env var renames (hard cutover):
- DA_CONFIG_DIR    -> AGNES_CONFIG_DIR
- DA_SERVER        -> AGNES_SERVER
- DA_SERVER_URL    -> AGNES_SERVER_URL  (test-only stale ref, not in spec)
- DA_NO_UPDATE_CHECK -> AGNES_NO_UPDATE_CHECK
- DA_LOCAL_DIR     -> AGNES_LOCAL_DIR
- DA_TOKEN         -> AGNES_TOKEN
- DA_STREAM_RETRIES -> AGNES_STREAM_RETRIES

Config dir rename: ~/.config/da/ -> ~/.config/agnes/ (across code,
comments, docstrings, error messages, install templates, dev scripts).

Stale `da X` references in CLI source (and adjacent app/, tests/):
swept docstrings, comments, help text, and error messages where the
verb survives the rewrite (init, pull, push, catalog, status, diagnose,
auth, admin, skills, query, schema, describe, explore, disk-info,
snapshot, login, logout, whoami, server, setup) and replaced `da X`
with `agnes X`. Intentionally kept `da sync`, `da fetch`, `da analyst`,
`da metrics` — those verbs are removed in later tasks; the legacy
strings will be detected by `_LEGACY_STRINGS` (added in Task 2).

Test fixes:
- TestCLIVersion now asserts output starts with `agnes ` (was `da `).

Test results: 2675 passed, 25 skipped (full pytest run, excluding 9
pre-existing test_db.py / test_user_management.py / test_e2e_extract.py
/ test_cli_binary_rename.py failures unrelated to this rename).
2026-05-04 16:35:44 +02:00

81 lines
2.7 KiB
Python

"""Diagnose command — agnes diagnose."""
import json
import typer
from cli.client import api_get
diagnose_app = typer.Typer(help="System diagnostics")
@diagnose_app.callback(invoke_without_command=True)
def diagnose(
symptom: str = typer.Option(None, "--symptom", help="Describe the problem"),
component: str = typer.Option(None, "--component", help="Check specific component"),
as_json: bool = typer.Option(False, "--json", help="Output as JSON"),
):
"""Run comprehensive system diagnostics. AI-agent friendly output."""
checks = []
# 1. API reachability
try:
resp = api_get("/api/health")
health = resp.json()
checks.append({"name": "api", "status": "ok", "latency_ms": resp.elapsed.total_seconds() * 1000})
# Detailed health (auth required) for service-level checks
try:
resp_d = api_get("/api/health/detailed")
detailed = resp_d.json()
for svc_name, svc_data in detailed.get("services", {}).items():
check = {"name": svc_name, "status": svc_data.get("status", "unknown")}
check.update({k: v for k, v in svc_data.items() if k != "status"})
checks.append(check)
except Exception:
# Auth may not be configured — minimal reachability is sufficient
pass
except Exception as e:
checks.append({"name": "api", "status": "error", "detail": str(e)})
# Determine overall
overall = "healthy"
for c in checks:
if c["status"] == "error":
overall = "unhealthy"
break
if c["status"] == "warning":
overall = "degraded"
# Generate suggested actions
actions = []
for c in checks:
if c["status"] == "error" and c["name"] == "api":
actions.append("Server unreachable. Check: docker compose ps, agnes server logs")
if c.get("stale_tables"):
for t in c["stale_tables"]:
actions.append(f"Table '{t}' is stale. Run: agnes server logs scheduler | grep {t}")
result = {
"overall": overall,
"checks": checks,
"suggested_actions": actions,
}
if as_json:
typer.echo(json.dumps(result, indent=2))
else:
typer.echo(f"Overall: {overall}")
for c in checks:
detail = ""
if "detail" in c:
detail = f"{c['detail']}"
if "tables" in c:
detail = f" ({c['tables']} tables)"
if "latency_ms" in c:
detail = f" ({c['latency_ms']:.0f}ms)"
typer.echo(f" [{c['status']:7s}] {c['name']}{detail}")
if actions:
typer.echo("\nSuggested actions:")
for a in actions:
typer.echo(f" - {a}")