Repository navigation
fix(action-center): keep an action's prompts, pages and competitors once, as its count (AC-009, 1/3) - #1388
Conversation
…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]>
|
@gkhngyk AC-009 part 1/3 (00244, server + SQL): actions keep their target list once; prompt delete/pause and competitor delete mark it. Part 2 (web reads one count everywhere, '(1 removed since this action was created)') follows stacked on this; part 3 is the nightly drift check. |
…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]>
gkhngyk
left a comment
There was a problem hiding this comment.
A few fixes before merge. Most are in the backfill, which runs once on prod and is cheaper to get right before it runs than to repair afterwards.
I ran the backfill on prod as a pg_temp copy in a rolled-back transaction:
- 739 live actions gave 4,129 rows in about 0.45 s.
- 316 rows were marked removed: 121 deleted and 195 paused.
- 664 actions get exactly
payload.targetCountrows. - The rest are below.
-
Backfill: the topic opportunity gets the prompts it measures, not the suggestions it counts.
00244_action_entities.sql:92-99readspromptIdsbeforetargetsfor a prompts action.- For
topic_opportunity,promptIdsare the topic's tracked prompts.from-topic-opportunity.js:123-124says so itself. - New sends are fine, because their
targetIdstake precedence. The existing one on prod gets its 3 tracked prompts for a count of 2. - Fix at
:315: passcase when a.kind = 'topic_opportunity' then a.payload - 'promptIds' else a.payload end.
-
Backfill: 19 actions that show a count get no list.
- 15
fix_low_scoresactions have the old{ url, score }object as their target id.:136now skips it, correctly. Their counts run from 1 to 25. - 4
convert_mentionsactions never named their prompts. Their counts run from 3 to 13. - (49 more have no
targetEntityand no count, so an empty list is right for them.) - If part 2 reads
totalfromaction_entity_counts, these 19 show 0. Do one of these:- Make part 2 fall back to
payload.targetCountfor an action with no rows, and say so here so part 2 is held to it. - Or backfill what can be recovered. For
convert_mentions, the linkeduncited_mentionssignals'promptIds(throughaction_signals) recover 19 of their 24 prompts.
- Make part 2 fall back to
- Either way, step 1 of How to test ("every action … has a list") doesn't hold on prod.
- 15
-
Two code paths count more items than the stored list keeps. The trigger keeps each key once and at most 1,000 keys. The JS counts the raw list:
from-opportunity.js:108-110: with no prompt ids, the keys are prompt texts. One content opportunity on prod has 20 texts but only 18 distinct ones. Its payload would say 20 and its list keep 18.from-topic-opportunity.js:121-125:targetCountissuggestions.length, buttargetIdsdrops empty texts and the trigger merges duplicate ones.definitions/_helpers.js:82-86:targetCountis the size of the union across signals. Each signal is capped at 1,000 but the union isn't, so two big signals givetargetCount> 1,000 with 1,000 rows kept.
Fix: dedupe the keys in JS (
[...new Set(keys)]). DerivetargetCountfrom the same list the trigger keeps, capped atMAX_TARGET_IDS, so the payload andaction_entitiesagree. -
Low, fine as a follow-up: signal payloads now carry up to 1,000 ids each.
- Signals that grow:
- group signals past 50 rows carry
targetIds; uncited_mentionscarries up to 1,000promptIds;audit_low_scorecarriestargetIdseven whenurlsalready lists every page (record.js:336-340).
- group signals past 50 rows carry
- The MCP
list_signalstool (web/src/lib/mcp/data.ts:3143) returns raw payloads for up to 200 signals, so an agent gets all of those ids. - Fix:
- Add
targetIdsto the audit signal only past 10 pages, asgroup()does past 50. - Leave
targetIdsout of the MCP raw row.
- Add
- Signals that grow:
Checked and fine:
-
Policy and grants. The policy is the same one
action_signalsandaction_eventsuse.authenticatedgets select only andanongets nothing. -
Functions. The definer functions pin
search_pathand are revoked frompublic,anonandauthenticated.action_entity_countsruns as the invoker. As a member, asking for 500 mixed action ids returned only that member's org's actions. -
Timing.
action_entity_countsasauthenticatedon the brand with the most actions took about 21 ms. 500 ids took about 3 ms. -
Triggers.
promptsalready has statement-level transition-table triggers on update and delete.- The payload self-update in the insert trigger doesn't fire either existing
actionstrigger: one fires only onstatus, the other only onbaseline. actionsisn't in a realtime publication.
-
Priority. No stored score moves:
- no action payload has more than 19
promptIds; - no signal has more than 47
promptIds; - so
slice(0, 50)inpromptIdsOfchanges nothing today.
- no action payload has more than 19
-
44ec1e58(pushed during this review) only addsremoved_keystoaction_entity_counts, and it's fine. Unlike the labels, the keys aren't capped at 20. That is what leaving them out of a list needs, and an action holds at most 1,000.
Merge notes
- It merges cleanly on current main (
5b679380) and contains no other open PR. - 00244 doesn't clash with 00240–00243, and none of those touch
actions,promptsorcompetitors. - For part 2:
web/src/types/supabase.tsneedsaction_entitiesandaction_entity_counts.
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]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 Thanks for running the backfill on prod. Everything is in 95708c6, on top of a merge of main (3a3291b).
One thing I couldn't check on the dev DB, which has no old opportunities: an old opportunity's count was select a.kind, count(*) actions,
count(*) filter (where l.total = (a.payload->>'targetCount')::numeric) same,
count(*) filter (where l.total = 0) no_list,
count(*) filter (where l.total > 0 and l.total <> (a.payload->>'targetCount')::numeric) differ
from actions a
cross join lateral (select count(*) total from action_entities e where e.action_id = a.id) l
where a.deleted_at is null and jsonb_typeof(a.payload->'targetCount') = 'number'
and (a.payload->>'targetCount')::numeric > 0
group by 1 order by 1;Part 2 caps the removed count at the saved count, so a smaller list can only understate what was removed. Server: 2,115 tests, lint and format pass; schema.sql rebuilt. #1389 (now with |
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good. All four items from the last review are done or answered. One note on the deploy order is below.
Earlier items
- Topic opportunity backfill: done.
00244_action_entities.sql:329now passespayload - 'promptIds'fortopic_opportunity.- On prod, the existing one now keeps its 2 suggestions instead of its 3 tracked prompts.
- The 19 actions with no list: answered.
- #1389 shows
payload.targetCountless the removed rows, so these actions keep their saved count. - The migration comment (
:322-325) and How to test step 1 now say so. - On prod it is the same 19: 15
fix_low_scoresand 4convert_mentions.
- #1389 shows
- Payload count vs kept list: done.
targetKeys(lib/target-ids.js:16) builds keys by 00244's rules. Spaces are trimmed (only spaces, likebtrim). Each key is kept once, in order, up to 1,000. Empty keys, objects, lists and text starting with{or[are dropped.- Every path that sets a counted list now takes
targetCountfrom it:targetPayload(which liststargetIdswhenever the names shown differ from the keys),actionTargets, the topic opportunity, g02, F01 and the topic gap. - I checked the other paths. Content drafts, mentions, traffic pages and Improve send unique keys, so their counts already match what the trigger keeps.
- Signal payload size: done.
- The audit signal carries
targetIdsonly past 10 pages (record.js:340). - The MCP rows and the drawer drop
targetIdsin #1389 (withoutTargetIds).
- The audit signal carries
Backfill re-run on prod (pg_temp copy in a rolled-back transaction, head 95708c69)
- 739 live actions gave 4,128 rows in about 0.38 s. That's one fewer than last time: the topic opportunity's 3 rows became 2.
- 121 rows are marked deleted and 195 paused, as before.
- 685 actions have a
targetCountabove 0. 665 of them now get exactly that many rows (last time 664). - The other 20:
- 19 keep no list. These are the actions in item 2. Their payload names nothing a list can hold:
- the 15
fix_low_scoreshave{ url, score }objects as target ids; - the 4
convert_mentionsnever named their prompts.
- the 15
- 1 content opportunity counts 20 but keeps 18.
- 19 keep no list. These are the actions in item 2. Their payload names nothing a list can hold:
- 54 actions have no count: 49 have no
targetEntity, and 5 have one with a count of 0. They correctly keep no rows. - No action names more than 50 targets, so no backfilled list is cut at 50.
- Of the opportunities you asked about: the 4 with
promptIdsall match, and 3 of the 4 named by text do.
Deploy order (no code change)
- The PR body says to apply 00244 "with or before the server deploy".
- The server on prod still makes
fix_low_scoresactions with{ url, score }target ids every night. Three were made in the last two days. - If 00244 goes in before this server, any made in between keep no list. #1390 then reports each one as missing every night until it closes.
- Deploy this server first, or in the same window before the next nightly run.
- Server first is safe: nothing in the server needs 00244. The backfill reads the
targetIdsthe new server writes, and keeps the full list for any action made in between.
Merge notes
- It merges cleanly on current main (
c4568814). - Re-merging the parents of
3a3291b4gives the same tree exceptschema.sql. That file is main's plus 00244's 329 lines, with nothing else changed. 95708c69touches only the files listed above.- It contains no other open PR. #1389 and #1390 both contain its head.
- 00244 doesn't clash with 00243, 00245, 00246 or 00247.
gkhngyk
left a comment
There was a problem hiding this comment.
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).
Summary
AC-009, part 1 of 3: an action now keeps the list of what it is about (its prompts, pages, competitors…) once, when it is created. Prompts deleted or paused since, and competitors deleted, are marked removed.
The web reads counts from this list in part 2 (stacked on this); part 3 adds the nightly drift check.
Why. One action showed 13 prompts in its title, 12 in its detail and 13 in a task:
payload.targetCount, saved at creation;current_value.Migration 00244
action_entities(action_id, entity_type, entity_key, label, removed_at, removed_reason). Members can select it (asactions); the service role can write.An
AFTER INSERTtrigger onactionswrites the list from the payload, so every way an action is made keeps it alike: the engine plus the seven Send-to-Action-Center paths. The list comes from:targetIds;promptIds(prompts) orpages(pages);targets, or the list under the targetEntity (Improve'splatforms).Empty lists fall through to the next source. Labels come from the payload, or from the prompt's text.
The trigger never refuses an action. An error becomes a warning, and part 3's check finds the action. It then moves
targetIdsout of the payload, so reads of the action don't carry up to a thousand keys.Statement-level triggers:
deleted;paused, and cleared when it's resumed;deleted, matched by id or name across its brand's actions.action_entity_counts(uuid[]): per action and type, the total, the removed and up to 20 removed names. Security invoker, read under the member's policy.Backfill: existing actions get the list their payload still names (for a big group, the first 50; their count stays the payload's). Prompts already gone are marked removed.
targets, not the tracked prompts it measures (payload - 'promptIds').fix_low_scoreswith{ url, score }ids and 4 oldconvert_mentions. Part 2 (fix(action-center): show an action's count, less what was removed since, everywhere (AC-009, 2/3) #1389) never readstotal: it showspayload.targetCountless the rows removed, so these keep their saved count.Server
Signals:
targetIds, up toMAX_TARGET_IDS= 1000,lib/target-ids.js).collectTargets/targetPayload: count the whole group, named ones first, and passtargetIdson.One count, one list.
targetKeys(values)(lib/target-ids.js) gives the keys an action keeps exactly as 00244 keeps them: trimmed of spaces, once each, in order, at most 1,000, with no empty key, object or list. Every path that sets a counted list takestargetCountfrom it:targetPayload(signals each capped at 1,000 can add up past it);current_value;Content drafts, mentions and traffic pages send unique ids or paths, a few at a time.
Audit signal:
targetIdsonly past the 10 pagesurlslists, as group signals list theirs past 50.g02 counts and keeps its uncited prompts.
Person-sent paths keep the keys of what their count counts:
targetCount= that list), else the prompts it names;Fix: Site Audit's
urlsentries are{ url, score }.legacyTargetsread them as strings, so every page collapsed into one "[object Object]" key.Priority demand still reads the first 50 of a signal's prompts, as before.
Verified on the dev DB (00244 applied):
targetIdsfrom its payload, and marks nothing removed at creation;total 11, removed 2;Deploy note
Apply 00244 with or before the server deploy; there's no order trap. The trigger and backfill don't need the server, and the server's new
targetIdsonly add keys the trigger reads.Related issue
AC-009 "Single count source" (ROADMAP, Drive › Diğer). No GitHub issue.
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
select action_id, count(*) from action_entities group by 1). The old actions listed above don't, and keep their saved count.targetEntity: 'prompts'andtargets: ["some prompt text"]. It inserts, with one row labelled with that text.action_entity_countsshowsremoved 1;removed 0;removed 1(deleted).deleted.Tests:
target-ids.test.js: keys trimmed, once, in order, no empty key, object or list, capped at 1,000;definitions/_helpers.test.js: the whole group counted, named first; two big signals capped at 1,000; names whose keys differ listed; audit urls by address; F01 and g02 counting their lists;signals/library/group.test.js:targetIdspast 50;signals/audit-low-score.test.js: notargetIdsat 10 pages, all of them past 10;from-opportunity.test.js:actionTargets, a prompt named twice counted once;from-topic-opportunity.test.js: suggestions counted as kept, and none with words refused;uncited-mentions.test.js.There is no SQL test pattern in the repo; the SQL was checked on the dev DB as above.
Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/): web not touched in this partyarn typecheckpasses (run fromweb/): web not touched in this partyarn format:checkpasses (run fromweb/): web not touched in this partAlso run:
npm run format,npm run lintandnpm test(2,115 tests);bash supabase/build-schema.sh.🤖 Generated with Claude Code