Skip to content

feat(action-center): add Run agent to actions, starting a Chat run scoped to the action (W2-17) - #1895

Merged
gkhngyk merged 8 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-w2-17-run-agent
Oct 31, 2026
Merged

gkhngyk merged 8 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-w2-17-run-agent

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Chat W2-17: Run agent on an Action Center action. It starts a Chat run scoped to that action, with one of the person's agents or with Chat itself (CHAT-21 §F, CHAT-09 item 21b, governance mockup #g-run-agent). Stacked on #1874 (W2-16); review only the commits after its head. Everything stays behind CHAT_AGENTIC / CHAT_AGENTIC_ORGS. No migration: 00419 already has trigger action, action_id and stop reason action_closed.

Who sees it: Admins and Managers, on an open action's row and at the top of its drawer, while the new Chat is on. Analysts and Agency partners don't see it. The server refuses them too.

The picker (run-agent-button.tsx, loaded when it opens):

  • The agents the person may run whose scope covers the action's brand, last used on this kind of action first (GET /api/chat/agents/compatible).
  • Run with Chat: no agent, the person's own Chat on the action.
  • Create an agent for this kind of action: a skill that fits the category.
  • A pick opens the run's chat in the side panel, or Chat's page where the panel isn't. A run that waits for a slot says so. A refusal shows the out-of-permission card. A read-only workspace gets the upgrade dialog.

Starting the run: POST /api/chat/agents/{id}/run { actionId }.

  • It starts one run on the action's brand, trigger action, acting as the person who clicked.
  • The action as it was goes into evidence_json: number, title, category and open tasks.
  • Untrusted text: an action's title and tasks may come from outside the workspace.
    • The run's message names the action only by number and category (AC-{no} ({category})).
    • Title and tasks go into the scope, which the models read inside <untrusted_data> (progress.scopeUntrusted).
    • The run starts tainted, so every change is a card in every mode.
    • startChatRun takes these as the server's own options; a browser's body can't set them.
  • One run per click:
    • A second click by the same person on the same agent and action within 60 seconds answers the run already queued, running or waiting on them.
    • With 3 runs already waiting on this agent and brand, a click joins only a waiting run on the same action; otherwise it gets 409 queue_full.
    • Run with Chat does the same: two clicks at once, or a second within 60 seconds, open the same chat and start one run.
  • The action closes:
    • A run doesn't start on an action closed since the click ("The action was closed, so the run didn't start.").
    • A running run stops (stop_reason action_closed). Its chat says "This action was closed. Nothing more was changed." stopChatRun keeps the reason on the Chat run, so a driver in another request ends it with the same words.
    • A run not yet handed to Chat is left to its driver within the grace period.
    • Run with Chat doesn't stop when the action closes: it is the person's own chat.
  • History: "Agent run started", "Agent run stopped · action closed" and "Chat started on this action". A deleted agent's line ends without an empty separator.

Drawer at 1024px:

  • Run agent has the height of Copy Data and Export (28px) and is still the primary button.
  • Beside it, the short ID shows the number alone; the full "Action ID: AC-423" stays as its title and for screen readers.

Help: help/action-center/action-center-actions.md (approved: false).

Related issue

None. Chat Wave 2 (W2-17).

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. Set CHAT_AGENTIC=true in web and server (and the W2-16 setup from feat(chat): run agents from schedules and signals, within their caps (W2-16) #1874). Sign in as an Admin.
  2. Open the Action Center. An open action's row and its drawer show Run agent. As an Analyst, they don't.
  3. In the picker:
    • pick an agent: its chat opens in the side panel, named "{agent} · AC-n", with an Action chip;
    • pick Run with Chat: your own chat on the action.
  4. Click the same agent again within a minute: the same run opens and nothing new starts.
  5. Close the action while its run is running: the chat ends with "This action was closed. Nothing more was changed." The action's History says "Agent run stopped · action closed".
  6. Drawer at 1024px, light and dark: Copy Data, Export, the short ID and Run agent stay on one row.
  7. Unit tests:
    • agent-action, action-chat, agent-runs, agent-driver, engine.test;
    • run-agent-button, action-drawer;
    • the chat/actions/[id]/chat and chat/agents routes.

Checked in the browser at 1024px, light and dark: the drawer, the picker, History and the row's icon. The user approved the screens. Screenshots are available on request.

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)

