Skip to main content

Module session_lifecycle

Module session_lifecycle 

Source
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 plain Uuid.
  • ensure_and_touch_session and the per-model upsert in accumulate_usage use raw SQL INSERT ... ON CONFLICT DO UPDATE via Statement::from_sql_and_values to express the COALESCE dataset-backfill and the WHERE status = 'running' clause on the update — neither of which sea_orm::sea_query::OnConflict surfaces portably. The dialect is SQLite/Postgres-shared syntax; branching on get_database_backend selects the right backend marker.
  • The effective-status helper computes now - threshold in Rust and binds it as a parameter (mirrors Python at metrics.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 at get_sessions_router.py:148-158 because SQLite has no EXTRACT(epoch ...).

Functions§

abandon_after_seconds
Read the abandonment threshold (seconds) from the environment. Default 1800 (30 min) — Decision 12. Mirrors Python’s _abandon_after_seconds at metrics.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