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
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,11 @@ Preconditions:
Today it answers 200, stores the pointer and turns on
`enable_classify_and_switch_llm_tool` with no meta-profile behind it. The
trap clears the pointer, which turns the switch tool off again.
- **Agent profile that no longer loads (`F18.secret-scope-profile-errors`), known bug.**
- **Agent profile that no longer loads (`F18.secret-scope-profile-errors`).**
Save an agent profile that scopes `QA_F18_TOKEN` (arrange), break its file
so it no longer validates (`secret_refs` a string instead of a list), then
list secrets scoped by its id.
Requires: `F18.secret-put-list-get`
```sh
AP="$AGENT_SERVER_VERIFY_RUN/home/.openhands/agent-profiles"
trap 'rm -f "$AP/qa-f18-broken.json" || true' EXIT
Expand All @@ -772,15 +773,15 @@ Preconditions:
cp "$F18/qa-f18-broken.json" "$AP/qa-f18-broken.json"
control-agent-server api GET /api/agent-profiles/qa-f18-broken --expect 400 \
--check detail contains 'Failed to load profile' --save F18.secret-scope-profile-errors/agent-profile-route
control-agent-server api GET /api/settings/secrets --query "agent_profile_id=$BROKEN" --expect 4xx --save F18.secret-scope-profile-errors/unloadable # bug: status eq 500 && response.body.exception contains 'Failed to load profile'
control-agent-server api GET /api/settings/secrets --query "agent_profile_id=$BROKEN" --expect 400 \
--check detail contains 'Failed to load profile' --save F18.secret-scope-profile-errors/unloadable
```
Scoping works while the profile is valid (one secret), and the
agent-profile route reports the broken file as 400 `Failed to load profile
...`. Expected: the scoped list answers a client error too. Today it is an
unhandled 500 (`exception` carries the validation error): `list_secrets`
calls `AgentProfileStore.load` outside `store_errors()`. The trap removes
the profile file.
- **Agent-profile store busy (`F18.secret-scope-profile-errors`), known bug.**
...`. The scoped list answers the same 400 with the load error in
`detail`: `list_secrets` reads the profile inside `store_errors()`, as the
agent-profile routes do. The trap removes the profile file.
- **Agent-profile store busy (`F18.secret-scope-profile-errors`).**
Another process (here `flock`, standing in for a second server on the same
persistence directory) holds the agent-profile store's lock for 75 seconds.
The server's own 30-second lock timeout is under test, so the bullet waits
Expand All @@ -797,13 +798,11 @@ Preconditions:
control-agent-server api GET /api/agent-profiles/qa-f18-busy --timeout 60 --expect 503 \
--check exception eq '503: Profile store is busy. Please retry.' --save F18.secret-scope-profile-errors/agent-profile-busy
control-agent-server api GET /api/settings/secrets --query agent_profile_id=00000000-0000-4000-8000-0000000000f1 --timeout 60 \
--expect 503 --save F18.secret-scope-profile-errors/secrets-busy # bug: status eq 500 && response.body.exception contains 'lock acquisition timed out'
--expect 503 --check exception eq '503: Profile store is busy. Please retry.' --save F18.secret-scope-profile-errors/secrets-busy
```
After 30 seconds the agent-profile route answers 503 `Profile store is
busy. Please retry.`. Expected: the scoped secrets list answers 503 as
well. Today it waits the same 30 seconds and answers an unhandled 500
(`Agent profile store lock acquisition timed out after 30.0s`). The trap
waits until `flock` lets go.
busy. Please retry.`, and the scoped secrets list answers the same 503
after the same wait. The trap waits until `flock` lets go.
- **Documented status for an invalid name (`F18.secret-name-contract`).**
Read the status the published OpenAPI description promises for an invalid
secret name, then send one.
Expand Down Expand Up @@ -897,12 +896,12 @@ Preconditions:
- Invalid secret names are 422, as the route docstrings, and so the OpenAPI
descriptions, say (`F18.secret-name-contract`). The 404 detail
is always `Secret not found`, also for a stored `null` value.
- `?agent_profile_id=` loads the agent profile outside the store's error
mapping: a profile file that no longer validates, or a lock held by
another process for 30 seconds, is an unhandled 500 there, while the
agent-profile routes answer 400 and 503 (`F18.secret-scope-profile-errors`).
Like every file-locked store route, a held lock also stalls the whole
server for those 30 seconds.
- `?agent_profile_id=` loads the agent profile under the store's error
mapping: a profile file that no longer validates answers 400, and a lock
held by another process for 30 seconds answers 503, as on the
agent-profile routes (`F18.secret-scope-profile-errors`). Like every
file-locked store route, a held lock also stalls the whole server for
those 30 seconds.
- `RemoteWorkspace.clone_repos` runs `git` on the client's machine, not on
the server, and leaves the token in the clone's `remote.origin.url`
(`https://<github_token>@github.com/...`). A local stand-in therefore
Expand Down
11 changes: 7 additions & 4 deletions openhands-agent-server/openhands/agent_server/settings_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,13 @@ async def list_secrets(
allowed_names: set[str] | None = None
if agent_profile_id is not None:
profile_store = get_agent_profile_store()
profile_name = profile_store.name_for_id(agent_profile_id)
if profile_name is None:
raise HTTPException(status_code=404, detail="Agent profile not found")
profile = profile_store.load(profile_name)
# store_errors() answers an unloadable profile (400) and a busy store
# (503) the way the agent-profile routes do.
with store_errors():
profile_name = profile_store.name_for_id(agent_profile_id)
if profile_name is None:
raise HTTPException(status_code=404, detail="Agent profile not found")
profile = profile_store.load(profile_name)
if profile.secret_refs is not None:
allowed_names = set(profile.secret_refs)

Expand Down
48 changes: 48 additions & 0 deletions tests/agent_server/test_settings_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
PERSISTED_SETTINGS_SCHEMA_VERSION,
FileSettingsStore,
PersistedSettings,
get_agent_profile_store,
get_llm_profile_store,
reset_stores,
)
Expand Down Expand Up @@ -1767,6 +1768,53 @@ def test_list_secrets_rejects_unknown_agent_profile(client_with_settings):
assert response.status_code == 404


