Merge morsov: AgentIdentity, session helpers
This commit is contained in:
@@ -514,11 +514,12 @@ func (b *Beads) IsBeadsRepo() bool {
|
||||
// AgentFields holds structured fields for agent beads.
|
||||
// These are stored as "key: value" lines in the description.
|
||||
type AgentFields struct {
|
||||
RoleType string // polecat, witness, refinery, deacon, mayor
|
||||
Rig string // Rig name (empty for global agents like mayor/deacon)
|
||||
AgentState string // spawning, working, done, stuck
|
||||
HookBead string // Currently pinned work bead ID
|
||||
RoleBead string // Role definition bead ID (canonical location; may not exist yet)
|
||||
RoleType string // polecat, witness, refinery, deacon, mayor
|
||||
Rig string // Rig name (empty for global agents like mayor/deacon)
|
||||
AgentState string // spawning, working, done, stuck
|
||||
HookBead string // Currently pinned work bead ID
|
||||
RoleBead string // Role definition bead ID (canonical location; may not exist yet)
|
||||
CleanupStatus string // ZFC: polecat self-reports git state (clean, has_uncommitted, has_stash, has_unpushed)
|
||||
}
|
||||
|
||||
// FormatAgentDescription creates a description string from agent fields.
|
||||
@@ -552,6 +553,12 @@ func FormatAgentDescription(title string, fields *AgentFields) string {
|
||||
lines = append(lines, "role_bead: null")
|
||||
}
|
||||
|
||||
if fields.CleanupStatus != "" {
|
||||
lines = append(lines, fmt.Sprintf("cleanup_status: %s", fields.CleanupStatus))
|
||||
} else {
|
||||
lines = append(lines, "cleanup_status: null")
|
||||
}
|
||||
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
@@ -587,6 +594,8 @@ func ParseAgentFields(description string) *AgentFields {
|
||||
fields.HookBead = value
|
||||
case "role_bead":
|
||||
fields.RoleBead = value
|
||||
case "cleanup_status":
|
||||
fields.CleanupStatus = value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -640,6 +649,26 @@ func (b *Beads) UpdateAgentState(id string, state string, hookBead *string) erro
|
||||
return b.Update(id, UpdateOptions{Description: &description})
|
||||
}
|
||||
|
||||
// UpdateAgentCleanupStatus updates the cleanup_status field in an agent bead.
|
||||
// This is called by the polecat to self-report its git state (ZFC compliance).
|
||||
// Valid statuses: clean, has_uncommitted, has_stash, has_unpushed
|
||||
func (b *Beads) UpdateAgentCleanupStatus(id string, cleanupStatus string) error {
|
||||
// First get current issue to preserve other fields
|
||||
issue, err := b.Show(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Parse existing fields
|
||||
fields := ParseAgentFields(issue.Description)
|
||||
fields.CleanupStatus = cleanupStatus
|
||||
|
||||
// Format new description
|
||||
description := FormatAgentDescription(issue.Title, fields)
|
||||
|
||||
return b.Update(id, UpdateOptions{Description: &description})
|
||||
}
|
||||
|
||||
// DeleteAgentBead permanently deletes an agent bead.
|
||||
// Uses --hard --force for immediate permanent deletion (no tombstone).
|
||||
func (b *Beads) DeleteAgentBead(id string) error {
|
||||
|
||||
@@ -227,6 +227,8 @@ func runDone(cmd *cobra.Command, args []string) error {
|
||||
// - COMPLETED → "done"
|
||||
// - ESCALATED → "stuck"
|
||||
// - DEFERRED → "idle"
|
||||
//
|
||||
// Also self-reports cleanup_status for ZFC compliance (#10).
|
||||
func updateAgentStateOnDone(cwd, townRoot, exitType, issueID string) {
|
||||
// Get role context
|
||||
roleInfo, err := GetRoleWithContext(cwd, townRoot)
|
||||
@@ -267,4 +269,37 @@ func updateAgentStateOnDone(cwd, townRoot, exitType, issueID string) {
|
||||
// Silently ignore - beads might not be configured
|
||||
return
|
||||
}
|
||||
|
||||
// ZFC #10: Self-report cleanup status
|
||||
// Compute git state and report so Witness can decide removal safety
|
||||
cleanupStatus := computeCleanupStatus(cwd)
|
||||
if cleanupStatus != "" {
|
||||
if err := bd.UpdateAgentCleanupStatus(agentBeadID, cleanupStatus); err != nil {
|
||||
// Silently ignore
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// computeCleanupStatus checks git state and returns the cleanup status.
|
||||
// Returns the most critical issue: has_unpushed > has_stash > has_uncommitted > clean
|
||||
func computeCleanupStatus(cwd string) string {
|
||||
g := git.NewGit(cwd)
|
||||
status, err := g.CheckUncommittedWork()
|
||||
if err != nil {
|
||||
// If we can't check, report unknown - Witness should be cautious
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// Check in priority order (most critical first)
|
||||
if status.UnpushedCommits > 0 {
|
||||
return "has_unpushed"
|
||||
}
|
||||
if status.StashCount > 0 {
|
||||
return "has_stash"
|
||||
}
|
||||
if status.HasUncommittedChanges {
|
||||
return "has_uncommitted"
|
||||
}
|
||||
return "clean"
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/steveyegge/gastown/internal/session"
|
||||
"github.com/steveyegge/gastown/internal/style"
|
||||
"github.com/steveyegge/gastown/internal/tmux"
|
||||
"github.com/steveyegge/gastown/internal/workspace"
|
||||
@@ -371,34 +372,13 @@ func sessionWorkDir(sessionName, townRoot string) (string, error) {
|
||||
}
|
||||
|
||||
// sessionToGTRole converts a session name to a GT_ROLE value.
|
||||
// Uses session.ParseSessionName for consistent parsing across the codebase.
|
||||
func sessionToGTRole(sessionName string) string {
|
||||
switch {
|
||||
case sessionName == "gt-mayor":
|
||||
return "mayor"
|
||||
case sessionName == "gt-deacon":
|
||||
return "deacon"
|
||||
case strings.Contains(sessionName, "-crew-"):
|
||||
// gt-<rig>-crew-<name> -> <rig>/crew/<name>
|
||||
parts := strings.Split(sessionName, "-")
|
||||
for i, p := range parts {
|
||||
if p == "crew" && i > 1 && i < len(parts)-1 {
|
||||
rig := strings.Join(parts[1:i], "-")
|
||||
name := strings.Join(parts[i+1:], "-")
|
||||
return fmt.Sprintf("%s/crew/%s", rig, name)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
case strings.HasSuffix(sessionName, "-witness"):
|
||||
rig := strings.TrimPrefix(sessionName, "gt-")
|
||||
rig = strings.TrimSuffix(rig, "-witness")
|
||||
return fmt.Sprintf("%s/witness", rig)
|
||||
case strings.HasSuffix(sessionName, "-refinery"):
|
||||
rig := strings.TrimPrefix(sessionName, "gt-")
|
||||
rig = strings.TrimSuffix(rig, "-refinery")
|
||||
return fmt.Sprintf("%s/refinery", rig)
|
||||
default:
|
||||
identity, err := session.ParseSessionName(sessionName)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return identity.GTRole()
|
||||
}
|
||||
|
||||
// detectTownRootFromCwd walks up from the current directory to find the town root.
|
||||
|
||||
@@ -6,18 +6,14 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/steveyegge/gastown/internal/beads"
|
||||
"github.com/steveyegge/gastown/internal/session"
|
||||
"github.com/steveyegge/gastown/internal/style"
|
||||
"github.com/steveyegge/gastown/internal/tmux"
|
||||
)
|
||||
|
||||
// claudeStartupDelay is how long to wait for Claude to start before nudging.
|
||||
// This fixes gt-1dbcp: polecat auto-start doesn't process initial nudge.
|
||||
const claudeStartupDelay = 2 * time.Second
|
||||
|
||||
var slingCmd = &cobra.Command{
|
||||
Use: "sling <bead-or-formula> [target]",
|
||||
GroupID: GroupWork,
|
||||
@@ -181,12 +177,6 @@ func runSling(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
targetAgent = spawnInfo.AgentID()
|
||||
targetPane = spawnInfo.Pane
|
||||
|
||||
// Wait for Claude to start up before nudging (fixes gt-1dbcp)
|
||||
if targetPane != "" {
|
||||
fmt.Printf("Waiting for Claude to initialize...\n")
|
||||
time.Sleep(claudeStartupDelay)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Slinging to an existing agent
|
||||
@@ -257,7 +247,7 @@ func runSling(cmd *cobra.Command, args []string) error {
|
||||
if slingArgs != "" {
|
||||
if err := storeArgsInBead(beadID, slingArgs); err != nil {
|
||||
// Warn but don't fail - args will still be in the nudge prompt
|
||||
style.PrintWarning("Could not store args in bead: %v", err)
|
||||
fmt.Printf("%s Could not store args in bead: %v\n", style.Dim.Render("Warning:"), err)
|
||||
} else {
|
||||
fmt.Printf("%s Args stored in bead (durable)\n", style.Bold.Render("✓"))
|
||||
}
|
||||
@@ -373,40 +363,14 @@ func resolveTargetAgent(target string) (agentID string, pane string, hookRoot st
|
||||
}
|
||||
|
||||
// sessionToAgentID converts a session name to agent ID format.
|
||||
func sessionToAgentID(session string) string {
|
||||
switch {
|
||||
case session == "gt-mayor":
|
||||
return "mayor"
|
||||
case session == "gt-deacon":
|
||||
return "deacon"
|
||||
case strings.Contains(session, "-crew-"):
|
||||
// gt-gastown-crew-max -> gastown/crew/max
|
||||
parts := strings.Split(session, "-")
|
||||
for i, p := range parts {
|
||||
if p == "crew" && i > 1 && i < len(parts)-1 {
|
||||
rig := strings.Join(parts[1:i], "-")
|
||||
name := strings.Join(parts[i+1:], "-")
|
||||
return fmt.Sprintf("%s/crew/%s", rig, name)
|
||||
}
|
||||
}
|
||||
case strings.HasSuffix(session, "-witness"):
|
||||
rig := strings.TrimPrefix(session, "gt-")
|
||||
rig = strings.TrimSuffix(rig, "-witness")
|
||||
return fmt.Sprintf("%s/witness", rig)
|
||||
case strings.HasSuffix(session, "-refinery"):
|
||||
rig := strings.TrimPrefix(session, "gt-")
|
||||
rig = strings.TrimSuffix(rig, "-refinery")
|
||||
return fmt.Sprintf("%s/refinery", rig)
|
||||
case strings.HasPrefix(session, "gt-"):
|
||||
// gt-gastown-nux -> gastown/polecats/nux (polecat)
|
||||
parts := strings.Split(strings.TrimPrefix(session, "gt-"), "-")
|
||||
if len(parts) >= 2 {
|
||||
rig := parts[0]
|
||||
name := strings.Join(parts[1:], "-")
|
||||
return fmt.Sprintf("%s/polecats/%s", rig, name)
|
||||
}
|
||||
// Uses session.ParseSessionName for consistent parsing across the codebase.
|
||||
func sessionToAgentID(sessionName string) string {
|
||||
identity, err := session.ParseSessionName(sessionName)
|
||||
if err != nil {
|
||||
// Fallback for unparseable sessions
|
||||
return sessionName
|
||||
}
|
||||
return session
|
||||
return identity.Address()
|
||||
}
|
||||
|
||||
// verifyBeadExists checks that the bead exists using bd show.
|
||||
@@ -550,12 +514,6 @@ func runSlingFormula(args []string) error {
|
||||
}
|
||||
targetAgent = spawnInfo.AgentID()
|
||||
targetPane = spawnInfo.Pane
|
||||
|
||||
// Wait for Claude to start up before nudging (fixes gt-1dbcp)
|
||||
if targetPane != "" {
|
||||
fmt.Printf("Waiting for Claude to initialize...\n")
|
||||
time.Sleep(claudeStartupDelay)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Slinging to an existing agent
|
||||
@@ -614,7 +572,7 @@ func runSlingFormula(args []string) error {
|
||||
}
|
||||
if err := json.Unmarshal(wispOut, &wispResult); err != nil {
|
||||
// Fallback: use formula name as identifier, but warn user
|
||||
style.PrintWarning("Could not parse wisp output, using formula name as ID")
|
||||
fmt.Printf("%s Could not parse wisp output, using formula name as ID\n", style.Dim.Render("Warning:"))
|
||||
wispResult.RootID = formulaName
|
||||
}
|
||||
|
||||
@@ -634,7 +592,7 @@ func runSlingFormula(args []string) error {
|
||||
// Store args in wisp bead if provided (no-tmux mode: beads as data plane)
|
||||
if slingArgs != "" {
|
||||
if err := storeArgsInBead(wispResult.RootID, slingArgs); err != nil {
|
||||
style.PrintWarning("Could not store args in bead: %v", err)
|
||||
fmt.Printf("%s Could not store args in bead: %v\n", style.Dim.Render("Warning:"), err)
|
||||
} else {
|
||||
fmt.Printf("%s Args stored in bead (durable)\n", style.Bold.Render("✓"))
|
||||
}
|
||||
|
||||
@@ -89,6 +89,53 @@ func (m *Manager) agentBeadID(name string) string {
|
||||
return fmt.Sprintf("gt-polecat-%s-%s", m.rig.Name, name)
|
||||
}
|
||||
|
||||
// getCleanupStatusFromBead reads the cleanup_status from the polecat's agent bead.
|
||||
// Returns empty string if the bead doesn't exist or has no cleanup_status.
|
||||
// ZFC #10: This is the ZFC-compliant way to check if removal is safe.
|
||||
func (m *Manager) getCleanupStatusFromBead(name string) string {
|
||||
agentID := m.agentBeadID(name)
|
||||
_, fields, err := m.beads.GetAgentBead(agentID)
|
||||
if err != nil || fields == nil {
|
||||
return ""
|
||||
}
|
||||
return fields.CleanupStatus
|
||||
}
|
||||
|
||||
// checkCleanupStatus validates the cleanup status against removal safety rules.
|
||||
// Returns an error if removal should be blocked based on the status.
|
||||
// force=true: allow has_uncommitted, block has_stash and has_unpushed
|
||||
// force=false: block all non-clean statuses
|
||||
func (m *Manager) checkCleanupStatus(name, cleanupStatus string, force bool) error {
|
||||
switch cleanupStatus {
|
||||
case "clean":
|
||||
return nil
|
||||
case "has_uncommitted":
|
||||
if force {
|
||||
return nil // force bypasses uncommitted changes
|
||||
}
|
||||
return &UncommittedWorkError{
|
||||
PolecatName: name,
|
||||
Status: &git.UncommittedWorkStatus{HasUncommittedChanges: true},
|
||||
}
|
||||
case "has_stash":
|
||||
return &UncommittedWorkError{
|
||||
PolecatName: name,
|
||||
Status: &git.UncommittedWorkStatus{StashCount: 1},
|
||||
}
|
||||
case "has_unpushed":
|
||||
return &UncommittedWorkError{
|
||||
PolecatName: name,
|
||||
Status: &git.UncommittedWorkStatus{UnpushedCommits: 1},
|
||||
}
|
||||
default:
|
||||
// Unknown status - be conservative and block
|
||||
return &UncommittedWorkError{
|
||||
PolecatName: name,
|
||||
Status: &git.UncommittedWorkStatus{HasUncommittedChanges: true},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// repoBase returns the git directory and Git object to use for worktree operations.
|
||||
// Prefers the shared bare repo (.repo.git) if it exists, otherwise falls back to mayor/rig.
|
||||
// The bare repo architecture allows all worktrees (refinery, polecats) to share branch visibility.
|
||||
@@ -206,26 +253,41 @@ func (m *Manager) Remove(name string, force bool) error {
|
||||
// RemoveWithOptions deletes a polecat worktree with explicit control over safety checks.
|
||||
// force=true: bypass uncommitted changes check (legacy behavior)
|
||||
// nuclear=true: bypass ALL safety checks including stashes and unpushed commits
|
||||
//
|
||||
// ZFC #10: Uses cleanup_status from agent bead if available (polecat self-report),
|
||||
// falls back to git check for backward compatibility.
|
||||
func (m *Manager) RemoveWithOptions(name string, force, nuclear bool) error {
|
||||
if !m.exists(name) {
|
||||
return ErrPolecatNotFound
|
||||
}
|
||||
|
||||
polecatPath := m.polecatDir(name)
|
||||
polecatGit := git.NewGit(polecatPath)
|
||||
|
||||
// Check for uncommitted work unless bypassed
|
||||
if !nuclear {
|
||||
status, err := polecatGit.CheckUncommittedWork()
|
||||
if err == nil && !status.Clean() {
|
||||
// For backward compatibility: force only bypasses uncommitted changes, not stashes/unpushed
|
||||
if force {
|
||||
// Force mode: allow uncommitted changes but still block on stashes/unpushed
|
||||
if status.StashCount > 0 || status.UnpushedCommits > 0 {
|
||||
// ZFC #10: First try to read cleanup_status from agent bead
|
||||
// This is the ZFC-compliant path - trust what the polecat reported
|
||||
cleanupStatus := m.getCleanupStatusFromBead(name)
|
||||
|
||||
if cleanupStatus != "" && cleanupStatus != "unknown" {
|
||||
// ZFC path: Use polecat's self-reported status
|
||||
if err := m.checkCleanupStatus(name, cleanupStatus, force); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// Fallback path: Check git directly (for polecats that haven't reported yet)
|
||||
polecatGit := git.NewGit(polecatPath)
|
||||
status, err := polecatGit.CheckUncommittedWork()
|
||||
if err == nil && !status.Clean() {
|
||||
// For backward compatibility: force only bypasses uncommitted changes, not stashes/unpushed
|
||||
if force {
|
||||
// Force mode: allow uncommitted changes but still block on stashes/unpushed
|
||||
if status.StashCount > 0 || status.UnpushedCommits > 0 {
|
||||
return &UncommittedWorkError{PolecatName: name, Status: status}
|
||||
}
|
||||
} else {
|
||||
return &UncommittedWorkError{PolecatName: name, Status: status}
|
||||
}
|
||||
} else {
|
||||
return &UncommittedWorkError{PolecatName: name, Status: status}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
144
internal/session/identity.go
Normal file
144
internal/session/identity.go
Normal file
@@ -0,0 +1,144 @@
|
||||
// Package session provides polecat session lifecycle management.
|
||||
package session
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Role represents the type of Gas Town agent.
|
||||
type Role string
|
||||
|
||||
const (
|
||||
RoleMayor Role = "mayor"
|
||||
RoleDeacon Role = "deacon"
|
||||
RoleWitness Role = "witness"
|
||||
RoleRefinery Role = "refinery"
|
||||
RoleCrew Role = "crew"
|
||||
RolePolecat Role = "polecat"
|
||||
)
|
||||
|
||||
// AgentIdentity represents a parsed Gas Town agent identity.
|
||||
type AgentIdentity struct {
|
||||
Role Role // mayor, deacon, witness, refinery, crew, polecat
|
||||
Rig string // empty for mayor/deacon
|
||||
Name string // crew/polecat name (empty for mayor/deacon/witness/refinery)
|
||||
}
|
||||
|
||||
// ParseSessionName parses a tmux session name into an AgentIdentity.
|
||||
//
|
||||
// Session name formats:
|
||||
// - gt-mayor → Role: mayor
|
||||
// - gt-deacon → Role: deacon
|
||||
// - gt-<rig>-witness → Role: witness, Rig: <rig>
|
||||
// - gt-<rig>-refinery → Role: refinery, Rig: <rig>
|
||||
// - gt-<rig>-crew-<name> → Role: crew, Rig: <rig>, Name: <name>
|
||||
// - gt-<rig>-<name> → Role: polecat, Rig: <rig>, Name: <name>
|
||||
//
|
||||
// For polecat sessions without a crew marker, the last segment after the rig
|
||||
// is assumed to be the polecat name. This works for simple rig names but may
|
||||
// be ambiguous for rig names containing hyphens.
|
||||
func ParseSessionName(session string) (*AgentIdentity, error) {
|
||||
if !strings.HasPrefix(session, Prefix) {
|
||||
return nil, fmt.Errorf("invalid session name %q: missing %q prefix", session, Prefix)
|
||||
}
|
||||
|
||||
suffix := strings.TrimPrefix(session, Prefix)
|
||||
if suffix == "" {
|
||||
return nil, fmt.Errorf("invalid session name %q: empty after prefix", session)
|
||||
}
|
||||
|
||||
// Check for global roles first (no rig)
|
||||
switch suffix {
|
||||
case "mayor":
|
||||
return &AgentIdentity{Role: RoleMayor}, nil
|
||||
case "deacon":
|
||||
return &AgentIdentity{Role: RoleDeacon}, nil
|
||||
}
|
||||
|
||||
// Parse rig-based roles
|
||||
parts := strings.Split(suffix, "-")
|
||||
if len(parts) < 2 {
|
||||
return nil, fmt.Errorf("invalid session name %q: expected rig-role format", session)
|
||||
}
|
||||
|
||||
// Check for witness/refinery (suffix markers)
|
||||
if parts[len(parts)-1] == "witness" {
|
||||
rig := strings.Join(parts[:len(parts)-1], "-")
|
||||
return &AgentIdentity{Role: RoleWitness, Rig: rig}, nil
|
||||
}
|
||||
if parts[len(parts)-1] == "refinery" {
|
||||
rig := strings.Join(parts[:len(parts)-1], "-")
|
||||
return &AgentIdentity{Role: RoleRefinery, Rig: rig}, nil
|
||||
}
|
||||
|
||||
// Check for crew (marker in middle)
|
||||
for i, p := range parts {
|
||||
if p == "crew" && i > 0 && i < len(parts)-1 {
|
||||
rig := strings.Join(parts[:i], "-")
|
||||
name := strings.Join(parts[i+1:], "-")
|
||||
return &AgentIdentity{Role: RoleCrew, Rig: rig, Name: name}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Default to polecat: rig is everything except the last segment
|
||||
if len(parts) < 2 {
|
||||
return nil, fmt.Errorf("invalid session name %q: cannot determine rig/name", session)
|
||||
}
|
||||
rig := strings.Join(parts[:len(parts)-1], "-")
|
||||
name := parts[len(parts)-1]
|
||||
return &AgentIdentity{Role: RolePolecat, Rig: rig, Name: name}, nil
|
||||
}
|
||||
|
||||
// SessionName returns the tmux session name for this identity.
|
||||
func (a *AgentIdentity) SessionName() string {
|
||||
switch a.Role {
|
||||
case RoleMayor:
|
||||
return MayorSessionName()
|
||||
case RoleDeacon:
|
||||
return DeaconSessionName()
|
||||
case RoleWitness:
|
||||
return WitnessSessionName(a.Rig)
|
||||
case RoleRefinery:
|
||||
return RefinerySessionName(a.Rig)
|
||||
case RoleCrew:
|
||||
return CrewSessionName(a.Rig, a.Name)
|
||||
case RolePolecat:
|
||||
return PolecatSessionName(a.Rig, a.Name)
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// Address returns the mail-style address for this identity.
|
||||
// Examples:
|
||||
// - mayor → "mayor"
|
||||
// - deacon → "deacon"
|
||||
// - witness → "gastown/witness"
|
||||
// - refinery → "gastown/refinery"
|
||||
// - crew → "gastown/crew/max"
|
||||
// - polecat → "gastown/polecats/Toast"
|
||||
func (a *AgentIdentity) Address() string {
|
||||
switch a.Role {
|
||||
case RoleMayor:
|
||||
return "mayor"
|
||||
case RoleDeacon:
|
||||
return "deacon"
|
||||
case RoleWitness:
|
||||
return fmt.Sprintf("%s/witness", a.Rig)
|
||||
case RoleRefinery:
|
||||
return fmt.Sprintf("%s/refinery", a.Rig)
|
||||
case RoleCrew:
|
||||
return fmt.Sprintf("%s/crew/%s", a.Rig, a.Name)
|
||||
case RolePolecat:
|
||||
return fmt.Sprintf("%s/polecats/%s", a.Rig, a.Name)
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// GTRole returns the GT_ROLE environment variable format.
|
||||
// This is the same as Address() for most roles.
|
||||
func (a *AgentIdentity) GTRole() string {
|
||||
return a.Address()
|
||||
}
|
||||
252
internal/session/identity_test.go
Normal file
252
internal/session/identity_test.go
Normal file
@@ -0,0 +1,252 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseSessionName(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
session string
|
||||
wantRole Role
|
||||
wantRig string
|
||||
wantName string
|
||||
wantErr bool
|
||||
}{
|
||||
// Global roles (no rig)
|
||||
{
|
||||
name: "mayor",
|
||||
session: "gt-mayor",
|
||||
wantRole: RoleMayor,
|
||||
},
|
||||
{
|
||||
name: "deacon",
|
||||
session: "gt-deacon",
|
||||
wantRole: RoleDeacon,
|
||||
},
|
||||
|
||||
// Witness (simple rig)
|
||||
{
|
||||
name: "witness simple rig",
|
||||
session: "gt-gastown-witness",
|
||||
wantRole: RoleWitness,
|
||||
wantRig: "gastown",
|
||||
},
|
||||
{
|
||||
name: "witness hyphenated rig",
|
||||
session: "gt-foo-bar-witness",
|
||||
wantRole: RoleWitness,
|
||||
wantRig: "foo-bar",
|
||||
},
|
||||
|
||||
// Refinery (simple rig)
|
||||
{
|
||||
name: "refinery simple rig",
|
||||
session: "gt-gastown-refinery",
|
||||
wantRole: RoleRefinery,
|
||||
wantRig: "gastown",
|
||||
},
|
||||
{
|
||||
name: "refinery hyphenated rig",
|
||||
session: "gt-my-project-refinery",
|
||||
wantRole: RoleRefinery,
|
||||
wantRig: "my-project",
|
||||
},
|
||||
|
||||
// Crew (with marker)
|
||||
{
|
||||
name: "crew simple",
|
||||
session: "gt-gastown-crew-max",
|
||||
wantRole: RoleCrew,
|
||||
wantRig: "gastown",
|
||||
wantName: "max",
|
||||
},
|
||||
{
|
||||
name: "crew hyphenated rig",
|
||||
session: "gt-foo-bar-crew-alice",
|
||||
wantRole: RoleCrew,
|
||||
wantRig: "foo-bar",
|
||||
wantName: "alice",
|
||||
},
|
||||
{
|
||||
name: "crew hyphenated name",
|
||||
session: "gt-gastown-crew-my-worker",
|
||||
wantRole: RoleCrew,
|
||||
wantRig: "gastown",
|
||||
wantName: "my-worker",
|
||||
},
|
||||
|
||||
// Polecat (fallback)
|
||||
{
|
||||
name: "polecat simple",
|
||||
session: "gt-gastown-morsov",
|
||||
wantRole: RolePolecat,
|
||||
wantRig: "gastown",
|
||||
wantName: "morsov",
|
||||
},
|
||||
{
|
||||
name: "polecat hyphenated rig",
|
||||
session: "gt-foo-bar-Toast",
|
||||
wantRole: RolePolecat,
|
||||
wantRig: "foo-bar",
|
||||
wantName: "Toast",
|
||||
},
|
||||
|
||||
// Error cases
|
||||
{
|
||||
name: "missing prefix",
|
||||
session: "gastown-witness",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "empty after prefix",
|
||||
session: "gt-",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "just prefix",
|
||||
session: "gt-x",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := ParseSessionName(tt.session)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("ParseSessionName(%q) error = %v, wantErr %v", tt.session, err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if got.Role != tt.wantRole {
|
||||
t.Errorf("ParseSessionName(%q).Role = %v, want %v", tt.session, got.Role, tt.wantRole)
|
||||
}
|
||||
if got.Rig != tt.wantRig {
|
||||
t.Errorf("ParseSessionName(%q).Rig = %v, want %v", tt.session, got.Rig, tt.wantRig)
|
||||
}
|
||||
if got.Name != tt.wantName {
|
||||
t.Errorf("ParseSessionName(%q).Name = %v, want %v", tt.session, got.Name, tt.wantName)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentIdentity_SessionName(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
identity AgentIdentity
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "mayor",
|
||||
identity: AgentIdentity{Role: RoleMayor},
|
||||
want: "gt-mayor",
|
||||
},
|
||||
{
|
||||
name: "deacon",
|
||||
identity: AgentIdentity{Role: RoleDeacon},
|
||||
want: "gt-deacon",
|
||||
},
|
||||
{
|
||||
name: "witness",
|
||||
identity: AgentIdentity{Role: RoleWitness, Rig: "gastown"},
|
||||
want: "gt-gastown-witness",
|
||||
},
|
||||
{
|
||||
name: "refinery",
|
||||
identity: AgentIdentity{Role: RoleRefinery, Rig: "my-project"},
|
||||
want: "gt-my-project-refinery",
|
||||
},
|
||||
{
|
||||
name: "crew",
|
||||
identity: AgentIdentity{Role: RoleCrew, Rig: "gastown", Name: "max"},
|
||||
want: "gt-gastown-crew-max",
|
||||
},
|
||||
{
|
||||
name: "polecat",
|
||||
identity: AgentIdentity{Role: RolePolecat, Rig: "gastown", Name: "morsov"},
|
||||
want: "gt-gastown-morsov",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.identity.SessionName(); got != tt.want {
|
||||
t.Errorf("AgentIdentity.SessionName() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentIdentity_Address(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
identity AgentIdentity
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "mayor",
|
||||
identity: AgentIdentity{Role: RoleMayor},
|
||||
want: "mayor",
|
||||
},
|
||||
{
|
||||
name: "deacon",
|
||||
identity: AgentIdentity{Role: RoleDeacon},
|
||||
want: "deacon",
|
||||
},
|
||||
{
|
||||
name: "witness",
|
||||
identity: AgentIdentity{Role: RoleWitness, Rig: "gastown"},
|
||||
want: "gastown/witness",
|
||||
},
|
||||
{
|
||||
name: "refinery",
|
||||
identity: AgentIdentity{Role: RoleRefinery, Rig: "my-project"},
|
||||
want: "my-project/refinery",
|
||||
},
|
||||
{
|
||||
name: "crew",
|
||||
identity: AgentIdentity{Role: RoleCrew, Rig: "gastown", Name: "max"},
|
||||
want: "gastown/crew/max",
|
||||
},
|
||||
{
|
||||
name: "polecat",
|
||||
identity: AgentIdentity{Role: RolePolecat, Rig: "gastown", Name: "Toast"},
|
||||
want: "gastown/polecats/Toast",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := tt.identity.Address(); got != tt.want {
|
||||
t.Errorf("AgentIdentity.Address() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseSessionName_RoundTrip(t *testing.T) {
|
||||
// Test that parsing then reconstructing gives the same result
|
||||
sessions := []string{
|
||||
"gt-mayor",
|
||||
"gt-deacon",
|
||||
"gt-gastown-witness",
|
||||
"gt-foo-bar-refinery",
|
||||
"gt-gastown-crew-max",
|
||||
"gt-gastown-morsov",
|
||||
}
|
||||
|
||||
for _, session := range sessions {
|
||||
t.Run(session, func(t *testing.T) {
|
||||
identity, err := ParseSessionName(session)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseSessionName(%q) error = %v", session, err)
|
||||
}
|
||||
if got := identity.SessionName(); got != session {
|
||||
t.Errorf("Round-trip failed: ParseSessionName(%q).SessionName() = %q", session, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user