Skip to content

feat(content): show what each agent suggestion was written from (CE-033, CE-034) - #1399

Merged
gkhngyk merged 45 commits into
ansvisor:mainfrom
cihan-gyk:feature/editor-based-on
Oct 23, 2026
Merged

gkhngyk merged 45 commits into
ansvisor:mainfrom
cihan-gyk:feature/editor-based-on

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Content Editor v1.1, CE-033 (web) and the rest of CE-034: every agent suggestion shows the person's data it was written from. "Based on" chips sit under it, a small number marks the words each source supports, and a click opens that source in Sources. The agent card says how many of their sources the batch was built from.

Stacked on #1398 (CE-033 server, 00248), which is stacked on #1397 → #1396 → #1395 → #1394 → #1393 → #1387. Merge after them.

On each suggestion (_suggestions.ts)

  • Based on: a chip per source, with its number, kind (Prompt, Fan-out, Cited page, Search Console) and name. A cited page shows its site. Hover shows what the Sources tab knows of it, e.g. "Microsoft Copilot · searched 4×" or "53 citations · other" (sourceDetail, now shared with the Sources tab).
  • Marks: the source's number after the words it supports, as a small button.
    • A replace's words are cut at link and mark ends.
    • An insert's marks are offsets into its HTML read as text, as the server read it: a fragment, via a <template>.
    • The words are taken from there and found again in the card as drawn: where they were, or else after the last mark from as far back as the card's cleaning took out. Dropped whitespace never shifts them, and the second "2026" isn't mistaken for the first.
  • A click on a chip or a mark opens Sources, scrolls to that row and rings it for a moment.
    • It waits until Sources shows. A tab visited before is only hidden, and a hidden row can't be scrolled to.
    • A source is shown once. A row that isn't listed, such as a query no longer related to the title, gives no ring and no later surprise.
  • Like the rest of the card, the chips and marks are decorations, outside the document: no export, Copy or Copy HTML includes them.

The agent card

  • "… · built from 3 of your sources · See sources" on a waiting batch and on a rewritten page (batchSources, each source once). See sources opens the Sources tab.

Fix open rules sends the kept sources

Data

  • DraftSuggestion gains sources and sourceMarks.
    • getDraftSuggestions reads them and keeps only what the editor can show (suggestionSources, sourceMarks).
    • Marks are in order, inside the text, and point to a source that exists.
    • A source left out before others renumbers the marks after it.
  • supabase.ts gains the two columns.
  • The first-visit note gets its third sentence, now true: "Every change shows the sources it used."

Related issue

Content Editor v1.1 (Drive spec, 2026-10-23): CE-033 (web), CE-034 (card "built from N of your sources · See sources"). 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. With feat(content): have the Content Agent write from the kept sources and name them (CE-033, server) #1398 deployed, keep some sources in Sources and press Run Agent. The suggestions come with "Based on" chips, and numbers after the words their sources support.
  2. Hover a chip: the source's numbers. Click it: Sources opens on that row, ringed for a moment.
  3. The agent card reads "… · built from N of your sources · See sources".

I checked this in the browser on the dev DB with two temporary suggestions on the demo draft (deleted afterwards), at 1024 px in light and dark:

  • a replace ("2026¹ pricing") and an insert, with marks after "2026" and "coffee subscription box comparison";
  • the chips and their hover details;
  • the card's "built from 4 of your sources";
  • a click opening Sources on the cited page's row, ringed and then cleared.

Tests:

  • page.test.tsx:
    • Fix sends the kept sources;
    • a replace's mark and chip, the hover detail and the card's "built from 1 of your sources";
    • a click opening Sources and ringing the row, also in a Sources tab visited before and now hidden, and only once;
    • a mark ending inside a link, which keeps both;
    • an insert's number after the marked "2026", not the one before it;
    • an insert's mark found again in the drawn card;
  • content-drafts.test.ts: the columns read, and bad sources or marks left out;
  • suggestions.test.ts: suggestionSources (renumbering after a source left out) and batchSources;
  • _agent-card.test.tsx: "built from N of your sources · See sources" on a batch and on a rewritten page, and nothing without sources;
  • _data-note.test.tsx: the note's new sentence.

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 (2,944 tests) and yarn build.

🤖 Generated with Claude Code

cihan-gyk and others added 18 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]>
… name them

Content Editor v1.1, CE-033 (server): Fix open rules, Start with Agent and
Rewrite the whole page read the sources the Sources tab keeps, numbered
S1, S2… (content-sources.js). Each suggestion keeps the sources it names
and the words each supports (content_draft_suggestions.sources and
source_marks, 00248); a page rewritten whole keeps its sources.