def test_list_secrets_unloadable_agent_profile_is_400(
client_with_settings, temp_persistence_dir
):
"""A scoping profile that no longer validates answers 400, as the
agent-profile routes do, not an unhandled 500."""
create = client_with_settings.post(
"/api/agent-profiles/broken",
json={"llm_profile_ref": "default", "secret_refs": ["ALLOWED"]},
)
assert create.status_code == 201
profile_id = client_with_settings.get("/api/agent-profiles/broken").json()[
"profile"
]["id"]
path = temp_persistence_dir / "agent-profiles" / "broken.json"
data = json.loads(path.read_text())
data["secret_refs"] = "ALLOWED"
path.write_text(json.dumps(data))
assert client_with_settings.get("/api/agent-profiles/broken").status_code == 400

response = client_with_settings.get(
"/api/settings/secrets", params={"agent_profile_id": profile_id}
)

assert response.status_code == 400
assert "Failed to load profile" in response.json()["detail"]


def test_list_secrets_busy_agent_profile_store_is_503(
client_with_settings, monkeypatch
):
"""A lock timeout on the agent-profile store answers 503, not 500."""
store = get_agent_profile_store()

def _timeout(*_args, **_kwargs):
raise TimeoutError("Agent profile store lock acquisition timed out")

monkeypatch.setattr(store, "name_for_id", _timeout)

response = client_with_settings.get(
"/api/settings/secrets",
params={"agent_profile_id": "00000000-0000-4000-8000-0000000000f1"},
)

assert response.status_code == 503
assert response.json()["exception"] == "503: Profile store is busy. Please retry."


def test_get_secret_value(client_with_settings):
"""GET /api/settings/secrets/{name} returns the raw secret value."""
# Create a secret
Expand Down
Loading