Skip to content

feat(chat): run an agent when a brand is added, once per brand (Onboarding & Help, 00482) - #2150

Merged
gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:feature/onboarding-brand-trigger
Nov 6, 2026
Merged

gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:feature/onboarding-brand-trigger

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

AGENTS plan #11, Onboarding & Help: this adds the "New brand added" trigger that §3.B row 11 names ("New brand or new member"). #2136 left it out because New agent had no such kind. The owner decided to build it now.

The trigger: "When a brand is added" (brand_added)

  • Where it fires
    • The only way a person adds a brand is the server action createBrand (lib/actions/brand.ts). Once the brand is saved, it calls fireBrandAdded inside after(), so the page doesn't wait.
    • The demo brand is made by the API server and never goes through createBrand. fireBrandAdded also skips any brand marked is_demo.
  • Which agents fire. All of these must hold:
    • the agent is active and the trigger is on;
    • its scope is All brands;
    • its workspace has Chat on and isn't read-only (readArmedOrgs);
    • it isn't one of the demo brand's sample agents (activeAgents).
  • Who the run acts as. The run acts as the agent's owner (CHAT-21 §H item 38), and fires only when the owner may use the new brand (readBrandAccess).
    • An owner on Selected brands without the new brand gets nothing. The agent stays on, since it still works on its other brands.
    • An agent with no owner, or whose owner left the workspace, queues as fireBrands does. Its run then stops before it starts and pauses the agent (owner_role), so an Admin sees Take over.
    • The run's chat is the owner's, so it shows in their Inbox, like other triggered runs.
  • Once only. Before it queues, a fire inserts a claim row in the new chat_agent_trigger_claims table: one row per (agent, kind, brand), with "on conflict do nothing" (upsert … ignoreDuplicates). A retry, or two servers firing at once, finds the claim taken and does nothing.
  • Limits
    • brand_added is an event kind, so it counts against the plan's event runs per day: 2 on a trial, 6 on Starter and Growth, 12 on Enterprise.
    • A fire past that limit is kept as Skipped · Daily run limit, as signals are.
    • The agent's monthly cap and the own key's monthly agent limit pause the agent at fire time (cap, own_key_cap).
    • The AI limit and every other check run again when the run starts (openAgentRun).
  • No cooldown exemption is needed. The 6-hour cooldown reads only the agent's runs on the brand being fired (queueTriggeredRun's onBrand). cooldown_until on the trigger row is written but never read. So a second new brand added minutes later is never held back; a test checks this.
  • When the run starts. Runs wait in the queue, and the agents' minute tick (the sweep's start step) starts them within about a minute. The tick runs in its own request with a 300-second limit; a page's server action is a poor place to drive a whole agent run. The owner accepted the one-minute delay. fireBrandAdded takes a driver for callers that want runs to start at once.
  • What the run says. Its message reads "It started because this brand was just added to the workspace." Run history names the trigger Brand added.

New agent (agent-form-triggers.tsx, KINDS)

  • When a brand is added is in the trigger picker. Its note reads "Once per new brand · needs All brands", and its WHEN line reads "a brand is added to the workspace, once per brand".
  • With picked brands, the Brands token is marked: "When a brand is added needs All brands: a new brand isn't in a list you pick."
  • The API refuses the same thing (brand_added_scope, 400) when an agent is created, or when a change to its triggers or scope would leave this trigger without All brands. The form shows the refusal on the Brands field.

The preset (presets/onboarding-help.json)

  • It now has the default trigger [{ "kind": "brand_added" }], which §3.B row 11 supports.
  • Use this agent opens New agent on All brands for this preset. Its card shows "Trigger: When a brand is added".
  • The mode stays Ask me first: every change needs the person's click (owner decision).

Migration 00482_chat_agent_brand_added.sql

  • Adds brand_added to chat_agent_triggers_kind_check and to chat_agent_runs_trigger_kind_check, keeping every kind 00466 allows (agent included).
  • Adds the table chat_agent_trigger_claims:
    • columns: agent_id, kind (brand_added only), brand_id, trigger_id (set null when the trigger is removed), claimed_at;
    • primary key (agent_id, kind, brand_id); rows cascade with their agent and brand; index on brand_id;
    • RLS on, with its neighbours' grants: revoke all from anon and authenticated, select for authenticated, all for service_role;
    • one policy: a person reads a claim where they see the agent and may use the brand. Only the server writes.
  • schema.sql is regenerated and types/supabase.ts updated.
  • The demo's coverage.js lists the table in SKIPPED: a person never adds the demo brand, and no trigger fires for it.
  • Numbering. 00480 is on main, and 00481 is feat(chat): let an Admin choose who starts agent work, kept with who changed it and when (AC-011, 00481) #2147 (00481_agent_work.sql). Whichever of feat(chat): let an Admin choose who starts agent work, kept with who changed it and when (AC-011, 00481) #2147 and this PR merges second gets a schema.sql conflict. Rebuild it with bash supabase/build-schema.sh.

