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
76 lines
2.2 KiB
Go
76 lines
2.2 KiB
Go
package service
|
|
|
|
import (
|
|
"log/slog"
|
|
|
|
"codespace/internal/process"
|
|
"codespace/internal/shell"
|
|
"codespace/internal/workspace"
|
|
)
|
|
|
|
// WorkspaceService orchestrates workspace lifecycle operations.
|
|
type WorkspaceService struct {
|
|
workspaces workspace.Manager
|
|
processes process.Manager
|
|
shells shell.Manager
|
|
logger *slog.Logger
|
|
}
|
|
|
|
// NewWorkspaceService creates a WorkspaceService.
|
|
// If lg is nil, slog.Default() is used.
|
|
func NewWorkspaceService(workspaces workspace.Manager, processes process.Manager, shells shell.Manager, lg *slog.Logger) *WorkspaceService {
|
|
if lg == nil {
|
|
lg = slog.Default()
|
|
}
|
|
return &WorkspaceService{workspaces: workspaces, processes: processes, shells: shells, logger: lg}
|
|
}
|
|
|
|
// Create creates a new workspace.
|
|
func (s *WorkspaceService) Create(id string) (*workspace.Workspace, error) {
|
|
ws, err := s.workspaces.Create(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
s.logger.Info("workspace created", "workspace_id", id)
|
|
|
|
if shellID, err := s.shells.Start(ws.ID, ws.Root); err != nil {
|
|
s.logger.Warn("failed to auto-start shell for workspace", "workspace_id", id, "error", err)
|
|
} else {
|
|
s.logger.Info("auto-started default shell", "workspace_id", id, "shell_id", shellID)
|
|
}
|
|
|
|
return ws, nil
|
|
}
|
|
|
|
// Get retrieves a workspace by id.
|
|
func (s *WorkspaceService) Get(id string) (*workspace.Workspace, error) {
|
|
return s.workspaces.Get(id)
|
|
}
|
|
|
|
// List returns all workspaces sorted by ID.
|
|
func (s *WorkspaceService) List() ([]workspace.Workspace, error) {
|
|
return s.workspaces.List()
|
|
}
|
|
|
|
// Delete stops any running process for the workspace, then removes it.
|
|
func (s *WorkspaceService) Delete(id string) error {
|
|
shells := s.shells.List(id)
|
|
for _, info := range shells {
|
|
if err := s.shells.Stop(id, info.ShellID); err != nil {
|
|
s.logger.Warn("failed to stop workspace shell before delete", "workspace_id", id, "shell_id", info.ShellID, "error", err)
|
|
}
|
|
}
|
|
|
|
status := s.processes.Status(id)
|
|
if status.Running {
|
|
if err := s.processes.Stop(id); err != nil {
|
|
s.logger.Error("failed to stop workspace process before delete", "workspace_id", id, "error", err)
|
|
}
|
|
}
|
|
if err := s.workspaces.Delete(id); err != nil {
|
|
return err
|
|
}
|
|
s.logger.Info("workspace deleted", "workspace_id", id)
|
|
return nil
|
|
}
|