@exercode/problem-utils 2.4.3 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/dist/cli/checkAllProblems.cjs +1 -1
  2. package/dist/cli/checkAllProblems.cjs.map +1 -1
  3. package/dist/cli/checkAllProblems.js +1 -1
  4. package/dist/cli/checkAllProblems.js.map +1 -1
  5. package/dist/helpers/checkProblemDirIsolation.cjs +1 -1
  6. package/dist/helpers/checkProblemDirIsolation.js +1 -1
  7. package/dist/helpers/cleanWorkingDirectory.cjs +1 -1
  8. package/dist/helpers/cleanWorkingDirectory.cjs.map +1 -1
  9. package/dist/helpers/cleanWorkingDirectory.d.ts +2 -6
  10. package/dist/helpers/cleanWorkingDirectory.js +1 -1
  11. package/dist/helpers/cleanWorkingDirectory.js.map +1 -1
  12. package/dist/helpers/compareExpectedOutputFiles.cjs +1 -1
  13. package/dist/helpers/compareExpectedOutputFiles.cjs.map +1 -1
  14. package/dist/helpers/compareExpectedOutputFiles.js +1 -1
  15. package/dist/helpers/compareExpectedOutputFiles.js.map +1 -1
  16. package/dist/helpers/copyTestCaseFileInput.cjs +1 -1
  17. package/dist/helpers/copyTestCaseFileInput.cjs.map +1 -1
  18. package/dist/helpers/copyTestCaseFileInput.js +1 -1
  19. package/dist/helpers/copyTestCaseFileInput.js.map +1 -1
  20. package/dist/helpers/readOutputFiles.cjs +1 -1
  21. package/dist/helpers/readOutputFiles.cjs.map +1 -1
  22. package/dist/helpers/readOutputFiles.d.ts +0 -9
  23. package/dist/helpers/readOutputFiles.js +1 -1
  24. package/dist/helpers/readOutputFiles.js.map +1 -1
  25. package/dist/helpers/runCommandInTemporaryPackageManagerProject.cjs +1 -1
  26. package/dist/helpers/runCommandInTemporaryPackageManagerProject.cjs.map +1 -1
  27. package/dist/helpers/runCommandInTemporaryPackageManagerProject.d.ts +0 -3
  28. package/dist/helpers/runCommandInTemporaryPackageManagerProject.js +1 -1
  29. package/dist/helpers/runCommandInTemporaryPackageManagerProject.js.map +1 -1
  30. package/dist/helpers/spawnSyncWithTimeout.cjs +1 -1
  31. package/dist/helpers/spawnSyncWithTimeout.cjs.map +1 -1
  32. package/dist/helpers/spawnSyncWithTimeout.d.ts +0 -7
  33. package/dist/helpers/spawnSyncWithTimeout.js +1 -1
  34. package/dist/helpers/spawnSyncWithTimeout.js.map +1 -1
  35. package/dist/helpers/startHttpServer.cjs +1 -1
  36. package/dist/helpers/startHttpServer.cjs.map +1 -1
  37. package/dist/helpers/startHttpServer.js +1 -1
  38. package/dist/helpers/startHttpServer.js.map +1 -1
  39. package/dist/index.cjs +1 -1
  40. package/dist/index.d.ts +0 -2
  41. package/dist/index.js +1 -1
  42. package/dist/presets/command.cjs +1 -1
  43. package/dist/presets/command.cjs.map +1 -1
  44. package/dist/presets/command.d.ts +0 -16
  45. package/dist/presets/command.js +1 -1
  46. package/dist/presets/command.js.map +1 -1
  47. package/dist/presets/evaluation.cjs +4 -4
  48. package/dist/presets/evaluation.cjs.map +1 -1
  49. package/dist/presets/evaluation.js +4 -4
  50. package/dist/presets/evaluation.js.map +1 -1
  51. package/dist/presets/guiCommand.cjs +3 -3
  52. package/dist/presets/guiCommand.cjs.map +1 -1
  53. package/dist/presets/guiCommand.d.ts +0 -24
  54. package/dist/presets/guiCommand.js +3 -3
  55. package/dist/presets/guiCommand.js.map +1 -1
  56. package/dist/presets/stdio.cjs +1 -1
  57. package/dist/presets/stdio.cjs.map +1 -1
  58. package/dist/presets/stdio.js +1 -1
  59. package/dist/presets/stdio.js.map +1 -1
  60. package/dist/types/language.cjs +2 -2
  61. package/dist/types/language.cjs.map +1 -1
  62. package/dist/types/language.js +2 -2
  63. package/dist/types/language.js.map +1 -1
  64. package/package.json +2 -2
  65. package/dist/helpers/runCustomRunner.cjs +0 -2
  66. package/dist/helpers/runCustomRunner.cjs.map +0 -1
  67. package/dist/helpers/runCustomRunner.d.ts +0 -8
  68. package/dist/helpers/runCustomRunner.js +0 -2
  69. package/dist/helpers/runCustomRunner.js.map +0 -1
  70. package/dist/helpers/safeFs.cjs +0 -2
  71. package/dist/helpers/safeFs.cjs.map +0 -1
  72. package/dist/helpers/safeFs.d.ts +0 -29
  73. package/dist/helpers/safeFs.js +0 -2
  74. package/dist/helpers/safeFs.js.map +0 -1
  75. package/dist/helpers/sandboxUser.cjs +0 -3
  76. package/dist/helpers/sandboxUser.cjs.map +0 -1
  77. package/dist/helpers/sandboxUser.d.ts +0 -146
  78. package/dist/helpers/sandboxUser.js +0 -3
  79. package/dist/helpers/sandboxUser.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"evaluation.cjs","names":["parseArgs","resolveCwds","passesIsolationCheckInDebugMode","path","judgeByStaticAnalysis","readProblemMarkdownFrontMatter","isSafeSubmissionOutputPath","fs","DecisionCode","parseCsvRecords","CsvParseError","parseCsv"],"sources":["../../src/presets/evaluation.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport { judgeByStaticAnalysis } from '../helpers/judgeByStaticAnalysis.js';\nimport { parseArgs } from '../helpers/parseArgs.js';\nimport { CsvParseError, type CsvParseErrorReason, parseCsv, parseCsvRecords } from '../helpers/parseCsv.js';\nimport { printTestCaseResult } from '../helpers/printTestCaseResult.js';\nimport { isSafeSubmissionOutputPath } from '../helpers/readOutputFiles.js';\nimport { readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { passesIsolationCheckInDebugMode } from '../helpers/runIsolationCheckInDebugMode.js';\nimport {\n printDebugCwdBanner,\n printDebugExpectationFailureBanner,\n resolveCwds,\n type ResolvedCwd,\n} from '../helpers/resolveCwds.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport type { TestCaseResult } from '../types/testCaseResult.js';\n\nconst DEFAULT_SUBMISSION_FILE_PATH = 'submission.csv';\nconst TEST_CASE_ID = 'evaluation';\nconst MAX_LISTED_IDS = 10;\n// Learner-controlled cells are echoed into the feedback, so clip them to keep the result line small.\nconst MAX_ECHOED_CELL_LENGTH = 50;\nconst CSV_PARSE_ERROR_MESSAGES: Record<CsvParseErrorReason, string> = {\n unterminated_quote: '引用符(`\"`)が閉じられていません',\n unexpected_quote: '引用符(`\"`)は値全体を囲む位置にしか書けません',\n text_after_quoted_field: '閉じ引用符の後に区切り文字以外の文字があります',\n};\n\nexport interface EvaluationMetric {\n /** Label shown with the score, e.g. `RMSLE`. */\n label: string;\n isHigherBetter: boolean;\n /**\n * Returns an error message when a predicted value cannot be scored (e.g. it is not a number).\n * The message is appended to the row's line number and the offending value, so phrase it as a\n * predicate such as `数値ではありません`. It also validates the ground truth when it is loaded.\n */\n validatePrediction?: (prediction: string) => string | undefined;\n /** Computes the score from predictions and answers aligned by id. Both are raw CSV cell strings. */\n compute: (predictions: readonly string[], answers: readonly string[]) => number;\n}\n\nexport interface EvaluationJudgePresetOptions {\n /** CSV file holding the hidden ground truth, relative to the problem directory. */\n answerFilePath: string;\n /** CSV file the submission must contain. Defaults to `submission.csv`. */\n submissionFilePath?: string;\n /** Column that identifies each row in both CSV files. */\n idColumn: string;\n /** Column holding the answer in the ground truth and the prediction in the submission. */\n targetColumn: string;\n metric: EvaluationMetric;\n /**\n * Score required for acceptance: the submission is accepted when its score is at least this value\n * for a higher-is-better metric and at most this value otherwise. Every valid submission is\n * accepted when omitted.\n */\n acceptableScore?: number;\n}\n\nexport const evaluationMetrics = {\n rmsle: {\n label: 'RMSLE',\n isHigherBetter: false,\n validatePrediction: (prediction: string) =>\n validateFiniteNumber(prediction) ?? (Number(prediction) < 0 ? '負の値です' : undefined),\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n Math.sqrt(\n mean(\n predictions.map(\n (prediction, index) => (Math.log1p(Number(prediction)) - Math.log1p(Number(answers[index]))) ** 2\n )\n )\n ),\n },\n rmse: {\n label: 'RMSE',\n isHigherBetter: false,\n validatePrediction: validateFiniteNumber,\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n Math.sqrt(mean(predictions.map((prediction, index) => (Number(prediction) - Number(answers[index])) ** 2))),\n },\n mae: {\n label: 'MAE',\n isHigherBetter: false,\n validatePrediction: validateFiniteNumber,\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n mean(predictions.map((prediction, index) => Math.abs(Number(prediction) - Number(answers[index])))),\n },\n accuracy: {\n label: 'Accuracy',\n isHigherBetter: true,\n validatePrediction: (prediction: string) => (prediction.trim() === '' ? '空です' : undefined),\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n mean(predictions.map((prediction, index) => (prediction.trim() === answers[index]?.trim() ? 1 : 0))),\n },\n} as const satisfies Record<string, EvaluationMetric>;\n\n/**\n * A preset for Kaggle-style model evaluation problems: the submission is a CSV of predictions for a\n * hidden test set, and the judge reports the metric value as `score` instead of comparing outputs.\n * The problem directory keeps the ground truth CSV, so it is never shipped to learners.\n *\n * @example\n * Create `judge.ts`:\n * ```ts\n * import { evaluationJudgePreset, evaluationMetrics } from '@exercode/problem-utils/presets/evaluation';\n *\n * await evaluationJudgePreset(import.meta.dirname, {\n * answerFilePath: 'evaluation/answer.csv',\n * idColumn: 'Id',\n * targetColumn: 'TradePrice',\n * metric: evaluationMetrics.rmsle,\n * acceptableScore: 0.5,\n * });\n * ```\n *\n * Run `bun judge.ts model_answers/default` to judge one submission, or `bun judge.ts` to judge every\n * `<problemDir>/model_answers/*` and `<problemDir>/model_answers.fails/*` directory for debugging.\n */\nexport async function evaluationJudgePreset(problemDir: string, options: EvaluationJudgePresetOptions): Promise<void> {\n const args = parseArgs(process.argv);\n const { cwds, isDebugMode } = await resolveCwds(problemDir, args.cwd);\n\n if (isDebugMode && !(await passesIsolationCheckInDebugMode(problemDir, cwds, args.params))) {\n process.exitCode = 1;\n return;\n }\n\n const answer = await readAnswer(path.join(problemDir, options.answerFilePath), options);\n for (const resolvedCwd of cwds) {\n if (isDebugMode) printDebugCwdBanner(problemDir, resolvedCwd);\n const result = await judgeSubmission(problemDir, resolvedCwd.cwd, answer, options);\n printTestCaseResult({ testCaseId: TEST_CASE_ID, ...result });\n if (isDebugMode && !matchesExpectedResult(resolvedCwd, result)) {\n process.exitCode = 1;\n printDebugExpectationFailureBanner(problemDir, resolvedCwd);\n }\n }\n}\n\nasync function judgeSubmission(\n problemDir: string,\n cwd: string,\n answer: ReadonlyMap<string, string>,\n options: EvaluationJudgePresetOptions\n): Promise<Omit<TestCaseResult, 'testCaseId'>> {\n const staticAnalysisResult = await judgeByStaticAnalysis(cwd, await readProblemMarkdownFrontMatter(problemDir));\n if (staticAnalysisResult) return staticAnalysisResult;\n\n const submissionFilePath = options.submissionFilePath ?? DEFAULT_SUBMISSION_FILE_PATH;\n let submissionText: string;\n try {\n const resolvedPath = path.join(cwd, submissionFilePath);\n // The harness reads the file as the trusted user, so refuse symlinks escaping the submission.\n if (!(await isSafeSubmissionOutputPath(cwd, resolvedPath))) throw new Error('unsafe path');\n submissionText = await fs.readFile(resolvedPath, 'utf8');\n } catch {\n return {\n decisionCode: DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,\n feedbackMarkdown: `\\`${submissionFilePath}\\`が見つかりません。予測結果のCSVファイルを\\`${submissionFilePath}\\`という名前で提出してください。`,\n };\n }\n\n const predictions = readPredictions(submissionText, submissionFilePath, answer, options);\n if (typeof predictions === 'string') {\n return { decisionCode: DecisionCode.WRONG_ANSWER, feedbackMarkdown: predictions };\n }\n\n const answerValues = [...answer.values()];\n const score = options.metric.compute(\n [...answer.keys()].map((id) => predictions.get(id) as string),\n answerValues\n );\n if (!Number.isFinite(score)) {\n return {\n decisionCode: DecisionCode.WRONG_ANSWER,\n feedbackMarkdown: `予測値が極端なため、${options.metric.label}を計算できませんでした。予測値を見直してください。`,\n };\n }\n const isAccepted =\n options.acceptableScore === undefined ||\n (options.metric.isHigherBetter ? score >= options.acceptableScore : score <= options.acceptableScore);\n return {\n decisionCode: isAccepted ? DecisionCode.ACCEPTED : DecisionCode.WRONG_ANSWER,\n score,\n scoreLabel: options.metric.label,\n feedbackMarkdown: buildScoreMarkdown(score, answerValues.length, isAccepted, options),\n };\n}\n\nfunction buildScoreMarkdown(\n score: number,\n rowCount: number,\n isAccepted: boolean,\n options: EvaluationJudgePresetOptions\n): string {\n const { label, isHigherBetter } = options.metric;\n const precision = findDistinguishingPrecision(score, options.acceptableScore);\n const acceptanceLine =\n options.acceptableScore === undefined\n ? ''\n : `\\n合格基準: ${label} ${isHigherBetter ? '≧' : '≦'} ${formatScore(options.acceptableScore, precision)}(${isAccepted ? '達成' : '未達成'})`;\n return `| 指標 | スコア |\n| ---- | ------ |\n| ${label} | ${formatScore(score, precision)} |\n\n評価件数: ${rowCount.toLocaleString('en-US')}件${acceptanceLine}\n`;\n}\n\n/**\n * Finds the number of significant digits at which the score and the threshold render differently\n * (or stop changing), so the feedback never shows a pair that contradicts the decision.\n */\nfunction findDistinguishingPrecision(score: number, threshold: number | undefined): number {\n const minimumPrecision = 6;\n if (threshold === undefined || score === threshold) return minimumPrecision;\n for (let precision = minimumPrecision; precision < 17; precision++) {\n if (formatScore(score, precision) !== formatScore(threshold, precision)) return precision;\n }\n return 17;\n}\n\nfunction formatScore(score: number, precision: number): string {\n return Number(score.toPrecision(precision)).toString();\n}\n\nfunction readPredictions(\n submissionText: string,\n submissionFilePath: string,\n answer: ReadonlyMap<string, string>,\n options: EvaluationJudgePresetOptions\n): Map<string, string> | string {\n let records: ReturnType<typeof parseCsvRecords>;\n try {\n records = parseCsvRecords(submissionText);\n } catch (error) {\n const detail =\n error instanceof CsvParseError ? `${error.lineNumber}行目付近: ${CSV_PARSE_ERROR_MESSAGES[error.reason]}` : '';\n return `\\`${submissionFilePath}\\`をCSVとして読み込めませんでした。${detail}`;\n }\n const columns = findColumns(records[0]?.cells ?? [], options);\n if (!columns) {\n return `\\`${submissionFilePath}\\`の1行目には\\`${options.idColumn}\\`列と\\`${options.targetColumn}\\`列のヘッダーが必要です。`;\n }\n\n const predictions = new Map<string, string>();\n const problems: string[] = [];\n let problemCount = 0;\n const reportProblem = (problem: string): void => {\n problemCount++;\n if (problems.length < MAX_LISTED_IDS) problems.push(problem);\n };\n for (const { cells: row, lineNumber } of records.slice(1)) {\n if (isBlankRow(row)) continue;\n const id = row[columns.idIndex]?.trim() ?? '';\n const prediction = row[columns.targetIndex] ?? '';\n if (!answer.has(id)) {\n reportProblem(`${lineNumber}行目: \\`${options.idColumn}\\`が\\`${clip(id)}\\`の行は評価対象ではありません`);\n } else if (predictions.has(id)) {\n reportProblem(`${lineNumber}行目: \\`${options.idColumn}\\`が\\`${clip(id)}\\`の行が重複しています`);\n } else {\n const validationError = options.metric.validatePrediction?.(prediction);\n if (validationError) {\n reportProblem(`${lineNumber}行目: \\`${options.targetColumn}\\`の値\\`${clip(prediction)}\\`は${validationError}`);\n }\n predictions.set(id, prediction);\n }\n }\n const missingIds = [...answer.keys()].filter((id) => !predictions.has(id));\n if (missingIds.length > 0) {\n // Missing rows are the most actionable problem, so they must survive the listing cap.\n problemCount++;\n problems.unshift(\n `\\`${options.idColumn}\\`が${listIds(missingIds)}の行がありません(不足 ${missingIds.length.toLocaleString('en-US')}件)`\n );\n }\n if (problemCount > 0) {\n return `\\`${submissionFilePath}\\`の内容に問題があります。\n\n${problems.map((problem) => `- ${problem}`).join('\\n')}${problemCount > problems.length ? `\\n- ほか${(problemCount - problems.length).toLocaleString('en-US')}件` : ''}\n`;\n }\n return predictions;\n}\n\nasync function readAnswer(answerFilePath: string, options: EvaluationJudgePresetOptions): Promise<Map<string, string>> {\n const rows = parseCsv(await fs.readFile(answerFilePath, 'utf8'));\n const columns = findColumns(rows[0] ?? [], options);\n if (!columns) {\n throw new Error(`answer file must have ${options.idColumn} and ${options.targetColumn} columns: ${answerFilePath}`);\n }\n const answer = new Map<string, string>();\n for (const row of rows.slice(1)) {\n if (isBlankRow(row)) continue;\n const id = row[columns.idIndex]?.trim() ?? '';\n const value = row[columns.targetIndex] ?? '';\n if (!id) throw new Error(`empty ${options.idColumn} in answer file: ${answerFilePath}`);\n if (answer.has(id)) throw new Error(`duplicate ${options.idColumn} in answer file: ${id}`);\n const validationError = options.metric.validatePrediction?.(value);\n if (validationError)\n throw new Error(`invalid ${options.targetColumn} in answer file for ${id}: ${validationError}`);\n answer.set(id, value);\n }\n if (answer.size === 0) throw new Error(`answer file has no rows: ${answerFilePath}`);\n return answer;\n}\n\nfunction findColumns(\n header: readonly string[],\n options: EvaluationJudgePresetOptions\n): { idIndex: number; targetIndex: number } | undefined {\n const names = header.map((name) => name.trim());\n const idIndex = names.indexOf(options.idColumn);\n const targetIndex = names.indexOf(options.targetColumn);\n return idIndex === -1 || targetIndex === -1 ? undefined : { idIndex, targetIndex };\n}\n\nfunction isBlankRow(row: readonly string[]): boolean {\n return row.every((cell) => cell.trim() === '');\n}\n\nfunction listIds(ids: readonly string[]): string {\n const listed = ids.slice(0, MAX_LISTED_IDS).map((id) => `\\`${clip(id)}\\``);\n return ids.length > MAX_LISTED_IDS ? `${listed.join(', ')}, ...` : listed.join(', ');\n}\n\nfunction clip(value: string): string {\n // Control characters and backticks would break the markdown list item and inline code the value is echoed in.\n const printable = value.replaceAll(/[\\p{Cc}]/gu, ' ').replaceAll('`', \"'\");\n if (printable.trim() === '') return '(空)';\n return printable.length > MAX_ECHOED_CELL_LENGTH ? `${printable.slice(0, MAX_ECHOED_CELL_LENGTH)}…` : printable;\n}\n\nfunction matchesExpectedResult(resolvedCwd: ResolvedCwd, result: Pick<TestCaseResult, 'decisionCode'>): boolean {\n return (result.decisionCode === DecisionCode.ACCEPTED) === (resolvedCwd.expectedResult === 'accepted');\n}\n\nfunction validateFiniteNumber(prediction: string): string | undefined {\n return Number.isFinite(Number(prediction)) && prediction.trim() !== '' ? undefined : '数値ではありません';\n}\n\nfunction mean(values: readonly number[]): number {\n return values.reduce((sum, value) => sum + value, 0) / values.length;\n}\n"],"mappings":"goBAmBA,MAKM,EAAgE,CACpE,mBAAoB,qBACpB,iBAAkB,4BAClB,wBAAyB,yBAC3B,EAkCa,EAAoB,CAC/B,MAAO,CACL,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AG,EAAO,KAAK,IAAI,EAAE,OAAS,EAAO,KAAK,IAAI,CACrF,CAEA,SAAS,EAAK,EAAuB,CAEnC,IAAM,EAAY,EAAM,WAAW,aAAc,GAAG,CAAC,CAAC,WAAW,IAAK,GAAG,EAEzE,OADI,EAAU,KAAK,IAAM,GAAW,MAC7B,EAAU,OAAS,GAAyB,GAAG,EAAU,MAAM,EAAG,EAAsB,EAAE,GAAK,CACxG,CAEA,SAAS,EAAsB,EAA0B,EAAuD,CAC9G,OAAQ,EAAO,eAAiBC,EAAAA,aAAa,WAAe,EAAY,iBAAmB,WAC7F,CAEA,SAAS,EAAqB,EAAwC,CACpE,OAAO,OAAO,SAAS,OAAO,CAAU,CAAC,GAAK,EAAW,KAAK,IAAM,GAAK,IAAA,GAAY,WACvF,CAEA,SAAS,EAAK,EAAmC,CAC/C,OAAO,EAAO,QAAQ,EAAK,IAAU,EAAM,EAAO,CAAC,EAAI,EAAO,MAChE"}
1
+ {"version":3,"file":"evaluation.cjs","names":["parseArgs","resolveCwds","passesIsolationCheckInDebugMode","path","judgeByStaticAnalysis","readProblemMarkdownFrontMatter","fs","DecisionCode","parseCsvRecords","CsvParseError","parseCsv"],"sources":["../../src/presets/evaluation.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport { judgeByStaticAnalysis } from '../helpers/judgeByStaticAnalysis.js';\nimport { parseArgs } from '../helpers/parseArgs.js';\nimport { CsvParseError, type CsvParseErrorReason, parseCsv, parseCsvRecords } from '../helpers/parseCsv.js';\nimport { printTestCaseResult } from '../helpers/printTestCaseResult.js';\nimport { readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { passesIsolationCheckInDebugMode } from '../helpers/runIsolationCheckInDebugMode.js';\nimport {\n printDebugCwdBanner,\n printDebugExpectationFailureBanner,\n resolveCwds,\n type ResolvedCwd,\n} from '../helpers/resolveCwds.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport type { TestCaseResult } from '../types/testCaseResult.js';\n\nconst DEFAULT_SUBMISSION_FILE_PATH = 'submission.csv';\nconst TEST_CASE_ID = 'evaluation';\nconst MAX_LISTED_IDS = 10;\n// Learner-controlled cells are echoed into the feedback, so clip them to keep the result line small.\nconst MAX_ECHOED_CELL_LENGTH = 50;\nconst CSV_PARSE_ERROR_MESSAGES: Record<CsvParseErrorReason, string> = {\n unterminated_quote: '引用符(`\"`)が閉じられていません',\n unexpected_quote: '引用符(`\"`)は値全体を囲む位置にしか書けません',\n text_after_quoted_field: '閉じ引用符の後に区切り文字以外の文字があります',\n};\n\nexport interface EvaluationMetric {\n /** Label shown with the score, e.g. `RMSLE`. */\n label: string;\n isHigherBetter: boolean;\n /**\n * Returns an error message when a predicted value cannot be scored (e.g. it is not a number).\n * The message is appended to the row's line number and the offending value, so phrase it as a\n * predicate such as `数値ではありません`. It also validates the ground truth when it is loaded.\n */\n validatePrediction?: (prediction: string) => string | undefined;\n /** Computes the score from predictions and answers aligned by id. Both are raw CSV cell strings. */\n compute: (predictions: readonly string[], answers: readonly string[]) => number;\n}\n\nexport interface EvaluationJudgePresetOptions {\n /** CSV file holding the hidden ground truth, relative to the problem directory. */\n answerFilePath: string;\n /** CSV file the submission must contain. Defaults to `submission.csv`. */\n submissionFilePath?: string;\n /** Column that identifies each row in both CSV files. */\n idColumn: string;\n /** Column holding the answer in the ground truth and the prediction in the submission. */\n targetColumn: string;\n metric: EvaluationMetric;\n /**\n * Score required for acceptance: the submission is accepted when its score is at least this value\n * for a higher-is-better metric and at most this value otherwise. Every valid submission is\n * accepted when omitted.\n */\n acceptableScore?: number;\n}\n\nexport const evaluationMetrics = {\n rmsle: {\n label: 'RMSLE',\n isHigherBetter: false,\n validatePrediction: (prediction: string) =>\n validateFiniteNumber(prediction) ?? (Number(prediction) < 0 ? '負の値です' : undefined),\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n Math.sqrt(\n mean(\n predictions.map(\n (prediction, index) => (Math.log1p(Number(prediction)) - Math.log1p(Number(answers[index]))) ** 2\n )\n )\n ),\n },\n rmse: {\n label: 'RMSE',\n isHigherBetter: false,\n validatePrediction: validateFiniteNumber,\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n Math.sqrt(mean(predictions.map((prediction, index) => (Number(prediction) - Number(answers[index])) ** 2))),\n },\n mae: {\n label: 'MAE',\n isHigherBetter: false,\n validatePrediction: validateFiniteNumber,\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n mean(predictions.map((prediction, index) => Math.abs(Number(prediction) - Number(answers[index])))),\n },\n accuracy: {\n label: 'Accuracy',\n isHigherBetter: true,\n validatePrediction: (prediction: string) => (prediction.trim() === '' ? '空です' : undefined),\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n mean(predictions.map((prediction, index) => (prediction.trim() === answers[index]?.trim() ? 1 : 0))),\n },\n} as const satisfies Record<string, EvaluationMetric>;\n\n/**\n * A preset for Kaggle-style model evaluation problems: the submission is a CSV of predictions for a\n * hidden test set, and the judge reports the metric value as `score` instead of comparing outputs.\n * The problem directory keeps the ground truth CSV, so it is never shipped to learners.\n *\n * @example\n * Create `judge.ts`:\n * ```ts\n * import { evaluationJudgePreset, evaluationMetrics } from '@exercode/problem-utils/presets/evaluation';\n *\n * await evaluationJudgePreset(import.meta.dirname, {\n * answerFilePath: 'evaluation/answer.csv',\n * idColumn: 'Id',\n * targetColumn: 'TradePrice',\n * metric: evaluationMetrics.rmsle,\n * acceptableScore: 0.5,\n * });\n * ```\n *\n * Run `bun judge.ts model_answers/default` to judge one submission, or `bun judge.ts` to judge every\n * `<problemDir>/model_answers/*` and `<problemDir>/model_answers.fails/*` directory for debugging.\n */\nexport async function evaluationJudgePreset(problemDir: string, options: EvaluationJudgePresetOptions): Promise<void> {\n const args = parseArgs(process.argv);\n const { cwds, isDebugMode } = await resolveCwds(problemDir, args.cwd);\n\n if (isDebugMode && !(await passesIsolationCheckInDebugMode(problemDir, cwds, args.params))) {\n process.exitCode = 1;\n return;\n }\n\n const answer = await readAnswer(path.join(problemDir, options.answerFilePath), options);\n for (const resolvedCwd of cwds) {\n if (isDebugMode) printDebugCwdBanner(problemDir, resolvedCwd);\n const result = await judgeSubmission(problemDir, resolvedCwd.cwd, answer, options);\n printTestCaseResult({ testCaseId: TEST_CASE_ID, ...result });\n if (isDebugMode && !matchesExpectedResult(resolvedCwd, result)) {\n process.exitCode = 1;\n printDebugExpectationFailureBanner(problemDir, resolvedCwd);\n }\n }\n}\n\nasync function judgeSubmission(\n problemDir: string,\n cwd: string,\n answer: ReadonlyMap<string, string>,\n options: EvaluationJudgePresetOptions\n): Promise<Omit<TestCaseResult, 'testCaseId'>> {\n const staticAnalysisResult = await judgeByStaticAnalysis(cwd, await readProblemMarkdownFrontMatter(problemDir));\n if (staticAnalysisResult) return staticAnalysisResult;\n\n const submissionFilePath = options.submissionFilePath ?? DEFAULT_SUBMISSION_FILE_PATH;\n let submissionText: string;\n try {\n const resolvedPath = path.join(cwd, submissionFilePath);\n submissionText = await fs.readFile(resolvedPath, 'utf8');\n } catch {\n return {\n decisionCode: DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,\n feedbackMarkdown: `\\`${submissionFilePath}\\`が見つかりません。予測結果のCSVファイルを\\`${submissionFilePath}\\`という名前で提出してください。`,\n };\n }\n\n const predictions = readPredictions(submissionText, submissionFilePath, answer, options);\n if (typeof predictions === 'string') {\n return { decisionCode: DecisionCode.WRONG_ANSWER, feedbackMarkdown: predictions };\n }\n\n const answerValues = [...answer.values()];\n const score = options.metric.compute(\n [...answer.keys()].map((id) => predictions.get(id) as string),\n answerValues\n );\n if (!Number.isFinite(score)) {\n return {\n decisionCode: DecisionCode.WRONG_ANSWER,\n feedbackMarkdown: `予測値が極端なため、${options.metric.label}を計算できませんでした。予測値を見直してください。`,\n };\n }\n const isAccepted =\n options.acceptableScore === undefined ||\n (options.metric.isHigherBetter ? score >= options.acceptableScore : score <= options.acceptableScore);\n return {\n decisionCode: isAccepted ? DecisionCode.ACCEPTED : DecisionCode.WRONG_ANSWER,\n score,\n scoreLabel: options.metric.label,\n feedbackMarkdown: buildScoreMarkdown(score, answerValues.length, isAccepted, options),\n };\n}\n\nfunction buildScoreMarkdown(\n score: number,\n rowCount: number,\n isAccepted: boolean,\n options: EvaluationJudgePresetOptions\n): string {\n const { label, isHigherBetter } = options.metric;\n const precision = findDistinguishingPrecision(score, options.acceptableScore);\n const acceptanceLine =\n options.acceptableScore === undefined\n ? ''\n : `\\n合格基準: ${label} ${isHigherBetter ? '≧' : '≦'} ${formatScore(options.acceptableScore, precision)}(${isAccepted ? '達成' : '未達成'})`;\n return `| 指標 | スコア |\n| ---- | ------ |\n| ${label} | ${formatScore(score, precision)} |\n\n評価件数: ${rowCount.toLocaleString('en-US')}件${acceptanceLine}\n`;\n}\n\n/**\n * Finds the number of significant digits at which the score and the threshold render differently\n * (or stop changing), so the feedback never shows a pair that contradicts the decision.\n */\nfunction findDistinguishingPrecision(score: number, threshold: number | undefined): number {\n const minimumPrecision = 6;\n if (threshold === undefined || score === threshold) return minimumPrecision;\n for (let precision = minimumPrecision; precision < 17; precision++) {\n if (formatScore(score, precision) !== formatScore(threshold, precision)) return precision;\n }\n return 17;\n}\n\nfunction formatScore(score: number, precision: number): string {\n return Number(score.toPrecision(precision)).toString();\n}\n\nfunction readPredictions(\n submissionText: string,\n submissionFilePath: string,\n answer: ReadonlyMap<string, string>,\n options: EvaluationJudgePresetOptions\n): Map<string, string> | string {\n let records: ReturnType<typeof parseCsvRecords>;\n try {\n records = parseCsvRecords(submissionText);\n } catch (error) {\n const detail =\n error instanceof CsvParseError ? `${error.lineNumber}行目付近: ${CSV_PARSE_ERROR_MESSAGES[error.reason]}` : '';\n return `\\`${submissionFilePath}\\`をCSVとして読み込めませんでした。${detail}`;\n }\n const columns = findColumns(records[0]?.cells ?? [], options);\n if (!columns) {\n return `\\`${submissionFilePath}\\`の1行目には\\`${options.idColumn}\\`列と\\`${options.targetColumn}\\`列のヘッダーが必要です。`;\n }\n\n const predictions = new Map<string, string>();\n const problems: string[] = [];\n let problemCount = 0;\n const reportProblem = (problem: string): void => {\n problemCount++;\n if (problems.length < MAX_LISTED_IDS) problems.push(problem);\n };\n for (const { cells: row, lineNumber } of records.slice(1)) {\n if (isBlankRow(row)) continue;\n const id = row[columns.idIndex]?.trim() ?? '';\n const prediction = row[columns.targetIndex] ?? '';\n if (!answer.has(id)) {\n reportProblem(`${lineNumber}行目: \\`${options.idColumn}\\`が\\`${clip(id)}\\`の行は評価対象ではありません`);\n } else if (predictions.has(id)) {\n reportProblem(`${lineNumber}行目: \\`${options.idColumn}\\`が\\`${clip(id)}\\`の行が重複しています`);\n } else {\n const validationError = options.metric.validatePrediction?.(prediction);\n if (validationError) {\n reportProblem(`${lineNumber}行目: \\`${options.targetColumn}\\`の値\\`${clip(prediction)}\\`は${validationError}`);\n }\n predictions.set(id, prediction);\n }\n }\n const missingIds = [...answer.keys()].filter((id) => !predictions.has(id));\n if (missingIds.length > 0) {\n // Missing rows are the most actionable problem, so they must survive the listing cap.\n problemCount++;\n problems.unshift(\n `\\`${options.idColumn}\\`が${listIds(missingIds)}の行がありません(不足 ${missingIds.length.toLocaleString('en-US')}件)`\n );\n }\n if (problemCount > 0) {\n return `\\`${submissionFilePath}\\`の内容に問題があります。\n\n${problems.map((problem) => `- ${problem}`).join('\\n')}${problemCount > problems.length ? `\\n- ほか${(problemCount - problems.length).toLocaleString('en-US')}件` : ''}\n`;\n }\n return predictions;\n}\n\nasync function readAnswer(answerFilePath: string, options: EvaluationJudgePresetOptions): Promise<Map<string, string>> {\n const rows = parseCsv(await fs.readFile(answerFilePath, 'utf8'));\n const columns = findColumns(rows[0] ?? [], options);\n if (!columns) {\n throw new Error(`answer file must have ${options.idColumn} and ${options.targetColumn} columns: ${answerFilePath}`);\n }\n const answer = new Map<string, string>();\n for (const row of rows.slice(1)) {\n if (isBlankRow(row)) continue;\n const id = row[columns.idIndex]?.trim() ?? '';\n const value = row[columns.targetIndex] ?? '';\n if (!id) throw new Error(`empty ${options.idColumn} in answer file: ${answerFilePath}`);\n if (answer.has(id)) throw new Error(`duplicate ${options.idColumn} in answer file: ${id}`);\n const validationError = options.metric.validatePrediction?.(value);\n if (validationError)\n throw new Error(`invalid ${options.targetColumn} in answer file for ${id}: ${validationError}`);\n answer.set(id, value);\n }\n if (answer.size === 0) throw new Error(`answer file has no rows: ${answerFilePath}`);\n return answer;\n}\n\nfunction findColumns(\n header: readonly string[],\n options: EvaluationJudgePresetOptions\n): { idIndex: number; targetIndex: number } | undefined {\n const names = header.map((name) => name.trim());\n const idIndex = names.indexOf(options.idColumn);\n const targetIndex = names.indexOf(options.targetColumn);\n return idIndex === -1 || targetIndex === -1 ? undefined : { idIndex, targetIndex };\n}\n\nfunction isBlankRow(row: readonly string[]): boolean {\n return row.every((cell) => cell.trim() === '');\n}\n\nfunction listIds(ids: readonly string[]): string {\n const listed = ids.slice(0, MAX_LISTED_IDS).map((id) => `\\`${clip(id)}\\``);\n return ids.length > MAX_LISTED_IDS ? `${listed.join(', ')}, ...` : listed.join(', ');\n}\n\nfunction clip(value: string): string {\n // Control characters and backticks would break the markdown list item and inline code the value is echoed in.\n const printable = value.replaceAll(/[\\p{Cc}]/gu, ' ').replaceAll('`', \"'\");\n if (printable.trim() === '') return '(空)';\n return printable.length > MAX_ECHOED_CELL_LENGTH ? `${printable.slice(0, MAX_ECHOED_CELL_LENGTH)}…` : printable;\n}\n\nfunction matchesExpectedResult(resolvedCwd: ResolvedCwd, result: Pick<TestCaseResult, 'decisionCode'>): boolean {\n return (result.decisionCode === DecisionCode.ACCEPTED) === (resolvedCwd.expectedResult === 'accepted');\n}\n\nfunction validateFiniteNumber(prediction: string): string | undefined {\n return Number.isFinite(Number(prediction)) && prediction.trim() !== '' ? undefined : '数値ではありません';\n}\n\nfunction mean(values: readonly number[]): number {\n return values.reduce((sum, value) => sum + value, 0) / values.length;\n}\n"],"mappings":"olBAkBA,MAKM,EAAgE,CACpE,mBAAoB,qBACpB,iBAAkB,4BACl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,OAAS,EAAO,KAAK,IAAI,CACrF,CAEA,SAAS,EAAK,EAAuB,CAEnC,IAAM,EAAY,EAAM,WAAW,aAAc,GAAG,CAAC,CAAC,WAAW,IAAK,GAAG,EAEzE,OADI,EAAU,KAAK,IAAM,GAAW,MAC7B,EAAU,OAAS,GAAyB,GAAG,EAAU,MAAM,EAAG,EAAsB,EAAE,GAAK,CACxG,CAEA,SAAS,EAAsB,EAA0B,EAAuD,CAC9G,OAAQ,EAAO,eAAiBC,EAAAA,aAAa,WAAe,EAAY,iBAAmB,WAC7F,CAEA,SAAS,EAAqB,EAAwC,CACpE,OAAO,OAAO,SAAS,OAAO,CAAU,CAAC,GAAK,EAAW,KAAK,IAAM,GAAK,IAAA,GAAY,WACvF,CAEA,SAAS,EAAK,EAAmC,CAC/C,OAAO,EAAO,QAAQ,EAAK,IAAU,EAAM,EAAO,CAAC,EAAI,EAAO,MAChE"}
@@ -1,11 +1,11 @@
1
- import{printTestCaseResult as e}from"../helpers/printTestCaseResult.js";import{isSafeSubmissionOutputPath as t}from"../helpers/readOutputFiles.js";import{parseArgs as n}from"../helpers/parseArgs.js";import{CsvParseError as r,parseCsv as i,parseCsvRecords as a}from"../helpers/parseCsv.js";import{DecisionCode as o}from"../types/decisionCode.js";import{judgeByStaticAnalysis as s}from"../helpers/judgeByStaticAnalysis.js";import{passesIsolationCheckInDebugMode as c}from"../helpers/runIsolationCheckInDebugMode.js";import{readProblemMarkdownFrontMatter as l}from"../helpers/readProblemMarkdownFrontMatter.js";import{printDebugCwdBanner as u,printDebugExpectationFailureBanner as d,resolveCwds as f}from"../helpers/resolveCwds.js";import p from"node:path";import m from"node:fs/promises";const h={unterminated_quote:'引用符(`"`)が閉じられていません',unexpected_quote:'引用符(`"`)は値全体を囲む位置にしか書けません',text_after_quoted_field:`閉じ引用符の後に区切り文字以外の文字があります`},g={rmsle:{label:`RMSLE`,isHigherBetter:!1,validatePrediction:e=>k(e)??(Number(e)<0?`負の値です`:void 0),compute:(e,t)=>Math.sqrt(A(e.map((e,n)=>(Math.log1p(Number(e))-Math.log1p(Number(t[n])))**2)))},rmse:{label:`RMSE`,isHigherBetter:!1,validatePrediction:k,compute:(e,t)=>Math.sqrt(A(e.map((e,n)=>(Number(e)-Number(t[n]))**2)))},mae:{label:`MAE`,isHigherBetter:!1,validatePrediction:k,compute:(e,t)=>A(e.map((e,n)=>Math.abs(Number(e)-Number(t[n]))))},accuracy:{label:`Accuracy`,isHigherBetter:!0,validatePrediction:e=>e.trim()===``?`空です`:void 0,compute:(e,t)=>A(e.map((e,n)=>+(e.trim()===t[n]?.trim())))}};async function _(t,r){let i=n(process.argv),{cwds:a,isDebugMode:o}=await f(t,i.cwd);if(o&&!await c(t,a,i.params)){process.exitCode=1;return}let s=await C(p.join(t,r.answerFilePath),r);for(let n of a){o&&u(t,n);let i=await v(t,n.cwd,s,r);e({testCaseId:`evaluation`,...i}),o&&!O(n,i)&&(process.exitCode=1,d(t,n))}}async function v(e,n,r,i){let a=await s(n,await l(e));if(a)return a;let c=i.submissionFilePath??`submission.csv`,u;try{let e=p.join(n,c);if(!await t(n,e))throw Error(`unsafe path`);u=await m.readFile(e,`utf8`)}catch{return{decisionCode:o.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,feedbackMarkdown:`\`${c}\`が見つかりません。予測結果のCSVファイルを\`${c}\`という名前で提出してください。`}}let d=S(u,c,r,i);if(typeof d==`string`)return{decisionCode:o.WRONG_ANSWER,feedbackMarkdown:d};let f=[...r.values()],h=i.metric.compute([...r.keys()].map(e=>d.get(e)),f);if(!Number.isFinite(h))return{decisionCode:o.WRONG_ANSWER,feedbackMarkdown:`予測値が極端なため、${i.metric.label}を計算できませんでした。予測値を見直してください。`};let g=i.acceptableScore===void 0||(i.metric.isHigherBetter?h>=i.acceptableScore:h<=i.acceptableScore);return{decisionCode:g?o.ACCEPTED:o.WRONG_ANSWER,score:h,scoreLabel:i.metric.label,feedbackMarkdown:y(h,f.length,g,i)}}function y(e,t,n,r){let{label:i,isHigherBetter:a}=r.metric,o=b(e,r.acceptableScore),s=r.acceptableScore===void 0?``:`\n合格基準: ${i} ${a?`≧`:`≦`} ${x(r.acceptableScore,o)}(${n?`達成`:`未達成`})`;return`| 指標 | スコア |
1
+ import{printTestCaseResult as e}from"../helpers/printTestCaseResult.js";import{parseArgs as t}from"../helpers/parseArgs.js";import{CsvParseError as n,parseCsv as r,parseCsvRecords as i}from"../helpers/parseCsv.js";import{DecisionCode as a}from"../types/decisionCode.js";import{judgeByStaticAnalysis as o}from"../helpers/judgeByStaticAnalysis.js";import{passesIsolationCheckInDebugMode as s}from"../helpers/runIsolationCheckInDebugMode.js";import{readProblemMarkdownFrontMatter as c}from"../helpers/readProblemMarkdownFrontMatter.js";import{printDebugCwdBanner as l,printDebugExpectationFailureBanner as u,resolveCwds as d}from"../helpers/resolveCwds.js";import f from"node:path";import p from"node:fs/promises";const m={unterminated_quote:'引用符(`"`)が閉じられていません',unexpected_quote:'引用符(`"`)は値全体を囲む位置にしか書けません',text_after_quoted_field:`閉じ引用符の後に区切り文字以外の文字があります`},h={rmsle:{label:`RMSLE`,isHigherBetter:!1,validatePrediction:e=>O(e)??(Number(e)<0?`負の値です`:void 0),compute:(e,t)=>Math.sqrt(k(e.map((e,n)=>(Math.log1p(Number(e))-Math.log1p(Number(t[n])))**2)))},rmse:{label:`RMSE`,isHigherBetter:!1,validatePrediction:O,compute:(e,t)=>Math.sqrt(k(e.map((e,n)=>(Number(e)-Number(t[n]))**2)))},mae:{label:`MAE`,isHigherBetter:!1,validatePrediction:O,compute:(e,t)=>k(e.map((e,n)=>Math.abs(Number(e)-Number(t[n]))))},accuracy:{label:`Accuracy`,isHigherBetter:!0,validatePrediction:e=>e.trim()===``?`空です`:void 0,compute:(e,t)=>k(e.map((e,n)=>+(e.trim()===t[n]?.trim())))}};async function g(n,r){let i=t(process.argv),{cwds:a,isDebugMode:o}=await d(n,i.cwd);if(o&&!await s(n,a,i.params)){process.exitCode=1;return}let c=await S(f.join(n,r.answerFilePath),r);for(let t of a){o&&l(n,t);let i=await _(n,t.cwd,c,r);e({testCaseId:`evaluation`,...i}),o&&!D(t,i)&&(process.exitCode=1,u(n,t))}}async function _(e,t,n,r){let i=await o(t,await c(e));if(i)return i;let s=r.submissionFilePath??`submission.csv`,l;try{let e=f.join(t,s);l=await p.readFile(e,`utf8`)}catch{return{decisionCode:a.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,feedbackMarkdown:`\`${s}\`が見つかりません。予測結果のCSVファイルを\`${s}\`という名前で提出してください。`}}let u=x(l,s,n,r);if(typeof u==`string`)return{decisionCode:a.WRONG_ANSWER,feedbackMarkdown:u};let d=[...n.values()],m=r.metric.compute([...n.keys()].map(e=>u.get(e)),d);if(!Number.isFinite(m))return{decisionCode:a.WRONG_ANSWER,feedbackMarkdown:`予測値が極端なため、${r.metric.label}を計算できませんでした。予測値を見直してください。`};let h=r.acceptableScore===void 0||(r.metric.isHigherBetter?m>=r.acceptableScore:m<=r.acceptableScore);return{decisionCode:h?a.ACCEPTED:a.WRONG_ANSWER,score:m,scoreLabel:r.metric.label,feedbackMarkdown:v(m,d.length,h,r)}}function v(e,t,n,r){let{label:i,isHigherBetter:a}=r.metric,o=y(e,r.acceptableScore),s=r.acceptableScore===void 0?``:`\n合格基準: ${i} ${a?`≧`:`≦`} ${b(r.acceptableScore,o)}(${n?`達成`:`未達成`})`;return`| 指標 | スコア |
2
2
  | ---- | ------ |
3
- | ${i} | ${x(e,o)} |
3
+ | ${i} | ${b(e,o)} |
4
4
 
5
5
  評価件数: ${t.toLocaleString(`en-US`)}件${s}
6
- `}function b(e,t){if(t===void 0||e===t)return 6;for(let n=6;n<17;n++)if(x(e,n)!==x(t,n))return n;return 17}function x(e,t){return Number(e.toPrecision(t)).toString()}function S(e,t,n,i){let o;try{o=a(e)}catch(e){return`\`${t}\`をCSVとして読み込めませんでした。${e instanceof r?`${e.lineNumber}行目付近: ${h[e.reason]}`:``}`}let s=w(o[0]?.cells??[],i);if(!s)return`\`${t}\`の1行目には\`${i.idColumn}\`列と\`${i.targetColumn}\`列のヘッダーが必要です。`;let c=new Map,l=[],u=0,d=e=>{u++,l.length<10&&l.push(e)};for(let{cells:e,lineNumber:t}of o.slice(1)){if(T(e))continue;let r=e[s.idIndex]?.trim()??``,a=e[s.targetIndex]??``;if(!n.has(r))d(`${t}行目: \`${i.idColumn}\`が\`${D(r)}\`の行は評価対象ではありません`);else if(c.has(r))d(`${t}行目: \`${i.idColumn}\`が\`${D(r)}\`の行が重複しています`);else{let e=i.metric.validatePrediction?.(a);e&&d(`${t}行目: \`${i.targetColumn}\`の値\`${D(a)}\`は${e}`),c.set(r,a)}}let f=[...n.keys()].filter(e=>!c.has(e));return f.length>0&&(u++,l.unshift(`\`${i.idColumn}\`が${E(f)}の行がありません(不足 ${f.length.toLocaleString(`en-US`)}件)`)),u>0?`\`${t}\`の内容に問題があります。
6
+ `}function y(e,t){if(t===void 0||e===t)return 6;for(let n=6;n<17;n++)if(b(e,n)!==b(t,n))return n;return 17}function b(e,t){return Number(e.toPrecision(t)).toString()}function x(e,t,r,a){let o;try{o=i(e)}catch(e){return`\`${t}\`をCSVとして読み込めませんでした。${e instanceof n?`${e.lineNumber}行目付近: ${m[e.reason]}`:``}`}let s=C(o[0]?.cells??[],a);if(!s)return`\`${t}\`の1行目には\`${a.idColumn}\`列と\`${a.targetColumn}\`列のヘッダーが必要です。`;let c=new Map,l=[],u=0,d=e=>{u++,l.length<10&&l.push(e)};for(let{cells:e,lineNumber:t}of o.slice(1)){if(w(e))continue;let n=e[s.idIndex]?.trim()??``,i=e[s.targetIndex]??``;if(!r.has(n))d(`${t}行目: \`${a.idColumn}\`が\`${E(n)}\`の行は評価対象ではありません`);else if(c.has(n))d(`${t}行目: \`${a.idColumn}\`が\`${E(n)}\`の行が重複しています`);else{let e=a.metric.validatePrediction?.(i);e&&d(`${t}行目: \`${a.targetColumn}\`の値\`${E(i)}\`は${e}`),c.set(n,i)}}let f=[...r.keys()].filter(e=>!c.has(e));return f.length>0&&(u++,l.unshift(`\`${a.idColumn}\`が${T(f)}の行がありません(不足 ${f.length.toLocaleString(`en-US`)}件)`)),u>0?`\`${t}\`の内容に問題があります。
7
7
 
8
8
  ${l.map(e=>`- ${e}`).join(`
9
9
  `)}${u>l.length?`\n- ほか${(u-l.length).toLocaleString(`en-US`)}件`:``}
10
- `:c}async function C(e,t){let n=i(await m.readFile(e,`utf8`)),r=w(n[0]??[],t);if(!r)throw Error(`answer file must have ${t.idColumn} and ${t.targetColumn} columns: ${e}`);let a=new Map;for(let i of n.slice(1)){if(T(i))continue;let n=i[r.idIndex]?.trim()??``,o=i[r.targetIndex]??``;if(!n)throw Error(`empty ${t.idColumn} in answer file: ${e}`);if(a.has(n))throw Error(`duplicate ${t.idColumn} in answer file: ${n}`);let s=t.metric.validatePrediction?.(o);if(s)throw Error(`invalid ${t.targetColumn} in answer file for ${n}: ${s}`);a.set(n,o)}if(a.size===0)throw Error(`answer file has no rows: ${e}`);return a}function w(e,t){let n=e.map(e=>e.trim()),r=n.indexOf(t.idColumn),i=n.indexOf(t.targetColumn);return r===-1||i===-1?void 0:{idIndex:r,targetIndex:i}}function T(e){return e.every(e=>e.trim()===``)}function E(e){let t=e.slice(0,10).map(e=>`\`${D(e)}\``);return e.length>10?`${t.join(`, `)}, ...`:t.join(`, `)}function D(e){let t=e.replaceAll(/[\p{Cc}]/gu,` `).replaceAll("`",`'`);return t.trim()===``?`(空)`:t.length>50?`${t.slice(0,50)}…`:t}function O(e,t){return t.decisionCode===o.ACCEPTED==(e.expectedResult===`accepted`)}function k(e){return Number.isFinite(Number(e))&&e.trim()!==``?void 0:`数値ではありません`}function A(e){return e.reduce((e,t)=>e+t,0)/e.length}export{_ as evaluationJudgePreset,g as evaluationMetrics};
10
+ `:c}async function S(e,t){let n=r(await p.readFile(e,`utf8`)),i=C(n[0]??[],t);if(!i)throw Error(`answer file must have ${t.idColumn} and ${t.targetColumn} columns: ${e}`);let a=new Map;for(let r of n.slice(1)){if(w(r))continue;let n=r[i.idIndex]?.trim()??``,o=r[i.targetIndex]??``;if(!n)throw Error(`empty ${t.idColumn} in answer file: ${e}`);if(a.has(n))throw Error(`duplicate ${t.idColumn} in answer file: ${n}`);let s=t.metric.validatePrediction?.(o);if(s)throw Error(`invalid ${t.targetColumn} in answer file for ${n}: ${s}`);a.set(n,o)}if(a.size===0)throw Error(`answer file has no rows: ${e}`);return a}function C(e,t){let n=e.map(e=>e.trim()),r=n.indexOf(t.idColumn),i=n.indexOf(t.targetColumn);return r===-1||i===-1?void 0:{idIndex:r,targetIndex:i}}function w(e){return e.every(e=>e.trim()===``)}function T(e){let t=e.slice(0,10).map(e=>`\`${E(e)}\``);return e.length>10?`${t.join(`, `)}, ...`:t.join(`, `)}function E(e){let t=e.replaceAll(/[\p{Cc}]/gu,` `).replaceAll("`",`'`);return t.trim()===``?`(空)`:t.length>50?`${t.slice(0,50)}…`:t}function D(e,t){return t.decisionCode===a.ACCEPTED==(e.expectedResult===`accepted`)}function O(e){return Number.isFinite(Number(e))&&e.trim()!==``?void 0:`数値ではありません`}function k(e){return e.reduce((e,t)=>e+t,0)/e.length}export{g as evaluationJudgePreset,h as evaluationMetrics};
11
11
  //# sourceMappingURL=evaluation.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"evaluation.js","names":[],"sources":["../../src/presets/evaluation.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport { judgeByStaticAnalysis } from '../helpers/judgeByStaticAnalysis.js';\nimport { parseArgs } from '../helpers/parseArgs.js';\nimport { CsvParseError, type CsvParseErrorReason, parseCsv, parseCsvRecords } from '../helpers/parseCsv.js';\nimport { printTestCaseResult } from '../helpers/printTestCaseResult.js';\nimport { isSafeSubmissionOutputPath } from '../helpers/readOutputFiles.js';\nimport { readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { passesIsolationCheckInDebugMode } from '../helpers/runIsolationCheckInDebugMode.js';\nimport {\n printDebugCwdBanner,\n printDebugExpectationFailureBanner,\n resolveCwds,\n type ResolvedCwd,\n} from '../helpers/resolveCwds.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport type { TestCaseResult } from '../types/testCaseResult.js';\n\nconst DEFAULT_SUBMISSION_FILE_PATH = 'submission.csv';\nconst TEST_CASE_ID = 'evaluation';\nconst MAX_LISTED_IDS = 10;\n// Learner-controlled cells are echoed into the feedback, so clip them to keep the result line small.\nconst MAX_ECHOED_CELL_LENGTH = 50;\nconst CSV_PARSE_ERROR_MESSAGES: Record<CsvParseErrorReason, string> = {\n unterminated_quote: '引用符(`\"`)が閉じられていません',\n unexpected_quote: '引用符(`\"`)は値全体を囲む位置にしか書けません',\n text_after_quoted_field: '閉じ引用符の後に区切り文字以外の文字があります',\n};\n\nexport interface EvaluationMetric {\n /** Label shown with the score, e.g. `RMSLE`. */\n label: string;\n isHigherBetter: boolean;\n /**\n * Returns an error message when a predicted value cannot be scored (e.g. it is not a number).\n * The message is appended to the row's line number and the offending value, so phrase it as a\n * predicate such as `数値ではありません`. It also validates the ground truth when it is loaded.\n */\n validatePrediction?: (prediction: string) => string | undefined;\n /** Computes the score from predictions and answers aligned by id. Both are raw CSV cell strings. */\n compute: (predictions: readonly string[], answers: readonly string[]) => number;\n}\n\nexport interface EvaluationJudgePresetOptions {\n /** CSV file holding the hidden ground truth, relative to the problem directory. */\n answerFilePath: string;\n /** CSV file the submission must contain. Defaults to `submission.csv`. */\n submissionFilePath?: string;\n /** Column that identifies each row in both CSV files. */\n idColumn: string;\n /** Column holding the answer in the ground truth and the prediction in the submission. */\n targetColumn: string;\n metric: EvaluationMetric;\n /**\n * Score required for acceptance: the submission is accepted when its score is at least this value\n * for a higher-is-better metric and at most this value otherwise. Every valid submission is\n * accepted when omitted.\n */\n acceptableScore?: number;\n}\n\nexport const evaluationMetrics = {\n rmsle: {\n label: 'RMSLE',\n isHigherBetter: false,\n validatePrediction: (prediction: string) =>\n validateFiniteNumber(prediction) ?? (Number(prediction) < 0 ? '負の値です' : undefined),\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n Math.sqrt(\n mean(\n predictions.map(\n (prediction, index) => (Math.log1p(Number(prediction)) - Math.log1p(Number(answers[index]))) ** 2\n )\n )\n ),\n },\n rmse: {\n label: 'RMSE',\n isHigherBetter: false,\n validatePrediction: validateFiniteNumber,\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n Math.sqrt(mean(predictions.map((prediction, index) => (Number(prediction) - Number(answers[index])) ** 2))),\n },\n mae: {\n label: 'MAE',\n isHigherBetter: false,\n validatePrediction: validateFiniteNumber,\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n mean(predictions.map((prediction, index) => Math.abs(Number(prediction) - Number(answers[index])))),\n },\n accuracy: {\n label: 'Accuracy',\n isHigherBetter: true,\n validatePrediction: (prediction: string) => (prediction.trim() === '' ? '空です' : undefined),\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n mean(predictions.map((prediction, index) => (prediction.trim() === answers[index]?.trim() ? 1 : 0))),\n },\n} as const satisfies Record<string, EvaluationMetric>;\n\n/**\n * A preset for Kaggle-style model evaluation problems: the submission is a CSV of predictions for a\n * hidden test set, and the judge reports the metric value as `score` instead of comparing outputs.\n * The problem directory keeps the ground truth CSV, so it is never shipped to learners.\n *\n * @example\n * Create `judge.ts`:\n * ```ts\n * import { evaluationJudgePreset, evaluationMetrics } from '@exercode/problem-utils/presets/evaluation';\n *\n * await evaluationJudgePreset(import.meta.dirname, {\n * answerFilePath: 'evaluation/answer.csv',\n * idColumn: 'Id',\n * targetColumn: 'TradePrice',\n * metric: evaluationMetrics.rmsle,\n * acceptableScore: 0.5,\n * });\n * ```\n *\n * Run `bun judge.ts model_answers/default` to judge one submission, or `bun judge.ts` to judge every\n * `<problemDir>/model_answers/*` and `<problemDir>/model_answers.fails/*` directory for debugging.\n */\nexport async function evaluationJudgePreset(problemDir: string, options: EvaluationJudgePresetOptions): Promise<void> {\n const args = parseArgs(process.argv);\n const { cwds, isDebugMode } = await resolveCwds(problemDir, args.cwd);\n\n if (isDebugMode && !(await passesIsolationCheckInDebugMode(problemDir, cwds, args.params))) {\n process.exitCode = 1;\n return;\n }\n\n const answer = await readAnswer(path.join(problemDir, options.answerFilePath), options);\n for (const resolvedCwd of cwds) {\n if (isDebugMode) printDebugCwdBanner(problemDir, resolvedCwd);\n const result = await judgeSubmission(problemDir, resolvedCwd.cwd, answer, options);\n printTestCaseResult({ testCaseId: TEST_CASE_ID, ...result });\n if (isDebugMode && !matchesExpectedResult(resolvedCwd, result)) {\n process.exitCode = 1;\n printDebugExpectationFailureBanner(problemDir, resolvedCwd);\n }\n }\n}\n\nasync function judgeSubmission(\n problemDir: string,\n cwd: string,\n answer: ReadonlyMap<string, string>,\n options: EvaluationJudgePresetOptions\n): Promise<Omit<TestCaseResult, 'testCaseId'>> {\n const staticAnalysisResult = await judgeByStaticAnalysis(cwd, await readProblemMarkdownFrontMatter(problemDir));\n if (staticAnalysisResult) return staticAnalysisResult;\n\n const submissionFilePath = options.submissionFilePath ?? DEFAULT_SUBMISSION_FILE_PATH;\n let submissionText: string;\n try {\n const resolvedPath = path.join(cwd, submissionFilePath);\n // The harness reads the file as the trusted user, so refuse symlinks escaping the submission.\n if (!(await isSafeSubmissionOutputPath(cwd, resolvedPath))) throw new Error('unsafe path');\n submissionText = await fs.readFile(resolvedPath, 'utf8');\n } catch {\n return {\n decisionCode: DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,\n feedbackMarkdown: `\\`${submissionFilePath}\\`が見つかりません。予測結果のCSVファイルを\\`${submissionFilePath}\\`という名前で提出してください。`,\n };\n }\n\n const predictions = readPredictions(submissionText, submissionFilePath, answer, options);\n if (typeof predictions === 'string') {\n return { decisionCode: DecisionCode.WRONG_ANSWER, feedbackMarkdown: predictions };\n }\n\n const answerValues = [...answer.values()];\n const score = options.metric.compute(\n [...answer.keys()].map((id) => predictions.get(id) as string),\n answerValues\n );\n if (!Number.isFinite(score)) {\n return {\n decisionCode: DecisionCode.WRONG_ANSWER,\n feedbackMarkdown: `予測値が極端なため、${options.metric.label}を計算できませんでした。予測値を見直してください。`,\n };\n }\n const isAccepted =\n options.acceptableScore === undefined ||\n (options.metric.isHigherBetter ? score >= options.acceptableScore : score <= options.acceptableScore);\n return {\n decisionCode: isAccepted ? DecisionCode.ACCEPTED : DecisionCode.WRONG_ANSWER,\n score,\n scoreLabel: options.metric.label,\n feedbackMarkdown: buildScoreMarkdown(score, answerValues.length, isAccepted, options),\n };\n}\n\nfunction buildScoreMarkdown(\n score: number,\n rowCount: number,\n isAccepted: boolean,\n options: EvaluationJudgePresetOptions\n): string {\n const { label, isHigherBetter } = options.metric;\n const precision = findDistinguishingPrecision(score, options.acceptableScore);\n const acceptanceLine =\n options.acceptableScore === undefined\n ? ''\n : `\\n合格基準: ${label} ${isHigherBetter ? '≧' : '≦'} ${formatScore(options.acceptableScore, precision)}(${isAccepted ? '達成' : '未達成'})`;\n return `| 指標 | スコア |\n| ---- | ------ |\n| ${label} | ${formatScore(score, precision)} |\n\n評価件数: ${rowCount.toLocaleString('en-US')}件${acceptanceLine}\n`;\n}\n\n/**\n * Finds the number of significant digits at which the score and the threshold render differently\n * (or stop changing), so the feedback never shows a pair that contradicts the decision.\n */\nfunction findDistinguishingPrecision(score: number, threshold: number | undefined): number {\n const minimumPrecision = 6;\n if (threshold === undefined || score === threshold) return minimumPrecision;\n for (let precision = minimumPrecision; precision < 17; precision++) {\n if (formatScore(score, precision) !== formatScore(threshold, precision)) return precision;\n }\n return 17;\n}\n\nfunction formatScore(score: number, precision: number): string {\n return Number(score.toPrecision(precision)).toString();\n}\n\nfunction readPredictions(\n submissionText: string,\n submissionFilePath: string,\n answer: ReadonlyMap<string, string>,\n options: EvaluationJudgePresetOptions\n): Map<string, string> | string {\n let records: ReturnType<typeof parseCsvRecords>;\n try {\n records = parseCsvRecords(submissionText);\n } catch (error) {\n const detail =\n error instanceof CsvParseError ? `${error.lineNumber}行目付近: ${CSV_PARSE_ERROR_MESSAGES[error.reason]}` : '';\n return `\\`${submissionFilePath}\\`をCSVとして読み込めませんでした。${detail}`;\n }\n const columns = findColumns(records[0]?.cells ?? [], options);\n if (!columns) {\n return `\\`${submissionFilePath}\\`の1行目には\\`${options.idColumn}\\`列と\\`${options.targetColumn}\\`列のヘッダーが必要です。`;\n }\n\n const predictions = new Map<string, string>();\n const problems: string[] = [];\n let problemCount = 0;\n const reportProblem = (problem: string): void => {\n problemCount++;\n if (problems.length < MAX_LISTED_IDS) problems.push(problem);\n };\n for (const { cells: row, lineNumber } of records.slice(1)) {\n if (isBlankRow(row)) continue;\n const id = row[columns.idIndex]?.trim() ?? '';\n const prediction = row[columns.targetIndex] ?? '';\n if (!answer.has(id)) {\n reportProblem(`${lineNumber}行目: \\`${options.idColumn}\\`が\\`${clip(id)}\\`の行は評価対象ではありません`);\n } else if (predictions.has(id)) {\n reportProblem(`${lineNumber}行目: \\`${options.idColumn}\\`が\\`${clip(id)}\\`の行が重複しています`);\n } else {\n const validationError = options.metric.validatePrediction?.(prediction);\n if (validationError) {\n reportProblem(`${lineNumber}行目: \\`${options.targetColumn}\\`の値\\`${clip(prediction)}\\`は${validationError}`);\n }\n predictions.set(id, prediction);\n }\n }\n const missingIds = [...answer.keys()].filter((id) => !predictions.has(id));\n if (missingIds.length > 0) {\n // Missing rows are the most actionable problem, so they must survive the listing cap.\n problemCount++;\n problems.unshift(\n `\\`${options.idColumn}\\`が${listIds(missingIds)}の行がありません(不足 ${missingIds.length.toLocaleString('en-US')}件)`\n );\n }\n if (problemCount > 0) {\n return `\\`${submissionFilePath}\\`の内容に問題があります。\n\n${problems.map((problem) => `- ${problem}`).join('\\n')}${problemCount > problems.length ? `\\n- ほか${(problemCount - problems.length).toLocaleString('en-US')}件` : ''}\n`;\n }\n return predictions;\n}\n\nasync function readAnswer(answerFilePath: string, options: EvaluationJudgePresetOptions): Promise<Map<string, string>> {\n const rows = parseCsv(await fs.readFile(answerFilePath, 'utf8'));\n const columns = findColumns(rows[0] ?? [], options);\n if (!columns) {\n throw new Error(`answer file must have ${options.idColumn} and ${options.targetColumn} columns: ${answerFilePath}`);\n }\n const answer = new Map<string, string>();\n for (const row of rows.slice(1)) {\n if (isBlankRow(row)) continue;\n const id = row[columns.idIndex]?.trim() ?? '';\n const value = row[columns.targetIndex] ?? '';\n if (!id) throw new Error(`empty ${options.idColumn} in answer file: ${answerFilePath}`);\n if (answer.has(id)) throw new Error(`duplicate ${options.idColumn} in answer file: ${id}`);\n const validationError = options.metric.validatePrediction?.(value);\n if (validationError)\n throw new Error(`invalid ${options.targetColumn} in answer file for ${id}: ${validationError}`);\n answer.set(id, value);\n }\n if (answer.size === 0) throw new Error(`answer file has no rows: ${answerFilePath}`);\n return answer;\n}\n\nfunction findColumns(\n header: readonly string[],\n options: EvaluationJudgePresetOptions\n): { idIndex: number; targetIndex: number } | undefined {\n const names = header.map((name) => name.trim());\n const idIndex = names.indexOf(options.idColumn);\n const targetIndex = names.indexOf(options.targetColumn);\n return idIndex === -1 || targetIndex === -1 ? undefined : { idIndex, targetIndex };\n}\n\nfunction isBlankRow(row: readonly string[]): boolean {\n return row.every((cell) => cell.trim() === '');\n}\n\nfunction listIds(ids: readonly string[]): string {\n const listed = ids.slice(0, MAX_LISTED_IDS).map((id) => `\\`${clip(id)}\\``);\n return ids.length > MAX_LISTED_IDS ? `${listed.join(', ')}, ...` : listed.join(', ');\n}\n\nfunction clip(value: string): string {\n // Control characters and backticks would break the markdown list item and inline code the value is echoed in.\n const printable = value.replaceAll(/[\\p{Cc}]/gu, ' ').replaceAll('`', \"'\");\n if (printable.trim() === '') return '(空)';\n return printable.length > MAX_ECHOED_CELL_LENGTH ? `${printable.slice(0, MAX_ECHOED_CELL_LENGTH)}…` : printable;\n}\n\nfunction matchesExpectedResult(resolvedCwd: ResolvedCwd, result: Pick<TestCaseResult, 'decisionCode'>): boolean {\n return (result.decisionCode === DecisionCode.ACCEPTED) === (resolvedCwd.expectedResult === 'accepted');\n}\n\nfunction validateFiniteNumber(prediction: string): string | undefined {\n return Number.isFinite(Number(prediction)) && prediction.trim() !== '' ? undefined : '数値ではありません';\n}\n\nfunction mean(values: readonly number[]): number {\n return values.reduce((sum, value) => sum + value, 0) / values.length;\n}\n"],"mappings":"kxBAmBA,MAKM,EAAgE,CACpE,mBAAoB,qBACpB,iBAAkB,4BAClB,wBAAyB,yBAC3B,EAkCa,EAAoB,CAC/B,MAAO,CACL,MAAO,QACP,eAAgB,GAChB,mBAAqB,GACnB,EAAqB,CAAU,IAAM,OAAO,CAAU,EAAI,EAAI,QAAU,IAAA,IAC1E,SAAU,EAAgC,IACxC,KAAK,KACH,EACE,EAAY,KACT,EAAY,KAAW,KAAK,MAAM,OAAO,CAAU,CAAC,EAAI,KAAK,MAAM,OAAO,EAAQ,EAAM,CAAC,IAAM,CAClG,CACF,CACF,CACJ,EACA,KAAM,CACJ,MAAO,OACP,eAAgB,GAChB,mBAAoB,EACpB,SAAU,EAAgC,IACxC,KAAK,KAAK,EAAK,EAAY,KAAK,EAAY,KAAW,OAAO,CAAU,EAAI,OAAO,EAAQ,EAAM,IAAM,CAAC,CAAC,CAAC,CAC9G,EACA,IAAK,CACH,MAAO,MACP,eAAgB,GAChB,mBAAoB,EACpB,SAAU,EAAgC,IACxC,EAAK,EAAY,KAAK,EAAY,IAAU,KAAK,IAAI,OAAO,CAAU,EAAI,OAAO,EAAQ,EAAM,CAAC,CAAC,CAAC,CACtG,EACA,SAAU,CACR,MAAO,WACP,eAAgB,GAChB,mBAAqB,GAAwB,EAAW,KAAK,IAAM,GAAK,MAAQ,IAAA,GAChF,SAAU,EAAgC,IACxC,EAAK,EAAY,KAAK,EAAY,IAAW,IAAW,KAAK,IAAM,EAAQ,EAAM,EAAE,KAAK,EAAU,CAAC,CACvG,CACF,EAwBA,eAAsB,EAAsB,EAAoB,EAAsD,CACpH,IAAM,EAAO,EAAU,QAAQ,IAAI,EAC7B,CAAE,OAAM,eAAgB,MAAM,EAAY,EAAY,EAAK,GAA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CAAC,CAAC,IAAK,GAAO,KAAK,EAAK,CAAE,EAAE,GAAG,EACzE,OAAO,EAAI,OAAS,GAAiB,GAAG,EAAO,KAAK,IAAI,EAAE,OAAS,EAAO,KAAK,IAAI,CACrF,CAEA,SAAS,EAAK,EAAuB,CAEnC,IAAM,EAAY,EAAM,WAAW,aAAc,GAAG,CAAC,CAAC,WAAW,IAAK,GAAG,EAEzE,OADI,EAAU,KAAK,IAAM,GAAW,MAC7B,EAAU,OAAS,GAAyB,GAAG,EAAU,MAAM,EAAG,EAAsB,EAAE,GAAK,CACxG,CAEA,SAAS,EAAsB,EAA0B,EAAuD,CAC9G,OAAQ,EAAO,eAAiB,EAAa,WAAe,EAAY,iBAAmB,WAC7F,CAEA,SAAS,EAAqB,EAAwC,CACpE,OAAO,OAAO,SAAS,OAAO,CAAU,CAAC,GAAK,EAAW,KAAK,IAAM,GAAK,IAAA,GAAY,WACvF,CAEA,SAAS,EAAK,EAAmC,CAC/C,OAAO,EAAO,QAAQ,EAAK,IAAU,EAAM,EAAO,CAAC,EAAI,EAAO,MAChE"}
