Skip to content

ACP session state machine and orchestration #1004

Closed

Description

@malibio

Overview

Implement the session lifecycle for external agent interactions: initialize a session with an agent, manage the state machine, handle message routing, and ensure clean teardown. This orchestrates the ACP transport and registry into a coherent session experience.

Acceptance Criteria

  • State machine enforces valid transitions, rejects invalid operations
  • Full lifecycle: create session, exchange messages, complete cleanly
  • Initialization handshake: send ACP initialize, receive capabilities, transition to Active
  • Timeout handling: 30s initialization, configurable message timeout
  • Error recovery: transport failure transitions to Failed with diagnostic reason
  • Multi-session: concurrent sessions with different agent types work
  • Single-session per agent: second session for same agent type is rejected with clear error
  • Unit tests with mock transport for all state transitions and edge cases
  • Integration test with a simple test agent subprocess (echo server)
  • Code passes bun run quality:fix

Technical Specifications

New Module

  • packages/desktop-app/src-tauri/src/acp/session.rs

State Machine

Idle -> Initializing -> Active -> Completing -> Completed
                    \                        \-> Failed
                     \-> Failed
  • Enforce valid transitions at the type level or with runtime checks
  • Reject operations invalid for current state (e.g., send message while Initializing)
  • Store reason string when transitioning to Failed

Session Initialization

  1. Verify agent is available via AgentRegistry
  2. Spawn transport via StdioTransport with agent's binary/args/env
  3. Send ACP initialize message: { jsonrpc: "2.0", method: "initialize", params: { protocolVersion: "1", capabilities: {...} } }
  4. Wait for initialized response (30s timeout)
  5. Send session/new with MCP server config: { mcpServers: [{ name: "nodespace", url: "http://localhost:3100/mcp" }] }
  6. Transition to Active

Message Routing (Active State)

  • Forward user messages to agent via transport
  • Receive agent responses and forward to frontend via Tauri events
  • Agent tool-call requests happen out-of-band (agent -> MCP HTTP server)
  • Track message count for session metadata

Completion

  • Send completion signal
  • Wait for agent's final response (configurable timeout)
  • Transition to Completed
  • Tear down transport gracefully

Multi-Session Management

  • HashMap of active sessions keyed by session ID
  • One active session per AgentKind (reject duplicates)
  • Allow concurrent sessions with different agent types
  • Session cleanup on completion or failure

Error Handling

  • Transport failure -> immediate transition to Failed
  • Timeout on any operation -> retry once, then Failed
  • Agent process crash -> Failed with stderr output
  • All errors include diagnostic info for frontend display

Reference Files

Exports

  • AcpSession struct
  • AcpClientService facade for managing multiple sessions

Dependencies

Related Issues

Activity

  1. malibio commented on Apr 3, 2026

    @malibio
    CollaboratorAuthor

    Baseline: Frontend tests have a pre-existing Vitest startup error (TypeError in error handler). This is a known issue on the worktree, not introduced by this change. Rust backend is the focus of this issue.

  2. self-assigned this
    on Apr 3, 2026
  3. added 2 commits that reference this issue on Apr 3, 2026
    74abef5
    a312f38
  4. added a commit that references this issue on Jun 22, 2026
    97e9391
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions