agnes-the-ai-analyst/app/api/me.py
Vojtech 37ad39c8a3
feat(home): status frame on /home (operator-gated, onboarded-only) (#297)
* feat(home): status frame on /home — last sync, sessions, prompts, tokens, projects

Adds the homepage status frame: a 5-card row above the install-hero /
offboard-strip on /home showing the calling user's Last sync (their
last `agnes pull`), Sessions, Prompts, Tokens used, and Projects worked
on, with a 24h/7d pill toggle.

Backed by `GET /api/me/home-stats?window=` (one DuckDB CTE joining
`users` + `usage_session_summary` + `usage_events`) and SSR'd from the
same `compute_home_stats` helper on initial paint so there's no
spinner. The window toggle is the only JS-driven path.

Side surfaces:
- `GET /api/sync/manifest` now stamps `users.last_pull_at` so
  `agnes pull` (and the Claude Code SessionStart hook that wraps it)
  imprints the analyst's last sync time for the new card.
- `usage_session_summary` gains four BIGINT token counters
  (input_tokens, output_tokens, cache_read_tokens, cache_creation_tokens)
  summed from JSONL `message.usage.*` per assistant turn.
- `USAGE_PROCESSOR_VERSION` bumps 1 → 2 so the session-pipeline
  reprocess loop invalidates stale summaries and backfills tokens
  on the next tick.

Schema migration v43 → v44 is idempotent ALTERs (last_pull_at +
4 token columns) — fresh installs receive them from `_SYSTEM_SCHEMA`,
upgrade path runs `_v43_to_v44`. Defaults (NULL / 0) backfill
existing rows cleanly.

9 new tests in tests/test_home_stats.py cover the migration,
endpoint shapes (24h/7d/unknown/empty/missing-user), and the
manifest-side last_pull_at bump.

* docs(CHANGELOG): homepage status frame entries under [Unreleased]

The post-rebase release-cut now belongs to whichever PR lands next
after main rolled to 0.54.9. This PR logs its bullets under
[Unreleased] (Added: homepage status frame, per-user pull tracking,
token counters; Changed: schema v43 → v44 migration) so they ride
out with the next release-cut.

* fix(tests): bump test_schema_v42_migration asserts to v44

CI failed because tests/test_schema_v42_migration.py hardcoded
`assert SCHEMA_VERSION == 43` and `assert v == 43` after init.
v44 (homepage stats frame backing columns) was introduced in the
preceding feat commit; this aligns the existing v42-era migration
tests with the new schema version.

* feat(home): gate status frame on operator flag + user.onboarded

Two gates on the homepage status frame:

1. **Operator master switch** — `get_home_status_frame_visibility()` in
   app/instance_config.py mirrors the existing `get_home_automode_visibility()`
   shape: env var `AGNES_HOME_SHOW_STATUS_FRAME` > yaml
   `instance.home.show_status_frame` > default `True`. Cautious-rollout
   instances can disable the frame without forking; the yaml example
   documents both knobs.

2. **Onboarded gate** — the template only renders the frame when the
   caller's `users.onboarded` is true. First-day users see a clean
   install-hero before all-zero stat cards; the frame appears
   automatically on the next render after `agnes init` POSTs
   `/api/me/onboarded`.

Router skips the `compute_home_stats` DB read entirely when either
gate is closed; `home_stats` arrives at the template as None in that
branch and the `{% if %}` shortcuts the include.

Why both gates: PostHog feature flags evaluated and rejected — this
codebase uses PostHog for analytics capture only, not feature gating;
adding a per-user feature_enabled() call on the /home critical path
would couple the homepage render to a remote eval and still require
an admin master switch. The onboarded gate is a UX coherence rule
layered on top of the operator switch, not an A/B test signal.

3 new tests in test_home_stats.py cover the env-var resolution
(falsey values + default-true). The yaml example gets a `home:`
block documenting both `show_automode` (pre-existing flag, was
undocumented in the example) and `show_status_frame`.
2026-05-14 09:28:47 +00:00

190 lines
6.7 KiB
Python

