Sidebar review runs: decide how the entity form sidebar should execute criteria
# Sidebar review runs: decide how the entity form sidebar should execute criteria
<!--
Planning issue, drafted with ai-workflow/plan.md per ai-workflow/template-routing.md.
Held back deliberately: #3585810 (MR !11) is not merged or approved yet, so this
is written as a decision to be taken rather than as work to be reviewed. Nothing
here is staged with that ticket.
-->
## Goal
Decide how the AI Review sidebar on entity edit forms should execute criteria, now that stepped execution exists for the review tab. Today the sidebar ignores a criterion's execution mode and runs everything in one request, which loses long runs and leaves their records stuck at `running`. The question for this issue is whether the sidebar should reuse the tab's poll cycle, get its own mechanism, or be reduced in scope.
## Background
The module has two independent ways to start a review:
1. The review tab, a form plus `js/review_run.js`, talking to JSON routes (`/run`, `/poll`, `/pause`, `/heartbeat`).
2. The sidebar on the entity edit form, a single HTMX POST to `run_row` that swaps one row's markup.
The stepped execution added in #3585810 is wired only into the first. `ReviewRunController::runRow()` calls `ReviewOrchestrator::runCriterion()` unconditionally and never reads `execution_mode`, so a criterion configured for polling still runs every turn inside that one POST from the sidebar.
That matters because of where the ceiling actually is. Measured on a local DDEV site with a real provider, by pairing the `Call provider` and `Response from provider` entries in dblog:
- one agent turn costs 20 to 24 seconds,
- a full review is three or more turns, so 60 seconds and up,
- PHP-FPM `request_terminate_timeout` kills the worker mid-request and is not a catchable PHP error, so no `try`/`catch`, shutdown function or `finally` in the module runs.
With the worker limit set below one turn, every run died on turn one in both modes, and stepping could not help: one turn per request is the finest granularity that exists. Raising the limit above a single turn is a prerequisite for any of this, and is a site configuration question rather than a module one. It is worth stating in the module's documentation, because "polling protects you from timeouts" is only true above that floor.
Three separate failures follow from a killed request, and they are worth keeping apart:
- **The work is lost.** Unavoidable for `direct`, which persists nothing by design. Avoidable for `polling`, but only if the sidebar actually steps.
- **The row never recovers.** HTMX leaves its in-flight class on the row when a POST does not answer, and nothing removes it, so the row spins until the page is reloaded.
- **The record is never closed.** It sits at `running` for good. On the test site 30 records had accumulated in that state across this work.
The second and third are independent of which execution mode the sidebar ends up using, so they are worth fixing either way.
## Proposed approach
Reuse everything that exists. Specifically:
- `runRow` asks the criterion's merged configuration for its execution mode, then either runs it (`runCriterion`, unchanged) or starts it (`startCriterion`, already used by the tab) and hands the row back still open.
- The browser drives an open row through the **existing** `/poll`, `/heartbeat` and `/pause` routes. No new endpoints, no new server-side protocol, no second definition of what a run is.
- One `GET` route is added, `render_row`, which re-renders a row without running anything. It is what lets a settled row come from the same server-side builder as a page load instead of being assembled in JavaScript.
- One shared row builder replaces the row-building logic that was already duplicated between `FormHooks` and `runRow` (and would have become a third copy). The decision "is a run in flight here" then cannot differ depending on how the row reached the screen.
Plus the two mode-independent lifecycle fixes:
- A poll whose criterion reports "done, no result" closes the record as failed with an explanation. That is the signature of a killed turn: nothing left to step, so the run is provably over.
- Starting a run closes any record still open for the same input, at the moment it is superseded. The duplicate guard has already loaded those records, so this costs no extra query, and it is why no cron reaper is needed.
### Why not simply reuse `js/review_run.js`?
This is the obvious question and it deserves a straight answer: the poll cycle is reused, the DOM code is not.
**Option A: share the JavaScript too.** The two surfaces have different markup and different lifetimes. `review_run.js` addresses cells by id (`#ai-review-status-{rowKey}`) and takes its run descriptors from `drupalSettings`, which works because the tab renders every row once and keeps them. The sidebar has no per-cell ids and swaps rows one at a time via HTMX, so `drupalSettings` attached to the original page does not describe a replacement row. Sharing would mean generalising both the markup and the descriptor lookup, i.e. changing the surface that is currently under review in #3585810.
**Option B: separate driver, shared server contract** (proposed). The sidebar reads its run descriptor from `data-*` attributes on the row, which survive an HTMX swap, and re-renders finished rows server-side rather than patching cells. Roughly 120 lines, no change to `review_run.js`, no change to the JSON routes. The duplicated part is the poll loop shape (poll, heartbeat, pause on `pagehide`), around 40 lines.
Option B is proposed because it leaves the code under review untouched. If maintainers would rather pay for the generalisation now, that is Option A and it should be a deliberate choice, not something smuggled in with this.
### Scope alternative
If reviewers would rather not have two driven surfaces at all, the honest smaller option is to make the sidebar refuse to start a polling criterion and link to the review tab instead. That is less capable but much less code, and it keeps exactly one driver in the module. Worth saying out loud before building the larger thing.
## Open questions
* Should the sidebar drive stepped runs at all, or defer to the review tab (see "Scope alternative")?
* If it should: separate driver sharing the server contract (Option B), or generalise `review_run.js` to cover both surfaces (Option A)?
* Should `execution_mode` remain per criterion, or is a site-wide default with per criterion override closer to what site builders want? The sidebar makes the per criterion choice much more visible, since two rows side by side can behave differently.
* The two lifecycle fixes (closing a dead run on poll, closing a superseded one) are independent of the above. Should they be split into their own issue so they can land regardless of how this is decided?
* Is `request_terminate_timeout` above one agent turn something the module should document as a requirement, or check and warn about at runtime?
## Resources
* #3585810 Use Agent polling service
## Decision
<!-- Fill in before closing: summarise what was decided and the key reason. Leave empty until resolved. -->
<!-- If this issue description was significantly AI-generated (entire sections, not autocomplete), please note it in a comment below. See https://www.drupal.org/docs/develop/issues/issue-procedures-and-etiquette/policy-on-the-use-of-ai-when-contributing-to-drupal -->
task
GitLab AI Context
Project: project/ai_content_review
Instance: https://git.drupalcode.org
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://git.drupalcode.org/project/ai_content_review/-/raw/1.x/README.md — project overview and setup
- https://git.drupalcode.org/project/ai_content_review/-/raw/1.x/CLAUDE.md — Claude Code instructions
Repository: https://git.drupalcode.org/project/ai_content_review
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD