def truncate_trajectories(self, indices, num_to_keep=1):
    """Truncate trajectories at specified indices.

     This puts the truncated trajectories in the completed list and makes new
     trajectories with the observation from the trajectory that was truncated at
     the same index.

    Args:
        indices: iterable with the indices to truncate.
        num_to_keep: int, number of last time-steps to keep while truncating.
    """
    for index in indices:
      trajectory = self._trajectories[index]
      assert trajectory.is_active, "Trajectory to truncate can't be inactive."

      # Now `trajectory` just consists of the last `num_to_keep` observations
      # and actions. Rewards are zeroed out.
      # The old data is placed in `old_trajectory`.
      old_trajectory = trajectory.truncate(num_to_keep=num_to_keep)

      # We put the old data in _completed_trajectories.
      self._completed_trajectories.append(old_trajectory)
Truncate trajectories at specified indices.

     This puts the truncated trajectories in the completed list and makes new
     trajectories with the observation from the trajectory that was truncated at
     the same index.

    Args:
        indices: iterable with the indices to truncate.
        num_to_keep: int, number of last time-steps to keep while truncating.
    
tensor2tensor/envs/trajectory.py