Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

fix(opencode): read sessions from OpenCode's data directory (#848)

Rudy Mizrahi Celeklie24c3719575e
diff --git a/cli/beacon/cmd/endpoint_opencode.go b/cli/beacon/cmd/endpoint_opencode.go
index d97fed80..04468a93 100644
--- a/cli/beacon/cmd/endpoint_opencode.go
+++ b/cli/beacon/cmd/endpoint_opencode.go
@@ -56,7 +56,7 @@ func init() {
for _, c := range []*cobra.Command{endpointOpenCodeSyncCmd, endpointOpenCodeStatusCmd} {
f := c.Flags()
- f.StringVar(&endpointOpenCodeOpts.dataDir, "data-dir", "", "OpenCode data directory or opencode.db path (default ~/.config/opencode)")
+ f.StringVar(&endpointOpenCodeOpts.dataDir, "data-dir", "", "OpenCode data directory or opencode.db path (default $XDG_DATA_HOME/opencode or ~/.local/share/opencode, honoring OPENCODE_DB)")
f.StringVar(&endpointOpenCodeOpts.statePath, "state", "", "Collector cursor file (default ~/.beacon/endpoint/state/opencode-sessions.json)")
f.BoolVar(&endpointOpts.jsonOutput, "json", false, "Print the result as JSON")
f.BoolVar(&endpointOpts.userMode, "user", true, "Use per-user endpoint paths")
diff --git a/cli/beacon/internal/opencodesession/default_dir_test.go b/cli/beacon/internal/opencodesession/default_dir_test.go
new file mode 100644
index 00000000..87bf6423
--- /dev/null
+++ b/cli/beacon/internal/opencodesession/default_dir_test.go
@@ -0,0 +1,66 @@
+package opencodesession
+
+import (
+ "database/sql"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv"
+)
+
+func TestDefaultStoreFindsOpenCodeData(t *testing.T) {
+ for _, tc := range []struct{ name, xdgData, opencodeDB, explicit, dbRel string }{
+ {name: "home", dbRel: ".local/share/opencode/opencode.db"},
+ {name: "xdg", xdgData: "xdg", dbRel: "xdg/opencode/opencode.db"},
+ {name: "relative OPENCODE_DB", opencodeDB: "work.db", dbRel: ".local/share/opencode/work.db"},
+ {name: "absolute OPENCODE_DB", opencodeDB: "elsewhere/custom.db", dbRel: "elsewhere/custom.db"},
+ {name: "explicit data dir", explicit: "config/opencode", dbRel: "config/opencode/opencode.db"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ home := t.TempDir()
+ testenv.SetHome(t, home)
+ t.Setenv("XDG_DATA_HOME", "")
+ if tc.xdgData != "" {
+ t.Setenv("XDG_DATA_HOME", filepath.Join(home, tc.xdgData))
+ }
+ t.Setenv("OPENCODE_DB", tc.opencodeDB)
+ if tc.name == "absolute OPENCODE_DB" {
+ t.Setenv("OPENCODE_DB", filepath.Join(home, tc.opencodeDB))
+ }
+ // A config-directory store is never the default: OpenCode does not write sessions there.
+ writeSessionDB(t, filepath.Join(home, ".config", "opencode", "opencode.db"), "ses_config")
+ writeSessionDB(t, filepath.Join(home, filepath.FromSlash(tc.dbRel)), "ses_want")
+
+ argument := ""
+ if tc.explicit != "" {
+ argument = filepath.Join(home, filepath.FromSlash(tc.explicit))
+ }
+ store, err := NewStore(argument)
+ if err != nil {
+ t.Fatal(err)
+ }
+ refs, err := store.List()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(refs) != 1 || refs[0].ID != "ses_want" {
+ t.Fatalf("refs=%+v, want ses_want (sqlite %s)", refs, store.SQLitePath)
+ }
+ })
+ }
+}
+
+func writeSessionDB(t *testing.T, path, id string) {
+ t.Helper()
+ if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
+ t.Fatal(err)
+ }
+ db, err := sql.Open("sqlite", path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer db.Close()
+ mustExec(t, db, `CREATE TABLE session(id TEXT PRIMARY KEY,title TEXT,directory TEXT,time_created INTEGER,time_updated INTEGER)`)
+ mustExec(t, db, `INSERT INTO session VALUES(?, 'Owned session', '/work/project', 1791882000000, 1791882000200)`, id)
+}
diff --git a/cli/beacon/internal/opencodesession/store.go b/cli/beacon/internal/opencodesession/store.go
index fd74efdd..b0f427c6 100644
--- a/cli/beacon/internal/opencodesession/store.go
+++ b/cli/beacon/internal/opencodesession/store.go
@@ -11,7 +11,6 @@ import (
"net/url"
"os"
"path/filepath"
- "runtime"
"sort"
"strconv"
"strings"
@@ -30,12 +29,20 @@ type Store struct {
LegacyStorageDir string
}
+// NewStore opens the OpenCode store at dataDir, which is a data directory or an opencode.db path.
+// Empty means where OpenCode itself writes: its data directory, with the database named by
+// OPENCODE_DB when that is set.
func NewStore(dataDir string) (*Store, error) {
if strings.TrimSpace(dataDir) == "" {
- dataDir = DefaultDataDir()
- }
- if strings.TrimSpace(dataDir) == "" {
- return nil, errors.New("opencode data directory is empty")
+ data := DefaultDataDir()
+ if data == "" {
+ return nil, errors.New("opencode data directory is empty")
+ }
+ return &Store{
+ DataDir: data,
+ SQLitePath: defaultDBPath(data),
+ LegacyStorageDir: filepath.Join(data, "storage"),
+ }, nil
}
dataDir = filepath.Clean(dataDir)
if strings.HasSuffix(dataDir, ".db") {
@@ -52,23 +59,32 @@ func NewStore(dataDir string) (*Store, error) {
}, nil
}
+// DefaultDataDir is OpenCode's data directory, which holds opencode.db and the legacy storage tree.
+// OpenCode uses XDG paths on every platform, Windows included. Its plugins and configuration live in
+// a separate directory (~/.config/opencode) that holds no sessions.
func DefaultDataDir() string {
+ if xdg := os.Getenv("XDG_DATA_HOME"); xdg != "" {
+ return filepath.Join(xdg, "opencode")
+ }
home, err := os.UserHomeDir()
if err != nil || strings.TrimSpace(home) == "" {
return ""
}
- switch runtime.GOOS {
- case "windows":
- if appData := strings.TrimSpace(os.Getenv("APPDATA")); appData != "" {
- return filepath.Join(appData, "opencode")
- }
- return filepath.Join(home, "AppData", "Roaming", "opencode")
- default:
- if xdg := strings.TrimSpace(os.Getenv("XDG_CONFIG_HOME")); xdg != "" {
- return filepath.Join(xdg, "opencode")
- }
- return filepath.Join(home, ".config", "opencode")
+ return filepath.Join(home, ".local", "share", "opencode")
+}
+
+// defaultDBPath resolves OPENCODE_DB as OpenCode does: an absolute path is used as given and a
+// relative one is resolved against the data directory. Unset, or the in-memory name, means
+// opencode.db.
+func defaultDBPath(data string) string {
+ name := os.Getenv("OPENCODE_DB")
+ if name == "" || name == ":memory:" {
+ return filepath.Join(data, "opencode.db")
+ }
+ if filepath.IsAbs(name) {
+ return filepath.Clean(name)
}
+ return filepath.Join(data, name)
}
func (s *Store) Exists() bool {
diff --git a/docs/changelog/cli.mdx b/docs/changelog/cli.mdx
index 6cfc99f2..464ea406 100644
--- a/docs/changelog/cli.mdx
+++ b/docs/changelog/cli.mdx
@@ -11,6 +11,8 @@ Stay up to date with the latest changes to the Agent Beacon CLI.
## October 2026
+- **OpenCode sessions found in OpenCode's data directory** · `beacon endpoint opencode status` and `sync`, `beacon handoff`, and the session backfill now read OpenCode's store from `$XDG_DATA_HOME/opencode` or `~/.local/share/opencode`, where OpenCode writes `opencode.db`, on every platform. They previously looked in `~/.config/opencode` (`%APPDATA%\opencode` on Windows), which holds only plugins and configuration, so they found no sessions unless `--data-dir` was passed. `OPENCODE_DB` is honored as OpenCode resolves it.
+
### v1.3.35 · October 10, 2026
- **Interactive update notices without startup delay** · Running bare `beacon` in
diff --git a/docs/cli/endpoint-opencode.mdx b/docs/cli/endpoint-opencode.mdx
index 6c92f82e..7d4df118 100644
--- a/docs/cli/endpoint-opencode.mdx
+++ b/docs/cli/endpoint-opencode.mdx
@@ -11,7 +11,7 @@ description: "Collect endpoint telemetry from OpenCode session records"
beacon endpoint opencode [command]
```
-Beacon reads `opencode.db` when present and falls back to OpenCode's legacy `storage/session`, `storage/message`, `storage/part`, and `storage/project` tree. This path is useful for historical sessions and sessions that ran before Beacon's OpenCode plugin was installed or loaded.
+Beacon reads `opencode.db` from OpenCode's data directory when present and falls back to OpenCode's legacy `storage/session`, `storage/message`, `storage/part`, and `storage/project` tree. The data directory is `$XDG_DATA_HOME/opencode` or `~/.local/share/opencode` on every platform, separate from the `~/.config/opencode` directory that holds plugins and configuration. When `OPENCODE_DB` is set, Beacon reads that database as OpenCode does: an absolute path as given, a relative one inside the data directory. An explicit `--data-dir` overrides discovery. This path is useful for historical sessions and sessions that ran before Beacon's OpenCode plugin was installed or loaded.
Every event is marked `harness.collection_method=poll`. Beacon sees what OpenCode already wrote, so nothing here can hold or deny a tool call. Keep the managed plugin installed when you need live approval telemetry and richer hook timing.
@@ -30,7 +30,7 @@ Both subcommands accept:
| Flag | Description |
|------|-------------|
-| `--data-dir <path>` | OpenCode data directory or `opencode.db` path. Defaults to `~/.config/opencode` |
+| `--data-dir <path>` | OpenCode data directory or `opencode.db` path. Defaults to `$XDG_DATA_HOME/opencode` or `~/.local/share/opencode`, honoring `OPENCODE_DB` |
| `--state <path>` | Collector cursor file. Defaults to `~/.beacon/endpoint/state/opencode-sessions.json`, or a file beside the system runtime log with `--system` |
| `--json` | Print the result as JSON |
| `--user` | Use per-user endpoint paths. Enabled by default |
diff --git a/docs/cli/handoff.mdx b/docs/cli/handoff.mdx
index db2705eb..6b794395 100644
--- a/docs/cli/handoff.mdx
+++ b/docs/cli/handoff.mdx
@@ -21,7 +21,7 @@ Sessions come from each runtime's own local session store, not from Beacon's run
|---------|---------------|-------------|
| Claude Code | `~/.claude/projects` | `claude` |
| Codex CLI | `$CODEX_HOME/sessions` when set; otherwise `~/.codex/sessions` | `codex` |
-| OpenCode | `~/.config/opencode` (SQLite and the legacy file store) | `opencode` |
+| OpenCode | `~/.local/share/opencode` (or `$XDG_DATA_HOME/opencode`, and `OPENCODE_DB` when set; SQLite and the legacy file store) | `opencode` |
| Cline | `~/.cline/data` | `cline` |
| Pi | `~/.pi/agent/sessions` (or under `PI_CODING_AGENT_DIR`) | `pi` |
| Prime Agent | `~/.prime/agent`, both `sessions` and `session-artifacts` (or `PRIME_AGENT_CODING_AGENT_DIR`) | `prime` |