Skip to content

Commit efcd8c4

Browse files
committed
fix(gotchas-memory): apply errorWindowMs window and fix critical sort order
Two bugs in GotchasMemory: 1. trackError() never applied the errorWindowMs rolling window (fixes #475). Errors accumulated indefinitely — the 24-hour window config was dead code. Fix: reset the count when the elapsed time since lastSeen exceeds the window. 2. listGotchas() used `|| 2` fallback on severityOrder, causing `critical` (= 0) to be treated as falsy and sorted last instead of first (fixes #476). Fix: use `?? 2` (nullish coalescing) to preserve the zero value. 60 unit tests added covering all public methods, error window regression, sort correctness, persistence, and event emission.
1 parent fcfb757 commit efcd8c4

2 files changed

Lines changed: 599 additions & 2 deletions

File tree

.aiox-core/core/memory/gotchas-memory.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,12 @@ class GotchasMemory extends EventEmitter {
257257
errorPattern: errorData.message,
258258
category: this._detectCategory(errorData.message + ' ' + (errorData.stack || '')),
259259
};
260+
} else if (now - tracking.lastSeen >= this.options.errorWindowMs) {
261+
// Window expired — reset count so only errors within the rolling window
262+
// contribute toward the auto-capture threshold (fixes #475)
263+
tracking.count = 0;
264+
tracking.firstSeen = now;
265+
tracking.samples = [];
260266
}
261267

262268
// Update tracking
@@ -311,10 +317,13 @@ class GotchasMemory extends EventEmitter {
311317
gotchas = gotchas.filter((g) => !g.resolved);
312318
}
313319

314-
// Sort by severity (critical first), then by last occurrence
320+
// Sort by severity (critical first), then by last occurrence.
321+
// Use ?? not || so that 0 (critical) is not treated as falsy (fixes #476)
315322
const severityOrder = { critical: 0, warning: 1, info: 2 };
316323
gotchas.sort((a, b) => {
317-
const severityDiff = (severityOrder[a.severity] || 2) - (severityOrder[b.severity] || 2);
324+
const aSev = severityOrder[a.severity] ?? 2;
325+
const bSev = severityOrder[b.severity] ?? 2;
326+
const severityDiff = aSev - bSev;
318327
if (severityDiff !== 0) return severityDiff;
319328
return new Date(b.source.lastSeen) - new Date(a.source.lastSeen);
320329
});

0 commit comments

Comments
 (0)