6182d89135
Closes the half-feature surfaced during Phase 1 review: doctor now
shows that persistent session_mode is overridden to direct on native
Windows, but the launch-time entry paths were silently downgrading
without telling the user. This commit adds the one-line warning
specified by the user, gated to the four entry commands where the
downgrade actually has an effect.
cmd/persistent.go:
- New package-level const platformOverrideWarning carries the exact
spec-mandated text so tests don't have to retype the copy.
- New helper emitPlatformOverrideWarningIfNeeded(alwaysPersistent).
Loads the resolver, reads SessionMode(), and emits the warning to
stderr when:
1. the call site is NOT AlwaysPersistent (attach has no
direct-mode fallback and continues to refuse on native
Windows via preflightPersistentEntry), AND
2. the resolver's value resolved through PlatformOverride.
Doctor and info do not call this helper — they render source
attribution themselves and an extra stderr line from a diagnostic
command would be noise. "Once per invocation" is provided
implicitly by the call site running at most once per ctask
command; no warn-once subsystem.
cmd/entry.go:
- defaultRunWorkspaceEntry calls the helper at the top, before any
launch work. This covers all four entry paths (new / resume / last
/ open) since they all funnel through runWorkspaceEntry. attach
also routes here but sets AlwaysPersistent=true, so the helper
short-circuits.
cmd/platform_warning_test.go (5 cases):
- TestPlatformOverrideWarningEmittedOnLaunch — simulated native
Windows + config session_mode: persistent + AlwaysPersistent=false
→ warning text appears on stderr.
- TestPlatformOverrideWarningNotEmittedWhenDirect — config
session_mode: direct → no warning even on simulated Windows.
- TestPlatformOverrideWarningNotEmittedWithoutConfig — builtin
default (direct) → no warning.
- TestPlatformOverrideWarningNotEmittedOnNonWindows — persistent
config but isNativeWindows() returns false → no warning.
- TestPlatformOverrideWarningSkippedForAlwaysPersistent — attach's
AlwaysPersistent=true gate prevents emission. attach's actual
refusal contract on native Windows is already covered by
TestPreflightRefusesNativeWindows in persistent_test.go.
Validation: `go test ./... -count=1`, `go vet ./...`, `go build`,
and `just build-linux` all clean.
157 lines
6.2 KiB
Go
157 lines
6.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"runtime"
|
|
"time"
|
|
|
|
"github.com/warrenronsiek/ctask/internal/config"
|
|
"github.com/warrenronsiek/ctask/internal/session"
|
|
"github.com/warrenronsiek/ctask/internal/shell"
|
|
)
|
|
|
|
// isTTYCheck is the test seam for terminal detection. Tests override this
|
|
// package-level variable to control the TTY refusal path without depending
|
|
// on the real process's stdin/stdout state. Production callers go through
|
|
// defaultIsTTYCheck.
|
|
var isTTYCheck = defaultIsTTYCheck
|
|
|
|
// platformOverrideWarning is the exact stderr line emitted when the v0.6
|
|
// session_mode platform override (persistent → direct on native Windows)
|
|
// kicks in. Kept as a package-level string so tests can reference it
|
|
// without re-typing the user-facing copy.
|
|
const platformOverrideWarning = "[ctask] warning: persistent session mode is not supported on native Windows; using direct mode. Use WSL for persistent sessions."
|
|
|
|
// emitPlatformOverrideWarningIfNeeded prints the platform-override
|
|
// warning once per invocation when:
|
|
// 1. the workspace entry is NOT marked AlwaysPersistent (attach has no
|
|
// direct-mode fallback and refuses via preflight rather than
|
|
// downgrading), AND
|
|
// 2. the resolver's session_mode value resolved through the
|
|
// PlatformOverride source — i.e. the user asked for persistent mode
|
|
// via config or env but the host is native Windows.
|
|
//
|
|
// Doctor and info must NOT call this helper. They render source
|
|
// attribution explicitly in their own output; emitting an additional
|
|
// stderr line from a diagnostic command would be noise.
|
|
//
|
|
// "Once per invocation" is provided implicitly by the call site in
|
|
// defaultRunWorkspaceEntry, which runs at most once per ctask command.
|
|
// No warn-once subsystem is needed in v0.6.
|
|
func emitPlatformOverrideWarningIfNeeded(alwaysPersistent bool) {
|
|
if alwaysPersistent {
|
|
return
|
|
}
|
|
s := config.LoadResolver().SessionMode()
|
|
if s.Source == config.PlatformOverride && s.Value == "direct" {
|
|
fmt.Fprintln(os.Stderr, platformOverrideWarning)
|
|
}
|
|
}
|
|
|
|
func defaultIsTTYCheck() bool {
|
|
return shell.IsTTY(os.Stdin) && shell.IsTTY(os.Stdout)
|
|
}
|
|
|
|
// preflightPersistentEntry validates the host environment supports
|
|
// tmux-based persistent mode and returns the validated tmux binary path
|
|
// for callers to pass through `session.LaunchOpts.TmuxPath`.
|
|
//
|
|
// Order of checks:
|
|
//
|
|
// 1. Native Windows refusal — tmux is not supported; recommend WSL.
|
|
// 2. Nested tmux refusal — `$TMUX` is set in the parent process.
|
|
// 3. Non-TTY refusal — stdin or stdout is not a terminal.
|
|
// 4. shell.LookupTmux — handles ErrTmuxNotFound and ErrTmuxTooOld
|
|
// with platform-aware install hints.
|
|
//
|
|
// commandName ("new", "resume", "last", "open", "attach") is rendered into
|
|
// the bypass hint so each command tells the user the right form.
|
|
func preflightPersistentEntry(commandName string) (string, error) {
|
|
bin := invocationName()
|
|
bypass := " " + bin + " " + commandName + " <workspace> --direct"
|
|
|
|
if runtime.GOOS == "windows" && os.Getenv("WSL_DISTRO_NAME") == "" {
|
|
return "", fmt.Errorf(
|
|
"ctask persistent mode requires tmux, which is not supported on native Windows.\n\n"+
|
|
"Recommended:\n Run ctask from WSL and install tmux there:\n sudo apt install tmux\n\n"+
|
|
"Or bypass persistent mode:\n%s", bypass)
|
|
}
|
|
if os.Getenv("TMUX") != "" {
|
|
return "", fmt.Errorf(
|
|
"ctask persistent mode cannot attach while already inside tmux.\n\n"+
|
|
"Run ctask from outside tmux, or bypass persistent mode:\n%s", bypass)
|
|
}
|
|
if !isTTYCheck() {
|
|
return "", fmt.Errorf(
|
|
"ctask persistent mode requires an interactive terminal.\n\n"+
|
|
"Over SSH, use:\n ssh -t <host> ctask %s <workspace>\n\n"+
|
|
"Or bypass persistent mode:\n%s", commandName, bypass)
|
|
}
|
|
tmuxPath, ver, err := shell.LookupTmux()
|
|
if err != nil {
|
|
if errors.Is(err, shell.ErrTmuxNotFound) {
|
|
return "", fmt.Errorf(
|
|
"ctask is configured for persistent sessions, but tmux is not installed.\n\n"+
|
|
"Install tmux:\n"+
|
|
" Debian/Ubuntu/WSL: sudo apt install tmux\n"+
|
|
" macOS: brew install tmux\n"+
|
|
" Arch: sudo pacman -S tmux\n"+
|
|
" Fedora: sudo dnf install tmux\n\n"+
|
|
"Or bypass persistent mode for this command:\n%s\n\n"+
|
|
"To disable persistent mode:\n unset CTASK_SESSION_MODE", bypass)
|
|
}
|
|
if errors.Is(err, shell.ErrTmuxTooOld) {
|
|
raw := ver.Raw
|
|
if raw == "" {
|
|
raw = "unknown version"
|
|
}
|
|
return "", fmt.Errorf(
|
|
"ctask persistent mode requires tmux 3.0 or newer (found: %s).\n\n"+
|
|
"Update tmux:\n"+
|
|
" Debian/Ubuntu/WSL: sudo apt install tmux (Debian 10+ ships 2.8; consider backports or a newer release)\n"+
|
|
" macOS: brew upgrade tmux\n"+
|
|
" Arch: sudo pacman -Syu tmux\n"+
|
|
" Fedora: sudo dnf upgrade tmux\n\n"+
|
|
"Or bypass persistent mode for this command:\n%s", raw, bypass)
|
|
}
|
|
return "", err
|
|
}
|
|
return tmuxPath, nil
|
|
}
|
|
|
|
// confirmFreshRemoteAdoption prompts the user to confirm adoption of a
|
|
// workspace whose lease is fresh but from a different host. Refuses on
|
|
// non-TTY environments — silent overwrite of a fresh remote lease is
|
|
// never appropriate.
|
|
func confirmFreshRemoteAdoption(wsPath string) error {
|
|
l, _ := session.ReadLease(session.LeasePath(wsPath))
|
|
hostStr := "unknown"
|
|
heartbeatAgo := "unknown"
|
|
if l != nil {
|
|
hostStr = l.Hostname
|
|
heartbeatAgo = session.FormatAgo(time.Since(l.LastHeartbeatAt))
|
|
}
|
|
if !isTTYCheck() {
|
|
return fmt.Errorf(
|
|
"ctask refused to adopt persistent session: lease is fresh from another host (%s, last heartbeat %s ago); rerun in an interactive terminal to confirm",
|
|
hostStr, heartbeatAgo)
|
|
}
|
|
fmt.Fprintf(os.Stderr,
|
|
"[ctask] tmux session exists, but a fresh lease from another host is present:\n"+
|
|
"[ctask] hostname: %s\n"+
|
|
"[ctask] last heartbeat: %s ago\n"+
|
|
"[ctask]\n"+
|
|
"[ctask] This may indicate another machine is actively using this workspace\n"+
|
|
"[ctask] (e.g., shared filesystem). Adopting will overwrite the remote lease\n"+
|
|
"[ctask] and may create conflicting workspace activity.\n"+
|
|
"[ctask]\n"+
|
|
"Adopt anyway? [y/N] ",
|
|
hostStr, heartbeatAgo)
|
|
if !session.ConfirmYN(os.Stdin, os.Stderr, "", false) {
|
|
return fmt.Errorf("[ctask] adoption refused; wait for the remote session to end or unset CTASK_SESSION_MODE")
|
|
}
|
|
return nil
|
|
}
|