danylokhodus commited on
Commit
46fc486
·
1 Parent(s): 3159e54

fix(api): fix EF double-track bug in UpdateFromAgent causing nodes to be deleted but not re-inserted - reorder SaveFullGraphAsync BEFORE UpdateAsync to avoid DbContext tracking conflict on same Graph entity

Browse files
FlowAPI/Controllers/AgentCanvasController.cs CHANGED
@@ -194,32 +194,40 @@ namespace FlowAPI.Controllers
194
  }
195
  }
196
 
197
- // If the title or description is provided, update the Graph metadata
198
- if (!string.IsNullOrWhiteSpace(schema.Title))
199
- {
200
- dbGraph.Title = schema.Title.Trim();
201
- }
202
- if (schema.Description != null)
203
- {
204
- dbGraph.Description = schema.Description;
205
- }
206
- await _graphRepo.UpdateAsync(dbGraph);
207
 
208
- // Perform Auto-Layout on the new nodes
209
  var fullGraph = AutoLayoutEngine.LayoutGraph(schema, dbGraph.UserId);
210
  fullGraph.Id = id; // Sync graph ID
211
 
212
- // Save all new nodes and edges, completely replacing the old graph structure
213
- var saveResult = await _graphService.SaveFullGraphAsync(id, fullGraph, dbGraph.UserId);
 
 
214
  if (!saveResult)
215
  {
216
- return BadRequest(new { message = "Failed to update generated task nodes in database." });
 
 
 
 
 
 
 
 
 
 
 
 
217
  }
218
 
219
  return Ok(new
220
  {
221
  success = true,
222
  graphId = id,
 
223
  canvasUrl = $"/canvas/{id}"
224
  });
225
  }
 
194
  }
195
  }
196
 
197
+ // Access check already done above; we'll apply title/description after SaveFullGraphAsync
198
+ // to avoid EF double-tracking conflicts (UpdateAsync + GetByIdWithDetailsAsync in SaveFullGraphAsync
199
+ // would both track the same Graph entity in the same DbContext scope).
 
 
 
 
 
 
 
200
 
201
+ // Perform Auto-Layout on the new nodes BEFORE mutating dbGraph further
202
  var fullGraph = AutoLayoutEngine.LayoutGraph(schema, dbGraph.UserId);
203
  fullGraph.Id = id; // Sync graph ID
204
 
205
+ // Save all new nodes and edges first (uses its own fresh DB context load)
206
+ // This must happen BEFORE UpdateAsync to avoid EF double-tracking the same Graph entity
207
+ var ownerUserId = dbGraph.UserId;
208
+ var saveResult = await _graphService.SaveFullGraphAsync(id, fullGraph, ownerUserId);
209
  if (!saveResult)
210
  {
211
+ return BadRequest(new { message = $"Failed to update generated task nodes in database. " +
212
+ $"Graph ID: {id}, Owner: {ownerUserId}, Nodes in payload: {fullGraph.Nodes?.Count ?? 0}" });
213
+ }
214
+
215
+ // NOW update the title/description on the freshly-saved graph
216
+ var freshGraph = await _graphRepo.GetByIdAsync(id);
217
+ if (freshGraph != null)
218
+ {
219
+ if (!string.IsNullOrWhiteSpace(schema.Title))
220
+ freshGraph.Title = schema.Title.Trim();
221
+ if (schema.Description != null)
222
+ freshGraph.Description = schema.Description;
223
+ await _graphRepo.UpdateAsync(freshGraph);
224
  }
225
 
226
  return Ok(new
227
  {
228
  success = true,
229
  graphId = id,
230
+ nodeCount = fullGraph.Nodes?.Count ?? 0,
231
  canvasUrl = $"/canvas/{id}"
232
  });
233
  }