-
Notifications
You must be signed in to change notification settings - Fork 217
fix: send hook_config to server in RemoteConversation #2115
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 8 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e286b4b
fix: send hook_config to server in RemoteConversation
xingyaoww 80ba8c6
test: add test for hook_config being sent to server
xingyaoww a790935
feat: Add HookExecutionEvent for hook observability
xingyaoww 8acdc2f
Update examples/02_remote_agent_server/01_convo_with_local_agent_serv…
xingyaoww 16caa43
Apply suggestion from @xingyaoww
xingyaoww a8eae38
feat: Add hook_config to ConversationState and SystemPromptEvent
xingyaoww 2444bd1
test: add stop hook and verify HookExecutionEvent for both hooks
openhands-agent 9990f4e
Merge branch 'main' into fix/remote-conversation-hook-config
xingyaoww ecacb88
fix: avoid circular import when rebuilding SystemPromptEvent
openhands-agent 008a9d7
fix: truncate HookExecutionEvent logs to 50k chars
openhands-agent 988b89b
docs: fix RemoteConversation PostToolUse hook comment
openhands-agent 071702b
test: update example to demonstrate on_stop hook with syntax validation
xingyaoww 69f37a7
test: fix agent instruction to demonstrate full on_stop hook cycle
xingyaoww fd8a2af
Merge main into fix/remote-conversation-hook-config
xingyaoww 32c781e
Add example run log after merge from main
xingyaoww 4c14b6e
Add JSON mode example run log
xingyaoww cfc7445
Rename pre_commit_check.sh to pycompile_check.sh
xingyaoww 70ebee0
Remove old log files from .pr folder
xingyaoww ce7f1e9
Merge branch 'main' into fix/remote-conversation-hook-config
xingyaoww e346ea2
fix: make all hooks server-only in RemoteConversation
openhands-agent ee3a7c0
refactor: remove hook_config from SystemPromptEvent
openhands-agent aa0129f
chore: Remove PR-only artifacts [automated]
e68d1ee
Merge branch 'main' into fix/remote-conversation-hook-config
xingyaoww 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
Some comments aren't visible on the classic Files Changed page.
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
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,129 @@ | ||
| """Hook execution event for observability into hook execution.""" | ||
|
|
||
| from typing import Any, Literal | ||
|
|
||
| from pydantic import Field | ||
| from rich.text import Text | ||
|
|
||
| from openhands.sdk.event.base import Event | ||
| from openhands.sdk.event.types import SourceType | ||
|
|
||
|
|
||
| HookEventType = Literal[ | ||
| "PreToolUse", | ||
| "PostToolUse", | ||
| "UserPromptSubmit", | ||
| "SessionStart", | ||
| "SessionEnd", | ||
| "Stop", | ||
| ] | ||
|
|
||
|
|
||
| class HookExecutionEvent(Event): | ||
| """Event emitted when a hook is executed. | ||
|
|
||
| This event provides observability into hook execution, including: | ||
| - Which hook type was triggered | ||
| - The command that was run | ||
| - The result (success/blocked/error) | ||
| - Any output from the hook | ||
|
|
||
| This allows clients to track hook execution via the event stream. | ||
| """ | ||
|
|
||
| source: SourceType = Field( | ||
| default="hook", description="Source is always 'hook' for hook execution events" | ||
| ) | ||
|
|
||
| # Hook identification | ||
| hook_event_type: HookEventType = Field( | ||
| ..., description="The type of hook event that triggered this execution" | ||
| ) | ||
| hook_command: str = Field(..., description="The hook command that was executed") | ||
| tool_name: str | None = Field( | ||
| default=None, | ||
| description="Tool name for PreToolUse/PostToolUse hooks", | ||
| ) | ||
|
|
||
| # Execution result | ||
| success: bool = Field(..., description="Whether the hook executed successfully") | ||
| blocked: bool = Field( | ||
| default=False, | ||
| description="Whether the hook blocked the operation (exit code 2 or deny)", | ||
| ) | ||
| exit_code: int = Field(..., description="Exit code from the hook command") | ||
|
|
||
| # Output | ||
| stdout: str = Field(default="", description="Standard output from the hook") | ||
| stderr: str = Field(default="", description="Standard error from the hook") | ||
| reason: str | None = Field( | ||
| default=None, description="Reason provided by hook (for blocking)" | ||
| ) | ||
| additional_context: str | None = Field( | ||
| default=None, | ||
| description="Additional context injected by hook (e.g., for UserPromptSubmit)", | ||
| ) | ||
| error: str | None = Field( | ||
| default=None, description="Error message if hook execution failed" | ||
| ) | ||
|
|
||
| # Context | ||
| action_id: str | None = Field( | ||
| default=None, | ||
| description="ID of the action this hook is associated with (PreToolUse/PostToolUse)", # noqa: E501 | ||
| ) | ||
| message_id: str | None = Field( | ||
| default=None, | ||
| description="ID of the message this hook is associated with (UserPromptSubmit)", | ||
| ) | ||
| hook_input: dict[str, Any] | None = Field( | ||
| default=None, | ||
| description="The input data that was passed to the hook", | ||
| ) | ||
|
|
||
| @property | ||
| def visualize(self) -> Text: | ||
| """Return Rich Text representation of this hook execution event.""" | ||
| content = Text() | ||
| content.append("Hook: ", style="bold") | ||
| content.append(f"{self.hook_event_type}") | ||
| if self.tool_name: | ||
| content.append(f" ({self.tool_name})") | ||
| content.append("\n") | ||
|
|
||
| # Status | ||
| if self.blocked: | ||
| content.append("Status: ", style="bold") | ||
| content.append("BLOCKED", style="bold red") | ||
| if self.reason: | ||
| content.append(f" - {self.reason}") | ||
| elif self.success: | ||
| content.append("Status: ", style="bold") | ||
| content.append("SUCCESS", style="bold green") | ||
| else: | ||
| content.append("Status: ", style="bold") | ||
| content.append("FAILED", style="bold red") | ||
| if self.error: | ||
| content.append(f" - {self.error}") | ||
|
|
||
| content.append(f"\nExit Code: {self.exit_code}") | ||
|
|
||
| # Output (truncated) | ||
| if self.stdout: | ||
| output_preview = self.stdout[:200] | ||
| if len(self.stdout) > 200: | ||
| output_preview += "..." | ||
| content.append(f"\nOutput: {output_preview}") | ||
|
|
||
| if self.additional_context: | ||
| content.append(f"\nInjected Context: {self.additional_context[:100]}...") | ||
|
|
||
| return content | ||
|
|
||
| def __str__(self) -> str: | ||
| """Plain text string representation for HookExecutionEvent.""" | ||
| status = ( | ||
| "BLOCKED" if self.blocked else ("SUCCESS" if self.success else "FAILED") | ||
| ) | ||
| tool_info = f" ({self.tool_name})" if self.tool_name else "" | ||
| return f"HookExecutionEvent: {self.hook_event_type}{tool_info} - {status}" |
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.
🟡 Suggestion: This example has grown significantly in complexity. The original showed basic RemoteConversation usage (~50 lines). Now it's a comprehensive hook testing harness (~350 lines) that deliberately creates broken Python files to test stop hook denial and retry logic.
For a "01_" introductory example, consider splitting this into:
01_convo_with_local_agent_server.py- basic usage (~50 lines)02_hooks_with_stop_validation.py- comprehensive hook testing (current content)The comprehensive demonstration is valuable for showing hook capabilities, but might be better as a separate, clearly-labeled advanced example.
Not blocking - this is an organizational suggestion to improve discoverability for users learning the basics.