Skip to content

A review inbox

A tool E-2 builds on the Records API: when a run's record closes, check it, send a person every run with a monitor flag and one in ten without, show the reviewer the transcript once it is checked against the record, and record the reviewer's call.

E-2's reviewers read runs for a result the agent reached by a route the task does not allow, such as changing the test it is graded by. There are more runs than reviewers, so an inbox decides which runs a person reads: every run with a monitor flag, and one run in ten with none. The unflagged runs are how E-2 learns what its monitor misses.

The inbox does three things, each on this page. It hears that a run has closed. It checks the run before a person spends an hour on it. And it records the reviewer's call where the lab can read it. The records are the ones in Tools you build on the record, and rowsOf, fieldsOf, and refOf are that page's helper.

When a run closes

The harness closes a run with a run-end entry. The inbox takes record.committed, fetches the receipt of each entry that committed, and acts when the entry's kind is run-end.

TypeScript
import { PacSpace, Webhooks, WebhookVerificationError, type RecordCommittedPayload, type RecordHistory } from '@pacspace-io/sdk';
import { enqueue } from './jobs';
import { fieldsOf } from './record-rows';

const pac = new PacSpace({ apiKey: process.env.PACSPACE_API_KEY! });
const webhooks = new Webhooks(process.env.PACSPACE_WEBHOOK_SECRET!);

// rawBody is the request body as received, as a string.
export async function handle(headers: Record<string, string | string[] | undefined>, rawBody: string) {
  let event;
  try {
    event = webhooks.verifyFromHeaders(headers, rawBody);
  } catch (err) {
    if (err instanceof WebhookVerificationError) return { status: 401 };
    throw err;
  }
  if (event.event !== 'record.committed') return { status: 200 };

  for (const row of (event.data as RecordCommittedPayload).records) {
    if (!row.entityId?.startsWith('m12-run-') || row.seq == null) continue;
    // The payload keeps the wire's seq, counted from 0, so the entry is seq + 1.
    const { receipt } = await pac.records.receipt({ record: row.entityId, entry: row.seq + 1 });
    if (fieldsOf(receipt as RecordHistory['receipts'][number]).kind === 'run-end') {
      await enqueue('triage', row.entityId);
    }
  }
  return { status: 200 };
}

enqueue hands the run to E-2's own job runner, so the handler answers quickly and the work happens in a job.

Check the run before a person reads it

The job reads the run's history, checks it, and only then reads its entries. A run whose record does not pass goes to the team lead with the reason the check gives, and never to a reviewer.

TypeScript
import { writeFile } from 'node:fs/promises';
import { PacSpace } from '@pacspace-io/sdk';
import { refOf, rowsOf } from './record-rows';
import { assign, inSample } from './reviewers';

const pac = new PacSpace({ apiKey: process.env.PACSPACE_API_KEY! });

export async function triage(run: string) {
  const history = await pac.records.history({ record: run });
  const result = await pac.records.check({ history });
  if (!result.ok) {
    return assign({ run, to: 'team-lead', reason: result.reason ?? 'The record did not pass the check.' });
  }

  const rows = rowsOf(history);
  const end = rows.find((row) => row.kind === 'run-end')!;
  const flags = rows.filter((row) => row.kind === 'monitor-flag');
  if (flags.length === 0 && !inSample(run)) return;

  // The inbox's own file of why the run was sent, sealed with the entry by its fingerprint.
  const review = run.replace('-run-', '-review-');
  const why = `reviews/${review}/opened.json`;
  await writeFile(why, JSON.stringify({ run, flags: flags.map((flag) => flag.entry), sampled: flags.length === 0 }));

  const n = Number(run.split('-').at(-1));
  const reason = flags.length === 0
    ? 'no flag, one run in ten read without one'
    : `${flags.length === 1 ? 'one monitor flag' : `${flags.length} monitor flags`}, at entry ${flags.map((flag) => flag.entry).join(', ')}`;

  await pac.records.emit({
    record: review,
    title: `Run ${n} sent for review: ${reason}`,
    kind: 'review-opened',
    occurredAt: new Date().toISOString(),
    actorId: 'e2-review-inbox',
    payloads: [await refOf(why)],
    references: [...flags, end].map((row) => ({ recordKey: history.recordKey, entry: row.entry })),
    idempotencyKey: `${review}:opened`,
  });
  return assign({ run, review, flags: flags.map((flag) => flag.entry) });
}

