Skip to content

fix(tracking): stop progress bar freezing on webhook-mode runs - #279

Merged
gkhngyk merged 2 commits into
mainfrom
fix/tracking-progress-stuck
Jun 21, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
fix/tracking-progress-stuck

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Summary

The analysis progress bar (insights page loading banner) could freeze partway
on webhook-mode tracking runs while results kept arriving in the background —
refreshing the page then showed the saved results. Most visible on large brands
whose runs take ~45 minutes.

Root cause is in the webhook-mode drain loop in tracking-worker.js. While
waiting for Cloro to push results, it counted every cloro_pending_tasks
row for the brand:

.from('cloro_pending_tasks').eq('brand_id', brandId)   // brand-wide, not this run

Tasks Cloro never delivers a webhook for are never cleaned up, so they pile up
as orphan rows (found rows 9–28 days old in production). A brand-wide count is
poisoned by those orphans (and by concurrent runs): it never reaches zero, the
loop runs to its 60-minute deadline, and processed = expected - pending
plateaus below the real value — the bar freezes. The /cloro/callback handler
keeps inserting the genuine results into prompt_results independently, which
is why a refresh shows them.

Changes

  • Job-scoped drain count — count only the task_ids this run submitted.
    Brand-scoped read (a handful of rows) intersected in memory with the run's own
    ids, avoiding an oversized .in(...) URL. The loop now finishes as soon as
    this run's results are in.
  • Stall guard — give up after ~10 min of no new delivery instead of holding
    a worker (and a concurrency slot) for the full hour when Cloro drops tasks.
  • Orphan sweep — new cleanupStalePendingTasks() deletes pending rows older
    than 2h (well past the 60-min drain deadline). Runs at startup and from
    runDailyTracking, so it covers both cloud and self-hosted.
  • Client banner TTL — bump the saved-job TTL in tracking-job-store.ts from
    10 min to 60 min so the progress banner survives a refresh during a long run.

Related issue

None — production bug fix (frozen progress bar / orphaned pending tasks).

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. Trigger an analysis for a brand with many prompts (webhook mode, CLORO_WEBHOOK_URL set).
  2. Watch the insights progress banner: it should advance to completion as results land, not stall partway.
  3. Refresh mid-run (after >10 min): the banner is restored and keeps updating (previously it disappeared at 10 min).
  4. Leave an undelivered task in cloro_pending_tasks older than 2h; after a server restart / daily run it is swept and no longer affects future runs' counts.

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

Mostly server-side (server/src/...); node --check + npx prettier --check pass on the changed server files. The one web/ change (tracking-job-store.ts) passes lint/typecheck/format:check.

The webhook-mode drain loop counted every cloro_pending_tasks row for the
brand, not just the task_ids the current run submitted. Tasks Cloro never
delivered a webhook for are never cleaned up, so they accumulate as orphan
rows (observed: rows 9-28 days old). A brand-wide count is therefore poisoned
by those orphans (and by concurrent runs): it never reaches zero, the loop
runs to its 60-minute deadline, and the progress bar freezes partway even
though real results keep landing via the callback — so a refresh shows the
saved results while the banner looked stuck. Larger brands (~45-minute runs,
more tasks) hit this most.

- Drain loop now counts only this run's own task_ids (brand-scoped read
  intersected in memory, so no oversized .in(...) URL), and gives up early if
  delivery stalls for ~10 min instead of holding a worker for the full hour.
- Add cleanupStalePendingTasks() to sweep orphan rows older than 2h; run it at
  startup and from runDailyTracking (covers both cloud and self-hosted).
- Bump the client's saved-job TTL 10m -> 60m so the progress banner survives a
  refresh during a legitimately long run.

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 webhook-mode tracking runs where the insights progress banner could freeze due to counting brand-wide cloro_pending_tasks (including orphans / concurrent runs) instead of tasks submitted by the current job, and adds cleanup/TTL adjustments to make long runs more resilient.

Changes:

  • Scope the webhook-mode drain loop to the current run’s submitted task_ids and add a stall guard to stop holding a worker for up to an hour.
  • Add a periodic orphan sweep (cleanupStalePendingTasks()) for old cloro_pending_tasks rows (startup + daily tracking).
  • Extend the client-side stored tracking job TTL from 10 minutes to 60 minutes to survive refreshes during long runs.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
web/src/lib/tracking-job-store.ts Extends localStorage TTL for restoring the progress banner during long runs.
server/src/workers/tracking-worker.js Fixes drain-loop counting to be job-scoped and adds an early stall cutoff.
server/src/server.js Invokes stale pending-task cleanup at startup and before daily tracking runs.
server/src/lib/job-manager.js Adds cleanupStalePendingTasks() to delete orphaned pending-task rows.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread server/src/workers/tracking-worker.js Resolved
Comment thread server/src/lib/job-manager.js Resolved
…-only cleanup

- Drain loop: a transient cloro_pending_tasks read error no longer counts as
  zero pending (which would end the run early and report it complete while
  tasks are still in flight). Skip the tick and retry on the next poll.
- cleanupStalePendingTasks: use delete({ count: 'exact' }) instead of selecting
  every deleted task_id just to log the total.
@gkhngyk
gkhngyk merged commit 0f4cc60 into main Jun 21, 2026
3 checks passed
@gkhngyk
gkhngyk deleted the fix/tracking-progress-stuck branch June 21, 2026 12:36
@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