refactor(background-agent): extract cleanupPendingByParent helper

Extract duplicated 8-line pendingByParent cleanup pattern into a
reusable helper method. Reduces code duplication across 5 call sites.

Addresses cubic-dev-ai feedback on PR #736.
This commit is contained in:
Gladdonilli
2026-01-14 14:08:53 +08:00
parent 5d99e9ab64
commit 4d966ec99b

View File

@@ -433,15 +433,7 @@ export class BackgroundManager {
task.concurrencyKey = undefined // Prevent double-release
}
// Clean up pendingByParent to prevent stale entries
if (task.parentSessionID) {
const pending = this.pendingByParent.get(task.parentSessionID)
if (pending) {
pending.delete(task.id)
if (pending.size === 0) {
this.pendingByParent.delete(task.parentSessionID)
}
}
}
this.cleanupPendingByParent(task)
this.markForNotification(task)
await this.notifyParentSession(task)
log("[background-agent] Task completed via session.idle event:", task.id)
@@ -469,15 +461,7 @@ export class BackgroundManager {
task.concurrencyKey = undefined // Prevent double-release
}
// Clean up pendingByParent to prevent stale entries
if (task.parentSessionID) {
const pending = this.pendingByParent.get(task.parentSessionID)
if (pending) {
pending.delete(task.id)
if (pending.size === 0) {
this.pendingByParent.delete(task.parentSessionID)
}
}
}
this.cleanupPendingByParent(task)
this.tasks.delete(task.id)
this.clearNotificationsForTask(task.id)
subagentSessions.delete(sessionID)
@@ -569,6 +553,21 @@ export class BackgroundManager {
}
}
/**
* Remove task from pending tracking for its parent session.
* Cleans up the parent entry if no pending tasks remain.
*/
private cleanupPendingByParent(task: BackgroundTask): void {
if (!task.parentSessionID) return
const pending = this.pendingByParent.get(task.parentSessionID)
if (pending) {
pending.delete(task.id)
if (pending.size === 0) {
this.pendingByParent.delete(task.parentSessionID)
}
}
}
private startPolling(): void {
if (this.pollingInterval) return
@@ -739,15 +738,7 @@ Use \`background_output(task_id="${task.id}")\` to retrieve this result when rea
task.concurrencyKey = undefined // Prevent double-release
}
// Clean up pendingByParent to prevent stale entries
if (task.parentSessionID) {
const pending = this.pendingByParent.get(task.parentSessionID)
if (pending) {
pending.delete(task.id)
if (pending.size === 0) {
this.pendingByParent.delete(task.parentSessionID)
}
}
}
this.cleanupPendingByParent(task)
this.clearNotificationsForTask(taskId)
this.tasks.delete(taskId)
subagentSessions.delete(task.sessionID)
@@ -806,15 +797,7 @@ try {
task.concurrencyKey = undefined // Prevent double-release
}
// Clean up pendingByParent to prevent stale entries
if (task.parentSessionID) {
const pending = this.pendingByParent.get(task.parentSessionID)
if (pending) {
pending.delete(task.id)
if (pending.size === 0) {
this.pendingByParent.delete(task.parentSessionID)
}
}
}
this.cleanupPendingByParent(task)
this.markForNotification(task)
await this.notifyParentSession(task)
log("[background-agent] Task completed via polling:", task.id)
@@ -887,15 +870,7 @@ if (lastMessage) {
task.concurrencyKey = undefined // Prevent double-release
}
// Clean up pendingByParent to prevent stale entries
if (task.parentSessionID) {
const pending = this.pendingByParent.get(task.parentSessionID)
if (pending) {
pending.delete(task.id)
if (pending.size === 0) {
this.pendingByParent.delete(task.parentSessionID)
}
}
}
this.cleanupPendingByParent(task)
this.markForNotification(task)
await this.notifyParentSession(task)
log("[background-agent] Task completed via stability detection:", task.id)