Skip to content

bug: chat-created agents lose orchestrator state on restart #35

@ianphil

Description

@ianphil

Bug Description

After closing and reopening cmux, chat-created agents appear in the left pane but the chat orchestrator has no knowledge of them. Asking "What agents do I have?" in chat returns empty, and �p_send_to_agent fails because the orchestrator doesn't know the agent exists.

Chat history is restored. The agent entry in the left pane is restored. But the orchestrator's in-memory state and SDK sessions are not.

Steps to Reproduce

  1. Open cmux, switch to Chat view
  2. Ask chat to create one or more agents (e.g., "Create an agent for ~/src/my-project")
  3. Agents appear in left pane with activity feed working
  4. Close cmux completely
  5. Reopen cmux
  6. Agents still appear in left pane (persisted via session.json)
  7. Ask chat "What agents do I have?" → reports none
  8. Activity feed cards are also gone (expected — documented as in-memory only)

Root Cause

Three things are persisted to disk but not reconnected on restore:

1. OrchestratorTools.managedAgents Map is never repopulated

src/main/services/OrchestratorTools.ts:25managedAgents is a plain in-memory Map<string, { label, cwd }>, populated only when vp_create_agent runs. Nothing repopulates it on startup, so both vp_list_agents and the enrichPrompt() context injection (via getActiveAgents()) return empty.

2. useSessionRestore doesn't notify the main process

src/renderer/hooks/useSessionRestore.ts:23-33 — for hasSession: true agents, the restore path dispatches ADD_AGENT to the renderer state but never calls any IPC to register the agent back into the main process orchestrator.

3. SDK sessions are not recreated

src/main/services/AgentSessionService.ts holds sessions in an in-memory Map. The restore path never calls agent-session:create for hasSession: true agents, so no SDK session exists for them.

Proposed Fix

  1. Add a registration IPC handler (e.g., orchestrator:register-agent) in OrchestratorTools.ts or copilot.ts that inserts an agent into managedAgents without creating a new SDK session.

  2. Call it from useSessionRestore for each hasSession: true agent during restore, so the orchestrator knows about pre-existing agents.

  3. Lazy-recreate SDK sessions — when vp_send_to_agent is called for a registered agent that has no active SDK session, create the session on demand rather than failing.

This keeps the restore path lightweight (no eager SDK session creation) while ensuring chat awareness of existing agents.

Related

Affected Files

File Role
src/main/services/OrchestratorTools.ts:25 managedAgents Map (never repopulated)
src/main/services/OrchestratorTools.ts:116-136 vp_list_agents reads from empty map
src/main/services/CopilotService.ts:137-143 enrichPrompt() injects empty agent context
src/renderer/hooks/useSessionRestore.ts:23-33 Restore path skips main process registration
src/main/ipc/copilot.ts:8-26 Tool init, agent context provider setup
src/main/services/AgentSessionService.ts SDK sessions not recreated

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