Skip to content

Commit e035f24

Browse files
committed
refactor: dedupe queued-message foreground backgrounding in TaskService
#3605 introduced two near-identical guard blocks that background registered foreground waits when a tool-end message is already queued (one in the workspace-turn wait path, one in the task-await path). Extract a single private helper backgroundForegroundWaitIfQueued() shared by both sites. Behavior-preserving: the helper folds in the requestingWorkspaceId truthiness guard, which is always true at the workspace-turn site (asserted non-empty) and already implied by shouldBackgroundOnQueuedMessage at the task-await site.
1 parent 3d85cf9 commit e035f24

1 file changed

Lines changed: 28 additions & 13 deletions

File tree

src/node/services/taskService.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3806,6 +3806,26 @@ export class TaskService {
38063806
return count;
38073807
}
38083808

3809+
/**
3810+
* Background any registered foreground waits for the requesting workspace when a
3811+
* tool-end message is already queued. Shared by both wait-registration paths
3812+
* (workspace-turn and task await): the auto-backgrounding signal is edge-triggered
3813+
* on enqueue, so a message queued before the waiter registered must be re-checked
3814+
* here. No-op when backgrounding is disabled or no requesting workspace is set.
3815+
*/
3816+
private backgroundForegroundWaitIfQueued(
3817+
shouldBackgroundOnQueuedMessage: boolean,
3818+
requestingWorkspaceId: string | undefined
3819+
): void {
3820+
if (
3821+
shouldBackgroundOnQueuedMessage &&
3822+
requestingWorkspaceId &&
3823+
this.workspaceService.hasQueuedMessages(requestingWorkspaceId, "tool-end")
3824+
) {
3825+
this.backgroundForegroundWaitsForWorkspace(requestingWorkspaceId);
3826+
}
3827+
}
3828+
38093829
private buildWorkspaceTurnWaitResult(
38103830
record: WorkspaceTurnTaskHandleRecord
38113831
): WorkspaceTurnWaitResult {
@@ -4030,12 +4050,10 @@ export class TaskService {
40304050
timeoutMs
40314051
);
40324052

4033-
if (
4034-
shouldBackgroundOnQueuedMessage &&
4035-
this.workspaceService.hasQueuedMessages(options.requestingWorkspaceId, "tool-end")
4036-
) {
4037-
this.backgroundForegroundWaitsForWorkspace(options.requestingWorkspaceId);
4038-
}
4053+
this.backgroundForegroundWaitIfQueued(
4054+
shouldBackgroundOnQueuedMessage,
4055+
options.requestingWorkspaceId
4056+
);
40394057

40404058
void (async () => {
40414059
const record = await this.taskHandleStore.getWorkspaceTurn(
@@ -4383,13 +4401,10 @@ export class TaskService {
43834401
options.abortSignal.addEventListener("abort", abortListener, { once: true });
43844402
}
43854403

4386-
if (
4387-
shouldBackgroundOnQueuedMessage &&
4388-
requestingWorkspaceId &&
4389-
this.workspaceService.hasQueuedMessages(requestingWorkspaceId, "tool-end")
4390-
) {
4391-
this.backgroundForegroundWaitsForWorkspace(requestingWorkspaceId);
4392-
}
4404+
this.backgroundForegroundWaitIfQueued(
4405+
shouldBackgroundOnQueuedMessage,
4406+
requestingWorkspaceId
4407+
);
43934408
})().catch((error: unknown) => {
43944409
reject(error instanceof Error ? error : new Error(String(error)));
43954410
});

0 commit comments

Comments
 (0)