Skip to content

fix(chat): rank and count as the pages do - #2462

Merged
gkhngyk merged 22 commits into
ansvisor:mainfrom
cihan-gyk:fix/chat-same-maths-as-pages
Nov 18, 2026
Merged

gkhngyk merged 22 commits into
ansvisor:mainfrom
cihan-gyk:fix/chat-same-maths-as-pages

Conversation

@cihan-gyk

@cihan-gyk cihan-gyk commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Merged with main (17 Nov): merged #2459 (with main at 3b4d242) and resolved the conflicts as a union of both sides: list_content_opportunities keeps the Content sorts and main's get_content_opportunity pointer, and the help keywords keep both sides. approved-articles.json is regenerated. get_prompt_locations (from main) now reads Prompts' rolling window. git merge-tree against main is clean.

Review fixes (17 Nov)

A long window that starts or ends inside a day now reads the daily rollups (review of 17 Nov: getVisibilitySummaryFor read raw competitor_aggregates for any window with a time inside a day. On the busiest brand that took 13 s for 7 days and more than 120 s for 90 days.)

  • web/src/lib/mcp/data.ts getVisibilitySummaryFor:
    • Whole UTC days still go to the rollups, as before.
    • A window with a time inside a day that holds at most about a day of answers (25 hours, counted up to now) is still read to the minute from the raw aggregates. Each read covers at most one day of answers. This keeps a rolling "Last 24 hours" exact.
    • A longer window is rounded to the whole UTC days it touches and read from competitor_aggregates_daily and ai_visibility_aggregates_daily. The result then has roundedToDays ({ dayFrom, dayTo }) and a note: "The period asked starts or ends inside a day and is longer than a day, so it was rounded to whole UTC days: … to …, every day it touches, …". An open bound stays open.
    • A bound that is neither a day nor a time is now refused with a clear message before any read (date_from and date_to take a day (2026-11-01) or an ISO time (…)). Before, it reached the database as a timestamp error.
  • web/src/lib/insights-windows.ts: two new pure helpers. coveringDayWindow() gives the whole UTC days a range touches. answeredSpanMs() gives how long a range holds answers for, up to now.
  • web/src/lib/mcp/server.ts: the get_visibility_summary MCP tool description and its date_from/date_to fields now explain the rounding. docs/guides/mcp-server.mdx has the same in its tools table.
  • Chat's windows are whole days, so its answers don't change.
  • Also in this round: the get_prompt_locations read (from main, fix(chat): read what the MCP Server already reads, and keep the two in step #2464) is a Prompts › Tracked read. The app now gives it the Prompts page's rolling window, like every other Prompts read on this branch (mcp-wrapped-reads.test.ts).

Tests

  • web/src/lib/mcp/visibility-summary.test.ts, "over a long window inside a day":
    • A 7-day and a 90-day window with mid-day timestamps call only competitor_aggregates_daily and ai_visibility_aggregates_daily, never competitor_aggregates, ai_visibility_aggregates or visible_prompt_stats. Each covers the right days and has roundedToDays and the note.
    • One open bound is rounded and kept open.
    • A 6-hour window, a rolling 24 hours open to now, and 24 hours to a day's end still use the raw aggregates, with no note.
    • An unreadable bound is refused before any RPC.
    • Whole-day windows (bare days, T00:00Z–T23:59:59Z, a midnight upper bound) have no note and never call competitor_aggregates.
  • web/src/lib/chat/page-maths.test.ts: a 24.5-hour window is added up over every answer from the raw aggregates. A 65-hour window with mid-day bounds is read from the rollups over 2026-11-10 to 2026-11-12, with no prompt_results read.
  • web/src/lib/insights-windows.test.ts: coveringDayWindow (widening on each side, a non-UTC zone, a midnight upper bound, open bounds, unreadable input) and answeredSpanMs (bare days, capped at now, null cases).

Merged with main (17 Nov): via the updated #2459; the eval files are a union (pageTop next to scenario checks); #2466's Topics/AI-bots/Search-Console scope test now expects this PR's rolling N × 24-hour window, as those pages cut a preset.

Review fixes (16 Nov):

Stacked on #2459. Until it lands, this diff also shows its commits. This PR's own changes are commits 064d0521b and 0284fd29f (review fixes).

Summary

Step 7 of the "Chat must read everything a page shows" plan (owner, 16 Nov).

Chat beside a page now counts and ranks the way the page does, so its numbers and its "top" lists are the ones on screen. Before, they could differ:

  • Counts. Citations read the window's answers in one query, which stops at 1,000. On a busy brand, Chat's citation counts were lower than the page's cards.
  • Last 24 hours. Overview and Citations read it as the latest completed tracking run's day or days. Chat read the rolling 24 hours.
  • Ranking (G9). Prompts ranks by Visibility, the AI Visibility Score. Chat ranked by the average answer score, a different number in a different order. Actions opens on Updated and Content on Newest; Chat's reads had no way to give those orders.
  • Why and trends. explain_change always compared 7 days with 7 days. A trend kept only its latest 50 days.

The first merge commit joined #2459 and #2460 (now in main). Their only conflicts were eval scenarios, eval data, eval tests and Chat Help's keywords. Each kept both sides, and approved-articles.json was regenerated. The merged base passed the full check set before this work.

Changes

  • Counts from the pages' own aggregates (no migration, no paging).
    • Citations (readCitationAggregates: list_citations and its domain or address lookups), over whole UTC days:
      • The domains, the citations and the answers come from the page's daily functions citations_domains_daily and citations_window_stats_daily, with the page's arguments. These are security invoker over the 00118 rollup tables, so the service role reads them by brand after the brand check.
      • Each domain is classified as the page classifies its rows. The Sources filter keeps its own domains, as the page filters its uncapped list. Every source's totals come from the same rows.
      • The cited pages, results_with_citations and the prompts citing a domain or page have no function the service role can run, so they come from one bounded request of the window's newest answers (at most 1,000 answers, never paged). When the period has more answers, answers_cut and the note say that the pages cover the newest answers "from on", while the domains and totals cover the whole period. The note never gives a count of rows.
      • A window that starts or ends inside a day (only the raw functions answer those, and they refuse the service role too) is counted from that one request, and says so when it is cut.
      • As on the page, the domains carry no article types.
    • getVisibilitySummaryFor on a window inside a day (get_visibility_summary, also the MCP tool, and get_overview_summary on its raw path): the raw aggregates the page reads, four functions together, no answer rows. Whole days keep the rollups. Both paths build the summary the same way.
    • getProductVisibility: the Products page reads cards itself, so this is one bounded request with an exact count. total_appearances counts every appearance. The breakdowns cover the newest appearances, said in answers_cut when cut.
    • Checked and already whole:
      • Mentions and sentiment: mentions_overview and mentions_perception return one value; mentions_list pages.
      • The visibility, competitor and share-of-voice aggregates and their daily versions return one value.
      • prompt_performance_aggregates returns one value.
      • The trend functions return one row a day or a week.
      • prompt_visibility_summaries returns one row per prompt, paged by the page's own reader (readAllRpcRows).
      • AI Traffic scans its logs a page at a time.
    • Left as designed: explain_change reads evidence one page per batch of prompts. It already marks a cut batch evidence_partial and draws no cause from it.
    • Every read still checks the brand against the person's organization and access first, and filters by brand_id.
  • Last 24 hours as the page reads it (new plain module lib/chat/page-periods.ts, applied in scopeRead).
    • On Overview's reads (get_visibility_summary, get_overview_summary, get_visibility_trend) and on Citations' list_citations, 24 hours means the latest completed run's UTC days.
    • Those days come from the pages' own getTrackingWindow (runDays), run with the service role after brandAllowed (latestRunDaysFor in overview-reads.ts).
    • Before a brand's first run it stays the rolling 24 hours, as the pages do.
    • The read's scope gets latest_run: true. The writer says the window is the latest tracking run's day or days.
    • Also found: Prompts (resolvePromptRange) and AI Referral Traffic (its page's own range) read 7, 30 and 90 days as the last N × 24 hours, not whole UTC days. Their reads now do the same. Mentions' function already turns its window into the same days as Chat's.
  • Prompts: the page's Visibility and order (new lib/mcp/prompt-scores.ts).
    • Each row of get_prompt_performance now has ai_visibility_score (the page's Visibility) and coverage_pct (the page's Coverage).
    • They always come from the page's own getPromptVisibilitySummaries (prompt_visibility_summaries), run with the service role. That function takes no platform, and the page counts every platform's answers, the plan's or not. So the scores are the page's on every plan, including Starter, whose reads are held to the plan's platforms.
    • Its default sort_by: visibility now ranks by that score with the table's own comparator: a prompt with no score counts as 0, and a prompt the page doesn't score goes last.
    • The old average answer score stays as avg_visibility, sorted by answer_score. The description says which is which and never to call avg_visibility "Visibility".
    • MCP's get_prompt_performance doesn't ask for scores (withScore), so its output and order are unchanged.
  • The page's default order (new plain module lib/chat/page-order.ts).
    • Actions: list_action_center_tasks takes status: "all" (every status, as the list shows). sort: "updated" now breaks ties by the later action number, as the list does.
    • Content Opportunities: list_content_opportunities takes the page's sort (newest, score, oldest), ordered as the page's list orders them. Without sort it keeps its own order, highest score first. MCP passes no sort, so its order is unchanged.
    • Prompts: the read's default is already the page's order.
    • Beside <page_context>, both models get one line: the page's default order, and the inputs that read its rows in that order for "the first ones" or "the top 5 on the page". A page filtered by status keeps its own status.
    • "What matters most" and "What should I do first" keep the read's own order (priority, or score).
    • The writer says which order a list is in.
    • The other list pages already match: Signals, Mentions and Library list newest first, as their reads do. Citations lists most cited first.
  • explain_change uses the run's period.
    • The run's period is the page's dates, else the person's range. With no periods or around given, it compares that period's days, up to yesterday, with the same number of days just before.
    • Those are the windows the pages compare a change over (deltaDayWindows). A custom range is taken as picked, at most its last 92 days.
    • Under 3 days (Last 24 hours) the Why read keeps its own last 7 days against the 7 before, because it needs 3 days to tell a change from a day's swing.
    • Periods or a day the person names stay theirs. The read's step now shows the period.
  • Trends return all their days.
    • get_visibility_trend returns every bucket of its window, oldest first, without empty fields, and a note with how many days and from when to when.
    • Its row limit is TREND_ROWS (400) instead of 50, and its share of the run is 20,000 characters. 90 days by day fit whole.
    • A longer series is cut by fitResult, keeping its latest days. The result says so (truncated, buckets_more), never silently.
  • Help. New section in Chat › "Numbers that match the page" (54 words): "Chat ranks and counts as the page does. "My top 5 prompts" on Prompts are the table's top 5 by Visibility, and "the first ones" on Actions or Content are the rows you see, in the page's order. On Overview and Citations, Last 24 hours is the latest tracking run, as on those pages." "Why a number changed" now says its default is the page's period or your range. Every section is under 150 words, and approved-articles.json is regenerated.
  • Eval. New scenario prompts-page-top: on Prompts (Last 24 hours), "What are my top 5 prompts?".
    • It must read get_prompt_performance over the page's 24 hours.
    • It must say "as on the page".
    • It must name the page's top 5 by Visibility, in order. The eval data ranks them differently by the average answer score, so an answer that uses the old order fails.

Page by page

Page Now the same as the screen
Visibility › Overview Last 24 hours is the latest run's days (summary, overview, trend). A trend by day returns every day, up to 90 and beyond. Why compares the page's period with the one before.
Visibility › Citations Citation counts, domains and answers from the page's daily rollups, over every answer of the period. Pages and citing prompts come from the newest answers, and the answer says so when the period has more. Last 24 hours is the latest run's days. The same applies to lookups by domain or address.
Prompts › Tracked Each prompt's Visibility (AI Visibility Score) and Coverage from the page's own function, on every plan, ranked by the table's Visibility comparator. 24h, 7d, 30d and 90d are the last N × 24 hours, as the page. "Top 5" is the table's top 5.
Action Center › Actions "The first ones on the page" read every status, most recently updated first, ties by number, as the list. A status filter on the page keeps its status. "What should I do first" stays by Priority Score.
Content › Opportunities "The first ones" read newest first, as the page opens. Score and Oldest are available too.
AI Referral Traffic Presets are the last N × 24 hours, as the page.
Visibility › Products Every appearance of a product counted. The breakdowns cover the newest, said when cut.
Mentions, Signals, Library Already matched: same windows, newest first.

How to test

  1. On a brand with more than 1,000 answers in 30 days, open Visibility › Citations on Last 30 days and ask Chat "How many citations do we have?". The number is the Total Citations card's. Before, it was lower. Ask "Which pages are cited most?": the answer says the pages come from the newest answers, from a given day on.
  2. On a Starter workspace, open Prompts and ask "What are my top 5 prompts?". The Visibility numbers are the table's.
  3. After the nightly run, open Citations or Overview on Last 24 hours and ask "How many citations?" or "How is my visibility?". The numbers match the cards. The step says "Last 24 hours", and the answer may name the run's days.
  4. On Prompts, sort by Visibility (the default) and ask "What are my top 5 prompts?". Chat names the table's first 5, with their Visibility. Prompts added in the last day lead the table until a column is picked, so sort by Visibility first.
  5. On Action Center › Actions (Updated, newest first), ask "What are the first 5 actions on the page?": the same 5 rows, in the same order. Ask "What should I do first?": by Priority Score, and the answer says so.
  6. On Content, ask "Show me the first 3 opportunities": the newest 3, as the list.
  7. On Overview with Last 30 days, ask "Why did visibility drop?". The Why card compares the page's 30 days, up to yesterday, with the 30 days before. On Last 24 hours it compares the last 7 days with the 7 before.
  8. On Overview with Last 90 days, ask "Show my visibility by day": every day of the 90.
  9. Optional, real model: cd web && SKILL_EVAL=prompts-page-top yarn vitest run src/lib/chat/agent-eval.test.ts

Checks

  • Merged base (e4e5bd84b) before this work: npx tsc --noEmit -p . passes; the vitest set below passes, 383 files and 5,188 tests (1 skipped: the real-model eval).
  • This branch, with the review fixes:
    • npx tsc --noEmit -p . passes.
    • Vitest on src/lib/chat (all), src/lib/mcp, src/components/chat, src/app/api/chat, src/app/api/mcp, the pages' tests (Citations, Overview, Mentions, Products, Prompts, Actions, Content) and their actions, src/lib/action-center, src/lib/help, src/i18n, use-server-exports and subscription-guard-coverage: 448 files, 5,770 tests, all pass (1 skipped: the real-model eval).
  • New tests (page-maths.test.ts, lookups.test.ts, visibility-summary.test.ts, eval tests) compare each read with the page's own read on one fixture:
    • 2,400 answers with PostgREST's 1,000-row limit: list_citations equals getCitationsOverview (totals, average, domains) from the page's daily functions, with the page's days. It selects prompt_results once and never with .range(), and the pages say they come from the newest answers. A window that fits the bounded read has no cut. A domain lookup takes the page's row and its prompts from that one read.
    • A window inside a day: the visibility summary counts all 2,400 from competitor_aggregates and the other three functions, with no prompt_results select and no answers_cut.
    • A product with 1,200 appearances: one request, never .range(), total_appearances 1,200, and the cut note.
    • Last 24 hours: Chat's window equals Citations' rollupWindow days and Overview's getTrackingWindow().runDays. It is rolling before the first run and on Mentions, and nothing is read for a brand outside the person's access.
    • Prompts: the order and scores equal getPromptVisibilitySummaries sorted by the table's comparePromptSortRows('visibility'). The old order is shown to differ. Presets equal resolvePromptRange.
    • Starter: a read that scopeRead holds to Starter's platforms still scores with prompt_visibility_summaries and no prompt_results select. Its scores and order equal the page's, including off-plan answers that raise a prompt's score.
    • Ties and missing scores: a prompt with no score sorts with the 0s, and an unscored one goes last, in both directions, as the table's comparator does.
    • Actions' rows equal the list's compareActions('updated') over every status, and the read's own default stays by priority. Content's rows follow the page's newest-first order. The page-order line respects a status filter.
    • explain_change's periods equal the page's deltaDayWindows (minus today), and whyPeriods accepts them. Custom ranges, the 92-day limit, 24 hours, around and no range are covered.
    • A 90-day trend keeps all 90 days through capRows and fitResult.
    • The eval scenario passes the page's top 5 and fails the answer-score order. The engine test shows both models get the page's order line and the writer gets the order rule.
  • ESLint and Prettier are clean on the changed TypeScript files. The only ESLint output is 2 warnings at engine.test.ts:255–256, which were already there.
  • git merge-tree --write-tree upstream/main HEAD (0044e8fea): clean.
  • No migration, new dependency, lockfile change or 'use server' file change. chat-thread-view.tsx, page-pane.tsx and page-open.ts are untouched.
  • MCP output: get_visibility_summary has the same shape as before this PR. list_citations and get_product_visibility add one optional field, answers_cut, only when the period has more answers than the bounded read takes. On list_citations, the domains' article_types are empty, as on the page's rows, and results_with_citations counts the answers read.

Not in this PR

  • The page's chosen sort (plan item 8). The panel sends no sort yet, so Chat uses each page's default order. That needs chat-thread-view.tsx.
  • Prompts' new prompts. Until a column is picked, the table puts prompts added in the last day first. Chat ranks them by Visibility with the rest.
  • Prompts' custom range. The page takes its days in the viewer's time zone; Chat takes UTC days, as elsewhere.
  • History's order. The page sorts by the date an action closed, or was raised if it is still running. Chat's history read sorts by the close date and puts running ones last.
  • Read caps from audit section 5 that are not about ranking: get_ai_traffic_summary (top 10 pages), get_prompt_insights (10, at most 24) and get_ads_opportunities (cut at 50).

Needs a migration (not in this PR)

  • The cited pages and the prompts citing them. The domains and totals now come from the page's own daily functions, which the service role can run. The page's URL list (citations_urls_daily), the URL detail's answers (citation_url_candidates, citation_url_occurrences) and the raw functions for a window inside a day (citations_domains, citations_window_stats, citations_urls) are security definer and ask can_access_brand, which answers no without a person. The proposed migration lets the service role through these six, as 00434 does for the Insights functions, with the same check for a person. It is schema-only, with no backfill. With it, Chat would read the pages and citing prompts from the page's functions, and the bounded read would go.

Decisions for the owner

  • The Why read on a preset. "Last 7 days" now compares the page's 7 days, without today (6 days, up to yesterday), with the 7 days before them. Those are the page's own change windows, minus today, which isn't tracked yet. The other choice is the 7 complete days up to yesterday against the 7 before. That is a full week each side, but one day off the page's period.
  • The migration above. Until it lands, the cited pages and the prompts citing them cover the newest answers (one request), and the answer says from which day. The domains and totals already match the page.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

Chat beside a page now reads what the page shows (the owner's rule,
2026-11-16; step 5 of the page-data plan, gaps G1 and G2).

- Period: a period the message names wins, then the page's date chip,
  then the person's default. A page on All time reads all time. On a
  page with its own dates the range card isn't shown and nothing is
  kept as the default; the composer hides the person's range chip
  beside the page's. A read with a period input (Ads) takes the range
  when it is one of its periods.
- Filters (lib/chat/page-scope.ts, from scopeRead): platform, region
  and topic fill the reads of the page's brand that take them; a
  list's own filters (status, category, impact, type, Sources, prompt)
  only the read of that page's list. Values are checked against each
  input's schema; a chip no read takes the same way stays text. The
  model's own input wins, "" reads all, and a question that asks for
  all of a filter drops that chip. A page of another brand than the
  run's keeps its filters as words only.
- Rules: the reader is told the page's dates and filters apply; each
  read's scope has from_page, and the writer says once what the
  answer covers, as on the page, never row counts.
- Help: Chat answers for the page's dates and filters unless you ask
  otherwise.
- Eval: citations-page-scope (24 hours, ChatGPT, Brand sources).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
The owner's decisions on ansvisor#2459 (16 Nov):

- Citations: the domain and page lists follow the page's Sources, as
  its table does, and the citations and their average per answer count
  every source, as its cards do. When list_citations took Sources from
  the page, the engine runs the same read over every source beside it
  (one row, same filters and window) and puts its totals next to the
  filtered lists as all_sources_totals (lib/chat/citation-totals.ts).
  The person's own Sources read once, as before. The writer says
  counts are for all sources and lists for the chosen Sources. No
  change to lib/mcp or the read's registry row.
- Ads: get_ads_opportunities takes the page's period only on Ads, and
  a period the message names anywhere; elsewhere it reads its own 30
  days, never the person's default or another page's range
  (ScopeFacts.rangeFrom: message, page or default).
- Lookups by domains or urls take no page filter, as codes and ids.
- Help: Ads' Chat line says the period it reads.
- Eval: the Citations scenario checks the answer says its scope.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Stacks step 7 on both open Chat PRs (ansvisor#2459 page period and filters,
ansvisor#2460 lookups by name). Their eval scenarios, eval data, eval tests and
Chat help keywords are kept as a union; approved-articles.json is
regenerated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Step 7 of the "Chat must read everything a page shows" plan: Chat's
numbers and "top" lists equal the screen's.

- Counts past 1,000 answers: the citations scan, the visibility
  summary's raw window and a product's appearances read the window a
  page at a time, newest first, up to 50,000 answers (answer-pages.ts).
  Past that the note says what the counts cover, never a row count.
- Last 24 hours as the page reads it: on Overview and Citations reads,
  the latest completed tracking run's days (the pages' own
  getTrackingWindow, with the service role after brandAllowed); the
  rolling 24 hours before a first run. Prompts and AI Traffic read a
  preset as the last N x 24 hours, as their pages do (page-periods.ts).
- Prompts: get_prompt_performance gives each prompt's AI Visibility
  Score and Coverage, as the Prompts table (the page's own read, or the
  same sums over a platform's answers) and ranks by it by default;
  avg_visibility stays, sorted by "answer_score", and is labelled.
- The page's default order: Actions can read every status, most
  recently updated first (the list's tie-break too); Content
  Opportunities take the page's Sort (newest, score, oldest). The
  models get the page's order beside <page_context>; "what should I do
  first" keeps priority, and the writer says which order a list is in.
- explain_change compares the run's period (the page's, else the
  person's range) with the days before it, as the pages compare a
  change, to yesterday; Last 24 hours keeps its 7 against 7.
- get_visibility_trend returns every day of its period, compact, within
  its share of the run.
- Help: "Numbers that match the page"; "Why a number changed" says its
  new default. approved-articles.json regenerated.
- Eval: prompts-page-top ("What are my top 5 prompts?" on Prompts).

No migration. MCP output is unchanged but for additive fields.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

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

answer-pages.ts readAnswerPages pages prompt_results by OFFSET, 4 pages at once up to 50k rows with jsonb (competitor_mentions / citations), and now sits under getVisibilitySummaryFor (also the external MCP tool), readCitationAggregates (twice per question with #2459's all-sources read), getProductVisibility and promptScoresFor. Busy brands on 90 days / All time will read tens of thousands of jsonb rows per call — the pages use rollups/RPCs (00118 daily citation rollups, Overview RPCs); please read through those instead. Also promptScoresFor only uses prompt_visibility_summaries when no model is set, but scope.ts always sets the plan's platforms on Starter, so Starter takes the raw path and drops off-plan answers the Prompts page counts (pass p_models or skip the restriction). Minor: ai_visibility ties/nulls sort differently from the page (null as 0 there).

When Sources came from the Citations page, the engine ran list_citations
twice in parallel: the filtered read and the same read over every source
(limit 1) for the cards' totals. Limit 1 didn't shrink the work, so every
such question scanned the window's answers twice. The read now counts every
source's citations in the same pass that builds the Sources-filtered domain
and page lists, and returns them as all_sources_totals when the engine asks
(ChatToolContext.allSourcesTotals): one read, one scan. The writer's rule
and the result's shape stay as they were.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
The maintainer's review: readAnswerPages paged prompt_results by offset,
four pages at once up to 50,000 rows of jsonb, under the visibility
summary (also the external MCP tool), the citations reads, product
visibility and the prompt scores. It is gone.

- Citations (list_citations and the domain/page lookups): over whole UTC
  days the domains, citations and answers come from the Citations page's
  own daily rollups (00118: citations_domains_daily and
  citations_window_stats_daily). They are security invoker over the rollup
  tables, so the service role runs them after the brand check, by brand.
  Every source's totals come from the same rows. The pages and the prompts
  citing them have no aggregate the service role may run
  (citations_urls_daily and citation_url_occurrences refuse it), so they
  come from one bounded request of the newest answers, never paged; the
  result says what that covers when the window holds more.
- Visibility summary inside a day: the raw aggregates the page reads
  (competitor_aggregates, ai_visibility_aggregates, visible_prompt_stats,
  tracked_prompt_count), no answer rows. Whole days keep the rollups. The
  MCP output keeps its shape.
- Product visibility: the Products page reads cards itself, so one bounded
  request with an exact count: every appearance counted, the breakdowns of
  the newest read, said when cut.
- Prompt scores: always the Prompts page's prompt_visibility_summaries. It
  takes no platform, and the page counts every platform's answers, so the
  plan's platforms no longer send Starter to a raw read.
- Visibility order: the Prompts table's own comparator, a prompt with no
  score counted as 0, an unscored one last.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

@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, the four earlier points are fixed. One left: getVisibilitySummaryFor (data.ts) now calls raw competitor_aggregates whenever a window starts or ends inside a day. On the busiest brand that took 13 s for 7 days and ran past 120 s for 90 days. Chat's windows are whole days, but external MCP calls with timestamps hit it. Please snap to whole days (or refuse sub-day windows longer than ~1 day).

The page's own-list filters (and, above, its search) reach a read only
when the read's route is the page's. The Prompts area's base path opens
on Tracked Prompts (next.config.ts redirects it there), but it never
matched list_prompts' /dashboard/prompts/tracked. One alias map in
page-route.ts (pageRoute, samePage) now decides it for the page's
filters (page-scope.ts) and its period (scope.ts).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
…-same-maths-as-pages

list_content_opportunities' description keeps both sides: the Content
page's sorts and main's pointer to get_content_opportunity.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
getVisibilitySummaryFor read the raw competitor_aggregates whenever a
window started or ended inside a day: on the busiest brand 13 s for 7
days and past 120 s for 90, which external MCP calls with timestamps
reach. Now a window of at most about a day (25 hours of answers, up to
now) is still read to the minute from the raw aggregates; a longer one
is read over the whole UTC days it touches from the daily rollups, and
the result says so (roundedToDays and a note). A bound that isn't a day
or a time is refused with a clear message before any read. Chat's
whole-day windows are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
help/chat/chat.md keeps both sides' keywords; approved-articles.json
regenerated from the help files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
get_prompt_locations (from main) is a read of Prompts › Tracked, so the
app gives it a preset as that page reads it: the last N × 24 hours up to
now (page-periods.ts rollingWindow), as every other Prompts read here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
…-same-maths-as-pages

help/chat/chat.md keeps both sides' keywords; approved-articles.json
regenerated from the help files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
@mintlify

mintlify Bot commented Nov 17, 2026

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit d1a83ba.

Approve preview

gkhngyk
gkhngyk previously approved these changes Nov 18, 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.

Thanks, fixed: sub-day windows longer than 25 h now read the daily rollups over the covering whole days, with a note.

@cihan-gyk
cihan-gyk dismissed gkhngyk’s stale review November 18, 2026 11:25

The merge-base changed after approval.

@gkhngyk
gkhngyk merged commit 633f496 into ansvisor:main Nov 18, 2026
13 checks passed
gkhngyk pushed a commit that referenced this pull request Nov 18, 2026
…erge

The merged base runs #2462's page periods over the reads #2464 and #2466
add: Prompts › Tracked's locations, the Topics tab, and AI Referral
Traffic's bots and Search Console pages cut a preset as their pages do (the
last N × 24 hours to today), and the eval set now has rows 38 (Today's first
action) and 39 (losses to a competitor). Their tests say so.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
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.

3 participants