musistudio/claude-code-router · error · Error

OpenCode session list exited with code + result.exitCode

Error message

OpenCode session list exited with code  + result.exitCode

What it means

listSessions() in the OpenCode CLI middleware runtime shells out to `opencode session list --format json` and throws when the process exits non-zero. The thrown message prefers the CLI's error/stderr output and only falls back to the generic 'exited with code N' text when both streams are empty. It almost always indicates the bundled/external OpenCode CLI failed to start or parse its session store.

Source

Thrown at packages/core/src/agents/codex/cli-middleware-runtime.ts:1389

        if (command.name === "models") return this.renderModels(command.args);
        if (command.name === "usage") return this.renderSessionUsage(key);
        if (command.name === "memory") return this.updateSessionMemory(key, command.args);
        if (command.name === "skills") return renderAgentSkills(directory, "opencode");
        if (command.name === "skill") return forwardSkillCommand(command.args);
        if (command.name === "shortcut") return this.handleSessionShortcut(key, command.args);
        if (command.name === "doctor") return renderBotDiagnostics(bridge.diagnostics());
        if (command.name === "deliveries") return renderBotDeliveries(bridge.diagnostics());
      }
      return null;
    } catch (error) {
      return "OpenCode bot command failed: " + conciseError(error);
    }
  }

  async listSessions(includeArchived = false) {
    const result = await runOpenCodeBotCli(this.command, ["session", "list", "--format", "json", "-n", "100"], this.defaultCwd);
    if (result.exitCode !== 0) {
      throw new Error(result.error || result.stderr || "OpenCode session list exited with code " + result.exitCode);
    }
    const store = this.loadStore();
    return parseOpenCodeSessionList(result.stdout)
      .map((session) => ({ ...session, title: store.sessionAliases[session.id] || session.title, archived: store.archivedSessionIds.includes(session.id) }))
      .filter((session) => includeArchived ? session.archived : !session.archived);
  }

  loadStore() {
    if (this.store) return this.store;
    const value = readJsonFile(openCodeBotSessionStorePath());
    const conversations = value && typeof value === "object" &&
      Number(value.version || 0) >= 2 &&
      value.conversations && typeof value.conversations === "object"
      ? value.conversations
      : {};
    const pendingTurns = value && Array.isArray(value.pendingTurns) ? value.pendingTurns.filter((item) => item && typeof item === "object") : [];
    const projectAliases = value && value.projectAliases && typeof value.projectAliases === "object" ? value.projectAliases : {};
    const sessionAliases = value && value.sessionAliases && typeof value.sessionAliases === "object" ? value.sessionAliases : {};

View on GitHub (pinned to 99f24806c6)

Solutions

  1. Run the CLI manually to see the real error: opencode session list --format json -n 100
  2. Upgrade or pin the opencode CLI to the version this middleware expects so `session list --format json` is supported
  3. Clear or repair the opencode session store (back it up first) if it is corrupted
  4. Verify the CLI binary resolves on PATH from the middleware's defaultCwd and that auth is valid

Example fix

// before
const sessions = await runtime.listSessions();

// after
const sessions = await runtime.listSessions().catch((err) => {
  console.error("session list failed:", err.message);
  return []; // degrade gracefully instead of aborting
});
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

null

Try / catch

try { const s = await runtime.listSessions(); } catch (e) { if (/session list exited|exited with code/.test(String(e))) { /* run `opencode session list --format json` to diagnose, return [] */ } else throw e; }

Prevention

When it happens

Trigger: Calling listSessions() (thread list / session listing) when runOpenCodeBotCli returns exitCode !== 0: corrupt session store, wrong CLI version not supporting `session list --format json`, CLI binary missing/broken PATH, or auth failure inside the CLI.

Common situations: opencode CLI upgraded and changed subcommand flags; session storage file corrupted after a crash; middleware pinned to an old CLI version; sandboxed environment where the CLI cannot spawn.

Related errors


AI-assisted analysis of musistudio/claude-code-router@99f24806c6 (2026-08-27). Data as JSON: /api/errors/cb65b47a07ee9c99. Report an issue: GitHub.