Skip to content

Fix same-day notice expiry - #1027

Merged
neuromechanist merged 1 commit into
devfrom
fix/issue-1024-notice-expiry
Jul 25, 2026
Merged

neuromechanist merged 1 commit into
devfrom
fix/issue-1024-notice-expiry

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Closes #1024.

A notice with an expires_at later today kept being served as active until the next UTC day.

Root cause

getActiveNotices filters with expires_at > datetime('now'). Both sides are TEXT, so SQLite compares them byte-wise. datetime('now') emits YYYY-MM-DD HH:MM:SS (space separator); expires_at was stored as RFC3339 with a T, because the create route validates with z.string().datetime({ offset: true }) and bound the value through unchanged. T is 0x54, space is 0x20, so whenever the date halves matched, the ISO value always compared greater:

sqlite> SELECT '2026-07-25T00:00:00.000Z' > '2026-07-25 09:00:00';   -- 1

Expiries on an earlier date compared correctly, which is why this survived — it looks right most of the time and fails on exactly the same-day window ("maintenance ends at 14:00 today") that same-day expiries exist for.

Fix

Normalize on write rather than patching the comparison:

  • datetime(?) on insert — converts any offset (or a Z) to the storage format, and is idempotent.
  • Migration 0064 — backfills existing rows.
  • The predicate is now a like-for-like comparison, and idx_notices_expires is finally sorted in the format the query actually compares against, which the string-vs-string version never was.

The backfill is guarded with AND datetime(expires_at) IS NOT NULL. datetime() returns NULL for an unparseable string and NULL in this column means never expires — without the guard a malformed timestamp would be silently promoted from "expired" to "permanent", the worst possible direction for a banner. Such a row is left as-is for a human to notice.

Wire format change (worth a look)

Both timestamps are now projected as explicit-UTC RFC3339 (strftime('%Y-%m-%dT%H:%M:%SZ', …)).

Storage has to be space-separated for the comparison to work, but that format is a trap on the wire: Date.parse("2026-07-25 14:30:00") is not ISO-8601, and V8 and JavaScriptCore both read it as local time, silently shifting the instant by each viewer's own UTC offset.

I applied it to created_at as well as expires_at, which is slightly beyond the letter of #1024 — flagging it rather than burying it. The reasoning: they arrive in the same object, and returning one unambiguous field beside one that parses as local time is precisely how a timezone bug gets introduced later. created_at was already being misparsed this way by both the website and the CLI; this fixes that too.

Testing

backend/test/notice-expiry.test.ts — 15 tests against real in-memory SQLite, no mocks:

  • the bug itself, pinned: a raw ISO value still compares greater than datetime('now') on the same day, and the same instant through datetime() does not. If someone reverts the insert to binding the raw string, this fails.
  • insert normalization: Z, a +02:00 offset converted to UTC (14:30+02:00 → 12:30), null preserved, idempotent on already-normalized input
  • the active filter: expired-earlier-today dropped, expiring-later-today kept, previous-day dropped, far-future and null kept
  • wire format: both fields come back …Z, null stays null, and the parsed instant is timezone-independent
  • migration backfill: normalizes ISO rows, leaves nulls, leaves an unparseable value untouched instead of nulling it, idempotent on re-run

The test imports NOTICE_ACTIVE_FILTER and NOTICE_COLUMNS from the service rather than copying the SQL, so it can't drift from what the service actually runs. Those two fragments are newly exported for exactly that reason.

bun test backend/test/ — 796 passing. tsc --noEmit and biome check src/ clean.

Note for the website

nemarOrg/website computes expiry client-side as a workaround for this bug (isNoticeExpired in src/lib/notices-api.ts). That code stays correct and becomes a harmless second opinion once this deploys — no coordinated change needed.

@neuromechanist
neuromechanist merged commit a93d330 into dev Jul 25, 2026
10 checks passed
@neuromechanist
neuromechanist deleted the fix/issue-1024-notice-expiry branch July 25, 2026 17:32

This branch was previously deployed

1 inactive deployment
npm — 257addfd Deployed Jul 25, 2026 by neuromechanist via publish #1872
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant