From 708d45f90edffdd91735ca4bcec0c8147eb8e4c5 Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Wed, 7 Oct 2026 23:08:50 +0530 Subject: [PATCH] perf(state): pre-filter the per-turn tool-retry scan with LIKE take_unannounced_tool_retries runs on every desktop turn (prompt_turn, no flag gate) and json-parsed display_metadata on every row of the session's lineage. A LIKE '%"retried"%' pre-filter skips the parse on rows that cannot hold the key; json_extract still decides, so results are unchanged. Measured on a real SessionDB, 20,000-row session, warm: 8.43 -> 6.09 ms/turn. Announce-once behaviour re-checked by probe (take1 returns the retry, take2 []). --- hermes_state_tool_retries.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hermes_state_tool_retries.py b/hermes_state_tool_retries.py index 40599dcfbb..02994ddc29 100644 --- a/hermes_state_tool_retries.py +++ b/hermes_state_tool_retries.py @@ -49,8 +49,11 @@ class SessionToolRetriesMixin: return [] lineage = self._resume_lineage_ids(session_id) key = self.TOOL_RETRY_METADATA_KEY + # Every turn of every desktop chat asks. The LIKE pre-filter skips the JSON parse on rows that cannot hold + # the key (nearly all of them); json_extract still decides. select = ("SELECT id, tool_name, display_metadata FROM messages " f"WHERE session_id IN ({_placeholders(lineage)}){_DISPLAY_ACTIVE_CLAUSE} " + f"AND display_metadata LIKE '%\"{key}\"%' " f"AND {_sql_json_extract('display_metadata', '$.' + key)} IS NOT NULL ORDER BY id") def _unannounced(row) -> Optional[Dict[str, Any]]: