Returns true if this message should be processed; false to drop as a duplicate.
(message: { id?: string | null; seq?: number | null })
| 119 | |
| 120 | /** Returns true if this message should be processed; false to drop as a duplicate. */ |
| 121 | accept(message: { id?: string | null; seq?: number | null }): boolean { |
| 122 | const id = typeof message.id === 'string' && message.id.length > 0 ? message.id : null |
| 123 | if (id && this.seenIds.has(id)) { |
| 124 | // Refresh recency: the hub re-emits the same id every 5 s until the |
| 125 | // CLI acks (releaseMatureScheduledMessages contract). Without a |
| 126 | // delete+re-add the entry stays at its first-insert position and can |
| 127 | // be evicted by a burst of unrelated ids before the ack lands — |
| 128 | // the next re-emit would then be treated as new and double-deliver. |
| 129 | this.seenIds.delete(id) |
| 130 | this.seenIds.add(id) |
| 131 | return false |
| 132 | } |
| 133 | |
| 134 | const seq = typeof message.seq === 'number' ? message.seq : null |
| 135 | if (!id && seq !== null && this.lastSeenSeq !== null && seq <= this.lastSeenSeq) { |
| 136 | return false |
| 137 | } |
| 138 | |
| 139 | if (id) { |
| 140 | this.seenIds.add(id) |
| 141 | if (this.seenIds.size > this.capacity) { |
| 142 | // Set iteration is insertion-ordered; with delete+re-add on dedup hit |
| 143 | // (above) this becomes a true LRU eviction. |
| 144 | const oldest = this.seenIds.values().next().value |
| 145 | if (oldest !== undefined) this.seenIds.delete(oldest) |
| 146 | } |
| 147 | } |
| 148 | if (seq !== null) { |
| 149 | this.lastSeenSeq = Math.max(this.lastSeenSeq ?? 0, seq) |
| 150 | } |
| 151 | return true |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | export class ApiSessionClient extends EventEmitter { |
no test coverage detected