diff --git a/internal/cmd/mail.go b/internal/cmd/mail.go index 0d673d4d..7936ef8d 100644 --- a/internal/cmd/mail.go +++ b/internal/cmd/mail.go @@ -292,10 +292,20 @@ func runMailSend(cmd *cobra.Command, args []string) error { if err != nil || townRoot == "" { return fmt.Errorf("not in a Gas Town workspace") } - ctx := detectRole(cwd, townRoot) + roleInfo, err := GetRoleWithContext(cwd, townRoot) + if err != nil { + return fmt.Errorf("detecting role: %w", err) + } + ctx := RoleContext{ + Role: roleInfo.Role, + Rig: roleInfo.Rig, + Polecat: roleInfo.Polecat, + TownRoot: townRoot, + WorkDir: cwd, + } to = buildAgentIdentity(ctx) if to == "" { - return fmt.Errorf("cannot determine identity from current directory (role: %s)", ctx.Role) + return fmt.Errorf("cannot determine identity (role: %s)", ctx.Role) } } else if len(args) > 0 { to = args[0] diff --git a/internal/cmd/molecule_attach.go b/internal/cmd/molecule_attach.go index b5731e8c..b6eaffef 100644 --- a/internal/cmd/molecule_attach.go +++ b/internal/cmd/molecule_attach.go @@ -131,7 +131,7 @@ func runMoleculeAttachment(cmd *cobra.Command, args []string) error { return nil } -// detectCurrentAgent returns the current agent identity based on working directory. +// detectCurrentAgent returns the current agent identity based on GT_ROLE or working directory. // Returns empty string if identity cannot be determined. func detectCurrentAgent() string { cwd, err := os.Getwd() @@ -144,6 +144,16 @@ func detectCurrentAgent() string { return "" } - ctx := detectRole(cwd, townRoot) + roleInfo, err := GetRoleWithContext(cwd, townRoot) + if err != nil { + return "" + } + ctx := RoleContext{ + Role: roleInfo.Role, + Rig: roleInfo.Rig, + Polecat: roleInfo.Polecat, + TownRoot: townRoot, + WorkDir: cwd, + } return buildAgentIdentity(ctx) } diff --git a/internal/cmd/molecule_attach_from_mail.go b/internal/cmd/molecule_attach_from_mail.go index 645f0e2d..31e3a7c2 100644 --- a/internal/cmd/molecule_attach_from_mail.go +++ b/internal/cmd/molecule_attach_from_mail.go @@ -30,11 +30,21 @@ func runMoleculeAttachFromMail(cmd *cobra.Command, args []string) error { return fmt.Errorf("not in a Gas Town workspace") } - // Detect agent role and identity - roleCtx := detectRole(cwd, townRoot) + // Detect agent role and identity using env-aware detection + roleInfo, err := GetRoleWithContext(cwd, townRoot) + if err != nil { + return fmt.Errorf("detecting role: %w", err) + } + roleCtx := RoleContext{ + Role: roleInfo.Role, + Rig: roleInfo.Rig, + Polecat: roleInfo.Polecat, + TownRoot: townRoot, + WorkDir: cwd, + } agentIdentity := buildAgentIdentity(roleCtx) if agentIdentity == "" { - return fmt.Errorf("cannot determine agent identity from current directory (role: %s)", roleCtx.Role) + return fmt.Errorf("cannot determine agent identity (role: %s)", roleCtx.Role) } // Get the agent's mailbox diff --git a/internal/cmd/molecule_lifecycle.go b/internal/cmd/molecule_lifecycle.go index 2e5fe0a4..976ab28c 100644 --- a/internal/cmd/molecule_lifecycle.go +++ b/internal/cmd/molecule_lifecycle.go @@ -289,11 +289,21 @@ func runMoleculeBurn(cmd *cobra.Command, args []string) error { if len(args) > 0 { target = args[0] } else { - // Auto-detect from current directory - roleCtx := detectRole(cwd, townRoot) + // Auto-detect using env-aware role detection + roleInfo, err := GetRoleWithContext(cwd, townRoot) + if err != nil { + return fmt.Errorf("detecting role: %w", err) + } + roleCtx := RoleContext{ + Role: roleInfo.Role, + Rig: roleInfo.Rig, + Polecat: roleInfo.Polecat, + TownRoot: townRoot, + WorkDir: cwd, + } target = buildAgentIdentity(roleCtx) if target == "" { - return fmt.Errorf("cannot determine agent identity from current directory") + return fmt.Errorf("cannot determine agent identity (role: %s)", roleCtx.Role) } } @@ -375,11 +385,21 @@ func runMoleculeSquash(cmd *cobra.Command, args []string) error { if len(args) > 0 { target = args[0] } else { - // Auto-detect from current directory - roleCtx := detectRole(cwd, townRoot) + // Auto-detect using env-aware role detection + roleInfo, err := GetRoleWithContext(cwd, townRoot) + if err != nil { + return fmt.Errorf("detecting role: %w", err) + } + roleCtx := RoleContext{ + Role: roleInfo.Role, + Rig: roleInfo.Rig, + Polecat: roleInfo.Polecat, + TownRoot: townRoot, + WorkDir: cwd, + } target = buildAgentIdentity(roleCtx) if target == "" { - return fmt.Errorf("cannot determine agent identity from current directory") + return fmt.Errorf("cannot determine agent identity (role: %s)", roleCtx.Role) } } diff --git a/internal/cmd/rig.go b/internal/cmd/rig.go index 34a69439..5e7c785d 100644 --- a/internal/cmd/rig.go +++ b/internal/cmd/rig.go @@ -339,12 +339,15 @@ func runRigReset(cmd *cobra.Command, args []string) error { // Determine role to reset roleKey := rigResetRole if roleKey == "" { - // Auto-detect from cwd - ctx := detectRole(cwd, townRoot) - if ctx.Role == RoleUnknown { - return fmt.Errorf("could not detect role from current directory; use --role to specify") + // Auto-detect using env-aware role detection + roleInfo, err := GetRoleWithContext(cwd, townRoot) + if err != nil { + return fmt.Errorf("detecting role: %w", err) } - roleKey = string(ctx.Role) + if roleInfo.Role == RoleUnknown { + return fmt.Errorf("could not detect role; use --role to specify") + } + roleKey = string(roleInfo.Role) } // If no specific flags, reset all; otherwise only reset what's specified