Skip to content

feat(chat): let Chat send findings to Action Center, attach files to tasks and set Ready for review, with Undo (W2-10) - #1897

Merged
gkhngyk merged 9 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-w2-10-action-center
Oct 31, 2026
Merged

gkhngyk merged 9 commits into
ansvisor:mainfrom
cihan-gyk:feature/chat-w2-10-action-center

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Chat W2-10 adds four things, each with Undo:

  • Chat sends a finding to Action Center as an action.
  • Chat adds a finding to an existing action.
  • Chat attaches a file it made to a task.
  • Chat marks a task Ready for review.

Specs: CHAT-09 §D–E, CHAT-20 §9 rows 43–46, §10a–b and §12. Built on main. Everything stays behind CHAT_AGENTIC / CHAT_AGENTIC_ORGS. No migration.

Server (server/src/routes/action-center.js, lib/action-center/from-chat.js)

  • POST /api/action-center/chat-actions/check lists the brand's active actions that already cover a finding's prompts, pages or competitors, closest first. It compares the action's kept list (00244), the prompts and pages its payload names, and the competitor ids it names.
  • POST /api/action-center/chat-actions saves the finding as an action of kind chat_finding, with payload.source: 'chat', the chat, the run and execution.mode (assisted or automated).
    • Duplicates: an action that already covers the finding answers 409 duplicate with that action. Create anyway is offered only when the entities differ.
    • Idempotency: a repeated idempotency key returns what was already made.
  • Frozen at creation: every entity, the targets, the baseline and the approved tasks.
    • The baseline is the basket's metrics over the 14 days before, each named prompt per named platform.
    • The tasks are checked against AC-004: a page or place per task, a Done when, none of analyze/identify/validate/measure, no "for N prompts".
    • tasks_generated_at is set, so the task writer never rewrites them.
    • If the tasks can't be written, the action is deleted again, so no action is left without the tasks the person approved. The same approval tried again makes it whole.
  • The day's five (owner's decision, 2026-10-31): an action sent from Chat counts toward the brand's five new actions a day, like Improve's, Content's and Mentions' sends.
    • It is always made, never refused or queued.
    • The nightly generator opens fewer of its own that day.
    • The answer carries the day's count (dailyLimit), and Chat says one sentence when the day is full.
  • Who and where: Admins and Managers of the brand only. Both routes are on the demo-brand list.
  • What gets recorded: the server reads the chat, the run and the approval step again. A caller's word alone is never recorded.
  • Refusals:
    • an approval whose action was cancelled or deleted since answers 409 conflict;
    • a bare word such as pricing is a path on the brand's site, never a host.

Chat (web/src/lib/chat/registry/action-center-tools.ts)
The tools are for Admins and Managers, with policy Ask (auto allowed in Full auto). No tool completes, dismisses or edits a task or an action.

Tool What it does Undo
create_action (Send to Action Center) Its card lists the tasks. The duplicate check runs before the card: an active action that covers the finding is said with no card, offering Add to AC-{n}. 24 h, while no task has started. Undo dismisses the action, which stays in Dismissed with "Cancelled from Chat by {user} · undo" in History. Any later event (assigned, edited, a task added, a finding or file attached) makes Undo answer changed.
attach_to_action (Add to AC-{n}) Adds the finding as evidence in History. The action's targets never change. 24 h
attach_file_to_task (Attach only) Writes chat_file_task_links, attaches the file and adds a History line. 7 days
set_task_ready_for_review Sets Chat's status in chat_task_state and adds a History line. The task's own status doesn't change. 7 days
  • Untrusted text (CHAT-20 §16a rule 3):
    • Send to Action Center under the latest answer sends that answer's run (fromRunId). The server reads that run's taint.
    • An answer that read outside text shows its card even in Full auto.
    • A run from another chat, or one that can't be read, counts as tainted.
    • Create anyway always shows a card.
  • The card shows what is frozen once the action is made:
    • each task with its new text (shortened, Show all);
    • the finding and the goal;
    • the prompts, pages, competitors and platforms.
  • An action.sent event shows "Sent to Action Center · AC-n" or "Added to AC-n", with Open in Actions. It is kept after a reload. /dashboard/action-center/actions/[id] opens the drawer on the action's brand with no filter.

Action Center

  • "Attached from Chat" in a task's details: name, who, when, Open. The task row shows a paperclip count, read to screen readers as "{n} files attached from Chat".
  • History lines: created from Chat, attachments, findings (with their note and sources), Ready for review, and their Undo.
  • Kind chat_finding gets its title, finding, goal and a Source: Chat block. The decision of 2026-10-31 approved the drawer list, "Source: Chat" and the paperclip count.

Help: help/chat/chat.md, help/action-center/action-tasks.md and help/action-center/action-center-actions.md (approved: false).

Related issue

None. Chat Wave 2 (W2-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

  1. Set CHAT_AGENTIC=true in web and server. Sign in as an Admin.
  2. Ask Chat about a visibility drop. Under the answer, click Send to Action Center. Check the card lists each task's text, the finding, the goal and the targets. Approve it: "Sent to Action Center · AC-n" appears. Open in Actions opens its drawer, which shows the Source: Chat block.
  3. Send the same finding again. Chat names the existing action and offers Add to AC-n, with no new action.
  4. Undo the send within 24 hours. The action moves to Dismissed, and its History says "Cancelled from Chat by … · undo". Assign the action first, and Undo answers that it changed.
  5. Ask Chat to attach a file it made to a task. The task row shows the paperclip count, and its details list the file under "Attached from Chat".
  6. In Full auto, ask for a send from an answer that read a web page: the card still shows.
  7. Send from Chat after the generator has used the day's five: the action is made, and Chat says the day is full.
  8. Unit tests:
    • server: from-chat.test.js, action-center-chat.test.js, generate tests;
    • web: action-center-tools, chat-action-center, undo-action-center, engine.test, action-drawer, proposal-details, the actions and actions/[id] pages.

Screens checked in the browser at 1024px, light and dark, and approved by the user. 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:

For production (@gkhngyk): deploy the server first, then the web. The web tools call the new /api/action-center/chat-actions routes.

Follow-ups, not in this PR:

🤖 Generated with Claude Code

cihan-gyk and others added 5 commits October 31, 2026 11:43
…uplicates checked first (W2-10)

CHAT-09 §E and CHAT-20 §12 (V1 Final rules), behind the web's Chat tools.

- POST /api/action-center/chat-actions/check: the brand's active actions
  that already cover a finding's prompts, pages or competitors (their kept
  list, 00244, and the prompts and pages their payload names), closest
  first: all of it, or some.
- POST /api/action-center/chat-actions: the finding as an action, kind
  chat_finding with payload.source chat, the chat, the run and
  execution.mode (assisted or automated). An action that covers it answers
  409 duplicate with that action and whether Create anyway may go (only
  when the entities differ); the same idempotency key returns what it made.
- Frozen at creation: every entity, the targets, the baseline (the basket's
  metrics over the 14 days before, each named prompt per named platform)
  and the approved tasks, checked against AC-004 (a page or place per task,
  Done when, no analyze/identify/validate/measure, no "for N prompts").
  tasks_generated_at is set, so the task writer never rewrites them.
- Manual: Chat's own dedup key takes no definition slot, and the generator
  leaves chat_finding out of the day's five. The engine scores it on
  creation (priority.js sentByHand) and validate.js measures it on its
  prompts and pages, as other person-sent kinds.
- Admins and Managers of the brand only; both routes on the demo-brand list.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tasks and set Ready for review, with Undo (W2-10)

CHAT-09 §D–E, CHAT-20 §9 rows 43–46, §10a–b, §12. Behind CHAT_AGENTIC.

Tools (lib/chat/registry/action-center-tools.ts, Admins and Managers,
policy Ask (auto allowed in Full auto)):
- create_action (Send to Action Center): its card lists the tasks; a
  duplicate check runs before the card (a row's new `precheck`), so an
  active action that covers it is said with no card, for Add to AC-{n};
  Create anyway only where the entities differ. Undo 24 h while no task
  started: the action is cancelled, in its History.
- attach_to_action (Add to AC-{n}): the finding as evidence in History;
  the action's targets never change. Undo 24 h.
- attach_file_to_task (Attach only): chat_file_task_links, file attached,
  History line. Undo 7 d. The model now gets the file id a change filed.
- set_task_ready_for_review: Chat's status in chat_task_state and a
  History line; the task's own status doesn't change. Undo 7 d.
No tool completes, dismisses or edits a task or an action.

Chat: Send to Action Center under the latest answer (Admins, Managers, own
chat); approval cards list a change's items; a new action.sent event shows
"Sent to Action Center · AC-n" / "Added to AC-n" with Open in Actions
(/dashboard/action-center/actions/[id] redirects to its drawer), kept after
a reload; the place (chat, run, approval key, execution.mode) reaches the
write rows.

Action Center: "Attached from Chat" in a task's details (name, who, when,
Open) with a count on the task row; History lines for created from Chat,
attachments, findings, Ready for review and their Undo; kind chat_finding
with its title, finding, goal and a Source: Chat block.

Help: chat, action-tasks and action-center-actions updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… as any person's send (W2-10)

