fix(background-agent): defer parent wakes during active turns
Record fresh parent session message activity before parent-wake flushing so stale idle status cannot dispatch a background completion into a live reasoning turn. Add regression coverage for the Discord 4.2.3/OpenCode 1.15.5 duplicate-branch repro shape where a parent reasoning delta arrives before the background all-complete wake. Refs #4212 Refs #4019 Refs #3774 Plan: plans/background-notification-active-turn-queue.md
This commit is contained in:
@@ -67,6 +67,7 @@ type ParentWakeNotifierOptions = {
|
||||
* inside OpenCode's `@parcel/watcher` TSFN callback path. See issue #4120.
|
||||
*/
|
||||
userMessageInProgressWindowMs: number
|
||||
parentSessionActivityInProgressWindowMs?: number
|
||||
}
|
||||
|
||||
type ToolWaitDeferralDecision = {
|
||||
@@ -94,6 +95,7 @@ export class ParentWakeNotifier {
|
||||
private pendingParentWakeTimers: Map<string, ReturnType<typeof setTimeout>> = new Map()
|
||||
private dispatchedParentWakes: Map<string, PendingParentWake> = new Map()
|
||||
private dispatchedParentWakeTimers: Map<string, ReturnType<typeof setTimeout>> = new Map()
|
||||
private recentParentSessionActivity: Map<string, number> = new Map()
|
||||
|
||||
constructor(
|
||||
private readonly deps: ParentWakeNotifierDeps,
|
||||
@@ -116,6 +118,14 @@ export class ParentWakeNotifier {
|
||||
return this.dispatchedParentWakeTimers
|
||||
}
|
||||
|
||||
recordParentSessionActivity(sessionID: string): void {
|
||||
this.recentParentSessionActivity.set(sessionID, Date.now())
|
||||
}
|
||||
|
||||
clearParentSessionActivity(sessionID: string): void {
|
||||
this.recentParentSessionActivity.delete(sessionID)
|
||||
}
|
||||
|
||||
queuePendingParentWake(
|
||||
sessionID: string,
|
||||
notification: string,
|
||||
@@ -163,6 +173,14 @@ export class ParentWakeNotifier {
|
||||
return
|
||||
}
|
||||
|
||||
if (this.hasRecentParentSessionActivity(sessionID)) {
|
||||
this.schedulePendingParentWakeFlush(sessionID)
|
||||
log("[background-agent] Deferred parent wake because parent session activity is still fresh:", {
|
||||
sessionID,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const toolWaitDecision = await this.shouldDeferParentWakeForSessionHistory(sessionID, latestWake)
|
||||
if (toolWaitDecision.defer) {
|
||||
this.schedulePendingParentWakeFlush(sessionID)
|
||||
@@ -324,12 +342,29 @@ export class ParentWakeNotifier {
|
||||
this.dispatchedParentWakeTimers.clear()
|
||||
this.pendingParentWakes.clear()
|
||||
this.dispatchedParentWakes.clear()
|
||||
this.recentParentSessionActivity.clear()
|
||||
}
|
||||
|
||||
private async isSessionActive(sessionID: string): Promise<boolean> {
|
||||
return isOpenCodeSessionActive(this.deps.client, sessionID)
|
||||
}
|
||||
|
||||
private hasRecentParentSessionActivity(sessionID: string): boolean {
|
||||
const windowMs = this.options.parentSessionActivityInProgressWindowMs ?? 0
|
||||
if (windowMs <= 0) {
|
||||
return false
|
||||
}
|
||||
const lastActivityAt = this.recentParentSessionActivity.get(sessionID)
|
||||
if (lastActivityAt === undefined) {
|
||||
return false
|
||||
}
|
||||
if (Date.now() - lastActivityAt <= windowMs) {
|
||||
return true
|
||||
}
|
||||
this.recentParentSessionActivity.delete(sessionID)
|
||||
return false
|
||||
}
|
||||
|
||||
private resolveParentWakePromptContext(promptContext: ParentWakePromptContext): ParentWakePromptContext {
|
||||
const resolvedAgent = resolveRegisteredAgentName(promptContext.agent)
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user