-
Notifications
You must be signed in to change notification settings - Fork 0
feat: implement hierarchical delegation and loop prevention (#12, #17) #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b58c572
feat: implement hierarchical delegation and loop prevention (#12, #17)
Aureliolo d46b455
refactor: address pre-PR review findings across delegation and loop p…
Aureliolo a9a1ba9
refactor: address 33 PR review findings from local agents and externa…
Aureliolo 1d8d010
refactor: address 7 post-push review findings from CodeRabbit and Gre…
Aureliolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| """Hierarchical delegation subsystem.""" | ||
|
|
||
| from ai_company.communication.delegation.authority import ( | ||
| AuthorityCheckResult, | ||
| AuthorityValidator, | ||
| ) | ||
| from ai_company.communication.delegation.hierarchy import ( | ||
| HierarchyResolver, | ||
| ) | ||
| from ai_company.communication.delegation.models import ( | ||
| DelegationRecord, | ||
| DelegationRequest, | ||
| DelegationResult, | ||
| ) | ||
| from ai_company.communication.delegation.service import ( | ||
| DelegationService, | ||
| ) | ||
|
|
||
| __all__ = [ | ||
| "AuthorityCheckResult", | ||
| "AuthorityValidator", | ||
| "DelegationRecord", | ||
| "DelegationRequest", | ||
| "DelegationResult", | ||
| "DelegationService", | ||
| "HierarchyResolver", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| """Authority validation for hierarchical delegation.""" | ||
|
|
||
| from typing import Self | ||
|
|
||
| from pydantic import BaseModel, ConfigDict, Field, model_validator | ||
|
|
||
| from ai_company.communication.config import HierarchyConfig # noqa: TC001 | ||
| from ai_company.communication.delegation.hierarchy import ( # noqa: TC001 | ||
| HierarchyResolver, | ||
| ) | ||
| from ai_company.core.agent import AgentIdentity # noqa: TC001 | ||
| from ai_company.observability import get_logger | ||
| from ai_company.observability.events.delegation import ( | ||
| DELEGATION_AUTHORITY_DENIED, | ||
| DELEGATION_AUTHORIZED, | ||
| ) | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| class AuthorityCheckResult(BaseModel): | ||
| """Result of an authority validation check. | ||
|
|
||
| Attributes: | ||
| allowed: Whether the delegation is authorized. | ||
| reason: Explanation (empty on success). | ||
| """ | ||
|
|
||
| model_config = ConfigDict(frozen=True) | ||
|
|
||
| allowed: bool = Field(description="Whether delegation is allowed") | ||
| reason: str = Field(default="", description="Explanation") | ||
|
|
||
| @model_validator(mode="after") | ||
| def _validate_allowed_reason(self) -> Self: | ||
| """Enforce allowed/reason correlation.""" | ||
| if self.allowed and self.reason: | ||
| msg = "reason must be empty when allowed is True" | ||
| raise ValueError(msg) | ||
| if not self.allowed and not self.reason.strip(): | ||
| msg = "reason is required when allowed is False" | ||
| raise ValueError(msg) | ||
| return self | ||
|
|
||
|
|
||
| class AuthorityValidator: | ||
| """Validates delegation authority using hierarchy and role permissions. | ||
|
|
||
| Checks: | ||
| 1. Hierarchy: delegatee must be a subordinate of delegator | ||
| (direct or skip-level depending on config). | ||
| 2. Roles: if ``delegator.authority.can_delegate_to`` is | ||
| non-empty, ``delegatee.role`` must be in it; if empty, | ||
| all roles are permitted. | ||
|
|
||
| Args: | ||
| hierarchy: Resolved org hierarchy. | ||
| hierarchy_config: Hierarchy enforcement configuration. | ||
| """ | ||
|
|
||
| __slots__ = ("_config", "_hierarchy") | ||
|
|
||
| def __init__( | ||
| self, | ||
| hierarchy: HierarchyResolver, | ||
| hierarchy_config: HierarchyConfig, | ||
| ) -> None: | ||
| self._hierarchy = hierarchy | ||
| self._config = hierarchy_config | ||
|
|
||
| def validate( | ||
| self, | ||
| delegator: AgentIdentity, | ||
| delegatee: AgentIdentity, | ||
| ) -> AuthorityCheckResult: | ||
| """Validate whether delegator can delegate to delegatee. | ||
|
|
||
| Args: | ||
| delegator: Identity of the delegating agent. | ||
| delegatee: Identity of the target agent. | ||
|
|
||
| Returns: | ||
| Result indicating whether delegation is authorized. | ||
| """ | ||
| if self._config.enforce_chain_of_command: | ||
| result = self._check_hierarchy(delegator, delegatee) | ||
| if not result.allowed: | ||
| return result | ||
|
|
||
| result = self._check_role_permissions(delegator, delegatee) | ||
| if not result.allowed: | ||
| return result | ||
|
|
||
| logger.info( | ||
| DELEGATION_AUTHORIZED, | ||
| delegator=delegator.name, | ||
| delegatee=delegatee.name, | ||
| ) | ||
| return AuthorityCheckResult(allowed=True) | ||
|
|
||
| def _check_hierarchy( | ||
| self, | ||
| delegator: AgentIdentity, | ||
| delegatee: AgentIdentity, | ||
| ) -> AuthorityCheckResult: | ||
| """Check hierarchy constraints.""" | ||
| is_direct = self._hierarchy.is_direct_report(delegator.name, delegatee.name) | ||
| if is_direct: | ||
| return AuthorityCheckResult(allowed=True) | ||
|
|
||
| if self._config.allow_skip_level: | ||
| is_sub = self._hierarchy.is_subordinate(delegator.name, delegatee.name) | ||
| if is_sub: | ||
| return AuthorityCheckResult(allowed=True) | ||
|
|
||
| reason = ( | ||
| f"{delegatee.name!r} is not a " | ||
| f"{'subordinate' if self._config.allow_skip_level else 'direct report'} " | ||
| f"of {delegator.name!r}" | ||
| ) | ||
| logger.info( | ||
| DELEGATION_AUTHORITY_DENIED, | ||
| delegator=delegator.name, | ||
| delegatee=delegatee.name, | ||
| reason=reason, | ||
| ) | ||
| return AuthorityCheckResult( | ||
| allowed=False, | ||
| reason=reason, | ||
| ) | ||
|
|
||
| def _check_role_permissions( | ||
| self, | ||
| delegator: AgentIdentity, | ||
| delegatee: AgentIdentity, | ||
| ) -> AuthorityCheckResult: | ||
| """Check role-based delegation permissions.""" | ||
| allowed_roles = delegator.authority.can_delegate_to | ||
| if not allowed_roles: | ||
| return AuthorityCheckResult(allowed=True) | ||
|
|
||
| if delegatee.role in allowed_roles: | ||
| return AuthorityCheckResult(allowed=True) | ||
|
|
||
| reason = ( | ||
| f"Role {delegatee.role!r} is not in " | ||
| f"delegator's can_delegate_to: {allowed_roles}" | ||
| ) | ||
| logger.info( | ||
| DELEGATION_AUTHORITY_DENIED, | ||
| delegator=delegator.name, | ||
| delegatee=delegatee.name, | ||
| reason=reason, | ||
| ) | ||
| return AuthorityCheckResult( | ||
| allowed=False, | ||
| reason=reason, | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.