@@ -75,7 +75,7 @@ function buildActiveShellSnapshot(input: {
7575type ShellTrimRow = Pick <
7676 OrchestrationV2ThreadShell ,
7777 "id" | "projectId" | "lineage" | "settledOverride" | "settledAt"
78- > ;
78+ > & { readonly hasRemoteTasks ?: boolean } ;
7979
8080/**
8181 * What a subscriber's trims read across every active row. Computed from full shells or from the
@@ -84,6 +84,8 @@ type ShellTrimRow = Pick<
8484interface ShellTrimFacts {
8585 readonly subagentParentIds : ReadonlySet < string > ;
8686 readonly subagentThreadIds : Set < string > ;
87+ /** Only the store's index knows these; shells alone leave it empty. */
88+ readonly remoteTaskParentIds : ReadonlySet < string > ;
8789 /** Undefined when no thread is settled, so none is older. */
8890 readonly olderSettledBeforeMs : number | undefined ;
8991 readonly olderSettledCounts : ReadonlyArray < OrchestrationV2OlderSettledCount > ;
@@ -94,8 +96,10 @@ const NEWEST_SETTLED_KEPT = 25;
9496function shellTrimFacts ( rows : ReadonlyArray < ShellTrimRow > ) : ShellTrimFacts {
9597 const subagentParentIds = new Set < string > ( ) ;
9698 const subagentThreadIds = new Set < string > ( ) ;
99+ const remoteTaskParentIds = new Set < string > ( ) ;
97100 const settledTimes : Array < number > = [ ] ;
98101 for ( const row of rows ) {
102+ if ( row . hasRemoteTasks === true ) remoteTaskParentIds . add ( row . id ) ;
99103 if ( row . lineage . relationshipToParent === "subagent" ) {
100104 subagentThreadIds . add ( row . id ) ;
101105 if ( row . lineage . parentThreadId ) subagentParentIds . add ( row . lineage . parentThreadId ) ;
@@ -121,6 +125,7 @@ function shellTrimFacts(rows: ReadonlyArray<ShellTrimRow>): ShellTrimFacts {
121125 return {
122126 subagentParentIds,
123127 subagentThreadIds,
128+ remoteTaskParentIds,
124129 olderSettledBeforeMs,
125130 olderSettledCounts : Array . from ( counts , ( [ projectId , count ] ) => ( { projectId, count } ) ) ,
126131 } ;
@@ -140,13 +145,14 @@ function hiddenChildAncestorIds(
140145
141146function omitIdleSubagentShellsWith (
142147 threads : ReadonlyArray < OrchestrationV2ThreadShell > ,
143- facts : Pick < ShellTrimFacts , "subagentParentIds" > ,
148+ facts : Pick < ShellTrimFacts , "subagentParentIds" | "remoteTaskParentIds" > ,
144149) : ReadonlyArray < OrchestrationV2ThreadShell > {
145150 return threads . filter (
146151 ( thread ) =>
147152 thread . lineage . relationshipToParent !== "subagent" ||
148153 isLiveSubagentShell ( thread ) ||
149- facts . subagentParentIds . has ( thread . id ) ,
154+ facts . subagentParentIds . has ( thread . id ) ||
155+ facts . remoteTaskParentIds . has ( thread . id ) ,
150156 ) ;
151157}
152158
@@ -156,7 +162,10 @@ function omitOlderSettledShellsWith(
156162 readonly snapshotSequence : number ;
157163 readonly now : DateTime . Utc ;
158164 } ,
159- facts : Pick < ShellTrimFacts , "olderSettledBeforeMs" | "olderSettledCounts" > ,
165+ facts : Pick <
166+ ShellTrimFacts ,
167+ "olderSettledBeforeMs" | "olderSettledCounts" | "remoteTaskParentIds"
168+ > ,
160169) : {
161170 readonly threads : ReadonlyArray < OrchestrationV2ThreadShell > ;
162171 readonly olderSettled : OrchestrationV2OlderSettledThreads ;
@@ -170,7 +179,8 @@ function omitOlderSettledShellsWith(
170179 thread . pinnedAt != null ||
171180 ( thread . snoozedUntil != null && DateTime . toEpochMillis ( thread . snoozedUntil ) > nowMs ) ||
172181 isLiveSubagentShell ( thread ) ||
173- ancestorIds . has ( thread . id ) ,
182+ ancestorIds . has ( thread . id ) ||
183+ facts . remoteTaskParentIds . has ( thread . id ) ,
174184 ) ;
175185 return {
176186 threads,
@@ -185,7 +195,8 @@ function omitOlderSettledShellsWith(
185195/**
186196 * Idle subagent children have no sidebar row, so clients that opt in read them from their thread
187197 * family instead. A child stays while it is live or the parent of another child, so the request
188- * and working walks over shell rows never meet a gap in a nested chain.
198+ * and working walks over shell rows never meet a gap in a nested chain. It also stays as the
199+ * parent of a task in a linked environment, whose clients nest that task under it.
189200 */
190201export function omitIdleSubagentShells (
191202 threads : ReadonlyArray < OrchestrationV2ThreadShell > ,
@@ -214,8 +225,8 @@ export function activeShellRowsForSubscriber(
214225/**
215226 * Opted-in snapshots keep the 25 newest settled top-level threads (and ties) and count the rest per
216227 * project in `olderSettled`; clients page them with `getOlderSettledThreads`. Older rows stay while
217- * pinned, snoozed, live, or above a kept subagent or side chat, so walks over shell rows still find
218- * them.
228+ * pinned, snoozed, live, above a kept subagent or side chat, or the parent of a task in a linked
229+ * environment, so walks over shell rows and other environments' tasks still find them.
219230 */
220231export function omitOlderSettledShells ( input : {
221232 readonly threads : ReadonlyArray < OrchestrationV2ThreadShell > ;
@@ -261,7 +272,10 @@ function shellSubscriberThreadIds(
261272 const subagents = index . filter (
262273 ( entry ) =>
263274 entry . lineage . relationshipToParent === "subagent" &&
264- ( ! options . omitIdleSubagents || entry . mayBeLive || facts . subagentParentIds . has ( entry . id ) ) ,
275+ ( ! options . omitIdleSubagents ||
276+ entry . mayBeLive ||
277+ entry . hasRemoteTasks ||
278+ facts . subagentParentIds . has ( entry . id ) ) ,
265279 ) ;
266280 const ancestorIds = hiddenChildAncestorIds ( [
267281 ...subagents ,
@@ -280,6 +294,7 @@ function shellSubscriberThreadIds(
280294 entry . pinnedAt != null ||
281295 entry . snoozedUntil != null ||
282296 entry . mayBeLive ||
297+ entry . hasRemoteTasks ||
283298 ancestorIds . has ( entry . id )
284299 ? [ entry . id ]
285300 : [ ] ,
0 commit comments