Add town session cycling for mayor/deacon (C-b n/p)

- Create gt town next/prev commands for cycling between town-level sessions
- Add SetTownCycleBindings() to tmux package
- Wire up bindings when starting mayor and deacon sessions

Now mayor and deacon have the same C-b n/p cycling behavior as crew workers.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-12-28 16:00:28 -08:00
parent 213b3bab20
commit faee888a9f
4 changed files with 186 additions and 0 deletions

View File

@@ -188,6 +188,9 @@ func startDeaconSession(t *tmux.Tmux) error {
theme := tmux.DeaconTheme() theme := tmux.DeaconTheme()
_ = t.ConfigureGasTownSession(DeaconSessionName, theme, "", "Deacon", "health-check") _ = t.ConfigureGasTownSession(DeaconSessionName, theme, "", "Deacon", "health-check")
// Set up C-b n/p keybindings for town session cycling (non-fatal)
_ = t.SetTownCycleBindings(DeaconSessionName)
// Launch Claude directly (no shell respawn loop) // Launch Claude directly (no shell respawn loop)
// Restarts are handled by daemon via ensureDeaconRunning on each heartbeat // Restarts are handled by daemon via ensureDeaconRunning on each heartbeat
// The startup hook handles context loading automatically // The startup hook handles context loading automatically

View File

@@ -126,6 +126,9 @@ func startMayorSession(t *tmux.Tmux) error {
theme := tmux.MayorTheme() theme := tmux.MayorTheme()
_ = t.ConfigureGasTownSession(MayorSessionName, theme, "", "Mayor", "coordinator") _ = t.ConfigureGasTownSession(MayorSessionName, theme, "", "Mayor", "coordinator")
// Set up C-b n/p keybindings for town session cycling (non-fatal)
_ = t.SetTownCycleBindings(MayorSessionName)
// Launch Claude - the startup hook handles 'gt prime' automatically // Launch Claude - the startup hook handles 'gt prime' automatically
// Use SendKeysDelayed to allow shell initialization after NewSession // Use SendKeysDelayed to allow shell initialization after NewSession
// Export GT_ROLE and BD_ACTOR in the command since tmux SetEnvironment only affects new panes // Export GT_ROLE and BD_ACTOR in the command since tmux SetEnvironment only affects new panes

163
internal/cmd/town_cycle.go Normal file
View File

@@ -0,0 +1,163 @@
package cmd
import (
"fmt"
"os/exec"
"sort"
"github.com/spf13/cobra"
)
// townCycleSession is the --session flag for town next/prev commands.
// When run via tmux key binding (run-shell), the session context may not be
// correct, so we pass the session name explicitly via #{session_name} expansion.
var townCycleSession string
// Town-level sessions that participate in cycling (mayor, deacon).
// These are the session names without the "gt-" prefix.
var townLevelSessions = []string{"gt-mayor", "gt-deacon"}
func init() {
rootCmd.AddCommand(townCmd)
townCmd.AddCommand(townNextCmd)
townCmd.AddCommand(townPrevCmd)
townNextCmd.Flags().StringVar(&townCycleSession, "session", "", "Override current session (used by tmux binding)")
townPrevCmd.Flags().StringVar(&townCycleSession, "session", "", "Override current session (used by tmux binding)")
}
var townCmd = &cobra.Command{
Use: "town",
Short: "Town-level operations",
Long: `Commands for town-level operations including session cycling.`,
}
var townNextCmd = &cobra.Command{
Use: "next",
Short: "Switch to next town session (mayor/deacon)",
Long: `Switch to the next town-level session in the cycle order.
Town sessions cycle between Mayor and Deacon.
This command is typically invoked via the C-b n keybinding when in a
town-level session (Mayor or Deacon).`,
RunE: func(cmd *cobra.Command, args []string) error {
return cycleTownSession(1, townCycleSession)
},
}
var townPrevCmd = &cobra.Command{
Use: "prev",
Short: "Switch to previous town session (mayor/deacon)",
Long: `Switch to the previous town-level session in the cycle order.
Town sessions cycle between Mayor and Deacon.
This command is typically invoked via the C-b p keybinding when in a
town-level session (Mayor or Deacon).`,
RunE: func(cmd *cobra.Command, args []string) error {
return cycleTownSession(-1, townCycleSession)
},
}
// cycleTownSession switches to the next or previous town-level session.
// direction: 1 for next, -1 for previous
// sessionOverride: if non-empty, use this instead of detecting current session
func cycleTownSession(direction int, sessionOverride string) error {
var currentSession string
var err error
if sessionOverride != "" {
currentSession = sessionOverride
} else {
currentSession, err = getCurrentTmuxSession()
if err != nil {
return fmt.Errorf("not in a tmux session: %w", err)
}
if currentSession == "" {
return fmt.Errorf("not in a tmux session")
}
}
// Check if current session is a town-level session
isTownSession := false
for _, s := range townLevelSessions {
if s == currentSession {
isTownSession = true
break
}
}
if !isTownSession {
// Not a town session - no cycling, just stay put
return nil
}
// Find running town sessions
sessions, err := findRunningTownSessions()
if err != nil {
return fmt.Errorf("listing sessions: %w", err)
}
if len(sessions) == 0 {
return fmt.Errorf("no town sessions found")
}
// Sort for consistent ordering
sort.Strings(sessions)
// Find current position
currentIdx := -1
for i, s := range sessions {
if s == currentSession {
currentIdx = i
break
}
}
if currentIdx == -1 {
// Current session not in list (shouldn't happen)
return fmt.Errorf("current session not found in town session list")
}
// Calculate target index (with wrapping)
targetIdx := (currentIdx + direction + len(sessions)) % len(sessions)
if targetIdx == currentIdx {
// Only one session, nothing to switch to
return nil
}
targetSession := sessions[targetIdx]
// Switch to target session
cmd := exec.Command("tmux", "switch-client", "-t", targetSession)
if err := cmd.Run(); err != nil {
return fmt.Errorf("switching to %s: %w", targetSession, err)
}
return nil
}
// findRunningTownSessions returns a list of currently running town-level sessions.
func findRunningTownSessions() ([]string, error) {
// Get all tmux sessions
out, err := exec.Command("tmux", "list-sessions", "-F", "#{session_name}").Output()
if err != nil {
return nil, fmt.Errorf("listing tmux sessions: %w", err)
}
var running []string
for _, line := range splitLines(string(out)) {
if line == "" {
continue
}
// Check if this is a town-level session
for _, townSession := range townLevelSessions {
if line == townSession {
running = append(running, line)
break
}
}
}
return running, nil
}

View File

@@ -674,6 +674,23 @@ func (t *Tmux) SetCrewCycleBindings(session string) error {
return nil return nil
} }
// SetTownCycleBindings sets up C-b n/p to cycle through town-level sessions.
// Town-level sessions are Mayor and Deacon - the global coordinators.
// This allows quick switching between them without using the session picker.
func (t *Tmux) SetTownCycleBindings(session string) error {
// C-b n → gt town next (switch to next town session)
if _, err := t.run("bind-key", "-T", "prefix", "n",
"run-shell", "gt town next --session '#{session_name}'"); err != nil {
return err
}
// C-b p → gt town prev (switch to previous town session)
if _, err := t.run("bind-key", "-T", "prefix", "p",
"run-shell", "gt town prev --session '#{session_name}'"); err != nil {
return err
}
return nil
}
// SetFeedBinding configures C-b a to jump to the activity feed window. // SetFeedBinding configures C-b a to jump to the activity feed window.
// This creates the feed window if it doesn't exist, or switches to it if it does. // This creates the feed window if it doesn't exist, or switches to it if it does.
// Uses `gt feed --window` which handles both creation and switching. // Uses `gt feed --window` which handles both creation and switching.