Skip to content

Support Claude Code subagent conversations #104

@sanggggg

Description

@sanggggg

Problem

Currently, RetroChat assumes "1 conversation = 1 file" when importing Claude Code conversations. However, Claude Code stores subagent conversations (spawned via the Task tool) in separate files, breaking this assumption.

How Claude Stores Subagent Conversations

File Structure

~/.claude/projects/{project}/
├── {sessionId}.jsonl           # Main conversation
└── agent-{agentId}.jsonl       # Subagent conversations (separate files)

Linking Mechanism

Main conversation contains a Task tool invocation and result:

// Task tool invocation (in assistant message)
{
  "type": "tool_use",
  "name": "Task",
  "input": {
    "description": "Explore data model structure",
    "prompt": "...",
    "subagent_type": "Explore"
  }
}

// Tool result with agentId reference (in user message)
{
  "toolUseResult": {
    "status": "completed",
    "agentId": "aadaaea",           // Links to agent-aadaaea.jsonl
    "prompt": "...",
    "totalDurationMs": 63921,
    "totalToolUseCount": 26
  }
}

Agent file (agent-{agentId}.jsonl) contains:

{
  "sessionId": "20b86564-...",     // Same sessionId as parent!
  "agentId": "aadaaea",
  "isSidechain": true,             // Marks as subagent conversation
  "parentUuid": "...",             // Message chain within agent
  ...
}

Key Fields

Field Location Purpose
sessionId Both files Shared identifier grouping parent + agents
agentId Agent file Unique identifier (filename: agent-{agentId}.jsonl)
isSidechain Agent file Boolean flag marking messages as subagent context
toolUseResult.agentId Parent file Links to which agent file has the full conversation

Current Behavior

  • agent-*.jsonl files are completely ignored during import
  • Subagent conversations are lost
  • The toolUseResult in the parent conversation contains only the final summary, not the full agent conversation

Proposed Solution

Schema Changes

ALTER TABLE chat_sessions ADD COLUMN parent_session_id TEXT 
    REFERENCES chat_sessions(id) ON DELETE CASCADE;
ALTER TABLE chat_sessions ADD COLUMN agent_id TEXT;
ALTER TABLE chat_sessions ADD COLUMN is_sidechain BOOLEAN DEFAULT FALSE;
ALTER TABLE chat_sessions ADD COLUMN spawn_tool_use_id TEXT;

Relationship Model

┌─────────────────────────────────────────────────────────┐
│ Main Session: 20b86564-d913-435b-ae5f-970dc6781821      │
│ file: 20b86564-....jsonl                                │
│                                                         │
│  msg1 → msg2 → [Task tool: spawn agent-aadaaea] → msg3  │
│                        │                                │
│                        ▼                                │
│         ┌─────────────────────────────────┐             │
│         │ Subagent Session: aadaaea       │             │
│         │ file: agent-aadaaea.jsonl       │             │
│         │ parent_session_id: 20b86564-... │             │
│         │ is_sidechain: true              │             │
│         │                                 │             │
│         │ agent_msg1 → agent_msg2 → ...   │             │
│         └─────────────────────────────────┘             │
└─────────────────────────────────────────────────────────┘

Implementation Tasks

  • Update ClaudeCodeParser to detect and parse agent-*.jsonl files
  • Add new fields to ChatSession model
  • Create database migration for new schema
  • Link agent sessions to parent sessions via toolUseResult.agentId
  • Update TUI to display session hierarchy (collapsible subagents?)
  • Update MCP server to expose subagent relationships
  • Consider analytics implications (aggregate parent+children?)

Notes

The "1 conversation = 1 file" paradigm still holds in a sense - Claude treats subagents as separate conversations that share a session context. The sessionId is a grouping identifier, not a file identifier.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions