Skip to content

The agent that grows with you

fix(state): a reset local session no longer records ending before it started

Teknium34496d1b9a83
diff --git a/hermes_state_local_lineage.py b/hermes_state_local_lineage.py
index 1da8155e03b..768ee4bed93 100644
--- a/hermes_state_local_lineage.py
+++ b/hermes_state_local_lineage.py
@@ -4,6 +4,7 @@ The creation ID remains the admission/execution owner. Transcript rotation must
not re-key idempotency digests, queued inputs, subscribers or uncertain claims.
"""
import json
+import time
from hermes_state_local import POLICY_PREFIX
from hermes_state_runtime import RuntimeStoreError, _epoch, _json
@@ -108,8 +109,8 @@ def reset_local_target(db, *, epoch, parent_session_id, entry):
child_session_id=entry['session_id'], source=policy['source'], model=policy['model'],
model_config={'_reset_from': parent_session_id}, system_prompt=None,
cwd=policy['cwd'], profile_name=parent['profile_name'])
- conn.execute("UPDATE sessions SET ended_at=strftime('%s','now'),end_reason='session_reset' WHERE id=?",
- (parent_session_id,))
+ conn.execute("UPDATE sessions SET ended_at=?,end_reason='session_reset' WHERE id=?",
+ (time.time(), parent_session_id))
db._bump_conversation_generation(conn, parent_session_id, 'session_reset')
advance_local_target(conn, parent_session_id, entry['session_id'], entry=entry)
conn.execute('UPDATE sessions SET runtime_generation=runtime_generation+1 WHERE id=?', (logical_id,))
diff --git a/tests/hermes_state/test_ledger_delete_consistency.py b/tests/hermes_state/test_ledger_delete_consistency.py
index a5b062fe123..7e3e69e4407 100644
--- a/tests/hermes_state/test_ledger_delete_consistency.py
+++ b/tests/hermes_state/test_ledger_delete_consistency.py
@@ -279,3 +279,17 @@ def test_discarding_an_unadmitted_row_leaves_no_fence_but_an_admitted_row_is_fen
_settled_admission(db, epoch, 'used')
assert db.discard_unadmitted_session('used') is True
assert db.get_meta(RETIRED_PREFIX + 'used') is not None
+
+
+def test_local_reset_stamps_ended_at_on_the_same_float_clock_as_started_at(tmp_path):
+ """``ended_at`` uses the ``time.time()`` clock of ``started_at`` and every sibling end path:
+ SQLite's integer-second clock truncates, so a reset in the same second as the creation
+ recorded a session that ended before it started."""
+ from hermes_state_local_lineage import reset_local_target
+ with closing(SessionDB(tmp_path / 'state.db')) as db:
+ epoch = rt.begin_runtime_epoch(db, instance_id='owner')
+ sid, reset = _local_session(db, epoch)
+ before = time.time()
+ reset_local_target(db, epoch=epoch, parent_session_id=sid, entry=reset)
+ parent = db.get_session(sid)
+ assert parent['ended_at'] >= max(parent['started_at'], before)