Skip to content

fix(cron): warn owners before stale-nm deletion, never auto-delete (#662) - #663

Merged
neuromechanist merged 2 commits into
devfrom
feature/issue-662-staleness-warnings
May 30, 2026
Merged

neuromechanist merged 2 commits into
devfrom
feature/issue-662-staleness-warnings

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Closes #662.

Problem

The daily Cloudflare Workers cron (scheduledCleanup, crons = ["0 3 * * *"]) silently hard-deleted private, no-DOI nm datasets once inactive 90 days, using the same total cascade as a manual admin delete (GitHub repo + S3 + D1). On 2026-05-27 it destroyed nm000111 (ISRUC-Sleep), nm000116 (SEED-V), nm000117 (SEED-VIG) with no owner warning and no human in the loop. Recoverable only because S3 versioning was on. 103 more private/no-DOI nm datasets were exposed to the same fate.

Fix

The cron never auto-deletes nm datasets anymore. Instead:

  • Emails the owner an escalating warning runway at 30 / 14 / 7 / 2 / 1 days before the 90-day mark, tracked in D1 (staleness_warn_stage) so the daily run does not re-spam; tracking resets when the dataset gets fresh activity, a DOI, a pending pub request, or is made public.
  • At day 0 it emails admins a 'ready to delete, confirm manually' notice. Deletion of real archive data is now only ever a deliberate nemar admin delete-dataset action.
  • Sandbox xx 14-day auto-deletion is unchanged (disposable).

Changes

  • backend/src/services/staleness.ts — pure threshold math (unit-tested).
  • backend/src/db/migrations/0027_dataset_staleness_tracking.sql — staleness_warn_stage, staleness_admin_notified_at.
  • backend/src/services/email.ts — owner warning + admin review templates (Resend).
  • backend/src/index.ts — rewrites the nm branch of scheduledCleanup.

Tests

  • test/staleness.unit.test.ts — 12 pure-function tests (no I/O, no sends): age/days-left maths, the 30/14/7/2/1 escalation emits each threshold exactly once, never emits at/after the deadline.
  • test/email-staleness.test.ts — captures the outbound Resend request (override globalThis.fetch, no real email) to assert recipient/subject/body; plus an opt-in GET /domains connectivity check gated behind RESEND_API_KEY (no send, no cost).

Deploy note

Migration 0027 must be applied to dev + prod D1 with the deploy (the cron queries the new columns). No at-risk datasets currently, so the old cron deletes nothing before this ships.

Recovery (separate, already done)

nm000111/116/117 fully recovered: S3 delete markers removed (versions restored), GitHub repos restored via org UI within the 90-day window, D1 rows + owner S3 grants re-registered, recovery logged to audit_log.

The daily Cloudflare cron silently hard-deleted private, no-DOI nm
datasets at 90 days inactive (it wiped nm000111/116/117 across GitHub,
S3, and D1). Replace that with a human-gated runway:

- Email the owner escalating warnings at 30/14/7/2/1 days before the
  deadline, tracked in D1 (staleness_warn_stage) so the daily cron does
  not re-spam; reset when the dataset gets fresh activity.
- At the deadline the cron does NOT delete; it emails admins a
  ready-to-delete notice. nm deletion is now only a manual admin action.
- Sandbox xx 14-day auto-deletion is unchanged (disposable).

Adds services/staleness.ts (pure threshold math), migration 0027, and
owner/admin email templates. Tested: staleness unit tests + email
fetch-capture tests (no real Resend sends).
@github-actions

github-actions Bot commented May 30, 2026 •

Copy link
Copy Markdown

📦 Package Preview

Your package is ready to test!

bun install -g nemar-cli@PR663

Version: 0.8.38-PR663.6f490cd
Commit: 6f490cd


🤖 Updated on each push.

Address pr-review-toolkit findings on the cron warning runway:

- Only advance staleness_warn_stage / staleness_admin_notified_at when
  the email was actually delivered (or there is genuinely no recipient).
  Previously a swallowed Resend failure or a missing RESEND_API_KEY left
  the column stamped "done", permanently suppressing the warning/admin
  notice - the same silent-failure class that lost nm000111/116/117.
- sendStalenessAdminReviewEmail now returns the delivered count; the cron
  advances the flag only when delivered > 0.
- Fetch admin emails in their own try/catch so a D1 hiccup can't abandon
  every owner warning; wrap each row in a per-dataset boundary.
- Exclude already-notified past-deadline rows from candidates so a backlog
  can't starve newly-past-deadline datasets out of the LIMIT.
- Use the publication_request email category, not announcements (a
  data-loss alert must not ride an opt-out newsletter preference).
- Log when RESEND_API_KEY is absent instead of skipping silently.
- Accurate email copy: owner notice shows real inactive-days; admin notice
  states the actual warning stage reached instead of over-claiming.

Tests: add cron SQL-structural guards (reset/candidate clauses, starvation
exclusion, handled-gate), an ISO-timestamp parse case, an admin-subject
assertion, and a zero-delivered admin-email case.
@nemarOwner

Copy link
Copy Markdown

/review-pr summary (4 agents, sonnet)

Ran code-reviewer, silent-failure-hunter, pr-test-analyzer, comment-analyzer in parallel. Findings and resolution (all addressed in 6f490cd):

Critical

  • DB tracking advanced even when the email send failed or RESEND_API_KEY was absent → state stamped 'done', permanently suppressing the warning/admin notice (the same silent-failure class that lost the datasets). Now state advances only when the email is actually delivered (sendStalenessAdminReviewEmail returns a delivered count; owner send wrapped in try/catch); otherwise it retries next run.

Important

  • getAdminEmailsForCategory throw aborted the whole loop → moved into its own try/catch.
  • No per-row error boundary → each dataset wrapped so one failure can't abandon the rest.
  • LIMIT starvation: already-notified past-deadline rows could crowd out newly-past-deadline ones → excluded via NOT (past-deadline AND admin_notified_at IS NOT NULL).
  • Wrong email category (announcements, an opt-out newsletter) → publication_request.
  • Silent skip when RESEND_API_KEY missing → now logged.

Comment/copy accuracy

  • Admin email over-claimed 'owner was warned at 30/14/7/2/1 and took no action' → now states the actual stage reached.
  • Owner email 'nearly 90 days' was wrong at the 30-day notice → now dynamic (STALENESS_LIMIT_DAYS - daysLeft).
  • isFinal docstring read like a parameter → reworded.

Tests added

  • Cron SQL-structural guards (reset + candidate clauses, starvation exclusion, handled-gate), ISO-timestamp parse case, admin-subject assertion, zero-delivered admin-email case.

Reviewed, kept by design

  • code-reviewer flagged test/email-staleness.test.ts globalThis.fetch capture as a no-mock-rule violation; pr-test-analyzer judged it appropriate (captures real request construction, not a mock library). Kept intentionally — it's the requested 'writes to Resend' check without spending quota; the opt-in GET /domains test covers real connectivity.

22 tests pass, typecheck + Biome clean.

This branch was previously deployed

1 inactive deployment
npm — 6f490cd0 Deployed May 30, 2026 by neuromechanist via publish #1213
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.

2 participants