The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: recover fresh and historical session row layouts by identity
Teknium9c681b2d6702
diff --git a/hermes_cli/session_lost_and_found.py b/hermes_cli/session_lost_and_found.pyindex 9800515e44e..a661f34e1d3 100644--- a/hermes_cli/session_lost_and_found.py+++ b/hermes_cli/session_lost_and_found.py@@ -255,6 +255,17 @@ def _looks_like_source(value: Any) -> bool:)+def _session_source_index(cells: tuple[Any, ...]) -> Optional[int]:+ if len(cells) > 1 and _looks_like_source(cells[1]):+ return 1+ # Fresh runtime schemas inserted counters after id; upgraded databases+ # append those same columns. Width alone cannot distinguish the layouts.+ if (len(cells) > 3 and _looks_like_source(cells[3])+ and all(v is None or isinstance(v, int) for v in cells[1:3])):+ return 3+ return None++def classify_lost_and_found_row(nfield: int, cells: tuple[Any, ...]) -> Optional[str]:"""Classify one lost_and_found record by field count + sentinel values."""if len(cells) >= 3 and cells[0] is None:@@ -272,7 +283,7 @@ def classify_lost_and_found_row(nfield: int, cells: tuple[Any, ...]) -> Optional# Known sessions layouts, or an unknown historical one (>= 30 fields): session id + source is enough.if (nfield in SESSIONS_LAYOUT_NFIELDS or nfield == SESSIONS_LEGACY_MINIMAL_NFIELD or nfield >= 30- ) and _looks_like_source(second):+ ) and _session_source_index(cells) is not None:return "sessions"return None@@ -335,11 +346,15 @@ def map_lost_and_found_rows(lf_conn: sqlite3.Connection, dest: sqlite3.Connectio# Per-kind destination columns + NOT NULL substitutes. Identity fields are never fabricated:# rows with a NULL session id / role / source were already rejected by classify_lost_and_found_row.targets: dict[str, tuple[list[str], dict[int, Any]]] = {}- for kind_name, protected in (("sessions", (0, 1)), ("messages", (1, 2)), ("session_model_usage", (0, 1))):+ for kind_name, protected in (+ ("sessions", ("id", "source")), ("messages", ("session_id", "role")),+ ("session_model_usage", ("session_id", "model")),+ ):+ columns = _table_columns(dest, kind_name)defaults = _notnull_defaults(dest, kind_name)- for index in protected:- defaults.pop(index, None)- targets[kind_name] = (_table_columns(dest, kind_name), defaults)+ for name in protected:+ defaults.pop(columns.index(name), None)+ targets[kind_name] = (columns, defaults)lf_tables = [str(row[0]) for row inlf_conn.execute("SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'lost_and_found%'")@@ -361,6 +376,17 @@ def map_lost_and_found_rows(lf_conn: sqlite3.Connection, dest: sqlite3.Connectioreport["unmapped_rows"] += 1continuecolumns, defaults = targets[kind]+ if kind == "sessions" and nfield != SESSIONS_LEGACY_MINIMAL_NFIELD:+ runtime_columns = ["runtime_revision", "runtime_generation"]+ base_columns = [c for c in columns if c not in runtime_columns]+ source_columns = (+ [base_columns[0], *runtime_columns, *base_columns[1:]]+ if _session_source_index(cells) == 3+ else [*base_columns, *runtime_columns]+ )+ named_defaults = {columns[i]: value for i, value in defaults.items()}+ columns = source_columns+ defaults = {i: named_defaults[c] for i, c in enumerate(columns) if c in named_defaults}try:if kind == "sessions" and nfield == SESSIONS_LEGACY_MINIMAL_NFIELD:# Pre-modern layout with unknown column order: salvage identity + timing only.diff --git a/tests/hermes_cli/test_session_recovery_lost_and_found.py b/tests/hermes_cli/test_session_recovery_lost_and_found.pyindex 5831bbd6026..005196544ad 100644--- a/tests/hermes_cli/test_session_recovery_lost_and_found.py+++ b/tests/hermes_cli/test_session_recovery_lost_and_found.py@@ -350,6 +350,11 @@ def _make_synthetic_lost_and_found()def session_row(session_id: str, ncols: int) -> list:+ # Historical records predate the two authority columns; they are+ # not prefixes of a freshly created runtime schema.+ columns = sessions_columns if ncols == current_width else [+ c for c in sessions_columns if c not in {"runtime_revision", "runtime_generation"}+ ]base = {"id": session_id,"source": "telegram",@@ -357,7 +362,7 @@ def _make_synthetic_lost_and_found("message_count": 2,"title": f"synthetic {session_id}",}- return [base.get(column) for column in sessions_columns[:ncols]]+ return [base.get(column) for column in columns[:ncols]]# Current layout (dynamic width) and historical 52-column layout.insert(max_fields, 1, session_row("20260101_010101_aaa001", max_fields))