Probe suggestion status before catching up a waiting frame

Merged
JO
2d ago
+697-57

Replaces #5870, which came from a fork branch and so couldn't join the stack. Its earlier bot review threads are there.

Stacked on #5827

This follows the bill-suggestion catch-up logic in #5827. It stays a draft until #5827 lands, then should be retargeted to refactor/ai-plumbing.

Problem

The waiting frame navigated to #show as soon as the ActionCable subscription reported connected, guarded by a sessionStorage key written once per suggestion id. That key was never cleared, so a still-pending answer could navigate before the result was stored, and a later open could trust a flag left by an earlier session.

Behaviour

  • The Stimulus controller asks #show for { status } when the cable subscribes (on connect, or on the connected mutation) and navigates the frame only for a terminal status — complete, failed or cancelled. A pending answer leaves the waiting frame in place for the broadcast.
  • #show in both suggestion controllers answers JSON with only the suggestion status. The stored proposal and rationale are not part of that response.
  • The one-shot sessionStorage flag is gone; a reopened dialog always checks the current status.

Validation

  • Full Rails suite on this branch: 19,330 runs / 212,938 assertions / 0 failures / 0 errors / 32 skips.
  • JS controller test (node --test test/javascript/ai_suggestion_controller_test.mjs): 5 tests, 5 pass.
  • Focused controller tests: 30 runs / 189 assertions / 0 failures / 0 errors.
  • Biome check, erb_lint and RuboCop are clean.

Summary by cubic

Fixes the AI suggestion waiting frame so it never navigates to #show before the answer is final, and enforces that all model calls run inside jobs rather than on web threads.

  • The waiting frame probes #show for { status } when the cable subscribes and navigates only for a terminal status (complete, failed, cancelled). A failed probe is retried up to three times with backoff, and a cable reconnect asks again, so a missed broadcast can still resolve.
  • This replaces the sticky one-shot sessionStorage flag: a pending answer keeps the waiting frame up for the broadcast, and a reopened dialog always rechecks the server. The JSON probe returns only the status, never the stored proposal or rationale.
  • Every transport call (generate, judge, embed, ocr, probe) runs Ai::Capsule.check!, which raises outside a job or capsule in development and test. Web requests and non-AI jobs that call a transport directly fail; tests that call the plumbing directly opt in via ai_capsule!.
  • Framework feature requesters, tool calls, and document imports hand work to worker jobs instead of calling transports on the web thread.
  • Only evals may call a model outside a job: Eval::GenerationClient and lib/tasks/evals.rake wrap runs in Ai::Capsule.outside.
  • The new Sure/AiOutsideCapsule RuboCop cop flags every hand-set capsule mark (outside, within, enter, leave, mark, and direct Fiber writes) outside app/models/eval/** and lib/tasks/evals.rake; within is allowed only in ApplicationJob.
  • Architecture tests cover the capsule boundary and that a replayed or redelivered step never reports a generation batch or run ending twice.

Refactors

  • McpController leaves the tool wait to Ai::ToolJob.run's default timeout; the hand-off test now checks only that the tool reaches the job.
  • The AI pipeline guide and cop test now describe the widened AiOutsideCapsule cop, covering each hand-set mark and its allowlist.

Summary by CodeRabbit

  • Bug Fixes
    • Suggestion dialogs now update reliably when a suggestion finishes, fails, or is cancelled, including when the connection is established late.
    • Pending suggestions and unsuccessful status checks no longer trigger an unexpected navigation.
    • Status requests return only the suggestion’s status, without including stored details such as bill names or rationale.