opencode-sesh 1.1.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/AGENTS.md CHANGED
@@ -39,11 +39,10 @@ HOME=/tmp/fakehome bash install.sh # installer smoke test (never touches ~/.co
39
39
  - `bin/` — flat; no subdirs. Scripts resolve siblings from their own location.
40
40
  Never hardcode install paths. `sesh-waiting.sh` owns the needs-input query
41
41
  (shared heuristic with the TUI sidebar copy, marked `NEEDS_INPUT_SQL` in
42
- both); `sesh-prune.sh` archives stale sessions; `sesh-costs.sh` sums
42
+ both); `sesh-prune.sh` deletes stale sessions; `sesh-costs.sh` sums
43
43
  assistant-message cost per directory. Reads go through `sqlite3 -readonly`
44
- (required; `opencode db` no longer exists); the direct `UPDATE` in
45
- `sesh-prune.sh` (archive) is the only sanctioned database write — the API has
46
- no archive endpoint — and hard deletes stay on `opencode session delete`.
44
+ (required; `opencode db` no longer exists). Sesh never writes the database
45
+ directly; deletes go through `opencode session delete`.
47
46
  - `contrib/` — caller-side integrations (currently cmux). Nothing here may add
48
47
  window management to sesh itself.
49
48
  - `tui-plugins/sesh-panel/tui.tsx` — SolidJS panel, `/** @jsxImportSource @opentui/solid */`.
