-
Notifications
You must be signed in to change notification settings - Fork 525
fix(sdk): resolve async response secrets outside the event loop #4967
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
neubig
wants to merge
6
commits into
main
Choose a base branch
from
fix/async-secret-masking
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 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
088dae0
fix(sdk): resolve async response secrets outside the event loop
openhands-agent 8cef037
fix: address masking review regressions
openhands-agent 24679a6
fix: mask secrets registered during asynchronous lookups
openhands-agent bcd7240
Merge remote-tracking branch 'origin/main' into factory/ready-sdk-4967
openhands-agent 6c1e3e5
docs: add live Canvas before and after secret masking evidence
openhands-agent 6d4fe23
chore: Remove PR-only artifacts [automated]
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import asyncio | ||
|
|
||
| import pytest | ||
|
|
||
| from openhands.sdk import Agent, Conversation | ||
| from openhands.sdk.event import ActionEvent, MessageEvent | ||
| from openhands.sdk.llm import Message, TextContent | ||
| from openhands.sdk.llm.message import MessageToolCall | ||
| from openhands.sdk.secret import LookupSecret | ||
| from openhands.sdk.testing import TestLLM | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_response_masks_loopback_lookup_secret_without_blocking(tmp_path): | ||
| requested = asyncio.Event() | ||
|
|
||
| async def serve_secret(reader, writer): | ||
| await reader.readuntil(b"\r\n\r\n") | ||
| requested.set() | ||
| body = b"loopback-secret-value" | ||
| writer.write( | ||
| b"HTTP/1.1 200 OK\r\nContent-Length: " | ||
| + str(len(body)).encode() | ||
| + b"\r\nConnection: close\r\n\r\n" | ||
| + body | ||
| ) | ||
| await writer.drain() | ||
| writer.close() | ||
| await writer.wait_closed() | ||
|
|
||
| server = await asyncio.start_server(serve_secret, "127.0.0.1", 0) | ||
| port = server.sockets[0].getsockname()[1] | ||
| llm = TestLLM.from_messages( | ||
| [ | ||
| Message( | ||
| role="assistant", | ||
| content=[TextContent(text="value loopback-secret-value")], | ||
| ) | ||
| ] | ||
| ) | ||
| conversation = Conversation( | ||
| agent=Agent(llm=llm, tools=[]), | ||
| workspace=str(tmp_path), | ||
| visualizer=None, | ||
| secrets={"TEST_TOKEN": LookupSecret(url=f"http://127.0.0.1:{port}/secret")}, | ||
| ) | ||
| try: | ||
| conversation.send_message("hello") | ||
| await asyncio.wait_for(conversation.arun(), timeout=5) | ||
| assert requested.is_set() | ||
| messages = [ | ||
| event | ||
| for event in conversation.state.events | ||
| if isinstance(event, MessageEvent) and event.source == "agent" | ||
| ] | ||
| assert messages[-1].llm_message.content == [ | ||
| TextContent(text="value <secret-hidden>") | ||
| ] | ||
| finally: | ||
| await asyncio.to_thread(conversation.close) | ||
| server.close() | ||
| await server.wait_closed() | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_async_tool_thought_matches_sync_behavior(tmp_path): | ||
| thoughts = [] | ||
| for use_async in [False, True]: | ||
| message = Message( | ||
| role="assistant", | ||
| content=[TextContent(text="existing tool thought")], | ||
| tool_calls=[ | ||
| MessageToolCall( | ||
| id="finish-call", | ||
| origin="completion", | ||
| name="finish", | ||
| arguments='{"message":"done"}', | ||
| ) | ||
| ], | ||
| ) | ||
| conversation = Conversation( | ||
| agent=Agent(llm=TestLLM.from_messages([message]), tools=[]), | ||
| workspace=str(tmp_path), | ||
| visualizer=None, | ||
| secrets={"TOKEN": "existing tool thought"}, | ||
| ) | ||
| try: | ||
| conversation.send_message("finish") | ||
| if use_async: | ||
| await conversation.arun() | ||
| else: | ||
| await asyncio.to_thread(conversation.run) | ||
| thoughts.append( | ||
| [ | ||
| event.thought | ||
| for event in conversation.state.events | ||
| if isinstance(event, ActionEvent) | ||
| ] | ||
| ) | ||
| finally: | ||
| await asyncio.to_thread(conversation.close) | ||
| assert thoughts[0] | ||
| assert thoughts[0] == thoughts[1] |
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.