1
+ {"version":3,"file":"evaluation.js","names":[],"sources":["../../src/presets/evaluation.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport { judgeByStaticAnalysis } from '../helpers/judgeByStaticAnalysis.js';\nimport { parseArgs } from '../helpers/parseArgs.js';\nimport { CsvParseError, type CsvParseErrorReason, parseCsv, parseCsvRecords } from '../helpers/parseCsv.js';\nimport { printTestCaseResult } from '../helpers/printTestCaseResult.js';\nimport { readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { passesIsolationCheckInDebugMode } from '../helpers/runIsolationCheckInDebugMode.js';\nimport {\n printDebugCwdBanner,\n printDebugExpectationFailureBanner,\n resolveCwds,\n type ResolvedCwd,\n} from '../helpers/resolveCwds.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport type { TestCaseResult } from '../types/testCaseResult.js';\n\nconst DEFAULT_SUBMISSION_FILE_PATH = 'submission.csv';\nconst TEST_CASE_ID = 'evaluation';\nconst MAX_LISTED_IDS = 10;\n// Learner-controlled cells are echoed into the feedback, so clip them to keep the result line small.\nconst MAX_ECHOED_CELL_LENGTH = 50;\nconst CSV_PARSE_ERROR_MESSAGES: Record<CsvParseErrorReason, string> = {\n unterminated_quote: '引用符(`\"`)が閉じられていません',\n unexpected_quote: '引用符(`\"`)は値全体を囲む位置にしか書けません',\n text_after_quoted_field: '閉じ引用符の後に区切り文字以外の文字があります',\n};\n\nexport interface EvaluationMetric {\n /** Label shown with the score, e.g. `RMSLE`. */\n label: string;\n isHigherBetter: boolean;\n /**\n * Returns an error message when a predicted value cannot be scored (e.g. it is not a number).\n * The message is appended to the row's line number and the offending value, so phrase it as a\n * predicate such as `数値ではありません`. It also validates the ground truth when it is loaded.\n */\n validatePrediction?: (prediction: string) => string | undefined;\n /** Computes the score from predictions and answers aligned by id. Both are raw CSV cell strings. */\n compute: (predictions: readonly string[], answers: readonly string[]) => number;\n}\n\nexport interface EvaluationJudgePresetOptions {\n /** CSV file holding the hidden ground truth, relative to the problem directory. */\n answerFilePath: string;\n /** CSV file the submission must contain. Defaults to `submission.csv`. */\n submissionFilePath?: string;\n /** Column that identifies each row in both CSV files. */\n idColumn: string;\n /** Column holding the answer in the ground truth and the prediction in the submission. */\n targetColumn: string;\n metric: EvaluationMetric;\n /**\n * Score required for acceptance: the submission is accepted when its score is at least this value\n * for a higher-is-better metric and at most this value otherwise. Every valid submission is\n * accepted when omitted.\n */\n acceptableScore?: number;\n}\n\nexport const evaluationMetrics = {\n rmsle: {\n label: 'RMSLE',\n isHigherBetter: false,\n validatePrediction: (prediction: string) =>\n validateFiniteNumber(prediction) ?? (Number(prediction) < 0 ? '負の値です' : undefined),\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n Math.sqrt(\n mean(\n predictions.map(\n (prediction, index) => (Math.log1p(Number(prediction)) - Math.log1p(Number(answers[index]))) ** 2\n )\n )\n ),\n },\n rmse: {\n label: 'RMSE',\n isHigherBetter: false,\n validatePrediction: validateFiniteNumber,\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n Math.sqrt(mean(predictions.map((prediction, index) => (Number(prediction) - Number(answers[index])) ** 2))),\n },\n mae: {\n label: 'MAE',\n isHigherBetter: false,\n validatePrediction: validateFiniteNumber,\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n mean(predictions.map((prediction, index) => Math.abs(Number(prediction) - Number(answers[index])))),\n },\n accuracy: {\n label: 'Accuracy',\n isHigherBetter: true,\n validatePrediction: (prediction: string) => (prediction.trim() === '' ? '空です' : undefined),\n compute: (predictions: readonly string[], answers: readonly string[]) =>\n mean(predictions.map((prediction, index) => (prediction.trim() === answers[index]?.trim() ? 1 : 0))),\n },\n} as const satisfies Record<string, EvaluationMetric>;\n\n/**\n * A preset for Kaggle-style model evaluation problems: the submission is a CSV of predictions for a\n * hidden test set, and the judge reports the metric value as `score` instead of comparing outputs.\n * The problem directory keeps the ground truth CSV, so it is never shipped to learners.\n *\n * @example\n * Create `judge.ts`:\n * ```ts\n * import { evaluationJudgePreset, evaluationMetrics } from '@exercode/problem-utils/presets/evaluation';\n *\n * await evaluationJudgePreset(import.meta.dirname, {\n * answerFilePath: 'evaluation/answer.csv',\n * idColumn: 'Id',\n * targetColumn: 'TradePrice',\n * metric: evaluationMetrics.rmsle,\n * acceptableScore: 0.5,\n * });\n * ```\n *\n * Run `bun judge.ts model_answers/default` to judge one submission, or `bun judge.ts` to judge every\n * `<problemDir>/model_answers/*` and `<problemDir>/model_answers.fails/*` directory for debugging.\n */\nexport async function evaluationJudgePreset(problemDir: string, options: EvaluationJudgePresetOptions): Promise<void> {\n const args = parseArgs(process.argv);\n const { cwds, isDebugMode } = await resolveCwds(problemDir, args.cwd);\n\n if (isDebugMode && !(await passesIsolationCheckInDebugMode(problemDir, cwds, args.params))) {\n process.exitCode = 1;\n return;\n }\n\n const answer = await readAnswer(path.join(problemDir, options.answerFilePath), options);\n for (const resolvedCwd of cwds) {\n if (isDebugMode) printDebugCwdBanner(problemDir, resolvedCwd);\n const result = await judgeSubmission(problemDir, resolvedCwd.cwd, answer, options);\n printTestCaseResult({ testCaseId: TEST_CASE_ID, ...result });\n if (isDebugMode && !matchesExpectedResult(resolvedCwd, result)) {\n process.exitCode = 1;\n printDebugExpectationFailureBanner(problemDir, resolvedCwd);\n }\n }\n}\n\nasync function judgeSubmission(\n problemDir: string,\n cwd: string,\n answer: ReadonlyMap<string, string>,\n options: EvaluationJudgePresetOptions\n): Promise<Omit<TestCaseResult, 'testCaseId'>> {\n const staticAnalysisResult = await judgeByStaticAnalysis(cwd, await readProblemMarkdownFrontMatter(problemDir));\n if (staticAnalysisResult) return staticAnalysisResult;\n\n const submissionFilePath = options.submissionFilePath ?? DEFAULT_SUBMISSION_FILE_PATH;\n let submissionText: string;\n try {\n const resolvedPath = path.join(cwd, submissionFilePath);\n submissionText = await fs.readFile(resolvedPath, 'utf8');\n } catch {\n return {\n decisionCode: DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,\n feedbackMarkdown: `\\`${submissionFilePath}\\`が見つかりません。予測結果のCSVファイルを\\`${submissionFilePath}\\`という名前で提出してください。`,\n };\n }\n\n const predictions = readPredictions(submissionText, submissionFilePath, answer, options);\n if (typeof predictions === 'string') {\n return { decisionCode: DecisionCode.WRONG_ANSWER, feedbackMarkdown: predictions };\n }\n\n const answerValues = [...answer.values()];\n const score = options.metric.compute(\n [...answer.keys()].map((id) => predictions.get(id) as string),\n answerValues\n );\n if (!Number.isFinite(score)) {\n return {\n decisionCode: DecisionCode.WRONG_ANSWER,\n feedbackMarkdown: `予測値が極端なため、${options.metric.label}を計算できませんでした。予測値を見直してください。`,\n };\n }\n const isAccepted =\n options.acceptableScore === undefined ||\n (options.metric.isHigherBetter ? score >= options.acceptableScore : score <= options.acceptableScore);\n return {\n decisionCode: isAccepted ? DecisionCode.ACCEPTED : DecisionCode.WRONG_ANSWER,\n score,\n scoreLabel: options.metric.label,\n feedbackMarkdown: buildScoreMarkdown(score, answerValues.length, isAccepted, options),\n };\n}\n\nfunction buildScoreMarkdown(\n score: number,\n rowCount: number,\n isAccepted: boolean,\n options: EvaluationJudgePresetOptions\n): string {\n const { label, isHigherBetter } = options.metric;\n const precision = findDistinguishingPrecision(score, options.acceptableScore);\n const acceptanceLine =\n options.acceptableScore === undefined\n ? ''\n : `\\n合格基準: ${label} ${isHigherBetter ? '≧' : '≦'} ${formatScore(options.acceptableScore, precision)}(${isAccepted ? '達成' : '未達成'})`;\n return `| 指標 | スコア |\n| ---- | ------ |\n| ${label} | ${formatScore(score, precision)} |\n\n評価件数: ${rowCount.toLocaleString('en-US')}件${acceptanceLine}\n`;\n}\n\n/**\n * Finds the number of significant digits at which the score and the threshold render differently\n * (or stop changing), so the feedback never shows a pair that contradicts the decision.\n */\nfunction findDistinguishingPrecision(score: number, threshold: number | undefined): number {\n const minimumPrecision = 6;\n if (threshold === undefined || score === threshold) return minimumPrecision;\n for (let precision = minimumPrecision; precision < 17; precision++) {\n if (formatScore(score, precision) !== formatScore(threshold, precision)) return precision;\n }\n return 17;\n}\n\nfunction formatScore(score: number, precision: number): string {\n return Number(score.toPrecision(precision)).toString();\n}\n\nfunction readPredictions(\n submissionText: string,\n submissionFilePath: string,\n answer: ReadonlyMap<string, string>,\n options: EvaluationJudgePresetOptions\n): Map<string, string> | string {\n let records: ReturnType<typeof parseCsvRecords>;\n try {\n records = parseCsvRecords(submissionText);\n } catch (error) {\n const detail =\n error instanceof CsvParseError ? `${error.lineNumber}行目付近: ${CSV_PARSE_ERROR_MESSAGES[error.reason]}` : '';\n return `\\`${submissionFilePath}\\`をCSVとして読み込めませんでした。${detail}`;\n }\n const columns = findColumns(records[0]?.cells ?? [], options);\n if (!columns) {\n return `\\`${submissionFilePath}\\`の1行目には\\`${options.idColumn}\\`列と\\`${options.targetColumn}\\`列のヘッダーが必要です。`;\n }\n\n const predictions = new Map<string, string>();\n const problems: string[] = [];\n let problemCount = 0;\n const reportProblem = (problem: string): void => {\n problemCount++;\n if (problems.length < MAX_LISTED_IDS) problems.push(problem);\n };\n for (const { cells: row, lineNumber } of records.slice(1)) {\n if (isBlankRow(row)) continue;\n const id = row[columns.idIndex]?.trim() ?? '';\n const prediction = row[columns.targetIndex] ?? '';\n if (!answer.has(id)) {\n reportProblem(`${lineNumber}行目: \\`${options.idColumn}\\`が\\`${clip(id)}\\`の行は評価対象ではありません`);\n } else if (predictions.has(id)) {\n reportProblem(`${lineNumber}行目: \\`${options.idColumn}\\`が\\`${clip(id)}\\`の行が重複しています`);\n } else {\n const validationError = options.metric.validatePrediction?.(prediction);\n if (validationError) {\n reportProblem(`${lineNumber}行目: \\`${options.targetColumn}\\`の値\\`${clip(prediction)}\\`は${validationError}`);\n }\n predictions.set(id, prediction);\n }\n }\n const missingIds = [...answer.keys()].filter((id) => !predictions.has(id));\n if (missingIds.length > 0) {\n // Missing rows are the most actionable problem, so they must survive the listing cap.\n problemCount++;\n problems.unshift(\n `\\`${options.idColumn}\\`が${listIds(missingIds)}の行がありません(不足 ${missingIds.length.toLocaleString('en-US')}件)`\n );\n }\n if (problemCount > 0) {\n return `\\`${submissionFilePath}\\`の内容に問題があります。\n\n${problems.map((problem) => `- ${problem}`).join('\\n')}${problemCount > problems.length ? `\\n- ほか${(problemCount - problems.length).toLocaleString('en-US')}件` : ''}\n`;\n }\n return predictions;\n}\n\nasync function readAnswer(answerFilePath: string, options: EvaluationJudgePresetOptions): Promise<Map<string, string>> {\n const rows = parseCsv(await fs.readFile(answerFilePath, 'utf8'));\n const columns = findColumns(rows[0] ?? [], options);\n if (!columns) {\n throw new Error(`answer file must have ${options.idColumn} and ${options.targetColumn} columns: ${answerFilePath}`);\n }\n const answer = new Map<string, string>();\n for (const row of rows.slice(1)) {\n if (isBlankRow(row)) continue;\n const id = row[columns.idIndex]?.trim() ?? '';\n const value = row[columns.targetIndex] ?? '';\n if (!id) throw new Error(`empty ${options.idColumn} in answer file: ${answerFilePath}`);\n if (answer.has(id)) throw new Error(`duplicate ${options.idColumn} in answer file: ${id}`);\n const validationError = options.metric.validatePrediction?.(value);\n if (validationError)\n throw new Error(`invalid ${options.targetColumn} in answer file for ${id}: ${validationError}`);\n answer.set(id, value);\n }\n if (answer.size === 0) throw new Error(`answer file has no rows: ${answerFilePath}`);\n return answer;\n}\n\nfunction findColumns(\n header: readonly string[],\n options: EvaluationJudgePresetOptions\n): { idIndex: number; targetIndex: number } | undefined {\n const names = header.map((name) => name.trim());\n const idIndex = names.indexOf(options.idColumn);\n const targetIndex = names.indexOf(options.targetColumn);\n return idIndex === -1 || targetIndex === -1 ? undefined : { idIndex, targetIndex };\n}\n\nfunction isBlankRow(row: readonly string[]): boolean {\n return row.every((cell) => cell.trim() === '');\n}\n\nfunction listIds(ids: readonly string[]): string {\n const listed = ids.slice(0, MAX_LISTED_IDS).map((id) => `\\`${clip(id)}\\``);\n return ids.length > MAX_LISTED_IDS ? `${listed.join(', ')}, ...` : listed.join(', ');\n}\n\nfunction clip(value: string): string {\n // Control characters and backticks would break the markdown list item and inline code the value is echoed in.\n const printable = value.replaceAll(/[\\p{Cc}]/gu, ' ').replaceAll('`', \"'\");\n if (printable.trim() === '') return '(空)';\n return printable.length > MAX_ECHOED_CELL_LENGTH ? `${printable.slice(0, MAX_ECHOED_CELL_LENGTH)}…` : printable;\n}\n\nfunction matchesExpectedResult(resolvedCwd: ResolvedCwd, result: Pick<TestCaseResult, 'decisionCode'>): boolean {\n return (result.decisionCode === DecisionCode.ACCEPTED) === (resolvedCwd.expectedResult === 'accepted');\n}\n\nfunction validateFiniteNumber(prediction: string): string | undefined {\n return Number.isFinite(Number(prediction)) && prediction.trim() !== '' ? undefined : '数値ではありません';\n}\n\nfunction mean(values: readonly number[]): number {\n return values.reduce((sum, value) => sum + value, 0) / values.length;\n}\n"],"mappings":"usBAkBA,MAKM,EAAgE,CACpE,mBAAoB,qBACpB,iBAAkB,4BAClB,wBAAyB,yBAC3B,EAkCa,EAAoB,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MAAM,EAAG,EAAsB,EAAE,GAAK,CACxG,CAEA,SAAS,EAAsB,EAA0B,EAAuD,CAC9G,OAAQ,EAAO,eAAiB,EAAa,WAAe,EAAY,iBAAmB,WAC7F,CAEA,SAAS,EAAqB,EAAwC,CACpE,OAAO,OAAO,SAAS,OAAO,CAAU,CAAC,GAAK,EAAW,KAAK,IAAM,GAAK,IAAA,GAAY,WACvF,CAEA,SAAS,EAAK,EAAmC,CAC/C,OAAO,EAAO,QAAQ,EAAK,IAAU,EAAM,EAAO,CAAC,EAAI,EAAO,MAChE"}
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("../helpers/printTestCaseResult.cjs"),n=require("../helpers/sandboxUser.cjs"),r=require("../helpers/readOutputFiles.cjs"),i=require("../helpers/parseArgs.cjs"),a=require("../helpers/readTestCases.cjs"),o=require("../types/language.cjs"),s=require("../types/decisionCode.cjs"),c=require("../helpers/cleanWorkingDirectory.cjs"),l=require("../helpers/copyTestCaseFileInput.cjs"),u=require("../helpers/findEntryPointFile.cjs"),d=require("../helpers/findLanguageDefinitionByPath.cjs"),f=require("../helpers/judgeByStaticAnalysis.cjs"),p=require("../helpers/runCustomRunner.cjs"),m=require("../helpers/readProblemMarkdownFrontMatter.cjs"),h=require("../helpers/spawnSyncWithTimeout.cjs");let g=require("node:path");g=e.__toESM(g,1);let _=require("zod"),v=require("node:child_process");v=e.__toESM(v,1);let y=require("node:os");y=e.__toESM(y,1);let b=require("node:fs/promises");b=e.__toESM(b,1);let x=require("node:timers/promises");const S=.3,C=.2,w=[y.default.platform()===`darwin`?`gtime`:`/usr/bin/time`,`--format`,`%e %M`],T=_.z.object({language:_.z.union([_.z.string(),_.z.array(_.z.string())]).optional()});async function E(e,a){let o=i.parseArgs(process.argv);if(!o.cwd)throw Error(`cwd argument required`);let h=T.parse(o.params),g=o.cwd;n.makeAccessibleToSandboxUser(g);let _=await m.readProblemMarkdownFrontMatter(e),v=await(a.readTestCases??j)(e),y=v.length>0?v:[{id:`default`}],b=y[0]?.id??`prebuild`,x=await f.judgeByStaticAnalysis(o.cwd,_);if(x){t.printTestCaseResult({testCaseId:b,...x});return}let C=a.mainFilePath??await u.findEntryPointFile(o.cwd,h.language);if(!C){t.printTestCaseResult({testCaseId:b,decisionCode:s.DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,stderr:a.mainFilePath?`required main file not found: ${a.mainFilePath}`:`main file not found${h.language?`: language: ${h.language}`:``}`});return}let w=d.findLanguageDefinitionByPath(C);if(!w){t.printTestCaseResult({testCaseId:b,decisionCode:s.DecisionCode.WRONG_ANSWER,stderr:`unsupported language`});return}let E={...process.env,CI:``,FORCE_COLOR:`0`},N=await D({cwd:o.cwd,language:h.language,configuredMainFilePath:a.mainFilePath});if(w.prebuild)try{await w.prebuild(o.cwd);let e=await D({cwd:o.cwd,language:h.language??O(C),configuredMainFilePath:a.mainFilePath,allowConfiguredPathFallback:!0});e&&(N=e)}catch(e){t.printTestCaseResult({testCaseId:b,decisionCode:s.DecisionCode.BUILD_ERROR,stderr:V(e)});return}if(!N){t.printTestCaseResult({testCaseId:b,decisionCode:s.DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,stderr:a.mainFilePath?`required main file not found: ${a.mainFilePath}`:`main file not found${h.language?`: language: ${h.language}`:``}`});return}let P;if(a.prepare)try{P=await p.runCustomRunner(()=>a.prepare?.({cwd:g,env:{...E,...n.getSandboxUserEnvOverrides(E)},mainFilePath:N,problemMarkdownFrontMatter:_}))}catch(e){t.printTestCaseResult({testCaseId:b,decisionCode:s.DecisionCode.BUILD_ERROR,stderr:V(e)});return}let F=P??k({cwd:o.cwd,env:E,mainFilePath:N,languageDefinition:w});if(F){t.printTestCaseResult({testCaseId:b,decisionCode:F.decisionCode??s.DecisionCode.BUILD_ERROR,feedbackMarkdown:F.feedbackMarkdown,stderr:F.stderr,stdout:F.stdout,outputFiles:F.outputFiles});return}let L=await c.snapshotWorkingDirectory(o.cwd),R,z=b,B;try{R=a.runCommand?void 0:await I();let e=v.shared?.fileInputPath;for(let i of y){z=i.id,e&&await l.copyTestCaseFileInput(e,o.cwd),i.fileInputPath&&await l.copyTestCaseFileInput(i.fileInputPath,o.cwd);let u=typeof _.timeLimitMs==`number`?_.timeLimitMs/1e3:a.runTimeoutSeconds??5,d=R?{...E,DISPLAY:R.display}:E,f=await a.resolveInput?.({testCase:i,cwd:o.cwd,env:d})??i.input??``;B=f;let m=await a.command?.({testCase:i,cwd:o.cwd,env:d,mainFilePath:N})??w.command(N),h;try{h=a.runCommand?await p.runCustomRunner(()=>a.runCommand?.({testCase:i,command:n.wrapCommandWithSandboxUser(m),stdin:f,cwd:g,env:{...d,...n.getSandboxUserEnvOverrides(d)},timeLimitSeconds:u,screenshotWaitSeconds:a.screenshotWaitSeconds??S,stopDetectionThreshold:a.stopDetectionThreshold??5})):await M({command:m,stdin:f,cwd:o.cwd,env:d,timeLimitSeconds:u,screenshotWaitSeconds:a.screenshotWaitSeconds??S,stopDetectionThreshold:a.stopDetectionThreshold??5})}catch(e){t.printTestCaseResult({testCaseId:i.id,decisionCode:s.DecisionCode.RUNTIME_ERROR,stdin:f,stderr:V(e)}),await c.cleanWorkingDirectory(o.cwd,L);return}let v=await r.readOutputFiles(o.cwd,_.requiredOutputFilePaths??[]),y={timeLimitSeconds:u,problemMarkdownFrontMatter:{memoryLimitByte:_.memoryLimitByte,requiredOutputFilePaths:_.requiredOutputFilePaths}},b=A({runResult:h,outputFiles:v,context:y}),x=b;if(b.decisionCode===s.DecisionCode.ACCEPTED)try{let e=await a.test({testCase:i,runResult:h,outputFiles:v,cwd:o.cwd,context:y});x={decisionCode:e.decisionCode??b.decisionCode,feedbackMarkdown:e.feedbackMarkdown,stderr:e.stderr,stdout:e.stdout,outputFiles:e.outputFiles}}catch(e){x={decisionCode:s.DecisionCode.RUNTIME_ERROR,stderr:V(e)}}let C=x.decisionCode??s.DecisionCode.ACCEPTED,T=x.stdout??h.stdout,D=x.stderr??h.stderr;if(t.printTestCaseResult({testCaseId:i.id,decisionCode:C,exitStatus:h.status,stdin:h.stdin||void 0,stdout:T||void 0,stderr:D||void 0,timeSeconds:h.timeSeconds,memoryBytes:h.memoryBytes,feedbackMarkdown:x.feedbackMarkdown,outputFiles:x.outputFiles??(v.length>0?v:void 0)}),await c.cleanWorkingDirectory(o.cwd,L),C!==s.DecisionCode.ACCEPTED)break}}catch(e){t.printTestCaseResult({testCaseId:z,decisionCode:s.DecisionCode.RUNTIME_ERROR,stdin:B,stderr:V(e)}),await c.cleanWorkingDirectory(o.cwd,L)}finally{try{n.sandboxUserName&&n.killSandboxUserProcesses()}finally{await R?.dispose()}}}async function D(e){if(e.configuredMainFilePath){if(await B(g.default.join(e.cwd,e.configuredMainFilePath)))return e.configuredMainFilePath;if(!e.allowConfiguredPathFallback)return}return await u.findEntryPointFile(e.cwd,e.language)}function O(e){let t=Object.entries(o.languageIdToDefinition).filter(([,t])=>t.fileExtensions.some(t=>e.endsWith(t))).map(([e])=>e);return t.length>0?t:void 0}function k(e){let t=e.languageDefinition.buildCommand?.(e.mainFilePath);if(!t)return;let n=h.spawnSyncWithTimeout(t[0],t.slice(1),{cwd:e.cwd,encoding:`utf8`,env:e.env},10);if(n.timeSeconds>10)return{decisionCode:s.DecisionCode.BUILD_TIME_LIMIT_EXCEEDED,stderr:n.stderr||void 0};if(n.status!==0)return{decisionCode:s.DecisionCode.BUILD_ERROR,stderr:n.stderr||n.stdout||void 0}}function A(e){if(e.runResult.stopReason===`timeout`)return{decisionCode:s.DecisionCode.TIME_LIMIT_EXCEEDED,stderr:e.runResult.stderr};if(e.runResult.status!==0)return{decisionCode:s.DecisionCode.RUNTIME_ERROR,stderr:e.runResult.stderr};if(e.runResult.memoryBytes>(e.context.problemMarkdownFrontMatter.memoryLimitByte??1/0))return{decisionCode:s.DecisionCode.MEMORY_LIMIT_EXCEEDED,stderr:e.runResult.stderr};let t=e.context.problemMarkdownFrontMatter.requiredOutputFilePaths?.length??0;return e.outputFiles.length<t?{decisionCode:s.DecisionCode.MISSING_REQUIRED_OUTPUT_FILE_ERROR}:{decisionCode:s.DecisionCode.ACCEPTED}}async function j(e){return await a.readTestCases(g.default.join(e,`test_cases`))}async function M(e){let t=n.wrapCommandWithSandboxUser([n.TIMEOUT_COMMAND,e.timeLimitSeconds.toFixed(3),...w,...e.command]),r=n.startSandboxTimeoutWatchdog(e.timeLimitSeconds),i;try{i=v.default.spawn(t[0],t.slice(1),{cwd:e.cwd,env:{...e.env,...n.getSandboxUserEnvOverrides(e.env)},detached:process.platform!==`win32`,stdio:[`pipe`,`pipe`,`pipe`]})}catch(e){throw r.cancel(),e}i.stdout.setEncoding(`utf8`),i.stderr.setEncoding(`utf8`);let a=``,o=``,s,c,l=`process_exit`,u=!1,d=[],f=[],p=Date.now()/1e3,m=0;i.stdout.on(`data`,e=>{a+=e}),i.stderr.on(`data`,e=>{o+=e}),i.on(`error`,e=>{c=e,s=1}),i.stdin.on(`error`,()=>{}),i.on(`close`,(e,t)=>{if(e===124){l=`timeout`,s=0;return}if(t){s=1,o=o?`${o}\nprocess terminated by signal: ${t}`:`process terminated by signal: ${t}`;return}s=e??1});try{for(e.stdin&&i.stdin.write(e.stdin),i.stdin.end();s===void 0;){if(await(0,x.setTimeout)(e.screenshotWaitSeconds*1e3),m=Math.max(m,R(i.pid)),f=N(e.env.DISPLAY).toSorted((e,t)=>e.data.length-t.data.length),f.length>0){let t=f.map(e=>e.data).toSorted();if(d.unshift(t),d.length=Math.min(d.length,e.stopDetectionThreshold),d.length===e.stopDetectionThreshold&&d.every(e=>e.length===t.length&&e.every((e,n)=>e===t[n]))){l=`stable_screenshot`,s=0;break}}if(Date.now()/1e3-p>e.timeLimitSeconds){l=`timeout`,s=0;break}}l!==`process_exit`&&i.exitCode===null&&(i.removeAllListeners(`close`),i.removeAllListeners(`error`));let t=l===`process_exit`&&r.fired();if(await L(i),u=!0,c)throw c;let{memoryBytes:n,stderr:h,timeSeconds:g}=F(o,p,m);return{stdin:e.stdin,stdout:a.trimEnd(),stderr:h,status:t?0:s,timeSeconds:g,memoryBytes:n,screenshots:f,stopReason:t?`timeout`:l}}finally{u&&r.cancel()}}function N(e){let t=n.getTrustedHelperEnv(e?{DISPLAY:e}:{DISPLAY:process.env.DISPLAY}),r=v.default.spawnSync(`xwininfo`,[`-root`,`-tree`],{encoding:`utf8`,env:t});if(r.error)throw r.error;if(r.status!==0||!r.stdout)return[];let i=[];for(let e of P(r.stdout)){let n=v.default.spawnSync(`maim`,[`-i`,e],{env:t});if(n.error)throw n.error;if(n.status!==0||n.stdout.length===0)continue;let r=v.default.spawnSync(`xdotool`,[`getwindowname`,e],{encoding:`utf8`,env:t});if(r.error)throw r.error;let a=r.stdout.trim().replaceAll(/[\s/]/g,`_`);i.push({path:`${a||`window`}_${e}.png`,data:n.stdout.toString(`base64`),encoding:`base64`})}return i}function P(e){let t=[],n=e.split(`
2
- `);for(let e of n){if(e.includes(`Root window id:`)||e.includes(`Parent window id:`)||e.includes(`()`))continue;let n=/^\s{5}(0x[\da-f]+) /.exec(e);n?.[1]&&t.push(Number.parseInt(n[1],16).toString())}return t}function F(e,t,n){let r=/(?:^|\n)(\d+\.\d+) (\d+)\s*$/.exec(e),i=r?e.slice(0,r.index).trimEnd():e.trimEnd(),a=Number(r?.[2])*1024||0;return{stderr:i,timeSeconds:Number(r?.[1])||Date.now()/1e3-t,memoryBytes:Math.max(a,n)}}async function I(){if(process.platform!==`linux`)throw Error(`GUI screenshot capture is supported only on Linux.`);for(let e=90;e<100;e++){let t=`:${e}`,r,i=v.default.spawn(`Xvfb`,[t,`-screen`,`0`,`1280x1024x24`,`-ac`],{stdio:`ignore`,env:n.getTrustedHelperEnv()});if(i.on(`error`,e=>{r=e}),await(0,x.setTimeout)(300),r)throw r;if(i.exitCode===null)return{display:t,dispose:async()=>{i.killed||(i.kill(`SIGTERM`),await(0,x.setTimeout)(100),i.exitCode===null&&i.kill(`SIGKILL`))}}}throw Error(`Xvfb could not be started.`)}async function L(e){if(e.pid){if(n.sandboxUserName){n.killSandboxUserProcesses([`TERM`]),await(0,x.setTimeout)(C*1e3),n.killSandboxUserProcesses([`KILL`]);return}z(e.pid,`SIGTERM`),await(0,x.setTimeout)(C*1e3),e.exitCode===null&&z(e.pid,`SIGKILL`)}}function R(e){if(!e||process.platform!==`linux`)return 0;let t=v.default.spawnSync(`ps`,[`-o`,`rss=`,`--no-headers`,`--pgroup`,String(e)],{encoding:`utf8`,env:n.getTrustedHelperEnv()});return t.error||t.status!==0||!t.stdout?0:t.stdout.split(`
3
- `).map(e=>Number(e.trim())).filter(e=>Number.isFinite(e)&&e>0).reduce((e,t)=>e+t*1024,0)}function z(e,t){try{if(process.platform!==`win32`){process.kill(-e,t);return}}catch{}try{process.kill(e,t)}catch{}}async function B(e){try{return await b.default.access(e),!0}catch{return!1}}function V(e){return e instanceof Error?e.message:String(e)}exports.guiCommandJudgePreset=E;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("../helpers/printTestCaseResult.cjs"),n=require("../helpers/parseArgs.cjs"),r=require("../helpers/readTestCases.cjs"),i=require("../types/language.cjs"),a=require("../types/decisionCode.cjs"),o=require("../helpers/cleanWorkingDirectory.cjs"),s=require("../helpers/copyTestCaseFileInput.cjs"),c=require("../helpers/findEntryPointFile.cjs"),l=require("../helpers/findLanguageDefinitionByPath.cjs"),u=require("../helpers/judgeByStaticAnalysis.cjs"),d=require("../helpers/readOutputFiles.cjs"),f=require("../helpers/readProblemMarkdownFrontMatter.cjs"),p=require("../helpers/spawnSyncWithTimeout.cjs");let m=require("node:path");m=e.__toESM(m,1);let h=require("zod"),g=require("node:fs/promises");g=e.__toESM(g,1);let _=require("node:child_process");_=e.__toESM(_,1);let v=require("node:os");v=e.__toESM(v,1);let y=require("node:timers/promises");const b=.3,x=[v.default.platform()===`darwin`?`gtime`:`/usr/bin/time`,`--format`,`%e %M`],S=h.z.object({language:h.z.union([h.z.string(),h.z.array(h.z.string())]).optional()});async function C(e,r){let i=n.parseArgs(process.argv);if(!i.cwd)throw Error(`cwd argument required`);let p=S.parse(i.params),m=i.cwd,h=await f.readProblemMarkdownFrontMatter(e),g=await(r.readTestCases??O)(e),_=g.length>0?g:[{id:`default`}],v=_[0]?.id??`prebuild`,y=await u.judgeByStaticAnalysis(i.cwd,h);if(y){t.printTestCaseResult({testCaseId:v,...y});return}let x=r.mainFilePath??await c.findEntryPointFile(i.cwd,p.language);if(!x){t.printTestCaseResult({testCaseId:v,decisionCode:a.DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,stderr:r.mainFilePath?`required main file not found: ${r.mainFilePath}`:`main file not found${p.language?`: language: ${p.language}`:``}`});return}let C=l.findLanguageDefinitionByPath(x);if(!C){t.printTestCaseResult({testCaseId:v,decisionCode:a.DecisionCode.WRONG_ANSWER,stderr:`unsupported language`});return}let A={...process.env,CI:``,FORCE_COLOR:`0`},j=await w({cwd:i.cwd,language:p.language,configuredMainFilePath:r.mainFilePath});if(C.prebuild)try{await C.prebuild(i.cwd);let e=await w({cwd:i.cwd,language:p.language??T(x),configuredMainFilePath:r.mainFilePath,allowConfiguredPathFallback:!0});e&&(j=e)}catch(e){t.printTestCaseResult({testCaseId:v,decisionCode:a.DecisionCode.BUILD_ERROR,stderr:R(e)});return}if(!j){t.printTestCaseResult({testCaseId:v,decisionCode:a.DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,stderr:r.mainFilePath?`required main file not found: ${r.mainFilePath}`:`main file not found${p.language?`: language: ${p.language}`:``}`});return}let M;if(r.prepare)try{M=await r.prepare({cwd:m,env:A,mainFilePath:j,problemMarkdownFrontMatter:h})}catch(e){t.printTestCaseResult({testCaseId:v,decisionCode:a.DecisionCode.BUILD_ERROR,stderr:R(e)});return}let P=M??E({cwd:i.cwd,env:A,mainFilePath:j,languageDefinition:C});if(P){t.printTestCaseResult({testCaseId:v,decisionCode:P.decisionCode??a.DecisionCode.BUILD_ERROR,feedbackMarkdown:P.feedbackMarkdown,stderr:P.stderr,stdout:P.stdout,outputFiles:P.outputFiles});return}let F=await o.snapshotWorkingDirectory(i.cwd),I,L=v,z;try{I=r.runCommand?void 0:await N();let e=g.shared?.fileInputPath;for(let n of _){L=n.id,e&&await s.copyTestCaseFileInput(e,i.cwd),n.fileInputPath&&await s.copyTestCaseFileInput(n.fileInputPath,i.cwd);let c=typeof h.timeLimitMs==`number`?h.timeLimitMs/1e3:r.runTimeoutSeconds??5,l=I?{...A,DISPLAY:I.display}:A,u=await r.resolveInput?.({testCase:n,cwd:i.cwd,env:l})??n.input??``;z=u;let f=await r.command?.({testCase:n,cwd:i.cwd,env:l,mainFilePath:j})??C.command(j),p;try{p=r.runCommand?await r.runCommand({testCase:n,command:f,stdin:u,cwd:m,env:l,timeLimitSeconds:c,screenshotWaitSeconds:r.screenshotWaitSeconds??b,stopDetectionThreshold:r.stopDetectionThreshold??5}):await k({command:f,stdin:u,cwd:i.cwd,env:l,timeLimitSeconds:c,screenshotWaitSeconds:r.screenshotWaitSeconds??b,stopDetectionThreshold:r.stopDetectionThreshold??5})}catch(e){t.printTestCaseResult({testCaseId:n.id,decisionCode:a.DecisionCode.RUNTIME_ERROR,stdin:u,stderr:R(e)}),await o.cleanWorkingDirectory(i.cwd,F);return}let g=await d.readOutputFiles(i.cwd,h.requiredOutputFilePaths??[]),_={timeLimitSeconds:c,problemMarkdownFrontMatter:{memoryLimitByte:h.memoryLimitByte,requiredOutputFilePaths:h.requiredOutputFilePaths}},v=D({runResult:p,outputFiles:g,context:_}),y=v;if(v.decisionCode===a.DecisionCode.ACCEPTED)try{let e=await r.test({testCase:n,runResult:p,outputFiles:g,cwd:i.cwd,context:_});y={decisionCode:e.decisionCode??v.decisionCode,feedbackMarkdown:e.feedbackMarkdown,stderr:e.stderr,stdout:e.stdout,outputFiles:e.outputFiles}}catch(e){y={decisionCode:a.DecisionCode.RUNTIME_ERROR,stderr:R(e)}}let x=y.decisionCode??a.DecisionCode.ACCEPTED,S=y.stdout??p.stdout,w=y.stderr??p.stderr;if(t.printTestCaseResult({testCaseId:n.id,decisionCode:x,exitStatus:p.status,stdin:p.stdin||void 0,stdout:S||void 0,stderr:w||void 0,timeSeconds:p.timeSeconds,memoryBytes:p.memoryBytes,feedbackMarkdown:y.feedbackMarkdown,outputFiles:y.outputFiles??(g.length>0?g:void 0)}),await o.cleanWorkingDirectory(i.cwd,F),x!==a.DecisionCode.ACCEPTED)break}}catch(e){t.printTestCaseResult({testCaseId:L,decisionCode:a.DecisionCode.RUNTIME_ERROR,stdin:z,stderr:R(e)}),await o.cleanWorkingDirectory(i.cwd,F)}finally{await I?.dispose()}}async function w(e){if(e.configuredMainFilePath){if(await L(m.default.join(e.cwd,e.configuredMainFilePath)))return e.configuredMainFilePath;if(!e.allowConfiguredPathFallback)return}return await c.findEntryPointFile(e.cwd,e.language)}function T(e){let t=Object.entries(i.languageIdToDefinition).filter(([,t])=>t.fileExtensions.some(t=>e.endsWith(t))).map(([e])=>e);return t.length>0?t:void 0}function E(e){let t=e.languageDefinition.buildCommand?.(e.mainFilePath);if(!t)return;let n=p.spawnSyncWithTimeout(t[0],t.slice(1),{cwd:e.cwd,encoding:`utf8`,env:e.env},10);if(n.timeSeconds>10)return{decisionCode:a.DecisionCode.BUILD_TIME_LIMIT_EXCEEDED,stderr:n.stderr||void 0};if(n.status!==0)return{decisionCode:a.DecisionCode.BUILD_ERROR,stderr:n.stderr||n.stdout||void 0}}function D(e){if(e.runResult.stopReason===`timeout`)return{decisionCode:a.DecisionCode.TIME_LIMIT_EXCEEDED,stderr:e.runResult.stderr};if(e.runResult.status!==0)return{decisionCode:a.DecisionCode.RUNTIME_ERROR,stderr:e.runResult.stderr};if(e.runResult.memoryBytes>(e.context.problemMarkdownFrontMatter.memoryLimitByte??1/0))return{decisionCode:a.DecisionCode.MEMORY_LIMIT_EXCEEDED,stderr:e.runResult.stderr};let t=e.context.problemMarkdownFrontMatter.requiredOutputFilePaths?.length??0;return e.outputFiles.length<t?{decisionCode:a.DecisionCode.MISSING_REQUIRED_OUTPUT_FILE_ERROR}:{decisionCode:a.DecisionCode.ACCEPTED}}async function O(e){return await r.readTestCases(m.default.join(e,`test_cases`))}async function k(e){let t=_.default.spawn(`timeout`,[e.timeLimitSeconds.toFixed(3),...x,...e.command],{cwd:e.cwd,env:e.env,detached:process.platform!==`win32`,stdio:[`pipe`,`pipe`,`pipe`]});t.stdout.setEncoding(`utf8`),t.stderr.setEncoding(`utf8`);let n=``,r=``,i,a,o=`process_exit`,s=[],c=[],l=Date.now()/1e3,u=0;t.stdout.on(`data`,e=>{n+=e}),t.stderr.on(`data`,e=>{r+=e}),t.on(`error`,e=>{a=e,i=1}),t.stdin.on(`error`,()=>{}),t.on(`close`,(e,t)=>{if(e===124){o=`timeout`,i=0;return}if(t){i=1,r=r?`${r}\nprocess terminated by signal: ${t}`:`process terminated by signal: ${t}`;return}i=e??1});try{for(e.stdin&&t.stdin.write(e.stdin),t.stdin.end();i===void 0;){if(await(0,y.setTimeout)(e.screenshotWaitSeconds*1e3),u=Math.max(u,F(t.pid)),c=A(e.env.DISPLAY).toSorted((e,t)=>e.data.length-t.data.length),c.length>0){let t=c.map(e=>e.data).toSorted();if(s.unshift(t),s.length=Math.min(s.length,e.stopDetectionThreshold),s.length===e.stopDetectionThreshold&&s.every(e=>e.length===t.length&&e.every((e,n)=>e===t[n]))){o=`stable_screenshot`,i=0;break}}if(Date.now()/1e3-l>e.timeLimitSeconds){o=`timeout`,i=0;break}}o!==`process_exit`&&t.exitCode===null&&(t.removeAllListeners(`close`),t.removeAllListeners(`error`))}finally{await P(t)}if(a)throw a;let{memoryBytes:d,stderr:f,timeSeconds:p}=M(r,l,u);return{stdin:e.stdin,stdout:n.trimEnd(),stderr:f,status:i,timeSeconds:p,memoryBytes:d,screenshots:c,stopReason:o}}function A(e){let t={...process.env,DISPLAY:e??process.env.DISPLAY},n=_.default.spawnSync(`xwininfo`,[`-root`,`-tree`],{encoding:`utf8`,env:t});if(n.error)throw n.error;if(n.status!==0||!n.stdout)return[];let r=[];for(let e of j(n.stdout)){let n=_.default.spawnSync(`maim`,[`-i`,e],{env:t});if(n.error)throw n.error;if(n.status!==0||n.stdout.length===0)continue;let i=_.default.spawnSync(`xdotool`,[`getwindowname`,e],{encoding:`utf8`,env:t});if(i.error)throw i.error;let a=i.stdout.trim().replaceAll(/[\s/]/g,`_`);r.push({path:`${a||`window`}_${e}.png`,data:n.stdout.toString(`base64`),encoding:`base64`})}return r}function j(e){let t=[],n=e.split(`
2
+ `);for(let e of n){if(e.includes(`Root window id:`)||e.includes(`Parent window id:`)||e.includes(`()`))continue;let n=/^\s{5}(0x[\da-f]+) /.exec(e);n?.[1]&&t.push(Number.parseInt(n[1],16).toString())}return t}function M(e,t,n){let r=/(?:^|\n)(\d+\.\d+) (\d+)\s*$/.exec(e),i=r?e.slice(0,r.index).trimEnd():e.trimEnd(),a=Number(r?.[2])*1024||0;return{stderr:i,timeSeconds:Number(r?.[1])||Date.now()/1e3-t,memoryBytes:Math.max(a,n)}}async function N(){if(process.platform!==`linux`)throw Error(`GUI screenshot capture is supported only on Linux.`);for(let e=90;e<100;e++){let t=`:${e}`,n,r=_.default.spawn(`Xvfb`,[t,`-screen`,`0`,`1280x1024x24`,`-ac`],{stdio:`ignore`});if(r.on(`error`,e=>{n=e}),await(0,y.setTimeout)(300),n)throw n;if(r.exitCode===null)return{display:t,dispose:async()=>{r.killed||(r.kill(`SIGTERM`),await(0,y.setTimeout)(100),r.exitCode===null&&r.kill(`SIGKILL`))}}}throw Error(`Xvfb could not be started.`)}async function P(e){e.pid&&(I(e.pid,`SIGTERM`),await(0,y.setTimeout)(200),e.exitCode===null&&I(e.pid,`SIGKILL`))}function F(e){if(!e||process.platform!==`linux`)return 0;let t=_.default.spawnSync(`ps`,[`-o`,`rss=`,`--no-headers`,`--pgroup`,String(e)],{encoding:`utf8`});return t.error||t.status!==0||!t.stdout?0:t.stdout.split(`
3
+ `).map(e=>Number(e.trim())).filter(e=>Number.isFinite(e)&&e>0).reduce((e,t)=>e+t*1024,0)}function I(e,t){try{if(process.platform!==`win32`){process.kill(-e,t);return}}catch{}try{process.kill(e,t)}catch{}}async function L(e){try{return await g.default.access(e),!0}catch{return!1}}function R(e){return e instanceof Error?e.message:String(e)}exports.guiCommandJudgePreset=C;
4
4
  //# sourceMappingURL=guiCommand.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"guiCommand.cjs","names":["os","z","parseArgs","readProblemMarkdownFrontMatter","judgeByStaticAnalysis","findEntryPointFile","DecisionCode","findLanguageDefinitionByPath","runCustomRunner","getSandboxUserEnvOverrides","snapshotWorkingDirectory","copyTestCaseFileInput","wrapCommandWithSandboxUser","cleanWorkingDirectory","readOutputFiles","sandboxUserName","path","languageIdToDefinition","spawnSyncWithTimeout","readFileTestCases","TIMEOUT_COMMAND","startSandboxTimeoutWatchdog","childProcess","wait","getTrustedHelperEnv","fs"],"sources":["../../src/presets/guiCommand.ts"],"sourcesContent":["import childProcess from 'node:child_process';\nimport fs from 'node:fs/promises';\nimport os from 'node:os';\nimport path from 'node:path';\nimport { setTimeout as wait } from 'node:timers/promises';\n\nimport { z } from 'zod';\n\nimport { cleanWorkingDirectory, snapshotWorkingDirectory } from '../helpers/cleanWorkingDirectory.js';\nimport { copyTestCaseFileInput } from '../helpers/copyTestCaseFileInput.js';\nimport { findEntryPointFile } from '../helpers/findEntryPointFile.js';\nimport { findLanguageDefinitionByPath } from '../helpers/findLanguageDefinitionByPath.js';\nimport { judgeByStaticAnalysis } from '../helpers/judgeByStaticAnalysis.js';\nimport { parseArgs } from '../helpers/parseArgs.js';\nimport { printTestCaseResult } from '../helpers/printTestCaseResult.js';\nimport { readOutputFiles } from '../helpers/readOutputFiles.js';\nimport { readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { readTestCases as readFileTestCases } from '../helpers/readTestCases.js';\nimport { runCustomRunner } from '../helpers/runCustomRunner.js';\nimport {\n getSandboxUserEnvOverrides,\n getTrustedHelperEnv,\n killSandboxUserProcesses,\n makeAccessibleToSandboxUser,\n sandboxUserName,\n startSandboxTimeoutWatchdog,\n TIMEOUT_COMMAND,\n wrapCommandWithSandboxUser,\n} from '../helpers/sandboxUser.js';\nimport { spawnSyncWithTimeout } from '../helpers/spawnSyncWithTimeout.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport { languageIdToDefinition } from '../types/language.js';\nimport type { ProblemMarkdownFrontMatter } from '../types/problem.js';\nimport type { TestCaseResult } from '../types/testCaseResult.js';\n\nconst BUILD_TIMEOUT_SECONDS = 10;\nconst JUDGE_DEFAULT_TIMEOUT_SECONDS = 5;\nconst SCREENSHOT_WAIT_SECONDS = 0.3;\nconst XVFB_STARTUP_WAIT_SECONDS = 0.3;\nconst XVFB_SHUTDOWN_WAIT_SECONDS = 0.1;\nconst PROCESS_SHUTDOWN_WAIT_SECONDS = 0.2;\nconst STOP_DETECTION_THRESHOLD = 5;\nconst TIME_COMMAND = [os.platform() === 'darwin' ? 'gtime' : '/usr/bin/time', '--format', '%e %M'] as const;\n\nconst judgeParamsSchema = z.object({\n language: z.union([z.string(), z.array(z.string())]).optional(),\n});\n\n/** What every GUI test case needs; a custom `readTestCases` may add any fields of its own. */\ninterface BaseGuiTestCase {\n id: string;\n /** Standard input (`test_cases/<id>.in`). */\n input?: string;\n /** Directory copied into the working directory before the run (`test_cases/<id>.fin/`). */\n fileInputPath?: string;\n}\n\n/** A test case read from `test_cases/` by the default reader. */\nexport interface GuiTestCase extends BaseGuiTestCase {\n /** Expected standard output (`test_cases/<id>.out`), for the problem's `test` to compare. */\n output?: string;\n /** Directory of expected output files (`test_cases/<id>.fout/`), for the problem's `test` to compare. */\n fileOutputPath?: string;\n}\n\nexport interface GuiScreenshotFile {\n path: string;\n data: string;\n encoding: 'base64';\n}\n\nexport interface GuiCommandRunResult {\n stdin: string;\n stdout: string;\n stderr: string;\n status: number | undefined;\n timeSeconds: number;\n memoryBytes: number;\n screenshots: GuiScreenshotFile[];\n stopReason: 'process_exit' | 'stable_screenshot' | 'timeout';\n}\n\ninterface GuiJudgeContext {\n timeLimitSeconds: number;\n problemMarkdownFrontMatter: Pick<ProblemMarkdownFrontMatter, 'memoryLimitByte' | 'requiredOutputFilePaths'>;\n}\n\ntype GuiJudgeCaseResult = Pick<\n TestCaseResult,\n 'decisionCode' | 'feedbackMarkdown' | 'stderr' | 'stdout' | 'outputFiles'\n>;\n\nexport interface GuiCommandJudgePresetOptions<TTestCase extends BaseGuiTestCase = GuiTestCase> {\n mainFilePath?: string;\n runTimeoutSeconds?: number;\n screenshotWaitSeconds?: number;\n stopDetectionThreshold?: number;\n readTestCases?: (problemDir: string) => Promise<readonly TTestCase[]>;\n prepare?: (context: {\n cwd: string;\n /**\n * Build the submission with it. Under `EXERCODE_SANDBOX_USER` delegation it already carries the\n * sandbox user's overrides, but the handler must still wrap whatever it spawns with\n * `wrapCommandWithSandboxUser` (both exported): a build runs the submission's own scripts\n * (`package.json` lifecycle scripts, `build.gradle`, `build.rs`), which as the trusted harness\n * user could read the problem's test cases and rewrite the harness. The preset terminates\n * leftover sandbox processes after the handler returns.\n */\n env: NodeJS.ProcessEnv;\n mainFilePath: string;\n problemMarkdownFrontMatter: ProblemMarkdownFrontMatter;\n }) => Promise<Partial<GuiJudgeCaseResult> | undefined> | Partial<GuiJudgeCaseResult> | undefined;\n /**\n * Runs as the trusted harness user with the submission's `cwd`. Fixture files it creates there\n * must be written with `createDirectoryWithoutFollowingSymlinks`/`writeFileWithoutFollowingSymlinks`\n * (both exported): a submission can plant a symlink at a fixture path, and a plain `fs.writeFile`\n * would follow it into a file only the harness can write.\n */\n resolveInput?: (context: { testCase: TTestCase; cwd: string; env: NodeJS.ProcessEnv }) => Promise<string> | string;\n command?: (context: {\n testCase: TTestCase;\n cwd: string;\n env: NodeJS.ProcessEnv;\n mainFilePath: string;\n }) => Promise<readonly [string, ...string[]]> | readonly [string, ...string[]];\n runCommand?: (context: {\n testCase: TTestCase;\n /**\n * The command to run. Under `EXERCODE_SANDBOX_USER` delegation it is already wrapped so it\n * executes as the sandbox user; spawn it as given (with the supplied `env`) instead of\n * reconstructing it, or the submission runs as the trusted harness user.\n *\n * Its direct child is then a root-owned `sudo` whose descendants belong to the sandbox user, so\n * the handler cannot signal them: enforce `timeLimitSeconds` with `startSandboxTimeoutWatchdog`\n * and `killSandboxUserProcesses` (both exported) rather than `child.kill()` or an outer\n * `timeout`. The preset terminates leftover sandbox processes after the handler returns.\n */\n command: readonly [string, ...string[]];\n stdin: string;\n cwd: string;\n env: NodeJS.ProcessEnv;\n timeLimitSeconds: number;\n screenshotWaitSeconds: number;\n stopDetectionThreshold: number;\n }) => Promise<GuiCommandRunResult> | GuiCommandRunResult;\n test: (context: {\n testCase: TTestCase;\n runResult: GuiCommandRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n /** The submission's working directory, e.g. for `compareExpectedOutputFiles(cwd, testCase.fileOutputPath)`. */\n cwd: string;\n context: GuiJudgeContext;\n }) => Promise<Partial<GuiJudgeCaseResult>> | Partial<GuiJudgeCaseResult>;\n}\n\n/**\n * A preset function for judging GUI programs by collecting screenshots while the program runs.\n *\n * Keep problem-specific logic in `prepare`, `command`, and `test`.\n *\n * @example\n * Create `judge.ts`:\n * ```ts\n * import { DecisionCode } from '@exercode/problem-utils';\n * import { guiCommandJudgePreset } from '@exercode/problem-utils/presets/guiCommand';\n *\n * await guiCommandJudgePreset(import.meta.dirname, {\n * mainFilePath: 'Main.java',\n * readTestCases: async () => [{ id: 'default' }],\n * test: ({ runResult }) => {\n * return runResult.screenshots.length > 0\n * ? { decisionCode: DecisionCode.ACCEPTED }\n * : { decisionCode: DecisionCode.WRONG_ANSWER };\n * },\n * });\n * ```\n */\nexport async function guiCommandJudgePreset<TTestCase extends BaseGuiTestCase = GuiTestCase>(\n problemDir: string,\n options: GuiCommandJudgePresetOptions<TTestCase>\n): Promise<void> {\n const args = parseArgs(process.argv);\n if (!args.cwd) throw new Error('cwd argument required');\n const params = judgeParamsSchema.parse(args.params);\n const submissionDir = args.cwd;\n\n // The sandboxed submission must read its sources and write build/run outputs in its directory.\n makeAccessibleToSandboxUser(submissionDir);\n\n const problemMarkdownFrontMatter = await readProblemMarkdownFrontMatter(problemDir);\n const configuredTestCases = await (options.readTestCases ?? readGuiTestCases<TTestCase>)(problemDir);\n const testCases =\n configuredTestCases.length > 0 ? configuredTestCases : ([{ id: 'default' }] as unknown as readonly TTestCase[]);\n const prebuildTestCaseId = testCases[0]?.id ?? 'prebuild';\n\n const staticAnalysisResult = await judgeByStaticAnalysis(args.cwd, problemMarkdownFrontMatter);\n if (staticAnalysisResult) {\n printTestCaseResult({ testCaseId: prebuildTestCaseId, ...staticAnalysisResult });\n return;\n }\n\n const initialMainFilePath = options.mainFilePath ?? (await findEntryPointFile(args.cwd, params.language));\n if (!initialMainFilePath) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,\n stderr: options.mainFilePath\n ? `required main file not found: ${options.mainFilePath}`\n : `main file not found${params.language ? `: language: ${params.language}` : ''}`,\n });\n return;\n }\n\n const languageDefinition = findLanguageDefinitionByPath(initialMainFilePath);\n if (!languageDefinition) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.WRONG_ANSWER,\n stderr: 'unsupported language',\n });\n return;\n }\n\n const env = { ...process.env, CI: '', FORCE_COLOR: '0' };\n\n let resolvedMainFilePath = await resolveMainFilePath({\n cwd: args.cwd,\n language: params.language,\n configuredMainFilePath: options.mainFilePath,\n });\n if (languageDefinition.prebuild) {\n try {\n await languageDefinition.prebuild(args.cwd);\n const prebuiltMainFilePath = await resolveMainFilePath({\n cwd: args.cwd,\n language: params.language ?? inferLanguageIdsByPath(initialMainFilePath),\n configuredMainFilePath: options.mainFilePath,\n allowConfiguredPathFallback: true,\n });\n if (prebuiltMainFilePath) resolvedMainFilePath = prebuiltMainFilePath;\n } catch (error) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.BUILD_ERROR,\n stderr: errorToMessage(error),\n });\n return;\n }\n }\n if (!resolvedMainFilePath) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,\n stderr: options.mainFilePath\n ? `required main file not found: ${options.mainFilePath}`\n : `main file not found${params.language ? `: language: ${params.language}` : ''}`,\n });\n return;\n }\n\n let customPrepareResult: Partial<GuiJudgeCaseResult> | undefined;\n if (options.prepare) {\n try {\n customPrepareResult = await runCustomRunner(() =>\n options.prepare?.({\n cwd: submissionDir,\n env: { ...env, ...getSandboxUserEnvOverrides(env) },\n mainFilePath: resolvedMainFilePath,\n problemMarkdownFrontMatter,\n })\n );\n } catch (error) {\n // Like the `prebuild` step above: report the failed build rather than letting the throw (from\n // the handler itself, or from the fail-closed sandbox sweep around it) end the run resultless.\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.BUILD_ERROR,\n stderr: errorToMessage(error),\n });\n return;\n }\n }\n const prepareResult =\n customPrepareResult ??\n runDefaultPrepare({\n cwd: args.cwd,\n env,\n mainFilePath: resolvedMainFilePath,\n languageDefinition,\n });\n if (prepareResult) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: prepareResult.decisionCode ?? DecisionCode.BUILD_ERROR,\n feedbackMarkdown: prepareResult.feedbackMarkdown,\n stderr: prepareResult.stderr,\n stdout: prepareResult.stdout,\n outputFiles: prepareResult.outputFiles,\n });\n return;\n }\n\n const cwdSnapshot = await snapshotWorkingDirectory(args.cwd);\n let displayServer: Awaited<ReturnType<typeof ensureDisplayServer>> | undefined;\n let currentTestCaseId = prebuildTestCaseId;\n let currentStdin: string | undefined;\n try {\n displayServer = options.runCommand ? undefined : await ensureDisplayServer();\n const sharedFileInputPath = (configuredTestCases as { shared?: { fileInputPath?: string } }).shared?.fileInputPath;\n for (const testCase of testCases) {\n currentTestCaseId = testCase.id;\n if (sharedFileInputPath) await copyTestCaseFileInput(sharedFileInputPath, args.cwd);\n if (testCase.fileInputPath) await copyTestCaseFileInput(testCase.fileInputPath, args.cwd);\n\n const timeLimitSeconds =\n typeof problemMarkdownFrontMatter.timeLimitMs === 'number'\n ? problemMarkdownFrontMatter.timeLimitMs / 1000\n : (options.runTimeoutSeconds ?? JUDGE_DEFAULT_TIMEOUT_SECONDS);\n\n const runEnv = displayServer ? { ...env, DISPLAY: displayServer.display } : env;\n const stdin = (await options.resolveInput?.({ testCase, cwd: args.cwd, env: runEnv })) ?? testCase.input ?? '';\n currentStdin = stdin;\n const command =\n (await options.command?.({ testCase, cwd: args.cwd, env: runEnv, mainFilePath: resolvedMainFilePath })) ??\n languageDefinition.command(resolvedMainFilePath);\n\n let runResult: GuiCommandRunResult;\n try {\n runResult = options.runCommand\n ? await runCustomRunner(\n () =>\n // Hand custom runners a command that is already sandbox-wrapped, and the matching\n // environment: they spawn it themselves, so an unwrapped command would run the\n // submission as the trusted harness user and defeat the delegation boundary.\n options.runCommand?.({\n testCase,\n command: wrapCommandWithSandboxUser(command),\n stdin,\n cwd: submissionDir,\n env: { ...runEnv, ...getSandboxUserEnvOverrides(runEnv) },\n timeLimitSeconds,\n screenshotWaitSeconds: options.screenshotWaitSeconds ?? SCREENSHOT_WAIT_SECONDS,\n stopDetectionThreshold: options.stopDetectionThreshold ?? STOP_DETECTION_THRESHOLD,\n }) as Promise<GuiCommandRunResult>\n )\n : await spawnGuiProgram({\n command,\n stdin,\n cwd: args.cwd,\n env: runEnv,\n timeLimitSeconds,\n screenshotWaitSeconds: options.screenshotWaitSeconds ?? SCREENSHOT_WAIT_SECONDS,\n stopDetectionThreshold: options.stopDetectionThreshold ?? STOP_DETECTION_THRESHOLD,\n });\n } catch (error) {\n printTestCaseResult({\n testCaseId: testCase.id,\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stdin,\n stderr: errorToMessage(error),\n });\n await cleanWorkingDirectory(args.cwd, cwdSnapshot);\n return;\n }\n\n const outputFiles = await readOutputFiles(args.cwd, problemMarkdownFrontMatter.requiredOutputFilePaths ?? []);\n const judgeContext: GuiJudgeContext = {\n timeLimitSeconds,\n problemMarkdownFrontMatter: {\n memoryLimitByte: problemMarkdownFrontMatter.memoryLimitByte,\n requiredOutputFilePaths: problemMarkdownFrontMatter.requiredOutputFilePaths,\n },\n };\n const baseJudgeResult = evaluateGuiRunResult({ runResult, outputFiles, context: judgeContext });\n let judgeResult = baseJudgeResult;\n if (baseJudgeResult.decisionCode === DecisionCode.ACCEPTED) {\n try {\n const extendedJudgeResult = await options.test({\n testCase,\n runResult,\n outputFiles,\n cwd: args.cwd,\n context: judgeContext,\n });\n judgeResult = {\n decisionCode: extendedJudgeResult.decisionCode ?? baseJudgeResult.decisionCode,\n feedbackMarkdown: extendedJudgeResult.feedbackMarkdown,\n stderr: extendedJudgeResult.stderr,\n stdout: extendedJudgeResult.stdout,\n outputFiles: extendedJudgeResult.outputFiles,\n };\n } catch (error) {\n judgeResult = {\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stderr: errorToMessage(error),\n };\n }\n }\n\n const decisionCode = judgeResult.decisionCode ?? DecisionCode.ACCEPTED;\n const stdout = judgeResult.stdout ?? runResult.stdout;\n const stderr = judgeResult.stderr ?? runResult.stderr;\n printTestCaseResult({\n testCaseId: testCase.id,\n decisionCode,\n exitStatus: runResult.status,\n stdin: runResult.stdin || undefined,\n stdout: stdout || undefined,\n stderr: stderr || undefined,\n timeSeconds: runResult.timeSeconds,\n memoryBytes: runResult.memoryBytes,\n feedbackMarkdown: judgeResult.feedbackMarkdown,\n outputFiles: judgeResult.outputFiles ?? (outputFiles.length > 0 ? outputFiles : undefined),\n });\n\n await cleanWorkingDirectory(args.cwd, cwdSnapshot);\n if (decisionCode !== DecisionCode.ACCEPTED) break;\n }\n } catch (error) {\n printTestCaseResult({\n testCaseId: currentTestCaseId,\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stdin: currentStdin,\n stderr: errorToMessage(error),\n });\n await cleanWorkingDirectory(args.cwd, cwdSnapshot);\n } finally {\n try {\n // Sweep any sandbox processes the run left behind (e.g. a SIGTERM-ignoring forked child) so\n // they cannot race the harness's output reads or survive into the next test case/request.\n if (sandboxUserName) killSandboxUserProcesses();\n } finally {\n // Must run even when the sweep fails closed, or Xvfb keeps holding its display number and\n // later GUI requests exhaust the `:90`–`:99` range.\n await displayServer?.dispose();\n }\n }\n}\n\nasync function resolveMainFilePath(context: {\n cwd: string;\n language?: string | string[];\n configuredMainFilePath?: string;\n allowConfiguredPathFallback?: boolean;\n}): Promise<string | undefined> {\n if (context.configuredMainFilePath) {\n const resolvedPath = path.join(context.cwd, context.configuredMainFilePath);\n if (await pathExists(resolvedPath)) return context.configuredMainFilePath;\n if (!context.allowConfiguredPathFallback) return undefined;\n }\n\n return await findEntryPointFile(context.cwd, context.language);\n}\n\nfunction inferLanguageIdsByPath(filePath: string): string[] | undefined {\n const languageIds = Object.entries(languageIdToDefinition)\n .filter(([, definition]) => definition.fileExtensions.some((ext) => filePath.endsWith(ext)))\n .map(([languageId]) => languageId);\n return languageIds.length > 0 ? languageIds : undefined;\n}\n\nfunction runDefaultPrepare(context: {\n cwd: string;\n env: NodeJS.ProcessEnv;\n mainFilePath: string;\n languageDefinition: NonNullable<ReturnType<typeof findLanguageDefinitionByPath>>;\n}): Partial<GuiJudgeCaseResult> | undefined {\n const buildCommand = context.languageDefinition.buildCommand?.(context.mainFilePath);\n if (!buildCommand) return undefined;\n\n const buildResult = spawnSyncWithTimeout(\n buildCommand[0],\n buildCommand.slice(1),\n { cwd: context.cwd, encoding: 'utf8', env: context.env },\n BUILD_TIMEOUT_SECONDS\n );\n\n if (buildResult.timeSeconds > BUILD_TIMEOUT_SECONDS) {\n return {\n decisionCode: DecisionCode.BUILD_TIME_LIMIT_EXCEEDED,\n stderr: buildResult.stderr || undefined,\n };\n }\n\n if (buildResult.status !== 0) {\n return {\n decisionCode: DecisionCode.BUILD_ERROR,\n stderr: buildResult.stderr || buildResult.stdout || undefined,\n };\n }\n\n return undefined;\n}\n\nfunction evaluateGuiRunResult(context: {\n runResult: GuiCommandRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n context: GuiJudgeContext;\n}): Partial<GuiJudgeCaseResult> {\n if (context.runResult.stopReason === 'timeout') {\n return {\n decisionCode: DecisionCode.TIME_LIMIT_EXCEEDED,\n stderr: context.runResult.stderr,\n };\n }\n\n if (context.runResult.status !== 0) {\n return {\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stderr: context.runResult.stderr,\n };\n }\n\n if (\n context.runResult.memoryBytes >\n (context.context.problemMarkdownFrontMatter.memoryLimitByte ?? Number.POSITIVE_INFINITY)\n ) {\n return {\n decisionCode: DecisionCode.MEMORY_LIMIT_EXCEEDED,\n stderr: context.runResult.stderr,\n };\n }\n\n const requiredOutputFilesCount = context.context.problemMarkdownFrontMatter.requiredOutputFilePaths?.length ?? 0;\n if (context.outputFiles.length < requiredOutputFilesCount) {\n return {\n decisionCode: DecisionCode.MISSING_REQUIRED_OUTPUT_FILE_ERROR,\n };\n }\n\n return { decisionCode: DecisionCode.ACCEPTED };\n}\n\nasync function readGuiTestCases<TTestCase extends BaseGuiTestCase>(problemDir: string): Promise<readonly TTestCase[]> {\n // The default reader yields the base shape; a narrower TTestCase must come from `options.readTestCases`.\n return (await readFileTestCases(path.join(problemDir, 'test_cases'))) as unknown as readonly TTestCase[];\n}\n\nasync function spawnGuiProgram(context: {\n command: readonly [string, ...string[]];\n stdin: string;\n cwd: string;\n env: NodeJS.ProcessEnv;\n timeLimitSeconds: number;\n screenshotWaitSeconds: number;\n stopDetectionThreshold: number;\n}): Promise<GuiCommandRunResult> {\n const wrappedCommand = wrapCommandWithSandboxUser([\n TIMEOUT_COMMAND,\n context.timeLimitSeconds.toFixed(3),\n ...TIME_COMMAND,\n ...context.command,\n ]);\n // A submission can signal its own sandboxed `timeout` and then wedge the event loop (e.g.\n // XGrabServer makes the synchronous `xwininfo`/`maim` block), so a harness-owned watchdog\n // force-kills the sandbox user at the deadline; killing the client also releases its X grab.\n // Armed before the submission starts: a submission that exhausts the PID cgroup would otherwise\n // prevent the watchdog from spawning at all.\n const watchdog = startSandboxTimeoutWatchdog(context.timeLimitSeconds);\n let child: childProcess.ChildProcessWithoutNullStreams;\n try {\n child = childProcess.spawn(wrappedCommand[0], wrappedCommand.slice(1), {\n cwd: context.cwd,\n env: { ...context.env, ...getSandboxUserEnvOverrides(context.env) },\n detached: process.platform !== 'win32',\n stdio: ['pipe', 'pipe', 'pipe'],\n });\n } catch (error) {\n watchdog.cancel();\n throw error;\n }\n\n child.stdout.setEncoding('utf8');\n child.stderr.setEncoding('utf8');\n\n let stdout = '';\n let stderr = '';\n let exitCode: number | undefined;\n let spawnError: Error | undefined;\n let stopReason: GuiCommandRunResult['stopReason'] = 'process_exit';\n let submissionStopped = false;\n const screenshotSignaturesHistory: string[][] = [];\n let screenshots: GuiScreenshotFile[] = [];\n const startTimeSeconds = Date.now() / 1000;\n let sampledMemoryBytes = 0;\n child.stdout.on('data', (chunk: string) => {\n stdout += chunk;\n });\n child.stderr.on('data', (chunk: string) => {\n stderr += chunk;\n });\n child.on('error', (error) => {\n spawnError = error;\n exitCode = 1;\n });\n // A child that exits before the input is fully written makes the write fail; without a listener\n // that EPIPE would be an unhandled stream error terminating the harness mid-run.\n child.stdin.on('error', () => {\n // The submission simply stopped reading its input; the exit handling below reports the result.\n });\n child.on('close', (code, signal) => {\n if (code === 124) {\n stopReason = 'timeout';\n exitCode = 0;\n return;\n }\n if (signal) {\n exitCode = 1;\n stderr = stderr\n ? `${stderr}\\nprocess terminated by signal: ${signal}`\n : `process terminated by signal: ${signal}`;\n return;\n }\n exitCode = code ?? 1;\n });\n\n // Everything below must run under the `finally` that decides the watchdog's fate: it is cancelled\n // only once the submission is demonstrably stopped. When a helper throws first — `takeScreenshots`\n // rethrows spawn failures, which a submission can provoke by exhausting PIDs — the watchdog stays\n // armed on purpose, since nothing else would end the submission. The cost is that a detached\n // watchdog can outlive this harness and SIGKILL sandbox processes of a request that starts within\n // its remaining deadline, which is the same trade-off the package-manager runner accepts.\n try {\n if (context.stdin) child.stdin.write(context.stdin);\n child.stdin.end();\n\n while (exitCode === undefined) {\n await wait(context.screenshotWaitSeconds * 1000);\n sampledMemoryBytes = Math.max(sampledMemoryBytes, readProcessGroupMemoryBytes(child.pid));\n const currentScreenshots = takeScreenshots(context.env.DISPLAY);\n screenshots = currentScreenshots.toSorted((a, b) => a.data.length - b.data.length);\n\n if (screenshots.length > 0) {\n const screenshotSignatures = screenshots.map((file) => file.data).toSorted();\n screenshotSignaturesHistory.unshift(screenshotSignatures);\n screenshotSignaturesHistory.length = Math.min(\n screenshotSignaturesHistory.length,\n context.stopDetectionThreshold\n );\n if (\n screenshotSignaturesHistory.length === context.stopDetectionThreshold &&\n screenshotSignaturesHistory.every(\n (files) =>\n files.length === screenshotSignatures.length &&\n files.every((file, index) => file === screenshotSignatures[index])\n )\n ) {\n stopReason = 'stable_screenshot';\n exitCode = 0;\n break;\n }\n }\n\n if (Date.now() / 1000 - startTimeSeconds > context.timeLimitSeconds) {\n stopReason = 'timeout';\n exitCode = 0;\n break;\n }\n }\n\n if (stopReason !== 'process_exit' && child.exitCode === null) {\n child.removeAllListeners('close');\n child.removeAllListeners('error');\n }\n // A submission can kill its own same-UID `timeout` and be stopped by the watchdog instead,\n // which surfaces as a SIGKILL rather than a deadline. Report that as the timeout it is, not as\n // the runtime error the signal would otherwise imply — but only when the child's fate is what\n // decided the verdict. A `stable_screenshot` (or the loop's own `timeout`) was already decided\n // from the screenshots, so the watchdog must not overwrite it.\n const watchdogFired = stopReason === 'process_exit' && watchdog.fired();\n await stopProcess(child);\n submissionStopped = true;\n if (spawnError) throw spawnError;\n const {\n memoryBytes,\n stderr: normalizedStderr,\n timeSeconds,\n } = parseTimedStderr(stderr, startTimeSeconds, sampledMemoryBytes);\n\n return {\n stdin: context.stdin,\n stdout: stdout.trimEnd(),\n stderr: normalizedStderr,\n status: watchdogFired ? 0 : exitCode,\n timeSeconds,\n memoryBytes,\n screenshots,\n stopReason: watchdogFired ? 'timeout' : stopReason,\n };\n } finally {\n // Only once the submission is demonstrably stopped: if termination itself failed (or never ran\n // because a helper threw), the watchdog is the only thing left able to end it, and cancelling\n // would leave it running unchecked.\n if (submissionStopped) watchdog.cancel();\n }\n}\n\nfunction takeScreenshots(display: string | undefined): GuiScreenshotFile[] {\n // These helpers run as the trusted harness user, so they must not be resolved through a\n // submission-influenced `PATH` (see `getTrustedHelperEnv`).\n const env = getTrustedHelperEnv(display ? { DISPLAY: display } : { DISPLAY: process.env.DISPLAY });\n const xwininfo = childProcess.spawnSync('xwininfo', ['-root', '-tree'], { encoding: 'utf8', env });\n if (xwininfo.error) throw xwininfo.error;\n if (xwininfo.status !== 0 || !xwininfo.stdout) return [];\n\n const screenshots: GuiScreenshotFile[] = [];\n for (const windowId of extractTopLevelWindowIds(xwininfo.stdout)) {\n const screenshot = childProcess.spawnSync('maim', ['-i', windowId], { env });\n if (screenshot.error) throw screenshot.error;\n if (screenshot.status !== 0 || screenshot.stdout.length === 0) continue;\n\n const windowNameResult = childProcess.spawnSync('xdotool', ['getwindowname', windowId], { encoding: 'utf8', env });\n if (windowNameResult.error) throw windowNameResult.error;\n const windowName = windowNameResult.stdout.trim().replaceAll(/[\\s/]/g, '_');\n\n screenshots.push({\n path: `${windowName || 'window'}_${windowId}.png`,\n data: screenshot.stdout.toString('base64'),\n encoding: 'base64',\n });\n }\n\n return screenshots;\n}\n\nfunction extractTopLevelWindowIds(stdout: string): string[] {\n const windowIds: string[] = [];\n const lines = stdout.split('\\n');\n for (const line of lines) {\n if (line.includes('Root window id:') || line.includes('Parent window id:') || line.includes('()')) continue;\n\n const match = /^\\s{5}(0x[\\da-f]+) /.exec(line);\n if (!match?.[1]) continue;\n windowIds.push(Number.parseInt(match[1], 16).toString());\n }\n return windowIds;\n}\n\nfunction parseTimedStderr(\n stderr: string,\n startTimeSeconds: number,\n sampledMemoryBytes: number\n): Pick<GuiCommandRunResult, 'stderr' | 'timeSeconds' | 'memoryBytes'> {\n const match = /(?:^|\\n)(\\d+\\.\\d+) (\\d+)\\s*$/.exec(stderr);\n const normalizedStderr = match ? stderr.slice(0, match.index).trimEnd() : stderr.trimEnd();\n const parsedMemoryBytes = Number(match?.[2]) * 1024 || 0;\n return {\n stderr: normalizedStderr,\n timeSeconds: Number(match?.[1]) || Date.now() / 1000 - startTimeSeconds,\n memoryBytes: Math.max(parsedMemoryBytes, sampledMemoryBytes),\n };\n}\n\nasync function ensureDisplayServer(): Promise<{ display: string; dispose: () => Promise<void> }> {\n if (process.platform !== 'linux') {\n throw new Error('GUI screenshot capture is supported only on Linux.');\n }\n\n for (let displayNumber = 90; displayNumber < 100; displayNumber++) {\n const display = `:${displayNumber}`;\n let spawnError: Error | undefined;\n const xvfb = childProcess.spawn('Xvfb', [display, '-screen', '0', '1280x1024x24', '-ac'], {\n stdio: 'ignore',\n // Runs as the trusted harness user, so pin the lookup to system paths.\n env: getTrustedHelperEnv(),\n });\n xvfb.on('error', (error) => {\n spawnError = error;\n });\n\n await wait(XVFB_STARTUP_WAIT_SECONDS * 1000);\n if (spawnError) throw spawnError;\n if (xvfb.exitCode !== null) continue;\n\n return {\n display,\n dispose: async () => {\n if (!xvfb.killed) {\n xvfb.kill('SIGTERM');\n await wait(XVFB_SHUTDOWN_WAIT_SECONDS * 1000);\n if (xvfb.exitCode === null) xvfb.kill('SIGKILL');\n }\n },\n };\n }\n\n throw new Error('Xvfb could not be started.');\n}\n\nasync function stopProcess(child: childProcess.ChildProcess): Promise<void> {\n if (!child.pid) return;\n // The current user cannot signal the root-owned sudo wrapper nor the sandbox user's processes, so\n // go through sudo. SIGKILL is unconditional after the grace period: `child.exitCode` reflects only\n // the sudo wrapper, so a daemonized child that ignored SIGTERM would survive if we gated on it.\n if (sandboxUserName) {\n killSandboxUserProcesses(['TERM']);\n await wait(PROCESS_SHUTDOWN_WAIT_SECONDS * 1000);\n killSandboxUserProcesses(['KILL']);\n return;\n }\n killProcessGroup(child.pid, 'SIGTERM');\n await wait(PROCESS_SHUTDOWN_WAIT_SECONDS * 1000);\n if (child.exitCode === null) killProcessGroup(child.pid, 'SIGKILL');\n}\n\nfunction readProcessGroupMemoryBytes(processGroupId: number | undefined): number {\n if (!processGroupId || process.platform !== 'linux') return 0;\n\n // The delegation contract requires sudoers `!use_pty`, so sudo execs the command in place and it\n // stays in the detached child's process group; `--pgroup` therefore still captures it there.\n const result = childProcess.spawnSync('ps', ['-o', 'rss=', '--no-headers', '--pgroup', String(processGroupId)], {\n encoding: 'utf8',\n // Runs as the trusted harness user, so pin the lookup to system paths.\n env: getTrustedHelperEnv(),\n });\n if (result.error || result.status !== 0 || !result.stdout) return 0;\n\n return result.stdout\n .split('\\n')\n .map((line) => Number(line.trim()))\n .filter((value) => Number.isFinite(value) && value > 0)\n .reduce((sum, value) => sum + value * 1024, 0);\n}\n\nfunction killProcessGroup(processGroupId: number, signal: NodeJS.Signals): void {\n try {\n if (process.platform !== 'win32') {\n process.kill(-processGroupId, signal);\n return;\n }\n } catch {\n // The process group may already be gone. Fall back to the direct PID below.\n }\n\n try {\n process.kill(processGroupId, signal);\n } catch {\n // The direct child may also already be gone.\n }\n}\n\nasync function pathExists(filePath: string): Promise<boolean> {\n try {\n await fs.access(filePath);\n return true;\n } catch {\n return false;\n }\n}\n\nfunction errorToMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n"],"mappings":"8iCAmCA,MAEM,EAA0B,GAG1B,EAAgC,GAEhC,EAAe,CAACA,EAAAA,QAAG,SAAS,IAAM,SAAW,QAAU,gBAAiB,WAAY,OAAO,EAE3F,EAAoBC,EAAAA,EAAE,OAAO,CACjC,SAAUA,EAAAA,EAAE,MAAM,CAACA,EAAAA,EAAE,OAAO,EA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9E,GAAI,CACF,GAAI,QAAQ,WAAa,QAAS,CAChC,QAAQ,KAAK,CAAC,EAAgB,CAAM,EACpC,MACF,CACF,MAAQ,CAER,CAEA,GAAI,CACF,QAAQ,KAAK,EAAgB,CAAM,CACrC,MAAQ,CAER,CACF,CAEA,eAAe,EAAW,EAAoC,CAC5D,GAAI,CAEF,OADA,MAAMC,EAAAA,QAAG,OAAO,CAAQ,EACjB,EACT,MAAQ,CACN,MAAO,EACT,CACF,CAEA,SAAS,EAAe,EAAwB,CAC9C,OAAO,aAAiB,MAAQ,EAAM,QAAU,OAAO,CAAK,CAC9D"}
