fix(simulation): make create_simulation idempotent to prevent duplicate sims#690
Open
alreadyhavefeel wants to merge 1 commit into
Open
fix(simulation): make create_simulation idempotent to prevent duplicate sims#690alreadyhavefeel wants to merge 1 commit into
alreadyhavefeel wants to merge 1 commit into
Conversation
…te sims Creating a simulation was not idempotent: each POST /api/simulation/create minted a fresh sim_<uuid>. The frontend wraps createSimulation in requestWithRetry(..., 3, 1000), so a slow/timed-out first request triggers a retry that creates a second identical simulation for the same project+graph (observed: two sims created ~2s apart). Guard against this by reusing an existing, not-yet-run simulation for the same project_id + graph_id (and matching platform flags) when status is CREATED/PREPARING/READY and current_round == 0. A force_new=True escape hatch preserves the ability to intentionally create a fresh simulation. Verified: repeated /create calls now return the same simulation_id instead of spawning duplicates. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Problem
Creating a simulation is not idempotent. Each
POST /api/simulation/createmints a freshsim_<uuid>viaSimulationManager.create_simulation. The frontend calls it throughrequestWithRetry(() => service.post('/api/simulation/create', data), 3, 1000)(frontend/src/api/simulation.js), so when the first request is slow or times out, the automatic retry fires a second create for the same project + graph.Result: two identical simulations for the same project/graph, created ~2 seconds apart (matching the 1000 ms retry interval), which is confusing in the Simulation History UI.
Fix
Add an idempotency guard at the top of
create_simulation: before minting a new id, reuse an existing simulation for the sameproject_id+graph_id(and matchingenable_twitter/enable_reddit) when it has not been run yet —current_round == 0and status inCREATED/PREPARING/READY.A
force_new=Trueparameter is added as an escape hatch for callers that deliberately want a brand-new simulation.This makes the POST safe to retry (the real root cause is a non-idempotent POST behind an auto-retry).
Verification
Repeated
POST /api/simulation/createfor the same project now returns the samesimulation_idinstead of spawning duplicates.Scope
Single-file change:
backend/app/services/simulation_manager.py. No API signature change (the new param is optional and defaults to current behavior for fresh projects).