Skip to content

fix(mentions): never serve a Perception snapshot older than its rollups - #1392

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/perception-snapshot-followups
Oct 23, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:fix/perception-snapshot-followups

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

The follow-ups from #1385's review. The Perception tab's default view never shows figures older than its rollups. The drain no longer recomputes views for a brand whose rebuild failed. The slow note waits its 4 s again.

  1. A snapshot is set aside as soon as its rollups change. Migration 00246 redefines refresh_mention_terms_daily (00231) with one addition at its end, in the same transaction:

    update public.mentions_perception_snapshots
    set day_to = date '1970-01-01', computed_at = clock_timestamp()
    where brand_id = p_brand_id;

    This is the update forget_mentions_perception_snapshots makes. Every rebuild of the Perception rollups goes through this function: a completed run, the nightly sweep, the deleted-prompt drain, or a backfill in SQL. So from the moment new rollups commit, the default view reads them live until the server refreshes the snapshots.

    • A failed refresh now leaves the tab on the live read, as the comments in mentions-rollups.js and insights-rollups.js already said.
    • The rest of the function is 00231's, unchanged.
    • The update runs last, so a snapshot refresh that started before it (and read the old rollups) has an earlier computed_at, and the guard keeps it from writing over the set-aside row.
    • What remains: a refresh that starts in the instant between the update and the commit could still write the old figures. The refresh after the rebuild replaces them. Closing that would need a shared advisory lock in 00241's refresh, which isn't worth it here.
    • The snapshots table's comment now says so too.
  2. The drain refreshes the snapshots only after the terms refresh has run. This is the shape suggested in the review. A brand whose Insights rebuild keeps failing no longer recomputes its three windows every 5 minutes.

  3. The slow note waits again. slow is cleared when its read's effect ends. Going back to filters whose earlier read was slow no longer shows the note before SLOW_MS.

Related issue

Follow-up to #1385 (review notes 1–3). No GitHub issue.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Apply 00246, then pick a brand with snapshots and run select refresh_mention_terms_daily('<brand>', current_date - 2, current_date);. Its three rows in mentions_perception_snapshots now read day_to = 1970-01-01.
  2. Run refresh_mentions_perception_snapshot('<brand>', d) for 7, 30 and 90. They read today again.
  3. Web: on Mentions › Perception, pick a custom range in the past and let it read slowly (throttle the network). Switch to 7 days, then back. The note doesn't show until 4 s into the new read.

I ran steps 1–2 on the dev DB (00246 applied) for the demo brand: set aside after the rollups, back to today after the refresh.

Tests:

  • insights-rollups.test.js: b1's views aren't refreshed when its rebuild failed. "Perception rollups fail to rebuild" still holds.
  • _perception.test.tsx: back on filters whose last read was slow, the note waits its 4 s. The test fails without the fix.

Deploy note: 00246 needs only 00241. Apply it any time before or with the server deploy.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run:

  • web yarn test and yarn build;
  • server npm run format, npm run lint and npm test;
  • bash supabase/build-schema.sh.

🤖 Generated with Claude Code

Follow-ups from ansvisor#1385's review:
- 00246: refresh_mention_terms_daily sets the brand's snapshots aside at
  its end, in the rebuild's transaction, so the default view reads the
  new rollups live until the snapshots are refreshed, even when that
  refresh fails or the rollups were rebuilt in SQL.
- The deleted-prompt drain refreshes the snapshots only after the terms
  refresh has run.
- The slow note is cleared when its read's effect ends, so going back to
  filters whose last read was slow doesn't show it before SLOW_MS.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk #1385's three follow-ups, as you suggested them. 1: the set-aside in refresh_mention_terms_daily (00246, the first option), applied on the dev DB and checked: set aside after the rollups, back to today after the refresh. 2: the drain's if (ok) { … }, with the assertion in "keeps the entry for the next drain when a refresh fails". 3: the slow-note cleanup, with a test that fails without it. Apply 00246 any time after 00241. Speed note: every rebuild now puts the default view on the live read until the refresh after it lands; refreshing 30 days first (the default range) could be a cheap follow-up if you want it.

@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 23, 2026
gkhngyk
gkhngyk previously approved these changes Oct 23, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. All three items from #1385's review are done:

  1. A snapshot is never older than its rollups. refresh_mention_terms_daily now sets the brand's snapshots aside as its last statement, inside the rebuild's transaction (00246_perception_snapshots_follow_rollups.sql:98-100). Every rollup rebuild reaches it:

    • refresh_mention_terms calls it last, for runs, the sweep and the history backfill.
    • The drain and the terms backfill call it directly.

    Each of these server paths refreshes the snapshots afterwards. A refresh that started before the set-aside can't write over it, because its computed_at is earlier.

  2. The drain refreshes only after a rebuild. if (ok) { … } is at server/src/lib/insights-rollups.js:138-141, with the b1 assertion at insights-rollups.test.js:278-279.

  3. The slow note is cleared in the effect's cleanup (_perception.tsx:86-87). Without the cleanup, slow would still hold the June read's key and the note would show at once, so the new test's toBe('') (_perception.test.tsx:352) catches it.

Checked on production:

  • Same function, plus one statement. I compared production's refresh_mention_terms_daily (pg_get_functiondef) with 00246, comments stripped. The only difference is the three-line update at the end.

    • The header is unchanged: security invoker, search_path = public.
    • Execute stays with postgres and service_role. create or replace keeps the ACL, and the revoke changes nothing.
    • The only other statement is the table comment.
    • service_role has UPDATE on mentions_perception_snapshots.
    • The only SQL caller is refresh_mention_terms, which is also service-role only, so no other role runs the new update.
  • Cost. I timed both versions on the largest brand as pg_temp copies in a rolled-back block:

    Call Before After
    One day (a run's call) 0.78–0.91 s 0.75–0.86 s
    A 7-day drain chunk 6.2 s 5.8 s

    The added update takes 0.5 ms for the brand's three rows, so the difference is within noise.

  • Locks. The update row-locks the brand's three snapshot rows from the update until commit. Commit follows at once, because it is the last statement of both functions.

    • The Perception read is a plain select and never waits on these locks.
    • A snapshot refresh computes its payload first and only locks its row in the upsert. Either side waits at most for the other's commit.
    • No new lock cycle: the refresh holds no other lock, and the advisory lock is unchanged.
    • A backfill in SQL should still commit per call, as the server scripts do. A single transaction over many brands would hold these rows, and widen the gap the PR describes, until it commits.
  • Applying. The migration changes no rows. The existing snapshots are served until each brand's next rebuild (its next run, the nightly sweep or a drain). That rebuild sets them aside, and the server refreshes them right after.

  • Order. None is needed between the migration and the server. The deployed server already refreshes after every rebuild path, and the drain change only skips a wasted refresh.

The trade-off you mention: from the first set-aside until the refresh after it, the default view reads live. On the largest brand, that covers the remaining day calls of the run or the sweep, plus 3–16 s for the three windows. Runs and the sweep come about twice a night. For a deleted prompt whose answers go back to June, the drain's 21 weekly chunks at about 6 s each make it about 2 minutes. That is fine as it is.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approving after merging main into the branch. The merge only brings in the squashed parent PR; the tree matches the one tested (web and server tests, typecheck, lint, format).

@gkhngyk
gkhngyk merged commit e58c9b0 into ansvisor:main Oct 23, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants