Skip to content

Commit d412871

Browse files
juliusmarminget3dotggclaude
authored
fix(server): a task on a linked environment stays under its parent in trimmed shell snapshots (#34870)
Fixes #30320. Co-authored-by: Theo Browne <[email protected]> Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 1032135 commit d412871

4 files changed

Lines changed: 49 additions & 13 deletions

File tree

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ export interface ActiveShellIndexEntry extends Pick<
436436
> {
437437
/** False only when no run, request, work item, or watch can make the shell live. */
438438
readonly mayBeLive: boolean;
439+
/** Whether it delegated a task to a linked environment, whose clients nest that task under it. */
440+
readonly hasRemoteTasks: boolean;
439441
}
440442

441443
export interface OlderSettledThreadsQuery {
@@ -7745,6 +7747,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
77457747
readonly pinned_at: string | null;
77467748
readonly snoozed_until: string | null;
77477749
readonly may_be_live: number;
7750+
readonly has_remote_tasks: number;
77487751
}>`
77497752
SELECT
77507753
t.thread_id,
@@ -7790,7 +7793,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
77907793
SELECT 1 FROM json_each(t.payload_json, '$.pullRequests') link
77917794
WHERE json_type(link.value, '$.watch') IS NOT NULL
77927795
)
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
77947805
FROM orchestration_v2_projection_threads t
77957806
WHERE t.deleted_at IS NULL
77967807
AND json_extract(t.payload_json, '$.archivedAt') IS NULL
@@ -7817,6 +7828,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
78177828
pinnedAt: row.pinned_at === null ? null : DateTime.makeUnsafe(row.pinned_at),
78187829
snoozedUntil: row.snoozed_until === null ? null : DateTime.makeUnsafe(row.snoozed_until),
78197830
mayBeLive: row.may_be_live === 1,
7831+
hasRemoteTasks: row.has_remote_tasks === 1,
78207832
}));
78217833
}, Effect.mapError(shellSnapshotReadError));
78227834

‎apps/server/src/orchestration-v2/RemoteDelegatedTask.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ it.layer(layerTest)("a delegated task in a linked environment", (it) => {
222222
assert.deepEqual(item?.type === "subagent" ? item.remoteChild : undefined, remoteChild);
223223
// No thread was created here for the child.
224224
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);
225229
}),
226230
);
227231

‎apps/server/src/orchestration-v2/ShellStream.ts‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function buildActiveShellSnapshot(input: {
7575
type 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<
8484
interface 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;
9496
function 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

141146
function 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
*/
190201
export 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
*/
220231
export 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
: [],

‎apps/server/src/ws.test.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,8 @@ it.effect("shell snapshots omit idle children and older settled threads only on
708708
shell("live", "parent", "running"),
709709
...Array.from({ length: 25 }, (_, index) => shell(`recent-${index}`, null, null, 1)),
710710
shell("old", null, null, 30),
711+
shell("remote-parent", null, null, 40),
712+
shell("remote-child-parent", "parent", null),
711713
];
712714
const firstSnapshot = (input: {
713715
readonly omitIdleSubagents?: boolean;
@@ -724,6 +726,7 @@ it.effect("shell snapshots omit idle children and older settled threads only on
724726
pinnedAt: null,
725727
snoozedUntil: null,
726728
mayBeLive: thread.activityRunStatus !== null,
729+
hasRemoteTasks: thread.id.startsWith("remote-"),
727730
})),
728731
),
729732
readShellSnapshot: (options?: { readonly threadIds?: ReadonlyArray<string> }) =>
@@ -767,15 +770,17 @@ it.effect("shell snapshots omit idle children and older settled threads only on
767770
const withoutChildren = yield* firstSnapshot({ omitIdleSubagents: true });
768771
assert.deepEqual(
769772
withoutChildren?.threads.map((thread) => thread.id),
770-
["parent", "live", ...recent, "old"],
773+
["parent", "live", ...recent, "old", "remote-parent", "remote-child-parent"],
771774
);
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.
772777
const withoutOld = yield* firstSnapshot({ omitIdleSubagents: true, omitOlderSettled: true });
773778
assert.deepEqual(
774779
withoutOld?.threads.map((thread) => thread.id),
775-
["parent", "live", ...recent],
780+
["parent", "live", ...recent, "remote-parent", "remote-child-parent"],
776781
);
777782
assert.deepEqual(withoutOld?.olderSettled?.counts, [
778-
{ projectId: ProjectId.make("project"), count: 1 },
783+
{ projectId: ProjectId.make("project"), count: 2 },
779784
]);
780785
}).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
781786
);

0 commit comments

Comments
 (0)