Skip to content

feat(chat): approve all of a draft's pending changes at once - #2377

Merged
gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:feature/approve-all-for-a-draft
Nov 16, 2026
Merged

gkhngyk merged 4 commits into
ansvisor:mainfrom
cihan-gyk:feature/approve-all-for-a-draft

Conversation

@cihan-gyk

@cihan-gyk cihan-gyk commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Several agents can each propose changes to the same content draft. Each one leaves its own waiting run with its own approval, so people approved the same page again and again. Chat's Inbox now groups the waiting changes by draft. A group shows "N changes from M agents" with one Approve all button, when they can be approved together. Approving one change at a time still works as before.

Review fixes (CI, 16 Nov)

  • The problem: Chat Home, All chats, the chat menu and the page pane failed to load in tests with supabaseUrl is required. chat-inbox.tsx → inbox-draft-groups.tsx → lib/actions/chat-approve-all.ts loaded the admin client (directly, and through lib/chat/engine.ts and lib/chat/pending-approvals.ts) at load time.
  • The fix: the Approve all work moved, unchanged, to web/src/lib/chat/approve-all.ts, a plain server-only module (not 'use server'). The 'use server' file (lib/actions/chat-approve-all.ts) keeps only the subscription guard and the result types. It loads that module when an action is called, as chat-agent-calls.ts and chat-uploads.ts already do.
    • approveGroup still awaits assertWritable({ brandId }) first.
    • inbox-draft-groups.tsx is unchanged.
    • A static import trace from chat-inbox.tsx, inbox-draft-groups.tsx and the action file finds no path to admin.ts.
  • Behaviour is unchanged:
    • the plan is checked first;
    • a group with more than one page rewrite is refused (one_by_one);
    • each change is made before the answer;
    • the Inbox page keeps its 300 s maxDuration.
  • Before: the 4 test files failed to load, so 0 tests ran. After: 4 files and 97 tests pass.
  • main is merged in (clean).

Review fixes (13 Nov)

  • The problem: an approved change is saved exactly as its card showed it. update_draft sends the whole page, and a resumed run doesn't re-read the draft: resumePending replays the stored input, and changeDraft replaces the whole page. So two page rewrites of the same draft overwrite each other, even one after the other. A new engine test shows this.
  • Approve all is now offered only when at most one change in the group rewrites the page.
    • Only set_draft_ready and suggest_draft_changes count as leaving the page alone. update_draft, optimize_draft and any future draft write count as rewrites.
    • Otherwise the card says "Approve these one by one: they change the same page" and links each change to its chat.
    • The server refuses such a group too (one_by_one), before approving anything.
  • The result counts only changes that were actually made. The action now waits until each card's change is made or fails (driveRun gets an onCard(made) callback). A change that was approved but not made is listed, not counted.
  • Runs get 300 s. The runs now start inside the request, and the Inbox page sets export const maxDuration = 300, the same as the single approve route. This is on page.tsx, not the 'use server' file. A run cut off by that limit is ended as timed out by the existing sweepChatRuns.

