Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .agents/skills/verify-sdk/programs/S21/acp_conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,8 @@ async def cancel_mid_prompt() -> str:
conversation.send_message("[cancel] build everything")
task = asyncio.create_task(conversation.arun())
deadline = time.monotonic() + 30
while not journal("cancel_waiting") and time.monotonic() < deadline:
# The fake ACP server only writes a journal file; nothing to await.
while not journal("cancel_waiting") and time.monotonic() < deadline: # noqa: ASYNC110
await asyncio.sleep(0.1)
await asyncio.sleep(0.3)
task.cancel()
Expand Down
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ repos:
types: [python]
pass_filenames: true
always_run: false
- id: check-async-store-access
name: Keep blocking store I/O off the agent-server event loop
entry: uv
args: [run, python, scripts/check_async_store_access.py]
language: system
files: ^openhands-agent-server/.*\.py$
pass_filenames: true
always_run: false
- id: check-secret-fields
name: Keep secrets on SecretField
entry: uv
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
FileSettingsStore,
PersistedSettings,
ServerStores,
run_store,
)
from openhands.agent_server.profiles_router import MAX_PROFILES, _has_api_key
from openhands.sdk.errors import Conflict, NotFound, OperationFailed, Unprocessable
Expand Down Expand Up @@ -582,11 +583,11 @@ async def materialize_agent_profile(
store = stores.agent_profiles
try:
# Off the event loop: the store lock wait blocks (up to 30 s).
profile = await asyncio.to_thread(store.load, name)
profile = await run_store(store.load, name)
except FileNotFoundError:
raise NotFound(f"Agent profile '{name}' not found")

settings = stores.settings.load() or PersistedSettings()
settings = await run_store(stores.settings.load) or PersistedSettings()
context = settings.agent_settings.agent_context
return await asyncio.to_thread(
lambda: preview_launch(
Expand Down
4 changes: 2 additions & 2 deletions openhands-agent-server/openhands/agent_server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
check_openai_api_key,
openai_router,
)
from openhands.agent_server.persistence import reencrypt_stale_stores
from openhands.agent_server.persistence import reencrypt_stale_stores, run_store
from openhands.agent_server.plugins_router import plugins_router
from openhands.agent_server.profiles_router import profiles_router
from openhands.agent_server.provider_connections_router import (
Expand Down Expand Up @@ -419,7 +419,7 @@ async def stop_tool_preload_service():

# Non-deferred (legacy) path: build and enter the conversation
# service as part of the lifespan, exactly as before.
await asyncio.to_thread(reencrypt_stale_stores, context.stores)
await run_store(reencrypt_stale_stores, context.stores)
#
# A key that came from the config file is visible only in ``config``;
# in-process callers of this server read it from the environment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ async def prepare(self, name: str, revision: str) -> BackendStatus:
if (
prepared is not None
and prepared.revision == revision
and Path(prepared.artifact_dir).is_dir()
and await asyncio.to_thread(Path(prepared.artifact_dir).is_dir)
):
return self._status_unlocked(name)

Expand Down Expand Up @@ -790,19 +790,20 @@ async def start(self, name: str, revision: str) -> BackendStatus:
if prepared is None or prepared.revision != revision:
raise RuntimeError("backend revision must be prepared before start")
artifact_dir = Path(prepared.artifact_dir)
if not artifact_dir.is_dir():
if not await asyncio.to_thread(artifact_dir.is_dir):
raise RuntimeError("prepared backend artifact is missing")

parts = self._backend_parts(name)
assert parts is not None
backend, artifact, current_platform = parts
expected_artifact_dir = (
self.artifacts_dir / name / prepared.artifact_sha256
).resolve()
expected_artifact_dir = await asyncio.to_thread(
(self.artifacts_dir / name / prepared.artifact_sha256).resolve
)
if (
prepared.platform != current_platform
or prepared.artifact_sha256 != artifact.sha256
or artifact_dir.resolve() != expected_artifact_dir
or await asyncio.to_thread(artifact_dir.resolve)
!= expected_artifact_dir
):
raise RuntimeError(
"prepared backend metadata does not match manifest"
Expand Down Expand Up @@ -918,8 +919,12 @@ def _runtime_group_alive(self, runtime: _Runtime) -> bool:
process.returncode is None or self._group_alive(process.pid)
)

async def _wait_for_group_exit(self, pgid: int, timeout: float) -> bool:
deadline = time.monotonic() + timeout
async def _wait_for_group_exit(self, pgid: int) -> bool:
"""Whether the group exits within ``_STOP_TIMEOUT_SECONDS``.

Nothing signals a process group's exit, so this polls.
"""
deadline = time.monotonic() + _STOP_TIMEOUT_SECONDS
while True:
if not self._group_alive(pgid):
return True
Expand All @@ -938,14 +943,14 @@ async def _terminate_runtime(self, runtime: _Runtime) -> None:
# on the leader's exit code would leak those descendants.
if process.returncode is None:
self._signal_group(pgid, signal.SIGTERM)
if not await self._wait_for_group_exit(pgid, _STOP_TIMEOUT_SECONDS):
if not await self._wait_for_group_exit(pgid):
self._signal_group(pgid, signal.SIGKILL)
await self._wait_for_group_exit(pgid, _STOP_TIMEOUT_SECONDS)
await self._wait_for_group_exit(pgid)
elif self._group_alive(pgid):
# With no leader left to shut down cleanly, its descendants are
# orphaned; do not let one hold status or restart for the grace period.
self._signal_group(pgid, signal.SIGKILL)
await self._wait_for_group_exit(pgid, _STOP_TIMEOUT_SECONDS)
await self._wait_for_group_exit(pgid)
if process.returncode is None:
await process.wait()
await self._finish_runtime(runtime)
Expand Down Expand Up @@ -1004,11 +1009,9 @@ async def reap_orphans(self) -> None:
continue # Its server is still running and owns it.
if self._is_recorded_backend(record):
self._signal_group(record.pgid, signal.SIGTERM)
if not await self._wait_for_group_exit(
record.pgid, _STOP_TIMEOUT_SECONDS
):
if not await self._wait_for_group_exit(record.pgid):
self._signal_group(record.pgid, signal.SIGKILL)
await self._wait_for_group_exit(record.pgid, _STOP_TIMEOUT_SECONDS)
await self._wait_for_group_exit(record.pgid)
path.unlink(missing_ok=True)

def has_running_backends(self) -> bool:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
UpdateSecretsRequest,
trim_conversation_response_skills,
)
from openhands.agent_server.persistence import run_store
from openhands.sdk import LLM
from openhands.sdk.conversation.state import ConversationExecutionStatus
from openhands.sdk.conversation.types import (
Expand Down Expand Up @@ -645,7 +646,9 @@ async def switch_conversation_llm(
# reference or missing credential surfaces as 422 before the working LLM
# is replaced. Inline-key configs without a provider_connection_id are
# byte-identical to the old path.
llm = llm_profile_store.resolve_provider_connection(llm, cipher=cipher)
llm = await run_store(
llm_profile_store.resolve_provider_connection, llm, cipher=cipher
)
try:
conversation.switch_llm(llm)
except ValueError as e:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@
StoredConversation,
UpdateConversationRequest,
)
from openhands.agent_server.persistence import PersistedSettings, ServerStores
from openhands.agent_server.persistence import (
PersistedSettings,
ServerStores,
run_store,
)
from openhands.agent_server.pub_sub import Subscriber
from openhands.agent_server.server_details_router import update_last_execution_time
from openhands.agent_server.telemetry import (
Expand Down Expand Up @@ -812,10 +816,7 @@ def _is_codex_agent(agent: AgentBase | None) -> bool:
async def _has_local_codex_credential(self) -> bool:
if self.stores is None:
return False
value = await asyncio.to_thread(
self.stores.secrets.get_secret,
CODEX_AUTH_SECRET_NAME,
)
value = await run_store(self.stores.secrets.get_secret, CODEX_AUTH_SECRET_NAME)
return value is not None

async def _load_settings(self) -> PersistedSettings:
Expand All @@ -827,7 +828,7 @@ async def _load_settings(self) -> PersistedSettings:
# every launch reads it, so a bad file mode must not take down request
# shapes that need nothing from settings.
try:
settings = await asyncio.to_thread(stores.settings.load)
settings = await run_store(stores.settings.load)
except (PermissionError, OSError):
logger.warning(
"Cannot read settings; starting without the stored agent preferences",
Expand Down Expand Up @@ -2822,7 +2823,7 @@ async def __call__(self, event: Event) -> None:
# truncation. This keeps auto-titling non-breaking for consumers who
# don't configure title_llm_profile.
conversation = self.service._conversation
title_llm = self._load_title_llm()
title_llm = await run_store(self._load_title_llm)
if title_llm is None:
title_llm = conversation.agent.llm if conversation else None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from openhands.agent_server.conversation_service import ConversationService
from openhands.agent_server.dependencies import get_conversation_service
from openhands.agent_server.event_service import CredentialBindingActivationTooLate
from openhands.agent_server.persistence import FileSecretsStore
from openhands.agent_server.persistence import FileSecretsStore, run_store
from openhands.sdk.agent.acp_file_credentials import supports_file_credential_binding
from openhands.sdk.credential import (
CredentialAuthorizationRejected,
Expand Down Expand Up @@ -58,7 +58,7 @@ class LocalVersionedCredentialBinding:

async def load(self) -> ResolvedCredential:
try:
value, version = await asyncio.to_thread(
value, version = await run_store(
self.store.load_versioned_secret,
self.secret_name,
)
Expand All @@ -72,7 +72,7 @@ async def load(self) -> ResolvedCredential:

async def replace(self, expected_version: str, value: str) -> str:
try:
return await asyncio.to_thread(
return await run_store(
self.store.replace_versioned_secret,
self.secret_name,
expected_version,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async def proxy_http(
workspace: ProxyTarget,
*,
upstream_path: str,
timeout: float | None = None,
read_timeout: float | None = None,
body: bytes | None = None,
on_close: Callable[[], Any] | None = None,
reject_redirects: bool = False,
Expand All @@ -108,8 +108,8 @@ async def proxy_http(
agent-server to forward to. Typically the same path the outer
server received, since the inner agent-server exposes the same
API surface.
timeout: Per-request timeout in seconds. ``None`` (the default) means
no read timeout — conversation event streams can be long-lived.
read_timeout: Upstream read timeout in seconds. ``None`` (the default)
means no read timeout — conversation event streams can be long-lived.
body: Replacement request body. By default the incoming body is streamed.
on_close: Awaited or invoked once the streamed response is fully consumed,
client disconnects, or connection fails. Callers use this to release
Expand Down Expand Up @@ -157,7 +157,9 @@ async def _request_body() -> AsyncIterator[bytes]:
try:
client = await stack.enter_async_context(
httpx.AsyncClient(
timeout=httpx.Timeout(connect=10.0, read=timeout, write=30.0, pool=10.0)
timeout=httpx.Timeout(
connect=10.0, read=read_timeout, write=30.0, pool=10.0
)
)
)
upstream = await stack.enter_async_context(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
StartConversationRequest,
UpdateSecretsRequest,
)
from openhands.agent_server.persistence import PersistedSettings
from openhands.agent_server.persistence import PersistedSettings, run_store
from openhands.agent_server.utils import safe_rmtree
from openhands.sdk.conversation.request import ConversationContainerOptions
from openhands.sdk.errors import (
Expand Down Expand Up @@ -147,6 +147,32 @@ async def detach() -> None:
)


def _worktree_git_paths(workspace_path: Path) -> dict[str, Path]:
"""The repository and git common dir a worktree start records, if any."""
try:
validate_git_repository(workspace_path)
repo_root = Path(
run_git_command(
["git", "--no-pager", "rev-parse", "--show-toplevel"],
workspace_path,
)
).resolve()
git_common_dir = Path(
run_git_command(
["git", "--no-pager", "rev-parse", "--git-common-dir"],
workspace_path,
)
)
except (GitCommandError, GitRepositoryError):
return {}
if not git_common_dir.is_absolute():
git_common_dir = workspace_path / git_common_dir
return {
"worktree_repository_path": repo_root,
"worktree_git_common_dir_path": git_common_dir.resolve(),
}


@docker_conversation_router.post("")
async def start_conversation(
request: Request,
Expand Down Expand Up @@ -212,33 +238,14 @@ async def start_docker_conversation(
payload, launched, options, worktree = await _prepare_forward(
body, registry, existing=existing
)
identity = registry.provisioning.create(conversation_id, host_workspace)
identity = await run_store(
registry.provisioning.create, conversation_id, host_workspace
)
updates: dict[str, Any] = {}
if worktree:
try:
validate_git_repository(identity.workspace_path)
repo_root = Path(
run_git_command(
["git", "--no-pager", "rev-parse", "--show-toplevel"],
identity.workspace_path,
)
).resolve()
git_common_dir = Path(
run_git_command(
["git", "--no-pager", "rev-parse", "--git-common-dir"],
identity.workspace_path,
)
)
if not git_common_dir.is_absolute():
git_common_dir = identity.workspace_path / git_common_dir
git_common_dir = git_common_dir.resolve()
except (GitCommandError, GitRepositoryError):
pass
else:
updates.update(
worktree_repository_path=repo_root,
worktree_git_common_dir_path=git_common_dir,
)
updates.update(
await asyncio.to_thread(_worktree_git_paths, identity.workspace_path)
)
if launched is not None and identity.launched_agent_profile is None:
updates["launched_agent_profile"] = launched
# Fixed once the container accepted the conversation; until then a
Expand All @@ -247,7 +254,7 @@ async def start_docker_conversation(
updates["container"] = options
if updates:
identity = identity.model_copy(update=updates)
registry.provisioning.save(identity)
await run_store(registry.provisioning.save, identity)
container = await registry.get_or_create(conversation_id)
release = registry.acquire_lease(conversation_id)
forwarded = await asyncio.to_thread(payload, identity.cipher)
Expand Down Expand Up @@ -310,7 +317,7 @@ async def _prepare_forward(
),
)
try:
settings = await asyncio.to_thread(registry.stores.settings.load)
settings = await run_store(registry.stores.settings.load)
except (OSError, PermissionError):
settings = None
settings = settings or PersistedSettings()
Expand Down Expand Up @@ -359,7 +366,7 @@ async def runtime_credentials(
registry = get_registry(conversation_registry)
if not registry.conversation_dir(conversation_id).joinpath("meta.json").is_file():
raise NotFound("Conversation not found")
identity = registry.provisioning.load(conversation_id)
identity = await run_store(registry.provisioning.load, conversation_id)
return JSONResponse({"session_api_key": identity.api_key.get_secret_value()})


Expand Down Expand Up @@ -421,9 +428,10 @@ async def delete_conversation(
logger.exception(
"Could not clean up conversation worktree %s", conversation_id
)
await asyncio.to_thread(
safe_rmtree, registry.provisioning.runtime_dir(conversation_id)
runtime_dir = await run_store(
registry.provisioning.runtime_dir, conversation_id
)
await asyncio.to_thread(safe_rmtree, runtime_dir)
await asyncio.to_thread(safe_rmtree, registry.conversation_dir(conversation_id))
registry.provisioning.manifest_path(conversation_id).unlink(missing_ok=True)
registry.archived_marker(conversation_id).unlink(missing_ok=True)
Expand Down Expand Up @@ -481,7 +489,8 @@ async def proxy_conversation(
update = UpdateSecretsRequest.model_validate_json(await request.body())
except ValueError as exc:
raise Unprocessable("Invalid secrets payload") from exc
profile = registry.provisioning.load(conversation_id).launched_agent_profile
identity = await run_store(registry.provisioning.load, conversation_id)
profile = identity.launched_agent_profile
secrets = update.secrets
if profile is not None:
secrets = {
Expand Down
Loading
Loading