Role-Based Agent System

Medium
Agents

Implement role-based agent system:

  1. assign_role(role, permissions, behavior_rules): Set agent role
    • role: string name
    • permissions: list of allowed actions
    • behavior_rules: constraints like 'max_calls_per_minute'
  2. can_perform(action): Check permission
  3. execute_with_role(task): Execute if permitted
    • Check permission
    • Apply behavior constraints
    • Return compliance violations
  4. validate_compliance(actions): Check action history against rules

Behavior Rules: {'max_calls_per_minute': 10, 'allowed_tools': ['search']}

Compliance: List of violated rules or empty if compliant.

Examples

Example 1:
Input: agent = RoleBasedAgent('A'); agent.assign_role('researcher', ['search', 'read'], {}); agent.can_perform('search')
Output: True
Explanation: Researcher role has search permission

Starter Code

class RoleBasedAgent:
    """
    Agent that operates based on assigned role with specific behaviors.
    """
    
    def __init__(self, agent_id):
        self.agent_id = agent_id
        self.role = None
        self.permissions = set()
        self.behaviors = {}
    
    def assign_role(self, role, permissions, behavior_rules):
        """Assign role with permissions and behavior rules"""
        # Your implementation here
        pass
    
    def can_perform(self, action):
        """Check if agent has permission for action"""
        # Your implementation here
        pass
    
    def execute_with_role(self, task):
        """
        Execute task according to role behaviors.
        Returns {'performed': bool, 'result': ..., 'compliance': [...]}
        """
        # Your implementation here
        pass
    
    def validate_compliance(self, actions):
        """Check if actions comply with role rules"""
        # Your implementation here
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews