Skip to content

fix: update CP immediately after predicting#4504

Open
SylvainChevalier wants to merge 2 commits intomainfrom
claude/issue-4437-update-the-cp-on-predict
Open

fix: update CP immediately after predicting#4504
SylvainChevalier wants to merge 2 commits intomainfrom
claude/issue-4437-update-the-cp-on-predict

Conversation

@SylvainChevalier
Copy link
Copy Markdown
Contributor

@SylvainChevalier SylvainChevalier commented Mar 17, 2026

Closes #4437

Summary

  • After submitting a prediction, fetch fresh post data (including updated CP) from the API, matching the reaffirm flow pattern
  • Previously, the predict flow relied solely on revalidatePath which didn't reliably update the client-side CP display

Test plan

  • Predict on a binary question and verify the CP updates immediately
  • Predict on a continuous question and verify the CP updates
  • Predict on a multiple choice question and verify the CP updates
  • Reaffirm a prediction and verify it still works correctly

Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • ForecastMaker now refreshes prediction data before submission, ensuring you're always working with the most current information when making predictions.

After submitting a prediction, fetch fresh post data (including updated
CP) from the API, matching the reaffirm flow pattern. Previously, the
predict flow relied solely on revalidatePath which didn't reliably
update the client-side CP display.

Closes #4437

Co-authored-by: Sylvain <SylvainChevalier@users.noreply.github.com>
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 17, 2026

Warning

Rate limit exceeded

@claude[bot] has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 59 minutes and 35 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 59 minutes and 35 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7d54436a-9d80-4161-839b-9f678c8f2730

📥 Commits

Reviewing files that changed from the base of the PR and between 021e1c6 and 4bb0bb7.

📒 Files selected for processing (2)
  • front_end/src/app/(main)/questions/[id]/components/question_view/forecaster_question_view/index.tsx
  • front_end/src/components/forecast_maker/index.tsx
📝 Walkthrough

Walkthrough

ForecastMaker component now maintains a local currentPost state synchronized with the incoming post prop. The onPredictionSubmit callback is wrapped to fetch the latest post data before delegating to the original handler, ensuring rendered state reflects current predictions immediately.

Changes

Cohort / File(s) Summary
ForecastMaker Local State & Callback Wrapping
front_end/src/components/forecast_maker/index.tsx
Added local currentPost state with useEffect sync, wrapped onPredictionSubmit to asynchronously fetch fresh post data via ClientPostsApi.getPost() before invoking original callback, and updated downstream component props to use currentPost.

Sequence Diagram

sequenceDiagram
    participant User
    participant ForecastMaker
    participant ClientPostsApi
    participant ChildComponents

    User->>ForecastMaker: Click Predict
    ForecastMaker->>ForecastMaker: handlePredictionSubmit triggered
    ForecastMaker->>ClientPostsApi: getPost(currentPost.id)
    activate ClientPostsApi
    ClientPostsApi-->>ForecastMaker: Fresh post data
    deactivate ClientPostsApi
    ForecastMaker->>ForecastMaker: Update currentPost state
    ForecastMaker->>ForecastMaker: Call original onPredictionSubmit
    ForecastMaker->>ChildComponents: Pass updated post & handlePredictionSubmit
    ChildComponents-->>User: Render with updated CP
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • feat: add pre-predictions #4557: Also modifies ForecastMaker component to compute and pass predictLabel, with overlapping changes to the same file requiring coordinated review.

Suggested reviewers

  • ncarazon
  • lsabor
  • hlbmtc

Poem

🐰 Predict, and see it bloom so bright,
Fresh numbers dancing in the light,
No waiting for a second click,
The CP updates—swift and quick!

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: updating CP (Community Prediction) immediately after predicting, which directly addresses the PR's primary objective.
Linked Issues check ✅ Passed The code changes implement the core requirement from #4437 by fetching fresh post data with updated CP after prediction submission, mirroring the reaffirm flow pattern.
Out of Scope Changes check ✅ Passed All changes are scoped to ForecastMaker component's prediction handling, directly addressing the linked issue requirement with no unrelated modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/issue-4437-update-the-cp-on-predict

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Mar 17, 2026