The owner's decision of 2026-10-31: a Chat-created action counts toward the
brand's daily five new actions like Improve's, Content's or Mentions' sends.
Those are never refused or queued for it: they are made, and the nightly
generator opens fewer of its own, the rest waiting in its queue. So:

- generate.js counts chat_finding again (the exclusion is taken back), with
  a test that Chat's sends today leave the generator less room.
- POST /api/action-center/chat-actions still always makes the action, and
  says the day's count (dailyLimit: used, limit, reached) after it.
- createChatAction passes on daily_limit_reached and the one sentence Chat
  says then (chatEngine.actionCard.dailyLimit); create_action's description
  tells the model to say it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rver (W2-10)

Review follow-ups for Send to Action Center's route (CHAT-09 §E, CHAT-20 §12).

- S1: an action whose tasks can't be written is deleted again, so no action
  is left without the tasks the person approved; the same approval tried
  again makes it whole.
- S2: the duplicate check reads the competitors a payload names
  (competitorIds, competitorId) beside the kept list.
- K3: the chat, the run and how it was approved are read again on the
  server (verifiedPlace): the caller's own chat, a run of it, and its step
  by the approval key, whose mark says automated. A caller's word alone is
  never recorded; executionMode is null when it can't be read.
- K4: an approval whose action was cancelled or deleted since answers 409
  conflict instead of "made".
- K5: the routes' 400s carry code invalid.
- K9: a bare word ("pricing", "blog/aeo") is a path on the brand's site,
  never a host.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
… and the Action Center drawer (W2-10)

- B1: Send to Action Center sends the answer's run (fromRunId). The server
  reads that run's taint, as for a next move; a run that isn't this chat's,
  or an unreadable one, counts as tainted. A change written from an answer
  that read outside text shows its card even in Full auto (§16a rule 3).
- S3: create_anyway shows its card in every mode, Full auto included
  (alwaysCardWhen, read as overBatch by the precedence): that choice is the
  person's (CHAT-09 18a).
- S4: the card shows what is frozen once made: each task with its new text
  (shortened, Show all), the finding (why), the goal and the targets
  (prompts, pages, competitors, platforms). cardDetails replaces cardItems;
  ApprovalCard takes children; copy in chatRun.proposal.
- S5: Undo of a sent action only dismisses it (no deleted_at), so it stays
  in Dismissed, with "Cancelled from Chat by {user} · undo" in History; any
  event but created since (assigned, edited, a task added, a finding or a
  file attached) answers changed.
- S6: conflict is a said refusal: a closed action's or task's words reach
  the model.
- K1: a file's title on the card is read within the brand's workspace.
- K3: the server action reads the chat, run and step itself (the step the
  engine marks when it ran without a card) and records nothing a caller
  only said; Ready for review refuses a task of another brand than its
  chat's.
- K5: 403 not_in_workspace / no_brand_access and 409 conflict are said as
  such.
- K6: the paperclip says "{n} files attached from Chat" to screen readers.
- K7: unique keys; Open the chat only for a reader who may open it.
- K8: actions/[id] opens the drawer on the action's brand with no filter.
- K10: a finding's note and sources show in History; a withdrawn one says
  which finding.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	help/chat/chat.md
#	web/messages/en.json
#	web/src/lib/chat/engine.test.ts
#	web/src/lib/chat/engine.ts
#	web/src/lib/chat/precedence-context.ts
#	web/src/lib/chat/registry/define-tool.ts
#	web/src/lib/chat/registry/tools.test.ts
#	web/src/lib/chat/registry/tools.ts
#	web/src/lib/chat/run-state.test.tsx
#	web/src/lib/chat/run-state.ts
#	web/src/lib/chat/thread.ts
#	web/src/lib/chat/undo.ts
#	web/src/lib/chat/write.test.ts
…eady for review (W2-10)

The member reads of actions and action_tasks see the whole workspace, so a
person given Selected brands could reach another brand's action. Each of the
three server actions now asks can_access_brand first and answers not_found.
…ogged brand's (W2-10)

The inverse runs with the service role; it now reads the task with its
action's brand and finds nothing when that isn't the brand the change was
logged on.
gkhngyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 31, 2026
Resolves ansvisor#1897's and ansvisor#1900's chat files against main's W2-04..W2-25 work;
For you now's Complete tasks chip now types /tasks so the run keeps its
task strip, as the PR's Complete with Chat message did.
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.

Approving, with 2 fixes on top: commits 8ebd130 and a4c4f06. It fast-forwards from this PR's head, with origin/main merged in at e976992.

Merge with main: 13 files conflicted against W2-04/07/19/25. Each one was resolved by keeping both sides:

  • readEarlierRuns keeps fromTaintedRun, through a fromTainted flag that also sets tainted.
  • overBatchCard(tool, input, before) keeps alwaysCardWhen.
  • Cards carry both cardNotes and cardDetailsOf.
  • ChatToolContext has both chat and pageSites.
  • SAID_FAILURES has both not_connected and this PR's codes.
  • The run state, thread, registry, Undo rules, en.json, help and test lists are unions of both sides.

Fixes

  1. 8ebd1302b (brand access in lib/actions/chat-action-center.ts): attachToAction, attachFileToTask and setTaskReadyForReview read actions and tasks with the member's own client. Each one now calls can_access_brand first and answers not_found when it fails. Send to Action Center already had this check, because the server route runs assertBrandAccess. Test: brand access (Selected brands) in chat-action-center.test.ts. It fails without the fix.
  2. a4c4f0638 (Undo scope in undo-action-center.ts): the attach_file_to_task inverse read the task with the service role and no brand filter. It now reads the task's action brand and answers gone when that isn't the logged brand. Test: "takes nothing off a task that isn't the logged brand's". It fails without the fix.

Checked and fine

  • Roles: all four rows are WRITE with minRole: manager. The server actions re-check isManager and the route re-checks assertRole(MANAGER_ROLES).
  • Client input: the chat, run and execution mode are read again on the server (placeOf / verifiedPlace). A browser fromRunId from another chat counts as tainted.
  • Card rules: Create anyway always shows a card, and a tainted run shows a card even in Full auto.
  • Idempotency: one key per approval step, and a retry after Undo gets a 409.
  • Undo: create is cancelled only while untouched, within 24h; withdrawals and detaches are new History events.
  • Guards: the demo guard covers both new routes, and assertWritable is on every write.
  • Embeds: each one names a single FK, checked against the prod schema.
  • Files: the member's read of the file is brand-scoped by RLS.
  • Bundle: client components import only types.

Follow-ups (not blocking)

  • Chat's readBrandAccess still returns 'all'. Brand scope in Chat's precedence and card targets (for example the file title on an attach card) waits on that global change.
  • attachToAction has no idempotency key, so a retried approval could add the finding twice. Keying the event on the approval step would close this.
  • Two concurrent sends with the same key: the second returns duplicate of the first instead of the same action. This is harmless.

Tests

  • Web vitest: src/lib/chat, src/components/chat, action-center (lib, components, pages), src/lib/actions, subscription-guard-coverage, demo-gate-routes, src/lib/guards (plan-row) and src/lib/help all pass. The exception is two engine "who may do what" cases that time out at 5s under full-suite load. They pass alone, at 10s and 3s.
  • Web tsc --noEmit is clean, and eslint and prettier are clean on touched files.
  • Server: the suite passes except 8 timing-only cases (page-content, content-agent, content-people, audit, traffic-events). Prettier and eslint are clean on the touched server files.

@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 194427a into ansvisor:main Oct 31, 2026
4 of 5 checks passed
gkhngyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 31, 2026
Resolves help/chat/chat.md keywords, thread.ts imports and thread.test.ts against the squashed ansvisor#1897/ansvisor#1900 on main.
gkhngyk pushed a commit that referenced this pull request Nov 1, 2026
…click repeats (#1935)

Follow-ups from the approval notes on #1897, #1902 and #1898.

- Add to AC-{n} (attachToAction, W2-10): the evidence_attached event
  carries its approval's key (`chat:{step}`, as Send to Action Center's
  payload does), and a retried approval finds that event and returns it
  instead of adding the finding again. Two calls at the very same moment
  are already kept apart by the engine's idempotency claim; a database-level
  guarantee would need a unique index (a migration), not added here.
- Card clicks (W2-12): Attach to task & mark done and its Undo write the
  task's status only when it really changes (`.neq('status', …)` through
  writeTaskStatus's new onlyIfChanged), so two clicks at once leave one
  History event. The drawer's own writes are unchanged.
- Attach only from a card now records the card's chat and run on its
  file_attached event, as a mark done's event does. attachFileToTask takes
  the card's place and reads it on the server (the person's own chat and a
  run of it) before recording it; no execution mode, as it is the
  person's own click.
- Daily suggestions (W2-26): recordUse skips a removed suggestion (no use,
  no feed stamp), and the page's last Site Audit matches the action's pages
  by pageKey (no scheme, www or trailing slash), by the audit's url or
  final_url, among the brand's latest 500 audits (as the server's recheck
  reads them).
- The test fake reads JSON-path filters (`data->>key`) as PostgREST does.

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