Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: reject phantom day keys and non-array sources in the activeDays union (#4120)

[._.]/ Adam Eivy466551d5c69d
diff --git a/server/lib/activeDays.js b/server/lib/activeDays.js
index a941cd3853..79422261d7 100644
--- a/server/lib/activeDays.js
+++ b/server/lib/activeDays.js
@@ -53,8 +53,18 @@ const DAY_KEY_RE = /^\d{4}-\d{2}-\d{2}$/;
export function toUserDayKey(value, timezone) {
if (typeof value !== 'string' || !value) return null;
- // No time component → a day LABEL, not an instant. Take it as authored (see header).
- if (!value.includes('T')) return DAY_KEY_RE.test(value) ? value : null;
+ // No time component → a day LABEL, not an instant. Take it as authored (see header), but
+ // only once it is a real calendar day. Shape alone is not validity, and neither is parsing:
+ // `2026-13-45` matches the regex, and `2026-02-30` even parses — `Date` silently rolls it
+ // over to March 2 rather than rejecting it. Round-trip through `Date` so only a day that
+ // survives unchanged counts; anything else is a phantom that would inflate the tally by a
+ // day the user never had.
+ if (!value.includes('T')) {
+ if (!DAY_KEY_RE.test(value)) return null;
+ const day = new Date(value);
+ if (Number.isNaN(day.getTime())) return null;
+ return day.toISOString().slice(0, 10) === value ? value : null;
+ }
const at = new Date(value);
if (Number.isNaN(at.getTime())) return null;
@@ -64,14 +74,24 @@ export function toUserDayKey(value, timezone) {
/**
* Union several domains' date values into one sorted set of user-local day keys.
*
+ * A non-array source contributes nothing rather than throwing — this function unions what it
+ * is given and does not classify read failures. Deciding that a missing/unreadable domain means
+ * `unavailable` (never a confident 0) is the CALLER's job, and `characterMetrics.js`'s
+ * `daysActive` validates every source before calling in for exactly that reason.
+ *
* @param {Array<Array<unknown>|null|undefined>} sources - one array of date values per domain
* @param {string} timezone - IANA timezone (from `getUserTimezone()`)
* @returns {string[]} sorted, de-duplicated day keys. `.length` is the honest "days active".
*/
export function unionActiveDayKeys(sources, timezone) {
+ if (!Array.isArray(sources)) return [];
+
const days = new Set();
- for (const source of sources || []) {
- for (const value of source || []) {
+ for (const source of sources) {
+ // `Array.isArray`, not `source || []` — a truthy non-array source (a bare object, a number)
+ // is not iterable, and `for...of` over it would throw a TypeError.
+ if (!Array.isArray(source)) continue;
+ for (const value of source) {
const key = toUserDayKey(value, timezone);
if (key) days.add(key);
}
diff --git a/server/lib/activeDays.test.js b/server/lib/activeDays.test.js
index bbf02861a8..6034415449 100644
--- a/server/lib/activeDays.test.js
+++ b/server/lib/activeDays.test.js
@@ -40,6 +40,20 @@ describe('toUserDayKey', () => {
expect(toUserDayKey('not-a-date T nope', TZ)).toBeNull();
expect(toUserDayKey('2026-13-45T99:99:99Z', TZ)).toBeNull();
});
+
+ it('rejects a day-SHAPED value that is not a real calendar day', () => {
+ // The shape check alone is not validity, and neither is parsing: `2026-13-45` is rejected
+ // by Date, but `2026-02-30` PARSES — Date rolls it over to March 2 instead of failing. Both
+ // must be dropped, or a corrupt stored key inflates the tally by a day that never existed.
+ for (const phantom of ['2026-13-45', '2026-00-00', '2026-02-30', '2026-99-99', '2026-04-31']) {
+ expect(toUserDayKey(phantom, TZ)).toBeNull();
+ }
+ });
+
+ it('accepts a real leap day and rejects a fake one', () => {
+ expect(toUserDayKey('2024-02-29', TZ)).toBe('2024-02-29');
+ expect(toUserDayKey('2026-02-29', TZ)).toBeNull(); // 2026 is not a leap year
+ });
});
describe('unionActiveDayKeys', () => {
@@ -77,6 +91,16 @@ describe('unionActiveDayKeys', () => {
expect(unionActiveDayKeys(null, TZ)).toEqual([]);
});
+ it('skips a non-array source instead of throwing on it', () => {
+ // `source || []` would hand a truthy non-array straight to `for...of` and TypeError. This
+ // function unions what it is given; classifying an unreadable domain is the caller's job
+ // (characterMetrics.js validates each source before calling in).
+ expect(() => unionActiveDayKeys([42, true, { date: '2026-03-14' }, ['2026-03-14']], TZ)).not.toThrow();
+ expect(unionActiveDayKeys([42, true, { date: '2026-03-15' }, ['2026-03-14']], TZ))
+ .toEqual(['2026-03-14']);
+ expect(unionActiveDayKeys('2026-03-14', TZ)).toEqual([]);
+ });
+
it('returns the keys sorted ascending', () => {
expect(unionActiveDayKeys([['2026-03-15', '2026-01-02'], ['2026-02-09']], TZ))
.toEqual(['2026-01-02', '2026-02-09', '2026-03-15']);