-
Notifications
You must be signed in to change notification settings - Fork 65
[agent] Optimize the agent structure by moving the confirmation logic and risk level assessment down to the security service. #1164
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
Open
CLFutureX
wants to merge
23
commits into
OpenHands:main
Choose a base branch
from
CLFutureX:fix_agent_struct
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
619c0eb
Simplify the agent structure
CLFutureX b907da8
update
CLFutureX f6c4389
update
CLFutureX 9759b25
update
CLFutureX 6c46731
update
CLFutureX 41793bf
update
CLFutureX ba03bad
update
CLFutureX 14a8e71
update
CLFutureX e91af4e
update
CLFutureX 8dae665
update
CLFutureX 0706eb7
update
CLFutureX ec34e68
Merge branch 'main' into fix_agent_struct
CLFutureX 731da9e
update
CLFutureX eecb0b0
update
CLFutureX da5a48a
update
CLFutureX 75924f6
update
CLFutureX a74703d
update
CLFutureX b637061
update
CLFutureX 65b0a1d
update
CLFutureX 178d652
update
CLFutureX 8573491
update
CLFutureX 6c76a1e
update
CLFutureX 5e3ed91
Merge branch 'main' into fix_agent_struct
CLFutureX 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
136 changes: 136 additions & 0 deletions
136
openhands-sdk/openhands/sdk/security/security_service.py
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,136 @@ | ||
| from abc import ABC, abstractmethod | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from openhands.sdk.utils.models import DiscriminatedUnionMixin | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from openhands.sdk.conversation.state import ConversationState | ||
| from groq import BaseModel | ||
|
|
||
| from openhands.sdk.event.llm_convertible.action import ActionEvent | ||
| from openhands.sdk.security import risk | ||
| from openhands.sdk.security.llm_analyzer import LLMSecurityAnalyzer | ||
| from openhands.sdk.tool.builtins.finish import FinishAction | ||
| from openhands.sdk.tool.builtins.think import ThinkAction | ||
|
|
||
|
|
||
| class AccessConfirmRes(BaseModel): | ||
| access_confirm: bool | ||
| security_level: risk.SecurityRisk | None = None | ||
|
|
||
|
|
||
| class SecurityServiceBase(DiscriminatedUnionMixin, ABC): | ||
| """ | ||
| Security service interface defining core security-related methods. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def access_confirm(self, action_events: list[ActionEvent]) -> AccessConfirmRes: | ||
| """ | ||
| Determine whether user confirmation is required to proceed with actions. | ||
|
|
||
| :param action_events: List of pending action events | ||
| :return: AccessConfirm object will never return None: | ||
| - access_confirm: bool = True (needs user confirmation) | ||
| / False (no confirmation needed) | ||
| - security_level: Optional[risk.SecurityRisk] = Security risk | ||
| level of the actions (defaults to the highest risk level among all | ||
| actions; None if no risk)) | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| class DefaultSecurityService(SecurityServiceBase): | ||
| def __init__( | ||
| self, | ||
| state: "ConversationState", | ||
| ): | ||
| self._state = state | ||
|
|
||
| def access_confirm( | ||
| self, | ||
| action_events: list[ActionEvent], | ||
| ) -> AccessConfirmRes: | ||
| """ | ||
| Decide whether user confirmation is needed to proceed. | ||
|
|
||
| Rules: | ||
| 1. Confirmation mode is enabled | ||
| 2. Every action requires confirmation | ||
| 3. A single `FinishAction` never requires confirmation | ||
| 4. A single `ThinkAction` never requires confirmation | ||
| """ | ||
| # If there are no actions there is nothing to confirm | ||
| if len(action_events) == 0: | ||
| return AccessConfirmRes(access_confirm=False) | ||
|
|
||
| if all( | ||
| isinstance(action_event.action, (FinishAction, ThinkAction)) | ||
| for action_event in action_events | ||
| ): | ||
| return AccessConfirmRes(access_confirm=False) | ||
| # If a security analyzer is registered, use it to grab the risks of the actions | ||
| # involved. If not, we'll set the risks to UNKNOWN. | ||
| non_unknown_risks = [] | ||
| if self._state.security_analyzer is not None: | ||
| risks = [ | ||
| r | ||
| for _, r in self._state.security_analyzer.analyze_pending_actions( | ||
| action_events | ||
| ) | ||
| ] | ||
| non_unknown_risks = [r for r in risks if r != risk.SecurityRisk.UNKNOWN] | ||
| else: | ||
| risks = [risk.SecurityRisk.UNKNOWN for _ in action_events] | ||
|
|
||
| access_confirm = any( | ||
| self._state.confirmation_policy.should_confirm(r) for r in risks | ||
| ) | ||
| # Return the highest risk level. | ||
| if non_unknown_risks: | ||
| security_level = max( | ||
| non_unknown_risks, | ||
| key=lambda x: { | ||
| risk.SecurityRisk.LOW: 1, | ||
| risk.SecurityRisk.MEDIUM: 2, | ||
| risk.SecurityRisk.HIGH: 3, | ||
| }[x], | ||
| ) | ||
| else: | ||
| security_level = risk.SecurityRisk.UNKNOWN | ||
|
|
||
| return AccessConfirmRes( | ||
| access_confirm=access_confirm, security_level=security_level | ||
| ) | ||
|
|
||
| def extract_security_risk( | ||
| self, | ||
| arguments: dict, | ||
| tool_name: str, | ||
| read_only_tool: bool, | ||
| ) -> risk.SecurityRisk: | ||
| requires_sr = isinstance(self._state.security_analyzer, LLMSecurityAnalyzer) | ||
| raw = arguments.pop("security_risk", None) | ||
|
|
||
| # Default risk value for action event | ||
| # Tool is marked as read-only so security risk can be ignored | ||
| if read_only_tool: | ||
| return risk.SecurityRisk.UNKNOWN | ||
|
|
||
| # Raises exception if failed to pass risk field when expected | ||
| # Exception will be sent back to agent as error event | ||
| # Strong models like GPT-5 can correct itself by retrying | ||
| if requires_sr and raw is None: | ||
| raise ValueError( | ||
| f"Failed to provide security_risk field in tool '{tool_name}'" | ||
| ) | ||
|
|
||
| # When using weaker models without security analyzer | ||
| # safely ignore missing security risk fields | ||
| if not requires_sr and raw is None: | ||
| return risk.SecurityRisk.UNKNOWN | ||
|
|
||
| # Raises exception if invalid risk enum passed by LLM | ||
| security_risk = risk.SecurityRisk(raw) | ||
| return security_risk | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We seem to have multiple layers for the interface, could we reduce them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, adjustments done!