Skip to content

The agent that grows with you

test: trim the created_source tests to the salvage bar

kshitijk4poorf97207ce2810
diff --git a/tests/gateway/test_resume_latest_local_cli.py b/tests/gateway/test_resume_latest_local_cli.py
index 3abae21a4c1..ba631677324 100644
--- a/tests/gateway/test_resume_latest_local_cli.py
+++ b/tests/gateway/test_resume_latest_local_cli.py
@@ -151,83 +151,6 @@ async def test_reset_copies_the_parent_surface_and_falls_back_to_policy(tmp_path
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()
-
-
-async def test_worker_create_after_peer_refresh_still_resumes(tmp_path, monkeypatch):
- """The peer refresh rewrites source to local and worker_create is refused.
- The insert stamp must already hold cli, or latest cannot see the row."""
- from hermes_state_runtime import RuntimeStoreError
- from hermes_state_worker_lifecycle import worker_create
-
- store, authority, owner = _owner(tmp_path / 'home', monkeypatch)
- try:
- created = await _call(owner, 'session.create', request_id='chat', source='cli',
- cwd=str(tmp_path), toolsets=[], model='m')
- sid = created['session_id']
- live = authority.sessions[sid]
- store._record_gateway_session_peer(sid, live.route, live.source)
- with pytest.raises(RuntimeStoreError, match='permission_denied'):
- authority.db._execute_write(lambda conn: worker_create(authority.db, conn, sid, {'source': 'cli'}))
- authority.db.append_message(sid, 'user', 'hi')
- authority.sessions.clear()
- resumed = await _call(owner, 'session.resume', latest='cli')
- assert isinstance(resumed, dict) and resumed['session_id'] == sid, resumed
- finally:
- await owner.close()
- store.close_all_db_handles()
-
-
def _listed(db, source, capsys):
from argparse import Namespace
from hermes_cli.sessions_cmd import _cmd_list
@@ -251,28 +174,6 @@ async def test_oneshot_keeps_its_own_created_source_and_stays_out_of_cli(tmp_pat
store.close_all_db_handles()
[email protected](('surface', 'labels'), [('gui', ('gui', 'desktop')), ('acp', ('acp',))])
-async def test_gui_and_acp_list_under_the_policy_label_and_the_agent_surface(
- tmp_path, monkeypatch, capsys, surface, labels):
- """session.create stamps the agent surface (gui is desktop). --source accepts
- either name, before the peer refresh and after the row is stored as local."""
- store, authority, owner = _owner(tmp_path / 'home', monkeypatch)
- try:
- created = await _call(owner, 'session.create', request_id=surface, source=surface,
- cwd=str(tmp_path), toolsets=[], model='m')
- sid = created['session_id']
- for label in labels:
- assert sid in _listed(authority.db, label, capsys), (surface, label, 'before peer')
- live = authority.sessions[sid]
- store._record_gateway_session_peer(sid, live.route, live.source)
- for label in labels:
- assert sid in _listed(authority.db, label, capsys), (surface, label, 'after peer')
- finally:
- await owner.close()
- store.close_all_db_handles()
-
-
@pytest.mark.asyncio
async def test_latest_skips_a_newer_empty_launch_stub(tmp_path, monkeypatch):
"""CLI-31: the insert stamps the surface, so a 0-message launch stub left by a crash or a
diff --git a/tests/hermes_cli/test_sessions_source_local.py b/tests/hermes_cli/test_sessions_source_local.py
index 6c8cae04ce1..05321c2b7ca 100644
--- a/tests/hermes_cli/test_sessions_source_local.py
+++ b/tests/hermes_cli/test_sessions_source_local.py
@@ -4,64 +4,14 @@ from argparse import Namespace
from hermes_state import SessionDB
-def test_list_and_browse_source_cli_include_the_gateway_row(tmp_path, capsys, monkeypatch):
- from hermes_cli.sessions_cmd import _cmd_browse, _cmd_list
-
- db = SessionDB(db_path=tmp_path / "state.db")
- db.create_session("cli-session", source="local")
- db.create_session("desk-session", source="local")
- with db._lock:
- db._conn.executemany(
- "UPDATE sessions SET created_source=?, started_at=? WHERE id=?",
- [("cli", 1, "cli-session"), ("desktop", 2, "desk-session")],
- )
- db._conn.commit()
-
- _cmd_list(db, Namespace(limit=20, source="cli", workspace=None))
- listed = capsys.readouterr().out
- assert "cli-session" in listed
- assert "desk-session" not in listed
- assert "No sessions found." not in listed
-
- offered = {}
-
- def picker(sessions, session_db=None):
- offered["ids"] = [row["id"] for row in sessions]
-
- monkeypatch.setattr("hermes_cli.sessions_cmd._session_browse_picker", picker)
- _cmd_browse(db, Namespace(limit=20, source="cli"))
- assert offered["ids"] == ["cli-session"]
- assert "No sessions found." not in capsys.readouterr().out
-
-
-def test_stats_counts_cli_segments_while_list_shows_one_conversation(tmp_path, capsys):
- """Stats counts every cli segment. List shows the conversation once, as its tip."""
- from hermes_cli.sessions_cmd import _cmd_list, _cmd_stats
-
- db = SessionDB(db_path=tmp_path / "state.db")
- db.create_session("segment-root", source="local")
- with db._lock:
- db._conn.execute("UPDATE sessions SET created_source=? WHERE id=?", ("cli", "segment-root"))
- db._conn.commit()
- db.append_message("segment-root", role="user", content="hi")
- db.publish_compression_child(
- parent_session_id="segment-root", child_session_id="segment-tip", source="local",
- messages=[{"role": "user", "content": "hi"}], require_compression_lease=False)
- _cmd_list(db, Namespace(limit=20, source="cli", workspace=None))
- listed = capsys.readouterr().out
- assert "segment-tip" in listed
- assert "segment-root" not in listed
- _cmd_stats(db, Namespace())
- assert "cli: 2 sessions" in capsys.readouterr().out
-
-
-def test_list_includes_a_compression_tip_whose_root_has_no_created_source(tmp_path):
+def test_list_includes_a_compression_tip_whose_root_has_no_created_source(tmp_path, capsys):
"""Search and stats see a tip healed to cli. List must show that conversation too.
The page hides compression children and paints the root as the tip, so the usage
write that heals the tip also stamps the NULL ancestors of that chain.
"""
from hermes_cli.gateway_session_store import SurfaceLabelReader
+ from hermes_cli.sessions_cmd import _cmd_list, _cmd_stats
db = SessionDB(db_path=tmp_path / "state.db")
db.create_session("chain-root", source="local")
@@ -79,39 +29,11 @@ def test_list_includes_a_compression_tip_whose_root_has_no_created_source(tmp_pa
"chain-tip", input_tokens=2, output_tokens=1, model="m", api_call_count=1, source="cli")
assert [db.get_session(sid)["created_source"] for sid in ("chain-root", "chain-mid", "chain-tip")] == [
"cli", "cli", "cli"]
- reader = SurfaceLabelReader(db)
- listed = [row["id"] for row in reader.list_sessions_rich(source="cli", limit=10)]
- assert listed == ["chain-tip"]
- assert "chain-tip" in [row["id"] for row in reader.search_sessions(source="cli", limit=10)]
- assert reader.session_count(source="cli") == 3 # stats counts every segment
- db.close()
-
-
-def test_reader_session_count_includes_gateway_rows_and_a_pre_column_store(tmp_path):
- """session_count through the reader matches a local/cli row, and a read-only
- store from before the column still counts its source=cli rows without migrating."""
- from hermes_cli.gateway_session_store import SurfaceLabelReader
-
- db = SessionDB(db_path=tmp_path / "state.db")
- db.create_session("gateway-cli", source="local")
- with db._lock:
- db._conn.execute("UPDATE sessions SET created_source=? WHERE id=?", ("cli", "gateway-cli"))
- db._conn.commit()
- assert SurfaceLabelReader(db).session_count(source="cli") == 1
+ capsys.readouterr()
+ _cmd_list(db, Namespace(limit=20, source="cli", workspace=None))
+ listed = capsys.readouterr().out
+ assert "chain-tip" in listed and "chain-root" not in listed
+ assert "chain-tip" in [row["id"] for row in SurfaceLabelReader(db).search_sessions(source="cli", limit=10)]
+ _cmd_stats(db, Namespace())
+ assert "cli: 3 sessions" in capsys.readouterr().out # stats counts every segment
db.close()
-
- path = tmp_path / "old.db"
- writer = SessionDB(db_path=path)
- writer.create_session("legacy-cli", source="cli")
- with writer._lock:
- writer._conn.execute("DROP INDEX IF EXISTS idx_sessions_local_created_started")
- writer._conn.execute("ALTER TABLE sessions DROP COLUMN created_source")
- writer._conn.commit()
- writer.close()
- ro = SessionDB(db_path=path, read_only=True)
- try:
- assert SurfaceLabelReader(ro).session_count(source="cli") == 1
- columns = {row[1] for row in ro._conn.execute("PRAGMA table_info(sessions)")}
- assert "created_source" not in columns
- finally:
- ro.close()
diff --git a/tests/hermes_state/test_compression_created_source.py b/tests/hermes_state/test_compression_created_source.py
index 07123b7a5bc..0cc4a63d1b4 100644
--- a/tests/hermes_state/test_compression_created_source.py
+++ b/tests/hermes_state/test_compression_created_source.py
@@ -1,8 +1,7 @@
-"""A compression child must not freeze ``local`` or ``unknown`` as created_source.
+"""A managed worker's usage write heals a NULL compression child to the agent surface.
-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.
+Automatic compression does not create the child again, so the worker's ``usage.main``
+is the write that fills the column, and it carries the surface to the NULL root.
"""
from hermes_state import SessionDB
@@ -24,63 +23,6 @@ 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()
-
-
def test_desktop_worker_heals_gui_to_desktop(tmp_path):
"""A managed Desktop worker's platform is the policy label ``gui``. The healed
column is ``desktop``, the same surface the in-process agent and the insert stamp."""
@@ -97,5 +39,6 @@ def test_desktop_worker_heals_gui_to_desktop(tmp_path):
"input_tokens": 4, "output_tokens": 1, "api_call_count": 1, "model": "m",
"billing_provider": "p", "source": "gui"})
assert _created(db, child) == "desktop"
+ assert _created(db, "parent-" + child) == "desktop"
finally:
db.close()
diff --git a/tests/hermes_state/test_placeholder_provenance.py b/tests/hermes_state/test_placeholder_provenance.py
index 2b83226a265..f5b6eda9b65 100644
--- a/tests/hermes_state/test_placeholder_provenance.py
+++ b/tests/hermes_state/test_placeholder_provenance.py
@@ -1,13 +1,11 @@
"""Placeholder-first creation must not hide real CLI sessions (#138831)."""
-import pytest
-
from hermes_state import SessionDB
from hermes_cli.gateway_session_store import SurfaceLabelReader
from hermes_cli.session_listing import query_session_listing
[email protected]("surface", ["cli", "tui", "gui"])
-def test_placeholder_creator_remains_visible_after_gateway_routing(tmp_path, surface):
+def test_placeholder_creator_remains_visible_after_gateway_routing(tmp_path):
+ surface = "cli"
db = SessionDB(db_path=tmp_path / "state.db")
try:
db.update_token_counts("previous", input_tokens=5)
@@ -22,26 +20,3 @@ def test_placeholder_creator_remains_visible_after_gateway_routing(tmp_path, sur
assert db.get_session("previous")["input_tokens"] == 5
finally:
db.close()
-
-
-def test_known_provenance_survives_later_creators_and_accounting(tmp_path):
- db = SessionDB(db_path=tmp_path / "state.db")
- try:
- db.create_session("child", source="subagent")
- db.update_token_counts("child", input_tokens=5)
- db.create_session("child", source="cli")
- db.record_gateway_session_peer("child", source="local", session_key="local:child")
- assert db.get_session("child")["created_source"] == "subagent"
- assert SurfaceLabelReader(db).search_sessions(source="cli") == []
- db.update_token_counts("pending", input_tokens=1)
- db.update_token_counts("pending", input_tokens=2)
- assert db.get_session("pending")["created_source"] == "unknown"
- assert db.get_session("pending")["input_tokens"] == 3
- db.create_session("legacy", source="cli")
- db._execute_write(lambda conn: conn.execute(
- "UPDATE sessions SET created_source=NULL WHERE id=?", ("legacy",),
- ))
- db.create_session("legacy", source="tui")
- assert db.get_session("legacy")["created_source"] == "tui"
- finally:
- db.close()