@exercode/problem-utils 4.2.6 → 4.3.1

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 (90) 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.cjs.map +1 -1
  7. package/dist/helpers/checkProblemDirIsolation.js +1 -1
  8. package/dist/helpers/checkProblemDirIsolation.js.map +1 -1
  9. package/dist/helpers/findModelAnswerDirs.cjs +1 -1
  10. package/dist/helpers/findModelAnswerDirs.cjs.map +1 -1
  11. package/dist/helpers/findModelAnswerDirs.js +1 -1
  12. package/dist/helpers/findModelAnswerDirs.js.map +1 -1
  13. package/dist/helpers/guiRecording.cjs +2 -0
  14. package/dist/helpers/guiRecording.cjs.map +1 -0
  15. package/dist/helpers/guiRecording.d.ts +32 -0
  16. package/dist/helpers/guiRecording.js +2 -0
  17. package/dist/helpers/guiRecording.js.map +1 -0
  18. package/dist/helpers/judgeByStaticAnalysis.cjs +1 -1
  19. package/dist/helpers/judgeByStaticAnalysis.cjs.map +1 -1
  20. package/dist/helpers/judgeByStaticAnalysis.js +1 -1
  21. package/dist/helpers/judgeByStaticAnalysis.js.map +1 -1
  22. package/dist/helpers/readTestCases.cjs +1 -1
  23. package/dist/helpers/readTestCases.cjs.map +1 -1
  24. package/dist/helpers/readTestCases.js +1 -1
  25. package/dist/helpers/readTestCases.js.map +1 -1
  26. package/dist/helpers/runCommandInTemporaryPackageManagerProject.cjs +1 -1
  27. package/dist/helpers/runCommandInTemporaryPackageManagerProject.cjs.map +1 -1
  28. package/dist/helpers/runCommandInTemporaryPackageManagerProject.js +1 -1
  29. package/dist/helpers/runCommandInTemporaryPackageManagerProject.js.map +1 -1
  30. package/dist/helpers/runHarnessProcess.cjs +1 -1
  31. package/dist/helpers/runHarnessProcess.cjs.map +1 -1
  32. package/dist/helpers/runHarnessProcess.js +1 -1
  33. package/dist/helpers/runHarnessProcess.js.map +1 -1
  34. package/dist/helpers/spawnWithLimits.cjs +1 -1
  35. package/dist/helpers/spawnWithLimits.cjs.map +1 -1
  36. package/dist/helpers/spawnWithLimits.js +1 -1
  37. package/dist/helpers/spawnWithLimits.js.map +1 -1
  38. package/dist/index.cjs +1 -1
  39. package/dist/index.d.ts +1 -0
  40. package/dist/index.js +1 -1
  41. package/dist/learningMaterial/fsHelpers.cjs +1 -1
  42. package/dist/learningMaterial/fsHelpers.cjs.map +1 -1
  43. package/dist/learningMaterial/fsHelpers.js +1 -1
  44. package/dist/learningMaterial/fsHelpers.js.map +1 -1
  45. package/dist/learningMaterial/validateContest.cjs +1 -1
  46. package/dist/learningMaterial/validateContest.cjs.map +1 -1
  47. package/dist/learningMaterial/validateContest.js +1 -1
  48. package/dist/learningMaterial/validateContest.js.map +1 -1
  49. package/dist/learningMaterial/validateCourse.cjs +1 -1
  50. package/dist/learningMaterial/validateCourse.cjs.map +1 -1
  51. package/dist/learningMaterial/validateCourse.js +1 -1
  52. package/dist/learningMaterial/validateCourse.js.map +1 -1
  53. package/dist/learningMaterial/validateMaterial.cjs +3 -3
  54. package/dist/learningMaterial/validateMaterial.cjs.map +1 -1
  55. package/dist/learningMaterial/validateMaterial.js +3 -3
  56. package/dist/learningMaterial/validateMaterial.js.map +1 -1
  57. package/dist/learningMaterial/validateProblem.cjs +1 -1
  58. package/dist/learningMaterial/validateProblem.cjs.map +1 -1
  59. package/dist/learningMaterial/validateProblem.js +1 -1
  60. package/dist/learningMaterial/validateProblem.js.map +1 -1
  61. package/dist/learningMaterial/validationResult.cjs +1 -1
  62. package/dist/learningMaterial/validationResult.cjs.map +1 -1
  63. package/dist/learningMaterial/validationResult.js +1 -1
  64. package/dist/learningMaterial/validationResult.js.map +1 -1
  65. package/dist/presets/command.cjs +1 -1
  66. package/dist/presets/command.cjs.map +1 -1
  67. package/dist/presets/command.js +1 -1
  68. package/dist/presets/command.js.map +1 -1
  69. package/dist/presets/evaluation.cjs +2 -2
  70. package/dist/presets/evaluation.cjs.map +1 -1
  71. package/dist/presets/evaluation.js +2 -2
  72. package/dist/presets/evaluation.js.map +1 -1
  73. package/dist/presets/guiCommand.cjs +3 -3
  74. package/dist/presets/guiCommand.cjs.map +1 -1
  75. package/dist/presets/guiCommand.d.ts +17 -3
  76. package/dist/presets/guiCommand.js +3 -3
  77. package/dist/presets/guiCommand.js.map +1 -1
  78. package/dist/presets/springBoot.cjs +4 -4
  79. package/dist/presets/springBoot.cjs.map +1 -1
  80. package/dist/presets/springBoot.js +4 -4
  81. package/dist/presets/springBoot.js.map +1 -1
  82. package/dist/presets/stdio.cjs +1 -1
  83. package/dist/presets/stdio.cjs.map +1 -1
  84. package/dist/presets/stdio.js +1 -1
  85. package/dist/presets/stdio.js.map +1 -1
  86. package/dist/presets/tomcat.cjs +4 -4
  87. package/dist/presets/tomcat.cjs.map +1 -1
  88. package/dist/presets/tomcat.js +4 -4
  89. package/dist/presets/tomcat.js.map +1 -1
  90. package/package.json +5 -4
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("../types/testCaseResult.cjs"),n=require("../helpers/defaultStdioHarness.cjs"),r=require("../helpers/readTestCases.cjs"),i=require("../helpers/temporaryProblemDirCopy.cjs"),a=require("../helpers/readProblemMarkdownFrontMatter.cjs"),o=require("../types/decisionCode.cjs"),s=require("../helpers/runHarnessProcess.cjs"),c=require("../helpers/findModelAnswerDirs.cjs"),l=require("./runSingleHarness.cjs");let u=require("node:path");u=e.__toESM(u,1);let d=require("node:fs/promises");d=e.__toESM(d,1);const f=1e3,p=new Map(Object.entries(o.DecisionCode).map(([e,t])=>[t,e]));async function m(e){let t=_(e),i=u.default.resolve(t.rootDir),o=e=>(u.default.relative(i,e)||`.`).replaceAll(u.default.sep,`/`),s=await v(i),d=s.filter(e=>{let n=o(e);return t.only.length>0&&!t.only.some(e=>n.includes(e))?!1:!t.skip.some(e=>n.includes(e))});if(d.length===0)return console.error(s.length===0?`No problem directories (containing problem.md or <id>.problem.md) found under ${i}.`:`All ${s.length} problem directories under ${i} were excluded by --only/--skip.`),1;let f=[],p=[];for(let e of d){let t=await n.findDefaultStdioHarnessFiles(e);if(t.length>0){f.push(`${o(e)}: ${l.formatDefaultHarnessError(t)}`);continue}let i=await c.findModelAnswerDirs(e);if(i.length===0){f.push(`${o(e)}: no model answers found under model_answers/`);continue}if(!await b(u.default.join(e,`judge.ts`))&&(await r.readTestCases(u.default.join(e,`test_cases`))).length===0){let t;try{t=await a.readProblemMarkdownFrontMatter(e)}catch(t){f.push(`${o(e)}: failed to read the problem markdown front matter: ${t instanceof Error?t.message:String(t)}`);continue}if(!a.judgesWithoutTestCases(t)){f.push(`${o(e)}: ${l.MISSING_TEST_CASES_ERROR}`);continue}}let s=await c.findFailingModelAnswerDirs(e);p.push(...i.map(t=>({problemDir:e,answerDir:t,expectation:`accepted`})),...s.map(t=>({problemDir:e,answerDir:t,expectation:`rejected`})))}for(let e of f)console.error(`✗ ${e}`);let m=u.default.resolve(process.argv[1]??``),g=0,y=0;return await Promise.all(Array.from({length:Math.max(1,Math.min(t.concurrency,p.length))},async()=>{for(;y<p.length;){let e=p[y++];if(!e)return;let t=`${o(e.problemDir)} ${u.default.relative(e.problemDir,e.answerDir).replaceAll(u.default.sep,`/`)}`,n=await h(e,m);n===void 0?(g++,console.info(`✓ ${t}`)):(f.push(`${t}: ${n}`),console.error(`✗ ${t}: ${n}`))}})),console.info(`\n${g} passed, ${f.length} failed (${p.length} runs, ${d.length} problems)`),f.length===0?0:1}async function h(e,t){let n,r;try{({tempRoot:n,copiedProblemDir:r}=await i.copyProblemDirToTemporaryRoot(e.problemDir))}catch(e){return x(`failed to copy the problem directory to a temporary location: ${e instanceof Error?e.message:String(e)}`)}let a,o;try{a=g(e,await s.runHarnessProcess([`run`,t,`judge`,u.default.relative(e.problemDir,e.answerDir)],{cwd:r,env:process.env,timeoutMs:6e5,maxOutputBytes:67108864,tempRoot:n}))}catch(e){a=x(`harness execution failed: ${e instanceof Error?e.message:String(e)}`)}finally{o=await i.forciblyRemoveDirectory(n)}if(o)return a;let c=`failed to remove the temporary copy at ${n} (judged code may have left permission-locked files)`;return a===void 0?c:x(`${a}; ${c}`)}function g(e,n){let{stdout:r,stderr:i}=n;if(n.failureReason!==void 0)return x(n.failureReason);if(n.exitCode!==0)return x(`harness exited with ${n.exitCode??`a signal`}${i.trim()?`: ${i.trim()}`:``}`);let a=r.split(/\r?\n/).filter(e=>e.startsWith(t.TEST_CASE_RESULT_PREFIX)),s=[];for(let e of a){let n;try{n=t.testCaseResultSchema.safeParse(JSON.parse(e.slice(t.TEST_CASE_RESULT_PREFIX.length)))}catch{n=void 0}if(!n?.success)return x(`malformed test case result line: ${e}`);s.push(n.data)}if(s.length===0)return`no test case results were printed`;if(e.expectation===`accepted`){let e=s.find(e=>e.decisionCode!==o.DecisionCode.ACCEPTED);return e?x(`${p.get(e.decisionCode)??e.decisionCode} on test case ${e.testCaseId}${e.stderr?.trim()?`: ${e.stderr.trim()}`:``}`):void 0}return s.every(e=>e.decisionCode===o.DecisionCode.ACCEPTED)?`expected at least one failing test case, but all test cases were accepted`:void 0}function _(e){let t={rootDir:`.`,concurrency:1,only:[],skip:[]},n=!1;for(let r=0;r<e.length;r++){let i=e[r];if(i===void 0)break;if(i===`--concurrency`||i===`--only`||i===`--skip`){let n=e[++r];if(n===void 0)throw Error(`${i} requires a value`);if(i===`--concurrency`){if(t.concurrency=Number(n),!Number.isInteger(t.concurrency)||t.concurrency<=0)throw Error(`--concurrency requires a positive integer, but got ${n}`)}else i===`--only`?t.only.push(n):t.skip.push(n)}else if(i.startsWith(`--`))throw Error(`Unknown option: ${i}`);else if(n)throw Error(`Only one root directory can be specified, but got both ${t.rootDir} and ${i}`);else t.rootDir=i,n=!0}return t}async function v(e){let t=[];return await y(e,t),t.toSorted()}async function y(e,t){let n=await d.default.readdir(e,{withFileTypes:!0});if(n.some(e=>e.isFile()&&(e.name===`problem.md`||e.name.endsWith(`.problem.md`)))){t.push(e);return}for(let r of n)r.isDirectory()&&r.name!==`node_modules`&&!r.name.startsWith(`.`)&&await y(u.default.join(e,r.name),t)}async function b(e){try{return await d.default.stat(e),!0}catch{return!1}}function x(e){return e.length<=f?e:`${e.slice(0,f)}...`}exports.checkAllProblems=m;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("../types/testCaseResult.cjs"),n=require("../helpers/defaultStdioHarness.cjs"),r=require("../helpers/readTestCases.cjs"),i=require("../helpers/temporaryProblemDirCopy.cjs"),a=require("../helpers/readProblemMarkdownFrontMatter.cjs"),o=require("../types/decisionCode.cjs"),s=require("../helpers/runHarnessProcess.cjs"),c=require("../helpers/findModelAnswerDirs.cjs"),l=require("./runSingleHarness.cjs");let u=require("node:path");u=e.__toESM(u,1);let d=require("node:fs/promises");d=e.__toESM(d,1);let f=require("@willbooster/shared-lib");const p=1e3,m=new Map(Object.entries(o.DecisionCode).map(([e,t])=>[t,e]));async function h(e){let t=v(e),i=u.default.resolve(t.rootDir),o=e=>(u.default.relative(i,e)||`.`).replaceAll(u.default.sep,`/`),s=await y(i),d=s.filter(e=>{let n=o(e);return t.only.length>0&&!t.only.some(e=>n.includes(e))?!1:!t.skip.some(e=>n.includes(e))});if(d.length===0)return console.error(s.length===0?`No problem directories (containing problem.md or <id>.problem.md) found under ${i}.`:`All ${s.length} problem directories under ${i} were excluded by --only/--skip.`),1;let p=[],m=[];for(let e of d){let t=await n.findDefaultStdioHarnessFiles(e);if(t.length>0){p.push(`${o(e)}: ${l.formatDefaultHarnessError(t)}`);continue}let i=await c.findModelAnswerDirs(e);if(i.length===0){p.push(`${o(e)}: no model answers found under model_answers/`);continue}if(!await x(u.default.join(e,`judge.ts`))&&(await r.readTestCases(u.default.join(e,`test_cases`))).length===0){let t;try{t=await a.readProblemMarkdownFrontMatter(e)}catch(t){p.push(`${o(e)}: failed to read the problem markdown front matter: ${(0,f.getErrorMessage)(t)}`);continue}if(!a.judgesWithoutTestCases(t)){p.push(`${o(e)}: ${l.MISSING_TEST_CASES_ERROR}`);continue}}let s=await c.findFailingModelAnswerDirs(e);m.push(...i.map(t=>({problemDir:e,answerDir:t,expectation:`accepted`})),...s.map(t=>({problemDir:e,answerDir:t,expectation:`rejected`})))}for(let e of p)console.error(`✗ ${e}`);let h=u.default.resolve(process.argv[1]??``),_=0;return await(0,f.forEachConcurrently)(m,t.concurrency,async e=>{let t=`${o(e.problemDir)} ${u.default.relative(e.problemDir,e.answerDir).replaceAll(u.default.sep,`/`)}`,n=await g(e,h);n===void 0?(_++,console.info(`✓ ${t}`)):(p.push(`${t}: ${n}`),console.error(`✗ ${t}: ${n}`))}),console.info(`\n${_} passed, ${p.length} failed (${m.length} runs, ${d.length} problems)`),p.length===0?0:1}async function g(e,t){let n,r;try{({tempRoot:n,copiedProblemDir:r}=await i.copyProblemDirToTemporaryRoot(e.problemDir))}catch(e){return S(`failed to copy the problem directory to a temporary location: ${(0,f.getErrorMessage)(e)}`)}let a,o;try{a=_(e,await s.runHarnessProcess([`run`,t,`judge`,u.default.relative(e.problemDir,e.answerDir)],{cwd:r,env:process.env,timeoutMs:6e5,maxOutputBytes:67108864,tempRoot:n}))}catch(e){a=S(`harness execution failed: ${(0,f.getErrorMessage)(e)}`)}finally{o=await i.forciblyRemoveDirectory(n)}if(o)return a;let c=`failed to remove the temporary copy at ${n} (judged code may have left permission-locked files)`;return a===void 0?c:S(`${a}; ${c}`)}function _(e,n){let{stdout:r,stderr:i}=n;if(n.failureReason!==void 0)return S(n.failureReason);if(n.exitCode!==0)return S(`harness exited with ${n.exitCode??`a signal`}${i.trim()?`: ${i.trim()}`:``}`);let a=r.split(/\r?\n/).filter(e=>e.startsWith(t.TEST_CASE_RESULT_PREFIX)),s=[];for(let e of a){let n;try{n=t.testCaseResultSchema.safeParse(JSON.parse(e.slice(t.TEST_CASE_RESULT_PREFIX.length)))}catch{n=void 0}if(!n?.success)return S(`malformed test case result line: ${e}`);s.push(n.data)}if(s.length===0)return`no test case results were printed`;if(e.expectation===`accepted`){let e=s.find(e=>e.decisionCode!==o.DecisionCode.ACCEPTED);return e?S(`${m.get(e.decisionCode)??e.decisionCode} on test case ${e.testCaseId}${e.stderr?.trim()?`: ${e.stderr.trim()}`:``}`):void 0}return s.every(e=>e.decisionCode===o.DecisionCode.ACCEPTED)?`expected at least one failing test case, but all test cases were accepted`:void 0}function v(e){let t={rootDir:`.`,concurrency:1,only:[],skip:[]},n=!1;for(let r=0;r<e.length;r++){let i=e[r];if(i===void 0)break;if(i===`--concurrency`||i===`--only`||i===`--skip`){let n=e[++r];if(n===void 0)throw Error(`${i} requires a value`);if(i===`--concurrency`){if(t.concurrency=Number(n),!Number.isInteger(t.concurrency)||t.concurrency<=0)throw Error(`--concurrency requires a positive integer, but got ${n}`)}else i===`--only`?t.only.push(n):t.skip.push(n)}else if(i.startsWith(`--`))throw Error(`Unknown option: ${i}`);else if(n)throw Error(`Only one root directory can be specified, but got both ${t.rootDir} and ${i}`);else t.rootDir=i,n=!0}return t}async function y(e){let t=[];return await b(e,t),t.toSorted()}async function b(e,t){let n=await d.default.readdir(e,{withFileTypes:!0});if(n.some(e=>e.isFile()&&(e.name===`problem.md`||e.name.endsWith(`.problem.md`)))){t.push(e);return}for(let r of n)r.isDirectory()&&r.name!==`node_modules`&&!r.name.startsWith(`.`)&&await b(u.default.join(e,r.name),t)}async function x(e){try{return await d.default.stat(e),!0}catch{return!1}}function S(e){return e.length<=p?e:`${e.slice(0,p)}...`}exports.checkAllProblems=h;
2
2
  //# sourceMappingURL=checkAllProblems.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"checkAllProblems.cjs","names":["DecisionCode","path","findDefaultStdioHarnessFiles","formatDefaultHarnessError","findModelAnswerDirs","readTestCases","readProblemMarkdownFrontMatter","judgesWithoutTestCases","MISSING_TEST_CASES_ERROR","findFailingModelAnswerDirs","copyProblemDirToTemporaryRoot","runHarnessProcess","forciblyRemoveDirectory","TEST_CASE_RESULT_PREFIX","testCaseResultSchema","fs"],"sources":["../../src/cli/checkAllProblems.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport { findDefaultStdioHarnessFiles } from '../helpers/defaultStdioHarness.js';\nimport { findFailingModelAnswerDirs, findModelAnswerDirs } from '../helpers/findModelAnswerDirs.js';\nimport { judgesWithoutTestCases, readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { readTestCases } from '../helpers/readTestCases.js';\nimport { type HarnessProcessResult, runHarnessProcess } from '../helpers/runHarnessProcess.js';\nimport { copyProblemDirToTemporaryRoot, forciblyRemoveDirectory } from '../helpers/temporaryProblemDirCopy.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport { TEST_CASE_RESULT_PREFIX, testCaseResultSchema } from '../types/testCaseResult.js';\n\nimport { formatDefaultHarnessError, MISSING_TEST_CASES_ERROR } from './runSingleHarness.js';\n\nconst RUN_TIMEOUT_MS = 600_000;\nconst MAX_RUN_OUTPUT_BYTES = 64 * 1024 * 1024;\nconst MAX_FAILURE_DETAIL_LENGTH = 1000;\n\n// DecisionCode is a plain const object (not a TypeScript enum), so it has no reverse mapping.\nconst decisionCodeNames = new Map<number, string>(Object.entries(DecisionCode).map(([name, code]) => [code, name]));\n\ninterface CheckOptions {\n rootDir: string;\n concurrency: number;\n only: string[];\n skip: string[];\n}\n\ninterface CheckRun {\n problemDir: string;\n answerDir: string;\n /** Whether all test cases must be accepted (`model_answers`) or at least one must fail (`model_answers.fails`). */\n expectation: 'accepted' | 'rejected';\n}\n\n/**\n * Judge all model answers of all problems (directories containing `problem.md` or\n * `<id>.problem.md`) under a root directory: `model_answers/*` must be fully accepted and\n * `model_answers.fails/*` must fail at least one test case. Returns the process exit code.\n */\nexport async function checkAllProblems(args: readonly string[]): Promise<number> {\n const options = parseCheckArgs(args);\n const rootDir = path.resolve(options.rootDir);\n // Normalize to forward slashes so --only / --skip substrings like courses/foo match on Windows.\n const toRelative = (dir: string): string => (path.relative(rootDir, dir) || '.').replaceAll(path.sep, '/');\n\n const allProblemDirs = await findProblemDirs(rootDir);\n const problemDirs = allProblemDirs.filter((problemDir) => {\n const relativeDir = toRelative(problemDir);\n if (options.only.length > 0 && !options.only.some((substring) => relativeDir.includes(substring))) return false;\n return !options.skip.some((substring) => relativeDir.includes(substring));\n });\n if (problemDirs.length === 0) {\n console.error(\n allProblemDirs.length === 0\n ? `No problem directories (containing problem.md or <id>.problem.md) found under ${rootDir}.`\n : `All ${allProblemDirs.length} problem directories under ${rootDir} were excluded by --only/--skip.`\n );\n return 1;\n }\n\n const failures: string[] = [];\n const runs: CheckRun[] = [];\n for (const problemDir of problemDirs) {\n const defaultHarnessFileNames = await findDefaultStdioHarnessFiles(problemDir);\n if (defaultHarnessFileNames.length > 0) {\n failures.push(`${toRelative(problemDir)}: ${formatDefaultHarnessError(defaultHarnessFileNames)}`);\n continue;\n }\n\n const modelAnswerDirs = await findModelAnswerDirs(problemDir);\n if (modelAnswerDirs.length === 0) {\n failures.push(`${toRelative(problemDir)}: no model answers found under model_answers/`);\n continue;\n }\n\n // Without test cases, stdioJudgePreset prints a single accepted sentinel result, so a standard\n // problem with an empty or missing test_cases/ would otherwise pass without being judged —\n // unless static-analysis rules or manual scoring make the problem judgeable without them.\n if (!(await fileExists(path.join(problemDir, 'judge.ts')))) {\n const testCases = await readTestCases(path.join(problemDir, 'test_cases'));\n if (testCases.length === 0) {\n let frontMatter;\n try {\n frontMatter = await readProblemMarkdownFrontMatter(problemDir);\n } catch (error) {\n failures.push(\n `${toRelative(problemDir)}: failed to read the problem markdown front matter: ${error instanceof Error ? error.message : String(error)}`\n );\n continue;\n }\n if (!judgesWithoutTestCases(frontMatter)) {\n failures.push(`${toRelative(problemDir)}: ${MISSING_TEST_CASES_ERROR}`);\n continue;\n }\n }\n }\n\n const failingModelAnswerDirs = await findFailingModelAnswerDirs(problemDir);\n runs.push(\n ...modelAnswerDirs.map((answerDir): CheckRun => ({ problemDir, answerDir, expectation: 'accepted' })),\n ...failingModelAnswerDirs.map((answerDir): CheckRun => ({ problemDir, answerDir, expectation: 'rejected' }))\n );\n }\n for (const failure of failures) console.error(`✗ ${failure}`);\n\n const cliEntryPath = path.resolve(process.argv[1] ?? '');\n let passedCount = 0;\n let nextRunIndex = 0;\n await Promise.all(\n Array.from({ length: Math.max(1, Math.min(options.concurrency, runs.length)) }, async () => {\n while (nextRunIndex < runs.length) {\n const run = runs[nextRunIndex++];\n if (!run) return;\n const label = `${toRelative(run.problemDir)} ${path.relative(run.problemDir, run.answerDir).replaceAll(path.sep, '/')}`;\n const failureDetail = await executeCheckRun(run, cliEntryPath);\n if (failureDetail === undefined) {\n passedCount++;\n console.info(`✓ ${label}`);\n } else {\n failures.push(`${label}: ${failureDetail}`);\n console.error(`✗ ${label}: ${failureDetail}`);\n }\n }\n })\n );\n\n console.info(\n `\\n${passedCount} passed, ${failures.length} failed (${runs.length} runs, ${problemDirs.length} problems)`\n );\n return failures.length === 0 ? 0 : 1;\n}\n\n/**\n * Run one answer directory through the problem's harness and return a failure detail, if any.\n * The problem directory is copied to a temporary location first so that judging (e.g. build\n * artifacts in answer directories) never modifies the checked repository.\n */\nasync function executeCheckRun(run: CheckRun, cliEntryPath: string): Promise<string | undefined> {\n let tempRoot: string;\n let copiedProblemDir: string;\n try {\n ({ tempRoot, copiedProblemDir } = await copyProblemDirToTemporaryRoot(run.problemDir));\n } catch (error) {\n return truncate(\n `failed to copy the problem directory to a temporary location: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n\n let harnessFailureDetail;\n let removedTempRoot;\n try {\n const result = await runHarnessProcess(\n ['run', cliEntryPath, 'judge', path.relative(run.problemDir, run.answerDir)],\n {\n cwd: copiedProblemDir,\n env: process.env,\n timeoutMs: RUN_TIMEOUT_MS,\n maxOutputBytes: MAX_RUN_OUTPUT_BYTES,\n tempRoot,\n }\n );\n harnessFailureDetail = summarizeHarnessFailure(run, result);\n } catch (error) {\n harnessFailureDetail = truncate(\n `harness execution failed: ${error instanceof Error ? error.message : String(error)}`\n );\n } finally {\n removedTempRoot = await forciblyRemoveDirectory(tempRoot);\n }\n if (removedTempRoot) return harnessFailureDetail;\n const removalFailureDetail = `failed to remove the temporary copy at ${tempRoot} (judged code may have left permission-locked files)`;\n return harnessFailureDetail === undefined\n ? removalFailureDetail\n : truncate(`${harnessFailureDetail}; ${removalFailureDetail}`);\n}\n\n/** Return why the harness run failed the check, or `undefined` when it passed. */\nfunction summarizeHarnessFailure(run: CheckRun, result: HarnessProcessResult): string | undefined {\n const { stdout, stderr } = result;\n if (result.failureReason !== undefined) return truncate(result.failureReason);\n if (result.exitCode !== 0) {\n return truncate(`harness exited with ${result.exitCode ?? 'a signal'}${stderr.trim() ? `: ${stderr.trim()}` : ''}`);\n }\n\n const resultLines = stdout.split(/\\r?\\n/).filter((line) => line.startsWith(TEST_CASE_RESULT_PREFIX));\n const testCaseResults = [];\n for (const line of resultLines) {\n let parsedResult;\n try {\n parsedResult = testCaseResultSchema.safeParse(JSON.parse(line.slice(TEST_CASE_RESULT_PREFIX.length)));\n } catch {\n parsedResult = undefined;\n }\n if (!parsedResult?.success) return truncate(`malformed test case result line: ${line}`);\n testCaseResults.push(parsedResult.data);\n }\n if (testCaseResults.length === 0) return 'no test case results were printed';\n\n if (run.expectation === 'accepted') {\n const rejectedResult = testCaseResults.find((result) => result.decisionCode !== DecisionCode.ACCEPTED);\n if (rejectedResult) {\n return truncate(\n `${decisionCodeNames.get(rejectedResult.decisionCode) ?? rejectedResult.decisionCode} on test case ${rejectedResult.testCaseId}${rejectedResult.stderr?.trim() ? `: ${rejectedResult.stderr.trim()}` : ''}`\n );\n }\n return undefined;\n }\n return testCaseResults.every((result) => result.decisionCode === DecisionCode.ACCEPTED)\n ? 'expected at least one failing test case, but all test cases were accepted'\n : undefined;\n}\n\nfunction parseCheckArgs(args: readonly string[]): CheckOptions {\n const options: CheckOptions = {\n rootDir: '.',\n // Serial by default: judging decides TIME_LIMIT_EXCEEDED from wall-clock time, so parallel\n // runs on a small CI runner could fail timing-sensitive problems non-deterministically.\n concurrency: 1,\n only: [],\n skip: [],\n };\n let hasRootDir = false;\n for (let index = 0; index < args.length; index++) {\n const arg = args[index];\n if (arg === undefined) break;\n if (arg === '--concurrency' || arg === '--only' || arg === '--skip') {\n const value = args[++index];\n if (value === undefined) throw new Error(`${arg} requires a value`);\n if (arg === '--concurrency') {\n options.concurrency = Number(value);\n if (!Number.isInteger(options.concurrency) || options.concurrency <= 0) {\n throw new Error(`--concurrency requires a positive integer, but got ${value}`);\n }\n } else if (arg === '--only') {\n options.only.push(value);\n } else {\n options.skip.push(value);\n }\n } else if (arg.startsWith('--')) {\n throw new Error(`Unknown option: ${arg}`);\n } else if (hasRootDir) {\n throw new Error(`Only one root directory can be specified, but got both ${options.rootDir} and ${arg}`);\n } else {\n options.rootDir = arg;\n hasRootDir = true;\n }\n }\n return options;\n}\n\n/** Find directories containing `problem.md` or `<id>.problem.md`, without descending into found problems. */\nasync function findProblemDirs(rootDir: string): Promise<string[]> {\n const problemDirs: string[] = [];\n await visitDirectory(rootDir, problemDirs);\n return problemDirs.toSorted();\n}\n\nasync function visitDirectory(dir: string, problemDirs: string[]): Promise<void> {\n // Traversal errors (e.g. an unreadable subtree) must fail the check: skipping them silently\n // could report a green result while covering only part of the repository.\n const entries = await fs.readdir(dir, { withFileTypes: true });\n if (entries.some((entry) => entry.isFile() && (entry.name === 'problem.md' || entry.name.endsWith('.problem.md')))) {\n problemDirs.push(dir);\n return;\n }\n for (const entry of entries) {\n if (!entry.isDirectory() || entry.name === 'node_modules' || entry.name.startsWith('.')) continue;\n await visitDirectory(path.join(dir, entry.name), problemDirs);\n }\n}\n\nasync function fileExists(filePath: string): Promise<boolean> {\n try {\n await fs.stat(filePath);\n return true;\n } catch {\n return false;\n }\n}\n\nfunction truncate(text: string): string {\n return text.length <= MAX_FAILURE_DETAIL_LENGTH ? text : `${text.slice(0, MAX_FAILURE_DETAIL_LENGTH)}...`;\n}\n"],"mappings":"+nBAcA,MAEM,EAA4B,IAG5B,EAAoB,IAAI,IAAoB,OAAO,QAAQA,EAAAA,YAAY,CAAC,CAAC,KAAK,CAAC,EAAM,KAAU,CAAC,EAAM,CAAI,CAAC,CAAC,EAqBlH,eAAsB,EAAiB,EAA0C,CAC/E,IAAM,EAAU,EAAe,CAAI,EAC7B,EAAUC,EAAAA,QAAK,QAAQ,EAAQ,OAAO,EAEtC,EAAc,IAAyBA,EAAAA,QAAK,SAAS,EAAS,CAAG,GAAK,IAAA,CAAK,WAAWA,EAAAA,QAAK,IAAK,GAAG,EAEnG,EAAiB,MAAM,EAAgB,CAAO,EAC9C,EAAc,EAAe,OAAQ,GAAe,CACxD,IAAM,EAAc,EAAW,CAAU,EAEzC,OADI,EAAQ,KAAK,OAAS,GAAK,CAAC,EAAQ,KAAK,KAAM,GAAc,EAAY,SAAS,CAAS,CAAC,EAAU,GACnG,CAAC,EAAQ,KAAK,KAAM,GAAc,EAAY,SAAS,CAAS,CAAC,CAC1E,CAAC,EACD,GAAI,EAAY,SAAW,EAMzB,OALA,QAAQ,MACN,EAAe,SAAW,EACtB,iFAAiF,EAAQ,GACzF,OAAO,EAAe,OAAO,6BAA6B,EAAQ,iCACxE,EACO,EAGT,IAAM,EAAqB,CAAC,EACtB,EAAmB,CAAC,EAC1B,IAAK,IAAM,KAAc,EAAa,CACpC,IAAM,EAA0B,MAAMC,EAAAA,6BAA6B,CAAU,EAC7E,GAAI,EAAwB,OAAS,EAAG,CACtC,EAAS,KAAK,GAAG,EAAW,CAAU,EAAE,IAAIC,EAAAA,0BAA0B,CAAuB,GAAG,EAChG,QACF,CAEA,IAAM,EAAkB,MAAMC,EAAAA,oBAAoB,CAA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
1
+ {"version":3,"file":"checkAllProblems.cjs","names":["DecisionCode","path","findDefaultStdioHarnessFiles","formatDefaultHarnessError","findModelAnswerDirs","readTestCases","readProblemMarkdownFrontMatter","getErrorMessage","judgesWithoutTestCases","MISSING_TEST_CASES_ERROR","findFailingModelAnswerDirs","forEachConcurrently","copyProblemDirToTemporaryRoot","runHarnessProcess","forciblyRemoveDirectory","TEST_CASE_RESULT_PREFIX","testCaseResultSchema","fs"],"sources":["../../src/cli/checkAllProblems.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\nimport { forEachConcurrently, getErrorMessage } from '@willbooster/shared-lib';\n\nimport { findDefaultStdioHarnessFiles } from '../helpers/defaultStdioHarness.js';\nimport { findFailingModelAnswerDirs, findModelAnswerDirs } from '../helpers/findModelAnswerDirs.js';\nimport { judgesWithoutTestCases, readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { readTestCases } from '../helpers/readTestCases.js';\nimport { type HarnessProcessResult, runHarnessProcess } from '../helpers/runHarnessProcess.js';\nimport { copyProblemDirToTemporaryRoot, forciblyRemoveDirectory } from '../helpers/temporaryProblemDirCopy.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport { TEST_CASE_RESULT_PREFIX, testCaseResultSchema } from '../types/testCaseResult.js';\n\nimport { formatDefaultHarnessError, MISSING_TEST_CASES_ERROR } from './runSingleHarness.js';\n\nconst RUN_TIMEOUT_MS = 600_000;\nconst MAX_RUN_OUTPUT_BYTES = 64 * 1024 * 1024;\nconst MAX_FAILURE_DETAIL_LENGTH = 1000;\n\n// DecisionCode is a plain const object (not a TypeScript enum), so it has no reverse mapping.\nconst decisionCodeNames = new Map<number, string>(Object.entries(DecisionCode).map(([name, code]) => [code, name]));\n\ninterface CheckOptions {\n rootDir: string;\n concurrency: number;\n only: string[];\n skip: string[];\n}\n\ninterface CheckRun {\n problemDir: string;\n answerDir: string;\n /** Whether all test cases must be accepted (`model_answers`) or at least one must fail (`model_answers.fails`). */\n expectation: 'accepted' | 'rejected';\n}\n\n/**\n * Judge all model answers of all problems (directories containing `problem.md` or\n * `<id>.problem.md`) under a root directory: `model_answers/*` must be fully accepted and\n * `model_answers.fails/*` must fail at least one test case. Returns the process exit code.\n */\nexport async function checkAllProblems(args: readonly string[]): Promise<number> {\n const options = parseCheckArgs(args);\n const rootDir = path.resolve(options.rootDir);\n // Normalize to forward slashes so --only / --skip substrings like courses/foo match on Windows.\n const toRelative = (dir: string): string => (path.relative(rootDir, dir) || '.').replaceAll(path.sep, '/');\n\n const allProblemDirs = await findProblemDirs(rootDir);\n const problemDirs = allProblemDirs.filter((problemDir) => {\n const relativeDir = toRelative(problemDir);\n if (options.only.length > 0 && !options.only.some((substring) => relativeDir.includes(substring))) return false;\n return !options.skip.some((substring) => relativeDir.includes(substring));\n });\n if (problemDirs.length === 0) {\n console.error(\n allProblemDirs.length === 0\n ? `No problem directories (containing problem.md or <id>.problem.md) found under ${rootDir}.`\n : `All ${allProblemDirs.length} problem directories under ${rootDir} were excluded by --only/--skip.`\n );\n return 1;\n }\n\n const failures: string[] = [];\n const runs: CheckRun[] = [];\n for (const problemDir of problemDirs) {\n const defaultHarnessFileNames = await findDefaultStdioHarnessFiles(problemDir);\n if (defaultHarnessFileNames.length > 0) {\n failures.push(`${toRelative(problemDir)}: ${formatDefaultHarnessError(defaultHarnessFileNames)}`);\n continue;\n }\n\n const modelAnswerDirs = await findModelAnswerDirs(problemDir);\n if (modelAnswerDirs.length === 0) {\n failures.push(`${toRelative(problemDir)}: no model answers found under model_answers/`);\n continue;\n }\n\n // Without test cases, stdioJudgePreset prints a single accepted sentinel result, so a standard\n // problem with an empty or missing test_cases/ would otherwise pass without being judged —\n // unless static-analysis rules or manual scoring make the problem judgeable without them.\n if (!(await fileExists(path.join(problemDir, 'judge.ts')))) {\n const testCases = await readTestCases(path.join(problemDir, 'test_cases'));\n if (testCases.length === 0) {\n let frontMatter;\n try {\n frontMatter = await readProblemMarkdownFrontMatter(problemDir);\n } catch (error) {\n failures.push(\n `${toRelative(problemDir)}: failed to read the problem markdown front matter: ${getErrorMessage(error)}`\n );\n continue;\n }\n if (!judgesWithoutTestCases(frontMatter)) {\n failures.push(`${toRelative(problemDir)}: ${MISSING_TEST_CASES_ERROR}`);\n continue;\n }\n }\n }\n\n const failingModelAnswerDirs = await findFailingModelAnswerDirs(problemDir);\n runs.push(\n ...modelAnswerDirs.map((answerDir): CheckRun => ({ problemDir, answerDir, expectation: 'accepted' })),\n ...failingModelAnswerDirs.map((answerDir): CheckRun => ({ problemDir, answerDir, expectation: 'rejected' }))\n );\n }\n for (const failure of failures) console.error(`✗ ${failure}`);\n\n const cliEntryPath = path.resolve(process.argv[1] ?? '');\n let passedCount = 0;\n await forEachConcurrently(runs, options.concurrency, async (run) => {\n const label = `${toRelative(run.problemDir)} ${path.relative(run.problemDir, run.answerDir).replaceAll(path.sep, '/')}`;\n const failureDetail = await executeCheckRun(run, cliEntryPath);\n if (failureDetail === undefined) {\n passedCount++;\n console.info(`✓ ${label}`);\n } else {\n failures.push(`${label}: ${failureDetail}`);\n console.error(`✗ ${label}: ${failureDetail}`);\n }\n });\n\n console.info(\n `\\n${passedCount} passed, ${failures.length} failed (${runs.length} runs, ${problemDirs.length} problems)`\n );\n return failures.length === 0 ? 0 : 1;\n}\n\n/**\n * Run one answer directory through the problem's harness and return a failure detail, if any.\n * The problem directory is copied to a temporary location first so that judging (e.g. build\n * artifacts in answer directories) never modifies the checked repository.\n */\nasync function executeCheckRun(run: CheckRun, cliEntryPath: string): Promise<string | undefined> {\n let tempRoot: string;\n let copiedProblemDir: string;\n try {\n ({ tempRoot, copiedProblemDir } = await copyProblemDirToTemporaryRoot(run.problemDir));\n } catch (error) {\n return truncate(`failed to copy the problem directory to a temporary location: ${getErrorMessage(error)}`);\n }\n\n let harnessFailureDetail;\n let removedTempRoot;\n try {\n const result = await runHarnessProcess(\n ['run', cliEntryPath, 'judge', path.relative(run.problemDir, run.answerDir)],\n {\n cwd: copiedProblemDir,\n env: process.env,\n timeoutMs: RUN_TIMEOUT_MS,\n maxOutputBytes: MAX_RUN_OUTPUT_BYTES,\n tempRoot,\n }\n );\n harnessFailureDetail = summarizeHarnessFailure(run, result);\n } catch (error) {\n harnessFailureDetail = truncate(`harness execution failed: ${getErrorMessage(error)}`);\n } finally {\n removedTempRoot = await forciblyRemoveDirectory(tempRoot);\n }\n if (removedTempRoot) return harnessFailureDetail;\n const removalFailureDetail = `failed to remove the temporary copy at ${tempRoot} (judged code may have left permission-locked files)`;\n return harnessFailureDetail === undefined\n ? removalFailureDetail\n : truncate(`${harnessFailureDetail}; ${removalFailureDetail}`);\n}\n\n/** Return why the harness run failed the check, or `undefined` when it passed. */\nfunction summarizeHarnessFailure(run: CheckRun, result: HarnessProcessResult): string | undefined {\n const { stdout, stderr } = result;\n if (result.failureReason !== undefined) return truncate(result.failureReason);\n if (result.exitCode !== 0) {\n return truncate(`harness exited with ${result.exitCode ?? 'a signal'}${stderr.trim() ? `: ${stderr.trim()}` : ''}`);\n }\n\n const resultLines = stdout.split(/\\r?\\n/).filter((line) => line.startsWith(TEST_CASE_RESULT_PREFIX));\n const testCaseResults = [];\n for (const line of resultLines) {\n let parsedResult;\n try {\n parsedResult = testCaseResultSchema.safeParse(JSON.parse(line.slice(TEST_CASE_RESULT_PREFIX.length)));\n } catch {\n parsedResult = undefined;\n }\n if (!parsedResult?.success) return truncate(`malformed test case result line: ${line}`);\n testCaseResults.push(parsedResult.data);\n }\n if (testCaseResults.length === 0) return 'no test case results were printed';\n\n if (run.expectation === 'accepted') {\n const rejectedResult = testCaseResults.find((result) => result.decisionCode !== DecisionCode.ACCEPTED);\n if (rejectedResult) {\n return truncate(\n `${decisionCodeNames.get(rejectedResult.decisionCode) ?? rejectedResult.decisionCode} on test case ${rejectedResult.testCaseId}${rejectedResult.stderr?.trim() ? `: ${rejectedResult.stderr.trim()}` : ''}`\n );\n }\n return undefined;\n }\n return testCaseResults.every((result) => result.decisionCode === DecisionCode.ACCEPTED)\n ? 'expected at least one failing test case, but all test cases were accepted'\n : undefined;\n}\n\nfunction parseCheckArgs(args: readonly string[]): CheckOptions {\n const options: CheckOptions = {\n rootDir: '.',\n // Serial by default: judging decides TIME_LIMIT_EXCEEDED from wall-clock time, so parallel\n // runs on a small CI runner could fail timing-sensitive problems non-deterministically.\n concurrency: 1,\n only: [],\n skip: [],\n };\n let hasRootDir = false;\n for (let index = 0; index < args.length; index++) {\n const arg = args[index];\n if (arg === undefined) break;\n if (arg === '--concurrency' || arg === '--only' || arg === '--skip') {\n const value = args[++index];\n if (value === undefined) throw new Error(`${arg} requires a value`);\n if (arg === '--concurrency') {\n options.concurrency = Number(value);\n if (!Number.isInteger(options.concurrency) || options.concurrency <= 0) {\n throw new Error(`--concurrency requires a positive integer, but got ${value}`);\n }\n } else if (arg === '--only') {\n options.only.push(value);\n } else {\n options.skip.push(value);\n }\n } else if (arg.startsWith('--')) {\n throw new Error(`Unknown option: ${arg}`);\n } else if (hasRootDir) {\n throw new Error(`Only one root directory can be specified, but got both ${options.rootDir} and ${arg}`);\n } else {\n options.rootDir = arg;\n hasRootDir = true;\n }\n }\n return options;\n}\n\n/** Find directories containing `problem.md` or `<id>.problem.md`, without descending into found problems. */\nasync function findProblemDirs(rootDir: string): Promise<string[]> {\n const problemDirs: string[] = [];\n await visitDirectory(rootDir, problemDirs);\n return problemDirs.toSorted();\n}\n\nasync function visitDirectory(dir: string, problemDirs: string[]): Promise<void> {\n // Traversal errors (e.g. an unreadable subtree) must fail the check: skipping them silently\n // could report a green result while covering only part of the repository.\n const entries = await fs.readdir(dir, { withFileTypes: true });\n if (entries.some((entry) => entry.isFile() && (entry.name === 'problem.md' || entry.name.endsWith('.problem.md')))) {\n problemDirs.push(dir);\n return;\n }\n for (const entry of entries) {\n if (!entry.isDirectory() || entry.name === 'node_modules' || entry.name.startsWith('.')) continue;\n await visitDirectory(path.join(dir, entry.name), problemDirs);\n }\n}\n\nasync function fileExists(filePath: string): Promise<boolean> {\n try {\n await fs.stat(filePath);\n return true;\n } catch {\n return false;\n }\n}\n\nfunction truncate(text: string): string {\n return text.length <= MAX_FAILURE_DETAIL_LENGTH ? text : `${text.slice(0, MAX_FAILURE_DETAIL_LENGTH)}...`;\n}\n"],"mappings":"wqBAeA,MAEM,EAA4B,IAG5B,EAAoB,IAAI,IAAoB,OAAO,QAAQA,EAAAA,YAAY,CAAC,CAAC,KAAK,CAAC,EAAM,KAAU,CAAC,EAAM,CAAI,CAAC,CAAC,EAqBlH,eAAsB,EAAiB,EAA0C,CAC/E,IAAM,EAAU,EAAe,CAAI,EAC7B,EAAUC,EAAAA,QAAK,QAAQ,EAAQ,OAAO,EAEtC,EAAc,IAAyBA,EAAAA,QAAK,SAAS,EAAS,CAAG,GAAK,IAAA,CAAK,WAAWA,EAAAA,QAAK,IAAK,GAAG,EAEnG,EAAiB,MAAM,EAAgB,CAAO,EAC9C,EAAc,EAAe,OAAQ,GAAe,CACxD,IAAM,EAAc,EAAW,CAAU,EAEzC,OADI,EAAQ,KAAK,OAAS,GAAK,CAAC,EAAQ,KAAK,KAAM,GAAc,EAAY,SAAS,CAAS,CAAC,EAAU,GACnG,CAAC,EAAQ,KAAK,KAAM,GAAc,EAAY,SAAS,CAAS,CAAC,CAC1E,CAAC,EACD,GAAI,EAAY,SAAW,EAMzB,OALA,QAAQ,MACN,EAAe,SAAW,EACtB,iFAAiF,EAAQ,GACzF,OAAO,EAAe,OAAO,6BAA6B,EAAQ,iCACxE,EACO,EAGT,IAAM,EAAqB,CAAC,EACtB,EAAmB,CAAC,EAC1B,IAAK,IAAM,KAAc,EAAa,CACpC,IAAM,EAA0B,MAAMC,EAAAA,6BAA6B,CAAU,EAC7E,GAAI,EAAwB,OAAS,EAAG,CACtC,EAAS,KAAK,GAAG,EAAW,CAAU,EAAE,IAAIC,EAAAA,0BAA0B,CAAuB,GAAG,EAChG,QACF,CAEA,IAAM,EAAkB,MAAMC,EAAAA,oBAAoB,CAAU,EAC5D,GAAI,EA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,MAAU,MAAM,sDAAsD,GAAO,CAAA,MAEtE,IAAQ,SACjB,EAAQ,KAAK,KAAK,CAAK,EAEvB,EAAQ,KAAK,KAAK,CAAK,CAE3B,MAAO,GAAI,EAAI,WAAW,IAAI,EAC5B,MAAU,MAAM,mBAAmB,GAAK,OACnC,GAAI,EACT,MAAU,MAAM,0DAA0D,EAAQ,QAAQ,OAAO,GAAK,MAEtG,GAAQ,QAAU,EAClB,EAAa,EAEjB,CACA,OAAO,CACT,CAGA,eAAe,EAAgB,EAAoC,CACjE,IAAM,EAAwB,CAAC,EAE/B,OADA,MAAM,EAAe,EAAS,CAAW,EAClC,EAAY,SAAS,CAC9B,CAEA,eAAe,EAAe,EAAa,EAAsC,CAG/E,IAAM,EAAU,MAAMiB,EAAAA,QAAG,QAAQ,EAAK,CAAE,cAAe,EAAK,CAAC,EAC7D,GAAI,EAAQ,KAAM,GAAU,EAAM,OAAO,IAAM,EAAM,OAAS,cAAgB,EAAM,KAAK,SAAS,aAAa,EAAE,EAAG,CAClH,EAAY,KAAK,CAAG,EACpB,MACF,CACA,IAAK,IAAM,KAAS,EACb,EAAM,YAAY,GAAK,EAAM,OAAS,gBAAkB,GAAM,KAAK,WAAW,GAAG,GACtF,MAAM,EAAehB,EAAAA,QAAK,KAAK,EAAK,EAAM,IAAI,EAAG,CAAW,CAEhE,CAEA,eAAe,EAAW,EAAoC,CAC5D,GAAI,CAEF,OADA,MAAMgB,EAAAA,QAAG,KAAK,CAAQ,EACf,EACT,MAAQ,CACN,MAAO,EACT,CACF,CAEA,SAAS,EAAS,EAAsB,CACtC,OAAO,EAAK,QAAU,EAA4B,EAAO,GAAG,EAAK,MAAM,EAAG,CAAyB,EAAE,IACvG"}
@@ -1,2 +1,2 @@
1
- import{TEST_CASE_RESULT_PREFIX as e,testCaseResultSchema as t}from"../types/testCaseResult.js";import{findDefaultStdioHarnessFiles as n}from"../helpers/defaultStdioHarness.js";import{readTestCases as r}from"../helpers/readTestCases.js";import{copyProblemDirToTemporaryRoot as i,forciblyRemoveDirectory as a}from"../helpers/temporaryProblemDirCopy.js";import{judgesWithoutTestCases as o,readProblemMarkdownFrontMatter as s}from"../helpers/readProblemMarkdownFrontMatter.js";import{DecisionCode as c}from"../types/decisionCode.js";import{runHarnessProcess as l}from"../helpers/runHarnessProcess.js";import{findFailingModelAnswerDirs as u,findModelAnswerDirs as d}from"../helpers/findModelAnswerDirs.js";import{MISSING_TEST_CASES_ERROR as f,formatDefaultHarnessError as p}from"./runSingleHarness.js";import m from"node:path";import h from"node:fs/promises";const g=1e3,_=new Map(Object.entries(c).map(([e,t])=>[t,e]));async function v(e){let t=x(e),i=m.resolve(t.rootDir),a=e=>(m.relative(i,e)||`.`).replaceAll(m.sep,`/`),c=await S(i),l=c.filter(e=>{let n=a(e);return t.only.length>0&&!t.only.some(e=>n.includes(e))?!1:!t.skip.some(e=>n.includes(e))});if(l.length===0)return console.error(c.length===0?`No problem directories (containing problem.md or <id>.problem.md) found under ${i}.`:`All ${c.length} problem directories under ${i} were excluded by --only/--skip.`),1;let h=[],g=[];for(let e of l){let t=await n(e);if(t.length>0){h.push(`${a(e)}: ${p(t)}`);continue}let i=await d(e);if(i.length===0){h.push(`${a(e)}: no model answers found under model_answers/`);continue}if(!await w(m.join(e,`judge.ts`))&&(await r(m.join(e,`test_cases`))).length===0){let t;try{t=await s(e)}catch(t){h.push(`${a(e)}: failed to read the problem markdown front matter: ${t instanceof Error?t.message:String(t)}`);continue}if(!o(t)){h.push(`${a(e)}: ${f}`);continue}}let c=await u(e);g.push(...i.map(t=>({problemDir:e,answerDir:t,expectation:`accepted`})),...c.map(t=>({problemDir:e,answerDir:t,expectation:`rejected`})))}for(let e of h)console.error(`✗ ${e}`);let _=m.resolve(process.argv[1]??``),v=0,b=0;return await Promise.all(Array.from({length:Math.max(1,Math.min(t.concurrency,g.length))},async()=>{for(;b<g.length;){let e=g[b++];if(!e)return;let t=`${a(e.problemDir)} ${m.relative(e.problemDir,e.answerDir).replaceAll(m.sep,`/`)}`,n=await y(e,_);n===void 0?(v++,console.info(`✓ ${t}`)):(h.push(`${t}: ${n}`),console.error(`✗ ${t}: ${n}`))}})),console.info(`\n${v} passed, ${h.length} failed (${g.length} runs, ${l.length} problems)`),h.length===0?0:1}async function y(e,t){let n,r;try{({tempRoot:n,copiedProblemDir:r}=await i(e.problemDir))}catch(e){return T(`failed to copy the problem directory to a temporary location: ${e instanceof Error?e.message:String(e)}`)}let o,s;try{o=b(e,await l([`run`,t,`judge`,m.relative(e.problemDir,e.answerDir)],{cwd:r,env:process.env,timeoutMs:6e5,maxOutputBytes:67108864,tempRoot:n}))}catch(e){o=T(`harness execution failed: ${e instanceof Error?e.message:String(e)}`)}finally{s=await a(n)}if(s)return o;let c=`failed to remove the temporary copy at ${n} (judged code may have left permission-locked files)`;return o===void 0?c:T(`${o}; ${c}`)}function b(n,r){let{stdout:i,stderr:a}=r;if(r.failureReason!==void 0)return T(r.failureReason);if(r.exitCode!==0)return T(`harness exited with ${r.exitCode??`a signal`}${a.trim()?`: ${a.trim()}`:``}`);let o=i.split(/\r?\n/).filter(t=>t.startsWith(e)),s=[];for(let n of o){let r;try{r=t.safeParse(JSON.parse(n.slice(e.length)))}catch{r=void 0}if(!r?.success)return T(`malformed test case result line: ${n}`);s.push(r.data)}if(s.length===0)return`no test case results were printed`;if(n.expectation===`accepted`){let e=s.find(e=>e.decisionCode!==c.ACCEPTED);return e?T(`${_.get(e.decisionCode)??e.decisionCode} on test case ${e.testCaseId}${e.stderr?.trim()?`: ${e.stderr.trim()}`:``}`):void 0}return s.every(e=>e.decisionCode===c.ACCEPTED)?`expected at least one failing test case, but all test cases were accepted`:void 0}function x(e){let t={rootDir:`.`,concurrency:1,only:[],skip:[]},n=!1;for(let r=0;r<e.length;r++){let i=e[r];if(i===void 0)break;if(i===`--concurrency`||i===`--only`||i===`--skip`){let n=e[++r];if(n===void 0)throw Error(`${i} requires a value`);if(i===`--concurrency`){if(t.concurrency=Number(n),!Number.isInteger(t.concurrency)||t.concurrency<=0)throw Error(`--concurrency requires a positive integer, but got ${n}`)}else i===`--only`?t.only.push(n):t.skip.push(n)}else if(i.startsWith(`--`))throw Error(`Unknown option: ${i}`);else if(n)throw Error(`Only one root directory can be specified, but got both ${t.rootDir} and ${i}`);else t.rootDir=i,n=!0}return t}async function S(e){let t=[];return await C(e,t),t.toSorted()}async function C(e,t){let n=await h.readdir(e,{withFileTypes:!0});if(n.some(e=>e.isFile()&&(e.name===`problem.md`||e.name.endsWith(`.problem.md`)))){t.push(e);return}for(let r of n)r.isDirectory()&&r.name!==`node_modules`&&!r.name.startsWith(`.`)&&await C(m.join(e,r.name),t)}async function w(e){try{return await h.stat(e),!0}catch{return!1}}function T(e){return e.length<=g?e:`${e.slice(0,g)}...`}export{v as checkAllProblems};
1
+ import{TEST_CASE_RESULT_PREFIX as e,testCaseResultSchema as t}from"../types/testCaseResult.js";import{findDefaultStdioHarnessFiles as n}from"../helpers/defaultStdioHarness.js";import{readTestCases as r}from"../helpers/readTestCases.js";import{copyProblemDirToTemporaryRoot as i,forciblyRemoveDirectory as a}from"../helpers/temporaryProblemDirCopy.js";import{judgesWithoutTestCases as o,readProblemMarkdownFrontMatter as s}from"../helpers/readProblemMarkdownFrontMatter.js";import{DecisionCode as c}from"../types/decisionCode.js";import{runHarnessProcess as l}from"../helpers/runHarnessProcess.js";import{findFailingModelAnswerDirs as u,findModelAnswerDirs as d}from"../helpers/findModelAnswerDirs.js";import{MISSING_TEST_CASES_ERROR as f,formatDefaultHarnessError as p}from"./runSingleHarness.js";import m from"node:path";import h from"node:fs/promises";import{forEachConcurrently as g,getErrorMessage as _}from"@willbooster/shared-lib";const v=1e3,y=new Map(Object.entries(c).map(([e,t])=>[t,e]));async function b(e){let t=C(e),i=m.resolve(t.rootDir),a=e=>(m.relative(i,e)||`.`).replaceAll(m.sep,`/`),c=await w(i),l=c.filter(e=>{let n=a(e);return t.only.length>0&&!t.only.some(e=>n.includes(e))?!1:!t.skip.some(e=>n.includes(e))});if(l.length===0)return console.error(c.length===0?`No problem directories (containing problem.md or <id>.problem.md) found under ${i}.`:`All ${c.length} problem directories under ${i} were excluded by --only/--skip.`),1;let h=[],v=[];for(let e of l){let t=await n(e);if(t.length>0){h.push(`${a(e)}: ${p(t)}`);continue}let i=await d(e);if(i.length===0){h.push(`${a(e)}: no model answers found under model_answers/`);continue}if(!await E(m.join(e,`judge.ts`))&&(await r(m.join(e,`test_cases`))).length===0){let t;try{t=await s(e)}catch(t){h.push(`${a(e)}: failed to read the problem markdown front matter: ${_(t)}`);continue}if(!o(t)){h.push(`${a(e)}: ${f}`);continue}}let c=await u(e);v.push(...i.map(t=>({problemDir:e,answerDir:t,expectation:`accepted`})),...c.map(t=>({problemDir:e,answerDir:t,expectation:`rejected`})))}for(let e of h)console.error(`✗ ${e}`);let y=m.resolve(process.argv[1]??``),b=0;return await g(v,t.concurrency,async e=>{let t=`${a(e.problemDir)} ${m.relative(e.problemDir,e.answerDir).replaceAll(m.sep,`/`)}`,n=await x(e,y);n===void 0?(b++,console.info(`✓ ${t}`)):(h.push(`${t}: ${n}`),console.error(`✗ ${t}: ${n}`))}),console.info(`\n${b} passed, ${h.length} failed (${v.length} runs, ${l.length} problems)`),h.length===0?0:1}async function x(e,t){let n,r;try{({tempRoot:n,copiedProblemDir:r}=await i(e.problemDir))}catch(e){return D(`failed to copy the problem directory to a temporary location: ${_(e)}`)}let o,s;try{o=S(e,await l([`run`,t,`judge`,m.relative(e.problemDir,e.answerDir)],{cwd:r,env:process.env,timeoutMs:6e5,maxOutputBytes:67108864,tempRoot:n}))}catch(e){o=D(`harness execution failed: ${_(e)}`)}finally{s=await a(n)}if(s)return o;let c=`failed to remove the temporary copy at ${n} (judged code may have left permission-locked files)`;return o===void 0?c:D(`${o}; ${c}`)}function S(n,r){let{stdout:i,stderr:a}=r;if(r.failureReason!==void 0)return D(r.failureReason);if(r.exitCode!==0)return D(`harness exited with ${r.exitCode??`a signal`}${a.trim()?`: ${a.trim()}`:``}`);let o=i.split(/\r?\n/).filter(t=>t.startsWith(e)),s=[];for(let n of o){let r;try{r=t.safeParse(JSON.parse(n.slice(e.length)))}catch{r=void 0}if(!r?.success)return D(`malformed test case result line: ${n}`);s.push(r.data)}if(s.length===0)return`no test case results were printed`;if(n.expectation===`accepted`){let e=s.find(e=>e.decisionCode!==c.ACCEPTED);return e?D(`${y.get(e.decisionCode)??e.decisionCode} on test case ${e.testCaseId}${e.stderr?.trim()?`: ${e.stderr.trim()}`:``}`):void 0}return s.every(e=>e.decisionCode===c.ACCEPTED)?`expected at least one failing test case, but all test cases were accepted`:void 0}function C(e){let t={rootDir:`.`,concurrency:1,only:[],skip:[]},n=!1;for(let r=0;r<e.length;r++){let i=e[r];if(i===void 0)break;if(i===`--concurrency`||i===`--only`||i===`--skip`){let n=e[++r];if(n===void 0)throw Error(`${i} requires a value`);if(i===`--concurrency`){if(t.concurrency=Number(n),!Number.isInteger(t.concurrency)||t.concurrency<=0)throw Error(`--concurrency requires a positive integer, but got ${n}`)}else i===`--only`?t.only.push(n):t.skip.push(n)}else if(i.startsWith(`--`))throw Error(`Unknown option: ${i}`);else if(n)throw Error(`Only one root directory can be specified, but got both ${t.rootDir} and ${i}`);else t.rootDir=i,n=!0}return t}async function w(e){let t=[];return await T(e,t),t.toSorted()}async function T(e,t){let n=await h.readdir(e,{withFileTypes:!0});if(n.some(e=>e.isFile()&&(e.name===`problem.md`||e.name.endsWith(`.problem.md`)))){t.push(e);return}for(let r of n)r.isDirectory()&&r.name!==`node_modules`&&!r.name.startsWith(`.`)&&await T(m.join(e,r.name),t)}async function E(e){try{return await h.stat(e),!0}catch{return!1}}function D(e){return e.length<=v?e:`${e.slice(0,v)}...`}export{b as checkAllProblems};
2
2
  //# sourceMappingURL=checkAllProblems.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"checkAllProblems.js","names":[],"sources":["../../src/cli/checkAllProblems.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport { findDefaultStdioHarnessFiles } from '../helpers/defaultStdioHarness.js';\nimport { findFailingModelAnswerDirs, findModelAnswerDirs } from '../helpers/findModelAnswerDirs.js';\nimport { judgesWithoutTestCases, readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { readTestCases } from '../helpers/readTestCases.js';\nimport { type HarnessProcessResult, runHarnessProcess } from '../helpers/runHarnessProcess.js';\nimport { copyProblemDirToTemporaryRoot, forciblyRemoveDirectory } from '../helpers/temporaryProblemDirCopy.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport { TEST_CASE_RESULT_PREFIX, testCaseResultSchema } from '../types/testCaseResult.js';\n\nimport { formatDefaultHarnessError, MISSING_TEST_CASES_ERROR } from './runSingleHarness.js';\n\nconst RUN_TIMEOUT_MS = 600_000;\nconst MAX_RUN_OUTPUT_BYTES = 64 * 1024 * 1024;\nconst MAX_FAILURE_DETAIL_LENGTH = 1000;\n\n// DecisionCode is a plain const object (not a TypeScript enum), so it has no reverse mapping.\nconst decisionCodeNames = new Map<number, string>(Object.entries(DecisionCode).map(([name, code]) => [code, name]));\n\ninterface CheckOptions {\n rootDir: string;\n concurrency: number;\n only: string[];\n skip: string[];\n}\n\ninterface CheckRun {\n problemDir: string;\n answerDir: string;\n /** Whether all test cases must be accepted (`model_answers`) or at least one must fail (`model_answers.fails`). */\n expectation: 'accepted' | 'rejected';\n}\n\n/**\n * Judge all model answers of all problems (directories containing `problem.md` or\n * `<id>.problem.md`) under a root directory: `model_answers/*` must be fully accepted and\n * `model_answers.fails/*` must fail at least one test case. Returns the process exit code.\n */\nexport async function checkAllProblems(args: readonly string[]): Promise<number> {\n const options = parseCheckArgs(args);\n const rootDir = path.resolve(options.rootDir);\n // Normalize to forward slashes so --only / --skip substrings like courses/foo match on Windows.\n const toRelative = (dir: string): string => (path.relative(rootDir, dir) || '.').replaceAll(path.sep, '/');\n\n const allProblemDirs = await findProblemDirs(rootDir);\n const problemDirs = allProblemDirs.filter((problemDir) => {\n const relativeDir = toRelative(problemDir);\n if (options.only.length > 0 && !options.only.some((substring) => relativeDir.includes(substring))) return false;\n return !options.skip.some((substring) => relativeDir.includes(substring));\n });\n if (problemDirs.length === 0) {\n console.error(\n allProblemDirs.length === 0\n ? `No problem directories (containing problem.md or <id>.problem.md) found under ${rootDir}.`\n : `All ${allProblemDirs.length} problem directories under ${rootDir} were excluded by --only/--skip.`\n );\n return 1;\n }\n\n const failures: string[] = [];\n const runs: CheckRun[] = [];\n for (const problemDir of problemDirs) {\n const defaultHarnessFileNames = await findDefaultStdioHarnessFiles(problemDir);\n if (defaultHarnessFileNames.length > 0) {\n failures.push(`${toRelative(problemDir)}: ${formatDefaultHarnessError(defaultHarnessFileNames)}`);\n continue;\n }\n\n const modelAnswerDirs = await findModelAnswerDirs(problemDir);\n if (modelAnswerDirs.length === 0) {\n failures.push(`${toRelative(problemDir)}: no model answers found under model_answers/`);\n continue;\n }\n\n // Without test cases, stdioJudgePreset prints a single accepted sentinel result, so a standard\n // problem with an empty or missing test_cases/ would otherwise pass without being judged —\n // unless static-analysis rules or manual scoring make the problem judgeable without them.\n if (!(await fileExists(path.join(problemDir, 'judge.ts')))) {\n const testCases = await readTestCases(path.join(problemDir, 'test_cases'));\n if (testCases.length === 0) {\n let frontMatter;\n try {\n frontMatter = await readProblemMarkdownFrontMatter(problemDir);\n } catch (error) {\n failures.push(\n `${toRelative(problemDir)}: failed to read the problem markdown front matter: ${error instanceof Error ? error.message : String(error)}`\n );\n continue;\n }\n if (!judgesWithoutTestCases(frontMatter)) {\n failures.push(`${toRelative(problemDir)}: ${MISSING_TEST_CASES_ERROR}`);\n continue;\n }\n }\n }\n\n const failingModelAnswerDirs = await findFailingModelAnswerDirs(problemDir);\n runs.push(\n ...modelAnswerDirs.map((answerDir): CheckRun => ({ problemDir, answerDir, expectation: 'accepted' })),\n ...failingModelAnswerDirs.map((answerDir): CheckRun => ({ problemDir, answerDir, expectation: 'rejected' }))\n );\n }\n for (const failure of failures) console.error(`✗ ${failure}`);\n\n const cliEntryPath = path.resolve(process.argv[1] ?? '');\n let passedCount = 0;\n let nextRunIndex = 0;\n await Promise.all(\n Array.from({ length: Math.max(1, Math.min(options.concurrency, runs.length)) }, async () => {\n while (nextRunIndex < runs.length) {\n const run = runs[nextRunIndex++];\n if (!run) return;\n const label = `${toRelative(run.problemDir)} ${path.relative(run.problemDir, run.answerDir).replaceAll(path.sep, '/')}`;\n const failureDetail = await executeCheckRun(run, cliEntryPath);\n if (failureDetail === undefined) {\n passedCount++;\n console.info(`✓ ${label}`);\n } else {\n failures.push(`${label}: ${failureDetail}`);\n console.error(`✗ ${label}: ${failureDetail}`);\n }\n }\n })\n );\n\n console.info(\n `\\n${passedCount} passed, ${failures.length} failed (${runs.length} runs, ${problemDirs.length} problems)`\n );\n return failures.length === 0 ? 0 : 1;\n}\n\n/**\n * Run one answer directory through the problem's harness and return a failure detail, if any.\n * The problem directory is copied to a temporary location first so that judging (e.g. build\n * artifacts in answer directories) never modifies the checked repository.\n */\nasync function executeCheckRun(run: CheckRun, cliEntryPath: string): Promise<string | undefined> {\n let tempRoot: string;\n let copiedProblemDir: string;\n try {\n ({ tempRoot, copiedProblemDir } = await copyProblemDirToTemporaryRoot(run.problemDir));\n } catch (error) {\n return truncate(\n `failed to copy the problem directory to a temporary location: ${error instanceof Error ? error.message : String(error)}`\n );\n }\n\n let harnessFailureDetail;\n let removedTempRoot;\n try {\n const result = await runHarnessProcess(\n ['run', cliEntryPath, 'judge', path.relative(run.problemDir, run.answerDir)],\n {\n cwd: copiedProblemDir,\n env: process.env,\n timeoutMs: RUN_TIMEOUT_MS,\n maxOutputBytes: MAX_RUN_OUTPUT_BYTES,\n tempRoot,\n }\n );\n harnessFailureDetail = summarizeHarnessFailure(run, result);\n } catch (error) {\n harnessFailureDetail = truncate(\n `harness execution failed: ${error instanceof Error ? error.message : String(error)}`\n );\n } finally {\n removedTempRoot = await forciblyRemoveDirectory(tempRoot);\n }\n if (removedTempRoot) return harnessFailureDetail;\n const removalFailureDetail = `failed to remove the temporary copy at ${tempRoot} (judged code may have left permission-locked files)`;\n return harnessFailureDetail === undefined\n ? removalFailureDetail\n : truncate(`${harnessFailureDetail}; ${removalFailureDetail}`);\n}\n\n/** Return why the harness run failed the check, or `undefined` when it passed. */\nfunction summarizeHarnessFailure(run: CheckRun, result: HarnessProcessResult): string | undefined {\n const { stdout, stderr } = result;\n if (result.failureReason !== undefined) return truncate(result.failureReason);\n if (result.exitCode !== 0) {\n return truncate(`harness exited with ${result.exitCode ?? 'a signal'}${stderr.trim() ? `: ${stderr.trim()}` : ''}`);\n }\n\n const resultLines = stdout.split(/\\r?\\n/).filter((line) => line.startsWith(TEST_CASE_RESULT_PREFIX));\n const testCaseResults = [];\n for (const line of resultLines) {\n let parsedResult;\n try {\n parsedResult = testCaseResultSchema.safeParse(JSON.parse(line.slice(TEST_CASE_RESULT_PREFIX.length)));\n } catch {\n parsedResult = undefined;\n }\n if (!parsedResult?.success) return truncate(`malformed test case result line: ${line}`);\n testCaseResults.push(parsedResult.data);\n }\n if (testCaseResults.length === 0) return 'no test case results were printed';\n\n if (run.expectation === 'accepted') {\n const rejectedResult = testCaseResults.find((result) => result.decisionCode !== DecisionCode.ACCEPTED);\n if (rejectedResult) {\n return truncate(\n `${decisionCodeNames.get(rejectedResult.decisionCode) ?? rejectedResult.decisionCode} on test case ${rejectedResult.testCaseId}${rejectedResult.stderr?.trim() ? `: ${rejectedResult.stderr.trim()}` : ''}`\n );\n }\n return undefined;\n }\n return testCaseResults.every((result) => result.decisionCode === DecisionCode.ACCEPTED)\n ? 'expected at least one failing test case, but all test cases were accepted'\n : undefined;\n}\n\nfunction parseCheckArgs(args: readonly string[]): CheckOptions {\n const options: CheckOptions = {\n rootDir: '.',\n // Serial by default: judging decides TIME_LIMIT_EXCEEDED from wall-clock time, so parallel\n // runs on a small CI runner could fail timing-sensitive problems non-deterministically.\n concurrency: 1,\n only: [],\n skip: [],\n };\n let hasRootDir = false;\n for (let index = 0; index < args.length; index++) {\n const arg = args[index];\n if (arg === undefined) break;\n if (arg === '--concurrency' || arg === '--only' || arg === '--skip') {\n const value = args[++index];\n if (value === undefined) throw new Error(`${arg} requires a value`);\n if (arg === '--concurrency') {\n options.concurrency = Number(value);\n if (!Number.isInteger(options.concurrency) || options.concurrency <= 0) {\n throw new Error(`--concurrency requires a positive integer, but got ${value}`);\n }\n } else if (arg === '--only') {\n options.only.push(value);\n } else {\n options.skip.push(value);\n }\n } else if (arg.startsWith('--')) {\n throw new Error(`Unknown option: ${arg}`);\n } else if (hasRootDir) {\n throw new Error(`Only one root directory can be specified, but got both ${options.rootDir} and ${arg}`);\n } else {\n options.rootDir = arg;\n hasRootDir = true;\n }\n }\n return options;\n}\n\n/** Find directories containing `problem.md` or `<id>.problem.md`, without descending into found problems. */\nasync function findProblemDirs(rootDir: string): Promise<string[]> {\n const problemDirs: string[] = [];\n await visitDirectory(rootDir, problemDirs);\n return problemDirs.toSorted();\n}\n\nasync function visitDirectory(dir: string, problemDirs: string[]): Promise<void> {\n // Traversal errors (e.g. an unreadable subtree) must fail the check: skipping them silently\n // could report a green result while covering only part of the repository.\n const entries = await fs.readdir(dir, { withFileTypes: true });\n if (entries.some((entry) => entry.isFile() && (entry.name === 'problem.md' || entry.name.endsWith('.problem.md')))) {\n problemDirs.push(dir);\n return;\n }\n for (const entry of entries) {\n if (!entry.isDirectory() || entry.name === 'node_modules' || entry.name.startsWith('.')) continue;\n await visitDirectory(path.join(dir, entry.name), problemDirs);\n }\n}\n\nasync function fileExists(filePath: string): Promise<boolean> {\n try {\n await fs.stat(filePath);\n return true;\n } catch {\n return false;\n }\n}\n\nfunction truncate(text: string): string {\n return text.length <= MAX_FAILURE_DETAIL_LENGTH ? text : `${text.slice(0, MAX_FAILURE_DETAIL_LENGTH)}...`;\n}\n"],"mappings":"s1BAcA,MAEM,EAA4B,IAG5B,EAAoB,IAAI,IAAoB,OAAO,QAAQ,CAAY,CAAC,CAAC,KAAK,CAAC,EAAM,KAAU,CA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CAAC,EAE/B,OADA,MAAM,EAAe,EAAS,CAAW,EAClC,EAAY,SAAS,CAC9B,CAEA,eAAe,EAAe,EAAa,EAAsC,CAG/E,IAAM,EAAU,MAAM,EAAG,QAAQ,EAAK,CAAE,cAAe,EAAK,CAAC,EAC7D,GAAI,EAAQ,KAAM,GAAU,EAAM,OAAO,IAAM,EAAM,OAAS,cAAgB,EAAM,KAAK,SAAS,aAAa,EAAE,EAAG,CAClH,EAAY,KAAK,CAAG,EACpB,MACF,CACA,IAAK,IAAM,KAAS,EACb,EAAM,YAAY,GAAK,EAAM,OAAS,gBAAkB,GAAM,KAAK,WAAW,GAAG,GACtF,MAAM,EAAe,EAAK,KAAK,EAAK,EAAM,IAAI,EAAG,CAAW,CAEhE,CAEA,eAAe,EAAW,EAAoC,CAC5D,GAAI,CAEF,OADA,MAAM,EAAG,KAAK,CAAQ,EACf,EACT,MAAQ,CACN,MAAO,EACT,CACF,CAEA,SAAS,EAAS,EAAsB,CACtC,OAAO,EAAK,QAAU,EAA4B,EAAO,GAAG,EAAK,MAAM,EAAG,CAAyB,EAAE,IACvG"}
1
+ {"version":3,"file":"checkAllProblems.js","names":[],"sources":["../../src/cli/checkAllProblems.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\nimport { forEachConcurrently, getErrorMessage } from '@willbooster/shared-lib';\n\nimport { findDefaultStdioHarnessFiles } from '../helpers/defaultStdioHarness.js';\nimport { findFailingModelAnswerDirs, findModelAnswerDirs } from '../helpers/findModelAnswerDirs.js';\nimport { judgesWithoutTestCases, readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { readTestCases } from '../helpers/readTestCases.js';\nimport { type HarnessProcessResult, runHarnessProcess } from '../helpers/runHarnessProcess.js';\nimport { copyProblemDirToTemporaryRoot, forciblyRemoveDirectory } from '../helpers/temporaryProblemDirCopy.js';\nimport { DecisionCode } from '../types/decisionCode.js';\nimport { TEST_CASE_RESULT_PREFIX, testCaseResultSchema } from '../types/testCaseResult.js';\n\nimport { formatDefaultHarnessError, MISSING_TEST_CASES_ERROR } from './runSingleHarness.js';\n\nconst RUN_TIMEOUT_MS = 600_000;\nconst MAX_RUN_OUTPUT_BYTES = 64 * 1024 * 1024;\nconst MAX_FAILURE_DETAIL_LENGTH = 1000;\n\n// DecisionCode is a plain const object (not a TypeScript enum), so it has no reverse mapping.\nconst decisionCodeNames = new Map<number, string>(Object.entries(DecisionCode).map(([name, code]) => [code, name]));\n\ninterface CheckOptions {\n rootDir: string;\n concurrency: number;\n only: string[];\n skip: string[];\n}\n\ninterface CheckRun {\n problemDir: string;\n answerDir: string;\n /** Whether all test cases must be accepted (`model_answers`) or at least one must fail (`model_answers.fails`). */\n expectation: 'accepted' | 'rejected';\n}\n\n/**\n * Judge all model answers of all problems (directories containing `problem.md` or\n * `<id>.problem.md`) under a root directory: `model_answers/*` must be fully accepted and\n * `model_answers.fails/*` must fail at least one test case. Returns the process exit code.\n */\nexport async function checkAllProblems(args: readonly string[]): Promise<number> {\n const options = parseCheckArgs(args);\n const rootDir = path.resolve(options.rootDir);\n // Normalize to forward slashes so --only / --skip substrings like courses/foo match on Windows.\n const toRelative = (dir: string): string => (path.relative(rootDir, dir) || '.').replaceAll(path.sep, '/');\n\n const allProblemDirs = await findProblemDirs(rootDir);\n const problemDirs = allProblemDirs.filter((problemDir) => {\n const relativeDir = toRelative(problemDir);\n if (options.only.length > 0 && !options.only.some((substring) => relativeDir.includes(substring))) return false;\n return !options.skip.some((substring) => relativeDir.includes(substring));\n });\n if (problemDirs.length === 0) {\n console.error(\n allProblemDirs.length === 0\n ? `No problem directories (containing problem.md or <id>.problem.md) found under ${rootDir}.`\n : `All ${allProblemDirs.length} problem directories under ${rootDir} were excluded by --only/--skip.`\n );\n return 1;\n }\n\n const failures: string[] = [];\n const runs: CheckRun[] = [];\n for (const problemDir of problemDirs) {\n const defaultHarnessFileNames = await findDefaultStdioHarnessFiles(problemDir);\n if (defaultHarnessFileNames.length > 0) {\n failures.push(`${toRelative(problemDir)}: ${formatDefaultHarnessError(defaultHarnessFileNames)}`);\n continue;\n }\n\n const modelAnswerDirs = await findModelAnswerDirs(problemDir);\n if (modelAnswerDirs.length === 0) {\n failures.push(`${toRelative(problemDir)}: no model answers found under model_answers/`);\n continue;\n }\n\n // Without test cases, stdioJudgePreset prints a single accepted sentinel result, so a standard\n // problem with an empty or missing test_cases/ would otherwise pass without being judged —\n // unless static-analysis rules or manual scoring make the problem judgeable without them.\n if (!(await fileExists(path.join(problemDir, 'judge.ts')))) {\n const testCases = await readTestCases(path.join(problemDir, 'test_cases'));\n if (testCases.length === 0) {\n let frontMatter;\n try {\n frontMatter = await readProblemMarkdownFrontMatter(problemDir);\n } catch (error) {\n failures.push(\n `${toRelative(problemDir)}: failed to read the problem markdown front matter: ${getErrorMessage(error)}`\n );\n continue;\n }\n if (!judgesWithoutTestCases(frontMatter)) {\n failures.push(`${toRelative(problemDir)}: ${MISSING_TEST_CASES_ERROR}`);\n continue;\n }\n }\n }\n\n const failingModelAnswerDirs = await findFailingModelAnswerDirs(problemDir);\n runs.push(\n ...modelAnswerDirs.map((answerDir): CheckRun => ({ problemDir, answerDir, expectation: 'accepted' })),\n ...failingModelAnswerDirs.map((answerDir): CheckRun => ({ problemDir, answerDir, expectation: 'rejected' }))\n );\n }\n for (const failure of failures) console.error(`✗ ${failure}`);\n\n const cliEntryPath = path.resolve(process.argv[1] ?? '');\n let passedCount = 0;\n await forEachConcurrently(runs, options.concurrency, async (run) => {\n const label = `${toRelative(run.problemDir)} ${path.relative(run.problemDir, run.answerDir).replaceAll(path.sep, '/')}`;\n const failureDetail = await executeCheckRun(run, cliEntryPath);\n if (failureDetail === undefined) {\n passedCount++;\n console.info(`✓ ${label}`);\n } else {\n failures.push(`${label}: ${failureDetail}`);\n console.error(`✗ ${label}: ${failureDetail}`);\n }\n });\n\n console.info(\n `\\n${passedCount} passed, ${failures.length} failed (${runs.length} runs, ${problemDirs.length} problems)`\n );\n return failures.length === 0 ? 0 : 1;\n}\n\n/**\n * Run one answer directory through the problem's harness and return a failure detail, if any.\n * The problem directory is copied to a temporary location first so that judging (e.g. build\n * artifacts in answer directories) never modifies the checked repository.\n */\nasync function executeCheckRun(run: CheckRun, cliEntryPath: string): Promise<string | undefined> {\n let tempRoot: string;\n let copiedProblemDir: string;\n try {\n ({ tempRoot, copiedProblemDir } = await copyProblemDirToTemporaryRoot(run.problemDir));\n } catch (error) {\n return truncate(`failed to copy the problem directory to a temporary location: ${getErrorMessage(error)}`);\n }\n\n let harnessFailureDetail;\n let removedTempRoot;\n try {\n const result = await runHarnessProcess(\n ['run', cliEntryPath, 'judge', path.relative(run.problemDir, run.answerDir)],\n {\n cwd: copiedProblemDir,\n env: process.env,\n timeoutMs: RUN_TIMEOUT_MS,\n maxOutputBytes: MAX_RUN_OUTPUT_BYTES,\n tempRoot,\n }\n );\n harnessFailureDetail = summarizeHarnessFailure(run, result);\n } catch (error) {\n harnessFailureDetail = truncate(`harness execution failed: ${getErrorMessage(error)}`);\n } finally {\n removedTempRoot = await forciblyRemoveDirectory(tempRoot);\n }\n if (removedTempRoot) return harnessFailureDetail;\n const removalFailureDetail = `failed to remove the temporary copy at ${tempRoot} (judged code may have left permission-locked files)`;\n return harnessFailureDetail === undefined\n ? removalFailureDetail\n : truncate(`${harnessFailureDetail}; ${removalFailureDetail}`);\n}\n\n/** Return why the harness run failed the check, or `undefined` when it passed. */\nfunction summarizeHarnessFailure(run: CheckRun, result: HarnessProcessResult): string | undefined {\n const { stdout, stderr } = result;\n if (result.failureReason !== undefined) return truncate(result.failureReason);\n if (result.exitCode !== 0) {\n return truncate(`harness exited with ${result.exitCode ?? 'a signal'}${stderr.trim() ? `: ${stderr.trim()}` : ''}`);\n }\n\n const resultLines = stdout.split(/\\r?\\n/).filter((line) => line.startsWith(TEST_CASE_RESULT_PREFIX));\n const testCaseResults = [];\n for (const line of resultLines) {\n let parsedResult;\n try {\n parsedResult = testCaseResultSchema.safeParse(JSON.parse(line.slice(TEST_CASE_RESULT_PREFIX.length)));\n } catch {\n parsedResult = undefined;\n }\n if (!parsedResult?.success) return truncate(`malformed test case result line: ${line}`);\n testCaseResults.push(parsedResult.data);\n }\n if (testCaseResults.length === 0) return 'no test case results were printed';\n\n if (run.expectation === 'accepted') {\n const rejectedResult = testCaseResults.find((result) => result.decisionCode !== DecisionCode.ACCEPTED);\n if (rejectedResult) {\n return truncate(\n `${decisionCodeNames.get(rejectedResult.decisionCode) ?? rejectedResult.decisionCode} on test case ${rejectedResult.testCaseId}${rejectedResult.stderr?.trim() ? `: ${rejectedResult.stderr.trim()}` : ''}`\n );\n }\n return undefined;\n }\n return testCaseResults.every((result) => result.decisionCode === DecisionCode.ACCEPTED)\n ? 'expected at least one failing test case, but all test cases were accepted'\n : undefined;\n}\n\nfunction parseCheckArgs(args: readonly string[]): CheckOptions {\n const options: CheckOptions = {\n rootDir: '.',\n // Serial by default: judging decides TIME_LIMIT_EXCEEDED from wall-clock time, so parallel\n // runs on a small CI runner could fail timing-sensitive problems non-deterministically.\n concurrency: 1,\n only: [],\n skip: [],\n };\n let hasRootDir = false;\n for (let index = 0; index < args.length; index++) {\n const arg = args[index];\n if (arg === undefined) break;\n if (arg === '--concurrency' || arg === '--only' || arg === '--skip') {\n const value = args[++index];\n if (value === undefined) throw new Error(`${arg} requires a value`);\n if (arg === '--concurrency') {\n options.concurrency = Number(value);\n if (!Number.isInteger(options.concurrency) || options.concurrency <= 0) {\n throw new Error(`--concurrency requires a positive integer, but got ${value}`);\n }\n } else if (arg === '--only') {\n options.only.push(value);\n } else {\n options.skip.push(value);\n }\n } else if (arg.startsWith('--')) {\n throw new Error(`Unknown option: ${arg}`);\n } else if (hasRootDir) {\n throw new Error(`Only one root directory can be specified, but got both ${options.rootDir} and ${arg}`);\n } else {\n options.rootDir = arg;\n hasRootDir = true;\n }\n }\n return options;\n}\n\n/** Find directories containing `problem.md` or `<id>.problem.md`, without descending into found problems. */\nasync function findProblemDirs(rootDir: string): Promise<string[]> {\n const problemDirs: string[] = [];\n await visitDirectory(rootDir, problemDirs);\n return problemDirs.toSorted();\n}\n\nasync function visitDirectory(dir: string, problemDirs: string[]): Promise<void> {\n // Traversal errors (e.g. an unreadable subtree) must fail the check: skipping them silently\n // could report a green result while covering only part of the repository.\n const entries = await fs.readdir(dir, { withFileTypes: true });\n if (entries.some((entry) => entry.isFile() && (entry.name === 'problem.md' || entry.name.endsWith('.problem.md')))) {\n problemDirs.push(dir);\n return;\n }\n for (const entry of entries) {\n if (!entry.isDirectory() || entry.name === 'node_modules' || entry.name.startsWith('.')) continue;\n await visitDirectory(path.join(dir, entry.name), problemDirs);\n }\n}\n\nasync function fileExists(filePath: string): Promise<boolean> {\n try {\n await fs.stat(filePath);\n return true;\n } catch {\n return false;\n }\n}\n\nfunction truncate(text: string): string {\n return text.length <= MAX_FAILURE_DETAIL_LENGTH ? text : `${text.slice(0, MAX_FAILURE_DETAIL_LENGTH)}...`;\n}\n"],"mappings":"y6BAeA,MAEM,EAA4B,IAG5B,EAAoB,IAAI,IAAoB,OAAO,QAAQ,CAAY,CAAC,CAAC,KAAK,CAAC,EAAM,KAAU,CAAC,EAAM,CAAI,CAAC,CAAC,EAqBlH,eAAsB,EAAiB,EAA0C,CAC/E,IAAM,EAAU,EAAe,CAAI,EAC7B,EAAU,EAAK,QAAQ,EAAQ,OAAO,EAEtC,EAAc,IAAyB,EAAK,SAAS,EAAS,CAAG,GAAK,IAAA,CAAK,WAAW,EAAK,IAAK,GAAG,EAEnG,EAAiB,MAAM,EAAgB,CAAO,EAC9C,EAAc,EAAe,OAAQ,GAAe,CACxD,IAAM,EAAc,EAAW,CAAU,EAEzC,OADI,EAAQ,KAAK,OAAS,GAAK,CAAC,EAAQ,KAAK,KAAM,GAAc,EAAY,SAAS,CAAS,CAAC,EAAU,GACnG,CAAC,EAAQ,KAAK,KAAM,GAAc,EAAY,SAAS,CAAS,CAAC,CAC1E,CAAC,EACD,GAAI,EAAY,SAAW,EAMzB,OALA,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=require("./temporaryProblemDirCopy.cjs"),r=require("../types/decisionCode.cjs"),i=require("./printDebugBanner.cjs"),a=require("./runHarnessProcess.cjs");let o=require("node:path");o=e.__toESM(o,1);const s=3e4,c=1e4;async function l(e,t,r,l={}){let h=Math.max(s,(l.expectedMaxDurationMs??0)+c),g;try{let s=await n.copyProblemDirToTemporaryRoot(e);g=s.tempRoot;let{copiedProblemDir:c}=s,_=o.default.resolve(e),v=o.default.relative(_,o.default.resolve(t.cwd)),y=o.default.join(c,v),b=p(_);if(b.startsWith(`..`)||o.default.isAbsolute(b))return i.printDebugBanner([`[DEBUG MODE] isolated problem directory check skipped`,``,`The invoked judge script is located outside the problem directory.`,`Script path: ${b}`]),{passed:!0};let x=process.execArgv.filter(d),S=JSON.stringify(f(r)?r:{}),C=await a.runHarnessProcess([...x,b,y,S],{cwd:c,env:process.env,timeoutMs:h,maxOutputBytes:67108864,tempRoot:g});return C.exitCode===0&&m(C.stdout)?(i.printDebugBanner([`[DEBUG MODE] isolated problem directory check passed`,``,`Copied problem dir : ${c}`,`Checked cwd : ${v}`]),{passed:!0}):(i.printDebugBanner([`[DEBUG MODE] isolated problem directory check failed`,``,...u(C,h,l),``,`Copied problem dir : ${c}`,`Checked cwd : ${v}`,`Exit status : ${C.exitCode??C.signal??`unknown`}`,`Failure reason : ${C.failureReason??`<none>`}`,``,`stdout:`,C.stdout.trimEnd()||`<empty>`,``,`stderr:`,C.stderr.trimEnd()||`<empty>`]),{passed:!1})}catch(e){return i.printDebugBanner([`[DEBUG MODE] isolated problem directory check failed due to an unexpected error`,``,e instanceof Error?e.message:String(e)]),{passed:!1}}finally{g&&await n.forciblyRemoveDirectory(g)}}function u(e,t,n){return e.timedOut?[`The judge did not finish within ${t/1e3} seconds after copying only the problem directory to a temporary location.`,...n.expectedMaxDurationMs===void 0?[`This judge declares no run time, so the check uses its fixed ${s/1e3}-second budget.`]:[`The budget is the larger of ${s/1e3} seconds and the build timeout plus the time limit of every test case plus a ${c/1e3}-second overhead.`,`Check timeLimitMs, the number of test cases, and one-off startup costs such as cold caches.`]]:e.failureReason===void 0?e.exitCode===void 0?[`The judge was terminated by ${e.signal??`an unknown signal`} (e.g. an out-of-memory kill or a cancelled job).`]:[`The judge did not complete successfully after copying only the problem directory to a temporary location.`,`Make sure judge.ts imports only files included in the problem directory.`]:[`The judge was stopped before it finished; see the failure reason below.`]}function d(e){return!e.startsWith(`--inspect`)&&!e.startsWith(`--watch`)&&!e.startsWith(`--hot`)}function f(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function p(e){let t=process.argv[1];if(!t)return`.${o.default.sep}judge.ts`;let n=o.default.relative(e,o.default.resolve(t));return o.default.isAbsolute(n)||n.startsWith(`.`)?n:`.${o.default.sep}${n}`}function m(e){let n=e.split(/\r?\n/).filter(e=>e.startsWith(t.TEST_CASE_RESULT_PREFIX));return n.length!==0&&n.every(e=>{try{let n=t.testCaseResultSchema.safeParse(JSON.parse(e.slice(t.TEST_CASE_RESULT_PREFIX.length)));return n.success&&n.data.decisionCode===r.DecisionCode.ACCEPTED}catch{return!1}})}exports.checkProblemDirIsolation=l;
1
+ "use strict";const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("../types/testCaseResult.cjs"),n=require("./temporaryProblemDirCopy.cjs"),r=require("../types/decisionCode.cjs"),i=require("./printDebugBanner.cjs"),a=require("./runHarnessProcess.cjs");let o=require("node:path");o=e.__toESM(o,1);let s=require("@willbooster/shared-lib");const c=3e4,l=1e4;async function u(e,t,r,u={}){let h=Math.max(c,(u.expectedMaxDurationMs??0)+l),g;try{let c=await n.copyProblemDirToTemporaryRoot(e);g=c.tempRoot;let{copiedProblemDir:l}=c,_=o.default.resolve(e),v=o.default.relative(_,o.default.resolve(t.cwd)),y=o.default.join(l,v),b=p(_);if(b.startsWith(`..`)||o.default.isAbsolute(b))return i.printDebugBanner([`[DEBUG MODE] isolated problem directory check skipped`,``,`The invoked judge script is located outside the problem directory.`,`Script path: ${b}`]),{passed:!0};let x=process.execArgv.filter(f),S=JSON.stringify((0,s.isRecord)(r)?r:{}),C=await a.runHarnessProcess([...x,b,y,S],{cwd:l,env:process.env,timeoutMs:h,maxOutputBytes:67108864,tempRoot:g});return C.exitCode===0&&m(C.stdout)?(i.printDebugBanner([`[DEBUG MODE] isolated problem directory check passed`,``,`Copied problem dir : ${l}`,`Checked cwd : ${v}`]),{passed:!0}):(i.printDebugBanner([`[DEBUG MODE] isolated problem directory check failed`,``,...d(C,h,u),``,`Copied problem dir : ${l}`,`Checked cwd : ${v}`,`Exit status : ${C.exitCode??C.signal??`unknown`}`,`Failure reason : ${C.failureReason??`<none>`}`,``,`stdout:`,C.stdout.trimEnd()||`<empty>`,``,`stderr:`,C.stderr.trimEnd()||`<empty>`]),{passed:!1})}catch(e){return i.printDebugBanner([`[DEBUG MODE] isolated problem directory check failed due to an unexpected error`,``,(0,s.getErrorMessage)(e)]),{passed:!1}}finally{g&&await n.forciblyRemoveDirectory(g)}}function d(e,t,n){return e.timedOut?[`The judge did not finish within ${t/1e3} seconds after copying only the problem directory to a temporary location.`,...n.expectedMaxDurationMs===void 0?[`This judge declares no run time, so the check uses its fixed ${c/1e3}-second budget.`]:[`The budget is the larger of ${c/1e3} seconds and the build timeout plus the time limit of every test case plus a ${l/1e3}-second overhead.`,`Check timeLimitMs, the number of test cases, and one-off startup costs such as cold caches.`]]:e.failureReason===void 0?e.exitCode===void 0?[`The judge was terminated by ${e.signal??`an unknown signal`} (e.g. an out-of-memory kill or a cancelled job).`]:[`The judge did not complete successfully after copying only the problem directory to a temporary location.`,`Make sure judge.ts imports only files included in the problem directory.`]:[`The judge was stopped before it finished; see the failure reason below.`]}function f(e){return!e.startsWith(`--inspect`)&&!e.startsWith(`--watch`)&&!e.startsWith(`--hot`)}function p(e){let t=process.argv[1];if(!t)return`.${o.default.sep}judge.ts`;let n=o.default.relative(e,o.default.resolve(t));return o.default.isAbsolute(n)||n.startsWith(`.`)?n:`.${o.default.sep}${n}`}function m(e){let n=e.split(/\r?\n/).filter(e=>e.startsWith(t.TEST_CASE_RESULT_PREFIX));return n.length!==0&&n.every(e=>{try{let n=t.testCaseResultSchema.safeParse(JSON.parse(e.slice(t.TEST_CASE_RESULT_PREFIX.length)));return n.success&&n.data.decisionCode===r.DecisionCode.ACCEPTED}catch{return!1}})}exports.checkProblemDirIsolation=u;
2
2
  //# sourceMappingURL=checkProblemDirIsolation.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"checkProblemDirIsolation.cjs","names":["copyProblemDirToTemporaryRoot","path","runHarnessProcess","forciblyRemoveDirectory","TEST_CASE_RESULT_PREFIX","testCaseResultSchema","DecisionCode"],"sources":["../../src/helpers/checkProblemDirIsolation.ts"],"sourcesContent":["import path from 'node:path';\n\nimport { DecisionCode } from '../types/decisionCode.js';\nimport { TEST_CASE_RESULT_PREFIX, testCaseResultSchema } from '../types/testCaseResult.js';\n\nimport { printDebugBanner } from './printDebugBanner.js';\nimport type { ResolvedCwd } from './resolveCwds.js';\nimport { type HarnessProcessResult, runHarnessProcess } from './runHarnessProcess.js';\nimport { copyProblemDirToTemporaryRoot, forciblyRemoveDirectory } from './temporaryProblemDirCopy.js';\n\nconst ISOLATION_CHECK_MIN_TIMEOUT_MS = 30_000;\n// Starting bun and printing results are not covered by the judge's own limits.\nconst ISOLATION_CHECK_OVERHEAD_MS = 10_000;\nconst ISOLATION_CHECK_MAX_OUTPUT_BYTES = 64 * 1024 * 1024;\n\nexport interface ProblemDirIsolationCheckOptions {\n /**\n * The judge's declared worst-case duration for one accepted submission (build timeout plus the\n * time limit of every test case). The check allows at least that plus a fixed overhead; per-case\n * process startup and custom judge callbacks are not budgeted separately.\n */\n expectedMaxDurationMs?: number;\n}\n\nexport interface ProblemDirIsolationCheckResult {\n passed: boolean;\n}\n\n/**\n * Check that a judge can run after only the problem directory is copied elsewhere.\n */\nexport async function checkProblemDirIsolation(\n problemDir: string,\n resolvedCwd: ResolvedCwd,\n params: unknown,\n options: ProblemDirIsolationCheckOptions = {}\n): Promise<ProblemDirIsolationCheckResult> {\n const timeoutMs = Math.max(\n ISOLATION_CHECK_MIN_TIMEOUT_MS,\n (options.expectedMaxDurationMs ?? 0) + ISOLATION_CHECK_OVERHEAD_MS\n );\n let tempRoot: string | undefined;\n try {\n const copyResult = await copyProblemDirToTemporaryRoot(problemDir);\n tempRoot = copyResult.tempRoot;\n const { copiedProblemDir } = copyResult;\n const absoluteProblemDir = path.resolve(problemDir);\n\n const relativeCwd = path.relative(absoluteProblemDir, path.resolve(resolvedCwd.cwd));\n const copiedCwd = path.join(copiedProblemDir, relativeCwd);\n const scriptPath = getInvokedScriptPath(absoluteProblemDir);\n if (scriptPath.startsWith('..') || path.isAbsolute(scriptPath)) {\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check skipped',\n '',\n 'The invoked judge script is located outside the problem directory.',\n `Script path: ${scriptPath}`,\n ]);\n return { passed: true };\n }\n const execArgv = process.execArgv.filter(isIsolationExecArg);\n const paramsJson = JSON.stringify(isJudgeParamsObject(params) ? params : {});\n const result = await runHarnessProcess([...execArgv, scriptPath, copiedCwd, paramsJson], {\n cwd: copiedProblemDir,\n env: process.env,\n timeoutMs,\n maxOutputBytes: ISOLATION_CHECK_MAX_OUTPUT_BYTES,\n tempRoot,\n });\n\n if (result.exitCode === 0 && isAcceptedJudgeOutput(result.stdout)) {\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check passed',\n '',\n `Copied problem dir : ${copiedProblemDir}`,\n `Checked cwd : ${relativeCwd}`,\n ]);\n return { passed: true };\n }\n\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check failed',\n '',\n ...describeFailure(result, timeoutMs, options),\n '',\n `Copied problem dir : ${copiedProblemDir}`,\n `Checked cwd : ${relativeCwd}`,\n `Exit status : ${result.exitCode ?? result.signal ?? 'unknown'}`,\n `Failure reason : ${result.failureReason ?? '<none>'}`,\n '',\n 'stdout:',\n result.stdout.trimEnd() || '<empty>',\n '',\n 'stderr:',\n result.stderr.trimEnd() || '<empty>',\n ]);\n return { passed: false };\n } catch (error) {\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check failed due to an unexpected error',\n '',\n error instanceof Error ? error.message : String(error),\n ]);\n return { passed: false };\n } finally {\n // Cleanup failures must not mask the primary isolation check result.\n if (tempRoot) await forciblyRemoveDirectory(tempRoot);\n }\n}\n\nfunction describeFailure(\n result: HarnessProcessResult,\n timeoutMs: number,\n options: ProblemDirIsolationCheckOptions\n): string[] {\n if (result.timedOut) {\n return [\n `The judge did not finish within ${timeoutMs / 1000} seconds after copying only the problem directory to a temporary location.`,\n ...(options.expectedMaxDurationMs === undefined\n ? [\n `This judge declares no run time, so the check uses its fixed ${ISOLATION_CHECK_MIN_TIMEOUT_MS / 1000}-second budget.`,\n ]\n : [\n `The budget is the larger of ${ISOLATION_CHECK_MIN_TIMEOUT_MS / 1000} seconds and the build timeout plus the time limit of every test case plus a ${ISOLATION_CHECK_OVERHEAD_MS / 1000}-second overhead.`,\n 'Check timeLimitMs, the number of test cases, and one-off startup costs such as cold caches.',\n ]),\n ];\n }\n if (result.failureReason !== undefined) {\n return ['The judge was stopped before it finished; see the failure reason below.'];\n }\n if (result.exitCode === undefined) {\n return [\n `The judge was terminated by ${result.signal ?? 'an unknown signal'} (e.g. an out-of-memory kill or a cancelled job).`,\n ];\n }\n return [\n 'The judge did not complete successfully after copying only the problem directory to a temporary location.',\n 'Make sure judge.ts imports only files included in the problem directory.',\n ];\n}\n\nfunction isIsolationExecArg(arg: string): boolean {\n return !arg.startsWith('--inspect') && !arg.startsWith('--watch') && !arg.startsWith('--hot');\n}\n\nfunction isJudgeParamsObject(params: unknown): params is object {\n return params !== undefined && params !== null && typeof params === 'object' && !Array.isArray(params);\n}\n\nfunction getInvokedScriptPath(problemDir: string): string {\n const scriptPath = process.argv[1];\n if (!scriptPath) return `.${path.sep}judge.ts`;\n const relativeScriptPath = path.relative(problemDir, path.resolve(scriptPath));\n if (path.isAbsolute(relativeScriptPath)) return relativeScriptPath;\n return relativeScriptPath.startsWith('.') ? relativeScriptPath : `.${path.sep}${relativeScriptPath}`;\n}\n\nfunction isAcceptedJudgeOutput(stdout: string): boolean {\n const resultLines = stdout.split(/\\r?\\n/).filter((line) => line.startsWith(TEST_CASE_RESULT_PREFIX));\n if (resultLines.length === 0) return false;\n\n return resultLines.every((line) => {\n try {\n const parsedResult = testCaseResultSchema.safeParse(JSON.parse(line.slice(TEST_CASE_RESULT_PREFIX.length)));\n return parsedResult.success && parsedResult.data.decisionCode === DecisionCode.ACCEPTED;\n } catch {\n return false;\n }\n });\n}\n"],"mappings":"kTAUA,MAAM,EAAiC,IAEjC,EAA8B,IAmBpC,eAAsB,EACpB,EACA,EACA,EACA,EAA2C,CAAC,EACH,CACzC,IAAM,EAAY,KAAK,IACrB,GACC,EAAQ,uBAAyB,GAAK,CACzC,EACI,EACJ,GAAI,CACF,IAAM,EAAa,MAAMA,EAAAA,8BAA8B,CAAU,EACjE,EAAW,EAAW,SACtB,GAAM,CAAE,oBAAqB,EACvB,EAAqBC,EAAAA,QAAK,QAAQ,CAAU,EAE5C,EAAcA,EAAAA,QAAK,SAAS,EAAoBA,EAAAA,QAAK,QAAQ,EAAY,GAAG,CAAC,EAC7E,EAAYA,EAAAA,QAAK,KAAK,EAAkB,CAAW,EACnD,EAAa,EAAqB,CAAkB,EAC1D,GAAI,EAAW,WAAW,IAAI,GAAKA,EAAAA,QAAK,WAAW,CAAU,EAO3D,OANA,EAAA,iBAAiB,CACf,wDACA,GACA,qEACA,gBAAgB,GAClB,CAAC,EACM,CAAE,OAAQ,EAAK,EAExB,IAAM,EAAW,QAAQ,SAAS,OAAO,CAAkB,EACrD,EAAa,KAAK,UAAU,EAAoB,CAAM,EAAI,EAAS,CAAC,CAAC,EACrE,EAAS,MAAMC,EAAAA,kBAAkB,CAAC,GAAG,EAAU,EAAY,EAAW,CAAU,EAAG,CACvF,IAAK,EACL,IAAK,QAAQ,IACb,YACA,eAAgB,SAChB,UACF,CAAC,EA4BD,OA1BI,EAAO,WAA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EAAqB,EAA4B,CACxD,IAAM,EAAa,QAAQ,KAAK,GAChC,GAAI,CAAC,EAAY,MAAO,IAAIF,EAAAA,QAAK,IAAI,UACrC,IAAM,EAAqBA,EAAAA,QAAK,SAAS,EAAYA,EAAAA,QAAK,QAAQ,CAAU,CAAC,EAE7E,OADIA,EAAAA,QAAK,WAAW,CAAkB,GAC/B,EAAmB,WAAW,GAAG,EADQ,EACiB,IAAIA,EAAAA,QAAK,MAAM,GAClF,CAEA,SAAS,EAAsB,EAAyB,CACtD,IAAM,EAAc,EAAO,MAAM,OAAO,CAAC,CAAC,OAAQ,GAAS,EAAK,WAAWG,EAAAA,uBAAuB,CAAC,EAGnG,OAFI,EAAY,SAAW,GAEpB,EAAY,MAAO,GAAS,CACjC,GAAI,CACF,IAAM,EAAeC,EAAAA,qBAAqB,UAAU,KAAK,MAAM,EAAK,MAAMD,EAAAA,wBAAwB,MAAM,CAAC,CAAC,EAC1G,OAAO,EAAa,SAAW,EAAa,KAAK,eAAiBE,EAAAA,aAAa,QACjF,MAAQ,CACN,MAAO,EACT,CACF,CAAC,CACH"}
1
+ {"version":3,"file":"checkProblemDirIsolation.cjs","names":["copyProblemDirToTemporaryRoot","path","isRecord","runHarnessProcess","getErrorMessage","forciblyRemoveDirectory","TEST_CASE_RESULT_PREFIX","testCaseResultSchema","DecisionCode"],"sources":["../../src/helpers/checkProblemDirIsolation.ts"],"sourcesContent":["import path from 'node:path';\nimport { getErrorMessage, isRecord } from '@willbooster/shared-lib';\n\nimport { DecisionCode } from '../types/decisionCode.js';\nimport { TEST_CASE_RESULT_PREFIX, testCaseResultSchema } from '../types/testCaseResult.js';\n\nimport { printDebugBanner } from './printDebugBanner.js';\nimport type { ResolvedCwd } from './resolveCwds.js';\nimport { type HarnessProcessResult, runHarnessProcess } from './runHarnessProcess.js';\nimport { copyProblemDirToTemporaryRoot, forciblyRemoveDirectory } from './temporaryProblemDirCopy.js';\n\nconst ISOLATION_CHECK_MIN_TIMEOUT_MS = 30_000;\n// Starting bun and printing results are not covered by the judge's own limits.\nconst ISOLATION_CHECK_OVERHEAD_MS = 10_000;\nconst ISOLATION_CHECK_MAX_OUTPUT_BYTES = 64 * 1024 * 1024;\n\nexport interface ProblemDirIsolationCheckOptions {\n /**\n * The judge's declared worst-case duration for one accepted submission (build timeout plus the\n * time limit of every test case). The check allows at least that plus a fixed overhead; per-case\n * process startup and custom judge callbacks are not budgeted separately.\n */\n expectedMaxDurationMs?: number;\n}\n\nexport interface ProblemDirIsolationCheckResult {\n passed: boolean;\n}\n\n/**\n * Check that a judge can run after only the problem directory is copied elsewhere.\n */\nexport async function checkProblemDirIsolation(\n problemDir: string,\n resolvedCwd: ResolvedCwd,\n params: unknown,\n options: ProblemDirIsolationCheckOptions = {}\n): Promise<ProblemDirIsolationCheckResult> {\n const timeoutMs = Math.max(\n ISOLATION_CHECK_MIN_TIMEOUT_MS,\n (options.expectedMaxDurationMs ?? 0) + ISOLATION_CHECK_OVERHEAD_MS\n );\n let tempRoot: string | undefined;\n try {\n const copyResult = await copyProblemDirToTemporaryRoot(problemDir);\n tempRoot = copyResult.tempRoot;\n const { copiedProblemDir } = copyResult;\n const absoluteProblemDir = path.resolve(problemDir);\n\n const relativeCwd = path.relative(absoluteProblemDir, path.resolve(resolvedCwd.cwd));\n const copiedCwd = path.join(copiedProblemDir, relativeCwd);\n const scriptPath = getInvokedScriptPath(absoluteProblemDir);\n if (scriptPath.startsWith('..') || path.isAbsolute(scriptPath)) {\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check skipped',\n '',\n 'The invoked judge script is located outside the problem directory.',\n `Script path: ${scriptPath}`,\n ]);\n return { passed: true };\n }\n const execArgv = process.execArgv.filter(isIsolationExecArg);\n const paramsJson = JSON.stringify(isRecord(params) ? params : {});\n const result = await runHarnessProcess([...execArgv, scriptPath, copiedCwd, paramsJson], {\n cwd: copiedProblemDir,\n env: process.env,\n timeoutMs,\n maxOutputBytes: ISOLATION_CHECK_MAX_OUTPUT_BYTES,\n tempRoot,\n });\n\n if (result.exitCode === 0 && isAcceptedJudgeOutput(result.stdout)) {\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check passed',\n '',\n `Copied problem dir : ${copiedProblemDir}`,\n `Checked cwd : ${relativeCwd}`,\n ]);\n return { passed: true };\n }\n\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check failed',\n '',\n ...describeFailure(result, timeoutMs, options),\n '',\n `Copied problem dir : ${copiedProblemDir}`,\n `Checked cwd : ${relativeCwd}`,\n `Exit status : ${result.exitCode ?? result.signal ?? 'unknown'}`,\n `Failure reason : ${result.failureReason ?? '<none>'}`,\n '',\n 'stdout:',\n result.stdout.trimEnd() || '<empty>',\n '',\n 'stderr:',\n result.stderr.trimEnd() || '<empty>',\n ]);\n return { passed: false };\n } catch (error) {\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check failed due to an unexpected error',\n '',\n getErrorMessage(error),\n ]);\n return { passed: false };\n } finally {\n // Cleanup failures must not mask the primary isolation check result.\n if (tempRoot) await forciblyRemoveDirectory(tempRoot);\n }\n}\n\nfunction describeFailure(\n result: HarnessProcessResult,\n timeoutMs: number,\n options: ProblemDirIsolationCheckOptions\n): string[] {\n if (result.timedOut) {\n return [\n `The judge did not finish within ${timeoutMs / 1000} seconds after copying only the problem directory to a temporary location.`,\n ...(options.expectedMaxDurationMs === undefined\n ? [\n `This judge declares no run time, so the check uses its fixed ${ISOLATION_CHECK_MIN_TIMEOUT_MS / 1000}-second budget.`,\n ]\n : [\n `The budget is the larger of ${ISOLATION_CHECK_MIN_TIMEOUT_MS / 1000} seconds and the build timeout plus the time limit of every test case plus a ${ISOLATION_CHECK_OVERHEAD_MS / 1000}-second overhead.`,\n 'Check timeLimitMs, the number of test cases, and one-off startup costs such as cold caches.',\n ]),\n ];\n }\n if (result.failureReason !== undefined) {\n return ['The judge was stopped before it finished; see the failure reason below.'];\n }\n if (result.exitCode === undefined) {\n return [\n `The judge was terminated by ${result.signal ?? 'an unknown signal'} (e.g. an out-of-memory kill or a cancelled job).`,\n ];\n }\n return [\n 'The judge did not complete successfully after copying only the problem directory to a temporary location.',\n 'Make sure judge.ts imports only files included in the problem directory.',\n ];\n}\n\nfunction isIsolationExecArg(arg: string): boolean {\n return !arg.startsWith('--inspect') && !arg.startsWith('--watch') && !arg.startsWith('--hot');\n}\n\nfunction getInvokedScriptPath(problemDir: string): string {\n const scriptPath = process.argv[1];\n if (!scriptPath) return `.${path.sep}judge.ts`;\n const relativeScriptPath = path.relative(problemDir, path.resolve(scriptPath));\n if (path.isAbsolute(relativeScriptPath)) return relativeScriptPath;\n return relativeScriptPath.startsWith('.') ? relativeScriptPath : `.${path.sep}${relativeScriptPath}`;\n}\n\nfunction isAcceptedJudgeOutput(stdout: string): boolean {\n const resultLines = stdout.split(/\\r?\\n/).filter((line) => line.startsWith(TEST_CASE_RESULT_PREFIX));\n if (resultLines.length === 0) return false;\n\n return resultLines.every((line) => {\n try {\n const parsedResult = testCaseResultSchema.safeParse(JSON.parse(line.slice(TEST_CASE_RESULT_PREFIX.length)));\n return parsedResult.success && parsedResult.data.decisionCode === DecisionCode.ACCEPTED;\n } catch {\n return false;\n }\n });\n}\n"],"mappings":"2VAWA,MAAM,EAAiC,IAEjC,EAA8B,IAmBpC,eAAsB,EACpB,EACA,EACA,EACA,EAA2C,CAAC,EACH,CACzC,IAAM,EAAY,KAAK,IACrB,GACC,EAAQ,uBAAyB,GAAK,CACzC,EACI,EACJ,GAAI,CACF,IAAM,EAAa,MAAMA,EAAAA,8BAA8B,CAAU,EACjE,EAAW,EAAW,SACtB,GAAM,CAAE,oBAAqB,EACvB,EAAqBC,EAAAA,QAAK,QAAQ,CAAU,EAE5C,EAAcA,EAAAA,QAAK,SAAS,EAAoBA,EAAAA,QAAK,QAAQ,EAAY,GAAG,CAAC,EAC7E,EAAYA,EAAAA,QAAK,KAAK,EAAkB,CAAW,EACnD,EAAa,EAAqB,CAAkB,EAC1D,GAAI,EAAW,WAAW,IAAI,GAAKA,EAA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BACvL,6FACF,CACN,EAEE,EAAO,gBAAkB,IAAA,GAGzB,EAAO,WAAa,IAAA,GACf,CACL,+BAA+B,EAAO,QAAU,oBAAoB,kDACtE,EAEK,CACL,4GACA,0EACF,EAVS,CAAC,yEAAyE,CAWrF,CAEA,SAAS,EAAmB,EAAsB,CAChD,MAAO,CAAC,EAAI,WAAW,WAAW,GAAK,CAAC,EAAI,WAAW,SAAS,GAAK,CAAC,EAAI,WAAW,OAAO,CAC9F,CAEA,SAAS,EAAqB,EAA4B,CACxD,IAAM,EAAa,QAAQ,KAAK,GAChC,GAAI,CAAC,EAAY,MAAO,IAAIJ,EAAAA,QAAK,IAAI,UACrC,IAAM,EAAqBA,EAAAA,QAAK,SAAS,EAAYA,EAAAA,QAAK,QAAQ,CAAU,CAAC,EAE7E,OADIA,EAAAA,QAAK,WAAW,CAAkB,GAC/B,EAAmB,WAAW,GAAG,EADQ,EACiB,IAAIA,EAAAA,QAAK,MAAM,GAClF,CAEA,SAAS,EAAsB,EAAyB,CACtD,IAAM,EAAc,EAAO,MAAM,OAAO,CAAC,CAAC,OAAQ,GAAS,EAAK,WAAWK,EAAAA,uBAAuB,CAAC,EAGnG,OAFI,EAAY,SAAW,GAEpB,EAAY,MAAO,GAAS,CACjC,GAAI,CACF,IAAM,EAAeC,EAAAA,qBAAqB,UAAU,KAAK,MAAM,EAAK,MAAMD,EAAAA,wBAAwB,MAAM,CAAC,CAAC,EAC1G,OAAO,EAAa,SAAW,EAAa,KAAK,eAAiBE,EAAAA,aAAa,QACjF,MAAQ,CACN,MAAO,EACT,CACF,CAAC,CACH"}
@@ -1,2 +1,2 @@
1
- import{TEST_CASE_RESULT_PREFIX as e,testCaseResultSchema as t}from"../types/testCaseResult.js";import{copyProblemDirToTemporaryRoot as n,forciblyRemoveDirectory as r}from"./temporaryProblemDirCopy.js";import{DecisionCode as i}from"../types/decisionCode.js";import{printDebugBanner as a}from"./printDebugBanner.js";import{runHarnessProcess as o}from"./runHarnessProcess.js";import s from"node:path";const c=3e4,l=1e4;async function u(e,t,i,u={}){let g=Math.max(c,(u.expectedMaxDurationMs??0)+l),_;try{let r=await n(e);_=r.tempRoot;let{copiedProblemDir:c}=r,l=s.resolve(e),v=s.relative(l,s.resolve(t.cwd)),y=s.join(c,v),b=m(l);if(b.startsWith(`..`)||s.isAbsolute(b))return a([`[DEBUG MODE] isolated problem directory check skipped`,``,`The invoked judge script is located outside the problem directory.`,`Script path: ${b}`]),{passed:!0};let x=process.execArgv.filter(f),S=JSON.stringify(p(i)?i:{}),C=await o([...x,b,y,S],{cwd:c,env:process.env,timeoutMs:g,maxOutputBytes:67108864,tempRoot:_});return C.exitCode===0&&h(C.stdout)?(a([`[DEBUG MODE] isolated problem directory check passed`,``,`Copied problem dir : ${c}`,`Checked cwd : ${v}`]),{passed:!0}):(a([`[DEBUG MODE] isolated problem directory check failed`,``,...d(C,g,u),``,`Copied problem dir : ${c}`,`Checked cwd : ${v}`,`Exit status : ${C.exitCode??C.signal??`unknown`}`,`Failure reason : ${C.failureReason??`<none>`}`,``,`stdout:`,C.stdout.trimEnd()||`<empty>`,``,`stderr:`,C.stderr.trimEnd()||`<empty>`]),{passed:!1})}catch(e){return a([`[DEBUG MODE] isolated problem directory check failed due to an unexpected error`,``,e instanceof Error?e.message:String(e)]),{passed:!1}}finally{_&&await r(_)}}function d(e,t,n){return e.timedOut?[`The judge did not finish within ${t/1e3} seconds after copying only the problem directory to a temporary location.`,...n.expectedMaxDurationMs===void 0?[`This judge declares no run time, so the check uses its fixed ${c/1e3}-second budget.`]:[`The budget is the larger of ${c/1e3} seconds and the build timeout plus the time limit of every test case plus a ${l/1e3}-second overhead.`,`Check timeLimitMs, the number of test cases, and one-off startup costs such as cold caches.`]]:e.failureReason===void 0?e.exitCode===void 0?[`The judge was terminated by ${e.signal??`an unknown signal`} (e.g. an out-of-memory kill or a cancelled job).`]:[`The judge did not complete successfully after copying only the problem directory to a temporary location.`,`Make sure judge.ts imports only files included in the problem directory.`]:[`The judge was stopped before it finished; see the failure reason below.`]}function f(e){return!e.startsWith(`--inspect`)&&!e.startsWith(`--watch`)&&!e.startsWith(`--hot`)}function p(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function m(e){let t=process.argv[1];if(!t)return`.${s.sep}judge.ts`;let n=s.relative(e,s.resolve(t));return s.isAbsolute(n)||n.startsWith(`.`)?n:`.${s.sep}${n}`}function h(n){let r=n.split(/\r?\n/).filter(t=>t.startsWith(e));return r.length!==0&&r.every(n=>{try{let r=t.safeParse(JSON.parse(n.slice(e.length)));return r.success&&r.data.decisionCode===i.ACCEPTED}catch{return!1}})}export{u as checkProblemDirIsolation};
1
+ import{TEST_CASE_RESULT_PREFIX as e,testCaseResultSchema as t}from"../types/testCaseResult.js";import{copyProblemDirToTemporaryRoot as n,forciblyRemoveDirectory as r}from"./temporaryProblemDirCopy.js";import{DecisionCode as i}from"../types/decisionCode.js";import{printDebugBanner as a}from"./printDebugBanner.js";import{runHarnessProcess as o}from"./runHarnessProcess.js";import s from"node:path";import{getErrorMessage as c,isRecord as l}from"@willbooster/shared-lib";const u=3e4,d=1e4;async function f(e,t,i,f={}){let _=Math.max(u,(f.expectedMaxDurationMs??0)+d),v;try{let r=await n(e);v=r.tempRoot;let{copiedProblemDir:c}=r,u=s.resolve(e),d=s.relative(u,s.resolve(t.cwd)),y=s.join(c,d),b=h(u);if(b.startsWith(`..`)||s.isAbsolute(b))return a([`[DEBUG MODE] isolated problem directory check skipped`,``,`The invoked judge script is located outside the problem directory.`,`Script path: ${b}`]),{passed:!0};let x=process.execArgv.filter(m),S=JSON.stringify(l(i)?i:{}),C=await o([...x,b,y,S],{cwd:c,env:process.env,timeoutMs:_,maxOutputBytes:67108864,tempRoot:v});return C.exitCode===0&&g(C.stdout)?(a([`[DEBUG MODE] isolated problem directory check passed`,``,`Copied problem dir : ${c}`,`Checked cwd : ${d}`]),{passed:!0}):(a([`[DEBUG MODE] isolated problem directory check failed`,``,...p(C,_,f),``,`Copied problem dir : ${c}`,`Checked cwd : ${d}`,`Exit status : ${C.exitCode??C.signal??`unknown`}`,`Failure reason : ${C.failureReason??`<none>`}`,``,`stdout:`,C.stdout.trimEnd()||`<empty>`,``,`stderr:`,C.stderr.trimEnd()||`<empty>`]),{passed:!1})}catch(e){return a([`[DEBUG MODE] isolated problem directory check failed due to an unexpected error`,``,c(e)]),{passed:!1}}finally{v&&await r(v)}}function p(e,t,n){return e.timedOut?[`The judge did not finish within ${t/1e3} seconds after copying only the problem directory to a temporary location.`,...n.expectedMaxDurationMs===void 0?[`This judge declares no run time, so the check uses its fixed ${u/1e3}-second budget.`]:[`The budget is the larger of ${u/1e3} seconds and the build timeout plus the time limit of every test case plus a ${d/1e3}-second overhead.`,`Check timeLimitMs, the number of test cases, and one-off startup costs such as cold caches.`]]:e.failureReason===void 0?e.exitCode===void 0?[`The judge was terminated by ${e.signal??`an unknown signal`} (e.g. an out-of-memory kill or a cancelled job).`]:[`The judge did not complete successfully after copying only the problem directory to a temporary location.`,`Make sure judge.ts imports only files included in the problem directory.`]:[`The judge was stopped before it finished; see the failure reason below.`]}function m(e){return!e.startsWith(`--inspect`)&&!e.startsWith(`--watch`)&&!e.startsWith(`--hot`)}function h(e){let t=process.argv[1];if(!t)return`.${s.sep}judge.ts`;let n=s.relative(e,s.resolve(t));return s.isAbsolute(n)||n.startsWith(`.`)?n:`.${s.sep}${n}`}function g(n){let r=n.split(/\r?\n/).filter(t=>t.startsWith(e));return r.length!==0&&r.every(n=>{try{let r=t.safeParse(JSON.parse(n.slice(e.length)));return r.success&&r.data.decisionCode===i.ACCEPTED}catch{return!1}})}export{f as checkProblemDirIsolation};
2
2
  //# sourceMappingURL=checkProblemDirIsolation.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"checkProblemDirIsolation.js","names":[],"sources":["../../src/helpers/checkProblemDirIsolation.ts"],"sourcesContent":["import path from 'node:path';\n\nimport { DecisionCode } from '../types/decisionCode.js';\nimport { TEST_CASE_RESULT_PREFIX, testCaseResultSchema } from '../types/testCaseResult.js';\n\nimport { printDebugBanner } from './printDebugBanner.js';\nimport type { ResolvedCwd } from './resolveCwds.js';\nimport { type HarnessProcessResult, runHarnessProcess } from './runHarnessProcess.js';\nimport { copyProblemDirToTemporaryRoot, forciblyRemoveDirectory } from './temporaryProblemDirCopy.js';\n\nconst ISOLATION_CHECK_MIN_TIMEOUT_MS = 30_000;\n// Starting bun and printing results are not covered by the judge's own limits.\nconst ISOLATION_CHECK_OVERHEAD_MS = 10_000;\nconst ISOLATION_CHECK_MAX_OUTPUT_BYTES = 64 * 1024 * 1024;\n\nexport interface ProblemDirIsolationCheckOptions {\n /**\n * The judge's declared worst-case duration for one accepted submission (build timeout plus the\n * time limit of every test case). The check allows at least that plus a fixed overhead; per-case\n * process startup and custom judge callbacks are not budgeted separately.\n */\n expectedMaxDurationMs?: number;\n}\n\nexport interface ProblemDirIsolationCheckResult {\n passed: boolean;\n}\n\n/**\n * Check that a judge can run after only the problem directory is copied elsewhere.\n */\nexport async function checkProblemDirIsolation(\n problemDir: string,\n resolvedCwd: ResolvedCwd,\n params: unknown,\n options: ProblemDirIsolationCheckOptions = {}\n): Promise<ProblemDirIsolationCheckResult> {\n const timeoutMs = Math.max(\n ISOLATION_CHECK_MIN_TIMEOUT_MS,\n (options.expectedMaxDurationMs ?? 0) + ISOLATION_CHECK_OVERHEAD_MS\n );\n let tempRoot: string | undefined;\n try {\n const copyResult = await copyProblemDirToTemporaryRoot(problemDir);\n tempRoot = copyResult.tempRoot;\n const { copiedProblemDir } = copyResult;\n const absoluteProblemDir = path.resolve(problemDir);\n\n const relativeCwd = path.relative(absoluteProblemDir, path.resolve(resolvedCwd.cwd));\n const copiedCwd = path.join(copiedProblemDir, relativeCwd);\n const scriptPath = getInvokedScriptPath(absoluteProblemDir);\n if (scriptPath.startsWith('..') || path.isAbsolute(scriptPath)) {\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check skipped',\n '',\n 'The invoked judge script is located outside the problem directory.',\n `Script path: ${scriptPath}`,\n ]);\n return { passed: true };\n }\n const execArgv = process.execArgv.filter(isIsolationExecArg);\n const paramsJson = JSON.stringify(isJudgeParamsObject(params) ? params : {});\n const result = await runHarnessProcess([...execArgv, scriptPath, copiedCwd, paramsJson], {\n cwd: copiedProblemDir,\n env: process.env,\n timeoutMs,\n maxOutputBytes: ISOLATION_CHECK_MAX_OUTPUT_BYTES,\n tempRoot,\n });\n\n if (result.exitCode === 0 && isAcceptedJudgeOutput(result.stdout)) {\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check passed',\n '',\n `Copied problem dir : ${copiedProblemDir}`,\n `Checked cwd : ${relativeCwd}`,\n ]);\n return { passed: true };\n }\n\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check failed',\n '',\n ...describeFailure(result, timeoutMs, options),\n '',\n `Copied problem dir : ${copiedProblemDir}`,\n `Checked cwd : ${relativeCwd}`,\n `Exit status : ${result.exitCode ?? result.signal ?? 'unknown'}`,\n `Failure reason : ${result.failureReason ?? '<none>'}`,\n '',\n 'stdout:',\n result.stdout.trimEnd() || '<empty>',\n '',\n 'stderr:',\n result.stderr.trimEnd() || '<empty>',\n ]);\n return { passed: false };\n } catch (error) {\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check failed due to an unexpected error',\n '',\n error instanceof Error ? error.message : String(error),\n ]);\n return { passed: false };\n } finally {\n // Cleanup failures must not mask the primary isolation check result.\n if (tempRoot) await forciblyRemoveDirectory(tempRoot);\n }\n}\n\nfunction describeFailure(\n result: HarnessProcessResult,\n timeoutMs: number,\n options: ProblemDirIsolationCheckOptions\n): string[] {\n if (result.timedOut) {\n return [\n `The judge did not finish within ${timeoutMs / 1000} seconds after copying only the problem directory to a temporary location.`,\n ...(options.expectedMaxDurationMs === undefined\n ? [\n `This judge declares no run time, so the check uses its fixed ${ISOLATION_CHECK_MIN_TIMEOUT_MS / 1000}-second budget.`,\n ]\n : [\n `The budget is the larger of ${ISOLATION_CHECK_MIN_TIMEOUT_MS / 1000} seconds and the build timeout plus the time limit of every test case plus a ${ISOLATION_CHECK_OVERHEAD_MS / 1000}-second overhead.`,\n 'Check timeLimitMs, the number of test cases, and one-off startup costs such as cold caches.',\n ]),\n ];\n }\n if (result.failureReason !== undefined) {\n return ['The judge was stopped before it finished; see the failure reason below.'];\n }\n if (result.exitCode === undefined) {\n return [\n `The judge was terminated by ${result.signal ?? 'an unknown signal'} (e.g. an out-of-memory kill or a cancelled job).`,\n ];\n }\n return [\n 'The judge did not complete successfully after copying only the problem directory to a temporary location.',\n 'Make sure judge.ts imports only files included in the problem directory.',\n ];\n}\n\nfunction isIsolationExecArg(arg: string): boolean {\n return !arg.startsWith('--inspect') && !arg.startsWith('--watch') && !arg.startsWith('--hot');\n}\n\nfunction isJudgeParamsObject(params: unknown): params is object {\n return params !== undefined && params !== null && typeof params === 'object' && !Array.isArray(params);\n}\n\nfunction getInvokedScriptPath(problemDir: string): string {\n const scriptPath = process.argv[1];\n if (!scriptPath) return `.${path.sep}judge.ts`;\n const relativeScriptPath = path.relative(problemDir, path.resolve(scriptPath));\n if (path.isAbsolute(relativeScriptPath)) return relativeScriptPath;\n return relativeScriptPath.startsWith('.') ? relativeScriptPath : `.${path.sep}${relativeScriptPath}`;\n}\n\nfunction isAcceptedJudgeOutput(stdout: string): boolean {\n const resultLines = stdout.split(/\\r?\\n/).filter((line) => line.startsWith(TEST_CASE_RESULT_PREFIX));\n if (resultLines.length === 0) return false;\n\n return resultLines.every((line) => {\n try {\n const parsedResult = testCaseResultSchema.safeParse(JSON.parse(line.slice(TEST_CASE_RESULT_PREFIX.length)));\n return parsedResult.success && parsedResult.data.decisionCode === DecisionCode.ACCEPTED;\n } catch {\n return false;\n }\n });\n}\n"],"mappings":"8YAUA,MAAM,EAAiC,IAEjC,EAA8B,IAmBpC,eAAsB,EACpB,EACA,EACA,EACA,EAA2C,CAAC,EACH,CACzC,IAAM,EAAY,KAAK,IACrB,GACC,EAAQ,uBAAyB,GAAK,CACzC,EACI,EACJ,GAAI,CAC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ProblemDirIsolation.js","names":[],"sources":["../../src/helpers/checkProblemDirIsolation.ts"],"sourcesContent":["import path from 'node:path';\nimport { getErrorMessage, isRecord } from '@willbooster/shared-lib';\n\nimport { DecisionCode } from '../types/decisionCode.js';\nimport { TEST_CASE_RESULT_PREFIX, testCaseResultSchema } from '../types/testCaseResult.js';\n\nimport { printDebugBanner } from './printDebugBanner.js';\nimport type { ResolvedCwd } from './resolveCwds.js';\nimport { type HarnessProcessResult, runHarnessProcess } from './runHarnessProcess.js';\nimport { copyProblemDirToTemporaryRoot, forciblyRemoveDirectory } from './temporaryProblemDirCopy.js';\n\nconst ISOLATION_CHECK_MIN_TIMEOUT_MS = 30_000;\n// Starting bun and printing results are not covered by the judge's own limits.\nconst ISOLATION_CHECK_OVERHEAD_MS = 10_000;\nconst ISOLATION_CHECK_MAX_OUTPUT_BYTES = 64 * 1024 * 1024;\n\nexport interface ProblemDirIsolationCheckOptions {\n /**\n * The judge's declared worst-case duration for one accepted submission (build timeout plus the\n * time limit of every test case). The check allows at least that plus a fixed overhead; per-case\n * process startup and custom judge callbacks are not budgeted separately.\n */\n expectedMaxDurationMs?: number;\n}\n\nexport interface ProblemDirIsolationCheckResult {\n passed: boolean;\n}\n\n/**\n * Check that a judge can run after only the problem directory is copied elsewhere.\n */\nexport async function checkProblemDirIsolation(\n problemDir: string,\n resolvedCwd: ResolvedCwd,\n params: unknown,\n options: ProblemDirIsolationCheckOptions = {}\n): Promise<ProblemDirIsolationCheckResult> {\n const timeoutMs = Math.max(\n ISOLATION_CHECK_MIN_TIMEOUT_MS,\n (options.expectedMaxDurationMs ?? 0) + ISOLATION_CHECK_OVERHEAD_MS\n );\n let tempRoot: string | undefined;\n try {\n const copyResult = await copyProblemDirToTemporaryRoot(problemDir);\n tempRoot = copyResult.tempRoot;\n const { copiedProblemDir } = copyResult;\n const absoluteProblemDir = path.resolve(problemDir);\n\n const relativeCwd = path.relative(absoluteProblemDir, path.resolve(resolvedCwd.cwd));\n const copiedCwd = path.join(copiedProblemDir, relativeCwd);\n const scriptPath = getInvokedScriptPath(absoluteProblemDir);\n if (scriptPath.startsWith('..') || path.isAbsolute(scriptPath)) {\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check skipped',\n '',\n 'The invoked judge script is located outside the problem directory.',\n `Script path: ${scriptPath}`,\n ]);\n return { passed: true };\n }\n const execArgv = process.execArgv.filter(isIsolationExecArg);\n const paramsJson = JSON.stringify(isRecord(params) ? params : {});\n const result = await runHarnessProcess([...execArgv, scriptPath, copiedCwd, paramsJson], {\n cwd: copiedProblemDir,\n env: process.env,\n timeoutMs,\n maxOutputBytes: ISOLATION_CHECK_MAX_OUTPUT_BYTES,\n tempRoot,\n });\n\n if (result.exitCode === 0 && isAcceptedJudgeOutput(result.stdout)) {\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check passed',\n '',\n `Copied problem dir : ${copiedProblemDir}`,\n `Checked cwd : ${relativeCwd}`,\n ]);\n return { passed: true };\n }\n\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check failed',\n '',\n ...describeFailure(result, timeoutMs, options),\n '',\n `Copied problem dir : ${copiedProblemDir}`,\n `Checked cwd : ${relativeCwd}`,\n `Exit status : ${result.exitCode ?? result.signal ?? 'unknown'}`,\n `Failure reason : ${result.failureReason ?? '<none>'}`,\n '',\n 'stdout:',\n result.stdout.trimEnd() || '<empty>',\n '',\n 'stderr:',\n result.stderr.trimEnd() || '<empty>',\n ]);\n return { passed: false };\n } catch (error) {\n printDebugBanner([\n '[DEBUG MODE] isolated problem directory check failed due to an unexpected error',\n '',\n getErrorMessage(error),\n ]);\n return { passed: false };\n } finally {\n // Cleanup failures must not mask the primary isolation check result.\n if (tempRoot) await forciblyRemoveDirectory(tempRoot);\n }\n}\n\nfunction describeFailure(\n result: HarnessProcessResult,\n timeoutMs: number,\n options: ProblemDirIsolationCheckOptions\n): string[] {\n if (result.timedOut) {\n return [\n `The judge did not finish within ${timeoutMs / 1000} seconds after copying only the problem directory to a temporary location.`,\n ...(options.expectedMaxDurationMs === undefined\n ? [\n `This judge declares no run time, so the check uses its fixed ${ISOLATION_CHECK_MIN_TIMEOUT_MS / 1000}-second budget.`,\n ]\n : [\n `The budget is the larger of ${ISOLATION_CHECK_MIN_TIMEOUT_MS / 1000} seconds and the build timeout plus the time limit of every test case plus a ${ISOLATION_CHECK_OVERHEAD_MS / 1000}-second overhead.`,\n 'Check timeLimitMs, the number of test cases, and one-off startup costs such as cold caches.',\n ]),\n ];\n }\n if (result.failureReason !== undefined) {\n return ['The judge was stopped before it finished; see the failure reason below.'];\n }\n if (result.exitCode === undefined) {\n return [\n `The judge was terminated by ${result.signal ?? 'an unknown signal'} (e.g. an out-of-memory kill or a cancelled job).`,\n ];\n }\n return [\n 'The judge did not complete successfully after copying only the problem directory to a temporary location.',\n 'Make sure judge.ts imports only files included in the problem directory.',\n ];\n}\n\nfunction isIsolationExecArg(arg: string): boolean {\n return !arg.startsWith('--inspect') && !arg.startsWith('--watch') && !arg.startsWith('--hot');\n}\n\nfunction getInvokedScriptPath(problemDir: string): string {\n const scriptPath = process.argv[1];\n if (!scriptPath) return `.${path.sep}judge.ts`;\n const relativeScriptPath = path.relative(problemDir, path.resolve(scriptPath));\n if (path.isAbsolute(relativeScriptPath)) return relativeScriptPath;\n return relativeScriptPath.startsWith('.') ? relativeScriptPath : `.${path.sep}${relativeScriptPath}`;\n}\n\nfunction isAcceptedJudgeOutput(stdout: string): boolean {\n const resultLines = stdout.split(/\\r?\\n/).filter((line) => line.startsWith(TEST_CASE_RESULT_PREFIX));\n if (resultLines.length === 0) return false;\n\n return resultLines.every((line) => {\n try {\n const parsedResult = testCaseResultSchema.safeParse(JSON.parse(line.slice(TEST_CASE_RESULT_PREFIX.length)));\n return parsedResult.success && parsedResult.data.decisionCode === DecisionCode.ACCEPTED;\n } catch {\n return false;\n }\n });\n}\n"],"mappings":"sdAWA,MAAM,EAAiC,IAEjC,EAA8B,IAmBpC,eAAsB,EACpB,EACA,EACA,EACA,EAA2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/runtime.cjs");let t=require("node:path");t=e.__toESM(t,1);let n=require("node:fs/promises");n=e.__toESM(n,1);const r=`model_answers`,i=`model_answers.fails`;async function a(e){return s(e,r)}async function o(e){return s(e,i)}async function s(e,r){let i=t.default.join(e,r),a;try{a=await n.default.readdir(i,{withFileTypes:!0})}catch(e){if(e.code===`ENOENT`)return[];throw e}return a.filter(e=>e.isDirectory()).map(e=>t.default.join(i,e.name)).toSorted()}exports.FAILING_MODEL_ANSWERS_DIRNAME=i,exports.MODEL_ANSWERS_DIRNAME=r,exports.findFailingModelAnswerDirs=o,exports.findModelAnswerDirs=a;
1
+ "use strict";const e=require("../_virtual/_rolldown/runtime.cjs");let t=require("node:path");t=e.__toESM(t,1);let n=require("node:fs/promises");n=e.__toESM(n,1);let r=require("@willbooster/shared-lib");const i=`model_answers`,a=`model_answers.fails`;async function o(e){return c(e,i)}async function s(e){return c(e,a)}async function c(e,i){let a=t.default.join(e,i),o;try{o=await n.default.readdir(a,{withFileTypes:!0})}catch(e){if((0,r.hasErrorCode)(e,`ENOENT`))return[];throw e}return o.filter(e=>e.isDirectory()).map(e=>t.default.join(a,e.name)).toSorted()}exports.FAILING_MODEL_ANSWERS_DIRNAME=a,exports.MODEL_ANSWERS_DIRNAME=i,exports.findFailingModelAnswerDirs=s,exports.findModelAnswerDirs=o;
2
2
  //# sourceMappingURL=findModelAnswerDirs.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"findModelAnswerDirs.cjs","names":["path","fs"],"sources":["../../src/helpers/findModelAnswerDirs.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\n\nexport const MODEL_ANSWERS_DIRNAME = 'model_answers';\nexport const FAILING_MODEL_ANSWERS_DIRNAME = 'model_answers.fails';\n\n/**\n * Find model answer directories under `<problemDir>/model_answers/`.\n * Returns absolute paths sorted lexicographically.\n */\nexport async function findModelAnswerDirs(problemDir: string): Promise<string[]> {\n return findAnswerDirs(problemDir, MODEL_ANSWERS_DIRNAME);\n}\n\n/**\n * Find failing model answer directories under `<problemDir>/model_answers.fails/`.\n * Returns absolute paths sorted lexicographically.\n */\nexport async function findFailingModelAnswerDirs(problemDir: string): Promise<string[]> {\n return findAnswerDirs(problemDir, FAILING_MODEL_ANSWERS_DIRNAME);\n}\n\nasync function findAnswerDirs(problemDir: string, dirname: string): Promise<string[]> {\n const modelAnswersDir = path.join(problemDir, dirname);\n let entries;\n try {\n entries = await fs.readdir(modelAnswersDir, { withFileTypes: true });\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') return [];\n throw error;\n }\n return entries\n .filter((entry) => entry.isDirectory())\n .map((entry) => path.join(modelAnswersDir, entry.name))\n .toSorted();\n}\n"],"mappings":"iKAGA,MAAa,EAAwB,gBACxB,EAAgC,sBAM7C,eAAsB,EAAoB,EAAuC,CAC/E,OAAO,EAAe,EAAY,CAAqB,CACzD,CAMA,eAAsB,EAA2B,EAAuC,CACtF,OAAO,EAAe,EAAY,CAA6B,CACjE,CAEA,eAAe,EAAe,EAAoB,EAAoC,CACpF,IAAM,EAAkBA,EAAAA,QAAK,KAAK,EAAY,CAAO,EACjD,EACJ,GAAI,CACF,EAAU,MAAMC,EAAAA,QAAG,QAAQ,EAAiB,CAAE,cAAe,EAAK,CAAC,CACrE,OAAS,EAAO,CACd,GAAK,EAAgC,OAAS,SAAU,MAAO,CAAC,EAChE,MAAM,CACR,CACA,OAAO,EACJ,OAAQ,GAAU,EAAM,YAAY,CAAC,CAAC,CACtC,IAAK,GAAUD,EAAAA,QAAK,KAAK,EAAiB,EAAM,IAAI,CAAC,CAAC,CACtD,SAAS,CACd"}
1
+ {"version":3,"file":"findModelAnswerDirs.cjs","names":["path","fs","hasErrorCode"],"sources":["../../src/helpers/findModelAnswerDirs.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\nimport { hasErrorCode } from '@willbooster/shared-lib';\n\nexport const MODEL_ANSWERS_DIRNAME = 'model_answers';\nexport const FAILING_MODEL_ANSWERS_DIRNAME = 'model_answers.fails';\n\n/**\n * Find model answer directories under `<problemDir>/model_answers/`.\n * Returns absolute paths sorted lexicographically.\n */\nexport async function findModelAnswerDirs(problemDir: string): Promise<string[]> {\n return findAnswerDirs(problemDir, MODEL_ANSWERS_DIRNAME);\n}\n\n/**\n * Find failing model answer directories under `<problemDir>/model_answers.fails/`.\n * Returns absolute paths sorted lexicographically.\n */\nexport async function findFailingModelAnswerDirs(problemDir: string): Promise<string[]> {\n return findAnswerDirs(problemDir, FAILING_MODEL_ANSWERS_DIRNAME);\n}\n\nasync function findAnswerDirs(problemDir: string, dirname: string): Promise<string[]> {\n const modelAnswersDir = path.join(problemDir, dirname);\n let entries;\n try {\n entries = await fs.readdir(modelAnswersDir, { withFileTypes: true });\n } catch (error) {\n if (hasErrorCode(error, 'ENOENT')) return [];\n throw error;\n }\n return entries\n .filter((entry) => entry.isDirectory())\n .map((entry) => path.join(modelAnswersDir, entry.name))\n .toSorted();\n}\n"],"mappings":"0MAIA,MAAa,EAAwB,gBACxB,EAAgC,sBAM7C,eAAsB,EAAoB,EAAuC,CAC/E,OAAO,EAAe,EAAY,CAAqB,CACzD,CAMA,eAAsB,EAA2B,EAAuC,CACtF,OAAO,EAAe,EAAY,CAA6B,CACjE,CAEA,eAAe,EAAe,EAAoB,EAAoC,CACpF,IAAM,EAAkBA,EAAAA,QAAK,KAAK,EAAY,CAAO,EACjD,EACJ,GAAI,CACF,EAAU,MAAMC,EAAAA,QAAG,QAAQ,EAAiB,CAAE,cAAe,EAAK,CAAC,CACrE,OAAS,EAAO,CACd,IAAA,EAAIC,EAAAA,aAAAA,CAAa,EAAO,QAAQ,EAAG,MAAO,CAAC,EAC3C,MAAM,CACR,CACA,OAAO,EACJ,OAAQ,GAAU,EAAM,YAAY,CAAC,CAAC,CACtC,IAAK,GAAUF,EAAAA,QAAK,KAAK,EAAiB,EAAM,IAAI,CAAC,CAAC,CACtD,SAAS,CACd"}
@@ -1,2 +1,2 @@
1
- import e from"node:path";import t from"node:fs/promises";const n=`model_answers`,r=`model_answers.fails`;async function i(e){return o(e,n)}async function a(e){return o(e,r)}async function o(n,r){let i=e.join(n,r),a;try{a=await t.readdir(i,{withFileTypes:!0})}catch(e){if(e.code===`ENOENT`)return[];throw e}return a.filter(e=>e.isDirectory()).map(t=>e.join(i,t.name)).toSorted()}export{r as FAILING_MODEL_ANSWERS_DIRNAME,n as MODEL_ANSWERS_DIRNAME,a as findFailingModelAnswerDirs,i as findModelAnswerDirs};
1
+ import e from"node:path";import t from"node:fs/promises";import{hasErrorCode as n}from"@willbooster/shared-lib";const r=`model_answers`,i=`model_answers.fails`;async function a(e){return s(e,r)}async function o(e){return s(e,i)}async function s(r,i){let a=e.join(r,i),o;try{o=await t.readdir(a,{withFileTypes:!0})}catch(e){if(n(e,`ENOENT`))return[];throw e}return o.filter(e=>e.isDirectory()).map(t=>e.join(a,t.name)).toSorted()}export{i as FAILING_MODEL_ANSWERS_DIRNAME,r as MODEL_ANSWERS_DIRNAME,o as findFailingModelAnswerDirs,a as findModelAnswerDirs};
2
2
  //# sourceMappingURL=findModelAnswerDirs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"findModelAnswerDirs.js","names":[],"sources":["../../src/helpers/findModelAnswerDirs.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\n\nexport const MODEL_ANSWERS_DIRNAME = 'model_answers';\nexport const FAILING_MODEL_ANSWERS_DIRNAME = 'model_answers.fails';\n\n/**\n * Find model answer directories under `<problemDir>/model_answers/`.\n * Returns absolute paths sorted lexicographically.\n */\nexport async function findModelAnswerDirs(problemDir: string): Promise<string[]> {\n return findAnswerDirs(problemDir, MODEL_ANSWERS_DIRNAME);\n}\n\n/**\n * Find failing model answer directories under `<problemDir>/model_answers.fails/`.\n * Returns absolute paths sorted lexicographically.\n */\nexport async function findFailingModelAnswerDirs(problemDir: string): Promise<string[]> {\n return findAnswerDirs(problemDir, FAILING_MODEL_ANSWERS_DIRNAME);\n}\n\nasync function findAnswerDirs(problemDir: string, dirname: string): Promise<string[]> {\n const modelAnswersDir = path.join(problemDir, dirname);\n let entries;\n try {\n entries = await fs.readdir(modelAnswersDir, { withFileTypes: true });\n } catch (error) {\n if ((error as NodeJS.ErrnoException).code === 'ENOENT') return [];\n throw error;\n }\n return entries\n .filter((entry) => entry.isDirectory())\n .map((entry) => path.join(modelAnswersDir, entry.name))\n .toSorted();\n}\n"],"mappings":"yDAGA,MAAa,EAAwB,gBACxB,EAAgC,sBAM7C,eAAsB,EAAoB,EAAuC,CAC/E,OAAO,EAAe,EAAY,CAAqB,CACzD,CAMA,eAAsB,EAA2B,EAAuC,CACtF,OAAO,EAAe,EAAY,CAA6B,CACjE,CAEA,eAAe,EAAe,EAAoB,EAAoC,CACpF,IAAM,EAAkB,EAAK,KAAK,EAAY,CAAO,EACjD,EACJ,GAAI,CACF,EAAU,MAAM,EAAG,QAAQ,EAAiB,CAAE,cAAe,EAAK,CAAC,CACrE,OAAS,EAAO,CACd,GAAK,EAAgC,OAAS,SAAU,MAAO,CAAC,EAChE,MAAM,CACR,CACA,OAAO,EACJ,OAAQ,GAAU,EAAM,YAAY,CAAC,CAAC,CACtC,IAAK,GAAU,EAAK,KAAK,EAAiB,EAAM,IAAI,CAAC,CAAC,CACtD,SAAS,CACd"}
1
+ {"version":3,"file":"findModelAnswerDirs.js","names":[],"sources":["../../src/helpers/findModelAnswerDirs.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\nimport { hasErrorCode } from '@willbooster/shared-lib';\n\nexport const MODEL_ANSWERS_DIRNAME = 'model_answers';\nexport const FAILING_MODEL_ANSWERS_DIRNAME = 'model_answers.fails';\n\n/**\n * Find model answer directories under `<problemDir>/model_answers/`.\n * Returns absolute paths sorted lexicographically.\n */\nexport async function findModelAnswerDirs(problemDir: string): Promise<string[]> {\n return findAnswerDirs(problemDir, MODEL_ANSWERS_DIRNAME);\n}\n\n/**\n * Find failing model answer directories under `<problemDir>/model_answers.fails/`.\n * Returns absolute paths sorted lexicographically.\n */\nexport async function findFailingModelAnswerDirs(problemDir: string): Promise<string[]> {\n return findAnswerDirs(problemDir, FAILING_MODEL_ANSWERS_DIRNAME);\n}\n\nasync function findAnswerDirs(problemDir: string, dirname: string): Promise<string[]> {\n const modelAnswersDir = path.join(problemDir, dirname);\n let entries;\n try {\n entries = await fs.readdir(modelAnswersDir, { withFileTypes: true });\n } catch (error) {\n if (hasErrorCode(error, 'ENOENT')) return [];\n throw error;\n }\n return entries\n .filter((entry) => entry.isDirectory())\n .map((entry) => path.join(modelAnswersDir, entry.name))\n .toSorted();\n}\n"],"mappings":"gHAIA,MAAa,EAAwB,gBACxB,EAAgC,sBAM7C,eAAsB,EAAoB,EAAuC,CAC/E,OAAO,EAAe,EAAY,CAAqB,CACzD,CAMA,eAAsB,EAA2B,EAAuC,CACtF,OAAO,EAAe,EAAY,CAA6B,CACjE,CAEA,eAAe,EAAe,EAAoB,EAAoC,CACpF,IAAM,EAAkB,EAAK,KAAK,EAAY,CAAO,EACjD,EACJ,GAAI,CACF,EAAU,MAAM,EAAG,QAAQ,EAAiB,CAAE,cAAe,EAAK,CAAC,CACrE,OAAS,EAAO,CACd,GAAI,EAAa,EAAO,QAAQ,EAAG,MAAO,CAAC,EAC3C,MAAM,CACR,CACA,OAAO,EACJ,OAAQ,GAAU,EAAM,YAAY,CAAC,CAAC,CACtC,IAAK,GAAU,EAAK,KAAK,EAAiB,EAAM,IAAI,CAAC,CAAC,CACtD,SAAS,CACd"}
@@ -0,0 +1,2 @@
1
+ "use strict";const e=require("../_virtual/_rolldown/runtime.cjs");let t=require("node:crypto");t=e.__toESM(t,1);let n=require("node:zlib");n=e.__toESM(n,1);const r=Buffer.from(`89504e470d0a1a0a`,`hex`),i={0:1,2:3,3:1,4:2,6:4},a=65535;var o=class{#e=/* @__PURE__ */ new Map;add(e,n,r){let i=this.#e.get(e)??{path:``,frames:[],lastDigest:``,changeCount:0,keptChangeInterval:1,lastSeenAtMs:0,isTooLarge:!1};this.#e.set(e,i),i.path=n.path.replace(/\.png$/,`_recording.png`),i.lastSeenAtMs=r;let a=t.default.createHash(`sha256`).update(n.data).digest(`base64`);if(i.lastDigest===a)return;i.lastDigest=a;let o=i.changeCount++;i.isTooLarge||o%i.keptChangeInterval!==0||(i.frames.push({png:Buffer.from(n.data,`base64`),capturedAtMs:r}),this.#t(({frames:e})=>e.reduce((e,t)=>e+t.png.length,0)))}build(e){let t=/* @__PURE__ */ new Map,n=({frames:n,lastSeenAtMs:r})=>{let i=t.get(n)??c(n.map((t,i)=>({png:t.png,delayMs:(n[i+1]?.capturedAtMs??r+e)-t.capturedAtMs})));return t.set(n,i),i},r=e=>s(e)&&e.frames.length>1;return this.#t(e=>r(e)?n(e).length:0),[...this.#e.values()].filter(r).map(e=>({path:e.path,data:n(e).toString(`base64`),encoding:`base64`}))}#t(e){let t=[...this.#e.values()];for(;;){let n=new Map(t.map(t=>[t,e(t)]));if([...n.values()].reduce((e,t)=>e+t,0)<=2097152)return;let r=t.filter(e=>e.frames.length>0).toSorted((e,t)=>(n.get(t)??0)-(n.get(e)??0)),[i]=r.filter(e=>!s(e)).toSorted((e,t)=>e.changeCount-t.changeCount),a=r.find(e=>e.frames.length>2);if(i)i.frames=[];else if(a)a.frames=a.frames.filter((e,t)=>t%2==0),a.keptChangeInterval*=2;else{let[e]=r;e.frames=[],e.isTooLarge=!0}}}};function s(e){return e.changeCount>=3}function c(e){let t=e.map(e=>({...l(e.png),delayMs:e.delayMs})),o=t[0]?.header.subarray(8);if(!o)throw Error(`an animated PNG needs at least one frame`);let s=t.filter(e=>e.header.subarray(8).equals(o)).map(e=>({...e,width:e.header.readUInt32BE(0),height:e.header.readUInt32BE(4)})),[c]=s,d=Math.max(...s.map(e=>e.width)),f=Math.max(...s.map(e=>e.height)),p=Buffer.from(c.header);p.writeUInt32BE(d,0),p.writeUInt32BE(f,4);let m=Buffer.alloc(8);m.writeUInt32BE(s.length,0);let h=[r,u(`IHDR`,p),...c.metadataChunks,u(`acTL`,m)],g=c.width===d&&c.height===f;if(!g){let e=o[0]*(i[o[1]]??1),t=Buffer.alloc(f*(1+Math.ceil(d*e/8)));h.push(u(`IDAT`,n.default.deflateSync(t)))}let _=0;for(let[e,t]of s.entries()){let n=Buffer.alloc(26);n.writeUInt32BE(_++,0),n.writeUInt32BE(t.width,4),n.writeUInt32BE(t.height,8);let r=Math.max(1,Math.min(1e3,Math.floor(a*1e3/t.delayMs))),i=Math.round(t.delayMs*r/1e3);if(n.writeUInt16BE(Math.min(Math.max(i,1),a),20),n.writeUInt16BE(r,22),n.writeUInt8(1,24),h.push(u(`fcTL`,n)),e===0&&g)h.push(u(`IDAT`,t.imageData));else{let e=Buffer.alloc(4);e.writeUInt32BE(_++),h.push(u(`fdAT`,Buffer.concat([e,t.imageData])))}}return h.push(u(`IEND`,Buffer.alloc(0))),Buffer.concat(h)}function l(e){if(!e.subarray(0,r.length).equals(r))throw Error(`not a PNG image`);let t,n=[],i=[],a=r.length;for(;a+12<=e.length;){let r=e.readUInt32BE(a),o=e.toString(`latin1`,a+4,a+8),s=e.subarray(a+8,a+8+r),c=a+12+r;o===`IHDR`?t=s:o===`IDAT`?i.push(s):i.length===0&&o!==`IEND`&&n.push(e.subarray(a,c)),a=c}if(t?.length!==13||i.length===0)throw Error(`not a PNG image`);return{header:t,metadataChunks:n,imageData:Buffer.concat(i)}}function u(e,t){let r=Buffer.concat([Buffer.from(e,`latin1`),t]),i=Buffer.alloc(r.length+8);return i.writeUInt32BE(t.length,0),r.copy(i,4),i.writeUInt32BE(n.default.crc32(r),i.length-4),i}exports.GuiRecorder=o,exports.MAX_GUI_RECORDING_BYTES=2097152,exports.encodeAnimatedPng=c;
2
+ //# sourceMappingURL=guiRecording.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"guiRecording.cjs","names":["crypto","zlib"],"sources":["../../src/helpers/guiRecording.ts"],"sourcesContent":["import crypto from 'node:crypto';\nimport zlib from 'node:zlib';\n\n/** Bounds the recordings of one GUI run in total, so a judge server can budget for them. */\nexport const MAX_GUI_RECORDING_BYTES = 2 * 1024 * 1024;\n\nexport interface GuiRecordingFile {\n path: string;\n data: string;\n encoding: 'base64';\n}\n\ninterface CapturedFrame {\n png: Buffer;\n capturedAtMs: number;\n}\n\ninterface WindowCaptures {\n path: string;\n /** The captures kept for the recording; none once the window turned out too large to record. */\n frames: CapturedFrame[];\n /** A digest of the last capture, kept or not, to tell whether the window changed. */\n lastDigest: string;\n changeCount: number;\n /** Every how many changes a capture is kept; doubled whenever half of the kept frames are dropped. */\n keptChangeInterval: number;\n lastSeenAtMs: number;\n isTooLarge: boolean;\n}\n\ninterface AnimationFrame {\n png: Buffer;\n delayMs: number;\n}\n\ninterface ParsedPng {\n /** The IHDR data: width, height, and the 5-byte pixel format. */\n header: Buffer;\n /** The chunks between IHDR and the image data (e.g. a palette), as they are in the file. */\n metadataChunks: Buffer[];\n imageData: Buffer;\n}\n\nconst PNG_SIGNATURE = Buffer.from('89504e470d0a1a0a', 'hex');\nconst CHANNELS_BY_COLOR_TYPE: Record<number, number> = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };\nconst MAX_UINT16 = 65_535;\n// A window is typically captured blank once before its first paint, which is not an animation.\nconst MIN_RECORDED_CHANGE_COUNT = 3;\n\n/**\n * Collects the screenshots of each window over a run and turns the windows that kept changing into\n * animated PNGs, which browsers play wherever they show a PNG. The captures it keeps are bounded\n * like the recordings, however long the run lasts and whatever the windows show.\n */\nexport class GuiRecorder {\n readonly #windowIdToCaptures = new Map<string, WindowCaptures>();\n\n add(windowId: string, screenshot: { path: string; data: string }, capturedAtMs: number): void {\n const captures = this.#windowIdToCaptures.get(windowId) ?? {\n path: '',\n frames: [],\n lastDigest: '',\n changeCount: 0,\n keptChangeInterval: 1,\n lastSeenAtMs: 0,\n isTooLarge: false,\n };\n this.#windowIdToCaptures.set(windowId, captures);\n captures.path = screenshot.path.replace(/\\.png$/, '_recording.png');\n captures.lastSeenAtMs = capturedAtMs;\n // An unchanged window only extends how long its last frame is shown.\n const digest = crypto.createHash('sha256').update(screenshot.data).digest('base64');\n if (captures.lastDigest === digest) return;\n\n captures.lastDigest = digest;\n const changeIndex = captures.changeCount++;\n if (captures.isTooLarge || changeIndex % captures.keptChangeInterval !== 0) return;\n\n captures.frames.push({ png: Buffer.from(screenshot.data, 'base64'), capturedAtMs });\n this.#shrinkToFit(({ frames }) => frames.reduce((sum, frame) => sum + frame.png.length, 0));\n }\n\n /** @param frameIntervalMs How long the last capture of a window is shown. */\n build(frameIntervalMs: number): GuiRecordingFile[] {\n const framesToAnimatedPng = new Map<CapturedFrame[], Buffer>();\n const encode = ({ frames, lastSeenAtMs }: WindowCaptures): Buffer => {\n const animatedPng =\n framesToAnimatedPng.get(frames) ??\n encodeAnimatedPng(\n frames.map((frame, index) => ({\n png: frame.png,\n delayMs: (frames[index + 1]?.capturedAtMs ?? lastSeenAtMs + frameIntervalMs) - frame.capturedAtMs,\n }))\n );\n framesToAnimatedPng.set(frames, animatedPng);\n return animatedPng;\n };\n const isRecorded = (captures: WindowCaptures): boolean => isAnimated(captures) && captures.frames.length > 1;\n\n // The chunks an animated PNG adds per frame can exceed what the frames alone were bounded to.\n this.#shrinkToFit((captures) => (isRecorded(captures) ? encode(captures).length : 0));\n return [...this.#windowIdToCaptures.values()].filter(isRecorded).map((captures) => ({\n path: captures.path,\n data: encode(captures).toString('base64'),\n encoding: 'base64',\n }));\n }\n\n /**\n * Frees space until all windows together fit `MAX_GUI_RECORDING_BYTES`, giving up what matters\n * least first: the early captures of a window that is not animated (yet), then every other frame\n * of the largest animated window, and only when no window has more than two frames a whole window,\n * which is then not recorded.\n */\n #shrinkToFit(measure: (captures: WindowCaptures) => number): void {\n const allCaptures = [...this.#windowIdToCaptures.values()];\n for (;;) {\n const sizes = new Map(allCaptures.map((captures) => [captures, measure(captures)]));\n if ([...sizes.values()].reduce((sum, size) => sum + size, 0) <= MAX_GUI_RECORDING_BYTES) return;\n\n const bySizeDescending = allCaptures\n .filter((captures) => captures.frames.length > 0)\n .toSorted((a, b) => (sizes.get(b) ?? 0) - (sizes.get(a) ?? 0));\n // The fewer changes a window showed, the less likely it is the start of an animation.\n const [notAnimated] = bySizeDescending\n .filter((captures) => !isAnimated(captures))\n .toSorted((a, b) => a.changeCount - b.changeCount);\n const shrinkable = bySizeDescending.find((captures) => captures.frames.length > 2);\n if (notAnimated) {\n notAnimated.frames = [];\n } else if (shrinkable) {\n shrinkable.frames = shrinkable.frames.filter((_, index) => index % 2 === 0);\n shrinkable.keptChangeInterval *= 2;\n } else {\n const [largest] = bySizeDescending as [WindowCaptures, ...WindowCaptures[]];\n largest.frames = [];\n largest.isTooLarge = true;\n }\n }\n }\n}\n\nfunction isAnimated(captures: WindowCaptures): boolean {\n return captures.changeCount >= MIN_RECORDED_CHANGE_COUNT;\n}\n\n/**\n * Assembles PNG images into an animated PNG (APNG) that loops forever. The compressed image data of\n * each frame is reused as is, so the frames must share the pixel format of the first one; frames of\n * another format are left out. A frame smaller than the largest one is drawn at the top-left corner.\n */\nexport function encodeAnimatedPng(frames: readonly AnimationFrame[]): Buffer {\n const parsedFrames = frames.map((frame) => ({ ...parsePng(frame.png), delayMs: frame.delayMs }));\n const pixelFormat = parsedFrames[0]?.header.subarray(8);\n if (!pixelFormat) throw new Error('an animated PNG needs at least one frame');\n const animationFrames = parsedFrames\n .filter((frame) => frame.header.subarray(8).equals(pixelFormat))\n .map((frame) => ({ ...frame, width: frame.header.readUInt32BE(0), height: frame.header.readUInt32BE(4) }));\n const [firstFrame] = animationFrames as [(typeof animationFrames)[number], ...typeof animationFrames];\n const width = Math.max(...animationFrames.map((frame) => frame.width));\n const height = Math.max(...animationFrames.map((frame) => frame.height));\n\n const header = Buffer.from(firstFrame.header);\n header.writeUInt32BE(width, 0);\n header.writeUInt32BE(height, 4);\n const animationControl = Buffer.alloc(8);\n animationControl.writeUInt32BE(animationFrames.length, 0);\n const chunks = [\n PNG_SIGNATURE,\n createChunk('IHDR', header),\n ...firstFrame.metadataChunks,\n createChunk('acTL', animationControl),\n ];\n\n // The image shown by viewers without animation support must fill the canvas. Only a first frame\n // of that size can serve as it; otherwise it is a blank image that is not part of the animation.\n const startsWithFirstFrame = firstFrame.width === width && firstFrame.height === height;\n if (!startsWithFirstFrame) {\n const bitsPerPixel = (pixelFormat[0] as number) * (CHANNELS_BY_COLOR_TYPE[pixelFormat[1] as number] ?? 1);\n const blankScanlines = Buffer.alloc(height * (1 + Math.ceil((width * bitsPerPixel) / 8)));\n chunks.push(createChunk('IDAT', zlib.deflateSync(blankScanlines)));\n }\n\n let sequenceNumber = 0;\n for (const [index, frame] of animationFrames.entries()) {\n const frameControl = Buffer.alloc(26);\n frameControl.writeUInt32BE(sequenceNumber++, 0);\n frameControl.writeUInt32BE(frame.width, 4);\n frameControl.writeUInt32BE(frame.height, 8);\n // The x and y offsets at 12 and 16 stay 0.\n // The delay is a fraction of two 16-bit integers: milliseconds, or the finest unit a longer delay fits in.\n const delayUnitsPerSecond = Math.max(1, Math.min(1000, Math.floor((MAX_UINT16 * 1000) / frame.delayMs)));\n const delay = Math.round((frame.delayMs * delayUnitsPerSecond) / 1000);\n frameControl.writeUInt16BE(Math.min(Math.max(delay, 1), MAX_UINT16), 20);\n frameControl.writeUInt16BE(delayUnitsPerSecond, 22);\n // Every frame is cleared before the next one, so a larger frame does not stay visible around a smaller one.\n frameControl.writeUInt8(1, 24);\n chunks.push(createChunk('fcTL', frameControl));\n\n if (index === 0 && startsWithFirstFrame) {\n chunks.push(createChunk('IDAT', frame.imageData));\n } else {\n const sequence = Buffer.alloc(4);\n sequence.writeUInt32BE(sequenceNumber++);\n chunks.push(createChunk('fdAT', Buffer.concat([sequence, frame.imageData])));\n }\n }\n chunks.push(createChunk('IEND', Buffer.alloc(0)));\n return Buffer.concat(chunks);\n}\n\nfunction parsePng(png: Buffer): ParsedPng {\n if (!png.subarray(0, PNG_SIGNATURE.length).equals(PNG_SIGNATURE)) throw new Error('not a PNG image');\n\n let header: Buffer | undefined;\n const metadataChunks: Buffer[] = [];\n const imageDataChunks: Buffer[] = [];\n let offset = PNG_SIGNATURE.length;\n while (offset + 12 <= png.length) {\n const dataLength = png.readUInt32BE(offset);\n const type = png.toString('latin1', offset + 4, offset + 8);\n const data = png.subarray(offset + 8, offset + 8 + dataLength);\n const nextOffset = offset + 12 + dataLength;\n if (type === 'IHDR') {\n header = data;\n } else if (type === 'IDAT') {\n imageDataChunks.push(data);\n } else if (imageDataChunks.length === 0 && type !== 'IEND') {\n metadataChunks.push(png.subarray(offset, nextOffset));\n }\n offset = nextOffset;\n }\n if (header?.length !== 13 || imageDataChunks.length === 0) throw new Error('not a PNG image');\n return { header, metadataChunks, imageData: Buffer.concat(imageDataChunks) };\n}\n\nfunction createChunk(type: string, data: Buffer): Buffer {\n const typeAndData = Buffer.concat([Buffer.from(type, 'latin1'), data]);\n const chunk = Buffer.alloc(typeAndData.length + 8);\n chunk.writeUInt32BE(data.length, 0);\n typeAndData.copy(chunk, 4);\n chunk.writeUInt32BE(zlib.crc32(typeAndData), chunk.length - 4);\n return chunk;\n}\n"],"mappings":"4JAIA,MAuCM,EAAgB,OAAO,KAAK,mBAAoB,KAAK,EACrD,EAAiD,CAAE,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,CAAE,EAChF,EAAa,MASnB,IAAa,EAAb,KAAyB,CACvB,mBAA+B,IAAI,IAEnC,IAAI,EAAkB,EAA4C,EAA4B,CAC5F,IAAM,EAAW,KAAK,GAAoB,IAAI,CAAQ,GAAK,CACzD,KAAM,GACN,OAAQ,CAAC,EACT,WAAY,GACZ,YAAa,EACb,mBAAoB,EACpB,aAAc,EACd,WAAY,EACd,EACA,KAAK,GAAoB,IAAI,EAAU,CAAQ,EAC/C,EAAS,KAAO,EAAW,KAAK,QAAQ,SAAU,gBAAgB,EAClE,EAAS,aAAe,EAExB,IAAM,EAASA,EAAAA,QAAO,WAAW,QAAQ,CAAC,CAAC,OAAO,EAAW,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 @@
1
+ /** Bounds the recordings of one GUI run in total, so a judge server can budget for them. */
2
+ export declare const MAX_GUI_RECORDING_BYTES: number;
3
+ export interface GuiRecordingFile {
4
+ path: string;
5
+ data: string;
6
+ encoding: 'base64';
7
+ }
8
+ interface AnimationFrame {
9
+ png: Buffer;
10
+ delayMs: number;
11
+ }
12
+ /**
13
+ * Collects the screenshots of each window over a run and turns the windows that kept changing into
14
+ * animated PNGs, which browsers play wherever they show a PNG. The captures it keeps are bounded
15
+ * like the recordings, however long the run lasts and whatever the windows show.
16
+ */
17
+ export declare class GuiRecorder {
18
+ #private;
19
+ add(windowId: string, screenshot: {
20
+ path: string;
21
+ data: string;
22
+ }, capturedAtMs: number): void;
23
+ /** @param frameIntervalMs How long the last capture of a window is shown. */
24
+ build(frameIntervalMs: number): GuiRecordingFile[];
25
+ }
26
+ /**
27
+ * Assembles PNG images into an animated PNG (APNG) that loops forever. The compressed image data of
28
+ * each frame is reused as is, so the frames must share the pixel format of the first one; frames of
29
+ * another format are left out. A frame smaller than the largest one is drawn at the top-left corner.
30
+ */
31
+ export declare function encodeAnimatedPng(frames: readonly AnimationFrame[]): Buffer;
32
+ export {};
@@ -0,0 +1,2 @@
1
+ import e from"node:crypto";import t from"node:zlib";const n=2097152,r=Buffer.from(`89504e470d0a1a0a`,`hex`),i={0:1,2:3,3:1,4:2,6:4},a=65535;var o=class{#e=/* @__PURE__ */ new Map;add(t,n,r){let i=this.#e.get(t)??{path:``,frames:[],lastDigest:``,changeCount:0,keptChangeInterval:1,lastSeenAtMs:0,isTooLarge:!1};this.#e.set(t,i),i.path=n.path.replace(/\.png$/,`_recording.png`),i.lastSeenAtMs=r;let a=e.createHash(`sha256`).update(n.data).digest(`base64`);if(i.lastDigest===a)return;i.lastDigest=a;let o=i.changeCount++;i.isTooLarge||o%i.keptChangeInterval!==0||(i.frames.push({png:Buffer.from(n.data,`base64`),capturedAtMs:r}),this.#t(({frames:e})=>e.reduce((e,t)=>e+t.png.length,0)))}build(e){let t=/* @__PURE__ */ new Map,n=({frames:n,lastSeenAtMs:r})=>{let i=t.get(n)??c(n.map((t,i)=>({png:t.png,delayMs:(n[i+1]?.capturedAtMs??r+e)-t.capturedAtMs})));return t.set(n,i),i},r=e=>s(e)&&e.frames.length>1;return this.#t(e=>r(e)?n(e).length:0),[...this.#e.values()].filter(r).map(e=>({path:e.path,data:n(e).toString(`base64`),encoding:`base64`}))}#t(e){let t=[...this.#e.values()];for(;;){let n=new Map(t.map(t=>[t,e(t)]));if([...n.values()].reduce((e,t)=>e+t,0)<=2097152)return;let r=t.filter(e=>e.frames.length>0).toSorted((e,t)=>(n.get(t)??0)-(n.get(e)??0)),[i]=r.filter(e=>!s(e)).toSorted((e,t)=>e.changeCount-t.changeCount),a=r.find(e=>e.frames.length>2);if(i)i.frames=[];else if(a)a.frames=a.frames.filter((e,t)=>t%2==0),a.keptChangeInterval*=2;else{let[e]=r;e.frames=[],e.isTooLarge=!0}}}};function s(e){return e.changeCount>=3}function c(e){let n=e.map(e=>({...l(e.png),delayMs:e.delayMs})),o=n[0]?.header.subarray(8);if(!o)throw Error(`an animated PNG needs at least one frame`);let s=n.filter(e=>e.header.subarray(8).equals(o)).map(e=>({...e,width:e.header.readUInt32BE(0),height:e.header.readUInt32BE(4)})),[c]=s,d=Math.max(...s.map(e=>e.width)),f=Math.max(...s.map(e=>e.height)),p=Buffer.from(c.header);p.writeUInt32BE(d,0),p.writeUInt32BE(f,4);let m=Buffer.alloc(8);m.writeUInt32BE(s.length,0);let h=[r,u(`IHDR`,p),...c.metadataChunks,u(`acTL`,m)],g=c.width===d&&c.height===f;if(!g){let e=o[0]*(i[o[1]]??1),n=Buffer.alloc(f*(1+Math.ceil(d*e/8)));h.push(u(`IDAT`,t.deflateSync(n)))}let _=0;for(let[e,t]of s.entries()){let n=Buffer.alloc(26);n.writeUInt32BE(_++,0),n.writeUInt32BE(t.width,4),n.writeUInt32BE(t.height,8);let r=Math.max(1,Math.min(1e3,Math.floor(a*1e3/t.delayMs))),i=Math.round(t.delayMs*r/1e3);if(n.writeUInt16BE(Math.min(Math.max(i,1),a),20),n.writeUInt16BE(r,22),n.writeUInt8(1,24),h.push(u(`fcTL`,n)),e===0&&g)h.push(u(`IDAT`,t.imageData));else{let e=Buffer.alloc(4);e.writeUInt32BE(_++),h.push(u(`fdAT`,Buffer.concat([e,t.imageData])))}}return h.push(u(`IEND`,Buffer.alloc(0))),Buffer.concat(h)}function l(e){if(!e.subarray(0,r.length).equals(r))throw Error(`not a PNG image`);let t,n=[],i=[],a=r.length;for(;a+12<=e.length;){let r=e.readUInt32BE(a),o=e.toString(`latin1`,a+4,a+8),s=e.subarray(a+8,a+8+r),c=a+12+r;o===`IHDR`?t=s:o===`IDAT`?i.push(s):i.length===0&&o!==`IEND`&&n.push(e.subarray(a,c)),a=c}if(t?.length!==13||i.length===0)throw Error(`not a PNG image`);return{header:t,metadataChunks:n,imageData:Buffer.concat(i)}}function u(e,n){let r=Buffer.concat([Buffer.from(e,`latin1`),n]),i=Buffer.alloc(r.length+8);return i.writeUInt32BE(n.length,0),r.copy(i,4),i.writeUInt32BE(t.crc32(r),i.length-4),i}export{o as GuiRecorder,n as MAX_GUI_RECORDING_BYTES,c as encodeAnimatedPng};
2
+ //# sourceMappingURL=guiRecording.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"guiRecording.js","names":[],"sources":["../../src/helpers/guiRecording.ts"],"sourcesContent":["import crypto from 'node:crypto';\nimport zlib from 'node:zlib';\n\n/** Bounds the recordings of one GUI run in total, so a judge server can budget for them. */\nexport const MAX_GUI_RECORDING_BYTES = 2 * 1024 * 1024;\n\nexport interface GuiRecordingFile {\n path: string;\n data: string;\n encoding: 'base64';\n}\n\ninterface CapturedFrame {\n png: Buffer;\n capturedAtMs: number;\n}\n\ninterface WindowCaptures {\n path: string;\n /** The captures kept for the recording; none once the window turned out too large to record. */\n frames: CapturedFrame[];\n /** A digest of the last capture, kept or not, to tell whether the window changed. */\n lastDigest: string;\n changeCount: number;\n /** Every how many changes a capture is kept; doubled whenever half of the kept frames are dropped. */\n keptChangeInterval: number;\n lastSeenAtMs: number;\n isTooLarge: boolean;\n}\n\ninterface AnimationFrame {\n png: Buffer;\n delayMs: number;\n}\n\ninterface ParsedPng {\n /** The IHDR data: width, height, and the 5-byte pixel format. */\n header: Buffer;\n /** The chunks between IHDR and the image data (e.g. a palette), as they are in the file. */\n metadataChunks: Buffer[];\n imageData: Buffer;\n}\n\nconst PNG_SIGNATURE = Buffer.from('89504e470d0a1a0a', 'hex');\nconst CHANNELS_BY_COLOR_TYPE: Record<number, number> = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };\nconst MAX_UINT16 = 65_535;\n// A window is typically captured blank once before its first paint, which is not an animation.\nconst MIN_RECORDED_CHANGE_COUNT = 3;\n\n/**\n * Collects the screenshots of each window over a run and turns the windows that kept changing into\n * animated PNGs, which browsers play wherever they show a PNG. The captures it keeps are bounded\n * like the recordings, however long the run lasts and whatever the windows show.\n */\nexport class GuiRecorder {\n readonly #windowIdToCaptures = new Map<string, WindowCaptures>();\n\n add(windowId: string, screenshot: { path: string; data: string }, capturedAtMs: number): void {\n const captures = this.#windowIdToCaptures.get(windowId) ?? {\n path: '',\n frames: [],\n lastDigest: '',\n changeCount: 0,\n keptChangeInterval: 1,\n lastSeenAtMs: 0,\n isTooLarge: false,\n };\n this.#windowIdToCaptures.set(windowId, captures);\n captures.path = screenshot.path.replace(/\\.png$/, '_recording.png');\n captures.lastSeenAtMs = capturedAtMs;\n // An unchanged window only extends how long its last frame is shown.\n const digest = crypto.createHash('sha256').update(screenshot.data).digest('base64');\n if (captures.lastDigest === digest) return;\n\n captures.lastDigest = digest;\n const changeIndex = captures.changeCount++;\n if (captures.isTooLarge || changeIndex % captures.keptChangeInterval !== 0) return;\n\n captures.frames.push({ png: Buffer.from(screenshot.data, 'base64'), capturedAtMs });\n this.#shrinkToFit(({ frames }) => frames.reduce((sum, frame) => sum + frame.png.length, 0));\n }\n\n /** @param frameIntervalMs How long the last capture of a window is shown. */\n build(frameIntervalMs: number): GuiRecordingFile[] {\n const framesToAnimatedPng = new Map<CapturedFrame[], Buffer>();\n const encode = ({ frames, lastSeenAtMs }: WindowCaptures): Buffer => {\n const animatedPng =\n framesToAnimatedPng.get(frames) ??\n encodeAnimatedPng(\n frames.map((frame, index) => ({\n png: frame.png,\n delayMs: (frames[index + 1]?.capturedAtMs ?? lastSeenAtMs + frameIntervalMs) - frame.capturedAtMs,\n }))\n );\n framesToAnimatedPng.set(frames, animatedPng);\n return animatedPng;\n };\n const isRecorded = (captures: WindowCaptures): boolean => isAnimated(captures) && captures.frames.length > 1;\n\n // The chunks an animated PNG adds per frame can exceed what the frames alone were bounded to.\n this.#shrinkToFit((captures) => (isRecorded(captures) ? encode(captures).length : 0));\n return [...this.#windowIdToCaptures.values()].filter(isRecorded).map((captures) => ({\n path: captures.path,\n data: encode(captures).toString('base64'),\n encoding: 'base64',\n }));\n }\n\n /**\n * Frees space until all windows together fit `MAX_GUI_RECORDING_BYTES`, giving up what matters\n * least first: the early captures of a window that is not animated (yet), then every other frame\n * of the largest animated window, and only when no window has more than two frames a whole window,\n * which is then not recorded.\n */\n #shrinkToFit(measure: (captures: WindowCaptures) => number): void {\n const allCaptures = [...this.#windowIdToCaptures.values()];\n for (;;) {\n const sizes = new Map(allCaptures.map((captures) => [captures, measure(captures)]));\n if ([...sizes.values()].reduce((sum, size) => sum + size, 0) <= MAX_GUI_RECORDING_BYTES) return;\n\n const bySizeDescending = allCaptures\n .filter((captures) => captures.frames.length > 0)\n .toSorted((a, b) => (sizes.get(b) ?? 0) - (sizes.get(a) ?? 0));\n // The fewer changes a window showed, the less likely it is the start of an animation.\n const [notAnimated] = bySizeDescending\n .filter((captures) => !isAnimated(captures))\n .toSorted((a, b) => a.changeCount - b.changeCount);\n const shrinkable = bySizeDescending.find((captures) => captures.frames.length > 2);\n if (notAnimated) {\n notAnimated.frames = [];\n } else if (shrinkable) {\n shrinkable.frames = shrinkable.frames.filter((_, index) => index % 2 === 0);\n shrinkable.keptChangeInterval *= 2;\n } else {\n const [largest] = bySizeDescending as [WindowCaptures, ...WindowCaptures[]];\n largest.frames = [];\n largest.isTooLarge = true;\n }\n }\n }\n}\n\nfunction isAnimated(captures: WindowCaptures): boolean {\n return captures.changeCount >= MIN_RECORDED_CHANGE_COUNT;\n}\n\n/**\n * Assembles PNG images into an animated PNG (APNG) that loops forever. The compressed image data of\n * each frame is reused as is, so the frames must share the pixel format of the first one; frames of\n * another format are left out. A frame smaller than the largest one is drawn at the top-left corner.\n */\nexport function encodeAnimatedPng(frames: readonly AnimationFrame[]): Buffer {\n const parsedFrames = frames.map((frame) => ({ ...parsePng(frame.png), delayMs: frame.delayMs }));\n const pixelFormat = parsedFrames[0]?.header.subarray(8);\n if (!pixelFormat) throw new Error('an animated PNG needs at least one frame');\n const animationFrames = parsedFrames\n .filter((frame) => frame.header.subarray(8).equals(pixelFormat))\n .map((frame) => ({ ...frame, width: frame.header.readUInt32BE(0), height: frame.header.readUInt32BE(4) }));\n const [firstFrame] = animationFrames as [(typeof animationFrames)[number], ...typeof animationFrames];\n const width = Math.max(...animationFrames.map((frame) => frame.width));\n const height = Math.max(...animationFrames.map((frame) => frame.height));\n\n const header = Buffer.from(firstFrame.header);\n header.writeUInt32BE(width, 0);\n header.writeUInt32BE(height, 4);\n const animationControl = Buffer.alloc(8);\n animationControl.writeUInt32BE(animationFrames.length, 0);\n const chunks = [\n PNG_SIGNATURE,\n createChunk('IHDR', header),\n ...firstFrame.metadataChunks,\n createChunk('acTL', animationControl),\n ];\n\n // The image shown by viewers without animation support must fill the canvas. Only a first frame\n // of that size can serve as it; otherwise it is a blank image that is not part of the animation.\n const startsWithFirstFrame = firstFrame.width === width && firstFrame.height === height;\n if (!startsWithFirstFrame) {\n const bitsPerPixel = (pixelFormat[0] as number) * (CHANNELS_BY_COLOR_TYPE[pixelFormat[1] as number] ?? 1);\n const blankScanlines = Buffer.alloc(height * (1 + Math.ceil((width * bitsPerPixel) / 8)));\n chunks.push(createChunk('IDAT', zlib.deflateSync(blankScanlines)));\n }\n\n let sequenceNumber = 0;\n for (const [index, frame] of animationFrames.entries()) {\n const frameControl = Buffer.alloc(26);\n frameControl.writeUInt32BE(sequenceNumber++, 0);\n frameControl.writeUInt32BE(frame.width, 4);\n frameControl.writeUInt32BE(frame.height, 8);\n // The x and y offsets at 12 and 16 stay 0.\n // The delay is a fraction of two 16-bit integers: milliseconds, or the finest unit a longer delay fits in.\n const delayUnitsPerSecond = Math.max(1, Math.min(1000, Math.floor((MAX_UINT16 * 1000) / frame.delayMs)));\n const delay = Math.round((frame.delayMs * delayUnitsPerSecond) / 1000);\n frameControl.writeUInt16BE(Math.min(Math.max(delay, 1), MAX_UINT16), 20);\n frameControl.writeUInt16BE(delayUnitsPerSecond, 22);\n // Every frame is cleared before the next one, so a larger frame does not stay visible around a smaller one.\n frameControl.writeUInt8(1, 24);\n chunks.push(createChunk('fcTL', frameControl));\n\n if (index === 0 && startsWithFirstFrame) {\n chunks.push(createChunk('IDAT', frame.imageData));\n } else {\n const sequence = Buffer.alloc(4);\n sequence.writeUInt32BE(sequenceNumber++);\n chunks.push(createChunk('fdAT', Buffer.concat([sequence, frame.imageData])));\n }\n }\n chunks.push(createChunk('IEND', Buffer.alloc(0)));\n return Buffer.concat(chunks);\n}\n\nfunction parsePng(png: Buffer): ParsedPng {\n if (!png.subarray(0, PNG_SIGNATURE.length).equals(PNG_SIGNATURE)) throw new Error('not a PNG image');\n\n let header: Buffer | undefined;\n const metadataChunks: Buffer[] = [];\n const imageDataChunks: Buffer[] = [];\n let offset = PNG_SIGNATURE.length;\n while (offset + 12 <= png.length) {\n const dataLength = png.readUInt32BE(offset);\n const type = png.toString('latin1', offset + 4, offset + 8);\n const data = png.subarray(offset + 8, offset + 8 + dataLength);\n const nextOffset = offset + 12 + dataLength;\n if (type === 'IHDR') {\n header = data;\n } else if (type === 'IDAT') {\n imageDataChunks.push(data);\n } else if (imageDataChunks.length === 0 && type !== 'IEND') {\n metadataChunks.push(png.subarray(offset, nextOffset));\n }\n offset = nextOffset;\n }\n if (header?.length !== 13 || imageDataChunks.length === 0) throw new Error('not a PNG image');\n return { header, metadataChunks, imageData: Buffer.concat(imageDataChunks) };\n}\n\nfunction createChunk(type: string, data: Buffer): Buffer {\n const typeAndData = Buffer.concat([Buffer.from(type, 'latin1'), data]);\n const chunk = Buffer.alloc(typeAndData.length + 8);\n chunk.writeUInt32BE(data.length, 0);\n typeAndData.copy(chunk, 4);\n chunk.writeUInt32BE(zlib.crc32(typeAndData), chunk.length - 4);\n return chunk;\n}\n"],"mappings":"oDAIA,MAAa,EAA0B,QAuCjC,EAAgB,OAAO,KAAK,mBAAoB,KAAK,EACrD,EAAiD,CAAE,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,EAAG,CAAE,EAChF,EAAa,MASnB,IAAa,EAAb,KAAyB,CACvB,mBAA+B,IAAI,IAEnC,IAAI,EAAkB,EAA4C,EAA4B,CAC5F,IAAM,EAAW,KAAK,GAAoB,IAAI,CAAQ,GAAK,CACzD,KAAM,GACN,OAAQ,CAAC,EACT,WAAY,GACZ,YAAa,EACb,mBAAoB,EACpB,aAAc,EACd,WAAY,EACd,EACA,KAAK,GAAoB,IAAI,EAAU,CAAQ,EAC/C,EAAS,KAAO,EAAW,KAAK,QAAQ,SAAU,gBAAgB,EAClE,EAAS,aAAe,EAExB,IAAM,EAAS,EAAO,WAAW,QAAQ,CAAC,CAAC,OAAO,EAAW,IAAI,CAAC,CAAC,OAAO,QAAQ,EAClF,GAAI,EAAS,aAAe,EAAQ,OAEpC,EAAS,WAAa,EACtB,IAAM,EAAc,EAAS,cACzB,EAAS,YAAc,EAAc,EAAS,qBAAuB,IAEzE,EAAS,OAAO,KAAK,CAAE,IAAK,OAAO,KAAK,EAAW,KAAM,QAAQ,EAAG,cAAa,CAAC,EAClF,KAAK,IAAc,CAAE,YAAa,EAAO,QAAQ,EAAK,IAAU,EAAM,EAAM,I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MAAU,MAAM,iBAAiB,EAEnG,IAAI,EACE,EAA2B,CAAC,EAC5B,EAA4B,CAAC,EAC/B,EAAS,EAAc,OAC3B,KAAO,EAAS,IAAM,EAAI,QAAQ,CAChC,IAAM,EAAa,EAAI,aAAa,CAAM,EACpC,EAAO,EAAI,SAAS,SAAU,EAAS,EAAG,EAAS,CAAC,EACpD,EAAO,EAAI,SAAS,EAAS,EAAG,EAAS,EAAI,CAAU,EACvD,EAAa,EAAS,GAAK,EAC7B,IAAS,OACX,EAAS,EACA,IAAS,OAClB,EAAgB,KAAK,CAAI,EAChB,EAAgB,SAAW,GAAK,IAAS,QAClD,EAAe,KAAK,EAAI,SAAS,EAAQ,CAAU,CAAC,EAEtD,EAAS,CACX,CACA,GAAI,GAAQ,SAAW,IAAM,EAAgB,SAAW,EAAG,MAAU,MAAM,iBAAiB,EAC5F,MAAO,CAAE,SAAQ,iBAAgB,UAAW,OAAO,OAAO,CAAe,CAAE,CAC7E,CAEA,SAAS,EAAY,EAAc,EAAsB,CACvD,IAAM,EAAc,OAAO,OAAO,CAAC,OAAO,KAAK,EAAM,QAAQ,EAAG,CAAI,CAAC,EAC/D,EAAQ,OAAO,MAAM,EAAY,OAAS,CAAC,EAIjD,OAHA,EAAM,cAAc,EAAK,OAAQ,CAAC,EAClC,EAAY,KAAK,EAAO,CAAC,EACzB,EAAM,cAAc,EAAK,MAAM,CAAW,EAAG,EAAM,OAAS,CAAC,EACtD,CACT"}
@@ -1,4 +1,4 @@
1
- "use strict";const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("./removeCommentsInSourceCode.cjs"),n=require("../types/problem.cjs"),r=require("./findLanguageDefinitionByPath.cjs"),i=require("../types/decisionCode.cjs");let a=require("node:fs");a=e.__toESM(a,1);let o=require("node:path");o=e.__toESM(o,1);async function s(e,s){let l=!!s.forbiddenRegExpsInCode?.length||!!s.forbiddenTextsInCode?.length||!!s.requiredRegExpsInCode?.length;if(!l&&!s.requiredSubmissionFilePaths?.length)return;let u=new Set,d=[];for(let n of await a.default.promises.readdir(e,{withFileTypes:!0,recursive:!0})){if(!n.isFile())continue;let i=o.default.relative(e,o.default.join(n.parentPath,n.name));if(u.add(i),!l)continue;let s=r.findLanguageDefinitionByPath(n.name);if(!s)continue;let c=await a.default.promises.readFile(o.default.join(n.parentPath,n.name),`utf8`);c.includes(`�`)||d.push({path:i,data:s.grammer?t.removeCommentsInSourceCode(s.grammer,c):c})}if(s.requiredSubmissionFilePaths){let e=s.requiredSubmissionFilePaths.filter(e=>!u.has(e)).toSorted();if(e.length>0)return{decisionCode:i.DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,feedbackMarkdown:`ファイルが不足しています。
1
+ "use strict";const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("./removeCommentsInSourceCode.cjs"),n=require("../types/problem.cjs"),r=require("./findLanguageDefinitionByPath.cjs"),i=require("../types/decisionCode.cjs");let a=require("node:fs");a=e.__toESM(a,1);let o=require("node:path");o=e.__toESM(o,1);async function s(e,s){let l=!!s.forbiddenRegExpsInCode?.length||!!s.forbiddenTextsInCode?.length||!!s.requiredRegExpsInCode?.length;if(!l&&!s.requiredSubmissionFilePaths?.length)return;let u=/* @__PURE__ */ new Set,d=[];for(let n of await a.default.promises.readdir(e,{withFileTypes:!0,recursive:!0})){if(!n.isFile())continue;let i=o.default.relative(e,o.default.join(n.parentPath,n.name));if(u.add(i),!l)continue;let s=r.findLanguageDefinitionByPath(n.name);if(!s)continue;let c=await a.default.promises.readFile(o.default.join(n.parentPath,n.name),`utf8`);c.includes(`�`)||d.push({path:i,data:s.grammer?t.removeCommentsInSourceCode(s.grammer,c):c})}if(s.requiredSubmissionFilePaths){let e=s.requiredSubmissionFilePaths.filter(e=>!u.has(e)).toSorted();if(e.length>0)return{decisionCode:i.DecisionCode.MISSING_REQUIRED_SUBMISSION_FILE_ERROR,feedbackMarkdown:`ファイルが不足しています。
2
2
  次のファイルを追加してから再度提出してください。
3
3
 
4
4
  ${e.map(e=>`- \`${e}\``).join(`