fix: Make Mayor/Deacon session names include town name
Session names `gt-mayor` and `gt-deacon` were hardcoded, causing tmux
session name collisions when running multiple towns simultaneously.
Changed to `gt-{town}-mayor` and `gt-{town}-deacon` format (e.g.,
`gt-ai-mayor`) to allow concurrent multi-town operation.
Key changes:
- session.MayorSessionName() and DeaconSessionName() now take townName param
- Added workspace.GetTownName() helper to load town name from config
- Updated all callers in cmd/, daemon/, doctor/, mail/, rig/, templates/
- Updated tests with new session name format
- Bead IDs remain unchanged (already scoped by .beads/ directory)
Fixes #60
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -20,7 +20,9 @@ import (
|
||||
"github.com/steveyegge/gastown/internal/deacon"
|
||||
"github.com/steveyegge/gastown/internal/feed"
|
||||
"github.com/steveyegge/gastown/internal/polecat"
|
||||
"github.com/steveyegge/gastown/internal/session"
|
||||
"github.com/steveyegge/gastown/internal/tmux"
|
||||
"github.com/steveyegge/gastown/internal/workspace"
|
||||
)
|
||||
|
||||
// Daemon is the town-level background service.
|
||||
@@ -188,12 +190,20 @@ func (d *Daemon) heartbeat(state *State) {
|
||||
d.logger.Printf("Heartbeat complete (#%d)", state.HeartbeatCount)
|
||||
}
|
||||
|
||||
// DeaconSessionName is the tmux session name for the Deacon.
|
||||
const DeaconSessionName = "gt-deacon"
|
||||
|
||||
// DeaconRole is the role name for the Deacon's handoff bead.
|
||||
const DeaconRole = "deacon"
|
||||
|
||||
// getDeaconSessionName returns the Deacon session name for the daemon's town.
|
||||
func (d *Daemon) getDeaconSessionName() string {
|
||||
townName, err := workspace.GetTownName(d.config.TownRoot)
|
||||
if err != nil {
|
||||
// Fallback to legacy name if town config can't be loaded
|
||||
d.logger.Printf("Warning: failed to get town name: %v, using fallback", err)
|
||||
return "gt-deacon"
|
||||
}
|
||||
return session.DeaconSessionName(townName)
|
||||
}
|
||||
|
||||
// ensureBootRunning spawns Boot to triage the Deacon.
|
||||
// Boot is a fresh-each-tick watchdog that decides whether to start/wake/nudge
|
||||
// the Deacon, centralizing the "when to wake" decision in an agent.
|
||||
@@ -238,7 +248,7 @@ func (d *Daemon) runDegradedBootTriage(b *boot.Boot) {
|
||||
}
|
||||
|
||||
// Simple check: is Deacon session alive?
|
||||
hasDeacon, err := d.tmux.HasSession(DeaconSessionName)
|
||||
hasDeacon, err := d.tmux.HasSession(d.getDeaconSessionName())
|
||||
if err != nil {
|
||||
d.logger.Printf("Error checking Deacon session: %v", err)
|
||||
status.LastAction = "error"
|
||||
@@ -265,7 +275,7 @@ func (d *Daemon) runDegradedBootTriage(b *boot.Boot) {
|
||||
// The Deacon is the system's heartbeat - it must always be running.
|
||||
func (d *Daemon) ensureDeaconRunning() {
|
||||
// Check agent bead state (ZFC: trust what agent reports)
|
||||
beadState, beadErr := d.getAgentBeadState("gt-deacon")
|
||||
beadState, beadErr := d.getAgentBeadState(d.getDeaconSessionName())
|
||||
if beadErr == nil {
|
||||
if beadState == "running" || beadState == "working" {
|
||||
// Agent reports it's running - trust it
|
||||
@@ -277,9 +287,10 @@ func (d *Daemon) ensureDeaconRunning() {
|
||||
// Agent bead check failed or state is not running.
|
||||
// FALLBACK: Check if tmux session is actually healthy before attempting restart.
|
||||
// This prevents killing healthy sessions when bead state is stale or unreadable.
|
||||
hasSession, sessionErr := d.tmux.HasSession(DeaconSessionName)
|
||||
deaconSession := d.getDeaconSessionName()
|
||||
hasSession, sessionErr := d.tmux.HasSession(deaconSession)
|
||||
if sessionErr == nil && hasSession {
|
||||
if d.tmux.IsClaudeRunning(DeaconSessionName) {
|
||||
if d.tmux.IsClaudeRunning(deaconSession) {
|
||||
d.logger.Println("Deacon session healthy (Claude running), skipping restart despite stale bead")
|
||||
return
|
||||
}
|
||||
@@ -291,19 +302,20 @@ func (d *Daemon) ensureDeaconRunning() {
|
||||
// Create session in deacon directory (ensures correct CLAUDE.md is loaded)
|
||||
// Use EnsureSessionFresh to handle zombie sessions that exist but have dead Claude
|
||||
deaconDir := filepath.Join(d.config.TownRoot, "deacon")
|
||||
if err := d.tmux.EnsureSessionFresh(DeaconSessionName, deaconDir); err != nil {
|
||||
sessionName := d.getDeaconSessionName()
|
||||
if err := d.tmux.EnsureSessionFresh(sessionName, deaconDir); err != nil {
|
||||
d.logger.Printf("Error creating Deacon session: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Set environment (non-fatal: session works without these)
|
||||
_ = d.tmux.SetEnvironment(DeaconSessionName, "GT_ROLE", "deacon")
|
||||
_ = d.tmux.SetEnvironment(DeaconSessionName, "BD_ACTOR", "deacon")
|
||||
_ = d.tmux.SetEnvironment(sessionName, "GT_ROLE", "deacon")
|
||||
_ = d.tmux.SetEnvironment(sessionName, "BD_ACTOR", "deacon")
|
||||
|
||||
// Launch Claude directly (no shell respawn loop)
|
||||
// The daemon will detect if Claude exits and restart it on next heartbeat
|
||||
// Export GT_ROLE and BD_ACTOR so Claude inherits them (tmux SetEnvironment doesn't export to processes)
|
||||
if err := d.tmux.SendKeys(DeaconSessionName, config.BuildAgentStartupCommand("deacon", "deacon", "", "")); err != nil {
|
||||
if err := d.tmux.SendKeys(sessionName, config.BuildAgentStartupCommand("deacon", "deacon", "", "")); err != nil {
|
||||
d.logger.Printf("Error launching Claude in Deacon session: %v", err)
|
||||
return
|
||||
}
|
||||
@@ -331,8 +343,10 @@ func (d *Daemon) checkDeaconHeartbeat() {
|
||||
|
||||
d.logger.Printf("Deacon heartbeat is stale (%s old), checking session...", age.Round(time.Minute))
|
||||
|
||||
sessionName := d.getDeaconSessionName()
|
||||
|
||||
// Check if session exists
|
||||
hasSession, err := d.tmux.HasSession(DeaconSessionName)
|
||||
hasSession, err := d.tmux.HasSession(sessionName)
|
||||
if err != nil {
|
||||
d.logger.Printf("Error checking Deacon session: %v", err)
|
||||
return
|
||||
@@ -347,14 +361,14 @@ func (d *Daemon) checkDeaconHeartbeat() {
|
||||
if age > 30*time.Minute {
|
||||
// Very stuck - restart the session
|
||||
d.logger.Printf("Deacon stuck for %s - restarting session", age.Round(time.Minute))
|
||||
if err := d.tmux.KillSession(DeaconSessionName); err != nil {
|
||||
if err := d.tmux.KillSession(sessionName); err != nil {
|
||||
d.logger.Printf("Error killing stuck Deacon: %v", err)
|
||||
}
|
||||
// ensureDeaconRunning will be called next heartbeat to restart
|
||||
} else {
|
||||
// Stuck but not critically - nudge to wake up
|
||||
d.logger.Printf("Deacon stuck for %s - nudging session", age.Round(time.Minute))
|
||||
if err := d.tmux.NudgeSession(DeaconSessionName, "HEALTH_CHECK: heartbeat stale, respond to confirm responsiveness"); err != nil {
|
||||
if err := d.tmux.NudgeSession(sessionName, "HEALTH_CHECK: heartbeat stale, respond to confirm responsiveness"); err != nil {
|
||||
d.logger.Printf("Error nudging stuck Deacon: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,9 @@ import (
|
||||
"github.com/steveyegge/gastown/internal/beads"
|
||||
"github.com/steveyegge/gastown/internal/config"
|
||||
"github.com/steveyegge/gastown/internal/constants"
|
||||
"github.com/steveyegge/gastown/internal/session"
|
||||
"github.com/steveyegge/gastown/internal/tmux"
|
||||
"github.com/steveyegge/gastown/internal/workspace"
|
||||
)
|
||||
|
||||
// BeadsMessage represents a message from gt mail inbox --json.
|
||||
@@ -310,8 +312,16 @@ func (d *Daemon) identityToSession(identity string) string {
|
||||
|
||||
// Fallback: use default patterns based on role type
|
||||
switch parsed.RoleType {
|
||||
case "mayor", "deacon":
|
||||
return "gt-" + parsed.RoleType
|
||||
case "mayor":
|
||||
if townName, err := workspace.GetTownName(d.config.TownRoot); err == nil {
|
||||
return session.MayorSessionName(townName)
|
||||
}
|
||||
return ""
|
||||
case "deacon":
|
||||
if townName, err := workspace.GetTownName(d.config.TownRoot); err == nil {
|
||||
return session.DeaconSessionName(townName)
|
||||
}
|
||||
return ""
|
||||
case "witness", "refinery":
|
||||
return fmt.Sprintf("gt-%s-%s", parsed.RigName, parsed.RoleType)
|
||||
case "crew":
|
||||
|
||||
@@ -3,6 +3,8 @@ package daemon
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -14,6 +16,33 @@ func testDaemon() *Daemon {
|
||||
}
|
||||
}
|
||||
|
||||
// testDaemonWithTown creates a Daemon with a proper town setup for testing.
|
||||
// Returns the daemon and a cleanup function.
|
||||
func testDaemonWithTown(t *testing.T, townName string) (*Daemon, func()) {
|
||||
t.Helper()
|
||||
townRoot := t.TempDir()
|
||||
|
||||
// Create mayor directory and town.json
|
||||
mayorDir := filepath.Join(townRoot, "mayor")
|
||||
if err := os.MkdirAll(mayorDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create mayor dir: %v", err)
|
||||
}
|
||||
townJSON := filepath.Join(mayorDir, "town.json")
|
||||
content := `{"name": "` + townName + `"}`
|
||||
if err := os.WriteFile(townJSON, []byte(content), 0644); err != nil {
|
||||
t.Fatalf("failed to write town.json: %v", err)
|
||||
}
|
||||
|
||||
d := &Daemon{
|
||||
config: &Config{TownRoot: townRoot},
|
||||
logger: log.New(io.Discard, "", 0),
|
||||
}
|
||||
|
||||
return d, func() {
|
||||
// Cleanup handled by t.TempDir()
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseLifecycleRequest_Cycle(t *testing.T) {
|
||||
d := testDaemon()
|
||||
|
||||
@@ -152,11 +181,12 @@ func TestParseLifecycleRequest_AlwaysUsesFromField(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestIdentityToSession_Mayor(t *testing.T) {
|
||||
d := testDaemon()
|
||||
d, cleanup := testDaemonWithTown(t, "ai")
|
||||
defer cleanup()
|
||||
|
||||
result := d.identityToSession("mayor")
|
||||
if result != "gt-mayor" {
|
||||
t.Errorf("identityToSession('mayor') = %q, expected 'gt-mayor'", result)
|
||||
if result != "gt-ai-mayor" {
|
||||
t.Errorf("identityToSession('mayor') = %q, expected 'gt-ai-mayor'", result)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user