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
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, delete, restart and fork.
- Rename, move, deleting a group, restart and fork.
- Diff review.
- The editor.
- Copying a reply, marking idle, cancelling an after-turn end, reordering.
Expand Down
4 changes: 2 additions & 2 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,10 @@ The connection sits in the list after your own groups, in its own color with an
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.
- `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`.
- On the connection row itself, `r` edits it and `d` removes it. Removing asks first; its sessions keep running on that host.

Rename, move, delete, 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, 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.

## Status

Expand Down
4 changes: 2 additions & 2 deletions internal/remote/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ func (c *Client) Kill(ctx context.Context, ref Ref) (sessioncmd.Session, error)
return c.lifecycle(ctx, ref, []string{"kill", "--json"})
}

// Delete removes the session on its host, which refuses while its worktree
// holds unsaved work.
// Delete removes the session and the terminals under it on its host. A
// worktree with unsaved work stays there, and the notice says so.
func (c *Client) Delete(ctx context.Context, ref Ref) (sessioncmd.Deletion, error) {
var deleted sessioncmd.Deletion
if err := validRef(ref); err != nil {
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 @@ -323,6 +323,9 @@ func TestUnknownCommandIsTooOldNotUnreadable(t *testing.T) {
if _, err := c.Type(context.Background(), Ref{Host: "gpu", ID: "a1"}, "yes"); err == nil || err.Error() != "gpu: agent-manager there is too old for SSH connections; update agent-manager on that host" {
t.Fatalf("type on a host that predates it = %v", err)
}
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)
}
c = newTestClient(t, func(context.Context, []string) ([]byte, []byte, error) {
return []byte("closed terminal a1\n"), nil, nil
})
Expand Down
8 changes: 6 additions & 2 deletions internal/ui/connections/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
OpRevive
OpArchive
OpRestore
OpDelete
)

