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 []).
This commit is contained in:
kshitijk4poor
2026-10-07 23:08:50 +05:30
committed by Siddharth Balyan
parent 9d33231734
commit 708d45f90e

View File

@@ -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]]: