@devries-cameron20 / t3code-1 / commits / d412871f27
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): a task on a linked environment stays under its parent in trimmed shell snapshots (#34870)

Fixes #30320. Co-authored-by: Theo Browne <me@theo.li> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

Julius Marminge committed Nov 12, 2026 at 03:50 UTC d412871f27a52709c5978c3f9b0a0433242c50f6
4 files changed +49 -13
apps/server/src/orchestration-v2/ProjectionStore.ts
+13 -1
@@ -436,6 +436,8 @@ export interface ActiveShellIndexEntry extends Pick<
436 > {
437 /** False only when no run, request, work item, or watch can make the shell live. */
438 readonly mayBeLive: boolean;
439 + /** Whether it delegated a task to a linked environment, whose clients nest that task under it. */
440 + readonly hasRemoteTasks: boolean;
441 }
442
443 export interface OlderSettledThreadsQuery {
@@ -7745,6 +7747,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7747 readonly pinned_at: string | null;
7748 readonly snoozed_until: string | null;
7749 readonly may_be_live: number;
7750 + readonly has_remote_tasks: number;
7751 }>`
7752 SELECT
7753 t.thread_id,
@@ -7790,7 +7793,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7793 SELECT 1 FROM json_each(t.payload_json, '$.pullRequests') link
7794 WHERE json_type(link.value, '$.watch') IS NOT NULL
7795 )
7793 - ) AS may_be_live
7796 + ) AS may_be_live,
7797 + -- One list for the whole read, found through the child_thread_id index. A lookup per
7798 + -- thread would parse the payload of every subagent row.
7799 + t.thread_id IN (
7800 + SELECT subagent.thread_id FROM orchestration_v2_projection_subagents subagent
7801 + WHERE subagent.origin = 'app_owned'
7802 + AND subagent.child_thread_id IS NULL
7803 + AND json_type(subagent.payload_json, '$.remoteChild') = 'object'
7804 + ) AS has_remote_tasks
7805 FROM orchestration_v2_projection_threads t
7806 WHERE t.deleted_at IS NULL
7807 AND json_extract(t.payload_json, '$.archivedAt') IS NULL
@@ -7817,6 +7828,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7828 pinnedAt: row.pinned_at === null ? null : DateTime.makeUnsafe(row.pinned_at),
7829 snoozedUntil: row.snoozed_until === null ? null : DateTime.makeUnsafe(row.snoozed_until),
7830 mayBeLive: row.may_be_live === 1,
7831 + hasRemoteTasks: row.has_remote_tasks === 1,
7832 }));
7833 }, Effect.mapError(shellSnapshotReadError));
7834
apps/server/src/orchestration-v2/RemoteDelegatedTask.test.ts
+4
@@ -222,6 +222,10 @@ it.layer(layerTest)("a delegated task in a linked environment", (it) => {
222 assert.deepEqual(item?.type === "subagent" ? item.remoteChild : undefined, remoteChild);
223 // No thread was created here for the child.
224 assert.equal(yield* orchestrator.getThreadShell(remoteChild.threadId), null);
225 + const other = yield* seedLiveParent("records-no-task");
226 + const index = yield* (yield* ProjectionStore.ProjectionStoreV2).listActiveShellIndex();
227 + assert.isTrue(index.find((entry) => entry.id === parent.threadId)?.hasRemoteTasks);
228 + assert.isFalse(index.find((entry) => entry.id === other.threadId)?.hasRemoteTasks);
229 }),
230 );
231
apps/server/src/orchestration-v2/ShellStream.ts
+24 -9
@@ -75,7 +75,7 @@ function buildActiveShellSnapshot(input: {
75 type ShellTrimRow = Pick<
76 OrchestrationV2ThreadShell,
77 "id" | "projectId" | "lineage" | "settledOverride" | "settledAt"
78 ->;
78 +> & { readonly hasRemoteTasks?: boolean };
79
80 /**
81 * What a subscriber's trims read across every active row. Computed from full shells or from the
@@ -84,6 +84,8 @@ type ShellTrimRow = Pick<
84 interface ShellTrimFacts {
85 readonly subagentParentIds: ReadonlySet<string>;
86 readonly subagentThreadIds: Set<string>;
87 + /** Only the store's index knows these; shells alone leave it empty. */
88 + readonly remoteTaskParentIds: ReadonlySet<string>;
89 /** Undefined when no thread is settled, so none is older. */
90 readonly olderSettledBeforeMs: number | undefined;
91 readonly olderSettledCounts: ReadonlyArray<OrchestrationV2OlderSettledCount>;
@@ -94,8 +96,10 @@ const NEWEST_SETTLED_KEPT = 25;
96 function shellTrimFacts(rows: ReadonlyArray<ShellTrimRow>): ShellTrimFacts {
97 const subagentParentIds = new Set<string>();
98 const subagentThreadIds = new Set<string>();
99 + const remoteTaskParentIds = new Set<string>();
100 const settledTimes: Array<number> = [];
101 for (const row of rows) {
102 + if (row.hasRemoteTasks === true) remoteTaskParentIds.add(row.id);
103 if (row.lineage.relationshipToParent === "subagent") {
104 subagentThreadIds.add(row.id);
105 if (row.lineage.parentThreadId) subagentParentIds.add(row.lineage.parentThreadId);
@@ -121,6 +125,7 @@ function shellTrimFacts(rows: ReadonlyArray<ShellTrimRow>): ShellTrimFacts {
125 return {
126 subagentParentIds,
127 subagentThreadIds,
128 + remoteTaskParentIds,
129 olderSettledBeforeMs,
130 olderSettledCounts: Array.from(counts, ([projectId, count]) => ({ projectId, count })),
131 };
@@ -140,13 +145,14 @@ function hiddenChildAncestorIds(
145
146 function omitIdleSubagentShellsWith(
147 threads: ReadonlyArray<OrchestrationV2ThreadShell>,
143 - facts: Pick<ShellTrimFacts, "subagentParentIds">,
148 + facts: Pick<ShellTrimFacts, "subagentParentIds" | "remoteTaskParentIds">,
149 ): ReadonlyArray<OrchestrationV2ThreadShell> {
150 return threads.filter(
151 (thread) =>
152 thread.lineage.relationshipToParent !== "subagent" ||
153 isLiveSubagentShell(thread) ||
149 - facts.subagentParentIds.has(thread.id),
154 + facts.subagentParentIds.has(thread.id) ||
155 + facts.remoteTaskParentIds.has(thread.id),
156 );
157 }
158
@@ -156,7 +162,10 @@ function omitOlderSettledShellsWith(
162 readonly snapshotSequence: number;
163 readonly now: DateTime.Utc;
164 },
159 - facts: Pick<ShellTrimFacts, "olderSettledBeforeMs" | "olderSettledCounts">,
165 + facts: Pick<
166 + ShellTrimFacts,
167 + "olderSettledBeforeMs" | "olderSettledCounts" | "remoteTaskParentIds"
168 + >,
169 ): {
170 readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
171 readonly olderSettled: OrchestrationV2OlderSettledThreads;
@@ -170,7 +179,8 @@ function omitOlderSettledShellsWith(
179 thread.pinnedAt != null ||
180 (thread.snoozedUntil != null && DateTime.toEpochMillis(thread.snoozedUntil) > nowMs) ||
181 isLiveSubagentShell(thread) ||
173 - ancestorIds.has(thread.id),
182 + ancestorIds.has(thread.id) ||
183 + facts.remoteTaskParentIds.has(thread.id),
184 );
185 return {
186 threads,
@@ -185,7 +195,8 @@ function omitOlderSettledShellsWith(
195 /**
196 * Idle subagent children have no sidebar row, so clients that opt in read them from their thread
197 * 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.
200 */
201 export function omitIdleSubagentShells(
202 threads: ReadonlyArray<OrchestrationV2ThreadShell>,
@@ -214,8 +225,8 @@ export function activeShellRowsForSubscriber(
225 /**
226 * Opted-in snapshots keep the 25 newest settled top-level threads (and ties) and count the rest per
227 * 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.
230 */
231 export function omitOlderSettledShells(input: {
232 readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
@@ -261,7 +272,10 @@ function shellSubscriberThreadIds(
272 const subagents = index.filter(
273 (entry) =>
274 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)),
279 );
280 const ancestorIds = hiddenChildAncestorIds([
281 ...subagents,
@@ -280,6 +294,7 @@ function shellSubscriberThreadIds(
294 entry.pinnedAt != null ||
295 entry.snoozedUntil != null ||
296 entry.mayBeLive ||
297 + entry.hasRemoteTasks ||
298 ancestorIds.has(entry.id)
299 ? [entry.id]
300 : [],
apps/server/src/ws.test.ts
+8 -3
@@ -708,6 +708,8 @@ it.effect("shell snapshots omit idle children and older settled threads only on
708 shell("live", "parent", "running"),
709 ...Array.from({ length: 25 }, (_, index) => shell(`recent-${index}`, null, null, 1)),
710 shell("old", null, null, 30),
711 + shell("remote-parent", null, null, 40),
712 + shell("remote-child-parent", "parent", null),
713 ];
714 const firstSnapshot = (input: {
715 readonly omitIdleSubagents?: boolean;
@@ -724,6 +726,7 @@ it.effect("shell snapshots omit idle children and older settled threads only on
726 pinnedAt: null,
727 snoozedUntil: null,
728 mayBeLive: thread.activityRunStatus !== null,
729 + hasRemoteTasks: thread.id.startsWith("remote-"),
730 })),
731 ),
732 readShellSnapshot: (options?: { readonly threadIds?: ReadonlyArray<string> }) =>
@@ -767,15 +770,17 @@ it.effect("shell snapshots omit idle children and older settled threads only on
770 const withoutChildren = yield* firstSnapshot({ omitIdleSubagents: true });
771 assert.deepEqual(
772 withoutChildren?.threads.map((thread) => thread.id),
770 - ["parent", "live", ...recent, "old"],
773 + ["parent", "live", ...recent, "old", "remote-parent", "remote-child-parent"],
774 );
775 + // A linked environment's clients nest its task under its parent, so that parent stays: an
776 + // older settled thread or an idle subagent.
777 const withoutOld = yield* firstSnapshot({ omitIdleSubagents: true, omitOlderSettled: true });
778 assert.deepEqual(
779 withoutOld?.threads.map((thread) => thread.id),
775 - ["parent", "live", ...recent],
780 + ["parent", "live", ...recent, "remote-parent", "remote-child-parent"],
781 );
782 assert.deepEqual(withoutOld?.olderSettled?.counts, [
778 - { projectId: ProjectId.make("project"), count: 1 },
783 + { projectId: ProjectId.make("project"), count: 2 },
784 ]);
785 }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
786 );