Expand description
Repository implementation for SessionLifecycleDb (LIB-05).
Line-for-line port of Python’s
cognee/modules/session_lifecycle/metrics.py plus the three
aggregate queries that live inline in
cognee/api/v1/sessions/routers/get_sessions_router.py (stats /
cost-by-model). The trait, public domain types, and effective-status
semantics live in traits::session_lifecycle_db.
Implementation choices:
- UUIDs persist as 32-char hex (per LIB-03 /
uuid_hex.rs); the repository converts at the boundary so the trait surface is plainUuid. ensure_and_touch_sessionand the per-model upsert inaccumulate_usageuse raw SQLINSERT ... ON CONFLICT DO UPDATEviaStatement::from_sql_and_valuesto express the COALESCE dataset-backfill and theWHERE status = 'running'clause on the update — neither of whichsea_orm::sea_query::OnConflictsurfaces portably. The dialect is SQLite/Postgres-shared syntax; branching onget_database_backendselects the right backend marker.- The effective-status helper computes
now - thresholdin Rust and binds it as a parameter (mirrors Python atmetrics.py:281-282), so no SQL function for elapsed time is needed and the expression is portable across SQLite / Postgres. - Duration aggregation pulls
(started_at, ended_at, last_activity_at)rows and folds in Rust — Python does the same fallback atget_sessions_router.py:148-158because SQLite has noEXTRACT(epoch ...).
Functions§
- abandon_
after_ seconds - Read the abandonment threshold (seconds) from the environment.
Default
1800(30 min) — Decision 12. Mirrors Python’s_abandon_after_secondsatmetrics.py:47-52: a non-numeric or empty value falls through to the default. - accumulate_
usage - aggregate_
stats - cost_
by_ model - ensure_
and_ touch_ session - get_
session_ row - list_
session_ rows