No number without a source: the model reads a source by its words only, so
a number the agent adds must be the page's, the brand's, the brief's, the
rules', today's, or in a source's own words (then marked to it). A
suggestion with any other number is not kept; a page written whole gets a
placeholder in its place. Source numbers written into the words are taken
out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Content Editor v1.1, CE-033 (web) and the rest of CE-034: under each
suggestion, "Based on" chips name the sources it used (number, kind, name;
the Sources tab's details on hover), and a small number marks the words
each supports. A click on either opens Sources on that row, scrolled to
and ringed for a moment, once. The agent card adds "built from N of your
sources · See sources". Fix open rules sends the kept sources, and the
first-visit note says every change shows the sources it used.

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 CE-033 web half, plus the rest of CE-034. It's web only and stacked on #1398 (server + 00248).

  • "Based on" chips sit under each suggestion, with the Sources tab's details on hover.
  • A small number marks the words each source supports. A click on a chip or a mark opens Sources on that row.
  • The agent card says "built from N of your sources · See sources".
  • Fix open rules now sends the kept sources.

Chips and marks are ProseMirror decorations, outside the document, so no export, Copy or Copy HTML includes them. That matches the Quality spec's rule for editor-only UI.

Checks: lint (0 errors), typecheck, 2,944 web tests, format:check and build. I browser-checked it at 1024 px in light and dark, with temporary suggestions on the demo draft, deleted afterwards.

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: getDraftSuggestions now selects sources and source_marks (web/src/lib/actions/content-drafts.ts:1381). Vercel deploys on merge, so apply 00248 (#1398) first. Without it, every editor's suggestions read fails and Fix open rules never sees its batch, as with 00247 on #1394.

Checked and fine:

  • Sources and marks are filtered on read (suggestionSources). Only known kinds with a ref and a label are kept. Marks must be in order, in range and point to a source that exists, and they are renumbered when an earlier source is dropped.
  • An insert's HTML is parsed into a <template>, which is inert. Chips and marks are built from text nodes, so a source's label is never set as HTML.
  • A chip's key matches its Sources tab row: normalizeQuery is the same on the server and the web.
  • Fix open rules sends the kept sources through rebuildSources, within the server's limits. The server accepts them as optional, so an older editor still works.
  • Chips and marks are decorations outside the document, so no export, Copy or Copy HTML includes them.
  • The highlight waits until Sources is showing, fires once per click and clears its ring.
  • The new strings are in en.json. Tests cover reading, renumbering, marks in replaces and inserts, the chips, the hover detail and the card's "built from N" line.

cihan-gyk and others added 5 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]>
cihan-gyk and others added 13 commits October 23, 2026 15:01
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]>
…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]>
…s-server

# Conflicts:
#	server/src/lib/content-agent.js
#	server/src/lib/content-writer.js
#	supabase/schema.sql
…ates, addresses or words

A claim (a %, a ×, a scale, a currency) is known only with the same unit or
sign, so a fan-out's "crm for 50 users" no longer sources "50%". Today adds
only its year. A cited page's address is no words of its own. The brand's
name and industry are known, digits stuck to a letter ("B2B", "H2") are no
figure, and a source number written into a reason is taken out.

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]>
…d-on

# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
#	web/src/lib/content/suggestions.test.ts
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk No change of its own: merged #1398 (with main and its number fixes) forward. Conflicts were tests only: both sides' imports in suggestions.test.ts, and in page.test.tsx #1373's fan-out test kept beside this PR's sources tests (pressing Run Agent, the button's name in #1396). Web 2,954 tests + build pass.

@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 two merges of #1398's branch:
    • dc35196 had conflicts in two test files only (page.test.tsx, suggestions.test.ts). It keeps both sides: the fan-out test from main and the CE-033 sources test, plus both imports.
    • 95c7060 re-merges to the same tree.
  • The PR merges cleanly on current main.

The merge note still applies. getDraftSuggestions selects sources and source_marks, and Vercel deploys on merge, so 00248 (#1398) must be on prod before this PR merges. 00248 isn't on prod yet.

@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 8cde68c 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
cihan-gyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 23, 2026
…t-steps-server

Main squash-merged ansvisor#1389 and ansvisor#1393–ansvisor#1399; its tree is ansvisor#1399's last head
(b84e724), so this is that merge: this branch's changes on top of it.
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