Skip to content

feat(settings): add Settings › Chat with the tool policy and run modes - #1782

Merged
gkhngyk merged 18 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-settings-policy
Oct 29, 2026
Merged

gkhngyk merged 18 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-settings-policy

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Chat P0, part B3 (1 of 2): Settings › Chat, the tool policy and run modes (CHAT-20 §4, §5 steps 4 and 6, §8b; CHAT-07 v2). Behind CHAT_AGENTIC. The Activity log page (part 2) follows once A4's log lands.

Settings › Chat (new last section; every role sees it when the flag is on)

  • Plan card. Non-admins also get a read-only bar ("Read-only · only Admins change the Chat tool policy") with Ask your Admin.
  • Run modes
    • "Full auto for members" and "Don't ask again" switches, both Growth and up, with "Needs Growth · you're on X" when locked.
    • Two fixed rules: "Full auto over MCP: Off in v1" and "Untrusted content: every WRITE shows a card".
  • Who decides: A3's six steps, in plain words.
  • Tool policy, by area
    • Each row shows its label (never an internal or tool name), a class badge, the policy in effect, and an "In Full auto" column. Below Growth, writes always show a card.
    • Admins pick from the row's own choices. Choices the plan locks are disabled with "Needs Growth" (policyChoiceLock). EXECUTE and always-card rows never offer Always.
    • "default" returns to the row's default, and "Reset to defaults" resets them all.
  • What Managers may take live: the four go-live groups (manager_execute_off).
  • Your auto changes: the person's own "Don't ask again" choices, each with Remove. Open in a read-only workspace too, since removing only takes access back.

lib/actions/chat-settings.ts: getChatSettingsView, setToolPolicy, resetToolPolicies, setChatSwitch, setManagerExecute, removeAutoChoice.

Left out: the mockup's "Default run mode" picker. The mode is per person in the composer, and there is no workspace-level column for it.

Related issue

None (Chat P0, Drive › Chat).

Stacked on #1781 (Chat B2). The diff against it holds only this change.

Type of change

  • feat — New feature (behind CHAT_AGENTIC)

How to test

With CHAT_AGENTIC=true:

  1. As an Admin on Growth, Settings › Chat: set a write tool to Always, then reset it. Turn "Full auto for members" off. Switch a Manager go-live group off.
  2. On Starter: Always and "Ask (auto in Full auto)" are disabled with "Needs Growth".
  3. As a Manager: everything is read-only, with Ask your Admin; Your auto changes lists their own choices, each with Remove.
  4. Tests: cd web && yarn vitest run src/lib/actions/chat-settings.test.ts src/components/chat-access/ src/lib/chat/ and the settings tests (304 in all).

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Web: related tests (304) pass; typecheck, eslint and prettier clean; embed guard passes
  • Browser check: in the next batched local test
  • Full suite and build: CI

🤖 Generated with Claude Code

cihan-gyk and others added 14 commits October 29, 2026 12:51
…eams

The first part of the new Chat (Drive Chat specs, P0 part A1). Nothing
uses it yet, and with the flag off nothing changes: today's
/dashboard/agent keeps its own tables and code.

- CHAT_AGENTIC turns the new Chat on, read on the server at request time
  (web lib/chat/flag.ts, server lib/chat-flag.js), so the same build runs
  with it on or off. CHAT_AGENTIC_ORGS narrows it to listed workspaces.
  Off unless "true", so a self-hosted install sees nothing new.
- 00400 adds chat_conversations, chat_messages, chat_runs, chat_run_steps
  and chat_user_prefs. A chat is private to its author (CHAT-20 §16c):
  only they read it, admins included, and messages, runs and steps follow
  their chat's policies, so sharing with the team can be one more policy
  later. The server writes them; a person writes only their own Chat
  settings (menu, where pages open, focus areas, model, effort), kept on
  the server so they follow them across devices (DECISIONS N3).
- A step's tool and debug record stay with the server: members are
  granted every other column. Step kinds live in chat_step_kinds, so a
  new kind is an inserted row, not a rewritten check.
- web lib/chat/events.ts types every event a run streams (CHAT-00, with
  room for CHAT-25's), names steps by their place in the run, and
  chatEvent() refuses a tool's name, a debug record, an id or a dollar
  amount before an event reaches the browser.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A deleted brand now leaves its chats with their author, as about the
workspace, the way agent_conversations does (00009), rather than
deleting a person's private history with it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nd add app links and the step converter

A uuid in an event's text (a page path, a link the person pasted) no
longer stops the run: chatEvent() shows it as "…". The answer's own text
is still sent as it is, and forbidden keys still throw.

- Forbidden keys are patterns now, in the type (ChatForbiddenKey) and at
  run time alike: any tool key (toolInput, toolResult…) and params; debug,
  tokens and latency; money (usd, price, amount, spend, cost, cents). A
  cost multiplier, a percent and a tooltip still pass.
- Deliverable cards and page.opened get an optional href for their Open
  (CHAT-06, CHAT-25): a path under /dashboard/ only, with no host, query,
  fragment, "//" or ".." part, kept whole by the masking.
- steps.ts holds chat_run_steps' member columns (every one but tool and
  debug_json, checked against the migration's grant) and toClientStep(),
  the one way a stored step reaches the browser: built field by field, so
  a row read with the service role gives up its tool and debug record too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ch new message

- chat_user_prefs.focus holds at most the 8 areas, each once
  (chat_text_array_distinct: a check can't hold a subquery); the model
  column says the server checks it against the catalog before a run.
- A trigger on chat_messages sets its chat's last_message_at (security
  definer, so it reaches the chat whoever writes the message; it never
  moves back).
- The step kinds comment no longer says a kind can be removed: steps
  keep theirs, and there is no index on kind to find out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y be read

A change applies when the app starts again: a restart when self-hosted,
a new deployment on Vercel. The web flag is for dynamic server code only;
a page rendered at build time would keep the build's value.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Chat P0, A2 (CHAT-20 §9): one place that says, for each tool Chat may
call, what it does, who may call it, on which plan and how a call is
approved. The approval flow (A3), the model's tool list (C3), Settings
› Chat and Roles & access (B1, B3) read these rows instead of keeping
their own lists. Nothing calls the registry yet; it sits behind no flag
because it changes nothing on its own.

- lib/chat/registry/define-tool.ts: defineTool and the row fields:
  internal name, class (read / write / execute), min role (TeamRole),
  plan gate (PlanId, plan Feature, or the plan's platforms), default
  and maximum policy, flags (always_card, cost_card, admin may turn a
  read off), cost class, label key, app route, the MCP tool doing the
  same work, its CHAT-20 row and a fixture. defineTool keeps the hard
  rules as rows are defined: a read runs, an execute is always a click,
  and an always_card write never goes past Ask.
- read-tools.ts: 27 READ rows, each calling the MCP data function the
  MCP tool of the same work calls (imported, not changed), so Chat, the
  MCP Server and today's agent read the same numbers.
- write-tools.ts: create_draft, update_opportunity and
  dismiss_prompt_suggestion, calling the server actions the app's own
  buttons call (createContentDraft, updateOpportunityStatus,
  dismissSuggestion), as the signed-in member.
- tools.ts: CHAT_TOOLS, chatTool(name) and registryCatalog(), the rows
  as plain JSON-safe data for the Settings pages. No generated file:
  every reader is in web/ and can call it.
- messages/en.json: chatTools.{key}.name (Settings) and .step (CHAT-03
  step title) per row. The keys don't spell the tool names, since every
  page gets all of en.json and a tool name never reaches a customer.

Tests: every row has its two labels and a fixture its schema takes;
names and labels are unique; routes are pages of the app; each row runs
its own function with the fixture (reads with the member's context,
writes through the app's actions, with the same arguments the pages
pass); an execute is always a click; the catalog is plain data. Against
the MCP Server: every named MCP tool is registered, reads map to reads
and writes to writes, and readOnlyHint agrees wherever a tool states it
(3 tools on main; all once ansvisor#1732 merges).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… mentions reads

- registryCatalog() is now what a browser may get: each row goes by its
  label key (`key`), without its internal name, its MCP tool name or its
  spec row. A tool's name never reaches a customer (CHAT-03 item 22).
  The names stay on the server in CHAT_TOOLS, where the approval checks,
  the run steps' `tool` column and the MCP parity read them, so no
  second, server-side catalog is needed. chatToolByKey() leads a key a
  page sends back (a policy change, say) to its row.
- get_mention, get_mention_competitors and get_mentions_perception: READ
  rows for the Mentions area's reads today's agent (/dashboard/agent)
  has, so Chat answers what it does. Not in CHAT-20 §9 yet; mapped to
  the MCP tools of the same names, with their en.json labels.

Tests: the catalog has no tool name in any value and passes the event
rule chatEvent() applies (clientEventProblems); every key leads back to
its row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Chat's approval precedence (CHAT-20 §5): before every tool call the
server decides whether the person may run it now, and how: it runs, it
waits on an approval card, it waits on their click, or it is refused
with a reason. Nothing reads it yet; the engine (C3) calls it, behind
CHAT_AGENTIC.

- lib/chat/precedence.ts: decideToolCall and allowedTools, pure. Most
  restrictive first: brand scope; a read-only workspace (402), the demo
  brand and a Never policy refuse; an EXECUTE is at most a click and an
  always_card WRITE at most a card in every mode; then the plan gate,
  the live role, the Admin's policy and switches, the run mode, the
  person's "Don't ask again" and taint. Self-hosted has no plan gates;
  MCP gets no Full auto and no ticks (v1).
- lib/chat/denial.ts: the out-of-permission answer's four parts (what
  was asked, the rule and where it is set, who can by role, what to do
  next), from en.json, naming nothing outside the person's scope.
- lib/chat/plan-facts.ts: the plan's features, limits, trial, Full auto
  and step up, read from config/plans.ts through the organization row.
- lib/chat/precedence-context.ts: reads the live role, the plan, the
  workspace's Chat settings and the person's ticks fresh for every call.
  The demo flag reads false until the demo chain (ansvisor#1734) merges.
- 00401: chat_settings (tool policy by label key, Full auto for
  non-Admins, "Don't ask again", go-live groups off for Managers;
  members read, the server writes) and chat_auto_choices (a person's
  ticks, theirs to read, add and remove).
- Registry: every EXECUTE row names its go-live group, and saving a
  draft opens to every role (the 2026-10-29 role model: daily work open,
  settings, deleting, prompts and costly work for Admins and Managers).

Tests: a fixture table of cases plus every combination of role, plan,
mode, surface, policy, switches, tick, taint and demo (about 135,000
decisions), one test per §21 criterion it covers.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…Auto

Two decisions of 2026-10-29 for Chat's approval precedence:

- Roles: dismissing a prompt suggestion is open to Admins, Managers and
  Analysts, not Agency partners, as the server (ansvisor#1763), MCP (ansvisor#1762) and
  RLS have it. A registry row open to every role may now leave roles out
  (excludeRoles, never an Admin); the catalog carries it, and a denial's
  "who can" names the roles a row allows. Reviewing or dismissing a
  content opportunity stays with Admins and Managers.
- Always: a tool an Admin sets to Always runs without a card in Auto and
  Full auto, on any plan; Ask me first, MCP and untrusted content still
  show the card. CHAT-20 §5 step 5 ("Auto: each WRITE shows a card")
  now holds for tools not set to Always, so Always and "Ask (auto allowed
  in Full auto)" differ. EXECUTE and always_card rows still can't be
  Always.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
An Admin's Always runs a change without a card only where Full auto is
open: on Starter and in the trial it asks as Ask does, with the reason
full_auto_locked, so Always can't get past the plan difference.
policyChoiceLock tells Settings › Chat to show Always and "Ask (auto
allowed in Full auto)" locked there, for the same reason.

Always still doesn't need "Full auto for members": it is the Admin's own
choice for one tool, now said in the code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ate MCP by plan

Review of Chat's approval precedence:

- Every registry row says where a call's brand comes from (brandOf): the
  input's brand_id, or the brand of the row an object's id names
  (content_opportunities, prompt_suggestions, prompt_results,
  content_drafts, actions, site_audits). The server finds it in the
  person's workspace (resolveCallBrand checks brands.organization_id, so
  'all' never reaches another workspace's brand), and a brand-bound call
  whose brand isn't found is refused as brand_scope. A run about no
  brand yet still lists the brand-bound rows. The registry test checks
  every row that takes a brand or an id says so.
- A surface other than Chat, or one the code doesn't know, is the closed
  side: its writes always ask. A mode it doesn't know is Ask me first,
  and readPrecedenceContext checks both against CHAT_MODES and
  CHAT_SURFACES.
- MCP's plan gates (CHAT-20 §7, §17): nothing in the trial, reads only on
  Starter, writes and executes from Growth; allowedTools follows.
- decideOrDeny refuses a call without a context (no_access), with its
  denial words. DEFAULT_CHAT_SETTINGS is frozen. A note for the engine:
  an approval or a click decides again before it runs.
- A type test stops compiling once ansvisor#1734 adds brands.is_demo, so the
  demo flag gets wired then; another checks every lookup table has
  brand_id.
- Tests: Full auto off for members with ticks on, the tick rule against
  role, switch and surface both ways, a detected injection in every
  combination, and rows for an unknown surface and mode.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…t Chat can do

Chat P0 B1 (CHAT-20 §4a, §4b, §7a, §8), behind CHAT_AGENTIC: the
dashboard's layout reads the flag on the server and hands the browser
only the answer (ChatAgenticProvider).

- Settings › Roles & access (after Team, everyone reads it): the plan
  card, members with their role, brands, what they do through Chat and
  their last sign-in (e-mails for Admins only), what each role can do in
  the app and through Chat (CHAT-20 §4's matrix), and the Admin switches
  that change a role's rights (Managers going live, Full auto) with who
  set them. Admins change another member's role here as in Team; the
  rest of the team's management stays in Team.
- Settings › Account: a Your access card (role, brands, what Chat may do
  as you, the go-live groups an Admin switched off for Managers), linking
  to Roles & access and to "What Chat can do".
- The sidebar's profile row says the person's role.
- "What Chat can do" (/dashboard/chat/capabilities): the registry by
  area (registryCatalog), with how each change is approved, who may make
  it, and the plan a row needs, searchable and filtered by class. Only
  what the registry has is listed: a capability not in it isn't Chat's.
- Your plan (§7a): plan and price, what it includes and locks ("Needs
  Growth · you're on Starter"), AI usage as a percentage, and what the
  next plan adds from config/plans.ts (more platforms, Full auto, MCP
  writes, the AI limit's multiple; Claude tracking and a contract on
  Enterprise; "After your trial" on the trial). Upgrade for Admins only;
  no upsell or lock lines on Enterprise. No AI budget in dollars.

getChatAccess / listRoleMembers read the facts the approval precedence
checks (readPrecedenceContext), so a page and the server agree.
readMemberAuth gives a member's e-mail and last sign-in from one call.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Chat P0 B2 (CHAT-20 §4c, registry row 80a), behind CHAT_AGENTIC.

- chat_access_requests (00403): what was asked, the rule that refused it
  (role, plan, policy, workspace, brand, demo) and its words, the refused
  tool's label key, the chat it came from and any drafted change; pending
  until an Admin approves or dismisses it (the first to act decides) or
  the person withdraws it. The server writes every row; the requester
  reads their own, the workspace's Admins all of its.
- requestAccess: only by the person's click; Admins have no one to ask;
  at most 10 waiting per person, and the same ask (tool key and words,
  case and spacing aside) once a day; a brand or chat that isn't theirs
  is dropped. withdrawAccessRequest, decideAccessRequest (Admins only),
  listAccessRequests (an Admin's: the workspace's; anyone else's: their
  own; waiting first, then 30 days of decided ones) and the count.
- Registry row request_access ("Ask your Admin"): Ask fixed, always a
  card, every role, no brand; it calls requestAccess, as the buttons do.
- Ask your Admin buttons: on Roles & access' read-only bar and the Your
  access card (a role), and in Upgrade's place on the plan card for
  anyone but an Admin (the next plan), with the person's words editable.
- Roles & access › Requests: an Admin approves or dismisses, anyone else
  withdraws theirs; Settings shows an Admin the waiting count on Roles &
  access.
- recordAccessDenied (lib/chat/access-log.ts): one typed way in for
  every denial (who, the tool's label key and class, surface, the
  reason, brand unless out of scope, chat and run). The server's log
  for now; the Activity log (A4) fills it in without a caller changing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…live in Settings

Chat P0 B3, part 1 (CHAT-20 §5, §8b; mockup #g-policy), behind
CHAT_AGENTIC: a Settings › Chat section, listed last, for every role.

- Run modes: Full auto for members and "Don't ask again" (Growth and up,
  locked below with "Needs Growth · you're on …"); Full auto over MCP
  (off in v1) and untrusted content (every WRITE asks) as fixed rules.
- Who decides: the six steps of the approval precedence.
- Tool policy by area: every registry row by its label, never its name,
  its class, what applies, and what it does in Full auto. An Admin picks
  among what the row offers (policyChoices: a read only Never where it
  may be switched off, an EXECUTE only Never, a WRITE up to its maximum,
  so never Always on an always-card row), with what the plan locks
  (policyChoiceLock) disabled, or gives it back its default; Reset to
  defaults.
- What Managers may take live: the four go-live groups.
- Your auto changes: the person's own "Don't ask again" ticks, which
  anyone removes, on a read-only workspace too.

lib/actions/chat-settings.ts reads the facts the precedence decides on
and checks every change again on the server (an Admin, an active plan,
the row's choices, the plan's locks). Every action answers { ok } with a
typed failure and never throws (ansvisor#1777's pattern). The Activity log page
follows A4 (00402).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 29, 2026
…ver by throwing

As ansvisor#1777: getChatAccess, listRoleMembers, listAccessRequests and
countPendingAccessRequests answer { ok } with `chat_off` or `failed`
instead of throwing when the database doesn't answer, so a page never
shows React's hidden error message.

- Roles & access says it couldn't load, with Retry; the requests list
  says so on its own, apart from the rest of the page.
- "What Chat can do" is not found with Chat off, and says it couldn't
  load, with Retry, on a failed read.
- The Your access card and the Settings count stay hidden on a failure.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Added f1659d0: the Chat access reads from B1/B2 (getChatAccess, listRoleMembers, listAccessRequests, countPendingAccessRequests) now return { ok: false, failure } instead of throwing, as #1777 does. Roles & access shows "Couldn't load…" with Retry, and the Requests list reports its own failure apart from the rest of the page. /dashboard/chat/capabilities returns notFound when Chat is off, and a plain message with Retry when the read fails. The Your access card and the Settings badge hide on failure. Related tests: 308 pass; typecheck clean.

Each one checks the plan itself (asAdmin: ctx.plan.writable) so that a
read-only workspace gets read_only as a result rather than a throw;
removeAutoChoice only takes an auto-approval back. The read-only coverage
test failed without them.

@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 ae13dfe (take-1782). I merged main into the branch, which keeps main's merged copies of #1758, #1759, #1776, #1780 and #1781. On top of that I added one test fix.

What I changed

  • The read-only coverage test failed. subscription-guard-coverage.test.ts wants every server action that writes to call assertWritable() or to be listed in OPEN_ACTIONS with a reason.
    • setToolPolicy, resetToolPolicies, setChatSwitch and setManagerExecute write chat_settings, and removeAutoChoice deletes from chat_auto_choices. None of them did either.
    • The behaviour is right: asAdmin() checks ctx.plan.writable, the same check as assertWritable, and answers read_only as a result instead of throwing.
    • So I listed the five actions with that reason. removeAutoChoice is listed because it only takes an auto-approval back.
    • CI doesn't run the web tests, which is why this didn't show up.

What I checked

  • Flag off.
    • who() checks chatAgenticEnabled(org) before any chat_* read.
    • With the flag off, the Settings section is hidden and its address falls back to Account (ChatAgenticProvider, set by the server layout).
    • chatAgenticEnabled reads process.env on every request, and it isn't NEXT_PUBLIC_.
  • Server checks.
    • The policy, the switches and Manager live tools all need an Admin.
    • Full auto, Don't ask again and each policy's plan lock are checked again on the server (plan.fullAuto, policyChoiceLock, policyChoices).
    • Tool keys are checked against the registry.
    • The organization comes from the profile, never from the request.
  • Access pages (cc9e259de). Every caller of the four reworked actions now reads the failure from the result.
  • Builds and embeds.
    • No client component reaches the server Supabase client; plan-row.test.ts passes on the stack.
    • No new embeds. No migration.

Follow-ups (not blocking)

  1. setChatSwitch answers locked even when an Admin switches Full auto for members or Don't ask again off after a downgrade, so the stored true comes back on upgrade. Switching off should be allowed on any plan.
  2. saveSettings upserts the whole row from the values it read, so two Admins saving at the same moment can overwrite each other's change. A column-level update would avoid that.
  3. On a Growth trial, or in a read-only workspace, plan.fullAuto is false, and the switch then reads "Needs Growth · you're on Growth".
  4. PolicyPicker shows no option selected when a stored choice was clamped by effectivePolicy.
  5. Copy: "Off, everyone's ticks stop at once." → "When off, everyone's ticks stop at once." Also "Off, a Manager gets Ask your Admin instead." → "When off, a Manager is offered Ask your Admin instead."

Tests: chat-settings, chat-access and the read-only coverage test pass. The whole stack, merged on main, passes the full server and web suites, tsc, eslint, prettier and next build (see #1794).

@gkhngyk
gkhngyk merged commit 150e599 into ansvisor:main Oct 29, 2026
1 of 2 checks passed
@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 29, 2026
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