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
39 changes: 39 additions & 0 deletions agents/agentd/cmd/agentd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1914,6 +1914,8 @@ func (a *Agent) handleMessage(msgType string, payload json.RawMessage) {
a.handleTerminalInput(payload)
case "terminal.resize":
a.handleTerminalResize(payload)
case "terminal.navigate":
a.handleTerminalNavigate(payload)
case "terminal.control":
a.handleTerminalControl(payload)
case "terminal.detach":
Expand Down Expand Up @@ -5480,6 +5482,43 @@ func (a *Agent) handleTerminalResize(payload json.RawMessage) {
}
}

func (a *Agent) handleTerminalNavigate(payload json.RawMessage) {
var req protocol.TerminalNavigatePayload
if err := json.Unmarshal(payload, &req); err != nil {
log.Printf("Failed to parse terminal.navigate: %v", err)
return
}

navigation := tmux.TerminalNavigation{Op: tmux.TerminalNavigationOp(req.Op)}
switch navigation.Op {
case tmux.NavigateSelectWindow:
if req.WindowIndex == nil {
log.Printf("Failed to parse terminal.navigate: select_window requires window_index")
return
}
navigation.WindowIndex = *req.WindowIndex
case tmux.NavigateSelectPane:
if strings.TrimSpace(req.PaneID) == "" {
log.Printf("Failed to parse terminal.navigate: select_pane requires pane_id")
return
}
navigation.PaneID = req.PaneID
case tmux.NavigateZoom:
if req.On == nil {
log.Printf("Failed to parse terminal.navigate: zoom requires on")
return
}
navigation.On = *req.On
default:
log.Printf("Failed to parse terminal.navigate: unsupported op %q", req.Op)
return
}

if err := a.terminalManager.Navigate(req.ChannelID, navigation); err != nil {
log.Printf("Failed to navigate terminal: %v", err)
}
}

Comment on lines +5485 to +5521

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win

Run terminal navigation in a goroutine to prevent stalling the WebSocket read loop.

a.terminalManager.Navigate executes synchronous tmux shell-outs (blocking I/O). Calling it directly inside handleTerminalNavigate blocks the agent's sequential readPump, which delays the processing of all other incoming protocol messages.

Please dispatch the navigation call in a goroutine (similar to handleTerminalAttach).

⚡ Proposed fix
-	if err := a.terminalManager.Navigate(req.ChannelID, navigation); err != nil {
-		log.Printf("Failed to navigate terminal: %v", err)
-	}
+	go func() {
+		if err := a.terminalManager.Navigate(req.ChannelID, navigation); err != nil {
+			log.Printf("Failed to navigate terminal: %v", err)
+		}
+	}()
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func (a *Agent) handleTerminalNavigate(payload json.RawMessage) {
var req protocol.TerminalNavigatePayload
if err := json.Unmarshal(payload, &req); err != nil {
log.Printf("Failed to parse terminal.navigate: %v", err)
return
}
navigation := tmux.TerminalNavigation{Op: tmux.TerminalNavigationOp(req.Op)}
switch navigation.Op {
case tmux.NavigateSelectWindow:
if req.WindowIndex == nil {
log.Printf("Failed to parse terminal.navigate: select_window requires window_index")
return
}
navigation.WindowIndex = *req.WindowIndex
case tmux.NavigateSelectPane:
if strings.TrimSpace(req.PaneID) == "" {
log.Printf("Failed to parse terminal.navigate: select_pane requires pane_id")
return
}
navigation.PaneID = req.PaneID
case tmux.NavigateZoom:
if req.On == nil {
log.Printf("Failed to parse terminal.navigate: zoom requires on")
return
}
navigation.On = *req.On
default:
log.Printf("Failed to parse terminal.navigate: unsupported op %q", req.Op)
return
}
if err := a.terminalManager.Navigate(req.ChannelID, navigation); err != nil {
log.Printf("Failed to navigate terminal: %v", err)
}
}
func (a *Agent) handleTerminalNavigate(payload json.RawMessage) {
var req protocol.TerminalNavigatePayload
if err := json.Unmarshal(payload, &req); err != nil {
log.Printf("Failed to parse terminal.navigate: %v", err)
return
}
navigation := tmux.TerminalNavigation{Op: tmux.TerminalNavigationOp(req.Op)}
switch navigation.Op {
case tmux.NavigateSelectWindow:
if req.WindowIndex == nil {
log.Printf("Failed to parse terminal.navigate: select_window requires window_index")
return
}
navigation.WindowIndex = *req.WindowIndex
case tmux.NavigateSelectPane:
if strings.TrimSpace(req.PaneID) == "" {
log.Printf("Failed to parse terminal.navigate: select_pane requires pane_id")
return
}
navigation.PaneID = req.PaneID
case tmux.NavigateZoom:
if req.On == nil {
log.Printf("Failed to parse terminal.navigate: zoom requires on")
return
}
navigation.On = *req.On
default:
log.Printf("Failed to parse terminal.navigate: unsupported op %q", req.Op)
return
}
go func() {
if err := a.terminalManager.Navigate(req.ChannelID, navigation); err != nil {
log.Printf("Failed to navigate terminal: %v", err)
}
}()
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@agents/agentd/cmd/agentd/main.go` around lines 5485 - 5521, Update
handleTerminalNavigate so the validated a.terminalManager.Navigate call is
dispatched in a goroutine, keeping the synchronous tmux operation out of the
WebSocket read loop. Preserve the existing channel ID, navigation payload, and
error logging within the asynchronous operation, similar to
handleTerminalAttach.

func (a *Agent) handleTerminalControl(payload json.RawMessage) {
var req protocol.TerminalChannelPayload
if err := json.Unmarshal(payload, &req); err != nil {
Expand Down
42 changes: 42 additions & 0 deletions agents/agentd/cmd/agentd/main_terminal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,48 @@ func TestTerminalAuditHandlerEmitsAdditiveAuditEvent(t *testing.T) {
}
}

func TestHandleTerminalNavigateRoutesToGroupedViewerOnPrivateSocket(t *testing.T) {
tmuxClient := newPrivateCommandTmux(t)
panes, err := tmuxClient.ListPanes()
if err != nil || len(panes) != 1 {
t.Fatalf("initial panes=%+v err=%v", panes, err)
}
if _, err := tmuxClient.NewWindow("command-test", "second", ""); err != nil {
t.Fatalf("create second window: %v", err)
}
if err := tmuxClient.SelectWindow("command-test:0"); err != nil {
t.Fatalf("restore origin window: %v", err)
}

manager := tmux.NewTerminalManager(tmuxClient, t.TempDir())
manager.SetPerViewerPTY(true)
defer manager.Close()
if _, err := manager.AttachWithOptions("navigate-channel", panes[0].PaneID, tmux.AttachOptions{SessionID: "session-1"}); err != nil {
t.Fatalf("attach viewer: %v", err)
}
agent := &Agent{terminalManager: manager}
windowIndex := 1
payload, err := json.Marshal(protocol.TerminalNavigatePayload{
ChannelID: "navigate-channel",
Op: "select_window",
WindowIndex: &windowIndex,
})
if err != nil {
t.Fatal(err)
}
agent.handleMessage(protocol.TypeTerminalNavigate, payload)

panes, err = tmuxClient.ListPanes()
if err != nil {
t.Fatal(err)
}
for _, pane := range panes {
if pane.SessionName == "command-test" && pane.WindowIndex == 0 && !pane.WindowActive {
t.Fatal("terminal.navigate changed the origin session's current window")
}
}
}

func TestTerminalAttachSupersedesStaleChannelAfterControlPlaneReconnect(t *testing.T) {
tmuxClient := newPrivateCommandTmux(t)
panes, err := tmuxClient.ListPanes()
Expand Down
1 change: 1 addition & 0 deletions agents/agentd/internal/protocol/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
TypeTerminalAttach = "terminal.attach"
TypeTerminalInput = "terminal.input"
TypeTerminalResize = "terminal.resize"
TypeTerminalNavigate = "terminal.navigate"
TypeTerminalDetach = "terminal.detach"
TypeTerminalControl = "terminal.control"
TypeTerminalOutput = "terminal.output"
Expand Down
8 changes: 8 additions & 0 deletions agents/agentd/internal/protocol/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ type TerminalResizePayload struct {
Rows int `json:"rows"`
}

type TerminalNavigatePayload struct {
ChannelID string `json:"channel_id"`
Op string `json:"op"`
WindowIndex *int `json:"window_index,omitempty"`
PaneID string `json:"pane_id,omitempty"`
On *bool `json:"on,omitempty"`
}

type TerminalChannelPayload struct {
ChannelID string `json:"channel_id"`
}
Expand Down
Loading
Loading