Skip to content

bug: delegate_task subagent sessions pollute session list as orphan entries #5122

@rahimsais

Description

@rahimsais

Description

When delegate_task spawns subagent sessions, they appear in hermes sessions list and /resume as untitled orphan entries indistinguishable from real user sessions.

Root Cause

delegate_tool.py passes session_db to child agents (line 229) but not parent_session_id. The child agent calls create_session() (run_agent.py:968) with parent_session_id=None, creating a top-level session with source=cli.

The DB schema already has parent_session_id as a FK column on the sessions table, and session_search already handles it correctly (filters children in recent list, resolves to parent in FTS). But delegate_tool never wires it up.

Reproduction

hermes chat
> delegate a task to check the date
> /resume

The subagent session appears as an untitled entry alongside real sessions.

Impact

  • hermes sessions list shows orphan subagent sessions
  • /resume picker shows them
  • session_search recent sessions list shows them
  • Each delegate_task call (especially batch with 3 tasks) creates 1-3 extra visible sessions

Additional: FK cascade bug in delete_session/prune_sessions

The sessions.parent_session_id FK has no ON DELETE CASCADE. When sessions DO have parent_session_id set (compression chains today, subagents after fix), deleting a parent raises IntegrityError because children reference it.

delete_session() and prune_sessions() need to delete children before parents.

Suggested Fix

  1. Pass parent_session_id=parent_agent.session_id in delegate_tool._build_child_agent()
  2. Add parent_session_id param to run_agent.py __init__, pass to create_session()
  3. Filter parent_session_id IS NULL in list_sessions_rich()
  4. Delete children first in delete_session()/prune_sessions()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions