#!/usr/bin/env bun /** * Deterministic script grader for the script-grader stdin payload contract. * * It verifies that AgentV sends the canonical wire payload to script graders: * final answer text in `output`, full transcript in `messages`, test context in * `input`/`criteria`, and no deprecated `answer` alias. */ import { readFileSync } from 'node:fs'; interface Check { readonly text: string; readonly pass: boolean; readonly reason: string; readonly evidence?: string; } const checks: Check[] = []; function push(text: string, passed: boolean, evidence?: string): void { checks.push({ text, pass: passed, reason: evidence ?? (passed ? 'Check passed' : 'Check failed'), ...(evidence ? { evidence } : {}), }); } function isRecord(value: unknown): value is Record { return value !== null && typeof value === 'object' && !Array.isArray(value); } function hasOwn(record: Record, key: string): boolean { return Object.prototype.hasOwnProperty.call(record, key); } function collectStrings(value: unknown, strings: string[]): void { if (typeof value === 'string') { strings.push(value); return; } if (Array.isArray(value)) { for (const item of value) collectStrings(item, strings); return; } if (isRecord(value)) { for (const item of Object.values(value)) collectStrings(item, strings); } } let payload: Record; try { const parsed = JSON.parse(readFileSync(0, 'utf8')) as unknown; if (!isRecord(parsed)) { push('stdin payload is a JSON object', false, `Received ${typeof parsed}`); console.log(JSON.stringify({ checks })); process.exit(0); } payload = parsed; } catch (error) { const message = error instanceof Error ? error.message : String(error); push('stdin payload is valid JSON', false, message); console.log(JSON.stringify({ checks })); process.exit(0); } const output = payload.output; const outputIsString = typeof output === 'string'; push( 'output is the final answer string', outputIsString, outputIsString ? undefined : `Expected string, got ${Array.isArray(output) ? 'array' : typeof output}`, ); const outputText = outputIsString ? output : ''; const outputHasRelease = outputText.includes('RELEASE'); const outputHasContract = outputText.includes('CONTRACT'); push( 'output contains tokens from both input messages', outputText.trim().length > 0 && outputHasRelease && outputHasContract, outputText.trim().length > 0 ? `output=${JSON.stringify(outputText.slice(0, 80))}` : 'output was empty', ); const messages = payload.messages; const messagesIsArray = Array.isArray(messages); push( 'messages is the full transcript array', messagesIsArray, messagesIsArray ? undefined : `Expected array, got ${typeof messages}`, ); const hasAssistantMessage = messagesIsArray && messages.some((message) => isRecord(message) && message.role === 'assistant'); push( 'messages contains an assistant message', hasAssistantMessage, hasAssistantMessage ? undefined : 'No assistant role message found in messages', ); const inputStrings: string[] = []; collectStrings(payload.input, inputStrings); const inputIncludesSystemToken = inputStrings.some((text) => text.includes('RELEASE')); const inputIncludesUserToken = inputStrings.some((text) => text.includes('CONTRACT')); push( 'input contains both source prompts', inputIncludesSystemToken && inputIncludesUserToken, inputIncludesSystemToken && inputIncludesUserToken ? undefined : 'RELEASE or CONTRACT token not found in input', ); const criteria = payload.criteria; push( 'criteria is a string', typeof criteria === 'string', typeof criteria === 'string' ? `criteria=${JSON.stringify(criteria)}` : 'criteria was missing', ); const camelCaseKeys = [ 'expectedOutput', 'inputFiles', 'outputPath', 'traceSummary', 'workspacePath', 'costUsd', 'durationMs', 'startTime', 'endTime', 'fileChanges', ]; const leakedCamelCaseKeys = camelCaseKeys.filter((key) => hasOwn(payload, key)); push( 'grader payload uses snake_case wire keys', Array.isArray(payload.input_files) && leakedCamelCaseKeys.length === 0, leakedCamelCaseKeys.length > 0 ? `Unexpected camelCase keys: ${leakedCamelCaseKeys.join(', ')}` : Array.isArray(payload.input_files) ? undefined : 'input_files was missing or not an array', ); push( 'deprecated answer alias is absent', !hasOwn(payload, 'answer'), hasOwn(payload, 'answer') ? 'Unexpected answer field found in payload' : undefined, ); console.log(JSON.stringify({ checks }));