Skip to content

feat(chat): check a person's brand access in Chat and agents (brand access PR 9, 00436) - #1975

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/brand-access-web-chat
Nov 2, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/brand-access-web-chat

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Brand access PR 9: Chat and agents check each person's real brand access. It adds one server-side rule for the web, web/src/lib/brand-access.ts, which mirrors can_access_brand and the API server's lib/access.js. Migration 00436. Everyone still has All brands, so nothing anyone sees changes until PR 12 (the Team UI).

What changes

  • brand-access.ts: readBrandAccess, canAccessBrand, hasBrandAccess, allowedBrands, readAccessProfile / brandAccessOf, hasAllBrands. It fails closed: All brands only for an Admin or an explicit 'all' scope, a read error throws, and canAccessBrand throws without can_access_brand.
  • Chat:
    • precedence reads the person's real access (the stub that always said 'all' is gone);
    • a brand that wasn't shared gets 404;
    • a chat about a brand the person lost is hidden, not deleted (§9 Q4);
    • list_brands and list_connections list only their brands, from the same read precedence made.
  • Which brands a chat touched (00436, touched_brand_ids on chat_conversations and chat_files):
    • The engine notes each call's brand before the call runs. If noting fails, the call doesn't run.
    • A message's @ references note their brands before the message is stored.
    • Agent-run chats start from the run's brand.
    • A Selected-brands person reads a shared chat, an agent run's chat or a file only when every brand it touched is theirs, branded chats included. A chat whose brands aren't known (older rows, null) stays hidden from them.
    • Files made outside a chat keep their own brand, and existing ones are backfilled.
    • Note functions are service-role only.
  • Agents:
    • all_brands means all brands of the person a run acts as, checked again at run start.
    • A triggered agent whose owner can use none of its brands pauses as brand_gone.
    • An Admin's Resume checks the owner's brands and connections.
    • chat_agents RLS goes through can_see_chat_agent(id) (security definer, fixed search_path), after an organization pre-filter.
  • schema.sql and types/supabase.ts are regenerated.

For PR 11 and PR 12:

  • list_members still says 'all'.
  • Older agent versions follow the current version's scope.
  • A bulk "who can see which brand" reader is still to come.

Related issue

None. Brand access plan, PR 9 (spec §5.3, §10).

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

Checked on the dev database with 00436 applied, in a rolled-back transaction. A second demo-workspace member shared four chats:

  1. demo brand, also touching Acme;
  2. demo brand only;
  3. brandless, touching Acme;
  4. brandless, brands not known.

The admin (All brands) sees all four, as before. The same person as a Selected-brands manager with nothing shared sees only chat 2. The demo brand is everyone's, so that is the one they may read.

Counts the admin can see in conversations, files, agents and runs are the same before and after.

Unit tests: brand-access, touched-brands, Chat precedence, agents, triggers and driver, Resume, list_brands/list_connections, and the migration-reading server tests.

A pre-push review found no blocking issue; its follow-ups are in.

Needs migration on prod: 00436 (gkhngyk).

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes (0 errors; 7 old warnings in files this PR doesn't touch)
  • yarn typecheck passes
  • yarn format:check passes
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change)

CI passed on this branch (1776a4d), run on the fork (https://github.com/cihan-gyk/ansvisor/actions/runs/41361035770): schema is up to date; web format:check, lint, typecheck, all tests and yarn build pass; server lint, format:check and all tests pass.

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits November 2, 2026 01:08
…ccess PR 9, 00436)

BRAND-ACCESS PR 9 (spec §5.3, §10). Everyone still has All brands, so
nothing anyone sees changes until the Team screen narrows someone.

- web/src/lib/brand-access.ts: the web's one server-side rule, read with
  the service role as the API server's lib/access.js reads it:
  readBrandAccess (all, the shared brand ids plus the demo brand, or null
  for a non-member), canAccessBrand (the database's can_access_brand,
  optionally within a workspace), hasBrandAccess, allowedBrands,
  readAccessProfile and brandAccessOf. Before 00388 everyone counts as All
  brands. Chat's session-free writers (actor/access.ts) use it too.
- Chat: readPrecedenceContext and readAgentContext read the real access
  from the profile they already read, so the precedence refuses a brand
  that wasn't shared (brand_scope). startChatRun and createChat refuse a
  brand the person can't use, and a chat about a brand they lost answers
  not found (§9 Q4). list_brands and list_connections list their brands
  only.
- Agents: all_brands means all brands of the person a run acts as;
  runnableBrands takes that access, triggers queue on the owner's brands,
  and the driver checks again at start (a lost brand stops the run as
  brand_gone, none left pauses the agent). A scope brand the person can't
  use is refused as not found; list and detail show only their brands'
  names and runs; a Selected-brands owner sees their own All-brands agent.
- 00436: chat_conversations/chat_files.touched_brand_ids (null = not
  known, every existing row), chat_start_touched_brands and
  chat_note_touched_brands (service role only), a chat_files trigger that
  copies its chat's set, the shared-chat and files policies that also let
  in a brandless row whose touched brands are all the person's, and
  can_see_chat_agent for chat_agents (versions, triggers and runs follow;
  off the coverage test's deferred list). The engine notes each call's
  brand before the call runs (a failed note refuses the call); a web
  deployed before 00436 notes nothing. schema.sql and types regenerated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…o their owner's brands (00436)

Review follow-ups for brand access PR 9, before PR 12 turns narrowing on.
00436 has been applied nowhere yet, so it changes in place. With
everyone on All brands nothing anyone sees changes.

- 00436: a chat or file about one brand that touched another is read by
  a Selected-brands person only when every touched brand is theirs: the
  shared-chat, agent-run-chat and files policies (admins and All-brands
  people as before; not-known rows stay hidden from Selected brands).
  A file made outside a chat keeps its own brand, and the files there
  are get it too. chat_agents' policy filters by the caller's
  organization before can_see_chat_agent.
- An agent run's chat starts its touched set from the run's brand. A
  message's @ references note their brands before the message goes in
  (a prompt's or task's own brand, the page's brand chip for a page,
  chart, metric or table; the whole workspace when it can't tell).
- A database without chat_note_touched_brands makes the chat's set not
  known instead of leaving it; a missing column is ignored.
- The run's precedence hands its brand access to the tool context
  (McpAuthContext.brandAccess, brandAccessFor), so list_brands and
  list_connections read it once. The chat's own brand is never noted
  again.
- A triggered agent whose owner may use none of its brands pauses as
  brand_gone. An Admin's Resume of someone else's agent checks the
  owner's brands and their connections.
- brand-access.ts fails closed: All brands only for an Admin or an
  explicit 'all' scope, and canAccessBrand throws without
  can_access_brand.

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 Nov 1, 2026
gkhngyk
gkhngyk previously approved these changes Nov 2, 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

# Conflicts:
#	supabase/schema.sql

@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 7d91b85 into ansvisor:main Nov 2, 2026
5 checks passed
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.

2 participants