@devries-cameron20 / t3code Mirror · read-only / commits / 910d4590d0
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 13:30 UTC

fix(server): drain queued native child continuations (#21461)

Co-authored-by: Exotic209093 <134711311+Exotic209093@users.noreply.github.com> Co-authored-by: Issue triage verification <triage@localhost>

Julius Marminge committed Nov 14, 2026 at 16:53 UTC 910d4590d0d0feaebea21a171ee118a31a4da0e1
2 files changed +191 -2
apps/server/src/orchestration-v2/RunExecutionService.test.ts
+177 -1
@@ -1924,6 +1924,89 @@ it.effect("ends a nested native subagent and its thread when the root run is int
1924 }),
1925 );
1926
1927 +it.effect("drains queued child output after the root loses provider-thread ownership", () =>
1928 + Effect.gen(function* () {
1929 + const childEvents: Array<string> = [];
1930 + yield* runBackgroundItemScenario(
1931 + "queued-child-output",
1932 + (ids) => {
1933 + const childProviderThreadId = ProviderThreadId.make(
1934 + "provider-thread:queued-child-output:child",
1935 + );
1936 + const snapshot = (child: boolean, pendingBackgroundWork: boolean) => ({
1937 + type: "provider_thread.updated" as const,
1938 + driver,
1939 + providerThread: {
1940 + id: child ? childProviderThreadId : ids.providerThreadId,
1941 + driver,
1942 + providerInstanceId: ProviderInstanceId.make("codex"),
1943 + providerSessionId: null,
1944 + appThreadId: child ? ids.childThreadId : ids.threadId,
1945 + ownerNodeId: null,
1946 + nativeThreadRef: null,
1947 + nativeConversationHeadRef: null,
1948 + status: "idle",
1949 + firstRunOrdinal: 1,
1950 + lastRunOrdinal: 1,
1951 + handoffIds: [],
1952 + forkedFrom: null,
1953 + pendingBackgroundTasks: [],
1954 + createdAt: DateTime.makeUnsafe(0),
1955 + updatedAt: DateTime.makeUnsafe(0),
1956 + } satisfies OrchestrationV2ProviderThread,
1957 + pendingBackgroundWork,
1958 + });
1959 + const childTurn = (status: "running" | "completed") => ({
1960 + type: "provider_turn.updated" as const,
1961 + driver,
1962 + threadId: ids.childThreadId,
1963 + providerTurn: {
1964 + id: ProviderTurnId.make("provider-turn:queued-child-output:child"),
1965 + providerThreadId: childProviderThreadId,
1966 + runAttemptId: null,
1967 + ordinal: 2,
1968 + status,
1969 + } as OrchestrationV2ProviderTurn,
1970 + });
1971 + const message = (text: string) =>
1972 + ({
1973 + type: "message.updated",
1974 + driver,
1975 + message: { threadId: ids.childThreadId, runId: null, text },
1976 + }) as ProviderAdapter.ProviderAdapterV2Event;
1977 + return [
1978 + childThreadCreatedEvent(ids),
1979 + snapshot(false, true),
1980 + snapshot(true, true),
1981 + subagentEvent(ids, "running"),
1982 + rootTerminalEvent(ids, "completed"),
1983 + snapshot(false, true),
1984 + subagentEvent(ids, "completed"),
1985 + childTurn("running"),
1986 + message("GRANDCHILD_OK"),
1987 + childTurn("completed"),
1988 + snapshot(true, false),
1989 + snapshot(false, false),
1990 + message("after-drain"),
1991 + ];
1992 + },
1993 + {
1994 + keepEventStreamOpen: true,
1995 + hasPendingBackgroundWorkForThread: () => Effect.succeed(false),
1996 + providerThreadOwnerLost: true,
1997 + onIngest: (event) =>
1998 + Effect.sync(() => {
1999 + if (event.type === "provider_turn.updated") {
2000 + childEvents.push(event.providerTurn.status);
2001 + }
2002 + if (event.type === "message.updated") childEvents.push(event.message.text);
2003 + }),
2004 + },
2005 + );
2006 + assert.deepEqual(childEvents, ["running", "GRANDCHILD_OK", "completed"]);
2007 + }),
2008 +);
2009 +
2010 it.effect("keeps ingesting until queued background work clears after the probe goes idle", () =>
2011 Effect.gen(function* () {
2012 // The adapter's probe runs ahead of its event queue, so only the queued update may release.
@@ -2105,6 +2188,93 @@ it.effect.each([null, "run:nested-sdk"] as const)(
2188 }),
2189 );
2190
2191 +it.effect.each(["none", "sibling", "grandchild"] as const)(
2192 + "drops a transferred child's pending snapshot but keeps other owned pending work (%s)",
2193 + (otherPending) =>
2194 + Effect.gen(function* () {
2195 + const consumed = yield* Deferred.make<void>();
2196 + const result = yield* Effect.raceFirst(
2197 + runBackgroundItemScenario(
2198 + "child-transfer-pending",
2199 + (ids) => {
2200 + const childProviderThreadId = ProviderThreadId.make(
2201 + "provider-thread:child-transfer-pending:child",
2202 + );
2203 + const otherThreadId = ThreadId.make("thread:child-transfer-pending:other");
2204 + const snapshot = (
2205 + providerThreadId: ProviderThreadId,
2206 + appThreadId: ThreadId,
2207 + pendingBackgroundWork: boolean,
2208 + ) => ({
2209 + type: "provider_thread.updated" as const,
2210 + driver,
2211 + providerThread: {
2212 + id: providerThreadId,
2213 + appThreadId,
2214 + status: "idle",
2215 + } as OrchestrationV2ProviderThread,
2216 + pendingBackgroundWork,
2217 + });
2218 + const running = subagentEvent(ids, "running");
2219 + if (running.type !== "subagent.updated") throw new Error("Missing subagent");
2220 + const linked = {
2221 + ...running,
2222 + subagent: {
2223 + ...running.subagent,
2224 + origin: "provider_native" as const,
2225 + childThreadId: ids.childThreadId,
2226 + providerThreadId: childProviderThreadId,
2227 + },
2228 + };
2229 + // A grandchild stays with this run when its parent moves; a sibling was never the child's.
2230 + const other =
2231 + otherPending === "none"
2232 + ? []
2233 + : [
2234 + {
2235 + ...childThreadCreatedEvent(ids),
2236 + appThread: {
2237 + id: otherThreadId,
2238 + lineage: {
2239 + parentThreadId:
2240 + otherPending === "grandchild" ? ids.childThreadId : ids.threadId,
2241 + relationshipToParent: "subagent",
2242 + rootThreadId: ids.threadId,
2243 + },
2244 + },
2245 + } as ProviderAdapter.ProviderAdapterV2Event,
2246 + snapshot(
2247 + ProviderThreadId.make("provider-thread:child-transfer-pending:other"),
2248 + otherThreadId,
2249 + true,
2250 + ),
2251 + ];
2252 + return [
2253 + childThreadCreatedEvent(ids),
2254 + linked,
2255 + snapshot(childProviderThreadId, ids.childThreadId, true),
2256 + ...other,
2257 + rootTerminalEvent(ids, "completed"),
2258 + { ...linked, subagent: { ...linked.subagent, runId: RunId.make("run:replacement") } },
2259 + snapshot(childProviderThreadId, ids.childThreadId, false),
2260 + ];
2261 + },
2262 + {
2263 + keepEventStreamOpen: true,
2264 + hasPendingBackgroundWorkForThread: () => Effect.succeed(false),
2265 + onEventsConsumed: Deferred.succeed(consumed, undefined).pipe(Effect.asVoid),
2266 + },
2267 + ).pipe(Effect.asSome),
2268 + Deferred.await(consumed).pipe(Effect.as(Option.none())),
2269 + );
2270 + assert.strictEqual(
2271 + Option.isSome(result),
2272 + otherPending === "none",
2273 + "only pending work this run still owns may keep the prior subscription open",
2274 + );
2275 + }),
2276 +);
2277 +
2278 // The handover event is not this run's, yet it is what settles the run's agent turns.
2279 it.effect(
2280 "ends preview activity when a transferred child settles a run kept open by background work",
@@ -4099,6 +4269,8 @@ function runBackgroundItemScenario(
4269 >;
4270 readonly onSubscribe?: Effect.Effect<void>;
4271 readonly onEventsConsumed?: Effect.Effect<void>;
4272 + // Owner-gated root snapshots store nothing, as after a newer run claims the thread.
4273 + readonly providerThreadOwnerLost?: boolean;
4274 readonly afterEvents?: (
4275 activity: PreviewAgentActivity.PreviewAgentActivity["Service"],
4276 ) => Effect.Effect<void>;
@@ -4177,7 +4349,11 @@ function runBackgroundItemScenario(
4349 ]);
4350 }
4351 // Only the count of stored events matters to the run.
4180 - return event.type === "provider_thread.updated"
4352 + return event.type === "provider_thread.updated" &&
4353 + !(
4354 + options?.providerThreadOwnerLost === true &&
4355 + input.writeIfProviderThreadOwner !== undefined
4356 + )
4357 ? [{} as OrchestrationV2StoredEvent]
4358 : [];
4359 });
apps/server/src/orchestration-v2/RunExecutionService.ts
+14 -1
@@ -1134,6 +1134,9 @@ export const layer: Layer.Layer<
1134 const openRunOwnedSubagents = yield* Ref.make(emptyOpenRunOwnedSubagentProjection());
1135 // The last stored root snapshot's pendingBackgroundWork; adapters that omit it leave it false.
1136 let rootSnapshotPendingWork = false;
1137 + // Owned child threads whose last snapshot reported pending work, such as a queued native
1138 + // continuation. Unlike the root's, these must hold the stream past owner loss.
1139 + const childSnapshotPendingWork = new Set<ProviderThreadId>();
1140 let rootTurnOwnershipChecked = false;
1141 const assistantStreaming = makeAssistantStreamingFilter(responseStreamingMode);
1142 // Set when ingestion ends or finalization settles the run's open items, which a later
@@ -1382,7 +1385,7 @@ export const layer: Layer.Layer<
1385 // while these sets are non-empty: turn_item.updated writes are not ownership-gated, so
1386 // late completions still land.
1387 const backgroundItems = yield* Ref.get(activeBackgroundTurnItems);
1385 - if (backgroundItems.size > 0) {
1388 + if (backgroundItems.size > 0 || childSnapshotPendingWork.size > 0) {
1389 return false;
1390 }
1391 if (yield* Ref.get(providerThreadOwnerLost)) {
@@ -1481,6 +1484,10 @@ export const layer: Layer.Layer<
1484 pendingBackgroundProviderTurns.delete(id);
1485 }
1486 }
1487 + // Routing now rejects the moved thread's snapshots, so none would clear its entry.
1488 + for (const id of childSnapshotPendingWork) {
1489 + if (!routing.ownedProviderThreadIds.has(id)) childSnapshotPendingWork.delete(id);
1490 + }
1491 yield* Ref.update(
1492 activeBackgroundTurnItems,
1493 (current) => new Set([...current].filter((id) => !detached.itemIds.has(id))),
@@ -1682,6 +1689,12 @@ export const layer: Layer.Layer<
1689 if (event.providerThread.id === input.providerThread.id && storedEventCount > 0) {
1690 yield* Ref.set(latestProviderThread, event.providerThread);
1691 rootSnapshotPendingWork = event.pendingBackgroundWork === true;
1692 + } else if (event.providerThread.id !== input.providerThread.id) {
1693 + if (event.pendingBackgroundWork === true) {
1694 + childSnapshotPendingWork.add(event.providerThread.id);
1695 + } else if (event.pendingBackgroundWork === false) {
1696 + childSnapshotPendingWork.delete(event.providerThread.id);
1697 + }
1698 }
1699 }
1700 if (