Skip to content

feat(content): hand a draft's open Quality checks to the Content Agent in one run (00275, CE-051, part 1) - #1481

Merged
gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:feature/quality-handoff-api
Oct 25, 2026
Merged

gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:feature/quality-handoff-api

Conversation

@cihan-gyk

@cihan-gyk cihan-gyk commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Quality Phase 2, CE-051 "Hand off to Content Agent", part 1: the server and 00275. This PR is from main.

The spec (§5) says: "One button, Hand off, in the Quality segment. The agent runs every fixable suggestion in one go… Results arrive as normal suggestions. What only the user can give stays on the to-do list. The agent leaves [placeholders]; it never fills them itself." There's also a Settings › Content toggle, "Hand off quality fixes automatically" (off by default), that does it for every new or optimized page, with every run logged and counted toward AI usage.

Quality's checks are computed in the browser, so the editor sends the open, fixable ones. The server says what each asks, and runs the existing Fix open rules pipeline as one run of its own kind. That pipeline already has the budget hold, the step chips, Stop, the number, person and quote guards, and locked passages. The web parts follow as stacked PRs: Settings, the Hand off card, then the automatic run and History.

POST /api/content-drafts/:draftId/handoff (Quality phase 2, else 404 off)

  • Body: { brandId, items: [{ key, detail }], textRules, locked, sources?, origin }.
  • key is one of the 15 fixable checks:
    • Readability: average sentence, long sentences, reading level, answer first, short paragraphs, heading spacing, passive voice and jargon.
    • Trust: claims sourced, brand facts, fresh sources, weak sources, primary sources, disclosure, balanced, broken links.
  • HANDOFF_CHECKS holds the instruction for each key in the server's own words. A to-do's key, or any other, is a 400.
  • detail is what the check names on the page (its sentences, links, the disclosure line).
  • It answers { count, batchId } and its errors as /suggestions does. New:
    • 409 not_due: an automatic hand-off on a draft its brand doesn't hand off. It's checked first.
    • 409 already: the draft had its automatic one.

Guards, on top of Fix's

  • The editor's details and the person's own rules aren't facts the agent may write. A number or name only they carry isn't kept.
  • trust:brandFacts reads the brand's approved facts (still on its site) on the server, and those it may write.
  • A placeholder stays the person's. A change that takes one out of its passage, filled or dropped, isn't kept. The check compares the passage before and after the change, so a "before" that cuts into the brackets doesn't get through.
  • The source checks (claims, fresh, weak, primary, broken links) read the draft's sources as link targets, so the agent can link one.

00275

  • content_agent_runs:
    • kind gains 'handoff';
    • a new origin column (person | auto, default person);
    • a unique index makes an automatic hand-off once a draft (its row is the claim).
  • brands.content_quality_handoff, off by default; brands' admin/manager update policy covers who may change it.
  • content_drafts.quality_handoff: a trigger copies the brand's setting when the draft is made, and undoes a member's change. Turning it on reaches new drafts only. Turning it off stops the drafts not handed off yet: an automatic run also needs the brand's setting as it is now (3c8eb75).
  • Applied on the dev DB and tested in a rolled-back transaction:
    • a new draft copies the brand's setting;
    • a member's change is undone;
    • a second automatic run is refused;
    • a person's runs repeat.

Automatic runs

  • An automatic run the AI budget stops before it begins is taken back, so it runs once the budget renews.
  • Any other failure keeps it as failed, so a failure that would repeat can't loop.

Deploy order: apply 00275 before this server code. A person's run writes no origin; the column's default covers it, so older runs still insert. But hand-off runs, and the new kind, need the migration.

Pre-push review fixes: link targets for the source checks, brand facts read on the server, the placeholder check on the whole passage, text rules no longer counted as facts, not_due first, no origin on a person's run, a run not taken back marked failed. All have tests.

Left for follow-ups:

  • Removing a weak or broken link (the fix here links a better source or softens the claim; an unlink needs the editor's accept to drop a mark).
  • The spec's "schema", which has no Quality check behind it: Before you publish gives the JSON-LD.

Related issue

Quality spec (Drive), Phase 2, CE-051 (part 1).

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. Apply 00275 and set CONTENT_QUALITY_PHASE=2 on the server.
  2. POST /api/content-drafts/{id}/handoff with { brandId, items: [{ key: "readability:longSentences", detail: "…" }] }. It answers { count, batchId }, and the draft has a new suggestion batch. content_agent_runs has a handoff run.
  3. With origin: "auto" on a brand with content_quality_handoff off: 409 not_due. Turn it on, make a new draft, hand it off twice: the second is 409 already.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/) — no web change
  • yarn typecheck passes (run from web/) — no web change
  • yarn format:check passes (run from web/) — no web change
  • Changes are focused — one concern per PR

Server: npm run format, npm run lint and npm test (2,534 passed) pass. supabase/schema.sql rebuilt.

Migration order: 00275 follows 00271–00274 (open PRs). I'll renumber if they merge out of order.

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits October 25, 2026 09:29
…t in one run (00275, CE-051, part 1)

POST /api/content-drafts/:draftId/handoff (Quality phase 2): the editor
sends the page's fixable checks by key, with what each names on the page;
the server says what each asks and runs Fix open rules as one run of kind
'handoff', its suggestions left for review. The editor's details are no
fact the agent may write, and a placeholder stays the person's.

00275: content_agent_runs.kind 'handoff' and origin (person | auto), an
automatic hand-off once a draft; brands.content_quality_handoff (off by
default) and content_drafts.quality_handoff, the brand's setting copied
when the draft is made. An automatic run on a draft its brand doesn't
hand off is 409 not_due, a second one 409 already; one the AI budget stops
before it begins is taken back, to run once the budget renews.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rand's approved facts

Pre-push review fixes:
- the source checks (claims, fresh, weak, primary, broken) read the
  draft's sources as link targets, so the agent can link one
- trust:brandFacts writes the brand's approved facts, read on the server
- a placeholder is read in the passage before and after the change: a
  "before" that cuts into its brackets doesn't fill it
- the person's own rules are no fact a hand-off may write either
- not_due first; a person's run writes no origin (the column's default),
  so a database before 00275 takes it; a run not taken back is failed

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 25, 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.

One fix before merge, plus two small follow-ups.

I ran 00275 against the current schema in a rolled-back transaction, after 00271–00274. It applies cleanly, and these all behaved as described:

  • A new draft copies its brand's setting, and a member can't change it on the draft.
  • Only admins and managers can change the brand's setting; another organization's member can't see or change it.
  • Members can read runs but can't write them.
  • A second automatic hand-off on a draft is refused; a person's hand-offs can repeat.
  • The prompt_improve_usage.feature check is untouched (a hand-off charges content).

The budget hold, the miss/timeout handling and Stop are the same as Fix open rules'. Unknown or to-do check keys are rejected (400).

  1. The placeholder check lets a fill through when a passage has the same placeholder twice. server/src/lib/content-agent.js:525-530: dropsPlaceholder only asks whether each placeholder still appears somewhere in the changed passage (changed.includes(placeholder)). The number guard writes its own placeholder in one fixed form ([figure, source]), so a paragraph with two unsourced figures often has the same placeholder twice.

    • Example: in "Teams save [figure, source] and cut costs by [figure, source].", a replace of "Teams save [figure, source]" with a sourced figure is kept, because the second copy still matches. trust:claimsSourced asks for exactly this kind of change.
    • Fix: count each placeholder before and after the change, and drop the suggestion when any count falls. Ideally, count it in what will actually be kept (the words and blocks after splitReplace), not in the raw after.
    • Add a test with a duplicate placeholder.
    • Smaller point: the editor reads only the agent's own placeholder forms in headings (placeholderIn(heading) in web/src/lib/content/placeholders.ts). Using the same rule for h1–h6 passages would stop a title like "… [2026 Update]" from blocking a heading change.
  2. Low, follow-up: the model isn't told to leave placeholders alone. The guard throws those suggestions away only after the model's call has been paid for. buildFixPrompt (content-agent.js:533, called at :830) gets no hand-off flag. For a hand-off, add one line along these lines: "Leave every [placeholder] exactly as it is: the person fills it; don't change or remove one."

  3. Low, follow-up: nothing on the server stops two runs at once on the same draft. Fix and Hand off both check for open suggestions and a Start/Rewrite claim (content-agent.js:653-666), but not for another Fix or hand-off run that's still going. A person's Hand off (or Fix) during the automatic run, or from a second tab, would make two model calls and charge both. The later batch would then hide the earlier one. This already happens with Fix, but the automatic run makes overlaps more likely.

    • Fix: before startRun, refuse with 409 (running) when the draft has a fix or handoff run that hasn't finished and isn't stale.
    • At the least, the web part should disable Hand off and Fix while either is running.

cihan-gyk and others added 2 commits October 25, 2026 10:09
…ne fix at a time (CE-051)

- dropsPlaceholder counts each placeholder before and after the change,
  read in what is kept (the new words and the blocks after them): filling
  one of two "[figure, source]" in a passage is dropped too. In a heading
  only the agent's own forms count, as the editor reads them.
- A hand-off's prompt says to leave every [placeholder] as it is.
- Fix open rules and a hand-off are refused with 409 `running` while
  another fix or hand-off on the draft is under way (fixUnderWay).

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

Copy link
Copy Markdown
Collaborator Author

Fixed in a1b8c2c. Main was merged forward first in 01aae69; the only conflict was schema.sql, rebuilt with build-schema.sh.

  1. Duplicate placeholders. dropsPlaceholder now counts each placeholder before and after the change, and drops the suggestion when any count falls.
    • It reads what will be kept: the passage with its new words, plus the text of the blocks splitReplace moves after it. A placeholder moved into a following paragraph still counts.
    • "Teams save [figure, source] and cut costs by [figure, source]." with the first one filled is now dropped. Tests are added for the plain case and through keepSuggestions.
    • In h1–h6 passages, only the agent's own forms count ([figure, source…], [name of a real person], [quote from a real person…]), as placeholderIn(heading) does in the editor. "Best CRM tools [2026 Update]" no longer blocks a heading change.
  2. Prompt line (follow-up done here). On a hand-off, buildFixPrompt adds: "Leave every [placeholder] exactly as it is: the person fills it; don't change or remove one." Fix open rules' prompt is unchanged. Tested both ways.
  3. Two runs at once (done here too).
    • fixDraftRules now refuses with 409 running while the draft has a fix or handoff run that hasn't finished and isn't stale (fixUnderWay in content-runs.js, within RUN_STALE_MS).
    • The check comes after the published, open-suggestions and writing checks, and before anything is charged. It covers Fix open rules and Hand off alike; the route docs list it.
    • A narrow race between the check and startRun remains: two clicks within a few milliseconds.
    • On the web, feat(content-editor): hand the page off to Content Agent from Quality, in one run (CE-051, part 3) #1484 already disables Hand off and Fix while either runs in the tab. It gets a message for running when I merge this forward.

Server: format, lint and npm test pass (2,539 passed).

cihan-gyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 25, 2026
… Hand off sends, and say `running` (CE-051)

- usePageRead's reading now counts a save not asked about yet (ansvisor#1468), so
  the hand-off's own `behind` flag goes. Hand off waits for the page's
  read before its first answer, or after a save while it sends what AI
  read of the page (Answer first, the disclosure, balance); not after
  every save.
- The API's 409 `running` (another fix or hand-off on the draft, ansvisor#1481)
  is said, for Fix open rules too.

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

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

All items done:

  • dropsPlaceholder counts each placeholder before and after the change, in what will be kept. In headings it counts only the agent's own forms, as the editor does.
  • On a hand-off, the prompt tells the model to leave placeholders as they are. Fix open rules' prompt is unchanged.
  • Fix and Hand off answer 409 running while another fix or hand-off on the draft is under way. This is checked before anything is charged. An automatic hand-off refused this way hasn't taken its claim yet, so it can still run later.

The merge of main (01aae69) changes nothing beyond the schema.sql rebuild.

I ran 00275 again after 00273 and 00274 against the current schema, in a rolled-back transaction. I tested it as an analyst, a manager and another organization's admin:

  • Only the manager can change the brand's setting. Another organization's admin can't see or change the brand, its drafts or its runs.
  • A new draft copies the brand's setting, even when the insert asks otherwise. Neither the analyst nor the manager can change it on the draft.
  • Members can read runs but can't insert or update them.
  • A second automatic hand-off on a draft is refused (23505). A person's hand-offs can repeat.
  • An unknown kind or origin is refused.
  • The prompt_improve_usage.feature check is untouched; a hand-off charges content, which it allows.

Two small follow-ups:

  1. Low: the new keepSuggestions case passes without the placeholder check. In content-agent.test.js:1342-1350, the filled suggestion writes "30%" with no source, so the number guard drops it anyway. Fill the placeholder with words instead (for example "Teams save hours"), so that only the placeholder check can drop it.
  2. Low: a stopped run still counts as under way until it ends. fixUnderWay (content-runs.js:53-63) doesn't skip stopped runs. When another server holds the stopped run, it ends only after its model call returns, which can take up to a minute. Until then, Stop followed by Hand off or Fix answers running. A stopped run keeps nothing, so the query can add .is('stopped_at', null), as stopDraftRun does.

@gkhngyk
gkhngyk merged commit 86140d8 into ansvisor:main Oct 25, 2026
4 of 5 checks passed
gkhngyk added a commit that referenced this pull request Oct 25, 2026