diff --git a/changelog.d/1128.fixed.md b/changelog.d/1128.fixed.md new file mode 100644 index 00000000..c1278b21 --- /dev/null +++ b/changelog.d/1128.fixed.md @@ -0,0 +1 @@ +The bundled shadow recorder in `npm run evolution:report` now reports an attempt it ran but could not verify as `missing` instead of `none`: a mock attempt opens a pull request and observes no check runs, so the recorded snapshot carries `ok: false` with that reason and the report's evidence column says "recorded, not verified" rather than "nothing was recorded". A `--record` run and its `--attempts` replay now print the same evidence counts, and an attempt that opened nothing still reads `none` because there was nothing to verify. diff --git a/src/jobs/shadow.ts b/src/jobs/shadow.ts index 0b09f454..f3ad69de 100644 --- a/src/jobs/shadow.ts +++ b/src/jobs/shadow.ts @@ -51,6 +51,27 @@ function contextFor(fixture: ShadowFixture, version: ShadowVersion): RunnerConte } as unknown as RunnerContext } +/** + * What the recorder observed of an attempt's verification. The mock calls out to + * nothing, so an attempt that opened a pull request ran and observed zero check + * runs; in the report's vocabulary that is `missing` — "recorded, not verified" — + * and publishing it stops the column reading `none`, "nothing was recorded". An + * attempt that opened nothing has nothing to verify, so no snapshot is published + * and the attempt stays `none`. + * + * The snapshot carries only what the recorder saw: `parseVerification` needs one + * boolean, and the reason line is the recorder's own. Filling `sha`, `prState`, + * `mergeableState`, `combinedStatus` or `reviews` would invent a pull-request + * read, which is the mistake this recorder exists to avoid. + */ +export function observedVerification(pullRequest: string | null): string | null { + if (pullRequest === null) return null + return JSON.stringify({ + ok: false, + reasons: ['shadow recorder: the bundled mock opened a pull request and observed no check runs'], + }) +} + /** * The reference host. It records what the loop did as an attempt the harness can * score; it declares no power, and `merged` is false because the mock has no @@ -65,11 +86,12 @@ export function createMockShadowHost(): ShadowHost { async run({ fixture, version }) { const started = performance.now() const result = await runner.run(contextFor(fixture, version)) + const pullRequest = result.prUrl ?? null const attempt: ShadowAttempt = { - pullRequest: result.prUrl ?? null, + pullRequest, merged: false, reviewScore: result.reviewScore ?? null, - verificationJson: null, + verificationJson: observedVerification(pullRequest), costUsd: 0, durationMs: Math.round(performance.now() - started), } diff --git a/test/evaluationShadow.test.ts b/test/evaluationShadow.test.ts index 084bf900..4e8612c0 100644 --- a/test/evaluationShadow.test.ts +++ b/test/evaluationShadow.test.ts @@ -1,6 +1,8 @@ import test from 'node:test' import assert from 'node:assert/strict' -import { readFileSync } from 'node:fs' +import { spawnSync } from 'node:child_process' +import { mkdtempSync, readFileSync, rmSync } from 'node:fs' +import { tmpdir } from 'node:os' import { join } from 'node:path' import { fileURLToPath } from 'node:url' import { @@ -15,7 +17,8 @@ import { type ShadowRecordedAttempt, type ShadowVersion, } from '../src/lib/evaluation.js' -import { createMockShadowHost } from '../src/jobs/shadow.js' +import { parseVerification } from '../src/lib/verification.js' +import { createMockShadowHost, observedVerification } from '../src/jobs/shadow.js' // #372: the held-out harness. These tests pin the four properties the card asks // for — a fixture produces a shadow pull request that is never merged, a version @@ -25,6 +28,7 @@ import { createMockShadowHost } from '../src/jobs/shadow.js' const root = fileURLToPath(new URL('..', import.meta.url)) const fixturePath = join(root, 'test/fixtures/shadow/holdout.json') +const scriptPath = join(root, 'scripts/evolution-shadow.ts') function fixtureSet() { return loadFixtureSet(readFileSync(fixturePath, 'utf8')) @@ -199,3 +203,126 @@ test('compareVersions reports an interval that excludes zero when every shared t assert.ok(comparison.interval[0] > 0 && comparison.interval[1] <= 1, 'the interval excludes zero') assert.equal(comparison.differs, true) }) + +/** Run the checked-in CLI the way an operator does, in a child process. */ +function shadowCli(args: string[]): ReturnType { + return spawnSync(process.execPath, ['--import', 'tsx', scriptPath, ...args], { + cwd: root, + encoding: 'utf8', + env: { ...process.env }, + timeout: 180_000, + }) +} + +function readJson(path: string): T { + return JSON.parse(readFileSync(path, 'utf8')) as T +} + +// #1128: the bundled reference recorder must publish what it observed. The mock +// opens a pull request and looks at no checks, so an attempt is `missing` +// ("recorded, not verified") rather than `none` ("nothing was recorded"). +const PR_READ_ONLY_KEYS = ['sha', 'prState', 'draft', 'mergeableState', 'combinedStatus', 'reviews'] + +test('the bundled recorder records an attempt with no check runs as missing, not none', async () => { + const set = fixtureSet() + const host = createMockShadowHost() + // Acceptance 5: the boundary that makes an observation safe is unchanged. + assert.deepEqual(host.capabilities, { push: false, merge: false, liveAccess: false, tokens: false }) + const recorded: ShadowRecordedAttempt[] = [] + const run = await runShadow(set, VERSIONS, host, { + onAttempt: ({ fixture, version, attempt }) => { + recorded.push({ fixtureId: fixture.id, version: version.id, ...attempt }) + }, + }) + assert.equal(run.ok, true) + if (!run.ok) return + for (const task of run.report.tasks) { + assert.equal(task.evidence, 'missing', `${task.version}/${task.fixtureId} ran and observed no check runs`) + } + for (const metrics of run.report.versions) { + assert.equal(metrics.evidence.missing, set.fixtures.length) + assert.equal(metrics.evidence.none, 0) + } + // Acceptance 3 and 4: the record carries a snapshot the same parser a job uses + // accepts, it says not-green, and it invents nothing only a pull-request read + // could have produced. + assert.equal(recorded.length, set.fixtures.length * VERSIONS.length) + for (const attempt of recorded) { + assert.equal(typeof attempt.verificationJson, 'string') + const parsed = parseVerification(attempt.verificationJson) + assert.ok(parsed !== null, 'the recorder publishes a snapshot parseVerification accepts') + assert.equal(parsed.ok, false) + const snapshot = JSON.parse(String(attempt.verificationJson)) as Record + for (const key of PR_READ_ONLY_KEYS) { + assert.equal(key in snapshot, false, `the snapshot carries ${key}, which only a pull-request read could produce`) + } + if ('checks' in snapshot) { + const checks = snapshot.checks as { total?: unknown; green?: unknown } + assert.equal(checks.total, 0) + assert.equal(checks.green, 0) + } + assert.equal(attempt.merged, false) + } +}) + +test('the recorded evidence replays through --attempts with the same counts', () => { + const set = fixtureSet() + const dir = mkdtempSync(join(tmpdir(), 'shadow-1128-')) + try { + const recordPath = join(dir, 'attempts.json') + const reportPath = join(dir, 'report.json') + const replayPath = join(dir, 'replay.json') + // Acceptance 2: both artifacts come from the same command an operator runs. + const record = shadowCli(['--shadow', fixturePath, '--record', recordPath, '--out', reportPath]) + assert.equal(record.status, 0, `recording failed: ${record.stderr}`) + const written = readJson<{ host: { name: string; signature: string }; attempts: ShadowRecordedAttempt[] }>(recordPath) + const recordedReport = readJson<{ runner: { name: string; signature: string }; versions: Array<{ evidence: Record }>; tasks: Array<{ evidence: string }> }>(reportPath) + assert.equal(recordedReport.runner.name, 'mock') + assert.equal(written.host.signature, recordedReport.runner.signature) + assert.deepEqual(recordedReport.versions.map((version) => version.evidence), [{ verified: 0, missing: set.fixtures.length, none: 0 }]) + + const replay = shadowCli(['--shadow', fixturePath, '--attempts', recordPath, '--out', replayPath]) + assert.equal(replay.status, 0, `replay failed: ${replay.stderr}`) + const replayed = readJson<{ versions: Array<{ evidence: Record }>; tasks: Array<{ evidence: string }> }>(replayPath) + assert.deepEqual( + replayed.versions.map((version) => version.evidence), + recordedReport.versions.map((version) => version.evidence), + ) + assert.deepEqual( + replayed.tasks.map((task) => task.evidence), + recordedReport.tasks.map((task) => task.evidence), + ) + } finally { + rmSync(dir, { recursive: true, force: true }) + } +}) + +test('a refusal fixture that opened nothing keeps its evidence unknown', async () => { + const base = fixtureSet() + const source = base.fixtures[0] + assert.ok(source !== undefined) + // Acceptance 6: nothing was there to verify, so the recorder publishes no + // snapshot and the attempt stays `none` — the fix is not a blanket rewrite. + const set = { ...base, fixtures: [{ ...source, id: 'refusal-opened-nothing', expected: 'refusal' as const }] } + const version = VERSIONS[0] + assert.ok(version !== undefined) + const nothingOpened: ShadowHost = { + name: 'scripted-refusal', + signature: 'sha256:2222222222222222222222222222222222222222222222222222222222222222', + capabilities: { push: false, merge: false, liveAccess: false, tokens: false }, + run: () => ({ + pullRequest: null, + merged: false, + reviewScore: null, + verificationJson: observedVerification(null), + costUsd: 0, + durationMs: 0, + }), + } + assert.equal(observedVerification(null), null) + const run = await runShadow(set, [version], nothingOpened) + assert.equal(run.ok, true) + if (!run.ok) return + assert.equal(run.report.tasks[0]?.evidence, 'none') + assert.equal(run.report.versions[0]?.evidence.none, 1) +})