🚀 Preview Environment

Your preview environment is ready!

Resource Details
🌐 Preview URL https://metaculus-pr-4504-claude-issue-4437-update-the-c-preview.mtcl.cc
📦 Docker Image ghcr.io/metaculus/metaculus:claude-issue-4437-update-the-cp-on-predict-4bb0bb7
🗄️ PostgreSQL NeonDB branch preview/pr-4504-claude-issue-4437-update-the-c
Redis Fly Redis mtc-redis-pr-4504-claude-issue-4437-update-the-c

Details

  • Commit: 4bb0bb71b30894522b06365163750f3231157a75
  • Branch: claude/issue-4437-update-the-cp-on-predict
  • Fly App: metaculus-pr-4504-claude-issue-4437-update-the-c

ℹ️ Preview Environment Info

Isolation:

  • PostgreSQL and Redis are fully isolated from production
  • Each PR gets its own database branch and Redis instance
  • Changes pushed to this PR will trigger a new deployment

Limitations:

  • Background workers and cron jobs are not deployed in preview environments
  • If you need to test background jobs, use Heroku staging environments

Cleanup:

  • This preview will be automatically destroyed when the PR is closed

@SylvainChevalier
Copy link
Copy Markdown
Contributor Author

Seems I can't test this because the preview env doesn't recompute CP at all?

@SylvainChevalier SylvainChevalier marked this pull request as ready for review April 7, 2026 21:30
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
front_end/src/components/forecast_maker/index.tsx (1)

35-37: Add minimal error telemetry for CP refresh failures.

Line 35-37 silently swallows refresh errors, which makes production debugging hard when CP does not update as expected.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@front_end/src/components/forecast_maker/index.tsx` around lines 35 - 37, The
empty catch in the forecast_maker component swallows CP refresh errors; change
the anonymous catch to capture the error (catch (err)) and emit minimal
telemetry and/or log the error so failures are visible in production (e.g., call
your telemetry helper like sendTelemetry or trackError with an event name such
as "CPRefreshFailed" and include err.message/stack), then keep the existing
comment/behavior if desired; update the catch in
front_end/src/components/forecast_maker/index.tsx accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@front_end/src/components/forecast_maker/index.tsx`:
- Around line 31-35: The async fetch in handlePredictionSubmit uses
currentPost.id then unconditionally calls setCurrentPost with the response,
which lets a stale response overwrite newer state; fix by capturing the
request's intended id (e.g., const requestedId = currentPost.id) before awaiting
ClientPostsApi.getPost(requestedId) and, after the await, verify the component
still intends to update that same id (compare requestedId to the latest
currentPost.id or an up-to-date ref) before calling setCurrentPost;
alternatively cancel/ignore responses with an AbortController or mounted flag so
setCurrentPost only runs for the latest in-flight request.

---

Nitpick comments:
In `@front_end/src/components/forecast_maker/index.tsx`:
- Around line 35-37: The empty catch in the forecast_maker component swallows CP
refresh errors; change the anonymous catch to capture the error (catch (err))
and emit minimal telemetry and/or log the error so failures are visible in
production (e.g., call your telemetry helper like sendTelemetry or trackError
with an event name such as "CPRefreshFailed" and include err.message/stack),
then keep the existing comment/behavior if desired; update the catch in
front_end/src/components/forecast_maker/index.tsx accordingly.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f489c6e1-eb20-45be-88a3-6528357d669b

📥 Commits

Reviewing files that changed from the base of the PR and between 8c53a55 and 021e1c6.

📒 Files selected for processing (1)
  • front_end/src/components/forecast_maker/index.tsx

Comment thread front_end/src/components/forecast_maker/index.tsx Outdated
Comment on lines +41 to +45
const {
group_of_questions: groupOfQuestions,
conditional,
question,
} = currentPost;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fresh post only threads into ForecastMaker subtree. Sibling DetailedQuestionCard / DetailedGroupCard in forecaster_question_view still render stale CP from parent postData until revalidatePath lands. If #4437 is about the chart, this won't fix it – lift the refetch to the parent page.

