56255f83f7
_collect_delegate_child_ids() walks the _delegate_from marker chain to gather delegate subagents for cascade deletion, but started its visited set empty. When the chain loops back onto a parent — a delegation cycle, or a parent that is also another parent's delegate child when several ids are deleted together — that parent was collected as one of its own descendants and then permanently deleted, along with all of its messages, by _delete_delegate_children(). Seed the visited set with the parent ids so they can never be re-collected, and exclude them from the returned child set. Callers (delete_session, bulk delete) remove the parents separately, so this only prevents the unintended parent deletion; legitimate child collection is unchanged. Add regression tests (in-memory sqlite) covering single/multi-level delegate chains, the parent_session_id+marker branch, untagged children (orphan-don't-delete contract), and the cycle case that previously leaked the parent into the deletion set. Fixes #49148