Skip to content

Commit fb8a7b7

Browse files
committed
fix(client-runtime): keep side chats in ownership reads and hide them only from list reads
environmentThreadRefsAtom and threadShellsAtom now include every thread the environment owns, side chats included, so worktree cleanup and other ownership checks cannot mistake a side chat's shared worktree for an orphan. Lists read the new environmentVisibleThreadRefsAtom and visibleThreadShellsAtom, which hide side chats while their parent exists. environmentAllThreadRefsAtom is gone: the unfiltered list is the default again.
1 parent a318a9c commit fb8a7b7

2 files changed

Lines changed: 62 additions & 20 deletions

File tree

‎packages/client-runtime/src/state/entities.test.ts‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,25 @@ describe("environment entity projections", () => {
228228
),
229229
);
230230

231+
const visibleRefs = harness.registry.get(
232+
harness.threadShells.environmentVisibleThreadRefsAtom(ENVIRONMENT_ID),
233+
);
234+
expect(visibleRefs.some((ref) => ref.threadId === SIDE_CHAT_ID)).toBe(false);
235+
// Ownership reads keep the side chat: its worktree is still in use.
231236
const refs = harness.registry.get(
232237
harness.threadShells.environmentThreadRefsAtom(ENVIRONMENT_ID),
233238
);
234-
expect(refs.some((ref) => ref.threadId === SIDE_CHAT_ID)).toBe(false);
235-
const allRefs = harness.registry.get(
236-
harness.threadShells.environmentAllThreadRefsAtom(ENVIRONMENT_ID),
237-
);
238-
expect(allRefs.some((ref) => ref.threadId === SIDE_CHAT_ID)).toBe(true);
239+
expect(refs.some((ref) => ref.threadId === SIDE_CHAT_ID)).toBe(true);
240+
expect(
241+
harness.registry
242+
.get(harness.threadShells.visibleThreadShellsAtom)
243+
.some((thread) => thread.id === SIDE_CHAT_ID),
244+
).toBe(false);
245+
expect(
246+
harness.registry
247+
.get(harness.threadShells.threadShellsAtom)
248+
.some((thread) => thread.id === SIDE_CHAT_ID),
249+
).toBe(true);
239250
const refsByProject = harness.registry.get(
240251
harness.threadShells.environmentThreadRefsByProjectAtom(ENVIRONMENT_ID),
241252
);
@@ -285,9 +296,14 @@ describe("environment entity projections", () => {
285296
);
286297

287298
const refs = harness.registry.get(
288-
harness.threadShells.environmentThreadRefsAtom(ENVIRONMENT_ID),
299+
harness.threadShells.environmentVisibleThreadRefsAtom(ENVIRONMENT_ID),
289300
);
290301
expect(refs.some((ref) => ref.threadId === SIDE_CHAT_ID)).toBe(true);
302+
expect(
303+
harness.registry
304+
.get(harness.threadShells.visibleThreadShellsAtom)
305+
.some((thread) => thread.id === SIDE_CHAT_ID),
306+
).toBe(true);
291307
const refsByProject = harness.registry.get(
292308
harness.threadShells.environmentThreadRefsByProjectAtom(ENVIRONMENT_ID),
293309
);

‎packages/client-runtime/src/state/threadShell.ts‎

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,17 @@ export function createEnvironmentThreadShellAtoms(input: {
9494
}).pipe(Atom.withLabel(`environment-thread-index:${environmentId}`)),
9595
);
9696

97+
// Every thread the environment owns, side chats included. Ownership checks
98+
// (worktree cleanup, membership, outbox routing) must read this list: a
99+
// side chat shares its parent's worktree, and forgetting it would make that
100+
// worktree look orphaned when the parent is deleted.
97101
const environmentThreadRefsAtom = Atom.family((environmentId: EnvironmentId) => {
98102
let previous: ReadonlyArray<ScopedThreadRef> = [];
99103
return Atom.make((get) => {
100-
const threadIds = get(environmentThreadIndexAtom(environmentId));
101-
const next = get(environmentThreadsAtom(environmentId))
102-
.filter((thread) => !isHiddenSideChat(thread, threadIds))
103-
.map((thread) => ({
104-
environmentId,
105-
threadId: thread.id,
106-
}));
104+
const next = get(environmentThreadsAtom(environmentId)).map((thread) => ({
105+
environmentId,
106+
threadId: thread.id,
107+
}));
107108
if (threadRefsEqual(previous, next)) {
108109
return previous;
109110
}
@@ -112,19 +113,24 @@ export function createEnvironmentThreadShellAtoms(input: {
112113
}).pipe(Atom.withLabel(`environment-thread-refs:${environmentId}`));
113114
});
114115

115-
const environmentAllThreadRefsAtom = Atom.family((environmentId: EnvironmentId) => {
116+
// What thread lists show: side chats stay hidden while their parent exists
117+
// and are grouped beside it instead.
118+
const environmentVisibleThreadRefsAtom = Atom.family((environmentId: EnvironmentId) => {
116119
let previous: ReadonlyArray<ScopedThreadRef> = [];
117120
return Atom.make((get) => {
118-
const next = get(environmentThreadsAtom(environmentId)).map((thread) => ({
119-
environmentId,
120-
threadId: thread.id,
121-
}));
121+
const threadIds = get(environmentThreadIndexAtom(environmentId));
122+
const next = get(environmentThreadsAtom(environmentId))
123+
.filter((thread) => !isHiddenSideChat(thread, threadIds))
124+
.map((thread) => ({
125+
environmentId,
126+
threadId: thread.id,
127+
}));
122128
if (threadRefsEqual(previous, next)) {
123129
return previous;
124130
}
125131
previous = next;
126132
return next;
127-
}).pipe(Atom.withLabel(`environment-all-thread-refs:${environmentId}`));
133+
}).pipe(Atom.withLabel(`environment-visible-thread-refs:${environmentId}`));
128134
});
129135

130136
const environmentThreadRefsByProjectAtom = Atom.family((environmentId: EnvironmentId) => {
@@ -298,15 +304,35 @@ export function createEnvironmentThreadShellAtoms(input: {
298304
return previousThreadShells;
299305
}).pipe(Atom.withLabel("environment-thread-shell-list"));
300306

307+
// The list a thread picker or sidebar renders: `threadShellsAtom` minus side
308+
// chats whose parent is still present.
309+
let previousVisibleThreadShells: ReadonlyArray<EnvironmentThreadShell> = [];
310+
const visibleThreadShellsAtom = Atom.make((get) => {
311+
const next: EnvironmentThreadShell[] = [];
312+
for (const environmentId of get(input.catalogValueAtom).entries.keys()) {
313+
const threadIds = get(environmentThreadIndexAtom(environmentId));
314+
for (const thread of get(environmentThreadsAtom(environmentId))) {
315+
if (isHiddenSideChat(thread, threadIds)) continue;
316+
next.push(scopedThread(environmentId, thread));
317+
}
318+
}
319+
if (arrayElementsEqual(previousVisibleThreadShells, next)) {
320+
return previousVisibleThreadShells;
321+
}
322+
previousVisibleThreadShells = next;
323+
return previousVisibleThreadShells;
324+
}).pipe(Atom.withLabel("environment-visible-thread-shell-list"));
325+
301326
return {
302327
environmentThreadsAtom,
303328
environmentThreadIndexAtom,
304-
environmentAllThreadRefsAtom,
305329
environmentThreadRefsAtom,
330+
environmentVisibleThreadRefsAtom,
306331
environmentThreadRefsByProjectAtom,
307332
environmentSideChatsByParentAtom,
308333
threadRefsAtom,
309334
threadShellsAtom,
335+
visibleThreadShellsAtom,
310336
sideChatsByParentAtom: (ref: ScopedThreadRef) => sideChatsByParentAtomFamily(threadKey(ref)),
311337
threadShellsForProjectRefsAtom: (refs: ReadonlyArray<ScopedProjectRef>) =>
312338
threadShellsForProjectRefsAtomFamily(projectRefCollectionKey(refs)),

0 commit comments

Comments
 (0)