The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): stamp created_source on reset, branch and compression children
David Dudok de Wit33ee730853dd
diff --git a/hermes_state_compression.py b/hermes_state_compression.pyindex 3d54d537ce8..f7055852e95 100644--- a/hermes_state_compression.py+++ b/hermes_state_compression.py@@ -249,19 +249,29 @@ class SessionCompressionMixin:# Pin/archive are compression-lineage flags; canonical branch/reset verbs also publish through# here, and their child is its own conversation, so it starts unpinned and visible.continuation = not any(k in (model_config or {}) for k in ("_branched_from", "_reset_from"))+ # Copy a real creating surface. Automatic compression passes the parent's live+ # label (``local`` after the peer refresh); reset, branch and the compress+ # mutation pass the policy label. ``local`` and ``unknown`` are not surfaces,+ # so those stay NULL. Compression sets ``_session_db_created`` and does not+ # create the child again. The turn's ``update_token_counts(source=...)`` fills+ # that NULL; auxiliary usage must not write ``unknown`` into it first.+ from gateway.session_policy import created_surface+ created_source = parent["created_source"]+ if created_source in (None, "", "local", "unknown"):+ created_source = created_surface(source)# The child continues the parent's tools[] pin (the compaction refresh re-pinned it just# before publish), or its first hop to another surface re-derives the array.conn.execute("""INSERT INTO sessions (- id, source, model, model_config, system_prompt,+ id, source, created_source, model, model_config, system_prompt,system_prompt_hash, tool_names,parent_session_id, cwd, git_branch, git_repo_root,profile_name, user_id, session_key, chat_id, chat_type,thread_id, display_name, origin_json, pinned, started_at,archived, auto_archived- ) VALUES (?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",+ ) VALUES (?, ?, ?, ?, ?, NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",(- child_session_id, source, model, json.dumps(model_config) if model_config else None,+ child_session_id, source, created_source, model, json.dumps(model_config) if model_config else None,system_prompt_hash, parent["tool_names"], parent_session_id, cwd or parent["cwd"], parent["git_branch"],parent["git_repo_root"],profile_name or parent["profile_name"] or self._own_profile_name(),@@ -330,7 +340,7 @@ class SessionCompressionMixin:"""SELECT ended_at, end_reason, cwd, git_branch, git_repo_root,user_id, session_key, chat_id, chat_type,thread_id, display_name, origin_json, profile_name, tool_names,- archived, auto_archived, pinned, model_config+ archived, auto_archived, pinned, model_config, created_sourceFROM sessions WHERE id = ?""",(parent_session_id,),).fetchone()diff --git a/hermes_state_runtime.py b/hermes_state_runtime.pyindex 7ebad76355e..ff6a3d639fd 100644--- a/hermes_state_runtime.py+++ b/hermes_state_runtime.py@@ -864,9 +864,13 @@ def _worker_turn(db, conn, session_id, payload, operation):def _worker_usage(db, conn, session_id, payload, *, auxiliary=False):from hermes_state_usage import _MODEL_USAGE_FIELDS, _TOKEN_COUNTERS+ source = Noneif not auxiliary:- # ``source`` feeds the legacy path's row-existence guard (#111999); an authority-owned- # session row was minted with its real surface at admission, so nothing to repair here.+ # ``source`` is the agent's surface (turn_usage._agent_session_source). The row is+ # usually stamped at admission. A compression child can still have a NULL+ # created_source, and this worker never create_session's it, so fill only that NULL.+ raw = payload.get('source')+ source = raw if isinstance(raw, str) else Nonepayload = {k: v for k, v in payload.items() if k != 'source'}allowed = (_MODEL_USAGE_FIELDS - {'billing_mode', 'actual_cost_usd', 'cost_status', 'cost_source'} | {'task'}) if auxiliary else (_MODEL_USAGE_FIELDS | {'pricing_version', 'absolute', 'task'})if set(payload) - allowed:@@ -888,6 +892,10 @@ def _worker_usage(db, conn, session_id, payload, *, auxiliary=False):db._record_model_usage(conn, session_id, **({'api_call_count': 1} | payload))else:db._update_token_counts_in_transaction(conn, session_id, **payload)+ if source not in (None, '', 'local', 'unknown'):+ conn.execute(+ "UPDATE sessions SET created_source=? WHERE id=? AND created_source IS NULL",+ (source, session_id))return {'value': None}diff --git a/hermes_state_usage.py b/hermes_state_usage.pyindex ae91f8a22ff..e01fff0b991 100644--- a/hermes_state_usage.py+++ b/hermes_state_usage.py@@ -396,9 +396,11 @@ 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: same guard as update_token_counts; the aux path carries no surface, so- # the placeholder stays repairable by the creator's upsert (_insert_session_row).- self._insert_session_row(session_id, "unknown")+ # 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.+ 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))def auxiliary_usage_by_task(self, session_id: str) -> dict[str, dict[str, float]]:diff --git a/tests/gateway/test_resume_latest_local_cli.py b/tests/gateway/test_resume_latest_local_cli.pyindex 4b5c8156cb8..b8801b19b1a 100644--- a/tests/gateway/test_resume_latest_local_cli.py+++ b/tests/gateway/test_resume_latest_local_cli.py@@ -99,6 +99,108 @@ async def test_auxiliary_usage_cannot_replace_the_insert_stamp(tmp_path, monkeypstore.close_all_db_handles()+def _set_created(db, sid, value):+ with db._lock:+ db._conn.execute("UPDATE sessions SET created_source=? WHERE id=?", (value, sid))+ db._conn.commit()+++def _reset(authority, store, sid, child):+ from gateway.session import SessionEntry+ from hermes_state_local import local_receipt+ from hermes_state_local_lineage import reset_local_target++ live = authority.sessions[sid]+ store._record_gateway_session_peer(sid, live.route, live.source)+ old = SessionEntry.from_dict(local_receipt(authority.db, sid)['entry'])+ entry = SessionEntry(old.session_key, child, old.created_at, old.updated_at, origin=old.origin,+ platform=old.platform, chat_type=old.chat_type, is_fresh_reset=True)+ reset_local_target(authority.db, epoch=authority.epoch, parent_session_id=sid, entry=entry.to_dict())+ return child+++async def test_reset_copies_the_parent_surface_and_falls_back_to_policy(tmp_path, monkeypatch):+ """A real parent created_source is copied. NULL, local and unknown are not.++ /new publishes with the policy label (cli), not the live local source. Copying+ tui is not the policy fallback; a parent with no surface becomes cli.+ """+ store, authority, owner = _owner(tmp_path / 'home', monkeypatch)+ try:+ copied = await _call(owner, 'session.create', request_id='copy', source='cli',+ cwd=str(tmp_path), toolsets=[], model='m')+ sid = copied['session_id']+ _set_created(authority.db, sid, 'tui')+ child = _reset(authority, store, sid, 'reset-copied')+ assert authority.db.get_session(child)['created_source'] == 'tui'++ for parent_created, name in ((None, 'reset-null'), ('unknown', 'reset-unknown'), ('local', 'reset-local')):+ created = await _call(owner, 'session.create', request_id=name, source='cli',+ cwd=str(tmp_path), toolsets=[], model='m')+ sid = created['session_id']+ _set_created(authority.db, sid, parent_created)+ child = _reset(authority, store, sid, name + '-child')+ row = authority.db.get_session(child)+ assert row['created_source'] == 'cli', (parent_created, row['source'], row['created_source'])+ assert row['source'] == 'cli'+ finally:+ await owner.close()+ store.close_all_db_handles()+++async def test_branch_child_copies_the_parent_surface(tmp_path, monkeypatch):+ from hermes_state_mutation_branch import branch_in_transaction++ store, authority, owner = _owner(tmp_path / 'home', monkeypatch)+ try:+ created = await _call(owner, 'session.create', request_id='branch', source='cli',+ cwd=str(tmp_path), toolsets=[], model='m')+ sid = created['session_id']+ _set_created(authority.db, sid, 'desktop')+ branched = authority.db._execute_write(+ lambda conn: branch_in_transaction(authority.db, conn, sid, {}))+ child = branched[1]['branched_session_id']+ assert authority.db.get_session(child)['created_source'] == 'desktop'+ finally:+ await owner.close()+ store.close_all_db_handles()+++async def test_compress_mutation_copies_parent_or_uses_the_policy_surface(tmp_path, monkeypatch):+ from hermes_state_mutation_compress import compress_in_transaction+ from hermes_state_mutation_prepared import local_snapshot++ store, authority, owner = _owner(tmp_path / 'home', monkeypatch)+ try:+ def compress(sid):+ def write(conn):+ snapshot = local_snapshot(authority.db, conn, sid)+ prepared = {'snapshot': snapshot, 'in_place': False,+ 'messages': [{'role': 'user', 'content': 'hi'}]}+ return compress_in_transaction(authority.db, conn, sid, {}, prepared)+ return authority.db._execute_write(write)[1]['target_session_id']++ copied = await _call(owner, 'session.create', request_id='compress-copy', source='cli',+ cwd=str(tmp_path), toolsets=[], model='m')+ sid = copied['session_id']+ _set_created(authority.db, sid, 'desktop')+ assert authority.db.get_session(compress(sid))['created_source'] == 'desktop'++ for parent_created, name in ((None, 'compress-null'), ('local', 'compress-local')):+ created = await _call(owner, 'session.create', request_id=name, source='cli',+ cwd=str(tmp_path), toolsets=[], model='m')+ sid = created['session_id']+ _set_created(authority.db, sid, parent_created)+ row = authority.db.get_session(compress(sid))+ assert row['created_source'] == 'cli', (parent_created, row['created_source'])+ finally:+ await owner.close()+ store.close_all_db_handles()++@pytest.mark.asyncioasync def test_worker_create_after_peer_refresh_still_resumes(tmp_path, monkeypatch):"""The peer refresh rewrites source to local and worker_create is refused.diff --git a/tests/hermes_state/test_compression_created_source.py b/tests/hermes_state/test_compression_created_source.pynew file mode 100644index 00000000000..7f2166243c8--- /dev/null+++ b/tests/hermes_state/test_compression_created_source.py@@ -0,0 +1,81 @@+"""A compression child must not freeze ``local`` or ``unknown`` as created_source.++Automatic compression does not create the child again. The turn's+``update_token_counts(source=...)`` is the write that fills a NULL column,+including when auxiliary usage ran first.+"""+from hermes_state import SessionDB+++def _compress(db, parent_created, child):+ parent = "parent-" + child+ db.create_session(parent, source="local")+ with db._lock:+ db._conn.execute("UPDATE sessions SET created_source=? WHERE id=?", (parent_created, parent))+ db._conn.commit()+ db.append_message(parent, role="user", content="hi")+ db.publish_compression_child(+ parent_session_id=parent, child_session_id=child, source="local",+ messages=[{"role": "user", "content": "hi"}], require_compression_lease=False)+ return child+++def _created(db, sid):+ return db.get_session(sid)["created_source"]+++def test_compression_child_heals_through_token_counts_after_aux_usage(tmp_path):+ """NULL, unknown and local parents leave the child unset.++ ``record_auxiliary_usage`` runs first and must not store ``unknown``.+ ``update_token_counts(source='cli')`` then sets ``cli``. A parent that+ already has ``cli`` is copied, and neither write replaces it.+ """+ db = SessionDB(db_path=tmp_path / "state.db")+ try:+ for parent_created, child in ((None, "heal-null"), ("unknown", "heal-unknown"), ("local", "heal-local")):+ _compress(db, parent_created, child)+ assert _created(db, child) is None+ db.record_auxiliary_usage(child, "compression", model="m", input_tokens=2, output_tokens=1)+ assert _created(db, child) is None+ db.update_token_counts(child, input_tokens=3, output_tokens=1, model="m",+ api_call_count=1, source="cli")+ assert _created(db, child) == "cli"+ copied = _compress(db, "cli", "copied")+ assert _created(db, copied) == "cli"+ db.record_auxiliary_usage(copied, "compression", model="m", input_tokens=1)+ db.update_token_counts(copied, input_tokens=1, output_tokens=1, model="m",+ api_call_count=1, source="desktop")+ assert _created(db, copied) == "cli"+ finally:+ db.close()+++def test_worker_usage_fills_a_null_compression_child(tmp_path):+ """A managed worker's usage.main carries the surface and drops it on the floor+ unless the column is still NULL. Auxiliary usage on that row does not freeze it."""+ import hermes_state_runtime as rt++ db = SessionDB(db_path=tmp_path / "state.db")+ try:+ child = _compress(db, None, "worker-child")+ assert _created(db, child) is None+ db.record_auxiliary_usage(child, "compression", model="aux", input_tokens=1)+ assert _created(db, child) is None+ epoch = rt.begin_runtime_epoch(db, instance_id="owner")+ scope = dict(epoch=epoch, execution_id="w", session_id=child, generation=0)+ rt.register_worker_execution(db, **scope, kind="compute", adoption_secret="private")+ rt.mutate_worker_execution(db, **scope, sequence=1, operation="usage.main", payload={+ "input_tokens": 5, "output_tokens": 1, "api_call_count": 1, "model": "m",+ "billing_provider": "p", "source": "cli"})+ assert _created(db, child) == "cli"++ kept = _compress(db, "tui", "worker-kept")+ assert _created(db, kept) == "tui"+ scope = dict(epoch=epoch, execution_id="w2", session_id=kept, generation=0)+ rt.register_worker_execution(db, **scope, kind="compute", adoption_secret="private")+ rt.mutate_worker_execution(db, **scope, sequence=1, operation="usage.main", payload={+ "input_tokens": 1, "api_call_count": 1, "model": "m", "source": "cli"})+ assert _created(db, kept) == "tui"+ finally:+ db.close()