Repository navigation
fix(harness): expose shared knowledge to isolated users - #2687
q827505931 wants to merge 7 commits into
Conversation
123b8a8 to
ae626b4
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
AgentScopeJavaBot
left a comment
There was a problem hiding this comment.
🤖 AI Review
This PR introduces LocalWorkspaceOverlay, a new three-layer overlay filesystem that inserts a shared agent workspace between the isolated user/session upper layer and the project lower layer — but only for knowledge/ paths. The read fallback order is isolated workspace → shared agent workspace → project. Writes always target the isolated upper layer, and edits of shared knowledge files use copy-on-write. ProjectAwareOverlay is updated to extend LocalWorkspaceOverlay and its constructor gains a sharedWorkspace parameter. The test suite covers the three-layer fallback, copy-on-write edit, delete protection, move semantics, namespace isolation, and non-knowledge path isolation.
The design is sound and the test coverage is thorough. Two findings below.
| while (normalized.startsWith("/")) { | ||
| normalized = normalized.substring(1); | ||
| } | ||
| return normalized.equals(KNOWLEDGE) || normalized.startsWith(KNOWLEDGE + "/"); |
There was a problem hiding this comment.
[major] Path traversal in isKnowledgePath bypasses knowledge-only restriction on the shared workspace fallback.
isKnowledgePath checks the raw path string against the knowledge prefix without normalizing .. segments. A path like knowledge/../MEMORY.md passes the check (starts with knowledge/) but resolves to MEMORY.md at the workspace root when the underlying LocalFilesystem normalizes it.
In a multi-user deployment this means a user can read workspace-level files (e.g. MEMORY.md, AGENTS.md, other users' data) through the shared workspace fallback, bypassing the user/session namespace isolation that the upper layer provides.
The underlying LocalFilesystem prevents escaping the workspace root entirely (so this is not an arbitrary-file-read), but it does expose files within the workspace that are outside the knowledge/ namespace.
Fix: Normalize the path before the prefix check:
private static boolean isKnowledgePath(String path) {
if (path == null || path.isBlank()) {
return false;
}
String normalized = path.replace('\\', '/').strip();
while (normalized.startsWith("/")) {
normalized = normalized.substring(1);
}
normalized = Path.of(normalized).normalize().toString().replace('\\', '/');
return normalized.equals(KNOWLEDGE) || normalized.startsWith(KNOWLEDGE + "/");
}Consider adding a test case: filesystem.read(alice, "knowledge/../MEMORY.md", ...) should NOT fall through to the shared workspace.
There was a problem hiding this comment.
Addressed in d3d3b30. isKnowledgePath now normalizes the path before checking the knowledge/ namespace. I also added sharedFallbackDoesNotAllowTraversalOutsideKnowledge, which verifies that knowledge/../MEMORY.md is rejected and cannot reach a workspace-root file. The underlying LocalFilesystem already rejects .. segments; the additional normalization keeps the shared-workspace classification correct as defense in depth.
There was a problem hiding this comment.
✅ Verified as addressed in d3d3b30: isKnowledgePath (line 199) now normalizes via Path.of(normalized).normalize() before the knowledge/ prefix check, so knowledge/../MEMORY.md resolves to MEMORY.md and is correctly rejected.
| } | ||
| return upper().edit(rc, filePath, oldString, newString, replaceAll); | ||
| } | ||
|
|
There was a problem hiding this comment.
[minor] move on a shared knowledge file copies to upper without removing the source from the shared layer.
move() delegates to write() + delete(). When the source is a shared knowledge file (exists in the shared layer but not in the isolated layer), write() copies it to the upper layer, then delete() on the LocalFilesystem tries to delete the file at <namespace>/knowledge/foo.md — which doesn't exist in the upper layer. The delete silently succeeds (no-op), so the shared file remains accessible for reads.
This means move behaves as copy for shared knowledge files. Document this limitation or add logic to create a deletion marker in the upper layer to mask the shared file after the move.
There was a problem hiding this comment.
Documented the intended behavior in d3d3b30. The shared workspace is a read-only baseline, so moving a shared knowledge file uses copy-on-write: it creates the destination in the isolated upper layer and deliberately preserves the shared source. sharedKnowledgeMoveCopiesToIsolatedLayerAndDeleteCannotRemoveBaseline verifies this behavior.
There was a problem hiding this comment.
✅ Verified as addressed in d3d3b30: move (line 175) no longer delegates to super.write+delete for shared knowledge sources; it explicitly reads from sharedWorkspace and writes to upper, eliminating the partial-side-effect bug. Javadoc documents the copy-on-write contract.
AgentScopeJavaBot
left a comment
There was a problem hiding this comment.
🤖 AI Review
This PR introduces LocalWorkspaceOverlay, a new three-layer overlay filesystem that inserts a shared agent workspace between the isolated user/session upper layer and the project lower layer — but only for knowledge/ paths. The read fallback order is isolated workspace → shared agent workspace → project. Writes always target the isolated upper layer, and edits of shared knowledge files use copy-on-write. ProjectAwareOverlay is updated to extend LocalWorkspaceOverlay and its constructor gains a sharedWorkspace parameter. The test suite covers the three-layer fallback, copy-on-write edit, delete protection, move semantics, namespace isolation, and non-knowledge path isolation.
The design is sound and the test coverage is thorough. Two findings below.
| while (normalized.startsWith("/")) { | ||
| normalized = normalized.substring(1); | ||
| } | ||
| return normalized.equals(KNOWLEDGE) || normalized.startsWith(KNOWLEDGE + "/"); |
There was a problem hiding this comment.
[major] Path traversal in isKnowledgePath bypasses knowledge-only restriction on the shared workspace fallback.
isKnowledgePath checks the raw path string against the knowledge prefix without normalizing .. segments. A path like knowledge/../MEMORY.md passes the check (starts with knowledge/) but resolves to MEMORY.md at the workspace root when the underlying LocalFilesystem normalizes it.
In a multi-user deployment this means a user can read workspace-level files (e.g. MEMORY.md, AGENTS.md, other users' data) through the shared workspace fallback, bypassing the user/session namespace isolation that the upper layer provides.
The underlying LocalFilesystem prevents escaping the workspace root entirely (so this is not an arbitrary-file-read), but it does expose files within the workspace that are outside the knowledge/ namespace.
Fix: Normalize the path before the prefix check:
private static boolean isKnowledgePath(String path) {
if (path == null || path.isBlank()) {
return false;
}
String normalized = path.replace('\\', '/').strip();
while (normalized.startsWith("/")) {
normalized = normalized.substring(1);
}
normalized = Path.of(normalized).normalize().toString().replace('\\', '/');
return normalized.equals(KNOWLEDGE) || normalized.startsWith(KNOWLEDGE + "/");
}Consider adding a test case: filesystem.read(alice, "knowledge/../MEMORY.md", ...) should NOT fall through to the shared workspace.
There was a problem hiding this comment.
Addressed in d3d3b30 (duplicate review thread). isKnowledgePath now normalizes the path before the namespace check, and the new traversal regression test verifies that knowledge/../MEMORY.md cannot access the shared workspace root.
There was a problem hiding this comment.
✅ Verified as addressed in d3d3b30: Duplicate of thread 1 — same isKnowledgePath normalization fix at line 199 covers this thread concern.
AgentScopeJavaBot
left a comment
There was a problem hiding this comment.
All previously raised review comments have been addressed.
|
This PR currently conflicts with git fetch upstream
git checkout agent/fix-local-knowledge-fallback
git rebase upstream/main
# resolve conflicts, then:
git push --force-with-leaseThis is a one-time reminder. Once the conflicts are resolved, comment Automated notification by github-manager-bot |
oss-maintainer
left a comment
There was a problem hiding this comment.
Summary
Thanks for the thorough write-up and the regression suite — the three-layer knowledge resolution (isolated workspace > agent workspace > project) is the right shape, copy-on-write is kept on the write path, and non-knowledge paths still route through the plain overlay, so shell/project-writable behaviour is untouched. I verified the resolution order, the copy-on-write edit path, and the merge helpers against LocalFilesystem/OverlayFilesystem on main. Three things are worth fixing before this is mergeable, all in LocalWorkspaceOverlay; the rest is nits-free.
Headline issue: the merged discovery paths (ls/glob/grep) key on the display path returned by each layer, and the new sharedWorkspace layer is constructed as new LocalFilesystem(workspace, true, 10, null) — virtualMode=true makes it report /knowledge/... while the upper layer (default LocalFsMode.ROOTED, and always namespace-stripped) reports knowledge/.... The same physical entry therefore shows up twice in ls/glob output for every configuration, including non-namespaced ones that previously got a single entry from OverlayFilesystem. A maintainer should confirm, but this looks like a user-visible regression in the default setup rather than an isolated-only edge case.
Note on validation: I could not run mvn -pl agentscope-harness -am test in this environment, so the "832 harness tests green" result is taken from your PR description; the findings above come from static reading of the diff plus the main sources.
Findings
- [Warning]
LocalWorkspaceOverlay.java:94— duplicatedls/glob/grepentries caused by the aliased, differently-normalisedsharedWorkspacelayer (see inline comment). - [Warning]
LocalWorkspaceOverlay.java:144— copy-on-write pre-seeding races a concurrent edit and silently overwrites it with the shared baseline (see inline comment). - [Warning]
LocalWorkspaceOverlay.java:94— knowledgels/glob/grepreport success with an empty result when every layer failed, whichOverlayFilesystemdeliberately does not do (see inline comment).
Automated review by github-manager-bot
| mergeFileInfo(merged, upper().ls(rc, path)); | ||
| return LsResult.success(new ArrayList<>(merged.values())); | ||
| } | ||
|
|
There was a problem hiding this comment.
[Warning] With no namespace (and with AGENT/GLOBAL scope), upper and sharedWorkspace are the same workspace but report different paths: the sandboxed shared filesystem emits /knowledge/foo, while the upper emits knowledge/foo. Since the merge keys are the raw paths, ls and glob duplicate every workspace knowledge entry; grep duplicates matches too. Normalize paths/keying or skip the shared merge when it aliases upper, and add a non-namespaced regression test.
| } | ||
| return upper().edit(rc, filePath, oldString, newString, replaceAll); | ||
| } | ||
|
|
There was a problem hiding this comment.
[Warning] This copy-on-write sequence races another edit in the same namespace. After both callers see no upper file, one can create it and the other returns Cannot copy... solely because write sees it already exists. The previous overlay lets upper.edit serialize through its per-file lock. If a failed write leaves an upper file, continue with upper.edit (or retry), and add a concurrent shared-knowledge edit test.
| mergeFileInfo(merged, upper().ls(rc, path)); | ||
| return LsResult.success(new ArrayList<>(merged.values())); | ||
| } | ||
|
|
There was a problem hiding this comment.
[Warning] ls now returns LsResult.success(empty) whenever all three layers fail. A nonexistent knowledge subdirectory, or a knowledge file passed to ls, therefore appears as an empty directory instead of preserving OverlayFilesystem's error result; FilesystemTool will report it misleadingly. Return an appropriate failed layer result when no layer succeeds, and cover missing and non-directory knowledge paths.
What changed
Root cause
The local filesystem upper layer applies the user/session namespace to every relative path. Once isolation is enabled,
knowledge/foo.mdresolves only under<workspace>/<namespace>/knowledge/foo.md; the overlay then falls back directly to the project filesystem and never checks the agent-level<workspace>/knowledge/foo.mdbaseline that was already injected into the prompt.Validation
mvn -pl agentscope-harness -am testFixes #2683