Repository navigation
fix(app-conversation): list the /workspace root in the Files endpoint - #684
jpshackelford wants to merge 1 commit into
Conversation
Intentionally not fixed in this PRTwo problems exist in the files endpoint today and are not fixed here. Neither is made worse by this PR; the table says what this PR changes for each case, and the sections below have what is wrong, a way to reproduce it, and what a fix should do, so they can be filed as follow-on issues.
What "wider" means for grouping on, no repo. The old listing already showed the other conversations' What the testing on
1. Conversations with sandbox grouping enabled resolve to the wrong directoryWhat is wrong. When the user's Effect.
Reproduce offline (no instance needed). Run from an from types import SimpleNamespace
from uuid import uuid4
from openhands.app_server.app_conversation.app_conversation_router import _resolve_workspace_dir
from openhands.app_server.app_conversation.app_conversation_service_base import get_project_dir
from openhands.app_server.settings.settings_models import SandboxGroupingStrategy, grouped_workspace_dir
SPEC_WORKING_DIR = '/workspace/project'
conv_id = uuid4()
for strategy in (SandboxGroupingStrategy.NO_GROUPING, SandboxGroupingStrategy.GROUP_BY_NEWEST):
for repo in (None, 'OpenHands/software-agent-sdk'):
actual = get_project_dir(grouped_workspace_dir(SPEC_WORKING_DIR, strategy, conv_id.hex), repo)
ctx = SimpleNamespace(
sandbox_spec=SimpleNamespace(working_dir=SPEC_WORKING_DIR),
conversation=SimpleNamespace(selected_repository=repo),
)
listed = _resolve_workspace_dir(None, ctx)
flag = 'OK ' if actual == listed else 'MISMATCH'
print(f'{flag} {strategy.value:15} repo={str(repo):30} actual={actual} listed={listed}')Output: Not reproduced on a live instance. The live steps are: set What a fix should do. Stop re-deriving the directory. Either use the conversation's real working dir (the agent server returns it as 2. A failed listing is returned as an empty listWhat is wrong. Reproduce on a live instance (verified twice). First on a staging Enterprise instance running agent-server 1.50.1, without this PR, on a conversation whose # $CLOUD = backend host, $ID = conversation id, $KEY = API key
curl -s -w ' (HTTP %{http_code})\n' -H "Authorization: Bearer $KEY" "$CLOUD/api/v1/app-conversations/$ID/files?path=/workspace/project"
curl -s -w ' (HTTP %{http_code})\n' -H "Authorization: Bearer $KEY" "$CLOUD/api/v1/app-conversations/$ID/files?path=/workspace/project/does-not-exist"Both print What a fix should do. Return a distinguishable result when the command fails, for example a 404 for a missing directory and a 5xx (or an explicit error field) for any other non-zero exit, and have the Files tab show an error or "workspace not found" state instead of the empty state. Acceptance: the two requests above return different responses, and the UI shows different text for them. The user-facing bug, the UI change and the merge order are in OpenHands/OpenHands#18108 and OpenHands/OpenHands#18109. This comment was drafted by an AI agent on behalf of the user. |
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
Tested on a fleet VMThis PR's image ( All calls below use No repo selectedAn agent in a no-repo conversation ran
The Repo selected (
|
| Request | Result |
|---|---|
files?path=/workspace/project |
["README"] |
files?path=/workspace/project/Hello-World |
["README"] |
files?path=/workspace |
["outside-repo.txt","project/Hello-World/README"] |
The clone is at /workspace/project/Hello-World and the conversation carries archiveworkspacepath=/workspace/project, which is the ungrouped layout this endpoint assumes. I did not compare the repo-selected requests against an unpatched backend. The statement that they are unchanged rests on the code path: the paths the current UI sends resolve to the same directory as before.
Failed listings
| Conversation | Request | Result |
|---|---|---|
| no repo | files?path=/workspace/project/does-not-exist |
[] (HTTP 200) |
| repo selected | files?path=/workspace/project/Hello-World/does-not-exist |
[] (HTTP 200) |
A missing directory inside the project dir returns the same [] as an empty one, as described in the second item of the deferred-items comment. Paths outside the project dir are not errors either: files?path=/workspace/nonexistent falls back to the project dir ([] with no repo, ["README"] with a repo), so it does not exercise the failure path.
UI
OpenHands/OpenHands#18109 (c13d47a) was run locally against this backend. With no repo the Files tab lists repro-demo and top.txt and opens hello.txt; with the repo it shows only the clone's README, which opens. Screenshots are on that PR.
Not tested
- The merge-order failure described at the top of this PR (new UI against an old backend). I did not reproduce it.
- Any
sandbox_grouping_strategyother thanNO_GROUPING. See the deferred-items comment. - A repo-selected conversation on an unpatched backend, for a direct before/after comparison.
This comment was drafted by an AI agent on behalf of the user.
hieptl
left a comment
There was a problem hiding this comment.
Hello @jpshackelford,
Thank you for the thorough write-up and for testing this on shared-6. The results made the change easy to follow. The overall approach looks good to me: a single, explicit /workspace exception, pruning the agent-server state dirs only at the top level, and no change for the paths today's UI sends. I also ran the new find command against a scratch tree, and it prunes conversations/, bash_events/ and worktrees/ only at the top level while keeping a nested src/conversations/, as intended.
I've marked two items as blocking, though I'm happy to discuss either of them:
- Limiting the
/workspaceexception to conversations without a repo, since reads don't round-trip when a repo is selected. - Normalizing
pathso..can't get past the guard or the new pruning, with test cases to match.
The remaining comments are non-blocking suggestions, so please feel free to address them here or in a follow-up, whichever you prefer.
On the open question in your deferred-items comment: grouped sandboxes are only reused across the same user's conversations (_find_running_sandbox_for_user filters on sandbox.created_by_user_id == user_id), so the wider /workspace listing shouldn't expose another user's files.
Thank you very much 🙏
| # ancestor of the project dir, so agents that write outside the project dir | ||
| # (e.g. clone straight into ``/workspace``) still show up. Only the root | ||
| # itself is accepted, never an arbitrary sibling. | ||
| if path.rstrip('/') == _WORKSPACE_ROOT and _is_within(project_dir, _WORKSPACE_ROOT): |
There was a problem hiding this comment.
Hello @jpshackelford,
Blocking: I noticed this also accepts /workspace when a repository is selected, and the shared-6 run shows the listing working (["outside-repo.txt","project/Hello-World/README"]). Reading some of those entries back may not work as expected, though. _resolve_file_path still treats working_dir (/workspace/project) as a stale root, so a listed file under /workspace/project but outside the clone gets re-anchored into the clone:
/workspace/project/notes.mdis read as/workspace/project/Hello-World/notes.md, so the result is empty./workspace/project/READMEis read as/workspace/project/Hello-World/README, so the tab would show the repo's README for a different file.
The second case concerns me a bit more, since it returns different content with no error. The companion UI only sends /workspace when there's no repo, so nothing should hit this today. Since the endpoint allows it, though, would it make sense to limit the exception to conversations without a selected repository? That way the listing never returns paths the read endpoint can't serve:
if (
path.rstrip('/') == _WORKSPACE_ROOT
and not ctx.conversation.selected_repository
and _is_within(project_dir, _WORKSPACE_ROOT)
):
return _WORKSPACE_ROOTA test for the repo case (expecting the clone dir) would help keep it that way. If you'd prefer to keep /workspace open for repo conversations, I think _resolve_file_path would first need a way to tell a real /workspace/project/... path apart from the stale frontend root. Happy to talk through either option.
Thank you very much 🙏
There was a problem hiding this comment.
Done in d0da885. /workspace is now only honored when the conversation has no selected repository (and no repository override), using your suggested condition. With a repo it falls back to the clone dir. Added test_workspace_root_is_not_accepted_when_repository_is_selected.
This reply was written by an AI agent (OpenHands) on behalf of @jpshackelford.
| base = project_dir.rstrip('/') | ||
| normalized = path.rstrip('/') | ||
| if normalized == base or normalized.startswith(base.rstrip('/') + '/'): | ||
| if _is_within(path, project_dir): |
There was a problem hiding this comment.
Hello @jpshackelford,
Blocking: would it be possible to normalize path before this check? It's currently a plain string-prefix match, so anything that starts with the project dir is returned as-is and becomes the agent server's cwd, which isn't normalized on that side either:
/workspace/project/..is accepted and lists/workspace. Becausecwdisn't literally/workspace, the newconversations//bash_events//worktrees/pruning is skipped./workspace/project/../../etcis accepted and lists/etc.
Since it's the caller's own sandbox, I don't think this is a cross-user issue. It does, however, weaken the "never lists outside the workspace" guarantee in the docstring and lets requests skip the pruning this PR adds. A single line at the top should address both, and it keeps the cwd.rstrip('/') == _WORKSPACE_ROOT check in list_conversation_files reliable:
if not path:
return project_dir
path = posixpath.normpath(path) # sandbox paths are always POSIXWith that change, /workspace/project/.. resolves to /workspace (pruned), /workspace/project/../../etc falls back to the project dir, and trailing slashes are dropped. As far as I can tell, none of the existing tests rely on the raw string being echoed back.
Thank you very much 🙏
There was a problem hiding this comment.
Done. path is now passed through posixpath.normpath right after the empty check. /workspace/project/.. resolves to /workspace (pruned), /workspace/project/../../etc falls back to the project dir, and /workspace/ is accepted via the normalized form.
This reply was written by an AI agent (OpenHands) on behalf of @jpshackelford.
| uuid4(), working_dir='/workspace/project', selected_repository=None | ||
| ) | ||
|
|
||
| for bad in ('/workspace/other', '/', '/workspace/../etc'): |
There was a problem hiding this comment.
Hello @jpshackelford,
Blocking (pairs with the normalization fix): it looks like /workspace/../etc is rejected only because it doesn't start with /workspace/project/, so this loop doesn't quite exercise the .. handling. Could we add /workspace/project/../../etc here? It's currently accepted and lists /etc. A separate assertion for /workspace/project/.. would also be useful: once the path is normalized, it should resolve to /workspace with the state dirs pruned. Today it lists the workspace root without pruning (I've added more detail in my comment on _resolve_workspace_dir).
Thank you very much 🙏
There was a problem hiding this comment.
Added /workspace/project/../../etc to the rejected paths, plus test_dotdot_to_workspace_root_is_normalized_and_pruned (/workspace/project/.. -> cwd /workspace with conversations pruned) and a trailing-slash case.
This reply was written by an AI agent (OpenHands) on behalf of @jpshackelford.
| stale_roots.append(f'{working_dir}/project/{repo_name}') | ||
| stale_roots.append(f'{working_dir}/project') | ||
| stale_roots.append(working_dir) | ||
| if not _is_within(project_dir, _WORKSPACE_ROOT): |
There was a problem hiding this comment.
Hello @jpshackelford,
Non-blocking: this nicely fixes the no-repo case for specs outside /workspace, though I think the repo case on those specs still doesn't resolve correctly. The UI requests /workspace/project/<repo>, which falls back to the clone, so the listing is relative to the clone. The read of /workspace/project/<repo>/a.py then matches /workspace here and becomes {project_dir}/project/<repo>/a.py, for example /home/openhands/workspace/r/project/r/a.py for repo o/r.
Adding /workspace/project[/<repo>] as stale roots would fix that case, but it would break a no-repo project that really has a project/ dir, since /workspace/project/x.py would then be stripped to {project_dir}/x.py. The stale-root list has to guess which root the listing used, and I couldn't find a combination that is always right. I suspect the same guessing is behind the merge-order caveat in the description. Longer term, it might be cleaner for list_conversation_files to report the directory it actually listed, for example in an X-Workspace-Root response header, which keeps the list[str] body unchanged. The UI could then build read paths from it. This read also failed before this PR, so a follow-up issue next to the two you already listed seems perfectly fine.
Thank you very much 🙏
There was a problem hiding this comment.
Agreed, and thanks for working through the stale-root options. Leaving this out of this PR: it is the same root-guessing problem as the merge-order caveat, and the read failed before this change too. Reporting the listed directory (e.g. an X-Workspace-Root header) looks like the right follow-up issue.
This reply was written by an AI agent (OpenHands) on behalf of @jpshackelford.
| # Agent-server bookkeeping that lives beside the project dir (see the sandbox | ||
| # ``/api/init`` bodies). It is runtime state, not user files, so it is pruned | ||
| # when the whole workspace root is listed. | ||
| _WORKSPACE_ROOT_STATE_DIRS = ('conversations', 'bash_events', 'worktrees') |
There was a problem hiding this comment.
Hello @jpshackelford,
Non-blocking: these three names duplicate the paths the sandbox services set up. In E2B and K8s they come from {workspace_dir}/conversations|bash_events|worktrees in _build_init_request. In the Docker and remote default specs they are OH_CONVERSATIONS_PATH / OH_BASH_EVENTS_DIR (the comment currently points only at the /api/init bodies). If either side renames a dir later, the Files tab could quietly start listing agent-server state, including persisted conversation events, without any test failing. What do you think about sharing the names through one constant used in both places? Alternatively, a test that builds an init body and checks every state path is covered by _WORKSPACE_ROOT_STATE_DIRS would catch it.
Thank you very much 🙏
There was a problem hiding this comment.
Good catch. Leaving it for a follow-up so this PR does not touch the sandbox services: either a shared constant, or a test that builds each init body and checks its state paths are covered by _WORKSPACE_ROOT_STATE_DIRS.
This reply was written by an AI agent (OpenHands) on behalf of @jpshackelford.
| if normalized == base or normalized.startswith(base.rstrip('/') + '/'): | ||
| if _is_within(path, project_dir): | ||
| return path | ||
| # The sandbox workspace root is the one deliberate exception: it is an |
There was a problem hiding this comment.
Hello @jpshackelford,
Non-blocking (nit): the docstring above still says path is honored only when it is the project dir or a descendant of it, and the path query description on list_conversation_files says much the same. Now that the UI relies on the /workspace exception, and results for it are relative to /workspace (so project files come back as project/...), would you mind mentioning it in both places? That way the contract is also visible in the OpenAPI docs.
Thank you very much 🙏
There was a problem hiding this comment.
Done. The _resolve_workspace_dir docstring and the path query description now describe the /workspace exception (no-repo conversations only, results relative to /workspace, project files as project/...).
This reply was written by an AI agent (OpenHands) on behalf of @jpshackelford.
… endpoint
The endpoint only ever listed {spec.working_dir}[/{repo}], so files agents
write elsewhere under /workspace were invisible. Accept the exact /workspace
root (pruning agent-server state dirs) and keep file reads rooted correctly.
Co-authored-by: openhands <openhands@all-hands.dev>
4e8cffa to
d0da885
Compare
|
Rebased onto current This comment was written by an AI agent (OpenHands) on behalf of @jpshackelford. |
HUMAN:
Tested by an AI agent working for me, on a Replicated fleet VM (shared-6) running a preview build of this PR (ghcr.io/openhands/enterprise-server:sha-485d6ab). Results are under "Tested on a fleet VM" in How to Test
AGENT:
Why
The Files tab lists a cloud conversation's files through
GET /api/v1/app-conversations/{id}/files, and that endpoint only ever lists{sandbox spec working_dir}[/{repo}], which is/workspace/projectby default. When no repo is selected the agent often clones or writes straight into/workspace, so those files never show up and the UI says "No files in workspace".The endpoint now also accepts the exact path
/workspaceas the directory to list, but only when the sandbox's project dir is under/workspace, so no other path outside the project dir is ever accepted. When/workspaceis listed, the agent server's ownconversations/,bash_events/andworktrees/directories are skipped. Requests for/workspace/project(what the current UI sends) behave as before.Summary
_resolve_workspace_diraccepts/workspaceas an extra root;_build_workspace_list_commandcan prune named top-level directories, used for the agent-server state dirs._resolve_file_pathre-anchors/workspace/...under the project dir only for sandbox specs whose project dir is not under/workspace. For the default spec,/workspace/foo/a.pyis read where it is./,.., specs outside/workspace) and the read-path rules.Issue Number
Companion to OpenHands/OpenHands#18108, which tracks the user-visible bug (there is no separate issue in this repo).
How to Test
Unit tests (the root
conftest.pystarts a Postgres container that these tests do not use, hence--noconftest):uv sync --frozen uv run --frozen pytest --noconftest tests/unit/app_server/test_app_conversation_router.py # 98 passedAgainst a real sandbox on a staging Enterprise instance (conversation with no repo, deployed backend without this change):
The new
findcommand, built by_build_workspace_list_command(_WORKSPACE_ROOT_STATE_DIRS)and sent through the sandbox agent server'sPOST /api/bash/execute_bash_commandwithcwd=/workspace(the call the endpoint makes), returnedexit_code: 0and./repro-demo/src/hello.txt, withconversations/,bash_events/and the project dir's.gitleft out.After deploying, repeat the two
GETcalls above:path=/workspaceshould now return["repro-demo/src/hello.txt"], andpath=/workspace/projectshould be unchanged.Video/Screenshots
Backend-only change, so there is nothing to screenshot here. The UI before/after is on the companion PR.
Type
Notes
NO_GROUPINGare not resolved by this endpoint, and a failed listing (for example a missing directory) still returns[]./workspacestay hidden in that case./workspaceare relative to/workspace, so the project dir's files appear asproject/....This PR was drafted by an AI agent on behalf of the user.
Enterprise server image for this PR: