Skip to content

fix: keep an attaching client's directory from re-pinning another project's session - #1733

Open
cmbergamo wants to merge 5 commits into
1jehuang:masterfrom
cmbergamo:fix/subscribe-project-isolation
Open

cmbergamo wants to merge 5 commits into
1jehuang:masterfrom
cmbergamo:fix/subscribe-project-isolation

Conversation

@cmbergamo

Copy link
Copy Markdown

One daemon serves sessions for many projects. A client-reported working_dir describes the project that client is sitting in, not the project its target session belongs to, but the resume and subscribe paths both treated it as authoritative. A client in project A attaching to a session stored under project B therefore moved B's bash and file tools, its project-local MCP config, its memory scope and its swarm grouping into A's tree, while the header still showed the original project.

session_working_dir_for_client now decides it in one place: an existing session directory always wins, a session with none adopts the reported directory only while it is being created, and a session that stays unattributed stays unattributed rather than falling back to the daemon's cwd. Deliberately moving a session to another project needs an explicit request the wire protocol does not have; the only three requests carrying a working_dir (Subscribe, CommSpawn, spawn-agent) all either create a session or attach to one.

handle_resume_session resolves bound_working_dir once and feeds both the restored session and mcp_working_dir, which is what let the agent and its MCP config disagree. It reads the live target through try_lock: a generating target owns that lock and awaiting it would deadlock the attach. A refused lock reads the on-disk session stub rather than being swallowed, because a refused lock is not the same fact as a session having no directory, and reading it as one is what handed the attaching client's directory to a busy session.

The policy lives in subscribe_working_dir_policy.rs so client_session.rs does not grow past its tracked code-size budget.

Regression tests

Three new tests cover both attach paths. Reverting the guard makes each fail on its own assertion, reporting the subscriber's directory against the session's:

  • resume_preserves_target_working_dir_on_cross_project_attach
  • cross_project_attach_preserves_target_session_working_dir
  • cross_project_attach_of_offline_session_preserves_its_working_dir

Verification

  • All seven budget gates, including code size (now improved)
  • 14 isolation tests pass
  • cargo check -p jcode-app-core --tests, cargo fmt --check

One daemon serves sessions for many projects. A client-reported `working_dir`
describes the project that *client* is sitting in, not the project its target
session belongs to, but both the resume path and the subscribe path treated it
as authoritative. Attaching to a session from another project therefore moved
that session's bash and file tools, its project-local MCP config, its memory
scope, and its swarm grouping into the attaching client's tree, while the header
still showed the original project.

A client's directory is now creation-only, mirroring the rule the request
handler already applied to system prompts ("never apply an override to a target
attachment") and which had never been applied to `working_dir`.
`session_working_dir_for_client` decides it in one place: an existing session
directory always wins, a session with none adopts the reported directory only
while it is being created, and a session that stays unattributed stays
unattributed rather than falling back to the daemon's cwd. Deliberately moving a
session to another project needs an explicit request, which the wire protocol
does not have; the only three requests carrying a `working_dir` (Subscribe,
CommSpawn, spawn-agent) all either create a session or attach to one.

All four writers reachable from a single attach now read one resolved answer
instead of resolving separately, which is what made the agent and its MCP config
disagree:

- `handle_resume_session` resolves `bound_working_dir` once, from the live
  target's agent or the on-disk copy, and feeds both the restored session and
  `mcp_working_dir`. It reads the live agent with `try_lock`: a generating
  target owns that lock, and awaiting it would deadlock the attach.
- `apply_or_defer_subscribe_working_dir` applies the rule in both branches,
  including the deferred one that runs for a busy agent, where the guard would
  otherwise lapse mid-turn.
- The swarm re-key is computed after the same rule, so membership cannot be
  grouped under the attaching client's project while the session's own tools run
  in its own.
- The `session_working_dir_for_client` unit test covers each arm, and the
  pre-existing test that asserted a project-to-project move was honored now
  asserts the opposite contract: such a report is refused, and a session with no
  directory yet still adopts one at creation.

`canonicalize_or` in `server::util` is shared instead of duplicating, so resume
and resume-all cannot drift on what counts as the same project.

Tests: `cross_project_attach_preserves_target_session_working_dir` covers the
live-target attach (agent directory, post-subscribe directory, swarm member, and
the on-disk copy), and
`cross_project_attach_of_offline_session_preserves_its_working_dir` covers the
restore-from-disk path, which is the only one that binds a directory. Each was
confirmed to fail with its own guard disabled.
…s branch

The cherry-pick brought `working_dir_in_scope` across, but upstream has no
resume-all project scoping any more, so nothing calls it. Removing it keeps
this PR to the subscribe and resume attach paths it is actually about.

`cross_project_attach.rs` only needs `crate::session::Session`.
…ject's session

One daemon serves sessions for many projects. A client-reported `working_dir`
describes the project that *client* is sitting in, not the project its target
session belongs to, but the resume and subscribe paths both treated it as
authoritative. A client in project A attaching to a session stored under project
B therefore moved B's bash and file tools, its project-local MCP config, its
memory scope and its swarm grouping into A's tree, while the header still showed
the original project.

`session_working_dir_for_client` now decides it in one place: an existing session
directory always wins, a session with none adopts the reported directory only
while it is being created, and a session that stays unattributed stays
unattributed rather than falling back to the daemon's cwd. Deliberately moving a
session to another project needs an explicit request the wire protocol does not
have; the only three requests carrying a `working_dir` (Subscribe, CommSpawn,
spawn-agent) all either create a session or attach to one.

`handle_resume_session` resolves `bound_working_dir` once and feeds both the
restored session and `mcp_working_dir`, which is what let the agent and its MCP
config disagree. It reads the live target through `try_lock`: a generating
target owns that lock and awaiting it would deadlock the attach. A refused lock
reads the on-disk session stub rather than being swallowed, because a refused
lock is not the same fact as a session having no directory, and reading it as one
is what handed the attaching client's directory to a busy session.

The policy lives in `subscribe_working_dir_policy.rs` so `client_session.rs` does
not grow past its tracked code-size budget.

Regression tests cover both attach paths. Reverting the guard makes
`resume_preserves_target_working_dir_on_cross_project_attach`,
`cross_project_attach_preserves_target_session_working_dir` and
`cross_project_attach_of_offline_session_preserves_its_working_dir` fail on their
own assertions, each reporting the subscriber's directory against the session's.

Verified: all seven budget gates, the 14 isolation tests, `cargo check
-p jcode-app-core --tests`, `cargo fmt --check`.
@greptile-apps

greptile-apps Bot commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High risk] Refactors session directory binding for multi-project attach scenarios.

The three previously reported project-isolation issues are addressed in the current code. No outstanding findings block merging.

What we checked:

  • New sessions use the client's directory: handle_client reads the directory from the initial subscription and gives it to the provisional agent before subscription bookkeeping runs.
  • Unattributed sessions skip local MCP: McpManager forwards the optional directory unchanged. McpConfig loads project-local files only when a directory is present.

Summary

Keeps an existing session's project when a client attaches from another directory.

  • Uses the established session directory for subscription bookkeeping and project-local MCP configuration, including when the agent is busy.
  • Allows a client-reported directory to be adopted only during session creation; existing unattributed sessions remain unattributed.
  • Adds regression coverage for busy attachments, repeated subscriptions, offline restores, and unattributed sessions.
  • All three previous findings are addressed. No outstanding merge-blocking issues remain.

Reviews (2) · Last reviewed commit: "Merge branch '1jehuang:master' into fix/..."

…h and reconnect

- Resolve established directory from persisted stub or member state when live agent lock is busy (issue 1)
- Unify authoritative session directory for agent, swarm member, and MCP registration across repeated subscribes (issue 2)
- Pass explicit creation context to preserve unattributed working directory on attach/reconnect (issue 3)
- Add regression tests covering busy attach, repeated subscribe, and unattributed reconnect
xenking added a commit to xenking/jcode that referenced this pull request Nov 22, 2026
Use the target session root for Subscribe and ResumeSession. A client cwd can initialize a new session but cannot override a restored or live target, including its project-local MCP scope.

Upstream: 1jehuang#1733

Co-authored-by: Cristiano Martini Bergamo <[email protected]>
xenking added a commit to xenking/jcode that referenced this pull request Nov 22, 2026
Upstream-PR: 1jehuang#1357
Upstream-PR-Commit: 1jehuang#1357 0da5e81
Upstream-PR: 1jehuang#1362
Upstream-PR-Commit: 1jehuang#1362 5962058
Upstream-PR: 1jehuang#1486
Upstream-PR-Commit: 1jehuang#1486 fadc624
Upstream-PR: 1jehuang#1733
Upstream-PR-Commit: 1jehuang#1733 01f3415
Upstream-PR: 1jehuang#1750
Upstream-PR-Commit: 1jehuang#1750 1ac0c82
Upstream-PR: 1jehuang#1822
Upstream-PR-Commit: 1jehuang#1822 73f85fc
Upstream-PR: 1jehuang#1912
Upstream-PR-Commit: 1jehuang#1912 36d7682
Upstream-PR: 1jehuang#1978
Upstream-PR-Commit: 1jehuang#1978 0f55643
Upstream-PR: 1jehuang#2007
Upstream-PR-Commit: 1jehuang#2007 638f87c
Upstream-PR: 1jehuang#2213
Upstream-PR-Commit: 1jehuang#2213 c5e1487
Upstream-PR: 1jehuang#2234
Upstream-PR-Commit: 1jehuang#2234 0d4d5d4
Upstream-PR: 1jehuang#2266
Upstream-PR-Commit: 1jehuang#2266 217ddc2
Upstream-PR: 1jehuang#2267
Upstream-PR-Commit: 1jehuang#2267 e98c40b

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant