Skip to content

fix(chat): look up an action by its code, and never answer with "the closest" ones - #2445

Merged
gkhngyk merged 15 commits into
ansvisor:mainfrom
cihan-gyk:fix/chat-action-by-code
Nov 16, 2026
Merged

gkhngyk merged 15 commits into
ansvisor:mainfrom
cihan-gyk:fix/chat-action-by-code

Conversation

@cihan-gyk

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

Copy link
Copy Markdown
Collaborator

Stacked on #2423 (→ #2422 → #2420). Until they land, this diff also shows their commits. This PR's own changes are commits c4cdf4b11 and c2a6654df.

Merged with main (16 Nov): the updated #2423 is merged in, and with it main at d6ed27377. The only conflict was help/chat/chat.md's last_checked line; the later date was kept and the keywords are the union of both sides. "Ask about your actions" is 146 words. approved-articles.json was regenerated. Typecheck is clean, and 328 test files (4,732 tests) pass. The branch merges cleanly on main.

Owner decisions (16 Nov): approved as proposed; see "Owner decisions" below. Commit c2a6654df adds the title rule. Chat's tests pass (188 files, 2,831 tests), and the branch still merges cleanly on main.

Summary

The product owner hit this in production today:

  • The Actions list was open with Chat beside it. The list showed 55 actions, and the first row was Expand visibility on 1 platform (AC-481).
  • They asked "AC 481 ve AC-63 nedir" ("what are AC 481 and AC-63").
  • Chat read the list several times, then answered that there were no actions AC-481 or AC-63 in the data it read. It then listed "the closest" actions it could see (AC-62…).
  • That answer was wrong twice. Both actions exist, and Chat guessed instead of saying what it didn't know.

Cause

  • Chat had no way to read an action by its code.
  • list_action_center_tasks returns a page ranked by Priority Score, and get_action takes only an id.
  • AC-481 had a low score, so it wasn't on the first page of 50. Chat took "not on this page" to mean "doesn't exist".
  • No rule told Chat not to offer other actions in place of the one asked about.

Changes

  • Look up by code. list_action_center_tasks takes a new codes input: up to 10 codes in one read.
    • Accepts "AC-481", "AC 481", "ac481", "feat(traffic): add 7 / 30 / 90-day time-range picker #481", "481", "AC-07" and "AC-7".
    • Matching uses the stack's own rule (matchesActionCode, as in the Actions search and actions/code/AC-n). A code matches the brand's number (brand_no) or the old global number (action_no).
    • Each row says whether the code matched its code or its previous code, and gives previous_ref when the action has one.
    • When a code matches two actions, both come back and the note says "never pick one".
    • Codes with no match go in not_found. The note tells Chat to say so plainly and that codes are per brand.
    • Reads every status, Completed and Dismissed included. Deleted actions are never returned.
    • When codes is set, status, category, sort, limit and offset don't apply.
  • Why a codes input on the list, not a code on get_action.
    • The list already scopes by brand_id and returns compact rows, so it can return two actions for one ambiguous code.
    • get_action finds its brand from the row the id names. With a code there's no id to look up, so it would need a new brand input and a new scope path.
    • get_action also returns the full action with its tasks, signals and events. That's too heavy for "what is AC-481".
    • The input is a list, so "AC 481 and AC-63" takes one read.
  • Scoping. Every read checks brandAllowed (the run's organization and the person's brand access) and an inner join on brands.organization_id, and filters by brand_id.
    • A code from another brand or workspace comes back as not found.
    • Only the parsed numbers reach the filter, never the model's text.
  • No cut rows. If the model sends a limit with codes, the row cap ignores it (scope.ts NAMED_ROWS). This keeps both actions of an ambiguous pair.
  • No guessing. New rules in the list_action_center_tasks and get_action descriptions and in ACTION_NAME_RULES:
    • Read the code first.
    • If a code isn't found, say so and say codes are per brand. Never offer "the closest" actions instead.
    • Never say an action doesn't exist because a page didn't show it.
  • The app's title in any language (c2a6654df). A new line in ACTION_NAME_RULES: write the title exactly as the app shows it, even in a Turkish answer, so the person finds the same row in the list. A short explanation in the person's language may follow it, but a translated title never replaces it. The eval already checked for the app's title; now the rules say it too.
  • Shared logic. The matching and the note text live in a plain module, lib/action-center/action-codes.ts. Chat's read and the eval use the same code.
  • Help. One line in Chat › "Ask about your actions". approved-articles.json is regenerated.
  • Eval. New scenario action-by-code: "AC 481 ve AC-63 nedir" on a brand with 55 open actions.
    • AC-63 is on the first page. AC-481 is past it.
    • It must read both by code and name both by title and code. It must not name other actions in their place.
    • Only this scenario uses the 55-action workspace. Every other scenario reads the same 4 actions as before.

Not changed: page context. The Actions page already gives Chat's panel its first 5 visible rows (id and title, no code), but only for the @ menu. A row reaches Chat only when the person picks it with @. Sending the visible rows with every message would mean changing the request that chat-thread-view.tsx posts, which this PR leaves alone. With the code lookup, "AC-481" takes one cheap read.

Owner decisions (approved 16 Nov)

  • Titles in a Turkish answer: Chat writes the title as the app shows it, and may add a short Turkish explanation after it. Done in c2a6654df, with a test.
  • MCP server: the same code lookup for the MCP server's list_actions follows in its own PR, stacked on this one.
  • Visible rows in Chat's context: not in this PR. It belongs to the later step of the Chat page-data plan that sends the page's view with each message.

How to test

  1. Open Action Center › Actions on a brand with more than 50 open actions, with Chat in the side panel.
  2. Ask about a low-priority action and a high-priority one by code, e.g. "AC 481 ve AC-63 nedir".
    • Chat runs one Action Center read and names both by title and code.
    • The titles read as they do in the list, even though the answer is in Turkish.
    • It says each code is the action's own code.
  3. Ask about a code the brand doesn't have, e.g. "what is AC-99999?".
    • Chat says this brand has no action with that code and that codes are per brand.
    • It lists no other actions.
  4. Ask about a Completed or Dismissed action by its code. Chat finds it and gives its status.
  5. On a numbered brand, ask by an action's old number ("Previously AC-648"). Chat names the action and says that is its previous code.
  6. Optional, real model: cd web && SKILL_EVAL=action-by-code yarn vitest run src/lib/chat/agent-eval.test.ts

Checks

🤖 Generated with Claude Code

https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9

…numbers

Brings in actionCode and matchesActionCode (action-ref.ts), which this
branch extends with each brand's own numbers.

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

The owner asked Chat "what are the most important actions?" on a brand
with 53 actions. Chat read 10 (its row default for the Action Center
read), judged only those, and named actions by bare numbers ("AC-251,
249, 250") that the Actions list doesn't show.

- list_action_center_tasks reads a page from a new Chat read,
  listActionsPageFor: every open action (New, In progress, On hold) is
  counted and ranked on the server by Priority Score, 50 to a page with
  the total, next_offset and a "Showing N of M" note; offset pages on.
  Rows are compact: ref, title (as the Actions list words it), category,
  status, score and level, stale_since, due, assignee, tasks, updated.
  Still scoped to the caller's organization and brand access.
- The page is never probed for one more row (it counts itself) and gets
  a 20,000-character result share, so all 50 rows reach the model.
- The writer's rules and the tool's description require a title with
  the linked number ("**Title** (AC-12)"), never a number alone, a run of
  bare numbers or a range, and saying "the top 50 of 53" when a read
  didn't cover them all. The 30-day plan skill names actions the same way.
- Every action code Chat shows or links comes from actionRef (the
  action-ref helper), examples included, so a change of numbering only
  changes its input.
- The agents' eval flags an answer with a bare "AC-251" (must check).
- Help: "Ask about your actions" in Use Chat.

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

Brings in actionRef and Chat's own list of actions, which this branch
names by each brand's code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
The owner asked for each brand's actions to be numbered from 1 instead of
the global action_no, which hinted at other customers' volume and read as
unrelated to the brand's list ("AC-648" beside "AC-251"). Signals get their
own brand numbers too (schema and backfill here; their codes come next).

- 00534: actions.brand_no and signals.brand_no, a server-only counter per
  brand and kind (brand_number_counters), and a before-insert trigger that
  gives the next number under the counter's row lock. Schema and functions
  only; nothing existing is numbered in the migration. The unique indexes
  are built after the backfill (only on an empty table in the migration).
- backfill_brand_numbers numbers one brand's rows per call by creation
  order, idempotent and resumable, run by src/scripts/backfill-brand-numbers.js
  (--dry-run, --kind, --brand, --from-brand). Until a brand is numbered its
  new rows wait too, so a brand never mixes old and new codes.
- One formatter: actionCode / actionCodeNo (web action-ref.ts, server
  action-code.js): "AC-03", padded to two digits, falling back to the old
  "AC-648" while brand_no is null. Used by the Action Center (list, drawer,
  exports, History, review, run agent), Chat (tool results, cards, Undo,
  agent run scope and prompt, Inbox suggestions, its list of actions), the
  MCP tools (a `code` beside action_no), Reports, Content, Products,
  Prompts and the send toasts.
- Old links keep their meaning: actions/AC-n still opens the old global
  number, in the person's workspace. A code in Chat's text links to
  actions/code/AC-n, which opens the one action with that code or old
  number and otherwise the list searched for it, never a guess. Other new
  links point by id.
- The drawer shows "Previously AC-648" beside the new code, and the list
  search matches the old number as well as the new code.
- Help: Action Center actions (Action codes) and Chat.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Stacked on feature/action-brand-numbers, whose 00534 gives signals their
brand_no and whose backfill numbers the ones already there.

- One formatter, signalCode: "S-03", padded to two digits; none until the
  signal's brand is numbered (signals had no code before, so there is no
  old one to keep).
