Skip to content

feat(content): show the data the Content Agent writes from (CE-032, CE-034) - #1397

Merged
gkhngyk merged 37 commits into
ansvisor:mainfrom
cihan-gyk:feature/editor-data-sources
Oct 23, 2026
Merged

gkhngyk merged 37 commits into
ansvisor:mainfrom
cihan-gyk:feature/editor-data-sources

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Content Editor v1.1, part 3: the editor shows which of the person's data the Content Agent writes from: under each Run Agent card, on the Sources tab, and in a first-visit note.

Stacked on #1396 (the Instructions tab), which is stacked on #1395 → #1394 → #1393 → #1387. Merge after them. Spec: Drive › Content Editor › CONTENT-EDITOR-v1.1-spec.md §3b, with the mockup CONTENT-EDITOR-v1.1-mockup.html. This covers CE-032 and CE-034, apart from the parts that need per-suggestion sources (below).

Under each Run Agent card (CE-032)

  • Rules and Brief both get one line: "Uses your data: 1 prompt · 21 fan-outs · 20 cited pages · 10 Search Console queries · Edit sources".
  • The counts are the sources the Sources tab keeps: buildSourceGroups + includedCounts, on the editor's one shared read. Unchecking a source there lowers the count at once.
  • Edit sources opens the Sources tab.
  • When no sources are kept, the line reads "No sources kept yet · Edit sources". While the sources are being read, or if reading fails, there is no line.

Sources tab (CE-034)

  • The tab shows how many sources are in use. Hovering shows "N sources in use", and a screen reader reads it out too.
  • The tab's top reads "N sources in use" (was "Built from N sources"), with the breakdown, then "Last used by Content Agent · 2 minutes ago" (or "just now", or "Not used by Content Agent yet").
    • The time is the latest of three: when the agent's latest batch of suggestions came, when it built the brief, and when it last wrote the page (agent_state_at: Start with Agent, Rewrite from brief on a new page, which leave no suggestions).
    • A run that failed doesn't count, and a value that isn't a date is skipped.
    • DraftSuggestion gains an optional createdAt, read in getDraftSuggestions.

First-visit note (CE-032)

  • At the top of Instructions: "Content Agent writes from your data · Your prompts, fan-outs, cited pages and Search Console queries." with a ✕.
  • Closing it is kept per user, in their own auth user_metadata (content_agent_data_note_dismissed), so it never shows again on any device. There's no migration.
    • The spec asks for the dismissal "stored per user", so this doesn't follow the editor's other notes, which use localStorage (per browser).
    • updateUser({ data }) merges keys, so full_name stays. Nothing authoritative reads this key.
  • It never shows while the user isn't known yet, so it doesn't flash.
  • The spec's third sentence, "Every change shows the sources it used", is left out until the "Based on" chips ship in part 5. Until then it wouldn't be true.

Not in this part

  • Fix open rules doesn't read these sources yet. Today it sends the open rules, the person's own rules and the page; it reads the cited pages only for link rules. The spec has Fix read them too ("Reading 20 cited pages → Matching 21 fan-outs → Checking 10 Search Console queries"). That comes next, in the server part with the "Based on" chips (CE-033), so the line under Run Agent becomes exact for Fix too. Rewrite the whole page and the brief already write from the kept sources.
  • The agent card's "built from N of your sources · See sources", and "Based on" chips with ¹ ² marks, need the sources each suggestion used. That data comes from the server in part 5 (CE-033).
  • The brief's "cited pages average N characters" needs a server read of the cited pages' lengths. It comes as its own small PR.

Related issue

Content Editor v1.1 (Drive spec, 2026-10-23): CE-032, CE-034. No GitHub issue.

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. Open a draft that has sources.
    • Under Rules' Run Agent card it reads "Uses your data: … · Edit sources".
    • Brief shows the same line.
  2. Edit sources opens Sources.
    • The tab shows the count.
    • The top reads "N sources in use", the breakdown, and when the agent last wrote from them.
  3. Uncheck a source, then go back to Rules: the count under Run Agent is one lower.
  4. On a first visit the note shows at the top of Instructions. Close it and reload: it stays closed, on any browser.

I checked this in the browser on the dev DB at 1024 px, in light and dark:

  • a temporary draft (deleted afterwards): "No sources kept yet", and "Last used by Content Agent · 57 minutes ago" from its suggestions;
  • the demo draft, read only: "Uses your data: 3 prompts · 7 fan-outs · 3 cited pages · 0 Search Console queries", the tab reading 13, and the tabs fitting the 400 px panel;
  • closing the note, then reloading: it stays closed. The demo user's flag was removed afterwards.

Tests:

  • _data-note.test.tsx:
    • the line's counts, "No sources kept yet" and Edit sources;
    • the note waits for the user and stays away once closed;
    • closing it writes the user's metadata and updates the store;
    • a failed write still hides it, and it shows again on the next visit;
  • _sources-tab.test.tsx: "Last used by Content Agent" (never / 2 minutes ago / just now) and "N sources in use";
  • page.test.tsx:
    • the line under Run Agent and under Brief;
    • Edit sources opening Sources, with the tab's count;
    • unchecking a source lowering the count under Run Agent;
    • no line and no count while the sources can't be read;
    • "Last used" for a page the agent wrote, and not for a run that failed;
  • content-drafts.test.ts: a suggestion is read with its createdAt.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/) (0 errors; the 6 warnings are in untouched files, as on main)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run: web yarn test and yarn build.

🤖 Generated with Claude Code

cihan-gyk and others added 16 commits October 23, 2026 02:24
The workspace's one AI budget (Improve, the Content Editor's AI and the
Action Center's task writer) had no meter: the first sign of it was an
"AI usage is used up" error. Migration 00242 records what each AI call
was for (prompt_improve_usage.feature, backfilled) and sums the period's
spend by feature (ai_usage_by_feature). GET /api/billing/ai-usage
returns the shares of the plan's budget, and the Billing page shows the
share used, when it resets and what used it, with Agent Chat noted as
running on the workspace's own key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The workspace's AI budget was checked by reading the period's spend; the
call's cost was written once the model answered. Calls running at once
(two tabs, a teammate, an Action Center sweep) all read the same spend
and all ran past the budget. 00243's reserve_ai_usage takes a
per-organization lock, reads the spend with the calls in flight, and
holds the call's estimated cost; reserveAiBudget turns the hold into the
real cost or releases it. Improve, the brief rebuild, Apply rules with
Agent, Start Agent and the Action Center task writer use it in place of
enforceImproveQuota, which goes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review fixes:
- Apply rules with Agent and Start Agent charge an answer the provider
  billed but that missed the schema (NoObjectGeneratedError), as Improve,
  the brief and the task writer do; before, the hold was let go and the
  call ran free.
- A call holds the org's median cost over its last 20 calls this period,
  never less than the model's typical cost, so calls in flight pass the
  budget by far less.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Apply rules with Agent makes at most 8 small edits a run, which can't
bring a long page to its Length (about 2% a run on prod). The Content
Agent can now rewrite the whole page in one background call, left as ONE
suggestion (kind 'page', migration 00247) to compare and accept or reject:
- POST /api/content-drafts/:draftId/rewrite, through Start Agent's claim,
  budget hold and background run;
- passages AI quotes today, tables, images, quotes and HTML blocks go to
  the model as [[KEEP n]] lines and come back as they were; a rewrite that
  drops one fails the run;
- one H1, only the page's own links and the ones offered, the editor's
  tags; the page as it was is kept on the suggestion (before_html).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…Length

Review fixes:
- Kept blocks are described under the page (a quoted passage's words, a
  table's rows, an image's alt) and their text counts toward Length, so a
  rewrite aims at the whole page's length; with Length off it keeps the
  page's own. A kept H1 stays first, and the page's language is kept.
- The answer is taken out of a code fence, text left at its root goes in
  paragraphs, and an answer in Markdown fails the run.
- The page's own links stay exactly as written (query and anchor too).
- Kept blocks go back untouched (an empty table cell stays), only the
  page's first H1 counts as its title, a page that writes "[[KEEP" gets
  markers of its own, and a page that is only kept blocks isn't sent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…compare it

The Rules tab opens with the agent's two ways to meet its rules: Apply
rules with Agent (small edits, up to 8 a run) and Rewrite the page to
these rules (the whole page as one change). The rewrite is recommended,
with a one-line reason, when the page is more than 30% off its Length or
4 rules or more are open.

A rewritten page comes back as one suggestion with its own bar over the
page: the characters before and after and the agent's reason, Compare
(the page and the rewrite side by side), Accept (History keeps the page
first), Reject, and Undo. A page that changed since the agent read it
isn't written over. The page waits for the rewrite with its own steps,
and a failed rewrite's Try again rewrites again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review fixes:
- A page is compared with the one the agent read by its words, with a
  space between blocks, so stored line breaks and lists don't read as a
  change (Accept and Undo were refused on imported pages).
- One agent run at a time from the page; a rewrite never starts before
  the locked passages are read, and sends the editor's own locked keys.
- Accept holds the page still from the start and checks it again before
  putting the rewrite in; Undo puts the page back as it was (two H1s too),
  and the bar waits while the HTML view is open.
- No rewrite advice for an empty page or one too long to send whole; the
  rewrite mark survives a reload; the waiting view says the rewrite waits
  in the editor; Compare's links don't navigate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Content Editor v1.1, part 1 (CE-025, CE-026, §4): the agent row and the
rewrite bar become one card above the toolbar, in three states:
- waiting: "Content Agent has {n} suggestions for you", what they change
  and the rules they gain ("Rules 5 → 6 of 13"), with Review (scrolls to
  the first suggestion and marks it), Reject all and Accept all; a page
  rewritten shows its characters, with Compare;
- all reviewed: what was accepted and the rules met and still open, with
  Undo all and Run Agent;
- working: while Apply rules with Agent runs.
The tile moves a little (a soft ring, a blink, a spark; a sweep and a
breath while working), never a rotating ring, and not at all with reduced
motion. The card wraps on a narrow page; Accept all and Run Agent are the
primary buttons.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Run Agent on the card keeps to one agent run at a time and says why it
  waits; Apply rules never starts beside another run or before the locked
  passages are read.
- Ghost buttons show their hover on the card's grey; the tile and its
  count are hidden from screen readers (the title says it) and the count
  is darker for contrast; "+N more" comes from en.json, the reason's first
  letter is upper-cased in the page's locale.
- Review picks the first open suggestion in the page's order and focuses
  it; the sweep shows on the light tile in dark mode; the spark is slower
  once all are reviewed; without motion the working line is a faint full
  line; "0 still open" isn't said.
- globals.css keeps its own lines; only the motion block is added.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…split Run Agent

Content Editor v1.1 part 2 (CE-028–CE-031): the side panel's tabs become
Instructions · Sources · Live page. Instructions holds Brief | Rules, with
the rules still open on the tab and the segment. Each segment opens with a
status card: Rules has one split Run Agent ▾ (Fix open rules, or Rewrite
the whole page, which renames it "Rewrite page") in place of the two rule
buttons; Brief has Run Agent while empty and Rebuild brief once written.
Why the agent can't run is written under the card, and each card's ⓘ tip
opens below it. The brief shows "Not set yet" and its Length in characters
and about as many words; the toolbar counter adds the page's words.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- Run Agent's choice is worked out once and falls back to Fix without a
  rewrite; once a rewrite starts, the button is back on Fix.
- ▾'s choices are menu radio items; Fix is off when only rules the agent
  can't fix are open, and the card says what to do instead.
- The reason line under the card stays mounted for screen readers; Rewrite
  from brief writes its reason too; the tab's count is read out.
- Brief | Rules switch with the arrow keys, one tab stop.
- An edited brief's rebuild question sits under the card; a brief without
  H2s says to rebuild it; an empty brief shows no origin line.
- Plural copy for the open suggestions and the counter's words; rule names
  joined by the formatter; Run Agent's tip leaves ▾ out without a rewrite.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Content Editor v1.1 part 3 (CE-032, CE-034): under each Run Agent card,
"Uses your data: 1 prompt · 21 fan-outs · 20 cited pages · 10 Search
Console queries · Edit sources", counted as the Sources tab keeps them, so
turning one off lowers it at once. The Sources tab carries its count, and
its top says "N sources in use" and when the Content Agent last wrote from
them (its latest suggestions or the brief it built). A first visit's note
at the top of Instructions says the agent writes from the person's data;
closing it is kept in their own user metadata.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- "Last used by Content Agent" also counts the agent writing the page
  (agent_state_at: Start with Agent, Rewrite from brief on a new page),
  not a run that failed; a value that isn't a date is skipped.
- The latest batch's time is its first suggestion's, not a sort per render.
- Tests: the Brief segment's line, no line while the sources can't be read,
  the tab's count, the note showing again after a failed write, and a
  suggestion read with its createdAt.

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 Oct 23, 2026
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Content Editor v1.1 part 3 (CE-032, CE-034). Web only, no migration. Stacked on #1396 → #1395 → #1394 → #1393 → #1387.

  • "Uses your data: … · Edit sources" sits under each Run Agent card. The counts are live from the Sources tab.
  • The Sources tab shows the number of sources in use, and when the Content Agent last used them.
  • A first-visit note is kept closed per user in auth user_metadata (updateUser({ data }) merges keys, so full_name stays).

Known gap: today Fix open rules doesn't read the prompts, fan-outs or Search Console queries. It reads cited pages only for link rules. The spec has it read them all ("Reading 20 cited pages → Matching 21 fan-outs → …"). That's the next server PR, together with the "Based on" chips (CE-033), so the line becomes exact for Fix as well.

PR titles now carry their task IDs (#1395 CE-025/026, #1396 CE-028–031), as the product brief asks.

Checks: lint (0 errors), typecheck, 2,935 web tests, format:check, build. Browser-checked at 1024 px in light and dark.

gkhngyk
gkhngyk previously approved these changes Oct 23, 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.

Merge note: "Uses your data" under Rules' Run Agent (_side-panel.tsx:307) says Fix open rules writes from the kept sources. That's only true once #1398 takes sources on Fix and #1399 sends them. "Last used by Content Agent" also counts a Fix batch (_side-panel.tsx:179). Merge #1397–#1399 together, so the line is never shown on its own.

Checked and fine:

  • The note's flag is only in the user's own user_metadata:
    • auth.users has no update trigger;
    • nothing that grants access reads the key;
    • the dashboard layout reads the user with getUser(), so the flag holds after a reload and on another device.
  • There are no new reads. created_at was already selected, and one batch is one insert, so suggestions[0].createdAt is the batch's time.
  • The counts come from the same buildSourceGroups/includedCounts the Sources tab uses, so unchecking a source lowers both.
  • "Last used" skips a failed run and values that aren't dates.
  • The tab's count is read out through sr-only.
  • Every string is in en.json, and the tests cover the line, the note, the tab and createdAt.

cihan-gyk and others added 9 commits October 23, 2026 14:45
# Conflicts:
#	server/src/lib/content-writer.js
#	server/src/lib/content-writer.test.js
#	supabase/schema.sql
…le it writes

content_drafts.agent_run ('write' or 'rewrite', in 00247 and its guard) is
set when Start or a rewrite claims the draft, so every device knows a failed
rewrite from a failed Start. Fix open rules and the MCP suggest call answer
409 'writing' while a run holds the draft, so its answer is never dropped
after it was charged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…le it writes

content_drafts.agent_run ('write' or 'rewrite', in 00247 and its guard) is
set when Start or a rewrite claims the draft, so every device knows a failed
rewrite from a failed Start. Fix open rules and the MCP suggest call answer
409 'writing' while a run holds the draft, so its answer is never dropped
after it was charged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…st batch's rows

The editor reads content_drafts.agent_run, so a failed rewrite is tried again
as one on every device (the localStorage mark is gone). Fix open rules' new
409 'writing' says the agent is already writing. getDraftSuggestions finds
the latest batch first and reads only its rows, and Compare's links can't
take focus.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The card's Run Agent writes its reason in the card's second line (amber, tied
to the button by aria-describedby) instead of a hover title, and is turned
off when the only rules left open are ones the agent doesn't fix (one H1),
as the Rules card does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A merge brought in a line formatted by another Prettier version; this puts
back main's formatting, so format:check passes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
cihan-gyk and others added 6 commits October 23, 2026 15:03
…ions

# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
With only One H1 open and no rewrite advice, ▾ no longer shows the disabled
Fix as Recommended.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nd two lines

A test from main still pressed "Apply rules with Agent", and two messages
still named it; the button is Run Agent, its default Fix open rules.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk I merged main (#1404–#1410) forward through the content stack, bottom-up: #1393 → #1394 → … → #1403 → #1411 → #1412. Each PR is now mergeable again.

gkhngyk
gkhngyk previously approved these changes Oct 23, 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.

Still approve.

  • The PR has no new commits of its own, only three merges of #1396's branch (8a293fe, 82ab17f, 339790c). Each one re-merges to the same tree.
  • The PR merges cleanly on current main.

The merge note still applies. The "Uses your data" line under Run Agent describes behaviour that #1398 and #1399 add, so merge #1397–#1399 together.

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

Re-approving after merging main into the branch. The merge only brings in the squashed parent PR; the tree matches the one tested (web and server tests, typecheck, lint, format).

@gkhngyk
gkhngyk merged commit 8db6bac into ansvisor:main Oct 23, 2026
4 of 5 checks passed
@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 23, 2026
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