Environment Boundaries and Constraints › Implementing a Complete Boundary System

Michael BrenndoerferJuly 17, 20252 min read
AI Agent HandbookAll chapters
Part 8 of 15Environment and Interaction - The Agent's World
Chapter 23Environment Boundaries and Constraints › Implementing a Complete Boundary System
Reading section 6 of 11 · Implementing a Complete Boundary System · 2 minSection 6 of 11 · 2 min

Book contents

Section 6 of 11 · 2 min read

Environment Boundaries and Constraints › Implementing a Complete Boundary SystemOne section · Sections in this chapter6 / 11 ↓

Let's bring everything together into a cohesive boundary system for our personal assistant. This combines all the constraint types we've discussed:

In[9]:
Code
## Example (Claude Sonnet 4.5)
## Using Claude Sonnet 4.5 for its ability to reason about complex permission systems


class AgentBoundarySystem:
    def __init__(self, config):
        self.access_policy = config.get("access_policy", {})
        self.action_policy = config.get("action_policy", {})
        self.rate_limiters = config.get("rate_limiters", {})
        self.scope_boundaries = config.get("scope_boundaries", {})
        self.environment_mode = config.get("environment", "production")

    def can_execute(self, action_request):
        """
        Check if an action can be executed given all constraints.
        Returns (allowed, reason) tuple.
        """
        action_type = action_request["type"]
        resource = action_request.get("resource")

        # Check access permissions
        if not self.check_access_permission(resource, action_type):
            return False, "Access denied: insufficient permissions"

        # Check action policy
        if self.requires_confirmation(action_type):
            if not action_request.get("user_confirmed"):
                return False, "Action requires user confirmation"

        # Check rate limits
        if not self.check_rate_limit(action_type):
            return False, "Rate limit exceeded"

        # Check scope boundaries
        if not self.within_scope(action_request):
            return False, "Action outside allowed scope"

        return True, "Action allowed"

    def check_access_permission(self, resource, action):
        """Check if we have permission for this resource/action combo"""
        if resource not in self.access_policy:
            return False
        return action in self.access_policy[resource]

    def requires_confirmation(self, action_type):
        """Check if action needs user confirmation"""
        return self.action_policy.get(action_type, {}).get("confirm", False)

    def check_rate_limit(self, action_type):
        """Check rate limits for this action type"""
        if action_type in self.rate_limiters:
            return self.rate_limiters[action_type].can_proceed(action_type)
        return True

    def within_scope(self, action_request):
        """Check if action is within allowed scope"""
        # Check file system boundaries
        if "file_path" in action_request:
            fs_boundary = self.scope_boundaries.get("filesystem")
            if fs_boundary and not fs_boundary.is_allowed(
                action_request["file_path"]
            ):
                return False

        # Check network boundaries
        if "url" in action_request:
            net_boundary = self.scope_boundaries.get("network")
            if net_boundary and not net_boundary.can_access(
                action_request["url"]
            ):
                return False

        return True


## Configure the boundary system
config = {
    "access_policy": {
        "calendar": ["read", "write"],
        "email": ["read", "draft"],
        "contacts": ["read"],
    },
    "action_policy": {
        "send": {"confirm": True},
        "delete": {"confirm": True},
        "read": {"confirm": False},
    },
    "rate_limiters": {"email": RateLimiter(max_calls=50, time_window=3600)},
    "scope_boundaries": {
        "filesystem": FileSystemBoundary(["/home/user/agent_workspace"]),
        "network": NetworkBoundary(
            ["api.weather.com", "api.calendar.google.com"]
        ),
    },
    "environment": "production",
}

boundary_system = AgentBoundarySystem(config)

## Test various actions
test_actions = [
    {
        "type": "read",
        "resource": "calendar",
        "description": "Read calendar events",
    },
    {
        "type": "send",
        "resource": "email",
        "description": "Send email",
        "user_confirmed": False,
    },
    {
        "type": "write",
        "resource": "calendar",
        "description": "Create calendar event",
    },
]

for action in test_actions:
    allowed, reason = boundary_system.can_execute(action)
    status = "✓ Allowed" if allowed else "✗ Blocked"
    print(f"{status}: {action['description']} - {reason}")
Out[9]:
Console
✓ Allowed: Read calendar events - Action allowed
✗ Blocked: Send email - Access denied: insufficient permissions
✓ Allowed: Create calendar event - Action allowed

This system checks every configured boundary before allowing an action while keeping the decision path readable and maintainable.