On this branch (with #1874's head 540cbe0 merged in):

Follow-ups, not in this PR:

  • "Ready for review" is left out of the run's words and the Help article until set_task_ready_for_review lands (W2-10).
  • Once Agent Center (agents' own page under Chat) lands, the picker's "Create an agent" link should point to /dashboard/agents/new. The old address will redirect.

🤖 Generated with Claude Code

cihan-gyk and others added 7 commits October 31, 2026 01:02
…(W2-16)

The agent run driver hands a started agent run to Chat: a chat of its own
(origin agent, agent_run_id) for the person it acts as (the clicker for Run
now, the agent's owner now for a trigger, an Admin or a Manager still),
after the checks before a run read again for them; a triggered agent
pauses for the reason (owner_role, plan, brand_gone, cap). Nothing starts
in a read-only workspace or with Chat off. The engine drives it headless;
each step, the help answer's too, is held as the agent's within the run's
cap: max_share of the plan's included monthly budget, worked out on the
server (run_cap past it), the run's spend counting every part (own key,
included budget, credits: a step's credit parts keep its agent run). The
server keeps the run's usage_share on the same base, and the Agents list's
shares now use it too.

The agent run follows its Chat run: Needs you on a card, paused_cap at a
limit (the month's pauses the agent), Stopped at its limit for a triggered
run at its own cap or a run held at a limit for 7 days, No answer in 7
days, done or failed. Three failures in a row pause the agent; a run that
shipped gets a Pending outcome.

Triggers: the server's minute cron calls the web's internal tick
(CHAT_INTERNAL_SECRET, constant-time compare) to fire due schedules in
their time zone (no catch-up, claimed once) and to sweep (Chat's own
sweep, lost and stale runs, failures, the AI limit's auto-resume, and the
start of queued runs: never chained in a finished run's request). The
signal pass tells the web which new signals feed which definitions, so
Signal triggers fire; 15-minute coalescing, a 6-hour cooldown and the
plan's daily event runs (UTC day) apply. Reads page past PostgREST's 1000
rows; each job, trigger and brand fails on its own. KPI and watch-rule
triggers don't fire yet.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The driver now checks the agent's own monthly cap (max_month_share)
before a run starts, not only when a trigger fires: a Run now on an agent
paused at its cap stops before it starts, and a triggered run at the cap
pauses the agent (cap). The cap comparison allows for floating point
(0.18 + 0.02 is just under 0.2), here and in the sweep's resume.
…oped to the action (W2-17)

CHAT-21 §F and CHAT-09 item 21b (governance mockup #g-run-agent), on top of
the agent run driver (W2-16): Admins and Managers get Run agent on an open
action's row and at the top of its drawer, while the new Chat is on.
Analysts and Agency partners don't see it. No migration: 00419 already has
trigger 'action', action_id and stop_reason action_closed.

- Picker (run-agent-button.tsx; the picker's code loads when it opens):
  the agents the person may run whose scope covers the action's brand,
  last used on this kind of action first (GET /api/chat/agents/compatible),
  Run with Chat (no agent) and "Create an agent for this kind of action"
  (a skill that fits the category). A pick opens the run's chat in the side
  panel (panel.openChat), or Chat's page where the panel isn't; a run that
  waits for a slot says so; a refusal shows the out-of-permission card; a
  read-only workspace gets the upgrade dialog.
- POST /api/chat/agents/{id}/run { actionId }: one run on the action's
  brand, trigger 'action', the action as it was (number, title, category,
  open tasks) in evidence_json, acting as the person who clicked; one run
  per person, agent and action a minute (edge case 14); answers the run's
  chat once it started.
- Driver: the run's chat is "{agent} · {action}", with an Action chip, the
  action as an @ reference and its open tasks in the message. It doesn't
  start on an action closed since the click, and stops when the action
  closes while it runs (stop_reason action_closed, edge case 7).
- History (action_events, source chat, as the acting person): "Agent run
  started · {agent}", "Agent run completed/failed/stopped · {agent}", and
  "Chat started on this action" for Run with Chat.
- POST /api/chat/actions/{id}/chat: Run with Chat, the person's own run on
  the action in their default mode, driven past the answer.
- Help: Action Center actions and Chat › Agents articles updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… Run agent at 1024px (W2-17)

With Run agent in the drawer's header, Copy Data, Export and "Action ID:
AC-423" dropped to a second line in the 1024px drawer.

- Run agent in the drawer is the size of Copy Data and Export beside it
  (h-7, text-xs, a 12px icon), still the primary button.
- The top row is a container (@container, group/top): while it is narrower
  than 36rem and holds Run agent, "Action ID:" goes sr-only and the row
  shows "AC-423" alone; the full "Action ID: AC-423" stays as the title
  and for screen readers. Without Run agent, or in a wider drawer, the
  label shows as before.
- Tests: the drawer keeps the full label (title and text), and Run agent
  in the drawer carries data-run-agent at the row's button height.

Checked at 1024px in light and dark: one row, all three buttons 28px high.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ne run per click, the closed-action stop (W2-17)

Pre-push review of W2-17.

- An action's title and tasks may come from outside: the run's message
  names the action by number and category only; its title and open tasks
  go in the scope, which the models read inside <untrusted_data>
  (progress.scopeUntrusted), and the run starts tainted, every change a
  card. startChatRun takes the server's own options for it (Run with
  Chat); a browser's body can't set them.
- A second Run agent click on the same action within 60 seconds is the
  first run, waiting or under way, whatever minute the clicks fall in.
  With 3 runs waiting on other work, a click joins only a waiting run on
  the same action, else it is refused: 409 queue_full ("3 runs already
  wait for this agent on this brand").
- "Ready for review" leaves the run's words and the Help article until
  set_task_ready_for_review lands (W2-10).
- A run that ended before it started says why ("The action was closed, so
  the run didn't start."), and its action's History says "Agent run
  stopped · action closed".
- A run not handed to Chat yet is left to its driver within the grace.
- When the action closes, the run's chat says "This action was closed.
  Nothing more was changed." (stopChatRun's why, kept on the Chat run for
  a driver in another request).
- Run with Chat: a second click by the same person within 60 seconds, or
  two at once, open the same chat and start one run. It doesn't stop when
  the action closes: it is the person's own chat (commented).
- aria-label starts with the visible words: "Run agent · {title}"; the
  picker's ↑ ↓ and Esc are tested; a picker that finds the workspace
  read-only opens the upgrade dialog; a deleted agent's History line ends
  without an empty separator.
- The drawer's short ID no longer uses a container query that matched at
  every width: beside Run agent the number alone, always.
- Tests: Agency partners refused, a brand not shared, settleAgentRun with
  a finished Chat run, no chat yet and a failed stop, two Run with Chat
  posts at once.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
gkhngyk
gkhngyk previously approved these changes Oct 31, 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.

Looks good, approving as is (reviewed the commits after #1874's head: d03e97f, 1b21680, b07f2fa and the merge c46c55f).

What I checked:

  • Who can run it: the button shows only for Admins and Managers, only on open actions, and only with Chat on. All three routes check the role again on the server. Analysts and Agency partners get the out-of-permission answer. With the flag off, every route answers 404 before reading a session.
  • Scope: the action is read with the service role, then checked against the person's workspace and brand access (another workspace's action, a brand not shared with the person, a deleted action or a missing one all answer 404). Demo-brand actions get 402 and closed actions get 409. The agent has to be one the person may run (agentForAct / runnableAgents), and its scope has to cover the action's brand.
  • Read-only, plan and limits: writeRefusal runs first on both POST routes, and the picker opens the upgrade dialog. The agent path goes through runChecks (including the monthly cap from #1874), and Run with Chat goes through startChatRun's limits. subscription-guard-coverage, demo-gate-routes and plan-row all pass.
  • Double clicks: a minute key plus a check for the same person's run on the same action in the last 60 seconds. A click past the 3 waiting runs joins only a run on the same action, or gets a 409 (queue_full). It is never folded into another action's run.
  • Closed action: a run on an action that closed before it started ends action_closed. While it runs, the minute sweep stops its Chat run with that reason. The reason is stored on chat_runs.error, which passes the column's check, so a driver in another request finishes with the same words. The run then writes "Agent run completed" to the action's History once.
  • Untrusted text: the action's title and tasks stay out of the message. They go into the scope, which is wrapped as untrusted_data when scopeUntrusted is set, and both run types start tainted, so every change is a card. The publishing switch still applies, because the run acts as the person who clicked and goes through the same engine checks.
  • Database: trigger_kind = 'action', stop_reason = 'action_closed' and the new action_events event names all pass the live constraints, so no migration is needed. The new reads have matching indexes. There are no new PostgREST embeds.
  • Client bundle: the client files import only types from the server modules.

Tests: vitest on src/lib/chat, src/components/chat, src/components/action-center, src/lib/action-center, src/app/api/chat, guards, subscription-guard-coverage, demo-gate-routes and help: 1828/1829 pass. The one failure is the known chat-menu clock case. tsc --noEmit, eslint and prettier on the touched files are clean.

Nits, none blocking:

  • In help/action-center/action-center-actions.md, "If the action is completed or dismissed, the run stops" reads as if it covers Run with Chat too, but only an agent's run stops. Run with Chat stays the person's own chat, as action-chat.ts says.
  • actionCenter.actionsPage.runAgent.toast.openRun isn't used anywhere.
  • Two clicks at the same moment that fall on either side of a minute boundary can still queue two runs, because each gets a different key and neither sees the other's row yet. This is rare and is the same gap Run now has.

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

Merging in order with the other approved PRs. This head merges current main; its tree is what main will be after this squash, checked with the full server and web suites, tsc, lint, format and next build.

@gkhngyk
gkhngyk merged commit bf707cb into ansvisor:main Oct 31, 2026
4 of 5 checks passed
gkhngyk pushed a commit that referenced this pull request Nov 2, 2026
#1986)

actionCenter.actionsPage.runAgent.toast.openRun has no reader: the run
opens beside the action, so no toast offers to open it (a follow-up from
the #1895 review). Agent Center's own chatAgents.actions.openRun stays.

The rest of that follow-up is already on main: the Help article says only
an agent's run stops when the action closes, and the picker and the agent
driver link to /dashboard/agents.

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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