Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ the arrows keep working inside `claude`.
| `e` | rename the focused project or session |
| `x` | end session — close and keep the worktree, or delete both. In the session view, `x` on a running agent only stops it, and a second `x` ends the session |
| `c` | connect this session to one on another project |
| `K` / `J`, or `⇧↑` / `⇧↓` | move the selected session up / down its project's list |
| `t` | theme picker |
| `?` | help |
| `q` | quit |
Expand Down Expand Up @@ -147,11 +148,18 @@ moves the cursor, and over the pane it goes to the agent. An agent that
tracks the mouse, as cathode does with mouse capture on, gets it as a wheel
event. Any other agent gets `↑` and `↓`.

Sessions keep the order you give them. Drag a session row, in the sidebar or
the dashboard's session list, onto another row of the same project, or use the
keys above. The new order is saved as it changes. A new session still starts at
the top of its project, and a session never moves to another project, because
its worktree belongs to that project's repository.

| Command — press `ctrl+g` first | |
|---|---|
| `^g d` | dashboard |
| `^g s` | sessions |
| `^g j` / `^g k` | down / up the session list (stays attached if it is live) |
| `^g K` / `^g J` | move this session up / down its project's list |
| `^g` and a number | jump straight to that session |
| `^g n` | new session |
| `^g e` | rename this session |
Expand Down
31 changes: 27 additions & 4 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,11 @@ A click on a project or a session puts the cursor and the focus there. A second
press in a row on that same row, with no key between, opens it, as `↵` does.
Having the cursor is not enough on its own: at launch the first project already
has it, and opening on a first click would start an agent from a click meant to
select. No double-click timing is involved. Only presses act — a drag and a release are
events of their own — and a press cancels an armed `^g`. A modal takes no mouse
input at all, because it replaces the frame, and a click would act on rows the
user cannot see.
select. No double-click timing is involved. Presses act, and a press cancels an
armed `^g`. Motion and a release act only to carry a drag that a press on a
session row started — see **Sessions keep the order they are given** — so they
never count as clicks. A modal takes no mouse input at all, because it replaces
the frame, and a click would act on rows the user cannot see.

The session sidebar is built the same way (`sidebarLines`, `hitRow`) and
follows the same rules. `Model.sidebarTop` keeps it still between frames, as
Expand Down Expand Up @@ -813,6 +814,28 @@ The tests aim at the rendered frame: `cellOf` finds a label in the frame as the
terminal shows it and clicks that cell. A click placed with the hit map's own
arithmetic would test the hit map against itself.

### Sessions keep the order they are given (`store.MoveSession`, `ui/reorder.go`)

Each session holds an `Order`, lowest first. A new one takes its negated
creation time (`orderFor`), which is below the value of every session created
before it by the clock, so it lands at the top as newest-first always did. A
move only reassigns the values a project's sessions already hold, in their new
sequence, so a later session still sorts above all of them. A clock set back
breaks that: the new session then sorts below the ones it is older than by the
clock. A file written before `Order` existed is filled in from the same rule on
`Load`, which opens it in the order it was shown in. A Deck binary older than
`Order` drops the field when it saves, and the arrangement with it.

The keys and the drag both go through `moveSession`, which saves and puts both
cursors back on the sessions they were on rather than on the row index they
held: the session under an index changes when the order does. A drag starts on
a press over a session row, follows motion with the left button held, and is
saved at every step rather than at the release, because a button let go
outside the window may send no release. Any press or key ends it. Once the
session has moved, the starting press stops counting toward a double click,
because the row it was recorded against now holds another session. A drop lands
only on a session of the same project.

### Failures are reported, never absorbed

A failed worktree is never quietly downgraded to running in the project
Expand Down
57 changes: 55 additions & 2 deletions internal/store/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package store
// Reading and mutating the loaded document.

import (
"slices"
"sort"
"time"
)
Expand Down Expand Up @@ -41,6 +42,19 @@ type Session struct {
Isolated bool `json:"isolated"`
Agent string `json:"agent"`
CreatedAt time.Time `json:"created_at"`
// Order places the session in its project's list, lowest first. A new
// session takes its negated creation time (orderFor), which is below the
// value of every session created before it by the clock, so it lands first
// as newest-first always did. Moving one only permutes the values a
// project's sessions already hold, raising equal ones apart.
Order int64 `json:"order,omitempty"`
}

// orderFor is the Order a session starts with. Zero is "not set", which is
// what a state file written before Order existed holds, and Load fills it in
// from the same rule, so such a file keeps the order it was shown in.
func orderFor(created time.Time) int64 {
return -created.UnixNano()
}

// Project returns the project with the given id, or nil.
Expand Down Expand Up @@ -73,18 +87,54 @@ func (s *State) ProjectByPath(path string) *Project {
return nil
}

// SessionsFor returns a project's sessions, newest first.
// SessionsFor returns a project's sessions in their Order: as the user
// arranged them, with any session opened since at the top.
func (s *State) SessionsFor(projectID string) []Session {
var out []Session
for _, sess := range s.Sessions {
if sess.ProjectID == projectID {
out = append(out, sess)
}
}
sort.Slice(out, func(i, j int) bool { return out[i].CreatedAt.After(out[j].CreatedAt) })
sort.SliceStable(out, func(i, j int) bool { return out[i].Order < out[j].Order })
return out
}

// MoveSession puts a session at position to in its project's list, clamped
// to the list, and reports whether its position changed.
//
// The values the project's sessions hold are reassigned in their new order
// rather than recomputed, so a session opened later by the clock still takes
// a value below all of them. Equal values — a hand-edited file with no creation times — are
// raised to be distinct first, upwards only for the same reason; reassigning
// equal values would move nothing and still report a move.
func (s *State) MoveSession(id string, to int) bool {
sess := s.Session(id)
if sess == nil {
return false
}
list := s.SessionsFor(sess.ProjectID)
from := slices.IndexFunc(list, func(x Session) bool { return x.ID == id })
to = max(0, min(to, len(list)-1))
if from == to {
return false
}
orders := make([]int64, len(list))
for i := range list {
orders[i] = list[i].Order
if i > 0 && orders[i] <= orders[i-1] {
orders[i] = orders[i-1] + 1
}
}
moved := list[from]
list = slices.Delete(list, from, from+1)
list = slices.Insert(list, to, moved)
for i := range list {
s.Session(list[i].ID).Order = orders[i]
}
return true
}

// AddProject appends a project and returns it.
func (s *State) AddProject(p Project) *Project {
if p.ID == "" {
Expand All @@ -105,6 +155,9 @@ func (s *State) AddSession(sess Session) *Session {
if sess.CreatedAt.IsZero() {
sess.CreatedAt = time.Now()
}
if sess.Order == 0 {
sess.Order = orderFor(sess.CreatedAt)
}
s.Sessions = append(s.Sessions, sess)
return &s.Sessions[len(s.Sessions)-1]
}
Expand Down
5 changes: 5 additions & 0 deletions internal/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ func Load() (*State, error) {
// Sessions recorded before Agent existed name no program. Filling them in
// here, once, is what lets every reader treat the field as always set
// instead of guessing at the point of use.
//
// Order is filled in the same way, for the same reason.
for i := range s.Sessions {
if s.Sessions[i].Agent == "" {
s.Sessions[i].Agent = DefaultAgent
}
if s.Sessions[i].Order == 0 {
s.Sessions[i].Order = orderFor(s.Sessions[i].CreatedAt)
}
}
return &s, nil
}
Expand Down
107 changes: 107 additions & 0 deletions internal/store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -104,3 +105,109 @@ func TestSessionsForIsNewestFirst(t *testing.T) {
t.Errorf("first session = %q, want newer", got[0].Name)
}
}

// names lists a project's sessions by name, in the order SessionsFor gives.
func names(s *State, projectID string) string {
var out []string
for _, sess := range s.SessionsFor(projectID) {
out = append(out, sess.Name)
}
return strings.Join(out, ",")
}

// TestMoveSessionReorders covers a move in each direction, one that changes
// nothing, and one past the end, which clamps rather than refusing.
func TestMoveSessionReorders(t *testing.T) {
s := &State{}
p := s.AddProject(Project{Name: "demo", Path: "/demo"})
now := time.Now()
for i, name := range []string{"a", "b", "c"} {
s.AddSession(Session{ProjectID: p.ID, Name: name, CreatedAt: now.Add(-time.Duration(i) * time.Minute)})
}
other := s.AddSession(Session{ProjectID: "other", Name: "elsewhere", CreatedAt: now})
id := func(name string) string { return s.SessionsFor(p.ID)[strings.Index("abc", name)].ID }
c, a := id("c"), id("a")

if !s.MoveSession(c, 0) || names(s, p.ID) != "c,a,b" {
t.Errorf("moving c to the top gave %s", names(s, p.ID))
}
if s.MoveSession(c, 0) {
t.Error("a move to where the session already is reported a move")
}
if !s.MoveSession(a, 99) || names(s, p.ID) != "c,b,a" {
t.Errorf("moving a past the end gave %s, want it clamped to the bottom", names(s, p.ID))
}
if got := s.Session(other.ID).Order; got != orderFor(now) {
t.Errorf("a session of another project changed order: %d", got)
}
}

// TestANewSessionLandsFirstAfterAMove keeps the newest-first rule once the
// user has arranged the list: a move only permutes the values given out.
func TestANewSessionLandsFirstAfterAMove(t *testing.T) {
s := &State{}
p := s.AddProject(Project{Name: "demo", Path: "/demo"})
old := time.Now().Add(-time.Hour)
s.AddSession(Session{ProjectID: p.ID, Name: "a", CreatedAt: old})
b := s.AddSession(Session{ProjectID: p.ID, Name: "b", CreatedAt: old.Add(-time.Minute)})
s.MoveSession(b.ID, 0)

s.AddSession(Session{ProjectID: p.ID, Name: "new"})
if got := names(s, p.ID); got != "new,b,a" {
t.Errorf("order = %s, want the new session first and the move kept", got)
}
}

// TestMoveSessionWithEqualOrders is a hand-edited file whose sessions share a
// value. Reassigning equal values would move nothing and still say it moved.
func TestMoveSessionWithEqualOrders(t *testing.T) {
s := &State{Sessions: []Session{
{ID: "1", ProjectID: "p", Name: "a", Order: 5},
{ID: "2", ProjectID: "p", Name: "b", Order: 5},
}}
if !s.MoveSession("2", 0) || names(s, "p") != "b,a" {
t.Errorf("order = %s, want b moved first", names(s, "p"))
}
}

// TestLoadKeepsTheOrderOfAnOlderFile: a file written before Order existed
// opens newest first, as it always did, and a move then survives a reload.
func TestLoadKeepsTheOrderOfAnOlderFile(t *testing.T) {
t.Setenv("XDG_STATE_HOME", t.TempDir())
dir, err := Dir()
if err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
old := `{"projects":[{"id":"p","name":"demo","path":"/demo"}],"sessions":[
{"id":"1","project_id":"p","name":"older","created_at":"2026-01-01T10:00:00Z"},
{"id":"2","project_id":"p","name":"newer","created_at":"2026-01-02T10:00:00Z"}]}`
path, err := statePath()
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(old), 0o600); err != nil {
t.Fatal(err)
}

s, err := Load()
if err != nil {
t.Fatal(err)
}
if got := names(s, "p"); got != "newer,older" {
t.Fatalf("an older file opened as %s, want newest first", got)
}
s.MoveSession("1", 0)
if err := s.Save(); err != nil {
t.Fatal(err)
}
again, err := Load()
if err != nil {
t.Fatal(err)
}
if got := names(again, "p"); got != "older,newer" {
t.Errorf("after a move and a reload the order is %s, want older,newer", got)
}
}
2 changes: 1 addition & 1 deletion internal/ui/chrome.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (m Model) commandHint() string {
return truncateStyled(line, m.width)
}
pairs := [][2]string{
{"d", "dashboard"}, {"j/k", "switch"}, {"1…", "jump"}, {"n", "new"}, {"e", "rename"},
{"d", "dashboard"}, {"j/k", "switch"}, {"K/J", "move"}, {"1…", "jump"}, {"n", "new"}, {"e", "rename"},
{"x", "stop/end"}, {"↵", "attach"}, {"esc", "detach"}, {"?", "help"},
{"q", "quit"}, {"^g", "literal"},
}
Expand Down
3 changes: 3 additions & 0 deletions internal/ui/dashmouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func (m Model) click(t target, again bool) (tea.Model, tea.Cmd) {
return m.openFromDashboard()
}
m.focus, m.listIx = colContent, t.index
if sess := m.dashboardSession(); sess != nil {
m.dragging = sess.ID
}
case hitTab:
if m.tabIx != t.index {
m.selectTab(t.index)
Expand Down
10 changes: 8 additions & 2 deletions internal/ui/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
)

