Files
ctask/cmd/persistent.go
T
typebasedio c204d87b47 polish(v0.5.3): v0.4 lease-prompt hint, invocation-name in user-facing commands, smoke-checklist fixes
Three independent v0.5.3 polish items surfaced during manual WSL smoke testing,
bundled into one commit since they're all string/UX touches with no behavior change.

1. v0.4 lease-prompt tightening (`internal/session/{lease,run,run_preflight}.go`,
   `cmd/entry.go`): when ctask is about to enter direct mode on a workspace that
   already has a live tmux session, the "Continue anyway?" prompt now suggests
   `ctask attach <slug>` as the reattach path. Threaded via
   `PreflightOpts.ActiveLeaseHint` / `LaunchOpts.ActiveLeaseHint`; computed in
   `cmd/entry.go::directModeTmuxHint` (best-effort: silent when no tmux on PATH
   or no session for the workspace). Closes the footgun where a user forgot to
   `export CTASK_SESSION_MODE=persistent` in a second terminal and hit the v0.4
   coexistence prompt without realizing tmux passive-reattach was the intended
   path.

2. Invocation-name in user-facing command hints (`cmd/invocation.go` new,
   `cmd/{persistent,entry,resume,doctor}.go`): the binary name printed in
   bypass / restore / "create one with" suggestions now reflects
   `filepath.Base(os.Args[0])` instead of a hard-coded "ctask". Local-build
   PowerShell users running `.\ctask.exe` see `ctask.exe new <ws> --direct`,
   matching what they need to type. Installed contexts continue to see `ctask`.
   Test seam (`invocationNameOverride`) pins the name to "ctask" in unit tests
   so substring assertions stay stable across Go test binary names. Descriptive
   prose ("ctask persistent mode requires...") and the ssh-remote hint
   (`ssh -t <host> ctask <subcmd>`) intentionally keep the literal "ctask" —
   they refer to the program identity / remote invocation, not the local
   command form. Affected tests: `cmd/{persistent,resume}_test.go` tightened to
   check the full `"<binary> <subcmd> <workspace> --direct"` form.

3. Smoke-checklist fixes (`docs/.../2026-05-08-v0.5.3-smoke-test-checklist.md`):
   six issues caught during the run -- S2 now exports CTASK_SESSION_MODE in
   both terminals (previously only WSL-A had it, which routed WSL-B's
   secondary resume through direct mode instead of passive reattach); O3/A2
   tmux-ls expectations corrected (tmux doesn't emit a literal "(detached)"
   token); P2 expected behavior rewritten (passive reattach detach returns to
   prompt immediately -- AttachExisting calls only shell.AttachSession with
   no PollSessionEnd, the owner is responsible for finalize); A1 no longer
   asks for Ctrl-C in WSL-B; A6 path now uses a glob (was hardcoded to the
   checklist's authoring date, broke on v0.5.1 local-time directory naming);
   M1 PATH-hide rewritten to `$HOME/.local/bin` only (was `:/bin` which is a
   symlink to /usr/bin on usrmerge systems, did not hide tmux) and uses
   `command -v` instead of `which` (which is itself in /usr/bin, unreachable
   under the minimal PATH); M3/M4 reordered so the no-workspace verification
   runs after PATH is restored (was silently failing under minimal PATH);
   T1 wording made "substring match" explicit; T2 wording made N/A
   unambiguous; section 10 split into 10a-10f, each a separate input, to
   work around PSReadLine multi-line paste parsing.
2026-05-14 18:22:27 -04:00

124 lines
4.7 KiB
Go

package cmd
import (
"errors"
"fmt"
"os"
"runtime"
"time"
"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
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
}