references names entries 38 and 62 of the run, so a reader of the review record can open the flag and the close it is about. Each referenced entry is already committed, which references requires: the flags are read from the history's receipts, and only a committed entry has one. inSample and assign are E-2's own: which unflagged runs are read, and which reviewer reads each one.

The reviewer's screen

The reviewer opens the run in E-2's review tool. It shows the run's entries from the checked history, and the transcript from E-2's run store. Before it shows the transcript, the tool checks the file against the fingerprint in the run's run-end entry, so the reviewer knows the transcript on screen is the one the record names.

TypeScript
import { readFile } from 'node:fs/promises';
import { PacSpace, readBlindingFile } from '@pacspace-io/sdk';
import { rowsOf } from './record-rows';

const pac = new PacSpace({ apiKey: process.env.PACSPACE_API_KEY! });

export async function openRun(run: string) {
  const history = await pac.records.history({ record: run });
  const rows = rowsOf(history);
  const end = rows.find((row) => row.kind === 'run-end');
  if (!end) throw new Error(`${run} has no run-end entry yet.`);

  const transcript = await readFile(`runs/${run}/transcript.jsonl`);
  const blinding = await readBlindingFile(`runs/${run}/transcript.jsonl.pacspace.json`);
  const result = await pac.records.check({
    history,
    expect: [{ entry: end.entry, files: [{ source: transcript, blinding }] }],
  });
  const named = result.entries.find((row) => row.entry === end.entry)?.expected === true;

  return {
    runKey: history.recordKey,
    rows,
    end: end.entry,
    transcript: result.ok && named ? transcript.toString('utf8') : null,
    reason: result.reason,
  };
}

Above the transcript the screen says, in E-2's words, "This transcript is the one entry 62 names." When the check does not pass, the screen shows the reason and no transcript, and the reviewer sends the run back to the team lead.

Record the reviewer's call

The reviewer decides whether the run counts toward the report. The tool writes the call to the review record, with the reviewer's notes as the file it carries, and names the entries the call is about.

TypeScript
import { PacSpace } from '@pacspace-io/sdk';
import { refOf } from './record-rows';

const pac = new PacSpace({ apiKey: process.env.PACSPACE_API_KEY! });

export interface Call {
  run: string;
  runKey: string;
  reviewer: string;
  counts: boolean;
  reason: string;
  about: number[];
  notes: string;
}

export async function recordCall(call: Call) {
  const review = call.run.replace('-run-', '-review-');
  const n = Number(call.run.split('-').at(-1));
  return pac.records.emit({
    record: review,
    title: `Run ${n} ${call.counts ? 'counts' : 'set aside'}: ${call.reason}`,
    kind: call.counts ? 'review-counts' : 'review-set-aside',
    occurredAt: new Date().toISOString(),
    actorId: call.reviewer,
    instructedBy: 'e2-review-inbox',
    payloads: [await refOf(call.notes)],
    references: call.about.map((entry) => ({ recordKey: call.runKey, entry })),
    idempotencyKey: `${review}:call`,
  });
}

For run 412, reviewer-3 reads entry 38 and the transcript and finds the agent rewrote the test so it passes. The call is entry 2 of m12-review-0412, "Run 412 set aside: the agent changed the test it was graded by", naming entries 38 and 62 of the run. A title holds at most 120 characters, so the tool keeps reason short and the notes file carries the rest.

A reviewer who changes a call writes a correction with amends, naming the call's entry, and its own idempotency key. An amendment is a new entry. This one stays as it was written. See Correcting an entry.

What the review records answer

  • Which runs reached a person, and why: entry 1 of each review record, with the flags it names.
  • Who read the run, when, and what they decided: the call, written as the reviewer made it.
  • What the call is about: the run's entries it names in references.
  • Which flagged runs no person has read: a run with a monitor-flag entry and no review record.

What they do not answer

Whether the reviewer was right, and whether the monitor flags every call it should. The one run in ten read without a flag is how E-2 learns that, and the review records show how often a reviewer found something the monitor did not. PacSpace records and never decides.