diff --git a/src/main/agent-awake-service-platform-assertions.test.ts b/src/main/agent-awake-service-platform-assertions.test.ts index cd1d7fb1adc..7b3566b322f 100644 --- a/src/main/agent-awake-service-platform-assertions.test.ts +++ b/src/main/agent-awake-service-platform-assertions.test.ts @@ -22,6 +22,20 @@ function workingStatus(): AgentAwakeStatus { } } +describe('AgentAwakeService status array ownership', () => { + it('does not observe rows appended to the caller array after setStatuses', () => { + const service = new AgentAwakeService() + service.setMode('auto') + const statuses: AgentAwakeStatus[] = [workingStatus()] + + service.setStatuses(statuses) + const before = service.getWorkingAgentCount() + statuses.push(workingStatus(), workingStatus()) + + expect(service.getWorkingAgentCount()).toBe(before) + }) +}) + function createBlocker() { const startedIds = new Set() let nextId = 1 diff --git a/src/main/agent-awake-service.ts b/src/main/agent-awake-service.ts index b79612e2b9c..6be27e9d0e6 100644 --- a/src/main/agent-awake-service.ts +++ b/src/main/agent-awake-service.ts @@ -105,7 +105,8 @@ export class AgentAwakeService { } setStatuses(statuses: AgentAwakeStatus[]): void { - this.statuses = statuses.map((status) => ({ ...status })) + // Copy the array, not every row: the hook server allocates each row fresh per event. + this.statuses = [...statuses] this.refresh('status-change') } @@ -171,7 +172,8 @@ export class AgentAwakeService { private getEligibleRunningStatusCount(): number { const now = this.now() - return this.statuses.filter((status) => this.isWakeEligible(status, now)).length + // Counted in place: the filtered array was only ever measured, and this runs per hook event. + return this.statuses.reduce((count, s) => count + (this.isWakeEligible(s, now) ? 1 : 0), 0) } private isWakeEligible(status: AgentAwakeStatus, now: number): boolean {