Skip to content

fix(chat): look up any listed item by its code, id, words or address - #2460

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/chat-lookups-by-name
Nov 16, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/chat-lookups-by-name

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Step 2 of the owner's plan "Chat must read everything a page shows" (16 Nov): lookups for every visible code or name.

  • Until now, Chat could find most items only by paging through a list. When someone asked "what is S-07?" and S-07 was past the first page of 50, Chat said it couldn't find it.
  • Now each list read can look an item up directly: by the code, id, words, domain or address the person named.
  • A lookup works whatever page the item is on, and in any status. The list's own filters and paging don't hide it.
  • Every lookup returns what it found, plus not_found for what it didn't. A short note tells Chat to say "not found" plainly, with where it looked, and never to offer "the closest" items.
  • This follows the pattern of fix(chat): look up an action by its code, and never answer with "the closest" ones #2445 (codes on list_action_center_tasks).

Changes

Read New inputs What it matches
list_signals codes, ids A signal's S- code, written "S-07", "S 7", "s7", "#7" or "7" (searchedSignalNo, the same rule as the Signals search), or its id. Every status and date: the period, status, kind and page don't apply.
list_prompts search, ids Every word of search somewhere in the prompt's text, in any order and case, or the id. Every topic, active and paused: topic_id and is_active don't apply. Search results page by offset.
get_prompt_performance prompt_ids Those prompts' figures over the same period, platforms and region, whatever their topic. topic_id, sort, order and the page don't apply. A tracked prompt with no answers in the window goes in no_results.
list_citations domains, urls A domain (scheme, www. and path don't matter) finds itself and its subdomains, each with its 5 most cited pages. An address finds its page however it is written: scheme, www., query and a trailing slash don't matter. Each item comes with the prompts whose answers cite it. Every source type: source_filter and the page don't apply. Not cited in the period: it goes in not_found, and for a page the note also says whether its domain is cited.
list_content_opportunities search, ids Title words or the id. Every status, archived included. A year in the words isn't matched, because Content shows a past year in a title as this year.
list_drafts search, ids Title words or the id. Every status. Archived drafts are included and marked archived: true.
get_reports search, ids Title words or the id.
  • Never cut by a limit. NAMED_ROWS in scope.ts now lists the fields of each lookup above. If the model sends a limit beside a lookup, it is dropped. This is a small, local change: a field list and a namesRows() helper.
  • Signal codes on every signal row. list_signals page rows now carry code (S-07). The actions built on a signal now show the codes the app shows (AC-03). Before, they showed the old global number (AC-1497).
  • Descriptions. Each read's description now says: read by the code, id, words or address first; which of the list's inputs a lookup ignores; and say "not found" plainly, never with the closest. get_draft's id input now points to list_drafts' search.
  • Shared code. The parsing, matching and note words live in a new plain module, lib/mcp/lookup.ts. The reads and the eval both use it. The DB lookups are lookUpSignalsFor (signal-page.ts); lookUpPromptsFor, lookUpPromptPerformanceFor, lookUpCitationsFor, lookUpOpportunitiesFor and lookUpReportsFor (data.ts); and lookUpDraftsFor (content.ts).
    • Each one runs through the list's existing query. The changes to the shared functions are additive: promptsPageFor, contentOpportunitiesPageFor and getPromptPerformanceFor get new optional params, and reportsPageFor and contentDraftsPageFor get a new optional last argument.
    • listCitationsFor's scan moved into readCitationAggregates, which both citation reads share. Its output is unchanged.
  • MCP. Tool shapes are unchanged. No MCP tool passes the new params, so the MCP queries and outputs stay as they were.
  • No rules or engine change. READ_ON_RULES and NEVER_GIVE_UP_RULES already say to look an item up first. rules.ts and engine.ts aren't touched.
  • Help. One line in Chat › "Long lists": ask about one item by its code, words, domain, address or title. The section is now 141 words. approved-articles.json was regenerated.
  • Evals. Two new question scenarios on a long_lists workspace:
    • signal-by-code: "S-07 nedir". S-07 is resolved, from August, and past the first page of 60 signals.
    • citation-by-domain: "decafdigest.example yapay zekâ yanıtlarında kaç kez kaynak gösterildi?". The domain is 66th of 70.
    • Both must read by code or domain, name the item, and name no other item in its place. They also pass the give-up check.
    • give-up.ts counts domains, urls and prompt_ids as lookups.

Safety

  • Scoping. Every lookup checks brandAllowed first: the run's organization and the person's brand access. Nothing else is read for a brand outside them.
    • The rows are filtered by brand_id.
    • Signals, opportunities, drafts and reports also inner-join brands.organization_id. Prompts join prompt_sets.brands.organization_id.
    • An id or code from another brand or workspace comes back in not_found.
    • New rows in brand-scope.test.ts cover all 7 lookups.
  • Citation lookups reuse the existing scan, with the brand checked first. The texts of the citing prompts are read only for this brand's prompts.
  • Model text is data.
    • Ids must be UUIDs, in the schema and again in the read.
    • Codes become numbers before they reach a filter.
    • Search text is capped at 200 characters and 8 words. Each word goes in its own parameterized .ilike() as %word%, with \, % and _ escaped. * is dropped, because PostgREST reads it as a wildcard.
    • No filter string is built from the model's text. There is no or() with text.
  • Untrusted text. Citation titles still come from list_citations, which stays an untrusted read, as before.
  • No row counts. Nothing shown to users changed. The notes are for the model only.

How to test

  1. Open Action Center › Signals on a brand with more than 50 signals, with Chat in the side panel. Ask about an old, resolved one: "S-07 nedir".
    • Chat runs one Signals read and names the signal by its title and S-07, with its status.
  2. Ask "S-999 nedir".
    • Chat says this brand has no signal S-999 and that codes are per brand. It lists no other signals.
  3. On Prompts › Tracked with more than 50 prompts, ask about an old or paused prompt by a few of its words: "İçinde 'skip a month' geçen prompt nasıl gidiyor?"
    • Chat finds the prompt by its words, then reads its performance by its id.
    • If it has no answers in the period, Chat says so and says it is tracked.
  4. On Visibility › Citations, ask about a domain that is not on the first page: "decafdigest.example kaç kez kaynak gösterildi?". Try it again as "https://www.decafdigest.example/".
    • Chat gives the count over your period, the top pages and the prompts that cite it.
  5. Ask about a page address, with and without www. and a trailing slash: "https://example.com/blog/x/ sayfası kaynak gösterildi mi?". Then ask about a page that isn't cited.
    • Chat says it isn't cited in that period, and whether its domain is.
  6. On Content, ask "'storage guide' fırsatı ne durumda?" about an archived opportunity, and "'gift boxes' taslağı nerede?" about an archived draft.
    • Chat finds both and says the draft is archived.
  7. On Reports, ask about an older report by its title: "'Competitor benchmark' raporu ne zaman hazırlandı?"
  8. Optional, real model: cd web && SKILL_EVAL=signal-by-code,citation-by-domain yarn vitest run src/lib/chat/agent-eval.test.ts

Checks

  • npx tsc --noEmit -p .: passes.
  • Vitest on src/lib/chat, src/lib/mcp, src/lib/action-center, src/lib/signals, src/components/chat, src/lib/help, src/i18n, use-server-exports and subscription-guard-coverage: 344 files, 4,971 tests pass (1 skipped).
  • Also passing: the other 62 test files that use the changed modules or the fake Supabase (src/app/api/mcp, src/app/api/chat, src/lib/actions), with 457 tests.
  • New tests:
    • lib/mcp/lookups.test.ts, 31 tests on the fake Supabase: every form of a code; ids in any case; words in any order and case; % and _ read as text, and * never sent; domain against URL normalization; an ambiguous code returning both signals; a match past the first page found; a search paging past 50; not_found and its note; another brand or workspace never read; a brand that isn't shared reading nothing but the brand.
    • Registry: each lookup calls its read with only its own inputs, the schema limits, and the descriptions.
    • scope.test.ts: limit never cuts any lookup.
    • The scripted harness: signal-by-code passes a good answer and fails a "closest" one; citation-by-domain passes.
  • ESLint and Prettier are clean on the changed TypeScript files.
  • git merge-tree with main is clean. fix(chat): read over the page's dates and filters in the side panel #2459 (the page's dates and filters) also edits scope.ts; whichever lands second gets main merged in.
  • No migration, new dependency, lockfile change or new server action.
  • No index is needed. Each lookup filters by brand first.
    • Signal codes use signals_brand_no_key (from 00534's runbook) when it is built. Until then, the existing (brand_id, detected_at) index keeps the lookup to one brand's signals.
    • Prompt, opportunity, draft and report searches scan one brand's rows.

Not in this PR:

Follow-ups for the owner to decide (this PR doesn't depend on them)

  1. MCP. The MCP tools (list_signals, list_prompts, get_prompt_performance, list_citations, list_content_opportunities, list_content_drafts, list_reports) could take the same lookup inputs, as fix(mcp): look up an action by its code, and never answer with "the closest" ones #2452 did for actions. The shared functions are ready. This PR keeps them Chat-only.
  2. Exact words only. A search matches each word as written: "subscriptions" doesn't find "subscription". The tool description and the note tell Chat to try fewer or shorter words before it says "not found". Stemmed or fuzzy matching (Postgres full-text search or trigram) would need an index and a migration.

Generated by Claude Code

…report by what the person names

Chat answered "what is X?" from the first page of a list, so an item past it
read as missing. Each list read now takes a direct lookup:

- list_signals: codes ("S-07", "S 7", "s7", "#7", "7") and ids, every status and date
- list_prompts: search (words of the text, any order and case) and ids, every topic and state
- get_prompt_performance: prompt_ids, whatever the topic; tracked prompts with no answers in no_results
- list_citations: domains (with subdomains, top pages and citing prompts) and urls (scheme, www and trailing slash aside), every source type
- list_content_opportunities, list_drafts, get_reports: search (title words) and ids, archived included

Each returns what it found and not_found, with a note to say "not found"
plainly and never offer the closest. A lookup is never cut by limit
(NAMED_ROWS). Every read keeps brandAllowed, the brand_id filter and the
organization join; words reach the filter only as escaped ilike patterns.
Shared helpers in lib/mcp/lookup.ts; MCP tool shapes unchanged.

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.

LGTM. Minor: with ids and a search in one call, scope.ts (~447) caps rows at 50 while lookUpBy returns up to 10 id rows + a 50-row search page, so the search's next_offset skips the rows cut; list_citations domain rows can also exceed the cap with no way to page.

@gkhngyk
gkhngyk merged commit 7de32b3 into ansvisor:main Nov 16, 2026
13 checks passed
gkhngyk pushed a commit that referenced this pull request Nov 16, 2026
…eport by what the person names

An MCP client asked "what is S-07?", "how does the 'skip a month' prompt
do?" or "is example.com cited?" could only page a list (50 signals, 100
prompts, the top 50 domains, ...), so an item past the page looked missing
and the client could answer with the closest ones instead. Chat got direct
lookups in #2460; the MCP tools now read them the same way, through Chat's
reads: the same query, rows and note, no second query.

- list_signals: `codes` ("S-07", "S 7", "s7", "#7", "7") and `ids`, every
  status and date (lookUpSignalsFor). status, kind and limit don't apply.
- list_prompts: `search` (words of the text, any order and case) and `ids`,
  every topic and state (lookUpPromptsFor). topic_id, is_active and limit
  don't apply.
- get_prompt_performance: `prompt_ids`, whatever the topic, never cut by
  limit; tracked prompts with no answers in no_results
  (lookUpPromptPerformanceFor). topic_id, sort_by, order and limit don't
  apply.
- list_citations: `domains` (with subdomains, top pages and citing
  prompts) and `urls` (scheme, www and trailing slash aside), every source
  type (lookUpCitationsFor). source_filter and limit don't apply.
- list_content_opportunities, list_content_drafts, list_reports: `search`
  (title words) and `ids`, archived included where the list has it
  (lookUpOpportunitiesFor, lookUpDraftsFor, lookUpReportsFor). Their
  filters and limit don't apply.
- A search pages 50 at a time: `offset` on those four tools takes the
  result's next_offset and applies to a search only.
- Each returns { note, <rows>, not_found } in place of the list. Without a
  lookup input every tool reads exactly as before.
- Scope as before: brandAllowed (organization and brand access), the
  brand_id filter and an inner join on the brand's organization. An id or
  code of another brand or workspace is not found; ids are checked as
  UUIDs, codes become numbers, words reach a filter only as escaped ilike
  patterns (#2460).
- Descriptions: read what the person names first; which list inputs a
  lookup ignores; "not found" plainly, never the closest; never say an
  item doesn't exist because a page didn't show it. get_report,
  get_content_draft and get_content_opportunity say where a named one's
  id comes from.
- The REST mirrors (/api/mcp/*) are unchanged.
- Docs: the MCP guide's rows. Help: one example line in the MCP article's
  "What you can ask". The coach skill names the new inputs where it lists
  each tool's inputs, and a pitfall for "missing because a list didn't
  show it".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 16, 2026
The page's search (from its view) now fills `search` on the reads of the
page's own list that take one, as ansvisor#2459 fills its list filters: as
ansvisor#2460's capped lookup words, never over the model's own search, ids or
codes, not for another brand's page, and dropped when the question asks
for all. The read's scope names it (from_page.search) and the writer
says it. In the models' page_view block every row and ref stay, each
label cut to 80 characters; the stored view's cap is now a safety net
that every 100-row view fits under.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 16, 2026
Brings in ansvisor#2449, ansvisor#2450, ansvisor#2457, ansvisor#2460 and ansvisor#2461 (Chat and MCP lookups by
code, id, words, domain or address; never give up on data it could read).

Conflicts, resolved as the union of both sides:
- agent-eval.ts: the scenario keeps askedSignals/askedDomains and checks;
  QUESTION_SCENARIOS holds action-by-code, signal-by-code,
  citation-by-domain and the three ai-revenue questions; every run gets
  "never gives up on data it could read" and then its scenario's checks.
- agent-eval-data.ts: EvalWorkspace is default, action_backlog,
  long_lists and conversions_off; both sides' imports.
- agent-eval.test.ts: both sides' imports.
- brand-scope.test.ts: the AI-revenue reads and the lookups.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
gkhngyk pushed a commit that referenced this pull request Nov 18, 2026
Stacks step 7 on both open Chat PRs (#2459 page period and filters,
#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
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