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