Program: 96675
Project: 96675
Buggy source:
public JaxAgileItem write(){
  IAtsChangeSet changes=services.getStoreService().createAtsChangeSet("String_Node_Str",AtsCoreUsers.SYSTEM_USER);
  if (newItem.isSetFeatures()) {
    Collection<IAgileFeatureGroup> features=agileService.getAgileFeatureGroups(newItem.getFeatures());
    List<ArtifactToken> featureArts=new LinkedList<>();
    for (    IAgileFeatureGroup feature : features) {
      featureArts.add(feature.getStoreObject());
    }
    for (    ArtifactToken awa : services.getArtifacts(newItem.getUuids())) {
      for (      IAgileFeatureGroup feature : features) {
        ArtifactToken featureArt=feature.getStoreObject();
        if (!services.getRelationResolver().areRelated(featureArt,AtsRelationTypes.AgileFeatureToItem_FeatureGroup,awa)) {
          changes.relate(feature,AtsRelationTypes.AgileFeatureToItem_AtsItem,awa);
        }
      }
      for (      ArtifactToken featureArt : services.getRelationResolver().getRelated(awa,AtsRelationTypes.AgileFeatureToItem_FeatureGroup)) {
        if (!featureArts.contains(featureArt)) {
          changes.unrelate(featureArt,AtsRelationTypes.AgileFeatureToItem_AtsItem,awa);
        }
      }
    }
  }
 else   if (newItem.isRemoveFeatures()) {
    for (    ArtifactToken awa : services.getArtifacts(newItem.getUuids())) {
      for (      ArtifactToken feature : services.getRelationResolver().getRelated(awa,AtsRelationTypes.AgileFeatureToItem_FeatureGroup)) {
        changes.unrelate(feature,AtsRelationTypes.AgileFeatureToItem_FeatureGroup,awa);
      }
    }
  }
  if (newItem.isSetSprint()) {
    ArtifactToken sprintArt=services.getArtifact(newItem.getSprintUuid());
    IAgileSprint sprint=services.getAgileService().getAgileSprint(sprintArt);
    for (    ArtifactToken awa : services.getArtifacts(newItem.getUuids())) {
      if (sprint != null) {
        changes.setRelation(sprint,AtsRelationTypes.AgileSprintToItem_AtsItem,awa);
      }
 else {
        changes.unrelateAll(awa,AtsRelationTypes.AgileSprintToItem_AtsItem);
      }
      changes.add(sprint);
    }
  }
  if (newItem.isSetBacklog()) {
    ArtifactToken backlogArt=services.getArtifact(newItem.getBacklogUuid());
    IAgileSprint backlog=services.getAgileService().getAgileSprint(backlogArt);
    for (    ArtifactToken awa : services.getArtifacts(newItem.getUuids())) {
      if (backlog != null) {
        changes.setRelation(backlog,AtsRelationTypes.Goal_Member,awa);
      }
 else {
        changes.unrelateAll(awa,AtsRelationTypes.Goal_Member);
      }
      changes.add(backlog);
    }
  }
  if (!changes.isEmpty()) {
    changes.execute();
  }
  return newItem;
}

Fixed version (ground truth):
public JaxAgileItem write(){
  IAtsChangeSet changes=services.getStoreService().createAtsChangeSet("String_Node_Str",AtsCoreUsers.SYSTEM_USER);
  if (newItem.isSetFeatures()) {
    Collection<IAgileFeatureGroup> features=agileService.getAgileFeatureGroups(newItem.getFeatures());
    List<ArtifactToken> featureArts=new LinkedList<>();
    for (    IAgileFeatureGroup feature : features) {
      featureArts.add(feature.getStoreObject());
    }
    for (    ArtifactToken awa : services.getArtifacts(newItem.getUuids())) {
      for (      IAgileFeatureGroup feature : features) {
        ArtifactToken featureArt=feature.getStoreObject();
        if (!services.getRelationResolver().areRelated(featureArt,AtsRelationTypes.AgileFeatureToItem_FeatureGroup,awa)) {
          changes.relate(feature,AtsRelationTypes.AgileFeatureToItem_AtsItem,awa);
        }
      }
      for (      ArtifactToken featureArt : services.getRelationResolver().getRelated(awa,AtsRelationTypes.AgileFeatureToItem_FeatureGroup)) {
        if (!featureArts.contains(featureArt)) {
          changes.unrelate(featureArt,AtsRelationTypes.AgileFeatureToItem_AtsItem,awa);
        }
      }
    }
  }
 else   if (newItem.isRemoveFeatures()) {
    for (    ArtifactToken awa : services.getArtifacts(newItem.getUuids())) {
      for (      ArtifactToken feature : services.getRelationResolver().getRelated(awa,AtsRelationTypes.AgileFeatureToItem_FeatureGroup)) {
        changes.unrelate(feature,AtsRelationTypes.AgileFeatureToItem_AtsItem,awa);
      }
    }
  }
  if (newItem.isSetSprint()) {
    ArtifactToken sprintArt=services.getArtifact(newItem.getSprintUuid());
    IAgileSprint sprint=services.getAgileService().getAgileSprint(sprintArt);
    for (    ArtifactToken awa : services.getArtifacts(newItem.getUuids())) {
      if (sprint != null) {
        changes.setRelation(sprint,AtsRelationTypes.AgileSprintToItem_AtsItem,awa);
      }
 else {
        changes.unrelateAll(awa,AtsRelationTypes.AgileSprintToItem_AtsItem);
      }
      changes.add(sprint);
    }
  }
  if (newItem.isSetBacklog()) {
    ArtifactToken backlogArt=services.getArtifact(newItem.getBacklogUuid());
    IAgileSprint backlog=services.getAgileService().getAgileSprint(backlogArt);
    for (    ArtifactToken awa : services.getArtifacts(newItem.getUuids())) {
      if (backlog != null) {
        changes.setRelation(backlog,AtsRelationTypes.Goal_Member,awa);
      }
 else {
        changes.unrelateAll(awa,AtsRelationTypes.Goal_Member);
      }
      changes.add(backlog);
    }
  }
  if (!changes.isEmpty()) {
    changes.execute();
  }
  return newItem;
}