Comment on lines +32 to +34
try {
const freshPost = await ClientPostsApi.getPost(currentPost.id);
setCurrentPost(freshPost);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Double-fetch in prediction flow: prediction_flow_post.tsx:62 already refetches the post in its own onPredictionSubmit. This wrapper now fires a second GET /posts/:id right before it.

Comment on lines +25 to +27
useEffect(() => {
setCurrentPost(post);
}, [post]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Race: local setCurrentPost(fresh) can be overwritten moments later when the parent re-renders with a staler post prop from revalidatePath, via the useEffect sync. Causes a flash back to stale CP.

@SylvainChevalier
Copy link
Copy Markdown
Contributor Author

@claude please make requested changes.

@claude
Copy link
Copy Markdown
Contributor

claude Bot commented Apr 15, 2026

Claude finished @SylvainChevalier's task in 7m 49s —— View job


Changes pushed ✓

Addressed all three review points from @ncarazon and the stale-response concern from @coderabbitai by lifting the post refetch out of ForecastMaker and into the parent ForecasterQuestionView.

Why this is the right shape

ForecasterQuestionView renders QuestionHeader, DetailedQuestionCard / DetailedGroupCard, and ForecastMaker as siblings, all off the same postData prop from the server. Refreshing state inside ForecastMaker never reaches the chart — which is what #4437 is about. Lifting the state so all siblings receive the fresh currentPost fixes that.

Concerns addressed

  • Sibling stale CP (ncarazon, line 45)DetailedQuestionCard / DetailedGroupCard now consume currentPost from ForecasterQuestionView's local state, so they re-render with the refreshed CP immediately.
  • Double-fetch in prediction flow (ncarazon, line 34)prediction_flow_post.tsx:62 already refetches on submit. With ForecastMaker reverted to a pass-through, there's no wrapper firing a second GET /posts/:id. The prediction-flow path is unchanged.
  • Race on revalidatePath (ncarazon, line 27 / coderabbit, line 34) — Guarded with a latestRequestIdRef: the response is only applied if its request id is still the latest. If the parent re-renders with a staler postData (e.g., from revalidatePath), useEffect syncs, which is safe because any subsequent local refetch will bump the ref.
  • Silent catch (coderabbit, line 35) — Errors now go to console.error so production failures are visible.

Files changed

  • front_end/src/components/forecast_maker/index.tsx — reverted to a simple pass-through (removed local state, useEffect, and callback wrapping)
  • front_end/src/app/(main)/questions/[id]/components/question_view/forecaster_question_view/index.tsx — converted to a client component ("use client"), added local currentPost state with prop sync, handlePredictionSubmit that refetches via ClientPostsApi.getPost(), race protection via latestRequestIdRef, logged errors

Tasks

  • Read current forecast_maker/index.tsx
  • Analyze forecaster_question_view parent
  • Check prediction_flow_post.tsx double-fetch (confirmed at line 62-79)
  • Determine fix approach (lift refetch to parent)
  • Revert ForecastMaker to simple passthrough
  • Convert ForecasterQuestionView to client component with local state + refetch
  • Commit and push

Notes

  • I couldn't run tsc/next lint in this sandbox (commands blocked), so CI on the updated preview environment will need to confirm types/lint. The edit is pure TypeScript/React (no new deps) and mirrors the existing reaffirm / prediction-flow patterns, so I don't expect surprises.
  • Preview env caveat still applies — as Sylvain noted on 2026-04-07, the preview environment doesn't recompute CP at all, so a deployed test needs staging or production to confirm the behavioral fix end-to-end.

Commit: 4bb0bb7 • • claude/issue-4437-update-the-cp-on-predict

Move the post-prediction refetch out of ForecastMaker and into the
parent ForecasterQuestionView so that siblings (DetailedQuestionCard /
DetailedGroupCard) also render the fresh CP immediately, not just the
forecast maker subtree. Also avoids double-fetching in the prediction
flow, where prediction_flow_post already refetches on submit, and
protects against stale in-flight responses via a request-id ref.

Addresses review feedback from @ncarazon and @coderabbitai on #4504.

Co-authored-by: Sylvain <SylvainChevalier@users.noreply.github.com>
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.

Update the CP on predict

2 participants