1
+ {"version":3,"file":"guiCommand.cjs","names":["os","z","parseArgs","readProblemMarkdownFrontMatter","judgeByStaticAnalysis","findEntryPointFile","DecisionCode","findLanguageDefinitionByPath","snapshotWorkingDirectory","copyTestCaseFileInput","cleanWorkingDirectory","readOutputFiles","path","languageIdToDefinition","spawnSyncWithTimeout","readFileTestCases","childProcess","wait","fs"],"sources":["../../src/presets/guiCommand.ts"],"sourcesContent":["import childProcess from 'node:child_process';\nimport fs from 'node:fs/promises';\nimport os from 'node:os';\nimport path from 'node:path';\nimport { setTimeout as wait } from 'node:timers/promises';\n\nimport { z } from 'zod';\n\nimport { cleanWorkingDirectory, snapshotWorkingDirectory } from '../helpers/cleanWorkingDirectory.js';\nimport { copyTestCaseFileInput } from '../helpers/copyTestCaseFileInput.js';\nimport { findEntryPointFile } from '../helpers/findEntryPointFile.js';\nimport { findLanguageDefinitionByPath } from '../helpers/findLanguageDefinitionByPath.js';\nimport { judgeByStaticAnalysis } from '../helpers/judgeByStaticAnalysis.js';\nimport { parseArgs } from '../helpers/parseArgs.js';\nimport { printTestCaseResult } from '../helpers/printTestCaseResult.js';\nimport { readOutputFiles } from '../helpers/readOutputFiles.js';\nimport { readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { readTestCases as readFileTestCases } from '../helpers/readTestCases.js';\nimport { spawnSyncWithTimeout } from '../helpers/spawnSyncWithTimeout.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport { languageIdToDefinition } from '../types/language.js';\nimport type { ProblemMarkdownFrontMatter } from '../types/problem.js';\nimport type { TestCaseResult } from '../types/testCaseResult.js';\n\nconst BUILD_TIMEOUT_SECONDS = 10;\nconst JUDGE_DEFAULT_TIMEOUT_SECONDS = 5;\nconst SCREENSHOT_WAIT_SECONDS = 0.3;\nconst XVFB_STARTUP_WAIT_SECONDS = 0.3;\nconst XVFB_SHUTDOWN_WAIT_SECONDS = 0.1;\nconst PROCESS_SHUTDOWN_WAIT_SECONDS = 0.2;\nconst STOP_DETECTION_THRESHOLD = 5;\nconst TIME_COMMAND = [os.platform() === 'darwin' ? 'gtime' : '/usr/bin/time', '--format', '%e %M'] as const;\n\nconst judgeParamsSchema = z.object({\n language: z.union([z.string(), z.array(z.string())]).optional(),\n});\n\n/** What every GUI test case needs; a custom `readTestCases` may add any fields of its own. */\ninterface BaseGuiTestCase {\n id: string;\n /** Standard input (`test_cases/<id>.in`). */\n input?: string;\n /** Directory copied into the working directory before the run (`test_cases/<id>.fin/`). */\n fileInputPath?: string;\n}\n\n/** A test case read from `test_cases/` by the default reader. */\nexport interface GuiTestCase extends BaseGuiTestCase {\n /** Expected standard output (`test_cases/<id>.out`), for the problem's `test` to compare. */\n output?: string;\n /** Directory of expected output files (`test_cases/<id>.fout/`), for the problem's `test` to compare. */\n fileOutputPath?: string;\n}\n\nexport interface GuiScreenshotFile {\n path: string;\n data: string;\n encoding: 'base64';\n}\n\nexport interface GuiCommandRunResult {\n stdin: string;\n stdout: string;\n stderr: string;\n status: number | undefined;\n timeSeconds: number;\n memoryBytes: number;\n screenshots: GuiScreenshotFile[];\n stopReason: 'process_exit' | 'stable_screenshot' | 'timeout';\n}\n\ninterface GuiJudgeContext {\n timeLimitSeconds: number;\n problemMarkdownFrontMatter: Pick<ProblemMarkdownFrontMatter, 'memoryLimitByte' | 'requiredOutputFilePaths'>;\n}\n\ntype GuiJudgeCaseResult = Pick<\n TestCaseResult,\n 'decisionCode' | 'feedbackMarkdown' | 'stderr' | 'stdout' | 'outputFiles'\n>;\n\nexport interface GuiCommandJudgePresetOptions<TTestCase extends BaseGuiTestCase = GuiTestCase> {\n mainFilePath?: string;\n runTimeoutSeconds?: number;\n screenshotWaitSeconds?: number;\n stopDetectionThreshold?: number;\n readTestCases?: (problemDir: string) => Promise<readonly TTestCase[]>;\n prepare?: (context: {\n cwd: string;\n env: NodeJS.ProcessEnv;\n mainFilePath: string;\n problemMarkdownFrontMatter: ProblemMarkdownFrontMatter;\n }) => Promise<Partial<GuiJudgeCaseResult> | undefined> | Partial<GuiJudgeCaseResult> | undefined;\n resolveInput?: (context: { testCase: TTestCase; cwd: string; env: NodeJS.ProcessEnv }) => Promise<string> | string;\n command?: (context: {\n testCase: TTestCase;\n cwd: string;\n env: NodeJS.ProcessEnv;\n mainFilePath: string;\n }) => Promise<readonly [string, ...string[]]> | readonly [string, ...string[]];\n runCommand?: (context: {\n testCase: TTestCase;\n command: readonly [string, ...string[]];\n stdin: string;\n cwd: string;\n env: NodeJS.ProcessEnv;\n timeLimitSeconds: number;\n screenshotWaitSeconds: number;\n stopDetectionThreshold: number;\n }) => Promise<GuiCommandRunResult> | GuiCommandRunResult;\n test: (context: {\n testCase: TTestCase;\n runResult: GuiCommandRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n /** The submission's working directory, e.g. for `compareExpectedOutputFiles(cwd, testCase.fileOutputPath)`. */\n cwd: string;\n context: GuiJudgeContext;\n }) => Promise<Partial<GuiJudgeCaseResult>> | Partial<GuiJudgeCaseResult>;\n}\n\n/**\n * A preset function for judging GUI programs by collecting screenshots while the program runs.\n *\n * Keep problem-specific logic in `prepare`, `command`, and `test`.\n *\n * @example\n * Create `judge.ts`:\n * ```ts\n * import { DecisionCode } from '@exercode/problem-utils';\n * import { guiCommandJudgePreset } from '@exercode/problem-utils/presets/guiCommand';\n *\n * await guiCommandJudgePreset(import.meta.dirname, {\n * mainFilePath: 'Main.java',\n * readTestCases: async () => [{ id: 'default' }],\n * test: ({ runResult }) => {\n * return runResult.screenshots.length > 0\n * ? { decisionCode: DecisionCode.ACCEPTED }\n * : { decisionCode: DecisionCode.WRONG_ANSWER };\n * },\n * });\n * ```\n */\nexport async function guiCommandJudgePreset<TTestCase extends BaseGuiTestCase = GuiTestCase>(\n problemDir: string,\n options: GuiCommandJudgePresetOptions<TTestCase>\n): Promise<void> {\n const args = parseArgs(process.argv);\n if (!args.cwd) throw new Error('cwd argument required');\n const params = judgeParamsSchema.parse(args.params);\n const submissionDir = args.cwd;\n\n const problemMarkdownFrontMatter = await readProblemMarkdownFrontMatter(problemDir);\n const configuredTestCases = await (options.readTestCases ?? readGuiTestCases<TTestCase>)(problemDir);\n const testCases =\n configuredTestCases.length > 0 ? configuredTestCases : ([{ id: 'default' }] as unknown as readonly TTestCase[]);\n const prebuildTestCaseId = testCases[0]?.id ?? 'prebuild';\n\n const staticAnalysisResult = await judgeByStaticAnalysis(args.cwd, problemMarkdownFrontMatter);\n if (staticAnalysisResult) {\n printTestCaseResult({ testCaseId: prebuildTestCaseId, ...staticAnalysisResult });\n return;\n }\n\n const initialMainFilePath = options.mainFilePath ?? (await findEntryPointFile(args.cwd, params.language));\n if (!initialMainFilePath) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,\n stderr: options.mainFilePath\n ? `required main file not found: ${options.mainFilePath}`\n : `main file not found${params.language ? `: language: ${params.language}` : ''}`,\n });\n return;\n }\n\n const languageDefinition = findLanguageDefinitionByPath(initialMainFilePath);\n if (!languageDefinition) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.WRONG_ANSWER,\n stderr: 'unsupported language',\n });\n return;\n }\n\n const env = { ...process.env, CI: '', FORCE_COLOR: '0' };\n\n let resolvedMainFilePath = await resolveMainFilePath({\n cwd: args.cwd,\n language: params.language,\n configuredMainFilePath: options.mainFilePath,\n });\n if (languageDefinition.prebuild) {\n try {\n await languageDefinition.prebuild(args.cwd);\n const prebuiltMainFilePath = await resolveMainFilePath({\n cwd: args.cwd,\n language: params.language ?? inferLanguageIdsByPath(initialMainFilePath),\n configuredMainFilePath: options.mainFilePath,\n allowConfiguredPathFallback: true,\n });\n if (prebuiltMainFilePath) resolvedMainFilePath = prebuiltMainFilePath;\n } catch (error) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.BUILD_ERROR,\n stderr: errorToMessage(error),\n });\n return;\n }\n }\n if (!resolvedMainFilePath) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,\n stderr: options.mainFilePath\n ? `required main file not found: ${options.mainFilePath}`\n : `main file not found${params.language ? `: language: ${params.language}` : ''}`,\n });\n return;\n }\n\n let customPrepareResult: Partial<GuiJudgeCaseResult> | undefined;\n if (options.prepare) {\n try {\n customPrepareResult = await options.prepare({\n cwd: submissionDir,\n env,\n mainFilePath: resolvedMainFilePath,\n problemMarkdownFrontMatter,\n });\n } catch (error) {\n // Like the `prebuild` step above: report the failed build rather than letting the throw end the\n // run resultless.\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: DecisionCode.BUILD_ERROR,\n stderr: errorToMessage(error),\n });\n return;\n }\n }\n const prepareResult =\n customPrepareResult ??\n runDefaultPrepare({\n cwd: args.cwd,\n env,\n mainFilePath: resolvedMainFilePath,\n languageDefinition,\n });\n if (prepareResult) {\n printTestCaseResult({\n testCaseId: prebuildTestCaseId,\n decisionCode: prepareResult.decisionCode ?? DecisionCode.BUILD_ERROR,\n feedbackMarkdown: prepareResult.feedbackMarkdown,\n stderr: prepareResult.stderr,\n stdout: prepareResult.stdout,\n outputFiles: prepareResult.outputFiles,\n });\n return;\n }\n\n const cwdSnapshot = await snapshotWorkingDirectory(args.cwd);\n let displayServer: Awaited<ReturnType<typeof ensureDisplayServer>> | undefined;\n let currentTestCaseId = prebuildTestCaseId;\n let currentStdin: string | undefined;\n try {\n displayServer = options.runCommand ? undefined : await ensureDisplayServer();\n const sharedFileInputPath = (configuredTestCases as { shared?: { fileInputPath?: string } }).shared?.fileInputPath;\n for (const testCase of testCases) {\n currentTestCaseId = testCase.id;\n if (sharedFileInputPath) await copyTestCaseFileInput(sharedFileInputPath, args.cwd);\n if (testCase.fileInputPath) await copyTestCaseFileInput(testCase.fileInputPath, args.cwd);\n\n const timeLimitSeconds =\n typeof problemMarkdownFrontMatter.timeLimitMs === 'number'\n ? problemMarkdownFrontMatter.timeLimitMs / 1000\n : (options.runTimeoutSeconds ?? JUDGE_DEFAULT_TIMEOUT_SECONDS);\n\n const runEnv = displayServer ? { ...env, DISPLAY: displayServer.display } : env;\n const stdin = (await options.resolveInput?.({ testCase, cwd: args.cwd, env: runEnv })) ?? testCase.input ?? '';\n currentStdin = stdin;\n const command =\n (await options.command?.({ testCase, cwd: args.cwd, env: runEnv, mainFilePath: resolvedMainFilePath })) ??\n languageDefinition.command(resolvedMainFilePath);\n\n let runResult: GuiCommandRunResult;\n try {\n runResult = options.runCommand\n ? await options.runCommand({\n testCase,\n command,\n stdin,\n cwd: submissionDir,\n env: runEnv,\n timeLimitSeconds,\n screenshotWaitSeconds: options.screenshotWaitSeconds ?? SCREENSHOT_WAIT_SECONDS,\n stopDetectionThreshold: options.stopDetectionThreshold ?? STOP_DETECTION_THRESHOLD,\n })\n : await spawnGuiProgram({\n command,\n stdin,\n cwd: args.cwd,\n env: runEnv,\n timeLimitSeconds,\n screenshotWaitSeconds: options.screenshotWaitSeconds ?? SCREENSHOT_WAIT_SECONDS,\n stopDetectionThreshold: options.stopDetectionThreshold ?? STOP_DETECTION_THRESHOLD,\n });\n } catch (error) {\n printTestCaseResult({\n testCaseId: testCase.id,\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stdin,\n stderr: errorToMessage(error),\n });\n await cleanWorkingDirectory(args.cwd, cwdSnapshot);\n return;\n }\n\n const outputFiles = await readOutputFiles(args.cwd, problemMarkdownFrontMatter.requiredOutputFilePaths ?? []);\n const judgeContext: GuiJudgeContext = {\n timeLimitSeconds,\n problemMarkdownFrontMatter: {\n memoryLimitByte: problemMarkdownFrontMatter.memoryLimitByte,\n requiredOutputFilePaths: problemMarkdownFrontMatter.requiredOutputFilePaths,\n },\n };\n const baseJudgeResult = evaluateGuiRunResult({ runResult, outputFiles, context: judgeContext });\n let judgeResult = baseJudgeResult;\n if (baseJudgeResult.decisionCode === DecisionCode.ACCEPTED) {\n try {\n const extendedJudgeResult = await options.test({\n testCase,\n runResult,\n outputFiles,\n cwd: args.cwd,\n context: judgeContext,\n });\n judgeResult = {\n decisionCode: extendedJudgeResult.decisionCode ?? baseJudgeResult.decisionCode,\n feedbackMarkdown: extendedJudgeResult.feedbackMarkdown,\n stderr: extendedJudgeResult.stderr,\n stdout: extendedJudgeResult.stdout,\n outputFiles: extendedJudgeResult.outputFiles,\n };\n } catch (error) {\n judgeResult = {\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stderr: errorToMessage(error),\n };\n }\n }\n\n const decisionCode = judgeResult.decisionCode ?? DecisionCode.ACCEPTED;\n const stdout = judgeResult.stdout ?? runResult.stdout;\n const stderr = judgeResult.stderr ?? runResult.stderr;\n printTestCaseResult({\n testCaseId: testCase.id,\n decisionCode,\n exitStatus: runResult.status,\n stdin: runResult.stdin || undefined,\n stdout: stdout || undefined,\n stderr: stderr || undefined,\n timeSeconds: runResult.timeSeconds,\n memoryBytes: runResult.memoryBytes,\n feedbackMarkdown: judgeResult.feedbackMarkdown,\n outputFiles: judgeResult.outputFiles ?? (outputFiles.length > 0 ? outputFiles : undefined),\n });\n\n await cleanWorkingDirectory(args.cwd, cwdSnapshot);\n if (decisionCode !== DecisionCode.ACCEPTED) break;\n }\n } catch (error) {\n printTestCaseResult({\n testCaseId: currentTestCaseId,\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stdin: currentStdin,\n stderr: errorToMessage(error),\n });\n await cleanWorkingDirectory(args.cwd, cwdSnapshot);\n } finally {\n await displayServer?.dispose();\n }\n}\n\nasync function resolveMainFilePath(context: {\n cwd: string;\n language?: string | string[];\n configuredMainFilePath?: string;\n allowConfiguredPathFallback?: boolean;\n}): Promise<string | undefined> {\n if (context.configuredMainFilePath) {\n const resolvedPath = path.join(context.cwd, context.configuredMainFilePath);\n if (await pathExists(resolvedPath)) return context.configuredMainFilePath;\n if (!context.allowConfiguredPathFallback) return undefined;\n }\n\n return await findEntryPointFile(context.cwd, context.language);\n}\n\nfunction inferLanguageIdsByPath(filePath: string): string[] | undefined {\n const languageIds = Object.entries(languageIdToDefinition)\n .filter(([, definition]) => definition.fileExtensions.some((ext) => filePath.endsWith(ext)))\n .map(([languageId]) => languageId);\n return languageIds.length > 0 ? languageIds : undefined;\n}\n\nfunction runDefaultPrepare(context: {\n cwd: string;\n env: NodeJS.ProcessEnv;\n mainFilePath: string;\n languageDefinition: NonNullable<ReturnType<typeof findLanguageDefinitionByPath>>;\n}): Partial<GuiJudgeCaseResult> | undefined {\n const buildCommand = context.languageDefinition.buildCommand?.(context.mainFilePath);\n if (!buildCommand) return undefined;\n\n const buildResult = spawnSyncWithTimeout(\n buildCommand[0],\n buildCommand.slice(1),\n { cwd: context.cwd, encoding: 'utf8', env: context.env },\n BUILD_TIMEOUT_SECONDS\n );\n\n if (buildResult.timeSeconds > BUILD_TIMEOUT_SECONDS) {\n return {\n decisionCode: DecisionCode.BUILD_TIME_LIMIT_EXCEEDED,\n stderr: buildResult.stderr || undefined,\n };\n }\n\n if (buildResult.status !== 0) {\n return {\n decisionCode: DecisionCode.BUILD_ERROR,\n stderr: buildResult.stderr || buildResult.stdout || undefined,\n };\n }\n\n return undefined;\n}\n\nfunction evaluateGuiRunResult(context: {\n runResult: GuiCommandRunResult;\n outputFiles: NonNullable<TestCaseResult['outputFiles']>;\n context: GuiJudgeContext;\n}): Partial<GuiJudgeCaseResult> {\n if (context.runResult.stopReason === 'timeout') {\n return {\n decisionCode: DecisionCode.TIME_LIMIT_EXCEEDED,\n stderr: context.runResult.stderr,\n };\n }\n\n if (context.runResult.status !== 0) {\n return {\n decisionCode: DecisionCode.RUNTIME_ERROR,\n stderr: context.runResult.stderr,\n };\n }\n\n if (\n context.runResult.memoryBytes >\n (context.context.problemMarkdownFrontMatter.memoryLimitByte ?? Number.POSITIVE_INFINITY)\n ) {\n return {\n decisionCode: DecisionCode.MEMORY_LIMIT_EXCEEDED,\n stderr: context.runResult.stderr,\n };\n }\n\n const requiredOutputFilesCount = context.context.problemMarkdownFrontMatter.requiredOutputFilePaths?.length ?? 0;\n if (context.outputFiles.length < requiredOutputFilesCount) {\n return {\n decisionCode: DecisionCode.MISSING_REQUIRED_OUTPUT_FILE_ERROR,\n };\n }\n\n return { decisionCode: DecisionCode.ACCEPTED };\n}\n\nasync function readGuiTestCases<TTestCase extends BaseGuiTestCase>(problemDir: string): Promise<readonly TTestCase[]> {\n // The default reader yields the base shape; a narrower TTestCase must come from `options.readTestCases`.\n return (await readFileTestCases(path.join(problemDir, 'test_cases'))) as unknown as readonly TTestCase[];\n}\n\nasync function spawnGuiProgram(context: {\n command: readonly [string, ...string[]];\n stdin: string;\n cwd: string;\n env: NodeJS.ProcessEnv;\n timeLimitSeconds: number;\n screenshotWaitSeconds: number;\n stopDetectionThreshold: number;\n}): Promise<GuiCommandRunResult> {\n const child = childProcess.spawn(\n 'timeout',\n [context.timeLimitSeconds.toFixed(3), ...TIME_COMMAND, ...context.command],\n {\n cwd: context.cwd,\n env: context.env,\n detached: process.platform !== 'win32',\n stdio: ['pipe', 'pipe', 'pipe'],\n }\n );\n\n child.stdout.setEncoding('utf8');\n child.stderr.setEncoding('utf8');\n\n let stdout = '';\n let stderr = '';\n let exitCode: number | undefined;\n let spawnError: Error | undefined;\n let stopReason: GuiCommandRunResult['stopReason'] = 'process_exit';\n const screenshotSignaturesHistory: string[][] = [];\n let screenshots: GuiScreenshotFile[] = [];\n const startTimeSeconds = Date.now() / 1000;\n let sampledMemoryBytes = 0;\n child.stdout.on('data', (chunk: string) => {\n stdout += chunk;\n });\n child.stderr.on('data', (chunk: string) => {\n stderr += chunk;\n });\n child.on('error', (error) => {\n spawnError = error;\n exitCode = 1;\n });\n // A child that exits before the input is fully written makes the write fail; without a listener\n // that EPIPE would be an unhandled stream error terminating the harness mid-run.\n child.stdin.on('error', () => {\n // The submission simply stopped reading its input; the exit handling below reports the result.\n });\n child.on('close', (code, signal) => {\n if (code === 124) {\n stopReason = 'timeout';\n exitCode = 0;\n return;\n }\n if (signal) {\n exitCode = 1;\n stderr = stderr\n ? `${stderr}\\nprocess terminated by signal: ${signal}`\n : `process terminated by signal: ${signal}`;\n return;\n }\n exitCode = code ?? 1;\n });\n\n // Stop the program even when a helper throws (e.g. `xwininfo` missing); the spawned process\n // would otherwise outlive this run.\n try {\n if (context.stdin) child.stdin.write(context.stdin);\n child.stdin.end();\n\n while (exitCode === undefined) {\n await wait(context.screenshotWaitSeconds * 1000);\n sampledMemoryBytes = Math.max(sampledMemoryBytes, readProcessGroupMemoryBytes(child.pid));\n const currentScreenshots = takeScreenshots(context.env.DISPLAY);\n screenshots = currentScreenshots.toSorted((a, b) => a.data.length - b.data.length);\n\n if (screenshots.length > 0) {\n const screenshotSignatures = screenshots.map((file) => file.data).toSorted();\n screenshotSignaturesHistory.unshift(screenshotSignatures);\n screenshotSignaturesHistory.length = Math.min(\n screenshotSignaturesHistory.length,\n context.stopDetectionThreshold\n );\n if (\n screenshotSignaturesHistory.length === context.stopDetectionThreshold &&\n screenshotSignaturesHistory.every(\n (files) =>\n files.length === screenshotSignatures.length &&\n files.every((file, index) => file === screenshotSignatures[index])\n )\n ) {\n stopReason = 'stable_screenshot';\n exitCode = 0;\n break;\n }\n }\n\n if (Date.now() / 1000 - startTimeSeconds > context.timeLimitSeconds) {\n stopReason = 'timeout';\n exitCode = 0;\n break;\n }\n }\n\n if (stopReason !== 'process_exit' && child.exitCode === null) {\n child.removeAllListeners('close');\n child.removeAllListeners('error');\n }\n } finally {\n await stopProcess(child);\n }\n if (spawnError) throw spawnError;\n const {\n memoryBytes,\n stderr: normalizedStderr,\n timeSeconds,\n } = parseTimedStderr(stderr, startTimeSeconds, sampledMemoryBytes);\n\n return {\n stdin: context.stdin,\n stdout: stdout.trimEnd(),\n stderr: normalizedStderr,\n status: exitCode,\n timeSeconds,\n memoryBytes,\n screenshots,\n stopReason,\n };\n}\n\nfunction takeScreenshots(display: string | undefined): GuiScreenshotFile[] {\n // Node omits environment entries whose value is undefined, so a missing display is simply not set.\n const env = { ...process.env, DISPLAY: display ?? process.env.DISPLAY };\n const xwininfo = childProcess.spawnSync('xwininfo', ['-root', '-tree'], { encoding: 'utf8', env });\n if (xwininfo.error) throw xwininfo.error;\n if (xwininfo.status !== 0 || !xwininfo.stdout) return [];\n\n const screenshots: GuiScreenshotFile[] = [];\n for (const windowId of extractTopLevelWindowIds(xwininfo.stdout)) {\n const screenshot = childProcess.spawnSync('maim', ['-i', windowId], { env });\n if (screenshot.error) throw screenshot.error;\n if (screenshot.status !== 0 || screenshot.stdout.length === 0) continue;\n\n const windowNameResult = childProcess.spawnSync('xdotool', ['getwindowname', windowId], { encoding: 'utf8', env });\n if (windowNameResult.error) throw windowNameResult.error;\n const windowName = windowNameResult.stdout.trim().replaceAll(/[\\s/]/g, '_');\n\n screenshots.push({\n path: `${windowName || 'window'}_${windowId}.png`,\n data: screenshot.stdout.toString('base64'),\n encoding: 'base64',\n });\n }\n\n return screenshots;\n}\n\nfunction extractTopLevelWindowIds(stdout: string): string[] {\n const windowIds: string[] = [];\n const lines = stdout.split('\\n');\n for (const line of lines) {\n if (line.includes('Root window id:') || line.includes('Parent window id:') || line.includes('()')) continue;\n\n const match = /^\\s{5}(0x[\\da-f]+) /.exec(line);\n if (!match?.[1]) continue;\n windowIds.push(Number.parseInt(match[1], 16).toString());\n }\n return windowIds;\n}\n\nfunction parseTimedStderr(\n stderr: string,\n startTimeSeconds: number,\n sampledMemoryBytes: number\n): Pick<GuiCommandRunResult, 'stderr' | 'timeSeconds' | 'memoryBytes'> {\n const match = /(?:^|\\n)(\\d+\\.\\d+) (\\d+)\\s*$/.exec(stderr);\n const normalizedStderr = match ? stderr.slice(0, match.index).trimEnd() : stderr.trimEnd();\n const parsedMemoryBytes = Number(match?.[2]) * 1024 || 0;\n return {\n stderr: normalizedStderr,\n timeSeconds: Number(match?.[1]) || Date.now() / 1000 - startTimeSeconds,\n memoryBytes: Math.max(parsedMemoryBytes, sampledMemoryBytes),\n };\n}\n\nasync function ensureDisplayServer(): Promise<{ display: string; dispose: () => Promise<void> }> {\n if (process.platform !== 'linux') {\n throw new Error('GUI screenshot capture is supported only on Linux.');\n }\n\n for (let displayNumber = 90; displayNumber < 100; displayNumber++) {\n const display = `:${displayNumber}`;\n let spawnError: Error | undefined;\n const xvfb = childProcess.spawn('Xvfb', [display, '-screen', '0', '1280x1024x24', '-ac'], { stdio: 'ignore' });\n xvfb.on('error', (error) => {\n spawnError = error;\n });\n\n await wait(XVFB_STARTUP_WAIT_SECONDS * 1000);\n if (spawnError) throw spawnError;\n if (xvfb.exitCode !== null) continue;\n\n return {\n display,\n dispose: async () => {\n if (!xvfb.killed) {\n xvfb.kill('SIGTERM');\n await wait(XVFB_SHUTDOWN_WAIT_SECONDS * 1000);\n if (xvfb.exitCode === null) xvfb.kill('SIGKILL');\n }\n },\n };\n }\n\n throw new Error('Xvfb could not be started.');\n}\n\nasync function stopProcess(child: childProcess.ChildProcess): Promise<void> {\n if (!child.pid) return;\n killProcessGroup(child.pid, 'SIGTERM');\n await wait(PROCESS_SHUTDOWN_WAIT_SECONDS * 1000);\n if (child.exitCode === null) killProcessGroup(child.pid, 'SIGKILL');\n}\n\nfunction readProcessGroupMemoryBytes(processGroupId: number | undefined): number {\n if (!processGroupId || process.platform !== 'linux') return 0;\n\n const result = childProcess.spawnSync('ps', ['-o', 'rss=', '--no-headers', '--pgroup', String(processGroupId)], {\n encoding: 'utf8',\n });\n if (result.error || result.status !== 0 || !result.stdout) return 0;\n\n return result.stdout\n .split('\\n')\n .map((line) => Number(line.trim()))\n .filter((value) => Number.isFinite(value) && value > 0)\n .reduce((sum, value) => sum + value * 1024, 0);\n}\n\nfunction killProcessGroup(processGroupId: number, signal: NodeJS.Signals): void {\n try {\n if (process.platform !== 'win32') {\n process.kill(-processGroupId, signal);\n return;\n }\n } catch {\n // The process group may already be gone. Fall back to the direct PID below.\n }\n\n try {\n process.kill(processGroupId, signal);\n } catch {\n // The direct child may also already be gone.\n }\n}\n\nasync function pathExists(filePath: string): Promise<boolean> {\n try {\n await fs.access(filePath);\n return true;\n } catch {\n return false;\n }\n}\n\nfunction errorToMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n"],"mappings":"09BAwBA,MAEM,EAA0B,GAK1B,EAAe,CAACA,EAAAA,QAAG,SAAS,IAAM,SAAW,QAAU,gBAAiB,WAAY,OAAO,EAE3F,EAAoBC,EAAAA,EAAE,OAAO,CACjC,SAAUA,EAAAA,EAAE,MAAM,CAACA,EAAAA,EAAE,OAAO,EAAGA,EAAAA,EAAE,MAAMA,EAAAA,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAChE,CAAC,EA2GD,eAAsB,EACpB,EACA,EACe,CACf,IAAM,EAAOC,EAAAA,UAAU,QAAQ,IAAI,EACnC,GAAI,CAAC,EAAK,IAAK,MAAU,MAAM,uBAAuB,EACtD,IAAM,EAAS,EAAkB,MAAM,EAAK,MAAM,EAC5C,EAAgB,EAAK,IAErB,EAA6B,MAAMC,EAAAA,+BAA+B,CAAU,EAC5E,EAAsB,MAAO,EAAQ,eAAiB,EAAA,CAA6B,CAAU,EAC7F,EACJ,EAAoB,OAAS,EAAI,EAAuB,CAAC,CAAE,GAAI,SAAU,CAAC,EACtE,EAAqB,EAAU,EAAE,EAAE,IAAM,WAEzC,EAAuB,MAAMC,EAAAA,sBAAsB,EAAK,IAAK,CAA0B,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jB,EACT,MAAQ,CACN,MAAO,EACT,CACF,CAEA,SAAS,EAAe,EAAwB,CAC9C,OAAO,aAAiB,MAAQ,EAAM,QAAU,OAAO,CAAK,CAC9D"}
@@ -43,24 +43,10 @@ export interface GuiCommandJudgePresetOptions<TTestCase extends BaseGuiTestCase
43
43
  readTestCases?: (problemDir: string) => Promise<readonly TTestCase[]>;
44
44
  prepare?: (context: {
45
45
  cwd: string;
46
- /**
47
- * Build the submission with it. Under `EXERCODE_SANDBOX_USER` delegation it already carries the
48
- * sandbox user's overrides, but the handler must still wrap whatever it spawns with
49
- * `wrapCommandWithSandboxUser` (both exported): a build runs the submission's own scripts
50
- * (`package.json` lifecycle scripts, `build.gradle`, `build.rs`), which as the trusted harness
51
- * user could read the problem's test cases and rewrite the harness. The preset terminates
52
- * leftover sandbox processes after the handler returns.
53
- */
54
46
  env: NodeJS.ProcessEnv;
55
47
  mainFilePath: string;
56
48
  problemMarkdownFrontMatter: ProblemMarkdownFrontMatter;
57
49
  }) => Promise<Partial<GuiJudgeCaseResult> | undefined> | Partial<GuiJudgeCaseResult> | undefined;
58
- /**
59
- * Runs as the trusted harness user with the submission's `cwd`. Fixture files it creates there
60
- * must be written with `createDirectoryWithoutFollowingSymlinks`/`writeFileWithoutFollowingSymlinks`
61
- * (both exported): a submission can plant a symlink at a fixture path, and a plain `fs.writeFile`
62
- * would follow it into a file only the harness can write.
63
- */
64
50
  resolveInput?: (context: {
65
51
  testCase: TTestCase;
66
52
  cwd: string;
@@ -74,16 +60,6 @@ export interface GuiCommandJudgePresetOptions<TTestCase extends BaseGuiTestCase
74
60
  }) => Promise<readonly [string, ...string[]]> | readonly [string, ...string[]];
75
61
  runCommand?: (context: {
76
62
  testCase: TTestCase;
77
- /**
78
- * The command to run. Under `EXERCODE_SANDBOX_USER` delegation it is already wrapped so it
79
- * executes as the sandbox user; spawn it as given (with the supplied `env`) instead of
80
- * reconstructing it, or the submission runs as the trusted harness user.
81
- *
82
- * Its direct child is then a root-owned `sudo` whose descendants belong to the sandbox user, so
83
- * the handler cannot signal them: enforce `timeLimitSeconds` with `startSandboxTimeoutWatchdog`
84
- * and `killSandboxUserProcesses` (both exported) rather than `child.kill()` or an outer
85
- * `timeout`. The preset terminates leftover sandbox processes after the handler returns.
86
- */
87
63
  command: readonly [string, ...string[]];
88
64
  stdin: string;
89
65
  cwd: string;