Skip to content

fix: resolve target agent workspace for cross-agent subagent spawns#40176

Merged
mcaxtr merged 2 commits intoopenclaw:mainfrom
moshehbenavraham:fix/cross-agent-workspace-isolation
Mar 13, 2026
Merged

fix: resolve target agent workspace for cross-agent subagent spawns#40176
mcaxtr merged 2 commits intoopenclaw:mainfrom
moshehbenavraham:fix/cross-agent-workspace-isolation

Conversation

@moshehbenavraham
Copy link
Copy Markdown
Contributor

Summary

  • Cross-agent subagent spawns (e.g. PM → Ops via sessions_spawn(agentId: "ops")) incorrectly inherited the caller's workspace and persona files instead of the target agent's
  • Added targetAgentId parameter to resolveSpawnedWorkspaceInheritance so it resolves the target agent's workspace
  • For cross-agent spawns, skip passing the caller's ctx.workspaceDir as explicitWorkspaceDir so it doesn't short-circuit the resolution

Fixes #40171

Test plan

  • Unit tests pass (spawned-context.test.ts, subagent-spawn.attachments.test.ts — 19 tests)
  • Build passes (pnpm build)
  • Live test: PM spawns cross-agent sub-agent to Ops → sub-agent reads workspace-ops/IDENTITY.md and returns "Ops Agent" (previously read workspace-pm/IDENTITY.md and returned "PM Agent")
  • Same-agent spawns still inherit workspace correctly (no regression)

🤖 Generated with Claude Code

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Mar 8, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Mar 8, 2026

Greptile Summary

This PR fixes a bug where cross-agent subagent spawns (e.g. PM → Ops) incorrectly inherited the caller's workspace directory instead of the target agent's, by threading targetAgentId through resolveSpawnedWorkspaceInheritance and suppressing the caller's explicitWorkspaceDir for cross-agent cases. The logic change is minimal, focused, and correct.

Key observations:

  • The core fix is sound: for cross-agent spawns, explicitWorkspaceDir is set to undefined so it doesn't short-circuit resolution, and targetAgentId takes priority via ?? to drive workspace lookup via resolveAgentWorkspaceDir.
  • The same-agent path is correctly preserved — explicitWorkspaceDir is only cleared when targetAgentId !== requesterAgentId.
  • spawned-context.test.ts was not updated to cover the new targetAgentId behavior. The only call site always passes a non-nullish targetAgentId, making the requesterSessionKey-based fallback branch dead code for that caller — there are no tests asserting the cross-agent workspace resolution or the precedence of targetAgentId.
  • The PR test plan has one unchecked item ("Same-agent spawns still inherit workspace correctly") which should be verified before merge.

Confidence Score: 4/5

  • Safe to merge with minor caveats — the fix is logically correct but test coverage for the new behavior is incomplete and one test-plan item is unverified.
  • The change is small and targeted. The cross-agent workspace resolution logic is correct, and the same-agent path is unchanged. The main gaps are: (1) no unit tests for the new targetAgentId parameter in spawned-context.test.ts, and (2) the PR's own test plan leaves the same-agent regression check unchecked. These reduce confidence slightly but are not blockers for the fix itself.
  • src/agents/spawned-context.test.ts — needs tests for the targetAgentId parameter behavior.

Comments Outside Diff (1)

  1. src/agents/spawned-context.test.ts, line 46-64 (link)

    Missing tests for the new targetAgentId parameter

    The resolveSpawnedWorkspaceInheritance describe block was not updated to cover the new targetAgentId parameter — the core behavior introduced by this PR.

    Two cases that should be exercised:

    1. Cross-agent spawntargetAgentId provided and different from what requesterSessionKey would derive → should return the target agent's configured workspace, not the requester's.
    2. targetAgentId always wins — since targetAgentId is passed as a non-nullish string at the only call site in subagent-spawn.ts, the requesterSessionKey-based fallback inside the function is now dead code for that caller. A test confirming targetAgentId takes precedence would document this intention explicitly.

    Adding coverage here would also provide a regression guard for the unchecked item in the PR's test plan ("Same-agent spawns still inherit workspace correctly").

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: src/agents/spawned-context.test.ts
    Line: 46-64
    
    Comment:
    **Missing tests for the new `targetAgentId` parameter**
    
    The `resolveSpawnedWorkspaceInheritance` describe block was not updated to cover the new `targetAgentId` parameter — the core behavior introduced by this PR.
    
    Two cases that should be exercised:
    
    1. **Cross-agent spawn**`targetAgentId` provided and different from what `requesterSessionKey` would derive → should return the target agent's configured workspace, not the requester's.
    2. **`targetAgentId` always wins** — since `targetAgentId` is passed as a non-nullish string at the only call site in `subagent-spawn.ts`, the `requesterSessionKey`-based fallback inside the function is now dead code for that caller. A test confirming `targetAgentId` takes precedence would document this intention explicitly.
    
    Adding coverage here would also provide a regression guard for the unchecked item in the PR's test plan ("Same-agent spawns still inherit workspace correctly").
    
    How can I resolve this? If you propose a fix, please make it concise.

Last reviewed commit: 3ef4491

Comment thread src/agents/subagent-spawn.ts
@mcaxtr mcaxtr self-assigned this Mar 13, 2026
moshehbenavraham and others added 2 commits March 13, 2026 11:52
Cross-agent spawns (e.g. PM → Ops) incorrectly inherited the caller's
workspace and persona files instead of the target agent's. Two issues:

1. resolveSpawnedWorkspaceInheritance extracted agent ID from the
   requester's session key, not the target agent
2. The caller's ctx.workspaceDir was passed as explicitWorkspaceDir,
   short-circuiting resolution before targetAgentId was considered

Now targetAgentId is passed to the resolver and takes priority, and
cross-agent spawns skip the caller's inherited workspace override.

Fixes openclaw#40171
@mcaxtr mcaxtr force-pushed the fix/cross-agent-workspace-isolation branch from 89824f4 to 2378e40 Compare March 13, 2026 14:57
@mcaxtr mcaxtr merged commit 55e79ad into openclaw:main Mar 13, 2026
29 checks passed
@mcaxtr
Copy link
Copy Markdown
Member

mcaxtr commented Mar 13, 2026

Merged via squash.

Thanks @moshehbenavraham!

@moshehbenavraham
Copy link
Copy Markdown
Contributor Author

Thank you and the team @mcaxtr ! Love the project!

z-hao-wang pushed a commit to z-hao-wang/openclaw that referenced this pull request Mar 13, 2026
…penclaw#40176)

Merged via squash.

Prepared head SHA: 2378e40
Co-authored-by: moshehbenavraham <17122072+moshehbenavraham@users.noreply.github.com>
Co-authored-by: mcaxtr <7562095+mcaxtr@users.noreply.github.com>
Reviewed-by: @mcaxtr
frankekn pushed a commit to xinhuagu/openclaw that referenced this pull request Mar 14, 2026
…penclaw#40176)

Merged via squash.

Prepared head SHA: 2378e40
Co-authored-by: moshehbenavraham <17122072+moshehbenavraham@users.noreply.github.com>
Co-authored-by: mcaxtr <7562095+mcaxtr@users.noreply.github.com>
Reviewed-by: @mcaxtr
@cosmicallycooked
Copy link
Copy Markdown

Thanks @moshehbenavraham just uncovered the same problem today for our setup. Thanks for the PR

sbezludny pushed a commit to sbezludny/openclaw that referenced this pull request Mar 27, 2026
…penclaw#40176)

Merged via squash.

Prepared head SHA: 2378e40
Co-authored-by: moshehbenavraham <17122072+moshehbenavraham@users.noreply.github.com>
Co-authored-by: mcaxtr <7562095+mcaxtr@users.noreply.github.com>
Reviewed-by: @mcaxtr
lovewanwan pushed a commit to lovewanwan/openclaw that referenced this pull request Apr 28, 2026
…penclaw#40176)

Merged via squash.

Prepared head SHA: 2378e40
Co-authored-by: moshehbenavraham <17122072+moshehbenavraham@users.noreply.github.com>
Co-authored-by: mcaxtr <7562095+mcaxtr@users.noreply.github.com>
Reviewed-by: @mcaxtr
ogt-redknie pushed a commit to ogt-redknie/OPENX that referenced this pull request May 2, 2026
…penclaw#40176)

Merged via squash.

Prepared head SHA: 2378e40
Co-authored-by: moshehbenavraham <17122072+moshehbenavraham@users.noreply.github.com>
Co-authored-by: mcaxtr <7562095+mcaxtr@users.noreply.github.com>
Reviewed-by: @mcaxtr
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
…penclaw#40176)

Merged via squash.

Prepared head SHA: 2378e40
Co-authored-by: moshehbenavraham <17122072+moshehbenavraham@users.noreply.github.com>
Co-authored-by: mcaxtr <7562095+mcaxtr@users.noreply.github.com>
Reviewed-by: @mcaxtr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cross-agent subagent spawn inherits caller's workspace instead of target agent's

3 participants