Skip to content

fix(harness): honor isolation scope for shell working directory - #2364

Open
logicwu0 wants to merge 3 commits into
agentscope-ai:mainfrom
logicwu0:fix/2329-isolated-shell-working-directory
Open

logicwu0 wants to merge 3 commits into
agentscope-ai:mainfrom
logicwu0:fix/2329-isolated-shell-working-directory

Conversation

@logicwu0

Copy link
Copy Markdown
Contributor

Summary

  • apply USER/SESSION namespaces before selecting the shell working directory
  • preserve the configured project directory when no namespace applies
  • add cross-platform regression coverage for SESSION, USER, and GLOBAL isolation

Testing

  • mvn -pl agentscope-harness -Dtest=ProjectAwareOverlayTest,LocalFilesystemModeTest test
  • git diff --check

Fixes #2329

@codecov

codecov Bot commented Jul 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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

Review: PR #2364 — fix(harness): honor isolation scope for shell working directory

Verdict: Approved ✅

Summary

Fixes resolveExecuteCwd() to check namespace scope before falling back to shellCwd. Previously, shellCwd took priority, which meant USER/SESSION isolation was silently bypassed when a default shell working directory was configured.

Analysis

The bug:
When shellCwd was set (e.g., to the user's project directory), namespaced calls (USER/SESSION isolation) would still use shellCwd instead of the namespaced path. This broke workspace isolation — a SESSION-scoped agent could see/modify another session's files.

The fix:

// Before: shellCwd checked first
if (shellCwd != null) return shellCwd;
if (namespace != null) return namespacedPath;

// After: namespace checked first
if (namespace != null) return namespacedPath;
if (shellCwd != null) return shellCwd;

This is the correct priority: explicit isolation scope should always override the default working directory.

Test coverage:

  • SESSION namespace with shellCwd set → uses namespaced path ✅
  • USER namespace with shellCwd set → uses namespaced path ✅
  • GLOBAL (null namespace) with shellCwd → uses shellCwd ✅
  • Null namespace + null shellCwd → falls back to cwd ✅
  • Directory creation failure → proper error handling ✅

Javadoc updates accurately describe the new priority order.

All CI checks pass. Important isolation fix.

@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/harness agentscope-harness (test/runtime support) labels Aug 11, 2026

@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 fixes a genuine isolation bug in LocalFilesystemWithShell.execute(). Previously, shell commands always ran with pwd = shellCwd (or rootDir) regardless of whether a NamespaceFactory was configured. The fix adds a branch: when a namespace is active, pwd is set to getCwd() (the namespaced root); otherwise it falls back to shellCwd / rootDir. Six new tests cover all namespace × shellCwd combinations. The core logic is correct and the fix is clean.

(inline comments could not be attached — line numbers fell outside PR hunks. See archived report.)

@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 fixes a genuine isolation bug in LocalFilesystemWithShell.execute(). Previously, shell commands always ran with pwd = shellCwd (or rootDir) regardless of whether a NamespaceFactory was configured. The fix adds a branch: when a namespace is active, pwd is set to getCwd() (the namespaced root); otherwise it falls back to shellCwd / rootDir. Six new tests cover all namespace × shellCwd combinations. The core logic is correct and the fix is clean.

(inline comments could not be attached — line numbers fell outside PR hunks. See archived report.)

@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 fixes a genuine isolation bug in LocalFilesystemWithShell.execute(). Previously, shell commands always ran with pwd = shellCwd (or rootDir) regardless of whether a NamespaceFactory was configured. The fix adds a branch: when a namespace is active, pwd is set to getCwd() (the namespaced root); otherwise it falls back to shellCwd / rootDir. Six new tests cover all namespace × shellCwd combinations. The core logic is correct and the fix is clean.

(inline comments could not be attached — line numbers fell outside PR hunks. See archived report.)

@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 fix/2329-isolated-shell-working-directory
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

@xzxiaoshan

Copy link
Copy Markdown
Collaborator

LocalFilesystemSpec#toFilesystem always
passes shellCwd = effectiveProject (defaulting to ${user.dir}), and resolveExecuteCwd
short-circuits on shellCwd != null, so the namespace branch is effectively dead code for
every default-configured agent. File tools namespace their writes into workspace/<ns>/
while the shell starts in the project root — which is precisely what #2329 reports. The bug
is still present on current main, so the fix itself is still needed. Flipping the priority
so an active namespace wins over shellCwd is the right semantic: isolation that applies to
file writes but not to the shell isn't isolation at all.

1. Must-fix before merge: the workspace system prompt now contradicts actual behavior.
WorkspaceContextMiddleware#buildWorkspaceParagraph renders
"Shell commands run with pwd set to the project directory." unconditionally in the
local-overlay branch. After this change, under SESSION/USER isolation with a non-empty
namespace, the real pwd is workspace/<ns>/ — so the prompt tells the model something that
is no longer true, and it will keep addressing project files via relative paths from a
wrong assumed cwd. This needs to branch on whether the namespace applies. The plumbing is
feasible: the call site at the buildWorkspaceParagraph(...) invocation already has the
per-call RuntimeContext in scope (it is passed to WorkspaceContextMaterials.register
a few lines below), it just isn't handed to the paragraph builder today. The Project:
line rendered from getShellCwd() stays correct as-is; only the shell-cwd sentence needs
to become conditional.

2. Please state the behavior change explicitly in the PR description. After the flip,
under SESSION/USER isolation the shell can no longer reach project files via relative
paths from pwd — commands that operate on the user's source tree must cd to the project
absolute path (which the prompt's Project: line still provides). That is the correct
trade-off for isolation semantics and matches the issue's expectation, but it is a
documented-behavior change (the shellCwd field javadoc previously promised "shell sees
the user's project directory"), so it should be called out for release notes. GLOBAL/AGENT
scopes and sharedLocalWorkspace(true) are unaffected — both resolve to an empty/absent
namespace and still fall back to the project directory.

3. Style: prefer keeping resolveExecuteCwd flat. The old implementation was a chain
of early returns; this change restructures it into a 4-level nested block (factory check →
namespace check → loop → try/catch). Since only the priority changed, extracting the
namespace resolution + directory creation into a small private helper would keep the method
readable as early returns, e.g. if (nsf != null && hasNamespace(nsf, rc)) return namespacedCwd(nsf, rc); return shellCwd != null ? shellCwd : getCwd();.

4. Minor observation on the failure path. When Files.createDirectories(namespaced)
fails we log a warning and still return the non-existent directory, relying on
ProcessBuilder to fail with an IOException at start-up. Your regression test covers this,
which is good; just flagging it as a candidate for a more diagnosable error (or a fallback)
if you'd like.

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]:IsolationScope (SESSION/USER) does not apply to execute_shell_command working_directory

4 participants