{"record":{"id":"cb65b47a07ee9c99","repo":"musistudio/claude-code-router","slug":"opencode-session-list-exited-with-code-result-e","errorCode":null,"errorMessage":"OpenCode session list exited with code  + result.exitCode","messagePattern":"OpenCode session list exited with code  \\+ result\\.exitCode","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/agents/codex/cli-middleware-runtime.ts","lineNumber":1389,"sourceCode":"        if (command.name === \"models\") return this.renderModels(command.args);\n        if (command.name === \"usage\") return this.renderSessionUsage(key);\n        if (command.name === \"memory\") return this.updateSessionMemory(key, command.args);\n        if (command.name === \"skills\") return renderAgentSkills(directory, \"opencode\");\n        if (command.name === \"skill\") return forwardSkillCommand(command.args);\n        if (command.name === \"shortcut\") return this.handleSessionShortcut(key, command.args);\n        if (command.name === \"doctor\") return renderBotDiagnostics(bridge.diagnostics());\n        if (command.name === \"deliveries\") return renderBotDeliveries(bridge.diagnostics());\n      }\n      return null;\n    } catch (error) {\n      return \"OpenCode bot command failed: \" + conciseError(error);\n    }\n  }\n\n  async listSessions(includeArchived = false) {\n    const result = await runOpenCodeBotCli(this.command, [\"session\", \"list\", \"--format\", \"json\", \"-n\", \"100\"], this.defaultCwd);\n    if (result.exitCode !== 0) {\n      throw new Error(result.error || result.stderr || \"OpenCode session list exited with code \" + result.exitCode);\n    }\n    const store = this.loadStore();\n    return parseOpenCodeSessionList(result.stdout)\n      .map((session) => ({ ...session, title: store.sessionAliases[session.id] || session.title, archived: store.archivedSessionIds.includes(session.id) }))\n      .filter((session) => includeArchived ? session.archived : !session.archived);\n  }\n\n  loadStore() {\n    if (this.store) return this.store;\n    const value = readJsonFile(openCodeBotSessionStorePath());\n    const conversations = value && typeof value === \"object\" &&\n      Number(value.version || 0) >= 2 &&\n      value.conversations && typeof value.conversations === \"object\"\n      ? value.conversations\n      : {};\n    const pendingTurns = value && Array.isArray(value.pendingTurns) ? value.pendingTurns.filter((item) => item && typeof item === \"object\") : [];\n    const projectAliases = value && value.projectAliases && typeof value.projectAliases === \"object\" ? value.projectAliases : {};\n    const sessionAliases = value && value.sessionAliases && typeof value.sessionAliases === \"object\" ? value.sessionAliases : {};","sourceCodeStart":1371,"sourceCodeEnd":1407,"githubUrl":"https://github.com/musistudio/claude-code-router/blob/99f24806c6a2c660b16e53e95211c517448a6c90/packages/core/src/agents/codex/cli-middleware-runtime.ts#L1371-L1407","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run the CLI manually to see the real error: opencode session list --format json -n 100","Upgrade or pin the opencode CLI to the version this middleware expects so `session list --format json` is supported","Clear or repair the opencode session store (back it up first) if it is corrupted","Verify the CLI binary resolves on PATH from the middleware's defaultCwd and that auth is valid"],"exampleFix":"// before\nconst sessions = await runtime.listSessions();\n\n// after\nconst sessions = await runtime.listSessions().catch((err) => {\n  console.error(\"session list failed:\", err.message);\n  return []; // degrade gracefully instead of aborting\n});","handlingStrategy":"try-catch","validationCode":"null","typeGuard":"null","tryCatchPattern":"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; }","preventionTips":["Pin the opencode CLI version the middleware was tested against","Smoke-test `opencode session list --format json` during deployment checks","Keep session store backups so corruption can be recovered quickly"],"tags":["cli","subprocess","session","exit-code"],"backgroundTag":"subprocess-nonzero-exit","analyzedSha":"99f24806c6a2c660b16e53e95211c517448a6c90","analyzedAt":"2026-08-27T04:11:01.184Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}