iterate.com / cloud

git clone https://git.iterate.com/repos/cloud

cloud / internal-packages / installer / worker / run-log.jsmain at aacbc74

// run-log.js — WHICH LINES OF A RUN'S LOG ITS STATE KEEPS (deploy-run.js). A deploy says more than
// the state can hold, so the state has the end of the log. The end alone lost three things that
// the page reads from the lines (src/deploy-steps.ts), seen on 12 November 2026 in a run whose
// deploy step said 1,300 lines:
//   the steps     a line `== step <id>` starts one; without them every step was "To do" in the
//                 middle of the deploy, and the step that failed had no name
//   the notes     a line `Note: …` is what the person must know when the run has worked
//   the erase     what "Erase and replace" erased and what it left: the record of what is gone
// So the state keeps those from the part that is cut, and says how many lines it did not keep.

const STEP = /^== step (\w+)/;
/** The most lines of the erase step that are kept from before the end. */
const MOST_OF_ERASE = 300;

/** The lines to keep: the last `most`, and before them each step's first line, each note, the
 *  erase step's lines, and one line per step that says how many lines are not here. */
export function keptLines(lines, most = 400) {
  if (lines.length <= most) return lines;
  const end = lines.length - most;
  const kept = [];
  let step = "";
  let ofErase = 0;
  let cut = 0;
  const sayCut = () => {
    if (cut > 0) kept.push(`(${cut} ${cut === 1 ? "line" : "lines"} of the log not kept here)`);
    cut = 0;
  };
  for (const line of lines.slice(0, end)) {
    const id = STEP.exec(line)?.[1];
    if (id) {
      sayCut();
      step = id;
      kept.push(line);
    } else if (line.startsWith("Note: ")) kept.push(line);
    else if (step === "erase" && ofErase < MOST_OF_ERASE) {
      ofErase++;
      kept.push(line);
    } else cut++;
  }
  sayCut();
  return [...kept, ...lines.slice(end)];
}