Skip to content

The agent that grows with you

fix(cli): keep profile backfill when auxiliary usage skips unknown

David Dudok de Wit599cd71c7557
diff --git a/hermes_state_usage.py b/hermes_state_usage.py
index e01fff0b991..bc002ceae47 100644
--- a/hermes_state_usage.py
+++ b/hermes_state_usage.py
@@ -397,11 +397,22 @@ class SessionUsageMixin:
return
usage["api_call_count"] = 1 if api_call_count is None else int(api_call_count)
# FK to sessions.id. A missing row is inserted as ``unknown``, and that created_source
- # never moves. An existing row keeps its column, including NULL: a compression child
- # is not create_session'd again, and update_token_counts(source=...) fills that NULL.
+ # never moves. An existing row keeps source and created_source, including a NULL
+ # surface update_token_counts can still fill. The upsert's profile_name backfill
+ # still runs: a NULL owner is filled without writing ``unknown`` anywhere.
+ own_profile = None
if self._read_one("SELECT 1 FROM sessions WHERE id=?", (session_id,)) is None:
self._insert_session_row(session_id, "unknown")
- self._execute_write(lambda conn: self._record_model_usage(conn, session_id, task=task, **usage))
+ else:
+ own_profile = self._own_profile_name()
+
+ def _write(conn):
+ if own_profile:
+ conn.execute(
+ "UPDATE sessions SET profile_name=? WHERE id=? AND profile_name IS NULL",
+ (own_profile, session_id))
+ self._record_model_usage(conn, session_id, task=task, **usage)
+ self._execute_write(_write)
def auxiliary_usage_by_task(self, session_id: str) -> dict[str, dict[str, float]]:
"""Per-task auxiliary usage (``task != ''``: vision, compression, title_generation, ...) summed
diff --git a/tests/hermes_state/test_aux_usage_accounting.py b/tests/hermes_state/test_aux_usage_accounting.py
index d960e0d78d3..1d6a101b414 100644
--- a/tests/hermes_state/test_aux_usage_accounting.py
+++ b/tests/hermes_state/test_aux_usage_accounting.py
@@ -96,7 +96,24 @@ class TestRecordAuxiliaryUsage:
tasks = sorted(r["task"] for r in rows)
assert tasks == ["", "title_generation"]
-
+ def test_existing_row_keeps_null_created_source_and_backfills_profile(self, db):
+ """Skipping the unknown upsert must not skip the NULL profile_name backfill.
+
+ source and created_source stay as they are. A NULL profile_name is filled
+ with this store's profile, the same value the upsert would have written.
+ """
+ db.create_session("s1", source="local")
+ db._own_profile_name = lambda: "default"
+ with db._lock:
+ db._conn.execute(
+ "UPDATE sessions SET created_source=NULL, profile_name=NULL WHERE id=?",
+ ("s1",))
+ db._conn.commit()
+ db.record_auxiliary_usage("s1", "compression", model="m", input_tokens=1)
+ row = db.get_session("s1")
+ assert row["source"] == "local"
+ assert row["created_source"] is None
+ assert row["profile_name"] == "default"
class TestSchemaMigrationV22: