Skip to content
Open
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
2 changes: 1 addition & 1 deletion docs/ssh-connections.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ The local machine can be Linux, macOS or Windows. The remote host must be Linux
## Not in scope

These are refused on a remote row with a message naming the gap:
- Rename, move, deleting a group, restart and fork.
- Rename, moving or deleting a group, moving or nesting a terminal, restart and fork.
- Diff review.
- The editor.
- Copying a reply, marking idle, cancelling an after-turn end, reordering.
Expand Down
3 changes: 2 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,10 @@ On a connection's rows:
- `enter` and `A` attach over SSH in this terminal; detaching returns to the manager. A remote pane is never embedded.
- Quick prompt mode types into a remote agent's pane as you, the way it does on this machine, or starts a new agent on the CLI, model, effort and profile the bar shows when a connection or remote group is selected. `n` starts one there too; the directory and the worktree default are the other machine's unless you set them.
- `T` opens a terminal there and `g` creates a group there, under the selected one; `x` kills an agent or closes a terminal, `v` revives, `a` and `u` archive and restore. `d` deletes an agent there the way it does here, its worktree included when clean, and asks first; on a terminal it closes it, like `x`.
- `m` on an agent moves it to another group on the same host: the picker lists that host's groups and its root, never this machine's. It leaves out the archived groups the host refuses, apart from the agent's own and the groups above it, so enter leaves an archived agent where it is. Its terminals go with it.
- On the connection row itself, `r` edits it and `d` removes it. Removing asks first; its sessions keep running on that host.

Rename, move, deleting a group, restart, fork, review, the editor, copying a reply, reordering, marking idle, cancelling an after-turn end and kill all or revive all are refused on a remote row with a message naming the gap. A failed call shows the other machine's error. Nothing from a connection is written to this machine's store. See [SSH connections](ssh-connections.md) for the protocol.
Rename, moving or deleting a group, moving or nesting a terminal, restart, fork, review, the editor, copying a reply, reordering, marking idle, cancelling an after-turn end and kill all or revive all are refused on a remote row with a message naming the gap. A failed call shows the other machine's error. Nothing from a connection is written to this machine's store. See [SSH connections](ssh-connections.md) for the protocol.

## Status

Expand Down
3 changes: 3 additions & 0 deletions internal/remote/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ func TestUnknownCommandIsTooOldNotUnreadable(t *testing.T) {
if _, err := c.Delete(context.Background(), Ref{Host: "gpu", ID: "a1"}); err == nil || err.Error() != "gpu: agent-manager there is too old for SSH connections; update agent-manager on that host" {
t.Fatalf("delete on a host that predates it = %v", err)
}
if _, err := c.Move(context.Background(), Ref{Host: "gpu", ID: "a1"}, "ops"); err == nil || err.Error() != "gpu: agent-manager there is too old for SSH connections; update agent-manager on that host" {
t.Fatalf("move on a host that predates it = %v", err)
}
c = newTestClient(t, func(context.Context, []string) ([]byte, []byte, error) {
return []byte("closed terminal a1\n"), nil, nil
})
Expand Down
7 changes: 6 additions & 1 deletion internal/ui/connections/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
OpArchive
OpRestore
OpDelete
OpMove
)

