Files
gastown/internal/session/town.go
aleiby 31bd120077 fix(startup): unify agent startup with beacon + instructions in CLI prompt (#977)
All agents now receive their startup beacon + role-specific instructions via
the CLI prompt, making sessions identifiable in /resume picker while removing
unreliable post-startup nudges.

Changes:
- Rename FormatStartupNudge → FormatStartupBeacon, StartupNudgeConfig → BeaconConfig
- Remove StartupNudge() function (no longer needed)
- Remove PropulsionNudge() and PropulsionNudgeForRole() functions
- Update deacon, witness, refinery, polecat managers to include beacon in CLI prompt
- Update boot to inline beacon (can't import session due to import cycle)
- Update daemon/lifecycle.go to include beacon via BuildCommandWithPrompt
- Update cmd/deacon.go to include beacon in startup command
- Remove redundant StartupNudge and PropulsionNudge calls from all startup paths

The beacon is now part of the CLI prompt which is queued before Claude starts,
making it more reliable than post-startup nudges which had timing issues.
SessionStart hook runs gt prime automatically, so PropulsionNudge was redundant.

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 18:08:57 -08:00

78 lines
2.3 KiB
Go

// Package session provides polecat session lifecycle management.
package session
import (
"fmt"
"time"
"github.com/steveyegge/gastown/internal/events"
"github.com/steveyegge/gastown/internal/tmux"
)
// TownSession represents a town-level tmux session.
type TownSession struct {
Name string // Display name (e.g., "Mayor")
SessionID string // Tmux session ID (e.g., "hq-mayor")
}
// TownSessions returns the list of town-level sessions in shutdown order.
// Order matters: Boot (Deacon's watchdog) must be stopped before Deacon,
// otherwise Boot will try to restart Deacon.
func TownSessions() []TownSession {
return []TownSession{
{"Mayor", MayorSessionName()},
{"Boot", BootSessionName()},
{"Deacon", DeaconSessionName()},
}
}
// StopTownSession stops a single town-level tmux session.
// If force is true, skips graceful shutdown (Ctrl-C) and kills immediately.
// Returns true if the session was running and stopped, false if not running.
func StopTownSession(t *tmux.Tmux, ts TownSession, force bool) (bool, error) {
running, err := t.HasSession(ts.SessionID)
if err != nil {
return false, err
}
if !running {
return false, nil
}
return stopTownSessionInternal(t, ts, force)
}
// StopTownSessionWithCache is like StopTownSession but uses a pre-fetched
// SessionSet for O(1) existence check instead of spawning a subprocess.
func StopTownSessionWithCache(t *tmux.Tmux, ts TownSession, force bool, cache *tmux.SessionSet) (bool, error) {
if !cache.Has(ts.SessionID) {
return false, nil
}
return stopTownSessionInternal(t, ts, force)
}
// stopTownSessionInternal performs the actual session stop.
func stopTownSessionInternal(t *tmux.Tmux, ts TownSession, force bool) (bool, error) {
// Try graceful shutdown first (unless forced)
if !force {
_ = t.SendKeysRaw(ts.SessionID, "C-c")
time.Sleep(100 * time.Millisecond)
}
// Log pre-death event for crash investigation (before killing)
reason := "user shutdown"
if force {
reason = "forced shutdown"
}
_ = events.LogFeed(events.TypeSessionDeath, ts.SessionID,
events.SessionDeathPayload(ts.SessionID, ts.Name, reason, "gt down"))
// Kill the session.
// Use KillSessionWithProcesses to ensure all descendant processes are killed.
if err := t.KillSessionWithProcesses(ts.SessionID); err != nil {
return false, fmt.Errorf("killing %s session: %w", ts.Name, err)
}
return true, nil
}