feat(v0.5.4): info Session block

Add a Session block to ctask info output, surfacing the workspace
session lease state derived from SessionStatus. Inserted between Path
and any launch-dir fields so the new content is visually distinct
from both blocks.

Format: state on the header line, then indented Mode / Owner / Attach
/ Note rows aligned at column 14. The Owner line omits the hostname
when it matches the local machine. The Attach hint surfaces only for
active+persistent sessions and uses invocationName() so the suggested
command reflects the user's actual invocation. Malformed leases
render as stale with a single-line diagnostic and no Mode/Owner/Attach
rows so we never display fields parsed from a broken file.

Exposes session.CurrentHostname() so the cmd layer has a single
source of truth for the local-vs-remote hostname check.
This commit is contained in:
2026-05-14 19:51:21 -04:00
parent 7f2c43d599
commit e0e9cd764e
3 changed files with 300 additions and 0 deletions
+60
View File
@@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"
"github.com/warrenronsiek/ctask/internal/config"
"github.com/warrenronsiek/ctask/internal/session"
)
var infoCmd = &cobra.Command{
@@ -44,6 +45,8 @@ func runInfo(cmd *cobra.Command, args []string) error {
fmt.Printf("Updated: %s\n", m.UpdatedAt.Local().Format("2006-01-02 15:04:05"))
fmt.Printf("Path: %s\n", ws.Path)
printSessionBlock(ws.Path, m.Slug)
if m.LaunchDir != "" {
// Per spec amendment: stat the expected path directly instead of
// inferring existence from ResolveLaunch's fallback behavior. info
@@ -81,3 +84,60 @@ func runInfo(cmd *cobra.Command, args []string) error {
return nil
}
// printSessionBlock renders the v0.5.4 Session block for `ctask info`.
//
// Layout (values align at column 14 across the block):
//
// Session: <state>
// Mode: <mode> (omitted when malformed)
// Owner: [host / ]pid N (Active; "Last owner:" when stale)
// Attach: <bin> attach <slug> (Active + persistent only)
// Note: <diagnostic> (stale or malformed only)
//
// The hostname is omitted from the Owner/Last-owner line when it matches
// the local machine, matching the spec's "omit when local" rule.
//
// All command-form text uses invocationName() so the hint reflects how
// the user actually invoked the binary (./ctask vs ctask.exe vs ctask).
// SessionStatus itself stays neutral and never builds a command string.
func printSessionBlock(wsPath, slug string) {
s := session.SessionStatus(wsPath)
fmt.Println()
switch s.State {
case session.SessionStateNone:
fmt.Println("Session: none")
return
case session.SessionStateStale:
// Malformed lease: SessionStatus reports state=stale, mode empty,
// diagnostic set. Render only the Note so we don't pretend to
// know the mode/owner when the file couldn't be parsed.
if s.Diagnostic != "" {
fmt.Println("Session: stale")
fmt.Printf(" Note: %s\n", s.Diagnostic)
return
}
}
fmt.Printf("Session: %s\n", s.State)
fmt.Printf(" Mode: %s\n", s.Mode)
ownerValue := fmt.Sprintf("pid %d", s.PID)
if s.Hostname != "" && s.Hostname != session.CurrentHostname() {
ownerValue = s.Hostname + " / " + ownerValue
}
if s.State == session.SessionStateActive {
fmt.Printf(" Owner: %s\n", ownerValue)
} else {
fmt.Printf(" Last owner: %s\n", ownerValue)
}
if s.State == session.SessionStateActive && s.Mode == "persistent" {
fmt.Printf(" Attach: %s attach %s\n", invocationName(), slug)
}
if s.State == session.SessionStateStale {
fmt.Println(" Note: lease expired; workspace may be available")
}
}