fix: reduce Convex moderation churn causing excessive bandwidth and action compute#48
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
This comment was marked as spam.
This comment was marked as spam.
4106e26 to
db9e88d
Compare
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
convex/contentAnalysis.test.ts (1)
9-73: Please add direct coverage for the new scheduler state machine.These helper assertions are good, but the churn fix in this PR lives in
scheduleAnalyzeRecentImagesRun()/claimScheduledAnalyzeRecentImagesRun(). I’d add cases for “later run ignored”, “earlier run replaces token”, and “stale token no-ops” so the dedupe behavior has direct regression coverage.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@convex/contentAnalysis.test.ts` around lines 9 - 73, Add direct unit tests for the scheduler state machine around scheduleAnalyzeRecentImagesRun and claimScheduledAnalyzeRecentImagesRun: add three tests that (1) schedule a run, then attempt to schedule a later run and assert the later run is ignored (the original token/time remains), (2) schedule a run then schedule an earlier run and assert the earlier run replaces the existing token/time, and (3) simulate a stale token (e.g., claim with an old timestamp or after the run has been processed) and assert claimScheduledAnalyzeRecentImagesRun becomes a no-op and does not re-schedule or mutate state; use the same helper setup used by existing tests to create provider health/queuedImage state and assert final scheduler state (token/timestamp) and return values from scheduleAnalyzeRecentImagesRun/claimScheduledAnalyzeRecentImagesRun to validate dedupe behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@convex/contentAnalysis.ts`:
- Around line 132-147: getProviderRecoveryDelayMs currently returns null when
there are unavailable providers but none have a future rateLimitedUntil, which
stalls scheduling; change it so that if any providerHealths entries are present
and not isAvailable but unavailableResets is empty, the function returns
DELAY_BETWEEN_REQUESTS_MS (instead of null). Locate getProviderRecoveryDelayMs
and its usage of providerHealths / ProviderHealthSnapshot / isAvailable /
rateLimitedUntil, detect the case where unavailable providers exist but no
numeric reset times, and return DELAY_BETWEEN_REQUESTS_MS so analyzeRecentImages
will reschedule retries rather than stopping.
In `@convex/generatedImages.ts`:
- Around line 1196-1209: The fallback query storing legacyUndefinedSensitivity
uses ctx.db.query("generatedImages").filter(...) with
q.eq(q.field("isSensitive"), undefined) which forces a table-scan; fix by making
this path indexable — either backfill legacy rows so isSensitive is explicitly
set (e.g., null) and then change the query to use q.eq(q.field("isSensitive"),
null) (or another indexed sentinel) or add a dedicated indexed flag (e.g.,
legacyRecoveryNeeded) and query on that field instead; update the code that
computes legacyUndefinedSensitivity and any recovery loop to rely on the indexed
condition (and run a one-time backfill job to mutate existing rows) so the query
avoids full-table scans.
---
Nitpick comments:
In `@convex/contentAnalysis.test.ts`:
- Around line 9-73: Add direct unit tests for the scheduler state machine around
scheduleAnalyzeRecentImagesRun and claimScheduledAnalyzeRecentImagesRun: add
three tests that (1) schedule a run, then attempt to schedule a later run and
assert the later run is ignored (the original token/time remains), (2) schedule
a run then schedule an earlier run and assert the earlier run replaces the
existing token/time, and (3) simulate a stale token (e.g., claim with an old
timestamp or after the run has been processed) and assert
claimScheduledAnalyzeRecentImagesRun becomes a no-op and does not re-schedule or
mutate state; use the same helper setup used by existing tests to create
provider health/queuedImage state and assert final scheduler state
(token/timestamp) and return values from
scheduleAnalyzeRecentImagesRun/claimScheduledAnalyzeRecentImagesRun to validate
dedupe behavior.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 20bf2514-6263-4731-82ab-86e904bb4802
⛔ Files ignored due to path filters (1)
convex/_generated/api.d.tsis excluded by!**/_generated/**
📒 Files selected for processing (8)
convex/contentAnalysis.test.tsconvex/contentAnalysis.tsconvex/crons.tsconvex/generatedImages.tsconvex/lib/providerHealthFunctions.tsconvex/schema.tsworkers/bloomstudio-worker/src/moderation.tsworkers/bloomstudio-worker/src/types.ts
💤 Files with no reviewable changes (1)
- workers/bloomstudio-worker/src/types.ts
|
Caution Review failedPull request was closed or merged during review No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (8)
📝 WalkthroughWalkthroughThis PR implements background job scheduling for content analysis with provider health-aware recovery delays, introduces legacy moderation state normalization with batch processing, refactors the worker moderation claim flow to return full response objects, and adds supporting database schema indexes and type infrastructure. Changes
Sequence Diagram(s)No sequence diagrams generated. While the changes introduce new background job scheduling features, the interactions are primarily internal decision logic and state management rather than multi-component sequential flows that would benefit from visualization. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
This fixes the moderation recovery path that was driving runaway Convex usage.
In
convex/contentAnalysis.ts, recovery could keep rescheduling itself every ~2 seconds when providers were rate-limited, and multiple scheduled runs could overlap. This change makes recovery wait until provider reset windows and adds deduped scheduling so stale or overlapping runs no-op instead of fanning out.In
convex/generatedImages.tsandconvex/schema.ts,getRecoverableUnanalyzedImageswas scanning broad unresolved-image ranges and filtering most rows after reading them, which drove excessive database bandwidth. This change adds an index for recoverable pending moderation work and makes the query read that index directly, leaving only a narrow legacy fallback path.In
workers/bloomstudio-worker/src/moderation.tsandconvex/contentAnalysis.ts, moderation jobs were doing an extra claim/continue round-trip before work could start. This change returns the prompt or image URL directly from claim so the worker can begin immediately with fewer Convex calls.Validation
bun run typecheckbun run test -- convex/contentAnalysis.test.tsSummary by CodeRabbit
New Features
Improvements