atlas-iac/services/hermes/scripts/migrate_api_session_lineage.py

69 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""Nest known legacy agent API workers under their originating objective."""
from __future__ import annotations
import sqlite3
from pathlib import Path
STATE_DB = Path("/opt/data/state.db")
LEGACY_CASSANDRA_PARENT = "20260812_042739_d6ee37"
LEGACY_CASSANDRA_WORKERS = {
"api-d947ebb51d09eb97": "Cassandra handoff existence check",
"api-bd98c6145593e9e9": "Cassandra handoff heading check",
"api-cb00bdf2f7cd3889": "Cassandra worktree file check",
"api-91b5c2de1759380b": "Agent hosted route check",
"api-0d1d15e79c619f7b": "Cassandra file-tool check",
"api-5550b3556424fbdc": "Cassandra handoff summary",
"api-c288b9f3024a91b8": "Cassandra stale-worktree check",
"api-058e51802b58ee3f": "Cassandra verification worker",
}
LEGACY_ORPHANED_SMOKE_SESSIONS = {
"e17f2d888689": "Archived agent workspace smoke test",
}
def migrate(path: Path = STATE_DB) -> int:
"""Apply idempotent, transcript-preserving lineage corrections."""
if not path.is_file():
return 0
changed = 0
with sqlite3.connect(path) as connection:
parent = connection.execute(
"SELECT id FROM sessions WHERE id = ?", (LEGACY_CASSANDRA_PARENT,)
).fetchone()
if parent:
for session_id, title in LEGACY_CASSANDRA_WORKERS.items():
cursor = connection.execute(
"""
UPDATE sessions
SET parent_session_id = ?,
title = ?
WHERE id = ?
AND source = 'api_server'
AND parent_session_id IS NULL
""",
(LEGACY_CASSANDRA_PARENT, title, session_id),
)
changed += cursor.rowcount
for session_id, title in LEGACY_ORPHANED_SMOKE_SESSIONS.items():
cursor = connection.execute(
"""
UPDATE sessions
SET archived = 1,
title = ?
WHERE id = ?
AND source = 'api_server'
AND parent_session_id IS NULL
AND archived = 0
""",
(title, session_id),
)
changed += cursor.rowcount
return changed
if __name__ == "__main__":
print(f"migrated {migrate()} legacy API sessions")