@devries-cameron20 / t3code Mirror · read-only / commits / ccc9aaf10b
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, 21:00 UTC

fix(server): a cancelled queued Claude continuation no longer drops later wakes (#30114)

Julius Marminge committed Nov 11, 2026 at 16:51 UTC ccc9aaf10b5f40382d0ca68b8523aa2f434c841f
6 files changed +1354 -57
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+705 -4
@@ -13910,7 +13910,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
13910 ),
13911 );
13912
13913 - it.effect("keeps a newer wake latched when a stale continuation request is cleared", () =>
13913 + it.effect("keeps a newer wake's output when a stale continuation request is cleared", () =>
13914 Effect.scoped(
13915 Effect.gen(function* () {
13916 const SECOND_TASK_ID = "b2ndwake";
@@ -13976,11 +13976,546 @@ describe("ClaudeAdapterV2 background wake turns", () => {
13976 );
13977 assert.lengthOf(harness.continuationRequests, 2);
13978
13979 - yield* harness.continuationRequests[0]!.clearIfCurrent!();
13979 + yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
13980 yield* harness.offerAndWait(
13981 - claudeSdkFrame({ ...wakeAssistant, uuid: "00000000-0000-4000-8000-000000000135" }),
13981 + makeAssistantTextFrame({
13982 + uuid: "00000000-0000-4000-8000-000000000135",
13983 + text: "Second build done.",
13984 + }),
13985 + );
13986 + yield* harness.offerAndWait(
13987 + makeResultFrame({
13988 + uuid: "00000000-0000-4000-8000-000000000136",
13989 + result: "Second build done.",
13990 + origin: { kind: "task-notification" },
13991 + }),
13992 + );
13993 + assert.lengthOf(harness.continuationRequests, 2);
13994 + yield* harness.runtime.startTurn(
13995 + makeClaudeTestTurnInput({
13996 + threadId: harness.threadId,
13997 + providerThread: harness.providerThread,
13998 + now,
13999 + attemptId: RunAttemptId.make("attempt-claude-wake-stale-clear-3"),
14000 + text: "Background task completed.",
14001 + attachments: [],
14002 + providerTurnOrdinal: 3,
14003 + messageCreatedBy: "agent",
14004 + messageCreationSource: "provider",
14005 + }),
14006 + );
14007 + yield* harness.awaitTerminal(3);
14008 + assert.isTrue(
14009 + harness.events.some(
14010 + (event) =>
14011 + event.type === "turn_item.updated" &&
14012 + event.turnItem.type === "assistant_message" &&
14013 + event.turnItem.text === "Second build done.",
14014 + ),
14015 + "clearing the first wake's request must not discard the second wake's output",
14016 + );
14017 + }).pipe(
14018 + Effect.provide(
14019 + Layer.mergeAll(
14020 + IdAllocator.layer,
14021 + McpProviderSessions.layer,
14022 + NodeServices.layer,
14023 + NodeCrypto.layer,
14024 + ),
14025 + ),
14026 + ),
14027 + ),
14028 + );
14029 +
14030 + it.effect("offers the next wake when a cancelled wake's release arrives after it opened", () =>
14031 + Effect.scoped(
14032 + Effect.gen(function* () {
14033 + const SECOND_TASK_ID = "b2ndwake";
14034 + const SECOND_TOOL_USE_ID = "toolu_second_wake";
14035 + const harness = yield* makeWakeHarness;
14036 + const now = yield* DateTime.now;
14037 +
14038 + yield* harness.runtime.startTurn(
14039 + makeClaudeTestTurnInput({
14040 + threadId: harness.threadId,
14041 + providerThread: harness.providerThread,
14042 + now,
14043 + attemptId: RunAttemptId.make("attempt-claude-wake-late-release"),
14044 + text: "Run two builds in the background.",
14045 + attachments: [],
14046 + }),
14047 + );
14048 + yield* harness.offerAndWait(wakeTaskStarted);
14049 + yield* harness.offerAndWait(
14050 + claudeSdkFrame({
14051 + ...wakeTaskStarted,
14052 + task_id: SECOND_TASK_ID,
14053 + tool_use_id: SECOND_TOOL_USE_ID,
14054 + uuid: "00000000-0000-4000-8000-000000000141",
14055 + }),
14056 + );
14057 + yield* harness.offerAndWait(turnOneResult);
14058 + yield* harness.awaitTerminal(1);
14059 +
14060 + yield* harness.offerAndWait(wakeNotification);
14061 + yield* harness.offerAndWait(wakeTurnInit);
14062 + yield* harness.offerAndWait(wakeAssistant);
14063 + yield* harness.offerAndWait(wakeResult);
14064 + yield* harness.offerAndWait(
14065 + claudeSdkFrame({
14066 + ...wakeNotification,
14067 + task_id: SECOND_TASK_ID,
14068 + tool_use_id: SECOND_TOOL_USE_ID,
14069 + uuid: "00000000-0000-4000-8000-000000000142",
14070 + }),
14071 + );
14072 + yield* harness.offerAndWait(
14073 + claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000143" }),
14074 + );
14075 + assert.lengthOf(harness.continuationRequests, 1);
14076 +
14077 + yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
14078 + yield* harness.offerAndWait(
14079 + claudeSdkFrame({ ...wakeAssistant, uuid: "00000000-0000-4000-8000-000000000144" }),
14080 + );
14081 + yield* harness.offerAndWait(
14082 + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000145", result: "Done." }),
14083 + );
14084 + assert.lengthOf(
14085 + harness.continuationRequests,
14086 + 2,
14087 + "the wake that opened before the cancelled wake's release must get its own continuation",
14088 + );
14089 + }).pipe(
14090 + Effect.provide(
14091 + Layer.mergeAll(
14092 + IdAllocator.layer,
14093 + McpProviderSessions.layer,
14094 + NodeServices.layer,
14095 + NodeCrypto.layer,
14096 + ),
14097 + ),
14098 + ),
14099 + ),
14100 + );
14101 +
14102 + it.effect("does not offer a wake again when it is cancelled before its first root frame", () =>
14103 + Effect.scoped(
14104 + Effect.gen(function* () {
14105 + const harness = yield* makeWakeHarness;
14106 + const now = yield* DateTime.now;
14107 +
14108 + yield* harness.runtime.startTurn(
14109 + makeClaudeTestTurnInput({
14110 + threadId: harness.threadId,
14111 + providerThread: harness.providerThread,
14112 + now,
14113 + attemptId: RunAttemptId.make("attempt-claude-wake-early-cancel"),
14114 + text: "Start an agent in the background.",
14115 + attachments: [],
14116 + }),
14117 + );
14118 + yield* harness.offerAndWait(
14119 + claudeSdkFrame({ ...wakeTaskStarted, task_type: "local_agent" }),
14120 + );
14121 + yield* harness.offerAndWait(turnOneResult);
14122 + yield* harness.awaitTerminal(1);
14123 +
14124 + yield* harness.offerAndWait(wakeNotification);
14125 + assert.lengthOf(harness.continuationRequests, 1);
14126 + yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
14127 + yield* harness.offerAndWait(wakeTurnInit);
14128 + yield* harness.offerAndWait(wakeAssistant);
14129 + yield* harness.offerAndWait(wakeResult);
14130 + assert.lengthOf(
14131 + harness.continuationRequests,
14132 + 1,
14133 + "the cancelled wake must not be offered again",
14134 + );
14135 +
14136 + yield* harness.offerAndWait(
14137 + claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000146" }),
14138 + );
14139 + assert.lengthOf(harness.continuationRequests, 2, "the next wake must be offered");
14140 + }).pipe(
14141 + Effect.provide(
14142 + Layer.mergeAll(
14143 + IdAllocator.layer,
14144 + McpProviderSessions.layer,
14145 + NodeServices.layer,
14146 + NodeCrypto.layer,
14147 + ),
14148 + ),
14149 + ),
14150 + ),
14151 + );
14152 +
14153 + it.effect("offers a later task's wake when a cancelled wake never opens", () =>
14154 + Effect.scoped(
14155 + Effect.gen(function* () {
14156 + const SECOND_TASK_ID = "b2ndwake";
14157 + const SECOND_TOOL_USE_ID = "toolu_second_wake";
14158 + const harness = yield* makeWakeHarness;
14159 + const now = yield* DateTime.now;
14160 +
14161 + yield* harness.runtime.startTurn(
14162 + makeClaudeTestTurnInput({
14163 + threadId: harness.threadId,
14164 + providerThread: harness.providerThread,
14165 + now,
14166 + attemptId: RunAttemptId.make("attempt-claude-wake-never-opens"),
14167 + text: "Start an agent and a build in the background.",
14168 + attachments: [],
14169 + }),
14170 + );
14171 + yield* harness.offerAndWait(
14172 + claudeSdkFrame({ ...wakeTaskStarted, task_type: "local_agent" }),
14173 + );
14174 + yield* harness.offerAndWait(
14175 + claudeSdkFrame({
14176 + ...wakeTaskStarted,
14177 + task_id: SECOND_TASK_ID,
14178 + tool_use_id: SECOND_TOOL_USE_ID,
14179 + uuid: "00000000-0000-4000-8000-000000000151",
14180 + }),
14181 + );
14182 + yield* harness.offerAndWait(turnOneResult);
14183 + yield* harness.awaitTerminal(1);
14184 +
14185 + // Claude never opens a wake for the agent's notification.
14186 + yield* harness.offerAndWait(wakeNotification);
14187 + assert.lengthOf(harness.continuationRequests, 1);
14188 + yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
14189 +
14190 + yield* harness.offerAndWait(
14191 + claudeSdkFrame({
14192 + ...wakeNotification,
14193 + task_id: SECOND_TASK_ID,
14194 + tool_use_id: SECOND_TOOL_USE_ID,
14195 + output_file: `/tmp/claude-replay/tasks/${SECOND_TASK_ID}.output`,
14196 + uuid: "00000000-0000-4000-8000-000000000152",
14197 + }),
14198 + );
14199 + yield* harness.offerAndWait(
14200 + claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000153" }),
14201 + );
14202 + yield* harness.offerAndWait(
14203 + makeAssistantTextFrame({ uuid: "00000000-0000-4000-8000-000000000154", text: "Built." }),
14204 + );
14205 + yield* harness.offerAndWait(
14206 + makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000155", result: "Built." }),
14207 + );
14208 + assert.lengthOf(
14209 + harness.continuationRequests,
14210 + 2,
14211 + "the build's wake must get its own continuation",
14212 + );
14213 + }).pipe(
14214 + Effect.provide(
14215 + Layer.mergeAll(
14216 + IdAllocator.layer,
14217 + McpProviderSessions.layer,
14218 + NodeServices.layer,
14219 + NodeCrypto.layer,
14220 + ),
14221 + ),
14222 + ),
14223 + ),
14224 + );
14225 +
14226 + it.effect.each([
14227 + { ending: "notification", cancelled: "after its result" },
14228 + { ending: "tool result", cancelled: "after its result" },
14229 + { ending: "tool result", cancelled: "while the subagent ran" },
14230 + ] as const)(
14231 + "does not replay a subagent that finished inside a cancelled wake (ended by $ending, cancelled $cancelled)",
14232 + ({ ending, cancelled }) =>
14233 + Effect.scoped(
14234 + Effect.gen(function* () {
14235 + const SECOND_TASK_ID = "b2ndwake";
14236 + const SECOND_TOOL_USE_ID = "toolu_second_wake";
14237 + const SUBAGENT_TASK_ID = "fg-agent";
14238 + const SUBAGENT_TOOL_USE_ID = "toolu-fg-agent";
14239 + const harness = yield* makeWakeHarness;
14240 + const now = yield* DateTime.now;
14241 +
14242 + yield* harness.runtime.startTurn(
14243 + makeClaudeTestTurnInput({
14244 + threadId: harness.threadId,
14245 + providerThread: harness.providerThread,
14246 + now,
14247 + attemptId: RunAttemptId.make("attempt-claude-wake-finished-subagent"),
14248 + text: "Run two builds in the background.",
14249 + attachments: [],
14250 + }),
14251 + );
14252 + yield* harness.offerAndWait(wakeTaskStarted);
14253 + yield* harness.offerAndWait(
14254 + claudeSdkFrame({
14255 + ...wakeTaskStarted,
14256 + task_id: SECOND_TASK_ID,
14257 + tool_use_id: SECOND_TOOL_USE_ID,
14258 + uuid: "00000000-0000-4000-8000-000000000161",
14259 + }),
14260 + );
14261 + yield* harness.offerAndWait(turnOneResult);
14262 + yield* harness.awaitTerminal(1);
14263 +
14264 + // The first wake runs a subagent to completion before its result.
14265 + yield* harness.offerAndWait(wakeNotification);
14266 + yield* harness.offerAndWait(wakeTurnInit);
14267 + assert.lengthOf(harness.continuationRequests, 1);
14268 + yield* harness.offerAndWait(wakeAssistant);
14269 + yield* harness.offerAndWait(
14270 + claudeSdkFrame({
14271 + type: "system",
14272 + subtype: "task_started",
14273 + task_id: SUBAGENT_TASK_ID,
14274 + tool_use_id: SUBAGENT_TOOL_USE_ID,
14275 + description: "Inspect build output",
14276 + subagent_type: "general-purpose",
14277 + task_type: "local_agent",
14278 + prompt: "Inspect the build output.",
14279 + uuid: "00000000-0000-4000-8000-000000000162",
14280 + session_id: WAKE_NATIVE_SESSION,
14281 + }),
14282 + );
14283 + yield* harness.offerAndWait(
14284 + claudeSdkFrame({
14285 + ...makeAssistantTextFrame({
14286 + uuid: "00000000-0000-4000-8000-000000000168",
14287 + text: "Reading the build log.",
14288 + }),
14289 + parent_tool_use_id: SUBAGENT_TOOL_USE_ID,
14290 + }),
14291 + );
14292 + if (cancelled === "while the subagent ran") {
14293 + yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
14294 + }
14295 + yield* harness.offerAndWait(
14296 + ending === "notification"
14297 + ? claudeSdkFrame({
14298 + type: "system",
14299 + subtype: "task_notification",
14300 + task_id: SUBAGENT_TASK_ID,
14301 + tool_use_id: SUBAGENT_TOOL_USE_ID,
14302 + status: "completed",
14303 + output_file: "/tmp/fg-agent.output",
14304 + summary: "Inspected",
14305 + uuid: "00000000-0000-4000-8000-000000000163",
14306 + session_id: WAKE_NATIVE_SESSION,
14307 + })
14308 + : claudeSdkFrame({
14309 + type: "user",
14310 + uuid: "00000000-0000-4000-8000-000000000163",
14311 + session_id: WAKE_NATIVE_SESSION,
14312 + parent_tool_use_id: null,
14313 + message: {
14314 + role: "user",
14315 + content: [
14316 + {
14317 + type: "tool_result",
14318 + tool_use_id: SUBAGENT_TOOL_USE_ID,
14319 + content: "Inspected",
14320 + },
14321 + ],
14322 + },
14323 + }),
14324 + );
14325 + yield* harness.offerAndWait(wakeResult);
14326 + if (cancelled === "after its result") {
14327 + yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
14328 + }
14329 +
14330 + yield* harness.offerAndWait(
14331 + claudeSdkFrame({
14332 + ...wakeNotification,
14333 + task_id: SECOND_TASK_ID,
14334 + tool_use_id: SECOND_TOOL_USE_ID,
14335 + output_file: `/tmp/claude-replay/tasks/${SECOND_TASK_ID}.output`,
14336 + uuid: "00000000-0000-4000-8000-000000000164",
14337 + }),
14338 + );
14339 + yield* harness.offerAndWait(
14340 + claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000165" }),
14341 + );
14342 + assert.lengthOf(harness.continuationRequests, 2);
14343 + yield* harness.runtime.startTurn(
14344 + makeClaudeTestTurnInput({
14345 + threadId: harness.threadId,
14346 + providerThread: harness.providerThread,
14347 + now,
14348 + attemptId: RunAttemptId.make("attempt-claude-wake-finished-subagent-2"),
14349 + text: "Background task completed.",
14350 + attachments: [],
14351 + providerTurnOrdinal: 2,
14352 + messageCreatedBy: "agent",
14353 + messageCreationSource: "provider",
14354 + }),
14355 + );
14356 + yield* harness.offerAndWait(
14357 + makeAssistantTextFrame({
14358 + uuid: "00000000-0000-4000-8000-000000000166",
14359 + text: "Second build done.",
14360 + }),
14361 + );
14362 + yield* harness.offerAndWait(
14363 + makeResultFrame({
14364 + uuid: "00000000-0000-4000-8000-000000000167",
14365 + result: "Second build done.",
14366 + origin: { kind: "task-notification" },
14367 + }),
14368 + );
14369 + yield* harness.awaitTerminal(2);
14370 +
14371 + const subagentStatuses = harness.events.flatMap((event) =>
14372 + event.type === "subagent.updated" &&
14373 + event.subagent.nativeTaskRef?.nativeId === SUBAGENT_TASK_ID
14374 + ? [event.subagent.status]
14375 + : [],
14376 + );
14377 + assert.notInclude(
14378 + subagentStatuses,
14379 + "running",
14380 + "a subagent that finished in the cancelled wake must not be replayed as running",
14381 + );
14382 + assert.isFalse(yield* harness.hasPendingBackgroundWork);
14383 + }).pipe(
14384 + Effect.provide(
14385 + Layer.mergeAll(
14386 + IdAllocator.layer,
14387 + McpProviderSessions.layer,
14388 + NodeServices.layer,
14389 + NodeCrypto.layer,
14390 + ),
14391 + ),
14392 + ),
14393 + ),
14394 + );
14395 +
14396 + it.effect("offers a cancelled wake once when an empty lifecycle result precedes its turn", () =>
14397 + Effect.scoped(
14398 + Effect.gen(function* () {
14399 + const SECOND_TASK_ID = "b2ndwake";
14400 + const SECOND_TOOL_USE_ID = "toolu_second_wake";
14401 + const harness = yield* makeWakeHarness;
14402 + const now = yield* DateTime.now;
14403 +
14404 + yield* harness.runtime.startTurn(
14405 + makeClaudeTestTurnInput({
14406 + threadId: harness.threadId,
14407 + providerThread: harness.providerThread,
14408 + now,
14409 + attemptId: RunAttemptId.make("attempt-claude-wake-empty-lifecycle"),
14410 + text: "Run two builds in the background.",
14411 + attachments: [],
14412 + }),
14413 + );
14414 + yield* harness.offerAndWait(wakeTaskStarted);
14415 + yield* harness.offerAndWait(
14416 + claudeSdkFrame({
14417 + ...wakeTaskStarted,
14418 + task_id: SECOND_TASK_ID,
14419 + tool_use_id: SECOND_TOOL_USE_ID,
14420 + uuid: "00000000-0000-4000-8000-000000000171",
14421 + }),
14422 + );
14423 + yield* harness.offerAndWait(turnOneResult);
14424 + yield* harness.awaitTerminal(1);
14425 +
14426 + yield* harness.offerAndWait(wakeNotification);
14427 + yield* harness.offerAndWait(wakeTurnInit);
14428 + assert.lengthOf(harness.continuationRequests, 1);
14429 + yield* harness.offerAndWait(
14430 + makeResultFrame({
14431 + uuid: "00000000-0000-4000-8000-000000000172",
14432 + result: "",
14433 + numTurns: 0,
14434 + origin: { kind: "task-notification" },
14435 + terminalReason: null,
14436 + }),
14437 + );
14438 + yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
14439 + yield* harness.offerAndWait(
14440 + claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000173" }),
14441 + );
14442 + yield* harness.offerAndWait(wakeAssistant);
14443 + yield* harness.offerAndWait(wakeResult);
14444 + assert.lengthOf(
14445 + harness.continuationRequests,
14446 + 1,
14447 + "the cancelled wake must not be offered again",
14448 + );
14449 +
14450 + yield* harness.offerAndWait(
14451 + claudeSdkFrame({
14452 + ...wakeNotification,
14453 + task_id: SECOND_TASK_ID,
14454 + tool_use_id: SECOND_TOOL_USE_ID,
14455 + output_file: `/tmp/claude-replay/tasks/${SECOND_TASK_ID}.output`,
14456 + uuid: "00000000-0000-4000-8000-000000000174",
14457 + }),
14458 + );
14459 + yield* harness.offerAndWait(
14460 + claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000175" }),
14461 + );
14462 + assert.lengthOf(harness.continuationRequests, 2, "the next wake must be offered");
14463 + }).pipe(
14464 + Effect.provide(
14465 + Layer.mergeAll(
14466 + IdAllocator.layer,
14467 + McpProviderSessions.layer,
14468 + NodeServices.layer,
14469 + NodeCrypto.layer,
14470 + ),
14471 + ),
14472 + ),
14473 + ),
14474 + );
14475 +
14476 + it.effect("does not offer a cancelled wake again for its trailing empty lifecycle result", () =>
14477 + Effect.scoped(
14478 + Effect.gen(function* () {
14479 + const harness = yield* makeWakeHarness;
14480 + const now = yield* DateTime.now;
14481 +
14482 + yield* harness.runtime.startTurn(
14483 + makeClaudeTestTurnInput({
14484 + threadId: harness.threadId,
14485 + providerThread: harness.providerThread,
14486 + now,
14487 + attemptId: RunAttemptId.make("attempt-claude-wake-trailing-lifecycle"),
14488 + text: "Run a build in the background.",
14489 + attachments: [],
14490 + }),
14491 + );
14492 + yield* harness.offerAndWait(wakeTaskStarted);
14493 + yield* harness.offerAndWait(turnOneResult);
14494 + yield* harness.awaitTerminal(1);
14495 +
14496 + yield* harness.offerAndWait(wakeNotification);
14497 + yield* harness.offerAndWait(wakeTurnInit);
14498 + yield* harness.offerAndWait(wakeAssistant);
14499 + yield* harness.offerAndWait(wakeResult);
14500 + yield* harness.offerAndWait(
14501 + claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000176" }),
14502 + );
14503 + yield* harness.offerAndWait(
14504 + makeResultFrame({
14505 + uuid: "00000000-0000-4000-8000-000000000177",
14506 + result: "",
14507 + numTurns: 0,
14508 + origin: { kind: "task-notification" },
14509 + terminalReason: null,
14510 + }),
14511 + );
14512 + assert.lengthOf(harness.continuationRequests, 1);
14513 + yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
14514 + assert.lengthOf(
14515 + harness.continuationRequests,
14516 + 1,
14517 + "a lifecycle result after the cancelled wake must not open another continuation",
14518 );
13983 - assert.lengthOf(harness.continuationRequests, 2);
14519 }).pipe(
14520 Effect.provide(
14521 Layer.mergeAll(
@@ -13994,6 +14529,82 @@ describe("ClaudeAdapterV2 background wake turns", () => {
14529 ),
14530 );
14531
14532 + it.effect.each([
14533 + "before the cancelled wake's result",
14534 + "after the cancelled wake's result",
14535 + ] as const)(
14536 + "offers a subagent's wake that arrives behind a cancelled wake (notified %s)",
14537 + (notified) =>
14538 + Effect.scoped(
14539 + Effect.gen(function* () {
14540 + const SUBAGENT_TASK_ID = "bg-agent";
14541 + const SUBAGENT_TOOL_USE_ID = "toolu-bg-agent";
14542 + const harness = yield* makeWakeHarness;
14543 + const now = yield* DateTime.now;
14544 +
14545 + yield* harness.runtime.startTurn(
14546 + makeClaudeTestTurnInput({
14547 + threadId: harness.threadId,
14548 + providerThread: harness.providerThread,
14549 + now,
14550 + attemptId: RunAttemptId.make("attempt-claude-wake-later-subagent"),
14551 + text: "Run a build and an agent in the background.",
14552 + attachments: [],
14553 + }),
14554 + );
14555 + yield* harness.offerAndWait(wakeTaskStarted);
14556 + yield* harness.offerAndWait(
14557 + claudeSdkFrame({
14558 + ...wakeTaskStarted,
14559 + task_id: SUBAGENT_TASK_ID,
14560 + tool_use_id: SUBAGENT_TOOL_USE_ID,
14561 + task_type: "local_agent",
14562 + uuid: "00000000-0000-4000-8000-000000000181",
14563 + }),
14564 + );
14565 + yield* harness.offerAndWait(turnOneResult);
14566 + yield* harness.awaitTerminal(1);
14567 +
14568 + const subagentNotification = claudeSdkFrame({
14569 + ...wakeNotification,
14570 + task_id: SUBAGENT_TASK_ID,
14571 + tool_use_id: SUBAGENT_TOOL_USE_ID,
14572 + output_file: `/tmp/claude-replay/tasks/${SUBAGENT_TASK_ID}.output`,
14573 + summary: "Agent finished",
14574 + uuid: "00000000-0000-4000-8000-000000000182",
14575 + });
14576 + yield* harness.offerAndWait(wakeNotification);
14577 + yield* harness.offerAndWait(wakeTurnInit);
14578 + yield* harness.offerAndWait(wakeAssistant);
14579 + assert.lengthOf(harness.continuationRequests, 1);
14580 + if (notified === "before the cancelled wake's result") {
14581 + yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
14582 + yield* harness.offerAndWait(subagentNotification);
14583 + yield* harness.offerAndWait(wakeResult);
14584 + } else {
14585 + yield* harness.offerAndWait(wakeResult);
14586 + yield* harness.offerAndWait(subagentNotification);
14587 + yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
14588 + }
14589 + assert.lengthOf(
14590 + harness.continuationRequests,
14591 + 2,
14592 + "the subagent's wake must get its own continuation",
14593 + );
14594 + assert.equal(harness.continuationRequests[1]?.detail, "Agent finished");
14595 + }).pipe(
14596 + Effect.provide(
14597 + Layer.mergeAll(
14598 + IdAllocator.layer,
14599 + McpProviderSessions.layer,
14600 + NodeServices.layer,
14601 + NodeCrypto.layer,
14602 + ),
14603 + ),
14604 + ),
14605 + ),
14606 + );
14607 +
14608 it.effect.each([true, false])(
14609 "carries a rejected wake rate limit into the continuation failure (event=%s)",
14610 (standaloneEvent) =>
@@ -14412,6 +15023,96 @@ describe("ClaudeAdapterV2 background wake turns", () => {
15023 ),
15024 );
15025
15026 + it.effect.each(["before", "after"] as const)(
15027 + "offers a later wake when a wake a user turn absorbed is cancelled %s that turn ends",
15028 + (cancelled) =>
15029 + Effect.scoped(
15030 + Effect.gen(function* () {
15031 + const SECOND_TASK_ID = "b2ndwake";
15032 + const SECOND_TOOL_USE_ID = "toolu_second_wake";
15033 + const harness = yield* makeWakeHarness;
15034 + const now = yield* DateTime.now;
15035 +
15036 + yield* harness.runtime.startTurn(
15037 + makeClaudeTestTurnInput({
15038 + threadId: harness.threadId,
15039 + providerThread: harness.providerThread,
15040 + now,
15041 + attemptId: RunAttemptId.make("attempt-claude-wake-absorbed-cancel-1"),
15042 + text: "Run two builds in the background.",
15043 + attachments: [],
15044 + }),
15045 + );
15046 + yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
15047 + yield* Queue.offer(
15048 + harness.sdkMessages,
15049 + claudeSdkFrame({
15050 + ...wakeTaskStarted,
15051 + task_id: SECOND_TASK_ID,
15052 + tool_use_id: SECOND_TOOL_USE_ID,
15053 + uuid: "00000000-0000-4000-8000-000000000191",
15054 + }),
15055 + );
15056 + yield* Queue.offer(harness.sdkMessages, wakeNotification);
15057 + yield* Queue.offer(harness.sdkMessages, turnOneResult);
15058 + yield* harness.awaitTerminal(1);
15059 + yield* harness.offerAndWait(wakeTurnInit);
15060 + yield* harness.offerAndWait(wakeAssistant);
15061 + yield* harness.awaitContinuation(1);
15062 +
15063 + yield* harness.runtime.startTurn(
15064 + makeClaudeTestTurnInput({
15065 + threadId: harness.threadId,
15066 + providerThread: harness.providerThread,
15067 + now,
15068 + attemptId: RunAttemptId.make("attempt-claude-wake-absorbed-cancel-2"),
15069 + text: "Any update on the pull request?",
15070 + attachments: [],
15071 + providerTurnOrdinal: 2,
15072 + }),
15073 + );
15074 + yield* harness.until(() => harness.offeredMessages.length === 2);
15075 + if (cancelled === "before") {
15076 + yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
15077 + }
15078 + yield* Queue.offer(
15079 + harness.sdkMessages,
15080 + makeResultFrame({
15081 + uuid: "00000000-0000-4000-8000-000000000192",
15082 + result: "Checks passed and the pull request is up to date.",
15083 + }),
15084 + );
15085 + yield* harness.awaitTerminal(2);
15086 + if (cancelled === "after") {
15087 + yield* harness.continuationRequests[0]!.onRunEndedWithoutStarting!();
15088 + }
15089 +
15090 + yield* harness.offerAndWait(
15091 + claudeSdkFrame({
15092 + ...wakeNotification,
15093 + task_id: SECOND_TASK_ID,
15094 + tool_use_id: SECOND_TOOL_USE_ID,
15095 + output_file: `/tmp/claude-replay/tasks/${SECOND_TASK_ID}.output`,
15096 + uuid: "00000000-0000-4000-8000-000000000193",
15097 + }),
15098 + );
15099 + yield* harness.offerAndWait(
15100 + claudeSdkFrame({ ...wakeTurnInit, uuid: "00000000-0000-4000-8000-000000000194" }),
15101 + );
15102 + assert.lengthOf(harness.continuationRequests, 2, "the later wake must be offered");
15103 + }).pipe(
15104 + Effect.provide(
15105 + Layer.mergeAll(
15106 + IdAllocator.layer,
15107 + McpProviderSessions.layer,
15108 + NodeServices.layer,
15109 + NodeCrypto.layer,
15110 + ),
15111 + ),
15112 + ),
15113 + ),
15114 + );
15115 +
15116 it.effect("ignores a zero-turn task-notification origin result during a normal user turn", () =>
15117 Effect.scoped(
15118 Effect.gen(function* () {
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+281 -35
@@ -3616,6 +3616,45 @@ function isClaudeSubagentAsyncLaunchAck(output: ClaudeNativeToolOutput): boolean
3616 return claudeSubagentResultText(output).startsWith("Async agent launched successfully.");
3617 }
3618
3619 +// Background Agent launches ACK immediately and workflow launches return run handles; only task_notification settles those.
3620 +function claudeToolResultSettlesSubagent(
3621 + toolResult: ClaudeToolResultContentBlock,
3622 + output: ClaudeNativeToolOutput,
3623 +): boolean {
3624 + return (
3625 + isClaudeToolResultError(toolResult) ||
3626 + (parseClaudeWorkflowRunHandles(claudeNativeToolOutputValue(output)) === undefined &&
3627 + !isClaudeSubagentAsyncLaunchAck(output))
3628 + );
3629 +}
3630 +
3631 +function claudeSettledSubagentToolUseIds(messages: ReadonlyArray<SDKMessage>): ReadonlySet<string> {
3632 + return new Set(
3633 + messages.flatMap((entry) =>
3634 + entry.type === "user" && entry.parent_tool_use_id == null
3635 + ? claudeToolResultEntriesFromMessage(entry).flatMap(({ toolResult, output }) =>
3636 + claudeToolResultSettlesSubagent(toolResult, output) ? [toolResult.tool_use_id] : [],
3637 + )
3638 + : [],
3639 + ),
3640 + );
3641 +}
3642 +
3643 +function claudeWakeBufferDetail(messages: ReadonlyArray<SDKMessage>): string | null {
3644 + return (
3645 + messages
3646 + .flatMap((entry) =>
3647 + entry.type === "system" &&
3648 + entry.subtype === "task_notification" &&
3649 + typeof entry.summary === "string" &&
3650 + entry.summary.length > 0
3651 + ? [entry.summary]
3652 + : [],
3653 + )
3654 + .at(-1) ?? null
3655 + );
3656 +}
3657 +
3658 const WEB_FETCH_SNIPPET_MAX_CHARS = 8_000;
3659
3660 function webSearchPatternsFromClaudeTool(input: {
@@ -4242,6 +4281,18 @@ function isClaudeTaskNotificationOriginResult(message: SDKMessage): message is S
4281 return message.type === "result" && message.origin?.kind === "task-notification";
4282 }
4283
4284 +// A session-task lifecycle result: it can precede a wake's real turn and ends no turn.
4285 +function isClaudeEmptyTaskNotificationSuccess(message: SDKMessage): boolean {
4286 + return (
4287 + isClaudeTaskNotificationOriginResult(message) &&
4288 + message.subtype === "success" &&
4289 + !message.is_error &&
4290 + message.num_turns === 0 &&
4291 + message.result.length === 0 &&
4292 + terminalStatusFromResult(message) === "completed"
4293 + );
4294 +}
4295 +
4296 // The CLI throttles unchanged rate_limit_event frames, but repeats this
4297 // undeclared payload on each blocked assistant response.
4298 const isClaudeBlockedAssistantRateLimit = Schema.is(
@@ -5695,7 +5746,19 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
5746 return { kind: "command", label, outcome };
5747 }
5748 });
5698 - const requestedContinuations = yield* Ref.make(new Map<string, symbol>());
5749 + // `end` is the offered wake's last frame once known: its result, or the last frame a user turn absorbed.
5750 + // A retired latch belongs to a cancelled wake that has not ended; its root frames are dropped until it does.
5751 + // Claude may never open an "unopened" one, so a later task's notification releases it instead.
5752 + const requestedContinuations = yield* Ref.make(
5753 + new Map<
5754 + string,
5755 + {
5756 + readonly token: symbol;
5757 + readonly end: SDKMessage | undefined;
5758 + readonly retired: "unopened" | "opened" | null;
5759 + }
5760 + >(),
5761 + );
5762 const absorbedWakes = yield* Ref.make(new Set<string>());
5763 const heldProposedPlansByToolUseId = new Map<string, string>();
5764 const runtimeContext = yield* Effect.context<never>();
@@ -6185,13 +6248,34 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
6248 yield* forgetAbsorbedWake(nativeThreadId);
6249 });
6250
6251 + const releaseContinuationLatch = (nativeThreadId: string) =>
6252 + Ref.update(requestedContinuations, (current) => {
6253 + if (!current.has(nativeThreadId)) return current;
6254 + const updated = new Map(current);
6255 + updated.delete(nativeThreadId);
6256 + return updated;
6257 + });
6258 +
6259 // Claude runs one root turn at a time, so a user turn's result also ends a wake turn it
6260 // folded in. The continuation replays that wake's buffered frames but gets no result of its own.
6261 const markAbsorbedWake = Effect.fnUntraced(function* (nativeThreadId: string) {
6262 + const latch = (yield* Ref.get(requestedContinuations)).get(nativeThreadId);
6263 + if (latch !== undefined && latch.retired !== null) {
6264 + yield* releaseContinuationLatch(nativeThreadId);
6265 + return;
6266 + }
6267 const messages = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
6268 const lastResult = messages.findLastIndex((entry) => entry.type === "result");
6269 if (!messages.slice(lastResult + 1).some(isClaudeRootTurnFrame)) return;
6270 yield* Ref.update(absorbedWakes, (current) => new Set(current).add(nativeThreadId));
6271 + if (latch !== undefined && latch.end === undefined) {
6272 + yield* Ref.update(requestedContinuations, (current) =>
6273 + new Map(current).set(nativeThreadId, {
6274 + ...latch,
6275 + end: messages.findLast(isClaudeRootTurnFrame),
6276 + }),
6277 + );
6278 + }
6279 });
6280
6281 // The SDK emits no initial roster; reset roster and wake eligibility when opening a live query.
@@ -8960,6 +9044,34 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9044 if (!isWakeEvidence) {
9045 return;
9046 }
9047 + const retired = (yield* Ref.get(requestedContinuations)).get(
9048 + wakeInput.nativeThreadId,
9049 + )?.retired;
9050 + if (
9051 + retired === "unopened" &&
9052 + (isPendingTaskNotification || isPendingSubagentNotification)
9053 + ) {
9054 + yield* releaseContinuationLatch(wakeInput.nativeThreadId);
9055 + } else if (
9056 + retired != null &&
9057 + (isClaudeRootTurnFrame(message) ||
9058 + message.type === "result" ||
9059 + message.type === "rate_limit_event")
9060 + ) {
9061 + if (isClaudeEmptyTaskNotificationSuccess(message)) {
9062 + yield* setRetiredLatch(wakeInput.nativeThreadId, "unopened");
9063 + } else if (message.type === "result") {
9064 + yield* releaseContinuationLatch(wakeInput.nativeThreadId);
9065 + yield* offerBufferedWake(wakeInput.nativeThreadId);
9066 + } else if (isClaudeRootTurnFrame(message)) {
9067 + yield* setRetiredLatch(wakeInput.nativeThreadId, "opened");
9068 + yield* dropSettledSubagents(
9069 + wakeInput.nativeThreadId,
9070 + claudeSettledSubagentToolUseIds([message]),
9071 + );
9072 + }
9073 + return;
9074 + }
9075 const notificationSummary =
9076 isNotification && typeof message.summary === "string" && message.summary.length > 0
9077 ? message.summary
@@ -9010,6 +9122,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9122 message.task_id,
9123 );
9124 }
9125 + if (message.type === "result" && !isClaudeEmptyTaskNotificationSuccess(message)) {
9126 + yield* Ref.update(requestedContinuations, (current) => {
9127 + const latch = current.get(wakeInput.nativeThreadId);
9128 + if (latch === undefined || latch.end !== undefined) return current;
9129 + return new Map(current).set(wakeInput.nativeThreadId, { ...latch, end: message });
9130 + });
9131 + }
9132 // Open continuations only after root init, user, assistant, or result frames prove a wake began.
9133 // Background child frames can stream while the root is idle.
9134 const buffered = (yield* Ref.get(wakeBuffers)).get(wakeInput.nativeThreadId);
@@ -9029,30 +9148,38 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9148 ) {
9149 return;
9150 }
9032 - const route = (yield* Ref.get(lastTurnRouteByNativeThread)).get(wakeInput.nativeThreadId);
9151 + yield* offerWakeContinuation(wakeInput.nativeThreadId);
9152 + });
9153 +
9154 + const offerWakeContinuation = Effect.fnUntraced(function* (nativeThreadId: string) {
9155 + const route = (yield* Ref.get(lastTurnRouteByNativeThread)).get(nativeThreadId);
9156 if (route === undefined) {
9157 yield* Effect.logWarning("orchestration-v2.claude-wake-turn-unroutable", {
9158 providerSessionId: input.providerSessionId,
9036 - nativeThreadId: wakeInput.nativeThreadId,
9159 + nativeThreadId,
9160 });
9161 return;
9162 }
9040 - const nativeThreadId = wakeInput.nativeThreadId;
9041 - const latch = Symbol(nativeThreadId);
9163 + const buffered = (yield* Ref.get(wakeBuffers)).get(nativeThreadId);
9164 + const token = Symbol(nativeThreadId);
9165 const shouldOffer = yield* Ref.modify(requestedContinuations, (current) => {
9166 if (current.has(nativeThreadId)) {
9167 return [false, current] as const;
9168 }
9169 const updated = new Map(current);
9047 - updated.set(nativeThreadId, latch);
9170 + updated.set(nativeThreadId, {
9171 + token,
9172 + end: buffered?.messages.find(
9173 + (entry) => entry.type === "result" && !isClaudeEmptyTaskNotificationSuccess(entry),
9174 + ),
9175 + retired: null,
9176 + });
9177 return [true, updated] as const;
9178 });
9179 if (!shouldOffer) {
9180 return;
9181 }
9053 - const detail =
9054 - (yield* Ref.get(wakeBuffers)).get(wakeInput.nativeThreadId)?.detail ?? null;
9055 - const reports = yield* takeWakeReports(wakeInput.nativeThreadId);
9182 + const reports = yield* takeWakeReports(nativeThreadId);
9183 const notification = backgroundWorkNotification(
9184 [...(reports?.values() ?? [])].map((entry) => entry.report),
9185 );
@@ -9061,25 +9188,156 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
9188 threadId: route.threadId,
9189 providerThreadId: route.providerThreadId,
9190 });
9191 + // A process reset can clear and re-latch this thread; a stale request must not retire the newer wake.
9192 + const retire = (): Effect.Effect<void> =>
9193 + sdkMessageLock.withPermits(1)(retireOfferedWake(nativeThreadId, token));
9194 yield* continuationRequests.offer({
9195 threadId: route.threadId,
9196 providerThreadId: route.providerThreadId,
9197 driver: CLAUDE_PROVIDER,
9068 - detail,
9198 + detail: buffered?.detail ?? null,
9199 ...(notification === null ? {} : { notification }),
9070 - // A process reset can clear and re-latch this thread; a stale failure must not release the newer wake.
9071 - clearIfCurrent: () =>
9072 - Ref.update(requestedContinuations, (current) => {
9073 - if (current.get(nativeThreadId) !== latch) {
9074 - return current;
9075 - }
9076 - const updated = new Map(current);
9077 - updated.delete(nativeThreadId);
9078 - return updated;
9079 - }),
9200 + clearIfCurrent: retire,
9201 + onRunEndedWithoutStarting: retire,
9202 });
9203 });
9204
9205 + // Drops what the unstarted continuation would have shown. Starts of subagents still running stay
9206 + // because later child frames and notifications resolve their owner from them.
9207 + const retireOfferedWake = Effect.fnUntraced(function* (
9208 + nativeThreadId: string,
9209 + token: symbol,
9210 + ) {
9211 + const latch = (yield* Ref.get(requestedContinuations)).get(nativeThreadId);
9212 + if (latch?.token !== token) return;
9213 + const buffered = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages;
9214 + const messages = buffered ?? [];
9215 + const endIndex =
9216 + latch.end === undefined ? messages.length - 1 : messages.indexOf(latch.end);
9217 + const retired = messages.slice(0, endIndex + 1);
9218 + const later = messages.slice(endIndex + 1);
9219 + const retiredTaskIds = new Set(
9220 + retired.flatMap((entry) =>
9221 + entry.type === "system" && entry.subtype === "task_notification"
9222 + ? [entry.task_id]
9223 + : [],
9224 + ),
9225 + );
9226 + const settledToolUseIds = claudeSettledSubagentToolUseIds(retired);
9227 + const kept = [
9228 + ...retired.filter(
9229 + (entry) =>
9230 + entry.type === "system" &&
9231 + entry.subtype === "task_started" &&
9232 + !retiredTaskIds.has(entry.task_id) &&
9233 + (entry.tool_use_id === undefined || !settledToolUseIds.has(entry.tool_use_id)),
9234 + ),
9235 + ...later,
9236 + ];
9237 + if (!(yield* replaceWakeBuffer(nativeThreadId, buffered, kept, retiredTaskIds))) return;
9238 + if (latch.end === undefined) {
9239 + // A lifecycle result closes the root frames before it without opening the wake's turn.
9240 + const sinceLifecycleResult = retired.slice(
9241 + retired.findLastIndex(isClaudeEmptyTaskNotificationSuccess) + 1,
9242 + );
9243 + yield* setRetiredLatch(
9244 + nativeThreadId,
9245 + sinceLifecycleResult.some(isClaudeRootTurnFrame) ? "opened" : "unopened",
9246 + );
9247 + return;
9248 + }
9249 + yield* releaseContinuationLatch(nativeThreadId);
9250 + yield* forgetAbsorbedWake(nativeThreadId);
9251 + yield* offerBufferedWake(nativeThreadId);
9252 + });
9253 +
9254 + const setRetiredLatch = (nativeThreadId: string, retired: "unopened" | "opened") =>
9255 + Ref.update(requestedContinuations, (current) => {
9256 + const latch = current.get(nativeThreadId);
9257 + return latch === undefined || latch.retired === retired
9258 + ? current
9259 + : new Map(current).set(nativeThreadId, { ...latch, retired });
9260 + });
9261 +
9262 + // Compare-and-set: a process reset clears the buffer without the SDK message lock, and must not be undone.
9263 + const replaceWakeBuffer = Effect.fnUntraced(function* (
9264 + nativeThreadId: string,
9265 + expected: ReadonlyArray<SDKMessage> | undefined,
9266 + messages: ReadonlyArray<SDKMessage>,
9267 + droppedTaskIds: ReadonlySet<string>,
9268 + ) {
9269 + const replaced = yield* Ref.modify(wakeBuffers, (current) => {
9270 + if (current.get(nativeThreadId)?.messages !== expected)
9271 + return [false, current] as const;
9272 + const updated = new Map(current);
9273 + if (messages.length === 0) updated.delete(nativeThreadId);
9274 + else
9275 + updated.set(nativeThreadId, { messages, detail: claudeWakeBufferDetail(messages) });
9276 + return [true, updated] as const;
9277 + });
9278 + if (!replaced) return false;
9279 + yield* Ref.update(wakeReportsByNativeThread, ({ userTurns, reports }) => {
9280 + const threadReports = reports.get(nativeThreadId);
9281 + if (threadReports === undefined) return { userTurns, reports };
9282 + const remaining = new Map(
9283 + [...threadReports].filter(([taskId]) => !droppedTaskIds.has(taskId)),
9284 + );
9285 + const updatedReports = new Map(reports);
9286 + if (remaining.size === 0) updatedReports.delete(nativeThreadId);
9287 + else updatedReports.set(nativeThreadId, remaining);
9288 + return { userTurns, reports: updatedReports };
9289 + });
9290 + return true;
9291 + });
9292 +
9293 + // A retired wake's dropped tool results still settle the subagents it kept for later frames.
9294 + const dropSettledSubagents = Effect.fnUntraced(function* (
9295 + nativeThreadId: string,
9296 + settledToolUseIds: ReadonlySet<string>,
9297 + ) {
9298 + if (settledToolUseIds.size === 0) return;
9299 + const buffered = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages;
9300 + const messages = buffered ?? [];
9301 + const settledTaskIds = new Set(
9302 + messages.flatMap((entry) =>
9303 + entry.type === "system" &&
9304 + entry.subtype === "task_started" &&
9305 + entry.tool_use_id !== undefined &&
9306 + settledToolUseIds.has(entry.tool_use_id)
9307 + ? [entry.task_id]
9308 + : [],
9309 + ),
9310 + );
9311 + const kept = messages.filter((entry) => {
9312 + const parentToolUseId = parentToolUseIdFromSdkMessage(entry);
9313 + return (
9314 + (parentToolUseId === null || !settledToolUseIds.has(parentToolUseId)) &&
9315 + !(entry.type === "system" && "task_id" in entry && settledTaskIds.has(entry.task_id))
9316 + );
9317 + });
9318 + if (kept.length === messages.length) return;
9319 + yield* replaceWakeBuffer(nativeThreadId, buffered, kept, settledTaskIds);
9320 + });
9321 +
9322 + // After a retired wake releases its latch, a wake buffered behind it has had no offer of its own.
9323 + const offerBufferedWake = Effect.fnUntraced(function* (nativeThreadId: string) {
9324 + const messages = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
9325 + const reports = (yield* Ref.get(wakeReportsByNativeThread)).reports.get(nativeThreadId);
9326 + // A lifecycle result ends no turn, and closes the root frames before it.
9327 + const hasWake =
9328 + messages
9329 + .slice(messages.findLastIndex(isClaudeEmptyTaskNotificationSuccess) + 1)
9330 + .some(isClaudeRootTurnFrame) ||
9331 + messages.some(
9332 + (entry) =>
9333 + (entry.type === "result" && !isClaudeEmptyTaskNotificationSuccess(entry)) ||
9334 + (entry.type === "system" &&
9335 + entry.subtype === "task_notification" &&
9336 + reports?.get(entry.task_id)?.report.kind === "subagent"),
9337 + );
9338 + if (hasWake) yield* offerWakeContinuation(nativeThreadId);
9339 + });
9340 +
9341 // Untraced like the roster message handling: it runs for every streamed roster change.
9342 const emitRosterUpdate = Effect.fnUntraced(function* (
9343 nativeThreadId: string,
@@ -10241,13 +10499,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
10499 subagent !== undefined &&
10500 findToolCall(context, toolResult.tool_use_id) === undefined
10501 ) {
10244 - // Background Agent launches ACK immediately; only task_notification settles them.
10245 - if (
10246 - !isClaudeToolResultError(toolResult) &&
10247 - (runHandles !== undefined || isClaudeSubagentAsyncLaunchAck(output))
10248 - ) {
10249 - continue;
10250 - }
10502 + if (!claudeToolResultSettlesSubagent(toolResult, output)) continue;
10503 const result = claudeSubagentResultText(output);
10504 yield* updateClaudeSubagentNode({
10505 context,
@@ -11922,15 +12174,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
12174 .some((entry) => isClaudeTurnStartMessage(entry));
12175 // The CLI can append empty task successes after a failure. Filter
12176 // those for replay without changing the raw-tail successor guard.
11925 - const isEmptyTaskNotificationSuccess = (entry: SDKMessage) =>
11926 - isClaudeTaskNotificationOriginResult(entry) &&
11927 - entry.subtype === "success" &&
11928 - !entry.is_error &&
11929 - entry.num_turns === 0 &&
11930 - entry.result.length === 0 &&
11931 - terminalStatusFromResult(entry) === "completed";
12177 const replayResultIndex = drained.findLastIndex(
11933 - (entry) => entry.type === "result" && !isEmptyTaskNotificationSuccess(entry),
12178 + (entry) =>
12179 + entry.type === "result" && !isClaudeEmptyTaskNotificationSuccess(entry),
12180 );
12181 const lastResult =
12182 drained[replayResultIndex === -1 ? lastResultIndex : replayResultIndex];
apps/server/src/orchestration-v2/ClaudeAutomaticDelivery.integration.test.ts
+290
@@ -15,6 +15,7 @@ import {
15 type OrchestrationV2DomainEvent,
16 } from "@t3tools/contracts";
17 import * as DateTime from "effect/DateTime";
18 +import * as Deferred from "effect/Deferred";
19 import * as SecureRandom from "effect/crypto/SecureRandom";
20 import * as Effect from "effect/Effect";
21 import * as FileSystem from "effect/FileSystem";
@@ -36,6 +37,7 @@ import * as EffectWorker from "./EffectWorker.ts";
37 import * as EventSink from "./EventSink.ts";
38 import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
39 import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions";
40 +import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
41 import * as LegacyV1ThreadImporter from "./legacy/LegacyV1ThreadImporter.ts";
42 import * as Orchestrator from "./Orchestrator.ts";
43 import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
@@ -610,3 +612,291 @@ it.effect("applies an effort change to the running Claude query without waiting
612 ),
613 ),
614 );
615 +
616 +const backgroundTaskStarted = (taskId: string) =>
617 + frame({
618 + type: "system",
619 + subtype: "task_started",
620 + task_id: taskId,
621 + tool_use_id: `tool-${taskId}`,
622 + description: "Background build",
623 + is_backgrounded: true,
624 + task_type: "local_bash",
625 + uuid: `started-${taskId}`,
626 + session_id: sessionId,
627 + });
628 +
629 +const backgroundTaskNotification = (taskId: string) =>
630 + frame({
631 + type: "system",
632 + subtype: "task_notification",
633 + task_id: taskId,
634 + tool_use_id: `tool-${taskId}`,
635 + status: "completed",
636 + output_file: `/tmp/claude-replay/tasks/${taskId}.output`,
637 + summary: `Background build ${taskId} completed`,
638 + uuid: `notification-${taskId}`,
639 + session_id: sessionId,
640 + });
641 +
642 +const wakeAssistant = (uuid: string) =>
643 + frame({
644 + type: "assistant",
645 + uuid,
646 + session_id: sessionId,
647 + parent_tool_use_id: null,
648 + message: {
649 + id: `msg-${uuid}`,
650 + role: "assistant",
651 + type: "message",
652 + model: modelSelection.model,
653 + content: [{ type: "text", text: `Background build finished (${uuid}).` }],
654 + stop_reason: null,
655 + stop_sequence: null,
656 + usage: { input_tokens: 1, output_tokens: 1 },
657 + },
658 + });
659 +
660 +it.effect("offers a continuation for a later wake after a queued continuation is cancelled", () =>
661 + Effect.scoped(
662 + Effect.gen(function* () {
663 + const cwd = yield* checkpointWorkspace("claude-cancelled-continuation");
664 + const sdkMessages = yield* Queue.unbounded<SDKMessage>();
665 + const processed = new WeakMap<SDKMessage, Deferred.Deferred<void>>();
666 + const offerAndWait = Effect.fnUntraced(function* (message: SDKMessage) {
667 + const done = yield* Deferred.make<void>();
668 + processed.set(message, done);
669 + yield* Queue.offer(sdkMessages, message);
670 + yield* Deferred.await(done);
671 + });
672 + const fileSystem = yield* FileSystem.FileSystem;
673 + const path = yield* Path.Path;
674 + const random = yield* SecureRandom.SecureRandom;
675 + const idAllocator = yield* IdAllocator.IdAllocatorV2;
676 + const continuations: ProviderContinuationRequests.ProviderContinuationRequest[] = [];
677 + const services =
678 + yield* Effect.context<
679 + Effect.Services<ReturnType<typeof ClaudeAdapterV2.makeClaudeAdapterV2>>
680 + >();
681 + const registry = ProviderAdapterRegistry.layerFromAdaptersEffect(
682 + Effect.gen(function* () {
683 + const requests = yield* ProviderContinuationRequests.ProviderContinuationRequests;
684 + const adapter = yield* ClaudeAdapterV2.makeClaudeAdapterV2({
685 + instanceId: modelSelection.instanceId,
686 + settings,
687 + environment: {},
688 + attachmentsDir: cwd,
689 + fileSystem,
690 + path,
691 + random,
692 + idAllocator,
693 + continuationRequests: {
694 + offer: (request) =>
695 + Effect.sync(() => continuations.push(request)).pipe(
696 + Effect.andThen(requests.offer(request)),
697 + ),
698 + },
699 + queryRunner: {
700 + allocateSessionId: Effect.succeed(sessionId),
701 + open: () =>
702 + Effect.succeed({
703 + // Resolve each frame's Deferred once the adapter pulls the next one.
704 + messages: Stream.fromQueue(sdkMessages).pipe(
705 + Stream.flatMap((message) =>
706 + Stream.make(message).pipe(
707 + Stream.concat(
708 + Stream.fromEffect(
709 + Effect.suspend(() => {
710 + const done = processed.get(message);
711 + return done === undefined
712 + ? Effect.void
713 + : Deferred.succeed(done, undefined);
714 + }),
715 + ).pipe(Stream.drain),
716 + ),
717 + ),
718 + ),
719 + ),
720 + offer: () => Effect.void,
721 + setModel: () => Effect.void,
722 + applyFlagSettings: () => Effect.void,
723 + setPermissionMode: () => Effect.void,
724 + backgroundTasks: () => Effect.succeed(false),
725 + mcpServerStatus: Effect.succeed([]),
726 + readMcpResource: () => Effect.die("unexpected MCP resource read"),
727 + callMcpTool: () => Effect.die("unexpected MCP tool call"),
728 + listPermissionRules: Effect.die("unexpected permission rules read"),
729 + stopTask: () => Effect.void,
730 + interrupt: Effect.void,
731 + close: Effect.void,
732 + }),
733 + forkSession: () => Effect.die("unused"),
734 + subagentLaunchToolUseId: () => Effect.succeed(null),
735 + assertComplete: Effect.void,
736 + },
737 + }).pipe(Effect.provideContext(services));
738 + return [adapter];
739 + }),
740 + );
741 + yield* Effect.gen(function* () {
742 + const orchestrator = yield* Orchestrator.OrchestratorV2;
743 + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
744 + const threadId = ThreadId.make("thread:cancelled-continuation");
745 + const watch = (predicate: (event: OrchestrationV2DomainEvent) => boolean) =>
746 + orchestrator.streamDomainEvents.pipe(
747 + Stream.filter(predicate),
748 + Stream.take(1),
749 + Stream.runDrain,
750 + Effect.forkScoped,
751 + );
752 + const runUpdated = (messageId: string, status: string) =>
753 + watch(
754 + (event) =>
755 + event.type === "run.updated" &&
756 + event.payload.userMessageId === messageId &&
757 + event.payload.status === status,
758 + );
759 + // A user turn that leaves a background task running when it ends.
760 + const userTurnWithBackgroundTask = Effect.fnUntraced(function* (
761 + id: string,
762 + taskId: string,
763 + ) {
764 + const running = yield* runUpdated(id, "running");
765 + yield* orchestrator.dispatch({
766 + type: "message.dispatch",
767 + commandId: CommandId.make(id),
768 + threadId,
769 + messageId: MessageId.make(id),
770 + text: "Run the build in the background.",
771 + attachments: [],
772 + dispatchMode: { type: "start_immediately" },
773 + createdBy: "user",
774 + creationSource: "web",
775 + });
776 + yield* worker.drain();
777 + yield* Fiber.join(running);
778 + const waiting = yield* runUpdated(id, "waiting");
779 + yield* Queue.offer(sdkMessages, backgroundTaskStarted(taskId));
780 + yield* Queue.offer(sdkMessages, result(`result-${id}`, false));
781 + yield* Fiber.join(waiting);
782 + });
783 + yield* orchestrator.dispatch({
784 + type: "thread.create",
785 + commandId: CommandId.make("create"),
786 + threadId,
787 + projectId: ProjectId.make("project:cancelled-continuation"),
788 + title: "Cancelled continuation",
789 + modelSelection,
790 + runtimeMode: "full-access",
791 + interactionMode: "default",
792 + branch: null,
793 + worktreePath: cwd,
794 + createdBy: "user",
795 + creationSource: "web",
796 + });
797 +
798 + // The first run is still finalizing when its background task wakes Claude.
799 + yield* userTurnWithBackgroundTask("first", "build-1");
800 + const queued = yield* watch(
801 + (event) => event.type === "run.created" && event.payload.status === "queued",
802 + );
803 + yield* offerAndWait(backgroundTaskNotification("build-1"));
804 + yield* offerAndWait(wakeAssistant("wake-1"));
805 + yield* Fiber.join(queued);
806 + assert.lengthOf(continuations, 1);
807 + const continuationRun = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
808 + (run) => run.status === "queued",
809 + );
810 + assert.ok(continuationRun !== undefined);
811 + yield* orchestrator.dispatch({
812 + type: "queued-run.cancel",
813 + commandId: CommandId.make("cancel"),
814 + threadId,
815 + runId: continuationRun.id,
816 + });
817 + yield* offerAndWait(result("wake-1-result", false));
818 + yield* worker.drain();
819 + yield* orchestrator.resumeQueuedRuns;
820 + yield* worker.drain();
821 + const idle = yield* orchestrator.getThreadProjection(threadId);
822 + assert.equal(idle.runs.find((run) => run.id === continuationRun.id)?.status, "cancelled");
823 + assert.isFalse(idle.runs.some((run) => run.status === "queued"));
824 +
825 + yield* userTurnWithBackgroundTask("second", "build-2");
826 + yield* worker.drain();
827 + // The second user run has completed, so this continuation starts without queueing.
828 + const isContinuationRun = (
829 + event: OrchestrationV2DomainEvent,
830 + type: string,
831 + status: string,
832 + ) =>
833 + (event.type === "run.created" || event.type === "run.updated") &&
834 + event.type === type &&
835 + event.payload.status === status &&
836 + event.payload.userMessageId !== MessageId.make("first") &&
837 + event.payload.userMessageId !== MessageId.make("second");
838 + const secondCreated = yield* watch((event) =>
839 + isContinuationRun(event, "run.created", "starting"),
840 + );
841 + const secondRunning = yield* watch((event) =>
842 + isContinuationRun(event, "run.updated", "running"),
843 + );
844 + yield* offerAndWait(backgroundTaskNotification("build-2"));
845 + yield* offerAndWait(wakeAssistant("wake-2"));
846 + assert.lengthOf(
847 + continuations,
848 + 2,
849 + "the wake after a cancelled queued continuation must offer a new continuation",
850 + );
851 + yield* Fiber.join(secondCreated);
852 + yield* worker.drain();
853 + yield* Fiber.join(secondRunning);
854 + const secondContinuation = (yield* orchestrator.getThreadProjection(threadId)).runs.find(
855 + (run) => run.status === "running",
856 + );
857 + assert.ok(secondContinuation !== undefined);
858 + const secondWaiting = yield* watch(
859 + (event) =>
860 + event.type === "run.updated" &&
861 + event.payload.id === secondContinuation.id &&
862 + event.payload.status === "waiting",
863 + );
864 + yield* offerAndWait(result("wake-2-result", false));
865 + yield* Fiber.join(secondWaiting);
866 + yield* worker.drain();
867 + const settled = yield* orchestrator.getThreadProjection(threadId);
868 + assert.equal(
869 + settled.runs.find((run) => run.id === secondContinuation.id)?.status,
870 + "completed",
871 + );
872 + assert.deepEqual(
873 + settled.turnItems.flatMap((item) =>
874 + item.runId === secondContinuation.id && item.type === "assistant_message"
875 + ? [item.text]
876 + : [],
877 + ),
878 + ["Background build finished (wake-2)."],
879 + "the continuation must carry only its own wake's output",
880 + );
881 + assert.lengthOf(continuations, 2, "two wakes must offer exactly two continuations");
882 + }).pipe(
883 + Effect.provide(
884 + ProviderReplayHarness.layerWithRegistry(
885 + { name: "claude-cancelled-continuation" },
886 + registry,
887 + { runEffectWorker: false, runContinuationWorker: true },
888 + ),
889 + ),
890 + );
891 + }).pipe(
892 + Effect.provide(
893 + Layer.mergeAll(
894 + IdAllocator.layer,
895 + McpProviderSessions.layer,
896 + NodeServices.layer,
897 + NodeCrypto.layer,
898 + ),
899 + ),
900 + ),
901 + ),
902 +);
apps/server/src/orchestration-v2/ProviderContinuationService.ts
+75 -18
@@ -1,10 +1,17 @@
1 -import { CommandId, type OrchestrationV2ThreadProjection } from "@t3tools/contracts";
1 +import {
2 + CommandId,
3 + type MessageId,
4 + type OrchestrationV2ThreadProjection,
5 +} from "@t3tools/contracts";
6 import * as Effect from "effect/Effect";
7 import * as Layer from "effect/Layer";
8 +import * as Option from "effect/Option";
9 import * as Ref from "effect/Ref";
10 +import * as Stream from "effect/Stream";
11
12 import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
13 import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
14 +import type * as Orchestrator from "./Orchestrator.ts";
15 import * as ThreadManagementService from "./ThreadManagementService.ts";
16 import { isUndeliveredMailboxSteer } from "./NotificationMailbox.ts";
17
@@ -73,6 +80,54 @@ export const layer = Layer.effectDiscard(
80 return [Math.min(100 * 2 ** Math.min(attempt, 6), 5_000), updated] as const;
81 });
82
83 + // Cancelling or stopping a queued continuation ends it unstarted; only the request can release the adapter's wake latch.
84 + const releaseIfNeverStarted = Effect.fn("ProviderContinuationService.releaseIfNeverStarted")(
85 + function* (
86 + request: ProviderContinuationRequests.ProviderContinuationRequest,
87 + messageId: MessageId,
88 + dispatched: Orchestrator.OrchestratorV2DispatchResult,
89 + ) {
90 + const onRunEndedWithoutStarting = request.onRunEndedWithoutStarting;
91 + if (onRunEndedWithoutStarting === undefined) return;
92 + const runId = dispatched.storedEvents.flatMap(({ event }) =>
93 + event.type === "run.created" && event.payload.userMessageId === messageId
94 + ? [event.payload.id]
95 + : [],
96 + )[0];
97 + if (runId === undefined) return;
98 + yield* threads
99 + .streamStoredEventsFrom({
100 + threadId: request.threadId,
101 + afterSequence: dispatched.sequence,
102 + eventType: "run.updated",
103 + })
104 + .pipe(
105 + Stream.filter(
106 + ({ event }) =>
107 + event.type === "run.updated" &&
108 + event.payload.id === runId &&
109 + ThreadManagementService.isTerminalRunStatus(event.payload.status),
110 + ),
111 + Stream.runHead,
112 + Effect.flatMap((ended) =>
113 + Option.isSome(ended) &&
114 + ended.value.event.type === "run.updated" &&
115 + ended.value.event.payload.startedAt === null
116 + ? onRunEndedWithoutStarting()
117 + : Effect.void,
118 + ),
119 + Effect.catchCause((cause) =>
120 + Effect.logWarning("orchestration-v2.provider-continuation.run-watch-failed", {
121 + threadId: request.threadId,
122 + providerThreadId: request.providerThreadId,
123 + cause,
124 + }),
125 + ),
126 + Effect.forkScoped,
127 + );
128 + },
129 + );
130 +
131 const dispatchContinuation = Effect.fn("ProviderContinuationService.dispatchContinuation")(
132 function* (request: ProviderContinuationRequests.ProviderContinuationRequest) {
133 const projection = yield* threads.getThreadRecords(
@@ -186,23 +241,25 @@ export const layer = Layer.effectDiscard(
241 ordinal: projection.messages.length + 1,
242 }));
243 const commandId = CommandId.make(`provider-continuation:${messageId}`);
189 - const dispatch = threads.dispatch({
190 - type: "message.dispatch",
191 - commandId,
192 - threadId: request.threadId,
193 - messageId,
194 - text: request.detail ?? CONTINUATION_MESSAGE_TEXT,
195 - notification: request.notification ?? {
196 - source: { kind: "background_task" },
197 - outcome: "updated",
198 - summary: "Background activity updated",
199 - },
200 - attachments: [],
201 - dispatchMode: { type: "queue_after_active" },
202 - createdBy: "agent",
203 - // Only buffered wakes use creationSource "provider"; Claude would drain an empty buffer instead of prompting message_text.
204 - creationSource: request.delivery === "message_text" ? "server" : "provider",
205 - });
244 + const dispatch = threads
245 + .dispatch({
246 + type: "message.dispatch",
247 + commandId,
248 + threadId: request.threadId,
249 + messageId,
250 + text: request.detail ?? CONTINUATION_MESSAGE_TEXT,
251 + notification: request.notification ?? {
252 + source: { kind: "background_task" },
253 + outcome: "updated",
254 + summary: "Background activity updated",
255 + },
256 + attachments: [],
257 + dispatchMode: { type: "queue_after_active" },
258 + createdBy: "agent",
259 + // Only buffered wakes use creationSource "provider"; Claude would drain an empty buffer instead of prompting message_text.
260 + creationSource: request.delivery === "message_text" ? "server" : "provider",
261 + })
262 + .pipe(Effect.tap((dispatched) => releaseIfNeverStarted(request, messageId, dispatched)));
263 // Catch after dispatchIfCurrent so adapters see raw failures first; clearIfCurrent is idempotent and covers others.
264 const logInstanceDisabled = (
265 error: ThreadManagementService.ThreadManagementProviderInstanceDisabledError,
apps/server/src/orchestration-v2/testkit/ProviderReplayHarness.ts
+1
@@ -518,6 +518,7 @@ export function layerWithRegistry<Error>(
518 recoverDelegatedTask: orchestrator.recoverDelegatedTask,
519 cancelDelegatedTask: orchestrator.cancelDelegatedTask,
520 delegatedTaskResultPending: orchestrator.delegatedTaskResultPending,
521 + streamStoredEventsFrom: orchestrator.streamStoredEventsFrom,
522 });
523 }),
524 ).pipe(Layer.provide(layerOrchestratorProvided));
packages/provider-core/src/server/ProviderContinuationRequests.ts
+2
@@ -30,6 +30,8 @@ export interface ProviderContinuationRequest {
30 effect: Effect.Effect<A, E, R>,
31 ) => Effect.Effect<Option.Option<A>, E, R>;
32 readonly clearIfCurrent?: () => Effect.Effect<void>;
33 + /** Called when the run this request dispatched ends without starting, such as a cancelled queued run. */
34 + readonly onRunEndedWithoutStarting?: () => Effect.Effect<void>;
35 }
36
37 /** The default drops requests; share the live layer with the worker that drains them. */