Skip to content

Commit 01eaf66

Browse files
committed
fix(server): include native subagents in handoff recovery preflight
1 parent e946d30 commit 01eaf66

5 files changed

Lines changed: 51 additions & 8 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12131,6 +12131,25 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1213112131
yield* changes.until(() => terminals().length === 1);
1213212132
const settledTurn = terminals()[0]!;
1213312133

12134+
assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(providerThread));
12135+
assert.isTrue(
12136+
yield* runtime.hasPendingBackgroundWorkForThread!(providerThread, {
12137+
includeSubagents: true,
12138+
}),
12139+
);
12140+
assert.isFalse(
12141+
yield* runtime.hasPendingBackgroundWorkForThread!(
12142+
{
12143+
...providerThread,
12144+
nativeThreadRef: {
12145+
...providerThread.nativeThreadRef!,
12146+
nativeId: "sibling-native-session",
12147+
},
12148+
},
12149+
{ includeSubagents: true },
12150+
),
12151+
);
12152+
1213412153
const alternateModel = CLAUDE_TEST_RESTART_MODEL_SELECTION;
1213512154
const switchTurn = (attempt: string) =>
1213612155
runtime.startTurn(

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10566,11 +10566,20 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1056610566
}),
1056710567
hasPendingBackgroundWorkForThread: Effect.fn(
1056810568
"ClaudeAdapterV2.hasPendingBackgroundWorkForThread",
10569-
)(function* (providerThread) {
10569+
)(function* (providerThread, options) {
1057010570
const nativeThreadId = providerThread.nativeThreadRef?.nativeId;
1057110571
if (nativeThreadId === undefined || nativeThreadId === null) {
1057210572
return false;
1057310573
}
10574+
if (options?.includeSubagents) {
10575+
const live = yield* Ref.get(queryContext);
10576+
if (
10577+
live?.nativeThreadId === nativeThreadId &&
10578+
!live.stopping &&
10579+
(yield* liveProcessRunsBackgroundWork(live))
10580+
)
10581+
return true;
10582+
}
1057410583
return (
1057510584
rosterForNativeThread(
1057610585
yield* Ref.get(pendingBackgroundTasksByNativeThread),

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,9 @@ export const layer: Layer.Layer<
10781078
);
10791079
if (!fitsFresh) return resumed.success;
10801080
const hasPendingWork = yield* (
1081-
session.hasPendingBackgroundWorkForThread?.(resumed.success) ??
1081+
session.hasPendingBackgroundWorkForThread?.(resumed.success, {
1082+
includeSubagents: true,
1083+
}) ??
10821084
session.hasPendingBackgroundWork ??
10831085
Effect.succeed(false)
10841086
);

‎apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ function makeTestAdapter(input: {
115115
readonly interruptedRunOrdinals?: ReadonlySet<number>;
116116
readonly holdRunOrdinal?: number;
117117
readonly pendingBackgroundWork?: Ref.Ref<boolean>;
118+
readonly subagentOnlyBackgroundWork?: boolean;
118119
readonly holdFirstTurn?: Deferred.Deferred<void>;
119120
readonly releaseFirstTurn?: Deferred.Deferred<void>;
120121
}): ProviderAdapter.ProviderAdapterV2["Service"] {
@@ -149,7 +150,10 @@ function makeTestAdapter(input: {
149150
...(input.pendingBackgroundWork === undefined
150151
? {}
151152
: {
152-
hasPendingBackgroundWorkForThread: () => Ref.get(input.pendingBackgroundWork!),
153+
hasPendingBackgroundWorkForThread: (_thread, options) =>
154+
input.subagentOnlyBackgroundWork && options?.includeSubagents !== true
155+
? Effect.succeed(false)
156+
: Ref.get(input.pendingBackgroundWork!),
153157
}),
154158
...(input.getModelContextWindow === undefined
155159
? {}
@@ -424,6 +428,7 @@ describe("orchestration v2 provider switching", () => {
424428
"screenshot-exhausted-target-oversized-native",
425429
"screenshot-exhausted-target-tight-native",
426430
"screenshot-exhausted-target-background-native",
431+
"screenshot-exhausted-target-subagent-native",
427432
"prior-images-reasoning-change-replacement-small-native",
428433
"imported-prior-images-fallback",
429434
"prior-images-telemetry-native",
@@ -484,7 +489,12 @@ describe("orchestration v2 provider switching", () => {
484489
...(scenario.includes("retry") || scenario.includes("unsent") ? { failStartOnce } : {}),
485490
...(replaceNative ? { failResumeOnce, nativeThreadGeneration: generation } : {}),
486491
...(exhaustedTarget ? { nativeThreadGeneration: generation } : {}),
487-
...(scenario.includes("target-background") ? { pendingBackgroundWork } : {}),
492+
...(scenario.includes("target-background") || scenario.includes("target-subagent")
493+
? {
494+
pendingBackgroundWork,
495+
subagentOnlyBackgroundWork: scenario.includes("target-subagent"),
496+
}
497+
: {}),
488498
...(scenario.includes("reported-capacity")
489499
? { initialContextUsage: { usedTokens: 999_999, maxTokens: 1_000_000 } }
490500
: {}),
@@ -744,7 +754,8 @@ describe("orchestration v2 provider switching", () => {
744754
yield* Ref.set(failResumeOnce, true);
745755
}
746756
if (scenario.includes("retry")) yield* Ref.set(failStartOnce, true);
747-
if (scenario.includes("target-background")) yield* Ref.set(pendingBackgroundWork, true);
757+
if (scenario.includes("target-background") || scenario.includes("target-subagent"))
758+
yield* Ref.set(pendingBackgroundWork, true);
748759
yield* dispatch(targetOrdinal, current, targetSelection);
749760
yield* wait(targetOrdinal);
750761
if (scenario.includes("retry")) {
@@ -833,13 +844,14 @@ describe("orchestration v2 provider switching", () => {
833844
if (
834845
scenario.includes("target-oversized") ||
835846
scenario.includes("target-tight") ||
836-
scenario.includes("target-background")
847+
scenario.includes("target-background") ||
848+
scenario.includes("target-subagent")
837849
) {
838850
assert.equal(projection.runs.at(-1)?.status, "failed");
839851
assert.equal(yield* Ref.get(generation), 1);
840852
assert.equal(projection.messages.at(-1)!.text, current);
841853
assert.deepEqual(projection.messages.at(-1)!.attachments, screenshots);
842-
if (scenario.includes("target-background")) {
854+
if (scenario.includes("target-background") || scenario.includes("target-subagent")) {
843855
assert.isTrue(yield* Ref.get(pendingBackgroundWork));
844856
const failure = projection.turnItems.findLast((item) => item.type === "error");
845857
assert.equal(

‎packages/provider-core/src/server/ProviderAdapter.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,9 +550,10 @@ export interface ProviderAdapterV2SessionRuntime {
550550
/** Manager runtimes broadcast independent subscriptions; adapters may expose only a single-consumer stream. */
551551
readonly subscribeEvents?: Effect.Effect<ProviderAdapterV2EventSubscription>;
552552
readonly hasPendingBackgroundWork?: Effect.Effect<boolean>;
553-
/** Overrides the session-wide probe so sibling threads cannot keep this root subscription open. */
553+
/** Root-subscription probe; replacement checks opt in to subagents owned by the live process. */
554554
readonly hasPendingBackgroundWorkForThread?: (
555555
providerThread: OrchestrationV2ProviderThread,
556+
options?: { readonly includeSubagents?: boolean },
556557
) => Effect.Effect<boolean>;
557558
/** Set only if all session background tasks end with its process; otherwise threads must keep Waiting. */
558559
readonly backgroundTasksEndWithProcess?: true;

0 commit comments

Comments
 (0)