fix(state): tolerate malformed session marker JSON

This commit is contained in:
Efe Büken
2026-09-04 22:53:27 +03:00
committed by Teknium
parent 78f85112d9
commit a239c4f811
6 changed files with 87 additions and 31 deletions
+7 -14
View File
@@ -12,14 +12,15 @@ import time
from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Tuple
from hermes_state_common import _RECOVERABLE_END_REASONS_SQL, _RESET_END_REASONS_SQL, _sql_session_last_active
from hermes_state_common import (
_RECOVERABLE_END_REASONS_SQL, _RESET_END_REASONS_SQL, _sql_json_extract, _sql_session_last_active)
# Log-record parity with the origin module (caplog tests pin "hermes_state").
logger = logging.getLogger("hermes_state")
# Recursive CTE naming a session plus its compression ancestors (rows a
# resume must keep on one routing peer); branch/delegate/tool rows stop it.
_COMPRESSION_LINEAGE_CTE = """
_COMPRESSION_LINEAGE_CTE = f"""
WITH RECURSIVE compression_lineage(id) AS (
SELECT ?
UNION
@@ -28,14 +29,8 @@ _COMPRESSION_LINEAGE_CTE = """
JOIN sessions child ON child.id = lineage.id
JOIN sessions parent ON parent.id = child.parent_session_id
WHERE parent.end_reason = 'compression'
AND json_extract(
COALESCE(child.model_config, '{}'),
'$._branched_from'
) IS NULL
AND json_extract(
COALESCE(child.model_config, '{}'),
'$._delegate_from'
) IS NULL
AND {_sql_json_extract('child.model_config', '$._branched_from')} IS NULL
AND {_sql_json_extract('child.model_config', '$._delegate_from')} IS NULL
AND COALESCE(child.source, '') != 'tool'
)
"""
@@ -108,10 +103,8 @@ _ORPHANS_SQL = f"""
AND EXISTS (SELECT 1 FROM messages m
WHERE m.session_id = o.id)
AND COALESCE(o.source, '') != 'tool'
AND json_extract(COALESCE(o.model_config, '{{}}'),
'$._branched_from') IS NULL
AND json_extract(COALESCE(o.model_config, '{{}}'),
'$._delegate_from') IS NULL
AND {_sql_json_extract('o.model_config', '$._branched_from')} IS NULL
AND {_sql_json_extract('o.model_config', '$._delegate_from')} IS NULL
ORDER BY o.started_at ASC
"""
_ORPHAN_LINEAGE_DONOR_SQL = f"""