Skip to content

Pi adapter: support OPENCLAW_STATE_DIR and multi-agent session directories #128

@efacsen

Description

@efacsen

Problem

The Pi adapter hardcodes the session directory to ~/.openclaw/agents/main/sessions/. This doesn't work for:

  1. Custom state directories — OpenClaw supports OPENCLAW_STATE_DIR env var to relocate the state directory (common for staging/production setups on the same machine, documented here)
  2. Multi-agent setups — OpenClaw supports multiple named agents under agents/<agentId>/sessions/ (see docs/pi.md), but the adapter only reads agents/main/

My setup

I run staging and production OpenClaw on the same VPS with separate state dirs and multiple agents:

$STAGING_STATE_DIR/agents/agent-a/sessions/   (8 sessions)
$STAGING_STATE_DIR/agents/main/sessions/      (17 sessions)
$PRODUCTION_STATE_DIR/agents/main/sessions/   (31 sessions)

The adapter finds none of these — it only reads ~/.openclaw/agents/main/sessions/.

Proposed Solution

Two small changes:

1. Respect OPENCLAW_STATE_DIR env var

func resolveBaseDir() string {
    if dir := os.Getenv("OPENCLAW_STATE_DIR"); dir != "" {
        return dir
    }
    home, _ := os.UserHomeDir()
    return filepath.Join(home, ".openclaw")
}

2. Scan agents/*/sessions/ instead of just agents/main/sessions/

// Glob all agent directories
entries, _ := os.ReadDir(filepath.Join(baseDir, "agents"))
for _, e := range entries {
    sessDir := filepath.Join(baseDir, "agents", e.Name(), "sessions")
    // add to watch list
}

Impact

  • CWD filtering still works correctly (each session has its own cwd in the header)
  • The watcher watches all agent directories with fsnotify
  • Session names remain unique (UUIDs)
  • No API changes needed — just broader discovery

Usage

# Point sidecar at a specific OpenClaw instance
OPENCLAW_STATE_DIR=/path/to/state sidecar

I've already built a working patch locally — happy to submit a PR if you'd prefer that. All 42 existing Pi adapter tests pass with the changes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions