Repository navigation
Add ai hint polling - #1790
Merged
Merged
Add ai hint polling#1790
Conversation
cocomarine
temporarily deployed
to
previews/1790/merge
November 26, 2026 11:51 — with
GitHub Actions
Inactive
1 task done
cocomarine
marked this pull request as ready for review
November 26, 2026 14:59
Contributor
ApprovabilityVerdict: Needs Human Approval Macroscope's review found this PR requires human approval — AI hints now use bounded, cancellable polling instead of a synchronous request. The required backend PR remains open; confirm its endpoints, migration and worker queue are deployed before releasing this UI change to avoid losing hint availability. Needs human approval because:
No code changes detected at You can add or adjust custom eligibility rules. Learn more. |
zetter-rpf
approved these changes
Nov 30, 2026
cocomarine
added a commit
to RaspberryPiFoundation/editor-api
that referenced
this pull request
Nov 30, 2026
## Status - Part of RaspberryPiFoundation/digital-editor-issues#1932 Moves AI hint generation off the request thread. editor-ui PR: RaspberryPiFoundation/editor-ui#1790 The client creates a hint, gets an ID straight away, and polls until the hint is ready. ## Performance considerations - Web threads aren't blocked. The inference call can take up to 15s, and it now runs in the background, so POST /api/ai_hints returns 202 straight away. - AI hints have their own queue. The ai_hints queue runs up to 5 jobs at a time. A burst of hint requests can't hold up other jobs and vice versa. - Hints that are already too late fail straight away. If a hint has waited more than 15s in the queue, the job marks it failed without calling inference. This avoids spending inference calls on hints the client will never collect. - The worker has a bigger DB pool. The GoodJob queues add up to 22 threads, so the worker now sets DB_POOL=25 to stop threads waiting for a connection. Web dynos keep using RAILS_MAX_THREADS. - Polling is cheap. GET /api/ai_hints/:id reads one row by primary key and returns only status and hint. - Rows stay small. input, which can be up to 100 KB, is cleared once a hint completes or fails. - **Staging connection limits**: staging's main DB allows only 20 connections, compared with 200 in production, so the worker's pool and thread counts are now configurable per app: - Staging, with WORKER_DB_POOL=8 set (see below TODO before merging section), gets DB_POOL=8 - 2 Puma x 5 + spare (2) -> 8 left for the worker out of total 20 - Production, with it not set, gets DB_POOL=25 🤖 Generated with help of Claude Code ## What's changed? - AiHint model and migration: - Has user_id, input, status (pending / completed / failed) and hint. `input` is cleared once the hint completes or fails. - Api::AiHintsController: - POST /api/ai_hints validates the payload (requires message, trace, runtime and code; 100 KB max), creates a pending hint, queues the job and returns 202 with the id. - GET /api/ai_hints/:id returns status and hint, only for the hint's owner. - Both are behind the feature flag. - `GenerateAiHintJob` runs on a new ai_hints GoodJob queue with 5 threads. It calls `FriendlyErrorMessagesInferenceClient` and marks the hint completed or failed. - A hint that waited more than 15s in the queue is failed straight away: editor-ui polls for about 30s and inference can take up to 15s, so it couldn't finish in time. - DB pool: database.yml now reads `DB_POOL` first and falls back to RAILS_MAX_THREADS. The worker in the Procfile sets DB_POOL=25 so the extra queue threads don't run out of connections. - Added weekly cleanup job for old AI hints. ## Relationship to existing `FriendlyErrorMessagesController` - Its endpoint POST /api/friendly_error_messages stays as it is in this PR. - The new `AiHintsController` for the similar job of running the inference in the background will eventually replace it. - Both endpoints stay live until editor-ui has switched over to the new endpoints. After that, we should remove the existing controller and its routes and specs. ## TODO before merging - [x] Add these to the config_vars map in terraform.tfvars: ```bash ... WORKER_DB_POOL = "8" GOOD_JOB_QUEUES = "create_students_job:1;import_schools_job:1;salesforce_sync:2;default:1;ai_hints:2" ... ``` ## Steps to perform after deploying to production - 🏃♀️ Runs a migration (create_ai_hints). - 🛻 Needs the worker dyno to restart to pick up DB_POOL and the new queue. - 🧹 Once editor-ui has switched over to use new routes, remove `FriendlyErrorMessagesController` and its routes and specs.
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Warning
Merge after the editor-api PR is merged.
Status
Switches AI hints to the new async endpoints. The client creates a hint, then polls until it's ready, instead of holding one request open while inference runs.
What's changed?
createAiHint(POST /api/ai_hints) andgetAiHint(GET /api/ai_hints/:id), each with a 5s timeout, and removesgetAiErrorHint.pollAiHinthelper:MAX_QUEUE_WAITin editor-api.AiHintPollErrorwhosereasonisfailed,timeoutoraborted.AiErrorMessage: