* 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`.
108 lines
4.6 KiB
Python
108 lines
4.6 KiB
Python
"""v41 → v42 migration: 7 new usage_* tables for telemetry."""
|
|
import duckdb
|
|
import pytest
|
|
from src.db import _ensure_schema as init_database, SCHEMA_VERSION
|
|
|
|
|
|
def test_schema_version_is_42():
|
|
# v44 bumped by PR #297 (homepage stats frame backing columns) — keep
|
|
# this assertion in lockstep with `_SYSTEM_SCHEMA` SCHEMA_VERSION
|
|
# constant. Test name preserved for git-blame continuity; the
|
|
# version-pinned tests in test_db_schema_version.py and
|
|
# test_home_stats.py carry the v44 commentary.
|
|
assert SCHEMA_VERSION == 44
|
|
|
|
|
|
def test_v42_tables_exist_after_init(tmp_path):
|
|
db_path = tmp_path / "test.duckdb"
|
|
conn = duckdb.connect(str(db_path))
|
|
init_database(conn)
|
|
tables = {row[0] for row in conn.execute("SELECT table_name FROM information_schema.tables WHERE table_schema='main'").fetchall()}
|
|
for tbl in [
|
|
"usage_events", "usage_session_summary",
|
|
"usage_tool_daily", "usage_plugin_daily",
|
|
"usage_attribution_skills", "usage_attribution_agents", "usage_attribution_commands",
|
|
]:
|
|
assert tbl in tables, f"missing table {tbl}"
|
|
conn.close()
|
|
|
|
|
|
def test_v42_indices_exist(tmp_path):
|
|
db_path = tmp_path / "test.duckdb"
|
|
conn = duckdb.connect(str(db_path))
|
|
init_database(conn)
|
|
idx_names = {row[0] for row in conn.execute("SELECT index_name FROM duckdb_indexes WHERE table_name LIKE 'usage_%'").fetchall()}
|
|
for idx in [
|
|
"idx_usage_events_session", "idx_usage_events_user_time", "idx_usage_events_tool",
|
|
"idx_usage_events_skill", "idx_usage_events_ref",
|
|
"idx_usage_session_user", "idx_usage_session_started",
|
|
"idx_usage_attr_skill_lookup", "idx_usage_attr_agent_lookup", "idx_usage_attr_command_lookup",
|
|
]:
|
|
assert idx in idx_names, f"missing index {idx}"
|
|
conn.close()
|
|
|
|
|
|
def test_v41_to_v42_is_idempotent(tmp_path):
|
|
"""Running init twice on same DB must not error and version stays 41."""
|
|
db_path = tmp_path / "twice.duckdb"
|
|
conn = duckdb.connect(str(db_path))
|
|
init_database(conn)
|
|
conn.close()
|
|
conn = duckdb.connect(str(db_path))
|
|
init_database(conn)
|
|
v = conn.execute("SELECT MAX(version) FROM schema_version").fetchone()[0]
|
|
assert v == 44
|
|
conn.close()
|
|
|
|
|
|
def test_v41_db_upgrades_cleanly(tmp_path):
|
|
"""A v40-state DB (post-Activity-Center) must climb to v41 without error."""
|
|
db_path = tmp_path / "v41.duckdb"
|
|
conn = duckdb.connect(str(db_path))
|
|
# Minimal v40 baseline shape — schema_version + audit_log with v40 columns.
|
|
conn.execute("CREATE TABLE schema_version (version INTEGER, applied_at TIMESTAMP DEFAULT current_timestamp)")
|
|
conn.execute("INSERT INTO schema_version (version) VALUES (41)")
|
|
conn.execute("""CREATE TABLE audit_log (
|
|
id VARCHAR PRIMARY KEY, timestamp TIMESTAMP DEFAULT current_timestamp,
|
|
user_id VARCHAR, action VARCHAR, resource VARCHAR, params JSON,
|
|
result VARCHAR, duration_ms INTEGER,
|
|
params_before JSON, client_ip VARCHAR, client_kind VARCHAR, correlation_id VARCHAR
|
|
)""")
|
|
conn.close()
|
|
conn = duckdb.connect(str(db_path))
|
|
init_database(conn)
|
|
v = conn.execute("SELECT MAX(version) FROM schema_version").fetchone()[0]
|
|
assert v == 44
|
|
# All 7 new v41 tables exist after the v40→v41 upgrade
|
|
tables = {row[0] for row in conn.execute(
|
|
"SELECT table_name FROM information_schema.tables WHERE table_schema='main'"
|
|
).fetchall()}
|
|
for tbl in [
|
|
"usage_events", "usage_session_summary",
|
|
"usage_tool_daily", "usage_plugin_daily",
|
|
"usage_attribution_skills", "usage_attribution_agents", "usage_attribution_commands",
|
|
]:
|
|
assert tbl in tables, f"missing table {tbl} after v40→v41 upgrade"
|
|
conn.close()
|
|
|
|
|
|
def test_v30_db_ladders_all_the_way_up(tmp_path):
|
|
"""Old v30-state DB must climb all the way to v41 without losing data."""
|
|
db_path = tmp_path / "v30.duckdb"
|
|
conn = duckdb.connect(str(db_path))
|
|
conn.execute("CREATE TABLE schema_version (version INTEGER, applied_at TIMESTAMP DEFAULT current_timestamp)")
|
|
conn.execute("INSERT INTO schema_version (version) VALUES (30)")
|
|
conn.execute("CREATE TABLE audit_log (id VARCHAR PRIMARY KEY)")
|
|
conn.execute("INSERT INTO audit_log (id) VALUES ('vintage')")
|
|
conn.close()
|
|
|
|
conn = duckdb.connect(str(db_path))
|
|
init_database(conn)
|
|
v = conn.execute("SELECT MAX(version) FROM schema_version").fetchone()[0]
|
|
assert v == 44
|
|
cnt = conn.execute("SELECT COUNT(*) FROM audit_log WHERE id='vintage'").fetchone()[0]
|
|
assert cnt == 1
|
|
# New v41 table exists
|
|
cnt2 = conn.execute("SELECT COUNT(*) FROM usage_events").fetchone()[0]
|
|
assert cnt2 == 0
|
|
conn.close()
|