func (m Model) handleKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
// A key between two clicks makes them two first clicks, not a pair.
m.lastPress = target{}
// A key between two clicks makes them two first clicks, not a pair. It
// ends a drag too: a key can open a modal, end the session being dragged or
// change screens, and the rows the drag was moving between may be gone.
m.lastPress, m.dragging = target{}, ""

// A modal owns every key while it is up.
if m.picker != nil {
Expand Down Expand Up @@ -114,6 +116,10 @@ func (m Model) command(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
m.moveRow(-1)
case "x":
return m.stopOrEnd()
case "K", "shift+up":
return m.moveSelected(-1)
case "J", "shift+down":
return m.moveSelected(1)
case "esc", " ":
m.attached = false
case "enter", "i":
Expand Down
8 changes: 8 additions & 0 deletions internal/ui/keyroutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func (m Model) dashboardKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
return m.openThemePicker()
case key.Matches(msg, m.keys.Delete):
return m.endSelectedFromDashboard()
case key.Matches(msg, m.keys.MoveUp):
return m.moveSelected(-1)
case key.Matches(msg, m.keys.MoveDown):
return m.moveSelected(1)
case key.Matches(msg, m.keys.Enter):
return m.openFromDashboard()
}
Expand All @@ -64,6 +68,10 @@ func (m Model) sessionKey(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
return m.openSessionRename()
case key.Matches(msg, m.keys.Delete):
return m.stopOrEnd()
case key.Matches(msg, m.keys.MoveUp):
return m.moveSelected(-1)
case key.Matches(msg, m.keys.MoveDown):
return m.moveSelected(1)
case key.Matches(msg, m.keys.Connect):
return m.openConnectPicker(m.currentSession())
case key.Matches(msg, m.keys.Theme):
Expand Down
Loading
Loading