Program: 38654
Project: 38654
Buggy source:
public JsRecords execute(IDb db) throws IOException {
  Optional<JsScope> executionScope=getOptionalExecutionScope(db);
  if (!executionScope.isPresent()) {
    throw new MissingScopeException(Joiner.on("String_Node_Str").join(predefinedScopeNames));
  }
  Records records=db.createQuery().from(table.table).where(table.typeColumn.notEqualTo(ValueType.SCOPE.value)).where(table.scopeColumn.in(subScopeIds)).select(table.scopeColumn,table.typeColumn,table.keyColumn,table.valueColumn).orderBy(table.scopeColumn).orderBy(table.idColumn).fetch();
  if (records.isEmpty()) {
    return JsRecords.empty(executionScope.get().getScopeId());
  }
  List<JsRecord> jsRecords=Lists.newLinkedList();
  Long previousScopeId=records.get(0).get(table.scopeColumn);
  Map<String,ValueType> types=Maps.newHashMap();
  Map<String,Object> values=Maps.newHashMap();
  List<JsonDbTuple> jsonTuples=Lists.newLinkedList();
  Set<String> jsonKeys=Sets.newHashSet();
  Iterator<Record> iterator=records.iterator();
  while (iterator.hasNext()) {
    Record record=iterator.next();
    Long currentScopeId=record.get(table.scopeColumn);
    if (!Objects.equals(previousScopeId,currentScopeId)) {
      addJsRecord(currentScopeId,types,values,jsonTuples,jsonKeys,jsRecords);
      types=Maps.newHashMap();
      values=Maps.newHashMap();
      jsonTuples=Lists.newLinkedList();
      jsonKeys=Sets.newHashSet();
      previousScopeId=currentScopeId;
    }
    appendRecord(types,values,jsonTuples,jsonKeys,record);
    if (!iterator.hasNext()) {
      addJsRecord(currentScopeId,types,values,jsonTuples,jsonKeys,jsRecords);
    }
  }
  return new JsRecords(executionScope.get().getScopeId(),jsRecords);
}

Fixed version (ground truth):
public JsRecords execute(IDb db) throws IOException {
  Optional<JsScope> executionScope=getOptionalExecutionScope(db);
  if (!executionScope.isPresent()) {
    throw new MissingScopeException(Joiner.on("String_Node_Str").join(predefinedScopeNames));
  }
  Records records=db.createQuery().from(table.table).where(table.typeColumn.notEqualTo(ValueType.SCOPE.value)).where(table.scopeColumn.in(subScopeIds)).select(table.scopeColumn,table.typeColumn,table.keyColumn,table.valueColumn).orderBy(table.scopeColumn).orderBy(table.idColumn).fetch();
  if (records.isEmpty()) {
    return JsRecords.empty(executionScope.get().getScopeId());
  }
  List<JsRecord> jsRecords=Lists.newLinkedList();
  Long previousScopeId=records.get(0).get(table.scopeColumn);
  Map<String,ValueType> types=Maps.newHashMap();
  Map<String,Object> values=Maps.newHashMap();
  List<JsonDbTuple> jsonTuples=Lists.newLinkedList();
  Set<String> jsonKeys=Sets.newHashSet();
  Iterator<Record> iterator=records.iterator();
  while (iterator.hasNext()) {
    Record record=iterator.next();
    Long currentScopeId=record.get(table.scopeColumn);
    if (!Objects.equals(previousScopeId,currentScopeId)) {
      addJsRecord(previousScopeId,types,values,jsonTuples,jsonKeys,jsRecords);
      types=Maps.newHashMap();
      values=Maps.newHashMap();
      jsonTuples=Lists.newLinkedList();
      jsonKeys=Sets.newHashSet();
      previousScopeId=currentScopeId;
    }
    appendRecord(types,values,jsonTuples,jsonKeys,record);
    if (!iterator.hasNext()) {
      addJsRecord(currentScopeId,types,values,jsonTuples,jsonKeys,jsRecords);
    }
  }
  return new JsRecords(executionScope.get().getScopeId(),jsRecords);
}