// Call is the lifecycle call a rail action makes on a remote session or
Expand All @@ -42,7 +43,7 @@ type Call struct {
var Refusals = map[uirail.ActionKind]string{
uirail.RenameAction: "rename",
uirail.MoveToGroup: "move",
uirail.Delete: "delete",
uirail.Delete: "deleting a group",
uirail.Restart: "restart",
uirail.Fork: "fork",
uirail.OpenReview: "review",
Expand All @@ -67,6 +68,7 @@ var confirm = map[Op]struct {
OpRevive: {uidialogs.ConfirmRevive, "revive", ""},
OpArchive: {uidialogs.ConfirmArchive, "archive", ""},
OpRestore: {uidialogs.ConfirmRestore, "restore", ""},
OpDelete: {uidialogs.ConfirmDelete, "delete", ""},
}

// KeyHint fills format with the list key bound to action, and is empty while
Expand All @@ -88,8 +90,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.Kill && row.Terminal:
case (kind == uirail.Kill || kind == uirail.Delete) && row.Terminal:
return asking(OpCloseTerminal, false, hint)
case kind == uirail.Delete:
return asking(OpDelete, false, hint)
case kind == uirail.Kill:
return asking(OpKill, row.Status == "dead", hint)
case kind == uirail.Revive:
Expand Down
4 changes: 4 additions & 0 deletions internal/ui/connections/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func TestSessionCallAnswersEveryRemoteAction(t *testing.T) {
Call{Op: OpKill, Dead: true, Answered: true, Action: uidialogs.ConfirmKill, Verb: "kill"}},
{"a terminal kill closes it", Row{Kind: SessionRow, Host: "box", Name: "sh", Terminal: true}, uirail.Kill,
Call{Op: OpCloseTerminal, Answered: true, Action: uidialogs.ConfirmKill, Verb: "close terminal"}},
{"delete asks without a revive", row, uirail.Delete,
Call{Op: OpDelete, Answered: true, Action: uidialogs.ConfirmDelete, Verb: "delete"}},
{"a terminal delete closes it", Row{Kind: SessionRow, Host: "box", Name: "sh", Terminal: true}, uirail.Delete,
Call{Op: OpCloseTerminal, Answered: true, Action: uidialogs.ConfirmKill, Verb: "close terminal"}},
{"revive", row, uirail.Revive,
Call{Op: OpRevive, Answered: true, Action: uidialogs.ConfirmRevive, Verb: "revive"}},
{"archive an unarchived row", row, uirail.Archive,
Expand Down
14 changes: 13 additions & 1 deletion internal/ui/connections/link/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
Archive
Restore
CreateGroup
Delete
)

type Call struct {
Expand All @@ -43,9 +44,10 @@ type Call struct {
}

// Result is what a call returned: the session or terminal it made or
// changed.
// changed, and why a deleted session's worktree stayed on the host.
type Result struct {
Session sessioncmd.Session
Notice string
}

func Run(ctx context.Context, client *remote.Client, openers remote.Openers, call Call) (Result, error) {
Expand All @@ -64,6 +66,10 @@ func Run(ctx context.Context, client *remote.Client, openers remote.Openers, cal
result.Session, err = client.Kill(ctx, call.Ref)
case CloseTerminal:
err = client.Terminals(openers).CloseForUser(ctx, call.Ref)
case Delete:
var deleted sessioncmd.Deletion
deleted, err = client.Delete(ctx, call.Ref)
result.Session, result.Notice = deleted.Session, deleted.Notice
case Revive:
result.Session, err = client.Revive(ctx, call.Ref)
case Archive, Restore:
Expand Down Expand Up @@ -100,6 +106,12 @@ func (s *State) ApplyCall(root Root, call Call, result Result, err error) tea.Cm
root.ReportDone("opened terminal " + result.Session.Name + on)
case Kill, CloseTerminal:
root.ReportDone("killed " + call.Name + on)
case Delete:
if result.Notice != "" {
root.ReportErr("deleted " + call.Name + on + "; " + result.Notice)
} else {
root.ReportDone("deleted " + call.Name + on)
}
case Revive:
root.ReportDone("revived " + call.Name + on)
case Archive:
Expand Down
13 changes: 13 additions & 0 deletions internal/ui/connections/link/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestRunTurnsEachCallIntoItsHostCommand(t *testing.T) {
}{
{Type, []string{"type", "--json", "--", "s1", "hello"}},
{Kill, []string{"kill", "--json", "--", "s1"}},
{Delete, []string{"delete", "--json", "--", "s1"}},
{Revive, []string{"revive", "--json", "--", "s1"}},
{Archive, []string{"archive", "--json", "--", "s1"}},
{Restore, []string{"archive", "--restore", "--json", "--", "s1"}},
Expand Down Expand Up @@ -86,6 +87,18 @@ func TestASpawnFromTheFormClosesItAndAQuickSpawnClearsTheBar(t *testing.T) {
}
}

// A delete whose worktree stayed on the host says so with the delete, on
// the host it happened, since the row stays until the next refresh.
func TestADeleteReportsTheHostsNotice(t *testing.T) {
state := listedState(t, &ssh{})
call := Call{Op: Delete, Host: "box", Ref: remote.Ref{Host: "box", ID: "s1"}, Name: "api"}
root := &recorder{}
state.ApplyCall(root, call, Result{Notice: "worktree kept (has work): /srv/wt/api"}, nil)
if want := []string{"deleted api on box; worktree kept (has work): /srv/wt/api"}; !equal(root.errs, want) || len(root.done) != 0 {
t.Fatalf("done %q errors %q, want errors %q", root.done, root.errs, want)
}
}

func equal(left, right []string) bool {
if len(left) != len(right) {
return false
Expand Down
11 changes: 10 additions & 1 deletion internal/ui/connections/rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type Row struct {
Status string
Terminal bool
Archived bool
// Terminals counts the host's terminals nested under an agent, which
// its delete takes with it.
Terminals int
}

// Row finds the target's row; an agent or terminal the last snapshot does
Expand All @@ -41,7 +44,13 @@ func (c *Connections) Row(target Target) (Row, bool) {
host := c.hosts[target.Host]
for _, sess := range host.Sessions {
if sess.ID == target.ID {
return row.of(sess, false), true
row = row.of(sess, false)
for _, term := range host.Terminals {
if term.ParentID == sess.ID {
row.Terminals++
}
}
return row, true
}
}
for _, term := range host.Terminals {
Expand Down
4 changes: 2 additions & 2 deletions internal/ui/connections/rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ func TestRowFindsAgentsAndTerminals(t *testing.T) {
c.Refreshed("box", c.Gen(), Host{
OK: true,
Sessions: []Session{{ID: "s1", Name: "api", Group: "web", Status: "idle"}},
Terminals: []Session{{ID: "t1", Name: "sh", Group: "web", Archived: true}},
Terminals: []Session{{ID: "t1", Name: "sh", Group: "web", Archived: true}, {ID: "t2", Name: "api-sh", Group: "web", ParentID: "s1"}, {ID: "t3", Name: "api-log", Group: "web", ParentID: "s1", Archived: true}},
})
agent, ok := c.Row(Target{Kind: SessionRow, Host: "box", ID: "s1"})
if !ok || agent.Terminal || agent.Ref != (remote.Ref{Host: "box", ID: "s1"}) || agent.Name != "api" || agent.Group != "web" {
if !ok || agent.Terminal || agent.Ref != (remote.Ref{Host: "box", ID: "s1"}) || agent.Name != "api" || agent.Group != "web" || agent.Terminals != 2 {
t.Fatalf("agent row = %+v, %v", agent, ok)
}
term, ok := c.Row(Target{Kind: SessionRow, Host: "box", ID: "t1"})
Expand Down
15 changes: 11 additions & 4 deletions internal/ui/connections_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (m *Model) remoteSessionIntent(row uiconnections.Row, call uiconnections.Ca
request := remoteCall(row.Host, row.Ref, row.Name)
request.Op = remoteOp[call.Op]
request.Dead = call.Dead
return m.confirmRemote(request, call), true
return m.confirmRemote(request, call, row.Terminals), true
}

// remoteOp is the call a remote row's lifecycle op makes.
Expand All @@ -103,17 +103,24 @@ var remoteOp = map[uiconnections.Op]uilink.Op{
uiconnections.OpRevive: uilink.Revive,
uiconnections.OpArchive: uilink.Archive,
uiconnections.OpRestore: uilink.Restore,
uiconnections.OpDelete: uilink.Delete,
}

// confirmRemote asks before a lifecycle call goes to the host; an offline
// host refuses before the question.
func (m *Model) confirmRemote(request uilink.Call, call uiconnections.Call) tea.Cmd {
// host refuses before the question. A delete there takes the terminals under
// the agent, and names them as a local delete does.
func (m *Model) confirmRemote(request uilink.Call, call uiconnections.Call, terminals int) tea.Cmd {
if refusal, offline := m.ssh.Offline(request.Host); offline {
m.reportErr(refusal)
return nil
}
subject := presentation.EscapeControlsInline(request.Name + " on " + request.Host)
label := call.Verb + " " + subject + "?" + call.Consequence
if call.Op == uiconnections.OpDelete {
label = uisession.FollowConfirmLabel(call.Verb, subject, terminals, "kills its tmux session there.", "kills their tmux sessions there.")
}
m.openConfirm(confirmTarget{
prompt: uidialogs.Confirm{Action: call.Action, Label: call.Verb + " " + presentation.EscapeControlsInline(request.Name+" on "+request.Host+"?") + call.Consequence},
prompt: uidialogs.Confirm{Action: call.Action, Label: label},
remote: &request,
})
return nil
Expand Down
35 changes: 34 additions & 1 deletion internal/ui/connections_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func TestRemoteActionsBuildTheirRemoteCall(t *testing.T) {
}{
{"kill an agent", boxSession("s1"), uirail.Kill, []string{"kill", "--json", "--", "s1"}, "killed api on box"},
{"kill a terminal closes it", boxSession("t1"), uirail.Kill, []string{"terminal", "close", "--", "t1"}, "killed sh on box"},
{"delete an agent", boxSession("s1"), uirail.Delete, []string{"delete", "--json", "--", "s1"}, "deleted api on box"},
{"delete an archived agent", boxSession("s3"), uirail.Delete, []string{"delete", "--json", "--", "s3"}, "deleted shelved on box"},
{"delete a terminal closes it", boxSession("t1"), uirail.Delete, []string{"terminal", "close", "--", "t1"}, "killed sh on box"},
{"revive", boxSession("s2"), uirail.Revive, []string{"revive", "--json", "--", "s2"}, "revived old on box"},
{"archive", boxSession("s1"), uirail.Archive, []string{"archive", "--json", "--", "s1"}, "archived api on box"},
{"restore", boxSession("s3"), uirail.Restore, []string{"archive", "--restore", "--json", "--", "s3"}, "restored shelved on box"},
Expand Down Expand Up @@ -116,7 +119,7 @@ func TestRemoteActionsAlreadyInPlaceDoNothing(t *testing.T) {

func TestUnsupportedRemoteActionsAreRefusedWithoutIO(t *testing.T) {
refused := map[uirail.ActionKind]string{
uirail.RenameAction: "rename", uirail.MoveToGroup: "move", uirail.Delete: "delete",
uirail.RenameAction: "rename", uirail.MoveToGroup: "move",
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",
Expand All @@ -140,6 +143,10 @@ func TestUnsupportedRemoteActionsAreRefusedWithoutIO(t *testing.T) {
t.Fatalf("kill on %+v: status %q", target, m.errBar.text)
}
}
m.clearErr()
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)
}
if calls := fake.taken(); len(calls) != 0 {
t.Fatalf("calls = %q, want none", calls)
}
Expand Down Expand Up @@ -346,6 +353,32 @@ func TestRemoteLifecycleAsksFirstAndNoSendsNothing(t *testing.T) {
}
}

// The host's delete takes the terminals nested under the agent, archived
// ones too, so its confirm counts them the way a local delete does.
func TestRemoteDeleteConfirmCountsTheTerminalsItTakes(t *testing.T) {
fake := &fakeSSH{}
m := listedModel(t, fake)
fake.snapshot.Terminals = append(fake.snapshot.Terminals,
sessioncmd.Terminal{ID: "t2", Name: "api-sh", Group: "web", Status: "idle", Running: true, ParentID: "s1"},
sessioncmd.Terminal{ID: "t3", Name: "api-log", Group: "web", Status: "dead", ParentID: "s1", Archived: true},
sessioncmd.Terminal{ID: "t4", Name: "shelved-sh", Group: "web", Status: "dead", ParentID: "s3", Archived: true})
setHost(m, "box", uilink.Facts(remote.HostState{OK: true, Snapshot: fake.snapshot}))
for id, want := range map[string]string{
"s1": "delete api on box and 2 terminals? kills their tmux sessions there.",
"s2": "delete old on box? kills its tmux session there.",
"s3": "delete shelved on box and 1 terminal? kills their tmux sessions there.",
} {
m.runRailIntent(uirail.Intent{Kind: uirail.Delete, Target: boxSession(id)})
if !confirmOpen(m) || pendingConfirm(m).prompt.Label != want {
t.Fatalf("delete %s: label %q, want %q", id, pendingConfirm(m).prompt.Label, want)
}
m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("n")})
}
if calls := fake.taken(); len(calls) != 0 {
t.Fatalf("calls before an answer = %q", calls)
}
}

func TestQuickBarRefusesWhenARefreshDropsItsRemoteTarget(t *testing.T) {
fake := &fakeSSH{}
m := connectedModel(t, fake)
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/help/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +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.fixed("", "its sessions: attach, quick prompt, kill, revive, archive, restore")
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
13 changes: 11 additions & 2 deletions internal/ui/rail/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,12 @@ func TestConnectionMenusOfferOnlyRemoteActions(t *testing.T) {
t.Errorf("connection menu = %v", got)
}
session := labels(Selection{Kind: SessionRow, SessionID: "far", Host: "box"})
for _, refused := range []string{"Rename", "Move to group", "Fork", "Review changes", "Open in editor", "Restart", "Delete"} {
for _, refused := range []string{"Rename", "Move to group", "Fork", "Review changes", "Open in editor", "Restart"} {
if slices.Contains(session, refused) {
t.Errorf("remote session menu offers %s: %v", refused, session)
}
}
if !slices.Contains(session, "Kill") || !slices.Contains(session, "Attach") {
if !slices.Contains(session, "Kill") || !slices.Contains(session, "Attach") || !slices.Contains(session, "Delete") {
t.Errorf("remote session menu = %v", session)
}
// The host archives and revives agents only.
Expand All @@ -248,6 +248,15 @@ func TestConnectionMenusOfferOnlyRemoteActions(t *testing.T) {
if got := labels(Selection{Kind: GroupRow}); !slices.Contains(got, "New connection") {
t.Errorf("root menu = %v, want New connection", got)
}
model.Reconcile(Snapshot{Hosts: []Host{{Name: "box", State: HostOnline, Sessions: []Session{{ID: "far", Name: "far", Tool: "claude", Status: "dead"}}}}})
if got := labels(Selection{Kind: SessionRow, SessionID: "far", Host: "box"}); !slices.Equal(got, []string{"Revive", "Archive", "Delete"}) {
t.Errorf("dead remote session menu = %v, want Revive, Archive and Delete", got)
}
model.SetArchived(true)
model.Reconcile(Snapshot{Hosts: []Host{{Name: "box", State: HostOnline, Sessions: []Session{{ID: "far", Name: "far", Tool: "claude", Status: "dead", Archived: true}}}}})
if got := labels(Selection{Kind: SessionRow, SessionID: "far", Host: "box"}); !slices.Equal(got, []string{"Restore", "Delete"}) {
t.Errorf("archived remote session menu = %v, want Restore and Delete", got)
}
}

func TestHostFoldsFollowTheirConnection(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions internal/ui/rail/legend.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ func (m Model) RemoteLegend(ctx LegendContext, row RemoteRow, collapsed bool) Le
title, kill, leads = "Shell", "close", nil
}
pairs := [][2]string{{k(keybind.Open, keybind.Attach), "attach over SSH"}, {k(keybind.Kill), kill}}
if !row.Terminal {
pairs = append(pairs, [2]string{k(keybind.Delete), "delete"})
}
if row.Dead {
pairs = append(pairs, [2]string{k(keybind.Revive), "revive"})
}
Expand Down
14 changes: 14 additions & 0 deletions internal/ui/rail/legend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,17 @@ func TestRootLegendOnlyOffersApplicableActions(t *testing.T) {
}
}
}

func TestRemoteLegendOffersDeleteOnAnAgentOnly(t *testing.T) {
model := rail.New(nil)
ctx := rail.LegendContext{ListKeys: keybind.DefaultList()}
agent := model.RemoteLegend(ctx, rail.RemoteRow{Kind: rail.SessionRow, Host: "box"}, false)
if !slices.Contains(agent.Pairs, [2]string{"d", "delete"}) {
t.Fatalf("a remote agent should offer delete, got %+v", agent.Pairs)
}
// d on a shell closes it, which x already offers.
shell := model.RemoteLegend(ctx, rail.RemoteRow{Kind: rail.SessionRow, Host: "box", Terminal: true}, false)
if slices.Contains(shell.Pairs, [2]string{"d", "delete"}) {
t.Fatalf("a remote shell should leave delete to close, got %+v", shell.Pairs)
}
}
Loading
Loading