Repository navigation
fix(tracking): never re-analyze an already-analyzed prompt in analyze-new - #278
Merged
Merged
Conversation
…-new The POST /api/tracking/analyze-new explicit-promptIds branch only validated brand membership and is_active, skipping the prompt_results check that the auto-detect branch and GET /unanalyzed already apply. Because the client always sends specific IDs, an already-analyzed prompt could be re-submitted — most notably during the async webhook window, when results from an in-flight on-demand run have not all landed yet and the prompt still reads as unanalyzed. This caused the same prompts to be analyzed multiple times, double-spending Cloro/LLM credits. Move the prompt_results exclusion into a single shared block that runs for both branches, so this endpoint only ever analyzes prompts with no results for the brand. Full re-runs remain available via /check and the daily scheduled job, which are unchanged.
There was a problem hiding this comment.
Pull request overview
Fixes a production credit double-spend bug in POST /api/tracking/analyze-new by ensuring prompts that already have prompt_results for a brand are never re-submitted for analysis, even when the client provides explicit promptIds.
Changes:
- Refactors
/analyze-newto build a single “candidate prompt ID” set for both explicit-ID and auto-detect flows. - Applies the
prompt_resultsexclusion filter to that shared candidate set so already-analyzed prompts are always excluded before enqueuing a job.
Comments suppressed due to low confidence (1)
server/src/routes/tracking.js:165
- The
newPromptIds.length === 0branch always returns"All prompts already analyzed". When the client sendspromptIdsbut none are valid/active for this brand,candidateIdsbecomes empty and this message is misleading (nothing was analyzed because nothing was eligible). Consider returning a more accurate message in that case to aid debugging and avoid confusing UI states.
if (newPromptIds.length === 0) {
return res.json({ success: true, newCount: 0, message: 'All prompts already analyzed' });
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
POST /api/tracking/analyze-new("Analyze Prompts" on the brand prompts page) couldre-analyze prompts that already had results, double-spending Cloro/LLM credits.
The handler had two branches:
promptIds) — excluded already-analyzed prompts viaprompt_results.promptIds) — only validated brand membership +is_active, never checkedprompt_results.The client always sends specific IDs, so the explicit branch was the live path and the
exclusion was effectively bypassed. In cloud, Cloro runs asynchronously via webhook and
result rows land over several minutes; reopening the dialog during that window showed the
prompts as still "unanalyzed", letting them be re-submitted. Observed in production: the
same set of prompts analyzed three times within ~75 minutes.
This moves the
prompt_resultsexclusion into a single shared block that runs for bothbranches, so the endpoint only ever analyzes prompts with no results for the brand —
regardless of what the client sends. Full re-analysis remains available via
POST /api/tracking/checkand the daily scheduled job, both unchanged.Related issue
None — direct production bug fix (credit double-spend).
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
{ success: true, newCount: 0, message: "All prompts already analyzed" }and no job is queued.POST /api/tracking/checkstill re-run all prompts as before.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)