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:
parent
5d99e9ab64
commit
4d966ec99b
@ -433,15 +433,7 @@ export class BackgroundManager {
|
|||||||
task.concurrencyKey = undefined // Prevent double-release
|
task.concurrencyKey = undefined // Prevent double-release
|
||||||
}
|
}
|
||||||
// Clean up pendingByParent to prevent stale entries
|
// Clean up pendingByParent to prevent stale entries
|
||||||
if (task.parentSessionID) {
|
this.cleanupPendingByParent(task)
|
||||||
const pending = this.pendingByParent.get(task.parentSessionID)
|
|
||||||
if (pending) {
|
|
||||||
pending.delete(task.id)
|
|
||||||
if (pending.size === 0) {
|
|
||||||
this.pendingByParent.delete(task.parentSessionID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.markForNotification(task)
|
this.markForNotification(task)
|
||||||
await this.notifyParentSession(task)
|
await this.notifyParentSession(task)
|
||||||
log("[background-agent] Task completed via session.idle event:", task.id)
|
log("[background-agent] Task completed via session.idle event:", task.id)
|
||||||
@ -469,15 +461,7 @@ export class BackgroundManager {
|
|||||||
task.concurrencyKey = undefined // Prevent double-release
|
task.concurrencyKey = undefined // Prevent double-release
|
||||||
}
|
}
|
||||||
// Clean up pendingByParent to prevent stale entries
|
// Clean up pendingByParent to prevent stale entries
|
||||||
if (task.parentSessionID) {
|
this.cleanupPendingByParent(task)
|
||||||
const pending = this.pendingByParent.get(task.parentSessionID)
|
|
||||||
if (pending) {
|
|
||||||
pending.delete(task.id)
|
|
||||||
if (pending.size === 0) {
|
|
||||||
this.pendingByParent.delete(task.parentSessionID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.tasks.delete(task.id)
|
this.tasks.delete(task.id)
|
||||||
this.clearNotificationsForTask(task.id)
|
this.clearNotificationsForTask(task.id)
|
||||||
subagentSessions.delete(sessionID)
|
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 {
|
private startPolling(): void {
|
||||||
if (this.pollingInterval) return
|
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
|
task.concurrencyKey = undefined // Prevent double-release
|
||||||
}
|
}
|
||||||
// Clean up pendingByParent to prevent stale entries
|
// Clean up pendingByParent to prevent stale entries
|
||||||
if (task.parentSessionID) {
|
this.cleanupPendingByParent(task)
|
||||||
const pending = this.pendingByParent.get(task.parentSessionID)
|
|
||||||
if (pending) {
|
|
||||||
pending.delete(task.id)
|
|
||||||
if (pending.size === 0) {
|
|
||||||
this.pendingByParent.delete(task.parentSessionID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.clearNotificationsForTask(taskId)
|
this.clearNotificationsForTask(taskId)
|
||||||
this.tasks.delete(taskId)
|
this.tasks.delete(taskId)
|
||||||
subagentSessions.delete(task.sessionID)
|
subagentSessions.delete(task.sessionID)
|
||||||
@ -806,15 +797,7 @@ try {
|
|||||||
task.concurrencyKey = undefined // Prevent double-release
|
task.concurrencyKey = undefined // Prevent double-release
|
||||||
}
|
}
|
||||||
// Clean up pendingByParent to prevent stale entries
|
// Clean up pendingByParent to prevent stale entries
|
||||||
if (task.parentSessionID) {
|
this.cleanupPendingByParent(task)
|
||||||
const pending = this.pendingByParent.get(task.parentSessionID)
|
|
||||||
if (pending) {
|
|
||||||
pending.delete(task.id)
|
|
||||||
if (pending.size === 0) {
|
|
||||||
this.pendingByParent.delete(task.parentSessionID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.markForNotification(task)
|
this.markForNotification(task)
|
||||||
await this.notifyParentSession(task)
|
await this.notifyParentSession(task)
|
||||||
log("[background-agent] Task completed via polling:", task.id)
|
log("[background-agent] Task completed via polling:", task.id)
|
||||||
@ -887,15 +870,7 @@ if (lastMessage) {
|
|||||||
task.concurrencyKey = undefined // Prevent double-release
|
task.concurrencyKey = undefined // Prevent double-release
|
||||||
}
|
}
|
||||||
// Clean up pendingByParent to prevent stale entries
|
// Clean up pendingByParent to prevent stale entries
|
||||||
if (task.parentSessionID) {
|
this.cleanupPendingByParent(task)
|
||||||
const pending = this.pendingByParent.get(task.parentSessionID)
|
|
||||||
if (pending) {
|
|
||||||
pending.delete(task.id)
|
|
||||||
if (pending.size === 0) {
|
|
||||||
this.pendingByParent.delete(task.parentSessionID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.markForNotification(task)
|
this.markForNotification(task)
|
||||||
await this.notifyParentSession(task)
|
await this.notifyParentSession(task)
|
||||||
log("[background-agent] Task completed via stability detection:", task.id)
|
log("[background-agent] Task completed via stability detection:", task.id)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user