@@ -9210,7 +9210,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92109210 ) {
92119211 const latch = (yield* Ref.get(requestedContinuations)).get(nativeThreadId);
92129212 if (latch?.token !== token) return;
9213- const messages = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
9213+ const buffered = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages;
9214+ const messages = buffered ?? [];
92149215 const endIndex =
92159216 latch.end === undefined ? messages.length - 1 : messages.indexOf(latch.end);
92169217 const retired = messages.slice(0, endIndex + 1);
@@ -9233,7 +9234,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92339234 ),
92349235 ...later,
92359236 ];
9236- yield* replaceWakeBuffer(nativeThreadId, kept, retiredTaskIds);
9237+ if (!( yield* replaceWakeBuffer(nativeThreadId, buffered, kept, retiredTaskIds))) return ;
92379238 if (latch.end === undefined) {
92389239 // A lifecycle result closes the root frames before it without opening the wake's turn.
92399240 const sinceLifecycleResult = retired.slice(
@@ -9258,18 +9259,23 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92589259 : new Map(current).set(nativeThreadId, { ...latch, retired });
92599260 });
92609261
9262+ // Compare-and-set: a process reset clears the buffer without the SDK message lock, and must not be undone.
92619263 const replaceWakeBuffer = Effect.fnUntraced(function* (
92629264 nativeThreadId: string,
9265+ expected: ReadonlyArray<SDKMessage> | undefined,
92639266 messages: ReadonlyArray<SDKMessage>,
92649267 droppedTaskIds: ReadonlySet<string>,
92659268 ) {
9266- yield* Ref.update(wakeBuffers, (current) => {
9269+ const replaced = yield* Ref.modify(wakeBuffers, (current) => {
9270+ if (current.get(nativeThreadId)?.messages !== expected)
9271+ return [false, current] as const;
92679272 const updated = new Map(current);
92689273 if (messages.length === 0) updated.delete(nativeThreadId);
92699274 else
92709275 updated.set(nativeThreadId, { messages, detail: claudeWakeBufferDetail(messages) });
9271- return updated;
9276+ return [true, updated] as const ;
92729277 });
9278+ if (!replaced) return false;
92739279 yield* Ref.update(wakeReportsByNativeThread, ({ userTurns, reports }) => {
92749280 const threadReports = reports.get(nativeThreadId);
92759281 if (threadReports === undefined) return { userTurns, reports };
@@ -9281,6 +9287,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92819287 else updatedReports.set(nativeThreadId, remaining);
92829288 return { userTurns, reports: updatedReports };
92839289 });
9290+ return true;
92849291 });
92859292
92869293 // A retired wake's dropped tool results still settle the subagents it kept for later frames.
@@ -9289,7 +9296,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
92899296 settledToolUseIds: ReadonlySet<string>,
92909297 ) {
92919298 if (settledToolUseIds.size === 0) return;
9292- const messages = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
9299+ const buffered = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages;
9300+ const messages = buffered ?? [];
92939301 const settledTaskIds = new Set(
92949302 messages.flatMap((entry) =>
92959303 entry.type === "system" &&
@@ -9308,21 +9316,25 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
93089316 );
93099317 });
93109318 if (kept.length === messages.length) return;
9311- yield* replaceWakeBuffer(nativeThreadId, kept, settledTaskIds);
9319+ yield* replaceWakeBuffer(nativeThreadId, buffered, kept, settledTaskIds);
93129320 });
93139321
93149322 // After a retired wake releases its latch, a wake buffered behind it has had no offer of its own.
93159323 const offerBufferedWake = Effect.fnUntraced(function* (nativeThreadId: string) {
93169324 const messages = (yield* Ref.get(wakeBuffers)).get(nativeThreadId)?.messages ?? [];
93179325 const reports = (yield* Ref.get(wakeReportsByNativeThread)).reports.get(nativeThreadId);
9318- const hasWake = messages.some(
9319- (entry) =>
9320- isClaudeRootTurnFrame(entry) ||
9321- entry.type === "result" ||
9322- (entry.type === "system" &&
9323- entry.subtype === "task_notification" &&
9324- reports?.get(entry.task_id)?.report.kind === "subagent"),
9325- );
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+ );
93269338 if (hasWake) yield* offerWakeContinuation(nativeThreadId);
93279339 });
93289340
0 commit comments