Skip to content

The agent that grows with you

refactor(state): trash-teardown drop helper; simplify deferral-record parsing

Teknium96508304ec8f
diff --git a/hermes_state_schema.py b/hermes_state_schema.py
index 663ba2f7770..49ac7e1291e 100644
--- a/hermes_state_schema.py
+++ b/hermes_state_schema.py
@@ -366,7 +366,7 @@ class SessionSchemaMixin:
``_FTS_HOLDER_ESCALATE_SECONDS``, provably inactive orphan Desktop
backends are reaped and the holders re-checked."""
now = time.time()
- record = None
+ record = {}
try:
row = cursor.execute(
"SELECT value FROM state_meta WHERE key = ? LIMIT 1", (FTS_REBUILD_DEFERRAL_KEY,),
@@ -376,13 +376,12 @@ class SessionSchemaMixin:
if isinstance(parsed, dict):
record = parsed
except (sqlite3.Error, TypeError, ValueError, json.JSONDecodeError):
- record = None
+ record = {}
try:
- first_seen = float((record or {}).get("first_seen", now))
- attempts = int((record or {}).get("attempts", 0)) + 1
+ first_seen = float(record.get("first_seen", now))
+ attempts = int(record.get("attempts", 0)) + 1
except (TypeError, ValueError):
- first_seen = now
- attempts = 1
+ first_seen, attempts = now, 1
if first_seen > now or first_seen < 0:
first_seen = now
diagnostic = {
diff --git a/hermes_state_search.py b/hermes_state_search.py
index 6a5e98d28de..95a13ebda96 100644
--- a/hermes_state_search.py
+++ b/hermes_state_search.py
@@ -318,10 +318,7 @@ class SessionSearchMixin:
(high_water,),
).fetchall()
if not upper_rows:
- conn.execute(f"DROP TABLE IF EXISTS {tbl}")
- _delete_meta(conn, marker_key)
- logger.info("Old FTS shadow table %s torn down.", tbl)
- return True
+ return _drop(conn, marker_key)
upper = upper_rows[-1][0]
cur = conn.execute(f"DELETE FROM {tbl} WHERE {key} > ? AND {key} <= ?", (high_water, upper))
if cur.rowcount > 0:
@@ -332,10 +329,17 @@ class SessionSearchMixin:
f"(SELECT {key} FROM {tbl} LIMIT {self._FTS_REBUILD_CHUNK_ROWS})"
)
if cur.rowcount == 0:
- conn.execute(f"DROP TABLE IF EXISTS {tbl}")
- logger.info("Old FTS shadow table %s torn down.", tbl)
+ return _drop(conn)
return True # re-check: more trash tables / chunks may remain
+ def _drop(conn, marker_key: Optional[str] = None) -> bool:
+ """Drained — the DROP is cheap now. True: re-check for more trash."""
+ conn.execute(f"DROP TABLE IF EXISTS {tbl}")
+ if marker_key is not None:
+ _delete_meta(conn, marker_key)
+ logger.info("Old FTS shadow table %s torn down.", tbl)
+ return True
+
try:
return bool(self._execute_write(_do))
except sqlite3.OperationalError as exc: