Skip to content

feat(action-center): check every night that each action's count and its list agree (AC-009, 3/3) - #1390

Merged
gkhngyk merged 10 commits into
ansvisor:mainfrom
cihan-gyk:feature/action-count-drift
Oct 23, 2026
Merged

gkhngyk merged 10 commits into
ansvisor:mainfrom
cihan-gyk:feature/action-count-drift

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

AC-009, part 3 of 3: the daily health e-mail checks that every open action's count agrees with the list it kept, and names a few that don't. It should always read 0.

Stacked on #1388 (part 1: 00244 action_entities, now with kept_at). Merge after it. Independent of part 2 (#1389).

Migration 00245: action_count_drift(p_examples). Service role only. It looks at open actions (not deleted, not completed or dismissed) that are about something (targetCount > 0, a targetEntity):

  • missing: an action made since lists are kept (00244's backfill time) that kept none. Older actions whose payload named nothing a list could hold (e.g. old g02 or F01 shapes) don't raise it every night;
  • mismatch: the list holds another number than the count the action saved. A list cut at the 1,000-key cap isn't a mismatch;
  • tasks: a planned task names another count of the same thing ("across 13 prompts" under an action of 12).

Each comes back with its count and up to 5 action numbers, the newest first, so a new regression shows before long-open actions. Payload values that aren't numbers are skipped, never cast.

mismatch and tasks judge only lists kept as their action was created (kept_at within a minute of created_at). Part 1's backfill wrote older actions' lists from what their payload still named, and some older counts never matched it, so they'd make the e-mail permanently red.

Server. health/report.js gains an "Action counts" section:

  • the issue list raises any non-zero kind, e.g. "7 open action(s) kept a list of a different size from their count (e.g. AC-40, AC-12)";
  • the e-mail shows all three counts;
  • a failed check says so, like the other sections.

On the dev DB (00244 + 00245 applied):

  • today it reads checked 2, missing 0, mismatch 0, tasks 0;
  • in a rolled-back transaction, an action whose payload named 2 of its 5 prompts and a task naming 4 of 3 were both raised.

Related issue

AC-009 "Single count source" (ROADMAP, Drive › Diğer): "A nightly check finds actions whose stored text and list disagree, and alerts the team (should be 0)". 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 00244 (fix(action-center): keep an action's prompts, pages and competitors once, as its count (AC-009, 1/3) #1388) and 00245, then run select action_count_drift();. Every count is 0 on a fresh database.
  2. In a transaction, insert an action whose targetCount is 5 while its payload names 2 targets, and a task whose title_params.count differs from its action's. The function names both. Roll back.
  3. Run the health report (POST /api/internal/health-report). The e-mail has an "Action counts" section.

Tests: health/report.test.js covers the issue lines, the e-mail block, all zero, the RPC's arguments, and a check that couldn't run.

Known edge: text keys are trimmed and deduplicated in SQL. A payload counting ["q", "q "] as 2 keeps 1, and the check rightly reports the mismatch.

Checklist

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

Also run:

  • server npm run format, npm run lint and npm test (2,056 tests);
  • bash supabase/build-schema.sh.

🤖 Generated with Claude Code

cihan-gyk and others added 5 commits October 23, 2026 02:50
…nce, as its count

One action showed 13 prompts in its title, 12 in its detail and 13 in a
task: the title and tasks read the count saved at creation, the detail
and exports re-counted signals the nightly pass rewrites, and deleting
or pausing a prompt changed nothing. Migration 00244 keeps each action's
targets in action_entities when it is created (a trigger, so every way
an action is made keeps them alike), marks a prompt deleted or paused
and a competitor deleted as removed, and reads the counts per action
(action_entity_counts). Group signals now list every target's id beyond
the 50 they name, so an action counts and keeps the whole group, and
Site Audit's low pages count by their addresses.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t each count counts

Review fixes on the action list (00244): a prompt kept by its text no
longer fails the uuid cast (which would have refused the action), the
insert trigger never refuses an action, and moves the target keys out of
the payload once kept. Each action sent by a person keeps the keys of
what its count counts (an opportunity's prompts, a topic opportunity's
suggestions, traffic pages by path); empty lists fall through. Nothing
is marked removed when a new action is made; only the backfill marks
prompts already gone. The counts RPC returns `total`; the limit is one
constant; priority still reads the first 50 prompts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ounts

action_entity_counts also returns the removed targets' keys, so a list
of an action's prompts or pages can leave them out by id rather than by
name (two prompts can share a text, and only 20 names come back).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
action_entities.kept_at says when a list was written: as its action was
created, or by the backfill for one made before. The nightly count check
(AC-009 part 3) judges only lists kept at creation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ts list agree

AC-009 part 3. action_count_drift (00245) counts the open actions whose
saved count and kept list disagree: an action made since lists are kept
that kept none, a list kept at creation of a different size from its
count (a list cut at the 1000-key cap aside), and a task naming a
different count from its action's; with the newest few action numbers.
The daily health e-mail raises any that isn't 0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 23, 2026
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk AC-009 part 3/3 (00245, server): the daily health e-mail now checks that each open action's count agrees with its kept list (should be 0). Stacked on #1388, which gained kept_at (91f74a1) so only lists kept at creation are judged.

cihan-gyk and others added 3 commits October 23, 2026 09:53
Review fixes for AC-009 part 1:
- targetKeys gives the keys an action keeps as 00244 keeps them (trimmed,
  once, in order, at most 1,000, no empty key, object or list); every
  path that sets a counted list takes targetCount from it: targetPayload,
  an opportunity's prompts, a topic opportunity's suggestions (refused
  when none has words), g02, F01 (no longer the signal's raw count) and a
  topic gap's prompts.
- The backfill reads a topic opportunity's suggestions, not the tracked
  prompts it measures.
- The audit signal lists targetIds only past the 10 pages it shows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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. There's one optional follow-up and a note on apply order.

Checked and fine

  • Security.
    • action_count_drift is security definer with search_path pinned.
    • It is revoked from public, anon and authenticated, and granted to service_role only.
    • It is stable and writes nothing, and it returns only counts and action numbers.
    • The server change only adds a section to the health e-mail, which goes to HEALTH_REPORT_RECIPIENTS. Nothing user-visible is written.
  • Cost on prod. I simulated 00244's backfill and ran a pg_temp copy of the function over it, in a rolled-back transaction.
    • It took 24 ms, and 23 ms on a second run.
    • It checks 685 open actions.
    • Every per-action read uses the primary key of action_entities or action_tasks_action_idx.
  • Day one reads all zeros: checked 685, missing 0, mismatch 0, tasks 0.
    • I also ran it with the kept_at / since guards off, so every list is judged. That gives missing 19 (the 15 old fix_low_scores and 4 convert_mentions from #1388), mismatch 1 (the content opportunity with two duplicate prompt texts) and tasks 0.
    • So the guards hold back exactly the backfill's known leftovers.
    • No task on prod names a count other than its action's.
  • Report. Each kind raises its own issue line, with up to 5 action numbers, newest first. A failed RPC becomes a "could not be checked" line rather than failing the e-mail. Tests cover each of these.
  • Merges.
    • 8b33329b and 8797009c re-merge to identical trees: no hidden changes.
    • The PR's own diff on top of #1388 is 00245, health/report.js, its test and schema.sql.
    • It merges cleanly on main and with #1389.

Low, optional follow-up

  1. report.js:370 prints "685 open actions checked", but on day one none of those lists is actually judged. Mismatch and tasks judge only lists kept at creation, and missing judges only actions made since 00244, so the line overstates what was checked until the old actions close.
    • Fix: return a judged count from 00245_action_count_drift.sql:84, the open actions whose list was kept at creation.
    • Then print it beside checked, e.g. "685 open actions, 12 with a list kept at creation".

Apply order

  • Apply 00245 after 00244. Its SQL body names action_entities.kept_at, so it fails to create without it.
  • Apply 00245 before deploying this server. Otherwise the morning e-mail reads "Action counts could not be checked" until it is applied.
  • Apply 00244 together with #1388's server, or after it (see #1388). Otherwise fix_low_scores actions made in between by the current server keep no list, and this check reports them as missing every night.

@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 24efbd7 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