Spaces:
Running
Running
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 files193 unmodified lines194195196197198199200201202203204205206197198199200208201202203204212213205206207208209210216211212213214215216217218219220221222223224225226227228229230231232233193 unmodified lines } }
// If the title or description is provided, update the Graph metadata if (!string.IsNullOrWhiteSpace(schema.Title)) { dbGraph.Title = schema.Title.Trim(); } if (schema.Description != null) { dbGraph.Description = schema.Description; } await _graphRepo.UpdateAsync(dbGraph); // Access check already done above; we'll apply title/description after SaveFullGraphAsync // to avoid EF double-tracking conflicts (UpdateAsync + GetByIdWithDetailsAsync in SaveFullGraphAsync // would both track the same Graph entity in the same DbContext scope).
// Perform Auto-Layout on the new nodes // Perform Auto-Layout on the new nodes BEFORE mutating dbGraph further var fullGraph = AutoLayoutEngine.LayoutGraph(schema, dbGraph.UserId); fullGraph.Id = id; // Sync graph ID
// Save all new nodes and edges, completely replacing the old graph structure var saveResult = await _graphService.SaveFullGraphAsync(id, fullGraph, dbGraph.UserId); // Save all new nodes and edges first (uses its own fresh DB context load) // This must happen BEFORE UpdateAsync to avoid EF double-tracking the same Graph entity var ownerUserId = dbGraph.UserId; var saveResult = await _graphService.SaveFullGraphAsync(id, fullGraph, ownerUserId); if (!saveResult) { return BadRequest(new { message = "Failed to update generated task nodes in database." }); return BadRequest(new { message = $"Failed to update generated task nodes in database. " + $"Graph ID: {id}, Owner: {ownerUserId}, Nodes in payload: {fullGraph.Nodes?.Count ?? 0}" }); }
// NOW update the title/description on the freshly-saved graph var freshGraph = await _graphRepo.GetByIdAsync(id); if (freshGraph != null) { if (!string.IsNullOrWhiteSpace(schema.Title)) freshGraph.Title = schema.Title.Trim(); if (schema.Description != null) freshGraph.Description = schema.Description; await _graphRepo.UpdateAsync(freshGraph); }
return Ok(new { success = true, graphId = id, nodeCount = fullGraph.Nodes?.Count ?? 0, canvasUrl = $"/canvas/{id}" }); }FlowAPI/Controllers/AgentCanvasController.cs+22-14