fix: conditional promote + real SessionDB tests

Address review feedback on #63068:

1. Replace unconditional reopen_session() + end_session() with a
   conditional promote_to_session_reset() method in SessionDB.
   The new method only promotes live rows or rows ended with
   agent_close — explicit boundaries (compression, session_reset,
   new_command) are preserved via first-writer-wins semantics.

2. Rewrite tests to use real SessionDB instead of MagicMock:
   - 7 unit tests for promote_to_session_reset edge cases
   - 3 integration tests verifying the actual recovery contract
     in find_latest_gateway_session_for_peer after promotion
This commit is contained in:
saitsuki
2026-07-16 07:53:59 +08:00
committed by Teknium
parent e701cdc86e
commit 3305dcedbb
3 changed files with 199 additions and 49 deletions
+30
View File
@@ -2286,6 +2286,36 @@ class SessionDB:
)
self._execute_write(_do)
def promote_to_session_reset(self, session_id: str) -> bool:
"""Mark a session as ended by session_reset — but only when safe.
Promotes *only* live rows (``ended_at IS NULL``) or rows ended with
``agent_close``. Explicit conversation boundaries such as
``compression``, ``session_reset``, ``new_command``, etc. are
preserved — the first writer wins for those, and a later expiry
finalization must not silently overwrite them.
Returns ``True`` when the row was promoted, ``False`` when skipped
(already has a different explicit end_reason, or row not found).
"""
if not session_id:
return False
now = time.time()
def _do(conn):
cursor = conn.execute(
"UPDATE sessions SET ended_at = ?, end_reason = 'session_reset' "
"WHERE id = ? AND (ended_at IS NULL OR end_reason = 'agent_close')",
(now, session_id),
)
return cursor.rowcount
try:
rows = self._execute_write(_do)
return bool(rows)
except Exception:
return False
def update_session_cwd(
self, session_id: str, cwd: str, git_branch: str = None, git_repo_root: str = None
) -> None: