Skip to content

fix(reports): rank query fan-out over the whole window, and show per-engine coverage - #764

Merged
gkhngyk merged 2 commits into
mainfrom
fix/report-query-fanout
Aug 21, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
fix/report-query-fanout

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #762, from a report where Query Fan-out listed only two of the three engines that had actually fanned out. Two separate things were wrong.

The section ranked over an arbitrary 1000 rows

getFanoutSnapshot was the fourth section reading its window through an unpaginated select, and the worst of the four: no .range(), no pagination, and no .order() either, so the 1000 rows PostgREST returns were not even a defined slice.

On a brand with ~12,000 answers in the window that is a twelfth of the data, and almost nothing repeats inside a twelfth. The symptom is visible in the stored payload: all ten entries came out at timesSearched: 2, listed alphabetically because every count was tied. The real top query had been searched 24 times and was not in the list at all.

Now aggregated in SQL alongside the three from #762 (00064) — 220ms for that window — with the counting rules the fold used: a sub-query counts once per answer however often that answer repeats it, and an engine is the item's own source_platform when it has one, otherwise the answer's platform.

Fixing the rows did not bring the missing engine back

This is the part worth reading. Reading the right 12,000 rows produces the same two engines, because the ranking measures something other than what the section claims to.

Engine Distinct sub-queries Answers Top repeat
ChatGPT 3,341 1,286 9
Perplexity 952 1,857 16
Copilot 782 2,038 18

Ranking by how many answers ran the identical string rewards an engine for reusing its phrasing. ChatGPT rephrases every time, so it issues more distinct sub-queries than the other two combined and never places in the top ten.

A reader concludes ChatGPT does not fan out. It fans out the most.

So each engine now carries its distinct sub-query count and the answers those came from, above the table (00065). Two numbers rather than one because they point in opposite directions here — Copilot fans out on more answers, ChatGPT with more different queries — and either alone would mislead in its own way.

The ranking itself is untouched. Whether the top ten should be picked per engine is a larger question about what the section is for; this makes the current table honest rather than answering it.

Related issue

None — reported directly from testing, on the report generated after #762.

Type of change

  • fix — Bug fix

Migrations

00064_report_query_fanout_rpc.sql and 00065_report_query_fanout_engines.sql. Both already applied to the hosted database. Additive — two new functions nothing referenced yet — so applying before the code that calls them is the safe order.

How to test

Needs a brand with several thousand answers in the window.

  1. Generate a report including Fan-out over 7 days.
  2. Check the table. Expected: counts well clear of 2, in descending order, not alphabetical.
  3. Check the line above the table. Expected: every engine that fanned out, including ones absent from the table, each with its distinct sub-query count and answer count.
  4. Compare the top query against the Prompts page for the same range.
  5. Open a report generated before this. Expected: table as before, no coverage line — the payload has no such field.

Verification already done

  • EXPLAIN ANALYZE on both functions against production data: 220ms and 190ms for a 7-day window on the brand in question.
  • Checked the new function against the payload that surfaced this: stored entries all timesSearched: 2, new output 24 / 19 / 18 / 17.
  • The coverage figures in the table above are that function's real output, not an estimate.
  • yarn typecheck, yarn lint (0 errors), yarn format:check, yarn test (90 passing) and yarn build pass from web/.

Not covered by tests — same limitation as #762: nothing in this repo can reach a server action or a Postgres function.

Checklist

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

…ary 1000 rows

getFanoutSnapshot was the fourth section reading its window through an
unpaginated select, and the worst of the four: no .range(), no pagination, and
no .order() either, so the 1000 rows PostgREST returns were not even a defined
slice.

On a brand with ~12k answers in the window that is a twelfth of the data, and
almost nothing repeats inside it. The section that reported this came out with
every one of its ten entries at timesSearched = 2, ordered alphabetically
because the counts were all tied. The real top query had been searched 24
times and was not in the list at all.

The aggregation moves into SQL alongside the three from 00063, with the same
counting rules: a sub-query counts once per answer however often that answer
repeats it, and an engine is the item's own source_platform when it has one,
otherwise the answer's platform. 220ms for the window above.

This does not change which engines appear. Ranking by how many answers ran the
identical string favours engines that reuse their phrasing, and that is a
separate question from reading the wrong rows.
The table ranks sub-queries by how many answers ran the identical string. That
quietly measures how much an engine reuses its phrasing rather than how much it
fans out, and the difference is not small: over one week on one brand, ChatGPT
issued 3,341 distinct sub-queries to Copilot's 782, and appeared in the top ten
exactly never -- its most repeated string ran 9 times against Copilot's 18.

Read on its own, the table says ChatGPT does not fan out. It fans out the most.

Each engine now carries its distinct sub-query count and the number of answers
those came from, above the table. Variety and volume pull in opposite
directions here -- Copilot fans out on more answers, ChatGPT with more
different queries -- and one number could not have shown that.

The ranking itself is unchanged. Whether the top ten should be picked per
engine is a larger question about what the section is for, and it is not
answered here.

The coverage is gathered alongside the ranking, from the same rows and the same
window, so the two cannot disagree. Older reports simply have no coverage field
and render as before.
@gkhngyk gkhngyk added bug Something isn't working needs-migration Adds or edits a migration — apply it to the hosted database labels Aug 21, 2026
@gkhngyk
gkhngyk merged commit 139fd21 into main Aug 21, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the fix/report-query-fanout branch August 21, 2026 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working 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.

1 participant