Skip to content

Commit 88635e0

Browse files
fix(server): an idle task notification no longer pins a Claude wakeup past its fire
The notification marker outlived the turn it was meant for: after a lone empty notification turn, or none at all, it stayed set, so Claude's early wake turn kept the hold and the thread stayed Working after the wakeup ran. An idle turn opened inside the early-fire margin is now judged by its result instead: a result that is not a task notification's is the wakeup and releases it. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 28aaa06 commit 88635e0

2 files changed

Lines changed: 106 additions & 24 deletions

File tree

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10733,6 +10733,91 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1073310733
),
1073410734
);
1073510735

10736+
it.effect.each([{ emptyResult: true }, { emptyResult: false }])(
10737+
"an early wakeup after an idle task notification releases its hold (empty result: $emptyResult)",
10738+
({ emptyResult }) =>
10739+
Effect.scoped(
10740+
Effect.gen(function* () {
10741+
const harness = yield* makeWakeHarness;
10742+
const now = yield* DateTime.now;
10743+
yield* harness.runtime.startTurn(
10744+
makeClaudeTestTurnInput({
10745+
threadId: harness.threadId,
10746+
providerThread: harness.providerThread,
10747+
now,
10748+
attemptId: RunAttemptId.make("attempt-claude-schedule-wakeup-notified"),
10749+
text: "Run the build in the background and check back in two minutes.",
10750+
attachments: [],
10751+
}),
10752+
);
10753+
yield* Queue.offer(harness.sdkMessages, wakeTaskStarted);
10754+
for (const frame of scheduleWakeupFrames({
10755+
toolUseId: "toolu_schedule_wakeup_notified",
10756+
uuids: ["00000000-0000-4000-8000-000000002601", "00000000-0000-4000-8000-000000002602"],
10757+
toolInput: { delaySeconds: 120, prompt: "check the build", reason: "fallback" },
10758+
scheduledFor: DateTime.toEpochMillis(now) + 120_000,
10759+
})) {
10760+
yield* Queue.offer(harness.sdkMessages, frame);
10761+
}
10762+
yield* Queue.offer(harness.sdkMessages, turnOneResult);
10763+
yield* harness.awaitTerminal(1);
10764+
10765+
yield* TestClock.adjust("10 seconds");
10766+
yield* harness.offerAndWait(wakeNotification);
10767+
if (emptyResult) {
10768+
yield* harness.offerAndWait(
10769+
makeResultFrame({
10770+
uuid: "00000000-0000-4000-8000-000000002603",
10771+
result: "",
10772+
numTurns: 0,
10773+
origin: { kind: "task-notification" },
10774+
terminalReason: null,
10775+
}),
10776+
);
10777+
}
10778+
10779+
yield* TestClock.adjust("80 seconds");
10780+
yield* harness.offerAndWait(wakeTurnInit);
10781+
yield* harness.awaitContinuation(1);
10782+
yield* harness.runtime.startTurn(
10783+
makeClaudeTestTurnInput({
10784+
threadId: harness.threadId,
10785+
providerThread: harness.providerThread,
10786+
now: yield* DateTime.now,
10787+
attemptId: RunAttemptId.make("attempt-claude-schedule-wakeup-notified-2"),
10788+
text: "check the build",
10789+
attachments: [],
10790+
providerTurnOrdinal: 2,
10791+
messageCreatedBy: "agent",
10792+
messageCreationSource: "provider",
10793+
}),
10794+
);
10795+
yield* harness.offerAndWait(
10796+
makeAssistantTextFrame({
10797+
uuid: "00000000-0000-4000-8000-000000002604",
10798+
text: "Built.",
10799+
}),
10800+
);
10801+
yield* harness.offerAndWait(
10802+
makeResultFrame({ uuid: "00000000-0000-4000-8000-000000002605", result: "Built." }),
10803+
);
10804+
yield* harness.awaitTerminal(2);
10805+
assert.equal(harness.terminalEvents()[1]?.status, "completed");
10806+
assert.isFalse(backgroundWorkHoldsCompletion(publishedRoster(harness)));
10807+
assert.isFalse(yield* harness.hasPendingBackgroundWork);
10808+
}).pipe(
10809+
Effect.provide(
10810+
Layer.mergeAll(
10811+
IdAllocator.layer,
10812+
McpProviderSessions.layer,
10813+
NodeServices.layer,
10814+
NodeCrypto.layer,
10815+
),
10816+
),
10817+
),
10818+
),
10819+
);
10820+
1073610821
it.effect("a failed turn on a live Claude process keeps the pending wakeup's hold", () =>
1073710822
Effect.scoped(
1073810823
Effect.gen(function* () {

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5367,37 +5367,33 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
53675367
// mode change; replacing the process cancels it, and that clears this record too.
53685368
const scheduleWakeupCalls = new Set<string>();
53695369
const scheduledWakeupsByNativeThread = new Map<string, number>();
5370-
const notifiedWhileIdleNativeThreads = new Set<string>();
5370+
// Native threads whose current turn Claude started by itself inside the early-fire margin.
5371+
const possibleEarlyWakeNativeThreads = new Set<string>();
53715372
const trackClaudeScheduledWakeup = Effect.fnUntraced(function* (
53725373
nativeThreadId: string,
53735374
message: SDKMessage,
53745375
) {
5375-
if (message.type === "system" && message.subtype === "task_notification") {
5376-
if ((yield* Ref.get(activeTurn)) === null) {
5377-
notifiedWhileIdleNativeThreads.add(nativeThreadId);
5378-
}
5379-
return;
5380-
}
5381-
// An empty notification turn is often followed by the real one, so only a result
5382-
// that did work ends the notification's claim on the next idle turn.
5383-
if (
5384-
message.type === "result" &&
5385-
!(isClaudeTaskNotificationOriginResult(message) && message.num_turns === 0)
5386-
) {
5387-
notifiedWhileIdleNativeThreads.delete(nativeThreadId);
5388-
return;
5389-
}
53905376
if (isClaudeTurnStartMessage(message)) {
53915377
const scheduledFor = scheduledWakeupsByNativeThread.get(nativeThreadId);
53925378
if (scheduledFor === undefined) return;
5393-
// Only a turn Claude starts by itself, with no task notification before it, can be
5394-
// the early-jittered wake; anything else leaves the wakeup pending until scheduledFor.
5395-
const earliestWake =
5379+
const now = DateTime.toEpochMillis(yield* DateTime.now);
5380+
if (now >= scheduledFor) {
5381+
scheduledWakeupsByNativeThread.delete(nativeThreadId);
5382+
} else if (
53965383
(yield* Ref.get(activeTurn)) === null &&
5397-
!notifiedWhileIdleNativeThreads.has(nativeThreadId)
5398-
? scheduledFor - CLAUDE_SCHEDULED_WAKEUP_EARLY_FIRE_MARGIN_MS
5399-
: scheduledFor;
5400-
if (DateTime.toEpochMillis(yield* DateTime.now) >= earliestWake) {
5384+
now >= scheduledFor - CLAUDE_SCHEDULED_WAKEUP_EARLY_FIRE_MARGIN_MS
5385+
) {
5386+
possibleEarlyWakeNativeThreads.add(nativeThreadId);
5387+
}
5388+
return;
5389+
}
5390+
// A task notification also starts a turn by itself, so an early turn is the wake only
5391+
// when its result is not a task notification's.
5392+
if (message.type === "result") {
5393+
if (
5394+
possibleEarlyWakeNativeThreads.delete(nativeThreadId) &&
5395+
!isClaudeTaskNotificationOriginResult(message)
5396+
) {
54015397
scheduledWakeupsByNativeThread.delete(nativeThreadId);
54025398
}
54035399
return;
@@ -5417,6 +5413,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
54175413
: undefined;
54185414
// stop:true reports scheduledFor 0. A failed call also clears: losing the hold early
54195415
// is recoverable, a hold that outlives its wakeup is not.
5416+
possibleEarlyWakeNativeThreads.delete(nativeThreadId);
54205417
if (typeof scheduledFor === "number" && scheduledFor > 0) {
54215418
scheduledWakeupsByNativeThread.set(nativeThreadId, scheduledFor);
54225419
} else {
@@ -6022,7 +6019,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
60226019
bufferedTaskNotificationIds.has(taskId),
60236020
);
60246021
scheduledWakeupsByNativeThread.delete(nativeThreadId);
6025-
notifiedWhileIdleNativeThreads.delete(nativeThreadId);
6022+
possibleEarlyWakeNativeThreads.delete(nativeThreadId);
60266023
yield* clearPendingBackgroundTasksForNativeThread(nativeThreadId);
60276024
yield* clearNativeThreadTaskIdSet(
60286025
wakeEligibleBackgroundTasksByNativeThread,

0 commit comments

Comments
 (0)