GeezerVison is showing github.com — fix(server): a Claude /goal no longer reads as complete while a backg… · Pipyakas/t3code@8eac722
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2/Adapters
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts +132Lines changed: 132 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15419,6 +15419,138 @@ describe("ClaudeAdapterV2 background wake turns", () => { | 15419 | 15419 | ), | 15420 | 15420 | ); | 15421 | 15421 |
| | 15422 | + it.effect( | | 15423 | + "defers goal completion until an ambient subagent finishes and Claude checks again", | | 15424 | + () => | | 15425 | + Effect.gen(function* () { | | 15426 | + const harness = yield* makeWakeHarness; | | 15427 | + const condition = "the review finds no bugs"; | | 15428 | + const taskId = "task-goal-review"; | | 15429 | + const toolUseId = "toolu-goal-review"; | | 15430 | + const now = yield* DateTime.now; | | 15431 | + yield* harness.runtime.startTurn( | | 15432 | + makeClaudeTestTurnInput({ | | 15433 | + threadId: harness.threadId, | | 15434 | + providerThread: harness.providerThread, | | 15435 | + now, | | 15436 | + attemptId: RunAttemptId.make("goal-background-attempt"), | | 15437 | + text: `/goal ${condition}`, | | 15438 | + attachments: [], | | 15439 | + }), | | 15440 | + ); | | 15441 | + for (const frame of [ | | 15442 | + syntheticFrame("goal-background-set", `Goal set: ${condition}`), | | 15443 | + claudeSdkFrame({ | | 15444 | + ...makeSubagentTaskStartedFrame({ taskId, toolUseId, uuid: "goal-review-start" }), | | 15445 | + ambient: true, | | 15446 | + skip_transcript: true, | | 15447 | + }), | | 15448 | + makeAssistantTextFrame({ | | 15449 | + uuid: "goal-background-wait", | | 15450 | + text: "Waiting for the review.", | | 15451 | + }), | | 15452 | + makeResultFrame({ uuid: "goal-background-result", result: "Waiting for the review." }), | | 15453 | + ]) { | | 15454 | + yield* Queue.offer(harness.sdkMessages, frame); | | 15455 | + } | | 15456 | + assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed"); | | 15457 | + assert.deepEqual(goalStatuses(harness.events).at(-1), { | | 15458 | + objective: condition, | | 15459 | + status: "active", | | 15460 | + checks: 0, | | 15461 | + }); | | 15462 | + | | 15463 | + yield* harness.offerAndWait( | | 15464 | + makeSubagentNotificationFrame({ | | 15465 | + taskId, | | 15466 | + toolUseId, | | 15467 | + summary: "No bugs found.", | | 15468 | + uuid: "goal-review-done", | | 15469 | + }), | | 15470 | + ); | | 15471 | + yield* harness.offerAndWait(wakeTurnInit); | | 15472 | + yield* Queue.take(harness.continuationReceipts); | | 15473 | + assert.equal(goalStatuses(harness.events).at(-1)?.status, "active"); | | 15474 | + yield* harness.runtime.startTurn( | | 15475 | + makeClaudeTestTurnInput({ | | 15476 | + threadId: harness.threadId, | | 15477 | + providerThread: harness.providerThread, | | 15478 | + now, | | 15479 | + attemptId: RunAttemptId.make("goal-background-wake"), | | 15480 | + text: "Background task completed.", | | 15481 | + attachments: [], | | 15482 | + providerTurnOrdinal: 2, | | 15483 | + messageCreatedBy: "agent", | | 15484 | + messageCreationSource: "provider", | | 15485 | + }), | | 15486 | + ); | | 15487 | + yield* harness.offerAndWait( | | 15488 | + makeAssistantTextFrame({ uuid: "goal-background-wake-work", text: "No bugs found." }), | | 15489 | + ); | | 15490 | + yield* harness.offerAndWait( | | 15491 | + makeResultFrame({ | | 15492 | + uuid: "goal-background-wake-result", | | 15493 | + result: "No bugs found.", | | 15494 | + origin: { kind: "task-notification" }, | | 15495 | + }), | | 15496 | + ); | | 15497 | + assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed"); | | 15498 | + assert.deepEqual(goalStatuses(harness.events).at(-1), { | | 15499 | + objective: condition, | | 15500 | + status: "complete", | | 15501 | + checks: 0, | | 15502 | + }); | | 15503 | + }).pipe( | | 15504 | + Effect.scoped, | | 15505 | + Effect.provide( | | 15506 | + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), | | 15507 | + ), | | 15508 | + ), | | 15509 | + ); | | 15510 | + | | 15511 | + it.effect.each([ | | 15512 | + { taskType: "mcp_task", status: "complete" }, | | 15513 | + { taskType: undefined, status: "active" }, | | 15514 | + ] as const)("leaves the goal $status with a running $taskType task", ({ taskType, status }) => | | 15515 | + Effect.gen(function* () { | | 15516 | + const harness = yield* makeWakeHarness; | | 15517 | + const condition = "the docs build"; | | 15518 | + yield* harness.runtime.startTurn( | | 15519 | + makeClaudeTestTurnInput({ | | 15520 | + threadId: harness.threadId, | | 15521 | + providerThread: harness.providerThread, | | 15522 | + now: yield* DateTime.now, | | 15523 | + attemptId: RunAttemptId.make("goal-task-type-attempt"), | | 15524 | + text: `/goal ${condition}`, | | 15525 | + attachments: [], | | 15526 | + }), | | 15527 | + ); | | 15528 | + for (const frame of [ | | 15529 | + syntheticFrame("goal-task-type-set", `Goal set: ${condition}`), | | 15530 | + claudeSdkFrame({ | | 15531 | + type: "system", | | 15532 | + subtype: "task_started", | | 15533 | + task_id: "task-goal-docs", | | 15534 | + description: "Watch the docs", | | 15535 | + ...(taskType === undefined ? {} : { task_type: taskType }), | | 15536 | + uuid: "goal-task-type-start", | | 15537 | + session_id: WAKE_NATIVE_SESSION, | | 15538 | + }), | | 15539 | + makeAssistantTextFrame({ uuid: "goal-task-type-work", text: "The docs build." }), | | 15540 | + makeResultFrame({ uuid: "goal-task-type-result", result: "The docs build." }), | | 15541 | + ]) { | | 15542 | + yield* Queue.offer(harness.sdkMessages, frame); | | 15543 | + } | | 15544 | + assert.equal((yield* Queue.take(harness.terminalReceipts)).status, "completed"); | | 15545 | + assert.equal(goalStatuses(harness.events).at(-1)?.status, status); | | 15546 | + }).pipe( | | 15547 | + Effect.scoped, | | 15548 | + Effect.provide( | | 15549 | + Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer), | | 15550 | + ), | | 15551 | + ), | | 15552 | + ); | | 15553 | + | 15422 | 15554 | it.effect("keeps a goal active after a command turn with no model output", () => | 15423 | 15555 | Effect.gen(function* () { | 15424 | 15556 | const harness = yield* makeWakeHarness; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +24-1Lines changed: 24 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2124,6 +2124,15 @@ const CLAUDE_OPAQUE_BACKGROUND_TASK_KINDS: ReadonlyMap< | 2124 | 2124 | Exclude<OrchestrationV2PendingBackgroundTask["kind"], "subagent"> | 2125 | 2125 | > = new Map([["local_bash", "command"]]); | 2126 | 2126 |
| | 2127 | +// Claude Code 2.1.292 defers its goal Stop hook for these subagent types. | | 2128 | +// local_bash tasks defer it through the opaque-task roster. | | 2129 | +const CLAUDE_GOAL_DEFERRING_TASK_TYPES: ReadonlySet<string> = new Set([ | | 2130 | + "local_agent", | | 2131 | + "remote_agent", | | 2132 | + "in_process_teammate", | | 2133 | + "local_workflow", | | 2134 | +]); | | 2135 | + | 2127 | 2136 | function isClaudeOpaqueBackgroundTaskType(taskType: string | null | undefined): boolean { | 2128 | 2137 | return typeof taskType === "string" && CLAUDE_OPAQUE_BACKGROUND_TASK_KINDS.has(taskType); | 2129 | 2138 | } | @@ -3162,6 +3171,7 @@ interface ActiveClaudeProviderRetry { | 3162 | 3171 |
| 3163 | 3172 | interface ActiveClaudeSubagent { | 3164 | 3173 | task: OrchestrationV2Subagent; | | 3174 | + readonly nativeTaskType: string | undefined; | 3165 | 3175 | // Local parent-turn cleanup can end the card without a native task outcome. | 3166 | 3176 | readonly confirmedOutcome: ProviderAdapter.ProviderAdapterV2SubagentOutcome | undefined; | 3167 | 3177 | // The launch run's root node. task.parentNodeId is that same node, or the | @@ -4680,6 +4690,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 4680 | 4690 | const ids = claudeSubagentIds(resume.context, resume.taskId); | 4681 | 4691 | const now = yield* DateTime.now; | 4682 | 4692 | const subagent: ActiveClaudeSubagent = { | | 4693 | + nativeTaskType: undefined, | 4683 | 4694 | confirmedOutcome: undefined, | 4684 | 4695 | childThread: null, | 4685 | 4696 | task: { | @@ -5127,6 +5138,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 5127 | 5138 | // started this one; read only when this call registers the subagent. | 5128 | 5139 | readonly effort?: string; | 5129 | 5140 | readonly owner?: ActiveClaudeSubagent; | | 5141 | + readonly nativeTaskType?: string; | 5130 | 5142 | readonly progress?: string; | 5131 | 5143 | /** Raw SDK frame, folded into the coordinator's workflow snapshot. */ | 5132 | 5144 | readonly workflowFrame?: unknown; | @@ -5308,6 +5320,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 5308 | 5320 | } satisfies OrchestrationV2Subagent; | 5309 | 5321 | const subagent = { | 5310 | 5322 | task, | | 5323 | + nativeTaskType: input.nativeTaskType ?? existingSubagent?.nativeTaskType, | 5311 | 5324 | confirmedOutcome: | 5312 | 5325 | input.nativeOutcome !== undefined | 5313 | 5326 | ? confirmedSubagentOutcome(task) | @@ -6252,13 +6265,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 6252 | 6265 | nativeThreadId === null ? undefined : goalsByNativeThread.get(nativeThreadId); | 6253 | 6266 | const goalChecked = goalCheckedTurns.delete(input.context.providerTurnId); | 6254 | 6267 | const terminalReason = input.result?.terminal_reason; | | 6268 | + const liveQuery = yield* Ref.get(queryContext); | 6255 | 6269 | if ( | 6256 | 6270 | nativeThreadId !== null && | 6257 | 6271 | goal?.status === "active" && | 6258 | 6272 | goalChecked && | 6259 | 6273 | input.status === "completed" && | 6260 | 6274 | (terminalReason === undefined || terminalReason === "completed") && | 6261 | | - roster.size === 0 | | 6275 | + roster.size === 0 && | | 6276 | + !( | | 6277 | + liveQuery?.nativeThreadId === nativeThreadId && | | 6278 | + (yield* liveProcessRunsBackgroundWork(liveQuery, { forGoalCheck: true })) | | 6279 | + ) | 6262 | 6280 | ) { | 6263 | 6281 | goalsByNativeThread.set(nativeThreadId, { | 6264 | 6282 | objective: goal.objective, | @@ -7516,6 +7534,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 7516 | 7534 | yield* updateClaudeSubagentNode({ | 7517 | 7535 | context, | 7518 | 7536 | taskId: message.task_id, | | 7537 | + ...(message.task_type === undefined ? {} : { nativeTaskType: message.task_type }), | 7519 | 7538 | ...(message.tool_use_id === undefined ? {} : { toolUseId: message.tool_use_id }), | 7520 | 7539 | ...(message.prompt === undefined ? {} : { prompt: message.prompt }), | 7521 | 7540 | ...(model === undefined ? {} : { model }), | @@ -8601,6 +8620,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 8601 | 8620 | // already buffered is done: the buffer outlives the process. | 8602 | 8621 | const liveProcessRunsBackgroundWork = Effect.fnUntraced(function* ( | 8603 | 8622 | live: ClaudeLiveQueryContext, | | 8623 | + options?: { readonly forGoalCheck?: boolean }, | 8604 | 8624 | ) { | 8605 | 8625 | if ( | 8606 | 8626 | rosterForNativeThread( | @@ -8615,6 +8635,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 8615 | 8635 | if ( | 8616 | 8636 | subagent.task.status === "running" && | 8617 | 8637 | !live.subagentsFromEarlierProcesses.has(subagent) && | | 8638 | + (options?.forGoalCheck !== true || | | 8639 | + subagent.nativeTaskType === undefined || | | 8640 | + CLAUDE_GOAL_DEFERRING_TASK_TYPES.has(subagent.nativeTaskType)) && | 8618 | 8641 | !buffered.some( | 8619 | 8642 | (message) => | 8620 | 8643 | message.type === "system" && | |
|