chenhg5/cc-connect · error

opencode: session list: %w

Error message

opencode: session list: %w

What it means

listOpencodeSessions runs `opencode session list --format json` and reads its stdout via c.Output(); if the command fails (non-zero exit), the exec error is wrapped as `opencode: session list: %w`. Note only the exec error is wrapped — the CLI's diagnostic output (available via ExitError.Stderr) is not included, so check the wrapped error's type for details.

Source

Thrown at agent/opencode/opencode.go:632

}

// -- Session listing --

// opencodeSessionEntry represents a session from `opencode session list` output.
type opencodeSessionEntry struct {
	ID      string `json:"id"`
	Title   string `json:"title"`
	Updated int64  `json:"updated"` // Unix timestamp in milliseconds
	Created int64  `json:"created"`
}

func listOpencodeSessions(cmd, workDir string) ([]core.AgentSessionInfo, error) {
	c := exec.Command(cmd, "session", "list", "--format", "json")
	c.Dir = workDir

	out, err := c.Output()
	if err != nil {
		return nil, fmt.Errorf("opencode: session list: %w", err)
	}

	var entries []opencodeSessionEntry
	if err := json.Unmarshal(out, &entries); err != nil {
		return nil, fmt.Errorf("opencode: parse session list: %w", err)
	}

	msgCounts := querySessionMessageCounts()

	var sessions []core.AgentSessionInfo
	for _, e := range entries {
		sessions = append(sessions, core.AgentSessionInfo{
			ID:           e.ID,
			Summary:      e.Title,
			MessageCount: msgCounts[e.ID],
			ModifiedAt:   time.UnixMilli(e.Updated),
		})
	}

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Check the wrapped exec.ExitError's Stderr for opencode's own message; run `opencode session list --format json` manually in workDir to reproduce.
  2. Verify CLI version supports `--format json` (`opencode session list --help`); upgrade/downgrade opencode to match this adapter.
  3. Fix PATH/workDir/permissions for the daemon user, then retry ListSessions.
  4. Guard callers: on this error fall back to an empty session list or surface a clear message instead of crashing the command handler.

Example fix

// before
sessions, err := agent.ListSessions(ctx)
if err != nil {
    return err
}

// after
sessions, err := agent.ListSessions(ctx)
if err != nil {
    var exitErr *exec.ExitError
    if errors.As(err, &exitErr) {
        slog.Warn("opencode session list failed", "stderr", string(exitErr.Stderr))
    }
    return nil // degraded: empty list instead of hard failure
}
Defensive patterns

Strategy: retry

Validate before calling

// verify the CLI works in this workDir before relying on ListSessions
probe := exec.Command(opencodeCmd, "session", "list", "--format", "json")
probe.Dir = workDir
if out, err := probe.CombinedOutput(); err != nil {
    return fmt.Errorf("opencode session list unusable here: %v: %s", err, out)
}

Type guard

func isSessionListFailure(err error) bool {
    return err != nil && strings.Contains(err.Error(), "opencode: session list:")
}

Try / catch

sessions, err := agent.ListSessions(ctx)
if err != nil {
    var exitErr *exec.ExitError
    if errors.As(err, &exitErr) {
        slog.Warn("opencode session list failed", "stderr", string(exitErr.Stderr))
    }
    sessions = nil // degrade gracefully
}

Prevention

When it happens

Trigger: Calling ListSessions when: the opencode CLI cannot run in workDir (missing binary, bad workDir, permissions); opencode exits non-zero (corrupt session store, changed subcommand flags such as --format being removed in a newer/older CLI version); the command times out or is killed.

Common situations: CLI version mismatch (older opencode without `--format json`); running as a user without access to the opencode data directory; workDir deleted after the agent was constructed; PATH issues for the daemon.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/f71766e86fbd3b57. Report an issue: GitHub.