Skip to content

fix(harness): expose shared knowledge to isolated users - #2687

Open
q827505931 wants to merge 7 commits into
agentscope-ai:mainfrom
q827505931:agent/fix-local-knowledge-fallback
Open

q827505931 wants to merge 7 commits into
agentscope-ai:mainfrom
q827505931:agent/fix-local-knowledge-fallback

Conversation

@q827505931

Copy link
Copy Markdown

What changed

  • add a local workspace overlay that resolves knowledge files in this order: isolated user/session workspace, shared agent workspace, then project
  • preserve copy-on-write behavior when editing shared knowledge
  • keep shell execution and project-writable routing intact
  • add regression coverage for shared fallback, isolated overrides, copy-on-write, shell capability, and cross-user isolation

Root cause

The local filesystem upper layer applies the user/session namespace to every relative path. Once isolation is enabled, knowledge/foo.md resolves 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.md baseline that was already injected into the prompt.

Validation

  • mvn -pl agentscope-harness -am test
  • core: 2,274 tests, 0 failures, 0 errors
  • harness: 832 tests, 0 failures, 0 errors

Fixes #2683

@CLAassistant

CLAassistant commented Aug 13, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@q827505931
q827505931 force-pushed the agent/fix-local-knowledge-fallback branch from 123b8a8 to ae626b4 Compare August 13, 2026 10:13
@codecov

codecov Bot commented Aug 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.53191% with 23 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...arness/agent/filesystem/LocalWorkspaceOverlay.java 74.44% 8 Missing and 15 partials ⚠️

📢 Thoughts on this report? Let us know!

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 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 + "/");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

@q827505931 q827505931 Aug 14, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

@q827505931 q827505931 Aug 14, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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 AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 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 + "/");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

@q827505931 q827505931 Aug 14, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Verified as addressed in d3d3b30: Duplicate of thread 1 — same isKnowledgePath normalization fix at line 199 covers this thread concern.

@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/harness agentscope-harness (test/runtime support) labels Aug 15, 2026
@q827505931
q827505931 marked this pull request as ready for review August 15, 2026 09:08

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All previously raised review comments have been addressed.

@oss-maintainer

Copy link
Copy Markdown
Collaborator

⚠️ Merge conflict detected

This PR currently conflicts with main and cannot be merged. Please rebase (or merge main in) and resolve the conflicts:

git fetch upstream
git checkout agent/fix-local-knowledge-fallback
git rebase upstream/main
# resolve conflicts, then:
git push --force-with-lease

This is a one-time reminder. Once the conflicts are resolved, comment @oss-maintainer please re-review and I will pick it up again.


Automated notification by github-manager-bot

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 — duplicated ls/glob/grep entries caused by the aliased, differently-normalised sharedWorkspace layer (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 — knowledge ls/glob/grep report success with an empty result when every layer failed, which OverlayFilesystem deliberately 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()));
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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()));
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/harness agentscope-harness (test/runtime support) bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:<agent-id>/knowledge文件夹下的文件读取不到

4 participants