Skip to content

Delete usage metrics and feedback after five years - #726

Merged
neuromechanist merged 4 commits into
developfrom
feature/issue-725-retention-five-years
Nov 11, 2026
Merged

neuromechanist merged 4 commits into
developfrom
feature/issue-725-retention-five-years

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

The maintainer decided on 2026-11-11 that usage metrics and feedback are kept for five years. This PR makes the server delete them after that, and makes PRIVACY.md say so in place of "no automatic deletion".

  • src/metrics/db.py:
    • METRICS_RETENTION_YEARS = 5.
    • retention_cutoff(now): five calendar years before now, in UTC. On 29 February it falls back to 28 February.
    • purge_expired_metrics(db_path, now): deletes the request_log rows and feedback_log entries older than the cutoff, in one commit, and returns PurgeCounts. Every writer stores datetime.now(UTC).isoformat(), so rows are compared as strings, and a test covers the mixed forms with and without microseconds.
  • src/api/main.py: startup runs the purge after init_metrics_db. The scheduler only starts with sync_enabled, so without this a deployment with sync off would never purge. A failure is logged with its traceback and startup goes on.
  • src/api/scheduler.py: the scheduler runs it daily at 03:15 (metrics_retention). A SQLite or OS error is logged and the scheduler keeps running.
  • PRIVACY.md: "kept for five years, then deleted automatically", for both usage metrics and feedback. Server logs are not covered by this decision and the text about them is unchanged.
  • CHANGELOG entry.

Nothing is older than five years today; the first deletions come in 2030 at the earliest.

The docs.osc.earth mirror (OpenScience-Collective/documentation#46) and NEMAR's policy (nemarOrg/docs#116) get the same sentence in their open PRs.

Closes #725

Tests

  • tests/test_metrics/test_retention.py (new), on real SQLite files with the real writers:
    • the cutoff, including 29 February and a time given in another zone;
    • both tables are purged, and a row exactly at the cutoff is kept;
    • mixed timestamp forms;
    • an empty database;
    • startup through the real lifespan with sync off;
    • the daily job;
    • a corrupt database file, which is logged and does not raise.
  • pytest tests/test_metrics tests/test_api tests/test_frontend -m 'not network and not llm' (Python 3.12): 2267 passed
  • ruff check and ruff format on the changed files: clean

…nd daily

purge_expired_metrics deletes request_log and feedback_log rows older than
METRICS_RETENTION_YEARS (5) calendar years. Startup runs it (the scheduler only starts
with sync enabled); the scheduler repeats it daily at 03:15. Failures are logged.

Tested: tests/test_metrics (incl. new test_retention.py), tests/test_api, tests/test_frontend: 2267 passed.
…deleted; changelog

Tested: tests/test_frontend (privacy page) passes.
neuromechanist added a commit to OpenScience-Collective/documentation that referenced this pull request Nov 11, 2026
…s, then deleted

Mirrors OSA's PRIVACY.md after OpenScience-Collective/osa#726. mkdocs build: the page builds;
the only warning is OSCAR's examples link (submodule, same pin as main).
…calate repeated job failures, test the 03:15 registration, fixed-offset zone test, skip the startup purge when init failed

Tested: tests/test_metrics, tests/test_api, tests/test_frontend: 2270 passed. The erase test
fails with secure_delete and the checkpoint removed (negative control run by hand).
@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 1 (Sonnet code reviewer): outcomes

No critical findings, 1 important, 4 suggestions; all fixed in the round 1 commit.

# Severity Finding Outcome
1 Important DELETE only unlinks rows; freed pages and the write-ahead log keep the text, and feedback comments can hold personal data Fixed: PRAGMA secure_delete=ON on the purge connection, and wal_checkpoint(TRUNCATE) after a purge that deleted rows (a checkpoint blocked by a reader is logged as a warning). New test: a marker comment is in the files before the purge and in neither metrics.db nor metrics.db-wal after it. Negative control: with both removed, the test fails.
2 Suggestion The daily job did not escalate repeated failures like the budget and mirror jobs Fixed: _metrics_retention_failures counter, CRITICAL at MAX_CONSECUTIVE_FAILURES, reset on success; tested
3 Suggestion Nothing tested that the scheduler registers the job Fixed: registration moved into _add_metrics_retention_job(scheduler), which start_scheduler calls; a test uses a real, unstarted BackgroundScheduler and checks the job, its function, and hour 3, minute 15
4 Suggestion The zone test used the host's zone, which proves nothing on a UTC machine Fixed: fixed UTC-8 offset, asserting that the wall clock differs and the cutoff is the same
5 Suggestion The startup purge ran after a failed init_metrics_db Fixed: skipped when initialization failed

…g names the erasure and the escalation, accurate blocked-checkpoint wording

Tested: tests/test_metrics and test_bedrock_deployments (331 passed).
@neuromechanist

Copy link
Copy Markdown
Member Author

Review round 2 (Sonnet code reviewer): outcomes

All 5 round 1 findings are confirmed resolved. The reviewer also ran the case where a reader blocks the checkpoint: the purge returned, logged the warning and raised nothing. 3 new suggestions, all fixed in the round 2 commit:

# Severity Finding Outcome
1 Suggestion No test of the startup path with a broken database (the metrics_ready guard) Fixed: a lifespan test with a non-SQLite metrics.db checks that startup completes, the initialization error is logged, and no purge error is
2 Suggestion CHANGELOG did not mention the erasure or the escalation Fixed: one sentence on each, and the test list is updated
3 Suggestion The docstring said a blocked checkpoint is "left to SQLite's next one", but automatic checkpoints are passive and do not truncate Fixed: the docstring and the warning now say the older copies stay until SQLite reuses those frames, with no time bound

This was the second and last round.

@neuromechanist
neuromechanist merged commit 959dc12 into develop Nov 11, 2026
9 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-725-retention-five-years branch November 11, 2026 20:03
neuromechanist added a commit to OpenScience-Collective/documentation that referenced this pull request Nov 11, 2026
…erence (#46)

* OSA privacy page and widget reference: the Privacy choices dialog and Share usage data

* docs(osa): privacy, usage metrics and feedback are kept for five years, then deleted

Mirrors OSA's PRIVACY.md after OpenScience-Collective/osa#726. mkdocs build: the page builds;
the only warning is OSCAR's examples link (submodule, same pin as main).

* docs(osa): privacy, [email protected] for privacy questions and requests about your data

Mirrors OpenScience-Collective/osa#727.

* docs(osa): privacy, contact wording matches OpenScience-Collective/osa#727 after review
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