fix(sessions): preserve branch lineage on reopen

This commit is contained in:
KoNit-K
2026-09-14 12:20:53 +08:00
committed by Teknium
parent 1f08821bd0
commit a5aa2379f0
2 changed files with 76 additions and 3 deletions

View File

@@ -448,14 +448,18 @@ class SessionSessionsMixin:
return changed
def reopen_session(self, session_id: str) -> None:
"""Clear ended_at/end_reason so a session can be resumed; first stamp markerless legacy reset
children that depend on the parent's mutable end_reason (WHERE shared with the listing predicate
so they cannot drift)."""
"""Clear ended_at/end_reason so a session can be resumed; first freeze only legacy reset children
whose creation order proves they followed the reset. The shared legacy predicate keeps list-time
compatibility broad, while this durable backfill must not overwrite explicit fork provenance."""
def _do(conn):
conn.execute(
"UPDATE sessions AS child SET model_config = json_set("
"COALESCE(child.model_config, '{}'), '$._reset_from', child.parent_session_id) "
f"WHERE child.parent_session_id = ? AND {_sql_json_extract('child.model_config', '$._reset_from')} IS NULL "
f"AND {_sql_json_extract('child.model_config', '$._branched_from')} IS NULL "
f"AND {_sql_json_extract('child.model_config', '$._delegate_from')} IS NULL "
"AND COALESCE(child.source, '') != 'tool' "
"AND child.started_at >= (SELECT p.ended_at FROM sessions p WHERE p.id = child.parent_session_id) "
f"AND {_legacy_reset_child_sql('child', _session_ids_placeholders(_RESET_END_REASONS))}",
(session_id, *_RESET_END_REASONS),
)