docs: protected void appendEntries(AppendRequest request, CompletableFuture<AppendResponse> future) {
      // Compute the last entry index from the previous log index and request entry count.
      final long lastEntryIndex = request.prevLogIndex() + request.entries().size();
  
      // Ensure the commitIndex is not increased beyond the index of the last entry in the request.
      final long commitIndex = Math.max(raft.getCommitIndex(), Math.min(request.commitIndex(), lastEntryIndex));
  
      // Track the last log index while entries are appended.
      long lastLogIndex = request.prevLogIndex();
  
      if (!request.entries().isEmpty()) {
        final RaftLogWriter writer = raft.getLogWriter();
        final RaftLogReader reader = raft.getLogReader();
  
        // If the previous term is zero, that indicates the previous index represents the beginning of the log.
        // Reset the log to the previous index plus one.
        if (request.prevLogTerm() == 0) {
          log.debug("Reset first index to {}", request.prevLogIndex() + 1);
          writer.reset(request.prevLogIndex() + 1);
        }
  
        // Iterate through entries and append them.
        for (RaftLogEntry entry : request.entries()) {
          long index = ++lastLogIndex;
  
          // Get the last entry written to the log by the writer.
          Indexed<RaftLogEntry> lastEntry = writer.getLastEntry();
  
          if (lastEntry != null) {
            // If the last written entry index is greater than the next append entry index,
            // we need to validate that the entry that's already in the log matches this entry.
            if (lastEntry.index() > index) {
              // Reset the reader to the current entry index.
              if (reader.getNextIndex() != index) {
                reader.reset(index);
              }
  
              // If the reader does not have any next entry, that indicates an inconsistency between the reader and writer.
              if (!reader.hasNext()) {
                throw new IllegalStateException("Log reader inconsistent with log writer");
              }
  
              // Read the existing entry from the log.
              Indexed<RaftLogEntry> existingEntry = reader.next();
  
              // If the existing entry term doesn't match the leader's term for the same entry, truncate
              // the log and append the leader's entry.
              if (existingEntry.entry().term() != entry.term()) {
                writer.truncate(index - 1);
                if (!appendEntry(index, entry, writer, future)) {
                  return;
                }
              }
            }
            // If the last written entry is equal to the append entry index, we don't need
            // to read the entry from disk and can just compare the last entry in the writer.
            else if (lastEntry.index() == index) {
              // If the last entry term doesn't match the leader's term for the same entry, truncate
              // the log and append the leader's entry.
              if (lastEntry.entry().term() != entry.term()) {
                writer.truncate(index - 1);
                if (!appendEntry(index, entry, writer, future)) {
                  return;
                }
              }
            }
            // Otherwise, this entry is being appended at the end of the log.
            else {
              // If the last entry index isn't the previous index, throw an exception because something crazy happened!
              if (lastEntry.index() != index - 1) {
                throw new IllegalStateException("Log writer inconsistent with next append entry index " + index);
              }
  
              // Append the entry and log a message.
              if (!appendEntry(index, entry, writer, future)) {
                return;
              }
            }
          }
          // Otherwise, if the last entry is null just append the entry and log a message.
          else {
            if (!appendEntry(index, entry, writer, future)) {
              return;
            }
          }
  
          // If the last log index meets the commitIndex, break the append loop to avoid appending uncommitted entries.
          if (!role().active() && index == commitIndex) {
            break;
          }
        }
      }
  
      // Set the first commit index.
      raft.setFirstCommitIndex(request.commitIndex());
  
      // Update the context commit and global indices.
      long previousCommitIndex = raft.setCommitIndex(commitIndex);
      if (previousCommitIndex < commitIndex) {
        log.trace("Committed entries up to index {}", commitIndex);
        raft.getServiceManager().applyAll(commitIndex);
      }
  
      // Return a successful append response.
      succeedAppend(lastLogIndex, future);
    }