feat(shell): PTY resize via HTTP + frontend xterm.onResize hook

The PTY was hardcoded to 80x24 at start, so full-screen programs
(htop, vim, less, tmux, top) drew themselves for 80x24 regardless
of the actual xterm window. Add a path for the frontend to push the
real cols/rows to the backend, which calls pty.Setsize on the pty
file.

Backend:
- internal/shell/manager.go: Resize(workspaceID, cols, rows) added to
  the Manager interface and implemented on LocalManager. Looks up the
  session, type-asserts sess.Stdin.(*os.File), calls pty.Setsize.
  Validates cols/rows in [1, 10000] and returns CodeBadRequest on
  bad input, CodeNotFound when no session.
- internal/service/shell_service.go: ShellService.Resize wrapper that
  checks workspace existence first.
- internal/api/shell_handler.go: resize handler, 204 on success.
- internal/api/router.go: register POST /workspaces/:id/shell/resize.
- internal/model/shell.go: ShellResizeRequest{Cols, Rows}.
- internal/shell/manager_test.go: 3 new tests (valid resize via
  pty.Getsize round-trip, invalid size, missing session).

Frontend:
- web/src/lib/api/process.ts: useShellResize mutation hook.
- web/src/components/terminal/TerminalPanel.tsx: terminal.onResize
  subscription with 100ms debounce; filters 0x0; only fires when
  workspaceId is set; cleanup clears timeout + disposes listener.

E2E: 'stty size' after POST /shell/resize {cols:200, rows:50} returns
'50 200'. 0x40 → 400, missing workspace → 404, valid → 204.

Conversation: 019f360a-5eba-7c81-94d3-e5d58ad3c026
This commit is contained in:
tao.chen
2026-07-06 14:16:09 +08:00
parent 5ed618494d
commit f5a6ff8b0d
8 changed files with 158 additions and 2 deletions
+20
View File
@@ -29,6 +29,7 @@ type Manager interface {
Subscribe(workspaceID string) (Subscription, error)
Stdin(workspaceID string) (io.WriteCloser, error)
ExitStatus(workspaceID string) (ExitInfo, error)
Resize(workspaceID string, cols, rows int) error
}
// LocalManager implements Manager using local OS processes.
@@ -203,6 +204,25 @@ func (m *LocalManager) Restart(workspaceID string, workspaceRoot string) error {
return m.Start(workspaceID, workspaceRoot)
}
// Resize resizes the PTY for the given workspace.
// Returns CodeNotFound if no session exists, or CodeBadRequest for invalid dimensions.
func (m *LocalManager) Resize(workspaceID string, cols, rows int) error {
m.mu.Lock()
sess, ok := m.sessions[workspaceID]
m.mu.Unlock()
if !ok {
return util.New(util.CodeNotFound, "no running shell for workspace")
}
if cols <= 0 || rows <= 0 || cols > 10000 || rows > 10000 {
return util.New(util.CodeBadRequest, "invalid cols/rows")
}
f, ok := sess.Stdin.(*os.File)
if !ok {
return util.New(util.CodeInternal, "shell stdin is not a pty file")
}
return pty.Setsize(f, &pty.Winsize{Cols: uint16(cols), Rows: uint16(rows), X: 0, Y: 0})
}
// Status returns the current shell status for the workspace.
func (m *LocalManager) Status(workspaceID string) Status {
m.mu.Lock()
+50
View File
@@ -9,6 +9,14 @@ import (
"time"
)
import (
"os"
"codespace/internal/util"
"github.com/creack/pty"
)
func startTestShell(t *testing.T) (*LocalManager, io.WriteCloser, Subscription) {
t.Helper()
mgr := NewManager("bash", []string{"-i"})
@@ -84,3 +92,45 @@ func TestShellPTYTermEnv(t *testing.T) {
t.Errorf("expected TERM=xterm-256color in output, got:\n%s", out)
}
}
func TestShellResizeUpdatesPTYSize(t *testing.T) {
mgr, stdin, sub := startTestShell(t)
_ = sub
f, ok := stdin.(*os.File)
if !ok {
t.Fatal("stdin is not a pty file")
}
if err := mgr.Resize("test-ws", 120, 40); err != nil {
t.Fatalf("Resize failed: %v", err)
}
rows, cols, err := pty.Getsize(f)
if err != nil {
t.Fatalf("Getsize failed: %v", err)
}
if cols != 120 || rows != 40 {
t.Errorf("expected cols=120 rows=40, got cols=%d rows=%d", cols, rows)
}
}
func TestShellResizeRejectsInvalidSize(t *testing.T) {
mgr, _, _ := startTestShell(t)
if err := mgr.Resize("test-ws", 0, 40); err == nil {
t.Fatal("expected error for invalid size")
} else if util.CodeOf(err) != util.CodeBadRequest {
t.Errorf("expected CodeBadRequest, got %v", util.CodeOf(err))
}
}
func TestShellResizeRejectsMissingSession(t *testing.T) {
mgr := NewManager("bash", []string{"-i"})
if err := mgr.Resize("missing-ws", 80, 24); err == nil {
t.Fatal("expected error for missing session")
} else if util.CodeOf(err) != util.CodeNotFound {
t.Errorf("expected CodeNotFound, got %v", util.CodeOf(err))
}
}