feat(shell): multi-shell per workspace + fix WS disconnect killing bash
BREAKING: every shell operation now requires a shellId. The Manager
previously keyed by workspaceID alone (one bash per workspace). It now
keys by (workspaceID, shellID) where shellID is a UUID returned by
Start.
Fixes the long-standing bug where the WS handler's closeAll called
stdin.Close() and killed bash when a WS client disconnected. The
Session owns the pty file; the WS handler no longer closes it. The
pty is only closed by Manager.Stop (explicit) or by captureOutput
when the process naturally exits (EOF).
- internal/shell/manager.go: Manager interface gains List and every
method takes shellID; storage becomes
map[workspaceID]map[shellID]*Session; Start returns (shellID, err)
via uuid.NewString; Resize/Status/ExitStatus/Subscribe/Stdin/Stop
route by shellID; new List(workspaceID) returns ShellInfo[] in
creation order.
- internal/shell/session.go: Session gains ShellID + CreatedAt; Status
type gains ShellID.
- internal/shell/manager_test.go: updated existing tests for new
signatures; added TestShellMultiInstance (two shells in one
workspace, no output cross-talk, independent stop, List behavior).
- internal/service/shell_service.go: wrappers carry shellID; new
List method.
- internal/service/workspace_service.go: auto-start captures/logs
shellID; Delete iterates and stops all workspace shells.
- internal/api/shell_handler.go: WS closeAll drops stdin.Close();
start/restart return 201 with {shellId, pid, ...}; new list handler;
stop/resize take shellId in body.
- internal/api/router.go: GET /api/workspaces/:id/shell (list).
- internal/model/shell.go: new ShellStartResponse, ShellInfo,
ShellListResponse, ShellStopRequest, ShellRestartRequest; updated
ShellStatusResponse + ShellResizeRequest to carry shellId.
- go.mod/go.sum: github.com/google/uuid.
E2E:
- workspace create -> 1 auto shell
- start 2 more -> 3 shells in list
- stop 1 -> 2 shells in list
- WS connect -> send cmd -> disconnect -> WS reconnect -> send cmd ->
response OK, no [process exited] banner
This commit is contained in:
@@ -26,86 +26,99 @@ func NewShellService(workspaces workspace.Manager, shells shell.Manager, lg *slo
|
||||
}
|
||||
|
||||
// Start starts an interactive shell for the given workspace.
|
||||
func (s *ShellService) Start(workspaceID string) error {
|
||||
func (s *ShellService) Start(workspaceID string) (string, error) {
|
||||
ws, err := s.workspaces.Get(workspaceID)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
if err := s.shells.Start(workspaceID, ws.Root); err != nil {
|
||||
shellID, err := s.shells.Start(workspaceID, ws.Root)
|
||||
if err != nil {
|
||||
s.logger.Error("shell start failed", "workspace_id", workspaceID, "error", err)
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
status := s.shells.Status(workspaceID)
|
||||
s.logger.Info("shell started", "workspace_id", workspaceID, "pid", status.PID)
|
||||
return nil
|
||||
status := s.shells.Status(workspaceID, shellID)
|
||||
s.logger.Info("shell started", "workspace_id", workspaceID, "shell_id", shellID, "pid", status.PID)
|
||||
return shellID, nil
|
||||
}
|
||||
|
||||
// Stop stops the interactive shell for the given workspace.
|
||||
func (s *ShellService) Stop(workspaceID string) error {
|
||||
if err := s.shells.Stop(workspaceID); err != nil {
|
||||
func (s *ShellService) Stop(workspaceID string, shellID string) error {
|
||||
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.shells.Stop(workspaceID, shellID); err != nil {
|
||||
s.logger.Error("shell stop failed", "workspace_id", workspaceID, "error", err)
|
||||
return err
|
||||
}
|
||||
s.logger.Info("shell stopped", "workspace_id", workspaceID)
|
||||
s.logger.Info("shell stopped", "workspace_id", workspaceID, "shell_id", shellID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Restart restarts the interactive shell for the given workspace.
|
||||
func (s *ShellService) Restart(workspaceID string) error {
|
||||
func (s *ShellService) Restart(workspaceID string, shellID string) (string, error) {
|
||||
ws, err := s.workspaces.Get(workspaceID)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
if err := s.shells.Restart(workspaceID, ws.Root); err != nil {
|
||||
newShellID, err := s.shells.Restart(workspaceID, shellID, ws.Root)
|
||||
if err != nil {
|
||||
s.logger.Error("shell restart failed", "workspace_id", workspaceID, "error", err)
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
status := s.shells.Status(workspaceID)
|
||||
s.logger.Info("shell restarted", "workspace_id", workspaceID, "pid", status.PID)
|
||||
return nil
|
||||
status := s.shells.Status(workspaceID, newShellID)
|
||||
s.logger.Info("shell restarted", "workspace_id", workspaceID, "old_shell_id", shellID, "new_shell_id", newShellID, "pid", status.PID)
|
||||
return newShellID, nil
|
||||
}
|
||||
|
||||
// Resize resizes the PTY for the given workspace.
|
||||
func (s *ShellService) Resize(workspaceID string, cols, rows int) error {
|
||||
func (s *ShellService) Resize(workspaceID string, shellID string, cols, rows int) error {
|
||||
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.shells.Resize(workspaceID, cols, rows); err != nil {
|
||||
if err := s.shells.Resize(workspaceID, shellID, cols, rows); err != nil {
|
||||
s.logger.Error("shell resize failed", "workspace_id", workspaceID, "error", err)
|
||||
return err
|
||||
}
|
||||
s.logger.Info("shell resized", "workspace_id", workspaceID, "cols", cols, "rows", rows)
|
||||
s.logger.Info("shell resized", "workspace_id", workspaceID, "shell_id", shellID, "cols", cols, "rows", rows)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Status returns the shell status for the given workspace.
|
||||
func (s *ShellService) Status(workspaceID string) (shell.Status, error) {
|
||||
func (s *ShellService) Status(workspaceID string, shellID string) (shell.Status, error) {
|
||||
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
||||
return shell.Status{}, err
|
||||
}
|
||||
return s.shells.Status(workspaceID), nil
|
||||
return s.shells.Status(workspaceID, shellID), nil
|
||||
}
|
||||
|
||||
// Subscribe subscribes to output events for the workspace shell.
|
||||
func (s *ShellService) Subscribe(workspaceID string) (shell.Subscription, error) {
|
||||
func (s *ShellService) Subscribe(workspaceID string, shellID string) (shell.Subscription, error) {
|
||||
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.shells.Subscribe(workspaceID)
|
||||
return s.shells.Subscribe(workspaceID, shellID)
|
||||
}
|
||||
|
||||
// Input returns the stdin writer for the workspace shell.
|
||||
func (s *ShellService) Input(workspaceID string) (io.WriteCloser, error) {
|
||||
func (s *ShellService) Input(workspaceID string, shellID string) (io.WriteCloser, error) {
|
||||
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.shells.Stdin(workspaceID)
|
||||
return s.shells.Stdin(workspaceID, shellID)
|
||||
}
|
||||
|
||||
// ExitStatus returns the exit status for the workspace shell.
|
||||
func (s *ShellService) ExitStatus(workspaceID string) (shell.ExitInfo, error) {
|
||||
func (s *ShellService) ExitStatus(workspaceID string, shellID string) (shell.ExitInfo, error) {
|
||||
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
||||
return shell.ExitInfo{}, err
|
||||
}
|
||||
return s.shells.ExitStatus(workspaceID)
|
||||
return s.shells.ExitStatus(workspaceID, shellID)
|
||||
}
|
||||
|
||||
// List returns all shells for the workspace.
|
||||
func (s *ShellService) List(workspaceID string) ([]shell.ShellInfo, error) {
|
||||
if _, err := s.workspaces.Get(workspaceID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.shells.List(workspaceID), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user