@@ -130,7 +129,7 @@ Resume happens in place (`exec` after `cd` to the session's cwd).
130
129
  - Destructive actions confirm. `sesh-delete.sh` prompts on a TTY and refuses
131
130
  without `--yes` when stdin is not one; the TUI arms on the first Option-D and
132
131
  commits on the second (or `y`).
133
- - The installer prefers the local opencode version for `@opencode-ai/*` and
132
+ - The installer prefers the local opencode version for `@opencode/*` and
134
133
  merges (never clobbers) the config `package.json`; opencode runs `bun install`
135
134
  at startup.
136
135
  - Commit messages follow [Conventional Commits](https://www.conventionalcommits.org/)
package/CHANGELOG.md CHANGED
@@ -5,6 +5,16 @@ All notable changes to this project are documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [2.0.0] - 2026-10-10
9
+
10
+ ### Breaking changes
11
+
12
+ - Finish V2 session compatibility and prune semantics _(breaking)_
13
+
14
+ ### Fixed
15
+
16
+ - **tui:** Align sidebar search with Sessions heading
17
+
8
18
  ## [1.1.0] - 2026-10-09
9
19
 
10
20
  ### Fixed
@@ -472,6 +482,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
472
482
  - Fixture-database regression suite, TypeScript typechecking, and ShellCheck in
473
483
  CI.
474
484
 
485
+ [2.0.0]: https://github.com/ozandogrultan/opencode-sesh/compare/v1.1.0...v2.0.0
475
486
  [1.1.0]: https://github.com/ozandogrultan/opencode-sesh/compare/v1.0.8...v1.1.0
476
487
  [1.0.8]: https://github.com/ozandogrultan/opencode-sesh/compare/v1.0.7...v1.0.8
477
488
  [1.0.7]: https://github.com/ozandogrultan/opencode-sesh/compare/v1.0.6...v1.0.7
package/README.md CHANGED
@@ -19,7 +19,7 @@ grouped by directory, full-text searchable, and previewable from anywhere.
19
19
 
20
20
  ```text
21
21
  Sessions (8) ▾ hide
22
- search…
22
+ search…
23
23
  ▾ …/Projects/portal (5)
24
24
  ├○ Retry the OAuth token refr… 2h
25
25
  ├○ Login rate limiting notes 3d
@@ -137,16 +137,19 @@ Flags: `--cwd` (current directory only), `--limit N` (default: all),
137
137
  `--json` (with `--print`, emit JSON), `--query TEXT`, `--check`,
138
138
  `--needs-input` (list sessions waiting on you instead of opening the picker).
139
139
 
140
- `sesh prune [--older-than 30d] [--dry-run] [--yes] [--delete]` archives stale
141
- sessions (not updated within the threshold) so the list stays triageable.
140
+ `sesh prune [--older-than 30d] [--dry-run] [--yes]` deletes stale
141
+ sessions (not updated within the threshold) through `opencode session delete`, including their children.
142
142
  Pinned sessions, sessions waiting on you, subagent child sessions and already-archived
143
- sessions are never touched; archiving is reversible, while `--delete`
144
- hard-deletes through `opencode session delete`. Without `--dry-run`, a TTY run
143
+ sessions are never selected. Without `--dry-run`, a TTY run
145
144
  confirms first and a non-TTY run needs `--yes`.
146
145
 
147
146
  `sesh costs [--days N] [--json]` sums assistant-message cost per project, with a
148
147
  recent window beside the lifetime total, so a day's work reads as a
149
- per-project record.
148
+ per-project record. Legacy V1 history is included when available; sessions
149
+ already present in V2 are counted only once.
150
+
151
+ Session browsing uses the V2 store. Sessions remaining only in the legacy V1
152
+ tables are not listed or resumable through sesh.
150
153
 
151
154
  When searching, title matches outrank transcript-only matches and the current
152
155
  project rises, so the list answers "where was that thing I worked on" before it
@@ -225,7 +228,7 @@ All variables are optional.
225
228
 
226
229
  | Variable | Default | Effect |
227
230
  | --- | --- | --- |
228
- | `SESH_DB` | `opencode debug paths` | opencode SQLite database |
231
+ | `SESH_DB` | `${XDG_DATA_HOME:-~/.local/share}/opencode/opencode.db`, falling back to `opencode debug paths` | opencode SQLite database |
229
232
  | `SESH_SQLITE` | `sqlite3` | query tool |
230
233
  | `SESH_JQ` / `SESH_FZF` | `PATH` | explicit executable overrides |
231
234
  | `SESH_GLOW` | first `glow` on `PATH` | optional Markdown preview renderer |
package/bin/sesh-costs.sh CHANGED
@@ -32,7 +32,7 @@ resolve_db() {
32
32
  local default="${XDG_DATA_HOME:-$HOME/.local/share}/opencode/opencode.db" from_cli=''
33
33
  if [ -f "$default" ]; then printf '%s\n' "$default"; return 0; fi
34
34
  if command -v "$OPENCODE_BIN" >/dev/null 2>&1; then
35
- from_cli=$("$OPENCODE_BIN" debug paths 2>/dev/null | awk '$1 == "db" { print $2; exit }' || true)
35
+ from_cli=$("$OPENCODE_BIN" debug paths 2>/dev/null | awk '$1 == "db" { sub(/^db[ \t]+/, ""); print; exit }' || true)
36
36
  fi
37
37
  if [ -n "$from_cli" ] && [ -f "$from_cli" ]; then printf '%s\n' "$from_cli"; return 0; fi
38
38
  return 1
@@ -51,19 +51,33 @@ DB_PATH=$(resolve_db) || { echo 'sesh: session database unavailable' >&2; exit 1
51
51
  NOW_MS=$(python3 -c 'import time; print(int(time.time() * 1000))' 2>/dev/null || echo "$(( $(date +%s) * 1000 ))")
52
52
  CUTOFF=$((NOW_MS - days * 86400000))
53
53
 
54
+ [ -n "$SQLITE_BIN" ] || { echo 'sesh: sqlite3 is required' >&2; exit 1; }
55
+ LEGACY_QUERY=''
56
+ if [ "$("$SQLITE_BIN" -readonly "$DB_PATH" "SELECT count(*) FROM sqlite_master WHERE type = 'table' AND name IN ('session', 'message');")" = 2 ]; then
57
+ LEGACY_QUERY="UNION ALL
58
+ SELECT s.directory, s.id AS session_id, m.time_created, m.data
59
+ FROM message m JOIN session s ON s.id = m.session_id
60
+ WHERE json_extract(m.data, '\$.role') = 'assistant'
61
+ AND NOT EXISTS (SELECT 1 FROM session_v2 v WHERE v.id = s.id)"
62
+ fi
63
+
54
64
  # One pass over assistant messages; the window filter is a cheap CASE so the
55
65
  # lifetime total and the window total come back together.
56
66
  ROWS_QUERY="
57
- SELECT s.directory AS directory,
67
+ WITH costs AS (
68
+ SELECT s.directory, s.id AS session_id, m.time_created, m.data
69
+ FROM session_message m JOIN session_v2 s ON s.id = m.session_id
70
+ WHERE m.type = 'assistant'
71
+ $LEGACY_QUERY
72
+ )
73
+ SELECT m.directory AS directory,
58
74
  ROUND(SUM(CASE WHEN m.time_created >= $CUTOFF THEN COALESCE(json_extract(m.data, '\$.cost'), 0) ELSE 0 END), 4) AS window_cost,
59
75
  ROUND(SUM(COALESCE(json_extract(m.data, '\$.cost'), 0)), 4) AS lifetime_cost,
60
- COUNT(DISTINCT s.id) AS sessions,
76
+ COUNT(DISTINCT m.session_id) AS sessions,
61
77
  SUM(CASE WHEN m.time_created >= $CUTOFF THEN COALESCE(json_extract(m.data, '\$.tokens.input'), 0) + COALESCE(json_extract(m.data, '\$.tokens.output'), 0) ELSE 0 END) AS window_tokens
62
- FROM session_message m
63
- JOIN session_v2 s ON s.id = m.session_id
64
- WHERE m.type = 'assistant'
65
- AND json_extract(m.data, '\$.cost') IS NOT NULL
66
- GROUP BY s.directory
78
+ FROM costs m
79
+ WHERE json_extract(m.data, '\$.cost') IS NOT NULL
80
+ GROUP BY m.directory
67
81
  ORDER BY lifetime_cost DESC;"
68
82
 
69
83
  [ -n "$SQLITE_BIN" ] || { echo 'sesh: sqlite3 is required' >&2; exit 1; }
package/bin/sesh-list.sh CHANGED
@@ -79,7 +79,7 @@ resolve_db() {
79
79
  local default="${XDG_DATA_HOME:-$HOME/.local/share}/opencode/opencode.db" from_cli=''
80
80
  if [ -f "$default" ]; then printf '%s\n' "$default"; return 0; fi
81
81
  if command -v "$OPENCODE_BIN" >/dev/null 2>&1; then
82
- from_cli=$("$OPENCODE_BIN" debug paths 2>/dev/null | awk '$1 == "db" { print $2; exit }' || true)
82
+ from_cli=$("$OPENCODE_BIN" debug paths 2>/dev/null | awk '$1 == "db" { sub(/^db[ \t]+/, ""); print; exit }' || true)
83
83
  fi
84
84
  if [ -n "$from_cli" ] && [ -f "$from_cli" ]; then printf '%s\n' "$from_cli"; return 0; fi
85
85
  return 1
@@ -279,7 +279,7 @@ refresh() {
279
279
  db_json "SELECT session_id, COUNT(*) AS n, MAX(time_updated) AS m FROM session_message GROUP BY session_id;" > "$parts" 2>/dev/null || printf '[]\n' > "$parts"
280
280
  "$JQ_BIN" -e 'type == "array"' "$parts" >/dev/null 2>&1 || printf '[]\n' > "$parts"
281
281
 
282
- # Same bulk cache validation as the Claude list: every cache arrives as its
282
+ # Same bulk cache validation as the full list: every cache arrives as its
283
283
  # own file argument (ARG_MAX), records match on session id plus the two
284
284
  # change signals, and only vanished sessions are pruned — metadata always
285
285
  # covers the whole database, so pruning is safe under any scope or limit.
@@ -378,8 +378,8 @@ refresh() {
378
378
  | ($hist | map({key: .sessionId, value: .}) | from_entries) as $histById
379
379
  | [$meta[]
380
380
  | select((.id | type) == "string" and (.id | test("^ses_[A-Za-z0-9]+$")))
381
- # Child sessions (fork children, spawned subagents) surface only with
382
- # --archived; metadata still sees them, so cache pruning stays safe.
381
+ # Child sessions (spawned subagents) surface only with --archived;
382
+ # metadata still sees them, so cache pruning stays safe.
383
383
  | select($archived == 1 or (.archived == 0 and .parent == ""))
384
384
  | select($scope == "" or .directory == $scope)
385
385
  | . as $m | ($histById[$m.id] // {}) as $h
@@ -17,7 +17,7 @@ JQ_BIN=${SESH_JQ:-jq}
17
17
  OPENCODE_BIN=${SESH_OPENCODE:-opencode}
18
18
  DB_PATH=${SESH_DB:-"${XDG_DATA_HOME:-$HOME/.local/share}/opencode/opencode.db"}
19
19
  if [ -z "${SESH_DB:-}" ] && [ ! -f "$DB_PATH" ] && command -v "$OPENCODE_BIN" >/dev/null 2>&1; then
20
- DB_PATH=$("$OPENCODE_BIN" debug paths 2>/dev/null | awk '$1 == "db" { print $2; exit }' || true)
20
+ DB_PATH=$("$OPENCODE_BIN" debug paths 2>/dev/null | awk '$1 == "db" { sub(/^db[ \t]+/, ""); print; exit }' || true)
21
21
  fi
22
22
  [ -f "$DB_PATH" ] || { echo "(opencode database is unavailable)"; exit 0; }
23
23
 
package/bin/sesh-prune.sh CHANGED
@@ -1,15 +1,9 @@
1
1
  #!/usr/bin/env bash
2
- # Archive (or, with --delete, hard-delete) stale sessions: not updated within
2
+ # Delete stale sessions: not updated within
3
3
  # the threshold, never pinned, never waiting on the user, never already
4
4
  # archived, never a child (subagent) session.
5
5
  #
6
- # Archive is the default because it is reversible; hard deletes go through
7
- # `opencode session delete` so its own safeguards apply. The archive itself
8
- # is a single atomic UPDATE through sqlite3 — the opencode server API has no
9
- # archive endpoint, so a direct write is the only route (deletes stay on the
10
- # CLI).
11
- #
12
- # Usage: sesh-prune.sh [--older-than 30d] [--dry-run] [--yes] [--delete]
6
+ # Usage: sesh-prune.sh [--older-than 30d] [--dry-run] [--yes]
13
7
  # --older-than Nd|Nh|Nw (bare N means days; default 30d)
14
8
  # --dry-run lists candidates and changes nothing
15
9
  # without --dry-run, a TTY run confirms first; a non-TTY run needs --yes
@@ -21,15 +15,13 @@ OPENCODE_BIN=${SESH_OPENCODE:-opencode}
21
15
  older_than=30d
22
16
  dry_run=0
23
17
  assume_yes=0
24
- hard_delete=0
25
18
  while [ "$#" -gt 0 ]; do
26
19
  case "$1" in
27
20
  --older-than) older_than=${2:-}; shift ;;
28
21
  --older-than=*) older_than=${1#--older-than=} ;;
29
22
  --dry-run) dry_run=1 ;;
30
23
  --yes|-y) assume_yes=1 ;;
31
- --delete) hard_delete=1 ;;
32
- --help) echo "usage: ${0##*/} [--older-than 30d] [--dry-run] [--yes] [--delete]"; exit 0 ;;
24
+ --help) echo "usage: ${0##*/} [--older-than 30d] [--dry-run] [--yes]"; exit 0 ;;
33
25
  *) echo "unknown option: $1" >&2; exit 2 ;;
34
26
  esac
35
27
  shift
@@ -56,7 +48,7 @@ resolve_db() {
56
48
  local default="${XDG_DATA_HOME:-$HOME/.local/share}/opencode/opencode.db" from_cli=''
57
49
  if [ -f "$default" ]; then printf '%s\n' "$default"; return 0; fi
58
50
  if command -v "$OPENCODE_BIN" >/dev/null 2>&1; then
59
- from_cli=$("$OPENCODE_BIN" debug paths 2>/dev/null | awk '$1 == "db" { print $2; exit }' || true)
51
+ from_cli=$("$OPENCODE_BIN" debug paths 2>/dev/null | awk '$1 == "db" { sub(/^db[ \t]+/, ""); print; exit }' || true)
60
52
  fi
61
53
  if [ -n "$from_cli" ] && [ -f "$from_cli" ]; then printf '%s\n' "$from_cli"; return 0; fi
62
54
  return 1
@@ -74,7 +66,6 @@ DB_PATH=$(resolve_db) || { echo 'sesh: session database unavailable' >&2; exit 1
74
66
 
75
67
  [ -n "$SQLITE_BIN" ] || { echo 'sesh: sqlite3 is required' >&2; exit 1; }
76
68
  query() { "$SQLITE_BIN" -json -readonly "$DB_PATH" "$1"; }
77
- write_db() { "$SQLITE_BIN" "$DB_PATH" "$1"; }
78
69
 
79
70
  JQ_BIN=${SESH_JQ:-$(command -v jq || true)}
80
71
  [ -n "$JQ_BIN" ] || { echo 'sesh: jq is required' >&2; exit 1; }
@@ -98,8 +89,7 @@ ELIGIBLE=$(printf '%s' "$CANDIDATES" | "$JQ_BIN" -c --slurpfile _pins <(printf '
98
89
  ]
99
90
  ')
100
91
  COUNT=$(printf '%s' "$ELIGIBLE" | "$JQ_BIN" -r 'length')
101
- VERB=Archive
102
- [ "$hard_delete" = 0 ] || VERB=Delete
92
+ VERB=Delete
103
93
 
104
94
  if [ "$COUNT" = 0 ]; then
105
95
  echo "Nothing to prune older than $older_than."
@@ -137,29 +127,17 @@ if [ "$assume_yes" = 0 ]; then
137
127
  fi
138
128
  fi
139
129
 
140
- if [ "$hard_delete" = 0 ]; then
141
- IDS=$(printf '%s' "$ELIGIBLE" | "$JQ_BIN" -r '.[].id')
142
- IN_LIST=''
143
- for id in $IDS; do
144
- [[ "$id" =~ ^ses_[A-Za-z0-9]+$ ]] || { echo "Refusing: unexpected session id shape." >&2; exit 1; }
145
- IN_LIST="$IN_LIST'$id',"
146
- done
147
- IN_LIST=${IN_LIST%,}
148
- write_db "UPDATE session_v2 SET time_archived = $NOW_MS WHERE id IN ($IN_LIST);"
149
- echo "Archived $COUNT session(s) older than $older_than."
150
- else
151
- command -v "$OPENCODE_BIN" >/dev/null 2>&1 || { echo 'sesh: opencode executable is unavailable for deletes.' >&2; exit 1; }
152
- failed=0
153
- done_count=0
154
- for id in $(printf '%s' "$ELIGIBLE" | "$JQ_BIN" -r '.[].id'); do
155
- [[ "$id" =~ ^ses_[A-Za-z0-9]+$ ]] || { echo "Refusing: unexpected session id shape." >&2; exit 1; }
156
- if "$OPENCODE_BIN" session delete "$id" >/dev/null 2>&1; then
157
- done_count=$((done_count + 1))
158
- else
159
- echo "Could not delete $id." >&2
160
- failed=$((failed + 1))
161
- fi
162
- done
163
- echo "Deleted $done_count session(s) older than $older_than."
164
- [ "$failed" = 0 ] || exit 1
165
- fi
130
+ command -v "$OPENCODE_BIN" >/dev/null 2>&1 || { echo 'sesh: opencode executable is unavailable for deletes.' >&2; exit 1; }
131
+ failed=0
132
+ done_count=0
133
+ for id in $(printf '%s' "$ELIGIBLE" | "$JQ_BIN" -r '.[].id'); do
134
+ [[ "$id" =~ ^ses_[A-Za-z0-9]+$ ]] || { echo "Refusing: unexpected session id shape." >&2; exit 1; }
135
+ if "$OPENCODE_BIN" session delete "$id" >/dev/null 2>&1; then
136
+ done_count=$((done_count + 1))
137
+ else
138
+ echo "Could not delete $id." >&2
139
+ failed=$((failed + 1))
140
+ fi
141
+ done
142
+ echo "Deleted $done_count session(s) older than $older_than."
143
+ [ "$failed" = 0 ] || exit 1
@@ -5,12 +5,16 @@
5
5
  # owned by a running TUI server, whose in-memory permission/question state
6
6
  # is invisible cross-project.
7
7
  #
8
- # A session waits when it is not archived, is not a fork child, and either
8
+ # A session waits when it is not archived, is not a child (subagent) session,
9
+ # and either
9
10
  # - has a `question` tool part still pending or running with no later user
10
11
  # message in the same session (awaiting an answer; a dismissed question
11
12
  # that errored is not waiting), or
12
- # - has a `tool` part still marked `running` older than STUCK_AFTER_MS
13
- # (awaiting approval, or orphaned by a dead server).
13
+ # - has a `tool` part still marked `running` older than STUCK_AFTER_MS with
14
+ # no message activity in the session or its direct children since then
15
+ # (a long-running subagent/shell tool with recent activity is not stuck;
16
+ # V2 resumes interrupted runs server-side, so `running` with no activity
17
+ # means orphaned or awaiting approval).
14
18
  #
15
19
  # Usage: sesh-waiting.sh [--json]
16
20
  # default prints a human table; --json prints [{id,reason,title,directory,updated}]
@@ -34,7 +38,7 @@ resolve_db() {
34
38
  local default="${XDG_DATA_HOME:-$HOME/.local/share}/opencode/opencode.db" from_cli=''
35
39
  if [ -f "$default" ]; then printf '%s\n' "$default"; return 0; fi
36
40
  if command -v "$OPENCODE_BIN" >/dev/null 2>&1; then
37
- from_cli=$("$OPENCODE_BIN" debug paths 2>/dev/null | awk '$1 == "db" { print $2; exit }' || true)
41
+ from_cli=$("$OPENCODE_BIN" debug paths 2>/dev/null | awk '$1 == "db" { sub(/^db[ \t]+/, ""); print; exit }' || true)
38
42
  fi
39
43
  if [ -n "$from_cli" ] && [ -f "$from_cli" ]; then printf '%s\n' "$from_cli"; return 0; fi
40
44
  return 1
@@ -83,9 +87,12 @@ WHERE COALESCE(s.time_archived, 0) = 0
83
87
  AND NOT EXISTS (
84
88
  SELECT 1 FROM session_message u
85
89
  WHERE u.session_id = live.session_id AND u.type = 'user' AND u.seq > live.seq)
86
- ) OR EXISTS (
90
+ ) OR (EXISTS (
87
91
  SELECT 1 FROM live
88
- WHERE live.session_id = s.id AND live.status = 'running' AND live.created < $STUCK_BEFORE))
92
+ WHERE live.session_id = s.id AND live.status = 'running' AND live.created < $STUCK_BEFORE)
93
+ AND NOT EXISTS (
94
+ SELECT 1 FROM session_message r JOIN session_v2 f ON f.id = r.session_id
95
+ WHERE (f.id = s.id OR f.parent_id = s.id) AND r.time_updated >= $STUCK_BEFORE)))
89
96
  ORDER BY s.time_updated DESC;
90
97
  SQL
91
98
 
package/bin/sesh.sh CHANGED
@@ -203,11 +203,15 @@ fi
203
203
  command -v "$OPENCODE_BIN" >/dev/null 2>&1 || { echo 'sesh: opencode executable unavailable' >&2; exit 1; }
204
204
  # Resume in place: this terminal becomes the session. No tabs, no panes.
205
205
  if [ "$fork" = 1 ]; then
206
- forked=$(cd "$cwd" && "$OPENCODE_BIN" api session.fork --param "sessionID=$session_id" -d '{}' 2>/dev/null | "$SESH_JQ" -r '.data.id // empty' 2>/dev/null || true)
206
+ fork_err="$state_dir/fork-err"
207
+ forked=$(cd "$cwd" && "$OPENCODE_BIN" api session.fork --param "sessionID=$session_id" -d '{}' 2>"$fork_err" | "$SESH_JQ" -r '.data.id // empty' 2>/dev/null || true)
207
208
  if [[ "$forked" =~ ^ses_[A-Za-z0-9]+$ ]]; then
208
209
  session_id=$forked
210
+ rm -f "$fork_err"
209
211
  else
210
212
  echo 'sesh: could not fork the session; returning to the picker.' >&2
213
+ [ -s "$fork_err" ] && sed 's/^/sesh: /' "$fork_err" >&2
214
+ rm -f "$fork_err"
211
215
  continue
212
216
  fi
213
217
  fi
package/install.sh CHANGED
@@ -24,7 +24,7 @@ SYNC_PANEL=0
24
24
 
25
25
  # Fallback plugin version for when the opencode CLI is unavailable; the
26
26
  # installer prefers the local opencode version so the panel matches its SDK.
27
- PLUGIN_VERSION_FALLBACK="2.0.18"
27
+ PLUGIN_VERSION_FALLBACK="2.0.25"
28
28
  # Peer line the panel was developed and typechecked against. A checkout's
29
29
  # package.json overrides these, so dependency bumps cannot drift the installed
30
30
  # config out of sync (solid-js moves with OpenTUI via @opentui/keymap's peer).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-sesh",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "description": "Browse, search, preview and resume opencode sessions from the terminal and the TUI.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -61,8 +61,8 @@
61
61
  },
62
62
  "peerDependencies": {
63
63
  "@opencode/plugin": ">=2.0.0",
64
- "@opentui/core": ">=0.5.8",
65
- "@opentui/solid": ">=0.5.8",
64
+ "@opentui/core": "^0.5.8",
65
+ "@opentui/solid": "^0.5.8",
66
66
  "solid-js": ">=1.9.0"
67
67
  },
68
68
  "devDependencies": {
@@ -1,9 +1,19 @@
1
1
  import { Plugin } from "@opencode/plugin"
2
+ import { spawnSync } from "node:child_process"
3
+ import { existsSync } from "node:fs"
2
4
  import { homedir } from "node:os"
3
5
  import { join } from "node:path"
4
6
 
5
7
  function dbPath(): string {
6
- return process.env.SESH_DB ?? join(process.env.XDG_DATA_HOME ?? join(homedir(), ".local", "share"), "opencode", "opencode.db")
8
+ if (process.env.SESH_DB) return process.env.SESH_DB
9
+ const xdgPath = join(process.env.XDG_DATA_HOME ?? join(homedir(), ".local", "share"), "opencode", "opencode.db")
10
+ if (existsSync(xdgPath)) return xdgPath
11
+ try {
12
+ const result = spawnSync(process.env.SESH_OPENCODE ?? "opencode", ["debug", "paths"], { encoding: "utf8" })
13
+ const match = result.stdout?.match(/^db\s+(.+)$/m)
14
+ if (match) return match[1].trimEnd()
15
+ } catch {}
16
+ return xdgPath
7
17
  }
8
18
 
9
19
  const input = {
@@ -7,8 +7,9 @@ type TuiPluginApi = ReturnType<typeof createApi>
7
7
  type TuiPlugin = (api: TuiPluginApi) => Promise<void>
8
8
  import { createEffect, createMemo, createRoot, createSignal, For, getOwner, on, onCleanup, onMount, runWithOwner, Show } from "solid-js"
9
9
  import { mkdir, readFile, rename, rmdir, unlink, writeFile } from "node:fs/promises"
10
+ import { existsSync } from "node:fs"
10
11
  import { dirname, join } from "node:path"
11
- import { spawn } from "node:child_process"
12
+ import { spawn, spawnSync } from "node:child_process"
12
13
  import { getTreeSitterClient, RGBA, SyntaxStyle, TextAttributes } from "@opentui/core"
13
14
  import { Portal } from "@opentui/solid"
14
15
 
@@ -502,26 +503,63 @@ type IndexProgress = { indexed: number; total: number; complete: boolean }
502
503
 
503
504
  const nextTick = () => new Promise<void>((resolve) => setTimeout(resolve, 0))
504
505
 
506
+ function resolveDbPath(): string {
507
+ if (process.env.SESH_DB) return process.env.SESH_DB
508
+ const home = process.env.HOME ?? ""
509
+ const dataHome = process.env.XDG_DATA_HOME ?? `${home}/.local/share`
510
+ const xdgPath = `${dataHome}/opencode/opencode.db`
511
+ if (existsSync(xdgPath)) return xdgPath
512
+ try {
513
+ const result = spawnSync(process.env.SESH_OPENCODE ?? "opencode", ["debug", "paths"], { encoding: "utf8" })
514
+ const match = result.stdout?.match(/^db\s+(.+)$/m)
515
+ if (match) return match[1].trimEnd()
516
+ } catch {}
517
+ return xdgPath
518
+ }
519
+
505
520
  async function openTranscriptDb(): Promise<any | undefined> {
506
521
  try {
507
- const home = process.env.HOME ?? ""
508
- const dataHome = process.env.XDG_DATA_HOME ?? `${home}/.local/share`
509
522
  // @ts-ignore - bun:sqlite ships with the Bun runtime (verified); no type package installed
510
523
  const sqlite: any = await import("bun:sqlite")
511
524
  if (typeof sqlite?.Database !== "function") return undefined
512
- return new sqlite.Database(process.env.SESH_DB ?? `${dataHome}/opencode/opencode.db`, { readonly: true })
525
+ return new sqlite.Database(resolveDbPath(), { readonly: true })
513
526
  } catch {
514
527
  return undefined
515
528
  }
516
529
  }
517
530
 
531
+ function costRowsQuery(cutoff: number, includeLegacy: boolean): string {
532
+ const legacy = includeLegacy ? `UNION ALL
533
+ SELECT s.directory, s.id AS session_id, m.time_created, m.data
534
+ FROM message m JOIN session s ON s.id = m.session_id
535
+ WHERE json_extract(m.data, '$.role') = 'assistant'
536
+ AND NOT EXISTS (SELECT 1 FROM session_v2 v WHERE v.id = s.id)` : ""
537
+ return `WITH costs AS (
538
+ SELECT s.directory, s.id AS session_id, m.time_created, m.data
539
+ FROM session_message m JOIN session_v2 s ON s.id = m.session_id
540
+ WHERE m.type = 'assistant'
541
+ ${legacy}
542
+ )
543
+ SELECT m.directory AS directory,
544
+ ROUND(SUM(CASE WHEN m.time_created >= ${cutoff} THEN COALESCE(json_extract(m.data, '$.cost'), 0) ELSE 0 END), 4) AS window_cost,
545
+ ROUND(SUM(COALESCE(json_extract(m.data, '$.cost'), 0)), 4) AS lifetime_cost,
546
+ COUNT(DISTINCT m.session_id) AS sessions
547
+ FROM costs m
548
+ WHERE json_extract(m.data, '$.cost') IS NOT NULL
549
+ GROUP BY m.directory
550
+ ORDER BY lifetime_cost DESC`
551
+ }
552
+
518
553
  // NEEDS_INPUT_SQL: sessions waiting on the user (shared heuristic — keep in
519
554
  // sync with bin/sesh-waiting.sh). A session waits when it is not archived,
520
- // is not a fork child, and either has an unanswered `question` tool part or
521
- // a `tool` part still marked running older than STUCK_AFTER_MS (awaiting
522
- // approval, or orphaned by a dead server). Server-independent on purpose:
523
- // the TUI sync layer only sees permission/question state for sessions owned
524
- // by the current server, which is invisible cross-project.
555
+ // is not a child (subagent) session, and either has an unanswered `question`
556
+ // tool part or a `tool` part still marked running older than STUCK_AFTER_MS
557
+ // with no message activity in the session or its direct children since then (a
558
+ // long-running subagent/shell tool with recent activity is not stuck; V2
559
+ // resumes interrupted runs server-side, so `running` with no activity means
560
+ // orphaned or awaiting approval). Server-independent on purpose: the TUI
561
+ // sync layer only sees permission/question state for sessions owned by the
562
+ // current server, which is invisible cross-project.
525
563
  const STUCK_AFTER_MS = 600000
526
564
  const LIVE_TOOLS_CTE = (sessionFilter: string) => `
527
565
  WITH live AS (
@@ -539,6 +577,9 @@ const QUESTION_OPEN = `live.name = 'question'
539
577
  AND NOT EXISTS (
540
578
  SELECT 1 FROM session_message u
541
579
  WHERE u.session_id = live.session_id AND u.type = 'user' AND u.seq > live.seq)`
580
+ const NO_RECENT_ACTIVITY = (sessionIdExpr: string, stuckBefore: number) => `NOT EXISTS (
581
+ SELECT 1 FROM session_message r JOIN session_v2 f ON f.id = r.session_id
582
+ WHERE (f.id = ${sessionIdExpr} OR f.parent_id = ${sessionIdExpr}) AND r.time_updated >= ${stuckBefore})`
542
583
  const NEEDS_INPUT_SQL = (stuckBefore: number) => `${LIVE_TOOLS_CTE("")}
543
584
  SELECT s.id AS id,
544
585
  CASE WHEN EXISTS (SELECT 1 FROM live WHERE live.session_id = s.id AND ${QUESTION_OPEN})
@@ -547,9 +588,10 @@ FROM session_v2 s
547
588
  WHERE COALESCE(s.time_archived, 0) = 0
548
589
  AND s.parent_id IS NULL
549
590
  AND (EXISTS (SELECT 1 FROM live WHERE live.session_id = s.id AND ${QUESTION_OPEN})
550
- OR EXISTS (
591
+ OR (EXISTS (
551
592
  SELECT 1 FROM live
552
- WHERE live.session_id = s.id AND live.status = 'running' AND live.created < ${stuckBefore}))
593
+ WHERE live.session_id = s.id AND live.status = 'running' AND live.created < ${stuckBefore})
594
+ AND ${NO_RECENT_ACTIVITY("s.id", stuckBefore)}))
553
595
  ORDER BY s.time_updated DESC`
554
596
 
555
597
  async function queryWaitingIds(db: any): Promise<Map<string, string>> {
@@ -679,7 +721,7 @@ async function buildSearchIndexRemote(
679
721
  onEach: () => void,
680
722
  ): Promise<void> {
681
723
  const home = process.env.HOME ?? ""
682
- const cacheDir = process.env.SESH_CACHE_DIR ?? `${home}/.cache/sesh`
724
+ const cacheDir = process.env.SESH_CACHE_DIR ?? `${process.env.XDG_CACHE_HOME ?? `${home}/.cache`}/sesh`
683
725
  let cursor = 0
684
726
  // Bounded concurrency: a fixed worker pool instead of one request per session.
685
727
  const worker = async () => {
@@ -1558,7 +1600,6 @@ function SidebarSessions(props: { api: TuiPluginApi } & PinProps) {
1558
1600
  <box
1559
1601
  flexDirection="row"
1560
1602
  gap={1}
1561
- paddingLeft={1}
1562
1603
  paddingRight={1}
1563
1604
  backgroundColor={searching() ? theme().backgroundElement : RGBA.fromInts(0, 0, 0, 0)}
1564
1605
  onMouseDown={(event: { stopPropagation: () => void }) => {
@@ -1576,7 +1617,7 @@ function SidebarSessions(props: { api: TuiPluginApi } & PinProps) {
1576
1617
  wrapMode="none"
1577
1618
  style={{ fg: query() ? theme().text : theme().textMuted }}
1578
1619
  >
1579
- {query() || "search…"}
1620
+ {query() || (searching() ? "" : "search…")}
1580
1621
  {searching() ? "▏" : ""}
1581
1622
  </text>
1582
1623
  <Show when={query()}>
@@ -2001,7 +2042,7 @@ function HomeSessions(props: { api: TuiPluginApi } & PinProps) {
2001
2042
  wrapMode="none"
2002
2043
  style={{ fg: query() ? theme().text : theme().textMuted }}
2003
2044
  >
2004
- {query() || "search…"}
2045
+ {query() || (searching() ? "" : "search…")}
2005
2046
  {searching() ? "▏" : ""}
2006
2047
  </text>
2007
2048
  </box>
@@ -2574,6 +2615,7 @@ const tui: TuiPlugin = async (api) => {
2574
2615
  focusedBackgroundColor={api.theme.current.backgroundPanel}
2575
2616
  focusedTextColor={api.theme.current.text}
2576
2617
  cursorColor={api.theme.current.primary}
2618
+ cursorStyle={{ style: "line", blinking: false }}
2577
2619
  onInput={(value) => {
2578
2620
  if (pendingDelete()) return
2579
2621
  setQuery(value)
@@ -2851,17 +2893,8 @@ const tui: TuiPlugin = async (api) => {
2851
2893
  else {
2852
2894
  try {
2853
2895
  const cutoff = Date.now() - 86400000
2854
- const raw = (db.query(`
2855
- SELECT s.directory AS directory,
2856
- ROUND(SUM(CASE WHEN m.time_created >= ${cutoff} THEN COALESCE(json_extract(m.data, '$.cost'), 0) ELSE 0 END), 4) AS window_cost,
2857
- ROUND(SUM(COALESCE(json_extract(m.data, '$.cost'), 0)), 4) AS lifetime_cost,
2858
- COUNT(DISTINCT s.id) AS sessions
2859
- FROM session_message m
2860
- JOIN session_v2 s ON s.id = m.session_id
2861
- WHERE m.type = 'assistant'
2862
- AND json_extract(m.data, '$.cost') IS NOT NULL
2863
- GROUP BY s.directory
2864
- ORDER BY lifetime_cost DESC`).all() ?? []) as {
2896
+ const legacyTables = db.query("SELECT count(*) AS count FROM sqlite_master WHERE type = 'table' AND name IN ('session', 'message')").get()
2897
+ const raw = (db.query(costRowsQuery(cutoff, legacyTables?.count === 2)).all() ?? []) as {
2865
2898
  directory: unknown
2866
2899
  window_cost: unknown
2867
2900
  lifetime_cost: unknown