@exercode/problem-utils 1.18.2 → 1.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +26 -1
- package/bin/exercode.mjs +2 -0
- package/dist/cli/checkAllProblems.cjs +2 -0
- package/dist/cli/checkAllProblems.cjs.map +1 -0
- package/dist/cli/checkAllProblems.d.ts +6 -0
- package/dist/cli/checkAllProblems.js +2 -0
- package/dist/cli/checkAllProblems.js.map +1 -0
- package/dist/cli/exercode.cjs +9 -0
- package/dist/cli/exercode.cjs.map +1 -0
- package/dist/cli/exercode.d.ts +1 -0
- package/dist/cli/exercode.js +9 -0
- package/dist/cli/exercode.js.map +1 -0
- package/dist/cli/runSingleHarness.cjs +5 -0
- package/dist/cli/runSingleHarness.cjs.map +1 -0
- package/dist/cli/runSingleHarness.d.ts +8 -0
- package/dist/cli/runSingleHarness.js +5 -0
- package/dist/cli/runSingleHarness.js.map +1 -0
- package/dist/helpers/checkProblemDirIsolation.cjs +1 -1
- package/dist/helpers/checkProblemDirIsolation.cjs.map +1 -1
- package/dist/helpers/checkProblemDirIsolation.d.ts +15 -0
- package/dist/helpers/checkProblemDirIsolation.js +1 -1
- package/dist/helpers/checkProblemDirIsolation.js.map +1 -1
- package/dist/helpers/cleanWorkingDirectory.cjs +1 -1
- package/dist/helpers/cleanWorkingDirectory.js +1 -1
- package/dist/helpers/defaultStdioHarness.cjs +2 -0
- package/dist/helpers/defaultStdioHarness.cjs.map +1 -0
- package/dist/helpers/defaultStdioHarness.d.ts +19 -0
- package/dist/helpers/defaultStdioHarness.js +2 -0
- package/dist/helpers/defaultStdioHarness.js.map +1 -0
- package/dist/helpers/judgeByStaticAnalysis.cjs +1 -1
- package/dist/helpers/judgeByStaticAnalysis.js +1 -1
- package/dist/helpers/readOutputFiles.cjs +1 -1
- package/dist/helpers/readOutputFiles.js +1 -1
- package/dist/helpers/readProblemMarkdownFrontMatter.cjs +1 -1
- package/dist/helpers/readProblemMarkdownFrontMatter.cjs.map +1 -1
- package/dist/helpers/readProblemMarkdownFrontMatter.d.ts +5 -0
- package/dist/helpers/readProblemMarkdownFrontMatter.js +1 -1
- package/dist/helpers/readProblemMarkdownFrontMatter.js.map +1 -1
- package/dist/helpers/readTestCases.cjs +1 -1
- package/dist/helpers/readTestCases.js +1 -1
- package/dist/helpers/runCommandInTemporaryPackageManagerProject.cjs +1 -1
- package/dist/helpers/runCommandInTemporaryPackageManagerProject.js +1 -1
- package/dist/helpers/runCommandInTemporaryPackageManagerProject.js.map +1 -1
- package/dist/helpers/safeFs.cjs +1 -1
- package/dist/helpers/safeFs.js +1 -1
- package/dist/helpers/sandboxUser.cjs +1 -1
- package/dist/helpers/sandboxUser.js +1 -1
- package/dist/helpers/startHttpServer.cjs +1 -1
- package/dist/helpers/startHttpServer.js +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -1
- package/dist/presets/command.cjs +1 -1
- package/dist/presets/command.cjs.map +1 -1
- package/dist/presets/command.js +1 -1
- package/dist/presets/guiCommand.cjs +3 -3
- package/dist/presets/guiCommand.js +3 -3
- package/dist/presets/llm.cjs +1 -1
- package/dist/presets/llm.js +1 -1
- package/dist/presets/stdio.cjs +1 -1
- package/dist/presets/stdio.cjs.map +1 -1
- package/dist/presets/stdio.d.ts +13 -19
- package/dist/presets/stdio.js +1 -1
- package/dist/presets/stdio.js.map +1 -1
- package/dist/types/language.cjs +2 -2
- package/dist/types/language.js +2 -2
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -2,6 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://github.com/WillBooster/exercode-problem-utils/actions/workflows/test.yml)
|
|
4
4
|
[](https://github.com/semantic-release/semantic-release)
|
|
5
|
-
[](https://github.com/WillBooster/shared/tree/main/packages/wbfy)
|
|
6
6
|
|
|
7
7
|
:100: A set of utilities for judging programs on Exercode (https://exercode.willbooster.com/).
|
|
8
|
+
|
|
9
|
+
## CLI
|
|
10
|
+
|
|
11
|
+
The package ships an `exercode` command for problem authors (run it with `bun x` in a repository that depends on `@exercode/problem-utils`):
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
# Judge all model answers of all problems (directories containing problem.md or <id>.problem.md) under a directory.
|
|
15
|
+
# model_answers/* must be fully accepted; model_answers.fails/* must fail at least one test case.
|
|
16
|
+
bun x exercode # everything under the current directory
|
|
17
|
+
bun x exercode courses/foo # everything under courses/foo
|
|
18
|
+
bun x exercode --only a_plus --skip gui_ --concurrency 2
|
|
19
|
+
|
|
20
|
+
# Judge one answer directory of the problem in the current directory.
|
|
21
|
+
bun x exercode judge model_answers/python
|
|
22
|
+
bun x exercode judge model_answers/python '{ "language": "python" }'
|
|
23
|
+
|
|
24
|
+
# Debug one answer directory of the problem in the current directory.
|
|
25
|
+
bun x exercode debug model_answers/python '{ "stdin": "1 2" }'
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`judge` and `debug` run a custom `judge.ts` / `debug.ts` when the problem has one, and apply `stdioJudgePreset` / `stdioDebugPreset` otherwise, mirroring the Exercode server. The debug fallback applies only to standard problems: a problem with a custom `judge.ts` needs its own `debug.ts`, and `exercode debug` fails with a message otherwise (the server likewise reports debug as unsupported there).
|
|
29
|
+
|
|
30
|
+
The all-problem check judges serially by default because time limits are measured in wall-clock time; pass `--concurrency <n>` to parallelize when the checked problems are not timing-sensitive.
|
|
31
|
+
|
|
32
|
+
A standard stdin/stdout problem must NOT commit a `judge.ts` or `debug.ts` that is identical to the default stdio harness: the absence of `judge.ts` marks the problem as standard, and committed copies would drift from the server's defaults. The CLI rejects such files; a file kept intentionally (e.g. to demonstrate the default harness) can add an explanatory comment to be treated as custom.
|
package/bin/exercode.mjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("../helpers/defaultStdioHarness.cjs"),n=require("../types/testCaseResult.cjs"),r=require("../types/decisionCode.cjs"),i=require("../helpers/checkProblemDirIsolation.cjs"),a=require("../helpers/readProblemMarkdownFrontMatter.cjs"),o=require("../helpers/readTestCases.cjs"),s=require("../helpers/findModelAnswerDirs.cjs"),c=require("./runSingleHarness.cjs");let l=require("node:fs/promises");l=e.__toESM(l,1);let u=require("node:path");u=e.__toESM(u,1);let d=require("node:child_process");d=e.__toESM(d,1);const f=6e5,p=67108864,m=1e3,h=new Map(Object.entries(r.DecisionCode).map(([e,t])=>[t,e]));async function g(e){let n=T(e),r=u.default.resolve(n.rootDir),i=e=>(u.default.relative(r,e)||`.`).replaceAll(u.default.sep,`/`),l=await E(r),d=l.filter(e=>{let t=i(e);return n.only.length>0&&!n.only.some(e=>t.includes(e))?!1:!n.skip.some(e=>t.includes(e))});if(d.length===0)return console.error(l.length===0?`No problem directories (containing problem.md or <id>.problem.md) found under ${r}.`:`All ${l.length} problem directories under ${r} were excluded by --only/--skip.`),1;let f=[],p=[];for(let e of d){let n=await t.findDefaultStdioHarnessFiles(e);if(n.length>0){f.push(`${i(e)}: ${c.formatDefaultHarnessError(n)}`);continue}let r=await s.findModelAnswerDirs(e);if(r.length===0){f.push(`${i(e)}: no model answers found under model_answers/`);continue}if(!await O(u.default.join(e,`judge.ts`))&&(await o.readTestCases(u.default.join(e,`test_cases`))).length===0){let t;try{t=await a.readProblemMarkdownFrontMatter(e)}catch(t){f.push(`${i(e)}: failed to read the problem markdown front matter: ${t instanceof Error?t.message:String(t)}`);continue}if(!a.judgesWithoutTestCases(t)){f.push(`${i(e)}: ${c.MISSING_TEST_CASES_ERROR}`);continue}}let l=await s.findFailingModelAnswerDirs(e);p.push(...r.map(t=>({problemDir:e,answerDir:t,expectation:`accepted`})),...l.map(t=>({problemDir:e,answerDir:t,expectation:`rejected`})))}for(let e of f)console.error(`✗ ${e}`);let m=u.default.resolve(process.argv[1]??``),h=0,g=0;return await Promise.all(Array.from({length:Math.max(1,Math.min(n.concurrency,p.length))},async()=>{for(;g<p.length;){let e=p[g++];if(!e)return;let t=`${i(e.problemDir)} ${u.default.relative(e.problemDir,e.answerDir).replaceAll(u.default.sep,`/`)}`,n=await _(e,m);n===void 0?(h++,console.info(`✓ ${t}`)):(f.push(`${t}: ${n}`),console.error(`✗ ${t}: ${n}`))}})),console.info(`\n${h} passed, ${f.length} failed (${p.length} runs, ${d.length} problems)`),f.length===0?0:1}async function _(e,t){let n,r;try{({tempRoot:n,copiedProblemDir:r}=await i.copyProblemDirToTemporaryRoot(e.problemDir))}catch(e){return k(`failed to copy the problem directory to a temporary location: ${e instanceof Error?e.message:String(e)}`)}let a,o;try{a=v(e,await x([`run`,t,`judge`,u.default.relative(e.problemDir,e.answerDir)],r,n))}catch(e){a=k(`harness execution failed: ${e instanceof Error?e.message:String(e)}`)}finally{o=await i.forciblyRemoveDirectory(n)}if(o)return a;let s=`failed to remove the temporary copy at ${n} (judged code may have left permission-locked files)`;return a===void 0?s:k(`${a}; ${s}`)}function v(e,t){let{stdout:i,stderr:a}=t;if(t.failureReason!==void 0)return k(t.failureReason);if(t.exitCode!==0)return k(`harness exited with ${t.exitCode??`a signal`}${a.trim()?`: ${a.trim()}`:``}`);let o=i.split(/\r?\n/).filter(e=>e.startsWith(n.TEST_CASE_RESULT_PREFIX)),s=[];for(let e of o){let t;try{t=n.testCaseResultSchema.safeParse(JSON.parse(e.slice(n.TEST_CASE_RESULT_PREFIX.length)))}catch{t=void 0}if(!t?.success)return k(`malformed test case result line: ${e}`);s.push(t.data)}if(s.length===0)return`no test case results were printed`;if(e.expectation===`accepted`){let e=s.find(e=>e.decisionCode!==r.DecisionCode.ACCEPTED);return e?k(`${h.get(e.decisionCode)??e.decisionCode} on test case ${e.testCaseId}${e.stderr?.trim()?`: ${e.stderr.trim()}`:``}`):void 0}return s.every(e=>e.decisionCode===r.DecisionCode.ACCEPTED)?`expected at least one failing test case, but all test cases were accepted`:void 0}const y=new Set;let b=!1;function x(e,t,n){return S(),new Promise(r=>{let i=d.default.spawn(process.execPath,e,{cwd:t,detached:process.platform!==`win32`,env:w(),stdio:[`ignore`,`pipe`,`pipe`]}),a=i.pid===void 0?void 0:{pid:i.pid,tempRoot:n};a&&y.add(a);let o=[],s=[],c=0,l,u=!1,m=e=>{if(l===void 0){l=e,i.stdout?.destroy(),i.stderr?.destroy();try{i.pid===void 0?i.kill(`SIGKILL`):C(i.pid)}catch{i.kill(`SIGKILL`)}}},h=setTimeout(()=>m(`timed out after ${f/1e3} seconds`),f),g=(e,t)=>{if(c+=e.byteLength,c>p){m(`the harness printed more than ${p/1024/1024} MB of output`);return}t.push(e)};i.stdout?.on(`data`,e=>g(e,o)),i.stderr?.on(`data`,e=>g(e,s));let _=(e,t)=>{u||(u=!0,clearTimeout(h),a&&y.delete(a),r({stdout:Buffer.concat(o).toString(`utf8`),stderr:Buffer.concat(s).toString(`utf8`),exitCode:e,failureReason:l??(t?`failed to run the harness: ${t.message}`:void 0)}))};i.on(`error`,e=>_(void 0,e)),i.on(`close`,e=>_(e??void 0))})}function S(){if(!b){b=!0;for(let e of[`SIGINT`,`SIGTERM`])process.once(e,()=>{for(let e of y){try{C(e.pid)}catch{}i.forciblyRemoveDirectorySync(e.tempRoot)}process.kill(process.pid,e)})}}function C(e){process.platform===`win32`?d.default.spawnSync(`taskkill`,[`/PID`,String(e),`/T`,`/F`],{stdio:`ignore`}):process.kill(-e,`SIGKILL`)}function w(){let e={...process.env};return delete e.EXERCODE_SANDBOX_USER,e}function T(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 E(e){let t=[];return await D(e,t),t.toSorted()}async function D(e,t){let n=await l.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 D(u.default.join(e,r.name),t)}async function O(e){try{return await l.default.stat(e),!0}catch{return!1}}function k(e){return e.length<=m?e:`${e.slice(0,m)}...`}exports.checkAllProblems=g;
|
|
2
|
+
//# sourceMappingURL=checkAllProblems.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkAllProblems.cjs","names":["DecisionCode","path","findDefaultStdioHarnessFiles","formatDefaultHarnessError","findModelAnswerDirs","readTestCases","readProblemMarkdownFrontMatter","judgesWithoutTestCases","MISSING_TEST_CASES_ERROR","findFailingModelAnswerDirs","copyProblemDirToTemporaryRoot","forciblyRemoveDirectory","TEST_CASE_RESULT_PREFIX","testCaseResultSchema","child_process","fs"],"sources":["../../src/cli/checkAllProblems.ts"],"sourcesContent":["import child_process from 'node:child_process';\nimport fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport {\n copyProblemDirToTemporaryRoot,\n forciblyRemoveDirectory,\n forciblyRemoveDirectorySync,\n} from '../helpers/checkProblemDirIsolation.js';\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 { 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 copiedProblemDir,\n tempRoot\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\ninterface HarnessProcessResult {\n stdout: string;\n stderr: string;\n exitCode: number | undefined;\n failureReason: string | undefined;\n}\n\ninterface LiveHarnessRun {\n pid: number;\n tempRoot: string;\n}\n\n// Detached harness groups no longer receive the terminal's SIGINT, so an interrupted check run must\n// tear them down (and remove their temp copies) itself before exiting.\nconst liveHarnessRuns = new Set<LiveHarnessRun>();\nlet signalHandlersInstalled = false;\n\n/**\n * Run the harness in its own process group, killing the whole group on timeout or when the output\n * cap is exceeded so grandchild submission processes cannot outlive the run.\n */\nfunction runHarnessProcess(\n commandArgs: readonly string[],\n cwd: string,\n tempRoot: string\n): Promise<HarnessProcessResult> {\n installSignalHandlers();\n return new Promise((resolve) => {\n // process.execPath keeps the harness on the same bun executable regardless of PATH.\n const child = child_process.spawn(process.execPath, commandArgs, {\n cwd,\n detached: process.platform !== 'win32',\n env: createHarnessEnv(),\n stdio: ['ignore', 'pipe', 'pipe'],\n });\n const liveRun: LiveHarnessRun | undefined = child.pid === undefined ? undefined : { pid: child.pid, tempRoot };\n if (liveRun) liveHarnessRuns.add(liveRun);\n const stdoutChunks: Buffer[] = [];\n const stderrChunks: Buffer[] = [];\n let totalOutputBytes = 0;\n let failureReason: string | undefined;\n let settled = false;\n\n const killProcessGroup = (reason: string): void => {\n if (failureReason !== undefined) return;\n failureReason = reason;\n // Stop buffering immediately: OS pipe buffers can keep emitting data after the kill, and\n // destroyed streams also let `close` fire even if a stray grandchild inherited the pipes.\n child.stdout?.destroy();\n child.stderr?.destroy();\n try {\n if (child.pid === undefined) {\n child.kill('SIGKILL');\n } else {\n killHarnessTree(child.pid);\n }\n } catch {\n child.kill('SIGKILL');\n }\n };\n const timeoutId = setTimeout(\n () => killProcessGroup(`timed out after ${RUN_TIMEOUT_MS / 1000} seconds`),\n RUN_TIMEOUT_MS\n );\n\n const appendOutput = (chunk: Buffer, chunks: Buffer[]): void => {\n totalOutputBytes += chunk.byteLength;\n if (totalOutputBytes > MAX_RUN_OUTPUT_BYTES) {\n killProcessGroup(`the harness printed more than ${MAX_RUN_OUTPUT_BYTES / 1024 / 1024} MB of output`);\n return;\n }\n chunks.push(chunk);\n };\n child.stdout?.on('data', (chunk: Buffer) => appendOutput(chunk, stdoutChunks));\n child.stderr?.on('data', (chunk: Buffer) => appendOutput(chunk, stderrChunks));\n\n const settle = (exitCode: number | undefined, spawnError?: Error): void => {\n if (settled) return;\n settled = true;\n clearTimeout(timeoutId);\n if (liveRun) liveHarnessRuns.delete(liveRun);\n resolve({\n stdout: Buffer.concat(stdoutChunks).toString('utf8'),\n stderr: Buffer.concat(stderrChunks).toString('utf8'),\n exitCode,\n failureReason: failureReason ?? (spawnError ? `failed to run the harness: ${spawnError.message}` : undefined),\n });\n };\n child.on('error', (error) => settle(undefined, error));\n child.on('close', (exitCode) => settle(exitCode ?? undefined));\n });\n}\n\nfunction installSignalHandlers(): void {\n if (signalHandlersInstalled) return;\n signalHandlersInstalled = true;\n for (const signal of ['SIGINT', 'SIGTERM'] as const) {\n process.once(signal, () => {\n for (const liveRun of liveHarnessRuns) {\n try {\n killHarnessTree(liveRun.pid);\n } catch {\n // The tree already exited.\n }\n // Best-effort cleanup while exiting; judged code may have left permission-locked entries.\n forciblyRemoveDirectorySync(liveRun.tempRoot);\n }\n process.kill(process.pid, signal);\n });\n }\n}\n\n// On Windows the harness is not detached (process groups are unavailable), so killing only the\n// direct child would leave submission grandchildren running; taskkill terminates the whole tree.\nfunction killHarnessTree(pid: number): void {\n if (process.platform === 'win32') {\n child_process.spawnSync('taskkill', ['/PID', String(pid), '/T', '/F'], { stdio: 'ignore' });\n } else {\n process.kill(-pid, 'SIGKILL');\n }\n}\n\n// The all-problem check runs harnesses concurrently and has no sandbox-delegation contract, while the sandbox\n// helpers assume one harness at a time (their pkill targets every process of the sandbox user), so\n// never forward the sandbox user to judged runs.\nfunction createHarnessEnv(): NodeJS.ProcessEnv {\n const env = { ...process.env };\n delete env.EXERCODE_SANDBOX_USER;\n return env;\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":"uoBAkBA,MAAM,EAAiB,IACjB,EAAuB,SACvB,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,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,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Judge all model answers of all problems (directories containing `problem.md` or
|
|
3
|
+
* `<id>.problem.md`) under a root directory: `model_answers/*` must be fully accepted and
|
|
4
|
+
* `model_answers.fails/*` must fail at least one test case. Returns the process exit code.
|
|
5
|
+
*/
|
|
6
|
+
export declare function checkAllProblems(args: readonly string[]): Promise<number>;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{findDefaultStdioHarnessFiles as e}from"../helpers/defaultStdioHarness.js";import{TEST_CASE_RESULT_PREFIX as t,testCaseResultSchema as n}from"../types/testCaseResult.js";import{DecisionCode as r}from"../types/decisionCode.js";import{copyProblemDirToTemporaryRoot as i,forciblyRemoveDirectory as a,forciblyRemoveDirectorySync as o}from"../helpers/checkProblemDirIsolation.js";import{judgesWithoutTestCases as s,readProblemMarkdownFrontMatter as c}from"../helpers/readProblemMarkdownFrontMatter.js";import{readTestCases as l}from"../helpers/readTestCases.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:fs/promises";import h from"node:path";import g from"node:child_process";const _=6e5,v=67108864,y=1e3,b=new Map(Object.entries(r).map(([e,t])=>[t,e]));async function x(t){let n=A(t),r=h.resolve(n.rootDir),i=e=>(h.relative(r,e)||`.`).replaceAll(h.sep,`/`),a=await j(r),o=a.filter(e=>{let t=i(e);return n.only.length>0&&!n.only.some(e=>t.includes(e))?!1:!n.skip.some(e=>t.includes(e))});if(o.length===0)return console.error(a.length===0?`No problem directories (containing problem.md or <id>.problem.md) found under ${r}.`:`All ${a.length} problem directories under ${r} were excluded by --only/--skip.`),1;let m=[],g=[];for(let t of o){let n=await e(t);if(n.length>0){m.push(`${i(t)}: ${p(n)}`);continue}let r=await d(t);if(r.length===0){m.push(`${i(t)}: no model answers found under model_answers/`);continue}if(!await N(h.join(t,`judge.ts`))&&(await l(h.join(t,`test_cases`))).length===0){let e;try{e=await c(t)}catch(e){m.push(`${i(t)}: failed to read the problem markdown front matter: ${e instanceof Error?e.message:String(e)}`);continue}if(!s(e)){m.push(`${i(t)}: ${f}`);continue}}let a=await u(t);g.push(...r.map(e=>({problemDir:t,answerDir:e,expectation:`accepted`})),...a.map(e=>({problemDir:t,answerDir:e,expectation:`rejected`})))}for(let e of m)console.error(`✗ ${e}`);let _=h.resolve(process.argv[1]??``),v=0,y=0;return await Promise.all(Array.from({length:Math.max(1,Math.min(n.concurrency,g.length))},async()=>{for(;y<g.length;){let e=g[y++];if(!e)return;let t=`${i(e.problemDir)} ${h.relative(e.problemDir,e.answerDir).replaceAll(h.sep,`/`)}`,n=await S(e,_);n===void 0?(v++,console.info(`✓ ${t}`)):(m.push(`${t}: ${n}`),console.error(`✗ ${t}: ${n}`))}})),console.info(`\n${v} passed, ${m.length} failed (${g.length} runs, ${o.length} problems)`),m.length===0?0:1}async function S(e,t){let n,r;try{({tempRoot:n,copiedProblemDir:r}=await i(e.problemDir))}catch(e){return P(`failed to copy the problem directory to a temporary location: ${e instanceof Error?e.message:String(e)}`)}let o,s;try{o=C(e,await E([`run`,t,`judge`,h.relative(e.problemDir,e.answerDir)],r,n))}catch(e){o=P(`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:P(`${o}; ${c}`)}function C(e,i){let{stdout:a,stderr:o}=i;if(i.failureReason!==void 0)return P(i.failureReason);if(i.exitCode!==0)return P(`harness exited with ${i.exitCode??`a signal`}${o.trim()?`: ${o.trim()}`:``}`);let s=a.split(/\r?\n/).filter(e=>e.startsWith(t)),c=[];for(let e of s){let r;try{r=n.safeParse(JSON.parse(e.slice(t.length)))}catch{r=void 0}if(!r?.success)return P(`malformed test case result line: ${e}`);c.push(r.data)}if(c.length===0)return`no test case results were printed`;if(e.expectation===`accepted`){let e=c.find(e=>e.decisionCode!==r.ACCEPTED);return e?P(`${b.get(e.decisionCode)??e.decisionCode} on test case ${e.testCaseId}${e.stderr?.trim()?`: ${e.stderr.trim()}`:``}`):void 0}return c.every(e=>e.decisionCode===r.ACCEPTED)?`expected at least one failing test case, but all test cases were accepted`:void 0}const w=new Set;let T=!1;function E(e,t,n){return D(),new Promise(r=>{let i=g.spawn(process.execPath,e,{cwd:t,detached:process.platform!==`win32`,env:k(),stdio:[`ignore`,`pipe`,`pipe`]}),a=i.pid===void 0?void 0:{pid:i.pid,tempRoot:n};a&&w.add(a);let o=[],s=[],c=0,l,u=!1,d=e=>{if(l===void 0){l=e,i.stdout?.destroy(),i.stderr?.destroy();try{i.pid===void 0?i.kill(`SIGKILL`):O(i.pid)}catch{i.kill(`SIGKILL`)}}},f=setTimeout(()=>d(`timed out after ${_/1e3} seconds`),_),p=(e,t)=>{if(c+=e.byteLength,c>v){d(`the harness printed more than ${v/1024/1024} MB of output`);return}t.push(e)};i.stdout?.on(`data`,e=>p(e,o)),i.stderr?.on(`data`,e=>p(e,s));let m=(e,t)=>{u||(u=!0,clearTimeout(f),a&&w.delete(a),r({stdout:Buffer.concat(o).toString(`utf8`),stderr:Buffer.concat(s).toString(`utf8`),exitCode:e,failureReason:l??(t?`failed to run the harness: ${t.message}`:void 0)}))};i.on(`error`,e=>m(void 0,e)),i.on(`close`,e=>m(e??void 0))})}function D(){if(!T){T=!0;for(let e of[`SIGINT`,`SIGTERM`])process.once(e,()=>{for(let e of w){try{O(e.pid)}catch{}o(e.tempRoot)}process.kill(process.pid,e)})}}function O(e){process.platform===`win32`?g.spawnSync(`taskkill`,[`/PID`,String(e),`/T`,`/F`],{stdio:`ignore`}):process.kill(-e,`SIGKILL`)}function k(){let e={...process.env};return delete e.EXERCODE_SANDBOX_USER,e}function A(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 j(e){let t=[];return await M(e,t),t.toSorted()}async function M(e,t){let n=await m.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 M(h.join(e,r.name),t)}async function N(e){try{return await m.stat(e),!0}catch{return!1}}function P(e){return e.length<=y?e:`${e.slice(0,y)}...`}export{x as checkAllProblems};
|
|
2
|
+
//# sourceMappingURL=checkAllProblems.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkAllProblems.js","names":[],"sources":["../../src/cli/checkAllProblems.ts"],"sourcesContent":["import child_process from 'node:child_process';\nimport fs from 'node:fs/promises';\nimport path from 'node:path';\n\nimport {\n copyProblemDirToTemporaryRoot,\n forciblyRemoveDirectory,\n forciblyRemoveDirectorySync,\n} from '../helpers/checkProblemDirIsolation.js';\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 { 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 copiedProblemDir,\n tempRoot\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\ninterface HarnessProcessResult {\n stdout: string;\n stderr: string;\n exitCode: number | undefined;\n failureReason: string | undefined;\n}\n\ninterface LiveHarnessRun {\n pid: number;\n tempRoot: string;\n}\n\n// Detached harness groups no longer receive the terminal's SIGINT, so an interrupted check run must\n// tear them down (and remove their temp copies) itself before exiting.\nconst liveHarnessRuns = new Set<LiveHarnessRun>();\nlet signalHandlersInstalled = false;\n\n/**\n * Run the harness in its own process group, killing the whole group on timeout or when the output\n * cap is exceeded so grandchild submission processes cannot outlive the run.\n */\nfunction runHarnessProcess(\n commandArgs: readonly string[],\n cwd: string,\n tempRoot: string\n): Promise<HarnessProcessResult> {\n installSignalHandlers();\n return new Promise((resolve) => {\n // process.execPath keeps the harness on the same bun executable regardless of PATH.\n const child = child_process.spawn(process.execPath, commandArgs, {\n cwd,\n detached: process.platform !== 'win32',\n env: createHarnessEnv(),\n stdio: ['ignore', 'pipe', 'pipe'],\n });\n const liveRun: LiveHarnessRun | undefined = child.pid === undefined ? undefined : { pid: child.pid, tempRoot };\n if (liveRun) liveHarnessRuns.add(liveRun);\n const stdoutChunks: Buffer[] = [];\n const stderrChunks: Buffer[] = [];\n let totalOutputBytes = 0;\n let failureReason: string | undefined;\n let settled = false;\n\n const killProcessGroup = (reason: string): void => {\n if (failureReason !== undefined) return;\n failureReason = reason;\n // Stop buffering immediately: OS pipe buffers can keep emitting data after the kill, and\n // destroyed streams also let `close` fire even if a stray grandchild inherited the pipes.\n child.stdout?.destroy();\n child.stderr?.destroy();\n try {\n if (child.pid === undefined) {\n child.kill('SIGKILL');\n } else {\n killHarnessTree(child.pid);\n }\n } catch {\n child.kill('SIGKILL');\n }\n };\n const timeoutId = setTimeout(\n () => killProcessGroup(`timed out after ${RUN_TIMEOUT_MS / 1000} seconds`),\n RUN_TIMEOUT_MS\n );\n\n const appendOutput = (chunk: Buffer, chunks: Buffer[]): void => {\n totalOutputBytes += chunk.byteLength;\n if (totalOutputBytes > MAX_RUN_OUTPUT_BYTES) {\n killProcessGroup(`the harness printed more than ${MAX_RUN_OUTPUT_BYTES / 1024 / 1024} MB of output`);\n return;\n }\n chunks.push(chunk);\n };\n child.stdout?.on('data', (chunk: Buffer) => appendOutput(chunk, stdoutChunks));\n child.stderr?.on('data', (chunk: Buffer) => appendOutput(chunk, stderrChunks));\n\n const settle = (exitCode: number | undefined, spawnError?: Error): void => {\n if (settled) return;\n settled = true;\n clearTimeout(timeoutId);\n if (liveRun) liveHarnessRuns.delete(liveRun);\n resolve({\n stdout: Buffer.concat(stdoutChunks).toString('utf8'),\n stderr: Buffer.concat(stderrChunks).toString('utf8'),\n exitCode,\n failureReason: failureReason ?? (spawnError ? `failed to run the harness: ${spawnError.message}` : undefined),\n });\n };\n child.on('error', (error) => settle(undefined, error));\n child.on('close', (exitCode) => settle(exitCode ?? undefined));\n });\n}\n\nfunction installSignalHandlers(): void {\n if (signalHandlersInstalled) return;\n signalHandlersInstalled = true;\n for (const signal of ['SIGINT', 'SIGTERM'] as const) {\n process.once(signal, () => {\n for (const liveRun of liveHarnessRuns) {\n try {\n killHarnessTree(liveRun.pid);\n } catch {\n // The tree already exited.\n }\n // Best-effort cleanup while exiting; judged code may have left permission-locked entries.\n forciblyRemoveDirectorySync(liveRun.tempRoot);\n }\n process.kill(process.pid, signal);\n });\n }\n}\n\n// On Windows the harness is not detached (process groups are unavailable), so killing only the\n// direct child would leave submission grandchildren running; taskkill terminates the whole tree.\nfunction killHarnessTree(pid: number): void {\n if (process.platform === 'win32') {\n child_process.spawnSync('taskkill', ['/PID', String(pid), '/T', '/F'], { stdio: 'ignore' });\n } else {\n process.kill(-pid, 'SIGKILL');\n }\n}\n\n// The all-problem check runs harnesses concurrently and has no sandbox-delegation contract, while the sandbox\n// helpers assume one harness at a time (their pkill targets every process of the sandbox user), so\n// never forward the sandbox user to judged runs.\nfunction createHarnessEnv(): NodeJS.ProcessEnv {\n const env = { ...process.env };\n delete env.EXERCODE_SANDBOX_USER;\n return env;\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":"s1BAkBA,MAAM,EAAiB,IACjB,EAAuB,SACvB,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,QAAQ,MACN,EAAe,SAAW,EACtB,iFAAiF,E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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,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,EACd,CAAC,EAAM,YAAY,GAAK,EAAM,OAAS,gBAAkB,EAAM,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"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";const e=require("./runSingleHarness.cjs"),t=require("./checkAllProblems.cjs"),n=`Usage: exercode [rootDir] [--only <substring>]... [--skip <substring>]... [--concurrency <n>]
|
|
2
|
+
exercode judge <answerDir> [paramsJson]
|
|
3
|
+
exercode debug <answerDir> [paramsJson]
|
|
4
|
+
|
|
5
|
+
Without a subcommand, judge all model answers of all problems under rootDir
|
|
6
|
+
(default: the current directory). Use \`judge\` or \`debug\` in a problem
|
|
7
|
+
directory to run one answer directory, e.g.:
|
|
8
|
+
exercode debug model_answers/python '{ "stdin": "1 2" }'`;r();async function r(){let r=process.argv.slice(2);if(r[0]===`--help`||r[0]===`-h`){console.info(n);return}try{if(r[0]===`judge`||r[0]===`debug`){if(r.length<2){console.error(n),process.exitCode=1;return}process.argv.splice(2,1),process.exitCode=await e.runSingleHarness(r[0])}else process.exitCode=await t.checkAllProblems(r)}catch(e){console.error(e),process.exitCode=1}}
|
|
9
|
+
//# sourceMappingURL=exercode.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exercode.cjs","names":["runSingleHarness","checkAllProblems"],"sources":["../../src/cli/exercode.ts"],"sourcesContent":["import { checkAllProblems } from './checkAllProblems.js';\nimport { runSingleHarness } from './runSingleHarness.js';\n\nconst usage = `Usage: exercode [rootDir] [--only <substring>]... [--skip <substring>]... [--concurrency <n>]\n exercode judge <answerDir> [paramsJson]\n exercode debug <answerDir> [paramsJson]\n\nWithout a subcommand, judge all model answers of all problems under rootDir\n(default: the current directory). Use \\`judge\\` or \\`debug\\` in a problem\ndirectory to run one answer directory, e.g.:\n exercode debug model_answers/python '{ \"stdin\": \"1 2\" }'`;\n\n// oxlint-disable-next-line unicorn/prefer-top-level-await -- the CJS build output does not support top-level await\nvoid main();\n\nasync function main(): Promise<void> {\n const args = process.argv.slice(2);\n if (args[0] === '--help' || args[0] === '-h') {\n // console.log would be stripped from the built CLI, so print the usage with console.info.\n console.info(usage);\n return;\n }\n try {\n if (args[0] === 'judge' || args[0] === 'debug') {\n if (args.length < 2) {\n console.error(usage);\n process.exitCode = 1;\n return;\n }\n // Harness scripts and presets read the answer directory and params from fixed positions in\n // `process.argv`, so drop the subcommand to restore those positions.\n process.argv.splice(2, 1);\n process.exitCode = await runSingleHarness(args[0]);\n } else {\n process.exitCode = await checkAllProblems(args);\n }\n } catch (error) {\n // Print the whole error: harness failures need their stack and `cause`, not just the message.\n console.error(error);\n process.exitCode = 1;\n }\n}\n"],"mappings":"2FAGM,EAAQ;;;;;;;4DAUT,EAAK,EAEV,eAAe,GAAsB,CACnC,IAAM,EAAO,QAAQ,KAAK,MAAM,CAAC,EACjC,GAAI,EAAK,KAAO,UAAY,EAAK,KAAO,KAAM,CAE5C,QAAQ,KAAK,CAAK,EAClB,MACF,CACA,GAAI,CACF,GAAI,EAAK,KAAO,SAAW,EAAK,KAAO,QAAS,CAC9C,GAAI,EAAK,OAAS,EAAG,CACnB,QAAQ,MAAM,CAAK,EACnB,QAAQ,SAAW,EACnB,MACF,CAGA,QAAQ,KAAK,OAAO,EAAG,CAAC,EACxB,QAAQ,SAAW,MAAMA,EAAAA,iBAAiB,EAAK,EAAE,CACnD,KACE,SAAQ,SAAW,MAAMC,EAAAA,iBAAiB,CAAI,CAElD,OAAS,EAAO,CAEd,QAAQ,MAAM,CAAK,EACnB,QAAQ,SAAW,CACrB,CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import{runSingleHarness as e}from"./runSingleHarness.js";import{checkAllProblems as t}from"./checkAllProblems.js";const n=`Usage: exercode [rootDir] [--only <substring>]... [--skip <substring>]... [--concurrency <n>]
|
|
2
|
+
exercode judge <answerDir> [paramsJson]
|
|
3
|
+
exercode debug <answerDir> [paramsJson]
|
|
4
|
+
|
|
5
|
+
Without a subcommand, judge all model answers of all problems under rootDir
|
|
6
|
+
(default: the current directory). Use \`judge\` or \`debug\` in a problem
|
|
7
|
+
directory to run one answer directory, e.g.:
|
|
8
|
+
exercode debug model_answers/python '{ "stdin": "1 2" }'`;r();async function r(){let r=process.argv.slice(2);if(r[0]===`--help`||r[0]===`-h`){console.info(n);return}try{if(r[0]===`judge`||r[0]===`debug`){if(r.length<2){console.error(n),process.exitCode=1;return}process.argv.splice(2,1),process.exitCode=await e(r[0])}else process.exitCode=await t(r)}catch(e){console.error(e),process.exitCode=1}}
|
|
9
|
+
//# sourceMappingURL=exercode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exercode.js","names":[],"sources":["../../src/cli/exercode.ts"],"sourcesContent":["import { checkAllProblems } from './checkAllProblems.js';\nimport { runSingleHarness } from './runSingleHarness.js';\n\nconst usage = `Usage: exercode [rootDir] [--only <substring>]... [--skip <substring>]... [--concurrency <n>]\n exercode judge <answerDir> [paramsJson]\n exercode debug <answerDir> [paramsJson]\n\nWithout a subcommand, judge all model answers of all problems under rootDir\n(default: the current directory). Use \\`judge\\` or \\`debug\\` in a problem\ndirectory to run one answer directory, e.g.:\n exercode debug model_answers/python '{ \"stdin\": \"1 2\" }'`;\n\n// oxlint-disable-next-line unicorn/prefer-top-level-await -- the CJS build output does not support top-level await\nvoid main();\n\nasync function main(): Promise<void> {\n const args = process.argv.slice(2);\n if (args[0] === '--help' || args[0] === '-h') {\n // console.log would be stripped from the built CLI, so print the usage with console.info.\n console.info(usage);\n return;\n }\n try {\n if (args[0] === 'judge' || args[0] === 'debug') {\n if (args.length < 2) {\n console.error(usage);\n process.exitCode = 1;\n return;\n }\n // Harness scripts and presets read the answer directory and params from fixed positions in\n // `process.argv`, so drop the subcommand to restore those positions.\n process.argv.splice(2, 1);\n process.exitCode = await runSingleHarness(args[0]);\n } else {\n process.exitCode = await checkAllProblems(args);\n }\n } catch (error) {\n // Print the whole error: harness failures need their stack and `cause`, not just the message.\n console.error(error);\n process.exitCode = 1;\n }\n}\n"],"mappings":"kHAGA,MAAM,EAAQ;;;;;;;4DAUT,EAAK,EAEV,eAAe,GAAsB,CACnC,IAAM,EAAO,QAAQ,KAAK,MAAM,CAAC,EACjC,GAAI,EAAK,KAAO,UAAY,EAAK,KAAO,KAAM,CAE5C,QAAQ,KAAK,CAAK,EAClB,MACF,CACA,GAAI,CACF,GAAI,EAAK,KAAO,SAAW,EAAK,KAAO,QAAS,CAC9C,GAAI,EAAK,OAAS,EAAG,CACnB,QAAQ,MAAM,CAAK,EACnB,QAAQ,SAAW,EACnB,MACF,CAGA,QAAQ,KAAK,OAAO,EAAG,CAAC,EACxB,QAAQ,SAAW,MAAM,EAAiB,EAAK,EAAE,CACnD,KACE,SAAQ,SAAW,MAAM,EAAiB,CAAI,CAElD,OAAS,EAAO,CAEd,QAAQ,MAAM,CAAK,EACnB,QAAQ,SAAW,CACrB,CACF"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("../helpers/defaultStdioHarness.cjs"),n=require("../helpers/readProblemMarkdownFrontMatter.cjs"),r=require("../helpers/readTestCases.cjs"),i=require("../presets/stdio.cjs");let a=require("node:path");a=e.__toESM(a,1);let o=require("node:child_process");o=e.__toESM(o,1);let s=require("node:fs");s=e.__toESM(s,1);const c=`a standard stdio problem (without judge.ts) needs at least one test case under test_cases/, static-analysis rules (e.g. requiredRegExpsInCode), or isManualScoringRequired`;async function l(e){let l=process.cwd(),d=await t.findDefaultStdioHarnessFiles(l);if(d.length>0)return console.error(u(d)),1;let f=e===`judge`?`judge.ts`:`debug.ts`;if(s.default.existsSync(a.default.join(l,f))){let e=o.default.spawnSync(process.execPath,[`run`,f,...process.argv.slice(2)],{cwd:l,stdio:`inherit`});return e.error?(console.error(`failed to run ${f}: ${e.error.message}`),1):e.status??1}return e===`debug`&&s.default.existsSync(a.default.join(l,`judge.ts`))?(console.error(`This problem has a custom judge.ts but no debug.ts; add a debug.ts to support debugging.`),1):e===`judge`&&(await r.readTestCases(a.default.join(l,`test_cases`))).length===0&&!n.judgesWithoutTestCases(await n.readProblemMarkdownFrontMatter(l))?(console.error(c),1):(await(e===`judge`?i.stdioJudgePreset(l):i.stdioDebugPreset(l)),0)}function u(e){let t=e.join(` and `);return`${t} must not be committed because the content is identical to the default stdio harness.
|
|
2
|
+
A problem without judge.ts is automatically judged with stdioJudgePreset and debugged with stdioDebugPreset,
|
|
3
|
+
so delete ${t} and run the problem with \`bun x exercode judge <answerDir>\` or \`bun x exercode debug <answerDir>\`.
|
|
4
|
+
If the file intentionally demonstrates the default harness, add an explanatory comment to mark it as custom.`}exports.MISSING_TEST_CASES_ERROR=c,exports.formatDefaultHarnessError=u,exports.runSingleHarness=l;
|
|
5
|
+
//# sourceMappingURL=runSingleHarness.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runSingleHarness.cjs","names":["findDefaultStdioHarnessFiles","fs","path","child_process","readTestCases","judgesWithoutTestCases","readProblemMarkdownFrontMatter","stdioJudgePreset","stdioDebugPreset"],"sources":["../../src/cli/runSingleHarness.ts"],"sourcesContent":["import child_process from 'node:child_process';\nimport fs from 'node:fs';\nimport path from 'node:path';\n\nimport { findDefaultStdioHarnessFiles } from '../helpers/defaultStdioHarness.js';\nimport { judgesWithoutTestCases, readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { readTestCases } from '../helpers/readTestCases.js';\nimport { stdioDebugPreset, stdioJudgePreset } from '../presets/stdio.js';\n\nexport const MISSING_TEST_CASES_ERROR =\n 'a standard stdio problem (without judge.ts) needs at least one test case under test_cases/, static-analysis rules (e.g. requiredRegExpsInCode), or isManualScoringRequired';\n\n/**\n * Run the judge or debug harness of the problem in the current working directory: a custom\n * `judge.ts` / `debug.ts` when present, or the default stdio preset otherwise (mirroring the\n * Exercode server, which applies the stdio presets to problems without these files).\n */\nexport async function runSingleHarness(kind: 'judge' | 'debug'): Promise<number> {\n const problemDir = process.cwd();\n\n const defaultHarnessFileNames = await findDefaultStdioHarnessFiles(problemDir);\n if (defaultHarnessFileNames.length > 0) {\n console.error(formatDefaultHarnessError(defaultHarnessFileNames));\n return 1;\n }\n\n const harnessFileName = kind === 'judge' ? 'judge.ts' : 'debug.ts';\n if (fs.existsSync(path.join(problemDir, harnessFileName))) {\n // process.execPath keeps the harness on the same bun executable regardless of PATH.\n const spawnResult = child_process.spawnSync(process.execPath, ['run', harnessFileName, ...process.argv.slice(2)], {\n cwd: problemDir,\n stdio: 'inherit',\n });\n if (spawnResult.error) {\n console.error(`failed to run ${harnessFileName}: ${spawnResult.error.message}`);\n return 1;\n }\n return spawnResult.status ?? 1;\n }\n\n // A custom judge.ts marks the problem as non-standard, so the stdio debug preset would judge the\n // answer against a contract the problem does not have (mirroring the server, which reports debug\n // as unsupported in this case).\n if (kind === 'debug' && fs.existsSync(path.join(problemDir, 'judge.ts'))) {\n console.error('This problem has a custom judge.ts but no debug.ts; add a debug.ts to support debugging.');\n return 1;\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 (kind === 'judge') {\n const testCases = await readTestCases(path.join(problemDir, 'test_cases'));\n if (testCases.length === 0 && !judgesWithoutTestCases(await readProblemMarkdownFrontMatter(problemDir))) {\n console.error(MISSING_TEST_CASES_ERROR);\n return 1;\n }\n }\n\n // The presets read the answer directory and params from `process.argv`, which holds them at the\n // same positions as when a harness script is invoked directly.\n await (kind === 'judge' ? stdioJudgePreset(problemDir) : stdioDebugPreset(problemDir));\n return 0;\n}\n\nexport function formatDefaultHarnessError(defaultHarnessFileNames: readonly string[]): string {\n const fileNames = defaultHarnessFileNames.join(' and ');\n return `${fileNames} must not be committed because the content is identical to the default stdio harness.\nA problem without judge.ts is automatically judged with stdioJudgePreset and debugged with stdioDebugPreset,\nso delete ${fileNames} and run the problem with \\`bun x exercode judge <answerDir>\\` or \\`bun x exercode debug <answerDir>\\`.\nIf the file intentionally demonstrates the default harness, add an explanatory comment to mark it as custom.`;\n}\n"],"mappings":"ucASA,MAAa,EACX,6KAOF,eAAsB,EAAiB,EAA0C,CAC/E,IAAM,EAAa,QAAQ,IAAI,EAEzB,EAA0B,MAAMA,EAAAA,6BAA6B,CAAU,EAC7E,GAAI,EAAwB,OAAS,EAEnC,OADA,QAAQ,MAAM,EAA0B,CAAuB,CAAC,EACzD,EAGT,IAAM,EAAkB,IAAS,QAAU,WAAa,WACxD,GAAIC,EAAAA,QAAG,WAAWC,EAAAA,QAAK,KAAK,EAAY,CAAe,CAAC,EAAG,CAEzD,IAAM,EAAcC,EAAAA,QAAc,UAAU,QAAQ,SAAU,CAAC,MAAO,EAAiB,GAAG,QAAQ,KAAK,MAAM,CAAC,CAAC,EAAG,CAChH,IAAK,EACL,MAAO,SACT,CAAC,EAKD,OAJI,EAAY,OACd,QAAQ,MAAM,iBAAiB,EAAgB,IAAI,EAAY,MAAM,SAAS,EACvE,GAEF,EAAY,QAAU,CAC/B,CAwBA,OAnBI,IAAS,SAAWF,EAAAA,QAAG,WAAWC,EAAAA,QAAK,KAAK,EAAY,UAAU,CAAC,GACrE,QAAQ,MAAM,0FAA0F,EACjG,GAML,IAAS,UAEP,MADoBE,EAAAA,cAAcF,EAAAA,QAAK,KAAK,EAAY,YAAY,CAAC,EAAA,CAC3D,SAAW,GAAK,CAACG,EAAAA,uBAAuB,MAAMC,EAAAA,+BAA+B,CAAU,CAAC,GACpG,QAAQ,MAAM,CAAwB,EAC/B,IAMX,MAAO,IAAS,QAAUC,EAAAA,iBAAiB,CAAU,EAAIC,EAAAA,iBAAiB,CAAU,GAC7E,EACT,CAEA,SAAgB,EAA0B,EAAoD,CAC5F,IAAM,EAAY,EAAwB,KAAK,OAAO,EACtD,MAAO,GAAG,EAAU;;YAEV,EAAU;6GAEtB"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const MISSING_TEST_CASES_ERROR = "a standard stdio problem (without judge.ts) needs at least one test case under test_cases/, static-analysis rules (e.g. requiredRegExpsInCode), or isManualScoringRequired";
|
|
2
|
+
/**
|
|
3
|
+
* Run the judge or debug harness of the problem in the current working directory: a custom
|
|
4
|
+
* `judge.ts` / `debug.ts` when present, or the default stdio preset otherwise (mirroring the
|
|
5
|
+
* Exercode server, which applies the stdio presets to problems without these files).
|
|
6
|
+
*/
|
|
7
|
+
export declare function runSingleHarness(kind: 'judge' | 'debug'): Promise<number>;
|
|
8
|
+
export declare function formatDefaultHarnessError(defaultHarnessFileNames: readonly string[]): string;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import{findDefaultStdioHarnessFiles as e}from"../helpers/defaultStdioHarness.js";import{judgesWithoutTestCases as t,readProblemMarkdownFrontMatter as n}from"../helpers/readProblemMarkdownFrontMatter.js";import{readTestCases as r}from"../helpers/readTestCases.js";import{stdioDebugPreset as i,stdioJudgePreset as a}from"../presets/stdio.js";import o from"node:path";import s from"node:child_process";import c from"node:fs";const l=`a standard stdio problem (without judge.ts) needs at least one test case under test_cases/, static-analysis rules (e.g. requiredRegExpsInCode), or isManualScoringRequired`;async function u(u){let f=process.cwd(),p=await e(f);if(p.length>0)return console.error(d(p)),1;let m=u===`judge`?`judge.ts`:`debug.ts`;if(c.existsSync(o.join(f,m))){let e=s.spawnSync(process.execPath,[`run`,m,...process.argv.slice(2)],{cwd:f,stdio:`inherit`});return e.error?(console.error(`failed to run ${m}: ${e.error.message}`),1):e.status??1}return u===`debug`&&c.existsSync(o.join(f,`judge.ts`))?(console.error(`This problem has a custom judge.ts but no debug.ts; add a debug.ts to support debugging.`),1):u===`judge`&&(await r(o.join(f,`test_cases`))).length===0&&!t(await n(f))?(console.error(l),1):(await(u===`judge`?a(f):i(f)),0)}function d(e){let t=e.join(` and `);return`${t} must not be committed because the content is identical to the default stdio harness.
|
|
2
|
+
A problem without judge.ts is automatically judged with stdioJudgePreset and debugged with stdioDebugPreset,
|
|
3
|
+
so delete ${t} and run the problem with \`bun x exercode judge <answerDir>\` or \`bun x exercode debug <answerDir>\`.
|
|
4
|
+
If the file intentionally demonstrates the default harness, add an explanatory comment to mark it as custom.`}export{l as MISSING_TEST_CASES_ERROR,d as formatDefaultHarnessError,u as runSingleHarness};
|
|
5
|
+
//# sourceMappingURL=runSingleHarness.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runSingleHarness.js","names":[],"sources":["../../src/cli/runSingleHarness.ts"],"sourcesContent":["import child_process from 'node:child_process';\nimport fs from 'node:fs';\nimport path from 'node:path';\n\nimport { findDefaultStdioHarnessFiles } from '../helpers/defaultStdioHarness.js';\nimport { judgesWithoutTestCases, readProblemMarkdownFrontMatter } from '../helpers/readProblemMarkdownFrontMatter.js';\nimport { readTestCases } from '../helpers/readTestCases.js';\nimport { stdioDebugPreset, stdioJudgePreset } from '../presets/stdio.js';\n\nexport const MISSING_TEST_CASES_ERROR =\n 'a standard stdio problem (without judge.ts) needs at least one test case under test_cases/, static-analysis rules (e.g. requiredRegExpsInCode), or isManualScoringRequired';\n\n/**\n * Run the judge or debug harness of the problem in the current working directory: a custom\n * `judge.ts` / `debug.ts` when present, or the default stdio preset otherwise (mirroring the\n * Exercode server, which applies the stdio presets to problems without these files).\n */\nexport async function runSingleHarness(kind: 'judge' | 'debug'): Promise<number> {\n const problemDir = process.cwd();\n\n const defaultHarnessFileNames = await findDefaultStdioHarnessFiles(problemDir);\n if (defaultHarnessFileNames.length > 0) {\n console.error(formatDefaultHarnessError(defaultHarnessFileNames));\n return 1;\n }\n\n const harnessFileName = kind === 'judge' ? 'judge.ts' : 'debug.ts';\n if (fs.existsSync(path.join(problemDir, harnessFileName))) {\n // process.execPath keeps the harness on the same bun executable regardless of PATH.\n const spawnResult = child_process.spawnSync(process.execPath, ['run', harnessFileName, ...process.argv.slice(2)], {\n cwd: problemDir,\n stdio: 'inherit',\n });\n if (spawnResult.error) {\n console.error(`failed to run ${harnessFileName}: ${spawnResult.error.message}`);\n return 1;\n }\n return spawnResult.status ?? 1;\n }\n\n // A custom judge.ts marks the problem as non-standard, so the stdio debug preset would judge the\n // answer against a contract the problem does not have (mirroring the server, which reports debug\n // as unsupported in this case).\n if (kind === 'debug' && fs.existsSync(path.join(problemDir, 'judge.ts'))) {\n console.error('This problem has a custom judge.ts but no debug.ts; add a debug.ts to support debugging.');\n return 1;\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 (kind === 'judge') {\n const testCases = await readTestCases(path.join(problemDir, 'test_cases'));\n if (testCases.length === 0 && !judgesWithoutTestCases(await readProblemMarkdownFrontMatter(problemDir))) {\n console.error(MISSING_TEST_CASES_ERROR);\n return 1;\n }\n }\n\n // The presets read the answer directory and params from `process.argv`, which holds them at the\n // same positions as when a harness script is invoked directly.\n await (kind === 'judge' ? stdioJudgePreset(problemDir) : stdioDebugPreset(problemDir));\n return 0;\n}\n\nexport function formatDefaultHarnessError(defaultHarnessFileNames: readonly string[]): string {\n const fileNames = defaultHarnessFileNames.join(' and ');\n return `${fileNames} must not be committed because the content is identical to the default stdio harness.\nA problem without judge.ts is automatically judged with stdioJudgePreset and debugged with stdioDebugPreset,\nso delete ${fileNames} and run the problem with \\`bun x exercode judge <answerDir>\\` or \\`bun x exercode debug <answerDir>\\`.\nIf the file intentionally demonstrates the default harness, add an explanatory comment to mark it as custom.`;\n}\n"],"mappings":"saASA,MAAa,EACX,6KAOF,eAAsB,EAAiB,EAA0C,CAC/E,IAAM,EAAa,QAAQ,IAAI,EAEzB,EAA0B,MAAM,EAA6B,CAAU,EAC7E,GAAI,EAAwB,OAAS,EAEnC,OADA,QAAQ,MAAM,EAA0B,CAAuB,CAAC,EACzD,EAGT,IAAM,EAAkB,IAAS,QAAU,WAAa,WACxD,GAAI,EAAG,WAAW,EAAK,KAAK,EAAY,CAAe,CAAC,EAAG,CAEzD,IAAM,EAAc,EAAc,UAAU,QAAQ,SAAU,CAAC,MAAO,EAAiB,GAAG,QAAQ,KAAK,MAAM,CAAC,CAAC,EAAG,CAChH,IAAK,EACL,MAAO,SACT,CAAC,EAKD,OAJI,EAAY,OACd,QAAQ,MAAM,iBAAiB,EAAgB,IAAI,EAAY,MAAM,SAAS,EACvE,GAEF,EAAY,QAAU,CAC/B,CAwBA,OAnBI,IAAS,SAAW,EAAG,WAAW,EAAK,KAAK,EAAY,UAAU,CAAC,GACrE,QAAQ,MAAM,0FAA0F,EACjG,GAML,IAAS,UAEP,MADoB,EAAc,EAAK,KAAK,EAAY,YAAY,CAAC,EAAA,CAC3D,SAAW,GAAK,CAAC,EAAuB,MAAM,EAA+B,CAAU,CAAC,GACpG,QAAQ,MAAM,CAAwB,EAC/B,IAMX,MAAO,IAAS,QAAU,EAAiB,CAAU,EAAI,EAAiB,CAAU,GAC7E,EACT,CAEA,SAAgB,EAA0B,EAAoD,CAC5F,IAAM,EAAY,EAAwB,KAAK,OAAO,EACtD,MAAO,GAAG,EAAU;;YAEV,EAAU;6GAEtB"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("../types/testCaseResult.cjs"),n=require("../types/decisionCode.cjs"),r=require("./printDebugBanner.cjs");let i=require("node:
|
|
1
|
+
"use strict";const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("../types/testCaseResult.cjs"),n=require("../types/decisionCode.cjs"),r=require("./printDebugBanner.cjs");let i=require("node:path");i=e.__toESM(i,1);let a=require("node:child_process");a=e.__toESM(a,1);let o=require("node:fs");o=e.__toESM(o,1);let s=require("node:os");s=e.__toESM(s,1);async function c(e,t,n){let o;try{let s=await l(e);o=s.tempRoot;let{copiedProblemDir:c}=s,u=i.default.resolve(e),d=i.default.relative(u,i.default.resolve(t.cwd)),f=i.default.join(c,d),p=y(u);if(p.startsWith(`..`)||i.default.isAbsolute(p))return r.printDebugBanner([`[DEBUG MODE] isolated problem directory check skipped`,``,`The invoked judge script is located outside the problem directory.`,`Script path: ${p}`]),{passed:!0};let m=process.execArgv.filter(h),_=JSON.stringify(g(n)?n:{}),v=a.default.spawnSync(process.execPath,[...m,p,f,_],{cwd:c,encoding:`utf8`,env:process.env,timeout:3e4}),x=v.stdout??``,S=v.stderr??``;return v.status===0&&b(x)?(r.printDebugBanner([`[DEBUG MODE] isolated problem directory check passed`,``,`Copied problem dir : ${c}`,`Checked cwd : ${d}`]),{passed:!0}):(r.printDebugBanner([`[DEBUG MODE] isolated problem directory check failed`,``,`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.`,``,`Copied problem dir : ${c}`,`Checked cwd : ${d}`,`Exit status : ${v.status??v.signal??`unknown`}`,`Spawn error : ${v.error?.message??`<none>`}`,``,`stdout:`,x.trimEnd()||`<empty>`,``,`stderr:`,S.trimEnd()||`<empty>`]),{passed:!1})}catch(e){return r.printDebugBanner([`[DEBUG MODE] isolated problem directory check failed due to an unexpected error`,``,e instanceof Error?e.message:String(e)]),{passed:!1}}finally{o&&await u(o)}}async function l(e){let t=await o.default.promises.mkdtemp(i.default.join(s.default.tmpdir(),`problem-utils-isolation_`));try{let n=i.default.resolve(e),r=i.default.join(t,v(n));return await o.default.promises.mkdir(i.default.dirname(r),{recursive:!0}),await o.default.promises.cp(n,r,{recursive:!0,filter:p,verbatimSymlinks:!0}),await _(t,n),{tempRoot:t,copiedProblemDir:r}}catch(e){throw await u(t),e}}async function u(e){try{return await o.default.promises.rm(e,{recursive:!0,force:!0}),!0}catch{f(e);try{return await o.default.promises.rm(e,{recursive:!0,force:!0}),!0}catch{return!1}}}function d(e){try{return o.default.rmSync(e,{recursive:!0,force:!0}),!0}catch{f(e);try{return o.default.rmSync(e,{recursive:!0,force:!0}),!0}catch{return!1}}}function f(e){process.platform!==`win32`&&a.default.spawnSync(`chmod`,[`-R`,`u+rwX`,e])}async function p(e){if(!m(e))return!1;if((await o.default.promises.lstat(e)).isSymbolicLink()&&i.default.isAbsolute(await o.default.promises.readlink(e)))throw Error(`${e} is an absolute symlink, which would escape the temporary copy; use a relative symlink`);return!0}function m(e){let t=i.default.basename(e);return t!==`node_modules`&&t!==`.git`}function h(e){return!e.startsWith(`--inspect`)&&!e.startsWith(`--watch`)&&!e.startsWith(`--hot`)}function g(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}async function _(e,t){let n=i.default.resolve(t);for(;;){let t=i.default.join(n,`node_modules`);if(o.default.existsSync(t)){let r=i.default.join(e,v(n),`node_modules`);try{await o.default.promises.symlink(t,r,process.platform===`win32`?`junction`:`dir`)}catch{}}let r=i.default.dirname(n);if(r===n)break;n=r}}function v(e){let{root:t}=i.default.parse(e);return i.default.relative(t,e)}function y(e){let t=process.argv[1];if(!t)return`.${i.default.sep}judge.ts`;let n=i.default.relative(e,i.default.resolve(t));return i.default.isAbsolute(n)||n.startsWith(`.`)?n:`.${i.default.sep}${n}`}function b(e){let r=e.split(/\r?\n/).filter(e=>e.startsWith(t.TEST_CASE_RESULT_PREFIX));return r.length!==0&&r.every(e=>{try{let r=t.testCaseResultSchema.safeParse(JSON.parse(e.slice(t.TEST_CASE_RESULT_PREFIX.length)));return r.success&&r.data.decisionCode===n.DecisionCode.ACCEPTED}catch{return!1}})}exports.checkProblemDirIsolation=c,exports.copyProblemDirToTemporaryRoot=l,exports.forciblyRemoveDirectory=u,exports.forciblyRemoveDirectorySync=d;
|
|
2
2
|
//# sourceMappingURL=checkProblemDirIsolation.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkProblemDirIsolation.cjs","names":["fs","path","os","child_process","TEST_CASE_RESULT_PREFIX","testCaseResultSchema","DecisionCode"],"sources":["../../src/helpers/checkProblemDirIsolation.ts"],"sourcesContent":["import child_process from 'node:child_process';\nimport fs from 'node:fs';\nimport os from 'node:os';\nimport 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';\n\nconst ISOLATION_CHECK_TIMEOUT_MS = 30_000;\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): Promise<ProblemDirIsolationCheckResult> {\n let tempRoot: string | undefined;\n try {\n tempRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'problem-utils-isolation_'));\n const absoluteProblemDir = path.resolve(problemDir);\n const copiedProblemDir = path.join(tempRoot, toTempRelativePath(absoluteProblemDir));\n await fs.promises.mkdir(path.dirname(copiedProblemDir), { recursive: true });\n await fs.promises.cp(absoluteProblemDir, copiedProblemDir, {\n recursive: true,\n filter: isCopiedProblemPath,\n });\n await symlinkAllAncestorNodeModules(tempRoot, absoluteProblemDir);\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 spawnResult = child_process.spawnSync(process.execPath, [...execArgv, scriptPath, copiedCwd, paramsJson], {\n cwd: copiedProblemDir,\n encoding: 'utf8',\n env: process.env,\n timeout: ISOLATION_CHECK_TIMEOUT_MS,\n });\n const stdout = spawnResult.stdout ?? '';\n const stderr = spawnResult.stderr ?? '';\n\n if (spawnResult.status === 0 && isAcceptedJudgeOutput(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 '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 `Copied problem dir : ${copiedProblemDir}`,\n `Checked cwd : ${relativeCwd}`,\n `Exit status : ${spawnResult.status ?? spawnResult.signal ?? 'unknown'}`,\n `Spawn error : ${spawnResult.error?.message ?? '<none>'}`,\n '',\n 'stdout:',\n stdout.trimEnd() || '<empty>',\n '',\n 'stderr:',\n 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 if (tempRoot) {\n try {\n await fs.promises.rm(tempRoot, { recursive: true, force: true });\n } catch {\n // Cleanup errors should not mask the primary isolation check result.\n }\n }\n }\n}\n\nfunction isCopiedProblemPath(src: string): boolean {\n const name = path.basename(src);\n return name !== 'node_modules' && name !== '.git';\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\nasync function symlinkAllAncestorNodeModules(tempRoot: string, problemDir: string): Promise<void> {\n let currentDir = path.resolve(problemDir);\n while (true) {\n const nodeModulesPath = path.join(currentDir, 'node_modules');\n if (fs.existsSync(nodeModulesPath)) {\n const targetSymlinkPath = path.join(tempRoot, toTempRelativePath(currentDir), 'node_modules');\n try {\n await fs.promises.symlink(\n nodeModulesPath,\n targetSymlinkPath,\n process.platform === 'win32' ? 'junction' : 'dir'\n );\n } catch {\n // Package resolution is best-effort; the isolation check still reports a clear spawn failure if imports break.\n }\n }\n\n const parentDir = path.dirname(currentDir);\n if (parentDir === currentDir) break;\n currentDir = parentDir;\n }\n}\n\nfunction toTempRelativePath(absolutePath: string): string {\n const { root } = path.parse(absolutePath);\n return path.relative(root, absolutePath);\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":"2WAoBA,eAAsB,EACpB,EACA,EACA,EACyC,CACzC,IAAI,EACJ,GAAI,CACF,EAAW,MAAMA,EAAAA,QAAG,SAAS,QAAQC,EAAAA,QAAK,KAAKC,EAAAA,QAAG,OAAO,EAAG,0BAA0B,CAAC,EACvF,IAAM,EAAqBD,EAAAA,QAAK,QAAQ,CAAU,EAC5C,EAAmBA,EAAAA,QAAK,KAAK,EAAU,EAAmB,CAAkB,CAAC,EACnF,MAAMD,EAAAA,QAAG,SAAS,MAAMC,EAAAA,QAAK,QAAQ,CAAgB,EAAG,CAAE,UAAW,EAAK,CAAC,EAC3E,MAAMD,EAAAA,QAAG,SAAS,GAAG,EAAoB,EAAkB,CACzD,UAAW,GACX,OAAQ,CACV,CAAC,EACD,MAAM,EAA8B,EAAU,CAAkB,EAEhE,IAAM,EAAcC,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,OAA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AAK,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":["path","child_process","fs","os","TEST_CASE_RESULT_PREFIX","testCaseResultSchema","DecisionCode"],"sources":["../../src/helpers/checkProblemDirIsolation.ts"],"sourcesContent":["import child_process from 'node:child_process';\nimport fs from 'node:fs';\nimport os from 'node:os';\nimport 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';\n\nconst ISOLATION_CHECK_TIMEOUT_MS = 30_000;\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): Promise<ProblemDirIsolationCheckResult> {\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 spawnResult = child_process.spawnSync(process.execPath, [...execArgv, scriptPath, copiedCwd, paramsJson], {\n cwd: copiedProblemDir,\n encoding: 'utf8',\n env: process.env,\n timeout: ISOLATION_CHECK_TIMEOUT_MS,\n });\n const stdout = spawnResult.stdout ?? '';\n const stderr = spawnResult.stderr ?? '';\n\n if (spawnResult.status === 0 && isAcceptedJudgeOutput(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 '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 `Copied problem dir : ${copiedProblemDir}`,\n `Checked cwd : ${relativeCwd}`,\n `Exit status : ${spawnResult.status ?? spawnResult.signal ?? 'unknown'}`,\n `Spawn error : ${spawnResult.error?.message ?? '<none>'}`,\n '',\n 'stdout:',\n stdout.trimEnd() || '<empty>',\n '',\n 'stderr:',\n 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\n/**\n * Copy a problem directory into a temporary root, symlinking every ancestor `node_modules` so the\n * copied harness still resolves its imports. Callers must remove the returned `tempRoot`.\n */\nexport async function copyProblemDirToTemporaryRoot(\n problemDir: string\n): Promise<{ tempRoot: string; copiedProblemDir: string }> {\n const tempRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'problem-utils-isolation_'));\n try {\n const absoluteProblemDir = path.resolve(problemDir);\n const copiedProblemDir = path.join(tempRoot, toTempRelativePath(absoluteProblemDir));\n await fs.promises.mkdir(path.dirname(copiedProblemDir), { recursive: true });\n await fs.promises.cp(absoluteProblemDir, copiedProblemDir, {\n recursive: true,\n filter: rejectAbsoluteSymlinks,\n // Keep relative symlinks relative: the default rewrites them to absolute paths into the\n // source tree, so judging the copy could write through them into the checked repository.\n verbatimSymlinks: true,\n });\n await symlinkAllAncestorNodeModules(tempRoot, absoluteProblemDir);\n return { tempRoot, copiedProblemDir };\n } catch (error) {\n await forciblyRemoveDirectory(tempRoot);\n throw error;\n }\n}\n\n/**\n * Remove a temporary directory even when judged code left permission-locked entries in it (e.g. a\n * mode-000 directory makes a plain `fs.rm` fail with EACCES). Returns whether removal succeeded.\n */\nexport async function forciblyRemoveDirectory(dir: string): Promise<boolean> {\n try {\n await fs.promises.rm(dir, { recursive: true, force: true });\n return true;\n } catch {\n unlockPermissions(dir);\n try {\n await fs.promises.rm(dir, { recursive: true, force: true });\n return true;\n } catch {\n return false;\n }\n }\n}\n\n/** Synchronous variant of {@link forciblyRemoveDirectory} for signal handlers. */\nexport function forciblyRemoveDirectorySync(dir: string): boolean {\n try {\n fs.rmSync(dir, { recursive: true, force: true });\n return true;\n } catch {\n unlockPermissions(dir);\n try {\n fs.rmSync(dir, { recursive: true, force: true });\n return true;\n } catch {\n return false;\n }\n }\n}\n\n// chmod does not exist on Windows, where POSIX-mode locks cannot occur anyway. spawnSync reports\n// spawn failures via its return value instead of throwing, so no try/catch is needed.\nfunction unlockPermissions(dir: string): void {\n if (process.platform !== 'win32') child_process.spawnSync('chmod', ['-R', 'u+rwX', dir]);\n}\n\n// An absolute symlink is copied verbatim, so judging the copy could write through it into the\n// original tree; reject it instead of silently breaking the isolation guarantee.\nasync function rejectAbsoluteSymlinks(src: string): Promise<boolean> {\n if (!isCopiedProblemPath(src)) return false;\n const stats = await fs.promises.lstat(src);\n if (stats.isSymbolicLink() && path.isAbsolute(await fs.promises.readlink(src))) {\n throw new Error(`${src} is an absolute symlink, which would escape the temporary copy; use a relative symlink`);\n }\n return true;\n}\n\nfunction isCopiedProblemPath(src: string): boolean {\n const name = path.basename(src);\n return name !== 'node_modules' && name !== '.git';\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\nasync function symlinkAllAncestorNodeModules(tempRoot: string, problemDir: string): Promise<void> {\n let currentDir = path.resolve(problemDir);\n while (true) {\n const nodeModulesPath = path.join(currentDir, 'node_modules');\n if (fs.existsSync(nodeModulesPath)) {\n const targetSymlinkPath = path.join(tempRoot, toTempRelativePath(currentDir), 'node_modules');\n try {\n await fs.promises.symlink(\n nodeModulesPath,\n targetSymlinkPath,\n process.platform === 'win32' ? 'junction' : 'dir'\n );\n } catch {\n // Package resolution is best-effort; the isolation check still reports a clear spawn failure if imports break.\n }\n }\n\n const parentDir = path.dirname(currentDir);\n if (parentDir === currentDir) break;\n currentDir = parentDir;\n }\n}\n\nfunction toTempRelativePath(absolutePath: string): string {\n const { root } = path.parse(absolutePath);\n return path.relative(root, absolutePath);\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"],"m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C,GAAI,IAAc,EAAY,MAC9B,EAAa,CACf,CACF,CAEA,SAAS,EAAmB,EAA8B,CACxD,GAAM,CAAE,QAASA,EAAAA,QAAK,MAAM,CAAY,EACxC,OAAOA,EAAAA,QAAK,SAAS,EAAM,CAAY,CACzC,CAEA,SAAS,EAAqB,EAA4B,CACxD,IAAM,EAAa,QAAQ,KAAK,GAChC,GAAI,CAAC,EAAY,MAAO,IAAIA,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,WAAWI,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"}
|
|
@@ -6,3 +6,18 @@ export interface ProblemDirIsolationCheckResult {
|
|
|
6
6
|
* Check that a judge can run after only the problem directory is copied elsewhere.
|
|
7
7
|
*/
|
|
8
8
|
export declare function checkProblemDirIsolation(problemDir: string, resolvedCwd: ResolvedCwd, params: unknown): Promise<ProblemDirIsolationCheckResult>;
|
|
9
|
+
/**
|
|
10
|
+
* Copy a problem directory into a temporary root, symlinking every ancestor `node_modules` so the
|
|
11
|
+
* copied harness still resolves its imports. Callers must remove the returned `tempRoot`.
|
|
12
|
+
*/
|
|
13
|
+
export declare function copyProblemDirToTemporaryRoot(problemDir: string): Promise<{
|
|
14
|
+
tempRoot: string;
|
|
15
|
+
copiedProblemDir: string;
|
|
16
|
+
}>;
|
|
17
|
+
/**
|
|
18
|
+
* Remove a temporary directory even when judged code left permission-locked entries in it (e.g. a
|
|
19
|
+
* mode-000 directory makes a plain `fs.rm` fail with EACCES). Returns whether removal succeeded.
|
|
20
|
+
*/
|
|
21
|
+
export declare function forciblyRemoveDirectory(dir: string): Promise<boolean>;
|
|
22
|
+
/** Synchronous variant of {@link forciblyRemoveDirectory} for signal handlers. */
|
|
23
|
+
export declare function forciblyRemoveDirectorySync(dir: string): boolean;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{TEST_CASE_RESULT_PREFIX as e,testCaseResultSchema as t}from"../types/testCaseResult.js";import{DecisionCode as n}from"../types/decisionCode.js";import{printDebugBanner as r}from"./printDebugBanner.js";import i from"node:
|
|
1
|
+
import{TEST_CASE_RESULT_PREFIX as e,testCaseResultSchema as t}from"../types/testCaseResult.js";import{DecisionCode as n}from"../types/decisionCode.js";import{printDebugBanner as r}from"./printDebugBanner.js";import i from"node:path";import a from"node:child_process";import o from"node:fs";import s from"node:os";async function c(e,t,n){let o;try{let s=await l(e);o=s.tempRoot;let{copiedProblemDir:c}=s,u=i.resolve(e),d=i.relative(u,i.resolve(t.cwd)),f=i.join(c,d),p=y(u);if(p.startsWith(`..`)||i.isAbsolute(p))return r([`[DEBUG MODE] isolated problem directory check skipped`,``,`The invoked judge script is located outside the problem directory.`,`Script path: ${p}`]),{passed:!0};let m=process.execArgv.filter(h),_=JSON.stringify(g(n)?n:{}),v=a.spawnSync(process.execPath,[...m,p,f,_],{cwd:c,encoding:`utf8`,env:process.env,timeout:3e4}),x=v.stdout??``,S=v.stderr??``;return v.status===0&&b(x)?(r([`[DEBUG MODE] isolated problem directory check passed`,``,`Copied problem dir : ${c}`,`Checked cwd : ${d}`]),{passed:!0}):(r([`[DEBUG MODE] isolated problem directory check failed`,``,`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.`,``,`Copied problem dir : ${c}`,`Checked cwd : ${d}`,`Exit status : ${v.status??v.signal??`unknown`}`,`Spawn error : ${v.error?.message??`<none>`}`,``,`stdout:`,x.trimEnd()||`<empty>`,``,`stderr:`,S.trimEnd()||`<empty>`]),{passed:!1})}catch(e){return r([`[DEBUG MODE] isolated problem directory check failed due to an unexpected error`,``,e instanceof Error?e.message:String(e)]),{passed:!1}}finally{o&&await u(o)}}async function l(e){let t=await o.promises.mkdtemp(i.join(s.tmpdir(),`problem-utils-isolation_`));try{let n=i.resolve(e),r=i.join(t,v(n));return await o.promises.mkdir(i.dirname(r),{recursive:!0}),await o.promises.cp(n,r,{recursive:!0,filter:p,verbatimSymlinks:!0}),await _(t,n),{tempRoot:t,copiedProblemDir:r}}catch(e){throw await u(t),e}}async function u(e){try{return await o.promises.rm(e,{recursive:!0,force:!0}),!0}catch{f(e);try{return await o.promises.rm(e,{recursive:!0,force:!0}),!0}catch{return!1}}}function d(e){try{return o.rmSync(e,{recursive:!0,force:!0}),!0}catch{f(e);try{return o.rmSync(e,{recursive:!0,force:!0}),!0}catch{return!1}}}function f(e){process.platform!==`win32`&&a.spawnSync(`chmod`,[`-R`,`u+rwX`,e])}async function p(e){if(!m(e))return!1;if((await o.promises.lstat(e)).isSymbolicLink()&&i.isAbsolute(await o.promises.readlink(e)))throw Error(`${e} is an absolute symlink, which would escape the temporary copy; use a relative symlink`);return!0}function m(e){let t=i.basename(e);return t!==`node_modules`&&t!==`.git`}function h(e){return!e.startsWith(`--inspect`)&&!e.startsWith(`--watch`)&&!e.startsWith(`--hot`)}function g(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}async function _(e,t){let n=i.resolve(t);for(;;){let t=i.join(n,`node_modules`);if(o.existsSync(t)){let r=i.join(e,v(n),`node_modules`);try{await o.promises.symlink(t,r,process.platform===`win32`?`junction`:`dir`)}catch{}}let r=i.dirname(n);if(r===n)break;n=r}}function v(e){let{root:t}=i.parse(e);return i.relative(t,e)}function y(e){let t=process.argv[1];if(!t)return`.${i.sep}judge.ts`;let n=i.relative(e,i.resolve(t));return i.isAbsolute(n)||n.startsWith(`.`)?n:`.${i.sep}${n}`}function b(r){let i=r.split(/\r?\n/).filter(t=>t.startsWith(e));return i.length!==0&&i.every(r=>{try{let i=t.safeParse(JSON.parse(r.slice(e.length)));return i.success&&i.data.decisionCode===n.ACCEPTED}catch{return!1}})}export{c as checkProblemDirIsolation,l as copyProblemDirToTemporaryRoot,u as forciblyRemoveDirectory,d as forciblyRemoveDirectorySync};
|
|
2
2
|
//# sourceMappingURL=checkProblemDirIsolation.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checkProblemDirIsolation.js","names":[],"sources":["../../src/helpers/checkProblemDirIsolation.ts"],"sourcesContent":["import child_process from 'node:child_process';\nimport fs from 'node:fs';\nimport os from 'node:os';\nimport 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';\n\nconst ISOLATION_CHECK_TIMEOUT_MS = 30_000;\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): Promise<ProblemDirIsolationCheckResult> {\n let tempRoot: string | undefined;\n try {\n tempRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'problem-utils-isolation_'));\n const absoluteProblemDir = path.resolve(problemDir);\n const copiedProblemDir = path.join(tempRoot, toTempRelativePath(absoluteProblemDir));\n await fs.promises.mkdir(path.dirname(copiedProblemDir), { recursive: true });\n await fs.promises.cp(absoluteProblemDir, copiedProblemDir, {\n recursive: true,\n filter: isCopiedProblemPath,\n });\n await symlinkAllAncestorNodeModules(tempRoot, absoluteProblemDir);\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 spawnResult = child_process.spawnSync(process.execPath, [...execArgv, scriptPath, copiedCwd, paramsJson], {\n cwd: copiedProblemDir,\n encoding: 'utf8',\n env: process.env,\n timeout: ISOLATION_CHECK_TIMEOUT_MS,\n });\n const stdout = spawnResult.stdout ?? '';\n const stderr = spawnResult.stderr ?? '';\n\n if (spawnResult.status === 0 && isAcceptedJudgeOutput(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 '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 `Copied problem dir : ${copiedProblemDir}`,\n `Checked cwd : ${relativeCwd}`,\n `Exit status : ${spawnResult.status ?? spawnResult.signal ?? 'unknown'}`,\n `Spawn error : ${spawnResult.error?.message ?? '<none>'}`,\n '',\n 'stdout:',\n stdout.trimEnd() || '<empty>',\n '',\n 'stderr:',\n 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 if (tempRoot) {\n try {\n await fs.promises.rm(tempRoot, { recursive: true, force: true });\n } catch {\n // Cleanup errors should not mask the primary isolation check result.\n }\n }\n }\n}\n\nfunction isCopiedProblemPath(src: string): boolean {\n const name = path.basename(src);\n return name !== 'node_modules' && name !== '.git';\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\nasync function symlinkAllAncestorNodeModules(tempRoot: string, problemDir: string): Promise<void> {\n let currentDir = path.resolve(problemDir);\n while (true) {\n const nodeModulesPath = path.join(currentDir, 'node_modules');\n if (fs.existsSync(nodeModulesPath)) {\n const targetSymlinkPath = path.join(tempRoot, toTempRelativePath(currentDir), 'node_modules');\n try {\n await fs.promises.symlink(\n nodeModulesPath,\n targetSymlinkPath,\n process.platform === 'win32' ? 'junction' : 'dir'\n );\n } catch {\n // Package resolution is best-effort; the isolation check still reports a clear spawn failure if imports break.\n }\n }\n\n const parentDir = path.dirname(currentDir);\n if (parentDir === currentDir) break;\n currentDir = parentDir;\n }\n}\n\nfunction toTempRelativePath(absolutePath: string): string {\n const { root } = path.parse(absolutePath);\n return path.relative(root, absolutePath);\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":"yTAoBA,eAAsB,EACpB,EACA,EACA,EACyC,CACzC,IAAI,EACJ,GAAI,CACF,EAAW,MAAM,EAAG,SAAS,QAAQ,EAAK,KAAK,EAAG,OAAO,EAAG,0BAA0B,CAAC,EACvF,IAAM,EAAqB,EAAK,QAAQ,CAAU,EAC5C,EAAmB,EAAK,KAAK,EAAU,EAAmB,CAAkB,CAAC,EACnF,MAAM,EAAG,SAAS,MAAM,EAAK,QAAQ,CAAgB,EAAG,CAAE,UAAW,EAAK,CAAC,EAC3E,MAAM,EAAG,SAAS,GAAG,EAAoB,EAAkB,CACzD,UAAW,GACX,OAAQ,CACV,CAAC,EACD,MAAM,EAA8B,EAAU,CAAkB,EAEhE,IAAM,EAAc,EAAK,SAAS,EAAoB,EAAK,QAAQ,EAAY,GAAG,CAAC,EAC7E,EAAY,EAAK,KAAK,EAAkB,CAAW,EACnD,EAAa,EAAqB,CAAkB,EAC1D,GAAI,EAAW,WAAW,IAAI,GAAK,EAAK,WAAW,CAAU,EAO3D,OANA,EAAiB,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,EAAc,EAAc,UAAU,QAAQ,SAAU,CAAC,GAAG,EAAU,EAAY,EAAW,CAAU,EAAG,CAC9G,IAAK,EACL,SAAU,OACV,IAAK,QAAQ,IACb,QAAS,GACX,CAAC,EACK,EAAS,EAAY,QAAU,GAC/B,EAAS,EAAY,QAAU,GA6BrC,OA3BI,EAAY,SAAW,GAAK,EAAsB,CAAM,GAC1D,EAAiB,CACf,uDACA,GACA,wBAAwB,IACxB,wBAAwB,GAC1B,CAA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U,EACzC,GAAI,IAAc,EAAY,MAC9B,EAAa,CACf,CACF,CAEA,SAAS,EAAmB,EAA8B,CACxD,GAAM,CAAE,QAAS,EAAK,MAAM,CAAY,EACxC,OAAO,EAAK,SAAS,EAAM,CAAY,CACzC,CAEA,SAAS,EAAqB,EAA4B,CACxD,IAAM,EAAa,QAAQ,KAAK,GAChC,GAAI,CAAC,EAAY,MAAO,IAAI,EAAK,IAAI,UACrC,IAAM,EAAqB,EAAK,SAAS,EAAY,EAAK,QAAQ,CAAU,CAAC,EAE7E,OADI,EAAK,WAAW,CAAkB,GAC/B,EAAmB,WAAW,GAAG,EADQ,EACiB,IAAI,EAAK,MAAM,GAClF,CAEA,SAAS,EAAsB,EAAyB,CACtD,IAAM,EAAc,EAAO,MAAM,OAAO,CAAC,CAAC,OAAQ,GAAS,EAAK,WAAW,CAAuB,CAAC,EAGnG,OAFI,EAAY,SAAW,GAEpB,EAAY,MAAO,GAAS,CACjC,GAAI,CACF,IAAM,EAAe,EAAqB,UAAU,KAAK,MAAM,EAAK,MAAM,EAAwB,MAAM,CAAC,CAAC,EAC1G,OAAO,EAAa,SAAW,EAAa,KAAK,eAAiB,EAAa,QACjF,MAAQ,CACN,MAAO,EACT,CACF,CAAC,CACH"}
|
|
1
|
+
{"version":3,"file":"checkProblemDirIsolation.js","names":[],"sources":["../../src/helpers/checkProblemDirIsolation.ts"],"sourcesContent":["import child_process from 'node:child_process';\nimport fs from 'node:fs';\nimport os from 'node:os';\nimport 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';\n\nconst ISOLATION_CHECK_TIMEOUT_MS = 30_000;\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): Promise<ProblemDirIsolationCheckResult> {\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 spawnResult = child_process.spawnSync(process.execPath, [...execArgv, scriptPath, copiedCwd, paramsJson], {\n cwd: copiedProblemDir,\n encoding: 'utf8',\n env: process.env,\n timeout: ISOLATION_CHECK_TIMEOUT_MS,\n });\n const stdout = spawnResult.stdout ?? '';\n const stderr = spawnResult.stderr ?? '';\n\n if (spawnResult.status === 0 && isAcceptedJudgeOutput(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 '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 `Copied problem dir : ${copiedProblemDir}`,\n `Checked cwd : ${relativeCwd}`,\n `Exit status : ${spawnResult.status ?? spawnResult.signal ?? 'unknown'}`,\n `Spawn error : ${spawnResult.error?.message ?? '<none>'}`,\n '',\n 'stdout:',\n stdout.trimEnd() || '<empty>',\n '',\n 'stderr:',\n 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\n/**\n * Copy a problem directory into a temporary root, symlinking every ancestor `node_modules` so the\n * copied harness still resolves its imports. Callers must remove the returned `tempRoot`.\n */\nexport async function copyProblemDirToTemporaryRoot(\n problemDir: string\n): Promise<{ tempRoot: string; copiedProblemDir: string }> {\n const tempRoot = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'problem-utils-isolation_'));\n try {\n const absoluteProblemDir = path.resolve(problemDir);\n const copiedProblemDir = path.join(tempRoot, toTempRelativePath(absoluteProblemDir));\n await fs.promises.mkdir(path.dirname(copiedProblemDir), { recursive: true });\n await fs.promises.cp(absoluteProblemDir, copiedProblemDir, {\n recursive: true,\n filter: rejectAbsoluteSymlinks,\n // Keep relative symlinks relative: the default rewrites them to absolute paths into the\n // source tree, so judging the copy could write through them into the checked repository.\n verbatimSymlinks: true,\n });\n await symlinkAllAncestorNodeModules(tempRoot, absoluteProblemDir);\n return { tempRoot, copiedProblemDir };\n } catch (error) {\n await forciblyRemoveDirectory(tempRoot);\n throw error;\n }\n}\n\n/**\n * Remove a temporary directory even when judged code left permission-locked entries in it (e.g. a\n * mode-000 directory makes a plain `fs.rm` fail with EACCES). Returns whether removal succeeded.\n */\nexport async function forciblyRemoveDirectory(dir: string): Promise<boolean> {\n try {\n await fs.promises.rm(dir, { recursive: true, force: true });\n return true;\n } catch {\n unlockPermissions(dir);\n try {\n await fs.promises.rm(dir, { recursive: true, force: true });\n return true;\n } catch {\n return false;\n }\n }\n}\n\n/** Synchronous variant of {@link forciblyRemoveDirectory} for signal handlers. */\nexport function forciblyRemoveDirectorySync(dir: string): boolean {\n try {\n fs.rmSync(dir, { recursive: true, force: true });\n return true;\n } catch {\n unlockPermissions(dir);\n try {\n fs.rmSync(dir, { recursive: true, force: true });\n return true;\n } catch {\n return false;\n }\n }\n}\n\n// chmod does not exist on Windows, where POSIX-mode locks cannot occur anyway. spawnSync reports\n// spawn failures via its return value instead of throwing, so no try/catch is needed.\nfunction unlockPermissions(dir: string): void {\n if (process.platform !== 'win32') child_process.spawnSync('chmod', ['-R', 'u+rwX', dir]);\n}\n\n// An absolute symlink is copied verbatim, so judging the copy could write through it into the\n// original tree; reject it instead of silently breaking the isolation guarantee.\nasync function rejectAbsoluteSymlinks(src: string): Promise<boolean> {\n if (!isCopiedProblemPath(src)) return false;\n const stats = await fs.promises.lstat(src);\n if (stats.isSymbolicLink() && path.isAbsolute(await fs.promises.readlink(src))) {\n throw new Error(`${src} is an absolute symlink, which would escape the temporary copy; use a relative symlink`);\n }\n return true;\n}\n\nfunction isCopiedProblemPath(src: string): boolean {\n const name = path.basename(src);\n return name !== 'node_modules' && name !== '.git';\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\nasync function symlinkAllAncestorNodeModules(tempRoot: string, problemDir: string): Promise<void> {\n let currentDir = path.resolve(problemDir);\n while (true) {\n const nodeModulesPath = path.join(currentDir, 'node_modules');\n if (fs.existsSync(nodeModulesPath)) {\n const targetSymlinkPath = path.join(tempRoot, toTempRelativePath(currentDir), 'node_modules');\n try {\n await fs.promises.symlink(\n nodeModulesPath,\n targetSymlinkPath,\n process.platform === 'win32' ? 'junction' : 'dir'\n );\n } catch {\n // Package resolution is best-effort; the isolation check still reports a clear spawn failure if imports break.\n }\n }\n\n const parentDir = path.dirname(currentDir);\n if (parentDir === currentDir) break;\n currentDir = parentDir;\n }\n}\n\nfunction toTempRelativePath(absolutePath: string): string {\n const { root } = path.parse(absolutePath);\n return path.relative(root, absolutePath);\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":"yTAoBA,eAAsB,EACpB,EACA,EACA,EACyC,CACzC,IAAI,EACJ,GAAI,CACF,IAAM,EAAa,MAAM,EAA8B,CAAU,EACjE,EAAW,EAAW,SACtB,GAAM,CAAE,oBAAqB,EACvB,EAAqB,EAAK,QAAQ,CAAU,EAE5C,EAAc,EAAK,SAAS,EAAoB,EAAK,QAAQ,EAAY,GAAG,CAAC,EAC7E,EAAY,EAAK,KAAK,EAAkB,CAAW,EACnD,EAAa,EAAqB,CAAkB,EAC1D,GAAI,EAAW,WAAW,IAAI,GAAK,EAAK,WAAW,CAAU,EAO3D,OANA,EAAiB,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,EAAc,EAAc,UAAU,QAAQ,SAAU,CAAC,GAAG,EAAU,EAAY,EAAW,CAAU,EAAG,CAC9G,IAAK,EACL,SAAU,OACV,IAAK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AU,EACxC,OAAa,CACX,IAAM,EAAkB,EAAK,KAAK,EAAY,cAAc,EAC5D,GAAI,EAAG,WAAW,CAAe,EAAG,CAClC,IAAM,EAAoB,EAAK,KAAK,EAAU,EAAmB,CAAU,EAAG,cAAc,EAC5F,GAAI,CACF,MAAM,EAAG,SAAS,QAChB,EACA,EACA,QAAQ,WAAa,QAAU,WAAa,KAC9C,CACF,MAAQ,CAER,CACF,CAEA,IAAM,EAAY,EAAK,QAAQ,CAAU,EACzC,GAAI,IAAc,EAAY,MAC9B,EAAa,CACf,CACF,CAEA,SAAS,EAAmB,EAA8B,CACxD,GAAM,CAAE,QAAS,EAAK,MAAM,CAAY,EACxC,OAAO,EAAK,SAAS,EAAM,CAAY,CACzC,CAEA,SAAS,EAAqB,EAA4B,CACxD,IAAM,EAAa,QAAQ,KAAK,GAChC,GAAI,CAAC,EAAY,MAAO,IAAI,EAAK,IAAI,UACrC,IAAM,EAAqB,EAAK,SAAS,EAAY,EAAK,QAAQ,CAAU,CAAC,EAE7E,OADI,EAAK,WAAW,CAAkB,GAC/B,EAAmB,WAAW,GAAG,EADQ,EACiB,IAAI,EAAK,MAAM,GAClF,CAEA,SAAS,EAAsB,EAAyB,CACtD,IAAM,EAAc,EAAO,MAAM,OAAO,CAAC,CAAC,OAAQ,GAAS,EAAK,WAAW,CAAuB,CAAC,EAGnG,OAFI,EAAY,SAAW,GAEpB,EAAY,MAAO,GAAS,CACjC,GAAI,CACF,IAAM,EAAe,EAAqB,UAAU,KAAK,MAAM,EAAK,MAAM,EAAwB,MAAM,CAAC,CAAC,EAC1G,OAAO,EAAa,SAAW,EAAa,KAAK,eAAiB,EAAa,QACjF,MAAQ,CACN,MAAO,EACT,CACF,CAAC,CACH"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("./sandboxUser.cjs");let n=require("node:
|
|
1
|
+
"use strict";const e=require("../_virtual/_rolldown/runtime.cjs"),t=require("./sandboxUser.cjs");let n=require("node:path");n=e.__toESM(n,1);let r=require("node:fs");r=e.__toESM(r,1);async function i(e){let t=new Map;return await o(e,e,t),t}async function a(e,t){await s(e,e,t)}async function o(e,t,r){for(let i of await l(t)){let a=n.default.join(t,i.name);r.set(n.default.relative(e,a),c(i)),i.isDirectory()&&await o(e,a,r)}}async function s(e,r,i){for(let a of await l(r)){let o=n.default.join(r,a.name),l=i.get(n.default.relative(e,o));if(l===void 0||l!==c(a)){await t.forceRemove(o);continue}a.isDirectory()&&await s(e,o,i)}}function c(e){return e.isSymbolicLink()?`symlink`:e.isDirectory()?`directory`:e.isFile()?`file`:`other`}async function l(e){try{return await r.default.promises.readdir(e,{withFileTypes:!0})}catch(n){if(!t.sandboxUserName)throw n;return t.relaxPermissionsAsSandboxUser(e),await r.default.promises.readdir(e,{withFileTypes:!0})}}exports.cleanWorkingDirectory=a,exports.snapshotWorkingDirectory=i;
|
|
2
2
|
//# sourceMappingURL=cleanWorkingDirectory.cjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{forceRemove as e,relaxPermissionsAsSandboxUser as t,sandboxUserName as n}from"./sandboxUser.js";import r from"node:
|
|
1
|
+
import{forceRemove as e,relaxPermissionsAsSandboxUser as t,sandboxUserName as n}from"./sandboxUser.js";import r from"node:path";import i from"node:fs";async function a(e){let t=new Map;return await s(e,e,t),t}async function o(e,t){await c(e,e,t)}async function s(e,t,n){for(let i of await u(t)){let a=r.join(t,i.name);n.set(r.relative(e,a),l(i)),i.isDirectory()&&await s(e,a,n)}}async function c(t,n,i){for(let a of await u(n)){let o=r.join(n,a.name),s=i.get(r.relative(t,o));if(s===void 0||s!==l(a)){await e(o);continue}a.isDirectory()&&await c(t,o,i)}}function l(e){return e.isSymbolicLink()?`symlink`:e.isDirectory()?`directory`:e.isFile()?`file`:`other`}async function u(e){try{return await i.promises.readdir(e,{withFileTypes:!0})}catch(r){if(!n)throw r;return t(e),await i.promises.readdir(e,{withFileTypes:!0})}}export{o as cleanWorkingDirectory,a as snapshotWorkingDirectory};
|
|
2
2
|
//# sourceMappingURL=cleanWorkingDirectory.js.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";const e=require("../_virtual/_rolldown/runtime.cjs");let t=require("node:fs/promises");t=e.__toESM(t,1);let n=require("node:path");n=e.__toESM(n,1);const r={"judge.ts":`stdioJudgePreset`,"debug.ts":`stdioDebugPreset`};async function i(e){let t=await a(n.default.join(e,`judge.ts`)),i=[];if(t!==void 0&&o(t,r[`judge.ts`])&&i.push(`judge.ts`),t===void 0||i.length!==0){let t=await a(n.default.join(e,`debug.ts`));t!==void 0&&o(t,r[`debug.ts`])&&i.push(`debug.ts`)}return i}async function a(e){try{return await t.default.readFile(e,`utf8`)}catch{return}}function o(e,t){let n=new RegExp(String.raw`^\s*import\s*\{\s*${t}(?:\s+as\s+([A-Za-z_$][\w$]*))?\s*,?\s*\}\s*from\s*`+String.raw`(['"])(?:@exercode/problem-utils|judge-utils)/presets/stdio(?:\.js)?\2\s*;?\s*`).exec(e);if(!n)return!1;let r=(n[1]??t).replaceAll(`$`,String.raw`\$`);return new RegExp(String.raw`^await\s+${r}\s*\(\s*import\.meta\.dirname\s*\)\s*;?\s*$`).test(e.slice(n[0].length))}exports.HARNESS_FILE_PRESETS=r,exports.findDefaultStdioHarnessFiles=i,exports.isDefaultStdioHarnessSource=o;
|
|
2
|
+
//# sourceMappingURL=defaultStdioHarness.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaultStdioHarness.cjs","names":["path","fs"],"sources":["../../src/helpers/defaultStdioHarness.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\n\nexport const HARNESS_FILE_PRESETS = {\n 'judge.ts': 'stdioJudgePreset',\n 'debug.ts': 'stdioDebugPreset',\n} as const;\n\nexport type HarnessFileName = keyof typeof HARNESS_FILE_PRESETS;\n\n/**\n * Find harness files (`judge.ts` / `debug.ts`) in a problem directory whose content is identical to\n * the default stdio harness. Such files must not be committed: a problem without `judge.ts` is\n * judged with `stdioJudgePreset` (and debugged with `stdioDebugPreset`) automatically, and\n * committed copies would drift from the server's defaults.\n */\nexport async function findDefaultStdioHarnessFiles(problemDir: string): Promise<HarnessFileName[]> {\n const judgeContent = await readFileOrUndefined(path.join(problemDir, 'judge.ts'));\n const foundFileNames: HarnessFileName[] = [];\n if (judgeContent !== undefined && isDefaultStdioHarnessSource(judgeContent, HARNESS_FILE_PRESETS['judge.ts'])) {\n foundFileNames.push('judge.ts');\n }\n // A problem with a custom judge.ts gets no stdioDebugPreset fallback, so it must commit its own\n // debug.ts — even one whose content matches the default stdio debug harness.\n const hasCustomJudge = judgeContent !== undefined && foundFileNames.length === 0;\n if (!hasCustomJudge) {\n const debugContent = await readFileOrUndefined(path.join(problemDir, 'debug.ts'));\n if (debugContent !== undefined && isDefaultStdioHarnessSource(debugContent, HARNESS_FILE_PRESETS['debug.ts'])) {\n foundFileNames.push('debug.ts');\n }\n }\n return foundFileNames;\n}\n\nasync function readFileOrUndefined(filePath: string): Promise<string | undefined> {\n try {\n return await fs.readFile(filePath, 'utf8');\n } catch {\n return undefined;\n }\n}\n\n/**\n * Whether the source consists only of importing the given stdio preset from this package (or its\n * legacy `judge-utils` name) and awaiting it, i.e. it is semantically identical to the default\n * harness the server generates. Comments count as a difference on purpose: a harness kept for\n * demonstration can add an explanatory comment to be treated as custom.\n */\nexport function isDefaultStdioHarnessSource(source: string, presetName: string): boolean {\n const importRegex = new RegExp(\n String.raw`^\\s*import\\s*\\{\\s*${presetName}(?:\\s+as\\s+([A-Za-z_$][\\w$]*))?\\s*,?\\s*\\}\\s*from\\s*` +\n String.raw`(['\"])(?:@exercode/problem-utils|judge-utils)/presets/stdio(?:\\.js)?\\2\\s*;?\\s*`\n );\n const importMatch = importRegex.exec(source);\n if (!importMatch) return false;\n const localPresetName = (importMatch[1] ?? presetName).replaceAll('$', String.raw`\\$`);\n const callRegex = new RegExp(String.raw`^await\\s+${localPresetName}\\s*\\(\\s*import\\.meta\\.dirname\\s*\\)\\s*;?\\s*$`);\n return callRegex.test(source.slice(importMatch[0].length));\n}\n"],"mappings":"iKAGA,MAAa,EAAuB,CAClC,WAAY,mBACZ,WAAY,kBACd,EAUA,eAAsB,EAA6B,EAAgD,CACjG,IAAM,EAAe,MAAM,EAAoBA,EAAAA,QAAK,KAAK,EAAY,UAAU,CAAC,EAC1E,EAAoC,CAAC,EAO3C,GANI,IAAiB,IAAA,IAAa,EAA4B,EAAc,EAAqB,WAAW,GAC1G,EAAe,KAAK,UAAU,EAIT,IAAiB,IAAA,IAAa,EAAe,SAAW,EAC1D,CACnB,IAAM,EAAe,MAAM,EAAoBA,EAAAA,QAAK,KAAK,EAAY,UAAU,CAAC,EAC5E,IAAiB,IAAA,IAAa,EAA4B,EAAc,EAAqB,WAAW,GAC1G,EAAe,KAAK,UAAU,CAElC,CACA,OAAO,CACT,CAEA,eAAe,EAAoB,EAA+C,CAChF,GAAI,CACF,OAAO,MAAMC,EAAAA,QAAG,SAAS,EAAU,MAAM,CAC3C,MAAQ,CACN,MACF,CACF,CAQA,SAAgB,EAA4B,EAAgB,EAA6B,CAKvF,IAAM,EAAc,IAJI,OACtB,OAAO,GAAG,qBAAqB,EAAW,qDACxC,OAAO,GAAG,gFAEgB,CAAC,CAAC,KAAK,CAAM,EAC3C,GAAI,CAAC,EAAa,MAAO,GACzB,IAAM,GAAmB,EAAY,IAAM,EAAA,CAAY,WAAW,IAAK,OAAO,GAAG,IAAI,EAErF,OAAO,IADe,OAAO,OAAO,GAAG,YAAY,EAAgB,4CACpD,CAAC,CAAC,KAAK,EAAO,MAAM,EAAY,EAAE,CAAC,MAAM,CAAC,CAC3D"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const HARNESS_FILE_PRESETS: {
|
|
2
|
+
readonly 'judge.ts': 'stdioJudgePreset';
|
|
3
|
+
readonly 'debug.ts': 'stdioDebugPreset';
|
|
4
|
+
};
|
|
5
|
+
export type HarnessFileName = keyof typeof HARNESS_FILE_PRESETS;
|
|
6
|
+
/**
|
|
7
|
+
* Find harness files (`judge.ts` / `debug.ts`) in a problem directory whose content is identical to
|
|
8
|
+
* the default stdio harness. Such files must not be committed: a problem without `judge.ts` is
|
|
9
|
+
* judged with `stdioJudgePreset` (and debugged with `stdioDebugPreset`) automatically, and
|
|
10
|
+
* committed copies would drift from the server's defaults.
|
|
11
|
+
*/
|
|
12
|
+
export declare function findDefaultStdioHarnessFiles(problemDir: string): Promise<HarnessFileName[]>;
|
|
13
|
+
/**
|
|
14
|
+
* Whether the source consists only of importing the given stdio preset from this package (or its
|
|
15
|
+
* legacy `judge-utils` name) and awaiting it, i.e. it is semantically identical to the default
|
|
16
|
+
* harness the server generates. Comments count as a difference on purpose: a harness kept for
|
|
17
|
+
* demonstration can add an explanatory comment to be treated as custom.
|
|
18
|
+
*/
|
|
19
|
+
export declare function isDefaultStdioHarnessSource(source: string, presetName: string): boolean;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import e from"node:fs/promises";import t from"node:path";const n={"judge.ts":`stdioJudgePreset`,"debug.ts":`stdioDebugPreset`};async function r(e){let r=await i(t.join(e,`judge.ts`)),o=[];if(r!==void 0&&a(r,n[`judge.ts`])&&o.push(`judge.ts`),r===void 0||o.length!==0){let r=await i(t.join(e,`debug.ts`));r!==void 0&&a(r,n[`debug.ts`])&&o.push(`debug.ts`)}return o}async function i(t){try{return await e.readFile(t,`utf8`)}catch{return}}function a(e,t){let n=new RegExp(String.raw`^\s*import\s*\{\s*${t}(?:\s+as\s+([A-Za-z_$][\w$]*))?\s*,?\s*\}\s*from\s*`+String.raw`(['"])(?:@exercode/problem-utils|judge-utils)/presets/stdio(?:\.js)?\2\s*;?\s*`).exec(e);if(!n)return!1;let r=(n[1]??t).replaceAll(`$`,String.raw`\$`);return new RegExp(String.raw`^await\s+${r}\s*\(\s*import\.meta\.dirname\s*\)\s*;?\s*$`).test(e.slice(n[0].length))}export{n as HARNESS_FILE_PRESETS,r as findDefaultStdioHarnessFiles,a as isDefaultStdioHarnessSource};
|
|
2
|
+
//# sourceMappingURL=defaultStdioHarness.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaultStdioHarness.js","names":[],"sources":["../../src/helpers/defaultStdioHarness.ts"],"sourcesContent":["import fs from 'node:fs/promises';\nimport path from 'node:path';\n\nexport const HARNESS_FILE_PRESETS = {\n 'judge.ts': 'stdioJudgePreset',\n 'debug.ts': 'stdioDebugPreset',\n} as const;\n\nexport type HarnessFileName = keyof typeof HARNESS_FILE_PRESETS;\n\n/**\n * Find harness files (`judge.ts` / `debug.ts`) in a problem directory whose content is identical to\n * the default stdio harness. Such files must not be committed: a problem without `judge.ts` is\n * judged with `stdioJudgePreset` (and debugged with `stdioDebugPreset`) automatically, and\n * committed copies would drift from the server's defaults.\n */\nexport async function findDefaultStdioHarnessFiles(problemDir: string): Promise<HarnessFileName[]> {\n const judgeContent = await readFileOrUndefined(path.join(problemDir, 'judge.ts'));\n const foundFileNames: HarnessFileName[] = [];\n if (judgeContent !== undefined && isDefaultStdioHarnessSource(judgeContent, HARNESS_FILE_PRESETS['judge.ts'])) {\n foundFileNames.push('judge.ts');\n }\n // A problem with a custom judge.ts gets no stdioDebugPreset fallback, so it must commit its own\n // debug.ts — even one whose content matches the default stdio debug harness.\n const hasCustomJudge = judgeContent !== undefined && foundFileNames.length === 0;\n if (!hasCustomJudge) {\n const debugContent = await readFileOrUndefined(path.join(problemDir, 'debug.ts'));\n if (debugContent !== undefined && isDefaultStdioHarnessSource(debugContent, HARNESS_FILE_PRESETS['debug.ts'])) {\n foundFileNames.push('debug.ts');\n }\n }\n return foundFileNames;\n}\n\nasync function readFileOrUndefined(filePath: string): Promise<string | undefined> {\n try {\n return await fs.readFile(filePath, 'utf8');\n } catch {\n return undefined;\n }\n}\n\n/**\n * Whether the source consists only of importing the given stdio preset from this package (or its\n * legacy `judge-utils` name) and awaiting it, i.e. it is semantically identical to the default\n * harness the server generates. Comments count as a difference on purpose: a harness kept for\n * demonstration can add an explanatory comment to be treated as custom.\n */\nexport function isDefaultStdioHarnessSource(source: string, presetName: string): boolean {\n const importRegex = new RegExp(\n String.raw`^\\s*import\\s*\\{\\s*${presetName}(?:\\s+as\\s+([A-Za-z_$][\\w$]*))?\\s*,?\\s*\\}\\s*from\\s*` +\n String.raw`(['\"])(?:@exercode/problem-utils|judge-utils)/presets/stdio(?:\\.js)?\\2\\s*;?\\s*`\n );\n const importMatch = importRegex.exec(source);\n if (!importMatch) return false;\n const localPresetName = (importMatch[1] ?? presetName).replaceAll('$', String.raw`\\$`);\n const callRegex = new RegExp(String.raw`^await\\s+${localPresetName}\\s*\\(\\s*import\\.meta\\.dirname\\s*\\)\\s*;?\\s*$`);\n return callRegex.test(source.slice(importMatch[0].length));\n}\n"],"mappings":"yDAGA,MAAa,EAAuB,CAClC,WAAY,mBACZ,WAAY,kBACd,EAUA,eAAsB,EAA6B,EAAgD,CACjG,IAAM,EAAe,MAAM,EAAoB,EAAK,KAAK,EAAY,UAAU,CAAC,EAC1E,EAAoC,CAAC,EAO3C,GANI,IAAiB,IAAA,IAAa,EAA4B,EAAc,EAAqB,WAAW,GAC1G,EAAe,KAAK,UAAU,EAIT,IAAiB,IAAA,IAAa,EAAe,SAAW,EAC1D,CACnB,IAAM,EAAe,MAAM,EAAoB,EAAK,KAAK,EAAY,UAAU,CAAC,EAC5E,IAAiB,IAAA,IAAa,EAA4B,EAAc,EAAqB,WAAW,GAC1G,EAAe,KAAK,UAAU,CAElC,CACA,OAAO,CACT,CAEA,eAAe,EAAoB,EAA+C,CAChF,GAAI,CACF,OAAO,MAAM,EAAG,SAAS,EAAU,MAAM,CAC3C,MAAQ,CACN,MACF,CACF,CAQA,SAAgB,EAA4B,EAAgB,EAA6B,CAKvF,IAAM,EAAc,IAJI,OACtB,OAAO,GAAG,qBAAqB,EAAW,qDACxC,OAAO,GAAG,gFAEgB,CAAC,CAAC,KAAK,CAAM,EAC3C,GAAI,CAAC,EAAa,MAAO,GACzB,IAAM,GAAmB,EAAY,IAAM,EAAA,CAAY,WAAW,IAAK,OAAO,GAAG,IAAI,EAErF,OAAO,IADe,OAAO,OAAO,GAAG,YAAY,EAAgB,4CACpD,CAAC,CAAC,KAAK,EAAO,MAAM,EAAY,EAAE,CAAC,MAAM,CAAC,CAC3D"}
|