Skip to content

fix(content): don't say the Content Agent couldn't run while it is still at work - #1350

Merged
gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/agent-fix-timeout
Oct 22, 2026
Merged

gkhngyk merged 1 commit into
ansvisor:mainfrom
cihan-gyk:fix/agent-fix-timeout

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Content Editor feedback item 8: Fix open rules with agent said "Content Agent couldn't run. Try again." while the agent was still working, and its suggestions showed up later.

Cause (from the code; I couldn't see the production logs): Fix open rules is synchronous. The server action calls POST /api/content-drafts/:id/suggestions and waits for the model call, which content-agent.js cuts at 60 s. When the answer is lost on the way (the page's function hitting the platform's time limit, or a gateway timeout), the editor's catch shows "couldn't run". Meanwhile the API finishes and inserts its batch.

Fix:

  • The editor's segment allows longer. A new content/editor/[draftId]/layout.tsx exports maxDuration = 300, as api/aeo/[...path] and api/agent/chat do. That covers the page's server actions.
  • Unknown failures wait for the agent's batch. fixDraftRules now returns a new unconfirmed error in two cases: the fetch threw, or the response wasn't the API's JSON with a code (a gateway's page). The editor treats a thrown action the same way.
    • On unconfirmed, fixRules reads getDraftSuggestions every 5 s (AGENT_POLL_MS) for up to 90 s from the click (FIX_WAIT_MS, past the 60 s call and the batch's save).
    • When an open batch lands, it shows as a normal run would (bar open, "Content Agent suggested N changes"). Only if none lands does it say "couldn't run". Fix open rules runs only with no open suggestions, so any open one is the agent's.
  • The API's own errors are shown at once, as before: ai_budget, no_subscription, empty, open_suggestions, published, and failed with its code.

Related issue

Content Editor feedback round (no 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. yarn test src/app/[locale]/(dashboard)/dashboard/content/editor covers two new cases:
    • the action throws, and the batch lands on the second look, so it shows with the success toast and no error;
    • unconfirmed comes back and nothing lands, so "couldn't run" appears only after 90 s.
  2. On a deployment: run Fix open rules on a long page. A slow run should end with the suggestions, never "couldn't run" followed by suggestions after a reload.

Checklist

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

Also run: yarn test (2632 tests pass) and yarn build.

🤖 Generated with Claude Code

…ill at work

Fix open rules waits on the agent's model call (up to 60 s), and an answer
lost on the way, a platform timeout, read as "Content Agent couldn't run"
while the API went on and left its suggestions. The editor's segment now
allows 300 s, as the API proxy's long routes do. When the answer doesn't come
back (the call threw, or a gateway answered without the API's code), the
editor looks for the agent's batch every 5 s for 90 s and shows it, saying it
couldn't run only if none lands. The API's own errors are shown at once.

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

Copy link
Copy Markdown
Collaborator Author

@gkhngyk this is item 8 from the editor feedback. I worked out the cause from the code, since I can't see the logs: if Vercel's logs show the editor's server action timing out around Fix open rules, that confirms it. The fix is maxDuration 300 on the editor segment, plus a 90 s look for the agent's batch when the answer doesn't come back. The API's own errors are unchanged.

@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. Three small follow-ups.

  1. web/src/lib/actions/content-drafts.ts:1563: any failed answer without a code becomes unconfirmed. That includes the API's own 401: the auth middleware answers { message }, for example on an expired session. The editor then waits 90 s before saying "couldn't run", for a run that never started. Keep unconfirmed for the cases where the run may have gone on: 502, 503 and 504, and a thrown fetch (:1570). Any other uncoded answer should stay failed.
  2. _draft-editor.tsx:123 and :595: the wait doesn't stop when the editor unmounts. If the person leaves the page or opens another draft, it keeps reading suggestions every 5 s. Up to 90 s later it toasts "suggested N changes" or "couldn't run" on whatever page they are on. Stop it on unmount: check a mounted ref in the loop and before the toasts, or pass an AbortSignal.
  3. web/src/lib/actions/content-drafts.test.ts:624: the action's new mapping has no test. Cover these cases:
    • a 504 with an HTML body, and a thrown fetch, both giving unconfirmed;
    • a 401 with { message }, giving failed once item 1 is done;
    • a coded 402, still giving its code.

The maxDuration = 300 in the segment's layout.tsx matches api/aeo/[...path] and api/agent/chat. The page is a client component, so the layout is the right place for it. The editor tests cover a batch that lands late and one that never lands.

Merge notes

  • Merges cleanly on main and with #1346, #1347, #1348, #1349, #1351 and #1353. No migration.
  • With #1346: the two new tests click 'Fix open rules with agent' (page.test.tsx:1670 and :1697). After #1346 that label is 'Apply rules with Agent'. There's no textual conflict, but the tests fail until the label is renamed. It's easiest to merge this before #1346 and rename them there.

Tested on top of main together with #1353, #1347, #1348, #1350 and #1351: web and server tests, tsc, lint and Prettier pass.

@gkhngyk
gkhngyk merged commit 73bea15 into ansvisor:main Oct 22, 2026
6 checks passed
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