// Call is the lifecycle call a rail action makes on a remote session or
Expand All @@ -42,7 +43,7 @@ type Call struct {
// status line words them.
var Refusals = map[uirail.ActionKind]string{
uirail.RenameAction: "rename",
uirail.MoveToGroup: "move",
uirail.MoveToGroup: "moving a group",
uirail.Delete: "deleting a group",
uirail.Restart: "restart",
uirail.Fork: "fork",
Expand Down Expand Up @@ -90,6 +91,10 @@ func SessionCall(row Row, kind uirail.ActionKind, hint KeyHint) Call {
return Call{Op: OpAttach, Answered: true}
case row.Terminal && (kind == uirail.Revive || kind == uirail.Archive || kind == uirail.Restore):
return Call{Refusal: "a terminal on " + row.Host + " is not archived or revived" + hint(keybind.Kill, " - %s closes it"), Answered: true}
case kind == uirail.MoveToGroup && row.Terminal:
return Call{Refusal: uirail.RemoteRefusal("moving or nesting a terminal"), Answered: true}
case kind == uirail.MoveToGroup:
return Call{Op: OpMove, Answered: true}
case (kind == uirail.Kill || kind == uirail.Delete) && row.Terminal:
return asking(OpCloseTerminal, false, hint)
case kind == uirail.Delete:
Expand Down
3 changes: 3 additions & 0 deletions internal/ui/connections/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func TestSessionCallAnswersEveryRemoteAction(t *testing.T) {
Call{Answered: true, Refusal: "terminal sh has exited on box - x closes it"}},
{"a terminal is not archived", Row{Kind: SessionRow, Host: "box", Name: "sh", Terminal: true}, uirail.Archive,
Call{Answered: true, Refusal: "a terminal on box is not archived or revived - x closes it"}},
{"move picks a group on the host", row, uirail.MoveToGroup, Call{Op: OpMove, Answered: true}},
{"a terminal is not moved or nested", Row{Kind: SessionRow, Host: "box", Name: "sh", Terminal: true}, uirail.MoveToGroup,
Call{Answered: true, Refusal: "moving or nesting a terminal isn't available on SSH connections yet"}},
{"a local action stays local", row, uirail.OpenReview, Call{}},
}
for _, tc := range cases {
Expand Down
9 changes: 9 additions & 0 deletions internal/ui/connections/link/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/YoanWai/agent-manager/internal/remote"
"github.com/YoanWai/agent-manager/internal/sessioncmd"
uiframe "github.com/YoanWai/agent-manager/internal/ui/frame"
tea "github.com/charmbracelet/bubbletea"
)

Expand All @@ -21,6 +22,7 @@ const (
Restore
CreateGroup
Delete
Move
)

type Call struct {
Expand All @@ -34,6 +36,9 @@ type Call struct {
// GroupDir is the directory a new group on the host defaults to; Name
// carries its path.
GroupDir string
// Group is the host's group a move files the session under, empty for
// its root.
Group string
// Draft and ComposerGen let a send or spawn from the quick bar, or a
// spawn from the New Session form, clear what asked for it.
Draft string
Expand Down Expand Up @@ -76,6 +81,8 @@ func Run(ctx context.Context, client *remote.Client, openers remote.Openers, cal
result.Session, err = client.Archive(ctx, call.Ref, call.Op == Restore)
case CreateGroup:
_, err = client.CreateGroup(ctx, call.Host, call.Name, call.GroupDir)
case Move:
result.Session, err = client.Move(ctx, call.Ref, call.Group)
}
return result, err
}
Expand Down Expand Up @@ -121,6 +128,8 @@ func (s *State) ApplyCall(root Root, call Call, result Result, err error) tea.Cm
case CreateGroup:
root.CloseAsker(call.ComposerGen, GroupFormAsker)
root.ReportDone("created group " + call.Name + on)
case Move:
root.ReportDone("moved " + call.Name + " to " + uiframe.DisplayGroup(call.Group) + on)
}
return s.PollHost(call.Host)
}
3 changes: 2 additions & 1 deletion internal/ui/connections/link/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ func TestRunTurnsEachCallIntoItsHostCommand(t *testing.T) {
{Revive, []string{"revive", "--json", "--", "s1"}},
{Archive, []string{"archive", "--json", "--", "s1"}},
{Restore, []string{"archive", "--restore", "--json", "--", "s1"}},
{Move, []string{"move", "--json", "--", "s1", "ops"}},
} {
fake := &ssh{}
client := listedState(t, fake).Client
call := Call{Op: tc.op, Host: "box", Ref: remote.Ref{Host: "box", ID: "s1"}, Name: "api", Text: "hello"}
call := Call{Op: tc.op, Host: "box", Ref: remote.Ref{Host: "box", ID: "s1"}, Name: "api", Text: "hello", Group: "ops"}
if _, err := Run(context.Background(), client, nil, call); err != nil {
t.Fatalf("%v: %v", tc.op, err)
}
Expand Down
55 changes: 55 additions & 0 deletions internal/ui/connections_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/YoanWai/agent-manager/internal/config"
"github.com/YoanWai/agent-manager/internal/remote"
"github.com/YoanWai/agent-manager/internal/sessioncmd"
"github.com/YoanWai/agent-manager/internal/store"
uiconnections "github.com/YoanWai/agent-manager/internal/ui/connections"
uilink "github.com/YoanWai/agent-manager/internal/ui/connections/link"
uidialogs "github.com/YoanWai/agent-manager/internal/ui/dialogs"
Expand Down Expand Up @@ -52,6 +53,9 @@ func (m *Model) connectionIntent(intent uirail.Intent) (tea.Cmd, bool) {
case uirail.Delete:
m.confirmRemoveConnection(row.Host)
return nil, true
case uirail.MoveToGroup:
m.reportErr(row.Host + " is a connection, not a group to move")
return nil, true
}
}
if intent.Kind == uirail.NewGroup {
Expand Down Expand Up @@ -87,6 +91,10 @@ func (m *Model) remoteSessionIntent(row uiconnections.Row, call uiconnections.Ca
if call.Op == uiconnections.OpAttach {
return m.attachRemote(row), true
}
if call.Op == uiconnections.OpMove {
m.openRemoteMove(row)
return nil, true
}
if call.Op == uiconnections.OpNone {
return nil, true
}
Expand Down Expand Up @@ -126,6 +134,53 @@ func (m *Model) confirmRemote(request uilink.Call, call uiconnections.Call, term
return nil
}

// openRemoteMove opens the move card on a remote agent with the groups of
// its own host, since a move never crosses machines. The host refuses an
// archived group, so the card leaves those out but the agent's own, where
// enter leaves it in place as it does here.
func (m *Model) openRemoteMove(row uiconnections.Row) {
if refusal, offline := m.ssh.Offline(row.Host); offline {
m.reportErr(refusal)
return
}
host, _ := m.ssh.Host(row.Host)
var groups []string
archived := map[string]bool{}
for _, group := range host.Groups {
groups = append(groups, group.Path)
archived[group.Path] = group.Archived
}
hidden := func(path string) bool {
return store.EffectivelyArchived(archived, path) && !uirail.InGroupSubtree(row.Group, path)
}
options, index := spawn.Options(groups, nil, hidden, row.Group)
m.gens.dialog++
m.push(uidialogs.NewMoveSurface(remoteMoveRoot{moveRoot{m}, row}, uidialogs.MoveSession(row.Ref.ID), spawn.Picker{Groups: options, GroupIndex: index}, m.prefs.glyphMode))
m.clearErr()
}

// remoteMoveRoot is the root as the move card on a remote agent reaches it:
// the pick goes to the host, and the card closes as it does.
type remoteMoveRoot struct {
moveRoot
row uiconnections.Row
}

func (r remoteMoveRoot) PlaceMove(_ uidialogs.Move, picker *spawn.Picker) tea.Cmd {
r.Pop()
group := picker.GroupPath()
current := r.row.Group
if now, ok := r.ssh.Row(uirail.Selection{Kind: uirail.SessionRow, SessionID: r.row.Ref.ID, Host: r.row.Host}); ok {
current = now.Group
}
if group == current {
return nil
}
request := remoteCall(r.row.Host, r.row.Ref, r.row.Name)
request.Op, request.Group = uilink.Move, group
return r.queueRemote(request)
}

// queueRemote puts a call on the remote lane, unless the host's last
// refresh could not reach it.
func (m *Model) queueRemote(request uilink.Call) tea.Cmd {
Expand Down
101 changes: 101 additions & 0 deletions internal/ui/connections_adapter_move_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package ui

import (
"slices"
"testing"

"github.com/YoanWai/agent-manager/internal/remote"
"github.com/YoanWai/agent-manager/internal/sessioncmd"
uilink "github.com/YoanWai/agent-manager/internal/ui/connections/link"
uidialogs "github.com/YoanWai/agent-manager/internal/ui/dialogs"
uirail "github.com/YoanWai/agent-manager/internal/ui/rail"
tea "github.com/charmbracelet/bubbletea"
)

// A move on a remote agent picks from that host's groups alone, which may
// take it, and the host files it there.
func TestRemoteMovePicksAmongTheHostsGroups(t *testing.T) {
fake := &fakeSSH{}
m := listedModel(t, fake)
m.workspace.groups = []string{"local-only"}
fake.snapshot.Groups = append(fake.snapshot.Groups, sessioncmd.Group{Path: "ops"}, sessioncmd.Group{Path: "ops/db"},
sessioncmd.Group{Path: "shelf", Archived: true}, sessioncmd.Group{Path: "shelf/inner"})
fake.snapshot.Sessions = append(fake.snapshot.Sessions,
sessioncmd.Session{ID: "s4", Name: "stored", Tool: "claude", Group: "shelf/inner", Status: "dead", Archived: true})
setHost(m, "box", uilink.Facts(remote.HostState{OK: true, Snapshot: fake.snapshot}))
picked := func() (paths []string, cursor string) {
card, ok := surfaceOf[*uidialogs.MoveSurface](m)
if !ok {
t.Fatalf("no move card, status %q", m.errBar.text)
}
for _, option := range card.Picker().Groups {
paths = append(paths, option.Path)
}
return paths, card.Picker().GroupPath()
}
press := func(msg tea.KeyMsg) {
_, cmd := m.Update(msg)
m.runRemoteCmd(t, cmd)
}

// The host refuses an archived group, so the card offers none but the
// agent's own.
m.runRailIntent(uirail.Intent{Kind: uirail.MoveToGroup, Target: boxSession("s1")})
if paths, cursor := picked(); !slices.Equal(paths, []string{"", "web", "ops", "ops/db"}) || cursor != "web" {
t.Fatalf("targets %q on %q, want the host's open groups on web", paths, cursor)
}
press(tea.KeyMsg{Type: tea.KeyEnter})
if calls := fake.taken(); moveOpen(m) || len(calls) != 0 {
t.Fatalf("enter on its own group: open %v, calls %q", moveOpen(m), calls)
}

m.runRailIntent(uirail.Intent{Kind: uirail.MoveToGroup, Target: boxSession("s1")})
press(tea.KeyMsg{Type: tea.KeyDown})
press(tea.KeyMsg{Type: tea.KeyEnter})
if calls := fake.taken(); moveOpen(m) || len(calls) == 0 || !slices.Equal(calls[0], []string{"move", "--json", "--", "s1", "ops"}) {
t.Fatalf("calls = %q, want the move to ops first", calls)
}
if m.errBar.text != "moved api to ops on box" {
t.Fatalf("status = %q", m.errBar.text)
}

// An archived agent in an archived group opens on its own group, and
// enter leaves it there, as the local card does.
fake.mu.Lock()
fake.calls = nil
fake.mu.Unlock()
m.clearErr()
m.runRailIntent(uirail.Intent{Kind: uirail.MoveToGroup, Target: uirail.Selection{Kind: uirail.SessionRow, SessionID: "s4", Group: "shelf/inner", Host: "box"}})
if paths, cursor := picked(); !slices.Equal(paths, []string{"", "web", "ops", "ops/db", "shelf", "shelf/inner"}) || cursor != "shelf/inner" {
t.Fatalf("targets %q on %q, want its own archived group kept and selected", paths, cursor)
}
press(tea.KeyMsg{Type: tea.KeyEnter})
if calls := fake.taken(); moveOpen(m) || len(calls) != 0 || m.errBar.text != "" {
t.Fatalf("enter on its own archived group: open %v, calls %q, status %q", moveOpen(m), calls, m.errBar.text)
}
}

// A pick is weighed against the group the agent is in now, not the one it
// was in when the card opened.
func TestRemoteMoveComparesWithTheGroupTheAgentIsInNow(t *testing.T) {
fake := &fakeSSH{}
m := listedModel(t, fake)
fake.snapshot.Groups = append(fake.snapshot.Groups, sessioncmd.Group{Path: "ops"})
setHost(m, "box", uilink.Facts(remote.HostState{OK: true, Snapshot: fake.snapshot}))
m.runRailIntent(uirail.Intent{Kind: uirail.MoveToGroup, Target: boxSession("s1")})

moved := fake.snapshot
moved.Sessions = slices.Clone(moved.Sessions)
for i := range moved.Sessions {
if moved.Sessions[i].ID == "s1" {
moved.Sessions[i].Group = "ops"
}
}
setHost(m, "box", uilink.Facts(remote.HostState{OK: true, Snapshot: moved}))

_, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter})
m.runRemoteCmd(t, cmd)
if calls := fake.taken(); len(calls) == 0 || !slices.Equal(calls[0], []string{"move", "--json", "--", "s1", "web"}) {
t.Fatalf("calls = %q, want the move back to web", calls)
}
}
28 changes: 20 additions & 8 deletions internal/ui/connections_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func TestRemoteActionsAlreadyInPlaceDoNothing(t *testing.T) {

func TestUnsupportedRemoteActionsAreRefusedWithoutIO(t *testing.T) {
refused := map[uirail.ActionKind]string{
uirail.RenameAction: "rename", uirail.MoveToGroup: "move",
uirail.Restart: "restart", uirail.Fork: "fork", uirail.OpenReview: "review",
uirail.RenameAction: "rename", uirail.Restart: "restart", uirail.Fork: "fork", uirail.OpenReview: "review",
uirail.OpenEditor: "the editor", uirail.CopyReply: "copy reply", uirail.MarkIdle: "mark idle",
uirail.CancelEnd: "cancel end", uirail.KillAll: "kill all",
uirail.ReviveAll: "revive all",
Expand All @@ -147,6 +146,16 @@ func TestUnsupportedRemoteActionsAreRefusedWithoutIO(t *testing.T) {
if _, cmd := m.runRailIntent(uirail.Intent{Kind: uirail.Delete, Target: boxGroup}); cmd != nil || confirmOpen(m) || m.errBar.text != "deleting a group isn't available on SSH connections yet" {
t.Fatalf("delete on a remote group: status %q", m.errBar.text)
}
for target, want := range map[uirail.Selection]string{
boxRow: "box is a connection, not a group to move",
boxGroup: "moving a group isn't available on SSH connections yet",
boxSession("t1"): "moving or nesting a terminal isn't available on SSH connections yet",
} {
m.clearErr()
if _, cmd := m.runRailIntent(uirail.Intent{Kind: uirail.MoveToGroup, Target: target}); cmd != nil || moveOpen(m) || m.errBar.text != want {
t.Fatalf("move on %+v: status %q, want %q", target, m.errBar.text, want)
}
}
if calls := fake.taken(); len(calls) != 0 {
t.Fatalf("calls = %q, want none", calls)
}
Expand All @@ -171,12 +180,15 @@ func TestOfflineHostRefusesCallsWithItsError(t *testing.T) {
state := hostState(m, "box")
state.OK, state.Failures, state.Attempted, state.Err = false, 1, time.Now(), "box: unreachable over SSH: connection refused"
setHost(m, "box", state)
_, cmd := m.runRailIntent(uirail.Intent{Kind: uirail.Kill, Target: boxSession("s1")})
if cmd != nil || !m.remoteIdle() || len(fake.taken()) != 0 {
t.Fatal("a call to an offline host went out")
}
if m.errBar.text != "box: unreachable over SSH: connection refused" {
t.Fatalf("status = %q", m.errBar.text)
for name, kind := range map[string]uirail.ActionKind{"kill": uirail.Kill, "move": uirail.MoveToGroup} {
m.clearErr()
_, cmd := m.runRailIntent(uirail.Intent{Kind: kind, Target: boxSession("s1")})
if cmd != nil || !m.remoteIdle() || len(fake.taken()) != 0 || moveOpen(m) {
t.Fatalf("%s on an offline host went out or opened a card", name)
}
if m.errBar.text != "box: unreachable over SSH: connection refused" {
t.Fatalf("%s: status = %q", name, m.errBar.text)
}
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/ui/help/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func connectionRowHelpRows(list keybind.Table, mode presentation.GlyphMode) [][2
h.action("edit its name and SSH destination", keybind.Rename)
h.action("remove it; its sessions keep running on that host", keybind.Delete)
h.action("new session or terminal on that host", keybind.NewSession, keybind.Terminal)
h.action("move one of its agents to another group there", keybind.Move)
h.fixed("", "its agents: attach, prompt, kill, revive, archive, restore, delete")
h.fixed("", "attach opens an SSH attach in this terminal")
return h.rows
Expand Down
21 changes: 21 additions & 0 deletions internal/ui/help/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,24 @@ func TestSessionKeyMapNamesTheDivider(t *testing.T) {
t.Fatal("the default resize key remains after the session binding moved")
}
}

// A connection's agents move among that host's groups, under whatever key
// move is bound to.
func TestCatalogNamesTheMoveKeyOnAConnection(t *testing.T) {
for key, keys := range map[string]keybind.Table{
"m": keybind.DefaultList(),
"M": keybind.DefaultList().With(keybind.Move, bindingOf(t, "M")),
} {
ctx := testContext(84)
ctx.ListKeys = keys
found := false
for _, section := range helpSections(ctx) {
for _, row := range section.rows {
found = found || section.title == "SSH connection under the cursor" && row == [2]string{key, "move one of its agents to another group there"}
}
}
if !found {
t.Errorf("no move row on %s for a connection", key)
}
}
}
Loading
Loading