From fa0dfc324e0ac5cccedcd821f71e24cdfdd07926 Mon Sep 17 00:00:00 2001 From: Steve Yegge Date: Fri, 26 Dec 2025 21:15:08 -0800 Subject: [PATCH] feat: Add crew session cycling fix and daemon exponential backoff (gt-ws8ol) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix crew next/prev: Pass session name via key binding to avoid run-shell context issue - Add TouchTownActivity() for town-level activity signaling - Implement daemon exponential backoff based on activity.json: - 0-5 min idle → 5 min heartbeat - 5-15 min idle → 10 min heartbeat - 15-45 min idle → 30 min heartbeat - 45+ min idle → 60 min heartbeat (max) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- internal/cmd/crew.go | 5 +++ internal/cmd/crew_cycle.go | 34 ++++++++++++----- internal/cmd/root.go | 4 ++ internal/daemon/daemon.go | 67 ++++++++++++++++++++++++++++++--- internal/keepalive/keepalive.go | 63 +++++++++++++++++++++++++++++++ 5 files changed, 158 insertions(+), 15 deletions(-) diff --git a/internal/cmd/crew.go b/internal/cmd/crew.go index 2b8eb2e5..e21912fb 100644 --- a/internal/cmd/crew.go +++ b/internal/cmd/crew.go @@ -273,6 +273,11 @@ func init() { crewCmd.AddCommand(crewRenameCmd) crewCmd.AddCommand(crewPristineCmd) crewCmd.AddCommand(crewRestartCmd) + + // Add --session flag to next/prev commands for tmux key binding support + // When run via run-shell, tmux session context may be wrong, so we pass it explicitly + crewNextCmd.Flags().StringVarP(&crewCycleSession, "session", "s", "", "tmux session name (for key bindings)") + crewPrevCmd.Flags().StringVarP(&crewCycleSession, "session", "s", "", "tmux session name (for key bindings)") crewCmd.AddCommand(crewNextCmd) crewCmd.AddCommand(crewPrevCmd) crewCmd.AddCommand(crewStartCmd) diff --git a/internal/cmd/crew_cycle.go b/internal/cmd/crew_cycle.go index 970e1bcb..0c4dd3a4 100644 --- a/internal/cmd/crew_cycle.go +++ b/internal/cmd/crew_cycle.go @@ -8,16 +8,30 @@ import ( "github.com/spf13/cobra" ) +// crewCycleSession is the --session flag for crew 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 crewCycleSession string + // cycleCrewSession switches to the next or previous crew session in the same rig. // direction: 1 for next, -1 for previous -func cycleCrewSession(direction int) error { - // Get current session (uses existing function from handoff.go) - 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") +// sessionOverride: if non-empty, use this instead of detecting current session +func cycleCrewSession(direction int, sessionOverride string) error { + var currentSession string + var err error + + if sessionOverride != "" { + // Use the provided session name (from tmux key binding) + currentSession = sessionOverride + } else { + // Get current session (uses existing function from handoff.go) + 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") + } } // Parse rig name from current session @@ -73,9 +87,9 @@ func cycleCrewSession(direction int) error { } func runCrewNext(cmd *cobra.Command, args []string) error { - return cycleCrewSession(1) + return cycleCrewSession(1, crewCycleSession) } func runCrewPrev(cmd *cobra.Command, args []string) error { - return cycleCrewSession(-1) + return cycleCrewSession(-1, crewCycleSession) } diff --git a/internal/cmd/root.go b/internal/cmd/root.go index 52cc630f..d1878861 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -22,6 +22,10 @@ across distributed teams of AI agents working on shared codebases.`, // Build command path: gt status, gt mail send, etc. cmdPath := buildCommandPath(cmd) keepalive.TouchWithArgs(cmdPath, args) + + // Also signal town-level activity for daemon exponential backoff + // This resets the backoff when any gt command runs + keepalive.TouchTownActivity(cmdPath) }, } diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 199709b2..df977e6b 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -11,6 +11,7 @@ import ( "syscall" "time" + "github.com/steveyegge/gastown/internal/keepalive" "github.com/steveyegge/gastown/internal/polecat" "github.com/steveyegge/gastown/internal/tmux" ) @@ -77,11 +78,13 @@ func (d *Daemon) Run() error { sigChan := make(chan os.Signal, 1) signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1) - // Heartbeat ticker - ticker := time.NewTicker(d.config.HeartbeatInterval) - defer ticker.Stop() + // Dynamic heartbeat timer with exponential backoff based on activity + // Start with base interval + nextInterval := d.config.HeartbeatInterval + timer := time.NewTimer(nextInterval) + defer timer.Stop() - d.logger.Printf("Daemon running, heartbeat every %v", d.config.HeartbeatInterval) + d.logger.Printf("Daemon running, initial heartbeat interval %v", nextInterval) // Initial heartbeat d.heartbeat(state) @@ -102,12 +105,66 @@ func (d *Daemon) Run() error { return d.shutdown(state) } - case <-ticker.C: + case <-timer.C: d.heartbeat(state) + + // Calculate next interval based on activity + nextInterval = d.calculateHeartbeatInterval() + timer.Reset(nextInterval) + d.logger.Printf("Next heartbeat in %v", nextInterval) } } } +// Backoff thresholds for exponential slowdown when idle +const ( + // Base interval when there's recent activity + baseInterval = 5 * time.Minute + + // Tier thresholds for backoff + tier1Threshold = 5 * time.Minute // 0-5 min idle → 5 min interval + tier2Threshold = 15 * time.Minute // 5-15 min idle → 10 min interval + tier3Threshold = 45 * time.Minute // 15-45 min idle → 30 min interval + // 45+ min idle → 60 min interval (max) + + // Corresponding intervals + tier1Interval = 5 * time.Minute + tier2Interval = 10 * time.Minute + tier3Interval = 30 * time.Minute + tier4Interval = 60 * time.Minute // max +) + +// calculateHeartbeatInterval determines the next heartbeat interval based on activity. +// Reads ~/gt/daemon/activity.json to determine how long since the last gt/bd command. +// Returns exponentially increasing intervals as idle time grows. +// +// | Idle Duration | Next Heartbeat | +// |---------------|----------------| +// | 0-5 min | 5 min (base) | +// | 5-15 min | 10 min | +// | 15-45 min | 30 min | +// | 45+ min | 60 min (max) | +func (d *Daemon) calculateHeartbeatInterval() time.Duration { + activity := keepalive.ReadTownActivity() + if activity == nil { + // No activity file - assume recent activity (might be first run) + return baseInterval + } + + idleDuration := activity.Age() + + switch { + case idleDuration < tier1Threshold: + return tier1Interval + case idleDuration < tier2Threshold: + return tier2Interval + case idleDuration < tier3Threshold: + return tier3Interval + default: + return tier4Interval + } +} + // heartbeat performs one heartbeat cycle. // The daemon's job is minimal: ensure Deacon is running and send heartbeats. // All health checking and decision-making belongs in the Deacon molecule. diff --git a/internal/keepalive/keepalive.go b/internal/keepalive/keepalive.go index 52962c7b..9395e85f 100644 --- a/internal/keepalive/keepalive.go +++ b/internal/keepalive/keepalive.go @@ -64,6 +64,69 @@ func TouchInWorkspace(workspaceRoot, command string) { _ = os.WriteFile(keepalivePath, data, 0644) // non-fatal: status file for debugging } +// TouchTownActivity writes a town-level activity signal to ~/gt/daemon/activity.json. +// This is used by the daemon to implement exponential backoff when the town is idle. +// Any gt command activity resets the backoff to the base heartbeat interval. +// It silently ignores errors (best-effort signaling). +func TouchTownActivity(command string) { + // Get town root from GT_TOWN_ROOT or default to ~/gt + townRoot := os.Getenv("GT_TOWN_ROOT") + if townRoot == "" { + home, err := os.UserHomeDir() + if err != nil { + return + } + townRoot = filepath.Join(home, "gt") + } + + daemonDir := filepath.Join(townRoot, "daemon") + + // Ensure daemon directory exists + if err := os.MkdirAll(daemonDir, 0755); err != nil { + return + } + + state := State{ + LastCommand: command, + Timestamp: time.Now().UTC(), + } + + data, err := json.Marshal(state) + if err != nil { + return + } + + activityPath := filepath.Join(daemonDir, "activity.json") + _ = os.WriteFile(activityPath, data, 0644) // non-fatal: activity signal for daemon +} + +// ReadTownActivity returns the current town-level activity state. +// Returns nil if the file doesn't exist or can't be read. +func ReadTownActivity() *State { + townRoot := os.Getenv("GT_TOWN_ROOT") + if townRoot == "" { + home, err := os.UserHomeDir() + if err != nil { + return nil + } + townRoot = filepath.Join(home, "gt") + } + + activityPath := filepath.Join(townRoot, "daemon", "activity.json") + + data, err := os.ReadFile(activityPath) + if err != nil { + return nil + } + + var state State + if err := json.Unmarshal(data, &state); err != nil { + return nil + } + + return &state +} + // Read returns the current keepalive state for the workspace. // Returns nil if the file doesn't exist or can't be read. func Read(workspaceRoot string) *State {