Help (chat/agents.md, ≤150 words per section)

  • The intro lists the new trigger.
  • Onboarding & Help: the agent starts on All brands with When a brand is added, and each new brand gets one run, which acts as the owner and shows in their Inbox.
  • When an agent runs by itself: the trigger's rules (once per brand, never the demo brand, All brands, the owner's access, the daily limit).
  • approved-articles.json is regenerated.

Not in this PR

  • The plan's "new member" half. Per the owner, a new member gets recommendations on their own Chat Home, with no agent run. That comes in a separate PR with migration 00483.
  • Starting a brand-added run at once from the page. The minute tick starts it.

Open question: an agent with no owner, or whose owner left, still queues, and its run then pauses the agent. This matches fireBrands. Should it be skipped silently instead?

Related issue

None. AGENTS plan #11, §3.B row 11 (Drive › Agents › AGENTS-plan.md); CHAT-21 §E (triggers) and §H item 38 (runs act as the owner).

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

  1. Apply 00482 to the database.
  2. As a Manager with All brands:
    • open Agent Center › Ready to use; Onboarding & Help shows "Trigger: When a brand is added";
    • click Use this agent: New agent opens on All brands with that trigger and Ask me first;
    • click Turn on.
  3. Add a brand (Brands › New brand). The page answers at once.
    • Within a minute (the agents' tick), the agent's page shows a run on the new brand with trigger Brand added.
    • Its chat is in your Inbox and starts with "It started because this brand was just added to the workspace."
  4. Add a second brand straight away. It gets its own run, with no cooldown skip.
  5. In New agent, pick When a brand is added with one brand picked, then click Next. The Brands token is marked with "When a brand is added needs All brands…". Pick All brands, and the step passes.
  6. As an Admin, add a brand that isn't shared with a Selected-brands Manager who owns such an agent. Nothing fires for that agent, and it stays on.
  7. Run the tests:
    • cd web && npx vitest run src/lib/chat/agent-triggers.test.ts src/lib/chat/agent-rules.test.ts src/lib/chat/agent-form.test.ts src/lib/chat/agents.test.ts src/lib/actions/brand.test.ts src/lib/chat/schema.test.ts src/lib/chat/agents src/components/chat/agents src/lib/help
    • cd server && npm test (demo coverage)

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change) — see help/README.md

Checked locally on cc03aa2, which branched from main 5a8de56. main (e2be79b, #2143) was then merged in with no conflicts, giving be36719, which still merges cleanly into current main.

  • web/ on cc03aa2:
    • format:check and typecheck pass;
    • lint has 0 errors; its 7 warnings were already on main, and none are in this PR's files;
    • yarn test: 988 of 988 files pass (10,617 tests, 1 skipped);
    • yarn build passes with CI's placeholder env.
  • web/ after the merge: typecheck passes, and the help, agents, triggers and schema tests pass (7 files, 236 tests).
  • server/ (coverage.js): format:check and lint pass; npm test passes 419 of 419 files (5,781 tests, 4 skipped).
  • Schema: bash supabase/build-schema.sh leaves schema.sql unchanged.

No skills/ changes.


Generated by Claude Code

…rding & Help, 00482)

A new trigger kind, "When a brand is added" (brand_added). createBrand
fires it after the answer has gone (after()), never for the demo brand:
each enabled trigger of an active agent on All brands, in a workspace with
Chat on and a writable plan, queues one run on the new brand that acts as
the agent's owner, only when the owner may use that brand. A claim row per
(agent, kind, brand) in chat_agent_trigger_claims, inserted with "on
conflict do nothing", keeps it to once. The daily limit, the monthly and
own-key caps and the AI limit apply as for any event; the 6-hour cooldown
reads the agent's runs on that brand only, so it never holds a new brand
and needs no exemption.

New agent offers the kind; it needs All brands, and the form and the API
say so. Onboarding & Help gets it as its default trigger, on All brands,
still Ask me first. Migration 00482 adds the kind to both check
constraints and the claim table (RLS, select where the agent and brand
are, server writes). Help updated.

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 5, 2026
Main brought ansvisor#2146, ansvisor#2148 and ansvisor#2149. help/chat/agents.md's front matter
keeps both keyword and synonym lists; approved-articles.json regenerated;
schema.sql rebuilt.

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.

gkhngyk
gkhngyk previously approved these changes Nov 5, 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.

gkhngyk
gkhngyk previously approved these changes Nov 5, 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.

…brand-trigger

# Conflicts:
#	help/chat/agents.md
#	web/src/lib/help/approved-articles.json
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Merged main in after #2179/#2180: help/chat/agents.md keeps both sides' keywords, and approved-articles.json is regenerated from the article (Help tests pass).

gkhngyk
gkhngyk previously approved these changes Nov 6, 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.

…brand-trigger

# Conflicts:
#	help/chat/agents.md
#	supabase/schema.sql
#	web/src/lib/help/approved-articles.json
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

Merged main in again after #2147: agents.md keeps both keyword sets, schema.sql rebuilt with build-schema.sh (00481 + 00482), approved-articles.json regenerated; Help tests pass.

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

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