Skip to content

Commit 89424d9

Browse files
Jreevoclaudejuliusmarminge
authored
fix(server): a Claude /goal no longer reads as complete while a background subagent runs (pingdotgg#16598)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 300a356 commit 89424d9

2 files changed

Lines changed: 156 additions & 1 deletion

File tree

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

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16013,6 +16013,138 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1601316013
),
1601416014
);
1601516015

16016+
it.effect(
16017+
"defers goal completion until an ambient subagent finishes and Claude checks again",
16018+
() =>
16019+
Effect.gen(function* () {
16020+
const harness = yield* makeWakeHarness;
16021+
const condition = "the review finds no bugs";
16022+
const taskId = "task-goal-review";
16023+
const toolUseId = "toolu-goal-review";
16024+
const now = yield* DateTime.now;
16025+
yield* harness.runtime.startTurn(
16026+
makeClaudeTestTurnInput({
16027+
threadId: harness.threadId,
16028+
providerThread: harness.providerThread,
16029+
now,
16030+
attemptId: RunAttemptId.make("goal-background-attempt"),
16031+
text: `/goal ${condition}`,
16032+
attachments: [],
16033+
}),
16034+
);
16035+
for (const frame of [
16036+
syntheticFrame("goal-background-set", `Goal set: ${condition}`),
16037+
claudeSdkFrame({
16038+
...makeSubagentTaskStartedFrame({ taskId, toolUseId, uuid: "goal-review-start" }),
16039+
ambient: true,
16040+
skip_transcript: true,
16041+
}),
16042+
makeAssistantTextFrame({
16043+
uuid: "goal-background-wait",
16044+
text: "Waiting for the review.",
16045+
}),
16046+
makeResultFrame({ uuid: "goal-background-result", result: "Waiting for the review." }),
16047+
]) {
16048+
yield* Queue.offer(harness.sdkMessages, frame);
16049+
}
16050+
assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
16051+
assert.deepEqual(goalStatuses(harness.events).at(-1), {
16052+
objective: condition,
16053+
status: "active",
16054+
checks: 0,
16055+
});
16056+
16057+
yield* harness.offerAndWait(
16058+
makeSubagentNotificationFrame({
16059+
taskId,
16060+
toolUseId,
16061+
summary: "No bugs found.",
16062+
uuid: "goal-review-done",
16063+
}),
16064+
);
16065+
yield* harness.offerAndWait(wakeTurnInit);
16066+
yield* Queue.take(harness.continuationReceipts);
16067+
assert.equal(goalStatuses(harness.events).at(-1)?.status, "active");
16068+
yield* harness.runtime.startTurn(
16069+
makeClaudeTestTurnInput({
16070+
threadId: harness.threadId,
16071+
providerThread: harness.providerThread,
16072+
now,
16073+
attemptId: RunAttemptId.make("goal-background-wake"),
16074+
text: "Background task completed.",
16075+
attachments: [],
16076+
providerTurnOrdinal: 2,
16077+
messageCreatedBy: "agent",
16078+
messageCreationSource: "provider",
16079+
}),
16080+
);
16081+
yield* harness.offerAndWait(
16082+
makeAssistantTextFrame({ uuid: "goal-background-wake-work", text: "No bugs found." }),
16083+
);
16084+
yield* harness.offerAndWait(
16085+
makeResultFrame({
16086+
uuid: "goal-background-wake-result",
16087+
result: "No bugs found.",
16088+
origin: { kind: "task-notification" },
16089+
}),
16090+
);
16091+
assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
16092+
assert.deepEqual(goalStatuses(harness.events).at(-1), {
16093+
objective: condition,
16094+
status: "complete",
16095+
checks: 0,
16096+
});
16097+
}).pipe(
16098+
Effect.scoped,
16099+
Effect.provide(
16100+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
16101+
),
16102+
),
16103+
);
16104+
16105+
it.effect.each([
16106+
{ taskType: "mcp_task", status: "complete" },
16107+
{ taskType: undefined, status: "active" },
16108+
] as const)("leaves the goal $status with a running $taskType task", ({ taskType, status }) =>
16109+
Effect.gen(function* () {
16110+
const harness = yield* makeWakeHarness;
16111+
const condition = "the docs build";
16112+
yield* harness.runtime.startTurn(
16113+
makeClaudeTestTurnInput({
16114+
threadId: harness.threadId,
16115+
providerThread: harness.providerThread,
16116+
now: yield* DateTime.now,
16117+
attemptId: RunAttemptId.make("goal-task-type-attempt"),
16118+
text: `/goal ${condition}`,
16119+
attachments: [],
16120+
}),
16121+
);
16122+
for (const frame of [
16123+
syntheticFrame("goal-task-type-set", `Goal set: ${condition}`),
16124+
claudeSdkFrame({
16125+
type: "system",
16126+
subtype: "task_started",
16127+
task_id: "task-goal-docs",
16128+
description: "Watch the docs",
16129+
...(taskType === undefined ? {} : { task_type: taskType }),
16130+
uuid: "goal-task-type-start",
16131+
session_id: WAKE_NATIVE_SESSION,
16132+
}),
16133+
makeAssistantTextFrame({ uuid: "goal-task-type-work", text: "The docs build." }),
16134+
makeResultFrame({ uuid: "goal-task-type-result", result: "The docs build." }),
16135+
]) {
16136+
yield* Queue.offer(harness.sdkMessages, frame);
16137+
}
16138+
assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed");
16139+
assert.equal(goalStatuses(harness.events).at(-1)?.status, status);
16140+
}).pipe(
16141+
Effect.scoped,
16142+
Effect.provide(
16143+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
16144+
),
16145+
),
16146+
);
16147+
1601616148
it.effect("keeps a goal active after a command turn with no model output", () =>
1601716149
Effect.gen(function* () {
1601816150
const harness = yield* makeWakeHarness;

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2078,6 +2078,15 @@ const CLAUDE_OPAQUE_BACKGROUND_TASK_KINDS: ReadonlyMap<
20782078
Exclude<OrchestrationV2PendingBackgroundTask["kind"], "subagent">
20792079
> = new Map([["local_bash", "command"]]);
20802080

2081+
// Claude Code 2.1.292 defers its goal Stop hook for these subagent types.
2082+
// local_bash tasks defer it through the opaque-task roster.
2083+
const CLAUDE_GOAL_DEFERRING_TASK_TYPES: ReadonlySet<string> = new Set([
2084+
"local_agent",
2085+
"remote_agent",
2086+
"in_process_teammate",
2087+
"local_workflow",
2088+
]);
2089+
20812090
function isClaudeOpaqueBackgroundTaskType(taskType: string | null | undefined): boolean {
20822091
return typeof taskType === "string" && CLAUDE_OPAQUE_BACKGROUND_TASK_KINDS.has(taskType);
20832092
}
@@ -3079,6 +3088,7 @@ interface ActiveClaudeProviderRetry {
30793088

30803089
interface ActiveClaudeSubagent {
30813090
task: OrchestrationV2Subagent;
3091+
readonly nativeTaskType: string | undefined;
30823092
readonly confirmedOutcome: ProviderAdapter.ProviderAdapterV2SubagentOutcome | undefined;
30833093
readonly rootNodeId: OrchestrationV2ExecutionNode["id"];
30843094
readonly childThreadId: ThreadId;
@@ -4504,6 +4514,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
45044514
const ids = claudeSubagentIds(resume.context, resume.taskId);
45054515
const now = yield* DateTime.now;
45064516
const subagent: ActiveClaudeSubagent = {
4517+
nativeTaskType: undefined,
45074518
confirmedOutcome: undefined,
45084519
childThread: null,
45094520
task: {
@@ -4985,6 +4996,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
49854996
readonly model?: string;
49864997
readonly effort?: string;
49874998
readonly owner?: ActiveClaudeSubagent;
4999+
readonly nativeTaskType?: string;
49885000
readonly progress?: string;
49895001
readonly workflowFrame?: unknown;
49905002
readonly workflowName?: string;
@@ -5143,6 +5155,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
51435155
} satisfies OrchestrationV2Subagent;
51445156
const subagent = {
51455157
task,
5158+
nativeTaskType: input.nativeTaskType ?? existingSubagent?.nativeTaskType,
51465159
confirmedOutcome:
51475160
input.nativeOutcome !== undefined
51485161
? confirmedSubagentOutcome(task)
@@ -6275,13 +6288,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
62756288
nativeThreadId === null ? undefined : goalsByNativeThread.get(nativeThreadId);
62766289
const goalChecked = goalCheckedTurns.delete(input.context.providerTurnId);
62776290
const terminalReason = input.result?.terminal_reason;
6291+
const liveQuery = yield* Ref.get(queryContext);
62786292
if (
62796293
nativeThreadId !== null &&
62806294
goal?.status === "active" &&
62816295
goalChecked &&
62826296
input.status === "completed" &&
62836297
(terminalReason === undefined || terminalReason === "completed") &&
6284-
roster.size === 0
6298+
roster.size === 0 &&
6299+
!(
6300+
liveQuery?.nativeThreadId === nativeThreadId &&
6301+
(yield* liveProcessRunsBackgroundWork(liveQuery, { forGoalCheck: true }))
6302+
)
62856303
) {
62866304
goalsByNativeThread.set(nativeThreadId, {
62876305
objective: goal.objective,
@@ -7533,6 +7551,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
75337551
yield* updateClaudeSubagentNode({
75347552
context,
75357553
taskId: message.task_id,
7554+
...(message.task_type === undefined ? {} : { nativeTaskType: message.task_type }),
75367555
...(message.tool_use_id === undefined ? {} : { toolUseId: message.tool_use_id }),
75377556
...(message.prompt === undefined ? {} : { prompt: message.prompt }),
75387557
...(model === undefined ? {} : { model }),
@@ -8472,6 +8491,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
84728491

84738492
const liveProcessRunsBackgroundWork = Effect.fnUntraced(function* (
84748493
live: ClaudeLiveQueryContext,
8494+
options?: { readonly forGoalCheck?: boolean },
84758495
) {
84768496
if (
84778497
rosterForNativeThread(
@@ -8486,6 +8506,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
84868506
if (
84878507
subagent.task.status === "running" &&
84888508
!live.subagentsFromEarlierProcesses.has(subagent) &&
8509+
(options?.forGoalCheck !== true ||
8510+
subagent.nativeTaskType === undefined ||
8511+
CLAUDE_GOAL_DEFERRING_TASK_TYPES.has(subagent.nativeTaskType)) &&
84898512
!buffered.some(
84908513
(message) =>
84918514
message.type === "system" &&

0 commit comments

Comments
 (0)