The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
docs(state): an unknown created_source is repaired by the creator upsert
kshitijk4poor1308bd3ccda6
diff --git a/hermes_state_local.py b/hermes_state_local.pyindex 20037d13a04..572ba6ddba7 100644--- a/hermes_state_local.py+++ b/hermes_state_local.py@@ -56,11 +56,11 @@ def commit_local_session(db, *, epoch, receipt):policy, entry = receipt['policy'], receipt['entry']from datetime import datetimestarted = datetime.fromisoformat(entry['created_at']).timestamp()- # Auxiliary usage inserts created_source='unknown' when it creates the row, and- # that value never moves (record_auxiliary_usage carries no surface). The agent's- # own accounting passes its surface (turn_usage._agent_session_source) and is not- # that writer. Stamp the agent surface in this insert so a later usage write- # cannot fill the column. gui's in-process agent is desktop; a managed worker+ # Auxiliary usage inserts created_source='unknown' when it creates the row+ # (record_auxiliary_usage carries no surface). The creator upsert repairs+ # 'unknown', but this DO NOTHING insert does not, so the surface is stamped here.+ # The agent's own accounting passes its surface (turn_usage._agent_session_source)+ # and is not that writer. gui's in-process agent is desktop; a managed worker# creates with the policy label gui.from gateway.session_policy import created_surfacecreated = created_surface(policy['source'])diff --git a/hermes_state_usage.py b/hermes_state_usage.pyindex bc002ceae47..af5167f0702 100644--- a/hermes_state_usage.py+++ b/hermes_state_usage.py@@ -396,9 +396,9 @@ class SessionUsageMixin:if not session_id or not task:returnusage["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 source and created_source, including a NULL- # surface update_token_counts can still fill. The upsert's profile_name backfill+ # FK to sessions.id. A missing row is inserted as the ``unknown`` placeholder, which+ # the creator upsert later replaces. 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 = Noneif self._read_one("SELECT 1 FROM sessions WHERE id=?", (session_id,)) is None:diff --git a/tests/gateway/test_resume_latest_local_cli.py b/tests/gateway/test_resume_latest_local_cli.pyindex ba631677324..cf6ae953ed3 100644--- a/tests/gateway/test_resume_latest_local_cli.py+++ b/tests/gateway/test_resume_latest_local_cli.py@@ -78,9 +78,9 @@ async def test_resume_latest_matches_the_owners_local_rows_by_creating_surface(tasync def test_auxiliary_usage_cannot_replace_the_insert_stamp(tmp_path, monkeypatch):"""Auxiliary usage creates a missing row as created_source='unknown'.- The agent's own token accounting passes its surface, so it is not that writer.- session.create stamps the surface in the insert; a later record_auxiliary_usage- must leave it, and session.resume latest='cli' still finds the row.+ The creator upsert repairs 'unknown', but session.create's DO NOTHING insert does+ not, so it stamps the surface itself; a later record_auxiliary_usage must leave it,+ and session.resume latest='cli' still finds the row."""store, authority, owner = _owner(tmp_path / 'home', monkeypatch)try: