Skip to content

The agent that grows with you

fix(desktop): keep root-only project chats out of Home

brooklyn!03add1fe7a7c
diff --git a/apps/desktop/src/app/chat/sidebar/projects/workspace-groups.test.ts b/apps/desktop/src/app/chat/sidebar/projects/workspace-groups.test.ts
index bc2c3b9b9f7..5bbf2b8f57b 100644
--- a/apps/desktop/src/app/chat/sidebar/projects/workspace-groups.test.ts
+++ b/apps/desktop/src/app/chat/sidebar/projects/workspace-groups.test.ts
@@ -1080,6 +1080,31 @@ describe('overlayLiveLanes', () => {
expect(overlaid.sessionCount).toBe(2)
})
+ it('never lists a chat owned by a named project in Home, even while its live copy is detached', () => {
+ // #77591: the snapshot assigns the chat to p_app, but session.info can land
+ // with an empty cwd + root. Home must defer to the one owner, including by
+ // lineage root after compression rotates the live id.
+ const home = homeNode([makeCwdSession(null, { id: 'stale' })])
+
+ const owners = new Map([
+ ['owned', 'p_app'],
+ ['root', 'p_app'],
+ ['stale', 'p_app'],
+ ['homeless', NO_PROJECT_ID]
+ ])
+
+ const live = [
+ makeCwdSession(null, { id: 'owned' }),
+ makeCwdSession(null, { id: 'tip', _lineage_root_id: 'root' }),
+ makeCwdSession(null, { id: 'homeless' })
+ ]
+
+ const overlaid = overlayLiveLanes(home, live, new Set(), owners)
+
+ expect(overlaid.repos[0].groups[0].sessions.map(s => s.id)).toEqual(['homeless'])
+ expect(overlaid.sessionCount).toBe(1)
+ })
+
it('leaves Home alone for a session that has a cwd', () => {
// A cwd-carrying row the backend hasn't placed yet (junk root, deleted
// workspace) needs its probes — guessing here would flicker it into Home
diff --git a/apps/desktop/src/app/chat/sidebar/projects/workspace-groups.ts b/apps/desktop/src/app/chat/sidebar/projects/workspace-groups.ts
index ae1fa18c2b4..7a1a26ef0bf 100644
--- a/apps/desktop/src/app/chat/sidebar/projects/workspace-groups.ts
+++ b/apps/desktop/src/app/chat/sidebar/projects/workspace-groups.ts
@@ -711,16 +711,27 @@ export function overlayRepoLanes(
/**
* Home's overlay: its rows have no cwd to place, so this is a plain upsert of
* detached live sessions into its single lane — a brand-new project-less chat
- * shows the instant it's created, matching the flat Recents list.
+ * shows the instant it's created, matching the flat Recents list. A chat the
+ * authoritative owner map gives to a named project stays out, even while its
+ * live copy is briefly detached (else it lists in both places).
*/
function overlayHomeLane(
project: SidebarProjectTree,
live: SessionInfo[],
- removed: ReadonlySet<string>
+ removed: ReadonlySet<string>,
+ owners: ReadonlyMap<string, string>
): SidebarProjectTree {
+ const ownedElsewhere = (session: SessionInfo): boolean => {
+ const owner =
+ owners.get(session.id) ?? (session._lineage_root_id ? owners.get(session._lineage_root_id) : undefined)
+
+ return Boolean(owner) && owner !== NO_PROJECT_ID
+ }
+
+ const belongs = (session: SessionInfo): boolean => !removed.has(session.id) && !ownedElsewhere(session)
const lane = project.repos[0]?.groups[0]
- const detached = live.filter(session => isDetachedSession(session) && !removed.has(session.id))
- const kept = (lane?.sessions ?? []).filter(session => !removed.has(session.id))
+ const detached = live.filter(session => isDetachedSession(session) && belongs(session))
+ const kept = (lane?.sessions ?? []).filter(belongs)
if (!detached.length && kept.length === (lane?.sessions.length ?? 0)) {
return project
@@ -803,7 +814,7 @@ export function overlayLiveLanes(
authoritativeOwners: ReadonlyMap<string, string> = NO_OWNERS
): SidebarProjectTree {
if (project.isNoProject) {
- return overlayHomeLane(project, live, removed)
+ return overlayHomeLane(project, live, removed, authoritativeOwners)
}
let changed = false
diff --git a/tests/tui_gateway/test_project_tree.py b/tests/tui_gateway/test_project_tree.py
index a731d2a5e3c..b98c913d459 100644
--- a/tests/tui_gateway/test_project_tree.py
+++ b/tests/tui_gateway/test_project_tree.py
@@ -659,3 +659,19 @@ def test_equivalent_windows_spellings_derive_one_lane_key():
b = pt._place_by_heuristic("C:\\work\\notes\\")
assert a is not None and b is not None
assert pt._lane_key(a["lane_key"]) == pt._lane_key(b["lane_key"])
+
+
+def test_cwdless_session_with_repo_root_stays_in_its_explicit_project():
+ """A row with an empty cwd but a persisted git_repo_root belongs to the project owning
+ that root, not Home (#77591). Home keeps only rows with neither anchor, matching the
+ renderer's ``isDetachedSession``."""
+ project = _project("p_app", "App", ["/www/app"])
+ owned = _session(None, repo_root="/www/app", branch="main")
+ detached = _session(None)
+
+ tree = pt.build_tree([project], [owned, detached], [], resolve=lambda _cwd: None, hydrate=True)
+
+ explicit = next(p for p in tree["projects"] if p["id"] == "p_app")
+ assert owned["id"] in explicit["sessionIds"]
+ assert owned["id"] in [s["id"] for s in _sessions_of(explicit)]
+ assert _home_session_ids(tree) == [detached["id"]]
diff --git a/tui_gateway/project_tree.py b/tui_gateway/project_tree.py
index 20f1e8ce11d..dc412ba2d5f 100644
--- a/tui_gateway/project_tree.py
+++ b/tui_gateway/project_tree.py
@@ -190,11 +190,13 @@ def _place(
def _place_session(session: dict, resolve: Optional[Resolve]) -> Optional[dict]:
- """``_place`` for a session row; ``None`` when it has no cwd."""
- cwd = _field(session, "cwd")
- if not cwd:
+ """``_place`` for a session row, anchored on its cwd or else its persisted repo root;
+ ``None`` only when it has neither (the renderer's ``isDetachedSession``)."""
+ root = _field(session, "git_repo_root")
+ anchor = _field(session, "cwd") or root
+ if not anchor:
return None
- return _place(cwd, _field(session, "git_branch"), resolve, _field(session, "git_repo_root"))
+ return _place(anchor, _field(session, "git_branch"), resolve, root)
def _session_repo_root(session: dict, resolve: Optional[Resolve]) -> str:
@@ -321,10 +323,11 @@ class _FolderIndex:
def _project_for_session(
session: dict, index: _FolderIndex, resolve: Optional[Resolve]) -> Optional[dict]:
cwd = _field(session, "cwd")
- if not cwd:
- return None
repo_root = _session_repo_root(session, resolve)
- candidates = [cwd, repo_root] if repo_root and repo_root != cwd else [cwd]
+ # A root-only row (empty cwd) still belongs to the project owning its root.
+ candidates = [t for t in dict.fromkeys((cwd, repo_root)) if t]
+ if not candidates:
+ return None
# Longest folder match wins; ties keep the cwd match (max() keeps the first maximum).
return max((index.match(t) for t in candidates), key=lambda hit: hit[1])[0]