Changes

  • web/src/lib/chat/pending-approvals.ts (new). Reads the person's waiting runs for a brand and groups them by the content draft they would change.
    • Only ask write cards on content_drafts tools are included. Go-live tools (publish, CMS send, webhook) and cost cards are left out on purpose, so Approve all can never publish.
    • Only drafts with two or more waiting runs are shown.
    • Each item records whether it rewrites the page. togetherOf holds the at-most-one-rewrite rule.
    • The browser view gets together and each item's chatId.
    • The agent name comes from the run's chat, then chat_agent_runs, then chat_agents. A plain chat shows as "Chat".
  • web/src/lib/actions/chat-approve-all.ts (new 'use server' file, async exports only), with the work in web/src/lib/chat/approve-all.ts (plain server-only module, loaded inside the actions).
    • listDraftApprovalGroups(brandId).
    • approveGroup(brandId, key, version). It reads the group again on the server and matches it only by the server's own key.
      • It refuses one_by_one groups.
      • version is a fingerprint of the cards' digests. If any card changed since the list was shown, the call is refused with changed.
      • It returns { made, failed }.
    • Each run is approved through the existing prepareResume, and its driver starts inside the action and stays alive through after().
    • Read-only and demo workspaces get read_only back instead of an error.
  • web/src/lib/chat/engine.ts: driveRun takes an optional onCard(made) callback.
  • Inbox page (chat/inbox/page.tsx): export const maxDuration = 300.
  • web/src/components/chat/inbox-draft-groups.tsx (new), plus small hooks in chat-inbox.tsx and chat-inbox-page.tsx. Shows the groups, their items, Approve all or the one-by-one note, and the result ("N changes made to …", with any that weren't made listed).
  • Strings and Help. Strings are in web/messages/en.json. One paragraph is in help/chat/agents.md (137 words), and Help is regenerated.

Tenant scoping

  • The brand must belong to the person's organization.
  • Run states are read with the person's user_id and organization_id, and progress.brandId must equal the brand.
  • Chats, agent runs and agents are read with organization_id; chats are also read with user_id.
  • Drafts are read with brand_id. A draft outside the brand reads as not found, and its runs are dropped.
  • The browser only gets an HMAC group key, a version fingerprint and each item's chat id (the person's own chats). It never gets run, draft or step ids.
  • The HMAC secret is SUPABASE_SERVICE_ROLE_KEY, falling back to CRON_SECRET. If neither is set, it throws.

Migration

None.

How to test

  1. Get two agents to propose changes to the same draft: one page rewrite (update_draft) and one Mark ready. Open Chat's Inbox. One group shows "2 changes from 2 agents" with Approve all.
  2. Click Approve all. Both changes land: the page shows the rewrite and the draft is ready. The result line says 2 changes were made.
  3. Get two agents to each rewrite the same draft. The group shows "Approve these one by one: they change the same page", with a link to each chat, and no Approve all.
  4. Change one of the cards before approving, then click Approve all. It is refused as changed.
  5. Run the tests:
cd web && yarn vitest run src/lib/chat/pending-approvals.test.ts src/lib/actions/chat-approve-all.test.ts src/components/chat/chat-inbox.test.tsx src/lib/chat/engine.test.ts src/components/chat/chat-home.test.tsx src/components/chat/chat-all-chats.test.tsx src/components/chat/chat-menu.test.tsx

Checks

  • Typecheck, ESLint and Prettier on the changed files: clean. The only two warnings are old ones in engine.test.ts, on lines this PR doesn't change.
  • After the CI fix: src/components/chat, src/lib/chat, chat-approve-all.test.ts (14 tests), the Inbox page test, use-server-exports, subscription-guard-coverage, src/lib/help and src/i18n: 256 files, 3,619 tests pass, 1 skipped.
  • New tests:
    • two page rewrites are refused (page unchanged, nothing approved);
    • a page rewrite plus Mark ready leaves both in place;
    • the count excludes a change that wasn't made;
    • maxDuration is set on the Inbox page;
    • the card UI.
  • No chat-thread-view.tsx, page-pane.tsx, page-open.ts, editor-folder or lockfile changes.

Generated by Claude Code

When several agents each leave a run waiting on the same Content draft,
the Inbox's Needs you now shows one card for the draft with an Approve
all button. Each run is approved through prepareResume and goes on in the
background; a change that can't be approved is listed and the others
still go on.

Co-Authored-By: Claude Sonnet 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.

Approve all resumes every run in the group at once, but update_draft sends the whole page and updateContentDraft has no version check, so the runs (all built from the same starting page) overwrite each other and only one change lands, though the card says N were approved. Please run a group's runs one after another (or allow Approve all only when one card rewrites the page). Also, the runs go on via after() in the inbox page's action with no maxDuration (the single approve route sets 300), so a run cut off mid-step stays 'running'.

…one; runs get 300 s

An approved run makes the change its card stored (resumePending runs the
call's own input, bound to the card's digest); it never goes back to the
model. update_draft's input is the whole page, written from the page its
run read, so two such changes overwrite each other even one after another.

