GeezerVison is showing github.com — fix(server): a Claude /goal no longer reads as complete while a backg… · pingdotgg/t3code@89424d9
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 |
|---|
@@ -16013,6 +16013,138 @@ describe("ClaudeAdapterV2 background wake turns", () => { | 16013 | 16013 | ), | 16014 | 16014 | ); | 16015 | 16015 |
| | 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 | + | 16016 | 16148 | it.effect("keeps a goal active after a command turn with no model output", () => | 16017 | 16149 | Effect.gen(function* () { | 16018 | 16150 | 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 |
|---|
@@ -2078,6 +2078,15 @@ const CLAUDE_OPAQUE_BACKGROUND_TASK_KINDS: ReadonlyMap< | 2078 | 2078 | Exclude<OrchestrationV2PendingBackgroundTask["kind"], "subagent"> | 2079 | 2079 | > = new Map([["local_bash", "command"]]); | 2080 | 2080 |
| | 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 | + | 2081 | 2090 | function isClaudeOpaqueBackgroundTaskType(taskType: string | null | undefined): boolean { | 2082 | 2091 | return typeof taskType === "string" && CLAUDE_OPAQUE_BACKGROUND_TASK_KINDS.has(taskType); | 2083 | 2092 | } | @@ -3079,6 +3088,7 @@ interface ActiveClaudeProviderRetry { | 3079 | 3088 |
| 3080 | 3089 | interface ActiveClaudeSubagent { | 3081 | 3090 | task: OrchestrationV2Subagent; | | 3091 | + readonly nativeTaskType: string | undefined; | 3082 | 3092 | readonly confirmedOutcome: ProviderAdapter.ProviderAdapterV2SubagentOutcome | undefined; | 3083 | 3093 | readonly rootNodeId: OrchestrationV2ExecutionNode["id"]; | 3084 | 3094 | readonly childThreadId: ThreadId; | @@ -4504,6 +4514,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 4504 | 4514 | const ids = claudeSubagentIds(resume.context, resume.taskId); | 4505 | 4515 | const now = yield* DateTime.now; | 4506 | 4516 | const subagent: ActiveClaudeSubagent = { | | 4517 | + nativeTaskType: undefined, | 4507 | 4518 | confirmedOutcome: undefined, | 4508 | 4519 | childThread: null, | 4509 | 4520 | task: { | @@ -4985,6 +4996,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 4985 | 4996 | readonly model?: string; | 4986 | 4997 | readonly effort?: string; | 4987 | 4998 | readonly owner?: ActiveClaudeSubagent; | | 4999 | + readonly nativeTaskType?: string; | 4988 | 5000 | readonly progress?: string; | 4989 | 5001 | readonly workflowFrame?: unknown; | 4990 | 5002 | readonly workflowName?: string; | @@ -5143,6 +5155,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 5143 | 5155 | } satisfies OrchestrationV2Subagent; | 5144 | 5156 | const subagent = { | 5145 | 5157 | task, | | 5158 | + nativeTaskType: input.nativeTaskType ?? existingSubagent?.nativeTaskType, | 5146 | 5159 | confirmedOutcome: | 5147 | 5160 | input.nativeOutcome !== undefined | 5148 | 5161 | ? confirmedSubagentOutcome(task) | @@ -6275,13 +6288,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 6275 | 6288 | nativeThreadId === null ? undefined : goalsByNativeThread.get(nativeThreadId); | 6276 | 6289 | const goalChecked = goalCheckedTurns.delete(input.context.providerTurnId); | 6277 | 6290 | const terminalReason = input.result?.terminal_reason; | | 6291 | + const liveQuery = yield* Ref.get(queryContext); | 6278 | 6292 | if ( | 6279 | 6293 | nativeThreadId !== null && | 6280 | 6294 | goal?.status === "active" && | 6281 | 6295 | goalChecked && | 6282 | 6296 | input.status === "completed" && | 6283 | 6297 | (terminalReason === undefined || terminalReason === "completed") && | 6284 | | - roster.size === 0 | | 6298 | + roster.size === 0 && | | 6299 | + !( | | 6300 | + liveQuery?.nativeThreadId === nativeThreadId && | | 6301 | + (yield* liveProcessRunsBackgroundWork(liveQuery, { forGoalCheck: true })) | | 6302 | + ) | 6285 | 6303 | ) { | 6286 | 6304 | goalsByNativeThread.set(nativeThreadId, { | 6287 | 6305 | objective: goal.objective, | @@ -7533,6 +7551,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 7533 | 7551 | yield* updateClaudeSubagentNode({ | 7534 | 7552 | context, | 7535 | 7553 | taskId: message.task_id, | | 7554 | + ...(message.task_type === undefined ? {} : { nativeTaskType: message.task_type }), | 7536 | 7555 | ...(message.tool_use_id === undefined ? {} : { toolUseId: message.tool_use_id }), | 7537 | 7556 | ...(message.prompt === undefined ? {} : { prompt: message.prompt }), | 7538 | 7557 | ...(model === undefined ? {} : { model }), | @@ -8472,6 +8491,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 8472 | 8491 |
| 8473 | 8492 | const liveProcessRunsBackgroundWork = Effect.fnUntraced(function* ( | 8474 | 8493 | live: ClaudeLiveQueryContext, | | 8494 | + options?: { readonly forGoalCheck?: boolean }, | 8475 | 8495 | ) { | 8476 | 8496 | if ( | 8477 | 8497 | rosterForNativeThread( | @@ -8486,6 +8506,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* ( | 8486 | 8506 | if ( | 8487 | 8507 | subagent.task.status === "running" && | 8488 | 8508 | !live.subagentsFromEarlierProcesses.has(subagent) && | | 8509 | + (options?.forGoalCheck !== true || | | 8510 | + subagent.nativeTaskType === undefined || | | 8511 | + CLAUDE_GOAL_DEFERRING_TASK_TYPES.has(subagent.nativeTaskType)) && | 8489 | 8512 | !buffered.some( | 8490 | 8513 | (message) => | 8491 | 8514 | message.type === "system" && | |
|