- The Signals list shows the code beside each title, small and muted, and
  its search finds a signal by "S-12", "s12" or "12". The signal's drawer
  shows it under its title.
- The MCP tools (and Chat, which reads signals through them) return a
  `code` with each signal. Links to a signal still point by id.
- Help: Action Center signals.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Conflicts:
- help/chat/chat.md: main's keywords (Settings in the chat menu) plus this
  branch's (most important actions, top actions, action title); main's new
  text and this branch's "Ask about your actions" section both kept.
- action-ref.test.ts: both sets of tests (actionRef from this branch;
  actionCode, searchedActionNo and matchesActionCode from main's ansvisor#2418).
  actionCode now builds its code with actionRef, so one formatter remains.
- web/src/lib/chat/skills/library.json: regenerated from skills/*/SKILL.md
  (yarn vitest run -u src/lib/chat/skills).
- approved-articles.json: regenerated (yarn vitest run -u src/lib/help).

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

Brings the updated ansvisor#2420, which carries upstream main.

Conflicts:
- action-ref.ts and action-ref.test.ts: this branch's versions (actionCode
  from the brand's number via actionCodeNo, with every test from both
  branches and ansvisor#2418).
- server/src/lib/action-center/from-chat.js: main's Ads source (`source`
  param, verifiedPlace without a conversation for Ads) and this branch's
  `brandNo` in the answer and actionCode in the duplicate message.
- from-chat.test.js: main's Ads test now expects the answer's brandNo
  for the same key again (this branch's answer shape).
- server/src/lib/demo/coverage.js: both SKIPPED entries,
  brand_number_counters (00534) and mentions_terms_packed_ready (00540).
- supabase/schema.sql: regenerated (bash supabase/build-schema.sh); 00534
  sits before 00535-00540 by file order.
- library.json and approved-articles.json: regenerated, unchanged from
  the automatic merge.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Earn (from main) sends through from-chat.js and answered only the old
global number, so its card read "Open AC-1300" while the Action Center
shows the brand's code. It now also answers brandNo (sent, the same key
again, the duplicate, and the covering action the drawer reads), and the
card shows actionCodeNo: "Open AC-03", or the old number until the brand
is numbered.

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

Brings the updated ansvisor#2422 (with ansvisor#2420 and upstream main). No conflicts;
schema.sql, library.json and approved-articles.json regenerated and
unchanged.

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

Chat could not answer "what is AC-481?" when AC-481 was on screen: it had no
way to read an action by its code, only a page of the list ranked by priority
(list_action_center_tasks) or one action by its id (get_action). AC-481 was
past the first page of 50, so Chat said it didn't exist and listed others.

- list_action_center_tasks takes `codes`: every code of a message in one read
  ("AC-481", "AC 481", "ac481", "ansvisor#481", "481", "AC-07"). It matches as the
  Actions list's search and the actions/code route do (matchesActionCode):
  the brand's number or the old global number. Each row says whether the code
  is its code or its previous code; a code that names two actions returns
  both; codes no action has come back in not_found. Every status, never a
  deleted action, this brand of the caller's organization only (brandAllowed
  and an inner join on brands.organization_id).
- A limit sent with codes no longer cuts the rows (scope.ts NAMED_ROWS).
- The tool descriptions and ACTION_NAME_RULES: read the code first; a code not
  found is "not found in this brand, codes are per brand", never "the
  closest"; never "doesn't exist" because a page didn't show it.
- Help: "Ask about your actions" says you can ask by code.
- Eval: "AC 481 ve AC-63 nedir" on a brand with 55 open actions, AC-481 past
  the first page; it must read both by code, name both by title and code, and
  name no other action in their place.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Nov 16, 2026
gkhngyk
gkhngyk previously approved these changes Nov 16, 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.

LGTM.

Brings in ansvisor#2446 (no row counts), ansvisor#2442, ansvisor#2447, ansvisor#2441 and ansvisor#2377.

Conflicts:
- approved-articles.json: regenerated (yarn vitest run -u src/lib/help).

help/chat/chat.md merged cleanly: main's "Watch the work" text (no row
counts) and this branch's action-title line are both kept (132 words);
updated stays 2026-11-16, last_checked 2026-11-15.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Takes the updated ansvisor#2420, which now includes upstream main (ansvisor#2446 no row
counts, ansvisor#2442, ansvisor#2447, ansvisor#2441, ansvisor#2377).

Conflicts:
- approved-articles.json: regenerated (yarn vitest run -u src/lib/help).

help/chat/chat.md merged cleanly: updated 2026-11-16, last_checked
2026-11-15.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Takes the updated ansvisor#2422, which now includes ansvisor#2420 and upstream main
(ansvisor#2446 no row counts, ansvisor#2442, ansvisor#2447, ansvisor#2441, ansvisor#2377). No conflicts;
approved-articles.json checked with yarn vitest run -u src/lib/help
(no change).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
Takes the updated ansvisor#2423, which now includes ansvisor#2422, ansvisor#2420 and upstream
main (ansvisor#2446 no row counts, ansvisor#2442, ansvisor#2447, ansvisor#2441, ansvisor#2377).

Conflicts:
- help/chat/chat.md: front matter only; took the later last_checked
  (2026-11-16). Both sides' text is kept: main's "Watch the work" (no
  row counts, 148 words) and this branch's look-up-by-code paragraph in
  "Ask about your actions" (146 words).
- approved-articles.json: regenerated (yarn vitest run -u src/lib/help).

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

The owner's decision on 2026-11-16: in a Turkish answer, Chat writes the
action's title as the app shows it, so the person finds the same row in
the list, and may add a short explanation in Turkish after it. It never
puts a translated title in its place.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
gkhngyk
gkhngyk previously approved these changes Nov 16, 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.

LGTM.

@cihan-gyk
cihan-gyk dismissed gkhngyk’s stale review November 16, 2026 16:38

The merge-base changed after approval.

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

@gkhngyk
gkhngyk merged commit 3fa2811 into ansvisor:main Nov 16, 2026
12 checks passed
gkhngyk pushed a commit that referenced this pull request Nov 16, 2026
…losest" ones

An MCP client asked "what is AC-481?" could only page list_actions by
priority (50 rows by default, 200 at most), so an action past the page
looked missing and the client could answer with other actions instead.
Chat got the same lookup in #2445; MCP now reads codes the same way.

- list_actions takes `codes` (up to 10: "AC-481", "AC 481", "ac481",
  "#481", "481", "AC-07"). With codes it answers from Chat's read
  (listActionsByCodeFor): the same query, rows and note, no second query.
  It matches the brand's number or the old global number, says per row
  whether that is its code or its previous code (previous_ref), returns
  both actions for a code that names two, reads every status but never a
  deleted action, and lists codes the brand doesn't have in not_found
  with "codes are per brand". status, category, sort and limit don't
  apply beside codes.
- Scope as before: brandAllowed (organization and brand access), the
  brand_id filter and an inner join on the brand's organization. A code
  of another brand or workspace is not found; only parsed numbers reach
  the filter.
- The list_actions description: read the code first; a code not found
  means this brand has no action with it; never offer the closest
  actions; never say an action doesn't exist because a page didn't show
  it. get_action's action_id says where a code's id comes from.
- The REST mirror (/api/mcp/actions) is unchanged.
- Docs: the MCP guide's list_actions row. Help: one example line in the
  MCP article's "What you can ask".

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
…versions

Brings in upstream main through the updated fix/chat-list-paging (ansvisor#2445's
action-by-code eval among it). Conflicts resolved as a union:
- agent-eval: one QUESTION_SCENARIOS with action-by-code and the three
  ai-revenue questions, all in SKILL_SCENARIOS; the eval's world becomes
  main's workspace (EvalWorkspace gains 'conversions_off').
- agent-eval-data: both data sets, one evalReadResult(tool, input, workspace).
- agent-eval tests: both test blocks; the /name check covers library skills.
- brand-scope.test.ts: both sets of rows.
- approved-articles.json 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

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.

3 participants