"""Self-scoped user endpoints for the /home onboarding flow.
POST /api/me/onboarded toggles ``users.onboarded`` for the calling user
and writes an audit_log row distinguishing the trigger source:
- ``agnes_init`` — fired by the CLI's ``agnes init`` final step.
- ``self_acknowledged`` — fired by the on-page "I've already set this up"
button shown to users who set up locally before /home shipped.
- ``self_unmark`` — fired by the on-page "Mark me as offboarded"
button (visible once the user is onboarded).
The body's optional ``onboarded`` field defaults to ``True`` for
backward compat with existing ``agnes init`` calls. Pass ``false`` to
flip back — useful when an analyst wipes their workspace and wants the
inline install steps back, or when an operator demos the not-onboarded
view without an SQL UPDATE.
Idempotent — a second call still returns 200 and writes a second audit
row, so duplicate fires are visible without breaking the client. See
origin: docs/brainstorms/home-page-requirements.md §2 + §6.
"""
from __future__ import annotations
from typing import Literal
import duckdb
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from app.auth.dependencies import _get_db, get_current_user
from src.repositories.audit import AuditRepository
router = APIRouter(prefix="/api/me", tags=["me"])
class OnboardedRequest(BaseModel):
source: Literal["agnes_init", "self_acknowledged", "self_unmark"] = "agnes_init"
onboarded: bool = True
@router.post("/onboarded")
async def post_onboarded(
body: OnboardedRequest = OnboardedRequest(),
user: dict = Depends(get_current_user),
conn: duckdb.DuckDBPyConnection = Depends(_get_db),
):
target = bool(body.onboarded)
conn.execute(
"UPDATE users SET onboarded = ? WHERE id = ?",
[target, user["id"]],
)
AuditRepository(conn).log(
user_id=user["id"],
action="user_onboarded" if target else "user_offboarded",
params={"source": body.source},
result="ok",
)
return {"status": "ok", "onboarded": target}
# ---------------------------------------------------------------------------
# GET /api/me/home-stats — backing data for the /home status frame
# ---------------------------------------------------------------------------
_WINDOW_INTERVALS = {
"24h": "INTERVAL 24 HOUR",
"7d": "INTERVAL 7 DAY",
}
def _username_for_stats(user: dict) -> str:
"""Map a users row to the filesystem username used by the session
collector and stored in ``usage_session_summary.username``.
Mirrors ``app.api.admin_user_sessions._username_from_user``: the
session collector writes JSONL under the OS username of the agent
process which, for current deployments, equals the email local-part.
Kept inline here so this endpoint has no cross-module dependency on
an admin-only helper; if the mapping evolves both copies must update.
"""
email: str = user.get("email", "") or ""
return email.split("@")[0] if "@" in email else email
def compute_home_stats(
conn: duckdb.DuckDBPyConnection, user: dict, window: str = "24h"
) -> dict:
"""Pure helper that returns the home-stats payload for the given user.
Shared by the HTTP endpoint and the /home Jinja handler (server-side
initial render). Unknown windows clamp to ``24h`` so callers never
need to pre-validate. Returns a dict with ISO-stringified
``last_pull_at`` (or None) so the same shape works for both JSON
serialization and Jinja rendering.
"""
interval = _WINDOW_INTERVALS.get(window)
if interval is None:
window = "24h"
interval = _WINDOW_INTERVALS["24h"]
username = _username_for_stats(user)
# f-string interpolates only the validated interval literal above;
# all user-controlled input flows through bound parameters.
sql = f"""
WITH win AS (
SELECT current_timestamp - {interval} AS since
),
sess AS (
SELECT
COUNT(*) AS sessions,
COALESCE(SUM(user_messages), 0) AS prompts,
COALESCE(SUM(input_tokens), 0) AS input_tokens,
COALESCE(SUM(output_tokens), 0) AS output_tokens,
COALESCE(SUM(cache_read_tokens), 0) AS cache_read,
COALESCE(SUM(cache_creation_tokens), 0) AS cache_creation
FROM usage_session_summary, win
WHERE username = ? AND started_at >= win.since
),
proj AS (
SELECT COUNT(DISTINCT cwd) AS projects
FROM usage_events, win
WHERE username = ?
AND cwd IS NOT NULL
AND occurred_at >= win.since
),
u AS (
SELECT last_pull_at FROM users WHERE id = ?
)
SELECT
u.last_pull_at,
sess.sessions, sess.prompts,
sess.input_tokens, sess.output_tokens,
sess.cache_read, sess.cache_creation,
proj.projects
FROM u, sess, proj
"""
row = conn.execute(sql, [username, username, user["id"]]).fetchone()
if row is None:
return {
"window": window,
"last_pull_at": None,
"sessions": 0,
"prompts": 0,
"tokens": {
"input": 0, "output": 0,
"cache_read": 0, "cache_creation": 0,
"total": 0,
},
"projects": 0,
}
(last_pull_at, sessions, prompts,
input_t, output_t, cache_read, cache_creation, projects) = row
return {
"window": window,
"last_pull_at": last_pull_at.isoformat() if last_pull_at else None,
"sessions": int(sessions or 0),
"prompts": int(prompts or 0),
"tokens": {
"input": int(input_t or 0),
"output": int(output_t or 0),
"cache_read": int(cache_read or 0),
"cache_creation": int(cache_creation or 0),
"total": int((input_t or 0) + (output_t or 0)
+ (cache_read or 0) + (cache_creation or 0)),
},
"projects": int(projects or 0),
}
@router.get("/home-stats")
async def get_home_stats(
window: str = "24h",
user: dict = Depends(get_current_user),
conn: duckdb.DuckDBPyConnection = Depends(_get_db),
):
"""Return the five counters rendered in the /home status frame for
the calling user, over a 24-hour or 7-day window.
Single round-trip: one DuckDB query joins ``users``,
``usage_session_summary``, and ``usage_events`` so the homepage
renders without N+1. Missing rows (new user, no telemetry yet)
surface as zeros / null rather than 404 — the frame still renders
cleanly for first-day analysts.
"""
return compute_home_stats(conn, user, window)