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 lines194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225193 unmodified lines}}// If the title or description is provided, update the Graph metadataif (!string.IsNullOrWhiteSpace(schema.Title)){dbGraph.Title = schema.Title.Trim();}if (schema.Description != null){dbGraph.Description = schema.Description;}await _graphRepo.UpdateAsync(dbGraph);// Perform Auto-Layout on the new nodesvar fullGraph = AutoLayoutEngine.LayoutGraph(schema, dbGraph.UserId);fullGraph.Id = id; // Sync graph ID// Save all new nodes and edges, completely replacing the old graph structurevar saveResult = await _graphService.SaveFullGraphAsync(id, fullGraph, dbGraph.UserId);if (!saveResult){return BadRequest(new { message = "Failed to update generated task nodes in database." });}return Ok(new{success = true,graphId = id,canvasUrl = $"/canvas/{id}"});}193 unmodified lines194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233193 unmodified lines}}// 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 BEFORE mutating dbGraph furthervar fullGraph = AutoLayoutEngine.LayoutGraph(schema, dbGraph.UserId);fullGraph.Id = id; // Sync graph ID// 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 entityvar 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. " +$"Graph ID: {id}, Owner: {ownerUserId}, Nodes in payload: {fullGraph.Nodes?.Count ?? 0}" });}// NOW update the title/description on the freshly-saved graphvar 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