Skip to content

feat(chat): decide each read's dates, platforms and rows on the server (W1-17) - #1819

Merged
gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-w1-17-scope
Oct 30, 2026
Merged

gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-w1-17-scope

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Chat Wave 1, behind CHAT_AGENTIC. Stacked on #1800 (W1-16 reads). The diff against main includes the PRs below it until they merge.

W1-17: server-side scope, decided in one place (web/src/lib/chat/scope.ts).

  • Dates: a read with no dates reads the last 30 days. All time only when the message asks ("all time", "since we started", "başından beri"). A model date can't pass today, and a reversed range is put in order.
  • Platforms: a platform the plan doesn't track isn't read (Perplexity on Starter, Claude on Growth). The model gets not_on_plan and says the plan doesn't track it. On Starter, a read without a platform is limited to the plan's platforms; get_overview_summary gains a model filter for that.
  • Row caps: every read tool has a default and a cap (at most 50). The rest is cut, with {list}_more and truncated: true.
  • The step shows "Last 30 days" and the plan's platforms; scope_json.used keeps the scope used.

No migration, no new dependency.

Related issue

Part of Chat (Wave 1, CHAT-04 / CHAT-05 / CHAT-20).

Type of change

  • feat — New feature

How to test

cd web && yarn vitest run src/lib/chat/scope.test.ts src/lib/chat covers the default window, all-time phrases, future and reversed dates, not_on_plan, Starter's platform limit and the caps.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Web: prettier/eslint, typecheck and src/lib/chat tests pass (run one at a time on an 8 GB machine; build left to CI)
  • Tests added

🤖 Generated with Claude Code

cihan-gyk and others added 4 commits October 29, 2026 20:38
Chat Wave 1 W1-16 (CHAT-20 §9): twelve READ rows, each with its label,
its brand (brandOf) and `data_as_of` in what it returns (§15a), listed in
tools.ts with the rest:

- prompts: get_fanouts (Query Fan-outs, the plan's engines only),
  list_ai_answers (a prompt's newest answers, shortened, the plan's
  platforms only), get_prompt_insights (estimated AI volume, by intent,
  the most demanded prompts).
- visibility: get_overview_summary (the visibility summary with when
  tracking last ran and runs next).
- action_center: get_action_center_kpis (the KPIs page's snapshots),
  get_onboarding_progress (Getting started's steps).
- ai_traffic: get_gsc_summary and get_ga_summary (clicks and sessions
  against the period before, or "not connected", with Google's lag).
- account: list_members (e-mails for Admins only), list_connections
  (Google per brand, CMS sites, AI provider keys set or not, API key
  names and last use: never a key or credential), get_chat_policy,
  get_plan_facts (counts and the plan card, never an AI budget in
  dollars).

The brand reads run the pages' own loaders with the service role once
brandInOrg passed (as product visibility and prompt locations do): the
KPIs, traffic, fan-out and Getting started loaders now take their client
from dbClient(), which on the pages is still the member's own. The RPCs
they call don't read auth.uid() (ai_traffic_page_citations lets the
service role through). None has an MCP tool yet (mcpTool null).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ounts to what the plan and Google show

Review of W1-16:

- list_connections read every member's API keys with the service role,
  so anyone saw an Admin's key names and last use. It now reads the
  person's own, as Settings › API Keys and its RLS do (your_api_keys).
- get_gsc_summary reads Google's latest reported days for a range of a
  week or less, as the page does (gscLatestDays), and its data_as_of is
  the last day Search Console reported; get_ga_summary's is Analytics'.
- A Google source is connected when the workspace's connection is on
  (integration_connections) and the brand has its property picked, as
  AI Referral Traffic reads it.
- list_ai_answers reads up to 50, keeps the plan's platforms, then the
  number asked for, so a page isn't short.
- get_fanouts reads the plan's platforms from the rows up (getQueryFanout
  takes an optional platforms filter; the page passes none), so its
  totals, times searched and data_as_of are theirs.
- get_prompt_insights says when the volume read stopped at 500 prompts
  (truncated).
- get_chat_policy's page is Settings › Roles & access.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… platforms and row limits (W1-17)

Chat's scope decisions now live in one place, lib/chat/scope.ts, and the
engine applies them to every read before it runs (CHAT-04 items 2-6,
CHAT-20 §7):

- Dates: a read with no dates covers the last 30 days; all time only when
  the person's message asks for it ("all time", "since we started",
  "başından beri"...). The model's own dates are kept, end today at the
  latest and are put in order. `days` reads get their default (Google's
  28) or, on an all-time ask, their 90-day maximum.
- Platforms: a platform or model the plan doesn't track (Perplexity on
  Starter, Claude on Growth) isn't read; the model gets `not_on_plan` with
  the plan's platforms and says so. On a plan that tracks only some apps
  (Starter), a "Platforms by plan" read that names none is held to the
  plan's. get_overview_summary takes the same `model` filter for this.
- Rows: each read has a default and a maximum (50 at most, CHAT-04
  Rule 1). A `limit` read asks for one row more to see if there are more;
  every list is cut to its limit with `{list}_more` and `truncated: true`.
- The step shows "Last 30 days" and the plan's platforms; scope_json keeps
  the scope used (`used`: dates, platforms, rows). The model sees each
  read's period and platforms beside its rows, and is told what a cut list
  and `not_on_plan` mean.

Behind CHAT_AGENTIC, like the rest of the engine; no migration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…l a read whose platforms can't be matched

- A cut series oldest first (the visibility trend's buckets, the mentions
  summary's days) kept its first 50 rows: a 90-day or all-time trend lost
  its latest weeks. It now keeps its latest rows.
- A start date after today stayed as given (and, swapped with a clamped
  end, became an end past today). Both dates now stop at today.
- On a plan that tracks some platforms, a failed read of the brand's
  models held the read to the plan's own ids, which read as no data. The
  read now fails instead.

Claude-Session: https://claude.ai/code/session_0121amRjX7UFrgmGELAtkc7A
gkhngyk
gkhngyk previously approved these changes Oct 30, 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.

Looks good with e72adb5 (one commit on this branch). It fixes three things:

  1. A cut trend kept its oldest days. capRows keeps a list's first rows, but the visibility trend's buckets and the mentions summary's daily come oldest first. So a trend over 50 days dropped its latest days: on a 91-day trend the model got Aug 1 to Sep 19 and read Sep 19 as the end. A list dated oldest first now keeps its last rows, and the writer prompt says so.
  2. A start date after today wasn't clamped. Only the end was, so a reversed pair could end with date_to in the future, or come back reversed. Both dates now stop at today.
  3. A failed lookup read as zero on Starter. When brandModels failed, the read was held to the scraper ids chatgpt-web,copilot-web. The filter matches on model_used, so it came back empty and the model reported zero. On error brandModels now returns null, and the read returns failed.

The three new test cases fail without the commit.

Checked, fine: dates default to the last 30 days, all time needs the person's own words, and days stays within 1–90. Platforms the plan doesn't track are refused with not_on_plan. Every row limit is at most 50 and within its schema. The service-role models lookup runs only after the brand-scope check. The lookup can use the covering index; caching it once per brand per run would save a read.

Tests: src/lib/chat and src/lib/mcp: 54 files, 662 tests pass. plan-row.test.ts, scope.test.ts and engine.test.ts pass on the commit merged with main, and tsc, prettier and eslint are clean. No migrations. #1820 carries this commit and keeps the same rule in fit.ts.

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

Merging in order with the other approved PRs. This head merges current main; its tree is what main will be after this squash, checked with the full server and web suites, tsc, lint, format and next build.

@gkhngyk
gkhngyk merged commit d1acf35 into ansvisor:main Oct 30, 2026
8 of 9 checks passed
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