- A draft's group is approved together only when at most one change
  rewrites the page (update_draft, optimize_draft; set_draft_ready and
  suggest_draft_changes leave it). Otherwise the card says "Approve these
  one by one", offers no Approve all and links each change to its chat;
  the server refuses such a group too (one_by_one).
- Drivers start inside the action, as streamRun starts them, and the
  action waits for each card's change (driveRun's new onCard). The result
  counts the changes made; one approved but not made is listed, not
  counted.
- The Inbox page sets maxDuration = 300 (RUN_REQUEST_MS), as the approve
  route does; the 'use server' file exports async functions only. A run
  its request leaves unfinished times out through sweepChatRuns, as on the
  single path.

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.

CI is red: chat-home, chat-all-chats, chat-menu and page-pane tests fail to load with 'supabaseUrl is required' from src/lib/supabase/admin.ts, so a chat component now imports a module that pulls in the admin client at load. Please keep the admin client out of the components' import graph (server-only module behind the action).

The Inbox's draft groups import the Approve all actions, and the action
file loaded the admin client, the engine and the pending approvals at
load. Every test that loads chat-inbox.tsx (Chat Home, All chats, the
chat menu, the page pane through the shell) then failed to load with
"supabaseUrl is required".

The work moves to lib/chat/approve-all.ts, a plain server-only module
with the service role. The 'use server' file keeps only the subscription
guard and the result types at load, and imports the module when an
action is called, as chat-agent-calls.ts and chat-uploads.ts do. It still
exports async functions only. Approve all behaves as before: the plan is
checked first, a group with more than one page rewrite is refused, each
card's change is made before the answer, and the Inbox page's 300 s
maxDuration is unchanged.

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 merged commit d6ed273 into ansvisor:main Nov 16, 2026
12 checks passed
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 16, 2026
Brings in ansvisor#2446 (no row counts), ansvisor#2442, ansvisor#2447, ansvisor#2441 and ansvisor#2377.

Conflicts:
- approved-articles.json: regenerated (yarn vitest run -u src/lib/help).

help/chat/chat.md merged cleanly: main's "Watch the work" text (no row
counts) and this branch's action-title line are both kept (132 words);
updated stays 2026-11-16, last_checked 2026-11-15.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 16, 2026
Takes the updated ansvisor#2420, which now includes upstream main (ansvisor#2446 no row
counts, ansvisor#2442, ansvisor#2447, ansvisor#2441, ansvisor#2377).

Conflicts:
- approved-articles.json: regenerated (yarn vitest run -u src/lib/help).

help/chat/chat.md merged cleanly: updated 2026-11-16, last_checked
2026-11-15.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 16, 2026
Takes the updated ansvisor#2422, which now includes ansvisor#2420 and upstream main
(ansvisor#2446 no row counts, ansvisor#2442, ansvisor#2447, ansvisor#2441, ansvisor#2377). No conflicts;
approved-articles.json checked with yarn vitest run -u src/lib/help
(no change).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 16, 2026
Takes the updated ansvisor#2423, which now includes ansvisor#2422, ansvisor#2420 and upstream
main (ansvisor#2446 no row counts, ansvisor#2442, ansvisor#2447, ansvisor#2441, ansvisor#2377).

Conflicts:
- help/chat/chat.md: front matter only; took the later last_checked
  (2026-11-16). Both sides' text is kept: main's "Watch the work" (no
  row counts, 148 words) and this branch's look-up-by-code paragraph in
  "Ask about your actions" (146 words).
- approved-articles.json: regenerated (yarn vitest run -u src/lib/help).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
cihan-gyk pushed a commit to cihan-gyk/ansvisor that referenced this pull request Nov 16, 2026
Brings in ansvisor#2446 (no row counts), ansvisor#2442, ansvisor#2447, ansvisor#2441 and ansvisor#2377.

Conflicts:
- help/chat/chat.md: front matter only; took the later last_checked
  (2026-11-16), updated stays 2026-11-16. Both sides' sections are kept:
  main's "Watch the work" (no row counts) and this branch's "Open and
  close Settings in the chat menu" (98 words).

approved-articles.json merged cleanly and is current (yarn vitest run -u
src/lib/help: no change).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
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.

3 participants