The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(sessions): spare pinned rows in the startup orphan sweep
686f6c6167900a0b30d3
diff --git a/tests/tui_gateway/test_startup_orphan_sweep.py b/tests/tui_gateway/test_startup_orphan_sweep.pyindex 49abc1070e3..8e1a75fbc24 100644--- a/tests/tui_gateway/test_startup_orphan_sweep.py+++ b/tests/tui_gateway/test_startup_orphan_sweep.py@@ -119,6 +119,21 @@ class TestSweepOrphanedSessionRows:assert server._sweep_orphaned_session_rows() == []assert db.get_session("fresh-branch")["ended_at"] is None+ def test_spares_pinned_idle_row(self, monkeypatch, tmp_path):+ """Pinned rows are prune-immune (#68035); the startup sweep must match auto-prune."""+ db = SessionDB(tmp_path / "state.db")+ stale = time.time() - 8 * 3600+ _seed_session(db, "pinned-tui", source="tui", last_active=stale)+ _seed_session(db, "stale-tui", source="tui", last_active=stale)+ db.set_session_pinned("pinned-tui", True)+ monkeypatch.setattr(server, "_get_db", lambda: db)+ monkeypatch.setattr(server, "_SESSION_TTL_S", float(IDLE_S))+ monkeypatch.setattr(server, "_sessions", {})++ assert server._sweep_orphaned_session_rows() == ["stale-tui"]+ assert db.get_session("pinned-tui")["ended_at"] is None+ assert db.get_session("stale-tui")["end_reason"] == "startup_orphan_reap"+def test_spares_live_in_memory_and_gateway_rows(self, monkeypatch, tmp_path):db = SessionDB(tmp_path / "state.db")stale = time.time() - 8 * 3600diff --git a/tui_gateway/session_reaper.py b/tui_gateway/session_reaper.pyindex 8afc252a8dd..d4e94441ba2 100644--- a/tui_gateway/session_reaper.py+++ b/tui_gateway/session_reaper.py@@ -407,7 +407,8 @@ def _sweep_orphaned_session_rows() -> list[str]:candidates += [getattr(session.get("agent"), "session_id", None), session.get("session_key")]live_ids.update(str(c) for c in candidates if c)swept = db.sweep_orphaned_sessions(- max_idle_seconds=_SESSION_TTL_S, sources=_ORPHAN_SWEEP_SOURCES, exclude_ids=tuple(sorted(live_ids)))+ max_idle_seconds=_SESSION_TTL_S, sources=_ORPHAN_SWEEP_SOURCES,+ exclude_ids=tuple(sorted(live_ids)), exclude_pinned=True)if swept:logger.info("Closed %d orphaned session row(s) from a previous gateway process (startup_orphan_reap): %s",