Repository navigation
fix(chat): rank and count as the pages do - #2462
Conversation
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
left a comment
There was a problem hiding this comment.
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).
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
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
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
left a comment
There was a problem hiding this comment.
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).
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
…-same-maths-as-pages Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
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
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
…-same-maths-as-pages Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
gkhngyk
left a comment
There was a problem hiding this comment.
Thanks, fixed: sub-day windows longer than 25 h now read the daily rollups over the covering whole days, with a note.
The merge-base changed after approval.
…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
Review fixes (17 Nov)
A long window that starts or ends inside a day now reads the daily rollups (review of 17 Nov:
getVisibilitySummaryForread rawcompetitor_aggregatesfor 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.tsgetVisibilitySummaryFor:competitor_aggregates_dailyandai_visibility_aggregates_daily. The result then hasroundedToDays({ dayFrom, dayTo }) and anote: "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.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: theget_visibility_summaryMCP tool description and itsdate_from/date_tofields now explain the rounding.docs/guides/mcp-server.mdxhas the same in its tools table.get_prompt_locationsread (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":competitor_aggregates_dailyandai_visibility_aggregates_daily, nevercompetitor_aggregates,ai_visibility_aggregatesorvisible_prompt_stats. Each covers the right days and hasroundedToDaysand the note.T00:00Z–T23:59:59Z, a midnight upper bound) have no note and never callcompetitor_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 noprompt_resultsread.web/src/lib/insights-windows.test.ts:coveringDayWindow(widening on each side, a non-UTC zone, a midnight upper bound, open bounds, unreadable input) andansweredSpanMs(bare days, capped at now, null cases).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:
explain_changealways 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.jsonwas regenerated. The merged base passed the full check set before this work.Changes
readCitationAggregates:list_citationsand its domain or address lookups), over whole UTC days:citations_domains_dailyandcitations_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.results_with_citationsand 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_cutand 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.getVisibilitySummaryForon a window inside a day (get_visibility_summary, also the MCP tool, andget_overview_summaryon 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_appearancescounts every appearance. The breakdowns cover the newest appearances, said inanswers_cutwhen cut.mentions_overviewandmentions_perceptionreturn one value;mentions_listpages.prompt_performance_aggregatesreturns one value.prompt_visibility_summariesreturns one row per prompt, paged by the page's own reader (readAllRpcRows).explain_changereads evidence one page per batch of prompts. It already marks a cut batchevidence_partialand draws no cause from it.brand_id.lib/chat/page-periods.ts, applied inscopeRead).get_visibility_summary,get_overview_summary,get_visibility_trend) and on Citations'list_citations, 24 hours means the latest completed run's UTC days.getTrackingWindow(runDays), run with the service role afterbrandAllowed(latestRunDaysForinoverview-reads.ts).latest_run: true. The writer says the window is the latest tracking run's day or days.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.lib/mcp/prompt-scores.ts).get_prompt_performancenow hasai_visibility_score(the page's Visibility) andcoverage_pct(the page's Coverage).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.sort_by: visibilitynow 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.avg_visibility, sorted byanswer_score. The description says which is which and never to callavg_visibility"Visibility".get_prompt_performancedoesn't ask for scores (withScore), so its output and order are unchanged.lib/chat/page-order.ts).list_action_center_taskstakesstatus: "all"(every status, as the list shows).sort: "updated"now breaks ties by the later action number, as the list does.list_content_opportunitiestakes the page'ssort(newest,score,oldest), ordered as the page's list orders them. Withoutsortit keeps its own order, highest score first. MCP passes no sort, so its order is unchanged.<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.explain_changeuses the run's period.aroundgiven, it compares that period's days, up to yesterday, with the same number of days just before.deltaDayWindows). A custom range is taken as picked, at most its last 92 days.get_visibility_trendreturns every bucket of its window, oldest first, without empty fields, and a note with how many days and from when to when.TREND_ROWS(400) instead of 50, and its share of the run is 20,000 characters. 90 days by day fit whole.fitResult, keeping its latest days. The result says so (truncated,buckets_more), never silently.approved-articles.jsonis regenerated.prompts-page-top: on Prompts (Last 24 hours), "What are my top 5 prompts?".get_prompt_performanceover the page's 24 hours.Page by page
How to test
cd web && SKILL_EVAL=prompts-page-top yarn vitest run src/lib/chat/agent-eval.test.tsChecks
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).npx tsc --noEmit -p .passes.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-exportsandsubscription-guard-coverage: 448 files, 5,770 tests, all pass (1 skipped: the real-model eval).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:list_citationsequalsgetCitationsOverview(totals, average, domains) from the page's daily functions, with the page's days. It selectsprompt_resultsonce 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.competitor_aggregatesand the other three functions, with noprompt_resultsselect and noanswers_cut..range(),total_appearances1,200, and the cut note.rollupWindowdays and Overview'sgetTrackingWindow().runDays. It is rolling before the first run and on Mentions, and nothing is read for a brand outside the person's access.getPromptVisibilitySummariessorted by the table'scomparePromptSortRows('visibility'). The old order is shown to differ. Presets equalresolvePromptRange.scopeReadholds to Starter's platforms still scores withprompt_visibility_summariesand noprompt_resultsselect. Its scores and order equal the page's, including off-plan answers that raise a prompt's score.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'sdeltaDayWindows(minus today), andwhyPeriodsaccepts them. Custom ranges, the 92-day limit, 24 hours,aroundand no range are covered.capRowsandfitResult.engine.test.ts:255–256, which were already there.git merge-tree --write-tree upstream/main HEAD(0044e8fea): clean.'use server'file change.chat-thread-view.tsx,page-pane.tsxandpage-open.tsare untouched.get_visibility_summaryhas the same shape as before this PR.list_citationsandget_product_visibilityadd one optional field,answers_cut, only when the period has more answers than the bounded read takes. Onlist_citations, the domains'article_typesare empty, as on the page's rows, andresults_with_citationscounts the answers read.Not in this PR
chat-thread-view.tsx.get_ai_traffic_summary(top 10 pages),get_prompt_insights(10, at most 24) andget_ads_opportunities(cut at 50).Needs a migration (not in this PR)
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 askcan_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
🤖 Generated with Claude Code
https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9