Skip to content

fix(tracking): never re-analyze an already-analyzed prompt in analyze-new - #278

Merged
gkhngyk merged 1 commit into
mainfrom
fix/analyze-new-skip-already-analyzed
Jun 21, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/analyze-new-skip-already-analyzed

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Summary

POST /api/tracking/analyze-new ("Analyze Prompts" on the brand prompts page) could
re-analyze prompts that already had results, double-spending Cloro/LLM credits.

The handler had two branches:

  • auto-detect (no promptIds) — excluded already-analyzed prompts via prompt_results.
  • explicit IDs (client sends promptIds) — only validated brand membership + is_active, never checked prompt_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_results exclusion into a single shared block that runs for both
branches, 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/check and the daily scheduled job, both unchanged.

Related issue

None — direct production bug fix (credit double-spend).

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. Pick a brand with at least one analyzed prompt.
  2. From the prompts page, open Analyze Prompts, select an already-analyzed prompt, and submit.
  3. Before: a new on-demand tracking job is created for it. After: the endpoint returns
    { success: true, newCount: 0, message: "All prompts already analyzed" } and no job is queued.
  4. Confirm a genuinely new (never-analyzed) prompt still gets analyzed normally.
  5. Confirm the daily scheduled job and POST /api/tracking/check still re-run all prompts as before.

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

lint/typecheck/format:check skipped — server-side change only (server/src/routes/tracking.js), no web/ source touched. node --check passes on the modified file.

…-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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-new to build a single “candidate prompt ID” set for both explicit-ID and auto-detect flows.
  • Applies the prompt_results exclusion 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 === 0 branch always returns "All prompts already analyzed". When the client sends promptIds but none are valid/active for this brand, candidateIds becomes 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.

@gkhngyk
gkhngyk merged commit 478890e into main Jun 21, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the fix/analyze-new-skip-already-analyzed branch June 21, 2026 12:14
@gkhngyk gkhngyk mentioned this pull request Jun 21, 2026
4 of 12 tasks
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