Skip to content

A nightly check

A tool E-2 builds on the Records API: every night, download the history file of every run and every review, run the open-source checker on it with --json, keep the file and the report, and tell the team lead about any record that did not pass.

A check run once, when a run closes, says the record was intact that day. E-2's report relies on hundreds of runs for weeks, and the lab may ask about any of them months later. So every night a job downloads each record's history file, runs the open-source checker on it, and keeps both. When a record does not pass, the team lead hears about it the next morning, while the run is fresh.

The job runs the same checker a lab or anyone else would run. Anyone with the same file can reproduce its result, without E-2's code and without PacSpace.

The records it checks

The harness adds each run to records/m12.json when it opens the run, and the review inbox adds each review record when it opens one. The job checks every record on that list, every night. A record left off the list is never checked, which is why the tools that open the records keep the list.

The job

TypeScript
import { execFile } from 'node:child_process';
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import type { CheckReport } from '@pacspace-io/check';

const day = new Date().toISOString().slice(0, 10);
const folder = `checks/${day}`;

// Download one record's history file, as the History page does with curl.
async function download(record: string): Promise<string> {
  const response = await fetch(
    `https://app.pacspace.io/api/v1/records/machine-action-record/${record}/history`,
    { headers: { 'x-api-key': process.env.PACSPACE_API_KEY!, Accept: 'application/octet-stream' } },
  );
  if (!response.ok) throw new Error(`${record}: the history did not download (${response.status}).`);
  const file = `${folder}/${record}.history.json`;
  await writeFile(file, Buffer.from(await response.arrayBuffer()));
  return file;
}

// Run the installed checker and read its --json report. The exit code says how it went.
function check(args: string[]): Promise<{ exitCode: number; report: CheckReport | null }> {
  return new Promise((resolve) => {
    execFile('npx', ['pacspace-check', ...args, '--json'], { maxBuffer: 64 * 1024 * 1024 }, (error, stdout) => {
      const exitCode = error ? (typeof error.code === 'number' ? error.code : 2) : 0;
      let report: CheckReport | null = null;
      try {
        report = JSON.parse(stdout);
      } catch {
        report = null;
      }
      resolve({ exitCode, report });
    });
  });
}

const records: string[] = JSON.parse(await readFile('records/m12.json', 'utf8'));
const counts: Record<string, number> = JSON.parse(await readFile('checks/entry-counts.json', 'utf8').catch(() => '{}'));
const morning: string[] = [];
await mkdir(folder, { recursive: true });

for (const record of records) {
  const file = await download(record);

  // A run's result file is checked too: the checker says which entry names it.
  const held = record.includes('-run-')
    ? ['--held-file', `runs/${record}/result.json`, '--blinding', `runs/${record}/result.json.pacspace.json`]
    : [];
  const { exitCode, report } = await check([file, ...held]);
  await writeFile(`${folder}/${record}.report.json`, JSON.stringify(report, null, 2));

  // 3: the proof layer could not be reached tonight. Nothing is known yet; the next night runs again.
  if (exitCode === 3) continue;
  if (exitCode !== 0 || !report) {
    morning.push(`${record}: ${report ? report.sentences.join(' ') : 'the checker could not use the file.'}`);
    continue;
  }

  // A closed run should not grow. A write after the close is still committed, as an attempt.
  const entries = report.record.entries ?? report.record.matched;
  if (counts[record] !== undefined && counts[record] !== entries) {
    morning.push(`${record}: ${entries} entries tonight, ${counts[record]} when it was first checked.`);
  }
  counts[record] ??= entries;
}

await writeFile('checks/entry-counts.json', JSON.stringify(counts, null, 2));
await writeFile(`${folder}/morning.txt`, morning.length ? morning.join('\n') : 'Every record passed.');

E-2's scheduler runs the job at 02:15 UTC, in a project with @pacspace-io/sdk and @pacspace-io/check installed, so npx pacspace-check runs the installed checker. morning.txt goes to the team lead's channel. A run has far fewer than 5,000 entries, so one download carries its whole record.

What each exit code means here

Exit codeWhat the checker foundWhat the job does
0The check passed as asked, and the result file is in the record.Keeps the file and the report.
1The check did not pass, or the result file is not in the record.Tells the team lead, in the checker's own sentences. The results table leaves the run out.
2The history file, the result file, its blinding file, or the arguments could not be used.Tells the team lead.
3The proof layer could not be reached.Checks again the next night.

A closed record can still grow. A write after the close is committed as an attempt, and the record still passes the check. The job notices the count, and the morning note says so; the attempt is in the record for any reader to see, marked as an attempt.

What it keeps

For every record, every night: the history file, and the checker's report on it. The report names the file by its fingerprint, file.fingerprint. Each export has its own fingerprint, so the report and its file stay together in the night's folder. The results table is built from one night's folder, and the lab gets the same folder.

What it does not show

The check shows the record is unchanged since it was committed. It does not show that what was recorded was true. A night with every record passing says nothing about a run the harness never recorded: a gap in the record is as telling as a change, and the list of records is where E-2 looks for one.