Skip to content

fix(server): page prompt_visibility_summaries reads past the 1000-row cap - #1096

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/server-rpc-row-cap
Oct 16, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/server-rpc-row-cap

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

PostgREST returns at most 1000 rows per request (max_rows = 1000 in supabase/config.toml) and drops the rest without an error — the same cap that cost the Tracked Prompts page most of its Positions at 30d/90d on 2026-10-16 (web fix: #1094). prompt_visibility_summaries returns one row per prompt, and the server read it unpaged in two places, so a brand with more than 1000 prompts silently lost the rest from:

  • the weekly pulse's movers (promptScores in server/src/lib/pulse/metrics.js);
  • the uncited-mention signal (uncitedMentionCandidates in server/src/lib/signals/record.js).

Changes:

  • server/src/lib/rpc-pages.js (new): readAllRpcRows(name, page), the server's copy of the web's readAllRpcRows from fix(prompts): read every page of the position and visibility summaries #1094. It asks for ordered .range() pages of 1000 until a short page, stops after 100 pages with a logger.warn, and returns supabase-js's { data, error } (like chunked-in.js). A failing page returns the error and discards the partial read.
  • Pulse: the timeout retry from Tracking cycle: stop waiting on ghost Cloro tasks — cuts the nightly cycle from ~5h to ~1.5h #687 moves into a retried(name, read) helper shared by rpc and a new rpcRows, so a paged read is still retried (from its first page). promptScores reads prompt_visibility_summaries through rpcRows, ordered by prompt_id.
  • Signals: uncitedMentionCandidates pages the same RPC ordered by prompt_id and counts prompts by id, so a row repeated at a page boundary (a write landing between two pages) is counted once.
  • Both functions are exported for their tests, as fetchAllPendingRows is in tracking-worker.js.

Other server RPC reads, checked against the migrations — none of them needs paging:

Status RPCs
Paged in this PR prompt_visibility_summaries (pulse movers, uncited-mention signal)
Already paged ga_page_ai_visibility (page-opportunities.js)
Set-returning but bounded gsc_candidate_queries (LIMIT 200), content_opportunity_aggregates (one row), claim_opportunity_usage (at most one request's slot count), citation_url_ids (one row per distinct URL of a single answer)
Return one jsonb/scalar value (not subject to max_rows) all ae_* in signals/library/detect.js, *_aggregates_daily, tracked_prompt_count_daily, brand_landing_page_sessions, brand_prompt_opportunity_metrics, brand_prompt_fanout_queries, opportunity_basket_metrics, delete_brand_results_batch, health_probe, refresh_*, replace_prompt_clusters

Related issue

Server-side counterpart of #1094.

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

No UI change: both reads run in the nightly signal pass and the weekly pulse.

  1. cd server && npm test. The new tests serve the RPC through a small PostgREST stand-in that never returns more than 1000 rows per response:
    • src/lib/rpc-pages.test.js: paging, exactly-full last page, empty page, failing page, page limit + warning, and RPC_PAGE_SIZE never above max_rows in supabase/config.toml.
    • src/lib/pulse/prompt-scores.test.js: 2500 prompts are all scored over 3 ordered pages; a timed-out page is retried from the first page.
    • src/lib/signals/uncited-mentions.test.js: 1500 uncited prompts are counted past the first 1000; a prompt repeated at a page boundary is counted once.
  2. With metrics.js and record.js reverted to main (only the export added), 6 of the new tests fail: the unpaged read stops at 1000 rows.

server/: npm run format (no changes), npm run lint and npm test (82 files, 1207 tests) pass. npm run version:check at the root fails on main too (docker-compose images still at 0.2.0 against 0.3.0), and this PR doesn't touch versions.

Checklist

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

🤖 Generated with Claude Code

… cap

PostgREST returns at most 1000 rows per request (max_rows) and drops the
rest without an error. prompt_visibility_summaries returns one row per
prompt, so a brand with more than 1000 prompts lost the rest from the
weekly pulse's movers and from the uncited-mention signal.

Add lib/rpc-pages.js (readAllRpcRows), the server's copy of the web's
paging loop: ordered .range() pages of 1000 until a short page, capped at
100 pages with a warning, returning { data, error }. The pulse keeps its
timeout retry, which now restarts the paged read from its first page, and
the uncited-mention signal counts prompts by id so a row repeated at a
page boundary is counted once.

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

@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.

Thanks, approving. On the tree merged with main server tests pass (1265). On production the largest brand has 709 prompts, so prompt_visibility_summaries stays one page everywhere for now. The paging costs nothing today and covers a brand past 1000 prompts.

@gkhngyk
gkhngyk merged commit 3b2fe27 into ansvisor:main Oct 16, 2026
6 checks passed
gkhngyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 17, 2026
…l-evidence

main carries the stack below as squashes. uncitedMentionCandidates keeps ansvisor#1096's one-row-per-prompt dedupe and this branch's most-mentioned-first order, and promptIds takes the rows' ids.
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